]> git.pld-linux.org Git - packages/kernel.git/blame - kernel-aufs4.patch
- unconditional noarch packages
[packages/kernel.git] / kernel-aufs4.patch
CommitLineData
2121bcd9 1SPDX-License-Identifier: GPL-2.0
e49925d1 2aufs4.19 kbuild patch
7f207e10
AM
3
4diff --git a/fs/Kconfig b/fs/Kconfig
0b487229 5index ac474a61be379..284cee954591e 100644
7f207e10
AM
6--- a/fs/Kconfig
7+++ b/fs/Kconfig
cd7a4cd9 8@@ -255,6 +255,7 @@ source "fs/pstore/Kconfig"
5527c038 9 source "fs/sysv/Kconfig"
7e9cd9fe 10 source "fs/ufs/Kconfig"
7f207e10
AM
11 source "fs/exofs/Kconfig"
12+source "fs/aufs/Kconfig"
13
14 endif # MISC_FILESYSTEMS
15
16diff --git a/fs/Makefile b/fs/Makefile
0b487229 17index 293733f61594b..12d19d0de07a6 100644
7f207e10
AM
18--- a/fs/Makefile
19+++ b/fs/Makefile
be118d29 20@@ -128,3 +128,4 @@ obj-y += exofs/ # Multiple modules
7f207e10 21 obj-$(CONFIG_CEPH_FS) += ceph/
bf0370f2 22 obj-$(CONFIG_PSTORE) += pstore/
c06a8ce3 23 obj-$(CONFIG_EFIVAR_FS) += efivarfs/
86dc4139 24+obj-$(CONFIG_AUFS_FS) += aufs/
2121bcd9 25SPDX-License-Identifier: GPL-2.0
e49925d1 26aufs4.19 base patch
7f207e10 27
c1595e42 28diff --git a/MAINTAINERS b/MAINTAINERS
0b487229 29index b2f710eee67a7..8449c695ba0b1 100644
c1595e42
JR
30--- a/MAINTAINERS
31+++ b/MAINTAINERS
acd2b654 32@@ -2605,6 +2605,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)
0b487229 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
0b487229 53index ea9debf59b225..9e534a36c5941 100644
392086de
AM
54--- a/drivers/block/loop.c
55+++ b/drivers/block/loop.c
acd2b654 56@@ -739,6 +739,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
0b487229 82index 2e7e8d85e9b40..9f57bd87bce5a 100644
c1595e42
JR
83--- a/fs/dcache.c
84+++ b/fs/dcache.c
acd2b654 85@@ -1238,7 +1238,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
0b487229 95index 4137d96534a6c..d4da10be555c9 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)
0b487229 103+int setfl(int fd, struct file *filp, unsigned long arg)
febd17d6
JR
104 {
105 struct inode * inode = file_inode(filp);
106 int error = 0;
2121bcd9 107@@ -63,6 +63,8 @@ static int setfl(int fd, struct file * filp, unsigned long arg)
febd17d6
JR
108
109 if (filp->f_op->check_flags)
110 error = filp->f_op->check_flags(arg);
111+ if (!error && filp->f_op->setfl)
112+ error = filp->f_op->setfl(filp, arg);
113 if (error)
114 return error;
115
5afbbe0d 116diff --git a/fs/inode.c b/fs/inode.c
0b487229 117index 42f6d25f32a52..fa6ae6a217fbd 100644
5afbbe0d
AM
118--- a/fs/inode.c
119+++ b/fs/inode.c
acd2b654 120@@ -1657,7 +1657,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
0b487229 130index 99186556f8d34..72c93f3e86478 100644
8b6a4947
AM
131--- a/fs/namespace.c
132+++ b/fs/namespace.c
acd2b654 133@@ -770,6 +770,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
0b487229 147index 8a2737f0d61d3..45e3e7a0d4e64 100644
5527c038
JR
148--- a/fs/read_write.c
149+++ b/fs/read_write.c
b00004a5 150@@ -489,6 +489,28 @@ ssize_t __vfs_write(struct file *file, const char __user *p, size_t count,
8b6a4947 151 return -EINVAL;
5527c038 152 }
5527c038
JR
153
154+vfs_readf_t vfs_readf(struct file *file)
155+{
156+ const struct file_operations *fop = file->f_op;
157+
158+ if (fop->read)
159+ return fop->read;
160+ if (fop->read_iter)
161+ return new_sync_read;
0b487229 162+ return ERR_PTR(-ENOSYS); /* doesn't have ->read(|_iter)() op */
5527c038
JR
163+}
164+
165+vfs_writef_t vfs_writef(struct file *file)
166+{
167+ const struct file_operations *fop = file->f_op;
168+
169+ if (fop->write)
170+ return fop->write;
171+ if (fop->write_iter)
172+ return new_sync_write;
0b487229 173+ return ERR_PTR(-ENOSYS); /* doesn't have ->write(|_iter)() op */
5527c038
JR
174+}
175+
8b6a4947 176 ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t *pos)
5527c038
JR
177 {
178 mm_segment_t old_fs;
7f207e10 179diff --git a/fs/splice.c b/fs/splice.c
0b487229 180index b3daa971f5977..1dd7f96b22dc4 100644
7f207e10
AM
181--- a/fs/splice.c
182+++ b/fs/splice.c
cd7a4cd9 183@@ -838,8 +838,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);
cd7a4cd9 194@@ -855,9 +855,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
0b487229 208index b54e0541ad899..28607828e96f2 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
0b487229 221index 897eae8faee1b..41da4219febfe 100644
5527c038
JR
222--- a/include/linux/fs.h
223+++ b/include/linux/fs.h
acd2b654 224@@ -1286,6 +1286,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
0b487229 228+extern int setfl(int fd, struct file *filp, unsigned long arg);
febd17d6 229 extern void __f_setown(struct file *filp, struct pid *, enum pid_type, int force);
3c1bdaff 230 extern int f_setown(struct file *filp, unsigned long arg, int force);
febd17d6 231 extern void f_delown(struct file *filp);
acd2b654 232@@ -1747,6 +1748,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);
acd2b654 240@@ -1818,6 +1820,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 *);
acd2b654 253@@ -2243,6 +2251,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;
acd2b654 261@@ -2530,6 +2539,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
0b487229 270index b0d0b51c4d850..f73ffaa0199ee 100644
8b6a4947
AM
271--- a/include/linux/lockdep.h
272+++ b/include/linux/lockdep.h
be118d29 273@@ -313,6 +313,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 *
be118d29 282@@ -439,6 +441,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
0b487229 291index 35942084cd40d..24f5fd1a789de 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
0b487229 310index 74b4911ac16dd..19789fbea5675 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
0b487229 325index dd13f865ad40e..fa6f5599a2a5e 100644
8b6a4947
AM
326--- a/kernel/locking/lockdep.c
327+++ b/kernel/locking/lockdep.c
be118d29 328@@ -140,7 +140,7 @@ static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
8b6a4947
AM
329 unsigned long nr_lock_classes;
330 static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
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 {
335 if (!hlock->class_idx) {
336 /*
be118d29 337@@ -151,6 +151,7 @@ static inline struct lock_class *hlock_class(struct held_lock *hlock)
8b6a4947
AM
338 }
339 return lock_classes + hlock->class_idx - 1;
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
e49925d1 346aufs4.19 mmap patch
fb47a38f 347
c1595e42 348diff --git a/fs/proc/base.c b/fs/proc/base.c
0b487229 349index 7e9f07bf260d2..3ab59011067ee 100644
c1595e42
JR
350--- a/fs/proc/base.c
351+++ b/fs/proc/base.c
acd2b654 352@@ -2016,7 +2016,7 @@ static int map_files_get_link(struct dentry *dentry, struct path *path)
0b487229 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
0b487229 362index 3b63be64e4364..fb9913bf3d10d 100644
fb47a38f
JR
363--- a/fs/proc/nommu.c
364+++ b/fs/proc/nommu.c
076b876e 365@@ -45,7 +45,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
0b487229 378index 5ea1d64cb0b4c..7865a4707d233 100644
fb47a38f
JR
379--- a/fs/proc/task_mmu.c
380+++ b/fs/proc/task_mmu.c
acd2b654 381@@ -305,7 +305,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;
acd2b654 393@@ -1727,7 +1730,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
0b487229 403index 0b63d68dedb2a..400d1c594ceb3 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
0b487229 419index 0416a7204be37..4a298a92681bb 100644
fb47a38f
JR
420--- a/include/linux/mm.h
421+++ b/include/linux/mm.h
acd2b654 422@@ -1440,6 +1440,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
0b487229 452index 5ed8f6292a533..01229754077f6 100644
fb47a38f
JR
453--- a/include/linux/mm_types.h
454+++ b/include/linux/mm_types.h
acd2b654 455@@ -239,6 +239,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
acd2b654 463@@ -313,6 +314,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
8b6a4947 470 atomic_long_t swap_readahead_info;
fb47a38f 471diff --git a/kernel/fork.c b/kernel/fork.c
0b487229 472index f0b58479534f0..fa562c364d020 100644
fb47a38f
JR
473--- a/kernel/fork.c
474+++ b/kernel/fork.c
acd2b654 475@@ -505,7 +505,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
0b487229 485index 26ef77a3883b5..b2869af1ef08e 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 \
7e9cd9fe
AM
492- debug.o $(mmu-y)
493+ prfile.o debug.o $(mmu-y)
076b876e
AM
494
495 obj-y += init-mm.o
496
fb47a38f 497diff --git a/mm/filemap.c b/mm/filemap.c
0b487229 498index 52517f28e6f4a..250f675dcfb28 100644
fb47a38f
JR
499--- a/mm/filemap.c
500+++ b/mm/filemap.c
cd7a4cd9
AM
501@@ -2700,7 +2700,7 @@ vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
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
0b487229 511index f7cd9cb966c0f..515e88a194bd0 100644
fb47a38f
JR
512--- a/mm/mmap.c
513+++ b/mm/mmap.c
c4adf169 514@@ -180,7 +180,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;
c4adf169 523@@ -905,7 +905,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);
acd2b654 532@@ -1821,8 +1821,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);
acd2b654 542@@ -2641,7 +2641,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);
acd2b654 551@@ -2660,7 +2660,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));
acd2b654 560@@ -2822,7 +2822,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);
acd2b654 569@@ -2897,10 +2897,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)
acd2b654 598@@ -3206,7 +3223,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
0b487229 608index e4aac33216aec..83bfcd1528a00 100644
fb47a38f
JR
609--- a/mm/nommu.c
610+++ b/mm/nommu.c
acd2b654 611@@ -625,7 +625,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 */
acd2b654 620@@ -763,7 +763,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 }
acd2b654 629@@ -1286,7 +1286,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;
0b487229 638@@ -1361,10 +1361,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)
0b487229
JR
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
0b487229 653index 0000000000000..00d51187c3250
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+ *
0b487229 665+ * Copyright (c) 2014-2020 Junjro R. Okajima
076b876e
AM
666+ * Copyright (c) 2014 Ian Campbell
667+ */
668+
669+#include <linux/mm.h>
670+#include <linux/file.h>
671+#include <linux/fs.h>
672+
673+/* #define PRFILE_TRACE */
674+static inline void prfile_trace(struct file *f, struct file *pr,
675+ const char func[], int line, const char func2[])
676+{
677+#ifdef PRFILE_TRACE
678+ if (pr)
1c60b727 679+ pr_info("%s:%d: %s, %pD2\n", func, line, func2, f);
076b876e
AM
680+#endif
681+}
682+
076b876e
AM
683+void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
684+ int line)
685+{
686+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
687+
688+ prfile_trace(f, pr, func, line, __func__);
689+ file_update_time(f);
690+ if (f && pr)
691+ file_update_time(pr);
692+}
693+
694+struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
695+ int line)
696+{
697+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
698+
699+ prfile_trace(f, pr, func, line, __func__);
700+ return (f && pr) ? pr : f;
701+}
702+
703+void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
704+{
705+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
706+
707+ prfile_trace(f, pr, func, line, __func__);
708+ get_file(f);
709+ if (f && pr)
710+ get_file(pr);
711+}
712+
713+void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
714+{
715+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
716+
717+ prfile_trace(f, pr, func, line, __func__);
718+ fput(f);
719+ if (f && pr)
720+ fput(pr);
721+}
b912730e
AM
722+
723+#ifndef CONFIG_MMU
076b876e
AM
724+struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
725+ int line)
726+{
727+ struct file *f = region->vm_file, *pr = region->vm_prfile;
728+
729+ prfile_trace(f, pr, func, line, __func__);
730+ return (f && pr) ? pr : f;
731+}
732+
733+void vmr_do_fput(struct vm_region *region, const char func[], int line)
734+{
735+ struct file *f = region->vm_file, *pr = region->vm_prfile;
736+
737+ prfile_trace(f, pr, func, line, __func__);
738+ fput(f);
739+ if (f && pr)
740+ fput(pr);
741+}
b912730e 742+#endif /* !CONFIG_MMU */
2121bcd9 743SPDX-License-Identifier: GPL-2.0
e49925d1 744aufs4.19 standalone patch
7f207e10 745
c1595e42 746diff --git a/fs/dcache.c b/fs/dcache.c
0b487229 747index 9f57bd87bce5a..328a13662ca2e 100644
c1595e42
JR
748--- a/fs/dcache.c
749+++ b/fs/dcache.c
acd2b654 750@@ -1343,6 +1343,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;
acd2b654 758@@ -2837,6 +2838,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
0b487229 767index 1ebf6e5a521d9..a72c294657eae 100644
79b8bda9
AM
768--- a/fs/exec.c
769+++ b/fs/exec.c
521ced18 770@@ -109,6 +109,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
0b487229 779index d4da10be555c9..fbc70ac63462f 100644
febd17d6
JR
780--- a/fs/fcntl.c
781+++ b/fs/fcntl.c
0b487229 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
0b487229 791index e49af4caf15d9..569020fd1fb38 100644
b912730e
AM
792--- a/fs/file_table.c
793+++ b/fs/file_table.c
acd2b654 794@@ -161,6 +161,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.
802@@ -323,6 +324,7 @@ void flush_delayed_fput(void)
8cdd5066
JR
803 {
804 delayed_fput(NULL);
805 }
febd17d6 806+EXPORT_SYMBOL_GPL(flush_delayed_fput);
8cdd5066
JR
807
808 static DECLARE_DELAYED_WORK(delayed_fput_work, delayed_fput);
809
acd2b654 810@@ -365,6 +367,7 @@ void __fput_sync(struct file *file)
8cdd5066
JR
811 }
812
813 EXPORT_SYMBOL(fput);
febd17d6 814+EXPORT_SYMBOL_GPL(__fput_sync);
8cdd5066 815
79b8bda9 816 void __init files_init(void)
8b6a4947 817 {
5afbbe0d 818diff --git a/fs/inode.c b/fs/inode.c
0b487229 819index fa6ae6a217fbd..69d4a6cded14b 100644
5afbbe0d
AM
820--- a/fs/inode.c
821+++ b/fs/inode.c
acd2b654 822@@ -1666,6 +1666,7 @@ int update_time(struct inode *inode, struct timespec64 *time, int flags)
5afbbe0d
AM
823
824 return update_time(inode, time, flags);
825 }
826+EXPORT_SYMBOL_GPL(update_time);
827
828 /**
829 * touch_atime - update the access time
7f207e10 830diff --git a/fs/namespace.c b/fs/namespace.c
0b487229 831index 72c93f3e86478..c49803ce04b8b 100644
7f207e10
AM
832--- a/fs/namespace.c
833+++ b/fs/namespace.c
acd2b654 834@@ -437,6 +437,7 @@ void __mnt_drop_write(struct vfsmount *mnt)
c06a8ce3
AM
835 mnt_dec_writers(real_mount(mnt));
836 preempt_enable();
837 }
838+EXPORT_SYMBOL_GPL(__mnt_drop_write);
839
840 /**
841 * mnt_drop_write - give up write access to a mount
acd2b654 842@@ -775,6 +776,7 @@ int is_current_mnt_ns(struct vfsmount *mnt)
8b6a4947
AM
843 {
844 return check_mnt(real_mount(mnt));
845 }
846+EXPORT_SYMBOL_GPL(is_current_mnt_ns);
847
848 /*
849 * vfsmount lock must be held for write
acd2b654 850@@ -1832,6 +1834,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
7f207e10
AM
851 }
852 return 0;
853 }
febd17d6 854+EXPORT_SYMBOL_GPL(iterate_mounts);
7f207e10 855
7eafdf33 856 static void cleanup_group_ids(struct mount *mnt, struct mount *end)
7f207e10
AM
857 {
858diff --git a/fs/notify/group.c b/fs/notify/group.c
0b487229 859index c03b836628769..94d210ca384a6 100644
7f207e10
AM
860--- a/fs/notify/group.c
861+++ b/fs/notify/group.c
0b487229 862@@ -112,6 +112,7 @@ void fsnotify_get_group(struct fsnotify_group *group)
1716fcea 863 {
2121bcd9 864 refcount_inc(&group->refcnt);
1716fcea 865 }
febd17d6 866+EXPORT_SYMBOL_GPL(fsnotify_get_group);
1716fcea
AM
867
868 /*
869 * Drop a reference to a group. Free it if it's through.
0b487229 870@@ -121,6 +122,7 @@ void fsnotify_put_group(struct fsnotify_group *group)
2121bcd9 871 if (refcount_dec_and_test(&group->refcnt))
1716fcea 872 fsnotify_final_destroy_group(group);
7f207e10 873 }
febd17d6 874+EXPORT_SYMBOL_GPL(fsnotify_put_group);
7f207e10
AM
875
876 /*
877 * Create a new fsnotify_group and hold a reference for the group returned.
0b487229 878@@ -150,6 +152,7 @@ struct fsnotify_group *fsnotify_alloc_group(const struct fsnotify_ops *ops)
7f207e10
AM
879
880 return group;
881 }
febd17d6 882+EXPORT_SYMBOL_GPL(fsnotify_alloc_group);
1716fcea
AM
883
884 int fsnotify_fasync(int fd, struct file *file, int on)
885 {
7f207e10 886diff --git a/fs/notify/mark.c b/fs/notify/mark.c
0b487229 887index 59cdb27826def..ce365c73f4fea 100644
7f207e10
AM
888--- a/fs/notify/mark.c
889+++ b/fs/notify/mark.c
acd2b654 890@@ -263,6 +263,7 @@ void fsnotify_put_mark(struct fsnotify_mark *mark)
cd7a4cd9
AM
891 queue_delayed_work(system_unbound_wq, &reaper_work,
892 FSNOTIFY_REAPER_DELAY);
7f207e10 893 }
febd17d6 894+EXPORT_SYMBOL_GPL(fsnotify_put_mark);
7f207e10 895
cd7a4cd9
AM
896 /*
897 * Get mark reference when we found the mark via lockless traversal of object
acd2b654 898@@ -417,6 +418,7 @@ void fsnotify_destroy_mark(struct fsnotify_mark *mark,
1716fcea 899 mutex_unlock(&group->mark_mutex);
79b8bda9 900 fsnotify_free_mark(mark);
7f207e10 901 }
febd17d6 902+EXPORT_SYMBOL_GPL(fsnotify_destroy_mark);
7f207e10 903
ffa93bbd
AM
904 /*
905 * Sorting function for lists of fsnotify marks.
acd2b654 906@@ -632,6 +634,7 @@ int fsnotify_add_mark(struct fsnotify_mark *mark, fsnotify_connp_t *connp,
cd7a4cd9 907 mutex_unlock(&group->mark_mutex);
7f207e10
AM
908 return ret;
909 }
febd17d6 910+EXPORT_SYMBOL_GPL(fsnotify_add_mark);
7f207e10 911
cd7a4cd9
AM
912 /*
913 * Given a list of marks, find the mark associated with given group. If found
acd2b654 914@@ -754,6 +757,7 @@ void fsnotify_init_mark(struct fsnotify_mark *mark,
ffa93bbd
AM
915 fsnotify_get_group(group);
916 mark->group = group;
7f207e10 917 }
febd17d6 918+EXPORT_SYMBOL_GPL(fsnotify_init_mark);
7f207e10 919
5afbbe0d
AM
920 /*
921 * Destroy all marks in destroy_list, waits for SRCU period to finish before
7f207e10 922diff --git a/fs/open.c b/fs/open.c
0b487229 923index 0285ce7dbd515..cb81623a8b09e 100644
7f207e10
AM
924--- a/fs/open.c
925+++ b/fs/open.c
c2c0f25c 926@@ -64,6 +64,7 @@ int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
febd17d6 927 inode_unlock(dentry->d_inode);
7f207e10
AM
928 return ret;
929 }
febd17d6 930+EXPORT_SYMBOL_GPL(do_truncate);
7f207e10 931
5afbbe0d 932 long vfs_truncate(const struct path *path, loff_t length)
7f207e10 933 {
5527c038 934diff --git a/fs/read_write.c b/fs/read_write.c
0b487229 935index 45e3e7a0d4e64..f802a3865acf4 100644
5527c038
JR
936--- a/fs/read_write.c
937+++ b/fs/read_write.c
b00004a5 938@@ -459,6 +459,7 @@ ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
8b6a4947
AM
939
940 return ret;
941 }
942+EXPORT_SYMBOL_GPL(vfs_read);
943
944 static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
945 {
b00004a5 946@@ -499,6 +500,7 @@ vfs_readf_t vfs_readf(struct file *file)
5527c038 947 return new_sync_read;
0b487229 948 return ERR_PTR(-ENOSYS); /* doesn't have ->read(|_iter)() op */
5527c038 949 }
febd17d6 950+EXPORT_SYMBOL_GPL(vfs_readf);
5527c038
JR
951
952 vfs_writef_t vfs_writef(struct file *file)
953 {
b00004a5 954@@ -510,6 +512,7 @@ vfs_writef_t vfs_writef(struct file *file)
5527c038 955 return new_sync_write;
0b487229 956 return ERR_PTR(-ENOSYS); /* doesn't have ->write(|_iter)() op */
5527c038 957 }
febd17d6 958+EXPORT_SYMBOL_GPL(vfs_writef);
5527c038 959
8b6a4947
AM
960 ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t *pos)
961 {
b00004a5 962@@ -579,6 +582,7 @@ ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_
8b6a4947
AM
963
964 return ret;
965 }
966+EXPORT_SYMBOL_GPL(vfs_write);
967
968 static inline loff_t file_pos_read(struct file *file)
5527c038 969 {
7f207e10 970diff --git a/fs/splice.c b/fs/splice.c
0b487229 971index 1dd7f96b22dc4..a5e3bcba0ea26 100644
7f207e10
AM
972--- a/fs/splice.c
973+++ b/fs/splice.c
cd7a4cd9 974@@ -851,6 +851,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
392086de
AM
975
976 return splice_write(pipe, out, ppos, len, flags);
7f207e10 977 }
febd17d6 978+EXPORT_SYMBOL_GPL(do_splice_from);
7f207e10
AM
979
980 /*
981 * Attempt to initiate a splice from a file to a pipe.
cd7a4cd9 982@@ -880,6 +881,7 @@ long do_splice_to(struct file *in, loff_t *ppos,
7f207e10
AM
983
984 return splice_read(in, ppos, pipe, len, flags);
985 }
febd17d6 986+EXPORT_SYMBOL_GPL(do_splice_to);
7f207e10
AM
987
988 /**
989 * splice_direct_to_actor - splices data directly between two non-pipes
a2654f78 990diff --git a/fs/sync.c b/fs/sync.c
0b487229 991index 28607828e96f2..ffd7ea43831ea 100644
a2654f78
AM
992--- a/fs/sync.c
993+++ b/fs/sync.c
2121bcd9 994@@ -39,6 +39,7 @@ int __sync_filesystem(struct super_block *sb, int wait)
a2654f78
AM
995 sb->s_op->sync_fs(sb, wait);
996 return __sync_blockdev(sb->s_bdev, wait);
997 }
998+EXPORT_SYMBOL_GPL(__sync_filesystem);
999
1000 /*
1001 * Write out and wait upon all dirty data associated with this
c1595e42 1002diff --git a/fs/xattr.c b/fs/xattr.c
0b487229 1003index 0d6a6a4af8616..7ce4701b7289d 100644
c1595e42
JR
1004--- a/fs/xattr.c
1005+++ b/fs/xattr.c
acd2b654 1006@@ -295,6 +295,7 @@ vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value,
c1595e42
JR
1007 *xattr_value = value;
1008 return error;
1009 }
febd17d6 1010+EXPORT_SYMBOL_GPL(vfs_getxattr_alloc);
c1595e42 1011
febd17d6 1012 ssize_t
f2c43d5f 1013 __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
8b6a4947 1014diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
0b487229 1015index fa6f5599a2a5e..7ac19efdac047 100644
8b6a4947
AM
1016--- a/kernel/locking/lockdep.c
1017+++ b/kernel/locking/lockdep.c
be118d29 1018@@ -151,6 +151,7 @@ inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
8b6a4947
AM
1019 }
1020 return lock_classes + hlock->class_idx - 1;
1021 }
1022+EXPORT_SYMBOL_GPL(lockdep_hlock_class);
1023 #define hlock_class(hlock) lockdep_hlock_class(hlock)
1024
1025 #ifdef CONFIG_LOCK_STAT
8cdd5066 1026diff --git a/kernel/task_work.c b/kernel/task_work.c
0b487229 1027index 0fef395662a6e..83fb1ecfc33de 100644
8cdd5066
JR
1028--- a/kernel/task_work.c
1029+++ b/kernel/task_work.c
2121bcd9 1030@@ -116,3 +116,4 @@ void task_work_run(void)
8cdd5066
JR
1031 } while (work);
1032 }
1033 }
febd17d6 1034+EXPORT_SYMBOL_GPL(task_work_run);
7f207e10 1035diff --git a/security/device_cgroup.c b/security/device_cgroup.c
0b487229 1036index cd97929fac663..20c59b2e3b261 100644
7f207e10
AM
1037--- a/security/device_cgroup.c
1038+++ b/security/device_cgroup.c
0b487229 1039@@ -824,3 +824,4 @@ int __devcgroup_check_permission(short type, u32 major, u32 minor,
7f207e10 1040
2121bcd9
AM
1041 return 0;
1042 }
1043+EXPORT_SYMBOL_GPL(__devcgroup_check_permission);
7f207e10 1044diff --git a/security/security.c b/security/security.c
0b487229 1045index 736e78da1ab9d..c67016362c13e 100644
7f207e10
AM
1046--- a/security/security.c
1047+++ b/security/security.c
acd2b654 1048@@ -542,6 +542,7 @@ int security_path_rmdir(const struct path *dir, struct dentry *dentry)
7f207e10 1049 return 0;
c2c0f25c 1050 return call_int_hook(path_rmdir, 0, dir, dentry);
7f207e10 1051 }
febd17d6 1052+EXPORT_SYMBOL_GPL(security_path_rmdir);
7f207e10 1053
5afbbe0d 1054 int security_path_unlink(const struct path *dir, struct dentry *dentry)
7f207e10 1055 {
acd2b654 1056@@ -558,6 +559,7 @@ int security_path_symlink(const struct path *dir, struct dentry *dentry,
7f207e10 1057 return 0;
c2c0f25c 1058 return call_int_hook(path_symlink, 0, dir, dentry, old_name);
7f207e10 1059 }
febd17d6 1060+EXPORT_SYMBOL_GPL(security_path_symlink);
7f207e10 1061
5afbbe0d 1062 int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
7f207e10 1063 struct dentry *new_dentry)
acd2b654 1064@@ -566,6 +568,7 @@ int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
7f207e10 1065 return 0;
c2c0f25c 1066 return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
7f207e10 1067 }
febd17d6 1068+EXPORT_SYMBOL_GPL(security_path_link);
7f207e10 1069
5afbbe0d
AM
1070 int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
1071 const struct path *new_dir, struct dentry *new_dentry,
acd2b654 1072@@ -593,6 +596,7 @@ int security_path_truncate(const struct path *path)
7f207e10 1073 return 0;
c2c0f25c 1074 return call_int_hook(path_truncate, 0, path);
7f207e10 1075 }
febd17d6 1076+EXPORT_SYMBOL_GPL(security_path_truncate);
7f207e10 1077
5afbbe0d 1078 int security_path_chmod(const struct path *path, umode_t mode)
7eafdf33 1079 {
acd2b654 1080@@ -600,6 +604,7 @@ int security_path_chmod(const struct path *path, umode_t mode)
7f207e10 1081 return 0;
c2c0f25c 1082 return call_int_hook(path_chmod, 0, path, mode);
7f207e10 1083 }
febd17d6 1084+EXPORT_SYMBOL_GPL(security_path_chmod);
7f207e10 1085
5afbbe0d 1086 int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
7f207e10 1087 {
acd2b654 1088@@ -607,6 +612,7 @@ int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
7f207e10 1089 return 0;
c2c0f25c 1090 return call_int_hook(path_chown, 0, path, uid, gid);
7f207e10 1091 }
febd17d6 1092+EXPORT_SYMBOL_GPL(security_path_chown);
7f207e10 1093
5afbbe0d 1094 int security_path_chroot(const struct path *path)
7f207e10 1095 {
0b487229 1096@@ -707,6 +713,7 @@ int security_inode_permission(struct inode *inode, int mask)
7f207e10 1097 return 0;
c2c0f25c 1098 return call_int_hook(inode_permission, 0, inode, mask);
7f207e10 1099 }
febd17d6 1100+EXPORT_SYMBOL_GPL(security_inode_permission);
7f207e10 1101
1e00d052 1102 int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
7f207e10 1103 {
0b487229 1104@@ -878,6 +885,7 @@ int security_file_permission(struct file *file, int mask)
7f207e10
AM
1105
1106 return fsnotify_perm(file, mask);
1107 }
febd17d6 1108+EXPORT_SYMBOL_GPL(security_file_permission);
7f207e10
AM
1109
1110 int security_file_alloc(struct file *file)
1111 {
0b487229 1112@@ -937,6 +945,7 @@ int security_mmap_file(struct file *file, unsigned long prot,
7f207e10
AM
1113 return ret;
1114 return ima_file_mmap(file, prot);
1115 }
febd17d6 1116+EXPORT_SYMBOL_GPL(security_mmap_file);
7f207e10 1117
0c3ec466
AM
1118 int security_mmap_addr(unsigned long addr)
1119 {
7f207e10
AM
1120diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
1121--- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs 1970-01-01 01:00:00.000000000 +0100
0b487229 1122+++ linux/Documentation/ABI/testing/debugfs-aufs 2021-02-22 23:30:37.699347580 +0100
062440b3 1123@@ -0,0 +1,55 @@
7f207e10
AM
1124+What: /debug/aufs/si_<id>/
1125+Date: March 2009
f6b6e03d 1126+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1127+Description:
1128+ Under /debug/aufs, a directory named si_<id> is created
1129+ per aufs mount, where <id> is a unique id generated
1130+ internally.
1facf9fc 1131+
86dc4139
AM
1132+What: /debug/aufs/si_<id>/plink
1133+Date: Apr 2013
f6b6e03d 1134+Contact: J. R. Okajima <hooanon05g@gmail.com>
86dc4139
AM
1135+Description:
1136+ It has three lines and shows the information about the
1137+ pseudo-link. The first line is a single number
1138+ representing a number of buckets. The second line is a
1139+ number of pseudo-links per buckets (separated by a
1140+ blank). The last line is a single number representing a
1141+ total number of psedo-links.
1142+ When the aufs mount option 'noplink' is specified, it
1143+ will show "1\n0\n0\n".
1144+
7f207e10
AM
1145+What: /debug/aufs/si_<id>/xib
1146+Date: March 2009
f6b6e03d 1147+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1148+Description:
1149+ It shows the consumed blocks by xib (External Inode Number
1150+ Bitmap), its block size and file size.
1151+ When the aufs mount option 'noxino' is specified, it
1152+ will be empty. About XINO files, see the aufs manual.
1153+
062440b3 1154+What: /debug/aufs/si_<id>/xi0, xi1 ... xiN and xiN-N
7f207e10 1155+Date: March 2009
f6b6e03d 1156+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1157+Description:
1158+ It shows the consumed blocks by xino (External Inode Number
1159+ Translation Table), its link count, block size and file
1160+ size.
062440b3
AM
1161+ Due to the file size limit, there may exist multiple
1162+ xino files per branch. In this case, "-N" is added to
1163+ the filename and it corresponds to the index of the
1164+ internal xino array. "-0" is omitted.
1165+ When the aufs mount option 'noxino' is specified, Those
1166+ entries won't exist. About XINO files, see the aufs
1167+ manual.
7f207e10
AM
1168+
1169+What: /debug/aufs/si_<id>/xigen
1170+Date: March 2009
f6b6e03d 1171+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1172+Description:
1173+ It shows the consumed blocks by xigen (External Inode
1174+ Generation Table), its block size and file size.
1175+ If CONFIG_AUFS_EXPORT is disabled, this entry will not
1176+ be created.
1177+ When the aufs mount option 'noxino' is specified, it
1178+ will be empty. About XINO files, see the aufs manual.
1179diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
1180--- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs 1970-01-01 01:00:00.000000000 +0100
0b487229 1181+++ linux/Documentation/ABI/testing/sysfs-aufs 2021-02-22 23:30:37.699347580 +0100
392086de 1182@@ -0,0 +1,31 @@
7f207e10
AM
1183+What: /sys/fs/aufs/si_<id>/
1184+Date: March 2009
f6b6e03d 1185+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1186+Description:
1187+ Under /sys/fs/aufs, a directory named si_<id> is created
1188+ per aufs mount, where <id> is a unique id generated
1189+ internally.
1190+
1191+What: /sys/fs/aufs/si_<id>/br0, br1 ... brN
1192+Date: March 2009
f6b6e03d 1193+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1194+Description:
1195+ It shows the abolute path of a member directory (which
1196+ is called branch) in aufs, and its permission.
1197+
392086de
AM
1198+What: /sys/fs/aufs/si_<id>/brid0, brid1 ... bridN
1199+Date: July 2013
f6b6e03d 1200+Contact: J. R. Okajima <hooanon05g@gmail.com>
392086de
AM
1201+Description:
1202+ It shows the id of a member directory (which is called
1203+ branch) in aufs.
1204+
7f207e10
AM
1205+What: /sys/fs/aufs/si_<id>/xi_path
1206+Date: March 2009
f6b6e03d 1207+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1208+Description:
1209+ It shows the abolute path of XINO (External Inode Number
1210+ Bitmap, Translation Table and Generation Table) file
1211+ even if it is the default path.
1212+ When the aufs mount option 'noxino' is specified, it
1213+ will be empty. About XINO files, see the aufs manual.
53392da6
AM
1214diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
1215--- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt 1970-01-01 01:00:00.000000000 +0100
0b487229 1216+++ linux/Documentation/filesystems/aufs/design/01intro.txt 2021-02-22 23:30:37.699347580 +0100
1c60b727 1217@@ -0,0 +1,171 @@
53392da6 1218+
0b487229 1219+# Copyright (C) 2005-2020 Junjiro R. Okajima
53392da6
AM
1220+#
1221+# This program is free software; you can redistribute it and/or modify
1222+# it under the terms of the GNU General Public License as published by
1223+# the Free Software Foundation; either version 2 of the License, or
1224+# (at your option) any later version.
1225+#
1226+# This program is distributed in the hope that it will be useful,
1227+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1228+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1229+# GNU General Public License for more details.
1230+#
1231+# You should have received a copy of the GNU General Public License
523b37e3 1232+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1233+
1234+Introduction
1235+----------------------------------------
1236+
3c1bdaff 1237+aufs [ei ju: ef es] | /ey-yoo-ef-es/ | [a u f s]
53392da6
AM
1238+1. abbrev. for "advanced multi-layered unification filesystem".
1239+2. abbrev. for "another unionfs".
1240+3. abbrev. for "auf das" in German which means "on the" in English.
1241+ Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
1242+ But "Filesystem aufs Filesystem" is hard to understand.
1c60b727 1243+4. abbrev. for "African Urban Fashion Show".
53392da6
AM
1244+
1245+AUFS is a filesystem with features:
1246+- multi layered stackable unification filesystem, the member directory
1247+ is called as a branch.
1248+- branch permission and attribute, 'readonly', 'real-readonly',
7e9cd9fe 1249+ 'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
53392da6
AM
1250+ combination.
1251+- internal "file copy-on-write".
1252+- logical deletion, whiteout.
1253+- dynamic branch manipulation, adding, deleting and changing permission.
1254+- allow bypassing aufs, user's direct branch access.
1255+- external inode number translation table and bitmap which maintains the
1256+ persistent aufs inode number.
1257+- seekable directory, including NFS readdir.
1258+- file mapping, mmap and sharing pages.
1259+- pseudo-link, hardlink over branches.
1260+- loopback mounted filesystem as a branch.
1261+- several policies to select one among multiple writable branches.
1262+- revert a single systemcall when an error occurs in aufs.
1263+- and more...
1264+
1265+
1266+Multi Layered Stackable Unification Filesystem
1267+----------------------------------------------------------------------
1268+Most people already knows what it is.
1269+It is a filesystem which unifies several directories and provides a
1270+merged single directory. When users access a file, the access will be
1271+passed/re-directed/converted (sorry, I am not sure which English word is
1272+correct) to the real file on the member filesystem. The member
1273+filesystem is called 'lower filesystem' or 'branch' and has a mode
1274+'readonly' and 'readwrite.' And the deletion for a file on the lower
1275+readonly branch is handled by creating 'whiteout' on the upper writable
1276+branch.
1277+
1278+On LKML, there have been discussions about UnionMount (Jan Blunck,
1279+Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
1280+different approaches to implement the merged-view.
1281+The former tries putting it into VFS, and the latter implements as a
1282+separate filesystem.
1283+(If I misunderstand about these implementations, please let me know and
1284+I shall correct it. Because it is a long time ago when I read their
1285+source files last time).
1286+
1287+UnionMount's approach will be able to small, but may be hard to share
1288+branches between several UnionMount since the whiteout in it is
1289+implemented in the inode on branch filesystem and always
1290+shared. According to Bharata's post, readdir does not seems to be
1291+finished yet.
1292+There are several missing features known in this implementations such as
1293+- for users, the inode number may change silently. eg. copy-up.
1294+- link(2) may break by copy-up.
1295+- read(2) may get an obsoleted filedata (fstat(2) too).
1296+- fcntl(F_SETLK) may be broken by copy-up.
1297+- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
1298+ open(O_RDWR).
1299+
7e9cd9fe
AM
1300+In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
1301+merged into mainline. This is another implementation of UnionMount as a
1302+separated filesystem. All the limitations and known problems which
1303+UnionMount are equally inherited to "overlay" filesystem.
1304+
1305+Unionfs has a longer history. When I started implementing a stackable
1306+filesystem (Aug 2005), it already existed. It has virtual super_block,
1307+inode, dentry and file objects and they have an array pointing lower
1308+same kind objects. After contributing many patches for Unionfs, I
1309+re-started my project AUFS (Jun 2006).
53392da6
AM
1310+
1311+In AUFS, the structure of filesystem resembles to Unionfs, but I
1312+implemented my own ideas, approaches and enhancements and it became
1313+totally different one.
1314+
1315+Comparing DM snapshot and fs based implementation
1316+- the number of bytes to be copied between devices is much smaller.
1317+- the type of filesystem must be one and only.
1318+- the fs must be writable, no readonly fs, even for the lower original
1319+ device. so the compression fs will not be usable. but if we use
1320+ loopback mount, we may address this issue.
1321+ for instance,
1322+ mount /cdrom/squashfs.img /sq
1323+ losetup /sq/ext2.img
1324+ losetup /somewhere/cow
1325+ dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1326+- it will be difficult (or needs more operations) to extract the
1327+ difference between the original device and COW.
1328+- DM snapshot-merge may help a lot when users try merging. in the
1329+ fs-layer union, users will use rsync(1).
1330+
7e9cd9fe
AM
1331+You may want to read my old paper "Filesystems in LiveCD"
1332+(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
53392da6 1333+
7e9cd9fe
AM
1334+
1335+Several characters/aspects/persona of aufs
53392da6
AM
1336+----------------------------------------------------------------------
1337+
7e9cd9fe 1338+Aufs has several characters, aspects or persona.
53392da6
AM
1339+1. a filesystem, callee of VFS helper
1340+2. sub-VFS, caller of VFS helper for branches
1341+3. a virtual filesystem which maintains persistent inode number
1342+4. reader/writer of files on branches such like an application
1343+
1344+1. Callee of VFS Helper
1345+As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1346+unlink(2) from an application reaches sys_unlink() kernel function and
1347+then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1348+calls filesystem specific unlink operation. Actually aufs implements the
1349+unlink operation but it behaves like a redirector.
1350+
1351+2. Caller of VFS Helper for Branches
1352+aufs_unlink() passes the unlink request to the branch filesystem as if
1353+it were called from VFS. So the called unlink operation of the branch
1354+filesystem acts as usual. As a caller of VFS helper, aufs should handle
1355+every necessary pre/post operation for the branch filesystem.
1356+- acquire the lock for the parent dir on a branch
1357+- lookup in a branch
1358+- revalidate dentry on a branch
1359+- mnt_want_write() for a branch
1360+- vfs_unlink() for a branch
1361+- mnt_drop_write() for a branch
1362+- release the lock on a branch
1363+
1364+3. Persistent Inode Number
1365+One of the most important issue for a filesystem is to maintain inode
1366+numbers. This is particularly important to support exporting a
1367+filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1368+backend block device for its own. But some storage is necessary to
7e9cd9fe
AM
1369+keep and maintain the inode numbers. It may be a large space and may not
1370+suit to keep in memory. Aufs rents some space from its first writable
1371+branch filesystem (by default) and creates file(s) on it. These files
1372+are created by aufs internally and removed soon (currently) keeping
1373+opened.
53392da6
AM
1374+Note: Because these files are removed, they are totally gone after
1375+ unmounting aufs. It means the inode numbers are not persistent
1376+ across unmount or reboot. I have a plan to make them really
1377+ persistent which will be important for aufs on NFS server.
1378+
1379+4. Read/Write Files Internally (copy-on-write)
1380+Because a branch can be readonly, when you write a file on it, aufs will
1381+"copy-up" it to the upper writable branch internally. And then write the
1382+originally requested thing to the file. Generally kernel doesn't
1383+open/read/write file actively. In aufs, even a single write may cause a
1384+internal "file copy". This behaviour is very similar to cp(1) command.
1385+
1386+Some people may think it is better to pass such work to user space
1387+helper, instead of doing in kernel space. Actually I am still thinking
1388+about it. But currently I have implemented it in kernel space.
1389diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
1390--- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
0b487229 1391+++ linux/Documentation/filesystems/aufs/design/02struct.txt 2021-02-22 23:30:37.699347580 +0100
7e9cd9fe 1392@@ -0,0 +1,258 @@
53392da6 1393+
0b487229 1394+# Copyright (C) 2005-2020 Junjiro R. Okajima
53392da6
AM
1395+#
1396+# This program is free software; you can redistribute it and/or modify
1397+# it under the terms of the GNU General Public License as published by
1398+# the Free Software Foundation; either version 2 of the License, or
1399+# (at your option) any later version.
1400+#
1401+# This program is distributed in the hope that it will be useful,
1402+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1403+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1404+# GNU General Public License for more details.
1405+#
1406+# You should have received a copy of the GNU General Public License
523b37e3 1407+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1408+
1409+Basic Aufs Internal Structure
1410+
1411+Superblock/Inode/Dentry/File Objects
1412+----------------------------------------------------------------------
1413+As like an ordinary filesystem, aufs has its own
1414+superblock/inode/dentry/file objects. All these objects have a
1415+dynamically allocated array and store the same kind of pointers to the
1416+lower filesystem, branch.
1417+For example, when you build a union with one readwrite branch and one
1418+readonly, mounted /au, /rw and /ro respectively.
1419+- /au = /rw + /ro
1420+- /ro/fileA exists but /rw/fileA
1421+
1422+Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1423+pointers are stored in a aufs dentry. The array in aufs dentry will be,
7e9cd9fe 1424+- [0] = NULL (because /rw/fileA doesn't exist)
53392da6
AM
1425+- [1] = /ro/fileA
1426+
1427+This style of an array is essentially same to the aufs
1428+superblock/inode/dentry/file objects.
1429+
1430+Because aufs supports manipulating branches, ie. add/delete/change
7e9cd9fe
AM
1431+branches dynamically, these objects has its own generation. When
1432+branches are changed, the generation in aufs superblock is
1433+incremented. And a generation in other object are compared when it is
1434+accessed. When a generation in other objects are obsoleted, aufs
1435+refreshes the internal array.
53392da6
AM
1436+
1437+
1438+Superblock
1439+----------------------------------------------------------------------
1440+Additionally aufs superblock has some data for policies to select one
1441+among multiple writable branches, XIB files, pseudo-links and kobject.
1442+See below in detail.
7e9cd9fe
AM
1443+About the policies which supports copy-down a directory, see
1444+wbr_policy.txt too.
53392da6
AM
1445+
1446+
1447+Branch and XINO(External Inode Number Translation Table)
1448+----------------------------------------------------------------------
1449+Every branch has its own xino (external inode number translation table)
1450+file. The xino file is created and unlinked by aufs internally. When two
1451+members of a union exist on the same filesystem, they share the single
1452+xino file.
1453+The struct of a xino file is simple, just a sequence of aufs inode
1454+numbers which is indexed by the lower inode number.
1455+In the above sample, assume the inode number of /ro/fileA is i111 and
1456+aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1457+4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1458+
1459+When the inode numbers are not contiguous, the xino file will be sparse
1460+which has a hole in it and doesn't consume as much disk space as it
1461+might appear. If your branch filesystem consumes disk space for such
1462+holes, then you should specify 'xino=' option at mounting aufs.
1463+
7e9cd9fe
AM
1464+Aufs has a mount option to free the disk blocks for such holes in XINO
1465+files on tmpfs or ramdisk. But it is not so effective actually. If you
1466+meet a problem of disk shortage due to XINO files, then you should try
1467+"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1468+The patch localizes the assignment inumbers per tmpfs-mount and avoid
1469+the holes in XINO files.
1470+
53392da6 1471+Also a writable branch has three kinds of "whiteout bases". All these
7e9cd9fe 1472+are existed when the branch is joined to aufs, and their names are
53392da6
AM
1473+whiteout-ed doubly, so that users will never see their names in aufs
1474+hierarchy.
7e9cd9fe 1475+1. a regular file which will be hardlinked to all whiteouts.
53392da6 1476+2. a directory to store a pseudo-link.
7e9cd9fe 1477+3. a directory to store an "orphan"-ed file temporary.
53392da6
AM
1478+
1479+1. Whiteout Base
1480+ When you remove a file on a readonly branch, aufs handles it as a
1481+ logical deletion and creates a whiteout on the upper writable branch
1482+ as a hardlink of this file in order not to consume inode on the
1483+ writable branch.
1484+2. Pseudo-link Dir
1485+ See below, Pseudo-link.
1486+3. Step-Parent Dir
1487+ When "fileC" exists on the lower readonly branch only and it is
1488+ opened and removed with its parent dir, and then user writes
1489+ something into it, then aufs copies-up fileC to this
1490+ directory. Because there is no other dir to store fileC. After
1491+ creating a file under this dir, the file is unlinked.
1492+
1493+Because aufs supports manipulating branches, ie. add/delete/change
7e9cd9fe
AM
1494+dynamically, a branch has its own id. When the branch order changes,
1495+aufs finds the new index by searching the branch id.
53392da6
AM
1496+
1497+
1498+Pseudo-link
1499+----------------------------------------------------------------------
1500+Assume "fileA" exists on the lower readonly branch only and it is
1501+hardlinked to "fileB" on the branch. When you write something to fileA,
1502+aufs copies-up it to the upper writable branch. Additionally aufs
1503+creates a hardlink under the Pseudo-link Directory of the writable
1504+branch. The inode of a pseudo-link is kept in aufs super_block as a
1505+simple list. If fileB is read after unlinking fileA, aufs returns
1506+filedata from the pseudo-link instead of the lower readonly
1507+branch. Because the pseudo-link is based upon the inode, to keep the
7e9cd9fe 1508+inode number by xino (see above) is essentially necessary.
53392da6
AM
1509+
1510+All the hardlinks under the Pseudo-link Directory of the writable branch
1511+should be restored in a proper location later. Aufs provides a utility
1512+to do this. The userspace helpers executed at remounting and unmounting
1513+aufs by default.
1514+During this utility is running, it puts aufs into the pseudo-link
1515+maintenance mode. In this mode, only the process which began the
1516+maintenance mode (and its child processes) is allowed to operate in
1517+aufs. Some other processes which are not related to the pseudo-link will
1518+be allowed to run too, but the rest have to return an error or wait
1519+until the maintenance mode ends. If a process already acquires an inode
1520+mutex (in VFS), it has to return an error.
1521+
1522+
1523+XIB(external inode number bitmap)
1524+----------------------------------------------------------------------
1525+Addition to the xino file per a branch, aufs has an external inode number
7e9cd9fe
AM
1526+bitmap in a superblock object. It is also an internal file such like a
1527+xino file.
53392da6
AM
1528+It is a simple bitmap to mark whether the aufs inode number is in-use or
1529+not.
1530+To reduce the file I/O, aufs prepares a single memory page to cache xib.
1531+
7e9cd9fe 1532+As well as XINO files, aufs has a feature to truncate/refresh XIB to
53392da6
AM
1533+reduce the number of consumed disk blocks for these files.
1534+
1535+
1536+Virtual or Vertical Dir, and Readdir in Userspace
1537+----------------------------------------------------------------------
1538+In order to support multiple layers (branches), aufs readdir operation
1539+constructs a virtual dir block on memory. For readdir, aufs calls
1540+vfs_readdir() internally for each dir on branches, merges their entries
1541+with eliminating the whiteout-ed ones, and sets it to file (dir)
1542+object. So the file object has its entry list until it is closed. The
1543+entry list will be updated when the file position is zero and becomes
7e9cd9fe 1544+obsoleted. This decision is made in aufs automatically.
53392da6
AM
1545+
1546+The dynamically allocated memory block for the name of entries has a
1547+unit of 512 bytes (by default) and stores the names contiguously (no
1548+padding). Another block for each entry is handled by kmem_cache too.
1549+During building dir blocks, aufs creates hash list and judging whether
1550+the entry is whiteouted by its upper branch or already listed.
1551+The merged result is cached in the corresponding inode object and
1552+maintained by a customizable life-time option.
1553+
1554+Some people may call it can be a security hole or invite DoS attack
1555+since the opened and once readdir-ed dir (file object) holds its entry
1556+list and becomes a pressure for system memory. But I'd say it is similar
1557+to files under /proc or /sys. The virtual files in them also holds a
1558+memory page (generally) while they are opened. When an idea to reduce
1559+memory for them is introduced, it will be applied to aufs too.
1560+For those who really hate this situation, I've developed readdir(3)
1561+library which operates this merging in userspace. You just need to set
1562+LD_PRELOAD environment variable, and aufs will not consume no memory in
1563+kernel space for readdir(3).
1564+
1565+
1566+Workqueue
1567+----------------------------------------------------------------------
1568+Aufs sometimes requires privilege access to a branch. For instance,
1569+in copy-up/down operation. When a user process is going to make changes
1570+to a file which exists in the lower readonly branch only, and the mode
1571+of one of ancestor directories may not be writable by a user
1572+process. Here aufs copy-up the file with its ancestors and they may
1573+require privilege to set its owner/group/mode/etc.
1574+This is a typical case of a application character of aufs (see
1575+Introduction).
1576+
1577+Aufs uses workqueue synchronously for this case. It creates its own
1578+workqueue. The workqueue is a kernel thread and has privilege. Aufs
1579+passes the request to call mkdir or write (for example), and wait for
1580+its completion. This approach solves a problem of a signal handler
1581+simply.
1582+If aufs didn't adopt the workqueue and changed the privilege of the
7e9cd9fe
AM
1583+process, then the process may receive the unexpected SIGXFSZ or other
1584+signals.
53392da6
AM
1585+
1586+Also aufs uses the system global workqueue ("events" kernel thread) too
1587+for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1588+whiteout base and etc. This is unrelated to a privilege.
1589+Most of aufs operation tries acquiring a rw_semaphore for aufs
1590+superblock at the beginning, at the same time waits for the completion
1591+of all queued asynchronous tasks.
1592+
1593+
1594+Whiteout
1595+----------------------------------------------------------------------
1596+The whiteout in aufs is very similar to Unionfs's. That is represented
1597+by its filename. UnionMount takes an approach of a file mode, but I am
1598+afraid several utilities (find(1) or something) will have to support it.
1599+
1600+Basically the whiteout represents "logical deletion" which stops aufs to
1601+lookup further, but also it represents "dir is opaque" which also stop
7e9cd9fe 1602+further lookup.
53392da6
AM
1603+
1604+In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1605+In order to make several functions in a single systemcall to be
1606+revertible, aufs adopts an approach to rename a directory to a temporary
1607+unique whiteouted name.
1608+For example, in rename(2) dir where the target dir already existed, aufs
1609+renames the target dir to a temporary unique whiteouted name before the
7e9cd9fe 1610+actual rename on a branch, and then handles other actions (make it opaque,
53392da6
AM
1611+update the attributes, etc). If an error happens in these actions, aufs
1612+simply renames the whiteouted name back and returns an error. If all are
1613+succeeded, aufs registers a function to remove the whiteouted unique
1614+temporary name completely and asynchronously to the system global
1615+workqueue.
1616+
1617+
1618+Copy-up
1619+----------------------------------------------------------------------
1620+It is a well-known feature or concept.
1621+When user modifies a file on a readonly branch, aufs operate "copy-up"
1622+internally and makes change to the new file on the upper writable branch.
1623+When the trigger systemcall does not update the timestamps of the parent
1624+dir, aufs reverts it after copy-up.
c2b27bf2
AM
1625+
1626+
1627+Move-down (aufs3.9 and later)
1628+----------------------------------------------------------------------
1629+"Copy-up" is one of the essential feature in aufs. It copies a file from
1630+the lower readonly branch to the upper writable branch when a user
1631+changes something about the file.
1632+"Move-down" is an opposite action of copy-up. Basically this action is
1633+ran manually instead of automatically and internally.
076b876e
AM
1634+For desgin and implementation, aufs has to consider these issues.
1635+- whiteout for the file may exist on the lower branch.
1636+- ancestor directories may not exist on the lower branch.
1637+- diropq for the ancestor directories may exist on the upper branch.
1638+- free space on the lower branch will reduce.
1639+- another access to the file may happen during moving-down, including
7e9cd9fe 1640+ UDBA (see "Revalidate Dentry and UDBA").
076b876e
AM
1641+- the file should not be hard-linked nor pseudo-linked. they should be
1642+ handled by auplink utility later.
c2b27bf2
AM
1643+
1644+Sometimes users want to move-down a file from the upper writable branch
1645+to the lower readonly or writable branch. For instance,
1646+- the free space of the upper writable branch is going to run out.
1647+- create a new intermediate branch between the upper and lower branch.
1648+- etc.
1649+
1650+For this purpose, use "aumvdown" command in aufs-util.git.
b912730e
AM
1651diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt linux/Documentation/filesystems/aufs/design/03atomic_open.txt
1652--- /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt 1970-01-01 01:00:00.000000000 +0100
0b487229 1653+++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt 2021-02-22 23:30:37.699347580 +0100
b912730e
AM
1654@@ -0,0 +1,85 @@
1655+
0b487229 1656+# Copyright (C) 2015-2020 Junjiro R. Okajima
b912730e
AM
1657+#
1658+# This program is free software; you can redistribute it and/or modify
1659+# it under the terms of the GNU General Public License as published by
1660+# the Free Software Foundation; either version 2 of the License, or
1661+# (at your option) any later version.
1662+#
1663+# This program is distributed in the hope that it will be useful,
1664+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1665+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1666+# GNU General Public License for more details.
1667+#
1668+# You should have received a copy of the GNU General Public License
1669+# along with this program. If not, see <http://www.gnu.org/licenses/>.
1670+
1671+Support for a branch who has its ->atomic_open()
1672+----------------------------------------------------------------------
1673+The filesystems who implement its ->atomic_open() are not majority. For
1674+example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
1675+particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
1676+->atomic_open(), NFSv4 returns an error for this open(2). While I am not
1677+sure whether all filesystems who have ->atomic_open() behave like this,
1678+but NFSv4 surely returns the error.
1679+
1680+In order to support ->atomic_open() for aufs, there are a few
1681+approaches.
1682+
1683+A. Introduce aufs_atomic_open()
1684+ - calls one of VFS:do_last(), lookup_open() or atomic_open() for
1685+ branch fs.
1686+B. Introduce aufs_atomic_open() calling create, open and chmod. this is
1687+ an aufs user Pip Cet's approach
1688+ - calls aufs_create(), VFS finish_open() and notify_change().
1689+ - pass fake-mode to finish_open(), and then correct the mode by
1690+ notify_change().
1691+C. Extend aufs_open() to call branch fs's ->atomic_open()
1692+ - no aufs_atomic_open().
1693+ - aufs_lookup() registers the TID to an aufs internal object.
1694+ - aufs_create() does nothing when the matching TID is registered, but
1695+ registers the mode.
1696+ - aufs_open() calls branch fs's ->atomic_open() when the matching
1697+ TID is registered.
1698+D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
1699+ credential
1700+ - no aufs_atomic_open().
1701+ - aufs_create() registers the TID to an internal object. this info
1702+ represents "this process created this file just now."
1703+ - when aufs gets EACCES from branch fs's ->open(), then confirm the
1704+ registered TID and re-try open() with superuser's credential.
1705+
1706+Pros and cons for each approach.
1707+
1708+A.
1709+ - straightforward but highly depends upon VFS internal.
1710+ - the atomic behavaiour is kept.
1711+ - some of parameters such as nameidata are hard to reproduce for
1712+ branch fs.
1713+ - large overhead.
1714+B.
1715+ - easy to implement.
1716+ - the atomic behavaiour is lost.
1717+C.
1718+ - the atomic behavaiour is kept.
1719+ - dirty and tricky.
1720+ - VFS checks whether the file is created correctly after calling
1721+ ->create(), which means this approach doesn't work.
1722+D.
1723+ - easy to implement.
1724+ - the atomic behavaiour is lost.
1725+ - to open a file with superuser's credential and give it to a user
1726+ process is a bad idea, since the file object keeps the credential
1727+ in it. It may affect LSM or something. This approach doesn't work
1728+ either.
1729+
1730+The approach A is ideal, but it hard to implement. So here is a
1731+variation of A, which is to be implemented.
1732+
1733+A-1. Introduce aufs_atomic_open()
1734+ - calls branch fs ->atomic_open() if exists. otherwise calls
1735+ vfs_create() and finish_open().
1736+ - the demerit is that the several checks after branch fs
1737+ ->atomic_open() are lost. in the ordinary case, the checks are
1738+ done by VFS:do_last(), lookup_open() and atomic_open(). some can
1739+ be implemented in aufs, but not all I am afraid.
53392da6
AM
1740diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
1741--- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
0b487229 1742+++ linux/Documentation/filesystems/aufs/design/03lookup.txt 2021-02-22 23:30:37.699347580 +0100
7e9cd9fe 1743@@ -0,0 +1,113 @@
53392da6 1744+
0b487229 1745+# Copyright (C) 2005-2020 Junjiro R. Okajima
53392da6
AM
1746+#
1747+# This program is free software; you can redistribute it and/or modify
1748+# it under the terms of the GNU General Public License as published by
1749+# the Free Software Foundation; either version 2 of the License, or
1750+# (at your option) any later version.
1751+#
1752+# This program is distributed in the hope that it will be useful,
1753+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1754+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1755+# GNU General Public License for more details.
1756+#
1757+# You should have received a copy of the GNU General Public License
523b37e3 1758+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1759+
1760+Lookup in a Branch
1761+----------------------------------------------------------------------
1762+Since aufs has a character of sub-VFS (see Introduction), it operates
7e9cd9fe
AM
1763+lookup for branches as VFS does. It may be a heavy work. But almost all
1764+lookup operation in aufs is the simplest case, ie. lookup only an entry
1765+directly connected to its parent. Digging down the directory hierarchy
1766+is unnecessary. VFS has a function lookup_one_len() for that use, and
1767+aufs calls it.
1768+
1769+When a branch is a remote filesystem, aufs basically relies upon its
53392da6
AM
1770+->d_revalidate(), also aufs forces the hardest revalidate tests for
1771+them.
1772+For d_revalidate, aufs implements three levels of revalidate tests. See
1773+"Revalidate Dentry and UDBA" in detail.
1774+
1775+
076b876e
AM
1776+Test Only the Highest One for the Directory Permission (dirperm1 option)
1777+----------------------------------------------------------------------
1778+Let's try case study.
1779+- aufs has two branches, upper readwrite and lower readonly.
1780+ /au = /rw + /ro
1781+- "dirA" exists under /ro, but /rw. and its mode is 0700.
1782+- user invoked "chmod a+rx /au/dirA"
1783+- the internal copy-up is activated and "/rw/dirA" is created and its
7e9cd9fe 1784+ permission bits are set to world readable.
076b876e
AM
1785+- then "/au/dirA" becomes world readable?
1786+
1787+In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1788+or it may be a natively readonly filesystem. If aufs respects the lower
1789+branch, it should not respond readdir request from other users. But user
1790+allowed it by chmod. Should really aufs rejects showing the entries
1791+under /ro/dirA?
1792+
7e9cd9fe
AM
1793+To be honest, I don't have a good solution for this case. So aufs
1794+implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1795+users.
076b876e
AM
1796+When dirperm1 is specified, aufs checks only the highest one for the
1797+directory permission, and shows the entries. Otherwise, as usual, checks
1798+every dir existing on all branches and rejects the request.
1799+
1800+As a side effect, dirperm1 option improves the performance of aufs
1801+because the number of permission check is reduced when the number of
1802+branch is many.
1803+
1804+
53392da6
AM
1805+Revalidate Dentry and UDBA (User's Direct Branch Access)
1806+----------------------------------------------------------------------
1807+Generally VFS helpers re-validate a dentry as a part of lookup.
1808+0. digging down the directory hierarchy.
1809+1. lock the parent dir by its i_mutex.
1810+2. lookup the final (child) entry.
1811+3. revalidate it.
1812+4. call the actual operation (create, unlink, etc.)
1813+5. unlock the parent dir
1814+
1815+If the filesystem implements its ->d_revalidate() (step 3), then it is
1816+called. Actually aufs implements it and checks the dentry on a branch is
1817+still valid.
1818+But it is not enough. Because aufs has to release the lock for the
1819+parent dir on a branch at the end of ->lookup() (step 2) and
1820+->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1821+held by VFS.
1822+If the file on a branch is changed directly, eg. bypassing aufs, after
1823+aufs released the lock, then the subsequent operation may cause
1824+something unpleasant result.
1825+
1826+This situation is a result of VFS architecture, ->lookup() and
1827+->d_revalidate() is separated. But I never say it is wrong. It is a good
1828+design from VFS's point of view. It is just not suitable for sub-VFS
1829+character in aufs.
1830+
1831+Aufs supports such case by three level of revalidation which is
1832+selectable by user.
1833+1. Simple Revalidate
1834+ Addition to the native flow in VFS's, confirm the child-parent
1835+ relationship on the branch just after locking the parent dir on the
1836+ branch in the "actual operation" (step 4). When this validation
1837+ fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1838+ checks the validation of the dentry on branches.
1839+2. Monitor Changes Internally by Inotify/Fsnotify
1840+ Addition to above, in the "actual operation" (step 4) aufs re-lookup
1841+ the dentry on the branch, and returns EBUSY if it finds different
1842+ dentry.
1843+ Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1844+ during it is in cache. When the event is notified, aufs registers a
1845+ function to kernel 'events' thread by schedule_work(). And the
1846+ function sets some special status to the cached aufs dentry and inode
1847+ private data. If they are not cached, then aufs has nothing to
1848+ do. When the same file is accessed through aufs (step 0-3) later,
1849+ aufs will detect the status and refresh all necessary data.
1850+ In this mode, aufs has to ignore the event which is fired by aufs
1851+ itself.
1852+3. No Extra Validation
1853+ This is the simplest test and doesn't add any additional revalidation
7e9cd9fe 1854+ test, and skip the revalidation in step 4. It is useful and improves
53392da6
AM
1855+ aufs performance when system surely hide the aufs branches from user,
1856+ by over-mounting something (or another method).
1857diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
1858--- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
0b487229 1859+++ linux/Documentation/filesystems/aufs/design/04branch.txt 2021-02-22 23:30:37.699347580 +0100
7e9cd9fe 1860@@ -0,0 +1,74 @@
53392da6 1861+
0b487229 1862+# Copyright (C) 2005-2020 Junjiro R. Okajima
53392da6
AM
1863+#
1864+# This program is free software; you can redistribute it and/or modify
1865+# it under the terms of the GNU General Public License as published by
1866+# the Free Software Foundation; either version 2 of the License, or
1867+# (at your option) any later version.
1868+#
1869+# This program is distributed in the hope that it will be useful,
1870+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1871+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1872+# GNU General Public License for more details.
1873+#
1874+# You should have received a copy of the GNU General Public License
523b37e3 1875+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1876+
1877+Branch Manipulation
1878+
1879+Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1880+and changing its permission/attribute, there are a lot of works to do.
1881+
1882+
1883+Add a Branch
1884+----------------------------------------------------------------------
1885+o Confirm the adding dir exists outside of aufs, including loopback
7e9cd9fe 1886+ mount, and its various attributes.
53392da6
AM
1887+o Initialize the xino file and whiteout bases if necessary.
1888+ See struct.txt.
1889+
1890+o Check the owner/group/mode of the directory
1891+ When the owner/group/mode of the adding directory differs from the
1892+ existing branch, aufs issues a warning because it may impose a
1893+ security risk.
1894+ For example, when a upper writable branch has a world writable empty
1895+ top directory, a malicious user can create any files on the writable
1896+ branch directly, like copy-up and modify manually. If something like
1897+ /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1898+ writable branch, and the writable branch is world-writable, then a
1899+ malicious guy may create /etc/passwd on the writable branch directly
1900+ and the infected file will be valid in aufs.
7e9cd9fe 1901+ I am afraid it can be a security issue, but aufs can do nothing except
53392da6
AM
1902+ producing a warning.
1903+
1904+
1905+Delete a Branch
1906+----------------------------------------------------------------------
1907+o Confirm the deleting branch is not busy
1908+ To be general, there is one merit to adopt "remount" interface to
1909+ manipulate branches. It is to discard caches. At deleting a branch,
1910+ aufs checks the still cached (and connected) dentries and inodes. If
1911+ there are any, then they are all in-use. An inode without its
1912+ corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1913+
1914+ For the cached one, aufs checks whether the same named entry exists on
1915+ other branches.
1916+ If the cached one is a directory, because aufs provides a merged view
1917+ to users, as long as one dir is left on any branch aufs can show the
1918+ dir to users. In this case, the branch can be removed from aufs.
1919+ Otherwise aufs rejects deleting the branch.
1920+
1921+ If any file on the deleting branch is opened by aufs, then aufs
1922+ rejects deleting.
1923+
1924+
1925+Modify the Permission of a Branch
1926+----------------------------------------------------------------------
1927+o Re-initialize or remove the xino file and whiteout bases if necessary.
1928+ See struct.txt.
1929+
1930+o rw --> ro: Confirm the modifying branch is not busy
1931+ Aufs rejects the request if any of these conditions are true.
1932+ - a file on the branch is mmap-ed.
1933+ - a regular file on the branch is opened for write and there is no
1934+ same named entry on the upper branch.
1935diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
1936--- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt 1970-01-01 01:00:00.000000000 +0100
0b487229 1937+++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt 2021-02-22 23:30:37.699347580 +0100
523b37e3 1938@@ -0,0 +1,64 @@
53392da6 1939+
0b487229 1940+# Copyright (C) 2005-2020 Junjiro R. Okajima
53392da6
AM
1941+#
1942+# This program is free software; you can redistribute it and/or modify
1943+# it under the terms of the GNU General Public License as published by
1944+# the Free Software Foundation; either version 2 of the License, or
1945+# (at your option) any later version.
1946+#
1947+# This program is distributed in the hope that it will be useful,
1948+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1949+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1950+# GNU General Public License for more details.
1951+#
1952+# You should have received a copy of the GNU General Public License
523b37e3 1953+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1954+
1955+Policies to Select One among Multiple Writable Branches
1956+----------------------------------------------------------------------
1957+When the number of writable branch is more than one, aufs has to decide
1958+the target branch for file creation or copy-up. By default, the highest
1959+writable branch which has the parent (or ancestor) dir of the target
1960+file is chosen (top-down-parent policy).
1961+By user's request, aufs implements some other policies to select the
7e9cd9fe
AM
1962+writable branch, for file creation several policies, round-robin,
1963+most-free-space, and other policies. For copy-up, top-down-parent,
1964+bottom-up-parent, bottom-up and others.
53392da6
AM
1965+
1966+As expected, the round-robin policy selects the branch in circular. When
1967+you have two writable branches and creates 10 new files, 5 files will be
1968+created for each branch. mkdir(2) systemcall is an exception. When you
1969+create 10 new directories, all will be created on the same branch.
1970+And the most-free-space policy selects the one which has most free
1971+space among the writable branches. The amount of free space will be
1972+checked by aufs internally, and users can specify its time interval.
1973+
1974+The policies for copy-up is more simple,
1975+top-down-parent is equivalent to the same named on in create policy,
1976+bottom-up-parent selects the writable branch where the parent dir
1977+exists and the nearest upper one from the copyup-source,
1978+bottom-up selects the nearest upper writable branch from the
1979+copyup-source, regardless the existence of the parent dir.
1980+
1981+There are some rules or exceptions to apply these policies.
1982+- If there is a readonly branch above the policy-selected branch and
1983+ the parent dir is marked as opaque (a variation of whiteout), or the
1984+ target (creating) file is whiteout-ed on the upper readonly branch,
1985+ then the result of the policy is ignored and the target file will be
1986+ created on the nearest upper writable branch than the readonly branch.
1987+- If there is a writable branch above the policy-selected branch and
1988+ the parent dir is marked as opaque or the target file is whiteouted
1989+ on the branch, then the result of the policy is ignored and the target
1990+ file will be created on the highest one among the upper writable
1991+ branches who has diropq or whiteout. In case of whiteout, aufs removes
1992+ it as usual.
1993+- link(2) and rename(2) systemcalls are exceptions in every policy.
1994+ They try selecting the branch where the source exists as possible
1995+ since copyup a large file will take long time. If it can't be,
1996+ ie. the branch where the source exists is readonly, then they will
1997+ follow the copyup policy.
1998+- There is an exception for rename(2) when the target exists.
1999+ If the rename target exists, aufs compares the index of the branches
2000+ where the source and the target exists and selects the higher
2001+ one. If the selected branch is readonly, then aufs follows the
2002+ copyup policy.
8b6a4947
AM
2003diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot linux/Documentation/filesystems/aufs/design/06dirren.dot
2004--- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot 1970-01-01 01:00:00.000000000 +0100
0b487229 2005+++ linux/Documentation/filesystems/aufs/design/06dirren.dot 2021-02-22 23:30:37.699347580 +0100
8b6a4947
AM
2006@@ -0,0 +1,31 @@
2007+
2008+// to view this graph, run dot(1) command in GRAPHVIZ.
2009+
2010+digraph G {
2011+node [shape=box];
2012+whinfo [label="detailed info file\n(lower_brid_root-hinum, h_inum, namelen, old name)"];
2013+
2014+node [shape=oval];
2015+
2016+aufs_rename -> whinfo [label="store/remove"];
2017+
2018+node [shape=oval];
2019+inode_list [label="h_inum list in branch\ncache"];
2020+
2021+node [shape=box];
2022+whinode [label="h_inum list file"];
2023+
2024+node [shape=oval];
2025+brmgmt [label="br_add/del/mod/umount"];
2026+
2027+brmgmt -> inode_list [label="create/remove"];
2028+brmgmt -> whinode [label="load/store"];
2029+
2030+inode_list -> whinode [style=dashed,dir=both];
2031+
2032+aufs_rename -> inode_list [label="add/del"];
2033+
2034+aufs_lookup -> inode_list [label="search"];
2035+
2036+aufs_lookup -> whinfo [label="load/remove"];
2037+}
2038diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt linux/Documentation/filesystems/aufs/design/06dirren.txt
2039--- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt 1970-01-01 01:00:00.000000000 +0100
0b487229 2040+++ linux/Documentation/filesystems/aufs/design/06dirren.txt 2021-02-22 23:30:37.699347580 +0100
8b6a4947
AM
2041@@ -0,0 +1,102 @@
2042+
0b487229 2043+# Copyright (C) 2017-2020 Junjiro R. Okajima
8b6a4947
AM
2044+#
2045+# This program is free software; you can redistribute it and/or modify
2046+# it under the terms of the GNU General Public License as published by
2047+# the Free Software Foundation; either version 2 of the License, or
2048+# (at your option) any later version.
2049+#
2050+# This program is distributed in the hope that it will be useful,
2051+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2052+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2053+# GNU General Public License for more details.
2054+#
2055+# You should have received a copy of the GNU General Public License
2056+# along with this program. If not, see <http://www.gnu.org/licenses/>.
2057+
2058+Special handling for renaming a directory (DIRREN)
2059+----------------------------------------------------------------------
2060+First, let's assume we have a simple usecase.
2061+
2062+- /u = /rw + /ro
2063+- /rw/dirA exists
2064+- /ro/dirA and /ro/dirA/file exist too
2065+- there is no dirB on both branches
2066+- a user issues rename("dirA", "dirB")
2067+
2068+Now, what should aufs behave against this rename(2)?
2069+There are a few possible cases.
2070+
2071+A. returns EROFS.
2072+ since dirA exists on a readonly branch which cannot be renamed.
2073+B. returns EXDEV.
2074+ it is possible to copy-up dirA (only the dir itself), but the child
2075+ entries ("file" in this case) should not be. it must be a bad
2076+ approach to copy-up recursively.
2077+C. returns a success.
2078+ even the branch /ro is readonly, aufs tries renaming it. Obviously it
2079+ is a violation of aufs' policy.
2080+D. construct an extra information which indicates that /ro/dirA should
2081+ be handled as the name of dirB.
2082+ overlayfs has a similar feature called REDIRECT.
2083+
2084+Until now, aufs implements the case B only which returns EXDEV, and
2085+expects the userspace application behaves like mv(1) which tries
2086+issueing rename(2) recursively.
2087+
2088+A new aufs feature called DIRREN is introduced which implements the case
2089+D. There are several "extra information" added.
2090+
2091+1. detailed info per renamed directory
2092+ path: /rw/dirB/$AUFS_WH_DR_INFO_PFX.<lower branch-id>
2093+2. the inode-number list of directories on a branch
2094+ path: /rw/dirB/$AUFS_WH_DR_BRHINO
2095+
2096+The filename of "detailed info per directory" represents the lower
2097+branch, and its format is
2098+- a type of the branch id
2099+ one of these.
2100+ + uuid (not implemented yet)
2101+ + fsid
2102+ + dev
2103+- the inode-number of the branch root dir
2104+
2105+And it contains these info in a single regular file.
2106+- magic number
2107+- branch's inode-number of the logically renamed dir
2108+- the name of the before-renamed dir
2109+
2110+The "detailed info per directory" file is created in aufs rename(2), and
2111+loaded in any lookup.
2112+The info is considered in lookup for the matching case only. Here
2113+"matching" means that the root of branch (in the info filename) is same
2114+to the current looking-up branch. After looking-up the before-renamed
2115+name, the inode-number is compared. And the matched dentry is used.
2116+
2117+The "inode-number list of directories" is a regular file which contains
2118+simply the inode-numbers on the branch. The file is created or updated
2119+in removing the branch, and loaded in adding the branch. Its lifetime is
2120+equal to the branch.
2121+The list is refered in lookup, and when the current target inode is
2122+found in the list, the aufs tries loading the "detailed info per
2123+directory" and get the changed and valid name of the dir.
2124+
2125+Theoretically these "extra informaiton" may be able to be put into XATTR
2126+in the dir inode. But aufs doesn't choose this way because
2127+1. XATTR may not be supported by the branch (or its configuration)
2128+2. XATTR may have its size limit.
2129+3. XATTR may be less easy to convert than a regular file, when the
2130+ format of the info is changed in the future.
2131+At the same time, I agree that the regular file approach is much slower
2132+than XATTR approach. So, in the future, aufs may take the XATTR or other
2133+better approach.
2134+
2135+This DIRREN feature is enabled by aufs configuration, and is activated
2136+by a new mount option.
2137+
2138+For the more complicated case, there is a work with UDBA option, which
2139+is to dected the direct access to the branches (by-passing aufs) and to
2140+maintain the cashes in aufs. Since a single cached aufs dentry may
2141+contains two names, before- and after-rename, the name comparision in
2142+UDBA handler may not work correctly. In this case, the behaviour will be
2143+equivalen to udba=reval case.
076b876e
AM
2144diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt linux/Documentation/filesystems/aufs/design/06fhsm.txt
2145--- /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt 1970-01-01 01:00:00.000000000 +0100
0b487229 2146+++ linux/Documentation/filesystems/aufs/design/06fhsm.txt 2021-02-22 23:30:37.699347580 +0100
076b876e
AM
2147@@ -0,0 +1,120 @@
2148+
0b487229 2149+# Copyright (C) 2011-2020 Junjiro R. Okajima
076b876e
AM
2150+#
2151+# This program is free software; you can redistribute it and/or modify
2152+# it under the terms of the GNU General Public License as published by
2153+# the Free Software Foundation; either version 2 of the License, or
2154+# (at your option) any later version.
2155+#
2156+# This program is distributed in the hope that it will be useful,
2157+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2158+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2159+# GNU General Public License for more details.
2160+#
2161+# You should have received a copy of the GNU General Public License
2162+# along with this program; if not, write to the Free Software
2163+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2164+
2165+
2166+File-based Hierarchical Storage Management (FHSM)
2167+----------------------------------------------------------------------
2168+Hierarchical Storage Management (or HSM) is a well-known feature in the
2169+storage world. Aufs provides this feature as file-based with multiple
7e9cd9fe 2170+writable branches, based upon the principle of "Colder, the Lower".
076b876e 2171+Here the word "colder" means that the less used files, and "lower" means
7e9cd9fe 2172+that the position in the order of the stacked branches vertically.
076b876e
AM
2173+These multiple writable branches are prioritized, ie. the topmost one
2174+should be the fastest drive and be used heavily.
2175+
2176+o Characters in aufs FHSM story
2177+- aufs itself and a new branch attribute.
2178+- a new ioctl interface to move-down and to establish a connection with
2179+ the daemon ("move-down" is a converse of "copy-up").
2180+- userspace tool and daemon.
2181+
2182+The userspace daemon establishes a connection with aufs and waits for
2183+the notification. The notified information is very similar to struct
2184+statfs containing the number of consumed blocks and inodes.
2185+When the consumed blocks/inodes of a branch exceeds the user-specified
2186+upper watermark, the daemon activates its move-down process until the
2187+consumed blocks/inodes reaches the user-specified lower watermark.
2188+
2189+The actual move-down is done by aufs based upon the request from
2190+user-space since we need to maintain the inode number and the internal
2191+pointer arrays in aufs.
2192+
2193+Currently aufs FHSM handles the regular files only. Additionally they
2194+must not be hard-linked nor pseudo-linked.
2195+
2196+
2197+o Cowork of aufs and the user-space daemon
2198+ During the userspace daemon established the connection, aufs sends a
2199+ small notification to it whenever aufs writes something into the
2200+ writable branch. But it may cost high since aufs issues statfs(2)
2201+ internally. So user can specify a new option to cache the
2202+ info. Actually the notification is controlled by these factors.
2203+ + the specified cache time.
2204+ + classified as "force" by aufs internally.
2205+ Until the specified time expires, aufs doesn't send the info
2206+ except the forced cases. When aufs decide forcing, the info is always
2207+ notified to userspace.
2208+ For example, the number of free inodes is generally large enough and
2209+ the shortage of it happens rarely. So aufs doesn't force the
2210+ notification when creating a new file, directory and others. This is
2211+ the typical case which aufs doesn't force.
2212+ When aufs writes the actual filedata and the files consumes any of new
2213+ blocks, the aufs forces notifying.
2214+
2215+
2216+o Interfaces in aufs
2217+- New branch attribute.
2218+ + fhsm
2219+ Specifies that the branch is managed by FHSM feature. In other word,
2220+ participant in the FHSM.
2221+ When nofhsm is set to the branch, it will not be the source/target
2222+ branch of the move-down operation. This attribute is set
2223+ independently from coo and moo attributes, and if you want full
2224+ FHSM, you should specify them as well.
2225+- New mount option.
2226+ + fhsm_sec
2227+ Specifies a second to suppress many less important info to be
2228+ notified.
2229+- New ioctl.
2230+ + AUFS_CTL_FHSM_FD
2231+ create a new file descriptor which userspace can read the notification
2232+ (a subset of struct statfs) from aufs.
2233+- Module parameter 'brs'
2234+ It has to be set to 1. Otherwise the new mount option 'fhsm' will not
2235+ be set.
2236+- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
2237+ When there are two or more branches with fhsm attributes,
2238+ /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
2239+ terminates it. As a result of remounting and branch-manipulation, the
2240+ number of branches with fhsm attribute can be one. In this case,
2241+ /sbin/mount.aufs will terminate the user-space daemon.
2242+
2243+
2244+Finally the operation is done as these steps in kernel-space.
2245+- make sure that,
2246+ + no one else is using the file.
2247+ + the file is not hard-linked.
2248+ + the file is not pseudo-linked.
2249+ + the file is a regular file.
2250+ + the parent dir is not opaqued.
2251+- find the target writable branch.
2252+- make sure the file is not whiteout-ed by the upper (than the target)
2253+ branch.
2254+- make the parent dir on the target branch.
2255+- mutex lock the inode on the branch.
2256+- unlink the whiteout on the target branch (if exists).
2257+- lookup and create the whiteout-ed temporary name on the target branch.
2258+- copy the file as the whiteout-ed temporary name on the target branch.
2259+- rename the whiteout-ed temporary name to the original name.
2260+- unlink the file on the source branch.
2261+- maintain the internal pointer array and the external inode number
2262+ table (XINO).
2263+- maintain the timestamps and other attributes of the parent dir and the
2264+ file.
2265+
2266+And of course, in every step, an error may happen. So the operation
2267+should restore the original file state after an error happens.
53392da6
AM
2268diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
2269--- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt 1970-01-01 01:00:00.000000000 +0100
0b487229 2270+++ linux/Documentation/filesystems/aufs/design/06mmap.txt 2021-02-22 23:30:37.699347580 +0100
b912730e 2271@@ -0,0 +1,72 @@
53392da6 2272+
0b487229 2273+# Copyright (C) 2005-2020 Junjiro R. Okajima
53392da6
AM
2274+#
2275+# This program is free software; you can redistribute it and/or modify
2276+# it under the terms of the GNU General Public License as published by
2277+# the Free Software Foundation; either version 2 of the License, or
2278+# (at your option) any later version.
2279+#
2280+# This program is distributed in the hope that it will be useful,
2281+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2282+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2283+# GNU General Public License for more details.
2284+#
2285+# You should have received a copy of the GNU General Public License
523b37e3 2286+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2287+
2288+mmap(2) -- File Memory Mapping
2289+----------------------------------------------------------------------
2290+In aufs, the file-mapped pages are handled by a branch fs directly, no
2291+interaction with aufs. It means aufs_mmap() calls the branch fs's
2292+->mmap().
2293+This approach is simple and good, but there is one problem.
7e9cd9fe 2294+Under /proc, several entries show the mmapped files by its path (with
53392da6
AM
2295+device and inode number), and the printed path will be the path on the
2296+branch fs's instead of virtual aufs's.
2297+This is not a problem in most cases, but some utilities lsof(1) (and its
2298+user) may expect the path on aufs.
2299+
2300+To address this issue, aufs adds a new member called vm_prfile in struct
2301+vm_area_struct (and struct vm_region). The original vm_file points to
2302+the file on the branch fs in order to handle everything correctly as
2303+usual. The new vm_prfile points to a virtual file in aufs, and the
2304+show-functions in procfs refers to vm_prfile if it is set.
2305+Also we need to maintain several other places where touching vm_file
2306+such like
2307+- fork()/clone() copies vma and the reference count of vm_file is
2308+ incremented.
2309+- merging vma maintains the ref count too.
2310+
7e9cd9fe 2311+This is not a good approach. It just fakes the printed path. But it
53392da6
AM
2312+leaves all behaviour around f_mapping unchanged. This is surely an
2313+advantage.
2314+Actually aufs had adopted another complicated approach which calls
2315+generic_file_mmap() and handles struct vm_operations_struct. In this
2316+approach, aufs met a hard problem and I could not solve it without
2317+switching the approach.
b912730e
AM
2318+
2319+There may be one more another approach which is
2320+- bind-mount the branch-root onto the aufs-root internally
2321+- grab the new vfsmount (ie. struct mount)
2322+- lazy-umount the branch-root internally
2323+- in open(2) the aufs-file, open the branch-file with the hidden
2324+ vfsmount (instead of the original branch's vfsmount)
2325+- ideally this "bind-mount and lazy-umount" should be done atomically,
2326+ but it may be possible from userspace by the mount helper.
2327+
2328+Adding the internal hidden vfsmount and using it in opening a file, the
2329+file path under /proc will be printed correctly. This approach looks
2330+smarter, but is not possible I am afraid.
2331+- aufs-root may be bind-mount later. when it happens, another hidden
2332+ vfsmount will be required.
2333+- it is hard to get the chance to bind-mount and lazy-umount
2334+ + in kernel-space, FS can have vfsmount in open(2) via
2335+ file->f_path, and aufs can know its vfsmount. But several locks are
2336+ already acquired, and if aufs tries to bind-mount and lazy-umount
2337+ here, then it may cause a deadlock.
2338+ + in user-space, bind-mount doesn't invoke the mount helper.
2339+- since /proc shows dev and ino, aufs has to give vma these info. it
2340+ means a new member vm_prinode will be necessary. this is essentially
2341+ equivalent to vm_prfile described above.
2342+
2343+I have to give up this "looks-smater" approach.
c1595e42
JR
2344diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt linux/Documentation/filesystems/aufs/design/06xattr.txt
2345--- /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt 1970-01-01 01:00:00.000000000 +0100
0b487229 2346+++ linux/Documentation/filesystems/aufs/design/06xattr.txt 2021-02-22 23:30:37.699347580 +0100
c1595e42
JR
2347@@ -0,0 +1,96 @@
2348+
0b487229 2349+# Copyright (C) 2014-2020 Junjiro R. Okajima
c1595e42
JR
2350+#
2351+# This program is free software; you can redistribute it and/or modify
2352+# it under the terms of the GNU General Public License as published by
2353+# the Free Software Foundation; either version 2 of the License, or
2354+# (at your option) any later version.
2355+#
2356+# This program is distributed in the hope that it will be useful,
2357+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2358+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2359+# GNU General Public License for more details.
2360+#
2361+# You should have received a copy of the GNU General Public License
2362+# along with this program; if not, write to the Free Software
2363+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2364+
2365+
2366+Listing XATTR/EA and getting the value
2367+----------------------------------------------------------------------
2368+For the inode standard attributes (owner, group, timestamps, etc.), aufs
2369+shows the values from the topmost existing file. This behaviour is good
7e9cd9fe 2370+for the non-dir entries since the bahaviour exactly matches the shown
c1595e42
JR
2371+information. But for the directories, aufs considers all the same named
2372+entries on the lower branches. Which means, if one of the lower entry
2373+rejects readdir call, then aufs returns an error even if the topmost
2374+entry allows it. This behaviour is necessary to respect the branch fs's
2375+security, but can make users confused since the user-visible standard
2376+attributes don't match the behaviour.
2377+To address this issue, aufs has a mount option called dirperm1 which
2378+checks the permission for the topmost entry only, and ignores the lower
2379+entry's permission.
2380+
2381+A similar issue can happen around XATTR.
2382+getxattr(2) and listxattr(2) families behave as if dirperm1 option is
7e9cd9fe
AM
2383+always set. Otherwise these very unpleasant situation would happen.
2384+- listxattr(2) may return the duplicated entries.
c1595e42
JR
2385+- users may not be able to remove or reset the XATTR forever,
2386+
2387+
2388+XATTR/EA support in the internal (copy,move)-(up,down)
2389+----------------------------------------------------------------------
7e9cd9fe 2390+Generally the extended attributes of inode are categorized as these.
c1595e42
JR
2391+- "security" for LSM and capability.
2392+- "system" for posix ACL, 'acl' mount option is required for the branch
2393+ fs generally.
2394+- "trusted" for userspace, CAP_SYS_ADMIN is required.
2395+- "user" for userspace, 'user_xattr' mount option is required for the
2396+ branch fs generally.
2397+
2398+Moreover there are some other categories. Aufs handles these rather
2399+unpopular categories as the ordinary ones, ie. there is no special
2400+condition nor exception.
2401+
2402+In copy-up, the support for XATTR on the dst branch may differ from the
2403+src branch. In this case, the copy-up operation will get an error and
7e9cd9fe
AM
2404+the original user operation which triggered the copy-up will fail. It
2405+can happen that even all copy-up will fail.
c1595e42
JR
2406+When both of src and dst branches support XATTR and if an error occurs
2407+during copying XATTR, then the copy-up should fail obviously. That is a
2408+good reason and aufs should return an error to userspace. But when only
7e9cd9fe 2409+the src branch support that XATTR, aufs should not return an error.
c1595e42
JR
2410+For example, the src branch supports ACL but the dst branch doesn't
2411+because the dst branch may natively un-support it or temporary
2412+un-support it due to "noacl" mount option. Of course, the dst branch fs
2413+may NOT return an error even if the XATTR is not supported. It is
2414+totally up to the branch fs.
2415+
2416+Anyway when the aufs internal copy-up gets an error from the dst branch
2417+fs, then aufs tries removing the just copied entry and returns the error
2418+to the userspace. The worst case of this situation will be all copy-up
2419+will fail.
2420+
2421+For the copy-up operation, there two basic approaches.
2422+- copy the specified XATTR only (by category above), and return the
7e9cd9fe 2423+ error unconditionally if it happens.
c1595e42
JR
2424+- copy all XATTR, and ignore the error on the specified category only.
2425+
2426+In order to support XATTR and to implement the correct behaviour, aufs
7e9cd9fe
AM
2427+chooses the latter approach and introduces some new branch attributes,
2428+"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
c1595e42 2429+They correspond to the XATTR namespaces (see above). Additionally, to be
7e9cd9fe
AM
2430+convenient, "icex" is also provided which means all "icex*" attributes
2431+are set (here the word "icex" stands for "ignore copy-error on XATTR").
c1595e42
JR
2432+
2433+The meaning of these attributes is to ignore the error from setting
2434+XATTR on that branch.
2435+Note that aufs tries copying all XATTR unconditionally, and ignores the
2436+error from the dst branch according to the specified attributes.
2437+
2438+Some XATTR may have its default value. The default value may come from
2439+the parent dir or the environment. If the default value is set at the
2440+file creating-time, it will be overwritten by copy-up.
2441+Some contradiction may happen I am afraid.
2442+Do we need another attribute to stop copying XATTR? I am unsure. For
2443+now, aufs implements the branch attributes to ignore the error.
53392da6
AM
2444diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
2445--- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
0b487229 2446+++ linux/Documentation/filesystems/aufs/design/07export.txt 2021-02-22 23:30:37.699347580 +0100
523b37e3 2447@@ -0,0 +1,58 @@
53392da6 2448+
0b487229 2449+# Copyright (C) 2005-2020 Junjiro R. Okajima
53392da6
AM
2450+#
2451+# This program is free software; you can redistribute it and/or modify
2452+# it under the terms of the GNU General Public License as published by
2453+# the Free Software Foundation; either version 2 of the License, or
2454+# (at your option) any later version.
2455+#
2456+# This program is distributed in the hope that it will be useful,
2457+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2458+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2459+# GNU General Public License for more details.
2460+#
2461+# You should have received a copy of the GNU General Public License
523b37e3 2462+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2463+
2464+Export Aufs via NFS
2465+----------------------------------------------------------------------
2466+Here is an approach.
2467+- like xino/xib, add a new file 'xigen' which stores aufs inode
2468+ generation.
2469+- iget_locked(): initialize aufs inode generation for a new inode, and
2470+ store it in xigen file.
2471+- destroy_inode(): increment aufs inode generation and store it in xigen
2472+ file. it is necessary even if it is not unlinked, because any data of
2473+ inode may be changed by UDBA.
2474+- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2475+ build file handle by
2476+ + branch id (4 bytes)
2477+ + superblock generation (4 bytes)
2478+ + inode number (4 or 8 bytes)
2479+ + parent dir inode number (4 or 8 bytes)
2480+ + inode generation (4 bytes))
2481+ + return value of exportfs_encode_fh() for the parent on a branch (4
2482+ bytes)
2483+ + file handle for a branch (by exportfs_encode_fh())
2484+- fh_to_dentry():
2485+ + find the index of a branch from its id in handle, and check it is
2486+ still exist in aufs.
2487+ + 1st level: get the inode number from handle and search it in cache.
7e9cd9fe
AM
2488+ + 2nd level: if not found in cache, get the parent inode number from
2489+ the handle and search it in cache. and then open the found parent
2490+ dir, find the matching inode number by vfs_readdir() and get its
2491+ name, and call lookup_one_len() for the target dentry.
53392da6
AM
2492+ + 3rd level: if the parent dir is not cached, call
2493+ exportfs_decode_fh() for a branch and get the parent on a branch,
2494+ build a pathname of it, convert it a pathname in aufs, call
2495+ path_lookup(). now aufs gets a parent dir dentry, then handle it as
2496+ the 2nd level.
2497+ + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2498+ for every branch, but not itself. to get this, (currently) aufs
2499+ searches in current->nsproxy->mnt_ns list. it may not be a good
2500+ idea, but I didn't get other approach.
2501+ + test the generation of the gotten inode.
2502+- every inode operation: they may get EBUSY due to UDBA. in this case,
2503+ convert it into ESTALE for NFSD.
2504+- readdir(): call lockdep_on/off() because filldir in NFSD calls
2505+ lookup_one_len(), vfs_getattr(), encode_fh() and others.
2506diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
2507--- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt 1970-01-01 01:00:00.000000000 +0100
0b487229 2508+++ linux/Documentation/filesystems/aufs/design/08shwh.txt 2021-02-22 23:30:37.699347580 +0100
523b37e3 2509@@ -0,0 +1,52 @@
53392da6 2510+
0b487229 2511+# Copyright (C) 2005-2020 Junjiro R. Okajima
53392da6
AM
2512+#
2513+# This program is free software; you can redistribute it and/or modify
2514+# it under the terms of the GNU General Public License as published by
2515+# the Free Software Foundation; either version 2 of the License, or
2516+# (at your option) any later version.
2517+#
2518+# This program is distributed in the hope that it will be useful,
2519+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2520+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2521+# GNU General Public License for more details.
2522+#
2523+# You should have received a copy of the GNU General Public License
523b37e3 2524+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2525+
2526+Show Whiteout Mode (shwh)
2527+----------------------------------------------------------------------
2528+Generally aufs hides the name of whiteouts. But in some cases, to show
2529+them is very useful for users. For instance, creating a new middle layer
2530+(branch) by merging existing layers.
2531+
2532+(borrowing aufs1 HOW-TO from a user, Michael Towers)
2533+When you have three branches,
2534+- Bottom: 'system', squashfs (underlying base system), read-only
2535+- Middle: 'mods', squashfs, read-only
2536+- Top: 'overlay', ram (tmpfs), read-write
2537+
2538+The top layer is loaded at boot time and saved at shutdown, to preserve
2539+the changes made to the system during the session.
2540+When larger changes have been made, or smaller changes have accumulated,
2541+the size of the saved top layer data grows. At this point, it would be
2542+nice to be able to merge the two overlay branches ('mods' and 'overlay')
2543+and rewrite the 'mods' squashfs, clearing the top layer and thus
2544+restoring save and load speed.
2545+
2546+This merging is simplified by the use of another aufs mount, of just the
2547+two overlay branches using the 'shwh' option.
2548+# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2549+ aufs /livesys/merge_union
2550+
2551+A merged view of these two branches is then available at
2552+/livesys/merge_union, and the new feature is that the whiteouts are
2553+visible!
2554+Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2555+writing to all branches. Also the default mode for all branches is 'ro'.
2556+It is now possible to save the combined contents of the two overlay
2557+branches to a new squashfs, e.g.:
2558+# mksquashfs /livesys/merge_union /path/to/newmods.squash
2559+
2560+This new squashfs archive can be stored on the boot device and the
2561+initramfs will use it to replace the old one at the next boot.
2562diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
2563--- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt 1970-01-01 01:00:00.000000000 +0100
0b487229 2564+++ linux/Documentation/filesystems/aufs/design/10dynop.txt 2021-02-22 23:30:37.699347580 +0100
7e9cd9fe 2565@@ -0,0 +1,47 @@
53392da6 2566+
0b487229 2567+# Copyright (C) 2010-2020 Junjiro R. Okajima
53392da6
AM
2568+#
2569+# This program is free software; you can redistribute it and/or modify
2570+# it under the terms of the GNU General Public License as published by
2571+# the Free Software Foundation; either version 2 of the License, or
2572+# (at your option) any later version.
2573+#
2574+# This program is distributed in the hope that it will be useful,
2575+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2576+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2577+# GNU General Public License for more details.
2578+#
2579+# You should have received a copy of the GNU General Public License
523b37e3 2580+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2581+
2582+Dynamically customizable FS operations
2583+----------------------------------------------------------------------
2584+Generally FS operations (struct inode_operations, struct
2585+address_space_operations, struct file_operations, etc.) are defined as
2586+"static const", but it never means that FS have only one set of
2587+operation. Some FS have multiple sets of them. For instance, ext2 has
2588+three sets, one for XIP, for NOBH, and for normal.
2589+Since aufs overrides and redirects these operations, sometimes aufs has
7e9cd9fe 2590+to change its behaviour according to the branch FS type. More importantly
53392da6
AM
2591+VFS acts differently if a function (member in the struct) is set or
2592+not. It means aufs should have several sets of operations and select one
2593+among them according to the branch FS definition.
2594+
7e9cd9fe 2595+In order to solve this problem and not to affect the behaviour of VFS,
53392da6 2596+aufs defines these operations dynamically. For instance, aufs defines
7e9cd9fe
AM
2597+dummy direct_IO function for struct address_space_operations, but it may
2598+not be set to the address_space_operations actually. When the branch FS
2599+doesn't have it, aufs doesn't set it to its address_space_operations
2600+while the function definition itself is still alive. So the behaviour
2601+itself will not change, and it will return an error when direct_IO is
2602+not set.
53392da6
AM
2603+
2604+The lifetime of these dynamically generated operation object is
2605+maintained by aufs branch object. When the branch is removed from aufs,
2606+the reference counter of the object is decremented. When it reaches
2607+zero, the dynamically generated operation object will be freed.
2608+
7e9cd9fe
AM
2609+This approach is designed to support AIO (io_submit), Direct I/O and
2610+XIP (DAX) mainly.
2611+Currently this approach is applied to address_space_operations for
2612+regular files only.
53392da6
AM
2613diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
2614--- /usr/share/empty/Documentation/filesystems/aufs/README 1970-01-01 01:00:00.000000000 +0100
0b487229
JR
2615+++ linux/Documentation/filesystems/aufs/README 2021-02-22 23:30:37.699347580 +0100
2616@@ -0,0 +1,404 @@
53392da6 2617+
5527c038 2618+Aufs4 -- advanced multi layered unification filesystem version 4.x
53392da6
AM
2619+http://aufs.sf.net
2620+Junjiro R. Okajima
2621+
2622+
2623+0. Introduction
2624+----------------------------------------
2625+In the early days, aufs was entirely re-designed and re-implemented
7e9cd9fe 2626+Unionfs Version 1.x series. Adding many original ideas, approaches,
53392da6
AM
2627+improvements and implementations, it becomes totally different from
2628+Unionfs while keeping the basic features.
2629+Recently, Unionfs Version 2.x series begin taking some of the same
2630+approaches to aufs1's.
2631+Unionfs is being developed by Professor Erez Zadok at Stony Brook
2632+University and his team.
2633+
5527c038 2634+Aufs4 supports linux-4.0 and later, and for linux-3.x series try aufs3.
53392da6
AM
2635+If you want older kernel version support, try aufs2-2.6.git or
2636+aufs2-standalone.git repository, aufs1 from CVS on SourceForge.
2637+
2638+Note: it becomes clear that "Aufs was rejected. Let's give it up."
38d290e6
JR
2639+ According to Christoph Hellwig, linux rejects all union-type
2640+ filesystems but UnionMount.
53392da6
AM
2641+<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
2642+
38d290e6
JR
2643+PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
2644+ UnionMount, and he pointed out an issue around a directory mutex
2645+ lock and aufs addressed it. But it is still unsure whether aufs will
2646+ be merged (or any other union solution).
076b876e 2647+<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
38d290e6 2648+
53392da6
AM
2649+
2650+1. Features
2651+----------------------------------------
2652+- unite several directories into a single virtual filesystem. The member
2653+ directory is called as a branch.
2654+- you can specify the permission flags to the branch, which are 'readonly',
2655+ 'readwrite' and 'whiteout-able.'
2656+- by upper writable branch, internal copyup and whiteout, files/dirs on
2657+ readonly branch are modifiable logically.
2658+- dynamic branch manipulation, add, del.
2659+- etc...
2660+
7e9cd9fe
AM
2661+Also there are many enhancements in aufs, such as:
2662+- test only the highest one for the directory permission (dirperm1)
2663+- copyup on open (coo=)
2664+- 'move' policy for copy-up between two writable branches, after
2665+ checking free space.
2666+- xattr, acl
53392da6
AM
2667+- readdir(3) in userspace.
2668+- keep inode number by external inode number table
2669+- keep the timestamps of file/dir in internal copyup operation
2670+- seekable directory, supporting NFS readdir.
2671+- whiteout is hardlinked in order to reduce the consumption of inodes
2672+ on branch
2673+- do not copyup, nor create a whiteout when it is unnecessary
2674+- revert a single systemcall when an error occurs in aufs
2675+- remount interface instead of ioctl
2676+- maintain /etc/mtab by an external command, /sbin/mount.aufs.
2677+- loopback mounted filesystem as a branch
2678+- kernel thread for removing the dir who has a plenty of whiteouts
2679+- support copyup sparse file (a file which has a 'hole' in it)
2680+- default permission flags for branches
2681+- selectable permission flags for ro branch, whether whiteout can
2682+ exist or not
2683+- export via NFS.
2684+- support <sysfs>/fs/aufs and <debugfs>/aufs.
2685+- support multiple writable branches, some policies to select one
2686+ among multiple writable branches.
2687+- a new semantics for link(2) and rename(2) to support multiple
2688+ writable branches.
2689+- no glibc changes are required.
2690+- pseudo hardlink (hardlink over branches)
2691+- allow a direct access manually to a file on branch, e.g. bypassing aufs.
2692+ including NFS or remote filesystem branch.
2693+- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
2694+- and more...
2695+
5527c038 2696+Currently these features are dropped temporary from aufs4.
53392da6 2697+See design/08plan.txt in detail.
53392da6
AM
2698+- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
2699+ (robr)
2700+- statistics of aufs thread (/sys/fs/aufs/stat)
53392da6
AM
2701+
2702+Features or just an idea in the future (see also design/*.txt),
2703+- reorder the branch index without del/re-add.
2704+- permanent xino files for NFSD
2705+- an option for refreshing the opened files after add/del branches
53392da6
AM
2706+- light version, without branch manipulation. (unnecessary?)
2707+- copyup in userspace
2708+- inotify in userspace
2709+- readv/writev
53392da6
AM
2710+
2711+
2712+2. Download
2713+----------------------------------------
5527c038
JR
2714+There are three GIT trees for aufs4, aufs4-linux.git,
2715+aufs4-standalone.git, and aufs-util.git. Note that there is no "4" in
1e00d052 2716+"aufs-util.git."
5527c038
JR
2717+While the aufs-util is always necessary, you need either of aufs4-linux
2718+or aufs4-standalone.
1e00d052 2719+
5527c038 2720+The aufs4-linux tree includes the whole linux mainline GIT tree,
1e00d052
AM
2721+git://git.kernel.org/.../torvalds/linux.git.
2722+And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
5527c038 2723+build aufs4 as an external kernel module.
2000de60 2724+Several extra patches are not included in this tree. Only
be52b249 2725+aufs4-standalone tree contains them. They are described in the later
2000de60 2726+section "Configuration and Compilation."
1e00d052 2727+
5527c038 2728+On the other hand, the aufs4-standalone tree has only aufs source files
53392da6 2729+and necessary patches, and you can select CONFIG_AUFS_FS=m.
2000de60 2730+But you need to apply all aufs patches manually.
53392da6 2731+
5527c038
JR
2732+You will find GIT branches whose name is in form of "aufs4.x" where "x"
2733+represents the linux kernel version, "linux-4.x". For instance,
2734+"aufs4.0" is for linux-4.0. For latest "linux-4.x-rcN", use
2735+"aufs4.x-rcN" branch.
1e00d052 2736+
5527c038 2737+o aufs4-linux tree
1e00d052 2738+$ git clone --reference /your/linux/git/tree \
5527c038 2739+ git://github.com/sfjro/aufs4-linux.git aufs4-linux.git
1e00d052 2740+- if you don't have linux GIT tree, then remove "--reference ..."
5527c038
JR
2741+$ cd aufs4-linux.git
2742+$ git checkout origin/aufs4.0
53392da6 2743+
2000de60
JR
2744+Or You may want to directly git-pull aufs into your linux GIT tree, and
2745+leave the patch-work to GIT.
2746+$ cd /your/linux/git/tree
5527c038
JR
2747+$ git remote add aufs4 git://github.com/sfjro/aufs4-linux.git
2748+$ git fetch aufs4
2749+$ git checkout -b my4.0 v4.0
2750+$ (add your local change...)
2751+$ git pull aufs4 aufs4.0
2752+- now you have v4.0 + your_changes + aufs4.0 in you my4.0 branch.
2000de60 2753+- you may need to solve some conflicts between your_changes and
5527c038
JR
2754+ aufs4.0. in this case, git-rerere is recommended so that you can
2755+ solve the similar conflicts automatically when you upgrade to 4.1 or
2000de60
JR
2756+ later in the future.
2757+
5527c038
JR
2758+o aufs4-standalone tree
2759+$ git clone git://github.com/sfjro/aufs4-standalone.git aufs4-standalone.git
2760+$ cd aufs4-standalone.git
2761+$ git checkout origin/aufs4.0
53392da6
AM
2762+
2763+o aufs-util tree
5527c038
JR
2764+$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
2765+- note that the public aufs-util.git is on SourceForge instead of
2766+ GitHUB.
53392da6 2767+$ cd aufs-util.git
5527c038 2768+$ git checkout origin/aufs4.0
53392da6 2769+
5527c038
JR
2770+Note: The 4.x-rcN branch is to be used with `rc' kernel versions ONLY.
2771+The minor version number, 'x' in '4.x', of aufs may not always
9dbd164d
AM
2772+follow the minor version number of the kernel.
2773+Because changes in the kernel that cause the use of a new
2774+minor version number do not always require changes to aufs-util.
2775+
2776+Since aufs-util has its own minor version number, you may not be
2777+able to find a GIT branch in aufs-util for your kernel's
2778+exact minor version number.
2779+In this case, you should git-checkout the branch for the
53392da6 2780+nearest lower number.
9dbd164d
AM
2781+
2782+For (an unreleased) example:
5527c038
JR
2783+If you are using "linux-4.10" and the "aufs4.10" branch
2784+does not exist in aufs-util repository, then "aufs4.9", "aufs4.8"
9dbd164d
AM
2785+or something numerically smaller is the branch for your kernel.
2786+
53392da6
AM
2787+Also you can view all branches by
2788+ $ git branch -a
2789+
2790+
2791+3. Configuration and Compilation
2792+----------------------------------------
2793+Make sure you have git-checkout'ed the correct branch.
2794+
5527c038 2795+For aufs4-linux tree,
c06a8ce3 2796+- enable CONFIG_AUFS_FS.
1e00d052
AM
2797+- set other aufs configurations if necessary.
2798+
5527c038 2799+For aufs4-standalone tree,
53392da6
AM
2800+There are several ways to build.
2801+
2802+1.
5527c038
JR
2803+- apply ./aufs4-kbuild.patch to your kernel source files.
2804+- apply ./aufs4-base.patch too.
2805+- apply ./aufs4-mmap.patch too.
2806+- apply ./aufs4-standalone.patch too, if you have a plan to set
2807+ CONFIG_AUFS_FS=m. otherwise you don't need ./aufs4-standalone.patch.
537831f9
AM
2808+- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2809+ kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
c06a8ce3 2810+- enable CONFIG_AUFS_FS, you can select either
53392da6
AM
2811+ =m or =y.
2812+- and build your kernel as usual.
2813+- install the built kernel.
c06a8ce3
AM
2814+ Note: Since linux-3.9, every filesystem module requires an alias
2815+ "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
2816+ modules.aliases file if you set CONFIG_AUFS_FS=m.
7eafdf33
AM
2817+- install the header files too by "make headers_install" to the
2818+ directory where you specify. By default, it is $PWD/usr.
b4510431 2819+ "make help" shows a brief note for headers_install.
53392da6
AM
2820+- and reboot your system.
2821+
2822+2.
2823+- module only (CONFIG_AUFS_FS=m).
5527c038
JR
2824+- apply ./aufs4-base.patch to your kernel source files.
2825+- apply ./aufs4-mmap.patch too.
2826+- apply ./aufs4-standalone.patch too.
53392da6
AM
2827+- build your kernel, don't forget "make headers_install", and reboot.
2828+- edit ./config.mk and set other aufs configurations if necessary.
b4510431 2829+ Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
53392da6
AM
2830+ every aufs configurations.
2831+- build the module by simple "make".
c06a8ce3
AM
2832+ Note: Since linux-3.9, every filesystem module requires an alias
2833+ "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
2834+ modules.aliases file.
53392da6
AM
2835+- you can specify ${KDIR} make variable which points to your kernel
2836+ source tree.
2837+- install the files
2838+ + run "make install" to install the aufs module, or copy the built
b4510431
AM
2839+ $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2840+ + run "make install_headers" (instead of headers_install) to install
2841+ the modified aufs header file (you can specify DESTDIR which is
2842+ available in aufs standalone version's Makefile only), or copy
2843+ $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2844+ you like manually. By default, the target directory is $PWD/usr.
5527c038 2845+- no need to apply aufs4-kbuild.patch, nor copying source files to your
53392da6
AM
2846+ kernel source tree.
2847+
b4510431 2848+Note: The header file aufs_type.h is necessary to build aufs-util
53392da6
AM
2849+ as well as "make headers_install" in the kernel source tree.
2850+ headers_install is subject to be forgotten, but it is essentially
2851+ necessary, not only for building aufs-util.
2852+ You may not meet problems without headers_install in some older
2853+ version though.
2854+
2855+And then,
2856+- read README in aufs-util, build and install it
9dbd164d
AM
2857+- note that your distribution may contain an obsoleted version of
2858+ aufs_type.h in /usr/include/linux or something. When you build aufs
2859+ utilities, make sure that your compiler refers the correct aufs header
2860+ file which is built by "make headers_install."
53392da6
AM
2861+- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2862+ then run "make install_ulib" too. And refer to the aufs manual in
2863+ detail.
2864+
5527c038 2865+There several other patches in aufs4-standalone.git. They are all
38d290e6 2866+optional. When you meet some problems, they will help you.
5527c038 2867+- aufs4-loopback.patch
38d290e6
JR
2868+ Supports a nested loopback mount in a branch-fs. This patch is
2869+ unnecessary until aufs produces a message like "you may want to try
2870+ another patch for loopback file".
0b487229
JR
2871+- proc_mounts.patch
2872+ When there are many mountpoints and many mount(2)/umount(2) are
2873+ running, then /proc/mounts may not show the all mountpoints. This
2874+ patch makes /proc/mounts always show the full mountpoints list.
2875+ If you don't want to apply this patch and meet such problem, then you
2876+ need to increase the value of 'ProcMounts_Times' make-variable in
2877+ aufs-util.git as a second best solution.
38d290e6
JR
2878+- vfs-ino.patch
2879+ Modifies a system global kernel internal function get_next_ino() in
2880+ order to stop assigning 0 for an inode-number. Not directly related to
2881+ aufs, but recommended generally.
2882+- tmpfs-idr.patch
2883+ Keeps the tmpfs inode number as the lowest value. Effective to reduce
2884+ the size of aufs XINO files for tmpfs branch. Also it prevents the
2885+ duplication of inode number, which is important for backup tools and
2886+ other utilities. When you find aufs XINO files for tmpfs branch
2887+ growing too much, try this patch.
be52b249
AM
2888+- lockdep-debug.patch
2889+ Because aufs is not only an ordinary filesystem (callee of VFS), but
2890+ also a caller of VFS functions for branch filesystems, subclassing of
2891+ the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
2892+ feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
2893+ need to apply this debug patch to expand several constant values.
0b487229
JR
2894+ If you don't know what LOCKDEP is, then you don't have apply this
2895+ patch.
38d290e6 2896+
53392da6
AM
2897+
2898+4. Usage
2899+----------------------------------------
2900+At first, make sure aufs-util are installed, and please read the aufs
2901+manual, aufs.5 in aufs-util.git tree.
2902+$ man -l aufs.5
2903+
2904+And then,
2905+$ mkdir /tmp/rw /tmp/aufs
2906+# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2907+
2908+Here is another example. The result is equivalent.
2909+# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2910+ Or
2911+# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2912+# mount -o remount,append:${HOME} /tmp/aufs
2913+
2914+Then, you can see whole tree of your home dir through /tmp/aufs. If
2915+you modify a file under /tmp/aufs, the one on your home directory is
2916+not affected, instead the same named file will be newly created under
2917+/tmp/rw. And all of your modification to a file will be applied to
2918+the one under /tmp/rw. This is called the file based Copy on Write
2919+(COW) method.
2920+Aufs mount options are described in aufs.5.
2921+If you run chroot or something and make your aufs as a root directory,
2922+then you need to customize the shutdown script. See the aufs manual in
2923+detail.
2924+
2925+Additionally, there are some sample usages of aufs which are a
2926+diskless system with network booting, and LiveCD over NFS.
2927+See sample dir in CVS tree on SourceForge.
2928+
2929+
2930+5. Contact
2931+----------------------------------------
2932+When you have any problems or strange behaviour in aufs, please let me
2933+know with:
2934+- /proc/mounts (instead of the output of mount(8))
2935+- /sys/module/aufs/*
2936+- /sys/fs/aufs/* (if you have them)
2937+- /debug/aufs/* (if you have them)
2938+- linux kernel version
2939+ if your kernel is not plain, for example modified by distributor,
2940+ the url where i can download its source is necessary too.
2941+- aufs version which was printed at loading the module or booting the
2942+ system, instead of the date you downloaded.
2943+- configuration (define/undefine CONFIG_AUFS_xxx)
2944+- kernel configuration or /proc/config.gz (if you have it)
0b487229 2945+- LSM (linux security module, if you are using)
53392da6
AM
2946+- behaviour which you think to be incorrect
2947+- actual operation, reproducible one is better
2948+- mailto: aufs-users at lists.sourceforge.net
2949+
2950+Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
2951+and Feature Requests) on SourceForge. Please join and write to
2952+aufs-users ML.
2953+
2954+
2955+6. Acknowledgements
2956+----------------------------------------
2957+Thanks to everyone who have tried and are using aufs, whoever
2958+have reported a bug or any feedback.
2959+
2960+Especially donators:
2961+Tomas Matejicek(slax.org) made a donation (much more than once).
2962+ Since Apr 2010, Tomas M (the author of Slax and Linux Live
2963+ scripts) is making "doubling" donations.
2964+ Unfortunately I cannot list all of the donators, but I really
b4510431 2965+ appreciate.
53392da6
AM
2966+ It ends Aug 2010, but the ordinary donation URL is still available.
2967+ <http://sourceforge.net/donate/index.php?group_id=167503>
2968+Dai Itasaka made a donation (2007/8).
2969+Chuck Smith made a donation (2008/4, 10 and 12).
2970+Henk Schoneveld made a donation (2008/9).
2971+Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
2972+Francois Dupoux made a donation (2008/11).
2973+Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
2974+ aufs2 GIT tree (2009/2).
2975+William Grant made a donation (2009/3).
2976+Patrick Lane made a donation (2009/4).
2977+The Mail Archive (mail-archive.com) made donations (2009/5).
2978+Nippy Networks (Ed Wildgoose) made a donation (2009/7).
2979+New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
2980+Pavel Pronskiy made a donation (2011/2).
2981+Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
2982+ Networks (Ed Wildgoose) made a donation for hardware (2011/3).
537831f9
AM
2983+Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
2984+11).
1e00d052 2985+Sam Liddicott made a donation (2011/9).
86dc4139
AM
2986+Era Scarecrow made a donation (2013/4).
2987+Bor Ratajc made a donation (2013/4).
2988+Alessandro Gorreta made a donation (2013/4).
2989+POIRETTE Marc made a donation (2013/4).
2990+Alessandro Gorreta made a donation (2013/4).
2991+lauri kasvandik made a donation (2013/5).
392086de 2992+"pemasu from Finland" made a donation (2013/7).
523b37e3
AM
2993+The Parted Magic Project made a donation (2013/9 and 11).
2994+Pavel Barta made a donation (2013/10).
38d290e6 2995+Nikolay Pertsev made a donation (2014/5).
c2c0f25c 2996+James B made a donation (2014/7 and 2015/7).
076b876e 2997+Stefano Di Biase made a donation (2014/8).
2000de60 2998+Daniel Epellei made a donation (2015/1).
c4adf169 2999+OmegaPhil made a donation (2016/1, 2018/4).
5afbbe0d 3000+Tomasz Szewczyk made a donation (2016/4).
f2c43d5f 3001+James Burry made a donation (2016/12).
acd2b654 3002+Carsten Rose made a donation (2018/9).
e49925d1 3003+Porteus Kiosk made a donation (2018/10).
53392da6
AM
3004+
3005+Thank you very much.
3006+Donations are always, including future donations, very important and
3007+helpful for me to keep on developing aufs.
3008+
3009+
3010+7.
3011+----------------------------------------
3012+If you are an experienced user, no explanation is needed. Aufs is
3013+just a linux filesystem.
3014+
3015+
3016+Enjoy!
3017+
3018+# Local variables: ;
3019+# mode: text;
3020+# End: ;
7f207e10
AM
3021diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
3022--- /usr/share/empty/fs/aufs/aufs.h 1970-01-01 01:00:00.000000000 +0100
0b487229 3023+++ linux/fs/aufs/aufs.h 2021-02-22 23:30:37.699347580 +0100
acd2b654 3024@@ -0,0 +1,62 @@
062440b3 3025+/* SPDX-License-Identifier: GPL-2.0 */
7f207e10 3026+/*
0b487229 3027+ * Copyright (C) 2005-2020 Junjiro R. Okajima
7f207e10
AM
3028+ *
3029+ * This program, aufs is free software; you can redistribute it and/or modify
3030+ * it under the terms of the GNU General Public License as published by
3031+ * the Free Software Foundation; either version 2 of the License, or
3032+ * (at your option) any later version.
3033+ *
3034+ * This program is distributed in the hope that it will be useful,
3035+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
3036+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3037+ * GNU General Public License for more details.
3038+ *
3039+ * You should have received a copy of the GNU General Public License
523b37e3 3040+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7f207e10
AM
3041+ */
3042+
3043+/*
3044+ * all header files
3045+ */
3046+
3047+#ifndef __AUFS_H__
3048+#define __AUFS_H__
3049+
3050+#ifdef __KERNEL__
3051+
3052+#define AuStub(type, name, body, ...) \
3053+ static inline type name(__VA_ARGS__) { body; }
3054+
3055+#define AuStubVoid(name, ...) \
3056+ AuStub(void, name, , __VA_ARGS__)
3057+#define AuStubInt0(name, ...) \
3058+ AuStub(int, name, return 0, __VA_ARGS__)
3059+
3060+#include "debug.h"
3061+
3062+#include "branch.h"
3063+#include "cpup.h"
3064+#include "dcsub.h"
3065+#include "dbgaufs.h"
3066+#include "dentry.h"
3067+#include "dir.h"
8b6a4947 3068+#include "dirren.h"
7f207e10
AM
3069+#include "dynop.h"
3070+#include "file.h"
3071+#include "fstype.h"
8b6a4947 3072+#include "hbl.h"
7f207e10 3073+#include "inode.h"
acd2b654 3074+#include "lcnt.h"
7f207e10
AM
3075+#include "loop.h"
3076+#include "module.h"
7f207e10
AM
3077+#include "opts.h"
3078+#include "rwsem.h"
7f207e10
AM
3079+#include "super.h"
3080+#include "sysaufs.h"
3081+#include "vfsub.h"
3082+#include "whout.h"
3083+#include "wkq.h"
3084+
3085+#endif /* __KERNEL__ */
3086+#endif /* __AUFS_H__ */
3087diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
3088--- /usr/share/empty/fs/aufs/branch.c 1970-01-01 01:00:00.000000000 +0100
0b487229
JR
3089+++ linux/fs/aufs/branch.c 2021-02-22 23:30:37.699347580 +0100
3090@@ -0,0 +1,1426 @@
cd7a4cd9 3091+// SPDX-License-Identifier: GPL-2.0
7f207e10 3092+/*
0b487229 3093+ * Copyright (C) 2005-2020 Junjiro R. Okajima
7f207e10
AM
3094+ *
3095+ * This program, aufs is free software; you can redistribute it and/or modify
3096+ * it under the terms of the GNU General Public License as published by
3097+ * the Free Software Foundation; either version 2 of the License, or
3098+ * (at your option) any later version.
3099+ *
3100+ * This program is distributed in the hope that it will be useful,
3101+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
3102+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3103+ * GNU General Public License for more details.
3104+ *
3105+ * You should have received a copy of the GNU General Public License
523b37e3 3106+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7f207e10
AM
3107+ */
3108+
3109+/*
3110+ * branch management
3111+ */
3112+
027c5e7a 3113+#include <linux/compat.h>
7f207e10
AM
3114+#include <linux/statfs.h>
3115+#include "aufs.h"
3116+
3117+/*
3118+ * free a single branch
1facf9fc 3119+ */
3120+static void au_br_do_free(struct au_branch *br)
3121+{
3122+ int i;
3123+ struct au_wbr *wbr;
4a4d8108 3124+ struct au_dykey **key;
1facf9fc 3125+
027c5e7a 3126+ au_hnotify_fin_br(br);
8b6a4947
AM
3127+ /* always, regardless the mount option */
3128+ au_dr_hino_free(&br->br_dirren);
062440b3 3129+ au_xino_put(br);
1facf9fc 3130+
acd2b654
AM
3131+ AuLCntZero(au_lcnt_read(&br->br_nfiles, /*do_rev*/0));
3132+ au_lcnt_fin(&br->br_nfiles, /*do_sync*/0);
3133+ AuLCntZero(au_lcnt_read(&br->br_count, /*do_rev*/0));
3134+ au_lcnt_fin(&br->br_count, /*do_sync*/0);
1facf9fc 3135+
3136+ wbr = br->br_wbr;
3137+ if (wbr) {
3138+ for (i = 0; i < AuBrWh_Last; i++)
3139+ dput(wbr->wbr_wh[i]);
3140+ AuDebugOn(atomic_read(&wbr->wbr_wh_running));
dece6358 3141+ AuRwDestroy(&wbr->wbr_wh_rwsem);
1facf9fc 3142+ }
3143+
076b876e
AM
3144+ if (br->br_fhsm) {
3145+ au_br_fhsm_fin(br->br_fhsm);
e49925d1 3146+ au_kfree_try_rcu(br->br_fhsm);
076b876e
AM
3147+ }
3148+
4a4d8108
AM
3149+ key = br->br_dykey;
3150+ for (i = 0; i < AuBrDynOp; i++, key++)
3151+ if (*key)
3152+ au_dy_put(*key);
3153+ else
3154+ break;
3155+
537831f9 3156+ /* recursive lock, s_umount of branch's */
acd2b654 3157+ /* synchronize_rcu(); */ /* why? */
537831f9 3158+ lockdep_off();
86dc4139 3159+ path_put(&br->br_path);
537831f9 3160+ lockdep_on();
e49925d1 3161+ au_kfree_rcu(wbr);
acd2b654
AM
3162+ au_lcnt_wait_for_fin(&br->br_nfiles);
3163+ au_lcnt_wait_for_fin(&br->br_count);
3164+ /* I don't know why, but percpu_refcount requires this */
3165+ /* synchronize_rcu(); */
e49925d1 3166+ au_kfree_rcu(br);
1facf9fc 3167+}
3168+
3169+/*
3170+ * frees all branches
3171+ */
3172+void au_br_free(struct au_sbinfo *sbinfo)
3173+{
3174+ aufs_bindex_t bmax;
3175+ struct au_branch **br;
3176+
dece6358
AM
3177+ AuRwMustWriteLock(&sbinfo->si_rwsem);
3178+
5afbbe0d 3179+ bmax = sbinfo->si_bbot + 1;
1facf9fc 3180+ br = sbinfo->si_branch;
3181+ while (bmax--)
3182+ au_br_do_free(*br++);
3183+}
3184+
3185+/*
3186+ * find the index of a branch which is specified by @br_id.
3187+ */
3188+int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
3189+{
5afbbe0d 3190+ aufs_bindex_t bindex, bbot;
1facf9fc 3191+
5afbbe0d
AM
3192+ bbot = au_sbbot(sb);
3193+ for (bindex = 0; bindex <= bbot; bindex++)
1facf9fc 3194+ if (au_sbr_id(sb, bindex) == br_id)
3195+ return bindex;
3196+ return -1;
3197+}
3198+
3199+/* ---------------------------------------------------------------------- */
3200+
3201+/*
3202+ * add a branch
3203+ */
3204+
b752ccd1
AM
3205+static int test_overlap(struct super_block *sb, struct dentry *h_adding,
3206+ struct dentry *h_root)
1facf9fc 3207+{
b752ccd1
AM
3208+ if (unlikely(h_adding == h_root
3209+ || au_test_loopback_overlap(sb, h_adding)))
1facf9fc 3210+ return 1;
b752ccd1
AM
3211+ if (h_adding->d_sb != h_root->d_sb)
3212+ return 0;
3213+ return au_test_subdir(h_adding, h_root)
3214+ || au_test_subdir(h_root, h_adding);
1facf9fc 3215+}
3216+
3217+/*
3218+ * returns a newly allocated branch. @new_nbranch is a number of branches
3219+ * after adding a branch.
3220+ */
3221+static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
3222+ int perm)
3223+{
3224+ struct au_branch *add_branch;
3225+ struct dentry *root;
5527c038 3226+ struct inode *inode;
4a4d8108 3227+ int err;
1facf9fc 3228+
4a4d8108 3229+ err = -ENOMEM;
be52b249 3230+ add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
1facf9fc 3231+ if (unlikely(!add_branch))
3232+ goto out;
acd2b654 3233+ add_branch->br_xino = au_xino_alloc(/*nfile*/1);
062440b3 3234+ if (unlikely(!add_branch->br_xino))
521ced18 3235+ goto out_br;
027c5e7a
AM
3236+ err = au_hnotify_init_br(add_branch, perm);
3237+ if (unlikely(err))
062440b3 3238+ goto out_xino;
027c5e7a 3239+
1facf9fc 3240+ if (au_br_writable(perm)) {
3241+ /* may be freed separately at changing the branch permission */
be52b249 3242+ add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
1facf9fc 3243+ GFP_NOFS);
3244+ if (unlikely(!add_branch->br_wbr))
027c5e7a 3245+ goto out_hnotify;
1facf9fc 3246+ }
3247+
076b876e
AM
3248+ if (au_br_fhsm(perm)) {
3249+ err = au_fhsm_br_alloc(add_branch);
3250+ if (unlikely(err))
3251+ goto out_wbr;
3252+ }
3253+
521ced18 3254+ root = sb->s_root;
e2f27e51 3255+ err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
4a4d8108 3256+ if (!err)
e2f27e51 3257+ err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
5527c038
JR
3258+ if (!err) {
3259+ inode = d_inode(root);
1c60b727
AM
3260+ err = au_hinode_realloc(au_ii(inode), new_nbranch,
3261+ /*may_shrink*/0);
5527c038 3262+ }
4a4d8108
AM
3263+ if (!err)
3264+ return add_branch; /* success */
1facf9fc 3265+
076b876e 3266+out_wbr:
e49925d1 3267+ au_kfree_rcu(add_branch->br_wbr);
027c5e7a
AM
3268+out_hnotify:
3269+ au_hnotify_fin_br(add_branch);
062440b3
AM
3270+out_xino:
3271+ au_xino_put(add_branch);
4f0767ce 3272+out_br:
e49925d1 3273+ au_kfree_rcu(add_branch);
4f0767ce 3274+out:
4a4d8108 3275+ return ERR_PTR(err);
1facf9fc 3276+}
3277+
3278+/*
3279+ * test if the branch permission is legal or not.
3280+ */
3281+static int test_br(struct inode *inode, int brperm, char *path)
3282+{
3283+ int err;
3284+
4a4d8108
AM
3285+ err = (au_br_writable(brperm) && IS_RDONLY(inode));
3286+ if (!err)
3287+ goto out;
1facf9fc 3288+
4a4d8108
AM
3289+ err = -EINVAL;
3290+ pr_err("write permission for readonly mount or inode, %s\n", path);
3291+
4f0767ce 3292+out:
1facf9fc 3293+ return err;
3294+}
3295+
3296+/*
3297+ * returns:
3298+ * 0: success, the caller will add it
3299+ * plus: success, it is already unified, the caller should ignore it
3300+ * minus: error
3301+ */
3302+static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3303+{
3304+ int err;
5afbbe0d 3305+ aufs_bindex_t bbot, bindex;
5527c038 3306+ struct dentry *root, *h_dentry;
1facf9fc 3307+ struct inode *inode, *h_inode;
3308+
3309+ root = sb->s_root;
5afbbe0d
AM
3310+ bbot = au_sbbot(sb);
3311+ if (unlikely(bbot >= 0
1facf9fc 3312+ && au_find_dbindex(root, add->path.dentry) >= 0)) {
3313+ err = 1;
3314+ if (!remount) {
3315+ err = -EINVAL;
4a4d8108 3316+ pr_err("%s duplicated\n", add->pathname);
1facf9fc 3317+ }
3318+ goto out;
3319+ }
3320+
3321+ err = -ENOSPC; /* -E2BIG; */
3322+ if (unlikely(AUFS_BRANCH_MAX <= add->bindex
5afbbe0d 3323+ || AUFS_BRANCH_MAX - 1 <= bbot)) {
4a4d8108 3324+ pr_err("number of branches exceeded %s\n", add->pathname);
1facf9fc 3325+ goto out;
3326+ }
3327+
3328+ err = -EDOM;
5afbbe0d 3329+ if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
4a4d8108 3330+ pr_err("bad index %d\n", add->bindex);
1facf9fc 3331+ goto out;
3332+ }
3333+
5527c038 3334+ inode = d_inode(add->path.dentry);
1facf9fc 3335+ err = -ENOENT;
3336+ if (unlikely(!inode->i_nlink)) {
4a4d8108 3337+ pr_err("no existence %s\n", add->pathname);
1facf9fc 3338+ goto out;
3339+ }
3340+
3341+ err = -EINVAL;
3342+ if (unlikely(inode->i_sb == sb)) {
4a4d8108 3343+ pr_err("%s must be outside\n", add->pathname);
1facf9fc 3344+ goto out;
3345+ }
3346+
3347+ if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
4a4d8108
AM
3348+ pr_err("unsupported filesystem, %s (%s)\n",
3349+ add->pathname, au_sbtype(inode->i_sb));
1facf9fc 3350+ goto out;
3351+ }
3352+
c1595e42
JR
3353+ if (unlikely(inode->i_sb->s_stack_depth)) {
3354+ pr_err("already stacked, %s (%s)\n",
3355+ add->pathname, au_sbtype(inode->i_sb));
3356+ goto out;
3357+ }
3358+
5527c038 3359+ err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
1facf9fc 3360+ if (unlikely(err))
3361+ goto out;
3362+
5afbbe0d 3363+ if (bbot < 0)
1facf9fc 3364+ return 0; /* success */
3365+
3366+ err = -EINVAL;
5afbbe0d 3367+ for (bindex = 0; bindex <= bbot; bindex++)
1facf9fc 3368+ if (unlikely(test_overlap(sb, add->path.dentry,
3369+ au_h_dptr(root, bindex)))) {
4a4d8108 3370+ pr_err("%s is overlapped\n", add->pathname);
1facf9fc 3371+ goto out;
3372+ }
3373+
3374+ err = 0;
3375+ if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
5527c038
JR
3376+ h_dentry = au_h_dptr(root, 0);
3377+ h_inode = d_inode(h_dentry);
1facf9fc 3378+ if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
0c3ec466
AM
3379+ || !uid_eq(h_inode->i_uid, inode->i_uid)
3380+ || !gid_eq(h_inode->i_gid, inode->i_gid))
3381+ pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3382+ add->pathname,
3383+ i_uid_read(inode), i_gid_read(inode),
3384+ (inode->i_mode & S_IALLUGO),
3385+ i_uid_read(h_inode), i_gid_read(h_inode),
3386+ (h_inode->i_mode & S_IALLUGO));
1facf9fc 3387+ }
3388+
4f0767ce 3389+out:
1facf9fc 3390+ return err;
3391+}
3392+
3393+/*
3394+ * initialize or clean the whiteouts for an adding branch
3395+ */
3396+static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
86dc4139 3397+ int new_perm)
1facf9fc 3398+{
3399+ int err, old_perm;
3400+ aufs_bindex_t bindex;
febd17d6 3401+ struct inode *h_inode;
1facf9fc 3402+ struct au_wbr *wbr;
3403+ struct au_hinode *hdir;
5527c038 3404+ struct dentry *h_dentry;
1facf9fc 3405+
86dc4139
AM
3406+ err = vfsub_mnt_want_write(au_br_mnt(br));
3407+ if (unlikely(err))
3408+ goto out;
3409+
1facf9fc 3410+ wbr = br->br_wbr;
3411+ old_perm = br->br_perm;
3412+ br->br_perm = new_perm;
3413+ hdir = NULL;
febd17d6 3414+ h_inode = NULL;
1facf9fc 3415+ bindex = au_br_index(sb, br->br_id);
3416+ if (0 <= bindex) {
5527c038 3417+ hdir = au_hi(d_inode(sb->s_root), bindex);
5afbbe0d 3418+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 3419+ } else {
5527c038 3420+ h_dentry = au_br_dentry(br);
febd17d6
JR
3421+ h_inode = d_inode(h_dentry);
3422+ inode_lock_nested(h_inode, AuLsc_I_PARENT);
1facf9fc 3423+ }
3424+ if (!wbr)
86dc4139 3425+ err = au_wh_init(br, sb);
1facf9fc 3426+ else {
3427+ wbr_wh_write_lock(wbr);
86dc4139 3428+ err = au_wh_init(br, sb);
1facf9fc 3429+ wbr_wh_write_unlock(wbr);
3430+ }
3431+ if (hdir)
5afbbe0d 3432+ au_hn_inode_unlock(hdir);
1facf9fc 3433+ else
febd17d6 3434+ inode_unlock(h_inode);
86dc4139 3435+ vfsub_mnt_drop_write(au_br_mnt(br));
1facf9fc 3436+ br->br_perm = old_perm;
3437+
3438+ if (!err && wbr && !au_br_writable(new_perm)) {
e49925d1 3439+ au_kfree_rcu(wbr);
1facf9fc 3440+ br->br_wbr = NULL;
3441+ }
3442+
86dc4139 3443+out:
1facf9fc 3444+ return err;
3445+}
3446+
3447+static int au_wbr_init(struct au_branch *br, struct super_block *sb,
86dc4139 3448+ int perm)
1facf9fc 3449+{
3450+ int err;
4a4d8108 3451+ struct kstatfs kst;
1facf9fc 3452+ struct au_wbr *wbr;
3453+
3454+ wbr = br->br_wbr;
dece6358 3455+ au_rw_init(&wbr->wbr_wh_rwsem);
1facf9fc 3456+ atomic_set(&wbr->wbr_wh_running, 0);
1facf9fc 3457+
4a4d8108
AM
3458+ /*
3459+ * a limit for rmdir/rename a dir
523b37e3 3460+ * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
4a4d8108 3461+ */
86dc4139 3462+ err = vfs_statfs(&br->br_path, &kst);
4a4d8108
AM
3463+ if (unlikely(err))
3464+ goto out;
3465+ err = -EINVAL;
3466+ if (kst.f_namelen >= NAME_MAX)
86dc4139 3467+ err = au_br_init_wh(sb, br, perm);
4a4d8108 3468+ else
523b37e3
AM
3469+ pr_err("%pd(%s), unsupported namelen %ld\n",
3470+ au_br_dentry(br),
86dc4139 3471+ au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
1facf9fc 3472+
4f0767ce 3473+out:
1facf9fc 3474+ return err;
3475+}
3476+
c1595e42 3477+/* initialize a new branch */
1facf9fc 3478+static int au_br_init(struct au_branch *br, struct super_block *sb,
3479+ struct au_opt_add *add)
3480+{
3481+ int err;
062440b3
AM
3482+ struct au_branch *brbase;
3483+ struct file *xf;
5527c038 3484+ struct inode *h_inode;
1facf9fc 3485+
3486+ err = 0;
1facf9fc 3487+ br->br_perm = add->perm;
86dc4139 3488+ br->br_path = add->path; /* set first, path_get() later */
4a4d8108 3489+ spin_lock_init(&br->br_dykey_lock);
acd2b654
AM
3490+ au_lcnt_init(&br->br_nfiles, /*release*/NULL);
3491+ au_lcnt_init(&br->br_count, /*release*/NULL);
1facf9fc 3492+ br->br_id = au_new_br_id(sb);
7f207e10 3493+ AuDebugOn(br->br_id < 0);
1facf9fc 3494+
8b6a4947
AM
3495+ /* always, regardless the given option */
3496+ err = au_dr_br_init(sb, br, &add->path);
3497+ if (unlikely(err))
3498+ goto out_err;
3499+
1facf9fc 3500+ if (au_br_writable(add->perm)) {
86dc4139 3501+ err = au_wbr_init(br, sb, add->perm);
1facf9fc 3502+ if (unlikely(err))
b752ccd1 3503+ goto out_err;
1facf9fc 3504+ }
3505+
3506+ if (au_opt_test(au_mntflags(sb), XINO)) {
062440b3 3507+ brbase = au_sbr(sb, 0);
acd2b654 3508+ xf = au_xino_file(brbase->br_xino, /*idx*/-1);
062440b3 3509+ AuDebugOn(!xf);
5527c038 3510+ h_inode = d_inode(add->path.dentry);
062440b3 3511+ err = au_xino_init_br(sb, br, h_inode->i_ino, &xf->f_path);
1facf9fc 3512+ if (unlikely(err)) {
acd2b654 3513+ AuDebugOn(au_xino_file(br->br_xino, /*idx*/-1));
b752ccd1 3514+ goto out_err;
1facf9fc 3515+ }
3516+ }
3517+
3518+ sysaufs_br_init(br);
86dc4139 3519+ path_get(&br->br_path);
b752ccd1 3520+ goto out; /* success */
1facf9fc 3521+
4f0767ce 3522+out_err:
86dc4139 3523+ memset(&br->br_path, 0, sizeof(br->br_path));
4f0767ce 3524+out:
1facf9fc 3525+ return err;
3526+}
3527+
3528+static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
5afbbe0d 3529+ struct au_branch *br, aufs_bindex_t bbot,
1facf9fc 3530+ aufs_bindex_t amount)
3531+{
3532+ struct au_branch **brp;
3533+
dece6358
AM
3534+ AuRwMustWriteLock(&sbinfo->si_rwsem);
3535+
1facf9fc 3536+ brp = sbinfo->si_branch + bindex;
3537+ memmove(brp + 1, brp, sizeof(*brp) * amount);
3538+ *brp = br;
5afbbe0d
AM
3539+ sbinfo->si_bbot++;
3540+ if (unlikely(bbot < 0))
3541+ sbinfo->si_bbot = 0;
1facf9fc 3542+}
3543+
3544+static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
5afbbe0d 3545+ aufs_bindex_t bbot, aufs_bindex_t amount)
1facf9fc 3546+{
3547+ struct au_hdentry *hdp;
3548+
1308ab2a 3549+ AuRwMustWriteLock(&dinfo->di_rwsem);
3550+
5afbbe0d 3551+ hdp = au_hdentry(dinfo, bindex);
1facf9fc 3552+ memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3553+ au_h_dentry_init(hdp);
5afbbe0d
AM
3554+ dinfo->di_bbot++;
3555+ if (unlikely(bbot < 0))
3556+ dinfo->di_btop = 0;
1facf9fc 3557+}
3558+
3559+static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
5afbbe0d 3560+ aufs_bindex_t bbot, aufs_bindex_t amount)
1facf9fc 3561+{
3562+ struct au_hinode *hip;
3563+
1308ab2a 3564+ AuRwMustWriteLock(&iinfo->ii_rwsem);
3565+
5afbbe0d 3566+ hip = au_hinode(iinfo, bindex);
1facf9fc 3567+ memmove(hip + 1, hip, sizeof(*hip) * amount);
5afbbe0d
AM
3568+ au_hinode_init(hip);
3569+ iinfo->ii_bbot++;
3570+ if (unlikely(bbot < 0))
3571+ iinfo->ii_btop = 0;
1facf9fc 3572+}
3573+
86dc4139
AM
3574+static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3575+ aufs_bindex_t bindex)
1facf9fc 3576+{
86dc4139 3577+ struct dentry *root, *h_dentry;
5527c038 3578+ struct inode *root_inode, *h_inode;
5afbbe0d 3579+ aufs_bindex_t bbot, amount;
1facf9fc 3580+
3581+ root = sb->s_root;
5527c038 3582+ root_inode = d_inode(root);
5afbbe0d
AM
3583+ bbot = au_sbbot(sb);
3584+ amount = bbot + 1 - bindex;
86dc4139 3585+ h_dentry = au_br_dentry(br);
53392da6 3586+ au_sbilist_lock();
5afbbe0d
AM
3587+ au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
3588+ au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
3589+ au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
1facf9fc 3590+ au_set_h_dptr(root, bindex, dget(h_dentry));
5527c038
JR
3591+ h_inode = d_inode(h_dentry);
3592+ au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
53392da6 3593+ au_sbilist_unlock();
1facf9fc 3594+}
3595+
3596+int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3597+{
3598+ int err;
5afbbe0d 3599+ aufs_bindex_t bbot, add_bindex;
1facf9fc 3600+ struct dentry *root, *h_dentry;
3601+ struct inode *root_inode;
3602+ struct au_branch *add_branch;
3603+
3604+ root = sb->s_root;
5527c038 3605+ root_inode = d_inode(root);
1facf9fc 3606+ IMustLock(root_inode);
5afbbe0d 3607+ IiMustWriteLock(root_inode);
1facf9fc 3608+ err = test_add(sb, add, remount);
3609+ if (unlikely(err < 0))
3610+ goto out;
3611+ if (err) {
3612+ err = 0;
3613+ goto out; /* success */
3614+ }
3615+
5afbbe0d
AM
3616+ bbot = au_sbbot(sb);
3617+ add_branch = au_br_alloc(sb, bbot + 2, add->perm);
1facf9fc 3618+ err = PTR_ERR(add_branch);
3619+ if (IS_ERR(add_branch))
3620+ goto out;
3621+
3622+ err = au_br_init(add_branch, sb, add);
3623+ if (unlikely(err)) {
3624+ au_br_do_free(add_branch);
3625+ goto out;
3626+ }
3627+
3628+ add_bindex = add->bindex;
062440b3
AM
3629+ sysaufs_brs_del(sb, add_bindex); /* remove successors */
3630+ au_br_do_add(sb, add_branch, add_bindex);
3631+ sysaufs_brs_add(sb, add_bindex); /* append successors */
3632+ dbgaufs_brs_add(sb, add_bindex, /*topdown*/0); /* rename successors */
1facf9fc 3633+
86dc4139 3634+ h_dentry = add->path.dentry;
1308ab2a 3635+ if (!add_bindex) {
1facf9fc 3636+ au_cpup_attr_all(root_inode, /*force*/1);
1308ab2a 3637+ sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3638+ } else
5527c038 3639+ au_add_nlink(root_inode, d_inode(h_dentry));
1facf9fc 3640+
4f0767ce 3641+out:
1facf9fc 3642+ return err;
3643+}
3644+
3645+/* ---------------------------------------------------------------------- */
3646+
79b8bda9 3647+static unsigned long long au_farray_cb(struct super_block *sb, void *a,
076b876e
AM
3648+ unsigned long long max __maybe_unused,
3649+ void *arg)
3650+{
3651+ unsigned long long n;
3652+ struct file **p, *f;
8b6a4947
AM
3653+ struct hlist_bl_head *files;
3654+ struct hlist_bl_node *pos;
076b876e 3655+ struct au_finfo *finfo;
076b876e
AM
3656+
3657+ n = 0;
3658+ p = a;
3659+ files = &au_sbi(sb)->si_files;
8b6a4947
AM
3660+ hlist_bl_lock(files);
3661+ hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
076b876e
AM
3662+ f = finfo->fi_file;
3663+ if (file_count(f)
3664+ && !special_file(file_inode(f)->i_mode)) {
3665+ get_file(f);
3666+ *p++ = f;
3667+ n++;
3668+ AuDebugOn(n > max);
3669+ }
3670+ }
8b6a4947 3671+ hlist_bl_unlock(files);
076b876e
AM
3672+
3673+ return n;
3674+}
3675+
3676+static struct file **au_farray_alloc(struct super_block *sb,
3677+ unsigned long long *max)
3678+{
acd2b654
AM
3679+ struct au_sbinfo *sbi;
3680+
3681+ sbi = au_sbi(sb);
3682+ *max = au_lcnt_read(&sbi->si_nfiles, /*do_rev*/1);
79b8bda9 3683+ return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
076b876e
AM
3684+}
3685+
3686+static void au_farray_free(struct file **a, unsigned long long max)
3687+{
3688+ unsigned long long ull;
3689+
3690+ for (ull = 0; ull < max; ull++)
3691+ if (a[ull])
3692+ fput(a[ull]);
be52b249 3693+ kvfree(a);
076b876e
AM
3694+}
3695+
3696+/* ---------------------------------------------------------------------- */
3697+
1facf9fc 3698+/*
3699+ * delete a branch
3700+ */
3701+
3702+/* to show the line number, do not make it inlined function */
4a4d8108 3703+#define AuVerbose(do_info, fmt, ...) do { \
1facf9fc 3704+ if (do_info) \
4a4d8108 3705+ pr_info(fmt, ##__VA_ARGS__); \
1facf9fc 3706+} while (0)
3707+
5afbbe0d
AM
3708+static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
3709+ aufs_bindex_t bbot)
027c5e7a 3710+{
5afbbe0d 3711+ return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
027c5e7a
AM
3712+}
3713+
5afbbe0d
AM
3714+static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
3715+ aufs_bindex_t bbot)
027c5e7a 3716+{
5afbbe0d 3717+ return au_test_ibusy(d_inode(dentry), btop, bbot);
027c5e7a
AM
3718+}
3719+
1facf9fc 3720+/*
3721+ * test if the branch is deletable or not.
3722+ */
3723+static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
b752ccd1 3724+ unsigned int sigen, const unsigned int verbose)
1facf9fc 3725+{
3726+ int err, i, j, ndentry;
5afbbe0d 3727+ aufs_bindex_t btop, bbot;
1facf9fc 3728+ struct au_dcsub_pages dpages;
3729+ struct au_dpage *dpage;
3730+ struct dentry *d;
1facf9fc 3731+
3732+ err = au_dpages_init(&dpages, GFP_NOFS);
3733+ if (unlikely(err))
3734+ goto out;
3735+ err = au_dcsub_pages(&dpages, root, NULL, NULL);
3736+ if (unlikely(err))
3737+ goto out_dpages;
3738+
1facf9fc 3739+ for (i = 0; !err && i < dpages.ndpage; i++) {
3740+ dpage = dpages.dpages + i;
3741+ ndentry = dpage->ndentry;
3742+ for (j = 0; !err && j < ndentry; j++) {
3743+ d = dpage->dentries[j];
c1595e42 3744+ AuDebugOn(au_dcount(d) <= 0);
027c5e7a 3745+ if (!au_digen_test(d, sigen)) {
1facf9fc 3746+ di_read_lock_child(d, AuLock_IR);
027c5e7a
AM
3747+ if (unlikely(au_dbrange_test(d))) {
3748+ di_read_unlock(d, AuLock_IR);
3749+ continue;
3750+ }
3751+ } else {
1facf9fc 3752+ di_write_lock_child(d);
027c5e7a
AM
3753+ if (unlikely(au_dbrange_test(d))) {
3754+ di_write_unlock(d);
3755+ continue;
3756+ }
1facf9fc 3757+ err = au_reval_dpath(d, sigen);
3758+ if (!err)
3759+ di_downgrade_lock(d, AuLock_IR);
3760+ else {
3761+ di_write_unlock(d);
3762+ break;
3763+ }
3764+ }
3765+
027c5e7a 3766+ /* AuDbgDentry(d); */
5afbbe0d
AM
3767+ btop = au_dbtop(d);
3768+ bbot = au_dbbot(d);
3769+ if (btop <= bindex
3770+ && bindex <= bbot
1facf9fc 3771+ && au_h_dptr(d, bindex)
5afbbe0d 3772+ && au_test_dbusy(d, btop, bbot)) {
1facf9fc 3773+ err = -EBUSY;
523b37e3 3774+ AuVerbose(verbose, "busy %pd\n", d);
027c5e7a 3775+ AuDbgDentry(d);
1facf9fc 3776+ }
3777+ di_read_unlock(d, AuLock_IR);
3778+ }
3779+ }
3780+
4f0767ce 3781+out_dpages:
1facf9fc 3782+ au_dpages_free(&dpages);
4f0767ce 3783+out:
1facf9fc 3784+ return err;
3785+}
3786+
3787+static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
b752ccd1 3788+ unsigned int sigen, const unsigned int verbose)
1facf9fc 3789+{
3790+ int err;
7f207e10
AM
3791+ unsigned long long max, ull;
3792+ struct inode *i, **array;
5afbbe0d 3793+ aufs_bindex_t btop, bbot;
1facf9fc 3794+
7f207e10
AM
3795+ array = au_iarray_alloc(sb, &max);
3796+ err = PTR_ERR(array);
3797+ if (IS_ERR(array))
3798+ goto out;
3799+
1facf9fc 3800+ err = 0;
7f207e10
AM
3801+ AuDbg("b%d\n", bindex);
3802+ for (ull = 0; !err && ull < max; ull++) {
3803+ i = array[ull];
076b876e
AM
3804+ if (unlikely(!i))
3805+ break;
7f207e10 3806+ if (i->i_ino == AUFS_ROOT_INO)
1facf9fc 3807+ continue;
3808+
7f207e10 3809+ /* AuDbgInode(i); */
537831f9 3810+ if (au_iigen(i, NULL) == sigen)
1facf9fc 3811+ ii_read_lock_child(i);
3812+ else {
3813+ ii_write_lock_child(i);
027c5e7a
AM
3814+ err = au_refresh_hinode_self(i);
3815+ au_iigen_dec(i);
1facf9fc 3816+ if (!err)
3817+ ii_downgrade_lock(i);
3818+ else {
3819+ ii_write_unlock(i);
3820+ break;
3821+ }
3822+ }
3823+
5afbbe0d
AM
3824+ btop = au_ibtop(i);
3825+ bbot = au_ibbot(i);
3826+ if (btop <= bindex
3827+ && bindex <= bbot
1facf9fc 3828+ && au_h_iptr(i, bindex)
5afbbe0d 3829+ && au_test_ibusy(i, btop, bbot)) {
1facf9fc 3830+ err = -EBUSY;
3831+ AuVerbose(verbose, "busy i%lu\n", i->i_ino);
7f207e10 3832+ AuDbgInode(i);
1facf9fc 3833+ }
3834+ ii_read_unlock(i);
3835+ }
7f207e10 3836+ au_iarray_free(array, max);
1facf9fc 3837+
7f207e10 3838+out:
1facf9fc 3839+ return err;
3840+}
3841+
b752ccd1
AM
3842+static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3843+ const unsigned int verbose)
1facf9fc 3844+{
3845+ int err;
3846+ unsigned int sigen;
3847+
3848+ sigen = au_sigen(root->d_sb);
3849+ DiMustNoWaiters(root);
5527c038 3850+ IiMustNoWaiters(d_inode(root));
1facf9fc 3851+ di_write_unlock(root);
b752ccd1 3852+ err = test_dentry_busy(root, bindex, sigen, verbose);
1facf9fc 3853+ if (!err)
b752ccd1 3854+ err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
1facf9fc 3855+ di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3856+
3857+ return err;
3858+}
3859+
076b876e
AM
3860+static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3861+ struct file **to_free, int *idx)
3862+{
3863+ int err;
c1595e42 3864+ unsigned char matched, root;
5afbbe0d 3865+ aufs_bindex_t bindex, bbot;
076b876e
AM
3866+ struct au_fidir *fidir;
3867+ struct au_hfile *hfile;
3868+
3869+ err = 0;
2000de60 3870+ root = IS_ROOT(file->f_path.dentry);
c1595e42
JR
3871+ if (root) {
3872+ get_file(file);
3873+ to_free[*idx] = file;
3874+ (*idx)++;
3875+ goto out;
3876+ }
3877+
076b876e 3878+ matched = 0;
076b876e
AM
3879+ fidir = au_fi(file)->fi_hdir;
3880+ AuDebugOn(!fidir);
5afbbe0d
AM
3881+ bbot = au_fbbot_dir(file);
3882+ for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
076b876e
AM
3883+ hfile = fidir->fd_hfile + bindex;
3884+ if (!hfile->hf_file)
3885+ continue;
3886+
c1595e42 3887+ if (hfile->hf_br->br_id == br_id) {
076b876e 3888+ matched = 1;
076b876e 3889+ break;
c1595e42 3890+ }
076b876e 3891+ }
c1595e42 3892+ if (matched)
076b876e
AM
3893+ err = -EBUSY;
3894+
3895+out:
3896+ return err;
3897+}
3898+
3899+static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3900+ struct file **to_free, int opened)
3901+{
3902+ int err, idx;
3903+ unsigned long long ull, max;
5afbbe0d 3904+ aufs_bindex_t btop;
076b876e 3905+ struct file *file, **array;
076b876e
AM
3906+ struct dentry *root;
3907+ struct au_hfile *hfile;
3908+
3909+ array = au_farray_alloc(sb, &max);
3910+ err = PTR_ERR(array);
3911+ if (IS_ERR(array))
3912+ goto out;
3913+
3914+ err = 0;
3915+ idx = 0;
3916+ root = sb->s_root;
3917+ di_write_unlock(root);
3918+ for (ull = 0; ull < max; ull++) {
3919+ file = array[ull];
3920+ if (unlikely(!file))
3921+ break;
3922+
3923+ /* AuDbg("%pD\n", file); */
3924+ fi_read_lock(file);
5afbbe0d 3925+ btop = au_fbtop(file);
2000de60 3926+ if (!d_is_dir(file->f_path.dentry)) {
076b876e
AM
3927+ hfile = &au_fi(file)->fi_htop;
3928+ if (hfile->hf_br->br_id == br_id)
3929+ err = -EBUSY;
3930+ } else
3931+ err = test_dir_busy(file, br_id, to_free, &idx);
3932+ fi_read_unlock(file);
3933+ if (unlikely(err))
3934+ break;
3935+ }
3936+ di_write_lock_child(root);
3937+ au_farray_free(array, max);
3938+ AuDebugOn(idx > opened);
3939+
3940+out:
3941+ return err;
3942+}
3943+
3944+static void br_del_file(struct file **to_free, unsigned long long opened,
062440b3 3945+ aufs_bindex_t br_id)
076b876e
AM
3946+{
3947+ unsigned long long ull;
5afbbe0d 3948+ aufs_bindex_t bindex, btop, bbot, bfound;
076b876e
AM
3949+ struct file *file;
3950+ struct au_fidir *fidir;
3951+ struct au_hfile *hfile;
3952+
3953+ for (ull = 0; ull < opened; ull++) {
3954+ file = to_free[ull];
3955+ if (unlikely(!file))
3956+ break;
3957+
3958+ /* AuDbg("%pD\n", file); */
2000de60 3959+ AuDebugOn(!d_is_dir(file->f_path.dentry));
076b876e
AM
3960+ bfound = -1;
3961+ fidir = au_fi(file)->fi_hdir;
3962+ AuDebugOn(!fidir);
3963+ fi_write_lock(file);
5afbbe0d
AM
3964+ btop = au_fbtop(file);
3965+ bbot = au_fbbot_dir(file);
3966+ for (bindex = btop; bindex <= bbot; bindex++) {
076b876e
AM
3967+ hfile = fidir->fd_hfile + bindex;
3968+ if (!hfile->hf_file)
3969+ continue;
3970+
3971+ if (hfile->hf_br->br_id == br_id) {
3972+ bfound = bindex;
3973+ break;
3974+ }
3975+ }
3976+ AuDebugOn(bfound < 0);
3977+ au_set_h_fptr(file, bfound, NULL);
5afbbe0d
AM
3978+ if (bfound == btop) {
3979+ for (btop++; btop <= bbot; btop++)
3980+ if (au_hf_dir(file, btop)) {
3981+ au_set_fbtop(file, btop);
076b876e
AM
3982+ break;
3983+ }
3984+ }
3985+ fi_write_unlock(file);
3986+ }
3987+}
3988+
1facf9fc 3989+static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
3990+ const aufs_bindex_t bindex,
5afbbe0d 3991+ const aufs_bindex_t bbot)
1facf9fc 3992+{
3993+ struct au_branch **brp, **p;
3994+
dece6358
AM
3995+ AuRwMustWriteLock(&sbinfo->si_rwsem);
3996+
1facf9fc 3997+ brp = sbinfo->si_branch + bindex;
5afbbe0d
AM
3998+ if (bindex < bbot)
3999+ memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
4000+ sbinfo->si_branch[0 + bbot] = NULL;
4001+ sbinfo->si_bbot--;
1facf9fc 4002+
e2f27e51
AM
4003+ p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
4004+ /*may_shrink*/1);
1facf9fc 4005+ if (p)
4006+ sbinfo->si_branch = p;
4a4d8108 4007+ /* harmless error */
1facf9fc 4008+}
4009+
4010+static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
5afbbe0d 4011+ const aufs_bindex_t bbot)
1facf9fc 4012+{
4013+ struct au_hdentry *hdp, *p;
4014+
1308ab2a 4015+ AuRwMustWriteLock(&dinfo->di_rwsem);
4016+
5afbbe0d
AM
4017+ hdp = au_hdentry(dinfo, bindex);
4018+ if (bindex < bbot)
4019+ memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
4020+ /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
4021+ dinfo->di_bbot--;
1facf9fc 4022+
e2f27e51
AM
4023+ p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
4024+ /*may_shrink*/1);
1facf9fc 4025+ if (p)
4026+ dinfo->di_hdentry = p;
4a4d8108 4027+ /* harmless error */
1facf9fc 4028+}
4029+
4030+static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
5afbbe0d 4031+ const aufs_bindex_t bbot)
1facf9fc 4032+{
4033+ struct au_hinode *hip, *p;
4034+
1308ab2a 4035+ AuRwMustWriteLock(&iinfo->ii_rwsem);
4036+
5afbbe0d
AM
4037+ hip = au_hinode(iinfo, bindex);
4038+ if (bindex < bbot)
4039+ memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
4040+ /* au_hinode_init(au_hinode(iinfo, bbot)); */
4041+ iinfo->ii_bbot--;
1facf9fc 4042+
e2f27e51
AM
4043+ p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
4044+ /*may_shrink*/1);
1facf9fc 4045+ if (p)
4046+ iinfo->ii_hinode = p;
4a4d8108 4047+ /* harmless error */
1facf9fc 4048+}
4049+
4050+static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
4051+ struct au_branch *br)
4052+{
5afbbe0d 4053+ aufs_bindex_t bbot;
1facf9fc 4054+ struct au_sbinfo *sbinfo;
53392da6
AM
4055+ struct dentry *root, *h_root;
4056+ struct inode *inode, *h_inode;
4057+ struct au_hinode *hinode;
1facf9fc 4058+
dece6358
AM
4059+ SiMustWriteLock(sb);
4060+
1facf9fc 4061+ root = sb->s_root;
5527c038 4062+ inode = d_inode(root);
1facf9fc 4063+ sbinfo = au_sbi(sb);
5afbbe0d 4064+ bbot = sbinfo->si_bbot;
1facf9fc 4065+
53392da6
AM
4066+ h_root = au_h_dptr(root, bindex);
4067+ hinode = au_hi(inode, bindex);
4068+ h_inode = au_igrab(hinode->hi_inode);
4069+ au_hiput(hinode);
1facf9fc 4070+
53392da6 4071+ au_sbilist_lock();
5afbbe0d
AM
4072+ au_br_do_del_brp(sbinfo, bindex, bbot);
4073+ au_br_do_del_hdp(au_di(root), bindex, bbot);
4074+ au_br_do_del_hip(au_ii(inode), bindex, bbot);
53392da6
AM
4075+ au_sbilist_unlock();
4076+
8b6a4947
AM
4077+ /* ignore an error */
4078+ au_dr_br_fin(sb, br); /* always, regardless the mount option */
4079+
53392da6
AM
4080+ dput(h_root);
4081+ iput(h_inode);
4082+ au_br_do_free(br);
1facf9fc 4083+}
4084+
79b8bda9
AM
4085+static unsigned long long empty_cb(struct super_block *sb, void *array,
4086+ unsigned long long max, void *arg)
076b876e
AM
4087+{
4088+ return max;
4089+}
4090+
1facf9fc 4091+int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
4092+{
4093+ int err, rerr, i;
076b876e 4094+ unsigned long long opened;
1facf9fc 4095+ unsigned int mnt_flags;
5afbbe0d 4096+ aufs_bindex_t bindex, bbot, br_id;
1facf9fc 4097+ unsigned char do_wh, verbose;
4098+ struct au_branch *br;
4099+ struct au_wbr *wbr;
076b876e
AM
4100+ struct dentry *root;
4101+ struct file **to_free;
1facf9fc 4102+
4103+ err = 0;
076b876e
AM
4104+ opened = 0;
4105+ to_free = NULL;
4106+ root = sb->s_root;
4107+ bindex = au_find_dbindex(root, del->h_path.dentry);
1facf9fc 4108+ if (bindex < 0) {
4109+ if (remount)
4110+ goto out; /* success */
4111+ err = -ENOENT;
4a4d8108 4112+ pr_err("%s no such branch\n", del->pathname);
1facf9fc 4113+ goto out;
4114+ }
4115+ AuDbg("bindex b%d\n", bindex);
4116+
4117+ err = -EBUSY;
4118+ mnt_flags = au_mntflags(sb);
4119+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
5afbbe0d
AM
4120+ bbot = au_sbbot(sb);
4121+ if (unlikely(!bbot)) {
1facf9fc 4122+ AuVerbose(verbose, "no more branches left\n");
4123+ goto out;
4124+ }
acd2b654 4125+
1facf9fc 4126+ br = au_sbr(sb, bindex);
86dc4139 4127+ AuDebugOn(!path_equal(&br->br_path, &del->h_path));
acd2b654
AM
4128+ if (unlikely(au_lcnt_read(&br->br_count, /*do_rev*/1))) {
4129+ AuVerbose(verbose, "br %pd2 is busy now\n", del->h_path.dentry);
4130+ goto out;
4131+ }
076b876e
AM
4132+
4133+ br_id = br->br_id;
acd2b654 4134+ opened = au_lcnt_read(&br->br_nfiles, /*do_rev*/1);
076b876e 4135+ if (unlikely(opened)) {
79b8bda9 4136+ to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
076b876e
AM
4137+ err = PTR_ERR(to_free);
4138+ if (IS_ERR(to_free))
4139+ goto out;
4140+
4141+ err = test_file_busy(sb, br_id, to_free, opened);
4142+ if (unlikely(err)) {
4143+ AuVerbose(verbose, "%llu file(s) opened\n", opened);
4144+ goto out;
4145+ }
1facf9fc 4146+ }
4147+
4148+ wbr = br->br_wbr;
4149+ do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
4150+ if (do_wh) {
1308ab2a 4151+ /* instead of WbrWhMustWriteLock(wbr) */
4152+ SiMustWriteLock(sb);
1facf9fc 4153+ for (i = 0; i < AuBrWh_Last; i++) {
4154+ dput(wbr->wbr_wh[i]);
4155+ wbr->wbr_wh[i] = NULL;
4156+ }
4157+ }
4158+
076b876e 4159+ err = test_children_busy(root, bindex, verbose);
1facf9fc 4160+ if (unlikely(err)) {
4161+ if (do_wh)
4162+ goto out_wh;
4163+ goto out;
4164+ }
4165+
4166+ err = 0;
076b876e
AM
4167+ if (to_free) {
4168+ /*
4169+ * now we confirmed the branch is deletable.
4170+ * let's free the remaining opened dirs on the branch.
4171+ */
4172+ di_write_unlock(root);
4173+ br_del_file(to_free, opened, br_id);
4174+ di_write_lock_child(root);
4175+ }
4176+
062440b3
AM
4177+ sysaufs_brs_del(sb, bindex); /* remove successors */
4178+ dbgaufs_xino_del(br); /* remove one */
4179+ au_br_do_del(sb, bindex, br);
4180+ sysaufs_brs_add(sb, bindex); /* append successors */
4181+ dbgaufs_brs_add(sb, bindex, /*topdown*/1); /* rename successors */
1facf9fc 4182+
1308ab2a 4183+ if (!bindex) {
5527c038 4184+ au_cpup_attr_all(d_inode(root), /*force*/1);
1308ab2a 4185+ sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
4186+ } else
5527c038 4187+ au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
1facf9fc 4188+ if (au_opt_test(mnt_flags, PLINK))
4189+ au_plink_half_refresh(sb, br_id);
4190+
1facf9fc 4191+ goto out; /* success */
4192+
4f0767ce 4193+out_wh:
1facf9fc 4194+ /* revert */
86dc4139 4195+ rerr = au_br_init_wh(sb, br, br->br_perm);
1facf9fc 4196+ if (rerr)
0c3ec466
AM
4197+ pr_warn("failed re-creating base whiteout, %s. (%d)\n",
4198+ del->pathname, rerr);
4f0767ce 4199+out:
076b876e
AM
4200+ if (to_free)
4201+ au_farray_free(to_free, opened);
1facf9fc 4202+ return err;
4203+}
4204+
4205+/* ---------------------------------------------------------------------- */
4206+
027c5e7a
AM
4207+static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
4208+{
4209+ int err;
5afbbe0d 4210+ aufs_bindex_t btop, bbot;
027c5e7a
AM
4211+ struct aufs_ibusy ibusy;
4212+ struct inode *inode, *h_inode;
4213+
4214+ err = -EPERM;
4215+ if (unlikely(!capable(CAP_SYS_ADMIN)))
4216+ goto out;
4217+
4218+ err = copy_from_user(&ibusy, arg, sizeof(ibusy));
4219+ if (!err)
4220+ err = !access_ok(VERIFY_WRITE, &arg->h_ino, sizeof(arg->h_ino));
4221+ if (unlikely(err)) {
4222+ err = -EFAULT;
4223+ AuTraceErr(err);
4224+ goto out;
4225+ }
4226+
4227+ err = -EINVAL;
4228+ si_read_lock(sb, AuLock_FLUSH);
5afbbe0d 4229+ if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
027c5e7a
AM
4230+ goto out_unlock;
4231+
4232+ err = 0;
4233+ ibusy.h_ino = 0; /* invalid */
4234+ inode = ilookup(sb, ibusy.ino);
4235+ if (!inode
4236+ || inode->i_ino == AUFS_ROOT_INO
5afbbe0d 4237+ || au_is_bad_inode(inode))
027c5e7a
AM
4238+ goto out_unlock;
4239+
4240+ ii_read_lock_child(inode);
5afbbe0d
AM
4241+ btop = au_ibtop(inode);
4242+ bbot = au_ibbot(inode);
4243+ if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
027c5e7a 4244+ h_inode = au_h_iptr(inode, ibusy.bindex);
5afbbe0d 4245+ if (h_inode && au_test_ibusy(inode, btop, bbot))
027c5e7a
AM
4246+ ibusy.h_ino = h_inode->i_ino;
4247+ }
4248+ ii_read_unlock(inode);
4249+ iput(inode);
4250+
4251+out_unlock:
4252+ si_read_unlock(sb);
4253+ if (!err) {
4254+ err = __put_user(ibusy.h_ino, &arg->h_ino);
4255+ if (unlikely(err)) {
4256+ err = -EFAULT;
4257+ AuTraceErr(err);
4258+ }
4259+ }
4260+out:
4261+ return err;
4262+}
4263+
4264+long au_ibusy_ioctl(struct file *file, unsigned long arg)
4265+{
2000de60 4266+ return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
027c5e7a
AM
4267+}
4268+
4269+#ifdef CONFIG_COMPAT
4270+long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
4271+{
2000de60 4272+ return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
027c5e7a
AM
4273+}
4274+#endif
4275+
4276+/* ---------------------------------------------------------------------- */
4277+
1facf9fc 4278+/*
4279+ * change a branch permission
4280+ */
4281+
dece6358
AM
4282+static void au_warn_ima(void)
4283+{
4284+#ifdef CONFIG_IMA
1308ab2a 4285+ /* since it doesn't support mark_files_ro() */
027c5e7a 4286+ AuWarn1("RW -> RO makes IMA to produce wrong message\n");
dece6358
AM
4287+#endif
4288+}
4289+
1facf9fc 4290+static int do_need_sigen_inc(int a, int b)
4291+{
4292+ return au_br_whable(a) && !au_br_whable(b);
4293+}
4294+
4295+static int need_sigen_inc(int old, int new)
4296+{
4297+ return do_need_sigen_inc(old, new)
4298+ || do_need_sigen_inc(new, old);
4299+}
4300+
4301+static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
4302+{
7f207e10 4303+ int err, do_warn;
027c5e7a 4304+ unsigned int mnt_flags;
7f207e10 4305+ unsigned long long ull, max;
e49829fe 4306+ aufs_bindex_t br_id;
38d290e6 4307+ unsigned char verbose, writer;
7f207e10 4308+ struct file *file, *hf, **array;
e49829fe 4309+ struct au_hfile *hfile;
0b487229 4310+ struct inode *h_inode;
1facf9fc 4311+
027c5e7a
AM
4312+ mnt_flags = au_mntflags(sb);
4313+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
4314+
7f207e10
AM
4315+ array = au_farray_alloc(sb, &max);
4316+ err = PTR_ERR(array);
4317+ if (IS_ERR(array))
1facf9fc 4318+ goto out;
4319+
7f207e10 4320+ do_warn = 0;
e49829fe 4321+ br_id = au_sbr_id(sb, bindex);
7f207e10
AM
4322+ for (ull = 0; ull < max; ull++) {
4323+ file = array[ull];
076b876e
AM
4324+ if (unlikely(!file))
4325+ break;
1facf9fc 4326+
523b37e3 4327+ /* AuDbg("%pD\n", file); */
1facf9fc 4328+ fi_read_lock(file);
4329+ if (unlikely(au_test_mmapped(file))) {
4330+ err = -EBUSY;
523b37e3 4331+ AuVerbose(verbose, "mmapped %pD\n", file);
7f207e10 4332+ AuDbgFile(file);
1facf9fc 4333+ FiMustNoWaiters(file);
4334+ fi_read_unlock(file);
7f207e10 4335+ goto out_array;
1facf9fc 4336+ }
4337+
e49829fe
JR
4338+ hfile = &au_fi(file)->fi_htop;
4339+ hf = hfile->hf_file;
7e9cd9fe 4340+ if (!d_is_reg(file->f_path.dentry)
1facf9fc 4341+ || !(file->f_mode & FMODE_WRITE)
e49829fe 4342+ || hfile->hf_br->br_id != br_id
7f207e10
AM
4343+ || !(hf->f_mode & FMODE_WRITE))
4344+ array[ull] = NULL;
4345+ else {
4346+ do_warn = 1;
4347+ get_file(file);
1facf9fc 4348+ }
4349+
1facf9fc 4350+ FiMustNoWaiters(file);
4351+ fi_read_unlock(file);
7f207e10
AM
4352+ fput(file);
4353+ }
1facf9fc 4354+
4355+ err = 0;
7f207e10 4356+ if (do_warn)
dece6358 4357+ au_warn_ima();
7f207e10
AM
4358+
4359+ for (ull = 0; ull < max; ull++) {
4360+ file = array[ull];
4361+ if (!file)
4362+ continue;
4363+
1facf9fc 4364+ /* todo: already flushed? */
523b37e3
AM
4365+ /*
4366+ * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4367+ * approach which resets f_mode and calls mnt_drop_write() and
4368+ * file_release_write() for each file, because the branch
4369+ * attribute in aufs world is totally different from the native
4370+ * fs rw/ro mode.
4371+ */
7f207e10
AM
4372+ /* fi_read_lock(file); */
4373+ hfile = &au_fi(file)->fi_htop;
4374+ hf = hfile->hf_file;
4375+ /* fi_read_unlock(file); */
027c5e7a 4376+ spin_lock(&hf->f_lock);
38d290e6
JR
4377+ writer = !!(hf->f_mode & FMODE_WRITER);
4378+ hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
027c5e7a 4379+ spin_unlock(&hf->f_lock);
38d290e6 4380+ if (writer) {
0b487229
JR
4381+ h_inode = file_inode(hf);
4382+ if (hf->f_mode & FMODE_READ)
4383+ i_readcount_inc(h_inode);
4384+ put_write_access(h_inode);
c06a8ce3 4385+ __mnt_drop_write(hf->f_path.mnt);
1facf9fc 4386+ }
4387+ }
4388+
7f207e10
AM
4389+out_array:
4390+ au_farray_free(array, max);
4f0767ce 4391+out:
7f207e10 4392+ AuTraceErr(err);
1facf9fc 4393+ return err;
4394+}
4395+
4396+int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
7f207e10 4397+ int *do_refresh)
1facf9fc 4398+{
4399+ int err, rerr;
4400+ aufs_bindex_t bindex;
4401+ struct dentry *root;
4402+ struct au_branch *br;
076b876e 4403+ struct au_br_fhsm *bf;
1facf9fc 4404+
4405+ root = sb->s_root;
1facf9fc 4406+ bindex = au_find_dbindex(root, mod->h_root);
4407+ if (bindex < 0) {
4408+ if (remount)
4409+ return 0; /* success */
4410+ err = -ENOENT;
4a4d8108 4411+ pr_err("%s no such branch\n", mod->path);
1facf9fc 4412+ goto out;
4413+ }
4414+ AuDbg("bindex b%d\n", bindex);
4415+
5527c038 4416+ err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
1facf9fc 4417+ if (unlikely(err))
4418+ goto out;
4419+
4420+ br = au_sbr(sb, bindex);
86dc4139 4421+ AuDebugOn(mod->h_root != au_br_dentry(br));
1facf9fc 4422+ if (br->br_perm == mod->perm)
4423+ return 0; /* success */
4424+
076b876e
AM
4425+ /* pre-allocate for non-fhsm --> fhsm */
4426+ bf = NULL;
4427+ if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4428+ err = au_fhsm_br_alloc(br);
4429+ if (unlikely(err))
4430+ goto out;
4431+ bf = br->br_fhsm;
4432+ br->br_fhsm = NULL;
4433+ }
4434+
1facf9fc 4435+ if (au_br_writable(br->br_perm)) {
4436+ /* remove whiteout base */
86dc4139 4437+ err = au_br_init_wh(sb, br, mod->perm);
1facf9fc 4438+ if (unlikely(err))
076b876e 4439+ goto out_bf;
1facf9fc 4440+
4441+ if (!au_br_writable(mod->perm)) {
4442+ /* rw --> ro, file might be mmapped */
4443+ DiMustNoWaiters(root);
5527c038 4444+ IiMustNoWaiters(d_inode(root));
1facf9fc 4445+ di_write_unlock(root);
4446+ err = au_br_mod_files_ro(sb, bindex);
4447+ /* aufs_write_lock() calls ..._child() */
4448+ di_write_lock_child(root);
4449+
4450+ if (unlikely(err)) {
4451+ rerr = -ENOMEM;
be52b249 4452+ br->br_wbr = kzalloc(sizeof(*br->br_wbr),
1facf9fc 4453+ GFP_NOFS);
86dc4139
AM
4454+ if (br->br_wbr)
4455+ rerr = au_wbr_init(br, sb, br->br_perm);
1facf9fc 4456+ if (unlikely(rerr)) {
4457+ AuIOErr("nested error %d (%d)\n",
4458+ rerr, err);
4459+ br->br_perm = mod->perm;
4460+ }
4461+ }
4462+ }
4463+ } else if (au_br_writable(mod->perm)) {
4464+ /* ro --> rw */
4465+ err = -ENOMEM;
be52b249 4466+ br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
1facf9fc 4467+ if (br->br_wbr) {
86dc4139 4468+ err = au_wbr_init(br, sb, mod->perm);
1facf9fc 4469+ if (unlikely(err)) {
e49925d1 4470+ au_kfree_rcu(br->br_wbr);
1facf9fc 4471+ br->br_wbr = NULL;
4472+ }
4473+ }
4474+ }
076b876e
AM
4475+ if (unlikely(err))
4476+ goto out_bf;
4477+
4478+ if (au_br_fhsm(br->br_perm)) {
4479+ if (!au_br_fhsm(mod->perm)) {
4480+ /* fhsm --> non-fhsm */
4481+ au_br_fhsm_fin(br->br_fhsm);
e49925d1 4482+ au_kfree_rcu(br->br_fhsm);
076b876e
AM
4483+ br->br_fhsm = NULL;
4484+ }
4485+ } else if (au_br_fhsm(mod->perm))
4486+ /* non-fhsm --> fhsm */
4487+ br->br_fhsm = bf;
4488+
076b876e
AM
4489+ *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4490+ br->br_perm = mod->perm;
4491+ goto out; /* success */
1facf9fc 4492+
076b876e 4493+out_bf:
e49925d1 4494+ au_kfree_try_rcu(bf);
076b876e
AM
4495+out:
4496+ AuTraceErr(err);
4497+ return err;
4498+}
4499+
4500+/* ---------------------------------------------------------------------- */
4501+
4502+int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4503+{
4504+ int err;
4505+ struct kstatfs kstfs;
4506+
4507+ err = vfs_statfs(&br->br_path, &kstfs);
1facf9fc 4508+ if (!err) {
076b876e
AM
4509+ stfs->f_blocks = kstfs.f_blocks;
4510+ stfs->f_bavail = kstfs.f_bavail;
4511+ stfs->f_files = kstfs.f_files;
4512+ stfs->f_ffree = kstfs.f_ffree;
1facf9fc 4513+ }
4514+
1facf9fc 4515+ return err;
4516+}
7f207e10
AM
4517diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
4518--- /usr/share/empty/fs/aufs/branch.h 1970-01-01 01:00:00.000000000 +0100
0b487229
JR
4519+++ linux/fs/aufs/branch.h 2021-02-22 23:30:37.699347580 +0100
4520@@ -0,0 +1,366 @@
062440b3 4521+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 4522+/*
0b487229 4523+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 4524+ *
4525+ * This program, aufs is free software; you can redistribute it and/or modify
4526+ * it under the terms of the GNU General Public License as published by
4527+ * the Free Software Foundation; either version 2 of the License, or
4528+ * (at your option) any later version.
dece6358
AM
4529+ *
4530+ * This program is distributed in the hope that it will be useful,
4531+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
4532+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4533+ * GNU General Public License for more details.
4534+ *
4535+ * You should have received a copy of the GNU General Public License
523b37e3 4536+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 4537+ */
4538+
4539+/*
4540+ * branch filesystems and xino for them
4541+ */
4542+
4543+#ifndef __AUFS_BRANCH_H__
4544+#define __AUFS_BRANCH_H__
4545+
4546+#ifdef __KERNEL__
4547+
1facf9fc 4548+#include <linux/mount.h>
8b6a4947 4549+#include "dirren.h"
4a4d8108 4550+#include "dynop.h"
acd2b654 4551+#include "lcnt.h"
1facf9fc 4552+#include "rwsem.h"
4553+#include "super.h"
4554+
4555+/* ---------------------------------------------------------------------- */
4556+
4557+/* a xino file */
062440b3 4558+struct au_xino {
acd2b654
AM
4559+ struct file **xi_file;
4560+ unsigned int xi_nfile;
4561+
521ced18
JR
4562+ struct {
4563+ spinlock_t spin;
4564+ ino_t *array;
4565+ int total;
4566+ /* reserved for future use */
4567+ /* unsigned long *bitmap; */
4568+ wait_queue_head_t wqh;
4569+ } xi_nondir;
1facf9fc 4570+
acd2b654 4571+ struct mutex xi_mtx; /* protects xi_file array */
e49925d1 4572+ struct hlist_bl_head xi_writing;
acd2b654 4573+
062440b3 4574+ atomic_t xi_truncating;
1facf9fc 4575+
062440b3 4576+ struct kref xi_kref;
1facf9fc 4577+};
4578+
076b876e
AM
4579+/* File-based Hierarchical Storage Management */
4580+struct au_br_fhsm {
4581+#ifdef CONFIG_AUFS_FHSM
4582+ struct mutex bf_lock;
4583+ unsigned long bf_jiffy;
4584+ struct aufs_stfs bf_stfs;
4585+ int bf_readable;
4586+#endif
4587+};
4588+
1facf9fc 4589+/* members for writable branch only */
4590+enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4591+struct au_wbr {
dece6358 4592+ struct au_rwsem wbr_wh_rwsem;
1facf9fc 4593+ struct dentry *wbr_wh[AuBrWh_Last];
4a4d8108 4594+ atomic_t wbr_wh_running;
1facf9fc 4595+#define wbr_whbase wbr_wh[AuBrWh_BASE] /* whiteout base */
4596+#define wbr_plink wbr_wh[AuBrWh_PLINK] /* pseudo-link dir */
4597+#define wbr_orph wbr_wh[AuBrWh_ORPH] /* dir for orphans */
4598+
4599+ /* mfs mode */
4600+ unsigned long long wbr_bytes;
4601+};
4602+
4a4d8108
AM
4603+/* ext2 has 3 types of operations at least, ext3 has 4 */
4604+#define AuBrDynOp (AuDyLast * 4)
4605+
1716fcea
AM
4606+#ifdef CONFIG_AUFS_HFSNOTIFY
4607+/* support for asynchronous destruction */
4608+struct au_br_hfsnotify {
4609+ struct fsnotify_group *hfsn_group;
4610+};
4611+#endif
4612+
392086de
AM
4613+/* sysfs entries */
4614+struct au_brsysfs {
4615+ char name[16];
4616+ struct attribute attr;
4617+};
4618+
4619+enum {
4620+ AuBrSysfs_BR,
4621+ AuBrSysfs_BRID,
4622+ AuBrSysfs_Last
4623+};
4624+
1facf9fc 4625+/* protected by superblock rwsem */
4626+struct au_branch {
062440b3 4627+ struct au_xino *br_xino;
1facf9fc 4628+
4629+ aufs_bindex_t br_id;
4630+
4631+ int br_perm;
86dc4139 4632+ struct path br_path;
4a4d8108
AM
4633+ spinlock_t br_dykey_lock;
4634+ struct au_dykey *br_dykey[AuBrDynOp];
acd2b654
AM
4635+ au_lcnt_t br_nfiles; /* opened files */
4636+ au_lcnt_t br_count; /* in-use for other */
1facf9fc 4637+
4638+ struct au_wbr *br_wbr;
076b876e 4639+ struct au_br_fhsm *br_fhsm;
1facf9fc 4640+
027c5e7a 4641+#ifdef CONFIG_AUFS_HFSNOTIFY
1716fcea 4642+ struct au_br_hfsnotify *br_hfsn;
027c5e7a
AM
4643+#endif
4644+
1facf9fc 4645+#ifdef CONFIG_SYSFS
392086de
AM
4646+ /* entries under sysfs per mount-point */
4647+ struct au_brsysfs br_sysfs[AuBrSysfs_Last];
1facf9fc 4648+#endif
8b6a4947 4649+
062440b3
AM
4650+#ifdef CONFIG_DEBUG_FS
4651+ struct dentry *br_dbgaufs; /* xino */
4652+#endif
4653+
8b6a4947 4654+ struct au_dr_br br_dirren;
1facf9fc 4655+};
4656+
4657+/* ---------------------------------------------------------------------- */
4658+
86dc4139
AM
4659+static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4660+{
4661+ return br->br_path.mnt;
4662+}
4663+
4664+static inline struct dentry *au_br_dentry(struct au_branch *br)
4665+{
4666+ return br->br_path.dentry;
4667+}
4668+
4669+static inline struct super_block *au_br_sb(struct au_branch *br)
4670+{
4671+ return au_br_mnt(br)->mnt_sb;
4672+}
4673+
1facf9fc 4674+static inline int au_br_rdonly(struct au_branch *br)
4675+{
8b6a4947 4676+ return (sb_rdonly(au_br_sb(br))
1facf9fc 4677+ || !au_br_writable(br->br_perm))
4678+ ? -EROFS : 0;
4679+}
4680+
4a4d8108 4681+static inline int au_br_hnotifyable(int brperm __maybe_unused)
1facf9fc 4682+{
4a4d8108 4683+#ifdef CONFIG_AUFS_HNOTIFY
1e00d052 4684+ return !(brperm & AuBrPerm_RR);
1facf9fc 4685+#else
4686+ return 0;
4687+#endif
4688+}
4689+
b912730e
AM
4690+static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4691+{
4692+ int err, exec_flag;
4693+
4694+ err = 0;
4695+ exec_flag = oflag & __FMODE_EXEC;
79b8bda9 4696+ if (unlikely(exec_flag && path_noexec(&br->br_path)))
b912730e
AM
4697+ err = -EACCES;
4698+
4699+ return err;
4700+}
4701+
062440b3
AM
4702+static inline void au_xino_get(struct au_branch *br)
4703+{
4704+ struct au_xino *xi;
4705+
4706+ xi = br->br_xino;
4707+ if (xi)
4708+ kref_get(&xi->xi_kref);
4709+}
4710+
4711+static inline int au_xino_count(struct au_branch *br)
4712+{
4713+ int v;
4714+ struct au_xino *xi;
4715+
4716+ v = 0;
4717+ xi = br->br_xino;
4718+ if (xi)
4719+ v = kref_read(&xi->xi_kref);
4720+
4721+ return v;
4722+}
4723+
1facf9fc 4724+/* ---------------------------------------------------------------------- */
4725+
4726+/* branch.c */
4727+struct au_sbinfo;
4728+void au_br_free(struct au_sbinfo *sinfo);
4729+int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4730+struct au_opt_add;
4731+int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4732+struct au_opt_del;
4733+int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
027c5e7a
AM
4734+long au_ibusy_ioctl(struct file *file, unsigned long arg);
4735+#ifdef CONFIG_COMPAT
4736+long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4737+#endif
1facf9fc 4738+struct au_opt_mod;
4739+int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
7f207e10 4740+ int *do_refresh);
076b876e
AM
4741+struct aufs_stfs;
4742+int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
1facf9fc 4743+
4744+/* xino.c */
4745+static const loff_t au_loff_max = LLONG_MAX;
4746+
acd2b654 4747+aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry);
0b487229
JR
4748+struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
4749+ int wbrtop);
062440b3
AM
4750+struct file *au_xino_create2(struct super_block *sb, struct path *base,
4751+ struct file *copy_src);
acd2b654
AM
4752+struct au_xi_new {
4753+ struct au_xino *xi; /* switch between xino and xigen */
4754+ int idx;
4755+ struct path *base;
4756+ struct file *copy_src;
4757+};
4758+struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew);
062440b3
AM
4759+
4760+int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4761+ ino_t *ino);
4762+int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4763+ ino_t ino);
5527c038 4764+ssize_t xino_fread(vfs_readf_t func, struct file *file, void *buf, size_t size,
1facf9fc 4765+ loff_t *pos);
5527c038
JR
4766+ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
4767+ size_t size, loff_t *pos);
062440b3
AM
4768+
4769+int au_xib_trunc(struct super_block *sb);
acd2b654 4770+int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin);
1facf9fc 4771+
acd2b654 4772+struct au_xino *au_xino_alloc(unsigned int nfile);
062440b3 4773+int au_xino_put(struct au_branch *br);
acd2b654 4774+struct file *au_xino_file1(struct au_xino *xi);
062440b3 4775+
1facf9fc 4776+struct au_opt_xino;
1facf9fc 4777+void au_xino_clr(struct super_block *sb);
062440b3 4778+int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount);
1facf9fc 4779+struct file *au_xino_def(struct super_block *sb);
062440b3
AM
4780+int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4781+ struct path *base);
4782+
4783+ino_t au_xino_new_ino(struct super_block *sb);
4784+void au_xino_delete_inode(struct inode *inode, const int unlinked);
1facf9fc 4785+
521ced18
JR
4786+void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
4787+ ino_t h_ino, int idx);
4788+int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4789+ int *idx);
4790+
062440b3
AM
4791+int au_xino_path(struct seq_file *seq, struct file *file);
4792+
1facf9fc 4793+/* ---------------------------------------------------------------------- */
4794+
acd2b654
AM
4795+/* @idx is signed to accept -1 meaning the first file */
4796+static inline struct file *au_xino_file(struct au_xino *xi, int idx)
4797+{
4798+ struct file *file;
4799+
4800+ file = NULL;
4801+ if (!xi)
4802+ goto out;
4803+
4804+ if (idx >= 0) {
4805+ if (idx < xi->xi_nfile)
4806+ file = xi->xi_file[idx];
4807+ } else
4808+ file = au_xino_file1(xi);
4809+
4810+out:
4811+ return file;
4812+}
4813+
4814+/* ---------------------------------------------------------------------- */
4815+
1facf9fc 4816+/* Superblock to branch */
4817+static inline
4818+aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4819+{
4820+ return au_sbr(sb, bindex)->br_id;
4821+}
4822+
4823+static inline
4824+struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4825+{
86dc4139 4826+ return au_br_mnt(au_sbr(sb, bindex));
1facf9fc 4827+}
4828+
4829+static inline
4830+struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4831+{
86dc4139 4832+ return au_br_sb(au_sbr(sb, bindex));
1facf9fc 4833+}
4834+
1facf9fc 4835+static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4836+{
4837+ return au_sbr(sb, bindex)->br_perm;
4838+}
4839+
4840+static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4841+{
4842+ return au_br_whable(au_sbr_perm(sb, bindex));
4843+}
4844+
4845+/* ---------------------------------------------------------------------- */
4846+
8b6a4947
AM
4847+#define wbr_wh_read_lock(wbr) au_rw_read_lock(&(wbr)->wbr_wh_rwsem)
4848+#define wbr_wh_write_lock(wbr) au_rw_write_lock(&(wbr)->wbr_wh_rwsem)
4849+#define wbr_wh_read_trylock(wbr) au_rw_read_trylock(&(wbr)->wbr_wh_rwsem)
4850+#define wbr_wh_write_trylock(wbr) au_rw_write_trylock(&(wbr)->wbr_wh_rwsem)
1facf9fc 4851+/*
8b6a4947
AM
4852+#define wbr_wh_read_trylock_nested(wbr) \
4853+ au_rw_read_trylock_nested(&(wbr)->wbr_wh_rwsem)
4854+#define wbr_wh_write_trylock_nested(wbr) \
4855+ au_rw_write_trylock_nested(&(wbr)->wbr_wh_rwsem)
4856+*/
1facf9fc 4857+
8b6a4947
AM
4858+#define wbr_wh_read_unlock(wbr) au_rw_read_unlock(&(wbr)->wbr_wh_rwsem)
4859+#define wbr_wh_write_unlock(wbr) au_rw_write_unlock(&(wbr)->wbr_wh_rwsem)
4860+#define wbr_wh_downgrade_lock(wbr) au_rw_dgrade_lock(&(wbr)->wbr_wh_rwsem)
4861+
4862+#define WbrWhMustNoWaiters(wbr) AuRwMustNoWaiters(&(wbr)->wbr_wh_rwsem)
4863+#define WbrWhMustAnyLock(wbr) AuRwMustAnyLock(&(wbr)->wbr_wh_rwsem)
4864+#define WbrWhMustWriteLock(wbr) AuRwMustWriteLock(&(wbr)->wbr_wh_rwsem)
dece6358 4865+
076b876e
AM
4866+/* ---------------------------------------------------------------------- */
4867+
4868+#ifdef CONFIG_AUFS_FHSM
4869+static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4870+{
4871+ mutex_init(&brfhsm->bf_lock);
4872+ brfhsm->bf_jiffy = 0;
4873+ brfhsm->bf_readable = 0;
4874+}
4875+
4876+static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4877+{
4878+ mutex_destroy(&brfhsm->bf_lock);
4879+}
4880+#else
4881+AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4882+AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4883+#endif
4884+
1facf9fc 4885+#endif /* __KERNEL__ */
4886+#endif /* __AUFS_BRANCH_H__ */
7f207e10
AM
4887diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
4888--- /usr/share/empty/fs/aufs/conf.mk 1970-01-01 01:00:00.000000000 +0100
0b487229 4889+++ linux/fs/aufs/conf.mk 2021-02-22 23:30:37.699347580 +0100
2121bcd9
AM
4890@@ -0,0 +1,40 @@
4891+# SPDX-License-Identifier: GPL-2.0
4a4d8108
AM
4892+
4893+AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4894+
4895+define AuConf
4896+ifdef ${1}
4897+AuConfStr += ${1}=${${1}}
4898+endif
4899+endef
4900+
b752ccd1 4901+AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
e49829fe 4902+ SBILIST \
7f207e10 4903+ HNOTIFY HFSNOTIFY \
4a4d8108 4904+ EXPORT INO_T_64 \
c1595e42 4905+ XATTR \
076b876e 4906+ FHSM \
4a4d8108 4907+ RDU \
8b6a4947 4908+ DIRREN \
4a4d8108
AM
4909+ SHWH \
4910+ BR_RAMFS \
4911+ BR_FUSE POLL \
4912+ BR_HFSPLUS \
4913+ BDEV_LOOP \
b752ccd1
AM
4914+ DEBUG MAGIC_SYSRQ
4915+$(foreach i, ${AuConfAll}, \
4a4d8108
AM
4916+ $(eval $(call AuConf,CONFIG_AUFS_${i})))
4917+
4918+AuConfName = ${obj}/conf.str
4919+${AuConfName}.tmp: FORCE
4920+ @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4921+${AuConfName}: ${AuConfName}.tmp
4922+ @diff -q $< $@ > /dev/null 2>&1 || { \
4923+ echo ' GEN ' $@; \
4924+ cp -p $< $@; \
4925+ }
4926+FORCE:
4927+clean-files += ${AuConfName} ${AuConfName}.tmp
4928+${obj}/sysfs.o: ${AuConfName}
b752ccd1
AM
4929+
4930+-include ${srctree}/${src}/conf_priv.mk
7f207e10
AM
4931diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
4932--- /usr/share/empty/fs/aufs/cpup.c 1970-01-01 01:00:00.000000000 +0100
0b487229 4933+++ linux/fs/aufs/cpup.c 2021-02-22 23:30:37.699347580 +0100
e49925d1 4934@@ -0,0 +1,1448 @@
cd7a4cd9 4935+// SPDX-License-Identifier: GPL-2.0
1facf9fc 4936+/*
0b487229 4937+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 4938+ *
4939+ * This program, aufs is free software; you can redistribute it and/or modify
4940+ * it under the terms of the GNU General Public License as published by
4941+ * the Free Software Foundation; either version 2 of the License, or
4942+ * (at your option) any later version.
dece6358
AM
4943+ *
4944+ * This program is distributed in the hope that it will be useful,
4945+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
4946+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4947+ * GNU General Public License for more details.
4948+ *
4949+ * You should have received a copy of the GNU General Public License
523b37e3 4950+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 4951+ */
4952+
4953+/*
4954+ * copy-up functions, see wbr_policy.c for copy-down
4955+ */
4956+
4957+#include <linux/fs_stack.h>
dece6358 4958+#include <linux/mm.h>
8cdd5066 4959+#include <linux/task_work.h>
1facf9fc 4960+#include "aufs.h"
4961+
86dc4139 4962+void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
1facf9fc 4963+{
4964+ const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
367653fa 4965+ | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
1facf9fc 4966+
86dc4139
AM
4967+ BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
4968+
4969+ dst->i_flags |= iflags & ~mask;
1facf9fc 4970+ if (au_test_fs_notime(dst->i_sb))
4971+ dst->i_flags |= S_NOATIME | S_NOCMTIME;
4972+}
4973+
4974+void au_cpup_attr_timesizes(struct inode *inode)
4975+{
4976+ struct inode *h_inode;
4977+
5afbbe0d 4978+ h_inode = au_h_iptr(inode, au_ibtop(inode));
1facf9fc 4979+ fsstack_copy_attr_times(inode, h_inode);
4a4d8108 4980+ fsstack_copy_inode_size(inode, h_inode);
1facf9fc 4981+}
4982+
4983+void au_cpup_attr_nlink(struct inode *inode, int force)
4984+{
4985+ struct inode *h_inode;
4986+ struct super_block *sb;
5afbbe0d 4987+ aufs_bindex_t bindex, bbot;
1facf9fc 4988+
4989+ sb = inode->i_sb;
5afbbe0d 4990+ bindex = au_ibtop(inode);
1facf9fc 4991+ h_inode = au_h_iptr(inode, bindex);
4992+ if (!force
4993+ && !S_ISDIR(h_inode->i_mode)
4994+ && au_opt_test(au_mntflags(sb), PLINK)
4995+ && au_plink_test(inode))
4996+ return;
4997+
7eafdf33
AM
4998+ /*
4999+ * 0 can happen in revalidating.
38d290e6
JR
5000+ * h_inode->i_mutex may not be held here, but it is harmless since once
5001+ * i_nlink reaches 0, it will never become positive except O_TMPFILE
5002+ * case.
5003+ * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
5004+ * the incorrect link count.
7eafdf33 5005+ */
92d182d2 5006+ set_nlink(inode, h_inode->i_nlink);
1facf9fc 5007+
5008+ /*
5009+ * fewer nlink makes find(1) noisy, but larger nlink doesn't.
5010+ * it may includes whplink directory.
5011+ */
5012+ if (S_ISDIR(h_inode->i_mode)) {
5afbbe0d
AM
5013+ bbot = au_ibbot(inode);
5014+ for (bindex++; bindex <= bbot; bindex++) {
1facf9fc 5015+ h_inode = au_h_iptr(inode, bindex);
5016+ if (h_inode)
5017+ au_add_nlink(inode, h_inode);
5018+ }
5019+ }
5020+}
5021+
5022+void au_cpup_attr_changeable(struct inode *inode)
5023+{
5024+ struct inode *h_inode;
5025+
5afbbe0d 5026+ h_inode = au_h_iptr(inode, au_ibtop(inode));
1facf9fc 5027+ inode->i_mode = h_inode->i_mode;
5028+ inode->i_uid = h_inode->i_uid;
5029+ inode->i_gid = h_inode->i_gid;
5030+ au_cpup_attr_timesizes(inode);
86dc4139 5031+ au_cpup_attr_flags(inode, h_inode->i_flags);
1facf9fc 5032+}
5033+
5034+void au_cpup_igen(struct inode *inode, struct inode *h_inode)
5035+{
5036+ struct au_iinfo *iinfo = au_ii(inode);
5037+
1308ab2a 5038+ IiMustWriteLock(inode);
5039+
1facf9fc 5040+ iinfo->ii_higen = h_inode->i_generation;
5041+ iinfo->ii_hsb1 = h_inode->i_sb;
5042+}
5043+
5044+void au_cpup_attr_all(struct inode *inode, int force)
5045+{
5046+ struct inode *h_inode;
5047+
5afbbe0d 5048+ h_inode = au_h_iptr(inode, au_ibtop(inode));
1facf9fc 5049+ au_cpup_attr_changeable(inode);
5050+ if (inode->i_nlink > 0)
5051+ au_cpup_attr_nlink(inode, force);
5052+ inode->i_rdev = h_inode->i_rdev;
5053+ inode->i_blkbits = h_inode->i_blkbits;
5054+ au_cpup_igen(inode, h_inode);
5055+}
5056+
5057+/* ---------------------------------------------------------------------- */
5058+
5059+/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
5060+
5061+/* keep the timestamps of the parent dir when cpup */
5062+void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
5063+ struct path *h_path)
5064+{
5065+ struct inode *h_inode;
5066+
5067+ dt->dt_dentry = dentry;
5068+ dt->dt_h_path = *h_path;
5527c038 5069+ h_inode = d_inode(h_path->dentry);
1facf9fc 5070+ dt->dt_atime = h_inode->i_atime;
5071+ dt->dt_mtime = h_inode->i_mtime;
5072+ /* smp_mb(); */
5073+}
5074+
5075+void au_dtime_revert(struct au_dtime *dt)
5076+{
5077+ struct iattr attr;
5078+ int err;
5079+
5080+ attr.ia_atime = dt->dt_atime;
5081+ attr.ia_mtime = dt->dt_mtime;
5082+ attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
5083+ | ATTR_ATIME | ATTR_ATIME_SET;
5084+
523b37e3
AM
5085+ /* no delegation since this is a directory */
5086+ err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
1facf9fc 5087+ if (unlikely(err))
0c3ec466 5088+ pr_warn("restoring timestamps failed(%d). ignored\n", err);
1facf9fc 5089+}
5090+
5091+/* ---------------------------------------------------------------------- */
5092+
86dc4139
AM
5093+/* internal use only */
5094+struct au_cpup_reg_attr {
5095+ int valid;
5096+ struct kstat st;
5097+ unsigned int iflags; /* inode->i_flags */
5098+};
5099+
1facf9fc 5100+static noinline_for_stack
86dc4139
AM
5101+int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src,
5102+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 5103+{
c1595e42 5104+ int err, sbits, icex;
7e9cd9fe
AM
5105+ unsigned int mnt_flags;
5106+ unsigned char verbose;
1facf9fc 5107+ struct iattr ia;
5108+ struct path h_path;
1308ab2a 5109+ struct inode *h_isrc, *h_idst;
86dc4139 5110+ struct kstat *h_st;
c1595e42 5111+ struct au_branch *br;
1facf9fc 5112+
5113+ h_path.dentry = au_h_dptr(dst, bindex);
5527c038 5114+ h_idst = d_inode(h_path.dentry);
c1595e42
JR
5115+ br = au_sbr(dst->d_sb, bindex);
5116+ h_path.mnt = au_br_mnt(br);
5527c038 5117+ h_isrc = d_inode(h_src);
1308ab2a 5118+ ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
1facf9fc 5119+ | ATTR_ATIME | ATTR_MTIME
5120+ | ATTR_ATIME_SET | ATTR_MTIME_SET;
86dc4139
AM
5121+ if (h_src_attr && h_src_attr->valid) {
5122+ h_st = &h_src_attr->st;
5123+ ia.ia_uid = h_st->uid;
5124+ ia.ia_gid = h_st->gid;
5125+ ia.ia_atime = h_st->atime;
5126+ ia.ia_mtime = h_st->mtime;
5127+ if (h_idst->i_mode != h_st->mode
5128+ && !S_ISLNK(h_idst->i_mode)) {
5129+ ia.ia_valid |= ATTR_MODE;
5130+ ia.ia_mode = h_st->mode;
5131+ }
5132+ sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
5133+ au_cpup_attr_flags(h_idst, h_src_attr->iflags);
5134+ } else {
5135+ ia.ia_uid = h_isrc->i_uid;
5136+ ia.ia_gid = h_isrc->i_gid;
5137+ ia.ia_atime = h_isrc->i_atime;
5138+ ia.ia_mtime = h_isrc->i_mtime;
5139+ if (h_idst->i_mode != h_isrc->i_mode
5140+ && !S_ISLNK(h_idst->i_mode)) {
5141+ ia.ia_valid |= ATTR_MODE;
5142+ ia.ia_mode = h_isrc->i_mode;
5143+ }
5144+ sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
5145+ au_cpup_attr_flags(h_idst, h_isrc->i_flags);
1308ab2a 5146+ }
523b37e3
AM
5147+ /* no delegation since it is just created */
5148+ err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
1facf9fc 5149+
5150+ /* is this nfs only? */
5151+ if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
5152+ ia.ia_valid = ATTR_FORCE | ATTR_MODE;
5153+ ia.ia_mode = h_isrc->i_mode;
523b37e3 5154+ err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
1facf9fc 5155+ }
5156+
c1595e42 5157+ icex = br->br_perm & AuBrAttr_ICEX;
7e9cd9fe
AM
5158+ if (!err) {
5159+ mnt_flags = au_mntflags(dst->d_sb);
5160+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
5161+ err = au_cpup_xattr(h_path.dentry, h_src, icex, verbose);
5162+ }
c1595e42 5163+
1facf9fc 5164+ return err;
5165+}
5166+
5167+/* ---------------------------------------------------------------------- */
5168+
5169+static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
5170+ char *buf, unsigned long blksize)
5171+{
5172+ int err;
5173+ size_t sz, rbytes, wbytes;
5174+ unsigned char all_zero;
5175+ char *p, *zp;
febd17d6 5176+ struct inode *h_inode;
1facf9fc 5177+ /* reduce stack usage */
5178+ struct iattr *ia;
5179+
5180+ zp = page_address(ZERO_PAGE(0));
5181+ if (unlikely(!zp))
5182+ return -ENOMEM; /* possible? */
5183+
5184+ err = 0;
5185+ all_zero = 0;
5186+ while (len) {
5187+ AuDbg("len %lld\n", len);
5188+ sz = blksize;
5189+ if (len < blksize)
5190+ sz = len;
5191+
5192+ rbytes = 0;
5193+ /* todo: signal_pending? */
5194+ while (!rbytes || err == -EAGAIN || err == -EINTR) {
5195+ rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
5196+ err = rbytes;
5197+ }
5198+ if (unlikely(err < 0))
5199+ break;
5200+
5201+ all_zero = 0;
5202+ if (len >= rbytes && rbytes == blksize)
5203+ all_zero = !memcmp(buf, zp, rbytes);
5204+ if (!all_zero) {
5205+ wbytes = rbytes;
5206+ p = buf;
5207+ while (wbytes) {
5208+ size_t b;
5209+
5210+ b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
5211+ err = b;
5212+ /* todo: signal_pending? */
5213+ if (unlikely(err == -EAGAIN || err == -EINTR))
5214+ continue;
5215+ if (unlikely(err < 0))
5216+ break;
5217+ wbytes -= b;
5218+ p += b;
5219+ }
392086de
AM
5220+ if (unlikely(err < 0))
5221+ break;
1facf9fc 5222+ } else {
5223+ loff_t res;
5224+
5225+ AuLabel(hole);
5226+ res = vfsub_llseek(dst, rbytes, SEEK_CUR);
5227+ err = res;
5228+ if (unlikely(res < 0))
5229+ break;
5230+ }
5231+ len -= rbytes;
5232+ err = 0;
5233+ }
5234+
5235+ /* the last block may be a hole */
5236+ if (!err && all_zero) {
5237+ AuLabel(last hole);
5238+
5239+ err = 1;
2000de60 5240+ if (au_test_nfs(dst->f_path.dentry->d_sb)) {
1facf9fc 5241+ /* nfs requires this step to make last hole */
5242+ /* is this only nfs? */
5243+ do {
5244+ /* todo: signal_pending? */
5245+ err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
5246+ } while (err == -EAGAIN || err == -EINTR);
5247+ if (err == 1)
5248+ dst->f_pos--;
5249+ }
5250+
5251+ if (err == 1) {
5252+ ia = (void *)buf;
5253+ ia->ia_size = dst->f_pos;
5254+ ia->ia_valid = ATTR_SIZE | ATTR_FILE;
5255+ ia->ia_file = dst;
febd17d6
JR
5256+ h_inode = file_inode(dst);
5257+ inode_lock_nested(h_inode, AuLsc_I_CHILD2);
523b37e3
AM
5258+ /* no delegation since it is just created */
5259+ err = vfsub_notify_change(&dst->f_path, ia,
5260+ /*delegated*/NULL);
febd17d6 5261+ inode_unlock(h_inode);
1facf9fc 5262+ }
5263+ }
5264+
5265+ return err;
5266+}
5267+
5268+int au_copy_file(struct file *dst, struct file *src, loff_t len)
5269+{
5270+ int err;
5271+ unsigned long blksize;
5272+ unsigned char do_kfree;
5273+ char *buf;
e49925d1 5274+ struct super_block *h_sb;
1facf9fc 5275+
5276+ err = -ENOMEM;
e49925d1
JR
5277+ h_sb = file_inode(dst)->i_sb;
5278+ blksize = h_sb->s_blocksize;
1facf9fc 5279+ if (!blksize || PAGE_SIZE < blksize)
5280+ blksize = PAGE_SIZE;
5281+ AuDbg("blksize %lu\n", blksize);
5282+ do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
5283+ if (do_kfree)
5284+ buf = kmalloc(blksize, GFP_NOFS);
5285+ else
5286+ buf = (void *)__get_free_page(GFP_NOFS);
5287+ if (unlikely(!buf))
5288+ goto out;
5289+
5290+ if (len > (1 << 22))
5291+ AuDbg("copying a large file %lld\n", (long long)len);
5292+
5293+ src->f_pos = 0;
5294+ dst->f_pos = 0;
5295+ err = au_do_copy_file(dst, src, len, buf, blksize);
e49925d1
JR
5296+ if (do_kfree) {
5297+ AuDebugOn(!au_kfree_do_sz_test(blksize));
5298+ au_kfree_do_rcu(buf);
5299+ } else
1c60b727 5300+ free_page((unsigned long)buf);
1facf9fc 5301+
4f0767ce 5302+out:
1facf9fc 5303+ return err;
5304+}
5305+
1c60b727
AM
5306+static int au_do_copy(struct file *dst, struct file *src, loff_t len)
5307+{
5308+ int err;
5309+ struct super_block *h_src_sb;
5310+ struct inode *h_src_inode;
5311+
5312+ h_src_inode = file_inode(src);
5313+ h_src_sb = h_src_inode->i_sb;
5314+
5315+ /* XFS acquires inode_lock */
5316+ if (!au_test_xfs(h_src_sb))
5317+ err = au_copy_file(dst, src, len);
5318+ else {
3c1bdaff 5319+ inode_unlock_shared(h_src_inode);
1c60b727 5320+ err = au_copy_file(dst, src, len);
be118d29 5321+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
1c60b727
AM
5322+ }
5323+
5324+ return err;
5325+}
5326+
5327+static int au_clone_or_copy(struct file *dst, struct file *src, loff_t len)
5328+{
5329+ int err;
5330+ struct super_block *h_src_sb;
5331+ struct inode *h_src_inode;
5332+
5333+ h_src_inode = file_inode(src);
5334+ h_src_sb = h_src_inode->i_sb;
5335+ if (h_src_sb != file_inode(dst)->i_sb
5336+ || !dst->f_op->clone_file_range) {
5337+ err = au_do_copy(dst, src, len);
5338+ goto out;
5339+ }
5340+
5341+ if (!au_test_nfs(h_src_sb)) {
3c1bdaff 5342+ inode_unlock_shared(h_src_inode);
1c60b727 5343+ err = vfsub_clone_file_range(src, dst, len);
be118d29 5344+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
1c60b727
AM
5345+ } else
5346+ err = vfsub_clone_file_range(src, dst, len);
5347+ /* older XFS has a condition in cloning */
5348+ if (unlikely(err != -EOPNOTSUPP))
5349+ goto out;
5350+
5351+ /* the backend fs on NFS may not support cloning */
5352+ err = au_do_copy(dst, src, len);
5353+
5354+out:
5355+ AuTraceErr(err);
5356+ return err;
5357+}
5358+
1facf9fc 5359+/*
5360+ * to support a sparse file which is opened with O_APPEND,
5361+ * we need to close the file.
5362+ */
c2b27bf2 5363+static int au_cp_regular(struct au_cp_generic *cpg)
1facf9fc 5364+{
5365+ int err, i;
5366+ enum { SRC, DST };
5367+ struct {
5368+ aufs_bindex_t bindex;
5369+ unsigned int flags;
5370+ struct dentry *dentry;
392086de 5371+ int force_wr;
1facf9fc 5372+ struct file *file;
1facf9fc 5373+ } *f, file[] = {
5374+ {
c2b27bf2 5375+ .bindex = cpg->bsrc,
1facf9fc 5376+ .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
1facf9fc 5377+ },
5378+ {
c2b27bf2 5379+ .bindex = cpg->bdst,
1facf9fc 5380+ .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
392086de 5381+ .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
1facf9fc 5382+ }
5383+ };
acd2b654 5384+ struct au_branch *br;
521ced18 5385+ struct super_block *sb, *h_src_sb;
e2f27e51 5386+ struct inode *h_src_inode;
8cdd5066 5387+ struct task_struct *tsk = current;
1facf9fc 5388+
5389+ /* bsrc branch can be ro/rw. */
c2b27bf2 5390+ sb = cpg->dentry->d_sb;
1facf9fc 5391+ f = file;
5392+ for (i = 0; i < 2; i++, f++) {
c2b27bf2
AM
5393+ f->dentry = au_h_dptr(cpg->dentry, f->bindex);
5394+ f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
392086de 5395+ /*file*/NULL, f->force_wr);
e49925d1
JR
5396+ if (IS_ERR(f->file)) {
5397+ err = PTR_ERR(f->file);
5398+ if (i == SRC)
5399+ goto out;
5400+ else
5401+ goto out_src;
5402+ }
1facf9fc 5403+ }
5404+
5405+ /* try stopping to update while we copyup */
e2f27e51 5406+ h_src_inode = d_inode(file[SRC].dentry);
521ced18
JR
5407+ h_src_sb = h_src_inode->i_sb;
5408+ if (!au_test_nfs(h_src_sb))
e2f27e51 5409+ IMustLock(h_src_inode);
1c60b727 5410+ err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
1facf9fc 5411+
8cdd5066
JR
5412+ /* i wonder if we had O_NO_DELAY_FPUT flag */
5413+ if (tsk->flags & PF_KTHREAD)
5414+ __fput_sync(file[DST].file);
5415+ else {
062440b3 5416+ /* it happened actually */
8cdd5066
JR
5417+ fput(file[DST].file);
5418+ /*
5419+ * too bad.
5420+ * we have to call both since we don't know which place the file
5421+ * was added to.
5422+ */
5423+ task_work_run();
5424+ flush_delayed_fput();
5425+ }
acd2b654
AM
5426+ br = au_sbr(sb, file[DST].bindex);
5427+ au_lcnt_dec(&br->br_nfiles);
523b37e3 5428+
4f0767ce 5429+out_src:
1facf9fc 5430+ fput(file[SRC].file);
acd2b654
AM
5431+ br = au_sbr(sb, file[SRC].bindex);
5432+ au_lcnt_dec(&br->br_nfiles);
4f0767ce 5433+out:
1facf9fc 5434+ return err;
5435+}
5436+
c2b27bf2 5437+static int au_do_cpup_regular(struct au_cp_generic *cpg,
86dc4139 5438+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 5439+{
5440+ int err, rerr;
5441+ loff_t l;
86dc4139 5442+ struct path h_path;
38d290e6 5443+ struct inode *h_src_inode, *h_dst_inode;
1facf9fc 5444+
5445+ err = 0;
5527c038 5446+ h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
86dc4139 5447+ l = i_size_read(h_src_inode);
c2b27bf2
AM
5448+ if (cpg->len == -1 || l < cpg->len)
5449+ cpg->len = l;
5450+ if (cpg->len) {
86dc4139 5451+ /* try stopping to update while we are referencing */
be118d29 5452+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
c2b27bf2 5453+ au_pin_hdir_unlock(cpg->pin);
1facf9fc 5454+
c2b27bf2
AM
5455+ h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5456+ h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
86dc4139 5457+ h_src_attr->iflags = h_src_inode->i_flags;
5527c038 5458+ if (!au_test_nfs(h_src_inode->i_sb))
521ced18 5459+ err = vfsub_getattr(&h_path, &h_src_attr->st);
5527c038 5460+ else {
3c1bdaff 5461+ inode_unlock_shared(h_src_inode);
521ced18 5462+ err = vfsub_getattr(&h_path, &h_src_attr->st);
be118d29 5463+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5527c038 5464+ }
86dc4139 5465+ if (unlikely(err)) {
3c1bdaff 5466+ inode_unlock_shared(h_src_inode);
86dc4139
AM
5467+ goto out;
5468+ }
5469+ h_src_attr->valid = 1;
e2f27e51
AM
5470+ if (!au_test_nfs(h_src_inode->i_sb)) {
5471+ err = au_cp_regular(cpg);
3c1bdaff 5472+ inode_unlock_shared(h_src_inode);
e2f27e51 5473+ } else {
3c1bdaff 5474+ inode_unlock_shared(h_src_inode);
e2f27e51
AM
5475+ err = au_cp_regular(cpg);
5476+ }
c2b27bf2 5477+ rerr = au_pin_hdir_relock(cpg->pin);
86dc4139
AM
5478+ if (!err && rerr)
5479+ err = rerr;
1facf9fc 5480+ }
38d290e6
JR
5481+ if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5482+ h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5527c038 5483+ h_dst_inode = d_inode(h_path.dentry);
38d290e6
JR
5484+ spin_lock(&h_dst_inode->i_lock);
5485+ h_dst_inode->i_state |= I_LINKABLE;
5486+ spin_unlock(&h_dst_inode->i_lock);
5487+ }
1facf9fc 5488+
4f0767ce 5489+out:
1facf9fc 5490+ return err;
5491+}
5492+
5493+static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5494+ struct inode *h_dir)
5495+{
5496+ int err, symlen;
5497+ mm_segment_t old_fs;
b752ccd1
AM
5498+ union {
5499+ char *k;
5500+ char __user *u;
5501+ } sym;
1facf9fc 5502+
5503+ err = -ENOMEM;
537831f9 5504+ sym.k = (void *)__get_free_page(GFP_NOFS);
b752ccd1 5505+ if (unlikely(!sym.k))
1facf9fc 5506+ goto out;
5507+
9dbd164d 5508+ /* unnecessary to support mmap_sem since symlink is not mmap-able */
1facf9fc 5509+ old_fs = get_fs();
5510+ set_fs(KERNEL_DS);
a2654f78 5511+ symlen = vfs_readlink(h_src, sym.u, PATH_MAX);
1facf9fc 5512+ err = symlen;
5513+ set_fs(old_fs);
5514+
5515+ if (symlen > 0) {
b752ccd1
AM
5516+ sym.k[symlen] = 0;
5517+ err = vfsub_symlink(h_dir, h_path, sym.k);
1facf9fc 5518+ }
1c60b727 5519+ free_page((unsigned long)sym.k);
1facf9fc 5520+
4f0767ce 5521+out:
1facf9fc 5522+ return err;
5523+}
5524+
8cdd5066
JR
5525+/*
5526+ * regardless 'acl' option, reset all ACL.
5527+ * All ACL will be copied up later from the original entry on the lower branch.
5528+ */
5529+static int au_reset_acl(struct inode *h_dir, struct path *h_path, umode_t mode)
5530+{
5531+ int err;
5532+ struct dentry *h_dentry;
5533+ struct inode *h_inode;
5534+
5535+ h_dentry = h_path->dentry;
5536+ h_inode = d_inode(h_dentry);
5537+ /* forget_all_cached_acls(h_inode)); */
5538+ err = vfsub_removexattr(h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
5539+ AuTraceErr(err);
5540+ if (err == -EOPNOTSUPP)
5541+ err = 0;
5542+ if (!err)
5543+ err = vfsub_acl_chmod(h_inode, mode);
5544+
5545+ AuTraceErr(err);
5546+ return err;
5547+}
5548+
5549+static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5550+ struct inode *h_dir, struct path *h_path)
5551+{
5552+ int err;
5553+ struct inode *dir, *inode;
5554+
5555+ err = vfsub_removexattr(h_path->dentry, XATTR_NAME_POSIX_ACL_DEFAULT);
5556+ AuTraceErr(err);
5557+ if (err == -EOPNOTSUPP)
5558+ err = 0;
5559+ if (unlikely(err))
5560+ goto out;
5561+
5562+ /*
5563+ * strange behaviour from the users view,
acd2b654 5564+ * particularly setattr case
8cdd5066
JR
5565+ */
5566+ dir = d_inode(dst_parent);
5afbbe0d 5567+ if (au_ibtop(dir) == cpg->bdst)
8cdd5066
JR
5568+ au_cpup_attr_nlink(dir, /*force*/1);
5569+ inode = d_inode(cpg->dentry);
5570+ au_cpup_attr_nlink(inode, /*force*/1);
5571+
5572+out:
5573+ return err;
5574+}
5575+
1facf9fc 5576+static noinline_for_stack
c2b27bf2 5577+int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
86dc4139 5578+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 5579+{
5580+ int err;
5581+ umode_t mode;
5582+ unsigned int mnt_flags;
076b876e 5583+ unsigned char isdir, isreg, force;
c2b27bf2 5584+ const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
1facf9fc 5585+ struct au_dtime dt;
5586+ struct path h_path;
5587+ struct dentry *h_src, *h_dst, *h_parent;
8cdd5066 5588+ struct inode *h_inode, *h_dir;
1facf9fc 5589+ struct super_block *sb;
5590+
5591+ /* bsrc branch can be ro/rw. */
c2b27bf2 5592+ h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038
JR
5593+ h_inode = d_inode(h_src);
5594+ AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
1facf9fc 5595+
5596+ /* try stopping to be referenced while we are creating */
c2b27bf2
AM
5597+ h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5598+ if (au_ftest_cpup(cpg->flags, RENAME))
86dc4139
AM
5599+ AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5600+ AUFS_WH_PFX_LEN));
1facf9fc 5601+ h_parent = h_dst->d_parent; /* dir inode is locked */
5527c038 5602+ h_dir = d_inode(h_parent);
1facf9fc 5603+ IMustLock(h_dir);
5604+ AuDebugOn(h_parent != h_dst->d_parent);
5605+
c2b27bf2
AM
5606+ sb = cpg->dentry->d_sb;
5607+ h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
1facf9fc 5608+ if (do_dt) {
5609+ h_path.dentry = h_parent;
5610+ au_dtime_store(&dt, dst_parent, &h_path);
5611+ }
5612+ h_path.dentry = h_dst;
5613+
076b876e 5614+ isreg = 0;
1facf9fc 5615+ isdir = 0;
5616+ mode = h_inode->i_mode;
5617+ switch (mode & S_IFMT) {
5618+ case S_IFREG:
076b876e 5619+ isreg = 1;
cd7a4cd9 5620+ err = vfsub_create(h_dir, &h_path, 0600, /*want_excl*/true);
1facf9fc 5621+ if (!err)
c2b27bf2 5622+ err = au_do_cpup_regular(cpg, h_src_attr);
1facf9fc 5623+ break;
5624+ case S_IFDIR:
5625+ isdir = 1;
5626+ err = vfsub_mkdir(h_dir, &h_path, mode);
8cdd5066
JR
5627+ if (!err)
5628+ err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
1facf9fc 5629+ break;
5630+ case S_IFLNK:
5631+ err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5632+ break;
5633+ case S_IFCHR:
5634+ case S_IFBLK:
5635+ AuDebugOn(!capable(CAP_MKNOD));
5636+ /*FALLTHROUGH*/
5637+ case S_IFIFO:
5638+ case S_IFSOCK:
5639+ err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5640+ break;
5641+ default:
5642+ AuIOErr("Unknown inode type 0%o\n", mode);
5643+ err = -EIO;
5644+ }
8cdd5066
JR
5645+ if (!err)
5646+ err = au_reset_acl(h_dir, &h_path, mode);
1facf9fc 5647+
5648+ mnt_flags = au_mntflags(sb);
5649+ if (!au_opt_test(mnt_flags, UDBA_NONE)
5650+ && !isdir
5651+ && au_opt_test(mnt_flags, XINO)
38d290e6
JR
5652+ && (h_inode->i_nlink == 1
5653+ || (h_inode->i_state & I_LINKABLE))
1facf9fc 5654+ /* todo: unnecessary? */
5527c038 5655+ /* && d_inode(cpg->dentry)->i_nlink == 1 */
c2b27bf2
AM
5656+ && cpg->bdst < cpg->bsrc
5657+ && !au_ftest_cpup(cpg->flags, KEEPLINO))
5658+ au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
1facf9fc 5659+ /* ignore this error */
5660+
076b876e
AM
5661+ if (!err) {
5662+ force = 0;
5663+ if (isreg) {
5664+ force = !!cpg->len;
5665+ if (cpg->len == -1)
5666+ force = !!i_size_read(h_inode);
5667+ }
5668+ au_fhsm_wrote(sb, cpg->bdst, force);
5669+ }
5670+
1facf9fc 5671+ if (do_dt)
5672+ au_dtime_revert(&dt);
5673+ return err;
5674+}
5675+
392086de 5676+static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
86dc4139
AM
5677+{
5678+ int err;
392086de 5679+ struct dentry *dentry, *h_dentry, *h_parent, *parent;
86dc4139 5680+ struct inode *h_dir;
392086de 5681+ aufs_bindex_t bdst;
86dc4139 5682+
392086de
AM
5683+ dentry = cpg->dentry;
5684+ bdst = cpg->bdst;
5685+ h_dentry = au_h_dptr(dentry, bdst);
5686+ if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5687+ dget(h_dentry);
5688+ au_set_h_dptr(dentry, bdst, NULL);
5689+ err = au_lkup_neg(dentry, bdst, /*wh*/0);
5690+ if (!err)
5691+ h_path->dentry = dget(au_h_dptr(dentry, bdst));
86dc4139 5692+ au_set_h_dptr(dentry, bdst, h_dentry);
392086de
AM
5693+ } else {
5694+ err = 0;
5695+ parent = dget_parent(dentry);
5696+ h_parent = au_h_dptr(parent, bdst);
5697+ dput(parent);
5698+ h_path->dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
5699+ if (IS_ERR(h_path->dentry))
5700+ err = PTR_ERR(h_path->dentry);
86dc4139 5701+ }
392086de
AM
5702+ if (unlikely(err))
5703+ goto out;
86dc4139 5704+
86dc4139 5705+ h_parent = h_dentry->d_parent; /* dir inode is locked */
5527c038 5706+ h_dir = d_inode(h_parent);
86dc4139 5707+ IMustLock(h_dir);
523b37e3
AM
5708+ AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5709+ /* no delegation since it is just created */
f2c43d5f
AM
5710+ err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
5711+ /*flags*/0);
86dc4139
AM
5712+ dput(h_path->dentry);
5713+
5714+out:
5715+ return err;
5716+}
5717+
1facf9fc 5718+/*
5719+ * copyup the @dentry from @bsrc to @bdst.
5720+ * the caller must set the both of lower dentries.
5721+ * @len is for truncating when it is -1 copyup the entire file.
5722+ * in link/rename cases, @dst_parent may be different from the real one.
c2b27bf2 5723+ * basic->bsrc can be larger than basic->bdst.
f2c43d5f 5724+ * aufs doesn't touch the credential so
acd2b654 5725+ * security_inode_copy_up{,_xattr}() are unnecessary.
1facf9fc 5726+ */
c2b27bf2 5727+static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
1facf9fc 5728+{
5729+ int err, rerr;
5afbbe0d 5730+ aufs_bindex_t old_ibtop;
1facf9fc 5731+ unsigned char isdir, plink;
1facf9fc 5732+ struct dentry *h_src, *h_dst, *h_parent;
5527c038 5733+ struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
1facf9fc 5734+ struct super_block *sb;
86dc4139 5735+ struct au_branch *br;
acd2b654 5736+ /* to reduce stack size */
c2b27bf2
AM
5737+ struct {
5738+ struct au_dtime dt;
5739+ struct path h_path;
5740+ struct au_cpup_reg_attr h_src_attr;
5741+ } *a;
1facf9fc 5742+
c2b27bf2
AM
5743+ err = -ENOMEM;
5744+ a = kmalloc(sizeof(*a), GFP_NOFS);
5745+ if (unlikely(!a))
5746+ goto out;
5747+ a->h_src_attr.valid = 0;
1facf9fc 5748+
c2b27bf2
AM
5749+ sb = cpg->dentry->d_sb;
5750+ br = au_sbr(sb, cpg->bdst);
5751+ a->h_path.mnt = au_br_mnt(br);
5752+ h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
1facf9fc 5753+ h_parent = h_dst->d_parent; /* dir inode is locked */
5527c038 5754+ h_dir = d_inode(h_parent);
1facf9fc 5755+ IMustLock(h_dir);
5756+
c2b27bf2 5757+ h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038 5758+ inode = d_inode(cpg->dentry);
1facf9fc 5759+
5760+ if (!dst_parent)
c2b27bf2 5761+ dst_parent = dget_parent(cpg->dentry);
1facf9fc 5762+ else
5763+ dget(dst_parent);
5764+
5765+ plink = !!au_opt_test(au_mntflags(sb), PLINK);
c2b27bf2 5766+ dst_inode = au_h_iptr(inode, cpg->bdst);
1facf9fc 5767+ if (dst_inode) {
5768+ if (unlikely(!plink)) {
5769+ err = -EIO;
027c5e7a
AM
5770+ AuIOErr("hi%lu(i%lu) exists on b%d "
5771+ "but plink is disabled\n",
c2b27bf2
AM
5772+ dst_inode->i_ino, inode->i_ino, cpg->bdst);
5773+ goto out_parent;
1facf9fc 5774+ }
5775+
5776+ if (dst_inode->i_nlink) {
c2b27bf2 5777+ const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
1facf9fc 5778+
c2b27bf2 5779+ h_src = au_plink_lkup(inode, cpg->bdst);
1facf9fc 5780+ err = PTR_ERR(h_src);
5781+ if (IS_ERR(h_src))
c2b27bf2 5782+ goto out_parent;
5527c038 5783+ if (unlikely(d_is_negative(h_src))) {
1facf9fc 5784+ err = -EIO;
79b8bda9 5785+ AuIOErr("i%lu exists on b%d "
027c5e7a 5786+ "but not pseudo-linked\n",
79b8bda9 5787+ inode->i_ino, cpg->bdst);
1facf9fc 5788+ dput(h_src);
c2b27bf2 5789+ goto out_parent;
1facf9fc 5790+ }
5791+
5792+ if (do_dt) {
c2b27bf2
AM
5793+ a->h_path.dentry = h_parent;
5794+ au_dtime_store(&a->dt, dst_parent, &a->h_path);
1facf9fc 5795+ }
86dc4139 5796+
c2b27bf2 5797+ a->h_path.dentry = h_dst;
523b37e3
AM
5798+ delegated = NULL;
5799+ err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
c2b27bf2 5800+ if (!err && au_ftest_cpup(cpg->flags, RENAME))
392086de 5801+ err = au_do_ren_after_cpup(cpg, &a->h_path);
1facf9fc 5802+ if (do_dt)
c2b27bf2 5803+ au_dtime_revert(&a->dt);
523b37e3
AM
5804+ if (unlikely(err == -EWOULDBLOCK)) {
5805+ pr_warn("cannot retry for NFSv4 delegation"
5806+ " for an internal link\n");
5807+ iput(delegated);
5808+ }
1facf9fc 5809+ dput(h_src);
c2b27bf2 5810+ goto out_parent;
1facf9fc 5811+ } else
5812+ /* todo: cpup_wh_file? */
5813+ /* udba work */
4a4d8108 5814+ au_update_ibrange(inode, /*do_put_zero*/1);
1facf9fc 5815+ }
5816+
86dc4139 5817+ isdir = S_ISDIR(inode->i_mode);
5afbbe0d 5818+ old_ibtop = au_ibtop(inode);
c2b27bf2 5819+ err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
1facf9fc 5820+ if (unlikely(err))
86dc4139 5821+ goto out_rev;
5527c038 5822+ dst_inode = d_inode(h_dst);
febd17d6 5823+ inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
86dc4139 5824+ /* todo: necessary? */
c2b27bf2 5825+ /* au_pin_hdir_unlock(cpg->pin); */
1facf9fc 5826+
c2b27bf2 5827+ err = cpup_iattr(cpg->dentry, cpg->bdst, h_src, &a->h_src_attr);
86dc4139
AM
5828+ if (unlikely(err)) {
5829+ /* todo: necessary? */
c2b27bf2 5830+ /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
febd17d6 5831+ inode_unlock(dst_inode);
86dc4139
AM
5832+ goto out_rev;
5833+ }
5834+
5afbbe0d 5835+ if (cpg->bdst < old_ibtop) {
86dc4139 5836+ if (S_ISREG(inode->i_mode)) {
c2b27bf2 5837+ err = au_dy_iaop(inode, cpg->bdst, dst_inode);
86dc4139 5838+ if (unlikely(err)) {
c2b27bf2
AM
5839+ /* ignore an error */
5840+ /* au_pin_hdir_relock(cpg->pin); */
febd17d6 5841+ inode_unlock(dst_inode);
86dc4139 5842+ goto out_rev;
4a4d8108 5843+ }
4a4d8108 5844+ }
5afbbe0d 5845+ au_set_ibtop(inode, cpg->bdst);
c2b27bf2 5846+ } else
5afbbe0d 5847+ au_set_ibbot(inode, cpg->bdst);
c2b27bf2 5848+ au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
86dc4139
AM
5849+ au_hi_flags(inode, isdir));
5850+
5851+ /* todo: necessary? */
c2b27bf2 5852+ /* err = au_pin_hdir_relock(cpg->pin); */
febd17d6 5853+ inode_unlock(dst_inode);
86dc4139
AM
5854+ if (unlikely(err))
5855+ goto out_rev;
5856+
5527c038 5857+ src_inode = d_inode(h_src);
86dc4139 5858+ if (!isdir
5527c038
JR
5859+ && (src_inode->i_nlink > 1
5860+ || src_inode->i_state & I_LINKABLE)
86dc4139 5861+ && plink)
c2b27bf2 5862+ au_plink_append(inode, cpg->bdst, h_dst);
86dc4139 5863+
c2b27bf2
AM
5864+ if (au_ftest_cpup(cpg->flags, RENAME)) {
5865+ a->h_path.dentry = h_dst;
392086de 5866+ err = au_do_ren_after_cpup(cpg, &a->h_path);
86dc4139
AM
5867+ }
5868+ if (!err)
c2b27bf2 5869+ goto out_parent; /* success */
1facf9fc 5870+
5871+ /* revert */
4a4d8108 5872+out_rev:
c2b27bf2
AM
5873+ a->h_path.dentry = h_parent;
5874+ au_dtime_store(&a->dt, dst_parent, &a->h_path);
5875+ a->h_path.dentry = h_dst;
86dc4139 5876+ rerr = 0;
5527c038 5877+ if (d_is_positive(h_dst)) {
523b37e3
AM
5878+ if (!isdir) {
5879+ /* no delegation since it is just created */
5880+ rerr = vfsub_unlink(h_dir, &a->h_path,
5881+ /*delegated*/NULL, /*force*/0);
5882+ } else
c2b27bf2 5883+ rerr = vfsub_rmdir(h_dir, &a->h_path);
86dc4139 5884+ }
c2b27bf2 5885+ au_dtime_revert(&a->dt);
1facf9fc 5886+ if (rerr) {
5887+ AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5888+ err = -EIO;
5889+ }
c2b27bf2 5890+out_parent:
1facf9fc 5891+ dput(dst_parent);
e49925d1 5892+ au_kfree_rcu(a);
c2b27bf2 5893+out:
1facf9fc 5894+ return err;
5895+}
5896+
7e9cd9fe 5897+#if 0 /* reserved */
1facf9fc 5898+struct au_cpup_single_args {
5899+ int *errp;
c2b27bf2 5900+ struct au_cp_generic *cpg;
1facf9fc 5901+ struct dentry *dst_parent;
5902+};
5903+
5904+static void au_call_cpup_single(void *args)
5905+{
5906+ struct au_cpup_single_args *a = args;
86dc4139 5907+
c2b27bf2
AM
5908+ au_pin_hdir_acquire_nest(a->cpg->pin);
5909+ *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5910+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 5911+}
c2b27bf2 5912+#endif
1facf9fc 5913+
53392da6
AM
5914+/*
5915+ * prevent SIGXFSZ in copy-up.
5916+ * testing CAP_MKNOD is for generic fs,
5917+ * but CAP_FSETID is for xfs only, currently.
5918+ */
86dc4139 5919+static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
53392da6
AM
5920+{
5921+ int do_sio;
86dc4139
AM
5922+ struct super_block *sb;
5923+ struct inode *h_dir;
53392da6
AM
5924+
5925+ do_sio = 0;
86dc4139 5926+ sb = au_pinned_parent(pin)->d_sb;
53392da6
AM
5927+ if (!au_wkq_test()
5928+ && (!au_sbi(sb)->si_plink_maint_pid
5929+ || au_plink_maint(sb, AuLock_NOPLM))) {
5930+ switch (mode & S_IFMT) {
5931+ case S_IFREG:
5932+ /* no condition about RLIMIT_FSIZE and the file size */
5933+ do_sio = 1;
5934+ break;
5935+ case S_IFCHR:
5936+ case S_IFBLK:
5937+ do_sio = !capable(CAP_MKNOD);
5938+ break;
5939+ }
5940+ if (!do_sio)
5941+ do_sio = ((mode & (S_ISUID | S_ISGID))
5942+ && !capable(CAP_FSETID));
86dc4139
AM
5943+ /* this workaround may be removed in the future */
5944+ if (!do_sio) {
5945+ h_dir = au_pinned_h_dir(pin);
5946+ do_sio = h_dir->i_mode & S_ISVTX;
5947+ }
53392da6
AM
5948+ }
5949+
5950+ return do_sio;
5951+}
5952+
7e9cd9fe 5953+#if 0 /* reserved */
c2b27bf2 5954+int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
1facf9fc 5955+{
5956+ int err, wkq_err;
1facf9fc 5957+ struct dentry *h_dentry;
5958+
c2b27bf2 5959+ h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038 5960+ if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
c2b27bf2 5961+ err = au_cpup_single(cpg, dst_parent);
1facf9fc 5962+ else {
5963+ struct au_cpup_single_args args = {
5964+ .errp = &err,
c2b27bf2
AM
5965+ .cpg = cpg,
5966+ .dst_parent = dst_parent
1facf9fc 5967+ };
5968+ wkq_err = au_wkq_wait(au_call_cpup_single, &args);
5969+ if (unlikely(wkq_err))
5970+ err = wkq_err;
5971+ }
5972+
5973+ return err;
5974+}
c2b27bf2 5975+#endif
1facf9fc 5976+
5977+/*
5978+ * copyup the @dentry from the first active lower branch to @bdst,
5979+ * using au_cpup_single().
5980+ */
c2b27bf2 5981+static int au_cpup_simple(struct au_cp_generic *cpg)
1facf9fc 5982+{
5983+ int err;
c2b27bf2
AM
5984+ unsigned int flags_orig;
5985+ struct dentry *dentry;
5986+
5987+ AuDebugOn(cpg->bsrc < 0);
1facf9fc 5988+
c2b27bf2 5989+ dentry = cpg->dentry;
86dc4139 5990+ DiMustWriteLock(dentry);
1facf9fc 5991+
c2b27bf2 5992+ err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
1facf9fc 5993+ if (!err) {
c2b27bf2
AM
5994+ flags_orig = cpg->flags;
5995+ au_fset_cpup(cpg->flags, RENAME);
5996+ err = au_cpup_single(cpg, NULL);
5997+ cpg->flags = flags_orig;
1facf9fc 5998+ if (!err)
5999+ return 0; /* success */
6000+
6001+ /* revert */
c2b27bf2 6002+ au_set_h_dptr(dentry, cpg->bdst, NULL);
5afbbe0d 6003+ au_set_dbtop(dentry, cpg->bsrc);
1facf9fc 6004+ }
6005+
6006+ return err;
6007+}
6008+
6009+struct au_cpup_simple_args {
6010+ int *errp;
c2b27bf2 6011+ struct au_cp_generic *cpg;
1facf9fc 6012+};
6013+
6014+static void au_call_cpup_simple(void *args)
6015+{
6016+ struct au_cpup_simple_args *a = args;
86dc4139 6017+
c2b27bf2
AM
6018+ au_pin_hdir_acquire_nest(a->cpg->pin);
6019+ *a->errp = au_cpup_simple(a->cpg);
6020+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 6021+}
6022+
c2b27bf2 6023+static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
1facf9fc 6024+{
6025+ int err, wkq_err;
c2b27bf2
AM
6026+ struct dentry *dentry, *parent;
6027+ struct file *h_file;
1facf9fc 6028+ struct inode *h_dir;
6029+
c2b27bf2
AM
6030+ dentry = cpg->dentry;
6031+ h_file = NULL;
6032+ if (au_ftest_cpup(cpg->flags, HOPEN)) {
6033+ AuDebugOn(cpg->bsrc < 0);
392086de 6034+ h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
c2b27bf2
AM
6035+ err = PTR_ERR(h_file);
6036+ if (IS_ERR(h_file))
6037+ goto out;
6038+ }
6039+
1facf9fc 6040+ parent = dget_parent(dentry);
5527c038 6041+ h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
53392da6 6042+ if (!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE)
5527c038 6043+ && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
c2b27bf2 6044+ err = au_cpup_simple(cpg);
1facf9fc 6045+ else {
6046+ struct au_cpup_simple_args args = {
6047+ .errp = &err,
c2b27bf2 6048+ .cpg = cpg
1facf9fc 6049+ };
6050+ wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
6051+ if (unlikely(wkq_err))
6052+ err = wkq_err;
6053+ }
6054+
6055+ dput(parent);
c2b27bf2
AM
6056+ if (h_file)
6057+ au_h_open_post(dentry, cpg->bsrc, h_file);
6058+
6059+out:
1facf9fc 6060+ return err;
6061+}
6062+
c2b27bf2 6063+int au_sio_cpup_simple(struct au_cp_generic *cpg)
367653fa 6064+{
5afbbe0d 6065+ aufs_bindex_t bsrc, bbot;
c2b27bf2 6066+ struct dentry *dentry, *h_dentry;
367653fa 6067+
c2b27bf2
AM
6068+ if (cpg->bsrc < 0) {
6069+ dentry = cpg->dentry;
5afbbe0d
AM
6070+ bbot = au_dbbot(dentry);
6071+ for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
c2b27bf2
AM
6072+ h_dentry = au_h_dptr(dentry, bsrc);
6073+ if (h_dentry) {
5527c038 6074+ AuDebugOn(d_is_negative(h_dentry));
c2b27bf2
AM
6075+ break;
6076+ }
6077+ }
5afbbe0d 6078+ AuDebugOn(bsrc > bbot);
c2b27bf2 6079+ cpg->bsrc = bsrc;
367653fa 6080+ }
c2b27bf2
AM
6081+ AuDebugOn(cpg->bsrc <= cpg->bdst);
6082+ return au_do_sio_cpup_simple(cpg);
6083+}
367653fa 6084+
c2b27bf2
AM
6085+int au_sio_cpdown_simple(struct au_cp_generic *cpg)
6086+{
6087+ AuDebugOn(cpg->bdst <= cpg->bsrc);
6088+ return au_do_sio_cpup_simple(cpg);
367653fa
AM
6089+}
6090+
1facf9fc 6091+/* ---------------------------------------------------------------------- */
6092+
6093+/*
6094+ * copyup the deleted file for writing.
6095+ */
c2b27bf2
AM
6096+static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
6097+ struct file *file)
1facf9fc 6098+{
6099+ int err;
c2b27bf2
AM
6100+ unsigned int flags_orig;
6101+ aufs_bindex_t bsrc_orig;
c2b27bf2 6102+ struct au_dinfo *dinfo;
5afbbe0d
AM
6103+ struct {
6104+ struct au_hdentry *hd;
6105+ struct dentry *h_dentry;
6106+ } hdst, hsrc;
1facf9fc 6107+
c2b27bf2 6108+ dinfo = au_di(cpg->dentry);
1308ab2a 6109+ AuRwMustWriteLock(&dinfo->di_rwsem);
6110+
c2b27bf2 6111+ bsrc_orig = cpg->bsrc;
5afbbe0d
AM
6112+ cpg->bsrc = dinfo->di_btop;
6113+ hdst.hd = au_hdentry(dinfo, cpg->bdst);
6114+ hdst.h_dentry = hdst.hd->hd_dentry;
6115+ hdst.hd->hd_dentry = wh_dentry;
6116+ dinfo->di_btop = cpg->bdst;
6117+
6118+ hsrc.h_dentry = NULL;
027c5e7a 6119+ if (file) {
5afbbe0d
AM
6120+ hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
6121+ hsrc.h_dentry = hsrc.hd->hd_dentry;
6122+ hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
027c5e7a 6123+ }
c2b27bf2
AM
6124+ flags_orig = cpg->flags;
6125+ cpg->flags = !AuCpup_DTIME;
6126+ err = au_cpup_single(cpg, /*h_parent*/NULL);
6127+ cpg->flags = flags_orig;
027c5e7a
AM
6128+ if (file) {
6129+ if (!err)
6130+ err = au_reopen_nondir(file);
5afbbe0d 6131+ hsrc.hd->hd_dentry = hsrc.h_dentry;
1facf9fc 6132+ }
5afbbe0d
AM
6133+ hdst.hd->hd_dentry = hdst.h_dentry;
6134+ dinfo->di_btop = cpg->bsrc;
c2b27bf2 6135+ cpg->bsrc = bsrc_orig;
1facf9fc 6136+
6137+ return err;
6138+}
6139+
c2b27bf2 6140+static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
1facf9fc 6141+{
6142+ int err;
c2b27bf2 6143+ aufs_bindex_t bdst;
1facf9fc 6144+ struct au_dtime dt;
c2b27bf2 6145+ struct dentry *dentry, *parent, *h_parent, *wh_dentry;
1facf9fc 6146+ struct au_branch *br;
6147+ struct path h_path;
6148+
c2b27bf2
AM
6149+ dentry = cpg->dentry;
6150+ bdst = cpg->bdst;
1facf9fc 6151+ br = au_sbr(dentry->d_sb, bdst);
6152+ parent = dget_parent(dentry);
6153+ h_parent = au_h_dptr(parent, bdst);
6154+ wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
6155+ err = PTR_ERR(wh_dentry);
6156+ if (IS_ERR(wh_dentry))
6157+ goto out;
6158+
6159+ h_path.dentry = h_parent;
86dc4139 6160+ h_path.mnt = au_br_mnt(br);
1facf9fc 6161+ au_dtime_store(&dt, parent, &h_path);
c2b27bf2 6162+ err = au_do_cpup_wh(cpg, wh_dentry, file);
1facf9fc 6163+ if (unlikely(err))
6164+ goto out_wh;
6165+
6166+ dget(wh_dentry);
6167+ h_path.dentry = wh_dentry;
2000de60 6168+ if (!d_is_dir(wh_dentry)) {
523b37e3 6169+ /* no delegation since it is just created */
5527c038 6170+ err = vfsub_unlink(d_inode(h_parent), &h_path,
523b37e3
AM
6171+ /*delegated*/NULL, /*force*/0);
6172+ } else
5527c038 6173+ err = vfsub_rmdir(d_inode(h_parent), &h_path);
1facf9fc 6174+ if (unlikely(err)) {
523b37e3
AM
6175+ AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
6176+ wh_dentry, err);
1facf9fc 6177+ err = -EIO;
6178+ }
6179+ au_dtime_revert(&dt);
5527c038 6180+ au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
1facf9fc 6181+
4f0767ce 6182+out_wh:
1facf9fc 6183+ dput(wh_dentry);
4f0767ce 6184+out:
1facf9fc 6185+ dput(parent);
6186+ return err;
6187+}
6188+
6189+struct au_cpup_wh_args {
6190+ int *errp;
c2b27bf2 6191+ struct au_cp_generic *cpg;
1facf9fc 6192+ struct file *file;
6193+};
6194+
6195+static void au_call_cpup_wh(void *args)
6196+{
6197+ struct au_cpup_wh_args *a = args;
86dc4139 6198+
c2b27bf2
AM
6199+ au_pin_hdir_acquire_nest(a->cpg->pin);
6200+ *a->errp = au_cpup_wh(a->cpg, a->file);
6201+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 6202+}
6203+
c2b27bf2 6204+int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
1facf9fc 6205+{
6206+ int err, wkq_err;
c2b27bf2 6207+ aufs_bindex_t bdst;
c1595e42 6208+ struct dentry *dentry, *parent, *h_orph, *h_parent;
86dc4139 6209+ struct inode *dir, *h_dir, *h_tmpdir;
1facf9fc 6210+ struct au_wbr *wbr;
c2b27bf2 6211+ struct au_pin wh_pin, *pin_orig;
1facf9fc 6212+
c2b27bf2
AM
6213+ dentry = cpg->dentry;
6214+ bdst = cpg->bdst;
1facf9fc 6215+ parent = dget_parent(dentry);
5527c038 6216+ dir = d_inode(parent);
1facf9fc 6217+ h_orph = NULL;
6218+ h_parent = NULL;
6219+ h_dir = au_igrab(au_h_iptr(dir, bdst));
6220+ h_tmpdir = h_dir;
c2b27bf2 6221+ pin_orig = NULL;
1facf9fc 6222+ if (!h_dir->i_nlink) {
6223+ wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
6224+ h_orph = wbr->wbr_orph;
6225+
6226+ h_parent = dget(au_h_dptr(parent, bdst));
1facf9fc 6227+ au_set_h_dptr(parent, bdst, dget(h_orph));
5527c038 6228+ h_tmpdir = d_inode(h_orph);
1facf9fc 6229+ au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
6230+
febd17d6 6231+ inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
4a4d8108 6232+ /* todo: au_h_open_pre()? */
86dc4139 6233+
c2b27bf2 6234+ pin_orig = cpg->pin;
86dc4139 6235+ au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
c2b27bf2
AM
6236+ AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
6237+ cpg->pin = &wh_pin;
1facf9fc 6238+ }
6239+
53392da6 6240+ if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE)
5527c038 6241+ && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
c2b27bf2 6242+ err = au_cpup_wh(cpg, file);
1facf9fc 6243+ else {
6244+ struct au_cpup_wh_args args = {
6245+ .errp = &err,
c2b27bf2
AM
6246+ .cpg = cpg,
6247+ .file = file
1facf9fc 6248+ };
6249+ wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
6250+ if (unlikely(wkq_err))
6251+ err = wkq_err;
6252+ }
6253+
6254+ if (h_orph) {
febd17d6 6255+ inode_unlock(h_tmpdir);
4a4d8108 6256+ /* todo: au_h_open_post()? */
1facf9fc 6257+ au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
1facf9fc 6258+ au_set_h_dptr(parent, bdst, h_parent);
c2b27bf2
AM
6259+ AuDebugOn(!pin_orig);
6260+ cpg->pin = pin_orig;
1facf9fc 6261+ }
6262+ iput(h_dir);
6263+ dput(parent);
6264+
6265+ return err;
6266+}
6267+
6268+/* ---------------------------------------------------------------------- */
6269+
6270+/*
6271+ * generic routine for both of copy-up and copy-down.
6272+ */
6273+/* cf. revalidate function in file.c */
6274+int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6275+ int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 6276+ struct au_pin *pin,
1facf9fc 6277+ struct dentry *h_parent, void *arg),
6278+ void *arg)
6279+{
6280+ int err;
6281+ struct au_pin pin;
5527c038 6282+ struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
1facf9fc 6283+
6284+ err = 0;
6285+ parent = dget_parent(dentry);
6286+ if (IS_ROOT(parent))
6287+ goto out;
6288+
6289+ au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
6290+ au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
6291+
6292+ /* do not use au_dpage */
6293+ real_parent = parent;
6294+ while (1) {
6295+ dput(parent);
6296+ parent = dget_parent(dentry);
6297+ h_parent = au_h_dptr(parent, bdst);
6298+ if (h_parent)
6299+ goto out; /* success */
6300+
6301+ /* find top dir which is necessary to cpup */
6302+ do {
6303+ d = parent;
6304+ dput(parent);
6305+ parent = dget_parent(d);
6306+ di_read_lock_parent3(parent, !AuLock_IR);
6307+ h_parent = au_h_dptr(parent, bdst);
6308+ di_read_unlock(parent, !AuLock_IR);
6309+ } while (!h_parent);
6310+
6311+ if (d != real_parent)
6312+ di_write_lock_child3(d);
6313+
6314+ /* somebody else might create while we were sleeping */
5527c038
JR
6315+ h_dentry = au_h_dptr(d, bdst);
6316+ if (!h_dentry || d_is_negative(h_dentry)) {
6317+ if (h_dentry)
5afbbe0d 6318+ au_update_dbtop(d);
1facf9fc 6319+
6320+ au_pin_set_dentry(&pin, d);
6321+ err = au_do_pin(&pin);
6322+ if (!err) {
86dc4139 6323+ err = cp(d, bdst, &pin, h_parent, arg);
1facf9fc 6324+ au_unpin(&pin);
6325+ }
6326+ }
6327+
6328+ if (d != real_parent)
6329+ di_write_unlock(d);
6330+ if (unlikely(err))
6331+ break;
6332+ }
6333+
4f0767ce 6334+out:
1facf9fc 6335+ dput(parent);
6336+ return err;
6337+}
6338+
6339+static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 6340+ struct au_pin *pin,
2000de60 6341+ struct dentry *h_parent __maybe_unused,
1facf9fc 6342+ void *arg __maybe_unused)
6343+{
c2b27bf2
AM
6344+ struct au_cp_generic cpg = {
6345+ .dentry = dentry,
6346+ .bdst = bdst,
6347+ .bsrc = -1,
6348+ .len = 0,
6349+ .pin = pin,
6350+ .flags = AuCpup_DTIME
6351+ };
6352+ return au_sio_cpup_simple(&cpg);
1facf9fc 6353+}
6354+
6355+int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6356+{
6357+ return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
6358+}
6359+
6360+int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6361+{
6362+ int err;
6363+ struct dentry *parent;
6364+ struct inode *dir;
6365+
6366+ parent = dget_parent(dentry);
5527c038 6367+ dir = d_inode(parent);
1facf9fc 6368+ err = 0;
6369+ if (au_h_iptr(dir, bdst))
6370+ goto out;
6371+
6372+ di_read_unlock(parent, AuLock_IR);
6373+ di_write_lock_parent(parent);
6374+ /* someone else might change our inode while we were sleeping */
6375+ if (!au_h_iptr(dir, bdst))
6376+ err = au_cpup_dirs(dentry, bdst);
6377+ di_downgrade_lock(parent, AuLock_IR);
6378+
4f0767ce 6379+out:
1facf9fc 6380+ dput(parent);
6381+ return err;
6382+}
7f207e10
AM
6383diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
6384--- /usr/share/empty/fs/aufs/cpup.h 1970-01-01 01:00:00.000000000 +0100
0b487229 6385+++ linux/fs/aufs/cpup.h 2021-02-22 23:30:37.699347580 +0100
062440b3
AM
6386@@ -0,0 +1,100 @@
6387+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 6388+/*
0b487229 6389+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 6390+ *
6391+ * This program, aufs is free software; you can redistribute it and/or modify
6392+ * it under the terms of the GNU General Public License as published by
6393+ * the Free Software Foundation; either version 2 of the License, or
6394+ * (at your option) any later version.
dece6358
AM
6395+ *
6396+ * This program is distributed in the hope that it will be useful,
6397+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6398+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6399+ * GNU General Public License for more details.
6400+ *
6401+ * You should have received a copy of the GNU General Public License
523b37e3 6402+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6403+ */
6404+
6405+/*
6406+ * copy-up/down functions
6407+ */
6408+
6409+#ifndef __AUFS_CPUP_H__
6410+#define __AUFS_CPUP_H__
6411+
6412+#ifdef __KERNEL__
6413+
dece6358 6414+#include <linux/path.h>
1facf9fc 6415+
dece6358
AM
6416+struct inode;
6417+struct file;
86dc4139 6418+struct au_pin;
dece6358 6419+
86dc4139 6420+void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
1facf9fc 6421+void au_cpup_attr_timesizes(struct inode *inode);
6422+void au_cpup_attr_nlink(struct inode *inode, int force);
6423+void au_cpup_attr_changeable(struct inode *inode);
6424+void au_cpup_igen(struct inode *inode, struct inode *h_inode);
6425+void au_cpup_attr_all(struct inode *inode, int force);
6426+
6427+/* ---------------------------------------------------------------------- */
6428+
c2b27bf2
AM
6429+struct au_cp_generic {
6430+ struct dentry *dentry;
6431+ aufs_bindex_t bdst, bsrc;
6432+ loff_t len;
6433+ struct au_pin *pin;
6434+ unsigned int flags;
6435+};
6436+
1facf9fc 6437+/* cpup flags */
392086de
AM
6438+#define AuCpup_DTIME 1 /* do dtime_store/revert */
6439+#define AuCpup_KEEPLINO (1 << 1) /* do not clear the lower xino,
6440+ for link(2) */
6441+#define AuCpup_RENAME (1 << 2) /* rename after cpup */
6442+#define AuCpup_HOPEN (1 << 3) /* call h_open_pre/post() in
6443+ cpup */
6444+#define AuCpup_OVERWRITE (1 << 4) /* allow overwriting the
6445+ existing entry */
6446+#define AuCpup_RWDST (1 << 5) /* force write target even if
6447+ the branch is marked as RO */
c2b27bf2 6448+
8b6a4947
AM
6449+#ifndef CONFIG_AUFS_BR_HFSPLUS
6450+#undef AuCpup_HOPEN
6451+#define AuCpup_HOPEN 0
6452+#endif
6453+
1facf9fc 6454+#define au_ftest_cpup(flags, name) ((flags) & AuCpup_##name)
7f207e10
AM
6455+#define au_fset_cpup(flags, name) \
6456+ do { (flags) |= AuCpup_##name; } while (0)
6457+#define au_fclr_cpup(flags, name) \
6458+ do { (flags) &= ~AuCpup_##name; } while (0)
1facf9fc 6459+
6460+int au_copy_file(struct file *dst, struct file *src, loff_t len);
c2b27bf2
AM
6461+int au_sio_cpup_simple(struct au_cp_generic *cpg);
6462+int au_sio_cpdown_simple(struct au_cp_generic *cpg);
6463+int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
1facf9fc 6464+
6465+int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6466+ int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 6467+ struct au_pin *pin,
1facf9fc 6468+ struct dentry *h_parent, void *arg),
6469+ void *arg);
6470+int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6471+int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6472+
6473+/* ---------------------------------------------------------------------- */
6474+
6475+/* keep timestamps when copyup */
6476+struct au_dtime {
6477+ struct dentry *dt_dentry;
6478+ struct path dt_h_path;
cd7a4cd9 6479+ struct timespec64 dt_atime, dt_mtime;
1facf9fc 6480+};
6481+void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6482+ struct path *h_path);
6483+void au_dtime_revert(struct au_dtime *dt);
6484+
6485+#endif /* __KERNEL__ */
6486+#endif /* __AUFS_CPUP_H__ */
7f207e10
AM
6487diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
6488--- /usr/share/empty/fs/aufs/dbgaufs.c 1970-01-01 01:00:00.000000000 +0100
0b487229 6489+++ linux/fs/aufs/dbgaufs.c 2021-02-22 23:30:37.699347580 +0100
e49925d1 6490@@ -0,0 +1,526 @@
cd7a4cd9 6491+// SPDX-License-Identifier: GPL-2.0
1facf9fc 6492+/*
0b487229 6493+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 6494+ *
6495+ * This program, aufs is free software; you can redistribute it and/or modify
6496+ * it under the terms of the GNU General Public License as published by
6497+ * the Free Software Foundation; either version 2 of the License, or
6498+ * (at your option) any later version.
dece6358
AM
6499+ *
6500+ * This program is distributed in the hope that it will be useful,
6501+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6502+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6503+ * GNU General Public License for more details.
6504+ *
6505+ * You should have received a copy of the GNU General Public License
523b37e3 6506+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6507+ */
6508+
6509+/*
6510+ * debugfs interface
6511+ */
6512+
6513+#include <linux/debugfs.h>
6514+#include "aufs.h"
6515+
6516+#ifndef CONFIG_SYSFS
6517+#error DEBUG_FS depends upon SYSFS
6518+#endif
6519+
6520+static struct dentry *dbgaufs;
cd7a4cd9 6521+static const mode_t dbgaufs_mode = 0444;
1facf9fc 6522+
6523+/* 20 is max digits length of ulong 64 */
6524+struct dbgaufs_arg {
6525+ int n;
6526+ char a[20 * 4];
6527+};
6528+
6529+/*
6530+ * common function for all XINO files
6531+ */
6532+static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6533+ struct file *file)
6534+{
e49925d1
JR
6535+ void *p;
6536+
6537+ p = file->private_data;
6538+ if (p) {
6539+ /* this is struct dbgaufs_arg */
6540+ AuDebugOn(!au_kfree_sz_test(p));
6541+ au_kfree_do_rcu(p);
6542+ }
1facf9fc 6543+ return 0;
6544+}
6545+
062440b3
AM
6546+static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt,
6547+ int cnt)
1facf9fc 6548+{
6549+ int err;
6550+ struct kstat st;
6551+ struct dbgaufs_arg *p;
6552+
6553+ err = -ENOMEM;
6554+ p = kmalloc(sizeof(*p), GFP_NOFS);
6555+ if (unlikely(!p))
6556+ goto out;
6557+
6558+ err = 0;
6559+ p->n = 0;
6560+ file->private_data = p;
6561+ if (!xf)
6562+ goto out;
6563+
521ced18 6564+ err = vfsub_getattr(&xf->f_path, &st);
1facf9fc 6565+ if (!err) {
6566+ if (do_fcnt)
6567+ p->n = snprintf
062440b3
AM
6568+ (p->a, sizeof(p->a), "%d, %llux%u %lld\n",
6569+ cnt, st.blocks, st.blksize,
1facf9fc 6570+ (long long)st.size);
6571+ else
521ced18 6572+ p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
1facf9fc 6573+ st.blocks, st.blksize,
6574+ (long long)st.size);
6575+ AuDebugOn(p->n >= sizeof(p->a));
6576+ } else {
6577+ p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6578+ err = 0;
6579+ }
6580+
4f0767ce 6581+out:
1facf9fc 6582+ return err;
1facf9fc 6583+}
6584+
6585+static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6586+ size_t count, loff_t *ppos)
6587+{
6588+ struct dbgaufs_arg *p;
6589+
6590+ p = file->private_data;
6591+ return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6592+}
6593+
6594+/* ---------------------------------------------------------------------- */
6595+
86dc4139
AM
6596+struct dbgaufs_plink_arg {
6597+ int n;
6598+ char a[];
6599+};
6600+
6601+static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6602+ struct file *file)
6603+{
1c60b727 6604+ free_page((unsigned long)file->private_data);
86dc4139
AM
6605+ return 0;
6606+}
6607+
6608+static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6609+{
6610+ int err, i, limit;
6611+ unsigned long n, sum;
6612+ struct dbgaufs_plink_arg *p;
6613+ struct au_sbinfo *sbinfo;
6614+ struct super_block *sb;
8b6a4947 6615+ struct hlist_bl_head *hbl;
86dc4139
AM
6616+
6617+ err = -ENOMEM;
6618+ p = (void *)get_zeroed_page(GFP_NOFS);
6619+ if (unlikely(!p))
6620+ goto out;
6621+
6622+ err = -EFBIG;
6623+ sbinfo = inode->i_private;
6624+ sb = sbinfo->si_sb;
6625+ si_noflush_read_lock(sb);
6626+ if (au_opt_test(au_mntflags(sb), PLINK)) {
6627+ limit = PAGE_SIZE - sizeof(p->n);
6628+
6629+ /* the number of buckets */
6630+ n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6631+ p->n += n;
6632+ limit -= n;
6633+
6634+ sum = 0;
8b6a4947
AM
6635+ for (i = 0, hbl = sbinfo->si_plink; i < AuPlink_NHASH;
6636+ i++, hbl++) {
6637+ n = au_hbl_count(hbl);
86dc4139
AM
6638+ sum += n;
6639+
6640+ n = snprintf(p->a + p->n, limit, "%lu ", n);
6641+ p->n += n;
6642+ limit -= n;
6643+ if (unlikely(limit <= 0))
6644+ goto out_free;
6645+ }
6646+ p->a[p->n - 1] = '\n';
6647+
6648+ /* the sum of plinks */
6649+ n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6650+ p->n += n;
6651+ limit -= n;
6652+ if (unlikely(limit <= 0))
6653+ goto out_free;
6654+ } else {
6655+#define str "1\n0\n0\n"
6656+ p->n = sizeof(str) - 1;
6657+ strcpy(p->a, str);
6658+#undef str
6659+ }
6660+ si_read_unlock(sb);
6661+
6662+ err = 0;
6663+ file->private_data = p;
6664+ goto out; /* success */
6665+
6666+out_free:
1c60b727 6667+ free_page((unsigned long)p);
86dc4139
AM
6668+out:
6669+ return err;
6670+}
6671+
6672+static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6673+ size_t count, loff_t *ppos)
6674+{
6675+ struct dbgaufs_plink_arg *p;
6676+
6677+ p = file->private_data;
6678+ return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6679+}
6680+
6681+static const struct file_operations dbgaufs_plink_fop = {
6682+ .owner = THIS_MODULE,
6683+ .open = dbgaufs_plink_open,
6684+ .release = dbgaufs_plink_release,
6685+ .read = dbgaufs_plink_read
6686+};
6687+
6688+/* ---------------------------------------------------------------------- */
6689+
1facf9fc 6690+static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6691+{
6692+ int err;
6693+ struct au_sbinfo *sbinfo;
6694+ struct super_block *sb;
6695+
6696+ sbinfo = inode->i_private;
6697+ sb = sbinfo->si_sb;
6698+ si_noflush_read_lock(sb);
062440b3 6699+ err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0, /*cnt*/0);
1facf9fc 6700+ si_read_unlock(sb);
6701+ return err;
6702+}
6703+
6704+static const struct file_operations dbgaufs_xib_fop = {
4a4d8108 6705+ .owner = THIS_MODULE,
1facf9fc 6706+ .open = dbgaufs_xib_open,
6707+ .release = dbgaufs_xi_release,
6708+ .read = dbgaufs_xi_read
6709+};
6710+
6711+/* ---------------------------------------------------------------------- */
6712+
6713+#define DbgaufsXi_PREFIX "xi"
6714+
6715+static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6716+{
acd2b654 6717+ int err, idx;
1facf9fc 6718+ long l;
acd2b654
AM
6719+ aufs_bindex_t bindex;
6720+ char *p, a[sizeof(DbgaufsXi_PREFIX) + 8];
1facf9fc 6721+ struct au_sbinfo *sbinfo;
6722+ struct super_block *sb;
acd2b654 6723+ struct au_xino *xi;
1facf9fc 6724+ struct file *xf;
6725+ struct qstr *name;
062440b3 6726+ struct au_branch *br;
1facf9fc 6727+
6728+ err = -ENOENT;
2000de60 6729+ name = &file->f_path.dentry->d_name;
1facf9fc 6730+ if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6731+ || memcmp(name->name, DbgaufsXi_PREFIX,
6732+ sizeof(DbgaufsXi_PREFIX) - 1)))
6733+ goto out;
acd2b654
AM
6734+
6735+ AuDebugOn(name->len >= sizeof(a));
6736+ memcpy(a, name->name, name->len);
6737+ a[name->len] = '\0';
6738+ p = strchr(a, '-');
6739+ if (p)
6740+ *p = '\0';
6741+ err = kstrtol(a + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
1facf9fc 6742+ if (unlikely(err))
6743+ goto out;
acd2b654
AM
6744+ bindex = l;
6745+ idx = 0;
6746+ if (p) {
6747+ err = kstrtol(p + 1, 10, &l);
6748+ if (unlikely(err))
6749+ goto out;
6750+ idx = l;
6751+ }
1facf9fc 6752+
acd2b654 6753+ err = -ENOENT;
1facf9fc 6754+ sbinfo = inode->i_private;
6755+ sb = sbinfo->si_sb;
6756+ si_noflush_read_lock(sb);
acd2b654
AM
6757+ if (unlikely(bindex < 0 || bindex > au_sbbot(sb)))
6758+ goto out_si;
6759+ br = au_sbr(sb, bindex);
6760+ xi = br->br_xino;
6761+ if (unlikely(idx >= xi->xi_nfile))
6762+ goto out_si;
6763+ xf = au_xino_file(xi, idx);
6764+ if (xf)
062440b3
AM
6765+ err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1,
6766+ au_xino_count(br));
1facf9fc 6767+
acd2b654
AM
6768+out_si:
6769+ si_read_unlock(sb);
4f0767ce 6770+out:
acd2b654 6771+ AuTraceErr(err);
1facf9fc 6772+ return err;
6773+}
6774+
6775+static const struct file_operations dbgaufs_xino_fop = {
4a4d8108 6776+ .owner = THIS_MODULE,
1facf9fc 6777+ .open = dbgaufs_xino_open,
6778+ .release = dbgaufs_xi_release,
6779+ .read = dbgaufs_xi_read
6780+};
6781+
062440b3
AM
6782+void dbgaufs_xino_del(struct au_branch *br)
6783+{
6784+ struct dentry *dbgaufs;
6785+
6786+ dbgaufs = br->br_dbgaufs;
6787+ if (!dbgaufs)
6788+ return;
6789+
6790+ br->br_dbgaufs = NULL;
6791+ /* debugfs acquires the parent i_mutex */
6792+ lockdep_off();
6793+ debugfs_remove(dbgaufs);
6794+ lockdep_on();
6795+}
6796+
1facf9fc 6797+void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6798+{
5afbbe0d 6799+ aufs_bindex_t bbot;
1facf9fc 6800+ struct au_branch *br;
1facf9fc 6801+
6802+ if (!au_sbi(sb)->si_dbgaufs)
6803+ return;
6804+
5afbbe0d
AM
6805+ bbot = au_sbbot(sb);
6806+ for (; bindex <= bbot; bindex++) {
1facf9fc 6807+ br = au_sbr(sb, bindex);
062440b3
AM
6808+ dbgaufs_xino_del(br);
6809+ }
6810+}
6811+
acd2b654
AM
6812+static void dbgaufs_br_do_add(struct super_block *sb, aufs_bindex_t bindex,
6813+ unsigned int idx, struct dentry *parent,
6814+ struct au_sbinfo *sbinfo)
062440b3
AM
6815+{
6816+ struct au_branch *br;
6817+ struct dentry *d;
acd2b654
AM
6818+ /* "xi" bindex(5) "-" idx(2) NULL */
6819+ char name[sizeof(DbgaufsXi_PREFIX) + 8];
062440b3 6820+
acd2b654
AM
6821+ if (!idx)
6822+ snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6823+ else
6824+ snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d-%u",
6825+ bindex, idx);
062440b3
AM
6826+ br = au_sbr(sb, bindex);
6827+ if (br->br_dbgaufs) {
6828+ struct qstr qstr = QSTR_INIT(name, strlen(name));
6829+
6830+ if (!au_qstreq(&br->br_dbgaufs->d_name, &qstr)) {
6831+ /* debugfs acquires the parent i_mutex */
6832+ lockdep_off();
6833+ d = debugfs_rename(parent, br->br_dbgaufs, parent,
6834+ name);
6835+ lockdep_on();
6836+ if (unlikely(!d))
6837+ pr_warn("failed renaming %pd/%s, ignored.\n",
6838+ parent, name);
6839+ }
6840+ } else {
e2f27e51 6841+ lockdep_off();
062440b3
AM
6842+ br->br_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6843+ sbinfo, &dbgaufs_xino_fop);
e2f27e51 6844+ lockdep_on();
062440b3 6845+ if (unlikely(!br->br_dbgaufs))
acd2b654 6846+ pr_warn("failed creating %pd/%s, ignored.\n",
062440b3 6847+ parent, name);
1facf9fc 6848+ }
6849+}
6850+
acd2b654
AM
6851+static void dbgaufs_br_add(struct super_block *sb, aufs_bindex_t bindex,
6852+ struct dentry *parent, struct au_sbinfo *sbinfo)
6853+{
6854+ struct au_branch *br;
6855+ struct au_xino *xi;
6856+ unsigned int u;
6857+
6858+ br = au_sbr(sb, bindex);
6859+ xi = br->br_xino;
6860+ for (u = 0; u < xi->xi_nfile; u++)
6861+ dbgaufs_br_do_add(sb, bindex, u, parent, sbinfo);
6862+}
6863+
062440b3 6864+void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown)
1facf9fc 6865+{
6866+ struct au_sbinfo *sbinfo;
6867+ struct dentry *parent;
5afbbe0d 6868+ aufs_bindex_t bbot;
062440b3
AM
6869+
6870+ if (!au_opt_test(au_mntflags(sb), XINO))
6871+ return;
1facf9fc 6872+
6873+ sbinfo = au_sbi(sb);
6874+ parent = sbinfo->si_dbgaufs;
6875+ if (!parent)
6876+ return;
6877+
5afbbe0d 6878+ bbot = au_sbbot(sb);
062440b3
AM
6879+ if (topdown)
6880+ for (; bindex <= bbot; bindex++)
6881+ dbgaufs_br_add(sb, bindex, parent, sbinfo);
6882+ else
6883+ for (; bbot >= bindex; bbot--)
6884+ dbgaufs_br_add(sb, bbot, parent, sbinfo);
1facf9fc 6885+}
6886+
6887+/* ---------------------------------------------------------------------- */
6888+
6889+#ifdef CONFIG_AUFS_EXPORT
6890+static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6891+{
6892+ int err;
6893+ struct au_sbinfo *sbinfo;
6894+ struct super_block *sb;
6895+
6896+ sbinfo = inode->i_private;
6897+ sb = sbinfo->si_sb;
6898+ si_noflush_read_lock(sb);
062440b3 6899+ err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0, /*cnt*/0);
1facf9fc 6900+ si_read_unlock(sb);
6901+ return err;
6902+}
6903+
6904+static const struct file_operations dbgaufs_xigen_fop = {
4a4d8108 6905+ .owner = THIS_MODULE,
1facf9fc 6906+ .open = dbgaufs_xigen_open,
6907+ .release = dbgaufs_xi_release,
6908+ .read = dbgaufs_xi_read
6909+};
6910+
6911+static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6912+{
6913+ int err;
6914+
dece6358 6915+ /*
c1595e42 6916+ * This function is a dynamic '__init' function actually,
dece6358
AM
6917+ * so the tiny check for si_rwsem is unnecessary.
6918+ */
6919+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6920+
1facf9fc 6921+ err = -EIO;
6922+ sbinfo->si_dbgaufs_xigen = debugfs_create_file
6923+ ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6924+ &dbgaufs_xigen_fop);
6925+ if (sbinfo->si_dbgaufs_xigen)
6926+ err = 0;
6927+
6928+ return err;
6929+}
6930+#else
6931+static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6932+{
6933+ return 0;
6934+}
6935+#endif /* CONFIG_AUFS_EXPORT */
6936+
6937+/* ---------------------------------------------------------------------- */
6938+
6939+void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6940+{
dece6358 6941+ /*
7e9cd9fe 6942+ * This function is a dynamic '__fin' function actually,
dece6358
AM
6943+ * so the tiny check for si_rwsem is unnecessary.
6944+ */
6945+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6946+
1facf9fc 6947+ debugfs_remove_recursive(sbinfo->si_dbgaufs);
6948+ sbinfo->si_dbgaufs = NULL;
1facf9fc 6949+}
6950+
6951+int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6952+{
6953+ int err;
6954+ char name[SysaufsSiNameLen];
6955+
dece6358 6956+ /*
c1595e42 6957+ * This function is a dynamic '__init' function actually,
dece6358
AM
6958+ * so the tiny check for si_rwsem is unnecessary.
6959+ */
6960+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6961+
1facf9fc 6962+ err = -ENOENT;
6963+ if (!dbgaufs) {
6964+ AuErr1("/debug/aufs is uninitialized\n");
6965+ goto out;
6966+ }
6967+
6968+ err = -EIO;
6969+ sysaufs_name(sbinfo, name);
6970+ sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6971+ if (unlikely(!sbinfo->si_dbgaufs))
6972+ goto out;
1facf9fc 6973+
062440b3 6974+ /* regardless plink/noplink option */
86dc4139
AM
6975+ sbinfo->si_dbgaufs_plink = debugfs_create_file
6976+ ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6977+ &dbgaufs_plink_fop);
6978+ if (unlikely(!sbinfo->si_dbgaufs_plink))
6979+ goto out_dir;
6980+
062440b3
AM
6981+ /* regardless xino/noxino option */
6982+ sbinfo->si_dbgaufs_xib = debugfs_create_file
6983+ ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6984+ &dbgaufs_xib_fop);
6985+ if (unlikely(!sbinfo->si_dbgaufs_xib))
6986+ goto out_dir;
6987+
1facf9fc 6988+ err = dbgaufs_xigen_init(sbinfo);
6989+ if (!err)
6990+ goto out; /* success */
6991+
4f0767ce 6992+out_dir:
1facf9fc 6993+ dbgaufs_si_fin(sbinfo);
4f0767ce 6994+out:
062440b3
AM
6995+ if (unlikely(err))
6996+ pr_err("debugfs/aufs failed\n");
1facf9fc 6997+ return err;
6998+}
6999+
7000+/* ---------------------------------------------------------------------- */
7001+
7002+void dbgaufs_fin(void)
7003+{
7004+ debugfs_remove(dbgaufs);
7005+}
7006+
7007+int __init dbgaufs_init(void)
7008+{
7009+ int err;
7010+
7011+ err = -EIO;
7012+ dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
7013+ if (dbgaufs)
7014+ err = 0;
7015+ return err;
7016+}
7f207e10
AM
7017diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
7018--- /usr/share/empty/fs/aufs/dbgaufs.h 1970-01-01 01:00:00.000000000 +0100
0b487229 7019+++ linux/fs/aufs/dbgaufs.h 2021-02-22 23:30:37.699347580 +0100
062440b3
AM
7020@@ -0,0 +1,53 @@
7021+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 7022+/*
0b487229 7023+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 7024+ *
7025+ * This program, aufs is free software; you can redistribute it and/or modify
7026+ * it under the terms of the GNU General Public License as published by
7027+ * the Free Software Foundation; either version 2 of the License, or
7028+ * (at your option) any later version.
dece6358
AM
7029+ *
7030+ * This program is distributed in the hope that it will be useful,
7031+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7032+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7033+ * GNU General Public License for more details.
7034+ *
7035+ * You should have received a copy of the GNU General Public License
523b37e3 7036+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7037+ */
7038+
7039+/*
7040+ * debugfs interface
7041+ */
7042+
7043+#ifndef __DBGAUFS_H__
7044+#define __DBGAUFS_H__
7045+
7046+#ifdef __KERNEL__
7047+
dece6358 7048+struct super_block;
1facf9fc 7049+struct au_sbinfo;
062440b3 7050+struct au_branch;
dece6358 7051+
1facf9fc 7052+#ifdef CONFIG_DEBUG_FS
7053+/* dbgaufs.c */
062440b3 7054+void dbgaufs_xino_del(struct au_branch *br);
1facf9fc 7055+void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
062440b3 7056+void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown);
1facf9fc 7057+void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
7058+int dbgaufs_si_init(struct au_sbinfo *sbinfo);
7059+void dbgaufs_fin(void);
7060+int __init dbgaufs_init(void);
1facf9fc 7061+#else
062440b3 7062+AuStubVoid(dbgaufs_xino_del, struct au_branch *br)
4a4d8108 7063+AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
062440b3
AM
7064+AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex,
7065+ int topdown)
4a4d8108
AM
7066+AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
7067+AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
7068+AuStubVoid(dbgaufs_fin, void)
7069+AuStubInt0(__init dbgaufs_init, void)
1facf9fc 7070+#endif /* CONFIG_DEBUG_FS */
7071+
7072+#endif /* __KERNEL__ */
7073+#endif /* __DBGAUFS_H__ */
7f207e10
AM
7074diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
7075--- /usr/share/empty/fs/aufs/dcsub.c 1970-01-01 01:00:00.000000000 +0100
0b487229 7076+++ linux/fs/aufs/dcsub.c 2021-02-22 23:30:37.699347580 +0100
cd7a4cd9
AM
7077@@ -0,0 +1,225 @@
7078+// SPDX-License-Identifier: GPL-2.0
1facf9fc 7079+/*
0b487229 7080+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 7081+ *
7082+ * This program, aufs is free software; you can redistribute it and/or modify
7083+ * it under the terms of the GNU General Public License as published by
7084+ * the Free Software Foundation; either version 2 of the License, or
7085+ * (at your option) any later version.
dece6358
AM
7086+ *
7087+ * This program is distributed in the hope that it will be useful,
7088+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7089+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7090+ * GNU General Public License for more details.
7091+ *
7092+ * You should have received a copy of the GNU General Public License
523b37e3 7093+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7094+ */
7095+
7096+/*
7097+ * sub-routines for dentry cache
7098+ */
7099+
7100+#include "aufs.h"
7101+
7102+static void au_dpage_free(struct au_dpage *dpage)
7103+{
7104+ int i;
7105+ struct dentry **p;
7106+
7107+ p = dpage->dentries;
7108+ for (i = 0; i < dpage->ndentry; i++)
7109+ dput(*p++);
1c60b727 7110+ free_page((unsigned long)dpage->dentries);
1facf9fc 7111+}
7112+
7113+int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
7114+{
7115+ int err;
7116+ void *p;
7117+
7118+ err = -ENOMEM;
7119+ dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
7120+ if (unlikely(!dpages->dpages))
7121+ goto out;
7122+
7123+ p = (void *)__get_free_page(gfp);
7124+ if (unlikely(!p))
7125+ goto out_dpages;
7126+
7127+ dpages->dpages[0].ndentry = 0;
7128+ dpages->dpages[0].dentries = p;
7129+ dpages->ndpage = 1;
7130+ return 0; /* success */
7131+
4f0767ce 7132+out_dpages:
e49925d1 7133+ au_kfree_try_rcu(dpages->dpages);
4f0767ce 7134+out:
1facf9fc 7135+ return err;
7136+}
7137+
7138+void au_dpages_free(struct au_dcsub_pages *dpages)
7139+{
7140+ int i;
7141+ struct au_dpage *p;
7142+
7143+ p = dpages->dpages;
7144+ for (i = 0; i < dpages->ndpage; i++)
7145+ au_dpage_free(p++);
e49925d1 7146+ au_kfree_try_rcu(dpages->dpages);
1facf9fc 7147+}
7148+
7149+static int au_dpages_append(struct au_dcsub_pages *dpages,
7150+ struct dentry *dentry, gfp_t gfp)
7151+{
7152+ int err, sz;
7153+ struct au_dpage *dpage;
7154+ void *p;
7155+
7156+ dpage = dpages->dpages + dpages->ndpage - 1;
7157+ sz = PAGE_SIZE / sizeof(dentry);
7158+ if (unlikely(dpage->ndentry >= sz)) {
7159+ AuLabel(new dpage);
7160+ err = -ENOMEM;
7161+ sz = dpages->ndpage * sizeof(*dpages->dpages);
7162+ p = au_kzrealloc(dpages->dpages, sz,
e2f27e51
AM
7163+ sz + sizeof(*dpages->dpages), gfp,
7164+ /*may_shrink*/0);
1facf9fc 7165+ if (unlikely(!p))
7166+ goto out;
7167+
7168+ dpages->dpages = p;
7169+ dpage = dpages->dpages + dpages->ndpage;
7170+ p = (void *)__get_free_page(gfp);
7171+ if (unlikely(!p))
7172+ goto out;
7173+
7174+ dpage->ndentry = 0;
7175+ dpage->dentries = p;
7176+ dpages->ndpage++;
7177+ }
7178+
c1595e42 7179+ AuDebugOn(au_dcount(dentry) <= 0);
027c5e7a 7180+ dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
1facf9fc 7181+ return 0; /* success */
7182+
4f0767ce 7183+out:
1facf9fc 7184+ return err;
7185+}
7186+
c1595e42
JR
7187+/* todo: BAD approach */
7188+/* copied from linux/fs/dcache.c */
7189+enum d_walk_ret {
7190+ D_WALK_CONTINUE,
7191+ D_WALK_QUIT,
7192+ D_WALK_NORETRY,
7193+ D_WALK_SKIP,
7194+};
7195+
7196+extern void d_walk(struct dentry *parent, void *data,
cd7a4cd9 7197+ enum d_walk_ret (*enter)(void *, struct dentry *));
c1595e42
JR
7198+
7199+struct ac_dpages_arg {
1facf9fc 7200+ int err;
c1595e42
JR
7201+ struct au_dcsub_pages *dpages;
7202+ struct super_block *sb;
7203+ au_dpages_test test;
7204+ void *arg;
7205+};
1facf9fc 7206+
c1595e42
JR
7207+static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
7208+{
7209+ enum d_walk_ret ret;
7210+ struct ac_dpages_arg *arg = _arg;
1facf9fc 7211+
c1595e42
JR
7212+ ret = D_WALK_CONTINUE;
7213+ if (dentry->d_sb == arg->sb
7214+ && !IS_ROOT(dentry)
7215+ && au_dcount(dentry) > 0
7216+ && au_di(dentry)
7217+ && (!arg->test || arg->test(dentry, arg->arg))) {
7218+ arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
7219+ if (unlikely(arg->err))
7220+ ret = D_WALK_QUIT;
1facf9fc 7221+ }
7222+
c1595e42
JR
7223+ return ret;
7224+}
027c5e7a 7225+
c1595e42
JR
7226+int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7227+ au_dpages_test test, void *arg)
7228+{
7229+ struct ac_dpages_arg args = {
7230+ .err = 0,
7231+ .dpages = dpages,
7232+ .sb = root->d_sb,
7233+ .test = test,
7234+ .arg = arg
7235+ };
027c5e7a 7236+
cd7a4cd9 7237+ d_walk(root, &args, au_call_dpages_append);
c1595e42
JR
7238+
7239+ return args.err;
1facf9fc 7240+}
7241+
7242+int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7243+ int do_include, au_dpages_test test, void *arg)
7244+{
7245+ int err;
7246+
7247+ err = 0;
027c5e7a
AM
7248+ write_seqlock(&rename_lock);
7249+ spin_lock(&dentry->d_lock);
7250+ if (do_include
c1595e42 7251+ && au_dcount(dentry) > 0
027c5e7a 7252+ && (!test || test(dentry, arg)))
1facf9fc 7253+ err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
027c5e7a
AM
7254+ spin_unlock(&dentry->d_lock);
7255+ if (unlikely(err))
7256+ goto out;
7257+
7258+ /*
523b37e3 7259+ * RCU for vfsmount is unnecessary since this is a traverse in a single
027c5e7a
AM
7260+ * mount
7261+ */
1facf9fc 7262+ while (!IS_ROOT(dentry)) {
027c5e7a
AM
7263+ dentry = dentry->d_parent; /* rename_lock is locked */
7264+ spin_lock(&dentry->d_lock);
c1595e42 7265+ if (au_dcount(dentry) > 0
027c5e7a 7266+ && (!test || test(dentry, arg)))
1facf9fc 7267+ err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
027c5e7a
AM
7268+ spin_unlock(&dentry->d_lock);
7269+ if (unlikely(err))
7270+ break;
1facf9fc 7271+ }
7272+
4f0767ce 7273+out:
027c5e7a 7274+ write_sequnlock(&rename_lock);
1facf9fc 7275+ return err;
7276+}
7277+
027c5e7a
AM
7278+static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
7279+{
7280+ return au_di(dentry) && dentry->d_sb == arg;
7281+}
7282+
7283+int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7284+ struct dentry *dentry, int do_include)
7285+{
7286+ return au_dcsub_pages_rev(dpages, dentry, do_include,
7287+ au_dcsub_dpages_aufs, dentry->d_sb);
7288+}
7289+
4a4d8108 7290+int au_test_subdir(struct dentry *d1, struct dentry *d2)
1facf9fc 7291+{
4a4d8108
AM
7292+ struct path path[2] = {
7293+ {
7294+ .dentry = d1
7295+ },
7296+ {
7297+ .dentry = d2
7298+ }
7299+ };
1facf9fc 7300+
4a4d8108 7301+ return path_is_under(path + 0, path + 1);
1facf9fc 7302+}
7f207e10
AM
7303diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
7304--- /usr/share/empty/fs/aufs/dcsub.h 1970-01-01 01:00:00.000000000 +0100
0b487229 7305+++ linux/fs/aufs/dcsub.h 2021-02-22 23:30:37.699347580 +0100
062440b3
AM
7306@@ -0,0 +1,137 @@
7307+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 7308+/*
0b487229 7309+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 7310+ *
7311+ * This program, aufs is free software; you can redistribute it and/or modify
7312+ * it under the terms of the GNU General Public License as published by
7313+ * the Free Software Foundation; either version 2 of the License, or
7314+ * (at your option) any later version.
dece6358
AM
7315+ *
7316+ * This program is distributed in the hope that it will be useful,
7317+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7318+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7319+ * GNU General Public License for more details.
7320+ *
7321+ * You should have received a copy of the GNU General Public License
523b37e3 7322+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7323+ */
7324+
7325+/*
7326+ * sub-routines for dentry cache
7327+ */
7328+
7329+#ifndef __AUFS_DCSUB_H__
7330+#define __AUFS_DCSUB_H__
7331+
7332+#ifdef __KERNEL__
7333+
7f207e10 7334+#include <linux/dcache.h>
027c5e7a 7335+#include <linux/fs.h>
dece6358 7336+
1facf9fc 7337+struct au_dpage {
7338+ int ndentry;
7339+ struct dentry **dentries;
7340+};
7341+
7342+struct au_dcsub_pages {
7343+ int ndpage;
7344+ struct au_dpage *dpages;
7345+};
7346+
7347+/* ---------------------------------------------------------------------- */
7348+
7f207e10 7349+/* dcsub.c */
1facf9fc 7350+int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
7351+void au_dpages_free(struct au_dcsub_pages *dpages);
7352+typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
7353+int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7354+ au_dpages_test test, void *arg);
7355+int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7356+ int do_include, au_dpages_test test, void *arg);
027c5e7a
AM
7357+int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7358+ struct dentry *dentry, int do_include);
4a4d8108 7359+int au_test_subdir(struct dentry *d1, struct dentry *d2);
1facf9fc 7360+
7f207e10
AM
7361+/* ---------------------------------------------------------------------- */
7362+
523b37e3
AM
7363+/*
7364+ * todo: in linux-3.13, several similar (but faster) helpers are added to
7365+ * include/linux/dcache.h. Try them (in the future).
7366+ */
7367+
027c5e7a
AM
7368+static inline int au_d_hashed_positive(struct dentry *d)
7369+{
7370+ int err;
5527c038 7371+ struct inode *inode = d_inode(d);
076b876e 7372+
027c5e7a 7373+ err = 0;
5527c038
JR
7374+ if (unlikely(d_unhashed(d)
7375+ || d_is_negative(d)
7376+ || !inode->i_nlink))
027c5e7a
AM
7377+ err = -ENOENT;
7378+ return err;
7379+}
7380+
38d290e6
JR
7381+static inline int au_d_linkable(struct dentry *d)
7382+{
7383+ int err;
5527c038 7384+ struct inode *inode = d_inode(d);
076b876e 7385+
38d290e6
JR
7386+ err = au_d_hashed_positive(d);
7387+ if (err
5527c038 7388+ && d_is_positive(d)
38d290e6
JR
7389+ && (inode->i_state & I_LINKABLE))
7390+ err = 0;
7391+ return err;
7392+}
7393+
027c5e7a
AM
7394+static inline int au_d_alive(struct dentry *d)
7395+{
7396+ int err;
7397+ struct inode *inode;
076b876e 7398+
027c5e7a
AM
7399+ err = 0;
7400+ if (!IS_ROOT(d))
7401+ err = au_d_hashed_positive(d);
7402+ else {
5527c038
JR
7403+ inode = d_inode(d);
7404+ if (unlikely(d_unlinked(d)
7405+ || d_is_negative(d)
7406+ || !inode->i_nlink))
027c5e7a
AM
7407+ err = -ENOENT;
7408+ }
7409+ return err;
7410+}
7411+
7412+static inline int au_alive_dir(struct dentry *d)
7f207e10 7413+{
027c5e7a 7414+ int err;
076b876e 7415+
027c5e7a 7416+ err = au_d_alive(d);
5527c038 7417+ if (unlikely(err || IS_DEADDIR(d_inode(d))))
027c5e7a
AM
7418+ err = -ENOENT;
7419+ return err;
7f207e10
AM
7420+}
7421+
38d290e6
JR
7422+static inline int au_qstreq(struct qstr *a, struct qstr *b)
7423+{
7424+ return a->len == b->len
7425+ && !memcmp(a->name, b->name, a->len);
7426+}
7427+
7e9cd9fe
AM
7428+/*
7429+ * by the commit
7430+ * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
7431+ * taking d_lock
7432+ * the type of d_lockref.count became int, but the inlined function d_count()
7433+ * still returns unsigned int.
7434+ * I don't know why. Maybe it is for every d_count() users?
7435+ * Anyway au_dcount() lives on.
7436+ */
c1595e42
JR
7437+static inline int au_dcount(struct dentry *d)
7438+{
7439+ return (int)d_count(d);
7440+}
7441+
1facf9fc 7442+#endif /* __KERNEL__ */
7443+#endif /* __AUFS_DCSUB_H__ */
7f207e10
AM
7444diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
7445--- /usr/share/empty/fs/aufs/debug.c 1970-01-01 01:00:00.000000000 +0100
0b487229
JR
7446+++ linux/fs/aufs/debug.c 2021-02-22 23:30:37.699347580 +0100
7447@@ -0,0 +1,441 @@
cd7a4cd9 7448+// SPDX-License-Identifier: GPL-2.0
1facf9fc 7449+/*
0b487229 7450+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 7451+ *
7452+ * This program, aufs is free software; you can redistribute it and/or modify
7453+ * it under the terms of the GNU General Public License as published by
7454+ * the Free Software Foundation; either version 2 of the License, or
7455+ * (at your option) any later version.
dece6358
AM
7456+ *
7457+ * This program is distributed in the hope that it will be useful,
7458+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7459+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7460+ * GNU General Public License for more details.
7461+ *
7462+ * You should have received a copy of the GNU General Public License
523b37e3 7463+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7464+ */
7465+
7466+/*
7467+ * debug print functions
7468+ */
7469+
0b487229 7470+#include <linux/iversion.h>
1facf9fc 7471+#include "aufs.h"
7472+
392086de
AM
7473+/* Returns 0, or -errno. arg is in kp->arg. */
7474+static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
7475+{
7476+ int err, n;
7477+
7478+ err = kstrtoint(val, 0, &n);
7479+ if (!err) {
7480+ if (n > 0)
7481+ au_debug_on();
7482+ else
7483+ au_debug_off();
7484+ }
7485+ return err;
7486+}
7487+
7488+/* Returns length written or -errno. Buffer is 4k (ie. be short!) */
7489+static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
7490+{
7491+ atomic_t *a;
7492+
7493+ a = kp->arg;
7494+ return sprintf(buffer, "%d", atomic_read(a));
7495+}
7496+
7497+static struct kernel_param_ops param_ops_atomic_t = {
7498+ .set = param_atomic_t_set,
7499+ .get = param_atomic_t_get
7500+ /* void (*free)(void *arg) */
7501+};
7502+
7503+atomic_t aufs_debug = ATOMIC_INIT(0);
1facf9fc 7504+MODULE_PARM_DESC(debug, "debug print");
cd7a4cd9 7505+module_param_named(debug, aufs_debug, atomic_t, 0664);
1facf9fc 7506+
c1595e42 7507+DEFINE_MUTEX(au_dbg_mtx); /* just to serialize the dbg msgs */
1facf9fc 7508+char *au_plevel = KERN_DEBUG;
e49829fe
JR
7509+#define dpri(fmt, ...) do { \
7510+ if ((au_plevel \
7511+ && strcmp(au_plevel, KERN_DEBUG)) \
7512+ || au_debug_test()) \
7513+ printk("%s" fmt, au_plevel, ##__VA_ARGS__); \
1facf9fc 7514+} while (0)
7515+
7516+/* ---------------------------------------------------------------------- */
7517+
7518+void au_dpri_whlist(struct au_nhash *whlist)
7519+{
7520+ unsigned long ul, n;
7521+ struct hlist_head *head;
c06a8ce3 7522+ struct au_vdir_wh *pos;
1facf9fc 7523+
7524+ n = whlist->nh_num;
7525+ head = whlist->nh_head;
7526+ for (ul = 0; ul < n; ul++) {
c06a8ce3 7527+ hlist_for_each_entry(pos, head, wh_hash)
1facf9fc 7528+ dpri("b%d, %.*s, %d\n",
c06a8ce3
AM
7529+ pos->wh_bindex,
7530+ pos->wh_str.len, pos->wh_str.name,
7531+ pos->wh_str.len);
1facf9fc 7532+ head++;
7533+ }
7534+}
7535+
7536+void au_dpri_vdir(struct au_vdir *vdir)
7537+{
7538+ unsigned long ul;
7539+ union au_vdir_deblk_p p;
7540+ unsigned char *o;
7541+
7542+ if (!vdir || IS_ERR(vdir)) {
7543+ dpri("err %ld\n", PTR_ERR(vdir));
7544+ return;
7545+ }
7546+
be118d29 7547+ dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %llu\n",
1facf9fc 7548+ vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
7549+ vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
7550+ for (ul = 0; ul < vdir->vd_nblk; ul++) {
7551+ p.deblk = vdir->vd_deblk[ul];
7552+ o = p.deblk;
7553+ dpri("[%lu]: %p\n", ul, o);
7554+ }
7555+}
7556+
53392da6 7557+static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
1facf9fc 7558+ struct dentry *wh)
7559+{
7560+ char *n = NULL;
7561+ int l = 0;
7562+
7563+ if (!inode || IS_ERR(inode)) {
7564+ dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7565+ return -1;
7566+ }
7567+
c2b27bf2 7568+ /* the type of i_blocks depends upon CONFIG_LBDAF */
1facf9fc 7569+ BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7570+ && sizeof(inode->i_blocks) != sizeof(u64));
7571+ if (wh) {
7572+ n = (void *)wh->d_name.name;
7573+ l = wh->d_name.len;
7574+ }
7575+
53392da6
AM
7576+ dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
7577+ " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7578+ bindex, inode,
1facf9fc 7579+ inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7580+ atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7581+ i_size_read(inode), (unsigned long long)inode->i_blocks,
cd7a4cd9 7582+ hn, (long long)timespec64_to_ns(&inode->i_ctime) & 0x0ffff,
1facf9fc 7583+ inode->i_mapping ? inode->i_mapping->nrpages : 0,
be118d29 7584+ inode->i_state, inode->i_flags, inode_peek_iversion(inode),
b752ccd1 7585+ inode->i_generation,
1facf9fc 7586+ l ? ", wh " : "", l, n);
7587+ return 0;
7588+}
7589+
7590+void au_dpri_inode(struct inode *inode)
7591+{
7592+ struct au_iinfo *iinfo;
5afbbe0d 7593+ struct au_hinode *hi;
1facf9fc 7594+ aufs_bindex_t bindex;
53392da6 7595+ int err, hn;
1facf9fc 7596+
53392da6 7597+ err = do_pri_inode(-1, inode, -1, NULL);
5afbbe0d 7598+ if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
1facf9fc 7599+ return;
7600+
7601+ iinfo = au_ii(inode);
5afbbe0d
AM
7602+ dpri("i-1: btop %d, bbot %d, gen %d\n",
7603+ iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
7604+ if (iinfo->ii_btop < 0)
1facf9fc 7605+ return;
53392da6 7606+ hn = 0;
5afbbe0d
AM
7607+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
7608+ hi = au_hinode(iinfo, bindex);
7609+ hn = !!au_hn(hi);
7610+ do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
53392da6 7611+ }
1facf9fc 7612+}
7613+
2cbb1c4b
JR
7614+void au_dpri_dalias(struct inode *inode)
7615+{
7616+ struct dentry *d;
7617+
7618+ spin_lock(&inode->i_lock);
c1595e42 7619+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
2cbb1c4b
JR
7620+ au_dpri_dentry(d);
7621+ spin_unlock(&inode->i_lock);
7622+}
7623+
1facf9fc 7624+static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7625+{
7626+ struct dentry *wh = NULL;
53392da6 7627+ int hn;
5afbbe0d 7628+ struct inode *inode;
076b876e 7629+ struct au_iinfo *iinfo;
5afbbe0d 7630+ struct au_hinode *hi;
1facf9fc 7631+
7632+ if (!dentry || IS_ERR(dentry)) {
7633+ dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7634+ return -1;
7635+ }
7636+ /* do not call dget_parent() here */
027c5e7a 7637+ /* note: access d_xxx without d_lock */
523b37e3
AM
7638+ dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7639+ bindex, dentry, dentry,
1facf9fc 7640+ dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
c1595e42 7641+ au_dcount(dentry), dentry->d_flags,
523b37e3 7642+ d_unhashed(dentry) ? "un" : "");
53392da6 7643+ hn = -1;
5afbbe0d
AM
7644+ inode = NULL;
7645+ if (d_is_positive(dentry))
7646+ inode = d_inode(dentry);
7647+ if (inode
7648+ && au_test_aufs(dentry->d_sb)
7649+ && bindex >= 0
7650+ && !au_is_bad_inode(inode)) {
7651+ iinfo = au_ii(inode);
7652+ hi = au_hinode(iinfo, bindex);
7653+ hn = !!au_hn(hi);
7654+ wh = hi->hi_whdentry;
7655+ }
7656+ do_pri_inode(bindex, inode, hn, wh);
1facf9fc 7657+ return 0;
7658+}
7659+
7660+void au_dpri_dentry(struct dentry *dentry)
7661+{
7662+ struct au_dinfo *dinfo;
7663+ aufs_bindex_t bindex;
7664+ int err;
7665+
7666+ err = do_pri_dentry(-1, dentry);
7667+ if (err || !au_test_aufs(dentry->d_sb))
7668+ return;
7669+
7670+ dinfo = au_di(dentry);
7671+ if (!dinfo)
7672+ return;
5afbbe0d
AM
7673+ dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7674+ dinfo->di_btop, dinfo->di_bbot,
38d290e6
JR
7675+ dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7676+ dinfo->di_tmpfile);
5afbbe0d 7677+ if (dinfo->di_btop < 0)
1facf9fc 7678+ return;
5afbbe0d
AM
7679+ for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
7680+ do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
1facf9fc 7681+}
7682+
7683+static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7684+{
7685+ char a[32];
7686+
7687+ if (!file || IS_ERR(file)) {
7688+ dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7689+ return -1;
7690+ }
7691+ a[0] = 0;
7692+ if (bindex < 0
b912730e 7693+ && !IS_ERR_OR_NULL(file->f_path.dentry)
2000de60 7694+ && au_test_aufs(file->f_path.dentry->d_sb)
1facf9fc 7695+ && au_fi(file))
e49829fe 7696+ snprintf(a, sizeof(a), ", gen %d, mmapped %d",
2cbb1c4b 7697+ au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
b752ccd1 7698+ dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
1facf9fc 7699+ bindex, file->f_mode, file->f_flags, (long)file_count(file),
b752ccd1 7700+ file->f_version, file->f_pos, a);
b912730e 7701+ if (!IS_ERR_OR_NULL(file->f_path.dentry))
2000de60 7702+ do_pri_dentry(bindex, file->f_path.dentry);
1facf9fc 7703+ return 0;
7704+}
7705+
7706+void au_dpri_file(struct file *file)
7707+{
7708+ struct au_finfo *finfo;
4a4d8108
AM
7709+ struct au_fidir *fidir;
7710+ struct au_hfile *hfile;
1facf9fc 7711+ aufs_bindex_t bindex;
7712+ int err;
7713+
7714+ err = do_pri_file(-1, file);
2000de60 7715+ if (err
b912730e 7716+ || IS_ERR_OR_NULL(file->f_path.dentry)
2000de60 7717+ || !au_test_aufs(file->f_path.dentry->d_sb))
1facf9fc 7718+ return;
7719+
7720+ finfo = au_fi(file);
7721+ if (!finfo)
7722+ return;
4a4d8108 7723+ if (finfo->fi_btop < 0)
1facf9fc 7724+ return;
4a4d8108
AM
7725+ fidir = finfo->fi_hdir;
7726+ if (!fidir)
7727+ do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7728+ else
e49829fe
JR
7729+ for (bindex = finfo->fi_btop;
7730+ bindex >= 0 && bindex <= fidir->fd_bbot;
4a4d8108
AM
7731+ bindex++) {
7732+ hfile = fidir->fd_hfile + bindex;
7733+ do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7734+ }
1facf9fc 7735+}
7736+
7737+static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7738+{
7739+ struct vfsmount *mnt;
7740+ struct super_block *sb;
7741+
7742+ if (!br || IS_ERR(br))
7743+ goto out;
86dc4139 7744+ mnt = au_br_mnt(br);
1facf9fc 7745+ if (!mnt || IS_ERR(mnt))
7746+ goto out;
7747+ sb = mnt->mnt_sb;
7748+ if (!sb || IS_ERR(sb))
7749+ goto out;
7750+
acd2b654 7751+ dpri("s%d: {perm 0x%x, id %d, wbr %p}, "
b752ccd1 7752+ "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
1facf9fc 7753+ "xino %d\n",
acd2b654
AM
7754+ bindex, br->br_perm, br->br_id, br->br_wbr,
7755+ au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
b752ccd1 7756+ sb->s_flags, sb->s_count,
acd2b654
AM
7757+ atomic_read(&sb->s_active),
7758+ !!au_xino_file(br->br_xino, /*idx*/-1));
1facf9fc 7759+ return 0;
7760+
4f0767ce 7761+out:
1facf9fc 7762+ dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7763+ return -1;
7764+}
7765+
7766+void au_dpri_sb(struct super_block *sb)
7767+{
7768+ struct au_sbinfo *sbinfo;
7769+ aufs_bindex_t bindex;
7770+ int err;
acd2b654 7771+ /* to reduce stack size */
1facf9fc 7772+ struct {
7773+ struct vfsmount mnt;
7774+ struct au_branch fake;
7775+ } *a;
7776+
7777+ /* this function can be called from magic sysrq */
7778+ a = kzalloc(sizeof(*a), GFP_ATOMIC);
7779+ if (unlikely(!a)) {
7780+ dpri("no memory\n");
7781+ return;
7782+ }
7783+
7784+ a->mnt.mnt_sb = sb;
86dc4139 7785+ a->fake.br_path.mnt = &a->mnt;
1facf9fc 7786+ err = do_pri_br(-1, &a->fake);
e49925d1 7787+ au_kfree_rcu(a);
1facf9fc 7788+ dpri("dev 0x%x\n", sb->s_dev);
7789+ if (err || !au_test_aufs(sb))
7790+ return;
7791+
7792+ sbinfo = au_sbi(sb);
7793+ if (!sbinfo)
7794+ return;
f0c0a007
AM
7795+ dpri("nw %d, gen %u, kobj %d\n",
7796+ atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
521ced18 7797+ kref_read(&sbinfo->si_kobj.kref));
5afbbe0d 7798+ for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
1facf9fc 7799+ do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7800+}
7801+
7802+/* ---------------------------------------------------------------------- */
7803+
027c5e7a
AM
7804+void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7805+{
5527c038 7806+ struct inode *h_inode, *inode = d_inode(dentry);
027c5e7a 7807+ struct dentry *h_dentry;
5afbbe0d 7808+ aufs_bindex_t bindex, bbot, bi;
027c5e7a
AM
7809+
7810+ if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7811+ return;
7812+
5afbbe0d
AM
7813+ bbot = au_dbbot(dentry);
7814+ bi = au_ibbot(inode);
7815+ if (bi < bbot)
7816+ bbot = bi;
7817+ bindex = au_dbtop(dentry);
7818+ bi = au_ibtop(inode);
027c5e7a
AM
7819+ if (bi > bindex)
7820+ bindex = bi;
7821+
5afbbe0d 7822+ for (; bindex <= bbot; bindex++) {
027c5e7a
AM
7823+ h_dentry = au_h_dptr(dentry, bindex);
7824+ if (!h_dentry)
7825+ continue;
7826+ h_inode = au_h_iptr(inode, bindex);
5527c038 7827+ if (unlikely(h_inode != d_inode(h_dentry))) {
392086de 7828+ au_debug_on();
027c5e7a
AM
7829+ AuDbg("b%d, %s:%d\n", bindex, func, line);
7830+ AuDbgDentry(dentry);
7831+ AuDbgInode(inode);
392086de 7832+ au_debug_off();
027c5e7a
AM
7833+ BUG();
7834+ }
7835+ }
7836+}
7837+
1facf9fc 7838+void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7839+{
7840+ int err, i, j;
7841+ struct au_dcsub_pages dpages;
7842+ struct au_dpage *dpage;
7843+ struct dentry **dentries;
7844+
7845+ err = au_dpages_init(&dpages, GFP_NOFS);
7846+ AuDebugOn(err);
027c5e7a 7847+ err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
1facf9fc 7848+ AuDebugOn(err);
7849+ for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7850+ dpage = dpages.dpages + i;
7851+ dentries = dpage->dentries;
7852+ for (j = dpage->ndentry - 1; !err && j >= 0; j--)
027c5e7a 7853+ AuDebugOn(au_digen_test(dentries[j], sigen));
1facf9fc 7854+ }
7855+ au_dpages_free(&dpages);
7856+}
7857+
1facf9fc 7858+void au_dbg_verify_kthread(void)
7859+{
53392da6 7860+ if (au_wkq_test()) {
1facf9fc 7861+ au_dbg_blocked();
1e00d052
AM
7862+ /*
7863+ * It may be recursive, but udba=notify between two aufs mounts,
7864+ * where a single ro branch is shared, is not a problem.
7865+ */
7866+ /* WARN_ON(1); */
1facf9fc 7867+ }
7868+}
7869+
7870+/* ---------------------------------------------------------------------- */
7871+
1facf9fc 7872+int __init au_debug_init(void)
7873+{
7874+ aufs_bindex_t bindex;
7875+ struct au_vdir_destr destr;
7876+
7877+ bindex = -1;
7878+ AuDebugOn(bindex >= 0);
7879+
7880+ destr.len = -1;
7881+ AuDebugOn(destr.len < NAME_MAX);
7882+
7883+#ifdef CONFIG_4KSTACKS
0c3ec466 7884+ pr_warn("CONFIG_4KSTACKS is defined.\n");
1facf9fc 7885+#endif
7886+
1facf9fc 7887+ return 0;
7888+}
7f207e10
AM
7889diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
7890--- /usr/share/empty/fs/aufs/debug.h 1970-01-01 01:00:00.000000000 +0100
0b487229 7891+++ linux/fs/aufs/debug.h 2021-02-22 23:30:37.699347580 +0100
062440b3
AM
7892@@ -0,0 +1,226 @@
7893+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 7894+/*
0b487229 7895+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 7896+ *
7897+ * This program, aufs is free software; you can redistribute it and/or modify
7898+ * it under the terms of the GNU General Public License as published by
7899+ * the Free Software Foundation; either version 2 of the License, or
7900+ * (at your option) any later version.
dece6358
AM
7901+ *
7902+ * This program is distributed in the hope that it will be useful,
7903+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7904+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7905+ * GNU General Public License for more details.
7906+ *
7907+ * You should have received a copy of the GNU General Public License
523b37e3 7908+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7909+ */
7910+
7911+/*
7912+ * debug print functions
7913+ */
7914+
7915+#ifndef __AUFS_DEBUG_H__
7916+#define __AUFS_DEBUG_H__
7917+
7918+#ifdef __KERNEL__
7919+
392086de 7920+#include <linux/atomic.h>
4a4d8108
AM
7921+#include <linux/module.h>
7922+#include <linux/kallsyms.h>
1facf9fc 7923+#include <linux/sysrq.h>
4a4d8108 7924+
1facf9fc 7925+#ifdef CONFIG_AUFS_DEBUG
7926+#define AuDebugOn(a) BUG_ON(a)
7927+
7928+/* module parameter */
392086de
AM
7929+extern atomic_t aufs_debug;
7930+static inline void au_debug_on(void)
1facf9fc 7931+{
392086de
AM
7932+ atomic_inc(&aufs_debug);
7933+}
7934+static inline void au_debug_off(void)
7935+{
7936+ atomic_dec_if_positive(&aufs_debug);
1facf9fc 7937+}
7938+
7939+static inline int au_debug_test(void)
7940+{
392086de 7941+ return atomic_read(&aufs_debug) > 0;
1facf9fc 7942+}
7943+#else
7944+#define AuDebugOn(a) do {} while (0)
392086de
AM
7945+AuStubVoid(au_debug_on, void)
7946+AuStubVoid(au_debug_off, void)
4a4d8108 7947+AuStubInt0(au_debug_test, void)
1facf9fc 7948+#endif /* CONFIG_AUFS_DEBUG */
7949+
392086de
AM
7950+#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7951+
1facf9fc 7952+/* ---------------------------------------------------------------------- */
7953+
7954+/* debug print */
7955+
4a4d8108 7956+#define AuDbg(fmt, ...) do { \
1facf9fc 7957+ if (au_debug_test()) \
4a4d8108 7958+ pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
1facf9fc 7959+} while (0)
4a4d8108
AM
7960+#define AuLabel(l) AuDbg(#l "\n")
7961+#define AuIOErr(fmt, ...) pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7962+#define AuWarn1(fmt, ...) do { \
1facf9fc 7963+ static unsigned char _c; \
7964+ if (!_c++) \
0c3ec466 7965+ pr_warn(fmt, ##__VA_ARGS__); \
1facf9fc 7966+} while (0)
7967+
4a4d8108 7968+#define AuErr1(fmt, ...) do { \
1facf9fc 7969+ static unsigned char _c; \
7970+ if (!_c++) \
4a4d8108 7971+ pr_err(fmt, ##__VA_ARGS__); \
1facf9fc 7972+} while (0)
7973+
4a4d8108 7974+#define AuIOErr1(fmt, ...) do { \
1facf9fc 7975+ static unsigned char _c; \
7976+ if (!_c++) \
4a4d8108 7977+ AuIOErr(fmt, ##__VA_ARGS__); \
1facf9fc 7978+} while (0)
7979+
7980+#define AuUnsupportMsg "This operation is not supported." \
7981+ " Please report this application to aufs-users ML."
4a4d8108
AM
7982+#define AuUnsupport(fmt, ...) do { \
7983+ pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
1facf9fc 7984+ dump_stack(); \
7985+} while (0)
7986+
7987+#define AuTraceErr(e) do { \
7988+ if (unlikely((e) < 0)) \
7989+ AuDbg("err %d\n", (int)(e)); \
7990+} while (0)
7991+
7992+#define AuTraceErrPtr(p) do { \
7993+ if (IS_ERR(p)) \
7994+ AuDbg("err %ld\n", PTR_ERR(p)); \
7995+} while (0)
7996+
7997+/* dirty macros for debug print, use with "%.*s" and caution */
7998+#define AuLNPair(qstr) (qstr)->len, (qstr)->name
1facf9fc 7999+
8000+/* ---------------------------------------------------------------------- */
8001+
dece6358 8002+struct dentry;
1facf9fc 8003+#ifdef CONFIG_AUFS_DEBUG
c1595e42 8004+extern struct mutex au_dbg_mtx;
1facf9fc 8005+extern char *au_plevel;
8006+struct au_nhash;
8007+void au_dpri_whlist(struct au_nhash *whlist);
8008+struct au_vdir;
8009+void au_dpri_vdir(struct au_vdir *vdir);
dece6358 8010+struct inode;
1facf9fc 8011+void au_dpri_inode(struct inode *inode);
2cbb1c4b 8012+void au_dpri_dalias(struct inode *inode);
1facf9fc 8013+void au_dpri_dentry(struct dentry *dentry);
dece6358 8014+struct file;
1facf9fc 8015+void au_dpri_file(struct file *filp);
dece6358 8016+struct super_block;
1facf9fc 8017+void au_dpri_sb(struct super_block *sb);
8018+
027c5e7a
AM
8019+#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
8020+void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
1facf9fc 8021+void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
1facf9fc 8022+void au_dbg_verify_kthread(void);
8023+
8024+int __init au_debug_init(void);
7e9cd9fe 8025+
1facf9fc 8026+#define AuDbgWhlist(w) do { \
c1595e42 8027+ mutex_lock(&au_dbg_mtx); \
1facf9fc 8028+ AuDbg(#w "\n"); \
8029+ au_dpri_whlist(w); \
c1595e42 8030+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 8031+} while (0)
8032+
8033+#define AuDbgVdir(v) do { \
c1595e42 8034+ mutex_lock(&au_dbg_mtx); \
1facf9fc 8035+ AuDbg(#v "\n"); \
8036+ au_dpri_vdir(v); \
c1595e42 8037+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 8038+} while (0)
8039+
8040+#define AuDbgInode(i) do { \
c1595e42 8041+ mutex_lock(&au_dbg_mtx); \
1facf9fc 8042+ AuDbg(#i "\n"); \
8043+ au_dpri_inode(i); \
c1595e42 8044+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 8045+} while (0)
8046+
2cbb1c4b 8047+#define AuDbgDAlias(i) do { \
c1595e42 8048+ mutex_lock(&au_dbg_mtx); \
2cbb1c4b
JR
8049+ AuDbg(#i "\n"); \
8050+ au_dpri_dalias(i); \
c1595e42 8051+ mutex_unlock(&au_dbg_mtx); \
2cbb1c4b
JR
8052+} while (0)
8053+
1facf9fc 8054+#define AuDbgDentry(d) do { \
c1595e42 8055+ mutex_lock(&au_dbg_mtx); \
1facf9fc 8056+ AuDbg(#d "\n"); \
8057+ au_dpri_dentry(d); \
c1595e42 8058+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 8059+} while (0)
8060+
8061+#define AuDbgFile(f) do { \
c1595e42 8062+ mutex_lock(&au_dbg_mtx); \
1facf9fc 8063+ AuDbg(#f "\n"); \
8064+ au_dpri_file(f); \
c1595e42 8065+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 8066+} while (0)
8067+
8068+#define AuDbgSb(sb) do { \
c1595e42 8069+ mutex_lock(&au_dbg_mtx); \
1facf9fc 8070+ AuDbg(#sb "\n"); \
8071+ au_dpri_sb(sb); \
c1595e42 8072+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 8073+} while (0)
8074+
4a4d8108
AM
8075+#define AuDbgSym(addr) do { \
8076+ char sym[KSYM_SYMBOL_LEN]; \
8077+ sprint_symbol(sym, (unsigned long)addr); \
8078+ AuDbg("%s\n", sym); \
8079+} while (0)
1facf9fc 8080+#else
027c5e7a 8081+AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
4a4d8108
AM
8082+AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
8083+AuStubVoid(au_dbg_verify_kthread, void)
8084+AuStubInt0(__init au_debug_init, void)
1facf9fc 8085+
1facf9fc 8086+#define AuDbgWhlist(w) do {} while (0)
8087+#define AuDbgVdir(v) do {} while (0)
8088+#define AuDbgInode(i) do {} while (0)
2cbb1c4b 8089+#define AuDbgDAlias(i) do {} while (0)
1facf9fc 8090+#define AuDbgDentry(d) do {} while (0)
8091+#define AuDbgFile(f) do {} while (0)
8092+#define AuDbgSb(sb) do {} while (0)
4a4d8108 8093+#define AuDbgSym(addr) do {} while (0)
1facf9fc 8094+#endif /* CONFIG_AUFS_DEBUG */
8095+
8096+/* ---------------------------------------------------------------------- */
8097+
8098+#ifdef CONFIG_AUFS_MAGIC_SYSRQ
8099+int __init au_sysrq_init(void);
8100+void au_sysrq_fin(void);
8101+
8102+#ifdef CONFIG_HW_CONSOLE
8103+#define au_dbg_blocked() do { \
8104+ WARN_ON(1); \
0c5527e5 8105+ handle_sysrq('w'); \
1facf9fc 8106+} while (0)
8107+#else
4a4d8108 8108+AuStubVoid(au_dbg_blocked, void)
1facf9fc 8109+#endif
8110+
8111+#else
4a4d8108
AM
8112+AuStubInt0(__init au_sysrq_init, void)
8113+AuStubVoid(au_sysrq_fin, void)
8114+AuStubVoid(au_dbg_blocked, void)
1facf9fc 8115+#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
8116+
8117+#endif /* __KERNEL__ */
8118+#endif /* __AUFS_DEBUG_H__ */
7f207e10
AM
8119diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
8120--- /usr/share/empty/fs/aufs/dentry.c 1970-01-01 01:00:00.000000000 +0100
0b487229
JR
8121+++ linux/fs/aufs/dentry.c 2021-02-22 23:30:37.699347580 +0100
8122@@ -0,0 +1,1154 @@
cd7a4cd9 8123+// SPDX-License-Identifier: GPL-2.0
1facf9fc 8124+/*
0b487229 8125+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 8126+ *
8127+ * This program, aufs is free software; you can redistribute it and/or modify
8128+ * it under the terms of the GNU General Public License as published by
8129+ * the Free Software Foundation; either version 2 of the License, or
8130+ * (at your option) any later version.
dece6358
AM
8131+ *
8132+ * This program is distributed in the hope that it will be useful,
8133+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
8134+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8135+ * GNU General Public License for more details.
8136+ *
8137+ * You should have received a copy of the GNU General Public License
523b37e3 8138+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 8139+ */
8140+
8141+/*
8142+ * lookup and dentry operations
8143+ */
8144+
0b487229 8145+#include <linux/iversion.h>
dece6358 8146+#include <linux/namei.h>
1facf9fc 8147+#include "aufs.h"
8148+
1facf9fc 8149+/*
8150+ * returns positive/negative dentry, NULL or an error.
8151+ * NULL means whiteout-ed or not-found.
8152+ */
8153+static struct dentry*
8154+au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
8b6a4947 8155+ aufs_bindex_t bindex, struct au_do_lookup_args *args)
1facf9fc 8156+{
8157+ struct dentry *h_dentry;
2000de60 8158+ struct inode *h_inode;
1facf9fc 8159+ struct au_branch *br;
8160+ int wh_found, opq;
8161+ unsigned char wh_able;
8162+ const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
076b876e
AM
8163+ const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
8164+ IGNORE_PERM);
1facf9fc 8165+
1facf9fc 8166+ wh_found = 0;
8167+ br = au_sbr(dentry->d_sb, bindex);
8168+ wh_able = !!au_br_whable(br->br_perm);
8169+ if (wh_able)
8b6a4947 8170+ wh_found = au_wh_test(h_parent, &args->whname, ignore_perm);
1facf9fc 8171+ h_dentry = ERR_PTR(wh_found);
8172+ if (!wh_found)
8173+ goto real_lookup;
8174+ if (unlikely(wh_found < 0))
8175+ goto out;
8176+
8177+ /* We found a whiteout */
5afbbe0d 8178+ /* au_set_dbbot(dentry, bindex); */
1facf9fc 8179+ au_set_dbwh(dentry, bindex);
8180+ if (!allow_neg)
8181+ return NULL; /* success */
8182+
4f0767ce 8183+real_lookup:
076b876e 8184+ if (!ignore_perm)
8b6a4947 8185+ h_dentry = vfsub_lkup_one(args->name, h_parent);
076b876e 8186+ else
8b6a4947 8187+ h_dentry = au_sio_lkup_one(args->name, h_parent);
2000de60
JR
8188+ if (IS_ERR(h_dentry)) {
8189+ if (PTR_ERR(h_dentry) == -ENAMETOOLONG
8190+ && !allow_neg)
8191+ h_dentry = NULL;
1facf9fc 8192+ goto out;
2000de60 8193+ }
1facf9fc 8194+
5527c038
JR
8195+ h_inode = d_inode(h_dentry);
8196+ if (d_is_negative(h_dentry)) {
1facf9fc 8197+ if (!allow_neg)
8198+ goto out_neg;
8199+ } else if (wh_found
8200+ || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
8201+ goto out_neg;
8b6a4947
AM
8202+ else if (au_ftest_lkup(args->flags, DIRREN)
8203+ /* && h_inode */
8204+ && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
8205+ AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
8206+ (unsigned long long)h_inode->i_ino);
8207+ goto out_neg;
8208+ }
1facf9fc 8209+
5afbbe0d
AM
8210+ if (au_dbbot(dentry) <= bindex)
8211+ au_set_dbbot(dentry, bindex);
8212+ if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
8213+ au_set_dbtop(dentry, bindex);
1facf9fc 8214+ au_set_h_dptr(dentry, bindex, h_dentry);
8215+
2000de60
JR
8216+ if (!d_is_dir(h_dentry)
8217+ || !wh_able
5527c038 8218+ || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
1facf9fc 8219+ goto out; /* success */
8220+
be118d29 8221+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
076b876e 8222+ opq = au_diropq_test(h_dentry);
3c1bdaff 8223+ inode_unlock_shared(h_inode);
1facf9fc 8224+ if (opq > 0)
8225+ au_set_dbdiropq(dentry, bindex);
8226+ else if (unlikely(opq < 0)) {
8227+ au_set_h_dptr(dentry, bindex, NULL);
8228+ h_dentry = ERR_PTR(opq);
8229+ }
8230+ goto out;
8231+
4f0767ce 8232+out_neg:
1facf9fc 8233+ dput(h_dentry);
8234+ h_dentry = NULL;
4f0767ce 8235+out:
1facf9fc 8236+ return h_dentry;
8237+}
8238+
dece6358
AM
8239+static int au_test_shwh(struct super_block *sb, const struct qstr *name)
8240+{
8241+ if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
8242+ && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
8243+ return -EPERM;
8244+ return 0;
8245+}
8246+
1facf9fc 8247+/*
8248+ * returns the number of lower positive dentries,
8249+ * otherwise an error.
8250+ * can be called at unlinking with @type is zero.
8251+ */
5afbbe0d
AM
8252+int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
8253+ unsigned int flags)
1facf9fc 8254+{
8255+ int npositive, err;
8256+ aufs_bindex_t bindex, btail, bdiropq;
8b6a4947 8257+ unsigned char isdir, dirperm1, dirren;
1facf9fc 8258+ struct au_do_lookup_args args = {
8b6a4947
AM
8259+ .flags = flags,
8260+ .name = &dentry->d_name
1facf9fc 8261+ };
1facf9fc 8262+ struct dentry *parent;
076b876e 8263+ struct super_block *sb;
1facf9fc 8264+
076b876e 8265+ sb = dentry->d_sb;
8b6a4947 8266+ err = au_test_shwh(sb, args.name);
dece6358 8267+ if (unlikely(err))
1facf9fc 8268+ goto out;
8269+
8b6a4947 8270+ err = au_wh_name_alloc(&args.whname, args.name);
1facf9fc 8271+ if (unlikely(err))
8272+ goto out;
8273+
2000de60 8274+ isdir = !!d_is_dir(dentry);
076b876e 8275+ dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
8b6a4947
AM
8276+ dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
8277+ if (dirren)
8278+ au_fset_lkup(args.flags, DIRREN);
1facf9fc 8279+
8280+ npositive = 0;
4a4d8108 8281+ parent = dget_parent(dentry);
1facf9fc 8282+ btail = au_dbtaildir(parent);
5afbbe0d 8283+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 8284+ struct dentry *h_parent, *h_dentry;
8285+ struct inode *h_inode, *h_dir;
8b6a4947 8286+ struct au_branch *br;
1facf9fc 8287+
8288+ h_dentry = au_h_dptr(dentry, bindex);
8289+ if (h_dentry) {
5527c038 8290+ if (d_is_positive(h_dentry))
1facf9fc 8291+ npositive++;
5afbbe0d 8292+ break;
1facf9fc 8293+ }
8294+ h_parent = au_h_dptr(parent, bindex);
2000de60 8295+ if (!h_parent || !d_is_dir(h_parent))
1facf9fc 8296+ continue;
8297+
8b6a4947
AM
8298+ if (dirren) {
8299+ /* if the inum matches, then use the prepared name */
8300+ err = au_dr_lkup_name(&args, bindex);
8301+ if (unlikely(err))
8302+ goto out_parent;
8303+ }
8304+
5527c038 8305+ h_dir = d_inode(h_parent);
be118d29 8306+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8b6a4947 8307+ h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
3c1bdaff 8308+ inode_unlock_shared(h_dir);
1facf9fc 8309+ err = PTR_ERR(h_dentry);
8310+ if (IS_ERR(h_dentry))
4a4d8108 8311+ goto out_parent;
2000de60
JR
8312+ if (h_dentry)
8313+ au_fclr_lkup(args.flags, ALLOW_NEG);
076b876e
AM
8314+ if (dirperm1)
8315+ au_fset_lkup(args.flags, IGNORE_PERM);
1facf9fc 8316+
79b8bda9 8317+ if (au_dbwh(dentry) == bindex)
1facf9fc 8318+ break;
8319+ if (!h_dentry)
8320+ continue;
5527c038 8321+ if (d_is_negative(h_dentry))
1facf9fc 8322+ continue;
5527c038 8323+ h_inode = d_inode(h_dentry);
1facf9fc 8324+ npositive++;
8325+ if (!args.type)
8326+ args.type = h_inode->i_mode & S_IFMT;
8327+ if (args.type != S_IFDIR)
8328+ break;
8329+ else if (isdir) {
8330+ /* the type of lower may be different */
8331+ bdiropq = au_dbdiropq(dentry);
8332+ if (bdiropq >= 0 && bdiropq <= bindex)
8333+ break;
8334+ }
8b6a4947
AM
8335+ br = au_sbr(sb, bindex);
8336+ if (dirren
8337+ && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
8338+ /*add_ent*/NULL)) {
8339+ /* prepare next name to lookup */
8340+ err = au_dr_lkup(&args, dentry, bindex);
8341+ if (unlikely(err))
8342+ goto out_parent;
8343+ }
1facf9fc 8344+ }
8345+
8346+ if (npositive) {
8347+ AuLabel(positive);
5afbbe0d 8348+ au_update_dbtop(dentry);
1facf9fc 8349+ }
8350+ err = npositive;
076b876e 8351+ if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
5afbbe0d 8352+ && au_dbtop(dentry) < 0)) {
1facf9fc 8353+ err = -EIO;
523b37e3
AM
8354+ AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
8355+ dentry, err);
027c5e7a 8356+ }
1facf9fc 8357+
4f0767ce 8358+out_parent:
4a4d8108 8359+ dput(parent);
e49925d1 8360+ au_kfree_try_rcu(args.whname.name);
8b6a4947
AM
8361+ if (dirren)
8362+ au_dr_lkup_fin(&args);
4f0767ce 8363+out:
1facf9fc 8364+ return err;
8365+}
8366+
076b876e 8367+struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent)
1facf9fc 8368+{
8369+ struct dentry *dentry;
8370+ int wkq_err;
8371+
5527c038 8372+ if (!au_test_h_perm_sio(d_inode(parent), MAY_EXEC))
b4510431 8373+ dentry = vfsub_lkup_one(name, parent);
1facf9fc 8374+ else {
b4510431
AM
8375+ struct vfsub_lkup_one_args args = {
8376+ .errp = &dentry,
8377+ .name = name,
8378+ .parent = parent
1facf9fc 8379+ };
8380+
b4510431 8381+ wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
1facf9fc 8382+ if (unlikely(wkq_err))
8383+ dentry = ERR_PTR(wkq_err);
8384+ }
8385+
8386+ return dentry;
8387+}
8388+
8389+/*
8390+ * lookup @dentry on @bindex which should be negative.
8391+ */
86dc4139 8392+int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
1facf9fc 8393+{
8394+ int err;
8395+ struct dentry *parent, *h_parent, *h_dentry;
86dc4139 8396+ struct au_branch *br;
1facf9fc 8397+
1facf9fc 8398+ parent = dget_parent(dentry);
8399+ h_parent = au_h_dptr(parent, bindex);
86dc4139
AM
8400+ br = au_sbr(dentry->d_sb, bindex);
8401+ if (wh)
8402+ h_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
8403+ else
076b876e 8404+ h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
1facf9fc 8405+ err = PTR_ERR(h_dentry);
8406+ if (IS_ERR(h_dentry))
8407+ goto out;
5527c038 8408+ if (unlikely(d_is_positive(h_dentry))) {
1facf9fc 8409+ err = -EIO;
523b37e3 8410+ AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
1facf9fc 8411+ dput(h_dentry);
8412+ goto out;
8413+ }
8414+
4a4d8108 8415+ err = 0;
5afbbe0d
AM
8416+ if (bindex < au_dbtop(dentry))
8417+ au_set_dbtop(dentry, bindex);
8418+ if (au_dbbot(dentry) < bindex)
8419+ au_set_dbbot(dentry, bindex);
1facf9fc 8420+ au_set_h_dptr(dentry, bindex, h_dentry);
1facf9fc 8421+
4f0767ce 8422+out:
1facf9fc 8423+ dput(parent);
8424+ return err;
8425+}
8426+
8427+/* ---------------------------------------------------------------------- */
8428+
8429+/* subset of struct inode */
8430+struct au_iattr {
8431+ unsigned long i_ino;
8432+ /* unsigned int i_nlink; */
0c3ec466
AM
8433+ kuid_t i_uid;
8434+ kgid_t i_gid;
1facf9fc 8435+ u64 i_version;
8436+/*
8437+ loff_t i_size;
8438+ blkcnt_t i_blocks;
8439+*/
8440+ umode_t i_mode;
8441+};
8442+
8443+static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
8444+{
8445+ ia->i_ino = h_inode->i_ino;
8446+ /* ia->i_nlink = h_inode->i_nlink; */
8447+ ia->i_uid = h_inode->i_uid;
8448+ ia->i_gid = h_inode->i_gid;
be118d29 8449+ ia->i_version = inode_query_iversion(h_inode);
1facf9fc 8450+/*
8451+ ia->i_size = h_inode->i_size;
8452+ ia->i_blocks = h_inode->i_blocks;
8453+*/
8454+ ia->i_mode = (h_inode->i_mode & S_IFMT);
8455+}
8456+
8457+static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
8458+{
8459+ return ia->i_ino != h_inode->i_ino
8460+ /* || ia->i_nlink != h_inode->i_nlink */
0c3ec466 8461+ || !uid_eq(ia->i_uid, h_inode->i_uid)
2dfbb274 8462+ || !gid_eq(ia->i_gid, h_inode->i_gid)
be118d29 8463+ || !inode_eq_iversion(h_inode, ia->i_version)
1facf9fc 8464+/*
8465+ || ia->i_size != h_inode->i_size
8466+ || ia->i_blocks != h_inode->i_blocks
8467+*/
8468+ || ia->i_mode != (h_inode->i_mode & S_IFMT);
8469+}
8470+
8471+static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
8472+ struct au_branch *br)
8473+{
8474+ int err;
8475+ struct au_iattr ia;
8476+ struct inode *h_inode;
8477+ struct dentry *h_d;
8478+ struct super_block *h_sb;
8479+
8480+ err = 0;
8481+ memset(&ia, -1, sizeof(ia));
8482+ h_sb = h_dentry->d_sb;
5527c038
JR
8483+ h_inode = NULL;
8484+ if (d_is_positive(h_dentry)) {
8485+ h_inode = d_inode(h_dentry);
1facf9fc 8486+ au_iattr_save(&ia, h_inode);
5527c038 8487+ } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
1facf9fc 8488+ /* nfs d_revalidate may return 0 for negative dentry */
8489+ /* fuse d_revalidate always return 0 for negative dentry */
8490+ goto out;
8491+
8492+ /* main purpose is namei.c:cached_lookup() and d_revalidate */
b4510431 8493+ h_d = vfsub_lkup_one(&h_dentry->d_name, h_parent);
1facf9fc 8494+ err = PTR_ERR(h_d);
8495+ if (IS_ERR(h_d))
8496+ goto out;
8497+
8498+ err = 0;
8499+ if (unlikely(h_d != h_dentry
5527c038 8500+ || d_inode(h_d) != h_inode
1facf9fc 8501+ || (h_inode && au_iattr_test(&ia, h_inode))))
8502+ err = au_busy_or_stale();
8503+ dput(h_d);
8504+
4f0767ce 8505+out:
1facf9fc 8506+ AuTraceErr(err);
8507+ return err;
8508+}
8509+
8510+int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8511+ struct dentry *h_parent, struct au_branch *br)
8512+{
8513+ int err;
8514+
8515+ err = 0;
027c5e7a
AM
8516+ if (udba == AuOpt_UDBA_REVAL
8517+ && !au_test_fs_remote(h_dentry->d_sb)) {
1facf9fc 8518+ IMustLock(h_dir);
5527c038 8519+ err = (d_inode(h_dentry->d_parent) != h_dir);
027c5e7a 8520+ } else if (udba != AuOpt_UDBA_NONE)
1facf9fc 8521+ err = au_h_verify_dentry(h_dentry, h_parent, br);
8522+
8523+ return err;
8524+}
8525+
8526+/* ---------------------------------------------------------------------- */
8527+
027c5e7a 8528+static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
1facf9fc 8529+{
027c5e7a 8530+ int err;
5afbbe0d 8531+ aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
027c5e7a
AM
8532+ struct au_hdentry tmp, *p, *q;
8533+ struct au_dinfo *dinfo;
8534+ struct super_block *sb;
1facf9fc 8535+
027c5e7a 8536+ DiMustWriteLock(dentry);
1308ab2a 8537+
027c5e7a
AM
8538+ sb = dentry->d_sb;
8539+ dinfo = au_di(dentry);
5afbbe0d 8540+ bbot = dinfo->di_bbot;
1facf9fc 8541+ bwh = dinfo->di_bwh;
8542+ bdiropq = dinfo->di_bdiropq;
5afbbe0d
AM
8543+ bindex = dinfo->di_btop;
8544+ p = au_hdentry(dinfo, bindex);
8545+ for (; bindex <= bbot; bindex++, p++) {
027c5e7a 8546+ if (!p->hd_dentry)
1facf9fc 8547+ continue;
8548+
027c5e7a
AM
8549+ new_bindex = au_br_index(sb, p->hd_id);
8550+ if (new_bindex == bindex)
1facf9fc 8551+ continue;
1facf9fc 8552+
1facf9fc 8553+ if (dinfo->di_bwh == bindex)
8554+ bwh = new_bindex;
8555+ if (dinfo->di_bdiropq == bindex)
8556+ bdiropq = new_bindex;
8557+ if (new_bindex < 0) {
8558+ au_hdput(p);
8559+ p->hd_dentry = NULL;
8560+ continue;
8561+ }
8562+
8563+ /* swap two lower dentries, and loop again */
5afbbe0d 8564+ q = au_hdentry(dinfo, new_bindex);
1facf9fc 8565+ tmp = *q;
8566+ *q = *p;
8567+ *p = tmp;
8568+ if (tmp.hd_dentry) {
8569+ bindex--;
8570+ p--;
8571+ }
8572+ }
8573+
1facf9fc 8574+ dinfo->di_bwh = -1;
5afbbe0d 8575+ if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
1facf9fc 8576+ dinfo->di_bwh = bwh;
8577+
8578+ dinfo->di_bdiropq = -1;
8579+ if (bdiropq >= 0
5afbbe0d 8580+ && bdiropq <= au_sbbot(sb)
1facf9fc 8581+ && au_sbr_whable(sb, bdiropq))
8582+ dinfo->di_bdiropq = bdiropq;
8583+
027c5e7a 8584+ err = -EIO;
5afbbe0d
AM
8585+ dinfo->di_btop = -1;
8586+ dinfo->di_bbot = -1;
8587+ bbot = au_dbbot(parent);
8588+ bindex = 0;
8589+ p = au_hdentry(dinfo, bindex);
8590+ for (; bindex <= bbot; bindex++, p++)
1facf9fc 8591+ if (p->hd_dentry) {
5afbbe0d 8592+ dinfo->di_btop = bindex;
1facf9fc 8593+ break;
8594+ }
8595+
5afbbe0d
AM
8596+ if (dinfo->di_btop >= 0) {
8597+ bindex = bbot;
8598+ p = au_hdentry(dinfo, bindex);
8599+ for (; bindex >= 0; bindex--, p--)
027c5e7a 8600+ if (p->hd_dentry) {
5afbbe0d 8601+ dinfo->di_bbot = bindex;
027c5e7a
AM
8602+ err = 0;
8603+ break;
8604+ }
8605+ }
8606+
8607+ return err;
1facf9fc 8608+}
8609+
027c5e7a 8610+static void au_do_hide(struct dentry *dentry)
1facf9fc 8611+{
027c5e7a 8612+ struct inode *inode;
1facf9fc 8613+
5527c038
JR
8614+ if (d_really_is_positive(dentry)) {
8615+ inode = d_inode(dentry);
8616+ if (!d_is_dir(dentry)) {
027c5e7a
AM
8617+ if (inode->i_nlink && !d_unhashed(dentry))
8618+ drop_nlink(inode);
8619+ } else {
8620+ clear_nlink(inode);
8621+ /* stop next lookup */
8622+ inode->i_flags |= S_DEAD;
8623+ }
8624+ smp_mb(); /* necessary? */
8625+ }
8626+ d_drop(dentry);
8627+}
1308ab2a 8628+
027c5e7a
AM
8629+static int au_hide_children(struct dentry *parent)
8630+{
8631+ int err, i, j, ndentry;
8632+ struct au_dcsub_pages dpages;
8633+ struct au_dpage *dpage;
8634+ struct dentry *dentry;
1facf9fc 8635+
027c5e7a 8636+ err = au_dpages_init(&dpages, GFP_NOFS);
1facf9fc 8637+ if (unlikely(err))
8638+ goto out;
027c5e7a
AM
8639+ err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8640+ if (unlikely(err))
8641+ goto out_dpages;
1facf9fc 8642+
027c5e7a
AM
8643+ /* in reverse order */
8644+ for (i = dpages.ndpage - 1; i >= 0; i--) {
8645+ dpage = dpages.dpages + i;
8646+ ndentry = dpage->ndentry;
8647+ for (j = ndentry - 1; j >= 0; j--) {
8648+ dentry = dpage->dentries[j];
8649+ if (dentry != parent)
8650+ au_do_hide(dentry);
8651+ }
8652+ }
1facf9fc 8653+
027c5e7a
AM
8654+out_dpages:
8655+ au_dpages_free(&dpages);
4f0767ce 8656+out:
027c5e7a 8657+ return err;
1facf9fc 8658+}
8659+
027c5e7a 8660+static void au_hide(struct dentry *dentry)
1facf9fc 8661+{
027c5e7a 8662+ int err;
1facf9fc 8663+
027c5e7a 8664+ AuDbgDentry(dentry);
2000de60 8665+ if (d_is_dir(dentry)) {
027c5e7a
AM
8666+ /* shrink_dcache_parent(dentry); */
8667+ err = au_hide_children(dentry);
8668+ if (unlikely(err))
523b37e3
AM
8669+ AuIOErr("%pd, failed hiding children, ignored %d\n",
8670+ dentry, err);
027c5e7a
AM
8671+ }
8672+ au_do_hide(dentry);
8673+}
1facf9fc 8674+
027c5e7a
AM
8675+/*
8676+ * By adding a dirty branch, a cached dentry may be affected in various ways.
8677+ *
8678+ * a dirty branch is added
8679+ * - on the top of layers
8680+ * - in the middle of layers
8681+ * - to the bottom of layers
8682+ *
8683+ * on the added branch there exists
8684+ * - a whiteout
8685+ * - a diropq
8686+ * - a same named entry
8687+ * + exist
8688+ * * negative --> positive
8689+ * * positive --> positive
8690+ * - type is unchanged
8691+ * - type is changed
8692+ * + doesn't exist
8693+ * * negative --> negative
8694+ * * positive --> negative (rejected by au_br_del() for non-dir case)
8695+ * - none
8696+ */
8697+static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8698+ struct au_dinfo *tmp)
8699+{
8700+ int err;
5afbbe0d 8701+ aufs_bindex_t bindex, bbot;
027c5e7a
AM
8702+ struct {
8703+ struct dentry *dentry;
8704+ struct inode *inode;
8705+ mode_t mode;
be52b249
AM
8706+ } orig_h, tmp_h = {
8707+ .dentry = NULL
8708+ };
027c5e7a
AM
8709+ struct au_hdentry *hd;
8710+ struct inode *inode, *h_inode;
8711+ struct dentry *h_dentry;
8712+
8713+ err = 0;
5afbbe0d 8714+ AuDebugOn(dinfo->di_btop < 0);
027c5e7a 8715+ orig_h.mode = 0;
5afbbe0d 8716+ orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
5527c038
JR
8717+ orig_h.inode = NULL;
8718+ if (d_is_positive(orig_h.dentry)) {
8719+ orig_h.inode = d_inode(orig_h.dentry);
027c5e7a 8720+ orig_h.mode = orig_h.inode->i_mode & S_IFMT;
5527c038 8721+ }
5afbbe0d
AM
8722+ if (tmp->di_btop >= 0) {
8723+ tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
5527c038
JR
8724+ if (d_is_positive(tmp_h.dentry)) {
8725+ tmp_h.inode = d_inode(tmp_h.dentry);
027c5e7a 8726+ tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
5527c038 8727+ }
027c5e7a
AM
8728+ }
8729+
5527c038
JR
8730+ inode = NULL;
8731+ if (d_really_is_positive(dentry))
8732+ inode = d_inode(dentry);
027c5e7a 8733+ if (!orig_h.inode) {
acd2b654 8734+ AuDbg("negative originally\n");
027c5e7a
AM
8735+ if (inode) {
8736+ au_hide(dentry);
8737+ goto out;
8738+ }
8739+ AuDebugOn(inode);
5afbbe0d 8740+ AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
027c5e7a
AM
8741+ AuDebugOn(dinfo->di_bdiropq != -1);
8742+
8743+ if (!tmp_h.inode) {
8744+ AuDbg("negative --> negative\n");
8745+ /* should have only one negative lower */
5afbbe0d
AM
8746+ if (tmp->di_btop >= 0
8747+ && tmp->di_btop < dinfo->di_btop) {
8748+ AuDebugOn(tmp->di_btop != tmp->di_bbot);
8749+ AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8750+ au_set_h_dptr(dentry, dinfo->di_btop, NULL);
027c5e7a 8751+ au_di_cp(dinfo, tmp);
5afbbe0d
AM
8752+ hd = au_hdentry(tmp, tmp->di_btop);
8753+ au_set_h_dptr(dentry, tmp->di_btop,
027c5e7a
AM
8754+ dget(hd->hd_dentry));
8755+ }
8756+ au_dbg_verify_dinode(dentry);
8757+ } else {
8758+ AuDbg("negative --> positive\n");
8759+ /*
8760+ * similar to the behaviour of creating with bypassing
8761+ * aufs.
8762+ * unhash it in order to force an error in the
8763+ * succeeding create operation.
8764+ * we should not set S_DEAD here.
8765+ */
8766+ d_drop(dentry);
8767+ /* au_di_swap(tmp, dinfo); */
8768+ au_dbg_verify_dinode(dentry);
8769+ }
8770+ } else {
8771+ AuDbg("positive originally\n");
8772+ /* inode may be NULL */
8773+ AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8774+ if (!tmp_h.inode) {
8775+ AuDbg("positive --> negative\n");
8776+ /* or bypassing aufs */
8777+ au_hide(dentry);
5afbbe0d 8778+ if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
027c5e7a
AM
8779+ dinfo->di_bwh = tmp->di_bwh;
8780+ if (inode)
8781+ err = au_refresh_hinode_self(inode);
8782+ au_dbg_verify_dinode(dentry);
8783+ } else if (orig_h.mode == tmp_h.mode) {
8784+ AuDbg("positive --> positive, same type\n");
8785+ if (!S_ISDIR(orig_h.mode)
5afbbe0d 8786+ && dinfo->di_btop > tmp->di_btop) {
027c5e7a
AM
8787+ /*
8788+ * similar to the behaviour of removing and
8789+ * creating.
8790+ */
8791+ au_hide(dentry);
8792+ if (inode)
8793+ err = au_refresh_hinode_self(inode);
8794+ au_dbg_verify_dinode(dentry);
8795+ } else {
8796+ /* fill empty slots */
5afbbe0d
AM
8797+ if (dinfo->di_btop > tmp->di_btop)
8798+ dinfo->di_btop = tmp->di_btop;
8799+ if (dinfo->di_bbot < tmp->di_bbot)
8800+ dinfo->di_bbot = tmp->di_bbot;
027c5e7a
AM
8801+ dinfo->di_bwh = tmp->di_bwh;
8802+ dinfo->di_bdiropq = tmp->di_bdiropq;
5afbbe0d
AM
8803+ bbot = dinfo->di_bbot;
8804+ bindex = tmp->di_btop;
8805+ hd = au_hdentry(tmp, bindex);
8806+ for (; bindex <= bbot; bindex++, hd++) {
027c5e7a
AM
8807+ if (au_h_dptr(dentry, bindex))
8808+ continue;
5afbbe0d 8809+ h_dentry = hd->hd_dentry;
027c5e7a
AM
8810+ if (!h_dentry)
8811+ continue;
5527c038
JR
8812+ AuDebugOn(d_is_negative(h_dentry));
8813+ h_inode = d_inode(h_dentry);
027c5e7a
AM
8814+ AuDebugOn(orig_h.mode
8815+ != (h_inode->i_mode
8816+ & S_IFMT));
8817+ au_set_h_dptr(dentry, bindex,
8818+ dget(h_dentry));
8819+ }
5afbbe0d
AM
8820+ if (inode)
8821+ err = au_refresh_hinode(inode, dentry);
027c5e7a
AM
8822+ au_dbg_verify_dinode(dentry);
8823+ }
8824+ } else {
8825+ AuDbg("positive --> positive, different type\n");
8826+ /* similar to the behaviour of removing and creating */
8827+ au_hide(dentry);
8828+ if (inode)
8829+ err = au_refresh_hinode_self(inode);
8830+ au_dbg_verify_dinode(dentry);
8831+ }
8832+ }
8833+
8834+out:
8835+ return err;
8836+}
8837+
79b8bda9
AM
8838+void au_refresh_dop(struct dentry *dentry, int force_reval)
8839+{
8840+ const struct dentry_operations *dop
8841+ = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8842+ static const unsigned int mask
8843+ = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8844+
8845+ BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8846+
8847+ if (dentry->d_op == dop)
8848+ return;
8849+
8850+ AuDbg("%pd\n", dentry);
8851+ spin_lock(&dentry->d_lock);
8852+ if (dop == &aufs_dop)
8853+ dentry->d_flags |= mask;
8854+ else
8855+ dentry->d_flags &= ~mask;
8856+ dentry->d_op = dop;
8857+ spin_unlock(&dentry->d_lock);
8858+}
8859+
027c5e7a
AM
8860+int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8861+{
e2f27e51 8862+ int err, ebrange, nbr;
027c5e7a
AM
8863+ unsigned int sigen;
8864+ struct au_dinfo *dinfo, *tmp;
8865+ struct super_block *sb;
8866+ struct inode *inode;
8867+
8868+ DiMustWriteLock(dentry);
8869+ AuDebugOn(IS_ROOT(dentry));
5527c038 8870+ AuDebugOn(d_really_is_negative(parent));
027c5e7a
AM
8871+
8872+ sb = dentry->d_sb;
027c5e7a
AM
8873+ sigen = au_sigen(sb);
8874+ err = au_digen_test(parent, sigen);
8875+ if (unlikely(err))
8876+ goto out;
8877+
e2f27e51 8878+ nbr = au_sbbot(sb) + 1;
027c5e7a 8879+ dinfo = au_di(dentry);
e2f27e51 8880+ err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
027c5e7a
AM
8881+ if (unlikely(err))
8882+ goto out;
8883+ ebrange = au_dbrange_test(dentry);
8884+ if (!ebrange)
8885+ ebrange = au_do_refresh_hdentry(dentry, parent);
8886+
38d290e6 8887+ if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
5afbbe0d 8888+ AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
5527c038
JR
8889+ if (d_really_is_positive(dentry)) {
8890+ inode = d_inode(dentry);
027c5e7a 8891+ err = au_refresh_hinode_self(inode);
5527c038 8892+ }
027c5e7a
AM
8893+ au_dbg_verify_dinode(dentry);
8894+ if (!err)
8895+ goto out_dgen; /* success */
8896+ goto out;
8897+ }
8898+
8899+ /* temporary dinfo */
8900+ AuDbgDentry(dentry);
8901+ err = -ENOMEM;
8902+ tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8903+ if (unlikely(!tmp))
8904+ goto out;
8905+ au_di_swap(tmp, dinfo);
8906+ /* returns the number of positive dentries */
8907+ /*
8908+ * if current working dir is removed, it returns an error.
8909+ * but the dentry is legal.
8910+ */
5afbbe0d 8911+ err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
027c5e7a
AM
8912+ AuDbgDentry(dentry);
8913+ au_di_swap(tmp, dinfo);
8914+ if (err == -ENOENT)
8915+ err = 0;
8916+ if (err >= 0) {
8917+ /* compare/refresh by dinfo */
8918+ AuDbgDentry(dentry);
8919+ err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8920+ au_dbg_verify_dinode(dentry);
8921+ AuTraceErr(err);
8922+ }
e2f27e51 8923+ au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
027c5e7a
AM
8924+ au_rw_write_unlock(&tmp->di_rwsem);
8925+ au_di_free(tmp);
8926+ if (unlikely(err))
8927+ goto out;
8928+
8929+out_dgen:
8930+ au_update_digen(dentry);
8931+out:
8932+ if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
523b37e3 8933+ AuIOErr("failed refreshing %pd, %d\n", dentry, err);
027c5e7a
AM
8934+ AuDbgDentry(dentry);
8935+ }
8936+ AuTraceErr(err);
8937+ return err;
8938+}
8939+
b4510431
AM
8940+static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8941+ struct dentry *dentry, aufs_bindex_t bindex)
027c5e7a
AM
8942+{
8943+ int err, valid;
027c5e7a
AM
8944+
8945+ err = 0;
8946+ if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8947+ goto out;
027c5e7a
AM
8948+
8949+ AuDbg("b%d\n", bindex);
b4510431
AM
8950+ /*
8951+ * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8952+ * due to whiteout and branch permission.
8953+ */
8954+ flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8955+ | LOOKUP_FOLLOW | LOOKUP_EXCL);
8956+ /* it may return tri-state */
8957+ valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
1facf9fc 8958+
8959+ if (unlikely(valid < 0))
8960+ err = valid;
8961+ else if (!valid)
8962+ err = -EINVAL;
8963+
4f0767ce 8964+out:
1facf9fc 8965+ AuTraceErr(err);
8966+ return err;
8967+}
8968+
8969+/* todo: remove this */
8970+static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8b6a4947 8971+ unsigned int flags, int do_udba, int dirren)
1facf9fc 8972+{
8973+ int err;
8974+ umode_t mode, h_mode;
5afbbe0d 8975+ aufs_bindex_t bindex, btail, btop, ibs, ibe;
38d290e6 8976+ unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
4a4d8108 8977+ struct inode *h_inode, *h_cached_inode;
1facf9fc 8978+ struct dentry *h_dentry;
8979+ struct qstr *name, *h_name;
8980+
8981+ err = 0;
8982+ plus = 0;
8983+ mode = 0;
1facf9fc 8984+ ibs = -1;
8985+ ibe = -1;
8986+ unhashed = !!d_unhashed(dentry);
8987+ is_root = !!IS_ROOT(dentry);
8988+ name = &dentry->d_name;
38d290e6 8989+ tmpfile = au_di(dentry)->di_tmpfile;
1facf9fc 8990+
8991+ /*
7f207e10
AM
8992+ * Theoretically, REVAL test should be unnecessary in case of
8993+ * {FS,I}NOTIFY.
8994+ * But {fs,i}notify doesn't fire some necessary events,
1facf9fc 8995+ * IN_ATTRIB for atime/nlink/pageio
1facf9fc 8996+ * Let's do REVAL test too.
8997+ */
8998+ if (do_udba && inode) {
8999+ mode = (inode->i_mode & S_IFMT);
9000+ plus = (inode->i_nlink > 0);
5afbbe0d
AM
9001+ ibs = au_ibtop(inode);
9002+ ibe = au_ibbot(inode);
1facf9fc 9003+ }
9004+
5afbbe0d
AM
9005+ btop = au_dbtop(dentry);
9006+ btail = btop;
1facf9fc 9007+ if (inode && S_ISDIR(inode->i_mode))
9008+ btail = au_dbtaildir(dentry);
5afbbe0d 9009+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 9010+ h_dentry = au_h_dptr(dentry, bindex);
9011+ if (!h_dentry)
9012+ continue;
9013+
523b37e3
AM
9014+ AuDbg("b%d, %pd\n", bindex, h_dentry);
9015+ h_nfs = !!au_test_nfs(h_dentry->d_sb);
027c5e7a 9016+ spin_lock(&h_dentry->d_lock);
1facf9fc 9017+ h_name = &h_dentry->d_name;
9018+ if (unlikely(do_udba
9019+ && !is_root
523b37e3
AM
9020+ && ((!h_nfs
9021+ && (unhashed != !!d_unhashed(h_dentry)
8b6a4947 9022+ || (!tmpfile && !dirren
38d290e6
JR
9023+ && !au_qstreq(name, h_name))
9024+ ))
523b37e3
AM
9025+ || (h_nfs
9026+ && !(flags & LOOKUP_OPEN)
9027+ && (h_dentry->d_flags
9028+ & DCACHE_NFSFS_RENAMED)))
1facf9fc 9029+ )) {
38d290e6
JR
9030+ int h_unhashed;
9031+
9032+ h_unhashed = d_unhashed(h_dentry);
027c5e7a 9033+ spin_unlock(&h_dentry->d_lock);
38d290e6
JR
9034+ AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
9035+ unhashed, h_unhashed, dentry, h_dentry);
1facf9fc 9036+ goto err;
9037+ }
027c5e7a 9038+ spin_unlock(&h_dentry->d_lock);
1facf9fc 9039+
b4510431 9040+ err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
1facf9fc 9041+ if (unlikely(err))
9042+ /* do not goto err, to keep the errno */
9043+ break;
9044+
9045+ /* todo: plink too? */
9046+ if (!do_udba)
9047+ continue;
9048+
9049+ /* UDBA tests */
5527c038 9050+ if (unlikely(!!inode != d_is_positive(h_dentry)))
1facf9fc 9051+ goto err;
9052+
5527c038
JR
9053+ h_inode = NULL;
9054+ if (d_is_positive(h_dentry))
9055+ h_inode = d_inode(h_dentry);
1facf9fc 9056+ h_plus = plus;
9057+ h_mode = mode;
9058+ h_cached_inode = h_inode;
9059+ if (h_inode) {
9060+ h_mode = (h_inode->i_mode & S_IFMT);
9061+ h_plus = (h_inode->i_nlink > 0);
9062+ }
9063+ if (inode && ibs <= bindex && bindex <= ibe)
9064+ h_cached_inode = au_h_iptr(inode, bindex);
9065+
523b37e3 9066+ if (!h_nfs) {
38d290e6 9067+ if (unlikely(plus != h_plus && !tmpfile))
523b37e3
AM
9068+ goto err;
9069+ } else {
9070+ if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
9071+ && !is_root
9072+ && !IS_ROOT(h_dentry)
9073+ && unhashed != d_unhashed(h_dentry)))
9074+ goto err;
9075+ }
9076+ if (unlikely(mode != h_mode
1facf9fc 9077+ || h_cached_inode != h_inode))
9078+ goto err;
9079+ continue;
9080+
f6b6e03d 9081+err:
1facf9fc 9082+ err = -EINVAL;
9083+ break;
9084+ }
9085+
523b37e3 9086+ AuTraceErr(err);
1facf9fc 9087+ return err;
9088+}
9089+
027c5e7a 9090+/* todo: consolidate with do_refresh() and au_reval_for_attr() */
1facf9fc 9091+static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
9092+{
9093+ int err;
9094+ struct dentry *parent;
1facf9fc 9095+
027c5e7a 9096+ if (!au_digen_test(dentry, sigen))
1facf9fc 9097+ return 0;
9098+
9099+ parent = dget_parent(dentry);
9100+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 9101+ AuDebugOn(au_digen_test(parent, sigen));
1facf9fc 9102+ au_dbg_verify_gen(parent, sigen);
027c5e7a 9103+ err = au_refresh_dentry(dentry, parent);
1facf9fc 9104+ di_read_unlock(parent, AuLock_IR);
9105+ dput(parent);
027c5e7a 9106+ AuTraceErr(err);
1facf9fc 9107+ return err;
9108+}
9109+
9110+int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
9111+{
9112+ int err;
9113+ struct dentry *d, *parent;
1facf9fc 9114+
027c5e7a 9115+ if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
1facf9fc 9116+ return simple_reval_dpath(dentry, sigen);
9117+
9118+ /* slow loop, keep it simple and stupid */
9119+ /* cf: au_cpup_dirs() */
9120+ err = 0;
9121+ parent = NULL;
027c5e7a 9122+ while (au_digen_test(dentry, sigen)) {
1facf9fc 9123+ d = dentry;
9124+ while (1) {
9125+ dput(parent);
9126+ parent = dget_parent(d);
027c5e7a 9127+ if (!au_digen_test(parent, sigen))
1facf9fc 9128+ break;
9129+ d = parent;
9130+ }
9131+
1facf9fc 9132+ if (d != dentry)
027c5e7a 9133+ di_write_lock_child2(d);
1facf9fc 9134+
9135+ /* someone might update our dentry while we were sleeping */
027c5e7a
AM
9136+ if (au_digen_test(d, sigen)) {
9137+ /*
9138+ * todo: consolidate with simple_reval_dpath(),
9139+ * do_refresh() and au_reval_for_attr().
9140+ */
1facf9fc 9141+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 9142+ err = au_refresh_dentry(d, parent);
1facf9fc 9143+ di_read_unlock(parent, AuLock_IR);
9144+ }
9145+
9146+ if (d != dentry)
9147+ di_write_unlock(d);
9148+ dput(parent);
9149+ if (unlikely(err))
9150+ break;
9151+ }
9152+
9153+ return err;
9154+}
9155+
9156+/*
9157+ * if valid returns 1, otherwise 0.
9158+ */
b4510431 9159+static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
1facf9fc 9160+{
9161+ int valid, err;
9162+ unsigned int sigen;
8b6a4947 9163+ unsigned char do_udba, dirren;
1facf9fc 9164+ struct super_block *sb;
9165+ struct inode *inode;
9166+
027c5e7a 9167+ /* todo: support rcu-walk? */
b4510431 9168+ if (flags & LOOKUP_RCU)
027c5e7a
AM
9169+ return -ECHILD;
9170+
9171+ valid = 0;
9172+ if (unlikely(!au_di(dentry)))
9173+ goto out;
9174+
e49829fe 9175+ valid = 1;
1facf9fc 9176+ sb = dentry->d_sb;
e49829fe
JR
9177+ /*
9178+ * todo: very ugly
9179+ * i_mutex of parent dir may be held,
9180+ * but we should not return 'invalid' due to busy.
9181+ */
9182+ err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
9183+ if (unlikely(err)) {
9184+ valid = err;
027c5e7a 9185+ AuTraceErr(err);
e49829fe
JR
9186+ goto out;
9187+ }
5527c038
JR
9188+ inode = NULL;
9189+ if (d_really_is_positive(dentry))
9190+ inode = d_inode(dentry);
5afbbe0d 9191+ if (unlikely(inode && au_is_bad_inode(inode))) {
c1595e42
JR
9192+ err = -EINVAL;
9193+ AuTraceErr(err);
9194+ goto out_dgrade;
9195+ }
027c5e7a
AM
9196+ if (unlikely(au_dbrange_test(dentry))) {
9197+ err = -EINVAL;
9198+ AuTraceErr(err);
9199+ goto out_dgrade;
1facf9fc 9200+ }
027c5e7a
AM
9201+
9202+ sigen = au_sigen(sb);
9203+ if (au_digen_test(dentry, sigen)) {
1facf9fc 9204+ AuDebugOn(IS_ROOT(dentry));
027c5e7a
AM
9205+ err = au_reval_dpath(dentry, sigen);
9206+ if (unlikely(err)) {
9207+ AuTraceErr(err);
1facf9fc 9208+ goto out_dgrade;
027c5e7a 9209+ }
1facf9fc 9210+ }
9211+ di_downgrade_lock(dentry, AuLock_IR);
9212+
1facf9fc 9213+ err = -EINVAL;
c1595e42 9214+ if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
523b37e3 9215+ && inode
38d290e6 9216+ && !(inode->i_state && I_LINKABLE)
79b8bda9
AM
9217+ && (IS_DEADDIR(inode) || !inode->i_nlink)) {
9218+ AuTraceErr(err);
027c5e7a 9219+ goto out_inval;
79b8bda9 9220+ }
027c5e7a 9221+
1facf9fc 9222+ do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
9223+ if (do_udba && inode) {
5afbbe0d 9224+ aufs_bindex_t btop = au_ibtop(inode);
027c5e7a 9225+ struct inode *h_inode;
1facf9fc 9226+
5afbbe0d
AM
9227+ if (btop >= 0) {
9228+ h_inode = au_h_iptr(inode, btop);
79b8bda9
AM
9229+ if (h_inode && au_test_higen(inode, h_inode)) {
9230+ AuTraceErr(err);
027c5e7a 9231+ goto out_inval;
79b8bda9 9232+ }
027c5e7a 9233+ }
1facf9fc 9234+ }
9235+
8b6a4947
AM
9236+ dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
9237+ err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
5afbbe0d 9238+ if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
1facf9fc 9239+ err = -EIO;
523b37e3
AM
9240+ AuDbg("both of real entry and whiteout found, %p, err %d\n",
9241+ dentry, err);
027c5e7a 9242+ }
e49829fe 9243+ goto out_inval;
1facf9fc 9244+
4f0767ce 9245+out_dgrade:
1facf9fc 9246+ di_downgrade_lock(dentry, AuLock_IR);
e49829fe 9247+out_inval:
1facf9fc 9248+ aufs_read_unlock(dentry, AuLock_IR);
9249+ AuTraceErr(err);
9250+ valid = !err;
e49829fe 9251+out:
027c5e7a 9252+ if (!valid) {
523b37e3 9253+ AuDbg("%pd invalid, %d\n", dentry, valid);
027c5e7a
AM
9254+ d_drop(dentry);
9255+ }
1facf9fc 9256+ return valid;
9257+}
9258+
9259+static void aufs_d_release(struct dentry *dentry)
9260+{
027c5e7a 9261+ if (au_di(dentry)) {
4a4d8108
AM
9262+ au_di_fin(dentry);
9263+ au_hn_di_reinit(dentry);
1facf9fc 9264+ }
1facf9fc 9265+}
9266+
4a4d8108 9267+const struct dentry_operations aufs_dop = {
c06a8ce3
AM
9268+ .d_revalidate = aufs_d_revalidate,
9269+ .d_weak_revalidate = aufs_d_revalidate,
9270+ .d_release = aufs_d_release
1facf9fc 9271+};
79b8bda9
AM
9272+
9273+/* aufs_dop without d_revalidate */
9274+const struct dentry_operations aufs_dop_noreval = {
9275+ .d_release = aufs_d_release
9276+};
7f207e10
AM
9277diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
9278--- /usr/share/empty/fs/aufs/dentry.h 1970-01-01 01:00:00.000000000 +0100
0b487229 9279+++ linux/fs/aufs/dentry.h 2021-02-22 23:30:37.699347580 +0100
e49925d1 9280@@ -0,0 +1,268 @@
062440b3 9281+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 9282+/*
0b487229 9283+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 9284+ *
9285+ * This program, aufs is free software; you can redistribute it and/or modify
9286+ * it under the terms of the GNU General Public License as published by
9287+ * the Free Software Foundation; either version 2 of the License, or
9288+ * (at your option) any later version.
dece6358
AM
9289+ *
9290+ * This program is distributed in the hope that it will be useful,
9291+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9292+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9293+ * GNU General Public License for more details.
9294+ *
9295+ * You should have received a copy of the GNU General Public License
523b37e3 9296+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 9297+ */
9298+
9299+/*
9300+ * lookup and dentry operations
9301+ */
9302+
9303+#ifndef __AUFS_DENTRY_H__
9304+#define __AUFS_DENTRY_H__
9305+
9306+#ifdef __KERNEL__
9307+
dece6358 9308+#include <linux/dcache.h>
8b6a4947 9309+#include "dirren.h"
1facf9fc 9310+#include "rwsem.h"
9311+
1facf9fc 9312+struct au_hdentry {
9313+ struct dentry *hd_dentry;
027c5e7a 9314+ aufs_bindex_t hd_id;
1facf9fc 9315+};
9316+
9317+struct au_dinfo {
9318+ atomic_t di_generation;
9319+
dece6358 9320+ struct au_rwsem di_rwsem;
5afbbe0d 9321+ aufs_bindex_t di_btop, di_bbot, di_bwh, di_bdiropq;
38d290e6 9322+ unsigned char di_tmpfile; /* to allow the different name */
1c60b727 9323+ struct au_hdentry *di_hdentry;
e49925d1 9324+ struct rcu_head rcu;
4a4d8108 9325+} ____cacheline_aligned_in_smp;
1facf9fc 9326+
9327+/* ---------------------------------------------------------------------- */
9328+
5afbbe0d
AM
9329+/* flags for au_lkup_dentry() */
9330+#define AuLkup_ALLOW_NEG 1
9331+#define AuLkup_IGNORE_PERM (1 << 1)
8b6a4947 9332+#define AuLkup_DIRREN (1 << 2)
5afbbe0d
AM
9333+#define au_ftest_lkup(flags, name) ((flags) & AuLkup_##name)
9334+#define au_fset_lkup(flags, name) \
9335+ do { (flags) |= AuLkup_##name; } while (0)
9336+#define au_fclr_lkup(flags, name) \
9337+ do { (flags) &= ~AuLkup_##name; } while (0)
9338+
8b6a4947
AM
9339+#ifndef CONFIG_AUFS_DIRREN
9340+#undef AuLkup_DIRREN
9341+#define AuLkup_DIRREN 0
9342+#endif
9343+
9344+struct au_do_lookup_args {
9345+ unsigned int flags;
9346+ mode_t type;
9347+ struct qstr whname, *name;
9348+ struct au_dr_lookup dirren;
9349+};
9350+
5afbbe0d
AM
9351+/* ---------------------------------------------------------------------- */
9352+
1facf9fc 9353+/* dentry.c */
79b8bda9 9354+extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
1facf9fc 9355+struct au_branch;
076b876e 9356+struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent);
1facf9fc 9357+int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
9358+ struct dentry *h_parent, struct au_branch *br);
9359+
5afbbe0d
AM
9360+int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
9361+ unsigned int flags);
86dc4139 9362+int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
027c5e7a 9363+int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
1facf9fc 9364+int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
79b8bda9 9365+void au_refresh_dop(struct dentry *dentry, int force_reval);
1facf9fc 9366+
9367+/* dinfo.c */
4a4d8108 9368+void au_di_init_once(void *_di);
027c5e7a
AM
9369+struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
9370+void au_di_free(struct au_dinfo *dinfo);
9371+void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
9372+void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
4a4d8108
AM
9373+int au_di_init(struct dentry *dentry);
9374+void au_di_fin(struct dentry *dentry);
e2f27e51 9375+int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
1facf9fc 9376+
9377+void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
9378+void di_read_unlock(struct dentry *d, int flags);
9379+void di_downgrade_lock(struct dentry *d, int flags);
9380+void di_write_lock(struct dentry *d, unsigned int lsc);
9381+void di_write_unlock(struct dentry *d);
9382+void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
9383+void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
9384+void di_write_unlock2(struct dentry *d1, struct dentry *d2);
9385+
9386+struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
2cbb1c4b 9387+struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
1facf9fc 9388+aufs_bindex_t au_dbtail(struct dentry *dentry);
9389+aufs_bindex_t au_dbtaildir(struct dentry *dentry);
9390+
9391+void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9392+ struct dentry *h_dentry);
027c5e7a
AM
9393+int au_digen_test(struct dentry *dentry, unsigned int sigen);
9394+int au_dbrange_test(struct dentry *dentry);
1facf9fc 9395+void au_update_digen(struct dentry *dentry);
9396+void au_update_dbrange(struct dentry *dentry, int do_put_zero);
5afbbe0d
AM
9397+void au_update_dbtop(struct dentry *dentry);
9398+void au_update_dbbot(struct dentry *dentry);
1facf9fc 9399+int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
9400+
9401+/* ---------------------------------------------------------------------- */
9402+
9403+static inline struct au_dinfo *au_di(struct dentry *dentry)
9404+{
9405+ return dentry->d_fsdata;
9406+}
9407+
9408+/* ---------------------------------------------------------------------- */
9409+
9410+/* lock subclass for dinfo */
9411+enum {
9412+ AuLsc_DI_CHILD, /* child first */
4a4d8108 9413+ AuLsc_DI_CHILD2, /* rename(2), link(2), and cpup at hnotify */
1facf9fc 9414+ AuLsc_DI_CHILD3, /* copyup dirs */
9415+ AuLsc_DI_PARENT,
9416+ AuLsc_DI_PARENT2,
027c5e7a
AM
9417+ AuLsc_DI_PARENT3,
9418+ AuLsc_DI_TMP /* temp for replacing dinfo */
1facf9fc 9419+};
9420+
9421+/*
9422+ * di_read_lock_child, di_write_lock_child,
9423+ * di_read_lock_child2, di_write_lock_child2,
9424+ * di_read_lock_child3, di_write_lock_child3,
9425+ * di_read_lock_parent, di_write_lock_parent,
9426+ * di_read_lock_parent2, di_write_lock_parent2,
9427+ * di_read_lock_parent3, di_write_lock_parent3,
9428+ */
9429+#define AuReadLockFunc(name, lsc) \
9430+static inline void di_read_lock_##name(struct dentry *d, int flags) \
9431+{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
9432+
9433+#define AuWriteLockFunc(name, lsc) \
9434+static inline void di_write_lock_##name(struct dentry *d) \
9435+{ di_write_lock(d, AuLsc_DI_##lsc); }
9436+
9437+#define AuRWLockFuncs(name, lsc) \
9438+ AuReadLockFunc(name, lsc) \
9439+ AuWriteLockFunc(name, lsc)
9440+
9441+AuRWLockFuncs(child, CHILD);
9442+AuRWLockFuncs(child2, CHILD2);
9443+AuRWLockFuncs(child3, CHILD3);
9444+AuRWLockFuncs(parent, PARENT);
9445+AuRWLockFuncs(parent2, PARENT2);
9446+AuRWLockFuncs(parent3, PARENT3);
9447+
9448+#undef AuReadLockFunc
9449+#undef AuWriteLockFunc
9450+#undef AuRWLockFuncs
9451+
9452+#define DiMustNoWaiters(d) AuRwMustNoWaiters(&au_di(d)->di_rwsem)
dece6358
AM
9453+#define DiMustAnyLock(d) AuRwMustAnyLock(&au_di(d)->di_rwsem)
9454+#define DiMustWriteLock(d) AuRwMustWriteLock(&au_di(d)->di_rwsem)
1facf9fc 9455+
9456+/* ---------------------------------------------------------------------- */
9457+
9458+/* todo: memory barrier? */
9459+static inline unsigned int au_digen(struct dentry *d)
9460+{
9461+ return atomic_read(&au_di(d)->di_generation);
9462+}
9463+
9464+static inline void au_h_dentry_init(struct au_hdentry *hdentry)
9465+{
9466+ hdentry->hd_dentry = NULL;
9467+}
9468+
5afbbe0d
AM
9469+static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
9470+ aufs_bindex_t bindex)
9471+{
9472+ return di->di_hdentry + bindex;
9473+}
9474+
1facf9fc 9475+static inline void au_hdput(struct au_hdentry *hd)
9476+{
4a4d8108
AM
9477+ if (hd)
9478+ dput(hd->hd_dentry);
1facf9fc 9479+}
9480+
5afbbe0d 9481+static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
1facf9fc 9482+{
1308ab2a 9483+ DiMustAnyLock(dentry);
5afbbe0d 9484+ return au_di(dentry)->di_btop;
1facf9fc 9485+}
9486+
5afbbe0d 9487+static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
1facf9fc 9488+{
1308ab2a 9489+ DiMustAnyLock(dentry);
5afbbe0d 9490+ return au_di(dentry)->di_bbot;
1facf9fc 9491+}
9492+
9493+static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
9494+{
1308ab2a 9495+ DiMustAnyLock(dentry);
1facf9fc 9496+ return au_di(dentry)->di_bwh;
9497+}
9498+
9499+static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
9500+{
1308ab2a 9501+ DiMustAnyLock(dentry);
1facf9fc 9502+ return au_di(dentry)->di_bdiropq;
9503+}
9504+
9505+/* todo: hard/soft set? */
5afbbe0d 9506+static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
1facf9fc 9507+{
1308ab2a 9508+ DiMustWriteLock(dentry);
5afbbe0d 9509+ au_di(dentry)->di_btop = bindex;
1facf9fc 9510+}
9511+
5afbbe0d 9512+static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
1facf9fc 9513+{
1308ab2a 9514+ DiMustWriteLock(dentry);
5afbbe0d 9515+ au_di(dentry)->di_bbot = bindex;
1facf9fc 9516+}
9517+
9518+static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
9519+{
1308ab2a 9520+ DiMustWriteLock(dentry);
5afbbe0d 9521+ /* dbwh can be outside of btop - bbot range */
1facf9fc 9522+ au_di(dentry)->di_bwh = bindex;
9523+}
9524+
9525+static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
9526+{
1308ab2a 9527+ DiMustWriteLock(dentry);
1facf9fc 9528+ au_di(dentry)->di_bdiropq = bindex;
9529+}
9530+
9531+/* ---------------------------------------------------------------------- */
9532+
4a4d8108 9533+#ifdef CONFIG_AUFS_HNOTIFY
1facf9fc 9534+static inline void au_digen_dec(struct dentry *d)
9535+{
e49829fe 9536+ atomic_dec(&au_di(d)->di_generation);
1facf9fc 9537+}
9538+
4a4d8108 9539+static inline void au_hn_di_reinit(struct dentry *dentry)
1facf9fc 9540+{
9541+ dentry->d_fsdata = NULL;
9542+}
9543+#else
4a4d8108
AM
9544+AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
9545+#endif /* CONFIG_AUFS_HNOTIFY */
1facf9fc 9546+
9547+#endif /* __KERNEL__ */
9548+#endif /* __AUFS_DENTRY_H__ */
7f207e10
AM
9549diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
9550--- /usr/share/empty/fs/aufs/dinfo.c 1970-01-01 01:00:00.000000000 +0100
0b487229 9551+++ linux/fs/aufs/dinfo.c 2021-02-22 23:30:37.699347580 +0100
062440b3 9552@@ -0,0 +1,554 @@
cd7a4cd9 9553+// SPDX-License-Identifier: GPL-2.0
1facf9fc 9554+/*
0b487229 9555+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 9556+ *
9557+ * This program, aufs is free software; you can redistribute it and/or modify
9558+ * it under the terms of the GNU General Public License as published by
9559+ * the Free Software Foundation; either version 2 of the License, or
9560+ * (at your option) any later version.
dece6358
AM
9561+ *
9562+ * This program is distributed in the hope that it will be useful,
9563+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9564+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9565+ * GNU General Public License for more details.
9566+ *
9567+ * You should have received a copy of the GNU General Public License
523b37e3 9568+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 9569+ */
9570+
9571+/*
9572+ * dentry private data
9573+ */
9574+
9575+#include "aufs.h"
9576+
e49829fe 9577+void au_di_init_once(void *_dinfo)
4a4d8108 9578+{
e49829fe 9579+ struct au_dinfo *dinfo = _dinfo;
4a4d8108 9580+
e49829fe 9581+ au_rw_init(&dinfo->di_rwsem);
4a4d8108
AM
9582+}
9583+
027c5e7a 9584+struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
1facf9fc 9585+{
9586+ struct au_dinfo *dinfo;
027c5e7a 9587+ int nbr, i;
1facf9fc 9588+
9589+ dinfo = au_cache_alloc_dinfo();
9590+ if (unlikely(!dinfo))
9591+ goto out;
9592+
5afbbe0d 9593+ nbr = au_sbbot(sb) + 1;
1facf9fc 9594+ if (nbr <= 0)
9595+ nbr = 1;
9596+ dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
027c5e7a
AM
9597+ if (dinfo->di_hdentry) {
9598+ au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
5afbbe0d
AM
9599+ dinfo->di_btop = -1;
9600+ dinfo->di_bbot = -1;
027c5e7a
AM
9601+ dinfo->di_bwh = -1;
9602+ dinfo->di_bdiropq = -1;
38d290e6 9603+ dinfo->di_tmpfile = 0;
027c5e7a
AM
9604+ for (i = 0; i < nbr; i++)
9605+ dinfo->di_hdentry[i].hd_id = -1;
9606+ goto out;
9607+ }
1facf9fc 9608+
1c60b727 9609+ au_cache_free_dinfo(dinfo);
027c5e7a
AM
9610+ dinfo = NULL;
9611+
4f0767ce 9612+out:
027c5e7a 9613+ return dinfo;
1facf9fc 9614+}
9615+
027c5e7a 9616+void au_di_free(struct au_dinfo *dinfo)
4a4d8108 9617+{
4a4d8108 9618+ struct au_hdentry *p;
5afbbe0d 9619+ aufs_bindex_t bbot, bindex;
4a4d8108
AM
9620+
9621+ /* dentry may not be revalidated */
5afbbe0d 9622+ bindex = dinfo->di_btop;
4a4d8108 9623+ if (bindex >= 0) {
5afbbe0d
AM
9624+ bbot = dinfo->di_bbot;
9625+ p = au_hdentry(dinfo, bindex);
9626+ while (bindex++ <= bbot)
4a4d8108
AM
9627+ au_hdput(p++);
9628+ }
e49925d1 9629+ au_kfree_try_rcu(dinfo->di_hdentry);
1c60b727 9630+ au_cache_free_dinfo(dinfo);
027c5e7a
AM
9631+}
9632+
9633+void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9634+{
9635+ struct au_hdentry *p;
9636+ aufs_bindex_t bi;
9637+
9638+ AuRwMustWriteLock(&a->di_rwsem);
9639+ AuRwMustWriteLock(&b->di_rwsem);
9640+
9641+#define DiSwap(v, name) \
9642+ do { \
9643+ v = a->di_##name; \
9644+ a->di_##name = b->di_##name; \
9645+ b->di_##name = v; \
9646+ } while (0)
9647+
9648+ DiSwap(p, hdentry);
5afbbe0d
AM
9649+ DiSwap(bi, btop);
9650+ DiSwap(bi, bbot);
027c5e7a
AM
9651+ DiSwap(bi, bwh);
9652+ DiSwap(bi, bdiropq);
9653+ /* smp_mb(); */
9654+
9655+#undef DiSwap
9656+}
9657+
9658+void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9659+{
9660+ AuRwMustWriteLock(&dst->di_rwsem);
9661+ AuRwMustWriteLock(&src->di_rwsem);
9662+
5afbbe0d
AM
9663+ dst->di_btop = src->di_btop;
9664+ dst->di_bbot = src->di_bbot;
027c5e7a
AM
9665+ dst->di_bwh = src->di_bwh;
9666+ dst->di_bdiropq = src->di_bdiropq;
9667+ /* smp_mb(); */
9668+}
9669+
9670+int au_di_init(struct dentry *dentry)
9671+{
9672+ int err;
9673+ struct super_block *sb;
9674+ struct au_dinfo *dinfo;
9675+
9676+ err = 0;
9677+ sb = dentry->d_sb;
9678+ dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9679+ if (dinfo) {
9680+ atomic_set(&dinfo->di_generation, au_sigen(sb));
9681+ /* smp_mb(); */ /* atomic_set */
9682+ dentry->d_fsdata = dinfo;
9683+ } else
9684+ err = -ENOMEM;
9685+
9686+ return err;
9687+}
9688+
9689+void au_di_fin(struct dentry *dentry)
9690+{
9691+ struct au_dinfo *dinfo;
9692+
9693+ dinfo = au_di(dentry);
9694+ AuRwDestroy(&dinfo->di_rwsem);
9695+ au_di_free(dinfo);
4a4d8108
AM
9696+}
9697+
e2f27e51 9698+int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
1facf9fc 9699+{
9700+ int err, sz;
9701+ struct au_hdentry *hdp;
9702+
1308ab2a 9703+ AuRwMustWriteLock(&dinfo->di_rwsem);
9704+
1facf9fc 9705+ err = -ENOMEM;
5afbbe0d 9706+ sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
1facf9fc 9707+ if (!sz)
9708+ sz = sizeof(*hdp);
e2f27e51
AM
9709+ hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
9710+ may_shrink);
1facf9fc 9711+ if (hdp) {
9712+ dinfo->di_hdentry = hdp;
9713+ err = 0;
9714+ }
9715+
9716+ return err;
9717+}
9718+
9719+/* ---------------------------------------------------------------------- */
9720+
9721+static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9722+{
9723+ switch (lsc) {
9724+ case AuLsc_DI_CHILD:
9725+ ii_write_lock_child(inode);
9726+ break;
9727+ case AuLsc_DI_CHILD2:
9728+ ii_write_lock_child2(inode);
9729+ break;
9730+ case AuLsc_DI_CHILD3:
9731+ ii_write_lock_child3(inode);
9732+ break;
9733+ case AuLsc_DI_PARENT:
9734+ ii_write_lock_parent(inode);
9735+ break;
9736+ case AuLsc_DI_PARENT2:
9737+ ii_write_lock_parent2(inode);
9738+ break;
9739+ case AuLsc_DI_PARENT3:
9740+ ii_write_lock_parent3(inode);
9741+ break;
9742+ default:
9743+ BUG();
9744+ }
9745+}
9746+
9747+static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9748+{
9749+ switch (lsc) {
9750+ case AuLsc_DI_CHILD:
9751+ ii_read_lock_child(inode);
9752+ break;
9753+ case AuLsc_DI_CHILD2:
9754+ ii_read_lock_child2(inode);
9755+ break;
9756+ case AuLsc_DI_CHILD3:
9757+ ii_read_lock_child3(inode);
9758+ break;
9759+ case AuLsc_DI_PARENT:
9760+ ii_read_lock_parent(inode);
9761+ break;
9762+ case AuLsc_DI_PARENT2:
9763+ ii_read_lock_parent2(inode);
9764+ break;
9765+ case AuLsc_DI_PARENT3:
9766+ ii_read_lock_parent3(inode);
9767+ break;
9768+ default:
9769+ BUG();
9770+ }
9771+}
9772+
9773+void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9774+{
5527c038
JR
9775+ struct inode *inode;
9776+
dece6358 9777+ au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
5527c038
JR
9778+ if (d_really_is_positive(d)) {
9779+ inode = d_inode(d);
1facf9fc 9780+ if (au_ftest_lock(flags, IW))
5527c038 9781+ do_ii_write_lock(inode, lsc);
1facf9fc 9782+ else if (au_ftest_lock(flags, IR))
5527c038 9783+ do_ii_read_lock(inode, lsc);
1facf9fc 9784+ }
9785+}
9786+
9787+void di_read_unlock(struct dentry *d, int flags)
9788+{
5527c038
JR
9789+ struct inode *inode;
9790+
9791+ if (d_really_is_positive(d)) {
9792+ inode = d_inode(d);
027c5e7a
AM
9793+ if (au_ftest_lock(flags, IW)) {
9794+ au_dbg_verify_dinode(d);
5527c038 9795+ ii_write_unlock(inode);
027c5e7a
AM
9796+ } else if (au_ftest_lock(flags, IR)) {
9797+ au_dbg_verify_dinode(d);
5527c038 9798+ ii_read_unlock(inode);
027c5e7a 9799+ }
1facf9fc 9800+ }
dece6358 9801+ au_rw_read_unlock(&au_di(d)->di_rwsem);
1facf9fc 9802+}
9803+
9804+void di_downgrade_lock(struct dentry *d, int flags)
9805+{
5527c038
JR
9806+ if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9807+ ii_downgrade_lock(d_inode(d));
dece6358 9808+ au_rw_dgrade_lock(&au_di(d)->di_rwsem);
1facf9fc 9809+}
9810+
9811+void di_write_lock(struct dentry *d, unsigned int lsc)
9812+{
dece6358 9813+ au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
5527c038
JR
9814+ if (d_really_is_positive(d))
9815+ do_ii_write_lock(d_inode(d), lsc);
1facf9fc 9816+}
9817+
9818+void di_write_unlock(struct dentry *d)
9819+{
027c5e7a 9820+ au_dbg_verify_dinode(d);
5527c038
JR
9821+ if (d_really_is_positive(d))
9822+ ii_write_unlock(d_inode(d));
dece6358 9823+ au_rw_write_unlock(&au_di(d)->di_rwsem);
1facf9fc 9824+}
9825+
9826+void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9827+{
9828+ AuDebugOn(d1 == d2
5527c038 9829+ || d_inode(d1) == d_inode(d2)
1facf9fc 9830+ || d1->d_sb != d2->d_sb);
9831+
521ced18
JR
9832+ if ((isdir && au_test_subdir(d1, d2))
9833+ || d1 < d2) {
1facf9fc 9834+ di_write_lock_child(d1);
9835+ di_write_lock_child2(d2);
9836+ } else {
1facf9fc 9837+ di_write_lock_child(d2);
9838+ di_write_lock_child2(d1);
9839+ }
9840+}
9841+
9842+void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9843+{
9844+ AuDebugOn(d1 == d2
5527c038 9845+ || d_inode(d1) == d_inode(d2)
1facf9fc 9846+ || d1->d_sb != d2->d_sb);
9847+
521ced18
JR
9848+ if ((isdir && au_test_subdir(d1, d2))
9849+ || d1 < d2) {
1facf9fc 9850+ di_write_lock_parent(d1);
9851+ di_write_lock_parent2(d2);
9852+ } else {
1facf9fc 9853+ di_write_lock_parent(d2);
9854+ di_write_lock_parent2(d1);
9855+ }
9856+}
9857+
9858+void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9859+{
9860+ di_write_unlock(d1);
5527c038 9861+ if (d_inode(d1) == d_inode(d2))
dece6358 9862+ au_rw_write_unlock(&au_di(d2)->di_rwsem);
1facf9fc 9863+ else
9864+ di_write_unlock(d2);
9865+}
9866+
9867+/* ---------------------------------------------------------------------- */
9868+
9869+struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9870+{
9871+ struct dentry *d;
9872+
1308ab2a 9873+ DiMustAnyLock(dentry);
9874+
5afbbe0d 9875+ if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
1facf9fc 9876+ return NULL;
9877+ AuDebugOn(bindex < 0);
5afbbe0d 9878+ d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
c1595e42 9879+ AuDebugOn(d && au_dcount(d) <= 0);
1facf9fc 9880+ return d;
9881+}
9882+
2cbb1c4b
JR
9883+/*
9884+ * extended version of au_h_dptr().
38d290e6
JR
9885+ * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9886+ * error.
2cbb1c4b
JR
9887+ */
9888+struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9889+{
9890+ struct dentry *h_dentry;
9891+ struct inode *inode, *h_inode;
9892+
5527c038 9893+ AuDebugOn(d_really_is_negative(dentry));
2cbb1c4b
JR
9894+
9895+ h_dentry = NULL;
5afbbe0d
AM
9896+ if (au_dbtop(dentry) <= bindex
9897+ && bindex <= au_dbbot(dentry))
2cbb1c4b 9898+ h_dentry = au_h_dptr(dentry, bindex);
38d290e6 9899+ if (h_dentry && !au_d_linkable(h_dentry)) {
2cbb1c4b
JR
9900+ dget(h_dentry);
9901+ goto out; /* success */
9902+ }
9903+
5527c038 9904+ inode = d_inode(dentry);
5afbbe0d
AM
9905+ AuDebugOn(bindex < au_ibtop(inode));
9906+ AuDebugOn(au_ibbot(inode) < bindex);
2cbb1c4b
JR
9907+ h_inode = au_h_iptr(inode, bindex);
9908+ h_dentry = d_find_alias(h_inode);
9909+ if (h_dentry) {
9910+ if (!IS_ERR(h_dentry)) {
38d290e6 9911+ if (!au_d_linkable(h_dentry))
2cbb1c4b
JR
9912+ goto out; /* success */
9913+ dput(h_dentry);
9914+ } else
9915+ goto out;
9916+ }
9917+
9918+ if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9919+ h_dentry = au_plink_lkup(inode, bindex);
9920+ AuDebugOn(!h_dentry);
9921+ if (!IS_ERR(h_dentry)) {
9922+ if (!au_d_hashed_positive(h_dentry))
9923+ goto out; /* success */
9924+ dput(h_dentry);
9925+ h_dentry = NULL;
9926+ }
9927+ }
9928+
9929+out:
9930+ AuDbgDentry(h_dentry);
9931+ return h_dentry;
9932+}
9933+
1facf9fc 9934+aufs_bindex_t au_dbtail(struct dentry *dentry)
9935+{
5afbbe0d 9936+ aufs_bindex_t bbot, bwh;
1facf9fc 9937+
5afbbe0d
AM
9938+ bbot = au_dbbot(dentry);
9939+ if (0 <= bbot) {
1facf9fc 9940+ bwh = au_dbwh(dentry);
9941+ if (!bwh)
9942+ return bwh;
5afbbe0d 9943+ if (0 < bwh && bwh < bbot)
1facf9fc 9944+ return bwh - 1;
9945+ }
5afbbe0d 9946+ return bbot;
1facf9fc 9947+}
9948+
9949+aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9950+{
5afbbe0d 9951+ aufs_bindex_t bbot, bopq;
1facf9fc 9952+
5afbbe0d
AM
9953+ bbot = au_dbtail(dentry);
9954+ if (0 <= bbot) {
1facf9fc 9955+ bopq = au_dbdiropq(dentry);
5afbbe0d
AM
9956+ if (0 <= bopq && bopq < bbot)
9957+ bbot = bopq;
1facf9fc 9958+ }
5afbbe0d 9959+ return bbot;
1facf9fc 9960+}
9961+
9962+/* ---------------------------------------------------------------------- */
9963+
9964+void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9965+ struct dentry *h_dentry)
9966+{
5afbbe0d
AM
9967+ struct au_dinfo *dinfo;
9968+ struct au_hdentry *hd;
027c5e7a 9969+ struct au_branch *br;
1facf9fc 9970+
1308ab2a 9971+ DiMustWriteLock(dentry);
9972+
5afbbe0d
AM
9973+ dinfo = au_di(dentry);
9974+ hd = au_hdentry(dinfo, bindex);
4a4d8108 9975+ au_hdput(hd);
1facf9fc 9976+ hd->hd_dentry = h_dentry;
027c5e7a
AM
9977+ if (h_dentry) {
9978+ br = au_sbr(dentry->d_sb, bindex);
9979+ hd->hd_id = br->br_id;
9980+ }
9981+}
9982+
9983+int au_dbrange_test(struct dentry *dentry)
9984+{
9985+ int err;
5afbbe0d 9986+ aufs_bindex_t btop, bbot;
027c5e7a
AM
9987+
9988+ err = 0;
5afbbe0d
AM
9989+ btop = au_dbtop(dentry);
9990+ bbot = au_dbbot(dentry);
9991+ if (btop >= 0)
9992+ AuDebugOn(bbot < 0 && btop > bbot);
027c5e7a
AM
9993+ else {
9994+ err = -EIO;
5afbbe0d 9995+ AuDebugOn(bbot >= 0);
027c5e7a
AM
9996+ }
9997+
9998+ return err;
9999+}
10000+
10001+int au_digen_test(struct dentry *dentry, unsigned int sigen)
10002+{
10003+ int err;
10004+
10005+ err = 0;
10006+ if (unlikely(au_digen(dentry) != sigen
5527c038 10007+ || au_iigen_test(d_inode(dentry), sigen)))
027c5e7a
AM
10008+ err = -EIO;
10009+
10010+ return err;
1facf9fc 10011+}
10012+
10013+void au_update_digen(struct dentry *dentry)
10014+{
10015+ atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
10016+ /* smp_mb(); */ /* atomic_set */
10017+}
10018+
10019+void au_update_dbrange(struct dentry *dentry, int do_put_zero)
10020+{
10021+ struct au_dinfo *dinfo;
10022+ struct dentry *h_d;
4a4d8108 10023+ struct au_hdentry *hdp;
5afbbe0d 10024+ aufs_bindex_t bindex, bbot;
1facf9fc 10025+
1308ab2a 10026+ DiMustWriteLock(dentry);
10027+
1facf9fc 10028+ dinfo = au_di(dentry);
5afbbe0d 10029+ if (!dinfo || dinfo->di_btop < 0)
1facf9fc 10030+ return;
10031+
10032+ if (do_put_zero) {
5afbbe0d
AM
10033+ bbot = dinfo->di_bbot;
10034+ bindex = dinfo->di_btop;
10035+ hdp = au_hdentry(dinfo, bindex);
10036+ for (; bindex <= bbot; bindex++, hdp++) {
10037+ h_d = hdp->hd_dentry;
5527c038 10038+ if (h_d && d_is_negative(h_d))
1facf9fc 10039+ au_set_h_dptr(dentry, bindex, NULL);
10040+ }
10041+ }
10042+
5afbbe0d
AM
10043+ dinfo->di_btop = 0;
10044+ hdp = au_hdentry(dinfo, dinfo->di_btop);
10045+ for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
10046+ if (hdp->hd_dentry)
1facf9fc 10047+ break;
5afbbe0d
AM
10048+ if (dinfo->di_btop > dinfo->di_bbot) {
10049+ dinfo->di_btop = -1;
10050+ dinfo->di_bbot = -1;
1facf9fc 10051+ return;
10052+ }
10053+
5afbbe0d
AM
10054+ hdp = au_hdentry(dinfo, dinfo->di_bbot);
10055+ for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
10056+ if (hdp->hd_dentry)
1facf9fc 10057+ break;
5afbbe0d 10058+ AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
1facf9fc 10059+}
10060+
5afbbe0d 10061+void au_update_dbtop(struct dentry *dentry)
1facf9fc 10062+{
5afbbe0d 10063+ aufs_bindex_t bindex, bbot;
1facf9fc 10064+ struct dentry *h_dentry;
10065+
5afbbe0d
AM
10066+ bbot = au_dbbot(dentry);
10067+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
1facf9fc 10068+ h_dentry = au_h_dptr(dentry, bindex);
10069+ if (!h_dentry)
10070+ continue;
5527c038 10071+ if (d_is_positive(h_dentry)) {
5afbbe0d 10072+ au_set_dbtop(dentry, bindex);
1facf9fc 10073+ return;
10074+ }
10075+ au_set_h_dptr(dentry, bindex, NULL);
10076+ }
10077+}
10078+
5afbbe0d 10079+void au_update_dbbot(struct dentry *dentry)
1facf9fc 10080+{
5afbbe0d 10081+ aufs_bindex_t bindex, btop;
1facf9fc 10082+ struct dentry *h_dentry;
10083+
5afbbe0d
AM
10084+ btop = au_dbtop(dentry);
10085+ for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
1facf9fc 10086+ h_dentry = au_h_dptr(dentry, bindex);
10087+ if (!h_dentry)
10088+ continue;
5527c038 10089+ if (d_is_positive(h_dentry)) {
5afbbe0d 10090+ au_set_dbbot(dentry, bindex);
1facf9fc 10091+ return;
10092+ }
10093+ au_set_h_dptr(dentry, bindex, NULL);
10094+ }
10095+}
10096+
10097+int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
10098+{
5afbbe0d 10099+ aufs_bindex_t bindex, bbot;
1facf9fc 10100+
5afbbe0d
AM
10101+ bbot = au_dbbot(dentry);
10102+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
1facf9fc 10103+ if (au_h_dptr(dentry, bindex) == h_dentry)
10104+ return bindex;
10105+ return -1;
10106+}
7f207e10
AM
10107diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
10108--- /usr/share/empty/fs/aufs/dir.c 1970-01-01 01:00:00.000000000 +0100
0b487229
JR
10109+++ linux/fs/aufs/dir.c 2021-02-22 23:30:37.699347580 +0100
10110@@ -0,0 +1,763 @@
cd7a4cd9 10111+// SPDX-License-Identifier: GPL-2.0
1facf9fc 10112+/*
0b487229 10113+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 10114+ *
10115+ * This program, aufs is free software; you can redistribute it and/or modify
10116+ * it under the terms of the GNU General Public License as published by
10117+ * the Free Software Foundation; either version 2 of the License, or
10118+ * (at your option) any later version.
dece6358
AM
10119+ *
10120+ * This program is distributed in the hope that it will be useful,
10121+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10122+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10123+ * GNU General Public License for more details.
10124+ *
10125+ * You should have received a copy of the GNU General Public License
523b37e3 10126+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 10127+ */
10128+
10129+/*
10130+ * directory operations
10131+ */
10132+
10133+#include <linux/fs_stack.h>
0b487229 10134+#include <linux/iversion.h>
1facf9fc 10135+#include "aufs.h"
10136+
10137+void au_add_nlink(struct inode *dir, struct inode *h_dir)
10138+{
9dbd164d
AM
10139+ unsigned int nlink;
10140+
1facf9fc 10141+ AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10142+
9dbd164d
AM
10143+ nlink = dir->i_nlink;
10144+ nlink += h_dir->i_nlink - 2;
1facf9fc 10145+ if (h_dir->i_nlink < 2)
9dbd164d 10146+ nlink += 2;
f6b6e03d 10147+ smp_mb(); /* for i_nlink */
7eafdf33 10148+ /* 0 can happen in revaliding */
92d182d2 10149+ set_nlink(dir, nlink);
1facf9fc 10150+}
10151+
10152+void au_sub_nlink(struct inode *dir, struct inode *h_dir)
10153+{
9dbd164d
AM
10154+ unsigned int nlink;
10155+
1facf9fc 10156+ AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10157+
9dbd164d
AM
10158+ nlink = dir->i_nlink;
10159+ nlink -= h_dir->i_nlink - 2;
1facf9fc 10160+ if (h_dir->i_nlink < 2)
9dbd164d 10161+ nlink -= 2;
f6b6e03d 10162+ smp_mb(); /* for i_nlink */
92d182d2 10163+ /* nlink == 0 means the branch-fs is broken */
9dbd164d 10164+ set_nlink(dir, nlink);
1facf9fc 10165+}
10166+
1308ab2a 10167+loff_t au_dir_size(struct file *file, struct dentry *dentry)
10168+{
10169+ loff_t sz;
5afbbe0d 10170+ aufs_bindex_t bindex, bbot;
1308ab2a 10171+ struct file *h_file;
10172+ struct dentry *h_dentry;
10173+
10174+ sz = 0;
10175+ if (file) {
2000de60 10176+ AuDebugOn(!d_is_dir(file->f_path.dentry));
1308ab2a 10177+
5afbbe0d
AM
10178+ bbot = au_fbbot_dir(file);
10179+ for (bindex = au_fbtop(file);
10180+ bindex <= bbot && sz < KMALLOC_MAX_SIZE;
1308ab2a 10181+ bindex++) {
4a4d8108 10182+ h_file = au_hf_dir(file, bindex);
c06a8ce3
AM
10183+ if (h_file && file_inode(h_file))
10184+ sz += vfsub_f_size_read(h_file);
1308ab2a 10185+ }
10186+ } else {
10187+ AuDebugOn(!dentry);
2000de60 10188+ AuDebugOn(!d_is_dir(dentry));
1308ab2a 10189+
5afbbe0d
AM
10190+ bbot = au_dbtaildir(dentry);
10191+ for (bindex = au_dbtop(dentry);
10192+ bindex <= bbot && sz < KMALLOC_MAX_SIZE;
1308ab2a 10193+ bindex++) {
10194+ h_dentry = au_h_dptr(dentry, bindex);
5527c038
JR
10195+ if (h_dentry && d_is_positive(h_dentry))
10196+ sz += i_size_read(d_inode(h_dentry));
1308ab2a 10197+ }
10198+ }
10199+ if (sz < KMALLOC_MAX_SIZE)
10200+ sz = roundup_pow_of_two(sz);
10201+ if (sz > KMALLOC_MAX_SIZE)
10202+ sz = KMALLOC_MAX_SIZE;
10203+ else if (sz < NAME_MAX) {
10204+ BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
10205+ sz = AUFS_RDBLK_DEF;
10206+ }
10207+ return sz;
10208+}
10209+
b912730e
AM
10210+struct au_dir_ts_arg {
10211+ struct dentry *dentry;
10212+ aufs_bindex_t brid;
10213+};
10214+
10215+static void au_do_dir_ts(void *arg)
10216+{
10217+ struct au_dir_ts_arg *a = arg;
10218+ struct au_dtime dt;
10219+ struct path h_path;
10220+ struct inode *dir, *h_dir;
10221+ struct super_block *sb;
10222+ struct au_branch *br;
10223+ struct au_hinode *hdir;
10224+ int err;
5afbbe0d 10225+ aufs_bindex_t btop, bindex;
b912730e
AM
10226+
10227+ sb = a->dentry->d_sb;
5527c038 10228+ if (d_really_is_negative(a->dentry))
b912730e 10229+ goto out;
5527c038 10230+ /* no dir->i_mutex lock */
b95c5147
AM
10231+ aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
10232+
5527c038 10233+ dir = d_inode(a->dentry);
5afbbe0d 10234+ btop = au_ibtop(dir);
b912730e 10235+ bindex = au_br_index(sb, a->brid);
5afbbe0d 10236+ if (bindex < btop)
b912730e
AM
10237+ goto out_unlock;
10238+
10239+ br = au_sbr(sb, bindex);
10240+ h_path.dentry = au_h_dptr(a->dentry, bindex);
10241+ if (!h_path.dentry)
10242+ goto out_unlock;
10243+ h_path.mnt = au_br_mnt(br);
10244+ au_dtime_store(&dt, a->dentry, &h_path);
10245+
5afbbe0d 10246+ br = au_sbr(sb, btop);
b912730e
AM
10247+ if (!au_br_writable(br->br_perm))
10248+ goto out_unlock;
5afbbe0d 10249+ h_path.dentry = au_h_dptr(a->dentry, btop);
b912730e
AM
10250+ h_path.mnt = au_br_mnt(br);
10251+ err = vfsub_mnt_want_write(h_path.mnt);
10252+ if (err)
10253+ goto out_unlock;
5afbbe0d
AM
10254+ hdir = au_hi(dir, btop);
10255+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
10256+ h_dir = au_h_iptr(dir, btop);
b912730e 10257+ if (h_dir->i_nlink
cd7a4cd9 10258+ && timespec64_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
b912730e
AM
10259+ dt.dt_h_path = h_path;
10260+ au_dtime_revert(&dt);
10261+ }
5afbbe0d 10262+ au_hn_inode_unlock(hdir);
b912730e
AM
10263+ vfsub_mnt_drop_write(h_path.mnt);
10264+ au_cpup_attr_timesizes(dir);
10265+
10266+out_unlock:
10267+ aufs_read_unlock(a->dentry, AuLock_DW);
10268+out:
10269+ dput(a->dentry);
10270+ au_nwt_done(&au_sbi(sb)->si_nowait);
e49925d1 10271+ au_kfree_try_rcu(arg);
b912730e
AM
10272+}
10273+
10274+void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
10275+{
10276+ int perm, wkq_err;
5afbbe0d 10277+ aufs_bindex_t btop;
b912730e
AM
10278+ struct au_dir_ts_arg *arg;
10279+ struct dentry *dentry;
10280+ struct super_block *sb;
10281+
10282+ IMustLock(dir);
10283+
10284+ dentry = d_find_any_alias(dir);
10285+ AuDebugOn(!dentry);
10286+ sb = dentry->d_sb;
5afbbe0d
AM
10287+ btop = au_ibtop(dir);
10288+ if (btop == bindex) {
b912730e
AM
10289+ au_cpup_attr_timesizes(dir);
10290+ goto out;
10291+ }
10292+
5afbbe0d 10293+ perm = au_sbr_perm(sb, btop);
b912730e
AM
10294+ if (!au_br_writable(perm))
10295+ goto out;
10296+
10297+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
10298+ if (!arg)
10299+ goto out;
10300+
10301+ arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
10302+ arg->brid = au_sbr_id(sb, bindex);
10303+ wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
10304+ if (unlikely(wkq_err)) {
10305+ pr_err("wkq %d\n", wkq_err);
10306+ dput(dentry);
e49925d1 10307+ au_kfree_try_rcu(arg);
b912730e
AM
10308+ }
10309+
10310+out:
10311+ dput(dentry);
10312+}
10313+
1facf9fc 10314+/* ---------------------------------------------------------------------- */
10315+
10316+static int reopen_dir(struct file *file)
10317+{
10318+ int err;
10319+ unsigned int flags;
5afbbe0d 10320+ aufs_bindex_t bindex, btail, btop;
1facf9fc 10321+ struct dentry *dentry, *h_dentry;
10322+ struct file *h_file;
10323+
10324+ /* open all lower dirs */
2000de60 10325+ dentry = file->f_path.dentry;
5afbbe0d
AM
10326+ btop = au_dbtop(dentry);
10327+ for (bindex = au_fbtop(file); bindex < btop; bindex++)
1facf9fc 10328+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d 10329+ au_set_fbtop(file, btop);
1facf9fc 10330+
10331+ btail = au_dbtaildir(dentry);
5afbbe0d 10332+ for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
1facf9fc 10333+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d 10334+ au_set_fbbot_dir(file, btail);
1facf9fc 10335+
4a4d8108 10336+ flags = vfsub_file_flags(file);
5afbbe0d 10337+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 10338+ h_dentry = au_h_dptr(dentry, bindex);
10339+ if (!h_dentry)
10340+ continue;
4a4d8108 10341+ h_file = au_hf_dir(file, bindex);
1facf9fc 10342+ if (h_file)
10343+ continue;
10344+
392086de 10345+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
1facf9fc 10346+ err = PTR_ERR(h_file);
10347+ if (IS_ERR(h_file))
10348+ goto out; /* close all? */
10349+ au_set_h_fptr(file, bindex, h_file);
10350+ }
10351+ au_update_figen(file);
10352+ /* todo: necessary? */
10353+ /* file->f_ra = h_file->f_ra; */
10354+ err = 0;
10355+
4f0767ce 10356+out:
1facf9fc 10357+ return err;
10358+}
10359+
b912730e 10360+static int do_open_dir(struct file *file, int flags, struct file *h_file)
1facf9fc 10361+{
10362+ int err;
10363+ aufs_bindex_t bindex, btail;
10364+ struct dentry *dentry, *h_dentry;
8cdd5066 10365+ struct vfsmount *mnt;
1facf9fc 10366+
1308ab2a 10367+ FiMustWriteLock(file);
b912730e 10368+ AuDebugOn(h_file);
1308ab2a 10369+
523b37e3 10370+ err = 0;
8cdd5066 10371+ mnt = file->f_path.mnt;
2000de60 10372+ dentry = file->f_path.dentry;
be118d29 10373+ file->f_version = inode_query_iversion(d_inode(dentry));
5afbbe0d
AM
10374+ bindex = au_dbtop(dentry);
10375+ au_set_fbtop(file, bindex);
1facf9fc 10376+ btail = au_dbtaildir(dentry);
5afbbe0d 10377+ au_set_fbbot_dir(file, btail);
1facf9fc 10378+ for (; !err && bindex <= btail; bindex++) {
10379+ h_dentry = au_h_dptr(dentry, bindex);
10380+ if (!h_dentry)
10381+ continue;
10382+
8cdd5066
JR
10383+ err = vfsub_test_mntns(mnt, h_dentry->d_sb);
10384+ if (unlikely(err))
10385+ break;
392086de 10386+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
1facf9fc 10387+ if (IS_ERR(h_file)) {
10388+ err = PTR_ERR(h_file);
10389+ break;
10390+ }
10391+ au_set_h_fptr(file, bindex, h_file);
10392+ }
10393+ au_update_figen(file);
10394+ /* todo: necessary? */
10395+ /* file->f_ra = h_file->f_ra; */
10396+ if (!err)
10397+ return 0; /* success */
10398+
10399+ /* close all */
5afbbe0d 10400+ for (bindex = au_fbtop(file); bindex <= btail; bindex++)
1facf9fc 10401+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d
AM
10402+ au_set_fbtop(file, -1);
10403+ au_set_fbbot_dir(file, -1);
4a4d8108 10404+
1facf9fc 10405+ return err;
10406+}
10407+
10408+static int aufs_open_dir(struct inode *inode __maybe_unused,
10409+ struct file *file)
10410+{
4a4d8108
AM
10411+ int err;
10412+ struct super_block *sb;
10413+ struct au_fidir *fidir;
10414+
10415+ err = -ENOMEM;
2000de60 10416+ sb = file->f_path.dentry->d_sb;
4a4d8108 10417+ si_read_lock(sb, AuLock_FLUSH);
e49829fe 10418+ fidir = au_fidir_alloc(sb);
4a4d8108 10419+ if (fidir) {
b912730e
AM
10420+ struct au_do_open_args args = {
10421+ .open = do_open_dir,
10422+ .fidir = fidir
10423+ };
10424+ err = au_do_open(file, &args);
4a4d8108 10425+ if (unlikely(err))
e49925d1 10426+ au_kfree_rcu(fidir);
4a4d8108
AM
10427+ }
10428+ si_read_unlock(sb);
10429+ return err;
1facf9fc 10430+}
10431+
10432+static int aufs_release_dir(struct inode *inode __maybe_unused,
10433+ struct file *file)
10434+{
10435+ struct au_vdir *vdir_cache;
4a4d8108
AM
10436+ struct au_finfo *finfo;
10437+ struct au_fidir *fidir;
f0c0a007 10438+ struct au_hfile *hf;
5afbbe0d 10439+ aufs_bindex_t bindex, bbot;
1facf9fc 10440+
4a4d8108
AM
10441+ finfo = au_fi(file);
10442+ fidir = finfo->fi_hdir;
10443+ if (fidir) {
8b6a4947
AM
10444+ au_hbl_del(&finfo->fi_hlist,
10445+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
4a4d8108
AM
10446+ vdir_cache = fidir->fd_vdir_cache; /* lock-free */
10447+ if (vdir_cache)
1c60b727 10448+ au_vdir_free(vdir_cache);
4a4d8108
AM
10449+
10450+ bindex = finfo->fi_btop;
10451+ if (bindex >= 0) {
f0c0a007 10452+ hf = fidir->fd_hfile + bindex;
4a4d8108
AM
10453+ /*
10454+ * calls fput() instead of filp_close(),
10455+ * since no dnotify or lock for the lower file.
10456+ */
5afbbe0d 10457+ bbot = fidir->fd_bbot;
f0c0a007
AM
10458+ for (; bindex <= bbot; bindex++, hf++)
10459+ if (hf->hf_file)
1c60b727 10460+ au_hfput(hf, /*execed*/0);
4a4d8108 10461+ }
e49925d1 10462+ au_kfree_rcu(fidir);
4a4d8108 10463+ finfo->fi_hdir = NULL;
1facf9fc 10464+ }
1c60b727 10465+ au_finfo_fin(file);
1facf9fc 10466+ return 0;
10467+}
10468+
10469+/* ---------------------------------------------------------------------- */
10470+
4a4d8108
AM
10471+static int au_do_flush_dir(struct file *file, fl_owner_t id)
10472+{
10473+ int err;
5afbbe0d 10474+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
10475+ struct file *h_file;
10476+
10477+ err = 0;
5afbbe0d
AM
10478+ bbot = au_fbbot_dir(file);
10479+ for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
4a4d8108
AM
10480+ h_file = au_hf_dir(file, bindex);
10481+ if (h_file)
10482+ err = vfsub_flush(h_file, id);
10483+ }
10484+ return err;
10485+}
10486+
10487+static int aufs_flush_dir(struct file *file, fl_owner_t id)
10488+{
10489+ return au_do_flush(file, id, au_do_flush_dir);
10490+}
10491+
10492+/* ---------------------------------------------------------------------- */
10493+
1facf9fc 10494+static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
10495+{
10496+ int err;
5afbbe0d 10497+ aufs_bindex_t bbot, bindex;
1facf9fc 10498+ struct inode *inode;
10499+ struct super_block *sb;
10500+
10501+ err = 0;
10502+ sb = dentry->d_sb;
5527c038 10503+ inode = d_inode(dentry);
1facf9fc 10504+ IMustLock(inode);
5afbbe0d
AM
10505+ bbot = au_dbbot(dentry);
10506+ for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
1facf9fc 10507+ struct path h_path;
1facf9fc 10508+
10509+ if (au_test_ro(sb, bindex, inode))
10510+ continue;
10511+ h_path.dentry = au_h_dptr(dentry, bindex);
10512+ if (!h_path.dentry)
10513+ continue;
1facf9fc 10514+
1facf9fc 10515+ h_path.mnt = au_sbr_mnt(sb, bindex);
53392da6 10516+ err = vfsub_fsync(NULL, &h_path, datasync);
1facf9fc 10517+ }
10518+
10519+ return err;
10520+}
10521+
10522+static int au_do_fsync_dir(struct file *file, int datasync)
10523+{
10524+ int err;
5afbbe0d 10525+ aufs_bindex_t bbot, bindex;
1facf9fc 10526+ struct file *h_file;
10527+ struct super_block *sb;
10528+ struct inode *inode;
1facf9fc 10529+
521ced18 10530+ err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
1facf9fc 10531+ if (unlikely(err))
10532+ goto out;
10533+
c06a8ce3 10534+ inode = file_inode(file);
b912730e 10535+ sb = inode->i_sb;
5afbbe0d
AM
10536+ bbot = au_fbbot_dir(file);
10537+ for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
4a4d8108 10538+ h_file = au_hf_dir(file, bindex);
1facf9fc 10539+ if (!h_file || au_test_ro(sb, bindex, inode))
10540+ continue;
10541+
53392da6 10542+ err = vfsub_fsync(h_file, &h_file->f_path, datasync);
1facf9fc 10543+ }
10544+
4f0767ce 10545+out:
1facf9fc 10546+ return err;
10547+}
10548+
10549+/*
10550+ * @file may be NULL
10551+ */
1e00d052
AM
10552+static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
10553+ int datasync)
1facf9fc 10554+{
10555+ int err;
b752ccd1 10556+ struct dentry *dentry;
5527c038 10557+ struct inode *inode;
1facf9fc 10558+ struct super_block *sb;
1facf9fc 10559+
10560+ err = 0;
2000de60 10561+ dentry = file->f_path.dentry;
5527c038 10562+ inode = d_inode(dentry);
febd17d6 10563+ inode_lock(inode);
1facf9fc 10564+ sb = dentry->d_sb;
10565+ si_noflush_read_lock(sb);
10566+ if (file)
10567+ err = au_do_fsync_dir(file, datasync);
10568+ else {
10569+ di_write_lock_child(dentry);
10570+ err = au_do_fsync_dir_no_file(dentry, datasync);
10571+ }
5527c038 10572+ au_cpup_attr_timesizes(inode);
1facf9fc 10573+ di_write_unlock(dentry);
10574+ if (file)
10575+ fi_write_unlock(file);
10576+
10577+ si_read_unlock(sb);
febd17d6 10578+ inode_unlock(inode);
1facf9fc 10579+ return err;
10580+}
10581+
10582+/* ---------------------------------------------------------------------- */
10583+
5afbbe0d 10584+static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
1facf9fc 10585+{
10586+ int err;
10587+ struct dentry *dentry;
9dbd164d 10588+ struct inode *inode, *h_inode;
1facf9fc 10589+ struct super_block *sb;
10590+
062440b3 10591+ AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
392086de 10592+
2000de60 10593+ dentry = file->f_path.dentry;
5527c038 10594+ inode = d_inode(dentry);
1facf9fc 10595+ IMustLock(inode);
10596+
10597+ sb = dentry->d_sb;
10598+ si_read_lock(sb, AuLock_FLUSH);
521ced18 10599+ err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
1facf9fc 10600+ if (unlikely(err))
10601+ goto out;
027c5e7a
AM
10602+ err = au_alive_dir(dentry);
10603+ if (!err)
10604+ err = au_vdir_init(file);
1facf9fc 10605+ di_downgrade_lock(dentry, AuLock_IR);
10606+ if (unlikely(err))
10607+ goto out_unlock;
10608+
5afbbe0d 10609+ h_inode = au_h_iptr(inode, au_ibtop(inode));
b752ccd1 10610+ if (!au_test_nfsd()) {
392086de 10611+ err = au_vdir_fill_de(file, ctx);
9dbd164d 10612+ fsstack_copy_attr_atime(inode, h_inode);
1facf9fc 10613+ } else {
10614+ /*
10615+ * nfsd filldir may call lookup_one_len(), vfs_getattr(),
10616+ * encode_fh() and others.
10617+ */
9dbd164d 10618+ atomic_inc(&h_inode->i_count);
1facf9fc 10619+ di_read_unlock(dentry, AuLock_IR);
10620+ si_read_unlock(sb);
392086de 10621+ err = au_vdir_fill_de(file, ctx);
1facf9fc 10622+ fsstack_copy_attr_atime(inode, h_inode);
10623+ fi_write_unlock(file);
9dbd164d 10624+ iput(h_inode);
1facf9fc 10625+
10626+ AuTraceErr(err);
10627+ return err;
10628+ }
10629+
4f0767ce 10630+out_unlock:
1facf9fc 10631+ di_read_unlock(dentry, AuLock_IR);
10632+ fi_write_unlock(file);
4f0767ce 10633+out:
1facf9fc 10634+ si_read_unlock(sb);
10635+ return err;
10636+}
10637+
10638+/* ---------------------------------------------------------------------- */
10639+
10640+#define AuTestEmpty_WHONLY 1
dece6358
AM
10641+#define AuTestEmpty_CALLED (1 << 1)
10642+#define AuTestEmpty_SHWH (1 << 2)
1facf9fc 10643+#define au_ftest_testempty(flags, name) ((flags) & AuTestEmpty_##name)
7f207e10
AM
10644+#define au_fset_testempty(flags, name) \
10645+ do { (flags) |= AuTestEmpty_##name; } while (0)
10646+#define au_fclr_testempty(flags, name) \
10647+ do { (flags) &= ~AuTestEmpty_##name; } while (0)
1facf9fc 10648+
dece6358
AM
10649+#ifndef CONFIG_AUFS_SHWH
10650+#undef AuTestEmpty_SHWH
10651+#define AuTestEmpty_SHWH 0
10652+#endif
10653+
1facf9fc 10654+struct test_empty_arg {
392086de 10655+ struct dir_context ctx;
1308ab2a 10656+ struct au_nhash *whlist;
1facf9fc 10657+ unsigned int flags;
10658+ int err;
10659+ aufs_bindex_t bindex;
10660+};
10661+
392086de
AM
10662+static int test_empty_cb(struct dir_context *ctx, const char *__name,
10663+ int namelen, loff_t offset __maybe_unused, u64 ino,
dece6358 10664+ unsigned int d_type)
1facf9fc 10665+{
392086de
AM
10666+ struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10667+ ctx);
1facf9fc 10668+ char *name = (void *)__name;
10669+
10670+ arg->err = 0;
10671+ au_fset_testempty(arg->flags, CALLED);
10672+ /* smp_mb(); */
10673+ if (name[0] == '.'
10674+ && (namelen == 1 || (name[1] == '.' && namelen == 2)))
10675+ goto out; /* success */
10676+
10677+ if (namelen <= AUFS_WH_PFX_LEN
10678+ || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10679+ if (au_ftest_testempty(arg->flags, WHONLY)
1308ab2a 10680+ && !au_nhash_test_known_wh(arg->whlist, name, namelen))
1facf9fc 10681+ arg->err = -ENOTEMPTY;
10682+ goto out;
10683+ }
10684+
10685+ name += AUFS_WH_PFX_LEN;
10686+ namelen -= AUFS_WH_PFX_LEN;
1308ab2a 10687+ if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
1facf9fc 10688+ arg->err = au_nhash_append_wh
1308ab2a 10689+ (arg->whlist, name, namelen, ino, d_type, arg->bindex,
dece6358 10690+ au_ftest_testempty(arg->flags, SHWH));
1facf9fc 10691+
4f0767ce 10692+out:
1facf9fc 10693+ /* smp_mb(); */
10694+ AuTraceErr(arg->err);
10695+ return arg->err;
10696+}
10697+
10698+static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10699+{
10700+ int err;
10701+ struct file *h_file;
acd2b654 10702+ struct au_branch *br;
1facf9fc 10703+
10704+ h_file = au_h_open(dentry, arg->bindex,
10705+ O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
392086de 10706+ /*file*/NULL, /*force_wr*/0);
1facf9fc 10707+ err = PTR_ERR(h_file);
10708+ if (IS_ERR(h_file))
10709+ goto out;
10710+
10711+ err = 0;
10712+ if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
c06a8ce3 10713+ && !file_inode(h_file)->i_nlink)
1facf9fc 10714+ goto out_put;
10715+
10716+ do {
10717+ arg->err = 0;
10718+ au_fclr_testempty(arg->flags, CALLED);
10719+ /* smp_mb(); */
392086de 10720+ err = vfsub_iterate_dir(h_file, &arg->ctx);
1facf9fc 10721+ if (err >= 0)
10722+ err = arg->err;
10723+ } while (!err && au_ftest_testempty(arg->flags, CALLED));
10724+
4f0767ce 10725+out_put:
1facf9fc 10726+ fput(h_file);
acd2b654
AM
10727+ br = au_sbr(dentry->d_sb, arg->bindex);
10728+ au_lcnt_dec(&br->br_nfiles);
4f0767ce 10729+out:
1facf9fc 10730+ return err;
10731+}
10732+
10733+struct do_test_empty_args {
10734+ int *errp;
10735+ struct dentry *dentry;
10736+ struct test_empty_arg *arg;
10737+};
10738+
10739+static void call_do_test_empty(void *args)
10740+{
10741+ struct do_test_empty_args *a = args;
10742+ *a->errp = do_test_empty(a->dentry, a->arg);
10743+}
10744+
10745+static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10746+{
10747+ int err, wkq_err;
10748+ struct dentry *h_dentry;
10749+ struct inode *h_inode;
10750+
10751+ h_dentry = au_h_dptr(dentry, arg->bindex);
5527c038 10752+ h_inode = d_inode(h_dentry);
53392da6 10753+ /* todo: i_mode changes anytime? */
be118d29 10754+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
1facf9fc 10755+ err = au_test_h_perm_sio(h_inode, MAY_EXEC | MAY_READ);
3c1bdaff 10756+ inode_unlock_shared(h_inode);
1facf9fc 10757+ if (!err)
10758+ err = do_test_empty(dentry, arg);
10759+ else {
10760+ struct do_test_empty_args args = {
10761+ .errp = &err,
10762+ .dentry = dentry,
10763+ .arg = arg
10764+ };
10765+ unsigned int flags = arg->flags;
10766+
10767+ wkq_err = au_wkq_wait(call_do_test_empty, &args);
10768+ if (unlikely(wkq_err))
10769+ err = wkq_err;
10770+ arg->flags = flags;
10771+ }
10772+
10773+ return err;
10774+}
10775+
10776+int au_test_empty_lower(struct dentry *dentry)
10777+{
10778+ int err;
1308ab2a 10779+ unsigned int rdhash;
5afbbe0d 10780+ aufs_bindex_t bindex, btop, btail;
1308ab2a 10781+ struct au_nhash whlist;
392086de
AM
10782+ struct test_empty_arg arg = {
10783+ .ctx = {
2000de60 10784+ .actor = test_empty_cb
392086de
AM
10785+ }
10786+ };
076b876e 10787+ int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
1facf9fc 10788+
dece6358
AM
10789+ SiMustAnyLock(dentry->d_sb);
10790+
1308ab2a 10791+ rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10792+ if (!rdhash)
10793+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10794+ err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
dece6358 10795+ if (unlikely(err))
1facf9fc 10796+ goto out;
10797+
1facf9fc 10798+ arg.flags = 0;
1308ab2a 10799+ arg.whlist = &whlist;
5afbbe0d 10800+ btop = au_dbtop(dentry);
dece6358
AM
10801+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10802+ au_fset_testempty(arg.flags, SHWH);
076b876e
AM
10803+ test_empty = do_test_empty;
10804+ if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10805+ test_empty = sio_test_empty;
5afbbe0d 10806+ arg.bindex = btop;
076b876e 10807+ err = test_empty(dentry, &arg);
1facf9fc 10808+ if (unlikely(err))
10809+ goto out_whlist;
10810+
10811+ au_fset_testempty(arg.flags, WHONLY);
10812+ btail = au_dbtaildir(dentry);
5afbbe0d 10813+ for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
1facf9fc 10814+ struct dentry *h_dentry;
10815+
10816+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 10817+ if (h_dentry && d_is_positive(h_dentry)) {
1facf9fc 10818+ arg.bindex = bindex;
076b876e 10819+ err = test_empty(dentry, &arg);
1facf9fc 10820+ }
10821+ }
10822+
4f0767ce 10823+out_whlist:
1308ab2a 10824+ au_nhash_wh_free(&whlist);
4f0767ce 10825+out:
1facf9fc 10826+ return err;
10827+}
10828+
10829+int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10830+{
10831+ int err;
392086de
AM
10832+ struct test_empty_arg arg = {
10833+ .ctx = {
2000de60 10834+ .actor = test_empty_cb
392086de
AM
10835+ }
10836+ };
1facf9fc 10837+ aufs_bindex_t bindex, btail;
10838+
10839+ err = 0;
1308ab2a 10840+ arg.whlist = whlist;
1facf9fc 10841+ arg.flags = AuTestEmpty_WHONLY;
dece6358
AM
10842+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10843+ au_fset_testempty(arg.flags, SHWH);
1facf9fc 10844+ btail = au_dbtaildir(dentry);
5afbbe0d 10845+ for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
1facf9fc 10846+ struct dentry *h_dentry;
10847+
10848+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 10849+ if (h_dentry && d_is_positive(h_dentry)) {
1facf9fc 10850+ arg.bindex = bindex;
10851+ err = sio_test_empty(dentry, &arg);
10852+ }
10853+ }
10854+
10855+ return err;
10856+}
10857+
10858+/* ---------------------------------------------------------------------- */
10859+
10860+const struct file_operations aufs_dir_fop = {
4a4d8108 10861+ .owner = THIS_MODULE,
027c5e7a 10862+ .llseek = default_llseek,
1facf9fc 10863+ .read = generic_read_dir,
5afbbe0d 10864+ .iterate_shared = aufs_iterate_shared,
1facf9fc 10865+ .unlocked_ioctl = aufs_ioctl_dir,
b752ccd1
AM
10866+#ifdef CONFIG_COMPAT
10867+ .compat_ioctl = aufs_compat_ioctl_dir,
10868+#endif
1facf9fc 10869+ .open = aufs_open_dir,
10870+ .release = aufs_release_dir,
4a4d8108 10871+ .flush = aufs_flush_dir,
1facf9fc 10872+ .fsync = aufs_fsync_dir
10873+};
7f207e10
AM
10874diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
10875--- /usr/share/empty/fs/aufs/dir.h 1970-01-01 01:00:00.000000000 +0100
0b487229 10876+++ linux/fs/aufs/dir.h 2021-02-22 23:30:37.699347580 +0100
e49925d1 10877@@ -0,0 +1,134 @@
062440b3 10878+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 10879+/*
0b487229 10880+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 10881+ *
10882+ * This program, aufs is free software; you can redistribute it and/or modify
10883+ * it under the terms of the GNU General Public License as published by
10884+ * the Free Software Foundation; either version 2 of the License, or
10885+ * (at your option) any later version.
dece6358
AM
10886+ *
10887+ * This program is distributed in the hope that it will be useful,
10888+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10889+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10890+ * GNU General Public License for more details.
10891+ *
10892+ * You should have received a copy of the GNU General Public License
523b37e3 10893+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 10894+ */
10895+
10896+/*
10897+ * directory operations
10898+ */
10899+
10900+#ifndef __AUFS_DIR_H__
10901+#define __AUFS_DIR_H__
10902+
10903+#ifdef __KERNEL__
10904+
10905+#include <linux/fs.h>
1facf9fc 10906+
10907+/* ---------------------------------------------------------------------- */
10908+
10909+/* need to be faster and smaller */
10910+
10911+struct au_nhash {
dece6358
AM
10912+ unsigned int nh_num;
10913+ struct hlist_head *nh_head;
1facf9fc 10914+};
10915+
10916+struct au_vdir_destr {
10917+ unsigned char len;
0b487229 10918+ unsigned char name[];
1facf9fc 10919+} __packed;
10920+
10921+struct au_vdir_dehstr {
10922+ struct hlist_node hash;
1c60b727 10923+ struct au_vdir_destr *str;
e49925d1 10924+ struct rcu_head rcu;
4a4d8108 10925+} ____cacheline_aligned_in_smp;
1facf9fc 10926+
10927+struct au_vdir_de {
10928+ ino_t de_ino;
10929+ unsigned char de_type;
10930+ /* caution: packed */
10931+ struct au_vdir_destr de_str;
10932+} __packed;
10933+
10934+struct au_vdir_wh {
10935+ struct hlist_node wh_hash;
dece6358
AM
10936+#ifdef CONFIG_AUFS_SHWH
10937+ ino_t wh_ino;
1facf9fc 10938+ aufs_bindex_t wh_bindex;
dece6358
AM
10939+ unsigned char wh_type;
10940+#else
10941+ aufs_bindex_t wh_bindex;
10942+#endif
10943+ /* caution: packed */
1facf9fc 10944+ struct au_vdir_destr wh_str;
10945+} __packed;
10946+
10947+union au_vdir_deblk_p {
10948+ unsigned char *deblk;
10949+ struct au_vdir_de *de;
10950+};
10951+
10952+struct au_vdir {
10953+ unsigned char **vd_deblk;
10954+ unsigned long vd_nblk;
1facf9fc 10955+ struct {
10956+ unsigned long ul;
10957+ union au_vdir_deblk_p p;
10958+ } vd_last;
10959+
be118d29 10960+ u64 vd_version;
dece6358 10961+ unsigned int vd_deblk_sz;
e49925d1
JR
10962+ unsigned long vd_jiffy;
10963+ struct rcu_head rcu;
4a4d8108 10964+} ____cacheline_aligned_in_smp;
1facf9fc 10965+
10966+/* ---------------------------------------------------------------------- */
10967+
10968+/* dir.c */
10969+extern const struct file_operations aufs_dir_fop;
10970+void au_add_nlink(struct inode *dir, struct inode *h_dir);
10971+void au_sub_nlink(struct inode *dir, struct inode *h_dir);
1308ab2a 10972+loff_t au_dir_size(struct file *file, struct dentry *dentry);
b912730e 10973+void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
1facf9fc 10974+int au_test_empty_lower(struct dentry *dentry);
10975+int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10976+
10977+/* vdir.c */
1308ab2a 10978+unsigned int au_rdhash_est(loff_t sz);
dece6358
AM
10979+int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
10980+void au_nhash_wh_free(struct au_nhash *whlist);
1facf9fc 10981+int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
10982+ int limit);
dece6358
AM
10983+int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
10984+int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
10985+ unsigned int d_type, aufs_bindex_t bindex,
10986+ unsigned char shwh);
1c60b727 10987+void au_vdir_free(struct au_vdir *vdir);
1facf9fc 10988+int au_vdir_init(struct file *file);
392086de 10989+int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
1facf9fc 10990+
10991+/* ioctl.c */
10992+long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
10993+
1308ab2a 10994+#ifdef CONFIG_AUFS_RDU
10995+/* rdu.c */
10996+long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
b752ccd1
AM
10997+#ifdef CONFIG_COMPAT
10998+long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
10999+ unsigned long arg);
11000+#endif
1308ab2a 11001+#else
c1595e42
JR
11002+AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
11003+ unsigned int cmd, unsigned long arg)
b752ccd1 11004+#ifdef CONFIG_COMPAT
c1595e42
JR
11005+AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
11006+ unsigned int cmd, unsigned long arg)
b752ccd1 11007+#endif
1308ab2a 11008+#endif
11009+
1facf9fc 11010+#endif /* __KERNEL__ */
11011+#endif /* __AUFS_DIR_H__ */
8b6a4947
AM
11012diff -urN /usr/share/empty/fs/aufs/dirren.c linux/fs/aufs/dirren.c
11013--- /usr/share/empty/fs/aufs/dirren.c 1970-01-01 01:00:00.000000000 +0100
0b487229 11014+++ linux/fs/aufs/dirren.c 2021-02-22 23:30:37.699347580 +0100
062440b3 11015@@ -0,0 +1,1316 @@
cd7a4cd9 11016+// SPDX-License-Identifier: GPL-2.0
8b6a4947 11017+/*
0b487229 11018+ * Copyright (C) 2017-2020 Junjiro R. Okajima
8b6a4947
AM
11019+ *
11020+ * This program, aufs is free software; you can redistribute it and/or modify
11021+ * it under the terms of the GNU General Public License as published by
11022+ * the Free Software Foundation; either version 2 of the License, or
11023+ * (at your option) any later version.
11024+ *
11025+ * This program is distributed in the hope that it will be useful,
11026+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11027+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11028+ * GNU General Public License for more details.
11029+ *
11030+ * You should have received a copy of the GNU General Public License
11031+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
11032+ */
11033+
11034+/*
acd2b654 11035+ * special handling in renaming a directory
8b6a4947
AM
11036+ * in order to support looking-up the before-renamed name on the lower readonly
11037+ * branches
11038+ */
11039+
11040+#include <linux/byteorder/generic.h>
11041+#include "aufs.h"
11042+
11043+static void au_dr_hino_del(struct au_dr_br *dr, struct au_dr_hino *ent)
11044+{
11045+ int idx;
11046+
11047+ idx = au_dr_ihash(ent->dr_h_ino);
11048+ au_hbl_del(&ent->dr_hnode, dr->dr_h_ino + idx);
11049+}
11050+
11051+static int au_dr_hino_test_empty(struct au_dr_br *dr)
11052+{
11053+ int ret, i;
11054+ struct hlist_bl_head *hbl;
11055+
11056+ ret = 1;
11057+ for (i = 0; ret && i < AuDirren_NHASH; i++) {
11058+ hbl = dr->dr_h_ino + i;
11059+ hlist_bl_lock(hbl);
11060+ ret &= hlist_bl_empty(hbl);
11061+ hlist_bl_unlock(hbl);
11062+ }
11063+
11064+ return ret;
11065+}
11066+
11067+static struct au_dr_hino *au_dr_hino_find(struct au_dr_br *dr, ino_t ino)
11068+{
11069+ struct au_dr_hino *found, *ent;
11070+ struct hlist_bl_head *hbl;
11071+ struct hlist_bl_node *pos;
11072+ int idx;
11073+
11074+ found = NULL;
11075+ idx = au_dr_ihash(ino);
11076+ hbl = dr->dr_h_ino + idx;
11077+ hlist_bl_lock(hbl);
11078+ hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
11079+ if (ent->dr_h_ino == ino) {
11080+ found = ent;
11081+ break;
11082+ }
11083+ hlist_bl_unlock(hbl);
11084+
11085+ return found;
11086+}
11087+
11088+int au_dr_hino_test_add(struct au_dr_br *dr, ino_t ino,
11089+ struct au_dr_hino *add_ent)
11090+{
11091+ int found, idx;
11092+ struct hlist_bl_head *hbl;
11093+ struct hlist_bl_node *pos;
11094+ struct au_dr_hino *ent;
11095+
11096+ found = 0;
11097+ idx = au_dr_ihash(ino);
11098+ hbl = dr->dr_h_ino + idx;
0b487229 11099+#if 0 /* debug print */
8b6a4947
AM
11100+ {
11101+ struct hlist_bl_node *tmp;
11102+
11103+ hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
11104+ AuDbg("hi%llu\n", (unsigned long long)ent->dr_h_ino);
11105+ }
11106+#endif
11107+ hlist_bl_lock(hbl);
11108+ hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
11109+ if (ent->dr_h_ino == ino) {
11110+ found = 1;
11111+ break;
11112+ }
11113+ if (!found && add_ent)
11114+ hlist_bl_add_head(&add_ent->dr_hnode, hbl);
11115+ hlist_bl_unlock(hbl);
11116+
11117+ if (!found && add_ent)
11118+ AuDbg("i%llu added\n", (unsigned long long)add_ent->dr_h_ino);
11119+
11120+ return found;
11121+}
11122+
11123+void au_dr_hino_free(struct au_dr_br *dr)
11124+{
11125+ int i;
11126+ struct hlist_bl_head *hbl;
11127+ struct hlist_bl_node *pos, *tmp;
11128+ struct au_dr_hino *ent;
11129+
11130+ /* SiMustWriteLock(sb); */
11131+
11132+ for (i = 0; i < AuDirren_NHASH; i++) {
11133+ hbl = dr->dr_h_ino + i;
11134+ /* no spinlock since sbinfo must be write-locked */
11135+ hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
e49925d1 11136+ au_kfree_rcu(ent);
8b6a4947
AM
11137+ INIT_HLIST_BL_HEAD(hbl);
11138+ }
11139+}
11140+
11141+/* returns the number of inodes or an error */
11142+static int au_dr_hino_store(struct super_block *sb, struct au_branch *br,
11143+ struct file *hinofile)
11144+{
11145+ int err, i;
11146+ ssize_t ssz;
11147+ loff_t pos, oldsize;
11148+ __be64 u64;
11149+ struct inode *hinoinode;
11150+ struct hlist_bl_head *hbl;
11151+ struct hlist_bl_node *n1, *n2;
11152+ struct au_dr_hino *ent;
11153+
11154+ SiMustWriteLock(sb);
11155+ AuDebugOn(!au_br_writable(br->br_perm));
11156+
11157+ hinoinode = file_inode(hinofile);
11158+ oldsize = i_size_read(hinoinode);
11159+
11160+ err = 0;
11161+ pos = 0;
11162+ hbl = br->br_dirren.dr_h_ino;
11163+ for (i = 0; !err && i < AuDirren_NHASH; i++, hbl++) {
11164+ /* no bit-lock since sbinfo must be write-locked */
11165+ hlist_bl_for_each_entry_safe(ent, n1, n2, hbl, dr_hnode) {
11166+ AuDbg("hi%llu, %pD2\n",
11167+ (unsigned long long)ent->dr_h_ino, hinofile);
11168+ u64 = cpu_to_be64(ent->dr_h_ino);
11169+ ssz = vfsub_write_k(hinofile, &u64, sizeof(u64), &pos);
11170+ if (ssz == sizeof(u64))
11171+ continue;
11172+
11173+ /* write error */
11174+ pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11175+ err = -ENOSPC;
11176+ if (ssz < 0)
11177+ err = ssz;
11178+ break;
11179+ }
11180+ }
11181+ /* regardless the error */
11182+ if (pos < oldsize) {
11183+ err = vfsub_trunc(&hinofile->f_path, pos, /*attr*/0, hinofile);
11184+ AuTraceErr(err);
11185+ }
11186+
11187+ AuTraceErr(err);
11188+ return err;
11189+}
11190+
11191+static int au_dr_hino_load(struct au_dr_br *dr, struct file *hinofile)
11192+{
11193+ int err, hidx;
11194+ ssize_t ssz;
11195+ size_t sz, n;
11196+ loff_t pos;
11197+ uint64_t u64;
11198+ struct au_dr_hino *ent;
11199+ struct inode *hinoinode;
11200+ struct hlist_bl_head *hbl;
11201+
11202+ err = 0;
11203+ pos = 0;
11204+ hbl = dr->dr_h_ino;
11205+ hinoinode = file_inode(hinofile);
11206+ sz = i_size_read(hinoinode);
11207+ AuDebugOn(sz % sizeof(u64));
11208+ n = sz / sizeof(u64);
11209+ while (n--) {
11210+ ssz = vfsub_read_k(hinofile, &u64, sizeof(u64), &pos);
11211+ if (unlikely(ssz != sizeof(u64))) {
11212+ pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11213+ err = -EINVAL;
11214+ if (ssz < 0)
11215+ err = ssz;
11216+ goto out_free;
11217+ }
11218+
11219+ ent = kmalloc(sizeof(*ent), GFP_NOFS);
11220+ if (!ent) {
11221+ err = -ENOMEM;
11222+ AuTraceErr(err);
11223+ goto out_free;
11224+ }
11225+ ent->dr_h_ino = be64_to_cpu((__force __be64)u64);
11226+ AuDbg("hi%llu, %pD2\n",
11227+ (unsigned long long)ent->dr_h_ino, hinofile);
11228+ hidx = au_dr_ihash(ent->dr_h_ino);
11229+ au_hbl_add(&ent->dr_hnode, hbl + hidx);
11230+ }
11231+ goto out; /* success */
11232+
11233+out_free:
11234+ au_dr_hino_free(dr);
11235+out:
11236+ AuTraceErr(err);
11237+ return err;
11238+}
11239+
11240+/*
11241+ * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
11242+ * @path is a switch to distinguish load and store.
11243+ */
11244+static int au_dr_hino(struct super_block *sb, aufs_bindex_t bindex,
11245+ struct au_branch *br, const struct path *path)
11246+{
11247+ int err, flags;
11248+ unsigned char load, suspend;
11249+ struct file *hinofile;
11250+ struct au_hinode *hdir;
11251+ struct inode *dir, *delegated;
11252+ struct path hinopath;
11253+ struct qstr hinoname = QSTR_INIT(AUFS_WH_DR_BRHINO,
11254+ sizeof(AUFS_WH_DR_BRHINO) - 1);
11255+
11256+ AuDebugOn(bindex < 0 && !br);
11257+ AuDebugOn(bindex >= 0 && br);
11258+
11259+ err = -EINVAL;
11260+ suspend = !br;
11261+ if (suspend)
11262+ br = au_sbr(sb, bindex);
11263+ load = !!path;
11264+ if (!load) {
11265+ path = &br->br_path;
11266+ AuDebugOn(!au_br_writable(br->br_perm));
11267+ if (unlikely(!au_br_writable(br->br_perm)))
11268+ goto out;
11269+ }
11270+
11271+ hdir = NULL;
11272+ if (suspend) {
11273+ dir = d_inode(sb->s_root);
11274+ hdir = au_hinode(au_ii(dir), bindex);
11275+ dir = hdir->hi_inode;
11276+ au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11277+ } else {
11278+ dir = d_inode(path->dentry);
11279+ inode_lock_nested(dir, AuLsc_I_CHILD);
11280+ }
11281+ hinopath.dentry = vfsub_lkup_one(&hinoname, path->dentry);
11282+ err = PTR_ERR(hinopath.dentry);
11283+ if (IS_ERR(hinopath.dentry))
11284+ goto out_unlock;
11285+
11286+ err = 0;
11287+ flags = O_RDONLY;
11288+ if (load) {
11289+ if (d_is_negative(hinopath.dentry))
11290+ goto out_dput; /* success */
11291+ } else {
11292+ if (au_dr_hino_test_empty(&br->br_dirren)) {
11293+ if (d_is_positive(hinopath.dentry)) {
11294+ delegated = NULL;
11295+ err = vfsub_unlink(dir, &hinopath, &delegated,
11296+ /*force*/0);
11297+ AuTraceErr(err);
11298+ if (unlikely(err))
11299+ pr_err("ignored err %d, %pd2\n",
11300+ err, hinopath.dentry);
11301+ if (unlikely(err == -EWOULDBLOCK))
11302+ iput(delegated);
11303+ err = 0;
11304+ }
11305+ goto out_dput;
11306+ } else if (!d_is_positive(hinopath.dentry)) {
11307+ err = vfsub_create(dir, &hinopath, 0600,
11308+ /*want_excl*/false);
11309+ AuTraceErr(err);
11310+ if (unlikely(err))
11311+ goto out_dput;
11312+ }
11313+ flags = O_WRONLY;
11314+ }
11315+ hinopath.mnt = path->mnt;
11316+ hinofile = vfsub_dentry_open(&hinopath, flags);
11317+ if (suspend)
11318+ au_hn_inode_unlock(hdir);
11319+ else
11320+ inode_unlock(dir);
11321+ dput(hinopath.dentry);
11322+ AuTraceErrPtr(hinofile);
11323+ if (IS_ERR(hinofile)) {
11324+ err = PTR_ERR(hinofile);
11325+ goto out;
11326+ }
11327+
11328+ if (load)
11329+ err = au_dr_hino_load(&br->br_dirren, hinofile);
11330+ else
11331+ err = au_dr_hino_store(sb, br, hinofile);
11332+ fput(hinofile);
11333+ goto out;
11334+
11335+out_dput:
11336+ dput(hinopath.dentry);
11337+out_unlock:
11338+ if (suspend)
11339+ au_hn_inode_unlock(hdir);
11340+ else
11341+ inode_unlock(dir);
11342+out:
11343+ AuTraceErr(err);
11344+ return err;
11345+}
11346+
11347+/* ---------------------------------------------------------------------- */
11348+
11349+static int au_dr_brid_init(struct au_dr_brid *brid, const struct path *path)
11350+{
11351+ int err;
11352+ struct kstatfs kstfs;
11353+ dev_t dev;
11354+ struct dentry *dentry;
11355+ struct super_block *sb;
11356+
11357+ err = vfs_statfs((void *)path, &kstfs);
11358+ AuTraceErr(err);
11359+ if (unlikely(err))
11360+ goto out;
11361+
11362+ /* todo: support for UUID */
11363+
11364+ if (kstfs.f_fsid.val[0] || kstfs.f_fsid.val[1]) {
11365+ brid->type = AuBrid_FSID;
11366+ brid->fsid = kstfs.f_fsid;
11367+ } else {
11368+ dentry = path->dentry;
11369+ sb = dentry->d_sb;
11370+ dev = sb->s_dev;
11371+ if (dev) {
11372+ brid->type = AuBrid_DEV;
11373+ brid->dev = dev;
11374+ }
11375+ }
11376+
11377+out:
11378+ return err;
11379+}
11380+
11381+int au_dr_br_init(struct super_block *sb, struct au_branch *br,
11382+ const struct path *path)
11383+{
11384+ int err, i;
11385+ struct au_dr_br *dr;
11386+ struct hlist_bl_head *hbl;
11387+
11388+ dr = &br->br_dirren;
11389+ hbl = dr->dr_h_ino;
11390+ for (i = 0; i < AuDirren_NHASH; i++, hbl++)
11391+ INIT_HLIST_BL_HEAD(hbl);
11392+
11393+ err = au_dr_brid_init(&dr->dr_brid, path);
11394+ if (unlikely(err))
11395+ goto out;
11396+
11397+ if (au_opt_test(au_mntflags(sb), DIRREN))
11398+ err = au_dr_hino(sb, /*bindex*/-1, br, path);
11399+
11400+out:
11401+ AuTraceErr(err);
11402+ return err;
11403+}
11404+
11405+int au_dr_br_fin(struct super_block *sb, struct au_branch *br)
11406+{
11407+ int err;
11408+
11409+ err = 0;
11410+ if (au_br_writable(br->br_perm))
11411+ err = au_dr_hino(sb, /*bindex*/-1, br, /*path*/NULL);
11412+ if (!err)
11413+ au_dr_hino_free(&br->br_dirren);
11414+
11415+ return err;
11416+}
11417+
11418+/* ---------------------------------------------------------------------- */
11419+
11420+static int au_brid_str(struct au_dr_brid *brid, struct inode *h_inode,
11421+ char *buf, size_t sz)
11422+{
11423+ int err;
11424+ unsigned int major, minor;
11425+ char *p;
11426+
11427+ p = buf;
11428+ err = snprintf(p, sz, "%d_", brid->type);
11429+ AuDebugOn(err > sz);
11430+ p += err;
11431+ sz -= err;
11432+ switch (brid->type) {
11433+ case AuBrid_Unset:
11434+ return -EINVAL;
11435+ case AuBrid_UUID:
11436+ err = snprintf(p, sz, "%pU", brid->uuid.b);
11437+ break;
11438+ case AuBrid_FSID:
11439+ err = snprintf(p, sz, "%08x-%08x",
11440+ brid->fsid.val[0], brid->fsid.val[1]);
11441+ break;
11442+ case AuBrid_DEV:
11443+ major = MAJOR(brid->dev);
11444+ minor = MINOR(brid->dev);
11445+ if (major <= 0xff && minor <= 0xff)
11446+ err = snprintf(p, sz, "%02x%02x", major, minor);
11447+ else
11448+ err = snprintf(p, sz, "%03x:%05x", major, minor);
11449+ break;
11450+ }
11451+ AuDebugOn(err > sz);
11452+ p += err;
11453+ sz -= err;
11454+ err = snprintf(p, sz, "_%llu", (unsigned long long)h_inode->i_ino);
11455+ AuDebugOn(err > sz);
11456+ p += err;
11457+ sz -= err;
11458+
11459+ return p - buf;
11460+}
11461+
11462+static int au_drinfo_name(struct au_branch *br, char *name, int len)
11463+{
11464+ int rlen;
11465+ struct dentry *br_dentry;
11466+ struct inode *br_inode;
11467+
11468+ br_dentry = au_br_dentry(br);
11469+ br_inode = d_inode(br_dentry);
11470+ rlen = au_brid_str(&br->br_dirren.dr_brid, br_inode, name, len);
11471+ AuDebugOn(rlen >= AUFS_DIRREN_ENV_VAL_SZ);
11472+ AuDebugOn(rlen > len);
11473+
11474+ return rlen;
11475+}
11476+
11477+/* ---------------------------------------------------------------------- */
11478+
11479+/*
11480+ * from the given @h_dentry, construct drinfo at @*fdata.
11481+ * when the size of @*fdata is not enough, reallocate and return new @fdata and
11482+ * @allocated.
11483+ */
11484+static int au_drinfo_construct(struct au_drinfo_fdata **fdata,
11485+ struct dentry *h_dentry,
11486+ unsigned char *allocated)
11487+{
11488+ int err, v;
11489+ struct au_drinfo_fdata *f, *p;
11490+ struct au_drinfo *drinfo;
11491+ struct inode *h_inode;
11492+ struct qstr *qname;
11493+
11494+ err = 0;
11495+ f = *fdata;
11496+ h_inode = d_inode(h_dentry);
11497+ qname = &h_dentry->d_name;
11498+ drinfo = &f->drinfo;
11499+ drinfo->ino = (__force uint64_t)cpu_to_be64(h_inode->i_ino);
11500+ drinfo->oldnamelen = qname->len;
11501+ if (*allocated < sizeof(*f) + qname->len) {
11502+ v = roundup_pow_of_two(*allocated + qname->len);
11503+ p = au_krealloc(f, v, GFP_NOFS, /*may_shrink*/0);
11504+ if (unlikely(!p)) {
11505+ err = -ENOMEM;
11506+ AuTraceErr(err);
11507+ goto out;
11508+ }
11509+ f = p;
11510+ *fdata = f;
11511+ *allocated = v;
11512+ drinfo = &f->drinfo;
11513+ }
11514+ memcpy(drinfo->oldname, qname->name, qname->len);
11515+ AuDbg("i%llu, %.*s\n",
11516+ be64_to_cpu((__force __be64)drinfo->ino), drinfo->oldnamelen,
11517+ drinfo->oldname);
11518+
11519+out:
11520+ AuTraceErr(err);
11521+ return err;
11522+}
11523+
11524+/* callers have to free the return value */
11525+static struct au_drinfo *au_drinfo_read_k(struct file *file, ino_t h_ino)
11526+{
11527+ struct au_drinfo *ret, *drinfo;
11528+ struct au_drinfo_fdata fdata;
11529+ int len;
11530+ loff_t pos;
11531+ ssize_t ssz;
11532+
11533+ ret = ERR_PTR(-EIO);
11534+ pos = 0;
11535+ ssz = vfsub_read_k(file, &fdata, sizeof(fdata), &pos);
11536+ if (unlikely(ssz != sizeof(fdata))) {
11537+ AuIOErr("ssz %zd, %u, %pD2\n",
11538+ ssz, (unsigned int)sizeof(fdata), file);
11539+ goto out;
11540+ }
11541+
11542+ fdata.magic = ntohl((__force __be32)fdata.magic);
11543+ switch (fdata.magic) {
11544+ case AUFS_DRINFO_MAGIC_V1:
11545+ break;
11546+ default:
11547+ AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
11548+ fdata.magic, AUFS_DRINFO_MAGIC_V1, file);
11549+ goto out;
11550+ }
11551+
11552+ drinfo = &fdata.drinfo;
11553+ len = drinfo->oldnamelen;
11554+ if (!len) {
11555+ AuIOErr("broken drinfo %pD2\n", file);
11556+ goto out;
11557+ }
11558+
11559+ ret = NULL;
11560+ drinfo->ino = be64_to_cpu((__force __be64)drinfo->ino);
11561+ if (unlikely(h_ino && drinfo->ino != h_ino)) {
11562+ AuDbg("ignored i%llu, i%llu, %pD2\n",
11563+ (unsigned long long)drinfo->ino,
11564+ (unsigned long long)h_ino, file);
11565+ goto out; /* success */
11566+ }
11567+
11568+ ret = kmalloc(sizeof(*ret) + len, GFP_NOFS);
11569+ if (unlikely(!ret)) {
11570+ ret = ERR_PTR(-ENOMEM);
11571+ AuTraceErrPtr(ret);
11572+ goto out;
11573+ }
11574+
11575+ *ret = *drinfo;
11576+ ssz = vfsub_read_k(file, (void *)ret->oldname, len, &pos);
11577+ if (unlikely(ssz != len)) {
e49925d1 11578+ au_kfree_rcu(ret);
8b6a4947
AM
11579+ ret = ERR_PTR(-EIO);
11580+ AuIOErr("ssz %zd, %u, %pD2\n", ssz, len, file);
11581+ goto out;
11582+ }
11583+
11584+ AuDbg("oldname %.*s\n", ret->oldnamelen, ret->oldname);
11585+
11586+out:
11587+ return ret;
11588+}
11589+
11590+/* ---------------------------------------------------------------------- */
11591+
11592+/* in order to be revertible */
11593+struct au_drinfo_rev_elm {
11594+ int created;
11595+ struct dentry *info_dentry;
11596+ struct au_drinfo *info_last;
11597+};
11598+
11599+struct au_drinfo_rev {
11600+ unsigned char already;
11601+ aufs_bindex_t nelm;
0b487229 11602+ struct au_drinfo_rev_elm elm[];
8b6a4947
AM
11603+};
11604+
11605+/* todo: isn't it too large? */
11606+struct au_drinfo_store {
11607+ struct path h_ppath;
11608+ struct dentry *h_dentry;
11609+ struct au_drinfo_fdata *fdata;
11610+ char *infoname; /* inside of whname, just after PFX */
11611+ char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ];
11612+ aufs_bindex_t btgt, btail;
11613+ unsigned char no_sio,
11614+ allocated, /* current size of *fdata */
11615+ infonamelen, /* room size for p */
acd2b654 11616+ whnamelen, /* length of the generated name */
8b6a4947
AM
11617+ renameback; /* renamed back */
11618+};
11619+
11620+/* on rename(2) error, the caller should revert it using @elm */
11621+static int au_drinfo_do_store(struct au_drinfo_store *w,
11622+ struct au_drinfo_rev_elm *elm)
11623+{
11624+ int err, len;
11625+ ssize_t ssz;
11626+ loff_t pos;
11627+ struct path infopath = {
11628+ .mnt = w->h_ppath.mnt
11629+ };
11630+ struct inode *h_dir, *h_inode, *delegated;
11631+ struct file *infofile;
11632+ struct qstr *qname;
11633+
11634+ AuDebugOn(elm
11635+ && memcmp(elm, page_address(ZERO_PAGE(0)), sizeof(*elm)));
11636+
11637+ infopath.dentry = vfsub_lookup_one_len(w->whname, w->h_ppath.dentry,
11638+ w->whnamelen);
11639+ AuTraceErrPtr(infopath.dentry);
11640+ if (IS_ERR(infopath.dentry)) {
11641+ err = PTR_ERR(infopath.dentry);
11642+ goto out;
11643+ }
11644+
11645+ err = 0;
11646+ h_dir = d_inode(w->h_ppath.dentry);
11647+ if (elm && d_is_negative(infopath.dentry)) {
11648+ err = vfsub_create(h_dir, &infopath, 0600, /*want_excl*/true);
11649+ AuTraceErr(err);
11650+ if (unlikely(err))
11651+ goto out_dput;
11652+ elm->created = 1;
11653+ elm->info_dentry = dget(infopath.dentry);
11654+ }
11655+
11656+ infofile = vfsub_dentry_open(&infopath, O_RDWR);
11657+ AuTraceErrPtr(infofile);
11658+ if (IS_ERR(infofile)) {
11659+ err = PTR_ERR(infofile);
11660+ goto out_dput;
11661+ }
11662+
11663+ h_inode = d_inode(infopath.dentry);
11664+ if (elm && i_size_read(h_inode)) {
11665+ h_inode = d_inode(w->h_dentry);
11666+ elm->info_last = au_drinfo_read_k(infofile, h_inode->i_ino);
11667+ AuTraceErrPtr(elm->info_last);
11668+ if (IS_ERR(elm->info_last)) {
11669+ err = PTR_ERR(elm->info_last);
11670+ elm->info_last = NULL;
11671+ AuDebugOn(elm->info_dentry);
11672+ goto out_fput;
11673+ }
11674+ }
11675+
11676+ if (elm && w->renameback) {
11677+ delegated = NULL;
11678+ err = vfsub_unlink(h_dir, &infopath, &delegated, /*force*/0);
11679+ AuTraceErr(err);
11680+ if (unlikely(err == -EWOULDBLOCK))
11681+ iput(delegated);
11682+ goto out_fput;
11683+ }
11684+
11685+ pos = 0;
11686+ qname = &w->h_dentry->d_name;
11687+ len = sizeof(*w->fdata) + qname->len;
11688+ if (!elm)
11689+ len = sizeof(*w->fdata) + w->fdata->drinfo.oldnamelen;
11690+ ssz = vfsub_write_k(infofile, w->fdata, len, &pos);
11691+ if (ssz == len) {
11692+ AuDbg("hi%llu, %.*s\n", w->fdata->drinfo.ino,
11693+ w->fdata->drinfo.oldnamelen, w->fdata->drinfo.oldname);
11694+ goto out_fput; /* success */
11695+ } else {
11696+ err = -EIO;
11697+ if (ssz < 0)
11698+ err = ssz;
11699+ /* the caller should revert it using @elm */
11700+ }
11701+
11702+out_fput:
11703+ fput(infofile);
11704+out_dput:
11705+ dput(infopath.dentry);
11706+out:
11707+ AuTraceErr(err);
11708+ return err;
11709+}
11710+
11711+struct au_call_drinfo_do_store_args {
11712+ int *errp;
11713+ struct au_drinfo_store *w;
11714+ struct au_drinfo_rev_elm *elm;
11715+};
11716+
11717+static void au_call_drinfo_do_store(void *args)
11718+{
11719+ struct au_call_drinfo_do_store_args *a = args;
11720+
11721+ *a->errp = au_drinfo_do_store(a->w, a->elm);
11722+}
11723+
11724+static int au_drinfo_store_sio(struct au_drinfo_store *w,
11725+ struct au_drinfo_rev_elm *elm)
11726+{
11727+ int err, wkq_err;
11728+
11729+ if (w->no_sio)
11730+ err = au_drinfo_do_store(w, elm);
11731+ else {
11732+ struct au_call_drinfo_do_store_args a = {
11733+ .errp = &err,
11734+ .w = w,
11735+ .elm = elm
11736+ };
11737+ wkq_err = au_wkq_wait(au_call_drinfo_do_store, &a);
11738+ if (unlikely(wkq_err))
11739+ err = wkq_err;
11740+ }
11741+ AuTraceErr(err);
11742+
11743+ return err;
11744+}
11745+
11746+static int au_drinfo_store_work_init(struct au_drinfo_store *w,
11747+ aufs_bindex_t btgt)
11748+{
11749+ int err;
11750+
11751+ memset(w, 0, sizeof(*w));
11752+ w->allocated = roundup_pow_of_two(sizeof(*w->fdata) + 40);
11753+ strcpy(w->whname, AUFS_WH_DR_INFO_PFX);
11754+ w->infoname = w->whname + sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11755+ w->infonamelen = sizeof(w->whname) - sizeof(AUFS_WH_DR_INFO_PFX);
11756+ w->btgt = btgt;
11757+ w->no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
11758+
11759+ err = -ENOMEM;
11760+ w->fdata = kcalloc(1, w->allocated, GFP_NOFS);
11761+ if (unlikely(!w->fdata)) {
11762+ AuTraceErr(err);
11763+ goto out;
11764+ }
11765+ w->fdata->magic = (__force uint32_t)htonl(AUFS_DRINFO_MAGIC_V1);
11766+ err = 0;
11767+
11768+out:
11769+ return err;
11770+}
11771+
11772+static void au_drinfo_store_work_fin(struct au_drinfo_store *w)
11773+{
e49925d1 11774+ au_kfree_rcu(w->fdata);
8b6a4947
AM
11775+}
11776+
11777+static void au_drinfo_store_rev(struct au_drinfo_rev *rev,
11778+ struct au_drinfo_store *w)
11779+{
11780+ struct au_drinfo_rev_elm *elm;
11781+ struct inode *h_dir, *delegated;
11782+ int err, nelm;
11783+ struct path infopath = {
11784+ .mnt = w->h_ppath.mnt
11785+ };
11786+
11787+ h_dir = d_inode(w->h_ppath.dentry);
11788+ IMustLock(h_dir);
11789+
11790+ err = 0;
11791+ elm = rev->elm;
11792+ for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11793+ AuDebugOn(elm->created && elm->info_last);
11794+ if (elm->created) {
11795+ AuDbg("here\n");
11796+ delegated = NULL;
11797+ infopath.dentry = elm->info_dentry;
11798+ err = vfsub_unlink(h_dir, &infopath, &delegated,
11799+ !w->no_sio);
11800+ AuTraceErr(err);
11801+ if (unlikely(err == -EWOULDBLOCK))
11802+ iput(delegated);
11803+ dput(elm->info_dentry);
11804+ } else if (elm->info_last) {
11805+ AuDbg("here\n");
11806+ w->fdata->drinfo = *elm->info_last;
11807+ memcpy(w->fdata->drinfo.oldname,
11808+ elm->info_last->oldname,
11809+ elm->info_last->oldnamelen);
11810+ err = au_drinfo_store_sio(w, /*elm*/NULL);
e49925d1 11811+ au_kfree_rcu(elm->info_last);
8b6a4947
AM
11812+ }
11813+ if (unlikely(err))
11814+ AuIOErr("%d, %s\n", err, w->whname);
11815+ /* go on even if err */
11816+ }
11817+}
11818+
11819+/* caller has to call au_dr_rename_fin() later */
11820+static int au_drinfo_store(struct dentry *dentry, aufs_bindex_t btgt,
11821+ struct qstr *dst_name, void *_rev)
11822+{
11823+ int err, sz, nelm;
11824+ aufs_bindex_t bindex, btail;
11825+ struct au_drinfo_store work;
11826+ struct au_drinfo_rev *rev, **p;
11827+ struct au_drinfo_rev_elm *elm;
11828+ struct super_block *sb;
11829+ struct au_branch *br;
11830+ struct au_hinode *hdir;
11831+
11832+ err = au_drinfo_store_work_init(&work, btgt);
11833+ AuTraceErr(err);
11834+ if (unlikely(err))
11835+ goto out;
11836+
11837+ err = -ENOMEM;
11838+ btail = au_dbtaildir(dentry);
11839+ nelm = btail - btgt;
11840+ sz = sizeof(*rev) + sizeof(*elm) * nelm;
11841+ rev = kcalloc(1, sz, GFP_NOFS);
11842+ if (unlikely(!rev)) {
11843+ AuTraceErr(err);
11844+ goto out_args;
11845+ }
11846+ rev->nelm = nelm;
11847+ elm = rev->elm;
11848+ p = _rev;
11849+ *p = rev;
11850+
11851+ err = 0;
11852+ sb = dentry->d_sb;
11853+ work.h_ppath.dentry = au_h_dptr(dentry, btgt);
11854+ work.h_ppath.mnt = au_sbr_mnt(sb, btgt);
11855+ hdir = au_hi(d_inode(dentry), btgt);
11856+ au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11857+ for (bindex = btgt + 1; bindex <= btail; bindex++, elm++) {
11858+ work.h_dentry = au_h_dptr(dentry, bindex);
11859+ if (!work.h_dentry)
11860+ continue;
11861+
11862+ err = au_drinfo_construct(&work.fdata, work.h_dentry,
11863+ &work.allocated);
11864+ AuTraceErr(err);
11865+ if (unlikely(err))
11866+ break;
11867+
11868+ work.renameback = au_qstreq(&work.h_dentry->d_name, dst_name);
11869+ br = au_sbr(sb, bindex);
11870+ work.whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11871+ work.whnamelen += au_drinfo_name(br, work.infoname,
11872+ work.infonamelen);
11873+ AuDbg("whname %.*s, i%llu, %.*s\n",
11874+ work.whnamelen, work.whname,
11875+ be64_to_cpu((__force __be64)work.fdata->drinfo.ino),
11876+ work.fdata->drinfo.oldnamelen,
11877+ work.fdata->drinfo.oldname);
11878+
11879+ err = au_drinfo_store_sio(&work, elm);
11880+ AuTraceErr(err);
11881+ if (unlikely(err))
11882+ break;
11883+ }
11884+ if (unlikely(err)) {
11885+ /* revert all drinfo */
11886+ au_drinfo_store_rev(rev, &work);
e49925d1 11887+ au_kfree_try_rcu(rev);
8b6a4947
AM
11888+ *p = NULL;
11889+ }
11890+ au_hn_inode_unlock(hdir);
11891+
11892+out_args:
11893+ au_drinfo_store_work_fin(&work);
11894+out:
11895+ return err;
11896+}
11897+
11898+/* ---------------------------------------------------------------------- */
11899+
11900+int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
11901+ struct qstr *dst_name, void *_rev)
11902+{
11903+ int err, already;
11904+ ino_t ino;
11905+ struct super_block *sb;
11906+ struct au_branch *br;
11907+ struct au_dr_br *dr;
11908+ struct dentry *h_dentry;
11909+ struct inode *h_inode;
11910+ struct au_dr_hino *ent;
11911+ struct au_drinfo_rev *rev, **p;
11912+
11913+ AuDbg("bindex %d\n", bindex);
11914+
11915+ err = -ENOMEM;
11916+ ent = kmalloc(sizeof(*ent), GFP_NOFS);
11917+ if (unlikely(!ent))
11918+ goto out;
11919+
11920+ sb = src->d_sb;
11921+ br = au_sbr(sb, bindex);
11922+ dr = &br->br_dirren;
11923+ h_dentry = au_h_dptr(src, bindex);
11924+ h_inode = d_inode(h_dentry);
11925+ ino = h_inode->i_ino;
11926+ ent->dr_h_ino = ino;
11927+ already = au_dr_hino_test_add(dr, ino, ent);
11928+ AuDbg("b%d, hi%llu, already %d\n",
11929+ bindex, (unsigned long long)ino, already);
11930+
11931+ err = au_drinfo_store(src, bindex, dst_name, _rev);
11932+ AuTraceErr(err);
11933+ if (!err) {
11934+ p = _rev;
11935+ rev = *p;
11936+ rev->already = already;
11937+ goto out; /* success */
11938+ }
11939+
11940+ /* revert */
11941+ if (!already)
11942+ au_dr_hino_del(dr, ent);
e49925d1 11943+ au_kfree_rcu(ent);
8b6a4947
AM
11944+
11945+out:
11946+ AuTraceErr(err);
11947+ return err;
11948+}
11949+
11950+void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11951+{
11952+ struct au_drinfo_rev *rev;
11953+ struct au_drinfo_rev_elm *elm;
11954+ int nelm;
11955+
11956+ rev = _rev;
11957+ elm = rev->elm;
11958+ for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11959+ dput(elm->info_dentry);
e49925d1 11960+ au_kfree_rcu(elm->info_last);
8b6a4947 11961+ }
e49925d1 11962+ au_kfree_try_rcu(rev);
8b6a4947
AM
11963+}
11964+
11965+void au_dr_rename_rev(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11966+{
11967+ int err;
11968+ struct au_drinfo_store work;
11969+ struct au_drinfo_rev *rev = _rev;
11970+ struct super_block *sb;
11971+ struct au_branch *br;
11972+ struct inode *h_inode;
11973+ struct au_dr_br *dr;
11974+ struct au_dr_hino *ent;
11975+
11976+ err = au_drinfo_store_work_init(&work, btgt);
11977+ if (unlikely(err))
11978+ goto out;
11979+
11980+ sb = src->d_sb;
11981+ br = au_sbr(sb, btgt);
11982+ work.h_ppath.dentry = au_h_dptr(src, btgt);
11983+ work.h_ppath.mnt = au_br_mnt(br);
11984+ au_drinfo_store_rev(rev, &work);
11985+ au_drinfo_store_work_fin(&work);
11986+ if (rev->already)
11987+ goto out;
11988+
11989+ dr = &br->br_dirren;
11990+ h_inode = d_inode(work.h_ppath.dentry);
11991+ ent = au_dr_hino_find(dr, h_inode->i_ino);
11992+ BUG_ON(!ent);
11993+ au_dr_hino_del(dr, ent);
e49925d1 11994+ au_kfree_rcu(ent);
8b6a4947
AM
11995+
11996+out:
e49925d1 11997+ au_kfree_try_rcu(rev);
8b6a4947
AM
11998+ if (unlikely(err))
11999+ pr_err("failed to remove dirren info\n");
12000+}
12001+
12002+/* ---------------------------------------------------------------------- */
12003+
12004+static struct au_drinfo *au_drinfo_do_load(struct path *h_ppath,
12005+ char *whname, int whnamelen,
12006+ struct dentry **info_dentry)
12007+{
12008+ struct au_drinfo *drinfo;
12009+ struct file *f;
12010+ struct inode *h_dir;
12011+ struct path infopath;
12012+ int unlocked;
12013+
12014+ AuDbg("%pd/%.*s\n", h_ppath->dentry, whnamelen, whname);
12015+
12016+ *info_dentry = NULL;
12017+ drinfo = NULL;
12018+ unlocked = 0;
12019+ h_dir = d_inode(h_ppath->dentry);
be118d29 12020+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8b6a4947
AM
12021+ infopath.dentry = vfsub_lookup_one_len(whname, h_ppath->dentry,
12022+ whnamelen);
12023+ if (IS_ERR(infopath.dentry)) {
12024+ drinfo = (void *)infopath.dentry;
12025+ goto out;
12026+ }
12027+
12028+ if (d_is_negative(infopath.dentry))
12029+ goto out_dput; /* success */
12030+
12031+ infopath.mnt = h_ppath->mnt;
12032+ f = vfsub_dentry_open(&infopath, O_RDONLY);
12033+ inode_unlock_shared(h_dir);
12034+ unlocked = 1;
12035+ if (IS_ERR(f)) {
12036+ drinfo = (void *)f;
12037+ goto out_dput;
12038+ }
12039+
12040+ drinfo = au_drinfo_read_k(f, /*h_ino*/0);
12041+ if (IS_ERR_OR_NULL(drinfo))
12042+ goto out_fput;
12043+
12044+ AuDbg("oldname %.*s\n", drinfo->oldnamelen, drinfo->oldname);
12045+ *info_dentry = dget(infopath.dentry); /* keep it alive */
12046+
12047+out_fput:
12048+ fput(f);
12049+out_dput:
12050+ dput(infopath.dentry);
12051+out:
12052+ if (!unlocked)
12053+ inode_unlock_shared(h_dir);
12054+ AuTraceErrPtr(drinfo);
12055+ return drinfo;
12056+}
12057+
12058+struct au_drinfo_do_load_args {
12059+ struct au_drinfo **drinfop;
12060+ struct path *h_ppath;
12061+ char *whname;
12062+ int whnamelen;
12063+ struct dentry **info_dentry;
12064+};
12065+
12066+static void au_call_drinfo_do_load(void *args)
12067+{
12068+ struct au_drinfo_do_load_args *a = args;
12069+
12070+ *a->drinfop = au_drinfo_do_load(a->h_ppath, a->whname, a->whnamelen,
12071+ a->info_dentry);
12072+}
12073+
12074+struct au_drinfo_load {
12075+ struct path h_ppath;
12076+ struct qstr *qname;
12077+ unsigned char no_sio;
12078+
12079+ aufs_bindex_t ninfo;
12080+ struct au_drinfo **drinfo;
12081+};
12082+
12083+static int au_drinfo_load(struct au_drinfo_load *w, aufs_bindex_t bindex,
12084+ struct au_branch *br)
12085+{
12086+ int err, wkq_err, whnamelen, e;
12087+ char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ]
12088+ = AUFS_WH_DR_INFO_PFX;
12089+ struct au_drinfo *drinfo;
12090+ struct qstr oldname;
12091+ struct inode *h_dir, *delegated;
12092+ struct dentry *info_dentry;
12093+ struct path infopath;
12094+
12095+ whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
12096+ whnamelen += au_drinfo_name(br, whname + whnamelen,
12097+ sizeof(whname) - whnamelen);
12098+ if (w->no_sio)
12099+ drinfo = au_drinfo_do_load(&w->h_ppath, whname, whnamelen,
12100+ &info_dentry);
12101+ else {
12102+ struct au_drinfo_do_load_args args = {
12103+ .drinfop = &drinfo,
12104+ .h_ppath = &w->h_ppath,
12105+ .whname = whname,
12106+ .whnamelen = whnamelen,
12107+ .info_dentry = &info_dentry
12108+ };
12109+ wkq_err = au_wkq_wait(au_call_drinfo_do_load, &args);
12110+ if (unlikely(wkq_err))
12111+ drinfo = ERR_PTR(wkq_err);
12112+ }
12113+ err = PTR_ERR(drinfo);
12114+ if (IS_ERR_OR_NULL(drinfo))
12115+ goto out;
12116+
12117+ err = 0;
12118+ oldname.len = drinfo->oldnamelen;
12119+ oldname.name = drinfo->oldname;
12120+ if (au_qstreq(w->qname, &oldname)) {
12121+ /* the name is renamed back */
e49925d1 12122+ au_kfree_rcu(drinfo);
8b6a4947
AM
12123+ drinfo = NULL;
12124+
12125+ infopath.dentry = info_dentry;
12126+ infopath.mnt = w->h_ppath.mnt;
12127+ h_dir = d_inode(w->h_ppath.dentry);
12128+ delegated = NULL;
12129+ inode_lock_nested(h_dir, AuLsc_I_PARENT);
12130+ e = vfsub_unlink(h_dir, &infopath, &delegated, !w->no_sio);
12131+ inode_unlock(h_dir);
12132+ if (unlikely(e))
12133+ AuIOErr("ignored %d, %pd2\n", e, &infopath.dentry);
12134+ if (unlikely(e == -EWOULDBLOCK))
12135+ iput(delegated);
12136+ }
e49925d1 12137+ au_kfree_rcu(w->drinfo[bindex]);
8b6a4947
AM
12138+ w->drinfo[bindex] = drinfo;
12139+ dput(info_dentry);
12140+
12141+out:
12142+ AuTraceErr(err);
12143+ return err;
12144+}
12145+
12146+/* ---------------------------------------------------------------------- */
12147+
12148+static void au_dr_lkup_free(struct au_drinfo **drinfo, int n)
12149+{
12150+ struct au_drinfo **p = drinfo;
12151+
12152+ while (n-- > 0)
e49925d1
JR
12153+ au_kfree_rcu(*drinfo++);
12154+ au_kfree_try_rcu(p);
8b6a4947
AM
12155+}
12156+
12157+int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12158+ aufs_bindex_t btgt)
12159+{
12160+ int err, ninfo;
12161+ struct au_drinfo_load w;
12162+ aufs_bindex_t bindex, bbot;
12163+ struct au_branch *br;
12164+ struct inode *h_dir;
12165+ struct au_dr_hino *ent;
12166+ struct super_block *sb;
12167+
12168+ AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
12169+ AuLNPair(&dentry->d_name), AuLNPair(&lkup->dirren.dr_name),
12170+ AuLNPair(&lkup->whname), btgt);
12171+
12172+ sb = dentry->d_sb;
12173+ bbot = au_sbbot(sb);
12174+ w.ninfo = bbot + 1;
12175+ if (!lkup->dirren.drinfo) {
12176+ lkup->dirren.drinfo = kcalloc(w.ninfo,
12177+ sizeof(*lkup->dirren.drinfo),
12178+ GFP_NOFS);
12179+ if (unlikely(!lkup->dirren.drinfo)) {
12180+ err = -ENOMEM;
12181+ goto out;
12182+ }
12183+ lkup->dirren.ninfo = w.ninfo;
12184+ }
12185+ w.drinfo = lkup->dirren.drinfo;
12186+ w.no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
12187+ w.h_ppath.dentry = au_h_dptr(dentry, btgt);
12188+ AuDebugOn(!w.h_ppath.dentry);
12189+ w.h_ppath.mnt = au_sbr_mnt(sb, btgt);
12190+ w.qname = &dentry->d_name;
12191+
12192+ ninfo = 0;
12193+ for (bindex = btgt + 1; bindex <= bbot; bindex++) {
12194+ br = au_sbr(sb, bindex);
12195+ err = au_drinfo_load(&w, bindex, br);
12196+ if (unlikely(err))
12197+ goto out_free;
12198+ if (w.drinfo[bindex])
12199+ ninfo++;
12200+ }
12201+ if (!ninfo) {
12202+ br = au_sbr(sb, btgt);
12203+ h_dir = d_inode(w.h_ppath.dentry);
12204+ ent = au_dr_hino_find(&br->br_dirren, h_dir->i_ino);
12205+ AuDebugOn(!ent);
12206+ au_dr_hino_del(&br->br_dirren, ent);
e49925d1 12207+ au_kfree_rcu(ent);
8b6a4947
AM
12208+ }
12209+ goto out; /* success */
12210+
12211+out_free:
12212+ au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12213+ lkup->dirren.ninfo = 0;
12214+ lkup->dirren.drinfo = NULL;
12215+out:
12216+ AuTraceErr(err);
12217+ return err;
12218+}
12219+
12220+void au_dr_lkup_fin(struct au_do_lookup_args *lkup)
12221+{
12222+ au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12223+}
12224+
12225+int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt)
12226+{
12227+ int err;
12228+ struct au_drinfo *drinfo;
12229+
12230+ err = 0;
12231+ if (!lkup->dirren.drinfo)
12232+ goto out;
0b487229
JR
12233+ AuDebugOn(lkup->dirren.ninfo <= btgt);
12234+ drinfo = lkup->dirren.drinfo[btgt];
8b6a4947
AM
12235+ if (!drinfo)
12236+ goto out;
12237+
e49925d1 12238+ au_kfree_try_rcu(lkup->whname.name);
8b6a4947
AM
12239+ lkup->whname.name = NULL;
12240+ lkup->dirren.dr_name.len = drinfo->oldnamelen;
12241+ lkup->dirren.dr_name.name = drinfo->oldname;
12242+ lkup->name = &lkup->dirren.dr_name;
12243+ err = au_wh_name_alloc(&lkup->whname, lkup->name);
12244+ if (!err)
12245+ AuDbg("name %.*s, whname %.*s, b%d\n",
12246+ AuLNPair(lkup->name), AuLNPair(&lkup->whname),
12247+ btgt);
12248+
12249+out:
12250+ AuTraceErr(err);
12251+ return err;
12252+}
12253+
12254+int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12255+ ino_t h_ino)
12256+{
12257+ int match;
12258+ struct au_drinfo *drinfo;
12259+
12260+ match = 1;
12261+ if (!lkup->dirren.drinfo)
12262+ goto out;
0b487229
JR
12263+ AuDebugOn(lkup->dirren.ninfo <= bindex);
12264+ drinfo = lkup->dirren.drinfo[bindex];
8b6a4947
AM
12265+ if (!drinfo)
12266+ goto out;
12267+
12268+ match = (drinfo->ino == h_ino);
12269+ AuDbg("match %d\n", match);
12270+
12271+out:
12272+ return match;
12273+}
12274+
12275+/* ---------------------------------------------------------------------- */
12276+
12277+int au_dr_opt_set(struct super_block *sb)
12278+{
12279+ int err;
12280+ aufs_bindex_t bindex, bbot;
12281+ struct au_branch *br;
12282+
12283+ err = 0;
12284+ bbot = au_sbbot(sb);
12285+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
12286+ br = au_sbr(sb, bindex);
12287+ err = au_dr_hino(sb, bindex, /*br*/NULL, &br->br_path);
12288+ }
12289+
12290+ return err;
12291+}
12292+
12293+int au_dr_opt_flush(struct super_block *sb)
12294+{
12295+ int err;
12296+ aufs_bindex_t bindex, bbot;
12297+ struct au_branch *br;
12298+
12299+ err = 0;
12300+ bbot = au_sbbot(sb);
12301+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
12302+ br = au_sbr(sb, bindex);
12303+ if (au_br_writable(br->br_perm))
12304+ err = au_dr_hino(sb, bindex, /*br*/NULL, /*path*/NULL);
12305+ }
12306+
12307+ return err;
12308+}
12309+
12310+int au_dr_opt_clr(struct super_block *sb, int no_flush)
12311+{
12312+ int err;
12313+ aufs_bindex_t bindex, bbot;
12314+ struct au_branch *br;
12315+
12316+ err = 0;
12317+ if (!no_flush) {
12318+ err = au_dr_opt_flush(sb);
12319+ if (unlikely(err))
12320+ goto out;
12321+ }
12322+
12323+ bbot = au_sbbot(sb);
12324+ for (bindex = 0; bindex <= bbot; bindex++) {
12325+ br = au_sbr(sb, bindex);
12326+ au_dr_hino_free(&br->br_dirren);
12327+ }
12328+
12329+out:
12330+ return err;
12331+}
12332diff -urN /usr/share/empty/fs/aufs/dirren.h linux/fs/aufs/dirren.h
12333--- /usr/share/empty/fs/aufs/dirren.h 1970-01-01 01:00:00.000000000 +0100
0b487229 12334+++ linux/fs/aufs/dirren.h 2021-02-22 23:30:37.699347580 +0100
062440b3
AM
12335@@ -0,0 +1,140 @@
12336+/* SPDX-License-Identifier: GPL-2.0 */
8b6a4947 12337+/*
0b487229 12338+ * Copyright (C) 2017-2020 Junjiro R. Okajima
8b6a4947
AM
12339+ *
12340+ * This program, aufs is free software; you can redistribute it and/or modify
12341+ * it under the terms of the GNU General Public License as published by
12342+ * the Free Software Foundation; either version 2 of the License, or
12343+ * (at your option) any later version.
12344+ *
12345+ * This program is distributed in the hope that it will be useful,
12346+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12347+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12348+ * GNU General Public License for more details.
12349+ *
12350+ * You should have received a copy of the GNU General Public License
12351+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
12352+ */
12353+
12354+/*
12355+ * renamed dir info
12356+ */
12357+
12358+#ifndef __AUFS_DIRREN_H__
12359+#define __AUFS_DIRREN_H__
12360+
12361+#ifdef __KERNEL__
12362+
12363+#include <linux/dcache.h>
12364+#include <linux/statfs.h>
12365+#include <linux/uuid.h>
12366+#include "hbl.h"
12367+
12368+#define AuDirren_NHASH 100
12369+
12370+#ifdef CONFIG_AUFS_DIRREN
12371+enum au_brid_type {
12372+ AuBrid_Unset,
12373+ AuBrid_UUID,
12374+ AuBrid_FSID,
12375+ AuBrid_DEV
12376+};
12377+
12378+struct au_dr_brid {
12379+ enum au_brid_type type;
12380+ union {
12381+ uuid_t uuid; /* unimplemented yet */
12382+ fsid_t fsid;
12383+ dev_t dev;
12384+ };
12385+};
12386+
12387+/* 20 is the max digits length of ulong 64 */
12388+/* brid-type "_" uuid "_" inum */
12389+#define AUFS_DIRREN_FNAME_SZ (1 + 1 + UUID_STRING_LEN + 20)
12390+#define AUFS_DIRREN_ENV_VAL_SZ (AUFS_DIRREN_FNAME_SZ + 1 + 20)
12391+
12392+struct au_dr_hino {
12393+ struct hlist_bl_node dr_hnode;
12394+ ino_t dr_h_ino;
12395+};
12396+
12397+struct au_dr_br {
12398+ struct hlist_bl_head dr_h_ino[AuDirren_NHASH];
12399+ struct au_dr_brid dr_brid;
12400+};
12401+
12402+struct au_dr_lookup {
12403+ /* dr_name is pointed by struct au_do_lookup_args.name */
12404+ struct qstr dr_name; /* subset of dr_info */
12405+ aufs_bindex_t ninfo;
12406+ struct au_drinfo **drinfo;
12407+};
12408+#else
12409+struct au_dr_hino;
12410+/* empty */
12411+struct au_dr_br { };
12412+struct au_dr_lookup { };
12413+#endif
12414+
12415+/* ---------------------------------------------------------------------- */
12416+
12417+struct au_branch;
12418+struct au_do_lookup_args;
12419+struct au_hinode;
12420+#ifdef CONFIG_AUFS_DIRREN
12421+int au_dr_hino_test_add(struct au_dr_br *dr, ino_t h_ino,
12422+ struct au_dr_hino *add_ent);
12423+void au_dr_hino_free(struct au_dr_br *dr);
12424+int au_dr_br_init(struct super_block *sb, struct au_branch *br,
12425+ const struct path *path);
12426+int au_dr_br_fin(struct super_block *sb, struct au_branch *br);
12427+int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
12428+ struct qstr *dst_name, void *_rev);
12429+void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *rev);
12430+void au_dr_rename_rev(struct dentry *src, aufs_bindex_t bindex, void *rev);
12431+int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12432+ aufs_bindex_t bindex);
12433+int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12434+int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12435+ ino_t h_ino);
12436+void au_dr_lkup_fin(struct au_do_lookup_args *lkup);
12437+int au_dr_opt_set(struct super_block *sb);
12438+int au_dr_opt_flush(struct super_block *sb);
12439+int au_dr_opt_clr(struct super_block *sb, int no_flush);
12440+#else
12441+AuStubInt0(au_dr_hino_test_add, struct au_dr_br *dr, ino_t h_ino,
12442+ struct au_dr_hino *add_ent);
12443+AuStubVoid(au_dr_hino_free, struct au_dr_br *dr);
12444+AuStubInt0(au_dr_br_init, struct super_block *sb, struct au_branch *br,
12445+ const struct path *path);
12446+AuStubInt0(au_dr_br_fin, struct super_block *sb, struct au_branch *br);
12447+AuStubInt0(au_dr_rename, struct dentry *src, aufs_bindex_t bindex,
12448+ struct qstr *dst_name, void *_rev);
12449+AuStubVoid(au_dr_rename_fin, struct dentry *src, aufs_bindex_t btgt, void *rev);
12450+AuStubVoid(au_dr_rename_rev, struct dentry *src, aufs_bindex_t bindex,
12451+ void *rev);
12452+AuStubInt0(au_dr_lkup, struct au_do_lookup_args *lkup, struct dentry *dentry,
12453+ aufs_bindex_t bindex);
12454+AuStubInt0(au_dr_lkup_name, struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12455+AuStubInt0(au_dr_lkup_h_ino, struct au_do_lookup_args *lkup,
12456+ aufs_bindex_t bindex, ino_t h_ino);
12457+AuStubVoid(au_dr_lkup_fin, struct au_do_lookup_args *lkup);
12458+AuStubInt0(au_dr_opt_set, struct super_block *sb);
12459+AuStubInt0(au_dr_opt_flush, struct super_block *sb);
12460+AuStubInt0(au_dr_opt_clr, struct super_block *sb, int no_flush);
12461+#endif
12462+
12463+/* ---------------------------------------------------------------------- */
12464+
12465+#ifdef CONFIG_AUFS_DIRREN
12466+static inline int au_dr_ihash(ino_t h_ino)
12467+{
12468+ return h_ino % AuDirren_NHASH;
12469+}
12470+#else
12471+AuStubInt0(au_dr_ihash, ino_t h_ino);
12472+#endif
12473+
12474+#endif /* __KERNEL__ */
12475+#endif /* __AUFS_DIRREN_H__ */
7f207e10
AM
12476diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
12477--- /usr/share/empty/fs/aufs/dynop.c 1970-01-01 01:00:00.000000000 +0100
0b487229 12478+++ linux/fs/aufs/dynop.c 2021-02-22 23:30:37.699347580 +0100
062440b3 12479@@ -0,0 +1,370 @@
cd7a4cd9 12480+// SPDX-License-Identifier: GPL-2.0
1facf9fc 12481+/*
0b487229 12482+ * Copyright (C) 2010-2020 Junjiro R. Okajima
1facf9fc 12483+ *
12484+ * This program, aufs is free software; you can redistribute it and/or modify
12485+ * it under the terms of the GNU General Public License as published by
12486+ * the Free Software Foundation; either version 2 of the License, or
12487+ * (at your option) any later version.
dece6358
AM
12488+ *
12489+ * This program is distributed in the hope that it will be useful,
12490+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12491+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12492+ * GNU General Public License for more details.
12493+ *
12494+ * You should have received a copy of the GNU General Public License
523b37e3 12495+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 12496+ */
12497+
12498+/*
4a4d8108 12499+ * dynamically customizable operations for regular files
1facf9fc 12500+ */
12501+
1facf9fc 12502+#include "aufs.h"
12503+
4a4d8108 12504+#define DyPrSym(key) AuDbgSym(key->dk_op.dy_hop)
1facf9fc 12505+
4a4d8108
AM
12506+/*
12507+ * How large will these lists be?
12508+ * Usually just a few elements, 20-30 at most for each, I guess.
12509+ */
8b6a4947 12510+static struct hlist_bl_head dynop[AuDyLast];
4a4d8108 12511+
8b6a4947
AM
12512+static struct au_dykey *dy_gfind_get(struct hlist_bl_head *hbl,
12513+ const void *h_op)
1facf9fc 12514+{
4a4d8108 12515+ struct au_dykey *key, *tmp;
8b6a4947 12516+ struct hlist_bl_node *pos;
1facf9fc 12517+
4a4d8108 12518+ key = NULL;
8b6a4947
AM
12519+ hlist_bl_lock(hbl);
12520+ hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
4a4d8108 12521+ if (tmp->dk_op.dy_hop == h_op) {
0b487229
JR
12522+ if (kref_get_unless_zero(&tmp->dk_kref))
12523+ key = tmp;
4a4d8108
AM
12524+ break;
12525+ }
8b6a4947 12526+ hlist_bl_unlock(hbl);
4a4d8108
AM
12527+
12528+ return key;
1facf9fc 12529+}
12530+
4a4d8108 12531+static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
1facf9fc 12532+{
4a4d8108
AM
12533+ struct au_dykey **k, *found;
12534+ const void *h_op = key->dk_op.dy_hop;
12535+ int i;
1facf9fc 12536+
4a4d8108
AM
12537+ found = NULL;
12538+ k = br->br_dykey;
12539+ for (i = 0; i < AuBrDynOp; i++)
12540+ if (k[i]) {
12541+ if (k[i]->dk_op.dy_hop == h_op) {
12542+ found = k[i];
12543+ break;
12544+ }
12545+ } else
12546+ break;
12547+ if (!found) {
12548+ spin_lock(&br->br_dykey_lock);
12549+ for (; i < AuBrDynOp; i++)
12550+ if (k[i]) {
12551+ if (k[i]->dk_op.dy_hop == h_op) {
12552+ found = k[i];
12553+ break;
12554+ }
12555+ } else {
12556+ k[i] = key;
12557+ break;
12558+ }
12559+ spin_unlock(&br->br_dykey_lock);
12560+ BUG_ON(i == AuBrDynOp); /* expand the array */
12561+ }
12562+
12563+ return found;
1facf9fc 12564+}
12565+
4a4d8108 12566+/* kref_get() if @key is already added */
8b6a4947 12567+static struct au_dykey *dy_gadd(struct hlist_bl_head *hbl, struct au_dykey *key)
4a4d8108
AM
12568+{
12569+ struct au_dykey *tmp, *found;
8b6a4947 12570+ struct hlist_bl_node *pos;
4a4d8108 12571+ const void *h_op = key->dk_op.dy_hop;
1facf9fc 12572+
4a4d8108 12573+ found = NULL;
8b6a4947
AM
12574+ hlist_bl_lock(hbl);
12575+ hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
4a4d8108 12576+ if (tmp->dk_op.dy_hop == h_op) {
0b487229
JR
12577+ if (kref_get_unless_zero(&tmp->dk_kref))
12578+ found = tmp;
4a4d8108
AM
12579+ break;
12580+ }
12581+ if (!found)
8b6a4947
AM
12582+ hlist_bl_add_head(&key->dk_hnode, hbl);
12583+ hlist_bl_unlock(hbl);
1facf9fc 12584+
4a4d8108
AM
12585+ if (!found)
12586+ DyPrSym(key);
12587+ return found;
12588+}
12589+
12590+static void dy_free_rcu(struct rcu_head *rcu)
1facf9fc 12591+{
4a4d8108
AM
12592+ struct au_dykey *key;
12593+
12594+ key = container_of(rcu, struct au_dykey, dk_rcu);
12595+ DyPrSym(key);
0b487229 12596+ kfree(key);
1facf9fc 12597+}
12598+
4a4d8108
AM
12599+static void dy_free(struct kref *kref)
12600+{
12601+ struct au_dykey *key;
8b6a4947 12602+ struct hlist_bl_head *hbl;
1facf9fc 12603+
4a4d8108 12604+ key = container_of(kref, struct au_dykey, dk_kref);
8b6a4947
AM
12605+ hbl = dynop + key->dk_op.dy_type;
12606+ au_hbl_del(&key->dk_hnode, hbl);
4a4d8108
AM
12607+ call_rcu(&key->dk_rcu, dy_free_rcu);
12608+}
12609+
12610+void au_dy_put(struct au_dykey *key)
1facf9fc 12611+{
4a4d8108
AM
12612+ kref_put(&key->dk_kref, dy_free);
12613+}
1facf9fc 12614+
4a4d8108
AM
12615+/* ---------------------------------------------------------------------- */
12616+
12617+#define DyDbgSize(cnt, op) AuDebugOn(cnt != sizeof(op)/sizeof(void *))
12618+
12619+#ifdef CONFIG_AUFS_DEBUG
12620+#define DyDbgDeclare(cnt) unsigned int cnt = 0
4f0767ce 12621+#define DyDbgInc(cnt) do { cnt++; } while (0)
4a4d8108
AM
12622+#else
12623+#define DyDbgDeclare(cnt) do {} while (0)
12624+#define DyDbgInc(cnt) do {} while (0)
12625+#endif
12626+
12627+#define DySet(func, dst, src, h_op, h_sb) do { \
12628+ DyDbgInc(cnt); \
12629+ if (h_op->func) { \
12630+ if (src.func) \
12631+ dst.func = src.func; \
12632+ else \
12633+ AuDbg("%s %s\n", au_sbtype(h_sb), #func); \
12634+ } \
12635+} while (0)
12636+
12637+#define DySetForce(func, dst, src) do { \
12638+ AuDebugOn(!src.func); \
12639+ DyDbgInc(cnt); \
12640+ dst.func = src.func; \
12641+} while (0)
12642+
12643+#define DySetAop(func) \
12644+ DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
12645+#define DySetAopForce(func) \
12646+ DySetForce(func, dyaop->da_op, aufs_aop)
12647+
12648+static void dy_aop(struct au_dykey *key, const void *h_op,
12649+ struct super_block *h_sb __maybe_unused)
12650+{
12651+ struct au_dyaop *dyaop = (void *)key;
12652+ const struct address_space_operations *h_aop = h_op;
12653+ DyDbgDeclare(cnt);
12654+
12655+ AuDbg("%s\n", au_sbtype(h_sb));
12656+
12657+ DySetAop(writepage);
12658+ DySetAopForce(readpage); /* force */
4a4d8108
AM
12659+ DySetAop(writepages);
12660+ DySetAop(set_page_dirty);
12661+ DySetAop(readpages);
12662+ DySetAop(write_begin);
12663+ DySetAop(write_end);
12664+ DySetAop(bmap);
12665+ DySetAop(invalidatepage);
12666+ DySetAop(releasepage);
027c5e7a 12667+ DySetAop(freepage);
7e9cd9fe 12668+ /* this one will be changed according to an aufs mount option */
4a4d8108 12669+ DySetAop(direct_IO);
4a4d8108 12670+ DySetAop(migratepage);
e2f27e51
AM
12671+ DySetAop(isolate_page);
12672+ DySetAop(putback_page);
4a4d8108
AM
12673+ DySetAop(launder_page);
12674+ DySetAop(is_partially_uptodate);
392086de 12675+ DySetAop(is_dirty_writeback);
4a4d8108 12676+ DySetAop(error_remove_page);
b4510431
AM
12677+ DySetAop(swap_activate);
12678+ DySetAop(swap_deactivate);
4a4d8108
AM
12679+
12680+ DyDbgSize(cnt, *h_aop);
4a4d8108
AM
12681+}
12682+
4a4d8108
AM
12683+/* ---------------------------------------------------------------------- */
12684+
12685+static void dy_bug(struct kref *kref)
12686+{
12687+ BUG();
12688+}
12689+
12690+static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
12691+{
12692+ struct au_dykey *key, *old;
8b6a4947 12693+ struct hlist_bl_head *hbl;
b752ccd1 12694+ struct op {
4a4d8108 12695+ unsigned int sz;
b752ccd1
AM
12696+ void (*set)(struct au_dykey *key, const void *h_op,
12697+ struct super_block *h_sb __maybe_unused);
12698+ };
12699+ static const struct op a[] = {
4a4d8108
AM
12700+ [AuDy_AOP] = {
12701+ .sz = sizeof(struct au_dyaop),
b752ccd1 12702+ .set = dy_aop
4a4d8108 12703+ }
b752ccd1
AM
12704+ };
12705+ const struct op *p;
4a4d8108 12706+
8b6a4947
AM
12707+ hbl = dynop + op->dy_type;
12708+ key = dy_gfind_get(hbl, op->dy_hop);
4a4d8108
AM
12709+ if (key)
12710+ goto out_add; /* success */
12711+
12712+ p = a + op->dy_type;
12713+ key = kzalloc(p->sz, GFP_NOFS);
12714+ if (unlikely(!key)) {
12715+ key = ERR_PTR(-ENOMEM);
12716+ goto out;
12717+ }
12718+
12719+ key->dk_op.dy_hop = op->dy_hop;
12720+ kref_init(&key->dk_kref);
86dc4139 12721+ p->set(key, op->dy_hop, au_br_sb(br));
8b6a4947 12722+ old = dy_gadd(hbl, key);
4a4d8108 12723+ if (old) {
e49925d1 12724+ au_kfree_rcu(key);
4a4d8108
AM
12725+ key = old;
12726+ }
12727+
12728+out_add:
12729+ old = dy_bradd(br, key);
12730+ if (old)
12731+ /* its ref-count should never be zero here */
12732+ kref_put(&key->dk_kref, dy_bug);
12733+out:
12734+ return key;
12735+}
12736+
12737+/* ---------------------------------------------------------------------- */
12738+/*
acd2b654 12739+ * Aufs prohibits O_DIRECT by default even if the branch supports it.
c1595e42 12740+ * This behaviour is necessary to return an error from open(O_DIRECT) instead
4a4d8108
AM
12741+ * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
12742+ * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
12743+ * See the aufs manual in detail.
4a4d8108
AM
12744+ */
12745+static void dy_adx(struct au_dyaop *dyaop, int do_dx)
12746+{
7e9cd9fe 12747+ if (!do_dx)
4a4d8108 12748+ dyaop->da_op.direct_IO = NULL;
7e9cd9fe 12749+ else
4a4d8108 12750+ dyaop->da_op.direct_IO = aufs_aop.direct_IO;
4a4d8108
AM
12751+}
12752+
12753+static struct au_dyaop *dy_aget(struct au_branch *br,
12754+ const struct address_space_operations *h_aop,
12755+ int do_dx)
12756+{
12757+ struct au_dyaop *dyaop;
12758+ struct au_dynop op;
12759+
12760+ op.dy_type = AuDy_AOP;
12761+ op.dy_haop = h_aop;
12762+ dyaop = (void *)dy_get(&op, br);
12763+ if (IS_ERR(dyaop))
12764+ goto out;
12765+ dy_adx(dyaop, do_dx);
12766+
12767+out:
12768+ return dyaop;
12769+}
12770+
12771+int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12772+ struct inode *h_inode)
12773+{
12774+ int err, do_dx;
12775+ struct super_block *sb;
12776+ struct au_branch *br;
12777+ struct au_dyaop *dyaop;
12778+
12779+ AuDebugOn(!S_ISREG(h_inode->i_mode));
12780+ IiMustWriteLock(inode);
12781+
12782+ sb = inode->i_sb;
12783+ br = au_sbr(sb, bindex);
12784+ do_dx = !!au_opt_test(au_mntflags(sb), DIO);
12785+ dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
12786+ err = PTR_ERR(dyaop);
12787+ if (IS_ERR(dyaop))
12788+ /* unnecessary to call dy_fput() */
12789+ goto out;
12790+
12791+ err = 0;
12792+ inode->i_mapping->a_ops = &dyaop->da_op;
12793+
12794+out:
12795+ return err;
12796+}
12797+
b752ccd1
AM
12798+/*
12799+ * Is it safe to replace a_ops during the inode/file is in operation?
12800+ * Yes, I hope so.
12801+ */
12802+int au_dy_irefresh(struct inode *inode)
12803+{
12804+ int err;
5afbbe0d 12805+ aufs_bindex_t btop;
b752ccd1
AM
12806+ struct inode *h_inode;
12807+
12808+ err = 0;
12809+ if (S_ISREG(inode->i_mode)) {
5afbbe0d
AM
12810+ btop = au_ibtop(inode);
12811+ h_inode = au_h_iptr(inode, btop);
12812+ err = au_dy_iaop(inode, btop, h_inode);
b752ccd1
AM
12813+ }
12814+ return err;
12815+}
12816+
4a4d8108
AM
12817+void au_dy_arefresh(int do_dx)
12818+{
8b6a4947
AM
12819+ struct hlist_bl_head *hbl;
12820+ struct hlist_bl_node *pos;
4a4d8108
AM
12821+ struct au_dykey *key;
12822+
8b6a4947
AM
12823+ hbl = dynop + AuDy_AOP;
12824+ hlist_bl_lock(hbl);
12825+ hlist_bl_for_each_entry(key, pos, hbl, dk_hnode)
4a4d8108 12826+ dy_adx((void *)key, do_dx);
8b6a4947 12827+ hlist_bl_unlock(hbl);
4a4d8108
AM
12828+}
12829+
4a4d8108
AM
12830+/* ---------------------------------------------------------------------- */
12831+
12832+void __init au_dy_init(void)
12833+{
12834+ int i;
12835+
12836+ /* make sure that 'struct au_dykey *' can be any type */
12837+ BUILD_BUG_ON(offsetof(struct au_dyaop, da_key));
4a4d8108
AM
12838+
12839+ for (i = 0; i < AuDyLast; i++)
8b6a4947 12840+ INIT_HLIST_BL_HEAD(dynop + i);
4a4d8108
AM
12841+}
12842+
12843+void au_dy_fin(void)
12844+{
12845+ int i;
12846+
12847+ for (i = 0; i < AuDyLast; i++)
8b6a4947 12848+ WARN_ON(!hlist_bl_empty(dynop + i));
4a4d8108 12849+}
7f207e10
AM
12850diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
12851--- /usr/share/empty/fs/aufs/dynop.h 1970-01-01 01:00:00.000000000 +0100
0b487229 12852+++ linux/fs/aufs/dynop.h 2021-02-22 23:30:37.699347580 +0100
062440b3
AM
12853@@ -0,0 +1,75 @@
12854+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 12855+/*
0b487229 12856+ * Copyright (C) 2010-2020 Junjiro R. Okajima
4a4d8108
AM
12857+ *
12858+ * This program, aufs is free software; you can redistribute it and/or modify
12859+ * it under the terms of the GNU General Public License as published by
12860+ * the Free Software Foundation; either version 2 of the License, or
12861+ * (at your option) any later version.
12862+ *
12863+ * This program is distributed in the hope that it will be useful,
12864+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12865+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12866+ * GNU General Public License for more details.
12867+ *
12868+ * You should have received a copy of the GNU General Public License
523b37e3 12869+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
12870+ */
12871+
12872+/*
12873+ * dynamically customizable operations (for regular files only)
12874+ */
12875+
12876+#ifndef __AUFS_DYNOP_H__
12877+#define __AUFS_DYNOP_H__
12878+
12879+#ifdef __KERNEL__
12880+
7e9cd9fe
AM
12881+#include <linux/fs.h>
12882+#include <linux/kref.h>
4a4d8108 12883+
2cbb1c4b 12884+enum {AuDy_AOP, AuDyLast};
4a4d8108
AM
12885+
12886+struct au_dynop {
12887+ int dy_type;
12888+ union {
12889+ const void *dy_hop;
12890+ const struct address_space_operations *dy_haop;
4a4d8108
AM
12891+ };
12892+};
12893+
12894+struct au_dykey {
12895+ union {
8b6a4947 12896+ struct hlist_bl_node dk_hnode;
4a4d8108
AM
12897+ struct rcu_head dk_rcu;
12898+ };
12899+ struct au_dynop dk_op;
12900+
12901+ /*
12902+ * during I am in the branch local array, kref is gotten. when the
12903+ * branch is removed, kref is put.
12904+ */
12905+ struct kref dk_kref;
12906+};
12907+
12908+/* stop unioning since their sizes are very different from each other */
12909+struct au_dyaop {
12910+ struct au_dykey da_key;
12911+ struct address_space_operations da_op; /* not const */
4a4d8108
AM
12912+};
12913+
4a4d8108
AM
12914+/* ---------------------------------------------------------------------- */
12915+
12916+/* dynop.c */
12917+struct au_branch;
12918+void au_dy_put(struct au_dykey *key);
12919+int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12920+ struct inode *h_inode);
b752ccd1 12921+int au_dy_irefresh(struct inode *inode);
4a4d8108 12922+void au_dy_arefresh(int do_dio);
4a4d8108
AM
12923+
12924+void __init au_dy_init(void);
12925+void au_dy_fin(void);
12926+
4a4d8108
AM
12927+#endif /* __KERNEL__ */
12928+#endif /* __AUFS_DYNOP_H__ */
7f207e10
AM
12929diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
12930--- /usr/share/empty/fs/aufs/export.c 1970-01-01 01:00:00.000000000 +0100
0b487229 12931+++ linux/fs/aufs/export.c 2021-02-22 23:30:37.699347580 +0100
062440b3 12932@@ -0,0 +1,838 @@
cd7a4cd9 12933+// SPDX-License-Identifier: GPL-2.0
4a4d8108 12934+/*
0b487229 12935+ * Copyright (C) 2005-2020 Junjiro R. Okajima
4a4d8108
AM
12936+ *
12937+ * This program, aufs is free software; you can redistribute it and/or modify
12938+ * it under the terms of the GNU General Public License as published by
12939+ * the Free Software Foundation; either version 2 of the License, or
12940+ * (at your option) any later version.
12941+ *
12942+ * This program is distributed in the hope that it will be useful,
12943+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12944+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12945+ * GNU General Public License for more details.
12946+ *
12947+ * You should have received a copy of the GNU General Public License
523b37e3 12948+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
12949+ */
12950+
12951+/*
12952+ * export via nfs
12953+ */
12954+
12955+#include <linux/exportfs.h>
7eafdf33 12956+#include <linux/fs_struct.h>
4a4d8108
AM
12957+#include <linux/namei.h>
12958+#include <linux/nsproxy.h>
12959+#include <linux/random.h>
12960+#include <linux/writeback.h>
12961+#include "aufs.h"
12962+
12963+union conv {
12964+#ifdef CONFIG_AUFS_INO_T_64
12965+ __u32 a[2];
12966+#else
12967+ __u32 a[1];
12968+#endif
12969+ ino_t ino;
12970+};
12971+
12972+static ino_t decode_ino(__u32 *a)
12973+{
12974+ union conv u;
12975+
12976+ BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
12977+ u.a[0] = a[0];
12978+#ifdef CONFIG_AUFS_INO_T_64
12979+ u.a[1] = a[1];
12980+#endif
12981+ return u.ino;
12982+}
12983+
12984+static void encode_ino(__u32 *a, ino_t ino)
12985+{
12986+ union conv u;
12987+
12988+ u.ino = ino;
12989+ a[0] = u.a[0];
12990+#ifdef CONFIG_AUFS_INO_T_64
12991+ a[1] = u.a[1];
12992+#endif
12993+}
12994+
12995+/* NFS file handle */
12996+enum {
12997+ Fh_br_id,
12998+ Fh_sigen,
12999+#ifdef CONFIG_AUFS_INO_T_64
13000+ /* support 64bit inode number */
13001+ Fh_ino1,
13002+ Fh_ino2,
13003+ Fh_dir_ino1,
13004+ Fh_dir_ino2,
13005+#else
13006+ Fh_ino1,
13007+ Fh_dir_ino1,
13008+#endif
13009+ Fh_igen,
13010+ Fh_h_type,
13011+ Fh_tail,
13012+
13013+ Fh_ino = Fh_ino1,
13014+ Fh_dir_ino = Fh_dir_ino1
13015+};
13016+
13017+static int au_test_anon(struct dentry *dentry)
13018+{
027c5e7a 13019+ /* note: read d_flags without d_lock */
4a4d8108
AM
13020+ return !!(dentry->d_flags & DCACHE_DISCONNECTED);
13021+}
13022+
a2a7ad62
AM
13023+int au_test_nfsd(void)
13024+{
13025+ int ret;
13026+ struct task_struct *tsk = current;
13027+ char comm[sizeof(tsk->comm)];
13028+
13029+ ret = 0;
13030+ if (tsk->flags & PF_KTHREAD) {
13031+ get_task_comm(comm, tsk);
13032+ ret = !strcmp(comm, "nfsd");
13033+ }
13034+
13035+ return ret;
13036+}
13037+
4a4d8108
AM
13038+/* ---------------------------------------------------------------------- */
13039+/* inode generation external table */
13040+
b752ccd1 13041+void au_xigen_inc(struct inode *inode)
4a4d8108 13042+{
4a4d8108
AM
13043+ loff_t pos;
13044+ ssize_t sz;
13045+ __u32 igen;
13046+ struct super_block *sb;
13047+ struct au_sbinfo *sbinfo;
13048+
4a4d8108 13049+ sb = inode->i_sb;
b752ccd1 13050+ AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
1facf9fc 13051+
b752ccd1 13052+ sbinfo = au_sbi(sb);
1facf9fc 13053+ pos = inode->i_ino;
13054+ pos *= sizeof(igen);
13055+ igen = inode->i_generation + 1;
1facf9fc 13056+ sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xigen, &igen,
13057+ sizeof(igen), &pos);
13058+ if (sz == sizeof(igen))
b752ccd1 13059+ return; /* success */
1facf9fc 13060+
b752ccd1 13061+ if (unlikely(sz >= 0))
1facf9fc 13062+ AuIOErr("xigen error (%zd)\n", sz);
1facf9fc 13063+}
13064+
13065+int au_xigen_new(struct inode *inode)
13066+{
13067+ int err;
13068+ loff_t pos;
13069+ ssize_t sz;
13070+ struct super_block *sb;
13071+ struct au_sbinfo *sbinfo;
13072+ struct file *file;
13073+
13074+ err = 0;
13075+ /* todo: dirty, at mount time */
13076+ if (inode->i_ino == AUFS_ROOT_INO)
13077+ goto out;
13078+ sb = inode->i_sb;
dece6358 13079+ SiMustAnyLock(sb);
1facf9fc 13080+ if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13081+ goto out;
13082+
13083+ err = -EFBIG;
13084+ pos = inode->i_ino;
13085+ if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
13086+ AuIOErr1("too large i%lld\n", pos);
13087+ goto out;
13088+ }
13089+ pos *= sizeof(inode->i_generation);
13090+
13091+ err = 0;
13092+ sbinfo = au_sbi(sb);
13093+ file = sbinfo->si_xigen;
13094+ BUG_ON(!file);
13095+
c06a8ce3 13096+ if (vfsub_f_size_read(file)
1facf9fc 13097+ < pos + sizeof(inode->i_generation)) {
13098+ inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
13099+ sz = xino_fwrite(sbinfo->si_xwrite, file, &inode->i_generation,
13100+ sizeof(inode->i_generation), &pos);
13101+ } else
13102+ sz = xino_fread(sbinfo->si_xread, file, &inode->i_generation,
13103+ sizeof(inode->i_generation), &pos);
13104+ if (sz == sizeof(inode->i_generation))
13105+ goto out; /* success */
13106+
13107+ err = sz;
13108+ if (unlikely(sz >= 0)) {
13109+ err = -EIO;
13110+ AuIOErr("xigen error (%zd)\n", sz);
13111+ }
13112+
4f0767ce 13113+out:
1facf9fc 13114+ return err;
13115+}
13116+
062440b3 13117+int au_xigen_set(struct super_block *sb, struct path *path)
1facf9fc 13118+{
13119+ int err;
13120+ struct au_sbinfo *sbinfo;
13121+ struct file *file;
13122+
dece6358
AM
13123+ SiMustWriteLock(sb);
13124+
1facf9fc 13125+ sbinfo = au_sbi(sb);
062440b3 13126+ file = au_xino_create2(sb, path, sbinfo->si_xigen);
1facf9fc 13127+ err = PTR_ERR(file);
13128+ if (IS_ERR(file))
13129+ goto out;
13130+ err = 0;
13131+ if (sbinfo->si_xigen)
13132+ fput(sbinfo->si_xigen);
13133+ sbinfo->si_xigen = file;
13134+
4f0767ce 13135+out:
062440b3 13136+ AuTraceErr(err);
1facf9fc 13137+ return err;
13138+}
13139+
13140+void au_xigen_clr(struct super_block *sb)
13141+{
13142+ struct au_sbinfo *sbinfo;
13143+
dece6358
AM
13144+ SiMustWriteLock(sb);
13145+
1facf9fc 13146+ sbinfo = au_sbi(sb);
13147+ if (sbinfo->si_xigen) {
13148+ fput(sbinfo->si_xigen);
13149+ sbinfo->si_xigen = NULL;
13150+ }
13151+}
13152+
13153+/* ---------------------------------------------------------------------- */
13154+
13155+static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
13156+ ino_t dir_ino)
13157+{
13158+ struct dentry *dentry, *d;
13159+ struct inode *inode;
13160+ unsigned int sigen;
13161+
13162+ dentry = NULL;
13163+ inode = ilookup(sb, ino);
13164+ if (!inode)
13165+ goto out;
13166+
13167+ dentry = ERR_PTR(-ESTALE);
13168+ sigen = au_sigen(sb);
5afbbe0d 13169+ if (unlikely(au_is_bad_inode(inode)
1facf9fc 13170+ || IS_DEADDIR(inode)
537831f9 13171+ || sigen != au_iigen(inode, NULL)))
1facf9fc 13172+ goto out_iput;
13173+
13174+ dentry = NULL;
13175+ if (!dir_ino || S_ISDIR(inode->i_mode))
13176+ dentry = d_find_alias(inode);
13177+ else {
027c5e7a 13178+ spin_lock(&inode->i_lock);
c1595e42 13179+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
027c5e7a 13180+ spin_lock(&d->d_lock);
1facf9fc 13181+ if (!au_test_anon(d)
5527c038 13182+ && d_inode(d->d_parent)->i_ino == dir_ino) {
027c5e7a
AM
13183+ dentry = dget_dlock(d);
13184+ spin_unlock(&d->d_lock);
1facf9fc 13185+ break;
13186+ }
027c5e7a
AM
13187+ spin_unlock(&d->d_lock);
13188+ }
13189+ spin_unlock(&inode->i_lock);
1facf9fc 13190+ }
027c5e7a 13191+ if (unlikely(dentry && au_digen_test(dentry, sigen))) {
2cbb1c4b 13192+ /* need to refresh */
1facf9fc 13193+ dput(dentry);
2cbb1c4b 13194+ dentry = NULL;
1facf9fc 13195+ }
13196+
4f0767ce 13197+out_iput:
1facf9fc 13198+ iput(inode);
4f0767ce 13199+out:
2cbb1c4b 13200+ AuTraceErrPtr(dentry);
1facf9fc 13201+ return dentry;
13202+}
13203+
13204+/* ---------------------------------------------------------------------- */
13205+
13206+/* todo: dirty? */
13207+/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
4a4d8108
AM
13208+
13209+struct au_compare_mnt_args {
13210+ /* input */
13211+ struct super_block *sb;
13212+
13213+ /* output */
13214+ struct vfsmount *mnt;
13215+};
13216+
13217+static int au_compare_mnt(struct vfsmount *mnt, void *arg)
13218+{
13219+ struct au_compare_mnt_args *a = arg;
13220+
13221+ if (mnt->mnt_sb != a->sb)
13222+ return 0;
13223+ a->mnt = mntget(mnt);
13224+ return 1;
13225+}
13226+
1facf9fc 13227+static struct vfsmount *au_mnt_get(struct super_block *sb)
13228+{
4a4d8108 13229+ int err;
7eafdf33 13230+ struct path root;
4a4d8108
AM
13231+ struct au_compare_mnt_args args = {
13232+ .sb = sb
13233+ };
1facf9fc 13234+
7eafdf33 13235+ get_fs_root(current->fs, &root);
523b37e3 13236+ rcu_read_lock();
7eafdf33 13237+ err = iterate_mounts(au_compare_mnt, &args, root.mnt);
523b37e3 13238+ rcu_read_unlock();
7eafdf33 13239+ path_put(&root);
4a4d8108
AM
13240+ AuDebugOn(!err);
13241+ AuDebugOn(!args.mnt);
13242+ return args.mnt;
1facf9fc 13243+}
13244+
13245+struct au_nfsd_si_lock {
4a4d8108 13246+ unsigned int sigen;
027c5e7a 13247+ aufs_bindex_t bindex, br_id;
1facf9fc 13248+ unsigned char force_lock;
13249+};
13250+
027c5e7a
AM
13251+static int si_nfsd_read_lock(struct super_block *sb,
13252+ struct au_nfsd_si_lock *nsi_lock)
1facf9fc 13253+{
027c5e7a 13254+ int err;
1facf9fc 13255+ aufs_bindex_t bindex;
13256+
13257+ si_read_lock(sb, AuLock_FLUSH);
13258+
13259+ /* branch id may be wrapped around */
027c5e7a 13260+ err = 0;
1facf9fc 13261+ bindex = au_br_index(sb, nsi_lock->br_id);
13262+ if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
13263+ goto out; /* success */
13264+
027c5e7a
AM
13265+ err = -ESTALE;
13266+ bindex = -1;
1facf9fc 13267+ if (!nsi_lock->force_lock)
13268+ si_read_unlock(sb);
1facf9fc 13269+
4f0767ce 13270+out:
027c5e7a
AM
13271+ nsi_lock->bindex = bindex;
13272+ return err;
1facf9fc 13273+}
13274+
13275+struct find_name_by_ino {
392086de 13276+ struct dir_context ctx;
1facf9fc 13277+ int called, found;
13278+ ino_t ino;
13279+ char *name;
13280+ int namelen;
13281+};
13282+
13283+static int
392086de
AM
13284+find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
13285+ loff_t offset, u64 ino, unsigned int d_type)
1facf9fc 13286+{
392086de
AM
13287+ struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
13288+ ctx);
1facf9fc 13289+
13290+ a->called++;
13291+ if (a->ino != ino)
13292+ return 0;
13293+
13294+ memcpy(a->name, name, namelen);
13295+ a->namelen = namelen;
13296+ a->found = 1;
13297+ return 1;
13298+}
13299+
13300+static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
13301+ struct au_nfsd_si_lock *nsi_lock)
13302+{
13303+ struct dentry *dentry, *parent;
13304+ struct file *file;
13305+ struct inode *dir;
392086de
AM
13306+ struct find_name_by_ino arg = {
13307+ .ctx = {
2000de60 13308+ .actor = find_name_by_ino
392086de
AM
13309+ }
13310+ };
1facf9fc 13311+ int err;
13312+
13313+ parent = path->dentry;
13314+ if (nsi_lock)
13315+ si_read_unlock(parent->d_sb);
4a4d8108 13316+ file = vfsub_dentry_open(path, au_dir_roflags);
1facf9fc 13317+ dentry = (void *)file;
13318+ if (IS_ERR(file))
13319+ goto out;
13320+
13321+ dentry = ERR_PTR(-ENOMEM);
537831f9 13322+ arg.name = (void *)__get_free_page(GFP_NOFS);
1facf9fc 13323+ if (unlikely(!arg.name))
13324+ goto out_file;
13325+ arg.ino = ino;
13326+ arg.found = 0;
13327+ do {
13328+ arg.called = 0;
13329+ /* smp_mb(); */
392086de 13330+ err = vfsub_iterate_dir(file, &arg.ctx);
1facf9fc 13331+ } while (!err && !arg.found && arg.called);
13332+ dentry = ERR_PTR(err);
13333+ if (unlikely(err))
13334+ goto out_name;
1716fcea
AM
13335+ /* instead of ENOENT */
13336+ dentry = ERR_PTR(-ESTALE);
1facf9fc 13337+ if (!arg.found)
13338+ goto out_name;
13339+
b4510431 13340+ /* do not call vfsub_lkup_one() */
5527c038 13341+ dir = d_inode(parent);
febd17d6 13342+ dentry = vfsub_lookup_one_len_unlocked(arg.name, parent, arg.namelen);
1facf9fc 13343+ AuTraceErrPtr(dentry);
13344+ if (IS_ERR(dentry))
13345+ goto out_name;
13346+ AuDebugOn(au_test_anon(dentry));
5527c038 13347+ if (unlikely(d_really_is_negative(dentry))) {
1facf9fc 13348+ dput(dentry);
13349+ dentry = ERR_PTR(-ENOENT);
13350+ }
13351+
4f0767ce 13352+out_name:
1c60b727 13353+ free_page((unsigned long)arg.name);
4f0767ce 13354+out_file:
1facf9fc 13355+ fput(file);
4f0767ce 13356+out:
1facf9fc 13357+ if (unlikely(nsi_lock
13358+ && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
13359+ if (!IS_ERR(dentry)) {
13360+ dput(dentry);
13361+ dentry = ERR_PTR(-ESTALE);
13362+ }
13363+ AuTraceErrPtr(dentry);
13364+ return dentry;
13365+}
13366+
13367+static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
13368+ ino_t dir_ino,
13369+ struct au_nfsd_si_lock *nsi_lock)
13370+{
13371+ struct dentry *dentry;
13372+ struct path path;
13373+
13374+ if (dir_ino != AUFS_ROOT_INO) {
13375+ path.dentry = decode_by_ino(sb, dir_ino, 0);
13376+ dentry = path.dentry;
13377+ if (!path.dentry || IS_ERR(path.dentry))
13378+ goto out;
13379+ AuDebugOn(au_test_anon(path.dentry));
13380+ } else
13381+ path.dentry = dget(sb->s_root);
13382+
13383+ path.mnt = au_mnt_get(sb);
13384+ dentry = au_lkup_by_ino(&path, ino, nsi_lock);
13385+ path_put(&path);
13386+
4f0767ce 13387+out:
1facf9fc 13388+ AuTraceErrPtr(dentry);
13389+ return dentry;
13390+}
13391+
13392+/* ---------------------------------------------------------------------- */
13393+
13394+static int h_acceptable(void *expv, struct dentry *dentry)
13395+{
13396+ return 1;
13397+}
13398+
13399+static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
13400+ char *buf, int len, struct super_block *sb)
13401+{
13402+ char *p;
13403+ int n;
13404+ struct path path;
13405+
13406+ p = d_path(h_rootpath, buf, len);
13407+ if (IS_ERR(p))
13408+ goto out;
13409+ n = strlen(p);
13410+
13411+ path.mnt = h_rootpath->mnt;
13412+ path.dentry = h_parent;
13413+ p = d_path(&path, buf, len);
13414+ if (IS_ERR(p))
13415+ goto out;
13416+ if (n != 1)
13417+ p += n;
13418+
13419+ path.mnt = au_mnt_get(sb);
13420+ path.dentry = sb->s_root;
13421+ p = d_path(&path, buf, len - strlen(p));
13422+ mntput(path.mnt);
13423+ if (IS_ERR(p))
13424+ goto out;
13425+ if (n != 1)
13426+ p[strlen(p)] = '/';
13427+
4f0767ce 13428+out:
1facf9fc 13429+ AuTraceErrPtr(p);
13430+ return p;
13431+}
13432+
13433+static
027c5e7a
AM
13434+struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
13435+ int fh_len, struct au_nfsd_si_lock *nsi_lock)
1facf9fc 13436+{
13437+ struct dentry *dentry, *h_parent, *root;
13438+ struct super_block *h_sb;
13439+ char *pathname, *p;
13440+ struct vfsmount *h_mnt;
13441+ struct au_branch *br;
13442+ int err;
13443+ struct path path;
13444+
027c5e7a 13445+ br = au_sbr(sb, nsi_lock->bindex);
86dc4139 13446+ h_mnt = au_br_mnt(br);
1facf9fc 13447+ h_sb = h_mnt->mnt_sb;
13448+ /* todo: call lower fh_to_dentry()? fh_to_parent()? */
5afbbe0d 13449+ lockdep_off();
1facf9fc 13450+ h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
13451+ fh_len - Fh_tail, fh[Fh_h_type],
13452+ h_acceptable, /*context*/NULL);
5afbbe0d 13453+ lockdep_on();
1facf9fc 13454+ dentry = h_parent;
13455+ if (unlikely(!h_parent || IS_ERR(h_parent))) {
13456+ AuWarn1("%s decode_fh failed, %ld\n",
13457+ au_sbtype(h_sb), PTR_ERR(h_parent));
13458+ goto out;
13459+ }
13460+ dentry = NULL;
13461+ if (unlikely(au_test_anon(h_parent))) {
13462+ AuWarn1("%s decode_fh returned a disconnected dentry\n",
13463+ au_sbtype(h_sb));
13464+ goto out_h_parent;
13465+ }
13466+
13467+ dentry = ERR_PTR(-ENOMEM);
13468+ pathname = (void *)__get_free_page(GFP_NOFS);
13469+ if (unlikely(!pathname))
13470+ goto out_h_parent;
13471+
13472+ root = sb->s_root;
13473+ path.mnt = h_mnt;
13474+ di_read_lock_parent(root, !AuLock_IR);
027c5e7a 13475+ path.dentry = au_h_dptr(root, nsi_lock->bindex);
1facf9fc 13476+ di_read_unlock(root, !AuLock_IR);
13477+ p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
13478+ dentry = (void *)p;
13479+ if (IS_ERR(p))
13480+ goto out_pathname;
13481+
13482+ si_read_unlock(sb);
13483+ err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
13484+ dentry = ERR_PTR(err);
13485+ if (unlikely(err))
13486+ goto out_relock;
13487+
13488+ dentry = ERR_PTR(-ENOENT);
13489+ AuDebugOn(au_test_anon(path.dentry));
5527c038 13490+ if (unlikely(d_really_is_negative(path.dentry)))
1facf9fc 13491+ goto out_path;
13492+
5527c038 13493+ if (ino != d_inode(path.dentry)->i_ino)
1facf9fc 13494+ dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
13495+ else
13496+ dentry = dget(path.dentry);
13497+
4f0767ce 13498+out_path:
1facf9fc 13499+ path_put(&path);
4f0767ce 13500+out_relock:
1facf9fc 13501+ if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
13502+ if (!IS_ERR(dentry)) {
13503+ dput(dentry);
13504+ dentry = ERR_PTR(-ESTALE);
13505+ }
4f0767ce 13506+out_pathname:
1c60b727 13507+ free_page((unsigned long)pathname);
4f0767ce 13508+out_h_parent:
1facf9fc 13509+ dput(h_parent);
4f0767ce 13510+out:
1facf9fc 13511+ AuTraceErrPtr(dentry);
13512+ return dentry;
13513+}
13514+
13515+/* ---------------------------------------------------------------------- */
13516+
13517+static struct dentry *
13518+aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
13519+ int fh_type)
13520+{
13521+ struct dentry *dentry;
13522+ __u32 *fh = fid->raw;
027c5e7a 13523+ struct au_branch *br;
1facf9fc 13524+ ino_t ino, dir_ino;
1facf9fc 13525+ struct au_nfsd_si_lock nsi_lock = {
1facf9fc 13526+ .force_lock = 0
13527+ };
13528+
1facf9fc 13529+ dentry = ERR_PTR(-ESTALE);
4a4d8108
AM
13530+ /* it should never happen, but the file handle is unreliable */
13531+ if (unlikely(fh_len < Fh_tail))
13532+ goto out;
13533+ nsi_lock.sigen = fh[Fh_sigen];
13534+ nsi_lock.br_id = fh[Fh_br_id];
13535+
1facf9fc 13536+ /* branch id may be wrapped around */
027c5e7a
AM
13537+ br = NULL;
13538+ if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
1facf9fc 13539+ goto out;
13540+ nsi_lock.force_lock = 1;
13541+
13542+ /* is this inode still cached? */
13543+ ino = decode_ino(fh + Fh_ino);
4a4d8108
AM
13544+ /* it should never happen */
13545+ if (unlikely(ino == AUFS_ROOT_INO))
8cdd5066 13546+ goto out_unlock;
4a4d8108 13547+
1facf9fc 13548+ dir_ino = decode_ino(fh + Fh_dir_ino);
13549+ dentry = decode_by_ino(sb, ino, dir_ino);
13550+ if (IS_ERR(dentry))
13551+ goto out_unlock;
13552+ if (dentry)
13553+ goto accept;
13554+
13555+ /* is the parent dir cached? */
027c5e7a 13556+ br = au_sbr(sb, nsi_lock.bindex);
acd2b654 13557+ au_lcnt_inc(&br->br_nfiles);
1facf9fc 13558+ dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
13559+ if (IS_ERR(dentry))
13560+ goto out_unlock;
13561+ if (dentry)
13562+ goto accept;
13563+
13564+ /* lookup path */
027c5e7a 13565+ dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
1facf9fc 13566+ if (IS_ERR(dentry))
13567+ goto out_unlock;
13568+ if (unlikely(!dentry))
13569+ /* todo?: make it ESTALE */
13570+ goto out_unlock;
13571+
4f0767ce 13572+accept:
027c5e7a 13573+ if (!au_digen_test(dentry, au_sigen(sb))
5527c038 13574+ && d_inode(dentry)->i_generation == fh[Fh_igen])
1facf9fc 13575+ goto out_unlock; /* success */
13576+
13577+ dput(dentry);
13578+ dentry = ERR_PTR(-ESTALE);
4f0767ce 13579+out_unlock:
027c5e7a 13580+ if (br)
acd2b654 13581+ au_lcnt_dec(&br->br_nfiles);
1facf9fc 13582+ si_read_unlock(sb);
4f0767ce 13583+out:
1facf9fc 13584+ AuTraceErrPtr(dentry);
13585+ return dentry;
13586+}
13587+
13588+#if 0 /* reserved for future use */
13589+/* support subtreecheck option */
13590+static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
13591+ int fh_len, int fh_type)
13592+{
13593+ struct dentry *parent;
13594+ __u32 *fh = fid->raw;
13595+ ino_t dir_ino;
13596+
13597+ dir_ino = decode_ino(fh + Fh_dir_ino);
13598+ parent = decode_by_ino(sb, dir_ino, 0);
13599+ if (IS_ERR(parent))
13600+ goto out;
13601+ if (!parent)
13602+ parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
13603+ dir_ino, fh, fh_len);
13604+
4f0767ce 13605+out:
1facf9fc 13606+ AuTraceErrPtr(parent);
13607+ return parent;
13608+}
13609+#endif
13610+
13611+/* ---------------------------------------------------------------------- */
13612+
0c3ec466
AM
13613+static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
13614+ struct inode *dir)
1facf9fc 13615+{
13616+ int err;
0c3ec466 13617+ aufs_bindex_t bindex;
1facf9fc 13618+ struct super_block *sb, *h_sb;
0c3ec466
AM
13619+ struct dentry *dentry, *parent, *h_parent;
13620+ struct inode *h_dir;
1facf9fc 13621+ struct au_branch *br;
13622+
1facf9fc 13623+ err = -ENOSPC;
13624+ if (unlikely(*max_len <= Fh_tail)) {
13625+ AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
13626+ goto out;
13627+ }
13628+
13629+ err = FILEID_ROOT;
0c3ec466
AM
13630+ if (inode->i_ino == AUFS_ROOT_INO) {
13631+ AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
1facf9fc 13632+ goto out;
13633+ }
13634+
1facf9fc 13635+ h_parent = NULL;
0c3ec466
AM
13636+ sb = inode->i_sb;
13637+ err = si_read_lock(sb, AuLock_FLUSH);
027c5e7a
AM
13638+ if (unlikely(err))
13639+ goto out;
13640+
1facf9fc 13641+#ifdef CONFIG_AUFS_DEBUG
13642+ if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13643+ AuWarn1("NFS-exporting requires xino\n");
13644+#endif
027c5e7a 13645+ err = -EIO;
0c3ec466
AM
13646+ parent = NULL;
13647+ ii_read_lock_child(inode);
5afbbe0d 13648+ bindex = au_ibtop(inode);
0c3ec466 13649+ if (!dir) {
c1595e42 13650+ dentry = d_find_any_alias(inode);
0c3ec466
AM
13651+ if (unlikely(!dentry))
13652+ goto out_unlock;
13653+ AuDebugOn(au_test_anon(dentry));
13654+ parent = dget_parent(dentry);
13655+ dput(dentry);
13656+ if (unlikely(!parent))
13657+ goto out_unlock;
5527c038
JR
13658+ if (d_really_is_positive(parent))
13659+ dir = d_inode(parent);
1facf9fc 13660+ }
0c3ec466
AM
13661+
13662+ ii_read_lock_parent(dir);
13663+ h_dir = au_h_iptr(dir, bindex);
13664+ ii_read_unlock(dir);
13665+ if (unlikely(!h_dir))
13666+ goto out_parent;
c1595e42 13667+ h_parent = d_find_any_alias(h_dir);
1facf9fc 13668+ if (unlikely(!h_parent))
0c3ec466 13669+ goto out_hparent;
1facf9fc 13670+
13671+ err = -EPERM;
13672+ br = au_sbr(sb, bindex);
86dc4139 13673+ h_sb = au_br_sb(br);
1facf9fc 13674+ if (unlikely(!h_sb->s_export_op)) {
13675+ AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
0c3ec466 13676+ goto out_hparent;
1facf9fc 13677+ }
13678+
13679+ fh[Fh_br_id] = br->br_id;
13680+ fh[Fh_sigen] = au_sigen(sb);
13681+ encode_ino(fh + Fh_ino, inode->i_ino);
0c3ec466 13682+ encode_ino(fh + Fh_dir_ino, dir->i_ino);
1facf9fc 13683+ fh[Fh_igen] = inode->i_generation;
13684+
13685+ *max_len -= Fh_tail;
13686+ fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
13687+ max_len,
13688+ /*connectable or subtreecheck*/0);
13689+ err = fh[Fh_h_type];
13690+ *max_len += Fh_tail;
13691+ /* todo: macros? */
1716fcea 13692+ if (err != FILEID_INVALID)
1facf9fc 13693+ err = 99;
13694+ else
13695+ AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
13696+
0c3ec466 13697+out_hparent:
1facf9fc 13698+ dput(h_parent);
0c3ec466 13699+out_parent:
1facf9fc 13700+ dput(parent);
0c3ec466
AM
13701+out_unlock:
13702+ ii_read_unlock(inode);
13703+ si_read_unlock(sb);
4f0767ce 13704+out:
1facf9fc 13705+ if (unlikely(err < 0))
1716fcea 13706+ err = FILEID_INVALID;
1facf9fc 13707+ return err;
13708+}
13709+
13710+/* ---------------------------------------------------------------------- */
13711+
4a4d8108
AM
13712+static int aufs_commit_metadata(struct inode *inode)
13713+{
13714+ int err;
13715+ aufs_bindex_t bindex;
13716+ struct super_block *sb;
13717+ struct inode *h_inode;
13718+ int (*f)(struct inode *inode);
13719+
13720+ sb = inode->i_sb;
e49829fe 13721+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
4a4d8108 13722+ ii_write_lock_child(inode);
5afbbe0d 13723+ bindex = au_ibtop(inode);
4a4d8108
AM
13724+ AuDebugOn(bindex < 0);
13725+ h_inode = au_h_iptr(inode, bindex);
13726+
13727+ f = h_inode->i_sb->s_export_op->commit_metadata;
13728+ if (f)
13729+ err = f(h_inode);
13730+ else {
13731+ struct writeback_control wbc = {
13732+ .sync_mode = WB_SYNC_ALL,
13733+ .nr_to_write = 0 /* metadata only */
13734+ };
13735+
13736+ err = sync_inode(h_inode, &wbc);
13737+ }
13738+
13739+ au_cpup_attr_timesizes(inode);
13740+ ii_write_unlock(inode);
13741+ si_read_unlock(sb);
13742+ return err;
13743+}
13744+
13745+/* ---------------------------------------------------------------------- */
13746+
1facf9fc 13747+static struct export_operations aufs_export_op = {
4a4d8108 13748+ .fh_to_dentry = aufs_fh_to_dentry,
1facf9fc 13749+ /* .fh_to_parent = aufs_fh_to_parent, */
4a4d8108
AM
13750+ .encode_fh = aufs_encode_fh,
13751+ .commit_metadata = aufs_commit_metadata
1facf9fc 13752+};
13753+
13754+void au_export_init(struct super_block *sb)
13755+{
13756+ struct au_sbinfo *sbinfo;
13757+ __u32 u;
13758+
5afbbe0d
AM
13759+ BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
13760+ && IS_MODULE(CONFIG_EXPORTFS),
13761+ AUFS_NAME ": unsupported configuration "
13762+ "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
13763+
1facf9fc 13764+ sb->s_export_op = &aufs_export_op;
13765+ sbinfo = au_sbi(sb);
13766+ sbinfo->si_xigen = NULL;
13767+ get_random_bytes(&u, sizeof(u));
13768+ BUILD_BUG_ON(sizeof(u) != sizeof(int));
13769+ atomic_set(&sbinfo->si_xigen_next, u);
13770+}
076b876e
AM
13771diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
13772--- /usr/share/empty/fs/aufs/fhsm.c 1970-01-01 01:00:00.000000000 +0100
0b487229 13773+++ linux/fs/aufs/fhsm.c 2021-02-22 23:30:37.699347580 +0100
062440b3 13774@@ -0,0 +1,427 @@
cd7a4cd9 13775+// SPDX-License-Identifier: GPL-2.0
076b876e 13776+/*
0b487229 13777+ * Copyright (C) 2011-2020 Junjiro R. Okajima
076b876e
AM
13778+ *
13779+ * This program, aufs is free software; you can redistribute it and/or modify
13780+ * it under the terms of the GNU General Public License as published by
13781+ * the Free Software Foundation; either version 2 of the License, or
13782+ * (at your option) any later version.
13783+ *
13784+ * This program is distributed in the hope that it will be useful,
13785+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13786+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13787+ * GNU General Public License for more details.
13788+ *
13789+ * You should have received a copy of the GNU General Public License
13790+ * along with this program; if not, write to the Free Software
13791+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
13792+ */
13793+
13794+/*
13795+ * File-based Hierarchy Storage Management
13796+ */
13797+
13798+#include <linux/anon_inodes.h>
13799+#include <linux/poll.h>
13800+#include <linux/seq_file.h>
13801+#include <linux/statfs.h>
13802+#include "aufs.h"
13803+
c1595e42
JR
13804+static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
13805+{
13806+ struct au_sbinfo *sbinfo;
13807+ struct au_fhsm *fhsm;
13808+
13809+ SiMustAnyLock(sb);
13810+
13811+ sbinfo = au_sbi(sb);
13812+ fhsm = &sbinfo->si_fhsm;
13813+ AuDebugOn(!fhsm);
13814+ return fhsm->fhsm_bottom;
13815+}
13816+
13817+void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
13818+{
13819+ struct au_sbinfo *sbinfo;
13820+ struct au_fhsm *fhsm;
13821+
13822+ SiMustWriteLock(sb);
13823+
13824+ sbinfo = au_sbi(sb);
13825+ fhsm = &sbinfo->si_fhsm;
13826+ AuDebugOn(!fhsm);
13827+ fhsm->fhsm_bottom = bindex;
13828+}
13829+
13830+/* ---------------------------------------------------------------------- */
13831+
076b876e
AM
13832+static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
13833+{
13834+ struct au_br_fhsm *bf;
13835+
13836+ bf = br->br_fhsm;
13837+ MtxMustLock(&bf->bf_lock);
13838+
13839+ return !bf->bf_readable
13840+ || time_after(jiffies,
13841+ bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
13842+}
13843+
13844+/* ---------------------------------------------------------------------- */
13845+
13846+static void au_fhsm_notify(struct super_block *sb, int val)
13847+{
13848+ struct au_sbinfo *sbinfo;
13849+ struct au_fhsm *fhsm;
13850+
13851+ SiMustAnyLock(sb);
13852+
13853+ sbinfo = au_sbi(sb);
13854+ fhsm = &sbinfo->si_fhsm;
13855+ if (au_fhsm_pid(fhsm)
13856+ && atomic_read(&fhsm->fhsm_readable) != -1) {
13857+ atomic_set(&fhsm->fhsm_readable, val);
13858+ if (val)
13859+ wake_up(&fhsm->fhsm_wqh);
13860+ }
13861+}
13862+
13863+static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
13864+ struct aufs_stfs *rstfs, int do_lock, int do_notify)
13865+{
13866+ int err;
13867+ struct au_branch *br;
13868+ struct au_br_fhsm *bf;
13869+
13870+ br = au_sbr(sb, bindex);
13871+ AuDebugOn(au_br_rdonly(br));
13872+ bf = br->br_fhsm;
13873+ AuDebugOn(!bf);
13874+
13875+ if (do_lock)
13876+ mutex_lock(&bf->bf_lock);
13877+ else
13878+ MtxMustLock(&bf->bf_lock);
13879+
13880+ /* sb->s_root for NFS is unreliable */
13881+ err = au_br_stfs(br, &bf->bf_stfs);
13882+ if (unlikely(err)) {
13883+ AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
13884+ goto out;
13885+ }
13886+
13887+ bf->bf_jiffy = jiffies;
13888+ bf->bf_readable = 1;
13889+ if (do_notify)
13890+ au_fhsm_notify(sb, /*val*/1);
13891+ if (rstfs)
13892+ *rstfs = bf->bf_stfs;
13893+
13894+out:
13895+ if (do_lock)
13896+ mutex_unlock(&bf->bf_lock);
13897+ au_fhsm_notify(sb, /*val*/1);
13898+
13899+ return err;
13900+}
13901+
13902+void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
13903+{
13904+ int err;
076b876e
AM
13905+ struct au_sbinfo *sbinfo;
13906+ struct au_fhsm *fhsm;
13907+ struct au_branch *br;
13908+ struct au_br_fhsm *bf;
13909+
13910+ AuDbg("b%d, force %d\n", bindex, force);
13911+ SiMustAnyLock(sb);
13912+
13913+ sbinfo = au_sbi(sb);
13914+ fhsm = &sbinfo->si_fhsm;
c1595e42
JR
13915+ if (!au_ftest_si(sbinfo, FHSM)
13916+ || fhsm->fhsm_bottom == bindex)
076b876e
AM
13917+ return;
13918+
13919+ br = au_sbr(sb, bindex);
13920+ bf = br->br_fhsm;
13921+ AuDebugOn(!bf);
13922+ mutex_lock(&bf->bf_lock);
13923+ if (force
13924+ || au_fhsm_pid(fhsm)
13925+ || au_fhsm_test_jiffy(sbinfo, br))
13926+ err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
13927+ /*do_notify*/1);
13928+ mutex_unlock(&bf->bf_lock);
13929+}
13930+
13931+void au_fhsm_wrote_all(struct super_block *sb, int force)
13932+{
5afbbe0d 13933+ aufs_bindex_t bindex, bbot;
076b876e
AM
13934+ struct au_branch *br;
13935+
13936+ /* exclude the bottom */
5afbbe0d
AM
13937+ bbot = au_fhsm_bottom(sb);
13938+ for (bindex = 0; bindex < bbot; bindex++) {
076b876e
AM
13939+ br = au_sbr(sb, bindex);
13940+ if (au_br_fhsm(br->br_perm))
13941+ au_fhsm_wrote(sb, bindex, force);
13942+ }
13943+}
13944+
13945+/* ---------------------------------------------------------------------- */
13946+
be118d29 13947+static __poll_t au_fhsm_poll(struct file *file, struct poll_table_struct *wait)
076b876e 13948+{
be118d29 13949+ __poll_t mask;
076b876e
AM
13950+ struct au_sbinfo *sbinfo;
13951+ struct au_fhsm *fhsm;
13952+
13953+ mask = 0;
13954+ sbinfo = file->private_data;
13955+ fhsm = &sbinfo->si_fhsm;
13956+ poll_wait(file, &fhsm->fhsm_wqh, wait);
13957+ if (atomic_read(&fhsm->fhsm_readable))
be118d29 13958+ mask = EPOLLIN /* | EPOLLRDNORM */;
076b876e 13959+
b00004a5
AM
13960+ if (!mask)
13961+ AuDbg("mask 0x%x\n", mask);
076b876e
AM
13962+ return mask;
13963+}
13964+
13965+static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
13966+ struct aufs_stfs *stfs, __s16 brid)
13967+{
13968+ int err;
13969+
13970+ err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
13971+ if (!err)
13972+ err = __put_user(brid, &stbr->brid);
13973+ if (unlikely(err))
13974+ err = -EFAULT;
13975+
13976+ return err;
13977+}
13978+
13979+static ssize_t au_fhsm_do_read(struct super_block *sb,
13980+ struct aufs_stbr __user *stbr, size_t count)
13981+{
13982+ ssize_t err;
13983+ int nstbr;
5afbbe0d 13984+ aufs_bindex_t bindex, bbot;
076b876e
AM
13985+ struct au_branch *br;
13986+ struct au_br_fhsm *bf;
13987+
13988+ /* except the bottom branch */
13989+ err = 0;
13990+ nstbr = 0;
5afbbe0d
AM
13991+ bbot = au_fhsm_bottom(sb);
13992+ for (bindex = 0; !err && bindex < bbot; bindex++) {
076b876e
AM
13993+ br = au_sbr(sb, bindex);
13994+ if (!au_br_fhsm(br->br_perm))
13995+ continue;
13996+
13997+ bf = br->br_fhsm;
13998+ mutex_lock(&bf->bf_lock);
13999+ if (bf->bf_readable) {
14000+ err = -EFAULT;
14001+ if (count >= sizeof(*stbr))
14002+ err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
14003+ br->br_id);
14004+ if (!err) {
14005+ bf->bf_readable = 0;
14006+ count -= sizeof(*stbr);
14007+ nstbr++;
14008+ }
14009+ }
14010+ mutex_unlock(&bf->bf_lock);
14011+ }
14012+ if (!err)
14013+ err = sizeof(*stbr) * nstbr;
14014+
14015+ return err;
14016+}
14017+
14018+static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
14019+ loff_t *pos)
14020+{
14021+ ssize_t err;
14022+ int readable;
5afbbe0d 14023+ aufs_bindex_t nfhsm, bindex, bbot;
076b876e
AM
14024+ struct au_sbinfo *sbinfo;
14025+ struct au_fhsm *fhsm;
14026+ struct au_branch *br;
14027+ struct super_block *sb;
14028+
14029+ err = 0;
14030+ sbinfo = file->private_data;
14031+ fhsm = &sbinfo->si_fhsm;
14032+need_data:
14033+ spin_lock_irq(&fhsm->fhsm_wqh.lock);
14034+ if (!atomic_read(&fhsm->fhsm_readable)) {
14035+ if (vfsub_file_flags(file) & O_NONBLOCK)
14036+ err = -EAGAIN;
14037+ else
14038+ err = wait_event_interruptible_locked_irq
14039+ (fhsm->fhsm_wqh,
14040+ atomic_read(&fhsm->fhsm_readable));
14041+ }
14042+ spin_unlock_irq(&fhsm->fhsm_wqh.lock);
14043+ if (unlikely(err))
14044+ goto out;
14045+
14046+ /* sb may already be dead */
14047+ au_rw_read_lock(&sbinfo->si_rwsem);
14048+ readable = atomic_read(&fhsm->fhsm_readable);
14049+ if (readable > 0) {
14050+ sb = sbinfo->si_sb;
14051+ AuDebugOn(!sb);
14052+ /* exclude the bottom branch */
14053+ nfhsm = 0;
5afbbe0d
AM
14054+ bbot = au_fhsm_bottom(sb);
14055+ for (bindex = 0; bindex < bbot; bindex++) {
076b876e
AM
14056+ br = au_sbr(sb, bindex);
14057+ if (au_br_fhsm(br->br_perm))
14058+ nfhsm++;
14059+ }
14060+ err = -EMSGSIZE;
14061+ if (nfhsm * sizeof(struct aufs_stbr) <= count) {
14062+ atomic_set(&fhsm->fhsm_readable, 0);
14063+ err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
14064+ count);
14065+ }
14066+ }
14067+ au_rw_read_unlock(&sbinfo->si_rwsem);
14068+ if (!readable)
14069+ goto need_data;
14070+
14071+out:
14072+ return err;
14073+}
14074+
14075+static int au_fhsm_release(struct inode *inode, struct file *file)
14076+{
14077+ struct au_sbinfo *sbinfo;
14078+ struct au_fhsm *fhsm;
14079+
14080+ /* sb may already be dead */
14081+ sbinfo = file->private_data;
14082+ fhsm = &sbinfo->si_fhsm;
14083+ spin_lock(&fhsm->fhsm_spin);
14084+ fhsm->fhsm_pid = 0;
14085+ spin_unlock(&fhsm->fhsm_spin);
14086+ kobject_put(&sbinfo->si_kobj);
14087+
14088+ return 0;
14089+}
14090+
14091+static const struct file_operations au_fhsm_fops = {
14092+ .owner = THIS_MODULE,
14093+ .llseek = noop_llseek,
14094+ .read = au_fhsm_read,
14095+ .poll = au_fhsm_poll,
14096+ .release = au_fhsm_release
14097+};
14098+
14099+int au_fhsm_fd(struct super_block *sb, int oflags)
14100+{
14101+ int err, fd;
14102+ struct au_sbinfo *sbinfo;
14103+ struct au_fhsm *fhsm;
14104+
14105+ err = -EPERM;
14106+ if (unlikely(!capable(CAP_SYS_ADMIN)))
14107+ goto out;
14108+
14109+ err = -EINVAL;
14110+ if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
14111+ goto out;
14112+
14113+ err = 0;
14114+ sbinfo = au_sbi(sb);
14115+ fhsm = &sbinfo->si_fhsm;
14116+ spin_lock(&fhsm->fhsm_spin);
14117+ if (!fhsm->fhsm_pid)
14118+ fhsm->fhsm_pid = current->pid;
14119+ else
14120+ err = -EBUSY;
14121+ spin_unlock(&fhsm->fhsm_spin);
14122+ if (unlikely(err))
14123+ goto out;
14124+
14125+ oflags |= O_RDONLY;
14126+ /* oflags |= FMODE_NONOTIFY; */
14127+ fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
14128+ err = fd;
14129+ if (unlikely(fd < 0))
14130+ goto out_pid;
14131+
acd2b654 14132+ /* succeed regardless 'fhsm' status */
076b876e
AM
14133+ kobject_get(&sbinfo->si_kobj);
14134+ si_noflush_read_lock(sb);
14135+ if (au_ftest_si(sbinfo, FHSM))
14136+ au_fhsm_wrote_all(sb, /*force*/0);
14137+ si_read_unlock(sb);
14138+ goto out; /* success */
14139+
14140+out_pid:
14141+ spin_lock(&fhsm->fhsm_spin);
14142+ fhsm->fhsm_pid = 0;
14143+ spin_unlock(&fhsm->fhsm_spin);
14144+out:
14145+ AuTraceErr(err);
14146+ return err;
14147+}
14148+
14149+/* ---------------------------------------------------------------------- */
14150+
14151+int au_fhsm_br_alloc(struct au_branch *br)
14152+{
14153+ int err;
14154+
14155+ err = 0;
14156+ br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
14157+ if (br->br_fhsm)
14158+ au_br_fhsm_init(br->br_fhsm);
14159+ else
14160+ err = -ENOMEM;
14161+
14162+ return err;
14163+}
14164+
14165+/* ---------------------------------------------------------------------- */
14166+
14167+void au_fhsm_fin(struct super_block *sb)
14168+{
14169+ au_fhsm_notify(sb, /*val*/-1);
14170+}
14171+
14172+void au_fhsm_init(struct au_sbinfo *sbinfo)
14173+{
14174+ struct au_fhsm *fhsm;
14175+
14176+ fhsm = &sbinfo->si_fhsm;
14177+ spin_lock_init(&fhsm->fhsm_spin);
14178+ init_waitqueue_head(&fhsm->fhsm_wqh);
14179+ atomic_set(&fhsm->fhsm_readable, 0);
14180+ fhsm->fhsm_expire
14181+ = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
c1595e42 14182+ fhsm->fhsm_bottom = -1;
076b876e
AM
14183+}
14184+
14185+void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
14186+{
14187+ sbinfo->si_fhsm.fhsm_expire
14188+ = msecs_to_jiffies(sec * MSEC_PER_SEC);
14189+}
14190+
14191+void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
14192+{
14193+ unsigned int u;
14194+
14195+ if (!au_ftest_si(sbinfo, FHSM))
14196+ return;
14197+
14198+ u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
14199+ if (u != AUFS_FHSM_CACHE_DEF_SEC)
14200+ seq_printf(seq, ",fhsm_sec=%u", u);
14201+}
7f207e10
AM
14202diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
14203--- /usr/share/empty/fs/aufs/file.c 1970-01-01 01:00:00.000000000 +0100
0b487229 14204+++ linux/fs/aufs/file.c 2021-02-22 23:30:37.702680911 +0100
acd2b654 14205@@ -0,0 +1,863 @@
cd7a4cd9 14206+// SPDX-License-Identifier: GPL-2.0
1facf9fc 14207+/*
0b487229 14208+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 14209+ *
14210+ * This program, aufs is free software; you can redistribute it and/or modify
14211+ * it under the terms of the GNU General Public License as published by
14212+ * the Free Software Foundation; either version 2 of the License, or
14213+ * (at your option) any later version.
dece6358
AM
14214+ *
14215+ * This program is distributed in the hope that it will be useful,
14216+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14217+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14218+ * GNU General Public License for more details.
14219+ *
14220+ * You should have received a copy of the GNU General Public License
523b37e3 14221+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 14222+ */
14223+
14224+/*
4a4d8108 14225+ * handling file/dir, and address_space operation
1facf9fc 14226+ */
14227+
7eafdf33
AM
14228+#ifdef CONFIG_AUFS_DEBUG
14229+#include <linux/migrate.h>
14230+#endif
4a4d8108 14231+#include <linux/pagemap.h>
1facf9fc 14232+#include "aufs.h"
14233+
4a4d8108
AM
14234+/* drop flags for writing */
14235+unsigned int au_file_roflags(unsigned int flags)
14236+{
14237+ flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
14238+ flags |= O_RDONLY | O_NOATIME;
14239+ return flags;
14240+}
14241+
14242+/* common functions to regular file and dir */
14243+struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
392086de 14244+ struct file *file, int force_wr)
1facf9fc 14245+{
1308ab2a 14246+ struct file *h_file;
4a4d8108
AM
14247+ struct dentry *h_dentry;
14248+ struct inode *h_inode;
14249+ struct super_block *sb;
14250+ struct au_branch *br;
14251+ struct path h_path;
b912730e 14252+ int err;
1facf9fc 14253+
4a4d8108
AM
14254+ /* a race condition can happen between open and unlink/rmdir */
14255+ h_file = ERR_PTR(-ENOENT);
14256+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 14257+ if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
4a4d8108 14258+ goto out;
5527c038 14259+ h_inode = d_inode(h_dentry);
027c5e7a
AM
14260+ spin_lock(&h_dentry->d_lock);
14261+ err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
5527c038 14262+ /* || !d_inode(dentry)->i_nlink */
027c5e7a
AM
14263+ ;
14264+ spin_unlock(&h_dentry->d_lock);
14265+ if (unlikely(err))
4a4d8108 14266+ goto out;
1facf9fc 14267+
4a4d8108
AM
14268+ sb = dentry->d_sb;
14269+ br = au_sbr(sb, bindex);
b912730e
AM
14270+ err = au_br_test_oflag(flags, br);
14271+ h_file = ERR_PTR(err);
14272+ if (unlikely(err))
027c5e7a 14273+ goto out;
1facf9fc 14274+
4a4d8108 14275+ /* drop flags for writing */
5527c038 14276+ if (au_test_ro(sb, bindex, d_inode(dentry))) {
392086de
AM
14277+ if (force_wr && !(flags & O_WRONLY))
14278+ force_wr = 0;
4a4d8108 14279+ flags = au_file_roflags(flags);
392086de
AM
14280+ if (force_wr) {
14281+ h_file = ERR_PTR(-EROFS);
14282+ flags = au_file_roflags(flags);
14283+ if (unlikely(vfsub_native_ro(h_inode)
14284+ || IS_APPEND(h_inode)))
14285+ goto out;
14286+ flags &= ~O_ACCMODE;
14287+ flags |= O_WRONLY;
14288+ }
14289+ }
4a4d8108 14290+ flags &= ~O_CREAT;
acd2b654 14291+ au_lcnt_inc(&br->br_nfiles);
4a4d8108 14292+ h_path.dentry = h_dentry;
86dc4139 14293+ h_path.mnt = au_br_mnt(br);
38d290e6 14294+ h_file = vfsub_dentry_open(&h_path, flags);
4a4d8108
AM
14295+ if (IS_ERR(h_file))
14296+ goto out_br;
dece6358 14297+
b912730e 14298+ if (flags & __FMODE_EXEC) {
4a4d8108
AM
14299+ err = deny_write_access(h_file);
14300+ if (unlikely(err)) {
14301+ fput(h_file);
14302+ h_file = ERR_PTR(err);
14303+ goto out_br;
14304+ }
14305+ }
953406b4 14306+ fsnotify_open(h_file);
4a4d8108 14307+ goto out; /* success */
1facf9fc 14308+
4f0767ce 14309+out_br:
acd2b654 14310+ au_lcnt_dec(&br->br_nfiles);
4f0767ce 14311+out:
4a4d8108
AM
14312+ return h_file;
14313+}
1308ab2a 14314+
076b876e
AM
14315+static int au_cmoo(struct dentry *dentry)
14316+{
8b6a4947 14317+ int err, cmoo, matched;
076b876e
AM
14318+ unsigned int udba;
14319+ struct path h_path;
14320+ struct au_pin pin;
14321+ struct au_cp_generic cpg = {
14322+ .dentry = dentry,
14323+ .bdst = -1,
14324+ .bsrc = -1,
14325+ .len = -1,
14326+ .pin = &pin,
14327+ .flags = AuCpup_DTIME | AuCpup_HOPEN
14328+ };
7e9cd9fe 14329+ struct inode *delegated;
076b876e
AM
14330+ struct super_block *sb;
14331+ struct au_sbinfo *sbinfo;
14332+ struct au_fhsm *fhsm;
14333+ pid_t pid;
14334+ struct au_branch *br;
14335+ struct dentry *parent;
14336+ struct au_hinode *hdir;
14337+
14338+ DiMustWriteLock(dentry);
5527c038 14339+ IiMustWriteLock(d_inode(dentry));
076b876e
AM
14340+
14341+ err = 0;
14342+ if (IS_ROOT(dentry))
14343+ goto out;
5afbbe0d 14344+ cpg.bsrc = au_dbtop(dentry);
076b876e
AM
14345+ if (!cpg.bsrc)
14346+ goto out;
14347+
14348+ sb = dentry->d_sb;
14349+ sbinfo = au_sbi(sb);
14350+ fhsm = &sbinfo->si_fhsm;
14351+ pid = au_fhsm_pid(fhsm);
8b6a4947
AM
14352+ rcu_read_lock();
14353+ matched = (pid
14354+ && (current->pid == pid
14355+ || rcu_dereference(current->real_parent)->pid == pid));
14356+ rcu_read_unlock();
14357+ if (matched)
076b876e
AM
14358+ goto out;
14359+
14360+ br = au_sbr(sb, cpg.bsrc);
14361+ cmoo = au_br_cmoo(br->br_perm);
14362+ if (!cmoo)
14363+ goto out;
7e9cd9fe 14364+ if (!d_is_reg(dentry))
076b876e
AM
14365+ cmoo &= AuBrAttr_COO_ALL;
14366+ if (!cmoo)
14367+ goto out;
14368+
14369+ parent = dget_parent(dentry);
14370+ di_write_lock_parent(parent);
14371+ err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
14372+ cpg.bdst = err;
14373+ if (unlikely(err < 0)) {
14374+ err = 0; /* there is no upper writable branch */
14375+ goto out_dgrade;
14376+ }
14377+ AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
14378+
14379+ /* do not respect the coo attrib for the target branch */
14380+ err = au_cpup_dirs(dentry, cpg.bdst);
14381+ if (unlikely(err))
14382+ goto out_dgrade;
14383+
14384+ di_downgrade_lock(parent, AuLock_IR);
14385+ udba = au_opt_udba(sb);
14386+ err = au_pin(&pin, dentry, cpg.bdst, udba,
14387+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14388+ if (unlikely(err))
14389+ goto out_parent;
14390+
14391+ err = au_sio_cpup_simple(&cpg);
14392+ au_unpin(&pin);
14393+ if (unlikely(err))
14394+ goto out_parent;
14395+ if (!(cmoo & AuBrWAttr_MOO))
14396+ goto out_parent; /* success */
14397+
14398+ err = au_pin(&pin, dentry, cpg.bsrc, udba,
14399+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14400+ if (unlikely(err))
14401+ goto out_parent;
14402+
14403+ h_path.mnt = au_br_mnt(br);
14404+ h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
5527c038 14405+ hdir = au_hi(d_inode(parent), cpg.bsrc);
076b876e
AM
14406+ delegated = NULL;
14407+ err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
14408+ au_unpin(&pin);
14409+ /* todo: keep h_dentry or not? */
14410+ if (unlikely(err == -EWOULDBLOCK)) {
14411+ pr_warn("cannot retry for NFSv4 delegation"
14412+ " for an internal unlink\n");
14413+ iput(delegated);
14414+ }
14415+ if (unlikely(err)) {
14416+ pr_err("unlink %pd after coo failed (%d), ignored\n",
14417+ dentry, err);
14418+ err = 0;
14419+ }
14420+ goto out_parent; /* success */
14421+
14422+out_dgrade:
14423+ di_downgrade_lock(parent, AuLock_IR);
14424+out_parent:
14425+ di_read_unlock(parent, AuLock_IR);
14426+ dput(parent);
14427+out:
14428+ AuTraceErr(err);
14429+ return err;
14430+}
14431+
b912730e 14432+int au_do_open(struct file *file, struct au_do_open_args *args)
1facf9fc 14433+{
8b6a4947 14434+ int err, aopen = args->aopen;
1facf9fc 14435+ struct dentry *dentry;
076b876e 14436+ struct au_finfo *finfo;
1308ab2a 14437+
8b6a4947 14438+ if (!aopen)
b912730e
AM
14439+ err = au_finfo_init(file, args->fidir);
14440+ else {
14441+ lockdep_off();
14442+ err = au_finfo_init(file, args->fidir);
14443+ lockdep_on();
14444+ }
4a4d8108
AM
14445+ if (unlikely(err))
14446+ goto out;
1facf9fc 14447+
2000de60 14448+ dentry = file->f_path.dentry;
b912730e 14449+ AuDebugOn(IS_ERR_OR_NULL(dentry));
8b6a4947
AM
14450+ di_write_lock_child(dentry);
14451+ err = au_cmoo(dentry);
14452+ di_downgrade_lock(dentry, AuLock_IR);
14453+ if (!err) {
14454+ if (!aopen)
b912730e 14455+ err = args->open(file, vfsub_file_flags(file), NULL);
8b6a4947
AM
14456+ else {
14457+ lockdep_off();
acd2b654
AM
14458+ err = args->open(file, vfsub_file_flags(file),
14459+ args->h_file);
8b6a4947
AM
14460+ lockdep_on();
14461+ }
b912730e 14462+ }
8b6a4947 14463+ di_read_unlock(dentry, AuLock_IR);
1facf9fc 14464+
076b876e
AM
14465+ finfo = au_fi(file);
14466+ if (!err) {
14467+ finfo->fi_file = file;
8b6a4947
AM
14468+ au_hbl_add(&finfo->fi_hlist,
14469+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
076b876e 14470+ }
8b6a4947 14471+ if (!aopen)
b912730e
AM
14472+ fi_write_unlock(file);
14473+ else {
14474+ lockdep_off();
14475+ fi_write_unlock(file);
14476+ lockdep_on();
14477+ }
4a4d8108 14478+ if (unlikely(err)) {
076b876e 14479+ finfo->fi_hdir = NULL;
1c60b727 14480+ au_finfo_fin(file);
1308ab2a 14481+ }
4a4d8108 14482+
4f0767ce 14483+out:
8b6a4947 14484+ AuTraceErr(err);
1308ab2a 14485+ return err;
14486+}
dece6358 14487+
4a4d8108 14488+int au_reopen_nondir(struct file *file)
1308ab2a 14489+{
4a4d8108 14490+ int err;
5afbbe0d 14491+ aufs_bindex_t btop;
4a4d8108 14492+ struct dentry *dentry;
acd2b654 14493+ struct au_branch *br;
4a4d8108 14494+ struct file *h_file, *h_file_tmp;
1308ab2a 14495+
2000de60 14496+ dentry = file->f_path.dentry;
5afbbe0d 14497+ btop = au_dbtop(dentry);
acd2b654 14498+ br = au_sbr(dentry->d_sb, btop);
4a4d8108 14499+ h_file_tmp = NULL;
5afbbe0d 14500+ if (au_fbtop(file) == btop) {
4a4d8108
AM
14501+ h_file = au_hf_top(file);
14502+ if (file->f_mode == h_file->f_mode)
14503+ return 0; /* success */
14504+ h_file_tmp = h_file;
14505+ get_file(h_file_tmp);
acd2b654 14506+ au_lcnt_inc(&br->br_nfiles);
5afbbe0d 14507+ au_set_h_fptr(file, btop, NULL);
4a4d8108
AM
14508+ }
14509+ AuDebugOn(au_fi(file)->fi_hdir);
86dc4139
AM
14510+ /*
14511+ * it can happen
14512+ * file exists on both of rw and ro
5afbbe0d 14513+ * open --> dbtop and fbtop are both 0
86dc4139
AM
14514+ * prepend a branch as rw, "rw" become ro
14515+ * remove rw/file
14516+ * delete the top branch, "rw" becomes rw again
5afbbe0d
AM
14517+ * --> dbtop is 1, fbtop is still 0
14518+ * write --> fbtop is 0 but dbtop is 1
86dc4139 14519+ */
5afbbe0d 14520+ /* AuDebugOn(au_fbtop(file) < btop); */
1308ab2a 14521+
5afbbe0d 14522+ h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
392086de 14523+ file, /*force_wr*/0);
4a4d8108 14524+ err = PTR_ERR(h_file);
86dc4139
AM
14525+ if (IS_ERR(h_file)) {
14526+ if (h_file_tmp) {
acd2b654 14527+ /* revert */
5afbbe0d 14528+ au_set_h_fptr(file, btop, h_file_tmp);
86dc4139
AM
14529+ h_file_tmp = NULL;
14530+ }
4a4d8108 14531+ goto out; /* todo: close all? */
86dc4139 14532+ }
4a4d8108
AM
14533+
14534+ err = 0;
5afbbe0d
AM
14535+ au_set_fbtop(file, btop);
14536+ au_set_h_fptr(file, btop, h_file);
4a4d8108
AM
14537+ au_update_figen(file);
14538+ /* todo: necessary? */
14539+ /* file->f_ra = h_file->f_ra; */
14540+
4f0767ce 14541+out:
acd2b654 14542+ if (h_file_tmp) {
4a4d8108 14543+ fput(h_file_tmp);
acd2b654
AM
14544+ au_lcnt_dec(&br->br_nfiles);
14545+ }
4a4d8108 14546+ return err;
1facf9fc 14547+}
14548+
1308ab2a 14549+/* ---------------------------------------------------------------------- */
14550+
4a4d8108
AM
14551+static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
14552+ struct dentry *hi_wh)
1facf9fc 14553+{
4a4d8108 14554+ int err;
5afbbe0d 14555+ aufs_bindex_t btop;
4a4d8108
AM
14556+ struct au_dinfo *dinfo;
14557+ struct dentry *h_dentry;
14558+ struct au_hdentry *hdp;
1facf9fc 14559+
2000de60 14560+ dinfo = au_di(file->f_path.dentry);
4a4d8108 14561+ AuRwMustWriteLock(&dinfo->di_rwsem);
dece6358 14562+
5afbbe0d
AM
14563+ btop = dinfo->di_btop;
14564+ dinfo->di_btop = btgt;
14565+ hdp = au_hdentry(dinfo, btgt);
14566+ h_dentry = hdp->hd_dentry;
14567+ hdp->hd_dentry = hi_wh;
4a4d8108 14568+ err = au_reopen_nondir(file);
5afbbe0d
AM
14569+ hdp->hd_dentry = h_dentry;
14570+ dinfo->di_btop = btop;
1facf9fc 14571+
1facf9fc 14572+ return err;
14573+}
14574+
4a4d8108 14575+static int au_ready_to_write_wh(struct file *file, loff_t len,
86dc4139 14576+ aufs_bindex_t bcpup, struct au_pin *pin)
1facf9fc 14577+{
4a4d8108 14578+ int err;
027c5e7a 14579+ struct inode *inode, *h_inode;
c2b27bf2
AM
14580+ struct dentry *h_dentry, *hi_wh;
14581+ struct au_cp_generic cpg = {
2000de60 14582+ .dentry = file->f_path.dentry,
c2b27bf2
AM
14583+ .bdst = bcpup,
14584+ .bsrc = -1,
14585+ .len = len,
14586+ .pin = pin
14587+ };
1facf9fc 14588+
5afbbe0d 14589+ au_update_dbtop(cpg.dentry);
5527c038 14590+ inode = d_inode(cpg.dentry);
027c5e7a 14591+ h_inode = NULL;
5afbbe0d
AM
14592+ if (au_dbtop(cpg.dentry) <= bcpup
14593+ && au_dbbot(cpg.dentry) >= bcpup) {
c2b27bf2 14594+ h_dentry = au_h_dptr(cpg.dentry, bcpup);
5527c038
JR
14595+ if (h_dentry && d_is_positive(h_dentry))
14596+ h_inode = d_inode(h_dentry);
027c5e7a 14597+ }
4a4d8108 14598+ hi_wh = au_hi_wh(inode, bcpup);
027c5e7a 14599+ if (!hi_wh && !h_inode)
c2b27bf2 14600+ err = au_sio_cpup_wh(&cpg, file);
4a4d8108
AM
14601+ else
14602+ /* already copied-up after unlink */
14603+ err = au_reopen_wh(file, bcpup, hi_wh);
1facf9fc 14604+
4a4d8108 14605+ if (!err
38d290e6
JR
14606+ && (inode->i_nlink > 1
14607+ || (inode->i_state & I_LINKABLE))
c2b27bf2
AM
14608+ && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
14609+ au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
1308ab2a 14610+
dece6358 14611+ return err;
1facf9fc 14612+}
14613+
4a4d8108
AM
14614+/*
14615+ * prepare the @file for writing.
14616+ */
14617+int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
1facf9fc 14618+{
4a4d8108 14619+ int err;
5afbbe0d 14620+ aufs_bindex_t dbtop;
c1595e42 14621+ struct dentry *parent;
86dc4139 14622+ struct inode *inode;
1facf9fc 14623+ struct super_block *sb;
4a4d8108 14624+ struct file *h_file;
c2b27bf2 14625+ struct au_cp_generic cpg = {
2000de60 14626+ .dentry = file->f_path.dentry,
c2b27bf2
AM
14627+ .bdst = -1,
14628+ .bsrc = -1,
14629+ .len = len,
14630+ .pin = pin,
14631+ .flags = AuCpup_DTIME
14632+ };
1facf9fc 14633+
c2b27bf2 14634+ sb = cpg.dentry->d_sb;
5527c038 14635+ inode = d_inode(cpg.dentry);
5afbbe0d 14636+ cpg.bsrc = au_fbtop(file);
c2b27bf2 14637+ err = au_test_ro(sb, cpg.bsrc, inode);
4a4d8108 14638+ if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
c2b27bf2
AM
14639+ err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
14640+ /*flags*/0);
1facf9fc 14641+ goto out;
4a4d8108 14642+ }
1facf9fc 14643+
027c5e7a 14644+ /* need to cpup or reopen */
c2b27bf2 14645+ parent = dget_parent(cpg.dentry);
4a4d8108 14646+ di_write_lock_parent(parent);
c2b27bf2
AM
14647+ err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14648+ cpg.bdst = err;
4a4d8108
AM
14649+ if (unlikely(err < 0))
14650+ goto out_dgrade;
14651+ err = 0;
14652+
c2b27bf2
AM
14653+ if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
14654+ err = au_cpup_dirs(cpg.dentry, cpg.bdst);
1facf9fc 14655+ if (unlikely(err))
4a4d8108
AM
14656+ goto out_dgrade;
14657+ }
14658+
c2b27bf2 14659+ err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
4a4d8108
AM
14660+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14661+ if (unlikely(err))
14662+ goto out_dgrade;
14663+
5afbbe0d
AM
14664+ dbtop = au_dbtop(cpg.dentry);
14665+ if (dbtop <= cpg.bdst)
c2b27bf2 14666+ cpg.bsrc = cpg.bdst;
027c5e7a 14667+
5afbbe0d 14668+ if (dbtop <= cpg.bdst /* just reopen */
c2b27bf2 14669+ || !d_unhashed(cpg.dentry) /* copyup and reopen */
027c5e7a 14670+ ) {
392086de 14671+ h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
86dc4139 14672+ if (IS_ERR(h_file))
027c5e7a 14673+ err = PTR_ERR(h_file);
86dc4139 14674+ else {
027c5e7a 14675+ di_downgrade_lock(parent, AuLock_IR);
5afbbe0d 14676+ if (dbtop > cpg.bdst)
c2b27bf2 14677+ err = au_sio_cpup_simple(&cpg);
027c5e7a
AM
14678+ if (!err)
14679+ err = au_reopen_nondir(file);
c2b27bf2 14680+ au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
027c5e7a 14681+ }
027c5e7a
AM
14682+ } else { /* copyup as wh and reopen */
14683+ /*
14684+ * since writable hfsplus branch is not supported,
14685+ * h_open_pre/post() are unnecessary.
14686+ */
c2b27bf2 14687+ err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
4a4d8108 14688+ di_downgrade_lock(parent, AuLock_IR);
4a4d8108 14689+ }
4a4d8108
AM
14690+
14691+ if (!err) {
14692+ au_pin_set_parent_lflag(pin, /*lflag*/0);
14693+ goto out_dput; /* success */
14694+ }
14695+ au_unpin(pin);
14696+ goto out_unlock;
1facf9fc 14697+
4f0767ce 14698+out_dgrade:
4a4d8108 14699+ di_downgrade_lock(parent, AuLock_IR);
4f0767ce 14700+out_unlock:
4a4d8108 14701+ di_read_unlock(parent, AuLock_IR);
4f0767ce 14702+out_dput:
4a4d8108 14703+ dput(parent);
4f0767ce 14704+out:
1facf9fc 14705+ return err;
14706+}
14707+
4a4d8108
AM
14708+/* ---------------------------------------------------------------------- */
14709+
14710+int au_do_flush(struct file *file, fl_owner_t id,
14711+ int (*flush)(struct file *file, fl_owner_t id))
1facf9fc 14712+{
4a4d8108 14713+ int err;
1facf9fc 14714+ struct super_block *sb;
4a4d8108 14715+ struct inode *inode;
1facf9fc 14716+
c06a8ce3
AM
14717+ inode = file_inode(file);
14718+ sb = inode->i_sb;
4a4d8108
AM
14719+ si_noflush_read_lock(sb);
14720+ fi_read_lock(file);
b752ccd1 14721+ ii_read_lock_child(inode);
1facf9fc 14722+
4a4d8108
AM
14723+ err = flush(file, id);
14724+ au_cpup_attr_timesizes(inode);
1facf9fc 14725+
b752ccd1 14726+ ii_read_unlock(inode);
4a4d8108 14727+ fi_read_unlock(file);
1308ab2a 14728+ si_read_unlock(sb);
dece6358 14729+ return err;
1facf9fc 14730+}
14731+
4a4d8108
AM
14732+/* ---------------------------------------------------------------------- */
14733+
14734+static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
1facf9fc 14735+{
4a4d8108 14736+ int err;
4a4d8108
AM
14737+ struct au_pin pin;
14738+ struct au_finfo *finfo;
c2b27bf2 14739+ struct dentry *parent, *hi_wh;
4a4d8108 14740+ struct inode *inode;
1facf9fc 14741+ struct super_block *sb;
c2b27bf2 14742+ struct au_cp_generic cpg = {
2000de60 14743+ .dentry = file->f_path.dentry,
c2b27bf2
AM
14744+ .bdst = -1,
14745+ .bsrc = -1,
14746+ .len = -1,
14747+ .pin = &pin,
14748+ .flags = AuCpup_DTIME
14749+ };
1facf9fc 14750+
4a4d8108
AM
14751+ FiMustWriteLock(file);
14752+
14753+ err = 0;
14754+ finfo = au_fi(file);
c2b27bf2 14755+ sb = cpg.dentry->d_sb;
5527c038 14756+ inode = d_inode(cpg.dentry);
5afbbe0d 14757+ cpg.bdst = au_ibtop(inode);
c2b27bf2 14758+ if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
1308ab2a 14759+ goto out;
dece6358 14760+
c2b27bf2
AM
14761+ parent = dget_parent(cpg.dentry);
14762+ if (au_test_ro(sb, cpg.bdst, inode)) {
4a4d8108 14763+ di_read_lock_parent(parent, !AuLock_IR);
c2b27bf2
AM
14764+ err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14765+ cpg.bdst = err;
4a4d8108
AM
14766+ di_read_unlock(parent, !AuLock_IR);
14767+ if (unlikely(err < 0))
14768+ goto out_parent;
14769+ err = 0;
1facf9fc 14770+ }
1facf9fc 14771+
4a4d8108 14772+ di_read_lock_parent(parent, AuLock_IR);
c2b27bf2 14773+ hi_wh = au_hi_wh(inode, cpg.bdst);
7f207e10
AM
14774+ if (!S_ISDIR(inode->i_mode)
14775+ && au_opt_test(au_mntflags(sb), PLINK)
4a4d8108 14776+ && au_plink_test(inode)
c2b27bf2 14777+ && !d_unhashed(cpg.dentry)
5afbbe0d 14778+ && cpg.bdst < au_dbtop(cpg.dentry)) {
c2b27bf2 14779+ err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
4a4d8108
AM
14780+ if (unlikely(err))
14781+ goto out_unlock;
14782+
14783+ /* always superio. */
c2b27bf2 14784+ err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
4a4d8108 14785+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
367653fa 14786+ if (!err) {
c2b27bf2 14787+ err = au_sio_cpup_simple(&cpg);
367653fa
AM
14788+ au_unpin(&pin);
14789+ }
4a4d8108
AM
14790+ } else if (hi_wh) {
14791+ /* already copied-up after unlink */
c2b27bf2 14792+ err = au_reopen_wh(file, cpg.bdst, hi_wh);
4a4d8108
AM
14793+ *need_reopen = 0;
14794+ }
1facf9fc 14795+
4f0767ce 14796+out_unlock:
4a4d8108 14797+ di_read_unlock(parent, AuLock_IR);
4f0767ce 14798+out_parent:
4a4d8108 14799+ dput(parent);
4f0767ce 14800+out:
1308ab2a 14801+ return err;
dece6358 14802+}
1facf9fc 14803+
4a4d8108 14804+static void au_do_refresh_dir(struct file *file)
dece6358 14805+{
5afbbe0d 14806+ aufs_bindex_t bindex, bbot, new_bindex, brid;
4a4d8108
AM
14807+ struct au_hfile *p, tmp, *q;
14808+ struct au_finfo *finfo;
1308ab2a 14809+ struct super_block *sb;
4a4d8108 14810+ struct au_fidir *fidir;
1facf9fc 14811+
4a4d8108 14812+ FiMustWriteLock(file);
1facf9fc 14813+
2000de60 14814+ sb = file->f_path.dentry->d_sb;
4a4d8108
AM
14815+ finfo = au_fi(file);
14816+ fidir = finfo->fi_hdir;
14817+ AuDebugOn(!fidir);
14818+ p = fidir->fd_hfile + finfo->fi_btop;
14819+ brid = p->hf_br->br_id;
5afbbe0d
AM
14820+ bbot = fidir->fd_bbot;
14821+ for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
4a4d8108
AM
14822+ if (!p->hf_file)
14823+ continue;
1308ab2a 14824+
4a4d8108
AM
14825+ new_bindex = au_br_index(sb, p->hf_br->br_id);
14826+ if (new_bindex == bindex)
14827+ continue;
14828+ if (new_bindex < 0) {
14829+ au_set_h_fptr(file, bindex, NULL);
14830+ continue;
14831+ }
1308ab2a 14832+
4a4d8108
AM
14833+ /* swap two lower inode, and loop again */
14834+ q = fidir->fd_hfile + new_bindex;
14835+ tmp = *q;
14836+ *q = *p;
14837+ *p = tmp;
14838+ if (tmp.hf_file) {
14839+ bindex--;
14840+ p--;
14841+ }
14842+ }
1308ab2a 14843+
4a4d8108 14844+ p = fidir->fd_hfile;
2000de60 14845+ if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
5afbbe0d
AM
14846+ bbot = au_sbbot(sb);
14847+ for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
4a4d8108
AM
14848+ finfo->fi_btop++, p++)
14849+ if (p->hf_file) {
c06a8ce3 14850+ if (file_inode(p->hf_file))
4a4d8108 14851+ break;
1c60b727 14852+ au_hfput(p, /*execed*/0);
4a4d8108
AM
14853+ }
14854+ } else {
5afbbe0d
AM
14855+ bbot = au_br_index(sb, brid);
14856+ for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
4a4d8108
AM
14857+ finfo->fi_btop++, p++)
14858+ if (p->hf_file)
1c60b727 14859+ au_hfput(p, /*execed*/0);
5afbbe0d 14860+ bbot = au_sbbot(sb);
4a4d8108 14861+ }
1308ab2a 14862+
5afbbe0d
AM
14863+ p = fidir->fd_hfile + bbot;
14864+ for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
4a4d8108
AM
14865+ fidir->fd_bbot--, p--)
14866+ if (p->hf_file) {
c06a8ce3 14867+ if (file_inode(p->hf_file))
4a4d8108 14868+ break;
1c60b727 14869+ au_hfput(p, /*execed*/0);
4a4d8108
AM
14870+ }
14871+ AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
1308ab2a 14872+}
14873+
4a4d8108
AM
14874+/*
14875+ * after branch manipulating, refresh the file.
14876+ */
14877+static int refresh_file(struct file *file, int (*reopen)(struct file *file))
1facf9fc 14878+{
e2f27e51 14879+ int err, need_reopen, nbr;
5afbbe0d 14880+ aufs_bindex_t bbot, bindex;
4a4d8108 14881+ struct dentry *dentry;
e2f27e51 14882+ struct super_block *sb;
1308ab2a 14883+ struct au_finfo *finfo;
4a4d8108 14884+ struct au_hfile *hfile;
1facf9fc 14885+
2000de60 14886+ dentry = file->f_path.dentry;
e2f27e51
AM
14887+ sb = dentry->d_sb;
14888+ nbr = au_sbbot(sb) + 1;
1308ab2a 14889+ finfo = au_fi(file);
4a4d8108
AM
14890+ if (!finfo->fi_hdir) {
14891+ hfile = &finfo->fi_htop;
14892+ AuDebugOn(!hfile->hf_file);
e2f27e51 14893+ bindex = au_br_index(sb, hfile->hf_br->br_id);
4a4d8108
AM
14894+ AuDebugOn(bindex < 0);
14895+ if (bindex != finfo->fi_btop)
5afbbe0d 14896+ au_set_fbtop(file, bindex);
4a4d8108 14897+ } else {
e2f27e51 14898+ err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
4a4d8108
AM
14899+ if (unlikely(err))
14900+ goto out;
14901+ au_do_refresh_dir(file);
14902+ }
1facf9fc 14903+
4a4d8108
AM
14904+ err = 0;
14905+ need_reopen = 1;
14906+ if (!au_test_mmapped(file))
14907+ err = au_file_refresh_by_inode(file, &need_reopen);
e2f27e51
AM
14908+ if (finfo->fi_hdir)
14909+ /* harmless if err */
14910+ au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
027c5e7a 14911+ if (!err && need_reopen && !d_unlinked(dentry))
4a4d8108
AM
14912+ err = reopen(file);
14913+ if (!err) {
14914+ au_update_figen(file);
14915+ goto out; /* success */
14916+ }
14917+
14918+ /* error, close all lower files */
14919+ if (finfo->fi_hdir) {
5afbbe0d
AM
14920+ bbot = au_fbbot_dir(file);
14921+ for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
4a4d8108
AM
14922+ au_set_h_fptr(file, bindex, NULL);
14923+ }
1facf9fc 14924+
4f0767ce 14925+out:
1facf9fc 14926+ return err;
14927+}
14928+
4a4d8108
AM
14929+/* common function to regular file and dir */
14930+int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
521ced18 14931+ int wlock, unsigned int fi_lsc)
dece6358 14932+{
1308ab2a 14933+ int err;
4a4d8108 14934+ unsigned int sigen, figen;
5afbbe0d 14935+ aufs_bindex_t btop;
4a4d8108
AM
14936+ unsigned char pseudo_link;
14937+ struct dentry *dentry;
14938+ struct inode *inode;
1facf9fc 14939+
4a4d8108 14940+ err = 0;
2000de60 14941+ dentry = file->f_path.dentry;
5527c038 14942+ inode = d_inode(dentry);
4a4d8108 14943+ sigen = au_sigen(dentry->d_sb);
521ced18 14944+ fi_write_lock_nested(file, fi_lsc);
4a4d8108 14945+ figen = au_figen(file);
521ced18
JR
14946+ if (!fi_lsc)
14947+ di_write_lock_child(dentry);
14948+ else
14949+ di_write_lock_child2(dentry);
5afbbe0d
AM
14950+ btop = au_dbtop(dentry);
14951+ pseudo_link = (btop != au_ibtop(inode));
14952+ if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
4a4d8108
AM
14953+ if (!wlock) {
14954+ di_downgrade_lock(dentry, AuLock_IR);
14955+ fi_downgrade_lock(file);
14956+ }
14957+ goto out; /* success */
14958+ }
dece6358 14959+
4a4d8108 14960+ AuDbg("sigen %d, figen %d\n", sigen, figen);
027c5e7a 14961+ if (au_digen_test(dentry, sigen)) {
4a4d8108 14962+ err = au_reval_dpath(dentry, sigen);
027c5e7a 14963+ AuDebugOn(!err && au_digen_test(dentry, sigen));
4a4d8108 14964+ }
dece6358 14965+
027c5e7a
AM
14966+ if (!err)
14967+ err = refresh_file(file, reopen);
4a4d8108
AM
14968+ if (!err) {
14969+ if (!wlock) {
14970+ di_downgrade_lock(dentry, AuLock_IR);
14971+ fi_downgrade_lock(file);
14972+ }
14973+ } else {
14974+ di_write_unlock(dentry);
14975+ fi_write_unlock(file);
14976+ }
1facf9fc 14977+
4f0767ce 14978+out:
1308ab2a 14979+ return err;
14980+}
1facf9fc 14981+
4a4d8108
AM
14982+/* ---------------------------------------------------------------------- */
14983+
14984+/* cf. aufs_nopage() */
14985+/* for madvise(2) */
14986+static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
1308ab2a 14987+{
4a4d8108
AM
14988+ unlock_page(page);
14989+ return 0;
14990+}
1facf9fc 14991+
4a4d8108 14992+/* it will never be called, but necessary to support O_DIRECT */
5afbbe0d 14993+static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
4a4d8108 14994+{ BUG(); return 0; }
1facf9fc 14995+
4a4d8108
AM
14996+/* they will never be called. */
14997+#ifdef CONFIG_AUFS_DEBUG
14998+static int aufs_write_begin(struct file *file, struct address_space *mapping,
14999+ loff_t pos, unsigned len, unsigned flags,
15000+ struct page **pagep, void **fsdata)
15001+{ AuUnsupport(); return 0; }
15002+static int aufs_write_end(struct file *file, struct address_space *mapping,
15003+ loff_t pos, unsigned len, unsigned copied,
15004+ struct page *page, void *fsdata)
15005+{ AuUnsupport(); return 0; }
15006+static int aufs_writepage(struct page *page, struct writeback_control *wbc)
15007+{ AuUnsupport(); return 0; }
1308ab2a 15008+
4a4d8108
AM
15009+static int aufs_set_page_dirty(struct page *page)
15010+{ AuUnsupport(); return 0; }
392086de
AM
15011+static void aufs_invalidatepage(struct page *page, unsigned int offset,
15012+ unsigned int length)
4a4d8108
AM
15013+{ AuUnsupport(); }
15014+static int aufs_releasepage(struct page *page, gfp_t gfp)
15015+{ AuUnsupport(); return 0; }
79b8bda9 15016+#if 0 /* called by memory compaction regardless file */
4a4d8108 15017+static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
7eafdf33 15018+ struct page *page, enum migrate_mode mode)
4a4d8108 15019+{ AuUnsupport(); return 0; }
79b8bda9 15020+#endif
e2f27e51
AM
15021+static bool aufs_isolate_page(struct page *page, isolate_mode_t mode)
15022+{ AuUnsupport(); return true; }
15023+static void aufs_putback_page(struct page *page)
15024+{ AuUnsupport(); }
4a4d8108
AM
15025+static int aufs_launder_page(struct page *page)
15026+{ AuUnsupport(); return 0; }
15027+static int aufs_is_partially_uptodate(struct page *page,
38d290e6
JR
15028+ unsigned long from,
15029+ unsigned long count)
4a4d8108 15030+{ AuUnsupport(); return 0; }
392086de
AM
15031+static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
15032+ bool *writeback)
15033+{ AuUnsupport(); }
4a4d8108
AM
15034+static int aufs_error_remove_page(struct address_space *mapping,
15035+ struct page *page)
15036+{ AuUnsupport(); return 0; }
b4510431
AM
15037+static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
15038+ sector_t *span)
15039+{ AuUnsupport(); return 0; }
15040+static void aufs_swap_deactivate(struct file *file)
15041+{ AuUnsupport(); }
4a4d8108
AM
15042+#endif /* CONFIG_AUFS_DEBUG */
15043+
15044+const struct address_space_operations aufs_aop = {
15045+ .readpage = aufs_readpage,
15046+ .direct_IO = aufs_direct_IO,
4a4d8108
AM
15047+#ifdef CONFIG_AUFS_DEBUG
15048+ .writepage = aufs_writepage,
4a4d8108
AM
15049+ /* no writepages, because of writepage */
15050+ .set_page_dirty = aufs_set_page_dirty,
15051+ /* no readpages, because of readpage */
15052+ .write_begin = aufs_write_begin,
15053+ .write_end = aufs_write_end,
15054+ /* no bmap, no block device */
15055+ .invalidatepage = aufs_invalidatepage,
15056+ .releasepage = aufs_releasepage,
79b8bda9
AM
15057+ /* is fallback_migrate_page ok? */
15058+ /* .migratepage = aufs_migratepage, */
e2f27e51
AM
15059+ .isolate_page = aufs_isolate_page,
15060+ .putback_page = aufs_putback_page,
4a4d8108
AM
15061+ .launder_page = aufs_launder_page,
15062+ .is_partially_uptodate = aufs_is_partially_uptodate,
392086de 15063+ .is_dirty_writeback = aufs_is_dirty_writeback,
b4510431
AM
15064+ .error_remove_page = aufs_error_remove_page,
15065+ .swap_activate = aufs_swap_activate,
15066+ .swap_deactivate = aufs_swap_deactivate
4a4d8108 15067+#endif /* CONFIG_AUFS_DEBUG */
dece6358 15068+};
7f207e10
AM
15069diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
15070--- /usr/share/empty/fs/aufs/file.h 1970-01-01 01:00:00.000000000 +0100
0b487229 15071+++ linux/fs/aufs/file.h 2021-02-22 23:30:37.702680911 +0100
e49925d1 15072@@ -0,0 +1,342 @@
062440b3 15073+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 15074+/*
0b487229 15075+ * Copyright (C) 2005-2020 Junjiro R. Okajima
4a4d8108
AM
15076+ *
15077+ * This program, aufs is free software; you can redistribute it and/or modify
15078+ * it under the terms of the GNU General Public License as published by
15079+ * the Free Software Foundation; either version 2 of the License, or
15080+ * (at your option) any later version.
15081+ *
15082+ * This program is distributed in the hope that it will be useful,
15083+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15084+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15085+ * GNU General Public License for more details.
15086+ *
15087+ * You should have received a copy of the GNU General Public License
523b37e3 15088+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 15089+ */
1facf9fc 15090+
4a4d8108
AM
15091+/*
15092+ * file operations
15093+ */
1facf9fc 15094+
4a4d8108
AM
15095+#ifndef __AUFS_FILE_H__
15096+#define __AUFS_FILE_H__
1facf9fc 15097+
4a4d8108 15098+#ifdef __KERNEL__
1facf9fc 15099+
2cbb1c4b 15100+#include <linux/file.h>
4a4d8108 15101+#include <linux/fs.h>
3c1bdaff 15102+#include <linux/mm_types.h>
4a4d8108 15103+#include <linux/poll.h>
4a4d8108 15104+#include "rwsem.h"
1facf9fc 15105+
4a4d8108
AM
15106+struct au_branch;
15107+struct au_hfile {
15108+ struct file *hf_file;
15109+ struct au_branch *hf_br;
15110+};
1facf9fc 15111+
4a4d8108
AM
15112+struct au_vdir;
15113+struct au_fidir {
15114+ aufs_bindex_t fd_bbot;
15115+ aufs_bindex_t fd_nent;
15116+ struct au_vdir *fd_vdir_cache;
15117+ struct au_hfile fd_hfile[];
15118+};
1facf9fc 15119+
4a4d8108 15120+static inline int au_fidir_sz(int nent)
dece6358 15121+{
4f0767ce
JR
15122+ AuDebugOn(nent < 0);
15123+ return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
4a4d8108 15124+}
1facf9fc 15125+
4a4d8108
AM
15126+struct au_finfo {
15127+ atomic_t fi_generation;
dece6358 15128+
4a4d8108
AM
15129+ struct au_rwsem fi_rwsem;
15130+ aufs_bindex_t fi_btop;
15131+
15132+ /* do not union them */
15133+ struct { /* for non-dir */
15134+ struct au_hfile fi_htop;
2cbb1c4b 15135+ atomic_t fi_mmapped;
4a4d8108
AM
15136+ };
15137+ struct au_fidir *fi_hdir; /* for dir only */
523b37e3 15138+
8b6a4947 15139+ struct hlist_bl_node fi_hlist;
1c60b727 15140+ struct file *fi_file; /* very ugly */
e49925d1 15141+ struct rcu_head rcu;
4a4d8108 15142+} ____cacheline_aligned_in_smp;
1facf9fc 15143+
4a4d8108 15144+/* ---------------------------------------------------------------------- */
1facf9fc 15145+
4a4d8108
AM
15146+/* file.c */
15147+extern const struct address_space_operations aufs_aop;
15148+unsigned int au_file_roflags(unsigned int flags);
15149+struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
392086de 15150+ struct file *file, int force_wr);
b912730e 15151+struct au_do_open_args {
8b6a4947 15152+ int aopen;
b912730e
AM
15153+ int (*open)(struct file *file, int flags,
15154+ struct file *h_file);
15155+ struct au_fidir *fidir;
15156+ struct file *h_file;
15157+};
15158+int au_do_open(struct file *file, struct au_do_open_args *args);
4a4d8108
AM
15159+int au_reopen_nondir(struct file *file);
15160+struct au_pin;
15161+int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
15162+int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
521ced18 15163+ int wlock, unsigned int fi_lsc);
4a4d8108
AM
15164+int au_do_flush(struct file *file, fl_owner_t id,
15165+ int (*flush)(struct file *file, fl_owner_t id));
1facf9fc 15166+
4a4d8108
AM
15167+/* poll.c */
15168+#ifdef CONFIG_AUFS_POLL
cd7a4cd9 15169+__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt);
4a4d8108 15170+#endif
1facf9fc 15171+
4a4d8108
AM
15172+#ifdef CONFIG_AUFS_BR_HFSPLUS
15173+/* hfsplus.c */
392086de
AM
15174+struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
15175+ int force_wr);
4a4d8108
AM
15176+void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
15177+ struct file *h_file);
15178+#else
c1595e42
JR
15179+AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
15180+ aufs_bindex_t bindex, int force_wr)
4a4d8108
AM
15181+AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
15182+ struct file *h_file);
15183+#endif
1facf9fc 15184+
4a4d8108
AM
15185+/* f_op.c */
15186+extern const struct file_operations aufs_file_fop;
b912730e 15187+int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
4a4d8108 15188+int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
521ced18 15189+struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
4a4d8108 15190+
4a4d8108 15191+/* finfo.c */
f0c0a007 15192+void au_hfput(struct au_hfile *hf, int execed);
4a4d8108
AM
15193+void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
15194+ struct file *h_file);
1facf9fc 15195+
4a4d8108 15196+void au_update_figen(struct file *file);
4a4d8108 15197+struct au_fidir *au_fidir_alloc(struct super_block *sb);
e2f27e51 15198+int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
1facf9fc 15199+
4a4d8108 15200+void au_fi_init_once(void *_fi);
1c60b727 15201+void au_finfo_fin(struct file *file);
4a4d8108 15202+int au_finfo_init(struct file *file, struct au_fidir *fidir);
1facf9fc 15203+
4a4d8108
AM
15204+/* ioctl.c */
15205+long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
b752ccd1
AM
15206+#ifdef CONFIG_COMPAT
15207+long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
15208+ unsigned long arg);
c2b27bf2
AM
15209+long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
15210+ unsigned long arg);
b752ccd1 15211+#endif
1facf9fc 15212+
4a4d8108 15213+/* ---------------------------------------------------------------------- */
1facf9fc 15214+
4a4d8108
AM
15215+static inline struct au_finfo *au_fi(struct file *file)
15216+{
38d290e6 15217+ return file->private_data;
4a4d8108 15218+}
1facf9fc 15219+
4a4d8108 15220+/* ---------------------------------------------------------------------- */
1facf9fc 15221+
8b6a4947
AM
15222+#define fi_read_lock(f) au_rw_read_lock(&au_fi(f)->fi_rwsem)
15223+#define fi_write_lock(f) au_rw_write_lock(&au_fi(f)->fi_rwsem)
15224+#define fi_read_trylock(f) au_rw_read_trylock(&au_fi(f)->fi_rwsem)
15225+#define fi_write_trylock(f) au_rw_write_trylock(&au_fi(f)->fi_rwsem)
4a4d8108 15226+/*
8b6a4947
AM
15227+#define fi_read_trylock_nested(f) \
15228+ au_rw_read_trylock_nested(&au_fi(f)->fi_rwsem)
15229+#define fi_write_trylock_nested(f) \
15230+ au_rw_write_trylock_nested(&au_fi(f)->fi_rwsem)
15231+*/
15232+
15233+#define fi_read_unlock(f) au_rw_read_unlock(&au_fi(f)->fi_rwsem)
15234+#define fi_write_unlock(f) au_rw_write_unlock(&au_fi(f)->fi_rwsem)
15235+#define fi_downgrade_lock(f) au_rw_dgrade_lock(&au_fi(f)->fi_rwsem)
1308ab2a 15236+
521ced18
JR
15237+/* lock subclass for finfo */
15238+enum {
15239+ AuLsc_FI_1,
15240+ AuLsc_FI_2
15241+};
15242+
15243+static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
15244+{
15245+ au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15246+}
15247+
15248+static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
15249+{
15250+ au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15251+}
15252+
15253+/*
15254+ * fi_read_lock_1, fi_write_lock_1,
15255+ * fi_read_lock_2, fi_write_lock_2
15256+ */
15257+#define AuReadLockFunc(name) \
15258+static inline void fi_read_lock_##name(struct file *f) \
15259+{ fi_read_lock_nested(f, AuLsc_FI_##name); }
15260+
15261+#define AuWriteLockFunc(name) \
15262+static inline void fi_write_lock_##name(struct file *f) \
15263+{ fi_write_lock_nested(f, AuLsc_FI_##name); }
15264+
15265+#define AuRWLockFuncs(name) \
15266+ AuReadLockFunc(name) \
15267+ AuWriteLockFunc(name)
15268+
15269+AuRWLockFuncs(1);
15270+AuRWLockFuncs(2);
15271+
15272+#undef AuReadLockFunc
15273+#undef AuWriteLockFunc
15274+#undef AuRWLockFuncs
15275+
4a4d8108
AM
15276+#define FiMustNoWaiters(f) AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
15277+#define FiMustAnyLock(f) AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
15278+#define FiMustWriteLock(f) AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
1facf9fc 15279+
1308ab2a 15280+/* ---------------------------------------------------------------------- */
15281+
4a4d8108 15282+/* todo: hard/soft set? */
5afbbe0d 15283+static inline aufs_bindex_t au_fbtop(struct file *file)
dece6358 15284+{
4a4d8108
AM
15285+ FiMustAnyLock(file);
15286+ return au_fi(file)->fi_btop;
15287+}
dece6358 15288+
5afbbe0d 15289+static inline aufs_bindex_t au_fbbot_dir(struct file *file)
4a4d8108
AM
15290+{
15291+ FiMustAnyLock(file);
15292+ AuDebugOn(!au_fi(file)->fi_hdir);
15293+ return au_fi(file)->fi_hdir->fd_bbot;
15294+}
1facf9fc 15295+
4a4d8108
AM
15296+static inline struct au_vdir *au_fvdir_cache(struct file *file)
15297+{
15298+ FiMustAnyLock(file);
15299+ AuDebugOn(!au_fi(file)->fi_hdir);
15300+ return au_fi(file)->fi_hdir->fd_vdir_cache;
15301+}
1facf9fc 15302+
5afbbe0d 15303+static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
4a4d8108
AM
15304+{
15305+ FiMustWriteLock(file);
15306+ au_fi(file)->fi_btop = bindex;
15307+}
1facf9fc 15308+
5afbbe0d 15309+static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
4a4d8108
AM
15310+{
15311+ FiMustWriteLock(file);
15312+ AuDebugOn(!au_fi(file)->fi_hdir);
15313+ au_fi(file)->fi_hdir->fd_bbot = bindex;
15314+}
1308ab2a 15315+
4a4d8108
AM
15316+static inline void au_set_fvdir_cache(struct file *file,
15317+ struct au_vdir *vdir_cache)
15318+{
15319+ FiMustWriteLock(file);
15320+ AuDebugOn(!au_fi(file)->fi_hdir);
15321+ au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
15322+}
dece6358 15323+
4a4d8108
AM
15324+static inline struct file *au_hf_top(struct file *file)
15325+{
15326+ FiMustAnyLock(file);
15327+ AuDebugOn(au_fi(file)->fi_hdir);
15328+ return au_fi(file)->fi_htop.hf_file;
15329+}
1facf9fc 15330+
4a4d8108
AM
15331+static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
15332+{
15333+ FiMustAnyLock(file);
15334+ AuDebugOn(!au_fi(file)->fi_hdir);
15335+ return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
dece6358
AM
15336+}
15337+
4a4d8108
AM
15338+/* todo: memory barrier? */
15339+static inline unsigned int au_figen(struct file *f)
dece6358 15340+{
4a4d8108
AM
15341+ return atomic_read(&au_fi(f)->fi_generation);
15342+}
dece6358 15343+
2cbb1c4b
JR
15344+static inline void au_set_mmapped(struct file *f)
15345+{
15346+ if (atomic_inc_return(&au_fi(f)->fi_mmapped))
15347+ return;
0c3ec466 15348+ pr_warn("fi_mmapped wrapped around\n");
2cbb1c4b
JR
15349+ while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
15350+ ;
15351+}
15352+
15353+static inline void au_unset_mmapped(struct file *f)
15354+{
15355+ atomic_dec(&au_fi(f)->fi_mmapped);
15356+}
15357+
4a4d8108
AM
15358+static inline int au_test_mmapped(struct file *f)
15359+{
2cbb1c4b
JR
15360+ return atomic_read(&au_fi(f)->fi_mmapped);
15361+}
15362+
15363+/* customize vma->vm_file */
15364+
15365+static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
15366+ struct file *file)
15367+{
53392da6
AM
15368+ struct file *f;
15369+
15370+ f = vma->vm_file;
2cbb1c4b
JR
15371+ get_file(file);
15372+ vma->vm_file = file;
53392da6 15373+ fput(f);
2cbb1c4b
JR
15374+}
15375+
15376+#ifdef CONFIG_MMU
15377+#define AuDbgVmRegion(file, vma) do {} while (0)
15378+
15379+static inline void au_vm_file_reset(struct vm_area_struct *vma,
15380+ struct file *file)
15381+{
15382+ au_do_vm_file_reset(vma, file);
15383+}
15384+#else
15385+#define AuDbgVmRegion(file, vma) \
15386+ AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
15387+
15388+static inline void au_vm_file_reset(struct vm_area_struct *vma,
15389+ struct file *file)
15390+{
53392da6
AM
15391+ struct file *f;
15392+
2cbb1c4b 15393+ au_do_vm_file_reset(vma, file);
53392da6 15394+ f = vma->vm_region->vm_file;
2cbb1c4b
JR
15395+ get_file(file);
15396+ vma->vm_region->vm_file = file;
53392da6 15397+ fput(f);
2cbb1c4b
JR
15398+}
15399+#endif /* CONFIG_MMU */
15400+
15401+/* handle vma->vm_prfile */
fb47a38f 15402+static inline void au_vm_prfile_set(struct vm_area_struct *vma,
2cbb1c4b
JR
15403+ struct file *file)
15404+{
2cbb1c4b
JR
15405+ get_file(file);
15406+ vma->vm_prfile = file;
15407+#ifndef CONFIG_MMU
15408+ get_file(file);
15409+ vma->vm_region->vm_prfile = file;
15410+#endif
fb47a38f 15411+}
1308ab2a 15412+
4a4d8108
AM
15413+#endif /* __KERNEL__ */
15414+#endif /* __AUFS_FILE_H__ */
7f207e10
AM
15415diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
15416--- /usr/share/empty/fs/aufs/finfo.c 1970-01-01 01:00:00.000000000 +0100
0b487229 15417+++ linux/fs/aufs/finfo.c 2021-02-22 23:30:37.702680911 +0100
062440b3 15418@@ -0,0 +1,149 @@
cd7a4cd9 15419+// SPDX-License-Identifier: GPL-2.0
4a4d8108 15420+/*
0b487229 15421+ * Copyright (C) 2005-2020 Junjiro R. Okajima
4a4d8108
AM
15422+ *
15423+ * This program, aufs is free software; you can redistribute it and/or modify
15424+ * it under the terms of the GNU General Public License as published by
15425+ * the Free Software Foundation; either version 2 of the License, or
15426+ * (at your option) any later version.
15427+ *
15428+ * This program is distributed in the hope that it will be useful,
15429+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15430+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15431+ * GNU General Public License for more details.
15432+ *
15433+ * You should have received a copy of the GNU General Public License
523b37e3 15434+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 15435+ */
1308ab2a 15436+
4a4d8108
AM
15437+/*
15438+ * file private data
15439+ */
1facf9fc 15440+
4a4d8108 15441+#include "aufs.h"
1facf9fc 15442+
f0c0a007 15443+void au_hfput(struct au_hfile *hf, int execed)
4a4d8108 15444+{
f0c0a007 15445+ if (execed)
4a4d8108
AM
15446+ allow_write_access(hf->hf_file);
15447+ fput(hf->hf_file);
15448+ hf->hf_file = NULL;
acd2b654 15449+ au_lcnt_dec(&hf->hf_br->br_nfiles);
4a4d8108
AM
15450+ hf->hf_br = NULL;
15451+}
1facf9fc 15452+
4a4d8108
AM
15453+void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
15454+{
15455+ struct au_finfo *finfo = au_fi(file);
15456+ struct au_hfile *hf;
15457+ struct au_fidir *fidir;
15458+
15459+ fidir = finfo->fi_hdir;
15460+ if (!fidir) {
15461+ AuDebugOn(finfo->fi_btop != bindex);
15462+ hf = &finfo->fi_htop;
15463+ } else
15464+ hf = fidir->fd_hfile + bindex;
15465+
15466+ if (hf && hf->hf_file)
f0c0a007 15467+ au_hfput(hf, vfsub_file_execed(file));
4a4d8108
AM
15468+ if (val) {
15469+ FiMustWriteLock(file);
b912730e 15470+ AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
4a4d8108 15471+ hf->hf_file = val;
2000de60 15472+ hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
1308ab2a 15473+ }
4a4d8108 15474+}
1facf9fc 15475+
4a4d8108
AM
15476+void au_update_figen(struct file *file)
15477+{
2000de60 15478+ atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
4a4d8108 15479+ /* smp_mb(); */ /* atomic_set */
1facf9fc 15480+}
15481+
4a4d8108
AM
15482+/* ---------------------------------------------------------------------- */
15483+
4a4d8108
AM
15484+struct au_fidir *au_fidir_alloc(struct super_block *sb)
15485+{
15486+ struct au_fidir *fidir;
15487+ int nbr;
15488+
5afbbe0d 15489+ nbr = au_sbbot(sb) + 1;
4a4d8108
AM
15490+ if (nbr < 2)
15491+ nbr = 2; /* initial allocate for 2 branches */
15492+ fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
15493+ if (fidir) {
15494+ fidir->fd_bbot = -1;
15495+ fidir->fd_nent = nbr;
4a4d8108
AM
15496+ }
15497+
15498+ return fidir;
15499+}
15500+
e2f27e51 15501+int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
4a4d8108
AM
15502+{
15503+ int err;
15504+ struct au_fidir *fidir, *p;
15505+
15506+ AuRwMustWriteLock(&finfo->fi_rwsem);
15507+ fidir = finfo->fi_hdir;
15508+ AuDebugOn(!fidir);
15509+
15510+ err = -ENOMEM;
15511+ p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
e2f27e51 15512+ GFP_NOFS, may_shrink);
4a4d8108
AM
15513+ if (p) {
15514+ p->fd_nent = nbr;
15515+ finfo->fi_hdir = p;
15516+ err = 0;
15517+ }
1facf9fc 15518+
dece6358 15519+ return err;
1facf9fc 15520+}
1308ab2a 15521+
15522+/* ---------------------------------------------------------------------- */
15523+
1c60b727 15524+void au_finfo_fin(struct file *file)
1308ab2a 15525+{
4a4d8108
AM
15526+ struct au_finfo *finfo;
15527+
acd2b654 15528+ au_lcnt_dec(&au_sbi(file->f_path.dentry->d_sb)->si_nfiles);
7f207e10 15529+
4a4d8108
AM
15530+ finfo = au_fi(file);
15531+ AuDebugOn(finfo->fi_hdir);
15532+ AuRwDestroy(&finfo->fi_rwsem);
1c60b727 15533+ au_cache_free_finfo(finfo);
1308ab2a 15534+}
1308ab2a 15535+
e49829fe 15536+void au_fi_init_once(void *_finfo)
4a4d8108 15537+{
e49829fe 15538+ struct au_finfo *finfo = _finfo;
1308ab2a 15539+
e49829fe 15540+ au_rw_init(&finfo->fi_rwsem);
4a4d8108 15541+}
1308ab2a 15542+
4a4d8108
AM
15543+int au_finfo_init(struct file *file, struct au_fidir *fidir)
15544+{
1716fcea 15545+ int err;
4a4d8108
AM
15546+ struct au_finfo *finfo;
15547+ struct dentry *dentry;
15548+
15549+ err = -ENOMEM;
2000de60 15550+ dentry = file->f_path.dentry;
4a4d8108
AM
15551+ finfo = au_cache_alloc_finfo();
15552+ if (unlikely(!finfo))
15553+ goto out;
15554+
15555+ err = 0;
acd2b654 15556+ au_lcnt_inc(&au_sbi(dentry->d_sb)->si_nfiles);
4a4d8108
AM
15557+ au_rw_write_lock(&finfo->fi_rwsem);
15558+ finfo->fi_btop = -1;
15559+ finfo->fi_hdir = fidir;
15560+ atomic_set(&finfo->fi_generation, au_digen(dentry));
15561+ /* smp_mb(); */ /* atomic_set */
15562+
15563+ file->private_data = finfo;
15564+
15565+out:
15566+ return err;
15567+}
7f207e10
AM
15568diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
15569--- /usr/share/empty/fs/aufs/f_op.c 1970-01-01 01:00:00.000000000 +0100
0b487229 15570+++ linux/fs/aufs/f_op.c 2021-02-22 23:30:37.699347580 +0100
acd2b654 15571@@ -0,0 +1,819 @@
cd7a4cd9 15572+// SPDX-License-Identifier: GPL-2.0
dece6358 15573+/*
0b487229 15574+ * Copyright (C) 2005-2020 Junjiro R. Okajima
dece6358
AM
15575+ *
15576+ * This program, aufs is free software; you can redistribute it and/or modify
15577+ * it under the terms of the GNU General Public License as published by
15578+ * the Free Software Foundation; either version 2 of the License, or
15579+ * (at your option) any later version.
15580+ *
15581+ * This program is distributed in the hope that it will be useful,
15582+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15583+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15584+ * GNU General Public License for more details.
15585+ *
15586+ * You should have received a copy of the GNU General Public License
523b37e3 15587+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358 15588+ */
1facf9fc 15589+
15590+/*
4a4d8108 15591+ * file and vm operations
1facf9fc 15592+ */
dece6358 15593+
86dc4139 15594+#include <linux/aio.h>
4a4d8108
AM
15595+#include <linux/fs_stack.h>
15596+#include <linux/mman.h>
4a4d8108 15597+#include <linux/security.h>
dece6358
AM
15598+#include "aufs.h"
15599+
b912730e 15600+int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
1facf9fc 15601+{
4a4d8108
AM
15602+ int err;
15603+ aufs_bindex_t bindex;
8cdd5066 15604+ struct dentry *dentry, *h_dentry;
4a4d8108 15605+ struct au_finfo *finfo;
38d290e6 15606+ struct inode *h_inode;
4a4d8108
AM
15607+
15608+ FiMustWriteLock(file);
15609+
523b37e3 15610+ err = 0;
2000de60 15611+ dentry = file->f_path.dentry;
b912730e 15612+ AuDebugOn(IS_ERR_OR_NULL(dentry));
4a4d8108
AM
15613+ finfo = au_fi(file);
15614+ memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
2cbb1c4b 15615+ atomic_set(&finfo->fi_mmapped, 0);
5afbbe0d 15616+ bindex = au_dbtop(dentry);
8cdd5066
JR
15617+ if (!h_file) {
15618+ h_dentry = au_h_dptr(dentry, bindex);
15619+ err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15620+ if (unlikely(err))
15621+ goto out;
b912730e 15622+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
acd2b654
AM
15623+ if (IS_ERR(h_file)) {
15624+ err = PTR_ERR(h_file);
15625+ goto out;
15626+ }
8cdd5066
JR
15627+ } else {
15628+ h_dentry = h_file->f_path.dentry;
15629+ err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15630+ if (unlikely(err))
15631+ goto out;
acd2b654 15632+ /* br ref is already inc-ed */
8cdd5066 15633+ }
acd2b654
AM
15634+
15635+ if ((flags & __O_TMPFILE)
15636+ && !(flags & O_EXCL)) {
15637+ h_inode = file_inode(h_file);
15638+ spin_lock(&h_inode->i_lock);
15639+ h_inode->i_state |= I_LINKABLE;
15640+ spin_unlock(&h_inode->i_lock);
4a4d8108 15641+ }
acd2b654
AM
15642+ au_set_fbtop(file, bindex);
15643+ au_set_h_fptr(file, bindex, h_file);
15644+ au_update_figen(file);
15645+ /* todo: necessary? */
15646+ /* file->f_ra = h_file->f_ra; */
027c5e7a 15647+
8cdd5066 15648+out:
4a4d8108 15649+ return err;
1facf9fc 15650+}
15651+
4a4d8108
AM
15652+static int aufs_open_nondir(struct inode *inode __maybe_unused,
15653+ struct file *file)
1facf9fc 15654+{
4a4d8108 15655+ int err;
1308ab2a 15656+ struct super_block *sb;
b912730e
AM
15657+ struct au_do_open_args args = {
15658+ .open = au_do_open_nondir
15659+ };
1facf9fc 15660+
523b37e3
AM
15661+ AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
15662+ file, vfsub_file_flags(file), file->f_mode);
1facf9fc 15663+
2000de60 15664+ sb = file->f_path.dentry->d_sb;
4a4d8108 15665+ si_read_lock(sb, AuLock_FLUSH);
b912730e 15666+ err = au_do_open(file, &args);
4a4d8108
AM
15667+ si_read_unlock(sb);
15668+ return err;
15669+}
1facf9fc 15670+
4a4d8108
AM
15671+int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
15672+{
15673+ struct au_finfo *finfo;
15674+ aufs_bindex_t bindex;
1facf9fc 15675+
4a4d8108 15676+ finfo = au_fi(file);
8b6a4947
AM
15677+ au_hbl_del(&finfo->fi_hlist,
15678+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
4a4d8108 15679+ bindex = finfo->fi_btop;
b4510431 15680+ if (bindex >= 0)
4a4d8108 15681+ au_set_h_fptr(file, bindex, NULL);
7f207e10 15682+
1c60b727 15683+ au_finfo_fin(file);
4a4d8108 15684+ return 0;
1facf9fc 15685+}
15686+
4a4d8108
AM
15687+/* ---------------------------------------------------------------------- */
15688+
15689+static int au_do_flush_nondir(struct file *file, fl_owner_t id)
dece6358 15690+{
1308ab2a 15691+ int err;
4a4d8108
AM
15692+ struct file *h_file;
15693+
15694+ err = 0;
15695+ h_file = au_hf_top(file);
15696+ if (h_file)
15697+ err = vfsub_flush(h_file, id);
15698+ return err;
15699+}
15700+
15701+static int aufs_flush_nondir(struct file *file, fl_owner_t id)
15702+{
15703+ return au_do_flush(file, id, au_do_flush_nondir);
15704+}
15705+
15706+/* ---------------------------------------------------------------------- */
9dbd164d
AM
15707+/*
15708+ * read and write functions acquire [fdi]_rwsem once, but release before
15709+ * mmap_sem. This is because to stop a race condition between mmap(2).
acd2b654 15710+ * Releasing these aufs-rwsem should be safe, no branch-management (by keeping
9dbd164d
AM
15711+ * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
15712+ * read functions after [fdi]_rwsem are released, but it should be harmless.
15713+ */
4a4d8108 15714+
b912730e 15715+/* Callers should call au_read_post() or fput() in the end */
521ced18 15716+struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
4a4d8108 15717+{
4a4d8108 15718+ struct file *h_file;
b912730e 15719+ int err;
1facf9fc 15720+
521ced18 15721+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
b912730e
AM
15722+ if (!err) {
15723+ di_read_unlock(file->f_path.dentry, AuLock_IR);
15724+ h_file = au_hf_top(file);
15725+ get_file(h_file);
15726+ if (!keep_fi)
15727+ fi_read_unlock(file);
15728+ } else
15729+ h_file = ERR_PTR(err);
15730+
15731+ return h_file;
15732+}
15733+
15734+static void au_read_post(struct inode *inode, struct file *h_file)
15735+{
15736+ /* update without lock, I don't think it a problem */
15737+ fsstack_copy_attr_atime(inode, file_inode(h_file));
15738+ fput(h_file);
15739+}
15740+
15741+struct au_write_pre {
521ced18
JR
15742+ /* input */
15743+ unsigned int lsc;
15744+
15745+ /* output */
b912730e 15746+ blkcnt_t blks;
5afbbe0d 15747+ aufs_bindex_t btop;
b912730e
AM
15748+};
15749+
15750+/*
15751+ * return with iinfo is write-locked
15752+ * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
15753+ * end
15754+ */
15755+static struct file *au_write_pre(struct file *file, int do_ready,
15756+ struct au_write_pre *wpre)
15757+{
15758+ struct file *h_file;
15759+ struct dentry *dentry;
15760+ int err;
521ced18 15761+ unsigned int lsc;
b912730e
AM
15762+ struct au_pin pin;
15763+
521ced18
JR
15764+ lsc = 0;
15765+ if (wpre)
15766+ lsc = wpre->lsc;
15767+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
b912730e 15768+ h_file = ERR_PTR(err);
dece6358
AM
15769+ if (unlikely(err))
15770+ goto out;
1facf9fc 15771+
b912730e
AM
15772+ dentry = file->f_path.dentry;
15773+ if (do_ready) {
15774+ err = au_ready_to_write(file, -1, &pin);
15775+ if (unlikely(err)) {
15776+ h_file = ERR_PTR(err);
15777+ di_write_unlock(dentry);
15778+ goto out_fi;
15779+ }
15780+ }
15781+
15782+ di_downgrade_lock(dentry, /*flags*/0);
15783+ if (wpre)
5afbbe0d 15784+ wpre->btop = au_fbtop(file);
4a4d8108 15785+ h_file = au_hf_top(file);
9dbd164d 15786+ get_file(h_file);
b912730e
AM
15787+ if (wpre)
15788+ wpre->blks = file_inode(h_file)->i_blocks;
15789+ if (do_ready)
15790+ au_unpin(&pin);
15791+ di_read_unlock(dentry, /*flags*/0);
15792+
15793+out_fi:
15794+ fi_write_unlock(file);
15795+out:
15796+ return h_file;
15797+}
15798+
15799+static void au_write_post(struct inode *inode, struct file *h_file,
15800+ struct au_write_pre *wpre, ssize_t written)
15801+{
15802+ struct inode *h_inode;
15803+
15804+ au_cpup_attr_timesizes(inode);
5afbbe0d 15805+ AuDebugOn(au_ibtop(inode) != wpre->btop);
b912730e
AM
15806+ h_inode = file_inode(h_file);
15807+ inode->i_mode = h_inode->i_mode;
15808+ ii_write_unlock(inode);
b912730e
AM
15809+ /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
15810+ if (written > 0)
5afbbe0d 15811+ au_fhsm_wrote(inode->i_sb, wpre->btop,
b912730e 15812+ /*force*/h_inode->i_blocks > wpre->blks);
1c60b727 15813+ fput(h_file);
b912730e
AM
15814+}
15815+
15816+static ssize_t aufs_read(struct file *file, char __user *buf, size_t count,
15817+ loff_t *ppos)
15818+{
15819+ ssize_t err;
15820+ struct inode *inode;
15821+ struct file *h_file;
15822+ struct super_block *sb;
15823+
15824+ inode = file_inode(file);
15825+ sb = inode->i_sb;
15826+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15827+
521ced18 15828+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
b912730e
AM
15829+ err = PTR_ERR(h_file);
15830+ if (IS_ERR(h_file))
15831+ goto out;
9dbd164d
AM
15832+
15833+ /* filedata may be obsoleted by concurrent copyup, but no problem */
4a4d8108
AM
15834+ err = vfsub_read_u(h_file, buf, count, ppos);
15835+ /* todo: necessary? */
15836+ /* file->f_ra = h_file->f_ra; */
b912730e 15837+ au_read_post(inode, h_file);
1308ab2a 15838+
4f0767ce 15839+out:
dece6358
AM
15840+ si_read_unlock(sb);
15841+ return err;
15842+}
1facf9fc 15843+
e49829fe
JR
15844+/*
15845+ * todo: very ugly
15846+ * it locks both of i_mutex and si_rwsem for read in safe.
15847+ * if the plink maintenance mode continues forever (that is the problem),
15848+ * may loop forever.
15849+ */
15850+static void au_mtx_and_read_lock(struct inode *inode)
15851+{
15852+ int err;
15853+ struct super_block *sb = inode->i_sb;
15854+
15855+ while (1) {
febd17d6 15856+ inode_lock(inode);
e49829fe
JR
15857+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
15858+ if (!err)
15859+ break;
febd17d6 15860+ inode_unlock(inode);
e49829fe
JR
15861+ si_read_lock(sb, AuLock_NOPLMW);
15862+ si_read_unlock(sb);
15863+ }
15864+}
15865+
4a4d8108
AM
15866+static ssize_t aufs_write(struct file *file, const char __user *ubuf,
15867+ size_t count, loff_t *ppos)
dece6358 15868+{
4a4d8108 15869+ ssize_t err;
b912730e
AM
15870+ struct au_write_pre wpre;
15871+ struct inode *inode;
4a4d8108
AM
15872+ struct file *h_file;
15873+ char __user *buf = (char __user *)ubuf;
1facf9fc 15874+
b912730e 15875+ inode = file_inode(file);
e49829fe 15876+ au_mtx_and_read_lock(inode);
1facf9fc 15877+
521ced18 15878+ wpre.lsc = 0;
b912730e
AM
15879+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15880+ err = PTR_ERR(h_file);
15881+ if (IS_ERR(h_file))
9dbd164d 15882+ goto out;
9dbd164d 15883+
4a4d8108 15884+ err = vfsub_write_u(h_file, buf, count, ppos);
b912730e 15885+ au_write_post(inode, h_file, &wpre, err);
1facf9fc 15886+
4f0767ce 15887+out:
b912730e 15888+ si_read_unlock(inode->i_sb);
febd17d6 15889+ inode_unlock(inode);
dece6358
AM
15890+ return err;
15891+}
1facf9fc 15892+
076b876e
AM
15893+static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
15894+ struct iov_iter *iov_iter)
dece6358 15895+{
4a4d8108
AM
15896+ ssize_t err;
15897+ struct file *file;
076b876e 15898+ ssize_t (*iter)(struct kiocb *, struct iov_iter *);
1facf9fc 15899+
4a4d8108
AM
15900+ err = security_file_permission(h_file, rw);
15901+ if (unlikely(err))
15902+ goto out;
1facf9fc 15903+
0b487229 15904+ err = -ENOSYS; /* the branch doesn't have its ->(read|write)_iter() */
076b876e 15905+ iter = NULL;
5527c038 15906+ if (rw == MAY_READ)
076b876e 15907+ iter = h_file->f_op->read_iter;
5527c038 15908+ else if (rw == MAY_WRITE)
076b876e 15909+ iter = h_file->f_op->write_iter;
076b876e
AM
15910+
15911+ file = kio->ki_filp;
15912+ kio->ki_filp = h_file;
15913+ if (iter) {
2cbb1c4b 15914+ lockdep_off();
076b876e
AM
15915+ err = iter(kio, iov_iter);
15916+ lockdep_on();
4a4d8108
AM
15917+ } else
15918+ /* currently there is no such fs */
15919+ WARN_ON_ONCE(1);
076b876e 15920+ kio->ki_filp = file;
1facf9fc 15921+
4f0767ce 15922+out:
dece6358
AM
15923+ return err;
15924+}
1facf9fc 15925+
076b876e 15926+static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
1facf9fc 15927+{
4a4d8108
AM
15928+ ssize_t err;
15929+ struct file *file, *h_file;
b912730e 15930+ struct inode *inode;
dece6358 15931+ struct super_block *sb;
1facf9fc 15932+
4a4d8108 15933+ file = kio->ki_filp;
b912730e
AM
15934+ inode = file_inode(file);
15935+ sb = inode->i_sb;
e49829fe 15936+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
4a4d8108 15937+
521ced18 15938+ h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
b912730e
AM
15939+ err = PTR_ERR(h_file);
15940+ if (IS_ERR(h_file))
15941+ goto out;
9dbd164d 15942+
5afbbe0d
AM
15943+ if (au_test_loopback_kthread()) {
15944+ au_warn_loopback(h_file->f_path.dentry->d_sb);
15945+ if (file->f_mapping != h_file->f_mapping) {
15946+ file->f_mapping = h_file->f_mapping;
15947+ smp_mb(); /* unnecessary? */
15948+ }
15949+ }
15950+ fi_read_unlock(file);
15951+
076b876e 15952+ err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
4a4d8108
AM
15953+ /* todo: necessary? */
15954+ /* file->f_ra = h_file->f_ra; */
b912730e 15955+ au_read_post(inode, h_file);
1facf9fc 15956+
4f0767ce 15957+out:
4a4d8108 15958+ si_read_unlock(sb);
1308ab2a 15959+ return err;
15960+}
1facf9fc 15961+
076b876e 15962+static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
1308ab2a 15963+{
4a4d8108 15964+ ssize_t err;
b912730e
AM
15965+ struct au_write_pre wpre;
15966+ struct inode *inode;
4a4d8108 15967+ struct file *file, *h_file;
1308ab2a 15968+
4a4d8108 15969+ file = kio->ki_filp;
b912730e 15970+ inode = file_inode(file);
e49829fe
JR
15971+ au_mtx_and_read_lock(inode);
15972+
521ced18 15973+ wpre.lsc = 0;
b912730e
AM
15974+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15975+ err = PTR_ERR(h_file);
15976+ if (IS_ERR(h_file))
9dbd164d 15977+ goto out;
9dbd164d 15978+
076b876e 15979+ err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
b912730e 15980+ au_write_post(inode, h_file, &wpre, err);
1facf9fc 15981+
4f0767ce 15982+out:
b912730e 15983+ si_read_unlock(inode->i_sb);
febd17d6 15984+ inode_unlock(inode);
dece6358 15985+ return err;
1facf9fc 15986+}
15987+
4a4d8108
AM
15988+static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
15989+ struct pipe_inode_info *pipe, size_t len,
15990+ unsigned int flags)
1facf9fc 15991+{
4a4d8108
AM
15992+ ssize_t err;
15993+ struct file *h_file;
b912730e 15994+ struct inode *inode;
dece6358 15995+ struct super_block *sb;
1facf9fc 15996+
b912730e
AM
15997+ inode = file_inode(file);
15998+ sb = inode->i_sb;
e49829fe 15999+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e 16000+
521ced18 16001+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
b912730e
AM
16002+ err = PTR_ERR(h_file);
16003+ if (IS_ERR(h_file))
dece6358 16004+ goto out;
1facf9fc 16005+
4a4d8108 16006+ err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
acd2b654 16007+ /* todo: necessary? */
4a4d8108 16008+ /* file->f_ra = h_file->f_ra; */
b912730e 16009+ au_read_post(inode, h_file);
1facf9fc 16010+
4f0767ce 16011+out:
4a4d8108 16012+ si_read_unlock(sb);
dece6358 16013+ return err;
1facf9fc 16014+}
16015+
4a4d8108
AM
16016+static ssize_t
16017+aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
16018+ size_t len, unsigned int flags)
1facf9fc 16019+{
4a4d8108 16020+ ssize_t err;
b912730e
AM
16021+ struct au_write_pre wpre;
16022+ struct inode *inode;
076b876e 16023+ struct file *h_file;
1facf9fc 16024+
b912730e 16025+ inode = file_inode(file);
e49829fe 16026+ au_mtx_and_read_lock(inode);
9dbd164d 16027+
521ced18 16028+ wpre.lsc = 0;
b912730e
AM
16029+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16030+ err = PTR_ERR(h_file);
16031+ if (IS_ERR(h_file))
9dbd164d 16032+ goto out;
9dbd164d 16033+
4a4d8108 16034+ err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
b912730e 16035+ au_write_post(inode, h_file, &wpre, err);
1facf9fc 16036+
4f0767ce 16037+out:
b912730e 16038+ si_read_unlock(inode->i_sb);
febd17d6 16039+ inode_unlock(inode);
4a4d8108
AM
16040+ return err;
16041+}
1facf9fc 16042+
38d290e6
JR
16043+static long aufs_fallocate(struct file *file, int mode, loff_t offset,
16044+ loff_t len)
16045+{
16046+ long err;
b912730e 16047+ struct au_write_pre wpre;
38d290e6
JR
16048+ struct inode *inode;
16049+ struct file *h_file;
16050+
b912730e 16051+ inode = file_inode(file);
38d290e6
JR
16052+ au_mtx_and_read_lock(inode);
16053+
521ced18 16054+ wpre.lsc = 0;
b912730e
AM
16055+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16056+ err = PTR_ERR(h_file);
16057+ if (IS_ERR(h_file))
38d290e6 16058+ goto out;
38d290e6
JR
16059+
16060+ lockdep_off();
03673fb0 16061+ err = vfs_fallocate(h_file, mode, offset, len);
38d290e6 16062+ lockdep_on();
b912730e 16063+ au_write_post(inode, h_file, &wpre, /*written*/1);
38d290e6
JR
16064+
16065+out:
b912730e 16066+ si_read_unlock(inode->i_sb);
febd17d6 16067+ inode_unlock(inode);
38d290e6
JR
16068+ return err;
16069+}
16070+
521ced18
JR
16071+static ssize_t aufs_copy_file_range(struct file *src, loff_t src_pos,
16072+ struct file *dst, loff_t dst_pos,
16073+ size_t len, unsigned int flags)
16074+{
16075+ ssize_t err;
16076+ struct au_write_pre wpre;
16077+ enum { SRC, DST };
16078+ struct {
16079+ struct inode *inode;
16080+ struct file *h_file;
16081+ struct super_block *h_sb;
16082+ } a[2];
16083+#define a_src a[SRC]
16084+#define a_dst a[DST]
16085+
16086+ err = -EINVAL;
16087+ a_src.inode = file_inode(src);
16088+ if (unlikely(!S_ISREG(a_src.inode->i_mode)))
16089+ goto out;
16090+ a_dst.inode = file_inode(dst);
16091+ if (unlikely(!S_ISREG(a_dst.inode->i_mode)))
16092+ goto out;
16093+
16094+ au_mtx_and_read_lock(a_dst.inode);
16095+ /*
16096+ * in order to match the order in di_write_lock2_{child,parent}(),
acd2b654 16097+ * use f_path.dentry for this comparison.
521ced18
JR
16098+ */
16099+ if (src->f_path.dentry < dst->f_path.dentry) {
16100+ a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_1);
16101+ err = PTR_ERR(a_src.h_file);
16102+ if (IS_ERR(a_src.h_file))
16103+ goto out_si;
16104+
16105+ wpre.lsc = AuLsc_FI_2;
16106+ a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
16107+ err = PTR_ERR(a_dst.h_file);
16108+ if (IS_ERR(a_dst.h_file)) {
16109+ au_read_post(a_src.inode, a_src.h_file);
16110+ goto out_si;
16111+ }
16112+ } else {
16113+ wpre.lsc = AuLsc_FI_1;
16114+ a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
16115+ err = PTR_ERR(a_dst.h_file);
16116+ if (IS_ERR(a_dst.h_file))
16117+ goto out_si;
16118+
16119+ a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_2);
16120+ err = PTR_ERR(a_src.h_file);
16121+ if (IS_ERR(a_src.h_file)) {
16122+ au_write_post(a_dst.inode, a_dst.h_file, &wpre,
16123+ /*written*/0);
16124+ goto out_si;
16125+ }
16126+ }
16127+
16128+ err = -EXDEV;
16129+ a_src.h_sb = file_inode(a_src.h_file)->i_sb;
16130+ a_dst.h_sb = file_inode(a_dst.h_file)->i_sb;
16131+ if (unlikely(a_src.h_sb != a_dst.h_sb)) {
16132+ AuDbgFile(src);
16133+ AuDbgFile(dst);
16134+ goto out_file;
16135+ }
16136+
16137+ err = vfsub_copy_file_range(a_src.h_file, src_pos, a_dst.h_file,
16138+ dst_pos, len, flags);
16139+
16140+out_file:
16141+ au_write_post(a_dst.inode, a_dst.h_file, &wpre, err);
16142+ fi_read_unlock(src);
16143+ au_read_post(a_src.inode, a_src.h_file);
16144+out_si:
16145+ si_read_unlock(a_dst.inode->i_sb);
16146+ inode_unlock(a_dst.inode);
16147+out:
16148+ return err;
16149+#undef a_src
16150+#undef a_dst
16151+}
16152+
4a4d8108
AM
16153+/* ---------------------------------------------------------------------- */
16154+
9dbd164d
AM
16155+/*
16156+ * The locking order around current->mmap_sem.
16157+ * - in most and regular cases
16158+ * file I/O syscall -- aufs_read() or something
16159+ * -- si_rwsem for read -- mmap_sem
16160+ * (Note that [fdi]i_rwsem are released before mmap_sem).
16161+ * - in mmap case
16162+ * mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
acd2b654
AM
16163+ * This AB-BA order is definitely bad, but is not a problem since "si_rwsem for
16164+ * read" allows multiple processes to acquire it and [fdi]i_rwsem are not held
16165+ * in file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
9dbd164d
AM
16166+ * It means that when aufs acquires si_rwsem for write, the process should never
16167+ * acquire mmap_sem.
16168+ *
392086de 16169+ * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
9dbd164d
AM
16170+ * problem either since any directory is not able to be mmap-ed.
16171+ * The similar scenario is applied to aufs_readlink() too.
16172+ */
16173+
38d290e6 16174+#if 0 /* stop calling security_file_mmap() */
2dfbb274
AM
16175+/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
16176+#define AuConv_VM_PROT(f, b) _calc_vm_trans(f, VM_##b, PROT_##b)
16177+
16178+static unsigned long au_arch_prot_conv(unsigned long flags)
16179+{
16180+ /* currently ppc64 only */
16181+#ifdef CONFIG_PPC64
16182+ /* cf. linux/arch/powerpc/include/asm/mman.h */
16183+ AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
16184+ return AuConv_VM_PROT(flags, SAO);
16185+#else
16186+ AuDebugOn(arch_calc_vm_prot_bits(-1));
16187+ return 0;
16188+#endif
16189+}
16190+
16191+static unsigned long au_prot_conv(unsigned long flags)
16192+{
16193+ return AuConv_VM_PROT(flags, READ)
16194+ | AuConv_VM_PROT(flags, WRITE)
16195+ | AuConv_VM_PROT(flags, EXEC)
16196+ | au_arch_prot_conv(flags);
16197+}
16198+
16199+/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
16200+#define AuConv_VM_MAP(f, b) _calc_vm_trans(f, VM_##b, MAP_##b)
16201+
16202+static unsigned long au_flag_conv(unsigned long flags)
16203+{
16204+ return AuConv_VM_MAP(flags, GROWSDOWN)
16205+ | AuConv_VM_MAP(flags, DENYWRITE)
2dfbb274
AM
16206+ | AuConv_VM_MAP(flags, LOCKED);
16207+}
38d290e6 16208+#endif
2dfbb274 16209+
9dbd164d 16210+static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
dece6358 16211+{
4a4d8108 16212+ int err;
4a4d8108 16213+ const unsigned char wlock
9dbd164d 16214+ = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
4a4d8108 16215+ struct super_block *sb;
9dbd164d 16216+ struct file *h_file;
b912730e 16217+ struct inode *inode;
9dbd164d
AM
16218+
16219+ AuDbgVmRegion(file, vma);
1308ab2a 16220+
b912730e
AM
16221+ inode = file_inode(file);
16222+ sb = inode->i_sb;
9dbd164d 16223+ lockdep_off();
e49829fe 16224+ si_read_lock(sb, AuLock_NOPLMW);
4a4d8108 16225+
b912730e 16226+ h_file = au_write_pre(file, wlock, /*wpre*/NULL);
9dbd164d 16227+ lockdep_on();
b912730e
AM
16228+ err = PTR_ERR(h_file);
16229+ if (IS_ERR(h_file))
16230+ goto out;
1308ab2a 16231+
b912730e
AM
16232+ err = 0;
16233+ au_set_mmapped(file);
9dbd164d 16234+ au_vm_file_reset(vma, h_file);
38d290e6
JR
16235+ /*
16236+ * we cannot call security_mmap_file() here since it may acquire
16237+ * mmap_sem or i_mutex.
16238+ *
16239+ * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
16240+ * au_flag_conv(vma->vm_flags));
16241+ */
9dbd164d 16242+ if (!err)
521ced18 16243+ err = call_mmap(h_file, vma);
b912730e
AM
16244+ if (!err) {
16245+ au_vm_prfile_set(vma, file);
16246+ fsstack_copy_attr_atime(inode, file_inode(h_file));
16247+ goto out_fput; /* success */
16248+ }
2cbb1c4b
JR
16249+ au_unset_mmapped(file);
16250+ au_vm_file_reset(vma, file);
b912730e 16251+
2cbb1c4b 16252+out_fput:
9dbd164d 16253+ lockdep_off();
b912730e
AM
16254+ ii_write_unlock(inode);
16255+ lockdep_on();
16256+ fput(h_file);
4f0767ce 16257+out:
b912730e 16258+ lockdep_off();
9dbd164d
AM
16259+ si_read_unlock(sb);
16260+ lockdep_on();
16261+ AuTraceErr(err);
4a4d8108
AM
16262+ return err;
16263+}
16264+
16265+/* ---------------------------------------------------------------------- */
16266+
1e00d052
AM
16267+static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
16268+ int datasync)
4a4d8108
AM
16269+{
16270+ int err;
b912730e 16271+ struct au_write_pre wpre;
4a4d8108
AM
16272+ struct inode *inode;
16273+ struct file *h_file;
4a4d8108
AM
16274+
16275+ err = 0; /* -EBADF; */ /* posix? */
16276+ if (unlikely(!(file->f_mode & FMODE_WRITE)))
b912730e 16277+ goto out;
4a4d8108 16278+
b912730e
AM
16279+ inode = file_inode(file);
16280+ au_mtx_and_read_lock(inode);
16281+
521ced18 16282+ wpre.lsc = 0;
b912730e
AM
16283+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16284+ err = PTR_ERR(h_file);
16285+ if (IS_ERR(h_file))
4a4d8108 16286+ goto out_unlock;
4a4d8108 16287+
53392da6 16288+ err = vfsub_fsync(h_file, &h_file->f_path, datasync);
b912730e 16289+ au_write_post(inode, h_file, &wpre, /*written*/0);
4a4d8108 16290+
4f0767ce 16291+out_unlock:
b912730e 16292+ si_read_unlock(inode->i_sb);
febd17d6 16293+ inode_unlock(inode);
b912730e 16294+out:
4a4d8108 16295+ return err;
dece6358
AM
16296+}
16297+
4a4d8108 16298+static int aufs_fasync(int fd, struct file *file, int flag)
dece6358 16299+{
4a4d8108
AM
16300+ int err;
16301+ struct file *h_file;
4a4d8108 16302+ struct super_block *sb;
1308ab2a 16303+
b912730e 16304+ sb = file->f_path.dentry->d_sb;
e49829fe 16305+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e 16306+
521ced18 16307+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
b912730e
AM
16308+ err = PTR_ERR(h_file);
16309+ if (IS_ERR(h_file))
4a4d8108
AM
16310+ goto out;
16311+
523b37e3 16312+ if (h_file->f_op->fasync)
4a4d8108 16313+ err = h_file->f_op->fasync(fd, h_file, flag);
b912730e 16314+ fput(h_file); /* instead of au_read_post() */
1308ab2a 16315+
4f0767ce 16316+out:
4a4d8108 16317+ si_read_unlock(sb);
1308ab2a 16318+ return err;
dece6358 16319+}
4a4d8108 16320+
febd17d6
JR
16321+static int aufs_setfl(struct file *file, unsigned long arg)
16322+{
16323+ int err;
16324+ struct file *h_file;
16325+ struct super_block *sb;
16326+
16327+ sb = file->f_path.dentry->d_sb;
16328+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16329+
521ced18 16330+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
febd17d6
JR
16331+ err = PTR_ERR(h_file);
16332+ if (IS_ERR(h_file))
16333+ goto out;
16334+
1c60b727
AM
16335+ /* stop calling h_file->fasync */
16336+ arg |= vfsub_file_flags(file) & FASYNC;
febd17d6
JR
16337+ err = setfl(/*unused fd*/-1, h_file, arg);
16338+ fput(h_file); /* instead of au_read_post() */
16339+
16340+out:
16341+ si_read_unlock(sb);
16342+ return err;
16343+}
16344+
4a4d8108
AM
16345+/* ---------------------------------------------------------------------- */
16346+
16347+/* no one supports this operation, currently */
0b487229 16348+#if 0 /* reserved for future use */
4a4d8108 16349+static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
2000de60 16350+ size_t len, loff_t *pos, int more)
4a4d8108
AM
16351+{
16352+}
16353+#endif
16354+
16355+/* ---------------------------------------------------------------------- */
16356+
16357+const struct file_operations aufs_file_fop = {
16358+ .owner = THIS_MODULE,
2cbb1c4b 16359+
027c5e7a 16360+ .llseek = default_llseek,
4a4d8108
AM
16361+
16362+ .read = aufs_read,
16363+ .write = aufs_write,
076b876e
AM
16364+ .read_iter = aufs_read_iter,
16365+ .write_iter = aufs_write_iter,
16366+
4a4d8108
AM
16367+#ifdef CONFIG_AUFS_POLL
16368+ .poll = aufs_poll,
16369+#endif
16370+ .unlocked_ioctl = aufs_ioctl_nondir,
b752ccd1 16371+#ifdef CONFIG_COMPAT
c2b27bf2 16372+ .compat_ioctl = aufs_compat_ioctl_nondir,
b752ccd1 16373+#endif
4a4d8108
AM
16374+ .mmap = aufs_mmap,
16375+ .open = aufs_open_nondir,
16376+ .flush = aufs_flush_nondir,
16377+ .release = aufs_release_nondir,
16378+ .fsync = aufs_fsync_nondir,
4a4d8108
AM
16379+ .fasync = aufs_fasync,
16380+ /* .sendpage = aufs_sendpage, */
febd17d6 16381+ .setfl = aufs_setfl,
4a4d8108
AM
16382+ .splice_write = aufs_splice_write,
16383+ .splice_read = aufs_splice_read,
0b487229 16384+#if 0 /* reserved for future use */
4a4d8108 16385+ .aio_splice_write = aufs_aio_splice_write,
38d290e6 16386+ .aio_splice_read = aufs_aio_splice_read,
4a4d8108 16387+#endif
521ced18
JR
16388+ .fallocate = aufs_fallocate,
16389+ .copy_file_range = aufs_copy_file_range
4a4d8108 16390+};
7f207e10
AM
16391diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
16392--- /usr/share/empty/fs/aufs/fstype.h 1970-01-01 01:00:00.000000000 +0100
0b487229 16393+++ linux/fs/aufs/fstype.h 2021-02-22 23:30:37.702680911 +0100
062440b3
AM
16394@@ -0,0 +1,401 @@
16395+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 16396+/*
0b487229 16397+ * Copyright (C) 2005-2020 Junjiro R. Okajima
4a4d8108
AM
16398+ *
16399+ * This program, aufs is free software; you can redistribute it and/or modify
16400+ * it under the terms of the GNU General Public License as published by
16401+ * the Free Software Foundation; either version 2 of the License, or
16402+ * (at your option) any later version.
16403+ *
16404+ * This program is distributed in the hope that it will be useful,
16405+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16406+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16407+ * GNU General Public License for more details.
16408+ *
16409+ * You should have received a copy of the GNU General Public License
523b37e3 16410+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
16411+ */
16412+
16413+/*
16414+ * judging filesystem type
16415+ */
16416+
16417+#ifndef __AUFS_FSTYPE_H__
16418+#define __AUFS_FSTYPE_H__
16419+
16420+#ifdef __KERNEL__
16421+
16422+#include <linux/fs.h>
16423+#include <linux/magic.h>
b912730e 16424+#include <linux/nfs_fs.h>
b95c5147 16425+#include <linux/romfs_fs.h>
4a4d8108
AM
16426+
16427+static inline int au_test_aufs(struct super_block *sb)
16428+{
16429+ return sb->s_magic == AUFS_SUPER_MAGIC;
16430+}
16431+
16432+static inline const char *au_sbtype(struct super_block *sb)
16433+{
16434+ return sb->s_type->name;
16435+}
1308ab2a 16436+
16437+static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
16438+{
f0c0a007 16439+#if IS_ENABLED(CONFIG_ISO9660_FS)
2000de60 16440+ return sb->s_magic == ISOFS_SUPER_MAGIC;
dece6358
AM
16441+#else
16442+ return 0;
16443+#endif
16444+}
16445+
1308ab2a 16446+static inline int au_test_romfs(struct super_block *sb __maybe_unused)
dece6358 16447+{
f0c0a007 16448+#if IS_ENABLED(CONFIG_ROMFS_FS)
2000de60 16449+ return sb->s_magic == ROMFS_MAGIC;
dece6358
AM
16450+#else
16451+ return 0;
16452+#endif
16453+}
16454+
1308ab2a 16455+static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
dece6358 16456+{
f0c0a007 16457+#if IS_ENABLED(CONFIG_CRAMFS)
1308ab2a 16458+ return sb->s_magic == CRAMFS_MAGIC;
16459+#endif
16460+ return 0;
16461+}
16462+
16463+static inline int au_test_nfs(struct super_block *sb __maybe_unused)
16464+{
f0c0a007 16465+#if IS_ENABLED(CONFIG_NFS_FS)
1308ab2a 16466+ return sb->s_magic == NFS_SUPER_MAGIC;
dece6358
AM
16467+#else
16468+ return 0;
16469+#endif
16470+}
16471+
1308ab2a 16472+static inline int au_test_fuse(struct super_block *sb __maybe_unused)
dece6358 16473+{
f0c0a007 16474+#if IS_ENABLED(CONFIG_FUSE_FS)
1308ab2a 16475+ return sb->s_magic == FUSE_SUPER_MAGIC;
dece6358
AM
16476+#else
16477+ return 0;
16478+#endif
16479+}
16480+
1308ab2a 16481+static inline int au_test_xfs(struct super_block *sb __maybe_unused)
dece6358 16482+{
f0c0a007 16483+#if IS_ENABLED(CONFIG_XFS_FS)
1308ab2a 16484+ return sb->s_magic == XFS_SB_MAGIC;
dece6358
AM
16485+#else
16486+ return 0;
16487+#endif
16488+}
16489+
1308ab2a 16490+static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
dece6358 16491+{
1308ab2a 16492+#ifdef CONFIG_TMPFS
16493+ return sb->s_magic == TMPFS_MAGIC;
16494+#else
16495+ return 0;
dece6358 16496+#endif
dece6358
AM
16497+}
16498+
1308ab2a 16499+static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
1facf9fc 16500+{
f0c0a007 16501+#if IS_ENABLED(CONFIG_ECRYPT_FS)
1308ab2a 16502+ return !strcmp(au_sbtype(sb), "ecryptfs");
16503+#else
16504+ return 0;
16505+#endif
1facf9fc 16506+}
16507+
1308ab2a 16508+static inline int au_test_ramfs(struct super_block *sb)
16509+{
16510+ return sb->s_magic == RAMFS_MAGIC;
16511+}
16512+
16513+static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
16514+{
f0c0a007 16515+#if IS_ENABLED(CONFIG_UBIFS_FS)
1308ab2a 16516+ return sb->s_magic == UBIFS_SUPER_MAGIC;
16517+#else
16518+ return 0;
16519+#endif
16520+}
16521+
16522+static inline int au_test_procfs(struct super_block *sb __maybe_unused)
16523+{
16524+#ifdef CONFIG_PROC_FS
16525+ return sb->s_magic == PROC_SUPER_MAGIC;
16526+#else
16527+ return 0;
16528+#endif
16529+}
16530+
16531+static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
16532+{
16533+#ifdef CONFIG_SYSFS
16534+ return sb->s_magic == SYSFS_MAGIC;
16535+#else
16536+ return 0;
16537+#endif
16538+}
16539+
16540+static inline int au_test_configfs(struct super_block *sb __maybe_unused)
16541+{
f0c0a007 16542+#if IS_ENABLED(CONFIG_CONFIGFS_FS)
1308ab2a 16543+ return sb->s_magic == CONFIGFS_MAGIC;
16544+#else
16545+ return 0;
16546+#endif
16547+}
16548+
16549+static inline int au_test_minix(struct super_block *sb __maybe_unused)
16550+{
f0c0a007 16551+#if IS_ENABLED(CONFIG_MINIX_FS)
1308ab2a 16552+ return sb->s_magic == MINIX3_SUPER_MAGIC
16553+ || sb->s_magic == MINIX2_SUPER_MAGIC
16554+ || sb->s_magic == MINIX2_SUPER_MAGIC2
16555+ || sb->s_magic == MINIX_SUPER_MAGIC
16556+ || sb->s_magic == MINIX_SUPER_MAGIC2;
16557+#else
16558+ return 0;
16559+#endif
16560+}
16561+
1308ab2a 16562+static inline int au_test_fat(struct super_block *sb __maybe_unused)
16563+{
f0c0a007 16564+#if IS_ENABLED(CONFIG_FAT_FS)
1308ab2a 16565+ return sb->s_magic == MSDOS_SUPER_MAGIC;
16566+#else
16567+ return 0;
16568+#endif
16569+}
16570+
16571+static inline int au_test_msdos(struct super_block *sb)
16572+{
16573+ return au_test_fat(sb);
16574+}
16575+
16576+static inline int au_test_vfat(struct super_block *sb)
16577+{
16578+ return au_test_fat(sb);
16579+}
16580+
16581+static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
16582+{
16583+#ifdef CONFIG_SECURITYFS
16584+ return sb->s_magic == SECURITYFS_MAGIC;
16585+#else
16586+ return 0;
16587+#endif
16588+}
16589+
16590+static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
16591+{
f0c0a007 16592+#if IS_ENABLED(CONFIG_SQUASHFS)
1308ab2a 16593+ return sb->s_magic == SQUASHFS_MAGIC;
16594+#else
16595+ return 0;
16596+#endif
16597+}
16598+
16599+static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
16600+{
f0c0a007 16601+#if IS_ENABLED(CONFIG_BTRFS_FS)
1308ab2a 16602+ return sb->s_magic == BTRFS_SUPER_MAGIC;
16603+#else
16604+ return 0;
16605+#endif
16606+}
16607+
16608+static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
16609+{
f0c0a007 16610+#if IS_ENABLED(CONFIG_XENFS)
1308ab2a 16611+ return sb->s_magic == XENFS_SUPER_MAGIC;
16612+#else
16613+ return 0;
16614+#endif
16615+}
16616+
16617+static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
16618+{
16619+#ifdef CONFIG_DEBUG_FS
16620+ return sb->s_magic == DEBUGFS_MAGIC;
16621+#else
16622+ return 0;
16623+#endif
16624+}
16625+
16626+static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
16627+{
f0c0a007 16628+#if IS_ENABLED(CONFIG_NILFS)
1308ab2a 16629+ return sb->s_magic == NILFS_SUPER_MAGIC;
16630+#else
16631+ return 0;
16632+#endif
16633+}
16634+
4a4d8108
AM
16635+static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
16636+{
f0c0a007 16637+#if IS_ENABLED(CONFIG_HFSPLUS_FS)
4a4d8108
AM
16638+ return sb->s_magic == HFSPLUS_SUPER_MAGIC;
16639+#else
16640+ return 0;
16641+#endif
16642+}
16643+
1308ab2a 16644+/* ---------------------------------------------------------------------- */
16645+/*
16646+ * they can't be an aufs branch.
16647+ */
16648+static inline int au_test_fs_unsuppoted(struct super_block *sb)
16649+{
16650+ return
16651+#ifndef CONFIG_AUFS_BR_RAMFS
16652+ au_test_ramfs(sb) ||
16653+#endif
16654+ au_test_procfs(sb)
16655+ || au_test_sysfs(sb)
16656+ || au_test_configfs(sb)
16657+ || au_test_debugfs(sb)
16658+ || au_test_securityfs(sb)
16659+ || au_test_xenfs(sb)
16660+ || au_test_ecryptfs(sb)
16661+ /* || !strcmp(au_sbtype(sb), "unionfs") */
16662+ || au_test_aufs(sb); /* will be supported in next version */
16663+}
16664+
1308ab2a 16665+static inline int au_test_fs_remote(struct super_block *sb)
16666+{
16667+ return !au_test_tmpfs(sb)
16668+#ifdef CONFIG_AUFS_BR_RAMFS
16669+ && !au_test_ramfs(sb)
16670+#endif
16671+ && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
16672+}
16673+
16674+/* ---------------------------------------------------------------------- */
16675+
16676+/*
16677+ * Note: these functions (below) are created after reading ->getattr() in all
16678+ * filesystems under linux/fs. it means we have to do so in every update...
16679+ */
16680+
16681+/*
16682+ * some filesystems require getattr to refresh the inode attributes before
16683+ * referencing.
16684+ * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
16685+ * and leave the work for d_revalidate()
16686+ */
16687+static inline int au_test_fs_refresh_iattr(struct super_block *sb)
16688+{
16689+ return au_test_nfs(sb)
16690+ || au_test_fuse(sb)
1308ab2a 16691+ /* || au_test_btrfs(sb) */ /* untested */
1308ab2a 16692+ ;
16693+}
16694+
16695+/*
16696+ * filesystems which don't maintain i_size or i_blocks.
16697+ */
16698+static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
16699+{
16700+ return au_test_xfs(sb)
4a4d8108
AM
16701+ || au_test_btrfs(sb)
16702+ || au_test_ubifs(sb)
16703+ || au_test_hfsplus(sb) /* maintained, but incorrect */
1308ab2a 16704+ /* || au_test_minix(sb) */ /* untested */
16705+ ;
16706+}
16707+
16708+/*
16709+ * filesystems which don't store the correct value in some of their inode
16710+ * attributes.
16711+ */
16712+static inline int au_test_fs_bad_iattr(struct super_block *sb)
16713+{
16714+ return au_test_fs_bad_iattr_size(sb)
1308ab2a 16715+ || au_test_fat(sb)
16716+ || au_test_msdos(sb)
16717+ || au_test_vfat(sb);
1facf9fc 16718+}
16719+
16720+/* they don't check i_nlink in link(2) */
16721+static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
16722+{
16723+ return au_test_tmpfs(sb)
16724+#ifdef CONFIG_AUFS_BR_RAMFS
16725+ || au_test_ramfs(sb)
16726+#endif
4a4d8108 16727+ || au_test_ubifs(sb)
4a4d8108 16728+ || au_test_hfsplus(sb);
1facf9fc 16729+}
16730+
16731+/*
16732+ * filesystems which sets S_NOATIME and S_NOCMTIME.
16733+ */
16734+static inline int au_test_fs_notime(struct super_block *sb)
16735+{
16736+ return au_test_nfs(sb)
16737+ || au_test_fuse(sb)
dece6358 16738+ || au_test_ubifs(sb)
1facf9fc 16739+ ;
16740+}
16741+
1facf9fc 16742+/* temporary support for i#1 in cramfs */
16743+static inline int au_test_fs_unique_ino(struct inode *inode)
16744+{
16745+ if (au_test_cramfs(inode->i_sb))
16746+ return inode->i_ino != 1;
16747+ return 1;
16748+}
16749+
16750+/* ---------------------------------------------------------------------- */
16751+
16752+/*
16753+ * the filesystem where the xino files placed must support i/o after unlink and
16754+ * maintain i_size and i_blocks.
16755+ */
16756+static inline int au_test_fs_bad_xino(struct super_block *sb)
16757+{
16758+ return au_test_fs_remote(sb)
16759+ || au_test_fs_bad_iattr_size(sb)
1facf9fc 16760+ /* don't want unnecessary work for xino */
16761+ || au_test_aufs(sb)
1308ab2a 16762+ || au_test_ecryptfs(sb)
16763+ || au_test_nilfs(sb);
1facf9fc 16764+}
16765+
16766+static inline int au_test_fs_trunc_xino(struct super_block *sb)
16767+{
16768+ return au_test_tmpfs(sb)
16769+ || au_test_ramfs(sb);
16770+}
16771+
16772+/*
16773+ * test if the @sb is real-readonly.
16774+ */
16775+static inline int au_test_fs_rr(struct super_block *sb)
16776+{
16777+ return au_test_squashfs(sb)
16778+ || au_test_iso9660(sb)
16779+ || au_test_cramfs(sb)
16780+ || au_test_romfs(sb);
16781+}
16782+
b912730e
AM
16783+/*
16784+ * test if the @inode is nfs with 'noacl' option
2121bcd9 16785+ * NFS always sets SB_POSIXACL regardless its mount option 'noacl.'
b912730e
AM
16786+ */
16787+static inline int au_test_nfs_noacl(struct inode *inode)
16788+{
16789+ return au_test_nfs(inode->i_sb)
16790+ /* && IS_POSIXACL(inode) */
16791+ && !nfs_server_capable(inode, NFS_CAP_ACLS);
16792+}
16793+
1facf9fc 16794+#endif /* __KERNEL__ */
16795+#endif /* __AUFS_FSTYPE_H__ */
8b6a4947
AM
16796diff -urN /usr/share/empty/fs/aufs/hbl.h linux/fs/aufs/hbl.h
16797--- /usr/share/empty/fs/aufs/hbl.h 1970-01-01 01:00:00.000000000 +0100
0b487229 16798+++ linux/fs/aufs/hbl.h 2021-02-22 23:30:37.702680911 +0100
062440b3
AM
16799@@ -0,0 +1,65 @@
16800+/* SPDX-License-Identifier: GPL-2.0 */
8b6a4947 16801+/*
0b487229 16802+ * Copyright (C) 2017-2020 Junjiro R. Okajima
8b6a4947
AM
16803+ *
16804+ * This program, aufs is free software; you can redistribute it and/or modify
16805+ * it under the terms of the GNU General Public License as published by
16806+ * the Free Software Foundation; either version 2 of the License, or
16807+ * (at your option) any later version.
16808+ *
16809+ * This program is distributed in the hope that it will be useful,
16810+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16811+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16812+ * GNU General Public License for more details.
16813+ *
16814+ * You should have received a copy of the GNU General Public License
16815+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
16816+ */
16817+
16818+/*
16819+ * helpers for hlist_bl.h
16820+ */
16821+
16822+#ifndef __AUFS_HBL_H__
16823+#define __AUFS_HBL_H__
16824+
16825+#ifdef __KERNEL__
16826+
16827+#include <linux/list_bl.h>
16828+
16829+static inline void au_hbl_add(struct hlist_bl_node *node,
16830+ struct hlist_bl_head *hbl)
16831+{
16832+ hlist_bl_lock(hbl);
16833+ hlist_bl_add_head(node, hbl);
16834+ hlist_bl_unlock(hbl);
16835+}
16836+
16837+static inline void au_hbl_del(struct hlist_bl_node *node,
16838+ struct hlist_bl_head *hbl)
16839+{
16840+ hlist_bl_lock(hbl);
16841+ hlist_bl_del(node);
16842+ hlist_bl_unlock(hbl);
16843+}
16844+
16845+#define au_hbl_for_each(pos, head) \
16846+ for (pos = hlist_bl_first(head); \
16847+ pos; \
16848+ pos = pos->next)
16849+
16850+static inline unsigned long au_hbl_count(struct hlist_bl_head *hbl)
16851+{
16852+ unsigned long cnt;
16853+ struct hlist_bl_node *pos;
16854+
16855+ cnt = 0;
16856+ hlist_bl_lock(hbl);
16857+ au_hbl_for_each(pos, hbl)
16858+ cnt++;
16859+ hlist_bl_unlock(hbl);
16860+ return cnt;
16861+}
16862+
16863+#endif /* __KERNEL__ */
16864+#endif /* __AUFS_HBL_H__ */
7f207e10
AM
16865diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
16866--- /usr/share/empty/fs/aufs/hfsnotify.c 1970-01-01 01:00:00.000000000 +0100
0b487229 16867+++ linux/fs/aufs/hfsnotify.c 2021-02-22 23:30:37.702680911 +0100
acd2b654 16868@@ -0,0 +1,289 @@
cd7a4cd9 16869+// SPDX-License-Identifier: GPL-2.0
1facf9fc 16870+/*
0b487229 16871+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 16872+ *
16873+ * This program, aufs is free software; you can redistribute it and/or modify
16874+ * it under the terms of the GNU General Public License as published by
16875+ * the Free Software Foundation; either version 2 of the License, or
16876+ * (at your option) any later version.
dece6358
AM
16877+ *
16878+ * This program is distributed in the hope that it will be useful,
16879+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16880+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16881+ * GNU General Public License for more details.
16882+ *
16883+ * You should have received a copy of the GNU General Public License
523b37e3 16884+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 16885+ */
16886+
16887+/*
4a4d8108 16888+ * fsnotify for the lower directories
1facf9fc 16889+ */
16890+
16891+#include "aufs.h"
16892+
4a4d8108
AM
16893+/* FS_IN_IGNORED is unnecessary */
16894+static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
16895+ | FS_CREATE | FS_EVENT_ON_CHILD);
7f207e10 16896+static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
7eafdf33 16897+static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
1facf9fc 16898+
0c5527e5 16899+static void au_hfsn_free_mark(struct fsnotify_mark *mark)
1facf9fc 16900+{
0c5527e5
AM
16901+ struct au_hnotify *hn = container_of(mark, struct au_hnotify,
16902+ hn_mark);
5afbbe0d 16903+ /* AuDbg("here\n"); */
1c60b727 16904+ au_cache_free_hnotify(hn);
8b6a4947 16905+ smp_mb__before_atomic(); /* for atomic64_dec */
1716fcea
AM
16906+ if (atomic64_dec_and_test(&au_hfsn_ifree))
16907+ wake_up(&au_hfsn_wq);
4a4d8108 16908+}
1facf9fc 16909+
027c5e7a 16910+static int au_hfsn_alloc(struct au_hinode *hinode)
4a4d8108 16911+{
1716fcea 16912+ int err;
027c5e7a
AM
16913+ struct au_hnotify *hn;
16914+ struct super_block *sb;
16915+ struct au_branch *br;
0c5527e5 16916+ struct fsnotify_mark *mark;
027c5e7a 16917+ aufs_bindex_t bindex;
1facf9fc 16918+
027c5e7a
AM
16919+ hn = hinode->hi_notify;
16920+ sb = hn->hn_aufs_inode->i_sb;
16921+ bindex = au_br_index(sb, hinode->hi_id);
16922+ br = au_sbr(sb, bindex);
1716fcea
AM
16923+ AuDebugOn(!br->br_hfsn);
16924+
0c5527e5 16925+ mark = &hn->hn_mark;
ffa93bbd 16926+ fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
0c5527e5 16927+ mark->mask = AuHfsnMask;
7f207e10
AM
16928+ /*
16929+ * by udba rename or rmdir, aufs assign a new inode to the known
16930+ * h_inode, so specify 1 to allow dups.
16931+ */
c1595e42 16932+ lockdep_off();
acd2b654 16933+ err = fsnotify_add_inode_mark(mark, hinode->hi_inode, /*allow_dups*/1);
c1595e42 16934+ lockdep_on();
1716fcea
AM
16935+
16936+ return err;
1facf9fc 16937+}
16938+
7eafdf33 16939+static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
1facf9fc 16940+{
0c5527e5 16941+ struct fsnotify_mark *mark;
7eafdf33 16942+ unsigned long long ull;
1716fcea 16943+ struct fsnotify_group *group;
7eafdf33
AM
16944+
16945+ ull = atomic64_inc_return(&au_hfsn_ifree);
16946+ BUG_ON(!ull);
953406b4 16947+
0c5527e5 16948+ mark = &hn->hn_mark;
1716fcea
AM
16949+ spin_lock(&mark->lock);
16950+ group = mark->group;
16951+ fsnotify_get_group(group);
16952+ spin_unlock(&mark->lock);
c1595e42 16953+ lockdep_off();
1716fcea 16954+ fsnotify_destroy_mark(mark, group);
5afbbe0d 16955+ fsnotify_put_mark(mark);
1716fcea 16956+ fsnotify_put_group(group);
c1595e42 16957+ lockdep_on();
7f207e10 16958+
7eafdf33
AM
16959+ /* free hn by myself */
16960+ return 0;
1facf9fc 16961+}
16962+
16963+/* ---------------------------------------------------------------------- */
16964+
4a4d8108 16965+static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
1facf9fc 16966+{
0c5527e5 16967+ struct fsnotify_mark *mark;
1facf9fc 16968+
0c5527e5
AM
16969+ mark = &hinode->hi_notify->hn_mark;
16970+ spin_lock(&mark->lock);
1facf9fc 16971+ if (do_set) {
0c5527e5
AM
16972+ AuDebugOn(mark->mask & AuHfsnMask);
16973+ mark->mask |= AuHfsnMask;
1facf9fc 16974+ } else {
0c5527e5
AM
16975+ AuDebugOn(!(mark->mask & AuHfsnMask));
16976+ mark->mask &= ~AuHfsnMask;
1facf9fc 16977+ }
0c5527e5 16978+ spin_unlock(&mark->lock);
4a4d8108 16979+ /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
1facf9fc 16980+}
16981+
4a4d8108 16982+/* ---------------------------------------------------------------------- */
1facf9fc 16983+
4a4d8108
AM
16984+/* #define AuDbgHnotify */
16985+#ifdef AuDbgHnotify
16986+static char *au_hfsn_name(u32 mask)
16987+{
16988+#ifdef CONFIG_AUFS_DEBUG
c06a8ce3
AM
16989+#define test_ret(flag) \
16990+ do { \
16991+ if (mask & flag) \
16992+ return #flag; \
16993+ } while (0)
4a4d8108
AM
16994+ test_ret(FS_ACCESS);
16995+ test_ret(FS_MODIFY);
16996+ test_ret(FS_ATTRIB);
16997+ test_ret(FS_CLOSE_WRITE);
16998+ test_ret(FS_CLOSE_NOWRITE);
16999+ test_ret(FS_OPEN);
17000+ test_ret(FS_MOVED_FROM);
17001+ test_ret(FS_MOVED_TO);
17002+ test_ret(FS_CREATE);
17003+ test_ret(FS_DELETE);
17004+ test_ret(FS_DELETE_SELF);
17005+ test_ret(FS_MOVE_SELF);
17006+ test_ret(FS_UNMOUNT);
17007+ test_ret(FS_Q_OVERFLOW);
17008+ test_ret(FS_IN_IGNORED);
b912730e 17009+ test_ret(FS_ISDIR);
4a4d8108
AM
17010+ test_ret(FS_IN_ONESHOT);
17011+ test_ret(FS_EVENT_ON_CHILD);
17012+ return "";
17013+#undef test_ret
17014+#else
17015+ return "??";
17016+#endif
1facf9fc 17017+}
4a4d8108 17018+#endif
1facf9fc 17019+
17020+/* ---------------------------------------------------------------------- */
17021+
1716fcea
AM
17022+static void au_hfsn_free_group(struct fsnotify_group *group)
17023+{
17024+ struct au_br_hfsnotify *hfsn = group->private;
17025+
5afbbe0d 17026+ /* AuDbg("here\n"); */
e49925d1 17027+ au_kfree_try_rcu(hfsn);
1716fcea
AM
17028+}
17029+
4a4d8108 17030+static int au_hfsn_handle_event(struct fsnotify_group *group,
fb47a38f 17031+ struct inode *inode,
a2654f78 17032+ u32 mask, const void *data, int data_type,
ffa93bbd
AM
17033+ const unsigned char *file_name, u32 cookie,
17034+ struct fsnotify_iter_info *iter_info)
1facf9fc 17035+{
17036+ int err;
4a4d8108
AM
17037+ struct au_hnotify *hnotify;
17038+ struct inode *h_dir, *h_inode;
fb47a38f 17039+ struct qstr h_child_qstr = QSTR_INIT(file_name, strlen(file_name));
cd7a4cd9 17040+ struct fsnotify_mark *inode_mark;
4a4d8108 17041+
fb47a38f 17042+ AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
1facf9fc 17043+
17044+ err = 0;
0c5527e5 17045+ /* if FS_UNMOUNT happens, there must be another bug */
4a4d8108 17046+ AuDebugOn(mask & FS_UNMOUNT);
0c5527e5 17047+ if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
1facf9fc 17048+ goto out;
1facf9fc 17049+
fb47a38f
JR
17050+ h_dir = inode;
17051+ h_inode = NULL;
4a4d8108 17052+#ifdef AuDbgHnotify
392086de 17053+ au_debug_on();
4a4d8108
AM
17054+ if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
17055+ || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
17056+ AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
17057+ h_dir->i_ino, mask, au_hfsn_name(mask),
17058+ AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
17059+ /* WARN_ON(1); */
1facf9fc 17060+ }
392086de 17061+ au_debug_off();
1facf9fc 17062+#endif
4a4d8108 17063+
cd7a4cd9 17064+ inode_mark = fsnotify_iter_inode_mark(iter_info);
0c5527e5
AM
17065+ AuDebugOn(!inode_mark);
17066+ hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
17067+ err = au_hnotify(h_dir, hnotify, mask, &h_child_qstr, h_inode);
1facf9fc 17068+
4a4d8108
AM
17069+out:
17070+ return err;
17071+}
1facf9fc 17072+
4a4d8108 17073+static struct fsnotify_ops au_hfsn_ops = {
1716fcea 17074+ .handle_event = au_hfsn_handle_event,
ffa93bbd
AM
17075+ .free_group_priv = au_hfsn_free_group,
17076+ .free_mark = au_hfsn_free_mark
4a4d8108
AM
17077+};
17078+
17079+/* ---------------------------------------------------------------------- */
17080+
027c5e7a
AM
17081+static void au_hfsn_fin_br(struct au_branch *br)
17082+{
1716fcea 17083+ struct au_br_hfsnotify *hfsn;
027c5e7a 17084+
1716fcea 17085+ hfsn = br->br_hfsn;
c1595e42
JR
17086+ if (hfsn) {
17087+ lockdep_off();
1716fcea 17088+ fsnotify_put_group(hfsn->hfsn_group);
c1595e42
JR
17089+ lockdep_on();
17090+ }
027c5e7a
AM
17091+}
17092+
1716fcea 17093+static int au_hfsn_init_br(struct au_branch *br, int perm)
4a4d8108
AM
17094+{
17095+ int err;
1716fcea
AM
17096+ struct fsnotify_group *group;
17097+ struct au_br_hfsnotify *hfsn;
1facf9fc 17098+
4a4d8108 17099+ err = 0;
1716fcea
AM
17100+ br->br_hfsn = NULL;
17101+ if (!au_br_hnotifyable(perm))
027c5e7a 17102+ goto out;
027c5e7a 17103+
1716fcea
AM
17104+ err = -ENOMEM;
17105+ hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
17106+ if (unlikely(!hfsn))
027c5e7a
AM
17107+ goto out;
17108+
1716fcea
AM
17109+ err = 0;
17110+ group = fsnotify_alloc_group(&au_hfsn_ops);
17111+ if (IS_ERR(group)) {
17112+ err = PTR_ERR(group);
0c5527e5 17113+ pr_err("fsnotify_alloc_group() failed, %d\n", err);
1716fcea 17114+ goto out_hfsn;
4a4d8108 17115+ }
1facf9fc 17116+
1716fcea
AM
17117+ group->private = hfsn;
17118+ hfsn->hfsn_group = group;
17119+ br->br_hfsn = hfsn;
17120+ goto out; /* success */
17121+
17122+out_hfsn:
e49925d1 17123+ au_kfree_try_rcu(hfsn);
027c5e7a 17124+out:
1716fcea
AM
17125+ return err;
17126+}
17127+
17128+static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
17129+{
17130+ int err;
17131+
17132+ err = 0;
17133+ if (!br->br_hfsn)
17134+ err = au_hfsn_init_br(br, perm);
17135+
1facf9fc 17136+ return err;
17137+}
17138+
7eafdf33
AM
17139+/* ---------------------------------------------------------------------- */
17140+
17141+static void au_hfsn_fin(void)
17142+{
17143+ AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
17144+ wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
17145+}
17146+
4a4d8108
AM
17147+const struct au_hnotify_op au_hnotify_op = {
17148+ .ctl = au_hfsn_ctl,
17149+ .alloc = au_hfsn_alloc,
17150+ .free = au_hfsn_free,
1facf9fc 17151+
7eafdf33
AM
17152+ .fin = au_hfsn_fin,
17153+
027c5e7a
AM
17154+ .reset_br = au_hfsn_reset_br,
17155+ .fin_br = au_hfsn_fin_br,
17156+ .init_br = au_hfsn_init_br
4a4d8108 17157+};
7f207e10
AM
17158diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
17159--- /usr/share/empty/fs/aufs/hfsplus.c 1970-01-01 01:00:00.000000000 +0100
0b487229 17160+++ linux/fs/aufs/hfsplus.c 2021-02-22 23:30:37.702680911 +0100
acd2b654 17161@@ -0,0 +1,60 @@
cd7a4cd9 17162+// SPDX-License-Identifier: GPL-2.0
4a4d8108 17163+/*
0b487229 17164+ * Copyright (C) 2010-2020 Junjiro R. Okajima
4a4d8108
AM
17165+ *
17166+ * This program, aufs is free software; you can redistribute it and/or modify
17167+ * it under the terms of the GNU General Public License as published by
17168+ * the Free Software Foundation; either version 2 of the License, or
17169+ * (at your option) any later version.
17170+ *
17171+ * This program is distributed in the hope that it will be useful,
17172+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17173+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17174+ * GNU General Public License for more details.
17175+ *
17176+ * You should have received a copy of the GNU General Public License
523b37e3 17177+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 17178+ */
1facf9fc 17179+
4a4d8108 17180+/*
acd2b654 17181+ * special support for filesystems which acquires an inode mutex
4a4d8108
AM
17182+ * at final closing a file, eg, hfsplus.
17183+ *
17184+ * This trick is very simple and stupid, just to open the file before really
acd2b654 17185+ * necessary open to tell hfsplus that this is not the final closing.
4a4d8108
AM
17186+ * The caller should call au_h_open_pre() after acquiring the inode mutex,
17187+ * and au_h_open_post() after releasing it.
17188+ */
1facf9fc 17189+
4a4d8108 17190+#include "aufs.h"
1facf9fc 17191+
392086de
AM
17192+struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
17193+ int force_wr)
4a4d8108
AM
17194+{
17195+ struct file *h_file;
17196+ struct dentry *h_dentry;
1facf9fc 17197+
4a4d8108
AM
17198+ h_dentry = au_h_dptr(dentry, bindex);
17199+ AuDebugOn(!h_dentry);
5527c038 17200+ AuDebugOn(d_is_negative(h_dentry));
4a4d8108
AM
17201+
17202+ h_file = NULL;
17203+ if (au_test_hfsplus(h_dentry->d_sb)
7e9cd9fe 17204+ && d_is_reg(h_dentry))
4a4d8108
AM
17205+ h_file = au_h_open(dentry, bindex,
17206+ O_RDONLY | O_NOATIME | O_LARGEFILE,
392086de 17207+ /*file*/NULL, force_wr);
4a4d8108 17208+ return h_file;
1facf9fc 17209+}
17210+
4a4d8108
AM
17211+void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
17212+ struct file *h_file)
17213+{
acd2b654
AM
17214+ struct au_branch *br;
17215+
4a4d8108
AM
17216+ if (h_file) {
17217+ fput(h_file);
acd2b654
AM
17218+ br = au_sbr(dentry->d_sb, bindex);
17219+ au_lcnt_dec(&br->br_nfiles);
4a4d8108
AM
17220+ }
17221+}
7f207e10
AM
17222diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
17223--- /usr/share/empty/fs/aufs/hnotify.c 1970-01-01 01:00:00.000000000 +0100
0b487229
JR
17224+++ linux/fs/aufs/hnotify.c 2021-02-22 23:30:37.702680911 +0100
17225@@ -0,0 +1,715 @@
cd7a4cd9 17226+// SPDX-License-Identifier: GPL-2.0
e49829fe 17227+/*
0b487229 17228+ * Copyright (C) 2005-2020 Junjiro R. Okajima
e49829fe
JR
17229+ *
17230+ * This program, aufs is free software; you can redistribute it and/or modify
17231+ * it under the terms of the GNU General Public License as published by
17232+ * the Free Software Foundation; either version 2 of the License, or
17233+ * (at your option) any later version.
17234+ *
17235+ * This program is distributed in the hope that it will be useful,
17236+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17237+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17238+ * GNU General Public License for more details.
17239+ *
17240+ * You should have received a copy of the GNU General Public License
523b37e3 17241+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
e49829fe
JR
17242+ */
17243+
17244+/*
7f207e10 17245+ * abstraction to notify the direct changes on lower directories
e49829fe
JR
17246+ */
17247+
0b487229 17248+/* #include <linux/iversion.h> */
e49829fe
JR
17249+#include "aufs.h"
17250+
027c5e7a 17251+int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
e49829fe
JR
17252+{
17253+ int err;
7f207e10 17254+ struct au_hnotify *hn;
1facf9fc 17255+
4a4d8108
AM
17256+ err = -ENOMEM;
17257+ hn = au_cache_alloc_hnotify();
17258+ if (hn) {
17259+ hn->hn_aufs_inode = inode;
027c5e7a
AM
17260+ hinode->hi_notify = hn;
17261+ err = au_hnotify_op.alloc(hinode);
17262+ AuTraceErr(err);
17263+ if (unlikely(err)) {
17264+ hinode->hi_notify = NULL;
1c60b727 17265+ au_cache_free_hnotify(hn);
4a4d8108
AM
17266+ /*
17267+ * The upper dir was removed by udba, but the same named
acd2b654 17268+ * dir left. In this case, aufs assigns a new inode
4a4d8108 17269+ * number and set the monitor again.
acd2b654 17270+ * For the lower dir, the old monitor is still left.
4a4d8108
AM
17271+ */
17272+ if (err == -EEXIST)
17273+ err = 0;
17274+ }
1308ab2a 17275+ }
1308ab2a 17276+
027c5e7a 17277+ AuTraceErr(err);
1308ab2a 17278+ return err;
dece6358 17279+}
1facf9fc 17280+
4a4d8108 17281+void au_hn_free(struct au_hinode *hinode)
dece6358 17282+{
4a4d8108 17283+ struct au_hnotify *hn;
1facf9fc 17284+
4a4d8108
AM
17285+ hn = hinode->hi_notify;
17286+ if (hn) {
4a4d8108 17287+ hinode->hi_notify = NULL;
7eafdf33 17288+ if (au_hnotify_op.free(hinode, hn))
1c60b727 17289+ au_cache_free_hnotify(hn);
4a4d8108
AM
17290+ }
17291+}
dece6358 17292+
4a4d8108 17293+/* ---------------------------------------------------------------------- */
dece6358 17294+
4a4d8108
AM
17295+void au_hn_ctl(struct au_hinode *hinode, int do_set)
17296+{
17297+ if (hinode->hi_notify)
17298+ au_hnotify_op.ctl(hinode, do_set);
17299+}
17300+
17301+void au_hn_reset(struct inode *inode, unsigned int flags)
17302+{
5afbbe0d 17303+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
17304+ struct inode *hi;
17305+ struct dentry *iwhdentry;
1facf9fc 17306+
5afbbe0d
AM
17307+ bbot = au_ibbot(inode);
17308+ for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
4a4d8108
AM
17309+ hi = au_h_iptr(inode, bindex);
17310+ if (!hi)
17311+ continue;
1308ab2a 17312+
febd17d6 17313+ /* inode_lock_nested(hi, AuLsc_I_CHILD); */
4a4d8108
AM
17314+ iwhdentry = au_hi_wh(inode, bindex);
17315+ if (iwhdentry)
17316+ dget(iwhdentry);
17317+ au_igrab(hi);
17318+ au_set_h_iptr(inode, bindex, NULL, 0);
17319+ au_set_h_iptr(inode, bindex, au_igrab(hi),
17320+ flags & ~AuHi_XINO);
17321+ iput(hi);
17322+ dput(iwhdentry);
febd17d6 17323+ /* inode_unlock(hi); */
1facf9fc 17324+ }
1facf9fc 17325+}
17326+
1308ab2a 17327+/* ---------------------------------------------------------------------- */
1facf9fc 17328+
4a4d8108 17329+static int hn_xino(struct inode *inode, struct inode *h_inode)
1facf9fc 17330+{
4a4d8108 17331+ int err;
5afbbe0d 17332+ aufs_bindex_t bindex, bbot, bfound, btop;
4a4d8108 17333+ struct inode *h_i;
1facf9fc 17334+
4a4d8108
AM
17335+ err = 0;
17336+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 17337+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
17338+ goto out;
17339+ }
1facf9fc 17340+
4a4d8108 17341+ bfound = -1;
5afbbe0d
AM
17342+ bbot = au_ibbot(inode);
17343+ btop = au_ibtop(inode);
4a4d8108 17344+#if 0 /* reserved for future use */
5afbbe0d 17345+ if (bindex == bbot) {
4a4d8108
AM
17346+ /* keep this ino in rename case */
17347+ goto out;
17348+ }
17349+#endif
5afbbe0d 17350+ for (bindex = btop; bindex <= bbot; bindex++)
4a4d8108
AM
17351+ if (au_h_iptr(inode, bindex) == h_inode) {
17352+ bfound = bindex;
17353+ break;
17354+ }
17355+ if (bfound < 0)
1308ab2a 17356+ goto out;
1facf9fc 17357+
5afbbe0d 17358+ for (bindex = btop; bindex <= bbot; bindex++) {
4a4d8108
AM
17359+ h_i = au_h_iptr(inode, bindex);
17360+ if (!h_i)
17361+ continue;
1facf9fc 17362+
4a4d8108
AM
17363+ err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
17364+ /* ignore this error */
17365+ /* bad action? */
1facf9fc 17366+ }
1facf9fc 17367+
4a4d8108 17368+ /* children inode number will be broken */
1facf9fc 17369+
4f0767ce 17370+out:
4a4d8108
AM
17371+ AuTraceErr(err);
17372+ return err;
1facf9fc 17373+}
17374+
4a4d8108 17375+static int hn_gen_tree(struct dentry *dentry)
1facf9fc 17376+{
4a4d8108
AM
17377+ int err, i, j, ndentry;
17378+ struct au_dcsub_pages dpages;
17379+ struct au_dpage *dpage;
17380+ struct dentry **dentries;
1facf9fc 17381+
4a4d8108
AM
17382+ err = au_dpages_init(&dpages, GFP_NOFS);
17383+ if (unlikely(err))
17384+ goto out;
17385+ err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
17386+ if (unlikely(err))
17387+ goto out_dpages;
1facf9fc 17388+
4a4d8108
AM
17389+ for (i = 0; i < dpages.ndpage; i++) {
17390+ dpage = dpages.dpages + i;
17391+ dentries = dpage->dentries;
17392+ ndentry = dpage->ndentry;
17393+ for (j = 0; j < ndentry; j++) {
17394+ struct dentry *d;
17395+
17396+ d = dentries[j];
17397+ if (IS_ROOT(d))
17398+ continue;
17399+
4a4d8108 17400+ au_digen_dec(d);
5527c038 17401+ if (d_really_is_positive(d))
4a4d8108
AM
17402+ /* todo: reset children xino?
17403+ cached children only? */
5527c038 17404+ au_iigen_dec(d_inode(d));
1308ab2a 17405+ }
dece6358 17406+ }
1facf9fc 17407+
4f0767ce 17408+out_dpages:
4a4d8108 17409+ au_dpages_free(&dpages);
4f0767ce 17410+out:
dece6358
AM
17411+ return err;
17412+}
17413+
1308ab2a 17414+/*
4a4d8108 17415+ * return 0 if processed.
1308ab2a 17416+ */
4a4d8108
AM
17417+static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
17418+ const unsigned int isdir)
dece6358 17419+{
1308ab2a 17420+ int err;
4a4d8108
AM
17421+ struct dentry *d;
17422+ struct qstr *dname;
1facf9fc 17423+
4a4d8108
AM
17424+ err = 1;
17425+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 17426+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
17427+ err = 0;
17428+ goto out;
17429+ }
dece6358 17430+
4a4d8108
AM
17431+ if (!isdir) {
17432+ AuDebugOn(!name);
17433+ au_iigen_dec(inode);
027c5e7a 17434+ spin_lock(&inode->i_lock);
c1595e42 17435+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
027c5e7a 17436+ spin_lock(&d->d_lock);
4a4d8108
AM
17437+ dname = &d->d_name;
17438+ if (dname->len != nlen
027c5e7a
AM
17439+ && memcmp(dname->name, name, nlen)) {
17440+ spin_unlock(&d->d_lock);
4a4d8108 17441+ continue;
027c5e7a 17442+ }
4a4d8108 17443+ err = 0;
4a4d8108
AM
17444+ au_digen_dec(d);
17445+ spin_unlock(&d->d_lock);
17446+ break;
1facf9fc 17447+ }
027c5e7a 17448+ spin_unlock(&inode->i_lock);
1308ab2a 17449+ } else {
027c5e7a 17450+ au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
c1595e42 17451+ d = d_find_any_alias(inode);
4a4d8108
AM
17452+ if (!d) {
17453+ au_iigen_dec(inode);
17454+ goto out;
17455+ }
1facf9fc 17456+
027c5e7a 17457+ spin_lock(&d->d_lock);
4a4d8108 17458+ dname = &d->d_name;
027c5e7a
AM
17459+ if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
17460+ spin_unlock(&d->d_lock);
4a4d8108 17461+ err = hn_gen_tree(d);
027c5e7a
AM
17462+ spin_lock(&d->d_lock);
17463+ }
17464+ spin_unlock(&d->d_lock);
4a4d8108
AM
17465+ dput(d);
17466+ }
1facf9fc 17467+
4f0767ce 17468+out:
4a4d8108 17469+ AuTraceErr(err);
1308ab2a 17470+ return err;
17471+}
dece6358 17472+
4a4d8108 17473+static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
1facf9fc 17474+{
4a4d8108 17475+ int err;
1facf9fc 17476+
5527c038 17477+ if (IS_ROOT(dentry)) {
0c3ec466 17478+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
17479+ return 0;
17480+ }
1308ab2a 17481+
4a4d8108
AM
17482+ err = 0;
17483+ if (!isdir) {
4a4d8108 17484+ au_digen_dec(dentry);
5527c038
JR
17485+ if (d_really_is_positive(dentry))
17486+ au_iigen_dec(d_inode(dentry));
4a4d8108 17487+ } else {
027c5e7a 17488+ au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
5527c038 17489+ if (d_really_is_positive(dentry))
4a4d8108
AM
17490+ err = hn_gen_tree(dentry);
17491+ }
17492+
17493+ AuTraceErr(err);
17494+ return err;
1facf9fc 17495+}
17496+
4a4d8108 17497+/* ---------------------------------------------------------------------- */
1facf9fc 17498+
4a4d8108
AM
17499+/* hnotify job flags */
17500+#define AuHnJob_XINO0 1
17501+#define AuHnJob_GEN (1 << 1)
17502+#define AuHnJob_DIRENT (1 << 2)
17503+#define AuHnJob_ISDIR (1 << 3)
17504+#define AuHnJob_TRYXINO0 (1 << 4)
17505+#define AuHnJob_MNTPNT (1 << 5)
17506+#define au_ftest_hnjob(flags, name) ((flags) & AuHnJob_##name)
7f207e10
AM
17507+#define au_fset_hnjob(flags, name) \
17508+ do { (flags) |= AuHnJob_##name; } while (0)
17509+#define au_fclr_hnjob(flags, name) \
17510+ do { (flags) &= ~AuHnJob_##name; } while (0)
1facf9fc 17511+
4a4d8108
AM
17512+enum {
17513+ AuHn_CHILD,
17514+ AuHn_PARENT,
17515+ AuHnLast
17516+};
1facf9fc 17517+
4a4d8108
AM
17518+struct au_hnotify_args {
17519+ struct inode *h_dir, *dir, *h_child_inode;
17520+ u32 mask;
17521+ unsigned int flags[AuHnLast];
17522+ unsigned int h_child_nlen;
17523+ char h_child_name[];
17524+};
1facf9fc 17525+
4a4d8108
AM
17526+struct hn_job_args {
17527+ unsigned int flags;
17528+ struct inode *inode, *h_inode, *dir, *h_dir;
17529+ struct dentry *dentry;
17530+ char *h_name;
17531+ int h_nlen;
17532+};
1308ab2a 17533+
4a4d8108
AM
17534+static int hn_job(struct hn_job_args *a)
17535+{
17536+ const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
076b876e 17537+ int e;
1308ab2a 17538+
4a4d8108
AM
17539+ /* reset xino */
17540+ if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
17541+ hn_xino(a->inode, a->h_inode); /* ignore this error */
1308ab2a 17542+
4a4d8108
AM
17543+ if (au_ftest_hnjob(a->flags, TRYXINO0)
17544+ && a->inode
17545+ && a->h_inode) {
be118d29 17546+ inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
38d290e6
JR
17547+ if (!a->h_inode->i_nlink
17548+ && !(a->h_inode->i_state & I_LINKABLE))
4a4d8108 17549+ hn_xino(a->inode, a->h_inode); /* ignore this error */
3c1bdaff 17550+ inode_unlock_shared(a->h_inode);
1308ab2a 17551+ }
1facf9fc 17552+
4a4d8108
AM
17553+ /* make the generation obsolete */
17554+ if (au_ftest_hnjob(a->flags, GEN)) {
076b876e 17555+ e = -1;
4a4d8108 17556+ if (a->inode)
076b876e 17557+ e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
4a4d8108 17558+ isdir);
076b876e 17559+ if (e && a->dentry)
4a4d8108
AM
17560+ hn_gen_by_name(a->dentry, isdir);
17561+ /* ignore this error */
1facf9fc 17562+ }
1facf9fc 17563+
4a4d8108
AM
17564+ /* make dir entries obsolete */
17565+ if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
17566+ struct au_vdir *vdir;
1facf9fc 17567+
4a4d8108
AM
17568+ vdir = au_ivdir(a->inode);
17569+ if (vdir)
17570+ vdir->vd_jiffy = 0;
17571+ /* IMustLock(a->inode); */
be118d29 17572+ /* inode_inc_iversion(a->inode); */
4a4d8108 17573+ }
1facf9fc 17574+
4a4d8108
AM
17575+ /* can do nothing but warn */
17576+ if (au_ftest_hnjob(a->flags, MNTPNT)
17577+ && a->dentry
17578+ && d_mountpoint(a->dentry))
523b37e3 17579+ pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
1facf9fc 17580+
4a4d8108 17581+ return 0;
1308ab2a 17582+}
1facf9fc 17583+
1308ab2a 17584+/* ---------------------------------------------------------------------- */
1facf9fc 17585+
4a4d8108
AM
17586+static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
17587+ struct inode *dir)
1308ab2a 17588+{
4a4d8108
AM
17589+ struct dentry *dentry, *d, *parent;
17590+ struct qstr *dname;
1308ab2a 17591+
c1595e42 17592+ parent = d_find_any_alias(dir);
4a4d8108
AM
17593+ if (!parent)
17594+ return NULL;
1308ab2a 17595+
4a4d8108 17596+ dentry = NULL;
027c5e7a 17597+ spin_lock(&parent->d_lock);
c1595e42 17598+ list_for_each_entry(d, &parent->d_subdirs, d_child) {
523b37e3 17599+ /* AuDbg("%pd\n", d); */
027c5e7a 17600+ spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
4a4d8108
AM
17601+ dname = &d->d_name;
17602+ if (dname->len != nlen || memcmp(dname->name, name, nlen))
027c5e7a
AM
17603+ goto cont_unlock;
17604+ if (au_di(d))
17605+ au_digen_dec(d);
17606+ else
17607+ goto cont_unlock;
c1595e42 17608+ if (au_dcount(d) > 0) {
027c5e7a 17609+ dentry = dget_dlock(d);
4a4d8108 17610+ spin_unlock(&d->d_lock);
027c5e7a 17611+ break;
dece6358 17612+ }
1facf9fc 17613+
f6b6e03d 17614+cont_unlock:
027c5e7a 17615+ spin_unlock(&d->d_lock);
1308ab2a 17616+ }
027c5e7a 17617+ spin_unlock(&parent->d_lock);
4a4d8108 17618+ dput(parent);
1facf9fc 17619+
4a4d8108
AM
17620+ if (dentry)
17621+ di_write_lock_child(dentry);
1308ab2a 17622+
4a4d8108
AM
17623+ return dentry;
17624+}
dece6358 17625+
4a4d8108
AM
17626+static struct inode *lookup_wlock_by_ino(struct super_block *sb,
17627+ aufs_bindex_t bindex, ino_t h_ino)
17628+{
17629+ struct inode *inode;
17630+ ino_t ino;
17631+ int err;
17632+
17633+ inode = NULL;
17634+ err = au_xino_read(sb, bindex, h_ino, &ino);
17635+ if (!err && ino)
17636+ inode = ilookup(sb, ino);
17637+ if (!inode)
17638+ goto out;
17639+
17640+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 17641+ pr_warn("wrong root branch\n");
4a4d8108
AM
17642+ iput(inode);
17643+ inode = NULL;
17644+ goto out;
1308ab2a 17645+ }
17646+
4a4d8108 17647+ ii_write_lock_child(inode);
1308ab2a 17648+
4f0767ce 17649+out:
4a4d8108 17650+ return inode;
dece6358
AM
17651+}
17652+
4a4d8108 17653+static void au_hn_bh(void *_args)
1facf9fc 17654+{
4a4d8108
AM
17655+ struct au_hnotify_args *a = _args;
17656+ struct super_block *sb;
5afbbe0d 17657+ aufs_bindex_t bindex, bbot, bfound;
4a4d8108 17658+ unsigned char xino, try_iput;
1facf9fc 17659+ int err;
1308ab2a 17660+ struct inode *inode;
4a4d8108
AM
17661+ ino_t h_ino;
17662+ struct hn_job_args args;
17663+ struct dentry *dentry;
17664+ struct au_sbinfo *sbinfo;
1facf9fc 17665+
4a4d8108
AM
17666+ AuDebugOn(!_args);
17667+ AuDebugOn(!a->h_dir);
17668+ AuDebugOn(!a->dir);
17669+ AuDebugOn(!a->mask);
17670+ AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
17671+ a->mask, a->dir->i_ino, a->h_dir->i_ino,
17672+ a->h_child_inode ? a->h_child_inode->i_ino : 0);
1facf9fc 17673+
4a4d8108
AM
17674+ inode = NULL;
17675+ dentry = NULL;
17676+ /*
17677+ * do not lock a->dir->i_mutex here
17678+ * because of d_revalidate() may cause a deadlock.
17679+ */
17680+ sb = a->dir->i_sb;
17681+ AuDebugOn(!sb);
17682+ sbinfo = au_sbi(sb);
17683+ AuDebugOn(!sbinfo);
7f207e10 17684+ si_write_lock(sb, AuLock_NOPLMW);
1facf9fc 17685+
8b6a4947
AM
17686+ if (au_opt_test(sbinfo->si_mntflags, DIRREN))
17687+ switch (a->mask & FS_EVENTS_POSS_ON_CHILD) {
17688+ case FS_MOVED_FROM:
17689+ case FS_MOVED_TO:
17690+ AuWarn1("DIRREN with UDBA may not work correctly "
17691+ "for the direct rename(2)\n");
17692+ }
17693+
4a4d8108
AM
17694+ ii_read_lock_parent(a->dir);
17695+ bfound = -1;
5afbbe0d
AM
17696+ bbot = au_ibbot(a->dir);
17697+ for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
4a4d8108
AM
17698+ if (au_h_iptr(a->dir, bindex) == a->h_dir) {
17699+ bfound = bindex;
17700+ break;
17701+ }
17702+ ii_read_unlock(a->dir);
17703+ if (unlikely(bfound < 0))
17704+ goto out;
1facf9fc 17705+
4a4d8108
AM
17706+ xino = !!au_opt_test(au_mntflags(sb), XINO);
17707+ h_ino = 0;
17708+ if (a->h_child_inode)
17709+ h_ino = a->h_child_inode->i_ino;
1facf9fc 17710+
4a4d8108
AM
17711+ if (a->h_child_nlen
17712+ && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
17713+ || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
17714+ dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
17715+ a->dir);
17716+ try_iput = 0;
5527c038
JR
17717+ if (dentry && d_really_is_positive(dentry))
17718+ inode = d_inode(dentry);
4a4d8108
AM
17719+ if (xino && !inode && h_ino
17720+ && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
17721+ || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
17722+ || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
17723+ inode = lookup_wlock_by_ino(sb, bfound, h_ino);
17724+ try_iput = 1;
f0c0a007 17725+ }
1facf9fc 17726+
4a4d8108
AM
17727+ args.flags = a->flags[AuHn_CHILD];
17728+ args.dentry = dentry;
17729+ args.inode = inode;
17730+ args.h_inode = a->h_child_inode;
17731+ args.dir = a->dir;
17732+ args.h_dir = a->h_dir;
17733+ args.h_name = a->h_child_name;
17734+ args.h_nlen = a->h_child_nlen;
17735+ err = hn_job(&args);
17736+ if (dentry) {
027c5e7a 17737+ if (au_di(dentry))
4a4d8108
AM
17738+ di_write_unlock(dentry);
17739+ dput(dentry);
17740+ }
17741+ if (inode && try_iput) {
17742+ ii_write_unlock(inode);
17743+ iput(inode);
17744+ }
1facf9fc 17745+
4a4d8108
AM
17746+ ii_write_lock_parent(a->dir);
17747+ args.flags = a->flags[AuHn_PARENT];
17748+ args.dentry = NULL;
17749+ args.inode = a->dir;
17750+ args.h_inode = a->h_dir;
17751+ args.dir = NULL;
17752+ args.h_dir = NULL;
17753+ args.h_name = NULL;
17754+ args.h_nlen = 0;
17755+ err = hn_job(&args);
17756+ ii_write_unlock(a->dir);
1facf9fc 17757+
4f0767ce 17758+out:
4a4d8108
AM
17759+ iput(a->h_child_inode);
17760+ iput(a->h_dir);
17761+ iput(a->dir);
027c5e7a
AM
17762+ si_write_unlock(sb);
17763+ au_nwt_done(&sbinfo->si_nowait);
e49925d1 17764+ au_kfree_rcu(a);
dece6358 17765+}
1facf9fc 17766+
4a4d8108
AM
17767+/* ---------------------------------------------------------------------- */
17768+
17769+int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
17770+ struct qstr *h_child_qstr, struct inode *h_child_inode)
dece6358 17771+{
4a4d8108 17772+ int err, len;
53392da6 17773+ unsigned int flags[AuHnLast], f;
4a4d8108
AM
17774+ unsigned char isdir, isroot, wh;
17775+ struct inode *dir;
17776+ struct au_hnotify_args *args;
17777+ char *p, *h_child_name;
dece6358 17778+
1308ab2a 17779+ err = 0;
4a4d8108
AM
17780+ AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
17781+ dir = igrab(hnotify->hn_aufs_inode);
17782+ if (!dir)
17783+ goto out;
1facf9fc 17784+
4a4d8108
AM
17785+ isroot = (dir->i_ino == AUFS_ROOT_INO);
17786+ wh = 0;
17787+ h_child_name = (void *)h_child_qstr->name;
17788+ len = h_child_qstr->len;
17789+ if (h_child_name) {
17790+ if (len > AUFS_WH_PFX_LEN
17791+ && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
17792+ h_child_name += AUFS_WH_PFX_LEN;
17793+ len -= AUFS_WH_PFX_LEN;
17794+ wh = 1;
17795+ }
1facf9fc 17796+ }
dece6358 17797+
4a4d8108
AM
17798+ isdir = 0;
17799+ if (h_child_inode)
17800+ isdir = !!S_ISDIR(h_child_inode->i_mode);
17801+ flags[AuHn_PARENT] = AuHnJob_ISDIR;
17802+ flags[AuHn_CHILD] = 0;
17803+ if (isdir)
17804+ flags[AuHn_CHILD] = AuHnJob_ISDIR;
17805+ au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
17806+ au_fset_hnjob(flags[AuHn_CHILD], GEN);
17807+ switch (mask & FS_EVENTS_POSS_ON_CHILD) {
17808+ case FS_MOVED_FROM:
17809+ case FS_MOVED_TO:
17810+ au_fset_hnjob(flags[AuHn_CHILD], XINO0);
17811+ au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17812+ /*FALLTHROUGH*/
17813+ case FS_CREATE:
fb47a38f 17814+ AuDebugOn(!h_child_name);
4a4d8108 17815+ break;
1facf9fc 17816+
4a4d8108
AM
17817+ case FS_DELETE:
17818+ /*
17819+ * aufs never be able to get this child inode.
17820+ * revalidation should be in d_revalidate()
17821+ * by checking i_nlink, i_generation or d_unhashed().
17822+ */
17823+ AuDebugOn(!h_child_name);
17824+ au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
17825+ au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17826+ break;
dece6358 17827+
4a4d8108
AM
17828+ default:
17829+ AuDebugOn(1);
17830+ }
1308ab2a 17831+
4a4d8108
AM
17832+ if (wh)
17833+ h_child_inode = NULL;
1308ab2a 17834+
4a4d8108
AM
17835+ err = -ENOMEM;
17836+ /* iput() and kfree() will be called in au_hnotify() */
4a4d8108 17837+ args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
4a4d8108
AM
17838+ if (unlikely(!args)) {
17839+ AuErr1("no memory\n");
17840+ iput(dir);
17841+ goto out;
17842+ }
17843+ args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
17844+ args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
17845+ args->mask = mask;
17846+ args->dir = dir;
17847+ args->h_dir = igrab(h_dir);
17848+ if (h_child_inode)
17849+ h_child_inode = igrab(h_child_inode); /* can be NULL */
17850+ args->h_child_inode = h_child_inode;
17851+ args->h_child_nlen = len;
17852+ if (len) {
17853+ p = (void *)args;
17854+ p += sizeof(*args);
17855+ memcpy(p, h_child_name, len);
17856+ p[len] = 0;
1308ab2a 17857+ }
1308ab2a 17858+
38d290e6 17859+ /* NFS fires the event for silly-renamed one from kworker */
53392da6 17860+ f = 0;
38d290e6
JR
17861+ if (!dir->i_nlink
17862+ || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
53392da6
AM
17863+ f = AuWkq_NEST;
17864+ err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
4a4d8108
AM
17865+ if (unlikely(err)) {
17866+ pr_err("wkq %d\n", err);
17867+ iput(args->h_child_inode);
17868+ iput(args->h_dir);
17869+ iput(args->dir);
e49925d1 17870+ au_kfree_rcu(args);
1facf9fc 17871+ }
1facf9fc 17872+
4a4d8108 17873+out:
1facf9fc 17874+ return err;
17875+}
17876+
027c5e7a
AM
17877+/* ---------------------------------------------------------------------- */
17878+
17879+int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
17880+{
17881+ int err;
17882+
17883+ AuDebugOn(!(udba & AuOptMask_UDBA));
17884+
17885+ err = 0;
17886+ if (au_hnotify_op.reset_br)
17887+ err = au_hnotify_op.reset_br(udba, br, perm);
17888+
17889+ return err;
17890+}
17891+
17892+int au_hnotify_init_br(struct au_branch *br, int perm)
17893+{
17894+ int err;
17895+
17896+ err = 0;
17897+ if (au_hnotify_op.init_br)
17898+ err = au_hnotify_op.init_br(br, perm);
17899+
17900+ return err;
17901+}
17902+
17903+void au_hnotify_fin_br(struct au_branch *br)
17904+{
17905+ if (au_hnotify_op.fin_br)
17906+ au_hnotify_op.fin_br(br);
17907+}
17908+
4a4d8108
AM
17909+static void au_hn_destroy_cache(void)
17910+{
1c60b727
AM
17911+ kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
17912+ au_cache[AuCache_HNOTIFY] = NULL;
4a4d8108 17913+}
1308ab2a 17914+
4a4d8108 17915+int __init au_hnotify_init(void)
1facf9fc 17916+{
1308ab2a 17917+ int err;
1308ab2a 17918+
4a4d8108 17919+ err = -ENOMEM;
1c60b727
AM
17920+ au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
17921+ if (au_cache[AuCache_HNOTIFY]) {
027c5e7a
AM
17922+ err = 0;
17923+ if (au_hnotify_op.init)
17924+ err = au_hnotify_op.init();
4a4d8108
AM
17925+ if (unlikely(err))
17926+ au_hn_destroy_cache();
1308ab2a 17927+ }
1308ab2a 17928+ AuTraceErr(err);
4a4d8108 17929+ return err;
1308ab2a 17930+}
17931+
4a4d8108 17932+void au_hnotify_fin(void)
1308ab2a 17933+{
027c5e7a
AM
17934+ if (au_hnotify_op.fin)
17935+ au_hnotify_op.fin();
f0c0a007 17936+
4a4d8108 17937+ /* cf. au_cache_fin() */
1c60b727 17938+ if (au_cache[AuCache_HNOTIFY])
4a4d8108 17939+ au_hn_destroy_cache();
dece6358 17940+}
7f207e10
AM
17941diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
17942--- /usr/share/empty/fs/aufs/iinfo.c 1970-01-01 01:00:00.000000000 +0100
0b487229 17943+++ linux/fs/aufs/iinfo.c 2021-02-22 23:30:37.702680911 +0100
062440b3 17944@@ -0,0 +1,286 @@
cd7a4cd9 17945+// SPDX-License-Identifier: GPL-2.0
dece6358 17946+/*
0b487229 17947+ * Copyright (C) 2005-2020 Junjiro R. Okajima
dece6358
AM
17948+ *
17949+ * This program, aufs is free software; you can redistribute it and/or modify
17950+ * it under the terms of the GNU General Public License as published by
17951+ * the Free Software Foundation; either version 2 of the License, or
17952+ * (at your option) any later version.
17953+ *
17954+ * This program is distributed in the hope that it will be useful,
17955+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17956+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17957+ * GNU General Public License for more details.
17958+ *
17959+ * You should have received a copy of the GNU General Public License
523b37e3 17960+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358 17961+ */
1facf9fc 17962+
dece6358 17963+/*
4a4d8108 17964+ * inode private data
dece6358 17965+ */
1facf9fc 17966+
1308ab2a 17967+#include "aufs.h"
1facf9fc 17968+
4a4d8108 17969+struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 17970+{
4a4d8108 17971+ struct inode *h_inode;
5afbbe0d 17972+ struct au_hinode *hinode;
1facf9fc 17973+
4a4d8108 17974+ IiMustAnyLock(inode);
1facf9fc 17975+
5afbbe0d
AM
17976+ hinode = au_hinode(au_ii(inode), bindex);
17977+ h_inode = hinode->hi_inode;
4a4d8108
AM
17978+ AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
17979+ return h_inode;
17980+}
1facf9fc 17981+
4a4d8108
AM
17982+/* todo: hard/soft set? */
17983+void au_hiput(struct au_hinode *hinode)
17984+{
17985+ au_hn_free(hinode);
17986+ dput(hinode->hi_whdentry);
17987+ iput(hinode->hi_inode);
17988+}
1facf9fc 17989+
4a4d8108
AM
17990+unsigned int au_hi_flags(struct inode *inode, int isdir)
17991+{
17992+ unsigned int flags;
17993+ const unsigned int mnt_flags = au_mntflags(inode->i_sb);
1facf9fc 17994+
4a4d8108
AM
17995+ flags = 0;
17996+ if (au_opt_test(mnt_flags, XINO))
17997+ au_fset_hi(flags, XINO);
17998+ if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
17999+ au_fset_hi(flags, HNOTIFY);
18000+ return flags;
1facf9fc 18001+}
18002+
4a4d8108
AM
18003+void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
18004+ struct inode *h_inode, unsigned int flags)
1308ab2a 18005+{
4a4d8108
AM
18006+ struct au_hinode *hinode;
18007+ struct inode *hi;
18008+ struct au_iinfo *iinfo = au_ii(inode);
1facf9fc 18009+
4a4d8108 18010+ IiMustWriteLock(inode);
dece6358 18011+
5afbbe0d 18012+ hinode = au_hinode(iinfo, bindex);
4a4d8108
AM
18013+ hi = hinode->hi_inode;
18014+ AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
18015+
18016+ if (hi)
18017+ au_hiput(hinode);
18018+ hinode->hi_inode = h_inode;
18019+ if (h_inode) {
18020+ int err;
18021+ struct super_block *sb = inode->i_sb;
18022+ struct au_branch *br;
18023+
027c5e7a
AM
18024+ AuDebugOn(inode->i_mode
18025+ && (h_inode->i_mode & S_IFMT)
18026+ != (inode->i_mode & S_IFMT));
5afbbe0d 18027+ if (bindex == iinfo->ii_btop)
4a4d8108
AM
18028+ au_cpup_igen(inode, h_inode);
18029+ br = au_sbr(sb, bindex);
18030+ hinode->hi_id = br->br_id;
18031+ if (au_ftest_hi(flags, XINO)) {
18032+ err = au_xino_write(sb, bindex, h_inode->i_ino,
18033+ inode->i_ino);
18034+ if (unlikely(err))
18035+ AuIOErr1("failed au_xino_write() %d\n", err);
18036+ }
18037+
18038+ if (au_ftest_hi(flags, HNOTIFY)
18039+ && au_br_hnotifyable(br->br_perm)) {
027c5e7a 18040+ err = au_hn_alloc(hinode, inode);
4a4d8108
AM
18041+ if (unlikely(err))
18042+ AuIOErr1("au_hn_alloc() %d\n", err);
1308ab2a 18043+ }
18044+ }
4a4d8108 18045+}
dece6358 18046+
4a4d8108
AM
18047+void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
18048+ struct dentry *h_wh)
18049+{
18050+ struct au_hinode *hinode;
dece6358 18051+
4a4d8108
AM
18052+ IiMustWriteLock(inode);
18053+
5afbbe0d 18054+ hinode = au_hinode(au_ii(inode), bindex);
4a4d8108
AM
18055+ AuDebugOn(hinode->hi_whdentry);
18056+ hinode->hi_whdentry = h_wh;
1facf9fc 18057+}
18058+
537831f9 18059+void au_update_iigen(struct inode *inode, int half)
1308ab2a 18060+{
537831f9
AM
18061+ struct au_iinfo *iinfo;
18062+ struct au_iigen *iigen;
18063+ unsigned int sigen;
18064+
18065+ sigen = au_sigen(inode->i_sb);
18066+ iinfo = au_ii(inode);
18067+ iigen = &iinfo->ii_generation;
be52b249 18068+ spin_lock(&iigen->ig_spin);
537831f9
AM
18069+ iigen->ig_generation = sigen;
18070+ if (half)
18071+ au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
18072+ else
18073+ au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
be52b249 18074+ spin_unlock(&iigen->ig_spin);
4a4d8108 18075+}
1facf9fc 18076+
4a4d8108
AM
18077+/* it may be called at remount time, too */
18078+void au_update_ibrange(struct inode *inode, int do_put_zero)
18079+{
18080+ struct au_iinfo *iinfo;
5afbbe0d 18081+ aufs_bindex_t bindex, bbot;
1facf9fc 18082+
5afbbe0d 18083+ AuDebugOn(au_is_bad_inode(inode));
4a4d8108 18084+ IiMustWriteLock(inode);
1facf9fc 18085+
5afbbe0d
AM
18086+ iinfo = au_ii(inode);
18087+ if (do_put_zero && iinfo->ii_btop >= 0) {
18088+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
4a4d8108
AM
18089+ bindex++) {
18090+ struct inode *h_i;
1facf9fc 18091+
5afbbe0d 18092+ h_i = au_hinode(iinfo, bindex)->hi_inode;
38d290e6
JR
18093+ if (h_i
18094+ && !h_i->i_nlink
18095+ && !(h_i->i_state & I_LINKABLE))
027c5e7a
AM
18096+ au_set_h_iptr(inode, bindex, NULL, 0);
18097+ }
4a4d8108
AM
18098+ }
18099+
5afbbe0d
AM
18100+ iinfo->ii_btop = -1;
18101+ iinfo->ii_bbot = -1;
18102+ bbot = au_sbbot(inode->i_sb);
18103+ for (bindex = 0; bindex <= bbot; bindex++)
18104+ if (au_hinode(iinfo, bindex)->hi_inode) {
18105+ iinfo->ii_btop = bindex;
4a4d8108 18106+ break;
027c5e7a 18107+ }
5afbbe0d
AM
18108+ if (iinfo->ii_btop >= 0)
18109+ for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
18110+ if (au_hinode(iinfo, bindex)->hi_inode) {
18111+ iinfo->ii_bbot = bindex;
027c5e7a
AM
18112+ break;
18113+ }
5afbbe0d 18114+ AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
1308ab2a 18115+}
1facf9fc 18116+
dece6358 18117+/* ---------------------------------------------------------------------- */
1facf9fc 18118+
4a4d8108 18119+void au_icntnr_init_once(void *_c)
dece6358 18120+{
4a4d8108
AM
18121+ struct au_icntnr *c = _c;
18122+ struct au_iinfo *iinfo = &c->iinfo;
1facf9fc 18123+
be52b249 18124+ spin_lock_init(&iinfo->ii_generation.ig_spin);
4a4d8108
AM
18125+ au_rw_init(&iinfo->ii_rwsem);
18126+ inode_init_once(&c->vfs_inode);
18127+}
1facf9fc 18128+
5afbbe0d
AM
18129+void au_hinode_init(struct au_hinode *hinode)
18130+{
18131+ hinode->hi_inode = NULL;
18132+ hinode->hi_id = -1;
18133+ au_hn_init(hinode);
18134+ hinode->hi_whdentry = NULL;
18135+}
18136+
4a4d8108
AM
18137+int au_iinfo_init(struct inode *inode)
18138+{
18139+ struct au_iinfo *iinfo;
18140+ struct super_block *sb;
5afbbe0d 18141+ struct au_hinode *hi;
4a4d8108 18142+ int nbr, i;
1facf9fc 18143+
4a4d8108
AM
18144+ sb = inode->i_sb;
18145+ iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
5afbbe0d 18146+ nbr = au_sbbot(sb) + 1;
4a4d8108
AM
18147+ if (unlikely(nbr <= 0))
18148+ nbr = 1;
5afbbe0d
AM
18149+ hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
18150+ if (hi) {
acd2b654 18151+ au_lcnt_inc(&au_sbi(sb)->si_ninodes);
5afbbe0d
AM
18152+
18153+ iinfo->ii_hinode = hi;
18154+ for (i = 0; i < nbr; i++, hi++)
18155+ au_hinode_init(hi);
1facf9fc 18156+
537831f9 18157+ iinfo->ii_generation.ig_generation = au_sigen(sb);
5afbbe0d
AM
18158+ iinfo->ii_btop = -1;
18159+ iinfo->ii_bbot = -1;
4a4d8108
AM
18160+ iinfo->ii_vdir = NULL;
18161+ return 0;
1308ab2a 18162+ }
4a4d8108
AM
18163+ return -ENOMEM;
18164+}
1facf9fc 18165+
e2f27e51 18166+int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
4a4d8108 18167+{
5afbbe0d 18168+ int err, i;
4a4d8108 18169+ struct au_hinode *hip;
1facf9fc 18170+
4a4d8108
AM
18171+ AuRwMustWriteLock(&iinfo->ii_rwsem);
18172+
18173+ err = -ENOMEM;
e2f27e51
AM
18174+ hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
18175+ may_shrink);
4a4d8108
AM
18176+ if (hip) {
18177+ iinfo->ii_hinode = hip;
5afbbe0d
AM
18178+ i = iinfo->ii_bbot + 1;
18179+ hip += i;
18180+ for (; i < nbr; i++, hip++)
18181+ au_hinode_init(hip);
4a4d8108 18182+ err = 0;
1308ab2a 18183+ }
4a4d8108 18184+
1308ab2a 18185+ return err;
1facf9fc 18186+}
18187+
4a4d8108 18188+void au_iinfo_fin(struct inode *inode)
1facf9fc 18189+{
4a4d8108
AM
18190+ struct au_iinfo *iinfo;
18191+ struct au_hinode *hi;
18192+ struct super_block *sb;
5afbbe0d 18193+ aufs_bindex_t bindex, bbot;
b752ccd1 18194+ const unsigned char unlinked = !inode->i_nlink;
1308ab2a 18195+
5afbbe0d 18196+ AuDebugOn(au_is_bad_inode(inode));
1308ab2a 18197+
b752ccd1 18198+ sb = inode->i_sb;
acd2b654 18199+ au_lcnt_dec(&au_sbi(sb)->si_ninodes);
b752ccd1
AM
18200+ if (si_pid_test(sb))
18201+ au_xino_delete_inode(inode, unlinked);
18202+ else {
18203+ /*
18204+ * it is safe to hide the dependency between sbinfo and
18205+ * sb->s_umount.
18206+ */
18207+ lockdep_off();
18208+ si_noflush_read_lock(sb);
18209+ au_xino_delete_inode(inode, unlinked);
18210+ si_read_unlock(sb);
18211+ lockdep_on();
18212+ }
18213+
5afbbe0d 18214+ iinfo = au_ii(inode);
4a4d8108 18215+ if (iinfo->ii_vdir)
1c60b727 18216+ au_vdir_free(iinfo->ii_vdir);
1308ab2a 18217+
5afbbe0d 18218+ bindex = iinfo->ii_btop;
b752ccd1 18219+ if (bindex >= 0) {
5afbbe0d
AM
18220+ hi = au_hinode(iinfo, bindex);
18221+ bbot = iinfo->ii_bbot;
18222+ while (bindex++ <= bbot) {
b752ccd1 18223+ if (hi->hi_inode)
4a4d8108 18224+ au_hiput(hi);
4a4d8108
AM
18225+ hi++;
18226+ }
18227+ }
e49925d1 18228+ au_kfree_rcu(iinfo->ii_hinode);
4a4d8108 18229+ AuRwDestroy(&iinfo->ii_rwsem);
dece6358 18230+}
7f207e10
AM
18231diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
18232--- /usr/share/empty/fs/aufs/inode.c 1970-01-01 01:00:00.000000000 +0100
0b487229
JR
18233+++ linux/fs/aufs/inode.c 2021-02-22 23:30:37.702680911 +0100
18234@@ -0,0 +1,529 @@
cd7a4cd9 18235+// SPDX-License-Identifier: GPL-2.0
4a4d8108 18236+/*
0b487229 18237+ * Copyright (C) 2005-2020 Junjiro R. Okajima
4a4d8108
AM
18238+ *
18239+ * This program, aufs is free software; you can redistribute it and/or modify
18240+ * it under the terms of the GNU General Public License as published by
18241+ * the Free Software Foundation; either version 2 of the License, or
18242+ * (at your option) any later version.
18243+ *
18244+ * This program is distributed in the hope that it will be useful,
18245+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18246+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18247+ * GNU General Public License for more details.
18248+ *
18249+ * You should have received a copy of the GNU General Public License
523b37e3 18250+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 18251+ */
1facf9fc 18252+
4a4d8108
AM
18253+/*
18254+ * inode functions
18255+ */
1facf9fc 18256+
0b487229 18257+#include <linux/iversion.h>
4a4d8108 18258+#include "aufs.h"
1308ab2a 18259+
4a4d8108
AM
18260+struct inode *au_igrab(struct inode *inode)
18261+{
18262+ if (inode) {
18263+ AuDebugOn(!atomic_read(&inode->i_count));
027c5e7a 18264+ ihold(inode);
1facf9fc 18265+ }
4a4d8108
AM
18266+ return inode;
18267+}
1facf9fc 18268+
4a4d8108
AM
18269+static void au_refresh_hinode_attr(struct inode *inode, int do_version)
18270+{
18271+ au_cpup_attr_all(inode, /*force*/0);
537831f9 18272+ au_update_iigen(inode, /*half*/1);
4a4d8108 18273+ if (do_version)
be118d29 18274+ inode_inc_iversion(inode);
dece6358 18275+}
1facf9fc 18276+
027c5e7a 18277+static int au_ii_refresh(struct inode *inode, int *update)
dece6358 18278+{
e2f27e51 18279+ int err, e, nbr;
027c5e7a 18280+ umode_t type;
4a4d8108 18281+ aufs_bindex_t bindex, new_bindex;
1308ab2a 18282+ struct super_block *sb;
4a4d8108 18283+ struct au_iinfo *iinfo;
027c5e7a 18284+ struct au_hinode *p, *q, tmp;
1facf9fc 18285+
5afbbe0d 18286+ AuDebugOn(au_is_bad_inode(inode));
4a4d8108 18287+ IiMustWriteLock(inode);
1facf9fc 18288+
027c5e7a 18289+ *update = 0;
4a4d8108 18290+ sb = inode->i_sb;
e2f27e51 18291+ nbr = au_sbbot(sb) + 1;
027c5e7a 18292+ type = inode->i_mode & S_IFMT;
4a4d8108 18293+ iinfo = au_ii(inode);
e2f27e51 18294+ err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
4a4d8108 18295+ if (unlikely(err))
1308ab2a 18296+ goto out;
1facf9fc 18297+
5afbbe0d
AM
18298+ AuDebugOn(iinfo->ii_btop < 0);
18299+ p = au_hinode(iinfo, iinfo->ii_btop);
18300+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
4a4d8108
AM
18301+ bindex++, p++) {
18302+ if (!p->hi_inode)
18303+ continue;
1facf9fc 18304+
027c5e7a 18305+ AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
4a4d8108
AM
18306+ new_bindex = au_br_index(sb, p->hi_id);
18307+ if (new_bindex == bindex)
18308+ continue;
1facf9fc 18309+
4a4d8108 18310+ if (new_bindex < 0) {
027c5e7a 18311+ *update = 1;
4a4d8108
AM
18312+ au_hiput(p);
18313+ p->hi_inode = NULL;
18314+ continue;
1308ab2a 18315+ }
4a4d8108 18316+
5afbbe0d
AM
18317+ if (new_bindex < iinfo->ii_btop)
18318+ iinfo->ii_btop = new_bindex;
18319+ if (iinfo->ii_bbot < new_bindex)
18320+ iinfo->ii_bbot = new_bindex;
4a4d8108 18321+ /* swap two lower inode, and loop again */
5afbbe0d 18322+ q = au_hinode(iinfo, new_bindex);
4a4d8108
AM
18323+ tmp = *q;
18324+ *q = *p;
18325+ *p = tmp;
18326+ if (tmp.hi_inode) {
18327+ bindex--;
18328+ p--;
1308ab2a 18329+ }
18330+ }
4a4d8108 18331+ au_update_ibrange(inode, /*do_put_zero*/0);
e2f27e51 18332+ au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
4a4d8108
AM
18333+ e = au_dy_irefresh(inode);
18334+ if (unlikely(e && !err))
18335+ err = e;
1facf9fc 18336+
4f0767ce 18337+out:
027c5e7a
AM
18338+ AuTraceErr(err);
18339+ return err;
18340+}
18341+
b95c5147
AM
18342+void au_refresh_iop(struct inode *inode, int force_getattr)
18343+{
18344+ int type;
18345+ struct au_sbinfo *sbi = au_sbi(inode->i_sb);
18346+ const struct inode_operations *iop
18347+ = force_getattr ? aufs_iop : sbi->si_iop_array;
18348+
18349+ if (inode->i_op == iop)
18350+ return;
18351+
18352+ switch (inode->i_mode & S_IFMT) {
18353+ case S_IFDIR:
18354+ type = AuIop_DIR;
18355+ break;
18356+ case S_IFLNK:
18357+ type = AuIop_SYMLINK;
18358+ break;
18359+ default:
18360+ type = AuIop_OTHER;
18361+ break;
18362+ }
18363+
18364+ inode->i_op = iop + type;
18365+ /* unnecessary smp_wmb() */
18366+}
18367+
027c5e7a
AM
18368+int au_refresh_hinode_self(struct inode *inode)
18369+{
18370+ int err, update;
18371+
18372+ err = au_ii_refresh(inode, &update);
18373+ if (!err)
18374+ au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
18375+
18376+ AuTraceErr(err);
4a4d8108
AM
18377+ return err;
18378+}
1facf9fc 18379+
4a4d8108
AM
18380+int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
18381+{
027c5e7a 18382+ int err, e, update;
4a4d8108 18383+ unsigned int flags;
027c5e7a 18384+ umode_t mode;
5afbbe0d 18385+ aufs_bindex_t bindex, bbot;
027c5e7a 18386+ unsigned char isdir;
4a4d8108
AM
18387+ struct au_hinode *p;
18388+ struct au_iinfo *iinfo;
1facf9fc 18389+
027c5e7a 18390+ err = au_ii_refresh(inode, &update);
4a4d8108
AM
18391+ if (unlikely(err))
18392+ goto out;
18393+
18394+ update = 0;
18395+ iinfo = au_ii(inode);
5afbbe0d 18396+ p = au_hinode(iinfo, iinfo->ii_btop);
027c5e7a
AM
18397+ mode = (inode->i_mode & S_IFMT);
18398+ isdir = S_ISDIR(mode);
4a4d8108 18399+ flags = au_hi_flags(inode, isdir);
5afbbe0d
AM
18400+ bbot = au_dbbot(dentry);
18401+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
5527c038 18402+ struct inode *h_i, *h_inode;
4a4d8108
AM
18403+ struct dentry *h_d;
18404+
18405+ h_d = au_h_dptr(dentry, bindex);
5527c038 18406+ if (!h_d || d_is_negative(h_d))
4a4d8108
AM
18407+ continue;
18408+
5527c038
JR
18409+ h_inode = d_inode(h_d);
18410+ AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
5afbbe0d 18411+ if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
4a4d8108
AM
18412+ h_i = au_h_iptr(inode, bindex);
18413+ if (h_i) {
5527c038 18414+ if (h_i == h_inode)
4a4d8108
AM
18415+ continue;
18416+ err = -EIO;
18417+ break;
18418+ }
18419+ }
5afbbe0d
AM
18420+ if (bindex < iinfo->ii_btop)
18421+ iinfo->ii_btop = bindex;
18422+ if (iinfo->ii_bbot < bindex)
18423+ iinfo->ii_bbot = bindex;
5527c038 18424+ au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
4a4d8108 18425+ update = 1;
1308ab2a 18426+ }
4a4d8108
AM
18427+ au_update_ibrange(inode, /*do_put_zero*/0);
18428+ e = au_dy_irefresh(inode);
18429+ if (unlikely(e && !err))
18430+ err = e;
027c5e7a
AM
18431+ if (!err)
18432+ au_refresh_hinode_attr(inode, update && isdir);
4a4d8108 18433+
4f0767ce 18434+out:
4a4d8108 18435+ AuTraceErr(err);
1308ab2a 18436+ return err;
dece6358
AM
18437+}
18438+
4a4d8108 18439+static int set_inode(struct inode *inode, struct dentry *dentry)
dece6358 18440+{
4a4d8108
AM
18441+ int err;
18442+ unsigned int flags;
18443+ umode_t mode;
5afbbe0d 18444+ aufs_bindex_t bindex, btop, btail;
4a4d8108
AM
18445+ unsigned char isdir;
18446+ struct dentry *h_dentry;
18447+ struct inode *h_inode;
18448+ struct au_iinfo *iinfo;
0b487229 18449+ const struct inode_operations *iop;
dece6358 18450+
4a4d8108 18451+ IiMustWriteLock(inode);
dece6358 18452+
4a4d8108
AM
18453+ err = 0;
18454+ isdir = 0;
b95c5147 18455+ iop = au_sbi(inode->i_sb)->si_iop_array;
5afbbe0d
AM
18456+ btop = au_dbtop(dentry);
18457+ h_dentry = au_h_dptr(dentry, btop);
5527c038 18458+ h_inode = d_inode(h_dentry);
4a4d8108
AM
18459+ mode = h_inode->i_mode;
18460+ switch (mode & S_IFMT) {
18461+ case S_IFREG:
18462+ btail = au_dbtail(dentry);
b95c5147 18463+ inode->i_op = iop + AuIop_OTHER;
4a4d8108 18464+ inode->i_fop = &aufs_file_fop;
5afbbe0d 18465+ err = au_dy_iaop(inode, btop, h_inode);
4a4d8108
AM
18466+ if (unlikely(err))
18467+ goto out;
18468+ break;
18469+ case S_IFDIR:
18470+ isdir = 1;
18471+ btail = au_dbtaildir(dentry);
b95c5147 18472+ inode->i_op = iop + AuIop_DIR;
4a4d8108
AM
18473+ inode->i_fop = &aufs_dir_fop;
18474+ break;
18475+ case S_IFLNK:
18476+ btail = au_dbtail(dentry);
b95c5147 18477+ inode->i_op = iop + AuIop_SYMLINK;
4a4d8108
AM
18478+ break;
18479+ case S_IFBLK:
18480+ case S_IFCHR:
18481+ case S_IFIFO:
18482+ case S_IFSOCK:
18483+ btail = au_dbtail(dentry);
b95c5147 18484+ inode->i_op = iop + AuIop_OTHER;
38d290e6 18485+ init_special_inode(inode, mode, h_inode->i_rdev);
4a4d8108
AM
18486+ break;
18487+ default:
18488+ AuIOErr("Unknown file type 0%o\n", mode);
18489+ err = -EIO;
1308ab2a 18490+ goto out;
4a4d8108 18491+ }
dece6358 18492+
4a4d8108
AM
18493+ /* do not set hnotify for whiteouted dirs (SHWH mode) */
18494+ flags = au_hi_flags(inode, isdir);
18495+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
18496+ && au_ftest_hi(flags, HNOTIFY)
18497+ && dentry->d_name.len > AUFS_WH_PFX_LEN
18498+ && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
18499+ au_fclr_hi(flags, HNOTIFY);
18500+ iinfo = au_ii(inode);
5afbbe0d
AM
18501+ iinfo->ii_btop = btop;
18502+ iinfo->ii_bbot = btail;
18503+ for (bindex = btop; bindex <= btail; bindex++) {
4a4d8108
AM
18504+ h_dentry = au_h_dptr(dentry, bindex);
18505+ if (h_dentry)
18506+ au_set_h_iptr(inode, bindex,
5527c038 18507+ au_igrab(d_inode(h_dentry)), flags);
4a4d8108
AM
18508+ }
18509+ au_cpup_attr_all(inode, /*force*/1);
c1595e42
JR
18510+ /*
18511+ * to force calling aufs_get_acl() every time,
18512+ * do not call cache_no_acl() for aufs inode.
18513+ */
dece6358 18514+
4f0767ce 18515+out:
4a4d8108
AM
18516+ return err;
18517+}
dece6358 18518+
027c5e7a
AM
18519+/*
18520+ * successful returns with iinfo write_locked
18521+ * minus: errno
18522+ * zero: success, matched
18523+ * plus: no error, but unmatched
18524+ */
18525+static int reval_inode(struct inode *inode, struct dentry *dentry)
4a4d8108
AM
18526+{
18527+ int err;
cfc41e69 18528+ unsigned int gen, igflags;
5afbbe0d 18529+ aufs_bindex_t bindex, bbot;
4a4d8108 18530+ struct inode *h_inode, *h_dinode;
5527c038 18531+ struct dentry *h_dentry;
dece6358 18532+
4a4d8108
AM
18533+ /*
18534+ * before this function, if aufs got any iinfo lock, it must be only
18535+ * one, the parent dir.
18536+ * it can happen by UDBA and the obsoleted inode number.
18537+ */
18538+ err = -EIO;
18539+ if (unlikely(inode->i_ino == parent_ino(dentry)))
18540+ goto out;
18541+
027c5e7a 18542+ err = 1;
4a4d8108 18543+ ii_write_lock_new_child(inode);
5afbbe0d 18544+ h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
5527c038 18545+ h_dinode = d_inode(h_dentry);
5afbbe0d
AM
18546+ bbot = au_ibbot(inode);
18547+ for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
4a4d8108 18548+ h_inode = au_h_iptr(inode, bindex);
537831f9
AM
18549+ if (!h_inode || h_inode != h_dinode)
18550+ continue;
18551+
18552+ err = 0;
cfc41e69 18553+ gen = au_iigen(inode, &igflags);
537831f9 18554+ if (gen == au_digen(dentry)
cfc41e69 18555+ && !au_ig_ftest(igflags, HALF_REFRESHED))
4a4d8108 18556+ break;
537831f9
AM
18557+
18558+ /* fully refresh inode using dentry */
18559+ err = au_refresh_hinode(inode, dentry);
18560+ if (!err)
18561+ au_update_iigen(inode, /*half*/0);
18562+ break;
1facf9fc 18563+ }
dece6358 18564+
4a4d8108
AM
18565+ if (unlikely(err))
18566+ ii_write_unlock(inode);
4f0767ce 18567+out:
1facf9fc 18568+ return err;
18569+}
1facf9fc 18570+
4a4d8108
AM
18571+int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18572+ unsigned int d_type, ino_t *ino)
1facf9fc 18573+{
521ced18
JR
18574+ int err, idx;
18575+ const int isnondir = d_type != DT_DIR;
1facf9fc 18576+
b752ccd1 18577+ /* prevent hardlinked inode number from race condition */
521ced18
JR
18578+ if (isnondir) {
18579+ err = au_xinondir_enter(sb, bindex, h_ino, &idx);
18580+ if (unlikely(err))
18581+ goto out;
4a4d8108 18582+ }
521ced18 18583+
4a4d8108
AM
18584+ err = au_xino_read(sb, bindex, h_ino, ino);
18585+ if (unlikely(err))
521ced18 18586+ goto out_xinondir;
1308ab2a 18587+
4a4d8108
AM
18588+ if (!*ino) {
18589+ err = -EIO;
18590+ *ino = au_xino_new_ino(sb);
18591+ if (unlikely(!*ino))
521ced18 18592+ goto out_xinondir;
4a4d8108
AM
18593+ err = au_xino_write(sb, bindex, h_ino, *ino);
18594+ if (unlikely(err))
521ced18 18595+ goto out_xinondir;
1308ab2a 18596+ }
1facf9fc 18597+
521ced18
JR
18598+out_xinondir:
18599+ if (isnondir && idx >= 0)
18600+ au_xinondir_leave(sb, bindex, h_ino, idx);
4f0767ce 18601+out:
1facf9fc 18602+ return err;
18603+}
18604+
4a4d8108
AM
18605+/* successful returns with iinfo write_locked */
18606+/* todo: return with unlocked? */
18607+struct inode *au_new_inode(struct dentry *dentry, int must_new)
1facf9fc 18608+{
5527c038 18609+ struct inode *inode, *h_inode;
4a4d8108
AM
18610+ struct dentry *h_dentry;
18611+ struct super_block *sb;
18612+ ino_t h_ino, ino;
521ced18 18613+ int err, idx, hlinked;
5afbbe0d 18614+ aufs_bindex_t btop;
1facf9fc 18615+
4a4d8108 18616+ sb = dentry->d_sb;
5afbbe0d
AM
18617+ btop = au_dbtop(dentry);
18618+ h_dentry = au_h_dptr(dentry, btop);
5527c038
JR
18619+ h_inode = d_inode(h_dentry);
18620+ h_ino = h_inode->i_ino;
521ced18 18621+ hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
b752ccd1 18622+
521ced18 18623+new_ino:
b752ccd1
AM
18624+ /*
18625+ * stop 'race'-ing between hardlinks under different
18626+ * parents.
18627+ */
521ced18
JR
18628+ if (hlinked) {
18629+ err = au_xinondir_enter(sb, btop, h_ino, &idx);
18630+ inode = ERR_PTR(err);
18631+ if (unlikely(err))
18632+ goto out;
18633+ }
b752ccd1 18634+
5afbbe0d 18635+ err = au_xino_read(sb, btop, h_ino, &ino);
4a4d8108
AM
18636+ inode = ERR_PTR(err);
18637+ if (unlikely(err))
521ced18 18638+ goto out_xinondir;
b752ccd1 18639+
4a4d8108
AM
18640+ if (!ino) {
18641+ ino = au_xino_new_ino(sb);
18642+ if (unlikely(!ino)) {
18643+ inode = ERR_PTR(-EIO);
521ced18 18644+ goto out_xinondir;
dece6358
AM
18645+ }
18646+ }
1facf9fc 18647+
4a4d8108
AM
18648+ AuDbg("i%lu\n", (unsigned long)ino);
18649+ inode = au_iget_locked(sb, ino);
18650+ err = PTR_ERR(inode);
18651+ if (IS_ERR(inode))
521ced18 18652+ goto out_xinondir;
1facf9fc 18653+
4a4d8108
AM
18654+ AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
18655+ if (inode->i_state & I_NEW) {
18656+ ii_write_lock_new_child(inode);
18657+ err = set_inode(inode, dentry);
18658+ if (!err) {
18659+ unlock_new_inode(inode);
521ced18 18660+ goto out_xinondir; /* success */
4a4d8108 18661+ }
1308ab2a 18662+
027c5e7a
AM
18663+ /*
18664+ * iget_failed() calls iput(), but we need to call
18665+ * ii_write_unlock() after iget_failed(). so dirty hack for
18666+ * i_count.
18667+ */
18668+ atomic_inc(&inode->i_count);
4a4d8108 18669+ iget_failed(inode);
027c5e7a 18670+ ii_write_unlock(inode);
5afbbe0d 18671+ au_xino_write(sb, btop, h_ino, /*ino*/0);
027c5e7a
AM
18672+ /* ignore this error */
18673+ goto out_iput;
18674+ } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
b752ccd1
AM
18675+ /*
18676+ * horrible race condition between lookup, readdir and copyup
18677+ * (or something).
18678+ */
521ced18
JR
18679+ if (hlinked && idx >= 0)
18680+ au_xinondir_leave(sb, btop, h_ino, idx);
027c5e7a
AM
18681+ err = reval_inode(inode, dentry);
18682+ if (unlikely(err < 0)) {
521ced18 18683+ hlinked = 0;
027c5e7a
AM
18684+ goto out_iput;
18685+ }
521ced18 18686+ if (!err)
4a4d8108 18687+ goto out; /* success */
521ced18
JR
18688+ else if (hlinked && idx >= 0) {
18689+ err = au_xinondir_enter(sb, btop, h_ino, &idx);
18690+ if (unlikely(err)) {
18691+ iput(inode);
18692+ inode = ERR_PTR(err);
18693+ goto out;
18694+ }
18695+ }
4a4d8108
AM
18696+ }
18697+
5527c038 18698+ if (unlikely(au_test_fs_unique_ino(h_inode)))
4a4d8108 18699+ AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
523b37e3 18700+ " b%d, %s, %pd, hi%lu, i%lu.\n",
5afbbe0d 18701+ btop, au_sbtype(h_dentry->d_sb), dentry,
4a4d8108
AM
18702+ (unsigned long)h_ino, (unsigned long)ino);
18703+ ino = 0;
5afbbe0d 18704+ err = au_xino_write(sb, btop, h_ino, /*ino*/0);
4a4d8108
AM
18705+ if (!err) {
18706+ iput(inode);
521ced18
JR
18707+ if (hlinked && idx >= 0)
18708+ au_xinondir_leave(sb, btop, h_ino, idx);
4a4d8108
AM
18709+ goto new_ino;
18710+ }
1308ab2a 18711+
4f0767ce 18712+out_iput:
4a4d8108 18713+ iput(inode);
4a4d8108 18714+ inode = ERR_PTR(err);
521ced18
JR
18715+out_xinondir:
18716+ if (hlinked && idx >= 0)
18717+ au_xinondir_leave(sb, btop, h_ino, idx);
4f0767ce 18718+out:
4a4d8108 18719+ return inode;
1facf9fc 18720+}
18721+
4a4d8108 18722+/* ---------------------------------------------------------------------- */
1facf9fc 18723+
4a4d8108
AM
18724+int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18725+ struct inode *inode)
18726+{
18727+ int err;
076b876e 18728+ struct inode *hi;
1facf9fc 18729+
4a4d8108 18730+ err = au_br_rdonly(au_sbr(sb, bindex));
1facf9fc 18731+
4a4d8108
AM
18732+ /* pseudo-link after flushed may happen out of bounds */
18733+ if (!err
18734+ && inode
5afbbe0d
AM
18735+ && au_ibtop(inode) <= bindex
18736+ && bindex <= au_ibbot(inode)) {
4a4d8108
AM
18737+ /*
18738+ * permission check is unnecessary since vfsub routine
18739+ * will be called later
18740+ */
076b876e 18741+ hi = au_h_iptr(inode, bindex);
4a4d8108
AM
18742+ if (hi)
18743+ err = IS_IMMUTABLE(hi) ? -EROFS : 0;
1facf9fc 18744+ }
18745+
4a4d8108
AM
18746+ return err;
18747+}
dece6358 18748+
4a4d8108
AM
18749+int au_test_h_perm(struct inode *h_inode, int mask)
18750+{
2dfbb274 18751+ if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
4a4d8108
AM
18752+ return 0;
18753+ return inode_permission(h_inode, mask);
18754+}
1facf9fc 18755+
4a4d8108
AM
18756+int au_test_h_perm_sio(struct inode *h_inode, int mask)
18757+{
18758+ if (au_test_nfs(h_inode->i_sb)
18759+ && (mask & MAY_WRITE)
18760+ && S_ISDIR(h_inode->i_mode))
18761+ mask |= MAY_READ; /* force permission check */
18762+ return au_test_h_perm(h_inode, mask);
1facf9fc 18763+}
7f207e10
AM
18764diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
18765--- /usr/share/empty/fs/aufs/inode.h 1970-01-01 01:00:00.000000000 +0100
0b487229 18766+++ linux/fs/aufs/inode.h 2021-02-22 23:30:37.702680911 +0100
e49925d1 18767@@ -0,0 +1,698 @@
062440b3 18768+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 18769+/*
0b487229 18770+ * Copyright (C) 2005-2020 Junjiro R. Okajima
4a4d8108
AM
18771+ *
18772+ * This program, aufs is free software; you can redistribute it and/or modify
18773+ * it under the terms of the GNU General Public License as published by
18774+ * the Free Software Foundation; either version 2 of the License, or
18775+ * (at your option) any later version.
18776+ *
18777+ * This program is distributed in the hope that it will be useful,
18778+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18779+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18780+ * GNU General Public License for more details.
18781+ *
18782+ * You should have received a copy of the GNU General Public License
523b37e3 18783+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 18784+ */
1facf9fc 18785+
1308ab2a 18786+/*
4a4d8108 18787+ * inode operations
1308ab2a 18788+ */
dece6358 18789+
4a4d8108
AM
18790+#ifndef __AUFS_INODE_H__
18791+#define __AUFS_INODE_H__
dece6358 18792+
4a4d8108 18793+#ifdef __KERNEL__
1308ab2a 18794+
4a4d8108 18795+#include <linux/fsnotify.h>
4a4d8108 18796+#include "rwsem.h"
1308ab2a 18797+
4a4d8108 18798+struct vfsmount;
1facf9fc 18799+
4a4d8108
AM
18800+struct au_hnotify {
18801+#ifdef CONFIG_AUFS_HNOTIFY
18802+#ifdef CONFIG_AUFS_HFSNOTIFY
7f207e10 18803+ /* never use fsnotify_add_vfsmount_mark() */
0c5527e5 18804+ struct fsnotify_mark hn_mark;
4a4d8108 18805+#endif
1c60b727 18806+ struct inode *hn_aufs_inode; /* no get/put */
e49925d1 18807+ struct rcu_head rcu;
4a4d8108
AM
18808+#endif
18809+} ____cacheline_aligned_in_smp;
1facf9fc 18810+
4a4d8108
AM
18811+struct au_hinode {
18812+ struct inode *hi_inode;
18813+ aufs_bindex_t hi_id;
18814+#ifdef CONFIG_AUFS_HNOTIFY
18815+ struct au_hnotify *hi_notify;
18816+#endif
dece6358 18817+
4a4d8108
AM
18818+ /* reference to the copied-up whiteout with get/put */
18819+ struct dentry *hi_whdentry;
18820+};
dece6358 18821+
537831f9
AM
18822+/* ig_flags */
18823+#define AuIG_HALF_REFRESHED 1
18824+#define au_ig_ftest(flags, name) ((flags) & AuIG_##name)
18825+#define au_ig_fset(flags, name) \
18826+ do { (flags) |= AuIG_##name; } while (0)
18827+#define au_ig_fclr(flags, name) \
18828+ do { (flags) &= ~AuIG_##name; } while (0)
18829+
18830+struct au_iigen {
be52b249 18831+ spinlock_t ig_spin;
537831f9
AM
18832+ __u32 ig_generation, ig_flags;
18833+};
18834+
4a4d8108
AM
18835+struct au_vdir;
18836+struct au_iinfo {
7a9e40b8 18837+ struct au_iigen ii_generation;
4a4d8108 18838+ struct super_block *ii_hsb1; /* no get/put */
1facf9fc 18839+
4a4d8108 18840+ struct au_rwsem ii_rwsem;
5afbbe0d 18841+ aufs_bindex_t ii_btop, ii_bbot;
4a4d8108
AM
18842+ __u32 ii_higen;
18843+ struct au_hinode *ii_hinode;
18844+ struct au_vdir *ii_vdir;
18845+};
1facf9fc 18846+
4a4d8108 18847+struct au_icntnr {
e49925d1
JR
18848+ struct au_iinfo iinfo;
18849+ struct inode vfs_inode;
18850+ struct hlist_bl_node plink;
18851+ struct rcu_head rcu;
4a4d8108 18852+} ____cacheline_aligned_in_smp;
1308ab2a 18853+
4a4d8108
AM
18854+/* au_pin flags */
18855+#define AuPin_DI_LOCKED 1
18856+#define AuPin_MNT_WRITE (1 << 1)
18857+#define au_ftest_pin(flags, name) ((flags) & AuPin_##name)
7f207e10
AM
18858+#define au_fset_pin(flags, name) \
18859+ do { (flags) |= AuPin_##name; } while (0)
18860+#define au_fclr_pin(flags, name) \
18861+ do { (flags) &= ~AuPin_##name; } while (0)
4a4d8108
AM
18862+
18863+struct au_pin {
18864+ /* input */
18865+ struct dentry *dentry;
18866+ unsigned int udba;
18867+ unsigned char lsc_di, lsc_hi, flags;
18868+ aufs_bindex_t bindex;
18869+
18870+ /* output */
18871+ struct dentry *parent;
18872+ struct au_hinode *hdir;
18873+ struct vfsmount *h_mnt;
86dc4139
AM
18874+
18875+ /* temporary unlock/relock for copyup */
18876+ struct dentry *h_dentry, *h_parent;
18877+ struct au_branch *br;
18878+ struct task_struct *task;
4a4d8108 18879+};
1facf9fc 18880+
86dc4139 18881+void au_pin_hdir_unlock(struct au_pin *p);
c1595e42 18882+int au_pin_hdir_lock(struct au_pin *p);
86dc4139 18883+int au_pin_hdir_relock(struct au_pin *p);
86dc4139
AM
18884+void au_pin_hdir_acquire_nest(struct au_pin *p);
18885+void au_pin_hdir_release(struct au_pin *p);
18886+
1308ab2a 18887+/* ---------------------------------------------------------------------- */
18888+
4a4d8108 18889+static inline struct au_iinfo *au_ii(struct inode *inode)
1facf9fc 18890+{
5afbbe0d
AM
18891+ BUG_ON(is_bad_inode(inode));
18892+ return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
4a4d8108 18893+}
1facf9fc 18894+
4a4d8108 18895+/* ---------------------------------------------------------------------- */
1facf9fc 18896+
4a4d8108
AM
18897+/* inode.c */
18898+struct inode *au_igrab(struct inode *inode);
b95c5147 18899+void au_refresh_iop(struct inode *inode, int force_getattr);
027c5e7a 18900+int au_refresh_hinode_self(struct inode *inode);
4a4d8108
AM
18901+int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
18902+int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18903+ unsigned int d_type, ino_t *ino);
18904+struct inode *au_new_inode(struct dentry *dentry, int must_new);
18905+int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18906+ struct inode *inode);
18907+int au_test_h_perm(struct inode *h_inode, int mask);
18908+int au_test_h_perm_sio(struct inode *h_inode, int mask);
1facf9fc 18909+
4a4d8108
AM
18910+static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
18911+ ino_t h_ino, unsigned int d_type, ino_t *ino)
18912+{
18913+#ifdef CONFIG_AUFS_SHWH
18914+ return au_ino(sb, bindex, h_ino, d_type, ino);
18915+#else
18916+ return 0;
18917+#endif
18918+}
1facf9fc 18919+
4a4d8108 18920+/* i_op.c */
b95c5147
AM
18921+enum {
18922+ AuIop_SYMLINK,
18923+ AuIop_DIR,
18924+ AuIop_OTHER,
18925+ AuIop_Last
18926+};
0b487229 18927+extern struct inode_operations aufs_iop[AuIop_Last], /* not const */
b95c5147 18928+ aufs_iop_nogetattr[AuIop_Last];
1308ab2a 18929+
4a4d8108
AM
18930+/* au_wr_dir flags */
18931+#define AuWrDir_ADD_ENTRY 1
7e9cd9fe
AM
18932+#define AuWrDir_ISDIR (1 << 1)
18933+#define AuWrDir_TMPFILE (1 << 2)
4a4d8108 18934+#define au_ftest_wrdir(flags, name) ((flags) & AuWrDir_##name)
7f207e10
AM
18935+#define au_fset_wrdir(flags, name) \
18936+ do { (flags) |= AuWrDir_##name; } while (0)
18937+#define au_fclr_wrdir(flags, name) \
18938+ do { (flags) &= ~AuWrDir_##name; } while (0)
1facf9fc 18939+
4a4d8108
AM
18940+struct au_wr_dir_args {
18941+ aufs_bindex_t force_btgt;
18942+ unsigned char flags;
18943+};
18944+int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
18945+ struct au_wr_dir_args *args);
dece6358 18946+
4a4d8108
AM
18947+struct dentry *au_pinned_h_parent(struct au_pin *pin);
18948+void au_pin_init(struct au_pin *pin, struct dentry *dentry,
18949+ aufs_bindex_t bindex, int lsc_di, int lsc_hi,
18950+ unsigned int udba, unsigned char flags);
18951+int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
18952+ unsigned int udba, unsigned char flags) __must_check;
18953+int au_do_pin(struct au_pin *pin) __must_check;
18954+void au_unpin(struct au_pin *pin);
c1595e42
JR
18955+int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
18956+
18957+#define AuIcpup_DID_CPUP 1
18958+#define au_ftest_icpup(flags, name) ((flags) & AuIcpup_##name)
18959+#define au_fset_icpup(flags, name) \
18960+ do { (flags) |= AuIcpup_##name; } while (0)
18961+#define au_fclr_icpup(flags, name) \
18962+ do { (flags) &= ~AuIcpup_##name; } while (0)
18963+
18964+struct au_icpup_args {
18965+ unsigned char flags;
18966+ unsigned char pin_flags;
18967+ aufs_bindex_t btgt;
18968+ unsigned int udba;
18969+ struct au_pin pin;
18970+ struct path h_path;
18971+ struct inode *h_inode;
18972+};
18973+
18974+int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
18975+ struct au_icpup_args *a);
18976+
0b487229
JR
18977+int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
18978+ struct path *h_path, int locked);
1facf9fc 18979+
4a4d8108
AM
18980+/* i_op_add.c */
18981+int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
18982+ struct dentry *h_parent, int isdir);
7eafdf33
AM
18983+int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
18984+ dev_t dev);
4a4d8108 18985+int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);
7eafdf33 18986+int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
b4510431 18987+ bool want_excl);
b912730e
AM
18988+struct vfsub_aopen_args;
18989+int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
18990+ struct vfsub_aopen_args *args);
38d290e6 18991+int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode);
4a4d8108
AM
18992+int aufs_link(struct dentry *src_dentry, struct inode *dir,
18993+ struct dentry *dentry);
7eafdf33 18994+int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
1facf9fc 18995+
4a4d8108
AM
18996+/* i_op_del.c */
18997+int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
18998+int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
18999+ struct dentry *h_parent, int isdir);
19000+int aufs_unlink(struct inode *dir, struct dentry *dentry);
19001+int aufs_rmdir(struct inode *dir, struct dentry *dentry);
1308ab2a 19002+
4a4d8108
AM
19003+/* i_op_ren.c */
19004+int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
19005+int aufs_rename(struct inode *src_dir, struct dentry *src_dentry,
f2c43d5f
AM
19006+ struct inode *dir, struct dentry *dentry,
19007+ unsigned int flags);
1facf9fc 19008+
4a4d8108
AM
19009+/* iinfo.c */
19010+struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
19011+void au_hiput(struct au_hinode *hinode);
19012+void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
19013+ struct dentry *h_wh);
19014+unsigned int au_hi_flags(struct inode *inode, int isdir);
1308ab2a 19015+
4a4d8108
AM
19016+/* hinode flags */
19017+#define AuHi_XINO 1
19018+#define AuHi_HNOTIFY (1 << 1)
19019+#define au_ftest_hi(flags, name) ((flags) & AuHi_##name)
7f207e10
AM
19020+#define au_fset_hi(flags, name) \
19021+ do { (flags) |= AuHi_##name; } while (0)
19022+#define au_fclr_hi(flags, name) \
19023+ do { (flags) &= ~AuHi_##name; } while (0)
1facf9fc 19024+
4a4d8108
AM
19025+#ifndef CONFIG_AUFS_HNOTIFY
19026+#undef AuHi_HNOTIFY
19027+#define AuHi_HNOTIFY 0
19028+#endif
1facf9fc 19029+
4a4d8108
AM
19030+void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
19031+ struct inode *h_inode, unsigned int flags);
1facf9fc 19032+
537831f9 19033+void au_update_iigen(struct inode *inode, int half);
4a4d8108 19034+void au_update_ibrange(struct inode *inode, int do_put_zero);
1facf9fc 19035+
4a4d8108 19036+void au_icntnr_init_once(void *_c);
5afbbe0d 19037+void au_hinode_init(struct au_hinode *hinode);
4a4d8108
AM
19038+int au_iinfo_init(struct inode *inode);
19039+void au_iinfo_fin(struct inode *inode);
e2f27e51 19040+int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
1308ab2a 19041+
e49829fe 19042+#ifdef CONFIG_PROC_FS
4a4d8108 19043+/* plink.c */
e49829fe 19044+int au_plink_maint(struct super_block *sb, int flags);
7e9cd9fe 19045+struct au_sbinfo;
e49829fe
JR
19046+void au_plink_maint_leave(struct au_sbinfo *sbinfo);
19047+int au_plink_maint_enter(struct super_block *sb);
4a4d8108
AM
19048+#ifdef CONFIG_AUFS_DEBUG
19049+void au_plink_list(struct super_block *sb);
19050+#else
19051+AuStubVoid(au_plink_list, struct super_block *sb)
19052+#endif
19053+int au_plink_test(struct inode *inode);
19054+struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
19055+void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
19056+ struct dentry *h_dentry);
e49829fe
JR
19057+void au_plink_put(struct super_block *sb, int verbose);
19058+void au_plink_clean(struct super_block *sb, int verbose);
4a4d8108 19059+void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
e49829fe
JR
19060+#else
19061+AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
19062+AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
19063+AuStubInt0(au_plink_maint_enter, struct super_block *sb);
19064+AuStubVoid(au_plink_list, struct super_block *sb);
19065+AuStubInt0(au_plink_test, struct inode *inode);
19066+AuStub(struct dentry *, au_plink_lkup, return NULL,
19067+ struct inode *inode, aufs_bindex_t bindex);
19068+AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
19069+ struct dentry *h_dentry);
19070+AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
19071+AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
19072+AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
19073+#endif /* CONFIG_PROC_FS */
1facf9fc 19074+
c1595e42
JR
19075+#ifdef CONFIG_AUFS_XATTR
19076+/* xattr.c */
7e9cd9fe
AM
19077+int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
19078+ unsigned int verbose);
c1595e42 19079+ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
f2c43d5f 19080+void au_xattr_init(struct super_block *sb);
c1595e42
JR
19081+#else
19082+AuStubInt0(au_cpup_xattr, struct dentry *h_dst, struct dentry *h_src,
7e9cd9fe 19083+ int ignore_flags, unsigned int verbose);
f2c43d5f 19084+AuStubVoid(au_xattr_init, struct super_block *sb);
c1595e42
JR
19085+#endif
19086+
19087+#ifdef CONFIG_FS_POSIX_ACL
19088+struct posix_acl *aufs_get_acl(struct inode *inode, int type);
19089+int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type);
19090+#endif
19091+
19092+#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
19093+enum {
19094+ AU_XATTR_SET,
c1595e42
JR
19095+ AU_ACL_SET
19096+};
19097+
f2c43d5f 19098+struct au_sxattr {
c1595e42
JR
19099+ int type;
19100+ union {
19101+ struct {
19102+ const char *name;
19103+ const void *value;
19104+ size_t size;
19105+ int flags;
19106+ } set;
19107+ struct {
c1595e42
JR
19108+ struct posix_acl *acl;
19109+ int type;
19110+ } acl_set;
19111+ } u;
19112+};
f2c43d5f
AM
19113+ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
19114+ struct au_sxattr *arg);
c1595e42
JR
19115+#endif
19116+
4a4d8108 19117+/* ---------------------------------------------------------------------- */
1308ab2a 19118+
4a4d8108
AM
19119+/* lock subclass for iinfo */
19120+enum {
19121+ AuLsc_II_CHILD, /* child first */
19122+ AuLsc_II_CHILD2, /* rename(2), link(2), and cpup at hnotify */
19123+ AuLsc_II_CHILD3, /* copyup dirs */
19124+ AuLsc_II_PARENT, /* see AuLsc_I_PARENT in vfsub.h */
19125+ AuLsc_II_PARENT2,
19126+ AuLsc_II_PARENT3, /* copyup dirs */
19127+ AuLsc_II_NEW_CHILD
19128+};
1308ab2a 19129+
1facf9fc 19130+/*
4a4d8108
AM
19131+ * ii_read_lock_child, ii_write_lock_child,
19132+ * ii_read_lock_child2, ii_write_lock_child2,
19133+ * ii_read_lock_child3, ii_write_lock_child3,
19134+ * ii_read_lock_parent, ii_write_lock_parent,
19135+ * ii_read_lock_parent2, ii_write_lock_parent2,
19136+ * ii_read_lock_parent3, ii_write_lock_parent3,
19137+ * ii_read_lock_new_child, ii_write_lock_new_child,
1facf9fc 19138+ */
4a4d8108
AM
19139+#define AuReadLockFunc(name, lsc) \
19140+static inline void ii_read_lock_##name(struct inode *i) \
19141+{ \
19142+ au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
19143+}
19144+
19145+#define AuWriteLockFunc(name, lsc) \
19146+static inline void ii_write_lock_##name(struct inode *i) \
19147+{ \
19148+ au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
19149+}
19150+
19151+#define AuRWLockFuncs(name, lsc) \
19152+ AuReadLockFunc(name, lsc) \
19153+ AuWriteLockFunc(name, lsc)
19154+
19155+AuRWLockFuncs(child, CHILD);
19156+AuRWLockFuncs(child2, CHILD2);
19157+AuRWLockFuncs(child3, CHILD3);
19158+AuRWLockFuncs(parent, PARENT);
19159+AuRWLockFuncs(parent2, PARENT2);
19160+AuRWLockFuncs(parent3, PARENT3);
19161+AuRWLockFuncs(new_child, NEW_CHILD);
19162+
19163+#undef AuReadLockFunc
19164+#undef AuWriteLockFunc
19165+#undef AuRWLockFuncs
1facf9fc 19166+
8b6a4947
AM
19167+#define ii_read_unlock(i) au_rw_read_unlock(&au_ii(i)->ii_rwsem)
19168+#define ii_write_unlock(i) au_rw_write_unlock(&au_ii(i)->ii_rwsem)
19169+#define ii_downgrade_lock(i) au_rw_dgrade_lock(&au_ii(i)->ii_rwsem)
1facf9fc 19170+
4a4d8108
AM
19171+#define IiMustNoWaiters(i) AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
19172+#define IiMustAnyLock(i) AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
19173+#define IiMustWriteLock(i) AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
1facf9fc 19174+
4a4d8108 19175+/* ---------------------------------------------------------------------- */
1308ab2a 19176+
027c5e7a
AM
19177+static inline void au_icntnr_init(struct au_icntnr *c)
19178+{
19179+#ifdef CONFIG_AUFS_DEBUG
19180+ c->vfs_inode.i_mode = 0;
19181+#endif
19182+}
19183+
cfc41e69 19184+static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
4a4d8108 19185+{
537831f9
AM
19186+ unsigned int gen;
19187+ struct au_iinfo *iinfo;
be52b249 19188+ struct au_iigen *iigen;
537831f9
AM
19189+
19190+ iinfo = au_ii(inode);
be52b249
AM
19191+ iigen = &iinfo->ii_generation;
19192+ spin_lock(&iigen->ig_spin);
cfc41e69
AM
19193+ if (igflags)
19194+ *igflags = iigen->ig_flags;
be52b249
AM
19195+ gen = iigen->ig_generation;
19196+ spin_unlock(&iigen->ig_spin);
537831f9
AM
19197+
19198+ return gen;
4a4d8108 19199+}
1308ab2a 19200+
4a4d8108
AM
19201+/* tiny test for inode number */
19202+/* tmpfs generation is too rough */
19203+static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
19204+{
19205+ struct au_iinfo *iinfo;
1308ab2a 19206+
4a4d8108
AM
19207+ iinfo = au_ii(inode);
19208+ AuRwMustAnyLock(&iinfo->ii_rwsem);
19209+ return !(iinfo->ii_hsb1 == h_inode->i_sb
19210+ && iinfo->ii_higen == h_inode->i_generation);
19211+}
1308ab2a 19212+
4a4d8108
AM
19213+static inline void au_iigen_dec(struct inode *inode)
19214+{
537831f9 19215+ struct au_iinfo *iinfo;
be52b249 19216+ struct au_iigen *iigen;
537831f9
AM
19217+
19218+ iinfo = au_ii(inode);
be52b249
AM
19219+ iigen = &iinfo->ii_generation;
19220+ spin_lock(&iigen->ig_spin);
19221+ iigen->ig_generation--;
19222+ spin_unlock(&iigen->ig_spin);
027c5e7a
AM
19223+}
19224+
19225+static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
19226+{
19227+ int err;
19228+
19229+ err = 0;
537831f9 19230+ if (unlikely(inode && au_iigen(inode, NULL) != sigen))
027c5e7a
AM
19231+ err = -EIO;
19232+
19233+ return err;
4a4d8108 19234+}
1308ab2a 19235+
4a4d8108 19236+/* ---------------------------------------------------------------------- */
1308ab2a 19237+
5afbbe0d
AM
19238+static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
19239+ aufs_bindex_t bindex)
19240+{
19241+ return iinfo->ii_hinode + bindex;
19242+}
19243+
19244+static inline int au_is_bad_inode(struct inode *inode)
19245+{
19246+ return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
19247+}
19248+
4a4d8108
AM
19249+static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
19250+ aufs_bindex_t bindex)
19251+{
19252+ IiMustAnyLock(inode);
5afbbe0d 19253+ return au_hinode(au_ii(inode), bindex)->hi_id;
4a4d8108 19254+}
1308ab2a 19255+
5afbbe0d 19256+static inline aufs_bindex_t au_ibtop(struct inode *inode)
4a4d8108
AM
19257+{
19258+ IiMustAnyLock(inode);
5afbbe0d 19259+ return au_ii(inode)->ii_btop;
4a4d8108 19260+}
1308ab2a 19261+
5afbbe0d 19262+static inline aufs_bindex_t au_ibbot(struct inode *inode)
4a4d8108
AM
19263+{
19264+ IiMustAnyLock(inode);
5afbbe0d 19265+ return au_ii(inode)->ii_bbot;
4a4d8108 19266+}
1308ab2a 19267+
4a4d8108
AM
19268+static inline struct au_vdir *au_ivdir(struct inode *inode)
19269+{
19270+ IiMustAnyLock(inode);
19271+ return au_ii(inode)->ii_vdir;
19272+}
1308ab2a 19273+
4a4d8108
AM
19274+static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
19275+{
19276+ IiMustAnyLock(inode);
5afbbe0d 19277+ return au_hinode(au_ii(inode), bindex)->hi_whdentry;
4a4d8108 19278+}
1308ab2a 19279+
5afbbe0d 19280+static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 19281+{
4a4d8108 19282+ IiMustWriteLock(inode);
5afbbe0d 19283+ au_ii(inode)->ii_btop = bindex;
4a4d8108 19284+}
1308ab2a 19285+
5afbbe0d 19286+static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
4a4d8108
AM
19287+{
19288+ IiMustWriteLock(inode);
5afbbe0d 19289+ au_ii(inode)->ii_bbot = bindex;
1308ab2a 19290+}
19291+
4a4d8108
AM
19292+static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
19293+{
19294+ IiMustWriteLock(inode);
19295+ au_ii(inode)->ii_vdir = vdir;
19296+}
1facf9fc 19297+
4a4d8108 19298+static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 19299+{
4a4d8108 19300+ IiMustAnyLock(inode);
5afbbe0d 19301+ return au_hinode(au_ii(inode), bindex);
4a4d8108 19302+}
dece6358 19303+
4a4d8108 19304+/* ---------------------------------------------------------------------- */
1facf9fc 19305+
4a4d8108
AM
19306+static inline struct dentry *au_pinned_parent(struct au_pin *pin)
19307+{
19308+ if (pin)
19309+ return pin->parent;
19310+ return NULL;
1facf9fc 19311+}
19312+
4a4d8108 19313+static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
1facf9fc 19314+{
4a4d8108
AM
19315+ if (pin && pin->hdir)
19316+ return pin->hdir->hi_inode;
19317+ return NULL;
1308ab2a 19318+}
1facf9fc 19319+
4a4d8108
AM
19320+static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
19321+{
19322+ if (pin)
19323+ return pin->hdir;
19324+ return NULL;
19325+}
1facf9fc 19326+
4a4d8108 19327+static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
1308ab2a 19328+{
4a4d8108
AM
19329+ if (pin)
19330+ pin->dentry = dentry;
19331+}
1308ab2a 19332+
4a4d8108
AM
19333+static inline void au_pin_set_parent_lflag(struct au_pin *pin,
19334+ unsigned char lflag)
19335+{
19336+ if (pin) {
7f207e10 19337+ if (lflag)
4a4d8108 19338+ au_fset_pin(pin->flags, DI_LOCKED);
7f207e10 19339+ else
4a4d8108 19340+ au_fclr_pin(pin->flags, DI_LOCKED);
1308ab2a 19341+ }
4a4d8108
AM
19342+}
19343+
7e9cd9fe 19344+#if 0 /* reserved */
4a4d8108
AM
19345+static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
19346+{
19347+ if (pin) {
19348+ dput(pin->parent);
19349+ pin->parent = dget(parent);
1facf9fc 19350+ }
4a4d8108 19351+}
7e9cd9fe 19352+#endif
1facf9fc 19353+
4a4d8108
AM
19354+/* ---------------------------------------------------------------------- */
19355+
027c5e7a 19356+struct au_branch;
4a4d8108
AM
19357+#ifdef CONFIG_AUFS_HNOTIFY
19358+struct au_hnotify_op {
19359+ void (*ctl)(struct au_hinode *hinode, int do_set);
027c5e7a 19360+ int (*alloc)(struct au_hinode *hinode);
7eafdf33
AM
19361+
19362+ /*
19363+ * if it returns true, the the caller should free hinode->hi_notify,
19364+ * otherwise ->free() frees it.
19365+ */
19366+ int (*free)(struct au_hinode *hinode,
19367+ struct au_hnotify *hn) __must_check;
4a4d8108
AM
19368+
19369+ void (*fin)(void);
19370+ int (*init)(void);
027c5e7a
AM
19371+
19372+ int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
19373+ void (*fin_br)(struct au_branch *br);
19374+ int (*init_br)(struct au_branch *br, int perm);
4a4d8108
AM
19375+};
19376+
19377+/* hnotify.c */
027c5e7a 19378+int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
4a4d8108
AM
19379+void au_hn_free(struct au_hinode *hinode);
19380+void au_hn_ctl(struct au_hinode *hinode, int do_set);
19381+void au_hn_reset(struct inode *inode, unsigned int flags);
19382+int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
19383+ struct qstr *h_child_qstr, struct inode *h_child_inode);
027c5e7a
AM
19384+int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
19385+int au_hnotify_init_br(struct au_branch *br, int perm);
19386+void au_hnotify_fin_br(struct au_branch *br);
4a4d8108
AM
19387+int __init au_hnotify_init(void);
19388+void au_hnotify_fin(void);
19389+
7f207e10 19390+/* hfsnotify.c */
4a4d8108
AM
19391+extern const struct au_hnotify_op au_hnotify_op;
19392+
19393+static inline
19394+void au_hn_init(struct au_hinode *hinode)
19395+{
19396+ hinode->hi_notify = NULL;
1308ab2a 19397+}
19398+
53392da6
AM
19399+static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
19400+{
19401+ return hinode->hi_notify;
19402+}
19403+
4a4d8108 19404+#else
c1595e42
JR
19405+AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
19406+ struct au_hinode *hinode __maybe_unused,
19407+ struct inode *inode __maybe_unused)
19408+AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
4a4d8108
AM
19409+AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
19410+AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
19411+ int do_set __maybe_unused)
19412+AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
19413+ unsigned int flags __maybe_unused)
027c5e7a
AM
19414+AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
19415+ struct au_branch *br __maybe_unused,
19416+ int perm __maybe_unused)
19417+AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
19418+ int perm __maybe_unused)
19419+AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
4a4d8108
AM
19420+AuStubInt0(__init au_hnotify_init, void)
19421+AuStubVoid(au_hnotify_fin, void)
19422+AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
19423+#endif /* CONFIG_AUFS_HNOTIFY */
19424+
19425+static inline void au_hn_suspend(struct au_hinode *hdir)
19426+{
19427+ au_hn_ctl(hdir, /*do_set*/0);
1308ab2a 19428+}
19429+
4a4d8108 19430+static inline void au_hn_resume(struct au_hinode *hdir)
1308ab2a 19431+{
4a4d8108
AM
19432+ au_hn_ctl(hdir, /*do_set*/1);
19433+}
1308ab2a 19434+
5afbbe0d 19435+static inline void au_hn_inode_lock(struct au_hinode *hdir)
4a4d8108 19436+{
febd17d6 19437+ inode_lock(hdir->hi_inode);
4a4d8108
AM
19438+ au_hn_suspend(hdir);
19439+}
dece6358 19440+
5afbbe0d 19441+static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
4a4d8108
AM
19442+ unsigned int sc __maybe_unused)
19443+{
febd17d6 19444+ inode_lock_nested(hdir->hi_inode, sc);
4a4d8108 19445+ au_hn_suspend(hdir);
1facf9fc 19446+}
1facf9fc 19447+
8b6a4947
AM
19448+#if 0 /* unused */
19449+#include "vfsub.h"
3c1bdaff
AM
19450+static inline void au_hn_inode_lock_shared_nested(struct au_hinode *hdir,
19451+ unsigned int sc)
19452+{
be118d29 19453+ inode_lock_shared_nested(hdir->hi_inode, sc);
3c1bdaff
AM
19454+ au_hn_suspend(hdir);
19455+}
8b6a4947 19456+#endif
3c1bdaff 19457+
5afbbe0d 19458+static inline void au_hn_inode_unlock(struct au_hinode *hdir)
4a4d8108
AM
19459+{
19460+ au_hn_resume(hdir);
febd17d6 19461+ inode_unlock(hdir->hi_inode);
4a4d8108
AM
19462+}
19463+
19464+#endif /* __KERNEL__ */
19465+#endif /* __AUFS_INODE_H__ */
7f207e10
AM
19466diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
19467--- /usr/share/empty/fs/aufs/ioctl.c 1970-01-01 01:00:00.000000000 +0100
0b487229 19468+++ linux/fs/aufs/ioctl.c 2021-02-22 23:30:37.702680911 +0100
062440b3 19469@@ -0,0 +1,220 @@
cd7a4cd9 19470+// SPDX-License-Identifier: GPL-2.0
4a4d8108 19471+/*
0b487229 19472+ * Copyright (C) 2005-2020 Junjiro R. Okajima
4a4d8108
AM
19473+ *
19474+ * This program, aufs is free software; you can redistribute it and/or modify
19475+ * it under the terms of the GNU General Public License as published by
19476+ * the Free Software Foundation; either version 2 of the License, or
19477+ * (at your option) any later version.
19478+ *
19479+ * This program is distributed in the hope that it will be useful,
19480+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19481+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19482+ * GNU General Public License for more details.
19483+ *
19484+ * You should have received a copy of the GNU General Public License
523b37e3 19485+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
19486+ */
19487+
19488+/*
19489+ * ioctl
19490+ * plink-management and readdir in userspace.
19491+ * assist the pathconf(3) wrapper library.
c2b27bf2 19492+ * move-down
076b876e 19493+ * File-based Hierarchical Storage Management.
4a4d8108
AM
19494+ */
19495+
c2b27bf2
AM
19496+#include <linux/compat.h>
19497+#include <linux/file.h>
4a4d8108
AM
19498+#include "aufs.h"
19499+
1e00d052 19500+static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
4a4d8108
AM
19501+{
19502+ int err, fd;
5afbbe0d 19503+ aufs_bindex_t wbi, bindex, bbot;
4a4d8108
AM
19504+ struct file *h_file;
19505+ struct super_block *sb;
19506+ struct dentry *root;
1e00d052
AM
19507+ struct au_branch *br;
19508+ struct aufs_wbr_fd wbrfd = {
19509+ .oflags = au_dir_roflags,
19510+ .brid = -1
19511+ };
19512+ const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
19513+ | O_NOATIME | O_CLOEXEC;
4a4d8108 19514+
1e00d052
AM
19515+ AuDebugOn(wbrfd.oflags & ~valid);
19516+
19517+ if (arg) {
19518+ err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
19519+ if (unlikely(err)) {
19520+ err = -EFAULT;
19521+ goto out;
19522+ }
19523+
19524+ err = -EINVAL;
19525+ AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
19526+ wbrfd.oflags |= au_dir_roflags;
19527+ AuDbg("0%o\n", wbrfd.oflags);
19528+ if (unlikely(wbrfd.oflags & ~valid))
19529+ goto out;
19530+ }
19531+
2000de60 19532+ fd = get_unused_fd_flags(0);
1e00d052
AM
19533+ err = fd;
19534+ if (unlikely(fd < 0))
4a4d8108 19535+ goto out;
4a4d8108 19536+
1e00d052 19537+ h_file = ERR_PTR(-EINVAL);
4a4d8108 19538+ wbi = 0;
1e00d052 19539+ br = NULL;
4a4d8108
AM
19540+ sb = path->dentry->d_sb;
19541+ root = sb->s_root;
19542+ aufs_read_lock(root, AuLock_IR);
5afbbe0d 19543+ bbot = au_sbbot(sb);
1e00d052
AM
19544+ if (wbrfd.brid >= 0) {
19545+ wbi = au_br_index(sb, wbrfd.brid);
5afbbe0d 19546+ if (unlikely(wbi < 0 || wbi > bbot))
1e00d052
AM
19547+ goto out_unlock;
19548+ }
19549+
19550+ h_file = ERR_PTR(-ENOENT);
19551+ br = au_sbr(sb, wbi);
19552+ if (!au_br_writable(br->br_perm)) {
19553+ if (arg)
19554+ goto out_unlock;
19555+
19556+ bindex = wbi + 1;
19557+ wbi = -1;
5afbbe0d 19558+ for (; bindex <= bbot; bindex++) {
1e00d052
AM
19559+ br = au_sbr(sb, bindex);
19560+ if (au_br_writable(br->br_perm)) {
4a4d8108 19561+ wbi = bindex;
1e00d052 19562+ br = au_sbr(sb, wbi);
4a4d8108
AM
19563+ break;
19564+ }
19565+ }
4a4d8108
AM
19566+ }
19567+ AuDbg("wbi %d\n", wbi);
1e00d052 19568+ if (wbi >= 0)
392086de
AM
19569+ h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
19570+ /*force_wr*/0);
1e00d052
AM
19571+
19572+out_unlock:
4a4d8108
AM
19573+ aufs_read_unlock(root, AuLock_IR);
19574+ err = PTR_ERR(h_file);
19575+ if (IS_ERR(h_file))
19576+ goto out_fd;
19577+
acd2b654 19578+ au_lcnt_dec(&br->br_nfiles); /* cf. au_h_open() */
4a4d8108
AM
19579+ fd_install(fd, h_file);
19580+ err = fd;
19581+ goto out; /* success */
19582+
4f0767ce 19583+out_fd:
4a4d8108 19584+ put_unused_fd(fd);
4f0767ce 19585+out:
1e00d052 19586+ AuTraceErr(err);
4a4d8108
AM
19587+ return err;
19588+}
19589+
19590+/* ---------------------------------------------------------------------- */
19591+
19592+long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
19593+{
19594+ long err;
c1595e42 19595+ struct dentry *dentry;
4a4d8108
AM
19596+
19597+ switch (cmd) {
4a4d8108
AM
19598+ case AUFS_CTL_RDU:
19599+ case AUFS_CTL_RDU_INO:
19600+ err = au_rdu_ioctl(file, cmd, arg);
19601+ break;
19602+
19603+ case AUFS_CTL_WBR_FD:
1e00d052 19604+ err = au_wbr_fd(&file->f_path, (void __user *)arg);
4a4d8108
AM
19605+ break;
19606+
027c5e7a
AM
19607+ case AUFS_CTL_IBUSY:
19608+ err = au_ibusy_ioctl(file, arg);
19609+ break;
19610+
076b876e
AM
19611+ case AUFS_CTL_BRINFO:
19612+ err = au_brinfo_ioctl(file, arg);
19613+ break;
19614+
19615+ case AUFS_CTL_FHSM_FD:
2000de60 19616+ dentry = file->f_path.dentry;
c1595e42
JR
19617+ if (IS_ROOT(dentry))
19618+ err = au_fhsm_fd(dentry->d_sb, arg);
19619+ else
19620+ err = -ENOTTY;
076b876e
AM
19621+ break;
19622+
4a4d8108
AM
19623+ default:
19624+ /* do not call the lower */
19625+ AuDbg("0x%x\n", cmd);
19626+ err = -ENOTTY;
19627+ }
19628+
19629+ AuTraceErr(err);
19630+ return err;
19631+}
19632+
19633+long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
19634+{
19635+ long err;
19636+
19637+ switch (cmd) {
c2b27bf2 19638+ case AUFS_CTL_MVDOWN:
2000de60 19639+ err = au_mvdown(file->f_path.dentry, (void __user *)arg);
c2b27bf2
AM
19640+ break;
19641+
4a4d8108 19642+ case AUFS_CTL_WBR_FD:
1e00d052 19643+ err = au_wbr_fd(&file->f_path, (void __user *)arg);
4a4d8108
AM
19644+ break;
19645+
19646+ default:
19647+ /* do not call the lower */
19648+ AuDbg("0x%x\n", cmd);
19649+ err = -ENOTTY;
19650+ }
19651+
19652+ AuTraceErr(err);
19653+ return err;
19654+}
b752ccd1
AM
19655+
19656+#ifdef CONFIG_COMPAT
19657+long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
19658+ unsigned long arg)
19659+{
19660+ long err;
19661+
19662+ switch (cmd) {
19663+ case AUFS_CTL_RDU:
19664+ case AUFS_CTL_RDU_INO:
19665+ err = au_rdu_compat_ioctl(file, cmd, arg);
19666+ break;
19667+
027c5e7a
AM
19668+ case AUFS_CTL_IBUSY:
19669+ err = au_ibusy_compat_ioctl(file, arg);
19670+ break;
19671+
076b876e
AM
19672+ case AUFS_CTL_BRINFO:
19673+ err = au_brinfo_compat_ioctl(file, arg);
19674+ break;
19675+
b752ccd1
AM
19676+ default:
19677+ err = aufs_ioctl_dir(file, cmd, arg);
19678+ }
19679+
19680+ AuTraceErr(err);
19681+ return err;
19682+}
19683+
b752ccd1
AM
19684+long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
19685+ unsigned long arg)
19686+{
19687+ return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
19688+}
19689+#endif
7f207e10
AM
19690diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
19691--- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
0b487229
JR
19692+++ linux/fs/aufs/i_op_add.c 2021-02-22 23:30:37.702680911 +0100
19693@@ -0,0 +1,936 @@
cd7a4cd9 19694+// SPDX-License-Identifier: GPL-2.0
4a4d8108 19695+/*
0b487229 19696+ * Copyright (C) 2005-2020 Junjiro R. Okajima
4a4d8108
AM
19697+ *
19698+ * This program, aufs is free software; you can redistribute it and/or modify
19699+ * it under the terms of the GNU General Public License as published by
19700+ * the Free Software Foundation; either version 2 of the License, or
19701+ * (at your option) any later version.
19702+ *
19703+ * This program is distributed in the hope that it will be useful,
19704+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19705+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19706+ * GNU General Public License for more details.
19707+ *
19708+ * You should have received a copy of the GNU General Public License
523b37e3 19709+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
19710+ */
19711+
19712+/*
19713+ * inode operations (add entry)
19714+ */
19715+
0b487229 19716+#include <linux/iversion.h>
4a4d8108
AM
19717+#include "aufs.h"
19718+
19719+/*
19720+ * final procedure of adding a new entry, except link(2).
19721+ * remove whiteout, instantiate, copyup the parent dir's times and size
19722+ * and update version.
19723+ * if it failed, re-create the removed whiteout.
19724+ */
19725+static int epilog(struct inode *dir, aufs_bindex_t bindex,
19726+ struct dentry *wh_dentry, struct dentry *dentry)
19727+{
19728+ int err, rerr;
19729+ aufs_bindex_t bwh;
19730+ struct path h_path;
076b876e 19731+ struct super_block *sb;
4a4d8108
AM
19732+ struct inode *inode, *h_dir;
19733+ struct dentry *wh;
19734+
19735+ bwh = -1;
076b876e 19736+ sb = dir->i_sb;
4a4d8108 19737+ if (wh_dentry) {
5527c038 19738+ h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
4a4d8108
AM
19739+ IMustLock(h_dir);
19740+ AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
19741+ bwh = au_dbwh(dentry);
19742+ h_path.dentry = wh_dentry;
076b876e 19743+ h_path.mnt = au_sbr_mnt(sb, bindex);
4a4d8108
AM
19744+ err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
19745+ dentry);
19746+ if (unlikely(err))
19747+ goto out;
19748+ }
19749+
19750+ inode = au_new_inode(dentry, /*must_new*/1);
19751+ if (!IS_ERR(inode)) {
19752+ d_instantiate(dentry, inode);
5527c038 19753+ dir = d_inode(dentry->d_parent); /* dir inode is locked */
4a4d8108 19754+ IMustLock(dir);
b912730e 19755+ au_dir_ts(dir, bindex);
be118d29 19756+ inode_inc_iversion(dir);
076b876e 19757+ au_fhsm_wrote(sb, bindex, /*force*/0);
4a4d8108
AM
19758+ return 0; /* success */
19759+ }
19760+
19761+ err = PTR_ERR(inode);
19762+ if (!wh_dentry)
19763+ goto out;
19764+
19765+ /* revert */
19766+ /* dir inode is locked */
19767+ wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
19768+ rerr = PTR_ERR(wh);
19769+ if (IS_ERR(wh)) {
523b37e3
AM
19770+ AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
19771+ dentry, err, rerr);
4a4d8108
AM
19772+ err = -EIO;
19773+ } else
19774+ dput(wh);
19775+
4f0767ce 19776+out:
4a4d8108
AM
19777+ return err;
19778+}
19779+
027c5e7a
AM
19780+static int au_d_may_add(struct dentry *dentry)
19781+{
19782+ int err;
19783+
19784+ err = 0;
19785+ if (unlikely(d_unhashed(dentry)))
19786+ err = -ENOENT;
5527c038 19787+ if (unlikely(d_really_is_positive(dentry)))
027c5e7a
AM
19788+ err = -EEXIST;
19789+ return err;
19790+}
19791+
4a4d8108
AM
19792+/*
19793+ * simple tests for the adding inode operations.
19794+ * following the checks in vfs, plus the parent-child relationship.
19795+ */
19796+int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
19797+ struct dentry *h_parent, int isdir)
19798+{
19799+ int err;
19800+ umode_t h_mode;
19801+ struct dentry *h_dentry;
19802+ struct inode *h_inode;
19803+
19804+ err = -ENAMETOOLONG;
19805+ if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19806+ goto out;
19807+
19808+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 19809+ if (d_really_is_negative(dentry)) {
4a4d8108 19810+ err = -EEXIST;
5527c038 19811+ if (unlikely(d_is_positive(h_dentry)))
4a4d8108
AM
19812+ goto out;
19813+ } else {
19814+ /* rename(2) case */
19815+ err = -EIO;
5527c038
JR
19816+ if (unlikely(d_is_negative(h_dentry)))
19817+ goto out;
19818+ h_inode = d_inode(h_dentry);
19819+ if (unlikely(!h_inode->i_nlink))
4a4d8108
AM
19820+ goto out;
19821+
19822+ h_mode = h_inode->i_mode;
19823+ if (!isdir) {
19824+ err = -EISDIR;
19825+ if (unlikely(S_ISDIR(h_mode)))
19826+ goto out;
19827+ } else if (unlikely(!S_ISDIR(h_mode))) {
19828+ err = -ENOTDIR;
19829+ goto out;
19830+ }
19831+ }
19832+
19833+ err = 0;
19834+ /* expected parent dir is locked */
19835+ if (unlikely(h_parent != h_dentry->d_parent))
19836+ err = -EIO;
19837+
4f0767ce 19838+out:
4a4d8108
AM
19839+ AuTraceErr(err);
19840+ return err;
19841+}
19842+
19843+/*
19844+ * initial procedure of adding a new entry.
19845+ * prepare writable branch and the parent dir, lock it,
19846+ * and lookup whiteout for the new entry.
19847+ */
19848+static struct dentry*
19849+lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
19850+ struct dentry *src_dentry, struct au_pin *pin,
19851+ struct au_wr_dir_args *wr_dir_args)
19852+{
19853+ struct dentry *wh_dentry, *h_parent;
19854+ struct super_block *sb;
19855+ struct au_branch *br;
19856+ int err;
19857+ unsigned int udba;
19858+ aufs_bindex_t bcpup;
19859+
523b37e3 19860+ AuDbg("%pd\n", dentry);
4a4d8108
AM
19861+
19862+ err = au_wr_dir(dentry, src_dentry, wr_dir_args);
19863+ bcpup = err;
19864+ wh_dentry = ERR_PTR(err);
19865+ if (unlikely(err < 0))
19866+ goto out;
19867+
19868+ sb = dentry->d_sb;
19869+ udba = au_opt_udba(sb);
19870+ err = au_pin(pin, dentry, bcpup, udba,
19871+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
19872+ wh_dentry = ERR_PTR(err);
19873+ if (unlikely(err))
19874+ goto out;
19875+
19876+ h_parent = au_pinned_h_parent(pin);
19877+ if (udba != AuOpt_UDBA_NONE
5afbbe0d 19878+ && au_dbtop(dentry) == bcpup)
4a4d8108
AM
19879+ err = au_may_add(dentry, bcpup, h_parent,
19880+ au_ftest_wrdir(wr_dir_args->flags, ISDIR));
19881+ else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19882+ err = -ENAMETOOLONG;
19883+ wh_dentry = ERR_PTR(err);
19884+ if (unlikely(err))
19885+ goto out_unpin;
19886+
19887+ br = au_sbr(sb, bcpup);
19888+ if (dt) {
19889+ struct path tmp = {
19890+ .dentry = h_parent,
86dc4139 19891+ .mnt = au_br_mnt(br)
4a4d8108
AM
19892+ };
19893+ au_dtime_store(dt, au_pinned_parent(pin), &tmp);
19894+ }
19895+
19896+ wh_dentry = NULL;
19897+ if (bcpup != au_dbwh(dentry))
19898+ goto out; /* success */
19899+
2000de60
JR
19900+ /*
19901+ * ENAMETOOLONG here means that if we allowed create such name, then it
19902+ * would not be able to removed in the future. So we don't allow such
19903+ * name here and we don't handle ENAMETOOLONG differently here.
19904+ */
4a4d8108
AM
19905+ wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
19906+
4f0767ce 19907+out_unpin:
4a4d8108
AM
19908+ if (IS_ERR(wh_dentry))
19909+ au_unpin(pin);
4f0767ce 19910+out:
4a4d8108
AM
19911+ return wh_dentry;
19912+}
19913+
19914+/* ---------------------------------------------------------------------- */
19915+
19916+enum { Mknod, Symlink, Creat };
19917+struct simple_arg {
19918+ int type;
19919+ union {
19920+ struct {
b912730e
AM
19921+ umode_t mode;
19922+ bool want_excl;
19923+ bool try_aopen;
19924+ struct vfsub_aopen_args *aopen;
4a4d8108
AM
19925+ } c;
19926+ struct {
19927+ const char *symname;
19928+ } s;
19929+ struct {
7eafdf33 19930+ umode_t mode;
4a4d8108
AM
19931+ dev_t dev;
19932+ } m;
19933+ } u;
19934+};
19935+
19936+static int add_simple(struct inode *dir, struct dentry *dentry,
19937+ struct simple_arg *arg)
19938+{
076b876e 19939+ int err, rerr;
5afbbe0d 19940+ aufs_bindex_t btop;
4a4d8108 19941+ unsigned char created;
b912730e
AM
19942+ const unsigned char try_aopen
19943+ = (arg->type == Creat && arg->u.c.try_aopen);
acd2b654 19944+ struct vfsub_aopen_args *aopen = arg->u.c.aopen;
4a4d8108
AM
19945+ struct dentry *wh_dentry, *parent;
19946+ struct inode *h_dir;
b912730e
AM
19947+ struct super_block *sb;
19948+ struct au_branch *br;
acd2b654 19949+ /* to reduce stack size */
c2b27bf2
AM
19950+ struct {
19951+ struct au_dtime dt;
19952+ struct au_pin pin;
19953+ struct path h_path;
19954+ struct au_wr_dir_args wr_dir_args;
19955+ } *a;
4a4d8108 19956+
523b37e3 19957+ AuDbg("%pd\n", dentry);
4a4d8108
AM
19958+ IMustLock(dir);
19959+
c2b27bf2
AM
19960+ err = -ENOMEM;
19961+ a = kmalloc(sizeof(*a), GFP_NOFS);
19962+ if (unlikely(!a))
19963+ goto out;
19964+ a->wr_dir_args.force_btgt = -1;
19965+ a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
19966+
4a4d8108 19967+ parent = dentry->d_parent; /* dir inode is locked */
b912730e
AM
19968+ if (!try_aopen) {
19969+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
19970+ if (unlikely(err))
19971+ goto out_free;
19972+ }
027c5e7a
AM
19973+ err = au_d_may_add(dentry);
19974+ if (unlikely(err))
19975+ goto out_unlock;
b912730e
AM
19976+ if (!try_aopen)
19977+ di_write_lock_parent(parent);
c2b27bf2
AM
19978+ wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
19979+ &a->pin, &a->wr_dir_args);
4a4d8108
AM
19980+ err = PTR_ERR(wh_dentry);
19981+ if (IS_ERR(wh_dentry))
027c5e7a 19982+ goto out_parent;
4a4d8108 19983+
5afbbe0d 19984+ btop = au_dbtop(dentry);
b912730e 19985+ sb = dentry->d_sb;
5afbbe0d
AM
19986+ br = au_sbr(sb, btop);
19987+ a->h_path.dentry = au_h_dptr(dentry, btop);
b912730e 19988+ a->h_path.mnt = au_br_mnt(br);
c2b27bf2 19989+ h_dir = au_pinned_h_dir(&a->pin);
4a4d8108
AM
19990+ switch (arg->type) {
19991+ case Creat:
acd2b654 19992+ if (!try_aopen || !h_dir->i_op->atomic_open) {
b912730e
AM
19993+ err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
19994+ arg->u.c.want_excl);
acd2b654
AM
19995+ created = !err;
19996+ if (!err && try_aopen)
19997+ aopen->file->f_mode |= FMODE_CREATED;
19998+ } else {
19999+ aopen->br = br;
20000+ err = vfsub_atomic_open(h_dir, a->h_path.dentry, aopen);
20001+ AuDbg("err %d\n", err);
20002+ AuDbgFile(aopen->file);
20003+ created = err >= 0
20004+ && !!(aopen->file->f_mode & FMODE_CREATED);
20005+ }
4a4d8108
AM
20006+ break;
20007+ case Symlink:
c2b27bf2 20008+ err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
acd2b654 20009+ created = !err;
4a4d8108
AM
20010+ break;
20011+ case Mknod:
c2b27bf2
AM
20012+ err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
20013+ arg->u.m.dev);
acd2b654 20014+ created = !err;
4a4d8108
AM
20015+ break;
20016+ default:
20017+ BUG();
20018+ }
acd2b654
AM
20019+ if (unlikely(err < 0))
20020+ goto out_unpin;
20021+
20022+ err = epilog(dir, btop, wh_dentry, dentry);
4a4d8108 20023+ if (!err)
acd2b654 20024+ goto out_unpin; /* success */
4a4d8108
AM
20025+
20026+ /* revert */
acd2b654 20027+ if (created /* && d_is_positive(a->h_path.dentry) */) {
523b37e3
AM
20028+ /* no delegation since it is just created */
20029+ rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
20030+ /*force*/0);
4a4d8108 20031+ if (rerr) {
523b37e3
AM
20032+ AuIOErr("%pd revert failure(%d, %d)\n",
20033+ dentry, err, rerr);
4a4d8108
AM
20034+ err = -EIO;
20035+ }
c2b27bf2 20036+ au_dtime_revert(&a->dt);
4a4d8108 20037+ }
acd2b654
AM
20038+ if (try_aopen && h_dir->i_op->atomic_open
20039+ && (aopen->file->f_mode & FMODE_OPENED))
20040+ /* aopen->file is still opened */
20041+ au_lcnt_dec(&aopen->br->br_nfiles);
4a4d8108 20042+
acd2b654 20043+out_unpin:
c2b27bf2 20044+ au_unpin(&a->pin);
4a4d8108 20045+ dput(wh_dentry);
027c5e7a 20046+out_parent:
b912730e
AM
20047+ if (!try_aopen)
20048+ di_write_unlock(parent);
027c5e7a 20049+out_unlock:
4a4d8108 20050+ if (unlikely(err)) {
5afbbe0d 20051+ au_update_dbtop(dentry);
4a4d8108
AM
20052+ d_drop(dentry);
20053+ }
b912730e
AM
20054+ if (!try_aopen)
20055+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2 20056+out_free:
e49925d1 20057+ au_kfree_rcu(a);
027c5e7a 20058+out:
4a4d8108
AM
20059+ return err;
20060+}
20061+
7eafdf33
AM
20062+int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
20063+ dev_t dev)
4a4d8108
AM
20064+{
20065+ struct simple_arg arg = {
20066+ .type = Mknod,
20067+ .u.m = {
20068+ .mode = mode,
20069+ .dev = dev
20070+ }
20071+ };
20072+ return add_simple(dir, dentry, &arg);
20073+}
20074+
20075+int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
20076+{
20077+ struct simple_arg arg = {
20078+ .type = Symlink,
20079+ .u.s.symname = symname
20080+ };
20081+ return add_simple(dir, dentry, &arg);
20082+}
20083+
7eafdf33 20084+int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
b4510431 20085+ bool want_excl)
4a4d8108
AM
20086+{
20087+ struct simple_arg arg = {
20088+ .type = Creat,
20089+ .u.c = {
b4510431
AM
20090+ .mode = mode,
20091+ .want_excl = want_excl
4a4d8108
AM
20092+ }
20093+ };
20094+ return add_simple(dir, dentry, &arg);
20095+}
20096+
b912730e
AM
20097+int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
20098+ struct vfsub_aopen_args *aopen_args)
20099+{
20100+ struct simple_arg arg = {
20101+ .type = Creat,
20102+ .u.c = {
20103+ .mode = aopen_args->create_mode,
20104+ .want_excl = aopen_args->open_flag & O_EXCL,
20105+ .try_aopen = true,
20106+ .aopen = aopen_args
20107+ }
20108+ };
20109+ return add_simple(dir, dentry, &arg);
20110+}
20111+
38d290e6
JR
20112+int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
20113+{
20114+ int err;
20115+ aufs_bindex_t bindex;
20116+ struct super_block *sb;
20117+ struct dentry *parent, *h_parent, *h_dentry;
20118+ struct inode *h_dir, *inode;
20119+ struct vfsmount *h_mnt;
20120+ struct au_wr_dir_args wr_dir_args = {
20121+ .force_btgt = -1,
20122+ .flags = AuWrDir_TMPFILE
20123+ };
20124+
20125+ /* copy-up may happen */
febd17d6 20126+ inode_lock(dir);
38d290e6
JR
20127+
20128+ sb = dir->i_sb;
20129+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
20130+ if (unlikely(err))
20131+ goto out;
20132+
20133+ err = au_di_init(dentry);
20134+ if (unlikely(err))
20135+ goto out_si;
20136+
20137+ err = -EBUSY;
20138+ parent = d_find_any_alias(dir);
20139+ AuDebugOn(!parent);
20140+ di_write_lock_parent(parent);
5527c038 20141+ if (unlikely(d_inode(parent) != dir))
38d290e6
JR
20142+ goto out_parent;
20143+
20144+ err = au_digen_test(parent, au_sigen(sb));
20145+ if (unlikely(err))
20146+ goto out_parent;
20147+
5afbbe0d
AM
20148+ bindex = au_dbtop(parent);
20149+ au_set_dbtop(dentry, bindex);
20150+ au_set_dbbot(dentry, bindex);
38d290e6
JR
20151+ err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
20152+ bindex = err;
20153+ if (unlikely(err < 0))
20154+ goto out_parent;
20155+
20156+ err = -EOPNOTSUPP;
20157+ h_dir = au_h_iptr(dir, bindex);
20158+ if (unlikely(!h_dir->i_op->tmpfile))
20159+ goto out_parent;
20160+
20161+ h_mnt = au_sbr_mnt(sb, bindex);
20162+ err = vfsub_mnt_want_write(h_mnt);
20163+ if (unlikely(err))
20164+ goto out_parent;
20165+
20166+ h_parent = au_h_dptr(parent, bindex);
521ced18
JR
20167+ h_dentry = vfs_tmpfile(h_parent, mode, /*open_flag*/0);
20168+ if (IS_ERR(h_dentry)) {
20169+ err = PTR_ERR(h_dentry);
38d290e6 20170+ goto out_mnt;
521ced18 20171+ }
38d290e6 20172+
5afbbe0d
AM
20173+ au_set_dbtop(dentry, bindex);
20174+ au_set_dbbot(dentry, bindex);
38d290e6
JR
20175+ au_set_h_dptr(dentry, bindex, dget(h_dentry));
20176+ inode = au_new_inode(dentry, /*must_new*/1);
20177+ if (IS_ERR(inode)) {
20178+ err = PTR_ERR(inode);
20179+ au_set_h_dptr(dentry, bindex, NULL);
5afbbe0d
AM
20180+ au_set_dbtop(dentry, -1);
20181+ au_set_dbbot(dentry, -1);
38d290e6
JR
20182+ } else {
20183+ if (!inode->i_nlink)
20184+ set_nlink(inode, 1);
20185+ d_tmpfile(dentry, inode);
20186+ au_di(dentry)->di_tmpfile = 1;
20187+
20188+ /* update without i_mutex */
5afbbe0d 20189+ if (au_ibtop(dir) == au_dbtop(dentry))
38d290e6
JR
20190+ au_cpup_attr_timesizes(dir);
20191+ }
38d290e6 20192+ dput(h_dentry);
521ced18 20193+
38d290e6
JR
20194+out_mnt:
20195+ vfsub_mnt_drop_write(h_mnt);
20196+out_parent:
20197+ di_write_unlock(parent);
20198+ dput(parent);
20199+ di_write_unlock(dentry);
5afbbe0d 20200+ if (unlikely(err)) {
38d290e6
JR
20201+ au_di_fin(dentry);
20202+ dentry->d_fsdata = NULL;
20203+ }
20204+out_si:
20205+ si_read_unlock(sb);
20206+out:
febd17d6 20207+ inode_unlock(dir);
38d290e6
JR
20208+ return err;
20209+}
20210+
4a4d8108
AM
20211+/* ---------------------------------------------------------------------- */
20212+
20213+struct au_link_args {
20214+ aufs_bindex_t bdst, bsrc;
20215+ struct au_pin pin;
20216+ struct path h_path;
20217+ struct dentry *src_parent, *parent;
20218+};
20219+
20220+static int au_cpup_before_link(struct dentry *src_dentry,
20221+ struct au_link_args *a)
20222+{
20223+ int err;
20224+ struct dentry *h_src_dentry;
c2b27bf2
AM
20225+ struct au_cp_generic cpg = {
20226+ .dentry = src_dentry,
20227+ .bdst = a->bdst,
20228+ .bsrc = a->bsrc,
20229+ .len = -1,
20230+ .pin = &a->pin,
20231+ .flags = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
20232+ };
4a4d8108
AM
20233+
20234+ di_read_lock_parent(a->src_parent, AuLock_IR);
20235+ err = au_test_and_cpup_dirs(src_dentry, a->bdst);
20236+ if (unlikely(err))
20237+ goto out;
20238+
20239+ h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
4a4d8108
AM
20240+ err = au_pin(&a->pin, src_dentry, a->bdst,
20241+ au_opt_udba(src_dentry->d_sb),
20242+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20243+ if (unlikely(err))
20244+ goto out;
367653fa 20245+
c2b27bf2 20246+ err = au_sio_cpup_simple(&cpg);
4a4d8108
AM
20247+ au_unpin(&a->pin);
20248+
4f0767ce 20249+out:
4a4d8108
AM
20250+ di_read_unlock(a->src_parent, AuLock_IR);
20251+ return err;
20252+}
20253+
86dc4139
AM
20254+static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
20255+ struct au_link_args *a)
4a4d8108
AM
20256+{
20257+ int err;
20258+ unsigned char plink;
5afbbe0d 20259+ aufs_bindex_t bbot;
4a4d8108 20260+ struct dentry *h_src_dentry;
523b37e3 20261+ struct inode *h_inode, *inode, *delegated;
4a4d8108
AM
20262+ struct super_block *sb;
20263+ struct file *h_file;
20264+
20265+ plink = 0;
20266+ h_inode = NULL;
20267+ sb = src_dentry->d_sb;
5527c038 20268+ inode = d_inode(src_dentry);
5afbbe0d 20269+ if (au_ibtop(inode) <= a->bdst)
4a4d8108
AM
20270+ h_inode = au_h_iptr(inode, a->bdst);
20271+ if (!h_inode || !h_inode->i_nlink) {
20272+ /* copyup src_dentry as the name of dentry. */
5afbbe0d
AM
20273+ bbot = au_dbbot(dentry);
20274+ if (bbot < a->bsrc)
20275+ au_set_dbbot(dentry, a->bsrc);
86dc4139
AM
20276+ au_set_h_dptr(dentry, a->bsrc,
20277+ dget(au_h_dptr(src_dentry, a->bsrc)));
20278+ dget(a->h_path.dentry);
20279+ au_set_h_dptr(dentry, a->bdst, NULL);
c1595e42
JR
20280+ AuDbg("temporary d_inode...\n");
20281+ spin_lock(&dentry->d_lock);
5527c038 20282+ dentry->d_inode = d_inode(src_dentry); /* tmp */
c1595e42 20283+ spin_unlock(&dentry->d_lock);
392086de 20284+ h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
86dc4139 20285+ if (IS_ERR(h_file))
4a4d8108 20286+ err = PTR_ERR(h_file);
86dc4139 20287+ else {
c2b27bf2
AM
20288+ struct au_cp_generic cpg = {
20289+ .dentry = dentry,
20290+ .bdst = a->bdst,
20291+ .bsrc = -1,
20292+ .len = -1,
20293+ .pin = &a->pin,
20294+ .flags = AuCpup_KEEPLINO
20295+ };
20296+ err = au_sio_cpup_simple(&cpg);
86dc4139
AM
20297+ au_h_open_post(dentry, a->bsrc, h_file);
20298+ if (!err) {
20299+ dput(a->h_path.dentry);
20300+ a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20301+ } else
20302+ au_set_h_dptr(dentry, a->bdst,
20303+ a->h_path.dentry);
20304+ }
c1595e42 20305+ spin_lock(&dentry->d_lock);
86dc4139 20306+ dentry->d_inode = NULL; /* restore */
c1595e42
JR
20307+ spin_unlock(&dentry->d_lock);
20308+ AuDbg("temporary d_inode...done\n");
86dc4139 20309+ au_set_h_dptr(dentry, a->bsrc, NULL);
5afbbe0d 20310+ au_set_dbbot(dentry, bbot);
4a4d8108
AM
20311+ } else {
20312+ /* the inode of src_dentry already exists on a.bdst branch */
20313+ h_src_dentry = d_find_alias(h_inode);
20314+ if (!h_src_dentry && au_plink_test(inode)) {
20315+ plink = 1;
20316+ h_src_dentry = au_plink_lkup(inode, a->bdst);
20317+ err = PTR_ERR(h_src_dentry);
20318+ if (IS_ERR(h_src_dentry))
20319+ goto out;
20320+
5527c038 20321+ if (unlikely(d_is_negative(h_src_dentry))) {
4a4d8108
AM
20322+ dput(h_src_dentry);
20323+ h_src_dentry = NULL;
20324+ }
20325+
20326+ }
20327+ if (h_src_dentry) {
523b37e3 20328+ delegated = NULL;
4a4d8108 20329+ err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
523b37e3
AM
20330+ &a->h_path, &delegated);
20331+ if (unlikely(err == -EWOULDBLOCK)) {
20332+ pr_warn("cannot retry for NFSv4 delegation"
20333+ " for an internal link\n");
20334+ iput(delegated);
20335+ }
4a4d8108
AM
20336+ dput(h_src_dentry);
20337+ } else {
20338+ AuIOErr("no dentry found for hi%lu on b%d\n",
20339+ h_inode->i_ino, a->bdst);
20340+ err = -EIO;
20341+ }
20342+ }
20343+
20344+ if (!err && !plink)
20345+ au_plink_append(inode, a->bdst, a->h_path.dentry);
20346+
20347+out:
2cbb1c4b 20348+ AuTraceErr(err);
4a4d8108
AM
20349+ return err;
20350+}
20351+
20352+int aufs_link(struct dentry *src_dentry, struct inode *dir,
20353+ struct dentry *dentry)
20354+{
20355+ int err, rerr;
20356+ struct au_dtime dt;
20357+ struct au_link_args *a;
20358+ struct dentry *wh_dentry, *h_src_dentry;
523b37e3 20359+ struct inode *inode, *delegated;
4a4d8108
AM
20360+ struct super_block *sb;
20361+ struct au_wr_dir_args wr_dir_args = {
20362+ /* .force_btgt = -1, */
20363+ .flags = AuWrDir_ADD_ENTRY
20364+ };
20365+
20366+ IMustLock(dir);
5527c038 20367+ inode = d_inode(src_dentry);
4a4d8108
AM
20368+ IMustLock(inode);
20369+
4a4d8108
AM
20370+ err = -ENOMEM;
20371+ a = kzalloc(sizeof(*a), GFP_NOFS);
20372+ if (unlikely(!a))
20373+ goto out;
20374+
20375+ a->parent = dentry->d_parent; /* dir inode is locked */
027c5e7a
AM
20376+ err = aufs_read_and_write_lock2(dentry, src_dentry,
20377+ AuLock_NOPLM | AuLock_GEN);
e49829fe
JR
20378+ if (unlikely(err))
20379+ goto out_kfree;
38d290e6 20380+ err = au_d_linkable(src_dentry);
027c5e7a
AM
20381+ if (unlikely(err))
20382+ goto out_unlock;
20383+ err = au_d_may_add(dentry);
20384+ if (unlikely(err))
20385+ goto out_unlock;
e49829fe 20386+
4a4d8108 20387+ a->src_parent = dget_parent(src_dentry);
5afbbe0d 20388+ wr_dir_args.force_btgt = au_ibtop(inode);
4a4d8108
AM
20389+
20390+ di_write_lock_parent(a->parent);
20391+ wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
20392+ wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
20393+ &wr_dir_args);
20394+ err = PTR_ERR(wh_dentry);
20395+ if (IS_ERR(wh_dentry))
027c5e7a 20396+ goto out_parent;
4a4d8108
AM
20397+
20398+ err = 0;
20399+ sb = dentry->d_sb;
5afbbe0d 20400+ a->bdst = au_dbtop(dentry);
4a4d8108
AM
20401+ a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20402+ a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
5afbbe0d 20403+ a->bsrc = au_ibtop(inode);
2cbb1c4b 20404+ h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
38d290e6
JR
20405+ if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
20406+ h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
2cbb1c4b 20407+ if (!h_src_dentry) {
5afbbe0d 20408+ a->bsrc = au_dbtop(src_dentry);
2cbb1c4b
JR
20409+ h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
20410+ AuDebugOn(!h_src_dentry);
38d290e6
JR
20411+ } else if (IS_ERR(h_src_dentry)) {
20412+ err = PTR_ERR(h_src_dentry);
2cbb1c4b 20413+ goto out_parent;
38d290e6 20414+ }
2cbb1c4b 20415+
f2c43d5f
AM
20416+ /*
20417+ * aufs doesn't touch the credential so
acd2b654 20418+ * security_dentry_create_files_as() is unnecessary.
f2c43d5f 20419+ */
4a4d8108
AM
20420+ if (au_opt_test(au_mntflags(sb), PLINK)) {
20421+ if (a->bdst < a->bsrc
20422+ /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
86dc4139 20423+ err = au_cpup_or_link(src_dentry, dentry, a);
523b37e3
AM
20424+ else {
20425+ delegated = NULL;
4a4d8108 20426+ err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
523b37e3
AM
20427+ &a->h_path, &delegated);
20428+ if (unlikely(err == -EWOULDBLOCK)) {
20429+ pr_warn("cannot retry for NFSv4 delegation"
20430+ " for an internal link\n");
20431+ iput(delegated);
20432+ }
20433+ }
2cbb1c4b 20434+ dput(h_src_dentry);
4a4d8108
AM
20435+ } else {
20436+ /*
20437+ * copyup src_dentry to the branch we process,
20438+ * and then link(2) to it.
20439+ */
2cbb1c4b 20440+ dput(h_src_dentry);
4a4d8108
AM
20441+ if (a->bdst < a->bsrc
20442+ /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
20443+ au_unpin(&a->pin);
20444+ di_write_unlock(a->parent);
20445+ err = au_cpup_before_link(src_dentry, a);
20446+ di_write_lock_parent(a->parent);
20447+ if (!err)
20448+ err = au_pin(&a->pin, dentry, a->bdst,
20449+ au_opt_udba(sb),
20450+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20451+ if (unlikely(err))
20452+ goto out_wh;
20453+ }
20454+ if (!err) {
20455+ h_src_dentry = au_h_dptr(src_dentry, a->bdst);
20456+ err = -ENOENT;
5527c038 20457+ if (h_src_dentry && d_is_positive(h_src_dentry)) {
523b37e3 20458+ delegated = NULL;
4a4d8108
AM
20459+ err = vfsub_link(h_src_dentry,
20460+ au_pinned_h_dir(&a->pin),
523b37e3
AM
20461+ &a->h_path, &delegated);
20462+ if (unlikely(err == -EWOULDBLOCK)) {
20463+ pr_warn("cannot retry"
20464+ " for NFSv4 delegation"
20465+ " for an internal link\n");
20466+ iput(delegated);
20467+ }
20468+ }
4a4d8108
AM
20469+ }
20470+ }
20471+ if (unlikely(err))
20472+ goto out_unpin;
20473+
20474+ if (wh_dentry) {
20475+ a->h_path.dentry = wh_dentry;
20476+ err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
20477+ dentry);
20478+ if (unlikely(err))
20479+ goto out_revert;
20480+ }
20481+
b912730e 20482+ au_dir_ts(dir, a->bdst);
be118d29 20483+ inode_inc_iversion(dir);
4a4d8108
AM
20484+ inc_nlink(inode);
20485+ inode->i_ctime = dir->i_ctime;
027c5e7a
AM
20486+ d_instantiate(dentry, au_igrab(inode));
20487+ if (d_unhashed(a->h_path.dentry))
4a4d8108
AM
20488+ /* some filesystem calls d_drop() */
20489+ d_drop(dentry);
076b876e
AM
20490+ /* some filesystems consume an inode even hardlink */
20491+ au_fhsm_wrote(sb, a->bdst, /*force*/0);
4a4d8108
AM
20492+ goto out_unpin; /* success */
20493+
4f0767ce 20494+out_revert:
523b37e3
AM
20495+ /* no delegation since it is just created */
20496+ rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
20497+ /*delegated*/NULL, /*force*/0);
027c5e7a 20498+ if (unlikely(rerr)) {
523b37e3 20499+ AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
027c5e7a
AM
20500+ err = -EIO;
20501+ }
4a4d8108 20502+ au_dtime_revert(&dt);
4f0767ce 20503+out_unpin:
4a4d8108 20504+ au_unpin(&a->pin);
4f0767ce 20505+out_wh:
4a4d8108 20506+ dput(wh_dentry);
027c5e7a
AM
20507+out_parent:
20508+ di_write_unlock(a->parent);
20509+ dput(a->src_parent);
4f0767ce 20510+out_unlock:
4a4d8108 20511+ if (unlikely(err)) {
5afbbe0d 20512+ au_update_dbtop(dentry);
4a4d8108
AM
20513+ d_drop(dentry);
20514+ }
4a4d8108 20515+ aufs_read_and_write_unlock2(dentry, src_dentry);
e49829fe 20516+out_kfree:
e49925d1 20517+ au_kfree_rcu(a);
4f0767ce 20518+out:
86dc4139 20519+ AuTraceErr(err);
4a4d8108
AM
20520+ return err;
20521+}
20522+
7eafdf33 20523+int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
4a4d8108
AM
20524+{
20525+ int err, rerr;
20526+ aufs_bindex_t bindex;
20527+ unsigned char diropq;
20528+ struct path h_path;
20529+ struct dentry *wh_dentry, *parent, *opq_dentry;
febd17d6 20530+ struct inode *h_inode;
4a4d8108
AM
20531+ struct super_block *sb;
20532+ struct {
20533+ struct au_pin pin;
20534+ struct au_dtime dt;
20535+ } *a; /* reduce the stack usage */
20536+ struct au_wr_dir_args wr_dir_args = {
20537+ .force_btgt = -1,
20538+ .flags = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
20539+ };
20540+
20541+ IMustLock(dir);
20542+
20543+ err = -ENOMEM;
20544+ a = kmalloc(sizeof(*a), GFP_NOFS);
20545+ if (unlikely(!a))
20546+ goto out;
20547+
027c5e7a
AM
20548+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
20549+ if (unlikely(err))
20550+ goto out_free;
20551+ err = au_d_may_add(dentry);
20552+ if (unlikely(err))
20553+ goto out_unlock;
20554+
4a4d8108
AM
20555+ parent = dentry->d_parent; /* dir inode is locked */
20556+ di_write_lock_parent(parent);
20557+ wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
20558+ &a->pin, &wr_dir_args);
20559+ err = PTR_ERR(wh_dentry);
20560+ if (IS_ERR(wh_dentry))
027c5e7a 20561+ goto out_parent;
4a4d8108
AM
20562+
20563+ sb = dentry->d_sb;
5afbbe0d 20564+ bindex = au_dbtop(dentry);
4a4d8108
AM
20565+ h_path.dentry = au_h_dptr(dentry, bindex);
20566+ h_path.mnt = au_sbr_mnt(sb, bindex);
20567+ err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
20568+ if (unlikely(err))
027c5e7a 20569+ goto out_unpin;
4a4d8108
AM
20570+
20571+ /* make the dir opaque */
20572+ diropq = 0;
febd17d6 20573+ h_inode = d_inode(h_path.dentry);
4a4d8108
AM
20574+ if (wh_dentry
20575+ || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
febd17d6 20576+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 20577+ opq_dentry = au_diropq_create(dentry, bindex);
febd17d6 20578+ inode_unlock(h_inode);
4a4d8108
AM
20579+ err = PTR_ERR(opq_dentry);
20580+ if (IS_ERR(opq_dentry))
20581+ goto out_dir;
20582+ dput(opq_dentry);
20583+ diropq = 1;
20584+ }
20585+
20586+ err = epilog(dir, bindex, wh_dentry, dentry);
20587+ if (!err) {
20588+ inc_nlink(dir);
027c5e7a 20589+ goto out_unpin; /* success */
4a4d8108
AM
20590+ }
20591+
20592+ /* revert */
20593+ if (diropq) {
20594+ AuLabel(revert opq);
febd17d6 20595+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 20596+ rerr = au_diropq_remove(dentry, bindex);
febd17d6 20597+ inode_unlock(h_inode);
4a4d8108 20598+ if (rerr) {
523b37e3
AM
20599+ AuIOErr("%pd reverting diropq failed(%d, %d)\n",
20600+ dentry, err, rerr);
4a4d8108
AM
20601+ err = -EIO;
20602+ }
20603+ }
20604+
4f0767ce 20605+out_dir:
4a4d8108
AM
20606+ AuLabel(revert dir);
20607+ rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
20608+ if (rerr) {
523b37e3
AM
20609+ AuIOErr("%pd reverting dir failed(%d, %d)\n",
20610+ dentry, err, rerr);
4a4d8108
AM
20611+ err = -EIO;
20612+ }
4a4d8108 20613+ au_dtime_revert(&a->dt);
027c5e7a 20614+out_unpin:
4a4d8108
AM
20615+ au_unpin(&a->pin);
20616+ dput(wh_dentry);
027c5e7a
AM
20617+out_parent:
20618+ di_write_unlock(parent);
20619+out_unlock:
4a4d8108 20620+ if (unlikely(err)) {
5afbbe0d 20621+ au_update_dbtop(dentry);
4a4d8108
AM
20622+ d_drop(dentry);
20623+ }
4a4d8108 20624+ aufs_read_unlock(dentry, AuLock_DW);
027c5e7a 20625+out_free:
e49925d1 20626+ au_kfree_rcu(a);
4f0767ce 20627+out:
4a4d8108
AM
20628+ return err;
20629+}
7f207e10
AM
20630diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
20631--- /usr/share/empty/fs/aufs/i_op.c 1970-01-01 01:00:00.000000000 +0100
0b487229
JR
20632+++ linux/fs/aufs/i_op.c 2021-02-22 23:30:37.702680911 +0100
20633@@ -0,0 +1,1505 @@
cd7a4cd9 20634+// SPDX-License-Identifier: GPL-2.0
4a4d8108 20635+/*
0b487229 20636+ * Copyright (C) 2005-2020 Junjiro R. Okajima
4a4d8108
AM
20637+ *
20638+ * This program, aufs is free software; you can redistribute it and/or modify
20639+ * it under the terms of the GNU General Public License as published by
20640+ * the Free Software Foundation; either version 2 of the License, or
20641+ * (at your option) any later version.
20642+ *
20643+ * This program is distributed in the hope that it will be useful,
20644+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20645+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20646+ * GNU General Public License for more details.
20647+ *
20648+ * You should have received a copy of the GNU General Public License
523b37e3 20649+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 20650+ */
1facf9fc 20651+
1308ab2a 20652+/*
4a4d8108 20653+ * inode operations (except add/del/rename)
1308ab2a 20654+ */
4a4d8108
AM
20655+
20656+#include <linux/device_cgroup.h>
20657+#include <linux/fs_stack.h>
0b487229 20658+#include <linux/iversion.h>
4a4d8108
AM
20659+#include <linux/namei.h>
20660+#include <linux/security.h>
4a4d8108
AM
20661+#include "aufs.h"
20662+
1e00d052 20663+static int h_permission(struct inode *h_inode, int mask,
79b8bda9 20664+ struct path *h_path, int brperm)
1facf9fc 20665+{
1308ab2a 20666+ int err;
4a4d8108 20667+ const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
1facf9fc 20668+
e2f27e51
AM
20669+ err = -EPERM;
20670+ if (write_mask && IS_IMMUTABLE(h_inode))
20671+ goto out;
20672+
4a4d8108 20673+ err = -EACCES;
e2f27e51
AM
20674+ if (((mask & MAY_EXEC)
20675+ && S_ISREG(h_inode->i_mode)
20676+ && (path_noexec(h_path)
cd7a4cd9 20677+ || !(h_inode->i_mode & 0111))))
4a4d8108
AM
20678+ goto out;
20679+
20680+ /*
20681+ * - skip the lower fs test in the case of write to ro branch.
20682+ * - nfs dir permission write check is optimized, but a policy for
20683+ * link/rename requires a real check.
2121bcd9 20684+ * - nfs always sets SB_POSIXACL regardless its mount option 'noacl.'
b912730e 20685+ * in this case, generic_permission() returns -EOPNOTSUPP.
4a4d8108
AM
20686+ */
20687+ if ((write_mask && !au_br_writable(brperm))
20688+ || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
20689+ && write_mask && !(mask & MAY_READ))
20690+ || !h_inode->i_op->permission) {
20691+ /* AuLabel(generic_permission); */
062440b3 20692+ /* AuDbg("get_acl %ps\n", h_inode->i_op->get_acl); */
1e00d052 20693+ err = generic_permission(h_inode, mask);
b912730e
AM
20694+ if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
20695+ err = h_inode->i_op->permission(h_inode, mask);
20696+ AuTraceErr(err);
1308ab2a 20697+ } else {
4a4d8108 20698+ /* AuLabel(h_inode->permission); */
1e00d052 20699+ err = h_inode->i_op->permission(h_inode, mask);
4a4d8108
AM
20700+ AuTraceErr(err);
20701+ }
1facf9fc 20702+
4a4d8108
AM
20703+ if (!err)
20704+ err = devcgroup_inode_permission(h_inode, mask);
7f207e10 20705+ if (!err)
4a4d8108 20706+ err = security_inode_permission(h_inode, mask);
4a4d8108 20707+
4f0767ce 20708+out:
1308ab2a 20709+ return err;
20710+}
dece6358 20711+
1e00d052 20712+static int aufs_permission(struct inode *inode, int mask)
1308ab2a 20713+{
20714+ int err;
5afbbe0d 20715+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
20716+ const unsigned char isdir = !!S_ISDIR(inode->i_mode),
20717+ write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
20718+ struct inode *h_inode;
20719+ struct super_block *sb;
20720+ struct au_branch *br;
1facf9fc 20721+
027c5e7a 20722+ /* todo: support rcu-walk? */
1e00d052 20723+ if (mask & MAY_NOT_BLOCK)
027c5e7a
AM
20724+ return -ECHILD;
20725+
4a4d8108
AM
20726+ sb = inode->i_sb;
20727+ si_read_lock(sb, AuLock_FLUSH);
20728+ ii_read_lock_child(inode);
0b487229
JR
20729+#if 0 /* reserved for future use */
20730+ /*
20731+ * This test may be rather 'too much' since the test is essentially done
20732+ * in the aufs_lookup(). Theoretically it is possible that the inode
20733+ * generation doesn't match to the superblock's here. But it isn't a
20734+ * big deal I suppose.
20735+ */
027c5e7a
AM
20736+ err = au_iigen_test(inode, au_sigen(sb));
20737+ if (unlikely(err))
20738+ goto out;
20739+#endif
dece6358 20740+
076b876e
AM
20741+ if (!isdir
20742+ || write_mask
20743+ || au_opt_test(au_mntflags(sb), DIRPERM1)) {
4a4d8108 20744+ err = au_busy_or_stale();
5afbbe0d 20745+ h_inode = au_h_iptr(inode, au_ibtop(inode));
4a4d8108
AM
20746+ if (unlikely(!h_inode
20747+ || (h_inode->i_mode & S_IFMT)
20748+ != (inode->i_mode & S_IFMT)))
20749+ goto out;
1facf9fc 20750+
4a4d8108 20751+ err = 0;
5afbbe0d 20752+ bindex = au_ibtop(inode);
4a4d8108 20753+ br = au_sbr(sb, bindex);
79b8bda9 20754+ err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
4a4d8108
AM
20755+ if (write_mask
20756+ && !err
20757+ && !special_file(h_inode->i_mode)) {
20758+ /* test whether the upper writable branch exists */
20759+ err = -EROFS;
20760+ for (; bindex >= 0; bindex--)
20761+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
20762+ err = 0;
20763+ break;
20764+ }
20765+ }
20766+ goto out;
20767+ }
dece6358 20768+
4a4d8108 20769+ /* non-write to dir */
1308ab2a 20770+ err = 0;
5afbbe0d
AM
20771+ bbot = au_ibbot(inode);
20772+ for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
4a4d8108
AM
20773+ h_inode = au_h_iptr(inode, bindex);
20774+ if (h_inode) {
20775+ err = au_busy_or_stale();
20776+ if (unlikely(!S_ISDIR(h_inode->i_mode)))
20777+ break;
20778+
20779+ br = au_sbr(sb, bindex);
79b8bda9 20780+ err = h_permission(h_inode, mask, &br->br_path,
4a4d8108
AM
20781+ br->br_perm);
20782+ }
20783+ }
1308ab2a 20784+
4f0767ce 20785+out:
4a4d8108
AM
20786+ ii_read_unlock(inode);
20787+ si_read_unlock(sb);
1308ab2a 20788+ return err;
20789+}
20790+
4a4d8108 20791+/* ---------------------------------------------------------------------- */
1facf9fc 20792+
4a4d8108 20793+static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
b4510431 20794+ unsigned int flags)
4a4d8108
AM
20795+{
20796+ struct dentry *ret, *parent;
b752ccd1 20797+ struct inode *inode;
4a4d8108 20798+ struct super_block *sb;
1716fcea 20799+ int err, npositive;
dece6358 20800+
4a4d8108 20801+ IMustLock(dir);
1308ab2a 20802+
537831f9
AM
20803+ /* todo: support rcu-walk? */
20804+ ret = ERR_PTR(-ECHILD);
20805+ if (flags & LOOKUP_RCU)
20806+ goto out;
20807+
20808+ ret = ERR_PTR(-ENAMETOOLONG);
20809+ if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20810+ goto out;
20811+
4a4d8108 20812+ sb = dir->i_sb;
7f207e10
AM
20813+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
20814+ ret = ERR_PTR(err);
20815+ if (unlikely(err))
20816+ goto out;
20817+
4a4d8108
AM
20818+ err = au_di_init(dentry);
20819+ ret = ERR_PTR(err);
20820+ if (unlikely(err))
7f207e10 20821+ goto out_si;
1308ab2a 20822+
9dbd164d 20823+ inode = NULL;
027c5e7a 20824+ npositive = 0; /* suppress a warning */
4a4d8108
AM
20825+ parent = dentry->d_parent; /* dir inode is locked */
20826+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a
AM
20827+ err = au_alive_dir(parent);
20828+ if (!err)
20829+ err = au_digen_test(parent, au_sigen(sb));
20830+ if (!err) {
5afbbe0d
AM
20831+ /* regardless LOOKUP_CREATE, always ALLOW_NEG */
20832+ npositive = au_lkup_dentry(dentry, au_dbtop(parent),
20833+ AuLkup_ALLOW_NEG);
027c5e7a
AM
20834+ err = npositive;
20835+ }
4a4d8108 20836+ di_read_unlock(parent, AuLock_IR);
4a4d8108
AM
20837+ ret = ERR_PTR(err);
20838+ if (unlikely(err < 0))
20839+ goto out_unlock;
1308ab2a 20840+
4a4d8108 20841+ if (npositive) {
b752ccd1 20842+ inode = au_new_inode(dentry, /*must_new*/0);
c1595e42
JR
20843+ if (IS_ERR(inode)) {
20844+ ret = (void *)inode;
20845+ inode = NULL;
20846+ goto out_unlock;
20847+ }
9dbd164d 20848+ }
4a4d8108 20849+
c1595e42
JR
20850+ if (inode)
20851+ atomic_inc(&inode->i_count);
4a4d8108 20852+ ret = d_splice_alias(inode, dentry);
0b487229 20853+#if 0 /* reserved for future use */
537831f9
AM
20854+ if (unlikely(d_need_lookup(dentry))) {
20855+ spin_lock(&dentry->d_lock);
20856+ dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
20857+ spin_unlock(&dentry->d_lock);
20858+ } else
20859+#endif
c1595e42 20860+ if (inode) {
2000de60 20861+ if (!IS_ERR(ret)) {
c1595e42 20862+ iput(inode);
2000de60
JR
20863+ if (ret && ret != dentry)
20864+ ii_write_unlock(inode);
20865+ } else {
c1595e42
JR
20866+ ii_write_unlock(inode);
20867+ iput(inode);
20868+ inode = NULL;
20869+ }
7f207e10 20870+ }
1facf9fc 20871+
4f0767ce 20872+out_unlock:
4a4d8108 20873+ di_write_unlock(dentry);
7f207e10 20874+out_si:
4a4d8108 20875+ si_read_unlock(sb);
7f207e10 20876+out:
4a4d8108
AM
20877+ return ret;
20878+}
1facf9fc 20879+
4a4d8108 20880+/* ---------------------------------------------------------------------- */
1facf9fc 20881+
acd2b654
AM
20882+/*
20883+ * very dirty and complicated aufs ->atomic_open().
20884+ * aufs_atomic_open()
20885+ * + au_aopen_or_create()
20886+ * + add_simple()
20887+ * + vfsub_atomic_open()
20888+ * + branch fs ->atomic_open()
20889+ * may call the actual 'open' for h_file
20890+ * + inc br_nfiles only if opened
20891+ * + au_aopen_no_open() or au_aopen_do_open()
20892+ *
20893+ * au_aopen_do_open()
20894+ * + finish_open()
20895+ * + au_do_aopen()
20896+ * + au_do_open() the body of all 'open'
20897+ * + au_do_open_nondir()
20898+ * set the passed h_file
20899+ *
20900+ * au_aopen_no_open()
20901+ * + finish_no_open()
20902+ */
20903+
b912730e 20904+struct aopen_node {
8b6a4947 20905+ struct hlist_bl_node hblist;
b912730e
AM
20906+ struct file *file, *h_file;
20907+};
20908+
20909+static int au_do_aopen(struct inode *inode, struct file *file)
20910+{
8b6a4947
AM
20911+ struct hlist_bl_head *aopen;
20912+ struct hlist_bl_node *pos;
b912730e
AM
20913+ struct aopen_node *node;
20914+ struct au_do_open_args args = {
8b6a4947
AM
20915+ .aopen = 1,
20916+ .open = au_do_open_nondir
b912730e
AM
20917+ };
20918+
20919+ aopen = &au_sbi(inode->i_sb)->si_aopen;
8b6a4947
AM
20920+ hlist_bl_lock(aopen);
20921+ hlist_bl_for_each_entry(node, pos, aopen, hblist)
b912730e
AM
20922+ if (node->file == file) {
20923+ args.h_file = node->h_file;
20924+ break;
20925+ }
8b6a4947 20926+ hlist_bl_unlock(aopen);
b912730e
AM
20927+ /* AuDebugOn(!args.h_file); */
20928+
20929+ return au_do_open(file, &args);
20930+}
20931+
acd2b654
AM
20932+static int au_aopen_do_open(struct file *file, struct dentry *dentry,
20933+ struct aopen_node *aopen_node)
20934+{
20935+ int err;
20936+ struct hlist_bl_head *aopen;
20937+
20938+ AuLabel(here);
20939+ aopen = &au_sbi(dentry->d_sb)->si_aopen;
20940+ au_hbl_add(&aopen_node->hblist, aopen);
20941+ err = finish_open(file, dentry, au_do_aopen);
20942+ au_hbl_del(&aopen_node->hblist, aopen);
20943+ /* AuDbgFile(file); */
20944+ AuDbg("%pd%s%s\n", dentry,
20945+ (file->f_mode & FMODE_CREATED) ? " created" : "",
20946+ (file->f_mode & FMODE_OPENED) ? " opened" : "");
20947+
20948+ AuTraceErr(err);
20949+ return err;
20950+}
20951+
20952+static int au_aopen_no_open(struct file *file, struct dentry *dentry)
20953+{
20954+ int err;
20955+
20956+ AuLabel(here);
20957+ dget(dentry);
20958+ err = finish_no_open(file, dentry);
20959+
20960+ AuTraceErr(err);
20961+ return err;
20962+}
20963+
b912730e
AM
20964+static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
20965+ struct file *file, unsigned int open_flag,
acd2b654 20966+ umode_t create_mode)
b912730e 20967+{
acd2b654 20968+ int err, did_open;
5afbbe0d 20969+ unsigned int lkup_flags;
acd2b654
AM
20970+ aufs_bindex_t bindex;
20971+ struct super_block *sb;
f0c0a007 20972+ struct dentry *parent, *d;
b912730e
AM
20973+ struct vfsub_aopen_args args = {
20974+ .open_flag = open_flag,
acd2b654 20975+ .create_mode = create_mode
b912730e
AM
20976+ };
20977+ struct aopen_node aopen_node = {
20978+ .file = file
20979+ };
20980+
20981+ IMustLock(dir);
5afbbe0d 20982+ AuDbg("open_flag 0%o\n", open_flag);
b912730e
AM
20983+ AuDbgDentry(dentry);
20984+
20985+ err = 0;
20986+ if (!au_di(dentry)) {
5afbbe0d
AM
20987+ lkup_flags = LOOKUP_OPEN;
20988+ if (open_flag & O_CREAT)
20989+ lkup_flags |= LOOKUP_CREATE;
20990+ d = aufs_lookup(dir, dentry, lkup_flags);
b912730e
AM
20991+ if (IS_ERR(d)) {
20992+ err = PTR_ERR(d);
5afbbe0d 20993+ AuTraceErr(err);
b912730e
AM
20994+ goto out;
20995+ } else if (d) {
20996+ /*
20997+ * obsoleted dentry found.
20998+ * another error will be returned later.
20999+ */
21000+ d_drop(d);
b912730e 21001+ AuDbgDentry(d);
5afbbe0d 21002+ dput(d);
b912730e
AM
21003+ }
21004+ AuDbgDentry(dentry);
21005+ }
21006+
21007+ if (d_is_positive(dentry)
21008+ || d_unhashed(dentry)
21009+ || d_unlinked(dentry)
acd2b654
AM
21010+ || !(open_flag & O_CREAT)) {
21011+ err = au_aopen_no_open(file, dentry);
21012+ goto out; /* success */
21013+ }
b912730e
AM
21014+
21015+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
21016+ if (unlikely(err))
21017+ goto out;
21018+
acd2b654 21019+ sb = dentry->d_sb;
b912730e
AM
21020+ parent = dentry->d_parent; /* dir is locked */
21021+ di_write_lock_parent(parent);
5afbbe0d 21022+ err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
acd2b654
AM
21023+ if (unlikely(err < 0))
21024+ goto out_parent;
b912730e
AM
21025+
21026+ AuDbgDentry(dentry);
acd2b654
AM
21027+ if (d_is_positive(dentry)) {
21028+ err = au_aopen_no_open(file, dentry);
21029+ goto out_parent; /* success */
21030+ }
b912730e 21031+
acd2b654 21032+ args.file = alloc_empty_file(file->f_flags, current_cred());
b912730e
AM
21033+ err = PTR_ERR(args.file);
21034+ if (IS_ERR(args.file))
acd2b654 21035+ goto out_parent;
b912730e 21036+
acd2b654 21037+ bindex = au_dbtop(dentry);
b912730e
AM
21038+ err = au_aopen_or_create(dir, dentry, &args);
21039+ AuTraceErr(err);
21040+ AuDbgFile(args.file);
acd2b654
AM
21041+ file->f_mode = args.file->f_mode & ~FMODE_OPENED;
21042+ did_open = !!(args.file->f_mode & FMODE_OPENED);
21043+ if (!did_open) {
21044+ fput(args.file);
21045+ args.file = NULL;
b912730e 21046+ }
8b6a4947
AM
21047+ di_write_unlock(parent);
21048+ di_write_unlock(dentry);
acd2b654
AM
21049+ if (unlikely(err < 0)) {
21050+ if (args.file)
21051+ fput(args.file);
21052+ goto out_sb;
b912730e 21053+ }
b912730e 21054+
acd2b654
AM
21055+ if (!did_open)
21056+ err = au_aopen_no_open(file, dentry);
8b6a4947 21057+ else {
acd2b654
AM
21058+ aopen_node.h_file = args.file;
21059+ err = au_aopen_do_open(file, dentry, &aopen_node);
8b6a4947 21060+ }
acd2b654
AM
21061+ if (unlikely(err < 0)) {
21062+ if (args.file)
21063+ fput(args.file);
21064+ if (did_open)
21065+ au_lcnt_dec(&args.br->br_nfiles);
b912730e 21066+ }
acd2b654
AM
21067+ goto out_sb; /* success */
21068+
21069+out_parent:
21070+ di_write_unlock(parent);
21071+ di_write_unlock(dentry);
21072+out_sb:
21073+ si_read_unlock(sb);
b912730e 21074+out:
b912730e 21075+ AuTraceErr(err);
acd2b654 21076+ AuDbgFile(file);
b912730e
AM
21077+ return err;
21078+}
21079+
21080+
21081+/* ---------------------------------------------------------------------- */
21082+
4a4d8108
AM
21083+static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
21084+ const unsigned char add_entry, aufs_bindex_t bcpup,
5afbbe0d 21085+ aufs_bindex_t btop)
4a4d8108
AM
21086+{
21087+ int err;
21088+ struct dentry *h_parent;
21089+ struct inode *h_dir;
1facf9fc 21090+
027c5e7a 21091+ if (add_entry)
5527c038 21092+ IMustLock(d_inode(parent));
027c5e7a 21093+ else
4a4d8108
AM
21094+ di_write_lock_parent(parent);
21095+
21096+ err = 0;
21097+ if (!au_h_dptr(parent, bcpup)) {
5afbbe0d 21098+ if (btop > bcpup)
c2b27bf2 21099+ err = au_cpup_dirs(dentry, bcpup);
5afbbe0d 21100+ else if (btop < bcpup)
4a4d8108
AM
21101+ err = au_cpdown_dirs(dentry, bcpup);
21102+ else
c2b27bf2 21103+ BUG();
4a4d8108 21104+ }
38d290e6 21105+ if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
4a4d8108 21106+ h_parent = au_h_dptr(parent, bcpup);
5527c038 21107+ h_dir = d_inode(h_parent);
be118d29 21108+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
7e9cd9fe 21109+ err = au_lkup_neg(dentry, bcpup, /*wh*/0);
4a4d8108 21110+ /* todo: no unlock here */
3c1bdaff 21111+ inode_unlock_shared(h_dir);
027c5e7a
AM
21112+
21113+ AuDbg("bcpup %d\n", bcpup);
21114+ if (!err) {
5527c038 21115+ if (d_really_is_negative(dentry))
5afbbe0d 21116+ au_set_h_dptr(dentry, btop, NULL);
4a4d8108
AM
21117+ au_update_dbrange(dentry, /*do_put_zero*/0);
21118+ }
1308ab2a 21119+ }
1facf9fc 21120+
4a4d8108
AM
21121+ if (!add_entry)
21122+ di_write_unlock(parent);
21123+ if (!err)
21124+ err = bcpup; /* success */
1308ab2a 21125+
027c5e7a 21126+ AuTraceErr(err);
4a4d8108
AM
21127+ return err;
21128+}
1facf9fc 21129+
4a4d8108
AM
21130+/*
21131+ * decide the branch and the parent dir where we will create a new entry.
21132+ * returns new bindex or an error.
21133+ * copyup the parent dir if needed.
21134+ */
21135+int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
21136+ struct au_wr_dir_args *args)
21137+{
21138+ int err;
392086de 21139+ unsigned int flags;
5afbbe0d 21140+ aufs_bindex_t bcpup, btop, src_btop;
86dc4139
AM
21141+ const unsigned char add_entry
21142+ = au_ftest_wrdir(args->flags, ADD_ENTRY)
38d290e6 21143+ | au_ftest_wrdir(args->flags, TMPFILE);
4a4d8108
AM
21144+ struct super_block *sb;
21145+ struct dentry *parent;
21146+ struct au_sbinfo *sbinfo;
1facf9fc 21147+
4a4d8108
AM
21148+ sb = dentry->d_sb;
21149+ sbinfo = au_sbi(sb);
21150+ parent = dget_parent(dentry);
5afbbe0d
AM
21151+ btop = au_dbtop(dentry);
21152+ bcpup = btop;
4a4d8108
AM
21153+ if (args->force_btgt < 0) {
21154+ if (src_dentry) {
5afbbe0d
AM
21155+ src_btop = au_dbtop(src_dentry);
21156+ if (src_btop < btop)
21157+ bcpup = src_btop;
4a4d8108 21158+ } else if (add_entry) {
392086de
AM
21159+ flags = 0;
21160+ if (au_ftest_wrdir(args->flags, ISDIR))
21161+ au_fset_wbr(flags, DIR);
21162+ err = AuWbrCreate(sbinfo, dentry, flags);
4a4d8108
AM
21163+ bcpup = err;
21164+ }
1facf9fc 21165+
5527c038 21166+ if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
4a4d8108
AM
21167+ if (add_entry)
21168+ err = AuWbrCopyup(sbinfo, dentry);
21169+ else {
21170+ if (!IS_ROOT(dentry)) {
21171+ di_read_lock_parent(parent, !AuLock_IR);
21172+ err = AuWbrCopyup(sbinfo, dentry);
21173+ di_read_unlock(parent, !AuLock_IR);
21174+ } else
21175+ err = AuWbrCopyup(sbinfo, dentry);
21176+ }
21177+ bcpup = err;
21178+ if (unlikely(err < 0))
21179+ goto out;
21180+ }
21181+ } else {
21182+ bcpup = args->force_btgt;
5527c038 21183+ AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
1308ab2a 21184+ }
027c5e7a 21185+
5afbbe0d 21186+ AuDbg("btop %d, bcpup %d\n", btop, bcpup);
4a4d8108 21187+ err = bcpup;
5afbbe0d 21188+ if (bcpup == btop)
4a4d8108 21189+ goto out; /* success */
4a4d8108
AM
21190+
21191+ /* copyup the new parent into the branch we process */
5afbbe0d 21192+ err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
027c5e7a 21193+ if (err >= 0) {
5527c038 21194+ if (d_really_is_negative(dentry)) {
5afbbe0d
AM
21195+ au_set_h_dptr(dentry, btop, NULL);
21196+ au_set_dbtop(dentry, bcpup);
21197+ au_set_dbbot(dentry, bcpup);
027c5e7a 21198+ }
38d290e6
JR
21199+ AuDebugOn(add_entry
21200+ && !au_ftest_wrdir(args->flags, TMPFILE)
21201+ && !au_h_dptr(dentry, bcpup));
027c5e7a 21202+ }
86dc4139
AM
21203+
21204+out:
21205+ dput(parent);
21206+ return err;
21207+}
21208+
21209+/* ---------------------------------------------------------------------- */
21210+
21211+void au_pin_hdir_unlock(struct au_pin *p)
21212+{
21213+ if (p->hdir)
5afbbe0d 21214+ au_hn_inode_unlock(p->hdir);
86dc4139
AM
21215+}
21216+
c1595e42 21217+int au_pin_hdir_lock(struct au_pin *p)
86dc4139
AM
21218+{
21219+ int err;
21220+
21221+ err = 0;
21222+ if (!p->hdir)
21223+ goto out;
21224+
21225+ /* even if an error happens later, keep this lock */
5afbbe0d 21226+ au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
86dc4139
AM
21227+
21228+ err = -EBUSY;
5527c038 21229+ if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
86dc4139
AM
21230+ goto out;
21231+
21232+ err = 0;
21233+ if (p->h_dentry)
21234+ err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
21235+ p->h_parent, p->br);
21236+
21237+out:
21238+ return err;
21239+}
21240+
21241+int au_pin_hdir_relock(struct au_pin *p)
21242+{
21243+ int err, i;
21244+ struct inode *h_i;
21245+ struct dentry *h_d[] = {
21246+ p->h_dentry,
21247+ p->h_parent
21248+ };
21249+
21250+ err = au_pin_hdir_lock(p);
21251+ if (unlikely(err))
21252+ goto out;
21253+
21254+ for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
21255+ if (!h_d[i])
21256+ continue;
5527c038
JR
21257+ if (d_is_positive(h_d[i])) {
21258+ h_i = d_inode(h_d[i]);
86dc4139 21259+ err = !h_i->i_nlink;
5527c038 21260+ }
86dc4139
AM
21261+ }
21262+
21263+out:
21264+ return err;
21265+}
21266+
5afbbe0d 21267+static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
86dc4139 21268+{
0b487229
JR
21269+#if !defined(CONFIG_RWSEM_GENERIC_SPINLOCK) \
21270+ && defined(CONFIG_RWSEM_SPIN_ON_OWNER)
5afbbe0d 21271+ p->hdir->hi_inode->i_rwsem.owner = task;
86dc4139
AM
21272+#endif
21273+}
21274+
21275+void au_pin_hdir_acquire_nest(struct au_pin *p)
21276+{
21277+ if (p->hdir) {
5afbbe0d 21278+ rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
86dc4139
AM
21279+ p->lsc_hi, 0, NULL, _RET_IP_);
21280+ au_pin_hdir_set_owner(p, current);
21281+ }
dece6358 21282+}
1facf9fc 21283+
86dc4139
AM
21284+void au_pin_hdir_release(struct au_pin *p)
21285+{
21286+ if (p->hdir) {
21287+ au_pin_hdir_set_owner(p, p->task);
5afbbe0d 21288+ rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, 1, _RET_IP_);
86dc4139
AM
21289+ }
21290+}
1308ab2a 21291+
4a4d8108 21292+struct dentry *au_pinned_h_parent(struct au_pin *pin)
1308ab2a 21293+{
4a4d8108
AM
21294+ if (pin && pin->parent)
21295+ return au_h_dptr(pin->parent, pin->bindex);
21296+ return NULL;
dece6358 21297+}
1facf9fc 21298+
4a4d8108 21299+void au_unpin(struct au_pin *p)
dece6358 21300+{
86dc4139
AM
21301+ if (p->hdir)
21302+ au_pin_hdir_unlock(p);
e49829fe 21303+ if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
b4510431 21304+ vfsub_mnt_drop_write(p->h_mnt);
4a4d8108
AM
21305+ if (!p->hdir)
21306+ return;
1facf9fc 21307+
4a4d8108
AM
21308+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21309+ di_read_unlock(p->parent, AuLock_IR);
21310+ iput(p->hdir->hi_inode);
21311+ dput(p->parent);
21312+ p->parent = NULL;
21313+ p->hdir = NULL;
21314+ p->h_mnt = NULL;
86dc4139 21315+ /* do not clear p->task */
4a4d8108 21316+}
1308ab2a 21317+
4a4d8108
AM
21318+int au_do_pin(struct au_pin *p)
21319+{
21320+ int err;
21321+ struct super_block *sb;
4a4d8108
AM
21322+ struct inode *h_dir;
21323+
21324+ err = 0;
21325+ sb = p->dentry->d_sb;
86dc4139 21326+ p->br = au_sbr(sb, p->bindex);
4a4d8108
AM
21327+ if (IS_ROOT(p->dentry)) {
21328+ if (au_ftest_pin(p->flags, MNT_WRITE)) {
86dc4139 21329+ p->h_mnt = au_br_mnt(p->br);
b4510431 21330+ err = vfsub_mnt_want_write(p->h_mnt);
4a4d8108
AM
21331+ if (unlikely(err)) {
21332+ au_fclr_pin(p->flags, MNT_WRITE);
21333+ goto out_err;
21334+ }
21335+ }
dece6358 21336+ goto out;
1facf9fc 21337+ }
21338+
86dc4139 21339+ p->h_dentry = NULL;
5afbbe0d 21340+ if (p->bindex <= au_dbbot(p->dentry))
86dc4139 21341+ p->h_dentry = au_h_dptr(p->dentry, p->bindex);
dece6358 21342+
4a4d8108
AM
21343+ p->parent = dget_parent(p->dentry);
21344+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21345+ di_read_lock(p->parent, AuLock_IR, p->lsc_di);
dece6358 21346+
4a4d8108 21347+ h_dir = NULL;
86dc4139 21348+ p->h_parent = au_h_dptr(p->parent, p->bindex);
5527c038 21349+ p->hdir = au_hi(d_inode(p->parent), p->bindex);
4a4d8108
AM
21350+ if (p->hdir)
21351+ h_dir = p->hdir->hi_inode;
dece6358 21352+
b752ccd1
AM
21353+ /*
21354+ * udba case, or
21355+ * if DI_LOCKED is not set, then p->parent may be different
21356+ * and h_parent can be NULL.
21357+ */
86dc4139 21358+ if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
e49829fe 21359+ err = -EBUSY;
4a4d8108
AM
21360+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21361+ di_read_unlock(p->parent, AuLock_IR);
21362+ dput(p->parent);
21363+ p->parent = NULL;
21364+ goto out_err;
21365+ }
1308ab2a 21366+
4a4d8108 21367+ if (au_ftest_pin(p->flags, MNT_WRITE)) {
86dc4139 21368+ p->h_mnt = au_br_mnt(p->br);
b4510431 21369+ err = vfsub_mnt_want_write(p->h_mnt);
dece6358 21370+ if (unlikely(err)) {
4a4d8108 21371+ au_fclr_pin(p->flags, MNT_WRITE);
86dc4139
AM
21372+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21373+ di_read_unlock(p->parent, AuLock_IR);
21374+ dput(p->parent);
21375+ p->parent = NULL;
21376+ goto out_err;
dece6358
AM
21377+ }
21378+ }
4a4d8108 21379+
86dc4139
AM
21380+ au_igrab(h_dir);
21381+ err = au_pin_hdir_lock(p);
21382+ if (!err)
21383+ goto out; /* success */
21384+
076b876e
AM
21385+ au_unpin(p);
21386+
4f0767ce 21387+out_err:
4a4d8108
AM
21388+ pr_err("err %d\n", err);
21389+ err = au_busy_or_stale();
4f0767ce 21390+out:
1facf9fc 21391+ return err;
21392+}
21393+
4a4d8108
AM
21394+void au_pin_init(struct au_pin *p, struct dentry *dentry,
21395+ aufs_bindex_t bindex, int lsc_di, int lsc_hi,
21396+ unsigned int udba, unsigned char flags)
21397+{
21398+ p->dentry = dentry;
21399+ p->udba = udba;
21400+ p->lsc_di = lsc_di;
21401+ p->lsc_hi = lsc_hi;
21402+ p->flags = flags;
21403+ p->bindex = bindex;
21404+
21405+ p->parent = NULL;
21406+ p->hdir = NULL;
21407+ p->h_mnt = NULL;
86dc4139
AM
21408+
21409+ p->h_dentry = NULL;
21410+ p->h_parent = NULL;
21411+ p->br = NULL;
21412+ p->task = current;
4a4d8108
AM
21413+}
21414+
21415+int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
21416+ unsigned int udba, unsigned char flags)
21417+{
21418+ au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
21419+ udba, flags);
21420+ return au_do_pin(pin);
21421+}
21422+
dece6358
AM
21423+/* ---------------------------------------------------------------------- */
21424+
1308ab2a 21425+/*
4a4d8108
AM
21426+ * ->setattr() and ->getattr() are called in various cases.
21427+ * chmod, stat: dentry is revalidated.
21428+ * fchmod, fstat: file and dentry are not revalidated, additionally they may be
21429+ * unhashed.
21430+ * for ->setattr(), ia->ia_file is passed from ftruncate only.
1308ab2a 21431+ */
027c5e7a 21432+/* todo: consolidate with do_refresh() and simple_reval_dpath() */
c1595e42 21433+int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
1facf9fc 21434+{
4a4d8108 21435+ int err;
4a4d8108 21436+ struct dentry *parent;
1facf9fc 21437+
1308ab2a 21438+ err = 0;
027c5e7a 21439+ if (au_digen_test(dentry, sigen)) {
4a4d8108
AM
21440+ parent = dget_parent(dentry);
21441+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 21442+ err = au_refresh_dentry(dentry, parent);
4a4d8108
AM
21443+ di_read_unlock(parent, AuLock_IR);
21444+ dput(parent);
dece6358 21445+ }
1facf9fc 21446+
4a4d8108 21447+ AuTraceErr(err);
1308ab2a 21448+ return err;
21449+}
dece6358 21450+
c1595e42
JR
21451+int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
21452+ struct au_icpup_args *a)
1308ab2a 21453+{
21454+ int err;
4a4d8108 21455+ loff_t sz;
5afbbe0d 21456+ aufs_bindex_t btop, ibtop;
4a4d8108
AM
21457+ struct dentry *hi_wh, *parent;
21458+ struct inode *inode;
4a4d8108
AM
21459+ struct au_wr_dir_args wr_dir_args = {
21460+ .force_btgt = -1,
21461+ .flags = 0
21462+ };
21463+
2000de60 21464+ if (d_is_dir(dentry))
4a4d8108
AM
21465+ au_fset_wrdir(wr_dir_args.flags, ISDIR);
21466+ /* plink or hi_wh() case */
5afbbe0d 21467+ btop = au_dbtop(dentry);
5527c038 21468+ inode = d_inode(dentry);
5afbbe0d
AM
21469+ ibtop = au_ibtop(inode);
21470+ if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
21471+ wr_dir_args.force_btgt = ibtop;
4a4d8108
AM
21472+ err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
21473+ if (unlikely(err < 0))
21474+ goto out;
21475+ a->btgt = err;
5afbbe0d 21476+ if (err != btop)
4a4d8108
AM
21477+ au_fset_icpup(a->flags, DID_CPUP);
21478+
21479+ err = 0;
21480+ a->pin_flags = AuPin_MNT_WRITE;
21481+ parent = NULL;
21482+ if (!IS_ROOT(dentry)) {
21483+ au_fset_pin(a->pin_flags, DI_LOCKED);
21484+ parent = dget_parent(dentry);
21485+ di_write_lock_parent(parent);
21486+ }
21487+
21488+ err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
21489+ if (unlikely(err))
21490+ goto out_parent;
21491+
4a4d8108 21492+ sz = -1;
5afbbe0d 21493+ a->h_path.dentry = au_h_dptr(dentry, btop);
5527c038 21494+ a->h_inode = d_inode(a->h_path.dentry);
c1595e42 21495+ if (ia && (ia->ia_valid & ATTR_SIZE)) {
be118d29 21496+ inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
c1595e42
JR
21497+ if (ia->ia_size < i_size_read(a->h_inode))
21498+ sz = ia->ia_size;
3c1bdaff 21499+ inode_unlock_shared(a->h_inode);
c1595e42 21500+ }
4a4d8108 21501+
4a4d8108 21502+ hi_wh = NULL;
027c5e7a 21503+ if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
4a4d8108
AM
21504+ hi_wh = au_hi_wh(inode, a->btgt);
21505+ if (!hi_wh) {
c2b27bf2
AM
21506+ struct au_cp_generic cpg = {
21507+ .dentry = dentry,
21508+ .bdst = a->btgt,
21509+ .bsrc = -1,
21510+ .len = sz,
21511+ .pin = &a->pin
21512+ };
21513+ err = au_sio_cpup_wh(&cpg, /*file*/NULL);
4a4d8108
AM
21514+ if (unlikely(err))
21515+ goto out_unlock;
21516+ hi_wh = au_hi_wh(inode, a->btgt);
21517+ /* todo: revalidate hi_wh? */
21518+ }
21519+ }
21520+
21521+ if (parent) {
21522+ au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
21523+ di_downgrade_lock(parent, AuLock_IR);
21524+ dput(parent);
21525+ parent = NULL;
21526+ }
21527+ if (!au_ftest_icpup(a->flags, DID_CPUP))
21528+ goto out; /* success */
21529+
21530+ if (!d_unhashed(dentry)) {
c2b27bf2
AM
21531+ struct au_cp_generic cpg = {
21532+ .dentry = dentry,
21533+ .bdst = a->btgt,
5afbbe0d 21534+ .bsrc = btop,
c2b27bf2
AM
21535+ .len = sz,
21536+ .pin = &a->pin,
21537+ .flags = AuCpup_DTIME | AuCpup_HOPEN
21538+ };
21539+ err = au_sio_cpup_simple(&cpg);
4a4d8108
AM
21540+ if (!err)
21541+ a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21542+ } else if (!hi_wh)
21543+ a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21544+ else
21545+ a->h_path.dentry = hi_wh; /* do not dget here */
1308ab2a 21546+
4f0767ce 21547+out_unlock:
5527c038 21548+ a->h_inode = d_inode(a->h_path.dentry);
86dc4139 21549+ if (!err)
dece6358 21550+ goto out; /* success */
4a4d8108 21551+ au_unpin(&a->pin);
4f0767ce 21552+out_parent:
4a4d8108
AM
21553+ if (parent) {
21554+ di_write_unlock(parent);
21555+ dput(parent);
21556+ }
4f0767ce 21557+out:
86dc4139 21558+ if (!err)
febd17d6 21559+ inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
1facf9fc 21560+ return err;
21561+}
21562+
4a4d8108 21563+static int aufs_setattr(struct dentry *dentry, struct iattr *ia)
1facf9fc 21564+{
4a4d8108 21565+ int err;
523b37e3 21566+ struct inode *inode, *delegated;
4a4d8108
AM
21567+ struct super_block *sb;
21568+ struct file *file;
21569+ struct au_icpup_args *a;
1facf9fc 21570+
5527c038 21571+ inode = d_inode(dentry);
4a4d8108 21572+ IMustLock(inode);
dece6358 21573+
f2c43d5f
AM
21574+ err = setattr_prepare(dentry, ia);
21575+ if (unlikely(err))
21576+ goto out;
21577+
4a4d8108
AM
21578+ err = -ENOMEM;
21579+ a = kzalloc(sizeof(*a), GFP_NOFS);
21580+ if (unlikely(!a))
21581+ goto out;
1facf9fc 21582+
4a4d8108
AM
21583+ if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
21584+ ia->ia_valid &= ~ATTR_MODE;
dece6358 21585+
4a4d8108
AM
21586+ file = NULL;
21587+ sb = dentry->d_sb;
e49829fe
JR
21588+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21589+ if (unlikely(err))
21590+ goto out_kfree;
21591+
4a4d8108
AM
21592+ if (ia->ia_valid & ATTR_FILE) {
21593+ /* currently ftruncate(2) only */
7e9cd9fe 21594+ AuDebugOn(!d_is_reg(dentry));
4a4d8108 21595+ file = ia->ia_file;
521ced18
JR
21596+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
21597+ /*fi_lsc*/0);
4a4d8108
AM
21598+ if (unlikely(err))
21599+ goto out_si;
21600+ ia->ia_file = au_hf_top(file);
21601+ a->udba = AuOpt_UDBA_NONE;
21602+ } else {
21603+ /* fchmod() doesn't pass ia_file */
21604+ a->udba = au_opt_udba(sb);
027c5e7a
AM
21605+ di_write_lock_child(dentry);
21606+ /* no d_unlinked(), to set UDBA_NONE for root */
4a4d8108
AM
21607+ if (d_unhashed(dentry))
21608+ a->udba = AuOpt_UDBA_NONE;
4a4d8108
AM
21609+ if (a->udba != AuOpt_UDBA_NONE) {
21610+ AuDebugOn(IS_ROOT(dentry));
21611+ err = au_reval_for_attr(dentry, au_sigen(sb));
21612+ if (unlikely(err))
21613+ goto out_dentry;
21614+ }
dece6358 21615+ }
dece6358 21616+
4a4d8108
AM
21617+ err = au_pin_and_icpup(dentry, ia, a);
21618+ if (unlikely(err < 0))
21619+ goto out_dentry;
21620+ if (au_ftest_icpup(a->flags, DID_CPUP)) {
21621+ ia->ia_file = NULL;
21622+ ia->ia_valid &= ~ATTR_FILE;
1308ab2a 21623+ }
dece6358 21624+
4a4d8108
AM
21625+ a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
21626+ if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
21627+ == (ATTR_MODE | ATTR_CTIME)) {
7eafdf33 21628+ err = security_path_chmod(&a->h_path, ia->ia_mode);
4a4d8108
AM
21629+ if (unlikely(err))
21630+ goto out_unlock;
21631+ } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
21632+ && (ia->ia_valid & ATTR_CTIME)) {
86dc4139 21633+ err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
4a4d8108
AM
21634+ if (unlikely(err))
21635+ goto out_unlock;
21636+ }
dece6358 21637+
4a4d8108
AM
21638+ if (ia->ia_valid & ATTR_SIZE) {
21639+ struct file *f;
1308ab2a 21640+
953406b4 21641+ if (ia->ia_size < i_size_read(inode))
4a4d8108 21642+ /* unmap only */
953406b4 21643+ truncate_setsize(inode, ia->ia_size);
1308ab2a 21644+
4a4d8108
AM
21645+ f = NULL;
21646+ if (ia->ia_valid & ATTR_FILE)
21647+ f = ia->ia_file;
febd17d6 21648+ inode_unlock(a->h_inode);
4a4d8108 21649+ err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
febd17d6 21650+ inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
523b37e3
AM
21651+ } else {
21652+ delegated = NULL;
21653+ while (1) {
21654+ err = vfsub_notify_change(&a->h_path, ia, &delegated);
21655+ if (delegated) {
21656+ err = break_deleg_wait(&delegated);
21657+ if (!err)
21658+ continue;
21659+ }
21660+ break;
21661+ }
21662+ }
8cdd5066
JR
21663+ /*
21664+ * regardless aufs 'acl' option setting.
21665+ * why don't all acl-aware fs call this func from their ->setattr()?
21666+ */
21667+ if (!err && (ia->ia_valid & ATTR_MODE))
21668+ err = vfsub_acl_chmod(a->h_inode, ia->ia_mode);
4a4d8108
AM
21669+ if (!err)
21670+ au_cpup_attr_changeable(inode);
1308ab2a 21671+
4f0767ce 21672+out_unlock:
febd17d6 21673+ inode_unlock(a->h_inode);
4a4d8108 21674+ au_unpin(&a->pin);
027c5e7a 21675+ if (unlikely(err))
5afbbe0d 21676+ au_update_dbtop(dentry);
4f0767ce 21677+out_dentry:
4a4d8108
AM
21678+ di_write_unlock(dentry);
21679+ if (file) {
21680+ fi_write_unlock(file);
21681+ ia->ia_file = file;
21682+ ia->ia_valid |= ATTR_FILE;
21683+ }
4f0767ce 21684+out_si:
4a4d8108 21685+ si_read_unlock(sb);
e49829fe 21686+out_kfree:
e49925d1 21687+ au_kfree_rcu(a);
4f0767ce 21688+out:
4a4d8108
AM
21689+ AuTraceErr(err);
21690+ return err;
1facf9fc 21691+}
21692+
c1595e42
JR
21693+#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
21694+static int au_h_path_to_set_attr(struct dentry *dentry,
21695+ struct au_icpup_args *a, struct path *h_path)
21696+{
21697+ int err;
21698+ struct super_block *sb;
21699+
21700+ sb = dentry->d_sb;
21701+ a->udba = au_opt_udba(sb);
21702+ /* no d_unlinked(), to set UDBA_NONE for root */
21703+ if (d_unhashed(dentry))
21704+ a->udba = AuOpt_UDBA_NONE;
21705+ if (a->udba != AuOpt_UDBA_NONE) {
21706+ AuDebugOn(IS_ROOT(dentry));
21707+ err = au_reval_for_attr(dentry, au_sigen(sb));
21708+ if (unlikely(err))
21709+ goto out;
21710+ }
21711+ err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
21712+ if (unlikely(err < 0))
21713+ goto out;
21714+
21715+ h_path->dentry = a->h_path.dentry;
21716+ h_path->mnt = au_sbr_mnt(sb, a->btgt);
21717+
21718+out:
21719+ return err;
21720+}
21721+
f2c43d5f
AM
21722+ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
21723+ struct au_sxattr *arg)
c1595e42
JR
21724+{
21725+ int err;
21726+ struct path h_path;
21727+ struct super_block *sb;
21728+ struct au_icpup_args *a;
5afbbe0d 21729+ struct inode *h_inode;
c1595e42 21730+
c1595e42
JR
21731+ IMustLock(inode);
21732+
21733+ err = -ENOMEM;
21734+ a = kzalloc(sizeof(*a), GFP_NOFS);
21735+ if (unlikely(!a))
21736+ goto out;
21737+
21738+ sb = dentry->d_sb;
21739+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21740+ if (unlikely(err))
21741+ goto out_kfree;
21742+
21743+ h_path.dentry = NULL; /* silence gcc */
21744+ di_write_lock_child(dentry);
21745+ err = au_h_path_to_set_attr(dentry, a, &h_path);
21746+ if (unlikely(err))
21747+ goto out_di;
21748+
febd17d6 21749+ inode_unlock(a->h_inode);
c1595e42
JR
21750+ switch (arg->type) {
21751+ case AU_XATTR_SET:
5afbbe0d 21752+ AuDebugOn(d_is_negative(h_path.dentry));
c1595e42
JR
21753+ err = vfsub_setxattr(h_path.dentry,
21754+ arg->u.set.name, arg->u.set.value,
21755+ arg->u.set.size, arg->u.set.flags);
21756+ break;
c1595e42
JR
21757+ case AU_ACL_SET:
21758+ err = -EOPNOTSUPP;
5527c038 21759+ h_inode = d_inode(h_path.dentry);
c1595e42 21760+ if (h_inode->i_op->set_acl)
f2c43d5f 21761+ /* this will call posix_acl_update_mode */
c1595e42
JR
21762+ err = h_inode->i_op->set_acl(h_inode,
21763+ arg->u.acl_set.acl,
21764+ arg->u.acl_set.type);
21765+ break;
21766+ }
21767+ if (!err)
21768+ au_cpup_attr_timesizes(inode);
21769+
21770+ au_unpin(&a->pin);
21771+ if (unlikely(err))
5afbbe0d 21772+ au_update_dbtop(dentry);
c1595e42
JR
21773+
21774+out_di:
21775+ di_write_unlock(dentry);
21776+ si_read_unlock(sb);
21777+out_kfree:
e49925d1 21778+ au_kfree_rcu(a);
c1595e42
JR
21779+out:
21780+ AuTraceErr(err);
21781+ return err;
21782+}
21783+#endif
21784+
4a4d8108
AM
21785+static void au_refresh_iattr(struct inode *inode, struct kstat *st,
21786+ unsigned int nlink)
1facf9fc 21787+{
9dbd164d
AM
21788+ unsigned int n;
21789+
4a4d8108 21790+ inode->i_mode = st->mode;
86dc4139
AM
21791+ /* don't i_[ug]id_write() here */
21792+ inode->i_uid = st->uid;
21793+ inode->i_gid = st->gid;
4a4d8108
AM
21794+ inode->i_atime = st->atime;
21795+ inode->i_mtime = st->mtime;
21796+ inode->i_ctime = st->ctime;
1facf9fc 21797+
4a4d8108
AM
21798+ au_cpup_attr_nlink(inode, /*force*/0);
21799+ if (S_ISDIR(inode->i_mode)) {
9dbd164d
AM
21800+ n = inode->i_nlink;
21801+ n -= nlink;
21802+ n += st->nlink;
f6b6e03d 21803+ smp_mb(); /* for i_nlink */
7eafdf33 21804+ /* 0 can happen */
92d182d2 21805+ set_nlink(inode, n);
4a4d8108 21806+ }
1facf9fc 21807+
4a4d8108
AM
21808+ spin_lock(&inode->i_lock);
21809+ inode->i_blocks = st->blocks;
21810+ i_size_write(inode, st->size);
21811+ spin_unlock(&inode->i_lock);
1facf9fc 21812+}
21813+
c1595e42 21814+/*
f2c43d5f 21815+ * common routine for aufs_getattr() and au_getxattr().
c1595e42
JR
21816+ * returns zero or negative (an error).
21817+ * @dentry will be read-locked in success.
21818+ */
0b487229
JR
21819+int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
21820+ struct path *h_path, int locked)
1facf9fc 21821+{
4a4d8108 21822+ int err;
076b876e 21823+ unsigned int mnt_flags, sigen;
c1595e42 21824+ unsigned char udba_none;
4a4d8108 21825+ aufs_bindex_t bindex;
4a4d8108 21826+ struct super_block *sb, *h_sb;
1facf9fc 21827+
c1595e42
JR
21828+ h_path->mnt = NULL;
21829+ h_path->dentry = NULL;
21830+
21831+ err = 0;
4a4d8108 21832+ sb = dentry->d_sb;
4a4d8108
AM
21833+ mnt_flags = au_mntflags(sb);
21834+ udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
1facf9fc 21835+
a2654f78
AM
21836+ if (unlikely(locked))
21837+ goto body; /* skip locking dinfo */
21838+
4a4d8108 21839+ /* support fstat(2) */
027c5e7a 21840+ if (!d_unlinked(dentry) && !udba_none) {
076b876e 21841+ sigen = au_sigen(sb);
027c5e7a
AM
21842+ err = au_digen_test(dentry, sigen);
21843+ if (!err) {
4a4d8108 21844+ di_read_lock_child(dentry, AuLock_IR);
027c5e7a 21845+ err = au_dbrange_test(dentry);
c1595e42
JR
21846+ if (unlikely(err)) {
21847+ di_read_unlock(dentry, AuLock_IR);
21848+ goto out;
21849+ }
027c5e7a 21850+ } else {
4a4d8108
AM
21851+ AuDebugOn(IS_ROOT(dentry));
21852+ di_write_lock_child(dentry);
027c5e7a
AM
21853+ err = au_dbrange_test(dentry);
21854+ if (!err)
21855+ err = au_reval_for_attr(dentry, sigen);
c1595e42
JR
21856+ if (!err)
21857+ di_downgrade_lock(dentry, AuLock_IR);
21858+ else {
21859+ di_write_unlock(dentry);
21860+ goto out;
21861+ }
4a4d8108
AM
21862+ }
21863+ } else
21864+ di_read_lock_child(dentry, AuLock_IR);
1facf9fc 21865+
a2654f78 21866+body:
0b487229
JR
21867+ if (!inode) {
21868+ inode = d_inode(dentry);
21869+ if (unlikely(!inode))
21870+ goto out;
21871+ }
5afbbe0d 21872+ bindex = au_ibtop(inode);
c1595e42
JR
21873+ h_path->mnt = au_sbr_mnt(sb, bindex);
21874+ h_sb = h_path->mnt->mnt_sb;
21875+ if (!force
21876+ && !au_test_fs_bad_iattr(h_sb)
21877+ && udba_none)
21878+ goto out; /* success */
1facf9fc 21879+
5afbbe0d 21880+ if (au_dbtop(dentry) == bindex)
c1595e42 21881+ h_path->dentry = au_h_dptr(dentry, bindex);
4a4d8108 21882+ else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
c1595e42
JR
21883+ h_path->dentry = au_plink_lkup(inode, bindex);
21884+ if (IS_ERR(h_path->dentry))
21885+ /* pretending success */
21886+ h_path->dentry = NULL;
21887+ else
21888+ dput(h_path->dentry);
4a4d8108 21889+ }
c1595e42
JR
21890+
21891+out:
21892+ return err;
21893+}
21894+
521ced18
JR
21895+static int aufs_getattr(const struct path *path, struct kstat *st,
21896+ u32 request, unsigned int query)
c1595e42
JR
21897+{
21898+ int err;
21899+ unsigned char positive;
21900+ struct path h_path;
521ced18 21901+ struct dentry *dentry;
c1595e42
JR
21902+ struct inode *inode;
21903+ struct super_block *sb;
21904+
521ced18 21905+ dentry = path->dentry;
5527c038 21906+ inode = d_inode(dentry);
c1595e42
JR
21907+ sb = dentry->d_sb;
21908+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21909+ if (unlikely(err))
21910+ goto out;
0b487229
JR
21911+ err = au_h_path_getattr(dentry, /*inode*/NULL, /*force*/0, &h_path,
21912+ /*locked*/0);
c1595e42
JR
21913+ if (unlikely(err))
21914+ goto out_si;
c06a8ce3 21915+ if (unlikely(!h_path.dentry))
c1595e42 21916+ /* illegally overlapped or something */
4a4d8108
AM
21917+ goto out_fill; /* pretending success */
21918+
5527c038 21919+ positive = d_is_positive(h_path.dentry);
4a4d8108 21920+ if (positive)
521ced18
JR
21921+ /* no vfsub version */
21922+ err = vfs_getattr(&h_path, st, request, query);
4a4d8108
AM
21923+ if (!err) {
21924+ if (positive)
c06a8ce3 21925+ au_refresh_iattr(inode, st,
5527c038 21926+ d_inode(h_path.dentry)->i_nlink);
4a4d8108 21927+ goto out_fill; /* success */
1facf9fc 21928+ }
7f207e10 21929+ AuTraceErr(err);
c1595e42 21930+ goto out_di;
4a4d8108 21931+
4f0767ce 21932+out_fill:
4a4d8108 21933+ generic_fillattr(inode, st);
c1595e42 21934+out_di:
4a4d8108 21935+ di_read_unlock(dentry, AuLock_IR);
c1595e42 21936+out_si:
4a4d8108 21937+ si_read_unlock(sb);
7f207e10
AM
21938+out:
21939+ AuTraceErr(err);
4a4d8108 21940+ return err;
1facf9fc 21941+}
21942+
21943+/* ---------------------------------------------------------------------- */
21944+
febd17d6
JR
21945+static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
21946+ struct delayed_call *done)
4a4d8108 21947+{
c2c0f25c 21948+ const char *ret;
c2c0f25c 21949+ struct dentry *h_dentry;
febd17d6 21950+ struct inode *h_inode;
4a4d8108 21951+ int err;
c2c0f25c 21952+ aufs_bindex_t bindex;
1facf9fc 21953+
79b8bda9 21954+ ret = NULL; /* suppress a warning */
febd17d6
JR
21955+ err = -ECHILD;
21956+ if (!dentry)
21957+ goto out;
21958+
027c5e7a
AM
21959+ err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
21960+ if (unlikely(err))
c2c0f25c 21961+ goto out;
027c5e7a
AM
21962+
21963+ err = au_d_hashed_positive(dentry);
c2c0f25c
AM
21964+ if (unlikely(err))
21965+ goto out_unlock;
21966+
21967+ err = -EINVAL;
21968+ inode = d_inode(dentry);
5afbbe0d 21969+ bindex = au_ibtop(inode);
c2c0f25c 21970+ h_inode = au_h_iptr(inode, bindex);
febd17d6 21971+ if (unlikely(!h_inode->i_op->get_link))
c2c0f25c
AM
21972+ goto out_unlock;
21973+
21974+ err = -EBUSY;
21975+ h_dentry = NULL;
5afbbe0d 21976+ if (au_dbtop(dentry) <= bindex) {
c2c0f25c
AM
21977+ h_dentry = au_h_dptr(dentry, bindex);
21978+ if (h_dentry)
21979+ dget(h_dentry);
027c5e7a 21980+ }
c2c0f25c
AM
21981+ if (!h_dentry) {
21982+ h_dentry = d_find_any_alias(h_inode);
21983+ if (IS_ERR(h_dentry)) {
21984+ err = PTR_ERR(h_dentry);
febd17d6 21985+ goto out_unlock;
c2c0f25c
AM
21986+ }
21987+ }
21988+ if (unlikely(!h_dentry))
febd17d6 21989+ goto out_unlock;
1facf9fc 21990+
c2c0f25c 21991+ err = 0;
062440b3 21992+ AuDbg("%ps\n", h_inode->i_op->get_link);
c2c0f25c 21993+ AuDbgDentry(h_dentry);
f2c43d5f 21994+ ret = vfs_get_link(h_dentry, done);
c2c0f25c 21995+ dput(h_dentry);
febd17d6
JR
21996+ if (IS_ERR(ret))
21997+ err = PTR_ERR(ret);
c2c0f25c 21998+
c2c0f25c
AM
21999+out_unlock:
22000+ aufs_read_unlock(dentry, AuLock_IR);
4f0767ce 22001+out:
c2c0f25c
AM
22002+ if (unlikely(err))
22003+ ret = ERR_PTR(err);
22004+ AuTraceErrPtr(ret);
22005+ return ret;
4a4d8108 22006+}
1facf9fc 22007+
4a4d8108 22008+/* ---------------------------------------------------------------------- */
1facf9fc 22009+
e2f27e51
AM
22010+static int au_is_special(struct inode *inode)
22011+{
22012+ return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
22013+}
22014+
acd2b654
AM
22015+static int aufs_update_time(struct inode *inode, struct timespec64 *ts,
22016+ int flags)
4a4d8108 22017+{
0c3ec466 22018+ int err;
e2f27e51 22019+ aufs_bindex_t bindex;
0c3ec466
AM
22020+ struct super_block *sb;
22021+ struct inode *h_inode;
e2f27e51 22022+ struct vfsmount *h_mnt;
0c3ec466
AM
22023+
22024+ sb = inode->i_sb;
e2f27e51
AM
22025+ WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
22026+ "unexpected s_flags 0x%lx", sb->s_flags);
22027+
0c3ec466
AM
22028+ /* mmap_sem might be acquired already, cf. aufs_mmap() */
22029+ lockdep_off();
22030+ si_read_lock(sb, AuLock_FLUSH);
22031+ ii_write_lock_child(inode);
e2f27e51
AM
22032+
22033+ err = 0;
22034+ bindex = au_ibtop(inode);
22035+ h_inode = au_h_iptr(inode, bindex);
22036+ if (!au_test_ro(sb, bindex, inode)) {
22037+ h_mnt = au_sbr_mnt(sb, bindex);
22038+ err = vfsub_mnt_want_write(h_mnt);
22039+ if (!err) {
22040+ err = vfsub_update_time(h_inode, ts, flags);
22041+ vfsub_mnt_drop_write(h_mnt);
22042+ }
22043+ } else if (au_is_special(h_inode)) {
22044+ /*
22045+ * Never copy-up here.
22046+ * These special files may already be opened and used for
22047+ * communicating. If we copied it up, then the communication
22048+ * would be corrupted.
22049+ */
22050+ AuWarn1("timestamps for i%lu are ignored "
22051+ "since it is on readonly branch (hi%lu).\n",
22052+ inode->i_ino, h_inode->i_ino);
22053+ } else if (flags & ~S_ATIME) {
22054+ err = -EIO;
22055+ AuIOErr1("unexpected flags 0x%x\n", flags);
22056+ AuDebugOn(1);
22057+ }
22058+
38d290e6
JR
22059+ if (!err)
22060+ au_cpup_attr_timesizes(inode);
0c3ec466
AM
22061+ ii_write_unlock(inode);
22062+ si_read_unlock(sb);
22063+ lockdep_on();
38d290e6
JR
22064+
22065+ if (!err && (flags & S_VERSION))
22066+ inode_inc_iversion(inode);
22067+
0c3ec466 22068+ return err;
4a4d8108 22069+}
1facf9fc 22070+
4a4d8108 22071+/* ---------------------------------------------------------------------- */
1308ab2a 22072+
b95c5147
AM
22073+/* no getattr version will be set by module.c:aufs_init() */
22074+struct inode_operations aufs_iop_nogetattr[AuIop_Last],
22075+ aufs_iop[] = {
22076+ [AuIop_SYMLINK] = {
22077+ .permission = aufs_permission,
c1595e42 22078+#ifdef CONFIG_FS_POSIX_ACL
b95c5147
AM
22079+ .get_acl = aufs_get_acl,
22080+ .set_acl = aufs_set_acl, /* unsupport for symlink? */
c1595e42
JR
22081+#endif
22082+
b95c5147
AM
22083+ .setattr = aufs_setattr,
22084+ .getattr = aufs_getattr,
0c3ec466 22085+
c1595e42 22086+#ifdef CONFIG_AUFS_XATTR
b95c5147 22087+ .listxattr = aufs_listxattr,
c1595e42
JR
22088+#endif
22089+
febd17d6 22090+ .get_link = aufs_get_link,
0c3ec466 22091+
b95c5147
AM
22092+ /* .update_time = aufs_update_time */
22093+ },
22094+ [AuIop_DIR] = {
22095+ .create = aufs_create,
22096+ .lookup = aufs_lookup,
22097+ .link = aufs_link,
22098+ .unlink = aufs_unlink,
22099+ .symlink = aufs_symlink,
22100+ .mkdir = aufs_mkdir,
22101+ .rmdir = aufs_rmdir,
22102+ .mknod = aufs_mknod,
22103+ .rename = aufs_rename,
22104+
22105+ .permission = aufs_permission,
c1595e42 22106+#ifdef CONFIG_FS_POSIX_ACL
b95c5147
AM
22107+ .get_acl = aufs_get_acl,
22108+ .set_acl = aufs_set_acl,
c1595e42
JR
22109+#endif
22110+
b95c5147
AM
22111+ .setattr = aufs_setattr,
22112+ .getattr = aufs_getattr,
0c3ec466 22113+
c1595e42 22114+#ifdef CONFIG_AUFS_XATTR
b95c5147 22115+ .listxattr = aufs_listxattr,
c1595e42
JR
22116+#endif
22117+
b95c5147
AM
22118+ .update_time = aufs_update_time,
22119+ .atomic_open = aufs_atomic_open,
22120+ .tmpfile = aufs_tmpfile
22121+ },
22122+ [AuIop_OTHER] = {
22123+ .permission = aufs_permission,
c1595e42 22124+#ifdef CONFIG_FS_POSIX_ACL
b95c5147
AM
22125+ .get_acl = aufs_get_acl,
22126+ .set_acl = aufs_set_acl,
c1595e42
JR
22127+#endif
22128+
b95c5147
AM
22129+ .setattr = aufs_setattr,
22130+ .getattr = aufs_getattr,
0c3ec466 22131+
c1595e42 22132+#ifdef CONFIG_AUFS_XATTR
b95c5147 22133+ .listxattr = aufs_listxattr,
c1595e42
JR
22134+#endif
22135+
b95c5147
AM
22136+ .update_time = aufs_update_time
22137+ }
4a4d8108 22138+};
7f207e10
AM
22139diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
22140--- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
0b487229
JR
22141+++ linux/fs/aufs/i_op_del.c 2021-02-22 23:30:37.702680911 +0100
22142@@ -0,0 +1,513 @@
cd7a4cd9 22143+// SPDX-License-Identifier: GPL-2.0
1facf9fc 22144+/*
0b487229 22145+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 22146+ *
22147+ * This program, aufs is free software; you can redistribute it and/or modify
22148+ * it under the terms of the GNU General Public License as published by
22149+ * the Free Software Foundation; either version 2 of the License, or
22150+ * (at your option) any later version.
dece6358
AM
22151+ *
22152+ * This program is distributed in the hope that it will be useful,
22153+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
22154+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22155+ * GNU General Public License for more details.
22156+ *
22157+ * You should have received a copy of the GNU General Public License
523b37e3 22158+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 22159+ */
22160+
22161+/*
4a4d8108 22162+ * inode operations (del entry)
1308ab2a 22163+ */
dece6358 22164+
0b487229 22165+#include <linux/iversion.h>
1308ab2a 22166+#include "aufs.h"
dece6358 22167+
4a4d8108
AM
22168+/*
22169+ * decide if a new whiteout for @dentry is necessary or not.
22170+ * when it is necessary, prepare the parent dir for the upper branch whose
22171+ * branch index is @bcpup for creation. the actual creation of the whiteout will
22172+ * be done by caller.
22173+ * return value:
22174+ * 0: wh is unnecessary
22175+ * plus: wh is necessary
22176+ * minus: error
22177+ */
22178+int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
1308ab2a 22179+{
4a4d8108 22180+ int need_wh, err;
5afbbe0d 22181+ aufs_bindex_t btop;
4a4d8108 22182+ struct super_block *sb;
dece6358 22183+
4a4d8108 22184+ sb = dentry->d_sb;
5afbbe0d 22185+ btop = au_dbtop(dentry);
4a4d8108 22186+ if (*bcpup < 0) {
5afbbe0d
AM
22187+ *bcpup = btop;
22188+ if (au_test_ro(sb, btop, d_inode(dentry))) {
4a4d8108
AM
22189+ err = AuWbrCopyup(au_sbi(sb), dentry);
22190+ *bcpup = err;
22191+ if (unlikely(err < 0))
22192+ goto out;
22193+ }
22194+ } else
5afbbe0d 22195+ AuDebugOn(btop < *bcpup
5527c038 22196+ || au_test_ro(sb, *bcpup, d_inode(dentry)));
5afbbe0d 22197+ AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
1308ab2a 22198+
5afbbe0d 22199+ if (*bcpup != btop) {
4a4d8108
AM
22200+ err = au_cpup_dirs(dentry, *bcpup);
22201+ if (unlikely(err))
22202+ goto out;
22203+ need_wh = 1;
22204+ } else {
027c5e7a 22205+ struct au_dinfo *dinfo, *tmp;
4a4d8108 22206+
027c5e7a
AM
22207+ need_wh = -ENOMEM;
22208+ dinfo = au_di(dentry);
22209+ tmp = au_di_alloc(sb, AuLsc_DI_TMP);
22210+ if (tmp) {
22211+ au_di_cp(tmp, dinfo);
22212+ au_di_swap(tmp, dinfo);
22213+ /* returns the number of positive dentries */
5afbbe0d
AM
22214+ need_wh = au_lkup_dentry(dentry, btop + 1,
22215+ /* AuLkup_IGNORE_PERM */ 0);
027c5e7a
AM
22216+ au_di_swap(tmp, dinfo);
22217+ au_rw_write_unlock(&tmp->di_rwsem);
22218+ au_di_free(tmp);
4a4d8108
AM
22219+ }
22220+ }
22221+ AuDbg("need_wh %d\n", need_wh);
22222+ err = need_wh;
22223+
4f0767ce 22224+out:
4a4d8108 22225+ return err;
1facf9fc 22226+}
22227+
4a4d8108
AM
22228+/*
22229+ * simple tests for the del-entry operations.
22230+ * following the checks in vfs, plus the parent-child relationship.
22231+ */
22232+int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
22233+ struct dentry *h_parent, int isdir)
1facf9fc 22234+{
4a4d8108
AM
22235+ int err;
22236+ umode_t h_mode;
22237+ struct dentry *h_dentry, *h_latest;
1308ab2a 22238+ struct inode *h_inode;
1facf9fc 22239+
4a4d8108 22240+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 22241+ if (d_really_is_positive(dentry)) {
4a4d8108 22242+ err = -ENOENT;
5527c038
JR
22243+ if (unlikely(d_is_negative(h_dentry)))
22244+ goto out;
22245+ h_inode = d_inode(h_dentry);
22246+ if (unlikely(!h_inode->i_nlink))
4a4d8108 22247+ goto out;
1facf9fc 22248+
4a4d8108
AM
22249+ h_mode = h_inode->i_mode;
22250+ if (!isdir) {
22251+ err = -EISDIR;
22252+ if (unlikely(S_ISDIR(h_mode)))
22253+ goto out;
22254+ } else if (unlikely(!S_ISDIR(h_mode))) {
22255+ err = -ENOTDIR;
22256+ goto out;
22257+ }
22258+ } else {
22259+ /* rename(2) case */
22260+ err = -EIO;
5527c038 22261+ if (unlikely(d_is_positive(h_dentry)))
4a4d8108
AM
22262+ goto out;
22263+ }
1facf9fc 22264+
4a4d8108
AM
22265+ err = -ENOENT;
22266+ /* expected parent dir is locked */
22267+ if (unlikely(h_parent != h_dentry->d_parent))
22268+ goto out;
22269+ err = 0;
22270+
22271+ /*
22272+ * rmdir a dir may break the consistency on some filesystem.
22273+ * let's try heavy test.
22274+ */
22275+ err = -EACCES;
076b876e 22276+ if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1)
5527c038 22277+ && au_test_h_perm(d_inode(h_parent),
076b876e 22278+ MAY_EXEC | MAY_WRITE)))
4a4d8108
AM
22279+ goto out;
22280+
076b876e 22281+ h_latest = au_sio_lkup_one(&dentry->d_name, h_parent);
4a4d8108
AM
22282+ err = -EIO;
22283+ if (IS_ERR(h_latest))
22284+ goto out;
22285+ if (h_latest == h_dentry)
22286+ err = 0;
22287+ dput(h_latest);
22288+
4f0767ce 22289+out:
4a4d8108 22290+ return err;
1308ab2a 22291+}
1facf9fc 22292+
4a4d8108
AM
22293+/*
22294+ * decide the branch where we operate for @dentry. the branch index will be set
acd2b654 22295+ * @rbcpup. after deciding it, 'pin' it and store the timestamps of the parent
4a4d8108
AM
22296+ * dir for reverting.
22297+ * when a new whiteout is necessary, create it.
22298+ */
22299+static struct dentry*
22300+lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
22301+ struct au_dtime *dt, struct au_pin *pin)
1308ab2a 22302+{
4a4d8108
AM
22303+ struct dentry *wh_dentry;
22304+ struct super_block *sb;
22305+ struct path h_path;
22306+ int err, need_wh;
22307+ unsigned int udba;
22308+ aufs_bindex_t bcpup;
dece6358 22309+
4a4d8108
AM
22310+ need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
22311+ wh_dentry = ERR_PTR(need_wh);
22312+ if (unlikely(need_wh < 0))
22313+ goto out;
22314+
22315+ sb = dentry->d_sb;
22316+ udba = au_opt_udba(sb);
22317+ bcpup = *rbcpup;
22318+ err = au_pin(pin, dentry, bcpup, udba,
22319+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
22320+ wh_dentry = ERR_PTR(err);
22321+ if (unlikely(err))
22322+ goto out;
22323+
22324+ h_path.dentry = au_pinned_h_parent(pin);
22325+ if (udba != AuOpt_UDBA_NONE
5afbbe0d 22326+ && au_dbtop(dentry) == bcpup) {
4a4d8108
AM
22327+ err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
22328+ wh_dentry = ERR_PTR(err);
22329+ if (unlikely(err))
22330+ goto out_unpin;
22331+ }
22332+
22333+ h_path.mnt = au_sbr_mnt(sb, bcpup);
22334+ au_dtime_store(dt, au_pinned_parent(pin), &h_path);
22335+ wh_dentry = NULL;
22336+ if (!need_wh)
22337+ goto out; /* success, no need to create whiteout */
22338+
22339+ wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
22340+ if (IS_ERR(wh_dentry))
22341+ goto out_unpin;
22342+
22343+ /* returns with the parent is locked and wh_dentry is dget-ed */
22344+ goto out; /* success */
22345+
4f0767ce 22346+out_unpin:
4a4d8108 22347+ au_unpin(pin);
4f0767ce 22348+out:
4a4d8108 22349+ return wh_dentry;
1facf9fc 22350+}
22351+
4a4d8108
AM
22352+/*
22353+ * when removing a dir, rename it to a unique temporary whiteout-ed name first
22354+ * in order to be revertible and save time for removing many child whiteouts
22355+ * under the dir.
22356+ * returns 1 when there are too many child whiteout and caller should remove
22357+ * them asynchronously. returns 0 when the number of children is enough small to
22358+ * remove now or the branch fs is a remote fs.
22359+ * otherwise return an error.
22360+ */
22361+static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
22362+ struct au_nhash *whlist, struct inode *dir)
1facf9fc 22363+{
4a4d8108
AM
22364+ int rmdir_later, err, dirwh;
22365+ struct dentry *h_dentry;
22366+ struct super_block *sb;
5527c038 22367+ struct inode *inode;
4a4d8108
AM
22368+
22369+ sb = dentry->d_sb;
22370+ SiMustAnyLock(sb);
22371+ h_dentry = au_h_dptr(dentry, bindex);
22372+ err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
22373+ if (unlikely(err))
22374+ goto out;
22375+
22376+ /* stop monitoring */
5527c038
JR
22377+ inode = d_inode(dentry);
22378+ au_hn_free(au_hi(inode, bindex));
4a4d8108
AM
22379+
22380+ if (!au_test_fs_remote(h_dentry->d_sb)) {
22381+ dirwh = au_sbi(sb)->si_dirwh;
22382+ rmdir_later = (dirwh <= 1);
22383+ if (!rmdir_later)
22384+ rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
22385+ dirwh);
22386+ if (rmdir_later)
22387+ return rmdir_later;
22388+ }
1facf9fc 22389+
4a4d8108
AM
22390+ err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
22391+ if (unlikely(err)) {
523b37e3
AM
22392+ AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
22393+ h_dentry, bindex, err);
4a4d8108
AM
22394+ err = 0;
22395+ }
dece6358 22396+
4f0767ce 22397+out:
4a4d8108
AM
22398+ AuTraceErr(err);
22399+ return err;
22400+}
1308ab2a 22401+
4a4d8108
AM
22402+/*
22403+ * final procedure for deleting a entry.
22404+ * maintain dentry and iattr.
22405+ */
22406+static void epilog(struct inode *dir, struct dentry *dentry,
22407+ aufs_bindex_t bindex)
22408+{
22409+ struct inode *inode;
1308ab2a 22410+
5527c038 22411+ inode = d_inode(dentry);
4a4d8108
AM
22412+ d_drop(dentry);
22413+ inode->i_ctime = dir->i_ctime;
1308ab2a 22414+
b912730e 22415+ au_dir_ts(dir, bindex);
be118d29 22416+ inode_inc_iversion(dir);
1facf9fc 22417+}
22418+
4a4d8108
AM
22419+/*
22420+ * when an error happened, remove the created whiteout and revert everything.
22421+ */
7f207e10
AM
22422+static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
22423+ aufs_bindex_t bwh, struct dentry *wh_dentry,
22424+ struct dentry *dentry, struct au_dtime *dt)
1facf9fc 22425+{
4a4d8108
AM
22426+ int rerr;
22427+ struct path h_path = {
22428+ .dentry = wh_dentry,
7f207e10 22429+ .mnt = au_sbr_mnt(dir->i_sb, bindex)
4a4d8108 22430+ };
dece6358 22431+
7f207e10 22432+ rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
4a4d8108
AM
22433+ if (!rerr) {
22434+ au_set_dbwh(dentry, bwh);
22435+ au_dtime_revert(dt);
22436+ return 0;
22437+ }
dece6358 22438+
523b37e3 22439+ AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
4a4d8108 22440+ return -EIO;
1facf9fc 22441+}
22442+
4a4d8108 22443+/* ---------------------------------------------------------------------- */
1facf9fc 22444+
4a4d8108 22445+int aufs_unlink(struct inode *dir, struct dentry *dentry)
1308ab2a 22446+{
4a4d8108 22447+ int err;
5afbbe0d 22448+ aufs_bindex_t bwh, bindex, btop;
523b37e3 22449+ struct inode *inode, *h_dir, *delegated;
4a4d8108 22450+ struct dentry *parent, *wh_dentry;
acd2b654 22451+ /* to reduce stack size */
c2b27bf2
AM
22452+ struct {
22453+ struct au_dtime dt;
22454+ struct au_pin pin;
22455+ struct path h_path;
22456+ } *a;
1facf9fc 22457+
4a4d8108 22458+ IMustLock(dir);
027c5e7a 22459+
c2b27bf2
AM
22460+ err = -ENOMEM;
22461+ a = kmalloc(sizeof(*a), GFP_NOFS);
22462+ if (unlikely(!a))
22463+ goto out;
22464+
027c5e7a
AM
22465+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
22466+ if (unlikely(err))
c2b27bf2 22467+ goto out_free;
027c5e7a
AM
22468+ err = au_d_hashed_positive(dentry);
22469+ if (unlikely(err))
22470+ goto out_unlock;
5527c038 22471+ inode = d_inode(dentry);
4a4d8108 22472+ IMustLock(inode);
027c5e7a 22473+ err = -EISDIR;
2000de60 22474+ if (unlikely(d_is_dir(dentry)))
027c5e7a 22475+ goto out_unlock; /* possible? */
1facf9fc 22476+
5afbbe0d 22477+ btop = au_dbtop(dentry);
4a4d8108
AM
22478+ bwh = au_dbwh(dentry);
22479+ bindex = -1;
027c5e7a
AM
22480+ parent = dentry->d_parent; /* dir inode is locked */
22481+ di_write_lock_parent(parent);
c2b27bf2
AM
22482+ wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
22483+ &a->pin);
4a4d8108
AM
22484+ err = PTR_ERR(wh_dentry);
22485+ if (IS_ERR(wh_dentry))
027c5e7a 22486+ goto out_parent;
1facf9fc 22487+
5afbbe0d
AM
22488+ a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
22489+ a->h_path.dentry = au_h_dptr(dentry, btop);
c2b27bf2 22490+ dget(a->h_path.dentry);
5afbbe0d 22491+ if (bindex == btop) {
c2b27bf2 22492+ h_dir = au_pinned_h_dir(&a->pin);
523b37e3
AM
22493+ delegated = NULL;
22494+ err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
22495+ if (unlikely(err == -EWOULDBLOCK)) {
22496+ pr_warn("cannot retry for NFSv4 delegation"
22497+ " for an internal unlink\n");
22498+ iput(delegated);
22499+ }
4a4d8108
AM
22500+ } else {
22501+ /* dir inode is locked */
5527c038 22502+ h_dir = d_inode(wh_dentry->d_parent);
4a4d8108
AM
22503+ IMustLock(h_dir);
22504+ err = 0;
22505+ }
dece6358 22506+
4a4d8108 22507+ if (!err) {
7f207e10 22508+ vfsub_drop_nlink(inode);
4a4d8108
AM
22509+ epilog(dir, dentry, bindex);
22510+
22511+ /* update target timestamps */
5afbbe0d 22512+ if (bindex == btop) {
c2b27bf2
AM
22513+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
22514+ /*ignore*/
5527c038 22515+ inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
4a4d8108
AM
22516+ } else
22517+ /* todo: this timestamp may be reverted later */
22518+ inode->i_ctime = h_dir->i_ctime;
027c5e7a 22519+ goto out_unpin; /* success */
1facf9fc 22520+ }
22521+
4a4d8108
AM
22522+ /* revert */
22523+ if (wh_dentry) {
22524+ int rerr;
22525+
c2b27bf2
AM
22526+ rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22527+ &a->dt);
4a4d8108
AM
22528+ if (rerr)
22529+ err = rerr;
dece6358 22530+ }
1facf9fc 22531+
027c5e7a 22532+out_unpin:
c2b27bf2 22533+ au_unpin(&a->pin);
4a4d8108 22534+ dput(wh_dentry);
c2b27bf2 22535+ dput(a->h_path.dentry);
027c5e7a 22536+out_parent:
4a4d8108 22537+ di_write_unlock(parent);
027c5e7a 22538+out_unlock:
4a4d8108 22539+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2 22540+out_free:
e49925d1 22541+ au_kfree_rcu(a);
027c5e7a 22542+out:
4a4d8108 22543+ return err;
dece6358
AM
22544+}
22545+
4a4d8108 22546+int aufs_rmdir(struct inode *dir, struct dentry *dentry)
1308ab2a 22547+{
4a4d8108 22548+ int err, rmdir_later;
5afbbe0d 22549+ aufs_bindex_t bwh, bindex, btop;
4a4d8108
AM
22550+ struct inode *inode;
22551+ struct dentry *parent, *wh_dentry, *h_dentry;
22552+ struct au_whtmp_rmdir *args;
acd2b654 22553+ /* to reduce stack size */
c2b27bf2
AM
22554+ struct {
22555+ struct au_dtime dt;
22556+ struct au_pin pin;
22557+ } *a;
1facf9fc 22558+
4a4d8108 22559+ IMustLock(dir);
027c5e7a 22560+
c2b27bf2
AM
22561+ err = -ENOMEM;
22562+ a = kmalloc(sizeof(*a), GFP_NOFS);
22563+ if (unlikely(!a))
22564+ goto out;
22565+
027c5e7a
AM
22566+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
22567+ if (unlikely(err))
c2b27bf2 22568+ goto out_free;
53392da6
AM
22569+ err = au_alive_dir(dentry);
22570+ if (unlikely(err))
027c5e7a 22571+ goto out_unlock;
5527c038 22572+ inode = d_inode(dentry);
4a4d8108 22573+ IMustLock(inode);
027c5e7a 22574+ err = -ENOTDIR;
2000de60 22575+ if (unlikely(!d_is_dir(dentry)))
027c5e7a 22576+ goto out_unlock; /* possible? */
dece6358 22577+
4a4d8108
AM
22578+ err = -ENOMEM;
22579+ args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
22580+ if (unlikely(!args))
22581+ goto out_unlock;
dece6358 22582+
4a4d8108
AM
22583+ parent = dentry->d_parent; /* dir inode is locked */
22584+ di_write_lock_parent(parent);
22585+ err = au_test_empty(dentry, &args->whlist);
22586+ if (unlikely(err))
027c5e7a 22587+ goto out_parent;
1facf9fc 22588+
5afbbe0d 22589+ btop = au_dbtop(dentry);
4a4d8108
AM
22590+ bwh = au_dbwh(dentry);
22591+ bindex = -1;
c2b27bf2
AM
22592+ wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
22593+ &a->pin);
4a4d8108
AM
22594+ err = PTR_ERR(wh_dentry);
22595+ if (IS_ERR(wh_dentry))
027c5e7a 22596+ goto out_parent;
1facf9fc 22597+
5afbbe0d 22598+ h_dentry = au_h_dptr(dentry, btop);
4a4d8108
AM
22599+ dget(h_dentry);
22600+ rmdir_later = 0;
5afbbe0d
AM
22601+ if (bindex == btop) {
22602+ err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
4a4d8108
AM
22603+ if (err > 0) {
22604+ rmdir_later = err;
22605+ err = 0;
22606+ }
22607+ } else {
22608+ /* stop monitoring */
5afbbe0d 22609+ au_hn_free(au_hi(inode, btop));
4a4d8108
AM
22610+
22611+ /* dir inode is locked */
5527c038 22612+ IMustLock(d_inode(wh_dentry->d_parent));
1facf9fc 22613+ err = 0;
22614+ }
22615+
4a4d8108 22616+ if (!err) {
027c5e7a 22617+ vfsub_dead_dir(inode);
4a4d8108
AM
22618+ au_set_dbdiropq(dentry, -1);
22619+ epilog(dir, dentry, bindex);
1308ab2a 22620+
4a4d8108 22621+ if (rmdir_later) {
5afbbe0d 22622+ au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
4a4d8108
AM
22623+ args = NULL;
22624+ }
1308ab2a 22625+
4a4d8108 22626+ goto out_unpin; /* success */
1facf9fc 22627+ }
22628+
4a4d8108
AM
22629+ /* revert */
22630+ AuLabel(revert);
22631+ if (wh_dentry) {
22632+ int rerr;
1308ab2a 22633+
c2b27bf2
AM
22634+ rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22635+ &a->dt);
4a4d8108
AM
22636+ if (rerr)
22637+ err = rerr;
1facf9fc 22638+ }
22639+
4f0767ce 22640+out_unpin:
c2b27bf2 22641+ au_unpin(&a->pin);
4a4d8108
AM
22642+ dput(wh_dentry);
22643+ dput(h_dentry);
027c5e7a 22644+out_parent:
4a4d8108
AM
22645+ di_write_unlock(parent);
22646+ if (args)
22647+ au_whtmp_rmdir_free(args);
4f0767ce 22648+out_unlock:
4a4d8108 22649+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2 22650+out_free:
e49925d1 22651+ au_kfree_rcu(a);
4f0767ce 22652+out:
4a4d8108
AM
22653+ AuTraceErr(err);
22654+ return err;
dece6358 22655+}
7f207e10
AM
22656diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
22657--- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
0b487229
JR
22658+++ linux/fs/aufs/i_op_ren.c 2021-02-22 23:30:37.702680911 +0100
22659@@ -0,0 +1,1250 @@
cd7a4cd9 22660+// SPDX-License-Identifier: GPL-2.0
1facf9fc 22661+/*
0b487229 22662+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 22663+ *
22664+ * This program, aufs is free software; you can redistribute it and/or modify
22665+ * it under the terms of the GNU General Public License as published by
22666+ * the Free Software Foundation; either version 2 of the License, or
22667+ * (at your option) any later version.
dece6358
AM
22668+ *
22669+ * This program is distributed in the hope that it will be useful,
22670+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
22671+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22672+ * GNU General Public License for more details.
22673+ *
22674+ * You should have received a copy of the GNU General Public License
523b37e3 22675+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 22676+ */
22677+
22678+/*
4a4d8108
AM
22679+ * inode operation (rename entry)
22680+ * todo: this is crazy monster
1facf9fc 22681+ */
22682+
0b487229 22683+#include <linux/iversion.h>
1facf9fc 22684+#include "aufs.h"
22685+
4a4d8108
AM
22686+enum { AuSRC, AuDST, AuSrcDst };
22687+enum { AuPARENT, AuCHILD, AuParentChild };
1facf9fc 22688+
f2c43d5f
AM
22689+#define AuRen_ISDIR_SRC 1
22690+#define AuRen_ISDIR_DST (1 << 1)
22691+#define AuRen_ISSAMEDIR (1 << 2)
22692+#define AuRen_WHSRC (1 << 3)
22693+#define AuRen_WHDST (1 << 4)
22694+#define AuRen_MNT_WRITE (1 << 5)
22695+#define AuRen_DT_DSTDIR (1 << 6)
22696+#define AuRen_DIROPQ_SRC (1 << 7)
22697+#define AuRen_DIROPQ_DST (1 << 8)
8b6a4947
AM
22698+#define AuRen_DIRREN (1 << 9)
22699+#define AuRen_DROPPED_SRC (1 << 10)
22700+#define AuRen_DROPPED_DST (1 << 11)
4a4d8108 22701+#define au_ftest_ren(flags, name) ((flags) & AuRen_##name)
7f207e10
AM
22702+#define au_fset_ren(flags, name) \
22703+ do { (flags) |= AuRen_##name; } while (0)
22704+#define au_fclr_ren(flags, name) \
22705+ do { (flags) &= ~AuRen_##name; } while (0)
1facf9fc 22706+
8b6a4947
AM
22707+#ifndef CONFIG_AUFS_DIRREN
22708+#undef AuRen_DIRREN
22709+#define AuRen_DIRREN 0
22710+#endif
22711+
4a4d8108
AM
22712+struct au_ren_args {
22713+ struct {
22714+ struct dentry *dentry, *h_dentry, *parent, *h_parent,
22715+ *wh_dentry;
22716+ struct inode *dir, *inode;
f2c43d5f 22717+ struct au_hinode *hdir, *hinode;
4a4d8108 22718+ struct au_dtime dt[AuParentChild];
f2c43d5f 22719+ aufs_bindex_t btop, bdiropq;
4a4d8108 22720+ } sd[AuSrcDst];
1facf9fc 22721+
4a4d8108
AM
22722+#define src_dentry sd[AuSRC].dentry
22723+#define src_dir sd[AuSRC].dir
22724+#define src_inode sd[AuSRC].inode
22725+#define src_h_dentry sd[AuSRC].h_dentry
22726+#define src_parent sd[AuSRC].parent
22727+#define src_h_parent sd[AuSRC].h_parent
22728+#define src_wh_dentry sd[AuSRC].wh_dentry
22729+#define src_hdir sd[AuSRC].hdir
f2c43d5f 22730+#define src_hinode sd[AuSRC].hinode
4a4d8108
AM
22731+#define src_h_dir sd[AuSRC].hdir->hi_inode
22732+#define src_dt sd[AuSRC].dt
5afbbe0d 22733+#define src_btop sd[AuSRC].btop
f2c43d5f 22734+#define src_bdiropq sd[AuSRC].bdiropq
1facf9fc 22735+
4a4d8108
AM
22736+#define dst_dentry sd[AuDST].dentry
22737+#define dst_dir sd[AuDST].dir
22738+#define dst_inode sd[AuDST].inode
22739+#define dst_h_dentry sd[AuDST].h_dentry
22740+#define dst_parent sd[AuDST].parent
22741+#define dst_h_parent sd[AuDST].h_parent
22742+#define dst_wh_dentry sd[AuDST].wh_dentry
22743+#define dst_hdir sd[AuDST].hdir
f2c43d5f 22744+#define dst_hinode sd[AuDST].hinode
4a4d8108
AM
22745+#define dst_h_dir sd[AuDST].hdir->hi_inode
22746+#define dst_dt sd[AuDST].dt
5afbbe0d 22747+#define dst_btop sd[AuDST].btop
f2c43d5f 22748+#define dst_bdiropq sd[AuDST].bdiropq
4a4d8108
AM
22749+
22750+ struct dentry *h_trap;
22751+ struct au_branch *br;
4a4d8108
AM
22752+ struct path h_path;
22753+ struct au_nhash whlist;
f2c43d5f 22754+ aufs_bindex_t btgt, src_bwh;
1facf9fc 22755+
f2c43d5f
AM
22756+ struct {
22757+ unsigned short auren_flags;
22758+ unsigned char flags; /* syscall parameter */
22759+ unsigned char exchange;
22760+ } __packed;
1facf9fc 22761+
4a4d8108
AM
22762+ struct au_whtmp_rmdir *thargs;
22763+ struct dentry *h_dst;
8b6a4947 22764+ struct au_hinode *h_root;
4a4d8108 22765+};
1308ab2a 22766+
4a4d8108 22767+/* ---------------------------------------------------------------------- */
1308ab2a 22768+
4a4d8108
AM
22769+/*
22770+ * functions for reverting.
22771+ * when an error happened in a single rename systemcall, we should revert
79b8bda9 22772+ * everything as if nothing happened.
4a4d8108
AM
22773+ * we don't need to revert the copied-up/down the parent dir since they are
22774+ * harmless.
22775+ */
1facf9fc 22776+
4a4d8108
AM
22777+#define RevertFailure(fmt, ...) do { \
22778+ AuIOErr("revert failure: " fmt " (%d, %d)\n", \
22779+ ##__VA_ARGS__, err, rerr); \
22780+ err = -EIO; \
22781+} while (0)
1facf9fc 22782+
f2c43d5f 22783+static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
1facf9fc 22784+{
4a4d8108 22785+ int rerr;
f2c43d5f
AM
22786+ struct dentry *d;
22787+#define src_or_dst(member) a->sd[idx].member
1facf9fc 22788+
f2c43d5f
AM
22789+ d = src_or_dst(dentry); /* {src,dst}_dentry */
22790+ au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22791+ rerr = au_diropq_remove(d, a->btgt);
22792+ au_hn_inode_unlock(src_or_dst(hinode));
22793+ au_set_dbdiropq(d, src_or_dst(bdiropq));
4a4d8108 22794+ if (rerr)
f2c43d5f
AM
22795+ RevertFailure("remove diropq %pd", d);
22796+
22797+#undef src_or_dst_
22798+}
22799+
22800+static void au_ren_rev_diropq(int err, struct au_ren_args *a)
22801+{
22802+ if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
22803+ au_ren_do_rev_diropq(err, a, AuSRC);
22804+ if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
22805+ au_ren_do_rev_diropq(err, a, AuDST);
4a4d8108 22806+}
1facf9fc 22807+
4a4d8108
AM
22808+static void au_ren_rev_rename(int err, struct au_ren_args *a)
22809+{
22810+ int rerr;
523b37e3 22811+ struct inode *delegated;
1facf9fc 22812+
b4510431
AM
22813+ a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name,
22814+ a->src_h_parent);
4a4d8108
AM
22815+ rerr = PTR_ERR(a->h_path.dentry);
22816+ if (IS_ERR(a->h_path.dentry)) {
523b37e3 22817+ RevertFailure("lkup one %pd", a->src_dentry);
4a4d8108 22818+ return;
1facf9fc 22819+ }
22820+
523b37e3 22821+ delegated = NULL;
4a4d8108
AM
22822+ rerr = vfsub_rename(a->dst_h_dir,
22823+ au_h_dptr(a->src_dentry, a->btgt),
f2c43d5f 22824+ a->src_h_dir, &a->h_path, &delegated, a->flags);
523b37e3
AM
22825+ if (unlikely(rerr == -EWOULDBLOCK)) {
22826+ pr_warn("cannot retry for NFSv4 delegation"
22827+ " for an internal rename\n");
22828+ iput(delegated);
22829+ }
4a4d8108
AM
22830+ d_drop(a->h_path.dentry);
22831+ dput(a->h_path.dentry);
22832+ /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
22833+ if (rerr)
523b37e3 22834+ RevertFailure("rename %pd", a->src_dentry);
1facf9fc 22835+}
22836+
4a4d8108 22837+static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
1facf9fc 22838+{
4a4d8108 22839+ int rerr;
523b37e3 22840+ struct inode *delegated;
dece6358 22841+
b4510431
AM
22842+ a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name,
22843+ a->dst_h_parent);
4a4d8108
AM
22844+ rerr = PTR_ERR(a->h_path.dentry);
22845+ if (IS_ERR(a->h_path.dentry)) {
523b37e3 22846+ RevertFailure("lkup one %pd", a->dst_dentry);
4a4d8108
AM
22847+ return;
22848+ }
5527c038 22849+ if (d_is_positive(a->h_path.dentry)) {
4a4d8108
AM
22850+ d_drop(a->h_path.dentry);
22851+ dput(a->h_path.dentry);
22852+ return;
dece6358
AM
22853+ }
22854+
523b37e3
AM
22855+ delegated = NULL;
22856+ rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
f2c43d5f 22857+ &delegated, a->flags);
523b37e3
AM
22858+ if (unlikely(rerr == -EWOULDBLOCK)) {
22859+ pr_warn("cannot retry for NFSv4 delegation"
22860+ " for an internal rename\n");
22861+ iput(delegated);
22862+ }
4a4d8108
AM
22863+ d_drop(a->h_path.dentry);
22864+ dput(a->h_path.dentry);
22865+ if (!rerr)
22866+ au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
22867+ else
523b37e3 22868+ RevertFailure("rename %pd", a->h_dst);
4a4d8108 22869+}
1308ab2a 22870+
4a4d8108
AM
22871+static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
22872+{
22873+ int rerr;
1308ab2a 22874+
4a4d8108
AM
22875+ a->h_path.dentry = a->src_wh_dentry;
22876+ rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
027c5e7a 22877+ au_set_dbwh(a->src_dentry, a->src_bwh);
4a4d8108 22878+ if (rerr)
523b37e3 22879+ RevertFailure("unlink %pd", a->src_wh_dentry);
4a4d8108 22880+}
4a4d8108 22881+#undef RevertFailure
1facf9fc 22882+
1308ab2a 22883+/* ---------------------------------------------------------------------- */
22884+
4a4d8108
AM
22885+/*
22886+ * when we have to copyup the renaming entry, do it with the rename-target name
22887+ * in order to minimize the cost (the later actual rename is unnecessary).
22888+ * otherwise rename it on the target branch.
22889+ */
22890+static int au_ren_or_cpup(struct au_ren_args *a)
1facf9fc 22891+{
dece6358 22892+ int err;
4a4d8108 22893+ struct dentry *d;
523b37e3 22894+ struct inode *delegated;
1facf9fc 22895+
4a4d8108 22896+ d = a->src_dentry;
5afbbe0d 22897+ if (au_dbtop(d) == a->btgt) {
4a4d8108 22898+ a->h_path.dentry = a->dst_h_dentry;
5afbbe0d 22899+ AuDebugOn(au_dbtop(d) != a->btgt);
523b37e3 22900+ delegated = NULL;
4a4d8108 22901+ err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
f2c43d5f
AM
22902+ a->dst_h_dir, &a->h_path, &delegated,
22903+ a->flags);
523b37e3
AM
22904+ if (unlikely(err == -EWOULDBLOCK)) {
22905+ pr_warn("cannot retry for NFSv4 delegation"
22906+ " for an internal rename\n");
22907+ iput(delegated);
22908+ }
c2b27bf2 22909+ } else
86dc4139 22910+ BUG();
1308ab2a 22911+
027c5e7a
AM
22912+ if (!err && a->h_dst)
22913+ /* it will be set to dinfo later */
22914+ dget(a->h_dst);
1facf9fc 22915+
dece6358
AM
22916+ return err;
22917+}
1facf9fc 22918+
4a4d8108
AM
22919+/* cf. aufs_rmdir() */
22920+static int au_ren_del_whtmp(struct au_ren_args *a)
dece6358 22921+{
4a4d8108
AM
22922+ int err;
22923+ struct inode *dir;
1facf9fc 22924+
4a4d8108
AM
22925+ dir = a->dst_dir;
22926+ SiMustAnyLock(dir->i_sb);
22927+ if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
22928+ au_sbi(dir->i_sb)->si_dirwh)
22929+ || au_test_fs_remote(a->h_dst->d_sb)) {
22930+ err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
22931+ if (unlikely(err))
523b37e3
AM
22932+ pr_warn("failed removing whtmp dir %pd (%d), "
22933+ "ignored.\n", a->h_dst, err);
4a4d8108
AM
22934+ } else {
22935+ au_nhash_wh_free(&a->thargs->whlist);
22936+ a->thargs->whlist = a->whlist;
22937+ a->whlist.nh_num = 0;
22938+ au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
22939+ dput(a->h_dst);
22940+ a->thargs = NULL;
22941+ }
22942+
22943+ return 0;
1308ab2a 22944+}
1facf9fc 22945+
4a4d8108 22946+/* make it 'opaque' dir. */
f2c43d5f 22947+static int au_ren_do_diropq(struct au_ren_args *a, int idx)
4a4d8108
AM
22948+{
22949+ int err;
f2c43d5f
AM
22950+ struct dentry *d, *diropq;
22951+#define src_or_dst(member) a->sd[idx].member
1facf9fc 22952+
4a4d8108 22953+ err = 0;
f2c43d5f
AM
22954+ d = src_or_dst(dentry); /* {src,dst}_dentry */
22955+ src_or_dst(bdiropq) = au_dbdiropq(d);
22956+ src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
22957+ au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22958+ diropq = au_diropq_create(d, a->btgt);
22959+ au_hn_inode_unlock(src_or_dst(hinode));
4a4d8108
AM
22960+ if (IS_ERR(diropq))
22961+ err = PTR_ERR(diropq);
076b876e
AM
22962+ else
22963+ dput(diropq);
1facf9fc 22964+
f2c43d5f 22965+#undef src_or_dst_
4a4d8108
AM
22966+ return err;
22967+}
1facf9fc 22968+
f2c43d5f 22969+static int au_ren_diropq(struct au_ren_args *a)
4a4d8108
AM
22970+{
22971+ int err;
f2c43d5f
AM
22972+ unsigned char always;
22973+ struct dentry *d;
1facf9fc 22974+
f2c43d5f
AM
22975+ err = 0;
22976+ d = a->dst_dentry; /* already renamed on the branch */
22977+ always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
22978+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
8b6a4947 22979+ && !au_ftest_ren(a->auren_flags, DIRREN)
f2c43d5f
AM
22980+ && a->btgt != au_dbdiropq(a->src_dentry)
22981+ && (a->dst_wh_dentry
22982+ || a->btgt <= au_dbdiropq(d)
22983+ /* hide the lower to keep xino */
22984+ /* the lowers may not be a dir, but we hide them anyway */
22985+ || a->btgt < au_dbbot(d)
22986+ || always)) {
22987+ AuDbg("here\n");
22988+ err = au_ren_do_diropq(a, AuSRC);
22989+ if (unlikely(err))
4a4d8108 22990+ goto out;
f2c43d5f 22991+ au_fset_ren(a->auren_flags, DIROPQ_SRC);
4a4d8108 22992+ }
f2c43d5f
AM
22993+ if (!a->exchange)
22994+ goto out; /* success */
1facf9fc 22995+
f2c43d5f
AM
22996+ d = a->src_dentry; /* already renamed on the branch */
22997+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)
22998+ && a->btgt != au_dbdiropq(a->dst_dentry)
22999+ && (a->btgt < au_dbdiropq(d)
23000+ || a->btgt < au_dbbot(d)
23001+ || always)) {
23002+ AuDbgDentry(a->src_dentry);
23003+ AuDbgDentry(a->dst_dentry);
23004+ err = au_ren_do_diropq(a, AuDST);
4a4d8108 23005+ if (unlikely(err))
f2c43d5f
AM
23006+ goto out_rev_src;
23007+ au_fset_ren(a->auren_flags, DIROPQ_DST);
23008+ }
23009+ goto out; /* success */
dece6358 23010+
f2c43d5f
AM
23011+out_rev_src:
23012+ AuDbg("err %d, reverting src\n", err);
23013+ au_ren_rev_diropq(err, a);
23014+out:
23015+ return err;
23016+}
23017+
23018+static int do_rename(struct au_ren_args *a)
23019+{
23020+ int err;
23021+ struct dentry *d, *h_d;
23022+
23023+ if (!a->exchange) {
23024+ /* prepare workqueue args for asynchronous rmdir */
23025+ h_d = a->dst_h_dentry;
23026+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)
8b6a4947 23027+ /* && !au_ftest_ren(a->auren_flags, DIRREN) */
f2c43d5f
AM
23028+ && d_is_positive(h_d)) {
23029+ err = -ENOMEM;
23030+ a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
23031+ GFP_NOFS);
23032+ if (unlikely(!a->thargs))
23033+ goto out;
23034+ a->h_dst = dget(h_d);
23035+ }
23036+
23037+ /* create whiteout for src_dentry */
23038+ if (au_ftest_ren(a->auren_flags, WHSRC)) {
23039+ a->src_bwh = au_dbwh(a->src_dentry);
23040+ AuDebugOn(a->src_bwh >= 0);
23041+ a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
23042+ a->src_h_parent);
23043+ err = PTR_ERR(a->src_wh_dentry);
23044+ if (IS_ERR(a->src_wh_dentry))
23045+ goto out_thargs;
23046+ }
23047+
23048+ /* lookup whiteout for dentry */
23049+ if (au_ftest_ren(a->auren_flags, WHDST)) {
23050+ h_d = au_wh_lkup(a->dst_h_parent,
23051+ &a->dst_dentry->d_name, a->br);
23052+ err = PTR_ERR(h_d);
23053+ if (IS_ERR(h_d))
23054+ goto out_whsrc;
23055+ if (d_is_negative(h_d))
23056+ dput(h_d);
23057+ else
23058+ a->dst_wh_dentry = h_d;
23059+ }
23060+
23061+ /* rename dentry to tmpwh */
23062+ if (a->thargs) {
23063+ err = au_whtmp_ren(a->dst_h_dentry, a->br);
23064+ if (unlikely(err))
23065+ goto out_whdst;
23066+
23067+ d = a->dst_dentry;
23068+ au_set_h_dptr(d, a->btgt, NULL);
23069+ err = au_lkup_neg(d, a->btgt, /*wh*/0);
23070+ if (unlikely(err))
23071+ goto out_whtmp;
23072+ a->dst_h_dentry = au_h_dptr(d, a->btgt);
23073+ }
4a4d8108 23074+ }
1facf9fc 23075+
5afbbe0d 23076+ BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
0b487229 23077+#if 0 /* debugging */
8b6a4947
AM
23078+ BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN)
23079+ && d_is_positive(a->dst_h_dentry)
23080+ && a->src_btop != a->btgt);
23081+#endif
1facf9fc 23082+
4a4d8108 23083+ /* rename by vfs_rename or cpup */
4a4d8108
AM
23084+ err = au_ren_or_cpup(a);
23085+ if (unlikely(err))
23086+ /* leave the copied-up one */
23087+ goto out_whtmp;
1308ab2a 23088+
4a4d8108 23089+ /* make dir opaque */
f2c43d5f
AM
23090+ err = au_ren_diropq(a);
23091+ if (unlikely(err))
23092+ goto out_rename;
1308ab2a 23093+
4a4d8108 23094+ /* update target timestamps */
f2c43d5f
AM
23095+ if (a->exchange) {
23096+ AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
23097+ a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
23098+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
23099+ a->dst_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
23100+ }
5afbbe0d 23101+ AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
4a4d8108
AM
23102+ a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
23103+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
5527c038 23104+ a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
1facf9fc 23105+
f2c43d5f
AM
23106+ if (!a->exchange) {
23107+ /* remove whiteout for dentry */
23108+ if (a->dst_wh_dentry) {
23109+ a->h_path.dentry = a->dst_wh_dentry;
23110+ err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
23111+ a->dst_dentry);
23112+ if (unlikely(err))
23113+ goto out_diropq;
23114+ }
1facf9fc 23115+
f2c43d5f
AM
23116+ /* remove whtmp */
23117+ if (a->thargs)
23118+ au_ren_del_whtmp(a); /* ignore this error */
1308ab2a 23119+
f2c43d5f
AM
23120+ au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
23121+ }
4a4d8108
AM
23122+ err = 0;
23123+ goto out_success;
23124+
4f0767ce 23125+out_diropq:
f2c43d5f 23126+ au_ren_rev_diropq(err, a);
4f0767ce 23127+out_rename:
7e9cd9fe 23128+ au_ren_rev_rename(err, a);
027c5e7a 23129+ dput(a->h_dst);
4f0767ce 23130+out_whtmp:
4a4d8108
AM
23131+ if (a->thargs)
23132+ au_ren_rev_whtmp(err, a);
4f0767ce 23133+out_whdst:
4a4d8108
AM
23134+ dput(a->dst_wh_dentry);
23135+ a->dst_wh_dentry = NULL;
4f0767ce 23136+out_whsrc:
4a4d8108
AM
23137+ if (a->src_wh_dentry)
23138+ au_ren_rev_whsrc(err, a);
4f0767ce 23139+out_success:
4a4d8108
AM
23140+ dput(a->src_wh_dentry);
23141+ dput(a->dst_wh_dentry);
4f0767ce 23142+out_thargs:
4a4d8108
AM
23143+ if (a->thargs) {
23144+ dput(a->h_dst);
23145+ au_whtmp_rmdir_free(a->thargs);
23146+ a->thargs = NULL;
23147+ }
4f0767ce 23148+out:
4a4d8108 23149+ return err;
dece6358 23150+}
1facf9fc 23151+
1308ab2a 23152+/* ---------------------------------------------------------------------- */
1facf9fc 23153+
4a4d8108
AM
23154+/*
23155+ * test if @dentry dir can be rename destination or not.
23156+ * success means, it is a logically empty dir.
23157+ */
23158+static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
1308ab2a 23159+{
4a4d8108 23160+ return au_test_empty(dentry, whlist);
1308ab2a 23161+}
1facf9fc 23162+
4a4d8108 23163+/*
8b6a4947
AM
23164+ * test if @a->src_dentry dir can be rename source or not.
23165+ * if it can, return 0.
4a4d8108
AM
23166+ * success means,
23167+ * - it is a logically empty dir.
23168+ * - or, it exists on writable branch and has no children including whiteouts
8b6a4947 23169+ * on the lower branch unless DIRREN is on.
4a4d8108 23170+ */
8b6a4947 23171+static int may_rename_srcdir(struct au_ren_args *a)
4a4d8108
AM
23172+{
23173+ int err;
23174+ unsigned int rdhash;
8b6a4947
AM
23175+ aufs_bindex_t btop, btgt;
23176+ struct dentry *dentry;
23177+ struct super_block *sb;
23178+ struct au_sbinfo *sbinfo;
1facf9fc 23179+
8b6a4947
AM
23180+ dentry = a->src_dentry;
23181+ sb = dentry->d_sb;
23182+ sbinfo = au_sbi(sb);
23183+ if (au_opt_test(sbinfo->si_mntflags, DIRREN))
23184+ au_fset_ren(a->auren_flags, DIRREN);
23185+
23186+ btgt = a->btgt;
5afbbe0d
AM
23187+ btop = au_dbtop(dentry);
23188+ if (btop != btgt) {
4a4d8108 23189+ struct au_nhash whlist;
dece6358 23190+
8b6a4947
AM
23191+ SiMustAnyLock(sb);
23192+ rdhash = sbinfo->si_rdhash;
4a4d8108
AM
23193+ if (!rdhash)
23194+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
23195+ dentry));
23196+ err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
23197+ if (unlikely(err))
23198+ goto out;
23199+ err = au_test_empty(dentry, &whlist);
23200+ au_nhash_wh_free(&whlist);
23201+ goto out;
23202+ }
dece6358 23203+
5afbbe0d 23204+ if (btop == au_dbtaildir(dentry))
4a4d8108 23205+ return 0; /* success */
dece6358 23206+
4a4d8108 23207+ err = au_test_empty_lower(dentry);
1facf9fc 23208+
4f0767ce 23209+out:
4a4d8108 23210+ if (err == -ENOTEMPTY) {
8b6a4947
AM
23211+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
23212+ err = 0;
23213+ } else {
23214+ AuWarn1("renaming dir who has child(ren) on multiple "
23215+ "branches, is not supported\n");
23216+ err = -EXDEV;
23217+ }
4a4d8108
AM
23218+ }
23219+ return err;
23220+}
1308ab2a 23221+
4a4d8108
AM
23222+/* side effect: sets whlist and h_dentry */
23223+static int au_ren_may_dir(struct au_ren_args *a)
1308ab2a 23224+{
4a4d8108
AM
23225+ int err;
23226+ unsigned int rdhash;
23227+ struct dentry *d;
1facf9fc 23228+
4a4d8108
AM
23229+ d = a->dst_dentry;
23230+ SiMustAnyLock(d->d_sb);
1facf9fc 23231+
4a4d8108 23232+ err = 0;
f2c43d5f 23233+ if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
4a4d8108
AM
23234+ rdhash = au_sbi(d->d_sb)->si_rdhash;
23235+ if (!rdhash)
23236+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
23237+ err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
23238+ if (unlikely(err))
23239+ goto out;
1308ab2a 23240+
f2c43d5f
AM
23241+ if (!a->exchange) {
23242+ au_set_dbtop(d, a->dst_btop);
23243+ err = may_rename_dstdir(d, &a->whlist);
23244+ au_set_dbtop(d, a->btgt);
23245+ } else
8b6a4947 23246+ err = may_rename_srcdir(a);
4a4d8108 23247+ }
5afbbe0d 23248+ a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
4a4d8108
AM
23249+ if (unlikely(err))
23250+ goto out;
23251+
23252+ d = a->src_dentry;
5afbbe0d 23253+ a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
f2c43d5f 23254+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
8b6a4947 23255+ err = may_rename_srcdir(a);
4a4d8108
AM
23256+ if (unlikely(err)) {
23257+ au_nhash_wh_free(&a->whlist);
23258+ a->whlist.nh_num = 0;
23259+ }
23260+ }
4f0767ce 23261+out:
4a4d8108 23262+ return err;
1facf9fc 23263+}
23264+
4a4d8108 23265+/* ---------------------------------------------------------------------- */
1facf9fc 23266+
4a4d8108
AM
23267+/*
23268+ * simple tests for rename.
23269+ * following the checks in vfs, plus the parent-child relationship.
23270+ */
23271+static int au_may_ren(struct au_ren_args *a)
23272+{
23273+ int err, isdir;
23274+ struct inode *h_inode;
1facf9fc 23275+
5afbbe0d 23276+ if (a->src_btop == a->btgt) {
4a4d8108 23277+ err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
f2c43d5f 23278+ au_ftest_ren(a->auren_flags, ISDIR_SRC));
4a4d8108
AM
23279+ if (unlikely(err))
23280+ goto out;
23281+ err = -EINVAL;
23282+ if (unlikely(a->src_h_dentry == a->h_trap))
23283+ goto out;
23284+ }
1facf9fc 23285+
4a4d8108 23286+ err = 0;
5afbbe0d 23287+ if (a->dst_btop != a->btgt)
4a4d8108 23288+ goto out;
1facf9fc 23289+
027c5e7a
AM
23290+ err = -ENOTEMPTY;
23291+ if (unlikely(a->dst_h_dentry == a->h_trap))
23292+ goto out;
23293+
4a4d8108 23294+ err = -EIO;
f2c43d5f 23295+ isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
5527c038
JR
23296+ if (d_really_is_negative(a->dst_dentry)) {
23297+ if (d_is_negative(a->dst_h_dentry))
23298+ err = au_may_add(a->dst_dentry, a->btgt,
23299+ a->dst_h_parent, isdir);
4a4d8108 23300+ } else {
5527c038 23301+ if (unlikely(d_is_negative(a->dst_h_dentry)))
4a4d8108 23302+ goto out;
5527c038
JR
23303+ h_inode = d_inode(a->dst_h_dentry);
23304+ if (h_inode->i_nlink)
23305+ err = au_may_del(a->dst_dentry, a->btgt,
23306+ a->dst_h_parent, isdir);
4a4d8108 23307+ }
1facf9fc 23308+
4f0767ce 23309+out:
4a4d8108
AM
23310+ if (unlikely(err == -ENOENT || err == -EEXIST))
23311+ err = -EIO;
23312+ AuTraceErr(err);
23313+ return err;
23314+}
1facf9fc 23315+
1308ab2a 23316+/* ---------------------------------------------------------------------- */
1facf9fc 23317+
4a4d8108
AM
23318+/*
23319+ * locking order
23320+ * (VFS)
23321+ * - src_dir and dir by lock_rename()
acd2b654 23322+ * - inode if exists
4a4d8108
AM
23323+ * (aufs)
23324+ * - lock all
23325+ * + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
23326+ * + si_read_lock
23327+ * + di_write_lock2_child()
23328+ * + di_write_lock_child()
23329+ * + ii_write_lock_child()
23330+ * + di_write_lock_child2()
23331+ * + ii_write_lock_child2()
23332+ * + src_parent and parent
23333+ * + di_write_lock_parent()
23334+ * + ii_write_lock_parent()
23335+ * + di_write_lock_parent2()
23336+ * + ii_write_lock_parent2()
23337+ * + lower src_dir and dir by vfsub_lock_rename()
23338+ * + verify the every relationships between child and parent. if any
23339+ * of them failed, unlock all and return -EBUSY.
23340+ */
23341+static void au_ren_unlock(struct au_ren_args *a)
1308ab2a 23342+{
4a4d8108
AM
23343+ vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
23344+ a->dst_h_parent, a->dst_hdir);
8b6a4947
AM
23345+ if (au_ftest_ren(a->auren_flags, DIRREN)
23346+ && a->h_root)
23347+ au_hn_inode_unlock(a->h_root);
f2c43d5f 23348+ if (au_ftest_ren(a->auren_flags, MNT_WRITE))
86dc4139 23349+ vfsub_mnt_drop_write(au_br_mnt(a->br));
1308ab2a 23350+}
23351+
4a4d8108 23352+static int au_ren_lock(struct au_ren_args *a)
1308ab2a 23353+{
4a4d8108
AM
23354+ int err;
23355+ unsigned int udba;
1308ab2a 23356+
4a4d8108
AM
23357+ err = 0;
23358+ a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
23359+ a->src_hdir = au_hi(a->src_dir, a->btgt);
23360+ a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
23361+ a->dst_hdir = au_hi(a->dst_dir, a->btgt);
86dc4139
AM
23362+
23363+ err = vfsub_mnt_want_write(au_br_mnt(a->br));
23364+ if (unlikely(err))
23365+ goto out;
f2c43d5f 23366+ au_fset_ren(a->auren_flags, MNT_WRITE);
8b6a4947
AM
23367+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
23368+ struct dentry *root;
23369+ struct inode *dir;
23370+
23371+ /*
23372+ * sbinfo is already locked, so this ii_read_lock is
23373+ * unnecessary. but our debugging feature checks it.
23374+ */
23375+ root = a->src_inode->i_sb->s_root;
23376+ if (root != a->src_parent && root != a->dst_parent) {
23377+ dir = d_inode(root);
23378+ ii_read_lock_parent3(dir);
23379+ a->h_root = au_hi(dir, a->btgt);
23380+ ii_read_unlock(dir);
23381+ au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3);
23382+ }
23383+ }
4a4d8108
AM
23384+ a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
23385+ a->dst_h_parent, a->dst_hdir);
23386+ udba = au_opt_udba(a->src_dentry->d_sb);
5527c038
JR
23387+ if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
23388+ || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
4a4d8108 23389+ err = au_busy_or_stale();
5afbbe0d 23390+ if (!err && au_dbtop(a->src_dentry) == a->btgt)
4a4d8108 23391+ err = au_h_verify(a->src_h_dentry, udba,
5527c038 23392+ d_inode(a->src_h_parent), a->src_h_parent,
4a4d8108 23393+ a->br);
5afbbe0d 23394+ if (!err && au_dbtop(a->dst_dentry) == a->btgt)
4a4d8108 23395+ err = au_h_verify(a->dst_h_dentry, udba,
5527c038 23396+ d_inode(a->dst_h_parent), a->dst_h_parent,
4a4d8108 23397+ a->br);
86dc4139 23398+ if (!err)
4a4d8108 23399+ goto out; /* success */
4a4d8108
AM
23400+
23401+ err = au_busy_or_stale();
4a4d8108 23402+ au_ren_unlock(a);
86dc4139 23403+
4f0767ce 23404+out:
4a4d8108 23405+ return err;
1facf9fc 23406+}
23407+
23408+/* ---------------------------------------------------------------------- */
23409+
4a4d8108 23410+static void au_ren_refresh_dir(struct au_ren_args *a)
1facf9fc 23411+{
4a4d8108 23412+ struct inode *dir;
dece6358 23413+
4a4d8108 23414+ dir = a->dst_dir;
be118d29 23415+ inode_inc_iversion(dir);
f2c43d5f 23416+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
4a4d8108
AM
23417+ /* is this updating defined in POSIX? */
23418+ au_cpup_attr_timesizes(a->src_inode);
23419+ au_cpup_attr_nlink(dir, /*force*/1);
4a4d8108 23420+ }
b912730e 23421+ au_dir_ts(dir, a->btgt);
dece6358 23422+
f2c43d5f
AM
23423+ if (a->exchange) {
23424+ dir = a->src_dir;
be118d29 23425+ inode_inc_iversion(dir);
f2c43d5f
AM
23426+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
23427+ /* is this updating defined in POSIX? */
23428+ au_cpup_attr_timesizes(a->dst_inode);
23429+ au_cpup_attr_nlink(dir, /*force*/1);
23430+ }
23431+ au_dir_ts(dir, a->btgt);
23432+ }
23433+
23434+ if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
4a4d8108 23435+ return;
dece6358 23436+
4a4d8108 23437+ dir = a->src_dir;
be118d29 23438+ inode_inc_iversion(dir);
f2c43d5f 23439+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
4a4d8108 23440+ au_cpup_attr_nlink(dir, /*force*/1);
b912730e 23441+ au_dir_ts(dir, a->btgt);
1facf9fc 23442+}
23443+
4a4d8108 23444+static void au_ren_refresh(struct au_ren_args *a)
1facf9fc 23445+{
5afbbe0d 23446+ aufs_bindex_t bbot, bindex;
4a4d8108
AM
23447+ struct dentry *d, *h_d;
23448+ struct inode *i, *h_i;
23449+ struct super_block *sb;
dece6358 23450+
027c5e7a
AM
23451+ d = a->dst_dentry;
23452+ d_drop(d);
23453+ if (a->h_dst)
23454+ /* already dget-ed by au_ren_or_cpup() */
23455+ au_set_h_dptr(d, a->btgt, a->h_dst);
23456+
23457+ i = a->dst_inode;
23458+ if (i) {
f2c43d5f
AM
23459+ if (!a->exchange) {
23460+ if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
23461+ vfsub_drop_nlink(i);
23462+ else {
23463+ vfsub_dead_dir(i);
23464+ au_cpup_attr_timesizes(i);
23465+ }
23466+ au_update_dbrange(d, /*do_put_zero*/1);
23467+ } else
23468+ au_cpup_attr_nlink(i, /*force*/1);
027c5e7a 23469+ } else {
5afbbe0d
AM
23470+ bbot = a->btgt;
23471+ for (bindex = au_dbtop(d); bindex < bbot; bindex++)
027c5e7a 23472+ au_set_h_dptr(d, bindex, NULL);
5afbbe0d
AM
23473+ bbot = au_dbbot(d);
23474+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
027c5e7a
AM
23475+ au_set_h_dptr(d, bindex, NULL);
23476+ au_update_dbrange(d, /*do_put_zero*/0);
23477+ }
23478+
8b6a4947
AM
23479+ if (a->exchange
23480+ || au_ftest_ren(a->auren_flags, DIRREN)) {
23481+ d_drop(a->src_dentry);
23482+ if (au_ftest_ren(a->auren_flags, DIRREN))
23483+ au_set_dbwh(a->src_dentry, -1);
23484+ return;
23485+ }
23486+
4a4d8108 23487+ d = a->src_dentry;
8b6a4947
AM
23488+ au_set_dbwh(d, -1);
23489+ bbot = au_dbbot(d);
23490+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23491+ h_d = au_h_dptr(d, bindex);
23492+ if (h_d)
23493+ au_set_h_dptr(d, bindex, NULL);
23494+ }
23495+ au_set_dbbot(d, a->btgt);
4a4d8108 23496+
8b6a4947
AM
23497+ sb = d->d_sb;
23498+ i = a->src_inode;
23499+ if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
23500+ return; /* success */
4a4d8108 23501+
8b6a4947
AM
23502+ bbot = au_ibbot(i);
23503+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23504+ h_i = au_h_iptr(i, bindex);
23505+ if (h_i) {
23506+ au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
23507+ /* ignore this error */
23508+ au_set_h_iptr(i, bindex, NULL, 0);
4a4d8108
AM
23509+ }
23510+ }
8b6a4947 23511+ au_set_ibbot(i, a->btgt);
1308ab2a 23512+}
dece6358 23513+
4a4d8108
AM
23514+/* ---------------------------------------------------------------------- */
23515+
23516+/* mainly for link(2) and rename(2) */
23517+int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
1308ab2a 23518+{
4a4d8108
AM
23519+ aufs_bindex_t bdiropq, bwh;
23520+ struct dentry *parent;
23521+ struct au_branch *br;
23522+
23523+ parent = dentry->d_parent;
5527c038 23524+ IMustLock(d_inode(parent)); /* dir is locked */
4a4d8108
AM
23525+
23526+ bdiropq = au_dbdiropq(parent);
23527+ bwh = au_dbwh(dentry);
23528+ br = au_sbr(dentry->d_sb, btgt);
23529+ if (au_br_rdonly(br)
23530+ || (0 <= bdiropq && bdiropq < btgt)
23531+ || (0 <= bwh && bwh < btgt))
23532+ btgt = -1;
23533+
23534+ AuDbg("btgt %d\n", btgt);
23535+ return btgt;
1facf9fc 23536+}
23537+
5afbbe0d 23538+/* sets src_btop, dst_btop and btgt */
4a4d8108 23539+static int au_ren_wbr(struct au_ren_args *a)
1facf9fc 23540+{
4a4d8108
AM
23541+ int err;
23542+ struct au_wr_dir_args wr_dir_args = {
23543+ /* .force_btgt = -1, */
23544+ .flags = AuWrDir_ADD_ENTRY
23545+ };
dece6358 23546+
5afbbe0d
AM
23547+ a->src_btop = au_dbtop(a->src_dentry);
23548+ a->dst_btop = au_dbtop(a->dst_dentry);
f2c43d5f
AM
23549+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
23550+ || au_ftest_ren(a->auren_flags, ISDIR_DST))
4a4d8108 23551+ au_fset_wrdir(wr_dir_args.flags, ISDIR);
5afbbe0d
AM
23552+ wr_dir_args.force_btgt = a->src_btop;
23553+ if (a->dst_inode && a->dst_btop < a->src_btop)
23554+ wr_dir_args.force_btgt = a->dst_btop;
4a4d8108
AM
23555+ wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
23556+ err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
23557+ a->btgt = err;
f2c43d5f
AM
23558+ if (a->exchange)
23559+ au_update_dbtop(a->dst_dentry);
dece6358 23560+
4a4d8108 23561+ return err;
1facf9fc 23562+}
23563+
4a4d8108 23564+static void au_ren_dt(struct au_ren_args *a)
1facf9fc 23565+{
4a4d8108
AM
23566+ a->h_path.dentry = a->src_h_parent;
23567+ au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
f2c43d5f 23568+ if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
4a4d8108
AM
23569+ a->h_path.dentry = a->dst_h_parent;
23570+ au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
23571+ }
1facf9fc 23572+
f2c43d5f
AM
23573+ au_fclr_ren(a->auren_flags, DT_DSTDIR);
23574+ if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
23575+ && !a->exchange)
4a4d8108 23576+ return;
dece6358 23577+
4a4d8108
AM
23578+ a->h_path.dentry = a->src_h_dentry;
23579+ au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
5527c038 23580+ if (d_is_positive(a->dst_h_dentry)) {
f2c43d5f 23581+ au_fset_ren(a->auren_flags, DT_DSTDIR);
4a4d8108
AM
23582+ a->h_path.dentry = a->dst_h_dentry;
23583+ au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
23584+ }
1308ab2a 23585+}
dece6358 23586+
4a4d8108 23587+static void au_ren_rev_dt(int err, struct au_ren_args *a)
1308ab2a 23588+{
4a4d8108 23589+ struct dentry *h_d;
febd17d6 23590+ struct inode *h_inode;
4a4d8108
AM
23591+
23592+ au_dtime_revert(a->src_dt + AuPARENT);
f2c43d5f 23593+ if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
4a4d8108
AM
23594+ au_dtime_revert(a->dst_dt + AuPARENT);
23595+
f2c43d5f 23596+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
4a4d8108 23597+ h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
febd17d6
JR
23598+ h_inode = d_inode(h_d);
23599+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 23600+ au_dtime_revert(a->src_dt + AuCHILD);
febd17d6 23601+ inode_unlock(h_inode);
4a4d8108 23602+
f2c43d5f 23603+ if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
4a4d8108 23604+ h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
febd17d6
JR
23605+ h_inode = d_inode(h_d);
23606+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 23607+ au_dtime_revert(a->dst_dt + AuCHILD);
febd17d6 23608+ inode_unlock(h_inode);
1facf9fc 23609+ }
23610+ }
23611+}
23612+
4a4d8108
AM
23613+/* ---------------------------------------------------------------------- */
23614+
23615+int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
f2c43d5f
AM
23616+ struct inode *_dst_dir, struct dentry *_dst_dentry,
23617+ unsigned int _flags)
1facf9fc 23618+{
f2c43d5f 23619+ int err, lock_flags;
8b6a4947 23620+ void *rev;
4a4d8108
AM
23621+ /* reduce stack space */
23622+ struct au_ren_args *a;
f2c43d5f 23623+ struct au_pin pin;
4a4d8108 23624+
f2c43d5f 23625+ AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
4a4d8108
AM
23626+ IMustLock(_src_dir);
23627+ IMustLock(_dst_dir);
23628+
f2c43d5f
AM
23629+ err = -EINVAL;
23630+ if (unlikely(_flags & RENAME_WHITEOUT))
23631+ goto out;
23632+
4a4d8108
AM
23633+ err = -ENOMEM;
23634+ BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
23635+ a = kzalloc(sizeof(*a), GFP_NOFS);
23636+ if (unlikely(!a))
23637+ goto out;
23638+
f2c43d5f 23639+ a->flags = _flags;
acd2b654
AM
23640+ BUILD_BUG_ON(sizeof(a->exchange) == sizeof(u8)
23641+ && RENAME_EXCHANGE > U8_MAX);
f2c43d5f 23642+ a->exchange = _flags & RENAME_EXCHANGE;
4a4d8108
AM
23643+ a->src_dir = _src_dir;
23644+ a->src_dentry = _src_dentry;
5527c038
JR
23645+ a->src_inode = NULL;
23646+ if (d_really_is_positive(a->src_dentry))
23647+ a->src_inode = d_inode(a->src_dentry);
4a4d8108
AM
23648+ a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
23649+ a->dst_dir = _dst_dir;
23650+ a->dst_dentry = _dst_dentry;
5527c038
JR
23651+ a->dst_inode = NULL;
23652+ if (d_really_is_positive(a->dst_dentry))
23653+ a->dst_inode = d_inode(a->dst_dentry);
4a4d8108
AM
23654+ a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
23655+ if (a->dst_inode) {
f2c43d5f
AM
23656+ /*
23657+ * if EXCHANGE && src is non-dir && dst is dir,
23658+ * dst is not locked.
23659+ */
23660+ /* IMustLock(a->dst_inode); */
4a4d8108 23661+ au_igrab(a->dst_inode);
1facf9fc 23662+ }
1facf9fc 23663+
4a4d8108 23664+ err = -ENOTDIR;
f2c43d5f 23665+ lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
2000de60 23666+ if (d_is_dir(a->src_dentry)) {
f2c43d5f
AM
23667+ au_fset_ren(a->auren_flags, ISDIR_SRC);
23668+ if (unlikely(!a->exchange
23669+ && d_really_is_positive(a->dst_dentry)
2000de60 23670+ && !d_is_dir(a->dst_dentry)))
4a4d8108 23671+ goto out_free;
f2c43d5f
AM
23672+ lock_flags |= AuLock_DIRS;
23673+ }
23674+ if (a->dst_inode && d_is_dir(a->dst_dentry)) {
23675+ au_fset_ren(a->auren_flags, ISDIR_DST);
23676+ if (unlikely(!a->exchange
23677+ && d_really_is_positive(a->src_dentry)
23678+ && !d_is_dir(a->src_dentry)))
23679+ goto out_free;
23680+ lock_flags |= AuLock_DIRS;
b95c5147 23681+ }
8b6a4947
AM
23682+ err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
23683+ lock_flags);
e49829fe
JR
23684+ if (unlikely(err))
23685+ goto out_free;
1facf9fc 23686+
027c5e7a
AM
23687+ err = au_d_hashed_positive(a->src_dentry);
23688+ if (unlikely(err))
23689+ goto out_unlock;
23690+ err = -ENOENT;
23691+ if (a->dst_inode) {
23692+ /*
f2c43d5f 23693+ * If it is a dir, VFS unhash it before this
027c5e7a
AM
23694+ * function. It means we cannot rely upon d_unhashed().
23695+ */
23696+ if (unlikely(!a->dst_inode->i_nlink))
23697+ goto out_unlock;
f2c43d5f 23698+ if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
027c5e7a 23699+ err = au_d_hashed_positive(a->dst_dentry);
f2c43d5f 23700+ if (unlikely(err && !a->exchange))
027c5e7a
AM
23701+ goto out_unlock;
23702+ } else if (unlikely(IS_DEADDIR(a->dst_inode)))
23703+ goto out_unlock;
23704+ } else if (unlikely(d_unhashed(a->dst_dentry)))
23705+ goto out_unlock;
23706+
7eafdf33
AM
23707+ /*
23708+ * is it possible?
79b8bda9 23709+ * yes, it happened (in linux-3.3-rcN) but I don't know why.
7eafdf33
AM
23710+ * there may exist a problem somewhere else.
23711+ */
23712+ err = -EINVAL;
5527c038 23713+ if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
7eafdf33
AM
23714+ goto out_unlock;
23715+
f2c43d5f 23716+ au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
4a4d8108 23717+ di_write_lock_parent(a->dst_parent);
1facf9fc 23718+
4a4d8108
AM
23719+ /* which branch we process */
23720+ err = au_ren_wbr(a);
23721+ if (unlikely(err < 0))
027c5e7a 23722+ goto out_parent;
4a4d8108 23723+ a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
86dc4139 23724+ a->h_path.mnt = au_br_mnt(a->br);
1facf9fc 23725+
4a4d8108
AM
23726+ /* are they available to be renamed */
23727+ err = au_ren_may_dir(a);
23728+ if (unlikely(err))
23729+ goto out_children;
1facf9fc 23730+
4a4d8108 23731+ /* prepare the writable parent dir on the same branch */
5afbbe0d 23732+ if (a->dst_btop == a->btgt) {
f2c43d5f 23733+ au_fset_ren(a->auren_flags, WHDST);
4a4d8108
AM
23734+ } else {
23735+ err = au_cpup_dirs(a->dst_dentry, a->btgt);
23736+ if (unlikely(err))
23737+ goto out_children;
23738+ }
1facf9fc 23739+
f2c43d5f
AM
23740+ err = 0;
23741+ if (!a->exchange) {
23742+ if (a->src_dir != a->dst_dir) {
23743+ /*
23744+ * this temporary unlock is safe,
23745+ * because both dir->i_mutex are locked.
23746+ */
23747+ di_write_unlock(a->dst_parent);
23748+ di_write_lock_parent(a->src_parent);
23749+ err = au_wr_dir_need_wh(a->src_dentry,
23750+ au_ftest_ren(a->auren_flags,
23751+ ISDIR_SRC),
23752+ &a->btgt);
23753+ di_write_unlock(a->src_parent);
23754+ di_write_lock2_parent(a->src_parent, a->dst_parent,
23755+ /*isdir*/1);
23756+ au_fclr_ren(a->auren_flags, ISSAMEDIR);
23757+ } else
23758+ err = au_wr_dir_need_wh(a->src_dentry,
23759+ au_ftest_ren(a->auren_flags,
23760+ ISDIR_SRC),
23761+ &a->btgt);
23762+ }
4a4d8108
AM
23763+ if (unlikely(err < 0))
23764+ goto out_children;
23765+ if (err)
f2c43d5f 23766+ au_fset_ren(a->auren_flags, WHSRC);
1facf9fc 23767+
86dc4139 23768+ /* cpup src */
5afbbe0d 23769+ if (a->src_btop != a->btgt) {
86dc4139
AM
23770+ err = au_pin(&pin, a->src_dentry, a->btgt,
23771+ au_opt_udba(a->src_dentry->d_sb),
23772+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
367653fa 23773+ if (!err) {
c2b27bf2
AM
23774+ struct au_cp_generic cpg = {
23775+ .dentry = a->src_dentry,
23776+ .bdst = a->btgt,
5afbbe0d 23777+ .bsrc = a->src_btop,
c2b27bf2
AM
23778+ .len = -1,
23779+ .pin = &pin,
23780+ .flags = AuCpup_DTIME | AuCpup_HOPEN
23781+ };
5afbbe0d 23782+ AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
c2b27bf2 23783+ err = au_sio_cpup_simple(&cpg);
367653fa 23784+ au_unpin(&pin);
86dc4139 23785+ }
86dc4139
AM
23786+ if (unlikely(err))
23787+ goto out_children;
5afbbe0d 23788+ a->src_btop = a->btgt;
86dc4139 23789+ a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
f2c43d5f
AM
23790+ if (!a->exchange)
23791+ au_fset_ren(a->auren_flags, WHSRC);
23792+ }
23793+
23794+ /* cpup dst */
23795+ if (a->exchange && a->dst_inode
23796+ && a->dst_btop != a->btgt) {
23797+ err = au_pin(&pin, a->dst_dentry, a->btgt,
23798+ au_opt_udba(a->dst_dentry->d_sb),
23799+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23800+ if (!err) {
23801+ struct au_cp_generic cpg = {
23802+ .dentry = a->dst_dentry,
23803+ .bdst = a->btgt,
23804+ .bsrc = a->dst_btop,
23805+ .len = -1,
23806+ .pin = &pin,
23807+ .flags = AuCpup_DTIME | AuCpup_HOPEN
23808+ };
23809+ err = au_sio_cpup_simple(&cpg);
23810+ au_unpin(&pin);
23811+ }
23812+ if (unlikely(err))
23813+ goto out_children;
23814+ a->dst_btop = a->btgt;
23815+ a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
86dc4139
AM
23816+ }
23817+
4a4d8108
AM
23818+ /* lock them all */
23819+ err = au_ren_lock(a);
23820+ if (unlikely(err))
86dc4139 23821+ /* leave the copied-up one */
4a4d8108 23822+ goto out_children;
1facf9fc 23823+
f2c43d5f
AM
23824+ if (!a->exchange) {
23825+ if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
23826+ err = au_may_ren(a);
23827+ else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
23828+ err = -ENAMETOOLONG;
23829+ if (unlikely(err))
23830+ goto out_hdir;
23831+ }
1facf9fc 23832+
4a4d8108
AM
23833+ /* store timestamps to be revertible */
23834+ au_ren_dt(a);
1facf9fc 23835+
8b6a4947
AM
23836+ /* store dirren info */
23837+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
23838+ err = au_dr_rename(a->src_dentry, a->btgt,
23839+ &a->dst_dentry->d_name, &rev);
23840+ AuTraceErr(err);
23841+ if (unlikely(err))
23842+ goto out_dt;
23843+ }
23844+
4a4d8108
AM
23845+ /* here we go */
23846+ err = do_rename(a);
23847+ if (unlikely(err))
8b6a4947
AM
23848+ goto out_dirren;
23849+
23850+ if (au_ftest_ren(a->auren_flags, DIRREN))
23851+ au_dr_rename_fin(a->src_dentry, a->btgt, rev);
4a4d8108
AM
23852+
23853+ /* update dir attributes */
23854+ au_ren_refresh_dir(a);
23855+
23856+ /* dput/iput all lower dentries */
23857+ au_ren_refresh(a);
23858+
23859+ goto out_hdir; /* success */
23860+
8b6a4947
AM
23861+out_dirren:
23862+ if (au_ftest_ren(a->auren_flags, DIRREN))
23863+ au_dr_rename_rev(a->src_dentry, a->btgt, rev);
4f0767ce 23864+out_dt:
4a4d8108 23865+ au_ren_rev_dt(err, a);
4f0767ce 23866+out_hdir:
4a4d8108 23867+ au_ren_unlock(a);
4f0767ce 23868+out_children:
4a4d8108 23869+ au_nhash_wh_free(&a->whlist);
5afbbe0d
AM
23870+ if (err && a->dst_inode && a->dst_btop != a->btgt) {
23871+ AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
027c5e7a 23872+ au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
5afbbe0d 23873+ au_set_dbtop(a->dst_dentry, a->dst_btop);
4a4d8108 23874+ }
027c5e7a 23875+out_parent:
f2c43d5f 23876+ if (!err) {
8b6a4947
AM
23877+ if (d_unhashed(a->src_dentry))
23878+ au_fset_ren(a->auren_flags, DROPPED_SRC);
23879+ if (d_unhashed(a->dst_dentry))
23880+ au_fset_ren(a->auren_flags, DROPPED_DST);
f2c43d5f
AM
23881+ if (!a->exchange)
23882+ d_move(a->src_dentry, a->dst_dentry);
8b6a4947 23883+ else {
f2c43d5f 23884+ d_exchange(a->src_dentry, a->dst_dentry);
8b6a4947
AM
23885+ if (au_ftest_ren(a->auren_flags, DROPPED_DST))
23886+ d_drop(a->dst_dentry);
23887+ }
23888+ if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
23889+ d_drop(a->src_dentry);
f2c43d5f 23890+ } else {
5afbbe0d 23891+ au_update_dbtop(a->dst_dentry);
027c5e7a
AM
23892+ if (!a->dst_inode)
23893+ d_drop(a->dst_dentry);
23894+ }
f2c43d5f 23895+ if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
4a4d8108
AM
23896+ di_write_unlock(a->dst_parent);
23897+ else
23898+ di_write_unlock2(a->src_parent, a->dst_parent);
027c5e7a 23899+out_unlock:
4a4d8108 23900+ aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
4f0767ce 23901+out_free:
4a4d8108
AM
23902+ iput(a->dst_inode);
23903+ if (a->thargs)
23904+ au_whtmp_rmdir_free(a->thargs);
e49925d1 23905+ au_kfree_rcu(a);
4f0767ce 23906+out:
4a4d8108
AM
23907+ AuTraceErr(err);
23908+ return err;
1308ab2a 23909+}
7f207e10
AM
23910diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
23911--- /usr/share/empty/fs/aufs/Kconfig 1970-01-01 01:00:00.000000000 +0100
0b487229 23912+++ linux/fs/aufs/Kconfig 2021-02-22 23:30:37.699347580 +0100
2121bcd9
AM
23913@@ -0,0 +1,199 @@
23914+# SPDX-License-Identifier: GPL-2.0
4a4d8108
AM
23915+config AUFS_FS
23916+ tristate "Aufs (Advanced multi layered unification filesystem) support"
4a4d8108
AM
23917+ help
23918+ Aufs is a stackable unification filesystem such as Unionfs,
23919+ which unifies several directories and provides a merged single
23920+ directory.
23921+ In the early days, aufs was entirely re-designed and
23922+ re-implemented Unionfs Version 1.x series. Introducing many
23923+ original ideas, approaches and improvements, it becomes totally
23924+ different from Unionfs while keeping the basic features.
1facf9fc 23925+
4a4d8108
AM
23926+if AUFS_FS
23927+choice
23928+ prompt "Maximum number of branches"
23929+ default AUFS_BRANCH_MAX_127
23930+ help
23931+ Specifies the maximum number of branches (or member directories)
23932+ in a single aufs. The larger value consumes more system
23933+ resources and has a minor impact to performance.
23934+config AUFS_BRANCH_MAX_127
23935+ bool "127"
23936+ help
23937+ Specifies the maximum number of branches (or member directories)
23938+ in a single aufs. The larger value consumes more system
23939+ resources and has a minor impact to performance.
23940+config AUFS_BRANCH_MAX_511
23941+ bool "511"
23942+ help
23943+ Specifies the maximum number of branches (or member directories)
23944+ in a single aufs. The larger value consumes more system
23945+ resources and has a minor impact to performance.
23946+config AUFS_BRANCH_MAX_1023
23947+ bool "1023"
23948+ help
23949+ Specifies the maximum number of branches (or member directories)
23950+ in a single aufs. The larger value consumes more system
23951+ resources and has a minor impact to performance.
23952+config AUFS_BRANCH_MAX_32767
23953+ bool "32767"
23954+ help
23955+ Specifies the maximum number of branches (or member directories)
23956+ in a single aufs. The larger value consumes more system
23957+ resources and has a minor impact to performance.
23958+endchoice
1facf9fc 23959+
e49829fe
JR
23960+config AUFS_SBILIST
23961+ bool
23962+ depends on AUFS_MAGIC_SYSRQ || PROC_FS
23963+ default y
23964+ help
23965+ Automatic configuration for internal use.
23966+ When aufs supports Magic SysRq or /proc, enabled automatically.
23967+
4a4d8108
AM
23968+config AUFS_HNOTIFY
23969+ bool "Detect direct branch access (bypassing aufs)"
23970+ help
23971+ If you want to modify files on branches directly, eg. bypassing aufs,
23972+ and want aufs to detect the changes of them fully, then enable this
23973+ option and use 'udba=notify' mount option.
7f207e10 23974+ Currently there is only one available configuration, "fsnotify".
4a4d8108
AM
23975+ It will have a negative impact to the performance.
23976+ See detail in aufs.5.
dece6358 23977+
4a4d8108
AM
23978+choice
23979+ prompt "method" if AUFS_HNOTIFY
23980+ default AUFS_HFSNOTIFY
23981+config AUFS_HFSNOTIFY
23982+ bool "fsnotify"
23983+ select FSNOTIFY
4a4d8108 23984+endchoice
1facf9fc 23985+
4a4d8108
AM
23986+config AUFS_EXPORT
23987+ bool "NFS-exportable aufs"
2cbb1c4b 23988+ depends on EXPORTFS
4a4d8108
AM
23989+ help
23990+ If you want to export your mounted aufs via NFS, then enable this
23991+ option. There are several requirements for this configuration.
23992+ See detail in aufs.5.
1facf9fc 23993+
4a4d8108
AM
23994+config AUFS_INO_T_64
23995+ bool
23996+ depends on AUFS_EXPORT
23997+ depends on 64BIT && !(ALPHA || S390)
23998+ default y
23999+ help
24000+ Automatic configuration for internal use.
24001+ /* typedef unsigned long/int __kernel_ino_t */
24002+ /* alpha and s390x are int */
1facf9fc 24003+
c1595e42
JR
24004+config AUFS_XATTR
24005+ bool "support for XATTR/EA (including Security Labels)"
24006+ help
24007+ If your branch fs supports XATTR/EA and you want to make them
24008+ available in aufs too, then enable this opsion and specify the
24009+ branch attributes for EA.
24010+ See detail in aufs.5.
24011+
076b876e
AM
24012+config AUFS_FHSM
24013+ bool "File-based Hierarchical Storage Management"
24014+ help
24015+ Hierarchical Storage Management (or HSM) is a well-known feature
24016+ in the storage world. Aufs provides this feature as file-based.
24017+ with multiple branches.
24018+ These multiple branches are prioritized, ie. the topmost one
24019+ should be the fastest drive and be used heavily.
24020+
4a4d8108
AM
24021+config AUFS_RDU
24022+ bool "Readdir in userspace"
24023+ help
24024+ Aufs has two methods to provide a merged view for a directory,
24025+ by a user-space library and by kernel-space natively. The latter
24026+ is always enabled but sometimes large and slow.
24027+ If you enable this option, install the library in aufs2-util
24028+ package, and set some environment variables for your readdir(3),
24029+ then the work will be handled in user-space which generally
24030+ shows better performance in most cases.
24031+ See detail in aufs.5.
1facf9fc 24032+
8b6a4947
AM
24033+config AUFS_DIRREN
24034+ bool "Workaround for rename(2)-ing a directory"
24035+ help
24036+ By default, aufs returns EXDEV error in renameing a dir who has
24037+ his child on the lower branch, since it is a bad idea to issue
24038+ rename(2) internally for every lower branch. But user may not
24039+ accept this behaviour. So here is a workaround to allow such
24040+ rename(2) and store some extra infromation on the writable
24041+ branch. Obviously this costs high (and I don't like it).
24042+ To use this feature, you need to enable this configuration AND
24043+ to specify the mount option `dirren.'
24044+ See details in aufs.5 and the design documents.
24045+
4a4d8108
AM
24046+config AUFS_SHWH
24047+ bool "Show whiteouts"
24048+ help
24049+ If you want to make the whiteouts in aufs visible, then enable
24050+ this option and specify 'shwh' mount option. Although it may
24051+ sounds like philosophy or something, but in technically it
24052+ simply shows the name of whiteout with keeping its behaviour.
1facf9fc 24053+
4a4d8108
AM
24054+config AUFS_BR_RAMFS
24055+ bool "Ramfs (initramfs/rootfs) as an aufs branch"
24056+ help
24057+ If you want to use ramfs as an aufs branch fs, then enable this
24058+ option. Generally tmpfs is recommended.
24059+ Aufs prohibited them to be a branch fs by default, because
24060+ initramfs becomes unusable after switch_root or something
24061+ generally. If you sets initramfs as an aufs branch and boot your
24062+ system by switch_root, you will meet a problem easily since the
24063+ files in initramfs may be inaccessible.
24064+ Unless you are going to use ramfs as an aufs branch fs without
24065+ switch_root or something, leave it N.
1facf9fc 24066+
4a4d8108
AM
24067+config AUFS_BR_FUSE
24068+ bool "Fuse fs as an aufs branch"
24069+ depends on FUSE_FS
24070+ select AUFS_POLL
24071+ help
24072+ If you want to use fuse-based userspace filesystem as an aufs
24073+ branch fs, then enable this option.
24074+ It implements the internal poll(2) operation which is
24075+ implemented by fuse only (curretnly).
1facf9fc 24076+
4a4d8108
AM
24077+config AUFS_POLL
24078+ bool
24079+ help
24080+ Automatic configuration for internal use.
1facf9fc 24081+
4a4d8108
AM
24082+config AUFS_BR_HFSPLUS
24083+ bool "Hfsplus as an aufs branch"
24084+ depends on HFSPLUS_FS
24085+ default y
24086+ help
24087+ If you want to use hfsplus fs as an aufs branch fs, then enable
24088+ this option. This option introduces a small overhead at
24089+ copying-up a file on hfsplus.
1facf9fc 24090+
4a4d8108
AM
24091+config AUFS_BDEV_LOOP
24092+ bool
24093+ depends on BLK_DEV_LOOP
24094+ default y
24095+ help
24096+ Automatic configuration for internal use.
24097+ Convert =[ym] into =y.
1308ab2a 24098+
4a4d8108
AM
24099+config AUFS_DEBUG
24100+ bool "Debug aufs"
24101+ help
24102+ Enable this to compile aufs internal debug code.
24103+ It will have a negative impact to the performance.
24104+
24105+config AUFS_MAGIC_SYSRQ
24106+ bool
24107+ depends on AUFS_DEBUG && MAGIC_SYSRQ
24108+ default y
24109+ help
24110+ Automatic configuration for internal use.
24111+ When aufs supports Magic SysRq, enabled automatically.
24112+endif
acd2b654
AM
24113diff -urN /usr/share/empty/fs/aufs/lcnt.h linux/fs/aufs/lcnt.h
24114--- /usr/share/empty/fs/aufs/lcnt.h 1970-01-01 01:00:00.000000000 +0100
0b487229 24115+++ linux/fs/aufs/lcnt.h 2021-02-22 23:30:37.702680911 +0100
acd2b654
AM
24116@@ -0,0 +1,186 @@
24117+/* SPDX-License-Identifier: GPL-2.0 */
24118+/*
0b487229 24119+ * Copyright (C) 2018-2020 Junjiro R. Okajima
acd2b654
AM
24120+ *
24121+ * This program, aufs is free software; you can redistribute it and/or modify
24122+ * it under the terms of the GNU General Public License as published by
24123+ * the Free Software Foundation; either version 2 of the License, or
24124+ * (at your option) any later version.
24125+ *
24126+ * This program is distributed in the hope that it will be useful,
24127+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24128+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24129+ * GNU General Public License for more details.
24130+ *
24131+ * You should have received a copy of the GNU General Public License
24132+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
24133+ */
24134+
24135+/*
24136+ * simple long counter wrapper
24137+ */
24138+
24139+#ifndef __AUFS_LCNT_H__
24140+#define __AUFS_LCNT_H__
24141+
24142+#ifdef __KERNEL__
24143+
24144+#include "debug.h"
24145+
24146+#define AuLCntATOMIC 1
24147+#define AuLCntPCPUCNT 2
24148+/*
24149+ * why does percpu_refcount require extra synchronize_rcu()s in
24150+ * au_br_do_free()
24151+ */
24152+#define AuLCntPCPUREF 3
24153+
24154+/* #define AuLCntChosen AuLCntATOMIC */
24155+#define AuLCntChosen AuLCntPCPUCNT
24156+/* #define AuLCntChosen AuLCntPCPUREF */
24157+
24158+#if AuLCntChosen == AuLCntATOMIC
24159+#include <linux/atomic.h>
24160+
24161+typedef atomic_long_t au_lcnt_t;
24162+
24163+static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
24164+{
24165+ atomic_long_set(cnt, 0);
24166+ return 0;
24167+}
24168+
24169+static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24170+{
24171+ /* empty */
24172+}
24173+
24174+static inline void au_lcnt_fin(au_lcnt_t *cnt __maybe_unused,
24175+ int do_sync __maybe_unused)
24176+{
24177+ /* empty */
24178+}
24179+
24180+static inline void au_lcnt_inc(au_lcnt_t *cnt)
24181+{
24182+ atomic_long_inc(cnt);
24183+}
24184+
24185+static inline void au_lcnt_dec(au_lcnt_t *cnt)
24186+{
24187+ atomic_long_dec(cnt);
24188+}
24189+
24190+static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
24191+{
24192+ return atomic_long_read(cnt);
24193+}
24194+#endif
24195+
24196+#if AuLCntChosen == AuLCntPCPUCNT
24197+#include <linux/percpu_counter.h>
24198+
24199+typedef struct percpu_counter au_lcnt_t;
24200+
24201+static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
24202+{
24203+ return percpu_counter_init(cnt, 0, GFP_NOFS);
24204+}
24205+
24206+static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24207+{
24208+ /* empty */
24209+}
24210+
24211+static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync __maybe_unused)
24212+{
24213+ percpu_counter_destroy(cnt);
24214+}
24215+
24216+static inline void au_lcnt_inc(au_lcnt_t *cnt)
24217+{
24218+ percpu_counter_inc(cnt);
24219+}
24220+
24221+static inline void au_lcnt_dec(au_lcnt_t *cnt)
24222+{
24223+ percpu_counter_dec(cnt);
24224+}
24225+
24226+static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
24227+{
24228+ s64 n;
24229+
24230+ n = percpu_counter_sum(cnt);
24231+ BUG_ON(n < 0);
24232+ if (LONG_MAX != LLONG_MAX
24233+ && n > LONG_MAX)
24234+ AuWarn1("%s\n", "wrap-around");
24235+
24236+ return n;
24237+}
24238+#endif
24239+
24240+#if AuLCntChosen == AuLCntPCPUREF
24241+#include <linux/percpu-refcount.h>
24242+
24243+typedef struct percpu_ref au_lcnt_t;
24244+
24245+static inline int au_lcnt_init(au_lcnt_t *cnt, percpu_ref_func_t *release)
24246+{
24247+ if (!release)
24248+ release = percpu_ref_exit;
24249+ return percpu_ref_init(cnt, release, /*percpu mode*/0, GFP_NOFS);
24250+}
24251+
24252+static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24253+{
24254+ synchronize_rcu();
24255+}
24256+
24257+static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync)
24258+{
24259+ percpu_ref_kill(cnt);
24260+ if (do_sync)
24261+ au_lcnt_wait_for_fin(cnt);
24262+}
24263+
24264+static inline void au_lcnt_inc(au_lcnt_t *cnt)
24265+{
24266+ percpu_ref_get(cnt);
24267+}
24268+
24269+static inline void au_lcnt_dec(au_lcnt_t *cnt)
24270+{
24271+ percpu_ref_put(cnt);
24272+}
24273+
24274+/*
24275+ * avoid calling this func as possible.
24276+ */
24277+static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev)
24278+{
24279+ long l;
24280+
24281+ percpu_ref_switch_to_atomic_sync(cnt);
24282+ l = atomic_long_read(&cnt->count);
24283+ if (do_rev)
24284+ percpu_ref_switch_to_percpu(cnt);
24285+
24286+ /* percpu_ref is initialized by 1 instead of 0 */
24287+ return l - 1;
24288+}
24289+#endif
24290+
24291+#ifdef CONFIG_AUFS_DEBUG
24292+#define AuLCntZero(val) do { \
24293+ long l = val; \
24294+ if (l) \
24295+ AuDbg("%s = %ld\n", #val, l); \
24296+} while (0)
24297+#else
24298+#define AuLCntZero(val) do {} while (0)
24299+#endif
24300+
24301+#endif /* __KERNEL__ */
24302+#endif /* __AUFS_LCNT_H__ */
7f207e10
AM
24303diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
24304--- /usr/share/empty/fs/aufs/loop.c 1970-01-01 01:00:00.000000000 +0100
0b487229 24305+++ linux/fs/aufs/loop.c 2021-02-22 23:30:37.702680911 +0100
062440b3 24306@@ -0,0 +1,148 @@
cd7a4cd9 24307+// SPDX-License-Identifier: GPL-2.0
1facf9fc 24308+/*
0b487229 24309+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 24310+ *
24311+ * This program, aufs is free software; you can redistribute it and/or modify
24312+ * it under the terms of the GNU General Public License as published by
24313+ * the Free Software Foundation; either version 2 of the License, or
24314+ * (at your option) any later version.
dece6358
AM
24315+ *
24316+ * This program is distributed in the hope that it will be useful,
24317+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24318+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24319+ * GNU General Public License for more details.
24320+ *
24321+ * You should have received a copy of the GNU General Public License
523b37e3 24322+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24323+ */
24324+
24325+/*
24326+ * support for loopback block device as a branch
24327+ */
24328+
1facf9fc 24329+#include "aufs.h"
24330+
392086de
AM
24331+/* added into drivers/block/loop.c */
24332+static struct file *(*backing_file_func)(struct super_block *sb);
24333+
1facf9fc 24334+/*
24335+ * test if two lower dentries have overlapping branches.
24336+ */
b752ccd1 24337+int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
1facf9fc 24338+{
b752ccd1 24339+ struct super_block *h_sb;
392086de
AM
24340+ struct file *backing_file;
24341+
24342+ if (unlikely(!backing_file_func)) {
24343+ /* don't load "loop" module here */
24344+ backing_file_func = symbol_get(loop_backing_file);
24345+ if (unlikely(!backing_file_func))
24346+ /* "loop" module is not loaded */
24347+ return 0;
24348+ }
1facf9fc 24349+
b752ccd1 24350+ h_sb = h_adding->d_sb;
392086de
AM
24351+ backing_file = backing_file_func(h_sb);
24352+ if (!backing_file)
1facf9fc 24353+ return 0;
24354+
2000de60 24355+ h_adding = backing_file->f_path.dentry;
b752ccd1
AM
24356+ /*
24357+ * h_adding can be local NFS.
24358+ * in this case aufs cannot detect the loop.
24359+ */
24360+ if (unlikely(h_adding->d_sb == sb))
1facf9fc 24361+ return 1;
b752ccd1 24362+ return !!au_test_subdir(h_adding, sb->s_root);
1facf9fc 24363+}
24364+
24365+/* true if a kernel thread named 'loop[0-9].*' accesses a file */
24366+int au_test_loopback_kthread(void)
24367+{
b752ccd1
AM
24368+ int ret;
24369+ struct task_struct *tsk = current;
a2a7ad62 24370+ char c, comm[sizeof(tsk->comm)];
b752ccd1
AM
24371+
24372+ ret = 0;
24373+ if (tsk->flags & PF_KTHREAD) {
a2a7ad62
AM
24374+ get_task_comm(comm, tsk);
24375+ c = comm[4];
b752ccd1 24376+ ret = ('0' <= c && c <= '9'
a2a7ad62 24377+ && !strncmp(comm, "loop", 4));
b752ccd1 24378+ }
1facf9fc 24379+
b752ccd1 24380+ return ret;
1facf9fc 24381+}
87a755f4
AM
24382+
24383+/* ---------------------------------------------------------------------- */
24384+
24385+#define au_warn_loopback_step 16
24386+static int au_warn_loopback_nelem = au_warn_loopback_step;
24387+static unsigned long *au_warn_loopback_array;
24388+
24389+void au_warn_loopback(struct super_block *h_sb)
24390+{
24391+ int i, new_nelem;
24392+ unsigned long *a, magic;
24393+ static DEFINE_SPINLOCK(spin);
24394+
24395+ magic = h_sb->s_magic;
24396+ spin_lock(&spin);
24397+ a = au_warn_loopback_array;
24398+ for (i = 0; i < au_warn_loopback_nelem && *a; i++)
24399+ if (a[i] == magic) {
24400+ spin_unlock(&spin);
24401+ return;
24402+ }
24403+
24404+ /* h_sb is new to us, print it */
24405+ if (i < au_warn_loopback_nelem) {
24406+ a[i] = magic;
24407+ goto pr;
24408+ }
24409+
24410+ /* expand the array */
24411+ new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
24412+ a = au_kzrealloc(au_warn_loopback_array,
24413+ au_warn_loopback_nelem * sizeof(unsigned long),
e2f27e51
AM
24414+ new_nelem * sizeof(unsigned long), GFP_ATOMIC,
24415+ /*may_shrink*/0);
87a755f4
AM
24416+ if (a) {
24417+ au_warn_loopback_nelem = new_nelem;
24418+ au_warn_loopback_array = a;
24419+ a[i] = magic;
24420+ goto pr;
24421+ }
24422+
24423+ spin_unlock(&spin);
24424+ AuWarn1("realloc failed, ignored\n");
24425+ return;
24426+
24427+pr:
24428+ spin_unlock(&spin);
0c3ec466
AM
24429+ pr_warn("you may want to try another patch for loopback file "
24430+ "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
87a755f4
AM
24431+}
24432+
24433+int au_loopback_init(void)
24434+{
24435+ int err;
24436+ struct super_block *sb __maybe_unused;
24437+
79b8bda9 24438+ BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(unsigned long));
87a755f4
AM
24439+
24440+ err = 0;
24441+ au_warn_loopback_array = kcalloc(au_warn_loopback_step,
24442+ sizeof(unsigned long), GFP_NOFS);
24443+ if (unlikely(!au_warn_loopback_array))
24444+ err = -ENOMEM;
24445+
24446+ return err;
24447+}
24448+
24449+void au_loopback_fin(void)
24450+{
79b8bda9
AM
24451+ if (backing_file_func)
24452+ symbol_put(loop_backing_file);
e49925d1 24453+ au_kfree_try_rcu(au_warn_loopback_array);
87a755f4 24454+}
7f207e10
AM
24455diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
24456--- /usr/share/empty/fs/aufs/loop.h 1970-01-01 01:00:00.000000000 +0100
0b487229
JR
24457+++ linux/fs/aufs/loop.h 2021-02-22 23:30:37.702680911 +0100
24458@@ -0,0 +1,55 @@
062440b3 24459+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 24460+/*
0b487229 24461+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 24462+ *
24463+ * This program, aufs is free software; you can redistribute it and/or modify
24464+ * it under the terms of the GNU General Public License as published by
24465+ * the Free Software Foundation; either version 2 of the License, or
24466+ * (at your option) any later version.
dece6358
AM
24467+ *
24468+ * This program is distributed in the hope that it will be useful,
24469+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24470+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24471+ * GNU General Public License for more details.
24472+ *
24473+ * You should have received a copy of the GNU General Public License
523b37e3 24474+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24475+ */
24476+
24477+/*
24478+ * support for loopback mount as a branch
24479+ */
24480+
24481+#ifndef __AUFS_LOOP_H__
24482+#define __AUFS_LOOP_H__
24483+
24484+#ifdef __KERNEL__
24485+
dece6358
AM
24486+struct dentry;
24487+struct super_block;
1facf9fc 24488+
24489+#ifdef CONFIG_AUFS_BDEV_LOOP
392086de
AM
24490+/* drivers/block/loop.c */
24491+struct file *loop_backing_file(struct super_block *sb);
24492+
1facf9fc 24493+/* loop.c */
b752ccd1 24494+int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
1facf9fc 24495+int au_test_loopback_kthread(void);
87a755f4
AM
24496+void au_warn_loopback(struct super_block *h_sb);
24497+
24498+int au_loopback_init(void);
24499+void au_loopback_fin(void);
1facf9fc 24500+#else
0b487229
JR
24501+AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
24502+
4a4d8108 24503+AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
b752ccd1 24504+ struct dentry *h_adding)
4a4d8108 24505+AuStubInt0(au_test_loopback_kthread, void)
87a755f4
AM
24506+AuStubVoid(au_warn_loopback, struct super_block *h_sb)
24507+
24508+AuStubInt0(au_loopback_init, void)
24509+AuStubVoid(au_loopback_fin, void)
1facf9fc 24510+#endif /* BLK_DEV_LOOP */
24511+
24512+#endif /* __KERNEL__ */
24513+#endif /* __AUFS_LOOP_H__ */
7f207e10
AM
24514diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
24515--- /usr/share/empty/fs/aufs/magic.mk 1970-01-01 01:00:00.000000000 +0100
0b487229 24516+++ linux/fs/aufs/magic.mk 2021-02-22 23:30:37.702680911 +0100
2121bcd9
AM
24517@@ -0,0 +1,31 @@
24518+# SPDX-License-Identifier: GPL-2.0
1facf9fc 24519+
24520+# defined in ${srctree}/fs/fuse/inode.c
24521+# tristate
24522+ifdef CONFIG_FUSE_FS
24523+ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
24524+endif
24525+
1facf9fc 24526+# defined in ${srctree}/fs/xfs/xfs_sb.h
24527+# tristate
24528+ifdef CONFIG_XFS_FS
24529+ccflags-y += -DXFS_SB_MAGIC=0x58465342
24530+endif
24531+
24532+# defined in ${srctree}/fs/configfs/mount.c
24533+# tristate
24534+ifdef CONFIG_CONFIGFS_FS
24535+ccflags-y += -DCONFIGFS_MAGIC=0x62656570
24536+endif
24537+
1facf9fc 24538+# defined in ${srctree}/fs/ubifs/ubifs.h
24539+# tristate
24540+ifdef CONFIG_UBIFS_FS
24541+ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
24542+endif
4a4d8108
AM
24543+
24544+# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
24545+# tristate
24546+ifdef CONFIG_HFSPLUS_FS
24547+ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
24548+endif
7f207e10
AM
24549diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
24550--- /usr/share/empty/fs/aufs/Makefile 1970-01-01 01:00:00.000000000 +0100
0b487229 24551+++ linux/fs/aufs/Makefile 2021-02-22 23:30:37.699347580 +0100
2121bcd9
AM
24552@@ -0,0 +1,46 @@
24553+# SPDX-License-Identifier: GPL-2.0
4a4d8108
AM
24554+
24555+include ${src}/magic.mk
24556+ifeq (${CONFIG_AUFS_FS},m)
24557+include ${src}/conf.mk
24558+endif
24559+-include ${src}/priv_def.mk
24560+
24561+# cf. include/linux/kernel.h
24562+# enable pr_debug
24563+ccflags-y += -DDEBUG
f6c5ef8b
AM
24564+# sparse requires the full pathname
24565+ifdef M
523b37e3 24566+ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
f6c5ef8b 24567+else
523b37e3 24568+ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
f6c5ef8b 24569+endif
4a4d8108
AM
24570+
24571+obj-$(CONFIG_AUFS_FS) += aufs.o
24572+aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
24573+ wkq.o vfsub.o dcsub.o \
e49829fe 24574+ cpup.o whout.o wbr_policy.o \
4a4d8108
AM
24575+ dinfo.o dentry.o \
24576+ dynop.o \
24577+ finfo.o file.o f_op.o \
24578+ dir.o vdir.o \
24579+ iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
c2b27bf2 24580+ mvdown.o ioctl.o
4a4d8108
AM
24581+
24582+# all are boolean
e49829fe 24583+aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
4a4d8108
AM
24584+aufs-$(CONFIG_SYSFS) += sysfs.o
24585+aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
24586+aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
24587+aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
24588+aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
4a4d8108 24589+aufs-$(CONFIG_AUFS_EXPORT) += export.o
c1595e42
JR
24590+aufs-$(CONFIG_AUFS_XATTR) += xattr.o
24591+aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
8b6a4947 24592+aufs-$(CONFIG_AUFS_DIRREN) += dirren.o
076b876e 24593+aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
4a4d8108
AM
24594+aufs-$(CONFIG_AUFS_POLL) += poll.o
24595+aufs-$(CONFIG_AUFS_RDU) += rdu.o
4a4d8108
AM
24596+aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
24597+aufs-$(CONFIG_AUFS_DEBUG) += debug.o
24598+aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
7f207e10
AM
24599diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
24600--- /usr/share/empty/fs/aufs/module.c 1970-01-01 01:00:00.000000000 +0100
0b487229 24601+++ linux/fs/aufs/module.c 2021-02-22 23:30:37.702680911 +0100
062440b3 24602@@ -0,0 +1,273 @@
cd7a4cd9 24603+// SPDX-License-Identifier: GPL-2.0
1facf9fc 24604+/*
0b487229 24605+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 24606+ *
24607+ * This program, aufs is free software; you can redistribute it and/or modify
24608+ * it under the terms of the GNU General Public License as published by
24609+ * the Free Software Foundation; either version 2 of the License, or
24610+ * (at your option) any later version.
dece6358
AM
24611+ *
24612+ * This program is distributed in the hope that it will be useful,
24613+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24614+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24615+ * GNU General Public License for more details.
24616+ *
24617+ * You should have received a copy of the GNU General Public License
523b37e3 24618+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24619+ */
24620+
24621+/*
24622+ * module global variables and operations
24623+ */
24624+
24625+#include <linux/module.h>
24626+#include <linux/seq_file.h>
24627+#include "aufs.h"
24628+
e2f27e51
AM
24629+/* shrinkable realloc */
24630+void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
1facf9fc 24631+{
e2f27e51
AM
24632+ size_t sz;
24633+ int diff;
1facf9fc 24634+
e2f27e51
AM
24635+ sz = 0;
24636+ diff = -1;
24637+ if (p) {
24638+#if 0 /* unused */
24639+ if (!new_sz) {
e49925d1 24640+ au_kfree_rcu(p);
e2f27e51
AM
24641+ p = NULL;
24642+ goto out;
24643+ }
24644+#else
24645+ AuDebugOn(!new_sz);
24646+#endif
24647+ sz = ksize(p);
24648+ diff = au_kmidx_sub(sz, new_sz);
24649+ }
24650+ if (sz && !diff)
24651+ goto out;
24652+
24653+ if (sz < new_sz)
24654+ /* expand or SLOB */
24655+ p = krealloc(p, new_sz, gfp);
24656+ else if (new_sz < sz && may_shrink) {
24657+ /* shrink */
24658+ void *q;
24659+
24660+ q = kmalloc(new_sz, gfp);
24661+ if (q) {
24662+ if (p) {
24663+ memcpy(q, p, new_sz);
e49925d1 24664+ au_kfree_try_rcu(p);
e2f27e51
AM
24665+ }
24666+ p = q;
24667+ } else
24668+ p = NULL;
24669+ }
24670+
24671+out:
24672+ return p;
24673+}
24674+
24675+void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24676+ int may_shrink)
24677+{
24678+ p = au_krealloc(p, new_sz, gfp, may_shrink);
24679+ if (p && new_sz > nused)
1facf9fc 24680+ memset(p + nused, 0, new_sz - nused);
24681+ return p;
24682+}
24683+
24684+/* ---------------------------------------------------------------------- */
1facf9fc 24685+/*
24686+ * aufs caches
24687+ */
1c60b727 24688+struct kmem_cache *au_cache[AuCache_Last];
5afbbe0d
AM
24689+
24690+static void au_cache_fin(void)
24691+{
24692+ int i;
24693+
24694+ /*
24695+ * Make sure all delayed rcu free inodes are flushed before we
24696+ * destroy cache.
24697+ */
24698+ rcu_barrier();
24699+
24700+ /* excluding AuCache_HNOTIFY */
24701+ BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
24702+ for (i = 0; i < AuCache_HNOTIFY; i++) {
1c60b727
AM
24703+ kmem_cache_destroy(au_cache[i]);
24704+ au_cache[i] = NULL;
5afbbe0d
AM
24705+ }
24706+}
24707+
1facf9fc 24708+static int __init au_cache_init(void)
24709+{
1c60b727
AM
24710+ au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
24711+ if (au_cache[AuCache_DINFO])
027c5e7a 24712+ /* SLAB_DESTROY_BY_RCU */
1c60b727 24713+ au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
f0c0a007 24714+ au_icntnr_init_once);
1c60b727
AM
24715+ if (au_cache[AuCache_ICNTNR])
24716+ au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
f0c0a007 24717+ au_fi_init_once);
1c60b727
AM
24718+ if (au_cache[AuCache_FINFO])
24719+ au_cache[AuCache_VDIR] = AuCache(au_vdir);
24720+ if (au_cache[AuCache_VDIR])
24721+ au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
24722+ if (au_cache[AuCache_DEHSTR])
1facf9fc 24723+ return 0;
24724+
5afbbe0d 24725+ au_cache_fin();
1facf9fc 24726+ return -ENOMEM;
24727+}
24728+
1facf9fc 24729+/* ---------------------------------------------------------------------- */
24730+
24731+int au_dir_roflags;
24732+
e49829fe 24733+#ifdef CONFIG_AUFS_SBILIST
1e00d052
AM
24734+/*
24735+ * iterate_supers_type() doesn't protect us from
24736+ * remounting (branch management)
24737+ */
8b6a4947 24738+struct hlist_bl_head au_sbilist;
e49829fe
JR
24739+#endif
24740+
1facf9fc 24741+/*
24742+ * functions for module interface.
24743+ */
24744+MODULE_LICENSE("GPL");
24745+/* MODULE_LICENSE("GPL v2"); */
dece6358 24746+MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
1facf9fc 24747+MODULE_DESCRIPTION(AUFS_NAME
24748+ " -- Advanced multi layered unification filesystem");
24749+MODULE_VERSION(AUFS_VERSION);
c06a8ce3 24750+MODULE_ALIAS_FS(AUFS_NAME);
1facf9fc 24751+
1facf9fc 24752+/* this module parameter has no meaning when SYSFS is disabled */
24753+int sysaufs_brs = 1;
24754+MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
cd7a4cd9 24755+module_param_named(brs, sysaufs_brs, int, 0444);
1facf9fc 24756+
076b876e 24757+/* this module parameter has no meaning when USER_NS is disabled */
8cdd5066 24758+bool au_userns;
076b876e 24759+MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
cd7a4cd9 24760+module_param_named(allow_userns, au_userns, bool, 0444);
076b876e 24761+
1facf9fc 24762+/* ---------------------------------------------------------------------- */
24763+
24764+static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
24765+
24766+int au_seq_path(struct seq_file *seq, struct path *path)
24767+{
79b8bda9
AM
24768+ int err;
24769+
24770+ err = seq_path(seq, path, au_esc_chars);
1c60b727 24771+ if (err >= 0)
79b8bda9 24772+ err = 0;
1c60b727 24773+ else
79b8bda9
AM
24774+ err = -ENOMEM;
24775+
24776+ return err;
1facf9fc 24777+}
24778+
24779+/* ---------------------------------------------------------------------- */
24780+
24781+static int __init aufs_init(void)
24782+{
24783+ int err, i;
24784+ char *p;
24785+
24786+ p = au_esc_chars;
24787+ for (i = 1; i <= ' '; i++)
24788+ *p++ = i;
24789+ *p++ = '\\';
24790+ *p++ = '\x7f';
24791+ *p = 0;
24792+
24793+ au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
24794+
b95c5147
AM
24795+ memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
24796+ for (i = 0; i < AuIop_Last; i++)
24797+ aufs_iop_nogetattr[i].getattr = NULL;
24798+
1c60b727 24799+ memset(au_cache, 0, sizeof(au_cache)); /* including hnotify */
f0c0a007 24800+
e49829fe 24801+ au_sbilist_init();
1facf9fc 24802+ sysaufs_brs_init();
24803+ au_debug_init();
4a4d8108 24804+ au_dy_init();
1facf9fc 24805+ err = sysaufs_init();
24806+ if (unlikely(err))
24807+ goto out;
062440b3 24808+ err = dbgaufs_init();
4f0767ce 24809+ if (unlikely(err))
953406b4 24810+ goto out_sysaufs;
062440b3
AM
24811+ err = au_procfs_init();
24812+ if (unlikely(err))
24813+ goto out_dbgaufs;
e49829fe
JR
24814+ err = au_wkq_init();
24815+ if (unlikely(err))
24816+ goto out_procfs;
87a755f4 24817+ err = au_loopback_init();
1facf9fc 24818+ if (unlikely(err))
24819+ goto out_wkq;
87a755f4
AM
24820+ err = au_hnotify_init();
24821+ if (unlikely(err))
24822+ goto out_loopback;
1facf9fc 24823+ err = au_sysrq_init();
24824+ if (unlikely(err))
24825+ goto out_hin;
24826+ err = au_cache_init();
24827+ if (unlikely(err))
24828+ goto out_sysrq;
076b876e
AM
24829+
24830+ aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
1facf9fc 24831+ err = register_filesystem(&aufs_fs_type);
24832+ if (unlikely(err))
24833+ goto out_cache;
076b876e 24834+
4a4d8108
AM
24835+ /* since we define pr_fmt, call printk directly */
24836+ printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
1facf9fc 24837+ goto out; /* success */
24838+
4f0767ce 24839+out_cache:
1facf9fc 24840+ au_cache_fin();
4f0767ce 24841+out_sysrq:
1facf9fc 24842+ au_sysrq_fin();
4f0767ce 24843+out_hin:
4a4d8108 24844+ au_hnotify_fin();
87a755f4
AM
24845+out_loopback:
24846+ au_loopback_fin();
4f0767ce 24847+out_wkq:
1facf9fc 24848+ au_wkq_fin();
e49829fe
JR
24849+out_procfs:
24850+ au_procfs_fin();
062440b3
AM
24851+out_dbgaufs:
24852+ dbgaufs_fin();
4f0767ce 24853+out_sysaufs:
1facf9fc 24854+ sysaufs_fin();
4a4d8108 24855+ au_dy_fin();
4f0767ce 24856+out:
1facf9fc 24857+ return err;
24858+}
24859+
24860+static void __exit aufs_exit(void)
24861+{
24862+ unregister_filesystem(&aufs_fs_type);
24863+ au_cache_fin();
24864+ au_sysrq_fin();
4a4d8108 24865+ au_hnotify_fin();
87a755f4 24866+ au_loopback_fin();
1facf9fc 24867+ au_wkq_fin();
e49829fe 24868+ au_procfs_fin();
062440b3 24869+ dbgaufs_fin();
1facf9fc 24870+ sysaufs_fin();
4a4d8108 24871+ au_dy_fin();
1facf9fc 24872+}
24873+
24874+module_init(aufs_init);
24875+module_exit(aufs_exit);
7f207e10
AM
24876diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
24877--- /usr/share/empty/fs/aufs/module.h 1970-01-01 01:00:00.000000000 +0100
0b487229 24878+++ linux/fs/aufs/module.h 2021-02-22 23:30:37.702680911 +0100
e49925d1 24879@@ -0,0 +1,166 @@
062440b3 24880+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 24881+/*
0b487229 24882+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 24883+ *
24884+ * This program, aufs is free software; you can redistribute it and/or modify
24885+ * it under the terms of the GNU General Public License as published by
24886+ * the Free Software Foundation; either version 2 of the License, or
24887+ * (at your option) any later version.
dece6358
AM
24888+ *
24889+ * This program is distributed in the hope that it will be useful,
24890+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24891+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24892+ * GNU General Public License for more details.
24893+ *
24894+ * You should have received a copy of the GNU General Public License
523b37e3 24895+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24896+ */
24897+
24898+/*
24899+ * module initialization and module-global
24900+ */
24901+
24902+#ifndef __AUFS_MODULE_H__
24903+#define __AUFS_MODULE_H__
24904+
24905+#ifdef __KERNEL__
24906+
24907+#include <linux/slab.h>
e49925d1
JR
24908+#include "debug.h"
24909+#include "dentry.h"
24910+#include "dir.h"
24911+#include "file.h"
24912+#include "inode.h"
1facf9fc 24913+
dece6358
AM
24914+struct path;
24915+struct seq_file;
24916+
1facf9fc 24917+/* module parameters */
1facf9fc 24918+extern int sysaufs_brs;
8cdd5066 24919+extern bool au_userns;
1facf9fc 24920+
24921+/* ---------------------------------------------------------------------- */
24922+
24923+extern int au_dir_roflags;
24924+
e2f27e51
AM
24925+void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
24926+void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24927+ int may_shrink);
24928+
e49925d1
JR
24929+/*
24930+ * Comparing the size of the object with sizeof(struct rcu_head)
24931+ * case 1: object is always larger
24932+ * --> au_kfree_rcu() or au_kfree_do_rcu()
24933+ * case 2: object is always smaller
24934+ * --> au_kfree_small()
24935+ * case 3: object can be any size
24936+ * --> au_kfree_try_rcu()
24937+ */
24938+
24939+static inline void au_kfree_do_rcu(const void *p)
24940+{
24941+ struct {
24942+ struct rcu_head rcu;
24943+ } *a = (void *)p;
24944+
24945+ kfree_rcu(a, rcu);
24946+}
24947+
24948+#define au_kfree_rcu(_p) do { \
24949+ typeof(_p) p = (_p); \
24950+ BUILD_BUG_ON(sizeof(*p) < sizeof(struct rcu_head)); \
24951+ if (p) \
24952+ au_kfree_do_rcu(p); \
24953+ } while (0)
24954+
24955+#define au_kfree_do_sz_test(sz) (sz >= sizeof(struct rcu_head))
24956+#define au_kfree_sz_test(p) (p && au_kfree_do_sz_test(ksize(p)))
24957+
24958+static inline void au_kfree_try_rcu(const void *p)
24959+{
24960+ if (!p)
24961+ return;
24962+ if (au_kfree_sz_test(p))
24963+ au_kfree_do_rcu(p);
24964+ else
24965+ kfree(p);
24966+}
24967+
24968+static inline void au_kfree_small(const void *p)
24969+{
24970+ if (!p)
24971+ return;
24972+ AuDebugOn(au_kfree_sz_test(p));
24973+ kfree(p);
24974+}
24975+
e2f27e51
AM
24976+static inline int au_kmidx_sub(size_t sz, size_t new_sz)
24977+{
24978+#ifndef CONFIG_SLOB
24979+ return kmalloc_index(sz) - kmalloc_index(new_sz);
24980+#else
24981+ return -1; /* SLOB is untested */
24982+#endif
24983+}
24984+
1facf9fc 24985+int au_seq_path(struct seq_file *seq, struct path *path);
24986+
e49829fe
JR
24987+#ifdef CONFIG_PROC_FS
24988+/* procfs.c */
24989+int __init au_procfs_init(void);
24990+void au_procfs_fin(void);
24991+#else
24992+AuStubInt0(au_procfs_init, void);
24993+AuStubVoid(au_procfs_fin, void);
24994+#endif
24995+
4f0767ce
JR
24996+/* ---------------------------------------------------------------------- */
24997+
1c60b727 24998+/* kmem cache */
1facf9fc 24999+enum {
25000+ AuCache_DINFO,
25001+ AuCache_ICNTNR,
25002+ AuCache_FINFO,
25003+ AuCache_VDIR,
25004+ AuCache_DEHSTR,
7eafdf33 25005+ AuCache_HNOTIFY, /* must be last */
1facf9fc 25006+ AuCache_Last
25007+};
25008+
1c60b727 25009+extern struct kmem_cache *au_cache[AuCache_Last];
f0c0a007 25010+
4a4d8108
AM
25011+#define AuCacheFlags (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
25012+#define AuCache(type) KMEM_CACHE(type, AuCacheFlags)
25013+#define AuCacheCtor(type, ctor) \
25014+ kmem_cache_create(#type, sizeof(struct type), \
25015+ __alignof__(struct type), AuCacheFlags, ctor)
1facf9fc 25016+
e49925d1
JR
25017+#define AuCacheFuncs(name, index) \
25018+ static inline struct au_##name *au_cache_alloc_##name(void) \
25019+ { return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); } \
25020+ static inline void au_cache_free_##name##_norcu(struct au_##name *p) \
25021+ { kmem_cache_free(au_cache[AuCache_##index], p); } \
25022+ \
25023+ static inline void au_cache_free_##name##_rcu_cb(struct rcu_head *rcu) \
25024+ { void *p = rcu; \
25025+ p -= offsetof(struct au_##name, rcu); \
25026+ kmem_cache_free(au_cache[AuCache_##index], p); } \
25027+ static inline void au_cache_free_##name##_rcu(struct au_##name *p) \
25028+ { BUILD_BUG_ON(sizeof(struct au_##name) < sizeof(struct rcu_head)); \
25029+ call_rcu(&p->rcu, au_cache_free_##name##_rcu_cb); } \
25030+ \
25031+ static inline void au_cache_free_##name(struct au_##name *p) \
25032+ { /* au_cache_free_##name##_norcu(p); */ \
25033+ au_cache_free_##name##_rcu(p); }
1facf9fc 25034+
25035+AuCacheFuncs(dinfo, DINFO);
25036+AuCacheFuncs(icntnr, ICNTNR);
25037+AuCacheFuncs(finfo, FINFO);
25038+AuCacheFuncs(vdir, VDIR);
4a4d8108
AM
25039+AuCacheFuncs(vdir_dehstr, DEHSTR);
25040+#ifdef CONFIG_AUFS_HNOTIFY
25041+AuCacheFuncs(hnotify, HNOTIFY);
25042+#endif
1facf9fc 25043+
4a4d8108
AM
25044+#endif /* __KERNEL__ */
25045+#endif /* __AUFS_MODULE_H__ */
c2b27bf2
AM
25046diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
25047--- /usr/share/empty/fs/aufs/mvdown.c 1970-01-01 01:00:00.000000000 +0100
0b487229 25048+++ linux/fs/aufs/mvdown.c 2021-02-22 23:30:37.702680911 +0100
062440b3 25049@@ -0,0 +1,705 @@
cd7a4cd9 25050+// SPDX-License-Identifier: GPL-2.0
c2b27bf2 25051+/*
0b487229 25052+ * Copyright (C) 2011-2020 Junjiro R. Okajima
c2b27bf2
AM
25053+ *
25054+ * This program, aufs is free software; you can redistribute it and/or modify
25055+ * it under the terms of the GNU General Public License as published by
25056+ * the Free Software Foundation; either version 2 of the License, or
25057+ * (at your option) any later version.
25058+ *
25059+ * This program is distributed in the hope that it will be useful,
25060+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
25061+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25062+ * GNU General Public License for more details.
25063+ *
25064+ * You should have received a copy of the GNU General Public License
523b37e3
AM
25065+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
25066+ */
25067+
25068+/*
25069+ * move-down, opposite of copy-up
c2b27bf2
AM
25070+ */
25071+
25072+#include "aufs.h"
25073+
c2b27bf2
AM
25074+struct au_mvd_args {
25075+ struct {
c2b27bf2
AM
25076+ struct super_block *h_sb;
25077+ struct dentry *h_parent;
25078+ struct au_hinode *hdir;
392086de 25079+ struct inode *h_dir, *h_inode;
c1595e42 25080+ struct au_pin pin;
c2b27bf2
AM
25081+ } info[AUFS_MVDOWN_NARRAY];
25082+
25083+ struct aufs_mvdown mvdown;
25084+ struct dentry *dentry, *parent;
25085+ struct inode *inode, *dir;
25086+ struct super_block *sb;
25087+ aufs_bindex_t bopq, bwh, bfound;
25088+ unsigned char rename_lock;
c2b27bf2
AM
25089+};
25090+
392086de 25091+#define mvd_errno mvdown.au_errno
076b876e
AM
25092+#define mvd_bsrc mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
25093+#define mvd_src_brid mvdown.stbr[AUFS_MVDOWN_UPPER].brid
25094+#define mvd_bdst mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
25095+#define mvd_dst_brid mvdown.stbr[AUFS_MVDOWN_LOWER].brid
c2b27bf2 25096+
392086de
AM
25097+#define mvd_h_src_sb info[AUFS_MVDOWN_UPPER].h_sb
25098+#define mvd_h_src_parent info[AUFS_MVDOWN_UPPER].h_parent
25099+#define mvd_hdir_src info[AUFS_MVDOWN_UPPER].hdir
25100+#define mvd_h_src_dir info[AUFS_MVDOWN_UPPER].h_dir
25101+#define mvd_h_src_inode info[AUFS_MVDOWN_UPPER].h_inode
c1595e42 25102+#define mvd_pin_src info[AUFS_MVDOWN_UPPER].pin
392086de
AM
25103+
25104+#define mvd_h_dst_sb info[AUFS_MVDOWN_LOWER].h_sb
25105+#define mvd_h_dst_parent info[AUFS_MVDOWN_LOWER].h_parent
25106+#define mvd_hdir_dst info[AUFS_MVDOWN_LOWER].hdir
25107+#define mvd_h_dst_dir info[AUFS_MVDOWN_LOWER].h_dir
25108+#define mvd_h_dst_inode info[AUFS_MVDOWN_LOWER].h_inode
c1595e42 25109+#define mvd_pin_dst info[AUFS_MVDOWN_LOWER].pin
c2b27bf2
AM
25110+
25111+#define AU_MVD_PR(flag, ...) do { \
25112+ if (flag) \
25113+ pr_err(__VA_ARGS__); \
25114+ } while (0)
25115+
076b876e
AM
25116+static int find_lower_writable(struct au_mvd_args *a)
25117+{
25118+ struct super_block *sb;
5afbbe0d 25119+ aufs_bindex_t bindex, bbot;
076b876e
AM
25120+ struct au_branch *br;
25121+
25122+ sb = a->sb;
25123+ bindex = a->mvd_bsrc;
5afbbe0d 25124+ bbot = au_sbbot(sb);
076b876e 25125+ if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
5afbbe0d 25126+ for (bindex++; bindex <= bbot; bindex++) {
076b876e
AM
25127+ br = au_sbr(sb, bindex);
25128+ if (au_br_fhsm(br->br_perm)
8b6a4947 25129+ && !sb_rdonly(au_br_sb(br)))
076b876e
AM
25130+ return bindex;
25131+ }
25132+ else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
5afbbe0d 25133+ for (bindex++; bindex <= bbot; bindex++) {
076b876e
AM
25134+ br = au_sbr(sb, bindex);
25135+ if (!au_br_rdonly(br))
25136+ return bindex;
25137+ }
25138+ else
5afbbe0d 25139+ for (bindex++; bindex <= bbot; bindex++) {
076b876e 25140+ br = au_sbr(sb, bindex);
8b6a4947 25141+ if (!sb_rdonly(au_br_sb(br))) {
076b876e
AM
25142+ if (au_br_rdonly(br))
25143+ a->mvdown.flags
25144+ |= AUFS_MVDOWN_ROLOWER_R;
25145+ return bindex;
25146+ }
25147+ }
25148+
25149+ return -1;
25150+}
25151+
c2b27bf2 25152+/* make the parent dir on bdst */
392086de 25153+static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25154+{
25155+ int err;
25156+
25157+ err = 0;
25158+ a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
25159+ a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
25160+ a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
25161+ a->mvd_h_dst_parent = NULL;
5afbbe0d 25162+ if (au_dbbot(a->parent) >= a->mvd_bdst)
c2b27bf2
AM
25163+ a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
25164+ if (!a->mvd_h_dst_parent) {
25165+ err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
25166+ if (unlikely(err)) {
392086de 25167+ AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
c2b27bf2
AM
25168+ goto out;
25169+ }
25170+ a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
25171+ }
25172+
25173+out:
25174+ AuTraceErr(err);
25175+ return err;
25176+}
25177+
25178+/* lock them all */
392086de 25179+static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25180+{
25181+ int err;
25182+ struct dentry *h_trap;
25183+
25184+ a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
25185+ a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
c1595e42
JR
25186+ err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
25187+ au_opt_udba(a->sb),
25188+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25189+ AuTraceErr(err);
25190+ if (unlikely(err)) {
25191+ AU_MVD_PR(dmsg, "pin_dst failed\n");
25192+ goto out;
25193+ }
25194+
c2b27bf2
AM
25195+ if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
25196+ a->rename_lock = 0;
c1595e42
JR
25197+ au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
25198+ AuLsc_DI_PARENT, AuLsc_I_PARENT3,
25199+ au_opt_udba(a->sb),
25200+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25201+ err = au_do_pin(&a->mvd_pin_src);
25202+ AuTraceErr(err);
5527c038 25203+ a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
c1595e42
JR
25204+ if (unlikely(err)) {
25205+ AU_MVD_PR(dmsg, "pin_src failed\n");
25206+ goto out_dst;
25207+ }
25208+ goto out; /* success */
c2b27bf2
AM
25209+ }
25210+
c2b27bf2 25211+ a->rename_lock = 1;
c1595e42
JR
25212+ au_pin_hdir_unlock(&a->mvd_pin_dst);
25213+ err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
25214+ au_opt_udba(a->sb),
25215+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25216+ AuTraceErr(err);
5527c038 25217+ a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
c1595e42
JR
25218+ if (unlikely(err)) {
25219+ AU_MVD_PR(dmsg, "pin_src failed\n");
25220+ au_pin_hdir_lock(&a->mvd_pin_dst);
25221+ goto out_dst;
25222+ }
25223+ au_pin_hdir_unlock(&a->mvd_pin_src);
c2b27bf2
AM
25224+ h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25225+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
25226+ if (h_trap) {
25227+ err = (h_trap != a->mvd_h_src_parent);
25228+ if (err)
25229+ err = (h_trap != a->mvd_h_dst_parent);
25230+ }
25231+ BUG_ON(err); /* it should never happen */
c1595e42
JR
25232+ if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
25233+ err = -EBUSY;
25234+ AuTraceErr(err);
25235+ vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25236+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
25237+ au_pin_hdir_lock(&a->mvd_pin_src);
25238+ au_unpin(&a->mvd_pin_src);
25239+ au_pin_hdir_lock(&a->mvd_pin_dst);
25240+ goto out_dst;
25241+ }
25242+ goto out; /* success */
c2b27bf2 25243+
c1595e42
JR
25244+out_dst:
25245+ au_unpin(&a->mvd_pin_dst);
c2b27bf2
AM
25246+out:
25247+ AuTraceErr(err);
25248+ return err;
25249+}
25250+
392086de 25251+static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2 25252+{
c1595e42
JR
25253+ if (!a->rename_lock)
25254+ au_unpin(&a->mvd_pin_src);
25255+ else {
c2b27bf2
AM
25256+ vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25257+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
c1595e42
JR
25258+ au_pin_hdir_lock(&a->mvd_pin_src);
25259+ au_unpin(&a->mvd_pin_src);
25260+ au_pin_hdir_lock(&a->mvd_pin_dst);
25261+ }
25262+ au_unpin(&a->mvd_pin_dst);
c2b27bf2
AM
25263+}
25264+
25265+/* copy-down the file */
392086de 25266+static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25267+{
25268+ int err;
25269+ struct au_cp_generic cpg = {
25270+ .dentry = a->dentry,
25271+ .bdst = a->mvd_bdst,
25272+ .bsrc = a->mvd_bsrc,
25273+ .len = -1,
c1595e42 25274+ .pin = &a->mvd_pin_dst,
c2b27bf2
AM
25275+ .flags = AuCpup_DTIME | AuCpup_HOPEN
25276+ };
25277+
25278+ AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
392086de
AM
25279+ if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25280+ au_fset_cpup(cpg.flags, OVERWRITE);
25281+ if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
25282+ au_fset_cpup(cpg.flags, RWDST);
c2b27bf2
AM
25283+ err = au_sio_cpdown_simple(&cpg);
25284+ if (unlikely(err))
392086de 25285+ AU_MVD_PR(dmsg, "cpdown failed\n");
c2b27bf2
AM
25286+
25287+ AuTraceErr(err);
25288+ return err;
25289+}
25290+
25291+/*
25292+ * unlink the whiteout on bdst if exist which may be created by UDBA while we
25293+ * were sleeping
25294+ */
392086de 25295+static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25296+{
25297+ int err;
25298+ struct path h_path;
25299+ struct au_branch *br;
523b37e3 25300+ struct inode *delegated;
c2b27bf2
AM
25301+
25302+ br = au_sbr(a->sb, a->mvd_bdst);
25303+ h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
25304+ err = PTR_ERR(h_path.dentry);
25305+ if (IS_ERR(h_path.dentry)) {
392086de 25306+ AU_MVD_PR(dmsg, "wh_lkup failed\n");
c2b27bf2
AM
25307+ goto out;
25308+ }
25309+
25310+ err = 0;
5527c038 25311+ if (d_is_positive(h_path.dentry)) {
c2b27bf2 25312+ h_path.mnt = au_br_mnt(br);
523b37e3 25313+ delegated = NULL;
5527c038 25314+ err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
523b37e3
AM
25315+ &delegated, /*force*/0);
25316+ if (unlikely(err == -EWOULDBLOCK)) {
25317+ pr_warn("cannot retry for NFSv4 delegation"
25318+ " for an internal unlink\n");
25319+ iput(delegated);
25320+ }
c2b27bf2 25321+ if (unlikely(err))
392086de 25322+ AU_MVD_PR(dmsg, "wh_unlink failed\n");
c2b27bf2
AM
25323+ }
25324+ dput(h_path.dentry);
25325+
25326+out:
25327+ AuTraceErr(err);
25328+ return err;
25329+}
25330+
25331+/*
25332+ * unlink the topmost h_dentry
c2b27bf2 25333+ */
392086de 25334+static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25335+{
25336+ int err;
25337+ struct path h_path;
523b37e3 25338+ struct inode *delegated;
c2b27bf2
AM
25339+
25340+ h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
25341+ h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
523b37e3
AM
25342+ delegated = NULL;
25343+ err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
25344+ if (unlikely(err == -EWOULDBLOCK)) {
25345+ pr_warn("cannot retry for NFSv4 delegation"
25346+ " for an internal unlink\n");
25347+ iput(delegated);
25348+ }
c2b27bf2 25349+ if (unlikely(err))
392086de 25350+ AU_MVD_PR(dmsg, "unlink failed\n");
c2b27bf2
AM
25351+
25352+ AuTraceErr(err);
25353+ return err;
25354+}
25355+
076b876e
AM
25356+/* Since mvdown succeeded, we ignore an error of this function */
25357+static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
25358+{
25359+ int err;
25360+ struct au_branch *br;
25361+
25362+ a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
25363+ br = au_sbr(a->sb, a->mvd_bsrc);
25364+ err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
25365+ if (!err) {
25366+ br = au_sbr(a->sb, a->mvd_bdst);
25367+ a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
25368+ err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
25369+ }
25370+ if (!err)
25371+ a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
25372+ else
25373+ AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
25374+}
25375+
c2b27bf2
AM
25376+/*
25377+ * copy-down the file and unlink the bsrc file.
25378+ * - unlink the bdst whout if exist
25379+ * - copy-down the file (with whtmp name and rename)
25380+ * - unlink the bsrc file
25381+ */
392086de 25382+static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25383+{
25384+ int err;
25385+
392086de 25386+ err = au_do_mkdir(dmsg, a);
c2b27bf2 25387+ if (!err)
392086de 25388+ err = au_do_lock(dmsg, a);
c2b27bf2
AM
25389+ if (unlikely(err))
25390+ goto out;
25391+
25392+ /*
25393+ * do not revert the activities we made on bdst since they should be
25394+ * harmless in aufs.
25395+ */
25396+
392086de 25397+ err = au_do_cpdown(dmsg, a);
c2b27bf2 25398+ if (!err)
392086de
AM
25399+ err = au_do_unlink_wh(dmsg, a);
25400+ if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
25401+ err = au_do_unlink(dmsg, a);
c2b27bf2
AM
25402+ if (unlikely(err))
25403+ goto out_unlock;
25404+
c1595e42
JR
25405+ AuDbg("%pd2, 0x%x, %d --> %d\n",
25406+ a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
076b876e
AM
25407+ if (find_lower_writable(a) < 0)
25408+ a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
25409+
25410+ if (a->mvdown.flags & AUFS_MVDOWN_STFS)
25411+ au_do_stfs(dmsg, a);
25412+
c2b27bf2 25413+ /* maintain internal array */
392086de
AM
25414+ if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
25415+ au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
5afbbe0d 25416+ au_set_dbtop(a->dentry, a->mvd_bdst);
392086de 25417+ au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
5afbbe0d 25418+ au_set_ibtop(a->inode, a->mvd_bdst);
79b8bda9
AM
25419+ } else {
25420+ /* hide the lower */
25421+ au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
5afbbe0d 25422+ au_set_dbbot(a->dentry, a->mvd_bsrc);
79b8bda9 25423+ au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
5afbbe0d 25424+ au_set_ibbot(a->inode, a->mvd_bsrc);
392086de 25425+ }
5afbbe0d
AM
25426+ if (au_dbbot(a->dentry) < a->mvd_bdst)
25427+ au_set_dbbot(a->dentry, a->mvd_bdst);
25428+ if (au_ibbot(a->inode) < a->mvd_bdst)
25429+ au_set_ibbot(a->inode, a->mvd_bdst);
c2b27bf2
AM
25430+
25431+out_unlock:
392086de 25432+ au_do_unlock(dmsg, a);
c2b27bf2
AM
25433+out:
25434+ AuTraceErr(err);
25435+ return err;
25436+}
25437+
25438+/* ---------------------------------------------------------------------- */
25439+
c2b27bf2 25440+/* make sure the file is idle */
392086de 25441+static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25442+{
25443+ int err, plinked;
c2b27bf2
AM
25444+
25445+ err = 0;
c2b27bf2 25446+ plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
5afbbe0d 25447+ if (au_dbtop(a->dentry) == a->mvd_bsrc
c1595e42 25448+ && au_dcount(a->dentry) == 1
c2b27bf2 25449+ && atomic_read(&a->inode->i_count) == 1
392086de 25450+ /* && a->mvd_h_src_inode->i_nlink == 1 */
c2b27bf2
AM
25451+ && (!plinked || !au_plink_test(a->inode))
25452+ && a->inode->i_nlink == 1)
25453+ goto out;
25454+
25455+ err = -EBUSY;
392086de 25456+ AU_MVD_PR(dmsg,
c1595e42 25457+ "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
5afbbe0d 25458+ a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
c2b27bf2 25459+ atomic_read(&a->inode->i_count), a->inode->i_nlink,
392086de 25460+ a->mvd_h_src_inode->i_nlink,
c2b27bf2
AM
25461+ plinked, plinked ? au_plink_test(a->inode) : 0);
25462+
25463+out:
25464+ AuTraceErr(err);
25465+ return err;
25466+}
25467+
25468+/* make sure the parent dir is fine */
392086de 25469+static int au_mvd_args_parent(const unsigned char dmsg,
c2b27bf2
AM
25470+ struct au_mvd_args *a)
25471+{
25472+ int err;
25473+ aufs_bindex_t bindex;
25474+
25475+ err = 0;
25476+ if (unlikely(au_alive_dir(a->parent))) {
25477+ err = -ENOENT;
392086de 25478+ AU_MVD_PR(dmsg, "parent dir is dead\n");
c2b27bf2
AM
25479+ goto out;
25480+ }
25481+
25482+ a->bopq = au_dbdiropq(a->parent);
25483+ bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
25484+ AuDbg("b%d\n", bindex);
25485+ if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
25486+ || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
25487+ err = -EINVAL;
392086de
AM
25488+ a->mvd_errno = EAU_MVDOWN_OPAQUE;
25489+ AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
c2b27bf2
AM
25490+ a->bopq, a->mvd_bdst);
25491+ }
25492+
25493+out:
25494+ AuTraceErr(err);
25495+ return err;
25496+}
25497+
392086de 25498+static int au_mvd_args_intermediate(const unsigned char dmsg,
c2b27bf2
AM
25499+ struct au_mvd_args *a)
25500+{
25501+ int err;
25502+ struct au_dinfo *dinfo, *tmp;
25503+
25504+ /* lookup the next lower positive entry */
25505+ err = -ENOMEM;
25506+ tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
25507+ if (unlikely(!tmp))
25508+ goto out;
25509+
25510+ a->bfound = -1;
25511+ a->bwh = -1;
25512+ dinfo = au_di(a->dentry);
25513+ au_di_cp(tmp, dinfo);
25514+ au_di_swap(tmp, dinfo);
25515+
25516+ /* returns the number of positive dentries */
5afbbe0d
AM
25517+ err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
25518+ /* AuLkup_IGNORE_PERM */ 0);
c2b27bf2
AM
25519+ if (!err)
25520+ a->bwh = au_dbwh(a->dentry);
25521+ else if (err > 0)
5afbbe0d 25522+ a->bfound = au_dbtop(a->dentry);
c2b27bf2
AM
25523+
25524+ au_di_swap(tmp, dinfo);
25525+ au_rw_write_unlock(&tmp->di_rwsem);
25526+ au_di_free(tmp);
25527+ if (unlikely(err < 0))
392086de 25528+ AU_MVD_PR(dmsg, "failed look-up lower\n");
c2b27bf2
AM
25529+
25530+ /*
25531+ * here, we have these cases.
25532+ * bfound == -1
25533+ * no positive dentry under bsrc. there are more sub-cases.
25534+ * bwh < 0
25535+ * there no whiteout, we can safely move-down.
25536+ * bwh <= bsrc
25537+ * impossible
25538+ * bsrc < bwh && bwh < bdst
25539+ * there is a whiteout on RO branch. cannot proceed.
25540+ * bwh == bdst
25541+ * there is a whiteout on the RW target branch. it should
25542+ * be removed.
25543+ * bdst < bwh
25544+ * there is a whiteout somewhere unrelated branch.
25545+ * -1 < bfound && bfound <= bsrc
25546+ * impossible.
25547+ * bfound < bdst
25548+ * found, but it is on RO branch between bsrc and bdst. cannot
25549+ * proceed.
25550+ * bfound == bdst
25551+ * found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
25552+ * error.
25553+ * bdst < bfound
25554+ * found, after we create the file on bdst, it will be hidden.
25555+ */
25556+
25557+ AuDebugOn(a->bfound == -1
25558+ && a->bwh != -1
25559+ && a->bwh <= a->mvd_bsrc);
25560+ AuDebugOn(-1 < a->bfound
25561+ && a->bfound <= a->mvd_bsrc);
25562+
25563+ err = -EINVAL;
25564+ if (a->bfound == -1
25565+ && a->mvd_bsrc < a->bwh
25566+ && a->bwh != -1
25567+ && a->bwh < a->mvd_bdst) {
392086de
AM
25568+ a->mvd_errno = EAU_MVDOWN_WHITEOUT;
25569+ AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
c2b27bf2
AM
25570+ a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
25571+ goto out;
25572+ } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
392086de
AM
25573+ a->mvd_errno = EAU_MVDOWN_UPPER;
25574+ AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
c2b27bf2
AM
25575+ a->mvd_bdst, a->bfound);
25576+ goto out;
25577+ }
25578+
25579+ err = 0; /* success */
25580+
25581+out:
25582+ AuTraceErr(err);
25583+ return err;
25584+}
25585+
392086de 25586+static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25587+{
25588+ int err;
25589+
392086de
AM
25590+ err = 0;
25591+ if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25592+ && a->bfound == a->mvd_bdst)
25593+ err = -EEXIST;
c2b27bf2
AM
25594+ AuTraceErr(err);
25595+ return err;
25596+}
25597+
392086de 25598+static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25599+{
25600+ int err;
25601+ struct au_branch *br;
25602+
25603+ err = -EISDIR;
25604+ if (unlikely(S_ISDIR(a->inode->i_mode)))
25605+ goto out;
25606+
25607+ err = -EINVAL;
392086de 25608+ if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
5afbbe0d 25609+ a->mvd_bsrc = au_ibtop(a->inode);
392086de
AM
25610+ else {
25611+ a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
25612+ if (unlikely(a->mvd_bsrc < 0
5afbbe0d
AM
25613+ || (a->mvd_bsrc < au_dbtop(a->dentry)
25614+ || au_dbbot(a->dentry) < a->mvd_bsrc
392086de 25615+ || !au_h_dptr(a->dentry, a->mvd_bsrc))
5afbbe0d
AM
25616+ || (a->mvd_bsrc < au_ibtop(a->inode)
25617+ || au_ibbot(a->inode) < a->mvd_bsrc
392086de
AM
25618+ || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
25619+ a->mvd_errno = EAU_MVDOWN_NOUPPER;
25620+ AU_MVD_PR(dmsg, "no upper\n");
25621+ goto out;
25622+ }
25623+ }
5afbbe0d 25624+ if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
392086de
AM
25625+ a->mvd_errno = EAU_MVDOWN_BOTTOM;
25626+ AU_MVD_PR(dmsg, "on the bottom\n");
c2b27bf2
AM
25627+ goto out;
25628+ }
392086de 25629+ a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
c2b27bf2
AM
25630+ br = au_sbr(a->sb, a->mvd_bsrc);
25631+ err = au_br_rdonly(br);
392086de
AM
25632+ if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
25633+ if (unlikely(err))
25634+ goto out;
25635+ } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
25636+ || IS_APPEND(a->mvd_h_src_inode))) {
25637+ if (err)
25638+ a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
25639+ /* go on */
25640+ } else
c2b27bf2
AM
25641+ goto out;
25642+
25643+ err = -EINVAL;
392086de
AM
25644+ if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
25645+ a->mvd_bdst = find_lower_writable(a);
25646+ if (unlikely(a->mvd_bdst < 0)) {
25647+ a->mvd_errno = EAU_MVDOWN_BOTTOM;
25648+ AU_MVD_PR(dmsg, "no writable lower branch\n");
25649+ goto out;
25650+ }
25651+ } else {
25652+ a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
25653+ if (unlikely(a->mvd_bdst < 0
5afbbe0d 25654+ || au_sbbot(a->sb) < a->mvd_bdst)) {
392086de
AM
25655+ a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
25656+ AU_MVD_PR(dmsg, "no lower brid\n");
25657+ goto out;
25658+ }
c2b27bf2
AM
25659+ }
25660+
392086de 25661+ err = au_mvd_args_busy(dmsg, a);
c2b27bf2 25662+ if (!err)
392086de 25663+ err = au_mvd_args_parent(dmsg, a);
c2b27bf2 25664+ if (!err)
392086de 25665+ err = au_mvd_args_intermediate(dmsg, a);
c2b27bf2 25666+ if (!err)
392086de 25667+ err = au_mvd_args_exist(dmsg, a);
c2b27bf2
AM
25668+ if (!err)
25669+ AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
25670+
25671+out:
25672+ AuTraceErr(err);
25673+ return err;
25674+}
25675+
25676+int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
25677+{
392086de
AM
25678+ int err, e;
25679+ unsigned char dmsg;
25680+ struct au_mvd_args *args;
79b8bda9 25681+ struct inode *inode;
c2b27bf2 25682+
79b8bda9 25683+ inode = d_inode(dentry);
c2b27bf2
AM
25684+ err = -EPERM;
25685+ if (unlikely(!capable(CAP_SYS_ADMIN)))
25686+ goto out;
25687+
392086de
AM
25688+ err = -ENOMEM;
25689+ args = kmalloc(sizeof(*args), GFP_NOFS);
25690+ if (unlikely(!args))
25691+ goto out;
25692+
25693+ err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
25694+ if (!err)
25695+ err = !access_ok(VERIFY_WRITE, uarg, sizeof(*uarg));
c2b27bf2
AM
25696+ if (unlikely(err)) {
25697+ err = -EFAULT;
392086de
AM
25698+ AuTraceErr(err);
25699+ goto out_free;
c2b27bf2 25700+ }
392086de
AM
25701+ AuDbg("flags 0x%x\n", args->mvdown.flags);
25702+ args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
25703+ args->mvdown.au_errno = 0;
25704+ args->dentry = dentry;
79b8bda9 25705+ args->inode = inode;
392086de 25706+ args->sb = dentry->d_sb;
c2b27bf2 25707+
392086de
AM
25708+ err = -ENOENT;
25709+ dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
25710+ args->parent = dget_parent(dentry);
5527c038 25711+ args->dir = d_inode(args->parent);
febd17d6 25712+ inode_lock_nested(args->dir, I_MUTEX_PARENT);
392086de
AM
25713+ dput(args->parent);
25714+ if (unlikely(args->parent != dentry->d_parent)) {
25715+ AU_MVD_PR(dmsg, "parent dir is moved\n");
c2b27bf2
AM
25716+ goto out_dir;
25717+ }
25718+
febd17d6 25719+ inode_lock_nested(inode, I_MUTEX_CHILD);
b95c5147 25720+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
c2b27bf2
AM
25721+ if (unlikely(err))
25722+ goto out_inode;
25723+
392086de
AM
25724+ di_write_lock_parent(args->parent);
25725+ err = au_mvd_args(dmsg, args);
c2b27bf2
AM
25726+ if (unlikely(err))
25727+ goto out_parent;
25728+
392086de 25729+ err = au_do_mvdown(dmsg, args);
c2b27bf2
AM
25730+ if (unlikely(err))
25731+ goto out_parent;
c2b27bf2 25732+
392086de 25733+ au_cpup_attr_timesizes(args->dir);
79b8bda9
AM
25734+ au_cpup_attr_timesizes(inode);
25735+ if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
25736+ au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
c2b27bf2
AM
25737+ /* au_digen_dec(dentry); */
25738+
25739+out_parent:
392086de 25740+ di_write_unlock(args->parent);
c2b27bf2
AM
25741+ aufs_read_unlock(dentry, AuLock_DW);
25742+out_inode:
febd17d6 25743+ inode_unlock(inode);
c2b27bf2 25744+out_dir:
febd17d6 25745+ inode_unlock(args->dir);
392086de
AM
25746+out_free:
25747+ e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
25748+ if (unlikely(e))
25749+ err = -EFAULT;
e49925d1 25750+ au_kfree_rcu(args);
c2b27bf2
AM
25751+out:
25752+ AuTraceErr(err);
25753+ return err;
25754+}
25755diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
25756--- /usr/share/empty/fs/aufs/opts.c 1970-01-01 01:00:00.000000000 +0100
0b487229
JR
25757+++ linux/fs/aufs/opts.c 2021-02-22 23:30:37.702680911 +0100
25758@@ -0,0 +1,1880 @@
cd7a4cd9 25759+// SPDX-License-Identifier: GPL-2.0
1facf9fc 25760+/*
0b487229 25761+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 25762+ *
25763+ * This program, aufs is free software; you can redistribute it and/or modify
25764+ * it under the terms of the GNU General Public License as published by
25765+ * the Free Software Foundation; either version 2 of the License, or
25766+ * (at your option) any later version.
dece6358
AM
25767+ *
25768+ * This program is distributed in the hope that it will be useful,
25769+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
25770+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25771+ * GNU General Public License for more details.
25772+ *
25773+ * You should have received a copy of the GNU General Public License
523b37e3 25774+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 25775+ */
25776+
25777+/*
25778+ * mount options/flags
25779+ */
25780+
dece6358 25781+#include <linux/namei.h>
1facf9fc 25782+#include <linux/types.h> /* a distribution requires */
25783+#include <linux/parser.h>
25784+#include "aufs.h"
25785+
25786+/* ---------------------------------------------------------------------- */
25787+
25788+enum {
25789+ Opt_br,
7e9cd9fe
AM
25790+ Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
25791+ Opt_idel, Opt_imod,
25792+ Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
dece6358 25793+ Opt_rdblk_def, Opt_rdhash_def,
7e9cd9fe 25794+ Opt_xino, Opt_noxino,
1facf9fc 25795+ Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
25796+ Opt_trunc_xino_path, Opt_itrunc_xino,
25797+ Opt_trunc_xib, Opt_notrunc_xib,
dece6358 25798+ Opt_shwh, Opt_noshwh,
1facf9fc 25799+ Opt_plink, Opt_noplink, Opt_list_plink,
25800+ Opt_udba,
4a4d8108 25801+ Opt_dio, Opt_nodio,
1facf9fc 25802+ Opt_diropq_a, Opt_diropq_w,
25803+ Opt_warn_perm, Opt_nowarn_perm,
25804+ Opt_wbr_copyup, Opt_wbr_create,
076b876e 25805+ Opt_fhsm_sec,
1facf9fc 25806+ Opt_verbose, Opt_noverbose,
25807+ Opt_sum, Opt_nosum, Opt_wsum,
076b876e 25808+ Opt_dirperm1, Opt_nodirperm1,
8b6a4947 25809+ Opt_dirren, Opt_nodirren,
c1595e42 25810+ Opt_acl, Opt_noacl,
1facf9fc 25811+ Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
25812+};
25813+
25814+static match_table_t options = {
25815+ {Opt_br, "br=%s"},
25816+ {Opt_br, "br:%s"},
25817+
25818+ {Opt_add, "add=%d:%s"},
25819+ {Opt_add, "add:%d:%s"},
25820+ {Opt_add, "ins=%d:%s"},
25821+ {Opt_add, "ins:%d:%s"},
25822+ {Opt_append, "append=%s"},
25823+ {Opt_append, "append:%s"},
25824+ {Opt_prepend, "prepend=%s"},
25825+ {Opt_prepend, "prepend:%s"},
25826+
25827+ {Opt_del, "del=%s"},
25828+ {Opt_del, "del:%s"},
25829+ /* {Opt_idel, "idel:%d"}, */
25830+ {Opt_mod, "mod=%s"},
25831+ {Opt_mod, "mod:%s"},
25832+ /* {Opt_imod, "imod:%d:%s"}, */
25833+
25834+ {Opt_dirwh, "dirwh=%d"},
25835+
25836+ {Opt_xino, "xino=%s"},
25837+ {Opt_noxino, "noxino"},
25838+ {Opt_trunc_xino, "trunc_xino"},
25839+ {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
25840+ {Opt_notrunc_xino, "notrunc_xino"},
25841+ {Opt_trunc_xino_path, "trunc_xino=%s"},
25842+ {Opt_itrunc_xino, "itrunc_xino=%d"},
25843+ /* {Opt_zxino, "zxino=%s"}, */
25844+ {Opt_trunc_xib, "trunc_xib"},
25845+ {Opt_notrunc_xib, "notrunc_xib"},
25846+
e49829fe 25847+#ifdef CONFIG_PROC_FS
1facf9fc 25848+ {Opt_plink, "plink"},
e49829fe
JR
25849+#else
25850+ {Opt_ignore_silent, "plink"},
25851+#endif
25852+
1facf9fc 25853+ {Opt_noplink, "noplink"},
e49829fe 25854+
1facf9fc 25855+#ifdef CONFIG_AUFS_DEBUG
25856+ {Opt_list_plink, "list_plink"},
25857+#endif
25858+
25859+ {Opt_udba, "udba=%s"},
25860+
4a4d8108
AM
25861+ {Opt_dio, "dio"},
25862+ {Opt_nodio, "nodio"},
25863+
8b6a4947
AM
25864+#ifdef CONFIG_AUFS_DIRREN
25865+ {Opt_dirren, "dirren"},
25866+ {Opt_nodirren, "nodirren"},
25867+#else
25868+ {Opt_ignore, "dirren"},
25869+ {Opt_ignore_silent, "nodirren"},
25870+#endif
25871+
076b876e
AM
25872+#ifdef CONFIG_AUFS_FHSM
25873+ {Opt_fhsm_sec, "fhsm_sec=%d"},
25874+#else
8b6a4947 25875+ {Opt_ignore, "fhsm_sec=%d"},
076b876e
AM
25876+#endif
25877+
1facf9fc 25878+ {Opt_diropq_a, "diropq=always"},
25879+ {Opt_diropq_a, "diropq=a"},
25880+ {Opt_diropq_w, "diropq=whiteouted"},
25881+ {Opt_diropq_w, "diropq=w"},
25882+
25883+ {Opt_warn_perm, "warn_perm"},
25884+ {Opt_nowarn_perm, "nowarn_perm"},
25885+
25886+ /* keep them temporary */
1facf9fc 25887+ {Opt_ignore_silent, "nodlgt"},
8b6a4947 25888+ {Opt_ignore, "clean_plink"},
1facf9fc 25889+
dece6358
AM
25890+#ifdef CONFIG_AUFS_SHWH
25891+ {Opt_shwh, "shwh"},
25892+#endif
25893+ {Opt_noshwh, "noshwh"},
25894+
076b876e
AM
25895+ {Opt_dirperm1, "dirperm1"},
25896+ {Opt_nodirperm1, "nodirperm1"},
25897+
1facf9fc 25898+ {Opt_verbose, "verbose"},
25899+ {Opt_verbose, "v"},
25900+ {Opt_noverbose, "noverbose"},
25901+ {Opt_noverbose, "quiet"},
25902+ {Opt_noverbose, "q"},
25903+ {Opt_noverbose, "silent"},
25904+
25905+ {Opt_sum, "sum"},
25906+ {Opt_nosum, "nosum"},
25907+ {Opt_wsum, "wsum"},
25908+
25909+ {Opt_rdcache, "rdcache=%d"},
25910+ {Opt_rdblk, "rdblk=%d"},
dece6358 25911+ {Opt_rdblk_def, "rdblk=def"},
1facf9fc 25912+ {Opt_rdhash, "rdhash=%d"},
dece6358 25913+ {Opt_rdhash_def, "rdhash=def"},
1facf9fc 25914+
25915+ {Opt_wbr_create, "create=%s"},
25916+ {Opt_wbr_create, "create_policy=%s"},
25917+ {Opt_wbr_copyup, "cpup=%s"},
25918+ {Opt_wbr_copyup, "copyup=%s"},
25919+ {Opt_wbr_copyup, "copyup_policy=%s"},
25920+
c1595e42
JR
25921+ /* generic VFS flag */
25922+#ifdef CONFIG_FS_POSIX_ACL
25923+ {Opt_acl, "acl"},
25924+ {Opt_noacl, "noacl"},
25925+#else
8b6a4947 25926+ {Opt_ignore, "acl"},
c1595e42
JR
25927+ {Opt_ignore_silent, "noacl"},
25928+#endif
25929+
1facf9fc 25930+ /* internal use for the scripts */
25931+ {Opt_ignore_silent, "si=%s"},
25932+
25933+ {Opt_br, "dirs=%s"},
25934+ {Opt_ignore, "debug=%d"},
25935+ {Opt_ignore, "delete=whiteout"},
25936+ {Opt_ignore, "delete=all"},
25937+ {Opt_ignore, "imap=%s"},
25938+
1308ab2a 25939+ /* temporary workaround, due to old mount(8)? */
25940+ {Opt_ignore_silent, "relatime"},
25941+
1facf9fc 25942+ {Opt_err, NULL}
25943+};
25944+
25945+/* ---------------------------------------------------------------------- */
25946+
076b876e 25947+static const char *au_parser_pattern(int val, match_table_t tbl)
1facf9fc 25948+{
076b876e
AM
25949+ struct match_token *p;
25950+
25951+ p = tbl;
25952+ while (p->pattern) {
25953+ if (p->token == val)
25954+ return p->pattern;
25955+ p++;
1facf9fc 25956+ }
25957+ BUG();
25958+ return "??";
25959+}
25960+
076b876e
AM
25961+static const char *au_optstr(int *val, match_table_t tbl)
25962+{
25963+ struct match_token *p;
25964+ int v;
25965+
25966+ v = *val;
2000de60
JR
25967+ if (!v)
25968+ goto out;
076b876e 25969+ p = tbl;
2000de60
JR
25970+ while (p->pattern) {
25971+ if (p->token
25972+ && (v & p->token) == p->token) {
076b876e
AM
25973+ *val &= ~p->token;
25974+ return p->pattern;
25975+ }
25976+ p++;
25977+ }
2000de60
JR
25978+
25979+out:
076b876e
AM
25980+ return NULL;
25981+}
25982+
1facf9fc 25983+/* ---------------------------------------------------------------------- */
25984+
1e00d052 25985+static match_table_t brperm = {
1facf9fc 25986+ {AuBrPerm_RO, AUFS_BRPERM_RO},
25987+ {AuBrPerm_RR, AUFS_BRPERM_RR},
25988+ {AuBrPerm_RW, AUFS_BRPERM_RW},
1e00d052
AM
25989+ {0, NULL}
25990+};
1facf9fc 25991+
86dc4139 25992+static match_table_t brattr = {
076b876e
AM
25993+ /* general */
25994+ {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
25995+ {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
c1595e42 25996+ /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
86dc4139 25997+ {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
2000de60 25998+#ifdef CONFIG_AUFS_FHSM
076b876e 25999+ {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
2000de60
JR
26000+#endif
26001+#ifdef CONFIG_AUFS_XATTR
c1595e42
JR
26002+ {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
26003+ {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
26004+ {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
26005+ {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
26006+ {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
26007+ {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
2000de60 26008+#endif
076b876e
AM
26009+
26010+ /* ro/rr branch */
1e00d052 26011+ {AuBrRAttr_WH, AUFS_BRRATTR_WH},
076b876e
AM
26012+
26013+ /* rw branch */
26014+ {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
1e00d052 26015+ {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
076b876e 26016+
1e00d052 26017+ {0, NULL}
1facf9fc 26018+};
26019+
1e00d052
AM
26020+static int br_attr_val(char *str, match_table_t table, substring_t args[])
26021+{
26022+ int attr, v;
26023+ char *p;
26024+
26025+ attr = 0;
26026+ do {
26027+ p = strchr(str, '+');
26028+ if (p)
26029+ *p = 0;
26030+ v = match_token(str, table, args);
076b876e
AM
26031+ if (v) {
26032+ if (v & AuBrAttr_CMOO_Mask)
26033+ attr &= ~AuBrAttr_CMOO_Mask;
1e00d052 26034+ attr |= v;
076b876e 26035+ } else {
1e00d052
AM
26036+ if (p)
26037+ *p = '+';
0c3ec466 26038+ pr_warn("ignored branch attribute %s\n", str);
1e00d052
AM
26039+ break;
26040+ }
26041+ if (p)
26042+ str = p + 1;
26043+ } while (p);
26044+
26045+ return attr;
26046+}
26047+
076b876e
AM
26048+static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
26049+{
26050+ int sz;
26051+ const char *p;
26052+ char *q;
26053+
076b876e
AM
26054+ q = str->a;
26055+ *q = 0;
26056+ p = au_optstr(&perm, brattr);
26057+ if (p) {
26058+ sz = strlen(p);
26059+ memcpy(q, p, sz + 1);
26060+ q += sz;
26061+ } else
26062+ goto out;
26063+
26064+ do {
26065+ p = au_optstr(&perm, brattr);
26066+ if (p) {
26067+ *q++ = '+';
26068+ sz = strlen(p);
26069+ memcpy(q, p, sz + 1);
26070+ q += sz;
26071+ }
26072+ } while (p);
26073+
26074+out:
c1595e42 26075+ return q - str->a;
076b876e
AM
26076+}
26077+
4a4d8108 26078+static int noinline_for_stack br_perm_val(char *perm)
1facf9fc 26079+{
076b876e
AM
26080+ int val, bad, sz;
26081+ char *p;
1facf9fc 26082+ substring_t args[MAX_OPT_ARGS];
076b876e 26083+ au_br_perm_str_t attr;
1facf9fc 26084+
1e00d052
AM
26085+ p = strchr(perm, '+');
26086+ if (p)
26087+ *p = 0;
26088+ val = match_token(perm, brperm, args);
26089+ if (!val) {
26090+ if (p)
26091+ *p = '+';
0c3ec466 26092+ pr_warn("ignored branch permission %s\n", perm);
1e00d052
AM
26093+ val = AuBrPerm_RO;
26094+ goto out;
26095+ }
26096+ if (!p)
26097+ goto out;
26098+
076b876e
AM
26099+ val |= br_attr_val(p + 1, brattr, args);
26100+
26101+ bad = 0;
86dc4139 26102+ switch (val & AuBrPerm_Mask) {
1e00d052
AM
26103+ case AuBrPerm_RO:
26104+ case AuBrPerm_RR:
076b876e
AM
26105+ bad = val & AuBrWAttr_Mask;
26106+ val &= ~AuBrWAttr_Mask;
1e00d052
AM
26107+ break;
26108+ case AuBrPerm_RW:
076b876e
AM
26109+ bad = val & AuBrRAttr_Mask;
26110+ val &= ~AuBrRAttr_Mask;
1e00d052
AM
26111+ break;
26112+ }
c1595e42
JR
26113+
26114+ /*
26115+ * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
26116+ * does not treat it as an error, just warning.
26117+ * this is a tiny guard for the user operation.
26118+ */
26119+ if (val & AuBrAttr_UNPIN) {
26120+ bad |= AuBrAttr_UNPIN;
26121+ val &= ~AuBrAttr_UNPIN;
26122+ }
26123+
076b876e
AM
26124+ if (unlikely(bad)) {
26125+ sz = au_do_optstr_br_attr(&attr, bad);
26126+ AuDebugOn(!sz);
26127+ pr_warn("ignored branch attribute %s\n", attr.a);
26128+ }
1e00d052
AM
26129+
26130+out:
1facf9fc 26131+ return val;
26132+}
26133+
076b876e 26134+void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
1facf9fc 26135+{
076b876e
AM
26136+ au_br_perm_str_t attr;
26137+ const char *p;
26138+ char *q;
1e00d052
AM
26139+ int sz;
26140+
076b876e
AM
26141+ q = str->a;
26142+ p = au_optstr(&perm, brperm);
26143+ AuDebugOn(!p || !*p);
26144+ sz = strlen(p);
26145+ memcpy(q, p, sz + 1);
26146+ q += sz;
1e00d052 26147+
076b876e
AM
26148+ sz = au_do_optstr_br_attr(&attr, perm);
26149+ if (sz) {
26150+ *q++ = '+';
26151+ memcpy(q, attr.a, sz + 1);
1e00d052
AM
26152+ }
26153+
076b876e 26154+ AuDebugOn(strlen(str->a) >= sizeof(str->a));
1facf9fc 26155+}
26156+
26157+/* ---------------------------------------------------------------------- */
26158+
26159+static match_table_t udbalevel = {
26160+ {AuOpt_UDBA_REVAL, "reval"},
26161+ {AuOpt_UDBA_NONE, "none"},
4a4d8108
AM
26162+#ifdef CONFIG_AUFS_HNOTIFY
26163+ {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
26164+#ifdef CONFIG_AUFS_HFSNOTIFY
26165+ {AuOpt_UDBA_HNOTIFY, "fsnotify"},
4a4d8108 26166+#endif
1facf9fc 26167+#endif
26168+ {-1, NULL}
26169+};
26170+
4a4d8108 26171+static int noinline_for_stack udba_val(char *str)
1facf9fc 26172+{
26173+ substring_t args[MAX_OPT_ARGS];
26174+
7f207e10 26175+ return match_token(str, udbalevel, args);
1facf9fc 26176+}
26177+
26178+const char *au_optstr_udba(int udba)
26179+{
076b876e 26180+ return au_parser_pattern(udba, udbalevel);
1facf9fc 26181+}
26182+
26183+/* ---------------------------------------------------------------------- */
26184+
26185+static match_table_t au_wbr_create_policy = {
26186+ {AuWbrCreate_TDP, "tdp"},
26187+ {AuWbrCreate_TDP, "top-down-parent"},
26188+ {AuWbrCreate_RR, "rr"},
26189+ {AuWbrCreate_RR, "round-robin"},
26190+ {AuWbrCreate_MFS, "mfs"},
26191+ {AuWbrCreate_MFS, "most-free-space"},
26192+ {AuWbrCreate_MFSV, "mfs:%d"},
26193+ {AuWbrCreate_MFSV, "most-free-space:%d"},
26194+
f2c43d5f
AM
26195+ /* top-down regardless the parent, and then mfs */
26196+ {AuWbrCreate_TDMFS, "tdmfs:%d"},
26197+ {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
26198+
1facf9fc 26199+ {AuWbrCreate_MFSRR, "mfsrr:%d"},
26200+ {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
26201+ {AuWbrCreate_PMFS, "pmfs"},
26202+ {AuWbrCreate_PMFSV, "pmfs:%d"},
392086de
AM
26203+ {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
26204+ {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
1facf9fc 26205+
26206+ {-1, NULL}
26207+};
26208+
1facf9fc 26209+static int au_wbr_mfs_wmark(substring_t *arg, char *str,
26210+ struct au_opt_wbr_create *create)
26211+{
26212+ int err;
26213+ unsigned long long ull;
26214+
26215+ err = 0;
a2654f78 26216+ if (!match_u64(arg, &ull))
1facf9fc 26217+ create->mfsrr_watermark = ull;
26218+ else {
4a4d8108 26219+ pr_err("bad integer in %s\n", str);
1facf9fc 26220+ err = -EINVAL;
26221+ }
26222+
26223+ return err;
26224+}
26225+
26226+static int au_wbr_mfs_sec(substring_t *arg, char *str,
26227+ struct au_opt_wbr_create *create)
26228+{
26229+ int n, err;
26230+
26231+ err = 0;
027c5e7a 26232+ if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
1facf9fc 26233+ create->mfs_second = n;
26234+ else {
4a4d8108 26235+ pr_err("bad integer in %s\n", str);
1facf9fc 26236+ err = -EINVAL;
26237+ }
26238+
26239+ return err;
26240+}
26241+
4a4d8108
AM
26242+static int noinline_for_stack
26243+au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
1facf9fc 26244+{
26245+ int err, e;
26246+ substring_t args[MAX_OPT_ARGS];
26247+
26248+ err = match_token(str, au_wbr_create_policy, args);
26249+ create->wbr_create = err;
26250+ switch (err) {
26251+ case AuWbrCreate_MFSRRV:
f2c43d5f 26252+ case AuWbrCreate_TDMFSV:
392086de 26253+ case AuWbrCreate_PMFSRRV:
1facf9fc 26254+ e = au_wbr_mfs_wmark(&args[0], str, create);
26255+ if (!e)
26256+ e = au_wbr_mfs_sec(&args[1], str, create);
26257+ if (unlikely(e))
26258+ err = e;
26259+ break;
26260+ case AuWbrCreate_MFSRR:
f2c43d5f 26261+ case AuWbrCreate_TDMFS:
392086de 26262+ case AuWbrCreate_PMFSRR:
1facf9fc 26263+ e = au_wbr_mfs_wmark(&args[0], str, create);
26264+ if (unlikely(e)) {
26265+ err = e;
26266+ break;
26267+ }
26268+ /*FALLTHROUGH*/
26269+ case AuWbrCreate_MFS:
26270+ case AuWbrCreate_PMFS:
027c5e7a 26271+ create->mfs_second = AUFS_MFS_DEF_SEC;
1facf9fc 26272+ break;
26273+ case AuWbrCreate_MFSV:
26274+ case AuWbrCreate_PMFSV:
26275+ e = au_wbr_mfs_sec(&args[0], str, create);
26276+ if (unlikely(e))
26277+ err = e;
26278+ break;
26279+ }
26280+
26281+ return err;
26282+}
26283+
26284+const char *au_optstr_wbr_create(int wbr_create)
26285+{
076b876e 26286+ return au_parser_pattern(wbr_create, au_wbr_create_policy);
1facf9fc 26287+}
26288+
26289+static match_table_t au_wbr_copyup_policy = {
26290+ {AuWbrCopyup_TDP, "tdp"},
26291+ {AuWbrCopyup_TDP, "top-down-parent"},
26292+ {AuWbrCopyup_BUP, "bup"},
26293+ {AuWbrCopyup_BUP, "bottom-up-parent"},
26294+ {AuWbrCopyup_BU, "bu"},
26295+ {AuWbrCopyup_BU, "bottom-up"},
26296+ {-1, NULL}
26297+};
26298+
4a4d8108 26299+static int noinline_for_stack au_wbr_copyup_val(char *str)
1facf9fc 26300+{
26301+ substring_t args[MAX_OPT_ARGS];
26302+
26303+ return match_token(str, au_wbr_copyup_policy, args);
26304+}
26305+
26306+const char *au_optstr_wbr_copyup(int wbr_copyup)
26307+{
076b876e 26308+ return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
1facf9fc 26309+}
26310+
26311+/* ---------------------------------------------------------------------- */
26312+
26313+static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
26314+
26315+static void dump_opts(struct au_opts *opts)
26316+{
26317+#ifdef CONFIG_AUFS_DEBUG
26318+ /* reduce stack space */
26319+ union {
26320+ struct au_opt_add *add;
26321+ struct au_opt_del *del;
26322+ struct au_opt_mod *mod;
26323+ struct au_opt_xino *xino;
26324+ struct au_opt_xino_itrunc *xino_itrunc;
26325+ struct au_opt_wbr_create *create;
26326+ } u;
26327+ struct au_opt *opt;
26328+
26329+ opt = opts->opt;
26330+ while (opt->type != Opt_tail) {
26331+ switch (opt->type) {
26332+ case Opt_add:
26333+ u.add = &opt->add;
26334+ AuDbg("add {b%d, %s, 0x%x, %p}\n",
26335+ u.add->bindex, u.add->pathname, u.add->perm,
26336+ u.add->path.dentry);
26337+ break;
26338+ case Opt_del:
26339+ case Opt_idel:
26340+ u.del = &opt->del;
26341+ AuDbg("del {%s, %p}\n",
26342+ u.del->pathname, u.del->h_path.dentry);
26343+ break;
26344+ case Opt_mod:
26345+ case Opt_imod:
26346+ u.mod = &opt->mod;
26347+ AuDbg("mod {%s, 0x%x, %p}\n",
26348+ u.mod->path, u.mod->perm, u.mod->h_root);
26349+ break;
26350+ case Opt_append:
26351+ u.add = &opt->add;
26352+ AuDbg("append {b%d, %s, 0x%x, %p}\n",
26353+ u.add->bindex, u.add->pathname, u.add->perm,
26354+ u.add->path.dentry);
26355+ break;
26356+ case Opt_prepend:
26357+ u.add = &opt->add;
26358+ AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
26359+ u.add->bindex, u.add->pathname, u.add->perm,
26360+ u.add->path.dentry);
26361+ break;
26362+ case Opt_dirwh:
26363+ AuDbg("dirwh %d\n", opt->dirwh);
26364+ break;
26365+ case Opt_rdcache:
26366+ AuDbg("rdcache %d\n", opt->rdcache);
26367+ break;
26368+ case Opt_rdblk:
26369+ AuDbg("rdblk %u\n", opt->rdblk);
26370+ break;
dece6358
AM
26371+ case Opt_rdblk_def:
26372+ AuDbg("rdblk_def\n");
26373+ break;
1facf9fc 26374+ case Opt_rdhash:
26375+ AuDbg("rdhash %u\n", opt->rdhash);
26376+ break;
dece6358
AM
26377+ case Opt_rdhash_def:
26378+ AuDbg("rdhash_def\n");
26379+ break;
1facf9fc 26380+ case Opt_xino:
26381+ u.xino = &opt->xino;
523b37e3 26382+ AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
1facf9fc 26383+ break;
26384+ case Opt_trunc_xino:
26385+ AuLabel(trunc_xino);
26386+ break;
26387+ case Opt_notrunc_xino:
26388+ AuLabel(notrunc_xino);
26389+ break;
26390+ case Opt_trunc_xino_path:
26391+ case Opt_itrunc_xino:
26392+ u.xino_itrunc = &opt->xino_itrunc;
26393+ AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
26394+ break;
1facf9fc 26395+ case Opt_noxino:
26396+ AuLabel(noxino);
26397+ break;
26398+ case Opt_trunc_xib:
26399+ AuLabel(trunc_xib);
26400+ break;
26401+ case Opt_notrunc_xib:
26402+ AuLabel(notrunc_xib);
26403+ break;
dece6358
AM
26404+ case Opt_shwh:
26405+ AuLabel(shwh);
26406+ break;
26407+ case Opt_noshwh:
26408+ AuLabel(noshwh);
26409+ break;
076b876e
AM
26410+ case Opt_dirperm1:
26411+ AuLabel(dirperm1);
26412+ break;
26413+ case Opt_nodirperm1:
26414+ AuLabel(nodirperm1);
26415+ break;
1facf9fc 26416+ case Opt_plink:
26417+ AuLabel(plink);
26418+ break;
26419+ case Opt_noplink:
26420+ AuLabel(noplink);
26421+ break;
26422+ case Opt_list_plink:
26423+ AuLabel(list_plink);
26424+ break;
26425+ case Opt_udba:
26426+ AuDbg("udba %d, %s\n",
26427+ opt->udba, au_optstr_udba(opt->udba));
26428+ break;
4a4d8108
AM
26429+ case Opt_dio:
26430+ AuLabel(dio);
26431+ break;
26432+ case Opt_nodio:
26433+ AuLabel(nodio);
26434+ break;
1facf9fc 26435+ case Opt_diropq_a:
26436+ AuLabel(diropq_a);
26437+ break;
26438+ case Opt_diropq_w:
26439+ AuLabel(diropq_w);
26440+ break;
26441+ case Opt_warn_perm:
26442+ AuLabel(warn_perm);
26443+ break;
26444+ case Opt_nowarn_perm:
26445+ AuLabel(nowarn_perm);
26446+ break;
1facf9fc 26447+ case Opt_verbose:
26448+ AuLabel(verbose);
26449+ break;
26450+ case Opt_noverbose:
26451+ AuLabel(noverbose);
26452+ break;
26453+ case Opt_sum:
26454+ AuLabel(sum);
26455+ break;
26456+ case Opt_nosum:
26457+ AuLabel(nosum);
26458+ break;
26459+ case Opt_wsum:
26460+ AuLabel(wsum);
26461+ break;
26462+ case Opt_wbr_create:
26463+ u.create = &opt->wbr_create;
26464+ AuDbg("create %d, %s\n", u.create->wbr_create,
26465+ au_optstr_wbr_create(u.create->wbr_create));
26466+ switch (u.create->wbr_create) {
26467+ case AuWbrCreate_MFSV:
26468+ case AuWbrCreate_PMFSV:
26469+ AuDbg("%d sec\n", u.create->mfs_second);
26470+ break;
26471+ case AuWbrCreate_MFSRR:
f2c43d5f 26472+ case AuWbrCreate_TDMFS:
1facf9fc 26473+ AuDbg("%llu watermark\n",
26474+ u.create->mfsrr_watermark);
26475+ break;
26476+ case AuWbrCreate_MFSRRV:
f2c43d5f 26477+ case AuWbrCreate_TDMFSV:
392086de 26478+ case AuWbrCreate_PMFSRRV:
1facf9fc 26479+ AuDbg("%llu watermark, %d sec\n",
26480+ u.create->mfsrr_watermark,
26481+ u.create->mfs_second);
26482+ break;
26483+ }
26484+ break;
26485+ case Opt_wbr_copyup:
26486+ AuDbg("copyup %d, %s\n", opt->wbr_copyup,
26487+ au_optstr_wbr_copyup(opt->wbr_copyup));
26488+ break;
076b876e
AM
26489+ case Opt_fhsm_sec:
26490+ AuDbg("fhsm_sec %u\n", opt->fhsm_second);
26491+ break;
8b6a4947
AM
26492+ case Opt_dirren:
26493+ AuLabel(dirren);
26494+ break;
26495+ case Opt_nodirren:
26496+ AuLabel(nodirren);
26497+ break;
c1595e42
JR
26498+ case Opt_acl:
26499+ AuLabel(acl);
26500+ break;
26501+ case Opt_noacl:
26502+ AuLabel(noacl);
26503+ break;
1facf9fc 26504+ default:
26505+ BUG();
26506+ }
26507+ opt++;
26508+ }
26509+#endif
26510+}
26511+
26512+void au_opts_free(struct au_opts *opts)
26513+{
26514+ struct au_opt *opt;
26515+
26516+ opt = opts->opt;
26517+ while (opt->type != Opt_tail) {
26518+ switch (opt->type) {
26519+ case Opt_add:
26520+ case Opt_append:
26521+ case Opt_prepend:
26522+ path_put(&opt->add.path);
26523+ break;
26524+ case Opt_del:
26525+ case Opt_idel:
26526+ path_put(&opt->del.h_path);
26527+ break;
26528+ case Opt_mod:
26529+ case Opt_imod:
26530+ dput(opt->mod.h_root);
26531+ break;
26532+ case Opt_xino:
26533+ fput(opt->xino.file);
26534+ break;
26535+ }
26536+ opt++;
26537+ }
26538+}
26539+
26540+static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
26541+ aufs_bindex_t bindex)
26542+{
26543+ int err;
26544+ struct au_opt_add *add = &opt->add;
26545+ char *p;
26546+
26547+ add->bindex = bindex;
1e00d052 26548+ add->perm = AuBrPerm_RO;
1facf9fc 26549+ add->pathname = opt_str;
26550+ p = strchr(opt_str, '=');
26551+ if (p) {
26552+ *p++ = 0;
26553+ if (*p)
26554+ add->perm = br_perm_val(p);
26555+ }
26556+
26557+ err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
26558+ if (!err) {
26559+ if (!p) {
26560+ add->perm = AuBrPerm_RO;
26561+ if (au_test_fs_rr(add->path.dentry->d_sb))
26562+ add->perm = AuBrPerm_RR;
2121bcd9 26563+ else if (!bindex && !(sb_flags & SB_RDONLY))
1facf9fc 26564+ add->perm = AuBrPerm_RW;
26565+ }
26566+ opt->type = Opt_add;
26567+ goto out;
26568+ }
4a4d8108 26569+ pr_err("lookup failed %s (%d)\n", add->pathname, err);
1facf9fc 26570+ err = -EINVAL;
26571+
4f0767ce 26572+out:
1facf9fc 26573+ return err;
26574+}
26575+
26576+static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
26577+{
26578+ int err;
26579+
26580+ del->pathname = args[0].from;
26581+ AuDbg("del path %s\n", del->pathname);
26582+
26583+ err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
26584+ if (unlikely(err))
4a4d8108 26585+ pr_err("lookup failed %s (%d)\n", del->pathname, err);
1facf9fc 26586+
26587+ return err;
26588+}
26589+
26590+#if 0 /* reserved for future use */
26591+static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
26592+ struct au_opt_del *del, substring_t args[])
26593+{
26594+ int err;
26595+ struct dentry *root;
26596+
26597+ err = -EINVAL;
26598+ root = sb->s_root;
26599+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d 26600+ if (bindex < 0 || au_sbbot(sb) < bindex) {
4a4d8108 26601+ pr_err("out of bounds, %d\n", bindex);
1facf9fc 26602+ goto out;
26603+ }
26604+
26605+ err = 0;
26606+ del->h_path.dentry = dget(au_h_dptr(root, bindex));
26607+ del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
26608+
4f0767ce 26609+out:
1facf9fc 26610+ aufs_read_unlock(root, !AuLock_IR);
26611+ return err;
26612+}
26613+#endif
26614+
4a4d8108
AM
26615+static int noinline_for_stack
26616+au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
1facf9fc 26617+{
26618+ int err;
26619+ struct path path;
26620+ char *p;
26621+
26622+ err = -EINVAL;
26623+ mod->path = args[0].from;
26624+ p = strchr(mod->path, '=');
26625+ if (unlikely(!p)) {
acd2b654 26626+ pr_err("no permission %s\n", args[0].from);
1facf9fc 26627+ goto out;
26628+ }
26629+
26630+ *p++ = 0;
26631+ err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
26632+ if (unlikely(err)) {
4a4d8108 26633+ pr_err("lookup failed %s (%d)\n", mod->path, err);
1facf9fc 26634+ goto out;
26635+ }
26636+
26637+ mod->perm = br_perm_val(p);
26638+ AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
26639+ mod->h_root = dget(path.dentry);
26640+ path_put(&path);
26641+
4f0767ce 26642+out:
1facf9fc 26643+ return err;
26644+}
26645+
26646+#if 0 /* reserved for future use */
26647+static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
26648+ struct au_opt_mod *mod, substring_t args[])
26649+{
26650+ int err;
26651+ struct dentry *root;
26652+
26653+ err = -EINVAL;
26654+ root = sb->s_root;
26655+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d 26656+ if (bindex < 0 || au_sbbot(sb) < bindex) {
4a4d8108 26657+ pr_err("out of bounds, %d\n", bindex);
1facf9fc 26658+ goto out;
26659+ }
26660+
26661+ err = 0;
26662+ mod->perm = br_perm_val(args[1].from);
26663+ AuDbg("mod path %s, perm 0x%x, %s\n",
26664+ mod->path, mod->perm, args[1].from);
26665+ mod->h_root = dget(au_h_dptr(root, bindex));
26666+
4f0767ce 26667+out:
1facf9fc 26668+ aufs_read_unlock(root, !AuLock_IR);
26669+ return err;
26670+}
26671+#endif
26672+
26673+static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
26674+ substring_t args[])
26675+{
26676+ int err;
26677+ struct file *file;
26678+
0b487229 26679+ file = au_xino_create(sb, args[0].from, /*silent*/0, /*wbrtop*/0);
1facf9fc 26680+ err = PTR_ERR(file);
26681+ if (IS_ERR(file))
26682+ goto out;
26683+
26684+ err = -EINVAL;
2000de60 26685+ if (unlikely(file->f_path.dentry->d_sb == sb)) {
1facf9fc 26686+ fput(file);
4a4d8108 26687+ pr_err("%s must be outside\n", args[0].from);
1facf9fc 26688+ goto out;
26689+ }
26690+
26691+ err = 0;
26692+ xino->file = file;
26693+ xino->path = args[0].from;
26694+
4f0767ce 26695+out:
1facf9fc 26696+ return err;
26697+}
26698+
4a4d8108
AM
26699+static int noinline_for_stack
26700+au_opts_parse_xino_itrunc_path(struct super_block *sb,
26701+ struct au_opt_xino_itrunc *xino_itrunc,
26702+ substring_t args[])
1facf9fc 26703+{
26704+ int err;
5afbbe0d 26705+ aufs_bindex_t bbot, bindex;
1facf9fc 26706+ struct path path;
26707+ struct dentry *root;
26708+
26709+ err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
26710+ if (unlikely(err)) {
4a4d8108 26711+ pr_err("lookup failed %s (%d)\n", args[0].from, err);
1facf9fc 26712+ goto out;
26713+ }
26714+
26715+ xino_itrunc->bindex = -1;
26716+ root = sb->s_root;
26717+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d
AM
26718+ bbot = au_sbbot(sb);
26719+ for (bindex = 0; bindex <= bbot; bindex++) {
1facf9fc 26720+ if (au_h_dptr(root, bindex) == path.dentry) {
26721+ xino_itrunc->bindex = bindex;
26722+ break;
26723+ }
26724+ }
26725+ aufs_read_unlock(root, !AuLock_IR);
26726+ path_put(&path);
26727+
26728+ if (unlikely(xino_itrunc->bindex < 0)) {
4a4d8108 26729+ pr_err("no such branch %s\n", args[0].from);
1facf9fc 26730+ err = -EINVAL;
26731+ }
26732+
4f0767ce 26733+out:
1facf9fc 26734+ return err;
26735+}
26736+
26737+/* called without aufs lock */
26738+int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
26739+{
26740+ int err, n, token;
26741+ aufs_bindex_t bindex;
26742+ unsigned char skipped;
26743+ struct dentry *root;
26744+ struct au_opt *opt, *opt_tail;
26745+ char *opt_str;
26746+ /* reduce the stack space */
26747+ union {
26748+ struct au_opt_xino_itrunc *xino_itrunc;
26749+ struct au_opt_wbr_create *create;
26750+ } u;
26751+ struct {
26752+ substring_t args[MAX_OPT_ARGS];
26753+ } *a;
26754+
26755+ err = -ENOMEM;
26756+ a = kmalloc(sizeof(*a), GFP_NOFS);
26757+ if (unlikely(!a))
26758+ goto out;
26759+
26760+ root = sb->s_root;
26761+ err = 0;
26762+ bindex = 0;
26763+ opt = opts->opt;
26764+ opt_tail = opt + opts->max_opt - 1;
26765+ opt->type = Opt_tail;
26766+ while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
26767+ err = -EINVAL;
26768+ skipped = 0;
26769+ token = match_token(opt_str, options, a->args);
26770+ switch (token) {
26771+ case Opt_br:
26772+ err = 0;
26773+ while (!err && (opt_str = strsep(&a->args[0].from, ":"))
26774+ && *opt_str) {
26775+ err = opt_add(opt, opt_str, opts->sb_flags,
26776+ bindex++);
26777+ if (unlikely(!err && ++opt > opt_tail)) {
26778+ err = -E2BIG;
26779+ break;
26780+ }
26781+ opt->type = Opt_tail;
26782+ skipped = 1;
26783+ }
26784+ break;
26785+ case Opt_add:
26786+ if (unlikely(match_int(&a->args[0], &n))) {
4a4d8108 26787+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26788+ break;
26789+ }
26790+ bindex = n;
26791+ err = opt_add(opt, a->args[1].from, opts->sb_flags,
26792+ bindex);
26793+ if (!err)
26794+ opt->type = token;
26795+ break;
26796+ case Opt_append:
26797+ err = opt_add(opt, a->args[0].from, opts->sb_flags,
26798+ /*dummy bindex*/1);
26799+ if (!err)
26800+ opt->type = token;
26801+ break;
26802+ case Opt_prepend:
26803+ err = opt_add(opt, a->args[0].from, opts->sb_flags,
26804+ /*bindex*/0);
26805+ if (!err)
26806+ opt->type = token;
26807+ break;
26808+ case Opt_del:
26809+ err = au_opts_parse_del(&opt->del, a->args);
26810+ if (!err)
26811+ opt->type = token;
26812+ break;
26813+#if 0 /* reserved for future use */
26814+ case Opt_idel:
26815+ del->pathname = "(indexed)";
26816+ if (unlikely(match_int(&args[0], &n))) {
4a4d8108 26817+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26818+ break;
26819+ }
26820+ err = au_opts_parse_idel(sb, n, &opt->del, a->args);
26821+ if (!err)
26822+ opt->type = token;
26823+ break;
26824+#endif
26825+ case Opt_mod:
26826+ err = au_opts_parse_mod(&opt->mod, a->args);
26827+ if (!err)
26828+ opt->type = token;
26829+ break;
26830+#ifdef IMOD /* reserved for future use */
26831+ case Opt_imod:
26832+ u.mod->path = "(indexed)";
26833+ if (unlikely(match_int(&a->args[0], &n))) {
4a4d8108 26834+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26835+ break;
26836+ }
26837+ err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
26838+ if (!err)
26839+ opt->type = token;
26840+ break;
26841+#endif
26842+ case Opt_xino:
26843+ err = au_opts_parse_xino(sb, &opt->xino, a->args);
26844+ if (!err)
26845+ opt->type = token;
26846+ break;
26847+
26848+ case Opt_trunc_xino_path:
26849+ err = au_opts_parse_xino_itrunc_path
26850+ (sb, &opt->xino_itrunc, a->args);
26851+ if (!err)
26852+ opt->type = token;
26853+ break;
26854+
26855+ case Opt_itrunc_xino:
26856+ u.xino_itrunc = &opt->xino_itrunc;
26857+ if (unlikely(match_int(&a->args[0], &n))) {
4a4d8108 26858+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26859+ break;
26860+ }
26861+ u.xino_itrunc->bindex = n;
26862+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d 26863+ if (n < 0 || au_sbbot(sb) < n) {
4a4d8108 26864+ pr_err("out of bounds, %d\n", n);
1facf9fc 26865+ aufs_read_unlock(root, !AuLock_IR);
26866+ break;
26867+ }
26868+ aufs_read_unlock(root, !AuLock_IR);
26869+ err = 0;
26870+ opt->type = token;
26871+ break;
26872+
26873+ case Opt_dirwh:
26874+ if (unlikely(match_int(&a->args[0], &opt->dirwh)))
26875+ break;
26876+ err = 0;
26877+ opt->type = token;
26878+ break;
26879+
26880+ case Opt_rdcache:
027c5e7a
AM
26881+ if (unlikely(match_int(&a->args[0], &n))) {
26882+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26883+ break;
027c5e7a
AM
26884+ }
26885+ if (unlikely(n > AUFS_RDCACHE_MAX)) {
26886+ pr_err("rdcache must be smaller than %d\n",
26887+ AUFS_RDCACHE_MAX);
26888+ break;
26889+ }
26890+ opt->rdcache = n;
1facf9fc 26891+ err = 0;
26892+ opt->type = token;
26893+ break;
26894+ case Opt_rdblk:
26895+ if (unlikely(match_int(&a->args[0], &n)
1308ab2a 26896+ || n < 0
1facf9fc 26897+ || n > KMALLOC_MAX_SIZE)) {
4a4d8108 26898+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26899+ break;
26900+ }
1308ab2a 26901+ if (unlikely(n && n < NAME_MAX)) {
4a4d8108
AM
26902+ pr_err("rdblk must be larger than %d\n",
26903+ NAME_MAX);
1facf9fc 26904+ break;
26905+ }
26906+ opt->rdblk = n;
26907+ err = 0;
26908+ opt->type = token;
26909+ break;
26910+ case Opt_rdhash:
26911+ if (unlikely(match_int(&a->args[0], &n)
1308ab2a 26912+ || n < 0
1facf9fc 26913+ || n * sizeof(struct hlist_head)
26914+ > KMALLOC_MAX_SIZE)) {
4a4d8108 26915+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26916+ break;
26917+ }
26918+ opt->rdhash = n;
26919+ err = 0;
26920+ opt->type = token;
26921+ break;
26922+
26923+ case Opt_trunc_xino:
26924+ case Opt_notrunc_xino:
26925+ case Opt_noxino:
26926+ case Opt_trunc_xib:
26927+ case Opt_notrunc_xib:
dece6358
AM
26928+ case Opt_shwh:
26929+ case Opt_noshwh:
076b876e
AM
26930+ case Opt_dirperm1:
26931+ case Opt_nodirperm1:
1facf9fc 26932+ case Opt_plink:
26933+ case Opt_noplink:
26934+ case Opt_list_plink:
4a4d8108
AM
26935+ case Opt_dio:
26936+ case Opt_nodio:
1facf9fc 26937+ case Opt_diropq_a:
26938+ case Opt_diropq_w:
26939+ case Opt_warn_perm:
26940+ case Opt_nowarn_perm:
1facf9fc 26941+ case Opt_verbose:
26942+ case Opt_noverbose:
26943+ case Opt_sum:
26944+ case Opt_nosum:
26945+ case Opt_wsum:
dece6358
AM
26946+ case Opt_rdblk_def:
26947+ case Opt_rdhash_def:
8b6a4947
AM
26948+ case Opt_dirren:
26949+ case Opt_nodirren:
c1595e42
JR
26950+ case Opt_acl:
26951+ case Opt_noacl:
1facf9fc 26952+ err = 0;
26953+ opt->type = token;
26954+ break;
26955+
26956+ case Opt_udba:
26957+ opt->udba = udba_val(a->args[0].from);
26958+ if (opt->udba >= 0) {
26959+ err = 0;
26960+ opt->type = token;
26961+ } else
4a4d8108 26962+ pr_err("wrong value, %s\n", opt_str);
1facf9fc 26963+ break;
26964+
26965+ case Opt_wbr_create:
26966+ u.create = &opt->wbr_create;
26967+ u.create->wbr_create
26968+ = au_wbr_create_val(a->args[0].from, u.create);
26969+ if (u.create->wbr_create >= 0) {
26970+ err = 0;
26971+ opt->type = token;
26972+ } else
4a4d8108 26973+ pr_err("wrong value, %s\n", opt_str);
1facf9fc 26974+ break;
26975+ case Opt_wbr_copyup:
26976+ opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
26977+ if (opt->wbr_copyup >= 0) {
26978+ err = 0;
26979+ opt->type = token;
26980+ } else
4a4d8108 26981+ pr_err("wrong value, %s\n", opt_str);
1facf9fc 26982+ break;
26983+
076b876e
AM
26984+ case Opt_fhsm_sec:
26985+ if (unlikely(match_int(&a->args[0], &n)
26986+ || n < 0)) {
26987+ pr_err("bad integer in %s\n", opt_str);
26988+ break;
26989+ }
26990+ if (sysaufs_brs) {
26991+ opt->fhsm_second = n;
26992+ opt->type = token;
26993+ } else
26994+ pr_warn("ignored %s\n", opt_str);
26995+ err = 0;
26996+ break;
26997+
1facf9fc 26998+ case Opt_ignore:
0c3ec466 26999+ pr_warn("ignored %s\n", opt_str);
1facf9fc 27000+ /*FALLTHROUGH*/
27001+ case Opt_ignore_silent:
27002+ skipped = 1;
27003+ err = 0;
27004+ break;
27005+ case Opt_err:
4a4d8108 27006+ pr_err("unknown option %s\n", opt_str);
1facf9fc 27007+ break;
27008+ }
27009+
27010+ if (!err && !skipped) {
27011+ if (unlikely(++opt > opt_tail)) {
27012+ err = -E2BIG;
27013+ opt--;
27014+ opt->type = Opt_tail;
27015+ break;
27016+ }
27017+ opt->type = Opt_tail;
27018+ }
27019+ }
27020+
e49925d1 27021+ au_kfree_rcu(a);
1facf9fc 27022+ dump_opts(opts);
27023+ if (unlikely(err))
27024+ au_opts_free(opts);
27025+
4f0767ce 27026+out:
1facf9fc 27027+ return err;
27028+}
27029+
27030+static int au_opt_wbr_create(struct super_block *sb,
27031+ struct au_opt_wbr_create *create)
27032+{
27033+ int err;
27034+ struct au_sbinfo *sbinfo;
27035+
dece6358
AM
27036+ SiMustWriteLock(sb);
27037+
1facf9fc 27038+ err = 1; /* handled */
27039+ sbinfo = au_sbi(sb);
27040+ if (sbinfo->si_wbr_create_ops->fin) {
27041+ err = sbinfo->si_wbr_create_ops->fin(sb);
27042+ if (!err)
27043+ err = 1;
27044+ }
27045+
27046+ sbinfo->si_wbr_create = create->wbr_create;
27047+ sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
27048+ switch (create->wbr_create) {
27049+ case AuWbrCreate_MFSRRV:
27050+ case AuWbrCreate_MFSRR:
f2c43d5f
AM
27051+ case AuWbrCreate_TDMFS:
27052+ case AuWbrCreate_TDMFSV:
392086de
AM
27053+ case AuWbrCreate_PMFSRR:
27054+ case AuWbrCreate_PMFSRRV:
1facf9fc 27055+ sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
27056+ /*FALLTHROUGH*/
27057+ case AuWbrCreate_MFS:
27058+ case AuWbrCreate_MFSV:
27059+ case AuWbrCreate_PMFS:
27060+ case AuWbrCreate_PMFSV:
e49829fe
JR
27061+ sbinfo->si_wbr_mfs.mfs_expire
27062+ = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
1facf9fc 27063+ break;
27064+ }
27065+
27066+ if (sbinfo->si_wbr_create_ops->init)
27067+ sbinfo->si_wbr_create_ops->init(sb); /* ignore */
27068+
27069+ return err;
27070+}
27071+
27072+/*
27073+ * returns,
27074+ * plus: processed without an error
27075+ * zero: unprocessed
27076+ */
27077+static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
27078+ struct au_opts *opts)
27079+{
27080+ int err;
27081+ struct au_sbinfo *sbinfo;
27082+
dece6358
AM
27083+ SiMustWriteLock(sb);
27084+
1facf9fc 27085+ err = 1; /* handled */
27086+ sbinfo = au_sbi(sb);
27087+ switch (opt->type) {
27088+ case Opt_udba:
27089+ sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27090+ sbinfo->si_mntflags |= opt->udba;
27091+ opts->given_udba |= opt->udba;
27092+ break;
27093+
27094+ case Opt_plink:
27095+ au_opt_set(sbinfo->si_mntflags, PLINK);
27096+ break;
27097+ case Opt_noplink:
27098+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
e49829fe 27099+ au_plink_put(sb, /*verbose*/1);
1facf9fc 27100+ au_opt_clr(sbinfo->si_mntflags, PLINK);
27101+ break;
27102+ case Opt_list_plink:
27103+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
27104+ au_plink_list(sb);
27105+ break;
27106+
4a4d8108
AM
27107+ case Opt_dio:
27108+ au_opt_set(sbinfo->si_mntflags, DIO);
27109+ au_fset_opts(opts->flags, REFRESH_DYAOP);
27110+ break;
27111+ case Opt_nodio:
27112+ au_opt_clr(sbinfo->si_mntflags, DIO);
27113+ au_fset_opts(opts->flags, REFRESH_DYAOP);
27114+ break;
27115+
076b876e
AM
27116+ case Opt_fhsm_sec:
27117+ au_fhsm_set(sbinfo, opt->fhsm_second);
27118+ break;
27119+
1facf9fc 27120+ case Opt_diropq_a:
27121+ au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27122+ break;
27123+ case Opt_diropq_w:
27124+ au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27125+ break;
27126+
27127+ case Opt_warn_perm:
27128+ au_opt_set(sbinfo->si_mntflags, WARN_PERM);
27129+ break;
27130+ case Opt_nowarn_perm:
27131+ au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
27132+ break;
27133+
1facf9fc 27134+ case Opt_verbose:
27135+ au_opt_set(sbinfo->si_mntflags, VERBOSE);
27136+ break;
27137+ case Opt_noverbose:
27138+ au_opt_clr(sbinfo->si_mntflags, VERBOSE);
27139+ break;
27140+
27141+ case Opt_sum:
27142+ au_opt_set(sbinfo->si_mntflags, SUM);
27143+ break;
27144+ case Opt_wsum:
27145+ au_opt_clr(sbinfo->si_mntflags, SUM);
27146+ au_opt_set(sbinfo->si_mntflags, SUM_W);
0b487229 27147+ break;
1facf9fc 27148+ case Opt_nosum:
27149+ au_opt_clr(sbinfo->si_mntflags, SUM);
27150+ au_opt_clr(sbinfo->si_mntflags, SUM_W);
27151+ break;
27152+
27153+ case Opt_wbr_create:
27154+ err = au_opt_wbr_create(sb, &opt->wbr_create);
27155+ break;
27156+ case Opt_wbr_copyup:
27157+ sbinfo->si_wbr_copyup = opt->wbr_copyup;
27158+ sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
27159+ break;
27160+
27161+ case Opt_dirwh:
27162+ sbinfo->si_dirwh = opt->dirwh;
27163+ break;
27164+
27165+ case Opt_rdcache:
e49829fe
JR
27166+ sbinfo->si_rdcache
27167+ = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
1facf9fc 27168+ break;
27169+ case Opt_rdblk:
27170+ sbinfo->si_rdblk = opt->rdblk;
27171+ break;
dece6358
AM
27172+ case Opt_rdblk_def:
27173+ sbinfo->si_rdblk = AUFS_RDBLK_DEF;
27174+ break;
1facf9fc 27175+ case Opt_rdhash:
27176+ sbinfo->si_rdhash = opt->rdhash;
27177+ break;
dece6358
AM
27178+ case Opt_rdhash_def:
27179+ sbinfo->si_rdhash = AUFS_RDHASH_DEF;
27180+ break;
27181+
27182+ case Opt_shwh:
27183+ au_opt_set(sbinfo->si_mntflags, SHWH);
27184+ break;
27185+ case Opt_noshwh:
27186+ au_opt_clr(sbinfo->si_mntflags, SHWH);
27187+ break;
1facf9fc 27188+
076b876e
AM
27189+ case Opt_dirperm1:
27190+ au_opt_set(sbinfo->si_mntflags, DIRPERM1);
27191+ break;
27192+ case Opt_nodirperm1:
27193+ au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
27194+ break;
27195+
1facf9fc 27196+ case Opt_trunc_xino:
27197+ au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
27198+ break;
27199+ case Opt_notrunc_xino:
27200+ au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
27201+ break;
27202+
27203+ case Opt_trunc_xino_path:
27204+ case Opt_itrunc_xino:
acd2b654
AM
27205+ err = au_xino_trunc(sb, opt->xino_itrunc.bindex,
27206+ /*idx_begin*/0);
1facf9fc 27207+ if (!err)
27208+ err = 1;
27209+ break;
27210+
27211+ case Opt_trunc_xib:
27212+ au_fset_opts(opts->flags, TRUNC_XIB);
27213+ break;
27214+ case Opt_notrunc_xib:
27215+ au_fclr_opts(opts->flags, TRUNC_XIB);
27216+ break;
27217+
8b6a4947
AM
27218+ case Opt_dirren:
27219+ err = 1;
27220+ if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27221+ err = au_dr_opt_set(sb);
27222+ if (!err)
27223+ err = 1;
27224+ }
27225+ if (err == 1)
27226+ au_opt_set(sbinfo->si_mntflags, DIRREN);
27227+ break;
27228+ case Opt_nodirren:
27229+ err = 1;
27230+ if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27231+ err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
27232+ DR_FLUSHED));
27233+ if (!err)
27234+ err = 1;
27235+ }
27236+ if (err == 1)
27237+ au_opt_clr(sbinfo->si_mntflags, DIRREN);
27238+ break;
27239+
c1595e42 27240+ case Opt_acl:
2121bcd9 27241+ sb->s_flags |= SB_POSIXACL;
c1595e42
JR
27242+ break;
27243+ case Opt_noacl:
2121bcd9 27244+ sb->s_flags &= ~SB_POSIXACL;
c1595e42
JR
27245+ break;
27246+
1facf9fc 27247+ default:
27248+ err = 0;
27249+ break;
27250+ }
27251+
27252+ return err;
27253+}
27254+
27255+/*
27256+ * returns tri-state.
27257+ * plus: processed without an error
27258+ * zero: unprocessed
27259+ * minus: error
27260+ */
27261+static int au_opt_br(struct super_block *sb, struct au_opt *opt,
27262+ struct au_opts *opts)
27263+{
27264+ int err, do_refresh;
27265+
27266+ err = 0;
27267+ switch (opt->type) {
27268+ case Opt_append:
5afbbe0d 27269+ opt->add.bindex = au_sbbot(sb) + 1;
1facf9fc 27270+ if (opt->add.bindex < 0)
27271+ opt->add.bindex = 0;
27272+ goto add;
0b487229 27273+ /* Always goto add, not fallthrough */
1facf9fc 27274+ case Opt_prepend:
27275+ opt->add.bindex = 0;
0b487229 27276+ /* fallthrough */
f6b6e03d 27277+ add: /* indented label */
1facf9fc 27278+ case Opt_add:
27279+ err = au_br_add(sb, &opt->add,
27280+ au_ftest_opts(opts->flags, REMOUNT));
27281+ if (!err) {
27282+ err = 1;
027c5e7a 27283+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 27284+ }
27285+ break;
27286+
27287+ case Opt_del:
27288+ case Opt_idel:
27289+ err = au_br_del(sb, &opt->del,
27290+ au_ftest_opts(opts->flags, REMOUNT));
27291+ if (!err) {
27292+ err = 1;
27293+ au_fset_opts(opts->flags, TRUNC_XIB);
027c5e7a 27294+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 27295+ }
27296+ break;
27297+
27298+ case Opt_mod:
27299+ case Opt_imod:
27300+ err = au_br_mod(sb, &opt->mod,
27301+ au_ftest_opts(opts->flags, REMOUNT),
27302+ &do_refresh);
27303+ if (!err) {
27304+ err = 1;
027c5e7a
AM
27305+ if (do_refresh)
27306+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 27307+ }
27308+ break;
27309+ }
1facf9fc 27310+ return err;
27311+}
27312+
27313+static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
27314+ struct au_opt_xino **opt_xino,
27315+ struct au_opts *opts)
27316+{
27317+ int err;
1facf9fc 27318+
27319+ err = 0;
27320+ switch (opt->type) {
27321+ case Opt_xino:
062440b3
AM
27322+ err = au_xino_set(sb, &opt->xino,
27323+ !!au_ftest_opts(opts->flags, REMOUNT));
27324+ if (unlikely(err))
27325+ break;
27326+
27327+ *opt_xino = &opt->xino;
1facf9fc 27328+ break;
27329+
27330+ case Opt_noxino:
27331+ au_xino_clr(sb);
1facf9fc 27332+ *opt_xino = (void *)-1;
27333+ break;
27334+ }
27335+
27336+ return err;
27337+}
27338+
27339+int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27340+ unsigned int pending)
27341+{
076b876e 27342+ int err, fhsm;
5afbbe0d 27343+ aufs_bindex_t bindex, bbot;
79b8bda9 27344+ unsigned char do_plink, skip, do_free, can_no_dreval;
1facf9fc 27345+ struct au_branch *br;
27346+ struct au_wbr *wbr;
79b8bda9 27347+ struct dentry *root, *dentry;
1facf9fc 27348+ struct inode *dir, *h_dir;
27349+ struct au_sbinfo *sbinfo;
27350+ struct au_hinode *hdir;
27351+
dece6358
AM
27352+ SiMustAnyLock(sb);
27353+
1facf9fc 27354+ sbinfo = au_sbi(sb);
27355+ AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
27356+
2121bcd9 27357+ if (!(sb_flags & SB_RDONLY)) {
dece6358 27358+ if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
0c3ec466 27359+ pr_warn("first branch should be rw\n");
dece6358 27360+ if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
febd17d6 27361+ pr_warn_once("shwh should be used with ro\n");
dece6358 27362+ }
1facf9fc 27363+
4a4d8108 27364+ if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
1facf9fc 27365+ && !au_opt_test(sbinfo->si_mntflags, XINO))
febd17d6 27366+ pr_warn_once("udba=*notify requires xino\n");
1facf9fc 27367+
076b876e 27368+ if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
febd17d6
JR
27369+ pr_warn_once("dirperm1 breaks the protection"
27370+ " by the permission bits on the lower branch\n");
076b876e 27371+
1facf9fc 27372+ err = 0;
076b876e 27373+ fhsm = 0;
1facf9fc 27374+ root = sb->s_root;
5527c038 27375+ dir = d_inode(root);
1facf9fc 27376+ do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
79b8bda9
AM
27377+ can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
27378+ UDBA_NONE);
5afbbe0d
AM
27379+ bbot = au_sbbot(sb);
27380+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
1facf9fc 27381+ skip = 0;
27382+ h_dir = au_h_iptr(dir, bindex);
27383+ br = au_sbr(sb, bindex);
1facf9fc 27384+
c1595e42
JR
27385+ if ((br->br_perm & AuBrAttr_ICEX)
27386+ && !h_dir->i_op->listxattr)
27387+ br->br_perm &= ~AuBrAttr_ICEX;
0b487229 27388+#if 0 /* untested */
c1595e42 27389+ if ((br->br_perm & AuBrAttr_ICEX_SEC)
2121bcd9 27390+ && (au_br_sb(br)->s_flags & SB_NOSEC))
c1595e42
JR
27391+ br->br_perm &= ~AuBrAttr_ICEX_SEC;
27392+#endif
27393+
27394+ do_free = 0;
1facf9fc 27395+ wbr = br->br_wbr;
27396+ if (wbr)
27397+ wbr_wh_read_lock(wbr);
27398+
1e00d052 27399+ if (!au_br_writable(br->br_perm)) {
1facf9fc 27400+ do_free = !!wbr;
27401+ skip = (!wbr
27402+ || (!wbr->wbr_whbase
27403+ && !wbr->wbr_plink
27404+ && !wbr->wbr_orph));
1e00d052 27405+ } else if (!au_br_wh_linkable(br->br_perm)) {
1facf9fc 27406+ /* skip = (!br->br_whbase && !br->br_orph); */
27407+ skip = (!wbr || !wbr->wbr_whbase);
27408+ if (skip && wbr) {
27409+ if (do_plink)
27410+ skip = !!wbr->wbr_plink;
27411+ else
27412+ skip = !wbr->wbr_plink;
27413+ }
1e00d052 27414+ } else {
1facf9fc 27415+ /* skip = (br->br_whbase && br->br_ohph); */
27416+ skip = (wbr && wbr->wbr_whbase);
27417+ if (skip) {
27418+ if (do_plink)
27419+ skip = !!wbr->wbr_plink;
27420+ else
27421+ skip = !wbr->wbr_plink;
27422+ }
1facf9fc 27423+ }
27424+ if (wbr)
27425+ wbr_wh_read_unlock(wbr);
27426+
79b8bda9
AM
27427+ if (can_no_dreval) {
27428+ dentry = br->br_path.dentry;
27429+ spin_lock(&dentry->d_lock);
27430+ if (dentry->d_flags &
27431+ (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
27432+ can_no_dreval = 0;
27433+ spin_unlock(&dentry->d_lock);
27434+ }
27435+
076b876e
AM
27436+ if (au_br_fhsm(br->br_perm)) {
27437+ fhsm++;
27438+ AuDebugOn(!br->br_fhsm);
27439+ }
27440+
1facf9fc 27441+ if (skip)
27442+ continue;
27443+
27444+ hdir = au_hi(dir, bindex);
5afbbe0d 27445+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 27446+ if (wbr)
27447+ wbr_wh_write_lock(wbr);
86dc4139 27448+ err = au_wh_init(br, sb);
1facf9fc 27449+ if (wbr)
27450+ wbr_wh_write_unlock(wbr);
5afbbe0d 27451+ au_hn_inode_unlock(hdir);
1facf9fc 27452+
27453+ if (!err && do_free) {
e49925d1 27454+ au_kfree_rcu(wbr);
1facf9fc 27455+ br->br_wbr = NULL;
27456+ }
27457+ }
27458+
79b8bda9
AM
27459+ if (can_no_dreval)
27460+ au_fset_si(sbinfo, NO_DREVAL);
27461+ else
27462+ au_fclr_si(sbinfo, NO_DREVAL);
27463+
c1595e42 27464+ if (fhsm >= 2) {
076b876e 27465+ au_fset_si(sbinfo, FHSM);
5afbbe0d 27466+ for (bindex = bbot; bindex >= 0; bindex--) {
c1595e42
JR
27467+ br = au_sbr(sb, bindex);
27468+ if (au_br_fhsm(br->br_perm)) {
27469+ au_fhsm_set_bottom(sb, bindex);
27470+ break;
27471+ }
27472+ }
27473+ } else {
076b876e 27474+ au_fclr_si(sbinfo, FHSM);
c1595e42
JR
27475+ au_fhsm_set_bottom(sb, -1);
27476+ }
076b876e 27477+
1facf9fc 27478+ return err;
27479+}
27480+
27481+int au_opts_mount(struct super_block *sb, struct au_opts *opts)
27482+{
27483+ int err;
27484+ unsigned int tmp;
5afbbe0d 27485+ aufs_bindex_t bindex, bbot;
1facf9fc 27486+ struct au_opt *opt;
27487+ struct au_opt_xino *opt_xino, xino;
27488+ struct au_sbinfo *sbinfo;
027c5e7a 27489+ struct au_branch *br;
076b876e 27490+ struct inode *dir;
1facf9fc 27491+
dece6358
AM
27492+ SiMustWriteLock(sb);
27493+
1facf9fc 27494+ err = 0;
27495+ opt_xino = NULL;
27496+ opt = opts->opt;
27497+ while (err >= 0 && opt->type != Opt_tail)
27498+ err = au_opt_simple(sb, opt++, opts);
27499+ if (err > 0)
27500+ err = 0;
27501+ else if (unlikely(err < 0))
27502+ goto out;
27503+
27504+ /* disable xino and udba temporary */
27505+ sbinfo = au_sbi(sb);
27506+ tmp = sbinfo->si_mntflags;
27507+ au_opt_clr(sbinfo->si_mntflags, XINO);
27508+ au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
27509+
27510+ opt = opts->opt;
27511+ while (err >= 0 && opt->type != Opt_tail)
27512+ err = au_opt_br(sb, opt++, opts);
27513+ if (err > 0)
27514+ err = 0;
27515+ else if (unlikely(err < 0))
27516+ goto out;
27517+
5afbbe0d
AM
27518+ bbot = au_sbbot(sb);
27519+ if (unlikely(bbot < 0)) {
1facf9fc 27520+ err = -EINVAL;
4a4d8108 27521+ pr_err("no branches\n");
1facf9fc 27522+ goto out;
27523+ }
27524+
27525+ if (au_opt_test(tmp, XINO))
27526+ au_opt_set(sbinfo->si_mntflags, XINO);
27527+ opt = opts->opt;
27528+ while (!err && opt->type != Opt_tail)
27529+ err = au_opt_xino(sb, opt++, &opt_xino, opts);
27530+ if (unlikely(err))
27531+ goto out;
27532+
27533+ err = au_opts_verify(sb, sb->s_flags, tmp);
27534+ if (unlikely(err))
27535+ goto out;
27536+
27537+ /* restore xino */
27538+ if (au_opt_test(tmp, XINO) && !opt_xino) {
27539+ xino.file = au_xino_def(sb);
27540+ err = PTR_ERR(xino.file);
27541+ if (IS_ERR(xino.file))
27542+ goto out;
27543+
27544+ err = au_xino_set(sb, &xino, /*remount*/0);
27545+ fput(xino.file);
27546+ if (unlikely(err))
27547+ goto out;
27548+ }
27549+
27550+ /* restore udba */
027c5e7a 27551+ tmp &= AuOptMask_UDBA;
1facf9fc 27552+ sbinfo->si_mntflags &= ~AuOptMask_UDBA;
027c5e7a 27553+ sbinfo->si_mntflags |= tmp;
5afbbe0d
AM
27554+ bbot = au_sbbot(sb);
27555+ for (bindex = 0; bindex <= bbot; bindex++) {
027c5e7a
AM
27556+ br = au_sbr(sb, bindex);
27557+ err = au_hnotify_reset_br(tmp, br, br->br_perm);
27558+ if (unlikely(err))
27559+ AuIOErr("hnotify failed on br %d, %d, ignored\n",
27560+ bindex, err);
27561+ /* go on even if err */
27562+ }
4a4d8108 27563+ if (au_opt_test(tmp, UDBA_HNOTIFY)) {
5527c038 27564+ dir = d_inode(sb->s_root);
4a4d8108 27565+ au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
1facf9fc 27566+ }
27567+
4f0767ce 27568+out:
1facf9fc 27569+ return err;
27570+}
27571+
27572+int au_opts_remount(struct super_block *sb, struct au_opts *opts)
27573+{
27574+ int err, rerr;
79b8bda9 27575+ unsigned char no_dreval;
1facf9fc 27576+ struct inode *dir;
27577+ struct au_opt_xino *opt_xino;
27578+ struct au_opt *opt;
27579+ struct au_sbinfo *sbinfo;
27580+
dece6358
AM
27581+ SiMustWriteLock(sb);
27582+
8b6a4947
AM
27583+ err = au_dr_opt_flush(sb);
27584+ if (unlikely(err))
27585+ goto out;
27586+ au_fset_opts(opts->flags, DR_FLUSHED);
27587+
5527c038 27588+ dir = d_inode(sb->s_root);
1facf9fc 27589+ sbinfo = au_sbi(sb);
1facf9fc 27590+ opt_xino = NULL;
27591+ opt = opts->opt;
27592+ while (err >= 0 && opt->type != Opt_tail) {
27593+ err = au_opt_simple(sb, opt, opts);
27594+ if (!err)
27595+ err = au_opt_br(sb, opt, opts);
27596+ if (!err)
27597+ err = au_opt_xino(sb, opt, &opt_xino, opts);
27598+ opt++;
27599+ }
27600+ if (err > 0)
27601+ err = 0;
27602+ AuTraceErr(err);
27603+ /* go on even err */
27604+
79b8bda9 27605+ no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
1facf9fc 27606+ rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
27607+ if (unlikely(rerr && !err))
27608+ err = rerr;
27609+
79b8bda9 27610+ if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
b95c5147 27611+ au_fset_opts(opts->flags, REFRESH_IDOP);
79b8bda9 27612+
1facf9fc 27613+ if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
27614+ rerr = au_xib_trunc(sb);
27615+ if (unlikely(rerr && !err))
27616+ err = rerr;
27617+ }
27618+
27619+ /* will be handled by the caller */
027c5e7a 27620+ if (!au_ftest_opts(opts->flags, REFRESH)
79b8bda9
AM
27621+ && (opts->given_udba
27622+ || au_opt_test(sbinfo->si_mntflags, XINO)
b95c5147 27623+ || au_ftest_opts(opts->flags, REFRESH_IDOP)
79b8bda9 27624+ ))
027c5e7a 27625+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 27626+
27627+ AuDbg("status 0x%x\n", opts->flags);
8b6a4947
AM
27628+
27629+out:
1facf9fc 27630+ return err;
27631+}
27632+
27633+/* ---------------------------------------------------------------------- */
27634+
27635+unsigned int au_opt_udba(struct super_block *sb)
27636+{
27637+ return au_mntflags(sb) & AuOptMask_UDBA;
27638+}
7f207e10
AM
27639diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
27640--- /usr/share/empty/fs/aufs/opts.h 1970-01-01 01:00:00.000000000 +0100
0b487229 27641+++ linux/fs/aufs/opts.h 2021-02-22 23:30:37.702680911 +0100
062440b3
AM
27642@@ -0,0 +1,225 @@
27643+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 27644+/*
0b487229 27645+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 27646+ *
27647+ * This program, aufs is free software; you can redistribute it and/or modify
27648+ * it under the terms of the GNU General Public License as published by
27649+ * the Free Software Foundation; either version 2 of the License, or
27650+ * (at your option) any later version.
dece6358
AM
27651+ *
27652+ * This program is distributed in the hope that it will be useful,
27653+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
27654+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27655+ * GNU General Public License for more details.
27656+ *
27657+ * You should have received a copy of the GNU General Public License
523b37e3 27658+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 27659+ */
27660+
27661+/*
27662+ * mount options/flags
27663+ */
27664+
27665+#ifndef __AUFS_OPTS_H__
27666+#define __AUFS_OPTS_H__
27667+
27668+#ifdef __KERNEL__
27669+
dece6358 27670+#include <linux/path.h>
1facf9fc 27671+
dece6358 27672+struct file;
dece6358 27673+
1facf9fc 27674+/* ---------------------------------------------------------------------- */
27675+
27676+/* mount flags */
27677+#define AuOpt_XINO 1 /* external inode number bitmap
27678+ and translation table */
27679+#define AuOpt_TRUNC_XINO (1 << 1) /* truncate xino files */
27680+#define AuOpt_UDBA_NONE (1 << 2) /* users direct branch access */
27681+#define AuOpt_UDBA_REVAL (1 << 3)
4a4d8108 27682+#define AuOpt_UDBA_HNOTIFY (1 << 4)
dece6358
AM
27683+#define AuOpt_SHWH (1 << 5) /* show whiteout */
27684+#define AuOpt_PLINK (1 << 6) /* pseudo-link */
076b876e
AM
27685+#define AuOpt_DIRPERM1 (1 << 7) /* ignore the lower dir's perm
27686+ bits */
dece6358
AM
27687+#define AuOpt_ALWAYS_DIROPQ (1 << 9) /* policy to creating diropq */
27688+#define AuOpt_SUM (1 << 10) /* summation for statfs(2) */
27689+#define AuOpt_SUM_W (1 << 11) /* unimplemented */
27690+#define AuOpt_WARN_PERM (1 << 12) /* warn when add-branch */
0b487229 27691+#define AuOpt_VERBOSE (1 << 13) /* print the cause of error */
4a4d8108 27692+#define AuOpt_DIO (1 << 14) /* direct io */
8b6a4947 27693+#define AuOpt_DIRREN (1 << 15) /* directory rename */
1facf9fc 27694+
4a4d8108
AM
27695+#ifndef CONFIG_AUFS_HNOTIFY
27696+#undef AuOpt_UDBA_HNOTIFY
27697+#define AuOpt_UDBA_HNOTIFY 0
1facf9fc 27698+#endif
8b6a4947
AM
27699+#ifndef CONFIG_AUFS_DIRREN
27700+#undef AuOpt_DIRREN
27701+#define AuOpt_DIRREN 0
27702+#endif
dece6358
AM
27703+#ifndef CONFIG_AUFS_SHWH
27704+#undef AuOpt_SHWH
27705+#define AuOpt_SHWH 0
27706+#endif
1facf9fc 27707+
27708+#define AuOpt_Def (AuOpt_XINO \
27709+ | AuOpt_UDBA_REVAL \
27710+ | AuOpt_PLINK \
27711+ /* | AuOpt_DIRPERM1 */ \
27712+ | AuOpt_WARN_PERM)
27713+#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
27714+ | AuOpt_UDBA_REVAL \
4a4d8108 27715+ | AuOpt_UDBA_HNOTIFY)
1facf9fc 27716+
27717+#define au_opt_test(flags, name) (flags & AuOpt_##name)
27718+#define au_opt_set(flags, name) do { \
27719+ BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
27720+ ((flags) |= AuOpt_##name); \
27721+} while (0)
27722+#define au_opt_set_udba(flags, name) do { \
27723+ (flags) &= ~AuOptMask_UDBA; \
27724+ ((flags) |= AuOpt_##name); \
27725+} while (0)
7f207e10
AM
27726+#define au_opt_clr(flags, name) do { \
27727+ ((flags) &= ~AuOpt_##name); \
27728+} while (0)
1facf9fc 27729+
e49829fe
JR
27730+static inline unsigned int au_opts_plink(unsigned int mntflags)
27731+{
27732+#ifdef CONFIG_PROC_FS
27733+ return mntflags;
27734+#else
27735+ return mntflags & ~AuOpt_PLINK;
27736+#endif
27737+}
27738+
1facf9fc 27739+/* ---------------------------------------------------------------------- */
27740+
27741+/* policies to select one among multiple writable branches */
27742+enum {
27743+ AuWbrCreate_TDP, /* top down parent */
27744+ AuWbrCreate_RR, /* round robin */
27745+ AuWbrCreate_MFS, /* most free space */
27746+ AuWbrCreate_MFSV, /* mfs with seconds */
27747+ AuWbrCreate_MFSRR, /* mfs then rr */
27748+ AuWbrCreate_MFSRRV, /* mfs then rr with seconds */
f2c43d5f
AM
27749+ AuWbrCreate_TDMFS, /* top down regardless parent and mfs */
27750+ AuWbrCreate_TDMFSV, /* top down regardless parent and mfs */
1facf9fc 27751+ AuWbrCreate_PMFS, /* parent and mfs */
27752+ AuWbrCreate_PMFSV, /* parent and mfs with seconds */
392086de
AM
27753+ AuWbrCreate_PMFSRR, /* parent, mfs and round-robin */
27754+ AuWbrCreate_PMFSRRV, /* plus seconds */
1facf9fc 27755+
27756+ AuWbrCreate_Def = AuWbrCreate_TDP
27757+};
27758+
27759+enum {
27760+ AuWbrCopyup_TDP, /* top down parent */
27761+ AuWbrCopyup_BUP, /* bottom up parent */
27762+ AuWbrCopyup_BU, /* bottom up */
27763+
27764+ AuWbrCopyup_Def = AuWbrCopyup_TDP
27765+};
27766+
27767+/* ---------------------------------------------------------------------- */
27768+
27769+struct au_opt_add {
27770+ aufs_bindex_t bindex;
27771+ char *pathname;
27772+ int perm;
27773+ struct path path;
27774+};
27775+
27776+struct au_opt_del {
27777+ char *pathname;
27778+ struct path h_path;
27779+};
27780+
27781+struct au_opt_mod {
27782+ char *path;
27783+ int perm;
27784+ struct dentry *h_root;
27785+};
27786+
27787+struct au_opt_xino {
27788+ char *path;
27789+ struct file *file;
27790+};
27791+
27792+struct au_opt_xino_itrunc {
27793+ aufs_bindex_t bindex;
27794+};
27795+
27796+struct au_opt_wbr_create {
27797+ int wbr_create;
27798+ int mfs_second;
27799+ unsigned long long mfsrr_watermark;
27800+};
27801+
27802+struct au_opt {
27803+ int type;
27804+ union {
27805+ struct au_opt_xino xino;
27806+ struct au_opt_xino_itrunc xino_itrunc;
27807+ struct au_opt_add add;
27808+ struct au_opt_del del;
27809+ struct au_opt_mod mod;
27810+ int dirwh;
27811+ int rdcache;
27812+ unsigned int rdblk;
27813+ unsigned int rdhash;
27814+ int udba;
27815+ struct au_opt_wbr_create wbr_create;
27816+ int wbr_copyup;
076b876e 27817+ unsigned int fhsm_second;
1facf9fc 27818+ };
27819+};
27820+
27821+/* opts flags */
27822+#define AuOpts_REMOUNT 1
027c5e7a
AM
27823+#define AuOpts_REFRESH (1 << 1)
27824+#define AuOpts_TRUNC_XIB (1 << 2)
27825+#define AuOpts_REFRESH_DYAOP (1 << 3)
b95c5147 27826+#define AuOpts_REFRESH_IDOP (1 << 4)
8b6a4947 27827+#define AuOpts_DR_FLUSHED (1 << 5)
1facf9fc 27828+#define au_ftest_opts(flags, name) ((flags) & AuOpts_##name)
7f207e10
AM
27829+#define au_fset_opts(flags, name) \
27830+ do { (flags) |= AuOpts_##name; } while (0)
27831+#define au_fclr_opts(flags, name) \
27832+ do { (flags) &= ~AuOpts_##name; } while (0)
1facf9fc 27833+
8b6a4947
AM
27834+#ifndef CONFIG_AUFS_DIRREN
27835+#undef AuOpts_DR_FLUSHED
27836+#define AuOpts_DR_FLUSHED 0
27837+#endif
27838+
1facf9fc 27839+struct au_opts {
27840+ struct au_opt *opt;
27841+ int max_opt;
27842+
27843+ unsigned int given_udba;
27844+ unsigned int flags;
27845+ unsigned long sb_flags;
27846+};
27847+
27848+/* ---------------------------------------------------------------------- */
27849+
7e9cd9fe 27850+/* opts.c */
076b876e 27851+void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
1facf9fc 27852+const char *au_optstr_udba(int udba);
27853+const char *au_optstr_wbr_copyup(int wbr_copyup);
27854+const char *au_optstr_wbr_create(int wbr_create);
27855+
27856+void au_opts_free(struct au_opts *opts);
3c1bdaff 27857+struct super_block;
1facf9fc 27858+int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
27859+int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27860+ unsigned int pending);
27861+int au_opts_mount(struct super_block *sb, struct au_opts *opts);
27862+int au_opts_remount(struct super_block *sb, struct au_opts *opts);
27863+
27864+unsigned int au_opt_udba(struct super_block *sb);
27865+
1facf9fc 27866+#endif /* __KERNEL__ */
27867+#endif /* __AUFS_OPTS_H__ */
7f207e10
AM
27868diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
27869--- /usr/share/empty/fs/aufs/plink.c 1970-01-01 01:00:00.000000000 +0100
0b487229 27870+++ linux/fs/aufs/plink.c 2021-02-22 23:30:37.702680911 +0100
062440b3 27871@@ -0,0 +1,516 @@
cd7a4cd9 27872+// SPDX-License-Identifier: GPL-2.0
1facf9fc 27873+/*
0b487229 27874+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 27875+ *
27876+ * This program, aufs is free software; you can redistribute it and/or modify
27877+ * it under the terms of the GNU General Public License as published by
27878+ * the Free Software Foundation; either version 2 of the License, or
27879+ * (at your option) any later version.
dece6358
AM
27880+ *
27881+ * This program is distributed in the hope that it will be useful,
27882+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
27883+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27884+ * GNU General Public License for more details.
27885+ *
27886+ * You should have received a copy of the GNU General Public License
523b37e3 27887+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 27888+ */
27889+
27890+/*
27891+ * pseudo-link
27892+ */
27893+
27894+#include "aufs.h"
27895+
27896+/*
e49829fe 27897+ * the pseudo-link maintenance mode.
1facf9fc 27898+ * during a user process maintains the pseudo-links,
27899+ * prohibit adding a new plink and branch manipulation.
e49829fe
JR
27900+ *
27901+ * Flags
27902+ * NOPLM:
27903+ * For entry functions which will handle plink, and i_mutex is already held
27904+ * in VFS.
27905+ * They cannot wait and should return an error at once.
27906+ * Callers has to check the error.
27907+ * NOPLMW:
27908+ * For entry functions which will handle plink, but i_mutex is not held
27909+ * in VFS.
27910+ * They can wait the plink maintenance mode to finish.
27911+ *
27912+ * They behave like F_SETLK and F_SETLKW.
27913+ * If the caller never handle plink, then both flags are unnecessary.
1facf9fc 27914+ */
e49829fe
JR
27915+
27916+int au_plink_maint(struct super_block *sb, int flags)
1facf9fc 27917+{
e49829fe
JR
27918+ int err;
27919+ pid_t pid, ppid;
f0c0a007 27920+ struct task_struct *parent, *prev;
e49829fe 27921+ struct au_sbinfo *sbi;
dece6358
AM
27922+
27923+ SiMustAnyLock(sb);
27924+
e49829fe
JR
27925+ err = 0;
27926+ if (!au_opt_test(au_mntflags(sb), PLINK))
27927+ goto out;
27928+
27929+ sbi = au_sbi(sb);
27930+ pid = sbi->si_plink_maint_pid;
27931+ if (!pid || pid == current->pid)
27932+ goto out;
27933+
27934+ /* todo: it highly depends upon /sbin/mount.aufs */
f0c0a007
AM
27935+ prev = NULL;
27936+ parent = current;
27937+ ppid = 0;
e49829fe 27938+ rcu_read_lock();
f0c0a007
AM
27939+ while (1) {
27940+ parent = rcu_dereference(parent->real_parent);
27941+ if (parent == prev)
27942+ break;
27943+ ppid = task_pid_vnr(parent);
27944+ if (pid == ppid) {
27945+ rcu_read_unlock();
27946+ goto out;
27947+ }
27948+ prev = parent;
27949+ }
e49829fe 27950+ rcu_read_unlock();
e49829fe
JR
27951+
27952+ if (au_ftest_lock(flags, NOPLMW)) {
027c5e7a
AM
27953+ /* if there is no i_mutex lock in VFS, we don't need to wait */
27954+ /* AuDebugOn(!lockdep_depth(current)); */
e49829fe
JR
27955+ while (sbi->si_plink_maint_pid) {
27956+ si_read_unlock(sb);
27957+ /* gave up wake_up_bit() */
27958+ wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
27959+
27960+ if (au_ftest_lock(flags, FLUSH))
27961+ au_nwt_flush(&sbi->si_nowait);
27962+ si_noflush_read_lock(sb);
27963+ }
27964+ } else if (au_ftest_lock(flags, NOPLM)) {
27965+ AuDbg("ppid %d, pid %d\n", ppid, pid);
27966+ err = -EAGAIN;
27967+ }
27968+
27969+out:
27970+ return err;
4a4d8108
AM
27971+}
27972+
e49829fe 27973+void au_plink_maint_leave(struct au_sbinfo *sbinfo)
4a4d8108 27974+{
4a4d8108 27975+ spin_lock(&sbinfo->si_plink_maint_lock);
027c5e7a 27976+ sbinfo->si_plink_maint_pid = 0;
4a4d8108 27977+ spin_unlock(&sbinfo->si_plink_maint_lock);
027c5e7a 27978+ wake_up_all(&sbinfo->si_plink_wq);
4a4d8108
AM
27979+}
27980+
e49829fe 27981+int au_plink_maint_enter(struct super_block *sb)
4a4d8108
AM
27982+{
27983+ int err;
4a4d8108
AM
27984+ struct au_sbinfo *sbinfo;
27985+
27986+ err = 0;
4a4d8108
AM
27987+ sbinfo = au_sbi(sb);
27988+ /* make sure i am the only one in this fs */
e49829fe
JR
27989+ si_write_lock(sb, AuLock_FLUSH);
27990+ if (au_opt_test(au_mntflags(sb), PLINK)) {
27991+ spin_lock(&sbinfo->si_plink_maint_lock);
27992+ if (!sbinfo->si_plink_maint_pid)
27993+ sbinfo->si_plink_maint_pid = current->pid;
27994+ else
27995+ err = -EBUSY;
27996+ spin_unlock(&sbinfo->si_plink_maint_lock);
27997+ }
4a4d8108
AM
27998+ si_write_unlock(sb);
27999+
28000+ return err;
1facf9fc 28001+}
28002+
28003+/* ---------------------------------------------------------------------- */
28004+
1facf9fc 28005+#ifdef CONFIG_AUFS_DEBUG
28006+void au_plink_list(struct super_block *sb)
28007+{
86dc4139 28008+ int i;
1facf9fc 28009+ struct au_sbinfo *sbinfo;
8b6a4947
AM
28010+ struct hlist_bl_head *hbl;
28011+ struct hlist_bl_node *pos;
5afbbe0d 28012+ struct au_icntnr *icntnr;
1facf9fc 28013+
dece6358
AM
28014+ SiMustAnyLock(sb);
28015+
1facf9fc 28016+ sbinfo = au_sbi(sb);
28017+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 28018+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 28019+
86dc4139 28020+ for (i = 0; i < AuPlink_NHASH; i++) {
8b6a4947
AM
28021+ hbl = sbinfo->si_plink + i;
28022+ hlist_bl_lock(hbl);
28023+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
5afbbe0d 28024+ AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
8b6a4947 28025+ hlist_bl_unlock(hbl);
86dc4139 28026+ }
1facf9fc 28027+}
28028+#endif
28029+
28030+/* is the inode pseudo-linked? */
28031+int au_plink_test(struct inode *inode)
28032+{
86dc4139 28033+ int found, i;
1facf9fc 28034+ struct au_sbinfo *sbinfo;
8b6a4947
AM
28035+ struct hlist_bl_head *hbl;
28036+ struct hlist_bl_node *pos;
5afbbe0d 28037+ struct au_icntnr *icntnr;
1facf9fc 28038+
28039+ sbinfo = au_sbi(inode->i_sb);
dece6358 28040+ AuRwMustAnyLock(&sbinfo->si_rwsem);
1facf9fc 28041+ AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
e49829fe 28042+ AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
1facf9fc 28043+
28044+ found = 0;
86dc4139 28045+ i = au_plink_hash(inode->i_ino);
8b6a4947
AM
28046+ hbl = sbinfo->si_plink + i;
28047+ hlist_bl_lock(hbl);
28048+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
5afbbe0d 28049+ if (&icntnr->vfs_inode == inode) {
1facf9fc 28050+ found = 1;
28051+ break;
28052+ }
8b6a4947 28053+ hlist_bl_unlock(hbl);
1facf9fc 28054+ return found;
28055+}
28056+
28057+/* ---------------------------------------------------------------------- */
28058+
28059+/*
28060+ * generate a name for plink.
28061+ * the file will be stored under AUFS_WH_PLINKDIR.
28062+ */
28063+/* 20 is max digits length of ulong 64 */
28064+#define PLINK_NAME_LEN ((20 + 1) * 2)
28065+
28066+static int plink_name(char *name, int len, struct inode *inode,
28067+ aufs_bindex_t bindex)
28068+{
28069+ int rlen;
28070+ struct inode *h_inode;
28071+
28072+ h_inode = au_h_iptr(inode, bindex);
28073+ rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
28074+ return rlen;
28075+}
28076+
7f207e10
AM
28077+struct au_do_plink_lkup_args {
28078+ struct dentry **errp;
28079+ struct qstr *tgtname;
28080+ struct dentry *h_parent;
28081+ struct au_branch *br;
28082+};
28083+
28084+static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
28085+ struct dentry *h_parent,
28086+ struct au_branch *br)
28087+{
28088+ struct dentry *h_dentry;
febd17d6 28089+ struct inode *h_inode;
7f207e10 28090+
febd17d6 28091+ h_inode = d_inode(h_parent);
be118d29 28092+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD2);
b4510431 28093+ h_dentry = vfsub_lkup_one(tgtname, h_parent);
3c1bdaff 28094+ inode_unlock_shared(h_inode);
7f207e10
AM
28095+ return h_dentry;
28096+}
28097+
28098+static void au_call_do_plink_lkup(void *args)
28099+{
28100+ struct au_do_plink_lkup_args *a = args;
28101+ *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
28102+}
28103+
1facf9fc 28104+/* lookup the plink-ed @inode under the branch at @bindex */
28105+struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
28106+{
28107+ struct dentry *h_dentry, *h_parent;
28108+ struct au_branch *br;
7f207e10 28109+ int wkq_err;
1facf9fc 28110+ char a[PLINK_NAME_LEN];
0c3ec466 28111+ struct qstr tgtname = QSTR_INIT(a, 0);
1facf9fc 28112+
e49829fe
JR
28113+ AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
28114+
1facf9fc 28115+ br = au_sbr(inode->i_sb, bindex);
28116+ h_parent = br->br_wbr->wbr_plink;
1facf9fc 28117+ tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28118+
2dfbb274 28119+ if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
7f207e10
AM
28120+ struct au_do_plink_lkup_args args = {
28121+ .errp = &h_dentry,
28122+ .tgtname = &tgtname,
28123+ .h_parent = h_parent,
28124+ .br = br
28125+ };
28126+
28127+ wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
28128+ if (unlikely(wkq_err))
28129+ h_dentry = ERR_PTR(wkq_err);
28130+ } else
28131+ h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
28132+
1facf9fc 28133+ return h_dentry;
28134+}
28135+
28136+/* create a pseudo-link */
28137+static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
28138+ struct dentry *h_dentry, struct au_branch *br)
28139+{
28140+ int err;
28141+ struct path h_path = {
86dc4139 28142+ .mnt = au_br_mnt(br)
1facf9fc 28143+ };
523b37e3 28144+ struct inode *h_dir, *delegated;
1facf9fc 28145+
5527c038 28146+ h_dir = d_inode(h_parent);
febd17d6 28147+ inode_lock_nested(h_dir, AuLsc_I_CHILD2);
4f0767ce 28148+again:
b4510431 28149+ h_path.dentry = vfsub_lkup_one(tgt, h_parent);
1facf9fc 28150+ err = PTR_ERR(h_path.dentry);
28151+ if (IS_ERR(h_path.dentry))
28152+ goto out;
28153+
28154+ err = 0;
28155+ /* wh.plink dir is not monitored */
7f207e10 28156+ /* todo: is it really safe? */
5527c038
JR
28157+ if (d_is_positive(h_path.dentry)
28158+ && d_inode(h_path.dentry) != d_inode(h_dentry)) {
523b37e3
AM
28159+ delegated = NULL;
28160+ err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
28161+ if (unlikely(err == -EWOULDBLOCK)) {
28162+ pr_warn("cannot retry for NFSv4 delegation"
28163+ " for an internal unlink\n");
28164+ iput(delegated);
28165+ }
1facf9fc 28166+ dput(h_path.dentry);
28167+ h_path.dentry = NULL;
28168+ if (!err)
28169+ goto again;
28170+ }
5527c038 28171+ if (!err && d_is_negative(h_path.dentry)) {
523b37e3
AM
28172+ delegated = NULL;
28173+ err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
28174+ if (unlikely(err == -EWOULDBLOCK)) {
28175+ pr_warn("cannot retry for NFSv4 delegation"
28176+ " for an internal link\n");
28177+ iput(delegated);
28178+ }
28179+ }
1facf9fc 28180+ dput(h_path.dentry);
28181+
4f0767ce 28182+out:
febd17d6 28183+ inode_unlock(h_dir);
1facf9fc 28184+ return err;
28185+}
28186+
28187+struct do_whplink_args {
28188+ int *errp;
28189+ struct qstr *tgt;
28190+ struct dentry *h_parent;
28191+ struct dentry *h_dentry;
28192+ struct au_branch *br;
28193+};
28194+
28195+static void call_do_whplink(void *args)
28196+{
28197+ struct do_whplink_args *a = args;
28198+ *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
28199+}
28200+
28201+static int whplink(struct dentry *h_dentry, struct inode *inode,
28202+ aufs_bindex_t bindex, struct au_branch *br)
28203+{
28204+ int err, wkq_err;
28205+ struct au_wbr *wbr;
28206+ struct dentry *h_parent;
1facf9fc 28207+ char a[PLINK_NAME_LEN];
0c3ec466 28208+ struct qstr tgtname = QSTR_INIT(a, 0);
1facf9fc 28209+
28210+ wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
28211+ h_parent = wbr->wbr_plink;
1facf9fc 28212+ tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28213+
28214+ /* always superio. */
2dfbb274 28215+ if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
1facf9fc 28216+ struct do_whplink_args args = {
28217+ .errp = &err,
28218+ .tgt = &tgtname,
28219+ .h_parent = h_parent,
28220+ .h_dentry = h_dentry,
28221+ .br = br
28222+ };
28223+ wkq_err = au_wkq_wait(call_do_whplink, &args);
28224+ if (unlikely(wkq_err))
28225+ err = wkq_err;
28226+ } else
28227+ err = do_whplink(&tgtname, h_parent, h_dentry, br);
1facf9fc 28228+
28229+ return err;
28230+}
28231+
1facf9fc 28232+/*
28233+ * create a new pseudo-link for @h_dentry on @bindex.
28234+ * the linked inode is held in aufs @inode.
28235+ */
28236+void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
28237+ struct dentry *h_dentry)
28238+{
28239+ struct super_block *sb;
28240+ struct au_sbinfo *sbinfo;
8b6a4947
AM
28241+ struct hlist_bl_head *hbl;
28242+ struct hlist_bl_node *pos;
5afbbe0d 28243+ struct au_icntnr *icntnr;
86dc4139 28244+ int found, err, cnt, i;
1facf9fc 28245+
28246+ sb = inode->i_sb;
28247+ sbinfo = au_sbi(sb);
28248+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 28249+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 28250+
86dc4139 28251+ found = au_plink_test(inode);
4a4d8108 28252+ if (found)
1facf9fc 28253+ return;
4a4d8108 28254+
86dc4139 28255+ i = au_plink_hash(inode->i_ino);
8b6a4947 28256+ hbl = sbinfo->si_plink + i;
5afbbe0d 28257+ au_igrab(inode);
1facf9fc 28258+
8b6a4947
AM
28259+ hlist_bl_lock(hbl);
28260+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink) {
5afbbe0d 28261+ if (&icntnr->vfs_inode == inode) {
4a4d8108
AM
28262+ found = 1;
28263+ break;
28264+ }
1facf9fc 28265+ }
5afbbe0d
AM
28266+ if (!found) {
28267+ icntnr = container_of(inode, struct au_icntnr, vfs_inode);
8b6a4947 28268+ hlist_bl_add_head(&icntnr->plink, hbl);
5afbbe0d 28269+ }
8b6a4947 28270+ hlist_bl_unlock(hbl);
4a4d8108 28271+ if (!found) {
8b6a4947 28272+ cnt = au_hbl_count(hbl);
acd2b654 28273+#define msg "unexpectedly unbalanced or too many pseudo-links"
86dc4139
AM
28274+ if (cnt > AUFS_PLINK_WARN)
28275+ AuWarn1(msg ", %d\n", cnt);
28276+#undef msg
1facf9fc 28277+ err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
5afbbe0d
AM
28278+ if (unlikely(err)) {
28279+ pr_warn("err %d, damaged pseudo link.\n", err);
8b6a4947 28280+ au_hbl_del(&icntnr->plink, hbl);
5afbbe0d 28281+ iput(&icntnr->vfs_inode);
4a4d8108 28282+ }
5afbbe0d
AM
28283+ } else
28284+ iput(&icntnr->vfs_inode);
1facf9fc 28285+}
28286+
28287+/* free all plinks */
e49829fe 28288+void au_plink_put(struct super_block *sb, int verbose)
1facf9fc 28289+{
86dc4139 28290+ int i, warned;
1facf9fc 28291+ struct au_sbinfo *sbinfo;
8b6a4947
AM
28292+ struct hlist_bl_head *hbl;
28293+ struct hlist_bl_node *pos, *tmp;
5afbbe0d 28294+ struct au_icntnr *icntnr;
1facf9fc 28295+
dece6358
AM
28296+ SiMustWriteLock(sb);
28297+
1facf9fc 28298+ sbinfo = au_sbi(sb);
28299+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 28300+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 28301+
1facf9fc 28302+ /* no spin_lock since sbinfo is write-locked */
86dc4139
AM
28303+ warned = 0;
28304+ for (i = 0; i < AuPlink_NHASH; i++) {
8b6a4947
AM
28305+ hbl = sbinfo->si_plink + i;
28306+ if (!warned && verbose && !hlist_bl_empty(hbl)) {
86dc4139
AM
28307+ pr_warn("pseudo-link is not flushed");
28308+ warned = 1;
28309+ }
8b6a4947 28310+ hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink)
5afbbe0d 28311+ iput(&icntnr->vfs_inode);
8b6a4947 28312+ INIT_HLIST_BL_HEAD(hbl);
86dc4139 28313+ }
1facf9fc 28314+}
28315+
e49829fe
JR
28316+void au_plink_clean(struct super_block *sb, int verbose)
28317+{
28318+ struct dentry *root;
28319+
28320+ root = sb->s_root;
28321+ aufs_write_lock(root);
28322+ if (au_opt_test(au_mntflags(sb), PLINK))
28323+ au_plink_put(sb, verbose);
28324+ aufs_write_unlock(root);
28325+}
28326+
86dc4139
AM
28327+static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
28328+{
28329+ int do_put;
5afbbe0d 28330+ aufs_bindex_t btop, bbot, bindex;
86dc4139
AM
28331+
28332+ do_put = 0;
5afbbe0d
AM
28333+ btop = au_ibtop(inode);
28334+ bbot = au_ibbot(inode);
28335+ if (btop >= 0) {
28336+ for (bindex = btop; bindex <= bbot; bindex++) {
86dc4139
AM
28337+ if (!au_h_iptr(inode, bindex)
28338+ || au_ii_br_id(inode, bindex) != br_id)
28339+ continue;
28340+ au_set_h_iptr(inode, bindex, NULL, 0);
28341+ do_put = 1;
28342+ break;
28343+ }
28344+ if (do_put)
5afbbe0d 28345+ for (bindex = btop; bindex <= bbot; bindex++)
86dc4139
AM
28346+ if (au_h_iptr(inode, bindex)) {
28347+ do_put = 0;
28348+ break;
28349+ }
28350+ } else
28351+ do_put = 1;
28352+
28353+ return do_put;
28354+}
28355+
1facf9fc 28356+/* free the plinks on a branch specified by @br_id */
28357+void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
28358+{
28359+ struct au_sbinfo *sbinfo;
8b6a4947
AM
28360+ struct hlist_bl_head *hbl;
28361+ struct hlist_bl_node *pos, *tmp;
5afbbe0d 28362+ struct au_icntnr *icntnr;
1facf9fc 28363+ struct inode *inode;
86dc4139 28364+ int i, do_put;
1facf9fc 28365+
dece6358
AM
28366+ SiMustWriteLock(sb);
28367+
1facf9fc 28368+ sbinfo = au_sbi(sb);
28369+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 28370+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 28371+
8b6a4947 28372+ /* no bit_lock since sbinfo is write-locked */
86dc4139 28373+ for (i = 0; i < AuPlink_NHASH; i++) {
8b6a4947
AM
28374+ hbl = sbinfo->si_plink + i;
28375+ hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink) {
5afbbe0d 28376+ inode = au_igrab(&icntnr->vfs_inode);
86dc4139
AM
28377+ ii_write_lock_child(inode);
28378+ do_put = au_plink_do_half_refresh(inode, br_id);
5afbbe0d 28379+ if (do_put) {
8b6a4947 28380+ hlist_bl_del(&icntnr->plink);
5afbbe0d
AM
28381+ iput(inode);
28382+ }
86dc4139
AM
28383+ ii_write_unlock(inode);
28384+ iput(inode);
dece6358 28385+ }
dece6358
AM
28386+ }
28387+}
7f207e10
AM
28388diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
28389--- /usr/share/empty/fs/aufs/poll.c 1970-01-01 01:00:00.000000000 +0100
0b487229 28390+++ linux/fs/aufs/poll.c 2021-02-22 23:30:37.702680911 +0100
cd7a4cd9
AM
28391@@ -0,0 +1,51 @@
28392+// SPDX-License-Identifier: GPL-2.0
dece6358 28393+/*
0b487229 28394+ * Copyright (C) 2005-2020 Junjiro R. Okajima
dece6358
AM
28395+ *
28396+ * This program, aufs is free software; you can redistribute it and/or modify
28397+ * it under the terms of the GNU General Public License as published by
28398+ * the Free Software Foundation; either version 2 of the License, or
28399+ * (at your option) any later version.
28400+ *
28401+ * This program is distributed in the hope that it will be useful,
28402+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28403+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28404+ * GNU General Public License for more details.
28405+ *
28406+ * You should have received a copy of the GNU General Public License
523b37e3 28407+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358
AM
28408+ */
28409+
1308ab2a 28410+/*
28411+ * poll operation
28412+ * There is only one filesystem which implements ->poll operation, currently.
28413+ */
28414+
28415+#include "aufs.h"
28416+
cd7a4cd9 28417+__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt)
1308ab2a 28418+{
be118d29 28419+ __poll_t mask;
1308ab2a 28420+ struct file *h_file;
1308ab2a 28421+ struct super_block *sb;
28422+
28423+ /* We should pretend an error happened. */
be118d29 28424+ mask = EPOLLERR /* | EPOLLIN | EPOLLOUT */;
b912730e 28425+ sb = file->f_path.dentry->d_sb;
e49829fe 28426+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e 28427+
521ced18 28428+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
062440b3
AM
28429+ if (IS_ERR(h_file)) {
28430+ AuDbg("h_file %ld\n", PTR_ERR(h_file));
1308ab2a 28431+ goto out;
062440b3 28432+ }
1308ab2a 28433+
cd7a4cd9 28434+ mask = vfs_poll(h_file, pt);
b912730e 28435+ fput(h_file); /* instead of au_read_post() */
1308ab2a 28436+
4f0767ce 28437+out:
1308ab2a 28438+ si_read_unlock(sb);
062440b3 28439+ if (mask & EPOLLERR)
b00004a5 28440+ AuDbg("mask 0x%x\n", mask);
1308ab2a 28441+ return mask;
28442+}
c1595e42
JR
28443diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
28444--- /usr/share/empty/fs/aufs/posix_acl.c 1970-01-01 01:00:00.000000000 +0100
0b487229
JR
28445+++ linux/fs/aufs/posix_acl.c 2021-02-22 23:30:37.702680911 +0100
28446@@ -0,0 +1,105 @@
cd7a4cd9 28447+// SPDX-License-Identifier: GPL-2.0
c1595e42 28448+/*
0b487229 28449+ * Copyright (C) 2014-2020 Junjiro R. Okajima
c1595e42
JR
28450+ *
28451+ * This program, aufs is free software; you can redistribute it and/or modify
28452+ * it under the terms of the GNU General Public License as published by
28453+ * the Free Software Foundation; either version 2 of the License, or
28454+ * (at your option) any later version.
28455+ *
28456+ * This program is distributed in the hope that it will be useful,
28457+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28458+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28459+ * GNU General Public License for more details.
28460+ *
28461+ * You should have received a copy of the GNU General Public License
28462+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
28463+ */
28464+
28465+/*
28466+ * posix acl operations
28467+ */
28468+
28469+#include <linux/fs.h>
c1595e42
JR
28470+#include "aufs.h"
28471+
28472+struct posix_acl *aufs_get_acl(struct inode *inode, int type)
28473+{
28474+ struct posix_acl *acl;
28475+ int err;
28476+ aufs_bindex_t bindex;
28477+ struct inode *h_inode;
28478+ struct super_block *sb;
28479+
28480+ acl = NULL;
28481+ sb = inode->i_sb;
28482+ si_read_lock(sb, AuLock_FLUSH);
28483+ ii_read_lock_child(inode);
2121bcd9 28484+ if (!(sb->s_flags & SB_POSIXACL))
c1595e42
JR
28485+ goto out;
28486+
5afbbe0d 28487+ bindex = au_ibtop(inode);
c1595e42
JR
28488+ h_inode = au_h_iptr(inode, bindex);
28489+ if (unlikely(!h_inode
28490+ || ((h_inode->i_mode & S_IFMT)
28491+ != (inode->i_mode & S_IFMT)))) {
28492+ err = au_busy_or_stale();
28493+ acl = ERR_PTR(err);
28494+ goto out;
28495+ }
28496+
28497+ /* always topmost only */
28498+ acl = get_acl(h_inode, type);
0b487229
JR
28499+ if (IS_ERR(acl))
28500+ forget_cached_acl(inode, type);
28501+ else
a2654f78 28502+ set_cached_acl(inode, type, acl);
c1595e42
JR
28503+
28504+out:
28505+ ii_read_unlock(inode);
28506+ si_read_unlock(sb);
28507+
28508+ AuTraceErrPtr(acl);
28509+ return acl;
28510+}
28511+
28512+int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
28513+{
28514+ int err;
28515+ ssize_t ssz;
28516+ struct dentry *dentry;
f2c43d5f 28517+ struct au_sxattr arg = {
c1595e42
JR
28518+ .type = AU_ACL_SET,
28519+ .u.acl_set = {
28520+ .acl = acl,
28521+ .type = type
28522+ },
28523+ };
28524+
5afbbe0d
AM
28525+ IMustLock(inode);
28526+
c1595e42
JR
28527+ if (inode->i_ino == AUFS_ROOT_INO)
28528+ dentry = dget(inode->i_sb->s_root);
28529+ else {
28530+ dentry = d_find_alias(inode);
28531+ if (!dentry)
28532+ dentry = d_find_any_alias(inode);
28533+ if (!dentry) {
28534+ pr_warn("cannot handle this inode, "
28535+ "please report to aufs-users ML\n");
28536+ err = -ENOENT;
28537+ goto out;
28538+ }
28539+ }
28540+
f2c43d5f 28541+ ssz = au_sxattr(dentry, inode, &arg);
0b487229
JR
28542+ /* forget even it if succeeds since the branch might set differently */
28543+ forget_cached_acl(inode, type);
c1595e42
JR
28544+ dput(dentry);
28545+ err = ssz;
0b487229 28546+ if (ssz >= 0)
c1595e42
JR
28547+ err = 0;
28548+
28549+out:
c1595e42
JR
28550+ return err;
28551+}
7f207e10
AM
28552diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
28553--- /usr/share/empty/fs/aufs/procfs.c 1970-01-01 01:00:00.000000000 +0100
0b487229 28554+++ linux/fs/aufs/procfs.c 2021-02-22 23:30:37.702680911 +0100
062440b3 28555@@ -0,0 +1,171 @@
cd7a4cd9 28556+// SPDX-License-Identifier: GPL-2.0
e49829fe 28557+/*
0b487229 28558+ * Copyright (C) 2010-2020 Junjiro R. Okajima
e49829fe
JR
28559+ *
28560+ * This program, aufs is free software; you can redistribute it and/or modify
28561+ * it under the terms of the GNU General Public License as published by
28562+ * the Free Software Foundation; either version 2 of the License, or
28563+ * (at your option) any later version.
28564+ *
28565+ * This program is distributed in the hope that it will be useful,
28566+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28567+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28568+ * GNU General Public License for more details.
28569+ *
28570+ * You should have received a copy of the GNU General Public License
523b37e3 28571+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
e49829fe
JR
28572+ */
28573+
28574+/*
28575+ * procfs interfaces
28576+ */
28577+
28578+#include <linux/proc_fs.h>
28579+#include "aufs.h"
28580+
28581+static int au_procfs_plm_release(struct inode *inode, struct file *file)
28582+{
28583+ struct au_sbinfo *sbinfo;
28584+
28585+ sbinfo = file->private_data;
28586+ if (sbinfo) {
28587+ au_plink_maint_leave(sbinfo);
28588+ kobject_put(&sbinfo->si_kobj);
28589+ }
28590+
28591+ return 0;
28592+}
28593+
28594+static void au_procfs_plm_write_clean(struct file *file)
28595+{
28596+ struct au_sbinfo *sbinfo;
28597+
28598+ sbinfo = file->private_data;
28599+ if (sbinfo)
28600+ au_plink_clean(sbinfo->si_sb, /*verbose*/0);
28601+}
28602+
28603+static int au_procfs_plm_write_si(struct file *file, unsigned long id)
28604+{
28605+ int err;
28606+ struct super_block *sb;
28607+ struct au_sbinfo *sbinfo;
8b6a4947 28608+ struct hlist_bl_node *pos;
e49829fe
JR
28609+
28610+ err = -EBUSY;
28611+ if (unlikely(file->private_data))
28612+ goto out;
28613+
28614+ sb = NULL;
53392da6 28615+ /* don't use au_sbilist_lock() here */
8b6a4947
AM
28616+ hlist_bl_lock(&au_sbilist);
28617+ hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
e49829fe 28618+ if (id == sysaufs_si_id(sbinfo)) {
0b487229
JR
28619+ if (kobject_get_unless_zero(&sbinfo->si_kobj))
28620+ sb = sbinfo->si_sb;
e49829fe
JR
28621+ break;
28622+ }
8b6a4947 28623+ hlist_bl_unlock(&au_sbilist);
e49829fe
JR
28624+
28625+ err = -EINVAL;
28626+ if (unlikely(!sb))
28627+ goto out;
28628+
28629+ err = au_plink_maint_enter(sb);
28630+ if (!err)
28631+ /* keep kobject_get() */
28632+ file->private_data = sbinfo;
28633+ else
28634+ kobject_put(&sbinfo->si_kobj);
28635+out:
28636+ return err;
28637+}
28638+
28639+/*
28640+ * Accept a valid "si=xxxx" only.
28641+ * Once it is accepted successfully, accept "clean" too.
28642+ */
28643+static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
28644+ size_t count, loff_t *ppos)
28645+{
28646+ ssize_t err;
28647+ unsigned long id;
28648+ /* last newline is allowed */
28649+ char buf[3 + sizeof(unsigned long) * 2 + 1];
28650+
28651+ err = -EACCES;
28652+ if (unlikely(!capable(CAP_SYS_ADMIN)))
28653+ goto out;
28654+
28655+ err = -EINVAL;
28656+ if (unlikely(count > sizeof(buf)))
28657+ goto out;
28658+
28659+ err = copy_from_user(buf, ubuf, count);
28660+ if (unlikely(err)) {
28661+ err = -EFAULT;
28662+ goto out;
28663+ }
28664+ buf[count] = 0;
28665+
28666+ err = -EINVAL;
28667+ if (!strcmp("clean", buf)) {
28668+ au_procfs_plm_write_clean(file);
28669+ goto out_success;
28670+ } else if (unlikely(strncmp("si=", buf, 3)))
28671+ goto out;
28672+
9dbd164d 28673+ err = kstrtoul(buf + 3, 16, &id);
e49829fe
JR
28674+ if (unlikely(err))
28675+ goto out;
28676+
28677+ err = au_procfs_plm_write_si(file, id);
28678+ if (unlikely(err))
28679+ goto out;
28680+
28681+out_success:
28682+ err = count; /* success */
28683+out:
28684+ return err;
28685+}
28686+
28687+static const struct file_operations au_procfs_plm_fop = {
28688+ .write = au_procfs_plm_write,
28689+ .release = au_procfs_plm_release,
28690+ .owner = THIS_MODULE
28691+};
28692+
28693+/* ---------------------------------------------------------------------- */
28694+
28695+static struct proc_dir_entry *au_procfs_dir;
28696+
28697+void au_procfs_fin(void)
28698+{
28699+ remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
28700+ remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28701+}
28702+
28703+int __init au_procfs_init(void)
28704+{
28705+ int err;
28706+ struct proc_dir_entry *entry;
28707+
28708+ err = -ENOMEM;
28709+ au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
28710+ if (unlikely(!au_procfs_dir))
28711+ goto out;
28712+
cd7a4cd9 28713+ entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | 0200,
e49829fe
JR
28714+ au_procfs_dir, &au_procfs_plm_fop);
28715+ if (unlikely(!entry))
28716+ goto out_dir;
28717+
28718+ err = 0;
28719+ goto out; /* success */
28720+
28721+
28722+out_dir:
28723+ remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28724+out:
28725+ return err;
28726+}
7f207e10
AM
28727diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
28728--- /usr/share/empty/fs/aufs/rdu.c 1970-01-01 01:00:00.000000000 +0100
0b487229 28729+++ linux/fs/aufs/rdu.c 2021-02-22 23:30:37.702680911 +0100
062440b3 28730@@ -0,0 +1,382 @@
cd7a4cd9 28731+// SPDX-License-Identifier: GPL-2.0
1308ab2a 28732+/*
0b487229 28733+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1308ab2a 28734+ *
28735+ * This program, aufs is free software; you can redistribute it and/or modify
28736+ * it under the terms of the GNU General Public License as published by
28737+ * the Free Software Foundation; either version 2 of the License, or
28738+ * (at your option) any later version.
28739+ *
28740+ * This program is distributed in the hope that it will be useful,
28741+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28742+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28743+ * GNU General Public License for more details.
28744+ *
28745+ * You should have received a copy of the GNU General Public License
523b37e3 28746+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1308ab2a 28747+ */
28748+
28749+/*
28750+ * readdir in userspace.
28751+ */
28752+
b752ccd1 28753+#include <linux/compat.h>
4a4d8108 28754+#include <linux/fs_stack.h>
1308ab2a 28755+#include <linux/security.h>
1308ab2a 28756+#include "aufs.h"
28757+
28758+/* bits for struct aufs_rdu.flags */
28759+#define AuRdu_CALLED 1
28760+#define AuRdu_CONT (1 << 1)
28761+#define AuRdu_FULL (1 << 2)
28762+#define au_ftest_rdu(flags, name) ((flags) & AuRdu_##name)
7f207e10
AM
28763+#define au_fset_rdu(flags, name) \
28764+ do { (flags) |= AuRdu_##name; } while (0)
28765+#define au_fclr_rdu(flags, name) \
28766+ do { (flags) &= ~AuRdu_##name; } while (0)
1308ab2a 28767+
28768+struct au_rdu_arg {
392086de 28769+ struct dir_context ctx;
1308ab2a 28770+ struct aufs_rdu *rdu;
28771+ union au_rdu_ent_ul ent;
28772+ unsigned long end;
28773+
28774+ struct super_block *sb;
28775+ int err;
28776+};
28777+
392086de 28778+static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
1308ab2a 28779+ loff_t offset, u64 h_ino, unsigned int d_type)
28780+{
28781+ int err, len;
392086de 28782+ struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
1308ab2a 28783+ struct aufs_rdu *rdu = arg->rdu;
28784+ struct au_rdu_ent ent;
28785+
28786+ err = 0;
28787+ arg->err = 0;
28788+ au_fset_rdu(rdu->cookie.flags, CALLED);
28789+ len = au_rdu_len(nlen);
28790+ if (arg->ent.ul + len < arg->end) {
28791+ ent.ino = h_ino;
28792+ ent.bindex = rdu->cookie.bindex;
28793+ ent.type = d_type;
28794+ ent.nlen = nlen;
4a4d8108
AM
28795+ if (unlikely(nlen > AUFS_MAX_NAMELEN))
28796+ ent.type = DT_UNKNOWN;
1308ab2a 28797+
9dbd164d 28798+ /* unnecessary to support mmap_sem since this is a dir */
1308ab2a 28799+ err = -EFAULT;
28800+ if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
28801+ goto out;
28802+ if (copy_to_user(arg->ent.e->name, name, nlen))
28803+ goto out;
28804+ /* the terminating NULL */
28805+ if (__put_user(0, arg->ent.e->name + nlen))
28806+ goto out;
28807+ err = 0;
28808+ /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
28809+ arg->ent.ul += len;
28810+ rdu->rent++;
28811+ } else {
28812+ err = -EFAULT;
28813+ au_fset_rdu(rdu->cookie.flags, FULL);
28814+ rdu->full = 1;
28815+ rdu->tail = arg->ent;
28816+ }
28817+
4f0767ce 28818+out:
1308ab2a 28819+ /* AuTraceErr(err); */
28820+ return err;
28821+}
28822+
28823+static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
28824+{
28825+ int err;
28826+ loff_t offset;
28827+ struct au_rdu_cookie *cookie = &arg->rdu->cookie;
28828+
92d182d2 28829+ /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
1308ab2a 28830+ offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
28831+ err = offset;
28832+ if (unlikely(offset != cookie->h_pos))
28833+ goto out;
28834+
28835+ err = 0;
28836+ do {
28837+ arg->err = 0;
28838+ au_fclr_rdu(cookie->flags, CALLED);
28839+ /* smp_mb(); */
392086de 28840+ err = vfsub_iterate_dir(h_file, &arg->ctx);
1308ab2a 28841+ if (err >= 0)
28842+ err = arg->err;
28843+ } while (!err
28844+ && au_ftest_rdu(cookie->flags, CALLED)
28845+ && !au_ftest_rdu(cookie->flags, FULL));
28846+ cookie->h_pos = h_file->f_pos;
28847+
4f0767ce 28848+out:
1308ab2a 28849+ AuTraceErr(err);
28850+ return err;
28851+}
28852+
28853+static int au_rdu(struct file *file, struct aufs_rdu *rdu)
28854+{
28855+ int err;
5afbbe0d 28856+ aufs_bindex_t bbot;
392086de
AM
28857+ struct au_rdu_arg arg = {
28858+ .ctx = {
2000de60 28859+ .actor = au_rdu_fill
392086de
AM
28860+ }
28861+ };
1308ab2a 28862+ struct dentry *dentry;
28863+ struct inode *inode;
28864+ struct file *h_file;
28865+ struct au_rdu_cookie *cookie = &rdu->cookie;
28866+
28867+ err = !access_ok(VERIFY_WRITE, rdu->ent.e, rdu->sz);
28868+ if (unlikely(err)) {
28869+ err = -EFAULT;
28870+ AuTraceErr(err);
28871+ goto out;
28872+ }
28873+ rdu->rent = 0;
28874+ rdu->tail = rdu->ent;
28875+ rdu->full = 0;
28876+ arg.rdu = rdu;
28877+ arg.ent = rdu->ent;
28878+ arg.end = arg.ent.ul;
28879+ arg.end += rdu->sz;
28880+
28881+ err = -ENOTDIR;
5afbbe0d 28882+ if (unlikely(!file->f_op->iterate && !file->f_op->iterate_shared))
1308ab2a 28883+ goto out;
28884+
28885+ err = security_file_permission(file, MAY_READ);
28886+ AuTraceErr(err);
28887+ if (unlikely(err))
28888+ goto out;
28889+
2000de60 28890+ dentry = file->f_path.dentry;
5527c038 28891+ inode = d_inode(dentry);
5afbbe0d 28892+ inode_lock_shared(inode);
1308ab2a 28893+
28894+ arg.sb = inode->i_sb;
e49829fe
JR
28895+ err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
28896+ if (unlikely(err))
28897+ goto out_mtx;
027c5e7a
AM
28898+ err = au_alive_dir(dentry);
28899+ if (unlikely(err))
28900+ goto out_si;
e49829fe 28901+ /* todo: reval? */
1308ab2a 28902+ fi_read_lock(file);
28903+
28904+ err = -EAGAIN;
28905+ if (unlikely(au_ftest_rdu(cookie->flags, CONT)
28906+ && cookie->generation != au_figen(file)))
28907+ goto out_unlock;
28908+
28909+ err = 0;
28910+ if (!rdu->blk) {
28911+ rdu->blk = au_sbi(arg.sb)->si_rdblk;
28912+ if (!rdu->blk)
28913+ rdu->blk = au_dir_size(file, /*dentry*/NULL);
28914+ }
5afbbe0d
AM
28915+ bbot = au_fbtop(file);
28916+ if (cookie->bindex < bbot)
28917+ cookie->bindex = bbot;
28918+ bbot = au_fbbot_dir(file);
28919+ /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
28920+ for (; !err && cookie->bindex <= bbot;
1308ab2a 28921+ cookie->bindex++, cookie->h_pos = 0) {
4a4d8108 28922+ h_file = au_hf_dir(file, cookie->bindex);
1308ab2a 28923+ if (!h_file)
28924+ continue;
28925+
28926+ au_fclr_rdu(cookie->flags, FULL);
28927+ err = au_rdu_do(h_file, &arg);
28928+ AuTraceErr(err);
28929+ if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
28930+ break;
28931+ }
28932+ AuDbg("rent %llu\n", rdu->rent);
28933+
28934+ if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
28935+ rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
28936+ au_fset_rdu(cookie->flags, CONT);
28937+ cookie->generation = au_figen(file);
28938+ }
28939+
28940+ ii_read_lock_child(inode);
5afbbe0d 28941+ fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
1308ab2a 28942+ ii_read_unlock(inode);
28943+
4f0767ce 28944+out_unlock:
1308ab2a 28945+ fi_read_unlock(file);
027c5e7a 28946+out_si:
1308ab2a 28947+ si_read_unlock(arg.sb);
4f0767ce 28948+out_mtx:
5afbbe0d 28949+ inode_unlock_shared(inode);
4f0767ce 28950+out:
1308ab2a 28951+ AuTraceErr(err);
28952+ return err;
28953+}
28954+
28955+static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
28956+{
28957+ int err;
28958+ ino_t ino;
28959+ unsigned long long nent;
28960+ union au_rdu_ent_ul *u;
28961+ struct au_rdu_ent ent;
28962+ struct super_block *sb;
28963+
28964+ err = 0;
28965+ nent = rdu->nent;
28966+ u = &rdu->ent;
2000de60 28967+ sb = file->f_path.dentry->d_sb;
1308ab2a 28968+ si_read_lock(sb, AuLock_FLUSH);
28969+ while (nent-- > 0) {
9dbd164d 28970+ /* unnecessary to support mmap_sem since this is a dir */
1308ab2a 28971+ err = copy_from_user(&ent, u->e, sizeof(ent));
4a4d8108
AM
28972+ if (!err)
28973+ err = !access_ok(VERIFY_WRITE, &u->e->ino, sizeof(ino));
1308ab2a 28974+ if (unlikely(err)) {
28975+ err = -EFAULT;
28976+ AuTraceErr(err);
28977+ break;
28978+ }
28979+
28980+ /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
28981+ if (!ent.wh)
28982+ err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
28983+ else
28984+ err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
28985+ &ino);
28986+ if (unlikely(err)) {
28987+ AuTraceErr(err);
28988+ break;
28989+ }
28990+
28991+ err = __put_user(ino, &u->e->ino);
28992+ if (unlikely(err)) {
28993+ err = -EFAULT;
28994+ AuTraceErr(err);
28995+ break;
28996+ }
28997+ u->ul += au_rdu_len(ent.nlen);
28998+ }
28999+ si_read_unlock(sb);
29000+
29001+ return err;
29002+}
29003+
29004+/* ---------------------------------------------------------------------- */
29005+
29006+static int au_rdu_verify(struct aufs_rdu *rdu)
29007+{
b752ccd1 29008+ AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
1308ab2a 29009+ "%llu, b%d, 0x%x, g%u}\n",
b752ccd1 29010+ rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
1308ab2a 29011+ rdu->blk,
29012+ rdu->rent, rdu->shwh, rdu->full,
29013+ rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
29014+ rdu->cookie.generation);
dece6358 29015+
b752ccd1 29016+ if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
1308ab2a 29017+ return 0;
dece6358 29018+
b752ccd1
AM
29019+ AuDbg("%u:%u\n",
29020+ rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
1308ab2a 29021+ return -EINVAL;
29022+}
29023+
29024+long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
dece6358 29025+{
1308ab2a 29026+ long err, e;
29027+ struct aufs_rdu rdu;
29028+ void __user *p = (void __user *)arg;
dece6358 29029+
1308ab2a 29030+ err = copy_from_user(&rdu, p, sizeof(rdu));
29031+ if (unlikely(err)) {
29032+ err = -EFAULT;
29033+ AuTraceErr(err);
29034+ goto out;
29035+ }
29036+ err = au_rdu_verify(&rdu);
dece6358
AM
29037+ if (unlikely(err))
29038+ goto out;
29039+
1308ab2a 29040+ switch (cmd) {
29041+ case AUFS_CTL_RDU:
29042+ err = au_rdu(file, &rdu);
29043+ if (unlikely(err))
29044+ break;
dece6358 29045+
1308ab2a 29046+ e = copy_to_user(p, &rdu, sizeof(rdu));
29047+ if (unlikely(e)) {
29048+ err = -EFAULT;
29049+ AuTraceErr(err);
29050+ }
29051+ break;
29052+ case AUFS_CTL_RDU_INO:
29053+ err = au_rdu_ino(file, &rdu);
29054+ break;
29055+
29056+ default:
4a4d8108 29057+ /* err = -ENOTTY; */
1308ab2a 29058+ err = -EINVAL;
29059+ }
dece6358 29060+
4f0767ce 29061+out:
1308ab2a 29062+ AuTraceErr(err);
29063+ return err;
1facf9fc 29064+}
b752ccd1
AM
29065+
29066+#ifdef CONFIG_COMPAT
29067+long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
29068+{
29069+ long err, e;
29070+ struct aufs_rdu rdu;
29071+ void __user *p = compat_ptr(arg);
29072+
29073+ /* todo: get_user()? */
29074+ err = copy_from_user(&rdu, p, sizeof(rdu));
29075+ if (unlikely(err)) {
29076+ err = -EFAULT;
29077+ AuTraceErr(err);
29078+ goto out;
29079+ }
29080+ rdu.ent.e = compat_ptr(rdu.ent.ul);
29081+ err = au_rdu_verify(&rdu);
29082+ if (unlikely(err))
29083+ goto out;
29084+
29085+ switch (cmd) {
29086+ case AUFS_CTL_RDU:
29087+ err = au_rdu(file, &rdu);
29088+ if (unlikely(err))
29089+ break;
29090+
29091+ rdu.ent.ul = ptr_to_compat(rdu.ent.e);
29092+ rdu.tail.ul = ptr_to_compat(rdu.tail.e);
29093+ e = copy_to_user(p, &rdu, sizeof(rdu));
29094+ if (unlikely(e)) {
29095+ err = -EFAULT;
29096+ AuTraceErr(err);
29097+ }
29098+ break;
29099+ case AUFS_CTL_RDU_INO:
29100+ err = au_rdu_ino(file, &rdu);
29101+ break;
29102+
29103+ default:
29104+ /* err = -ENOTTY; */
29105+ err = -EINVAL;
29106+ }
29107+
4f0767ce 29108+out:
b752ccd1
AM
29109+ AuTraceErr(err);
29110+ return err;
29111+}
29112+#endif
7f207e10
AM
29113diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
29114--- /usr/share/empty/fs/aufs/rwsem.h 1970-01-01 01:00:00.000000000 +0100
0b487229 29115+++ linux/fs/aufs/rwsem.h 2021-02-22 23:30:37.702680911 +0100
062440b3
AM
29116@@ -0,0 +1,73 @@
29117+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 29118+/*
0b487229 29119+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 29120+ *
29121+ * This program, aufs is free software; you can redistribute it and/or modify
29122+ * it under the terms of the GNU General Public License as published by
29123+ * the Free Software Foundation; either version 2 of the License, or
29124+ * (at your option) any later version.
dece6358
AM
29125+ *
29126+ * This program is distributed in the hope that it will be useful,
29127+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
29128+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29129+ * GNU General Public License for more details.
29130+ *
29131+ * You should have received a copy of the GNU General Public License
523b37e3 29132+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 29133+ */
29134+
29135+/*
29136+ * simple read-write semaphore wrappers
29137+ */
29138+
29139+#ifndef __AUFS_RWSEM_H__
29140+#define __AUFS_RWSEM_H__
29141+
29142+#ifdef __KERNEL__
29143+
4a4d8108 29144+#include "debug.h"
dece6358 29145+
acd2b654 29146+/* in the future, the name 'au_rwsem' will be totally gone */
8b6a4947 29147+#define au_rwsem rw_semaphore
dece6358
AM
29148+
29149+/* to debug easier, do not make them inlined functions */
8b6a4947 29150+#define AuRwMustNoWaiters(rw) AuDebugOn(rwsem_is_contended(rw))
dece6358 29151+/* rwsem_is_locked() is unusable */
8b6a4947
AM
29152+#define AuRwMustReadLock(rw) AuDebugOn(!lockdep_recursing(current) \
29153+ && debug_locks \
29154+ && !lockdep_is_held_type(rw, 1))
29155+#define AuRwMustWriteLock(rw) AuDebugOn(!lockdep_recursing(current) \
29156+ && debug_locks \
29157+ && !lockdep_is_held_type(rw, 0))
29158+#define AuRwMustAnyLock(rw) AuDebugOn(!lockdep_recursing(current) \
29159+ && debug_locks \
29160+ && !lockdep_is_held(rw))
29161+#define AuRwDestroy(rw) AuDebugOn(!lockdep_recursing(current) \
29162+ && debug_locks \
29163+ && lockdep_is_held(rw))
29164+
29165+#define au_rw_init(rw) init_rwsem(rw)
dece6358 29166+
5afbbe0d
AM
29167+#define au_rw_init_wlock(rw) do { \
29168+ au_rw_init(rw); \
8b6a4947 29169+ down_write(rw); \
5afbbe0d 29170+ } while (0)
dece6358 29171+
8b6a4947
AM
29172+#define au_rw_init_wlock_nested(rw, lsc) do { \
29173+ au_rw_init(rw); \
29174+ down_write_nested(rw, lsc); \
5afbbe0d 29175+ } while (0)
dece6358 29176+
8b6a4947
AM
29177+#define au_rw_read_lock(rw) down_read(rw)
29178+#define au_rw_read_lock_nested(rw, lsc) down_read_nested(rw, lsc)
29179+#define au_rw_read_unlock(rw) up_read(rw)
29180+#define au_rw_dgrade_lock(rw) downgrade_write(rw)
29181+#define au_rw_write_lock(rw) down_write(rw)
29182+#define au_rw_write_lock_nested(rw, lsc) down_write_nested(rw, lsc)
29183+#define au_rw_write_unlock(rw) up_write(rw)
29184+/* why is not _nested version defined? */
29185+#define au_rw_read_trylock(rw) down_read_trylock(rw)
29186+#define au_rw_write_trylock(rw) down_write_trylock(rw)
1facf9fc 29187+
29188+#endif /* __KERNEL__ */
29189+#endif /* __AUFS_RWSEM_H__ */
7f207e10
AM
29190diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
29191--- /usr/share/empty/fs/aufs/sbinfo.c 1970-01-01 01:00:00.000000000 +0100
0b487229
JR
29192+++ linux/fs/aufs/sbinfo.c 2021-02-22 23:30:37.702680911 +0100
29193@@ -0,0 +1,314 @@
cd7a4cd9 29194+// SPDX-License-Identifier: GPL-2.0
1facf9fc 29195+/*
0b487229 29196+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 29197+ *
29198+ * This program, aufs is free software; you can redistribute it and/or modify
29199+ * it under the terms of the GNU General Public License as published by
29200+ * the Free Software Foundation; either version 2 of the License, or
29201+ * (at your option) any later version.
dece6358
AM
29202+ *
29203+ * This program is distributed in the hope that it will be useful,
29204+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
29205+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29206+ * GNU General Public License for more details.
29207+ *
29208+ * You should have received a copy of the GNU General Public License
523b37e3 29209+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 29210+ */
29211+
29212+/*
29213+ * superblock private data
29214+ */
29215+
0b487229 29216+#include <linux/iversion.h>
1facf9fc 29217+#include "aufs.h"
29218+
29219+/*
29220+ * they are necessary regardless sysfs is disabled.
29221+ */
29222+void au_si_free(struct kobject *kobj)
29223+{
86dc4139 29224+ int i;
1facf9fc 29225+ struct au_sbinfo *sbinfo;
b752ccd1 29226+ char *locked __maybe_unused; /* debug only */
1facf9fc 29227+
29228+ sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
86dc4139 29229+ for (i = 0; i < AuPlink_NHASH; i++)
8b6a4947 29230+ AuDebugOn(!hlist_bl_empty(sbinfo->si_plink + i));
f0c0a007 29231+ AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
5afbbe0d 29232+
acd2b654
AM
29233+ AuLCntZero(au_lcnt_read(&sbinfo->si_ninodes, /*do_rev*/0));
29234+ au_lcnt_fin(&sbinfo->si_ninodes, /*do_sync*/0);
29235+ AuLCntZero(au_lcnt_read(&sbinfo->si_nfiles, /*do_rev*/0));
29236+ au_lcnt_fin(&sbinfo->si_nfiles, /*do_sync*/0);
1facf9fc 29237+
062440b3 29238+ dbgaufs_si_fin(sbinfo);
e49829fe 29239+ au_rw_write_lock(&sbinfo->si_rwsem);
1facf9fc 29240+ au_br_free(sbinfo);
e49829fe 29241+ au_rw_write_unlock(&sbinfo->si_rwsem);
b752ccd1 29242+
e49925d1 29243+ au_kfree_try_rcu(sbinfo->si_branch);
1facf9fc 29244+ mutex_destroy(&sbinfo->si_xib_mtx);
dece6358 29245+ AuRwDestroy(&sbinfo->si_rwsem);
1facf9fc 29246+
acd2b654
AM
29247+ au_lcnt_wait_for_fin(&sbinfo->si_ninodes);
29248+ /* si_nfiles is waited too */
e49925d1 29249+ au_kfree_rcu(sbinfo);
1facf9fc 29250+}
29251+
29252+int au_si_alloc(struct super_block *sb)
29253+{
86dc4139 29254+ int err, i;
1facf9fc 29255+ struct au_sbinfo *sbinfo;
29256+
29257+ err = -ENOMEM;
4a4d8108 29258+ sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
1facf9fc 29259+ if (unlikely(!sbinfo))
29260+ goto out;
29261+
29262+ /* will be reallocated separately */
29263+ sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
29264+ if (unlikely(!sbinfo->si_branch))
febd17d6 29265+ goto out_sbinfo;
1facf9fc 29266+
1facf9fc 29267+ err = sysaufs_si_init(sbinfo);
062440b3
AM
29268+ if (!err) {
29269+ dbgaufs_si_null(sbinfo);
29270+ err = dbgaufs_si_init(sbinfo);
29271+ if (unlikely(err))
29272+ kobject_put(&sbinfo->si_kobj);
29273+ }
1facf9fc 29274+ if (unlikely(err))
29275+ goto out_br;
29276+
29277+ au_nwt_init(&sbinfo->si_nowait);
dece6358 29278+ au_rw_init_wlock(&sbinfo->si_rwsem);
b752ccd1 29279+
acd2b654
AM
29280+ au_lcnt_init(&sbinfo->si_ninodes, /*release*/NULL);
29281+ au_lcnt_init(&sbinfo->si_nfiles, /*release*/NULL);
7f207e10 29282+
5afbbe0d 29283+ sbinfo->si_bbot = -1;
392086de 29284+ sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
1facf9fc 29285+
29286+ sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
29287+ sbinfo->si_wbr_create = AuWbrCreate_Def;
4a4d8108
AM
29288+ sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
29289+ sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
1facf9fc 29290+
076b876e
AM
29291+ au_fhsm_init(sbinfo);
29292+
e49829fe 29293+ sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
1facf9fc 29294+
392086de
AM
29295+ sbinfo->si_xino_jiffy = jiffies;
29296+ sbinfo->si_xino_expire
29297+ = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
1facf9fc 29298+ mutex_init(&sbinfo->si_xib_mtx);
1facf9fc 29299+ /* leave si_xib_last_pindex and si_xib_next_bit */
29300+
8b6a4947 29301+ INIT_HLIST_BL_HEAD(&sbinfo->si_aopen);
b912730e 29302+
e49829fe 29303+ sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
1facf9fc 29304+ sbinfo->si_rdblk = AUFS_RDBLK_DEF;
29305+ sbinfo->si_rdhash = AUFS_RDHASH_DEF;
29306+ sbinfo->si_dirwh = AUFS_DIRWH_DEF;
29307+
86dc4139 29308+ for (i = 0; i < AuPlink_NHASH; i++)
8b6a4947 29309+ INIT_HLIST_BL_HEAD(sbinfo->si_plink + i);
1facf9fc 29310+ init_waitqueue_head(&sbinfo->si_plink_wq);
4a4d8108 29311+ spin_lock_init(&sbinfo->si_plink_maint_lock);
1facf9fc 29312+
8b6a4947 29313+ INIT_HLIST_BL_HEAD(&sbinfo->si_files);
523b37e3 29314+
b95c5147
AM
29315+ /* with getattr by default */
29316+ sbinfo->si_iop_array = aufs_iop;
29317+
1facf9fc 29318+ /* leave other members for sysaufs and si_mnt. */
29319+ sbinfo->si_sb = sb;
29320+ sb->s_fs_info = sbinfo;
b752ccd1 29321+ si_pid_set(sb);
1facf9fc 29322+ return 0; /* success */
29323+
4f0767ce 29324+out_br:
e49925d1 29325+ au_kfree_try_rcu(sbinfo->si_branch);
4f0767ce 29326+out_sbinfo:
e49925d1 29327+ au_kfree_rcu(sbinfo);
4f0767ce 29328+out:
1facf9fc 29329+ return err;
29330+}
29331+
e2f27e51 29332+int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
1facf9fc 29333+{
29334+ int err, sz;
29335+ struct au_branch **brp;
29336+
dece6358
AM
29337+ AuRwMustWriteLock(&sbinfo->si_rwsem);
29338+
1facf9fc 29339+ err = -ENOMEM;
5afbbe0d 29340+ sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
1facf9fc 29341+ if (unlikely(!sz))
29342+ sz = sizeof(*brp);
e2f27e51
AM
29343+ brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
29344+ may_shrink);
1facf9fc 29345+ if (brp) {
29346+ sbinfo->si_branch = brp;
29347+ err = 0;
29348+ }
29349+
29350+ return err;
29351+}
29352+
29353+/* ---------------------------------------------------------------------- */
29354+
29355+unsigned int au_sigen_inc(struct super_block *sb)
29356+{
29357+ unsigned int gen;
5527c038 29358+ struct inode *inode;
1facf9fc 29359+
dece6358
AM
29360+ SiMustWriteLock(sb);
29361+
1facf9fc 29362+ gen = ++au_sbi(sb)->si_generation;
29363+ au_update_digen(sb->s_root);
5527c038
JR
29364+ inode = d_inode(sb->s_root);
29365+ au_update_iigen(inode, /*half*/0);
be118d29 29366+ inode_inc_iversion(inode);
1facf9fc 29367+ return gen;
29368+}
29369+
29370+aufs_bindex_t au_new_br_id(struct super_block *sb)
29371+{
29372+ aufs_bindex_t br_id;
29373+ int i;
29374+ struct au_sbinfo *sbinfo;
29375+
dece6358
AM
29376+ SiMustWriteLock(sb);
29377+
1facf9fc 29378+ sbinfo = au_sbi(sb);
29379+ for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
29380+ br_id = ++sbinfo->si_last_br_id;
7f207e10 29381+ AuDebugOn(br_id < 0);
1facf9fc 29382+ if (br_id && au_br_index(sb, br_id) < 0)
29383+ return br_id;
29384+ }
29385+
29386+ return -1;
29387+}
29388+
29389+/* ---------------------------------------------------------------------- */
29390+
e49829fe
JR
29391+/* it is ok that new 'nwt' tasks are appended while we are sleeping */
29392+int si_read_lock(struct super_block *sb, int flags)
29393+{
29394+ int err;
29395+
29396+ err = 0;
29397+ if (au_ftest_lock(flags, FLUSH))
29398+ au_nwt_flush(&au_sbi(sb)->si_nowait);
29399+
29400+ si_noflush_read_lock(sb);
29401+ err = au_plink_maint(sb, flags);
29402+ if (unlikely(err))
29403+ si_read_unlock(sb);
29404+
29405+ return err;
29406+}
29407+
29408+int si_write_lock(struct super_block *sb, int flags)
29409+{
29410+ int err;
29411+
29412+ if (au_ftest_lock(flags, FLUSH))
29413+ au_nwt_flush(&au_sbi(sb)->si_nowait);
29414+
29415+ si_noflush_write_lock(sb);
29416+ err = au_plink_maint(sb, flags);
29417+ if (unlikely(err))
29418+ si_write_unlock(sb);
29419+
29420+ return err;
29421+}
29422+
1facf9fc 29423+/* dentry and super_block lock. call at entry point */
e49829fe 29424+int aufs_read_lock(struct dentry *dentry, int flags)
1facf9fc 29425+{
e49829fe 29426+ int err;
027c5e7a 29427+ struct super_block *sb;
e49829fe 29428+
027c5e7a
AM
29429+ sb = dentry->d_sb;
29430+ err = si_read_lock(sb, flags);
29431+ if (unlikely(err))
29432+ goto out;
29433+
29434+ if (au_ftest_lock(flags, DW))
29435+ di_write_lock_child(dentry);
29436+ else
29437+ di_read_lock_child(dentry, flags);
29438+
29439+ if (au_ftest_lock(flags, GEN)) {
29440+ err = au_digen_test(dentry, au_sigen(sb));
79b8bda9
AM
29441+ if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
29442+ AuDebugOn(!err && au_dbrange_test(dentry));
29443+ else if (!err)
29444+ err = au_dbrange_test(dentry);
027c5e7a
AM
29445+ if (unlikely(err))
29446+ aufs_read_unlock(dentry, flags);
e49829fe
JR
29447+ }
29448+
027c5e7a 29449+out:
e49829fe 29450+ return err;
1facf9fc 29451+}
29452+
29453+void aufs_read_unlock(struct dentry *dentry, int flags)
29454+{
29455+ if (au_ftest_lock(flags, DW))
29456+ di_write_unlock(dentry);
29457+ else
29458+ di_read_unlock(dentry, flags);
29459+ si_read_unlock(dentry->d_sb);
29460+}
29461+
29462+void aufs_write_lock(struct dentry *dentry)
29463+{
e49829fe 29464+ si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
1facf9fc 29465+ di_write_lock_child(dentry);
29466+}
29467+
29468+void aufs_write_unlock(struct dentry *dentry)
29469+{
29470+ di_write_unlock(dentry);
29471+ si_write_unlock(dentry->d_sb);
29472+}
29473+
e49829fe 29474+int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
1facf9fc 29475+{
e49829fe 29476+ int err;
027c5e7a
AM
29477+ unsigned int sigen;
29478+ struct super_block *sb;
e49829fe 29479+
027c5e7a
AM
29480+ sb = d1->d_sb;
29481+ err = si_read_lock(sb, flags);
29482+ if (unlikely(err))
29483+ goto out;
29484+
b95c5147 29485+ di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
027c5e7a
AM
29486+
29487+ if (au_ftest_lock(flags, GEN)) {
29488+ sigen = au_sigen(sb);
29489+ err = au_digen_test(d1, sigen);
29490+ AuDebugOn(!err && au_dbrange_test(d1));
29491+ if (!err) {
29492+ err = au_digen_test(d2, sigen);
29493+ AuDebugOn(!err && au_dbrange_test(d2));
29494+ }
29495+ if (unlikely(err))
29496+ aufs_read_and_write_unlock2(d1, d2);
29497+ }
29498+
29499+out:
e49829fe 29500+ return err;
1facf9fc 29501+}
29502+
29503+void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
29504+{
29505+ di_write_unlock2(d1, d2);
29506+ si_read_unlock(d1->d_sb);
29507+}
7f207e10
AM
29508diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
29509--- /usr/share/empty/fs/aufs/super.c 1970-01-01 01:00:00.000000000 +0100
0b487229
JR
29510+++ linux/fs/aufs/super.c 2021-02-22 23:30:37.702680911 +0100
29511@@ -0,0 +1,1049 @@
cd7a4cd9 29512+// SPDX-License-Identifier: GPL-2.0
1facf9fc 29513+/*
0b487229 29514+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 29515+ *
29516+ * This program, aufs is free software; you can redistribute it and/or modify
29517+ * it under the terms of the GNU General Public License as published by
29518+ * the Free Software Foundation; either version 2 of the License, or
29519+ * (at your option) any later version.
dece6358
AM
29520+ *
29521+ * This program is distributed in the hope that it will be useful,
29522+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
29523+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29524+ * GNU General Public License for more details.
29525+ *
29526+ * You should have received a copy of the GNU General Public License
523b37e3 29527+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 29528+ */
29529+
29530+/*
29531+ * mount and super_block operations
29532+ */
29533+
0b487229 29534+#include <linux/iversion.h>
f6c5ef8b 29535+#include <linux/mm.h>
1facf9fc 29536+#include <linux/seq_file.h>
29537+#include <linux/statfs.h>
7f207e10 29538+#include <linux/vmalloc.h>
1facf9fc 29539+#include "aufs.h"
29540+
29541+/*
29542+ * super_operations
29543+ */
29544+static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
29545+{
29546+ struct au_icntnr *c;
29547+
29548+ c = au_cache_alloc_icntnr();
29549+ if (c) {
027c5e7a 29550+ au_icntnr_init(c);
be118d29 29551+ inode_set_iversion(&c->vfs_inode, 1); /* sigen(sb); */
1facf9fc 29552+ c->iinfo.ii_hinode = NULL;
29553+ return &c->vfs_inode;
29554+ }
29555+ return NULL;
29556+}
29557+
027c5e7a
AM
29558+static void aufs_destroy_inode_cb(struct rcu_head *head)
29559+{
29560+ struct inode *inode = container_of(head, struct inode, i_rcu);
29561+
1c60b727 29562+ au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
027c5e7a
AM
29563+}
29564+
1facf9fc 29565+static void aufs_destroy_inode(struct inode *inode)
29566+{
5afbbe0d
AM
29567+ if (!au_is_bad_inode(inode))
29568+ au_iinfo_fin(inode);
027c5e7a 29569+ call_rcu(&inode->i_rcu, aufs_destroy_inode_cb);
1facf9fc 29570+}
29571+
29572+struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
29573+{
29574+ struct inode *inode;
29575+ int err;
29576+
29577+ inode = iget_locked(sb, ino);
29578+ if (unlikely(!inode)) {
29579+ inode = ERR_PTR(-ENOMEM);
29580+ goto out;
29581+ }
29582+ if (!(inode->i_state & I_NEW))
29583+ goto out;
29584+
29585+ err = au_xigen_new(inode);
29586+ if (!err)
29587+ err = au_iinfo_init(inode);
29588+ if (!err)
be118d29 29589+ inode_inc_iversion(inode);
1facf9fc 29590+ else {
29591+ iget_failed(inode);
29592+ inode = ERR_PTR(err);
29593+ }
29594+
4f0767ce 29595+out:
1facf9fc 29596+ /* never return NULL */
29597+ AuDebugOn(!inode);
29598+ AuTraceErrPtr(inode);
29599+ return inode;
29600+}
29601+
29602+/* lock free root dinfo */
29603+static int au_show_brs(struct seq_file *seq, struct super_block *sb)
29604+{
29605+ int err;
5afbbe0d 29606+ aufs_bindex_t bindex, bbot;
1facf9fc 29607+ struct path path;
4a4d8108 29608+ struct au_hdentry *hdp;
1facf9fc 29609+ struct au_branch *br;
076b876e 29610+ au_br_perm_str_t perm;
1facf9fc 29611+
29612+ err = 0;
5afbbe0d
AM
29613+ bbot = au_sbbot(sb);
29614+ bindex = 0;
29615+ hdp = au_hdentry(au_di(sb->s_root), bindex);
29616+ for (; !err && bindex <= bbot; bindex++, hdp++) {
1facf9fc 29617+ br = au_sbr(sb, bindex);
86dc4139 29618+ path.mnt = au_br_mnt(br);
5afbbe0d 29619+ path.dentry = hdp->hd_dentry;
1facf9fc 29620+ err = au_seq_path(seq, &path);
79b8bda9 29621+ if (!err) {
076b876e 29622+ au_optstr_br_perm(&perm, br->br_perm);
79b8bda9 29623+ seq_printf(seq, "=%s", perm.a);
5afbbe0d 29624+ if (bindex != bbot)
79b8bda9 29625+ seq_putc(seq, ':');
1e00d052 29626+ }
1facf9fc 29627+ }
79b8bda9
AM
29628+ if (unlikely(err || seq_has_overflowed(seq)))
29629+ err = -E2BIG;
1facf9fc 29630+
29631+ return err;
29632+}
29633+
f2c43d5f
AM
29634+static void au_gen_fmt(char *fmt, int len __maybe_unused, const char *pat,
29635+ const char *append)
29636+{
29637+ char *p;
29638+
29639+ p = fmt;
29640+ while (*pat != ':')
29641+ *p++ = *pat++;
29642+ *p++ = *pat++;
29643+ strcpy(p, append);
29644+ AuDebugOn(strlen(fmt) >= len);
29645+}
29646+
1facf9fc 29647+static void au_show_wbr_create(struct seq_file *m, int v,
29648+ struct au_sbinfo *sbinfo)
29649+{
29650+ const char *pat;
f2c43d5f
AM
29651+ char fmt[32];
29652+ struct au_wbr_mfs *mfs;
1facf9fc 29653+
dece6358
AM
29654+ AuRwMustAnyLock(&sbinfo->si_rwsem);
29655+
c2b27bf2 29656+ seq_puts(m, ",create=");
1facf9fc 29657+ pat = au_optstr_wbr_create(v);
f2c43d5f 29658+ mfs = &sbinfo->si_wbr_mfs;
1facf9fc 29659+ switch (v) {
29660+ case AuWbrCreate_TDP:
29661+ case AuWbrCreate_RR:
29662+ case AuWbrCreate_MFS:
29663+ case AuWbrCreate_PMFS:
c2b27bf2 29664+ seq_puts(m, pat);
1facf9fc 29665+ break;
f2c43d5f
AM
29666+ case AuWbrCreate_MFSRR:
29667+ case AuWbrCreate_TDMFS:
29668+ case AuWbrCreate_PMFSRR:
29669+ au_gen_fmt(fmt, sizeof(fmt), pat, "%llu");
29670+ seq_printf(m, fmt, mfs->mfsrr_watermark);
1facf9fc 29671+ break;
f2c43d5f 29672+ case AuWbrCreate_MFSV:
1facf9fc 29673+ case AuWbrCreate_PMFSV:
f2c43d5f
AM
29674+ au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
29675+ seq_printf(m, fmt,
29676+ jiffies_to_msecs(mfs->mfs_expire)
e49829fe 29677+ / MSEC_PER_SEC);
1facf9fc 29678+ break;
1facf9fc 29679+ case AuWbrCreate_MFSRRV:
f2c43d5f 29680+ case AuWbrCreate_TDMFSV:
392086de 29681+ case AuWbrCreate_PMFSRRV:
f2c43d5f
AM
29682+ au_gen_fmt(fmt, sizeof(fmt), pat, "%llu:%lu");
29683+ seq_printf(m, fmt, mfs->mfsrr_watermark,
29684+ jiffies_to_msecs(mfs->mfs_expire) / MSEC_PER_SEC);
392086de 29685+ break;
f2c43d5f
AM
29686+ default:
29687+ BUG();
1facf9fc 29688+ }
29689+}
29690+
7eafdf33 29691+static int au_show_xino(struct seq_file *seq, struct super_block *sb)
1facf9fc 29692+{
29693+#ifdef CONFIG_SYSFS
29694+ return 0;
29695+#else
29696+ int err;
29697+ const int len = sizeof(AUFS_XINO_FNAME) - 1;
29698+ aufs_bindex_t bindex, brid;
1facf9fc 29699+ struct qstr *name;
29700+ struct file *f;
29701+ struct dentry *d, *h_root;
acd2b654 29702+ struct au_branch *br;
1facf9fc 29703+
dece6358
AM
29704+ AuRwMustAnyLock(&sbinfo->si_rwsem);
29705+
1facf9fc 29706+ err = 0;
1facf9fc 29707+ f = au_sbi(sb)->si_xib;
29708+ if (!f)
29709+ goto out;
29710+
29711+ /* stop printing the default xino path on the first writable branch */
29712+ h_root = NULL;
acd2b654
AM
29713+ bindex = au_xi_root(sb, f->f_path.dentry);
29714+ if (bindex >= 0) {
29715+ br = au_sbr_sb(sb, bindex);
29716+ h_root = au_br_dentry(br);
1facf9fc 29717+ }
acd2b654 29718+
2000de60 29719+ d = f->f_path.dentry;
1facf9fc 29720+ name = &d->d_name;
29721+ /* safe ->d_parent because the file is unlinked */
29722+ if (d->d_parent == h_root
29723+ && name->len == len
29724+ && !memcmp(name->name, AUFS_XINO_FNAME, len))
29725+ goto out;
29726+
29727+ seq_puts(seq, ",xino=");
29728+ err = au_xino_path(seq, f);
29729+
4f0767ce 29730+out:
1facf9fc 29731+ return err;
29732+#endif
29733+}
29734+
29735+/* seq_file will re-call me in case of too long string */
7eafdf33 29736+static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
1facf9fc 29737+{
027c5e7a 29738+ int err;
1facf9fc 29739+ unsigned int mnt_flags, v;
29740+ struct super_block *sb;
29741+ struct au_sbinfo *sbinfo;
29742+
29743+#define AuBool(name, str) do { \
29744+ v = au_opt_test(mnt_flags, name); \
29745+ if (v != au_opt_test(AuOpt_Def, name)) \
29746+ seq_printf(m, ",%s" #str, v ? "" : "no"); \
29747+} while (0)
29748+
29749+#define AuStr(name, str) do { \
29750+ v = mnt_flags & AuOptMask_##name; \
29751+ if (v != (AuOpt_Def & AuOptMask_##name)) \
29752+ seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
29753+} while (0)
29754+
29755+#define AuUInt(name, str, val) do { \
29756+ if (val != AUFS_##name##_DEF) \
29757+ seq_printf(m, "," #str "=%u", val); \
29758+} while (0)
29759+
7eafdf33 29760+ sb = dentry->d_sb;
2121bcd9 29761+ if (sb->s_flags & SB_POSIXACL)
c1595e42 29762+ seq_puts(m, ",acl");
0b487229 29763+#if 0 /* reserved for future use */
be118d29
JR
29764+ if (sb->s_flags & SB_I_VERSION)
29765+ seq_puts(m, ",i_version");
29766+#endif
c1595e42
JR
29767+
29768+ /* lock free root dinfo */
1facf9fc 29769+ si_noflush_read_lock(sb);
29770+ sbinfo = au_sbi(sb);
29771+ seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
29772+
29773+ mnt_flags = au_mntflags(sb);
29774+ if (au_opt_test(mnt_flags, XINO)) {
7eafdf33 29775+ err = au_show_xino(m, sb);
1facf9fc 29776+ if (unlikely(err))
29777+ goto out;
29778+ } else
29779+ seq_puts(m, ",noxino");
29780+
29781+ AuBool(TRUNC_XINO, trunc_xino);
29782+ AuStr(UDBA, udba);
dece6358 29783+ AuBool(SHWH, shwh);
1facf9fc 29784+ AuBool(PLINK, plink);
4a4d8108 29785+ AuBool(DIO, dio);
076b876e 29786+ AuBool(DIRPERM1, dirperm1);
1facf9fc 29787+
29788+ v = sbinfo->si_wbr_create;
29789+ if (v != AuWbrCreate_Def)
29790+ au_show_wbr_create(m, v, sbinfo);
29791+
29792+ v = sbinfo->si_wbr_copyup;
29793+ if (v != AuWbrCopyup_Def)
29794+ seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
29795+
29796+ v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
29797+ if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
29798+ seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
29799+
29800+ AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
29801+
027c5e7a
AM
29802+ v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
29803+ AuUInt(RDCACHE, rdcache, v);
1facf9fc 29804+
29805+ AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
29806+ AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
29807+
076b876e
AM
29808+ au_fhsm_show(m, sbinfo);
29809+
8b6a4947 29810+ AuBool(DIRREN, dirren);
1facf9fc 29811+ AuBool(SUM, sum);
29812+ /* AuBool(SUM_W, wsum); */
29813+ AuBool(WARN_PERM, warn_perm);
29814+ AuBool(VERBOSE, verbose);
29815+
4f0767ce 29816+out:
1facf9fc 29817+ /* be sure to print "br:" last */
29818+ if (!sysaufs_brs) {
29819+ seq_puts(m, ",br:");
29820+ au_show_brs(m, sb);
29821+ }
29822+ si_read_unlock(sb);
29823+ return 0;
29824+
1facf9fc 29825+#undef AuBool
29826+#undef AuStr
4a4d8108 29827+#undef AuUInt
1facf9fc 29828+}
29829+
29830+/* ---------------------------------------------------------------------- */
29831+
29832+/* sum mode which returns the summation for statfs(2) */
29833+
29834+static u64 au_add_till_max(u64 a, u64 b)
29835+{
29836+ u64 old;
29837+
29838+ old = a;
29839+ a += b;
92d182d2
AM
29840+ if (old <= a)
29841+ return a;
29842+ return ULLONG_MAX;
29843+}
29844+
29845+static u64 au_mul_till_max(u64 a, long mul)
29846+{
29847+ u64 old;
29848+
29849+ old = a;
29850+ a *= mul;
29851+ if (old <= a)
1facf9fc 29852+ return a;
29853+ return ULLONG_MAX;
29854+}
29855+
29856+static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
29857+{
29858+ int err;
92d182d2 29859+ long bsize, factor;
1facf9fc 29860+ u64 blocks, bfree, bavail, files, ffree;
5afbbe0d 29861+ aufs_bindex_t bbot, bindex, i;
1facf9fc 29862+ unsigned char shared;
7f207e10 29863+ struct path h_path;
1facf9fc 29864+ struct super_block *h_sb;
29865+
92d182d2
AM
29866+ err = 0;
29867+ bsize = LONG_MAX;
29868+ files = 0;
29869+ ffree = 0;
1facf9fc 29870+ blocks = 0;
29871+ bfree = 0;
29872+ bavail = 0;
5afbbe0d
AM
29873+ bbot = au_sbbot(sb);
29874+ for (bindex = 0; bindex <= bbot; bindex++) {
7f207e10
AM
29875+ h_path.mnt = au_sbr_mnt(sb, bindex);
29876+ h_sb = h_path.mnt->mnt_sb;
1facf9fc 29877+ shared = 0;
92d182d2 29878+ for (i = 0; !shared && i < bindex; i++)
1facf9fc 29879+ shared = (au_sbr_sb(sb, i) == h_sb);
29880+ if (shared)
29881+ continue;
29882+
29883+ /* sb->s_root for NFS is unreliable */
7f207e10
AM
29884+ h_path.dentry = h_path.mnt->mnt_root;
29885+ err = vfs_statfs(&h_path, buf);
1facf9fc 29886+ if (unlikely(err))
29887+ goto out;
29888+
92d182d2
AM
29889+ if (bsize > buf->f_bsize) {
29890+ /*
29891+ * we will reduce bsize, so we have to expand blocks
29892+ * etc. to match them again
29893+ */
29894+ factor = (bsize / buf->f_bsize);
29895+ blocks = au_mul_till_max(blocks, factor);
29896+ bfree = au_mul_till_max(bfree, factor);
29897+ bavail = au_mul_till_max(bavail, factor);
29898+ bsize = buf->f_bsize;
29899+ }
29900+
29901+ factor = (buf->f_bsize / bsize);
29902+ blocks = au_add_till_max(blocks,
29903+ au_mul_till_max(buf->f_blocks, factor));
29904+ bfree = au_add_till_max(bfree,
29905+ au_mul_till_max(buf->f_bfree, factor));
29906+ bavail = au_add_till_max(bavail,
29907+ au_mul_till_max(buf->f_bavail, factor));
1facf9fc 29908+ files = au_add_till_max(files, buf->f_files);
29909+ ffree = au_add_till_max(ffree, buf->f_ffree);
29910+ }
29911+
92d182d2 29912+ buf->f_bsize = bsize;
1facf9fc 29913+ buf->f_blocks = blocks;
29914+ buf->f_bfree = bfree;
29915+ buf->f_bavail = bavail;
29916+ buf->f_files = files;
29917+ buf->f_ffree = ffree;
92d182d2 29918+ buf->f_frsize = 0;
1facf9fc 29919+
4f0767ce 29920+out:
1facf9fc 29921+ return err;
29922+}
29923+
29924+static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
29925+{
29926+ int err;
7f207e10 29927+ struct path h_path;
1facf9fc 29928+ struct super_block *sb;
29929+
29930+ /* lock free root dinfo */
29931+ sb = dentry->d_sb;
29932+ si_noflush_read_lock(sb);
7f207e10 29933+ if (!au_opt_test(au_mntflags(sb), SUM)) {
1facf9fc 29934+ /* sb->s_root for NFS is unreliable */
7f207e10
AM
29935+ h_path.mnt = au_sbr_mnt(sb, 0);
29936+ h_path.dentry = h_path.mnt->mnt_root;
29937+ err = vfs_statfs(&h_path, buf);
29938+ } else
1facf9fc 29939+ err = au_statfs_sum(sb, buf);
29940+ si_read_unlock(sb);
29941+
29942+ if (!err) {
29943+ buf->f_type = AUFS_SUPER_MAGIC;
4a4d8108 29944+ buf->f_namelen = AUFS_MAX_NAMELEN;
1facf9fc 29945+ memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
29946+ }
29947+ /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
29948+
29949+ return err;
29950+}
29951+
29952+/* ---------------------------------------------------------------------- */
29953+
537831f9
AM
29954+static int aufs_sync_fs(struct super_block *sb, int wait)
29955+{
29956+ int err, e;
5afbbe0d 29957+ aufs_bindex_t bbot, bindex;
537831f9
AM
29958+ struct au_branch *br;
29959+ struct super_block *h_sb;
29960+
29961+ err = 0;
29962+ si_noflush_read_lock(sb);
5afbbe0d
AM
29963+ bbot = au_sbbot(sb);
29964+ for (bindex = 0; bindex <= bbot; bindex++) {
537831f9
AM
29965+ br = au_sbr(sb, bindex);
29966+ if (!au_br_writable(br->br_perm))
29967+ continue;
29968+
29969+ h_sb = au_sbr_sb(sb, bindex);
a2654f78
AM
29970+ e = vfsub_sync_filesystem(h_sb, wait);
29971+ if (unlikely(e && !err))
29972+ err = e;
29973+ /* go on even if an error happens */
537831f9
AM
29974+ }
29975+ si_read_unlock(sb);
29976+
29977+ return err;
29978+}
29979+
29980+/* ---------------------------------------------------------------------- */
29981+
1facf9fc 29982+/* final actions when unmounting a file system */
29983+static void aufs_put_super(struct super_block *sb)
29984+{
29985+ struct au_sbinfo *sbinfo;
29986+
29987+ sbinfo = au_sbi(sb);
062440b3
AM
29988+ if (sbinfo)
29989+ kobject_put(&sbinfo->si_kobj);
1facf9fc 29990+}
29991+
29992+/* ---------------------------------------------------------------------- */
29993+
79b8bda9
AM
29994+void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
29995+ struct super_block *sb, void *arg)
7f207e10
AM
29996+{
29997+ void *array;
076b876e 29998+ unsigned long long n, sz;
7f207e10
AM
29999+
30000+ array = NULL;
30001+ n = 0;
30002+ if (!*hint)
30003+ goto out;
30004+
30005+ if (*hint > ULLONG_MAX / sizeof(array)) {
30006+ array = ERR_PTR(-EMFILE);
30007+ pr_err("hint %llu\n", *hint);
30008+ goto out;
30009+ }
30010+
076b876e
AM
30011+ sz = sizeof(array) * *hint;
30012+ array = kzalloc(sz, GFP_NOFS);
7f207e10 30013+ if (unlikely(!array))
076b876e 30014+ array = vzalloc(sz);
7f207e10
AM
30015+ if (unlikely(!array)) {
30016+ array = ERR_PTR(-ENOMEM);
30017+ goto out;
30018+ }
30019+
79b8bda9 30020+ n = cb(sb, array, *hint, arg);
7f207e10
AM
30021+ AuDebugOn(n > *hint);
30022+
30023+out:
30024+ *hint = n;
30025+ return array;
30026+}
30027+
79b8bda9 30028+static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
7f207e10
AM
30029+ unsigned long long max __maybe_unused,
30030+ void *arg)
30031+{
30032+ unsigned long long n;
30033+ struct inode **p, *inode;
30034+ struct list_head *head;
30035+
30036+ n = 0;
30037+ p = a;
30038+ head = arg;
79b8bda9 30039+ spin_lock(&sb->s_inode_list_lock);
7f207e10 30040+ list_for_each_entry(inode, head, i_sb_list) {
5afbbe0d
AM
30041+ if (!au_is_bad_inode(inode)
30042+ && au_ii(inode)->ii_btop >= 0) {
2cbb1c4b
JR
30043+ spin_lock(&inode->i_lock);
30044+ if (atomic_read(&inode->i_count)) {
30045+ au_igrab(inode);
30046+ *p++ = inode;
30047+ n++;
30048+ AuDebugOn(n > max);
30049+ }
30050+ spin_unlock(&inode->i_lock);
7f207e10
AM
30051+ }
30052+ }
79b8bda9 30053+ spin_unlock(&sb->s_inode_list_lock);
7f207e10
AM
30054+
30055+ return n;
30056+}
30057+
30058+struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
30059+{
acd2b654
AM
30060+ struct au_sbinfo *sbi;
30061+
30062+ sbi = au_sbi(sb);
30063+ *max = au_lcnt_read(&sbi->si_ninodes, /*do_rev*/1);
79b8bda9 30064+ return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
7f207e10
AM
30065+}
30066+
30067+void au_iarray_free(struct inode **a, unsigned long long max)
30068+{
30069+ unsigned long long ull;
30070+
30071+ for (ull = 0; ull < max; ull++)
30072+ iput(a[ull]);
be52b249 30073+ kvfree(a);
7f207e10
AM
30074+}
30075+
30076+/* ---------------------------------------------------------------------- */
30077+
1facf9fc 30078+/*
30079+ * refresh dentry and inode at remount time.
30080+ */
027c5e7a
AM
30081+/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
30082+static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
30083+ struct dentry *parent)
1facf9fc 30084+{
30085+ int err;
1facf9fc 30086+
30087+ di_write_lock_child(dentry);
1facf9fc 30088+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a
AM
30089+ err = au_refresh_dentry(dentry, parent);
30090+ if (!err && dir_flags)
5527c038 30091+ au_hn_reset(d_inode(dentry), dir_flags);
1facf9fc 30092+ di_read_unlock(parent, AuLock_IR);
1facf9fc 30093+ di_write_unlock(dentry);
30094+
30095+ return err;
30096+}
30097+
027c5e7a
AM
30098+static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
30099+ struct au_sbinfo *sbinfo,
b95c5147 30100+ const unsigned int dir_flags, unsigned int do_idop)
1facf9fc 30101+{
027c5e7a
AM
30102+ int err;
30103+ struct dentry *parent;
027c5e7a
AM
30104+
30105+ err = 0;
30106+ parent = dget_parent(dentry);
30107+ if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
5527c038
JR
30108+ if (d_really_is_positive(dentry)) {
30109+ if (!d_is_dir(dentry))
027c5e7a
AM
30110+ err = au_do_refresh(dentry, /*dir_flags*/0,
30111+ parent);
30112+ else {
30113+ err = au_do_refresh(dentry, dir_flags, parent);
30114+ if (unlikely(err))
30115+ au_fset_si(sbinfo, FAILED_REFRESH_DIR);
30116+ }
30117+ } else
30118+ err = au_do_refresh(dentry, /*dir_flags*/0, parent);
30119+ AuDbgDentry(dentry);
30120+ }
30121+ dput(parent);
30122+
79b8bda9 30123+ if (!err) {
b95c5147 30124+ if (do_idop)
79b8bda9
AM
30125+ au_refresh_dop(dentry, /*force_reval*/0);
30126+ } else
30127+ au_refresh_dop(dentry, /*force_reval*/1);
30128+
027c5e7a
AM
30129+ AuTraceErr(err);
30130+ return err;
1facf9fc 30131+}
30132+
b95c5147 30133+static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
1facf9fc 30134+{
30135+ int err, i, j, ndentry, e;
027c5e7a 30136+ unsigned int sigen;
1facf9fc 30137+ struct au_dcsub_pages dpages;
30138+ struct au_dpage *dpage;
027c5e7a
AM
30139+ struct dentry **dentries, *d;
30140+ struct au_sbinfo *sbinfo;
30141+ struct dentry *root = sb->s_root;
5527c038 30142+ const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
1facf9fc 30143+
b95c5147 30144+ if (do_idop)
79b8bda9
AM
30145+ au_refresh_dop(root, /*force_reval*/0);
30146+
027c5e7a
AM
30147+ err = au_dpages_init(&dpages, GFP_NOFS);
30148+ if (unlikely(err))
1facf9fc 30149+ goto out;
027c5e7a
AM
30150+ err = au_dcsub_pages(&dpages, root, NULL, NULL);
30151+ if (unlikely(err))
1facf9fc 30152+ goto out_dpages;
1facf9fc 30153+
027c5e7a
AM
30154+ sigen = au_sigen(sb);
30155+ sbinfo = au_sbi(sb);
30156+ for (i = 0; i < dpages.ndpage; i++) {
1facf9fc 30157+ dpage = dpages.dpages + i;
30158+ dentries = dpage->dentries;
30159+ ndentry = dpage->ndentry;
027c5e7a 30160+ for (j = 0; j < ndentry; j++) {
1facf9fc 30161+ d = dentries[j];
79b8bda9 30162+ e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
b95c5147 30163+ do_idop);
027c5e7a
AM
30164+ if (unlikely(e && !err))
30165+ err = e;
30166+ /* go on even err */
1facf9fc 30167+ }
30168+ }
30169+
4f0767ce 30170+out_dpages:
1facf9fc 30171+ au_dpages_free(&dpages);
4f0767ce 30172+out:
1facf9fc 30173+ return err;
30174+}
30175+
b95c5147 30176+static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
1facf9fc 30177+{
027c5e7a
AM
30178+ int err, e;
30179+ unsigned int sigen;
30180+ unsigned long long max, ull;
30181+ struct inode *inode, **array;
1facf9fc 30182+
027c5e7a
AM
30183+ array = au_iarray_alloc(sb, &max);
30184+ err = PTR_ERR(array);
30185+ if (IS_ERR(array))
30186+ goto out;
1facf9fc 30187+
30188+ err = 0;
027c5e7a
AM
30189+ sigen = au_sigen(sb);
30190+ for (ull = 0; ull < max; ull++) {
30191+ inode = array[ull];
076b876e
AM
30192+ if (unlikely(!inode))
30193+ break;
b95c5147
AM
30194+
30195+ e = 0;
30196+ ii_write_lock_child(inode);
537831f9 30197+ if (au_iigen(inode, NULL) != sigen) {
027c5e7a 30198+ e = au_refresh_hinode_self(inode);
1facf9fc 30199+ if (unlikely(e)) {
b95c5147 30200+ au_refresh_iop(inode, /*force_getattr*/1);
027c5e7a 30201+ pr_err("error %d, i%lu\n", e, inode->i_ino);
1facf9fc 30202+ if (!err)
30203+ err = e;
30204+ /* go on even if err */
30205+ }
30206+ }
b95c5147
AM
30207+ if (!e && do_idop)
30208+ au_refresh_iop(inode, /*force_getattr*/0);
30209+ ii_write_unlock(inode);
1facf9fc 30210+ }
30211+
027c5e7a 30212+ au_iarray_free(array, max);
1facf9fc 30213+
4f0767ce 30214+out:
1facf9fc 30215+ return err;
30216+}
30217+
b95c5147 30218+static void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
1facf9fc 30219+{
027c5e7a
AM
30220+ int err, e;
30221+ unsigned int udba;
5afbbe0d 30222+ aufs_bindex_t bindex, bbot;
1facf9fc 30223+ struct dentry *root;
30224+ struct inode *inode;
027c5e7a 30225+ struct au_branch *br;
79b8bda9 30226+ struct au_sbinfo *sbi;
1facf9fc 30227+
30228+ au_sigen_inc(sb);
79b8bda9
AM
30229+ sbi = au_sbi(sb);
30230+ au_fclr_si(sbi, FAILED_REFRESH_DIR);
1facf9fc 30231+
30232+ root = sb->s_root;
30233+ DiMustNoWaiters(root);
5527c038 30234+ inode = d_inode(root);
1facf9fc 30235+ IiMustNoWaiters(inode);
1facf9fc 30236+
027c5e7a 30237+ udba = au_opt_udba(sb);
5afbbe0d
AM
30238+ bbot = au_sbbot(sb);
30239+ for (bindex = 0; bindex <= bbot; bindex++) {
027c5e7a
AM
30240+ br = au_sbr(sb, bindex);
30241+ err = au_hnotify_reset_br(udba, br, br->br_perm);
1facf9fc 30242+ if (unlikely(err))
027c5e7a
AM
30243+ AuIOErr("hnotify failed on br %d, %d, ignored\n",
30244+ bindex, err);
30245+ /* go on even if err */
1facf9fc 30246+ }
027c5e7a 30247+ au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
1facf9fc 30248+
b95c5147 30249+ if (do_idop) {
79b8bda9
AM
30250+ if (au_ftest_si(sbi, NO_DREVAL)) {
30251+ AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
30252+ sb->s_d_op = &aufs_dop_noreval;
b95c5147
AM
30253+ AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
30254+ sbi->si_iop_array = aufs_iop_nogetattr;
79b8bda9
AM
30255+ } else {
30256+ AuDebugOn(sb->s_d_op == &aufs_dop);
30257+ sb->s_d_op = &aufs_dop;
b95c5147
AM
30258+ AuDebugOn(sbi->si_iop_array == aufs_iop);
30259+ sbi->si_iop_array = aufs_iop;
79b8bda9 30260+ }
062440b3 30261+ pr_info("reset to %ps and %ps\n",
b95c5147 30262+ sb->s_d_op, sbi->si_iop_array);
79b8bda9
AM
30263+ }
30264+
027c5e7a 30265+ di_write_unlock(root);
b95c5147
AM
30266+ err = au_refresh_d(sb, do_idop);
30267+ e = au_refresh_i(sb, do_idop);
027c5e7a
AM
30268+ if (unlikely(e && !err))
30269+ err = e;
1facf9fc 30270+ /* aufs_write_lock() calls ..._child() */
30271+ di_write_lock_child(root);
027c5e7a
AM
30272+
30273+ au_cpup_attr_all(inode, /*force*/1);
30274+
30275+ if (unlikely(err))
30276+ AuIOErr("refresh failed, ignored, %d\n", err);
1facf9fc 30277+}
30278+
30279+/* stop extra interpretation of errno in mount(8), and strange error messages */
30280+static int cvt_err(int err)
30281+{
30282+ AuTraceErr(err);
30283+
30284+ switch (err) {
30285+ case -ENOENT:
30286+ case -ENOTDIR:
30287+ case -EEXIST:
30288+ case -EIO:
30289+ err = -EINVAL;
30290+ }
30291+ return err;
30292+}
30293+
30294+static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
30295+{
4a4d8108
AM
30296+ int err, do_dx;
30297+ unsigned int mntflags;
be52b249
AM
30298+ struct au_opts opts = {
30299+ .opt = NULL
30300+ };
1facf9fc 30301+ struct dentry *root;
30302+ struct inode *inode;
30303+ struct au_sbinfo *sbinfo;
30304+
30305+ err = 0;
30306+ root = sb->s_root;
30307+ if (!data || !*data) {
e49829fe
JR
30308+ err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
30309+ if (!err) {
30310+ di_write_lock_child(root);
30311+ err = au_opts_verify(sb, *flags, /*pending*/0);
30312+ aufs_write_unlock(root);
30313+ }
1facf9fc 30314+ goto out;
30315+ }
30316+
30317+ err = -ENOMEM;
1facf9fc 30318+ opts.opt = (void *)__get_free_page(GFP_NOFS);
30319+ if (unlikely(!opts.opt))
30320+ goto out;
30321+ opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
30322+ opts.flags = AuOpts_REMOUNT;
30323+ opts.sb_flags = *flags;
30324+
30325+ /* parse it before aufs lock */
30326+ err = au_opts_parse(sb, data, &opts);
30327+ if (unlikely(err))
30328+ goto out_opts;
30329+
30330+ sbinfo = au_sbi(sb);
5527c038 30331+ inode = d_inode(root);
febd17d6 30332+ inode_lock(inode);
e49829fe
JR
30333+ err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
30334+ if (unlikely(err))
30335+ goto out_mtx;
30336+ di_write_lock_child(root);
1facf9fc 30337+
30338+ /* au_opts_remount() may return an error */
30339+ err = au_opts_remount(sb, &opts);
30340+ au_opts_free(&opts);
30341+
027c5e7a 30342+ if (au_ftest_opts(opts.flags, REFRESH))
b95c5147 30343+ au_remount_refresh(sb, au_ftest_opts(opts.flags, REFRESH_IDOP));
1facf9fc 30344+
4a4d8108
AM
30345+ if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
30346+ mntflags = au_mntflags(sb);
30347+ do_dx = !!au_opt_test(mntflags, DIO);
30348+ au_dy_arefresh(do_dx);
30349+ }
30350+
076b876e 30351+ au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
1facf9fc 30352+ aufs_write_unlock(root);
953406b4 30353+
e49829fe 30354+out_mtx:
febd17d6 30355+ inode_unlock(inode);
4f0767ce 30356+out_opts:
1c60b727 30357+ free_page((unsigned long)opts.opt);
4f0767ce 30358+out:
1facf9fc 30359+ err = cvt_err(err);
30360+ AuTraceErr(err);
30361+ return err;
30362+}
30363+
4a4d8108 30364+static const struct super_operations aufs_sop = {
1facf9fc 30365+ .alloc_inode = aufs_alloc_inode,
30366+ .destroy_inode = aufs_destroy_inode,
b752ccd1 30367+ /* always deleting, no clearing */
1facf9fc 30368+ .drop_inode = generic_delete_inode,
30369+ .show_options = aufs_show_options,
30370+ .statfs = aufs_statfs,
30371+ .put_super = aufs_put_super,
537831f9 30372+ .sync_fs = aufs_sync_fs,
1facf9fc 30373+ .remount_fs = aufs_remount_fs
30374+};
30375+
30376+/* ---------------------------------------------------------------------- */
30377+
30378+static int alloc_root(struct super_block *sb)
30379+{
30380+ int err;
30381+ struct inode *inode;
30382+ struct dentry *root;
30383+
30384+ err = -ENOMEM;
30385+ inode = au_iget_locked(sb, AUFS_ROOT_INO);
30386+ err = PTR_ERR(inode);
30387+ if (IS_ERR(inode))
30388+ goto out;
30389+
b95c5147 30390+ inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
1facf9fc 30391+ inode->i_fop = &aufs_dir_fop;
30392+ inode->i_mode = S_IFDIR;
9dbd164d 30393+ set_nlink(inode, 2);
1facf9fc 30394+ unlock_new_inode(inode);
30395+
92d182d2 30396+ root = d_make_root(inode);
1facf9fc 30397+ if (unlikely(!root))
92d182d2 30398+ goto out;
1facf9fc 30399+ err = PTR_ERR(root);
30400+ if (IS_ERR(root))
92d182d2 30401+ goto out;
1facf9fc 30402+
4a4d8108 30403+ err = au_di_init(root);
1facf9fc 30404+ if (!err) {
30405+ sb->s_root = root;
30406+ return 0; /* success */
30407+ }
30408+ dput(root);
1facf9fc 30409+
4f0767ce 30410+out:
1facf9fc 30411+ return err;
1facf9fc 30412+}
30413+
30414+static int aufs_fill_super(struct super_block *sb, void *raw_data,
30415+ int silent __maybe_unused)
30416+{
30417+ int err;
be52b249
AM
30418+ struct au_opts opts = {
30419+ .opt = NULL
30420+ };
79b8bda9 30421+ struct au_sbinfo *sbinfo;
1facf9fc 30422+ struct dentry *root;
30423+ struct inode *inode;
30424+ char *arg = raw_data;
30425+
30426+ if (unlikely(!arg || !*arg)) {
30427+ err = -EINVAL;
4a4d8108 30428+ pr_err("no arg\n");
1facf9fc 30429+ goto out;
30430+ }
30431+
30432+ err = -ENOMEM;
1facf9fc 30433+ opts.opt = (void *)__get_free_page(GFP_NOFS);
30434+ if (unlikely(!opts.opt))
30435+ goto out;
30436+ opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
30437+ opts.sb_flags = sb->s_flags;
30438+
30439+ err = au_si_alloc(sb);
30440+ if (unlikely(err))
30441+ goto out_opts;
79b8bda9 30442+ sbinfo = au_sbi(sb);
1facf9fc 30443+
30444+ /* all timestamps always follow the ones on the branch */
2121bcd9 30445+ sb->s_flags |= SB_NOATIME | SB_NODIRATIME;
be118d29 30446+ sb->s_flags |= SB_I_VERSION; /* do we really need this? */
1facf9fc 30447+ sb->s_op = &aufs_sop;
027c5e7a 30448+ sb->s_d_op = &aufs_dop;
1facf9fc 30449+ sb->s_magic = AUFS_SUPER_MAGIC;
30450+ sb->s_maxbytes = 0;
c1595e42 30451+ sb->s_stack_depth = 1;
1facf9fc 30452+ au_export_init(sb);
f2c43d5f 30453+ au_xattr_init(sb);
1facf9fc 30454+
30455+ err = alloc_root(sb);
30456+ if (unlikely(err)) {
30457+ si_write_unlock(sb);
30458+ goto out_info;
30459+ }
30460+ root = sb->s_root;
5527c038 30461+ inode = d_inode(root);
1facf9fc 30462+
30463+ /*
30464+ * actually we can parse options regardless aufs lock here.
30465+ * but at remount time, parsing must be done before aufs lock.
30466+ * so we follow the same rule.
30467+ */
30468+ ii_write_lock_parent(inode);
30469+ aufs_write_unlock(root);
30470+ err = au_opts_parse(sb, arg, &opts);
30471+ if (unlikely(err))
30472+ goto out_root;
30473+
30474+ /* lock vfs_inode first, then aufs. */
febd17d6 30475+ inode_lock(inode);
1facf9fc 30476+ aufs_write_lock(root);
30477+ err = au_opts_mount(sb, &opts);
30478+ au_opts_free(&opts);
79b8bda9
AM
30479+ if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
30480+ sb->s_d_op = &aufs_dop_noreval;
062440b3 30481+ pr_info("%ps\n", sb->s_d_op);
79b8bda9 30482+ au_refresh_dop(root, /*force_reval*/0);
b95c5147
AM
30483+ sbinfo->si_iop_array = aufs_iop_nogetattr;
30484+ au_refresh_iop(inode, /*force_getattr*/0);
79b8bda9 30485+ }
1facf9fc 30486+ aufs_write_unlock(root);
febd17d6 30487+ inode_unlock(inode);
4a4d8108
AM
30488+ if (!err)
30489+ goto out_opts; /* success */
1facf9fc 30490+
4f0767ce 30491+out_root:
1facf9fc 30492+ dput(root);
30493+ sb->s_root = NULL;
4f0767ce 30494+out_info:
79b8bda9 30495+ kobject_put(&sbinfo->si_kobj);
1facf9fc 30496+ sb->s_fs_info = NULL;
4f0767ce 30497+out_opts:
1c60b727 30498+ free_page((unsigned long)opts.opt);
4f0767ce 30499+out:
1facf9fc 30500+ AuTraceErr(err);
30501+ err = cvt_err(err);
30502+ AuTraceErr(err);
30503+ return err;
30504+}
30505+
30506+/* ---------------------------------------------------------------------- */
30507+
027c5e7a
AM
30508+static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
30509+ const char *dev_name __maybe_unused,
30510+ void *raw_data)
1facf9fc 30511+{
027c5e7a 30512+ struct dentry *root;
1facf9fc 30513+
30514+ /* all timestamps always follow the ones on the branch */
30515+ /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
027c5e7a
AM
30516+ root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
30517+ if (IS_ERR(root))
30518+ goto out;
30519+
062440b3 30520+ au_sbilist_add(root->d_sb);
027c5e7a
AM
30521+
30522+out:
30523+ return root;
1facf9fc 30524+}
30525+
e49829fe
JR
30526+static void aufs_kill_sb(struct super_block *sb)
30527+{
30528+ struct au_sbinfo *sbinfo;
30529+
30530+ sbinfo = au_sbi(sb);
30531+ if (sbinfo) {
30532+ au_sbilist_del(sb);
30533+ aufs_write_lock(sb->s_root);
076b876e 30534+ au_fhsm_fin(sb);
e49829fe
JR
30535+ if (sbinfo->si_wbr_create_ops->fin)
30536+ sbinfo->si_wbr_create_ops->fin(sb);
30537+ if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
30538+ au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
b95c5147 30539+ au_remount_refresh(sb, /*do_idop*/0);
e49829fe
JR
30540+ }
30541+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
30542+ au_plink_put(sb, /*verbose*/1);
30543+ au_xino_clr(sb);
8b6a4947 30544+ au_dr_opt_flush(sb);
1e00d052 30545+ sbinfo->si_sb = NULL;
e49829fe 30546+ aufs_write_unlock(sb->s_root);
e49829fe
JR
30547+ au_nwt_flush(&sbinfo->si_nowait);
30548+ }
98d9a5b1 30549+ kill_anon_super(sb);
e49829fe
JR
30550+}
30551+
1facf9fc 30552+struct file_system_type aufs_fs_type = {
30553+ .name = AUFS_FSTYPE,
c06a8ce3
AM
30554+ /* a race between rename and others */
30555+ .fs_flags = FS_RENAME_DOES_D_MOVE,
027c5e7a 30556+ .mount = aufs_mount,
e49829fe 30557+ .kill_sb = aufs_kill_sb,
1facf9fc 30558+ /* no need to __module_get() and module_put(). */
30559+ .owner = THIS_MODULE,
30560+};
7f207e10
AM
30561diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
30562--- /usr/share/empty/fs/aufs/super.h 1970-01-01 01:00:00.000000000 +0100
0b487229 30563+++ linux/fs/aufs/super.h 2021-02-22 23:30:37.702680911 +0100
acd2b654 30564@@ -0,0 +1,589 @@
062440b3 30565+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 30566+/*
0b487229 30567+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 30568+ *
30569+ * This program, aufs is free software; you can redistribute it and/or modify
30570+ * it under the terms of the GNU General Public License as published by
30571+ * the Free Software Foundation; either version 2 of the License, or
30572+ * (at your option) any later version.
dece6358
AM
30573+ *
30574+ * This program is distributed in the hope that it will be useful,
30575+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
30576+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30577+ * GNU General Public License for more details.
30578+ *
30579+ * You should have received a copy of the GNU General Public License
523b37e3 30580+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 30581+ */
30582+
30583+/*
30584+ * super_block operations
30585+ */
30586+
30587+#ifndef __AUFS_SUPER_H__
30588+#define __AUFS_SUPER_H__
30589+
30590+#ifdef __KERNEL__
30591+
30592+#include <linux/fs.h>
5527c038 30593+#include <linux/kobject.h>
8b6a4947 30594+#include "hbl.h"
acd2b654 30595+#include "lcnt.h"
1facf9fc 30596+#include "rwsem.h"
1facf9fc 30597+#include "wkq.h"
30598+
1facf9fc 30599+/* policies to select one among multiple writable branches */
30600+struct au_wbr_copyup_operations {
30601+ int (*copyup)(struct dentry *dentry);
30602+};
30603+
392086de
AM
30604+#define AuWbr_DIR 1 /* target is a dir */
30605+#define AuWbr_PARENT (1 << 1) /* always require a parent */
30606+
30607+#define au_ftest_wbr(flags, name) ((flags) & AuWbr_##name)
30608+#define au_fset_wbr(flags, name) { (flags) |= AuWbr_##name; }
30609+#define au_fclr_wbr(flags, name) { (flags) &= ~AuWbr_##name; }
30610+
1facf9fc 30611+struct au_wbr_create_operations {
392086de 30612+ int (*create)(struct dentry *dentry, unsigned int flags);
1facf9fc 30613+ int (*init)(struct super_block *sb);
30614+ int (*fin)(struct super_block *sb);
30615+};
30616+
30617+struct au_wbr_mfs {
30618+ struct mutex mfs_lock; /* protect this structure */
30619+ unsigned long mfs_jiffy;
30620+ unsigned long mfs_expire;
30621+ aufs_bindex_t mfs_bindex;
30622+
30623+ unsigned long long mfsrr_bytes;
30624+ unsigned long long mfsrr_watermark;
30625+};
30626+
86dc4139
AM
30627+#define AuPlink_NHASH 100
30628+static inline int au_plink_hash(ino_t ino)
30629+{
30630+ return ino % AuPlink_NHASH;
30631+}
30632+
076b876e
AM
30633+/* File-based Hierarchical Storage Management */
30634+struct au_fhsm {
30635+#ifdef CONFIG_AUFS_FHSM
30636+ /* allow only one process who can receive the notification */
30637+ spinlock_t fhsm_spin;
30638+ pid_t fhsm_pid;
30639+ wait_queue_head_t fhsm_wqh;
30640+ atomic_t fhsm_readable;
30641+
c1595e42 30642+ /* these are protected by si_rwsem */
076b876e 30643+ unsigned long fhsm_expire;
c1595e42 30644+ aufs_bindex_t fhsm_bottom;
076b876e
AM
30645+#endif
30646+};
30647+
1facf9fc 30648+struct au_branch;
30649+struct au_sbinfo {
30650+ /* nowait tasks in the system-wide workqueue */
30651+ struct au_nowait_tasks si_nowait;
30652+
b752ccd1 30653+ /*
acd2b654 30654+ * tried sb->s_umount, but failed due to the dependency between i_mutex.
b752ccd1
AM
30655+ * rwsem for au_sbinfo is necessary.
30656+ */
dece6358 30657+ struct au_rwsem si_rwsem;
1facf9fc 30658+
7f207e10 30659+ /*
523b37e3
AM
30660+ * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
30661+ * remount.
7f207e10 30662+ */
acd2b654 30663+ au_lcnt_t si_ninodes, si_nfiles;
7f207e10 30664+
1facf9fc 30665+ /* branch management */
30666+ unsigned int si_generation;
30667+
2000de60 30668+ /* see AuSi_ flags */
1facf9fc 30669+ unsigned char au_si_status;
30670+
5afbbe0d 30671+ aufs_bindex_t si_bbot;
7f207e10
AM
30672+
30673+ /* dirty trick to keep br_id plus */
30674+ unsigned int si_last_br_id :
30675+ sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
1facf9fc 30676+ struct au_branch **si_branch;
30677+
30678+ /* policy to select a writable branch */
30679+ unsigned char si_wbr_copyup;
30680+ unsigned char si_wbr_create;
30681+ struct au_wbr_copyup_operations *si_wbr_copyup_ops;
30682+ struct au_wbr_create_operations *si_wbr_create_ops;
30683+
30684+ /* round robin */
30685+ atomic_t si_wbr_rr_next;
30686+
30687+ /* most free space */
30688+ struct au_wbr_mfs si_wbr_mfs;
30689+
076b876e
AM
30690+ /* File-based Hierarchical Storage Management */
30691+ struct au_fhsm si_fhsm;
30692+
1facf9fc 30693+ /* mount flags */
30694+ /* include/asm-ia64/siginfo.h defines a macro named si_flags */
30695+ unsigned int si_mntflags;
30696+
30697+ /* external inode number (bitmap and translation table) */
5527c038
JR
30698+ vfs_readf_t si_xread;
30699+ vfs_writef_t si_xwrite;
acd2b654
AM
30700+ loff_t si_ximaxent; /* max entries in a xino */
30701+
1facf9fc 30702+ struct file *si_xib;
30703+ struct mutex si_xib_mtx; /* protect xib members */
30704+ unsigned long *si_xib_buf;
30705+ unsigned long si_xib_last_pindex;
30706+ int si_xib_next_bit;
acd2b654 30707+
392086de
AM
30708+ unsigned long si_xino_jiffy;
30709+ unsigned long si_xino_expire;
1facf9fc 30710+ /* reserved for future use */
30711+ /* unsigned long long si_xib_limit; */ /* Max xib file size */
30712+
30713+#ifdef CONFIG_AUFS_EXPORT
30714+ /* i_generation */
acd2b654 30715+ /* todo: make xigen file an array to support many inode numbers */
1facf9fc 30716+ struct file *si_xigen;
30717+ atomic_t si_xigen_next;
30718+#endif
30719+
acd2b654 30720+ /* dirty trick to support atomic_open */
8b6a4947 30721+ struct hlist_bl_head si_aopen;
b912730e 30722+
1facf9fc 30723+ /* vdir parameters */
e49829fe 30724+ unsigned long si_rdcache; /* max cache time in jiffies */
1facf9fc 30725+ unsigned int si_rdblk; /* deblk size */
30726+ unsigned int si_rdhash; /* hash size */
30727+
30728+ /*
30729+ * If the number of whiteouts are larger than si_dirwh, leave all of
30730+ * them after au_whtmp_ren to reduce the cost of rmdir(2).
30731+ * future fsck.aufs or kernel thread will remove them later.
30732+ * Otherwise, remove all whiteouts and the dir in rmdir(2).
30733+ */
30734+ unsigned int si_dirwh;
30735+
1facf9fc 30736+ /* pseudo_link list */
8b6a4947 30737+ struct hlist_bl_head si_plink[AuPlink_NHASH];
1facf9fc 30738+ wait_queue_head_t si_plink_wq;
4a4d8108 30739+ spinlock_t si_plink_maint_lock;
e49829fe 30740+ pid_t si_plink_maint_pid;
1facf9fc 30741+
523b37e3 30742+ /* file list */
8b6a4947 30743+ struct hlist_bl_head si_files;
523b37e3 30744+
b95c5147 30745+ /* with/without getattr, brother of sb->s_d_op */
0b487229 30746+ const struct inode_operations *si_iop_array;
b95c5147 30747+
1facf9fc 30748+ /*
30749+ * sysfs and lifetime management.
30750+ * this is not a small structure and it may be a waste of memory in case
acd2b654 30751+ * of sysfs is disabled, particularly when many aufs-es are mounted.
1facf9fc 30752+ * but using sysfs is majority.
30753+ */
30754+ struct kobject si_kobj;
30755+#ifdef CONFIG_DEBUG_FS
86dc4139
AM
30756+ struct dentry *si_dbgaufs;
30757+ struct dentry *si_dbgaufs_plink;
30758+ struct dentry *si_dbgaufs_xib;
1facf9fc 30759+#ifdef CONFIG_AUFS_EXPORT
30760+ struct dentry *si_dbgaufs_xigen;
30761+#endif
30762+#endif
30763+
e49829fe 30764+#ifdef CONFIG_AUFS_SBILIST
8b6a4947 30765+ struct hlist_bl_node si_list;
e49829fe
JR
30766+#endif
30767+
1facf9fc 30768+ /* dirty, necessary for unmounting, sysfs and sysrq */
30769+ struct super_block *si_sb;
30770+};
30771+
dece6358
AM
30772+/* sbinfo status flags */
30773+/*
30774+ * set true when refresh_dirs() failed at remount time.
30775+ * then try refreshing dirs at access time again.
062440b3 30776+ * if it is false, refreshing dirs at access time is unnecessary
dece6358 30777+ */
027c5e7a 30778+#define AuSi_FAILED_REFRESH_DIR 1
076b876e 30779+#define AuSi_FHSM (1 << 1) /* fhsm is active now */
79b8bda9 30780+#define AuSi_NO_DREVAL (1 << 2) /* disable all d_revalidate */
076b876e
AM
30781+
30782+#ifndef CONFIG_AUFS_FHSM
30783+#undef AuSi_FHSM
30784+#define AuSi_FHSM 0
30785+#endif
30786+
dece6358
AM
30787+static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
30788+ unsigned int flag)
30789+{
30790+ AuRwMustAnyLock(&sbi->si_rwsem);
30791+ return sbi->au_si_status & flag;
30792+}
30793+#define au_ftest_si(sbinfo, name) au_do_ftest_si(sbinfo, AuSi_##name)
30794+#define au_fset_si(sbinfo, name) do { \
30795+ AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30796+ (sbinfo)->au_si_status |= AuSi_##name; \
30797+} while (0)
30798+#define au_fclr_si(sbinfo, name) do { \
30799+ AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30800+ (sbinfo)->au_si_status &= ~AuSi_##name; \
30801+} while (0)
30802+
1facf9fc 30803+/* ---------------------------------------------------------------------- */
30804+
30805+/* policy to select one among writable branches */
4a4d8108
AM
30806+#define AuWbrCopyup(sbinfo, ...) \
30807+ ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
30808+#define AuWbrCreate(sbinfo, ...) \
30809+ ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
1facf9fc 30810+
30811+/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
30812+#define AuLock_DW 1 /* write-lock dentry */
30813+#define AuLock_IR (1 << 1) /* read-lock inode */
30814+#define AuLock_IW (1 << 2) /* write-lock inode */
30815+#define AuLock_FLUSH (1 << 3) /* wait for 'nowait' tasks */
b95c5147 30816+#define AuLock_DIRS (1 << 4) /* target is a pair of dirs */
f2c43d5f 30817+ /* except RENAME_EXCHANGE */
e49829fe
JR
30818+#define AuLock_NOPLM (1 << 5) /* return err in plm mode */
30819+#define AuLock_NOPLMW (1 << 6) /* wait for plm mode ends */
027c5e7a 30820+#define AuLock_GEN (1 << 7) /* test digen/iigen */
1facf9fc 30821+#define au_ftest_lock(flags, name) ((flags) & AuLock_##name)
7f207e10
AM
30822+#define au_fset_lock(flags, name) \
30823+ do { (flags) |= AuLock_##name; } while (0)
30824+#define au_fclr_lock(flags, name) \
30825+ do { (flags) &= ~AuLock_##name; } while (0)
1facf9fc 30826+
30827+/* ---------------------------------------------------------------------- */
30828+
30829+/* super.c */
30830+extern struct file_system_type aufs_fs_type;
30831+struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
79b8bda9
AM
30832+typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
30833+ unsigned long long max, void *arg);
79b8bda9
AM
30834+void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30835+ struct super_block *sb, void *arg);
7f207e10
AM
30836+struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
30837+void au_iarray_free(struct inode **a, unsigned long long max);
1facf9fc 30838+
30839+/* sbinfo.c */
30840+void au_si_free(struct kobject *kobj);
30841+int au_si_alloc(struct super_block *sb);
e2f27e51 30842+int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
1facf9fc 30843+
30844+unsigned int au_sigen_inc(struct super_block *sb);
30845+aufs_bindex_t au_new_br_id(struct super_block *sb);
30846+
e49829fe
JR
30847+int si_read_lock(struct super_block *sb, int flags);
30848+int si_write_lock(struct super_block *sb, int flags);
30849+int aufs_read_lock(struct dentry *dentry, int flags);
1facf9fc 30850+void aufs_read_unlock(struct dentry *dentry, int flags);
30851+void aufs_write_lock(struct dentry *dentry);
30852+void aufs_write_unlock(struct dentry *dentry);
e49829fe 30853+int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
1facf9fc 30854+void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
30855+
30856+/* wbr_policy.c */
30857+extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
30858+extern struct au_wbr_create_operations au_wbr_create_ops[];
30859+int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
c2b27bf2 30860+int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
5afbbe0d 30861+int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
c2b27bf2
AM
30862+
30863+/* mvdown.c */
30864+int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
1facf9fc 30865+
076b876e
AM
30866+#ifdef CONFIG_AUFS_FHSM
30867+/* fhsm.c */
30868+
30869+static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
30870+{
30871+ pid_t pid;
30872+
30873+ spin_lock(&fhsm->fhsm_spin);
30874+ pid = fhsm->fhsm_pid;
30875+ spin_unlock(&fhsm->fhsm_spin);
30876+
30877+ return pid;
30878+}
30879+
30880+void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
30881+void au_fhsm_wrote_all(struct super_block *sb, int force);
30882+int au_fhsm_fd(struct super_block *sb, int oflags);
30883+int au_fhsm_br_alloc(struct au_branch *br);
c1595e42 30884+void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
076b876e
AM
30885+void au_fhsm_fin(struct super_block *sb);
30886+void au_fhsm_init(struct au_sbinfo *sbinfo);
30887+void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
30888+void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
30889+#else
30890+AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
30891+ int force)
30892+AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
30893+AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
c1595e42
JR
30894+AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
30895+AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
30896+AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
076b876e
AM
30897+AuStubVoid(au_fhsm_fin, struct super_block *sb)
30898+AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
30899+AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
30900+AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
30901+#endif
30902+
1facf9fc 30903+/* ---------------------------------------------------------------------- */
30904+
30905+static inline struct au_sbinfo *au_sbi(struct super_block *sb)
30906+{
30907+ return sb->s_fs_info;
30908+}
30909+
30910+/* ---------------------------------------------------------------------- */
30911+
30912+#ifdef CONFIG_AUFS_EXPORT
a2a7ad62 30913+int au_test_nfsd(void);
1facf9fc 30914+void au_export_init(struct super_block *sb);
b752ccd1 30915+void au_xigen_inc(struct inode *inode);
1facf9fc 30916+int au_xigen_new(struct inode *inode);
062440b3 30917+int au_xigen_set(struct super_block *sb, struct path *path);
1facf9fc 30918+void au_xigen_clr(struct super_block *sb);
30919+
30920+static inline int au_busy_or_stale(void)
30921+{
b752ccd1 30922+ if (!au_test_nfsd())
1facf9fc 30923+ return -EBUSY;
30924+ return -ESTALE;
30925+}
30926+#else
b752ccd1 30927+AuStubInt0(au_test_nfsd, void)
a2a7ad62 30928+AuStubVoid(au_export_init, struct super_block *sb)
b752ccd1 30929+AuStubVoid(au_xigen_inc, struct inode *inode)
4a4d8108 30930+AuStubInt0(au_xigen_new, struct inode *inode)
062440b3 30931+AuStubInt0(au_xigen_set, struct super_block *sb, struct path *path)
4a4d8108 30932+AuStubVoid(au_xigen_clr, struct super_block *sb)
c1595e42 30933+AuStub(int, au_busy_or_stale, return -EBUSY, void)
1facf9fc 30934+#endif /* CONFIG_AUFS_EXPORT */
30935+
30936+/* ---------------------------------------------------------------------- */
30937+
e49829fe
JR
30938+#ifdef CONFIG_AUFS_SBILIST
30939+/* module.c */
8b6a4947 30940+extern struct hlist_bl_head au_sbilist;
e49829fe
JR
30941+
30942+static inline void au_sbilist_init(void)
30943+{
8b6a4947 30944+ INIT_HLIST_BL_HEAD(&au_sbilist);
e49829fe
JR
30945+}
30946+
30947+static inline void au_sbilist_add(struct super_block *sb)
30948+{
8b6a4947 30949+ au_hbl_add(&au_sbi(sb)->si_list, &au_sbilist);
e49829fe
JR
30950+}
30951+
30952+static inline void au_sbilist_del(struct super_block *sb)
30953+{
8b6a4947 30954+ au_hbl_del(&au_sbi(sb)->si_list, &au_sbilist);
e49829fe 30955+}
53392da6
AM
30956+
30957+#ifdef CONFIG_AUFS_MAGIC_SYSRQ
30958+static inline void au_sbilist_lock(void)
30959+{
8b6a4947 30960+ hlist_bl_lock(&au_sbilist);
53392da6
AM
30961+}
30962+
30963+static inline void au_sbilist_unlock(void)
30964+{
8b6a4947 30965+ hlist_bl_unlock(&au_sbilist);
53392da6
AM
30966+}
30967+#define AuGFP_SBILIST GFP_ATOMIC
30968+#else
30969+AuStubVoid(au_sbilist_lock, void)
30970+AuStubVoid(au_sbilist_unlock, void)
30971+#define AuGFP_SBILIST GFP_NOFS
30972+#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
e49829fe
JR
30973+#else
30974+AuStubVoid(au_sbilist_init, void)
c1595e42
JR
30975+AuStubVoid(au_sbilist_add, struct super_block *sb)
30976+AuStubVoid(au_sbilist_del, struct super_block *sb)
53392da6
AM
30977+AuStubVoid(au_sbilist_lock, void)
30978+AuStubVoid(au_sbilist_unlock, void)
30979+#define AuGFP_SBILIST GFP_NOFS
e49829fe
JR
30980+#endif
30981+
30982+/* ---------------------------------------------------------------------- */
30983+
1facf9fc 30984+static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
30985+{
dece6358 30986+ /*
c1595e42 30987+ * This function is a dynamic '__init' function actually,
dece6358
AM
30988+ * so the tiny check for si_rwsem is unnecessary.
30989+ */
30990+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
1facf9fc 30991+#ifdef CONFIG_DEBUG_FS
30992+ sbinfo->si_dbgaufs = NULL;
86dc4139 30993+ sbinfo->si_dbgaufs_plink = NULL;
1facf9fc 30994+ sbinfo->si_dbgaufs_xib = NULL;
30995+#ifdef CONFIG_AUFS_EXPORT
30996+ sbinfo->si_dbgaufs_xigen = NULL;
30997+#endif
30998+#endif
30999+}
31000+
31001+/* ---------------------------------------------------------------------- */
31002+
a2654f78
AM
31003+/* current->atomic_flags */
31004+/* this value should never corrupt the ones defined in linux/sched.h */
31005+#define PFA_AUFS 7
31006+
31007+TASK_PFA_TEST(AUFS, test_aufs) /* task_test_aufs */
31008+TASK_PFA_SET(AUFS, aufs) /* task_set_aufs */
31009+TASK_PFA_CLEAR(AUFS, aufs) /* task_clear_aufs */
b752ccd1
AM
31010+
31011+static inline int si_pid_test(struct super_block *sb)
31012+{
a2654f78 31013+ return !!task_test_aufs(current);
b752ccd1
AM
31014+}
31015+
31016+static inline void si_pid_clr(struct super_block *sb)
31017+{
a2654f78
AM
31018+ AuDebugOn(!task_test_aufs(current));
31019+ task_clear_aufs(current);
b752ccd1
AM
31020+}
31021+
a2654f78
AM
31022+static inline void si_pid_set(struct super_block *sb)
31023+{
31024+ AuDebugOn(task_test_aufs(current));
31025+ task_set_aufs(current);
31026+}
febd17d6 31027+
b752ccd1
AM
31028+/* ---------------------------------------------------------------------- */
31029+
1facf9fc 31030+/* lock superblock. mainly for entry point functions */
8b6a4947
AM
31031+#define __si_read_lock(sb) au_rw_read_lock(&au_sbi(sb)->si_rwsem)
31032+#define __si_write_lock(sb) au_rw_write_lock(&au_sbi(sb)->si_rwsem)
31033+#define __si_read_trylock(sb) au_rw_read_trylock(&au_sbi(sb)->si_rwsem)
31034+#define __si_write_trylock(sb) au_rw_write_trylock(&au_sbi(sb)->si_rwsem)
31035+/*
31036+#define __si_read_trylock_nested(sb) \
31037+ au_rw_read_trylock_nested(&au_sbi(sb)->si_rwsem)
31038+#define __si_write_trylock_nested(sb) \
31039+ au_rw_write_trylock_nested(&au_sbi(sb)->si_rwsem)
31040+*/
31041+
31042+#define __si_read_unlock(sb) au_rw_read_unlock(&au_sbi(sb)->si_rwsem)
31043+#define __si_write_unlock(sb) au_rw_write_unlock(&au_sbi(sb)->si_rwsem)
31044+#define __si_downgrade_lock(sb) au_rw_dgrade_lock(&au_sbi(sb)->si_rwsem)
1facf9fc 31045+
dece6358
AM
31046+#define SiMustNoWaiters(sb) AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
31047+#define SiMustAnyLock(sb) AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
31048+#define SiMustWriteLock(sb) AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
31049+
b752ccd1
AM
31050+static inline void si_noflush_read_lock(struct super_block *sb)
31051+{
31052+ __si_read_lock(sb);
31053+ si_pid_set(sb);
31054+}
31055+
31056+static inline int si_noflush_read_trylock(struct super_block *sb)
31057+{
076b876e
AM
31058+ int locked;
31059+
31060+ locked = __si_read_trylock(sb);
b752ccd1
AM
31061+ if (locked)
31062+ si_pid_set(sb);
31063+ return locked;
31064+}
31065+
31066+static inline void si_noflush_write_lock(struct super_block *sb)
31067+{
31068+ __si_write_lock(sb);
31069+ si_pid_set(sb);
31070+}
31071+
31072+static inline int si_noflush_write_trylock(struct super_block *sb)
31073+{
076b876e
AM
31074+ int locked;
31075+
31076+ locked = __si_write_trylock(sb);
b752ccd1
AM
31077+ if (locked)
31078+ si_pid_set(sb);
31079+ return locked;
31080+}
31081+
7e9cd9fe 31082+#if 0 /* reserved */
1facf9fc 31083+static inline int si_read_trylock(struct super_block *sb, int flags)
31084+{
31085+ if (au_ftest_lock(flags, FLUSH))
31086+ au_nwt_flush(&au_sbi(sb)->si_nowait);
31087+ return si_noflush_read_trylock(sb);
31088+}
e49829fe 31089+#endif
1facf9fc 31090+
b752ccd1
AM
31091+static inline void si_read_unlock(struct super_block *sb)
31092+{
31093+ si_pid_clr(sb);
31094+ __si_read_unlock(sb);
31095+}
31096+
7e9cd9fe 31097+#if 0 /* reserved */
1facf9fc 31098+static inline int si_write_trylock(struct super_block *sb, int flags)
31099+{
31100+ if (au_ftest_lock(flags, FLUSH))
31101+ au_nwt_flush(&au_sbi(sb)->si_nowait);
31102+ return si_noflush_write_trylock(sb);
31103+}
b752ccd1
AM
31104+#endif
31105+
31106+static inline void si_write_unlock(struct super_block *sb)
31107+{
31108+ si_pid_clr(sb);
31109+ __si_write_unlock(sb);
31110+}
31111+
7e9cd9fe 31112+#if 0 /* reserved */
b752ccd1
AM
31113+static inline void si_downgrade_lock(struct super_block *sb)
31114+{
31115+ __si_downgrade_lock(sb);
31116+}
31117+#endif
1facf9fc 31118+
31119+/* ---------------------------------------------------------------------- */
31120+
5afbbe0d 31121+static inline aufs_bindex_t au_sbbot(struct super_block *sb)
1facf9fc 31122+{
dece6358 31123+ SiMustAnyLock(sb);
5afbbe0d 31124+ return au_sbi(sb)->si_bbot;
1facf9fc 31125+}
31126+
31127+static inline unsigned int au_mntflags(struct super_block *sb)
31128+{
dece6358 31129+ SiMustAnyLock(sb);
1facf9fc 31130+ return au_sbi(sb)->si_mntflags;
31131+}
31132+
31133+static inline unsigned int au_sigen(struct super_block *sb)
31134+{
dece6358 31135+ SiMustAnyLock(sb);
1facf9fc 31136+ return au_sbi(sb)->si_generation;
31137+}
31138+
31139+static inline struct au_branch *au_sbr(struct super_block *sb,
31140+ aufs_bindex_t bindex)
31141+{
dece6358 31142+ SiMustAnyLock(sb);
1facf9fc 31143+ return au_sbi(sb)->si_branch[0 + bindex];
31144+}
31145+
acd2b654 31146+static inline loff_t au_xi_maxent(struct super_block *sb)
1facf9fc 31147+{
dece6358 31148+ SiMustAnyLock(sb);
acd2b654 31149+ return au_sbi(sb)->si_ximaxent;
1facf9fc 31150+}
31151+
31152+#endif /* __KERNEL__ */
31153+#endif /* __AUFS_SUPER_H__ */
7f207e10
AM
31154diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
31155--- /usr/share/empty/fs/aufs/sysaufs.c 1970-01-01 01:00:00.000000000 +0100
0b487229 31156+++ linux/fs/aufs/sysaufs.c 2021-02-22 23:30:37.702680911 +0100
062440b3 31157@@ -0,0 +1,93 @@
cd7a4cd9 31158+// SPDX-License-Identifier: GPL-2.0
1facf9fc 31159+/*
0b487229 31160+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 31161+ *
31162+ * This program, aufs is free software; you can redistribute it and/or modify
31163+ * it under the terms of the GNU General Public License as published by
31164+ * the Free Software Foundation; either version 2 of the License, or
31165+ * (at your option) any later version.
dece6358
AM
31166+ *
31167+ * This program is distributed in the hope that it will be useful,
31168+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31169+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31170+ * GNU General Public License for more details.
31171+ *
31172+ * You should have received a copy of the GNU General Public License
523b37e3 31173+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31174+ */
31175+
31176+/*
31177+ * sysfs interface and lifetime management
31178+ * they are necessary regardless sysfs is disabled.
31179+ */
31180+
1facf9fc 31181+#include <linux/random.h>
1facf9fc 31182+#include "aufs.h"
31183+
31184+unsigned long sysaufs_si_mask;
e49829fe 31185+struct kset *sysaufs_kset;
1facf9fc 31186+
31187+#define AuSiAttr(_name) { \
31188+ .attr = { .name = __stringify(_name), .mode = 0444 }, \
31189+ .show = sysaufs_si_##_name, \
31190+}
31191+
31192+static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
31193+struct attribute *sysaufs_si_attrs[] = {
31194+ &sysaufs_si_attr_xi_path.attr,
31195+ NULL,
31196+};
31197+
4a4d8108 31198+static const struct sysfs_ops au_sbi_ops = {
1facf9fc 31199+ .show = sysaufs_si_show
31200+};
31201+
31202+static struct kobj_type au_sbi_ktype = {
31203+ .release = au_si_free,
31204+ .sysfs_ops = &au_sbi_ops,
31205+ .default_attrs = sysaufs_si_attrs
31206+};
31207+
31208+/* ---------------------------------------------------------------------- */
31209+
31210+int sysaufs_si_init(struct au_sbinfo *sbinfo)
31211+{
31212+ int err;
31213+
e49829fe 31214+ sbinfo->si_kobj.kset = sysaufs_kset;
1facf9fc 31215+ /* cf. sysaufs_name() */
31216+ err = kobject_init_and_add
e49829fe 31217+ (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
1facf9fc 31218+ SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
31219+
1facf9fc 31220+ return err;
31221+}
31222+
31223+void sysaufs_fin(void)
31224+{
e49829fe
JR
31225+ sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31226+ kset_unregister(sysaufs_kset);
1facf9fc 31227+}
31228+
31229+int __init sysaufs_init(void)
31230+{
31231+ int err;
31232+
31233+ do {
31234+ get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
31235+ } while (!sysaufs_si_mask);
31236+
4a4d8108 31237+ err = -EINVAL;
e49829fe
JR
31238+ sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
31239+ if (unlikely(!sysaufs_kset))
4a4d8108 31240+ goto out;
e49829fe
JR
31241+ err = PTR_ERR(sysaufs_kset);
31242+ if (IS_ERR(sysaufs_kset))
1facf9fc 31243+ goto out;
e49829fe 31244+ err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
062440b3 31245+ if (unlikely(err))
e49829fe 31246+ kset_unregister(sysaufs_kset);
1facf9fc 31247+
4f0767ce 31248+out:
1facf9fc 31249+ return err;
31250+}
7f207e10
AM
31251diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
31252--- /usr/share/empty/fs/aufs/sysaufs.h 1970-01-01 01:00:00.000000000 +0100
0b487229 31253+++ linux/fs/aufs/sysaufs.h 2021-02-22 23:30:37.702680911 +0100
062440b3
AM
31254@@ -0,0 +1,102 @@
31255+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 31256+/*
0b487229 31257+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 31258+ *
31259+ * This program, aufs is free software; you can redistribute it and/or modify
31260+ * it under the terms of the GNU General Public License as published by
31261+ * the Free Software Foundation; either version 2 of the License, or
31262+ * (at your option) any later version.
dece6358
AM
31263+ *
31264+ * This program is distributed in the hope that it will be useful,
31265+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31266+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31267+ * GNU General Public License for more details.
31268+ *
31269+ * You should have received a copy of the GNU General Public License
523b37e3 31270+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31271+ */
31272+
31273+/*
31274+ * sysfs interface and mount lifetime management
31275+ */
31276+
31277+#ifndef __SYSAUFS_H__
31278+#define __SYSAUFS_H__
31279+
31280+#ifdef __KERNEL__
31281+
1facf9fc 31282+#include <linux/sysfs.h>
1facf9fc 31283+#include "module.h"
31284+
dece6358
AM
31285+struct super_block;
31286+struct au_sbinfo;
31287+
1facf9fc 31288+struct sysaufs_si_attr {
31289+ struct attribute attr;
31290+ int (*show)(struct seq_file *seq, struct super_block *sb);
31291+};
31292+
31293+/* ---------------------------------------------------------------------- */
31294+
31295+/* sysaufs.c */
31296+extern unsigned long sysaufs_si_mask;
e49829fe 31297+extern struct kset *sysaufs_kset;
1facf9fc 31298+extern struct attribute *sysaufs_si_attrs[];
31299+int sysaufs_si_init(struct au_sbinfo *sbinfo);
31300+int __init sysaufs_init(void);
31301+void sysaufs_fin(void);
31302+
31303+/* ---------------------------------------------------------------------- */
31304+
31305+/* some people doesn't like to show a pointer in kernel */
31306+static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
31307+{
31308+ return sysaufs_si_mask ^ (unsigned long)sbinfo;
31309+}
31310+
31311+#define SysaufsSiNamePrefix "si_"
31312+#define SysaufsSiNameLen (sizeof(SysaufsSiNamePrefix) + 16)
31313+static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
31314+{
31315+ snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
31316+ sysaufs_si_id(sbinfo));
31317+}
31318+
31319+struct au_branch;
31320+#ifdef CONFIG_SYSFS
31321+/* sysfs.c */
31322+extern struct attribute_group *sysaufs_attr_group;
31323+
31324+int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
31325+ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31326+ char *buf);
076b876e
AM
31327+long au_brinfo_ioctl(struct file *file, unsigned long arg);
31328+#ifdef CONFIG_COMPAT
31329+long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
31330+#endif
1facf9fc 31331+
31332+void sysaufs_br_init(struct au_branch *br);
31333+void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
31334+void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
31335+
31336+#define sysaufs_brs_init() do {} while (0)
31337+
31338+#else
31339+#define sysaufs_attr_group NULL
31340+
4a4d8108 31341+AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
c1595e42
JR
31342+AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
31343+ struct attribute *attr, char *buf)
4a4d8108
AM
31344+AuStubVoid(sysaufs_br_init, struct au_branch *br)
31345+AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
31346+AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
1facf9fc 31347+
31348+static inline void sysaufs_brs_init(void)
31349+{
31350+ sysaufs_brs = 0;
31351+}
31352+
31353+#endif /* CONFIG_SYSFS */
31354+
31355+#endif /* __KERNEL__ */
31356+#endif /* __SYSAUFS_H__ */
7f207e10
AM
31357diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
31358--- /usr/share/empty/fs/aufs/sysfs.c 1970-01-01 01:00:00.000000000 +0100
0b487229 31359+++ linux/fs/aufs/sysfs.c 2021-02-22 23:30:37.702680911 +0100
062440b3 31360@@ -0,0 +1,373 @@
cd7a4cd9 31361+// SPDX-License-Identifier: GPL-2.0
1facf9fc 31362+/*
0b487229 31363+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 31364+ *
31365+ * This program, aufs is free software; you can redistribute it and/or modify
31366+ * it under the terms of the GNU General Public License as published by
31367+ * the Free Software Foundation; either version 2 of the License, or
31368+ * (at your option) any later version.
dece6358
AM
31369+ *
31370+ * This program is distributed in the hope that it will be useful,
31371+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31372+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31373+ * GNU General Public License for more details.
31374+ *
31375+ * You should have received a copy of the GNU General Public License
523b37e3 31376+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31377+ */
31378+
31379+/*
31380+ * sysfs interface
31381+ */
31382+
076b876e 31383+#include <linux/compat.h>
1facf9fc 31384+#include <linux/seq_file.h>
1facf9fc 31385+#include "aufs.h"
31386+
4a4d8108
AM
31387+#ifdef CONFIG_AUFS_FS_MODULE
31388+/* this entry violates the "one line per file" policy of sysfs */
31389+static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
31390+ char *buf)
31391+{
31392+ ssize_t err;
31393+ static char *conf =
31394+/* this file is generated at compiling */
31395+#include "conf.str"
31396+ ;
31397+
31398+ err = snprintf(buf, PAGE_SIZE, conf);
31399+ if (unlikely(err >= PAGE_SIZE))
31400+ err = -EFBIG;
31401+ return err;
31402+}
31403+
31404+static struct kobj_attribute au_config_attr = __ATTR_RO(config);
31405+#endif
31406+
1facf9fc 31407+static struct attribute *au_attr[] = {
4a4d8108
AM
31408+#ifdef CONFIG_AUFS_FS_MODULE
31409+ &au_config_attr.attr,
31410+#endif
1facf9fc 31411+ NULL, /* need to NULL terminate the list of attributes */
31412+};
31413+
31414+static struct attribute_group sysaufs_attr_group_body = {
31415+ .attrs = au_attr
31416+};
31417+
31418+struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
31419+
31420+/* ---------------------------------------------------------------------- */
31421+
31422+int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
31423+{
31424+ int err;
31425+
dece6358
AM
31426+ SiMustAnyLock(sb);
31427+
1facf9fc 31428+ err = 0;
31429+ if (au_opt_test(au_mntflags(sb), XINO)) {
31430+ err = au_xino_path(seq, au_sbi(sb)->si_xib);
31431+ seq_putc(seq, '\n');
31432+ }
31433+ return err;
31434+}
31435+
31436+/*
31437+ * the lifetime of branch is independent from the entry under sysfs.
31438+ * sysfs handles the lifetime of the entry, and never call ->show() after it is
31439+ * unlinked.
31440+ */
31441+static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
392086de 31442+ aufs_bindex_t bindex, int idx)
1facf9fc 31443+{
1e00d052 31444+ int err;
1facf9fc 31445+ struct path path;
31446+ struct dentry *root;
31447+ struct au_branch *br;
076b876e 31448+ au_br_perm_str_t perm;
1facf9fc 31449+
31450+ AuDbg("b%d\n", bindex);
31451+
1e00d052 31452+ err = 0;
1facf9fc 31453+ root = sb->s_root;
31454+ di_read_lock_parent(root, !AuLock_IR);
31455+ br = au_sbr(sb, bindex);
392086de
AM
31456+
31457+ switch (idx) {
31458+ case AuBrSysfs_BR:
31459+ path.mnt = au_br_mnt(br);
31460+ path.dentry = au_h_dptr(root, bindex);
79b8bda9
AM
31461+ err = au_seq_path(seq, &path);
31462+ if (!err) {
31463+ au_optstr_br_perm(&perm, br->br_perm);
31464+ seq_printf(seq, "=%s\n", perm.a);
31465+ }
392086de
AM
31466+ break;
31467+ case AuBrSysfs_BRID:
79b8bda9 31468+ seq_printf(seq, "%d\n", br->br_id);
392086de
AM
31469+ break;
31470+ }
076b876e 31471+ di_read_unlock(root, !AuLock_IR);
79b8bda9 31472+ if (unlikely(err || seq_has_overflowed(seq)))
076b876e 31473+ err = -E2BIG;
392086de 31474+
1e00d052 31475+ return err;
1facf9fc 31476+}
31477+
31478+/* ---------------------------------------------------------------------- */
31479+
31480+static struct seq_file *au_seq(char *p, ssize_t len)
31481+{
31482+ struct seq_file *seq;
31483+
31484+ seq = kzalloc(sizeof(*seq), GFP_NOFS);
31485+ if (seq) {
31486+ /* mutex_init(&seq.lock); */
31487+ seq->buf = p;
31488+ seq->size = len;
31489+ return seq; /* success */
31490+ }
31491+
31492+ seq = ERR_PTR(-ENOMEM);
31493+ return seq;
31494+}
31495+
392086de
AM
31496+#define SysaufsBr_PREFIX "br"
31497+#define SysaufsBrid_PREFIX "brid"
1facf9fc 31498+
31499+/* todo: file size may exceed PAGE_SIZE */
31500+ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
1308ab2a 31501+ char *buf)
1facf9fc 31502+{
31503+ ssize_t err;
392086de 31504+ int idx;
1facf9fc 31505+ long l;
5afbbe0d 31506+ aufs_bindex_t bbot;
1facf9fc 31507+ struct au_sbinfo *sbinfo;
31508+ struct super_block *sb;
31509+ struct seq_file *seq;
31510+ char *name;
31511+ struct attribute **cattr;
31512+
31513+ sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
31514+ sb = sbinfo->si_sb;
1308ab2a 31515+
31516+ /*
31517+ * prevent a race condition between sysfs and aufs.
31518+ * for instance, sysfs_file_read() calls sysfs_get_active_two() which
31519+ * prohibits maintaining the sysfs entries.
31520+ * hew we acquire read lock after sysfs_get_active_two().
31521+ * on the other hand, the remount process may maintain the sysfs/aufs
31522+ * entries after acquiring write lock.
31523+ * it can cause a deadlock.
31524+ * simply we gave up processing read here.
31525+ */
31526+ err = -EBUSY;
31527+ if (unlikely(!si_noflush_read_trylock(sb)))
31528+ goto out;
1facf9fc 31529+
31530+ seq = au_seq(buf, PAGE_SIZE);
31531+ err = PTR_ERR(seq);
31532+ if (IS_ERR(seq))
1308ab2a 31533+ goto out_unlock;
1facf9fc 31534+
31535+ name = (void *)attr->name;
31536+ cattr = sysaufs_si_attrs;
31537+ while (*cattr) {
31538+ if (!strcmp(name, (*cattr)->name)) {
31539+ err = container_of(*cattr, struct sysaufs_si_attr, attr)
31540+ ->show(seq, sb);
31541+ goto out_seq;
31542+ }
31543+ cattr++;
31544+ }
31545+
392086de
AM
31546+ if (!strncmp(name, SysaufsBrid_PREFIX,
31547+ sizeof(SysaufsBrid_PREFIX) - 1)) {
31548+ idx = AuBrSysfs_BRID;
31549+ name += sizeof(SysaufsBrid_PREFIX) - 1;
31550+ } else if (!strncmp(name, SysaufsBr_PREFIX,
31551+ sizeof(SysaufsBr_PREFIX) - 1)) {
31552+ idx = AuBrSysfs_BR;
1facf9fc 31553+ name += sizeof(SysaufsBr_PREFIX) - 1;
392086de
AM
31554+ } else
31555+ BUG();
31556+
31557+ err = kstrtol(name, 10, &l);
31558+ if (!err) {
5afbbe0d
AM
31559+ bbot = au_sbbot(sb);
31560+ if (l <= bbot)
392086de
AM
31561+ err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
31562+ else
31563+ err = -ENOENT;
1facf9fc 31564+ }
1facf9fc 31565+
4f0767ce 31566+out_seq:
1facf9fc 31567+ if (!err) {
31568+ err = seq->count;
31569+ /* sysfs limit */
31570+ if (unlikely(err == PAGE_SIZE))
31571+ err = -EFBIG;
31572+ }
e49925d1 31573+ au_kfree_rcu(seq);
4f0767ce 31574+out_unlock:
1facf9fc 31575+ si_read_unlock(sb);
4f0767ce 31576+out:
1facf9fc 31577+ return err;
31578+}
31579+
31580+/* ---------------------------------------------------------------------- */
31581+
076b876e
AM
31582+static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
31583+{
31584+ int err;
31585+ int16_t brid;
5afbbe0d 31586+ aufs_bindex_t bindex, bbot;
076b876e
AM
31587+ size_t sz;
31588+ char *buf;
31589+ struct seq_file *seq;
31590+ struct au_branch *br;
31591+
31592+ si_read_lock(sb, AuLock_FLUSH);
5afbbe0d
AM
31593+ bbot = au_sbbot(sb);
31594+ err = bbot + 1;
076b876e
AM
31595+ if (!arg)
31596+ goto out;
31597+
31598+ err = -ENOMEM;
31599+ buf = (void *)__get_free_page(GFP_NOFS);
31600+ if (unlikely(!buf))
31601+ goto out;
31602+
31603+ seq = au_seq(buf, PAGE_SIZE);
31604+ err = PTR_ERR(seq);
31605+ if (IS_ERR(seq))
31606+ goto out_buf;
31607+
31608+ sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
5afbbe0d 31609+ for (bindex = 0; bindex <= bbot; bindex++, arg++) {
076b876e
AM
31610+ err = !access_ok(VERIFY_WRITE, arg, sizeof(*arg));
31611+ if (unlikely(err))
31612+ break;
31613+
31614+ br = au_sbr(sb, bindex);
31615+ brid = br->br_id;
31616+ BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
31617+ err = __put_user(brid, &arg->id);
31618+ if (unlikely(err))
31619+ break;
31620+
31621+ BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
31622+ err = __put_user(br->br_perm, &arg->perm);
31623+ if (unlikely(err))
31624+ break;
31625+
79b8bda9
AM
31626+ err = au_seq_path(seq, &br->br_path);
31627+ if (unlikely(err))
31628+ break;
31629+ seq_putc(seq, '\0');
31630+ if (!seq_has_overflowed(seq)) {
076b876e
AM
31631+ err = copy_to_user(arg->path, seq->buf, seq->count);
31632+ seq->count = 0;
31633+ if (unlikely(err))
31634+ break;
31635+ } else {
31636+ err = -E2BIG;
31637+ goto out_seq;
31638+ }
31639+ }
31640+ if (unlikely(err))
31641+ err = -EFAULT;
31642+
31643+out_seq:
e49925d1 31644+ au_kfree_rcu(seq);
076b876e 31645+out_buf:
1c60b727 31646+ free_page((unsigned long)buf);
076b876e
AM
31647+out:
31648+ si_read_unlock(sb);
31649+ return err;
31650+}
31651+
31652+long au_brinfo_ioctl(struct file *file, unsigned long arg)
31653+{
2000de60 31654+ return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
076b876e
AM
31655+}
31656+
31657+#ifdef CONFIG_COMPAT
31658+long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
31659+{
2000de60 31660+ return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
076b876e
AM
31661+}
31662+#endif
31663+
31664+/* ---------------------------------------------------------------------- */
31665+
1facf9fc 31666+void sysaufs_br_init(struct au_branch *br)
31667+{
392086de
AM
31668+ int i;
31669+ struct au_brsysfs *br_sysfs;
31670+ struct attribute *attr;
4a4d8108 31671+
392086de
AM
31672+ br_sysfs = br->br_sysfs;
31673+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31674+ attr = &br_sysfs->attr;
31675+ sysfs_attr_init(attr);
31676+ attr->name = br_sysfs->name;
cd7a4cd9 31677+ attr->mode = 0444;
392086de
AM
31678+ br_sysfs++;
31679+ }
1facf9fc 31680+}
31681+
31682+void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
31683+{
31684+ struct au_branch *br;
31685+ struct kobject *kobj;
392086de
AM
31686+ struct au_brsysfs *br_sysfs;
31687+ int i;
5afbbe0d 31688+ aufs_bindex_t bbot;
1facf9fc 31689+
1facf9fc 31690+ if (!sysaufs_brs)
31691+ return;
31692+
31693+ kobj = &au_sbi(sb)->si_kobj;
5afbbe0d
AM
31694+ bbot = au_sbbot(sb);
31695+ for (; bindex <= bbot; bindex++) {
1facf9fc 31696+ br = au_sbr(sb, bindex);
392086de
AM
31697+ br_sysfs = br->br_sysfs;
31698+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31699+ sysfs_remove_file(kobj, &br_sysfs->attr);
31700+ br_sysfs++;
31701+ }
1facf9fc 31702+ }
31703+}
31704+
31705+void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
31706+{
392086de 31707+ int err, i;
5afbbe0d 31708+ aufs_bindex_t bbot;
1facf9fc 31709+ struct kobject *kobj;
31710+ struct au_branch *br;
392086de 31711+ struct au_brsysfs *br_sysfs;
1facf9fc 31712+
1facf9fc 31713+ if (!sysaufs_brs)
31714+ return;
31715+
31716+ kobj = &au_sbi(sb)->si_kobj;
5afbbe0d
AM
31717+ bbot = au_sbbot(sb);
31718+ for (; bindex <= bbot; bindex++) {
1facf9fc 31719+ br = au_sbr(sb, bindex);
392086de
AM
31720+ br_sysfs = br->br_sysfs;
31721+ snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
31722+ SysaufsBr_PREFIX "%d", bindex);
31723+ snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
31724+ SysaufsBrid_PREFIX "%d", bindex);
31725+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31726+ err = sysfs_create_file(kobj, &br_sysfs->attr);
31727+ if (unlikely(err))
31728+ pr_warn("failed %s under sysfs(%d)\n",
31729+ br_sysfs->name, err);
31730+ br_sysfs++;
31731+ }
1facf9fc 31732+ }
31733+}
7f207e10
AM
31734diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
31735--- /usr/share/empty/fs/aufs/sysrq.c 1970-01-01 01:00:00.000000000 +0100
0b487229
JR
31736+++ linux/fs/aufs/sysrq.c 2021-02-22 23:30:37.702680911 +0100
31737@@ -0,0 +1,149 @@
cd7a4cd9 31738+// SPDX-License-Identifier: GPL-2.0
1facf9fc 31739+/*
0b487229 31740+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 31741+ *
31742+ * This program, aufs is free software; you can redistribute it and/or modify
31743+ * it under the terms of the GNU General Public License as published by
31744+ * the Free Software Foundation; either version 2 of the License, or
31745+ * (at your option) any later version.
dece6358
AM
31746+ *
31747+ * This program is distributed in the hope that it will be useful,
31748+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31749+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31750+ * GNU General Public License for more details.
31751+ *
31752+ * You should have received a copy of the GNU General Public License
523b37e3 31753+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31754+ */
31755+
31756+/*
acd2b654 31757+ * magic sysrq handler
1facf9fc 31758+ */
31759+
1facf9fc 31760+/* #include <linux/sysrq.h> */
027c5e7a 31761+#include <linux/writeback.h>
1facf9fc 31762+#include "aufs.h"
31763+
31764+/* ---------------------------------------------------------------------- */
31765+
31766+static void sysrq_sb(struct super_block *sb)
31767+{
31768+ char *plevel;
31769+ struct au_sbinfo *sbinfo;
31770+ struct file *file;
8b6a4947
AM
31771+ struct hlist_bl_head *files;
31772+ struct hlist_bl_node *pos;
523b37e3 31773+ struct au_finfo *finfo;
0b487229 31774+ struct inode *i;
1facf9fc 31775+
31776+ plevel = au_plevel;
31777+ au_plevel = KERN_WARNING;
1facf9fc 31778+
4a4d8108 31779+ /* since we define pr_fmt, call printk directly */
c06a8ce3
AM
31780+#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
31781+
31782+ sbinfo = au_sbi(sb);
4a4d8108 31783+ printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
c06a8ce3 31784+ pr("superblock\n");
1facf9fc 31785+ au_dpri_sb(sb);
027c5e7a 31786+
0b487229 31787+#if 0 /* reserved */
027c5e7a
AM
31788+ do {
31789+ int err, i, j, ndentry;
31790+ struct au_dcsub_pages dpages;
31791+ struct au_dpage *dpage;
31792+
31793+ err = au_dpages_init(&dpages, GFP_ATOMIC);
31794+ if (unlikely(err))
31795+ break;
31796+ err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
31797+ if (!err)
31798+ for (i = 0; i < dpages.ndpage; i++) {
31799+ dpage = dpages.dpages + i;
31800+ ndentry = dpage->ndentry;
31801+ for (j = 0; j < ndentry; j++)
31802+ au_dpri_dentry(dpage->dentries[j]);
31803+ }
31804+ au_dpages_free(&dpages);
31805+ } while (0);
31806+#endif
31807+
0b487229
JR
31808+ pr("isolated inode\n");
31809+ spin_lock(&sb->s_inode_list_lock);
31810+ list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
31811+ spin_lock(&i->i_lock);
31812+ if (hlist_empty(&i->i_dentry))
31813+ au_dpri_inode(i);
31814+ spin_unlock(&i->i_lock);
027c5e7a 31815+ }
0b487229
JR
31816+ spin_unlock(&sb->s_inode_list_lock);
31817+
c06a8ce3 31818+ pr("files\n");
523b37e3 31819+ files = &au_sbi(sb)->si_files;
8b6a4947
AM
31820+ hlist_bl_lock(files);
31821+ hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
4a4d8108 31822+ umode_t mode;
076b876e 31823+
523b37e3 31824+ file = finfo->fi_file;
c06a8ce3 31825+ mode = file_inode(file)->i_mode;
38d290e6 31826+ if (!special_file(mode))
1facf9fc 31827+ au_dpri_file(file);
523b37e3 31828+ }
8b6a4947 31829+ hlist_bl_unlock(files);
c06a8ce3 31830+ pr("done\n");
1facf9fc 31831+
c06a8ce3 31832+#undef pr
1facf9fc 31833+ au_plevel = plevel;
1facf9fc 31834+}
31835+
31836+/* ---------------------------------------------------------------------- */
31837+
31838+/* module parameter */
31839+static char *aufs_sysrq_key = "a";
cd7a4cd9 31840+module_param_named(sysrq, aufs_sysrq_key, charp, 0444);
1facf9fc 31841+MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
31842+
0c5527e5 31843+static void au_sysrq(int key __maybe_unused)
1facf9fc 31844+{
1facf9fc 31845+ struct au_sbinfo *sbinfo;
8b6a4947 31846+ struct hlist_bl_node *pos;
1facf9fc 31847+
027c5e7a 31848+ lockdep_off();
53392da6 31849+ au_sbilist_lock();
8b6a4947 31850+ hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
1facf9fc 31851+ sysrq_sb(sbinfo->si_sb);
53392da6 31852+ au_sbilist_unlock();
027c5e7a 31853+ lockdep_on();
1facf9fc 31854+}
31855+
31856+static struct sysrq_key_op au_sysrq_op = {
31857+ .handler = au_sysrq,
31858+ .help_msg = "Aufs",
31859+ .action_msg = "Aufs",
31860+ .enable_mask = SYSRQ_ENABLE_DUMP
31861+};
31862+
31863+/* ---------------------------------------------------------------------- */
31864+
31865+int __init au_sysrq_init(void)
31866+{
31867+ int err;
31868+ char key;
31869+
31870+ err = -1;
31871+ key = *aufs_sysrq_key;
31872+ if ('a' <= key && key <= 'z')
31873+ err = register_sysrq_key(key, &au_sysrq_op);
31874+ if (unlikely(err))
4a4d8108 31875+ pr_err("err %d, sysrq=%c\n", err, key);
1facf9fc 31876+ return err;
31877+}
31878+
31879+void au_sysrq_fin(void)
31880+{
31881+ int err;
076b876e 31882+
1facf9fc 31883+ err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
31884+ if (unlikely(err))
4a4d8108 31885+ pr_err("err %d (ignored)\n", err);
1facf9fc 31886+}
7f207e10
AM
31887diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
31888--- /usr/share/empty/fs/aufs/vdir.c 1970-01-01 01:00:00.000000000 +0100
0b487229
JR
31889+++ linux/fs/aufs/vdir.c 2021-02-22 23:30:37.702680911 +0100
31890@@ -0,0 +1,896 @@
cd7a4cd9 31891+// SPDX-License-Identifier: GPL-2.0
1facf9fc 31892+/*
0b487229 31893+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 31894+ *
31895+ * This program, aufs is free software; you can redistribute it and/or modify
31896+ * it under the terms of the GNU General Public License as published by
31897+ * the Free Software Foundation; either version 2 of the License, or
31898+ * (at your option) any later version.
dece6358
AM
31899+ *
31900+ * This program is distributed in the hope that it will be useful,
31901+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31902+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31903+ * GNU General Public License for more details.
31904+ *
31905+ * You should have received a copy of the GNU General Public License
523b37e3 31906+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31907+ */
31908+
31909+/*
31910+ * virtual or vertical directory
31911+ */
31912+
0b487229 31913+#include <linux/iversion.h>
1facf9fc 31914+#include "aufs.h"
31915+
dece6358 31916+static unsigned int calc_size(int nlen)
1facf9fc 31917+{
dece6358 31918+ return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
1facf9fc 31919+}
31920+
31921+static int set_deblk_end(union au_vdir_deblk_p *p,
31922+ union au_vdir_deblk_p *deblk_end)
31923+{
31924+ if (calc_size(0) <= deblk_end->deblk - p->deblk) {
31925+ p->de->de_str.len = 0;
31926+ /* smp_mb(); */
31927+ return 0;
31928+ }
31929+ return -1; /* error */
31930+}
31931+
31932+/* returns true or false */
31933+static int is_deblk_end(union au_vdir_deblk_p *p,
31934+ union au_vdir_deblk_p *deblk_end)
31935+{
31936+ if (calc_size(0) <= deblk_end->deblk - p->deblk)
31937+ return !p->de->de_str.len;
31938+ return 1;
31939+}
31940+
31941+static unsigned char *last_deblk(struct au_vdir *vdir)
31942+{
31943+ return vdir->vd_deblk[vdir->vd_nblk - 1];
31944+}
31945+
31946+/* ---------------------------------------------------------------------- */
31947+
79b8bda9 31948+/* estimate the appropriate size for name hash table */
1308ab2a 31949+unsigned int au_rdhash_est(loff_t sz)
31950+{
31951+ unsigned int n;
31952+
31953+ n = UINT_MAX;
31954+ sz >>= 10;
31955+ if (sz < n)
31956+ n = sz;
31957+ if (sz < AUFS_RDHASH_DEF)
31958+ n = AUFS_RDHASH_DEF;
4a4d8108 31959+ /* pr_info("n %u\n", n); */
1308ab2a 31960+ return n;
31961+}
31962+
1facf9fc 31963+/*
31964+ * the allocated memory has to be freed by
dece6358 31965+ * au_nhash_wh_free() or au_nhash_de_free().
1facf9fc 31966+ */
dece6358 31967+int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
1facf9fc 31968+{
1facf9fc 31969+ struct hlist_head *head;
dece6358 31970+ unsigned int u;
076b876e 31971+ size_t sz;
1facf9fc 31972+
076b876e
AM
31973+ sz = sizeof(*nhash->nh_head) * num_hash;
31974+ head = kmalloc(sz, gfp);
dece6358
AM
31975+ if (head) {
31976+ nhash->nh_num = num_hash;
31977+ nhash->nh_head = head;
31978+ for (u = 0; u < num_hash; u++)
1facf9fc 31979+ INIT_HLIST_HEAD(head++);
dece6358 31980+ return 0; /* success */
1facf9fc 31981+ }
1facf9fc 31982+
dece6358 31983+ return -ENOMEM;
1facf9fc 31984+}
31985+
dece6358
AM
31986+static void nhash_count(struct hlist_head *head)
31987+{
0b487229 31988+#if 0 /* debugging */
dece6358
AM
31989+ unsigned long n;
31990+ struct hlist_node *pos;
31991+
31992+ n = 0;
31993+ hlist_for_each(pos, head)
31994+ n++;
4a4d8108 31995+ pr_info("%lu\n", n);
dece6358
AM
31996+#endif
31997+}
31998+
31999+static void au_nhash_wh_do_free(struct hlist_head *head)
1facf9fc 32000+{
c06a8ce3
AM
32001+ struct au_vdir_wh *pos;
32002+ struct hlist_node *node;
1facf9fc 32003+
c06a8ce3 32004+ hlist_for_each_entry_safe(pos, node, head, wh_hash)
e49925d1 32005+ au_kfree_rcu(pos);
1facf9fc 32006+}
32007+
dece6358 32008+static void au_nhash_de_do_free(struct hlist_head *head)
1facf9fc 32009+{
c06a8ce3
AM
32010+ struct au_vdir_dehstr *pos;
32011+ struct hlist_node *node;
1facf9fc 32012+
c06a8ce3 32013+ hlist_for_each_entry_safe(pos, node, head, hash)
1c60b727 32014+ au_cache_free_vdir_dehstr(pos);
1facf9fc 32015+}
32016+
dece6358
AM
32017+static void au_nhash_do_free(struct au_nhash *nhash,
32018+ void (*free)(struct hlist_head *head))
1facf9fc 32019+{
1308ab2a 32020+ unsigned int n;
1facf9fc 32021+ struct hlist_head *head;
1facf9fc 32022+
dece6358 32023+ n = nhash->nh_num;
1308ab2a 32024+ if (!n)
32025+ return;
32026+
dece6358 32027+ head = nhash->nh_head;
1308ab2a 32028+ while (n-- > 0) {
dece6358
AM
32029+ nhash_count(head);
32030+ free(head++);
1facf9fc 32031+ }
e49925d1 32032+ au_kfree_try_rcu(nhash->nh_head);
1facf9fc 32033+}
32034+
dece6358 32035+void au_nhash_wh_free(struct au_nhash *whlist)
1facf9fc 32036+{
dece6358
AM
32037+ au_nhash_do_free(whlist, au_nhash_wh_do_free);
32038+}
1facf9fc 32039+
dece6358
AM
32040+static void au_nhash_de_free(struct au_nhash *delist)
32041+{
32042+ au_nhash_do_free(delist, au_nhash_de_do_free);
1facf9fc 32043+}
32044+
32045+/* ---------------------------------------------------------------------- */
32046+
32047+int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
32048+ int limit)
32049+{
32050+ int num;
32051+ unsigned int u, n;
32052+ struct hlist_head *head;
c06a8ce3 32053+ struct au_vdir_wh *pos;
1facf9fc 32054+
32055+ num = 0;
32056+ n = whlist->nh_num;
32057+ head = whlist->nh_head;
1308ab2a 32058+ for (u = 0; u < n; u++, head++)
c06a8ce3
AM
32059+ hlist_for_each_entry(pos, head, wh_hash)
32060+ if (pos->wh_bindex == btgt && ++num > limit)
1facf9fc 32061+ return 1;
1facf9fc 32062+ return 0;
32063+}
32064+
32065+static struct hlist_head *au_name_hash(struct au_nhash *nhash,
dece6358 32066+ unsigned char *name,
1facf9fc 32067+ unsigned int len)
32068+{
dece6358
AM
32069+ unsigned int v;
32070+ /* const unsigned int magic_bit = 12; */
32071+
1308ab2a 32072+ AuDebugOn(!nhash->nh_num || !nhash->nh_head);
32073+
dece6358 32074+ v = 0;
f0c0a007
AM
32075+ if (len > 8)
32076+ len = 8;
dece6358
AM
32077+ while (len--)
32078+ v += *name++;
32079+ /* v = hash_long(v, magic_bit); */
32080+ v %= nhash->nh_num;
32081+ return nhash->nh_head + v;
32082+}
32083+
32084+static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
32085+ int nlen)
32086+{
32087+ return str->len == nlen && !memcmp(str->name, name, nlen);
1facf9fc 32088+}
32089+
32090+/* returns found or not */
dece6358 32091+int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
1facf9fc 32092+{
32093+ struct hlist_head *head;
c06a8ce3 32094+ struct au_vdir_wh *pos;
1facf9fc 32095+ struct au_vdir_destr *str;
32096+
dece6358 32097+ head = au_name_hash(whlist, name, nlen);
c06a8ce3
AM
32098+ hlist_for_each_entry(pos, head, wh_hash) {
32099+ str = &pos->wh_str;
1facf9fc 32100+ AuDbg("%.*s\n", str->len, str->name);
dece6358
AM
32101+ if (au_nhash_test_name(str, name, nlen))
32102+ return 1;
32103+ }
32104+ return 0;
32105+}
32106+
32107+/* returns found(true) or not */
32108+static int test_known(struct au_nhash *delist, char *name, int nlen)
32109+{
32110+ struct hlist_head *head;
c06a8ce3 32111+ struct au_vdir_dehstr *pos;
dece6358
AM
32112+ struct au_vdir_destr *str;
32113+
32114+ head = au_name_hash(delist, name, nlen);
c06a8ce3
AM
32115+ hlist_for_each_entry(pos, head, hash) {
32116+ str = pos->str;
dece6358
AM
32117+ AuDbg("%.*s\n", str->len, str->name);
32118+ if (au_nhash_test_name(str, name, nlen))
1facf9fc 32119+ return 1;
32120+ }
32121+ return 0;
32122+}
32123+
dece6358
AM
32124+static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
32125+ unsigned char d_type)
32126+{
32127+#ifdef CONFIG_AUFS_SHWH
32128+ wh->wh_ino = ino;
32129+ wh->wh_type = d_type;
32130+#endif
32131+}
32132+
32133+/* ---------------------------------------------------------------------- */
32134+
32135+int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
32136+ unsigned int d_type, aufs_bindex_t bindex,
32137+ unsigned char shwh)
1facf9fc 32138+{
32139+ int err;
32140+ struct au_vdir_destr *str;
32141+ struct au_vdir_wh *wh;
32142+
dece6358 32143+ AuDbg("%.*s\n", nlen, name);
1308ab2a 32144+ AuDebugOn(!whlist->nh_num || !whlist->nh_head);
32145+
1facf9fc 32146+ err = -ENOMEM;
dece6358 32147+ wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
1facf9fc 32148+ if (unlikely(!wh))
32149+ goto out;
32150+
32151+ err = 0;
32152+ wh->wh_bindex = bindex;
dece6358
AM
32153+ if (shwh)
32154+ au_shwh_init_wh(wh, ino, d_type);
1facf9fc 32155+ str = &wh->wh_str;
dece6358
AM
32156+ str->len = nlen;
32157+ memcpy(str->name, name, nlen);
32158+ hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
1facf9fc 32159+ /* smp_mb(); */
32160+
4f0767ce 32161+out:
1facf9fc 32162+ return err;
32163+}
32164+
1facf9fc 32165+static int append_deblk(struct au_vdir *vdir)
32166+{
32167+ int err;
dece6358 32168+ unsigned long ul;
1facf9fc 32169+ const unsigned int deblk_sz = vdir->vd_deblk_sz;
32170+ union au_vdir_deblk_p p, deblk_end;
32171+ unsigned char **o;
32172+
32173+ err = -ENOMEM;
e2f27e51
AM
32174+ o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
32175+ GFP_NOFS, /*may_shrink*/0);
1facf9fc 32176+ if (unlikely(!o))
32177+ goto out;
32178+
32179+ vdir->vd_deblk = o;
32180+ p.deblk = kmalloc(deblk_sz, GFP_NOFS);
32181+ if (p.deblk) {
32182+ ul = vdir->vd_nblk++;
32183+ vdir->vd_deblk[ul] = p.deblk;
32184+ vdir->vd_last.ul = ul;
32185+ vdir->vd_last.p.deblk = p.deblk;
32186+ deblk_end.deblk = p.deblk + deblk_sz;
32187+ err = set_deblk_end(&p, &deblk_end);
32188+ }
32189+
4f0767ce 32190+out:
1facf9fc 32191+ return err;
32192+}
32193+
dece6358
AM
32194+static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
32195+ unsigned int d_type, struct au_nhash *delist)
32196+{
32197+ int err;
32198+ unsigned int sz;
32199+ const unsigned int deblk_sz = vdir->vd_deblk_sz;
32200+ union au_vdir_deblk_p p, *room, deblk_end;
32201+ struct au_vdir_dehstr *dehstr;
32202+
32203+ p.deblk = last_deblk(vdir);
32204+ deblk_end.deblk = p.deblk + deblk_sz;
32205+ room = &vdir->vd_last.p;
32206+ AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
32207+ || !is_deblk_end(room, &deblk_end));
32208+
32209+ sz = calc_size(nlen);
32210+ if (unlikely(sz > deblk_end.deblk - room->deblk)) {
32211+ err = append_deblk(vdir);
32212+ if (unlikely(err))
32213+ goto out;
32214+
32215+ p.deblk = last_deblk(vdir);
32216+ deblk_end.deblk = p.deblk + deblk_sz;
32217+ /* smp_mb(); */
32218+ AuDebugOn(room->deblk != p.deblk);
32219+ }
32220+
32221+ err = -ENOMEM;
4a4d8108 32222+ dehstr = au_cache_alloc_vdir_dehstr();
dece6358
AM
32223+ if (unlikely(!dehstr))
32224+ goto out;
32225+
32226+ dehstr->str = &room->de->de_str;
32227+ hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
32228+ room->de->de_ino = ino;
32229+ room->de->de_type = d_type;
32230+ room->de->de_str.len = nlen;
32231+ memcpy(room->de->de_str.name, name, nlen);
32232+
32233+ err = 0;
32234+ room->deblk += sz;
32235+ if (unlikely(set_deblk_end(room, &deblk_end)))
32236+ err = append_deblk(vdir);
32237+ /* smp_mb(); */
32238+
4f0767ce 32239+out:
dece6358
AM
32240+ return err;
32241+}
32242+
32243+/* ---------------------------------------------------------------------- */
32244+
1c60b727 32245+void au_vdir_free(struct au_vdir *vdir)
dece6358
AM
32246+{
32247+ unsigned char **deblk;
32248+
32249+ deblk = vdir->vd_deblk;
1c60b727 32250+ while (vdir->vd_nblk--)
e49925d1
JR
32251+ au_kfree_try_rcu(*deblk++);
32252+ au_kfree_try_rcu(vdir->vd_deblk);
1c60b727 32253+ au_cache_free_vdir(vdir);
dece6358
AM
32254+}
32255+
1308ab2a 32256+static struct au_vdir *alloc_vdir(struct file *file)
1facf9fc 32257+{
32258+ struct au_vdir *vdir;
1308ab2a 32259+ struct super_block *sb;
1facf9fc 32260+ int err;
32261+
2000de60 32262+ sb = file->f_path.dentry->d_sb;
dece6358
AM
32263+ SiMustAnyLock(sb);
32264+
1facf9fc 32265+ err = -ENOMEM;
32266+ vdir = au_cache_alloc_vdir();
32267+ if (unlikely(!vdir))
32268+ goto out;
32269+
32270+ vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
32271+ if (unlikely(!vdir->vd_deblk))
32272+ goto out_free;
32273+
32274+ vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
1308ab2a 32275+ if (!vdir->vd_deblk_sz) {
79b8bda9 32276+ /* estimate the appropriate size for deblk */
1308ab2a 32277+ vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
4a4d8108 32278+ /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
1308ab2a 32279+ }
1facf9fc 32280+ vdir->vd_nblk = 0;
32281+ vdir->vd_version = 0;
32282+ vdir->vd_jiffy = 0;
32283+ err = append_deblk(vdir);
32284+ if (!err)
32285+ return vdir; /* success */
32286+
e49925d1 32287+ au_kfree_try_rcu(vdir->vd_deblk);
1facf9fc 32288+
4f0767ce 32289+out_free:
1c60b727 32290+ au_cache_free_vdir(vdir);
4f0767ce 32291+out:
1facf9fc 32292+ vdir = ERR_PTR(err);
32293+ return vdir;
32294+}
32295+
32296+static int reinit_vdir(struct au_vdir *vdir)
32297+{
32298+ int err;
32299+ union au_vdir_deblk_p p, deblk_end;
32300+
32301+ while (vdir->vd_nblk > 1) {
e49925d1 32302+ au_kfree_try_rcu(vdir->vd_deblk[vdir->vd_nblk - 1]);
1facf9fc 32303+ /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
32304+ vdir->vd_nblk--;
32305+ }
32306+ p.deblk = vdir->vd_deblk[0];
32307+ deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
32308+ err = set_deblk_end(&p, &deblk_end);
32309+ /* keep vd_dblk_sz */
32310+ vdir->vd_last.ul = 0;
32311+ vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32312+ vdir->vd_version = 0;
32313+ vdir->vd_jiffy = 0;
32314+ /* smp_mb(); */
32315+ return err;
32316+}
32317+
32318+/* ---------------------------------------------------------------------- */
32319+
1facf9fc 32320+#define AuFillVdir_CALLED 1
32321+#define AuFillVdir_WHABLE (1 << 1)
dece6358 32322+#define AuFillVdir_SHWH (1 << 2)
1facf9fc 32323+#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
7f207e10
AM
32324+#define au_fset_fillvdir(flags, name) \
32325+ do { (flags) |= AuFillVdir_##name; } while (0)
32326+#define au_fclr_fillvdir(flags, name) \
32327+ do { (flags) &= ~AuFillVdir_##name; } while (0)
1facf9fc 32328+
dece6358
AM
32329+#ifndef CONFIG_AUFS_SHWH
32330+#undef AuFillVdir_SHWH
32331+#define AuFillVdir_SHWH 0
32332+#endif
32333+
1facf9fc 32334+struct fillvdir_arg {
392086de 32335+ struct dir_context ctx;
1facf9fc 32336+ struct file *file;
32337+ struct au_vdir *vdir;
dece6358
AM
32338+ struct au_nhash delist;
32339+ struct au_nhash whlist;
1facf9fc 32340+ aufs_bindex_t bindex;
32341+ unsigned int flags;
32342+ int err;
32343+};
32344+
392086de 32345+static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
1facf9fc 32346+ loff_t offset __maybe_unused, u64 h_ino,
32347+ unsigned int d_type)
32348+{
392086de 32349+ struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
1facf9fc 32350+ char *name = (void *)__name;
32351+ struct super_block *sb;
1facf9fc 32352+ ino_t ino;
dece6358 32353+ const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
1facf9fc 32354+
1facf9fc 32355+ arg->err = 0;
2000de60 32356+ sb = arg->file->f_path.dentry->d_sb;
1facf9fc 32357+ au_fset_fillvdir(arg->flags, CALLED);
32358+ /* smp_mb(); */
dece6358 32359+ if (nlen <= AUFS_WH_PFX_LEN
1facf9fc 32360+ || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
dece6358
AM
32361+ if (test_known(&arg->delist, name, nlen)
32362+ || au_nhash_test_known_wh(&arg->whlist, name, nlen))
32363+ goto out; /* already exists or whiteouted */
1facf9fc 32364+
dece6358 32365+ arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
4a4d8108
AM
32366+ if (!arg->err) {
32367+ if (unlikely(nlen > AUFS_MAX_NAMELEN))
32368+ d_type = DT_UNKNOWN;
dece6358
AM
32369+ arg->err = append_de(arg->vdir, name, nlen, ino,
32370+ d_type, &arg->delist);
4a4d8108 32371+ }
1facf9fc 32372+ } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
32373+ name += AUFS_WH_PFX_LEN;
dece6358
AM
32374+ nlen -= AUFS_WH_PFX_LEN;
32375+ if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
32376+ goto out; /* already whiteouted */
1facf9fc 32377+
acd2b654 32378+ ino = 0; /* just to suppress a warning */
dece6358
AM
32379+ if (shwh)
32380+ arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
32381+ &ino);
4a4d8108
AM
32382+ if (!arg->err) {
32383+ if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
32384+ d_type = DT_UNKNOWN;
1facf9fc 32385+ arg->err = au_nhash_append_wh
dece6358
AM
32386+ (&arg->whlist, name, nlen, ino, d_type,
32387+ arg->bindex, shwh);
4a4d8108 32388+ }
1facf9fc 32389+ }
32390+
4f0767ce 32391+out:
1facf9fc 32392+ if (!arg->err)
32393+ arg->vdir->vd_jiffy = jiffies;
32394+ /* smp_mb(); */
32395+ AuTraceErr(arg->err);
32396+ return arg->err;
32397+}
32398+
dece6358
AM
32399+static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
32400+ struct au_nhash *whlist, struct au_nhash *delist)
32401+{
32402+#ifdef CONFIG_AUFS_SHWH
32403+ int err;
32404+ unsigned int nh, u;
32405+ struct hlist_head *head;
c06a8ce3
AM
32406+ struct au_vdir_wh *pos;
32407+ struct hlist_node *n;
dece6358
AM
32408+ char *p, *o;
32409+ struct au_vdir_destr *destr;
32410+
32411+ AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
32412+
32413+ err = -ENOMEM;
537831f9 32414+ o = p = (void *)__get_free_page(GFP_NOFS);
dece6358
AM
32415+ if (unlikely(!p))
32416+ goto out;
32417+
32418+ err = 0;
32419+ nh = whlist->nh_num;
32420+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32421+ p += AUFS_WH_PFX_LEN;
32422+ for (u = 0; u < nh; u++) {
32423+ head = whlist->nh_head + u;
c06a8ce3
AM
32424+ hlist_for_each_entry_safe(pos, n, head, wh_hash) {
32425+ destr = &pos->wh_str;
dece6358
AM
32426+ memcpy(p, destr->name, destr->len);
32427+ err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
c06a8ce3 32428+ pos->wh_ino, pos->wh_type, delist);
dece6358
AM
32429+ if (unlikely(err))
32430+ break;
32431+ }
32432+ }
32433+
1c60b727 32434+ free_page((unsigned long)o);
dece6358 32435+
4f0767ce 32436+out:
dece6358
AM
32437+ AuTraceErr(err);
32438+ return err;
32439+#else
32440+ return 0;
32441+#endif
32442+}
32443+
1facf9fc 32444+static int au_do_read_vdir(struct fillvdir_arg *arg)
32445+{
32446+ int err;
dece6358 32447+ unsigned int rdhash;
1facf9fc 32448+ loff_t offset;
5afbbe0d 32449+ aufs_bindex_t bbot, bindex, btop;
dece6358 32450+ unsigned char shwh;
1facf9fc 32451+ struct file *hf, *file;
32452+ struct super_block *sb;
32453+
1facf9fc 32454+ file = arg->file;
2000de60 32455+ sb = file->f_path.dentry->d_sb;
dece6358
AM
32456+ SiMustAnyLock(sb);
32457+
32458+ rdhash = au_sbi(sb)->si_rdhash;
1308ab2a 32459+ if (!rdhash)
32460+ rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
dece6358
AM
32461+ err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
32462+ if (unlikely(err))
1facf9fc 32463+ goto out;
dece6358
AM
32464+ err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
32465+ if (unlikely(err))
1facf9fc 32466+ goto out_delist;
32467+
32468+ err = 0;
32469+ arg->flags = 0;
dece6358
AM
32470+ shwh = 0;
32471+ if (au_opt_test(au_mntflags(sb), SHWH)) {
32472+ shwh = 1;
32473+ au_fset_fillvdir(arg->flags, SHWH);
32474+ }
5afbbe0d
AM
32475+ btop = au_fbtop(file);
32476+ bbot = au_fbbot_dir(file);
32477+ for (bindex = btop; !err && bindex <= bbot; bindex++) {
4a4d8108 32478+ hf = au_hf_dir(file, bindex);
1facf9fc 32479+ if (!hf)
32480+ continue;
32481+
32482+ offset = vfsub_llseek(hf, 0, SEEK_SET);
32483+ err = offset;
32484+ if (unlikely(offset))
32485+ break;
32486+
32487+ arg->bindex = bindex;
32488+ au_fclr_fillvdir(arg->flags, WHABLE);
dece6358 32489+ if (shwh
5afbbe0d 32490+ || (bindex != bbot
dece6358 32491+ && au_br_whable(au_sbr_perm(sb, bindex))))
1facf9fc 32492+ au_fset_fillvdir(arg->flags, WHABLE);
32493+ do {
32494+ arg->err = 0;
32495+ au_fclr_fillvdir(arg->flags, CALLED);
32496+ /* smp_mb(); */
392086de 32497+ err = vfsub_iterate_dir(hf, &arg->ctx);
1facf9fc 32498+ if (err >= 0)
32499+ err = arg->err;
32500+ } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
392086de
AM
32501+
32502+ /*
32503+ * dir_relax() may be good for concurrency, but aufs should not
32504+ * use it since it will cause a lockdep problem.
32505+ */
1facf9fc 32506+ }
dece6358
AM
32507+
32508+ if (!err && shwh)
32509+ err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
32510+
32511+ au_nhash_wh_free(&arg->whlist);
1facf9fc 32512+
4f0767ce 32513+out_delist:
dece6358 32514+ au_nhash_de_free(&arg->delist);
4f0767ce 32515+out:
1facf9fc 32516+ return err;
32517+}
32518+
32519+static int read_vdir(struct file *file, int may_read)
32520+{
32521+ int err;
32522+ unsigned long expire;
32523+ unsigned char do_read;
392086de
AM
32524+ struct fillvdir_arg arg = {
32525+ .ctx = {
2000de60 32526+ .actor = fillvdir
392086de
AM
32527+ }
32528+ };
1facf9fc 32529+ struct inode *inode;
32530+ struct au_vdir *vdir, *allocated;
32531+
32532+ err = 0;
c06a8ce3 32533+ inode = file_inode(file);
1facf9fc 32534+ IMustLock(inode);
5afbbe0d 32535+ IiMustWriteLock(inode);
dece6358
AM
32536+ SiMustAnyLock(inode->i_sb);
32537+
1facf9fc 32538+ allocated = NULL;
32539+ do_read = 0;
32540+ expire = au_sbi(inode->i_sb)->si_rdcache;
32541+ vdir = au_ivdir(inode);
32542+ if (!vdir) {
32543+ do_read = 1;
1308ab2a 32544+ vdir = alloc_vdir(file);
1facf9fc 32545+ err = PTR_ERR(vdir);
32546+ if (IS_ERR(vdir))
32547+ goto out;
32548+ err = 0;
32549+ allocated = vdir;
32550+ } else if (may_read
be118d29 32551+ && (!inode_eq_iversion(inode, vdir->vd_version)
1facf9fc 32552+ || time_after(jiffies, vdir->vd_jiffy + expire))) {
32553+ do_read = 1;
32554+ err = reinit_vdir(vdir);
32555+ if (unlikely(err))
32556+ goto out;
32557+ }
32558+
32559+ if (!do_read)
32560+ return 0; /* success */
32561+
32562+ arg.file = file;
32563+ arg.vdir = vdir;
32564+ err = au_do_read_vdir(&arg);
32565+ if (!err) {
392086de 32566+ /* file->f_pos = 0; */ /* todo: ctx->pos? */
be118d29 32567+ vdir->vd_version = inode_query_iversion(inode);
1facf9fc 32568+ vdir->vd_last.ul = 0;
32569+ vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32570+ if (allocated)
32571+ au_set_ivdir(inode, allocated);
32572+ } else if (allocated)
1c60b727 32573+ au_vdir_free(allocated);
1facf9fc 32574+
4f0767ce 32575+out:
1facf9fc 32576+ return err;
32577+}
32578+
32579+static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
32580+{
32581+ int err, rerr;
32582+ unsigned long ul, n;
32583+ const unsigned int deblk_sz = src->vd_deblk_sz;
32584+
32585+ AuDebugOn(tgt->vd_nblk != 1);
32586+
32587+ err = -ENOMEM;
32588+ if (tgt->vd_nblk < src->vd_nblk) {
32589+ unsigned char **p;
32590+
e2f27e51
AM
32591+ p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
32592+ GFP_NOFS, /*may_shrink*/0);
1facf9fc 32593+ if (unlikely(!p))
32594+ goto out;
32595+ tgt->vd_deblk = p;
32596+ }
32597+
1308ab2a 32598+ if (tgt->vd_deblk_sz != deblk_sz) {
32599+ unsigned char *p;
32600+
32601+ tgt->vd_deblk_sz = deblk_sz;
e2f27e51
AM
32602+ p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
32603+ /*may_shrink*/1);
1308ab2a 32604+ if (unlikely(!p))
32605+ goto out;
32606+ tgt->vd_deblk[0] = p;
32607+ }
1facf9fc 32608+ memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
1facf9fc 32609+ tgt->vd_version = src->vd_version;
32610+ tgt->vd_jiffy = src->vd_jiffy;
32611+
32612+ n = src->vd_nblk;
32613+ for (ul = 1; ul < n; ul++) {
dece6358
AM
32614+ tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
32615+ GFP_NOFS);
32616+ if (unlikely(!tgt->vd_deblk[ul]))
1facf9fc 32617+ goto out;
1308ab2a 32618+ tgt->vd_nblk++;
1facf9fc 32619+ }
1308ab2a 32620+ tgt->vd_nblk = n;
32621+ tgt->vd_last.ul = tgt->vd_last.ul;
32622+ tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
32623+ tgt->vd_last.p.deblk += src->vd_last.p.deblk
32624+ - src->vd_deblk[src->vd_last.ul];
1facf9fc 32625+ /* smp_mb(); */
32626+ return 0; /* success */
32627+
4f0767ce 32628+out:
1facf9fc 32629+ rerr = reinit_vdir(tgt);
32630+ BUG_ON(rerr);
32631+ return err;
32632+}
32633+
32634+int au_vdir_init(struct file *file)
32635+{
32636+ int err;
32637+ struct inode *inode;
32638+ struct au_vdir *vdir_cache, *allocated;
32639+
392086de 32640+ /* test file->f_pos here instead of ctx->pos */
1facf9fc 32641+ err = read_vdir(file, !file->f_pos);
32642+ if (unlikely(err))
32643+ goto out;
32644+
32645+ allocated = NULL;
32646+ vdir_cache = au_fvdir_cache(file);
32647+ if (!vdir_cache) {
1308ab2a 32648+ vdir_cache = alloc_vdir(file);
1facf9fc 32649+ err = PTR_ERR(vdir_cache);
32650+ if (IS_ERR(vdir_cache))
32651+ goto out;
32652+ allocated = vdir_cache;
32653+ } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
392086de 32654+ /* test file->f_pos here instead of ctx->pos */
1facf9fc 32655+ err = reinit_vdir(vdir_cache);
32656+ if (unlikely(err))
32657+ goto out;
32658+ } else
32659+ return 0; /* success */
32660+
c06a8ce3 32661+ inode = file_inode(file);
1facf9fc 32662+ err = copy_vdir(vdir_cache, au_ivdir(inode));
32663+ if (!err) {
be118d29 32664+ file->f_version = inode_query_iversion(inode);
1facf9fc 32665+ if (allocated)
32666+ au_set_fvdir_cache(file, allocated);
32667+ } else if (allocated)
1c60b727 32668+ au_vdir_free(allocated);
1facf9fc 32669+
4f0767ce 32670+out:
1facf9fc 32671+ return err;
32672+}
32673+
32674+static loff_t calc_offset(struct au_vdir *vdir)
32675+{
32676+ loff_t offset;
32677+ union au_vdir_deblk_p p;
32678+
32679+ p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
32680+ offset = vdir->vd_last.p.deblk - p.deblk;
32681+ offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
32682+ return offset;
32683+}
32684+
32685+/* returns true or false */
392086de 32686+static int seek_vdir(struct file *file, struct dir_context *ctx)
1facf9fc 32687+{
32688+ int valid;
32689+ unsigned int deblk_sz;
32690+ unsigned long ul, n;
32691+ loff_t offset;
32692+ union au_vdir_deblk_p p, deblk_end;
32693+ struct au_vdir *vdir_cache;
32694+
32695+ valid = 1;
32696+ vdir_cache = au_fvdir_cache(file);
32697+ offset = calc_offset(vdir_cache);
32698+ AuDbg("offset %lld\n", offset);
392086de 32699+ if (ctx->pos == offset)
1facf9fc 32700+ goto out;
32701+
32702+ vdir_cache->vd_last.ul = 0;
32703+ vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
392086de 32704+ if (!ctx->pos)
1facf9fc 32705+ goto out;
32706+
32707+ valid = 0;
32708+ deblk_sz = vdir_cache->vd_deblk_sz;
392086de 32709+ ul = div64_u64(ctx->pos, deblk_sz);
1facf9fc 32710+ AuDbg("ul %lu\n", ul);
32711+ if (ul >= vdir_cache->vd_nblk)
32712+ goto out;
32713+
32714+ n = vdir_cache->vd_nblk;
32715+ for (; ul < n; ul++) {
32716+ p.deblk = vdir_cache->vd_deblk[ul];
32717+ deblk_end.deblk = p.deblk + deblk_sz;
32718+ offset = ul;
32719+ offset *= deblk_sz;
392086de 32720+ while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
1facf9fc 32721+ unsigned int l;
32722+
32723+ l = calc_size(p.de->de_str.len);
32724+ offset += l;
32725+ p.deblk += l;
32726+ }
32727+ if (!is_deblk_end(&p, &deblk_end)) {
32728+ valid = 1;
32729+ vdir_cache->vd_last.ul = ul;
32730+ vdir_cache->vd_last.p = p;
32731+ break;
32732+ }
32733+ }
32734+
4f0767ce 32735+out:
1facf9fc 32736+ /* smp_mb(); */
b00004a5
AM
32737+ if (!valid)
32738+ AuDbg("valid %d\n", !valid);
1facf9fc 32739+ return valid;
32740+}
32741+
392086de 32742+int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
1facf9fc 32743+{
1facf9fc 32744+ unsigned int l, deblk_sz;
32745+ union au_vdir_deblk_p deblk_end;
32746+ struct au_vdir *vdir_cache;
32747+ struct au_vdir_de *de;
32748+
392086de 32749+ if (!seek_vdir(file, ctx))
1facf9fc 32750+ return 0;
32751+
acd2b654 32752+ vdir_cache = au_fvdir_cache(file);
1facf9fc 32753+ deblk_sz = vdir_cache->vd_deblk_sz;
32754+ while (1) {
32755+ deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32756+ deblk_end.deblk += deblk_sz;
32757+ while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
32758+ de = vdir_cache->vd_last.p.de;
32759+ AuDbg("%.*s, off%lld, i%lu, dt%d\n",
392086de 32760+ de->de_str.len, de->de_str.name, ctx->pos,
1facf9fc 32761+ (unsigned long)de->de_ino, de->de_type);
392086de
AM
32762+ if (unlikely(!dir_emit(ctx, de->de_str.name,
32763+ de->de_str.len, de->de_ino,
32764+ de->de_type))) {
1facf9fc 32765+ /* todo: ignore the error caused by udba? */
32766+ /* return err; */
32767+ return 0;
32768+ }
32769+
32770+ l = calc_size(de->de_str.len);
32771+ vdir_cache->vd_last.p.deblk += l;
392086de 32772+ ctx->pos += l;
1facf9fc 32773+ }
32774+ if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
32775+ vdir_cache->vd_last.ul++;
32776+ vdir_cache->vd_last.p.deblk
32777+ = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
392086de 32778+ ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
1facf9fc 32779+ continue;
32780+ }
32781+ break;
32782+ }
32783+
32784+ /* smp_mb(); */
32785+ return 0;
32786+}
7f207e10
AM
32787diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
32788--- /usr/share/empty/fs/aufs/vfsub.c 1970-01-01 01:00:00.000000000 +0100
0b487229
JR
32789+++ linux/fs/aufs/vfsub.c 2021-02-22 23:30:37.702680911 +0100
32790@@ -0,0 +1,895 @@
cd7a4cd9 32791+// SPDX-License-Identifier: GPL-2.0
1facf9fc 32792+/*
0b487229 32793+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 32794+ *
32795+ * This program, aufs is free software; you can redistribute it and/or modify
32796+ * it under the terms of the GNU General Public License as published by
32797+ * the Free Software Foundation; either version 2 of the License, or
32798+ * (at your option) any later version.
dece6358
AM
32799+ *
32800+ * This program is distributed in the hope that it will be useful,
32801+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
32802+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32803+ * GNU General Public License for more details.
32804+ *
32805+ * You should have received a copy of the GNU General Public License
523b37e3 32806+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 32807+ */
32808+
32809+/*
32810+ * sub-routines for VFS
32811+ */
32812+
8b6a4947 32813+#include <linux/mnt_namespace.h>
dece6358 32814+#include <linux/namei.h>
8cdd5066 32815+#include <linux/nsproxy.h>
dece6358
AM
32816+#include <linux/security.h>
32817+#include <linux/splice.h>
1facf9fc 32818+#include "aufs.h"
32819+
8cdd5066
JR
32820+#ifdef CONFIG_AUFS_BR_FUSE
32821+int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
32822+{
8cdd5066
JR
32823+ if (!au_test_fuse(h_sb) || !au_userns)
32824+ return 0;
32825+
8b6a4947 32826+ return is_current_mnt_ns(mnt) ? 0 : -EACCES;
8cdd5066
JR
32827+}
32828+#endif
32829+
a2654f78
AM
32830+int vfsub_sync_filesystem(struct super_block *h_sb, int wait)
32831+{
32832+ int err;
32833+
32834+ lockdep_off();
32835+ down_read(&h_sb->s_umount);
32836+ err = __sync_filesystem(h_sb, wait);
32837+ up_read(&h_sb->s_umount);
32838+ lockdep_on();
32839+
32840+ return err;
32841+}
32842+
8cdd5066
JR
32843+/* ---------------------------------------------------------------------- */
32844+
1facf9fc 32845+int vfsub_update_h_iattr(struct path *h_path, int *did)
32846+{
32847+ int err;
32848+ struct kstat st;
32849+ struct super_block *h_sb;
32850+
32851+ /* for remote fs, leave work for its getattr or d_revalidate */
32852+ /* for bad i_attr fs, handle them in aufs_getattr() */
32853+ /* still some fs may acquire i_mutex. we need to skip them */
32854+ err = 0;
32855+ if (!did)
32856+ did = &err;
32857+ h_sb = h_path->dentry->d_sb;
32858+ *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
32859+ if (*did)
521ced18 32860+ err = vfsub_getattr(h_path, &st);
1facf9fc 32861+
32862+ return err;
32863+}
32864+
32865+/* ---------------------------------------------------------------------- */
32866+
4a4d8108 32867+struct file *vfsub_dentry_open(struct path *path, int flags)
1308ab2a 32868+{
0b487229 32869+ return dentry_open(path, flags /* | __FMODE_NONOTIFY */,
7f207e10 32870+ current_cred());
1308ab2a 32871+}
32872+
1facf9fc 32873+struct file *vfsub_filp_open(const char *path, int oflags, int mode)
32874+{
32875+ struct file *file;
32876+
2cbb1c4b 32877+ lockdep_off();
7f207e10 32878+ file = filp_open(path,
2cbb1c4b 32879+ oflags /* | __FMODE_NONOTIFY */,
7f207e10 32880+ mode);
2cbb1c4b 32881+ lockdep_on();
1facf9fc 32882+ if (IS_ERR(file))
32883+ goto out;
32884+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
32885+
4f0767ce 32886+out:
1facf9fc 32887+ return file;
32888+}
32889+
b912730e
AM
32890+/*
32891+ * Ideally this function should call VFS:do_last() in order to keep all its
32892+ * checkings. But it is very hard for aufs to regenerate several VFS internal
32893+ * structure such as nameidata. This is a second (or third) best approach.
32894+ * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
32895+ */
32896+int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
acd2b654 32897+ struct vfsub_aopen_args *args)
b912730e
AM
32898+{
32899+ int err;
acd2b654 32900+ struct au_branch *br = args->br;
b912730e
AM
32901+ struct file *file = args->file;
32902+ /* copied from linux/fs/namei.c:atomic_open() */
32903+ struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
32904+
32905+ IMustLock(dir);
32906+ AuDebugOn(!dir->i_op->atomic_open);
32907+
32908+ err = au_br_test_oflag(args->open_flag, br);
32909+ if (unlikely(err))
32910+ goto out;
32911+
acd2b654
AM
32912+ au_lcnt_inc(&br->br_nfiles);
32913+ file->f_path.dentry = DENTRY_NOT_SET;
32914+ file->f_path.mnt = au_br_mnt(br);
32915+ AuDbg("%ps\n", dir->i_op->atomic_open);
b912730e 32916+ err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
acd2b654
AM
32917+ args->create_mode);
32918+ if (unlikely(err < 0)) {
32919+ au_lcnt_dec(&br->br_nfiles);
b912730e 32920+ goto out;
acd2b654 32921+ }
b912730e 32922+
acd2b654
AM
32923+ /* temporary workaround for nfsv4 branch */
32924+ if (au_test_nfs(dir->i_sb))
32925+ nfs_mark_for_revalidate(dir);
b912730e 32926+
acd2b654
AM
32927+ if (file->f_mode & FMODE_CREATED)
32928+ fsnotify_create(dir, dentry);
32929+ if (!(file->f_mode & FMODE_OPENED)) {
32930+ au_lcnt_dec(&br->br_nfiles);
32931+ goto out;
b912730e
AM
32932+ }
32933+
acd2b654
AM
32934+ /* todo: call VFS:may_open() here */
32935+ /* todo: ima_file_check() too? */
32936+ if (!err && (args->open_flag & __FMODE_EXEC))
32937+ err = deny_write_access(file);
32938+ if (!err)
32939+ fsnotify_open(file);
32940+ else
32941+ au_lcnt_dec(&br->br_nfiles);
32942+ /* note that the file is created and still opened */
b912730e
AM
32943+
32944+out:
32945+ return err;
32946+}
32947+
1facf9fc 32948+int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
32949+{
32950+ int err;
32951+
1facf9fc 32952+ err = kern_path(name, flags, path);
5527c038 32953+ if (!err && d_is_positive(path->dentry))
1facf9fc 32954+ vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
32955+ return err;
32956+}
32957+
febd17d6
JR
32958+struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
32959+ struct dentry *parent, int len)
32960+{
32961+ struct path path = {
32962+ .mnt = NULL
32963+ };
32964+
32965+ path.dentry = lookup_one_len_unlocked(name, parent, len);
32966+ if (IS_ERR(path.dentry))
32967+ goto out;
32968+ if (d_is_positive(path.dentry))
32969+ vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32970+
32971+out:
32972+ AuTraceErrPtr(path.dentry);
32973+ return path.dentry;
32974+}
32975+
1facf9fc 32976+struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
32977+ int len)
32978+{
32979+ struct path path = {
32980+ .mnt = NULL
32981+ };
32982+
1308ab2a 32983+ /* VFS checks it too, but by WARN_ON_ONCE() */
5527c038 32984+ IMustLock(d_inode(parent));
1facf9fc 32985+
32986+ path.dentry = lookup_one_len(name, parent, len);
32987+ if (IS_ERR(path.dentry))
32988+ goto out;
5527c038 32989+ if (d_is_positive(path.dentry))
1facf9fc 32990+ vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32991+
4f0767ce 32992+out:
4a4d8108 32993+ AuTraceErrPtr(path.dentry);
1facf9fc 32994+ return path.dentry;
32995+}
32996+
b4510431 32997+void vfsub_call_lkup_one(void *args)
2cbb1c4b 32998+{
b4510431
AM
32999+ struct vfsub_lkup_one_args *a = args;
33000+ *a->errp = vfsub_lkup_one(a->name, a->parent);
2cbb1c4b
JR
33001+}
33002+
1facf9fc 33003+/* ---------------------------------------------------------------------- */
33004+
33005+struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33006+ struct dentry *d2, struct au_hinode *hdir2)
33007+{
33008+ struct dentry *d;
33009+
2cbb1c4b 33010+ lockdep_off();
1facf9fc 33011+ d = lock_rename(d1, d2);
2cbb1c4b 33012+ lockdep_on();
4a4d8108 33013+ au_hn_suspend(hdir1);
1facf9fc 33014+ if (hdir1 != hdir2)
4a4d8108 33015+ au_hn_suspend(hdir2);
1facf9fc 33016+
33017+ return d;
33018+}
33019+
33020+void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33021+ struct dentry *d2, struct au_hinode *hdir2)
33022+{
4a4d8108 33023+ au_hn_resume(hdir1);
1facf9fc 33024+ if (hdir1 != hdir2)
4a4d8108 33025+ au_hn_resume(hdir2);
2cbb1c4b 33026+ lockdep_off();
1facf9fc 33027+ unlock_rename(d1, d2);
2cbb1c4b 33028+ lockdep_on();
1facf9fc 33029+}
33030+
33031+/* ---------------------------------------------------------------------- */
33032+
b4510431 33033+int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
1facf9fc 33034+{
33035+ int err;
33036+ struct dentry *d;
33037+
33038+ IMustLock(dir);
33039+
33040+ d = path->dentry;
33041+ path->dentry = d->d_parent;
b752ccd1 33042+ err = security_path_mknod(path, d, mode, 0);
1facf9fc 33043+ path->dentry = d;
33044+ if (unlikely(err))
33045+ goto out;
33046+
c1595e42 33047+ lockdep_off();
b4510431 33048+ err = vfs_create(dir, path->dentry, mode, want_excl);
c1595e42 33049+ lockdep_on();
1facf9fc 33050+ if (!err) {
33051+ struct path tmp = *path;
33052+ int did;
33053+
33054+ vfsub_update_h_iattr(&tmp, &did);
33055+ if (did) {
33056+ tmp.dentry = path->dentry->d_parent;
33057+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33058+ }
33059+ /*ignore*/
33060+ }
33061+
4f0767ce 33062+out:
1facf9fc 33063+ return err;
33064+}
33065+
33066+int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
33067+{
33068+ int err;
33069+ struct dentry *d;
33070+
33071+ IMustLock(dir);
33072+
33073+ d = path->dentry;
33074+ path->dentry = d->d_parent;
b752ccd1 33075+ err = security_path_symlink(path, d, symname);
1facf9fc 33076+ path->dentry = d;
33077+ if (unlikely(err))
33078+ goto out;
33079+
c1595e42 33080+ lockdep_off();
1facf9fc 33081+ err = vfs_symlink(dir, path->dentry, symname);
c1595e42 33082+ lockdep_on();
1facf9fc 33083+ if (!err) {
33084+ struct path tmp = *path;
33085+ int did;
33086+
33087+ vfsub_update_h_iattr(&tmp, &did);
33088+ if (did) {
33089+ tmp.dentry = path->dentry->d_parent;
33090+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33091+ }
33092+ /*ignore*/
33093+ }
33094+
4f0767ce 33095+out:
1facf9fc 33096+ return err;
33097+}
33098+
33099+int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
33100+{
33101+ int err;
33102+ struct dentry *d;
33103+
33104+ IMustLock(dir);
33105+
33106+ d = path->dentry;
33107+ path->dentry = d->d_parent;
027c5e7a 33108+ err = security_path_mknod(path, d, mode, new_encode_dev(dev));
1facf9fc 33109+ path->dentry = d;
33110+ if (unlikely(err))
33111+ goto out;
33112+
c1595e42 33113+ lockdep_off();
1facf9fc 33114+ err = vfs_mknod(dir, path->dentry, mode, dev);
c1595e42 33115+ lockdep_on();
1facf9fc 33116+ if (!err) {
33117+ struct path tmp = *path;
33118+ int did;
33119+
33120+ vfsub_update_h_iattr(&tmp, &did);
33121+ if (did) {
33122+ tmp.dentry = path->dentry->d_parent;
33123+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33124+ }
33125+ /*ignore*/
33126+ }
33127+
4f0767ce 33128+out:
1facf9fc 33129+ return err;
33130+}
33131+
33132+static int au_test_nlink(struct inode *inode)
33133+{
33134+ const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
33135+
33136+ if (!au_test_fs_no_limit_nlink(inode->i_sb)
33137+ || inode->i_nlink < link_max)
33138+ return 0;
33139+ return -EMLINK;
33140+}
33141+
523b37e3
AM
33142+int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
33143+ struct inode **delegated_inode)
1facf9fc 33144+{
33145+ int err;
33146+ struct dentry *d;
33147+
33148+ IMustLock(dir);
33149+
5527c038 33150+ err = au_test_nlink(d_inode(src_dentry));
1facf9fc 33151+ if (unlikely(err))
33152+ return err;
33153+
b4510431 33154+ /* we don't call may_linkat() */
1facf9fc 33155+ d = path->dentry;
33156+ path->dentry = d->d_parent;
b752ccd1 33157+ err = security_path_link(src_dentry, path, d);
1facf9fc 33158+ path->dentry = d;
33159+ if (unlikely(err))
33160+ goto out;
33161+
2cbb1c4b 33162+ lockdep_off();
523b37e3 33163+ err = vfs_link(src_dentry, dir, path->dentry, delegated_inode);
2cbb1c4b 33164+ lockdep_on();
1facf9fc 33165+ if (!err) {
33166+ struct path tmp = *path;
33167+ int did;
33168+
33169+ /* fuse has different memory inode for the same inumber */
33170+ vfsub_update_h_iattr(&tmp, &did);
33171+ if (did) {
33172+ tmp.dentry = path->dentry->d_parent;
33173+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33174+ tmp.dentry = src_dentry;
33175+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33176+ }
33177+ /*ignore*/
33178+ }
33179+
4f0767ce 33180+out:
1facf9fc 33181+ return err;
33182+}
33183+
33184+int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
523b37e3 33185+ struct inode *dir, struct path *path,
f2c43d5f 33186+ struct inode **delegated_inode, unsigned int flags)
1facf9fc 33187+{
33188+ int err;
33189+ struct path tmp = {
33190+ .mnt = path->mnt
33191+ };
33192+ struct dentry *d;
33193+
33194+ IMustLock(dir);
33195+ IMustLock(src_dir);
33196+
33197+ d = path->dentry;
33198+ path->dentry = d->d_parent;
33199+ tmp.dentry = src_dentry->d_parent;
38d290e6 33200+ err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
1facf9fc 33201+ path->dentry = d;
33202+ if (unlikely(err))
33203+ goto out;
33204+
2cbb1c4b 33205+ lockdep_off();
523b37e3 33206+ err = vfs_rename(src_dir, src_dentry, dir, path->dentry,
f2c43d5f 33207+ delegated_inode, flags);
2cbb1c4b 33208+ lockdep_on();
1facf9fc 33209+ if (!err) {
33210+ int did;
33211+
33212+ tmp.dentry = d->d_parent;
33213+ vfsub_update_h_iattr(&tmp, &did);
33214+ if (did) {
33215+ tmp.dentry = src_dentry;
33216+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33217+ tmp.dentry = src_dentry->d_parent;
33218+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33219+ }
33220+ /*ignore*/
33221+ }
33222+
4f0767ce 33223+out:
1facf9fc 33224+ return err;
33225+}
33226+
33227+int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
33228+{
33229+ int err;
33230+ struct dentry *d;
33231+
33232+ IMustLock(dir);
33233+
33234+ d = path->dentry;
33235+ path->dentry = d->d_parent;
b752ccd1 33236+ err = security_path_mkdir(path, d, mode);
1facf9fc 33237+ path->dentry = d;
33238+ if (unlikely(err))
33239+ goto out;
33240+
c1595e42 33241+ lockdep_off();
1facf9fc 33242+ err = vfs_mkdir(dir, path->dentry, mode);
c1595e42 33243+ lockdep_on();
1facf9fc 33244+ if (!err) {
33245+ struct path tmp = *path;
33246+ int did;
33247+
33248+ vfsub_update_h_iattr(&tmp, &did);
33249+ if (did) {
33250+ tmp.dentry = path->dentry->d_parent;
33251+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33252+ }
33253+ /*ignore*/
33254+ }
33255+
4f0767ce 33256+out:
1facf9fc 33257+ return err;
33258+}
33259+
33260+int vfsub_rmdir(struct inode *dir, struct path *path)
33261+{
33262+ int err;
33263+ struct dentry *d;
33264+
33265+ IMustLock(dir);
33266+
33267+ d = path->dentry;
33268+ path->dentry = d->d_parent;
b752ccd1 33269+ err = security_path_rmdir(path, d);
1facf9fc 33270+ path->dentry = d;
33271+ if (unlikely(err))
33272+ goto out;
33273+
2cbb1c4b 33274+ lockdep_off();
1facf9fc 33275+ err = vfs_rmdir(dir, path->dentry);
2cbb1c4b 33276+ lockdep_on();
1facf9fc 33277+ if (!err) {
33278+ struct path tmp = {
33279+ .dentry = path->dentry->d_parent,
33280+ .mnt = path->mnt
33281+ };
33282+
33283+ vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33284+ }
33285+
4f0767ce 33286+out:
1facf9fc 33287+ return err;
33288+}
33289+
33290+/* ---------------------------------------------------------------------- */
33291+
9dbd164d 33292+/* todo: support mmap_sem? */
1facf9fc 33293+ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33294+ loff_t *ppos)
33295+{
33296+ ssize_t err;
33297+
2cbb1c4b 33298+ lockdep_off();
1facf9fc 33299+ err = vfs_read(file, ubuf, count, ppos);
2cbb1c4b 33300+ lockdep_on();
1facf9fc 33301+ if (err >= 0)
33302+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33303+ return err;
33304+}
33305+
33306+/* todo: kernel_read()? */
33307+ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33308+ loff_t *ppos)
33309+{
33310+ ssize_t err;
33311+ mm_segment_t oldfs;
b752ccd1
AM
33312+ union {
33313+ void *k;
33314+ char __user *u;
33315+ } buf;
1facf9fc 33316+
b752ccd1 33317+ buf.k = kbuf;
1facf9fc 33318+ oldfs = get_fs();
33319+ set_fs(KERNEL_DS);
b752ccd1 33320+ err = vfsub_read_u(file, buf.u, count, ppos);
1facf9fc 33321+ set_fs(oldfs);
33322+ return err;
33323+}
33324+
33325+ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33326+ loff_t *ppos)
33327+{
33328+ ssize_t err;
33329+
2cbb1c4b 33330+ lockdep_off();
1facf9fc 33331+ err = vfs_write(file, ubuf, count, ppos);
2cbb1c4b 33332+ lockdep_on();
1facf9fc 33333+ if (err >= 0)
33334+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33335+ return err;
33336+}
33337+
33338+ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
33339+{
33340+ ssize_t err;
33341+ mm_segment_t oldfs;
b752ccd1
AM
33342+ union {
33343+ void *k;
33344+ const char __user *u;
33345+ } buf;
1facf9fc 33346+
b752ccd1 33347+ buf.k = kbuf;
1facf9fc 33348+ oldfs = get_fs();
33349+ set_fs(KERNEL_DS);
b752ccd1 33350+ err = vfsub_write_u(file, buf.u, count, ppos);
1facf9fc 33351+ set_fs(oldfs);
33352+ return err;
33353+}
33354+
4a4d8108
AM
33355+int vfsub_flush(struct file *file, fl_owner_t id)
33356+{
33357+ int err;
33358+
33359+ err = 0;
523b37e3 33360+ if (file->f_op->flush) {
2000de60 33361+ if (!au_test_nfs(file->f_path.dentry->d_sb))
2cbb1c4b
JR
33362+ err = file->f_op->flush(file, id);
33363+ else {
33364+ lockdep_off();
33365+ err = file->f_op->flush(file, id);
33366+ lockdep_on();
33367+ }
4a4d8108
AM
33368+ if (!err)
33369+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
33370+ /*ignore*/
33371+ }
33372+ return err;
33373+}
33374+
392086de 33375+int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
1facf9fc 33376+{
33377+ int err;
33378+
062440b3 33379+ AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
392086de 33380+
2cbb1c4b 33381+ lockdep_off();
392086de 33382+ err = iterate_dir(file, ctx);
2cbb1c4b 33383+ lockdep_on();
1facf9fc 33384+ if (err >= 0)
33385+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
1c60b727 33386+
1facf9fc 33387+ return err;
33388+}
33389+
33390+long vfsub_splice_to(struct file *in, loff_t *ppos,
33391+ struct pipe_inode_info *pipe, size_t len,
33392+ unsigned int flags)
33393+{
33394+ long err;
33395+
2cbb1c4b 33396+ lockdep_off();
0fc653ad 33397+ err = do_splice_to(in, ppos, pipe, len, flags);
2cbb1c4b 33398+ lockdep_on();
4a4d8108 33399+ file_accessed(in);
1facf9fc 33400+ if (err >= 0)
33401+ vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
33402+ return err;
33403+}
33404+
33405+long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33406+ loff_t *ppos, size_t len, unsigned int flags)
33407+{
33408+ long err;
33409+
2cbb1c4b 33410+ lockdep_off();
0fc653ad 33411+ err = do_splice_from(pipe, out, ppos, len, flags);
2cbb1c4b 33412+ lockdep_on();
1facf9fc 33413+ if (err >= 0)
33414+ vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
33415+ return err;
33416+}
33417+
53392da6
AM
33418+int vfsub_fsync(struct file *file, struct path *path, int datasync)
33419+{
33420+ int err;
33421+
33422+ /* file can be NULL */
33423+ lockdep_off();
33424+ err = vfs_fsync(file, datasync);
33425+ lockdep_on();
33426+ if (!err) {
33427+ if (!path) {
33428+ AuDebugOn(!file);
33429+ path = &file->f_path;
33430+ }
33431+ vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33432+ }
33433+ return err;
33434+}
33435+
1facf9fc 33436+/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
33437+int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33438+ struct file *h_file)
33439+{
33440+ int err;
33441+ struct inode *h_inode;
c06a8ce3 33442+ struct super_block *h_sb;
1facf9fc 33443+
1facf9fc 33444+ if (!h_file) {
c06a8ce3
AM
33445+ err = vfsub_truncate(h_path, length);
33446+ goto out;
1facf9fc 33447+ }
33448+
5527c038 33449+ h_inode = d_inode(h_path->dentry);
c06a8ce3
AM
33450+ h_sb = h_inode->i_sb;
33451+ lockdep_off();
33452+ sb_start_write(h_sb);
33453+ lockdep_on();
1facf9fc 33454+ err = locks_verify_truncate(h_inode, h_file, length);
33455+ if (!err)
953406b4 33456+ err = security_path_truncate(h_path);
2cbb1c4b
JR
33457+ if (!err) {
33458+ lockdep_off();
1facf9fc 33459+ err = do_truncate(h_path->dentry, length, attr, h_file);
2cbb1c4b
JR
33460+ lockdep_on();
33461+ }
c06a8ce3
AM
33462+ lockdep_off();
33463+ sb_end_write(h_sb);
33464+ lockdep_on();
1facf9fc 33465+
4f0767ce 33466+out:
1facf9fc 33467+ return err;
33468+}
33469+
33470+/* ---------------------------------------------------------------------- */
33471+
33472+struct au_vfsub_mkdir_args {
33473+ int *errp;
33474+ struct inode *dir;
33475+ struct path *path;
33476+ int mode;
33477+};
33478+
33479+static void au_call_vfsub_mkdir(void *args)
33480+{
33481+ struct au_vfsub_mkdir_args *a = args;
33482+ *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
33483+}
33484+
33485+int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
33486+{
33487+ int err, do_sio, wkq_err;
33488+
33489+ do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
c1595e42
JR
33490+ if (!do_sio) {
33491+ lockdep_off();
1facf9fc 33492+ err = vfsub_mkdir(dir, path, mode);
c1595e42
JR
33493+ lockdep_on();
33494+ } else {
1facf9fc 33495+ struct au_vfsub_mkdir_args args = {
33496+ .errp = &err,
33497+ .dir = dir,
33498+ .path = path,
33499+ .mode = mode
33500+ };
33501+ wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
33502+ if (unlikely(wkq_err))
33503+ err = wkq_err;
33504+ }
33505+
33506+ return err;
33507+}
33508+
33509+struct au_vfsub_rmdir_args {
33510+ int *errp;
33511+ struct inode *dir;
33512+ struct path *path;
33513+};
33514+
33515+static void au_call_vfsub_rmdir(void *args)
33516+{
33517+ struct au_vfsub_rmdir_args *a = args;
33518+ *a->errp = vfsub_rmdir(a->dir, a->path);
33519+}
33520+
33521+int vfsub_sio_rmdir(struct inode *dir, struct path *path)
33522+{
33523+ int err, do_sio, wkq_err;
33524+
33525+ do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
c1595e42
JR
33526+ if (!do_sio) {
33527+ lockdep_off();
1facf9fc 33528+ err = vfsub_rmdir(dir, path);
c1595e42
JR
33529+ lockdep_on();
33530+ } else {
1facf9fc 33531+ struct au_vfsub_rmdir_args args = {
33532+ .errp = &err,
33533+ .dir = dir,
33534+ .path = path
33535+ };
33536+ wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
33537+ if (unlikely(wkq_err))
33538+ err = wkq_err;
33539+ }
33540+
33541+ return err;
33542+}
33543+
33544+/* ---------------------------------------------------------------------- */
33545+
33546+struct notify_change_args {
33547+ int *errp;
33548+ struct path *path;
33549+ struct iattr *ia;
523b37e3 33550+ struct inode **delegated_inode;
1facf9fc 33551+};
33552+
33553+static void call_notify_change(void *args)
33554+{
33555+ struct notify_change_args *a = args;
33556+ struct inode *h_inode;
33557+
5527c038 33558+ h_inode = d_inode(a->path->dentry);
1facf9fc 33559+ IMustLock(h_inode);
33560+
33561+ *a->errp = -EPERM;
33562+ if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
c1595e42 33563+ lockdep_off();
523b37e3
AM
33564+ *a->errp = notify_change(a->path->dentry, a->ia,
33565+ a->delegated_inode);
c1595e42 33566+ lockdep_on();
1facf9fc 33567+ if (!*a->errp)
33568+ vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
33569+ }
33570+ AuTraceErr(*a->errp);
33571+}
33572+
523b37e3
AM
33573+int vfsub_notify_change(struct path *path, struct iattr *ia,
33574+ struct inode **delegated_inode)
1facf9fc 33575+{
33576+ int err;
33577+ struct notify_change_args args = {
523b37e3
AM
33578+ .errp = &err,
33579+ .path = path,
33580+ .ia = ia,
33581+ .delegated_inode = delegated_inode
1facf9fc 33582+ };
33583+
33584+ call_notify_change(&args);
33585+
33586+ return err;
33587+}
33588+
523b37e3
AM
33589+int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33590+ struct inode **delegated_inode)
1facf9fc 33591+{
33592+ int err, wkq_err;
33593+ struct notify_change_args args = {
523b37e3
AM
33594+ .errp = &err,
33595+ .path = path,
33596+ .ia = ia,
33597+ .delegated_inode = delegated_inode
1facf9fc 33598+ };
33599+
33600+ wkq_err = au_wkq_wait(call_notify_change, &args);
33601+ if (unlikely(wkq_err))
33602+ err = wkq_err;
33603+
33604+ return err;
33605+}
33606+
33607+/* ---------------------------------------------------------------------- */
33608+
33609+struct unlink_args {
33610+ int *errp;
33611+ struct inode *dir;
33612+ struct path *path;
523b37e3 33613+ struct inode **delegated_inode;
1facf9fc 33614+};
33615+
33616+static void call_unlink(void *args)
33617+{
33618+ struct unlink_args *a = args;
33619+ struct dentry *d = a->path->dentry;
33620+ struct inode *h_inode;
33621+ const int stop_sillyrename = (au_test_nfs(d->d_sb)
c1595e42 33622+ && au_dcount(d) == 1);
1facf9fc 33623+
33624+ IMustLock(a->dir);
33625+
33626+ a->path->dentry = d->d_parent;
33627+ *a->errp = security_path_unlink(a->path, d);
33628+ a->path->dentry = d;
33629+ if (unlikely(*a->errp))
33630+ return;
33631+
33632+ if (!stop_sillyrename)
33633+ dget(d);
5527c038
JR
33634+ h_inode = NULL;
33635+ if (d_is_positive(d)) {
33636+ h_inode = d_inode(d);
027c5e7a 33637+ ihold(h_inode);
5527c038 33638+ }
1facf9fc 33639+
2cbb1c4b 33640+ lockdep_off();
523b37e3 33641+ *a->errp = vfs_unlink(a->dir, d, a->delegated_inode);
2cbb1c4b 33642+ lockdep_on();
1facf9fc 33643+ if (!*a->errp) {
33644+ struct path tmp = {
33645+ .dentry = d->d_parent,
33646+ .mnt = a->path->mnt
33647+ };
33648+ vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33649+ }
33650+
33651+ if (!stop_sillyrename)
33652+ dput(d);
33653+ if (h_inode)
33654+ iput(h_inode);
33655+
33656+ AuTraceErr(*a->errp);
33657+}
33658+
33659+/*
33660+ * @dir: must be locked.
33661+ * @dentry: target dentry.
33662+ */
523b37e3
AM
33663+int vfsub_unlink(struct inode *dir, struct path *path,
33664+ struct inode **delegated_inode, int force)
1facf9fc 33665+{
33666+ int err;
33667+ struct unlink_args args = {
523b37e3
AM
33668+ .errp = &err,
33669+ .dir = dir,
33670+ .path = path,
33671+ .delegated_inode = delegated_inode
1facf9fc 33672+ };
33673+
33674+ if (!force)
33675+ call_unlink(&args);
33676+ else {
33677+ int wkq_err;
33678+
33679+ wkq_err = au_wkq_wait(call_unlink, &args);
33680+ if (unlikely(wkq_err))
33681+ err = wkq_err;
33682+ }
33683+
33684+ return err;
33685+}
7f207e10
AM
33686diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
33687--- /usr/share/empty/fs/aufs/vfsub.h 1970-01-01 01:00:00.000000000 +0100
0b487229
JR
33688+++ linux/fs/aufs/vfsub.h 2021-02-22 23:30:37.702680911 +0100
33689@@ -0,0 +1,354 @@
062440b3 33690+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 33691+/*
0b487229 33692+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 33693+ *
33694+ * This program, aufs is free software; you can redistribute it and/or modify
33695+ * it under the terms of the GNU General Public License as published by
33696+ * the Free Software Foundation; either version 2 of the License, or
33697+ * (at your option) any later version.
dece6358
AM
33698+ *
33699+ * This program is distributed in the hope that it will be useful,
33700+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
33701+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33702+ * GNU General Public License for more details.
33703+ *
33704+ * You should have received a copy of the GNU General Public License
523b37e3 33705+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 33706+ */
33707+
33708+/*
33709+ * sub-routines for VFS
33710+ */
33711+
33712+#ifndef __AUFS_VFSUB_H__
33713+#define __AUFS_VFSUB_H__
33714+
33715+#ifdef __KERNEL__
33716+
33717+#include <linux/fs.h>
b4510431 33718+#include <linux/mount.h>
8cdd5066 33719+#include <linux/posix_acl.h>
c1595e42 33720+#include <linux/xattr.h>
7f207e10 33721+#include "debug.h"
1facf9fc 33722+
7f207e10 33723+/* copied from linux/fs/internal.h */
2cbb1c4b 33724+/* todo: BAD approach!! */
c06a8ce3 33725+extern void __mnt_drop_write(struct vfsmount *);
acd2b654 33726+extern struct file *alloc_empty_file(int, const struct cred *);
7f207e10
AM
33727+
33728+/* ---------------------------------------------------------------------- */
1facf9fc 33729+
33730+/* lock subclass for lower inode */
33731+/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
33732+/* reduce? gave up. */
33733+enum {
c1595e42 33734+ AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
1facf9fc 33735+ AuLsc_I_PARENT, /* lower inode, parent first */
33736+ AuLsc_I_PARENT2, /* copyup dirs */
dece6358 33737+ AuLsc_I_PARENT3, /* copyup wh */
1facf9fc 33738+ AuLsc_I_CHILD,
33739+ AuLsc_I_CHILD2,
33740+ AuLsc_I_End
33741+};
33742+
33743+/* to debug easier, do not make them inlined functions */
33744+#define MtxMustLock(mtx) AuDebugOn(!mutex_is_locked(mtx))
febd17d6 33745+#define IMustLock(i) AuDebugOn(!inode_is_locked(i))
1facf9fc 33746+
33747+/* ---------------------------------------------------------------------- */
33748+
7f207e10
AM
33749+static inline void vfsub_drop_nlink(struct inode *inode)
33750+{
33751+ AuDebugOn(!inode->i_nlink);
33752+ drop_nlink(inode);
33753+}
33754+
027c5e7a
AM
33755+static inline void vfsub_dead_dir(struct inode *inode)
33756+{
33757+ AuDebugOn(!S_ISDIR(inode->i_mode));
33758+ inode->i_flags |= S_DEAD;
33759+ clear_nlink(inode);
33760+}
33761+
392086de
AM
33762+static inline int vfsub_native_ro(struct inode *inode)
33763+{
8b6a4947 33764+ return sb_rdonly(inode->i_sb)
392086de
AM
33765+ || IS_RDONLY(inode)
33766+ /* || IS_APPEND(inode) */
33767+ || IS_IMMUTABLE(inode);
33768+}
33769+
8cdd5066
JR
33770+#ifdef CONFIG_AUFS_BR_FUSE
33771+int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
33772+#else
33773+AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
33774+#endif
33775+
a2654f78
AM
33776+int vfsub_sync_filesystem(struct super_block *h_sb, int wait);
33777+
7f207e10
AM
33778+/* ---------------------------------------------------------------------- */
33779+
33780+int vfsub_update_h_iattr(struct path *h_path, int *did);
33781+struct file *vfsub_dentry_open(struct path *path, int flags);
33782+struct file *vfsub_filp_open(const char *path, int oflags, int mode);
acd2b654 33783+struct au_branch;
b912730e 33784+struct vfsub_aopen_args {
acd2b654
AM
33785+ struct file *file;
33786+ unsigned int open_flag;
33787+ umode_t create_mode;
33788+ struct au_branch *br;
b912730e 33789+};
b912730e 33790+int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
acd2b654 33791+ struct vfsub_aopen_args *args);
1facf9fc 33792+int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
b4510431 33793+
febd17d6
JR
33794+struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33795+ struct dentry *parent, int len);
1facf9fc 33796+struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
33797+ int len);
b4510431
AM
33798+
33799+struct vfsub_lkup_one_args {
33800+ struct dentry **errp;
33801+ struct qstr *name;
33802+ struct dentry *parent;
33803+};
33804+
33805+static inline struct dentry *vfsub_lkup_one(struct qstr *name,
33806+ struct dentry *parent)
33807+{
33808+ return vfsub_lookup_one_len(name->name, parent, name->len);
33809+}
33810+
33811+void vfsub_call_lkup_one(void *args);
33812+
33813+/* ---------------------------------------------------------------------- */
33814+
33815+static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
33816+{
33817+ int err;
076b876e 33818+
b4510431
AM
33819+ lockdep_off();
33820+ err = mnt_want_write(mnt);
33821+ lockdep_on();
33822+ return err;
33823+}
33824+
33825+static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
33826+{
33827+ lockdep_off();
33828+ mnt_drop_write(mnt);
33829+ lockdep_on();
33830+}
1facf9fc 33831+
7e9cd9fe 33832+#if 0 /* reserved */
c06a8ce3
AM
33833+static inline void vfsub_mnt_drop_write_file(struct file *file)
33834+{
33835+ lockdep_off();
33836+ mnt_drop_write_file(file);
33837+ lockdep_on();
33838+}
7e9cd9fe 33839+#endif
c06a8ce3 33840+
1facf9fc 33841+/* ---------------------------------------------------------------------- */
33842+
33843+struct au_hinode;
33844+struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33845+ struct dentry *d2, struct au_hinode *hdir2);
33846+void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33847+ struct dentry *d2, struct au_hinode *hdir2);
33848+
537831f9
AM
33849+int vfsub_create(struct inode *dir, struct path *path, int mode,
33850+ bool want_excl);
1facf9fc 33851+int vfsub_symlink(struct inode *dir, struct path *path,
33852+ const char *symname);
33853+int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
33854+int vfsub_link(struct dentry *src_dentry, struct inode *dir,
523b37e3 33855+ struct path *path, struct inode **delegated_inode);
1facf9fc 33856+int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
523b37e3 33857+ struct inode *hdir, struct path *path,
f2c43d5f 33858+ struct inode **delegated_inode, unsigned int flags);
1facf9fc 33859+int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
33860+int vfsub_rmdir(struct inode *dir, struct path *path);
33861+
33862+/* ---------------------------------------------------------------------- */
33863+
33864+ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33865+ loff_t *ppos);
33866+ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33867+ loff_t *ppos);
33868+ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33869+ loff_t *ppos);
33870+ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
33871+ loff_t *ppos);
4a4d8108 33872+int vfsub_flush(struct file *file, fl_owner_t id);
392086de
AM
33873+int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
33874+
c06a8ce3
AM
33875+static inline loff_t vfsub_f_size_read(struct file *file)
33876+{
33877+ return i_size_read(file_inode(file));
33878+}
33879+
4a4d8108
AM
33880+static inline unsigned int vfsub_file_flags(struct file *file)
33881+{
33882+ unsigned int flags;
33883+
33884+ spin_lock(&file->f_lock);
33885+ flags = file->f_flags;
33886+ spin_unlock(&file->f_lock);
33887+
33888+ return flags;
33889+}
1308ab2a 33890+
f0c0a007
AM
33891+static inline int vfsub_file_execed(struct file *file)
33892+{
33893+ /* todo: direct access f_flags */
33894+ return !!(vfsub_file_flags(file) & __FMODE_EXEC);
33895+}
33896+
7e9cd9fe 33897+#if 0 /* reserved */
1facf9fc 33898+static inline void vfsub_file_accessed(struct file *h_file)
33899+{
33900+ file_accessed(h_file);
33901+ vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
33902+}
7e9cd9fe 33903+#endif
1facf9fc 33904+
79b8bda9 33905+#if 0 /* reserved */
1facf9fc 33906+static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
33907+ struct dentry *h_dentry)
33908+{
33909+ struct path h_path = {
33910+ .dentry = h_dentry,
33911+ .mnt = h_mnt
33912+ };
92d182d2 33913+ touch_atime(&h_path);
1facf9fc 33914+ vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
33915+}
79b8bda9 33916+#endif
1facf9fc 33917+
cd7a4cd9
AM
33918+static inline int vfsub_update_time(struct inode *h_inode,
33919+ struct timespec64 *ts, int flags)
0c3ec466 33920+{
5afbbe0d 33921+ return update_time(h_inode, ts, flags);
0c3ec466
AM
33922+ /* no vfsub_update_h_iattr() since we don't have struct path */
33923+}
33924+
8cdd5066
JR
33925+#ifdef CONFIG_FS_POSIX_ACL
33926+static inline int vfsub_acl_chmod(struct inode *h_inode, umode_t h_mode)
33927+{
33928+ int err;
33929+
33930+ err = posix_acl_chmod(h_inode, h_mode);
33931+ if (err == -EOPNOTSUPP)
33932+ err = 0;
33933+ return err;
33934+}
33935+#else
33936+AuStubInt0(vfsub_acl_chmod, struct inode *h_inode, umode_t h_mode);
33937+#endif
33938+
4a4d8108
AM
33939+long vfsub_splice_to(struct file *in, loff_t *ppos,
33940+ struct pipe_inode_info *pipe, size_t len,
33941+ unsigned int flags);
33942+long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33943+ loff_t *ppos, size_t len, unsigned int flags);
c06a8ce3
AM
33944+
33945+static inline long vfsub_truncate(struct path *path, loff_t length)
33946+{
33947+ long err;
076b876e 33948+
c06a8ce3
AM
33949+ lockdep_off();
33950+ err = vfs_truncate(path, length);
33951+ lockdep_on();
33952+ return err;
33953+}
33954+
4a4d8108
AM
33955+int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33956+ struct file *h_file);
53392da6 33957+int vfsub_fsync(struct file *file, struct path *path, int datasync);
4a4d8108 33958+
521ced18
JR
33959+/*
33960+ * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
33961+ * ioctl.
33962+ */
33963+static inline int vfsub_clone_file_range(struct file *src, struct file *dst,
33964+ u64 len)
33965+{
33966+ int err;
33967+
33968+ lockdep_off();
33969+ err = vfs_clone_file_range(src, 0, dst, 0, len);
33970+ lockdep_on();
33971+
33972+ return err;
33973+}
33974+
33975+/* copy_file_range(2) is a systemcall */
33976+static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
33977+ struct file *dst, loff_t dst_pos,
33978+ size_t len, unsigned int flags)
33979+{
33980+ ssize_t ssz;
33981+
33982+ lockdep_off();
33983+ ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
33984+ lockdep_on();
33985+
33986+ return ssz;
33987+}
33988+
1facf9fc 33989+/* ---------------------------------------------------------------------- */
33990+
33991+static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
33992+{
33993+ loff_t err;
33994+
2cbb1c4b 33995+ lockdep_off();
1facf9fc 33996+ err = vfs_llseek(file, offset, origin);
2cbb1c4b 33997+ lockdep_on();
1facf9fc 33998+ return err;
33999+}
34000+
34001+/* ---------------------------------------------------------------------- */
34002+
4a4d8108
AM
34003+int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
34004+int vfsub_sio_rmdir(struct inode *dir, struct path *path);
523b37e3
AM
34005+int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
34006+ struct inode **delegated_inode);
34007+int vfsub_notify_change(struct path *path, struct iattr *ia,
34008+ struct inode **delegated_inode);
34009+int vfsub_unlink(struct inode *dir, struct path *path,
34010+ struct inode **delegated_inode, int force);
4a4d8108 34011+
521ced18
JR
34012+static inline int vfsub_getattr(const struct path *path, struct kstat *st)
34013+{
34014+ return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
34015+}
34016+
c1595e42
JR
34017+/* ---------------------------------------------------------------------- */
34018+
34019+static inline int vfsub_setxattr(struct dentry *dentry, const char *name,
34020+ const void *value, size_t size, int flags)
34021+{
34022+ int err;
34023+
34024+ lockdep_off();
34025+ err = vfs_setxattr(dentry, name, value, size, flags);
34026+ lockdep_on();
34027+
34028+ return err;
34029+}
34030+
34031+static inline int vfsub_removexattr(struct dentry *dentry, const char *name)
34032+{
34033+ int err;
34034+
34035+ lockdep_off();
34036+ err = vfs_removexattr(dentry, name);
34037+ lockdep_on();
34038+
34039+ return err;
34040+}
34041+
1facf9fc 34042+#endif /* __KERNEL__ */
34043+#endif /* __AUFS_VFSUB_H__ */
7f207e10
AM
34044diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
34045--- /usr/share/empty/fs/aufs/wbr_policy.c 1970-01-01 01:00:00.000000000 +0100
0b487229 34046+++ linux/fs/aufs/wbr_policy.c 2021-02-22 23:30:37.702680911 +0100
cd7a4cd9
AM
34047@@ -0,0 +1,830 @@
34048+// SPDX-License-Identifier: GPL-2.0
1facf9fc 34049+/*
0b487229 34050+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 34051+ *
34052+ * This program, aufs is free software; you can redistribute it and/or modify
34053+ * it under the terms of the GNU General Public License as published by
34054+ * the Free Software Foundation; either version 2 of the License, or
34055+ * (at your option) any later version.
dece6358
AM
34056+ *
34057+ * This program is distributed in the hope that it will be useful,
34058+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
34059+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34060+ * GNU General Public License for more details.
34061+ *
34062+ * You should have received a copy of the GNU General Public License
523b37e3 34063+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 34064+ */
34065+
34066+/*
34067+ * policies for selecting one among multiple writable branches
34068+ */
34069+
34070+#include <linux/statfs.h>
34071+#include "aufs.h"
34072+
34073+/* subset of cpup_attr() */
34074+static noinline_for_stack
34075+int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
34076+{
34077+ int err, sbits;
34078+ struct iattr ia;
34079+ struct inode *h_isrc;
34080+
5527c038 34081+ h_isrc = d_inode(h_src);
1facf9fc 34082+ ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
34083+ ia.ia_mode = h_isrc->i_mode;
34084+ ia.ia_uid = h_isrc->i_uid;
34085+ ia.ia_gid = h_isrc->i_gid;
34086+ sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
5527c038 34087+ au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
523b37e3
AM
34088+ /* no delegation since it is just created */
34089+ err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
1facf9fc 34090+
34091+ /* is this nfs only? */
34092+ if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
34093+ ia.ia_valid = ATTR_FORCE | ATTR_MODE;
34094+ ia.ia_mode = h_isrc->i_mode;
523b37e3 34095+ err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
1facf9fc 34096+ }
34097+
34098+ return err;
34099+}
34100+
34101+#define AuCpdown_PARENT_OPQ 1
34102+#define AuCpdown_WHED (1 << 1)
34103+#define AuCpdown_MADE_DIR (1 << 2)
34104+#define AuCpdown_DIROPQ (1 << 3)
34105+#define au_ftest_cpdown(flags, name) ((flags) & AuCpdown_##name)
7f207e10
AM
34106+#define au_fset_cpdown(flags, name) \
34107+ do { (flags) |= AuCpdown_##name; } while (0)
34108+#define au_fclr_cpdown(flags, name) \
34109+ do { (flags) &= ~AuCpdown_##name; } while (0)
1facf9fc 34110+
1facf9fc 34111+static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
c2b27bf2 34112+ unsigned int *flags)
1facf9fc 34113+{
34114+ int err;
34115+ struct dentry *opq_dentry;
34116+
34117+ opq_dentry = au_diropq_create(dentry, bdst);
34118+ err = PTR_ERR(opq_dentry);
34119+ if (IS_ERR(opq_dentry))
34120+ goto out;
34121+ dput(opq_dentry);
c2b27bf2 34122+ au_fset_cpdown(*flags, DIROPQ);
1facf9fc 34123+
4f0767ce 34124+out:
1facf9fc 34125+ return err;
34126+}
34127+
34128+static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
34129+ struct inode *dir, aufs_bindex_t bdst)
34130+{
34131+ int err;
34132+ struct path h_path;
34133+ struct au_branch *br;
34134+
34135+ br = au_sbr(dentry->d_sb, bdst);
34136+ h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
34137+ err = PTR_ERR(h_path.dentry);
34138+ if (IS_ERR(h_path.dentry))
34139+ goto out;
34140+
34141+ err = 0;
5527c038 34142+ if (d_is_positive(h_path.dentry)) {
86dc4139 34143+ h_path.mnt = au_br_mnt(br);
1facf9fc 34144+ err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
34145+ dentry);
34146+ }
34147+ dput(h_path.dentry);
34148+
4f0767ce 34149+out:
1facf9fc 34150+ return err;
34151+}
34152+
34153+static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 34154+ struct au_pin *pin,
1facf9fc 34155+ struct dentry *h_parent, void *arg)
34156+{
34157+ int err, rerr;
5afbbe0d 34158+ aufs_bindex_t bopq, btop;
1facf9fc 34159+ struct path h_path;
34160+ struct dentry *parent;
34161+ struct inode *h_dir, *h_inode, *inode, *dir;
c2b27bf2 34162+ unsigned int *flags = arg;
1facf9fc 34163+
5afbbe0d 34164+ btop = au_dbtop(dentry);
1facf9fc 34165+ /* dentry is di-locked */
34166+ parent = dget_parent(dentry);
5527c038
JR
34167+ dir = d_inode(parent);
34168+ h_dir = d_inode(h_parent);
1facf9fc 34169+ AuDebugOn(h_dir != au_h_iptr(dir, bdst));
34170+ IMustLock(h_dir);
34171+
86dc4139 34172+ err = au_lkup_neg(dentry, bdst, /*wh*/0);
1facf9fc 34173+ if (unlikely(err < 0))
34174+ goto out;
34175+ h_path.dentry = au_h_dptr(dentry, bdst);
34176+ h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
cd7a4cd9 34177+ err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path, 0755);
1facf9fc 34178+ if (unlikely(err))
34179+ goto out_put;
c2b27bf2 34180+ au_fset_cpdown(*flags, MADE_DIR);
1facf9fc 34181+
1facf9fc 34182+ bopq = au_dbdiropq(dentry);
c2b27bf2
AM
34183+ au_fclr_cpdown(*flags, WHED);
34184+ au_fclr_cpdown(*flags, DIROPQ);
1facf9fc 34185+ if (au_dbwh(dentry) == bdst)
c2b27bf2
AM
34186+ au_fset_cpdown(*flags, WHED);
34187+ if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
34188+ au_fset_cpdown(*flags, PARENT_OPQ);
5527c038 34189+ h_inode = d_inode(h_path.dentry);
febd17d6 34190+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
c2b27bf2
AM
34191+ if (au_ftest_cpdown(*flags, WHED)) {
34192+ err = au_cpdown_dir_opq(dentry, bdst, flags);
1facf9fc 34193+ if (unlikely(err)) {
febd17d6 34194+ inode_unlock(h_inode);
1facf9fc 34195+ goto out_dir;
34196+ }
34197+ }
34198+
5afbbe0d 34199+ err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
febd17d6 34200+ inode_unlock(h_inode);
1facf9fc 34201+ if (unlikely(err))
34202+ goto out_opq;
34203+
c2b27bf2 34204+ if (au_ftest_cpdown(*flags, WHED)) {
1facf9fc 34205+ err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
34206+ if (unlikely(err))
34207+ goto out_opq;
34208+ }
34209+
5527c038 34210+ inode = d_inode(dentry);
5afbbe0d
AM
34211+ if (au_ibbot(inode) < bdst)
34212+ au_set_ibbot(inode, bdst);
1facf9fc 34213+ au_set_h_iptr(inode, bdst, au_igrab(h_inode),
34214+ au_hi_flags(inode, /*isdir*/1));
076b876e 34215+ au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
1facf9fc 34216+ goto out; /* success */
34217+
34218+ /* revert */
4f0767ce 34219+out_opq:
c2b27bf2 34220+ if (au_ftest_cpdown(*flags, DIROPQ)) {
febd17d6 34221+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
1facf9fc 34222+ rerr = au_diropq_remove(dentry, bdst);
febd17d6 34223+ inode_unlock(h_inode);
1facf9fc 34224+ if (unlikely(rerr)) {
523b37e3
AM
34225+ AuIOErr("failed removing diropq for %pd b%d (%d)\n",
34226+ dentry, bdst, rerr);
1facf9fc 34227+ err = -EIO;
34228+ goto out;
34229+ }
34230+ }
4f0767ce 34231+out_dir:
c2b27bf2 34232+ if (au_ftest_cpdown(*flags, MADE_DIR)) {
1facf9fc 34233+ rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
34234+ if (unlikely(rerr)) {
523b37e3
AM
34235+ AuIOErr("failed removing %pd b%d (%d)\n",
34236+ dentry, bdst, rerr);
1facf9fc 34237+ err = -EIO;
34238+ }
34239+ }
4f0767ce 34240+out_put:
1facf9fc 34241+ au_set_h_dptr(dentry, bdst, NULL);
5afbbe0d
AM
34242+ if (au_dbbot(dentry) == bdst)
34243+ au_update_dbbot(dentry);
4f0767ce 34244+out:
1facf9fc 34245+ dput(parent);
34246+ return err;
34247+}
34248+
34249+int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
34250+{
34251+ int err;
c2b27bf2 34252+ unsigned int flags;
1facf9fc 34253+
c2b27bf2
AM
34254+ flags = 0;
34255+ err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
1facf9fc 34256+
34257+ return err;
34258+}
34259+
34260+/* ---------------------------------------------------------------------- */
34261+
34262+/* policies for create */
34263+
c2b27bf2 34264+int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
4a4d8108
AM
34265+{
34266+ int err, i, j, ndentry;
34267+ aufs_bindex_t bopq;
34268+ struct au_dcsub_pages dpages;
34269+ struct au_dpage *dpage;
34270+ struct dentry **dentries, *parent, *d;
34271+
34272+ err = au_dpages_init(&dpages, GFP_NOFS);
34273+ if (unlikely(err))
34274+ goto out;
34275+ parent = dget_parent(dentry);
027c5e7a 34276+ err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
4a4d8108
AM
34277+ if (unlikely(err))
34278+ goto out_free;
34279+
34280+ err = bindex;
34281+ for (i = 0; i < dpages.ndpage; i++) {
34282+ dpage = dpages.dpages + i;
34283+ dentries = dpage->dentries;
34284+ ndentry = dpage->ndentry;
34285+ for (j = 0; j < ndentry; j++) {
34286+ d = dentries[j];
34287+ di_read_lock_parent2(d, !AuLock_IR);
34288+ bopq = au_dbdiropq(d);
34289+ di_read_unlock(d, !AuLock_IR);
34290+ if (bopq >= 0 && bopq < err)
34291+ err = bopq;
34292+ }
34293+ }
34294+
34295+out_free:
34296+ dput(parent);
34297+ au_dpages_free(&dpages);
34298+out:
34299+ return err;
34300+}
34301+
1facf9fc 34302+static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
34303+{
34304+ for (; bindex >= 0; bindex--)
34305+ if (!au_br_rdonly(au_sbr(sb, bindex)))
34306+ return bindex;
34307+ return -EROFS;
34308+}
34309+
34310+/* top down parent */
392086de
AM
34311+static int au_wbr_create_tdp(struct dentry *dentry,
34312+ unsigned int flags __maybe_unused)
1facf9fc 34313+{
34314+ int err;
5afbbe0d 34315+ aufs_bindex_t btop, bindex;
1facf9fc 34316+ struct super_block *sb;
34317+ struct dentry *parent, *h_parent;
34318+
34319+ sb = dentry->d_sb;
5afbbe0d
AM
34320+ btop = au_dbtop(dentry);
34321+ err = btop;
34322+ if (!au_br_rdonly(au_sbr(sb, btop)))
1facf9fc 34323+ goto out;
34324+
34325+ err = -EROFS;
34326+ parent = dget_parent(dentry);
5afbbe0d 34327+ for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
1facf9fc 34328+ h_parent = au_h_dptr(parent, bindex);
5527c038 34329+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 34330+ continue;
34331+
34332+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
34333+ err = bindex;
34334+ break;
34335+ }
34336+ }
34337+ dput(parent);
34338+
34339+ /* bottom up here */
4a4d8108 34340+ if (unlikely(err < 0)) {
5afbbe0d 34341+ err = au_wbr_bu(sb, btop - 1);
4a4d8108
AM
34342+ if (err >= 0)
34343+ err = au_wbr_nonopq(dentry, err);
34344+ }
1facf9fc 34345+
4f0767ce 34346+out:
1facf9fc 34347+ AuDbg("b%d\n", err);
34348+ return err;
34349+}
34350+
34351+/* ---------------------------------------------------------------------- */
34352+
34353+/* an exception for the policy other than tdp */
34354+static int au_wbr_create_exp(struct dentry *dentry)
34355+{
34356+ int err;
34357+ aufs_bindex_t bwh, bdiropq;
34358+ struct dentry *parent;
34359+
34360+ err = -1;
34361+ bwh = au_dbwh(dentry);
34362+ parent = dget_parent(dentry);
34363+ bdiropq = au_dbdiropq(parent);
34364+ if (bwh >= 0) {
34365+ if (bdiropq >= 0)
34366+ err = min(bdiropq, bwh);
34367+ else
34368+ err = bwh;
34369+ AuDbg("%d\n", err);
34370+ } else if (bdiropq >= 0) {
34371+ err = bdiropq;
34372+ AuDbg("%d\n", err);
34373+ }
34374+ dput(parent);
34375+
4a4d8108
AM
34376+ if (err >= 0)
34377+ err = au_wbr_nonopq(dentry, err);
34378+
1facf9fc 34379+ if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
34380+ err = -1;
34381+
34382+ AuDbg("%d\n", err);
34383+ return err;
34384+}
34385+
34386+/* ---------------------------------------------------------------------- */
34387+
34388+/* round robin */
34389+static int au_wbr_create_init_rr(struct super_block *sb)
34390+{
34391+ int err;
34392+
5afbbe0d 34393+ err = au_wbr_bu(sb, au_sbbot(sb));
1facf9fc 34394+ atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
dece6358 34395+ /* smp_mb(); */
1facf9fc 34396+
34397+ AuDbg("b%d\n", err);
34398+ return err;
34399+}
34400+
392086de 34401+static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
1facf9fc 34402+{
34403+ int err, nbr;
34404+ unsigned int u;
5afbbe0d 34405+ aufs_bindex_t bindex, bbot;
1facf9fc 34406+ struct super_block *sb;
34407+ atomic_t *next;
34408+
34409+ err = au_wbr_create_exp(dentry);
34410+ if (err >= 0)
34411+ goto out;
34412+
34413+ sb = dentry->d_sb;
34414+ next = &au_sbi(sb)->si_wbr_rr_next;
5afbbe0d
AM
34415+ bbot = au_sbbot(sb);
34416+ nbr = bbot + 1;
34417+ for (bindex = 0; bindex <= bbot; bindex++) {
392086de 34418+ if (!au_ftest_wbr(flags, DIR)) {
1facf9fc 34419+ err = atomic_dec_return(next) + 1;
34420+ /* modulo for 0 is meaningless */
34421+ if (unlikely(!err))
34422+ err = atomic_dec_return(next) + 1;
34423+ } else
34424+ err = atomic_read(next);
34425+ AuDbg("%d\n", err);
34426+ u = err;
34427+ err = u % nbr;
34428+ AuDbg("%d\n", err);
34429+ if (!au_br_rdonly(au_sbr(sb, err)))
34430+ break;
34431+ err = -EROFS;
34432+ }
34433+
4a4d8108
AM
34434+ if (err >= 0)
34435+ err = au_wbr_nonopq(dentry, err);
34436+
4f0767ce 34437+out:
1facf9fc 34438+ AuDbg("%d\n", err);
34439+ return err;
34440+}
34441+
34442+/* ---------------------------------------------------------------------- */
34443+
34444+/* most free space */
392086de 34445+static void au_mfs(struct dentry *dentry, struct dentry *parent)
1facf9fc 34446+{
34447+ struct super_block *sb;
34448+ struct au_branch *br;
34449+ struct au_wbr_mfs *mfs;
392086de 34450+ struct dentry *h_parent;
5afbbe0d 34451+ aufs_bindex_t bindex, bbot;
1facf9fc 34452+ int err;
34453+ unsigned long long b, bavail;
7f207e10 34454+ struct path h_path;
1facf9fc 34455+ /* reduce the stack usage */
34456+ struct kstatfs *st;
34457+
34458+ st = kmalloc(sizeof(*st), GFP_NOFS);
34459+ if (unlikely(!st)) {
34460+ AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
34461+ return;
34462+ }
34463+
34464+ bavail = 0;
34465+ sb = dentry->d_sb;
34466+ mfs = &au_sbi(sb)->si_wbr_mfs;
dece6358 34467+ MtxMustLock(&mfs->mfs_lock);
1facf9fc 34468+ mfs->mfs_bindex = -EROFS;
34469+ mfs->mfsrr_bytes = 0;
392086de
AM
34470+ if (!parent) {
34471+ bindex = 0;
5afbbe0d 34472+ bbot = au_sbbot(sb);
392086de 34473+ } else {
5afbbe0d
AM
34474+ bindex = au_dbtop(parent);
34475+ bbot = au_dbtaildir(parent);
392086de
AM
34476+ }
34477+
5afbbe0d 34478+ for (; bindex <= bbot; bindex++) {
392086de
AM
34479+ if (parent) {
34480+ h_parent = au_h_dptr(parent, bindex);
5527c038 34481+ if (!h_parent || d_is_negative(h_parent))
392086de
AM
34482+ continue;
34483+ }
1facf9fc 34484+ br = au_sbr(sb, bindex);
34485+ if (au_br_rdonly(br))
34486+ continue;
34487+
34488+ /* sb->s_root for NFS is unreliable */
86dc4139 34489+ h_path.mnt = au_br_mnt(br);
7f207e10
AM
34490+ h_path.dentry = h_path.mnt->mnt_root;
34491+ err = vfs_statfs(&h_path, st);
1facf9fc 34492+ if (unlikely(err)) {
34493+ AuWarn1("failed statfs, b%d, %d\n", bindex, err);
34494+ continue;
34495+ }
34496+
34497+ /* when the available size is equal, select the lower one */
34498+ BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
34499+ || sizeof(b) < sizeof(st->f_bsize));
34500+ b = st->f_bavail * st->f_bsize;
34501+ br->br_wbr->wbr_bytes = b;
34502+ if (b >= bavail) {
34503+ bavail = b;
34504+ mfs->mfs_bindex = bindex;
34505+ mfs->mfs_jiffy = jiffies;
34506+ }
34507+ }
34508+
34509+ mfs->mfsrr_bytes = bavail;
34510+ AuDbg("b%d\n", mfs->mfs_bindex);
e49925d1 34511+ au_kfree_rcu(st);
1facf9fc 34512+}
34513+
392086de 34514+static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
1facf9fc 34515+{
34516+ int err;
392086de 34517+ struct dentry *parent;
1facf9fc 34518+ struct super_block *sb;
34519+ struct au_wbr_mfs *mfs;
34520+
34521+ err = au_wbr_create_exp(dentry);
34522+ if (err >= 0)
34523+ goto out;
34524+
34525+ sb = dentry->d_sb;
392086de
AM
34526+ parent = NULL;
34527+ if (au_ftest_wbr(flags, PARENT))
34528+ parent = dget_parent(dentry);
1facf9fc 34529+ mfs = &au_sbi(sb)->si_wbr_mfs;
34530+ mutex_lock(&mfs->mfs_lock);
34531+ if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34532+ || mfs->mfs_bindex < 0
34533+ || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
392086de 34534+ au_mfs(dentry, parent);
1facf9fc 34535+ mutex_unlock(&mfs->mfs_lock);
34536+ err = mfs->mfs_bindex;
392086de 34537+ dput(parent);
1facf9fc 34538+
4a4d8108
AM
34539+ if (err >= 0)
34540+ err = au_wbr_nonopq(dentry, err);
34541+
4f0767ce 34542+out:
1facf9fc 34543+ AuDbg("b%d\n", err);
34544+ return err;
34545+}
34546+
34547+static int au_wbr_create_init_mfs(struct super_block *sb)
34548+{
34549+ struct au_wbr_mfs *mfs;
34550+
34551+ mfs = &au_sbi(sb)->si_wbr_mfs;
34552+ mutex_init(&mfs->mfs_lock);
34553+ mfs->mfs_jiffy = 0;
34554+ mfs->mfs_bindex = -EROFS;
34555+
34556+ return 0;
34557+}
34558+
34559+static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
34560+{
34561+ mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
34562+ return 0;
34563+}
34564+
34565+/* ---------------------------------------------------------------------- */
34566+
f2c43d5f
AM
34567+/* top down regardless parent, and then mfs */
34568+static int au_wbr_create_tdmfs(struct dentry *dentry,
34569+ unsigned int flags __maybe_unused)
34570+{
34571+ int err;
34572+ aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
34573+ unsigned long long watermark;
34574+ struct super_block *sb;
34575+ struct au_wbr_mfs *mfs;
34576+ struct au_branch *br;
34577+ struct dentry *parent;
34578+
34579+ sb = dentry->d_sb;
34580+ mfs = &au_sbi(sb)->si_wbr_mfs;
34581+ mutex_lock(&mfs->mfs_lock);
34582+ if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34583+ || mfs->mfs_bindex < 0)
34584+ au_mfs(dentry, /*parent*/NULL);
34585+ watermark = mfs->mfsrr_watermark;
34586+ bmfs = mfs->mfs_bindex;
34587+ mutex_unlock(&mfs->mfs_lock);
34588+
34589+ /* another style of au_wbr_create_exp() */
34590+ bwh = au_dbwh(dentry);
34591+ parent = dget_parent(dentry);
34592+ btail = au_dbtaildir(parent);
34593+ if (bwh >= 0 && bwh < btail)
34594+ btail = bwh;
34595+
34596+ err = au_wbr_nonopq(dentry, btail);
34597+ if (unlikely(err < 0))
34598+ goto out;
34599+ btail = err;
34600+ bfound = -1;
34601+ for (bindex = 0; bindex <= btail; bindex++) {
34602+ br = au_sbr(sb, bindex);
34603+ if (au_br_rdonly(br))
34604+ continue;
34605+ if (br->br_wbr->wbr_bytes > watermark) {
34606+ bfound = bindex;
34607+ break;
34608+ }
34609+ }
34610+ err = bfound;
34611+ if (err < 0)
34612+ err = bmfs;
34613+
34614+out:
34615+ dput(parent);
34616+ AuDbg("b%d\n", err);
34617+ return err;
34618+}
34619+
34620+/* ---------------------------------------------------------------------- */
34621+
1facf9fc 34622+/* most free space and then round robin */
392086de 34623+static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
1facf9fc 34624+{
34625+ int err;
34626+ struct au_wbr_mfs *mfs;
34627+
392086de 34628+ err = au_wbr_create_mfs(dentry, flags);
1facf9fc 34629+ if (err >= 0) {
34630+ mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
dece6358 34631+ mutex_lock(&mfs->mfs_lock);
1facf9fc 34632+ if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
392086de 34633+ err = au_wbr_create_rr(dentry, flags);
dece6358 34634+ mutex_unlock(&mfs->mfs_lock);
1facf9fc 34635+ }
34636+
34637+ AuDbg("b%d\n", err);
34638+ return err;
34639+}
34640+
34641+static int au_wbr_create_init_mfsrr(struct super_block *sb)
34642+{
34643+ int err;
34644+
34645+ au_wbr_create_init_mfs(sb); /* ignore */
34646+ err = au_wbr_create_init_rr(sb);
34647+
34648+ return err;
34649+}
34650+
34651+/* ---------------------------------------------------------------------- */
34652+
34653+/* top down parent and most free space */
392086de 34654+static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
1facf9fc 34655+{
34656+ int err, e2;
34657+ unsigned long long b;
5afbbe0d 34658+ aufs_bindex_t bindex, btop, bbot;
1facf9fc 34659+ struct super_block *sb;
34660+ struct dentry *parent, *h_parent;
34661+ struct au_branch *br;
34662+
392086de 34663+ err = au_wbr_create_tdp(dentry, flags);
1facf9fc 34664+ if (unlikely(err < 0))
34665+ goto out;
34666+ parent = dget_parent(dentry);
5afbbe0d
AM
34667+ btop = au_dbtop(parent);
34668+ bbot = au_dbtaildir(parent);
34669+ if (btop == bbot)
1facf9fc 34670+ goto out_parent; /* success */
34671+
392086de 34672+ e2 = au_wbr_create_mfs(dentry, flags);
1facf9fc 34673+ if (e2 < 0)
34674+ goto out_parent; /* success */
34675+
34676+ /* when the available size is equal, select upper one */
34677+ sb = dentry->d_sb;
34678+ br = au_sbr(sb, err);
34679+ b = br->br_wbr->wbr_bytes;
34680+ AuDbg("b%d, %llu\n", err, b);
34681+
5afbbe0d 34682+ for (bindex = btop; bindex <= bbot; bindex++) {
1facf9fc 34683+ h_parent = au_h_dptr(parent, bindex);
5527c038 34684+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 34685+ continue;
34686+
34687+ br = au_sbr(sb, bindex);
34688+ if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
34689+ b = br->br_wbr->wbr_bytes;
34690+ err = bindex;
34691+ AuDbg("b%d, %llu\n", err, b);
34692+ }
34693+ }
34694+
4a4d8108
AM
34695+ if (err >= 0)
34696+ err = au_wbr_nonopq(dentry, err);
34697+
4f0767ce 34698+out_parent:
1facf9fc 34699+ dput(parent);
4f0767ce 34700+out:
1facf9fc 34701+ AuDbg("b%d\n", err);
34702+ return err;
34703+}
34704+
34705+/* ---------------------------------------------------------------------- */
34706+
392086de
AM
34707+/*
34708+ * - top down parent
34709+ * - most free space with parent
34710+ * - most free space round-robin regardless parent
34711+ */
34712+static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
34713+{
34714+ int err;
34715+ unsigned long long watermark;
34716+ struct super_block *sb;
34717+ struct au_branch *br;
34718+ struct au_wbr_mfs *mfs;
34719+
34720+ err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
34721+ if (unlikely(err < 0))
34722+ goto out;
34723+
34724+ sb = dentry->d_sb;
34725+ br = au_sbr(sb, err);
34726+ mfs = &au_sbi(sb)->si_wbr_mfs;
34727+ mutex_lock(&mfs->mfs_lock);
34728+ watermark = mfs->mfsrr_watermark;
34729+ mutex_unlock(&mfs->mfs_lock);
34730+ if (br->br_wbr->wbr_bytes < watermark)
34731+ /* regardless the parent dir */
34732+ err = au_wbr_create_mfsrr(dentry, flags);
34733+
34734+out:
34735+ AuDbg("b%d\n", err);
34736+ return err;
34737+}
34738+
34739+/* ---------------------------------------------------------------------- */
34740+
1facf9fc 34741+/* policies for copyup */
34742+
34743+/* top down parent */
34744+static int au_wbr_copyup_tdp(struct dentry *dentry)
34745+{
392086de 34746+ return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
1facf9fc 34747+}
34748+
34749+/* bottom up parent */
34750+static int au_wbr_copyup_bup(struct dentry *dentry)
34751+{
34752+ int err;
5afbbe0d 34753+ aufs_bindex_t bindex, btop;
1facf9fc 34754+ struct dentry *parent, *h_parent;
34755+ struct super_block *sb;
34756+
34757+ err = -EROFS;
34758+ sb = dentry->d_sb;
34759+ parent = dget_parent(dentry);
5afbbe0d
AM
34760+ btop = au_dbtop(parent);
34761+ for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
1facf9fc 34762+ h_parent = au_h_dptr(parent, bindex);
5527c038 34763+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 34764+ continue;
34765+
34766+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
34767+ err = bindex;
34768+ break;
34769+ }
34770+ }
34771+ dput(parent);
34772+
34773+ /* bottom up here */
34774+ if (unlikely(err < 0))
5afbbe0d 34775+ err = au_wbr_bu(sb, btop - 1);
1facf9fc 34776+
34777+ AuDbg("b%d\n", err);
34778+ return err;
34779+}
34780+
34781+/* bottom up */
5afbbe0d 34782+int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
1facf9fc 34783+{
34784+ int err;
34785+
5afbbe0d 34786+ err = au_wbr_bu(dentry->d_sb, btop);
4a4d8108 34787+ AuDbg("b%d\n", err);
5afbbe0d 34788+ if (err > btop)
4a4d8108 34789+ err = au_wbr_nonopq(dentry, err);
1facf9fc 34790+
34791+ AuDbg("b%d\n", err);
34792+ return err;
34793+}
34794+
076b876e
AM
34795+static int au_wbr_copyup_bu(struct dentry *dentry)
34796+{
34797+ int err;
5afbbe0d 34798+ aufs_bindex_t btop;
076b876e 34799+
5afbbe0d
AM
34800+ btop = au_dbtop(dentry);
34801+ err = au_wbr_do_copyup_bu(dentry, btop);
076b876e
AM
34802+ return err;
34803+}
34804+
1facf9fc 34805+/* ---------------------------------------------------------------------- */
34806+
34807+struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
34808+ [AuWbrCopyup_TDP] = {
34809+ .copyup = au_wbr_copyup_tdp
34810+ },
34811+ [AuWbrCopyup_BUP] = {
34812+ .copyup = au_wbr_copyup_bup
34813+ },
34814+ [AuWbrCopyup_BU] = {
34815+ .copyup = au_wbr_copyup_bu
34816+ }
34817+};
34818+
34819+struct au_wbr_create_operations au_wbr_create_ops[] = {
34820+ [AuWbrCreate_TDP] = {
34821+ .create = au_wbr_create_tdp
34822+ },
34823+ [AuWbrCreate_RR] = {
34824+ .create = au_wbr_create_rr,
34825+ .init = au_wbr_create_init_rr
34826+ },
34827+ [AuWbrCreate_MFS] = {
34828+ .create = au_wbr_create_mfs,
34829+ .init = au_wbr_create_init_mfs,
34830+ .fin = au_wbr_create_fin_mfs
34831+ },
34832+ [AuWbrCreate_MFSV] = {
34833+ .create = au_wbr_create_mfs,
34834+ .init = au_wbr_create_init_mfs,
34835+ .fin = au_wbr_create_fin_mfs
34836+ },
34837+ [AuWbrCreate_MFSRR] = {
34838+ .create = au_wbr_create_mfsrr,
34839+ .init = au_wbr_create_init_mfsrr,
34840+ .fin = au_wbr_create_fin_mfs
34841+ },
34842+ [AuWbrCreate_MFSRRV] = {
34843+ .create = au_wbr_create_mfsrr,
34844+ .init = au_wbr_create_init_mfsrr,
34845+ .fin = au_wbr_create_fin_mfs
34846+ },
f2c43d5f
AM
34847+ [AuWbrCreate_TDMFS] = {
34848+ .create = au_wbr_create_tdmfs,
34849+ .init = au_wbr_create_init_mfs,
34850+ .fin = au_wbr_create_fin_mfs
34851+ },
34852+ [AuWbrCreate_TDMFSV] = {
34853+ .create = au_wbr_create_tdmfs,
34854+ .init = au_wbr_create_init_mfs,
34855+ .fin = au_wbr_create_fin_mfs
34856+ },
1facf9fc 34857+ [AuWbrCreate_PMFS] = {
34858+ .create = au_wbr_create_pmfs,
34859+ .init = au_wbr_create_init_mfs,
34860+ .fin = au_wbr_create_fin_mfs
34861+ },
34862+ [AuWbrCreate_PMFSV] = {
34863+ .create = au_wbr_create_pmfs,
34864+ .init = au_wbr_create_init_mfs,
34865+ .fin = au_wbr_create_fin_mfs
392086de
AM
34866+ },
34867+ [AuWbrCreate_PMFSRR] = {
34868+ .create = au_wbr_create_pmfsrr,
34869+ .init = au_wbr_create_init_mfsrr,
34870+ .fin = au_wbr_create_fin_mfs
34871+ },
34872+ [AuWbrCreate_PMFSRRV] = {
34873+ .create = au_wbr_create_pmfsrr,
34874+ .init = au_wbr_create_init_mfsrr,
34875+ .fin = au_wbr_create_fin_mfs
1facf9fc 34876+ }
34877+};
7f207e10
AM
34878diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
34879--- /usr/share/empty/fs/aufs/whout.c 1970-01-01 01:00:00.000000000 +0100
0b487229 34880+++ linux/fs/aufs/whout.c 2021-02-22 23:30:37.702680911 +0100
062440b3 34881@@ -0,0 +1,1062 @@
cd7a4cd9 34882+// SPDX-License-Identifier: GPL-2.0
1facf9fc 34883+/*
0b487229 34884+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 34885+ *
34886+ * This program, aufs is free software; you can redistribute it and/or modify
34887+ * it under the terms of the GNU General Public License as published by
34888+ * the Free Software Foundation; either version 2 of the License, or
34889+ * (at your option) any later version.
dece6358
AM
34890+ *
34891+ * This program is distributed in the hope that it will be useful,
34892+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
34893+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34894+ * GNU General Public License for more details.
34895+ *
34896+ * You should have received a copy of the GNU General Public License
523b37e3 34897+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 34898+ */
34899+
34900+/*
34901+ * whiteout for logical deletion and opaque directory
34902+ */
34903+
1facf9fc 34904+#include "aufs.h"
34905+
cd7a4cd9 34906+#define WH_MASK 0444
1facf9fc 34907+
34908+/*
34909+ * If a directory contains this file, then it is opaque. We start with the
34910+ * .wh. flag so that it is blocked by lookup.
34911+ */
0c3ec466
AM
34912+static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
34913+ sizeof(AUFS_WH_DIROPQ) - 1);
1facf9fc 34914+
34915+/*
34916+ * generate whiteout name, which is NOT terminated by NULL.
34917+ * @name: original d_name.name
34918+ * @len: original d_name.len
34919+ * @wh: whiteout qstr
34920+ * returns zero when succeeds, otherwise error.
34921+ * succeeded value as wh->name should be freed by kfree().
34922+ */
34923+int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
34924+{
34925+ char *p;
34926+
34927+ if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
34928+ return -ENAMETOOLONG;
34929+
34930+ wh->len = name->len + AUFS_WH_PFX_LEN;
34931+ p = kmalloc(wh->len, GFP_NOFS);
34932+ wh->name = p;
34933+ if (p) {
34934+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
34935+ memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
34936+ /* smp_mb(); */
34937+ return 0;
34938+ }
34939+ return -ENOMEM;
34940+}
34941+
34942+/* ---------------------------------------------------------------------- */
34943+
34944+/*
34945+ * test if the @wh_name exists under @h_parent.
34946+ * @try_sio specifies the necessary of super-io.
34947+ */
076b876e 34948+int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio)
1facf9fc 34949+{
34950+ int err;
34951+ struct dentry *wh_dentry;
1facf9fc 34952+
1facf9fc 34953+ if (!try_sio)
b4510431 34954+ wh_dentry = vfsub_lkup_one(wh_name, h_parent);
1facf9fc 34955+ else
076b876e 34956+ wh_dentry = au_sio_lkup_one(wh_name, h_parent);
1facf9fc 34957+ err = PTR_ERR(wh_dentry);
2000de60
JR
34958+ if (IS_ERR(wh_dentry)) {
34959+ if (err == -ENAMETOOLONG)
34960+ err = 0;
1facf9fc 34961+ goto out;
2000de60 34962+ }
1facf9fc 34963+
34964+ err = 0;
5527c038 34965+ if (d_is_negative(wh_dentry))
1facf9fc 34966+ goto out_wh; /* success */
34967+
34968+ err = 1;
7e9cd9fe 34969+ if (d_is_reg(wh_dentry))
1facf9fc 34970+ goto out_wh; /* success */
34971+
34972+ err = -EIO;
523b37e3 34973+ AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
5527c038 34974+ wh_dentry, d_inode(wh_dentry)->i_mode);
1facf9fc 34975+
4f0767ce 34976+out_wh:
1facf9fc 34977+ dput(wh_dentry);
4f0767ce 34978+out:
1facf9fc 34979+ return err;
34980+}
34981+
34982+/*
34983+ * test if the @h_dentry sets opaque or not.
34984+ */
076b876e 34985+int au_diropq_test(struct dentry *h_dentry)
1facf9fc 34986+{
34987+ int err;
34988+ struct inode *h_dir;
34989+
5527c038 34990+ h_dir = d_inode(h_dentry);
076b876e 34991+ err = au_wh_test(h_dentry, &diropq_name,
1facf9fc 34992+ au_test_h_perm_sio(h_dir, MAY_EXEC));
34993+ return err;
34994+}
34995+
34996+/*
34997+ * returns a negative dentry whose name is unique and temporary.
34998+ */
34999+struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
35000+ struct qstr *prefix)
35001+{
1facf9fc 35002+ struct dentry *dentry;
35003+ int i;
027c5e7a 35004+ char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
4a4d8108 35005+ *name, *p;
027c5e7a 35006+ /* strict atomic_t is unnecessary here */
1facf9fc 35007+ static unsigned short cnt;
35008+ struct qstr qs;
35009+
4a4d8108
AM
35010+ BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
35011+
1facf9fc 35012+ name = defname;
027c5e7a
AM
35013+ qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
35014+ if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
1facf9fc 35015+ dentry = ERR_PTR(-ENAMETOOLONG);
4a4d8108 35016+ if (unlikely(qs.len > NAME_MAX))
1facf9fc 35017+ goto out;
35018+ dentry = ERR_PTR(-ENOMEM);
35019+ name = kmalloc(qs.len + 1, GFP_NOFS);
35020+ if (unlikely(!name))
35021+ goto out;
35022+ }
35023+
35024+ /* doubly whiteout-ed */
35025+ memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
35026+ p = name + AUFS_WH_PFX_LEN * 2;
35027+ memcpy(p, prefix->name, prefix->len);
35028+ p += prefix->len;
35029+ *p++ = '.';
4a4d8108 35030+ AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
1facf9fc 35031+
35032+ qs.name = name;
35033+ for (i = 0; i < 3; i++) {
b752ccd1 35034+ sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
076b876e 35035+ dentry = au_sio_lkup_one(&qs, h_parent);
5527c038 35036+ if (IS_ERR(dentry) || d_is_negative(dentry))
1facf9fc 35037+ goto out_name;
35038+ dput(dentry);
35039+ }
0c3ec466 35040+ /* pr_warn("could not get random name\n"); */
1facf9fc 35041+ dentry = ERR_PTR(-EEXIST);
35042+ AuDbg("%.*s\n", AuLNPair(&qs));
35043+ BUG();
35044+
4f0767ce 35045+out_name:
1facf9fc 35046+ if (name != defname)
e49925d1 35047+ au_kfree_try_rcu(name);
4f0767ce 35048+out:
4a4d8108 35049+ AuTraceErrPtr(dentry);
1facf9fc 35050+ return dentry;
1facf9fc 35051+}
35052+
35053+/*
35054+ * rename the @h_dentry on @br to the whiteouted temporary name.
35055+ */
35056+int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
35057+{
35058+ int err;
35059+ struct path h_path = {
86dc4139 35060+ .mnt = au_br_mnt(br)
1facf9fc 35061+ };
523b37e3 35062+ struct inode *h_dir, *delegated;
1facf9fc 35063+ struct dentry *h_parent;
35064+
35065+ h_parent = h_dentry->d_parent; /* dir inode is locked */
5527c038 35066+ h_dir = d_inode(h_parent);
1facf9fc 35067+ IMustLock(h_dir);
35068+
35069+ h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
35070+ err = PTR_ERR(h_path.dentry);
35071+ if (IS_ERR(h_path.dentry))
35072+ goto out;
35073+
35074+ /* under the same dir, no need to lock_rename() */
523b37e3 35075+ delegated = NULL;
f2c43d5f
AM
35076+ err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
35077+ /*flags*/0);
1facf9fc 35078+ AuTraceErr(err);
523b37e3
AM
35079+ if (unlikely(err == -EWOULDBLOCK)) {
35080+ pr_warn("cannot retry for NFSv4 delegation"
35081+ " for an internal rename\n");
35082+ iput(delegated);
35083+ }
1facf9fc 35084+ dput(h_path.dentry);
35085+
4f0767ce 35086+out:
4a4d8108 35087+ AuTraceErr(err);
1facf9fc 35088+ return err;
35089+}
35090+
35091+/* ---------------------------------------------------------------------- */
35092+/*
35093+ * functions for removing a whiteout
35094+ */
35095+
35096+static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
35097+{
523b37e3
AM
35098+ int err, force;
35099+ struct inode *delegated;
1facf9fc 35100+
35101+ /*
35102+ * forces superio when the dir has a sticky bit.
35103+ * this may be a violation of unix fs semantics.
35104+ */
35105+ force = (h_dir->i_mode & S_ISVTX)
5527c038 35106+ && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
523b37e3
AM
35107+ delegated = NULL;
35108+ err = vfsub_unlink(h_dir, h_path, &delegated, force);
35109+ if (unlikely(err == -EWOULDBLOCK)) {
35110+ pr_warn("cannot retry for NFSv4 delegation"
35111+ " for an internal unlink\n");
35112+ iput(delegated);
35113+ }
35114+ return err;
1facf9fc 35115+}
35116+
35117+int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35118+ struct dentry *dentry)
35119+{
35120+ int err;
35121+
35122+ err = do_unlink_wh(h_dir, h_path);
35123+ if (!err && dentry)
35124+ au_set_dbwh(dentry, -1);
35125+
35126+ return err;
35127+}
35128+
35129+static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
35130+ struct au_branch *br)
35131+{
35132+ int err;
35133+ struct path h_path = {
86dc4139 35134+ .mnt = au_br_mnt(br)
1facf9fc 35135+ };
35136+
35137+ err = 0;
b4510431 35138+ h_path.dentry = vfsub_lkup_one(wh, h_parent);
1facf9fc 35139+ if (IS_ERR(h_path.dentry))
35140+ err = PTR_ERR(h_path.dentry);
35141+ else {
5527c038
JR
35142+ if (d_is_reg(h_path.dentry))
35143+ err = do_unlink_wh(d_inode(h_parent), &h_path);
1facf9fc 35144+ dput(h_path.dentry);
35145+ }
35146+
35147+ return err;
35148+}
35149+
35150+/* ---------------------------------------------------------------------- */
35151+/*
35152+ * initialize/clean whiteout for a branch
35153+ */
35154+
35155+static void au_wh_clean(struct inode *h_dir, struct path *whpath,
35156+ const int isdir)
35157+{
35158+ int err;
523b37e3 35159+ struct inode *delegated;
1facf9fc 35160+
5527c038 35161+ if (d_is_negative(whpath->dentry))
1facf9fc 35162+ return;
35163+
86dc4139
AM
35164+ if (isdir)
35165+ err = vfsub_rmdir(h_dir, whpath);
523b37e3
AM
35166+ else {
35167+ delegated = NULL;
35168+ err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
35169+ if (unlikely(err == -EWOULDBLOCK)) {
35170+ pr_warn("cannot retry for NFSv4 delegation"
35171+ " for an internal unlink\n");
35172+ iput(delegated);
35173+ }
35174+ }
1facf9fc 35175+ if (unlikely(err))
523b37e3
AM
35176+ pr_warn("failed removing %pd (%d), ignored.\n",
35177+ whpath->dentry, err);
1facf9fc 35178+}
35179+
35180+static int test_linkable(struct dentry *h_root)
35181+{
5527c038 35182+ struct inode *h_dir = d_inode(h_root);
1facf9fc 35183+
35184+ if (h_dir->i_op->link)
35185+ return 0;
35186+
523b37e3
AM
35187+ pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
35188+ h_root, au_sbtype(h_root->d_sb));
0b487229 35189+ return -ENOSYS; /* the branch doesn't have its ->link() */
1facf9fc 35190+}
35191+
35192+/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
35193+static int au_whdir(struct inode *h_dir, struct path *path)
35194+{
35195+ int err;
35196+
35197+ err = -EEXIST;
5527c038 35198+ if (d_is_negative(path->dentry)) {
cd7a4cd9 35199+ int mode = 0700;
1facf9fc 35200+
35201+ if (au_test_nfs(path->dentry->d_sb))
cd7a4cd9 35202+ mode |= 0111;
86dc4139 35203+ err = vfsub_mkdir(h_dir, path, mode);
2000de60 35204+ } else if (d_is_dir(path->dentry))
1facf9fc 35205+ err = 0;
35206+ else
523b37e3 35207+ pr_err("unknown %pd exists\n", path->dentry);
1facf9fc 35208+
35209+ return err;
35210+}
35211+
35212+struct au_wh_base {
35213+ const struct qstr *name;
35214+ struct dentry *dentry;
35215+};
35216+
35217+static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
35218+ struct path *h_path)
35219+{
35220+ h_path->dentry = base[AuBrWh_BASE].dentry;
35221+ au_wh_clean(h_dir, h_path, /*isdir*/0);
35222+ h_path->dentry = base[AuBrWh_PLINK].dentry;
35223+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35224+ h_path->dentry = base[AuBrWh_ORPH].dentry;
35225+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35226+}
35227+
35228+/*
35229+ * returns tri-state,
c1595e42 35230+ * minus: error, caller should print the message
acd2b654 35231+ * zero: success
c1595e42 35232+ * plus: error, caller should NOT print the message
1facf9fc 35233+ */
35234+static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
35235+ int do_plink, struct au_wh_base base[],
35236+ struct path *h_path)
35237+{
35238+ int err;
35239+ struct inode *h_dir;
35240+
5527c038 35241+ h_dir = d_inode(h_root);
1facf9fc 35242+ h_path->dentry = base[AuBrWh_BASE].dentry;
35243+ au_wh_clean(h_dir, h_path, /*isdir*/0);
35244+ h_path->dentry = base[AuBrWh_PLINK].dentry;
35245+ if (do_plink) {
35246+ err = test_linkable(h_root);
35247+ if (unlikely(err)) {
35248+ err = 1;
35249+ goto out;
35250+ }
35251+
35252+ err = au_whdir(h_dir, h_path);
35253+ if (unlikely(err))
35254+ goto out;
35255+ wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35256+ } else
35257+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35258+ h_path->dentry = base[AuBrWh_ORPH].dentry;
35259+ err = au_whdir(h_dir, h_path);
35260+ if (unlikely(err))
35261+ goto out;
35262+ wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35263+
4f0767ce 35264+out:
1facf9fc 35265+ return err;
35266+}
35267+
35268+/*
35269+ * for the moment, aufs supports the branch filesystem which does not support
35270+ * link(2). testing on FAT which does not support i_op->setattr() fully either,
35271+ * copyup failed. finally, such filesystem will not be used as the writable
35272+ * branch.
35273+ *
35274+ * returns tri-state, see above.
35275+ */
35276+static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
35277+ int do_plink, struct au_wh_base base[],
35278+ struct path *h_path)
35279+{
35280+ int err;
35281+ struct inode *h_dir;
35282+
1308ab2a 35283+ WbrWhMustWriteLock(wbr);
35284+
1facf9fc 35285+ err = test_linkable(h_root);
35286+ if (unlikely(err)) {
35287+ err = 1;
35288+ goto out;
35289+ }
35290+
35291+ /*
35292+ * todo: should this create be done in /sbin/mount.aufs helper?
35293+ */
35294+ err = -EEXIST;
5527c038
JR
35295+ h_dir = d_inode(h_root);
35296+ if (d_is_negative(base[AuBrWh_BASE].dentry)) {
86dc4139
AM
35297+ h_path->dentry = base[AuBrWh_BASE].dentry;
35298+ err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
7e9cd9fe 35299+ } else if (d_is_reg(base[AuBrWh_BASE].dentry))
1facf9fc 35300+ err = 0;
35301+ else
523b37e3 35302+ pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
1facf9fc 35303+ if (unlikely(err))
35304+ goto out;
35305+
35306+ h_path->dentry = base[AuBrWh_PLINK].dentry;
35307+ if (do_plink) {
35308+ err = au_whdir(h_dir, h_path);
35309+ if (unlikely(err))
35310+ goto out;
35311+ wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35312+ } else
35313+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35314+ wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
35315+
35316+ h_path->dentry = base[AuBrWh_ORPH].dentry;
35317+ err = au_whdir(h_dir, h_path);
35318+ if (unlikely(err))
35319+ goto out;
35320+ wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35321+
4f0767ce 35322+out:
1facf9fc 35323+ return err;
35324+}
35325+
35326+/*
35327+ * initialize the whiteout base file/dir for @br.
35328+ */
86dc4139 35329+int au_wh_init(struct au_branch *br, struct super_block *sb)
1facf9fc 35330+{
35331+ int err, i;
35332+ const unsigned char do_plink
35333+ = !!au_opt_test(au_mntflags(sb), PLINK);
1facf9fc 35334+ struct inode *h_dir;
86dc4139
AM
35335+ struct path path = br->br_path;
35336+ struct dentry *h_root = path.dentry;
1facf9fc 35337+ struct au_wbr *wbr = br->br_wbr;
35338+ static const struct qstr base_name[] = {
0c3ec466
AM
35339+ [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
35340+ sizeof(AUFS_BASE_NAME) - 1),
35341+ [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
35342+ sizeof(AUFS_PLINKDIR_NAME) - 1),
35343+ [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
35344+ sizeof(AUFS_ORPHDIR_NAME) - 1)
1facf9fc 35345+ };
35346+ struct au_wh_base base[] = {
35347+ [AuBrWh_BASE] = {
35348+ .name = base_name + AuBrWh_BASE,
35349+ .dentry = NULL
35350+ },
35351+ [AuBrWh_PLINK] = {
35352+ .name = base_name + AuBrWh_PLINK,
35353+ .dentry = NULL
35354+ },
35355+ [AuBrWh_ORPH] = {
35356+ .name = base_name + AuBrWh_ORPH,
35357+ .dentry = NULL
35358+ }
35359+ };
35360+
1308ab2a 35361+ if (wbr)
35362+ WbrWhMustWriteLock(wbr);
1facf9fc 35363+
1facf9fc 35364+ for (i = 0; i < AuBrWh_Last; i++) {
35365+ /* doubly whiteouted */
35366+ struct dentry *d;
35367+
35368+ d = au_wh_lkup(h_root, (void *)base[i].name, br);
35369+ err = PTR_ERR(d);
35370+ if (IS_ERR(d))
35371+ goto out;
35372+
35373+ base[i].dentry = d;
35374+ AuDebugOn(wbr
35375+ && wbr->wbr_wh[i]
35376+ && wbr->wbr_wh[i] != base[i].dentry);
35377+ }
35378+
35379+ if (wbr)
35380+ for (i = 0; i < AuBrWh_Last; i++) {
35381+ dput(wbr->wbr_wh[i]);
35382+ wbr->wbr_wh[i] = NULL;
35383+ }
35384+
35385+ err = 0;
1e00d052 35386+ if (!au_br_writable(br->br_perm)) {
5527c038 35387+ h_dir = d_inode(h_root);
1facf9fc 35388+ au_wh_init_ro(h_dir, base, &path);
1e00d052 35389+ } else if (!au_br_wh_linkable(br->br_perm)) {
1facf9fc 35390+ err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
35391+ if (err > 0)
35392+ goto out;
35393+ else if (err)
35394+ goto out_err;
1e00d052 35395+ } else {
1facf9fc 35396+ err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
35397+ if (err > 0)
35398+ goto out;
35399+ else if (err)
35400+ goto out_err;
1facf9fc 35401+ }
35402+ goto out; /* success */
35403+
4f0767ce 35404+out_err:
523b37e3
AM
35405+ pr_err("an error(%d) on the writable branch %pd(%s)\n",
35406+ err, h_root, au_sbtype(h_root->d_sb));
4f0767ce 35407+out:
1facf9fc 35408+ for (i = 0; i < AuBrWh_Last; i++)
35409+ dput(base[i].dentry);
35410+ return err;
35411+}
35412+
35413+/* ---------------------------------------------------------------------- */
35414+/*
35415+ * whiteouts are all hard-linked usually.
35416+ * when its link count reaches a ceiling, we create a new whiteout base
35417+ * asynchronously.
35418+ */
35419+
35420+struct reinit_br_wh {
35421+ struct super_block *sb;
35422+ struct au_branch *br;
35423+};
35424+
35425+static void reinit_br_wh(void *arg)
35426+{
35427+ int err;
35428+ aufs_bindex_t bindex;
35429+ struct path h_path;
35430+ struct reinit_br_wh *a = arg;
35431+ struct au_wbr *wbr;
523b37e3 35432+ struct inode *dir, *delegated;
1facf9fc 35433+ struct dentry *h_root;
35434+ struct au_hinode *hdir;
35435+
35436+ err = 0;
35437+ wbr = a->br->br_wbr;
35438+ /* big aufs lock */
35439+ si_noflush_write_lock(a->sb);
35440+ if (!au_br_writable(a->br->br_perm))
35441+ goto out;
35442+ bindex = au_br_index(a->sb, a->br->br_id);
35443+ if (unlikely(bindex < 0))
35444+ goto out;
35445+
1308ab2a 35446+ di_read_lock_parent(a->sb->s_root, AuLock_IR);
5527c038 35447+ dir = d_inode(a->sb->s_root);
1facf9fc 35448+ hdir = au_hi(dir, bindex);
35449+ h_root = au_h_dptr(a->sb->s_root, bindex);
86dc4139 35450+ AuDebugOn(h_root != au_br_dentry(a->br));
1facf9fc 35451+
5afbbe0d 35452+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 35453+ wbr_wh_write_lock(wbr);
35454+ err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
35455+ h_root, a->br);
35456+ if (!err) {
86dc4139
AM
35457+ h_path.dentry = wbr->wbr_whbase;
35458+ h_path.mnt = au_br_mnt(a->br);
523b37e3
AM
35459+ delegated = NULL;
35460+ err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
35461+ /*force*/0);
35462+ if (unlikely(err == -EWOULDBLOCK)) {
35463+ pr_warn("cannot retry for NFSv4 delegation"
35464+ " for an internal unlink\n");
35465+ iput(delegated);
35466+ }
1facf9fc 35467+ } else {
523b37e3 35468+ pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
1facf9fc 35469+ err = 0;
35470+ }
35471+ dput(wbr->wbr_whbase);
35472+ wbr->wbr_whbase = NULL;
35473+ if (!err)
86dc4139 35474+ err = au_wh_init(a->br, a->sb);
1facf9fc 35475+ wbr_wh_write_unlock(wbr);
5afbbe0d 35476+ au_hn_inode_unlock(hdir);
1308ab2a 35477+ di_read_unlock(a->sb->s_root, AuLock_IR);
076b876e
AM
35478+ if (!err)
35479+ au_fhsm_wrote(a->sb, bindex, /*force*/0);
1facf9fc 35480+
4f0767ce 35481+out:
1facf9fc 35482+ if (wbr)
35483+ atomic_dec(&wbr->wbr_wh_running);
acd2b654 35484+ au_lcnt_dec(&a->br->br_count);
1facf9fc 35485+ si_write_unlock(a->sb);
027c5e7a 35486+ au_nwt_done(&au_sbi(a->sb)->si_nowait);
e49925d1 35487+ au_kfree_rcu(a);
1facf9fc 35488+ if (unlikely(err))
35489+ AuIOErr("err %d\n", err);
35490+}
35491+
35492+static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
35493+{
35494+ int do_dec, wkq_err;
35495+ struct reinit_br_wh *arg;
35496+
35497+ do_dec = 1;
35498+ if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
35499+ goto out;
35500+
35501+ /* ignore ENOMEM */
35502+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
35503+ if (arg) {
35504+ /*
35505+ * dec(wh_running), kfree(arg) and dec(br_count)
35506+ * in reinit function
35507+ */
35508+ arg->sb = sb;
35509+ arg->br = br;
acd2b654 35510+ au_lcnt_inc(&br->br_count);
53392da6 35511+ wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
1facf9fc 35512+ if (unlikely(wkq_err)) {
35513+ atomic_dec(&br->br_wbr->wbr_wh_running);
acd2b654 35514+ au_lcnt_dec(&br->br_count);
e49925d1 35515+ au_kfree_rcu(arg);
1facf9fc 35516+ }
35517+ do_dec = 0;
35518+ }
35519+
4f0767ce 35520+out:
1facf9fc 35521+ if (do_dec)
35522+ atomic_dec(&br->br_wbr->wbr_wh_running);
35523+}
35524+
35525+/* ---------------------------------------------------------------------- */
35526+
35527+/*
35528+ * create the whiteout @wh.
35529+ */
35530+static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
35531+ struct dentry *wh)
35532+{
35533+ int err;
35534+ struct path h_path = {
35535+ .dentry = wh
35536+ };
35537+ struct au_branch *br;
35538+ struct au_wbr *wbr;
35539+ struct dentry *h_parent;
523b37e3 35540+ struct inode *h_dir, *delegated;
1facf9fc 35541+
35542+ h_parent = wh->d_parent; /* dir inode is locked */
5527c038 35543+ h_dir = d_inode(h_parent);
1facf9fc 35544+ IMustLock(h_dir);
35545+
35546+ br = au_sbr(sb, bindex);
86dc4139 35547+ h_path.mnt = au_br_mnt(br);
1facf9fc 35548+ wbr = br->br_wbr;
35549+ wbr_wh_read_lock(wbr);
35550+ if (wbr->wbr_whbase) {
523b37e3
AM
35551+ delegated = NULL;
35552+ err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
35553+ if (unlikely(err == -EWOULDBLOCK)) {
35554+ pr_warn("cannot retry for NFSv4 delegation"
35555+ " for an internal link\n");
35556+ iput(delegated);
35557+ }
1facf9fc 35558+ if (!err || err != -EMLINK)
35559+ goto out;
35560+
35561+ /* link count full. re-initialize br_whbase. */
35562+ kick_reinit_br_wh(sb, br);
35563+ }
35564+
35565+ /* return this error in this context */
b4510431 35566+ err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
076b876e
AM
35567+ if (!err)
35568+ au_fhsm_wrote(sb, bindex, /*force*/0);
1facf9fc 35569+
4f0767ce 35570+out:
1facf9fc 35571+ wbr_wh_read_unlock(wbr);
35572+ return err;
35573+}
35574+
35575+/* ---------------------------------------------------------------------- */
35576+
35577+/*
35578+ * create or remove the diropq.
35579+ */
35580+static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
35581+ unsigned int flags)
35582+{
35583+ struct dentry *opq_dentry, *h_dentry;
35584+ struct super_block *sb;
35585+ struct au_branch *br;
35586+ int err;
35587+
35588+ sb = dentry->d_sb;
35589+ br = au_sbr(sb, bindex);
35590+ h_dentry = au_h_dptr(dentry, bindex);
b4510431 35591+ opq_dentry = vfsub_lkup_one(&diropq_name, h_dentry);
1facf9fc 35592+ if (IS_ERR(opq_dentry))
35593+ goto out;
35594+
35595+ if (au_ftest_diropq(flags, CREATE)) {
35596+ err = link_or_create_wh(sb, bindex, opq_dentry);
35597+ if (!err) {
35598+ au_set_dbdiropq(dentry, bindex);
35599+ goto out; /* success */
35600+ }
35601+ } else {
35602+ struct path tmp = {
35603+ .dentry = opq_dentry,
86dc4139 35604+ .mnt = au_br_mnt(br)
1facf9fc 35605+ };
5527c038 35606+ err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &tmp);
1facf9fc 35607+ if (!err)
35608+ au_set_dbdiropq(dentry, -1);
35609+ }
35610+ dput(opq_dentry);
35611+ opq_dentry = ERR_PTR(err);
35612+
4f0767ce 35613+out:
1facf9fc 35614+ return opq_dentry;
35615+}
35616+
35617+struct do_diropq_args {
35618+ struct dentry **errp;
35619+ struct dentry *dentry;
35620+ aufs_bindex_t bindex;
35621+ unsigned int flags;
35622+};
35623+
35624+static void call_do_diropq(void *args)
35625+{
35626+ struct do_diropq_args *a = args;
35627+ *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
35628+}
35629+
35630+struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35631+ unsigned int flags)
35632+{
35633+ struct dentry *diropq, *h_dentry;
35634+
35635+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 35636+ if (!au_test_h_perm_sio(d_inode(h_dentry), MAY_EXEC | MAY_WRITE))
1facf9fc 35637+ diropq = do_diropq(dentry, bindex, flags);
35638+ else {
35639+ int wkq_err;
35640+ struct do_diropq_args args = {
35641+ .errp = &diropq,
35642+ .dentry = dentry,
35643+ .bindex = bindex,
35644+ .flags = flags
35645+ };
35646+
35647+ wkq_err = au_wkq_wait(call_do_diropq, &args);
35648+ if (unlikely(wkq_err))
35649+ diropq = ERR_PTR(wkq_err);
35650+ }
35651+
35652+ return diropq;
35653+}
35654+
35655+/* ---------------------------------------------------------------------- */
35656+
35657+/*
35658+ * lookup whiteout dentry.
35659+ * @h_parent: lower parent dentry which must exist and be locked
35660+ * @base_name: name of dentry which will be whiteouted
35661+ * returns dentry for whiteout.
35662+ */
35663+struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35664+ struct au_branch *br)
35665+{
35666+ int err;
35667+ struct qstr wh_name;
35668+ struct dentry *wh_dentry;
35669+
35670+ err = au_wh_name_alloc(&wh_name, base_name);
35671+ wh_dentry = ERR_PTR(err);
35672+ if (!err) {
b4510431 35673+ wh_dentry = vfsub_lkup_one(&wh_name, h_parent);
e49925d1 35674+ au_kfree_try_rcu(wh_name.name);
1facf9fc 35675+ }
35676+ return wh_dentry;
35677+}
35678+
35679+/*
35680+ * link/create a whiteout for @dentry on @bindex.
35681+ */
35682+struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35683+ struct dentry *h_parent)
35684+{
35685+ struct dentry *wh_dentry;
35686+ struct super_block *sb;
35687+ int err;
35688+
35689+ sb = dentry->d_sb;
35690+ wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
5527c038 35691+ if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
1facf9fc 35692+ err = link_or_create_wh(sb, bindex, wh_dentry);
076b876e 35693+ if (!err) {
1facf9fc 35694+ au_set_dbwh(dentry, bindex);
076b876e
AM
35695+ au_fhsm_wrote(sb, bindex, /*force*/0);
35696+ } else {
1facf9fc 35697+ dput(wh_dentry);
35698+ wh_dentry = ERR_PTR(err);
35699+ }
35700+ }
35701+
35702+ return wh_dentry;
35703+}
35704+
35705+/* ---------------------------------------------------------------------- */
35706+
35707+/* Delete all whiteouts in this directory on branch bindex. */
35708+static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
35709+ aufs_bindex_t bindex, struct au_branch *br)
35710+{
35711+ int err;
35712+ unsigned long ul, n;
35713+ struct qstr wh_name;
35714+ char *p;
35715+ struct hlist_head *head;
c06a8ce3 35716+ struct au_vdir_wh *pos;
1facf9fc 35717+ struct au_vdir_destr *str;
35718+
35719+ err = -ENOMEM;
537831f9 35720+ p = (void *)__get_free_page(GFP_NOFS);
1facf9fc 35721+ wh_name.name = p;
35722+ if (unlikely(!wh_name.name))
35723+ goto out;
35724+
35725+ err = 0;
35726+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35727+ p += AUFS_WH_PFX_LEN;
35728+ n = whlist->nh_num;
35729+ head = whlist->nh_head;
35730+ for (ul = 0; !err && ul < n; ul++, head++) {
c06a8ce3
AM
35731+ hlist_for_each_entry(pos, head, wh_hash) {
35732+ if (pos->wh_bindex != bindex)
1facf9fc 35733+ continue;
35734+
c06a8ce3 35735+ str = &pos->wh_str;
1facf9fc 35736+ if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
35737+ memcpy(p, str->name, str->len);
35738+ wh_name.len = AUFS_WH_PFX_LEN + str->len;
35739+ err = unlink_wh_name(h_dentry, &wh_name, br);
35740+ if (!err)
35741+ continue;
35742+ break;
35743+ }
35744+ AuIOErr("whiteout name too long %.*s\n",
35745+ str->len, str->name);
35746+ err = -EIO;
35747+ break;
35748+ }
35749+ }
1c60b727 35750+ free_page((unsigned long)wh_name.name);
1facf9fc 35751+
4f0767ce 35752+out:
1facf9fc 35753+ return err;
35754+}
35755+
35756+struct del_wh_children_args {
35757+ int *errp;
35758+ struct dentry *h_dentry;
1308ab2a 35759+ struct au_nhash *whlist;
1facf9fc 35760+ aufs_bindex_t bindex;
35761+ struct au_branch *br;
35762+};
35763+
35764+static void call_del_wh_children(void *args)
35765+{
35766+ struct del_wh_children_args *a = args;
1308ab2a 35767+ *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
1facf9fc 35768+}
35769+
35770+/* ---------------------------------------------------------------------- */
35771+
35772+struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
35773+{
35774+ struct au_whtmp_rmdir *whtmp;
dece6358 35775+ int err;
1308ab2a 35776+ unsigned int rdhash;
dece6358
AM
35777+
35778+ SiMustAnyLock(sb);
1facf9fc 35779+
be52b249 35780+ whtmp = kzalloc(sizeof(*whtmp), gfp);
dece6358
AM
35781+ if (unlikely(!whtmp)) {
35782+ whtmp = ERR_PTR(-ENOMEM);
1facf9fc 35783+ goto out;
dece6358 35784+ }
1facf9fc 35785+
1308ab2a 35786+ /* no estimation for dir size */
35787+ rdhash = au_sbi(sb)->si_rdhash;
35788+ if (!rdhash)
35789+ rdhash = AUFS_RDHASH_DEF;
35790+ err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
35791+ if (unlikely(err)) {
e49925d1 35792+ au_kfree_rcu(whtmp);
1308ab2a 35793+ whtmp = ERR_PTR(err);
35794+ }
dece6358 35795+
4f0767ce 35796+out:
dece6358 35797+ return whtmp;
1facf9fc 35798+}
35799+
35800+void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
35801+{
027c5e7a 35802+ if (whtmp->br)
acd2b654 35803+ au_lcnt_dec(&whtmp->br->br_count);
1facf9fc 35804+ dput(whtmp->wh_dentry);
35805+ iput(whtmp->dir);
dece6358 35806+ au_nhash_wh_free(&whtmp->whlist);
e49925d1 35807+ au_kfree_rcu(whtmp);
1facf9fc 35808+}
35809+
35810+/*
35811+ * rmdir the whiteouted temporary named dir @h_dentry.
35812+ * @whlist: whiteouted children.
35813+ */
35814+int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35815+ struct dentry *wh_dentry, struct au_nhash *whlist)
35816+{
35817+ int err;
2000de60 35818+ unsigned int h_nlink;
1facf9fc 35819+ struct path h_tmp;
35820+ struct inode *wh_inode, *h_dir;
35821+ struct au_branch *br;
35822+
5527c038 35823+ h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
1facf9fc 35824+ IMustLock(h_dir);
35825+
35826+ br = au_sbr(dir->i_sb, bindex);
5527c038 35827+ wh_inode = d_inode(wh_dentry);
febd17d6 35828+ inode_lock_nested(wh_inode, AuLsc_I_CHILD);
1facf9fc 35829+
35830+ /*
35831+ * someone else might change some whiteouts while we were sleeping.
35832+ * it means this whlist may have an obsoleted entry.
35833+ */
35834+ if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
35835+ err = del_wh_children(wh_dentry, whlist, bindex, br);
35836+ else {
35837+ int wkq_err;
35838+ struct del_wh_children_args args = {
35839+ .errp = &err,
35840+ .h_dentry = wh_dentry,
1308ab2a 35841+ .whlist = whlist,
1facf9fc 35842+ .bindex = bindex,
35843+ .br = br
35844+ };
35845+
35846+ wkq_err = au_wkq_wait(call_del_wh_children, &args);
35847+ if (unlikely(wkq_err))
35848+ err = wkq_err;
35849+ }
febd17d6 35850+ inode_unlock(wh_inode);
1facf9fc 35851+
35852+ if (!err) {
35853+ h_tmp.dentry = wh_dentry;
86dc4139 35854+ h_tmp.mnt = au_br_mnt(br);
2000de60 35855+ h_nlink = h_dir->i_nlink;
1facf9fc 35856+ err = vfsub_rmdir(h_dir, &h_tmp);
2000de60
JR
35857+ /* some fs doesn't change the parent nlink in some cases */
35858+ h_nlink -= h_dir->i_nlink;
1facf9fc 35859+ }
35860+
35861+ if (!err) {
5afbbe0d 35862+ if (au_ibtop(dir) == bindex) {
7f207e10 35863+ /* todo: dir->i_mutex is necessary */
1facf9fc 35864+ au_cpup_attr_timesizes(dir);
2000de60
JR
35865+ if (h_nlink)
35866+ vfsub_drop_nlink(dir);
1facf9fc 35867+ }
35868+ return 0; /* success */
35869+ }
35870+
523b37e3 35871+ pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
1facf9fc 35872+ return err;
35873+}
35874+
35875+static void call_rmdir_whtmp(void *args)
35876+{
35877+ int err;
e49829fe 35878+ aufs_bindex_t bindex;
1facf9fc 35879+ struct au_whtmp_rmdir *a = args;
35880+ struct super_block *sb;
35881+ struct dentry *h_parent;
35882+ struct inode *h_dir;
1facf9fc 35883+ struct au_hinode *hdir;
35884+
35885+ /* rmdir by nfsd may cause deadlock with this i_mutex */
febd17d6 35886+ /* inode_lock(a->dir); */
e49829fe 35887+ err = -EROFS;
1facf9fc 35888+ sb = a->dir->i_sb;
e49829fe
JR
35889+ si_read_lock(sb, !AuLock_FLUSH);
35890+ if (!au_br_writable(a->br->br_perm))
35891+ goto out;
35892+ bindex = au_br_index(sb, a->br->br_id);
35893+ if (unlikely(bindex < 0))
1facf9fc 35894+ goto out;
35895+
35896+ err = -EIO;
1facf9fc 35897+ ii_write_lock_parent(a->dir);
35898+ h_parent = dget_parent(a->wh_dentry);
5527c038 35899+ h_dir = d_inode(h_parent);
e49829fe 35900+ hdir = au_hi(a->dir, bindex);
86dc4139
AM
35901+ err = vfsub_mnt_want_write(au_br_mnt(a->br));
35902+ if (unlikely(err))
35903+ goto out_mnt;
5afbbe0d 35904+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
e49829fe
JR
35905+ err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
35906+ a->br);
86dc4139
AM
35907+ if (!err)
35908+ err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
5afbbe0d 35909+ au_hn_inode_unlock(hdir);
86dc4139
AM
35910+ vfsub_mnt_drop_write(au_br_mnt(a->br));
35911+
35912+out_mnt:
1facf9fc 35913+ dput(h_parent);
35914+ ii_write_unlock(a->dir);
4f0767ce 35915+out:
febd17d6 35916+ /* inode_unlock(a->dir); */
1facf9fc 35917+ au_whtmp_rmdir_free(a);
027c5e7a
AM
35918+ si_read_unlock(sb);
35919+ au_nwt_done(&au_sbi(sb)->si_nowait);
1facf9fc 35920+ if (unlikely(err))
35921+ AuIOErr("err %d\n", err);
35922+}
35923+
35924+void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35925+ struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
35926+{
35927+ int wkq_err;
e49829fe 35928+ struct super_block *sb;
1facf9fc 35929+
35930+ IMustLock(dir);
35931+
35932+ /* all post-process will be done in do_rmdir_whtmp(). */
e49829fe 35933+ sb = dir->i_sb;
1facf9fc 35934+ args->dir = au_igrab(dir);
e49829fe 35935+ args->br = au_sbr(sb, bindex);
acd2b654 35936+ au_lcnt_inc(&args->br->br_count);
1facf9fc 35937+ args->wh_dentry = dget(wh_dentry);
53392da6 35938+ wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
1facf9fc 35939+ if (unlikely(wkq_err)) {
523b37e3 35940+ pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
1facf9fc 35941+ au_whtmp_rmdir_free(args);
35942+ }
35943+}
7f207e10
AM
35944diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
35945--- /usr/share/empty/fs/aufs/whout.h 1970-01-01 01:00:00.000000000 +0100
0b487229 35946+++ linux/fs/aufs/whout.h 2021-02-22 23:30:37.702680911 +0100
062440b3
AM
35947@@ -0,0 +1,86 @@
35948+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 35949+/*
0b487229 35950+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 35951+ *
35952+ * This program, aufs is free software; you can redistribute it and/or modify
35953+ * it under the terms of the GNU General Public License as published by
35954+ * the Free Software Foundation; either version 2 of the License, or
35955+ * (at your option) any later version.
dece6358
AM
35956+ *
35957+ * This program is distributed in the hope that it will be useful,
35958+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
35959+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35960+ * GNU General Public License for more details.
35961+ *
35962+ * You should have received a copy of the GNU General Public License
523b37e3 35963+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 35964+ */
35965+
35966+/*
35967+ * whiteout for logical deletion and opaque directory
35968+ */
35969+
35970+#ifndef __AUFS_WHOUT_H__
35971+#define __AUFS_WHOUT_H__
35972+
35973+#ifdef __KERNEL__
35974+
1facf9fc 35975+#include "dir.h"
35976+
35977+/* whout.c */
35978+int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
076b876e
AM
35979+int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio);
35980+int au_diropq_test(struct dentry *h_dentry);
3c1bdaff 35981+struct au_branch;
1facf9fc 35982+struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
35983+ struct qstr *prefix);
35984+int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
35985+int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35986+ struct dentry *dentry);
86dc4139 35987+int au_wh_init(struct au_branch *br, struct super_block *sb);
1facf9fc 35988+
35989+/* diropq flags */
35990+#define AuDiropq_CREATE 1
35991+#define au_ftest_diropq(flags, name) ((flags) & AuDiropq_##name)
7f207e10
AM
35992+#define au_fset_diropq(flags, name) \
35993+ do { (flags) |= AuDiropq_##name; } while (0)
35994+#define au_fclr_diropq(flags, name) \
35995+ do { (flags) &= ~AuDiropq_##name; } while (0)
1facf9fc 35996+
35997+struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35998+ unsigned int flags);
35999+struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
36000+ struct au_branch *br);
36001+struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
36002+ struct dentry *h_parent);
36003+
36004+/* real rmdir for the whiteout-ed dir */
36005+struct au_whtmp_rmdir {
36006+ struct inode *dir;
e49829fe 36007+ struct au_branch *br;
1facf9fc 36008+ struct dentry *wh_dentry;
dece6358 36009+ struct au_nhash whlist;
1facf9fc 36010+};
36011+
36012+struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
36013+void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
36014+int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
36015+ struct dentry *wh_dentry, struct au_nhash *whlist);
36016+void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
36017+ struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
36018+
36019+/* ---------------------------------------------------------------------- */
36020+
36021+static inline struct dentry *au_diropq_create(struct dentry *dentry,
36022+ aufs_bindex_t bindex)
36023+{
36024+ return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
36025+}
36026+
36027+static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
36028+{
36029+ return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
36030+}
36031+
36032+#endif /* __KERNEL__ */
36033+#endif /* __AUFS_WHOUT_H__ */
7f207e10
AM
36034diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
36035--- /usr/share/empty/fs/aufs/wkq.c 1970-01-01 01:00:00.000000000 +0100
0b487229
JR
36036+++ linux/fs/aufs/wkq.c 2021-02-22 23:30:37.702680911 +0100
36037@@ -0,0 +1,372 @@
cd7a4cd9 36038+// SPDX-License-Identifier: GPL-2.0
1facf9fc 36039+/*
0b487229 36040+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 36041+ *
36042+ * This program, aufs is free software; you can redistribute it and/or modify
36043+ * it under the terms of the GNU General Public License as published by
36044+ * the Free Software Foundation; either version 2 of the License, or
36045+ * (at your option) any later version.
dece6358
AM
36046+ *
36047+ * This program is distributed in the hope that it will be useful,
36048+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36049+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36050+ * GNU General Public License for more details.
36051+ *
36052+ * You should have received a copy of the GNU General Public License
523b37e3 36053+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 36054+ */
36055+
36056+/*
36057+ * workqueue for asynchronous/super-io operations
acd2b654 36058+ * todo: try new credential scheme
1facf9fc 36059+ */
36060+
dece6358 36061+#include <linux/module.h>
1facf9fc 36062+#include "aufs.h"
36063+
9dbd164d 36064+/* internal workqueue named AUFS_WKQ_NAME */
b752ccd1 36065+
9dbd164d 36066+static struct workqueue_struct *au_wkq;
1facf9fc 36067+
36068+struct au_wkinfo {
36069+ struct work_struct wk;
7f207e10 36070+ struct kobject *kobj;
1facf9fc 36071+
36072+ unsigned int flags; /* see wkq.h */
36073+
36074+ au_wkq_func_t func;
36075+ void *args;
36076+
8b6a4947
AM
36077+#ifdef CONFIG_LOCKDEP
36078+ int dont_check;
36079+ struct held_lock **hlock;
36080+#endif
36081+
1facf9fc 36082+ struct completion *comp;
36083+};
36084+
36085+/* ---------------------------------------------------------------------- */
8b6a4947
AM
36086+/*
36087+ * Aufs passes some operations to the workqueue such as the internal copyup.
36088+ * This scheme looks rather unnatural for LOCKDEP debugging feature, since the
36089+ * job run by workqueue depends upon the locks acquired in the other task.
36090+ * Delegating a small operation to the workqueue, aufs passes its lockdep
36091+ * information too. And the job in the workqueue restores the info in order to
36092+ * pretend as if it acquired those locks. This is just to make LOCKDEP work
36093+ * correctly and expectedly.
36094+ */
36095+
36096+#ifndef CONFIG_LOCKDEP
36097+AuStubInt0(au_wkq_lockdep_alloc, struct au_wkinfo *wkinfo);
36098+AuStubVoid(au_wkq_lockdep_free, struct au_wkinfo *wkinfo);
36099+AuStubVoid(au_wkq_lockdep_pre, struct au_wkinfo *wkinfo);
36100+AuStubVoid(au_wkq_lockdep_post, struct au_wkinfo *wkinfo);
36101+AuStubVoid(au_wkq_lockdep_init, struct au_wkinfo *wkinfo);
36102+#else
36103+static void au_wkq_lockdep_init(struct au_wkinfo *wkinfo)
36104+{
36105+ wkinfo->hlock = NULL;
36106+ wkinfo->dont_check = 0;
36107+}
36108+
36109+/*
36110+ * 1: matched
36111+ * 0: unmatched
36112+ */
36113+static int au_wkq_lockdep_test(struct lock_class_key *key, const char *name)
36114+{
36115+ static DEFINE_SPINLOCK(spin);
36116+ static struct {
36117+ char *name;
36118+ struct lock_class_key *key;
36119+ } a[] = {
36120+ { .name = "&sbinfo->si_rwsem" },
36121+ { .name = "&finfo->fi_rwsem" },
36122+ { .name = "&dinfo->di_rwsem" },
36123+ { .name = "&iinfo->ii_rwsem" }
36124+ };
36125+ static int set;
36126+ int i;
36127+
36128+ /* lockless read from 'set.' see below */
36129+ if (set == ARRAY_SIZE(a)) {
36130+ for (i = 0; i < ARRAY_SIZE(a); i++)
36131+ if (a[i].key == key)
36132+ goto match;
36133+ goto unmatch;
36134+ }
36135+
36136+ spin_lock(&spin);
36137+ if (set)
36138+ for (i = 0; i < ARRAY_SIZE(a); i++)
36139+ if (a[i].key == key) {
36140+ spin_unlock(&spin);
36141+ goto match;
36142+ }
36143+ for (i = 0; i < ARRAY_SIZE(a); i++) {
36144+ if (a[i].key) {
36145+ if (unlikely(a[i].key == key)) { /* rare but possible */
36146+ spin_unlock(&spin);
36147+ goto match;
36148+ } else
36149+ continue;
36150+ }
36151+ if (strstr(a[i].name, name)) {
36152+ /*
36153+ * the order of these three lines is important for the
36154+ * lockless read above.
36155+ */
36156+ a[i].key = key;
36157+ spin_unlock(&spin);
36158+ set++;
36159+ /* AuDbg("%d, %s\n", set, name); */
36160+ goto match;
36161+ }
36162+ }
36163+ spin_unlock(&spin);
36164+ goto unmatch;
36165+
36166+match:
36167+ return 1;
36168+unmatch:
36169+ return 0;
36170+}
36171+
36172+static int au_wkq_lockdep_alloc(struct au_wkinfo *wkinfo)
36173+{
36174+ int err, n;
36175+ struct task_struct *curr;
36176+ struct held_lock **hl, *held_locks, *p;
36177+
36178+ err = 0;
36179+ curr = current;
36180+ wkinfo->dont_check = lockdep_recursing(curr);
36181+ if (wkinfo->dont_check)
36182+ goto out;
36183+ n = curr->lockdep_depth;
36184+ if (!n)
36185+ goto out;
36186+
36187+ err = -ENOMEM;
36188+ wkinfo->hlock = kmalloc_array(n + 1, sizeof(*wkinfo->hlock), GFP_NOFS);
36189+ if (unlikely(!wkinfo->hlock))
36190+ goto out;
36191+
36192+ err = 0;
0b487229
JR
36193+#if 0 /* left for debugging */
36194+ if (0 && au_debug_test())
8b6a4947
AM
36195+ lockdep_print_held_locks(curr);
36196+#endif
36197+ held_locks = curr->held_locks;
36198+ hl = wkinfo->hlock;
36199+ while (n--) {
36200+ p = held_locks++;
36201+ if (au_wkq_lockdep_test(p->instance->key, p->instance->name))
36202+ *hl++ = p;
36203+ }
36204+ *hl = NULL;
36205+
36206+out:
36207+ return err;
36208+}
36209+
36210+static void au_wkq_lockdep_free(struct au_wkinfo *wkinfo)
36211+{
e49925d1 36212+ au_kfree_try_rcu(wkinfo->hlock);
8b6a4947
AM
36213+}
36214+
36215+static void au_wkq_lockdep_pre(struct au_wkinfo *wkinfo)
36216+{
36217+ struct held_lock *p, **hl = wkinfo->hlock;
36218+ int subclass;
36219+
36220+ if (wkinfo->dont_check)
36221+ lockdep_off();
36222+ if (!hl)
36223+ return;
36224+ while ((p = *hl++)) { /* assignment */
36225+ subclass = lockdep_hlock_class(p)->subclass;
36226+ /* AuDbg("%s, %d\n", p->instance->name, subclass); */
36227+ if (p->read)
36228+ rwsem_acquire_read(p->instance, subclass, 0,
36229+ /*p->acquire_ip*/_RET_IP_);
36230+ else
36231+ rwsem_acquire(p->instance, subclass, 0,
36232+ /*p->acquire_ip*/_RET_IP_);
36233+ }
36234+}
36235+
36236+static void au_wkq_lockdep_post(struct au_wkinfo *wkinfo)
36237+{
36238+ struct held_lock *p, **hl = wkinfo->hlock;
36239+
36240+ if (wkinfo->dont_check)
36241+ lockdep_on();
36242+ if (!hl)
36243+ return;
36244+ while ((p = *hl++)) /* assignment */
36245+ rwsem_release(p->instance, 0, /*p->acquire_ip*/_RET_IP_);
36246+}
36247+#endif
1facf9fc 36248+
1facf9fc 36249+static void wkq_func(struct work_struct *wk)
36250+{
36251+ struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
36252+
2dfbb274 36253+ AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
7f207e10
AM
36254+ AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
36255+
8b6a4947 36256+ au_wkq_lockdep_pre(wkinfo);
1facf9fc 36257+ wkinfo->func(wkinfo->args);
8b6a4947 36258+ au_wkq_lockdep_post(wkinfo);
1facf9fc 36259+ if (au_ftest_wkq(wkinfo->flags, WAIT))
36260+ complete(wkinfo->comp);
36261+ else {
7f207e10 36262+ kobject_put(wkinfo->kobj);
9dbd164d 36263+ module_put(THIS_MODULE); /* todo: ?? */
e49925d1 36264+ au_kfree_rcu(wkinfo);
1facf9fc 36265+ }
36266+}
36267+
36268+/*
36269+ * Since struct completion is large, try allocating it dynamically.
36270+ */
1facf9fc 36271+#define AuWkqCompDeclare(name) struct completion *comp = NULL
36272+
36273+static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
36274+{
36275+ *comp = kmalloc(sizeof(**comp), GFP_NOFS);
36276+ if (*comp) {
36277+ init_completion(*comp);
36278+ wkinfo->comp = *comp;
36279+ return 0;
36280+ }
36281+ return -ENOMEM;
36282+}
36283+
36284+static void au_wkq_comp_free(struct completion *comp)
36285+{
e49925d1 36286+ au_kfree_rcu(comp);
1facf9fc 36287+}
36288+
53392da6 36289+static void au_wkq_run(struct au_wkinfo *wkinfo)
1facf9fc 36290+{
53392da6
AM
36291+ if (au_ftest_wkq(wkinfo->flags, NEST)) {
36292+ if (au_wkq_test()) {
38d290e6 36293+ AuWarn1("wkq from wkq, unless silly-rename on NFS,"
acd2b654
AM
36294+ " due to a dead dir by UDBA,"
36295+ " or async xino write?\n");
53392da6
AM
36296+ AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
36297+ }
36298+ } else
36299+ au_dbg_verify_kthread();
36300+
36301+ if (au_ftest_wkq(wkinfo->flags, WAIT)) {
a1f66529 36302+ INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
9dbd164d 36303+ queue_work(au_wkq, &wkinfo->wk);
4a4d8108
AM
36304+ } else {
36305+ INIT_WORK(&wkinfo->wk, wkq_func);
36306+ schedule_work(&wkinfo->wk);
36307+ }
1facf9fc 36308+}
36309+
7f207e10
AM
36310+/*
36311+ * Be careful. It is easy to make deadlock happen.
36312+ * processA: lock, wkq and wait
36313+ * processB: wkq and wait, lock in wkq
36314+ * --> deadlock
36315+ */
b752ccd1 36316+int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
1facf9fc 36317+{
36318+ int err;
36319+ AuWkqCompDeclare(comp);
36320+ struct au_wkinfo wkinfo = {
b752ccd1 36321+ .flags = flags,
1facf9fc 36322+ .func = func,
36323+ .args = args
36324+ };
36325+
36326+ err = au_wkq_comp_alloc(&wkinfo, &comp);
8b6a4947
AM
36327+ if (unlikely(err))
36328+ goto out;
36329+ err = au_wkq_lockdep_alloc(&wkinfo);
36330+ if (unlikely(err))
36331+ goto out_comp;
1facf9fc 36332+ if (!err) {
53392da6 36333+ au_wkq_run(&wkinfo);
1facf9fc 36334+ /* no timeout, no interrupt */
36335+ wait_for_completion(wkinfo.comp);
1facf9fc 36336+ }
8b6a4947 36337+ au_wkq_lockdep_free(&wkinfo);
1facf9fc 36338+
8b6a4947
AM
36339+out_comp:
36340+ au_wkq_comp_free(comp);
36341+out:
36342+ destroy_work_on_stack(&wkinfo.wk);
1facf9fc 36343+ return err;
1facf9fc 36344+}
36345+
027c5e7a
AM
36346+/*
36347+ * Note: dget/dput() in func for aufs dentries are not supported. It will be a
36348+ * problem in a concurrent umounting.
36349+ */
53392da6
AM
36350+int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36351+ unsigned int flags)
1facf9fc 36352+{
36353+ int err;
36354+ struct au_wkinfo *wkinfo;
36355+
f0c0a007 36356+ atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
1facf9fc 36357+
36358+ /*
36359+ * wkq_func() must free this wkinfo.
36360+ * it highly depends upon the implementation of workqueue.
36361+ */
36362+ err = 0;
36363+ wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
36364+ if (wkinfo) {
7f207e10 36365+ wkinfo->kobj = &au_sbi(sb)->si_kobj;
53392da6 36366+ wkinfo->flags = flags & ~AuWkq_WAIT;
1facf9fc 36367+ wkinfo->func = func;
36368+ wkinfo->args = args;
36369+ wkinfo->comp = NULL;
8b6a4947 36370+ au_wkq_lockdep_init(wkinfo);
7f207e10 36371+ kobject_get(wkinfo->kobj);
9dbd164d 36372+ __module_get(THIS_MODULE); /* todo: ?? */
1facf9fc 36373+
53392da6 36374+ au_wkq_run(wkinfo);
1facf9fc 36375+ } else {
36376+ err = -ENOMEM;
e49829fe 36377+ au_nwt_done(&au_sbi(sb)->si_nowait);
1facf9fc 36378+ }
36379+
36380+ return err;
36381+}
36382+
36383+/* ---------------------------------------------------------------------- */
36384+
36385+void au_nwt_init(struct au_nowait_tasks *nwt)
36386+{
f0c0a007
AM
36387+ atomic_set(&nwt->nw_len, 0);
36388+ /* smp_mb(); */ /* atomic_set */
1facf9fc 36389+ init_waitqueue_head(&nwt->nw_wq);
36390+}
36391+
36392+void au_wkq_fin(void)
36393+{
9dbd164d 36394+ destroy_workqueue(au_wkq);
1facf9fc 36395+}
36396+
36397+int __init au_wkq_init(void)
36398+{
9dbd164d 36399+ int err;
b752ccd1
AM
36400+
36401+ err = 0;
86dc4139 36402+ au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
9dbd164d
AM
36403+ if (IS_ERR(au_wkq))
36404+ err = PTR_ERR(au_wkq);
36405+ else if (!au_wkq)
36406+ err = -ENOMEM;
b752ccd1
AM
36407+
36408+ return err;
1facf9fc 36409+}
7f207e10
AM
36410diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
36411--- /usr/share/empty/fs/aufs/wkq.h 1970-01-01 01:00:00.000000000 +0100
0b487229 36412+++ linux/fs/aufs/wkq.h 2021-02-22 23:30:37.702680911 +0100
acd2b654 36413@@ -0,0 +1,89 @@
062440b3 36414+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 36415+/*
0b487229 36416+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 36417+ *
36418+ * This program, aufs is free software; you can redistribute it and/or modify
36419+ * it under the terms of the GNU General Public License as published by
36420+ * the Free Software Foundation; either version 2 of the License, or
36421+ * (at your option) any later version.
dece6358
AM
36422+ *
36423+ * This program is distributed in the hope that it will be useful,
36424+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36425+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36426+ * GNU General Public License for more details.
36427+ *
36428+ * You should have received a copy of the GNU General Public License
523b37e3 36429+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 36430+ */
36431+
36432+/*
36433+ * workqueue for asynchronous/super-io operations
36434+ * todo: try new credentials management scheme
36435+ */
36436+
36437+#ifndef __AUFS_WKQ_H__
36438+#define __AUFS_WKQ_H__
36439+
36440+#ifdef __KERNEL__
36441+
8b6a4947 36442+#include <linux/wait.h>
5afbbe0d 36443+
dece6358
AM
36444+struct super_block;
36445+
1facf9fc 36446+/* ---------------------------------------------------------------------- */
36447+
36448+/*
36449+ * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
36450+ */
36451+struct au_nowait_tasks {
f0c0a007 36452+ atomic_t nw_len;
1facf9fc 36453+ wait_queue_head_t nw_wq;
36454+};
36455+
36456+/* ---------------------------------------------------------------------- */
36457+
36458+typedef void (*au_wkq_func_t)(void *args);
36459+
36460+/* wkq flags */
36461+#define AuWkq_WAIT 1
9dbd164d 36462+#define AuWkq_NEST (1 << 1)
1facf9fc 36463+#define au_ftest_wkq(flags, name) ((flags) & AuWkq_##name)
7f207e10
AM
36464+#define au_fset_wkq(flags, name) \
36465+ do { (flags) |= AuWkq_##name; } while (0)
36466+#define au_fclr_wkq(flags, name) \
36467+ do { (flags) &= ~AuWkq_##name; } while (0)
1facf9fc 36468+
36469+/* wkq.c */
b752ccd1 36470+int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
53392da6
AM
36471+int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36472+ unsigned int flags);
1facf9fc 36473+void au_nwt_init(struct au_nowait_tasks *nwt);
36474+int __init au_wkq_init(void);
36475+void au_wkq_fin(void);
36476+
36477+/* ---------------------------------------------------------------------- */
36478+
53392da6
AM
36479+static inline int au_wkq_test(void)
36480+{
36481+ return current->flags & PF_WQ_WORKER;
36482+}
36483+
b752ccd1 36484+static inline int au_wkq_wait(au_wkq_func_t func, void *args)
1facf9fc 36485+{
b752ccd1 36486+ return au_wkq_do_wait(AuWkq_WAIT, func, args);
1facf9fc 36487+}
36488+
36489+static inline void au_nwt_done(struct au_nowait_tasks *nwt)
36490+{
f0c0a007 36491+ if (atomic_dec_and_test(&nwt->nw_len))
1facf9fc 36492+ wake_up_all(&nwt->nw_wq);
36493+}
36494+
36495+static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
36496+{
f0c0a007 36497+ wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
1facf9fc 36498+ return 0;
36499+}
36500+
36501+#endif /* __KERNEL__ */
36502+#endif /* __AUFS_WKQ_H__ */
c1595e42
JR
36503diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
36504--- /usr/share/empty/fs/aufs/xattr.c 1970-01-01 01:00:00.000000000 +0100
0b487229 36505+++ linux/fs/aufs/xattr.c 2021-02-22 23:30:37.702680911 +0100
062440b3 36506@@ -0,0 +1,356 @@
cd7a4cd9 36507+// SPDX-License-Identifier: GPL-2.0
c1595e42 36508+/*
0b487229 36509+ * Copyright (C) 2014-2020 Junjiro R. Okajima
c1595e42
JR
36510+ *
36511+ * This program, aufs is free software; you can redistribute it and/or modify
36512+ * it under the terms of the GNU General Public License as published by
36513+ * the Free Software Foundation; either version 2 of the License, or
36514+ * (at your option) any later version.
36515+ *
36516+ * This program is distributed in the hope that it will be useful,
36517+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36518+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36519+ * GNU General Public License for more details.
36520+ *
36521+ * You should have received a copy of the GNU General Public License
36522+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
36523+ */
36524+
36525+/*
36526+ * handling xattr functions
36527+ */
36528+
a2654f78
AM
36529+#include <linux/fs.h>
36530+#include <linux/posix_acl_xattr.h>
c1595e42
JR
36531+#include <linux/xattr.h>
36532+#include "aufs.h"
36533+
36534+static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
36535+{
36536+ if (!ignore_flags)
36537+ goto out;
36538+ switch (err) {
36539+ case -ENOMEM:
36540+ case -EDQUOT:
36541+ goto out;
36542+ }
36543+
36544+ if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
36545+ err = 0;
36546+ goto out;
36547+ }
36548+
36549+#define cmp(brattr, prefix) do { \
36550+ if (!strncmp(name, XATTR_##prefix##_PREFIX, \
36551+ XATTR_##prefix##_PREFIX_LEN)) { \
36552+ if (ignore_flags & AuBrAttr_ICEX_##brattr) \
36553+ err = 0; \
36554+ goto out; \
36555+ } \
36556+ } while (0)
36557+
36558+ cmp(SEC, SECURITY);
36559+ cmp(SYS, SYSTEM);
36560+ cmp(TR, TRUSTED);
36561+ cmp(USR, USER);
36562+#undef cmp
36563+
36564+ if (ignore_flags & AuBrAttr_ICEX_OTH)
36565+ err = 0;
36566+
36567+out:
36568+ return err;
36569+}
36570+
36571+static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
36572+
36573+static int au_do_cpup_xattr(struct dentry *h_dst, struct dentry *h_src,
7e9cd9fe
AM
36574+ char *name, char **buf, unsigned int ignore_flags,
36575+ unsigned int verbose)
c1595e42
JR
36576+{
36577+ int err;
36578+ ssize_t ssz;
36579+ struct inode *h_idst;
36580+
36581+ ssz = vfs_getxattr_alloc(h_src, name, buf, 0, GFP_NOFS);
36582+ err = ssz;
36583+ if (unlikely(err <= 0)) {
c1595e42
JR
36584+ if (err == -ENODATA
36585+ || (err == -EOPNOTSUPP
b912730e 36586+ && ((ignore_flags & au_xattr_out_of_list)
5527c038 36587+ || (au_test_nfs_noacl(d_inode(h_src))
b912730e
AM
36588+ && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
36589+ || !strcmp(name,
36590+ XATTR_NAME_POSIX_ACL_DEFAULT))))
36591+ ))
c1595e42 36592+ err = 0;
b912730e
AM
36593+ if (err && (verbose || au_debug_test()))
36594+ pr_err("%s, err %d\n", name, err);
c1595e42
JR
36595+ goto out;
36596+ }
36597+
36598+ /* unlock it temporary */
5527c038 36599+ h_idst = d_inode(h_dst);
febd17d6 36600+ inode_unlock(h_idst);
c1595e42 36601+ err = vfsub_setxattr(h_dst, name, *buf, ssz, /*flags*/0);
febd17d6 36602+ inode_lock_nested(h_idst, AuLsc_I_CHILD2);
c1595e42 36603+ if (unlikely(err)) {
7e9cd9fe
AM
36604+ if (verbose || au_debug_test())
36605+ pr_err("%s, err %d\n", name, err);
c1595e42
JR
36606+ err = au_xattr_ignore(err, name, ignore_flags);
36607+ }
36608+
36609+out:
36610+ return err;
36611+}
36612+
7e9cd9fe
AM
36613+int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
36614+ unsigned int verbose)
c1595e42
JR
36615+{
36616+ int err, unlocked, acl_access, acl_default;
36617+ ssize_t ssz;
36618+ struct inode *h_isrc, *h_idst;
36619+ char *value, *p, *o, *e;
36620+
36621+ /* try stopping to update the source inode while we are referencing */
7e9cd9fe 36622+ /* there should not be the parent-child relationship between them */
5527c038
JR
36623+ h_isrc = d_inode(h_src);
36624+ h_idst = d_inode(h_dst);
febd17d6 36625+ inode_unlock(h_idst);
be118d29 36626+ inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
febd17d6 36627+ inode_lock_nested(h_idst, AuLsc_I_CHILD2);
c1595e42
JR
36628+ unlocked = 0;
36629+
36630+ /* some filesystems don't list POSIX ACL, for example tmpfs */
36631+ ssz = vfs_listxattr(h_src, NULL, 0);
36632+ err = ssz;
36633+ if (unlikely(err < 0)) {
36634+ AuTraceErr(err);
36635+ if (err == -ENODATA
36636+ || err == -EOPNOTSUPP)
36637+ err = 0; /* ignore */
36638+ goto out;
36639+ }
36640+
36641+ err = 0;
36642+ p = NULL;
36643+ o = NULL;
36644+ if (ssz) {
36645+ err = -ENOMEM;
36646+ p = kmalloc(ssz, GFP_NOFS);
36647+ o = p;
36648+ if (unlikely(!p))
36649+ goto out;
36650+ err = vfs_listxattr(h_src, p, ssz);
36651+ }
3c1bdaff 36652+ inode_unlock_shared(h_isrc);
c1595e42
JR
36653+ unlocked = 1;
36654+ AuDbg("err %d, ssz %zd\n", err, ssz);
36655+ if (unlikely(err < 0))
36656+ goto out_free;
36657+
36658+ err = 0;
36659+ e = p + ssz;
36660+ value = NULL;
36661+ acl_access = 0;
36662+ acl_default = 0;
36663+ while (!err && p < e) {
36664+ acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
36665+ sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
36666+ acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
36667+ sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
36668+ - 1);
7e9cd9fe
AM
36669+ err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
36670+ verbose);
c1595e42
JR
36671+ p += strlen(p) + 1;
36672+ }
36673+ AuTraceErr(err);
36674+ ignore_flags |= au_xattr_out_of_list;
36675+ if (!err && !acl_access) {
36676+ err = au_do_cpup_xattr(h_dst, h_src,
36677+ XATTR_NAME_POSIX_ACL_ACCESS, &value,
7e9cd9fe 36678+ ignore_flags, verbose);
c1595e42
JR
36679+ AuTraceErr(err);
36680+ }
36681+ if (!err && !acl_default) {
36682+ err = au_do_cpup_xattr(h_dst, h_src,
36683+ XATTR_NAME_POSIX_ACL_DEFAULT, &value,
7e9cd9fe 36684+ ignore_flags, verbose);
c1595e42
JR
36685+ AuTraceErr(err);
36686+ }
36687+
e49925d1 36688+ au_kfree_try_rcu(value);
c1595e42
JR
36689+
36690+out_free:
e49925d1 36691+ au_kfree_try_rcu(o);
c1595e42
JR
36692+out:
36693+ if (!unlocked)
3c1bdaff 36694+ inode_unlock_shared(h_isrc);
c1595e42
JR
36695+ AuTraceErr(err);
36696+ return err;
36697+}
36698+
36699+/* ---------------------------------------------------------------------- */
36700+
a2654f78
AM
36701+static int au_smack_reentering(struct super_block *sb)
36702+{
0b487229 36703+#if IS_ENABLED(CONFIG_SECURITY_SMACK) || IS_ENABLED(CONFIG_SECURITY_SELINUX)
a2654f78
AM
36704+ /*
36705+ * as a part of lookup, smack_d_instantiate() is called, and it calls
36706+ * i_op->getxattr(). ouch.
36707+ */
36708+ return si_pid_test(sb);
36709+#else
36710+ return 0;
36711+#endif
36712+}
36713+
c1595e42
JR
36714+enum {
36715+ AU_XATTR_LIST,
36716+ AU_XATTR_GET
36717+};
36718+
36719+struct au_lgxattr {
36720+ int type;
36721+ union {
36722+ struct {
36723+ char *list;
36724+ size_t size;
36725+ } list;
36726+ struct {
36727+ const char *name;
36728+ void *value;
36729+ size_t size;
36730+ } get;
36731+ } u;
36732+};
36733+
0b487229
JR
36734+static ssize_t au_lgxattr(struct dentry *dentry, struct inode *inode,
36735+ struct au_lgxattr *arg)
c1595e42
JR
36736+{
36737+ ssize_t err;
a2654f78 36738+ int reenter;
c1595e42
JR
36739+ struct path h_path;
36740+ struct super_block *sb;
36741+
36742+ sb = dentry->d_sb;
a2654f78
AM
36743+ reenter = au_smack_reentering(sb);
36744+ if (!reenter) {
36745+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
36746+ if (unlikely(err))
36747+ goto out;
36748+ }
0b487229 36749+ err = au_h_path_getattr(dentry, inode, /*force*/1, &h_path, reenter);
c1595e42
JR
36750+ if (unlikely(err))
36751+ goto out_si;
36752+ if (unlikely(!h_path.dentry))
36753+ /* illegally overlapped or something */
36754+ goto out_di; /* pretending success */
36755+
36756+ /* always topmost entry only */
36757+ switch (arg->type) {
36758+ case AU_XATTR_LIST:
36759+ err = vfs_listxattr(h_path.dentry,
36760+ arg->u.list.list, arg->u.list.size);
36761+ break;
36762+ case AU_XATTR_GET:
5afbbe0d 36763+ AuDebugOn(d_is_negative(h_path.dentry));
c1595e42
JR
36764+ err = vfs_getxattr(h_path.dentry,
36765+ arg->u.get.name, arg->u.get.value,
36766+ arg->u.get.size);
36767+ break;
36768+ }
36769+
36770+out_di:
a2654f78
AM
36771+ if (!reenter)
36772+ di_read_unlock(dentry, AuLock_IR);
c1595e42 36773+out_si:
a2654f78
AM
36774+ if (!reenter)
36775+ si_read_unlock(sb);
c1595e42
JR
36776+out:
36777+ AuTraceErr(err);
36778+ return err;
36779+}
36780+
36781+ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
36782+{
36783+ struct au_lgxattr arg = {
36784+ .type = AU_XATTR_LIST,
36785+ .u.list = {
36786+ .list = list,
36787+ .size = size
36788+ },
36789+ };
36790+
0b487229 36791+ return au_lgxattr(dentry, /*inode*/NULL, &arg);
c1595e42
JR
36792+}
36793+
0b487229 36794+static ssize_t au_getxattr(struct dentry *dentry, struct inode *inode,
f2c43d5f 36795+ const char *name, void *value, size_t size)
c1595e42
JR
36796+{
36797+ struct au_lgxattr arg = {
36798+ .type = AU_XATTR_GET,
36799+ .u.get = {
36800+ .name = name,
36801+ .value = value,
36802+ .size = size
36803+ },
36804+ };
36805+
0b487229 36806+ return au_lgxattr(dentry, inode, &arg);
c1595e42
JR
36807+}
36808+
f2c43d5f
AM
36809+static int au_setxattr(struct dentry *dentry, struct inode *inode,
36810+ const char *name, const void *value, size_t size,
36811+ int flags)
c1595e42 36812+{
f2c43d5f 36813+ struct au_sxattr arg = {
c1595e42
JR
36814+ .type = AU_XATTR_SET,
36815+ .u.set = {
36816+ .name = name,
36817+ .value = value,
36818+ .size = size,
36819+ .flags = flags
36820+ },
36821+ };
36822+
f2c43d5f 36823+ return au_sxattr(dentry, inode, &arg);
c1595e42
JR
36824+}
36825+
36826+/* ---------------------------------------------------------------------- */
36827+
f2c43d5f
AM
36828+static int au_xattr_get(const struct xattr_handler *handler,
36829+ struct dentry *dentry, struct inode *inode,
36830+ const char *name, void *buffer, size_t size)
c1595e42 36831+{
f2c43d5f 36832+ return au_getxattr(dentry, inode, name, buffer, size);
c1595e42
JR
36833+}
36834+
f2c43d5f
AM
36835+static int au_xattr_set(const struct xattr_handler *handler,
36836+ struct dentry *dentry, struct inode *inode,
36837+ const char *name, const void *value, size_t size,
36838+ int flags)
c1595e42 36839+{
f2c43d5f 36840+ return au_setxattr(dentry, inode, name, value, size, flags);
c1595e42
JR
36841+}
36842+
36843+static const struct xattr_handler au_xattr_handler = {
f2c43d5f
AM
36844+ .name = "",
36845+ .prefix = "",
c1595e42
JR
36846+ .get = au_xattr_get,
36847+ .set = au_xattr_set
c1595e42
JR
36848+};
36849+
36850+static const struct xattr_handler *au_xattr_handlers[] = {
a2654f78
AM
36851+#ifdef CONFIG_FS_POSIX_ACL
36852+ &posix_acl_access_xattr_handler,
36853+ &posix_acl_default_xattr_handler,
36854+#endif
36855+ &au_xattr_handler, /* must be last */
f2c43d5f 36856+ NULL
c1595e42
JR
36857+};
36858+
36859+void au_xattr_init(struct super_block *sb)
36860+{
f2c43d5f 36861+ sb->s_xattr = au_xattr_handlers;
c1595e42 36862+}
7f207e10
AM
36863diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
36864--- /usr/share/empty/fs/aufs/xino.c 1970-01-01 01:00:00.000000000 +0100
0b487229
JR
36865+++ linux/fs/aufs/xino.c 2021-02-22 23:30:37.706014241 +0100
36866@@ -0,0 +1,1966 @@
cd7a4cd9 36867+// SPDX-License-Identifier: GPL-2.0
1facf9fc 36868+/*
0b487229 36869+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 36870+ *
36871+ * This program, aufs is free software; you can redistribute it and/or modify
36872+ * it under the terms of the GNU General Public License as published by
36873+ * the Free Software Foundation; either version 2 of the License, or
36874+ * (at your option) any later version.
dece6358
AM
36875+ *
36876+ * This program is distributed in the hope that it will be useful,
36877+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36878+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36879+ * GNU General Public License for more details.
36880+ *
36881+ * You should have received a copy of the GNU General Public License
523b37e3 36882+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 36883+ */
36884+
36885+/*
36886+ * external inode number translation table and bitmap
062440b3
AM
36887+ *
36888+ * things to consider
36889+ * - the lifetime
36890+ * + au_xino object
36891+ * + XINO files (xino, xib, xigen)
36892+ * + dynamic debugfs entries (xiN)
36893+ * + static debugfs entries (xib, xigen)
36894+ * + static sysfs entry (xi_path)
36895+ * - several entry points to handle them.
36896+ * + mount(2) without xino option (default)
36897+ * + mount(2) with xino option
36898+ * + mount(2) with noxino option
36899+ * + umount(2)
36900+ * + remount with add/del branches
36901+ * + remount with xino/noxino options
1facf9fc 36902+ */
36903+
36904+#include <linux/seq_file.h>
392086de 36905+#include <linux/statfs.h>
1facf9fc 36906+#include "aufs.h"
36907+
062440b3
AM
36908+static aufs_bindex_t sbr_find_shared(struct super_block *sb, aufs_bindex_t btop,
36909+ aufs_bindex_t bbot,
36910+ struct super_block *h_sb)
1facf9fc 36911+{
062440b3
AM
36912+ /* todo: try binary-search if the branches are many */
36913+ for (; btop <= bbot; btop++)
36914+ if (h_sb == au_sbr_sb(sb, btop))
36915+ return btop;
36916+ return -1;
be52b249
AM
36917+}
36918+
062440b3
AM
36919+/*
36920+ * find another branch who is on the same filesystem of the specified
36921+ * branch{@btgt}. search until @bbot.
36922+ */
36923+static aufs_bindex_t is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
36924+ aufs_bindex_t bbot)
1facf9fc 36925+{
062440b3
AM
36926+ aufs_bindex_t bindex;
36927+ struct super_block *tgt_sb;
1facf9fc 36928+
062440b3
AM
36929+ tgt_sb = au_sbr_sb(sb, btgt);
36930+ bindex = sbr_find_shared(sb, /*btop*/0, btgt - 1, tgt_sb);
36931+ if (bindex < 0)
36932+ bindex = sbr_find_shared(sb, btgt + 1, bbot, tgt_sb);
1facf9fc 36933+
062440b3 36934+ return bindex;
1facf9fc 36935+}
36936+
36937+/* ---------------------------------------------------------------------- */
36938+
36939+/*
062440b3 36940+ * stop unnecessary notify events at creating xino files
1facf9fc 36941+ */
acd2b654
AM
36942+
36943+aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry)
36944+{
36945+ aufs_bindex_t bfound, bindex, bbot;
36946+ struct dentry *parent;
36947+ struct au_branch *br;
36948+
36949+ bfound = -1;
36950+ parent = dentry->d_parent; /* safe d_parent access */
36951+ bbot = au_sbbot(sb);
36952+ for (bindex = 0; bindex <= bbot; bindex++) {
36953+ br = au_sbr(sb, bindex);
36954+ if (au_br_dentry(br) == parent) {
36955+ bfound = bindex;
36956+ break;
36957+ }
36958+ }
36959+
36960+ AuDbg("bfound b%d\n", bfound);
36961+ return bfound;
36962+}
36963+
062440b3
AM
36964+struct au_xino_lock_dir {
36965+ struct au_hinode *hdir;
36966+ struct dentry *parent;
36967+ struct inode *dir;
36968+};
36969+
36970+static struct dentry *au_dget_parent_lock(struct dentry *dentry,
36971+ unsigned int lsc)
1facf9fc 36972+{
062440b3
AM
36973+ struct dentry *parent;
36974+ struct inode *dir;
1facf9fc 36975+
062440b3 36976+ parent = dget_parent(dentry);
5527c038 36977+ dir = d_inode(parent);
062440b3
AM
36978+ inode_lock_nested(dir, lsc);
36979+#if 0 /* it should not happen */
36980+ spin_lock(&dentry->d_lock);
36981+ if (unlikely(dentry->d_parent != parent)) {
36982+ spin_unlock(&dentry->d_lock);
36983+ inode_unlock(dir);
36984+ dput(parent);
36985+ parent = NULL;
1facf9fc 36986+ goto out;
36987+ }
062440b3 36988+ spin_unlock(&dentry->d_lock);
1facf9fc 36989+
4f0767ce 36990+out:
062440b3
AM
36991+#endif
36992+ return parent;
1facf9fc 36993+}
36994+
062440b3 36995+static void au_xino_lock_dir(struct super_block *sb, struct path *xipath,
1facf9fc 36996+ struct au_xino_lock_dir *ldir)
36997+{
acd2b654 36998+ aufs_bindex_t bindex;
1facf9fc 36999+
37000+ ldir->hdir = NULL;
acd2b654 37001+ bindex = au_xi_root(sb, xipath->dentry);
1facf9fc 37002+ if (bindex >= 0) {
062440b3 37003+ /* rw branch root */
5527c038 37004+ ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
5afbbe0d 37005+ au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
1facf9fc 37006+ } else {
062440b3
AM
37007+ /* other */
37008+ ldir->parent = au_dget_parent_lock(xipath->dentry,
37009+ AuLsc_I_PARENT);
febd17d6 37010+ ldir->dir = d_inode(ldir->parent);
1facf9fc 37011+ }
37012+}
37013+
37014+static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
37015+{
37016+ if (ldir->hdir)
5afbbe0d 37017+ au_hn_inode_unlock(ldir->hdir);
1facf9fc 37018+ else {
febd17d6 37019+ inode_unlock(ldir->dir);
1facf9fc 37020+ dput(ldir->parent);
37021+ }
37022+}
37023+
37024+/* ---------------------------------------------------------------------- */
37025+
062440b3
AM
37026+/*
37027+ * create and set a new xino file
37028+ */
0b487229
JR
37029+struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
37030+ int wbrtop)
062440b3
AM
37031+{
37032+ struct file *file;
37033+ struct dentry *h_parent, *d;
37034+ struct inode *h_dir, *inode;
37035+ int err;
0b487229 37036+ static DEFINE_MUTEX(mtx);
062440b3
AM
37037+
37038+ /*
37039+ * at mount-time, and the xino file is the default path,
37040+ * hnotify is disabled so we have no notify events to ignore.
37041+ * when a user specified the xino, we cannot get au_hdir to be ignored.
37042+ */
0b487229
JR
37043+ if (!wbrtop)
37044+ mutex_lock(&mtx);
062440b3
AM
37045+ file = vfsub_filp_open(fpath, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37046+ /* | __FMODE_NONOTIFY */,
cd7a4cd9 37047+ 0666);
062440b3 37048+ if (IS_ERR(file)) {
0b487229
JR
37049+ if (!wbrtop)
37050+ mutex_unlock(&mtx);
062440b3
AM
37051+ if (!silent)
37052+ pr_err("open %s(%ld)\n", fpath, PTR_ERR(file));
37053+ return file;
37054+ }
37055+
37056+ /* keep file count */
37057+ err = 0;
37058+ d = file->f_path.dentry;
37059+ h_parent = au_dget_parent_lock(d, AuLsc_I_PARENT);
0b487229
JR
37060+ if (!wbrtop)
37061+ mutex_unlock(&mtx);
062440b3
AM
37062+ /* mnt_want_write() is unnecessary here */
37063+ h_dir = d_inode(h_parent);
37064+ inode = file_inode(file);
37065+ /* no delegation since it is just created */
37066+ if (inode->i_nlink)
37067+ err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
37068+ /*force*/0);
37069+ inode_unlock(h_dir);
37070+ dput(h_parent);
37071+ if (unlikely(err)) {
37072+ if (!silent)
37073+ pr_err("unlink %s(%d)\n", fpath, err);
37074+ goto out;
37075+ }
37076+
37077+ err = -EINVAL;
37078+ if (unlikely(sb == d->d_sb)) {
37079+ if (!silent)
37080+ pr_err("%s must be outside\n", fpath);
37081+ goto out;
37082+ }
37083+ if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
37084+ if (!silent)
37085+ pr_err("xino doesn't support %s(%s)\n",
37086+ fpath, au_sbtype(d->d_sb));
37087+ goto out;
37088+ }
37089+ return file; /* success */
37090+
37091+out:
37092+ fput(file);
37093+ file = ERR_PTR(err);
37094+ return file;
37095+}
37096+
37097+/*
37098+ * create a new xinofile at the same place/path as @base.
37099+ */
37100+struct file *au_xino_create2(struct super_block *sb, struct path *base,
37101+ struct file *copy_src)
37102+{
37103+ struct file *file;
37104+ struct dentry *dentry, *parent;
37105+ struct inode *dir, *delegated;
37106+ struct qstr *name;
37107+ struct path path;
37108+ int err, do_unlock;
37109+ struct au_xino_lock_dir ldir;
37110+
37111+ do_unlock = 1;
37112+ au_xino_lock_dir(sb, base, &ldir);
37113+ dentry = base->dentry;
37114+ parent = dentry->d_parent; /* dir inode is locked */
37115+ dir = d_inode(parent);
37116+ IMustLock(dir);
37117+
37118+ name = &dentry->d_name;
37119+ path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
37120+ if (IS_ERR(path.dentry)) {
37121+ file = (void *)path.dentry;
37122+ pr_err("%pd lookup err %ld\n", dentry, PTR_ERR(path.dentry));
37123+ goto out;
37124+ }
37125+
37126+ /* no need to mnt_want_write() since we call dentry_open() later */
cd7a4cd9 37127+ err = vfs_create(dir, path.dentry, 0666, NULL);
062440b3
AM
37128+ if (unlikely(err)) {
37129+ file = ERR_PTR(err);
37130+ pr_err("%pd create err %d\n", dentry, err);
37131+ goto out_dput;
37132+ }
37133+
37134+ path.mnt = base->mnt;
37135+ file = vfsub_dentry_open(&path,
37136+ O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37137+ /* | __FMODE_NONOTIFY */);
37138+ if (IS_ERR(file)) {
37139+ pr_err("%pd open err %ld\n", dentry, PTR_ERR(file));
37140+ goto out_dput;
37141+ }
37142+
37143+ delegated = NULL;
37144+ err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
37145+ au_xino_unlock_dir(&ldir);
37146+ do_unlock = 0;
37147+ if (unlikely(err == -EWOULDBLOCK)) {
37148+ pr_warn("cannot retry for NFSv4 delegation"
37149+ " for an internal unlink\n");
37150+ iput(delegated);
37151+ }
37152+ if (unlikely(err)) {
37153+ pr_err("%pd unlink err %d\n", dentry, err);
37154+ goto out_fput;
37155+ }
37156+
37157+ if (copy_src) {
37158+ /* no one can touch copy_src xino */
37159+ err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
37160+ if (unlikely(err)) {
37161+ pr_err("%pd copy err %d\n", dentry, err);
37162+ goto out_fput;
37163+ }
37164+ }
37165+ goto out_dput; /* success */
1facf9fc 37166+
062440b3
AM
37167+out_fput:
37168+ fput(file);
37169+ file = ERR_PTR(err);
37170+out_dput:
37171+ dput(path.dentry);
37172+out:
37173+ if (do_unlock)
37174+ au_xino_unlock_dir(&ldir);
37175+ return file;
37176+}
37177+
acd2b654
AM
37178+struct file *au_xino_file1(struct au_xino *xi)
37179+{
37180+ struct file *file;
37181+ unsigned int u, nfile;
37182+
37183+ file = NULL;
37184+ nfile = xi->xi_nfile;
37185+ for (u = 0; u < nfile; u++) {
37186+ file = xi->xi_file[u];
37187+ if (file)
37188+ break;
37189+ }
37190+
37191+ return file;
37192+}
37193+
37194+static int au_xino_file_set(struct au_xino *xi, int idx, struct file *file)
37195+{
37196+ int err;
37197+ struct file *f;
37198+ void *p;
37199+
37200+ if (file)
37201+ get_file(file);
37202+
37203+ err = 0;
37204+ f = NULL;
37205+ if (idx < xi->xi_nfile) {
37206+ f = xi->xi_file[idx];
37207+ if (f)
37208+ fput(f);
37209+ } else {
37210+ p = au_kzrealloc(xi->xi_file,
37211+ sizeof(*xi->xi_file) * xi->xi_nfile,
37212+ sizeof(*xi->xi_file) * (idx + 1),
37213+ GFP_NOFS, /*may_shrink*/0);
37214+ if (p) {
37215+ MtxMustLock(&xi->xi_mtx);
37216+ xi->xi_file = p;
37217+ xi->xi_nfile = idx + 1;
37218+ } else {
37219+ err = -ENOMEM;
37220+ if (file)
37221+ fput(file);
37222+ goto out;
37223+ }
37224+ }
37225+ xi->xi_file[idx] = file;
37226+
37227+out:
37228+ return err;
37229+}
37230+
37231+/*
37232+ * if @xinew->xi is not set, then create new xigen file.
37233+ */
37234+struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew)
37235+{
37236+ struct file *file;
37237+ int err;
37238+
37239+ SiMustAnyLock(sb);
37240+
37241+ file = au_xino_create2(sb, xinew->base, xinew->copy_src);
37242+ if (IS_ERR(file)) {
37243+ err = PTR_ERR(file);
37244+ pr_err("%s[%d], err %d\n",
37245+ xinew->xi ? "xino" : "xigen",
37246+ xinew->idx, err);
37247+ goto out;
37248+ }
37249+
37250+ if (xinew->xi)
37251+ err = au_xino_file_set(xinew->xi, xinew->idx, file);
37252+ else {
37253+ BUG();
37254+ /* todo: make xigen file an array */
37255+ /* err = au_xigen_file_set(sb, xinew->idx, file); */
37256+ }
37257+ fput(file);
37258+ if (unlikely(err))
37259+ file = ERR_PTR(err);
37260+
37261+out:
37262+ return file;
37263+}
37264+
062440b3
AM
37265+/* ---------------------------------------------------------------------- */
37266+
37267+/*
37268+ * truncate xino files
37269+ */
acd2b654
AM
37270+static int au_xino_do_trunc(struct super_block *sb, aufs_bindex_t bindex,
37271+ int idx, struct kstatfs *st)
1facf9fc 37272+{
37273+ int err;
392086de 37274+ blkcnt_t blocks;
acd2b654
AM
37275+ struct file *file, *new_xino;
37276+ struct au_xi_new xinew = {
37277+ .idx = idx
37278+ };
37279+
37280+ err = 0;
37281+ xinew.xi = au_sbr(sb, bindex)->br_xino;
37282+ file = au_xino_file(xinew.xi, idx);
37283+ if (!file)
37284+ goto out;
37285+
37286+ xinew.base = &file->f_path;
37287+ err = vfs_statfs(xinew.base, st);
37288+ if (unlikely(err)) {
37289+ AuErr1("statfs err %d, ignored\n", err);
37290+ err = 0;
37291+ goto out;
37292+ }
37293+
37294+ blocks = file_inode(file)->i_blocks;
37295+ pr_info("begin truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37296+ bindex, idx, (u64)blocks, st->f_bfree, st->f_blocks);
37297+
37298+ xinew.copy_src = file;
37299+ new_xino = au_xi_new(sb, &xinew);
37300+ if (IS_ERR(new_xino)) {
37301+ err = PTR_ERR(new_xino);
37302+ pr_err("xino(b%d-%d), err %d, ignored\n", bindex, idx, err);
37303+ goto out;
37304+ }
37305+
37306+ err = vfs_statfs(&new_xino->f_path, st);
37307+ if (!err)
37308+ pr_info("end truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37309+ bindex, idx, (u64)file_inode(new_xino)->i_blocks,
37310+ st->f_bfree, st->f_blocks);
37311+ else {
37312+ AuErr1("statfs err %d, ignored\n", err);
37313+ err = 0;
37314+ }
37315+
37316+out:
37317+ return err;
37318+}
37319+
37320+int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin)
37321+{
37322+ int err, i;
37323+ unsigned long jiffy;
062440b3 37324+ aufs_bindex_t bbot;
392086de 37325+ struct kstatfs *st;
1facf9fc 37326+ struct au_branch *br;
acd2b654 37327+ struct au_xino *xi;
1facf9fc 37328+
392086de 37329+ err = -ENOMEM;
be52b249 37330+ st = kmalloc(sizeof(*st), GFP_NOFS);
392086de
AM
37331+ if (unlikely(!st))
37332+ goto out;
37333+
1facf9fc 37334+ err = -EINVAL;
5afbbe0d
AM
37335+ bbot = au_sbbot(sb);
37336+ if (unlikely(bindex < 0 || bbot < bindex))
392086de 37337+ goto out_st;
392086de 37338+
1facf9fc 37339+ err = 0;
acd2b654
AM
37340+ jiffy = jiffies;
37341+ br = au_sbr(sb, bindex);
37342+ xi = br->br_xino;
37343+ for (i = idx_begin; !err && i < xi->xi_nfile; i++)
37344+ err = au_xino_do_trunc(sb, bindex, i, st);
37345+ if (!err)
37346+ au_sbi(sb)->si_xino_jiffy = jiffy;
392086de
AM
37347+
37348+out_st:
e49925d1 37349+ au_kfree_rcu(st);
4f0767ce 37350+out:
1facf9fc 37351+ return err;
37352+}
37353+
37354+struct xino_do_trunc_args {
37355+ struct super_block *sb;
37356+ struct au_branch *br;
acd2b654 37357+ int idx;
1facf9fc 37358+};
37359+
37360+static void xino_do_trunc(void *_args)
37361+{
37362+ struct xino_do_trunc_args *args = _args;
37363+ struct super_block *sb;
37364+ struct au_branch *br;
37365+ struct inode *dir;
acd2b654 37366+ int err, idx;
1facf9fc 37367+ aufs_bindex_t bindex;
37368+
37369+ err = 0;
37370+ sb = args->sb;
5527c038 37371+ dir = d_inode(sb->s_root);
1facf9fc 37372+ br = args->br;
acd2b654 37373+ idx = args->idx;
1facf9fc 37374+
37375+ si_noflush_write_lock(sb);
37376+ ii_read_lock_parent(dir);
37377+ bindex = au_br_index(sb, br->br_id);
acd2b654 37378+ err = au_xino_trunc(sb, bindex, idx);
1facf9fc 37379+ ii_read_unlock(dir);
37380+ if (unlikely(err))
392086de 37381+ pr_warn("err b%d, (%d)\n", bindex, err);
062440b3 37382+ atomic_dec(&br->br_xino->xi_truncating);
acd2b654 37383+ au_lcnt_dec(&br->br_count);
1facf9fc 37384+ si_write_unlock(sb);
027c5e7a 37385+ au_nwt_done(&au_sbi(sb)->si_nowait);
e49925d1 37386+ au_kfree_rcu(args);
1facf9fc 37387+}
37388+
acd2b654
AM
37389+/*
37390+ * returns the index in the xi_file array whose corresponding file is necessary
37391+ * to truncate, or -1 which means no need to truncate.
37392+ */
392086de
AM
37393+static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
37394+{
37395+ int err;
acd2b654 37396+ unsigned int u;
392086de
AM
37397+ struct kstatfs st;
37398+ struct au_sbinfo *sbinfo;
acd2b654 37399+ struct au_xino *xi;
062440b3 37400+ struct file *file;
392086de
AM
37401+
37402+ /* todo: si_xino_expire and the ratio should be customizable */
37403+ sbinfo = au_sbi(sb);
37404+ if (time_before(jiffies,
37405+ sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
acd2b654 37406+ return -1;
392086de
AM
37407+
37408+ /* truncation border */
acd2b654
AM
37409+ xi = br->br_xino;
37410+ for (u = 0; u < xi->xi_nfile; u++) {
37411+ file = au_xino_file(xi, u);
37412+ if (!file)
37413+ continue;
37414+
37415+ err = vfs_statfs(&file->f_path, &st);
37416+ if (unlikely(err)) {
37417+ AuErr1("statfs err %d, ignored\n", err);
37418+ return -1;
37419+ }
37420+ if (div64_u64(st.f_bfree * 100, st.f_blocks)
37421+ >= AUFS_XINO_DEF_TRUNC)
37422+ return u;
392086de 37423+ }
392086de 37424+
acd2b654 37425+ return -1;
392086de
AM
37426+}
37427+
1facf9fc 37428+static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
37429+{
acd2b654 37430+ int idx;
1facf9fc 37431+ struct xino_do_trunc_args *args;
37432+ int wkq_err;
37433+
acd2b654
AM
37434+ idx = xino_trunc_test(sb, br);
37435+ if (idx < 0)
1facf9fc 37436+ return;
37437+
062440b3 37438+ if (atomic_inc_return(&br->br_xino->xi_truncating) > 1)
1facf9fc 37439+ goto out;
37440+
37441+ /* lock and kfree() will be called in trunc_xino() */
37442+ args = kmalloc(sizeof(*args), GFP_NOFS);
37443+ if (unlikely(!args)) {
37444+ AuErr1("no memory\n");
f0c0a007 37445+ goto out;
1facf9fc 37446+ }
37447+
acd2b654 37448+ au_lcnt_inc(&br->br_count);
1facf9fc 37449+ args->sb = sb;
37450+ args->br = br;
acd2b654 37451+ args->idx = idx;
53392da6 37452+ wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
1facf9fc 37453+ if (!wkq_err)
37454+ return; /* success */
37455+
4a4d8108 37456+ pr_err("wkq %d\n", wkq_err);
acd2b654 37457+ au_lcnt_dec(&br->br_count);
e49925d1 37458+ au_kfree_rcu(args);
1facf9fc 37459+
4f0767ce 37460+out:
062440b3 37461+ atomic_dec(&br->br_xino->xi_truncating);
1facf9fc 37462+}
37463+
37464+/* ---------------------------------------------------------------------- */
37465+
acd2b654
AM
37466+struct au_xi_calc {
37467+ int idx;
37468+ loff_t pos;
37469+};
37470+
37471+static void au_xi_calc(struct super_block *sb, ino_t h_ino,
37472+ struct au_xi_calc *calc)
37473+{
37474+ loff_t maxent;
37475+
37476+ maxent = au_xi_maxent(sb);
37477+ calc->idx = div64_u64_rem(h_ino, maxent, &calc->pos);
37478+ calc->pos *= sizeof(ino_t);
37479+}
37480+
37481+static int au_xino_do_new_async(struct super_block *sb, struct au_branch *br,
37482+ struct au_xi_calc *calc)
37483+{
37484+ int err;
37485+ struct file *file;
37486+ struct au_xino *xi = br->br_xino;
37487+ struct au_xi_new xinew = {
37488+ .xi = xi
37489+ };
37490+
37491+ SiMustAnyLock(sb);
37492+
37493+ err = 0;
37494+ if (!xi)
37495+ goto out;
37496+
37497+ mutex_lock(&xi->xi_mtx);
37498+ file = au_xino_file(xi, calc->idx);
37499+ if (file)
37500+ goto out_mtx;
37501+
37502+ file = au_xino_file(xi, /*idx*/-1);
37503+ AuDebugOn(!file);
37504+ xinew.idx = calc->idx;
37505+ xinew.base = &file->f_path;
37506+ /* xinew.copy_src = NULL; */
37507+ file = au_xi_new(sb, &xinew);
37508+ if (IS_ERR(file))
37509+ err = PTR_ERR(file);
37510+
37511+out_mtx:
37512+ mutex_unlock(&xi->xi_mtx);
37513+out:
37514+ return err;
37515+}
37516+
37517+struct au_xino_do_new_async_args {
37518+ struct super_block *sb;
37519+ struct au_branch *br;
37520+ struct au_xi_calc calc;
37521+ ino_t ino;
37522+};
37523+
e49925d1
JR
37524+struct au_xi_writing {
37525+ struct hlist_bl_node node;
37526+ ino_t h_ino, ino;
37527+};
37528+
acd2b654
AM
37529+static int au_xino_do_write(vfs_writef_t write, struct file *file,
37530+ struct au_xi_calc *calc, ino_t ino);
37531+
37532+static void au_xino_call_do_new_async(void *args)
37533+{
37534+ struct au_xino_do_new_async_args *a = args;
37535+ struct au_branch *br;
37536+ struct super_block *sb;
37537+ struct au_sbinfo *sbi;
37538+ struct inode *root;
37539+ struct file *file;
e49925d1
JR
37540+ struct au_xi_writing *del, *p;
37541+ struct hlist_bl_head *hbl;
37542+ struct hlist_bl_node *pos;
acd2b654
AM
37543+ int err;
37544+
37545+ br = a->br;
37546+ sb = a->sb;
37547+ sbi = au_sbi(sb);
37548+ si_noflush_read_lock(sb);
37549+ root = d_inode(sb->s_root);
37550+ ii_read_lock_child(root);
37551+ err = au_xino_do_new_async(sb, br, &a->calc);
e49925d1 37552+ if (unlikely(err)) {
acd2b654 37553+ AuIOErr("err %d\n", err);
e49925d1
JR
37554+ goto out;
37555+ }
37556+
37557+ file = au_xino_file(br->br_xino, a->calc.idx);
37558+ AuDebugOn(!file);
37559+ err = au_xino_do_write(sbi->si_xwrite, file, &a->calc, a->ino);
37560+ if (unlikely(err)) {
37561+ AuIOErr("err %d\n", err);
37562+ goto out;
37563+ }
37564+
37565+ del = NULL;
37566+ hbl = &br->br_xino->xi_writing;
37567+ hlist_bl_lock(hbl);
37568+ au_hbl_for_each(pos, hbl) {
37569+ p = container_of(pos, struct au_xi_writing, node);
37570+ if (p->ino == a->ino) {
37571+ del = p;
37572+ hlist_bl_del(&p->node);
37573+ break;
37574+ }
37575+ }
37576+ hlist_bl_unlock(hbl);
37577+ au_kfree_rcu(del);
37578+
37579+out:
acd2b654
AM
37580+ au_lcnt_dec(&br->br_count);
37581+ ii_read_unlock(root);
37582+ si_read_unlock(sb);
37583+ au_nwt_done(&sbi->si_nowait);
e49925d1 37584+ au_kfree_rcu(a);
acd2b654
AM
37585+}
37586+
37587+/*
37588+ * create a new xino file asynchronously
37589+ */
37590+static int au_xino_new_async(struct super_block *sb, struct au_branch *br,
37591+ struct au_xi_calc *calc, ino_t ino)
37592+{
37593+ int err;
37594+ struct au_xino_do_new_async_args *arg;
37595+
37596+ err = -ENOMEM;
37597+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
37598+ if (unlikely(!arg))
37599+ goto out;
37600+
37601+ arg->sb = sb;
37602+ arg->br = br;
37603+ arg->calc = *calc;
37604+ arg->ino = ino;
37605+ au_lcnt_inc(&br->br_count);
37606+ err = au_wkq_nowait(au_xino_call_do_new_async, arg, sb, AuWkq_NEST);
37607+ if (unlikely(err)) {
37608+ pr_err("wkq %d\n", err);
37609+ au_lcnt_dec(&br->br_count);
e49925d1 37610+ au_kfree_rcu(arg);
acd2b654
AM
37611+ }
37612+
37613+out:
37614+ return err;
37615+}
37616+
062440b3
AM
37617+/*
37618+ * read @ino from xinofile for the specified branch{@sb, @bindex}
37619+ * at the position of @h_ino.
37620+ */
37621+int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37622+ ino_t *ino)
37623+{
37624+ int err;
37625+ ssize_t sz;
acd2b654 37626+ struct au_xi_calc calc;
062440b3 37627+ struct au_sbinfo *sbinfo;
acd2b654 37628+ struct file *file;
e49925d1
JR
37629+ struct au_xino *xi;
37630+ struct hlist_bl_head *hbl;
37631+ struct hlist_bl_node *pos;
37632+ struct au_xi_writing *p;
062440b3
AM
37633+
37634+ *ino = 0;
37635+ if (!au_opt_test(au_mntflags(sb), XINO))
37636+ return 0; /* no xino */
37637+
37638+ err = 0;
acd2b654 37639+ au_xi_calc(sb, h_ino, &calc);
e49925d1
JR
37640+ xi = au_sbr(sb, bindex)->br_xino;
37641+ file = au_xino_file(xi, calc.idx);
37642+ if (!file) {
37643+ hbl = &xi->xi_writing;
37644+ hlist_bl_lock(hbl);
37645+ au_hbl_for_each(pos, hbl) {
37646+ p = container_of(pos, struct au_xi_writing, node);
37647+ if (p->h_ino == h_ino) {
37648+ AuDbg("hi%llu, i%llu, found\n",
37649+ (u64)p->h_ino, (u64)p->ino);
37650+ *ino = p->ino;
37651+ break;
37652+ }
37653+ }
37654+ hlist_bl_unlock(hbl);
37655+ return 0;
37656+ } else if (vfsub_f_size_read(file) < calc.pos + sizeof(*ino))
37657+ return 0; /* no xino */
062440b3 37658+
acd2b654
AM
37659+ sbinfo = au_sbi(sb);
37660+ sz = xino_fread(sbinfo->si_xread, file, ino, sizeof(*ino), &calc.pos);
062440b3
AM
37661+ if (sz == sizeof(*ino))
37662+ return 0; /* success */
37663+
37664+ err = sz;
37665+ if (unlikely(sz >= 0)) {
37666+ err = -EIO;
37667+ AuIOErr("xino read error (%zd)\n", sz);
37668+ }
062440b3
AM
37669+ return err;
37670+}
37671+
5527c038 37672+static int au_xino_do_write(vfs_writef_t write, struct file *file,
acd2b654 37673+ struct au_xi_calc *calc, ino_t ino)
1facf9fc 37674+{
1facf9fc 37675+ ssize_t sz;
37676+
acd2b654 37677+ sz = xino_fwrite(write, file, &ino, sizeof(ino), &calc->pos);
1facf9fc 37678+ if (sz == sizeof(ino))
37679+ return 0; /* success */
37680+
37681+ AuIOErr("write failed (%zd)\n", sz);
37682+ return -EIO;
37683+}
37684+
37685+/*
37686+ * write @ino to the xinofile for the specified branch{@sb, @bindex}
37687+ * at the position of @h_ino.
37688+ * even if @ino is zero, it is written to the xinofile and means no entry.
37689+ * if the size of the xino file on a specific filesystem exceeds the watermark,
37690+ * try truncating it.
37691+ */
37692+int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37693+ ino_t ino)
37694+{
37695+ int err;
37696+ unsigned int mnt_flags;
acd2b654
AM
37697+ struct au_xi_calc calc;
37698+ struct file *file;
1facf9fc 37699+ struct au_branch *br;
acd2b654 37700+ struct au_xino *xi;
e49925d1 37701+ struct au_xi_writing *p;
1facf9fc 37702+
dece6358 37703+ SiMustAnyLock(sb);
1facf9fc 37704+
37705+ mnt_flags = au_mntflags(sb);
37706+ if (!au_opt_test(mnt_flags, XINO))
37707+ return 0;
37708+
acd2b654 37709+ au_xi_calc(sb, h_ino, &calc);
1facf9fc 37710+ br = au_sbr(sb, bindex);
acd2b654
AM
37711+ xi = br->br_xino;
37712+ file = au_xino_file(xi, calc.idx);
37713+ if (!file) {
e49925d1
JR
37714+ /* store the inum pair into the list */
37715+ p = kmalloc(sizeof(*p), GFP_NOFS | __GFP_NOFAIL);
37716+ p->h_ino = h_ino;
37717+ p->ino = ino;
37718+ au_hbl_add(&p->node, &xi->xi_writing);
37719+
acd2b654
AM
37720+ /* create and write a new xino file asynchronously */
37721+ err = au_xino_new_async(sb, br, &calc, ino);
37722+ if (!err)
37723+ return 0; /* success */
37724+ goto out;
37725+ }
37726+
37727+ err = au_xino_do_write(au_sbi(sb)->si_xwrite, file, &calc, ino);
1facf9fc 37728+ if (!err) {
acd2b654 37729+ br = au_sbr(sb, bindex);
1facf9fc 37730+ if (au_opt_test(mnt_flags, TRUNC_XINO)
86dc4139 37731+ && au_test_fs_trunc_xino(au_br_sb(br)))
1facf9fc 37732+ xino_try_trunc(sb, br);
37733+ return 0; /* success */
37734+ }
37735+
acd2b654 37736+out:
1facf9fc 37737+ AuIOErr("write failed (%d)\n", err);
37738+ return -EIO;
37739+}
37740+
062440b3
AM
37741+static ssize_t xino_fread_wkq(vfs_readf_t func, struct file *file, void *buf,
37742+ size_t size, loff_t *pos);
1facf9fc 37743+
062440b3
AM
37744+/* todo: unnecessary to support mmap_sem since kernel-space? */
37745+ssize_t xino_fread(vfs_readf_t func, struct file *file, void *kbuf, size_t size,
37746+ loff_t *pos)
37747+{
37748+ ssize_t err;
37749+ mm_segment_t oldfs;
37750+ union {
37751+ void *k;
37752+ char __user *u;
37753+ } buf;
37754+ int i;
37755+ const int prevent_endless = 10;
1facf9fc 37756+
062440b3
AM
37757+ i = 0;
37758+ buf.k = kbuf;
37759+ oldfs = get_fs();
37760+ set_fs(KERNEL_DS);
37761+ do {
37762+ err = func(file, buf.u, size, pos);
37763+ if (err == -EINTR
37764+ && !au_wkq_test()
37765+ && fatal_signal_pending(current)) {
37766+ set_fs(oldfs);
37767+ err = xino_fread_wkq(func, file, kbuf, size, pos);
37768+ BUG_ON(err == -EINTR);
37769+ oldfs = get_fs();
37770+ set_fs(KERNEL_DS);
37771+ }
37772+ } while (i++ < prevent_endless
37773+ && (err == -EAGAIN || err == -EINTR));
37774+ set_fs(oldfs);
37775+
37776+#if 0 /* reserved for future use */
37777+ if (err > 0)
37778+ fsnotify_access(file->f_path.dentry);
37779+#endif
37780+
37781+ return err;
37782+}
37783+
37784+struct xino_fread_args {
37785+ ssize_t *errp;
37786+ vfs_readf_t func;
37787+ struct file *file;
37788+ void *buf;
37789+ size_t size;
37790+ loff_t *pos;
37791+};
37792+
37793+static void call_xino_fread(void *args)
37794+{
37795+ struct xino_fread_args *a = args;
37796+ *a->errp = xino_fread(a->func, a->file, a->buf, a->size, a->pos);
37797+}
37798+
37799+static ssize_t xino_fread_wkq(vfs_readf_t func, struct file *file, void *buf,
37800+ size_t size, loff_t *pos)
37801+{
37802+ ssize_t err;
37803+ int wkq_err;
37804+ struct xino_fread_args args = {
37805+ .errp = &err,
37806+ .func = func,
37807+ .file = file,
37808+ .buf = buf,
37809+ .size = size,
37810+ .pos = pos
37811+ };
37812+
37813+ wkq_err = au_wkq_wait(call_xino_fread, &args);
37814+ if (unlikely(wkq_err))
37815+ err = wkq_err;
37816+
37817+ return err;
37818+}
37819+
37820+static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
37821+ size_t size, loff_t *pos);
37822+
37823+static ssize_t do_xino_fwrite(vfs_writef_t func, struct file *file, void *kbuf,
37824+ size_t size, loff_t *pos)
37825+{
37826+ ssize_t err;
37827+ mm_segment_t oldfs;
37828+ union {
37829+ void *k;
37830+ const char __user *u;
37831+ } buf;
37832+ int i;
37833+ const int prevent_endless = 10;
37834+
37835+ i = 0;
37836+ buf.k = kbuf;
37837+ oldfs = get_fs();
37838+ set_fs(KERNEL_DS);
37839+ do {
37840+ err = func(file, buf.u, size, pos);
37841+ if (err == -EINTR
37842+ && !au_wkq_test()
37843+ && fatal_signal_pending(current)) {
37844+ set_fs(oldfs);
37845+ err = xino_fwrite_wkq(func, file, kbuf, size, pos);
37846+ BUG_ON(err == -EINTR);
37847+ oldfs = get_fs();
37848+ set_fs(KERNEL_DS);
37849+ }
37850+ } while (i++ < prevent_endless
37851+ && (err == -EAGAIN || err == -EINTR));
37852+ set_fs(oldfs);
37853+
37854+#if 0 /* reserved for future use */
37855+ if (err > 0)
37856+ fsnotify_modify(file->f_path.dentry);
37857+#endif
37858+
37859+ return err;
37860+}
37861+
37862+struct do_xino_fwrite_args {
37863+ ssize_t *errp;
37864+ vfs_writef_t func;
37865+ struct file *file;
37866+ void *buf;
37867+ size_t size;
37868+ loff_t *pos;
37869+};
37870+
37871+static void call_do_xino_fwrite(void *args)
37872+{
37873+ struct do_xino_fwrite_args *a = args;
37874+ *a->errp = do_xino_fwrite(a->func, a->file, a->buf, a->size, a->pos);
37875+}
37876+
37877+static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
37878+ size_t size, loff_t *pos)
37879+{
37880+ ssize_t err;
37881+ int wkq_err;
37882+ struct do_xino_fwrite_args args = {
37883+ .errp = &err,
37884+ .func = func,
37885+ .file = file,
37886+ .buf = buf,
37887+ .size = size,
37888+ .pos = pos
37889+ };
37890+
37891+ /*
37892+ * it breaks RLIMIT_FSIZE and normal user's limit,
37893+ * users should care about quota and real 'filesystem full.'
37894+ */
37895+ wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
37896+ if (unlikely(wkq_err))
37897+ err = wkq_err;
37898+
37899+ return err;
37900+}
37901+
37902+ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
37903+ size_t size, loff_t *pos)
37904+{
37905+ ssize_t err;
37906+
37907+ if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
37908+ lockdep_off();
37909+ err = do_xino_fwrite(func, file, buf, size, pos);
37910+ lockdep_on();
37911+ } else {
37912+ lockdep_off();
37913+ err = xino_fwrite_wkq(func, file, buf, size, pos);
37914+ lockdep_on();
37915+ }
37916+
37917+ return err;
37918+}
37919+
37920+/* ---------------------------------------------------------------------- */
37921+
37922+/*
37923+ * inode number bitmap
37924+ */
1facf9fc 37925+static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
37926+static ino_t xib_calc_ino(unsigned long pindex, int bit)
37927+{
37928+ ino_t ino;
37929+
37930+ AuDebugOn(bit < 0 || page_bits <= bit);
37931+ ino = AUFS_FIRST_INO + pindex * page_bits + bit;
37932+ return ino;
37933+}
37934+
37935+static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
37936+{
37937+ AuDebugOn(ino < AUFS_FIRST_INO);
37938+ ino -= AUFS_FIRST_INO;
37939+ *pindex = ino / page_bits;
37940+ *bit = ino % page_bits;
37941+}
37942+
37943+static int xib_pindex(struct super_block *sb, unsigned long pindex)
37944+{
37945+ int err;
37946+ loff_t pos;
37947+ ssize_t sz;
37948+ struct au_sbinfo *sbinfo;
37949+ struct file *xib;
37950+ unsigned long *p;
37951+
37952+ sbinfo = au_sbi(sb);
37953+ MtxMustLock(&sbinfo->si_xib_mtx);
37954+ AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
37955+ || !au_opt_test(sbinfo->si_mntflags, XINO));
37956+
37957+ if (pindex == sbinfo->si_xib_last_pindex)
37958+ return 0;
37959+
37960+ xib = sbinfo->si_xib;
37961+ p = sbinfo->si_xib_buf;
37962+ pos = sbinfo->si_xib_last_pindex;
37963+ pos *= PAGE_SIZE;
37964+ sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
37965+ if (unlikely(sz != PAGE_SIZE))
37966+ goto out;
37967+
37968+ pos = pindex;
37969+ pos *= PAGE_SIZE;
c06a8ce3 37970+ if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
1facf9fc 37971+ sz = xino_fread(sbinfo->si_xread, xib, p, PAGE_SIZE, &pos);
37972+ else {
37973+ memset(p, 0, PAGE_SIZE);
37974+ sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
37975+ }
37976+ if (sz == PAGE_SIZE) {
37977+ sbinfo->si_xib_last_pindex = pindex;
37978+ return 0; /* success */
37979+ }
37980+
4f0767ce 37981+out:
b752ccd1
AM
37982+ AuIOErr1("write failed (%zd)\n", sz);
37983+ err = sz;
37984+ if (sz >= 0)
37985+ err = -EIO;
37986+ return err;
37987+}
37988+
b752ccd1
AM
37989+static void au_xib_clear_bit(struct inode *inode)
37990+{
37991+ int err, bit;
37992+ unsigned long pindex;
37993+ struct super_block *sb;
37994+ struct au_sbinfo *sbinfo;
37995+
37996+ AuDebugOn(inode->i_nlink);
37997+
37998+ sb = inode->i_sb;
37999+ xib_calc_bit(inode->i_ino, &pindex, &bit);
38000+ AuDebugOn(page_bits <= bit);
38001+ sbinfo = au_sbi(sb);
38002+ mutex_lock(&sbinfo->si_xib_mtx);
38003+ err = xib_pindex(sb, pindex);
38004+ if (!err) {
38005+ clear_bit(bit, sbinfo->si_xib_buf);
38006+ sbinfo->si_xib_next_bit = bit;
38007+ }
38008+ mutex_unlock(&sbinfo->si_xib_mtx);
38009+}
38010+
1facf9fc 38011+/* ---------------------------------------------------------------------- */
38012+
1facf9fc 38013+/*
062440b3 38014+ * truncate a xino bitmap file
1facf9fc 38015+ */
1facf9fc 38016+
38017+/* todo: slow */
38018+static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
38019+{
38020+ int err, bit;
38021+ ssize_t sz;
38022+ unsigned long pindex;
38023+ loff_t pos, pend;
38024+ struct au_sbinfo *sbinfo;
5527c038 38025+ vfs_readf_t func;
1facf9fc 38026+ ino_t *ino;
38027+ unsigned long *p;
38028+
38029+ err = 0;
38030+ sbinfo = au_sbi(sb);
dece6358 38031+ MtxMustLock(&sbinfo->si_xib_mtx);
1facf9fc 38032+ p = sbinfo->si_xib_buf;
38033+ func = sbinfo->si_xread;
c06a8ce3 38034+ pend = vfsub_f_size_read(file);
1facf9fc 38035+ pos = 0;
38036+ while (pos < pend) {
38037+ sz = xino_fread(func, file, page, PAGE_SIZE, &pos);
38038+ err = sz;
38039+ if (unlikely(sz <= 0))
38040+ goto out;
38041+
38042+ err = 0;
38043+ for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
38044+ if (unlikely(*ino < AUFS_FIRST_INO))
38045+ continue;
38046+
38047+ xib_calc_bit(*ino, &pindex, &bit);
38048+ AuDebugOn(page_bits <= bit);
38049+ err = xib_pindex(sb, pindex);
38050+ if (!err)
38051+ set_bit(bit, p);
38052+ else
38053+ goto out;
38054+ }
38055+ }
38056+
4f0767ce 38057+out:
1facf9fc 38058+ return err;
38059+}
38060+
38061+static int xib_restore(struct super_block *sb)
38062+{
acd2b654
AM
38063+ int err, i;
38064+ unsigned int nfile;
5afbbe0d 38065+ aufs_bindex_t bindex, bbot;
1facf9fc 38066+ void *page;
062440b3 38067+ struct au_branch *br;
acd2b654
AM
38068+ struct au_xino *xi;
38069+ struct file *file;
1facf9fc 38070+
38071+ err = -ENOMEM;
38072+ page = (void *)__get_free_page(GFP_NOFS);
38073+ if (unlikely(!page))
38074+ goto out;
38075+
38076+ err = 0;
5afbbe0d
AM
38077+ bbot = au_sbbot(sb);
38078+ for (bindex = 0; !err && bindex <= bbot; bindex++)
062440b3
AM
38079+ if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0) {
38080+ br = au_sbr(sb, bindex);
acd2b654
AM
38081+ xi = br->br_xino;
38082+ nfile = xi->xi_nfile;
38083+ for (i = 0; i < nfile; i++) {
38084+ file = au_xino_file(xi, i);
38085+ if (file)
38086+ err = do_xib_restore(sb, file, page);
38087+ }
062440b3
AM
38088+ } else
38089+ AuDbg("skip shared b%d\n", bindex);
1c60b727 38090+ free_page((unsigned long)page);
1facf9fc 38091+
4f0767ce 38092+out:
1facf9fc 38093+ return err;
38094+}
38095+
38096+int au_xib_trunc(struct super_block *sb)
38097+{
38098+ int err;
38099+ ssize_t sz;
38100+ loff_t pos;
1facf9fc 38101+ struct au_sbinfo *sbinfo;
38102+ unsigned long *p;
38103+ struct file *file;
38104+
dece6358
AM
38105+ SiMustWriteLock(sb);
38106+
1facf9fc 38107+ err = 0;
38108+ sbinfo = au_sbi(sb);
38109+ if (!au_opt_test(sbinfo->si_mntflags, XINO))
38110+ goto out;
38111+
38112+ file = sbinfo->si_xib;
c06a8ce3 38113+ if (vfsub_f_size_read(file) <= PAGE_SIZE)
1facf9fc 38114+ goto out;
38115+
062440b3 38116+ file = au_xino_create2(sb, &sbinfo->si_xib->f_path, NULL);
1facf9fc 38117+ err = PTR_ERR(file);
38118+ if (IS_ERR(file))
38119+ goto out;
38120+ fput(sbinfo->si_xib);
38121+ sbinfo->si_xib = file;
38122+
38123+ p = sbinfo->si_xib_buf;
38124+ memset(p, 0, PAGE_SIZE);
38125+ pos = 0;
38126+ sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xib, p, PAGE_SIZE, &pos);
38127+ if (unlikely(sz != PAGE_SIZE)) {
38128+ err = sz;
38129+ AuIOErr("err %d\n", err);
38130+ if (sz >= 0)
38131+ err = -EIO;
38132+ goto out;
38133+ }
38134+
38135+ mutex_lock(&sbinfo->si_xib_mtx);
38136+ /* mnt_want_write() is unnecessary here */
38137+ err = xib_restore(sb);
38138+ mutex_unlock(&sbinfo->si_xib_mtx);
38139+
38140+out:
38141+ return err;
38142+}
38143+
38144+/* ---------------------------------------------------------------------- */
38145+
acd2b654 38146+struct au_xino *au_xino_alloc(unsigned int nfile)
062440b3
AM
38147+{
38148+ struct au_xino *xi;
38149+
38150+ xi = kzalloc(sizeof(*xi), GFP_NOFS);
38151+ if (unlikely(!xi))
38152+ goto out;
acd2b654
AM
38153+ xi->xi_nfile = nfile;
38154+ xi->xi_file = kcalloc(nfile, sizeof(*xi->xi_file), GFP_NOFS);
38155+ if (unlikely(!xi->xi_file))
38156+ goto out_free;
062440b3
AM
38157+
38158+ xi->xi_nondir.total = 8; /* initial size */
38159+ xi->xi_nondir.array = kcalloc(xi->xi_nondir.total, sizeof(ino_t),
38160+ GFP_NOFS);
38161+ if (unlikely(!xi->xi_nondir.array))
acd2b654 38162+ goto out_file;
062440b3
AM
38163+
38164+ spin_lock_init(&xi->xi_nondir.spin);
38165+ init_waitqueue_head(&xi->xi_nondir.wqh);
acd2b654 38166+ mutex_init(&xi->xi_mtx);
e49925d1 38167+ INIT_HLIST_BL_HEAD(&xi->xi_writing);
062440b3
AM
38168+ atomic_set(&xi->xi_truncating, 0);
38169+ kref_init(&xi->xi_kref);
38170+ goto out; /* success */
38171+
acd2b654 38172+out_file:
e49925d1 38173+ au_kfree_try_rcu(xi->xi_file);
062440b3 38174+out_free:
e49925d1 38175+ au_kfree_rcu(xi);
062440b3
AM
38176+ xi = NULL;
38177+out:
38178+ return xi;
38179+}
38180+
acd2b654 38181+static int au_xino_init(struct au_branch *br, int idx, struct file *file)
062440b3
AM
38182+{
38183+ int err;
38184+ struct au_xino *xi;
38185+
38186+ err = 0;
acd2b654 38187+ xi = au_xino_alloc(idx + 1);
062440b3
AM
38188+ if (unlikely(!xi)) {
38189+ err = -ENOMEM;
38190+ goto out;
38191+ }
38192+
acd2b654
AM
38193+ if (file)
38194+ get_file(file);
38195+ xi->xi_file[idx] = file;
062440b3
AM
38196+ AuDebugOn(br->br_xino);
38197+ br->br_xino = xi;
38198+
38199+out:
38200+ return err;
38201+}
38202+
38203+static void au_xino_release(struct kref *kref)
38204+{
38205+ struct au_xino *xi;
38206+ int i;
e49925d1
JR
38207+ unsigned long ul;
38208+ struct hlist_bl_head *hbl;
38209+ struct hlist_bl_node *pos, *n;
38210+ struct au_xi_writing *p;
062440b3
AM
38211+
38212+ xi = container_of(kref, struct au_xino, xi_kref);
acd2b654
AM
38213+ for (i = 0; i < xi->xi_nfile; i++)
38214+ if (xi->xi_file[i])
38215+ fput(xi->xi_file[i]);
062440b3
AM
38216+ for (i = xi->xi_nondir.total - 1; i >= 0; i--)
38217+ AuDebugOn(xi->xi_nondir.array[i]);
acd2b654 38218+ mutex_destroy(&xi->xi_mtx);
e49925d1
JR
38219+ hbl = &xi->xi_writing;
38220+ ul = au_hbl_count(hbl);
38221+ if (unlikely(ul)) {
38222+ pr_warn("xi_writing %lu\n", ul);
38223+ hlist_bl_lock(hbl);
0b487229 38224+ hlist_bl_for_each_entry_safe(p, pos, n, hbl, node) {
e49925d1 38225+ hlist_bl_del(&p->node);
0b487229
JR
38226+ /* kmemleak reported au_kfree_rcu() doesn't free it */
38227+ kfree(p);
e49925d1
JR
38228+ }
38229+ hlist_bl_unlock(hbl);
38230+ }
38231+ au_kfree_try_rcu(xi->xi_file);
38232+ au_kfree_try_rcu(xi->xi_nondir.array);
38233+ au_kfree_rcu(xi);
062440b3
AM
38234+}
38235+
38236+int au_xino_put(struct au_branch *br)
38237+{
38238+ int ret;
38239+ struct au_xino *xi;
38240+
38241+ ret = 0;
38242+ xi = br->br_xino;
38243+ if (xi) {
38244+ br->br_xino = NULL;
38245+ ret = kref_put(&xi->xi_kref, au_xino_release);
38246+ }
38247+
38248+ return ret;
38249+}
38250+
062440b3
AM
38251+/* ---------------------------------------------------------------------- */
38252+
1facf9fc 38253+/*
38254+ * xino mount option handlers
38255+ */
1facf9fc 38256+
38257+/* xino bitmap */
38258+static void xino_clear_xib(struct super_block *sb)
38259+{
38260+ struct au_sbinfo *sbinfo;
38261+
dece6358
AM
38262+ SiMustWriteLock(sb);
38263+
1facf9fc 38264+ sbinfo = au_sbi(sb);
062440b3 38265+ /* unnecessary to clear sbinfo->si_xread and ->si_xwrite */
1facf9fc 38266+ if (sbinfo->si_xib)
38267+ fput(sbinfo->si_xib);
38268+ sbinfo->si_xib = NULL;
f0c0a007 38269+ if (sbinfo->si_xib_buf)
1c60b727 38270+ free_page((unsigned long)sbinfo->si_xib_buf);
1facf9fc 38271+ sbinfo->si_xib_buf = NULL;
38272+}
38273+
062440b3 38274+static int au_xino_set_xib(struct super_block *sb, struct path *path)
1facf9fc 38275+{
38276+ int err;
38277+ loff_t pos;
38278+ struct au_sbinfo *sbinfo;
38279+ struct file *file;
acd2b654 38280+ struct super_block *xi_sb;
1facf9fc 38281+
dece6358
AM
38282+ SiMustWriteLock(sb);
38283+
1facf9fc 38284+ sbinfo = au_sbi(sb);
062440b3 38285+ file = au_xino_create2(sb, path, sbinfo->si_xib);
1facf9fc 38286+ err = PTR_ERR(file);
38287+ if (IS_ERR(file))
38288+ goto out;
38289+ if (sbinfo->si_xib)
38290+ fput(sbinfo->si_xib);
38291+ sbinfo->si_xib = file;
5527c038
JR
38292+ sbinfo->si_xread = vfs_readf(file);
38293+ sbinfo->si_xwrite = vfs_writef(file);
acd2b654
AM
38294+ xi_sb = file_inode(file)->i_sb;
38295+ sbinfo->si_ximaxent = xi_sb->s_maxbytes;
38296+ if (unlikely(sbinfo->si_ximaxent < PAGE_SIZE)) {
38297+ err = -EIO;
38298+ pr_err("s_maxbytes(%llu) on %s is too small\n",
38299+ (u64)sbinfo->si_ximaxent, au_sbtype(xi_sb));
38300+ goto out_unset;
38301+ }
38302+ sbinfo->si_ximaxent /= sizeof(ino_t);
1facf9fc 38303+
38304+ err = -ENOMEM;
38305+ if (!sbinfo->si_xib_buf)
38306+ sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
38307+ if (unlikely(!sbinfo->si_xib_buf))
38308+ goto out_unset;
38309+
38310+ sbinfo->si_xib_last_pindex = 0;
38311+ sbinfo->si_xib_next_bit = 0;
c06a8ce3 38312+ if (vfsub_f_size_read(file) < PAGE_SIZE) {
1facf9fc 38313+ pos = 0;
38314+ err = xino_fwrite(sbinfo->si_xwrite, file, sbinfo->si_xib_buf,
38315+ PAGE_SIZE, &pos);
38316+ if (unlikely(err != PAGE_SIZE))
38317+ goto out_free;
38318+ }
38319+ err = 0;
38320+ goto out; /* success */
38321+
4f0767ce 38322+out_free:
f0c0a007 38323+ if (sbinfo->si_xib_buf)
1c60b727 38324+ free_page((unsigned long)sbinfo->si_xib_buf);
b752ccd1
AM
38325+ sbinfo->si_xib_buf = NULL;
38326+ if (err >= 0)
38327+ err = -EIO;
4f0767ce 38328+out_unset:
b752ccd1
AM
38329+ fput(sbinfo->si_xib);
38330+ sbinfo->si_xib = NULL;
4f0767ce 38331+out:
062440b3 38332+ AuTraceErr(err);
b752ccd1 38333+ return err;
1facf9fc 38334+}
38335+
b752ccd1
AM
38336+/* xino for each branch */
38337+static void xino_clear_br(struct super_block *sb)
38338+{
5afbbe0d 38339+ aufs_bindex_t bindex, bbot;
b752ccd1 38340+ struct au_branch *br;
1facf9fc 38341+
5afbbe0d
AM
38342+ bbot = au_sbbot(sb);
38343+ for (bindex = 0; bindex <= bbot; bindex++) {
b752ccd1 38344+ br = au_sbr(sb, bindex);
062440b3
AM
38345+ AuDebugOn(!br);
38346+ au_xino_put(br);
38347+ }
38348+}
38349+
38350+static void au_xino_set_br_shared(struct super_block *sb, struct au_branch *br,
38351+ aufs_bindex_t bshared)
38352+{
38353+ struct au_branch *brshared;
b752ccd1 38354+
062440b3
AM
38355+ brshared = au_sbr(sb, bshared);
38356+ AuDebugOn(!brshared->br_xino);
38357+ AuDebugOn(!brshared->br_xino->xi_file);
38358+ if (br->br_xino != brshared->br_xino) {
38359+ au_xino_get(brshared);
38360+ au_xino_put(br);
38361+ br->br_xino = brshared->br_xino;
b752ccd1
AM
38362+ }
38363+}
38364+
062440b3
AM
38365+struct au_xino_do_set_br {
38366+ vfs_writef_t writef;
38367+ struct au_branch *br;
38368+ ino_t h_ino;
38369+ aufs_bindex_t bshared;
38370+};
38371+
38372+static int au_xino_do_set_br(struct super_block *sb, struct path *path,
38373+ struct au_xino_do_set_br *args)
1facf9fc 38374+{
38375+ int err;
acd2b654 38376+ struct au_xi_calc calc;
062440b3 38377+ struct file *file;
acd2b654
AM
38378+ struct au_branch *br;
38379+ struct au_xi_new xinew = {
38380+ .base = path
38381+ };
062440b3 38382+
acd2b654
AM
38383+ br = args->br;
38384+ xinew.xi = br->br_xino;
38385+ au_xi_calc(sb, args->h_ino, &calc);
38386+ xinew.copy_src = au_xino_file(xinew.xi, calc.idx);
38387+ if (args->bshared >= 0)
062440b3 38388+ /* shared xino */
acd2b654
AM
38389+ au_xino_set_br_shared(sb, br, args->bshared);
38390+ else if (!xinew.xi) {
38391+ /* new xino */
38392+ err = au_xino_init(br, calc.idx, xinew.copy_src);
38393+ if (unlikely(err))
38394+ goto out;
062440b3
AM
38395+ }
38396+
acd2b654
AM
38397+ /* force re-creating */
38398+ xinew.xi = br->br_xino;
38399+ xinew.idx = calc.idx;
38400+ mutex_lock(&xinew.xi->xi_mtx);
38401+ file = au_xi_new(sb, &xinew);
38402+ mutex_unlock(&xinew.xi->xi_mtx);
062440b3
AM
38403+ err = PTR_ERR(file);
38404+ if (IS_ERR(file))
38405+ goto out;
acd2b654
AM
38406+ AuDebugOn(!file);
38407+
38408+ err = au_xino_do_write(args->writef, file, &calc, AUFS_ROOT_INO);
38409+ if (unlikely(err))
38410+ au_xino_put(br);
062440b3 38411+
062440b3
AM
38412+out:
38413+ AuTraceErr(err);
38414+ return err;
38415+}
38416+
38417+static int au_xino_set_br(struct super_block *sb, struct path *path)
38418+{
38419+ int err;
38420+ aufs_bindex_t bindex, bbot;
38421+ struct au_xino_do_set_br args;
b752ccd1 38422+ struct inode *inode;
1facf9fc 38423+
b752ccd1
AM
38424+ SiMustWriteLock(sb);
38425+
5afbbe0d 38426+ bbot = au_sbbot(sb);
5527c038 38427+ inode = d_inode(sb->s_root);
062440b3
AM
38428+ args.writef = au_sbi(sb)->si_xwrite;
38429+ for (bindex = 0; bindex <= bbot; bindex++) {
38430+ args.h_ino = au_h_iptr(inode, bindex)->i_ino;
38431+ args.br = au_sbr(sb, bindex);
38432+ args.bshared = is_sb_shared(sb, bindex, bindex - 1);
38433+ err = au_xino_do_set_br(sb, path, &args);
b752ccd1 38434+ if (unlikely(err))
062440b3 38435+ break;
b752ccd1 38436+ }
1facf9fc 38437+
062440b3 38438+ AuTraceErr(err);
1facf9fc 38439+ return err;
38440+}
b752ccd1
AM
38441+
38442+void au_xino_clr(struct super_block *sb)
38443+{
38444+ struct au_sbinfo *sbinfo;
38445+
38446+ au_xigen_clr(sb);
38447+ xino_clear_xib(sb);
38448+ xino_clear_br(sb);
062440b3 38449+ dbgaufs_brs_del(sb, 0);
b752ccd1
AM
38450+ sbinfo = au_sbi(sb);
38451+ /* lvalue, do not call au_mntflags() */
38452+ au_opt_clr(sbinfo->si_mntflags, XINO);
38453+}
38454+
062440b3 38455+int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount)
b752ccd1
AM
38456+{
38457+ int err, skip;
062440b3 38458+ struct dentry *dentry, *parent, *cur_dentry, *cur_parent;
b752ccd1
AM
38459+ struct qstr *dname, *cur_name;
38460+ struct file *cur_xino;
b752ccd1 38461+ struct au_sbinfo *sbinfo;
062440b3 38462+ struct path *path, *cur_path;
b752ccd1
AM
38463+
38464+ SiMustWriteLock(sb);
38465+
38466+ err = 0;
38467+ sbinfo = au_sbi(sb);
062440b3
AM
38468+ path = &xiopt->file->f_path;
38469+ dentry = path->dentry;
38470+ parent = dget_parent(dentry);
b752ccd1
AM
38471+ if (remount) {
38472+ skip = 0;
b752ccd1
AM
38473+ cur_xino = sbinfo->si_xib;
38474+ if (cur_xino) {
062440b3
AM
38475+ cur_path = &cur_xino->f_path;
38476+ cur_dentry = cur_path->dentry;
38477+ cur_parent = dget_parent(cur_dentry);
38478+ cur_name = &cur_dentry->d_name;
38479+ dname = &dentry->d_name;
b752ccd1 38480+ skip = (cur_parent == parent
38d290e6 38481+ && au_qstreq(dname, cur_name));
b752ccd1
AM
38482+ dput(cur_parent);
38483+ }
38484+ if (skip)
38485+ goto out;
38486+ }
38487+
38488+ au_opt_set(sbinfo->si_mntflags, XINO);
062440b3
AM
38489+ err = au_xino_set_xib(sb, path);
38490+ /* si_x{read,write} are set */
b752ccd1 38491+ if (!err)
062440b3 38492+ err = au_xigen_set(sb, path);
b752ccd1 38493+ if (!err)
062440b3
AM
38494+ err = au_xino_set_br(sb, path);
38495+ if (!err) {
38496+ dbgaufs_brs_add(sb, 0, /*topdown*/1);
b752ccd1 38497+ goto out; /* success */
062440b3 38498+ }
b752ccd1
AM
38499+
38500+ /* reset all */
062440b3
AM
38501+ AuIOErr("failed setting xino(%d).\n", err);
38502+ au_xino_clr(sb);
b752ccd1 38503+
4f0767ce 38504+out:
b752ccd1
AM
38505+ dput(parent);
38506+ return err;
38507+}
38508+
b752ccd1
AM
38509+/*
38510+ * create a xinofile at the default place/path.
38511+ */
38512+struct file *au_xino_def(struct super_block *sb)
38513+{
38514+ struct file *file;
38515+ char *page, *p;
38516+ struct au_branch *br;
38517+ struct super_block *h_sb;
38518+ struct path path;
5afbbe0d 38519+ aufs_bindex_t bbot, bindex, bwr;
b752ccd1
AM
38520+
38521+ br = NULL;
5afbbe0d 38522+ bbot = au_sbbot(sb);
b752ccd1 38523+ bwr = -1;
5afbbe0d 38524+ for (bindex = 0; bindex <= bbot; bindex++) {
b752ccd1
AM
38525+ br = au_sbr(sb, bindex);
38526+ if (au_br_writable(br->br_perm)
86dc4139 38527+ && !au_test_fs_bad_xino(au_br_sb(br))) {
b752ccd1
AM
38528+ bwr = bindex;
38529+ break;
38530+ }
38531+ }
38532+
7f207e10
AM
38533+ if (bwr >= 0) {
38534+ file = ERR_PTR(-ENOMEM);
537831f9 38535+ page = (void *)__get_free_page(GFP_NOFS);
7f207e10
AM
38536+ if (unlikely(!page))
38537+ goto out;
86dc4139 38538+ path.mnt = au_br_mnt(br);
7f207e10
AM
38539+ path.dentry = au_h_dptr(sb->s_root, bwr);
38540+ p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
38541+ file = (void *)p;
38542+ if (!IS_ERR(p)) {
38543+ strcat(p, "/" AUFS_XINO_FNAME);
38544+ AuDbg("%s\n", p);
0b487229 38545+ file = au_xino_create(sb, p, /*silent*/0, /*wbrtop*/1);
7f207e10 38546+ }
1c60b727 38547+ free_page((unsigned long)page);
7f207e10 38548+ } else {
0b487229
JR
38549+ file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0,
38550+ /*wbrtop*/0);
7f207e10
AM
38551+ if (IS_ERR(file))
38552+ goto out;
2000de60 38553+ h_sb = file->f_path.dentry->d_sb;
7f207e10
AM
38554+ if (unlikely(au_test_fs_bad_xino(h_sb))) {
38555+ pr_err("xino doesn't support %s(%s)\n",
38556+ AUFS_XINO_DEFPATH, au_sbtype(h_sb));
38557+ fput(file);
38558+ file = ERR_PTR(-EINVAL);
38559+ }
7f207e10 38560+ }
0c5527e5 38561+
7f207e10
AM
38562+out:
38563+ return file;
38564+}
38565+
38566+/* ---------------------------------------------------------------------- */
38567+
062440b3
AM
38568+/*
38569+ * initialize the xinofile for the specified branch @br
38570+ * at the place/path where @base_file indicates.
38571+ * test whether another branch is on the same filesystem or not,
38572+ * if found then share the xinofile with another branch.
38573+ */
38574+int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
38575+ struct path *base)
7f207e10
AM
38576+{
38577+ int err;
062440b3
AM
38578+ struct au_xino_do_set_br args = {
38579+ .h_ino = h_ino,
38580+ .br = br
38581+ };
7f207e10 38582+
062440b3
AM
38583+ args.writef = au_sbi(sb)->si_xwrite;
38584+ args.bshared = sbr_find_shared(sb, /*btop*/0, au_sbbot(sb),
38585+ au_br_sb(br));
38586+ err = au_xino_do_set_br(sb, base, &args);
79b8bda9 38587+ if (unlikely(err))
062440b3 38588+ au_xino_put(br);
7f207e10 38589+
7f207e10
AM
38590+ return err;
38591+}
521ced18
JR
38592+
38593+/* ---------------------------------------------------------------------- */
38594+
062440b3
AM
38595+/*
38596+ * get an unused inode number from bitmap
38597+ */
38598+ino_t au_xino_new_ino(struct super_block *sb)
38599+{
38600+ ino_t ino;
38601+ unsigned long *p, pindex, ul, pend;
38602+ struct au_sbinfo *sbinfo;
38603+ struct file *file;
38604+ int free_bit, err;
38605+
38606+ if (!au_opt_test(au_mntflags(sb), XINO))
38607+ return iunique(sb, AUFS_FIRST_INO);
38608+
38609+ sbinfo = au_sbi(sb);
38610+ mutex_lock(&sbinfo->si_xib_mtx);
38611+ p = sbinfo->si_xib_buf;
38612+ free_bit = sbinfo->si_xib_next_bit;
38613+ if (free_bit < page_bits && !test_bit(free_bit, p))
38614+ goto out; /* success */
38615+ free_bit = find_first_zero_bit(p, page_bits);
38616+ if (free_bit < page_bits)
38617+ goto out; /* success */
38618+
38619+ pindex = sbinfo->si_xib_last_pindex;
38620+ for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
38621+ err = xib_pindex(sb, ul);
38622+ if (unlikely(err))
38623+ goto out_err;
38624+ free_bit = find_first_zero_bit(p, page_bits);
38625+ if (free_bit < page_bits)
38626+ goto out; /* success */
38627+ }
38628+
38629+ file = sbinfo->si_xib;
38630+ pend = vfsub_f_size_read(file) / PAGE_SIZE;
38631+ for (ul = pindex + 1; ul <= pend; ul++) {
38632+ err = xib_pindex(sb, ul);
38633+ if (unlikely(err))
38634+ goto out_err;
38635+ free_bit = find_first_zero_bit(p, page_bits);
38636+ if (free_bit < page_bits)
38637+ goto out; /* success */
38638+ }
38639+ BUG();
38640+
38641+out:
38642+ set_bit(free_bit, p);
38643+ sbinfo->si_xib_next_bit = free_bit + 1;
38644+ pindex = sbinfo->si_xib_last_pindex;
38645+ mutex_unlock(&sbinfo->si_xib_mtx);
38646+ ino = xib_calc_ino(pindex, free_bit);
38647+ AuDbg("i%lu\n", (unsigned long)ino);
38648+ return ino;
38649+out_err:
38650+ mutex_unlock(&sbinfo->si_xib_mtx);
38651+ AuDbg("i0\n");
38652+ return 0;
38653+}
38654+
38655+/* for s_op->delete_inode() */
38656+void au_xino_delete_inode(struct inode *inode, const int unlinked)
521ced18 38657+{
062440b3
AM
38658+ int err;
38659+ unsigned int mnt_flags;
38660+ aufs_bindex_t bindex, bbot, bi;
38661+ unsigned char try_trunc;
38662+ struct au_iinfo *iinfo;
38663+ struct super_block *sb;
38664+ struct au_hinode *hi;
38665+ struct inode *h_inode;
38666+ struct au_branch *br;
38667+ vfs_writef_t xwrite;
acd2b654
AM
38668+ struct au_xi_calc calc;
38669+ struct file *file;
521ced18 38670+
062440b3 38671+ AuDebugOn(au_is_bad_inode(inode));
521ced18 38672+
062440b3
AM
38673+ sb = inode->i_sb;
38674+ mnt_flags = au_mntflags(sb);
38675+ if (!au_opt_test(mnt_flags, XINO)
38676+ || inode->i_ino == AUFS_ROOT_INO)
38677+ return;
38678+
38679+ if (unlinked) {
38680+ au_xigen_inc(inode);
38681+ au_xib_clear_bit(inode);
38682+ }
38683+
38684+ iinfo = au_ii(inode);
38685+ bindex = iinfo->ii_btop;
38686+ if (bindex < 0)
38687+ return;
38688+
38689+ xwrite = au_sbi(sb)->si_xwrite;
38690+ try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
38691+ hi = au_hinode(iinfo, bindex);
38692+ bbot = iinfo->ii_bbot;
38693+ for (; bindex <= bbot; bindex++, hi++) {
38694+ h_inode = hi->hi_inode;
38695+ if (!h_inode
38696+ || (!unlinked && h_inode->i_nlink))
38697+ continue;
38698+
38699+ /* inode may not be revalidated */
38700+ bi = au_br_index(sb, hi->hi_id);
38701+ if (bi < 0)
38702+ continue;
38703+
38704+ br = au_sbr(sb, bi);
acd2b654
AM
38705+ au_xi_calc(sb, h_inode->i_ino, &calc);
38706+ file = au_xino_file(br->br_xino, calc.idx);
38707+ if (IS_ERR_OR_NULL(file))
38708+ continue;
38709+
38710+ err = au_xino_do_write(xwrite, file, &calc, /*ino*/0);
062440b3
AM
38711+ if (!err && try_trunc
38712+ && au_test_fs_trunc_xino(au_br_sb(br)))
38713+ xino_try_trunc(sb, br);
38714+ }
521ced18
JR
38715+}
38716+
062440b3
AM
38717+/* ---------------------------------------------------------------------- */
38718+
38719+static int au_xinondir_find(struct au_xino *xi, ino_t h_ino)
521ced18
JR
38720+{
38721+ int found, total, i;
38722+
38723+ found = -1;
062440b3 38724+ total = xi->xi_nondir.total;
521ced18 38725+ for (i = 0; i < total; i++) {
062440b3 38726+ if (xi->xi_nondir.array[i] != h_ino)
521ced18
JR
38727+ continue;
38728+ found = i;
38729+ break;
38730+ }
38731+
38732+ return found;
38733+}
38734+
062440b3 38735+static int au_xinondir_expand(struct au_xino *xi)
521ced18
JR
38736+{
38737+ int err, sz;
38738+ ino_t *p;
38739+
38740+ BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
38741+
38742+ err = -ENOMEM;
062440b3 38743+ sz = xi->xi_nondir.total * sizeof(ino_t);
521ced18
JR
38744+ if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
38745+ goto out;
062440b3 38746+ p = au_kzrealloc(xi->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
521ced18
JR
38747+ /*may_shrink*/0);
38748+ if (p) {
062440b3
AM
38749+ xi->xi_nondir.array = p;
38750+ xi->xi_nondir.total <<= 1;
38751+ AuDbg("xi_nondir.total %d\n", xi->xi_nondir.total);
521ced18
JR
38752+ err = 0;
38753+ }
38754+
38755+out:
38756+ return err;
38757+}
38758+
062440b3
AM
38759+void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
38760+ ino_t h_ino, int idx)
38761+{
38762+ struct au_xino *xi;
38763+
38764+ AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
38765+ xi = au_sbr(sb, bindex)->br_xino;
38766+ AuDebugOn(idx < 0 || xi->xi_nondir.total <= idx);
38767+
38768+ spin_lock(&xi->xi_nondir.spin);
38769+ AuDebugOn(xi->xi_nondir.array[idx] != h_ino);
38770+ xi->xi_nondir.array[idx] = 0;
38771+ spin_unlock(&xi->xi_nondir.spin);
38772+ wake_up_all(&xi->xi_nondir.wqh);
38773+}
38774+
521ced18
JR
38775+int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
38776+ int *idx)
38777+{
38778+ int err, found, empty;
062440b3 38779+ struct au_xino *xi;
521ced18
JR
38780+
38781+ err = 0;
38782+ *idx = -1;
38783+ if (!au_opt_test(au_mntflags(sb), XINO))
38784+ goto out; /* no xino */
38785+
062440b3 38786+ xi = au_sbr(sb, bindex)->br_xino;
521ced18
JR
38787+
38788+again:
062440b3
AM
38789+ spin_lock(&xi->xi_nondir.spin);
38790+ found = au_xinondir_find(xi, h_ino);
521ced18 38791+ if (found == -1) {
062440b3 38792+ empty = au_xinondir_find(xi, /*h_ino*/0);
521ced18 38793+ if (empty == -1) {
062440b3
AM
38794+ empty = xi->xi_nondir.total;
38795+ err = au_xinondir_expand(xi);
521ced18
JR
38796+ if (unlikely(err))
38797+ goto out_unlock;
38798+ }
062440b3 38799+ xi->xi_nondir.array[empty] = h_ino;
521ced18
JR
38800+ *idx = empty;
38801+ } else {
062440b3
AM
38802+ spin_unlock(&xi->xi_nondir.spin);
38803+ wait_event(xi->xi_nondir.wqh,
38804+ xi->xi_nondir.array[found] != h_ino);
521ced18
JR
38805+ goto again;
38806+ }
38807+
38808+out_unlock:
062440b3
AM
38809+ spin_unlock(&xi->xi_nondir.spin);
38810+out:
38811+ return err;
38812+}
38813+
38814+/* ---------------------------------------------------------------------- */
38815+
38816+int au_xino_path(struct seq_file *seq, struct file *file)
38817+{
38818+ int err;
38819+
38820+ err = au_seq_path(seq, &file->f_path);
38821+ if (unlikely(err))
38822+ goto out;
38823+
38824+#define Deleted "\\040(deleted)"
38825+ seq->count -= sizeof(Deleted) - 1;
38826+ AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
38827+ sizeof(Deleted) - 1));
38828+#undef Deleted
38829+
521ced18
JR
38830+out:
38831+ return err;
38832+}
537831f9
AM
38833diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
38834--- /usr/share/empty/include/uapi/linux/aufs_type.h 1970-01-01 01:00:00.000000000 +0100
0b487229
JR
38835+++ linux/include/uapi/linux/aufs_type.h 2021-02-22 23:30:37.706014241 +0100
38836@@ -0,0 +1,452 @@
062440b3 38837+/* SPDX-License-Identifier: GPL-2.0 */
7f207e10 38838+/*
0b487229 38839+ * Copyright (C) 2005-2020 Junjiro R. Okajima
7f207e10
AM
38840+ *
38841+ * This program, aufs is free software; you can redistribute it and/or modify
38842+ * it under the terms of the GNU General Public License as published by
38843+ * the Free Software Foundation; either version 2 of the License, or
38844+ * (at your option) any later version.
38845+ *
38846+ * This program is distributed in the hope that it will be useful,
38847+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
38848+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38849+ * GNU General Public License for more details.
38850+ *
38851+ * You should have received a copy of the GNU General Public License
523b37e3 38852+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7f207e10
AM
38853+ */
38854+
38855+#ifndef __AUFS_TYPE_H__
38856+#define __AUFS_TYPE_H__
38857+
f6c5ef8b
AM
38858+#define AUFS_NAME "aufs"
38859+
9dbd164d 38860+#ifdef __KERNEL__
f6c5ef8b
AM
38861+/*
38862+ * define it before including all other headers.
38863+ * sched.h may use pr_* macros before defining "current", so define the
38864+ * no-current version first, and re-define later.
38865+ */
38866+#define pr_fmt(fmt) AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
38867+#include <linux/sched.h>
38868+#undef pr_fmt
a2a7ad62
AM
38869+#define pr_fmt(fmt) \
38870+ AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
38871+ (int)sizeof(current->comm), current->comm, current->pid
0b487229 38872+#include <linux/limits.h>
9dbd164d
AM
38873+#else
38874+#include <stdint.h>
38875+#include <sys/types.h>
0b487229 38876+#include <limits.h>
f6c5ef8b 38877+#endif /* __KERNEL__ */
7f207e10 38878+
0b487229 38879+#define AUFS_VERSION "4.19-20200622"
7f207e10
AM
38880+
38881+/* todo? move this to linux-2.6.19/include/magic.h */
38882+#define AUFS_SUPER_MAGIC ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
38883+
38884+/* ---------------------------------------------------------------------- */
38885+
0b487229 38886+#ifdef __KERNEL__
7f207e10 38887+#ifdef CONFIG_AUFS_BRANCH_MAX_127
9dbd164d 38888+typedef int8_t aufs_bindex_t;
7f207e10
AM
38889+#define AUFS_BRANCH_MAX 127
38890+#else
9dbd164d 38891+typedef int16_t aufs_bindex_t;
7f207e10
AM
38892+#ifdef CONFIG_AUFS_BRANCH_MAX_511
38893+#define AUFS_BRANCH_MAX 511
38894+#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
38895+#define AUFS_BRANCH_MAX 1023
38896+#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
38897+#define AUFS_BRANCH_MAX 32767
38898+#endif
38899+#endif
38900+
7f207e10
AM
38901+#ifndef AUFS_BRANCH_MAX
38902+#error unknown CONFIG_AUFS_BRANCH_MAX value
38903+#endif
38904+#endif /* __KERNEL__ */
38905+
38906+/* ---------------------------------------------------------------------- */
38907+
7f207e10
AM
38908+#define AUFS_FSTYPE AUFS_NAME
38909+
38910+#define AUFS_ROOT_INO 2
38911+#define AUFS_FIRST_INO 11
38912+
38913+#define AUFS_WH_PFX ".wh."
38914+#define AUFS_WH_PFX_LEN ((int)sizeof(AUFS_WH_PFX) - 1)
38915+#define AUFS_WH_TMP_LEN 4
86dc4139 38916+/* a limit for rmdir/rename a dir and copyup */
7f207e10
AM
38917+#define AUFS_MAX_NAMELEN (NAME_MAX \
38918+ - AUFS_WH_PFX_LEN * 2 /* doubly whiteouted */\
38919+ - 1 /* dot */\
38920+ - AUFS_WH_TMP_LEN) /* hex */
38921+#define AUFS_XINO_FNAME "." AUFS_NAME ".xino"
38922+#define AUFS_XINO_DEFPATH "/tmp/" AUFS_XINO_FNAME
392086de
AM
38923+#define AUFS_XINO_DEF_SEC 30 /* seconds */
38924+#define AUFS_XINO_DEF_TRUNC 45 /* percentage */
7f207e10
AM
38925+#define AUFS_DIRWH_DEF 3
38926+#define AUFS_RDCACHE_DEF 10 /* seconds */
027c5e7a 38927+#define AUFS_RDCACHE_MAX 3600 /* seconds */
7f207e10
AM
38928+#define AUFS_RDBLK_DEF 512 /* bytes */
38929+#define AUFS_RDHASH_DEF 32
38930+#define AUFS_WKQ_NAME AUFS_NAME "d"
027c5e7a
AM
38931+#define AUFS_MFS_DEF_SEC 30 /* seconds */
38932+#define AUFS_MFS_MAX_SEC 3600 /* seconds */
076b876e 38933+#define AUFS_FHSM_CACHE_DEF_SEC 30 /* seconds */
86dc4139 38934+#define AUFS_PLINK_WARN 50 /* number of plinks in a single bucket */
7f207e10
AM
38935+
38936+/* pseudo-link maintenace under /proc */
38937+#define AUFS_PLINK_MAINT_NAME "plink_maint"
38938+#define AUFS_PLINK_MAINT_DIR "fs/" AUFS_NAME
38939+#define AUFS_PLINK_MAINT_PATH AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
38940+
8b6a4947
AM
38941+/* dirren, renamed dir */
38942+#define AUFS_DR_INFO_PFX AUFS_WH_PFX ".dr."
38943+#define AUFS_DR_BRHINO_NAME AUFS_WH_PFX "hino"
38944+/* whiteouted doubly */
38945+#define AUFS_WH_DR_INFO_PFX AUFS_WH_PFX AUFS_DR_INFO_PFX
38946+#define AUFS_WH_DR_BRHINO AUFS_WH_PFX AUFS_DR_BRHINO_NAME
38947+
7f207e10
AM
38948+#define AUFS_DIROPQ_NAME AUFS_WH_PFX ".opq" /* whiteouted doubly */
38949+#define AUFS_WH_DIROPQ AUFS_WH_PFX AUFS_DIROPQ_NAME
38950+
38951+#define AUFS_BASE_NAME AUFS_WH_PFX AUFS_NAME
38952+#define AUFS_PLINKDIR_NAME AUFS_WH_PFX "plnk"
38953+#define AUFS_ORPHDIR_NAME AUFS_WH_PFX "orph"
38954+
38955+/* doubly whiteouted */
38956+#define AUFS_WH_BASE AUFS_WH_PFX AUFS_BASE_NAME
38957+#define AUFS_WH_PLINKDIR AUFS_WH_PFX AUFS_PLINKDIR_NAME
38958+#define AUFS_WH_ORPHDIR AUFS_WH_PFX AUFS_ORPHDIR_NAME
38959+
1e00d052 38960+/* branch permissions and attributes */
7f207e10
AM
38961+#define AUFS_BRPERM_RW "rw"
38962+#define AUFS_BRPERM_RO "ro"
38963+#define AUFS_BRPERM_RR "rr"
076b876e
AM
38964+#define AUFS_BRATTR_COO_REG "coo_reg"
38965+#define AUFS_BRATTR_COO_ALL "coo_all"
38966+#define AUFS_BRATTR_FHSM "fhsm"
38967+#define AUFS_BRATTR_UNPIN "unpin"
c1595e42
JR
38968+#define AUFS_BRATTR_ICEX "icex"
38969+#define AUFS_BRATTR_ICEX_SEC "icexsec"
38970+#define AUFS_BRATTR_ICEX_SYS "icexsys"
38971+#define AUFS_BRATTR_ICEX_TR "icextr"
38972+#define AUFS_BRATTR_ICEX_USR "icexusr"
38973+#define AUFS_BRATTR_ICEX_OTH "icexoth"
1e00d052
AM
38974+#define AUFS_BRRATTR_WH "wh"
38975+#define AUFS_BRWATTR_NLWH "nolwh"
076b876e
AM
38976+#define AUFS_BRWATTR_MOO "moo"
38977+
38978+#define AuBrPerm_RW 1 /* writable, hardlinkable wh */
38979+#define AuBrPerm_RO (1 << 1) /* readonly */
38980+#define AuBrPerm_RR (1 << 2) /* natively readonly */
38981+#define AuBrPerm_Mask (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
38982+
38983+#define AuBrAttr_COO_REG (1 << 3) /* copy-up on open */
38984+#define AuBrAttr_COO_ALL (1 << 4)
38985+#define AuBrAttr_COO_Mask (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
38986+
38987+#define AuBrAttr_FHSM (1 << 5) /* file-based hsm */
38988+#define AuBrAttr_UNPIN (1 << 6) /* rename-able top dir of
c1595e42
JR
38989+ branch. meaningless since
38990+ linux-3.18-rc1 */
38991+
38992+/* ignore error in copying XATTR */
38993+#define AuBrAttr_ICEX_SEC (1 << 7)
38994+#define AuBrAttr_ICEX_SYS (1 << 8)
38995+#define AuBrAttr_ICEX_TR (1 << 9)
38996+#define AuBrAttr_ICEX_USR (1 << 10)
38997+#define AuBrAttr_ICEX_OTH (1 << 11)
38998+#define AuBrAttr_ICEX (AuBrAttr_ICEX_SEC \
38999+ | AuBrAttr_ICEX_SYS \
39000+ | AuBrAttr_ICEX_TR \
39001+ | AuBrAttr_ICEX_USR \
39002+ | AuBrAttr_ICEX_OTH)
39003+
39004+#define AuBrRAttr_WH (1 << 12) /* whiteout-able */
076b876e
AM
39005+#define AuBrRAttr_Mask AuBrRAttr_WH
39006+
c1595e42
JR
39007+#define AuBrWAttr_NoLinkWH (1 << 13) /* un-hardlinkable whiteouts */
39008+#define AuBrWAttr_MOO (1 << 14) /* move-up on open */
076b876e
AM
39009+#define AuBrWAttr_Mask (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
39010+
39011+#define AuBrAttr_CMOO_Mask (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
39012+
c1595e42 39013+/* #warning test userspace */
076b876e
AM
39014+#ifdef __KERNEL__
39015+#ifndef CONFIG_AUFS_FHSM
39016+#undef AuBrAttr_FHSM
39017+#define AuBrAttr_FHSM 0
39018+#endif
c1595e42
JR
39019+#ifndef CONFIG_AUFS_XATTR
39020+#undef AuBrAttr_ICEX
39021+#define AuBrAttr_ICEX 0
39022+#undef AuBrAttr_ICEX_SEC
39023+#define AuBrAttr_ICEX_SEC 0
39024+#undef AuBrAttr_ICEX_SYS
39025+#define AuBrAttr_ICEX_SYS 0
39026+#undef AuBrAttr_ICEX_TR
39027+#define AuBrAttr_ICEX_TR 0
39028+#undef AuBrAttr_ICEX_USR
39029+#define AuBrAttr_ICEX_USR 0
39030+#undef AuBrAttr_ICEX_OTH
39031+#define AuBrAttr_ICEX_OTH 0
39032+#endif
076b876e
AM
39033+#endif
39034+
39035+/* the longest combination */
c1595e42
JR
39036+/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
39037+#define AuBrPermStrSz sizeof(AUFS_BRPERM_RW \
39038+ "+" AUFS_BRATTR_COO_REG \
39039+ "+" AUFS_BRATTR_FHSM \
39040+ "+" AUFS_BRATTR_UNPIN \
7e9cd9fe
AM
39041+ "+" AUFS_BRATTR_ICEX_SEC \
39042+ "+" AUFS_BRATTR_ICEX_SYS \
39043+ "+" AUFS_BRATTR_ICEX_USR \
39044+ "+" AUFS_BRATTR_ICEX_OTH \
076b876e
AM
39045+ "+" AUFS_BRWATTR_NLWH)
39046+
39047+typedef struct {
39048+ char a[AuBrPermStrSz];
39049+} au_br_perm_str_t;
39050+
39051+static inline int au_br_writable(int brperm)
39052+{
39053+ return brperm & AuBrPerm_RW;
39054+}
39055+
39056+static inline int au_br_whable(int brperm)
39057+{
39058+ return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
39059+}
39060+
39061+static inline int au_br_wh_linkable(int brperm)
39062+{
39063+ return !(brperm & AuBrWAttr_NoLinkWH);
39064+}
39065+
39066+static inline int au_br_cmoo(int brperm)
39067+{
39068+ return brperm & AuBrAttr_CMOO_Mask;
39069+}
39070+
39071+static inline int au_br_fhsm(int brperm)
39072+{
39073+ return brperm & AuBrAttr_FHSM;
39074+}
7f207e10
AM
39075+
39076+/* ---------------------------------------------------------------------- */
39077+
39078+/* ioctl */
39079+enum {
39080+ /* readdir in userspace */
39081+ AuCtl_RDU,
39082+ AuCtl_RDU_INO,
39083+
076b876e
AM
39084+ AuCtl_WBR_FD, /* pathconf wrapper */
39085+ AuCtl_IBUSY, /* busy inode */
39086+ AuCtl_MVDOWN, /* move-down */
39087+ AuCtl_BR, /* info about branches */
39088+ AuCtl_FHSM_FD /* connection for fhsm */
7f207e10
AM
39089+};
39090+
39091+/* borrowed from linux/include/linux/kernel.h */
39092+#ifndef ALIGN
0b487229 39093+#ifdef _GNU_SOURCE
7f207e10 39094+#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
0b487229
JR
39095+#else
39096+#define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
39097+#endif
7f207e10
AM
39098+#define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
39099+#endif
39100+
39101+/* borrowed from linux/include/linux/compiler-gcc3.h */
39102+#ifndef __aligned
39103+#define __aligned(x) __attribute__((aligned(x)))
53392da6
AM
39104+#endif
39105+
39106+#ifdef __KERNEL__
39107+#ifndef __packed
7f207e10
AM
39108+#define __packed __attribute__((packed))
39109+#endif
53392da6 39110+#endif
7f207e10
AM
39111+
39112+struct au_rdu_cookie {
9dbd164d
AM
39113+ uint64_t h_pos;
39114+ int16_t bindex;
39115+ uint8_t flags;
39116+ uint8_t pad;
39117+ uint32_t generation;
7f207e10
AM
39118+} __aligned(8);
39119+
39120+struct au_rdu_ent {
9dbd164d
AM
39121+ uint64_t ino;
39122+ int16_t bindex;
39123+ uint8_t type;
39124+ uint8_t nlen;
39125+ uint8_t wh;
0b487229 39126+ char name[];
7f207e10
AM
39127+} __aligned(8);
39128+
39129+static inline int au_rdu_len(int nlen)
39130+{
39131+ /* include the terminating NULL */
39132+ return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
9dbd164d 39133+ sizeof(uint64_t));
7f207e10
AM
39134+}
39135+
39136+union au_rdu_ent_ul {
39137+ struct au_rdu_ent __user *e;
9dbd164d 39138+ uint64_t ul;
7f207e10
AM
39139+};
39140+
39141+enum {
39142+ AufsCtlRduV_SZ,
39143+ AufsCtlRduV_End
39144+};
39145+
39146+struct aufs_rdu {
39147+ /* input */
39148+ union {
9dbd164d
AM
39149+ uint64_t sz; /* AuCtl_RDU */
39150+ uint64_t nent; /* AuCtl_RDU_INO */
7f207e10
AM
39151+ };
39152+ union au_rdu_ent_ul ent;
9dbd164d 39153+ uint16_t verify[AufsCtlRduV_End];
7f207e10
AM
39154+
39155+ /* input/output */
9dbd164d 39156+ uint32_t blk;
7f207e10
AM
39157+
39158+ /* output */
39159+ union au_rdu_ent_ul tail;
39160+ /* number of entries which were added in a single call */
9dbd164d
AM
39161+ uint64_t rent;
39162+ uint8_t full;
39163+ uint8_t shwh;
7f207e10
AM
39164+
39165+ struct au_rdu_cookie cookie;
39166+} __aligned(8);
39167+
1e00d052
AM
39168+/* ---------------------------------------------------------------------- */
39169+
8b6a4947
AM
39170+/* dirren. the branch is identified by the filename who contains this */
39171+struct au_drinfo {
39172+ uint64_t ino;
39173+ union {
39174+ uint8_t oldnamelen;
39175+ uint64_t _padding;
39176+ };
0b487229 39177+ uint8_t oldname[];
8b6a4947
AM
39178+} __aligned(8);
39179+
39180+struct au_drinfo_fdata {
39181+ uint32_t magic;
39182+ struct au_drinfo drinfo;
39183+} __aligned(8);
39184+
39185+#define AUFS_DRINFO_MAGIC_V1 ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x01)
39186+/* future */
39187+#define AUFS_DRINFO_MAGIC_V2 ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x02)
39188+
39189+/* ---------------------------------------------------------------------- */
39190+
1e00d052 39191+struct aufs_wbr_fd {
9dbd164d
AM
39192+ uint32_t oflags;
39193+ int16_t brid;
1e00d052
AM
39194+} __aligned(8);
39195+
39196+/* ---------------------------------------------------------------------- */
39197+
027c5e7a 39198+struct aufs_ibusy {
9dbd164d
AM
39199+ uint64_t ino, h_ino;
39200+ int16_t bindex;
027c5e7a
AM
39201+} __aligned(8);
39202+
1e00d052
AM
39203+/* ---------------------------------------------------------------------- */
39204+
392086de
AM
39205+/* error code for move-down */
39206+/* the actual message strings are implemented in aufs-util.git */
39207+enum {
39208+ EAU_MVDOWN_OPAQUE = 1,
39209+ EAU_MVDOWN_WHITEOUT,
39210+ EAU_MVDOWN_UPPER,
39211+ EAU_MVDOWN_BOTTOM,
39212+ EAU_MVDOWN_NOUPPER,
39213+ EAU_MVDOWN_NOLOWERBR,
39214+ EAU_Last
39215+};
39216+
c2b27bf2 39217+/* flags for move-down */
392086de
AM
39218+#define AUFS_MVDOWN_DMSG 1
39219+#define AUFS_MVDOWN_OWLOWER (1 << 1) /* overwrite lower */
39220+#define AUFS_MVDOWN_KUPPER (1 << 2) /* keep upper */
39221+#define AUFS_MVDOWN_ROLOWER (1 << 3) /* do even if lower is RO */
39222+#define AUFS_MVDOWN_ROLOWER_R (1 << 4) /* did on lower RO */
39223+#define AUFS_MVDOWN_ROUPPER (1 << 5) /* do even if upper is RO */
39224+#define AUFS_MVDOWN_ROUPPER_R (1 << 6) /* did on upper RO */
39225+#define AUFS_MVDOWN_BRID_UPPER (1 << 7) /* upper brid */
39226+#define AUFS_MVDOWN_BRID_LOWER (1 << 8) /* lower brid */
076b876e
AM
39227+#define AUFS_MVDOWN_FHSM_LOWER (1 << 9) /* find fhsm attr for lower */
39228+#define AUFS_MVDOWN_STFS (1 << 10) /* req. stfs */
39229+#define AUFS_MVDOWN_STFS_FAILED (1 << 11) /* output: stfs is unusable */
39230+#define AUFS_MVDOWN_BOTTOM (1 << 12) /* output: no more lowers */
c2b27bf2 39231+
076b876e 39232+/* index for move-down */
392086de
AM
39233+enum {
39234+ AUFS_MVDOWN_UPPER,
39235+ AUFS_MVDOWN_LOWER,
39236+ AUFS_MVDOWN_NARRAY
39237+};
39238+
076b876e
AM
39239+/*
39240+ * additional info of move-down
39241+ * number of free blocks and inodes.
39242+ * subset of struct kstatfs, but smaller and always 64bit.
39243+ */
39244+struct aufs_stfs {
39245+ uint64_t f_blocks;
39246+ uint64_t f_bavail;
39247+ uint64_t f_files;
39248+ uint64_t f_ffree;
39249+};
39250+
39251+struct aufs_stbr {
39252+ int16_t brid; /* optional input */
39253+ int16_t bindex; /* output */
39254+ struct aufs_stfs stfs; /* output when AUFS_MVDOWN_STFS set */
39255+} __aligned(8);
39256+
c2b27bf2 39257+struct aufs_mvdown {
076b876e
AM
39258+ uint32_t flags; /* input/output */
39259+ struct aufs_stbr stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
39260+ int8_t au_errno; /* output */
39261+} __aligned(8);
39262+
39263+/* ---------------------------------------------------------------------- */
39264+
39265+union aufs_brinfo {
39266+ /* PATH_MAX may differ between kernel-space and user-space */
39267+ char _spacer[4096];
392086de 39268+ struct {
076b876e
AM
39269+ int16_t id;
39270+ int perm;
0b487229 39271+ char path[];
076b876e 39272+ };
c2b27bf2
AM
39273+} __aligned(8);
39274+
39275+/* ---------------------------------------------------------------------- */
39276+
7f207e10
AM
39277+#define AuCtlType 'A'
39278+#define AUFS_CTL_RDU _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
39279+#define AUFS_CTL_RDU_INO _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
1e00d052
AM
39280+#define AUFS_CTL_WBR_FD _IOW(AuCtlType, AuCtl_WBR_FD, \
39281+ struct aufs_wbr_fd)
027c5e7a 39282+#define AUFS_CTL_IBUSY _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
392086de
AM
39283+#define AUFS_CTL_MVDOWN _IOWR(AuCtlType, AuCtl_MVDOWN, \
39284+ struct aufs_mvdown)
076b876e
AM
39285+#define AUFS_CTL_BRINFO _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
39286+#define AUFS_CTL_FHSM_FD _IOW(AuCtlType, AuCtl_FHSM_FD, int)
7f207e10
AM
39287+
39288+#endif /* __AUFS_TYPE_H__ */
2121bcd9 39289SPDX-License-Identifier: GPL-2.0
e49925d1 39290aufs4.19 loopback patch
5527c038
JR
39291
39292diff --git a/drivers/block/loop.c b/drivers/block/loop.c
0b487229 39293index 9e534a36c5941..74cd74e6374b0 100644
5527c038
JR
39294--- a/drivers/block/loop.c
39295+++ b/drivers/block/loop.c
cd7a4cd9 39296@@ -626,6 +626,15 @@ static inline void loop_update_dio(struct loop_device *lo)
8b6a4947 39297 lo->use_dio);
5527c038
JR
39298 }
39299
5527c038
JR
39300+static struct file *loop_real_file(struct file *file)
39301+{
39302+ struct file *f = NULL;
39303+
39304+ if (file->f_path.dentry->d_sb->s_op->real_loop)
39305+ f = file->f_path.dentry->d_sb->s_op->real_loop(file);
39306+ return f;
8b6a4947
AM
39307+}
39308+
c2c0f25c 39309 static void loop_reread_partitions(struct loop_device *lo,
8b6a4947
AM
39310 struct block_device *bdev)
39311 {
cd7a4cd9 39312@@ -690,6 +699,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
5527c038
JR
39313 unsigned int arg)
39314 {
e49925d1 39315 struct file *file = NULL, *old_file;
5527c038 39316+ struct file *f, *virt_file = NULL, *old_virt_file;
5527c038 39317 int error;
e49925d1 39318 bool partscan;
5527c038 39319
acd2b654 39320@@ -705,12 +715,19 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
cd7a4cd9
AM
39321 file = fget(arg);
39322 if (!file)
e49925d1 39323 goto out_err;
5527c038
JR
39324+ f = loop_real_file(file);
39325+ if (f) {
39326+ virt_file = file;
39327+ file = f;
39328+ get_file(file);
39329+ }
39330
cd7a4cd9
AM
39331 error = loop_validate_file(file, bdev);
39332 if (error)
e49925d1 39333 goto out_err;
cd7a4cd9 39334
5527c038
JR
39335 old_file = lo->lo_backing_file;
39336+ old_virt_file = lo->lo_backing_virt_file;
39337
39338 error = -EINVAL;
39339
acd2b654 39340@@ -722,6 +739,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
8b6a4947
AM
39341 blk_mq_freeze_queue(lo->lo_queue);
39342 mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
39343 lo->lo_backing_file = file;
39344+ lo->lo_backing_virt_file = virt_file;
39345 lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
39346 mapping_set_gfp_mask(file->f_mapping,
39347 lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
e49925d1
JR
39348@@ -729,14 +747,18 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39349 * dependency.
39350 */
5527c038
JR
39351 fput(old_file);
39352+ if (old_virt_file)
39353+ fput(old_virt_file);
e49925d1 39354 if (partscan)
c2c0f25c 39355 loop_reread_partitions(lo, bdev);
5527c038
JR
39356 return 0;
39357
e49925d1
JR
39358 out_err:
39359 mutex_unlock(&loop_ctl_mutex);
39360 if (file)
39361 fput(file);
5527c038
JR
39362+ if (virt_file)
39363+ fput(virt_file);
5527c038
JR
39364 return error;
39365 }
e49925d1 39366
acd2b654 39367@@ -922,7 +944,7 @@ static int loop_prepare_queue(struct loop_device *lo)
5527c038
JR
39368 static int loop_set_fd(struct loop_device *lo, fmode_t mode,
39369 struct block_device *bdev, unsigned int arg)
39370 {
062440b3 39371- struct file *file;
5527c038
JR
39372+ struct file *file, *f, *virt_file = NULL;
39373 struct inode *inode;
39374 struct address_space *mapping;
8b6a4947 39375 int lo_flags = 0;
acd2b654 39376@@ -936,6 +958,12 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
5527c038
JR
39377 file = fget(arg);
39378 if (!file)
39379 goto out;
39380+ f = loop_real_file(file);
39381+ if (f) {
39382+ virt_file = file;
39383+ file = f;
39384+ get_file(file);
39385+ }
39386
0b487229
JR
39387 error = mutex_lock_killable(&loop_ctl_mutex);
39388 if (error)
acd2b654 39389@@ -968,6 +996,7 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
5527c038
JR
39390 lo->lo_device = bdev;
39391 lo->lo_flags = lo_flags;
39392 lo->lo_backing_file = file;
39393+ lo->lo_backing_virt_file = virt_file;
39394 lo->transfer = NULL;
39395 lo->ioctl = NULL;
39396 lo->lo_sizelimit = 0;
acd2b654 39397@@ -1001,6 +1030,8 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
e49925d1
JR
39398 mutex_unlock(&loop_ctl_mutex);
39399 out_putf:
5527c038
JR
39400 fput(file);
39401+ if (virt_file)
39402+ fput(virt_file);
e49925d1 39403 out:
5527c038
JR
39404 /* This is safe: open() is still holding a reference. */
39405 module_put(THIS_MODULE);
acd2b654 39406@@ -1047,6 +1078,7 @@ loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
0b487229 39407 static int __loop_clr_fd(struct loop_device *lo, bool release)
5527c038 39408 {
e49925d1 39409 struct file *filp = NULL;
5527c038
JR
39410+ struct file *virt_filp = lo->lo_backing_virt_file;
39411 gfp_t gfp = lo->old_gfp_mask;
39412 struct block_device *bdev = lo->lo_device;
0b487229
JR
39413 int err = 0;
39414@@ -1078,6 +1110,7 @@ static int loop_clr_fd(struct loop_device *lo)
5527c038 39415
5527c038 39416 spin_lock_irq(&lo->lo_lock);
5527c038
JR
39417 lo->lo_backing_file = NULL;
39418+ lo->lo_backing_virt_file = NULL;
39419 spin_unlock_irq(&lo->lo_lock);
39420
39421 loop_release_xfer(lo);
0b487229 39422@@ -1126,6 +1159,8 @@ static int loop_clr_fd(struct loop_device *lo)
5527c038 39423 */
e49925d1
JR
39424 if (filp)
39425 fput(filp);
5527c038
JR
39426+ if (virt_filp)
39427+ fput(virt_filp);
e49925d1 39428 return err;
5527c038
JR
39429 }
39430
39431diff --git a/drivers/block/loop.h b/drivers/block/loop.h
0b487229 39432index 4d42c7af7de75..a15dd5bff5eae 100644
5527c038
JR
39433--- a/drivers/block/loop.h
39434+++ b/drivers/block/loop.h
39435@@ -46,7 +46,7 @@ struct loop_device {
39436 int (*ioctl)(struct loop_device *, int cmd,
39437 unsigned long arg);
39438
39439- struct file * lo_backing_file;
0b487229 39440+ struct file *lo_backing_file, *lo_backing_virt_file;
5527c038 39441 struct block_device *lo_device;
5527c038 39442 void *key_data;
8b6a4947 39443
5527c038 39444diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
0b487229 39445index 6fb4a4ed8cc7f..ba9a959f2db27 100644
5527c038
JR
39446--- a/fs/aufs/f_op.c
39447+++ b/fs/aufs/f_op.c
acd2b654 39448@@ -359,7 +359,7 @@ static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
5527c038
JR
39449 if (IS_ERR(h_file))
39450 goto out;
39451
39452- if (au_test_loopback_kthread()) {
39453+ if (0 && au_test_loopback_kthread()) {
39454 au_warn_loopback(h_file->f_path.dentry->d_sb);
39455 if (file->f_mapping != h_file->f_mapping) {
39456 file->f_mapping = h_file->f_mapping;
39457diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
0b487229 39458index 50e80bd1d589d..d0f9777267f99 100644
5527c038
JR
39459--- a/fs/aufs/loop.c
39460+++ b/fs/aufs/loop.c
2121bcd9 39461@@ -133,3 +133,19 @@ void au_loopback_fin(void)
79b8bda9 39462 symbol_put(loop_backing_file);
e49925d1 39463 au_kfree_try_rcu(au_warn_loopback_array);
5527c038
JR
39464 }
39465+
39466+/* ---------------------------------------------------------------------- */
39467+
39468+/* support the loopback block device insude aufs */
39469+
39470+struct file *aufs_real_loop(struct file *file)
39471+{
39472+ struct file *f;
39473+
39474+ BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
39475+ fi_read_lock(file);
39476+ f = au_hf_top(file);
39477+ fi_read_unlock(file);
39478+ AuDebugOn(!f);
39479+ return f;
39480+}
39481diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
0b487229 39482index 94f4f80ae33bf..ca1194354aff4 100644
5527c038
JR
39483--- a/fs/aufs/loop.h
39484+++ b/fs/aufs/loop.h
0b487229 39485@@ -26,6 +26,8 @@ void au_warn_loopback(struct super_block *h_sb);
5527c038
JR
39486
39487 int au_loopback_init(void);
39488 void au_loopback_fin(void);
39489+
39490+struct file *aufs_real_loop(struct file *file);
39491 #else
0b487229
JR
39492 AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
39493
39494@@ -36,6 +38,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
5527c038
JR
39495
39496 AuStubInt0(au_loopback_init, void)
39497 AuStubVoid(au_loopback_fin, void)
39498+
39499+AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
39500 #endif /* BLK_DEV_LOOP */
39501
39502 #endif /* __KERNEL__ */
39503diff --git a/fs/aufs/super.c b/fs/aufs/super.c
0b487229 39504index e3b81808a78a7..eff4f475de060 100644
5527c038
JR
39505--- a/fs/aufs/super.c
39506+++ b/fs/aufs/super.c
0b487229 39507@@ -846,7 +846,10 @@ static const struct super_operations aufs_sop = {
5527c038
JR
39508 .statfs = aufs_statfs,
39509 .put_super = aufs_put_super,
39510 .sync_fs = aufs_sync_fs,
39511- .remount_fs = aufs_remount_fs
39512+ .remount_fs = aufs_remount_fs,
39513+#ifdef CONFIG_AUFS_BDEV_LOOP
39514+ .real_loop = aufs_real_loop
39515+#endif
39516 };
39517
39518 /* ---------------------------------------------------------------------- */
39519diff --git a/include/linux/fs.h b/include/linux/fs.h
0b487229 39520index 41da4219febfe..72fe934fd55b0 100644
5527c038
JR
39521--- a/include/linux/fs.h
39522+++ b/include/linux/fs.h
acd2b654 39523@@ -1882,6 +1882,10 @@ struct super_operations {
5527c038
JR
39524 struct shrink_control *);
39525 long (*free_cached_objects)(struct super_block *,
39526 struct shrink_control *);
0b487229 39527+#if IS_ENABLED(CONFIG_BLK_DEV_LOOP) || IS_ENABLED(CONFIG_BLK_DEV_LOOP_MODULE)
5527c038
JR
39528+ /* and aufs */
39529+ struct file *(*real_loop)(struct file *);
39530+#endif
39531 };
39532
39533 /*
This page took 6.895676 seconds and 4 git commands to generate.