]> git.pld-linux.org Git - packages/kernel.git/blame - kernel-aufs.patch
backport fixes for gcc 13
[packages/kernel.git] / kernel-aufs.patch
CommitLineData
2121bcd9 1SPDX-License-Identifier: GPL-2.0
2255d0fe 2aufs6.x-rcN kbuild patch
7f207e10 3
fbc438ed 4diff --git a/fs/Kconfig b/fs/Kconfig
2255d0fe 5index 2685a4d0d353..a2df3d4bb3aa 100644
fbc438ed
JR
6--- a/fs/Kconfig
7+++ b/fs/Kconfig
2255d0fe 8@@ -319,6 +319,7 @@ source "fs/sysv/Kconfig"
7e9cd9fe 9 source "fs/ufs/Kconfig"
9d41a5bf 10 source "fs/erofs/Kconfig"
394e211a 11 source "fs/vboxsf/Kconfig"
7f207e10
AM
12+source "fs/aufs/Kconfig"
13
14 endif # MISC_FILESYSTEMS
15
16diff --git a/fs/Makefile b/fs/Makefile
2255d0fe 17index 4dea17840761..9795cbfdd0bf 100644
7f207e10
AM
18--- a/fs/Makefile
19+++ b/fs/Makefile
2255d0fe 20@@ -137,3 +137,4 @@ obj-$(CONFIG_EFIVAR_FS) += efivarfs/
9d41a5bf 21 obj-$(CONFIG_EROFS_FS) += erofs/
394e211a
AM
22 obj-$(CONFIG_VBOXSF_FS) += vboxsf/
23 obj-$(CONFIG_ZONEFS_FS) += zonefs/
86dc4139 24+obj-$(CONFIG_AUFS_FS) += aufs/
2121bcd9 25SPDX-License-Identifier: GPL-2.0
2255d0fe 26aufs6.x-rcN base patch
7f207e10 27
c1595e42 28diff --git a/MAINTAINERS b/MAINTAINERS
2255d0fe 29index 379945f82a64..b3b8fbca497b 100644
c1595e42
JR
30--- a/MAINTAINERS
31+++ b/MAINTAINERS
2255d0fe 32@@ -3455,6 +3455,19 @@ F: include/uapi/linux/audit.h
c1595e42 33 F: kernel/audit*
46016270 34 F: lib/*audit.c
c1595e42
JR
35
36+AUFS (advanced multi layered unification filesystem) FILESYSTEM
37+M: "J. R. Okajima" <hooanon05g@gmail.com>
c1595e42 38+L: aufs-users@lists.sourceforge.net (members only)
eca801bf 39+L: linux-unionfs@vger.kernel.org
42a736d3 40+S: Supported
c1595e42 41+W: http://aufs.sourceforge.net
5527c038 42+T: git://github.com/sfjro/aufs4-linux.git
c1595e42
JR
43+F: Documentation/ABI/testing/debugfs-aufs
44+F: Documentation/ABI/testing/sysfs-aufs
42a736d3 45+F: Documentation/filesystems/aufs/
c1595e42
JR
46+F: fs/aufs/
47+F: include/uapi/linux/aufs_type.h
48+
49 AUXILIARY DISPLAY DRIVERS
13f235e5 50 M: Miguel Ojeda <ojeda@kernel.org>
cd7a4cd9 51 S: Maintained
392086de 52diff --git a/drivers/block/loop.c b/drivers/block/loop.c
82da57cb 53index ad92192c7d61..8ca56da3ca69 100644
392086de
AM
54--- a/drivers/block/loop.c
55+++ b/drivers/block/loop.c
22319442
JP
56@@ -645,6 +645,24 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
57 goto done;
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
2255d0fe 82index 52e6d5fdab6b..519321f32f95 100644
c1595e42
JR
83--- a/fs/dcache.c
84+++ b/fs/dcache.c
f4d37d76 85@@ -1345,7 +1345,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
82da57cb 95index 146c9ab0cd4b..c980a0524118 100644
febd17d6
JR
96--- a/fs/fcntl.c
97+++ b/fs/fcntl.c
0b2a12c6 98@@ -33,7 +33,7 @@
febd17d6
JR
99
100 #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME)
101
102-static int setfl(int fd, struct file * filp, unsigned long arg)
43982f53 103+int setfl(int fd, struct file *filp, unsigned long arg)
febd17d6
JR
104 {
105 struct inode * inode = file_inode(filp);
106 int error = 0;
22319442 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
8b6a4947 116diff --git a/fs/namespace.c b/fs/namespace.c
82da57cb 117index df137ba19d37..faae7f8644ac 100644
8b6a4947
AM
118--- a/fs/namespace.c
119+++ b/fs/namespace.c
26eb093c 120@@ -852,6 +852,12 @@ static inline int check_mnt(struct mount *mnt)
8b6a4947
AM
121 return mnt->mnt_ns == current->nsproxy->mnt_ns;
122 }
123
124+/* for aufs, CONFIG_AUFS_BR_FUSE */
125+int is_current_mnt_ns(struct vfsmount *mnt)
126+{
127+ return check_mnt(real_mount(mnt));
128+}
129+
130 /*
131 * vfsmount lock must be held for write
132 */
7f207e10 133diff --git a/fs/splice.c b/fs/splice.c
82da57cb 134index 0878b852b355..d83f9e5cddbc 100644
7f207e10
AM
135--- a/fs/splice.c
136+++ b/fs/splice.c
82da57cb 137@@ -756,8 +756,8 @@ static int warn_unsupported(struct file *file, const char *op)
7f207e10
AM
138 /*
139 * Attempt to initiate a splice from pipe to file.
140 */
141-static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
142- loff_t *ppos, size_t len, unsigned int flags)
143+long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
144+ loff_t *ppos, size_t len, unsigned int flags)
145 {
42a736d3
AM
146 if (unlikely(!out->f_op->splice_write))
147 return warn_unsupported(out, "write");
82da57cb 148@@ -767,9 +767,9 @@ static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
7f207e10
AM
149 /*
150 * Attempt to initiate a splice from a file to a pipe.
151 */
152-static long do_splice_to(struct file *in, loff_t *ppos,
153- struct pipe_inode_info *pipe, size_t len,
154- unsigned int flags)
155+long do_splice_to(struct file *in, loff_t *ppos,
156+ struct pipe_inode_info *pipe, size_t len,
157+ unsigned int flags)
158 {
13f235e5 159 unsigned int p_space;
42a736d3 160 int ret;
5527c038 161diff --git a/include/linux/fs.h b/include/linux/fs.h
2255d0fe 162index e654435f1651..433a4f08dab4 100644
5527c038
JR
163--- a/include/linux/fs.h
164+++ b/include/linux/fs.h
82da57cb 165@@ -1364,6 +1364,7 @@ extern void fasync_free(struct fasync_struct *);
febd17d6
JR
166 /* can be called from interrupts */
167 extern void kill_fasync(struct fasync_struct **, int, int);
168
43982f53 169+extern int setfl(int fd, struct file *filp, unsigned long arg);
febd17d6 170 extern void __f_setown(struct file *filp, struct pid *, enum pid_type, int force);
3c1bdaff 171 extern int f_setown(struct file *filp, unsigned long arg, int force);
febd17d6 172 extern void f_delown(struct file *filp);
2255d0fe 173@@ -2117,6 +2118,7 @@ struct file_operations {
febd17d6
JR
174 ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
175 unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
176 int (*check_flags)(int);
177+ int (*setfl)(struct file *, unsigned long);
178 int (*flock) (struct file *, int, struct file_lock *);
179 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
180 ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
8b6a4947 181diff --git a/include/linux/lockdep.h b/include/linux/lockdep.h
82da57cb 182index 1f1099dac3f0..81d10b8e9dc0 100644
8b6a4947
AM
183--- a/include/linux/lockdep.h
184+++ b/include/linux/lockdep.h
82da57cb 185@@ -248,6 +248,8 @@ static inline int lockdep_match_key(struct lockdep_map *lock,
8b6a4947
AM
186 return lock->key == key;
187 }
188
189+struct lock_class *lockdep_hlock_class(struct held_lock *hlock);
190+
191 /*
192 * Acquire a lock.
193 *
8b6a4947 194diff --git a/include/linux/mnt_namespace.h b/include/linux/mnt_namespace.h
26eb093c 195index 8f882f5881e8..6b9808f09843 100644
8b6a4947
AM
196--- a/include/linux/mnt_namespace.h
197+++ b/include/linux/mnt_namespace.h
42a736d3 198@@ -7,12 +7,15 @@ struct mnt_namespace;
8b6a4947
AM
199 struct fs_struct;
200 struct user_namespace;
f24b0ddd 201 struct ns_common;
8b6a4947
AM
202+struct vfsmount;
203
204 extern struct mnt_namespace *copy_mnt_ns(unsigned long, struct mnt_namespace *,
205 struct user_namespace *, struct fs_struct *);
206 extern void put_mnt_ns(struct mnt_namespace *ns);
f24b0ddd 207 extern struct ns_common *from_mnt_ns(struct mnt_namespace *);
8b6a4947
AM
208
209+extern int is_current_mnt_ns(struct vfsmount *mnt);
210+
211 extern const struct file_operations proc_mounts_operations;
212 extern const struct file_operations proc_mountinfo_operations;
213 extern const struct file_operations proc_mountstats_operations;
1e00d052 214diff --git a/include/linux/splice.h b/include/linux/splice.h
26eb093c 215index a55179fd60fc..8e21c53cf883 100644
1e00d052
AM
216--- a/include/linux/splice.h
217+++ b/include/linux/splice.h
42a736d3 218@@ -93,4 +93,10 @@ extern void splice_shrink_spd(struct splice_pipe_desc *);
4b3da204
AM
219
220 extern const struct pipe_buf_operations page_cache_pipe_buf_ops;
106341ce 221 extern const struct pipe_buf_operations default_pipe_buf_ops;
1e00d052
AM
222+
223+extern long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
224+ loff_t *ppos, size_t len, unsigned int flags);
225+extern long do_splice_to(struct file *in, loff_t *ppos,
226+ struct pipe_inode_info *pipe, size_t len,
227+ unsigned int flags);
228 #endif
8b6a4947 229diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
2255d0fe 230index e3375bc40dad..c1f0aadde539 100644
8b6a4947
AM
231--- a/kernel/locking/lockdep.c
232+++ b/kernel/locking/lockdep.c
22319442 233@@ -217,7 +217,7 @@ unsigned long max_lock_class_idx;
9f237c51 234 struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
46cb2ab5 235 DECLARE_BITMAP(lock_classes_in_use, MAX_LOCKDEP_KEYS);
8b6a4947
AM
236
237-static inline struct lock_class *hlock_class(struct held_lock *hlock)
238+inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
239 {
eca34b5c
AM
240 unsigned int class_idx = hlock->class_idx;
241
22319442 242@@ -238,6 +238,7 @@ static inline struct lock_class *hlock_class(struct held_lock *hlock)
eca34b5c
AM
243 */
244 return lock_classes + class_idx;
8b6a4947
AM
245 }
246+#define hlock_class(hlock) lockdep_hlock_class(hlock)
247
248 #ifdef CONFIG_LOCK_STAT
249 static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
2121bcd9 250SPDX-License-Identifier: GPL-2.0
2255d0fe 251aufs6.x-rcN mmap patch
fb47a38f 252
c1595e42 253diff --git a/fs/proc/base.c b/fs/proc/base.c
2255d0fe 254index 9e479d7d202b..986c2ae12f8b 100644
c1595e42
JR
255--- a/fs/proc/base.c
256+++ b/fs/proc/base.c
82da57cb 257@@ -2218,7 +2218,7 @@ static int map_files_get_link(struct dentry *dentry, struct path *path)
eca34b5c 258 rc = -ENOENT;
c1595e42
JR
259 vma = find_exact_vma(mm, vm_start, vm_end);
260 if (vma && vma->vm_file) {
261- *path = vma->vm_file->f_path;
262+ *path = vma_pr_or_file(vma)->f_path;
263 path_get(path);
264 rc = 0;
265 }
fb47a38f 266diff --git a/fs/proc/nommu.c b/fs/proc/nommu.c
82da57cb 267index 4d3493579458..42edd9a42c78 100644
fb47a38f
JR
268--- a/fs/proc/nommu.c
269+++ b/fs/proc/nommu.c
82da57cb 270@@ -39,7 +39,10 @@ static int nommu_region_show(struct seq_file *m, struct vm_region *region)
fb47a38f
JR
271 file = region->vm_file;
272
273 if (file) {
274- struct inode *inode = file_inode(region->vm_file);
275+ struct inode *inode;
076b876e 276+
fb47a38f
JR
277+ file = vmr_pr_or_file(region);
278+ inode = file_inode(file);
279 dev = inode->i_sb->s_dev;
280 ino = inode->i_ino;
281 }
282diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
2255d0fe 283index 8a74cdcc9af0..c05071b53f5b 100644
fb47a38f
JR
284--- a/fs/proc/task_mmu.c
285+++ b/fs/proc/task_mmu.c
2255d0fe 286@@ -284,7 +284,10 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
fb47a38f
JR
287 const char *name = NULL;
288
289 if (file) {
290- struct inode *inode = file_inode(vma->vm_file);
291+ struct inode *inode;
076b876e 292+
fb47a38f
JR
293+ file = vma_pr_or_file(vma);
294+ inode = file_inode(file);
295 dev = inode->i_sb->s_dev;
296 ino = inode->i_ino;
297 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
2255d0fe 298@@ -1934,7 +1937,7 @@ static int show_numa_map(struct seq_file *m, void *v)
076b876e
AM
299 struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
300 struct vm_area_struct *vma = v;
301 struct numa_maps *md = &numa_priv->md;
302- struct file *file = vma->vm_file;
303+ struct file *file = vma_pr_or_file(vma);
076b876e 304 struct mm_struct *mm = vma->vm_mm;
43982f53
AM
305 struct mempolicy *pol;
306 char buffer[64];
fb47a38f 307diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c
2255d0fe 308index 2fd06f52b6a4..b01030da424f 100644
fb47a38f
JR
309--- a/fs/proc/task_nommu.c
310+++ b/fs/proc/task_nommu.c
2255d0fe 311@@ -150,7 +150,10 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma)
fb47a38f
JR
312 file = vma->vm_file;
313
314 if (file) {
315- struct inode *inode = file_inode(vma->vm_file);
316+ struct inode *inode;
076b876e 317+
b912730e 318+ file = vma_pr_or_file(vma);
fb47a38f
JR
319+ inode = file_inode(file);
320 dev = inode->i_sb->s_dev;
321 ino = inode->i_ino;
322 pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
323diff --git a/include/linux/mm.h b/include/linux/mm.h
2255d0fe 324index 8bbcccbc5565..d5006a1f4271 100644
fb47a38f
JR
325--- a/include/linux/mm.h
326+++ b/include/linux/mm.h
2255d0fe 327@@ -1933,6 +1933,43 @@ static inline void unmap_shared_mapping_range(struct address_space *mapping,
a3232e84 328 unmap_mapping_range(mapping, holebegin, holelen, 0);
fb47a38f 329 }
fb47a38f 330
26eb093c 331+#if IS_ENABLED(CONFIG_AUFS_FS)
076b876e
AM
332+extern void vma_do_file_update_time(struct vm_area_struct *, const char[], int);
333+extern struct file *vma_do_pr_or_file(struct vm_area_struct *, const char[],
f2c43d5f 334+ int);
076b876e
AM
335+extern void vma_do_get_file(struct vm_area_struct *, const char[], int);
336+extern void vma_do_fput(struct vm_area_struct *, const char[], int);
fb47a38f 337+
f2c43d5f
AM
338+#define vma_file_update_time(vma) vma_do_file_update_time(vma, __func__, \
339+ __LINE__)
340+#define vma_pr_or_file(vma) vma_do_pr_or_file(vma, __func__, \
341+ __LINE__)
342+#define vma_get_file(vma) vma_do_get_file(vma, __func__, __LINE__)
343+#define vma_fput(vma) vma_do_fput(vma, __func__, __LINE__)
b912730e
AM
344+
345+#ifndef CONFIG_MMU
076b876e
AM
346+extern struct file *vmr_do_pr_or_file(struct vm_region *, const char[], int);
347+extern void vmr_do_fput(struct vm_region *, const char[], int);
348+
f2c43d5f
AM
349+#define vmr_pr_or_file(region) vmr_do_pr_or_file(region, __func__, \
350+ __LINE__)
351+#define vmr_fput(region) vmr_do_fput(region, __func__, __LINE__)
b912730e 352+#endif /* !CONFIG_MMU */
26eb093c
AM
353+
354+#else
355+
356+#define vma_file_update_time(vma) file_update_time((vma)->vm_file)
357+#define vma_pr_or_file(vma) (vma)->vm_file
358+#define vma_get_file(vma) get_file((vma)->vm_file)
359+#define vma_fput(vma) fput((vma)->vm_file)
360+
361+#ifndef CONFIG_MMU
362+#define vmr_pr_or_file(region) (region)->vm_file
363+#define vmr_fput(region) fput((region)->vm_file)
364+#endif /* !CONFIG_MMU */
365+
366+#endif /* CONFIG_AUFS_FS */
fb47a38f 367+
a3232e84
JR
368 extern int access_process_vm(struct task_struct *tsk, unsigned long addr,
369 void *buf, int len, unsigned int gup_flags);
fb47a38f 370 extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
fb47a38f 371diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
2255d0fe 372index 500e536796ca..ad766e2110ca 100644
fb47a38f
JR
373--- a/include/linux/mm_types.h
374+++ b/include/linux/mm_types.h
2255d0fe 375@@ -413,6 +413,9 @@ struct vm_region {
fb47a38f
JR
376 unsigned long vm_top; /* region allocated to here */
377 unsigned long vm_pgoff; /* the offset in vm_file corresponding to vm_start */
378 struct file *vm_file; /* the backing file or NULL */
26eb093c 379+#if IS_ENABLED(CONFIG_AUFS_FS)
fb47a38f 380+ struct file *vm_prfile; /* the virtual backing file or NULL */
26eb093c 381+#endif
fb47a38f
JR
382
383 int vm_usage; /* region usage count (access under nommu_region_sem) */
384 bool vm_icache_flushed : 1; /* true if the icache has been flushed for
2255d0fe 385@@ -494,6 +497,9 @@ struct vm_area_struct {
fb47a38f 386 unsigned long vm_pgoff; /* Offset (within vm_file) in PAGE_SIZE
5afbbe0d 387 units */
fb47a38f 388 struct file * vm_file; /* File we map to (can be NULL). */
26eb093c 389+#if IS_ENABLED(CONFIG_AUFS_FS)
fb47a38f 390+ struct file *vm_prfile; /* shadow of vm_file */
26eb093c 391+#endif
fb47a38f
JR
392 void * vm_private_data; /* was vm_pte (shared mem) */
393
eca34b5c 394 #ifdef CONFIG_SWAP
fb47a38f 395diff --git a/kernel/fork.c b/kernel/fork.c
2255d0fe 396index 08969f5aa38d..61871700033a 100644
fb47a38f
JR
397--- a/kernel/fork.c
398+++ b/kernel/fork.c
2255d0fe 399@@ -661,7 +661,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
758e9dad 400 if (file) {
fb47a38f
JR
401 struct address_space *mapping = file->f_mapping;
402
403- get_file(file);
404+ vma_get_file(tmp);
2000de60 405 i_mmap_lock_write(mapping);
758e9dad
AM
406 if (tmp->vm_flags & VM_SHARED)
407 mapping_allow_writable(mapping);
076b876e 408diff --git a/mm/Makefile b/mm/Makefile
2255d0fe 409index 8e105e5b3e29..c218d042dbce 100644
076b876e
AM
410--- a/mm/Makefile
411+++ b/mm/Makefile
2255d0fe 412@@ -138,3 +138,4 @@ obj-$(CONFIG_IO_MAPPING) += io-mapping.o
26eb093c
AM
413 obj-$(CONFIG_HAVE_BOOTMEM_INFO_NODE) += bootmem_info.o
414 obj-$(CONFIG_GENERIC_IOREMAP) += ioremap.o
82da57cb 415 obj-$(CONFIG_SHRINKER_DEBUG) += shrinker_debug.o
26eb093c 416+obj-$(CONFIG_AUFS_FS:m=y) += prfile.o
fb47a38f 417diff --git a/mm/filemap.c b/mm/filemap.c
2255d0fe 418index 08341616ae7a..410ca60fbf39 100644
fb47a38f
JR
419--- a/mm/filemap.c
420+++ b/mm/filemap.c
2255d0fe 421@@ -3418,7 +3418,7 @@ vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
cd7a4cd9 422 vm_fault_t ret = VM_FAULT_LOCKED;
fb47a38f 423
42a736d3 424 sb_start_pagefault(mapping->host->i_sb);
521ced18
JR
425- file_update_time(vmf->vma->vm_file);
426+ vma_file_update_time(vmf->vma);
f4d37d76
AM
427 folio_lock(folio);
428 if (folio->mapping != mapping) {
429 folio_unlock(folio);
fb47a38f 430diff --git a/mm/mmap.c b/mm/mmap.c
2255d0fe 431index 2def55555e05..fefc5c67e96e 100644
fb47a38f
JR
432--- a/mm/mmap.c
433+++ b/mm/mmap.c
2255d0fe 434@@ -139,7 +139,7 @@ static void remove_vma(struct vm_area_struct *vma)
fb47a38f
JR
435 if (vma->vm_ops && vma->vm_ops->close)
436 vma->vm_ops->close(vma);
437 if (vma->vm_file)
438- fput(vma->vm_file);
439+ vma_fput(vma);
440 mpol_put(vma_policy(vma));
cd7a4cd9 441 vm_area_free(vma);
2255d0fe
AM
442 }
443@@ -590,7 +590,7 @@ inline int vma_expand(struct ma_state *mas, struct vm_area_struct *vma,
fb47a38f
JR
444 if (remove_next) {
445 if (file) {
446 uprobe_munmap(next, next->vm_start, next->vm_end);
447- fput(file);
448+ vma_fput(vma);
449 }
450 if (next->anon_vma)
451 anon_vma_merge(vma, next);
2255d0fe
AM
452@@ -831,7 +831,7 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
453 again:
454 if (file) {
455 uprobe_munmap(next, next->vm_start, next->vm_end);
456- fput(file);
457+ vma_fput(vma);
458 }
459 if (next->anon_vma)
460 anon_vma_merge(vma, next);
461@@ -2228,7 +2228,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
fb47a38f
JR
462 goto out_free_mpol;
463
464 if (new->vm_file)
465- get_file(new->vm_file);
466+ vma_get_file(new);
467
468 if (new->vm_ops && new->vm_ops->open)
469 new->vm_ops->open(new);
2255d0fe 470@@ -2250,7 +2250,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
fb47a38f
JR
471 if (new->vm_ops && new->vm_ops->close)
472 new->vm_ops->close(new);
473 if (new->vm_file)
474- fput(new->vm_file);
475+ vma_fput(new);
476 unlink_anon_vmas(new);
477 out_free_mpol:
478 mpol_put(vma_policy(new));
2255d0fe
AM
479@@ -2746,7 +2746,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
480 if (vma->vm_ops && vma->vm_ops->close)
481 vma->vm_ops->close(vma);
482 unmap_and_free_vma:
483- fput(vma->vm_file);
484+ vma_fput(vma);
485 vma->vm_file = NULL;
486
487 /* Undo any partial mapping done by a device driver. */
488@@ -2813,6 +2813,9 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
7e9cd9fe
AM
489 unsigned long populate = 0;
490 unsigned long ret = -EINVAL;
26eb093c
AM
491 struct file *file;
492+#if IS_ENABLED(CONFIG_AUFS_FS)
493+ struct file *prfile;
494+#endif
7e9cd9fe 495
82da57cb 496 pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/mm/remap_file_pages.rst.\n",
5afbbe0d 497 current->comm, current->pid);
2255d0fe 498@@ -2871,10 +2874,34 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
46016270
AM
499 if (vma->vm_flags & VM_LOCKED)
500 flags |= MAP_LOCKED;
7e9cd9fe 501
26eb093c 502+#if IS_ENABLED(CONFIG_AUFS_FS)
7e9cd9fe 503+ vma_get_file(vma);
5afbbe0d
AM
504+ file = vma->vm_file;
505+ prfile = vma->vm_prfile;
26eb093c
AM
506+ ret = do_mmap(vma->vm_file, start, size,
507+ prot, flags, pgoff, &populate, NULL);
5afbbe0d
AM
508+ if (!IS_ERR_VALUE(ret) && file && prfile) {
509+ struct vm_area_struct *new_vma;
510+
511+ new_vma = find_vma(mm, ret);
512+ if (!new_vma->vm_prfile)
513+ new_vma->vm_prfile = prfile;
514+ if (new_vma != vma)
515+ get_file(prfile);
516+ }
517+ /*
518+ * two fput()s instead of vma_fput(vma),
519+ * coz vma may not be available anymore.
520+ */
26eb093c 521+ fput(file);
5afbbe0d
AM
522+ if (prfile)
523+ fput(prfile);
26eb093c
AM
524+#else
525 file = get_file(vma->vm_file);
526 ret = do_mmap(vma->vm_file, start, size,
527 prot, flags, pgoff, &populate, NULL);
528 fput(file);
529+#endif /* CONFIG_AUFS_FS */
7e9cd9fe 530 out:
42a736d3 531 mmap_write_unlock(mm);
7e9cd9fe 532 if (populate)
2255d0fe 533@@ -3227,7 +3254,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
79b8bda9
AM
534 if (anon_vma_clone(new_vma, vma))
535 goto out_free_mempol;
536 if (new_vma->vm_file)
537- get_file(new_vma->vm_file);
538+ vma_get_file(new_vma);
539 if (new_vma->vm_ops && new_vma->vm_ops->open)
540 new_vma->vm_ops->open(new_vma);
2255d0fe 541 if (vma_link(mm, new_vma))
fb47a38f 542diff --git a/mm/nommu.c b/mm/nommu.c
2255d0fe 543index 214c70e1d059..dccbb939d2a9 100644
fb47a38f
JR
544--- a/mm/nommu.c
545+++ b/mm/nommu.c
2255d0fe 546@@ -523,7 +523,7 @@ static void __put_nommu_region(struct vm_region *region)
fb47a38f
JR
547 up_write(&nommu_region_sem);
548
549 if (region->vm_file)
550- fput(region->vm_file);
551+ vmr_fput(region);
552
553 /* IO memory and memory shared directly out of the pagecache
554 * from ramfs/tmpfs mustn't be released here */
2255d0fe 555@@ -653,7 +653,7 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
fb47a38f
JR
556 if (vma->vm_ops && vma->vm_ops->close)
557 vma->vm_ops->close(vma);
558 if (vma->vm_file)
559- fput(vma->vm_file);
560+ vma_fput(vma);
561 put_nommu_region(vma->vm_region);
cd7a4cd9 562 vm_area_free(vma);
fb47a38f 563 }
2255d0fe 564@@ -1164,7 +1164,7 @@ unsigned long do_mmap(struct file *file,
fb47a38f
JR
565 goto error_just_free;
566 }
567 }
568- fput(region->vm_file);
569+ vmr_fput(region);
570 kmem_cache_free(vm_region_jar, region);
571 region = pregion;
572 result = start;
2255d0fe 573@@ -1241,10 +1241,10 @@ unsigned long do_mmap(struct file *file,
fb47a38f 574 error:
8ba2a77d 575 mas_destroy(&mas);
fb47a38f
JR
576 if (region->vm_file)
577- fput(region->vm_file);
578+ vmr_fput(region);
579 kmem_cache_free(vm_region_jar, region);
580 if (vma->vm_file)
eca801bf
AM
581- fput(vma->vm_file);
582+ vma_fput(vma);
583 vm_area_free(vma);
584 return ret;
585
076b876e
AM
586diff --git a/mm/prfile.c b/mm/prfile.c
587new file mode 100644
0dcfbb52 588index 000000000000..8f820a235364
076b876e
AM
589--- /dev/null
590+++ b/mm/prfile.c
2121bcd9 591@@ -0,0 +1,86 @@
cd7a4cd9 592+// SPDX-License-Identifier: GPL-2.0
076b876e 593+/*
1c60b727
AM
594+ * Mainly for aufs which mmap(2) different file and wants to print different
595+ * path in /proc/PID/maps.
076b876e
AM
596+ * Call these functions via macros defined in linux/mm.h.
597+ *
598+ * See Documentation/filesystems/aufs/design/06mmap.txt
599+ *
0dcfbb52 600+ * Copyright (c) 2014-2022 Junjro R. Okajima
076b876e
AM
601+ * Copyright (c) 2014 Ian Campbell
602+ */
603+
604+#include <linux/mm.h>
605+#include <linux/file.h>
606+#include <linux/fs.h>
607+
608+/* #define PRFILE_TRACE */
609+static inline void prfile_trace(struct file *f, struct file *pr,
610+ const char func[], int line, const char func2[])
611+{
612+#ifdef PRFILE_TRACE
613+ if (pr)
1c60b727 614+ pr_info("%s:%d: %s, %pD2\n", func, line, func2, f);
076b876e
AM
615+#endif
616+}
617+
076b876e
AM
618+void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
619+ int line)
620+{
621+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
622+
623+ prfile_trace(f, pr, func, line, __func__);
624+ file_update_time(f);
625+ if (f && pr)
626+ file_update_time(pr);
627+}
628+
629+struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
630+ int line)
631+{
632+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
633+
634+ prfile_trace(f, pr, func, line, __func__);
635+ return (f && pr) ? pr : f;
636+}
637+
638+void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
639+{
640+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
641+
642+ prfile_trace(f, pr, func, line, __func__);
643+ get_file(f);
644+ if (f && pr)
645+ get_file(pr);
646+}
647+
648+void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
649+{
650+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
651+
652+ prfile_trace(f, pr, func, line, __func__);
653+ fput(f);
654+ if (f && pr)
655+ fput(pr);
656+}
b912730e
AM
657+
658+#ifndef CONFIG_MMU
076b876e
AM
659+struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
660+ int line)
661+{
662+ struct file *f = region->vm_file, *pr = region->vm_prfile;
663+
664+ prfile_trace(f, pr, func, line, __func__);
665+ return (f && pr) ? pr : f;
666+}
667+
668+void vmr_do_fput(struct vm_region *region, const char func[], int line)
669+{
670+ struct file *f = region->vm_file, *pr = region->vm_prfile;
671+
672+ prfile_trace(f, pr, func, line, __func__);
673+ fput(f);
674+ if (f && pr)
675+ fput(pr);
676+}
b912730e 677+#endif /* !CONFIG_MMU */
2121bcd9 678SPDX-License-Identifier: GPL-2.0
2255d0fe 679aufs6.x-rcN standalone patch
7f207e10 680
c1595e42 681diff --git a/fs/dcache.c b/fs/dcache.c
2255d0fe 682index 519321f32f95..267e0c65914a 100644
c1595e42
JR
683--- a/fs/dcache.c
684+++ b/fs/dcache.c
f4d37d76 685@@ -1450,6 +1450,7 @@ void d_walk(struct dentry *parent, void *data,
c1595e42
JR
686 seq = 1;
687 goto again;
688 }
febd17d6 689+EXPORT_SYMBOL_GPL(d_walk);
c1595e42 690
a2654f78
AM
691 struct check_mount {
692 struct vfsmount *mnt;
2255d0fe 693@@ -3052,6 +3053,7 @@ void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
f2c43d5f
AM
694
695 write_sequnlock(&rename_lock);
696 }
697+EXPORT_SYMBOL_GPL(d_exchange);
698
699 /**
700 * d_ancestor - search for an ancestor
79b8bda9 701diff --git a/fs/exec.c b/fs/exec.c
2255d0fe 702index a0b1f0337a62..2038fdb508c9 100644
79b8bda9
AM
703--- a/fs/exec.c
704+++ b/fs/exec.c
2255d0fe 705@@ -110,6 +110,7 @@ bool path_noexec(const struct path *path)
79b8bda9
AM
706 return (path->mnt->mnt_flags & MNT_NOEXEC) ||
707 (path->mnt->mnt_sb->s_iflags & SB_I_NOEXEC);
708 }
febd17d6 709+EXPORT_SYMBOL_GPL(path_noexec);
79b8bda9
AM
710
711 #ifdef CONFIG_USELIB
712 /*
febd17d6 713diff --git a/fs/fcntl.c b/fs/fcntl.c
82da57cb 714index c980a0524118..40087d11a974 100644
febd17d6
JR
715--- a/fs/fcntl.c
716+++ b/fs/fcntl.c
82da57cb 717@@ -86,6 +86,7 @@ int setfl(int fd, struct file *filp, unsigned long arg)
febd17d6
JR
718 out:
719 return error;
720 }
721+EXPORT_SYMBOL_GPL(setfl);
722
723 static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
724 int force)
b912730e 725diff --git a/fs/file_table.c b/fs/file_table.c
2255d0fe 726index dd88701e54a9..ecdb7817b074 100644
b912730e
AM
727--- a/fs/file_table.c
728+++ b/fs/file_table.c
f4d37d76 729@@ -198,6 +198,7 @@ struct file *alloc_empty_file(int flags, const struct cred *cred)
b912730e
AM
730 }
731 return ERR_PTR(-ENFILE);
732 }
acd2b654 733+EXPORT_SYMBOL_GPL(alloc_empty_file);
b912730e 734
acd2b654
AM
735 /*
736 * Variant of alloc_empty_file() that doesn't check and modify nr_files.
7f207e10 737diff --git a/fs/namespace.c b/fs/namespace.c
82da57cb 738index faae7f8644ac..0310c99a83dd 100644
7f207e10
AM
739--- a/fs/namespace.c
740+++ b/fs/namespace.c
26eb093c 741@@ -456,6 +456,7 @@ void __mnt_drop_write(struct vfsmount *mnt)
c06a8ce3
AM
742 mnt_dec_writers(real_mount(mnt));
743 preempt_enable();
744 }
745+EXPORT_SYMBOL_GPL(__mnt_drop_write);
746
747 /**
748 * mnt_drop_write - give up write access to a mount
26eb093c 749@@ -857,6 +858,7 @@ int is_current_mnt_ns(struct vfsmount *mnt)
8b6a4947
AM
750 {
751 return check_mnt(real_mount(mnt));
752 }
753+EXPORT_SYMBOL_GPL(is_current_mnt_ns);
754
755 /*
756 * vfsmount lock must be held for write
26eb093c 757@@ -2060,6 +2062,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
7f207e10
AM
758 }
759 return 0;
760 }
febd17d6 761+EXPORT_SYMBOL_GPL(iterate_mounts);
7f207e10 762
fbc438ed 763 static void lock_mnt_tree(struct mount *mnt)
7f207e10 764 {
43982f53 765diff --git a/fs/notify/group.c b/fs/notify/group.c
22319442 766index 1de6631a3925..3008eb37a18d 100644
43982f53
AM
767--- a/fs/notify/group.c
768+++ b/fs/notify/group.c
42a736d3 769@@ -100,6 +100,7 @@ void fsnotify_get_group(struct fsnotify_group *group)
1716fcea 770 {
2121bcd9 771 refcount_inc(&group->refcnt);
1716fcea 772 }
febd17d6 773+EXPORT_SYMBOL_GPL(fsnotify_get_group);
1716fcea
AM
774
775 /*
776 * Drop a reference to a group. Free it if it's through.
7f207e10 777diff --git a/fs/open.c b/fs/open.c
2255d0fe 778index a81319b6177f..50bb15ea3fb5 100644
7f207e10
AM
779--- a/fs/open.c
780+++ b/fs/open.c
f4d37d76 781@@ -66,6 +66,7 @@ int do_truncate(struct user_namespace *mnt_userns, struct dentry *dentry,
febd17d6 782 inode_unlock(dentry->d_inode);
7f207e10
AM
783 return ret;
784 }
febd17d6 785+EXPORT_SYMBOL_GPL(do_truncate);
7f207e10 786
5afbbe0d 787 long vfs_truncate(const struct path *path, loff_t length)
7f207e10 788 {
5527c038 789diff --git a/fs/read_write.c b/fs/read_write.c
0dcfbb52 790index 328ce8cf9a85..59931848f794 100644
5527c038
JR
791--- a/fs/read_write.c
792+++ b/fs/read_write.c
82da57cb 793@@ -477,6 +477,7 @@ ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
42a736d3 794 inc_syscr(current);
8b6a4947
AM
795 return ret;
796 }
797+EXPORT_SYMBOL_GPL(vfs_read);
798
799 static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
800 {
0dcfbb52 801@@ -592,6 +593,7 @@ ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_
42a736d3 802 file_end_write(file);
8b6a4947
AM
803 return ret;
804 }
805+EXPORT_SYMBOL_GPL(vfs_write);
806
fbc438ed
JR
807 /* file_ppos returns &file->f_pos or NULL if file is stream */
808 static inline loff_t *file_ppos(struct file *file)
7f207e10 809diff --git a/fs/splice.c b/fs/splice.c
82da57cb 810index d83f9e5cddbc..ac355ebeb9f0 100644
7f207e10
AM
811--- a/fs/splice.c
812+++ b/fs/splice.c
82da57cb 813@@ -763,6 +763,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
42a736d3
AM
814 return warn_unsupported(out, "write");
815 return out->f_op->splice_write(pipe, out, ppos, len, flags);
7f207e10 816 }
febd17d6 817+EXPORT_SYMBOL_GPL(do_splice_from);
7f207e10
AM
818
819 /*
820 * Attempt to initiate a splice from a file to a pipe.
82da57cb 821@@ -792,6 +793,7 @@ long do_splice_to(struct file *in, loff_t *ppos,
42a736d3
AM
822 return warn_unsupported(in, "read");
823 return in->f_op->splice_read(in, ppos, pipe, len, flags);
7f207e10 824 }
febd17d6 825+EXPORT_SYMBOL_GPL(do_splice_to);
7f207e10
AM
826
827 /**
828 * splice_direct_to_actor - splices data directly between two non-pipes
c1595e42 829diff --git a/fs/xattr.c b/fs/xattr.c
2255d0fe 830index 61107b6bbed2..243855024966 100644
c1595e42
JR
831--- a/fs/xattr.c
832+++ b/fs/xattr.c
2255d0fe 833@@ -392,6 +392,7 @@ vfs_getxattr_alloc(struct user_namespace *mnt_userns, struct dentry *dentry,
c1595e42
JR
834 *xattr_value = value;
835 return error;
836 }
febd17d6 837+EXPORT_SYMBOL_GPL(vfs_getxattr_alloc);
c1595e42 838
febd17d6 839 ssize_t
f2c43d5f 840 __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
8b6a4947 841diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
2255d0fe 842index c1f0aadde539..278e0a322d20 100644
8b6a4947
AM
843--- a/kernel/locking/lockdep.c
844+++ b/kernel/locking/lockdep.c
22319442 845@@ -238,6 +238,7 @@ inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
eca34b5c
AM
846 */
847 return lock_classes + class_idx;
8b6a4947
AM
848 }
849+EXPORT_SYMBOL_GPL(lockdep_hlock_class);
850 #define hlock_class(hlock) lockdep_hlock_class(hlock)
851
852 #ifdef CONFIG_LOCK_STAT
8cdd5066 853diff --git a/kernel/task_work.c b/kernel/task_work.c
2255d0fe 854index 065e1ef8fc8d..c623c6f0c645 100644
8cdd5066
JR
855--- a/kernel/task_work.c
856+++ b/kernel/task_work.c
2255d0fe 857@@ -182,3 +182,4 @@ void task_work_run(void)
8cdd5066
JR
858 } while (work);
859 }
860 }
febd17d6 861+EXPORT_SYMBOL_GPL(task_work_run);
7f207e10 862diff --git a/security/security.c b/security/security.c
2255d0fe 863index 79d82cb6e469..1c50e6faadcd 100644
7f207e10
AM
864--- a/security/security.c
865+++ b/security/security.c
2255d0fe 866@@ -1167,6 +1167,7 @@ int security_path_rmdir(const struct path *dir, struct dentry *dentry)
7f207e10 867 return 0;
c2c0f25c 868 return call_int_hook(path_rmdir, 0, dir, dentry);
7f207e10 869 }
febd17d6 870+EXPORT_SYMBOL_GPL(security_path_rmdir);
7f207e10 871
5afbbe0d 872 int security_path_unlink(const struct path *dir, struct dentry *dentry)
7f207e10 873 {
2255d0fe 874@@ -1183,6 +1184,7 @@ int security_path_symlink(const struct path *dir, struct dentry *dentry,
7f207e10 875 return 0;
c2c0f25c 876 return call_int_hook(path_symlink, 0, dir, dentry, old_name);
7f207e10 877 }
febd17d6 878+EXPORT_SYMBOL_GPL(security_path_symlink);
7f207e10 879
5afbbe0d 880 int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
7f207e10 881 struct dentry *new_dentry)
2255d0fe 882@@ -1191,6 +1193,7 @@ int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
7f207e10 883 return 0;
c2c0f25c 884 return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
7f207e10 885 }
febd17d6 886+EXPORT_SYMBOL_GPL(security_path_link);
7f207e10 887
5afbbe0d
AM
888 int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
889 const struct path *new_dir, struct dentry *new_dentry,
2255d0fe 890@@ -1211,6 +1214,7 @@ int security_path_truncate(const struct path *path)
7f207e10 891 return 0;
c2c0f25c 892 return call_int_hook(path_truncate, 0, path);
7f207e10 893 }
febd17d6 894+EXPORT_SYMBOL_GPL(security_path_truncate);
7f207e10 895
5afbbe0d 896 int security_path_chmod(const struct path *path, umode_t mode)
7eafdf33 897 {
2255d0fe 898@@ -1218,6 +1222,7 @@ int security_path_chmod(const struct path *path, umode_t mode)
7f207e10 899 return 0;
c2c0f25c 900 return call_int_hook(path_chmod, 0, path, mode);
7f207e10 901 }
febd17d6 902+EXPORT_SYMBOL_GPL(security_path_chmod);
7f207e10 903
5afbbe0d 904 int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
7f207e10 905 {
2255d0fe 906@@ -1225,6 +1230,7 @@ int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
7f207e10 907 return 0;
c2c0f25c 908 return call_int_hook(path_chown, 0, path, uid, gid);
7f207e10 909 }
febd17d6 910+EXPORT_SYMBOL_GPL(security_path_chown);
7f207e10 911
5afbbe0d 912 int security_path_chroot(const struct path *path)
7f207e10 913 {
2255d0fe 914@@ -1325,6 +1331,7 @@ int security_inode_permission(struct inode *inode, int mask)
7f207e10 915 return 0;
c2c0f25c 916 return call_int_hook(inode_permission, 0, inode, mask);
7f207e10 917 }
febd17d6 918+EXPORT_SYMBOL_GPL(security_inode_permission);
7f207e10 919
82da57cb
AM
920 int security_inode_setattr(struct user_namespace *mnt_userns,
921 struct dentry *dentry, struct iattr *attr)
2255d0fe 922@@ -1523,6 +1530,7 @@ int security_file_permission(struct file *file, int mask)
7f207e10
AM
923
924 return fsnotify_perm(file, mask);
925 }
febd17d6 926+EXPORT_SYMBOL_GPL(security_file_permission);
7f207e10
AM
927
928 int security_file_alloc(struct file *file)
929 {
7f207e10 930diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
eca34b5c 931--- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 932+++ linux/Documentation/ABI/testing/debugfs-aufs 2022-11-05 23:02:18.955889283 +0100
062440b3 933@@ -0,0 +1,55 @@
7f207e10
AM
934+What: /debug/aufs/si_<id>/
935+Date: March 2009
f6b6e03d 936+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
937+Description:
938+ Under /debug/aufs, a directory named si_<id> is created
939+ per aufs mount, where <id> is a unique id generated
940+ internally.
1facf9fc 941+
86dc4139
AM
942+What: /debug/aufs/si_<id>/plink
943+Date: Apr 2013
f6b6e03d 944+Contact: J. R. Okajima <hooanon05g@gmail.com>
86dc4139
AM
945+Description:
946+ It has three lines and shows the information about the
947+ pseudo-link. The first line is a single number
948+ representing a number of buckets. The second line is a
949+ number of pseudo-links per buckets (separated by a
950+ blank). The last line is a single number representing a
951+ total number of psedo-links.
952+ When the aufs mount option 'noplink' is specified, it
953+ will show "1\n0\n0\n".
954+
7f207e10
AM
955+What: /debug/aufs/si_<id>/xib
956+Date: March 2009
f6b6e03d 957+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
958+Description:
959+ It shows the consumed blocks by xib (External Inode Number
960+ Bitmap), its block size and file size.
961+ When the aufs mount option 'noxino' is specified, it
962+ will be empty. About XINO files, see the aufs manual.
963+
0b2a12c6 964+What: /debug/aufs/si_<id>/xi<branch-index>
7f207e10 965+Date: March 2009
f6b6e03d 966+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
967+Description:
968+ It shows the consumed blocks by xino (External Inode Number
969+ Translation Table), its link count, block size and file
970+ size.
062440b3
AM
971+ Due to the file size limit, there may exist multiple
972+ xino files per branch. In this case, "-N" is added to
973+ the filename and it corresponds to the index of the
974+ internal xino array. "-0" is omitted.
975+ When the aufs mount option 'noxino' is specified, Those
976+ entries won't exist. About XINO files, see the aufs
977+ manual.
7f207e10
AM
978+
979+What: /debug/aufs/si_<id>/xigen
980+Date: March 2009
f6b6e03d 981+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
982+Description:
983+ It shows the consumed blocks by xigen (External Inode
984+ Generation Table), its block size and file size.
985+ If CONFIG_AUFS_EXPORT is disabled, this entry will not
986+ be created.
987+ When the aufs mount option 'noxino' is specified, it
988+ will be empty. About XINO files, see the aufs manual.
989diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
eca34b5c 990--- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 991+++ linux/Documentation/ABI/testing/sysfs-aufs 2022-11-05 23:02:18.955889283 +0100
392086de 992@@ -0,0 +1,31 @@
7f207e10
AM
993+What: /sys/fs/aufs/si_<id>/
994+Date: March 2009
f6b6e03d 995+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
996+Description:
997+ Under /sys/fs/aufs, a directory named si_<id> is created
998+ per aufs mount, where <id> is a unique id generated
999+ internally.
1000+
0b2a12c6 1001+What: /sys/fs/aufs/si_<id>/br<idx>
7f207e10 1002+Date: March 2009
f6b6e03d 1003+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1004+Description:
1005+ It shows the abolute path of a member directory (which
1006+ is called branch) in aufs, and its permission.
1007+
0b2a12c6 1008+What: /sys/fs/aufs/si_<id>/brid<idx>
392086de 1009+Date: July 2013
f6b6e03d 1010+Contact: J. R. Okajima <hooanon05g@gmail.com>
392086de
AM
1011+Description:
1012+ It shows the id of a member directory (which is called
1013+ branch) in aufs.
1014+
7f207e10
AM
1015+What: /sys/fs/aufs/si_<id>/xi_path
1016+Date: March 2009
f6b6e03d 1017+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1018+Description:
1019+ It shows the abolute path of XINO (External Inode Number
1020+ Bitmap, Translation Table and Generation Table) file
1021+ even if it is the default path.
1022+ When the aufs mount option 'noxino' is specified, it
1023+ will be empty. About XINO files, see the aufs manual.
53392da6 1024diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
eca34b5c 1025--- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 1026+++ linux/Documentation/filesystems/aufs/design/01intro.txt 2022-11-05 23:02:18.955889283 +0100
1c60b727 1027@@ -0,0 +1,171 @@
53392da6 1028+
0dcfbb52
AM
1029+# Copyright (C) 2005-2022 Junjiro R. Okajima
1030+#
53392da6
AM
1031+# This program is free software; you can redistribute it and/or modify
1032+# it under the terms of the GNU General Public License as published by
1033+# the Free Software Foundation; either version 2 of the License, or
1034+# (at your option) any later version.
0dcfbb52 1035+#
53392da6
AM
1036+# This program is distributed in the hope that it will be useful,
1037+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1038+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1039+# GNU General Public License for more details.
0dcfbb52 1040+#
53392da6 1041+# You should have received a copy of the GNU General Public License
523b37e3 1042+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1043+
1044+Introduction
1045+----------------------------------------
1046+
3c1bdaff 1047+aufs [ei ju: ef es] | /ey-yoo-ef-es/ | [a u f s]
53392da6
AM
1048+1. abbrev. for "advanced multi-layered unification filesystem".
1049+2. abbrev. for "another unionfs".
1050+3. abbrev. for "auf das" in German which means "on the" in English.
1051+ Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
1052+ But "Filesystem aufs Filesystem" is hard to understand.
1c60b727 1053+4. abbrev. for "African Urban Fashion Show".
53392da6
AM
1054+
1055+AUFS is a filesystem with features:
1056+- multi layered stackable unification filesystem, the member directory
1057+ is called as a branch.
1058+- branch permission and attribute, 'readonly', 'real-readonly',
7e9cd9fe 1059+ 'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
53392da6
AM
1060+ combination.
1061+- internal "file copy-on-write".
1062+- logical deletion, whiteout.
1063+- dynamic branch manipulation, adding, deleting and changing permission.
1064+- allow bypassing aufs, user's direct branch access.
1065+- external inode number translation table and bitmap which maintains the
1066+ persistent aufs inode number.
1067+- seekable directory, including NFS readdir.
1068+- file mapping, mmap and sharing pages.
1069+- pseudo-link, hardlink over branches.
1070+- loopback mounted filesystem as a branch.
1071+- several policies to select one among multiple writable branches.
1072+- revert a single systemcall when an error occurs in aufs.
1073+- and more...
1074+
1075+
1076+Multi Layered Stackable Unification Filesystem
1077+----------------------------------------------------------------------
1078+Most people already knows what it is.
1079+It is a filesystem which unifies several directories and provides a
1080+merged single directory. When users access a file, the access will be
1081+passed/re-directed/converted (sorry, I am not sure which English word is
1082+correct) to the real file on the member filesystem. The member
1083+filesystem is called 'lower filesystem' or 'branch' and has a mode
1084+'readonly' and 'readwrite.' And the deletion for a file on the lower
1085+readonly branch is handled by creating 'whiteout' on the upper writable
1086+branch.
1087+
1088+On LKML, there have been discussions about UnionMount (Jan Blunck,
1089+Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
1090+different approaches to implement the merged-view.
1091+The former tries putting it into VFS, and the latter implements as a
1092+separate filesystem.
1093+(If I misunderstand about these implementations, please let me know and
1094+I shall correct it. Because it is a long time ago when I read their
1095+source files last time).
1096+
1097+UnionMount's approach will be able to small, but may be hard to share
1098+branches between several UnionMount since the whiteout in it is
1099+implemented in the inode on branch filesystem and always
1100+shared. According to Bharata's post, readdir does not seems to be
1101+finished yet.
1102+There are several missing features known in this implementations such as
1103+- for users, the inode number may change silently. eg. copy-up.
1104+- link(2) may break by copy-up.
1105+- read(2) may get an obsoleted filedata (fstat(2) too).
1106+- fcntl(F_SETLK) may be broken by copy-up.
1107+- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
1108+ open(O_RDWR).
1109+
7e9cd9fe
AM
1110+In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
1111+merged into mainline. This is another implementation of UnionMount as a
1112+separated filesystem. All the limitations and known problems which
1113+UnionMount are equally inherited to "overlay" filesystem.
1114+
1115+Unionfs has a longer history. When I started implementing a stackable
1116+filesystem (Aug 2005), it already existed. It has virtual super_block,
1117+inode, dentry and file objects and they have an array pointing lower
1118+same kind objects. After contributing many patches for Unionfs, I
1119+re-started my project AUFS (Jun 2006).
53392da6
AM
1120+
1121+In AUFS, the structure of filesystem resembles to Unionfs, but I
1122+implemented my own ideas, approaches and enhancements and it became
1123+totally different one.
1124+
1125+Comparing DM snapshot and fs based implementation
1126+- the number of bytes to be copied between devices is much smaller.
1127+- the type of filesystem must be one and only.
1128+- the fs must be writable, no readonly fs, even for the lower original
1129+ device. so the compression fs will not be usable. but if we use
1130+ loopback mount, we may address this issue.
1131+ for instance,
1132+ mount /cdrom/squashfs.img /sq
1133+ losetup /sq/ext2.img
1134+ losetup /somewhere/cow
1135+ dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1136+- it will be difficult (or needs more operations) to extract the
1137+ difference between the original device and COW.
1138+- DM snapshot-merge may help a lot when users try merging. in the
1139+ fs-layer union, users will use rsync(1).
1140+
7e9cd9fe
AM
1141+You may want to read my old paper "Filesystems in LiveCD"
1142+(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
53392da6 1143+
7e9cd9fe
AM
1144+
1145+Several characters/aspects/persona of aufs
53392da6
AM
1146+----------------------------------------------------------------------
1147+
7e9cd9fe 1148+Aufs has several characters, aspects or persona.
53392da6
AM
1149+1. a filesystem, callee of VFS helper
1150+2. sub-VFS, caller of VFS helper for branches
1151+3. a virtual filesystem which maintains persistent inode number
1152+4. reader/writer of files on branches such like an application
1153+
1154+1. Callee of VFS Helper
1155+As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1156+unlink(2) from an application reaches sys_unlink() kernel function and
1157+then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1158+calls filesystem specific unlink operation. Actually aufs implements the
1159+unlink operation but it behaves like a redirector.
1160+
1161+2. Caller of VFS Helper for Branches
1162+aufs_unlink() passes the unlink request to the branch filesystem as if
1163+it were called from VFS. So the called unlink operation of the branch
1164+filesystem acts as usual. As a caller of VFS helper, aufs should handle
1165+every necessary pre/post operation for the branch filesystem.
1166+- acquire the lock for the parent dir on a branch
1167+- lookup in a branch
1168+- revalidate dentry on a branch
1169+- mnt_want_write() for a branch
1170+- vfs_unlink() for a branch
1171+- mnt_drop_write() for a branch
1172+- release the lock on a branch
1173+
1174+3. Persistent Inode Number
1175+One of the most important issue for a filesystem is to maintain inode
1176+numbers. This is particularly important to support exporting a
1177+filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1178+backend block device for its own. But some storage is necessary to
7e9cd9fe
AM
1179+keep and maintain the inode numbers. It may be a large space and may not
1180+suit to keep in memory. Aufs rents some space from its first writable
1181+branch filesystem (by default) and creates file(s) on it. These files
1182+are created by aufs internally and removed soon (currently) keeping
1183+opened.
53392da6
AM
1184+Note: Because these files are removed, they are totally gone after
1185+ unmounting aufs. It means the inode numbers are not persistent
1186+ across unmount or reboot. I have a plan to make them really
1187+ persistent which will be important for aufs on NFS server.
1188+
1189+4. Read/Write Files Internally (copy-on-write)
1190+Because a branch can be readonly, when you write a file on it, aufs will
1191+"copy-up" it to the upper writable branch internally. And then write the
1192+originally requested thing to the file. Generally kernel doesn't
1193+open/read/write file actively. In aufs, even a single write may cause a
1194+internal "file copy". This behaviour is very similar to cp(1) command.
1195+
1196+Some people may think it is better to pass such work to user space
1197+helper, instead of doing in kernel space. Actually I am still thinking
1198+about it. But currently I have implemented it in kernel space.
1199diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
eca34b5c 1200--- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 1201+++ linux/Documentation/filesystems/aufs/design/02struct.txt 2022-11-05 23:02:18.955889283 +0100
7e9cd9fe 1202@@ -0,0 +1,258 @@
53392da6 1203+
0dcfbb52
AM
1204+# Copyright (C) 2005-2022 Junjiro R. Okajima
1205+#
53392da6
AM
1206+# This program is free software; you can redistribute it and/or modify
1207+# it under the terms of the GNU General Public License as published by
1208+# the Free Software Foundation; either version 2 of the License, or
1209+# (at your option) any later version.
0dcfbb52 1210+#
53392da6
AM
1211+# This program is distributed in the hope that it will be useful,
1212+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1213+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1214+# GNU General Public License for more details.
0dcfbb52 1215+#
53392da6 1216+# You should have received a copy of the GNU General Public License
523b37e3 1217+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1218+
1219+Basic Aufs Internal Structure
1220+
1221+Superblock/Inode/Dentry/File Objects
1222+----------------------------------------------------------------------
1223+As like an ordinary filesystem, aufs has its own
1224+superblock/inode/dentry/file objects. All these objects have a
1225+dynamically allocated array and store the same kind of pointers to the
1226+lower filesystem, branch.
1227+For example, when you build a union with one readwrite branch and one
1228+readonly, mounted /au, /rw and /ro respectively.
1229+- /au = /rw + /ro
1230+- /ro/fileA exists but /rw/fileA
1231+
1232+Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1233+pointers are stored in a aufs dentry. The array in aufs dentry will be,
7e9cd9fe 1234+- [0] = NULL (because /rw/fileA doesn't exist)
53392da6
AM
1235+- [1] = /ro/fileA
1236+
1237+This style of an array is essentially same to the aufs
1238+superblock/inode/dentry/file objects.
1239+
1240+Because aufs supports manipulating branches, ie. add/delete/change
7e9cd9fe
AM
1241+branches dynamically, these objects has its own generation. When
1242+branches are changed, the generation in aufs superblock is
1243+incremented. And a generation in other object are compared when it is
1244+accessed. When a generation in other objects are obsoleted, aufs
1245+refreshes the internal array.
53392da6
AM
1246+
1247+
1248+Superblock
1249+----------------------------------------------------------------------
1250+Additionally aufs superblock has some data for policies to select one
1251+among multiple writable branches, XIB files, pseudo-links and kobject.
1252+See below in detail.
7e9cd9fe
AM
1253+About the policies which supports copy-down a directory, see
1254+wbr_policy.txt too.
53392da6
AM
1255+
1256+
1257+Branch and XINO(External Inode Number Translation Table)
1258+----------------------------------------------------------------------
1259+Every branch has its own xino (external inode number translation table)
1260+file. The xino file is created and unlinked by aufs internally. When two
1261+members of a union exist on the same filesystem, they share the single
1262+xino file.
1263+The struct of a xino file is simple, just a sequence of aufs inode
1264+numbers which is indexed by the lower inode number.
1265+In the above sample, assume the inode number of /ro/fileA is i111 and
1266+aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1267+4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1268+
1269+When the inode numbers are not contiguous, the xino file will be sparse
1270+which has a hole in it and doesn't consume as much disk space as it
1271+might appear. If your branch filesystem consumes disk space for such
1272+holes, then you should specify 'xino=' option at mounting aufs.
1273+
7e9cd9fe
AM
1274+Aufs has a mount option to free the disk blocks for such holes in XINO
1275+files on tmpfs or ramdisk. But it is not so effective actually. If you
1276+meet a problem of disk shortage due to XINO files, then you should try
1277+"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1278+The patch localizes the assignment inumbers per tmpfs-mount and avoid
1279+the holes in XINO files.
1280+
53392da6 1281+Also a writable branch has three kinds of "whiteout bases". All these
7e9cd9fe 1282+are existed when the branch is joined to aufs, and their names are
53392da6
AM
1283+whiteout-ed doubly, so that users will never see their names in aufs
1284+hierarchy.
7e9cd9fe 1285+1. a regular file which will be hardlinked to all whiteouts.
53392da6 1286+2. a directory to store a pseudo-link.
7e9cd9fe 1287+3. a directory to store an "orphan"-ed file temporary.
53392da6
AM
1288+
1289+1. Whiteout Base
1290+ When you remove a file on a readonly branch, aufs handles it as a
1291+ logical deletion and creates a whiteout on the upper writable branch
1292+ as a hardlink of this file in order not to consume inode on the
1293+ writable branch.
1294+2. Pseudo-link Dir
1295+ See below, Pseudo-link.
1296+3. Step-Parent Dir
1297+ When "fileC" exists on the lower readonly branch only and it is
1298+ opened and removed with its parent dir, and then user writes
1299+ something into it, then aufs copies-up fileC to this
1300+ directory. Because there is no other dir to store fileC. After
1301+ creating a file under this dir, the file is unlinked.
1302+
1303+Because aufs supports manipulating branches, ie. add/delete/change
7e9cd9fe
AM
1304+dynamically, a branch has its own id. When the branch order changes,
1305+aufs finds the new index by searching the branch id.
53392da6
AM
1306+
1307+
1308+Pseudo-link
1309+----------------------------------------------------------------------
1310+Assume "fileA" exists on the lower readonly branch only and it is
1311+hardlinked to "fileB" on the branch. When you write something to fileA,
1312+aufs copies-up it to the upper writable branch. Additionally aufs
1313+creates a hardlink under the Pseudo-link Directory of the writable
1314+branch. The inode of a pseudo-link is kept in aufs super_block as a
1315+simple list. If fileB is read after unlinking fileA, aufs returns
1316+filedata from the pseudo-link instead of the lower readonly
1317+branch. Because the pseudo-link is based upon the inode, to keep the
7e9cd9fe 1318+inode number by xino (see above) is essentially necessary.
53392da6
AM
1319+
1320+All the hardlinks under the Pseudo-link Directory of the writable branch
1321+should be restored in a proper location later. Aufs provides a utility
1322+to do this. The userspace helpers executed at remounting and unmounting
1323+aufs by default.
1324+During this utility is running, it puts aufs into the pseudo-link
1325+maintenance mode. In this mode, only the process which began the
1326+maintenance mode (and its child processes) is allowed to operate in
1327+aufs. Some other processes which are not related to the pseudo-link will
1328+be allowed to run too, but the rest have to return an error or wait
1329+until the maintenance mode ends. If a process already acquires an inode
1330+mutex (in VFS), it has to return an error.
1331+
1332+
1333+XIB(external inode number bitmap)
1334+----------------------------------------------------------------------
1335+Addition to the xino file per a branch, aufs has an external inode number
7e9cd9fe
AM
1336+bitmap in a superblock object. It is also an internal file such like a
1337+xino file.
53392da6
AM
1338+It is a simple bitmap to mark whether the aufs inode number is in-use or
1339+not.
1340+To reduce the file I/O, aufs prepares a single memory page to cache xib.
1341+
7e9cd9fe 1342+As well as XINO files, aufs has a feature to truncate/refresh XIB to
53392da6
AM
1343+reduce the number of consumed disk blocks for these files.
1344+
1345+
1346+Virtual or Vertical Dir, and Readdir in Userspace
1347+----------------------------------------------------------------------
1348+In order to support multiple layers (branches), aufs readdir operation
1349+constructs a virtual dir block on memory. For readdir, aufs calls
1350+vfs_readdir() internally for each dir on branches, merges their entries
1351+with eliminating the whiteout-ed ones, and sets it to file (dir)
1352+object. So the file object has its entry list until it is closed. The
1353+entry list will be updated when the file position is zero and becomes
7e9cd9fe 1354+obsoleted. This decision is made in aufs automatically.
53392da6
AM
1355+
1356+The dynamically allocated memory block for the name of entries has a
1357+unit of 512 bytes (by default) and stores the names contiguously (no
1358+padding). Another block for each entry is handled by kmem_cache too.
1359+During building dir blocks, aufs creates hash list and judging whether
1360+the entry is whiteouted by its upper branch or already listed.
1361+The merged result is cached in the corresponding inode object and
1362+maintained by a customizable life-time option.
1363+
1364+Some people may call it can be a security hole or invite DoS attack
1365+since the opened and once readdir-ed dir (file object) holds its entry
1366+list and becomes a pressure for system memory. But I'd say it is similar
1367+to files under /proc or /sys. The virtual files in them also holds a
1368+memory page (generally) while they are opened. When an idea to reduce
1369+memory for them is introduced, it will be applied to aufs too.
1370+For those who really hate this situation, I've developed readdir(3)
1371+library which operates this merging in userspace. You just need to set
1372+LD_PRELOAD environment variable, and aufs will not consume no memory in
1373+kernel space for readdir(3).
1374+
1375+
1376+Workqueue
1377+----------------------------------------------------------------------
1378+Aufs sometimes requires privilege access to a branch. For instance,
1379+in copy-up/down operation. When a user process is going to make changes
1380+to a file which exists in the lower readonly branch only, and the mode
1381+of one of ancestor directories may not be writable by a user
1382+process. Here aufs copy-up the file with its ancestors and they may
1383+require privilege to set its owner/group/mode/etc.
1384+This is a typical case of a application character of aufs (see
1385+Introduction).
1386+
1387+Aufs uses workqueue synchronously for this case. It creates its own
1388+workqueue. The workqueue is a kernel thread and has privilege. Aufs
1389+passes the request to call mkdir or write (for example), and wait for
1390+its completion. This approach solves a problem of a signal handler
1391+simply.
1392+If aufs didn't adopt the workqueue and changed the privilege of the
7e9cd9fe
AM
1393+process, then the process may receive the unexpected SIGXFSZ or other
1394+signals.
53392da6
AM
1395+
1396+Also aufs uses the system global workqueue ("events" kernel thread) too
1397+for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1398+whiteout base and etc. This is unrelated to a privilege.
1399+Most of aufs operation tries acquiring a rw_semaphore for aufs
1400+superblock at the beginning, at the same time waits for the completion
1401+of all queued asynchronous tasks.
1402+
1403+
1404+Whiteout
1405+----------------------------------------------------------------------
1406+The whiteout in aufs is very similar to Unionfs's. That is represented
1407+by its filename. UnionMount takes an approach of a file mode, but I am
1408+afraid several utilities (find(1) or something) will have to support it.
1409+
1410+Basically the whiteout represents "logical deletion" which stops aufs to
1411+lookup further, but also it represents "dir is opaque" which also stop
7e9cd9fe 1412+further lookup.
53392da6
AM
1413+
1414+In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1415+In order to make several functions in a single systemcall to be
1416+revertible, aufs adopts an approach to rename a directory to a temporary
1417+unique whiteouted name.
1418+For example, in rename(2) dir where the target dir already existed, aufs
1419+renames the target dir to a temporary unique whiteouted name before the
7e9cd9fe 1420+actual rename on a branch, and then handles other actions (make it opaque,
53392da6
AM
1421+update the attributes, etc). If an error happens in these actions, aufs
1422+simply renames the whiteouted name back and returns an error. If all are
1423+succeeded, aufs registers a function to remove the whiteouted unique
1424+temporary name completely and asynchronously to the system global
1425+workqueue.
1426+
1427+
1428+Copy-up
1429+----------------------------------------------------------------------
1430+It is a well-known feature or concept.
1431+When user modifies a file on a readonly branch, aufs operate "copy-up"
1432+internally and makes change to the new file on the upper writable branch.
1433+When the trigger systemcall does not update the timestamps of the parent
1434+dir, aufs reverts it after copy-up.
c2b27bf2
AM
1435+
1436+
1437+Move-down (aufs3.9 and later)
1438+----------------------------------------------------------------------
1439+"Copy-up" is one of the essential feature in aufs. It copies a file from
1440+the lower readonly branch to the upper writable branch when a user
1441+changes something about the file.
1442+"Move-down" is an opposite action of copy-up. Basically this action is
1443+ran manually instead of automatically and internally.
076b876e
AM
1444+For desgin and implementation, aufs has to consider these issues.
1445+- whiteout for the file may exist on the lower branch.
1446+- ancestor directories may not exist on the lower branch.
1447+- diropq for the ancestor directories may exist on the upper branch.
1448+- free space on the lower branch will reduce.
1449+- another access to the file may happen during moving-down, including
7e9cd9fe 1450+ UDBA (see "Revalidate Dentry and UDBA").
076b876e
AM
1451+- the file should not be hard-linked nor pseudo-linked. they should be
1452+ handled by auplink utility later.
c2b27bf2
AM
1453+
1454+Sometimes users want to move-down a file from the upper writable branch
1455+to the lower readonly or writable branch. For instance,
1456+- the free space of the upper writable branch is going to run out.
1457+- create a new intermediate branch between the upper and lower branch.
1458+- etc.
1459+
1460+For this purpose, use "aumvdown" command in aufs-util.git.
b912730e 1461diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt linux/Documentation/filesystems/aufs/design/03atomic_open.txt
eca34b5c 1462--- /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 1463+++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt 2022-11-05 23:02:18.955889283 +0100
b912730e
AM
1464@@ -0,0 +1,85 @@
1465+
0dcfbb52
AM
1466+# Copyright (C) 2015-2022 Junjiro R. Okajima
1467+#
b912730e
AM
1468+# This program is free software; you can redistribute it and/or modify
1469+# it under the terms of the GNU General Public License as published by
1470+# the Free Software Foundation; either version 2 of the License, or
1471+# (at your option) any later version.
0dcfbb52 1472+#
b912730e
AM
1473+# This program is distributed in the hope that it will be useful,
1474+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1475+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1476+# GNU General Public License for more details.
0dcfbb52 1477+#
b912730e
AM
1478+# You should have received a copy of the GNU General Public License
1479+# along with this program. If not, see <http://www.gnu.org/licenses/>.
1480+
1481+Support for a branch who has its ->atomic_open()
1482+----------------------------------------------------------------------
1483+The filesystems who implement its ->atomic_open() are not majority. For
1484+example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
1485+particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
1486+->atomic_open(), NFSv4 returns an error for this open(2). While I am not
1487+sure whether all filesystems who have ->atomic_open() behave like this,
1488+but NFSv4 surely returns the error.
1489+
1490+In order to support ->atomic_open() for aufs, there are a few
1491+approaches.
1492+
1493+A. Introduce aufs_atomic_open()
1494+ - calls one of VFS:do_last(), lookup_open() or atomic_open() for
1495+ branch fs.
1496+B. Introduce aufs_atomic_open() calling create, open and chmod. this is
1497+ an aufs user Pip Cet's approach
1498+ - calls aufs_create(), VFS finish_open() and notify_change().
1499+ - pass fake-mode to finish_open(), and then correct the mode by
1500+ notify_change().
1501+C. Extend aufs_open() to call branch fs's ->atomic_open()
1502+ - no aufs_atomic_open().
1503+ - aufs_lookup() registers the TID to an aufs internal object.
1504+ - aufs_create() does nothing when the matching TID is registered, but
1505+ registers the mode.
1506+ - aufs_open() calls branch fs's ->atomic_open() when the matching
1507+ TID is registered.
1508+D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
1509+ credential
1510+ - no aufs_atomic_open().
1511+ - aufs_create() registers the TID to an internal object. this info
1512+ represents "this process created this file just now."
1513+ - when aufs gets EACCES from branch fs's ->open(), then confirm the
1514+ registered TID and re-try open() with superuser's credential.
1515+
1516+Pros and cons for each approach.
1517+
1518+A.
1519+ - straightforward but highly depends upon VFS internal.
1520+ - the atomic behavaiour is kept.
1521+ - some of parameters such as nameidata are hard to reproduce for
1522+ branch fs.
1523+ - large overhead.
1524+B.
1525+ - easy to implement.
1526+ - the atomic behavaiour is lost.
1527+C.
1528+ - the atomic behavaiour is kept.
1529+ - dirty and tricky.
1530+ - VFS checks whether the file is created correctly after calling
1531+ ->create(), which means this approach doesn't work.
1532+D.
1533+ - easy to implement.
1534+ - the atomic behavaiour is lost.
1535+ - to open a file with superuser's credential and give it to a user
1536+ process is a bad idea, since the file object keeps the credential
1537+ in it. It may affect LSM or something. This approach doesn't work
1538+ either.
1539+
1540+The approach A is ideal, but it hard to implement. So here is a
1541+variation of A, which is to be implemented.
1542+
1543+A-1. Introduce aufs_atomic_open()
1544+ - calls branch fs ->atomic_open() if exists. otherwise calls
1545+ vfs_create() and finish_open().
1546+ - the demerit is that the several checks after branch fs
1547+ ->atomic_open() are lost. in the ordinary case, the checks are
1548+ done by VFS:do_last(), lookup_open() and atomic_open(). some can
1549+ be implemented in aufs, but not all I am afraid.
53392da6 1550diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
eca34b5c 1551--- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 1552+++ linux/Documentation/filesystems/aufs/design/03lookup.txt 2022-11-05 23:02:18.959222617 +0100
7e9cd9fe 1553@@ -0,0 +1,113 @@
53392da6 1554+
0dcfbb52
AM
1555+# Copyright (C) 2005-2022 Junjiro R. Okajima
1556+#
53392da6
AM
1557+# This program is free software; you can redistribute it and/or modify
1558+# it under the terms of the GNU General Public License as published by
1559+# the Free Software Foundation; either version 2 of the License, or
1560+# (at your option) any later version.
0dcfbb52 1561+#
53392da6
AM
1562+# This program is distributed in the hope that it will be useful,
1563+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1564+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1565+# GNU General Public License for more details.
0dcfbb52 1566+#
53392da6 1567+# You should have received a copy of the GNU General Public License
523b37e3 1568+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1569+
1570+Lookup in a Branch
1571+----------------------------------------------------------------------
1572+Since aufs has a character of sub-VFS (see Introduction), it operates
7e9cd9fe
AM
1573+lookup for branches as VFS does. It may be a heavy work. But almost all
1574+lookup operation in aufs is the simplest case, ie. lookup only an entry
1575+directly connected to its parent. Digging down the directory hierarchy
1576+is unnecessary. VFS has a function lookup_one_len() for that use, and
1577+aufs calls it.
1578+
1579+When a branch is a remote filesystem, aufs basically relies upon its
53392da6
AM
1580+->d_revalidate(), also aufs forces the hardest revalidate tests for
1581+them.
1582+For d_revalidate, aufs implements three levels of revalidate tests. See
1583+"Revalidate Dentry and UDBA" in detail.
1584+
1585+
076b876e
AM
1586+Test Only the Highest One for the Directory Permission (dirperm1 option)
1587+----------------------------------------------------------------------
1588+Let's try case study.
1589+- aufs has two branches, upper readwrite and lower readonly.
1590+ /au = /rw + /ro
1591+- "dirA" exists under /ro, but /rw. and its mode is 0700.
1592+- user invoked "chmod a+rx /au/dirA"
1593+- the internal copy-up is activated and "/rw/dirA" is created and its
7e9cd9fe 1594+ permission bits are set to world readable.
076b876e
AM
1595+- then "/au/dirA" becomes world readable?
1596+
1597+In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1598+or it may be a natively readonly filesystem. If aufs respects the lower
1599+branch, it should not respond readdir request from other users. But user
1600+allowed it by chmod. Should really aufs rejects showing the entries
1601+under /ro/dirA?
1602+
7e9cd9fe
AM
1603+To be honest, I don't have a good solution for this case. So aufs
1604+implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1605+users.
076b876e
AM
1606+When dirperm1 is specified, aufs checks only the highest one for the
1607+directory permission, and shows the entries. Otherwise, as usual, checks
1608+every dir existing on all branches and rejects the request.
1609+
1610+As a side effect, dirperm1 option improves the performance of aufs
1611+because the number of permission check is reduced when the number of
1612+branch is many.
1613+
1614+
53392da6
AM
1615+Revalidate Dentry and UDBA (User's Direct Branch Access)
1616+----------------------------------------------------------------------
1617+Generally VFS helpers re-validate a dentry as a part of lookup.
1618+0. digging down the directory hierarchy.
1619+1. lock the parent dir by its i_mutex.
1620+2. lookup the final (child) entry.
1621+3. revalidate it.
1622+4. call the actual operation (create, unlink, etc.)
1623+5. unlock the parent dir
1624+
1625+If the filesystem implements its ->d_revalidate() (step 3), then it is
1626+called. Actually aufs implements it and checks the dentry on a branch is
1627+still valid.
1628+But it is not enough. Because aufs has to release the lock for the
1629+parent dir on a branch at the end of ->lookup() (step 2) and
1630+->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1631+held by VFS.
1632+If the file on a branch is changed directly, eg. bypassing aufs, after
1633+aufs released the lock, then the subsequent operation may cause
1634+something unpleasant result.
1635+
1636+This situation is a result of VFS architecture, ->lookup() and
1637+->d_revalidate() is separated. But I never say it is wrong. It is a good
1638+design from VFS's point of view. It is just not suitable for sub-VFS
1639+character in aufs.
1640+
1641+Aufs supports such case by three level of revalidation which is
1642+selectable by user.
1643+1. Simple Revalidate
1644+ Addition to the native flow in VFS's, confirm the child-parent
1645+ relationship on the branch just after locking the parent dir on the
1646+ branch in the "actual operation" (step 4). When this validation
1647+ fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1648+ checks the validation of the dentry on branches.
1649+2. Monitor Changes Internally by Inotify/Fsnotify
1650+ Addition to above, in the "actual operation" (step 4) aufs re-lookup
1651+ the dentry on the branch, and returns EBUSY if it finds different
1652+ dentry.
1653+ Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1654+ during it is in cache. When the event is notified, aufs registers a
1655+ function to kernel 'events' thread by schedule_work(). And the
1656+ function sets some special status to the cached aufs dentry and inode
1657+ private data. If they are not cached, then aufs has nothing to
1658+ do. When the same file is accessed through aufs (step 0-3) later,
1659+ aufs will detect the status and refresh all necessary data.
1660+ In this mode, aufs has to ignore the event which is fired by aufs
1661+ itself.
1662+3. No Extra Validation
1663+ This is the simplest test and doesn't add any additional revalidation
7e9cd9fe 1664+ test, and skip the revalidation in step 4. It is useful and improves
53392da6
AM
1665+ aufs performance when system surely hide the aufs branches from user,
1666+ by over-mounting something (or another method).
1667diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
eca34b5c 1668--- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 1669+++ linux/Documentation/filesystems/aufs/design/04branch.txt 2022-11-05 23:02:18.959222617 +0100
7e9cd9fe 1670@@ -0,0 +1,74 @@
53392da6 1671+
0dcfbb52
AM
1672+# Copyright (C) 2005-2022 Junjiro R. Okajima
1673+#
53392da6
AM
1674+# This program is free software; you can redistribute it and/or modify
1675+# it under the terms of the GNU General Public License as published by
1676+# the Free Software Foundation; either version 2 of the License, or
1677+# (at your option) any later version.
0dcfbb52 1678+#
53392da6
AM
1679+# This program is distributed in the hope that it will be useful,
1680+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1681+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1682+# GNU General Public License for more details.
0dcfbb52 1683+#
53392da6 1684+# You should have received a copy of the GNU General Public License
523b37e3 1685+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1686+
1687+Branch Manipulation
1688+
1689+Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1690+and changing its permission/attribute, there are a lot of works to do.
1691+
1692+
1693+Add a Branch
1694+----------------------------------------------------------------------
1695+o Confirm the adding dir exists outside of aufs, including loopback
7e9cd9fe 1696+ mount, and its various attributes.
53392da6
AM
1697+o Initialize the xino file and whiteout bases if necessary.
1698+ See struct.txt.
1699+
1700+o Check the owner/group/mode of the directory
1701+ When the owner/group/mode of the adding directory differs from the
1702+ existing branch, aufs issues a warning because it may impose a
1703+ security risk.
1704+ For example, when a upper writable branch has a world writable empty
1705+ top directory, a malicious user can create any files on the writable
1706+ branch directly, like copy-up and modify manually. If something like
1707+ /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1708+ writable branch, and the writable branch is world-writable, then a
1709+ malicious guy may create /etc/passwd on the writable branch directly
1710+ and the infected file will be valid in aufs.
7e9cd9fe 1711+ I am afraid it can be a security issue, but aufs can do nothing except
53392da6
AM
1712+ producing a warning.
1713+
1714+
1715+Delete a Branch
1716+----------------------------------------------------------------------
1717+o Confirm the deleting branch is not busy
1718+ To be general, there is one merit to adopt "remount" interface to
1719+ manipulate branches. It is to discard caches. At deleting a branch,
1720+ aufs checks the still cached (and connected) dentries and inodes. If
1721+ there are any, then they are all in-use. An inode without its
1722+ corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1723+
1724+ For the cached one, aufs checks whether the same named entry exists on
1725+ other branches.
1726+ If the cached one is a directory, because aufs provides a merged view
1727+ to users, as long as one dir is left on any branch aufs can show the
1728+ dir to users. In this case, the branch can be removed from aufs.
1729+ Otherwise aufs rejects deleting the branch.
1730+
1731+ If any file on the deleting branch is opened by aufs, then aufs
1732+ rejects deleting.
1733+
1734+
1735+Modify the Permission of a Branch
1736+----------------------------------------------------------------------
1737+o Re-initialize or remove the xino file and whiteout bases if necessary.
1738+ See struct.txt.
1739+
1740+o rw --> ro: Confirm the modifying branch is not busy
1741+ Aufs rejects the request if any of these conditions are true.
1742+ - a file on the branch is mmap-ed.
1743+ - a regular file on the branch is opened for write and there is no
1744+ same named entry on the upper branch.
1745diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
eca34b5c 1746--- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 1747+++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt 2022-11-05 23:02:18.959222617 +0100
523b37e3 1748@@ -0,0 +1,64 @@
53392da6 1749+
0dcfbb52
AM
1750+# Copyright (C) 2005-2022 Junjiro R. Okajima
1751+#
53392da6
AM
1752+# This program is free software; you can redistribute it and/or modify
1753+# it under the terms of the GNU General Public License as published by
1754+# the Free Software Foundation; either version 2 of the License, or
1755+# (at your option) any later version.
0dcfbb52 1756+#
53392da6
AM
1757+# This program is distributed in the hope that it will be useful,
1758+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1759+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1760+# GNU General Public License for more details.
0dcfbb52 1761+#
53392da6 1762+# You should have received a copy of the GNU General Public License
523b37e3 1763+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1764+
1765+Policies to Select One among Multiple Writable Branches
1766+----------------------------------------------------------------------
1767+When the number of writable branch is more than one, aufs has to decide
1768+the target branch for file creation or copy-up. By default, the highest
1769+writable branch which has the parent (or ancestor) dir of the target
1770+file is chosen (top-down-parent policy).
1771+By user's request, aufs implements some other policies to select the
7e9cd9fe
AM
1772+writable branch, for file creation several policies, round-robin,
1773+most-free-space, and other policies. For copy-up, top-down-parent,
1774+bottom-up-parent, bottom-up and others.
53392da6
AM
1775+
1776+As expected, the round-robin policy selects the branch in circular. When
1777+you have two writable branches and creates 10 new files, 5 files will be
1778+created for each branch. mkdir(2) systemcall is an exception. When you
1779+create 10 new directories, all will be created on the same branch.
1780+And the most-free-space policy selects the one which has most free
1781+space among the writable branches. The amount of free space will be
1782+checked by aufs internally, and users can specify its time interval.
1783+
1784+The policies for copy-up is more simple,
1785+top-down-parent is equivalent to the same named on in create policy,
1786+bottom-up-parent selects the writable branch where the parent dir
1787+exists and the nearest upper one from the copyup-source,
1788+bottom-up selects the nearest upper writable branch from the
1789+copyup-source, regardless the existence of the parent dir.
1790+
1791+There are some rules or exceptions to apply these policies.
1792+- If there is a readonly branch above the policy-selected branch and
1793+ the parent dir is marked as opaque (a variation of whiteout), or the
1794+ target (creating) file is whiteout-ed on the upper readonly branch,
1795+ then the result of the policy is ignored and the target file will be
1796+ created on the nearest upper writable branch than the readonly branch.
1797+- If there is a writable branch above the policy-selected branch and
1798+ the parent dir is marked as opaque or the target file is whiteouted
1799+ on the branch, then the result of the policy is ignored and the target
1800+ file will be created on the highest one among the upper writable
1801+ branches who has diropq or whiteout. In case of whiteout, aufs removes
1802+ it as usual.
1803+- link(2) and rename(2) systemcalls are exceptions in every policy.
1804+ They try selecting the branch where the source exists as possible
1805+ since copyup a large file will take long time. If it can't be,
1806+ ie. the branch where the source exists is readonly, then they will
1807+ follow the copyup policy.
1808+- There is an exception for rename(2) when the target exists.
1809+ If the rename target exists, aufs compares the index of the branches
1810+ where the source and the target exists and selects the higher
1811+ one. If the selected branch is readonly, then aufs follows the
1812+ copyup policy.
8b6a4947 1813diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot linux/Documentation/filesystems/aufs/design/06dirren.dot
eca34b5c 1814--- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot 1970-01-01 01:00:00.000000000 +0100
0dcfbb52
AM
1815+++ linux/Documentation/filesystems/aufs/design/06dirren.dot 2022-11-05 23:02:18.959222617 +0100
1816@@ -0,0 +1,44 @@
8b6a4947
AM
1817+
1818+// to view this graph, run dot(1) command in GRAPHVIZ.
0dcfbb52
AM
1819+//
1820+// This program is free software; you can redistribute it and/or modify
1821+// it under the terms of the GNU General Public License as published by
1822+// the Free Software Foundation; either version 2 of the License, or
1823+// (at your option) any later version.
1824+//
1825+// This program is distributed in the hope that it will be useful,
1826+// but WITHOUT ANY WARRANTY; without even the implied warranty of
1827+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1828+// GNU General Public License for more details.
1829+//
1830+// You should have received a copy of the GNU General Public License
1831+// along with this program. If not, see <http://www.gnu.org/licenses/>.
8b6a4947
AM
1832+
1833+digraph G {
1834+node [shape=box];
1835+whinfo [label="detailed info file\n(lower_brid_root-hinum, h_inum, namelen, old name)"];
1836+
1837+node [shape=oval];
1838+
1839+aufs_rename -> whinfo [label="store/remove"];
1840+
1841+node [shape=oval];
1842+inode_list [label="h_inum list in branch\ncache"];
1843+
1844+node [shape=box];
1845+whinode [label="h_inum list file"];
1846+
1847+node [shape=oval];
1848+brmgmt [label="br_add/del/mod/umount"];
1849+
1850+brmgmt -> inode_list [label="create/remove"];
1851+brmgmt -> whinode [label="load/store"];
1852+
1853+inode_list -> whinode [style=dashed,dir=both];
1854+
1855+aufs_rename -> inode_list [label="add/del"];
1856+
1857+aufs_lookup -> inode_list [label="search"];
1858+
1859+aufs_lookup -> whinfo [label="load/remove"];
1860+}
1861diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt linux/Documentation/filesystems/aufs/design/06dirren.txt
eca34b5c 1862--- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 1863+++ linux/Documentation/filesystems/aufs/design/06dirren.txt 2022-11-05 23:02:18.959222617 +0100
8b6a4947
AM
1864@@ -0,0 +1,102 @@
1865+
0dcfbb52 1866+# Copyright (C) 2017-2022 Junjiro R. Okajima
8b6a4947
AM
1867+#
1868+# This program is free software; you can redistribute it and/or modify
1869+# it under the terms of the GNU General Public License as published by
1870+# the Free Software Foundation; either version 2 of the License, or
1871+# (at your option) any later version.
1872+#
1873+# This program is distributed in the hope that it will be useful,
1874+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1875+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1876+# GNU General Public License for more details.
1877+#
1878+# You should have received a copy of the GNU General Public License
1879+# along with this program. If not, see <http://www.gnu.org/licenses/>.
1880+
1881+Special handling for renaming a directory (DIRREN)
1882+----------------------------------------------------------------------
1883+First, let's assume we have a simple usecase.
1884+
1885+- /u = /rw + /ro
1886+- /rw/dirA exists
1887+- /ro/dirA and /ro/dirA/file exist too
1888+- there is no dirB on both branches
1889+- a user issues rename("dirA", "dirB")
1890+
1891+Now, what should aufs behave against this rename(2)?
1892+There are a few possible cases.
1893+
1894+A. returns EROFS.
1895+ since dirA exists on a readonly branch which cannot be renamed.
1896+B. returns EXDEV.
1897+ it is possible to copy-up dirA (only the dir itself), but the child
1898+ entries ("file" in this case) should not be. it must be a bad
1899+ approach to copy-up recursively.
1900+C. returns a success.
1901+ even the branch /ro is readonly, aufs tries renaming it. Obviously it
1902+ is a violation of aufs' policy.
1903+D. construct an extra information which indicates that /ro/dirA should
1904+ be handled as the name of dirB.
1905+ overlayfs has a similar feature called REDIRECT.
1906+
1907+Until now, aufs implements the case B only which returns EXDEV, and
1908+expects the userspace application behaves like mv(1) which tries
1909+issueing rename(2) recursively.
1910+
1911+A new aufs feature called DIRREN is introduced which implements the case
1912+D. There are several "extra information" added.
1913+
1914+1. detailed info per renamed directory
1915+ path: /rw/dirB/$AUFS_WH_DR_INFO_PFX.<lower branch-id>
1916+2. the inode-number list of directories on a branch
1917+ path: /rw/dirB/$AUFS_WH_DR_BRHINO
1918+
1919+The filename of "detailed info per directory" represents the lower
1920+branch, and its format is
1921+- a type of the branch id
1922+ one of these.
1923+ + uuid (not implemented yet)
1924+ + fsid
1925+ + dev
1926+- the inode-number of the branch root dir
1927+
1928+And it contains these info in a single regular file.
1929+- magic number
1930+- branch's inode-number of the logically renamed dir
1931+- the name of the before-renamed dir
1932+
1933+The "detailed info per directory" file is created in aufs rename(2), and
1934+loaded in any lookup.
1935+The info is considered in lookup for the matching case only. Here
1936+"matching" means that the root of branch (in the info filename) is same
1937+to the current looking-up branch. After looking-up the before-renamed
1938+name, the inode-number is compared. And the matched dentry is used.
1939+
1940+The "inode-number list of directories" is a regular file which contains
1941+simply the inode-numbers on the branch. The file is created or updated
1942+in removing the branch, and loaded in adding the branch. Its lifetime is
1943+equal to the branch.
22319442 1944+The list is referred in lookup, and when the current target inode is
8b6a4947
AM
1945+found in the list, the aufs tries loading the "detailed info per
1946+directory" and get the changed and valid name of the dir.
1947+
1948+Theoretically these "extra informaiton" may be able to be put into XATTR
1949+in the dir inode. But aufs doesn't choose this way because
1950+1. XATTR may not be supported by the branch (or its configuration)
1951+2. XATTR may have its size limit.
1952+3. XATTR may be less easy to convert than a regular file, when the
1953+ format of the info is changed in the future.
1954+At the same time, I agree that the regular file approach is much slower
1955+than XATTR approach. So, in the future, aufs may take the XATTR or other
1956+better approach.
1957+
1958+This DIRREN feature is enabled by aufs configuration, and is activated
1959+by a new mount option.
1960+
1961+For the more complicated case, there is a work with UDBA option, which
1962+is to dected the direct access to the branches (by-passing aufs) and to
1963+maintain the cashes in aufs. Since a single cached aufs dentry may
1964+contains two names, before- and after-rename, the name comparision in
1965+UDBA handler may not work correctly. In this case, the behaviour will be
1966+equivalen to udba=reval case.
076b876e 1967diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt linux/Documentation/filesystems/aufs/design/06fhsm.txt
eca34b5c 1968--- /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt 1970-01-01 01:00:00.000000000 +0100
0dcfbb52
AM
1969+++ linux/Documentation/filesystems/aufs/design/06fhsm.txt 2022-11-05 23:02:18.959222617 +0100
1970@@ -0,0 +1,118 @@
076b876e 1971+
0dcfbb52
AM
1972+# Copyright (C) 2011-2022 Junjiro R. Okajima
1973+#
076b876e
AM
1974+# This program is free software; you can redistribute it and/or modify
1975+# it under the terms of the GNU General Public License as published by
1976+# the Free Software Foundation; either version 2 of the License, or
1977+# (at your option) any later version.
0dcfbb52 1978+#
076b876e
AM
1979+# This program is distributed in the hope that it will be useful,
1980+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1981+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1982+# GNU General Public License for more details.
0dcfbb52 1983+#
076b876e 1984+# You should have received a copy of the GNU General Public License
0dcfbb52 1985+# along with this program. If not, see <http://www.gnu.org/licenses/>.
076b876e
AM
1986+
1987+File-based Hierarchical Storage Management (FHSM)
1988+----------------------------------------------------------------------
1989+Hierarchical Storage Management (or HSM) is a well-known feature in the
1990+storage world. Aufs provides this feature as file-based with multiple
7e9cd9fe 1991+writable branches, based upon the principle of "Colder, the Lower".
076b876e 1992+Here the word "colder" means that the less used files, and "lower" means
7e9cd9fe 1993+that the position in the order of the stacked branches vertically.
076b876e
AM
1994+These multiple writable branches are prioritized, ie. the topmost one
1995+should be the fastest drive and be used heavily.
1996+
1997+o Characters in aufs FHSM story
1998+- aufs itself and a new branch attribute.
1999+- a new ioctl interface to move-down and to establish a connection with
2000+ the daemon ("move-down" is a converse of "copy-up").
2001+- userspace tool and daemon.
2002+
2003+The userspace daemon establishes a connection with aufs and waits for
2004+the notification. The notified information is very similar to struct
2005+statfs containing the number of consumed blocks and inodes.
2006+When the consumed blocks/inodes of a branch exceeds the user-specified
2007+upper watermark, the daemon activates its move-down process until the
2008+consumed blocks/inodes reaches the user-specified lower watermark.
2009+
2010+The actual move-down is done by aufs based upon the request from
2011+user-space since we need to maintain the inode number and the internal
2012+pointer arrays in aufs.
2013+
2014+Currently aufs FHSM handles the regular files only. Additionally they
2015+must not be hard-linked nor pseudo-linked.
2016+
2017+
2018+o Cowork of aufs and the user-space daemon
2019+ During the userspace daemon established the connection, aufs sends a
2020+ small notification to it whenever aufs writes something into the
2021+ writable branch. But it may cost high since aufs issues statfs(2)
2022+ internally. So user can specify a new option to cache the
2023+ info. Actually the notification is controlled by these factors.
2024+ + the specified cache time.
2025+ + classified as "force" by aufs internally.
2026+ Until the specified time expires, aufs doesn't send the info
2027+ except the forced cases. When aufs decide forcing, the info is always
2028+ notified to userspace.
2029+ For example, the number of free inodes is generally large enough and
2030+ the shortage of it happens rarely. So aufs doesn't force the
2031+ notification when creating a new file, directory and others. This is
2032+ the typical case which aufs doesn't force.
2033+ When aufs writes the actual filedata and the files consumes any of new
2034+ blocks, the aufs forces notifying.
2035+
2036+
2037+o Interfaces in aufs
2038+- New branch attribute.
2039+ + fhsm
2040+ Specifies that the branch is managed by FHSM feature. In other word,
2041+ participant in the FHSM.
2042+ When nofhsm is set to the branch, it will not be the source/target
2043+ branch of the move-down operation. This attribute is set
2044+ independently from coo and moo attributes, and if you want full
2045+ FHSM, you should specify them as well.
2046+- New mount option.
2047+ + fhsm_sec
2048+ Specifies a second to suppress many less important info to be
2049+ notified.
2050+- New ioctl.
2051+ + AUFS_CTL_FHSM_FD
2052+ create a new file descriptor which userspace can read the notification
2053+ (a subset of struct statfs) from aufs.
2054+- Module parameter 'brs'
2055+ It has to be set to 1. Otherwise the new mount option 'fhsm' will not
2056+ be set.
2057+- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
2058+ When there are two or more branches with fhsm attributes,
2059+ /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
2060+ terminates it. As a result of remounting and branch-manipulation, the
2061+ number of branches with fhsm attribute can be one. In this case,
2062+ /sbin/mount.aufs will terminate the user-space daemon.
2063+
2064+
2065+Finally the operation is done as these steps in kernel-space.
2066+- make sure that,
2067+ + no one else is using the file.
2068+ + the file is not hard-linked.
2069+ + the file is not pseudo-linked.
2070+ + the file is a regular file.
2071+ + the parent dir is not opaqued.
2072+- find the target writable branch.
2073+- make sure the file is not whiteout-ed by the upper (than the target)
2074+ branch.
2075+- make the parent dir on the target branch.
2076+- mutex lock the inode on the branch.
2077+- unlink the whiteout on the target branch (if exists).
2078+- lookup and create the whiteout-ed temporary name on the target branch.
2079+- copy the file as the whiteout-ed temporary name on the target branch.
2080+- rename the whiteout-ed temporary name to the original name.
2081+- unlink the file on the source branch.
2082+- maintain the internal pointer array and the external inode number
2083+ table (XINO).
2084+- maintain the timestamps and other attributes of the parent dir and the
2085+ file.
2086+
2087+And of course, in every step, an error may happen. So the operation
2088+should restore the original file state after an error happens.
53392da6 2089diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
eca34b5c 2090--- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 2091+++ linux/Documentation/filesystems/aufs/design/06mmap.txt 2022-11-05 23:02:18.959222617 +0100
b912730e 2092@@ -0,0 +1,72 @@
53392da6 2093+
0dcfbb52
AM
2094+# Copyright (C) 2005-2022 Junjiro R. Okajima
2095+#
53392da6
AM
2096+# This program is free software; you can redistribute it and/or modify
2097+# it under the terms of the GNU General Public License as published by
2098+# the Free Software Foundation; either version 2 of the License, or
2099+# (at your option) any later version.
0dcfbb52 2100+#
53392da6
AM
2101+# This program is distributed in the hope that it will be useful,
2102+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2103+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2104+# GNU General Public License for more details.
0dcfbb52 2105+#
53392da6 2106+# You should have received a copy of the GNU General Public License
523b37e3 2107+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2108+
2109+mmap(2) -- File Memory Mapping
2110+----------------------------------------------------------------------
2111+In aufs, the file-mapped pages are handled by a branch fs directly, no
2112+interaction with aufs. It means aufs_mmap() calls the branch fs's
2113+->mmap().
2114+This approach is simple and good, but there is one problem.
7e9cd9fe 2115+Under /proc, several entries show the mmapped files by its path (with
53392da6
AM
2116+device and inode number), and the printed path will be the path on the
2117+branch fs's instead of virtual aufs's.
2118+This is not a problem in most cases, but some utilities lsof(1) (and its
2119+user) may expect the path on aufs.
2120+
2121+To address this issue, aufs adds a new member called vm_prfile in struct
2122+vm_area_struct (and struct vm_region). The original vm_file points to
2123+the file on the branch fs in order to handle everything correctly as
2124+usual. The new vm_prfile points to a virtual file in aufs, and the
2125+show-functions in procfs refers to vm_prfile if it is set.
2126+Also we need to maintain several other places where touching vm_file
2127+such like
2128+- fork()/clone() copies vma and the reference count of vm_file is
2129+ incremented.
2130+- merging vma maintains the ref count too.
2131+
7e9cd9fe 2132+This is not a good approach. It just fakes the printed path. But it
53392da6
AM
2133+leaves all behaviour around f_mapping unchanged. This is surely an
2134+advantage.
2135+Actually aufs had adopted another complicated approach which calls
2136+generic_file_mmap() and handles struct vm_operations_struct. In this
2137+approach, aufs met a hard problem and I could not solve it without
2138+switching the approach.
b912730e
AM
2139+
2140+There may be one more another approach which is
2141+- bind-mount the branch-root onto the aufs-root internally
2142+- grab the new vfsmount (ie. struct mount)
2143+- lazy-umount the branch-root internally
2144+- in open(2) the aufs-file, open the branch-file with the hidden
2145+ vfsmount (instead of the original branch's vfsmount)
2146+- ideally this "bind-mount and lazy-umount" should be done atomically,
2147+ but it may be possible from userspace by the mount helper.
2148+
2149+Adding the internal hidden vfsmount and using it in opening a file, the
2150+file path under /proc will be printed correctly. This approach looks
2151+smarter, but is not possible I am afraid.
2152+- aufs-root may be bind-mount later. when it happens, another hidden
2153+ vfsmount will be required.
2154+- it is hard to get the chance to bind-mount and lazy-umount
2155+ + in kernel-space, FS can have vfsmount in open(2) via
2156+ file->f_path, and aufs can know its vfsmount. But several locks are
2157+ already acquired, and if aufs tries to bind-mount and lazy-umount
2158+ here, then it may cause a deadlock.
2159+ + in user-space, bind-mount doesn't invoke the mount helper.
2160+- since /proc shows dev and ino, aufs has to give vma these info. it
2161+ means a new member vm_prinode will be necessary. this is essentially
2162+ equivalent to vm_prfile described above.
2163+
2164+I have to give up this "looks-smater" approach.
c1595e42 2165diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt linux/Documentation/filesystems/aufs/design/06xattr.txt
eca34b5c 2166--- /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt 1970-01-01 01:00:00.000000000 +0100
0dcfbb52
AM
2167+++ linux/Documentation/filesystems/aufs/design/06xattr.txt 2022-11-05 23:02:18.959222617 +0100
2168@@ -0,0 +1,94 @@
c1595e42 2169+
0dcfbb52 2170+# Copyright (C) 2014-2022 Junjiro R. Okajima
c1595e42
JR
2171+#
2172+# This program is free software; you can redistribute it and/or modify
2173+# it under the terms of the GNU General Public License as published by
2174+# the Free Software Foundation; either version 2 of the License, or
2175+# (at your option) any later version.
2176+#
2177+# This program is distributed in the hope that it will be useful,
2178+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2179+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2180+# GNU General Public License for more details.
2181+#
2182+# You should have received a copy of the GNU General Public License
0dcfbb52 2183+# along with this program. If not, see <http://www.gnu.org/licenses/>.
c1595e42
JR
2184+
2185+Listing XATTR/EA and getting the value
2186+----------------------------------------------------------------------
2187+For the inode standard attributes (owner, group, timestamps, etc.), aufs
2188+shows the values from the topmost existing file. This behaviour is good
7e9cd9fe 2189+for the non-dir entries since the bahaviour exactly matches the shown
c1595e42
JR
2190+information. But for the directories, aufs considers all the same named
2191+entries on the lower branches. Which means, if one of the lower entry
2192+rejects readdir call, then aufs returns an error even if the topmost
2193+entry allows it. This behaviour is necessary to respect the branch fs's
2194+security, but can make users confused since the user-visible standard
2195+attributes don't match the behaviour.
2196+To address this issue, aufs has a mount option called dirperm1 which
2197+checks the permission for the topmost entry only, and ignores the lower
2198+entry's permission.
2199+
2200+A similar issue can happen around XATTR.
2201+getxattr(2) and listxattr(2) families behave as if dirperm1 option is
7e9cd9fe
AM
2202+always set. Otherwise these very unpleasant situation would happen.
2203+- listxattr(2) may return the duplicated entries.
c1595e42
JR
2204+- users may not be able to remove or reset the XATTR forever,
2205+
2206+
2207+XATTR/EA support in the internal (copy,move)-(up,down)
2208+----------------------------------------------------------------------
7e9cd9fe 2209+Generally the extended attributes of inode are categorized as these.
c1595e42
JR
2210+- "security" for LSM and capability.
2211+- "system" for posix ACL, 'acl' mount option is required for the branch
2212+ fs generally.
2213+- "trusted" for userspace, CAP_SYS_ADMIN is required.
2214+- "user" for userspace, 'user_xattr' mount option is required for the
2215+ branch fs generally.
2216+
2217+Moreover there are some other categories. Aufs handles these rather
2218+unpopular categories as the ordinary ones, ie. there is no special
2219+condition nor exception.
2220+
2221+In copy-up, the support for XATTR on the dst branch may differ from the
2222+src branch. In this case, the copy-up operation will get an error and
7e9cd9fe
AM
2223+the original user operation which triggered the copy-up will fail. It
2224+can happen that even all copy-up will fail.
c1595e42
JR
2225+When both of src and dst branches support XATTR and if an error occurs
2226+during copying XATTR, then the copy-up should fail obviously. That is a
2227+good reason and aufs should return an error to userspace. But when only
7e9cd9fe 2228+the src branch support that XATTR, aufs should not return an error.
c1595e42
JR
2229+For example, the src branch supports ACL but the dst branch doesn't
2230+because the dst branch may natively un-support it or temporary
2231+un-support it due to "noacl" mount option. Of course, the dst branch fs
2232+may NOT return an error even if the XATTR is not supported. It is
2233+totally up to the branch fs.
2234+
2235+Anyway when the aufs internal copy-up gets an error from the dst branch
2236+fs, then aufs tries removing the just copied entry and returns the error
2237+to the userspace. The worst case of this situation will be all copy-up
2238+will fail.
2239+
2240+For the copy-up operation, there two basic approaches.
2241+- copy the specified XATTR only (by category above), and return the
7e9cd9fe 2242+ error unconditionally if it happens.
c1595e42
JR
2243+- copy all XATTR, and ignore the error on the specified category only.
2244+
2245+In order to support XATTR and to implement the correct behaviour, aufs
7e9cd9fe
AM
2246+chooses the latter approach and introduces some new branch attributes,
2247+"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
c1595e42 2248+They correspond to the XATTR namespaces (see above). Additionally, to be
7e9cd9fe
AM
2249+convenient, "icex" is also provided which means all "icex*" attributes
2250+are set (here the word "icex" stands for "ignore copy-error on XATTR").
c1595e42
JR
2251+
2252+The meaning of these attributes is to ignore the error from setting
2253+XATTR on that branch.
2254+Note that aufs tries copying all XATTR unconditionally, and ignores the
2255+error from the dst branch according to the specified attributes.
2256+
2257+Some XATTR may have its default value. The default value may come from
2258+the parent dir or the environment. If the default value is set at the
2259+file creating-time, it will be overwritten by copy-up.
2260+Some contradiction may happen I am afraid.
2261+Do we need another attribute to stop copying XATTR? I am unsure. For
2262+now, aufs implements the branch attributes to ignore the error.
53392da6 2263diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
eca34b5c 2264--- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 2265+++ linux/Documentation/filesystems/aufs/design/07export.txt 2022-11-05 23:02:18.959222617 +0100
523b37e3 2266@@ -0,0 +1,58 @@
53392da6 2267+
0dcfbb52
AM
2268+# Copyright (C) 2005-2022 Junjiro R. Okajima
2269+#
53392da6
AM
2270+# This program is free software; you can redistribute it and/or modify
2271+# it under the terms of the GNU General Public License as published by
2272+# the Free Software Foundation; either version 2 of the License, or
2273+# (at your option) any later version.
0dcfbb52 2274+#
53392da6
AM
2275+# This program is distributed in the hope that it will be useful,
2276+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2277+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2278+# GNU General Public License for more details.
0dcfbb52 2279+#
53392da6 2280+# You should have received a copy of the GNU General Public License
523b37e3 2281+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2282+
2283+Export Aufs via NFS
2284+----------------------------------------------------------------------
2285+Here is an approach.
2286+- like xino/xib, add a new file 'xigen' which stores aufs inode
2287+ generation.
2288+- iget_locked(): initialize aufs inode generation for a new inode, and
2289+ store it in xigen file.
2290+- destroy_inode(): increment aufs inode generation and store it in xigen
2291+ file. it is necessary even if it is not unlinked, because any data of
2292+ inode may be changed by UDBA.
2293+- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2294+ build file handle by
2295+ + branch id (4 bytes)
2296+ + superblock generation (4 bytes)
2297+ + inode number (4 or 8 bytes)
2298+ + parent dir inode number (4 or 8 bytes)
2299+ + inode generation (4 bytes))
2300+ + return value of exportfs_encode_fh() for the parent on a branch (4
2301+ bytes)
2302+ + file handle for a branch (by exportfs_encode_fh())
2303+- fh_to_dentry():
2304+ + find the index of a branch from its id in handle, and check it is
2305+ still exist in aufs.
2306+ + 1st level: get the inode number from handle and search it in cache.
7e9cd9fe
AM
2307+ + 2nd level: if not found in cache, get the parent inode number from
2308+ the handle and search it in cache. and then open the found parent
2309+ dir, find the matching inode number by vfs_readdir() and get its
2310+ name, and call lookup_one_len() for the target dentry.
53392da6
AM
2311+ + 3rd level: if the parent dir is not cached, call
2312+ exportfs_decode_fh() for a branch and get the parent on a branch,
2313+ build a pathname of it, convert it a pathname in aufs, call
2314+ path_lookup(). now aufs gets a parent dir dentry, then handle it as
2315+ the 2nd level.
2316+ + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2317+ for every branch, but not itself. to get this, (currently) aufs
2318+ searches in current->nsproxy->mnt_ns list. it may not be a good
2319+ idea, but I didn't get other approach.
2320+ + test the generation of the gotten inode.
2321+- every inode operation: they may get EBUSY due to UDBA. in this case,
2322+ convert it into ESTALE for NFSD.
2323+- readdir(): call lockdep_on/off() because filldir in NFSD calls
2324+ lookup_one_len(), vfs_getattr(), encode_fh() and others.
2325diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
eca34b5c 2326--- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 2327+++ linux/Documentation/filesystems/aufs/design/08shwh.txt 2022-11-05 23:02:18.959222617 +0100
523b37e3 2328@@ -0,0 +1,52 @@
53392da6 2329+
0dcfbb52
AM
2330+# Copyright (C) 2005-2022 Junjiro R. Okajima
2331+#
53392da6
AM
2332+# This program is free software; you can redistribute it and/or modify
2333+# it under the terms of the GNU General Public License as published by
2334+# the Free Software Foundation; either version 2 of the License, or
2335+# (at your option) any later version.
0dcfbb52 2336+#
53392da6
AM
2337+# This program is distributed in the hope that it will be useful,
2338+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2339+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2340+# GNU General Public License for more details.
0dcfbb52 2341+#
53392da6 2342+# You should have received a copy of the GNU General Public License
523b37e3 2343+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2344+
2345+Show Whiteout Mode (shwh)
2346+----------------------------------------------------------------------
2347+Generally aufs hides the name of whiteouts. But in some cases, to show
2348+them is very useful for users. For instance, creating a new middle layer
2349+(branch) by merging existing layers.
2350+
2351+(borrowing aufs1 HOW-TO from a user, Michael Towers)
2352+When you have three branches,
2353+- Bottom: 'system', squashfs (underlying base system), read-only
2354+- Middle: 'mods', squashfs, read-only
2355+- Top: 'overlay', ram (tmpfs), read-write
2356+
2357+The top layer is loaded at boot time and saved at shutdown, to preserve
2358+the changes made to the system during the session.
2359+When larger changes have been made, or smaller changes have accumulated,
2360+the size of the saved top layer data grows. At this point, it would be
2361+nice to be able to merge the two overlay branches ('mods' and 'overlay')
2362+and rewrite the 'mods' squashfs, clearing the top layer and thus
2363+restoring save and load speed.
2364+
2365+This merging is simplified by the use of another aufs mount, of just the
2366+two overlay branches using the 'shwh' option.
2367+# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2368+ aufs /livesys/merge_union
2369+
2370+A merged view of these two branches is then available at
2371+/livesys/merge_union, and the new feature is that the whiteouts are
2372+visible!
2373+Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2374+writing to all branches. Also the default mode for all branches is 'ro'.
2375+It is now possible to save the combined contents of the two overlay
2376+branches to a new squashfs, e.g.:
2377+# mksquashfs /livesys/merge_union /path/to/newmods.squash
2378+
2379+This new squashfs archive can be stored on the boot device and the
2380+initramfs will use it to replace the old one at the next boot.
2381diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
eca34b5c 2382--- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 2383+++ linux/Documentation/filesystems/aufs/design/10dynop.txt 2022-11-05 23:02:18.959222617 +0100
7e9cd9fe 2384@@ -0,0 +1,47 @@
53392da6 2385+
0dcfbb52 2386+# Copyright (C) 2010-2022 Junjiro R. Okajima
53392da6
AM
2387+#
2388+# This program is free software; you can redistribute it and/or modify
2389+# it under the terms of the GNU General Public License as published by
2390+# the Free Software Foundation; either version 2 of the License, or
2391+# (at your option) any later version.
2392+#
2393+# This program is distributed in the hope that it will be useful,
2394+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2395+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2396+# GNU General Public License for more details.
2397+#
2398+# You should have received a copy of the GNU General Public License
523b37e3 2399+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2400+
2401+Dynamically customizable FS operations
2402+----------------------------------------------------------------------
2403+Generally FS operations (struct inode_operations, struct
2404+address_space_operations, struct file_operations, etc.) are defined as
2405+"static const", but it never means that FS have only one set of
2406+operation. Some FS have multiple sets of them. For instance, ext2 has
2407+three sets, one for XIP, for NOBH, and for normal.
2408+Since aufs overrides and redirects these operations, sometimes aufs has
7e9cd9fe 2409+to change its behaviour according to the branch FS type. More importantly
53392da6
AM
2410+VFS acts differently if a function (member in the struct) is set or
2411+not. It means aufs should have several sets of operations and select one
2412+among them according to the branch FS definition.
2413+
7e9cd9fe 2414+In order to solve this problem and not to affect the behaviour of VFS,
53392da6 2415+aufs defines these operations dynamically. For instance, aufs defines
7e9cd9fe
AM
2416+dummy direct_IO function for struct address_space_operations, but it may
2417+not be set to the address_space_operations actually. When the branch FS
2418+doesn't have it, aufs doesn't set it to its address_space_operations
2419+while the function definition itself is still alive. So the behaviour
2420+itself will not change, and it will return an error when direct_IO is
2421+not set.
53392da6
AM
2422+
2423+The lifetime of these dynamically generated operation object is
2424+maintained by aufs branch object. When the branch is removed from aufs,
2425+the reference counter of the object is decremented. When it reaches
2426+zero, the dynamically generated operation object will be freed.
2427+
7e9cd9fe
AM
2428+This approach is designed to support AIO (io_submit), Direct I/O and
2429+XIP (DAX) mainly.
2430+Currently this approach is applied to address_space_operations for
2431+regular files only.
53392da6 2432diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
eca34b5c 2433--- /usr/share/empty/Documentation/filesystems/aufs/README 1970-01-01 01:00:00.000000000 +0100
0dcfbb52
AM
2434+++ linux/Documentation/filesystems/aufs/README 2022-11-05 23:02:18.955889283 +0100
2435@@ -0,0 +1,403 @@
53392da6 2436+
0dcfbb52 2437+Aufs6 -- advanced multi layered unification filesystem version 6.x
53392da6
AM
2438+http://aufs.sf.net
2439+Junjiro R. Okajima
2440+
2441+
2442+0. Introduction
2443+----------------------------------------
2444+In the early days, aufs was entirely re-designed and re-implemented
7e9cd9fe 2445+Unionfs Version 1.x series. Adding many original ideas, approaches,
fbc438ed 2446+improvements and implementations, it became totally different from
53392da6 2447+Unionfs while keeping the basic features.
fbc438ed 2448+Later, Unionfs Version 2.x series began taking some of the same
53392da6 2449+approaches to aufs1's.
fbc438ed 2450+Unionfs was being developed by Professor Erez Zadok at Stony Brook
53392da6
AM
2451+University and his team.
2452+
0dcfbb52
AM
2453+Aufs6 supports linux-v6.0 and later, try aufs6.0 branch in
2454+aufs-linux.git or aufs-standalone.git.
2455+If you want older kernel version support,
2456+- for linux-v5.x series, try aufs-linux.git or aufs-standalone.git
fbc438ed
JR
2457+- for linux-v4.x series, try aufs4-linux.git or aufs4-standalone.git
2458+- for linux-v3.x series, try aufs3-linux.git or aufs3-standalone.git
2459+- for linux-v2.6.16 and later, try aufs2-2.6.git, aufs2-standalone.git
2460+ or aufs1 from CVS on SourceForge.
53392da6 2461+
0dcfbb52
AM
2462+Note: the name of aufs5-linux.git and aufs5-standalone.git on github
2463+ were changed. Now they are aufs-linux.git and
2464+ aufs-standalone.git and they contain aufs5 and later branches.
2465+
53392da6 2466+Note: it becomes clear that "Aufs was rejected. Let's give it up."
38d290e6
JR
2467+ According to Christoph Hellwig, linux rejects all union-type
2468+ filesystems but UnionMount.
53392da6
AM
2469+<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
2470+
38d290e6
JR
2471+PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
2472+ UnionMount, and he pointed out an issue around a directory mutex
2473+ lock and aufs addressed it. But it is still unsure whether aufs will
2474+ be merged (or any other union solution).
076b876e 2475+<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
38d290e6 2476+
53392da6
AM
2477+
2478+1. Features
2479+----------------------------------------
2480+- unite several directories into a single virtual filesystem. The member
2481+ directory is called as a branch.
2482+- you can specify the permission flags to the branch, which are 'readonly',
2483+ 'readwrite' and 'whiteout-able.'
2484+- by upper writable branch, internal copyup and whiteout, files/dirs on
2485+ readonly branch are modifiable logically.
2486+- dynamic branch manipulation, add, del.
2487+- etc...
2488+
7e9cd9fe
AM
2489+Also there are many enhancements in aufs, such as:
2490+- test only the highest one for the directory permission (dirperm1)
2491+- copyup on open (coo=)
2492+- 'move' policy for copy-up between two writable branches, after
2493+ checking free space.
2494+- xattr, acl
53392da6
AM
2495+- readdir(3) in userspace.
2496+- keep inode number by external inode number table
2497+- keep the timestamps of file/dir in internal copyup operation
2498+- seekable directory, supporting NFS readdir.
2499+- whiteout is hardlinked in order to reduce the consumption of inodes
2500+ on branch
2501+- do not copyup, nor create a whiteout when it is unnecessary
2502+- revert a single systemcall when an error occurs in aufs
2503+- remount interface instead of ioctl
2504+- maintain /etc/mtab by an external command, /sbin/mount.aufs.
2505+- loopback mounted filesystem as a branch
2506+- kernel thread for removing the dir who has a plenty of whiteouts
2507+- support copyup sparse file (a file which has a 'hole' in it)
2508+- default permission flags for branches
2509+- selectable permission flags for ro branch, whether whiteout can
2510+ exist or not
2511+- export via NFS.
2512+- support <sysfs>/fs/aufs and <debugfs>/aufs.
2513+- support multiple writable branches, some policies to select one
2514+ among multiple writable branches.
2515+- a new semantics for link(2) and rename(2) to support multiple
2516+ writable branches.
2517+- no glibc changes are required.
2518+- pseudo hardlink (hardlink over branches)
2519+- allow a direct access manually to a file on branch, e.g. bypassing aufs.
2520+ including NFS or remote filesystem branch.
2521+- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
2522+- and more...
2523+
0dcfbb52 2524+Currently these features are dropped temporary from aufs6.
53392da6 2525+See design/08plan.txt in detail.
53392da6
AM
2526+- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
2527+ (robr)
2528+- statistics of aufs thread (/sys/fs/aufs/stat)
53392da6
AM
2529+
2530+Features or just an idea in the future (see also design/*.txt),
2531+- reorder the branch index without del/re-add.
2532+- permanent xino files for NFSD
2533+- an option for refreshing the opened files after add/del branches
53392da6
AM
2534+- light version, without branch manipulation. (unnecessary?)
2535+- copyup in userspace
2536+- inotify in userspace
2537+- readv/writev
53392da6
AM
2538+
2539+
2540+2. Download
2541+----------------------------------------
0dcfbb52
AM
2542+There are three GIT trees for aufs6, aufs6-linux.git,
2543+aufs6-standalone.git, and aufs-util.git. Note that there is no "6" in
1e00d052 2544+"aufs-util.git."
0dcfbb52
AM
2545+While the aufs-util is always necessary, you need either of aufs6-linux
2546+or aufs6-standalone.
1e00d052 2547+
0dcfbb52 2548+The aufs6-linux tree includes the whole linux mainline GIT tree,
1e00d052
AM
2549+git://git.kernel.org/.../torvalds/linux.git.
2550+And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
0dcfbb52 2551+build aufs6 as an external kernel module.
2000de60 2552+Several extra patches are not included in this tree. Only
0dcfbb52 2553+aufs6-standalone tree contains them. They are described in the later
2000de60 2554+section "Configuration and Compilation."
1e00d052 2555+
0dcfbb52 2556+On the other hand, the aufs6-standalone tree has only aufs source files
53392da6 2557+and necessary patches, and you can select CONFIG_AUFS_FS=m.
2000de60 2558+But you need to apply all aufs patches manually.
53392da6 2559+
0dcfbb52
AM
2560+You will find GIT branches whose name is in form of "aufs6.x" where "x"
2561+represents the linux kernel version, "linux-6.x". For instance,
2562+"aufs6.0" is for linux-6.0. For latest "linux-6.x-rcN", use
2563+"aufs6.x-rcN" branch.
1e00d052 2564+
0dcfbb52 2565+o aufs6-linux tree
1e00d052 2566+$ git clone --reference /your/linux/git/tree \
0dcfbb52 2567+ git://github.com/sfjro/aufs6-linux.git aufs6-linux.git
1e00d052 2568+- if you don't have linux GIT tree, then remove "--reference ..."
0dcfbb52
AM
2569+$ cd aufs6-linux.git
2570+$ git checkout origin/aufs6.0
53392da6 2571+
2000de60
JR
2572+Or You may want to directly git-pull aufs into your linux GIT tree, and
2573+leave the patch-work to GIT.
2574+$ cd /your/linux/git/tree
0dcfbb52
AM
2575+$ git remote add aufs6 git://github.com/sfjro/aufs6-linux.git
2576+$ git fetch aufs6
2577+$ git checkout -b my6.0 v6.0
5527c038 2578+$ (add your local change...)
0dcfbb52
AM
2579+$ git pull aufs6 aufs6.0
2580+- now you have v6.0 + your_changes + aufs6.0 in you my6.0 branch.
2000de60 2581+- you may need to solve some conflicts between your_changes and
0dcfbb52
AM
2582+ aufs6.0. in this case, git-rerere is recommended so that you can
2583+ solve the similar conflicts automatically when you upgrade to 6.1 or
2000de60
JR
2584+ later in the future.
2585+
0dcfbb52
AM
2586+o aufs6-standalone tree
2587+$ git clone git://github.com/sfjro/aufs6-standalone.git aufs6-standalone.git
2588+$ cd aufs6-standalone.git
2589+$ git checkout origin/aufs6.0
53392da6
AM
2590+
2591+o aufs-util tree
5527c038
JR
2592+$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
2593+- note that the public aufs-util.git is on SourceForge instead of
2594+ GitHUB.
53392da6 2595+$ cd aufs-util.git
0dcfbb52 2596+$ git checkout origin/aufs6.0
53392da6 2597+
0dcfbb52
AM
2598+Note: The 6.x-rcN branch is to be used with `rc' kernel versions ONLY.
2599+The minor version number, 'x' in '6.x', of aufs may not always
9dbd164d
AM
2600+follow the minor version number of the kernel.
2601+Because changes in the kernel that cause the use of a new
2602+minor version number do not always require changes to aufs-util.
2603+
2604+Since aufs-util has its own minor version number, you may not be
2605+able to find a GIT branch in aufs-util for your kernel's
2606+exact minor version number.
2607+In this case, you should git-checkout the branch for the
53392da6 2608+nearest lower number.
9dbd164d
AM
2609+
2610+For (an unreleased) example:
0dcfbb52
AM
2611+If you are using "linux-6.10" and the "aufs6.10" branch
2612+does not exist in aufs-util repository, then "aufs6.9", "aufs6.8"
9dbd164d
AM
2613+or something numerically smaller is the branch for your kernel.
2614+
53392da6
AM
2615+Also you can view all branches by
2616+ $ git branch -a
2617+
2618+
2619+3. Configuration and Compilation
2620+----------------------------------------
2621+Make sure you have git-checkout'ed the correct branch.
2622+
0dcfbb52 2623+For aufs6-linux tree,
c06a8ce3 2624+- enable CONFIG_AUFS_FS.
1e00d052 2625+- set other aufs configurations if necessary.
46016270
AM
2626+- for aufs5.13 and later
2627+ Because aufs is not only an ordinary filesystem (callee of VFS), but
2628+ also a caller of VFS functions for branch filesystems, subclassing of
2629+ the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
2630+ feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
2631+ need to customize some LOCKDEP numbers. Here are what I use on my
2632+ test environment.
2633+ CONFIG_LOCKDEP_BITS=21
2634+ CONFIG_LOCKDEP_CHAINS_BITS=21
2635+ CONFIG_LOCKDEP_STACK_TRACE_BITS=24
1e00d052 2636+
0dcfbb52 2637+For aufs6-standalone tree,
53392da6
AM
2638+There are several ways to build.
2639+
2640+1.
0dcfbb52
AM
2641+- apply ./aufs6-kbuild.patch to your kernel source files.
2642+- apply ./aufs6-base.patch too.
2643+- apply ./aufs6-mmap.patch too.
2644+- apply ./aufs6-standalone.patch too, if you have a plan to set
2645+ CONFIG_AUFS_FS=m. otherwise you don't need ./aufs6-standalone.patch.
537831f9
AM
2646+- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2647+ kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
c06a8ce3 2648+- enable CONFIG_AUFS_FS, you can select either
53392da6
AM
2649+ =m or =y.
2650+- and build your kernel as usual.
2651+- install the built kernel.
7eafdf33
AM
2652+- install the header files too by "make headers_install" to the
2653+ directory where you specify. By default, it is $PWD/usr.
b4510431 2654+ "make help" shows a brief note for headers_install.
53392da6
AM
2655+- and reboot your system.
2656+
2657+2.
2658+- module only (CONFIG_AUFS_FS=m).
0dcfbb52
AM
2659+- apply ./aufs6-base.patch to your kernel source files.
2660+- apply ./aufs6-mmap.patch too.
2661+- apply ./aufs6-standalone.patch too.
53392da6
AM
2662+- build your kernel, don't forget "make headers_install", and reboot.
2663+- edit ./config.mk and set other aufs configurations if necessary.
b4510431 2664+ Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
53392da6
AM
2665+ every aufs configurations.
2666+- build the module by simple "make".
2667+- you can specify ${KDIR} make variable which points to your kernel
2668+ source tree.
2669+- install the files
2670+ + run "make install" to install the aufs module, or copy the built
b4510431
AM
2671+ $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2672+ + run "make install_headers" (instead of headers_install) to install
2673+ the modified aufs header file (you can specify DESTDIR which is
2674+ available in aufs standalone version's Makefile only), or copy
2675+ $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2676+ you like manually. By default, the target directory is $PWD/usr.
0dcfbb52 2677+- no need to apply aufs6-kbuild.patch, nor copying source files to your
53392da6
AM
2678+ kernel source tree.
2679+
b4510431 2680+Note: The header file aufs_type.h is necessary to build aufs-util
53392da6
AM
2681+ as well as "make headers_install" in the kernel source tree.
2682+ headers_install is subject to be forgotten, but it is essentially
2683+ necessary, not only for building aufs-util.
2684+ You may not meet problems without headers_install in some older
2685+ version though.
2686+
2687+And then,
2688+- read README in aufs-util, build and install it
9dbd164d
AM
2689+- note that your distribution may contain an obsoleted version of
2690+ aufs_type.h in /usr/include/linux or something. When you build aufs
2691+ utilities, make sure that your compiler refers the correct aufs header
2692+ file which is built by "make headers_install."
53392da6
AM
2693+- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2694+ then run "make install_ulib" too. And refer to the aufs manual in
2695+ detail.
2696+
0dcfbb52 2697+There several other patches in aufs6-standalone.git. They are all
38d290e6 2698+optional. When you meet some problems, they will help you.
0dcfbb52 2699+- aufs6-loopback.patch
38d290e6
JR
2700+ Supports a nested loopback mount in a branch-fs. This patch is
2701+ unnecessary until aufs produces a message like "you may want to try
2702+ another patch for loopback file".
2703+- vfs-ino.patch
2704+ Modifies a system global kernel internal function get_next_ino() in
2705+ order to stop assigning 0 for an inode-number. Not directly related to
2706+ aufs, but recommended generally.
2707+- tmpfs-idr.patch
2708+ Keeps the tmpfs inode number as the lowest value. Effective to reduce
2709+ the size of aufs XINO files for tmpfs branch. Also it prevents the
2710+ duplication of inode number, which is important for backup tools and
2711+ other utilities. When you find aufs XINO files for tmpfs branch
2712+ growing too much, try this patch.
2713+
53392da6
AM
2714+
2715+4. Usage
2716+----------------------------------------
2717+At first, make sure aufs-util are installed, and please read the aufs
0dcfbb52
AM
2718+manual, aufs.6 in aufs-util.git tree.
2719+$ man -l aufs.6
53392da6
AM
2720+
2721+And then,
2722+$ mkdir /tmp/rw /tmp/aufs
2723+# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2724+
2725+Here is another example. The result is equivalent.
2726+# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2727+ Or
2728+# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2729+# mount -o remount,append:${HOME} /tmp/aufs
2730+
2731+Then, you can see whole tree of your home dir through /tmp/aufs. If
2732+you modify a file under /tmp/aufs, the one on your home directory is
2733+not affected, instead the same named file will be newly created under
2734+/tmp/rw. And all of your modification to a file will be applied to
2735+the one under /tmp/rw. This is called the file based Copy on Write
2736+(COW) method.
0dcfbb52 2737+Aufs mount options are described in aufs.6.
53392da6
AM
2738+If you run chroot or something and make your aufs as a root directory,
2739+then you need to customize the shutdown script. See the aufs manual in
2740+detail.
2741+
2742+Additionally, there are some sample usages of aufs which are a
2743+diskless system with network booting, and LiveCD over NFS.
2744+See sample dir in CVS tree on SourceForge.
2745+
2746+
2747+5. Contact
2748+----------------------------------------
2749+When you have any problems or strange behaviour in aufs, please let me
2750+know with:
2751+- /proc/mounts (instead of the output of mount(8))
2752+- /sys/module/aufs/*
2753+- /sys/fs/aufs/* (if you have them)
2754+- /debug/aufs/* (if you have them)
2755+- linux kernel version
2756+ if your kernel is not plain, for example modified by distributor,
2757+ the url where i can download its source is necessary too.
2758+- aufs version which was printed at loading the module or booting the
2759+ system, instead of the date you downloaded.
2760+- configuration (define/undefine CONFIG_AUFS_xxx)
2761+- kernel configuration or /proc/config.gz (if you have it)
43982f53 2762+- LSM (linux security module, if you are using)
53392da6
AM
2763+- behaviour which you think to be incorrect
2764+- actual operation, reproducible one is better
2765+- mailto: aufs-users at lists.sourceforge.net
2766+
2767+Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
2768+and Feature Requests) on SourceForge. Please join and write to
2769+aufs-users ML.
2770+
2771+
2772+6. Acknowledgements
2773+----------------------------------------
2774+Thanks to everyone who have tried and are using aufs, whoever
2775+have reported a bug or any feedback.
2776+
2777+Especially donators:
2778+Tomas Matejicek(slax.org) made a donation (much more than once).
2779+ Since Apr 2010, Tomas M (the author of Slax and Linux Live
2780+ scripts) is making "doubling" donations.
2781+ Unfortunately I cannot list all of the donators, but I really
b4510431 2782+ appreciate.
53392da6
AM
2783+ It ends Aug 2010, but the ordinary donation URL is still available.
2784+ <http://sourceforge.net/donate/index.php?group_id=167503>
2785+Dai Itasaka made a donation (2007/8).
2786+Chuck Smith made a donation (2008/4, 10 and 12).
2787+Henk Schoneveld made a donation (2008/9).
2788+Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
2789+Francois Dupoux made a donation (2008/11).
2790+Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
2791+ aufs2 GIT tree (2009/2).
2792+William Grant made a donation (2009/3).
2793+Patrick Lane made a donation (2009/4).
2794+The Mail Archive (mail-archive.com) made donations (2009/5).
2795+Nippy Networks (Ed Wildgoose) made a donation (2009/7).
2796+New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
2797+Pavel Pronskiy made a donation (2011/2).
2798+Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
2799+ Networks (Ed Wildgoose) made a donation for hardware (2011/3).
537831f9
AM
2800+Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
2801+11).
1e00d052 2802+Sam Liddicott made a donation (2011/9).
86dc4139
AM
2803+Era Scarecrow made a donation (2013/4).
2804+Bor Ratajc made a donation (2013/4).
2805+Alessandro Gorreta made a donation (2013/4).
2806+POIRETTE Marc made a donation (2013/4).
2807+Alessandro Gorreta made a donation (2013/4).
2808+lauri kasvandik made a donation (2013/5).
392086de 2809+"pemasu from Finland" made a donation (2013/7).
523b37e3
AM
2810+The Parted Magic Project made a donation (2013/9 and 11).
2811+Pavel Barta made a donation (2013/10).
38d290e6 2812+Nikolay Pertsev made a donation (2014/5).
f4d37d76 2813+James B made a donation (2014/7, 2015/7, and 2021/12).
076b876e 2814+Stefano Di Biase made a donation (2014/8).
2000de60 2815+Daniel Epellei made a donation (2015/1).
c4adf169 2816+OmegaPhil made a donation (2016/1, 2018/4).
5afbbe0d 2817+Tomasz Szewczyk made a donation (2016/4).
f2c43d5f 2818+James Burry made a donation (2016/12).
acd2b654 2819+Carsten Rose made a donation (2018/9).
9f237c51 2820+Porteus Kiosk made a donation (2018/10).
22319442 2821+Enya Quetzalli Gomez Rodriguez made a donation (2022/5).
53392da6
AM
2822+
2823+Thank you very much.
2824+Donations are always, including future donations, very important and
2825+helpful for me to keep on developing aufs.
2826+
2827+
2828+7.
2829+----------------------------------------
2830+If you are an experienced user, no explanation is needed. Aufs is
2831+just a linux filesystem.
2832+
2833+
2834+Enjoy!
2835+
2836+# Local variables: ;
2837+# mode: text;
2838+# End: ;
7f207e10 2839diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
eca34b5c 2840--- /usr/share/empty/fs/aufs/aufs.h 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 2841+++ linux/fs/aufs/aufs.h 2022-11-05 23:02:18.959222617 +0100
acd2b654 2842@@ -0,0 +1,62 @@
062440b3 2843+/* SPDX-License-Identifier: GPL-2.0 */
7f207e10 2844+/*
0dcfbb52 2845+ * Copyright (C) 2005-2022 Junjiro R. Okajima
7f207e10 2846+ *
0dcfbb52 2847+ * This program is free software; you can redistribute it and/or modify
7f207e10
AM
2848+ * it under the terms of the GNU General Public License as published by
2849+ * the Free Software Foundation; either version 2 of the License, or
2850+ * (at your option) any later version.
2851+ *
2852+ * This program is distributed in the hope that it will be useful,
2853+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
2854+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2855+ * GNU General Public License for more details.
2856+ *
2857+ * You should have received a copy of the GNU General Public License
523b37e3 2858+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7f207e10
AM
2859+ */
2860+
2861+/*
2862+ * all header files
2863+ */
2864+
2865+#ifndef __AUFS_H__
2866+#define __AUFS_H__
2867+
2868+#ifdef __KERNEL__
2869+
2870+#define AuStub(type, name, body, ...) \
2871+ static inline type name(__VA_ARGS__) { body; }
2872+
2873+#define AuStubVoid(name, ...) \
2874+ AuStub(void, name, , __VA_ARGS__)
2875+#define AuStubInt0(name, ...) \
2876+ AuStub(int, name, return 0, __VA_ARGS__)
2877+
2878+#include "debug.h"
2879+
2880+#include "branch.h"
2881+#include "cpup.h"
2882+#include "dcsub.h"
2883+#include "dbgaufs.h"
2884+#include "dentry.h"
2885+#include "dir.h"
8b6a4947 2886+#include "dirren.h"
7f207e10
AM
2887+#include "dynop.h"
2888+#include "file.h"
2889+#include "fstype.h"
8b6a4947 2890+#include "hbl.h"
7f207e10 2891+#include "inode.h"
acd2b654 2892+#include "lcnt.h"
7f207e10
AM
2893+#include "loop.h"
2894+#include "module.h"
7f207e10
AM
2895+#include "opts.h"
2896+#include "rwsem.h"
7f207e10
AM
2897+#include "super.h"
2898+#include "sysaufs.h"
2899+#include "vfsub.h"
2900+#include "whout.h"
2901+#include "wkq.h"
2902+
2903+#endif /* __KERNEL__ */
2904+#endif /* __AUFS_H__ */
2905diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
eca34b5c 2906--- /usr/share/empty/fs/aufs/branch.c 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 2907+++ linux/fs/aufs/branch.c 2022-11-05 23:02:18.959222617 +0100
42b5c33a 2908@@ -0,0 +1,1427 @@
cd7a4cd9 2909+// SPDX-License-Identifier: GPL-2.0
7f207e10 2910+/*
0dcfbb52 2911+ * Copyright (C) 2005-2022 Junjiro R. Okajima
7f207e10 2912+ *
0dcfbb52 2913+ * This program is free software; you can redistribute it and/or modify
7f207e10
AM
2914+ * it under the terms of the GNU General Public License as published by
2915+ * the Free Software Foundation; either version 2 of the License, or
2916+ * (at your option) any later version.
2917+ *
2918+ * This program is distributed in the hope that it will be useful,
2919+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
2920+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2921+ * GNU General Public License for more details.
2922+ *
2923+ * You should have received a copy of the GNU General Public License
523b37e3 2924+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7f207e10
AM
2925+ */
2926+
2927+/*
2928+ * branch management
2929+ */
2930+
027c5e7a 2931+#include <linux/compat.h>
7f207e10
AM
2932+#include <linux/statfs.h>
2933+#include "aufs.h"
2934+
2935+/*
2936+ * free a single branch
1facf9fc 2937+ */
2938+static void au_br_do_free(struct au_branch *br)
2939+{
2940+ int i;
2941+ struct au_wbr *wbr;
4a4d8108 2942+ struct au_dykey **key;
1facf9fc 2943+
027c5e7a 2944+ au_hnotify_fin_br(br);
8b6a4947
AM
2945+ /* always, regardless the mount option */
2946+ au_dr_hino_free(&br->br_dirren);
062440b3 2947+ au_xino_put(br);
1facf9fc 2948+
acd2b654
AM
2949+ AuLCntZero(au_lcnt_read(&br->br_nfiles, /*do_rev*/0));
2950+ au_lcnt_fin(&br->br_nfiles, /*do_sync*/0);
2951+ AuLCntZero(au_lcnt_read(&br->br_count, /*do_rev*/0));
2952+ au_lcnt_fin(&br->br_count, /*do_sync*/0);
1facf9fc 2953+
2954+ wbr = br->br_wbr;
2955+ if (wbr) {
2956+ for (i = 0; i < AuBrWh_Last; i++)
2957+ dput(wbr->wbr_wh[i]);
2958+ AuDebugOn(atomic_read(&wbr->wbr_wh_running));
dece6358 2959+ AuRwDestroy(&wbr->wbr_wh_rwsem);
1facf9fc 2960+ }
2961+
076b876e
AM
2962+ if (br->br_fhsm) {
2963+ au_br_fhsm_fin(br->br_fhsm);
9f237c51 2964+ au_kfree_try_rcu(br->br_fhsm);
076b876e
AM
2965+ }
2966+
4a4d8108
AM
2967+ key = br->br_dykey;
2968+ for (i = 0; i < AuBrDynOp; i++, key++)
2969+ if (*key)
2970+ au_dy_put(*key);
2971+ else
2972+ break;
2973+
537831f9 2974+ /* recursive lock, s_umount of branch's */
acd2b654 2975+ /* synchronize_rcu(); */ /* why? */
537831f9 2976+ lockdep_off();
86dc4139 2977+ path_put(&br->br_path);
537831f9 2978+ lockdep_on();
9f237c51 2979+ au_kfree_rcu(wbr);
acd2b654
AM
2980+ au_lcnt_wait_for_fin(&br->br_nfiles);
2981+ au_lcnt_wait_for_fin(&br->br_count);
2982+ /* I don't know why, but percpu_refcount requires this */
2983+ /* synchronize_rcu(); */
9f237c51 2984+ au_kfree_rcu(br);
1facf9fc 2985+}
2986+
2987+/*
2988+ * frees all branches
2989+ */
2990+void au_br_free(struct au_sbinfo *sbinfo)
2991+{
2992+ aufs_bindex_t bmax;
2993+ struct au_branch **br;
2994+
dece6358
AM
2995+ AuRwMustWriteLock(&sbinfo->si_rwsem);
2996+
5afbbe0d 2997+ bmax = sbinfo->si_bbot + 1;
1facf9fc 2998+ br = sbinfo->si_branch;
2999+ while (bmax--)
3000+ au_br_do_free(*br++);
3001+}
3002+
3003+/*
3004+ * find the index of a branch which is specified by @br_id.
3005+ */
3006+int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
3007+{
5afbbe0d 3008+ aufs_bindex_t bindex, bbot;
1facf9fc 3009+
5afbbe0d
AM
3010+ bbot = au_sbbot(sb);
3011+ for (bindex = 0; bindex <= bbot; bindex++)
1facf9fc 3012+ if (au_sbr_id(sb, bindex) == br_id)
3013+ return bindex;
3014+ return -1;
3015+}
3016+
3017+/* ---------------------------------------------------------------------- */
3018+
3019+/*
3020+ * add a branch
3021+ */
3022+
b752ccd1
AM
3023+static int test_overlap(struct super_block *sb, struct dentry *h_adding,
3024+ struct dentry *h_root)
1facf9fc 3025+{
b752ccd1
AM
3026+ if (unlikely(h_adding == h_root
3027+ || au_test_loopback_overlap(sb, h_adding)))
1facf9fc 3028+ return 1;
b752ccd1
AM
3029+ if (h_adding->d_sb != h_root->d_sb)
3030+ return 0;
3031+ return au_test_subdir(h_adding, h_root)
3032+ || au_test_subdir(h_root, h_adding);
1facf9fc 3033+}
3034+
3035+/*
3036+ * returns a newly allocated branch. @new_nbranch is a number of branches
3037+ * after adding a branch.
3038+ */
3039+static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
3040+ int perm)
3041+{
3042+ struct au_branch *add_branch;
3043+ struct dentry *root;
5527c038 3044+ struct inode *inode;
4a4d8108 3045+ int err;
1facf9fc 3046+
4a4d8108 3047+ err = -ENOMEM;
be52b249 3048+ add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
1facf9fc 3049+ if (unlikely(!add_branch))
3050+ goto out;
acd2b654 3051+ add_branch->br_xino = au_xino_alloc(/*nfile*/1);
062440b3 3052+ if (unlikely(!add_branch->br_xino))
521ced18 3053+ goto out_br;
027c5e7a
AM
3054+ err = au_hnotify_init_br(add_branch, perm);
3055+ if (unlikely(err))
062440b3 3056+ goto out_xino;
027c5e7a 3057+
1facf9fc 3058+ if (au_br_writable(perm)) {
3059+ /* may be freed separately at changing the branch permission */
be52b249 3060+ add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
1facf9fc 3061+ GFP_NOFS);
3062+ if (unlikely(!add_branch->br_wbr))
027c5e7a 3063+ goto out_hnotify;
1facf9fc 3064+ }
3065+
076b876e
AM
3066+ if (au_br_fhsm(perm)) {
3067+ err = au_fhsm_br_alloc(add_branch);
3068+ if (unlikely(err))
3069+ goto out_wbr;
3070+ }
3071+
521ced18 3072+ root = sb->s_root;
e2f27e51 3073+ err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
4a4d8108 3074+ if (!err)
e2f27e51 3075+ err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
5527c038
JR
3076+ if (!err) {
3077+ inode = d_inode(root);
1c60b727
AM
3078+ err = au_hinode_realloc(au_ii(inode), new_nbranch,
3079+ /*may_shrink*/0);
5527c038 3080+ }
4a4d8108
AM
3081+ if (!err)
3082+ return add_branch; /* success */
1facf9fc 3083+
076b876e 3084+out_wbr:
9f237c51 3085+ au_kfree_rcu(add_branch->br_wbr);
027c5e7a
AM
3086+out_hnotify:
3087+ au_hnotify_fin_br(add_branch);
062440b3
AM
3088+out_xino:
3089+ au_xino_put(add_branch);
4f0767ce 3090+out_br:
9f237c51 3091+ au_kfree_rcu(add_branch);
4f0767ce 3092+out:
4a4d8108 3093+ return ERR_PTR(err);
1facf9fc 3094+}
3095+
3096+/*
3097+ * test if the branch permission is legal or not.
3098+ */
3099+static int test_br(struct inode *inode, int brperm, char *path)
3100+{
3101+ int err;
3102+
4a4d8108
AM
3103+ err = (au_br_writable(brperm) && IS_RDONLY(inode));
3104+ if (!err)
3105+ goto out;
1facf9fc 3106+
4a4d8108
AM
3107+ err = -EINVAL;
3108+ pr_err("write permission for readonly mount or inode, %s\n", path);
3109+
4f0767ce 3110+out:
1facf9fc 3111+ return err;
3112+}
3113+
3114+/*
3115+ * returns:
3116+ * 0: success, the caller will add it
3117+ * plus: success, it is already unified, the caller should ignore it
3118+ * minus: error
3119+ */
3120+static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3121+{
3122+ int err;
5afbbe0d 3123+ aufs_bindex_t bbot, bindex;
5527c038 3124+ struct dentry *root, *h_dentry;
1facf9fc 3125+ struct inode *inode, *h_inode;
3126+
3127+ root = sb->s_root;
5afbbe0d
AM
3128+ bbot = au_sbbot(sb);
3129+ if (unlikely(bbot >= 0
1facf9fc 3130+ && au_find_dbindex(root, add->path.dentry) >= 0)) {
3131+ err = 1;
3132+ if (!remount) {
3133+ err = -EINVAL;
4a4d8108 3134+ pr_err("%s duplicated\n", add->pathname);
1facf9fc 3135+ }
3136+ goto out;
3137+ }
3138+
3139+ err = -ENOSPC; /* -E2BIG; */
3140+ if (unlikely(AUFS_BRANCH_MAX <= add->bindex
5afbbe0d 3141+ || AUFS_BRANCH_MAX - 1 <= bbot)) {
4a4d8108 3142+ pr_err("number of branches exceeded %s\n", add->pathname);
1facf9fc 3143+ goto out;
3144+ }
3145+
3146+ err = -EDOM;
5afbbe0d 3147+ if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
4a4d8108 3148+ pr_err("bad index %d\n", add->bindex);
1facf9fc 3149+ goto out;
3150+ }
3151+
5527c038 3152+ inode = d_inode(add->path.dentry);
1facf9fc 3153+ err = -ENOENT;
3154+ if (unlikely(!inode->i_nlink)) {
4a4d8108 3155+ pr_err("no existence %s\n", add->pathname);
1facf9fc 3156+ goto out;
3157+ }
3158+
3159+ err = -EINVAL;
3160+ if (unlikely(inode->i_sb == sb)) {
4a4d8108 3161+ pr_err("%s must be outside\n", add->pathname);
1facf9fc 3162+ goto out;
3163+ }
3164+
3165+ if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
4a4d8108
AM
3166+ pr_err("unsupported filesystem, %s (%s)\n",
3167+ add->pathname, au_sbtype(inode->i_sb));
1facf9fc 3168+ goto out;
3169+ }
3170+
c1595e42
JR
3171+ if (unlikely(inode->i_sb->s_stack_depth)) {
3172+ pr_err("already stacked, %s (%s)\n",
3173+ add->pathname, au_sbtype(inode->i_sb));
3174+ goto out;
3175+ }
3176+
5527c038 3177+ err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
1facf9fc 3178+ if (unlikely(err))
3179+ goto out;
3180+
5afbbe0d 3181+ if (bbot < 0)
1facf9fc 3182+ return 0; /* success */
3183+
3184+ err = -EINVAL;
5afbbe0d 3185+ for (bindex = 0; bindex <= bbot; bindex++)
1facf9fc 3186+ if (unlikely(test_overlap(sb, add->path.dentry,
3187+ au_h_dptr(root, bindex)))) {
4a4d8108 3188+ pr_err("%s is overlapped\n", add->pathname);
1facf9fc 3189+ goto out;
3190+ }
3191+
3192+ err = 0;
3193+ if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
5527c038
JR
3194+ h_dentry = au_h_dptr(root, 0);
3195+ h_inode = d_inode(h_dentry);
1facf9fc 3196+ if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
0c3ec466
AM
3197+ || !uid_eq(h_inode->i_uid, inode->i_uid)
3198+ || !gid_eq(h_inode->i_gid, inode->i_gid))
3199+ pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3200+ add->pathname,
3201+ i_uid_read(inode), i_gid_read(inode),
3202+ (inode->i_mode & S_IALLUGO),
3203+ i_uid_read(h_inode), i_gid_read(h_inode),
3204+ (h_inode->i_mode & S_IALLUGO));
1facf9fc 3205+ }
3206+
4f0767ce 3207+out:
1facf9fc 3208+ return err;
3209+}
3210+
3211+/*
3212+ * initialize or clean the whiteouts for an adding branch
3213+ */
3214+static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
86dc4139 3215+ int new_perm)
1facf9fc 3216+{
3217+ int err, old_perm;
3218+ aufs_bindex_t bindex;
febd17d6 3219+ struct inode *h_inode;
1facf9fc 3220+ struct au_wbr *wbr;
3221+ struct au_hinode *hdir;
5527c038 3222+ struct dentry *h_dentry;
1facf9fc 3223+
86dc4139
AM
3224+ err = vfsub_mnt_want_write(au_br_mnt(br));
3225+ if (unlikely(err))
3226+ goto out;
3227+
1facf9fc 3228+ wbr = br->br_wbr;
3229+ old_perm = br->br_perm;
3230+ br->br_perm = new_perm;
3231+ hdir = NULL;
febd17d6 3232+ h_inode = NULL;
1facf9fc 3233+ bindex = au_br_index(sb, br->br_id);
3234+ if (0 <= bindex) {
5527c038 3235+ hdir = au_hi(d_inode(sb->s_root), bindex);
5afbbe0d 3236+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 3237+ } else {
5527c038 3238+ h_dentry = au_br_dentry(br);
febd17d6
JR
3239+ h_inode = d_inode(h_dentry);
3240+ inode_lock_nested(h_inode, AuLsc_I_PARENT);
1facf9fc 3241+ }
3242+ if (!wbr)
86dc4139 3243+ err = au_wh_init(br, sb);
1facf9fc 3244+ else {
3245+ wbr_wh_write_lock(wbr);
86dc4139 3246+ err = au_wh_init(br, sb);
1facf9fc 3247+ wbr_wh_write_unlock(wbr);
3248+ }
3249+ if (hdir)
5afbbe0d 3250+ au_hn_inode_unlock(hdir);
1facf9fc 3251+ else
febd17d6 3252+ inode_unlock(h_inode);
86dc4139 3253+ vfsub_mnt_drop_write(au_br_mnt(br));
1facf9fc 3254+ br->br_perm = old_perm;
3255+
3256+ if (!err && wbr && !au_br_writable(new_perm)) {
9f237c51 3257+ au_kfree_rcu(wbr);
1facf9fc 3258+ br->br_wbr = NULL;
3259+ }
3260+
86dc4139 3261+out:
1facf9fc 3262+ return err;
3263+}
3264+
3265+static int au_wbr_init(struct au_branch *br, struct super_block *sb,
86dc4139 3266+ int perm)
1facf9fc 3267+{
3268+ int err;
4a4d8108 3269+ struct kstatfs kst;
1facf9fc 3270+ struct au_wbr *wbr;
3271+
3272+ wbr = br->br_wbr;
dece6358 3273+ au_rw_init(&wbr->wbr_wh_rwsem);
1facf9fc 3274+ atomic_set(&wbr->wbr_wh_running, 0);
1facf9fc 3275+
4a4d8108
AM
3276+ /*
3277+ * a limit for rmdir/rename a dir
523b37e3 3278+ * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
4a4d8108 3279+ */
86dc4139 3280+ err = vfs_statfs(&br->br_path, &kst);
4a4d8108
AM
3281+ if (unlikely(err))
3282+ goto out;
3283+ err = -EINVAL;
3284+ if (kst.f_namelen >= NAME_MAX)
86dc4139 3285+ err = au_br_init_wh(sb, br, perm);
4a4d8108 3286+ else
523b37e3
AM
3287+ pr_err("%pd(%s), unsupported namelen %ld\n",
3288+ au_br_dentry(br),
86dc4139 3289+ au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
1facf9fc 3290+
4f0767ce 3291+out:
1facf9fc 3292+ return err;
3293+}
3294+
c1595e42 3295+/* initialize a new branch */
1facf9fc 3296+static int au_br_init(struct au_branch *br, struct super_block *sb,
3297+ struct au_opt_add *add)
3298+{
3299+ int err;
062440b3
AM
3300+ struct au_branch *brbase;
3301+ struct file *xf;
5527c038 3302+ struct inode *h_inode;
1facf9fc 3303+
3304+ err = 0;
1facf9fc 3305+ br->br_perm = add->perm;
86dc4139 3306+ br->br_path = add->path; /* set first, path_get() later */
4a4d8108 3307+ spin_lock_init(&br->br_dykey_lock);
acd2b654
AM
3308+ au_lcnt_init(&br->br_nfiles, /*release*/NULL);
3309+ au_lcnt_init(&br->br_count, /*release*/NULL);
1facf9fc 3310+ br->br_id = au_new_br_id(sb);
7f207e10 3311+ AuDebugOn(br->br_id < 0);
1facf9fc 3312+
8b6a4947
AM
3313+ /* always, regardless the given option */
3314+ err = au_dr_br_init(sb, br, &add->path);
3315+ if (unlikely(err))
3316+ goto out_err;
3317+
1facf9fc 3318+ if (au_br_writable(add->perm)) {
86dc4139 3319+ err = au_wbr_init(br, sb, add->perm);
1facf9fc 3320+ if (unlikely(err))
b752ccd1 3321+ goto out_err;
1facf9fc 3322+ }
3323+
3324+ if (au_opt_test(au_mntflags(sb), XINO)) {
062440b3 3325+ brbase = au_sbr(sb, 0);
acd2b654 3326+ xf = au_xino_file(brbase->br_xino, /*idx*/-1);
062440b3 3327+ AuDebugOn(!xf);
5527c038 3328+ h_inode = d_inode(add->path.dentry);
062440b3 3329+ err = au_xino_init_br(sb, br, h_inode->i_ino, &xf->f_path);
1facf9fc 3330+ if (unlikely(err)) {
acd2b654 3331+ AuDebugOn(au_xino_file(br->br_xino, /*idx*/-1));
b752ccd1 3332+ goto out_err;
1facf9fc 3333+ }
3334+ }
3335+
3336+ sysaufs_br_init(br);
86dc4139 3337+ path_get(&br->br_path);
b752ccd1 3338+ goto out; /* success */
1facf9fc 3339+
4f0767ce 3340+out_err:
86dc4139 3341+ memset(&br->br_path, 0, sizeof(br->br_path));
4f0767ce 3342+out:
1facf9fc 3343+ return err;
3344+}
3345+
3346+static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
5afbbe0d 3347+ struct au_branch *br, aufs_bindex_t bbot,
1facf9fc 3348+ aufs_bindex_t amount)
3349+{
3350+ struct au_branch **brp;
3351+
dece6358
AM
3352+ AuRwMustWriteLock(&sbinfo->si_rwsem);
3353+
1facf9fc 3354+ brp = sbinfo->si_branch + bindex;
3355+ memmove(brp + 1, brp, sizeof(*brp) * amount);
3356+ *brp = br;
5afbbe0d
AM
3357+ sbinfo->si_bbot++;
3358+ if (unlikely(bbot < 0))
3359+ sbinfo->si_bbot = 0;
1facf9fc 3360+}
3361+
3362+static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
5afbbe0d 3363+ aufs_bindex_t bbot, aufs_bindex_t amount)
1facf9fc 3364+{
3365+ struct au_hdentry *hdp;
3366+
1308ab2a 3367+ AuRwMustWriteLock(&dinfo->di_rwsem);
3368+
5afbbe0d 3369+ hdp = au_hdentry(dinfo, bindex);
1facf9fc 3370+ memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3371+ au_h_dentry_init(hdp);
5afbbe0d
AM
3372+ dinfo->di_bbot++;
3373+ if (unlikely(bbot < 0))
3374+ dinfo->di_btop = 0;
1facf9fc 3375+}
3376+
3377+static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
5afbbe0d 3378+ aufs_bindex_t bbot, aufs_bindex_t amount)
1facf9fc 3379+{
3380+ struct au_hinode *hip;
3381+
1308ab2a 3382+ AuRwMustWriteLock(&iinfo->ii_rwsem);
3383+
5afbbe0d 3384+ hip = au_hinode(iinfo, bindex);
1facf9fc 3385+ memmove(hip + 1, hip, sizeof(*hip) * amount);
5afbbe0d
AM
3386+ au_hinode_init(hip);
3387+ iinfo->ii_bbot++;
3388+ if (unlikely(bbot < 0))
3389+ iinfo->ii_btop = 0;
1facf9fc 3390+}
3391+
86dc4139
AM
3392+static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3393+ aufs_bindex_t bindex)
1facf9fc 3394+{
86dc4139 3395+ struct dentry *root, *h_dentry;
5527c038 3396+ struct inode *root_inode, *h_inode;
5afbbe0d 3397+ aufs_bindex_t bbot, amount;
1facf9fc 3398+
3399+ root = sb->s_root;
5527c038 3400+ root_inode = d_inode(root);
5afbbe0d
AM
3401+ bbot = au_sbbot(sb);
3402+ amount = bbot + 1 - bindex;
86dc4139 3403+ h_dentry = au_br_dentry(br);
53392da6 3404+ au_sbilist_lock();
5afbbe0d
AM
3405+ au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
3406+ au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
3407+ au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
1facf9fc 3408+ au_set_h_dptr(root, bindex, dget(h_dentry));
5527c038
JR
3409+ h_inode = d_inode(h_dentry);
3410+ au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
53392da6 3411+ au_sbilist_unlock();
1facf9fc 3412+}
3413+
3414+int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3415+{
3416+ int err;
5afbbe0d 3417+ aufs_bindex_t bbot, add_bindex;
1facf9fc 3418+ struct dentry *root, *h_dentry;
3419+ struct inode *root_inode;
3420+ struct au_branch *add_branch;
3421+
3422+ root = sb->s_root;
5527c038 3423+ root_inode = d_inode(root);
1facf9fc 3424+ IMustLock(root_inode);
5afbbe0d 3425+ IiMustWriteLock(root_inode);
1facf9fc 3426+ err = test_add(sb, add, remount);
3427+ if (unlikely(err < 0))
3428+ goto out;
3429+ if (err) {
3430+ err = 0;
3431+ goto out; /* success */
3432+ }
3433+
5afbbe0d
AM
3434+ bbot = au_sbbot(sb);
3435+ add_branch = au_br_alloc(sb, bbot + 2, add->perm);
1facf9fc 3436+ err = PTR_ERR(add_branch);
3437+ if (IS_ERR(add_branch))
3438+ goto out;
3439+
3440+ err = au_br_init(add_branch, sb, add);
3441+ if (unlikely(err)) {
3442+ au_br_do_free(add_branch);
3443+ goto out;
3444+ }
3445+
3446+ add_bindex = add->bindex;
062440b3
AM
3447+ sysaufs_brs_del(sb, add_bindex); /* remove successors */
3448+ au_br_do_add(sb, add_branch, add_bindex);
3449+ sysaufs_brs_add(sb, add_bindex); /* append successors */
3450+ dbgaufs_brs_add(sb, add_bindex, /*topdown*/0); /* rename successors */
1facf9fc 3451+
86dc4139 3452+ h_dentry = add->path.dentry;
1308ab2a 3453+ if (!add_bindex) {
1facf9fc 3454+ au_cpup_attr_all(root_inode, /*force*/1);
1308ab2a 3455+ sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3456+ } else
5527c038 3457+ au_add_nlink(root_inode, d_inode(h_dentry));
1facf9fc 3458+
4f0767ce 3459+out:
1facf9fc 3460+ return err;
3461+}
3462+
3463+/* ---------------------------------------------------------------------- */
3464+
79b8bda9 3465+static unsigned long long au_farray_cb(struct super_block *sb, void *a,
076b876e
AM
3466+ unsigned long long max __maybe_unused,
3467+ void *arg)
3468+{
3469+ unsigned long long n;
3470+ struct file **p, *f;
8b6a4947
AM
3471+ struct hlist_bl_head *files;
3472+ struct hlist_bl_node *pos;
076b876e 3473+ struct au_finfo *finfo;
076b876e
AM
3474+
3475+ n = 0;
3476+ p = a;
3477+ files = &au_sbi(sb)->si_files;
8b6a4947
AM
3478+ hlist_bl_lock(files);
3479+ hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
076b876e
AM
3480+ f = finfo->fi_file;
3481+ if (file_count(f)
3482+ && !special_file(file_inode(f)->i_mode)) {
3483+ get_file(f);
3484+ *p++ = f;
3485+ n++;
3486+ AuDebugOn(n > max);
3487+ }
3488+ }
8b6a4947 3489+ hlist_bl_unlock(files);
076b876e
AM
3490+
3491+ return n;
3492+}
3493+
3494+static struct file **au_farray_alloc(struct super_block *sb,
3495+ unsigned long long *max)
3496+{
acd2b654
AM
3497+ struct au_sbinfo *sbi;
3498+
3499+ sbi = au_sbi(sb);
3500+ *max = au_lcnt_read(&sbi->si_nfiles, /*do_rev*/1);
79b8bda9 3501+ return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
076b876e
AM
3502+}
3503+
3504+static void au_farray_free(struct file **a, unsigned long long max)
3505+{
3506+ unsigned long long ull;
3507+
3508+ for (ull = 0; ull < max; ull++)
3509+ if (a[ull])
3510+ fput(a[ull]);
be52b249 3511+ kvfree(a);
076b876e
AM
3512+}
3513+
3514+/* ---------------------------------------------------------------------- */
3515+
1facf9fc 3516+/*
3517+ * delete a branch
3518+ */
3519+
3520+/* to show the line number, do not make it inlined function */
4a4d8108 3521+#define AuVerbose(do_info, fmt, ...) do { \
1facf9fc 3522+ if (do_info) \
4a4d8108 3523+ pr_info(fmt, ##__VA_ARGS__); \
1facf9fc 3524+} while (0)
3525+
5afbbe0d
AM
3526+static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
3527+ aufs_bindex_t bbot)
027c5e7a 3528+{
5afbbe0d 3529+ return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
027c5e7a
AM
3530+}
3531+
5afbbe0d
AM
3532+static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
3533+ aufs_bindex_t bbot)
027c5e7a 3534+{
5afbbe0d 3535+ return au_test_ibusy(d_inode(dentry), btop, bbot);
027c5e7a
AM
3536+}
3537+
1facf9fc 3538+/*
3539+ * test if the branch is deletable or not.
3540+ */
3541+static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
b752ccd1 3542+ unsigned int sigen, const unsigned int verbose)
1facf9fc 3543+{
3544+ int err, i, j, ndentry;
5afbbe0d 3545+ aufs_bindex_t btop, bbot;
1facf9fc 3546+ struct au_dcsub_pages dpages;
3547+ struct au_dpage *dpage;
3548+ struct dentry *d;
1facf9fc 3549+
3550+ err = au_dpages_init(&dpages, GFP_NOFS);
3551+ if (unlikely(err))
3552+ goto out;
3553+ err = au_dcsub_pages(&dpages, root, NULL, NULL);
3554+ if (unlikely(err))
3555+ goto out_dpages;
3556+
1facf9fc 3557+ for (i = 0; !err && i < dpages.ndpage; i++) {
3558+ dpage = dpages.dpages + i;
3559+ ndentry = dpage->ndentry;
3560+ for (j = 0; !err && j < ndentry; j++) {
3561+ d = dpage->dentries[j];
c1595e42 3562+ AuDebugOn(au_dcount(d) <= 0);
027c5e7a 3563+ if (!au_digen_test(d, sigen)) {
1facf9fc 3564+ di_read_lock_child(d, AuLock_IR);
027c5e7a
AM
3565+ if (unlikely(au_dbrange_test(d))) {
3566+ di_read_unlock(d, AuLock_IR);
3567+ continue;
3568+ }
3569+ } else {
1facf9fc 3570+ di_write_lock_child(d);
027c5e7a
AM
3571+ if (unlikely(au_dbrange_test(d))) {
3572+ di_write_unlock(d);
3573+ continue;
3574+ }
1facf9fc 3575+ err = au_reval_dpath(d, sigen);
3576+ if (!err)
3577+ di_downgrade_lock(d, AuLock_IR);
3578+ else {
3579+ di_write_unlock(d);
3580+ break;
3581+ }
3582+ }
3583+
027c5e7a 3584+ /* AuDbgDentry(d); */
5afbbe0d
AM
3585+ btop = au_dbtop(d);
3586+ bbot = au_dbbot(d);
3587+ if (btop <= bindex
3588+ && bindex <= bbot
1facf9fc 3589+ && au_h_dptr(d, bindex)
5afbbe0d 3590+ && au_test_dbusy(d, btop, bbot)) {
1facf9fc 3591+ err = -EBUSY;
523b37e3 3592+ AuVerbose(verbose, "busy %pd\n", d);
027c5e7a 3593+ AuDbgDentry(d);
1facf9fc 3594+ }
3595+ di_read_unlock(d, AuLock_IR);
3596+ }
3597+ }
3598+
4f0767ce 3599+out_dpages:
1facf9fc 3600+ au_dpages_free(&dpages);
4f0767ce 3601+out:
1facf9fc 3602+ return err;
3603+}
3604+
3605+static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
b752ccd1 3606+ unsigned int sigen, const unsigned int verbose)
1facf9fc 3607+{
3608+ int err;
7f207e10
AM
3609+ unsigned long long max, ull;
3610+ struct inode *i, **array;
5afbbe0d 3611+ aufs_bindex_t btop, bbot;
1facf9fc 3612+
7f207e10
AM
3613+ array = au_iarray_alloc(sb, &max);
3614+ err = PTR_ERR(array);
3615+ if (IS_ERR(array))
3616+ goto out;
3617+
1facf9fc 3618+ err = 0;
7f207e10
AM
3619+ AuDbg("b%d\n", bindex);
3620+ for (ull = 0; !err && ull < max; ull++) {
3621+ i = array[ull];
076b876e
AM
3622+ if (unlikely(!i))
3623+ break;
7f207e10 3624+ if (i->i_ino == AUFS_ROOT_INO)
1facf9fc 3625+ continue;
3626+
7f207e10 3627+ /* AuDbgInode(i); */
537831f9 3628+ if (au_iigen(i, NULL) == sigen)
1facf9fc 3629+ ii_read_lock_child(i);
3630+ else {
3631+ ii_write_lock_child(i);
027c5e7a
AM
3632+ err = au_refresh_hinode_self(i);
3633+ au_iigen_dec(i);
1facf9fc 3634+ if (!err)
3635+ ii_downgrade_lock(i);
3636+ else {
3637+ ii_write_unlock(i);
3638+ break;
3639+ }
3640+ }
3641+
5afbbe0d
AM
3642+ btop = au_ibtop(i);
3643+ bbot = au_ibbot(i);
3644+ if (btop <= bindex
3645+ && bindex <= bbot
1facf9fc 3646+ && au_h_iptr(i, bindex)
5afbbe0d 3647+ && au_test_ibusy(i, btop, bbot)) {
1facf9fc 3648+ err = -EBUSY;
3649+ AuVerbose(verbose, "busy i%lu\n", i->i_ino);
7f207e10 3650+ AuDbgInode(i);
1facf9fc 3651+ }
3652+ ii_read_unlock(i);
3653+ }
7f207e10 3654+ au_iarray_free(array, max);
1facf9fc 3655+
7f207e10 3656+out:
1facf9fc 3657+ return err;
3658+}
3659+
b752ccd1
AM
3660+static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3661+ const unsigned int verbose)
1facf9fc 3662+{
3663+ int err;
3664+ unsigned int sigen;
3665+
3666+ sigen = au_sigen(root->d_sb);
3667+ DiMustNoWaiters(root);
5527c038 3668+ IiMustNoWaiters(d_inode(root));
1facf9fc 3669+ di_write_unlock(root);
b752ccd1 3670+ err = test_dentry_busy(root, bindex, sigen, verbose);
1facf9fc 3671+ if (!err)
b752ccd1 3672+ err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
1facf9fc 3673+ di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3674+
3675+ return err;
3676+}
3677+
076b876e
AM
3678+static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3679+ struct file **to_free, int *idx)
3680+{
3681+ int err;
c1595e42 3682+ unsigned char matched, root;
5afbbe0d 3683+ aufs_bindex_t bindex, bbot;
076b876e
AM
3684+ struct au_fidir *fidir;
3685+ struct au_hfile *hfile;
3686+
3687+ err = 0;
2000de60 3688+ root = IS_ROOT(file->f_path.dentry);
c1595e42
JR
3689+ if (root) {
3690+ get_file(file);
3691+ to_free[*idx] = file;
3692+ (*idx)++;
3693+ goto out;
3694+ }
3695+
076b876e 3696+ matched = 0;
076b876e
AM
3697+ fidir = au_fi(file)->fi_hdir;
3698+ AuDebugOn(!fidir);
5afbbe0d
AM
3699+ bbot = au_fbbot_dir(file);
3700+ for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
076b876e
AM
3701+ hfile = fidir->fd_hfile + bindex;
3702+ if (!hfile->hf_file)
3703+ continue;
3704+
c1595e42 3705+ if (hfile->hf_br->br_id == br_id) {
076b876e 3706+ matched = 1;
076b876e 3707+ break;
c1595e42 3708+ }
076b876e 3709+ }
c1595e42 3710+ if (matched)
076b876e
AM
3711+ err = -EBUSY;
3712+
3713+out:
3714+ return err;
3715+}
3716+
3717+static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3718+ struct file **to_free, int opened)
3719+{
3720+ int err, idx;
3721+ unsigned long long ull, max;
5afbbe0d 3722+ aufs_bindex_t btop;
076b876e 3723+ struct file *file, **array;
076b876e
AM
3724+ struct dentry *root;
3725+ struct au_hfile *hfile;
3726+
3727+ array = au_farray_alloc(sb, &max);
3728+ err = PTR_ERR(array);
3729+ if (IS_ERR(array))
3730+ goto out;
3731+
3732+ err = 0;
3733+ idx = 0;
3734+ root = sb->s_root;
3735+ di_write_unlock(root);
3736+ for (ull = 0; ull < max; ull++) {
3737+ file = array[ull];
3738+ if (unlikely(!file))
3739+ break;
3740+
3741+ /* AuDbg("%pD\n", file); */
3742+ fi_read_lock(file);
5afbbe0d 3743+ btop = au_fbtop(file);
2000de60 3744+ if (!d_is_dir(file->f_path.dentry)) {
076b876e
AM
3745+ hfile = &au_fi(file)->fi_htop;
3746+ if (hfile->hf_br->br_id == br_id)
3747+ err = -EBUSY;
3748+ } else
3749+ err = test_dir_busy(file, br_id, to_free, &idx);
3750+ fi_read_unlock(file);
3751+ if (unlikely(err))
3752+ break;
3753+ }
3754+ di_write_lock_child(root);
3755+ au_farray_free(array, max);
3756+ AuDebugOn(idx > opened);
3757+
3758+out:
3759+ return err;
3760+}
3761+
3762+static void br_del_file(struct file **to_free, unsigned long long opened,
062440b3 3763+ aufs_bindex_t br_id)
076b876e
AM
3764+{
3765+ unsigned long long ull;
5afbbe0d 3766+ aufs_bindex_t bindex, btop, bbot, bfound;
076b876e
AM
3767+ struct file *file;
3768+ struct au_fidir *fidir;
3769+ struct au_hfile *hfile;
3770+
3771+ for (ull = 0; ull < opened; ull++) {
3772+ file = to_free[ull];
3773+ if (unlikely(!file))
3774+ break;
3775+
3776+ /* AuDbg("%pD\n", file); */
2000de60 3777+ AuDebugOn(!d_is_dir(file->f_path.dentry));
076b876e
AM
3778+ bfound = -1;
3779+ fidir = au_fi(file)->fi_hdir;
3780+ AuDebugOn(!fidir);
3781+ fi_write_lock(file);
5afbbe0d
AM
3782+ btop = au_fbtop(file);
3783+ bbot = au_fbbot_dir(file);
3784+ for (bindex = btop; bindex <= bbot; bindex++) {
076b876e
AM
3785+ hfile = fidir->fd_hfile + bindex;
3786+ if (!hfile->hf_file)
3787+ continue;
3788+
3789+ if (hfile->hf_br->br_id == br_id) {
3790+ bfound = bindex;
3791+ break;
3792+ }
3793+ }
3794+ AuDebugOn(bfound < 0);
3795+ au_set_h_fptr(file, bfound, NULL);
5afbbe0d
AM
3796+ if (bfound == btop) {
3797+ for (btop++; btop <= bbot; btop++)
3798+ if (au_hf_dir(file, btop)) {
3799+ au_set_fbtop(file, btop);
076b876e
AM
3800+ break;
3801+ }
3802+ }
3803+ fi_write_unlock(file);
3804+ }
3805+}
3806+
1facf9fc 3807+static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
3808+ const aufs_bindex_t bindex,
5afbbe0d 3809+ const aufs_bindex_t bbot)
1facf9fc 3810+{
3811+ struct au_branch **brp, **p;
3812+
dece6358
AM
3813+ AuRwMustWriteLock(&sbinfo->si_rwsem);
3814+
1facf9fc 3815+ brp = sbinfo->si_branch + bindex;
5afbbe0d
AM
3816+ if (bindex < bbot)
3817+ memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
3818+ sbinfo->si_branch[0 + bbot] = NULL;
3819+ sbinfo->si_bbot--;
1facf9fc 3820+
e2f27e51
AM
3821+ p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
3822+ /*may_shrink*/1);
1facf9fc 3823+ if (p)
3824+ sbinfo->si_branch = p;
4a4d8108 3825+ /* harmless error */
1facf9fc 3826+}
3827+
3828+static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
5afbbe0d 3829+ const aufs_bindex_t bbot)
1facf9fc 3830+{
3831+ struct au_hdentry *hdp, *p;
3832+
1308ab2a 3833+ AuRwMustWriteLock(&dinfo->di_rwsem);
3834+
5afbbe0d
AM
3835+ hdp = au_hdentry(dinfo, bindex);
3836+ if (bindex < bbot)
3837+ memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
3838+ /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
3839+ dinfo->di_bbot--;
1facf9fc 3840+
e2f27e51
AM
3841+ p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
3842+ /*may_shrink*/1);
1facf9fc 3843+ if (p)
3844+ dinfo->di_hdentry = p;
4a4d8108 3845+ /* harmless error */
1facf9fc 3846+}
3847+
3848+static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
5afbbe0d 3849+ const aufs_bindex_t bbot)
1facf9fc 3850+{
3851+ struct au_hinode *hip, *p;
3852+
1308ab2a 3853+ AuRwMustWriteLock(&iinfo->ii_rwsem);
3854+
5afbbe0d
AM
3855+ hip = au_hinode(iinfo, bindex);
3856+ if (bindex < bbot)
3857+ memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
3858+ /* au_hinode_init(au_hinode(iinfo, bbot)); */
3859+ iinfo->ii_bbot--;
1facf9fc 3860+
e2f27e51
AM
3861+ p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
3862+ /*may_shrink*/1);
1facf9fc 3863+ if (p)
3864+ iinfo->ii_hinode = p;
4a4d8108 3865+ /* harmless error */
1facf9fc 3866+}
3867+
3868+static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
3869+ struct au_branch *br)
3870+{
5afbbe0d 3871+ aufs_bindex_t bbot;
1facf9fc 3872+ struct au_sbinfo *sbinfo;
53392da6
AM
3873+ struct dentry *root, *h_root;
3874+ struct inode *inode, *h_inode;
3875+ struct au_hinode *hinode;
1facf9fc 3876+
dece6358
AM
3877+ SiMustWriteLock(sb);
3878+
1facf9fc 3879+ root = sb->s_root;
5527c038 3880+ inode = d_inode(root);
1facf9fc 3881+ sbinfo = au_sbi(sb);
5afbbe0d 3882+ bbot = sbinfo->si_bbot;
1facf9fc 3883+
53392da6
AM
3884+ h_root = au_h_dptr(root, bindex);
3885+ hinode = au_hi(inode, bindex);
3886+ h_inode = au_igrab(hinode->hi_inode);
3887+ au_hiput(hinode);
1facf9fc 3888+
53392da6 3889+ au_sbilist_lock();
5afbbe0d
AM
3890+ au_br_do_del_brp(sbinfo, bindex, bbot);
3891+ au_br_do_del_hdp(au_di(root), bindex, bbot);
3892+ au_br_do_del_hip(au_ii(inode), bindex, bbot);
53392da6
AM
3893+ au_sbilist_unlock();
3894+
8b6a4947
AM
3895+ /* ignore an error */
3896+ au_dr_br_fin(sb, br); /* always, regardless the mount option */
3897+
53392da6
AM
3898+ dput(h_root);
3899+ iput(h_inode);
3900+ au_br_do_free(br);
1facf9fc 3901+}
3902+
79b8bda9
AM
3903+static unsigned long long empty_cb(struct super_block *sb, void *array,
3904+ unsigned long long max, void *arg)
076b876e
AM
3905+{
3906+ return max;
3907+}
3908+
1facf9fc 3909+int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
3910+{
3911+ int err, rerr, i;
076b876e 3912+ unsigned long long opened;
1facf9fc 3913+ unsigned int mnt_flags;
5afbbe0d 3914+ aufs_bindex_t bindex, bbot, br_id;
1facf9fc 3915+ unsigned char do_wh, verbose;
3916+ struct au_branch *br;
3917+ struct au_wbr *wbr;
076b876e
AM
3918+ struct dentry *root;
3919+ struct file **to_free;
1facf9fc 3920+
3921+ err = 0;
076b876e
AM
3922+ opened = 0;
3923+ to_free = NULL;
3924+ root = sb->s_root;
3925+ bindex = au_find_dbindex(root, del->h_path.dentry);
1facf9fc 3926+ if (bindex < 0) {
3927+ if (remount)
3928+ goto out; /* success */
3929+ err = -ENOENT;
4a4d8108 3930+ pr_err("%s no such branch\n", del->pathname);
1facf9fc 3931+ goto out;
3932+ }
3933+ AuDbg("bindex b%d\n", bindex);
3934+
3935+ err = -EBUSY;
3936+ mnt_flags = au_mntflags(sb);
3937+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
5afbbe0d
AM
3938+ bbot = au_sbbot(sb);
3939+ if (unlikely(!bbot)) {
1facf9fc 3940+ AuVerbose(verbose, "no more branches left\n");
3941+ goto out;
3942+ }
acd2b654 3943+
1facf9fc 3944+ br = au_sbr(sb, bindex);
86dc4139 3945+ AuDebugOn(!path_equal(&br->br_path, &del->h_path));
acd2b654
AM
3946+ if (unlikely(au_lcnt_read(&br->br_count, /*do_rev*/1))) {
3947+ AuVerbose(verbose, "br %pd2 is busy now\n", del->h_path.dentry);
3948+ goto out;
3949+ }
076b876e
AM
3950+
3951+ br_id = br->br_id;
acd2b654 3952+ opened = au_lcnt_read(&br->br_nfiles, /*do_rev*/1);
076b876e 3953+ if (unlikely(opened)) {
79b8bda9 3954+ to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
076b876e
AM
3955+ err = PTR_ERR(to_free);
3956+ if (IS_ERR(to_free))
3957+ goto out;
3958+
3959+ err = test_file_busy(sb, br_id, to_free, opened);
3960+ if (unlikely(err)) {
3961+ AuVerbose(verbose, "%llu file(s) opened\n", opened);
3962+ goto out;
3963+ }
1facf9fc 3964+ }
3965+
3966+ wbr = br->br_wbr;
3967+ do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
3968+ if (do_wh) {
1308ab2a 3969+ /* instead of WbrWhMustWriteLock(wbr) */
3970+ SiMustWriteLock(sb);
1facf9fc 3971+ for (i = 0; i < AuBrWh_Last; i++) {
3972+ dput(wbr->wbr_wh[i]);
3973+ wbr->wbr_wh[i] = NULL;
3974+ }
3975+ }
3976+
076b876e 3977+ err = test_children_busy(root, bindex, verbose);
1facf9fc 3978+ if (unlikely(err)) {
3979+ if (do_wh)
3980+ goto out_wh;
3981+ goto out;
3982+ }
3983+
3984+ err = 0;
076b876e
AM
3985+ if (to_free) {
3986+ /*
3987+ * now we confirmed the branch is deletable.
3988+ * let's free the remaining opened dirs on the branch.
3989+ */
3990+ di_write_unlock(root);
3991+ br_del_file(to_free, opened, br_id);
3992+ di_write_lock_child(root);
3993+ }
3994+
062440b3
AM
3995+ sysaufs_brs_del(sb, bindex); /* remove successors */
3996+ dbgaufs_xino_del(br); /* remove one */
3997+ au_br_do_del(sb, bindex, br);
3998+ sysaufs_brs_add(sb, bindex); /* append successors */
3999+ dbgaufs_brs_add(sb, bindex, /*topdown*/1); /* rename successors */
1facf9fc 4000+
1308ab2a 4001+ if (!bindex) {
5527c038 4002+ au_cpup_attr_all(d_inode(root), /*force*/1);
1308ab2a 4003+ sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
4004+ } else
5527c038 4005+ au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
1facf9fc 4006+ if (au_opt_test(mnt_flags, PLINK))
4007+ au_plink_half_refresh(sb, br_id);
4008+
1facf9fc 4009+ goto out; /* success */
4010+
4f0767ce 4011+out_wh:
1facf9fc 4012+ /* revert */
86dc4139 4013+ rerr = au_br_init_wh(sb, br, br->br_perm);
1facf9fc 4014+ if (rerr)
0c3ec466
AM
4015+ pr_warn("failed re-creating base whiteout, %s. (%d)\n",
4016+ del->pathname, rerr);
4f0767ce 4017+out:
076b876e
AM
4018+ if (to_free)
4019+ au_farray_free(to_free, opened);
1facf9fc 4020+ return err;
4021+}
4022+
4023+/* ---------------------------------------------------------------------- */
4024+
027c5e7a
AM
4025+static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
4026+{
4027+ int err;
5afbbe0d 4028+ aufs_bindex_t btop, bbot;
027c5e7a
AM
4029+ struct aufs_ibusy ibusy;
4030+ struct inode *inode, *h_inode;
4031+
4032+ err = -EPERM;
4033+ if (unlikely(!capable(CAP_SYS_ADMIN)))
4034+ goto out;
4035+
4036+ err = copy_from_user(&ibusy, arg, sizeof(ibusy));
4037+ if (!err)
ba1aed25
AM
4038+ /* VERIFY_WRITE */
4039+ err = !access_ok(&arg->h_ino, sizeof(arg->h_ino));
027c5e7a
AM
4040+ if (unlikely(err)) {
4041+ err = -EFAULT;
4042+ AuTraceErr(err);
4043+ goto out;
4044+ }
4045+
4046+ err = -EINVAL;
4047+ si_read_lock(sb, AuLock_FLUSH);
5afbbe0d 4048+ if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
027c5e7a
AM
4049+ goto out_unlock;
4050+
4051+ err = 0;
4052+ ibusy.h_ino = 0; /* invalid */
4053+ inode = ilookup(sb, ibusy.ino);
4054+ if (!inode
4055+ || inode->i_ino == AUFS_ROOT_INO
5afbbe0d 4056+ || au_is_bad_inode(inode))
027c5e7a
AM
4057+ goto out_unlock;
4058+
4059+ ii_read_lock_child(inode);
5afbbe0d
AM
4060+ btop = au_ibtop(inode);
4061+ bbot = au_ibbot(inode);
4062+ if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
027c5e7a 4063+ h_inode = au_h_iptr(inode, ibusy.bindex);
5afbbe0d 4064+ if (h_inode && au_test_ibusy(inode, btop, bbot))
027c5e7a
AM
4065+ ibusy.h_ino = h_inode->i_ino;
4066+ }
4067+ ii_read_unlock(inode);
4068+ iput(inode);
4069+
4070+out_unlock:
4071+ si_read_unlock(sb);
4072+ if (!err) {
4073+ err = __put_user(ibusy.h_ino, &arg->h_ino);
4074+ if (unlikely(err)) {
4075+ err = -EFAULT;
4076+ AuTraceErr(err);
4077+ }
4078+ }
4079+out:
4080+ return err;
4081+}
4082+
4083+long au_ibusy_ioctl(struct file *file, unsigned long arg)
4084+{
2000de60 4085+ return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
027c5e7a
AM
4086+}
4087+
4088+#ifdef CONFIG_COMPAT
4089+long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
4090+{
2000de60 4091+ return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
027c5e7a
AM
4092+}
4093+#endif
4094+
4095+/* ---------------------------------------------------------------------- */
4096+
1facf9fc 4097+/*
4098+ * change a branch permission
4099+ */
4100+
dece6358
AM
4101+static void au_warn_ima(void)
4102+{
4103+#ifdef CONFIG_IMA
1308ab2a 4104+ /* since it doesn't support mark_files_ro() */
027c5e7a 4105+ AuWarn1("RW -> RO makes IMA to produce wrong message\n");
dece6358
AM
4106+#endif
4107+}
4108+
1facf9fc 4109+static int do_need_sigen_inc(int a, int b)
4110+{
4111+ return au_br_whable(a) && !au_br_whable(b);
4112+}
4113+
4114+static int need_sigen_inc(int old, int new)
4115+{
4116+ return do_need_sigen_inc(old, new)
4117+ || do_need_sigen_inc(new, old);
4118+}
4119+
4120+static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
4121+{
7f207e10 4122+ int err, do_warn;
027c5e7a 4123+ unsigned int mnt_flags;
7f207e10 4124+ unsigned long long ull, max;
e49829fe 4125+ aufs_bindex_t br_id;
38d290e6 4126+ unsigned char verbose, writer;
7f207e10 4127+ struct file *file, *hf, **array;
e49829fe 4128+ struct au_hfile *hfile;
eca34b5c 4129+ struct inode *h_inode;
1facf9fc 4130+
027c5e7a
AM
4131+ mnt_flags = au_mntflags(sb);
4132+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
4133+
7f207e10
AM
4134+ array = au_farray_alloc(sb, &max);
4135+ err = PTR_ERR(array);
4136+ if (IS_ERR(array))
1facf9fc 4137+ goto out;
4138+
7f207e10 4139+ do_warn = 0;
e49829fe 4140+ br_id = au_sbr_id(sb, bindex);
7f207e10
AM
4141+ for (ull = 0; ull < max; ull++) {
4142+ file = array[ull];
076b876e
AM
4143+ if (unlikely(!file))
4144+ break;
1facf9fc 4145+
523b37e3 4146+ /* AuDbg("%pD\n", file); */
1facf9fc 4147+ fi_read_lock(file);
4148+ if (unlikely(au_test_mmapped(file))) {
4149+ err = -EBUSY;
523b37e3 4150+ AuVerbose(verbose, "mmapped %pD\n", file);
7f207e10 4151+ AuDbgFile(file);
1facf9fc 4152+ FiMustNoWaiters(file);
4153+ fi_read_unlock(file);
7f207e10 4154+ goto out_array;
1facf9fc 4155+ }
4156+
e49829fe
JR
4157+ hfile = &au_fi(file)->fi_htop;
4158+ hf = hfile->hf_file;
7e9cd9fe 4159+ if (!d_is_reg(file->f_path.dentry)
1facf9fc 4160+ || !(file->f_mode & FMODE_WRITE)
e49829fe 4161+ || hfile->hf_br->br_id != br_id
7f207e10
AM
4162+ || !(hf->f_mode & FMODE_WRITE))
4163+ array[ull] = NULL;
4164+ else {
4165+ do_warn = 1;
4166+ get_file(file);
1facf9fc 4167+ }
4168+
1facf9fc 4169+ FiMustNoWaiters(file);
4170+ fi_read_unlock(file);
7f207e10
AM
4171+ fput(file);
4172+ }
1facf9fc 4173+
4174+ err = 0;
7f207e10 4175+ if (do_warn)
dece6358 4176+ au_warn_ima();
7f207e10
AM
4177+
4178+ for (ull = 0; ull < max; ull++) {
4179+ file = array[ull];
4180+ if (!file)
4181+ continue;
4182+
1facf9fc 4183+ /* todo: already flushed? */
523b37e3
AM
4184+ /*
4185+ * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4186+ * approach which resets f_mode and calls mnt_drop_write() and
4187+ * file_release_write() for each file, because the branch
4188+ * attribute in aufs world is totally different from the native
4189+ * fs rw/ro mode.
22319442 4190+ */
7f207e10
AM
4191+ /* fi_read_lock(file); */
4192+ hfile = &au_fi(file)->fi_htop;
4193+ hf = hfile->hf_file;
4194+ /* fi_read_unlock(file); */
027c5e7a 4195+ spin_lock(&hf->f_lock);
38d290e6
JR
4196+ writer = !!(hf->f_mode & FMODE_WRITER);
4197+ hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
027c5e7a 4198+ spin_unlock(&hf->f_lock);
38d290e6 4199+ if (writer) {
eca34b5c 4200+ h_inode = file_inode(hf);
42b5c33a
AM
4201+ if (hf->f_mode & FMODE_READ)
4202+ i_readcount_inc(h_inode);
eca34b5c 4203+ put_write_access(h_inode);
c06a8ce3 4204+ __mnt_drop_write(hf->f_path.mnt);
1facf9fc 4205+ }
4206+ }
4207+
7f207e10
AM
4208+out_array:
4209+ au_farray_free(array, max);
4f0767ce 4210+out:
7f207e10 4211+ AuTraceErr(err);
1facf9fc 4212+ return err;
4213+}
4214+
4215+int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
7f207e10 4216+ int *do_refresh)
1facf9fc 4217+{
4218+ int err, rerr;
4219+ aufs_bindex_t bindex;
4220+ struct dentry *root;
4221+ struct au_branch *br;
076b876e 4222+ struct au_br_fhsm *bf;
1facf9fc 4223+
4224+ root = sb->s_root;
1facf9fc 4225+ bindex = au_find_dbindex(root, mod->h_root);
4226+ if (bindex < 0) {
4227+ if (remount)
4228+ return 0; /* success */
4229+ err = -ENOENT;
4a4d8108 4230+ pr_err("%s no such branch\n", mod->path);
1facf9fc 4231+ goto out;
4232+ }
4233+ AuDbg("bindex b%d\n", bindex);
4234+
5527c038 4235+ err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
1facf9fc 4236+ if (unlikely(err))
4237+ goto out;
4238+
4239+ br = au_sbr(sb, bindex);
86dc4139 4240+ AuDebugOn(mod->h_root != au_br_dentry(br));
1facf9fc 4241+ if (br->br_perm == mod->perm)
4242+ return 0; /* success */
4243+
076b876e
AM
4244+ /* pre-allocate for non-fhsm --> fhsm */
4245+ bf = NULL;
4246+ if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4247+ err = au_fhsm_br_alloc(br);
4248+ if (unlikely(err))
4249+ goto out;
4250+ bf = br->br_fhsm;
4251+ br->br_fhsm = NULL;
4252+ }
4253+
1facf9fc 4254+ if (au_br_writable(br->br_perm)) {
4255+ /* remove whiteout base */
86dc4139 4256+ err = au_br_init_wh(sb, br, mod->perm);
1facf9fc 4257+ if (unlikely(err))
076b876e 4258+ goto out_bf;
1facf9fc 4259+
4260+ if (!au_br_writable(mod->perm)) {
4261+ /* rw --> ro, file might be mmapped */
4262+ DiMustNoWaiters(root);
5527c038 4263+ IiMustNoWaiters(d_inode(root));
1facf9fc 4264+ di_write_unlock(root);
4265+ err = au_br_mod_files_ro(sb, bindex);
4266+ /* aufs_write_lock() calls ..._child() */
4267+ di_write_lock_child(root);
4268+
4269+ if (unlikely(err)) {
4270+ rerr = -ENOMEM;
be52b249 4271+ br->br_wbr = kzalloc(sizeof(*br->br_wbr),
1facf9fc 4272+ GFP_NOFS);
86dc4139
AM
4273+ if (br->br_wbr)
4274+ rerr = au_wbr_init(br, sb, br->br_perm);
1facf9fc 4275+ if (unlikely(rerr)) {
4276+ AuIOErr("nested error %d (%d)\n",
4277+ rerr, err);
4278+ br->br_perm = mod->perm;
4279+ }
4280+ }
4281+ }
4282+ } else if (au_br_writable(mod->perm)) {
4283+ /* ro --> rw */
4284+ err = -ENOMEM;
be52b249 4285+ br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
1facf9fc 4286+ if (br->br_wbr) {
86dc4139 4287+ err = au_wbr_init(br, sb, mod->perm);
1facf9fc 4288+ if (unlikely(err)) {
9f237c51 4289+ au_kfree_rcu(br->br_wbr);
1facf9fc 4290+ br->br_wbr = NULL;
4291+ }
4292+ }
4293+ }
076b876e
AM
4294+ if (unlikely(err))
4295+ goto out_bf;
4296+
4297+ if (au_br_fhsm(br->br_perm)) {
4298+ if (!au_br_fhsm(mod->perm)) {
4299+ /* fhsm --> non-fhsm */
4300+ au_br_fhsm_fin(br->br_fhsm);
9f237c51 4301+ au_kfree_rcu(br->br_fhsm);
076b876e
AM
4302+ br->br_fhsm = NULL;
4303+ }
4304+ } else if (au_br_fhsm(mod->perm))
4305+ /* non-fhsm --> fhsm */
4306+ br->br_fhsm = bf;
4307+
076b876e
AM
4308+ *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4309+ br->br_perm = mod->perm;
4310+ goto out; /* success */
1facf9fc 4311+
076b876e 4312+out_bf:
9f237c51 4313+ au_kfree_try_rcu(bf);
076b876e
AM
4314+out:
4315+ AuTraceErr(err);
4316+ return err;
4317+}
4318+
4319+/* ---------------------------------------------------------------------- */
4320+
4321+int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4322+{
4323+ int err;
4324+ struct kstatfs kstfs;
4325+
4326+ err = vfs_statfs(&br->br_path, &kstfs);
1facf9fc 4327+ if (!err) {
076b876e
AM
4328+ stfs->f_blocks = kstfs.f_blocks;
4329+ stfs->f_bavail = kstfs.f_bavail;
4330+ stfs->f_files = kstfs.f_files;
4331+ stfs->f_ffree = kstfs.f_ffree;
1facf9fc 4332+ }
4333+
1facf9fc 4334+ return err;
4335+}
7f207e10 4336diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
eca34b5c 4337--- /usr/share/empty/fs/aufs/branch.h 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 4338+++ linux/fs/aufs/branch.h 2022-11-05 23:02:18.959222617 +0100
0b2a12c6 4339@@ -0,0 +1,375 @@
062440b3 4340+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 4341+/*
0dcfbb52 4342+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 4343+ *
0dcfbb52 4344+ * This program is free software; you can redistribute it and/or modify
1facf9fc 4345+ * it under the terms of the GNU General Public License as published by
4346+ * the Free Software Foundation; either version 2 of the License, or
4347+ * (at your option) any later version.
dece6358
AM
4348+ *
4349+ * This program is distributed in the hope that it will be useful,
4350+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
4351+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4352+ * GNU General Public License for more details.
4353+ *
4354+ * You should have received a copy of the GNU General Public License
523b37e3 4355+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 4356+ */
4357+
4358+/*
4359+ * branch filesystems and xino for them
4360+ */
4361+
4362+#ifndef __AUFS_BRANCH_H__
4363+#define __AUFS_BRANCH_H__
4364+
4365+#ifdef __KERNEL__
4366+
1facf9fc 4367+#include <linux/mount.h>
8b6a4947 4368+#include "dirren.h"
4a4d8108 4369+#include "dynop.h"
acd2b654 4370+#include "lcnt.h"
1facf9fc 4371+#include "rwsem.h"
4372+#include "super.h"
4373+
4374+/* ---------------------------------------------------------------------- */
4375+
4376+/* a xino file */
062440b3 4377+struct au_xino {
acd2b654
AM
4378+ struct file **xi_file;
4379+ unsigned int xi_nfile;
4380+
521ced18
JR
4381+ struct {
4382+ spinlock_t spin;
4383+ ino_t *array;
4384+ int total;
4385+ /* reserved for future use */
4386+ /* unsigned long *bitmap; */
4387+ wait_queue_head_t wqh;
4388+ } xi_nondir;
1facf9fc 4389+
acd2b654 4390+ struct mutex xi_mtx; /* protects xi_file array */
9f237c51 4391+ struct hlist_bl_head xi_writing;
acd2b654 4392+
062440b3 4393+ atomic_t xi_truncating;
1facf9fc 4394+
062440b3 4395+ struct kref xi_kref;
1facf9fc 4396+};
4397+
076b876e
AM
4398+/* File-based Hierarchical Storage Management */
4399+struct au_br_fhsm {
4400+#ifdef CONFIG_AUFS_FHSM
4401+ struct mutex bf_lock;
4402+ unsigned long bf_jiffy;
4403+ struct aufs_stfs bf_stfs;
4404+ int bf_readable;
4405+#endif
4406+};
4407+
1facf9fc 4408+/* members for writable branch only */
4409+enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4410+struct au_wbr {
dece6358 4411+ struct au_rwsem wbr_wh_rwsem;
1facf9fc 4412+ struct dentry *wbr_wh[AuBrWh_Last];
4a4d8108 4413+ atomic_t wbr_wh_running;
1facf9fc 4414+#define wbr_whbase wbr_wh[AuBrWh_BASE] /* whiteout base */
4415+#define wbr_plink wbr_wh[AuBrWh_PLINK] /* pseudo-link dir */
4416+#define wbr_orph wbr_wh[AuBrWh_ORPH] /* dir for orphans */
4417+
4418+ /* mfs mode */
4419+ unsigned long long wbr_bytes;
4420+};
4421+
4a4d8108
AM
4422+/* ext2 has 3 types of operations at least, ext3 has 4 */
4423+#define AuBrDynOp (AuDyLast * 4)
4424+
1716fcea
AM
4425+#ifdef CONFIG_AUFS_HFSNOTIFY
4426+/* support for asynchronous destruction */
4427+struct au_br_hfsnotify {
4428+ struct fsnotify_group *hfsn_group;
4429+};
4430+#endif
4431+
392086de
AM
4432+/* sysfs entries */
4433+struct au_brsysfs {
4434+ char name[16];
4435+ struct attribute attr;
4436+};
4437+
4438+enum {
4439+ AuBrSysfs_BR,
4440+ AuBrSysfs_BRID,
4441+ AuBrSysfs_Last
4442+};
4443+
1facf9fc 4444+/* protected by superblock rwsem */
4445+struct au_branch {
062440b3 4446+ struct au_xino *br_xino;
1facf9fc 4447+
4448+ aufs_bindex_t br_id;
4449+
4450+ int br_perm;
86dc4139 4451+ struct path br_path;
4a4d8108
AM
4452+ spinlock_t br_dykey_lock;
4453+ struct au_dykey *br_dykey[AuBrDynOp];
acd2b654
AM
4454+ au_lcnt_t br_nfiles; /* opened files */
4455+ au_lcnt_t br_count; /* in-use for other */
1facf9fc 4456+
4457+ struct au_wbr *br_wbr;
076b876e 4458+ struct au_br_fhsm *br_fhsm;
1facf9fc 4459+
027c5e7a 4460+#ifdef CONFIG_AUFS_HFSNOTIFY
1716fcea 4461+ struct au_br_hfsnotify *br_hfsn;
027c5e7a
AM
4462+#endif
4463+
1facf9fc 4464+#ifdef CONFIG_SYSFS
392086de
AM
4465+ /* entries under sysfs per mount-point */
4466+ struct au_brsysfs br_sysfs[AuBrSysfs_Last];
1facf9fc 4467+#endif
8b6a4947 4468+
062440b3
AM
4469+#ifdef CONFIG_DEBUG_FS
4470+ struct dentry *br_dbgaufs; /* xino */
4471+#endif
4472+
8b6a4947 4473+ struct au_dr_br br_dirren;
1facf9fc 4474+};
4475+
4476+/* ---------------------------------------------------------------------- */
4477+
86dc4139
AM
4478+static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4479+{
4480+ return br->br_path.mnt;
4481+}
4482+
4483+static inline struct dentry *au_br_dentry(struct au_branch *br)
4484+{
4485+ return br->br_path.dentry;
4486+}
4487+
0b2a12c6
JR
4488+static inline struct user_namespace *au_br_userns(struct au_branch *br)
4489+{
4490+ return mnt_user_ns(br->br_path.mnt);
4491+}
4492+
86dc4139
AM
4493+static inline struct super_block *au_br_sb(struct au_branch *br)
4494+{
4495+ return au_br_mnt(br)->mnt_sb;
4496+}
4497+
1facf9fc 4498+static inline int au_br_rdonly(struct au_branch *br)
4499+{
8b6a4947 4500+ return (sb_rdonly(au_br_sb(br))
1facf9fc 4501+ || !au_br_writable(br->br_perm))
4502+ ? -EROFS : 0;
4503+}
4504+
4a4d8108 4505+static inline int au_br_hnotifyable(int brperm __maybe_unused)
1facf9fc 4506+{
4a4d8108 4507+#ifdef CONFIG_AUFS_HNOTIFY
1e00d052 4508+ return !(brperm & AuBrPerm_RR);
1facf9fc 4509+#else
4510+ return 0;
4511+#endif
4512+}
4513+
b912730e
AM
4514+static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4515+{
4516+ int err, exec_flag;
4517+
4518+ err = 0;
4519+ exec_flag = oflag & __FMODE_EXEC;
79b8bda9 4520+ if (unlikely(exec_flag && path_noexec(&br->br_path)))
b912730e
AM
4521+ err = -EACCES;
4522+
4523+ return err;
4524+}
4525+
062440b3
AM
4526+static inline void au_xino_get(struct au_branch *br)
4527+{
4528+ struct au_xino *xi;
4529+
4530+ xi = br->br_xino;
4531+ if (xi)
4532+ kref_get(&xi->xi_kref);
4533+}
4534+
4535+static inline int au_xino_count(struct au_branch *br)
4536+{
4537+ int v;
4538+ struct au_xino *xi;
4539+
4540+ v = 0;
4541+ xi = br->br_xino;
4542+ if (xi)
4543+ v = kref_read(&xi->xi_kref);
4544+
4545+ return v;
4546+}
4547+
1facf9fc 4548+/* ---------------------------------------------------------------------- */
4549+
4550+/* branch.c */
4551+struct au_sbinfo;
4552+void au_br_free(struct au_sbinfo *sinfo);
4553+int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4554+struct au_opt_add;
4555+int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4556+struct au_opt_del;
4557+int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
027c5e7a
AM
4558+long au_ibusy_ioctl(struct file *file, unsigned long arg);
4559+#ifdef CONFIG_COMPAT
4560+long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4561+#endif
1facf9fc 4562+struct au_opt_mod;
4563+int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
7f207e10 4564+ int *do_refresh);
076b876e
AM
4565+struct aufs_stfs;
4566+int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
1facf9fc 4567+
4568+/* xino.c */
4569+static const loff_t au_loff_max = LLONG_MAX;
4570+
acd2b654 4571+aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry);
83b672a5
AM
4572+struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
4573+ int wbrtop);
062440b3
AM
4574+struct file *au_xino_create2(struct super_block *sb, struct path *base,
4575+ struct file *copy_src);
acd2b654
AM
4576+struct au_xi_new {
4577+ struct au_xino *xi; /* switch between xino and xigen */
4578+ int idx;
4579+ struct path *base;
4580+ struct file *copy_src;
4581+};
4582+struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew);
062440b3
AM
4583+
4584+int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4585+ ino_t *ino);
4586+int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4587+ ino_t ino);
e37dd06a
AM
4588+ssize_t xino_fread(struct file *file, void *buf, size_t size, loff_t *pos);
4589+ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos);
062440b3
AM
4590+
4591+int au_xib_trunc(struct super_block *sb);
acd2b654 4592+int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin);
1facf9fc 4593+
acd2b654 4594+struct au_xino *au_xino_alloc(unsigned int nfile);
062440b3 4595+int au_xino_put(struct au_branch *br);
acd2b654 4596+struct file *au_xino_file1(struct au_xino *xi);
062440b3 4597+
1facf9fc 4598+struct au_opt_xino;
1facf9fc 4599+void au_xino_clr(struct super_block *sb);
062440b3 4600+int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount);
1facf9fc 4601+struct file *au_xino_def(struct super_block *sb);
062440b3
AM
4602+int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4603+ struct path *base);
4604+
4605+ino_t au_xino_new_ino(struct super_block *sb);
4606+void au_xino_delete_inode(struct inode *inode, const int unlinked);
1facf9fc 4607+
521ced18
JR
4608+void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
4609+ ino_t h_ino, int idx);
4610+int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4611+ int *idx);
4612+
062440b3
AM
4613+int au_xino_path(struct seq_file *seq, struct file *file);
4614+
1facf9fc 4615+/* ---------------------------------------------------------------------- */
4616+
acd2b654
AM
4617+/* @idx is signed to accept -1 meaning the first file */
4618+static inline struct file *au_xino_file(struct au_xino *xi, int idx)
4619+{
4620+ struct file *file;
4621+
4622+ file = NULL;
4623+ if (!xi)
4624+ goto out;
4625+
4626+ if (idx >= 0) {
4627+ if (idx < xi->xi_nfile)
4628+ file = xi->xi_file[idx];
4629+ } else
4630+ file = au_xino_file1(xi);
4631+
4632+out:
4633+ return file;
4634+}
4635+
4636+/* ---------------------------------------------------------------------- */
4637+
1facf9fc 4638+/* Superblock to branch */
4639+static inline
4640+aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4641+{
4642+ return au_sbr(sb, bindex)->br_id;
4643+}
4644+
4645+static inline
4646+struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4647+{
86dc4139 4648+ return au_br_mnt(au_sbr(sb, bindex));
1facf9fc 4649+}
4650+
4651+static inline
0b2a12c6
JR
4652+struct user_namespace *au_sbr_userns(struct super_block *sb, aufs_bindex_t bindex)
4653+{
4654+ return au_br_userns(au_sbr(sb, bindex));
4655+}
4656+
4657+static inline
1facf9fc 4658+struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4659+{
86dc4139 4660+ return au_br_sb(au_sbr(sb, bindex));
1facf9fc 4661+}
4662+
1facf9fc 4663+static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4664+{
4665+ return au_sbr(sb, bindex)->br_perm;
4666+}
4667+
4668+static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4669+{
4670+ return au_br_whable(au_sbr_perm(sb, bindex));
4671+}
4672+
4673+/* ---------------------------------------------------------------------- */
4674+
8b6a4947
AM
4675+#define wbr_wh_read_lock(wbr) au_rw_read_lock(&(wbr)->wbr_wh_rwsem)
4676+#define wbr_wh_write_lock(wbr) au_rw_write_lock(&(wbr)->wbr_wh_rwsem)
4677+#define wbr_wh_read_trylock(wbr) au_rw_read_trylock(&(wbr)->wbr_wh_rwsem)
4678+#define wbr_wh_write_trylock(wbr) au_rw_write_trylock(&(wbr)->wbr_wh_rwsem)
1facf9fc 4679+/*
8b6a4947
AM
4680+#define wbr_wh_read_trylock_nested(wbr) \
4681+ au_rw_read_trylock_nested(&(wbr)->wbr_wh_rwsem)
4682+#define wbr_wh_write_trylock_nested(wbr) \
4683+ au_rw_write_trylock_nested(&(wbr)->wbr_wh_rwsem)
4684+*/
1facf9fc 4685+
8b6a4947
AM
4686+#define wbr_wh_read_unlock(wbr) au_rw_read_unlock(&(wbr)->wbr_wh_rwsem)
4687+#define wbr_wh_write_unlock(wbr) au_rw_write_unlock(&(wbr)->wbr_wh_rwsem)
4688+#define wbr_wh_downgrade_lock(wbr) au_rw_dgrade_lock(&(wbr)->wbr_wh_rwsem)
4689+
4690+#define WbrWhMustNoWaiters(wbr) AuRwMustNoWaiters(&(wbr)->wbr_wh_rwsem)
4691+#define WbrWhMustAnyLock(wbr) AuRwMustAnyLock(&(wbr)->wbr_wh_rwsem)
4692+#define WbrWhMustWriteLock(wbr) AuRwMustWriteLock(&(wbr)->wbr_wh_rwsem)
dece6358 4693+
076b876e
AM
4694+/* ---------------------------------------------------------------------- */
4695+
4696+#ifdef CONFIG_AUFS_FHSM
4697+static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4698+{
4699+ mutex_init(&brfhsm->bf_lock);
4700+ brfhsm->bf_jiffy = 0;
4701+ brfhsm->bf_readable = 0;
4702+}
4703+
4704+static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4705+{
4706+ mutex_destroy(&brfhsm->bf_lock);
4707+}
4708+#else
4709+AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4710+AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4711+#endif
4712+
1facf9fc 4713+#endif /* __KERNEL__ */
4714+#endif /* __AUFS_BRANCH_H__ */
7f207e10 4715diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
eca34b5c 4716--- /usr/share/empty/fs/aufs/conf.mk 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 4717+++ linux/fs/aufs/conf.mk 2022-11-05 23:02:18.959222617 +0100
2121bcd9
AM
4718@@ -0,0 +1,40 @@
4719+# SPDX-License-Identifier: GPL-2.0
4a4d8108
AM
4720+
4721+AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4722+
4723+define AuConf
4724+ifdef ${1}
4725+AuConfStr += ${1}=${${1}}
4726+endif
4727+endef
4728+
b752ccd1 4729+AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
e49829fe 4730+ SBILIST \
7f207e10 4731+ HNOTIFY HFSNOTIFY \
4a4d8108 4732+ EXPORT INO_T_64 \
c1595e42 4733+ XATTR \
076b876e 4734+ FHSM \
4a4d8108 4735+ RDU \
8b6a4947 4736+ DIRREN \
4a4d8108
AM
4737+ SHWH \
4738+ BR_RAMFS \
4739+ BR_FUSE POLL \
4740+ BR_HFSPLUS \
4741+ BDEV_LOOP \
b752ccd1
AM
4742+ DEBUG MAGIC_SYSRQ
4743+$(foreach i, ${AuConfAll}, \
4a4d8108
AM
4744+ $(eval $(call AuConf,CONFIG_AUFS_${i})))
4745+
4746+AuConfName = ${obj}/conf.str
4747+${AuConfName}.tmp: FORCE
4748+ @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4749+${AuConfName}: ${AuConfName}.tmp
4750+ @diff -q $< $@ > /dev/null 2>&1 || { \
4751+ echo ' GEN ' $@; \
4752+ cp -p $< $@; \
4753+ }
4754+FORCE:
4755+clean-files += ${AuConfName} ${AuConfName}.tmp
4756+${obj}/sysfs.o: ${AuConfName}
b752ccd1
AM
4757+
4758+-include ${srctree}/${src}/conf_priv.mk
7f207e10 4759diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
eca34b5c 4760--- /usr/share/empty/fs/aufs/cpup.c 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 4761+++ linux/fs/aufs/cpup.c 2022-11-05 23:02:18.962555950 +0100
758e9dad 4762@@ -0,0 +1,1459 @@
cd7a4cd9 4763+// SPDX-License-Identifier: GPL-2.0
1facf9fc 4764+/*
0dcfbb52 4765+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 4766+ *
0dcfbb52 4767+ * This program is free software; you can redistribute it and/or modify
1facf9fc 4768+ * it under the terms of the GNU General Public License as published by
4769+ * the Free Software Foundation; either version 2 of the License, or
4770+ * (at your option) any later version.
dece6358
AM
4771+ *
4772+ * This program is distributed in the hope that it will be useful,
4773+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
4774+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4775+ * GNU General Public License for more details.
4776+ *
4777+ * You should have received a copy of the GNU General Public License
523b37e3 4778+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 4779+ */
4780+
4781+/*
4782+ * copy-up functions, see wbr_policy.c for copy-down
4783+ */
4784+
4785+#include <linux/fs_stack.h>
dece6358 4786+#include <linux/mm.h>
8cdd5066 4787+#include <linux/task_work.h>
1facf9fc 4788+#include "aufs.h"
4789+
86dc4139 4790+void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
1facf9fc 4791+{
4792+ const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
367653fa 4793+ | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
1facf9fc 4794+
86dc4139
AM
4795+ BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
4796+
4797+ dst->i_flags |= iflags & ~mask;
1facf9fc 4798+ if (au_test_fs_notime(dst->i_sb))
4799+ dst->i_flags |= S_NOATIME | S_NOCMTIME;
4800+}
4801+
4802+void au_cpup_attr_timesizes(struct inode *inode)
4803+{
4804+ struct inode *h_inode;
4805+
5afbbe0d 4806+ h_inode = au_h_iptr(inode, au_ibtop(inode));
1facf9fc 4807+ fsstack_copy_attr_times(inode, h_inode);
4a4d8108 4808+ fsstack_copy_inode_size(inode, h_inode);
1facf9fc 4809+}
4810+
4811+void au_cpup_attr_nlink(struct inode *inode, int force)
4812+{
4813+ struct inode *h_inode;
4814+ struct super_block *sb;
5afbbe0d 4815+ aufs_bindex_t bindex, bbot;
1facf9fc 4816+
4817+ sb = inode->i_sb;
5afbbe0d 4818+ bindex = au_ibtop(inode);
1facf9fc 4819+ h_inode = au_h_iptr(inode, bindex);
4820+ if (!force
4821+ && !S_ISDIR(h_inode->i_mode)
4822+ && au_opt_test(au_mntflags(sb), PLINK)
4823+ && au_plink_test(inode))
4824+ return;
4825+
7eafdf33
AM
4826+ /*
4827+ * 0 can happen in revalidating.
38d290e6
JR
4828+ * h_inode->i_mutex may not be held here, but it is harmless since once
4829+ * i_nlink reaches 0, it will never become positive except O_TMPFILE
4830+ * case.
4831+ * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
4832+ * the incorrect link count.
7eafdf33 4833+ */
92d182d2 4834+ set_nlink(inode, h_inode->i_nlink);
1facf9fc 4835+
4836+ /*
4837+ * fewer nlink makes find(1) noisy, but larger nlink doesn't.
4838+ * it may includes whplink directory.
4839+ */
4840+ if (S_ISDIR(h_inode->i_mode)) {
5afbbe0d
AM
4841+ bbot = au_ibbot(inode);
4842+ for (bindex++; bindex <= bbot; bindex++) {
1facf9fc 4843+ h_inode = au_h_iptr(inode, bindex);
4844+ if (h_inode)
4845+ au_add_nlink(inode, h_inode);
4846+ }
4847+ }
4848+}
4849+
4850+void au_cpup_attr_changeable(struct inode *inode)
4851+{
4852+ struct inode *h_inode;
4853+
5afbbe0d 4854+ h_inode = au_h_iptr(inode, au_ibtop(inode));
1facf9fc 4855+ inode->i_mode = h_inode->i_mode;
4856+ inode->i_uid = h_inode->i_uid;
4857+ inode->i_gid = h_inode->i_gid;
4858+ au_cpup_attr_timesizes(inode);
86dc4139 4859+ au_cpup_attr_flags(inode, h_inode->i_flags);
1facf9fc 4860+}
4861+
4862+void au_cpup_igen(struct inode *inode, struct inode *h_inode)
4863+{
4864+ struct au_iinfo *iinfo = au_ii(inode);
4865+
1308ab2a 4866+ IiMustWriteLock(inode);
4867+
1facf9fc 4868+ iinfo->ii_higen = h_inode->i_generation;
4869+ iinfo->ii_hsb1 = h_inode->i_sb;
4870+}
4871+
4872+void au_cpup_attr_all(struct inode *inode, int force)
4873+{
4874+ struct inode *h_inode;
4875+
5afbbe0d 4876+ h_inode = au_h_iptr(inode, au_ibtop(inode));
1facf9fc 4877+ au_cpup_attr_changeable(inode);
4878+ if (inode->i_nlink > 0)
4879+ au_cpup_attr_nlink(inode, force);
4880+ inode->i_rdev = h_inode->i_rdev;
4881+ inode->i_blkbits = h_inode->i_blkbits;
4882+ au_cpup_igen(inode, h_inode);
4883+}
4884+
4885+/* ---------------------------------------------------------------------- */
4886+
4887+/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
4888+
4889+/* keep the timestamps of the parent dir when cpup */
4890+void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
4891+ struct path *h_path)
4892+{
4893+ struct inode *h_inode;
4894+
4895+ dt->dt_dentry = dentry;
4896+ dt->dt_h_path = *h_path;
5527c038 4897+ h_inode = d_inode(h_path->dentry);
1facf9fc 4898+ dt->dt_atime = h_inode->i_atime;
4899+ dt->dt_mtime = h_inode->i_mtime;
4900+ /* smp_mb(); */
4901+}
4902+
4903+void au_dtime_revert(struct au_dtime *dt)
4904+{
4905+ struct iattr attr;
4906+ int err;
4907+
4908+ attr.ia_atime = dt->dt_atime;
4909+ attr.ia_mtime = dt->dt_mtime;
4910+ attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
4911+ | ATTR_ATIME | ATTR_ATIME_SET;
4912+
523b37e3
AM
4913+ /* no delegation since this is a directory */
4914+ err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
1facf9fc 4915+ if (unlikely(err))
0c3ec466 4916+ pr_warn("restoring timestamps failed(%d). ignored\n", err);
1facf9fc 4917+}
4918+
4919+/* ---------------------------------------------------------------------- */
4920+
86dc4139
AM
4921+/* internal use only */
4922+struct au_cpup_reg_attr {
4923+ int valid;
4924+ struct kstat st;
4925+ unsigned int iflags; /* inode->i_flags */
4926+};
4927+
1facf9fc 4928+static noinline_for_stack
0b2a12c6 4929+int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct path *h_src,
86dc4139 4930+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 4931+{
c1595e42 4932+ int err, sbits, icex;
7e9cd9fe
AM
4933+ unsigned int mnt_flags;
4934+ unsigned char verbose;
1facf9fc 4935+ struct iattr ia;
4936+ struct path h_path;
1308ab2a 4937+ struct inode *h_isrc, *h_idst;
86dc4139 4938+ struct kstat *h_st;
c1595e42 4939+ struct au_branch *br;
1facf9fc 4940+
c1595e42
JR
4941+ br = au_sbr(dst->d_sb, bindex);
4942+ h_path.mnt = au_br_mnt(br);
0b2a12c6
JR
4943+ h_path.dentry = au_h_dptr(dst, bindex);
4944+ h_idst = d_inode(h_path.dentry);
4945+ h_isrc = d_inode(h_src->dentry);
1308ab2a 4946+ ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
1facf9fc 4947+ | ATTR_ATIME | ATTR_MTIME
4948+ | ATTR_ATIME_SET | ATTR_MTIME_SET;
86dc4139
AM
4949+ if (h_src_attr && h_src_attr->valid) {
4950+ h_st = &h_src_attr->st;
4951+ ia.ia_uid = h_st->uid;
4952+ ia.ia_gid = h_st->gid;
4953+ ia.ia_atime = h_st->atime;
4954+ ia.ia_mtime = h_st->mtime;
4955+ if (h_idst->i_mode != h_st->mode
4956+ && !S_ISLNK(h_idst->i_mode)) {
4957+ ia.ia_valid |= ATTR_MODE;
4958+ ia.ia_mode = h_st->mode;
4959+ }
4960+ sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
4961+ au_cpup_attr_flags(h_idst, h_src_attr->iflags);
4962+ } else {
4963+ ia.ia_uid = h_isrc->i_uid;
4964+ ia.ia_gid = h_isrc->i_gid;
4965+ ia.ia_atime = h_isrc->i_atime;
4966+ ia.ia_mtime = h_isrc->i_mtime;
4967+ if (h_idst->i_mode != h_isrc->i_mode
4968+ && !S_ISLNK(h_idst->i_mode)) {
4969+ ia.ia_valid |= ATTR_MODE;
4970+ ia.ia_mode = h_isrc->i_mode;
4971+ }
4972+ sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
4973+ au_cpup_attr_flags(h_idst, h_isrc->i_flags);
1308ab2a 4974+ }
523b37e3
AM
4975+ /* no delegation since it is just created */
4976+ err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
1facf9fc 4977+
4978+ /* is this nfs only? */
4979+ if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
4980+ ia.ia_valid = ATTR_FORCE | ATTR_MODE;
4981+ ia.ia_mode = h_isrc->i_mode;
523b37e3 4982+ err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
1facf9fc 4983+ }
4984+
c1595e42 4985+ icex = br->br_perm & AuBrAttr_ICEX;
7e9cd9fe
AM
4986+ if (!err) {
4987+ mnt_flags = au_mntflags(dst->d_sb);
4988+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
0b2a12c6 4989+ err = au_cpup_xattr(&h_path, h_src, icex, verbose);
7e9cd9fe 4990+ }
c1595e42 4991+
1facf9fc 4992+ return err;
4993+}
4994+
4995+/* ---------------------------------------------------------------------- */
4996+
4997+static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
4998+ char *buf, unsigned long blksize)
4999+{
5000+ int err;
5001+ size_t sz, rbytes, wbytes;
5002+ unsigned char all_zero;
5003+ char *p, *zp;
febd17d6 5004+ struct inode *h_inode;
1facf9fc 5005+ /* reduce stack usage */
5006+ struct iattr *ia;
5007+
5008+ zp = page_address(ZERO_PAGE(0));
5009+ if (unlikely(!zp))
5010+ return -ENOMEM; /* possible? */
5011+
5012+ err = 0;
5013+ all_zero = 0;
5014+ while (len) {
5015+ AuDbg("len %lld\n", len);
5016+ sz = blksize;
5017+ if (len < blksize)
5018+ sz = len;
5019+
5020+ rbytes = 0;
5021+ /* todo: signal_pending? */
5022+ while (!rbytes || err == -EAGAIN || err == -EINTR) {
5023+ rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
5024+ err = rbytes;
5025+ }
5026+ if (unlikely(err < 0))
5027+ break;
5028+
5029+ all_zero = 0;
5030+ if (len >= rbytes && rbytes == blksize)
5031+ all_zero = !memcmp(buf, zp, rbytes);
5032+ if (!all_zero) {
5033+ wbytes = rbytes;
5034+ p = buf;
5035+ while (wbytes) {
5036+ size_t b;
5037+
5038+ b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
5039+ err = b;
5040+ /* todo: signal_pending? */
5041+ if (unlikely(err == -EAGAIN || err == -EINTR))
5042+ continue;
5043+ if (unlikely(err < 0))
5044+ break;
5045+ wbytes -= b;
5046+ p += b;
5047+ }
392086de
AM
5048+ if (unlikely(err < 0))
5049+ break;
1facf9fc 5050+ } else {
5051+ loff_t res;
5052+
5053+ AuLabel(hole);
5054+ res = vfsub_llseek(dst, rbytes, SEEK_CUR);
5055+ err = res;
5056+ if (unlikely(res < 0))
5057+ break;
5058+ }
5059+ len -= rbytes;
5060+ err = 0;
5061+ }
5062+
5063+ /* the last block may be a hole */
5064+ if (!err && all_zero) {
5065+ AuLabel(last hole);
5066+
5067+ err = 1;
2000de60 5068+ if (au_test_nfs(dst->f_path.dentry->d_sb)) {
1facf9fc 5069+ /* nfs requires this step to make last hole */
5070+ /* is this only nfs? */
5071+ do {
5072+ /* todo: signal_pending? */
5073+ err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
5074+ } while (err == -EAGAIN || err == -EINTR);
5075+ if (err == 1)
5076+ dst->f_pos--;
5077+ }
5078+
5079+ if (err == 1) {
5080+ ia = (void *)buf;
5081+ ia->ia_size = dst->f_pos;
5082+ ia->ia_valid = ATTR_SIZE | ATTR_FILE;
5083+ ia->ia_file = dst;
febd17d6
JR
5084+ h_inode = file_inode(dst);
5085+ inode_lock_nested(h_inode, AuLsc_I_CHILD2);
523b37e3
AM
5086+ /* no delegation since it is just created */
5087+ err = vfsub_notify_change(&dst->f_path, ia,
5088+ /*delegated*/NULL);
febd17d6 5089+ inode_unlock(h_inode);
1facf9fc 5090+ }
5091+ }
5092+
5093+ return err;
5094+}
5095+
5096+int au_copy_file(struct file *dst, struct file *src, loff_t len)
5097+{
5098+ int err;
5099+ unsigned long blksize;
5100+ unsigned char do_kfree;
5101+ char *buf;
9f237c51 5102+ struct super_block *h_sb;
1facf9fc 5103+
5104+ err = -ENOMEM;
9f237c51
AM
5105+ h_sb = file_inode(dst)->i_sb;
5106+ blksize = h_sb->s_blocksize;
1facf9fc 5107+ if (!blksize || PAGE_SIZE < blksize)
5108+ blksize = PAGE_SIZE;
5109+ AuDbg("blksize %lu\n", blksize);
5110+ do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
5111+ if (do_kfree)
5112+ buf = kmalloc(blksize, GFP_NOFS);
5113+ else
5114+ buf = (void *)__get_free_page(GFP_NOFS);
5115+ if (unlikely(!buf))
5116+ goto out;
5117+
5118+ if (len > (1 << 22))
5119+ AuDbg("copying a large file %lld\n", (long long)len);
5120+
5121+ src->f_pos = 0;
5122+ dst->f_pos = 0;
5123+ err = au_do_copy_file(dst, src, len, buf, blksize);
9f237c51
AM
5124+ if (do_kfree) {
5125+ AuDebugOn(!au_kfree_do_sz_test(blksize));
5126+ au_kfree_do_rcu(buf);
5127+ } else
1c60b727 5128+ free_page((unsigned long)buf);
1facf9fc 5129+
4f0767ce 5130+out:
1facf9fc 5131+ return err;
5132+}
5133+
1c60b727
AM
5134+static int au_do_copy(struct file *dst, struct file *src, loff_t len)
5135+{
5136+ int err;
5137+ struct super_block *h_src_sb;
5138+ struct inode *h_src_inode;
5139+
5140+ h_src_inode = file_inode(src);
5141+ h_src_sb = h_src_inode->i_sb;
5142+
5143+ /* XFS acquires inode_lock */
5144+ if (!au_test_xfs(h_src_sb))
5145+ err = au_copy_file(dst, src, len);
5146+ else {
3c1bdaff 5147+ inode_unlock_shared(h_src_inode);
1c60b727 5148+ err = au_copy_file(dst, src, len);
be118d29 5149+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
1c60b727
AM
5150+ }
5151+
5152+ return err;
5153+}
5154+
5155+static int au_clone_or_copy(struct file *dst, struct file *src, loff_t len)
5156+{
5157+ int err;
9f237c51 5158+ loff_t lo;
1c60b727
AM
5159+ struct super_block *h_src_sb;
5160+ struct inode *h_src_inode;
5161+
5162+ h_src_inode = file_inode(src);
5163+ h_src_sb = h_src_inode->i_sb;
5164+ if (h_src_sb != file_inode(dst)->i_sb
9f237c51 5165+ || !dst->f_op->remap_file_range) {
1c60b727
AM
5166+ err = au_do_copy(dst, src, len);
5167+ goto out;
5168+ }
5169+
5170+ if (!au_test_nfs(h_src_sb)) {
3c1bdaff 5171+ inode_unlock_shared(h_src_inode);
9f237c51 5172+ lo = vfsub_clone_file_range(src, dst, len);
be118d29 5173+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
1c60b727 5174+ } else
9f237c51
AM
5175+ lo = vfsub_clone_file_range(src, dst, len);
5176+ if (lo == len) {
5177+ err = 0;
5178+ goto out; /* success */
5179+ } else if (lo >= 0)
5180+ /* todo: possible? */
5181+ /* paritially succeeded */
5182+ AuDbg("lo %lld, len %lld. Retrying.\n", lo, len);
5183+ else if (lo != -EOPNOTSUPP) {
5184+ /* older XFS has a condition in cloning */
5185+ err = lo;
1c60b727 5186+ goto out;
9f237c51 5187+ }
1c60b727
AM
5188+
5189+ /* the backend fs on NFS may not support cloning */
5190+ err = au_do_copy(dst, src, len);
5191+
5192+out:
5193+ AuTraceErr(err);
5194+ return err;
5195+}
5196+
1facf9fc 5197+/*
5198+ * to support a sparse file which is opened with O_APPEND,
5199+ * we need to close the file.
5200+ */
c2b27bf2 5201+static int au_cp_regular(struct au_cp_generic *cpg)
1facf9fc 5202+{
5203+ int err, i;
5204+ enum { SRC, DST };
5205+ struct {
5206+ aufs_bindex_t bindex;
5207+ unsigned int flags;
5208+ struct dentry *dentry;
392086de 5209+ int force_wr;
1facf9fc 5210+ struct file *file;
1facf9fc 5211+ } *f, file[] = {
5212+ {
c2b27bf2 5213+ .bindex = cpg->bsrc,
1facf9fc 5214+ .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
1facf9fc 5215+ },
5216+ {
c2b27bf2 5217+ .bindex = cpg->bdst,
1facf9fc 5218+ .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
392086de 5219+ .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
1facf9fc 5220+ }
5221+ };
acd2b654 5222+ struct au_branch *br;
521ced18 5223+ struct super_block *sb, *h_src_sb;
e2f27e51 5224+ struct inode *h_src_inode;
8cdd5066 5225+ struct task_struct *tsk = current;
1facf9fc 5226+
5227+ /* bsrc branch can be ro/rw. */
c2b27bf2 5228+ sb = cpg->dentry->d_sb;
1facf9fc 5229+ f = file;
5230+ for (i = 0; i < 2; i++, f++) {
c2b27bf2
AM
5231+ f->dentry = au_h_dptr(cpg->dentry, f->bindex);
5232+ f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
392086de 5233+ /*file*/NULL, f->force_wr);
9f237c51
AM
5234+ if (IS_ERR(f->file)) {
5235+ err = PTR_ERR(f->file);
5236+ if (i == SRC)
5237+ goto out;
5238+ else
5239+ goto out_src;
5240+ }
1facf9fc 5241+ }
5242+
5243+ /* try stopping to update while we copyup */
e2f27e51 5244+ h_src_inode = d_inode(file[SRC].dentry);
521ced18
JR
5245+ h_src_sb = h_src_inode->i_sb;
5246+ if (!au_test_nfs(h_src_sb))
e2f27e51 5247+ IMustLock(h_src_inode);
1c60b727 5248+ err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
1facf9fc 5249+
8cdd5066
JR
5250+ /* i wonder if we had O_NO_DELAY_FPUT flag */
5251+ if (tsk->flags & PF_KTHREAD)
5252+ __fput_sync(file[DST].file);
5253+ else {
062440b3 5254+ /* it happened actually */
8cdd5066
JR
5255+ fput(file[DST].file);
5256+ /*
5257+ * too bad.
5258+ * we have to call both since we don't know which place the file
5259+ * was added to.
5260+ */
5261+ task_work_run();
5262+ flush_delayed_fput();
5263+ }
acd2b654
AM
5264+ br = au_sbr(sb, file[DST].bindex);
5265+ au_lcnt_dec(&br->br_nfiles);
523b37e3 5266+
4f0767ce 5267+out_src:
1facf9fc 5268+ fput(file[SRC].file);
acd2b654
AM
5269+ br = au_sbr(sb, file[SRC].bindex);
5270+ au_lcnt_dec(&br->br_nfiles);
4f0767ce 5271+out:
1facf9fc 5272+ return err;
5273+}
5274+
c2b27bf2 5275+static int au_do_cpup_regular(struct au_cp_generic *cpg,
86dc4139 5276+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 5277+{
5278+ int err, rerr;
5279+ loff_t l;
86dc4139 5280+ struct path h_path;
38d290e6 5281+ struct inode *h_src_inode, *h_dst_inode;
1facf9fc 5282+
5283+ err = 0;
5527c038 5284+ h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
86dc4139 5285+ l = i_size_read(h_src_inode);
c2b27bf2
AM
5286+ if (cpg->len == -1 || l < cpg->len)
5287+ cpg->len = l;
5288+ if (cpg->len) {
86dc4139 5289+ /* try stopping to update while we are referencing */
be118d29 5290+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
c2b27bf2 5291+ au_pin_hdir_unlock(cpg->pin);
1facf9fc 5292+
c2b27bf2
AM
5293+ h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5294+ h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
86dc4139 5295+ h_src_attr->iflags = h_src_inode->i_flags;
5527c038 5296+ if (!au_test_nfs(h_src_inode->i_sb))
521ced18 5297+ err = vfsub_getattr(&h_path, &h_src_attr->st);
5527c038 5298+ else {
3c1bdaff 5299+ inode_unlock_shared(h_src_inode);
521ced18 5300+ err = vfsub_getattr(&h_path, &h_src_attr->st);
be118d29 5301+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5527c038 5302+ }
86dc4139 5303+ if (unlikely(err)) {
3c1bdaff 5304+ inode_unlock_shared(h_src_inode);
86dc4139
AM
5305+ goto out;
5306+ }
5307+ h_src_attr->valid = 1;
e2f27e51
AM
5308+ if (!au_test_nfs(h_src_inode->i_sb)) {
5309+ err = au_cp_regular(cpg);
3c1bdaff 5310+ inode_unlock_shared(h_src_inode);
e2f27e51 5311+ } else {
3c1bdaff 5312+ inode_unlock_shared(h_src_inode);
e2f27e51
AM
5313+ err = au_cp_regular(cpg);
5314+ }
c2b27bf2 5315+ rerr = au_pin_hdir_relock(cpg->pin);
86dc4139
AM
5316+ if (!err && rerr)
5317+ err = rerr;
1facf9fc 5318+ }
38d290e6
JR
5319+ if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5320+ h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5527c038 5321+ h_dst_inode = d_inode(h_path.dentry);
38d290e6
JR
5322+ spin_lock(&h_dst_inode->i_lock);
5323+ h_dst_inode->i_state |= I_LINKABLE;
5324+ spin_unlock(&h_dst_inode->i_lock);
5325+ }
1facf9fc 5326+
4f0767ce 5327+out:
1facf9fc 5328+ return err;
5329+}
5330+
5331+static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5332+ struct inode *h_dir)
5333+{
e37dd06a
AM
5334+ int err;
5335+ DEFINE_DELAYED_CALL(done);
5336+ const char *sym;
1facf9fc 5337+
e37dd06a
AM
5338+ sym = vfs_get_link(h_src, &done);
5339+ err = PTR_ERR(sym);
5340+ if (IS_ERR(sym))
1facf9fc 5341+ goto out;
5342+
e37dd06a 5343+ err = vfsub_symlink(h_dir, h_path, sym);
1facf9fc 5344+
4f0767ce 5345+out:
e37dd06a 5346+ do_delayed_call(&done);
1facf9fc 5347+ return err;
5348+}
5349+
8cdd5066
JR
5350+/*
5351+ * regardless 'acl' option, reset all ACL.
5352+ * All ACL will be copied up later from the original entry on the lower branch.
5353+ */
5354+static int au_reset_acl(struct inode *h_dir, struct path *h_path, umode_t mode)
5355+{
5356+ int err;
5357+ struct dentry *h_dentry;
5358+ struct inode *h_inode;
0b2a12c6 5359+ struct user_namespace *h_userns;
8cdd5066 5360+
0b2a12c6 5361+ h_userns = mnt_user_ns(h_path->mnt);
8cdd5066
JR
5362+ h_dentry = h_path->dentry;
5363+ h_inode = d_inode(h_dentry);
5364+ /* forget_all_cached_acls(h_inode)); */
0b2a12c6 5365+ err = vfsub_removexattr(h_userns, h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
8cdd5066
JR
5366+ AuTraceErr(err);
5367+ if (err == -EOPNOTSUPP)
5368+ err = 0;
5369+ if (!err)
0b2a12c6 5370+ err = vfsub_acl_chmod(h_userns, h_inode, mode);
8cdd5066
JR
5371+
5372+ AuTraceErr(err);
5373+ return err;
5374+}
5375+
5376+static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5377+ struct inode *h_dir, struct path *h_path)
5378+{
5379+ int err;
5380+ struct inode *dir, *inode;
0b2a12c6 5381+ struct user_namespace *h_userns;
8cdd5066 5382+
0b2a12c6
JR
5383+ h_userns = mnt_user_ns(h_path->mnt);
5384+ err = vfsub_removexattr(h_userns, h_path->dentry,
5385+ XATTR_NAME_POSIX_ACL_DEFAULT);
8cdd5066
JR
5386+ AuTraceErr(err);
5387+ if (err == -EOPNOTSUPP)
5388+ err = 0;
5389+ if (unlikely(err))
5390+ goto out;
5391+
5392+ /*
5393+ * strange behaviour from the users view,
acd2b654 5394+ * particularly setattr case
8cdd5066
JR
5395+ */
5396+ dir = d_inode(dst_parent);
5afbbe0d 5397+ if (au_ibtop(dir) == cpg->bdst)
8cdd5066
JR
5398+ au_cpup_attr_nlink(dir, /*force*/1);
5399+ inode = d_inode(cpg->dentry);
5400+ au_cpup_attr_nlink(inode, /*force*/1);
5401+
5402+out:
5403+ return err;
5404+}
5405+
1facf9fc 5406+static noinline_for_stack
c2b27bf2 5407+int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
86dc4139 5408+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 5409+{
5410+ int err;
5411+ umode_t mode;
5412+ unsigned int mnt_flags;
076b876e 5413+ unsigned char isdir, isreg, force;
c2b27bf2 5414+ const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
1facf9fc 5415+ struct au_dtime dt;
5416+ struct path h_path;
5417+ struct dentry *h_src, *h_dst, *h_parent;
8cdd5066 5418+ struct inode *h_inode, *h_dir;
1facf9fc 5419+ struct super_block *sb;
5420+
5421+ /* bsrc branch can be ro/rw. */
c2b27bf2 5422+ h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038
JR
5423+ h_inode = d_inode(h_src);
5424+ AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
1facf9fc 5425+
5426+ /* try stopping to be referenced while we are creating */
c2b27bf2
AM
5427+ h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5428+ if (au_ftest_cpup(cpg->flags, RENAME))
86dc4139
AM
5429+ AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5430+ AUFS_WH_PFX_LEN));
1facf9fc 5431+ h_parent = h_dst->d_parent; /* dir inode is locked */
5527c038 5432+ h_dir = d_inode(h_parent);
1facf9fc 5433+ IMustLock(h_dir);
5434+ AuDebugOn(h_parent != h_dst->d_parent);
5435+
c2b27bf2
AM
5436+ sb = cpg->dentry->d_sb;
5437+ h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
1facf9fc 5438+ if (do_dt) {
5439+ h_path.dentry = h_parent;
5440+ au_dtime_store(&dt, dst_parent, &h_path);
5441+ }
5442+ h_path.dentry = h_dst;
5443+
076b876e 5444+ isreg = 0;
1facf9fc 5445+ isdir = 0;
5446+ mode = h_inode->i_mode;
5447+ switch (mode & S_IFMT) {
5448+ case S_IFREG:
076b876e 5449+ isreg = 1;
cd7a4cd9 5450+ err = vfsub_create(h_dir, &h_path, 0600, /*want_excl*/true);
1facf9fc 5451+ if (!err)
c2b27bf2 5452+ err = au_do_cpup_regular(cpg, h_src_attr);
1facf9fc 5453+ break;
5454+ case S_IFDIR:
5455+ isdir = 1;
5456+ err = vfsub_mkdir(h_dir, &h_path, mode);
8cdd5066
JR
5457+ if (!err)
5458+ err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
1facf9fc 5459+ break;
5460+ case S_IFLNK:
5461+ err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5462+ break;
5463+ case S_IFCHR:
5464+ case S_IFBLK:
5465+ AuDebugOn(!capable(CAP_MKNOD));
42a736d3 5466+ fallthrough;
1facf9fc 5467+ case S_IFIFO:
5468+ case S_IFSOCK:
5469+ err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5470+ break;
5471+ default:
5472+ AuIOErr("Unknown inode type 0%o\n", mode);
5473+ err = -EIO;
5474+ }
8cdd5066
JR
5475+ if (!err)
5476+ err = au_reset_acl(h_dir, &h_path, mode);
1facf9fc 5477+
5478+ mnt_flags = au_mntflags(sb);
5479+ if (!au_opt_test(mnt_flags, UDBA_NONE)
5480+ && !isdir
5481+ && au_opt_test(mnt_flags, XINO)
38d290e6
JR
5482+ && (h_inode->i_nlink == 1
5483+ || (h_inode->i_state & I_LINKABLE))
1facf9fc 5484+ /* todo: unnecessary? */
5527c038 5485+ /* && d_inode(cpg->dentry)->i_nlink == 1 */
c2b27bf2
AM
5486+ && cpg->bdst < cpg->bsrc
5487+ && !au_ftest_cpup(cpg->flags, KEEPLINO))
5488+ au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
1facf9fc 5489+ /* ignore this error */
5490+
076b876e
AM
5491+ if (!err) {
5492+ force = 0;
5493+ if (isreg) {
5494+ force = !!cpg->len;
5495+ if (cpg->len == -1)
5496+ force = !!i_size_read(h_inode);
5497+ }
5498+ au_fhsm_wrote(sb, cpg->bdst, force);
5499+ }
5500+
1facf9fc 5501+ if (do_dt)
5502+ au_dtime_revert(&dt);
5503+ return err;
5504+}
5505+
392086de 5506+static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
86dc4139
AM
5507+{
5508+ int err;
392086de 5509+ struct dentry *dentry, *h_dentry, *h_parent, *parent;
758e9dad 5510+ struct path h_ppath;
86dc4139 5511+ struct inode *h_dir;
392086de 5512+ aufs_bindex_t bdst;
86dc4139 5513+
392086de
AM
5514+ dentry = cpg->dentry;
5515+ bdst = cpg->bdst;
758e9dad 5516+ h_ppath.mnt = au_sbr_mnt(dentry->d_sb, bdst);
392086de
AM
5517+ h_dentry = au_h_dptr(dentry, bdst);
5518+ if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5519+ dget(h_dentry);
5520+ au_set_h_dptr(dentry, bdst, NULL);
5521+ err = au_lkup_neg(dentry, bdst, /*wh*/0);
5522+ if (!err)
5523+ h_path->dentry = dget(au_h_dptr(dentry, bdst));
86dc4139 5524+ au_set_h_dptr(dentry, bdst, h_dentry);
392086de
AM
5525+ } else {
5526+ err = 0;
5527+ parent = dget_parent(dentry);
758e9dad 5528+ h_ppath.dentry = au_h_dptr(parent, bdst);
392086de 5529+ dput(parent);
758e9dad 5530+ h_path->dentry = vfsub_lkup_one(&dentry->d_name, &h_ppath);
392086de
AM
5531+ if (IS_ERR(h_path->dentry))
5532+ err = PTR_ERR(h_path->dentry);
86dc4139 5533+ }
392086de
AM
5534+ if (unlikely(err))
5535+ goto out;
86dc4139 5536+
86dc4139 5537+ h_parent = h_dentry->d_parent; /* dir inode is locked */
5527c038 5538+ h_dir = d_inode(h_parent);
86dc4139 5539+ IMustLock(h_dir);
523b37e3
AM
5540+ AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5541+ /* no delegation since it is just created */
f2c43d5f
AM
5542+ err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
5543+ /*flags*/0);
86dc4139
AM
5544+ dput(h_path->dentry);
5545+
5546+out:
5547+ return err;
5548+}
5549+
1facf9fc 5550+/*
5551+ * copyup the @dentry from @bsrc to @bdst.
5552+ * the caller must set the both of lower dentries.
5553+ * @len is for truncating when it is -1 copyup the entire file.
5554+ * in link/rename cases, @dst_parent may be different from the real one.
c2b27bf2 5555+ * basic->bsrc can be larger than basic->bdst.
f2c43d5f 5556+ * aufs doesn't touch the credential so
acd2b654 5557+ * security_inode_copy_up{,_xattr}() are unnecessary.
1facf9fc 5558+ */
c2b27bf2 5559+static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
1facf9fc 5560+{
5561+ int err, rerr;
5afbbe0d 5562+ aufs_bindex_t old_ibtop;
1facf9fc 5563+ unsigned char isdir, plink;
1facf9fc 5564+ struct dentry *h_src, *h_dst, *h_parent;
5527c038 5565+ struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
1facf9fc 5566+ struct super_block *sb;
86dc4139 5567+ struct au_branch *br;
0b2a12c6 5568+ struct path h_src_path;
acd2b654 5569+ /* to reduce stack size */
c2b27bf2
AM
5570+ struct {
5571+ struct au_dtime dt;
5572+ struct path h_path;
5573+ struct au_cpup_reg_attr h_src_attr;
5574+ } *a;
1facf9fc 5575+
c2b27bf2
AM
5576+ err = -ENOMEM;
5577+ a = kmalloc(sizeof(*a), GFP_NOFS);
5578+ if (unlikely(!a))
5579+ goto out;
5580+ a->h_src_attr.valid = 0;
1facf9fc 5581+
c2b27bf2
AM
5582+ sb = cpg->dentry->d_sb;
5583+ br = au_sbr(sb, cpg->bdst);
5584+ a->h_path.mnt = au_br_mnt(br);
5585+ h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
1facf9fc 5586+ h_parent = h_dst->d_parent; /* dir inode is locked */
5527c038 5587+ h_dir = d_inode(h_parent);
1facf9fc 5588+ IMustLock(h_dir);
5589+
c2b27bf2 5590+ h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038 5591+ inode = d_inode(cpg->dentry);
1facf9fc 5592+
5593+ if (!dst_parent)
c2b27bf2 5594+ dst_parent = dget_parent(cpg->dentry);
1facf9fc 5595+ else
5596+ dget(dst_parent);
5597+
5598+ plink = !!au_opt_test(au_mntflags(sb), PLINK);
c2b27bf2 5599+ dst_inode = au_h_iptr(inode, cpg->bdst);
1facf9fc 5600+ if (dst_inode) {
5601+ if (unlikely(!plink)) {
5602+ err = -EIO;
027c5e7a
AM
5603+ AuIOErr("hi%lu(i%lu) exists on b%d "
5604+ "but plink is disabled\n",
c2b27bf2
AM
5605+ dst_inode->i_ino, inode->i_ino, cpg->bdst);
5606+ goto out_parent;
1facf9fc 5607+ }
5608+
5609+ if (dst_inode->i_nlink) {
c2b27bf2 5610+ const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
1facf9fc 5611+
c2b27bf2 5612+ h_src = au_plink_lkup(inode, cpg->bdst);
1facf9fc 5613+ err = PTR_ERR(h_src);
5614+ if (IS_ERR(h_src))
c2b27bf2 5615+ goto out_parent;
5527c038 5616+ if (unlikely(d_is_negative(h_src))) {
1facf9fc 5617+ err = -EIO;
79b8bda9 5618+ AuIOErr("i%lu exists on b%d "
027c5e7a 5619+ "but not pseudo-linked\n",
79b8bda9 5620+ inode->i_ino, cpg->bdst);
1facf9fc 5621+ dput(h_src);
c2b27bf2 5622+ goto out_parent;
1facf9fc 5623+ }
5624+
5625+ if (do_dt) {
c2b27bf2
AM
5626+ a->h_path.dentry = h_parent;
5627+ au_dtime_store(&a->dt, dst_parent, &a->h_path);
1facf9fc 5628+ }
86dc4139 5629+
c2b27bf2 5630+ a->h_path.dentry = h_dst;
523b37e3
AM
5631+ delegated = NULL;
5632+ err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
c2b27bf2 5633+ if (!err && au_ftest_cpup(cpg->flags, RENAME))
392086de 5634+ err = au_do_ren_after_cpup(cpg, &a->h_path);
1facf9fc 5635+ if (do_dt)
c2b27bf2 5636+ au_dtime_revert(&a->dt);
523b37e3
AM
5637+ if (unlikely(err == -EWOULDBLOCK)) {
5638+ pr_warn("cannot retry for NFSv4 delegation"
5639+ " for an internal link\n");
5640+ iput(delegated);
5641+ }
1facf9fc 5642+ dput(h_src);
c2b27bf2 5643+ goto out_parent;
1facf9fc 5644+ } else
5645+ /* todo: cpup_wh_file? */
5646+ /* udba work */
4a4d8108 5647+ au_update_ibrange(inode, /*do_put_zero*/1);
1facf9fc 5648+ }
5649+
86dc4139 5650+ isdir = S_ISDIR(inode->i_mode);
5afbbe0d 5651+ old_ibtop = au_ibtop(inode);
c2b27bf2 5652+ err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
1facf9fc 5653+ if (unlikely(err))
86dc4139 5654+ goto out_rev;
5527c038 5655+ dst_inode = d_inode(h_dst);
febd17d6 5656+ inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
86dc4139 5657+ /* todo: necessary? */
c2b27bf2 5658+ /* au_pin_hdir_unlock(cpg->pin); */
1facf9fc 5659+
0b2a12c6
JR
5660+ h_src_path.dentry = h_src;
5661+ h_src_path.mnt = au_sbr_mnt(sb, cpg->bsrc);
5662+ err = cpup_iattr(cpg->dentry, cpg->bdst, &h_src_path, &a->h_src_attr);
86dc4139
AM
5663+ if (unlikely(err)) {
5664+ /* todo: necessary? */
c2b27bf2 5665+ /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
febd17d6 5666+ inode_unlock(dst_inode);
86dc4139
AM
5667+ goto out_rev;
5668+ }
5669+
5afbbe0d 5670+ if (cpg->bdst < old_ibtop) {
86dc4139 5671+ if (S_ISREG(inode->i_mode)) {
c2b27bf2 5672+ err = au_dy_iaop(inode, cpg->bdst, dst_inode);
86dc4139 5673+ if (unlikely(err)) {
c2b27bf2
AM
5674+ /* ignore an error */
5675+ /* au_pin_hdir_relock(cpg->pin); */
febd17d6 5676+ inode_unlock(dst_inode);
86dc4139 5677+ goto out_rev;
4a4d8108 5678+ }
4a4d8108 5679+ }
5afbbe0d 5680+ au_set_ibtop(inode, cpg->bdst);
c2b27bf2 5681+ } else
5afbbe0d 5682+ au_set_ibbot(inode, cpg->bdst);
c2b27bf2 5683+ au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
86dc4139
AM
5684+ au_hi_flags(inode, isdir));
5685+
5686+ /* todo: necessary? */
c2b27bf2 5687+ /* err = au_pin_hdir_relock(cpg->pin); */
febd17d6 5688+ inode_unlock(dst_inode);
86dc4139
AM
5689+ if (unlikely(err))
5690+ goto out_rev;
5691+
5527c038 5692+ src_inode = d_inode(h_src);
86dc4139 5693+ if (!isdir
5527c038
JR
5694+ && (src_inode->i_nlink > 1
5695+ || src_inode->i_state & I_LINKABLE)
86dc4139 5696+ && plink)
c2b27bf2 5697+ au_plink_append(inode, cpg->bdst, h_dst);
86dc4139 5698+
c2b27bf2
AM
5699+ if (au_ftest_cpup(cpg->flags, RENAME)) {
5700+ a->h_path.dentry = h_dst;
392086de 5701+ err = au_do_ren_after_cpup(cpg, &a->h_path);
86dc4139
AM
5702+ }
5703+ if (!err)
c2b27bf2 5704+ goto out_parent; /* success */
1facf9fc 5705+
5706+ /* revert */
4a4d8108 5707+out_rev:
c2b27bf2
AM
5708+ a->h_path.dentry = h_parent;
5709+ au_dtime_store(&a->dt, dst_parent, &a->h_path);
5710+ a->h_path.dentry = h_dst;
86dc4139 5711+ rerr = 0;
5527c038 5712+ if (d_is_positive(h_dst)) {
523b37e3
AM
5713+ if (!isdir) {
5714+ /* no delegation since it is just created */
5715+ rerr = vfsub_unlink(h_dir, &a->h_path,
5716+ /*delegated*/NULL, /*force*/0);
5717+ } else
c2b27bf2 5718+ rerr = vfsub_rmdir(h_dir, &a->h_path);
86dc4139 5719+ }
c2b27bf2 5720+ au_dtime_revert(&a->dt);
1facf9fc 5721+ if (rerr) {
5722+ AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5723+ err = -EIO;
5724+ }
c2b27bf2 5725+out_parent:
1facf9fc 5726+ dput(dst_parent);
9f237c51 5727+ au_kfree_rcu(a);
c2b27bf2 5728+out:
1facf9fc 5729+ return err;
5730+}
5731+
7e9cd9fe 5732+#if 0 /* reserved */
1facf9fc 5733+struct au_cpup_single_args {
5734+ int *errp;
c2b27bf2 5735+ struct au_cp_generic *cpg;
1facf9fc 5736+ struct dentry *dst_parent;
5737+};
5738+
5739+static void au_call_cpup_single(void *args)
5740+{
5741+ struct au_cpup_single_args *a = args;
86dc4139 5742+
c2b27bf2
AM
5743+ au_pin_hdir_acquire_nest(a->cpg->pin);
5744+ *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5745+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 5746+}
c2b27bf2 5747+#endif
1facf9fc 5748+
53392da6
AM
5749+/*
5750+ * prevent SIGXFSZ in copy-up.
5751+ * testing CAP_MKNOD is for generic fs,
5752+ * but CAP_FSETID is for xfs only, currently.
5753+ */
86dc4139 5754+static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
53392da6
AM
5755+{
5756+ int do_sio;
86dc4139
AM
5757+ struct super_block *sb;
5758+ struct inode *h_dir;
53392da6
AM
5759+
5760+ do_sio = 0;
86dc4139 5761+ sb = au_pinned_parent(pin)->d_sb;
53392da6
AM
5762+ if (!au_wkq_test()
5763+ && (!au_sbi(sb)->si_plink_maint_pid
5764+ || au_plink_maint(sb, AuLock_NOPLM))) {
5765+ switch (mode & S_IFMT) {
5766+ case S_IFREG:
5767+ /* no condition about RLIMIT_FSIZE and the file size */
5768+ do_sio = 1;
5769+ break;
5770+ case S_IFCHR:
5771+ case S_IFBLK:
5772+ do_sio = !capable(CAP_MKNOD);
5773+ break;
5774+ }
5775+ if (!do_sio)
5776+ do_sio = ((mode & (S_ISUID | S_ISGID))
5777+ && !capable(CAP_FSETID));
86dc4139
AM
5778+ /* this workaround may be removed in the future */
5779+ if (!do_sio) {
5780+ h_dir = au_pinned_h_dir(pin);
5781+ do_sio = h_dir->i_mode & S_ISVTX;
5782+ }
53392da6
AM
5783+ }
5784+
5785+ return do_sio;
5786+}
5787+
7e9cd9fe 5788+#if 0 /* reserved */
c2b27bf2 5789+int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
1facf9fc 5790+{
5791+ int err, wkq_err;
1facf9fc 5792+ struct dentry *h_dentry;
5793+
c2b27bf2 5794+ h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038 5795+ if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
c2b27bf2 5796+ err = au_cpup_single(cpg, dst_parent);
1facf9fc 5797+ else {
5798+ struct au_cpup_single_args args = {
5799+ .errp = &err,
c2b27bf2
AM
5800+ .cpg = cpg,
5801+ .dst_parent = dst_parent
1facf9fc 5802+ };
5803+ wkq_err = au_wkq_wait(au_call_cpup_single, &args);
5804+ if (unlikely(wkq_err))
5805+ err = wkq_err;
5806+ }
5807+
5808+ return err;
5809+}
c2b27bf2 5810+#endif
1facf9fc 5811+
5812+/*
5813+ * copyup the @dentry from the first active lower branch to @bdst,
5814+ * using au_cpup_single().
5815+ */
c2b27bf2 5816+static int au_cpup_simple(struct au_cp_generic *cpg)
1facf9fc 5817+{
5818+ int err;
c2b27bf2
AM
5819+ unsigned int flags_orig;
5820+ struct dentry *dentry;
5821+
5822+ AuDebugOn(cpg->bsrc < 0);
1facf9fc 5823+
c2b27bf2 5824+ dentry = cpg->dentry;
86dc4139 5825+ DiMustWriteLock(dentry);
1facf9fc 5826+
c2b27bf2 5827+ err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
1facf9fc 5828+ if (!err) {
c2b27bf2
AM
5829+ flags_orig = cpg->flags;
5830+ au_fset_cpup(cpg->flags, RENAME);
5831+ err = au_cpup_single(cpg, NULL);
5832+ cpg->flags = flags_orig;
1facf9fc 5833+ if (!err)
5834+ return 0; /* success */
5835+
5836+ /* revert */
c2b27bf2 5837+ au_set_h_dptr(dentry, cpg->bdst, NULL);
5afbbe0d 5838+ au_set_dbtop(dentry, cpg->bsrc);
1facf9fc 5839+ }
5840+
5841+ return err;
5842+}
5843+
5844+struct au_cpup_simple_args {
5845+ int *errp;
c2b27bf2 5846+ struct au_cp_generic *cpg;
1facf9fc 5847+};
5848+
5849+static void au_call_cpup_simple(void *args)
5850+{
5851+ struct au_cpup_simple_args *a = args;
86dc4139 5852+
c2b27bf2
AM
5853+ au_pin_hdir_acquire_nest(a->cpg->pin);
5854+ *a->errp = au_cpup_simple(a->cpg);
5855+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 5856+}
5857+
c2b27bf2 5858+static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
1facf9fc 5859+{
5860+ int err, wkq_err;
c2b27bf2
AM
5861+ struct dentry *dentry, *parent;
5862+ struct file *h_file;
1facf9fc 5863+ struct inode *h_dir;
0b2a12c6 5864+ struct user_namespace *h_userns;
1facf9fc 5865+
c2b27bf2
AM
5866+ dentry = cpg->dentry;
5867+ h_file = NULL;
5868+ if (au_ftest_cpup(cpg->flags, HOPEN)) {
5869+ AuDebugOn(cpg->bsrc < 0);
392086de 5870+ h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
c2b27bf2
AM
5871+ err = PTR_ERR(h_file);
5872+ if (IS_ERR(h_file))
5873+ goto out;
5874+ }
5875+
1facf9fc 5876+ parent = dget_parent(dentry);
5527c038 5877+ h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
0b2a12c6
JR
5878+ h_userns = au_sbr_userns(dentry->d_sb, cpg->bdst);
5879+ if (!au_test_h_perm_sio(h_userns, h_dir, MAY_EXEC | MAY_WRITE)
5527c038 5880+ && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
c2b27bf2 5881+ err = au_cpup_simple(cpg);
1facf9fc 5882+ else {
5883+ struct au_cpup_simple_args args = {
5884+ .errp = &err,
c2b27bf2 5885+ .cpg = cpg
1facf9fc 5886+ };
5887+ wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
5888+ if (unlikely(wkq_err))
5889+ err = wkq_err;
5890+ }
5891+
5892+ dput(parent);
c2b27bf2
AM
5893+ if (h_file)
5894+ au_h_open_post(dentry, cpg->bsrc, h_file);
5895+
5896+out:
1facf9fc 5897+ return err;
5898+}
5899+
c2b27bf2 5900+int au_sio_cpup_simple(struct au_cp_generic *cpg)
367653fa 5901+{
5afbbe0d 5902+ aufs_bindex_t bsrc, bbot;
c2b27bf2 5903+ struct dentry *dentry, *h_dentry;
367653fa 5904+
c2b27bf2
AM
5905+ if (cpg->bsrc < 0) {
5906+ dentry = cpg->dentry;
5afbbe0d
AM
5907+ bbot = au_dbbot(dentry);
5908+ for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
c2b27bf2
AM
5909+ h_dentry = au_h_dptr(dentry, bsrc);
5910+ if (h_dentry) {
5527c038 5911+ AuDebugOn(d_is_negative(h_dentry));
c2b27bf2
AM
5912+ break;
5913+ }
5914+ }
5afbbe0d 5915+ AuDebugOn(bsrc > bbot);
c2b27bf2 5916+ cpg->bsrc = bsrc;
367653fa 5917+ }
c2b27bf2
AM
5918+ AuDebugOn(cpg->bsrc <= cpg->bdst);
5919+ return au_do_sio_cpup_simple(cpg);
5920+}
367653fa 5921+
c2b27bf2
AM
5922+int au_sio_cpdown_simple(struct au_cp_generic *cpg)
5923+{
5924+ AuDebugOn(cpg->bdst <= cpg->bsrc);
5925+ return au_do_sio_cpup_simple(cpg);
367653fa
AM
5926+}
5927+
1facf9fc 5928+/* ---------------------------------------------------------------------- */
5929+
5930+/*
5931+ * copyup the deleted file for writing.
5932+ */
c2b27bf2
AM
5933+static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
5934+ struct file *file)
1facf9fc 5935+{
5936+ int err;
c2b27bf2
AM
5937+ unsigned int flags_orig;
5938+ aufs_bindex_t bsrc_orig;
c2b27bf2 5939+ struct au_dinfo *dinfo;
5afbbe0d
AM
5940+ struct {
5941+ struct au_hdentry *hd;
5942+ struct dentry *h_dentry;
5943+ } hdst, hsrc;
1facf9fc 5944+
c2b27bf2 5945+ dinfo = au_di(cpg->dentry);
1308ab2a 5946+ AuRwMustWriteLock(&dinfo->di_rwsem);
5947+
c2b27bf2 5948+ bsrc_orig = cpg->bsrc;
5afbbe0d
AM
5949+ cpg->bsrc = dinfo->di_btop;
5950+ hdst.hd = au_hdentry(dinfo, cpg->bdst);
5951+ hdst.h_dentry = hdst.hd->hd_dentry;
5952+ hdst.hd->hd_dentry = wh_dentry;
5953+ dinfo->di_btop = cpg->bdst;
5954+
5955+ hsrc.h_dentry = NULL;
027c5e7a 5956+ if (file) {
5afbbe0d
AM
5957+ hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
5958+ hsrc.h_dentry = hsrc.hd->hd_dentry;
5959+ hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
027c5e7a 5960+ }
c2b27bf2
AM
5961+ flags_orig = cpg->flags;
5962+ cpg->flags = !AuCpup_DTIME;
5963+ err = au_cpup_single(cpg, /*h_parent*/NULL);
5964+ cpg->flags = flags_orig;
027c5e7a
AM
5965+ if (file) {
5966+ if (!err)
5967+ err = au_reopen_nondir(file);
5afbbe0d 5968+ hsrc.hd->hd_dentry = hsrc.h_dentry;
1facf9fc 5969+ }
5afbbe0d
AM
5970+ hdst.hd->hd_dentry = hdst.h_dentry;
5971+ dinfo->di_btop = cpg->bsrc;
c2b27bf2 5972+ cpg->bsrc = bsrc_orig;
1facf9fc 5973+
5974+ return err;
5975+}
5976+
c2b27bf2 5977+static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
1facf9fc 5978+{
5979+ int err;
c2b27bf2 5980+ aufs_bindex_t bdst;
1facf9fc 5981+ struct au_dtime dt;
c2b27bf2 5982+ struct dentry *dentry, *parent, *h_parent, *wh_dentry;
1facf9fc 5983+ struct au_branch *br;
5984+ struct path h_path;
5985+
c2b27bf2
AM
5986+ dentry = cpg->dentry;
5987+ bdst = cpg->bdst;
1facf9fc 5988+ br = au_sbr(dentry->d_sb, bdst);
5989+ parent = dget_parent(dentry);
5990+ h_parent = au_h_dptr(parent, bdst);
5991+ wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
5992+ err = PTR_ERR(wh_dentry);
5993+ if (IS_ERR(wh_dentry))
5994+ goto out;
5995+
5996+ h_path.dentry = h_parent;
86dc4139 5997+ h_path.mnt = au_br_mnt(br);
1facf9fc 5998+ au_dtime_store(&dt, parent, &h_path);
c2b27bf2 5999+ err = au_do_cpup_wh(cpg, wh_dentry, file);
1facf9fc 6000+ if (unlikely(err))
6001+ goto out_wh;
6002+
6003+ dget(wh_dentry);
6004+ h_path.dentry = wh_dentry;
2000de60 6005+ if (!d_is_dir(wh_dentry)) {
523b37e3 6006+ /* no delegation since it is just created */
5527c038 6007+ err = vfsub_unlink(d_inode(h_parent), &h_path,
523b37e3
AM
6008+ /*delegated*/NULL, /*force*/0);
6009+ } else
5527c038 6010+ err = vfsub_rmdir(d_inode(h_parent), &h_path);
1facf9fc 6011+ if (unlikely(err)) {
523b37e3
AM
6012+ AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
6013+ wh_dentry, err);
1facf9fc 6014+ err = -EIO;
6015+ }
6016+ au_dtime_revert(&dt);
5527c038 6017+ au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
1facf9fc 6018+
4f0767ce 6019+out_wh:
1facf9fc 6020+ dput(wh_dentry);
4f0767ce 6021+out:
1facf9fc 6022+ dput(parent);
6023+ return err;
6024+}
6025+
6026+struct au_cpup_wh_args {
6027+ int *errp;
c2b27bf2 6028+ struct au_cp_generic *cpg;
1facf9fc 6029+ struct file *file;
6030+};
6031+
6032+static void au_call_cpup_wh(void *args)
6033+{
6034+ struct au_cpup_wh_args *a = args;
86dc4139 6035+
c2b27bf2
AM
6036+ au_pin_hdir_acquire_nest(a->cpg->pin);
6037+ *a->errp = au_cpup_wh(a->cpg, a->file);
6038+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 6039+}
6040+
c2b27bf2 6041+int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
1facf9fc 6042+{
6043+ int err, wkq_err;
c2b27bf2 6044+ aufs_bindex_t bdst;
c1595e42 6045+ struct dentry *dentry, *parent, *h_orph, *h_parent;
86dc4139 6046+ struct inode *dir, *h_dir, *h_tmpdir;
1facf9fc 6047+ struct au_wbr *wbr;
c2b27bf2 6048+ struct au_pin wh_pin, *pin_orig;
0b2a12c6 6049+ struct user_namespace *h_userns;
1facf9fc 6050+
c2b27bf2
AM
6051+ dentry = cpg->dentry;
6052+ bdst = cpg->bdst;
1facf9fc 6053+ parent = dget_parent(dentry);
5527c038 6054+ dir = d_inode(parent);
1facf9fc 6055+ h_orph = NULL;
6056+ h_parent = NULL;
6057+ h_dir = au_igrab(au_h_iptr(dir, bdst));
6058+ h_tmpdir = h_dir;
c2b27bf2 6059+ pin_orig = NULL;
1facf9fc 6060+ if (!h_dir->i_nlink) {
6061+ wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
6062+ h_orph = wbr->wbr_orph;
6063+
6064+ h_parent = dget(au_h_dptr(parent, bdst));
1facf9fc 6065+ au_set_h_dptr(parent, bdst, dget(h_orph));
5527c038 6066+ h_tmpdir = d_inode(h_orph);
1facf9fc 6067+ au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
6068+
febd17d6 6069+ inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
4a4d8108 6070+ /* todo: au_h_open_pre()? */
86dc4139 6071+
c2b27bf2 6072+ pin_orig = cpg->pin;
86dc4139 6073+ au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
c2b27bf2
AM
6074+ AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
6075+ cpg->pin = &wh_pin;
1facf9fc 6076+ }
6077+
0b2a12c6
JR
6078+ h_userns = au_sbr_userns(dentry->d_sb, bdst);
6079+ if (!au_test_h_perm_sio(h_userns, h_tmpdir, MAY_EXEC | MAY_WRITE)
5527c038 6080+ && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
c2b27bf2 6081+ err = au_cpup_wh(cpg, file);
1facf9fc 6082+ else {
6083+ struct au_cpup_wh_args args = {
6084+ .errp = &err,
c2b27bf2
AM
6085+ .cpg = cpg,
6086+ .file = file
1facf9fc 6087+ };
6088+ wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
6089+ if (unlikely(wkq_err))
6090+ err = wkq_err;
6091+ }
6092+
6093+ if (h_orph) {
febd17d6 6094+ inode_unlock(h_tmpdir);
4a4d8108 6095+ /* todo: au_h_open_post()? */
1facf9fc 6096+ au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
1facf9fc 6097+ au_set_h_dptr(parent, bdst, h_parent);
c2b27bf2
AM
6098+ AuDebugOn(!pin_orig);
6099+ cpg->pin = pin_orig;
1facf9fc 6100+ }
6101+ iput(h_dir);
6102+ dput(parent);
6103+
6104+ return err;
6105+}
6106+
6107+/* ---------------------------------------------------------------------- */
6108+
6109+/*
6110+ * generic routine for both of copy-up and copy-down.
6111+ */
6112+/* cf. revalidate function in file.c */
6113+int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6114+ int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 6115+ struct au_pin *pin,
1facf9fc 6116+ struct dentry *h_parent, void *arg),
6117+ void *arg)
6118+{
6119+ int err;
6120+ struct au_pin pin;
5527c038 6121+ struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
1facf9fc 6122+
6123+ err = 0;
6124+ parent = dget_parent(dentry);
6125+ if (IS_ROOT(parent))
6126+ goto out;
6127+
6128+ au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
6129+ au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
6130+
6131+ /* do not use au_dpage */
6132+ real_parent = parent;
6133+ while (1) {
6134+ dput(parent);
6135+ parent = dget_parent(dentry);
6136+ h_parent = au_h_dptr(parent, bdst);
6137+ if (h_parent)
6138+ goto out; /* success */
6139+
6140+ /* find top dir which is necessary to cpup */
6141+ do {
6142+ d = parent;
6143+ dput(parent);
6144+ parent = dget_parent(d);
6145+ di_read_lock_parent3(parent, !AuLock_IR);
6146+ h_parent = au_h_dptr(parent, bdst);
6147+ di_read_unlock(parent, !AuLock_IR);
6148+ } while (!h_parent);
6149+
6150+ if (d != real_parent)
6151+ di_write_lock_child3(d);
6152+
6153+ /* somebody else might create while we were sleeping */
5527c038
JR
6154+ h_dentry = au_h_dptr(d, bdst);
6155+ if (!h_dentry || d_is_negative(h_dentry)) {
6156+ if (h_dentry)
5afbbe0d 6157+ au_update_dbtop(d);
1facf9fc 6158+
6159+ au_pin_set_dentry(&pin, d);
6160+ err = au_do_pin(&pin);
6161+ if (!err) {
86dc4139 6162+ err = cp(d, bdst, &pin, h_parent, arg);
1facf9fc 6163+ au_unpin(&pin);
6164+ }
6165+ }
6166+
6167+ if (d != real_parent)
6168+ di_write_unlock(d);
6169+ if (unlikely(err))
6170+ break;
6171+ }
6172+
4f0767ce 6173+out:
1facf9fc 6174+ dput(parent);
6175+ return err;
6176+}
6177+
6178+static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 6179+ struct au_pin *pin,
2000de60 6180+ struct dentry *h_parent __maybe_unused,
1facf9fc 6181+ void *arg __maybe_unused)
6182+{
c2b27bf2
AM
6183+ struct au_cp_generic cpg = {
6184+ .dentry = dentry,
6185+ .bdst = bdst,
6186+ .bsrc = -1,
6187+ .len = 0,
6188+ .pin = pin,
6189+ .flags = AuCpup_DTIME
6190+ };
6191+ return au_sio_cpup_simple(&cpg);
1facf9fc 6192+}
6193+
6194+int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6195+{
6196+ return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
6197+}
6198+
6199+int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6200+{
6201+ int err;
6202+ struct dentry *parent;
6203+ struct inode *dir;
6204+
6205+ parent = dget_parent(dentry);
5527c038 6206+ dir = d_inode(parent);
1facf9fc 6207+ err = 0;
6208+ if (au_h_iptr(dir, bdst))
6209+ goto out;
6210+
6211+ di_read_unlock(parent, AuLock_IR);
6212+ di_write_lock_parent(parent);
6213+ /* someone else might change our inode while we were sleeping */
6214+ if (!au_h_iptr(dir, bdst))
6215+ err = au_cpup_dirs(dentry, bdst);
6216+ di_downgrade_lock(parent, AuLock_IR);
6217+
4f0767ce 6218+out:
1facf9fc 6219+ dput(parent);
6220+ return err;
6221+}
7f207e10 6222diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
eca34b5c 6223--- /usr/share/empty/fs/aufs/cpup.h 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 6224+++ linux/fs/aufs/cpup.h 2022-11-05 23:02:18.962555950 +0100
062440b3
AM
6225@@ -0,0 +1,100 @@
6226+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 6227+/*
0dcfbb52 6228+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 6229+ *
0dcfbb52 6230+ * This program is free software; you can redistribute it and/or modify
1facf9fc 6231+ * it under the terms of the GNU General Public License as published by
6232+ * the Free Software Foundation; either version 2 of the License, or
6233+ * (at your option) any later version.
dece6358
AM
6234+ *
6235+ * This program is distributed in the hope that it will be useful,
6236+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6237+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6238+ * GNU General Public License for more details.
6239+ *
6240+ * You should have received a copy of the GNU General Public License
523b37e3 6241+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6242+ */
6243+
6244+/*
6245+ * copy-up/down functions
6246+ */
6247+
6248+#ifndef __AUFS_CPUP_H__
6249+#define __AUFS_CPUP_H__
6250+
6251+#ifdef __KERNEL__
6252+
dece6358 6253+#include <linux/path.h>
1facf9fc 6254+
dece6358
AM
6255+struct inode;
6256+struct file;
86dc4139 6257+struct au_pin;
dece6358 6258+
86dc4139 6259+void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
1facf9fc 6260+void au_cpup_attr_timesizes(struct inode *inode);
6261+void au_cpup_attr_nlink(struct inode *inode, int force);
6262+void au_cpup_attr_changeable(struct inode *inode);
6263+void au_cpup_igen(struct inode *inode, struct inode *h_inode);
6264+void au_cpup_attr_all(struct inode *inode, int force);
6265+
6266+/* ---------------------------------------------------------------------- */
6267+
c2b27bf2
AM
6268+struct au_cp_generic {
6269+ struct dentry *dentry;
6270+ aufs_bindex_t bdst, bsrc;
6271+ loff_t len;
6272+ struct au_pin *pin;
6273+ unsigned int flags;
6274+};
6275+
1facf9fc 6276+/* cpup flags */
392086de
AM
6277+#define AuCpup_DTIME 1 /* do dtime_store/revert */
6278+#define AuCpup_KEEPLINO (1 << 1) /* do not clear the lower xino,
6279+ for link(2) */
6280+#define AuCpup_RENAME (1 << 2) /* rename after cpup */
6281+#define AuCpup_HOPEN (1 << 3) /* call h_open_pre/post() in
6282+ cpup */
6283+#define AuCpup_OVERWRITE (1 << 4) /* allow overwriting the
6284+ existing entry */
6285+#define AuCpup_RWDST (1 << 5) /* force write target even if
6286+ the branch is marked as RO */
c2b27bf2 6287+
8b6a4947
AM
6288+#ifndef CONFIG_AUFS_BR_HFSPLUS
6289+#undef AuCpup_HOPEN
6290+#define AuCpup_HOPEN 0
6291+#endif
6292+
1facf9fc 6293+#define au_ftest_cpup(flags, name) ((flags) & AuCpup_##name)
7f207e10
AM
6294+#define au_fset_cpup(flags, name) \
6295+ do { (flags) |= AuCpup_##name; } while (0)
6296+#define au_fclr_cpup(flags, name) \
6297+ do { (flags) &= ~AuCpup_##name; } while (0)
1facf9fc 6298+
6299+int au_copy_file(struct file *dst, struct file *src, loff_t len);
c2b27bf2
AM
6300+int au_sio_cpup_simple(struct au_cp_generic *cpg);
6301+int au_sio_cpdown_simple(struct au_cp_generic *cpg);
6302+int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
1facf9fc 6303+
6304+int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6305+ int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 6306+ struct au_pin *pin,
1facf9fc 6307+ struct dentry *h_parent, void *arg),
6308+ void *arg);
6309+int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6310+int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6311+
6312+/* ---------------------------------------------------------------------- */
6313+
6314+/* keep timestamps when copyup */
6315+struct au_dtime {
6316+ struct dentry *dt_dentry;
6317+ struct path dt_h_path;
cd7a4cd9 6318+ struct timespec64 dt_atime, dt_mtime;
1facf9fc 6319+};
6320+void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6321+ struct path *h_path);
6322+void au_dtime_revert(struct au_dtime *dt);
6323+
6324+#endif /* __KERNEL__ */
6325+#endif /* __AUFS_CPUP_H__ */
7f207e10 6326diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
eca34b5c 6327--- /usr/share/empty/fs/aufs/dbgaufs.c 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 6328+++ linux/fs/aufs/dbgaufs.c 2022-11-05 23:02:18.962555950 +0100
9f237c51 6329@@ -0,0 +1,526 @@
cd7a4cd9 6330+// SPDX-License-Identifier: GPL-2.0
1facf9fc 6331+/*
0dcfbb52 6332+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 6333+ *
0dcfbb52 6334+ * This program is free software; you can redistribute it and/or modify
1facf9fc 6335+ * it under the terms of the GNU General Public License as published by
6336+ * the Free Software Foundation; either version 2 of the License, or
6337+ * (at your option) any later version.
dece6358
AM
6338+ *
6339+ * This program is distributed in the hope that it will be useful,
6340+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6341+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6342+ * GNU General Public License for more details.
6343+ *
6344+ * You should have received a copy of the GNU General Public License
523b37e3 6345+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6346+ */
6347+
6348+/*
6349+ * debugfs interface
6350+ */
6351+
6352+#include <linux/debugfs.h>
6353+#include "aufs.h"
6354+
6355+#ifndef CONFIG_SYSFS
6356+#error DEBUG_FS depends upon SYSFS
6357+#endif
6358+
6359+static struct dentry *dbgaufs;
cd7a4cd9 6360+static const mode_t dbgaufs_mode = 0444;
1facf9fc 6361+
6362+/* 20 is max digits length of ulong 64 */
6363+struct dbgaufs_arg {
6364+ int n;
6365+ char a[20 * 4];
6366+};
6367+
6368+/*
6369+ * common function for all XINO files
6370+ */
6371+static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6372+ struct file *file)
6373+{
9f237c51
AM
6374+ void *p;
6375+
6376+ p = file->private_data;
6377+ if (p) {
6378+ /* this is struct dbgaufs_arg */
6379+ AuDebugOn(!au_kfree_sz_test(p));
6380+ au_kfree_do_rcu(p);
6381+ }
1facf9fc 6382+ return 0;
6383+}
6384+
062440b3
AM
6385+static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt,
6386+ int cnt)
1facf9fc 6387+{
6388+ int err;
6389+ struct kstat st;
6390+ struct dbgaufs_arg *p;
6391+
6392+ err = -ENOMEM;
6393+ p = kmalloc(sizeof(*p), GFP_NOFS);
6394+ if (unlikely(!p))
6395+ goto out;
6396+
6397+ err = 0;
6398+ p->n = 0;
6399+ file->private_data = p;
6400+ if (!xf)
6401+ goto out;
6402+
521ced18 6403+ err = vfsub_getattr(&xf->f_path, &st);
1facf9fc 6404+ if (!err) {
6405+ if (do_fcnt)
6406+ p->n = snprintf
062440b3
AM
6407+ (p->a, sizeof(p->a), "%d, %llux%u %lld\n",
6408+ cnt, st.blocks, st.blksize,
1facf9fc 6409+ (long long)st.size);
6410+ else
521ced18 6411+ p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
1facf9fc 6412+ st.blocks, st.blksize,
6413+ (long long)st.size);
6414+ AuDebugOn(p->n >= sizeof(p->a));
6415+ } else {
6416+ p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6417+ err = 0;
6418+ }
6419+
4f0767ce 6420+out:
1facf9fc 6421+ return err;
1facf9fc 6422+}
6423+
6424+static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6425+ size_t count, loff_t *ppos)
6426+{
6427+ struct dbgaufs_arg *p;
6428+
6429+ p = file->private_data;
6430+ return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6431+}
6432+
6433+/* ---------------------------------------------------------------------- */
6434+
86dc4139
AM
6435+struct dbgaufs_plink_arg {
6436+ int n;
6437+ char a[];
6438+};
6439+
6440+static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6441+ struct file *file)
6442+{
1c60b727 6443+ free_page((unsigned long)file->private_data);
86dc4139
AM
6444+ return 0;
6445+}
6446+
6447+static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6448+{
6449+ int err, i, limit;
6450+ unsigned long n, sum;
6451+ struct dbgaufs_plink_arg *p;
6452+ struct au_sbinfo *sbinfo;
6453+ struct super_block *sb;
8b6a4947 6454+ struct hlist_bl_head *hbl;
86dc4139
AM
6455+
6456+ err = -ENOMEM;
6457+ p = (void *)get_zeroed_page(GFP_NOFS);
6458+ if (unlikely(!p))
6459+ goto out;
6460+
6461+ err = -EFBIG;
6462+ sbinfo = inode->i_private;
6463+ sb = sbinfo->si_sb;
6464+ si_noflush_read_lock(sb);
6465+ if (au_opt_test(au_mntflags(sb), PLINK)) {
6466+ limit = PAGE_SIZE - sizeof(p->n);
6467+
6468+ /* the number of buckets */
6469+ n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6470+ p->n += n;
6471+ limit -= n;
6472+
6473+ sum = 0;
8b6a4947
AM
6474+ for (i = 0, hbl = sbinfo->si_plink; i < AuPlink_NHASH;
6475+ i++, hbl++) {
6476+ n = au_hbl_count(hbl);
86dc4139
AM
6477+ sum += n;
6478+
6479+ n = snprintf(p->a + p->n, limit, "%lu ", n);
6480+ p->n += n;
6481+ limit -= n;
6482+ if (unlikely(limit <= 0))
6483+ goto out_free;
6484+ }
6485+ p->a[p->n - 1] = '\n';
6486+
6487+ /* the sum of plinks */
6488+ n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6489+ p->n += n;
6490+ limit -= n;
6491+ if (unlikely(limit <= 0))
6492+ goto out_free;
6493+ } else {
6494+#define str "1\n0\n0\n"
6495+ p->n = sizeof(str) - 1;
6496+ strcpy(p->a, str);
6497+#undef str
6498+ }
6499+ si_read_unlock(sb);
6500+
6501+ err = 0;
6502+ file->private_data = p;
6503+ goto out; /* success */
6504+
6505+out_free:
1c60b727 6506+ free_page((unsigned long)p);
86dc4139
AM
6507+out:
6508+ return err;
6509+}
6510+
6511+static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6512+ size_t count, loff_t *ppos)
6513+{
6514+ struct dbgaufs_plink_arg *p;
6515+
6516+ p = file->private_data;
6517+ return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6518+}
6519+
6520+static const struct file_operations dbgaufs_plink_fop = {
6521+ .owner = THIS_MODULE,
6522+ .open = dbgaufs_plink_open,
6523+ .release = dbgaufs_plink_release,
6524+ .read = dbgaufs_plink_read
6525+};
6526+
6527+/* ---------------------------------------------------------------------- */
6528+
1facf9fc 6529+static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6530+{
6531+ int err;
6532+ struct au_sbinfo *sbinfo;
6533+ struct super_block *sb;
6534+
6535+ sbinfo = inode->i_private;
6536+ sb = sbinfo->si_sb;
6537+ si_noflush_read_lock(sb);
062440b3 6538+ err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0, /*cnt*/0);
1facf9fc 6539+ si_read_unlock(sb);
6540+ return err;
6541+}
6542+
6543+static const struct file_operations dbgaufs_xib_fop = {
4a4d8108 6544+ .owner = THIS_MODULE,
1facf9fc 6545+ .open = dbgaufs_xib_open,
6546+ .release = dbgaufs_xi_release,
6547+ .read = dbgaufs_xi_read
6548+};
6549+
6550+/* ---------------------------------------------------------------------- */
6551+
6552+#define DbgaufsXi_PREFIX "xi"
6553+
6554+static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6555+{
acd2b654 6556+ int err, idx;
1facf9fc 6557+ long l;
acd2b654
AM
6558+ aufs_bindex_t bindex;
6559+ char *p, a[sizeof(DbgaufsXi_PREFIX) + 8];
1facf9fc 6560+ struct au_sbinfo *sbinfo;
6561+ struct super_block *sb;
acd2b654 6562+ struct au_xino *xi;
1facf9fc 6563+ struct file *xf;
6564+ struct qstr *name;
062440b3 6565+ struct au_branch *br;
1facf9fc 6566+
6567+ err = -ENOENT;
2000de60 6568+ name = &file->f_path.dentry->d_name;
1facf9fc 6569+ if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6570+ || memcmp(name->name, DbgaufsXi_PREFIX,
6571+ sizeof(DbgaufsXi_PREFIX) - 1)))
6572+ goto out;
acd2b654
AM
6573+
6574+ AuDebugOn(name->len >= sizeof(a));
6575+ memcpy(a, name->name, name->len);
6576+ a[name->len] = '\0';
6577+ p = strchr(a, '-');
6578+ if (p)
6579+ *p = '\0';
6580+ err = kstrtol(a + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
1facf9fc 6581+ if (unlikely(err))
6582+ goto out;
acd2b654
AM
6583+ bindex = l;
6584+ idx = 0;
6585+ if (p) {
6586+ err = kstrtol(p + 1, 10, &l);
6587+ if (unlikely(err))
6588+ goto out;
6589+ idx = l;
6590+ }
1facf9fc 6591+
acd2b654 6592+ err = -ENOENT;
1facf9fc 6593+ sbinfo = inode->i_private;
6594+ sb = sbinfo->si_sb;
6595+ si_noflush_read_lock(sb);
acd2b654
AM
6596+ if (unlikely(bindex < 0 || bindex > au_sbbot(sb)))
6597+ goto out_si;
6598+ br = au_sbr(sb, bindex);
6599+ xi = br->br_xino;
6600+ if (unlikely(idx >= xi->xi_nfile))
6601+ goto out_si;
6602+ xf = au_xino_file(xi, idx);
6603+ if (xf)
062440b3
AM
6604+ err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1,
6605+ au_xino_count(br));
1facf9fc 6606+
acd2b654
AM
6607+out_si:
6608+ si_read_unlock(sb);
4f0767ce 6609+out:
acd2b654 6610+ AuTraceErr(err);
1facf9fc 6611+ return err;
6612+}
6613+
6614+static const struct file_operations dbgaufs_xino_fop = {
4a4d8108 6615+ .owner = THIS_MODULE,
1facf9fc 6616+ .open = dbgaufs_xino_open,
6617+ .release = dbgaufs_xi_release,
6618+ .read = dbgaufs_xi_read
6619+};
6620+
062440b3
AM
6621+void dbgaufs_xino_del(struct au_branch *br)
6622+{
6623+ struct dentry *dbgaufs;
6624+
6625+ dbgaufs = br->br_dbgaufs;
6626+ if (!dbgaufs)
6627+ return;
6628+
6629+ br->br_dbgaufs = NULL;
6630+ /* debugfs acquires the parent i_mutex */
6631+ lockdep_off();
6632+ debugfs_remove(dbgaufs);
6633+ lockdep_on();
6634+}
6635+
1facf9fc 6636+void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6637+{
5afbbe0d 6638+ aufs_bindex_t bbot;
1facf9fc 6639+ struct au_branch *br;
1facf9fc 6640+
6641+ if (!au_sbi(sb)->si_dbgaufs)
6642+ return;
6643+
5afbbe0d
AM
6644+ bbot = au_sbbot(sb);
6645+ for (; bindex <= bbot; bindex++) {
1facf9fc 6646+ br = au_sbr(sb, bindex);
062440b3
AM
6647+ dbgaufs_xino_del(br);
6648+ }
6649+}
6650+
acd2b654
AM
6651+static void dbgaufs_br_do_add(struct super_block *sb, aufs_bindex_t bindex,
6652+ unsigned int idx, struct dentry *parent,
6653+ struct au_sbinfo *sbinfo)
062440b3
AM
6654+{
6655+ struct au_branch *br;
6656+ struct dentry *d;
acd2b654
AM
6657+ /* "xi" bindex(5) "-" idx(2) NULL */
6658+ char name[sizeof(DbgaufsXi_PREFIX) + 8];
062440b3 6659+
acd2b654
AM
6660+ if (!idx)
6661+ snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6662+ else
6663+ snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d-%u",
6664+ bindex, idx);
062440b3
AM
6665+ br = au_sbr(sb, bindex);
6666+ if (br->br_dbgaufs) {
6667+ struct qstr qstr = QSTR_INIT(name, strlen(name));
6668+
6669+ if (!au_qstreq(&br->br_dbgaufs->d_name, &qstr)) {
6670+ /* debugfs acquires the parent i_mutex */
6671+ lockdep_off();
6672+ d = debugfs_rename(parent, br->br_dbgaufs, parent,
6673+ name);
6674+ lockdep_on();
6675+ if (unlikely(!d))
6676+ pr_warn("failed renaming %pd/%s, ignored.\n",
6677+ parent, name);
6678+ }
6679+ } else {
e2f27e51 6680+ lockdep_off();
062440b3
AM
6681+ br->br_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6682+ sbinfo, &dbgaufs_xino_fop);
e2f27e51 6683+ lockdep_on();
062440b3 6684+ if (unlikely(!br->br_dbgaufs))
acd2b654 6685+ pr_warn("failed creating %pd/%s, ignored.\n",
062440b3 6686+ parent, name);
1facf9fc 6687+ }
6688+}
6689+
acd2b654
AM
6690+static void dbgaufs_br_add(struct super_block *sb, aufs_bindex_t bindex,
6691+ struct dentry *parent, struct au_sbinfo *sbinfo)
6692+{
6693+ struct au_branch *br;
6694+ struct au_xino *xi;
6695+ unsigned int u;
6696+
6697+ br = au_sbr(sb, bindex);
6698+ xi = br->br_xino;
6699+ for (u = 0; u < xi->xi_nfile; u++)
6700+ dbgaufs_br_do_add(sb, bindex, u, parent, sbinfo);
6701+}
6702+
062440b3 6703+void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown)
1facf9fc 6704+{
6705+ struct au_sbinfo *sbinfo;
6706+ struct dentry *parent;
5afbbe0d 6707+ aufs_bindex_t bbot;
062440b3
AM
6708+
6709+ if (!au_opt_test(au_mntflags(sb), XINO))
6710+ return;
1facf9fc 6711+
6712+ sbinfo = au_sbi(sb);
6713+ parent = sbinfo->si_dbgaufs;
6714+ if (!parent)
6715+ return;
6716+
5afbbe0d 6717+ bbot = au_sbbot(sb);
062440b3
AM
6718+ if (topdown)
6719+ for (; bindex <= bbot; bindex++)
6720+ dbgaufs_br_add(sb, bindex, parent, sbinfo);
6721+ else
6722+ for (; bbot >= bindex; bbot--)
6723+ dbgaufs_br_add(sb, bbot, parent, sbinfo);
1facf9fc 6724+}
6725+
6726+/* ---------------------------------------------------------------------- */
6727+
6728+#ifdef CONFIG_AUFS_EXPORT
6729+static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6730+{
6731+ int err;
6732+ struct au_sbinfo *sbinfo;
6733+ struct super_block *sb;
6734+
6735+ sbinfo = inode->i_private;
6736+ sb = sbinfo->si_sb;
6737+ si_noflush_read_lock(sb);
062440b3 6738+ err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0, /*cnt*/0);
1facf9fc 6739+ si_read_unlock(sb);
6740+ return err;
6741+}
6742+
6743+static const struct file_operations dbgaufs_xigen_fop = {
4a4d8108 6744+ .owner = THIS_MODULE,
1facf9fc 6745+ .open = dbgaufs_xigen_open,
6746+ .release = dbgaufs_xi_release,
6747+ .read = dbgaufs_xi_read
6748+};
6749+
6750+static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6751+{
6752+ int err;
6753+
dece6358 6754+ /*
c1595e42 6755+ * This function is a dynamic '__init' function actually,
dece6358
AM
6756+ * so the tiny check for si_rwsem is unnecessary.
6757+ */
6758+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6759+
1facf9fc 6760+ err = -EIO;
6761+ sbinfo->si_dbgaufs_xigen = debugfs_create_file
6762+ ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6763+ &dbgaufs_xigen_fop);
6764+ if (sbinfo->si_dbgaufs_xigen)
6765+ err = 0;
6766+
6767+ return err;
6768+}
6769+#else
6770+static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6771+{
6772+ return 0;
6773+}
6774+#endif /* CONFIG_AUFS_EXPORT */
6775+
6776+/* ---------------------------------------------------------------------- */
6777+
6778+void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6779+{
dece6358 6780+ /*
7e9cd9fe 6781+ * This function is a dynamic '__fin' function actually,
dece6358
AM
6782+ * so the tiny check for si_rwsem is unnecessary.
6783+ */
6784+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6785+
1facf9fc 6786+ debugfs_remove_recursive(sbinfo->si_dbgaufs);
6787+ sbinfo->si_dbgaufs = NULL;
1facf9fc 6788+}
6789+
6790+int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6791+{
6792+ int err;
6793+ char name[SysaufsSiNameLen];
6794+
dece6358 6795+ /*
c1595e42 6796+ * This function is a dynamic '__init' function actually,
dece6358
AM
6797+ * so the tiny check for si_rwsem is unnecessary.
6798+ */
6799+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6800+
1facf9fc 6801+ err = -ENOENT;
6802+ if (!dbgaufs) {
6803+ AuErr1("/debug/aufs is uninitialized\n");
6804+ goto out;
6805+ }
6806+
6807+ err = -EIO;
6808+ sysaufs_name(sbinfo, name);
6809+ sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6810+ if (unlikely(!sbinfo->si_dbgaufs))
6811+ goto out;
1facf9fc 6812+
062440b3 6813+ /* regardless plink/noplink option */
86dc4139
AM
6814+ sbinfo->si_dbgaufs_plink = debugfs_create_file
6815+ ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6816+ &dbgaufs_plink_fop);
6817+ if (unlikely(!sbinfo->si_dbgaufs_plink))
6818+ goto out_dir;
6819+
062440b3
AM
6820+ /* regardless xino/noxino option */
6821+ sbinfo->si_dbgaufs_xib = debugfs_create_file
6822+ ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6823+ &dbgaufs_xib_fop);
6824+ if (unlikely(!sbinfo->si_dbgaufs_xib))
6825+ goto out_dir;
6826+
1facf9fc 6827+ err = dbgaufs_xigen_init(sbinfo);
6828+ if (!err)
6829+ goto out; /* success */
6830+
4f0767ce 6831+out_dir:
1facf9fc 6832+ dbgaufs_si_fin(sbinfo);
4f0767ce 6833+out:
062440b3
AM
6834+ if (unlikely(err))
6835+ pr_err("debugfs/aufs failed\n");
1facf9fc 6836+ return err;
6837+}
6838+
6839+/* ---------------------------------------------------------------------- */
6840+
6841+void dbgaufs_fin(void)
6842+{
6843+ debugfs_remove(dbgaufs);
6844+}
6845+
6846+int __init dbgaufs_init(void)
6847+{
6848+ int err;
6849+
6850+ err = -EIO;
6851+ dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
6852+ if (dbgaufs)
6853+ err = 0;
6854+ return err;
6855+}
7f207e10 6856diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
eca34b5c 6857--- /usr/share/empty/fs/aufs/dbgaufs.h 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 6858+++ linux/fs/aufs/dbgaufs.h 2022-11-05 23:02:18.962555950 +0100
062440b3
AM
6859@@ -0,0 +1,53 @@
6860+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 6861+/*
0dcfbb52 6862+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 6863+ *
0dcfbb52 6864+ * This program is free software; you can redistribute it and/or modify
1facf9fc 6865+ * it under the terms of the GNU General Public License as published by
6866+ * the Free Software Foundation; either version 2 of the License, or
6867+ * (at your option) any later version.
dece6358
AM
6868+ *
6869+ * This program is distributed in the hope that it will be useful,
6870+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6871+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6872+ * GNU General Public License for more details.
6873+ *
6874+ * You should have received a copy of the GNU General Public License
523b37e3 6875+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6876+ */
6877+
6878+/*
6879+ * debugfs interface
6880+ */
6881+
6882+#ifndef __DBGAUFS_H__
6883+#define __DBGAUFS_H__
6884+
6885+#ifdef __KERNEL__
6886+
dece6358 6887+struct super_block;
1facf9fc 6888+struct au_sbinfo;
062440b3 6889+struct au_branch;
dece6358 6890+
1facf9fc 6891+#ifdef CONFIG_DEBUG_FS
6892+/* dbgaufs.c */
062440b3 6893+void dbgaufs_xino_del(struct au_branch *br);
1facf9fc 6894+void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
062440b3 6895+void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown);
1facf9fc 6896+void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
6897+int dbgaufs_si_init(struct au_sbinfo *sbinfo);
6898+void dbgaufs_fin(void);
6899+int __init dbgaufs_init(void);
1facf9fc 6900+#else
062440b3 6901+AuStubVoid(dbgaufs_xino_del, struct au_branch *br)
4a4d8108 6902+AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
062440b3
AM
6903+AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex,
6904+ int topdown)
4a4d8108
AM
6905+AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
6906+AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
6907+AuStubVoid(dbgaufs_fin, void)
6908+AuStubInt0(__init dbgaufs_init, void)
1facf9fc 6909+#endif /* CONFIG_DEBUG_FS */
6910+
6911+#endif /* __KERNEL__ */
6912+#endif /* __DBGAUFS_H__ */
7f207e10 6913diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
eca34b5c 6914--- /usr/share/empty/fs/aufs/dcsub.c 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 6915+++ linux/fs/aufs/dcsub.c 2022-11-05 23:02:18.962555950 +0100
cd7a4cd9
AM
6916@@ -0,0 +1,225 @@
6917+// SPDX-License-Identifier: GPL-2.0
1facf9fc 6918+/*
0dcfbb52 6919+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 6920+ *
0dcfbb52 6921+ * This program is free software; you can redistribute it and/or modify
1facf9fc 6922+ * it under the terms of the GNU General Public License as published by
6923+ * the Free Software Foundation; either version 2 of the License, or
6924+ * (at your option) any later version.
dece6358
AM
6925+ *
6926+ * This program is distributed in the hope that it will be useful,
6927+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6928+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6929+ * GNU General Public License for more details.
6930+ *
6931+ * You should have received a copy of the GNU General Public License
523b37e3 6932+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6933+ */
6934+
6935+/*
6936+ * sub-routines for dentry cache
6937+ */
6938+
6939+#include "aufs.h"
6940+
6941+static void au_dpage_free(struct au_dpage *dpage)
6942+{
6943+ int i;
6944+ struct dentry **p;
6945+
6946+ p = dpage->dentries;
6947+ for (i = 0; i < dpage->ndentry; i++)
6948+ dput(*p++);
1c60b727 6949+ free_page((unsigned long)dpage->dentries);
1facf9fc 6950+}
6951+
6952+int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
6953+{
6954+ int err;
6955+ void *p;
6956+
6957+ err = -ENOMEM;
6958+ dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
6959+ if (unlikely(!dpages->dpages))
6960+ goto out;
6961+
6962+ p = (void *)__get_free_page(gfp);
6963+ if (unlikely(!p))
6964+ goto out_dpages;
6965+
6966+ dpages->dpages[0].ndentry = 0;
6967+ dpages->dpages[0].dentries = p;
6968+ dpages->ndpage = 1;
6969+ return 0; /* success */
6970+
4f0767ce 6971+out_dpages:
9f237c51 6972+ au_kfree_try_rcu(dpages->dpages);
4f0767ce 6973+out:
1facf9fc 6974+ return err;
6975+}
6976+
6977+void au_dpages_free(struct au_dcsub_pages *dpages)
6978+{
6979+ int i;
6980+ struct au_dpage *p;
6981+
6982+ p = dpages->dpages;
6983+ for (i = 0; i < dpages->ndpage; i++)
6984+ au_dpage_free(p++);
9f237c51 6985+ au_kfree_try_rcu(dpages->dpages);
1facf9fc 6986+}
6987+
6988+static int au_dpages_append(struct au_dcsub_pages *dpages,
6989+ struct dentry *dentry, gfp_t gfp)
6990+{
6991+ int err, sz;
6992+ struct au_dpage *dpage;
6993+ void *p;
6994+
6995+ dpage = dpages->dpages + dpages->ndpage - 1;
6996+ sz = PAGE_SIZE / sizeof(dentry);
6997+ if (unlikely(dpage->ndentry >= sz)) {
6998+ AuLabel(new dpage);
6999+ err = -ENOMEM;
7000+ sz = dpages->ndpage * sizeof(*dpages->dpages);
7001+ p = au_kzrealloc(dpages->dpages, sz,
e2f27e51
AM
7002+ sz + sizeof(*dpages->dpages), gfp,
7003+ /*may_shrink*/0);
1facf9fc 7004+ if (unlikely(!p))
7005+ goto out;
7006+
7007+ dpages->dpages = p;
7008+ dpage = dpages->dpages + dpages->ndpage;
7009+ p = (void *)__get_free_page(gfp);
7010+ if (unlikely(!p))
7011+ goto out;
7012+
7013+ dpage->ndentry = 0;
7014+ dpage->dentries = p;
7015+ dpages->ndpage++;
7016+ }
7017+
c1595e42 7018+ AuDebugOn(au_dcount(dentry) <= 0);
027c5e7a 7019+ dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
1facf9fc 7020+ return 0; /* success */
7021+
4f0767ce 7022+out:
1facf9fc 7023+ return err;
7024+}
7025+
c1595e42
JR
7026+/* todo: BAD approach */
7027+/* copied from linux/fs/dcache.c */
7028+enum d_walk_ret {
7029+ D_WALK_CONTINUE,
7030+ D_WALK_QUIT,
7031+ D_WALK_NORETRY,
7032+ D_WALK_SKIP,
7033+};
7034+
7035+extern void d_walk(struct dentry *parent, void *data,
cd7a4cd9 7036+ enum d_walk_ret (*enter)(void *, struct dentry *));
c1595e42
JR
7037+
7038+struct ac_dpages_arg {
1facf9fc 7039+ int err;
c1595e42
JR
7040+ struct au_dcsub_pages *dpages;
7041+ struct super_block *sb;
7042+ au_dpages_test test;
7043+ void *arg;
7044+};
1facf9fc 7045+
c1595e42
JR
7046+static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
7047+{
7048+ enum d_walk_ret ret;
7049+ struct ac_dpages_arg *arg = _arg;
1facf9fc 7050+
c1595e42
JR
7051+ ret = D_WALK_CONTINUE;
7052+ if (dentry->d_sb == arg->sb
7053+ && !IS_ROOT(dentry)
7054+ && au_dcount(dentry) > 0
7055+ && au_di(dentry)
7056+ && (!arg->test || arg->test(dentry, arg->arg))) {
7057+ arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
7058+ if (unlikely(arg->err))
7059+ ret = D_WALK_QUIT;
1facf9fc 7060+ }
7061+
c1595e42
JR
7062+ return ret;
7063+}
027c5e7a 7064+
c1595e42
JR
7065+int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7066+ au_dpages_test test, void *arg)
7067+{
7068+ struct ac_dpages_arg args = {
7069+ .err = 0,
7070+ .dpages = dpages,
7071+ .sb = root->d_sb,
7072+ .test = test,
7073+ .arg = arg
7074+ };
027c5e7a 7075+
cd7a4cd9 7076+ d_walk(root, &args, au_call_dpages_append);
c1595e42
JR
7077+
7078+ return args.err;
1facf9fc 7079+}
7080+
7081+int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7082+ int do_include, au_dpages_test test, void *arg)
7083+{
7084+ int err;
7085+
7086+ err = 0;
027c5e7a
AM
7087+ write_seqlock(&rename_lock);
7088+ spin_lock(&dentry->d_lock);
7089+ if (do_include
c1595e42 7090+ && au_dcount(dentry) > 0
027c5e7a 7091+ && (!test || test(dentry, arg)))
1facf9fc 7092+ err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
027c5e7a
AM
7093+ spin_unlock(&dentry->d_lock);
7094+ if (unlikely(err))
7095+ goto out;
7096+
7097+ /*
523b37e3 7098+ * RCU for vfsmount is unnecessary since this is a traverse in a single
027c5e7a
AM
7099+ * mount
7100+ */
1facf9fc 7101+ while (!IS_ROOT(dentry)) {
027c5e7a
AM
7102+ dentry = dentry->d_parent; /* rename_lock is locked */
7103+ spin_lock(&dentry->d_lock);
c1595e42 7104+ if (au_dcount(dentry) > 0
027c5e7a 7105+ && (!test || test(dentry, arg)))
1facf9fc 7106+ err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
027c5e7a
AM
7107+ spin_unlock(&dentry->d_lock);
7108+ if (unlikely(err))
7109+ break;
1facf9fc 7110+ }
7111+
4f0767ce 7112+out:
027c5e7a 7113+ write_sequnlock(&rename_lock);
1facf9fc 7114+ return err;
7115+}
7116+
027c5e7a
AM
7117+static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
7118+{
7119+ return au_di(dentry) && dentry->d_sb == arg;
7120+}
7121+
7122+int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7123+ struct dentry *dentry, int do_include)
7124+{
7125+ return au_dcsub_pages_rev(dpages, dentry, do_include,
7126+ au_dcsub_dpages_aufs, dentry->d_sb);
7127+}
7128+
4a4d8108 7129+int au_test_subdir(struct dentry *d1, struct dentry *d2)
1facf9fc 7130+{
4a4d8108
AM
7131+ struct path path[2] = {
7132+ {
7133+ .dentry = d1
7134+ },
7135+ {
7136+ .dentry = d2
7137+ }
7138+ };
1facf9fc 7139+
4a4d8108 7140+ return path_is_under(path + 0, path + 1);
1facf9fc 7141+}
7f207e10 7142diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
eca34b5c 7143--- /usr/share/empty/fs/aufs/dcsub.h 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 7144+++ linux/fs/aufs/dcsub.h 2022-11-05 23:02:18.962555950 +0100
062440b3
AM
7145@@ -0,0 +1,137 @@
7146+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 7147+/*
0dcfbb52 7148+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 7149+ *
0dcfbb52 7150+ * This program is free software; you can redistribute it and/or modify
1facf9fc 7151+ * it under the terms of the GNU General Public License as published by
7152+ * the Free Software Foundation; either version 2 of the License, or
7153+ * (at your option) any later version.
dece6358
AM
7154+ *
7155+ * This program is distributed in the hope that it will be useful,
7156+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7157+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7158+ * GNU General Public License for more details.
7159+ *
7160+ * You should have received a copy of the GNU General Public License
523b37e3 7161+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7162+ */
7163+
7164+/*
7165+ * sub-routines for dentry cache
7166+ */
7167+
7168+#ifndef __AUFS_DCSUB_H__
7169+#define __AUFS_DCSUB_H__
7170+
7171+#ifdef __KERNEL__
7172+
7f207e10 7173+#include <linux/dcache.h>
027c5e7a 7174+#include <linux/fs.h>
dece6358 7175+
1facf9fc 7176+struct au_dpage {
7177+ int ndentry;
7178+ struct dentry **dentries;
7179+};
7180+
7181+struct au_dcsub_pages {
7182+ int ndpage;
7183+ struct au_dpage *dpages;
7184+};
7185+
7186+/* ---------------------------------------------------------------------- */
7187+
7f207e10 7188+/* dcsub.c */
1facf9fc 7189+int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
7190+void au_dpages_free(struct au_dcsub_pages *dpages);
7191+typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
7192+int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7193+ au_dpages_test test, void *arg);
7194+int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7195+ int do_include, au_dpages_test test, void *arg);
027c5e7a
AM
7196+int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7197+ struct dentry *dentry, int do_include);
4a4d8108 7198+int au_test_subdir(struct dentry *d1, struct dentry *d2);
1facf9fc 7199+
7f207e10
AM
7200+/* ---------------------------------------------------------------------- */
7201+
523b37e3
AM
7202+/*
7203+ * todo: in linux-3.13, several similar (but faster) helpers are added to
7204+ * include/linux/dcache.h. Try them (in the future).
7205+ */
7206+
027c5e7a
AM
7207+static inline int au_d_hashed_positive(struct dentry *d)
7208+{
7209+ int err;
5527c038 7210+ struct inode *inode = d_inode(d);
076b876e 7211+
027c5e7a 7212+ err = 0;
5527c038
JR
7213+ if (unlikely(d_unhashed(d)
7214+ || d_is_negative(d)
7215+ || !inode->i_nlink))
027c5e7a
AM
7216+ err = -ENOENT;
7217+ return err;
7218+}
7219+
38d290e6
JR
7220+static inline int au_d_linkable(struct dentry *d)
7221+{
7222+ int err;
5527c038 7223+ struct inode *inode = d_inode(d);
076b876e 7224+
38d290e6
JR
7225+ err = au_d_hashed_positive(d);
7226+ if (err
5527c038 7227+ && d_is_positive(d)
38d290e6
JR
7228+ && (inode->i_state & I_LINKABLE))
7229+ err = 0;
7230+ return err;
7231+}
7232+
027c5e7a
AM
7233+static inline int au_d_alive(struct dentry *d)
7234+{
7235+ int err;
7236+ struct inode *inode;
076b876e 7237+
027c5e7a
AM
7238+ err = 0;
7239+ if (!IS_ROOT(d))
7240+ err = au_d_hashed_positive(d);
7241+ else {
5527c038
JR
7242+ inode = d_inode(d);
7243+ if (unlikely(d_unlinked(d)
7244+ || d_is_negative(d)
7245+ || !inode->i_nlink))
027c5e7a
AM
7246+ err = -ENOENT;
7247+ }
7248+ return err;
7249+}
7250+
7251+static inline int au_alive_dir(struct dentry *d)
7f207e10 7252+{
027c5e7a 7253+ int err;
076b876e 7254+
027c5e7a 7255+ err = au_d_alive(d);
5527c038 7256+ if (unlikely(err || IS_DEADDIR(d_inode(d))))
027c5e7a
AM
7257+ err = -ENOENT;
7258+ return err;
7f207e10
AM
7259+}
7260+
38d290e6
JR
7261+static inline int au_qstreq(struct qstr *a, struct qstr *b)
7262+{
7263+ return a->len == b->len
7264+ && !memcmp(a->name, b->name, a->len);
7265+}
7266+
7e9cd9fe
AM
7267+/*
7268+ * by the commit
7269+ * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
7270+ * taking d_lock
7271+ * the type of d_lockref.count became int, but the inlined function d_count()
7272+ * still returns unsigned int.
7273+ * I don't know why. Maybe it is for every d_count() users?
7274+ * Anyway au_dcount() lives on.
7275+ */
c1595e42
JR
7276+static inline int au_dcount(struct dentry *d)
7277+{
7278+ return (int)d_count(d);
7279+}
7280+
1facf9fc 7281+#endif /* __KERNEL__ */
7282+#endif /* __AUFS_DCSUB_H__ */
7f207e10 7283diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
eca34b5c 7284--- /usr/share/empty/fs/aufs/debug.c 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 7285+++ linux/fs/aufs/debug.c 2022-11-05 23:02:18.962555950 +0100
758e9dad 7286@@ -0,0 +1,444 @@
cd7a4cd9 7287+// SPDX-License-Identifier: GPL-2.0
1facf9fc 7288+/*
0dcfbb52 7289+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 7290+ *
0dcfbb52 7291+ * This program is free software; you can redistribute it and/or modify
1facf9fc 7292+ * it under the terms of the GNU General Public License as published by
7293+ * the Free Software Foundation; either version 2 of the License, or
7294+ * (at your option) any later version.
dece6358
AM
7295+ *
7296+ * This program is distributed in the hope that it will be useful,
7297+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7298+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7299+ * GNU General Public License for more details.
7300+ *
7301+ * You should have received a copy of the GNU General Public License
523b37e3 7302+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7303+ */
7304+
7305+/*
7306+ * debug print functions
7307+ */
7308+
eca801bf 7309+#include <linux/iversion.h>
1facf9fc 7310+#include "aufs.h"
7311+
392086de
AM
7312+/* Returns 0, or -errno. arg is in kp->arg. */
7313+static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
7314+{
7315+ int err, n;
7316+
7317+ err = kstrtoint(val, 0, &n);
7318+ if (!err) {
7319+ if (n > 0)
7320+ au_debug_on();
7321+ else
7322+ au_debug_off();
7323+ }
7324+ return err;
7325+}
7326+
7327+/* Returns length written or -errno. Buffer is 4k (ie. be short!) */
7328+static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
7329+{
7330+ atomic_t *a;
7331+
7332+ a = kp->arg;
7333+ return sprintf(buffer, "%d", atomic_read(a));
7334+}
7335+
22319442 7336+static const struct kernel_param_ops param_ops_atomic_t = {
392086de
AM
7337+ .set = param_atomic_t_set,
7338+ .get = param_atomic_t_get
7339+ /* void (*free)(void *arg) */
7340+};
7341+
7342+atomic_t aufs_debug = ATOMIC_INIT(0);
1facf9fc 7343+MODULE_PARM_DESC(debug, "debug print");
cd7a4cd9 7344+module_param_named(debug, aufs_debug, atomic_t, 0664);
1facf9fc 7345+
c1595e42 7346+DEFINE_MUTEX(au_dbg_mtx); /* just to serialize the dbg msgs */
1facf9fc 7347+char *au_plevel = KERN_DEBUG;
e49829fe
JR
7348+#define dpri(fmt, ...) do { \
7349+ if ((au_plevel \
7350+ && strcmp(au_plevel, KERN_DEBUG)) \
7351+ || au_debug_test()) \
7352+ printk("%s" fmt, au_plevel, ##__VA_ARGS__); \
1facf9fc 7353+} while (0)
7354+
7355+/* ---------------------------------------------------------------------- */
7356+
7357+void au_dpri_whlist(struct au_nhash *whlist)
7358+{
7359+ unsigned long ul, n;
7360+ struct hlist_head *head;
c06a8ce3 7361+ struct au_vdir_wh *pos;
1facf9fc 7362+
7363+ n = whlist->nh_num;
7364+ head = whlist->nh_head;
7365+ for (ul = 0; ul < n; ul++) {
c06a8ce3 7366+ hlist_for_each_entry(pos, head, wh_hash)
1facf9fc 7367+ dpri("b%d, %.*s, %d\n",
c06a8ce3
AM
7368+ pos->wh_bindex,
7369+ pos->wh_str.len, pos->wh_str.name,
7370+ pos->wh_str.len);
1facf9fc 7371+ head++;
7372+ }
7373+}
7374+
7375+void au_dpri_vdir(struct au_vdir *vdir)
7376+{
7377+ unsigned long ul;
7378+ union au_vdir_deblk_p p;
7379+ unsigned char *o;
7380+
7381+ if (!vdir || IS_ERR(vdir)) {
7382+ dpri("err %ld\n", PTR_ERR(vdir));
7383+ return;
7384+ }
7385+
be118d29 7386+ dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %llu\n",
1facf9fc 7387+ vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
7388+ vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
7389+ for (ul = 0; ul < vdir->vd_nblk; ul++) {
7390+ p.deblk = vdir->vd_deblk[ul];
7391+ o = p.deblk;
7392+ dpri("[%lu]: %p\n", ul, o);
7393+ }
7394+}
7395+
53392da6 7396+static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
1facf9fc 7397+ struct dentry *wh)
7398+{
7399+ char *n = NULL;
7400+ int l = 0;
7401+
7402+ if (!inode || IS_ERR(inode)) {
7403+ dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7404+ return -1;
7405+ }
7406+
c2b27bf2 7407+ /* the type of i_blocks depends upon CONFIG_LBDAF */
1facf9fc 7408+ BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7409+ && sizeof(inode->i_blocks) != sizeof(u64));
7410+ if (wh) {
7411+ n = (void *)wh->d_name.name;
7412+ l = wh->d_name.len;
7413+ }
7414+
53392da6
AM
7415+ dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
7416+ " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7417+ bindex, inode,
1facf9fc 7418+ inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7419+ atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7420+ i_size_read(inode), (unsigned long long)inode->i_blocks,
cd7a4cd9 7421+ hn, (long long)timespec64_to_ns(&inode->i_ctime) & 0x0ffff,
1facf9fc 7422+ inode->i_mapping ? inode->i_mapping->nrpages : 0,
be118d29 7423+ inode->i_state, inode->i_flags, inode_peek_iversion(inode),
b752ccd1 7424+ inode->i_generation,
1facf9fc 7425+ l ? ", wh " : "", l, n);
7426+ return 0;
7427+}
7428+
7429+void au_dpri_inode(struct inode *inode)
7430+{
7431+ struct au_iinfo *iinfo;
5afbbe0d 7432+ struct au_hinode *hi;
1facf9fc 7433+ aufs_bindex_t bindex;
53392da6 7434+ int err, hn;
1facf9fc 7435+
53392da6 7436+ err = do_pri_inode(-1, inode, -1, NULL);
5afbbe0d 7437+ if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
1facf9fc 7438+ return;
7439+
7440+ iinfo = au_ii(inode);
5afbbe0d
AM
7441+ dpri("i-1: btop %d, bbot %d, gen %d\n",
7442+ iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
7443+ if (iinfo->ii_btop < 0)
1facf9fc 7444+ return;
53392da6 7445+ hn = 0;
5afbbe0d
AM
7446+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
7447+ hi = au_hinode(iinfo, bindex);
7448+ hn = !!au_hn(hi);
7449+ do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
53392da6 7450+ }
1facf9fc 7451+}
7452+
2cbb1c4b
JR
7453+void au_dpri_dalias(struct inode *inode)
7454+{
7455+ struct dentry *d;
7456+
7457+ spin_lock(&inode->i_lock);
c1595e42 7458+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
2cbb1c4b
JR
7459+ au_dpri_dentry(d);
7460+ spin_unlock(&inode->i_lock);
7461+}
7462+
1facf9fc 7463+static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7464+{
7465+ struct dentry *wh = NULL;
53392da6 7466+ int hn;
5afbbe0d 7467+ struct inode *inode;
076b876e 7468+ struct au_iinfo *iinfo;
5afbbe0d 7469+ struct au_hinode *hi;
1facf9fc 7470+
7471+ if (!dentry || IS_ERR(dentry)) {
7472+ dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7473+ return -1;
7474+ }
7475+ /* do not call dget_parent() here */
027c5e7a 7476+ /* note: access d_xxx without d_lock */
523b37e3
AM
7477+ dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7478+ bindex, dentry, dentry,
1facf9fc 7479+ dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
c1595e42 7480+ au_dcount(dentry), dentry->d_flags,
523b37e3 7481+ d_unhashed(dentry) ? "un" : "");
53392da6 7482+ hn = -1;
5afbbe0d
AM
7483+ inode = NULL;
7484+ if (d_is_positive(dentry))
7485+ inode = d_inode(dentry);
7486+ if (inode
7487+ && au_test_aufs(dentry->d_sb)
7488+ && bindex >= 0
7489+ && !au_is_bad_inode(inode)) {
7490+ iinfo = au_ii(inode);
7491+ hi = au_hinode(iinfo, bindex);
7492+ hn = !!au_hn(hi);
7493+ wh = hi->hi_whdentry;
7494+ }
7495+ do_pri_inode(bindex, inode, hn, wh);
1facf9fc 7496+ return 0;
7497+}
7498+
7499+void au_dpri_dentry(struct dentry *dentry)
7500+{
7501+ struct au_dinfo *dinfo;
7502+ aufs_bindex_t bindex;
7503+ int err;
7504+
7505+ err = do_pri_dentry(-1, dentry);
7506+ if (err || !au_test_aufs(dentry->d_sb))
7507+ return;
7508+
7509+ dinfo = au_di(dentry);
7510+ if (!dinfo)
7511+ return;
5afbbe0d
AM
7512+ dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7513+ dinfo->di_btop, dinfo->di_bbot,
38d290e6
JR
7514+ dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7515+ dinfo->di_tmpfile);
5afbbe0d 7516+ if (dinfo->di_btop < 0)
1facf9fc 7517+ return;
5afbbe0d
AM
7518+ for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
7519+ do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
1facf9fc 7520+}
7521+
7522+static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7523+{
7524+ char a[32];
7525+
7526+ if (!file || IS_ERR(file)) {
7527+ dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7528+ return -1;
7529+ }
7530+ a[0] = 0;
7531+ if (bindex < 0
b912730e 7532+ && !IS_ERR_OR_NULL(file->f_path.dentry)
2000de60 7533+ && au_test_aufs(file->f_path.dentry->d_sb)
1facf9fc 7534+ && au_fi(file))
e49829fe 7535+ snprintf(a, sizeof(a), ", gen %d, mmapped %d",
2cbb1c4b 7536+ au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
b752ccd1 7537+ dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
1facf9fc 7538+ bindex, file->f_mode, file->f_flags, (long)file_count(file),
b752ccd1 7539+ file->f_version, file->f_pos, a);
b912730e 7540+ if (!IS_ERR_OR_NULL(file->f_path.dentry))
2000de60 7541+ do_pri_dentry(bindex, file->f_path.dentry);
1facf9fc 7542+ return 0;
7543+}
7544+
7545+void au_dpri_file(struct file *file)
7546+{
7547+ struct au_finfo *finfo;
4a4d8108
AM
7548+ struct au_fidir *fidir;
7549+ struct au_hfile *hfile;
1facf9fc 7550+ aufs_bindex_t bindex;
7551+ int err;
7552+
7553+ err = do_pri_file(-1, file);
2000de60 7554+ if (err
b912730e 7555+ || IS_ERR_OR_NULL(file->f_path.dentry)
2000de60 7556+ || !au_test_aufs(file->f_path.dentry->d_sb))
1facf9fc 7557+ return;
7558+
7559+ finfo = au_fi(file);
7560+ if (!finfo)
7561+ return;
4a4d8108 7562+ if (finfo->fi_btop < 0)
1facf9fc 7563+ return;
4a4d8108
AM
7564+ fidir = finfo->fi_hdir;
7565+ if (!fidir)
7566+ do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7567+ else
e49829fe
JR
7568+ for (bindex = finfo->fi_btop;
7569+ bindex >= 0 && bindex <= fidir->fd_bbot;
4a4d8108
AM
7570+ bindex++) {
7571+ hfile = fidir->fd_hfile + bindex;
7572+ do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7573+ }
1facf9fc 7574+}
7575+
7576+static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7577+{
7578+ struct vfsmount *mnt;
7579+ struct super_block *sb;
7580+
7581+ if (!br || IS_ERR(br))
7582+ goto out;
86dc4139 7583+ mnt = au_br_mnt(br);
1facf9fc 7584+ if (!mnt || IS_ERR(mnt))
7585+ goto out;
7586+ sb = mnt->mnt_sb;
7587+ if (!sb || IS_ERR(sb))
7588+ goto out;
7589+
acd2b654 7590+ dpri("s%d: {perm 0x%x, id %d, wbr %p}, "
b752ccd1 7591+ "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
1facf9fc 7592+ "xino %d\n",
acd2b654
AM
7593+ bindex, br->br_perm, br->br_id, br->br_wbr,
7594+ au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
b752ccd1 7595+ sb->s_flags, sb->s_count,
acd2b654
AM
7596+ atomic_read(&sb->s_active),
7597+ !!au_xino_file(br->br_xino, /*idx*/-1));
1facf9fc 7598+ return 0;
7599+
4f0767ce 7600+out:
1facf9fc 7601+ dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7602+ return -1;
7603+}
7604+
7605+void au_dpri_sb(struct super_block *sb)
7606+{
7607+ struct au_sbinfo *sbinfo;
7608+ aufs_bindex_t bindex;
7609+ int err;
acd2b654 7610+ /* to reduce stack size */
1facf9fc 7611+ struct {
7612+ struct vfsmount mnt;
7613+ struct au_branch fake;
7614+ } *a;
7615+
7616+ /* this function can be called from magic sysrq */
7617+ a = kzalloc(sizeof(*a), GFP_ATOMIC);
7618+ if (unlikely(!a)) {
7619+ dpri("no memory\n");
7620+ return;
7621+ }
7622+
7623+ a->mnt.mnt_sb = sb;
86dc4139 7624+ a->fake.br_path.mnt = &a->mnt;
1facf9fc 7625+ err = do_pri_br(-1, &a->fake);
9f237c51 7626+ au_kfree_rcu(a);
1facf9fc 7627+ dpri("dev 0x%x\n", sb->s_dev);
7628+ if (err || !au_test_aufs(sb))
7629+ return;
7630+
7631+ sbinfo = au_sbi(sb);
7632+ if (!sbinfo)
7633+ return;
f0c0a007
AM
7634+ dpri("nw %d, gen %u, kobj %d\n",
7635+ atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
521ced18 7636+ kref_read(&sbinfo->si_kobj.kref));
5afbbe0d 7637+ for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
1facf9fc 7638+ do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7639+}
7640+
7641+/* ---------------------------------------------------------------------- */
7642+
027c5e7a
AM
7643+void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7644+{
5527c038 7645+ struct inode *h_inode, *inode = d_inode(dentry);
027c5e7a 7646+ struct dentry *h_dentry;
5afbbe0d 7647+ aufs_bindex_t bindex, bbot, bi;
027c5e7a
AM
7648+
7649+ if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7650+ return;
7651+
5afbbe0d
AM
7652+ bbot = au_dbbot(dentry);
7653+ bi = au_ibbot(inode);
7654+ if (bi < bbot)
7655+ bbot = bi;
7656+ bindex = au_dbtop(dentry);
7657+ bi = au_ibtop(inode);
027c5e7a
AM
7658+ if (bi > bindex)
7659+ bindex = bi;
7660+
5afbbe0d 7661+ for (; bindex <= bbot; bindex++) {
027c5e7a
AM
7662+ h_dentry = au_h_dptr(dentry, bindex);
7663+ if (!h_dentry)
7664+ continue;
7665+ h_inode = au_h_iptr(inode, bindex);
5527c038 7666+ if (unlikely(h_inode != d_inode(h_dentry))) {
392086de 7667+ au_debug_on();
027c5e7a
AM
7668+ AuDbg("b%d, %s:%d\n", bindex, func, line);
7669+ AuDbgDentry(dentry);
7670+ AuDbgInode(inode);
392086de 7671+ au_debug_off();
758e9dad
AM
7672+ if (au_test_fuse(h_inode->i_sb))
7673+ WARN_ON_ONCE(1);
7674+ else
7675+ BUG();
027c5e7a
AM
7676+ }
7677+ }
7678+}
7679+
1facf9fc 7680+void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7681+{
7682+ int err, i, j;
7683+ struct au_dcsub_pages dpages;
7684+ struct au_dpage *dpage;
7685+ struct dentry **dentries;
7686+
7687+ err = au_dpages_init(&dpages, GFP_NOFS);
7688+ AuDebugOn(err);
027c5e7a 7689+ err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
1facf9fc 7690+ AuDebugOn(err);
7691+ for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7692+ dpage = dpages.dpages + i;
7693+ dentries = dpage->dentries;
7694+ for (j = dpage->ndentry - 1; !err && j >= 0; j--)
027c5e7a 7695+ AuDebugOn(au_digen_test(dentries[j], sigen));
1facf9fc 7696+ }
7697+ au_dpages_free(&dpages);
7698+}
7699+
1facf9fc 7700+void au_dbg_verify_kthread(void)
7701+{
53392da6 7702+ if (au_wkq_test()) {
1facf9fc 7703+ au_dbg_blocked();
1e00d052
AM
7704+ /*
7705+ * It may be recursive, but udba=notify between two aufs mounts,
7706+ * where a single ro branch is shared, is not a problem.
7707+ */
7708+ /* WARN_ON(1); */
1facf9fc 7709+ }
7710+}
7711+
7712+/* ---------------------------------------------------------------------- */
7713+
1facf9fc 7714+int __init au_debug_init(void)
7715+{
7716+ aufs_bindex_t bindex;
7717+ struct au_vdir_destr destr;
7718+
7719+ bindex = -1;
7720+ AuDebugOn(bindex >= 0);
7721+
7722+ destr.len = -1;
7723+ AuDebugOn(destr.len < NAME_MAX);
7724+
7725+#ifdef CONFIG_4KSTACKS
0c3ec466 7726+ pr_warn("CONFIG_4KSTACKS is defined.\n");
1facf9fc 7727+#endif
7728+
1facf9fc 7729+ return 0;
7730+}
7f207e10 7731diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
eca34b5c 7732--- /usr/share/empty/fs/aufs/debug.h 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 7733+++ linux/fs/aufs/debug.h 2022-11-05 23:02:18.962555950 +0100
062440b3
AM
7734@@ -0,0 +1,226 @@
7735+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 7736+/*
0dcfbb52 7737+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 7738+ *
0dcfbb52 7739+ * This program is free software; you can redistribute it and/or modify
1facf9fc 7740+ * it under the terms of the GNU General Public License as published by
7741+ * the Free Software Foundation; either version 2 of the License, or
7742+ * (at your option) any later version.
dece6358
AM
7743+ *
7744+ * This program is distributed in the hope that it will be useful,
7745+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7746+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7747+ * GNU General Public License for more details.
7748+ *
7749+ * You should have received a copy of the GNU General Public License
523b37e3 7750+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7751+ */
7752+
7753+/*
7754+ * debug print functions
7755+ */
7756+
7757+#ifndef __AUFS_DEBUG_H__
7758+#define __AUFS_DEBUG_H__
7759+
7760+#ifdef __KERNEL__
7761+
392086de 7762+#include <linux/atomic.h>
4a4d8108
AM
7763+#include <linux/module.h>
7764+#include <linux/kallsyms.h>
1facf9fc 7765+#include <linux/sysrq.h>
4a4d8108 7766+
1facf9fc 7767+#ifdef CONFIG_AUFS_DEBUG
7768+#define AuDebugOn(a) BUG_ON(a)
7769+
7770+/* module parameter */
392086de
AM
7771+extern atomic_t aufs_debug;
7772+static inline void au_debug_on(void)
1facf9fc 7773+{
392086de
AM
7774+ atomic_inc(&aufs_debug);
7775+}
7776+static inline void au_debug_off(void)
7777+{
7778+ atomic_dec_if_positive(&aufs_debug);
1facf9fc 7779+}
7780+
7781+static inline int au_debug_test(void)
7782+{
392086de 7783+ return atomic_read(&aufs_debug) > 0;
1facf9fc 7784+}
7785+#else
7786+#define AuDebugOn(a) do {} while (0)
392086de
AM
7787+AuStubVoid(au_debug_on, void)
7788+AuStubVoid(au_debug_off, void)
4a4d8108 7789+AuStubInt0(au_debug_test, void)
1facf9fc 7790+#endif /* CONFIG_AUFS_DEBUG */
7791+
392086de
AM
7792+#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7793+
1facf9fc 7794+/* ---------------------------------------------------------------------- */
7795+
7796+/* debug print */
7797+
4a4d8108 7798+#define AuDbg(fmt, ...) do { \
1facf9fc 7799+ if (au_debug_test()) \
4a4d8108 7800+ pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
1facf9fc 7801+} while (0)
4a4d8108
AM
7802+#define AuLabel(l) AuDbg(#l "\n")
7803+#define AuIOErr(fmt, ...) pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7804+#define AuWarn1(fmt, ...) do { \
1facf9fc 7805+ static unsigned char _c; \
7806+ if (!_c++) \
0c3ec466 7807+ pr_warn(fmt, ##__VA_ARGS__); \
1facf9fc 7808+} while (0)
7809+
4a4d8108 7810+#define AuErr1(fmt, ...) do { \
1facf9fc 7811+ static unsigned char _c; \
7812+ if (!_c++) \
4a4d8108 7813+ pr_err(fmt, ##__VA_ARGS__); \
1facf9fc 7814+} while (0)
7815+
4a4d8108 7816+#define AuIOErr1(fmt, ...) do { \
1facf9fc 7817+ static unsigned char _c; \
7818+ if (!_c++) \
4a4d8108 7819+ AuIOErr(fmt, ##__VA_ARGS__); \
1facf9fc 7820+} while (0)
7821+
7822+#define AuUnsupportMsg "This operation is not supported." \
7823+ " Please report this application to aufs-users ML."
4a4d8108
AM
7824+#define AuUnsupport(fmt, ...) do { \
7825+ pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
1facf9fc 7826+ dump_stack(); \
7827+} while (0)
7828+
7829+#define AuTraceErr(e) do { \
7830+ if (unlikely((e) < 0)) \
7831+ AuDbg("err %d\n", (int)(e)); \
7832+} while (0)
7833+
7834+#define AuTraceErrPtr(p) do { \
7835+ if (IS_ERR(p)) \
7836+ AuDbg("err %ld\n", PTR_ERR(p)); \
7837+} while (0)
7838+
7839+/* dirty macros for debug print, use with "%.*s" and caution */
7840+#define AuLNPair(qstr) (qstr)->len, (qstr)->name
1facf9fc 7841+
7842+/* ---------------------------------------------------------------------- */
7843+
dece6358 7844+struct dentry;
1facf9fc 7845+#ifdef CONFIG_AUFS_DEBUG
c1595e42 7846+extern struct mutex au_dbg_mtx;
1facf9fc 7847+extern char *au_plevel;
7848+struct au_nhash;
7849+void au_dpri_whlist(struct au_nhash *whlist);
7850+struct au_vdir;
7851+void au_dpri_vdir(struct au_vdir *vdir);
dece6358 7852+struct inode;
1facf9fc 7853+void au_dpri_inode(struct inode *inode);
2cbb1c4b 7854+void au_dpri_dalias(struct inode *inode);
1facf9fc 7855+void au_dpri_dentry(struct dentry *dentry);
dece6358 7856+struct file;
1facf9fc 7857+void au_dpri_file(struct file *filp);
dece6358 7858+struct super_block;
1facf9fc 7859+void au_dpri_sb(struct super_block *sb);
7860+
027c5e7a
AM
7861+#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
7862+void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
1facf9fc 7863+void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
1facf9fc 7864+void au_dbg_verify_kthread(void);
7865+
7866+int __init au_debug_init(void);
7e9cd9fe 7867+
1facf9fc 7868+#define AuDbgWhlist(w) do { \
c1595e42 7869+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7870+ AuDbg(#w "\n"); \
7871+ au_dpri_whlist(w); \
c1595e42 7872+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7873+} while (0)
7874+
7875+#define AuDbgVdir(v) do { \
c1595e42 7876+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7877+ AuDbg(#v "\n"); \
7878+ au_dpri_vdir(v); \
c1595e42 7879+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7880+} while (0)
7881+
7882+#define AuDbgInode(i) do { \
c1595e42 7883+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7884+ AuDbg(#i "\n"); \
7885+ au_dpri_inode(i); \
c1595e42 7886+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7887+} while (0)
7888+
2cbb1c4b 7889+#define AuDbgDAlias(i) do { \
c1595e42 7890+ mutex_lock(&au_dbg_mtx); \
2cbb1c4b
JR
7891+ AuDbg(#i "\n"); \
7892+ au_dpri_dalias(i); \
c1595e42 7893+ mutex_unlock(&au_dbg_mtx); \
2cbb1c4b
JR
7894+} while (0)
7895+
1facf9fc 7896+#define AuDbgDentry(d) do { \
c1595e42 7897+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7898+ AuDbg(#d "\n"); \
7899+ au_dpri_dentry(d); \
c1595e42 7900+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7901+} while (0)
7902+
7903+#define AuDbgFile(f) do { \
c1595e42 7904+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7905+ AuDbg(#f "\n"); \
7906+ au_dpri_file(f); \
c1595e42 7907+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7908+} while (0)
7909+
7910+#define AuDbgSb(sb) do { \
c1595e42 7911+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7912+ AuDbg(#sb "\n"); \
7913+ au_dpri_sb(sb); \
c1595e42 7914+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7915+} while (0)
7916+
4a4d8108
AM
7917+#define AuDbgSym(addr) do { \
7918+ char sym[KSYM_SYMBOL_LEN]; \
7919+ sprint_symbol(sym, (unsigned long)addr); \
7920+ AuDbg("%s\n", sym); \
7921+} while (0)
1facf9fc 7922+#else
027c5e7a 7923+AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
4a4d8108
AM
7924+AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
7925+AuStubVoid(au_dbg_verify_kthread, void)
7926+AuStubInt0(__init au_debug_init, void)
1facf9fc 7927+
1facf9fc 7928+#define AuDbgWhlist(w) do {} while (0)
7929+#define AuDbgVdir(v) do {} while (0)
7930+#define AuDbgInode(i) do {} while (0)
2cbb1c4b 7931+#define AuDbgDAlias(i) do {} while (0)
1facf9fc 7932+#define AuDbgDentry(d) do {} while (0)
7933+#define AuDbgFile(f) do {} while (0)
7934+#define AuDbgSb(sb) do {} while (0)
4a4d8108 7935+#define AuDbgSym(addr) do {} while (0)
1facf9fc 7936+#endif /* CONFIG_AUFS_DEBUG */
7937+
7938+/* ---------------------------------------------------------------------- */
7939+
7940+#ifdef CONFIG_AUFS_MAGIC_SYSRQ
7941+int __init au_sysrq_init(void);
7942+void au_sysrq_fin(void);
7943+
7944+#ifdef CONFIG_HW_CONSOLE
7945+#define au_dbg_blocked() do { \
7946+ WARN_ON(1); \
0c5527e5 7947+ handle_sysrq('w'); \
1facf9fc 7948+} while (0)
7949+#else
4a4d8108 7950+AuStubVoid(au_dbg_blocked, void)
1facf9fc 7951+#endif
7952+
7953+#else
4a4d8108
AM
7954+AuStubInt0(__init au_sysrq_init, void)
7955+AuStubVoid(au_sysrq_fin, void)
7956+AuStubVoid(au_dbg_blocked, void)
1facf9fc 7957+#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
7958+
7959+#endif /* __KERNEL__ */
7960+#endif /* __AUFS_DEBUG_H__ */
7f207e10 7961diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
eca34b5c 7962--- /usr/share/empty/fs/aufs/dentry.c 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 7963+++ linux/fs/aufs/dentry.c 2022-11-05 23:02:18.962555950 +0100
f4d37d76 7964@@ -0,0 +1,1168 @@
cd7a4cd9 7965+// SPDX-License-Identifier: GPL-2.0
1facf9fc 7966+/*
0dcfbb52 7967+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 7968+ *
0dcfbb52 7969+ * This program is free software; you can redistribute it and/or modify
1facf9fc 7970+ * it under the terms of the GNU General Public License as published by
7971+ * the Free Software Foundation; either version 2 of the License, or
7972+ * (at your option) any later version.
dece6358
AM
7973+ *
7974+ * This program is distributed in the hope that it will be useful,
7975+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7976+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7977+ * GNU General Public License for more details.
7978+ *
7979+ * You should have received a copy of the GNU General Public License
523b37e3 7980+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7981+ */
7982+
7983+/*
7984+ * lookup and dentry operations
7985+ */
7986+
eca801bf 7987+#include <linux/iversion.h>
1facf9fc 7988+#include "aufs.h"
7989+
1facf9fc 7990+/*
7991+ * returns positive/negative dentry, NULL or an error.
7992+ * NULL means whiteout-ed or not-found.
7993+ */
7994+static struct dentry*
7995+au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
8b6a4947 7996+ aufs_bindex_t bindex, struct au_do_lookup_args *args)
1facf9fc 7997+{
7998+ struct dentry *h_dentry;
2000de60 7999+ struct inode *h_inode;
1facf9fc 8000+ struct au_branch *br;
0b2a12c6 8001+ struct user_namespace *h_userns;
758e9dad 8002+ struct path h_path;
1facf9fc 8003+ int wh_found, opq;
8004+ unsigned char wh_able;
8005+ const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
076b876e
AM
8006+ const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
8007+ IGNORE_PERM);
1facf9fc 8008+
1facf9fc 8009+ wh_found = 0;
8010+ br = au_sbr(dentry->d_sb, bindex);
758e9dad
AM
8011+ h_path.dentry = h_parent;
8012+ h_path.mnt = au_br_mnt(br);
0b2a12c6 8013+ h_userns = au_br_userns(br);
1facf9fc 8014+ wh_able = !!au_br_whable(br->br_perm);
8015+ if (wh_able)
758e9dad 8016+ wh_found = au_wh_test(h_userns, &h_path, &args->whname,
0b2a12c6 8017+ ignore_perm);
1facf9fc 8018+ h_dentry = ERR_PTR(wh_found);
8019+ if (!wh_found)
8020+ goto real_lookup;
8021+ if (unlikely(wh_found < 0))
8022+ goto out;
8023+
8024+ /* We found a whiteout */
5afbbe0d 8025+ /* au_set_dbbot(dentry, bindex); */
1facf9fc 8026+ au_set_dbwh(dentry, bindex);
8027+ if (!allow_neg)
8028+ return NULL; /* success */
8029+
4f0767ce 8030+real_lookup:
076b876e 8031+ if (!ignore_perm)
758e9dad 8032+ h_dentry = vfsub_lkup_one(args->name, &h_path);
076b876e 8033+ else
758e9dad 8034+ h_dentry = au_sio_lkup_one(h_userns, args->name, &h_path);
2000de60
JR
8035+ if (IS_ERR(h_dentry)) {
8036+ if (PTR_ERR(h_dentry) == -ENAMETOOLONG
8037+ && !allow_neg)
8038+ h_dentry = NULL;
1facf9fc 8039+ goto out;
2000de60 8040+ }
1facf9fc 8041+
5527c038
JR
8042+ h_inode = d_inode(h_dentry);
8043+ if (d_is_negative(h_dentry)) {
1facf9fc 8044+ if (!allow_neg)
8045+ goto out_neg;
8046+ } else if (wh_found
8047+ || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
8048+ goto out_neg;
8b6a4947
AM
8049+ else if (au_ftest_lkup(args->flags, DIRREN)
8050+ /* && h_inode */
8051+ && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
8052+ AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
8053+ (unsigned long long)h_inode->i_ino);
8054+ goto out_neg;
8055+ }
1facf9fc 8056+
5afbbe0d
AM
8057+ if (au_dbbot(dentry) <= bindex)
8058+ au_set_dbbot(dentry, bindex);
8059+ if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
8060+ au_set_dbtop(dentry, bindex);
1facf9fc 8061+ au_set_h_dptr(dentry, bindex, h_dentry);
8062+
2000de60
JR
8063+ if (!d_is_dir(h_dentry)
8064+ || !wh_able
5527c038 8065+ || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
1facf9fc 8066+ goto out; /* success */
8067+
758e9dad 8068+ h_path.dentry = h_dentry;
be118d29 8069+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
758e9dad 8070+ opq = au_diropq_test(h_userns, &h_path);
3c1bdaff 8071+ inode_unlock_shared(h_inode);
1facf9fc 8072+ if (opq > 0)
8073+ au_set_dbdiropq(dentry, bindex);
8074+ else if (unlikely(opq < 0)) {
8075+ au_set_h_dptr(dentry, bindex, NULL);
8076+ h_dentry = ERR_PTR(opq);
8077+ }
8078+ goto out;
8079+
4f0767ce 8080+out_neg:
1facf9fc 8081+ dput(h_dentry);
8082+ h_dentry = NULL;
4f0767ce 8083+out:
1facf9fc 8084+ return h_dentry;
8085+}
8086+
dece6358
AM
8087+static int au_test_shwh(struct super_block *sb, const struct qstr *name)
8088+{
8089+ if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
8090+ && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
8091+ return -EPERM;
8092+ return 0;
8093+}
8094+
1facf9fc 8095+/*
8096+ * returns the number of lower positive dentries,
8097+ * otherwise an error.
8098+ * can be called at unlinking with @type is zero.
8099+ */
5afbbe0d
AM
8100+int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
8101+ unsigned int flags)
1facf9fc 8102+{
8103+ int npositive, err;
8104+ aufs_bindex_t bindex, btail, bdiropq;
8b6a4947 8105+ unsigned char isdir, dirperm1, dirren;
1facf9fc 8106+ struct au_do_lookup_args args = {
8b6a4947
AM
8107+ .flags = flags,
8108+ .name = &dentry->d_name
1facf9fc 8109+ };
1facf9fc 8110+ struct dentry *parent;
076b876e 8111+ struct super_block *sb;
1facf9fc 8112+
076b876e 8113+ sb = dentry->d_sb;
8b6a4947 8114+ err = au_test_shwh(sb, args.name);
dece6358 8115+ if (unlikely(err))
1facf9fc 8116+ goto out;
8117+
8b6a4947 8118+ err = au_wh_name_alloc(&args.whname, args.name);
1facf9fc 8119+ if (unlikely(err))
8120+ goto out;
8121+
2000de60 8122+ isdir = !!d_is_dir(dentry);
076b876e 8123+ dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
8b6a4947
AM
8124+ dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
8125+ if (dirren)
8126+ au_fset_lkup(args.flags, DIRREN);
1facf9fc 8127+
8128+ npositive = 0;
4a4d8108 8129+ parent = dget_parent(dentry);
1facf9fc 8130+ btail = au_dbtaildir(parent);
5afbbe0d 8131+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 8132+ struct dentry *h_parent, *h_dentry;
8133+ struct inode *h_inode, *h_dir;
8b6a4947 8134+ struct au_branch *br;
1facf9fc 8135+
8136+ h_dentry = au_h_dptr(dentry, bindex);
8137+ if (h_dentry) {
5527c038 8138+ if (d_is_positive(h_dentry))
1facf9fc 8139+ npositive++;
5afbbe0d 8140+ break;
1facf9fc 8141+ }
8142+ h_parent = au_h_dptr(parent, bindex);
2000de60 8143+ if (!h_parent || !d_is_dir(h_parent))
1facf9fc 8144+ continue;
8145+
8b6a4947
AM
8146+ if (dirren) {
8147+ /* if the inum matches, then use the prepared name */
8148+ err = au_dr_lkup_name(&args, bindex);
8149+ if (unlikely(err))
8150+ goto out_parent;
8151+ }
8152+
5527c038 8153+ h_dir = d_inode(h_parent);
be118d29 8154+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8b6a4947 8155+ h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
3c1bdaff 8156+ inode_unlock_shared(h_dir);
1facf9fc 8157+ err = PTR_ERR(h_dentry);
8158+ if (IS_ERR(h_dentry))
4a4d8108 8159+ goto out_parent;
2000de60
JR
8160+ if (h_dentry)
8161+ au_fclr_lkup(args.flags, ALLOW_NEG);
076b876e
AM
8162+ if (dirperm1)
8163+ au_fset_lkup(args.flags, IGNORE_PERM);
1facf9fc 8164+
79b8bda9 8165+ if (au_dbwh(dentry) == bindex)
1facf9fc 8166+ break;
8167+ if (!h_dentry)
8168+ continue;
5527c038 8169+ if (d_is_negative(h_dentry))
1facf9fc 8170+ continue;
5527c038 8171+ h_inode = d_inode(h_dentry);
1facf9fc 8172+ npositive++;
8173+ if (!args.type)
8174+ args.type = h_inode->i_mode & S_IFMT;
8175+ if (args.type != S_IFDIR)
8176+ break;
8177+ else if (isdir) {
8178+ /* the type of lower may be different */
8179+ bdiropq = au_dbdiropq(dentry);
8180+ if (bdiropq >= 0 && bdiropq <= bindex)
8181+ break;
8182+ }
8b6a4947
AM
8183+ br = au_sbr(sb, bindex);
8184+ if (dirren
8185+ && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
8186+ /*add_ent*/NULL)) {
8187+ /* prepare next name to lookup */
8188+ err = au_dr_lkup(&args, dentry, bindex);
8189+ if (unlikely(err))
8190+ goto out_parent;
8191+ }
1facf9fc 8192+ }
8193+
8194+ if (npositive) {
8195+ AuLabel(positive);
5afbbe0d 8196+ au_update_dbtop(dentry);
1facf9fc 8197+ }
8198+ err = npositive;
076b876e 8199+ if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
5afbbe0d 8200+ && au_dbtop(dentry) < 0)) {
1facf9fc 8201+ err = -EIO;
523b37e3
AM
8202+ AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
8203+ dentry, err);
027c5e7a 8204+ }
1facf9fc 8205+
4f0767ce 8206+out_parent:
4a4d8108 8207+ dput(parent);
9f237c51 8208+ au_kfree_try_rcu(args.whname.name);
8b6a4947
AM
8209+ if (dirren)
8210+ au_dr_lkup_fin(&args);
4f0767ce 8211+out:
1facf9fc 8212+ return err;
8213+}
8214+
0b2a12c6 8215+struct dentry *au_sio_lkup_one(struct user_namespace *userns, struct qstr *name,
758e9dad 8216+ struct path *ppath)
1facf9fc 8217+{
8218+ struct dentry *dentry;
8219+ int wkq_err;
8220+
758e9dad
AM
8221+ if (!au_test_h_perm_sio(userns, d_inode(ppath->dentry), MAY_EXEC))
8222+ dentry = vfsub_lkup_one(name, ppath);
1facf9fc 8223+ else {
b4510431
AM
8224+ struct vfsub_lkup_one_args args = {
8225+ .errp = &dentry,
8226+ .name = name,
758e9dad 8227+ .ppath = ppath
1facf9fc 8228+ };
8229+
b4510431 8230+ wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
1facf9fc 8231+ if (unlikely(wkq_err))
8232+ dentry = ERR_PTR(wkq_err);
8233+ }
8234+
8235+ return dentry;
8236+}
8237+
8238+/*
8239+ * lookup @dentry on @bindex which should be negative.
8240+ */
86dc4139 8241+int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
1facf9fc 8242+{
8243+ int err;
758e9dad 8244+ struct dentry *parent, *h_dentry;
86dc4139 8245+ struct au_branch *br;
0b2a12c6 8246+ struct user_namespace *h_userns;
758e9dad 8247+ struct path h_ppath;
1facf9fc 8248+
1facf9fc 8249+ parent = dget_parent(dentry);
86dc4139 8250+ br = au_sbr(dentry->d_sb, bindex);
758e9dad
AM
8251+ h_ppath.dentry = au_h_dptr(parent, bindex);
8252+ h_ppath.mnt = au_br_mnt(br);
0b2a12c6 8253+ h_userns = au_br_userns(br);
86dc4139 8254+ if (wh)
758e9dad 8255+ h_dentry = au_whtmp_lkup(h_ppath.dentry, br, &dentry->d_name);
86dc4139 8256+ else
758e9dad 8257+ h_dentry = au_sio_lkup_one(h_userns, &dentry->d_name, &h_ppath);
1facf9fc 8258+ err = PTR_ERR(h_dentry);
8259+ if (IS_ERR(h_dentry))
8260+ goto out;
5527c038 8261+ if (unlikely(d_is_positive(h_dentry))) {
1facf9fc 8262+ err = -EIO;
523b37e3 8263+ AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
1facf9fc 8264+ dput(h_dentry);
8265+ goto out;
8266+ }
8267+
4a4d8108 8268+ err = 0;
5afbbe0d
AM
8269+ if (bindex < au_dbtop(dentry))
8270+ au_set_dbtop(dentry, bindex);
8271+ if (au_dbbot(dentry) < bindex)
8272+ au_set_dbbot(dentry, bindex);
1facf9fc 8273+ au_set_h_dptr(dentry, bindex, h_dentry);
1facf9fc 8274+
4f0767ce 8275+out:
1facf9fc 8276+ dput(parent);
8277+ return err;
8278+}
8279+
8280+/* ---------------------------------------------------------------------- */
8281+
8282+/* subset of struct inode */
8283+struct au_iattr {
8284+ unsigned long i_ino;
8285+ /* unsigned int i_nlink; */
0c3ec466
AM
8286+ kuid_t i_uid;
8287+ kgid_t i_gid;
1facf9fc 8288+ u64 i_version;
8289+/*
8290+ loff_t i_size;
8291+ blkcnt_t i_blocks;
8292+*/
8293+ umode_t i_mode;
8294+};
8295+
8296+static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
8297+{
8298+ ia->i_ino = h_inode->i_ino;
8299+ /* ia->i_nlink = h_inode->i_nlink; */
8300+ ia->i_uid = h_inode->i_uid;
8301+ ia->i_gid = h_inode->i_gid;
be118d29 8302+ ia->i_version = inode_query_iversion(h_inode);
1facf9fc 8303+/*
8304+ ia->i_size = h_inode->i_size;
8305+ ia->i_blocks = h_inode->i_blocks;
8306+*/
8307+ ia->i_mode = (h_inode->i_mode & S_IFMT);
8308+}
8309+
8310+static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
8311+{
8312+ return ia->i_ino != h_inode->i_ino
8313+ /* || ia->i_nlink != h_inode->i_nlink */
0c3ec466 8314+ || !uid_eq(ia->i_uid, h_inode->i_uid)
2dfbb274 8315+ || !gid_eq(ia->i_gid, h_inode->i_gid)
be118d29 8316+ || !inode_eq_iversion(h_inode, ia->i_version)
1facf9fc 8317+/*
8318+ || ia->i_size != h_inode->i_size
8319+ || ia->i_blocks != h_inode->i_blocks
8320+*/
8321+ || ia->i_mode != (h_inode->i_mode & S_IFMT);
8322+}
8323+
8324+static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
8325+ struct au_branch *br)
8326+{
8327+ int err;
8328+ struct au_iattr ia;
8329+ struct inode *h_inode;
8330+ struct dentry *h_d;
8331+ struct super_block *h_sb;
758e9dad 8332+ struct path h_ppath;
1facf9fc 8333+
8334+ err = 0;
8335+ memset(&ia, -1, sizeof(ia));
8336+ h_sb = h_dentry->d_sb;
5527c038
JR
8337+ h_inode = NULL;
8338+ if (d_is_positive(h_dentry)) {
8339+ h_inode = d_inode(h_dentry);
1facf9fc 8340+ au_iattr_save(&ia, h_inode);
5527c038 8341+ } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
1facf9fc 8342+ /* nfs d_revalidate may return 0 for negative dentry */
8343+ /* fuse d_revalidate always return 0 for negative dentry */
8344+ goto out;
8345+
8346+ /* main purpose is namei.c:cached_lookup() and d_revalidate */
758e9dad
AM
8347+ h_ppath.dentry = h_parent;
8348+ h_ppath.mnt = au_br_mnt(br);
8349+ h_d = vfsub_lkup_one(&h_dentry->d_name, &h_ppath);
1facf9fc 8350+ err = PTR_ERR(h_d);
8351+ if (IS_ERR(h_d))
8352+ goto out;
8353+
8354+ err = 0;
8355+ if (unlikely(h_d != h_dentry
5527c038 8356+ || d_inode(h_d) != h_inode
1facf9fc 8357+ || (h_inode && au_iattr_test(&ia, h_inode))))
8358+ err = au_busy_or_stale();
8359+ dput(h_d);
8360+
4f0767ce 8361+out:
1facf9fc 8362+ AuTraceErr(err);
8363+ return err;
8364+}
8365+
8366+int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8367+ struct dentry *h_parent, struct au_branch *br)
8368+{
8369+ int err;
8370+
8371+ err = 0;
027c5e7a
AM
8372+ if (udba == AuOpt_UDBA_REVAL
8373+ && !au_test_fs_remote(h_dentry->d_sb)) {
1facf9fc 8374+ IMustLock(h_dir);
5527c038 8375+ err = (d_inode(h_dentry->d_parent) != h_dir);
027c5e7a 8376+ } else if (udba != AuOpt_UDBA_NONE)
1facf9fc 8377+ err = au_h_verify_dentry(h_dentry, h_parent, br);
8378+
8379+ return err;
8380+}
8381+
8382+/* ---------------------------------------------------------------------- */
8383+
027c5e7a 8384+static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
1facf9fc 8385+{
027c5e7a 8386+ int err;
5afbbe0d 8387+ aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
027c5e7a
AM
8388+ struct au_hdentry tmp, *p, *q;
8389+ struct au_dinfo *dinfo;
8390+ struct super_block *sb;
1facf9fc 8391+
027c5e7a 8392+ DiMustWriteLock(dentry);
1308ab2a 8393+
027c5e7a
AM
8394+ sb = dentry->d_sb;
8395+ dinfo = au_di(dentry);
5afbbe0d 8396+ bbot = dinfo->di_bbot;
1facf9fc 8397+ bwh = dinfo->di_bwh;
8398+ bdiropq = dinfo->di_bdiropq;
5afbbe0d
AM
8399+ bindex = dinfo->di_btop;
8400+ p = au_hdentry(dinfo, bindex);
8401+ for (; bindex <= bbot; bindex++, p++) {
027c5e7a 8402+ if (!p->hd_dentry)
1facf9fc 8403+ continue;
8404+
027c5e7a
AM
8405+ new_bindex = au_br_index(sb, p->hd_id);
8406+ if (new_bindex == bindex)
1facf9fc 8407+ continue;
1facf9fc 8408+
1facf9fc 8409+ if (dinfo->di_bwh == bindex)
8410+ bwh = new_bindex;
8411+ if (dinfo->di_bdiropq == bindex)
8412+ bdiropq = new_bindex;
8413+ if (new_bindex < 0) {
8414+ au_hdput(p);
8415+ p->hd_dentry = NULL;
8416+ continue;
8417+ }
8418+
8419+ /* swap two lower dentries, and loop again */
5afbbe0d 8420+ q = au_hdentry(dinfo, new_bindex);
1facf9fc 8421+ tmp = *q;
8422+ *q = *p;
8423+ *p = tmp;
8424+ if (tmp.hd_dentry) {
8425+ bindex--;
8426+ p--;
8427+ }
8428+ }
8429+
1facf9fc 8430+ dinfo->di_bwh = -1;
5afbbe0d 8431+ if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
1facf9fc 8432+ dinfo->di_bwh = bwh;
8433+
8434+ dinfo->di_bdiropq = -1;
8435+ if (bdiropq >= 0
5afbbe0d 8436+ && bdiropq <= au_sbbot(sb)
1facf9fc 8437+ && au_sbr_whable(sb, bdiropq))
8438+ dinfo->di_bdiropq = bdiropq;
8439+
027c5e7a 8440+ err = -EIO;
5afbbe0d
AM
8441+ dinfo->di_btop = -1;
8442+ dinfo->di_bbot = -1;
8443+ bbot = au_dbbot(parent);
8444+ bindex = 0;
8445+ p = au_hdentry(dinfo, bindex);
8446+ for (; bindex <= bbot; bindex++, p++)
1facf9fc 8447+ if (p->hd_dentry) {
5afbbe0d 8448+ dinfo->di_btop = bindex;
1facf9fc 8449+ break;
8450+ }
8451+
5afbbe0d
AM
8452+ if (dinfo->di_btop >= 0) {
8453+ bindex = bbot;
8454+ p = au_hdentry(dinfo, bindex);
8455+ for (; bindex >= 0; bindex--, p--)
027c5e7a 8456+ if (p->hd_dentry) {
5afbbe0d 8457+ dinfo->di_bbot = bindex;
027c5e7a
AM
8458+ err = 0;
8459+ break;
8460+ }
8461+ }
8462+
8463+ return err;
1facf9fc 8464+}
8465+
027c5e7a 8466+static void au_do_hide(struct dentry *dentry)
1facf9fc 8467+{
027c5e7a 8468+ struct inode *inode;
1facf9fc 8469+
5527c038
JR
8470+ if (d_really_is_positive(dentry)) {
8471+ inode = d_inode(dentry);
8472+ if (!d_is_dir(dentry)) {
027c5e7a
AM
8473+ if (inode->i_nlink && !d_unhashed(dentry))
8474+ drop_nlink(inode);
8475+ } else {
8476+ clear_nlink(inode);
8477+ /* stop next lookup */
8478+ inode->i_flags |= S_DEAD;
8479+ }
8480+ smp_mb(); /* necessary? */
8481+ }
8482+ d_drop(dentry);
8483+}
1308ab2a 8484+
027c5e7a
AM
8485+static int au_hide_children(struct dentry *parent)
8486+{
8487+ int err, i, j, ndentry;
8488+ struct au_dcsub_pages dpages;
8489+ struct au_dpage *dpage;
8490+ struct dentry *dentry;
1facf9fc 8491+
027c5e7a 8492+ err = au_dpages_init(&dpages, GFP_NOFS);
1facf9fc 8493+ if (unlikely(err))
8494+ goto out;
027c5e7a
AM
8495+ err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8496+ if (unlikely(err))
8497+ goto out_dpages;
1facf9fc 8498+
027c5e7a
AM
8499+ /* in reverse order */
8500+ for (i = dpages.ndpage - 1; i >= 0; i--) {
8501+ dpage = dpages.dpages + i;
8502+ ndentry = dpage->ndentry;
8503+ for (j = ndentry - 1; j >= 0; j--) {
8504+ dentry = dpage->dentries[j];
8505+ if (dentry != parent)
8506+ au_do_hide(dentry);
8507+ }
8508+ }
1facf9fc 8509+
027c5e7a
AM
8510+out_dpages:
8511+ au_dpages_free(&dpages);
4f0767ce 8512+out:
027c5e7a 8513+ return err;
1facf9fc 8514+}
8515+
027c5e7a 8516+static void au_hide(struct dentry *dentry)
1facf9fc 8517+{
027c5e7a 8518+ int err;
1facf9fc 8519+
027c5e7a 8520+ AuDbgDentry(dentry);
2000de60 8521+ if (d_is_dir(dentry)) {
027c5e7a
AM
8522+ /* shrink_dcache_parent(dentry); */
8523+ err = au_hide_children(dentry);
8524+ if (unlikely(err))
523b37e3
AM
8525+ AuIOErr("%pd, failed hiding children, ignored %d\n",
8526+ dentry, err);
027c5e7a
AM
8527+ }
8528+ au_do_hide(dentry);
8529+}
1facf9fc 8530+
027c5e7a
AM
8531+/*
8532+ * By adding a dirty branch, a cached dentry may be affected in various ways.
8533+ *
8534+ * a dirty branch is added
8535+ * - on the top of layers
8536+ * - in the middle of layers
8537+ * - to the bottom of layers
8538+ *
8539+ * on the added branch there exists
8540+ * - a whiteout
8541+ * - a diropq
8542+ * - a same named entry
8543+ * + exist
8544+ * * negative --> positive
8545+ * * positive --> positive
8546+ * - type is unchanged
8547+ * - type is changed
8548+ * + doesn't exist
8549+ * * negative --> negative
8550+ * * positive --> negative (rejected by au_br_del() for non-dir case)
8551+ * - none
8552+ */
8553+static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8554+ struct au_dinfo *tmp)
8555+{
8556+ int err;
5afbbe0d 8557+ aufs_bindex_t bindex, bbot;
027c5e7a
AM
8558+ struct {
8559+ struct dentry *dentry;
8560+ struct inode *inode;
8561+ mode_t mode;
be52b249
AM
8562+ } orig_h, tmp_h = {
8563+ .dentry = NULL
8564+ };
027c5e7a
AM
8565+ struct au_hdentry *hd;
8566+ struct inode *inode, *h_inode;
8567+ struct dentry *h_dentry;
8568+
8569+ err = 0;
5afbbe0d 8570+ AuDebugOn(dinfo->di_btop < 0);
027c5e7a 8571+ orig_h.mode = 0;
5afbbe0d 8572+ orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
5527c038
JR
8573+ orig_h.inode = NULL;
8574+ if (d_is_positive(orig_h.dentry)) {
8575+ orig_h.inode = d_inode(orig_h.dentry);
027c5e7a 8576+ orig_h.mode = orig_h.inode->i_mode & S_IFMT;
5527c038 8577+ }
5afbbe0d
AM
8578+ if (tmp->di_btop >= 0) {
8579+ tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
5527c038
JR
8580+ if (d_is_positive(tmp_h.dentry)) {
8581+ tmp_h.inode = d_inode(tmp_h.dentry);
027c5e7a 8582+ tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
5527c038 8583+ }
027c5e7a
AM
8584+ }
8585+
5527c038
JR
8586+ inode = NULL;
8587+ if (d_really_is_positive(dentry))
8588+ inode = d_inode(dentry);
027c5e7a 8589+ if (!orig_h.inode) {
acd2b654 8590+ AuDbg("negative originally\n");
027c5e7a
AM
8591+ if (inode) {
8592+ au_hide(dentry);
8593+ goto out;
8594+ }
8595+ AuDebugOn(inode);
5afbbe0d 8596+ AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
027c5e7a
AM
8597+ AuDebugOn(dinfo->di_bdiropq != -1);
8598+
8599+ if (!tmp_h.inode) {
8600+ AuDbg("negative --> negative\n");
8601+ /* should have only one negative lower */
5afbbe0d
AM
8602+ if (tmp->di_btop >= 0
8603+ && tmp->di_btop < dinfo->di_btop) {
8604+ AuDebugOn(tmp->di_btop != tmp->di_bbot);
8605+ AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8606+ au_set_h_dptr(dentry, dinfo->di_btop, NULL);
027c5e7a 8607+ au_di_cp(dinfo, tmp);
5afbbe0d
AM
8608+ hd = au_hdentry(tmp, tmp->di_btop);
8609+ au_set_h_dptr(dentry, tmp->di_btop,
027c5e7a
AM
8610+ dget(hd->hd_dentry));
8611+ }
8612+ au_dbg_verify_dinode(dentry);
8613+ } else {
8614+ AuDbg("negative --> positive\n");
8615+ /*
8616+ * similar to the behaviour of creating with bypassing
8617+ * aufs.
8618+ * unhash it in order to force an error in the
8619+ * succeeding create operation.
8620+ * we should not set S_DEAD here.
8621+ */
8622+ d_drop(dentry);
8623+ /* au_di_swap(tmp, dinfo); */
8624+ au_dbg_verify_dinode(dentry);
8625+ }
8626+ } else {
8627+ AuDbg("positive originally\n");
8628+ /* inode may be NULL */
8629+ AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8630+ if (!tmp_h.inode) {
8631+ AuDbg("positive --> negative\n");
8632+ /* or bypassing aufs */
8633+ au_hide(dentry);
5afbbe0d 8634+ if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
027c5e7a
AM
8635+ dinfo->di_bwh = tmp->di_bwh;
8636+ if (inode)
8637+ err = au_refresh_hinode_self(inode);
8638+ au_dbg_verify_dinode(dentry);
8639+ } else if (orig_h.mode == tmp_h.mode) {
8640+ AuDbg("positive --> positive, same type\n");
8641+ if (!S_ISDIR(orig_h.mode)
5afbbe0d 8642+ && dinfo->di_btop > tmp->di_btop) {
027c5e7a
AM
8643+ /*
8644+ * similar to the behaviour of removing and
8645+ * creating.
8646+ */
8647+ au_hide(dentry);
8648+ if (inode)
8649+ err = au_refresh_hinode_self(inode);
8650+ au_dbg_verify_dinode(dentry);
8651+ } else {
8652+ /* fill empty slots */
5afbbe0d
AM
8653+ if (dinfo->di_btop > tmp->di_btop)
8654+ dinfo->di_btop = tmp->di_btop;
8655+ if (dinfo->di_bbot < tmp->di_bbot)
8656+ dinfo->di_bbot = tmp->di_bbot;
027c5e7a
AM
8657+ dinfo->di_bwh = tmp->di_bwh;
8658+ dinfo->di_bdiropq = tmp->di_bdiropq;
5afbbe0d
AM
8659+ bbot = dinfo->di_bbot;
8660+ bindex = tmp->di_btop;
8661+ hd = au_hdentry(tmp, bindex);
8662+ for (; bindex <= bbot; bindex++, hd++) {
027c5e7a
AM
8663+ if (au_h_dptr(dentry, bindex))
8664+ continue;
5afbbe0d 8665+ h_dentry = hd->hd_dentry;
027c5e7a
AM
8666+ if (!h_dentry)
8667+ continue;
5527c038
JR
8668+ AuDebugOn(d_is_negative(h_dentry));
8669+ h_inode = d_inode(h_dentry);
027c5e7a
AM
8670+ AuDebugOn(orig_h.mode
8671+ != (h_inode->i_mode
8672+ & S_IFMT));
8673+ au_set_h_dptr(dentry, bindex,
8674+ dget(h_dentry));
8675+ }
5afbbe0d
AM
8676+ if (inode)
8677+ err = au_refresh_hinode(inode, dentry);
027c5e7a
AM
8678+ au_dbg_verify_dinode(dentry);
8679+ }
8680+ } else {
8681+ AuDbg("positive --> positive, different type\n");
8682+ /* similar to the behaviour of removing and creating */
8683+ au_hide(dentry);
8684+ if (inode)
8685+ err = au_refresh_hinode_self(inode);
8686+ au_dbg_verify_dinode(dentry);
8687+ }
8688+ }
8689+
8690+out:
8691+ return err;
8692+}
8693+
79b8bda9
AM
8694+void au_refresh_dop(struct dentry *dentry, int force_reval)
8695+{
8696+ const struct dentry_operations *dop
8697+ = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8698+ static const unsigned int mask
8699+ = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8700+
8701+ BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8702+
8703+ if (dentry->d_op == dop)
8704+ return;
8705+
8706+ AuDbg("%pd\n", dentry);
8707+ spin_lock(&dentry->d_lock);
8708+ if (dop == &aufs_dop)
8709+ dentry->d_flags |= mask;
8710+ else
8711+ dentry->d_flags &= ~mask;
8712+ dentry->d_op = dop;
8713+ spin_unlock(&dentry->d_lock);
8714+}
8715+
027c5e7a
AM
8716+int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8717+{
e2f27e51 8718+ int err, ebrange, nbr;
027c5e7a
AM
8719+ unsigned int sigen;
8720+ struct au_dinfo *dinfo, *tmp;
8721+ struct super_block *sb;
8722+ struct inode *inode;
8723+
8724+ DiMustWriteLock(dentry);
8725+ AuDebugOn(IS_ROOT(dentry));
5527c038 8726+ AuDebugOn(d_really_is_negative(parent));
027c5e7a
AM
8727+
8728+ sb = dentry->d_sb;
027c5e7a
AM
8729+ sigen = au_sigen(sb);
8730+ err = au_digen_test(parent, sigen);
8731+ if (unlikely(err))
8732+ goto out;
8733+
e2f27e51 8734+ nbr = au_sbbot(sb) + 1;
027c5e7a 8735+ dinfo = au_di(dentry);
e2f27e51 8736+ err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
027c5e7a
AM
8737+ if (unlikely(err))
8738+ goto out;
8739+ ebrange = au_dbrange_test(dentry);
8740+ if (!ebrange)
8741+ ebrange = au_do_refresh_hdentry(dentry, parent);
8742+
38d290e6 8743+ if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
5afbbe0d 8744+ AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
5527c038
JR
8745+ if (d_really_is_positive(dentry)) {
8746+ inode = d_inode(dentry);
027c5e7a 8747+ err = au_refresh_hinode_self(inode);
5527c038 8748+ }
027c5e7a
AM
8749+ au_dbg_verify_dinode(dentry);
8750+ if (!err)
8751+ goto out_dgen; /* success */
8752+ goto out;
8753+ }
8754+
8755+ /* temporary dinfo */
8756+ AuDbgDentry(dentry);
8757+ err = -ENOMEM;
8758+ tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8759+ if (unlikely(!tmp))
8760+ goto out;
8761+ au_di_swap(tmp, dinfo);
8762+ /* returns the number of positive dentries */
8763+ /*
8764+ * if current working dir is removed, it returns an error.
8765+ * but the dentry is legal.
8766+ */
5afbbe0d 8767+ err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
027c5e7a
AM
8768+ AuDbgDentry(dentry);
8769+ au_di_swap(tmp, dinfo);
8770+ if (err == -ENOENT)
8771+ err = 0;
8772+ if (err >= 0) {
8773+ /* compare/refresh by dinfo */
8774+ AuDbgDentry(dentry);
8775+ err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8776+ au_dbg_verify_dinode(dentry);
8777+ AuTraceErr(err);
8778+ }
e2f27e51 8779+ au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
027c5e7a
AM
8780+ au_rw_write_unlock(&tmp->di_rwsem);
8781+ au_di_free(tmp);
8782+ if (unlikely(err))
8783+ goto out;
8784+
8785+out_dgen:
8786+ au_update_digen(dentry);
8787+out:
8788+ if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
523b37e3 8789+ AuIOErr("failed refreshing %pd, %d\n", dentry, err);
027c5e7a
AM
8790+ AuDbgDentry(dentry);
8791+ }
8792+ AuTraceErr(err);
8793+ return err;
8794+}
8795+
b4510431
AM
8796+static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8797+ struct dentry *dentry, aufs_bindex_t bindex)
027c5e7a
AM
8798+{
8799+ int err, valid;
027c5e7a
AM
8800+
8801+ err = 0;
8802+ if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8803+ goto out;
027c5e7a
AM
8804+
8805+ AuDbg("b%d\n", bindex);
b4510431
AM
8806+ /*
8807+ * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8808+ * due to whiteout and branch permission.
8809+ */
8810+ flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8811+ | LOOKUP_FOLLOW | LOOKUP_EXCL);
8812+ /* it may return tri-state */
8813+ valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
1facf9fc 8814+
8815+ if (unlikely(valid < 0))
8816+ err = valid;
8817+ else if (!valid)
8818+ err = -EINVAL;
8819+
4f0767ce 8820+out:
1facf9fc 8821+ AuTraceErr(err);
8822+ return err;
8823+}
8824+
8825+/* todo: remove this */
8826+static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8b6a4947 8827+ unsigned int flags, int do_udba, int dirren)
1facf9fc 8828+{
8829+ int err;
8830+ umode_t mode, h_mode;
5afbbe0d 8831+ aufs_bindex_t bindex, btail, btop, ibs, ibe;
38d290e6 8832+ unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
4a4d8108 8833+ struct inode *h_inode, *h_cached_inode;
1facf9fc 8834+ struct dentry *h_dentry;
8835+ struct qstr *name, *h_name;
8836+
8837+ err = 0;
8838+ plus = 0;
8839+ mode = 0;
1facf9fc 8840+ ibs = -1;
8841+ ibe = -1;
8842+ unhashed = !!d_unhashed(dentry);
8843+ is_root = !!IS_ROOT(dentry);
8844+ name = &dentry->d_name;
38d290e6 8845+ tmpfile = au_di(dentry)->di_tmpfile;
1facf9fc 8846+
8847+ /*
7f207e10
AM
8848+ * Theoretically, REVAL test should be unnecessary in case of
8849+ * {FS,I}NOTIFY.
8850+ * But {fs,i}notify doesn't fire some necessary events,
1facf9fc 8851+ * IN_ATTRIB for atime/nlink/pageio
1facf9fc 8852+ * Let's do REVAL test too.
8853+ */
8854+ if (do_udba && inode) {
8855+ mode = (inode->i_mode & S_IFMT);
8856+ plus = (inode->i_nlink > 0);
5afbbe0d
AM
8857+ ibs = au_ibtop(inode);
8858+ ibe = au_ibbot(inode);
1facf9fc 8859+ }
8860+
5afbbe0d
AM
8861+ btop = au_dbtop(dentry);
8862+ btail = btop;
1facf9fc 8863+ if (inode && S_ISDIR(inode->i_mode))
8864+ btail = au_dbtaildir(dentry);
5afbbe0d 8865+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 8866+ h_dentry = au_h_dptr(dentry, bindex);
8867+ if (!h_dentry)
8868+ continue;
8869+
523b37e3
AM
8870+ AuDbg("b%d, %pd\n", bindex, h_dentry);
8871+ h_nfs = !!au_test_nfs(h_dentry->d_sb);
027c5e7a 8872+ spin_lock(&h_dentry->d_lock);
1facf9fc 8873+ h_name = &h_dentry->d_name;
8874+ if (unlikely(do_udba
8875+ && !is_root
523b37e3
AM
8876+ && ((!h_nfs
8877+ && (unhashed != !!d_unhashed(h_dentry)
8b6a4947 8878+ || (!tmpfile && !dirren
38d290e6
JR
8879+ && !au_qstreq(name, h_name))
8880+ ))
523b37e3
AM
8881+ || (h_nfs
8882+ && !(flags & LOOKUP_OPEN)
8883+ && (h_dentry->d_flags
8884+ & DCACHE_NFSFS_RENAMED)))
1facf9fc 8885+ )) {
38d290e6
JR
8886+ int h_unhashed;
8887+
8888+ h_unhashed = d_unhashed(h_dentry);
027c5e7a 8889+ spin_unlock(&h_dentry->d_lock);
38d290e6
JR
8890+ AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
8891+ unhashed, h_unhashed, dentry, h_dentry);
1facf9fc 8892+ goto err;
8893+ }
027c5e7a 8894+ spin_unlock(&h_dentry->d_lock);
1facf9fc 8895+
b4510431 8896+ err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
1facf9fc 8897+ if (unlikely(err))
8898+ /* do not goto err, to keep the errno */
8899+ break;
8900+
8901+ /* todo: plink too? */
8902+ if (!do_udba)
8903+ continue;
8904+
8905+ /* UDBA tests */
5527c038 8906+ if (unlikely(!!inode != d_is_positive(h_dentry)))
1facf9fc 8907+ goto err;
8908+
5527c038
JR
8909+ h_inode = NULL;
8910+ if (d_is_positive(h_dentry))
8911+ h_inode = d_inode(h_dentry);
1facf9fc 8912+ h_plus = plus;
8913+ h_mode = mode;
8914+ h_cached_inode = h_inode;
8915+ if (h_inode) {
8916+ h_mode = (h_inode->i_mode & S_IFMT);
8917+ h_plus = (h_inode->i_nlink > 0);
8918+ }
8919+ if (inode && ibs <= bindex && bindex <= ibe)
8920+ h_cached_inode = au_h_iptr(inode, bindex);
8921+
523b37e3 8922+ if (!h_nfs) {
38d290e6 8923+ if (unlikely(plus != h_plus && !tmpfile))
523b37e3
AM
8924+ goto err;
8925+ } else {
8926+ if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
8927+ && !is_root
8928+ && !IS_ROOT(h_dentry)
8929+ && unhashed != d_unhashed(h_dentry)))
8930+ goto err;
8931+ }
8932+ if (unlikely(mode != h_mode
1facf9fc 8933+ || h_cached_inode != h_inode))
8934+ goto err;
8935+ continue;
8936+
f6b6e03d 8937+err:
1facf9fc 8938+ err = -EINVAL;
8939+ break;
8940+ }
8941+
523b37e3 8942+ AuTraceErr(err);
1facf9fc 8943+ return err;
8944+}
8945+
027c5e7a 8946+/* todo: consolidate with do_refresh() and au_reval_for_attr() */
1facf9fc 8947+static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
8948+{
8949+ int err;
8950+ struct dentry *parent;
1facf9fc 8951+
027c5e7a 8952+ if (!au_digen_test(dentry, sigen))
1facf9fc 8953+ return 0;
8954+
8955+ parent = dget_parent(dentry);
8956+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 8957+ AuDebugOn(au_digen_test(parent, sigen));
1facf9fc 8958+ au_dbg_verify_gen(parent, sigen);
027c5e7a 8959+ err = au_refresh_dentry(dentry, parent);
1facf9fc 8960+ di_read_unlock(parent, AuLock_IR);
8961+ dput(parent);
027c5e7a 8962+ AuTraceErr(err);
1facf9fc 8963+ return err;
8964+}
8965+
8966+int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
8967+{
8968+ int err;
8969+ struct dentry *d, *parent;
1facf9fc 8970+
027c5e7a 8971+ if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
1facf9fc 8972+ return simple_reval_dpath(dentry, sigen);
8973+
8974+ /* slow loop, keep it simple and stupid */
8975+ /* cf: au_cpup_dirs() */
8976+ err = 0;
8977+ parent = NULL;
027c5e7a 8978+ while (au_digen_test(dentry, sigen)) {
1facf9fc 8979+ d = dentry;
8980+ while (1) {
8981+ dput(parent);
8982+ parent = dget_parent(d);
027c5e7a 8983+ if (!au_digen_test(parent, sigen))
1facf9fc 8984+ break;
8985+ d = parent;
8986+ }
8987+
1facf9fc 8988+ if (d != dentry)
027c5e7a 8989+ di_write_lock_child2(d);
1facf9fc 8990+
8991+ /* someone might update our dentry while we were sleeping */
027c5e7a
AM
8992+ if (au_digen_test(d, sigen)) {
8993+ /*
8994+ * todo: consolidate with simple_reval_dpath(),
8995+ * do_refresh() and au_reval_for_attr().
8996+ */
1facf9fc 8997+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 8998+ err = au_refresh_dentry(d, parent);
1facf9fc 8999+ di_read_unlock(parent, AuLock_IR);
9000+ }
9001+
9002+ if (d != dentry)
9003+ di_write_unlock(d);
9004+ dput(parent);
9005+ if (unlikely(err))
9006+ break;
9007+ }
9008+
9009+ return err;
9010+}
9011+
9012+/*
9013+ * if valid returns 1, otherwise 0.
9014+ */
b4510431 9015+static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
1facf9fc 9016+{
9017+ int valid, err;
9018+ unsigned int sigen;
8b6a4947 9019+ unsigned char do_udba, dirren;
1facf9fc 9020+ struct super_block *sb;
9021+ struct inode *inode;
9022+
027c5e7a 9023+ /* todo: support rcu-walk? */
b4510431 9024+ if (flags & LOOKUP_RCU)
027c5e7a
AM
9025+ return -ECHILD;
9026+
9027+ valid = 0;
9028+ if (unlikely(!au_di(dentry)))
9029+ goto out;
9030+
e49829fe 9031+ valid = 1;
1facf9fc 9032+ sb = dentry->d_sb;
e49829fe
JR
9033+ /*
9034+ * todo: very ugly
9035+ * i_mutex of parent dir may be held,
9036+ * but we should not return 'invalid' due to busy.
9037+ */
9038+ err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
9039+ if (unlikely(err)) {
9040+ valid = err;
027c5e7a 9041+ AuTraceErr(err);
e49829fe
JR
9042+ goto out;
9043+ }
5527c038
JR
9044+ inode = NULL;
9045+ if (d_really_is_positive(dentry))
9046+ inode = d_inode(dentry);
5afbbe0d 9047+ if (unlikely(inode && au_is_bad_inode(inode))) {
c1595e42
JR
9048+ err = -EINVAL;
9049+ AuTraceErr(err);
9050+ goto out_dgrade;
9051+ }
027c5e7a
AM
9052+ if (unlikely(au_dbrange_test(dentry))) {
9053+ err = -EINVAL;
9054+ AuTraceErr(err);
9055+ goto out_dgrade;
1facf9fc 9056+ }
027c5e7a
AM
9057+
9058+ sigen = au_sigen(sb);
9059+ if (au_digen_test(dentry, sigen)) {
1facf9fc 9060+ AuDebugOn(IS_ROOT(dentry));
027c5e7a
AM
9061+ err = au_reval_dpath(dentry, sigen);
9062+ if (unlikely(err)) {
9063+ AuTraceErr(err);
1facf9fc 9064+ goto out_dgrade;
027c5e7a 9065+ }
1facf9fc 9066+ }
9067+ di_downgrade_lock(dentry, AuLock_IR);
9068+
1facf9fc 9069+ err = -EINVAL;
c1595e42 9070+ if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
523b37e3 9071+ && inode
38d290e6 9072+ && !(inode->i_state && I_LINKABLE)
79b8bda9
AM
9073+ && (IS_DEADDIR(inode) || !inode->i_nlink)) {
9074+ AuTraceErr(err);
027c5e7a 9075+ goto out_inval;
79b8bda9 9076+ }
027c5e7a 9077+
1facf9fc 9078+ do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
9079+ if (do_udba && inode) {
5afbbe0d 9080+ aufs_bindex_t btop = au_ibtop(inode);
027c5e7a 9081+ struct inode *h_inode;
1facf9fc 9082+
5afbbe0d
AM
9083+ if (btop >= 0) {
9084+ h_inode = au_h_iptr(inode, btop);
79b8bda9
AM
9085+ if (h_inode && au_test_higen(inode, h_inode)) {
9086+ AuTraceErr(err);
027c5e7a 9087+ goto out_inval;
79b8bda9 9088+ }
027c5e7a 9089+ }
1facf9fc 9090+ }
9091+
8b6a4947
AM
9092+ dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
9093+ err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
5afbbe0d 9094+ if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
1facf9fc 9095+ err = -EIO;
523b37e3
AM
9096+ AuDbg("both of real entry and whiteout found, %p, err %d\n",
9097+ dentry, err);
027c5e7a 9098+ }
e49829fe 9099+ goto out_inval;
1facf9fc 9100+
4f0767ce 9101+out_dgrade:
1facf9fc 9102+ di_downgrade_lock(dentry, AuLock_IR);
e49829fe 9103+out_inval:
1facf9fc 9104+ aufs_read_unlock(dentry, AuLock_IR);
9105+ AuTraceErr(err);
9106+ valid = !err;
e49829fe 9107+out:
027c5e7a 9108+ if (!valid) {
523b37e3 9109+ AuDbg("%pd invalid, %d\n", dentry, valid);
027c5e7a
AM
9110+ d_drop(dentry);
9111+ }
1facf9fc 9112+ return valid;
9113+}
9114+
9115+static void aufs_d_release(struct dentry *dentry)
9116+{
027c5e7a 9117+ if (au_di(dentry)) {
4a4d8108
AM
9118+ au_di_fin(dentry);
9119+ au_hn_di_reinit(dentry);
1facf9fc 9120+ }
1facf9fc 9121+}
9122+
4a4d8108 9123+const struct dentry_operations aufs_dop = {
c06a8ce3
AM
9124+ .d_revalidate = aufs_d_revalidate,
9125+ .d_weak_revalidate = aufs_d_revalidate,
9126+ .d_release = aufs_d_release
1facf9fc 9127+};
79b8bda9
AM
9128+
9129+/* aufs_dop without d_revalidate */
9130+const struct dentry_operations aufs_dop_noreval = {
9131+ .d_release = aufs_d_release
9132+};
7f207e10 9133diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
eca34b5c 9134--- /usr/share/empty/fs/aufs/dentry.h 1970-01-01 01:00:00.000000000 +0100
2255d0fe
AM
9135+++ linux/fs/aufs/dentry.h 2022-12-17 09:21:34.796521861 +0100
9136@@ -0,0 +1,270 @@
062440b3 9137+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 9138+/*
0dcfbb52 9139+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 9140+ *
0dcfbb52 9141+ * This program is free software; you can redistribute it and/or modify
1facf9fc 9142+ * it under the terms of the GNU General Public License as published by
9143+ * the Free Software Foundation; either version 2 of the License, or
9144+ * (at your option) any later version.
dece6358
AM
9145+ *
9146+ * This program is distributed in the hope that it will be useful,
9147+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9148+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9149+ * GNU General Public License for more details.
9150+ *
9151+ * You should have received a copy of the GNU General Public License
523b37e3 9152+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 9153+ */
9154+
9155+/*
9156+ * lookup and dentry operations
9157+ */
9158+
9159+#ifndef __AUFS_DENTRY_H__
9160+#define __AUFS_DENTRY_H__
9161+
9162+#ifdef __KERNEL__
9163+
dece6358 9164+#include <linux/dcache.h>
8b6a4947 9165+#include "dirren.h"
1facf9fc 9166+#include "rwsem.h"
9167+
1facf9fc 9168+struct au_hdentry {
9169+ struct dentry *hd_dentry;
027c5e7a 9170+ aufs_bindex_t hd_id;
1facf9fc 9171+};
9172+
9173+struct au_dinfo {
9174+ atomic_t di_generation;
9175+
dece6358 9176+ struct au_rwsem di_rwsem;
5afbbe0d 9177+ aufs_bindex_t di_btop, di_bbot, di_bwh, di_bdiropq;
38d290e6 9178+ unsigned char di_tmpfile; /* to allow the different name */
1c60b727 9179+ struct au_hdentry *di_hdentry;
2255d0fe 9180+ struct file *di_htmpfile;
9f237c51 9181+ struct rcu_head rcu;
4a4d8108 9182+} ____cacheline_aligned_in_smp;
1facf9fc 9183+
9184+/* ---------------------------------------------------------------------- */
9185+
5afbbe0d
AM
9186+/* flags for au_lkup_dentry() */
9187+#define AuLkup_ALLOW_NEG 1
9188+#define AuLkup_IGNORE_PERM (1 << 1)
8b6a4947 9189+#define AuLkup_DIRREN (1 << 2)
5afbbe0d
AM
9190+#define au_ftest_lkup(flags, name) ((flags) & AuLkup_##name)
9191+#define au_fset_lkup(flags, name) \
9192+ do { (flags) |= AuLkup_##name; } while (0)
9193+#define au_fclr_lkup(flags, name) \
9194+ do { (flags) &= ~AuLkup_##name; } while (0)
9195+
8b6a4947
AM
9196+#ifndef CONFIG_AUFS_DIRREN
9197+#undef AuLkup_DIRREN
9198+#define AuLkup_DIRREN 0
9199+#endif
9200+
9201+struct au_do_lookup_args {
9202+ unsigned int flags;
9203+ mode_t type;
9204+ struct qstr whname, *name;
9205+ struct au_dr_lookup dirren;
9206+};
9207+
5afbbe0d
AM
9208+/* ---------------------------------------------------------------------- */
9209+
1facf9fc 9210+/* dentry.c */
79b8bda9 9211+extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
1facf9fc 9212+struct au_branch;
0b2a12c6 9213+struct dentry *au_sio_lkup_one(struct user_namespace *userns, struct qstr *name,
758e9dad 9214+ struct path *ppath);
1facf9fc 9215+int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
9216+ struct dentry *h_parent, struct au_branch *br);
9217+
5afbbe0d
AM
9218+int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
9219+ unsigned int flags);
86dc4139 9220+int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
027c5e7a 9221+int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
1facf9fc 9222+int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
79b8bda9 9223+void au_refresh_dop(struct dentry *dentry, int force_reval);
1facf9fc 9224+
9225+/* dinfo.c */
4a4d8108 9226+void au_di_init_once(void *_di);
027c5e7a
AM
9227+struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
9228+void au_di_free(struct au_dinfo *dinfo);
9229+void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
9230+void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
4a4d8108
AM
9231+int au_di_init(struct dentry *dentry);
9232+void au_di_fin(struct dentry *dentry);
e2f27e51 9233+int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
1facf9fc 9234+
9235+void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
9236+void di_read_unlock(struct dentry *d, int flags);
9237+void di_downgrade_lock(struct dentry *d, int flags);
9238+void di_write_lock(struct dentry *d, unsigned int lsc);
9239+void di_write_unlock(struct dentry *d);
9240+void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
9241+void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
9242+void di_write_unlock2(struct dentry *d1, struct dentry *d2);
9243+
9244+struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
2cbb1c4b 9245+struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
1facf9fc 9246+aufs_bindex_t au_dbtail(struct dentry *dentry);
9247+aufs_bindex_t au_dbtaildir(struct dentry *dentry);
9248+
9249+void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9250+ struct dentry *h_dentry);
027c5e7a
AM
9251+int au_digen_test(struct dentry *dentry, unsigned int sigen);
9252+int au_dbrange_test(struct dentry *dentry);
1facf9fc 9253+void au_update_digen(struct dentry *dentry);
9254+void au_update_dbrange(struct dentry *dentry, int do_put_zero);
5afbbe0d
AM
9255+void au_update_dbtop(struct dentry *dentry);
9256+void au_update_dbbot(struct dentry *dentry);
1facf9fc 9257+int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
9258+
9259+/* ---------------------------------------------------------------------- */
9260+
9261+static inline struct au_dinfo *au_di(struct dentry *dentry)
9262+{
9263+ return dentry->d_fsdata;
9264+}
9265+
9266+/* ---------------------------------------------------------------------- */
9267+
9268+/* lock subclass for dinfo */
9269+enum {
9270+ AuLsc_DI_CHILD, /* child first */
4a4d8108 9271+ AuLsc_DI_CHILD2, /* rename(2), link(2), and cpup at hnotify */
1facf9fc 9272+ AuLsc_DI_CHILD3, /* copyup dirs */
9273+ AuLsc_DI_PARENT,
9274+ AuLsc_DI_PARENT2,
027c5e7a
AM
9275+ AuLsc_DI_PARENT3,
9276+ AuLsc_DI_TMP /* temp for replacing dinfo */
1facf9fc 9277+};
9278+
9279+/*
9280+ * di_read_lock_child, di_write_lock_child,
9281+ * di_read_lock_child2, di_write_lock_child2,
9282+ * di_read_lock_child3, di_write_lock_child3,
9283+ * di_read_lock_parent, di_write_lock_parent,
9284+ * di_read_lock_parent2, di_write_lock_parent2,
9285+ * di_read_lock_parent3, di_write_lock_parent3,
9286+ */
9287+#define AuReadLockFunc(name, lsc) \
9288+static inline void di_read_lock_##name(struct dentry *d, int flags) \
9289+{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
9290+
9291+#define AuWriteLockFunc(name, lsc) \
9292+static inline void di_write_lock_##name(struct dentry *d) \
9293+{ di_write_lock(d, AuLsc_DI_##lsc); }
9294+
9295+#define AuRWLockFuncs(name, lsc) \
9296+ AuReadLockFunc(name, lsc) \
9297+ AuWriteLockFunc(name, lsc)
9298+
9299+AuRWLockFuncs(child, CHILD);
9300+AuRWLockFuncs(child2, CHILD2);
9301+AuRWLockFuncs(child3, CHILD3);
9302+AuRWLockFuncs(parent, PARENT);
9303+AuRWLockFuncs(parent2, PARENT2);
9304+AuRWLockFuncs(parent3, PARENT3);
9305+
9306+#undef AuReadLockFunc
9307+#undef AuWriteLockFunc
9308+#undef AuRWLockFuncs
9309+
9310+#define DiMustNoWaiters(d) AuRwMustNoWaiters(&au_di(d)->di_rwsem)
dece6358
AM
9311+#define DiMustAnyLock(d) AuRwMustAnyLock(&au_di(d)->di_rwsem)
9312+#define DiMustWriteLock(d) AuRwMustWriteLock(&au_di(d)->di_rwsem)
1facf9fc 9313+
9314+/* ---------------------------------------------------------------------- */
9315+
9316+/* todo: memory barrier? */
9317+static inline unsigned int au_digen(struct dentry *d)
9318+{
9319+ return atomic_read(&au_di(d)->di_generation);
9320+}
9321+
9322+static inline void au_h_dentry_init(struct au_hdentry *hdentry)
9323+{
9324+ hdentry->hd_dentry = NULL;
9325+}
9326+
5afbbe0d
AM
9327+static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
9328+ aufs_bindex_t bindex)
9329+{
9330+ return di->di_hdentry + bindex;
9331+}
9332+
1facf9fc 9333+static inline void au_hdput(struct au_hdentry *hd)
9334+{
4a4d8108
AM
9335+ if (hd)
9336+ dput(hd->hd_dentry);
1facf9fc 9337+}
9338+
5afbbe0d 9339+static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
1facf9fc 9340+{
1308ab2a 9341+ DiMustAnyLock(dentry);
5afbbe0d 9342+ return au_di(dentry)->di_btop;
1facf9fc 9343+}
9344+
5afbbe0d 9345+static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
1facf9fc 9346+{
1308ab2a 9347+ DiMustAnyLock(dentry);
5afbbe0d 9348+ return au_di(dentry)->di_bbot;
1facf9fc 9349+}
9350+
9351+static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
9352+{
1308ab2a 9353+ DiMustAnyLock(dentry);
1facf9fc 9354+ return au_di(dentry)->di_bwh;
9355+}
9356+
9357+static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
9358+{
1308ab2a 9359+ DiMustAnyLock(dentry);
1facf9fc 9360+ return au_di(dentry)->di_bdiropq;
9361+}
9362+
9363+/* todo: hard/soft set? */
5afbbe0d 9364+static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
1facf9fc 9365+{
1308ab2a 9366+ DiMustWriteLock(dentry);
5afbbe0d 9367+ au_di(dentry)->di_btop = bindex;
1facf9fc 9368+}
9369+
5afbbe0d 9370+static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
1facf9fc 9371+{
1308ab2a 9372+ DiMustWriteLock(dentry);
5afbbe0d 9373+ au_di(dentry)->di_bbot = bindex;
1facf9fc 9374+}
9375+
9376+static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
9377+{
1308ab2a 9378+ DiMustWriteLock(dentry);
5afbbe0d 9379+ /* dbwh can be outside of btop - bbot range */
1facf9fc 9380+ au_di(dentry)->di_bwh = bindex;
9381+}
9382+
9383+static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
9384+{
1308ab2a 9385+ DiMustWriteLock(dentry);
1facf9fc 9386+ au_di(dentry)->di_bdiropq = bindex;
9387+}
9388+
9389+/* ---------------------------------------------------------------------- */
9390+
4a4d8108 9391+#ifdef CONFIG_AUFS_HNOTIFY
1facf9fc 9392+static inline void au_digen_dec(struct dentry *d)
9393+{
e49829fe 9394+ atomic_dec(&au_di(d)->di_generation);
1facf9fc 9395+}
9396+
4a4d8108 9397+static inline void au_hn_di_reinit(struct dentry *dentry)
1facf9fc 9398+{
9399+ dentry->d_fsdata = NULL;
9400+}
9401+#else
4a4d8108
AM
9402+AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
9403+#endif /* CONFIG_AUFS_HNOTIFY */
1facf9fc 9404+
9405+#endif /* __KERNEL__ */
9406+#endif /* __AUFS_DENTRY_H__ */
7f207e10 9407diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
eca34b5c 9408--- /usr/share/empty/fs/aufs/dinfo.c 1970-01-01 01:00:00.000000000 +0100
2255d0fe
AM
9409+++ linux/fs/aufs/dinfo.c 2022-12-17 09:21:34.796521861 +0100
9410@@ -0,0 +1,555 @@
cd7a4cd9 9411+// SPDX-License-Identifier: GPL-2.0
1facf9fc 9412+/*
0dcfbb52 9413+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 9414+ *
0dcfbb52 9415+ * This program is free software; you can redistribute it and/or modify
1facf9fc 9416+ * it under the terms of the GNU General Public License as published by
9417+ * the Free Software Foundation; either version 2 of the License, or
9418+ * (at your option) any later version.
dece6358
AM
9419+ *
9420+ * This program is distributed in the hope that it will be useful,
9421+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9422+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9423+ * GNU General Public License for more details.
9424+ *
9425+ * You should have received a copy of the GNU General Public License
523b37e3 9426+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 9427+ */
9428+
9429+/*
9430+ * dentry private data
9431+ */
9432+
9433+#include "aufs.h"
9434+
e49829fe 9435+void au_di_init_once(void *_dinfo)
4a4d8108 9436+{
e49829fe 9437+ struct au_dinfo *dinfo = _dinfo;
4a4d8108 9438+
e49829fe 9439+ au_rw_init(&dinfo->di_rwsem);
4a4d8108
AM
9440+}
9441+
027c5e7a 9442+struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
1facf9fc 9443+{
9444+ struct au_dinfo *dinfo;
027c5e7a 9445+ int nbr, i;
1facf9fc 9446+
9447+ dinfo = au_cache_alloc_dinfo();
9448+ if (unlikely(!dinfo))
9449+ goto out;
9450+
5afbbe0d 9451+ nbr = au_sbbot(sb) + 1;
1facf9fc 9452+ if (nbr <= 0)
9453+ nbr = 1;
9454+ dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
027c5e7a
AM
9455+ if (dinfo->di_hdentry) {
9456+ au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
5afbbe0d
AM
9457+ dinfo->di_btop = -1;
9458+ dinfo->di_bbot = -1;
027c5e7a
AM
9459+ dinfo->di_bwh = -1;
9460+ dinfo->di_bdiropq = -1;
38d290e6 9461+ dinfo->di_tmpfile = 0;
027c5e7a
AM
9462+ for (i = 0; i < nbr; i++)
9463+ dinfo->di_hdentry[i].hd_id = -1;
2255d0fe 9464+ dinfo->di_htmpfile = NULL;
027c5e7a
AM
9465+ goto out;
9466+ }
1facf9fc 9467+
1c60b727 9468+ au_cache_free_dinfo(dinfo);
027c5e7a
AM
9469+ dinfo = NULL;
9470+
4f0767ce 9471+out:
027c5e7a 9472+ return dinfo;
1facf9fc 9473+}
9474+
027c5e7a 9475+void au_di_free(struct au_dinfo *dinfo)
4a4d8108 9476+{
4a4d8108 9477+ struct au_hdentry *p;
5afbbe0d 9478+ aufs_bindex_t bbot, bindex;
4a4d8108
AM
9479+
9480+ /* dentry may not be revalidated */
5afbbe0d 9481+ bindex = dinfo->di_btop;
4a4d8108 9482+ if (bindex >= 0) {
5afbbe0d
AM
9483+ bbot = dinfo->di_bbot;
9484+ p = au_hdentry(dinfo, bindex);
9485+ while (bindex++ <= bbot)
4a4d8108
AM
9486+ au_hdput(p++);
9487+ }
9f237c51 9488+ au_kfree_try_rcu(dinfo->di_hdentry);
1c60b727 9489+ au_cache_free_dinfo(dinfo);
027c5e7a
AM
9490+}
9491+
9492+void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9493+{
9494+ struct au_hdentry *p;
9495+ aufs_bindex_t bi;
9496+
9497+ AuRwMustWriteLock(&a->di_rwsem);
9498+ AuRwMustWriteLock(&b->di_rwsem);
9499+
9500+#define DiSwap(v, name) \
9501+ do { \
9502+ v = a->di_##name; \
9503+ a->di_##name = b->di_##name; \
9504+ b->di_##name = v; \
9505+ } while (0)
9506+
9507+ DiSwap(p, hdentry);
5afbbe0d
AM
9508+ DiSwap(bi, btop);
9509+ DiSwap(bi, bbot);
027c5e7a
AM
9510+ DiSwap(bi, bwh);
9511+ DiSwap(bi, bdiropq);
9512+ /* smp_mb(); */
9513+
9514+#undef DiSwap
9515+}
9516+
9517+void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9518+{
9519+ AuRwMustWriteLock(&dst->di_rwsem);
9520+ AuRwMustWriteLock(&src->di_rwsem);
9521+
5afbbe0d
AM
9522+ dst->di_btop = src->di_btop;
9523+ dst->di_bbot = src->di_bbot;
027c5e7a
AM
9524+ dst->di_bwh = src->di_bwh;
9525+ dst->di_bdiropq = src->di_bdiropq;
9526+ /* smp_mb(); */
9527+}
9528+
9529+int au_di_init(struct dentry *dentry)
9530+{
9531+ int err;
9532+ struct super_block *sb;
9533+ struct au_dinfo *dinfo;
9534+
9535+ err = 0;
9536+ sb = dentry->d_sb;
9537+ dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9538+ if (dinfo) {
9539+ atomic_set(&dinfo->di_generation, au_sigen(sb));
9540+ /* smp_mb(); */ /* atomic_set */
9541+ dentry->d_fsdata = dinfo;
9542+ } else
9543+ err = -ENOMEM;
9544+
9545+ return err;
9546+}
9547+
9548+void au_di_fin(struct dentry *dentry)
9549+{
9550+ struct au_dinfo *dinfo;
9551+
9552+ dinfo = au_di(dentry);
9553+ AuRwDestroy(&dinfo->di_rwsem);
9554+ au_di_free(dinfo);
4a4d8108
AM
9555+}
9556+
e2f27e51 9557+int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
1facf9fc 9558+{
9559+ int err, sz;
9560+ struct au_hdentry *hdp;
9561+
1308ab2a 9562+ AuRwMustWriteLock(&dinfo->di_rwsem);
9563+
1facf9fc 9564+ err = -ENOMEM;
5afbbe0d 9565+ sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
1facf9fc 9566+ if (!sz)
9567+ sz = sizeof(*hdp);
e2f27e51
AM
9568+ hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
9569+ may_shrink);
1facf9fc 9570+ if (hdp) {
9571+ dinfo->di_hdentry = hdp;
9572+ err = 0;
9573+ }
9574+
9575+ return err;
9576+}
9577+
9578+/* ---------------------------------------------------------------------- */
9579+
9580+static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9581+{
9582+ switch (lsc) {
9583+ case AuLsc_DI_CHILD:
9584+ ii_write_lock_child(inode);
9585+ break;
9586+ case AuLsc_DI_CHILD2:
9587+ ii_write_lock_child2(inode);
9588+ break;
9589+ case AuLsc_DI_CHILD3:
9590+ ii_write_lock_child3(inode);
9591+ break;
9592+ case AuLsc_DI_PARENT:
9593+ ii_write_lock_parent(inode);
9594+ break;
9595+ case AuLsc_DI_PARENT2:
9596+ ii_write_lock_parent2(inode);
9597+ break;
9598+ case AuLsc_DI_PARENT3:
9599+ ii_write_lock_parent3(inode);
9600+ break;
9601+ default:
9602+ BUG();
9603+ }
9604+}
9605+
9606+static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9607+{
9608+ switch (lsc) {
9609+ case AuLsc_DI_CHILD:
9610+ ii_read_lock_child(inode);
9611+ break;
9612+ case AuLsc_DI_CHILD2:
9613+ ii_read_lock_child2(inode);
9614+ break;
9615+ case AuLsc_DI_CHILD3:
9616+ ii_read_lock_child3(inode);
9617+ break;
9618+ case AuLsc_DI_PARENT:
9619+ ii_read_lock_parent(inode);
9620+ break;
9621+ case AuLsc_DI_PARENT2:
9622+ ii_read_lock_parent2(inode);
9623+ break;
9624+ case AuLsc_DI_PARENT3:
9625+ ii_read_lock_parent3(inode);
9626+ break;
9627+ default:
9628+ BUG();
9629+ }
9630+}
9631+
9632+void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9633+{
5527c038
JR
9634+ struct inode *inode;
9635+
dece6358 9636+ au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
5527c038
JR
9637+ if (d_really_is_positive(d)) {
9638+ inode = d_inode(d);
1facf9fc 9639+ if (au_ftest_lock(flags, IW))
5527c038 9640+ do_ii_write_lock(inode, lsc);
1facf9fc 9641+ else if (au_ftest_lock(flags, IR))
5527c038 9642+ do_ii_read_lock(inode, lsc);
1facf9fc 9643+ }
9644+}
9645+
9646+void di_read_unlock(struct dentry *d, int flags)
9647+{
5527c038
JR
9648+ struct inode *inode;
9649+
9650+ if (d_really_is_positive(d)) {
9651+ inode = d_inode(d);
027c5e7a
AM
9652+ if (au_ftest_lock(flags, IW)) {
9653+ au_dbg_verify_dinode(d);
5527c038 9654+ ii_write_unlock(inode);
027c5e7a
AM
9655+ } else if (au_ftest_lock(flags, IR)) {
9656+ au_dbg_verify_dinode(d);
5527c038 9657+ ii_read_unlock(inode);
027c5e7a 9658+ }
1facf9fc 9659+ }
dece6358 9660+ au_rw_read_unlock(&au_di(d)->di_rwsem);
1facf9fc 9661+}
9662+
9663+void di_downgrade_lock(struct dentry *d, int flags)
9664+{
5527c038
JR
9665+ if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9666+ ii_downgrade_lock(d_inode(d));
dece6358 9667+ au_rw_dgrade_lock(&au_di(d)->di_rwsem);
1facf9fc 9668+}
9669+
9670+void di_write_lock(struct dentry *d, unsigned int lsc)
9671+{
dece6358 9672+ au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
5527c038
JR
9673+ if (d_really_is_positive(d))
9674+ do_ii_write_lock(d_inode(d), lsc);
1facf9fc 9675+}
9676+
9677+void di_write_unlock(struct dentry *d)
9678+{
027c5e7a 9679+ au_dbg_verify_dinode(d);
5527c038
JR
9680+ if (d_really_is_positive(d))
9681+ ii_write_unlock(d_inode(d));
dece6358 9682+ au_rw_write_unlock(&au_di(d)->di_rwsem);
1facf9fc 9683+}
9684+
9685+void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9686+{
9687+ AuDebugOn(d1 == d2
5527c038 9688+ || d_inode(d1) == d_inode(d2)
1facf9fc 9689+ || d1->d_sb != d2->d_sb);
9690+
521ced18
JR
9691+ if ((isdir && au_test_subdir(d1, d2))
9692+ || d1 < d2) {
1facf9fc 9693+ di_write_lock_child(d1);
9694+ di_write_lock_child2(d2);
9695+ } else {
1facf9fc 9696+ di_write_lock_child(d2);
9697+ di_write_lock_child2(d1);
9698+ }
9699+}
9700+
9701+void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9702+{
9703+ AuDebugOn(d1 == d2
5527c038 9704+ || d_inode(d1) == d_inode(d2)
1facf9fc 9705+ || d1->d_sb != d2->d_sb);
9706+
521ced18
JR
9707+ if ((isdir && au_test_subdir(d1, d2))
9708+ || d1 < d2) {
1facf9fc 9709+ di_write_lock_parent(d1);
9710+ di_write_lock_parent2(d2);
9711+ } else {
1facf9fc 9712+ di_write_lock_parent(d2);
9713+ di_write_lock_parent2(d1);
9714+ }
9715+}
9716+
9717+void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9718+{
9719+ di_write_unlock(d1);
5527c038 9720+ if (d_inode(d1) == d_inode(d2))
dece6358 9721+ au_rw_write_unlock(&au_di(d2)->di_rwsem);
1facf9fc 9722+ else
9723+ di_write_unlock(d2);
9724+}
9725+
9726+/* ---------------------------------------------------------------------- */
9727+
9728+struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9729+{
9730+ struct dentry *d;
9731+
1308ab2a 9732+ DiMustAnyLock(dentry);
9733+
5afbbe0d 9734+ if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
1facf9fc 9735+ return NULL;
9736+ AuDebugOn(bindex < 0);
5afbbe0d 9737+ d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
c1595e42 9738+ AuDebugOn(d && au_dcount(d) <= 0);
1facf9fc 9739+ return d;
9740+}
9741+
2cbb1c4b
JR
9742+/*
9743+ * extended version of au_h_dptr().
38d290e6
JR
9744+ * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9745+ * error.
2cbb1c4b
JR
9746+ */
9747+struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9748+{
9749+ struct dentry *h_dentry;
9750+ struct inode *inode, *h_inode;
9751+
5527c038 9752+ AuDebugOn(d_really_is_negative(dentry));
2cbb1c4b
JR
9753+
9754+ h_dentry = NULL;
5afbbe0d
AM
9755+ if (au_dbtop(dentry) <= bindex
9756+ && bindex <= au_dbbot(dentry))
2cbb1c4b 9757+ h_dentry = au_h_dptr(dentry, bindex);
38d290e6 9758+ if (h_dentry && !au_d_linkable(h_dentry)) {
2cbb1c4b
JR
9759+ dget(h_dentry);
9760+ goto out; /* success */
9761+ }
9762+
5527c038 9763+ inode = d_inode(dentry);
5afbbe0d
AM
9764+ AuDebugOn(bindex < au_ibtop(inode));
9765+ AuDebugOn(au_ibbot(inode) < bindex);
2cbb1c4b
JR
9766+ h_inode = au_h_iptr(inode, bindex);
9767+ h_dentry = d_find_alias(h_inode);
9768+ if (h_dentry) {
9769+ if (!IS_ERR(h_dentry)) {
38d290e6 9770+ if (!au_d_linkable(h_dentry))
2cbb1c4b
JR
9771+ goto out; /* success */
9772+ dput(h_dentry);
9773+ } else
9774+ goto out;
9775+ }
9776+
9777+ if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9778+ h_dentry = au_plink_lkup(inode, bindex);
9779+ AuDebugOn(!h_dentry);
9780+ if (!IS_ERR(h_dentry)) {
9781+ if (!au_d_hashed_positive(h_dentry))
9782+ goto out; /* success */
9783+ dput(h_dentry);
9784+ h_dentry = NULL;
9785+ }
9786+ }
9787+
9788+out:
9789+ AuDbgDentry(h_dentry);
9790+ return h_dentry;
9791+}
9792+
1facf9fc 9793+aufs_bindex_t au_dbtail(struct dentry *dentry)
9794+{
5afbbe0d 9795+ aufs_bindex_t bbot, bwh;
1facf9fc 9796+
5afbbe0d
AM
9797+ bbot = au_dbbot(dentry);
9798+ if (0 <= bbot) {
1facf9fc 9799+ bwh = au_dbwh(dentry);
9800+ if (!bwh)
9801+ return bwh;
5afbbe0d 9802+ if (0 < bwh && bwh < bbot)
1facf9fc 9803+ return bwh - 1;
9804+ }
5afbbe0d 9805+ return bbot;
1facf9fc 9806+}
9807+
9808+aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9809+{
5afbbe0d 9810+ aufs_bindex_t bbot, bopq;
1facf9fc 9811+
5afbbe0d
AM
9812+ bbot = au_dbtail(dentry);
9813+ if (0 <= bbot) {
1facf9fc 9814+ bopq = au_dbdiropq(dentry);
5afbbe0d
AM
9815+ if (0 <= bopq && bopq < bbot)
9816+ bbot = bopq;
1facf9fc 9817+ }
5afbbe0d 9818+ return bbot;
1facf9fc 9819+}
9820+
9821+/* ---------------------------------------------------------------------- */
9822+
9823+void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9824+ struct dentry *h_dentry)
9825+{
5afbbe0d
AM
9826+ struct au_dinfo *dinfo;
9827+ struct au_hdentry *hd;
027c5e7a 9828+ struct au_branch *br;
1facf9fc 9829+
1308ab2a 9830+ DiMustWriteLock(dentry);
9831+
5afbbe0d
AM
9832+ dinfo = au_di(dentry);
9833+ hd = au_hdentry(dinfo, bindex);
4a4d8108 9834+ au_hdput(hd);
1facf9fc 9835+ hd->hd_dentry = h_dentry;
027c5e7a
AM
9836+ if (h_dentry) {
9837+ br = au_sbr(dentry->d_sb, bindex);
9838+ hd->hd_id = br->br_id;
9839+ }
9840+}
9841+
9842+int au_dbrange_test(struct dentry *dentry)
9843+{
9844+ int err;
5afbbe0d 9845+ aufs_bindex_t btop, bbot;
027c5e7a
AM
9846+
9847+ err = 0;
5afbbe0d
AM
9848+ btop = au_dbtop(dentry);
9849+ bbot = au_dbbot(dentry);
9850+ if (btop >= 0)
9851+ AuDebugOn(bbot < 0 && btop > bbot);
027c5e7a
AM
9852+ else {
9853+ err = -EIO;
5afbbe0d 9854+ AuDebugOn(bbot >= 0);
027c5e7a
AM
9855+ }
9856+
9857+ return err;
9858+}
9859+
9860+int au_digen_test(struct dentry *dentry, unsigned int sigen)
9861+{
9862+ int err;
9863+
9864+ err = 0;
9865+ if (unlikely(au_digen(dentry) != sigen
5527c038 9866+ || au_iigen_test(d_inode(dentry), sigen)))
027c5e7a
AM
9867+ err = -EIO;
9868+
9869+ return err;
1facf9fc 9870+}
9871+
9872+void au_update_digen(struct dentry *dentry)
9873+{
9874+ atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
9875+ /* smp_mb(); */ /* atomic_set */
9876+}
9877+
9878+void au_update_dbrange(struct dentry *dentry, int do_put_zero)
9879+{
9880+ struct au_dinfo *dinfo;
9881+ struct dentry *h_d;
4a4d8108 9882+ struct au_hdentry *hdp;
5afbbe0d 9883+ aufs_bindex_t bindex, bbot;
1facf9fc 9884+
1308ab2a 9885+ DiMustWriteLock(dentry);
9886+
1facf9fc 9887+ dinfo = au_di(dentry);
5afbbe0d 9888+ if (!dinfo || dinfo->di_btop < 0)
1facf9fc 9889+ return;
9890+
9891+ if (do_put_zero) {
5afbbe0d
AM
9892+ bbot = dinfo->di_bbot;
9893+ bindex = dinfo->di_btop;
9894+ hdp = au_hdentry(dinfo, bindex);
9895+ for (; bindex <= bbot; bindex++, hdp++) {
9896+ h_d = hdp->hd_dentry;
5527c038 9897+ if (h_d && d_is_negative(h_d))
1facf9fc 9898+ au_set_h_dptr(dentry, bindex, NULL);
9899+ }
9900+ }
9901+
5afbbe0d
AM
9902+ dinfo->di_btop = 0;
9903+ hdp = au_hdentry(dinfo, dinfo->di_btop);
9904+ for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
9905+ if (hdp->hd_dentry)
1facf9fc 9906+ break;
5afbbe0d
AM
9907+ if (dinfo->di_btop > dinfo->di_bbot) {
9908+ dinfo->di_btop = -1;
9909+ dinfo->di_bbot = -1;
1facf9fc 9910+ return;
9911+ }
9912+
5afbbe0d
AM
9913+ hdp = au_hdentry(dinfo, dinfo->di_bbot);
9914+ for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
9915+ if (hdp->hd_dentry)
1facf9fc 9916+ break;
5afbbe0d 9917+ AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
1facf9fc 9918+}
9919+
5afbbe0d 9920+void au_update_dbtop(struct dentry *dentry)
1facf9fc 9921+{
5afbbe0d 9922+ aufs_bindex_t bindex, bbot;
1facf9fc 9923+ struct dentry *h_dentry;
9924+
5afbbe0d
AM
9925+ bbot = au_dbbot(dentry);
9926+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
1facf9fc 9927+ h_dentry = au_h_dptr(dentry, bindex);
9928+ if (!h_dentry)
9929+ continue;
5527c038 9930+ if (d_is_positive(h_dentry)) {
5afbbe0d 9931+ au_set_dbtop(dentry, bindex);
1facf9fc 9932+ return;
9933+ }
9934+ au_set_h_dptr(dentry, bindex, NULL);
9935+ }
9936+}
9937+
5afbbe0d 9938+void au_update_dbbot(struct dentry *dentry)
1facf9fc 9939+{
5afbbe0d 9940+ aufs_bindex_t bindex, btop;
1facf9fc 9941+ struct dentry *h_dentry;
9942+
5afbbe0d
AM
9943+ btop = au_dbtop(dentry);
9944+ for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
1facf9fc 9945+ h_dentry = au_h_dptr(dentry, bindex);
9946+ if (!h_dentry)
9947+ continue;
5527c038 9948+ if (d_is_positive(h_dentry)) {
5afbbe0d 9949+ au_set_dbbot(dentry, bindex);
1facf9fc 9950+ return;
9951+ }
9952+ au_set_h_dptr(dentry, bindex, NULL);
9953+ }
9954+}
9955+
9956+int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
9957+{
5afbbe0d 9958+ aufs_bindex_t bindex, bbot;
1facf9fc 9959+
5afbbe0d
AM
9960+ bbot = au_dbbot(dentry);
9961+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
1facf9fc 9962+ if (au_h_dptr(dentry, bindex) == h_dentry)
9963+ return bindex;
9964+ return -1;
9965+}
7f207e10 9966diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
eca34b5c 9967--- /usr/share/empty/fs/aufs/dir.c 1970-01-01 01:00:00.000000000 +0100
2255d0fe 9968+++ linux/fs/aufs/dir.c 2022-12-17 09:21:34.796521861 +0100
0b2a12c6 9969@@ -0,0 +1,765 @@
cd7a4cd9 9970+// SPDX-License-Identifier: GPL-2.0
1facf9fc 9971+/*
0dcfbb52 9972+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 9973+ *
0dcfbb52 9974+ * This program is free software; you can redistribute it and/or modify
1facf9fc 9975+ * it under the terms of the GNU General Public License as published by
9976+ * the Free Software Foundation; either version 2 of the License, or
9977+ * (at your option) any later version.
dece6358
AM
9978+ *
9979+ * This program is distributed in the hope that it will be useful,
9980+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9981+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9982+ * GNU General Public License for more details.
9983+ *
9984+ * You should have received a copy of the GNU General Public License
523b37e3 9985+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 9986+ */
9987+
9988+/*
9989+ * directory operations
9990+ */
9991+
9992+#include <linux/fs_stack.h>
eca801bf 9993+#include <linux/iversion.h>
1facf9fc 9994+#include "aufs.h"
9995+
9996+void au_add_nlink(struct inode *dir, struct inode *h_dir)
9997+{
9dbd164d
AM
9998+ unsigned int nlink;
9999+
1facf9fc 10000+ AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10001+
9dbd164d
AM
10002+ nlink = dir->i_nlink;
10003+ nlink += h_dir->i_nlink - 2;
1facf9fc 10004+ if (h_dir->i_nlink < 2)
9dbd164d 10005+ nlink += 2;
f6b6e03d 10006+ smp_mb(); /* for i_nlink */
7eafdf33 10007+ /* 0 can happen in revaliding */
92d182d2 10008+ set_nlink(dir, nlink);
1facf9fc 10009+}
10010+
10011+void au_sub_nlink(struct inode *dir, struct inode *h_dir)
10012+{
9dbd164d
AM
10013+ unsigned int nlink;
10014+
1facf9fc 10015+ AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10016+
9dbd164d
AM
10017+ nlink = dir->i_nlink;
10018+ nlink -= h_dir->i_nlink - 2;
1facf9fc 10019+ if (h_dir->i_nlink < 2)
9dbd164d 10020+ nlink -= 2;
f6b6e03d 10021+ smp_mb(); /* for i_nlink */
92d182d2 10022+ /* nlink == 0 means the branch-fs is broken */
9dbd164d 10023+ set_nlink(dir, nlink);
1facf9fc 10024+}
10025+
1308ab2a 10026+loff_t au_dir_size(struct file *file, struct dentry *dentry)
10027+{
10028+ loff_t sz;
5afbbe0d 10029+ aufs_bindex_t bindex, bbot;
1308ab2a 10030+ struct file *h_file;
10031+ struct dentry *h_dentry;
10032+
10033+ sz = 0;
10034+ if (file) {
2000de60 10035+ AuDebugOn(!d_is_dir(file->f_path.dentry));
1308ab2a 10036+
5afbbe0d
AM
10037+ bbot = au_fbbot_dir(file);
10038+ for (bindex = au_fbtop(file);
10039+ bindex <= bbot && sz < KMALLOC_MAX_SIZE;
1308ab2a 10040+ bindex++) {
4a4d8108 10041+ h_file = au_hf_dir(file, bindex);
c06a8ce3
AM
10042+ if (h_file && file_inode(h_file))
10043+ sz += vfsub_f_size_read(h_file);
1308ab2a 10044+ }
10045+ } else {
10046+ AuDebugOn(!dentry);
2000de60 10047+ AuDebugOn(!d_is_dir(dentry));
1308ab2a 10048+
5afbbe0d
AM
10049+ bbot = au_dbtaildir(dentry);
10050+ for (bindex = au_dbtop(dentry);
10051+ bindex <= bbot && sz < KMALLOC_MAX_SIZE;
1308ab2a 10052+ bindex++) {
10053+ h_dentry = au_h_dptr(dentry, bindex);
5527c038
JR
10054+ if (h_dentry && d_is_positive(h_dentry))
10055+ sz += i_size_read(d_inode(h_dentry));
1308ab2a 10056+ }
10057+ }
10058+ if (sz < KMALLOC_MAX_SIZE)
10059+ sz = roundup_pow_of_two(sz);
10060+ if (sz > KMALLOC_MAX_SIZE)
10061+ sz = KMALLOC_MAX_SIZE;
10062+ else if (sz < NAME_MAX) {
10063+ BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
10064+ sz = AUFS_RDBLK_DEF;
10065+ }
10066+ return sz;
10067+}
10068+
b912730e
AM
10069+struct au_dir_ts_arg {
10070+ struct dentry *dentry;
10071+ aufs_bindex_t brid;
10072+};
10073+
10074+static void au_do_dir_ts(void *arg)
10075+{
10076+ struct au_dir_ts_arg *a = arg;
10077+ struct au_dtime dt;
10078+ struct path h_path;
10079+ struct inode *dir, *h_dir;
10080+ struct super_block *sb;
10081+ struct au_branch *br;
10082+ struct au_hinode *hdir;
10083+ int err;
5afbbe0d 10084+ aufs_bindex_t btop, bindex;
b912730e
AM
10085+
10086+ sb = a->dentry->d_sb;
5527c038 10087+ if (d_really_is_negative(a->dentry))
b912730e 10088+ goto out;
5527c038 10089+ /* no dir->i_mutex lock */
b95c5147
AM
10090+ aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
10091+
5527c038 10092+ dir = d_inode(a->dentry);
5afbbe0d 10093+ btop = au_ibtop(dir);
b912730e 10094+ bindex = au_br_index(sb, a->brid);
5afbbe0d 10095+ if (bindex < btop)
b912730e
AM
10096+ goto out_unlock;
10097+
10098+ br = au_sbr(sb, bindex);
10099+ h_path.dentry = au_h_dptr(a->dentry, bindex);
10100+ if (!h_path.dentry)
10101+ goto out_unlock;
10102+ h_path.mnt = au_br_mnt(br);
10103+ au_dtime_store(&dt, a->dentry, &h_path);
10104+
5afbbe0d 10105+ br = au_sbr(sb, btop);
b912730e
AM
10106+ if (!au_br_writable(br->br_perm))
10107+ goto out_unlock;
5afbbe0d 10108+ h_path.dentry = au_h_dptr(a->dentry, btop);
b912730e
AM
10109+ h_path.mnt = au_br_mnt(br);
10110+ err = vfsub_mnt_want_write(h_path.mnt);
10111+ if (err)
10112+ goto out_unlock;
5afbbe0d
AM
10113+ hdir = au_hi(dir, btop);
10114+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
10115+ h_dir = au_h_iptr(dir, btop);
b912730e 10116+ if (h_dir->i_nlink
cd7a4cd9 10117+ && timespec64_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
b912730e
AM
10118+ dt.dt_h_path = h_path;
10119+ au_dtime_revert(&dt);
10120+ }
5afbbe0d 10121+ au_hn_inode_unlock(hdir);
b912730e
AM
10122+ vfsub_mnt_drop_write(h_path.mnt);
10123+ au_cpup_attr_timesizes(dir);
10124+
10125+out_unlock:
10126+ aufs_read_unlock(a->dentry, AuLock_DW);
10127+out:
10128+ dput(a->dentry);
10129+ au_nwt_done(&au_sbi(sb)->si_nowait);
9f237c51 10130+ au_kfree_try_rcu(arg);
b912730e
AM
10131+}
10132+
10133+void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
10134+{
10135+ int perm, wkq_err;
5afbbe0d 10136+ aufs_bindex_t btop;
b912730e
AM
10137+ struct au_dir_ts_arg *arg;
10138+ struct dentry *dentry;
10139+ struct super_block *sb;
10140+
10141+ IMustLock(dir);
10142+
10143+ dentry = d_find_any_alias(dir);
10144+ AuDebugOn(!dentry);
10145+ sb = dentry->d_sb;
5afbbe0d
AM
10146+ btop = au_ibtop(dir);
10147+ if (btop == bindex) {
b912730e
AM
10148+ au_cpup_attr_timesizes(dir);
10149+ goto out;
10150+ }
10151+
5afbbe0d 10152+ perm = au_sbr_perm(sb, btop);
b912730e
AM
10153+ if (!au_br_writable(perm))
10154+ goto out;
10155+
10156+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
10157+ if (!arg)
10158+ goto out;
10159+
10160+ arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
10161+ arg->brid = au_sbr_id(sb, bindex);
10162+ wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
10163+ if (unlikely(wkq_err)) {
10164+ pr_err("wkq %d\n", wkq_err);
10165+ dput(dentry);
9f237c51 10166+ au_kfree_try_rcu(arg);
b912730e
AM
10167+ }
10168+
10169+out:
10170+ dput(dentry);
10171+}
10172+
1facf9fc 10173+/* ---------------------------------------------------------------------- */
10174+
10175+static int reopen_dir(struct file *file)
10176+{
10177+ int err;
10178+ unsigned int flags;
5afbbe0d 10179+ aufs_bindex_t bindex, btail, btop;
1facf9fc 10180+ struct dentry *dentry, *h_dentry;
10181+ struct file *h_file;
10182+
10183+ /* open all lower dirs */
2000de60 10184+ dentry = file->f_path.dentry;
5afbbe0d
AM
10185+ btop = au_dbtop(dentry);
10186+ for (bindex = au_fbtop(file); bindex < btop; bindex++)
1facf9fc 10187+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d 10188+ au_set_fbtop(file, btop);
1facf9fc 10189+
10190+ btail = au_dbtaildir(dentry);
5afbbe0d 10191+ for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
1facf9fc 10192+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d 10193+ au_set_fbbot_dir(file, btail);
1facf9fc 10194+
4a4d8108 10195+ flags = vfsub_file_flags(file);
5afbbe0d 10196+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 10197+ h_dentry = au_h_dptr(dentry, bindex);
10198+ if (!h_dentry)
10199+ continue;
4a4d8108 10200+ h_file = au_hf_dir(file, bindex);
1facf9fc 10201+ if (h_file)
10202+ continue;
10203+
392086de 10204+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
1facf9fc 10205+ err = PTR_ERR(h_file);
10206+ if (IS_ERR(h_file))
10207+ goto out; /* close all? */
10208+ au_set_h_fptr(file, bindex, h_file);
10209+ }
10210+ au_update_figen(file);
10211+ /* todo: necessary? */
10212+ /* file->f_ra = h_file->f_ra; */
10213+ err = 0;
10214+
4f0767ce 10215+out:
1facf9fc 10216+ return err;
10217+}
10218+
b912730e 10219+static int do_open_dir(struct file *file, int flags, struct file *h_file)
1facf9fc 10220+{
10221+ int err;
10222+ aufs_bindex_t bindex, btail;
10223+ struct dentry *dentry, *h_dentry;
8cdd5066 10224+ struct vfsmount *mnt;
1facf9fc 10225+
1308ab2a 10226+ FiMustWriteLock(file);
b912730e 10227+ AuDebugOn(h_file);
1308ab2a 10228+
523b37e3 10229+ err = 0;
8cdd5066 10230+ mnt = file->f_path.mnt;
2000de60 10231+ dentry = file->f_path.dentry;
be118d29 10232+ file->f_version = inode_query_iversion(d_inode(dentry));
5afbbe0d
AM
10233+ bindex = au_dbtop(dentry);
10234+ au_set_fbtop(file, bindex);
1facf9fc 10235+ btail = au_dbtaildir(dentry);
5afbbe0d 10236+ au_set_fbbot_dir(file, btail);
1facf9fc 10237+ for (; !err && bindex <= btail; bindex++) {
10238+ h_dentry = au_h_dptr(dentry, bindex);
10239+ if (!h_dentry)
10240+ continue;
10241+
8cdd5066
JR
10242+ err = vfsub_test_mntns(mnt, h_dentry->d_sb);
10243+ if (unlikely(err))
10244+ break;
392086de 10245+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
1facf9fc 10246+ if (IS_ERR(h_file)) {
10247+ err = PTR_ERR(h_file);
10248+ break;
10249+ }
10250+ au_set_h_fptr(file, bindex, h_file);
10251+ }
10252+ au_update_figen(file);
10253+ /* todo: necessary? */
10254+ /* file->f_ra = h_file->f_ra; */
10255+ if (!err)
10256+ return 0; /* success */
10257+
10258+ /* close all */
5afbbe0d 10259+ for (bindex = au_fbtop(file); bindex <= btail; bindex++)
1facf9fc 10260+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d
AM
10261+ au_set_fbtop(file, -1);
10262+ au_set_fbbot_dir(file, -1);
4a4d8108 10263+
1facf9fc 10264+ return err;
10265+}
10266+
10267+static int aufs_open_dir(struct inode *inode __maybe_unused,
10268+ struct file *file)
10269+{
4a4d8108
AM
10270+ int err;
10271+ struct super_block *sb;
10272+ struct au_fidir *fidir;
10273+
10274+ err = -ENOMEM;
2000de60 10275+ sb = file->f_path.dentry->d_sb;
4a4d8108 10276+ si_read_lock(sb, AuLock_FLUSH);
e49829fe 10277+ fidir = au_fidir_alloc(sb);
4a4d8108 10278+ if (fidir) {
b912730e
AM
10279+ struct au_do_open_args args = {
10280+ .open = do_open_dir,
10281+ .fidir = fidir
10282+ };
10283+ err = au_do_open(file, &args);
4a4d8108 10284+ if (unlikely(err))
9f237c51 10285+ au_kfree_rcu(fidir);
4a4d8108
AM
10286+ }
10287+ si_read_unlock(sb);
10288+ return err;
1facf9fc 10289+}
10290+
10291+static int aufs_release_dir(struct inode *inode __maybe_unused,
10292+ struct file *file)
10293+{
10294+ struct au_vdir *vdir_cache;
4a4d8108
AM
10295+ struct au_finfo *finfo;
10296+ struct au_fidir *fidir;
f0c0a007 10297+ struct au_hfile *hf;
5afbbe0d 10298+ aufs_bindex_t bindex, bbot;
1facf9fc 10299+
4a4d8108
AM
10300+ finfo = au_fi(file);
10301+ fidir = finfo->fi_hdir;
10302+ if (fidir) {
8b6a4947
AM
10303+ au_hbl_del(&finfo->fi_hlist,
10304+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
4a4d8108
AM
10305+ vdir_cache = fidir->fd_vdir_cache; /* lock-free */
10306+ if (vdir_cache)
1c60b727 10307+ au_vdir_free(vdir_cache);
4a4d8108
AM
10308+
10309+ bindex = finfo->fi_btop;
10310+ if (bindex >= 0) {
f0c0a007 10311+ hf = fidir->fd_hfile + bindex;
4a4d8108
AM
10312+ /*
10313+ * calls fput() instead of filp_close(),
10314+ * since no dnotify or lock for the lower file.
10315+ */
5afbbe0d 10316+ bbot = fidir->fd_bbot;
f0c0a007
AM
10317+ for (; bindex <= bbot; bindex++, hf++)
10318+ if (hf->hf_file)
1c60b727 10319+ au_hfput(hf, /*execed*/0);
4a4d8108 10320+ }
9f237c51 10321+ au_kfree_rcu(fidir);
4a4d8108 10322+ finfo->fi_hdir = NULL;
1facf9fc 10323+ }
1c60b727 10324+ au_finfo_fin(file);
1facf9fc 10325+ return 0;
10326+}
10327+
10328+/* ---------------------------------------------------------------------- */
10329+
4a4d8108
AM
10330+static int au_do_flush_dir(struct file *file, fl_owner_t id)
10331+{
10332+ int err;
5afbbe0d 10333+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
10334+ struct file *h_file;
10335+
10336+ err = 0;
5afbbe0d
AM
10337+ bbot = au_fbbot_dir(file);
10338+ for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
4a4d8108
AM
10339+ h_file = au_hf_dir(file, bindex);
10340+ if (h_file)
10341+ err = vfsub_flush(h_file, id);
10342+ }
10343+ return err;
10344+}
10345+
10346+static int aufs_flush_dir(struct file *file, fl_owner_t id)
10347+{
10348+ return au_do_flush(file, id, au_do_flush_dir);
10349+}
10350+
10351+/* ---------------------------------------------------------------------- */
10352+
1facf9fc 10353+static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
10354+{
10355+ int err;
5afbbe0d 10356+ aufs_bindex_t bbot, bindex;
1facf9fc 10357+ struct inode *inode;
10358+ struct super_block *sb;
10359+
10360+ err = 0;
10361+ sb = dentry->d_sb;
5527c038 10362+ inode = d_inode(dentry);
1facf9fc 10363+ IMustLock(inode);
5afbbe0d
AM
10364+ bbot = au_dbbot(dentry);
10365+ for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
1facf9fc 10366+ struct path h_path;
1facf9fc 10367+
10368+ if (au_test_ro(sb, bindex, inode))
10369+ continue;
10370+ h_path.dentry = au_h_dptr(dentry, bindex);
10371+ if (!h_path.dentry)
10372+ continue;
1facf9fc 10373+
1facf9fc 10374+ h_path.mnt = au_sbr_mnt(sb, bindex);
53392da6 10375+ err = vfsub_fsync(NULL, &h_path, datasync);
1facf9fc 10376+ }
10377+
10378+ return err;
10379+}
10380+
10381+static int au_do_fsync_dir(struct file *file, int datasync)
10382+{
10383+ int err;
5afbbe0d 10384+ aufs_bindex_t bbot, bindex;
1facf9fc 10385+ struct file *h_file;
10386+ struct super_block *sb;
10387+ struct inode *inode;
1facf9fc 10388+
521ced18 10389+ err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
1facf9fc 10390+ if (unlikely(err))
10391+ goto out;
10392+
c06a8ce3 10393+ inode = file_inode(file);
b912730e 10394+ sb = inode->i_sb;
5afbbe0d
AM
10395+ bbot = au_fbbot_dir(file);
10396+ for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
4a4d8108 10397+ h_file = au_hf_dir(file, bindex);
1facf9fc 10398+ if (!h_file || au_test_ro(sb, bindex, inode))
10399+ continue;
10400+
53392da6 10401+ err = vfsub_fsync(h_file, &h_file->f_path, datasync);
1facf9fc 10402+ }
10403+
4f0767ce 10404+out:
1facf9fc 10405+ return err;
10406+}
10407+
10408+/*
10409+ * @file may be NULL
10410+ */
1e00d052
AM
10411+static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
10412+ int datasync)
1facf9fc 10413+{
10414+ int err;
b752ccd1 10415+ struct dentry *dentry;
5527c038 10416+ struct inode *inode;
1facf9fc 10417+ struct super_block *sb;
1facf9fc 10418+
10419+ err = 0;
2000de60 10420+ dentry = file->f_path.dentry;
5527c038 10421+ inode = d_inode(dentry);
febd17d6 10422+ inode_lock(inode);
1facf9fc 10423+ sb = dentry->d_sb;
10424+ si_noflush_read_lock(sb);
10425+ if (file)
10426+ err = au_do_fsync_dir(file, datasync);
10427+ else {
10428+ di_write_lock_child(dentry);
10429+ err = au_do_fsync_dir_no_file(dentry, datasync);
10430+ }
5527c038 10431+ au_cpup_attr_timesizes(inode);
1facf9fc 10432+ di_write_unlock(dentry);
10433+ if (file)
10434+ fi_write_unlock(file);
10435+
10436+ si_read_unlock(sb);
febd17d6 10437+ inode_unlock(inode);
1facf9fc 10438+ return err;
10439+}
10440+
10441+/* ---------------------------------------------------------------------- */
10442+
5afbbe0d 10443+static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
1facf9fc 10444+{
10445+ int err;
10446+ struct dentry *dentry;
9dbd164d 10447+ struct inode *inode, *h_inode;
1facf9fc 10448+ struct super_block *sb;
10449+
062440b3 10450+ AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
392086de 10451+
2000de60 10452+ dentry = file->f_path.dentry;
5527c038 10453+ inode = d_inode(dentry);
1facf9fc 10454+ IMustLock(inode);
10455+
10456+ sb = dentry->d_sb;
10457+ si_read_lock(sb, AuLock_FLUSH);
521ced18 10458+ err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
1facf9fc 10459+ if (unlikely(err))
10460+ goto out;
027c5e7a
AM
10461+ err = au_alive_dir(dentry);
10462+ if (!err)
10463+ err = au_vdir_init(file);
1facf9fc 10464+ di_downgrade_lock(dentry, AuLock_IR);
10465+ if (unlikely(err))
10466+ goto out_unlock;
10467+
5afbbe0d 10468+ h_inode = au_h_iptr(inode, au_ibtop(inode));
b752ccd1 10469+ if (!au_test_nfsd()) {
392086de 10470+ err = au_vdir_fill_de(file, ctx);
9dbd164d 10471+ fsstack_copy_attr_atime(inode, h_inode);
1facf9fc 10472+ } else {
10473+ /*
10474+ * nfsd filldir may call lookup_one_len(), vfs_getattr(),
10475+ * encode_fh() and others.
10476+ */
9dbd164d 10477+ atomic_inc(&h_inode->i_count);
1facf9fc 10478+ di_read_unlock(dentry, AuLock_IR);
10479+ si_read_unlock(sb);
392086de 10480+ err = au_vdir_fill_de(file, ctx);
1facf9fc 10481+ fsstack_copy_attr_atime(inode, h_inode);
10482+ fi_write_unlock(file);
9dbd164d 10483+ iput(h_inode);
1facf9fc 10484+
10485+ AuTraceErr(err);
10486+ return err;
10487+ }
10488+
4f0767ce 10489+out_unlock:
1facf9fc 10490+ di_read_unlock(dentry, AuLock_IR);
10491+ fi_write_unlock(file);
4f0767ce 10492+out:
1facf9fc 10493+ si_read_unlock(sb);
10494+ return err;
10495+}
10496+
10497+/* ---------------------------------------------------------------------- */
10498+
10499+#define AuTestEmpty_WHONLY 1
dece6358
AM
10500+#define AuTestEmpty_CALLED (1 << 1)
10501+#define AuTestEmpty_SHWH (1 << 2)
1facf9fc 10502+#define au_ftest_testempty(flags, name) ((flags) & AuTestEmpty_##name)
7f207e10
AM
10503+#define au_fset_testempty(flags, name) \
10504+ do { (flags) |= AuTestEmpty_##name; } while (0)
10505+#define au_fclr_testempty(flags, name) \
10506+ do { (flags) &= ~AuTestEmpty_##name; } while (0)
1facf9fc 10507+
dece6358
AM
10508+#ifndef CONFIG_AUFS_SHWH
10509+#undef AuTestEmpty_SHWH
10510+#define AuTestEmpty_SHWH 0
10511+#endif
10512+
1facf9fc 10513+struct test_empty_arg {
392086de 10514+ struct dir_context ctx;
1308ab2a 10515+ struct au_nhash *whlist;
1facf9fc 10516+ unsigned int flags;
10517+ int err;
10518+ aufs_bindex_t bindex;
10519+};
10520+
2255d0fe
AM
10521+static bool test_empty_cb(struct dir_context *ctx, const char *__name,
10522+ int namelen, loff_t offset __maybe_unused, u64 ino,
10523+ unsigned int d_type)
1facf9fc 10524+{
392086de
AM
10525+ struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10526+ ctx);
1facf9fc 10527+ char *name = (void *)__name;
10528+
10529+ arg->err = 0;
10530+ au_fset_testempty(arg->flags, CALLED);
10531+ /* smp_mb(); */
10532+ if (name[0] == '.'
10533+ && (namelen == 1 || (name[1] == '.' && namelen == 2)))
10534+ goto out; /* success */
10535+
10536+ if (namelen <= AUFS_WH_PFX_LEN
10537+ || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10538+ if (au_ftest_testempty(arg->flags, WHONLY)
1308ab2a 10539+ && !au_nhash_test_known_wh(arg->whlist, name, namelen))
1facf9fc 10540+ arg->err = -ENOTEMPTY;
10541+ goto out;
10542+ }
10543+
10544+ name += AUFS_WH_PFX_LEN;
10545+ namelen -= AUFS_WH_PFX_LEN;
1308ab2a 10546+ if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
1facf9fc 10547+ arg->err = au_nhash_append_wh
1308ab2a 10548+ (arg->whlist, name, namelen, ino, d_type, arg->bindex,
dece6358 10549+ au_ftest_testempty(arg->flags, SHWH));
1facf9fc 10550+
4f0767ce 10551+out:
1facf9fc 10552+ /* smp_mb(); */
10553+ AuTraceErr(arg->err);
2255d0fe 10554+ return !arg->err;
1facf9fc 10555+}
10556+
10557+static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10558+{
10559+ int err;
10560+ struct file *h_file;
acd2b654 10561+ struct au_branch *br;
1facf9fc 10562+
10563+ h_file = au_h_open(dentry, arg->bindex,
10564+ O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
392086de 10565+ /*file*/NULL, /*force_wr*/0);
1facf9fc 10566+ err = PTR_ERR(h_file);
10567+ if (IS_ERR(h_file))
10568+ goto out;
10569+
10570+ err = 0;
10571+ if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
c06a8ce3 10572+ && !file_inode(h_file)->i_nlink)
1facf9fc 10573+ goto out_put;
10574+
10575+ do {
10576+ arg->err = 0;
10577+ au_fclr_testempty(arg->flags, CALLED);
10578+ /* smp_mb(); */
392086de 10579+ err = vfsub_iterate_dir(h_file, &arg->ctx);
1facf9fc 10580+ if (err >= 0)
10581+ err = arg->err;
10582+ } while (!err && au_ftest_testempty(arg->flags, CALLED));
10583+
4f0767ce 10584+out_put:
1facf9fc 10585+ fput(h_file);
acd2b654
AM
10586+ br = au_sbr(dentry->d_sb, arg->bindex);
10587+ au_lcnt_dec(&br->br_nfiles);
4f0767ce 10588+out:
1facf9fc 10589+ return err;
10590+}
10591+
10592+struct do_test_empty_args {
10593+ int *errp;
10594+ struct dentry *dentry;
10595+ struct test_empty_arg *arg;
10596+};
10597+
10598+static void call_do_test_empty(void *args)
10599+{
10600+ struct do_test_empty_args *a = args;
10601+ *a->errp = do_test_empty(a->dentry, a->arg);
10602+}
10603+
10604+static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10605+{
10606+ int err, wkq_err;
10607+ struct dentry *h_dentry;
10608+ struct inode *h_inode;
0b2a12c6 10609+ struct user_namespace *h_userns;
1facf9fc 10610+
0b2a12c6 10611+ h_userns = au_sbr_userns(dentry->d_sb, arg->bindex);
1facf9fc 10612+ h_dentry = au_h_dptr(dentry, arg->bindex);
5527c038 10613+ h_inode = d_inode(h_dentry);
53392da6 10614+ /* todo: i_mode changes anytime? */
be118d29 10615+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
0b2a12c6 10616+ err = au_test_h_perm_sio(h_userns, h_inode, MAY_EXEC | MAY_READ);
3c1bdaff 10617+ inode_unlock_shared(h_inode);
1facf9fc 10618+ if (!err)
10619+ err = do_test_empty(dentry, arg);
10620+ else {
10621+ struct do_test_empty_args args = {
10622+ .errp = &err,
10623+ .dentry = dentry,
10624+ .arg = arg
10625+ };
10626+ unsigned int flags = arg->flags;
10627+
10628+ wkq_err = au_wkq_wait(call_do_test_empty, &args);
10629+ if (unlikely(wkq_err))
10630+ err = wkq_err;
10631+ arg->flags = flags;
10632+ }
10633+
10634+ return err;
10635+}
10636+
10637+int au_test_empty_lower(struct dentry *dentry)
10638+{
10639+ int err;
1308ab2a 10640+ unsigned int rdhash;
5afbbe0d 10641+ aufs_bindex_t bindex, btop, btail;
1308ab2a 10642+ struct au_nhash whlist;
392086de
AM
10643+ struct test_empty_arg arg = {
10644+ .ctx = {
2000de60 10645+ .actor = test_empty_cb
392086de
AM
10646+ }
10647+ };
076b876e 10648+ int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
1facf9fc 10649+
dece6358
AM
10650+ SiMustAnyLock(dentry->d_sb);
10651+
1308ab2a 10652+ rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10653+ if (!rdhash)
10654+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10655+ err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
dece6358 10656+ if (unlikely(err))
1facf9fc 10657+ goto out;
10658+
1facf9fc 10659+ arg.flags = 0;
1308ab2a 10660+ arg.whlist = &whlist;
5afbbe0d 10661+ btop = au_dbtop(dentry);
dece6358
AM
10662+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10663+ au_fset_testempty(arg.flags, SHWH);
076b876e
AM
10664+ test_empty = do_test_empty;
10665+ if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10666+ test_empty = sio_test_empty;
5afbbe0d 10667+ arg.bindex = btop;
076b876e 10668+ err = test_empty(dentry, &arg);
1facf9fc 10669+ if (unlikely(err))
10670+ goto out_whlist;
10671+
10672+ au_fset_testempty(arg.flags, WHONLY);
10673+ btail = au_dbtaildir(dentry);
5afbbe0d 10674+ for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
1facf9fc 10675+ struct dentry *h_dentry;
10676+
10677+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 10678+ if (h_dentry && d_is_positive(h_dentry)) {
1facf9fc 10679+ arg.bindex = bindex;
076b876e 10680+ err = test_empty(dentry, &arg);
1facf9fc 10681+ }
10682+ }
10683+
4f0767ce 10684+out_whlist:
1308ab2a 10685+ au_nhash_wh_free(&whlist);
4f0767ce 10686+out:
1facf9fc 10687+ return err;
10688+}
10689+
10690+int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10691+{
10692+ int err;
392086de
AM
10693+ struct test_empty_arg arg = {
10694+ .ctx = {
2000de60 10695+ .actor = test_empty_cb
392086de
AM
10696+ }
10697+ };
1facf9fc 10698+ aufs_bindex_t bindex, btail;
10699+
10700+ err = 0;
1308ab2a 10701+ arg.whlist = whlist;
1facf9fc 10702+ arg.flags = AuTestEmpty_WHONLY;
dece6358
AM
10703+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10704+ au_fset_testempty(arg.flags, SHWH);
1facf9fc 10705+ btail = au_dbtaildir(dentry);
5afbbe0d 10706+ for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
1facf9fc 10707+ struct dentry *h_dentry;
10708+
10709+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 10710+ if (h_dentry && d_is_positive(h_dentry)) {
1facf9fc 10711+ arg.bindex = bindex;
10712+ err = sio_test_empty(dentry, &arg);
10713+ }
10714+ }
10715+
10716+ return err;
10717+}
10718+
10719+/* ---------------------------------------------------------------------- */
10720+
10721+const struct file_operations aufs_dir_fop = {
4a4d8108 10722+ .owner = THIS_MODULE,
027c5e7a 10723+ .llseek = default_llseek,
1facf9fc 10724+ .read = generic_read_dir,
5afbbe0d 10725+ .iterate_shared = aufs_iterate_shared,
1facf9fc 10726+ .unlocked_ioctl = aufs_ioctl_dir,
b752ccd1
AM
10727+#ifdef CONFIG_COMPAT
10728+ .compat_ioctl = aufs_compat_ioctl_dir,
10729+#endif
1facf9fc 10730+ .open = aufs_open_dir,
10731+ .release = aufs_release_dir,
4a4d8108 10732+ .flush = aufs_flush_dir,
1facf9fc 10733+ .fsync = aufs_fsync_dir
10734+};
7f207e10 10735diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
eca34b5c 10736--- /usr/share/empty/fs/aufs/dir.h 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 10737+++ linux/fs/aufs/dir.h 2022-11-05 23:02:18.962555950 +0100
9f237c51 10738@@ -0,0 +1,134 @@
062440b3 10739+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 10740+/*
0dcfbb52 10741+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 10742+ *
0dcfbb52 10743+ * This program is free software; you can redistribute it and/or modify
1facf9fc 10744+ * it under the terms of the GNU General Public License as published by
10745+ * the Free Software Foundation; either version 2 of the License, or
10746+ * (at your option) any later version.
dece6358
AM
10747+ *
10748+ * This program is distributed in the hope that it will be useful,
10749+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10750+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10751+ * GNU General Public License for more details.
10752+ *
10753+ * You should have received a copy of the GNU General Public License
523b37e3 10754+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 10755+ */
10756+
10757+/*
10758+ * directory operations
10759+ */
10760+
10761+#ifndef __AUFS_DIR_H__
10762+#define __AUFS_DIR_H__
10763+
10764+#ifdef __KERNEL__
10765+
10766+#include <linux/fs.h>
1facf9fc 10767+
10768+/* ---------------------------------------------------------------------- */
10769+
10770+/* need to be faster and smaller */
10771+
10772+struct au_nhash {
dece6358
AM
10773+ unsigned int nh_num;
10774+ struct hlist_head *nh_head;
1facf9fc 10775+};
10776+
10777+struct au_vdir_destr {
10778+ unsigned char len;
42b5c33a 10779+ unsigned char name[];
1facf9fc 10780+} __packed;
10781+
10782+struct au_vdir_dehstr {
10783+ struct hlist_node hash;
1c60b727 10784+ struct au_vdir_destr *str;
9f237c51 10785+ struct rcu_head rcu;
4a4d8108 10786+} ____cacheline_aligned_in_smp;
1facf9fc 10787+
10788+struct au_vdir_de {
10789+ ino_t de_ino;
10790+ unsigned char de_type;
10791+ /* caution: packed */
10792+ struct au_vdir_destr de_str;
10793+} __packed;
10794+
10795+struct au_vdir_wh {
10796+ struct hlist_node wh_hash;
dece6358
AM
10797+#ifdef CONFIG_AUFS_SHWH
10798+ ino_t wh_ino;
1facf9fc 10799+ aufs_bindex_t wh_bindex;
dece6358
AM
10800+ unsigned char wh_type;
10801+#else
10802+ aufs_bindex_t wh_bindex;
10803+#endif
10804+ /* caution: packed */
1facf9fc 10805+ struct au_vdir_destr wh_str;
10806+} __packed;
10807+
10808+union au_vdir_deblk_p {
10809+ unsigned char *deblk;
10810+ struct au_vdir_de *de;
10811+};
10812+
10813+struct au_vdir {
10814+ unsigned char **vd_deblk;
10815+ unsigned long vd_nblk;
1facf9fc 10816+ struct {
10817+ unsigned long ul;
10818+ union au_vdir_deblk_p p;
10819+ } vd_last;
10820+
be118d29 10821+ u64 vd_version;
dece6358 10822+ unsigned int vd_deblk_sz;
9f237c51
AM
10823+ unsigned long vd_jiffy;
10824+ struct rcu_head rcu;
4a4d8108 10825+} ____cacheline_aligned_in_smp;
1facf9fc 10826+
10827+/* ---------------------------------------------------------------------- */
10828+
10829+/* dir.c */
10830+extern const struct file_operations aufs_dir_fop;
10831+void au_add_nlink(struct inode *dir, struct inode *h_dir);
10832+void au_sub_nlink(struct inode *dir, struct inode *h_dir);
1308ab2a 10833+loff_t au_dir_size(struct file *file, struct dentry *dentry);
b912730e 10834+void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
1facf9fc 10835+int au_test_empty_lower(struct dentry *dentry);
10836+int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10837+
10838+/* vdir.c */
1308ab2a 10839+unsigned int au_rdhash_est(loff_t sz);
dece6358
AM
10840+int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
10841+void au_nhash_wh_free(struct au_nhash *whlist);
1facf9fc 10842+int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
10843+ int limit);
dece6358
AM
10844+int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
10845+int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
10846+ unsigned int d_type, aufs_bindex_t bindex,
10847+ unsigned char shwh);
1c60b727 10848+void au_vdir_free(struct au_vdir *vdir);
1facf9fc 10849+int au_vdir_init(struct file *file);
392086de 10850+int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
1facf9fc 10851+
10852+/* ioctl.c */
10853+long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
10854+
1308ab2a 10855+#ifdef CONFIG_AUFS_RDU
10856+/* rdu.c */
10857+long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
b752ccd1
AM
10858+#ifdef CONFIG_COMPAT
10859+long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
10860+ unsigned long arg);
10861+#endif
1308ab2a 10862+#else
c1595e42
JR
10863+AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
10864+ unsigned int cmd, unsigned long arg)
b752ccd1 10865+#ifdef CONFIG_COMPAT
c1595e42
JR
10866+AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
10867+ unsigned int cmd, unsigned long arg)
b752ccd1 10868+#endif
1308ab2a 10869+#endif
10870+
1facf9fc 10871+#endif /* __KERNEL__ */
10872+#endif /* __AUFS_DIR_H__ */
8b6a4947 10873diff -urN /usr/share/empty/fs/aufs/dirren.c linux/fs/aufs/dirren.c
eca34b5c 10874--- /usr/share/empty/fs/aufs/dirren.c 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 10875+++ linux/fs/aufs/dirren.c 2022-11-05 23:02:18.962555950 +0100
758e9dad 10876@@ -0,0 +1,1315 @@
cd7a4cd9 10877+// SPDX-License-Identifier: GPL-2.0
8b6a4947 10878+/*
0dcfbb52 10879+ * Copyright (C) 2017-2022 Junjiro R. Okajima
8b6a4947 10880+ *
0dcfbb52 10881+ * This program is free software; you can redistribute it and/or modify
8b6a4947
AM
10882+ * it under the terms of the GNU General Public License as published by
10883+ * the Free Software Foundation; either version 2 of the License, or
10884+ * (at your option) any later version.
10885+ *
10886+ * This program is distributed in the hope that it will be useful,
10887+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10888+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10889+ * GNU General Public License for more details.
10890+ *
10891+ * You should have received a copy of the GNU General Public License
10892+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
10893+ */
10894+
10895+/*
acd2b654 10896+ * special handling in renaming a directory
8b6a4947
AM
10897+ * in order to support looking-up the before-renamed name on the lower readonly
10898+ * branches
10899+ */
10900+
10901+#include <linux/byteorder/generic.h>
10902+#include "aufs.h"
10903+
10904+static void au_dr_hino_del(struct au_dr_br *dr, struct au_dr_hino *ent)
10905+{
10906+ int idx;
10907+
10908+ idx = au_dr_ihash(ent->dr_h_ino);
10909+ au_hbl_del(&ent->dr_hnode, dr->dr_h_ino + idx);
10910+}
10911+
10912+static int au_dr_hino_test_empty(struct au_dr_br *dr)
10913+{
10914+ int ret, i;
10915+ struct hlist_bl_head *hbl;
10916+
10917+ ret = 1;
10918+ for (i = 0; ret && i < AuDirren_NHASH; i++) {
10919+ hbl = dr->dr_h_ino + i;
10920+ hlist_bl_lock(hbl);
10921+ ret &= hlist_bl_empty(hbl);
10922+ hlist_bl_unlock(hbl);
10923+ }
10924+
10925+ return ret;
10926+}
10927+
10928+static struct au_dr_hino *au_dr_hino_find(struct au_dr_br *dr, ino_t ino)
10929+{
10930+ struct au_dr_hino *found, *ent;
10931+ struct hlist_bl_head *hbl;
10932+ struct hlist_bl_node *pos;
10933+ int idx;
10934+
10935+ found = NULL;
10936+ idx = au_dr_ihash(ino);
10937+ hbl = dr->dr_h_ino + idx;
10938+ hlist_bl_lock(hbl);
10939+ hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10940+ if (ent->dr_h_ino == ino) {
10941+ found = ent;
10942+ break;
10943+ }
10944+ hlist_bl_unlock(hbl);
10945+
10946+ return found;
10947+}
10948+
10949+int au_dr_hino_test_add(struct au_dr_br *dr, ino_t ino,
10950+ struct au_dr_hino *add_ent)
10951+{
10952+ int found, idx;
10953+ struct hlist_bl_head *hbl;
10954+ struct hlist_bl_node *pos;
10955+ struct au_dr_hino *ent;
10956+
10957+ found = 0;
10958+ idx = au_dr_ihash(ino);
10959+ hbl = dr->dr_h_ino + idx;
43982f53 10960+#if 0 /* debug print */
8b6a4947
AM
10961+ {
10962+ struct hlist_bl_node *tmp;
10963+
10964+ hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
10965+ AuDbg("hi%llu\n", (unsigned long long)ent->dr_h_ino);
10966+ }
10967+#endif
10968+ hlist_bl_lock(hbl);
10969+ hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10970+ if (ent->dr_h_ino == ino) {
10971+ found = 1;
10972+ break;
10973+ }
10974+ if (!found && add_ent)
10975+ hlist_bl_add_head(&add_ent->dr_hnode, hbl);
10976+ hlist_bl_unlock(hbl);
10977+
10978+ if (!found && add_ent)
10979+ AuDbg("i%llu added\n", (unsigned long long)add_ent->dr_h_ino);
10980+
10981+ return found;
10982+}
10983+
10984+void au_dr_hino_free(struct au_dr_br *dr)
10985+{
10986+ int i;
10987+ struct hlist_bl_head *hbl;
10988+ struct hlist_bl_node *pos, *tmp;
10989+ struct au_dr_hino *ent;
10990+
10991+ /* SiMustWriteLock(sb); */
10992+
10993+ for (i = 0; i < AuDirren_NHASH; i++) {
10994+ hbl = dr->dr_h_ino + i;
10995+ /* no spinlock since sbinfo must be write-locked */
10996+ hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
9f237c51 10997+ au_kfree_rcu(ent);
8b6a4947
AM
10998+ INIT_HLIST_BL_HEAD(hbl);
10999+ }
11000+}
11001+
11002+/* returns the number of inodes or an error */
11003+static int au_dr_hino_store(struct super_block *sb, struct au_branch *br,
11004+ struct file *hinofile)
11005+{
11006+ int err, i;
11007+ ssize_t ssz;
11008+ loff_t pos, oldsize;
11009+ __be64 u64;
11010+ struct inode *hinoinode;
11011+ struct hlist_bl_head *hbl;
11012+ struct hlist_bl_node *n1, *n2;
11013+ struct au_dr_hino *ent;
11014+
11015+ SiMustWriteLock(sb);
11016+ AuDebugOn(!au_br_writable(br->br_perm));
11017+
11018+ hinoinode = file_inode(hinofile);
11019+ oldsize = i_size_read(hinoinode);
11020+
11021+ err = 0;
11022+ pos = 0;
11023+ hbl = br->br_dirren.dr_h_ino;
11024+ for (i = 0; !err && i < AuDirren_NHASH; i++, hbl++) {
11025+ /* no bit-lock since sbinfo must be write-locked */
11026+ hlist_bl_for_each_entry_safe(ent, n1, n2, hbl, dr_hnode) {
11027+ AuDbg("hi%llu, %pD2\n",
11028+ (unsigned long long)ent->dr_h_ino, hinofile);
11029+ u64 = cpu_to_be64(ent->dr_h_ino);
11030+ ssz = vfsub_write_k(hinofile, &u64, sizeof(u64), &pos);
11031+ if (ssz == sizeof(u64))
11032+ continue;
11033+
11034+ /* write error */
11035+ pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11036+ err = -ENOSPC;
11037+ if (ssz < 0)
11038+ err = ssz;
11039+ break;
11040+ }
11041+ }
11042+ /* regardless the error */
11043+ if (pos < oldsize) {
11044+ err = vfsub_trunc(&hinofile->f_path, pos, /*attr*/0, hinofile);
11045+ AuTraceErr(err);
11046+ }
11047+
11048+ AuTraceErr(err);
11049+ return err;
11050+}
11051+
11052+static int au_dr_hino_load(struct au_dr_br *dr, struct file *hinofile)
11053+{
11054+ int err, hidx;
11055+ ssize_t ssz;
11056+ size_t sz, n;
11057+ loff_t pos;
11058+ uint64_t u64;
11059+ struct au_dr_hino *ent;
11060+ struct inode *hinoinode;
11061+ struct hlist_bl_head *hbl;
11062+
11063+ err = 0;
11064+ pos = 0;
11065+ hbl = dr->dr_h_ino;
11066+ hinoinode = file_inode(hinofile);
11067+ sz = i_size_read(hinoinode);
11068+ AuDebugOn(sz % sizeof(u64));
11069+ n = sz / sizeof(u64);
11070+ while (n--) {
11071+ ssz = vfsub_read_k(hinofile, &u64, sizeof(u64), &pos);
11072+ if (unlikely(ssz != sizeof(u64))) {
11073+ pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11074+ err = -EINVAL;
11075+ if (ssz < 0)
11076+ err = ssz;
11077+ goto out_free;
11078+ }
11079+
11080+ ent = kmalloc(sizeof(*ent), GFP_NOFS);
11081+ if (!ent) {
11082+ err = -ENOMEM;
11083+ AuTraceErr(err);
11084+ goto out_free;
11085+ }
11086+ ent->dr_h_ino = be64_to_cpu((__force __be64)u64);
11087+ AuDbg("hi%llu, %pD2\n",
11088+ (unsigned long long)ent->dr_h_ino, hinofile);
11089+ hidx = au_dr_ihash(ent->dr_h_ino);
11090+ au_hbl_add(&ent->dr_hnode, hbl + hidx);
11091+ }
11092+ goto out; /* success */
11093+
11094+out_free:
11095+ au_dr_hino_free(dr);
11096+out:
11097+ AuTraceErr(err);
11098+ return err;
11099+}
11100+
11101+/*
11102+ * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
11103+ * @path is a switch to distinguish load and store.
11104+ */
11105+static int au_dr_hino(struct super_block *sb, aufs_bindex_t bindex,
11106+ struct au_branch *br, const struct path *path)
11107+{
11108+ int err, flags;
11109+ unsigned char load, suspend;
11110+ struct file *hinofile;
11111+ struct au_hinode *hdir;
11112+ struct inode *dir, *delegated;
11113+ struct path hinopath;
11114+ struct qstr hinoname = QSTR_INIT(AUFS_WH_DR_BRHINO,
11115+ sizeof(AUFS_WH_DR_BRHINO) - 1);
11116+
11117+ AuDebugOn(bindex < 0 && !br);
11118+ AuDebugOn(bindex >= 0 && br);
11119+
11120+ err = -EINVAL;
11121+ suspend = !br;
11122+ if (suspend)
11123+ br = au_sbr(sb, bindex);
11124+ load = !!path;
11125+ if (!load) {
11126+ path = &br->br_path;
11127+ AuDebugOn(!au_br_writable(br->br_perm));
11128+ if (unlikely(!au_br_writable(br->br_perm)))
11129+ goto out;
11130+ }
11131+
11132+ hdir = NULL;
11133+ if (suspend) {
11134+ dir = d_inode(sb->s_root);
11135+ hdir = au_hinode(au_ii(dir), bindex);
11136+ dir = hdir->hi_inode;
11137+ au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11138+ } else {
11139+ dir = d_inode(path->dentry);
11140+ inode_lock_nested(dir, AuLsc_I_CHILD);
11141+ }
758e9dad
AM
11142+ hinopath.mnt = path->mnt;
11143+ hinopath.dentry = vfsub_lkup_one(&hinoname, (struct path *)path);
8b6a4947
AM
11144+ err = PTR_ERR(hinopath.dentry);
11145+ if (IS_ERR(hinopath.dentry))
11146+ goto out_unlock;
11147+
11148+ err = 0;
11149+ flags = O_RDONLY;
11150+ if (load) {
11151+ if (d_is_negative(hinopath.dentry))
11152+ goto out_dput; /* success */
11153+ } else {
11154+ if (au_dr_hino_test_empty(&br->br_dirren)) {
11155+ if (d_is_positive(hinopath.dentry)) {
11156+ delegated = NULL;
11157+ err = vfsub_unlink(dir, &hinopath, &delegated,
11158+ /*force*/0);
11159+ AuTraceErr(err);
11160+ if (unlikely(err))
11161+ pr_err("ignored err %d, %pd2\n",
11162+ err, hinopath.dentry);
11163+ if (unlikely(err == -EWOULDBLOCK))
11164+ iput(delegated);
11165+ err = 0;
11166+ }
11167+ goto out_dput;
11168+ } else if (!d_is_positive(hinopath.dentry)) {
11169+ err = vfsub_create(dir, &hinopath, 0600,
11170+ /*want_excl*/false);
11171+ AuTraceErr(err);
11172+ if (unlikely(err))
11173+ goto out_dput;
11174+ }
11175+ flags = O_WRONLY;
11176+ }
8b6a4947
AM
11177+ hinofile = vfsub_dentry_open(&hinopath, flags);
11178+ if (suspend)
11179+ au_hn_inode_unlock(hdir);
11180+ else
11181+ inode_unlock(dir);
11182+ dput(hinopath.dentry);
11183+ AuTraceErrPtr(hinofile);
11184+ if (IS_ERR(hinofile)) {
11185+ err = PTR_ERR(hinofile);
11186+ goto out;
11187+ }
11188+
11189+ if (load)
11190+ err = au_dr_hino_load(&br->br_dirren, hinofile);
11191+ else
11192+ err = au_dr_hino_store(sb, br, hinofile);
11193+ fput(hinofile);
11194+ goto out;
11195+
11196+out_dput:
11197+ dput(hinopath.dentry);
11198+out_unlock:
11199+ if (suspend)
11200+ au_hn_inode_unlock(hdir);
11201+ else
11202+ inode_unlock(dir);
11203+out:
11204+ AuTraceErr(err);
11205+ return err;
11206+}
11207+
11208+/* ---------------------------------------------------------------------- */
11209+
11210+static int au_dr_brid_init(struct au_dr_brid *brid, const struct path *path)
11211+{
11212+ int err;
11213+ struct kstatfs kstfs;
11214+ dev_t dev;
11215+ struct dentry *dentry;
11216+ struct super_block *sb;
11217+
11218+ err = vfs_statfs((void *)path, &kstfs);
11219+ AuTraceErr(err);
11220+ if (unlikely(err))
11221+ goto out;
11222+
11223+ /* todo: support for UUID */
11224+
11225+ if (kstfs.f_fsid.val[0] || kstfs.f_fsid.val[1]) {
11226+ brid->type = AuBrid_FSID;
11227+ brid->fsid = kstfs.f_fsid;
11228+ } else {
11229+ dentry = path->dentry;
11230+ sb = dentry->d_sb;
11231+ dev = sb->s_dev;
11232+ if (dev) {
11233+ brid->type = AuBrid_DEV;
11234+ brid->dev = dev;
11235+ }
11236+ }
11237+
11238+out:
11239+ return err;
11240+}
11241+
11242+int au_dr_br_init(struct super_block *sb, struct au_branch *br,
11243+ const struct path *path)
11244+{
11245+ int err, i;
11246+ struct au_dr_br *dr;
11247+ struct hlist_bl_head *hbl;
11248+
11249+ dr = &br->br_dirren;
11250+ hbl = dr->dr_h_ino;
11251+ for (i = 0; i < AuDirren_NHASH; i++, hbl++)
11252+ INIT_HLIST_BL_HEAD(hbl);
11253+
11254+ err = au_dr_brid_init(&dr->dr_brid, path);
11255+ if (unlikely(err))
11256+ goto out;
11257+
11258+ if (au_opt_test(au_mntflags(sb), DIRREN))
11259+ err = au_dr_hino(sb, /*bindex*/-1, br, path);
11260+
11261+out:
11262+ AuTraceErr(err);
11263+ return err;
11264+}
11265+
11266+int au_dr_br_fin(struct super_block *sb, struct au_branch *br)
11267+{
11268+ int err;
11269+
11270+ err = 0;
11271+ if (au_br_writable(br->br_perm))
11272+ err = au_dr_hino(sb, /*bindex*/-1, br, /*path*/NULL);
11273+ if (!err)
11274+ au_dr_hino_free(&br->br_dirren);
11275+
11276+ return err;
11277+}
11278+
11279+/* ---------------------------------------------------------------------- */
11280+
11281+static int au_brid_str(struct au_dr_brid *brid, struct inode *h_inode,
11282+ char *buf, size_t sz)
11283+{
11284+ int err;
11285+ unsigned int major, minor;
11286+ char *p;
11287+
11288+ p = buf;
11289+ err = snprintf(p, sz, "%d_", brid->type);
11290+ AuDebugOn(err > sz);
11291+ p += err;
11292+ sz -= err;
11293+ switch (brid->type) {
11294+ case AuBrid_Unset:
11295+ return -EINVAL;
11296+ case AuBrid_UUID:
11297+ err = snprintf(p, sz, "%pU", brid->uuid.b);
11298+ break;
11299+ case AuBrid_FSID:
11300+ err = snprintf(p, sz, "%08x-%08x",
11301+ brid->fsid.val[0], brid->fsid.val[1]);
11302+ break;
11303+ case AuBrid_DEV:
11304+ major = MAJOR(brid->dev);
11305+ minor = MINOR(brid->dev);
11306+ if (major <= 0xff && minor <= 0xff)
11307+ err = snprintf(p, sz, "%02x%02x", major, minor);
11308+ else
11309+ err = snprintf(p, sz, "%03x:%05x", major, minor);
11310+ break;
11311+ }
11312+ AuDebugOn(err > sz);
11313+ p += err;
11314+ sz -= err;
11315+ err = snprintf(p, sz, "_%llu", (unsigned long long)h_inode->i_ino);
11316+ AuDebugOn(err > sz);
11317+ p += err;
11318+ sz -= err;
11319+
11320+ return p - buf;
11321+}
11322+
11323+static int au_drinfo_name(struct au_branch *br, char *name, int len)
11324+{
11325+ int rlen;
11326+ struct dentry *br_dentry;
11327+ struct inode *br_inode;
11328+
11329+ br_dentry = au_br_dentry(br);
11330+ br_inode = d_inode(br_dentry);
11331+ rlen = au_brid_str(&br->br_dirren.dr_brid, br_inode, name, len);
11332+ AuDebugOn(rlen >= AUFS_DIRREN_ENV_VAL_SZ);
11333+ AuDebugOn(rlen > len);
11334+
11335+ return rlen;
11336+}
11337+
11338+/* ---------------------------------------------------------------------- */
11339+
11340+/*
11341+ * from the given @h_dentry, construct drinfo at @*fdata.
11342+ * when the size of @*fdata is not enough, reallocate and return new @fdata and
11343+ * @allocated.
11344+ */
11345+static int au_drinfo_construct(struct au_drinfo_fdata **fdata,
11346+ struct dentry *h_dentry,
11347+ unsigned char *allocated)
11348+{
11349+ int err, v;
11350+ struct au_drinfo_fdata *f, *p;
11351+ struct au_drinfo *drinfo;
11352+ struct inode *h_inode;
11353+ struct qstr *qname;
11354+
11355+ err = 0;
11356+ f = *fdata;
11357+ h_inode = d_inode(h_dentry);
11358+ qname = &h_dentry->d_name;
11359+ drinfo = &f->drinfo;
11360+ drinfo->ino = (__force uint64_t)cpu_to_be64(h_inode->i_ino);
11361+ drinfo->oldnamelen = qname->len;
11362+ if (*allocated < sizeof(*f) + qname->len) {
11363+ v = roundup_pow_of_two(*allocated + qname->len);
11364+ p = au_krealloc(f, v, GFP_NOFS, /*may_shrink*/0);
11365+ if (unlikely(!p)) {
11366+ err = -ENOMEM;
11367+ AuTraceErr(err);
11368+ goto out;
11369+ }
11370+ f = p;
11371+ *fdata = f;
11372+ *allocated = v;
11373+ drinfo = &f->drinfo;
11374+ }
11375+ memcpy(drinfo->oldname, qname->name, qname->len);
11376+ AuDbg("i%llu, %.*s\n",
11377+ be64_to_cpu((__force __be64)drinfo->ino), drinfo->oldnamelen,
11378+ drinfo->oldname);
11379+
11380+out:
11381+ AuTraceErr(err);
11382+ return err;
11383+}
11384+
11385+/* callers have to free the return value */
11386+static struct au_drinfo *au_drinfo_read_k(struct file *file, ino_t h_ino)
11387+{
11388+ struct au_drinfo *ret, *drinfo;
11389+ struct au_drinfo_fdata fdata;
11390+ int len;
11391+ loff_t pos;
11392+ ssize_t ssz;
11393+
11394+ ret = ERR_PTR(-EIO);
11395+ pos = 0;
11396+ ssz = vfsub_read_k(file, &fdata, sizeof(fdata), &pos);
11397+ if (unlikely(ssz != sizeof(fdata))) {
11398+ AuIOErr("ssz %zd, %u, %pD2\n",
11399+ ssz, (unsigned int)sizeof(fdata), file);
11400+ goto out;
11401+ }
11402+
11403+ fdata.magic = ntohl((__force __be32)fdata.magic);
11404+ switch (fdata.magic) {
11405+ case AUFS_DRINFO_MAGIC_V1:
11406+ break;
11407+ default:
11408+ AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
11409+ fdata.magic, AUFS_DRINFO_MAGIC_V1, file);
11410+ goto out;
11411+ }
11412+
11413+ drinfo = &fdata.drinfo;
11414+ len = drinfo->oldnamelen;
11415+ if (!len) {
11416+ AuIOErr("broken drinfo %pD2\n", file);
11417+ goto out;
11418+ }
11419+
11420+ ret = NULL;
11421+ drinfo->ino = be64_to_cpu((__force __be64)drinfo->ino);
11422+ if (unlikely(h_ino && drinfo->ino != h_ino)) {
11423+ AuDbg("ignored i%llu, i%llu, %pD2\n",
11424+ (unsigned long long)drinfo->ino,
11425+ (unsigned long long)h_ino, file);
11426+ goto out; /* success */
11427+ }
11428+
11429+ ret = kmalloc(sizeof(*ret) + len, GFP_NOFS);
11430+ if (unlikely(!ret)) {
11431+ ret = ERR_PTR(-ENOMEM);
11432+ AuTraceErrPtr(ret);
11433+ goto out;
11434+ }
11435+
11436+ *ret = *drinfo;
11437+ ssz = vfsub_read_k(file, (void *)ret->oldname, len, &pos);
11438+ if (unlikely(ssz != len)) {
9f237c51 11439+ au_kfree_rcu(ret);
8b6a4947
AM
11440+ ret = ERR_PTR(-EIO);
11441+ AuIOErr("ssz %zd, %u, %pD2\n", ssz, len, file);
11442+ goto out;
11443+ }
11444+
11445+ AuDbg("oldname %.*s\n", ret->oldnamelen, ret->oldname);
11446+
11447+out:
11448+ return ret;
11449+}
11450+
11451+/* ---------------------------------------------------------------------- */
11452+
11453+/* in order to be revertible */
11454+struct au_drinfo_rev_elm {
11455+ int created;
11456+ struct dentry *info_dentry;
11457+ struct au_drinfo *info_last;
11458+};
11459+
11460+struct au_drinfo_rev {
11461+ unsigned char already;
11462+ aufs_bindex_t nelm;
42b5c33a 11463+ struct au_drinfo_rev_elm elm[];
8b6a4947
AM
11464+};
11465+
11466+/* todo: isn't it too large? */
11467+struct au_drinfo_store {
11468+ struct path h_ppath;
11469+ struct dentry *h_dentry;
11470+ struct au_drinfo_fdata *fdata;
11471+ char *infoname; /* inside of whname, just after PFX */
11472+ char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ];
11473+ aufs_bindex_t btgt, btail;
11474+ unsigned char no_sio,
11475+ allocated, /* current size of *fdata */
11476+ infonamelen, /* room size for p */
acd2b654 11477+ whnamelen, /* length of the generated name */
8b6a4947
AM
11478+ renameback; /* renamed back */
11479+};
11480+
11481+/* on rename(2) error, the caller should revert it using @elm */
11482+static int au_drinfo_do_store(struct au_drinfo_store *w,
11483+ struct au_drinfo_rev_elm *elm)
11484+{
11485+ int err, len;
11486+ ssize_t ssz;
11487+ loff_t pos;
11488+ struct path infopath = {
11489+ .mnt = w->h_ppath.mnt
11490+ };
11491+ struct inode *h_dir, *h_inode, *delegated;
11492+ struct file *infofile;
11493+ struct qstr *qname;
11494+
11495+ AuDebugOn(elm
11496+ && memcmp(elm, page_address(ZERO_PAGE(0)), sizeof(*elm)));
11497+
758e9dad 11498+ infopath.dentry = vfsub_lookup_one_len(w->whname, &w->h_ppath,
8b6a4947
AM
11499+ w->whnamelen);
11500+ AuTraceErrPtr(infopath.dentry);
11501+ if (IS_ERR(infopath.dentry)) {
11502+ err = PTR_ERR(infopath.dentry);
11503+ goto out;
11504+ }
11505+
11506+ err = 0;
11507+ h_dir = d_inode(w->h_ppath.dentry);
11508+ if (elm && d_is_negative(infopath.dentry)) {
11509+ err = vfsub_create(h_dir, &infopath, 0600, /*want_excl*/true);
11510+ AuTraceErr(err);
11511+ if (unlikely(err))
11512+ goto out_dput;
11513+ elm->created = 1;
11514+ elm->info_dentry = dget(infopath.dentry);
11515+ }
11516+
11517+ infofile = vfsub_dentry_open(&infopath, O_RDWR);
11518+ AuTraceErrPtr(infofile);
11519+ if (IS_ERR(infofile)) {
11520+ err = PTR_ERR(infofile);
11521+ goto out_dput;
11522+ }
11523+
11524+ h_inode = d_inode(infopath.dentry);
11525+ if (elm && i_size_read(h_inode)) {
11526+ h_inode = d_inode(w->h_dentry);
11527+ elm->info_last = au_drinfo_read_k(infofile, h_inode->i_ino);
11528+ AuTraceErrPtr(elm->info_last);
11529+ if (IS_ERR(elm->info_last)) {
11530+ err = PTR_ERR(elm->info_last);
11531+ elm->info_last = NULL;
11532+ AuDebugOn(elm->info_dentry);
11533+ goto out_fput;
11534+ }
11535+ }
11536+
11537+ if (elm && w->renameback) {
11538+ delegated = NULL;
11539+ err = vfsub_unlink(h_dir, &infopath, &delegated, /*force*/0);
11540+ AuTraceErr(err);
11541+ if (unlikely(err == -EWOULDBLOCK))
11542+ iput(delegated);
11543+ goto out_fput;
11544+ }
11545+
11546+ pos = 0;
11547+ qname = &w->h_dentry->d_name;
11548+ len = sizeof(*w->fdata) + qname->len;
11549+ if (!elm)
11550+ len = sizeof(*w->fdata) + w->fdata->drinfo.oldnamelen;
11551+ ssz = vfsub_write_k(infofile, w->fdata, len, &pos);
11552+ if (ssz == len) {
11553+ AuDbg("hi%llu, %.*s\n", w->fdata->drinfo.ino,
11554+ w->fdata->drinfo.oldnamelen, w->fdata->drinfo.oldname);
11555+ goto out_fput; /* success */
11556+ } else {
11557+ err = -EIO;
11558+ if (ssz < 0)
11559+ err = ssz;
11560+ /* the caller should revert it using @elm */
11561+ }
11562+
11563+out_fput:
11564+ fput(infofile);
11565+out_dput:
11566+ dput(infopath.dentry);
11567+out:
11568+ AuTraceErr(err);
11569+ return err;
11570+}
11571+
11572+struct au_call_drinfo_do_store_args {
11573+ int *errp;
11574+ struct au_drinfo_store *w;
11575+ struct au_drinfo_rev_elm *elm;
11576+};
11577+
11578+static void au_call_drinfo_do_store(void *args)
11579+{
11580+ struct au_call_drinfo_do_store_args *a = args;
11581+
11582+ *a->errp = au_drinfo_do_store(a->w, a->elm);
11583+}
11584+
11585+static int au_drinfo_store_sio(struct au_drinfo_store *w,
11586+ struct au_drinfo_rev_elm *elm)
11587+{
11588+ int err, wkq_err;
11589+
11590+ if (w->no_sio)
11591+ err = au_drinfo_do_store(w, elm);
11592+ else {
11593+ struct au_call_drinfo_do_store_args a = {
11594+ .errp = &err,
11595+ .w = w,
11596+ .elm = elm
11597+ };
11598+ wkq_err = au_wkq_wait(au_call_drinfo_do_store, &a);
11599+ if (unlikely(wkq_err))
11600+ err = wkq_err;
11601+ }
11602+ AuTraceErr(err);
11603+
11604+ return err;
11605+}
11606+
11607+static int au_drinfo_store_work_init(struct au_drinfo_store *w,
11608+ aufs_bindex_t btgt)
11609+{
11610+ int err;
11611+
11612+ memset(w, 0, sizeof(*w));
11613+ w->allocated = roundup_pow_of_two(sizeof(*w->fdata) + 40);
11614+ strcpy(w->whname, AUFS_WH_DR_INFO_PFX);
11615+ w->infoname = w->whname + sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11616+ w->infonamelen = sizeof(w->whname) - sizeof(AUFS_WH_DR_INFO_PFX);
11617+ w->btgt = btgt;
11618+ w->no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
11619+
11620+ err = -ENOMEM;
11621+ w->fdata = kcalloc(1, w->allocated, GFP_NOFS);
11622+ if (unlikely(!w->fdata)) {
11623+ AuTraceErr(err);
11624+ goto out;
11625+ }
11626+ w->fdata->magic = (__force uint32_t)htonl(AUFS_DRINFO_MAGIC_V1);
11627+ err = 0;
11628+
11629+out:
11630+ return err;
11631+}
11632+
11633+static void au_drinfo_store_work_fin(struct au_drinfo_store *w)
11634+{
9f237c51 11635+ au_kfree_rcu(w->fdata);
8b6a4947
AM
11636+}
11637+
11638+static void au_drinfo_store_rev(struct au_drinfo_rev *rev,
11639+ struct au_drinfo_store *w)
11640+{
11641+ struct au_drinfo_rev_elm *elm;
11642+ struct inode *h_dir, *delegated;
11643+ int err, nelm;
11644+ struct path infopath = {
11645+ .mnt = w->h_ppath.mnt
11646+ };
11647+
11648+ h_dir = d_inode(w->h_ppath.dentry);
11649+ IMustLock(h_dir);
11650+
11651+ err = 0;
11652+ elm = rev->elm;
11653+ for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11654+ AuDebugOn(elm->created && elm->info_last);
11655+ if (elm->created) {
11656+ AuDbg("here\n");
11657+ delegated = NULL;
11658+ infopath.dentry = elm->info_dentry;
11659+ err = vfsub_unlink(h_dir, &infopath, &delegated,
11660+ !w->no_sio);
11661+ AuTraceErr(err);
11662+ if (unlikely(err == -EWOULDBLOCK))
11663+ iput(delegated);
11664+ dput(elm->info_dentry);
11665+ } else if (elm->info_last) {
11666+ AuDbg("here\n");
11667+ w->fdata->drinfo = *elm->info_last;
11668+ memcpy(w->fdata->drinfo.oldname,
11669+ elm->info_last->oldname,
11670+ elm->info_last->oldnamelen);
11671+ err = au_drinfo_store_sio(w, /*elm*/NULL);
9f237c51 11672+ au_kfree_rcu(elm->info_last);
8b6a4947
AM
11673+ }
11674+ if (unlikely(err))
11675+ AuIOErr("%d, %s\n", err, w->whname);
11676+ /* go on even if err */
11677+ }
11678+}
11679+
11680+/* caller has to call au_dr_rename_fin() later */
11681+static int au_drinfo_store(struct dentry *dentry, aufs_bindex_t btgt,
11682+ struct qstr *dst_name, void *_rev)
11683+{
11684+ int err, sz, nelm;
11685+ aufs_bindex_t bindex, btail;
11686+ struct au_drinfo_store work;
11687+ struct au_drinfo_rev *rev, **p;
11688+ struct au_drinfo_rev_elm *elm;
11689+ struct super_block *sb;
11690+ struct au_branch *br;
11691+ struct au_hinode *hdir;
11692+
11693+ err = au_drinfo_store_work_init(&work, btgt);
11694+ AuTraceErr(err);
11695+ if (unlikely(err))
11696+ goto out;
11697+
11698+ err = -ENOMEM;
11699+ btail = au_dbtaildir(dentry);
11700+ nelm = btail - btgt;
11701+ sz = sizeof(*rev) + sizeof(*elm) * nelm;
11702+ rev = kcalloc(1, sz, GFP_NOFS);
11703+ if (unlikely(!rev)) {
11704+ AuTraceErr(err);
11705+ goto out_args;
11706+ }
11707+ rev->nelm = nelm;
11708+ elm = rev->elm;
11709+ p = _rev;
11710+ *p = rev;
11711+
11712+ err = 0;
11713+ sb = dentry->d_sb;
11714+ work.h_ppath.dentry = au_h_dptr(dentry, btgt);
11715+ work.h_ppath.mnt = au_sbr_mnt(sb, btgt);
11716+ hdir = au_hi(d_inode(dentry), btgt);
11717+ au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11718+ for (bindex = btgt + 1; bindex <= btail; bindex++, elm++) {
11719+ work.h_dentry = au_h_dptr(dentry, bindex);
11720+ if (!work.h_dentry)
11721+ continue;
11722+
11723+ err = au_drinfo_construct(&work.fdata, work.h_dentry,
11724+ &work.allocated);
11725+ AuTraceErr(err);
11726+ if (unlikely(err))
11727+ break;
11728+
11729+ work.renameback = au_qstreq(&work.h_dentry->d_name, dst_name);
11730+ br = au_sbr(sb, bindex);
11731+ work.whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11732+ work.whnamelen += au_drinfo_name(br, work.infoname,
11733+ work.infonamelen);
11734+ AuDbg("whname %.*s, i%llu, %.*s\n",
11735+ work.whnamelen, work.whname,
11736+ be64_to_cpu((__force __be64)work.fdata->drinfo.ino),
11737+ work.fdata->drinfo.oldnamelen,
11738+ work.fdata->drinfo.oldname);
11739+
11740+ err = au_drinfo_store_sio(&work, elm);
11741+ AuTraceErr(err);
11742+ if (unlikely(err))
11743+ break;
11744+ }
11745+ if (unlikely(err)) {
11746+ /* revert all drinfo */
11747+ au_drinfo_store_rev(rev, &work);
9f237c51 11748+ au_kfree_try_rcu(rev);
8b6a4947
AM
11749+ *p = NULL;
11750+ }
11751+ au_hn_inode_unlock(hdir);
11752+
11753+out_args:
11754+ au_drinfo_store_work_fin(&work);
11755+out:
11756+ return err;
11757+}
11758+
11759+/* ---------------------------------------------------------------------- */
11760+
11761+int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
11762+ struct qstr *dst_name, void *_rev)
11763+{
11764+ int err, already;
11765+ ino_t ino;
11766+ struct super_block *sb;
11767+ struct au_branch *br;
11768+ struct au_dr_br *dr;
11769+ struct dentry *h_dentry;
11770+ struct inode *h_inode;
11771+ struct au_dr_hino *ent;
11772+ struct au_drinfo_rev *rev, **p;
11773+
11774+ AuDbg("bindex %d\n", bindex);
11775+
11776+ err = -ENOMEM;
11777+ ent = kmalloc(sizeof(*ent), GFP_NOFS);
11778+ if (unlikely(!ent))
11779+ goto out;
11780+
11781+ sb = src->d_sb;
11782+ br = au_sbr(sb, bindex);
11783+ dr = &br->br_dirren;
11784+ h_dentry = au_h_dptr(src, bindex);
11785+ h_inode = d_inode(h_dentry);
11786+ ino = h_inode->i_ino;
11787+ ent->dr_h_ino = ino;
11788+ already = au_dr_hino_test_add(dr, ino, ent);
11789+ AuDbg("b%d, hi%llu, already %d\n",
11790+ bindex, (unsigned long long)ino, already);
11791+
11792+ err = au_drinfo_store(src, bindex, dst_name, _rev);
11793+ AuTraceErr(err);
11794+ if (!err) {
11795+ p = _rev;
11796+ rev = *p;
11797+ rev->already = already;
11798+ goto out; /* success */
11799+ }
11800+
11801+ /* revert */
11802+ if (!already)
11803+ au_dr_hino_del(dr, ent);
9f237c51 11804+ au_kfree_rcu(ent);
8b6a4947
AM
11805+
11806+out:
11807+ AuTraceErr(err);
11808+ return err;
11809+}
11810+
11811+void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11812+{
11813+ struct au_drinfo_rev *rev;
11814+ struct au_drinfo_rev_elm *elm;
11815+ int nelm;
11816+
11817+ rev = _rev;
11818+ elm = rev->elm;
11819+ for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11820+ dput(elm->info_dentry);
9f237c51 11821+ au_kfree_rcu(elm->info_last);
8b6a4947 11822+ }
9f237c51 11823+ au_kfree_try_rcu(rev);
8b6a4947
AM
11824+}
11825+
11826+void au_dr_rename_rev(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11827+{
11828+ int err;
11829+ struct au_drinfo_store work;
11830+ struct au_drinfo_rev *rev = _rev;
11831+ struct super_block *sb;
11832+ struct au_branch *br;
11833+ struct inode *h_inode;
11834+ struct au_dr_br *dr;
11835+ struct au_dr_hino *ent;
11836+
11837+ err = au_drinfo_store_work_init(&work, btgt);
11838+ if (unlikely(err))
11839+ goto out;
11840+
11841+ sb = src->d_sb;
11842+ br = au_sbr(sb, btgt);
11843+ work.h_ppath.dentry = au_h_dptr(src, btgt);
11844+ work.h_ppath.mnt = au_br_mnt(br);
11845+ au_drinfo_store_rev(rev, &work);
11846+ au_drinfo_store_work_fin(&work);
11847+ if (rev->already)
11848+ goto out;
11849+
11850+ dr = &br->br_dirren;
11851+ h_inode = d_inode(work.h_ppath.dentry);
11852+ ent = au_dr_hino_find(dr, h_inode->i_ino);
11853+ BUG_ON(!ent);
11854+ au_dr_hino_del(dr, ent);
9f237c51 11855+ au_kfree_rcu(ent);
8b6a4947
AM
11856+
11857+out:
9f237c51 11858+ au_kfree_try_rcu(rev);
8b6a4947
AM
11859+ if (unlikely(err))
11860+ pr_err("failed to remove dirren info\n");
11861+}
11862+
11863+/* ---------------------------------------------------------------------- */
11864+
11865+static struct au_drinfo *au_drinfo_do_load(struct path *h_ppath,
11866+ char *whname, int whnamelen,
11867+ struct dentry **info_dentry)
11868+{
11869+ struct au_drinfo *drinfo;
11870+ struct file *f;
11871+ struct inode *h_dir;
11872+ struct path infopath;
11873+ int unlocked;
11874+
11875+ AuDbg("%pd/%.*s\n", h_ppath->dentry, whnamelen, whname);
11876+
11877+ *info_dentry = NULL;
11878+ drinfo = NULL;
11879+ unlocked = 0;
11880+ h_dir = d_inode(h_ppath->dentry);
be118d29 11881+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
758e9dad 11882+ infopath.dentry = vfsub_lookup_one_len(whname, h_ppath, whnamelen);
8b6a4947
AM
11883+ if (IS_ERR(infopath.dentry)) {
11884+ drinfo = (void *)infopath.dentry;
11885+ goto out;
11886+ }
11887+
11888+ if (d_is_negative(infopath.dentry))
11889+ goto out_dput; /* success */
11890+
11891+ infopath.mnt = h_ppath->mnt;
11892+ f = vfsub_dentry_open(&infopath, O_RDONLY);
11893+ inode_unlock_shared(h_dir);
11894+ unlocked = 1;
11895+ if (IS_ERR(f)) {
11896+ drinfo = (void *)f;
11897+ goto out_dput;
11898+ }
11899+
11900+ drinfo = au_drinfo_read_k(f, /*h_ino*/0);
11901+ if (IS_ERR_OR_NULL(drinfo))
11902+ goto out_fput;
11903+
11904+ AuDbg("oldname %.*s\n", drinfo->oldnamelen, drinfo->oldname);
11905+ *info_dentry = dget(infopath.dentry); /* keep it alive */
11906+
11907+out_fput:
11908+ fput(f);
11909+out_dput:
11910+ dput(infopath.dentry);
11911+out:
11912+ if (!unlocked)
11913+ inode_unlock_shared(h_dir);
11914+ AuTraceErrPtr(drinfo);
11915+ return drinfo;
11916+}
11917+
11918+struct au_drinfo_do_load_args {
11919+ struct au_drinfo **drinfop;
11920+ struct path *h_ppath;
11921+ char *whname;
11922+ int whnamelen;
11923+ struct dentry **info_dentry;
11924+};
11925+
11926+static void au_call_drinfo_do_load(void *args)
11927+{
11928+ struct au_drinfo_do_load_args *a = args;
11929+
11930+ *a->drinfop = au_drinfo_do_load(a->h_ppath, a->whname, a->whnamelen,
11931+ a->info_dentry);
11932+}
11933+
11934+struct au_drinfo_load {
11935+ struct path h_ppath;
11936+ struct qstr *qname;
11937+ unsigned char no_sio;
11938+
11939+ aufs_bindex_t ninfo;
11940+ struct au_drinfo **drinfo;
11941+};
11942+
11943+static int au_drinfo_load(struct au_drinfo_load *w, aufs_bindex_t bindex,
11944+ struct au_branch *br)
11945+{
11946+ int err, wkq_err, whnamelen, e;
11947+ char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ]
11948+ = AUFS_WH_DR_INFO_PFX;
11949+ struct au_drinfo *drinfo;
11950+ struct qstr oldname;
11951+ struct inode *h_dir, *delegated;
11952+ struct dentry *info_dentry;
11953+ struct path infopath;
11954+
11955+ whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11956+ whnamelen += au_drinfo_name(br, whname + whnamelen,
11957+ sizeof(whname) - whnamelen);
11958+ if (w->no_sio)
11959+ drinfo = au_drinfo_do_load(&w->h_ppath, whname, whnamelen,
11960+ &info_dentry);
11961+ else {
11962+ struct au_drinfo_do_load_args args = {
11963+ .drinfop = &drinfo,
11964+ .h_ppath = &w->h_ppath,
11965+ .whname = whname,
11966+ .whnamelen = whnamelen,
11967+ .info_dentry = &info_dentry
11968+ };
11969+ wkq_err = au_wkq_wait(au_call_drinfo_do_load, &args);
11970+ if (unlikely(wkq_err))
11971+ drinfo = ERR_PTR(wkq_err);
11972+ }
11973+ err = PTR_ERR(drinfo);
11974+ if (IS_ERR_OR_NULL(drinfo))
11975+ goto out;
11976+
11977+ err = 0;
11978+ oldname.len = drinfo->oldnamelen;
11979+ oldname.name = drinfo->oldname;
11980+ if (au_qstreq(w->qname, &oldname)) {
11981+ /* the name is renamed back */
9f237c51 11982+ au_kfree_rcu(drinfo);
8b6a4947
AM
11983+ drinfo = NULL;
11984+
11985+ infopath.dentry = info_dentry;
11986+ infopath.mnt = w->h_ppath.mnt;
11987+ h_dir = d_inode(w->h_ppath.dentry);
11988+ delegated = NULL;
11989+ inode_lock_nested(h_dir, AuLsc_I_PARENT);
11990+ e = vfsub_unlink(h_dir, &infopath, &delegated, !w->no_sio);
11991+ inode_unlock(h_dir);
11992+ if (unlikely(e))
11993+ AuIOErr("ignored %d, %pd2\n", e, &infopath.dentry);
11994+ if (unlikely(e == -EWOULDBLOCK))
11995+ iput(delegated);
11996+ }
9f237c51 11997+ au_kfree_rcu(w->drinfo[bindex]);
8b6a4947
AM
11998+ w->drinfo[bindex] = drinfo;
11999+ dput(info_dentry);
12000+
12001+out:
12002+ AuTraceErr(err);
12003+ return err;
12004+}
12005+
12006+/* ---------------------------------------------------------------------- */
12007+
12008+static void au_dr_lkup_free(struct au_drinfo **drinfo, int n)
12009+{
12010+ struct au_drinfo **p = drinfo;
12011+
12012+ while (n-- > 0)
9f237c51
AM
12013+ au_kfree_rcu(*drinfo++);
12014+ au_kfree_try_rcu(p);
8b6a4947
AM
12015+}
12016+
12017+int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12018+ aufs_bindex_t btgt)
12019+{
12020+ int err, ninfo;
12021+ struct au_drinfo_load w;
12022+ aufs_bindex_t bindex, bbot;
12023+ struct au_branch *br;
12024+ struct inode *h_dir;
12025+ struct au_dr_hino *ent;
12026+ struct super_block *sb;
12027+
12028+ AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
12029+ AuLNPair(&dentry->d_name), AuLNPair(&lkup->dirren.dr_name),
12030+ AuLNPair(&lkup->whname), btgt);
12031+
12032+ sb = dentry->d_sb;
12033+ bbot = au_sbbot(sb);
12034+ w.ninfo = bbot + 1;
12035+ if (!lkup->dirren.drinfo) {
12036+ lkup->dirren.drinfo = kcalloc(w.ninfo,
12037+ sizeof(*lkup->dirren.drinfo),
12038+ GFP_NOFS);
12039+ if (unlikely(!lkup->dirren.drinfo)) {
12040+ err = -ENOMEM;
12041+ goto out;
12042+ }
12043+ lkup->dirren.ninfo = w.ninfo;
12044+ }
12045+ w.drinfo = lkup->dirren.drinfo;
12046+ w.no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
12047+ w.h_ppath.dentry = au_h_dptr(dentry, btgt);
12048+ AuDebugOn(!w.h_ppath.dentry);
12049+ w.h_ppath.mnt = au_sbr_mnt(sb, btgt);
12050+ w.qname = &dentry->d_name;
12051+
12052+ ninfo = 0;
12053+ for (bindex = btgt + 1; bindex <= bbot; bindex++) {
12054+ br = au_sbr(sb, bindex);
12055+ err = au_drinfo_load(&w, bindex, br);
12056+ if (unlikely(err))
12057+ goto out_free;
12058+ if (w.drinfo[bindex])
12059+ ninfo++;
12060+ }
12061+ if (!ninfo) {
12062+ br = au_sbr(sb, btgt);
12063+ h_dir = d_inode(w.h_ppath.dentry);
12064+ ent = au_dr_hino_find(&br->br_dirren, h_dir->i_ino);
12065+ AuDebugOn(!ent);
12066+ au_dr_hino_del(&br->br_dirren, ent);
9f237c51 12067+ au_kfree_rcu(ent);
8b6a4947
AM
12068+ }
12069+ goto out; /* success */
12070+
12071+out_free:
12072+ au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12073+ lkup->dirren.ninfo = 0;
12074+ lkup->dirren.drinfo = NULL;
12075+out:
12076+ AuTraceErr(err);
12077+ return err;
12078+}
12079+
12080+void au_dr_lkup_fin(struct au_do_lookup_args *lkup)
12081+{
12082+ au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12083+}
12084+
12085+int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt)
12086+{
12087+ int err;
12088+ struct au_drinfo *drinfo;
12089+
12090+ err = 0;
12091+ if (!lkup->dirren.drinfo)
12092+ goto out;
43982f53
AM
12093+ AuDebugOn(lkup->dirren.ninfo <= btgt);
12094+ drinfo = lkup->dirren.drinfo[btgt];
8b6a4947
AM
12095+ if (!drinfo)
12096+ goto out;
12097+
9f237c51 12098+ au_kfree_try_rcu(lkup->whname.name);
8b6a4947
AM
12099+ lkup->whname.name = NULL;
12100+ lkup->dirren.dr_name.len = drinfo->oldnamelen;
12101+ lkup->dirren.dr_name.name = drinfo->oldname;
12102+ lkup->name = &lkup->dirren.dr_name;
12103+ err = au_wh_name_alloc(&lkup->whname, lkup->name);
12104+ if (!err)
12105+ AuDbg("name %.*s, whname %.*s, b%d\n",
12106+ AuLNPair(lkup->name), AuLNPair(&lkup->whname),
12107+ btgt);
12108+
12109+out:
12110+ AuTraceErr(err);
12111+ return err;
12112+}
12113+
12114+int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12115+ ino_t h_ino)
12116+{
12117+ int match;
12118+ struct au_drinfo *drinfo;
12119+
12120+ match = 1;
12121+ if (!lkup->dirren.drinfo)
12122+ goto out;
43982f53
AM
12123+ AuDebugOn(lkup->dirren.ninfo <= bindex);
12124+ drinfo = lkup->dirren.drinfo[bindex];
8b6a4947
AM
12125+ if (!drinfo)
12126+ goto out;
12127+
12128+ match = (drinfo->ino == h_ino);
12129+ AuDbg("match %d\n", match);
12130+
12131+out:
12132+ return match;
12133+}
12134+
12135+/* ---------------------------------------------------------------------- */
12136+
12137+int au_dr_opt_set(struct super_block *sb)
12138+{
12139+ int err;
12140+ aufs_bindex_t bindex, bbot;
12141+ struct au_branch *br;
12142+
12143+ err = 0;
12144+ bbot = au_sbbot(sb);
12145+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
12146+ br = au_sbr(sb, bindex);
12147+ err = au_dr_hino(sb, bindex, /*br*/NULL, &br->br_path);
12148+ }
12149+
12150+ return err;
12151+}
12152+
12153+int au_dr_opt_flush(struct super_block *sb)
12154+{
12155+ int err;
12156+ aufs_bindex_t bindex, bbot;
12157+ struct au_branch *br;
12158+
12159+ err = 0;
12160+ bbot = au_sbbot(sb);
12161+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
12162+ br = au_sbr(sb, bindex);
12163+ if (au_br_writable(br->br_perm))
12164+ err = au_dr_hino(sb, bindex, /*br*/NULL, /*path*/NULL);
12165+ }
12166+
12167+ return err;
12168+}
12169+
12170+int au_dr_opt_clr(struct super_block *sb, int no_flush)
12171+{
12172+ int err;
12173+ aufs_bindex_t bindex, bbot;
12174+ struct au_branch *br;
12175+
12176+ err = 0;
12177+ if (!no_flush) {
12178+ err = au_dr_opt_flush(sb);
12179+ if (unlikely(err))
12180+ goto out;
12181+ }
12182+
12183+ bbot = au_sbbot(sb);
12184+ for (bindex = 0; bindex <= bbot; bindex++) {
12185+ br = au_sbr(sb, bindex);
12186+ au_dr_hino_free(&br->br_dirren);
12187+ }
12188+
12189+out:
12190+ return err;
12191+}
12192diff -urN /usr/share/empty/fs/aufs/dirren.h linux/fs/aufs/dirren.h
eca34b5c 12193--- /usr/share/empty/fs/aufs/dirren.h 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 12194+++ linux/fs/aufs/dirren.h 2022-11-05 23:02:18.962555950 +0100
062440b3
AM
12195@@ -0,0 +1,140 @@
12196+/* SPDX-License-Identifier: GPL-2.0 */
8b6a4947 12197+/*
0dcfbb52 12198+ * Copyright (C) 2017-2022 Junjiro R. Okajima
8b6a4947 12199+ *
0dcfbb52 12200+ * This program is free software; you can redistribute it and/or modify
8b6a4947
AM
12201+ * it under the terms of the GNU General Public License as published by
12202+ * the Free Software Foundation; either version 2 of the License, or
12203+ * (at your option) any later version.
12204+ *
12205+ * This program is distributed in the hope that it will be useful,
12206+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12207+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12208+ * GNU General Public License for more details.
12209+ *
12210+ * You should have received a copy of the GNU General Public License
12211+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
12212+ */
12213+
12214+/*
12215+ * renamed dir info
12216+ */
12217+
12218+#ifndef __AUFS_DIRREN_H__
12219+#define __AUFS_DIRREN_H__
12220+
12221+#ifdef __KERNEL__
12222+
12223+#include <linux/dcache.h>
12224+#include <linux/statfs.h>
12225+#include <linux/uuid.h>
12226+#include "hbl.h"
12227+
12228+#define AuDirren_NHASH 100
12229+
12230+#ifdef CONFIG_AUFS_DIRREN
12231+enum au_brid_type {
12232+ AuBrid_Unset,
12233+ AuBrid_UUID,
12234+ AuBrid_FSID,
12235+ AuBrid_DEV
12236+};
12237+
12238+struct au_dr_brid {
12239+ enum au_brid_type type;
12240+ union {
12241+ uuid_t uuid; /* unimplemented yet */
12242+ fsid_t fsid;
12243+ dev_t dev;
12244+ };
12245+};
12246+
12247+/* 20 is the max digits length of ulong 64 */
12248+/* brid-type "_" uuid "_" inum */
12249+#define AUFS_DIRREN_FNAME_SZ (1 + 1 + UUID_STRING_LEN + 20)
12250+#define AUFS_DIRREN_ENV_VAL_SZ (AUFS_DIRREN_FNAME_SZ + 1 + 20)
12251+
12252+struct au_dr_hino {
12253+ struct hlist_bl_node dr_hnode;
12254+ ino_t dr_h_ino;
12255+};
12256+
12257+struct au_dr_br {
12258+ struct hlist_bl_head dr_h_ino[AuDirren_NHASH];
12259+ struct au_dr_brid dr_brid;
12260+};
12261+
12262+struct au_dr_lookup {
12263+ /* dr_name is pointed by struct au_do_lookup_args.name */
12264+ struct qstr dr_name; /* subset of dr_info */
12265+ aufs_bindex_t ninfo;
12266+ struct au_drinfo **drinfo;
12267+};
12268+#else
12269+struct au_dr_hino;
12270+/* empty */
12271+struct au_dr_br { };
12272+struct au_dr_lookup { };
12273+#endif
12274+
12275+/* ---------------------------------------------------------------------- */
12276+
12277+struct au_branch;
12278+struct au_do_lookup_args;
12279+struct au_hinode;
12280+#ifdef CONFIG_AUFS_DIRREN
12281+int au_dr_hino_test_add(struct au_dr_br *dr, ino_t h_ino,
12282+ struct au_dr_hino *add_ent);
12283+void au_dr_hino_free(struct au_dr_br *dr);
12284+int au_dr_br_init(struct super_block *sb, struct au_branch *br,
12285+ const struct path *path);
12286+int au_dr_br_fin(struct super_block *sb, struct au_branch *br);
12287+int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
12288+ struct qstr *dst_name, void *_rev);
12289+void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *rev);
12290+void au_dr_rename_rev(struct dentry *src, aufs_bindex_t bindex, void *rev);
12291+int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12292+ aufs_bindex_t bindex);
12293+int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12294+int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12295+ ino_t h_ino);
12296+void au_dr_lkup_fin(struct au_do_lookup_args *lkup);
12297+int au_dr_opt_set(struct super_block *sb);
12298+int au_dr_opt_flush(struct super_block *sb);
12299+int au_dr_opt_clr(struct super_block *sb, int no_flush);
12300+#else
12301+AuStubInt0(au_dr_hino_test_add, struct au_dr_br *dr, ino_t h_ino,
12302+ struct au_dr_hino *add_ent);
12303+AuStubVoid(au_dr_hino_free, struct au_dr_br *dr);
12304+AuStubInt0(au_dr_br_init, struct super_block *sb, struct au_branch *br,
12305+ const struct path *path);
12306+AuStubInt0(au_dr_br_fin, struct super_block *sb, struct au_branch *br);
12307+AuStubInt0(au_dr_rename, struct dentry *src, aufs_bindex_t bindex,
12308+ struct qstr *dst_name, void *_rev);
12309+AuStubVoid(au_dr_rename_fin, struct dentry *src, aufs_bindex_t btgt, void *rev);
12310+AuStubVoid(au_dr_rename_rev, struct dentry *src, aufs_bindex_t bindex,
12311+ void *rev);
12312+AuStubInt0(au_dr_lkup, struct au_do_lookup_args *lkup, struct dentry *dentry,
12313+ aufs_bindex_t bindex);
12314+AuStubInt0(au_dr_lkup_name, struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12315+AuStubInt0(au_dr_lkup_h_ino, struct au_do_lookup_args *lkup,
12316+ aufs_bindex_t bindex, ino_t h_ino);
12317+AuStubVoid(au_dr_lkup_fin, struct au_do_lookup_args *lkup);
12318+AuStubInt0(au_dr_opt_set, struct super_block *sb);
12319+AuStubInt0(au_dr_opt_flush, struct super_block *sb);
12320+AuStubInt0(au_dr_opt_clr, struct super_block *sb, int no_flush);
12321+#endif
12322+
12323+/* ---------------------------------------------------------------------- */
12324+
12325+#ifdef CONFIG_AUFS_DIRREN
12326+static inline int au_dr_ihash(ino_t h_ino)
12327+{
12328+ return h_ino % AuDirren_NHASH;
12329+}
12330+#else
12331+AuStubInt0(au_dr_ihash, ino_t h_ino);
12332+#endif
12333+
12334+#endif /* __KERNEL__ */
12335+#endif /* __AUFS_DIRREN_H__ */
7f207e10 12336diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
eca34b5c 12337--- /usr/share/empty/fs/aufs/dynop.c 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 12338+++ linux/fs/aufs/dynop.c 2022-11-05 23:02:18.962555950 +0100
82da57cb 12339@@ -0,0 +1,366 @@
cd7a4cd9 12340+// SPDX-License-Identifier: GPL-2.0
1facf9fc 12341+/*
0dcfbb52 12342+ * Copyright (C) 2010-2022 Junjiro R. Okajima
1facf9fc 12343+ *
0dcfbb52 12344+ * This program is free software; you can redistribute it and/or modify
1facf9fc 12345+ * it under the terms of the GNU General Public License as published by
12346+ * the Free Software Foundation; either version 2 of the License, or
12347+ * (at your option) any later version.
dece6358
AM
12348+ *
12349+ * This program is distributed in the hope that it will be useful,
12350+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12351+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12352+ * GNU General Public License for more details.
12353+ *
12354+ * You should have received a copy of the GNU General Public License
523b37e3 12355+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 12356+ */
12357+
12358+/*
4a4d8108 12359+ * dynamically customizable operations for regular files
1facf9fc 12360+ */
12361+
1facf9fc 12362+#include "aufs.h"
12363+
4a4d8108 12364+#define DyPrSym(key) AuDbgSym(key->dk_op.dy_hop)
1facf9fc 12365+
4a4d8108
AM
12366+/*
12367+ * How large will these lists be?
12368+ * Usually just a few elements, 20-30 at most for each, I guess.
12369+ */
8b6a4947 12370+static struct hlist_bl_head dynop[AuDyLast];
4a4d8108 12371+
8b6a4947
AM
12372+static struct au_dykey *dy_gfind_get(struct hlist_bl_head *hbl,
12373+ const void *h_op)
1facf9fc 12374+{
4a4d8108 12375+ struct au_dykey *key, *tmp;
8b6a4947 12376+ struct hlist_bl_node *pos;
1facf9fc 12377+
4a4d8108 12378+ key = NULL;
8b6a4947
AM
12379+ hlist_bl_lock(hbl);
12380+ hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
4a4d8108 12381+ if (tmp->dk_op.dy_hop == h_op) {
83b672a5
AM
12382+ if (kref_get_unless_zero(&tmp->dk_kref))
12383+ key = tmp;
4a4d8108
AM
12384+ break;
12385+ }
8b6a4947 12386+ hlist_bl_unlock(hbl);
4a4d8108
AM
12387+
12388+ return key;
1facf9fc 12389+}
12390+
4a4d8108 12391+static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
1facf9fc 12392+{
4a4d8108
AM
12393+ struct au_dykey **k, *found;
12394+ const void *h_op = key->dk_op.dy_hop;
12395+ int i;
1facf9fc 12396+
4a4d8108
AM
12397+ found = NULL;
12398+ k = br->br_dykey;
12399+ for (i = 0; i < AuBrDynOp; i++)
12400+ if (k[i]) {
12401+ if (k[i]->dk_op.dy_hop == h_op) {
12402+ found = k[i];
12403+ break;
12404+ }
12405+ } else
12406+ break;
12407+ if (!found) {
12408+ spin_lock(&br->br_dykey_lock);
12409+ for (; i < AuBrDynOp; i++)
12410+ if (k[i]) {
12411+ if (k[i]->dk_op.dy_hop == h_op) {
12412+ found = k[i];
12413+ break;
12414+ }
12415+ } else {
12416+ k[i] = key;
12417+ break;
12418+ }
12419+ spin_unlock(&br->br_dykey_lock);
12420+ BUG_ON(i == AuBrDynOp); /* expand the array */
12421+ }
12422+
12423+ return found;
1facf9fc 12424+}
12425+
4a4d8108 12426+/* kref_get() if @key is already added */
8b6a4947 12427+static struct au_dykey *dy_gadd(struct hlist_bl_head *hbl, struct au_dykey *key)
4a4d8108
AM
12428+{
12429+ struct au_dykey *tmp, *found;
8b6a4947 12430+ struct hlist_bl_node *pos;
4a4d8108 12431+ const void *h_op = key->dk_op.dy_hop;
1facf9fc 12432+
4a4d8108 12433+ found = NULL;
8b6a4947
AM
12434+ hlist_bl_lock(hbl);
12435+ hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
4a4d8108 12436+ if (tmp->dk_op.dy_hop == h_op) {
83b672a5
AM
12437+ if (kref_get_unless_zero(&tmp->dk_kref))
12438+ found = tmp;
4a4d8108
AM
12439+ break;
12440+ }
12441+ if (!found)
8b6a4947
AM
12442+ hlist_bl_add_head(&key->dk_hnode, hbl);
12443+ hlist_bl_unlock(hbl);
1facf9fc 12444+
4a4d8108
AM
12445+ if (!found)
12446+ DyPrSym(key);
12447+ return found;
12448+}
12449+
12450+static void dy_free_rcu(struct rcu_head *rcu)
1facf9fc 12451+{
4a4d8108
AM
12452+ struct au_dykey *key;
12453+
12454+ key = container_of(rcu, struct au_dykey, dk_rcu);
12455+ DyPrSym(key);
83b672a5 12456+ kfree(key);
1facf9fc 12457+}
12458+
4a4d8108
AM
12459+static void dy_free(struct kref *kref)
12460+{
12461+ struct au_dykey *key;
8b6a4947 12462+ struct hlist_bl_head *hbl;
1facf9fc 12463+
4a4d8108 12464+ key = container_of(kref, struct au_dykey, dk_kref);
8b6a4947
AM
12465+ hbl = dynop + key->dk_op.dy_type;
12466+ au_hbl_del(&key->dk_hnode, hbl);
4a4d8108
AM
12467+ call_rcu(&key->dk_rcu, dy_free_rcu);
12468+}
12469+
12470+void au_dy_put(struct au_dykey *key)
1facf9fc 12471+{
4a4d8108
AM
12472+ kref_put(&key->dk_kref, dy_free);
12473+}
1facf9fc 12474+
4a4d8108
AM
12475+/* ---------------------------------------------------------------------- */
12476+
12477+#define DyDbgSize(cnt, op) AuDebugOn(cnt != sizeof(op)/sizeof(void *))
12478+
12479+#ifdef CONFIG_AUFS_DEBUG
12480+#define DyDbgDeclare(cnt) unsigned int cnt = 0
4f0767ce 12481+#define DyDbgInc(cnt) do { cnt++; } while (0)
4a4d8108
AM
12482+#else
12483+#define DyDbgDeclare(cnt) do {} while (0)
12484+#define DyDbgInc(cnt) do {} while (0)
12485+#endif
12486+
12487+#define DySet(func, dst, src, h_op, h_sb) do { \
12488+ DyDbgInc(cnt); \
12489+ if (h_op->func) { \
12490+ if (src.func) \
12491+ dst.func = src.func; \
12492+ else \
12493+ AuDbg("%s %s\n", au_sbtype(h_sb), #func); \
12494+ } \
12495+} while (0)
12496+
12497+#define DySetForce(func, dst, src) do { \
12498+ AuDebugOn(!src.func); \
12499+ DyDbgInc(cnt); \
12500+ dst.func = src.func; \
12501+} while (0)
12502+
12503+#define DySetAop(func) \
12504+ DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
12505+#define DySetAopForce(func) \
12506+ DySetForce(func, dyaop->da_op, aufs_aop)
12507+
12508+static void dy_aop(struct au_dykey *key, const void *h_op,
12509+ struct super_block *h_sb __maybe_unused)
12510+{
12511+ struct au_dyaop *dyaop = (void *)key;
12512+ const struct address_space_operations *h_aop = h_op;
12513+ DyDbgDeclare(cnt);
12514+
12515+ AuDbg("%s\n", au_sbtype(h_sb));
12516+
12517+ DySetAop(writepage);
22319442 12518+ DySetAopForce(read_folio); /* force */
4a4d8108 12519+ DySetAop(writepages);
26eb093c
AM
12520+ DySetAop(dirty_folio);
12521+ DySetAop(invalidate_folio);
e37dd06a 12522+ DySetAop(readahead);
4a4d8108
AM
12523+ DySetAop(write_begin);
12524+ DySetAop(write_end);
12525+ DySetAop(bmap);
22319442
JP
12526+ DySetAop(release_folio);
12527+ DySetAop(free_folio);
7e9cd9fe 12528+ /* this one will be changed according to an aufs mount option */
4a4d8108 12529+ DySetAop(direct_IO);
82da57cb 12530+ DySetAop(migrate_folio);
26eb093c 12531+ DySetAop(launder_folio);
4a4d8108 12532+ DySetAop(is_partially_uptodate);
392086de 12533+ DySetAop(is_dirty_writeback);
4a4d8108 12534+ DySetAop(error_remove_page);
b4510431
AM
12535+ DySetAop(swap_activate);
12536+ DySetAop(swap_deactivate);
22319442 12537+ DySetAop(swap_rw);
4a4d8108
AM
12538+
12539+ DyDbgSize(cnt, *h_aop);
4a4d8108
AM
12540+}
12541+
4a4d8108
AM
12542+/* ---------------------------------------------------------------------- */
12543+
12544+static void dy_bug(struct kref *kref)
12545+{
12546+ BUG();
12547+}
12548+
12549+static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
12550+{
12551+ struct au_dykey *key, *old;
8b6a4947 12552+ struct hlist_bl_head *hbl;
b752ccd1 12553+ struct op {
4a4d8108 12554+ unsigned int sz;
b752ccd1
AM
12555+ void (*set)(struct au_dykey *key, const void *h_op,
12556+ struct super_block *h_sb __maybe_unused);
12557+ };
12558+ static const struct op a[] = {
4a4d8108
AM
12559+ [AuDy_AOP] = {
12560+ .sz = sizeof(struct au_dyaop),
b752ccd1 12561+ .set = dy_aop
4a4d8108 12562+ }
b752ccd1
AM
12563+ };
12564+ const struct op *p;
4a4d8108 12565+
8b6a4947
AM
12566+ hbl = dynop + op->dy_type;
12567+ key = dy_gfind_get(hbl, op->dy_hop);
4a4d8108
AM
12568+ if (key)
12569+ goto out_add; /* success */
12570+
12571+ p = a + op->dy_type;
12572+ key = kzalloc(p->sz, GFP_NOFS);
12573+ if (unlikely(!key)) {
12574+ key = ERR_PTR(-ENOMEM);
12575+ goto out;
12576+ }
12577+
12578+ key->dk_op.dy_hop = op->dy_hop;
12579+ kref_init(&key->dk_kref);
86dc4139 12580+ p->set(key, op->dy_hop, au_br_sb(br));
8b6a4947 12581+ old = dy_gadd(hbl, key);
4a4d8108 12582+ if (old) {
9f237c51 12583+ au_kfree_rcu(key);
4a4d8108
AM
12584+ key = old;
12585+ }
12586+
12587+out_add:
12588+ old = dy_bradd(br, key);
12589+ if (old)
12590+ /* its ref-count should never be zero here */
12591+ kref_put(&key->dk_kref, dy_bug);
12592+out:
12593+ return key;
12594+}
12595+
12596+/* ---------------------------------------------------------------------- */
12597+/*
acd2b654 12598+ * Aufs prohibits O_DIRECT by default even if the branch supports it.
c1595e42 12599+ * This behaviour is necessary to return an error from open(O_DIRECT) instead
4a4d8108
AM
12600+ * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
12601+ * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
12602+ * See the aufs manual in detail.
4a4d8108
AM
12603+ */
12604+static void dy_adx(struct au_dyaop *dyaop, int do_dx)
12605+{
7e9cd9fe 12606+ if (!do_dx)
4a4d8108 12607+ dyaop->da_op.direct_IO = NULL;
7e9cd9fe 12608+ else
4a4d8108 12609+ dyaop->da_op.direct_IO = aufs_aop.direct_IO;
4a4d8108
AM
12610+}
12611+
12612+static struct au_dyaop *dy_aget(struct au_branch *br,
12613+ const struct address_space_operations *h_aop,
12614+ int do_dx)
12615+{
12616+ struct au_dyaop *dyaop;
12617+ struct au_dynop op;
12618+
12619+ op.dy_type = AuDy_AOP;
12620+ op.dy_haop = h_aop;
12621+ dyaop = (void *)dy_get(&op, br);
12622+ if (IS_ERR(dyaop))
12623+ goto out;
12624+ dy_adx(dyaop, do_dx);
12625+
12626+out:
12627+ return dyaop;
12628+}
12629+
12630+int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12631+ struct inode *h_inode)
12632+{
12633+ int err, do_dx;
12634+ struct super_block *sb;
12635+ struct au_branch *br;
12636+ struct au_dyaop *dyaop;
12637+
12638+ AuDebugOn(!S_ISREG(h_inode->i_mode));
12639+ IiMustWriteLock(inode);
12640+
12641+ sb = inode->i_sb;
12642+ br = au_sbr(sb, bindex);
12643+ do_dx = !!au_opt_test(au_mntflags(sb), DIO);
12644+ dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
12645+ err = PTR_ERR(dyaop);
12646+ if (IS_ERR(dyaop))
12647+ /* unnecessary to call dy_fput() */
12648+ goto out;
12649+
12650+ err = 0;
12651+ inode->i_mapping->a_ops = &dyaop->da_op;
12652+
12653+out:
12654+ return err;
12655+}
12656+
b752ccd1
AM
12657+/*
12658+ * Is it safe to replace a_ops during the inode/file is in operation?
12659+ * Yes, I hope so.
12660+ */
12661+int au_dy_irefresh(struct inode *inode)
12662+{
12663+ int err;
5afbbe0d 12664+ aufs_bindex_t btop;
b752ccd1
AM
12665+ struct inode *h_inode;
12666+
12667+ err = 0;
12668+ if (S_ISREG(inode->i_mode)) {
5afbbe0d
AM
12669+ btop = au_ibtop(inode);
12670+ h_inode = au_h_iptr(inode, btop);
12671+ err = au_dy_iaop(inode, btop, h_inode);
b752ccd1
AM
12672+ }
12673+ return err;
12674+}
12675+
4a4d8108
AM
12676+void au_dy_arefresh(int do_dx)
12677+{
8b6a4947
AM
12678+ struct hlist_bl_head *hbl;
12679+ struct hlist_bl_node *pos;
4a4d8108
AM
12680+ struct au_dykey *key;
12681+
8b6a4947
AM
12682+ hbl = dynop + AuDy_AOP;
12683+ hlist_bl_lock(hbl);
12684+ hlist_bl_for_each_entry(key, pos, hbl, dk_hnode)
4a4d8108 12685+ dy_adx((void *)key, do_dx);
8b6a4947 12686+ hlist_bl_unlock(hbl);
4a4d8108
AM
12687+}
12688+
4a4d8108
AM
12689+/* ---------------------------------------------------------------------- */
12690+
12691+void __init au_dy_init(void)
12692+{
12693+ int i;
12694+
4a4d8108 12695+ for (i = 0; i < AuDyLast; i++)
8b6a4947 12696+ INIT_HLIST_BL_HEAD(dynop + i);
4a4d8108
AM
12697+}
12698+
12699+void au_dy_fin(void)
12700+{
12701+ int i;
12702+
12703+ for (i = 0; i < AuDyLast; i++)
8b6a4947 12704+ WARN_ON(!hlist_bl_empty(dynop + i));
4a4d8108 12705+}
7f207e10 12706diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
eca34b5c 12707--- /usr/share/empty/fs/aufs/dynop.h 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 12708+++ linux/fs/aufs/dynop.h 2022-11-05 23:02:18.962555950 +0100
fbc438ed 12709@@ -0,0 +1,77 @@
062440b3 12710+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 12711+/*
0dcfbb52 12712+ * Copyright (C) 2010-2022 Junjiro R. Okajima
4a4d8108 12713+ *
0dcfbb52 12714+ * This program is free software; you can redistribute it and/or modify
4a4d8108
AM
12715+ * it under the terms of the GNU General Public License as published by
12716+ * the Free Software Foundation; either version 2 of the License, or
12717+ * (at your option) any later version.
12718+ *
12719+ * This program is distributed in the hope that it will be useful,
12720+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12721+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12722+ * GNU General Public License for more details.
12723+ *
12724+ * You should have received a copy of the GNU General Public License
523b37e3 12725+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
12726+ */
12727+
12728+/*
12729+ * dynamically customizable operations (for regular files only)
12730+ */
12731+
12732+#ifndef __AUFS_DYNOP_H__
12733+#define __AUFS_DYNOP_H__
12734+
12735+#ifdef __KERNEL__
12736+
7e9cd9fe
AM
12737+#include <linux/fs.h>
12738+#include <linux/kref.h>
4a4d8108 12739+
2cbb1c4b 12740+enum {AuDy_AOP, AuDyLast};
4a4d8108
AM
12741+
12742+struct au_dynop {
12743+ int dy_type;
12744+ union {
12745+ const void *dy_hop;
12746+ const struct address_space_operations *dy_haop;
4a4d8108
AM
12747+ };
12748+};
12749+
12750+struct au_dykey {
12751+ union {
8b6a4947 12752+ struct hlist_bl_node dk_hnode;
4a4d8108
AM
12753+ struct rcu_head dk_rcu;
12754+ };
12755+ struct au_dynop dk_op;
12756+
12757+ /*
12758+ * during I am in the branch local array, kref is gotten. when the
12759+ * branch is removed, kref is put.
12760+ */
12761+ struct kref dk_kref;
12762+};
12763+
12764+/* stop unioning since their sizes are very different from each other */
12765+struct au_dyaop {
12766+ struct au_dykey da_key;
12767+ struct address_space_operations da_op; /* not const */
4a4d8108 12768+};
fbc438ed
JR
12769+/* make sure that 'struct au_dykey *' can be any type */
12770+static_assert(!offsetof(struct au_dyaop, da_key));
4a4d8108 12771+
4a4d8108
AM
12772+/* ---------------------------------------------------------------------- */
12773+
12774+/* dynop.c */
12775+struct au_branch;
12776+void au_dy_put(struct au_dykey *key);
12777+int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12778+ struct inode *h_inode);
b752ccd1 12779+int au_dy_irefresh(struct inode *inode);
4a4d8108 12780+void au_dy_arefresh(int do_dio);
4a4d8108
AM
12781+
12782+void __init au_dy_init(void);
12783+void au_dy_fin(void);
12784+
4a4d8108
AM
12785+#endif /* __KERNEL__ */
12786+#endif /* __AUFS_DYNOP_H__ */
7f207e10 12787diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
eca34b5c 12788--- /usr/share/empty/fs/aufs/export.c 1970-01-01 01:00:00.000000000 +0100
2255d0fe 12789+++ linux/fs/aufs/export.c 2022-12-17 09:21:34.796521861 +0100
f4d37d76 12790@@ -0,0 +1,830 @@
cd7a4cd9 12791+// SPDX-License-Identifier: GPL-2.0
4a4d8108 12792+/*
0dcfbb52 12793+ * Copyright (C) 2005-2022 Junjiro R. Okajima
4a4d8108 12794+ *
0dcfbb52 12795+ * This program is free software; you can redistribute it and/or modify
4a4d8108
AM
12796+ * it under the terms of the GNU General Public License as published by
12797+ * the Free Software Foundation; either version 2 of the License, or
12798+ * (at your option) any later version.
12799+ *
12800+ * This program is distributed in the hope that it will be useful,
12801+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12802+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12803+ * GNU General Public License for more details.
12804+ *
12805+ * You should have received a copy of the GNU General Public License
523b37e3 12806+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
12807+ */
12808+
12809+/*
12810+ * export via nfs
12811+ */
12812+
12813+#include <linux/exportfs.h>
7eafdf33 12814+#include <linux/fs_struct.h>
4a4d8108
AM
12815+#include <linux/nsproxy.h>
12816+#include <linux/random.h>
12817+#include <linux/writeback.h>
12818+#include "aufs.h"
12819+
12820+union conv {
12821+#ifdef CONFIG_AUFS_INO_T_64
12822+ __u32 a[2];
12823+#else
12824+ __u32 a[1];
12825+#endif
12826+ ino_t ino;
12827+};
12828+
12829+static ino_t decode_ino(__u32 *a)
12830+{
12831+ union conv u;
12832+
12833+ BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
12834+ u.a[0] = a[0];
12835+#ifdef CONFIG_AUFS_INO_T_64
12836+ u.a[1] = a[1];
12837+#endif
12838+ return u.ino;
12839+}
12840+
12841+static void encode_ino(__u32 *a, ino_t ino)
12842+{
12843+ union conv u;
12844+
12845+ u.ino = ino;
12846+ a[0] = u.a[0];
12847+#ifdef CONFIG_AUFS_INO_T_64
12848+ a[1] = u.a[1];
12849+#endif
12850+}
12851+
12852+/* NFS file handle */
12853+enum {
12854+ Fh_br_id,
12855+ Fh_sigen,
12856+#ifdef CONFIG_AUFS_INO_T_64
12857+ /* support 64bit inode number */
12858+ Fh_ino1,
12859+ Fh_ino2,
12860+ Fh_dir_ino1,
12861+ Fh_dir_ino2,
12862+#else
12863+ Fh_ino1,
12864+ Fh_dir_ino1,
12865+#endif
12866+ Fh_igen,
12867+ Fh_h_type,
12868+ Fh_tail,
12869+
12870+ Fh_ino = Fh_ino1,
12871+ Fh_dir_ino = Fh_dir_ino1
12872+};
12873+
12874+static int au_test_anon(struct dentry *dentry)
12875+{
027c5e7a 12876+ /* note: read d_flags without d_lock */
4a4d8108
AM
12877+ return !!(dentry->d_flags & DCACHE_DISCONNECTED);
12878+}
12879+
a2a7ad62
AM
12880+int au_test_nfsd(void)
12881+{
12882+ int ret;
12883+ struct task_struct *tsk = current;
12884+ char comm[sizeof(tsk->comm)];
12885+
12886+ ret = 0;
12887+ if (tsk->flags & PF_KTHREAD) {
12888+ get_task_comm(comm, tsk);
12889+ ret = !strcmp(comm, "nfsd");
12890+ }
12891+
12892+ return ret;
12893+}
12894+
4a4d8108
AM
12895+/* ---------------------------------------------------------------------- */
12896+/* inode generation external table */
12897+
b752ccd1 12898+void au_xigen_inc(struct inode *inode)
4a4d8108 12899+{
4a4d8108
AM
12900+ loff_t pos;
12901+ ssize_t sz;
12902+ __u32 igen;
12903+ struct super_block *sb;
12904+ struct au_sbinfo *sbinfo;
12905+
4a4d8108 12906+ sb = inode->i_sb;
b752ccd1 12907+ AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
1facf9fc 12908+
b752ccd1 12909+ sbinfo = au_sbi(sb);
1facf9fc 12910+ pos = inode->i_ino;
12911+ pos *= sizeof(igen);
12912+ igen = inode->i_generation + 1;
e37dd06a 12913+ sz = xino_fwrite(sbinfo->si_xigen, &igen, sizeof(igen), &pos);
1facf9fc 12914+ if (sz == sizeof(igen))
b752ccd1 12915+ return; /* success */
1facf9fc 12916+
b752ccd1 12917+ if (unlikely(sz >= 0))
1facf9fc 12918+ AuIOErr("xigen error (%zd)\n", sz);
1facf9fc 12919+}
12920+
12921+int au_xigen_new(struct inode *inode)
12922+{
12923+ int err;
12924+ loff_t pos;
12925+ ssize_t sz;
12926+ struct super_block *sb;
12927+ struct au_sbinfo *sbinfo;
12928+ struct file *file;
12929+
12930+ err = 0;
12931+ /* todo: dirty, at mount time */
12932+ if (inode->i_ino == AUFS_ROOT_INO)
12933+ goto out;
12934+ sb = inode->i_sb;
dece6358 12935+ SiMustAnyLock(sb);
1facf9fc 12936+ if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
12937+ goto out;
12938+
12939+ err = -EFBIG;
12940+ pos = inode->i_ino;
12941+ if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
12942+ AuIOErr1("too large i%lld\n", pos);
12943+ goto out;
12944+ }
12945+ pos *= sizeof(inode->i_generation);
12946+
12947+ err = 0;
12948+ sbinfo = au_sbi(sb);
12949+ file = sbinfo->si_xigen;
12950+ BUG_ON(!file);
12951+
c06a8ce3 12952+ if (vfsub_f_size_read(file)
1facf9fc 12953+ < pos + sizeof(inode->i_generation)) {
12954+ inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
e37dd06a 12955+ sz = xino_fwrite(file, &inode->i_generation,
1facf9fc 12956+ sizeof(inode->i_generation), &pos);
12957+ } else
e37dd06a 12958+ sz = xino_fread(file, &inode->i_generation,
1facf9fc 12959+ sizeof(inode->i_generation), &pos);
12960+ if (sz == sizeof(inode->i_generation))
12961+ goto out; /* success */
12962+
12963+ err = sz;
12964+ if (unlikely(sz >= 0)) {
12965+ err = -EIO;
12966+ AuIOErr("xigen error (%zd)\n", sz);
12967+ }
12968+
4f0767ce 12969+out:
1facf9fc 12970+ return err;
12971+}
12972+
062440b3 12973+int au_xigen_set(struct super_block *sb, struct path *path)
1facf9fc 12974+{
12975+ int err;
12976+ struct au_sbinfo *sbinfo;
12977+ struct file *file;
12978+
dece6358
AM
12979+ SiMustWriteLock(sb);
12980+
1facf9fc 12981+ sbinfo = au_sbi(sb);
062440b3 12982+ file = au_xino_create2(sb, path, sbinfo->si_xigen);
1facf9fc 12983+ err = PTR_ERR(file);
12984+ if (IS_ERR(file))
12985+ goto out;
12986+ err = 0;
12987+ if (sbinfo->si_xigen)
12988+ fput(sbinfo->si_xigen);
12989+ sbinfo->si_xigen = file;
12990+
4f0767ce 12991+out:
062440b3 12992+ AuTraceErr(err);
1facf9fc 12993+ return err;
12994+}
12995+
12996+void au_xigen_clr(struct super_block *sb)
12997+{
12998+ struct au_sbinfo *sbinfo;
12999+
dece6358
AM
13000+ SiMustWriteLock(sb);
13001+
1facf9fc 13002+ sbinfo = au_sbi(sb);
13003+ if (sbinfo->si_xigen) {
13004+ fput(sbinfo->si_xigen);
13005+ sbinfo->si_xigen = NULL;
13006+ }
13007+}
13008+
13009+/* ---------------------------------------------------------------------- */
13010+
13011+static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
13012+ ino_t dir_ino)
13013+{
13014+ struct dentry *dentry, *d;
13015+ struct inode *inode;
13016+ unsigned int sigen;
13017+
13018+ dentry = NULL;
13019+ inode = ilookup(sb, ino);
13020+ if (!inode)
13021+ goto out;
13022+
13023+ dentry = ERR_PTR(-ESTALE);
13024+ sigen = au_sigen(sb);
5afbbe0d 13025+ if (unlikely(au_is_bad_inode(inode)
1facf9fc 13026+ || IS_DEADDIR(inode)
537831f9 13027+ || sigen != au_iigen(inode, NULL)))
1facf9fc 13028+ goto out_iput;
13029+
13030+ dentry = NULL;
13031+ if (!dir_ino || S_ISDIR(inode->i_mode))
13032+ dentry = d_find_alias(inode);
13033+ else {
027c5e7a 13034+ spin_lock(&inode->i_lock);
c1595e42 13035+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
027c5e7a 13036+ spin_lock(&d->d_lock);
1facf9fc 13037+ if (!au_test_anon(d)
5527c038 13038+ && d_inode(d->d_parent)->i_ino == dir_ino) {
027c5e7a
AM
13039+ dentry = dget_dlock(d);
13040+ spin_unlock(&d->d_lock);
1facf9fc 13041+ break;
13042+ }
027c5e7a
AM
13043+ spin_unlock(&d->d_lock);
13044+ }
13045+ spin_unlock(&inode->i_lock);
1facf9fc 13046+ }
027c5e7a 13047+ if (unlikely(dentry && au_digen_test(dentry, sigen))) {
2cbb1c4b 13048+ /* need to refresh */
1facf9fc 13049+ dput(dentry);
2cbb1c4b 13050+ dentry = NULL;
1facf9fc 13051+ }
13052+
4f0767ce 13053+out_iput:
1facf9fc 13054+ iput(inode);
4f0767ce 13055+out:
2cbb1c4b 13056+ AuTraceErrPtr(dentry);
1facf9fc 13057+ return dentry;
13058+}
13059+
13060+/* ---------------------------------------------------------------------- */
13061+
13062+/* todo: dirty? */
13063+/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
4a4d8108
AM
13064+
13065+struct au_compare_mnt_args {
13066+ /* input */
13067+ struct super_block *sb;
13068+
13069+ /* output */
13070+ struct vfsmount *mnt;
13071+};
13072+
13073+static int au_compare_mnt(struct vfsmount *mnt, void *arg)
13074+{
13075+ struct au_compare_mnt_args *a = arg;
13076+
13077+ if (mnt->mnt_sb != a->sb)
13078+ return 0;
13079+ a->mnt = mntget(mnt);
13080+ return 1;
13081+}
13082+
1facf9fc 13083+static struct vfsmount *au_mnt_get(struct super_block *sb)
13084+{
4a4d8108 13085+ int err;
7eafdf33 13086+ struct path root;
4a4d8108
AM
13087+ struct au_compare_mnt_args args = {
13088+ .sb = sb
13089+ };
1facf9fc 13090+
7eafdf33 13091+ get_fs_root(current->fs, &root);
523b37e3 13092+ rcu_read_lock();
7eafdf33 13093+ err = iterate_mounts(au_compare_mnt, &args, root.mnt);
523b37e3 13094+ rcu_read_unlock();
7eafdf33 13095+ path_put(&root);
4a4d8108
AM
13096+ AuDebugOn(!err);
13097+ AuDebugOn(!args.mnt);
13098+ return args.mnt;
1facf9fc 13099+}
13100+
13101+struct au_nfsd_si_lock {
4a4d8108 13102+ unsigned int sigen;
027c5e7a 13103+ aufs_bindex_t bindex, br_id;
1facf9fc 13104+ unsigned char force_lock;
13105+};
13106+
027c5e7a
AM
13107+static int si_nfsd_read_lock(struct super_block *sb,
13108+ struct au_nfsd_si_lock *nsi_lock)
1facf9fc 13109+{
027c5e7a 13110+ int err;
1facf9fc 13111+ aufs_bindex_t bindex;
13112+
13113+ si_read_lock(sb, AuLock_FLUSH);
13114+
13115+ /* branch id may be wrapped around */
027c5e7a 13116+ err = 0;
1facf9fc 13117+ bindex = au_br_index(sb, nsi_lock->br_id);
13118+ if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
13119+ goto out; /* success */
13120+
027c5e7a
AM
13121+ err = -ESTALE;
13122+ bindex = -1;
1facf9fc 13123+ if (!nsi_lock->force_lock)
13124+ si_read_unlock(sb);
1facf9fc 13125+
4f0767ce 13126+out:
027c5e7a
AM
13127+ nsi_lock->bindex = bindex;
13128+ return err;
1facf9fc 13129+}
13130+
13131+struct find_name_by_ino {
392086de 13132+ struct dir_context ctx;
1facf9fc 13133+ int called, found;
13134+ ino_t ino;
13135+ char *name;
13136+ int namelen;
13137+};
13138+
2255d0fe 13139+static bool
392086de
AM
13140+find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
13141+ loff_t offset, u64 ino, unsigned int d_type)
1facf9fc 13142+{
392086de
AM
13143+ struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
13144+ ctx);
1facf9fc 13145+
13146+ a->called++;
13147+ if (a->ino != ino)
2255d0fe 13148+ return true;
1facf9fc 13149+
13150+ memcpy(a->name, name, namelen);
13151+ a->namelen = namelen;
13152+ a->found = 1;
2255d0fe 13153+ return false;
1facf9fc 13154+}
13155+
13156+static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
13157+ struct au_nfsd_si_lock *nsi_lock)
13158+{
13159+ struct dentry *dentry, *parent;
13160+ struct file *file;
13161+ struct inode *dir;
392086de
AM
13162+ struct find_name_by_ino arg = {
13163+ .ctx = {
2000de60 13164+ .actor = find_name_by_ino
392086de
AM
13165+ }
13166+ };
1facf9fc 13167+ int err;
13168+
13169+ parent = path->dentry;
13170+ if (nsi_lock)
13171+ si_read_unlock(parent->d_sb);
4a4d8108 13172+ file = vfsub_dentry_open(path, au_dir_roflags);
1facf9fc 13173+ dentry = (void *)file;
13174+ if (IS_ERR(file))
13175+ goto out;
13176+
13177+ dentry = ERR_PTR(-ENOMEM);
537831f9 13178+ arg.name = (void *)__get_free_page(GFP_NOFS);
1facf9fc 13179+ if (unlikely(!arg.name))
13180+ goto out_file;
13181+ arg.ino = ino;
13182+ arg.found = 0;
13183+ do {
13184+ arg.called = 0;
13185+ /* smp_mb(); */
392086de 13186+ err = vfsub_iterate_dir(file, &arg.ctx);
1facf9fc 13187+ } while (!err && !arg.found && arg.called);
13188+ dentry = ERR_PTR(err);
13189+ if (unlikely(err))
13190+ goto out_name;
1716fcea
AM
13191+ /* instead of ENOENT */
13192+ dentry = ERR_PTR(-ESTALE);
1facf9fc 13193+ if (!arg.found)
13194+ goto out_name;
13195+
b4510431 13196+ /* do not call vfsub_lkup_one() */
5527c038 13197+ dir = d_inode(parent);
758e9dad 13198+ dentry = vfsub_lookup_one_len_unlocked(arg.name, path, arg.namelen);
1facf9fc 13199+ AuTraceErrPtr(dentry);
13200+ if (IS_ERR(dentry))
13201+ goto out_name;
13202+ AuDebugOn(au_test_anon(dentry));
5527c038 13203+ if (unlikely(d_really_is_negative(dentry))) {
1facf9fc 13204+ dput(dentry);
13205+ dentry = ERR_PTR(-ENOENT);
13206+ }
13207+
4f0767ce 13208+out_name:
1c60b727 13209+ free_page((unsigned long)arg.name);
4f0767ce 13210+out_file:
1facf9fc 13211+ fput(file);
4f0767ce 13212+out:
1facf9fc 13213+ if (unlikely(nsi_lock
13214+ && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
13215+ if (!IS_ERR(dentry)) {
13216+ dput(dentry);
13217+ dentry = ERR_PTR(-ESTALE);
13218+ }
13219+ AuTraceErrPtr(dentry);
13220+ return dentry;
13221+}
13222+
13223+static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
13224+ ino_t dir_ino,
13225+ struct au_nfsd_si_lock *nsi_lock)
13226+{
13227+ struct dentry *dentry;
13228+ struct path path;
13229+
13230+ if (dir_ino != AUFS_ROOT_INO) {
13231+ path.dentry = decode_by_ino(sb, dir_ino, 0);
13232+ dentry = path.dentry;
13233+ if (!path.dentry || IS_ERR(path.dentry))
13234+ goto out;
13235+ AuDebugOn(au_test_anon(path.dentry));
13236+ } else
13237+ path.dentry = dget(sb->s_root);
13238+
13239+ path.mnt = au_mnt_get(sb);
13240+ dentry = au_lkup_by_ino(&path, ino, nsi_lock);
13241+ path_put(&path);
13242+
4f0767ce 13243+out:
1facf9fc 13244+ AuTraceErrPtr(dentry);
13245+ return dentry;
13246+}
13247+
13248+/* ---------------------------------------------------------------------- */
13249+
13250+static int h_acceptable(void *expv, struct dentry *dentry)
13251+{
13252+ return 1;
13253+}
13254+
13255+static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
13256+ char *buf, int len, struct super_block *sb)
13257+{
13258+ char *p;
13259+ int n;
13260+ struct path path;
13261+
13262+ p = d_path(h_rootpath, buf, len);
13263+ if (IS_ERR(p))
13264+ goto out;
13265+ n = strlen(p);
13266+
13267+ path.mnt = h_rootpath->mnt;
13268+ path.dentry = h_parent;
13269+ p = d_path(&path, buf, len);
13270+ if (IS_ERR(p))
13271+ goto out;
13272+ if (n != 1)
13273+ p += n;
13274+
13275+ path.mnt = au_mnt_get(sb);
13276+ path.dentry = sb->s_root;
13277+ p = d_path(&path, buf, len - strlen(p));
13278+ mntput(path.mnt);
13279+ if (IS_ERR(p))
13280+ goto out;
13281+ if (n != 1)
13282+ p[strlen(p)] = '/';
13283+
4f0767ce 13284+out:
1facf9fc 13285+ AuTraceErrPtr(p);
13286+ return p;
13287+}
13288+
13289+static
027c5e7a
AM
13290+struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
13291+ int fh_len, struct au_nfsd_si_lock *nsi_lock)
1facf9fc 13292+{
13293+ struct dentry *dentry, *h_parent, *root;
13294+ struct super_block *h_sb;
13295+ char *pathname, *p;
13296+ struct vfsmount *h_mnt;
13297+ struct au_branch *br;
13298+ int err;
13299+ struct path path;
13300+
027c5e7a 13301+ br = au_sbr(sb, nsi_lock->bindex);
86dc4139 13302+ h_mnt = au_br_mnt(br);
1facf9fc 13303+ h_sb = h_mnt->mnt_sb;
13304+ /* todo: call lower fh_to_dentry()? fh_to_parent()? */
5afbbe0d 13305+ lockdep_off();
1facf9fc 13306+ h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
13307+ fh_len - Fh_tail, fh[Fh_h_type],
13308+ h_acceptable, /*context*/NULL);
5afbbe0d 13309+ lockdep_on();
1facf9fc 13310+ dentry = h_parent;
13311+ if (unlikely(!h_parent || IS_ERR(h_parent))) {
13312+ AuWarn1("%s decode_fh failed, %ld\n",
13313+ au_sbtype(h_sb), PTR_ERR(h_parent));
13314+ goto out;
13315+ }
13316+ dentry = NULL;
13317+ if (unlikely(au_test_anon(h_parent))) {
13318+ AuWarn1("%s decode_fh returned a disconnected dentry\n",
13319+ au_sbtype(h_sb));
13320+ goto out_h_parent;
13321+ }
13322+
13323+ dentry = ERR_PTR(-ENOMEM);
13324+ pathname = (void *)__get_free_page(GFP_NOFS);
13325+ if (unlikely(!pathname))
13326+ goto out_h_parent;
13327+
13328+ root = sb->s_root;
13329+ path.mnt = h_mnt;
13330+ di_read_lock_parent(root, !AuLock_IR);
027c5e7a 13331+ path.dentry = au_h_dptr(root, nsi_lock->bindex);
1facf9fc 13332+ di_read_unlock(root, !AuLock_IR);
13333+ p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
13334+ dentry = (void *)p;
13335+ if (IS_ERR(p))
13336+ goto out_pathname;
13337+
13338+ si_read_unlock(sb);
13339+ err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
13340+ dentry = ERR_PTR(err);
13341+ if (unlikely(err))
13342+ goto out_relock;
13343+
13344+ dentry = ERR_PTR(-ENOENT);
13345+ AuDebugOn(au_test_anon(path.dentry));
5527c038 13346+ if (unlikely(d_really_is_negative(path.dentry)))
1facf9fc 13347+ goto out_path;
13348+
5527c038 13349+ if (ino != d_inode(path.dentry)->i_ino)
1facf9fc 13350+ dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
13351+ else
13352+ dentry = dget(path.dentry);
13353+
4f0767ce 13354+out_path:
1facf9fc 13355+ path_put(&path);
4f0767ce 13356+out_relock:
1facf9fc 13357+ if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
13358+ if (!IS_ERR(dentry)) {
13359+ dput(dentry);
13360+ dentry = ERR_PTR(-ESTALE);
13361+ }
4f0767ce 13362+out_pathname:
1c60b727 13363+ free_page((unsigned long)pathname);
4f0767ce 13364+out_h_parent:
1facf9fc 13365+ dput(h_parent);
4f0767ce 13366+out:
1facf9fc 13367+ AuTraceErrPtr(dentry);
13368+ return dentry;
13369+}
13370+
13371+/* ---------------------------------------------------------------------- */
13372+
13373+static struct dentry *
13374+aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
13375+ int fh_type)
13376+{
13377+ struct dentry *dentry;
13378+ __u32 *fh = fid->raw;
027c5e7a 13379+ struct au_branch *br;
1facf9fc 13380+ ino_t ino, dir_ino;
1facf9fc 13381+ struct au_nfsd_si_lock nsi_lock = {
1facf9fc 13382+ .force_lock = 0
13383+ };
13384+
1facf9fc 13385+ dentry = ERR_PTR(-ESTALE);
4a4d8108
AM
13386+ /* it should never happen, but the file handle is unreliable */
13387+ if (unlikely(fh_len < Fh_tail))
13388+ goto out;
13389+ nsi_lock.sigen = fh[Fh_sigen];
13390+ nsi_lock.br_id = fh[Fh_br_id];
13391+
1facf9fc 13392+ /* branch id may be wrapped around */
027c5e7a
AM
13393+ br = NULL;
13394+ if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
1facf9fc 13395+ goto out;
13396+ nsi_lock.force_lock = 1;
13397+
13398+ /* is this inode still cached? */
13399+ ino = decode_ino(fh + Fh_ino);
4a4d8108
AM
13400+ /* it should never happen */
13401+ if (unlikely(ino == AUFS_ROOT_INO))
8cdd5066 13402+ goto out_unlock;
4a4d8108 13403+
1facf9fc 13404+ dir_ino = decode_ino(fh + Fh_dir_ino);
13405+ dentry = decode_by_ino(sb, ino, dir_ino);
13406+ if (IS_ERR(dentry))
13407+ goto out_unlock;
13408+ if (dentry)
13409+ goto accept;
13410+
13411+ /* is the parent dir cached? */
027c5e7a 13412+ br = au_sbr(sb, nsi_lock.bindex);
acd2b654 13413+ au_lcnt_inc(&br->br_nfiles);
1facf9fc 13414+ dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
13415+ if (IS_ERR(dentry))
13416+ goto out_unlock;
13417+ if (dentry)
13418+ goto accept;
13419+
13420+ /* lookup path */
027c5e7a 13421+ dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
1facf9fc 13422+ if (IS_ERR(dentry))
13423+ goto out_unlock;
13424+ if (unlikely(!dentry))
13425+ /* todo?: make it ESTALE */
13426+ goto out_unlock;
13427+
4f0767ce 13428+accept:
027c5e7a 13429+ if (!au_digen_test(dentry, au_sigen(sb))
5527c038 13430+ && d_inode(dentry)->i_generation == fh[Fh_igen])
1facf9fc 13431+ goto out_unlock; /* success */
13432+
13433+ dput(dentry);
13434+ dentry = ERR_PTR(-ESTALE);
4f0767ce 13435+out_unlock:
027c5e7a 13436+ if (br)
acd2b654 13437+ au_lcnt_dec(&br->br_nfiles);
1facf9fc 13438+ si_read_unlock(sb);
4f0767ce 13439+out:
1facf9fc 13440+ AuTraceErrPtr(dentry);
13441+ return dentry;
13442+}
13443+
13444+#if 0 /* reserved for future use */
13445+/* support subtreecheck option */
13446+static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
13447+ int fh_len, int fh_type)
13448+{
13449+ struct dentry *parent;
13450+ __u32 *fh = fid->raw;
13451+ ino_t dir_ino;
13452+
13453+ dir_ino = decode_ino(fh + Fh_dir_ino);
13454+ parent = decode_by_ino(sb, dir_ino, 0);
13455+ if (IS_ERR(parent))
13456+ goto out;
13457+ if (!parent)
13458+ parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
13459+ dir_ino, fh, fh_len);
13460+
4f0767ce 13461+out:
1facf9fc 13462+ AuTraceErrPtr(parent);
13463+ return parent;
13464+}
13465+#endif
13466+
13467+/* ---------------------------------------------------------------------- */
13468+
0c3ec466
AM
13469+static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
13470+ struct inode *dir)
1facf9fc 13471+{
13472+ int err;
0c3ec466 13473+ aufs_bindex_t bindex;
1facf9fc 13474+ struct super_block *sb, *h_sb;
0c3ec466
AM
13475+ struct dentry *dentry, *parent, *h_parent;
13476+ struct inode *h_dir;
1facf9fc 13477+ struct au_branch *br;
13478+
1facf9fc 13479+ err = -ENOSPC;
13480+ if (unlikely(*max_len <= Fh_tail)) {
13481+ AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
13482+ goto out;
13483+ }
13484+
13485+ err = FILEID_ROOT;
0c3ec466
AM
13486+ if (inode->i_ino == AUFS_ROOT_INO) {
13487+ AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
1facf9fc 13488+ goto out;
13489+ }
13490+
1facf9fc 13491+ h_parent = NULL;
0c3ec466
AM
13492+ sb = inode->i_sb;
13493+ err = si_read_lock(sb, AuLock_FLUSH);
027c5e7a
AM
13494+ if (unlikely(err))
13495+ goto out;
13496+
1facf9fc 13497+#ifdef CONFIG_AUFS_DEBUG
13498+ if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13499+ AuWarn1("NFS-exporting requires xino\n");
13500+#endif
027c5e7a 13501+ err = -EIO;
0c3ec466
AM
13502+ parent = NULL;
13503+ ii_read_lock_child(inode);
5afbbe0d 13504+ bindex = au_ibtop(inode);
0c3ec466 13505+ if (!dir) {
c1595e42 13506+ dentry = d_find_any_alias(inode);
0c3ec466
AM
13507+ if (unlikely(!dentry))
13508+ goto out_unlock;
13509+ AuDebugOn(au_test_anon(dentry));
13510+ parent = dget_parent(dentry);
13511+ dput(dentry);
13512+ if (unlikely(!parent))
13513+ goto out_unlock;
5527c038
JR
13514+ if (d_really_is_positive(parent))
13515+ dir = d_inode(parent);
1facf9fc 13516+ }
0c3ec466
AM
13517+
13518+ ii_read_lock_parent(dir);
13519+ h_dir = au_h_iptr(dir, bindex);
13520+ ii_read_unlock(dir);
13521+ if (unlikely(!h_dir))
13522+ goto out_parent;
c1595e42 13523+ h_parent = d_find_any_alias(h_dir);
1facf9fc 13524+ if (unlikely(!h_parent))
0c3ec466 13525+ goto out_hparent;
1facf9fc 13526+
13527+ err = -EPERM;
13528+ br = au_sbr(sb, bindex);
86dc4139 13529+ h_sb = au_br_sb(br);
1facf9fc 13530+ if (unlikely(!h_sb->s_export_op)) {
13531+ AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
0c3ec466 13532+ goto out_hparent;
1facf9fc 13533+ }
13534+
13535+ fh[Fh_br_id] = br->br_id;
13536+ fh[Fh_sigen] = au_sigen(sb);
13537+ encode_ino(fh + Fh_ino, inode->i_ino);
0c3ec466 13538+ encode_ino(fh + Fh_dir_ino, dir->i_ino);
1facf9fc 13539+ fh[Fh_igen] = inode->i_generation;
13540+
13541+ *max_len -= Fh_tail;
13542+ fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
13543+ max_len,
13544+ /*connectable or subtreecheck*/0);
13545+ err = fh[Fh_h_type];
13546+ *max_len += Fh_tail;
13547+ /* todo: macros? */
1716fcea 13548+ if (err != FILEID_INVALID)
1facf9fc 13549+ err = 99;
13550+ else
13551+ AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
13552+
0c3ec466 13553+out_hparent:
1facf9fc 13554+ dput(h_parent);
0c3ec466 13555+out_parent:
1facf9fc 13556+ dput(parent);
0c3ec466
AM
13557+out_unlock:
13558+ ii_read_unlock(inode);
13559+ si_read_unlock(sb);
4f0767ce 13560+out:
1facf9fc 13561+ if (unlikely(err < 0))
1716fcea 13562+ err = FILEID_INVALID;
1facf9fc 13563+ return err;
13564+}
13565+
13566+/* ---------------------------------------------------------------------- */
13567+
4a4d8108
AM
13568+static int aufs_commit_metadata(struct inode *inode)
13569+{
13570+ int err;
13571+ aufs_bindex_t bindex;
13572+ struct super_block *sb;
13573+ struct inode *h_inode;
13574+ int (*f)(struct inode *inode);
13575+
13576+ sb = inode->i_sb;
e49829fe 13577+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
4a4d8108 13578+ ii_write_lock_child(inode);
5afbbe0d 13579+ bindex = au_ibtop(inode);
4a4d8108
AM
13580+ AuDebugOn(bindex < 0);
13581+ h_inode = au_h_iptr(inode, bindex);
13582+
13583+ f = h_inode->i_sb->s_export_op->commit_metadata;
13584+ if (f)
13585+ err = f(h_inode);
758e9dad
AM
13586+ else
13587+ err = sync_inode_metadata(h_inode, /*wait*/1);
4a4d8108
AM
13588+
13589+ au_cpup_attr_timesizes(inode);
13590+ ii_write_unlock(inode);
13591+ si_read_unlock(sb);
13592+ return err;
13593+}
13594+
13595+/* ---------------------------------------------------------------------- */
13596+
1facf9fc 13597+static struct export_operations aufs_export_op = {
4a4d8108 13598+ .fh_to_dentry = aufs_fh_to_dentry,
1facf9fc 13599+ /* .fh_to_parent = aufs_fh_to_parent, */
4a4d8108
AM
13600+ .encode_fh = aufs_encode_fh,
13601+ .commit_metadata = aufs_commit_metadata
1facf9fc 13602+};
13603+
13604+void au_export_init(struct super_block *sb)
13605+{
13606+ struct au_sbinfo *sbinfo;
13607+ __u32 u;
13608+
5afbbe0d
AM
13609+ BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
13610+ && IS_MODULE(CONFIG_EXPORTFS),
13611+ AUFS_NAME ": unsupported configuration "
13612+ "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
13613+
1facf9fc 13614+ sb->s_export_op = &aufs_export_op;
13615+ sbinfo = au_sbi(sb);
13616+ sbinfo->si_xigen = NULL;
13617+ get_random_bytes(&u, sizeof(u));
13618+ BUILD_BUG_ON(sizeof(u) != sizeof(int));
13619+ atomic_set(&sbinfo->si_xigen_next, u);
13620+}
076b876e 13621diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
eca34b5c 13622--- /usr/share/empty/fs/aufs/fhsm.c 1970-01-01 01:00:00.000000000 +0100
0dcfbb52
AM
13623+++ linux/fs/aufs/fhsm.c 2022-11-05 23:02:18.962555950 +0100
13624@@ -0,0 +1,426 @@
cd7a4cd9 13625+// SPDX-License-Identifier: GPL-2.0
076b876e 13626+/*
0dcfbb52 13627+ * Copyright (C) 2011-2022 Junjiro R. Okajima
076b876e 13628+ *
0dcfbb52 13629+ * This program is free software; you can redistribute it and/or modify
076b876e
AM
13630+ * it under the terms of the GNU General Public License as published by
13631+ * the Free Software Foundation; either version 2 of the License, or
13632+ * (at your option) any later version.
13633+ *
13634+ * This program is distributed in the hope that it will be useful,
13635+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13636+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13637+ * GNU General Public License for more details.
13638+ *
13639+ * You should have received a copy of the GNU General Public License
0dcfbb52 13640+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
076b876e
AM
13641+ */
13642+
13643+/*
13644+ * File-based Hierarchy Storage Management
13645+ */
13646+
13647+#include <linux/anon_inodes.h>
13648+#include <linux/poll.h>
13649+#include <linux/seq_file.h>
13650+#include <linux/statfs.h>
13651+#include "aufs.h"
13652+
c1595e42
JR
13653+static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
13654+{
13655+ struct au_sbinfo *sbinfo;
13656+ struct au_fhsm *fhsm;
13657+
13658+ SiMustAnyLock(sb);
13659+
13660+ sbinfo = au_sbi(sb);
13661+ fhsm = &sbinfo->si_fhsm;
13662+ AuDebugOn(!fhsm);
13663+ return fhsm->fhsm_bottom;
13664+}
13665+
13666+void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
13667+{
13668+ struct au_sbinfo *sbinfo;
13669+ struct au_fhsm *fhsm;
13670+
13671+ SiMustWriteLock(sb);
13672+
13673+ sbinfo = au_sbi(sb);
13674+ fhsm = &sbinfo->si_fhsm;
13675+ AuDebugOn(!fhsm);
13676+ fhsm->fhsm_bottom = bindex;
13677+}
13678+
13679+/* ---------------------------------------------------------------------- */
13680+
076b876e
AM
13681+static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
13682+{
13683+ struct au_br_fhsm *bf;
13684+
13685+ bf = br->br_fhsm;
13686+ MtxMustLock(&bf->bf_lock);
13687+
13688+ return !bf->bf_readable
13689+ || time_after(jiffies,
13690+ bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
13691+}
13692+
13693+/* ---------------------------------------------------------------------- */
13694+
13695+static void au_fhsm_notify(struct super_block *sb, int val)
13696+{
13697+ struct au_sbinfo *sbinfo;
13698+ struct au_fhsm *fhsm;
13699+
13700+ SiMustAnyLock(sb);
13701+
13702+ sbinfo = au_sbi(sb);
13703+ fhsm = &sbinfo->si_fhsm;
13704+ if (au_fhsm_pid(fhsm)
13705+ && atomic_read(&fhsm->fhsm_readable) != -1) {
13706+ atomic_set(&fhsm->fhsm_readable, val);
13707+ if (val)
13708+ wake_up(&fhsm->fhsm_wqh);
13709+ }
13710+}
13711+
13712+static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
13713+ struct aufs_stfs *rstfs, int do_lock, int do_notify)
13714+{
13715+ int err;
13716+ struct au_branch *br;
13717+ struct au_br_fhsm *bf;
13718+
13719+ br = au_sbr(sb, bindex);
13720+ AuDebugOn(au_br_rdonly(br));
13721+ bf = br->br_fhsm;
13722+ AuDebugOn(!bf);
13723+
13724+ if (do_lock)
13725+ mutex_lock(&bf->bf_lock);
13726+ else
13727+ MtxMustLock(&bf->bf_lock);
13728+
13729+ /* sb->s_root for NFS is unreliable */
13730+ err = au_br_stfs(br, &bf->bf_stfs);
13731+ if (unlikely(err)) {
13732+ AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
13733+ goto out;
13734+ }
13735+
13736+ bf->bf_jiffy = jiffies;
13737+ bf->bf_readable = 1;
13738+ if (do_notify)
13739+ au_fhsm_notify(sb, /*val*/1);
13740+ if (rstfs)
13741+ *rstfs = bf->bf_stfs;
13742+
13743+out:
13744+ if (do_lock)
13745+ mutex_unlock(&bf->bf_lock);
13746+ au_fhsm_notify(sb, /*val*/1);
13747+
13748+ return err;
13749+}
13750+
13751+void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
13752+{
13753+ int err;
076b876e
AM
13754+ struct au_sbinfo *sbinfo;
13755+ struct au_fhsm *fhsm;
13756+ struct au_branch *br;
13757+ struct au_br_fhsm *bf;
13758+
13759+ AuDbg("b%d, force %d\n", bindex, force);
13760+ SiMustAnyLock(sb);
13761+
13762+ sbinfo = au_sbi(sb);
13763+ fhsm = &sbinfo->si_fhsm;
c1595e42
JR
13764+ if (!au_ftest_si(sbinfo, FHSM)
13765+ || fhsm->fhsm_bottom == bindex)
076b876e
AM
13766+ return;
13767+
13768+ br = au_sbr(sb, bindex);
13769+ bf = br->br_fhsm;
13770+ AuDebugOn(!bf);
13771+ mutex_lock(&bf->bf_lock);
13772+ if (force
13773+ || au_fhsm_pid(fhsm)
13774+ || au_fhsm_test_jiffy(sbinfo, br))
13775+ err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
13776+ /*do_notify*/1);
13777+ mutex_unlock(&bf->bf_lock);
13778+}
13779+
13780+void au_fhsm_wrote_all(struct super_block *sb, int force)
13781+{
5afbbe0d 13782+ aufs_bindex_t bindex, bbot;
076b876e
AM
13783+ struct au_branch *br;
13784+
13785+ /* exclude the bottom */
5afbbe0d
AM
13786+ bbot = au_fhsm_bottom(sb);
13787+ for (bindex = 0; bindex < bbot; bindex++) {
076b876e
AM
13788+ br = au_sbr(sb, bindex);
13789+ if (au_br_fhsm(br->br_perm))
13790+ au_fhsm_wrote(sb, bindex, force);
13791+ }
13792+}
13793+
13794+/* ---------------------------------------------------------------------- */
13795+
be118d29 13796+static __poll_t au_fhsm_poll(struct file *file, struct poll_table_struct *wait)
076b876e 13797+{
be118d29 13798+ __poll_t mask;
076b876e
AM
13799+ struct au_sbinfo *sbinfo;
13800+ struct au_fhsm *fhsm;
13801+
13802+ mask = 0;
13803+ sbinfo = file->private_data;
13804+ fhsm = &sbinfo->si_fhsm;
13805+ poll_wait(file, &fhsm->fhsm_wqh, wait);
13806+ if (atomic_read(&fhsm->fhsm_readable))
be118d29 13807+ mask = EPOLLIN /* | EPOLLRDNORM */;
076b876e 13808+
b00004a5
AM
13809+ if (!mask)
13810+ AuDbg("mask 0x%x\n", mask);
076b876e
AM
13811+ return mask;
13812+}
13813+
13814+static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
13815+ struct aufs_stfs *stfs, __s16 brid)
13816+{
13817+ int err;
13818+
13819+ err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
13820+ if (!err)
13821+ err = __put_user(brid, &stbr->brid);
13822+ if (unlikely(err))
13823+ err = -EFAULT;
13824+
13825+ return err;
13826+}
13827+
13828+static ssize_t au_fhsm_do_read(struct super_block *sb,
13829+ struct aufs_stbr __user *stbr, size_t count)
13830+{
13831+ ssize_t err;
13832+ int nstbr;
5afbbe0d 13833+ aufs_bindex_t bindex, bbot;
076b876e
AM
13834+ struct au_branch *br;
13835+ struct au_br_fhsm *bf;
13836+
13837+ /* except the bottom branch */
13838+ err = 0;
13839+ nstbr = 0;
5afbbe0d
AM
13840+ bbot = au_fhsm_bottom(sb);
13841+ for (bindex = 0; !err && bindex < bbot; bindex++) {
076b876e
AM
13842+ br = au_sbr(sb, bindex);
13843+ if (!au_br_fhsm(br->br_perm))
13844+ continue;
13845+
13846+ bf = br->br_fhsm;
13847+ mutex_lock(&bf->bf_lock);
13848+ if (bf->bf_readable) {
13849+ err = -EFAULT;
13850+ if (count >= sizeof(*stbr))
13851+ err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
13852+ br->br_id);
13853+ if (!err) {
13854+ bf->bf_readable = 0;
13855+ count -= sizeof(*stbr);
13856+ nstbr++;
13857+ }
13858+ }
13859+ mutex_unlock(&bf->bf_lock);
13860+ }
13861+ if (!err)
13862+ err = sizeof(*stbr) * nstbr;
13863+
13864+ return err;
13865+}
13866+
13867+static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
13868+ loff_t *pos)
13869+{
13870+ ssize_t err;
13871+ int readable;
5afbbe0d 13872+ aufs_bindex_t nfhsm, bindex, bbot;
076b876e
AM
13873+ struct au_sbinfo *sbinfo;
13874+ struct au_fhsm *fhsm;
13875+ struct au_branch *br;
13876+ struct super_block *sb;
13877+
13878+ err = 0;
13879+ sbinfo = file->private_data;
13880+ fhsm = &sbinfo->si_fhsm;
13881+need_data:
13882+ spin_lock_irq(&fhsm->fhsm_wqh.lock);
13883+ if (!atomic_read(&fhsm->fhsm_readable)) {
13884+ if (vfsub_file_flags(file) & O_NONBLOCK)
13885+ err = -EAGAIN;
13886+ else
13887+ err = wait_event_interruptible_locked_irq
13888+ (fhsm->fhsm_wqh,
13889+ atomic_read(&fhsm->fhsm_readable));
13890+ }
13891+ spin_unlock_irq(&fhsm->fhsm_wqh.lock);
13892+ if (unlikely(err))
13893+ goto out;
13894+
13895+ /* sb may already be dead */
13896+ au_rw_read_lock(&sbinfo->si_rwsem);
13897+ readable = atomic_read(&fhsm->fhsm_readable);
13898+ if (readable > 0) {
13899+ sb = sbinfo->si_sb;
13900+ AuDebugOn(!sb);
13901+ /* exclude the bottom branch */
13902+ nfhsm = 0;
5afbbe0d
AM
13903+ bbot = au_fhsm_bottom(sb);
13904+ for (bindex = 0; bindex < bbot; bindex++) {
076b876e
AM
13905+ br = au_sbr(sb, bindex);
13906+ if (au_br_fhsm(br->br_perm))
13907+ nfhsm++;
13908+ }
13909+ err = -EMSGSIZE;
13910+ if (nfhsm * sizeof(struct aufs_stbr) <= count) {
13911+ atomic_set(&fhsm->fhsm_readable, 0);
13912+ err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
13913+ count);
13914+ }
13915+ }
13916+ au_rw_read_unlock(&sbinfo->si_rwsem);
13917+ if (!readable)
13918+ goto need_data;
13919+
13920+out:
13921+ return err;
13922+}
13923+
13924+static int au_fhsm_release(struct inode *inode, struct file *file)
13925+{
13926+ struct au_sbinfo *sbinfo;
13927+ struct au_fhsm *fhsm;
13928+
13929+ /* sb may already be dead */
13930+ sbinfo = file->private_data;
13931+ fhsm = &sbinfo->si_fhsm;
13932+ spin_lock(&fhsm->fhsm_spin);
13933+ fhsm->fhsm_pid = 0;
13934+ spin_unlock(&fhsm->fhsm_spin);
13935+ kobject_put(&sbinfo->si_kobj);
13936+
13937+ return 0;
13938+}
13939+
13940+static const struct file_operations au_fhsm_fops = {
13941+ .owner = THIS_MODULE,
13942+ .llseek = noop_llseek,
13943+ .read = au_fhsm_read,
13944+ .poll = au_fhsm_poll,
13945+ .release = au_fhsm_release
13946+};
13947+
13948+int au_fhsm_fd(struct super_block *sb, int oflags)
13949+{
13950+ int err, fd;
13951+ struct au_sbinfo *sbinfo;
13952+ struct au_fhsm *fhsm;
13953+
13954+ err = -EPERM;
13955+ if (unlikely(!capable(CAP_SYS_ADMIN)))
13956+ goto out;
13957+
13958+ err = -EINVAL;
13959+ if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
13960+ goto out;
13961+
13962+ err = 0;
13963+ sbinfo = au_sbi(sb);
13964+ fhsm = &sbinfo->si_fhsm;
13965+ spin_lock(&fhsm->fhsm_spin);
13966+ if (!fhsm->fhsm_pid)
13967+ fhsm->fhsm_pid = current->pid;
13968+ else
13969+ err = -EBUSY;
13970+ spin_unlock(&fhsm->fhsm_spin);
13971+ if (unlikely(err))
13972+ goto out;
13973+
13974+ oflags |= O_RDONLY;
13975+ /* oflags |= FMODE_NONOTIFY; */
13976+ fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
13977+ err = fd;
13978+ if (unlikely(fd < 0))
13979+ goto out_pid;
13980+
acd2b654 13981+ /* succeed regardless 'fhsm' status */
076b876e
AM
13982+ kobject_get(&sbinfo->si_kobj);
13983+ si_noflush_read_lock(sb);
13984+ if (au_ftest_si(sbinfo, FHSM))
13985+ au_fhsm_wrote_all(sb, /*force*/0);
13986+ si_read_unlock(sb);
13987+ goto out; /* success */
13988+
13989+out_pid:
13990+ spin_lock(&fhsm->fhsm_spin);
13991+ fhsm->fhsm_pid = 0;
13992+ spin_unlock(&fhsm->fhsm_spin);
13993+out:
13994+ AuTraceErr(err);
13995+ return err;
13996+}
13997+
13998+/* ---------------------------------------------------------------------- */
13999+
14000+int au_fhsm_br_alloc(struct au_branch *br)
14001+{
14002+ int err;
14003+
14004+ err = 0;
14005+ br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
14006+ if (br->br_fhsm)
14007+ au_br_fhsm_init(br->br_fhsm);
14008+ else
14009+ err = -ENOMEM;
14010+
14011+ return err;
14012+}
14013+
14014+/* ---------------------------------------------------------------------- */
14015+
14016+void au_fhsm_fin(struct super_block *sb)
14017+{
14018+ au_fhsm_notify(sb, /*val*/-1);
14019+}
14020+
14021+void au_fhsm_init(struct au_sbinfo *sbinfo)
14022+{
14023+ struct au_fhsm *fhsm;
14024+
14025+ fhsm = &sbinfo->si_fhsm;
14026+ spin_lock_init(&fhsm->fhsm_spin);
14027+ init_waitqueue_head(&fhsm->fhsm_wqh);
14028+ atomic_set(&fhsm->fhsm_readable, 0);
14029+ fhsm->fhsm_expire
14030+ = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
c1595e42 14031+ fhsm->fhsm_bottom = -1;
076b876e
AM
14032+}
14033+
14034+void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
14035+{
14036+ sbinfo->si_fhsm.fhsm_expire
14037+ = msecs_to_jiffies(sec * MSEC_PER_SEC);
14038+}
14039+
14040+void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
14041+{
14042+ unsigned int u;
14043+
14044+ if (!au_ftest_si(sbinfo, FHSM))
14045+ return;
14046+
14047+ u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
14048+ if (u != AUFS_FHSM_CACHE_DEF_SEC)
14049+ seq_printf(seq, ",fhsm_sec=%u", u);
14050+}
7f207e10 14051diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
eca34b5c 14052--- /usr/share/empty/fs/aufs/file.c 1970-01-01 01:00:00.000000000 +0100
2255d0fe
AM
14053+++ linux/fs/aufs/file.c 2022-12-17 09:21:34.799855195 +0100
14054@@ -0,0 +1,860 @@
cd7a4cd9 14055+// SPDX-License-Identifier: GPL-2.0
1facf9fc 14056+/*
0dcfbb52 14057+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 14058+ *
0dcfbb52 14059+ * This program is free software; you can redistribute it and/or modify
1facf9fc 14060+ * it under the terms of the GNU General Public License as published by
14061+ * the Free Software Foundation; either version 2 of the License, or
14062+ * (at your option) any later version.
dece6358
AM
14063+ *
14064+ * This program is distributed in the hope that it will be useful,
14065+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14066+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14067+ * GNU General Public License for more details.
14068+ *
14069+ * You should have received a copy of the GNU General Public License
523b37e3 14070+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 14071+ */
14072+
14073+/*
4a4d8108 14074+ * handling file/dir, and address_space operation
1facf9fc 14075+ */
14076+
7eafdf33
AM
14077+#ifdef CONFIG_AUFS_DEBUG
14078+#include <linux/migrate.h>
14079+#endif
4a4d8108 14080+#include <linux/pagemap.h>
1facf9fc 14081+#include "aufs.h"
14082+
4a4d8108
AM
14083+/* drop flags for writing */
14084+unsigned int au_file_roflags(unsigned int flags)
14085+{
14086+ flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
14087+ flags |= O_RDONLY | O_NOATIME;
14088+ return flags;
14089+}
14090+
14091+/* common functions to regular file and dir */
14092+struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
392086de 14093+ struct file *file, int force_wr)
1facf9fc 14094+{
1308ab2a 14095+ struct file *h_file;
4a4d8108
AM
14096+ struct dentry *h_dentry;
14097+ struct inode *h_inode;
14098+ struct super_block *sb;
14099+ struct au_branch *br;
14100+ struct path h_path;
b912730e 14101+ int err;
1facf9fc 14102+
4a4d8108
AM
14103+ /* a race condition can happen between open and unlink/rmdir */
14104+ h_file = ERR_PTR(-ENOENT);
14105+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 14106+ if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
4a4d8108 14107+ goto out;
5527c038 14108+ h_inode = d_inode(h_dentry);
027c5e7a
AM
14109+ spin_lock(&h_dentry->d_lock);
14110+ err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
5527c038 14111+ /* || !d_inode(dentry)->i_nlink */
027c5e7a
AM
14112+ ;
14113+ spin_unlock(&h_dentry->d_lock);
14114+ if (unlikely(err))
4a4d8108 14115+ goto out;
1facf9fc 14116+
4a4d8108
AM
14117+ sb = dentry->d_sb;
14118+ br = au_sbr(sb, bindex);
b912730e
AM
14119+ err = au_br_test_oflag(flags, br);
14120+ h_file = ERR_PTR(err);
14121+ if (unlikely(err))
027c5e7a 14122+ goto out;
1facf9fc 14123+
4a4d8108 14124+ /* drop flags for writing */
5527c038 14125+ if (au_test_ro(sb, bindex, d_inode(dentry))) {
392086de
AM
14126+ if (force_wr && !(flags & O_WRONLY))
14127+ force_wr = 0;
4a4d8108 14128+ flags = au_file_roflags(flags);
392086de
AM
14129+ if (force_wr) {
14130+ h_file = ERR_PTR(-EROFS);
14131+ flags = au_file_roflags(flags);
14132+ if (unlikely(vfsub_native_ro(h_inode)
14133+ || IS_APPEND(h_inode)))
14134+ goto out;
14135+ flags &= ~O_ACCMODE;
14136+ flags |= O_WRONLY;
14137+ }
14138+ }
4a4d8108 14139+ flags &= ~O_CREAT;
acd2b654 14140+ au_lcnt_inc(&br->br_nfiles);
4a4d8108 14141+ h_path.dentry = h_dentry;
86dc4139 14142+ h_path.mnt = au_br_mnt(br);
38d290e6 14143+ h_file = vfsub_dentry_open(&h_path, flags);
4a4d8108
AM
14144+ if (IS_ERR(h_file))
14145+ goto out_br;
dece6358 14146+
b912730e 14147+ if (flags & __FMODE_EXEC) {
4a4d8108
AM
14148+ err = deny_write_access(h_file);
14149+ if (unlikely(err)) {
14150+ fput(h_file);
14151+ h_file = ERR_PTR(err);
14152+ goto out_br;
14153+ }
14154+ }
953406b4 14155+ fsnotify_open(h_file);
4a4d8108 14156+ goto out; /* success */
1facf9fc 14157+
4f0767ce 14158+out_br:
acd2b654 14159+ au_lcnt_dec(&br->br_nfiles);
4f0767ce 14160+out:
4a4d8108
AM
14161+ return h_file;
14162+}
1308ab2a 14163+
076b876e
AM
14164+static int au_cmoo(struct dentry *dentry)
14165+{
8b6a4947 14166+ int err, cmoo, matched;
076b876e
AM
14167+ unsigned int udba;
14168+ struct path h_path;
14169+ struct au_pin pin;
14170+ struct au_cp_generic cpg = {
14171+ .dentry = dentry,
14172+ .bdst = -1,
14173+ .bsrc = -1,
14174+ .len = -1,
14175+ .pin = &pin,
14176+ .flags = AuCpup_DTIME | AuCpup_HOPEN
14177+ };
7e9cd9fe 14178+ struct inode *delegated;
076b876e
AM
14179+ struct super_block *sb;
14180+ struct au_sbinfo *sbinfo;
14181+ struct au_fhsm *fhsm;
14182+ pid_t pid;
14183+ struct au_branch *br;
14184+ struct dentry *parent;
14185+ struct au_hinode *hdir;
14186+
14187+ DiMustWriteLock(dentry);
5527c038 14188+ IiMustWriteLock(d_inode(dentry));
076b876e
AM
14189+
14190+ err = 0;
14191+ if (IS_ROOT(dentry))
14192+ goto out;
5afbbe0d 14193+ cpg.bsrc = au_dbtop(dentry);
076b876e
AM
14194+ if (!cpg.bsrc)
14195+ goto out;
14196+
14197+ sb = dentry->d_sb;
14198+ sbinfo = au_sbi(sb);
14199+ fhsm = &sbinfo->si_fhsm;
14200+ pid = au_fhsm_pid(fhsm);
8b6a4947
AM
14201+ rcu_read_lock();
14202+ matched = (pid
14203+ && (current->pid == pid
14204+ || rcu_dereference(current->real_parent)->pid == pid));
14205+ rcu_read_unlock();
14206+ if (matched)
076b876e
AM
14207+ goto out;
14208+
14209+ br = au_sbr(sb, cpg.bsrc);
14210+ cmoo = au_br_cmoo(br->br_perm);
14211+ if (!cmoo)
14212+ goto out;
7e9cd9fe 14213+ if (!d_is_reg(dentry))
076b876e
AM
14214+ cmoo &= AuBrAttr_COO_ALL;
14215+ if (!cmoo)
14216+ goto out;
14217+
14218+ parent = dget_parent(dentry);
14219+ di_write_lock_parent(parent);
14220+ err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
14221+ cpg.bdst = err;
14222+ if (unlikely(err < 0)) {
14223+ err = 0; /* there is no upper writable branch */
14224+ goto out_dgrade;
14225+ }
14226+ AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
14227+
14228+ /* do not respect the coo attrib for the target branch */
14229+ err = au_cpup_dirs(dentry, cpg.bdst);
14230+ if (unlikely(err))
14231+ goto out_dgrade;
14232+
14233+ di_downgrade_lock(parent, AuLock_IR);
14234+ udba = au_opt_udba(sb);
14235+ err = au_pin(&pin, dentry, cpg.bdst, udba,
14236+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14237+ if (unlikely(err))
14238+ goto out_parent;
14239+
14240+ err = au_sio_cpup_simple(&cpg);
14241+ au_unpin(&pin);
14242+ if (unlikely(err))
14243+ goto out_parent;
14244+ if (!(cmoo & AuBrWAttr_MOO))
14245+ goto out_parent; /* success */
14246+
14247+ err = au_pin(&pin, dentry, cpg.bsrc, udba,
14248+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14249+ if (unlikely(err))
14250+ goto out_parent;
14251+
14252+ h_path.mnt = au_br_mnt(br);
14253+ h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
5527c038 14254+ hdir = au_hi(d_inode(parent), cpg.bsrc);
076b876e
AM
14255+ delegated = NULL;
14256+ err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
14257+ au_unpin(&pin);
14258+ /* todo: keep h_dentry or not? */
14259+ if (unlikely(err == -EWOULDBLOCK)) {
14260+ pr_warn("cannot retry for NFSv4 delegation"
14261+ " for an internal unlink\n");
14262+ iput(delegated);
14263+ }
14264+ if (unlikely(err)) {
14265+ pr_err("unlink %pd after coo failed (%d), ignored\n",
14266+ dentry, err);
14267+ err = 0;
14268+ }
14269+ goto out_parent; /* success */
14270+
14271+out_dgrade:
14272+ di_downgrade_lock(parent, AuLock_IR);
14273+out_parent:
14274+ di_read_unlock(parent, AuLock_IR);
14275+ dput(parent);
14276+out:
14277+ AuTraceErr(err);
14278+ return err;
14279+}
14280+
b912730e 14281+int au_do_open(struct file *file, struct au_do_open_args *args)
1facf9fc 14282+{
8b6a4947 14283+ int err, aopen = args->aopen;
1facf9fc 14284+ struct dentry *dentry;
076b876e 14285+ struct au_finfo *finfo;
1308ab2a 14286+
8b6a4947 14287+ if (!aopen)
b912730e
AM
14288+ err = au_finfo_init(file, args->fidir);
14289+ else {
14290+ lockdep_off();
14291+ err = au_finfo_init(file, args->fidir);
14292+ lockdep_on();
14293+ }
4a4d8108
AM
14294+ if (unlikely(err))
14295+ goto out;
1facf9fc 14296+
2000de60 14297+ dentry = file->f_path.dentry;
b912730e 14298+ AuDebugOn(IS_ERR_OR_NULL(dentry));
8b6a4947
AM
14299+ di_write_lock_child(dentry);
14300+ err = au_cmoo(dentry);
8b6a4947 14301+ if (!err) {
2255d0fe
AM
14302+ if (!aopen) {
14303+ err = args->open(file, vfsub_file_flags(file),
14304+ au_di(dentry)->di_htmpfile);
14305+ di_write_unlock(dentry);
14306+ } else {
14307+ di_downgrade_lock(dentry, AuLock_IR);
8b6a4947 14308+ lockdep_off();
acd2b654
AM
14309+ err = args->open(file, vfsub_file_flags(file),
14310+ args->h_file);
8b6a4947 14311+ lockdep_on();
2255d0fe 14312+ di_read_unlock(dentry, AuLock_IR);
8b6a4947 14313+ }
b912730e 14314+ }
1facf9fc 14315+
076b876e
AM
14316+ finfo = au_fi(file);
14317+ if (!err) {
14318+ finfo->fi_file = file;
8b6a4947
AM
14319+ au_hbl_add(&finfo->fi_hlist,
14320+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
076b876e 14321+ }
8b6a4947 14322+ if (!aopen)
b912730e
AM
14323+ fi_write_unlock(file);
14324+ else {
14325+ lockdep_off();
14326+ fi_write_unlock(file);
14327+ lockdep_on();
14328+ }
4a4d8108 14329+ if (unlikely(err)) {
076b876e 14330+ finfo->fi_hdir = NULL;
1c60b727 14331+ au_finfo_fin(file);
1308ab2a 14332+ }
4a4d8108 14333+
4f0767ce 14334+out:
8b6a4947 14335+ AuTraceErr(err);
1308ab2a 14336+ return err;
14337+}
dece6358 14338+
4a4d8108 14339+int au_reopen_nondir(struct file *file)
1308ab2a 14340+{
4a4d8108 14341+ int err;
5afbbe0d 14342+ aufs_bindex_t btop;
4a4d8108 14343+ struct dentry *dentry;
acd2b654 14344+ struct au_branch *br;
4a4d8108 14345+ struct file *h_file, *h_file_tmp;
1308ab2a 14346+
2000de60 14347+ dentry = file->f_path.dentry;
5afbbe0d 14348+ btop = au_dbtop(dentry);
acd2b654 14349+ br = au_sbr(dentry->d_sb, btop);
4a4d8108 14350+ h_file_tmp = NULL;
5afbbe0d 14351+ if (au_fbtop(file) == btop) {
4a4d8108
AM
14352+ h_file = au_hf_top(file);
14353+ if (file->f_mode == h_file->f_mode)
14354+ return 0; /* success */
14355+ h_file_tmp = h_file;
14356+ get_file(h_file_tmp);
acd2b654 14357+ au_lcnt_inc(&br->br_nfiles);
5afbbe0d 14358+ au_set_h_fptr(file, btop, NULL);
4a4d8108
AM
14359+ }
14360+ AuDebugOn(au_fi(file)->fi_hdir);
86dc4139
AM
14361+ /*
14362+ * it can happen
14363+ * file exists on both of rw and ro
5afbbe0d 14364+ * open --> dbtop and fbtop are both 0
86dc4139
AM
14365+ * prepend a branch as rw, "rw" become ro
14366+ * remove rw/file
14367+ * delete the top branch, "rw" becomes rw again
5afbbe0d
AM
14368+ * --> dbtop is 1, fbtop is still 0
14369+ * write --> fbtop is 0 but dbtop is 1
86dc4139 14370+ */
5afbbe0d 14371+ /* AuDebugOn(au_fbtop(file) < btop); */
1308ab2a 14372+
5afbbe0d 14373+ h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
392086de 14374+ file, /*force_wr*/0);
4a4d8108 14375+ err = PTR_ERR(h_file);
86dc4139
AM
14376+ if (IS_ERR(h_file)) {
14377+ if (h_file_tmp) {
acd2b654 14378+ /* revert */
5afbbe0d 14379+ au_set_h_fptr(file, btop, h_file_tmp);
86dc4139
AM
14380+ h_file_tmp = NULL;
14381+ }
4a4d8108 14382+ goto out; /* todo: close all? */
86dc4139 14383+ }
4a4d8108
AM
14384+
14385+ err = 0;
5afbbe0d
AM
14386+ au_set_fbtop(file, btop);
14387+ au_set_h_fptr(file, btop, h_file);
4a4d8108
AM
14388+ au_update_figen(file);
14389+ /* todo: necessary? */
14390+ /* file->f_ra = h_file->f_ra; */
14391+
4f0767ce 14392+out:
acd2b654 14393+ if (h_file_tmp) {
4a4d8108 14394+ fput(h_file_tmp);
acd2b654
AM
14395+ au_lcnt_dec(&br->br_nfiles);
14396+ }
4a4d8108 14397+ return err;
1facf9fc 14398+}
14399+
1308ab2a 14400+/* ---------------------------------------------------------------------- */
14401+
4a4d8108
AM
14402+static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
14403+ struct dentry *hi_wh)
1facf9fc 14404+{
4a4d8108 14405+ int err;
5afbbe0d 14406+ aufs_bindex_t btop;
4a4d8108
AM
14407+ struct au_dinfo *dinfo;
14408+ struct dentry *h_dentry;
14409+ struct au_hdentry *hdp;
1facf9fc 14410+
2000de60 14411+ dinfo = au_di(file->f_path.dentry);
4a4d8108 14412+ AuRwMustWriteLock(&dinfo->di_rwsem);
dece6358 14413+
5afbbe0d
AM
14414+ btop = dinfo->di_btop;
14415+ dinfo->di_btop = btgt;
14416+ hdp = au_hdentry(dinfo, btgt);
14417+ h_dentry = hdp->hd_dentry;
14418+ hdp->hd_dentry = hi_wh;
4a4d8108 14419+ err = au_reopen_nondir(file);
5afbbe0d
AM
14420+ hdp->hd_dentry = h_dentry;
14421+ dinfo->di_btop = btop;
1facf9fc 14422+
1facf9fc 14423+ return err;
14424+}
14425+
4a4d8108 14426+static int au_ready_to_write_wh(struct file *file, loff_t len,
86dc4139 14427+ aufs_bindex_t bcpup, struct au_pin *pin)
1facf9fc 14428+{
4a4d8108 14429+ int err;
027c5e7a 14430+ struct inode *inode, *h_inode;
c2b27bf2
AM
14431+ struct dentry *h_dentry, *hi_wh;
14432+ struct au_cp_generic cpg = {
2000de60 14433+ .dentry = file->f_path.dentry,
c2b27bf2
AM
14434+ .bdst = bcpup,
14435+ .bsrc = -1,
14436+ .len = len,
14437+ .pin = pin
14438+ };
1facf9fc 14439+
5afbbe0d 14440+ au_update_dbtop(cpg.dentry);
5527c038 14441+ inode = d_inode(cpg.dentry);
027c5e7a 14442+ h_inode = NULL;
5afbbe0d
AM
14443+ if (au_dbtop(cpg.dentry) <= bcpup
14444+ && au_dbbot(cpg.dentry) >= bcpup) {
c2b27bf2 14445+ h_dentry = au_h_dptr(cpg.dentry, bcpup);
5527c038
JR
14446+ if (h_dentry && d_is_positive(h_dentry))
14447+ h_inode = d_inode(h_dentry);
027c5e7a 14448+ }
4a4d8108 14449+ hi_wh = au_hi_wh(inode, bcpup);
027c5e7a 14450+ if (!hi_wh && !h_inode)
c2b27bf2 14451+ err = au_sio_cpup_wh(&cpg, file);
4a4d8108
AM
14452+ else
14453+ /* already copied-up after unlink */
14454+ err = au_reopen_wh(file, bcpup, hi_wh);
1facf9fc 14455+
4a4d8108 14456+ if (!err
38d290e6
JR
14457+ && (inode->i_nlink > 1
14458+ || (inode->i_state & I_LINKABLE))
c2b27bf2
AM
14459+ && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
14460+ au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
1308ab2a 14461+
dece6358 14462+ return err;
1facf9fc 14463+}
14464+
4a4d8108
AM
14465+/*
14466+ * prepare the @file for writing.
14467+ */
14468+int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
1facf9fc 14469+{
4a4d8108 14470+ int err;
5afbbe0d 14471+ aufs_bindex_t dbtop;
c1595e42 14472+ struct dentry *parent;
86dc4139 14473+ struct inode *inode;
1facf9fc 14474+ struct super_block *sb;
4a4d8108 14475+ struct file *h_file;
c2b27bf2 14476+ struct au_cp_generic cpg = {
2000de60 14477+ .dentry = file->f_path.dentry,
c2b27bf2
AM
14478+ .bdst = -1,
14479+ .bsrc = -1,
14480+ .len = len,
14481+ .pin = pin,
14482+ .flags = AuCpup_DTIME
14483+ };
1facf9fc 14484+
c2b27bf2 14485+ sb = cpg.dentry->d_sb;
5527c038 14486+ inode = d_inode(cpg.dentry);
5afbbe0d 14487+ cpg.bsrc = au_fbtop(file);
c2b27bf2 14488+ err = au_test_ro(sb, cpg.bsrc, inode);
4a4d8108 14489+ if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
c2b27bf2
AM
14490+ err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
14491+ /*flags*/0);
1facf9fc 14492+ goto out;
4a4d8108 14493+ }
1facf9fc 14494+
027c5e7a 14495+ /* need to cpup or reopen */
c2b27bf2 14496+ parent = dget_parent(cpg.dentry);
4a4d8108 14497+ di_write_lock_parent(parent);
c2b27bf2
AM
14498+ err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14499+ cpg.bdst = err;
4a4d8108
AM
14500+ if (unlikely(err < 0))
14501+ goto out_dgrade;
14502+ err = 0;
14503+
c2b27bf2
AM
14504+ if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
14505+ err = au_cpup_dirs(cpg.dentry, cpg.bdst);
1facf9fc 14506+ if (unlikely(err))
4a4d8108
AM
14507+ goto out_dgrade;
14508+ }
14509+
c2b27bf2 14510+ err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
4a4d8108
AM
14511+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14512+ if (unlikely(err))
14513+ goto out_dgrade;
14514+
5afbbe0d
AM
14515+ dbtop = au_dbtop(cpg.dentry);
14516+ if (dbtop <= cpg.bdst)
c2b27bf2 14517+ cpg.bsrc = cpg.bdst;
027c5e7a 14518+
5afbbe0d 14519+ if (dbtop <= cpg.bdst /* just reopen */
c2b27bf2 14520+ || !d_unhashed(cpg.dentry) /* copyup and reopen */
027c5e7a 14521+ ) {
392086de 14522+ h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
86dc4139 14523+ if (IS_ERR(h_file))
027c5e7a 14524+ err = PTR_ERR(h_file);
86dc4139 14525+ else {
027c5e7a 14526+ di_downgrade_lock(parent, AuLock_IR);
5afbbe0d 14527+ if (dbtop > cpg.bdst)
c2b27bf2 14528+ err = au_sio_cpup_simple(&cpg);
027c5e7a
AM
14529+ if (!err)
14530+ err = au_reopen_nondir(file);
c2b27bf2 14531+ au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
027c5e7a 14532+ }
027c5e7a
AM
14533+ } else { /* copyup as wh and reopen */
14534+ /*
14535+ * since writable hfsplus branch is not supported,
14536+ * h_open_pre/post() are unnecessary.
14537+ */
c2b27bf2 14538+ err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
4a4d8108 14539+ di_downgrade_lock(parent, AuLock_IR);
4a4d8108 14540+ }
4a4d8108
AM
14541+
14542+ if (!err) {
14543+ au_pin_set_parent_lflag(pin, /*lflag*/0);
14544+ goto out_dput; /* success */
14545+ }
14546+ au_unpin(pin);
14547+ goto out_unlock;
1facf9fc 14548+
4f0767ce 14549+out_dgrade:
4a4d8108 14550+ di_downgrade_lock(parent, AuLock_IR);
4f0767ce 14551+out_unlock:
4a4d8108 14552+ di_read_unlock(parent, AuLock_IR);
4f0767ce 14553+out_dput:
4a4d8108 14554+ dput(parent);
4f0767ce 14555+out:
1facf9fc 14556+ return err;
14557+}
14558+
4a4d8108
AM
14559+/* ---------------------------------------------------------------------- */
14560+
14561+int au_do_flush(struct file *file, fl_owner_t id,
14562+ int (*flush)(struct file *file, fl_owner_t id))
1facf9fc 14563+{
4a4d8108 14564+ int err;
1facf9fc 14565+ struct super_block *sb;
4a4d8108 14566+ struct inode *inode;
1facf9fc 14567+
c06a8ce3
AM
14568+ inode = file_inode(file);
14569+ sb = inode->i_sb;
4a4d8108
AM
14570+ si_noflush_read_lock(sb);
14571+ fi_read_lock(file);
b752ccd1 14572+ ii_read_lock_child(inode);
1facf9fc 14573+
4a4d8108
AM
14574+ err = flush(file, id);
14575+ au_cpup_attr_timesizes(inode);
1facf9fc 14576+
b752ccd1 14577+ ii_read_unlock(inode);
4a4d8108 14578+ fi_read_unlock(file);
1308ab2a 14579+ si_read_unlock(sb);
dece6358 14580+ return err;
1facf9fc 14581+}
14582+
4a4d8108
AM
14583+/* ---------------------------------------------------------------------- */
14584+
14585+static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
1facf9fc 14586+{
4a4d8108 14587+ int err;
4a4d8108
AM
14588+ struct au_pin pin;
14589+ struct au_finfo *finfo;
c2b27bf2 14590+ struct dentry *parent, *hi_wh;
4a4d8108 14591+ struct inode *inode;
1facf9fc 14592+ struct super_block *sb;
c2b27bf2 14593+ struct au_cp_generic cpg = {
2000de60 14594+ .dentry = file->f_path.dentry,
c2b27bf2
AM
14595+ .bdst = -1,
14596+ .bsrc = -1,
14597+ .len = -1,
14598+ .pin = &pin,
14599+ .flags = AuCpup_DTIME
14600+ };
1facf9fc 14601+
4a4d8108
AM
14602+ FiMustWriteLock(file);
14603+
14604+ err = 0;
14605+ finfo = au_fi(file);
c2b27bf2 14606+ sb = cpg.dentry->d_sb;
5527c038 14607+ inode = d_inode(cpg.dentry);
5afbbe0d 14608+ cpg.bdst = au_ibtop(inode);
c2b27bf2 14609+ if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
1308ab2a 14610+ goto out;
dece6358 14611+
c2b27bf2
AM
14612+ parent = dget_parent(cpg.dentry);
14613+ if (au_test_ro(sb, cpg.bdst, inode)) {
4a4d8108 14614+ di_read_lock_parent(parent, !AuLock_IR);
c2b27bf2
AM
14615+ err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14616+ cpg.bdst = err;
4a4d8108
AM
14617+ di_read_unlock(parent, !AuLock_IR);
14618+ if (unlikely(err < 0))
14619+ goto out_parent;
14620+ err = 0;
1facf9fc 14621+ }
1facf9fc 14622+
4a4d8108 14623+ di_read_lock_parent(parent, AuLock_IR);
c2b27bf2 14624+ hi_wh = au_hi_wh(inode, cpg.bdst);
7f207e10
AM
14625+ if (!S_ISDIR(inode->i_mode)
14626+ && au_opt_test(au_mntflags(sb), PLINK)
4a4d8108 14627+ && au_plink_test(inode)
c2b27bf2 14628+ && !d_unhashed(cpg.dentry)
5afbbe0d 14629+ && cpg.bdst < au_dbtop(cpg.dentry)) {
c2b27bf2 14630+ err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
4a4d8108
AM
14631+ if (unlikely(err))
14632+ goto out_unlock;
14633+
14634+ /* always superio. */
c2b27bf2 14635+ err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
4a4d8108 14636+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
367653fa 14637+ if (!err) {
c2b27bf2 14638+ err = au_sio_cpup_simple(&cpg);
367653fa
AM
14639+ au_unpin(&pin);
14640+ }
4a4d8108
AM
14641+ } else if (hi_wh) {
14642+ /* already copied-up after unlink */
c2b27bf2 14643+ err = au_reopen_wh(file, cpg.bdst, hi_wh);
4a4d8108
AM
14644+ *need_reopen = 0;
14645+ }
1facf9fc 14646+
4f0767ce 14647+out_unlock:
4a4d8108 14648+ di_read_unlock(parent, AuLock_IR);
4f0767ce 14649+out_parent:
4a4d8108 14650+ dput(parent);
4f0767ce 14651+out:
1308ab2a 14652+ return err;
dece6358 14653+}
1facf9fc 14654+
4a4d8108 14655+static void au_do_refresh_dir(struct file *file)
dece6358 14656+{
5afbbe0d 14657+ aufs_bindex_t bindex, bbot, new_bindex, brid;
4a4d8108
AM
14658+ struct au_hfile *p, tmp, *q;
14659+ struct au_finfo *finfo;
1308ab2a 14660+ struct super_block *sb;
4a4d8108 14661+ struct au_fidir *fidir;
1facf9fc 14662+
4a4d8108 14663+ FiMustWriteLock(file);
1facf9fc 14664+
2000de60 14665+ sb = file->f_path.dentry->d_sb;
4a4d8108
AM
14666+ finfo = au_fi(file);
14667+ fidir = finfo->fi_hdir;
14668+ AuDebugOn(!fidir);
14669+ p = fidir->fd_hfile + finfo->fi_btop;
14670+ brid = p->hf_br->br_id;
5afbbe0d
AM
14671+ bbot = fidir->fd_bbot;
14672+ for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
4a4d8108
AM
14673+ if (!p->hf_file)
14674+ continue;
1308ab2a 14675+
4a4d8108
AM
14676+ new_bindex = au_br_index(sb, p->hf_br->br_id);
14677+ if (new_bindex == bindex)
14678+ continue;
14679+ if (new_bindex < 0) {
14680+ au_set_h_fptr(file, bindex, NULL);
14681+ continue;
14682+ }
1308ab2a 14683+
4a4d8108
AM
14684+ /* swap two lower inode, and loop again */
14685+ q = fidir->fd_hfile + new_bindex;
14686+ tmp = *q;
14687+ *q = *p;
14688+ *p = tmp;
14689+ if (tmp.hf_file) {
14690+ bindex--;
14691+ p--;
14692+ }
14693+ }
1308ab2a 14694+
4a4d8108 14695+ p = fidir->fd_hfile;
2000de60 14696+ if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
5afbbe0d
AM
14697+ bbot = au_sbbot(sb);
14698+ for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
4a4d8108
AM
14699+ finfo->fi_btop++, p++)
14700+ if (p->hf_file) {
c06a8ce3 14701+ if (file_inode(p->hf_file))
4a4d8108 14702+ break;
1c60b727 14703+ au_hfput(p, /*execed*/0);
4a4d8108
AM
14704+ }
14705+ } else {
5afbbe0d
AM
14706+ bbot = au_br_index(sb, brid);
14707+ for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
4a4d8108
AM
14708+ finfo->fi_btop++, p++)
14709+ if (p->hf_file)
1c60b727 14710+ au_hfput(p, /*execed*/0);
5afbbe0d 14711+ bbot = au_sbbot(sb);
4a4d8108 14712+ }
1308ab2a 14713+
5afbbe0d
AM
14714+ p = fidir->fd_hfile + bbot;
14715+ for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
4a4d8108
AM
14716+ fidir->fd_bbot--, p--)
14717+ if (p->hf_file) {
c06a8ce3 14718+ if (file_inode(p->hf_file))
4a4d8108 14719+ break;
1c60b727 14720+ au_hfput(p, /*execed*/0);
4a4d8108
AM
14721+ }
14722+ AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
1308ab2a 14723+}
14724+
4a4d8108
AM
14725+/*
14726+ * after branch manipulating, refresh the file.
14727+ */
14728+static int refresh_file(struct file *file, int (*reopen)(struct file *file))
1facf9fc 14729+{
e2f27e51 14730+ int err, need_reopen, nbr;
5afbbe0d 14731+ aufs_bindex_t bbot, bindex;
4a4d8108 14732+ struct dentry *dentry;
e2f27e51 14733+ struct super_block *sb;
1308ab2a 14734+ struct au_finfo *finfo;
4a4d8108 14735+ struct au_hfile *hfile;
1facf9fc 14736+
2000de60 14737+ dentry = file->f_path.dentry;
e2f27e51
AM
14738+ sb = dentry->d_sb;
14739+ nbr = au_sbbot(sb) + 1;
1308ab2a 14740+ finfo = au_fi(file);
4a4d8108
AM
14741+ if (!finfo->fi_hdir) {
14742+ hfile = &finfo->fi_htop;
14743+ AuDebugOn(!hfile->hf_file);
e2f27e51 14744+ bindex = au_br_index(sb, hfile->hf_br->br_id);
4a4d8108
AM
14745+ AuDebugOn(bindex < 0);
14746+ if (bindex != finfo->fi_btop)
5afbbe0d 14747+ au_set_fbtop(file, bindex);
4a4d8108 14748+ } else {
e2f27e51 14749+ err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
4a4d8108
AM
14750+ if (unlikely(err))
14751+ goto out;
14752+ au_do_refresh_dir(file);
14753+ }
1facf9fc 14754+
4a4d8108
AM
14755+ err = 0;
14756+ need_reopen = 1;
14757+ if (!au_test_mmapped(file))
14758+ err = au_file_refresh_by_inode(file, &need_reopen);
e2f27e51
AM
14759+ if (finfo->fi_hdir)
14760+ /* harmless if err */
14761+ au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
027c5e7a 14762+ if (!err && need_reopen && !d_unlinked(dentry))
4a4d8108
AM
14763+ err = reopen(file);
14764+ if (!err) {
14765+ au_update_figen(file);
14766+ goto out; /* success */
14767+ }
14768+
14769+ /* error, close all lower files */
14770+ if (finfo->fi_hdir) {
5afbbe0d
AM
14771+ bbot = au_fbbot_dir(file);
14772+ for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
4a4d8108
AM
14773+ au_set_h_fptr(file, bindex, NULL);
14774+ }
1facf9fc 14775+
4f0767ce 14776+out:
1facf9fc 14777+ return err;
14778+}
14779+
4a4d8108
AM
14780+/* common function to regular file and dir */
14781+int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
521ced18 14782+ int wlock, unsigned int fi_lsc)
dece6358 14783+{
1308ab2a 14784+ int err;
4a4d8108 14785+ unsigned int sigen, figen;
5afbbe0d 14786+ aufs_bindex_t btop;
4a4d8108
AM
14787+ unsigned char pseudo_link;
14788+ struct dentry *dentry;
14789+ struct inode *inode;
1facf9fc 14790+
4a4d8108 14791+ err = 0;
2000de60 14792+ dentry = file->f_path.dentry;
5527c038 14793+ inode = d_inode(dentry);
4a4d8108 14794+ sigen = au_sigen(dentry->d_sb);
521ced18 14795+ fi_write_lock_nested(file, fi_lsc);
4a4d8108 14796+ figen = au_figen(file);
521ced18
JR
14797+ if (!fi_lsc)
14798+ di_write_lock_child(dentry);
14799+ else
14800+ di_write_lock_child2(dentry);
5afbbe0d
AM
14801+ btop = au_dbtop(dentry);
14802+ pseudo_link = (btop != au_ibtop(inode));
14803+ if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
4a4d8108
AM
14804+ if (!wlock) {
14805+ di_downgrade_lock(dentry, AuLock_IR);
14806+ fi_downgrade_lock(file);
14807+ }
14808+ goto out; /* success */
14809+ }
dece6358 14810+
4a4d8108 14811+ AuDbg("sigen %d, figen %d\n", sigen, figen);
027c5e7a 14812+ if (au_digen_test(dentry, sigen)) {
4a4d8108 14813+ err = au_reval_dpath(dentry, sigen);
027c5e7a 14814+ AuDebugOn(!err && au_digen_test(dentry, sigen));
4a4d8108 14815+ }
dece6358 14816+
027c5e7a
AM
14817+ if (!err)
14818+ err = refresh_file(file, reopen);
4a4d8108
AM
14819+ if (!err) {
14820+ if (!wlock) {
14821+ di_downgrade_lock(dentry, AuLock_IR);
14822+ fi_downgrade_lock(file);
14823+ }
14824+ } else {
14825+ di_write_unlock(dentry);
14826+ fi_write_unlock(file);
14827+ }
1facf9fc 14828+
4f0767ce 14829+out:
1308ab2a 14830+ return err;
14831+}
1facf9fc 14832+
4a4d8108
AM
14833+/* ---------------------------------------------------------------------- */
14834+
14835+/* cf. aufs_nopage() */
14836+/* for madvise(2) */
22319442 14837+static int aufs_read_folio(struct file *file __maybe_unused, struct folio *folio)
1308ab2a 14838+{
22319442 14839+ folio_unlock(folio);
4a4d8108
AM
14840+ return 0;
14841+}
1facf9fc 14842+
4a4d8108 14843+/* it will never be called, but necessary to support O_DIRECT */
5afbbe0d 14844+static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
4a4d8108 14845+{ BUG(); return 0; }
1facf9fc 14846+
4a4d8108
AM
14847+/* they will never be called. */
14848+#ifdef CONFIG_AUFS_DEBUG
14849+static int aufs_write_begin(struct file *file, struct address_space *mapping,
22319442 14850+ loff_t pos, unsigned len,
4a4d8108
AM
14851+ struct page **pagep, void **fsdata)
14852+{ AuUnsupport(); return 0; }
14853+static int aufs_write_end(struct file *file, struct address_space *mapping,
14854+ loff_t pos, unsigned len, unsigned copied,
14855+ struct page *page, void *fsdata)
14856+{ AuUnsupport(); return 0; }
14857+static int aufs_writepage(struct page *page, struct writeback_control *wbc)
14858+{ AuUnsupport(); return 0; }
1308ab2a 14859+
26eb093c
AM
14860+static bool aufs_dirty_folio(struct address_space *mapping, struct folio *folio)
14861+{ AuUnsupport(); return true; }
14862+static void aufs_invalidate_folio(struct folio *folio, size_t offset, size_t len)
4a4d8108 14863+{ AuUnsupport(); }
22319442
JP
14864+static bool aufs_release_folio(struct folio *folio, gfp_t gfp)
14865+{ AuUnsupport(); return true; }
79b8bda9 14866+#if 0 /* called by memory compaction regardless file */
82da57cb
AM
14867+static int aufs_migrate_folio(struct address_space *mapping, struct folio *dst,
14868+ struct folio *src, enum migrate_mode mode)
4a4d8108 14869+{ AuUnsupport(); return 0; }
79b8bda9 14870+#endif
26eb093c 14871+static int aufs_launder_folio(struct folio *folio)
4a4d8108 14872+{ AuUnsupport(); return 0; }
26eb093c
AM
14873+static bool aufs_is_partially_uptodate(struct folio *folio, size_t from,
14874+ size_t count)
14875+{ AuUnsupport(); return true; }
22319442 14876+static void aufs_is_dirty_writeback(struct folio *folio, bool *dirty,
392086de
AM
14877+ bool *writeback)
14878+{ AuUnsupport(); }
4a4d8108
AM
14879+static int aufs_error_remove_page(struct address_space *mapping,
14880+ struct page *page)
14881+{ AuUnsupport(); return 0; }
b4510431
AM
14882+static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
14883+ sector_t *span)
14884+{ AuUnsupport(); return 0; }
14885+static void aufs_swap_deactivate(struct file *file)
14886+{ AuUnsupport(); }
22319442
JP
14887+static int aufs_swap_rw(struct kiocb *iocb, struct iov_iter *iter)
14888+{ AuUnsupport(); return 0; }
4a4d8108
AM
14889+#endif /* CONFIG_AUFS_DEBUG */
14890+
14891+const struct address_space_operations aufs_aop = {
22319442 14892+ .read_folio = aufs_read_folio,
4a4d8108 14893+ .direct_IO = aufs_direct_IO,
4a4d8108
AM
14894+#ifdef CONFIG_AUFS_DEBUG
14895+ .writepage = aufs_writepage,
4a4d8108 14896+ /* no writepages, because of writepage */
26eb093c 14897+ .dirty_folio = aufs_dirty_folio,
4a4d8108
AM
14898+ /* no readpages, because of readpage */
14899+ .write_begin = aufs_write_begin,
14900+ .write_end = aufs_write_end,
14901+ /* no bmap, no block device */
26eb093c 14902+ .invalidate_folio = aufs_invalidate_folio,
22319442 14903+ .release_folio = aufs_release_folio,
79b8bda9 14904+ /* is fallback_migrate_page ok? */
82da57cb 14905+ /* .migrate_folio = aufs_migrate_folio, */
26eb093c 14906+ .launder_folio = aufs_launder_folio,
4a4d8108 14907+ .is_partially_uptodate = aufs_is_partially_uptodate,
392086de 14908+ .is_dirty_writeback = aufs_is_dirty_writeback,
b4510431
AM
14909+ .error_remove_page = aufs_error_remove_page,
14910+ .swap_activate = aufs_swap_activate,
22319442
JP
14911+ .swap_deactivate = aufs_swap_deactivate,
14912+ .swap_rw = aufs_swap_rw
4a4d8108 14913+#endif /* CONFIG_AUFS_DEBUG */
dece6358 14914+};
7f207e10 14915diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
eca34b5c 14916--- /usr/share/empty/fs/aufs/file.h 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 14917+++ linux/fs/aufs/file.h 2022-11-05 23:02:18.965889284 +0100
9f237c51 14918@@ -0,0 +1,342 @@
062440b3 14919+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 14920+/*
0dcfbb52 14921+ * Copyright (C) 2005-2022 Junjiro R. Okajima
4a4d8108 14922+ *
0dcfbb52 14923+ * This program is free software; you can redistribute it and/or modify
4a4d8108
AM
14924+ * it under the terms of the GNU General Public License as published by
14925+ * the Free Software Foundation; either version 2 of the License, or
14926+ * (at your option) any later version.
14927+ *
14928+ * This program is distributed in the hope that it will be useful,
14929+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14930+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14931+ * GNU General Public License for more details.
14932+ *
14933+ * You should have received a copy of the GNU General Public License
523b37e3 14934+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 14935+ */
1facf9fc 14936+
4a4d8108
AM
14937+/*
14938+ * file operations
14939+ */
1facf9fc 14940+
4a4d8108
AM
14941+#ifndef __AUFS_FILE_H__
14942+#define __AUFS_FILE_H__
1facf9fc 14943+
4a4d8108 14944+#ifdef __KERNEL__
1facf9fc 14945+
2cbb1c4b 14946+#include <linux/file.h>
4a4d8108 14947+#include <linux/fs.h>
3c1bdaff 14948+#include <linux/mm_types.h>
4a4d8108 14949+#include <linux/poll.h>
4a4d8108 14950+#include "rwsem.h"
1facf9fc 14951+
4a4d8108
AM
14952+struct au_branch;
14953+struct au_hfile {
14954+ struct file *hf_file;
14955+ struct au_branch *hf_br;
14956+};
1facf9fc 14957+
4a4d8108
AM
14958+struct au_vdir;
14959+struct au_fidir {
14960+ aufs_bindex_t fd_bbot;
14961+ aufs_bindex_t fd_nent;
14962+ struct au_vdir *fd_vdir_cache;
14963+ struct au_hfile fd_hfile[];
14964+};
1facf9fc 14965+
4a4d8108 14966+static inline int au_fidir_sz(int nent)
dece6358 14967+{
4f0767ce
JR
14968+ AuDebugOn(nent < 0);
14969+ return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
4a4d8108 14970+}
1facf9fc 14971+
4a4d8108
AM
14972+struct au_finfo {
14973+ atomic_t fi_generation;
dece6358 14974+
4a4d8108
AM
14975+ struct au_rwsem fi_rwsem;
14976+ aufs_bindex_t fi_btop;
14977+
14978+ /* do not union them */
14979+ struct { /* for non-dir */
14980+ struct au_hfile fi_htop;
2cbb1c4b 14981+ atomic_t fi_mmapped;
4a4d8108
AM
14982+ };
14983+ struct au_fidir *fi_hdir; /* for dir only */
523b37e3 14984+
8b6a4947 14985+ struct hlist_bl_node fi_hlist;
1c60b727 14986+ struct file *fi_file; /* very ugly */
9f237c51 14987+ struct rcu_head rcu;
4a4d8108 14988+} ____cacheline_aligned_in_smp;
1facf9fc 14989+
4a4d8108 14990+/* ---------------------------------------------------------------------- */
1facf9fc 14991+
4a4d8108
AM
14992+/* file.c */
14993+extern const struct address_space_operations aufs_aop;
14994+unsigned int au_file_roflags(unsigned int flags);
14995+struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
392086de 14996+ struct file *file, int force_wr);
b912730e 14997+struct au_do_open_args {
8b6a4947 14998+ int aopen;
b912730e
AM
14999+ int (*open)(struct file *file, int flags,
15000+ struct file *h_file);
15001+ struct au_fidir *fidir;
15002+ struct file *h_file;
15003+};
15004+int au_do_open(struct file *file, struct au_do_open_args *args);
4a4d8108
AM
15005+int au_reopen_nondir(struct file *file);
15006+struct au_pin;
15007+int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
15008+int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
521ced18 15009+ int wlock, unsigned int fi_lsc);
4a4d8108
AM
15010+int au_do_flush(struct file *file, fl_owner_t id,
15011+ int (*flush)(struct file *file, fl_owner_t id));
1facf9fc 15012+
4a4d8108
AM
15013+/* poll.c */
15014+#ifdef CONFIG_AUFS_POLL
cd7a4cd9 15015+__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt);
4a4d8108 15016+#endif
1facf9fc 15017+
4a4d8108
AM
15018+#ifdef CONFIG_AUFS_BR_HFSPLUS
15019+/* hfsplus.c */
392086de
AM
15020+struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
15021+ int force_wr);
4a4d8108
AM
15022+void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
15023+ struct file *h_file);
15024+#else
c1595e42
JR
15025+AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
15026+ aufs_bindex_t bindex, int force_wr)
4a4d8108
AM
15027+AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
15028+ struct file *h_file);
15029+#endif
1facf9fc 15030+
4a4d8108
AM
15031+/* f_op.c */
15032+extern const struct file_operations aufs_file_fop;
b912730e 15033+int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
4a4d8108 15034+int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
521ced18 15035+struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
4a4d8108 15036+
4a4d8108 15037+/* finfo.c */
f0c0a007 15038+void au_hfput(struct au_hfile *hf, int execed);
4a4d8108
AM
15039+void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
15040+ struct file *h_file);
1facf9fc 15041+
4a4d8108 15042+void au_update_figen(struct file *file);
4a4d8108 15043+struct au_fidir *au_fidir_alloc(struct super_block *sb);
e2f27e51 15044+int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
1facf9fc 15045+
4a4d8108 15046+void au_fi_init_once(void *_fi);
1c60b727 15047+void au_finfo_fin(struct file *file);
4a4d8108 15048+int au_finfo_init(struct file *file, struct au_fidir *fidir);
1facf9fc 15049+
4a4d8108
AM
15050+/* ioctl.c */
15051+long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
b752ccd1
AM
15052+#ifdef CONFIG_COMPAT
15053+long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
15054+ unsigned long arg);
c2b27bf2
AM
15055+long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
15056+ unsigned long arg);
b752ccd1 15057+#endif
1facf9fc 15058+
4a4d8108 15059+/* ---------------------------------------------------------------------- */
1facf9fc 15060+
4a4d8108
AM
15061+static inline struct au_finfo *au_fi(struct file *file)
15062+{
38d290e6 15063+ return file->private_data;
4a4d8108 15064+}
1facf9fc 15065+
4a4d8108 15066+/* ---------------------------------------------------------------------- */
1facf9fc 15067+
8b6a4947
AM
15068+#define fi_read_lock(f) au_rw_read_lock(&au_fi(f)->fi_rwsem)
15069+#define fi_write_lock(f) au_rw_write_lock(&au_fi(f)->fi_rwsem)
15070+#define fi_read_trylock(f) au_rw_read_trylock(&au_fi(f)->fi_rwsem)
15071+#define fi_write_trylock(f) au_rw_write_trylock(&au_fi(f)->fi_rwsem)
4a4d8108 15072+/*
8b6a4947
AM
15073+#define fi_read_trylock_nested(f) \
15074+ au_rw_read_trylock_nested(&au_fi(f)->fi_rwsem)
15075+#define fi_write_trylock_nested(f) \
15076+ au_rw_write_trylock_nested(&au_fi(f)->fi_rwsem)
15077+*/
15078+
15079+#define fi_read_unlock(f) au_rw_read_unlock(&au_fi(f)->fi_rwsem)
15080+#define fi_write_unlock(f) au_rw_write_unlock(&au_fi(f)->fi_rwsem)
15081+#define fi_downgrade_lock(f) au_rw_dgrade_lock(&au_fi(f)->fi_rwsem)
1308ab2a 15082+
521ced18
JR
15083+/* lock subclass for finfo */
15084+enum {
15085+ AuLsc_FI_1,
15086+ AuLsc_FI_2
15087+};
15088+
15089+static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
15090+{
15091+ au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15092+}
15093+
15094+static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
15095+{
15096+ au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15097+}
15098+
15099+/*
15100+ * fi_read_lock_1, fi_write_lock_1,
15101+ * fi_read_lock_2, fi_write_lock_2
15102+ */
15103+#define AuReadLockFunc(name) \
15104+static inline void fi_read_lock_##name(struct file *f) \
15105+{ fi_read_lock_nested(f, AuLsc_FI_##name); }
15106+
15107+#define AuWriteLockFunc(name) \
15108+static inline void fi_write_lock_##name(struct file *f) \
15109+{ fi_write_lock_nested(f, AuLsc_FI_##name); }
15110+
15111+#define AuRWLockFuncs(name) \
15112+ AuReadLockFunc(name) \
15113+ AuWriteLockFunc(name)
15114+
15115+AuRWLockFuncs(1);
15116+AuRWLockFuncs(2);
15117+
15118+#undef AuReadLockFunc
15119+#undef AuWriteLockFunc
15120+#undef AuRWLockFuncs
15121+
4a4d8108
AM
15122+#define FiMustNoWaiters(f) AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
15123+#define FiMustAnyLock(f) AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
15124+#define FiMustWriteLock(f) AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
1facf9fc 15125+
1308ab2a 15126+/* ---------------------------------------------------------------------- */
15127+
4a4d8108 15128+/* todo: hard/soft set? */
5afbbe0d 15129+static inline aufs_bindex_t au_fbtop(struct file *file)
dece6358 15130+{
4a4d8108
AM
15131+ FiMustAnyLock(file);
15132+ return au_fi(file)->fi_btop;
15133+}
dece6358 15134+
5afbbe0d 15135+static inline aufs_bindex_t au_fbbot_dir(struct file *file)
4a4d8108
AM
15136+{
15137+ FiMustAnyLock(file);
15138+ AuDebugOn(!au_fi(file)->fi_hdir);
15139+ return au_fi(file)->fi_hdir->fd_bbot;
15140+}
1facf9fc 15141+
4a4d8108
AM
15142+static inline struct au_vdir *au_fvdir_cache(struct file *file)
15143+{
15144+ FiMustAnyLock(file);
15145+ AuDebugOn(!au_fi(file)->fi_hdir);
15146+ return au_fi(file)->fi_hdir->fd_vdir_cache;
15147+}
1facf9fc 15148+
5afbbe0d 15149+static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
4a4d8108
AM
15150+{
15151+ FiMustWriteLock(file);
15152+ au_fi(file)->fi_btop = bindex;
15153+}
1facf9fc 15154+
5afbbe0d 15155+static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
4a4d8108
AM
15156+{
15157+ FiMustWriteLock(file);
15158+ AuDebugOn(!au_fi(file)->fi_hdir);
15159+ au_fi(file)->fi_hdir->fd_bbot = bindex;
15160+}
1308ab2a 15161+
4a4d8108
AM
15162+static inline void au_set_fvdir_cache(struct file *file,
15163+ struct au_vdir *vdir_cache)
15164+{
15165+ FiMustWriteLock(file);
15166+ AuDebugOn(!au_fi(file)->fi_hdir);
15167+ au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
15168+}
dece6358 15169+
4a4d8108
AM
15170+static inline struct file *au_hf_top(struct file *file)
15171+{
15172+ FiMustAnyLock(file);
15173+ AuDebugOn(au_fi(file)->fi_hdir);
15174+ return au_fi(file)->fi_htop.hf_file;
15175+}
1facf9fc 15176+
4a4d8108
AM
15177+static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
15178+{
15179+ FiMustAnyLock(file);
15180+ AuDebugOn(!au_fi(file)->fi_hdir);
15181+ return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
dece6358
AM
15182+}
15183+
4a4d8108
AM
15184+/* todo: memory barrier? */
15185+static inline unsigned int au_figen(struct file *f)
dece6358 15186+{
4a4d8108
AM
15187+ return atomic_read(&au_fi(f)->fi_generation);
15188+}
dece6358 15189+
2cbb1c4b
JR
15190+static inline void au_set_mmapped(struct file *f)
15191+{
15192+ if (atomic_inc_return(&au_fi(f)->fi_mmapped))
15193+ return;
0c3ec466 15194+ pr_warn("fi_mmapped wrapped around\n");
2cbb1c4b
JR
15195+ while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
15196+ ;
15197+}
15198+
15199+static inline void au_unset_mmapped(struct file *f)
15200+{
15201+ atomic_dec(&au_fi(f)->fi_mmapped);
15202+}
15203+
4a4d8108
AM
15204+static inline int au_test_mmapped(struct file *f)
15205+{
2cbb1c4b
JR
15206+ return atomic_read(&au_fi(f)->fi_mmapped);
15207+}
15208+
15209+/* customize vma->vm_file */
15210+
15211+static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
15212+ struct file *file)
15213+{
53392da6
AM
15214+ struct file *f;
15215+
15216+ f = vma->vm_file;
2cbb1c4b
JR
15217+ get_file(file);
15218+ vma->vm_file = file;
53392da6 15219+ fput(f);
2cbb1c4b
JR
15220+}
15221+
15222+#ifdef CONFIG_MMU
15223+#define AuDbgVmRegion(file, vma) do {} while (0)
15224+
15225+static inline void au_vm_file_reset(struct vm_area_struct *vma,
15226+ struct file *file)
15227+{
15228+ au_do_vm_file_reset(vma, file);
15229+}
15230+#else
15231+#define AuDbgVmRegion(file, vma) \
15232+ AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
15233+
15234+static inline void au_vm_file_reset(struct vm_area_struct *vma,
15235+ struct file *file)
15236+{
53392da6
AM
15237+ struct file *f;
15238+
2cbb1c4b 15239+ au_do_vm_file_reset(vma, file);
53392da6 15240+ f = vma->vm_region->vm_file;
2cbb1c4b
JR
15241+ get_file(file);
15242+ vma->vm_region->vm_file = file;
53392da6 15243+ fput(f);
2cbb1c4b
JR
15244+}
15245+#endif /* CONFIG_MMU */
15246+
15247+/* handle vma->vm_prfile */
fb47a38f 15248+static inline void au_vm_prfile_set(struct vm_area_struct *vma,
2cbb1c4b
JR
15249+ struct file *file)
15250+{
2cbb1c4b
JR
15251+ get_file(file);
15252+ vma->vm_prfile = file;
15253+#ifndef CONFIG_MMU
15254+ get_file(file);
15255+ vma->vm_region->vm_prfile = file;
15256+#endif
fb47a38f 15257+}
1308ab2a 15258+
4a4d8108
AM
15259+#endif /* __KERNEL__ */
15260+#endif /* __AUFS_FILE_H__ */
7f207e10 15261diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
eca34b5c 15262--- /usr/share/empty/fs/aufs/finfo.c 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 15263+++ linux/fs/aufs/finfo.c 2022-11-05 23:02:18.965889284 +0100
062440b3 15264@@ -0,0 +1,149 @@
cd7a4cd9 15265+// SPDX-License-Identifier: GPL-2.0
4a4d8108 15266+/*
0dcfbb52 15267+ * Copyright (C) 2005-2022 Junjiro R. Okajima
4a4d8108 15268+ *
0dcfbb52 15269+ * This program is free software; you can redistribute it and/or modify
4a4d8108
AM
15270+ * it under the terms of the GNU General Public License as published by
15271+ * the Free Software Foundation; either version 2 of the License, or
15272+ * (at your option) any later version.
15273+ *
15274+ * This program is distributed in the hope that it will be useful,
15275+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15276+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15277+ * GNU General Public License for more details.
15278+ *
15279+ * You should have received a copy of the GNU General Public License
523b37e3 15280+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 15281+ */
1308ab2a 15282+
4a4d8108
AM
15283+/*
15284+ * file private data
15285+ */
1facf9fc 15286+
4a4d8108 15287+#include "aufs.h"
1facf9fc 15288+
f0c0a007 15289+void au_hfput(struct au_hfile *hf, int execed)
4a4d8108 15290+{
f0c0a007 15291+ if (execed)
4a4d8108
AM
15292+ allow_write_access(hf->hf_file);
15293+ fput(hf->hf_file);
15294+ hf->hf_file = NULL;
acd2b654 15295+ au_lcnt_dec(&hf->hf_br->br_nfiles);
4a4d8108
AM
15296+ hf->hf_br = NULL;
15297+}
1facf9fc 15298+
4a4d8108
AM
15299+void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
15300+{
15301+ struct au_finfo *finfo = au_fi(file);
15302+ struct au_hfile *hf;
15303+ struct au_fidir *fidir;
15304+
15305+ fidir = finfo->fi_hdir;
15306+ if (!fidir) {
15307+ AuDebugOn(finfo->fi_btop != bindex);
15308+ hf = &finfo->fi_htop;
15309+ } else
15310+ hf = fidir->fd_hfile + bindex;
15311+
15312+ if (hf && hf->hf_file)
f0c0a007 15313+ au_hfput(hf, vfsub_file_execed(file));
4a4d8108
AM
15314+ if (val) {
15315+ FiMustWriteLock(file);
b912730e 15316+ AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
4a4d8108 15317+ hf->hf_file = val;
2000de60 15318+ hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
1308ab2a 15319+ }
4a4d8108 15320+}
1facf9fc 15321+
4a4d8108
AM
15322+void au_update_figen(struct file *file)
15323+{
2000de60 15324+ atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
4a4d8108 15325+ /* smp_mb(); */ /* atomic_set */
1facf9fc 15326+}
15327+
4a4d8108
AM
15328+/* ---------------------------------------------------------------------- */
15329+
4a4d8108
AM
15330+struct au_fidir *au_fidir_alloc(struct super_block *sb)
15331+{
15332+ struct au_fidir *fidir;
15333+ int nbr;
15334+
5afbbe0d 15335+ nbr = au_sbbot(sb) + 1;
4a4d8108
AM
15336+ if (nbr < 2)
15337+ nbr = 2; /* initial allocate for 2 branches */
15338+ fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
15339+ if (fidir) {
15340+ fidir->fd_bbot = -1;
15341+ fidir->fd_nent = nbr;
4a4d8108
AM
15342+ }
15343+
15344+ return fidir;
15345+}
15346+
e2f27e51 15347+int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
4a4d8108
AM
15348+{
15349+ int err;
15350+ struct au_fidir *fidir, *p;
15351+
15352+ AuRwMustWriteLock(&finfo->fi_rwsem);
15353+ fidir = finfo->fi_hdir;
15354+ AuDebugOn(!fidir);
15355+
15356+ err = -ENOMEM;
15357+ p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
e2f27e51 15358+ GFP_NOFS, may_shrink);
4a4d8108
AM
15359+ if (p) {
15360+ p->fd_nent = nbr;
15361+ finfo->fi_hdir = p;
15362+ err = 0;
15363+ }
1facf9fc 15364+
dece6358 15365+ return err;
1facf9fc 15366+}
1308ab2a 15367+
15368+/* ---------------------------------------------------------------------- */
15369+
1c60b727 15370+void au_finfo_fin(struct file *file)
1308ab2a 15371+{
4a4d8108
AM
15372+ struct au_finfo *finfo;
15373+
acd2b654 15374+ au_lcnt_dec(&au_sbi(file->f_path.dentry->d_sb)->si_nfiles);
7f207e10 15375+
4a4d8108
AM
15376+ finfo = au_fi(file);
15377+ AuDebugOn(finfo->fi_hdir);
15378+ AuRwDestroy(&finfo->fi_rwsem);
1c60b727 15379+ au_cache_free_finfo(finfo);
1308ab2a 15380+}
1308ab2a 15381+
e49829fe 15382+void au_fi_init_once(void *_finfo)
4a4d8108 15383+{
e49829fe 15384+ struct au_finfo *finfo = _finfo;
1308ab2a 15385+
e49829fe 15386+ au_rw_init(&finfo->fi_rwsem);
4a4d8108 15387+}
1308ab2a 15388+
4a4d8108
AM
15389+int au_finfo_init(struct file *file, struct au_fidir *fidir)
15390+{
1716fcea 15391+ int err;
4a4d8108
AM
15392+ struct au_finfo *finfo;
15393+ struct dentry *dentry;
15394+
15395+ err = -ENOMEM;
2000de60 15396+ dentry = file->f_path.dentry;
4a4d8108
AM
15397+ finfo = au_cache_alloc_finfo();
15398+ if (unlikely(!finfo))
15399+ goto out;
15400+
15401+ err = 0;
acd2b654 15402+ au_lcnt_inc(&au_sbi(dentry->d_sb)->si_nfiles);
4a4d8108
AM
15403+ au_rw_write_lock(&finfo->fi_rwsem);
15404+ finfo->fi_btop = -1;
15405+ finfo->fi_hdir = fidir;
15406+ atomic_set(&finfo->fi_generation, au_digen(dentry));
15407+ /* smp_mb(); */ /* atomic_set */
15408+
15409+ file->private_data = finfo;
15410+
15411+out:
15412+ return err;
15413+}
7f207e10 15414diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
eca34b5c 15415--- /usr/share/empty/fs/aufs/f_op.c 1970-01-01 01:00:00.000000000 +0100
2255d0fe
AM
15416+++ linux/fs/aufs/f_op.c 2022-12-17 09:21:34.799855195 +0100
15417@@ -0,0 +1,776 @@
cd7a4cd9 15418+// SPDX-License-Identifier: GPL-2.0
dece6358 15419+/*
0dcfbb52 15420+ * Copyright (C) 2005-2022 Junjiro R. Okajima
dece6358 15421+ *
0dcfbb52 15422+ * This program is free software; you can redistribute it and/or modify
dece6358
AM
15423+ * it under the terms of the GNU General Public License as published by
15424+ * the Free Software Foundation; either version 2 of the License, or
15425+ * (at your option) any later version.
15426+ *
15427+ * This program is distributed in the hope that it will be useful,
15428+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15429+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15430+ * GNU General Public License for more details.
15431+ *
15432+ * You should have received a copy of the GNU General Public License
523b37e3 15433+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358 15434+ */
1facf9fc 15435+
15436+/*
4a4d8108 15437+ * file and vm operations
1facf9fc 15438+ */
dece6358 15439+
86dc4139 15440+#include <linux/aio.h>
4a4d8108
AM
15441+#include <linux/fs_stack.h>
15442+#include <linux/mman.h>
4a4d8108 15443+#include <linux/security.h>
dece6358
AM
15444+#include "aufs.h"
15445+
b912730e 15446+int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
1facf9fc 15447+{
4a4d8108
AM
15448+ int err;
15449+ aufs_bindex_t bindex;
8cdd5066 15450+ struct dentry *dentry, *h_dentry;
4a4d8108 15451+ struct au_finfo *finfo;
38d290e6 15452+ struct inode *h_inode;
4a4d8108
AM
15453+
15454+ FiMustWriteLock(file);
15455+
523b37e3 15456+ err = 0;
2000de60 15457+ dentry = file->f_path.dentry;
b912730e 15458+ AuDebugOn(IS_ERR_OR_NULL(dentry));
4a4d8108
AM
15459+ finfo = au_fi(file);
15460+ memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
2cbb1c4b 15461+ atomic_set(&finfo->fi_mmapped, 0);
5afbbe0d 15462+ bindex = au_dbtop(dentry);
8cdd5066
JR
15463+ if (!h_file) {
15464+ h_dentry = au_h_dptr(dentry, bindex);
15465+ err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15466+ if (unlikely(err))
15467+ goto out;
b912730e 15468+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
acd2b654
AM
15469+ if (IS_ERR(h_file)) {
15470+ err = PTR_ERR(h_file);
15471+ goto out;
15472+ }
8cdd5066
JR
15473+ } else {
15474+ h_dentry = h_file->f_path.dentry;
15475+ err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15476+ if (unlikely(err))
15477+ goto out;
acd2b654 15478+ /* br ref is already inc-ed */
8cdd5066 15479+ }
acd2b654 15480+
2255d0fe
AM
15481+ if (flags & __O_TMPFILE) {
15482+ AuDebugOn(!h_file);
15483+ AuDebugOn(h_file != au_di(dentry)->di_htmpfile);
15484+ au_di(dentry)->di_htmpfile = NULL;
15485+
15486+ if (!(flags & O_EXCL)) {
15487+ h_inode = file_inode(h_file);
15488+ spin_lock(&h_inode->i_lock);
15489+ h_inode->i_state |= I_LINKABLE;
15490+ spin_unlock(&h_inode->i_lock);
15491+ }
4a4d8108 15492+ }
acd2b654
AM
15493+ au_set_fbtop(file, bindex);
15494+ au_set_h_fptr(file, bindex, h_file);
15495+ au_update_figen(file);
15496+ /* todo: necessary? */
15497+ /* file->f_ra = h_file->f_ra; */
027c5e7a 15498+
8cdd5066 15499+out:
4a4d8108 15500+ return err;
1facf9fc 15501+}
15502+
4a4d8108
AM
15503+static int aufs_open_nondir(struct inode *inode __maybe_unused,
15504+ struct file *file)
1facf9fc 15505+{
4a4d8108 15506+ int err;
1308ab2a 15507+ struct super_block *sb;
b912730e
AM
15508+ struct au_do_open_args args = {
15509+ .open = au_do_open_nondir
15510+ };
1facf9fc 15511+
523b37e3
AM
15512+ AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
15513+ file, vfsub_file_flags(file), file->f_mode);
1facf9fc 15514+
2000de60 15515+ sb = file->f_path.dentry->d_sb;
4a4d8108 15516+ si_read_lock(sb, AuLock_FLUSH);
b912730e 15517+ err = au_do_open(file, &args);
4a4d8108
AM
15518+ si_read_unlock(sb);
15519+ return err;
15520+}
1facf9fc 15521+
4a4d8108
AM
15522+int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
15523+{
15524+ struct au_finfo *finfo;
15525+ aufs_bindex_t bindex;
1facf9fc 15526+
4a4d8108 15527+ finfo = au_fi(file);
8b6a4947
AM
15528+ au_hbl_del(&finfo->fi_hlist,
15529+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
4a4d8108 15530+ bindex = finfo->fi_btop;
b4510431 15531+ if (bindex >= 0)
4a4d8108 15532+ au_set_h_fptr(file, bindex, NULL);
7f207e10 15533+
1c60b727 15534+ au_finfo_fin(file);
4a4d8108 15535+ return 0;
1facf9fc 15536+}
15537+
4a4d8108
AM
15538+/* ---------------------------------------------------------------------- */
15539+
15540+static int au_do_flush_nondir(struct file *file, fl_owner_t id)
dece6358 15541+{
1308ab2a 15542+ int err;
4a4d8108
AM
15543+ struct file *h_file;
15544+
15545+ err = 0;
15546+ h_file = au_hf_top(file);
15547+ if (h_file)
15548+ err = vfsub_flush(h_file, id);
15549+ return err;
15550+}
15551+
15552+static int aufs_flush_nondir(struct file *file, fl_owner_t id)
15553+{
15554+ return au_do_flush(file, id, au_do_flush_nondir);
15555+}
15556+
15557+/* ---------------------------------------------------------------------- */
9dbd164d
AM
15558+/*
15559+ * read and write functions acquire [fdi]_rwsem once, but release before
15560+ * mmap_sem. This is because to stop a race condition between mmap(2).
acd2b654 15561+ * Releasing these aufs-rwsem should be safe, no branch-management (by keeping
9dbd164d
AM
15562+ * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
15563+ * read functions after [fdi]_rwsem are released, but it should be harmless.
15564+ */
4a4d8108 15565+
b912730e 15566+/* Callers should call au_read_post() or fput() in the end */
521ced18 15567+struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
4a4d8108 15568+{
4a4d8108 15569+ struct file *h_file;
b912730e 15570+ int err;
1facf9fc 15571+
521ced18 15572+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
b912730e
AM
15573+ if (!err) {
15574+ di_read_unlock(file->f_path.dentry, AuLock_IR);
15575+ h_file = au_hf_top(file);
15576+ get_file(h_file);
15577+ if (!keep_fi)
15578+ fi_read_unlock(file);
15579+ } else
15580+ h_file = ERR_PTR(err);
15581+
15582+ return h_file;
15583+}
15584+
15585+static void au_read_post(struct inode *inode, struct file *h_file)
15586+{
15587+ /* update without lock, I don't think it a problem */
15588+ fsstack_copy_attr_atime(inode, file_inode(h_file));
15589+ fput(h_file);
15590+}
15591+
15592+struct au_write_pre {
521ced18
JR
15593+ /* input */
15594+ unsigned int lsc;
15595+
15596+ /* output */
b912730e 15597+ blkcnt_t blks;
5afbbe0d 15598+ aufs_bindex_t btop;
b912730e
AM
15599+};
15600+
15601+/*
15602+ * return with iinfo is write-locked
15603+ * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
15604+ * end
15605+ */
15606+static struct file *au_write_pre(struct file *file, int do_ready,
15607+ struct au_write_pre *wpre)
15608+{
15609+ struct file *h_file;
15610+ struct dentry *dentry;
15611+ int err;
521ced18 15612+ unsigned int lsc;
b912730e
AM
15613+ struct au_pin pin;
15614+
521ced18
JR
15615+ lsc = 0;
15616+ if (wpre)
15617+ lsc = wpre->lsc;
15618+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
b912730e 15619+ h_file = ERR_PTR(err);
dece6358
AM
15620+ if (unlikely(err))
15621+ goto out;
1facf9fc 15622+
b912730e
AM
15623+ dentry = file->f_path.dentry;
15624+ if (do_ready) {
15625+ err = au_ready_to_write(file, -1, &pin);
15626+ if (unlikely(err)) {
15627+ h_file = ERR_PTR(err);
15628+ di_write_unlock(dentry);
15629+ goto out_fi;
15630+ }
15631+ }
15632+
15633+ di_downgrade_lock(dentry, /*flags*/0);
15634+ if (wpre)
5afbbe0d 15635+ wpre->btop = au_fbtop(file);
4a4d8108 15636+ h_file = au_hf_top(file);
9dbd164d 15637+ get_file(h_file);
b912730e
AM
15638+ if (wpre)
15639+ wpre->blks = file_inode(h_file)->i_blocks;
15640+ if (do_ready)
15641+ au_unpin(&pin);
15642+ di_read_unlock(dentry, /*flags*/0);
15643+
15644+out_fi:
15645+ fi_write_unlock(file);
15646+out:
15647+ return h_file;
15648+}
15649+
15650+static void au_write_post(struct inode *inode, struct file *h_file,
15651+ struct au_write_pre *wpre, ssize_t written)
15652+{
15653+ struct inode *h_inode;
15654+
15655+ au_cpup_attr_timesizes(inode);
5afbbe0d 15656+ AuDebugOn(au_ibtop(inode) != wpre->btop);
b912730e
AM
15657+ h_inode = file_inode(h_file);
15658+ inode->i_mode = h_inode->i_mode;
15659+ ii_write_unlock(inode);
b912730e
AM
15660+ /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
15661+ if (written > 0)
5afbbe0d 15662+ au_fhsm_wrote(inode->i_sb, wpre->btop,
b912730e 15663+ /*force*/h_inode->i_blocks > wpre->blks);
1c60b727 15664+ fput(h_file);
b912730e
AM
15665+}
15666+
e49829fe
JR
15667+/*
15668+ * todo: very ugly
15669+ * it locks both of i_mutex and si_rwsem for read in safe.
15670+ * if the plink maintenance mode continues forever (that is the problem),
15671+ * may loop forever.
15672+ */
15673+static void au_mtx_and_read_lock(struct inode *inode)
15674+{
15675+ int err;
15676+ struct super_block *sb = inode->i_sb;
15677+
15678+ while (1) {
febd17d6 15679+ inode_lock(inode);
e49829fe
JR
15680+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
15681+ if (!err)
15682+ break;
febd17d6 15683+ inode_unlock(inode);
e49829fe
JR
15684+ si_read_lock(sb, AuLock_NOPLMW);
15685+ si_read_unlock(sb);
15686+ }
15687+}
15688+
076b876e
AM
15689+static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
15690+ struct iov_iter *iov_iter)
dece6358 15691+{
4a4d8108
AM
15692+ ssize_t err;
15693+ struct file *file;
076b876e 15694+ ssize_t (*iter)(struct kiocb *, struct iov_iter *);
1facf9fc 15695+
4a4d8108
AM
15696+ err = security_file_permission(h_file, rw);
15697+ if (unlikely(err))
15698+ goto out;
1facf9fc 15699+
43982f53 15700+ err = -ENOSYS; /* the branch doesn't have its ->(read|write)_iter() */
076b876e 15701+ iter = NULL;
5527c038 15702+ if (rw == MAY_READ)
076b876e 15703+ iter = h_file->f_op->read_iter;
5527c038 15704+ else if (rw == MAY_WRITE)
076b876e 15705+ iter = h_file->f_op->write_iter;
076b876e
AM
15706+
15707+ file = kio->ki_filp;
15708+ kio->ki_filp = h_file;
15709+ if (iter) {
2cbb1c4b 15710+ lockdep_off();
076b876e
AM
15711+ err = iter(kio, iov_iter);
15712+ lockdep_on();
4a4d8108
AM
15713+ } else
15714+ /* currently there is no such fs */
15715+ WARN_ON_ONCE(1);
076b876e 15716+ kio->ki_filp = file;
1facf9fc 15717+
4f0767ce 15718+out:
dece6358
AM
15719+ return err;
15720+}
1facf9fc 15721+
076b876e 15722+static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
1facf9fc 15723+{
4a4d8108
AM
15724+ ssize_t err;
15725+ struct file *file, *h_file;
b912730e 15726+ struct inode *inode;
dece6358 15727+ struct super_block *sb;
1facf9fc 15728+
4a4d8108 15729+ file = kio->ki_filp;
b912730e
AM
15730+ inode = file_inode(file);
15731+ sb = inode->i_sb;
e49829fe 15732+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
4a4d8108 15733+
521ced18 15734+ h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
b912730e
AM
15735+ err = PTR_ERR(h_file);
15736+ if (IS_ERR(h_file))
15737+ goto out;
9dbd164d 15738+
5afbbe0d
AM
15739+ if (au_test_loopback_kthread()) {
15740+ au_warn_loopback(h_file->f_path.dentry->d_sb);
15741+ if (file->f_mapping != h_file->f_mapping) {
15742+ file->f_mapping = h_file->f_mapping;
15743+ smp_mb(); /* unnecessary? */
15744+ }
15745+ }
15746+ fi_read_unlock(file);
15747+
076b876e 15748+ err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
4a4d8108
AM
15749+ /* todo: necessary? */
15750+ /* file->f_ra = h_file->f_ra; */
b912730e 15751+ au_read_post(inode, h_file);
1facf9fc 15752+
4f0767ce 15753+out:
4a4d8108 15754+ si_read_unlock(sb);
1308ab2a 15755+ return err;
15756+}
1facf9fc 15757+
076b876e 15758+static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
1308ab2a 15759+{
4a4d8108 15760+ ssize_t err;
b912730e
AM
15761+ struct au_write_pre wpre;
15762+ struct inode *inode;
4a4d8108 15763+ struct file *file, *h_file;
1308ab2a 15764+
4a4d8108 15765+ file = kio->ki_filp;
b912730e 15766+ inode = file_inode(file);
e49829fe
JR
15767+ au_mtx_and_read_lock(inode);
15768+
521ced18 15769+ wpre.lsc = 0;
b912730e
AM
15770+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15771+ err = PTR_ERR(h_file);
15772+ if (IS_ERR(h_file))
9dbd164d 15773+ goto out;
9dbd164d 15774+
076b876e 15775+ err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
b912730e 15776+ au_write_post(inode, h_file, &wpre, err);
1facf9fc 15777+
4f0767ce 15778+out:
b912730e 15779+ si_read_unlock(inode->i_sb);
febd17d6 15780+ inode_unlock(inode);
dece6358 15781+ return err;
1facf9fc 15782+}
15783+
0b2a12c6
JR
15784+/*
15785+ * We may be able to remove aufs_splice_{read,write}() since almost all FSes
15786+ * don't have their own .splice_{read,write} implimentations, and they use
15787+ * generic_file_splice_read() and iter_file_splice_write() who can act like the
15788+ * simple converters to f_op->iter_read() and ->iter_write().
15789+ * But we keep our own implementations because some non-mainlined FSes may have
15790+ * their own .splice_{read,write} implimentations and aufs doesn't want to take
15791+ * away an opportunity to co-work with aufs from them.
15792+ */
4a4d8108
AM
15793+static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
15794+ struct pipe_inode_info *pipe, size_t len,
15795+ unsigned int flags)
1facf9fc 15796+{
4a4d8108
AM
15797+ ssize_t err;
15798+ struct file *h_file;
b912730e 15799+ struct inode *inode;
dece6358 15800+ struct super_block *sb;
1facf9fc 15801+
b912730e
AM
15802+ inode = file_inode(file);
15803+ sb = inode->i_sb;
e49829fe 15804+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e 15805+
521ced18 15806+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
b912730e
AM
15807+ err = PTR_ERR(h_file);
15808+ if (IS_ERR(h_file))
dece6358 15809+ goto out;
1facf9fc 15810+
4a4d8108 15811+ err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
acd2b654 15812+ /* todo: necessary? */
4a4d8108 15813+ /* file->f_ra = h_file->f_ra; */
b912730e 15814+ au_read_post(inode, h_file);
1facf9fc 15815+
4f0767ce 15816+out:
4a4d8108 15817+ si_read_unlock(sb);
dece6358 15818+ return err;
1facf9fc 15819+}
15820+
4a4d8108
AM
15821+static ssize_t
15822+aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
15823+ size_t len, unsigned int flags)
1facf9fc 15824+{
4a4d8108 15825+ ssize_t err;
b912730e
AM
15826+ struct au_write_pre wpre;
15827+ struct inode *inode;
076b876e 15828+ struct file *h_file;
1facf9fc 15829+
b912730e 15830+ inode = file_inode(file);
e49829fe 15831+ au_mtx_and_read_lock(inode);
9dbd164d 15832+
521ced18 15833+ wpre.lsc = 0;
b912730e
AM
15834+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15835+ err = PTR_ERR(h_file);
15836+ if (IS_ERR(h_file))
9dbd164d 15837+ goto out;
9dbd164d 15838+
4a4d8108 15839+ err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
b912730e 15840+ au_write_post(inode, h_file, &wpre, err);
1facf9fc 15841+
4f0767ce 15842+out:
b912730e 15843+ si_read_unlock(inode->i_sb);
febd17d6 15844+ inode_unlock(inode);
4a4d8108
AM
15845+ return err;
15846+}
1facf9fc 15847+
38d290e6
JR
15848+static long aufs_fallocate(struct file *file, int mode, loff_t offset,
15849+ loff_t len)
15850+{
15851+ long err;
b912730e 15852+ struct au_write_pre wpre;
38d290e6
JR
15853+ struct inode *inode;
15854+ struct file *h_file;
15855+
b912730e 15856+ inode = file_inode(file);
38d290e6
JR
15857+ au_mtx_and_read_lock(inode);
15858+
521ced18 15859+ wpre.lsc = 0;
b912730e
AM
15860+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15861+ err = PTR_ERR(h_file);
15862+ if (IS_ERR(h_file))
38d290e6 15863+ goto out;
38d290e6
JR
15864+
15865+ lockdep_off();
03673fb0 15866+ err = vfs_fallocate(h_file, mode, offset, len);
38d290e6 15867+ lockdep_on();
b912730e 15868+ au_write_post(inode, h_file, &wpre, /*written*/1);
38d290e6
JR
15869+
15870+out:
b912730e 15871+ si_read_unlock(inode->i_sb);
febd17d6 15872+ inode_unlock(inode);
38d290e6
JR
15873+ return err;
15874+}
15875+
521ced18
JR
15876+static ssize_t aufs_copy_file_range(struct file *src, loff_t src_pos,
15877+ struct file *dst, loff_t dst_pos,
15878+ size_t len, unsigned int flags)
15879+{
15880+ ssize_t err;
15881+ struct au_write_pre wpre;
15882+ enum { SRC, DST };
15883+ struct {
15884+ struct inode *inode;
15885+ struct file *h_file;
15886+ struct super_block *h_sb;
15887+ } a[2];
15888+#define a_src a[SRC]
15889+#define a_dst a[DST]
15890+
15891+ err = -EINVAL;
15892+ a_src.inode = file_inode(src);
15893+ if (unlikely(!S_ISREG(a_src.inode->i_mode)))
15894+ goto out;
15895+ a_dst.inode = file_inode(dst);
15896+ if (unlikely(!S_ISREG(a_dst.inode->i_mode)))
15897+ goto out;
15898+
15899+ au_mtx_and_read_lock(a_dst.inode);
15900+ /*
15901+ * in order to match the order in di_write_lock2_{child,parent}(),
acd2b654 15902+ * use f_path.dentry for this comparison.
521ced18
JR
15903+ */
15904+ if (src->f_path.dentry < dst->f_path.dentry) {
15905+ a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_1);
15906+ err = PTR_ERR(a_src.h_file);
15907+ if (IS_ERR(a_src.h_file))
15908+ goto out_si;
15909+
15910+ wpre.lsc = AuLsc_FI_2;
15911+ a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
15912+ err = PTR_ERR(a_dst.h_file);
15913+ if (IS_ERR(a_dst.h_file)) {
15914+ au_read_post(a_src.inode, a_src.h_file);
15915+ goto out_si;
15916+ }
15917+ } else {
15918+ wpre.lsc = AuLsc_FI_1;
15919+ a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
15920+ err = PTR_ERR(a_dst.h_file);
15921+ if (IS_ERR(a_dst.h_file))
15922+ goto out_si;
15923+
15924+ a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_2);
15925+ err = PTR_ERR(a_src.h_file);
15926+ if (IS_ERR(a_src.h_file)) {
15927+ au_write_post(a_dst.inode, a_dst.h_file, &wpre,
15928+ /*written*/0);
15929+ goto out_si;
15930+ }
15931+ }
15932+
f4d37d76
AM
15933+ err = -EXDEV;
15934+ a_src.h_sb = file_inode(a_src.h_file)->i_sb;
15935+ a_dst.h_sb = file_inode(a_dst.h_file)->i_sb;
15936+ if (unlikely(a_src.h_sb != a_dst.h_sb)) {
15937+ AuDbgFile(src);
15938+ AuDbgFile(dst);
15939+ goto out_file;
15940+ }
15941+
15942+ err = vfsub_copy_file_range(a_src.h_file, src_pos, a_dst.h_file,
15943+ dst_pos, len, flags);
15944+
15945+out_file:
15946+ au_write_post(a_dst.inode, a_dst.h_file, &wpre, err);
15947+ fi_read_unlock(src);
15948+ au_read_post(a_src.inode, a_src.h_file);
15949+out_si:
15950+ si_read_unlock(a_dst.inode->i_sb);
15951+ inode_unlock(a_dst.inode);
15952+out:
15953+ return err;
15954+#undef a_src
15955+#undef a_dst
15956+}
15957+
15958+/* ---------------------------------------------------------------------- */
15959+
15960+/*
15961+ * The locking order around current->mmap_sem.
15962+ * - in most and regular cases
15963+ * file I/O syscall -- aufs_read() or something
15964+ * -- si_rwsem for read -- mmap_sem
15965+ * (Note that [fdi]i_rwsem are released before mmap_sem).
15966+ * - in mmap case
15967+ * mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
15968+ * This AB-BA order is definitely bad, but is not a problem since "si_rwsem for
15969+ * read" allows multiple processes to acquire it and [fdi]i_rwsem are not held
15970+ * in file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
15971+ * It means that when aufs acquires si_rwsem for write, the process should never
15972+ * acquire mmap_sem.
15973+ *
15974+ * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
15975+ * problem either since any directory is not able to be mmap-ed.
15976+ * The similar scenario is applied to aufs_readlink() too.
15977+ */
15978+
15979+#if 0 /* stop calling security_file_mmap() */
15980+/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
15981+#define AuConv_VM_PROT(f, b) _calc_vm_trans(f, VM_##b, PROT_##b)
15982+
15983+static unsigned long au_arch_prot_conv(unsigned long flags)
15984+{
15985+ /* currently ppc64 only */
15986+#ifdef CONFIG_PPC64
15987+ /* cf. linux/arch/powerpc/include/asm/mman.h */
15988+ AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
15989+ return AuConv_VM_PROT(flags, SAO);
15990+#else
15991+ AuDebugOn(arch_calc_vm_prot_bits(-1));
15992+ return 0;
15993+#endif
15994+}
15995+
15996+static unsigned long au_prot_conv(unsigned long flags)
15997+{
15998+ return AuConv_VM_PROT(flags, READ)
15999+ | AuConv_VM_PROT(flags, WRITE)
16000+ | AuConv_VM_PROT(flags, EXEC)
16001+ | au_arch_prot_conv(flags);
16002+}
16003+
16004+/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
16005+#define AuConv_VM_MAP(f, b) _calc_vm_trans(f, VM_##b, MAP_##b)
16006+
16007+static unsigned long au_flag_conv(unsigned long flags)
16008+{
16009+ return AuConv_VM_MAP(flags, GROWSDOWN)
16010+ | AuConv_VM_MAP(flags, DENYWRITE)
16011+ | AuConv_VM_MAP(flags, LOCKED);
16012+}
16013+#endif
16014+
16015+static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
16016+{
16017+ int err;
16018+ const unsigned char wlock
16019+ = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
16020+ struct super_block *sb;
16021+ struct file *h_file;
16022+ struct inode *inode;
16023+
16024+ AuDbgVmRegion(file, vma);
16025+
16026+ inode = file_inode(file);
16027+ sb = inode->i_sb;
16028+ lockdep_off();
16029+ si_read_lock(sb, AuLock_NOPLMW);
16030+
16031+ h_file = au_write_pre(file, wlock, /*wpre*/NULL);
16032+ lockdep_on();
16033+ err = PTR_ERR(h_file);
16034+ if (IS_ERR(h_file))
16035+ goto out;
16036+
16037+ err = 0;
16038+ au_set_mmapped(file);
16039+ au_vm_file_reset(vma, h_file);
16040+ /*
16041+ * we cannot call security_mmap_file() here since it may acquire
16042+ * mmap_sem or i_mutex.
16043+ *
16044+ * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
16045+ * au_flag_conv(vma->vm_flags));
16046+ */
16047+ if (!err)
16048+ err = call_mmap(h_file, vma);
16049+ if (!err) {
16050+ au_vm_prfile_set(vma, file);
16051+ fsstack_copy_attr_atime(inode, file_inode(h_file));
16052+ goto out_fput; /* success */
16053+ }
16054+ au_unset_mmapped(file);
16055+ au_vm_file_reset(vma, file);
16056+
16057+out_fput:
16058+ lockdep_off();
16059+ ii_write_unlock(inode);
16060+ lockdep_on();
16061+ fput(h_file);
16062+out:
16063+ lockdep_off();
16064+ si_read_unlock(sb);
16065+ lockdep_on();
16066+ AuTraceErr(err);
16067+ return err;
16068+}
16069+
16070+/* ---------------------------------------------------------------------- */
16071+
16072+static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
16073+ int datasync)
16074+{
16075+ int err;
16076+ struct au_write_pre wpre;
16077+ struct inode *inode;
16078+ struct file *h_file;
16079+
16080+ err = 0; /* -EBADF; */ /* posix? */
16081+ if (unlikely(!(file->f_mode & FMODE_WRITE)))
16082+ goto out;
16083+
16084+ inode = file_inode(file);
16085+ au_mtx_and_read_lock(inode);
16086+
16087+ wpre.lsc = 0;
16088+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16089+ err = PTR_ERR(h_file);
16090+ if (IS_ERR(h_file))
16091+ goto out_unlock;
16092+
16093+ err = vfsub_fsync(h_file, &h_file->f_path, datasync);
16094+ au_write_post(inode, h_file, &wpre, /*written*/0);
16095+
16096+out_unlock:
16097+ si_read_unlock(inode->i_sb);
16098+ inode_unlock(inode);
16099+out:
16100+ return err;
16101+}
16102+
16103+static int aufs_fasync(int fd, struct file *file, int flag)
16104+{
16105+ int err;
16106+ struct file *h_file;
16107+ struct super_block *sb;
16108+
16109+ sb = file->f_path.dentry->d_sb;
16110+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16111+
16112+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16113+ err = PTR_ERR(h_file);
16114+ if (IS_ERR(h_file))
16115+ goto out;
16116+
16117+ if (h_file->f_op->fasync)
16118+ err = h_file->f_op->fasync(fd, h_file, flag);
16119+ fput(h_file); /* instead of au_read_post() */
16120+
16121+out:
16122+ si_read_unlock(sb);
16123+ return err;
16124+}
16125+
16126+static int aufs_setfl(struct file *file, unsigned long arg)
16127+{
16128+ int err;
16129+ struct file *h_file;
16130+ struct super_block *sb;
16131+
16132+ sb = file->f_path.dentry->d_sb;
16133+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16134+
16135+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16136+ err = PTR_ERR(h_file);
16137+ if (IS_ERR(h_file))
16138+ goto out;
16139+
16140+ /* stop calling h_file->fasync */
16141+ arg |= vfsub_file_flags(file) & FASYNC;
16142+ err = setfl(/*unused fd*/-1, h_file, arg);
16143+ fput(h_file); /* instead of au_read_post() */
16144+
16145+out:
16146+ si_read_unlock(sb);
16147+ return err;
16148+}
16149+
16150+/* ---------------------------------------------------------------------- */
16151+
16152+/* no one supports this operation, currently */
16153+#if 0 /* reserved for future use */
16154+static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
16155+ size_t len, loff_t *pos, int more)
16156+{
16157+}
16158+#endif
16159+
16160+/* ---------------------------------------------------------------------- */
16161+
16162+const struct file_operations aufs_file_fop = {
16163+ .owner = THIS_MODULE,
16164+
16165+ .llseek = default_llseek,
16166+
16167+ .read_iter = aufs_read_iter,
16168+ .write_iter = aufs_write_iter,
16169+
16170+#ifdef CONFIG_AUFS_POLL
16171+ .poll = aufs_poll,
16172+#endif
16173+ .unlocked_ioctl = aufs_ioctl_nondir,
16174+#ifdef CONFIG_COMPAT
16175+ .compat_ioctl = aufs_compat_ioctl_nondir,
16176+#endif
16177+ .mmap = aufs_mmap,
16178+ .open = aufs_open_nondir,
16179+ .flush = aufs_flush_nondir,
16180+ .release = aufs_release_nondir,
16181+ .fsync = aufs_fsync_nondir,
16182+ .fasync = aufs_fasync,
16183+ /* .sendpage = aufs_sendpage, */
16184+ .setfl = aufs_setfl,
16185+ .splice_write = aufs_splice_write,
16186+ .splice_read = aufs_splice_read,
16187+#if 0 /* reserved for future use */
16188+ .aio_splice_write = aufs_aio_splice_write,
16189+ .aio_splice_read = aufs_aio_splice_read,
16190+#endif
16191+ .fallocate = aufs_fallocate,
16192+ .copy_file_range = aufs_copy_file_range
16193+};
16194diff -urN /usr/share/empty/fs/aufs/fsctx.c linux/fs/aufs/fsctx.c
16195--- /usr/share/empty/fs/aufs/fsctx.c 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 16196+++ linux/fs/aufs/fsctx.c 2022-11-05 23:02:18.965889284 +0100
f4d37d76
AM
16197@@ -0,0 +1,1242 @@
16198+// SPDX-License-Identifier: GPL-2.0
16199+/*
16200+ * Copyright (C) 2022 Junjiro R. Okajima
16201+ *
0dcfbb52 16202+ * This program is free software; you can redistribute it and/or modify
f4d37d76
AM
16203+ * it under the terms of the GNU General Public License as published by
16204+ * the Free Software Foundation; either version 2 of the License, or
16205+ * (at your option) any later version.
16206+ *
16207+ * This program is distributed in the hope that it will be useful,
16208+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16209+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16210+ * GNU General Public License for more details.
16211+ *
16212+ * You should have received a copy of the GNU General Public License
16213+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
16214+ */
16215+
16216+/*
16217+ * fs context, aka new mount api
16218+ */
16219+
16220+#include <linux/fs_context.h>
16221+#include "aufs.h"
16222+
16223+struct au_fsctx_opts {
16224+ aufs_bindex_t bindex;
16225+ unsigned char skipped;
16226+ struct au_opt *opt, *opt_tail;
16227+ struct super_block *sb;
16228+ struct au_sbinfo *sbinfo;
16229+ struct au_opts opts;
16230+};
16231+
16232+/* stop extra interpretation of errno in mount(8), and strange error messages */
16233+static int cvt_err(int err)
16234+{
16235+ AuTraceErr(err);
16236+
16237+ switch (err) {
16238+ case -ENOENT:
16239+ case -ENOTDIR:
16240+ case -EEXIST:
16241+ case -EIO:
16242+ err = -EINVAL;
16243+ }
16244+ return err;
16245+}
16246+
16247+static int au_fsctx_reconfigure(struct fs_context *fc)
16248+{
16249+ int err, do_dx;
16250+ unsigned int mntflags;
16251+ struct dentry *root;
16252+ struct super_block *sb;
16253+ struct inode *inode;
16254+ struct au_fsctx_opts *a = fc->fs_private;
16255+
16256+ AuDbg("fc %p\n", fc);
16257+
16258+ root = fc->root;
16259+ sb = root->d_sb;
16260+ err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
16261+ if (!err) {
16262+ di_write_lock_child(root);
16263+ err = au_opts_verify(sb, fc->sb_flags, /*pending*/0);
16264+ aufs_write_unlock(root);
16265+ }
16266+
16267+ inode = d_inode(root);
16268+ inode_lock(inode);
16269+ err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
16270+ if (unlikely(err))
16271+ goto out;
16272+ di_write_lock_child(root);
16273+
16274+ /* au_opts_remount() may return an error */
16275+ err = au_opts_remount(sb, &a->opts);
16276+
16277+ if (au_ftest_opts(a->opts.flags, REFRESH))
16278+ au_remount_refresh(sb, au_ftest_opts(a->opts.flags,
16279+ REFRESH_IDOP));
16280+
16281+ if (au_ftest_opts(a->opts.flags, REFRESH_DYAOP)) {
16282+ mntflags = au_mntflags(sb);
16283+ do_dx = !!au_opt_test(mntflags, DIO);
16284+ au_dy_arefresh(do_dx);
16285+ }
16286+
16287+ au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
16288+ aufs_write_unlock(root);
16289+
16290+out:
16291+ inode_unlock(inode);
16292+ err = cvt_err(err);
16293+ AuTraceErr(err);
16294+
16295+ return err;
16296+}
16297+
16298+/* ---------------------------------------------------------------------- */
16299+
16300+static int au_fsctx_fill_super(struct super_block *sb, struct fs_context *fc)
16301+{
16302+ int err;
16303+ struct au_fsctx_opts *a = fc->fs_private;
16304+ struct au_sbinfo *sbinfo = a->sbinfo;
16305+ struct dentry *root;
16306+ struct inode *inode;
16307+
16308+ sbinfo->si_sb = sb;
16309+ sb->s_fs_info = sbinfo;
16310+ kobject_get(&sbinfo->si_kobj);
16311+
16312+ __si_write_lock(sb);
16313+ si_pid_set(sb);
16314+ au_sbilist_add(sb);
16315+
16316+ /* all timestamps always follow the ones on the branch */
16317+ sb->s_flags |= SB_NOATIME | SB_NODIRATIME;
16318+ sb->s_flags |= SB_I_VERSION; /* do we really need this? */
16319+ sb->s_op = &aufs_sop;
16320+ sb->s_d_op = &aufs_dop;
16321+ sb->s_magic = AUFS_SUPER_MAGIC;
16322+ sb->s_maxbytes = 0;
16323+ sb->s_stack_depth = 1;
16324+ au_export_init(sb);
16325+ au_xattr_init(sb);
16326+
16327+ err = au_alloc_root(sb);
16328+ if (unlikely(err)) {
16329+ si_write_unlock(sb);
16330+ goto out;
16331+ }
16332+ root = sb->s_root;
16333+ inode = d_inode(root);
16334+ ii_write_lock_parent(inode);
16335+ aufs_write_unlock(root);
16336+
16337+ /* lock vfs_inode first, then aufs. */
16338+ inode_lock(inode);
16339+ aufs_write_lock(root);
16340+ err = au_opts_mount(sb, &a->opts);
16341+ AuTraceErr(err);
16342+ if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
16343+ sb->s_d_op = &aufs_dop_noreval;
16344+ /* infofc(fc, "%ps", sb->s_d_op); */
16345+ pr_info("%ps\n", sb->s_d_op);
16346+ au_refresh_dop(root, /*force_reval*/0);
16347+ sbinfo->si_iop_array = aufs_iop_nogetattr;
16348+ au_refresh_iop(inode, /*force_getattr*/0);
16349+ }
16350+ aufs_write_unlock(root);
16351+ inode_unlock(inode);
16352+ if (!err)
16353+ goto out; /* success */
16354+
16355+ dput(root);
16356+ sb->s_root = NULL;
16357+
16358+out:
16359+ if (unlikely(err))
16360+ kobject_put(&sbinfo->si_kobj);
16361+ AuTraceErr(err);
16362+ err = cvt_err(err);
16363+ AuTraceErr(err);
16364+ return err;
16365+}
16366+
16367+static int au_fsctx_get_tree(struct fs_context *fc)
16368+{
16369+ int err;
16370+
16371+ AuDbg("fc %p\n", fc);
16372+ err = get_tree_nodev(fc, au_fsctx_fill_super);
16373+
16374+ AuTraceErr(err);
16375+ return err;
16376+}
16377+
16378+/* ---------------------------------------------------------------------- */
16379+
16380+static void au_fsctx_dump(struct au_opts *opts)
16381+{
16382+#ifdef CONFIG_AUFS_DEBUG
16383+ /* reduce stack space */
16384+ union {
16385+ struct au_opt_add *add;
16386+ struct au_opt_del *del;
16387+ struct au_opt_mod *mod;
16388+ struct au_opt_xino *xino;
16389+ struct au_opt_xino_itrunc *xino_itrunc;
16390+ struct au_opt_wbr_create *create;
16391+ } u;
16392+ struct au_opt *opt;
16393+
16394+ opt = opts->opt;
16395+ while (opt->type != Opt_tail) {
16396+ switch (opt->type) {
16397+ case Opt_add:
16398+ u.add = &opt->add;
16399+ AuDbg("add {b%d, %s, 0x%x, %p}\n",
16400+ u.add->bindex, u.add->pathname, u.add->perm,
16401+ u.add->path.dentry);
16402+ break;
16403+ case Opt_del:
16404+ fallthrough;
16405+ case Opt_idel:
16406+ u.del = &opt->del;
16407+ AuDbg("del {%s, %p}\n",
16408+ u.del->pathname, u.del->h_path.dentry);
16409+ break;
16410+ case Opt_mod:
16411+ fallthrough;
16412+ case Opt_imod:
16413+ u.mod = &opt->mod;
16414+ AuDbg("mod {%s, 0x%x, %p}\n",
16415+ u.mod->path, u.mod->perm, u.mod->h_root);
16416+ break;
16417+ case Opt_append:
16418+ u.add = &opt->add;
16419+ AuDbg("append {b%d, %s, 0x%x, %p}\n",
16420+ u.add->bindex, u.add->pathname, u.add->perm,
16421+ u.add->path.dentry);
16422+ break;
16423+ case Opt_prepend:
16424+ u.add = &opt->add;
16425+ AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
16426+ u.add->bindex, u.add->pathname, u.add->perm,
16427+ u.add->path.dentry);
16428+ break;
16429+
16430+ case Opt_dirwh:
16431+ AuDbg("dirwh %d\n", opt->dirwh);
16432+ break;
16433+ case Opt_rdcache:
16434+ AuDbg("rdcache %d\n", opt->rdcache);
16435+ break;
16436+ case Opt_rdblk:
16437+ AuDbg("rdblk %d\n", opt->rdblk);
16438+ break;
16439+ case Opt_rdhash:
16440+ AuDbg("rdhash %u\n", opt->rdhash);
16441+ break;
16442+
16443+ case Opt_xino:
16444+ u.xino = &opt->xino;
16445+ AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
16446+ break;
16447+
16448+#define au_fsctx_TF(name) \
16449+ case Opt_##name: \
16450+ if (opt->tf) \
16451+ AuLabel(name); \
16452+ else \
16453+ AuLabel(no##name); \
16454+ break;
16455+
16456+ /* simple true/false flag */
16457+ au_fsctx_TF(trunc_xino);
16458+ au_fsctx_TF(trunc_xib);
16459+ au_fsctx_TF(dirperm1);
16460+ au_fsctx_TF(plink);
16461+ au_fsctx_TF(shwh);
16462+ au_fsctx_TF(dio);
16463+ au_fsctx_TF(warn_perm);
16464+ au_fsctx_TF(verbose);
16465+ au_fsctx_TF(sum);
16466+ au_fsctx_TF(dirren);
16467+ au_fsctx_TF(acl);
16468+#undef au_fsctx_TF
16469+
16470+ case Opt_trunc_xino_path:
16471+ fallthrough;
16472+ case Opt_itrunc_xino:
16473+ u.xino_itrunc = &opt->xino_itrunc;
16474+ AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
16475+ break;
16476+ case Opt_noxino:
16477+ AuLabel(noxino);
16478+ break;
16479+
16480+ case Opt_list_plink:
16481+ AuLabel(list_plink);
16482+ break;
16483+ case Opt_udba:
16484+ AuDbg("udba %d, %s\n",
16485+ opt->udba, au_optstr_udba(opt->udba));
16486+ break;
16487+ case Opt_diropq_a:
16488+ AuLabel(diropq_a);
16489+ break;
16490+ case Opt_diropq_w:
16491+ AuLabel(diropq_w);
16492+ break;
16493+ case Opt_wsum:
16494+ AuLabel(wsum);
16495+ break;
16496+ case Opt_wbr_create:
16497+ u.create = &opt->wbr_create;
16498+ AuDbg("create %d, %s\n", u.create->wbr_create,
16499+ au_optstr_wbr_create(u.create->wbr_create));
16500+ switch (u.create->wbr_create) {
16501+ case AuWbrCreate_MFSV:
16502+ fallthrough;
16503+ case AuWbrCreate_PMFSV:
16504+ AuDbg("%d sec\n", u.create->mfs_second);
16505+ break;
16506+ case AuWbrCreate_MFSRR:
16507+ fallthrough;
16508+ case AuWbrCreate_TDMFS:
16509+ AuDbg("%llu watermark\n",
16510+ u.create->mfsrr_watermark);
16511+ break;
16512+ case AuWbrCreate_MFSRRV:
16513+ fallthrough;
16514+ case AuWbrCreate_TDMFSV:
16515+ fallthrough;
16516+ case AuWbrCreate_PMFSRRV:
16517+ AuDbg("%llu watermark, %d sec\n",
16518+ u.create->mfsrr_watermark,
16519+ u.create->mfs_second);
16520+ break;
16521+ }
16522+ break;
16523+ case Opt_wbr_copyup:
16524+ AuDbg("copyup %d, %s\n", opt->wbr_copyup,
16525+ au_optstr_wbr_copyup(opt->wbr_copyup));
16526+ break;
16527+ case Opt_fhsm_sec:
16528+ AuDbg("fhsm_sec %u\n", opt->fhsm_second);
16529+ break;
16530+
16531+ default:
16532+ AuDbg("type %d\n", opt->type);
16533+ BUG();
16534+ }
16535+ opt++;
16536+ }
16537+#endif
16538+}
16539+
16540+/* ---------------------------------------------------------------------- */
16541+
16542+/*
16543+ * For conditionally compiled mount options.
16544+ * Instead of fsparam_flag_no(), use this macro to distinguish ignore_silent.
16545+ */
16546+#define au_ignore_flag(name, action) \
16547+ fsparam_flag(name, action), \
16548+ fsparam_flag("no" name, Opt_ignore_silent)
16549+
16550+const struct fs_parameter_spec aufs_fsctx_paramspec[] = {
16551+ fsparam_string("br", Opt_br),
16552+
16553+ /* "add=%d:%s" or "ins=%d:%s" */
16554+ fsparam_string("add", Opt_add),
16555+ fsparam_string("ins", Opt_add),
16556+ fsparam_path("append", Opt_append),
16557+ fsparam_path("prepend", Opt_prepend),
16558+
16559+ fsparam_path("del", Opt_del),
16560+ /* fsparam_s32("idel", Opt_idel), */
16561+ fsparam_path("mod", Opt_mod),
16562+ /* fsparam_string("imod", Opt_imod), */
16563+
16564+ fsparam_s32("dirwh", Opt_dirwh),
16565+
16566+ fsparam_path("xino", Opt_xino),
16567+ fsparam_flag("noxino", Opt_noxino),
16568+ fsparam_flag_no("trunc_xino", Opt_trunc_xino),
16569+ /* "trunc_xino_v=%d:%d" */
16570+ /* fsparam_string("trunc_xino_v", Opt_trunc_xino_v), */
16571+ fsparam_path("trunc_xino", Opt_trunc_xino_path),
16572+ fsparam_s32("itrunc_xino", Opt_itrunc_xino),
16573+ /* fsparam_path("zxino", Opt_zxino), */
16574+ fsparam_flag_no("trunc_xib", Opt_trunc_xib),
16575+
16576+#ifdef CONFIG_PROC_FS
16577+ fsparam_flag_no("plink", Opt_plink),
16578+#else
16579+ au_ignore_flag("plink", Opt_ignore),
16580+#endif
16581+
16582+#ifdef CONFIG_AUFS_DEBUG
16583+ fsparam_flag("list_plink", Opt_list_plink),
16584+#endif
16585+
16586+ fsparam_string("udba", Opt_udba),
16587+
16588+ fsparam_flag_no("dio", Opt_dio),
16589+
16590+#ifdef CONFIG_AUFS_DIRREN
16591+ fsparam_flag_no("dirren", Opt_dirren),
16592+#else
16593+ au_ignore_flag("dirren", Opt_ignore),
16594+#endif
16595+
16596+#ifdef CONFIG_AUFS_FHSM
16597+ fsparam_s32("fhsm_sec", Opt_fhsm_sec),
16598+#else
16599+ fsparam_s32("fhsm_sec", Opt_ignore),
16600+#endif
16601+
16602+ /* always | a | whiteouted | w */
16603+ fsparam_string("diropq", Opt_diropq),
16604+
16605+ fsparam_flag_no("warn_perm", Opt_warn_perm),
16606+
16607+#ifdef CONFIG_AUFS_SHWH
16608+ fsparam_flag_no("shwh", Opt_shwh),
16609+#else
16610+ au_ignore_flag("shwh", Opt_err),
16611+#endif
16612+
16613+ fsparam_flag_no("dirperm1", Opt_dirperm1),
16614+
16615+ fsparam_flag_no("verbose", Opt_verbose),
16616+ fsparam_flag("v", Opt_verbose),
16617+ fsparam_flag("quiet", Opt_noverbose),
16618+ fsparam_flag("q", Opt_noverbose),
16619+ /* user-space may handle this */
16620+ fsparam_flag("silent", Opt_noverbose),
16621+
16622+ fsparam_flag_no("sum", Opt_sum),
16623+ fsparam_flag("wsum", Opt_wsum),
16624+
16625+ fsparam_s32("rdcache", Opt_rdcache),
16626+ /* "def" or s32 */
16627+ fsparam_string("rdblk", Opt_rdblk),
16628+ /* "def" or s32 */
16629+ fsparam_string("rdhash", Opt_rdhash),
16630+
16631+ fsparam_string("create", Opt_wbr_create),
16632+ fsparam_string("create_policy", Opt_wbr_create),
16633+ fsparam_string("cpup", Opt_wbr_copyup),
16634+ fsparam_string("copyup", Opt_wbr_copyup),
16635+ fsparam_string("copyup_policy", Opt_wbr_copyup),
16636+
16637+ /* generic VFS flag */
16638+#ifdef CONFIG_FS_POSIX_ACL
16639+ fsparam_flag_no("acl", Opt_acl),
16640+#else
22319442 16641+ au_ignore_flag("acl", Opt_ignore),
f4d37d76
AM
16642+#endif
16643+
16644+ /* internal use for the scripts */
16645+ fsparam_string("si", Opt_ignore_silent),
16646+
16647+ /* obsoleted, keep them temporary */
16648+ fsparam_flag("nodlgt", Opt_ignore_silent),
16649+ fsparam_flag("clean_plink", Opt_ignore),
16650+ fsparam_string("dirs", Opt_br),
16651+ fsparam_u32("debug", Opt_ignore),
16652+ /* "whiteout" or "all" */
16653+ fsparam_string("delete", Opt_ignore),
16654+ fsparam_string("imap", Opt_ignore),
16655+
16656+ /* temporary workaround, due to old mount(8)? */
16657+ fsparam_flag("relatime", Opt_ignore_silent),
16658+
16659+ {}
16660+};
16661+
16662+static int au_fsctx_parse_do_add(struct fs_context *fc, struct au_opt *opt,
16663+ char *brspec, size_t speclen,
16664+ aufs_bindex_t bindex)
16665+{
16666+ int err;
16667+ char *p;
16668+
16669+ AuDbg("brspec %s\n", brspec);
16670+
16671+ err = -ENOMEM;
16672+ if (!speclen)
16673+ speclen = strlen(brspec);
16674+ /* will be freed by au_fsctx_free() */
16675+ p = kmemdup_nul(brspec, speclen, GFP_NOFS);
16676+ if (unlikely(!p)) {
16677+ errorfc(fc, "failed in %s", brspec);
16678+ goto out;
16679+ }
16680+ err = au_opt_add(opt, p, fc->sb_flags, bindex);
16681+
16682+out:
16683+ AuTraceErr(err);
16684+ return err;
16685+}
16686+
16687+static int au_fsctx_parse_br(struct fs_context *fc, char *brspec)
16688+{
16689+ int err;
16690+ char *p;
16691+ struct au_fsctx_opts *a = fc->fs_private;
16692+ struct au_opt *opt = a->opt;
16693+ aufs_bindex_t bindex = a->bindex;
16694+
16695+ AuDbg("brspec %s\n", brspec);
16696+
16697+ err = -EINVAL;
16698+ while ((p = strsep(&brspec, ":")) && *p) {
16699+ err = au_fsctx_parse_do_add(fc, opt, p, /*len*/0, bindex);
16700+ AuTraceErr(err);
16701+ if (unlikely(err))
16702+ break;
16703+ bindex++;
16704+ opt++;
16705+ if (unlikely(opt > a->opt_tail)) {
16706+ err = -E2BIG;
16707+ bindex--;
16708+ opt--;
16709+ break;
16710+ }
16711+ opt->type = Opt_tail;
16712+ a->skipped = 1;
16713+ }
16714+ a->bindex = bindex;
16715+ a->opt = opt;
16716+
16717+ AuTraceErr(err);
16718+ return err;
16719+}
16720+
16721+static int au_fsctx_parse_add(struct fs_context *fc, char *addspec)
16722+{
16723+ int err, n;
16724+ char *p;
16725+ struct au_fsctx_opts *a = fc->fs_private;
16726+ struct au_opt *opt = a->opt;
16727+
16728+ err = -EINVAL;
16729+ p = strchr(addspec, ':');
16730+ if (unlikely(!p)) {
16731+ errorfc(fc, "bad arg in %s", addspec);
16732+ goto out;
16733+ }
16734+ *p++ = '\0';
16735+ err = kstrtoint(addspec, 0, &n);
16736+ if (unlikely(err)) {
16737+ errorfc(fc, "bad integer in %s", addspec);
16738+ goto out;
16739+ }
16740+ AuDbg("n %d\n", n);
16741+ err = au_fsctx_parse_do_add(fc, opt, p, /*len*/0, n);
16742+
16743+out:
16744+ AuTraceErr(err);
16745+ return err;
16746+}
16747+
16748+static int au_fsctx_parse_del(struct fs_context *fc, struct au_opt_del *del,
16749+ struct fs_parameter *param)
16750+{
16751+ int err;
16752+
16753+ err = -ENOMEM;
16754+ /* will be freed by au_fsctx_free() */
16755+ del->pathname = kmemdup_nul(param->string, param->size, GFP_NOFS);
16756+ if (unlikely(!del->pathname))
16757+ goto out;
16758+ AuDbg("del %s\n", del->pathname);
16759+ err = vfsub_kern_path(del->pathname, AuOpt_LkupDirFlags, &del->h_path);
16760+ if (unlikely(err))
16761+ errorfc(fc, "lookup failed %s (%d)", del->pathname, err);
16762+
16763+out:
16764+ AuTraceErr(err);
16765+ return err;
16766+}
16767+
16768+#if 0 /* reserved for future use */
16769+static int au_fsctx_parse_idel(struct fs_context *fc, struct au_opt_del *del,
16770+ aufs_bindex_t bindex)
16771+{
16772+ int err;
16773+ struct super_block *sb;
16774+ struct dentry *root;
16775+ struct au_fsctx_opts *a = fc->fs_private;
16776+
16777+ sb = a->sb;
16778+ AuDebugOn(!sb);
16779+
16780+ err = -EINVAL;
16781+ root = sb->s_root;
16782+ aufs_read_lock(root, AuLock_FLUSH);
16783+ if (bindex < 0 || au_sbbot(sb) < bindex) {
16784+ errorfc(fc, "out of bounds, %d", bindex);
16785+ goto out;
16786+ }
16787+
16788+ err = 0;
16789+ del->h_path.dentry = dget(au_h_dptr(root, bindex));
16790+ del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
16791+
16792+out:
16793+ aufs_read_unlock(root, !AuLock_IR);
16794+ AuTraceErr(err);
16795+ return err;
16796+}
16797+#endif
16798+
16799+static int au_fsctx_parse_mod(struct fs_context *fc, struct au_opt_mod *mod,
16800+ struct fs_parameter *param)
16801+{
16802+ int err;
16803+ struct path path;
16804+ char *p;
16805+
16806+ err = -ENOMEM;
16807+ /* will be freed by au_fsctx_free() */
16808+ mod->path = kmemdup_nul(param->string, param->size, GFP_NOFS);
16809+ if (unlikely(!mod->path))
16810+ goto out;
16811+
16812+ err = -EINVAL;
16813+ p = strchr(mod->path, '=');
16814+ if (unlikely(!p)) {
16815+ errorfc(fc, "no permission %s", mod->path);
16816+ goto out;
16817+ }
16818+
16819+ *p++ = 0;
16820+ err = vfsub_kern_path(mod->path, AuOpt_LkupDirFlags, &path);
16821+ if (unlikely(err)) {
16822+ errorfc(fc, "lookup failed %s (%d)", mod->path, err);
16823+ goto out;
16824+ }
16825+
16826+ mod->perm = au_br_perm_val(p);
16827+ AuDbg("mod path %s, perm 0x%x, %s", mod->path, mod->perm, p);
16828+ mod->h_root = dget(path.dentry);
16829+ path_put(&path);
16830+
16831+out:
16832+ AuTraceErr(err);
16833+ return err;
16834+}
16835+
16836+#if 0 /* reserved for future use */
16837+static int au_fsctx_parse_imod(struct fs_context *fc, struct au_opt_mod *mod,
16838+ char *ibrspec)
16839+{
16840+ int err, n;
16841+ char *p;
16842+ struct super_block *sb;
16843+ struct dentry *root;
16844+ struct au_fsctx_opts *a = fc->fs_private;
16845+
16846+ sb = a->sb;
16847+ AuDebugOn(!sb);
16848+
16849+ err = -EINVAL;
16850+ p = strchr(ibrspec, ':');
16851+ if (unlikely(!p)) {
16852+ errorfc(fc, "no index, %s", ibrspec);
16853+ goto out;
16854+ }
16855+ *p++ = '\0';
16856+ err = kstrtoint(ibrspec, 0, &n);
16857+ if (unlikely(err)) {
16858+ errorfc(fc, "bad integer in %s", ibrspec);
16859+ goto out;
16860+ }
16861+ AuDbg("n %d\n", n);
16862+
16863+ root = sb->s_root;
16864+ aufs_read_lock(root, AuLock_FLUSH);
16865+ if (n < 0 || au_sbbot(sb) < n) {
16866+ errorfc(fc, "out of bounds, %d", bindex);
16867+ goto out_root;
16868+ }
16869+
16870+ err = 0;
16871+ mod->perm = au_br_perm_val(p);
16872+ AuDbg("mod path %s, perm 0x%x, %s\n",
16873+ mod->path, mod->perm, p);
16874+ mod->h_root = dget(au_h_dptr(root, bindex));
16875+
16876+out_root:
16877+ aufs_read_unlock(root, !AuLock_IR);
16878+out:
16879+ AuTraceErr(err);
16880+ return err;
16881+}
16882+#endif
16883+
16884+static int au_fsctx_parse_xino(struct fs_context *fc,
16885+ struct au_opt_xino *xino,
16886+ struct fs_parameter *param)
16887+{
16888+ int err;
16889+ struct au_fsctx_opts *a = fc->fs_private;
16890+
16891+ err = -ENOMEM;
16892+ /* will be freed by au_opts_free() */
16893+ xino->path = kmemdup_nul(param->string, param->size, GFP_NOFS);
16894+ if (unlikely(!xino->path))
16895+ goto out;
16896+ AuDbg("path %s\n", xino->path);
16897+
16898+ xino->file = au_xino_create(a->sb, xino->path, /*silent*/0,
16899+ /*wbrtop*/0);
16900+ err = PTR_ERR(xino->file);
16901+ if (IS_ERR(xino->file)) {
16902+ xino->file = NULL;
16903+ goto out;
16904+ }
16905+
16906+ err = 0;
16907+ if (unlikely(a->sb && xino->file->f_path.dentry->d_sb == a->sb)) {
16908+ err = -EINVAL;
16909+ errorfc(fc, "%s must be outside", xino->path);
16910+ }
16911+
16912+out:
16913+ AuTraceErr(err);
16914+ return err;
16915+}
16916+
16917+static
16918+int au_fsctx_parse_xino_itrunc_path(struct fs_context *fc,
16919+ struct au_opt_xino_itrunc *xino_itrunc,
16920+ char *pathname)
16921+{
16922+ int err;
16923+ aufs_bindex_t bbot, bindex;
16924+ struct path path;
16925+ struct dentry *root;
16926+ struct au_fsctx_opts *a = fc->fs_private;
16927+
16928+ AuDebugOn(!a->sb);
16929+
16930+ err = vfsub_kern_path(pathname, AuOpt_LkupDirFlags, &path);
16931+ if (unlikely(err)) {
16932+ errorfc(fc, "lookup failed %s (%d)", pathname, err);
16933+ goto out;
16934+ }
16935+
16936+ xino_itrunc->bindex = -1;
16937+ root = a->sb->s_root;
16938+ aufs_read_lock(root, AuLock_FLUSH);
16939+ bbot = au_sbbot(a->sb);
16940+ for (bindex = 0; bindex <= bbot; bindex++) {
16941+ if (au_h_dptr(root, bindex) == path.dentry) {
16942+ xino_itrunc->bindex = bindex;
16943+ break;
16944+ }
16945+ }
16946+ aufs_read_unlock(root, !AuLock_IR);
16947+ path_put(&path);
16948+
16949+ if (unlikely(xino_itrunc->bindex < 0)) {
16950+ err = -EINVAL;
16951+ errorfc(fc, "no such branch %s", pathname);
16952+ }
16953+
16954+out:
16955+ AuTraceErr(err);
16956+ return err;
16957+}
16958+
16959+static int au_fsctx_parse_xino_itrunc(struct fs_context *fc,
16960+ struct au_opt_xino_itrunc *xino_itrunc,
16961+ unsigned int bindex)
16962+{
16963+ int err;
16964+ aufs_bindex_t bbot;
16965+ struct super_block *sb;
16966+ struct au_fsctx_opts *a = fc->fs_private;
16967+
16968+ sb = a->sb;
16969+ AuDebugOn(!sb);
16970+
16971+ err = 0;
16972+ si_noflush_read_lock(sb);
16973+ bbot = au_sbbot(sb);
16974+ si_read_unlock(sb);
16975+ if (bindex <= bbot)
16976+ xino_itrunc->bindex = bindex;
16977+ else {
16978+ err = -EINVAL;
16979+ errorfc(fc, "out of bounds, %u", bindex);
16980+ }
16981+
16982+ AuTraceErr(err);
16983+ return err;
16984+}
16985+
16986+static int au_fsctx_parse_param(struct fs_context *fc, struct fs_parameter *param)
16987+{
16988+ int err, token;
16989+ struct fs_parse_result result;
16990+ struct au_fsctx_opts *a = fc->fs_private;
16991+ struct au_opt *opt = a->opt;
16992+
16993+ AuDbg("fc %p, param {key %s, string %s}\n",
16994+ fc, param->key, param->string);
16995+ err = fs_parse(fc, aufs_fsctx_paramspec, param, &result);
16996+ if (unlikely(err < 0))
16997+ goto out;
16998+ token = err;
16999+ AuDbg("token %d, res{negated %d, uint64 %llu}\n",
17000+ token, result.negated, result.uint_64);
17001+
17002+ err = -EINVAL;
17003+ a->skipped = 0;
17004+ switch (token) {
17005+ case Opt_br:
17006+ err = au_fsctx_parse_br(fc, param->string);
17007+ break;
17008+ case Opt_add:
17009+ err = au_fsctx_parse_add(fc, param->string);
17010+ break;
17011+ case Opt_append:
17012+ err = au_fsctx_parse_do_add(fc, opt, param->string, param->size,
17013+ /*dummy bindex*/1);
17014+ break;
17015+ case Opt_prepend:
17016+ err = au_fsctx_parse_do_add(fc, opt, param->string, param->size,
17017+ /*bindex*/0);
17018+ break;
17019+
17020+ case Opt_del:
17021+ err = au_fsctx_parse_del(fc, &opt->del, param);
17022+ break;
17023+#if 0 /* reserved for future use */
17024+ case Opt_idel:
17025+ if (!a->sb) {
17026+ err = 0;
17027+ a->skipped = 1;
17028+ break;
17029+ }
17030+ del->pathname = "(indexed)";
17031+ err = au_opts_parse_idel(fc, &opt->del, result.uint_32);
17032+ break;
17033+#endif
17034+
17035+ case Opt_mod:
17036+ err = au_fsctx_parse_mod(fc, &opt->mod, param);
17037+ break;
17038+#ifdef IMOD /* reserved for future use */
17039+ case Opt_imod:
17040+ if (!a->sb) {
17041+ err = 0;
17042+ a->skipped = 1;
17043+ break;
17044+ }
17045+ u.mod->path = "(indexed)";
17046+ err = au_opts_parse_imod(fc, &opt->mod, param->string);
17047+ break;
17048+#endif
17049+
17050+ case Opt_xino:
17051+ err = au_fsctx_parse_xino(fc, &opt->xino, param);
17052+ break;
17053+ case Opt_trunc_xino_path:
17054+ if (!a->sb) {
17055+ errorfc(fc, "no such branch %s", param->string);
17056+ break;
17057+ }
17058+ err = au_fsctx_parse_xino_itrunc_path(fc, &opt->xino_itrunc,
17059+ param->string);
17060+ break;
17061+#if 0
17062+ case Opt_trunc_xino_v:
17063+ if (!a->sb) {
17064+ err = 0;
17065+ a->skipped = 1;
17066+ break;
17067+ }
17068+ err = au_fsctx_parse_xino_itrunc_path(fc, &opt->xino_itrunc,
17069+ param->string);
17070+ break;
17071+#endif
17072+ case Opt_itrunc_xino:
17073+ if (!a->sb) {
17074+ errorfc(fc, "out of bounds %s", param->string);
17075+ break;
17076+ }
17077+ err = au_fsctx_parse_xino_itrunc(fc, &opt->xino_itrunc,
17078+ result.int_32);
17079+ break;
17080+
17081+ case Opt_dirwh:
17082+ err = 0;
17083+ opt->dirwh = result.int_32;
17084+ break;
17085+
17086+ case Opt_rdcache:
17087+ if (unlikely(result.int_32 > AUFS_RDCACHE_MAX)) {
17088+ errorfc(fc, "rdcache must be smaller than %d",
17089+ AUFS_RDCACHE_MAX);
17090+ break;
17091+ }
17092+ err = 0;
17093+ opt->rdcache = result.int_32;
17094+ break;
17095+
17096+ case Opt_rdblk:
17097+ err = 0;
17098+ opt->rdblk = AUFS_RDBLK_DEF;
17099+ if (!strcmp(param->string, "def"))
17100+ break;
17101+
17102+ err = kstrtoint(param->string, 0, &result.int_32);
17103+ if (unlikely(err)) {
17104+ errorfc(fc, "bad value in %s", param->key);
17105+ break;
17106+ }
17107+ err = -EINVAL;
17108+ if (unlikely(result.int_32 < 0
17109+ || result.int_32 > KMALLOC_MAX_SIZE)) {
17110+ errorfc(fc, "bad value in %s", param->key);
17111+ break;
17112+ }
17113+ if (unlikely(result.int_32 && result.int_32 < NAME_MAX)) {
17114+ errorfc(fc, "rdblk must be larger than %d", NAME_MAX);
17115+ break;
17116+ }
17117+ err = 0;
17118+ opt->rdblk = result.int_32;
17119+ break;
17120+
17121+ case Opt_rdhash:
17122+ err = 0;
17123+ opt->rdhash = AUFS_RDHASH_DEF;
17124+ if (!strcmp(param->string, "def"))
17125+ break;
17126+
17127+ err = kstrtoint(param->string, 0, &result.int_32);
17128+ if (unlikely(err)) {
17129+ errorfc(fc, "bad value in %s", param->key);
17130+ break;
17131+ }
17132+ /* how about zero? */
17133+ if (result.int_32 < 0
17134+ || result.int_32 * sizeof(struct hlist_head)
17135+ > KMALLOC_MAX_SIZE) {
17136+ err = -EINVAL;
17137+ errorfc(fc, "bad integer in %s", param->key);
17138+ break;
17139+ }
17140+ opt->rdhash = result.int_32;
17141+ break;
17142+
17143+ case Opt_diropq:
17144+ /*
17145+ * As other options, fs/aufs/opts.c can handle these strings by
17146+ * match_token(). But "diropq=" is deprecated now and will
17147+ * never have other value. So simple strcmp() is enough here.
17148+ */
17149+ if (!strcmp(param->string, "a") ||
17150+ !strcmp(param->string, "always")) {
17151+ err = 0;
17152+ opt->type = Opt_diropq_a;
17153+ } else if (!strcmp(param->string, "w") ||
17154+ !strcmp(param->string, "whiteouted")) {
17155+ err = 0;
17156+ opt->type = Opt_diropq_w;
17157+ } else
17158+ errorfc(fc, "unknown value %s", param->string);
17159+ break;
17160+
17161+ case Opt_udba:
17162+ opt->udba = au_udba_val(param->string);
17163+ if (opt->udba >= 0)
17164+ err = 0;
17165+ else
17166+ errorf(fc, "wrong value, %s", param->string);
17167+ break;
17168+
17169+ case Opt_wbr_create:
17170+ opt->wbr_create.wbr_create
17171+ = au_wbr_create_val(param->string, &opt->wbr_create);
17172+ if (opt->wbr_create.wbr_create >= 0)
17173+ err = 0;
17174+ else
17175+ errorf(fc, "wrong value, %s", param->key);
17176+ break;
17177+
17178+ case Opt_wbr_copyup:
17179+ opt->wbr_copyup = au_wbr_copyup_val(param->string);
17180+ if (opt->wbr_copyup >= 0)
17181+ err = 0;
17182+ else
17183+ errorfc(fc, "wrong value, %s", param->key);
17184+ break;
17185+
17186+ case Opt_fhsm_sec:
17187+ if (unlikely(result.int_32 < 0)) {
17188+ errorfc(fc, "bad integer in %s\n", param->key);
17189+ break;
17190+ }
17191+ err = 0;
17192+ if (sysaufs_brs)
17193+ opt->fhsm_second = result.int_32;
17194+ else
17195+ warnfc(fc, "ignored %s %s", param->key, param->string);
17196+ break;
17197+
17198+ /* simple true/false flag */
17199+#define au_fsctx_TF(name) \
17200+ case Opt_##name: \
17201+ err = 0; \
17202+ opt->tf = !result.negated; \
22319442 17203+ break
f4d37d76
AM
17204+ au_fsctx_TF(trunc_xino);
17205+ au_fsctx_TF(trunc_xib);
17206+ au_fsctx_TF(dirperm1);
17207+ au_fsctx_TF(plink);
17208+ au_fsctx_TF(shwh);
17209+ au_fsctx_TF(dio);
17210+ au_fsctx_TF(warn_perm);
17211+ au_fsctx_TF(verbose);
17212+ au_fsctx_TF(sum);
17213+ au_fsctx_TF(dirren);
17214+ au_fsctx_TF(acl);
17215+#undef au_fsctx_TF
17216+
17217+ case Opt_noverbose:
17218+ err = 0;
17219+ opt->type = Opt_verbose;
17220+ opt->tf = false;
17221+ break;
17222+
17223+ case Opt_noxino:
17224+ fallthrough;
17225+ case Opt_list_plink:
17226+ fallthrough;
17227+ case Opt_wsum:
17228+ err = 0;
17229+ break;
17230+
17231+ case Opt_ignore:
17232+ warnfc(fc, "ignored %s", param->key);
17233+ fallthrough;
17234+ case Opt_ignore_silent:
17235+ a->skipped = 1;
17236+ err = 0;
17237+ break;
17238+ default:
17239+ a->skipped = 1;
17240+ err = -ENOPARAM;
17241+ break;
17242+ }
17243+ if (unlikely(err))
17244+ goto out;
17245+ if (a->skipped)
17246+ goto out;
17247+
17248+ switch (token) {
17249+ case Opt_br:
17250+ fallthrough;
17251+ case Opt_noverbose:
17252+ fallthrough;
17253+ case Opt_diropq:
17254+ break;
17255+ default:
17256+ opt->type = token;
17257+ break;
521ced18 17258+ }
f4d37d76
AM
17259+ opt++;
17260+ if (unlikely(opt > a->opt_tail)) {
17261+ err = -E2BIG;
17262+ opt--;
17263+ }
17264+ opt->type = Opt_tail;
17265+ a->opt = opt;
521ced18 17266+
521ced18
JR
17267+out:
17268+ return err;
521ced18
JR
17269+}
17270+
9dbd164d 17271+/*
f4d37d76
AM
17272+ * these options accept both 'name=val' and 'name:val' form.
17273+ * some accept optional '=' in its value.
17274+ * eg. br:/br1=rw:/br2=ro and br=/br1=rw:/br2=ro
9dbd164d 17275+ */
f4d37d76 17276+static inline unsigned int is_colonopt(char *str)
2dfbb274 17277+{
f4d37d76
AM
17278+#define do_test(name) \
17279+ if (!strncmp(str, name ":", sizeof(name))) \
22319442 17280+ return sizeof(name) - 1
f4d37d76
AM
17281+ do_test("br");
17282+ do_test("add");
17283+ do_test("ins");
17284+ do_test("append");
17285+ do_test("prepend");
17286+ do_test("del");
17287+ /* do_test("idel"); */
17288+ do_test("mod");
17289+ /* do_test("imod"); */
17290+#undef do_test
2dfbb274 17291+
f4d37d76 17292+ return 0;
2dfbb274
AM
17293+}
17294+
f4d37d76 17295+static int au_fsctx_parse_monolithic(struct fs_context *fc, void *data)
dece6358 17296+{
4a4d8108 17297+ int err;
f4d37d76
AM
17298+ unsigned int u;
17299+ char *str;
17300+ struct au_fsctx_opts *a = fc->fs_private;
17301+
17302+ str = data;
17303+ AuDbg("str %s\n", str);
17304+ while (str) {
17305+ u = is_colonopt(str);
17306+ if (u)
17307+ str[u] = '=';
17308+ str = strchr(str, ',');
17309+ if (!str)
17310+ break;
17311+ str++;
b912730e 17312+ }
f4d37d76
AM
17313+ str = data;
17314+ AuDbg("str %s\n", str);
b912730e 17315+
f4d37d76 17316+ err = generic_parse_monolithic(fc, str);
9dbd164d 17317+ AuTraceErr(err);
f4d37d76
AM
17318+ au_fsctx_dump(&a->opts);
17319+
4a4d8108
AM
17320+ return err;
17321+}
17322+
17323+/* ---------------------------------------------------------------------- */
17324+
f4d37d76 17325+static void au_fsctx_opts_free(struct au_opts *opts)
4a4d8108 17326+{
f4d37d76 17327+ struct au_opt *opt;
4a4d8108 17328+
f4d37d76
AM
17329+ opt = opts->opt;
17330+ while (opt->type != Opt_tail) {
17331+ switch (opt->type) {
17332+ case Opt_add:
17333+ fallthrough;
17334+ case Opt_append:
17335+ fallthrough;
17336+ case Opt_prepend:
17337+ kfree(opt->add.pathname);
17338+ path_put(&opt->add.path);
17339+ break;
17340+ case Opt_del:
17341+ kfree(opt->del.pathname);
17342+ fallthrough;
17343+ case Opt_idel:
17344+ path_put(&opt->del.h_path);
17345+ break;
17346+ case Opt_mod:
17347+ kfree(opt->mod.path);
17348+ fallthrough;
17349+ case Opt_imod:
17350+ dput(opt->mod.h_root);
17351+ break;
17352+ case Opt_xino:
17353+ kfree(opt->xino.path);
17354+ fput(opt->xino.file);
17355+ break;
17356+ }
17357+ opt++;
17358+ }
dece6358
AM
17359+}
17360+
f4d37d76 17361+static void au_fsctx_free(struct fs_context *fc)
dece6358 17362+{
f4d37d76 17363+ struct au_fsctx_opts *a = fc->fs_private;
4a4d8108 17364+
f4d37d76
AM
17365+ /* fs_type=%p, root=%pD */
17366+ AuDbg("fc %p{sb_flags 0x%x, sb_flags_mask 0x%x, purpose %u\n",
17367+ fc, fc->sb_flags, fc->sb_flags_mask, fc->purpose);
1308ab2a 17368+
f4d37d76
AM
17369+ kobject_put(&a->sbinfo->si_kobj);
17370+ au_fsctx_opts_free(&a->opts);
17371+ free_page((unsigned long)a->opts.opt);
17372+ au_kfree_rcu(a);
dece6358 17373+}
4a4d8108 17374+
f4d37d76
AM
17375+static const struct fs_context_operations au_fsctx_ops = {
17376+ .free = au_fsctx_free,
17377+ .parse_param = au_fsctx_parse_param,
17378+ .parse_monolithic = au_fsctx_parse_monolithic,
17379+ .get_tree = au_fsctx_get_tree,
17380+ .reconfigure = au_fsctx_reconfigure
17381+ /*
17382+ * nfs4 requires ->dup()? No.
17383+ * I don't know what is this ->dup() for.
17384+ */
17385+};
17386+
17387+int aufs_fsctx_init(struct fs_context *fc)
febd17d6
JR
17388+{
17389+ int err;
f4d37d76 17390+ struct au_fsctx_opts *a;
febd17d6 17391+
f4d37d76
AM
17392+ /* fs_type=%p, root=%pD */
17393+ AuDbg("fc %p{sb_flags 0x%x, sb_flags_mask 0x%x, purpose %u\n",
17394+ fc, fc->sb_flags, fc->sb_flags_mask, fc->purpose);
febd17d6 17395+
f4d37d76
AM
17396+ /* they will be freed by au_fsctx_free() */
17397+ err = -ENOMEM;
17398+ a = kzalloc(sizeof(*a), GFP_NOFS);
17399+ if (unlikely(!a))
febd17d6 17400+ goto out;
f4d37d76
AM
17401+ a->bindex = 0;
17402+ a->opts.opt = (void *)__get_free_page(GFP_NOFS);
17403+ if (unlikely(!a->opts.opt))
17404+ goto out_a;
17405+ a->opt = a->opts.opt;
17406+ a->opt->type = Opt_tail;
17407+ a->opts.max_opt = PAGE_SIZE / sizeof(*a->opts.opt);
17408+ a->opt_tail = a->opt + a->opts.max_opt - 1;
17409+ a->opts.sb_flags = fc->sb_flags;
17410+
17411+ a->sb = NULL;
17412+ if (fc->root) {
17413+ AuDebugOn(fc->purpose != FS_CONTEXT_FOR_RECONFIGURE);
17414+ a->opts.flags = AuOpts_REMOUNT;
17415+ a->sb = fc->root->d_sb;
17416+ a->sbinfo = au_sbi(a->sb);
17417+ kobject_get(&a->sbinfo->si_kobj);
17418+ } else {
17419+ a->sbinfo = au_si_alloc(a->sb);
17420+ AuDebugOn(!a->sbinfo);
17421+ err = PTR_ERR(a->sbinfo);
17422+ if (IS_ERR(a->sbinfo))
17423+ goto out_opt;
17424+ au_rw_write_unlock(&a->sbinfo->si_rwsem);
17425+ }
febd17d6 17426+
f4d37d76
AM
17427+ err = 0;
17428+ fc->fs_private = a;
17429+ fc->ops = &au_fsctx_ops;
17430+ goto out; /* success */
febd17d6 17431+
f4d37d76
AM
17432+out_opt:
17433+ free_page((unsigned long)a->opts.opt);
17434+out_a:
17435+ au_kfree_rcu(a);
febd17d6 17436+out:
f4d37d76 17437+ AuTraceErr(err);
febd17d6
JR
17438+ return err;
17439+}
7f207e10 17440diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
eca34b5c 17441--- /usr/share/empty/fs/aufs/fstype.h 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 17442+++ linux/fs/aufs/fstype.h 2022-11-05 23:02:18.965889284 +0100
062440b3
AM
17443@@ -0,0 +1,401 @@
17444+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 17445+/*
0dcfbb52 17446+ * Copyright (C) 2005-2022 Junjiro R. Okajima
4a4d8108 17447+ *
0dcfbb52 17448+ * This program is free software; you can redistribute it and/or modify
4a4d8108
AM
17449+ * it under the terms of the GNU General Public License as published by
17450+ * the Free Software Foundation; either version 2 of the License, or
17451+ * (at your option) any later version.
17452+ *
17453+ * This program is distributed in the hope that it will be useful,
17454+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17455+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17456+ * GNU General Public License for more details.
17457+ *
17458+ * You should have received a copy of the GNU General Public License
523b37e3 17459+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
17460+ */
17461+
17462+/*
17463+ * judging filesystem type
17464+ */
17465+
17466+#ifndef __AUFS_FSTYPE_H__
17467+#define __AUFS_FSTYPE_H__
17468+
17469+#ifdef __KERNEL__
17470+
17471+#include <linux/fs.h>
17472+#include <linux/magic.h>
b912730e 17473+#include <linux/nfs_fs.h>
b95c5147 17474+#include <linux/romfs_fs.h>
4a4d8108
AM
17475+
17476+static inline int au_test_aufs(struct super_block *sb)
17477+{
17478+ return sb->s_magic == AUFS_SUPER_MAGIC;
17479+}
17480+
17481+static inline const char *au_sbtype(struct super_block *sb)
17482+{
17483+ return sb->s_type->name;
17484+}
1308ab2a 17485+
17486+static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
17487+{
f0c0a007 17488+#if IS_ENABLED(CONFIG_ISO9660_FS)
2000de60 17489+ return sb->s_magic == ISOFS_SUPER_MAGIC;
dece6358
AM
17490+#else
17491+ return 0;
17492+#endif
17493+}
17494+
1308ab2a 17495+static inline int au_test_romfs(struct super_block *sb __maybe_unused)
dece6358 17496+{
f0c0a007 17497+#if IS_ENABLED(CONFIG_ROMFS_FS)
2000de60 17498+ return sb->s_magic == ROMFS_MAGIC;
dece6358
AM
17499+#else
17500+ return 0;
17501+#endif
17502+}
17503+
1308ab2a 17504+static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
dece6358 17505+{
f0c0a007 17506+#if IS_ENABLED(CONFIG_CRAMFS)
1308ab2a 17507+ return sb->s_magic == CRAMFS_MAGIC;
17508+#endif
17509+ return 0;
17510+}
17511+
17512+static inline int au_test_nfs(struct super_block *sb __maybe_unused)
17513+{
f0c0a007 17514+#if IS_ENABLED(CONFIG_NFS_FS)
1308ab2a 17515+ return sb->s_magic == NFS_SUPER_MAGIC;
dece6358
AM
17516+#else
17517+ return 0;
17518+#endif
17519+}
17520+
1308ab2a 17521+static inline int au_test_fuse(struct super_block *sb __maybe_unused)
dece6358 17522+{
f0c0a007 17523+#if IS_ENABLED(CONFIG_FUSE_FS)
1308ab2a 17524+ return sb->s_magic == FUSE_SUPER_MAGIC;
dece6358
AM
17525+#else
17526+ return 0;
17527+#endif
17528+}
17529+
1308ab2a 17530+static inline int au_test_xfs(struct super_block *sb __maybe_unused)
dece6358 17531+{
f0c0a007 17532+#if IS_ENABLED(CONFIG_XFS_FS)
1308ab2a 17533+ return sb->s_magic == XFS_SB_MAGIC;
dece6358
AM
17534+#else
17535+ return 0;
17536+#endif
17537+}
17538+
1308ab2a 17539+static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
dece6358 17540+{
1308ab2a 17541+#ifdef CONFIG_TMPFS
17542+ return sb->s_magic == TMPFS_MAGIC;
17543+#else
17544+ return 0;
dece6358 17545+#endif
dece6358
AM
17546+}
17547+
1308ab2a 17548+static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
1facf9fc 17549+{
f0c0a007 17550+#if IS_ENABLED(CONFIG_ECRYPT_FS)
1308ab2a 17551+ return !strcmp(au_sbtype(sb), "ecryptfs");
17552+#else
17553+ return 0;
17554+#endif
1facf9fc 17555+}
17556+
1308ab2a 17557+static inline int au_test_ramfs(struct super_block *sb)
17558+{
17559+ return sb->s_magic == RAMFS_MAGIC;
17560+}
17561+
17562+static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
17563+{
f0c0a007 17564+#if IS_ENABLED(CONFIG_UBIFS_FS)
1308ab2a 17565+ return sb->s_magic == UBIFS_SUPER_MAGIC;
17566+#else
17567+ return 0;
17568+#endif
17569+}
17570+
17571+static inline int au_test_procfs(struct super_block *sb __maybe_unused)
17572+{
17573+#ifdef CONFIG_PROC_FS
17574+ return sb->s_magic == PROC_SUPER_MAGIC;
17575+#else
17576+ return 0;
17577+#endif
17578+}
17579+
17580+static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
17581+{
17582+#ifdef CONFIG_SYSFS
17583+ return sb->s_magic == SYSFS_MAGIC;
17584+#else
17585+ return 0;
17586+#endif
17587+}
17588+
17589+static inline int au_test_configfs(struct super_block *sb __maybe_unused)
17590+{
f0c0a007 17591+#if IS_ENABLED(CONFIG_CONFIGFS_FS)
1308ab2a 17592+ return sb->s_magic == CONFIGFS_MAGIC;
17593+#else
17594+ return 0;
17595+#endif
17596+}
17597+
17598+static inline int au_test_minix(struct super_block *sb __maybe_unused)
17599+{
f0c0a007 17600+#if IS_ENABLED(CONFIG_MINIX_FS)
1308ab2a 17601+ return sb->s_magic == MINIX3_SUPER_MAGIC
17602+ || sb->s_magic == MINIX2_SUPER_MAGIC
17603+ || sb->s_magic == MINIX2_SUPER_MAGIC2
17604+ || sb->s_magic == MINIX_SUPER_MAGIC
17605+ || sb->s_magic == MINIX_SUPER_MAGIC2;
17606+#else
17607+ return 0;
17608+#endif
17609+}
17610+
1308ab2a 17611+static inline int au_test_fat(struct super_block *sb __maybe_unused)
17612+{
f0c0a007 17613+#if IS_ENABLED(CONFIG_FAT_FS)
1308ab2a 17614+ return sb->s_magic == MSDOS_SUPER_MAGIC;
17615+#else
17616+ return 0;
17617+#endif
17618+}
17619+
17620+static inline int au_test_msdos(struct super_block *sb)
17621+{
17622+ return au_test_fat(sb);
17623+}
17624+
17625+static inline int au_test_vfat(struct super_block *sb)
17626+{
17627+ return au_test_fat(sb);
17628+}
17629+
17630+static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
17631+{
17632+#ifdef CONFIG_SECURITYFS
17633+ return sb->s_magic == SECURITYFS_MAGIC;
17634+#else
17635+ return 0;
17636+#endif
17637+}
17638+
17639+static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
17640+{
f0c0a007 17641+#if IS_ENABLED(CONFIG_SQUASHFS)
1308ab2a 17642+ return sb->s_magic == SQUASHFS_MAGIC;
17643+#else
17644+ return 0;
17645+#endif
17646+}
17647+
17648+static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
17649+{
f0c0a007 17650+#if IS_ENABLED(CONFIG_BTRFS_FS)
1308ab2a 17651+ return sb->s_magic == BTRFS_SUPER_MAGIC;
17652+#else
17653+ return 0;
17654+#endif
17655+}
17656+
17657+static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
17658+{
f0c0a007 17659+#if IS_ENABLED(CONFIG_XENFS)
1308ab2a 17660+ return sb->s_magic == XENFS_SUPER_MAGIC;
17661+#else
17662+ return 0;
17663+#endif
17664+}
17665+
17666+static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
17667+{
17668+#ifdef CONFIG_DEBUG_FS
17669+ return sb->s_magic == DEBUGFS_MAGIC;
17670+#else
17671+ return 0;
17672+#endif
17673+}
17674+
17675+static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
17676+{
f0c0a007 17677+#if IS_ENABLED(CONFIG_NILFS)
1308ab2a 17678+ return sb->s_magic == NILFS_SUPER_MAGIC;
17679+#else
17680+ return 0;
17681+#endif
17682+}
17683+
4a4d8108
AM
17684+static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
17685+{
f0c0a007 17686+#if IS_ENABLED(CONFIG_HFSPLUS_FS)
4a4d8108
AM
17687+ return sb->s_magic == HFSPLUS_SUPER_MAGIC;
17688+#else
17689+ return 0;
17690+#endif
17691+}
17692+
1308ab2a 17693+/* ---------------------------------------------------------------------- */
17694+/*
17695+ * they can't be an aufs branch.
17696+ */
17697+static inline int au_test_fs_unsuppoted(struct super_block *sb)
17698+{
17699+ return
17700+#ifndef CONFIG_AUFS_BR_RAMFS
17701+ au_test_ramfs(sb) ||
17702+#endif
17703+ au_test_procfs(sb)
17704+ || au_test_sysfs(sb)
17705+ || au_test_configfs(sb)
17706+ || au_test_debugfs(sb)
17707+ || au_test_securityfs(sb)
17708+ || au_test_xenfs(sb)
17709+ || au_test_ecryptfs(sb)
17710+ /* || !strcmp(au_sbtype(sb), "unionfs") */
17711+ || au_test_aufs(sb); /* will be supported in next version */
17712+}
17713+
1308ab2a 17714+static inline int au_test_fs_remote(struct super_block *sb)
17715+{
17716+ return !au_test_tmpfs(sb)
17717+#ifdef CONFIG_AUFS_BR_RAMFS
17718+ && !au_test_ramfs(sb)
17719+#endif
17720+ && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
17721+}
17722+
17723+/* ---------------------------------------------------------------------- */
17724+
17725+/*
17726+ * Note: these functions (below) are created after reading ->getattr() in all
17727+ * filesystems under linux/fs. it means we have to do so in every update...
17728+ */
17729+
17730+/*
17731+ * some filesystems require getattr to refresh the inode attributes before
17732+ * referencing.
17733+ * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
17734+ * and leave the work for d_revalidate()
17735+ */
17736+static inline int au_test_fs_refresh_iattr(struct super_block *sb)
17737+{
17738+ return au_test_nfs(sb)
17739+ || au_test_fuse(sb)
1308ab2a 17740+ /* || au_test_btrfs(sb) */ /* untested */
1308ab2a 17741+ ;
17742+}
17743+
17744+/*
17745+ * filesystems which don't maintain i_size or i_blocks.
17746+ */
17747+static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
17748+{
17749+ return au_test_xfs(sb)
4a4d8108
AM
17750+ || au_test_btrfs(sb)
17751+ || au_test_ubifs(sb)
17752+ || au_test_hfsplus(sb) /* maintained, but incorrect */
1308ab2a 17753+ /* || au_test_minix(sb) */ /* untested */
17754+ ;
17755+}
17756+
17757+/*
17758+ * filesystems which don't store the correct value in some of their inode
17759+ * attributes.
17760+ */
17761+static inline int au_test_fs_bad_iattr(struct super_block *sb)
17762+{
17763+ return au_test_fs_bad_iattr_size(sb)
1308ab2a 17764+ || au_test_fat(sb)
17765+ || au_test_msdos(sb)
17766+ || au_test_vfat(sb);
1facf9fc 17767+}
17768+
17769+/* they don't check i_nlink in link(2) */
17770+static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
17771+{
17772+ return au_test_tmpfs(sb)
17773+#ifdef CONFIG_AUFS_BR_RAMFS
17774+ || au_test_ramfs(sb)
17775+#endif
4a4d8108 17776+ || au_test_ubifs(sb)
4a4d8108 17777+ || au_test_hfsplus(sb);
1facf9fc 17778+}
17779+
17780+/*
17781+ * filesystems which sets S_NOATIME and S_NOCMTIME.
17782+ */
17783+static inline int au_test_fs_notime(struct super_block *sb)
17784+{
17785+ return au_test_nfs(sb)
17786+ || au_test_fuse(sb)
dece6358 17787+ || au_test_ubifs(sb)
1facf9fc 17788+ ;
17789+}
17790+
1facf9fc 17791+/* temporary support for i#1 in cramfs */
17792+static inline int au_test_fs_unique_ino(struct inode *inode)
17793+{
17794+ if (au_test_cramfs(inode->i_sb))
17795+ return inode->i_ino != 1;
17796+ return 1;
17797+}
17798+
17799+/* ---------------------------------------------------------------------- */
17800+
17801+/*
17802+ * the filesystem where the xino files placed must support i/o after unlink and
17803+ * maintain i_size and i_blocks.
17804+ */
17805+static inline int au_test_fs_bad_xino(struct super_block *sb)
17806+{
17807+ return au_test_fs_remote(sb)
17808+ || au_test_fs_bad_iattr_size(sb)
1facf9fc 17809+ /* don't want unnecessary work for xino */
17810+ || au_test_aufs(sb)
1308ab2a 17811+ || au_test_ecryptfs(sb)
17812+ || au_test_nilfs(sb);
1facf9fc 17813+}
17814+
17815+static inline int au_test_fs_trunc_xino(struct super_block *sb)
17816+{
17817+ return au_test_tmpfs(sb)
17818+ || au_test_ramfs(sb);
17819+}
17820+
17821+/*
17822+ * test if the @sb is real-readonly.
17823+ */
17824+static inline int au_test_fs_rr(struct super_block *sb)
17825+{
17826+ return au_test_squashfs(sb)
17827+ || au_test_iso9660(sb)
17828+ || au_test_cramfs(sb)
17829+ || au_test_romfs(sb);
17830+}
17831+
b912730e
AM
17832+/*
17833+ * test if the @inode is nfs with 'noacl' option
2121bcd9 17834+ * NFS always sets SB_POSIXACL regardless its mount option 'noacl.'
b912730e
AM
17835+ */
17836+static inline int au_test_nfs_noacl(struct inode *inode)
17837+{
17838+ return au_test_nfs(inode->i_sb)
17839+ /* && IS_POSIXACL(inode) */
17840+ && !nfs_server_capable(inode, NFS_CAP_ACLS);
17841+}
17842+
1facf9fc 17843+#endif /* __KERNEL__ */
17844+#endif /* __AUFS_FSTYPE_H__ */
8b6a4947 17845diff -urN /usr/share/empty/fs/aufs/hbl.h linux/fs/aufs/hbl.h
eca34b5c 17846--- /usr/share/empty/fs/aufs/hbl.h 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 17847+++ linux/fs/aufs/hbl.h 2022-11-05 23:02:18.965889284 +0100
062440b3
AM
17848@@ -0,0 +1,65 @@
17849+/* SPDX-License-Identifier: GPL-2.0 */
8b6a4947 17850+/*
0dcfbb52 17851+ * Copyright (C) 2017-2022 Junjiro R. Okajima
8b6a4947 17852+ *
0dcfbb52 17853+ * This program is free software; you can redistribute it and/or modify
8b6a4947
AM
17854+ * it under the terms of the GNU General Public License as published by
17855+ * the Free Software Foundation; either version 2 of the License, or
17856+ * (at your option) any later version.
17857+ *
17858+ * This program is distributed in the hope that it will be useful,
17859+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17860+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17861+ * GNU General Public License for more details.
17862+ *
17863+ * You should have received a copy of the GNU General Public License
17864+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
17865+ */
17866+
17867+/*
17868+ * helpers for hlist_bl.h
17869+ */
17870+
17871+#ifndef __AUFS_HBL_H__
17872+#define __AUFS_HBL_H__
17873+
17874+#ifdef __KERNEL__
17875+
17876+#include <linux/list_bl.h>
17877+
17878+static inline void au_hbl_add(struct hlist_bl_node *node,
17879+ struct hlist_bl_head *hbl)
17880+{
17881+ hlist_bl_lock(hbl);
17882+ hlist_bl_add_head(node, hbl);
17883+ hlist_bl_unlock(hbl);
17884+}
17885+
17886+static inline void au_hbl_del(struct hlist_bl_node *node,
17887+ struct hlist_bl_head *hbl)
17888+{
17889+ hlist_bl_lock(hbl);
17890+ hlist_bl_del(node);
17891+ hlist_bl_unlock(hbl);
17892+}
17893+
17894+#define au_hbl_for_each(pos, head) \
17895+ for (pos = hlist_bl_first(head); \
17896+ pos; \
17897+ pos = pos->next)
17898+
17899+static inline unsigned long au_hbl_count(struct hlist_bl_head *hbl)
17900+{
17901+ unsigned long cnt;
17902+ struct hlist_bl_node *pos;
17903+
17904+ cnt = 0;
17905+ hlist_bl_lock(hbl);
17906+ au_hbl_for_each(pos, hbl)
17907+ cnt++;
17908+ hlist_bl_unlock(hbl);
17909+ return cnt;
17910+}
17911+
17912+#endif /* __KERNEL__ */
17913+#endif /* __AUFS_HBL_H__ */
7f207e10 17914diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
eca34b5c 17915--- /usr/share/empty/fs/aufs/hfsnotify.c 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 17916+++ linux/fs/aufs/hfsnotify.c 2022-11-05 23:02:18.965889284 +0100
22319442 17917@@ -0,0 +1,290 @@
cd7a4cd9 17918+// SPDX-License-Identifier: GPL-2.0
1facf9fc 17919+/*
0dcfbb52 17920+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 17921+ *
0dcfbb52 17922+ * This program is free software; you can redistribute it and/or modify
1facf9fc 17923+ * it under the terms of the GNU General Public License as published by
17924+ * the Free Software Foundation; either version 2 of the License, or
17925+ * (at your option) any later version.
dece6358
AM
17926+ *
17927+ * This program is distributed in the hope that it will be useful,
17928+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17929+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17930+ * GNU General Public License for more details.
17931+ *
17932+ * You should have received a copy of the GNU General Public License
523b37e3 17933+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 17934+ */
17935+
17936+/*
4a4d8108 17937+ * fsnotify for the lower directories
1facf9fc 17938+ */
17939+
17940+#include "aufs.h"
17941+
4a4d8108
AM
17942+/* FS_IN_IGNORED is unnecessary */
17943+static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
17944+ | FS_CREATE | FS_EVENT_ON_CHILD);
7f207e10 17945+static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
7eafdf33 17946+static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
1facf9fc 17947+
0c5527e5 17948+static void au_hfsn_free_mark(struct fsnotify_mark *mark)
1facf9fc 17949+{
0c5527e5
AM
17950+ struct au_hnotify *hn = container_of(mark, struct au_hnotify,
17951+ hn_mark);
5afbbe0d 17952+ /* AuDbg("here\n"); */
1c60b727 17953+ au_cache_free_hnotify(hn);
8b6a4947 17954+ smp_mb__before_atomic(); /* for atomic64_dec */
1716fcea
AM
17955+ if (atomic64_dec_and_test(&au_hfsn_ifree))
17956+ wake_up(&au_hfsn_wq);
4a4d8108 17957+}
1facf9fc 17958+
027c5e7a 17959+static int au_hfsn_alloc(struct au_hinode *hinode)
4a4d8108 17960+{
1716fcea 17961+ int err;
027c5e7a
AM
17962+ struct au_hnotify *hn;
17963+ struct super_block *sb;
17964+ struct au_branch *br;
0c5527e5 17965+ struct fsnotify_mark *mark;
027c5e7a 17966+ aufs_bindex_t bindex;
1facf9fc 17967+
027c5e7a
AM
17968+ hn = hinode->hi_notify;
17969+ sb = hn->hn_aufs_inode->i_sb;
17970+ bindex = au_br_index(sb, hinode->hi_id);
17971+ br = au_sbr(sb, bindex);
1716fcea
AM
17972+ AuDebugOn(!br->br_hfsn);
17973+
0c5527e5 17974+ mark = &hn->hn_mark;
ffa93bbd 17975+ fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
0c5527e5 17976+ mark->mask = AuHfsnMask;
7f207e10
AM
17977+ /*
17978+ * by udba rename or rmdir, aufs assign a new inode to the known
17979+ * h_inode, so specify 1 to allow dups.
17980+ */
c1595e42 17981+ lockdep_off();
acd2b654 17982+ err = fsnotify_add_inode_mark(mark, hinode->hi_inode, /*allow_dups*/1);
c1595e42 17983+ lockdep_on();
1716fcea
AM
17984+
17985+ return err;
1facf9fc 17986+}
17987+
7eafdf33 17988+static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
1facf9fc 17989+{
0c5527e5 17990+ struct fsnotify_mark *mark;
7eafdf33 17991+ unsigned long long ull;
1716fcea 17992+ struct fsnotify_group *group;
7eafdf33
AM
17993+
17994+ ull = atomic64_inc_return(&au_hfsn_ifree);
17995+ BUG_ON(!ull);
953406b4 17996+
0c5527e5 17997+ mark = &hn->hn_mark;
1716fcea
AM
17998+ spin_lock(&mark->lock);
17999+ group = mark->group;
18000+ fsnotify_get_group(group);
18001+ spin_unlock(&mark->lock);
c1595e42 18002+ lockdep_off();
1716fcea 18003+ fsnotify_destroy_mark(mark, group);
5afbbe0d 18004+ fsnotify_put_mark(mark);
1716fcea 18005+ fsnotify_put_group(group);
c1595e42 18006+ lockdep_on();
7f207e10 18007+
7eafdf33
AM
18008+ /* free hn by myself */
18009+ return 0;
1facf9fc 18010+}
18011+
18012+/* ---------------------------------------------------------------------- */
18013+
4a4d8108 18014+static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
1facf9fc 18015+{
0c5527e5 18016+ struct fsnotify_mark *mark;
1facf9fc 18017+
0c5527e5
AM
18018+ mark = &hinode->hi_notify->hn_mark;
18019+ spin_lock(&mark->lock);
1facf9fc 18020+ if (do_set) {
0c5527e5
AM
18021+ AuDebugOn(mark->mask & AuHfsnMask);
18022+ mark->mask |= AuHfsnMask;
1facf9fc 18023+ } else {
0c5527e5
AM
18024+ AuDebugOn(!(mark->mask & AuHfsnMask));
18025+ mark->mask &= ~AuHfsnMask;
1facf9fc 18026+ }
0c5527e5 18027+ spin_unlock(&mark->lock);
4a4d8108 18028+ /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
1facf9fc 18029+}
18030+
4a4d8108 18031+/* ---------------------------------------------------------------------- */
1facf9fc 18032+
4a4d8108
AM
18033+/* #define AuDbgHnotify */
18034+#ifdef AuDbgHnotify
18035+static char *au_hfsn_name(u32 mask)
18036+{
18037+#ifdef CONFIG_AUFS_DEBUG
c06a8ce3
AM
18038+#define test_ret(flag) \
18039+ do { \
18040+ if (mask & flag) \
18041+ return #flag; \
18042+ } while (0)
4a4d8108
AM
18043+ test_ret(FS_ACCESS);
18044+ test_ret(FS_MODIFY);
18045+ test_ret(FS_ATTRIB);
18046+ test_ret(FS_CLOSE_WRITE);
18047+ test_ret(FS_CLOSE_NOWRITE);
18048+ test_ret(FS_OPEN);
18049+ test_ret(FS_MOVED_FROM);
18050+ test_ret(FS_MOVED_TO);
18051+ test_ret(FS_CREATE);
18052+ test_ret(FS_DELETE);
18053+ test_ret(FS_DELETE_SELF);
18054+ test_ret(FS_MOVE_SELF);
18055+ test_ret(FS_UNMOUNT);
18056+ test_ret(FS_Q_OVERFLOW);
18057+ test_ret(FS_IN_IGNORED);
b912730e 18058+ test_ret(FS_ISDIR);
4a4d8108
AM
18059+ test_ret(FS_IN_ONESHOT);
18060+ test_ret(FS_EVENT_ON_CHILD);
18061+ return "";
18062+#undef test_ret
18063+#else
18064+ return "??";
18065+#endif
1facf9fc 18066+}
4a4d8108 18067+#endif
1facf9fc 18068+
18069+/* ---------------------------------------------------------------------- */
18070+
1716fcea
AM
18071+static void au_hfsn_free_group(struct fsnotify_group *group)
18072+{
18073+ struct au_br_hfsnotify *hfsn = group->private;
18074+
5afbbe0d 18075+ /* AuDbg("here\n"); */
9f237c51 18076+ au_kfree_try_rcu(hfsn);
1716fcea
AM
18077+}
18078+
4a4d8108 18079+static int au_hfsn_handle_event(struct fsnotify_group *group,
a2654f78 18080+ u32 mask, const void *data, int data_type,
e37dd06a 18081+ struct inode *dir,
83b672a5 18082+ const struct qstr *file_name, u32 cookie,
ffa93bbd 18083+ struct fsnotify_iter_info *iter_info)
1facf9fc 18084+{
18085+ int err;
4a4d8108
AM
18086+ struct au_hnotify *hnotify;
18087+ struct inode *h_dir, *h_inode;
cd7a4cd9 18088+ struct fsnotify_mark *inode_mark;
4a4d8108 18089+
cd233405
AM
18090+ AuDebugOn(!(data_type == FSNOTIFY_EVENT_INODE
18091+ || data_type == FSNOTIFY_EVENT_DENTRY));
1facf9fc 18092+
18093+ err = 0;
0c5527e5 18094+ /* if FS_UNMOUNT happens, there must be another bug */
4a4d8108 18095+ AuDebugOn(mask & FS_UNMOUNT);
0c5527e5 18096+ if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
1facf9fc 18097+ goto out;
1facf9fc 18098+
e37dd06a 18099+ h_dir = dir;
fb47a38f 18100+ h_inode = NULL;
4a4d8108 18101+#ifdef AuDbgHnotify
392086de 18102+ au_debug_on();
0dcfbb52
AM
18103+ if (1 || file_name.len != sizeof(AUFS_XINO_FNAME) - 1
18104+ || strncmp(file_name.name, AUFS_XINO_FNAME, file_name.len)) {
4a4d8108
AM
18105+ AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
18106+ h_dir->i_ino, mask, au_hfsn_name(mask),
0dcfbb52 18107+ AuLNPair(file_name), h_inode ? h_inode->i_ino : 0);
4a4d8108 18108+ /* WARN_ON(1); */
1facf9fc 18109+ }
392086de 18110+ au_debug_off();
1facf9fc 18111+#endif
4a4d8108 18112+
cd7a4cd9 18113+ inode_mark = fsnotify_iter_inode_mark(iter_info);
0c5527e5
AM
18114+ AuDebugOn(!inode_mark);
18115+ hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
b62f1e65 18116+ err = au_hnotify(h_dir, hnotify, mask, file_name, h_inode);
1facf9fc 18117+
4a4d8108
AM
18118+out:
18119+ return err;
18120+}
1facf9fc 18121+
4a4d8108 18122+static struct fsnotify_ops au_hfsn_ops = {
1716fcea 18123+ .handle_event = au_hfsn_handle_event,
ffa93bbd
AM
18124+ .free_group_priv = au_hfsn_free_group,
18125+ .free_mark = au_hfsn_free_mark
4a4d8108
AM
18126+};
18127+
18128+/* ---------------------------------------------------------------------- */
18129+
027c5e7a
AM
18130+static void au_hfsn_fin_br(struct au_branch *br)
18131+{
1716fcea 18132+ struct au_br_hfsnotify *hfsn;
027c5e7a 18133+
1716fcea 18134+ hfsn = br->br_hfsn;
c1595e42
JR
18135+ if (hfsn) {
18136+ lockdep_off();
1716fcea 18137+ fsnotify_put_group(hfsn->hfsn_group);
c1595e42
JR
18138+ lockdep_on();
18139+ }
027c5e7a
AM
18140+}
18141+
1716fcea 18142+static int au_hfsn_init_br(struct au_branch *br, int perm)
4a4d8108
AM
18143+{
18144+ int err;
1716fcea
AM
18145+ struct fsnotify_group *group;
18146+ struct au_br_hfsnotify *hfsn;
1facf9fc 18147+
4a4d8108 18148+ err = 0;
1716fcea
AM
18149+ br->br_hfsn = NULL;
18150+ if (!au_br_hnotifyable(perm))
027c5e7a 18151+ goto out;
027c5e7a 18152+
1716fcea
AM
18153+ err = -ENOMEM;
18154+ hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
18155+ if (unlikely(!hfsn))
027c5e7a
AM
18156+ goto out;
18157+
1716fcea 18158+ err = 0;
22319442
JP
18159+ group = fsnotify_alloc_group(&au_hfsn_ops,
18160+ /*flags - not for userspace*/0);
1716fcea
AM
18161+ if (IS_ERR(group)) {
18162+ err = PTR_ERR(group);
0c5527e5 18163+ pr_err("fsnotify_alloc_group() failed, %d\n", err);
1716fcea 18164+ goto out_hfsn;
4a4d8108 18165+ }
1facf9fc 18166+
1716fcea
AM
18167+ group->private = hfsn;
18168+ hfsn->hfsn_group = group;
18169+ br->br_hfsn = hfsn;
18170+ goto out; /* success */
18171+
18172+out_hfsn:
9f237c51 18173+ au_kfree_try_rcu(hfsn);
027c5e7a 18174+out:
1716fcea
AM
18175+ return err;
18176+}
18177+
18178+static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
18179+{
18180+ int err;
18181+
18182+ err = 0;
18183+ if (!br->br_hfsn)
18184+ err = au_hfsn_init_br(br, perm);
18185+
1facf9fc 18186+ return err;
18187+}
18188+
7eafdf33
AM
18189+/* ---------------------------------------------------------------------- */
18190+
18191+static void au_hfsn_fin(void)
18192+{
18193+ AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
18194+ wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
18195+}
18196+
4a4d8108
AM
18197+const struct au_hnotify_op au_hnotify_op = {
18198+ .ctl = au_hfsn_ctl,
18199+ .alloc = au_hfsn_alloc,
18200+ .free = au_hfsn_free,
1facf9fc 18201+
7eafdf33
AM
18202+ .fin = au_hfsn_fin,
18203+
027c5e7a
AM
18204+ .reset_br = au_hfsn_reset_br,
18205+ .fin_br = au_hfsn_fin_br,
18206+ .init_br = au_hfsn_init_br
4a4d8108 18207+};
7f207e10 18208diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
eca34b5c 18209--- /usr/share/empty/fs/aufs/hfsplus.c 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 18210+++ linux/fs/aufs/hfsplus.c 2022-11-05 23:02:18.965889284 +0100
acd2b654 18211@@ -0,0 +1,60 @@
cd7a4cd9 18212+// SPDX-License-Identifier: GPL-2.0
4a4d8108 18213+/*
0dcfbb52 18214+ * Copyright (C) 2010-2022 Junjiro R. Okajima
4a4d8108 18215+ *
0dcfbb52 18216+ * This program is free software; you can redistribute it and/or modify
4a4d8108
AM
18217+ * it under the terms of the GNU General Public License as published by
18218+ * the Free Software Foundation; either version 2 of the License, or
18219+ * (at your option) any later version.
18220+ *
18221+ * This program is distributed in the hope that it will be useful,
18222+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18223+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18224+ * GNU General Public License for more details.
18225+ *
18226+ * You should have received a copy of the GNU General Public License
523b37e3 18227+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 18228+ */
1facf9fc 18229+
4a4d8108 18230+/*
acd2b654 18231+ * special support for filesystems which acquires an inode mutex
4a4d8108
AM
18232+ * at final closing a file, eg, hfsplus.
18233+ *
18234+ * This trick is very simple and stupid, just to open the file before really
acd2b654 18235+ * necessary open to tell hfsplus that this is not the final closing.
4a4d8108
AM
18236+ * The caller should call au_h_open_pre() after acquiring the inode mutex,
18237+ * and au_h_open_post() after releasing it.
18238+ */
1facf9fc 18239+
4a4d8108 18240+#include "aufs.h"
1facf9fc 18241+
392086de
AM
18242+struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
18243+ int force_wr)
4a4d8108
AM
18244+{
18245+ struct file *h_file;
18246+ struct dentry *h_dentry;
1facf9fc 18247+
4a4d8108
AM
18248+ h_dentry = au_h_dptr(dentry, bindex);
18249+ AuDebugOn(!h_dentry);
5527c038 18250+ AuDebugOn(d_is_negative(h_dentry));
4a4d8108
AM
18251+
18252+ h_file = NULL;
18253+ if (au_test_hfsplus(h_dentry->d_sb)
7e9cd9fe 18254+ && d_is_reg(h_dentry))
4a4d8108
AM
18255+ h_file = au_h_open(dentry, bindex,
18256+ O_RDONLY | O_NOATIME | O_LARGEFILE,
392086de 18257+ /*file*/NULL, force_wr);
4a4d8108 18258+ return h_file;
1facf9fc 18259+}
18260+
4a4d8108
AM
18261+void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
18262+ struct file *h_file)
18263+{
acd2b654
AM
18264+ struct au_branch *br;
18265+
4a4d8108
AM
18266+ if (h_file) {
18267+ fput(h_file);
acd2b654
AM
18268+ br = au_sbr(dentry->d_sb, bindex);
18269+ au_lcnt_dec(&br->br_nfiles);
4a4d8108
AM
18270+ }
18271+}
7f207e10 18272diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
eca34b5c 18273--- /usr/share/empty/fs/aufs/hnotify.c 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 18274+++ linux/fs/aufs/hnotify.c 2022-11-05 23:02:18.965889284 +0100
43982f53 18275@@ -0,0 +1,715 @@
cd7a4cd9 18276+// SPDX-License-Identifier: GPL-2.0
e49829fe 18277+/*
0dcfbb52 18278+ * Copyright (C) 2005-2022 Junjiro R. Okajima
e49829fe 18279+ *
0dcfbb52 18280+ * This program is free software; you can redistribute it and/or modify
e49829fe
JR
18281+ * it under the terms of the GNU General Public License as published by
18282+ * the Free Software Foundation; either version 2 of the License, or
18283+ * (at your option) any later version.
18284+ *
18285+ * This program is distributed in the hope that it will be useful,
18286+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18287+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18288+ * GNU General Public License for more details.
18289+ *
18290+ * You should have received a copy of the GNU General Public License
523b37e3 18291+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
e49829fe
JR
18292+ */
18293+
18294+/*
7f207e10 18295+ * abstraction to notify the direct changes on lower directories
e49829fe
JR
18296+ */
18297+
eca801bf 18298+/* #include <linux/iversion.h> */
e49829fe
JR
18299+#include "aufs.h"
18300+
027c5e7a 18301+int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
e49829fe
JR
18302+{
18303+ int err;
7f207e10 18304+ struct au_hnotify *hn;
1facf9fc 18305+
4a4d8108
AM
18306+ err = -ENOMEM;
18307+ hn = au_cache_alloc_hnotify();
18308+ if (hn) {
18309+ hn->hn_aufs_inode = inode;
027c5e7a
AM
18310+ hinode->hi_notify = hn;
18311+ err = au_hnotify_op.alloc(hinode);
18312+ AuTraceErr(err);
18313+ if (unlikely(err)) {
18314+ hinode->hi_notify = NULL;
1c60b727 18315+ au_cache_free_hnotify(hn);
4a4d8108
AM
18316+ /*
18317+ * The upper dir was removed by udba, but the same named
acd2b654 18318+ * dir left. In this case, aufs assigns a new inode
4a4d8108 18319+ * number and set the monitor again.
acd2b654 18320+ * For the lower dir, the old monitor is still left.
4a4d8108
AM
18321+ */
18322+ if (err == -EEXIST)
18323+ err = 0;
18324+ }
1308ab2a 18325+ }
1308ab2a 18326+
027c5e7a 18327+ AuTraceErr(err);
1308ab2a 18328+ return err;
dece6358 18329+}
1facf9fc 18330+
4a4d8108 18331+void au_hn_free(struct au_hinode *hinode)
dece6358 18332+{
4a4d8108 18333+ struct au_hnotify *hn;
1facf9fc 18334+
4a4d8108
AM
18335+ hn = hinode->hi_notify;
18336+ if (hn) {
4a4d8108 18337+ hinode->hi_notify = NULL;
7eafdf33 18338+ if (au_hnotify_op.free(hinode, hn))
1c60b727 18339+ au_cache_free_hnotify(hn);
4a4d8108
AM
18340+ }
18341+}
dece6358 18342+
4a4d8108 18343+/* ---------------------------------------------------------------------- */
dece6358 18344+
4a4d8108
AM
18345+void au_hn_ctl(struct au_hinode *hinode, int do_set)
18346+{
18347+ if (hinode->hi_notify)
18348+ au_hnotify_op.ctl(hinode, do_set);
18349+}
18350+
18351+void au_hn_reset(struct inode *inode, unsigned int flags)
18352+{
5afbbe0d 18353+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
18354+ struct inode *hi;
18355+ struct dentry *iwhdentry;
1facf9fc 18356+
5afbbe0d
AM
18357+ bbot = au_ibbot(inode);
18358+ for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
4a4d8108
AM
18359+ hi = au_h_iptr(inode, bindex);
18360+ if (!hi)
18361+ continue;
1308ab2a 18362+
febd17d6 18363+ /* inode_lock_nested(hi, AuLsc_I_CHILD); */
4a4d8108
AM
18364+ iwhdentry = au_hi_wh(inode, bindex);
18365+ if (iwhdentry)
18366+ dget(iwhdentry);
18367+ au_igrab(hi);
18368+ au_set_h_iptr(inode, bindex, NULL, 0);
18369+ au_set_h_iptr(inode, bindex, au_igrab(hi),
18370+ flags & ~AuHi_XINO);
18371+ iput(hi);
18372+ dput(iwhdentry);
febd17d6 18373+ /* inode_unlock(hi); */
1facf9fc 18374+ }
1facf9fc 18375+}
18376+
1308ab2a 18377+/* ---------------------------------------------------------------------- */
1facf9fc 18378+
4a4d8108 18379+static int hn_xino(struct inode *inode, struct inode *h_inode)
1facf9fc 18380+{
4a4d8108 18381+ int err;
5afbbe0d 18382+ aufs_bindex_t bindex, bbot, bfound, btop;
4a4d8108 18383+ struct inode *h_i;
1facf9fc 18384+
4a4d8108
AM
18385+ err = 0;
18386+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 18387+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
18388+ goto out;
18389+ }
1facf9fc 18390+
4a4d8108 18391+ bfound = -1;
5afbbe0d
AM
18392+ bbot = au_ibbot(inode);
18393+ btop = au_ibtop(inode);
4a4d8108 18394+#if 0 /* reserved for future use */
5afbbe0d 18395+ if (bindex == bbot) {
4a4d8108
AM
18396+ /* keep this ino in rename case */
18397+ goto out;
18398+ }
18399+#endif
5afbbe0d 18400+ for (bindex = btop; bindex <= bbot; bindex++)
4a4d8108
AM
18401+ if (au_h_iptr(inode, bindex) == h_inode) {
18402+ bfound = bindex;
18403+ break;
18404+ }
18405+ if (bfound < 0)
1308ab2a 18406+ goto out;
1facf9fc 18407+
5afbbe0d 18408+ for (bindex = btop; bindex <= bbot; bindex++) {
4a4d8108
AM
18409+ h_i = au_h_iptr(inode, bindex);
18410+ if (!h_i)
18411+ continue;
1facf9fc 18412+
4a4d8108
AM
18413+ err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
18414+ /* ignore this error */
18415+ /* bad action? */
1facf9fc 18416+ }
1facf9fc 18417+
4a4d8108 18418+ /* children inode number will be broken */
1facf9fc 18419+
4f0767ce 18420+out:
4a4d8108
AM
18421+ AuTraceErr(err);
18422+ return err;
1facf9fc 18423+}
18424+
4a4d8108 18425+static int hn_gen_tree(struct dentry *dentry)
1facf9fc 18426+{
4a4d8108
AM
18427+ int err, i, j, ndentry;
18428+ struct au_dcsub_pages dpages;
18429+ struct au_dpage *dpage;
18430+ struct dentry **dentries;
1facf9fc 18431+
4a4d8108
AM
18432+ err = au_dpages_init(&dpages, GFP_NOFS);
18433+ if (unlikely(err))
18434+ goto out;
18435+ err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
18436+ if (unlikely(err))
18437+ goto out_dpages;
1facf9fc 18438+
4a4d8108
AM
18439+ for (i = 0; i < dpages.ndpage; i++) {
18440+ dpage = dpages.dpages + i;
18441+ dentries = dpage->dentries;
18442+ ndentry = dpage->ndentry;
18443+ for (j = 0; j < ndentry; j++) {
18444+ struct dentry *d;
18445+
18446+ d = dentries[j];
18447+ if (IS_ROOT(d))
18448+ continue;
18449+
4a4d8108 18450+ au_digen_dec(d);
5527c038 18451+ if (d_really_is_positive(d))
4a4d8108
AM
18452+ /* todo: reset children xino?
18453+ cached children only? */
5527c038 18454+ au_iigen_dec(d_inode(d));
1308ab2a 18455+ }
dece6358 18456+ }
1facf9fc 18457+
4f0767ce 18458+out_dpages:
4a4d8108 18459+ au_dpages_free(&dpages);
4f0767ce 18460+out:
dece6358
AM
18461+ return err;
18462+}
18463+
1308ab2a 18464+/*
4a4d8108 18465+ * return 0 if processed.
1308ab2a 18466+ */
4a4d8108
AM
18467+static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
18468+ const unsigned int isdir)
dece6358 18469+{
1308ab2a 18470+ int err;
4a4d8108
AM
18471+ struct dentry *d;
18472+ struct qstr *dname;
1facf9fc 18473+
4a4d8108
AM
18474+ err = 1;
18475+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 18476+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
18477+ err = 0;
18478+ goto out;
18479+ }
dece6358 18480+
4a4d8108
AM
18481+ if (!isdir) {
18482+ AuDebugOn(!name);
18483+ au_iigen_dec(inode);
027c5e7a 18484+ spin_lock(&inode->i_lock);
c1595e42 18485+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
027c5e7a 18486+ spin_lock(&d->d_lock);
4a4d8108
AM
18487+ dname = &d->d_name;
18488+ if (dname->len != nlen
027c5e7a
AM
18489+ && memcmp(dname->name, name, nlen)) {
18490+ spin_unlock(&d->d_lock);
4a4d8108 18491+ continue;
027c5e7a 18492+ }
4a4d8108 18493+ err = 0;
4a4d8108
AM
18494+ au_digen_dec(d);
18495+ spin_unlock(&d->d_lock);
18496+ break;
1facf9fc 18497+ }
027c5e7a 18498+ spin_unlock(&inode->i_lock);
1308ab2a 18499+ } else {
027c5e7a 18500+ au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
c1595e42 18501+ d = d_find_any_alias(inode);
4a4d8108
AM
18502+ if (!d) {
18503+ au_iigen_dec(inode);
18504+ goto out;
18505+ }
1facf9fc 18506+
027c5e7a 18507+ spin_lock(&d->d_lock);
4a4d8108 18508+ dname = &d->d_name;
027c5e7a
AM
18509+ if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
18510+ spin_unlock(&d->d_lock);
4a4d8108 18511+ err = hn_gen_tree(d);
027c5e7a
AM
18512+ spin_lock(&d->d_lock);
18513+ }
18514+ spin_unlock(&d->d_lock);
4a4d8108
AM
18515+ dput(d);
18516+ }
1facf9fc 18517+
4f0767ce 18518+out:
4a4d8108 18519+ AuTraceErr(err);
1308ab2a 18520+ return err;
18521+}
dece6358 18522+
4a4d8108 18523+static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
1facf9fc 18524+{
4a4d8108 18525+ int err;
1facf9fc 18526+
5527c038 18527+ if (IS_ROOT(dentry)) {
0c3ec466 18528+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
18529+ return 0;
18530+ }
1308ab2a 18531+
4a4d8108
AM
18532+ err = 0;
18533+ if (!isdir) {
4a4d8108 18534+ au_digen_dec(dentry);
5527c038
JR
18535+ if (d_really_is_positive(dentry))
18536+ au_iigen_dec(d_inode(dentry));
4a4d8108 18537+ } else {
027c5e7a 18538+ au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
5527c038 18539+ if (d_really_is_positive(dentry))
4a4d8108
AM
18540+ err = hn_gen_tree(dentry);
18541+ }
18542+
18543+ AuTraceErr(err);
18544+ return err;
1facf9fc 18545+}
18546+
4a4d8108 18547+/* ---------------------------------------------------------------------- */
1facf9fc 18548+
4a4d8108
AM
18549+/* hnotify job flags */
18550+#define AuHnJob_XINO0 1
18551+#define AuHnJob_GEN (1 << 1)
18552+#define AuHnJob_DIRENT (1 << 2)
18553+#define AuHnJob_ISDIR (1 << 3)
18554+#define AuHnJob_TRYXINO0 (1 << 4)
18555+#define AuHnJob_MNTPNT (1 << 5)
18556+#define au_ftest_hnjob(flags, name) ((flags) & AuHnJob_##name)
7f207e10
AM
18557+#define au_fset_hnjob(flags, name) \
18558+ do { (flags) |= AuHnJob_##name; } while (0)
18559+#define au_fclr_hnjob(flags, name) \
18560+ do { (flags) &= ~AuHnJob_##name; } while (0)
1facf9fc 18561+
4a4d8108
AM
18562+enum {
18563+ AuHn_CHILD,
18564+ AuHn_PARENT,
18565+ AuHnLast
18566+};
1facf9fc 18567+
4a4d8108
AM
18568+struct au_hnotify_args {
18569+ struct inode *h_dir, *dir, *h_child_inode;
18570+ u32 mask;
18571+ unsigned int flags[AuHnLast];
18572+ unsigned int h_child_nlen;
18573+ char h_child_name[];
18574+};
1facf9fc 18575+
4a4d8108
AM
18576+struct hn_job_args {
18577+ unsigned int flags;
18578+ struct inode *inode, *h_inode, *dir, *h_dir;
18579+ struct dentry *dentry;
18580+ char *h_name;
18581+ int h_nlen;
18582+};
1308ab2a 18583+
4a4d8108
AM
18584+static int hn_job(struct hn_job_args *a)
18585+{
18586+ const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
076b876e 18587+ int e;
1308ab2a 18588+
4a4d8108
AM
18589+ /* reset xino */
18590+ if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
18591+ hn_xino(a->inode, a->h_inode); /* ignore this error */
1308ab2a 18592+
4a4d8108
AM
18593+ if (au_ftest_hnjob(a->flags, TRYXINO0)
18594+ && a->inode
18595+ && a->h_inode) {
be118d29 18596+ inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
38d290e6
JR
18597+ if (!a->h_inode->i_nlink
18598+ && !(a->h_inode->i_state & I_LINKABLE))
4a4d8108 18599+ hn_xino(a->inode, a->h_inode); /* ignore this error */
3c1bdaff 18600+ inode_unlock_shared(a->h_inode);
1308ab2a 18601+ }
1facf9fc 18602+
4a4d8108
AM
18603+ /* make the generation obsolete */
18604+ if (au_ftest_hnjob(a->flags, GEN)) {
076b876e 18605+ e = -1;
4a4d8108 18606+ if (a->inode)
076b876e 18607+ e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
4a4d8108 18608+ isdir);
076b876e 18609+ if (e && a->dentry)
4a4d8108
AM
18610+ hn_gen_by_name(a->dentry, isdir);
18611+ /* ignore this error */
1facf9fc 18612+ }
1facf9fc 18613+
4a4d8108
AM
18614+ /* make dir entries obsolete */
18615+ if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
18616+ struct au_vdir *vdir;
1facf9fc 18617+
4a4d8108
AM
18618+ vdir = au_ivdir(a->inode);
18619+ if (vdir)
18620+ vdir->vd_jiffy = 0;
18621+ /* IMustLock(a->inode); */
be118d29 18622+ /* inode_inc_iversion(a->inode); */
4a4d8108 18623+ }
1facf9fc 18624+
4a4d8108
AM
18625+ /* can do nothing but warn */
18626+ if (au_ftest_hnjob(a->flags, MNTPNT)
18627+ && a->dentry
18628+ && d_mountpoint(a->dentry))
523b37e3 18629+ pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
1facf9fc 18630+
4a4d8108 18631+ return 0;
1308ab2a 18632+}
1facf9fc 18633+
1308ab2a 18634+/* ---------------------------------------------------------------------- */
1facf9fc 18635+
4a4d8108
AM
18636+static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
18637+ struct inode *dir)
1308ab2a 18638+{
4a4d8108
AM
18639+ struct dentry *dentry, *d, *parent;
18640+ struct qstr *dname;
1308ab2a 18641+
c1595e42 18642+ parent = d_find_any_alias(dir);
4a4d8108
AM
18643+ if (!parent)
18644+ return NULL;
1308ab2a 18645+
4a4d8108 18646+ dentry = NULL;
027c5e7a 18647+ spin_lock(&parent->d_lock);
c1595e42 18648+ list_for_each_entry(d, &parent->d_subdirs, d_child) {
523b37e3 18649+ /* AuDbg("%pd\n", d); */
027c5e7a 18650+ spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
4a4d8108
AM
18651+ dname = &d->d_name;
18652+ if (dname->len != nlen || memcmp(dname->name, name, nlen))
027c5e7a
AM
18653+ goto cont_unlock;
18654+ if (au_di(d))
18655+ au_digen_dec(d);
18656+ else
18657+ goto cont_unlock;
c1595e42 18658+ if (au_dcount(d) > 0) {
027c5e7a 18659+ dentry = dget_dlock(d);
4a4d8108 18660+ spin_unlock(&d->d_lock);
027c5e7a 18661+ break;
dece6358 18662+ }
1facf9fc 18663+
f6b6e03d 18664+cont_unlock:
027c5e7a 18665+ spin_unlock(&d->d_lock);
1308ab2a 18666+ }
027c5e7a 18667+ spin_unlock(&parent->d_lock);
4a4d8108 18668+ dput(parent);
1facf9fc 18669+
4a4d8108
AM
18670+ if (dentry)
18671+ di_write_lock_child(dentry);
1308ab2a 18672+
4a4d8108
AM
18673+ return dentry;
18674+}
dece6358 18675+
4a4d8108
AM
18676+static struct inode *lookup_wlock_by_ino(struct super_block *sb,
18677+ aufs_bindex_t bindex, ino_t h_ino)
18678+{
18679+ struct inode *inode;
18680+ ino_t ino;
18681+ int err;
18682+
18683+ inode = NULL;
18684+ err = au_xino_read(sb, bindex, h_ino, &ino);
18685+ if (!err && ino)
18686+ inode = ilookup(sb, ino);
18687+ if (!inode)
18688+ goto out;
18689+
18690+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 18691+ pr_warn("wrong root branch\n");
4a4d8108
AM
18692+ iput(inode);
18693+ inode = NULL;
18694+ goto out;
1308ab2a 18695+ }
18696+
4a4d8108 18697+ ii_write_lock_child(inode);
1308ab2a 18698+
4f0767ce 18699+out:
4a4d8108 18700+ return inode;
dece6358
AM
18701+}
18702+
4a4d8108 18703+static void au_hn_bh(void *_args)
1facf9fc 18704+{
4a4d8108
AM
18705+ struct au_hnotify_args *a = _args;
18706+ struct super_block *sb;
5afbbe0d 18707+ aufs_bindex_t bindex, bbot, bfound;
4a4d8108 18708+ unsigned char xino, try_iput;
1facf9fc 18709+ int err;
1308ab2a 18710+ struct inode *inode;
4a4d8108
AM
18711+ ino_t h_ino;
18712+ struct hn_job_args args;
18713+ struct dentry *dentry;
18714+ struct au_sbinfo *sbinfo;
1facf9fc 18715+
4a4d8108
AM
18716+ AuDebugOn(!_args);
18717+ AuDebugOn(!a->h_dir);
18718+ AuDebugOn(!a->dir);
18719+ AuDebugOn(!a->mask);
18720+ AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
18721+ a->mask, a->dir->i_ino, a->h_dir->i_ino,
18722+ a->h_child_inode ? a->h_child_inode->i_ino : 0);
1facf9fc 18723+
4a4d8108
AM
18724+ inode = NULL;
18725+ dentry = NULL;
18726+ /*
18727+ * do not lock a->dir->i_mutex here
18728+ * because of d_revalidate() may cause a deadlock.
18729+ */
18730+ sb = a->dir->i_sb;
18731+ AuDebugOn(!sb);
18732+ sbinfo = au_sbi(sb);
18733+ AuDebugOn(!sbinfo);
7f207e10 18734+ si_write_lock(sb, AuLock_NOPLMW);
1facf9fc 18735+
8b6a4947
AM
18736+ if (au_opt_test(sbinfo->si_mntflags, DIRREN))
18737+ switch (a->mask & FS_EVENTS_POSS_ON_CHILD) {
18738+ case FS_MOVED_FROM:
18739+ case FS_MOVED_TO:
18740+ AuWarn1("DIRREN with UDBA may not work correctly "
18741+ "for the direct rename(2)\n");
18742+ }
18743+
4a4d8108
AM
18744+ ii_read_lock_parent(a->dir);
18745+ bfound = -1;
5afbbe0d
AM
18746+ bbot = au_ibbot(a->dir);
18747+ for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
4a4d8108
AM
18748+ if (au_h_iptr(a->dir, bindex) == a->h_dir) {
18749+ bfound = bindex;
18750+ break;
18751+ }
18752+ ii_read_unlock(a->dir);
18753+ if (unlikely(bfound < 0))
18754+ goto out;
1facf9fc 18755+
4a4d8108
AM
18756+ xino = !!au_opt_test(au_mntflags(sb), XINO);
18757+ h_ino = 0;
18758+ if (a->h_child_inode)
18759+ h_ino = a->h_child_inode->i_ino;
1facf9fc 18760+
4a4d8108
AM
18761+ if (a->h_child_nlen
18762+ && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
18763+ || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
18764+ dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
18765+ a->dir);
18766+ try_iput = 0;
5527c038
JR
18767+ if (dentry && d_really_is_positive(dentry))
18768+ inode = d_inode(dentry);
4a4d8108
AM
18769+ if (xino && !inode && h_ino
18770+ && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
18771+ || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
18772+ || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
18773+ inode = lookup_wlock_by_ino(sb, bfound, h_ino);
18774+ try_iput = 1;
f0c0a007 18775+ }
1facf9fc 18776+
4a4d8108
AM
18777+ args.flags = a->flags[AuHn_CHILD];
18778+ args.dentry = dentry;
18779+ args.inode = inode;
18780+ args.h_inode = a->h_child_inode;
18781+ args.dir = a->dir;
18782+ args.h_dir = a->h_dir;
18783+ args.h_name = a->h_child_name;
18784+ args.h_nlen = a->h_child_nlen;
18785+ err = hn_job(&args);
18786+ if (dentry) {
027c5e7a 18787+ if (au_di(dentry))
4a4d8108
AM
18788+ di_write_unlock(dentry);
18789+ dput(dentry);
18790+ }
18791+ if (inode && try_iput) {
18792+ ii_write_unlock(inode);
18793+ iput(inode);
18794+ }
1facf9fc 18795+
4a4d8108
AM
18796+ ii_write_lock_parent(a->dir);
18797+ args.flags = a->flags[AuHn_PARENT];
18798+ args.dentry = NULL;
18799+ args.inode = a->dir;
18800+ args.h_inode = a->h_dir;
18801+ args.dir = NULL;
18802+ args.h_dir = NULL;
18803+ args.h_name = NULL;
18804+ args.h_nlen = 0;
18805+ err = hn_job(&args);
18806+ ii_write_unlock(a->dir);
1facf9fc 18807+
4f0767ce 18808+out:
4a4d8108
AM
18809+ iput(a->h_child_inode);
18810+ iput(a->h_dir);
18811+ iput(a->dir);
027c5e7a
AM
18812+ si_write_unlock(sb);
18813+ au_nwt_done(&sbinfo->si_nowait);
9f237c51 18814+ au_kfree_rcu(a);
dece6358 18815+}
1facf9fc 18816+
4a4d8108
AM
18817+/* ---------------------------------------------------------------------- */
18818+
18819+int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
b62f1e65 18820+ const struct qstr *h_child_qstr, struct inode *h_child_inode)
dece6358 18821+{
4a4d8108 18822+ int err, len;
53392da6 18823+ unsigned int flags[AuHnLast], f;
4a4d8108
AM
18824+ unsigned char isdir, isroot, wh;
18825+ struct inode *dir;
18826+ struct au_hnotify_args *args;
18827+ char *p, *h_child_name;
dece6358 18828+
1308ab2a 18829+ err = 0;
4a4d8108
AM
18830+ AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
18831+ dir = igrab(hnotify->hn_aufs_inode);
18832+ if (!dir)
18833+ goto out;
1facf9fc 18834+
4a4d8108
AM
18835+ isroot = (dir->i_ino == AUFS_ROOT_INO);
18836+ wh = 0;
18837+ h_child_name = (void *)h_child_qstr->name;
18838+ len = h_child_qstr->len;
18839+ if (h_child_name) {
18840+ if (len > AUFS_WH_PFX_LEN
18841+ && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
18842+ h_child_name += AUFS_WH_PFX_LEN;
18843+ len -= AUFS_WH_PFX_LEN;
18844+ wh = 1;
18845+ }
1facf9fc 18846+ }
dece6358 18847+
4a4d8108
AM
18848+ isdir = 0;
18849+ if (h_child_inode)
18850+ isdir = !!S_ISDIR(h_child_inode->i_mode);
18851+ flags[AuHn_PARENT] = AuHnJob_ISDIR;
18852+ flags[AuHn_CHILD] = 0;
18853+ if (isdir)
18854+ flags[AuHn_CHILD] = AuHnJob_ISDIR;
18855+ au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
18856+ au_fset_hnjob(flags[AuHn_CHILD], GEN);
fbc438ed 18857+ switch (mask & ALL_FSNOTIFY_DIRENT_EVENTS) {
4a4d8108
AM
18858+ case FS_MOVED_FROM:
18859+ case FS_MOVED_TO:
18860+ au_fset_hnjob(flags[AuHn_CHILD], XINO0);
18861+ au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
42a736d3 18862+ fallthrough;
4a4d8108 18863+ case FS_CREATE:
fb47a38f 18864+ AuDebugOn(!h_child_name);
4a4d8108 18865+ break;
1facf9fc 18866+
4a4d8108
AM
18867+ case FS_DELETE:
18868+ /*
18869+ * aufs never be able to get this child inode.
18870+ * revalidation should be in d_revalidate()
18871+ * by checking i_nlink, i_generation or d_unhashed().
18872+ */
18873+ AuDebugOn(!h_child_name);
18874+ au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
18875+ au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
18876+ break;
dece6358 18877+
4a4d8108
AM
18878+ default:
18879+ AuDebugOn(1);
18880+ }
1308ab2a 18881+
4a4d8108
AM
18882+ if (wh)
18883+ h_child_inode = NULL;
1308ab2a 18884+
4a4d8108
AM
18885+ err = -ENOMEM;
18886+ /* iput() and kfree() will be called in au_hnotify() */
4a4d8108 18887+ args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
4a4d8108
AM
18888+ if (unlikely(!args)) {
18889+ AuErr1("no memory\n");
18890+ iput(dir);
18891+ goto out;
18892+ }
18893+ args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
18894+ args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
18895+ args->mask = mask;
18896+ args->dir = dir;
18897+ args->h_dir = igrab(h_dir);
18898+ if (h_child_inode)
18899+ h_child_inode = igrab(h_child_inode); /* can be NULL */
18900+ args->h_child_inode = h_child_inode;
18901+ args->h_child_nlen = len;
18902+ if (len) {
18903+ p = (void *)args;
18904+ p += sizeof(*args);
18905+ memcpy(p, h_child_name, len);
18906+ p[len] = 0;
1308ab2a 18907+ }
1308ab2a 18908+
38d290e6 18909+ /* NFS fires the event for silly-renamed one from kworker */
53392da6 18910+ f = 0;
38d290e6
JR
18911+ if (!dir->i_nlink
18912+ || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
53392da6
AM
18913+ f = AuWkq_NEST;
18914+ err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
4a4d8108
AM
18915+ if (unlikely(err)) {
18916+ pr_err("wkq %d\n", err);
18917+ iput(args->h_child_inode);
18918+ iput(args->h_dir);
18919+ iput(args->dir);
9f237c51 18920+ au_kfree_rcu(args);
1facf9fc 18921+ }
1facf9fc 18922+
4a4d8108 18923+out:
1facf9fc 18924+ return err;
18925+}
18926+
027c5e7a
AM
18927+/* ---------------------------------------------------------------------- */
18928+
18929+int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
18930+{
18931+ int err;
18932+
18933+ AuDebugOn(!(udba & AuOptMask_UDBA));
18934+
18935+ err = 0;
18936+ if (au_hnotify_op.reset_br)
18937+ err = au_hnotify_op.reset_br(udba, br, perm);
18938+
18939+ return err;
18940+}
18941+
18942+int au_hnotify_init_br(struct au_branch *br, int perm)
18943+{
18944+ int err;
18945+
18946+ err = 0;
18947+ if (au_hnotify_op.init_br)
18948+ err = au_hnotify_op.init_br(br, perm);
18949+
18950+ return err;
18951+}
18952+
18953+void au_hnotify_fin_br(struct au_branch *br)
18954+{
18955+ if (au_hnotify_op.fin_br)
18956+ au_hnotify_op.fin_br(br);
18957+}
18958+
4a4d8108
AM
18959+static void au_hn_destroy_cache(void)
18960+{
1c60b727
AM
18961+ kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
18962+ au_cache[AuCache_HNOTIFY] = NULL;
4a4d8108 18963+}
1308ab2a 18964+
4a4d8108 18965+int __init au_hnotify_init(void)
1facf9fc 18966+{
1308ab2a 18967+ int err;
1308ab2a 18968+
4a4d8108 18969+ err = -ENOMEM;
1c60b727
AM
18970+ au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
18971+ if (au_cache[AuCache_HNOTIFY]) {
027c5e7a
AM
18972+ err = 0;
18973+ if (au_hnotify_op.init)
18974+ err = au_hnotify_op.init();
4a4d8108
AM
18975+ if (unlikely(err))
18976+ au_hn_destroy_cache();
1308ab2a 18977+ }
1308ab2a 18978+ AuTraceErr(err);
4a4d8108 18979+ return err;
1308ab2a 18980+}
18981+
4a4d8108 18982+void au_hnotify_fin(void)
1308ab2a 18983+{
027c5e7a
AM
18984+ if (au_hnotify_op.fin)
18985+ au_hnotify_op.fin();
f0c0a007 18986+
4a4d8108 18987+ /* cf. au_cache_fin() */
1c60b727 18988+ if (au_cache[AuCache_HNOTIFY])
4a4d8108 18989+ au_hn_destroy_cache();
dece6358 18990+}
7f207e10 18991diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
eca34b5c 18992--- /usr/share/empty/fs/aufs/iinfo.c 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 18993+++ linux/fs/aufs/iinfo.c 2022-11-05 23:02:18.965889284 +0100
062440b3 18994@@ -0,0 +1,286 @@
cd7a4cd9 18995+// SPDX-License-Identifier: GPL-2.0
dece6358 18996+/*
0dcfbb52 18997+ * Copyright (C) 2005-2022 Junjiro R. Okajima
dece6358 18998+ *
0dcfbb52 18999+ * This program is free software; you can redistribute it and/or modify
dece6358
AM
19000+ * it under the terms of the GNU General Public License as published by
19001+ * the Free Software Foundation; either version 2 of the License, or
19002+ * (at your option) any later version.
19003+ *
19004+ * This program is distributed in the hope that it will be useful,
19005+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19006+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19007+ * GNU General Public License for more details.
19008+ *
19009+ * You should have received a copy of the GNU General Public License
523b37e3 19010+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358 19011+ */
1facf9fc 19012+
dece6358 19013+/*
4a4d8108 19014+ * inode private data
dece6358 19015+ */
1facf9fc 19016+
1308ab2a 19017+#include "aufs.h"
1facf9fc 19018+
4a4d8108 19019+struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 19020+{
4a4d8108 19021+ struct inode *h_inode;
5afbbe0d 19022+ struct au_hinode *hinode;
1facf9fc 19023+
4a4d8108 19024+ IiMustAnyLock(inode);
1facf9fc 19025+
5afbbe0d
AM
19026+ hinode = au_hinode(au_ii(inode), bindex);
19027+ h_inode = hinode->hi_inode;
4a4d8108
AM
19028+ AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
19029+ return h_inode;
19030+}
1facf9fc 19031+
4a4d8108
AM
19032+/* todo: hard/soft set? */
19033+void au_hiput(struct au_hinode *hinode)
19034+{
19035+ au_hn_free(hinode);
19036+ dput(hinode->hi_whdentry);
19037+ iput(hinode->hi_inode);
19038+}
1facf9fc 19039+
4a4d8108
AM
19040+unsigned int au_hi_flags(struct inode *inode, int isdir)
19041+{
19042+ unsigned int flags;
19043+ const unsigned int mnt_flags = au_mntflags(inode->i_sb);
1facf9fc 19044+
4a4d8108
AM
19045+ flags = 0;
19046+ if (au_opt_test(mnt_flags, XINO))
19047+ au_fset_hi(flags, XINO);
19048+ if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
19049+ au_fset_hi(flags, HNOTIFY);
19050+ return flags;
1facf9fc 19051+}
19052+
4a4d8108
AM
19053+void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
19054+ struct inode *h_inode, unsigned int flags)
1308ab2a 19055+{
4a4d8108
AM
19056+ struct au_hinode *hinode;
19057+ struct inode *hi;
19058+ struct au_iinfo *iinfo = au_ii(inode);
1facf9fc 19059+
4a4d8108 19060+ IiMustWriteLock(inode);
dece6358 19061+
5afbbe0d 19062+ hinode = au_hinode(iinfo, bindex);
4a4d8108
AM
19063+ hi = hinode->hi_inode;
19064+ AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
19065+
19066+ if (hi)
19067+ au_hiput(hinode);
19068+ hinode->hi_inode = h_inode;
19069+ if (h_inode) {
19070+ int err;
19071+ struct super_block *sb = inode->i_sb;
19072+ struct au_branch *br;
19073+
027c5e7a
AM
19074+ AuDebugOn(inode->i_mode
19075+ && (h_inode->i_mode & S_IFMT)
19076+ != (inode->i_mode & S_IFMT));
5afbbe0d 19077+ if (bindex == iinfo->ii_btop)
4a4d8108
AM
19078+ au_cpup_igen(inode, h_inode);
19079+ br = au_sbr(sb, bindex);
19080+ hinode->hi_id = br->br_id;
19081+ if (au_ftest_hi(flags, XINO)) {
19082+ err = au_xino_write(sb, bindex, h_inode->i_ino,
19083+ inode->i_ino);
19084+ if (unlikely(err))
19085+ AuIOErr1("failed au_xino_write() %d\n", err);
19086+ }
19087+
19088+ if (au_ftest_hi(flags, HNOTIFY)
19089+ && au_br_hnotifyable(br->br_perm)) {
027c5e7a 19090+ err = au_hn_alloc(hinode, inode);
4a4d8108
AM
19091+ if (unlikely(err))
19092+ AuIOErr1("au_hn_alloc() %d\n", err);
1308ab2a 19093+ }
19094+ }
4a4d8108 19095+}
dece6358 19096+
4a4d8108
AM
19097+void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
19098+ struct dentry *h_wh)
19099+{
19100+ struct au_hinode *hinode;
dece6358 19101+
4a4d8108
AM
19102+ IiMustWriteLock(inode);
19103+
5afbbe0d 19104+ hinode = au_hinode(au_ii(inode), bindex);
4a4d8108
AM
19105+ AuDebugOn(hinode->hi_whdentry);
19106+ hinode->hi_whdentry = h_wh;
1facf9fc 19107+}
19108+
537831f9 19109+void au_update_iigen(struct inode *inode, int half)
1308ab2a 19110+{
537831f9
AM
19111+ struct au_iinfo *iinfo;
19112+ struct au_iigen *iigen;
19113+ unsigned int sigen;
19114+
19115+ sigen = au_sigen(inode->i_sb);
19116+ iinfo = au_ii(inode);
19117+ iigen = &iinfo->ii_generation;
be52b249 19118+ spin_lock(&iigen->ig_spin);
537831f9
AM
19119+ iigen->ig_generation = sigen;
19120+ if (half)
19121+ au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
19122+ else
19123+ au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
be52b249 19124+ spin_unlock(&iigen->ig_spin);
4a4d8108 19125+}
1facf9fc 19126+
4a4d8108
AM
19127+/* it may be called at remount time, too */
19128+void au_update_ibrange(struct inode *inode, int do_put_zero)
19129+{
19130+ struct au_iinfo *iinfo;
5afbbe0d 19131+ aufs_bindex_t bindex, bbot;
1facf9fc 19132+
5afbbe0d 19133+ AuDebugOn(au_is_bad_inode(inode));
4a4d8108 19134+ IiMustWriteLock(inode);
1facf9fc 19135+
5afbbe0d
AM
19136+ iinfo = au_ii(inode);
19137+ if (do_put_zero && iinfo->ii_btop >= 0) {
19138+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
4a4d8108
AM
19139+ bindex++) {
19140+ struct inode *h_i;
1facf9fc 19141+
5afbbe0d 19142+ h_i = au_hinode(iinfo, bindex)->hi_inode;
38d290e6
JR
19143+ if (h_i
19144+ && !h_i->i_nlink
19145+ && !(h_i->i_state & I_LINKABLE))
027c5e7a
AM
19146+ au_set_h_iptr(inode, bindex, NULL, 0);
19147+ }
4a4d8108
AM
19148+ }
19149+
5afbbe0d
AM
19150+ iinfo->ii_btop = -1;
19151+ iinfo->ii_bbot = -1;
19152+ bbot = au_sbbot(inode->i_sb);
19153+ for (bindex = 0; bindex <= bbot; bindex++)
19154+ if (au_hinode(iinfo, bindex)->hi_inode) {
19155+ iinfo->ii_btop = bindex;
4a4d8108 19156+ break;
027c5e7a 19157+ }
5afbbe0d
AM
19158+ if (iinfo->ii_btop >= 0)
19159+ for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
19160+ if (au_hinode(iinfo, bindex)->hi_inode) {
19161+ iinfo->ii_bbot = bindex;
027c5e7a
AM
19162+ break;
19163+ }
5afbbe0d 19164+ AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
1308ab2a 19165+}
1facf9fc 19166+
dece6358 19167+/* ---------------------------------------------------------------------- */
1facf9fc 19168+
4a4d8108 19169+void au_icntnr_init_once(void *_c)
dece6358 19170+{
4a4d8108
AM
19171+ struct au_icntnr *c = _c;
19172+ struct au_iinfo *iinfo = &c->iinfo;
1facf9fc 19173+
be52b249 19174+ spin_lock_init(&iinfo->ii_generation.ig_spin);
4a4d8108
AM
19175+ au_rw_init(&iinfo->ii_rwsem);
19176+ inode_init_once(&c->vfs_inode);
19177+}
1facf9fc 19178+
5afbbe0d
AM
19179+void au_hinode_init(struct au_hinode *hinode)
19180+{
19181+ hinode->hi_inode = NULL;
19182+ hinode->hi_id = -1;
19183+ au_hn_init(hinode);
19184+ hinode->hi_whdentry = NULL;
19185+}
19186+
4a4d8108
AM
19187+int au_iinfo_init(struct inode *inode)
19188+{
19189+ struct au_iinfo *iinfo;
19190+ struct super_block *sb;
5afbbe0d 19191+ struct au_hinode *hi;
4a4d8108 19192+ int nbr, i;
1facf9fc 19193+
4a4d8108
AM
19194+ sb = inode->i_sb;
19195+ iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
5afbbe0d 19196+ nbr = au_sbbot(sb) + 1;
4a4d8108
AM
19197+ if (unlikely(nbr <= 0))
19198+ nbr = 1;
5afbbe0d
AM
19199+ hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
19200+ if (hi) {
acd2b654 19201+ au_lcnt_inc(&au_sbi(sb)->si_ninodes);
5afbbe0d
AM
19202+
19203+ iinfo->ii_hinode = hi;
19204+ for (i = 0; i < nbr; i++, hi++)
19205+ au_hinode_init(hi);
1facf9fc 19206+
537831f9 19207+ iinfo->ii_generation.ig_generation = au_sigen(sb);
5afbbe0d
AM
19208+ iinfo->ii_btop = -1;
19209+ iinfo->ii_bbot = -1;
4a4d8108
AM
19210+ iinfo->ii_vdir = NULL;
19211+ return 0;
1308ab2a 19212+ }
4a4d8108
AM
19213+ return -ENOMEM;
19214+}
1facf9fc 19215+
e2f27e51 19216+int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
4a4d8108 19217+{
5afbbe0d 19218+ int err, i;
4a4d8108 19219+ struct au_hinode *hip;
1facf9fc 19220+
4a4d8108
AM
19221+ AuRwMustWriteLock(&iinfo->ii_rwsem);
19222+
19223+ err = -ENOMEM;
e2f27e51
AM
19224+ hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
19225+ may_shrink);
4a4d8108
AM
19226+ if (hip) {
19227+ iinfo->ii_hinode = hip;
5afbbe0d
AM
19228+ i = iinfo->ii_bbot + 1;
19229+ hip += i;
19230+ for (; i < nbr; i++, hip++)
19231+ au_hinode_init(hip);
4a4d8108 19232+ err = 0;
1308ab2a 19233+ }
4a4d8108 19234+
1308ab2a 19235+ return err;
1facf9fc 19236+}
19237+
4a4d8108 19238+void au_iinfo_fin(struct inode *inode)
1facf9fc 19239+{
4a4d8108
AM
19240+ struct au_iinfo *iinfo;
19241+ struct au_hinode *hi;
19242+ struct super_block *sb;
5afbbe0d 19243+ aufs_bindex_t bindex, bbot;
b752ccd1 19244+ const unsigned char unlinked = !inode->i_nlink;
1308ab2a 19245+
5afbbe0d 19246+ AuDebugOn(au_is_bad_inode(inode));
1308ab2a 19247+
b752ccd1 19248+ sb = inode->i_sb;
acd2b654 19249+ au_lcnt_dec(&au_sbi(sb)->si_ninodes);
b752ccd1
AM
19250+ if (si_pid_test(sb))
19251+ au_xino_delete_inode(inode, unlinked);
19252+ else {
19253+ /*
19254+ * it is safe to hide the dependency between sbinfo and
19255+ * sb->s_umount.
19256+ */
19257+ lockdep_off();
19258+ si_noflush_read_lock(sb);
19259+ au_xino_delete_inode(inode, unlinked);
19260+ si_read_unlock(sb);
19261+ lockdep_on();
19262+ }
19263+
5afbbe0d 19264+ iinfo = au_ii(inode);
4a4d8108 19265+ if (iinfo->ii_vdir)
1c60b727 19266+ au_vdir_free(iinfo->ii_vdir);
1308ab2a 19267+
5afbbe0d 19268+ bindex = iinfo->ii_btop;
b752ccd1 19269+ if (bindex >= 0) {
5afbbe0d
AM
19270+ hi = au_hinode(iinfo, bindex);
19271+ bbot = iinfo->ii_bbot;
19272+ while (bindex++ <= bbot) {
b752ccd1 19273+ if (hi->hi_inode)
4a4d8108 19274+ au_hiput(hi);
4a4d8108
AM
19275+ hi++;
19276+ }
19277+ }
9f237c51 19278+ au_kfree_rcu(iinfo->ii_hinode);
4a4d8108 19279+ AuRwDestroy(&iinfo->ii_rwsem);
dece6358 19280+}
7f207e10 19281diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
eca34b5c 19282--- /usr/share/empty/fs/aufs/inode.c 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 19283+++ linux/fs/aufs/inode.c 2022-11-05 23:02:18.965889284 +0100
0b2a12c6 19284@@ -0,0 +1,531 @@
cd7a4cd9 19285+// SPDX-License-Identifier: GPL-2.0
4a4d8108 19286+/*
0dcfbb52 19287+ * Copyright (C) 2005-2022 Junjiro R. Okajima
4a4d8108 19288+ *
0dcfbb52 19289+ * This program is free software; you can redistribute it and/or modify
4a4d8108
AM
19290+ * it under the terms of the GNU General Public License as published by
19291+ * the Free Software Foundation; either version 2 of the License, or
19292+ * (at your option) any later version.
19293+ *
19294+ * This program is distributed in the hope that it will be useful,
19295+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19296+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19297+ * GNU General Public License for more details.
19298+ *
19299+ * You should have received a copy of the GNU General Public License
523b37e3 19300+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 19301+ */
1facf9fc 19302+
4a4d8108
AM
19303+/*
19304+ * inode functions
19305+ */
1facf9fc 19306+
eca801bf 19307+#include <linux/iversion.h>
4a4d8108 19308+#include "aufs.h"
1308ab2a 19309+
4a4d8108
AM
19310+struct inode *au_igrab(struct inode *inode)
19311+{
19312+ if (inode) {
19313+ AuDebugOn(!atomic_read(&inode->i_count));
027c5e7a 19314+ ihold(inode);
1facf9fc 19315+ }
4a4d8108
AM
19316+ return inode;
19317+}
1facf9fc 19318+
4a4d8108
AM
19319+static void au_refresh_hinode_attr(struct inode *inode, int do_version)
19320+{
19321+ au_cpup_attr_all(inode, /*force*/0);
537831f9 19322+ au_update_iigen(inode, /*half*/1);
4a4d8108 19323+ if (do_version)
be118d29 19324+ inode_inc_iversion(inode);
dece6358 19325+}
1facf9fc 19326+
027c5e7a 19327+static int au_ii_refresh(struct inode *inode, int *update)
dece6358 19328+{
e2f27e51 19329+ int err, e, nbr;
027c5e7a 19330+ umode_t type;
4a4d8108 19331+ aufs_bindex_t bindex, new_bindex;
1308ab2a 19332+ struct super_block *sb;
4a4d8108 19333+ struct au_iinfo *iinfo;
027c5e7a 19334+ struct au_hinode *p, *q, tmp;
1facf9fc 19335+
5afbbe0d 19336+ AuDebugOn(au_is_bad_inode(inode));
4a4d8108 19337+ IiMustWriteLock(inode);
1facf9fc 19338+
027c5e7a 19339+ *update = 0;
4a4d8108 19340+ sb = inode->i_sb;
e2f27e51 19341+ nbr = au_sbbot(sb) + 1;
027c5e7a 19342+ type = inode->i_mode & S_IFMT;
4a4d8108 19343+ iinfo = au_ii(inode);
e2f27e51 19344+ err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
4a4d8108 19345+ if (unlikely(err))
1308ab2a 19346+ goto out;
1facf9fc 19347+
5afbbe0d
AM
19348+ AuDebugOn(iinfo->ii_btop < 0);
19349+ p = au_hinode(iinfo, iinfo->ii_btop);
19350+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
4a4d8108
AM
19351+ bindex++, p++) {
19352+ if (!p->hi_inode)
19353+ continue;
1facf9fc 19354+
027c5e7a 19355+ AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
4a4d8108
AM
19356+ new_bindex = au_br_index(sb, p->hi_id);
19357+ if (new_bindex == bindex)
19358+ continue;
1facf9fc 19359+
4a4d8108 19360+ if (new_bindex < 0) {
027c5e7a 19361+ *update = 1;
4a4d8108
AM
19362+ au_hiput(p);
19363+ p->hi_inode = NULL;
19364+ continue;
1308ab2a 19365+ }
4a4d8108 19366+
5afbbe0d
AM
19367+ if (new_bindex < iinfo->ii_btop)
19368+ iinfo->ii_btop = new_bindex;
19369+ if (iinfo->ii_bbot < new_bindex)
19370+ iinfo->ii_bbot = new_bindex;
4a4d8108 19371+ /* swap two lower inode, and loop again */
5afbbe0d 19372+ q = au_hinode(iinfo, new_bindex);
4a4d8108
AM
19373+ tmp = *q;
19374+ *q = *p;
19375+ *p = tmp;
19376+ if (tmp.hi_inode) {
19377+ bindex--;
19378+ p--;
1308ab2a 19379+ }
19380+ }
4a4d8108 19381+ au_update_ibrange(inode, /*do_put_zero*/0);
e2f27e51 19382+ au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
4a4d8108
AM
19383+ e = au_dy_irefresh(inode);
19384+ if (unlikely(e && !err))
19385+ err = e;
1facf9fc 19386+
4f0767ce 19387+out:
027c5e7a
AM
19388+ AuTraceErr(err);
19389+ return err;
19390+}
19391+
b95c5147
AM
19392+void au_refresh_iop(struct inode *inode, int force_getattr)
19393+{
19394+ int type;
19395+ struct au_sbinfo *sbi = au_sbi(inode->i_sb);
19396+ const struct inode_operations *iop
19397+ = force_getattr ? aufs_iop : sbi->si_iop_array;
19398+
19399+ if (inode->i_op == iop)
19400+ return;
19401+
19402+ switch (inode->i_mode & S_IFMT) {
19403+ case S_IFDIR:
19404+ type = AuIop_DIR;
19405+ break;
19406+ case S_IFLNK:
19407+ type = AuIop_SYMLINK;
19408+ break;
19409+ default:
19410+ type = AuIop_OTHER;
19411+ break;
19412+ }
19413+
19414+ inode->i_op = iop + type;
19415+ /* unnecessary smp_wmb() */
19416+}
19417+
027c5e7a
AM
19418+int au_refresh_hinode_self(struct inode *inode)
19419+{
19420+ int err, update;
19421+
19422+ err = au_ii_refresh(inode, &update);
19423+ if (!err)
19424+ au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
19425+
19426+ AuTraceErr(err);
4a4d8108
AM
19427+ return err;
19428+}
1facf9fc 19429+
4a4d8108
AM
19430+int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
19431+{
027c5e7a 19432+ int err, e, update;
4a4d8108 19433+ unsigned int flags;
027c5e7a 19434+ umode_t mode;
5afbbe0d 19435+ aufs_bindex_t bindex, bbot;
027c5e7a 19436+ unsigned char isdir;
4a4d8108
AM
19437+ struct au_hinode *p;
19438+ struct au_iinfo *iinfo;
1facf9fc 19439+
027c5e7a 19440+ err = au_ii_refresh(inode, &update);
4a4d8108
AM
19441+ if (unlikely(err))
19442+ goto out;
19443+
19444+ update = 0;
19445+ iinfo = au_ii(inode);
5afbbe0d 19446+ p = au_hinode(iinfo, iinfo->ii_btop);
027c5e7a
AM
19447+ mode = (inode->i_mode & S_IFMT);
19448+ isdir = S_ISDIR(mode);
4a4d8108 19449+ flags = au_hi_flags(inode, isdir);
5afbbe0d
AM
19450+ bbot = au_dbbot(dentry);
19451+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
5527c038 19452+ struct inode *h_i, *h_inode;
4a4d8108
AM
19453+ struct dentry *h_d;
19454+
19455+ h_d = au_h_dptr(dentry, bindex);
5527c038 19456+ if (!h_d || d_is_negative(h_d))
4a4d8108
AM
19457+ continue;
19458+
5527c038
JR
19459+ h_inode = d_inode(h_d);
19460+ AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
5afbbe0d 19461+ if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
4a4d8108
AM
19462+ h_i = au_h_iptr(inode, bindex);
19463+ if (h_i) {
5527c038 19464+ if (h_i == h_inode)
4a4d8108
AM
19465+ continue;
19466+ err = -EIO;
19467+ break;
19468+ }
19469+ }
5afbbe0d
AM
19470+ if (bindex < iinfo->ii_btop)
19471+ iinfo->ii_btop = bindex;
19472+ if (iinfo->ii_bbot < bindex)
19473+ iinfo->ii_bbot = bindex;
5527c038 19474+ au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
4a4d8108 19475+ update = 1;
1308ab2a 19476+ }
4a4d8108
AM
19477+ au_update_ibrange(inode, /*do_put_zero*/0);
19478+ e = au_dy_irefresh(inode);
19479+ if (unlikely(e && !err))
19480+ err = e;
027c5e7a
AM
19481+ if (!err)
19482+ au_refresh_hinode_attr(inode, update && isdir);
4a4d8108 19483+
4f0767ce 19484+out:
4a4d8108 19485+ AuTraceErr(err);
1308ab2a 19486+ return err;
dece6358
AM
19487+}
19488+
4a4d8108 19489+static int set_inode(struct inode *inode, struct dentry *dentry)
dece6358 19490+{
4a4d8108
AM
19491+ int err;
19492+ unsigned int flags;
19493+ umode_t mode;
5afbbe0d 19494+ aufs_bindex_t bindex, btop, btail;
4a4d8108
AM
19495+ unsigned char isdir;
19496+ struct dentry *h_dentry;
19497+ struct inode *h_inode;
19498+ struct au_iinfo *iinfo;
43982f53 19499+ const struct inode_operations *iop;
dece6358 19500+
4a4d8108 19501+ IiMustWriteLock(inode);
dece6358 19502+
4a4d8108
AM
19503+ err = 0;
19504+ isdir = 0;
b95c5147 19505+ iop = au_sbi(inode->i_sb)->si_iop_array;
5afbbe0d
AM
19506+ btop = au_dbtop(dentry);
19507+ h_dentry = au_h_dptr(dentry, btop);
5527c038 19508+ h_inode = d_inode(h_dentry);
4a4d8108
AM
19509+ mode = h_inode->i_mode;
19510+ switch (mode & S_IFMT) {
19511+ case S_IFREG:
19512+ btail = au_dbtail(dentry);
b95c5147 19513+ inode->i_op = iop + AuIop_OTHER;
4a4d8108 19514+ inode->i_fop = &aufs_file_fop;
5afbbe0d 19515+ err = au_dy_iaop(inode, btop, h_inode);
4a4d8108
AM
19516+ if (unlikely(err))
19517+ goto out;
19518+ break;
19519+ case S_IFDIR:
19520+ isdir = 1;
19521+ btail = au_dbtaildir(dentry);
b95c5147 19522+ inode->i_op = iop + AuIop_DIR;
4a4d8108
AM
19523+ inode->i_fop = &aufs_dir_fop;
19524+ break;
19525+ case S_IFLNK:
19526+ btail = au_dbtail(dentry);
b95c5147 19527+ inode->i_op = iop + AuIop_SYMLINK;
4a4d8108
AM
19528+ break;
19529+ case S_IFBLK:
19530+ case S_IFCHR:
19531+ case S_IFIFO:
19532+ case S_IFSOCK:
19533+ btail = au_dbtail(dentry);
b95c5147 19534+ inode->i_op = iop + AuIop_OTHER;
38d290e6 19535+ init_special_inode(inode, mode, h_inode->i_rdev);
4a4d8108
AM
19536+ break;
19537+ default:
19538+ AuIOErr("Unknown file type 0%o\n", mode);
19539+ err = -EIO;
1308ab2a 19540+ goto out;
4a4d8108 19541+ }
dece6358 19542+
4a4d8108
AM
19543+ /* do not set hnotify for whiteouted dirs (SHWH mode) */
19544+ flags = au_hi_flags(inode, isdir);
19545+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
19546+ && au_ftest_hi(flags, HNOTIFY)
19547+ && dentry->d_name.len > AUFS_WH_PFX_LEN
19548+ && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
19549+ au_fclr_hi(flags, HNOTIFY);
19550+ iinfo = au_ii(inode);
5afbbe0d
AM
19551+ iinfo->ii_btop = btop;
19552+ iinfo->ii_bbot = btail;
19553+ for (bindex = btop; bindex <= btail; bindex++) {
4a4d8108
AM
19554+ h_dentry = au_h_dptr(dentry, bindex);
19555+ if (h_dentry)
19556+ au_set_h_iptr(inode, bindex,
5527c038 19557+ au_igrab(d_inode(h_dentry)), flags);
4a4d8108
AM
19558+ }
19559+ au_cpup_attr_all(inode, /*force*/1);
c1595e42
JR
19560+ /*
19561+ * to force calling aufs_get_acl() every time,
19562+ * do not call cache_no_acl() for aufs inode.
19563+ */
dece6358 19564+
4f0767ce 19565+out:
4a4d8108
AM
19566+ return err;
19567+}
dece6358 19568+
027c5e7a
AM
19569+/*
19570+ * successful returns with iinfo write_locked
19571+ * minus: errno
19572+ * zero: success, matched
19573+ * plus: no error, but unmatched
19574+ */
19575+static int reval_inode(struct inode *inode, struct dentry *dentry)
4a4d8108
AM
19576+{
19577+ int err;
cfc41e69 19578+ unsigned int gen, igflags;
5afbbe0d 19579+ aufs_bindex_t bindex, bbot;
4a4d8108 19580+ struct inode *h_inode, *h_dinode;
5527c038 19581+ struct dentry *h_dentry;
dece6358 19582+
4a4d8108
AM
19583+ /*
19584+ * before this function, if aufs got any iinfo lock, it must be only
19585+ * one, the parent dir.
19586+ * it can happen by UDBA and the obsoleted inode number.
19587+ */
19588+ err = -EIO;
19589+ if (unlikely(inode->i_ino == parent_ino(dentry)))
19590+ goto out;
19591+
027c5e7a 19592+ err = 1;
4a4d8108 19593+ ii_write_lock_new_child(inode);
5afbbe0d 19594+ h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
5527c038 19595+ h_dinode = d_inode(h_dentry);
5afbbe0d
AM
19596+ bbot = au_ibbot(inode);
19597+ for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
4a4d8108 19598+ h_inode = au_h_iptr(inode, bindex);
537831f9
AM
19599+ if (!h_inode || h_inode != h_dinode)
19600+ continue;
19601+
19602+ err = 0;
cfc41e69 19603+ gen = au_iigen(inode, &igflags);
537831f9 19604+ if (gen == au_digen(dentry)
cfc41e69 19605+ && !au_ig_ftest(igflags, HALF_REFRESHED))
4a4d8108 19606+ break;
537831f9
AM
19607+
19608+ /* fully refresh inode using dentry */
19609+ err = au_refresh_hinode(inode, dentry);
19610+ if (!err)
19611+ au_update_iigen(inode, /*half*/0);
19612+ break;
1facf9fc 19613+ }
dece6358 19614+
4a4d8108
AM
19615+ if (unlikely(err))
19616+ ii_write_unlock(inode);
4f0767ce 19617+out:
1facf9fc 19618+ return err;
19619+}
1facf9fc 19620+
4a4d8108
AM
19621+int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
19622+ unsigned int d_type, ino_t *ino)
1facf9fc 19623+{
521ced18
JR
19624+ int err, idx;
19625+ const int isnondir = d_type != DT_DIR;
1facf9fc 19626+
b752ccd1 19627+ /* prevent hardlinked inode number from race condition */
521ced18
JR
19628+ if (isnondir) {
19629+ err = au_xinondir_enter(sb, bindex, h_ino, &idx);
19630+ if (unlikely(err))
19631+ goto out;
4a4d8108 19632+ }
521ced18 19633+
4a4d8108
AM
19634+ err = au_xino_read(sb, bindex, h_ino, ino);
19635+ if (unlikely(err))
521ced18 19636+ goto out_xinondir;
1308ab2a 19637+
4a4d8108
AM
19638+ if (!*ino) {
19639+ err = -EIO;
19640+ *ino = au_xino_new_ino(sb);
19641+ if (unlikely(!*ino))
521ced18 19642+ goto out_xinondir;
4a4d8108
AM
19643+ err = au_xino_write(sb, bindex, h_ino, *ino);
19644+ if (unlikely(err))
521ced18 19645+ goto out_xinondir;
1308ab2a 19646+ }
1facf9fc 19647+
521ced18
JR
19648+out_xinondir:
19649+ if (isnondir && idx >= 0)
19650+ au_xinondir_leave(sb, bindex, h_ino, idx);
4f0767ce 19651+out:
1facf9fc 19652+ return err;
19653+}
19654+
4a4d8108
AM
19655+/* successful returns with iinfo write_locked */
19656+/* todo: return with unlocked? */
19657+struct inode *au_new_inode(struct dentry *dentry, int must_new)
1facf9fc 19658+{
5527c038 19659+ struct inode *inode, *h_inode;
4a4d8108
AM
19660+ struct dentry *h_dentry;
19661+ struct super_block *sb;
19662+ ino_t h_ino, ino;
521ced18 19663+ int err, idx, hlinked;
5afbbe0d 19664+ aufs_bindex_t btop;
1facf9fc 19665+
4a4d8108 19666+ sb = dentry->d_sb;
5afbbe0d
AM
19667+ btop = au_dbtop(dentry);
19668+ h_dentry = au_h_dptr(dentry, btop);
5527c038
JR
19669+ h_inode = d_inode(h_dentry);
19670+ h_ino = h_inode->i_ino;
521ced18 19671+ hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
b752ccd1 19672+
521ced18 19673+new_ino:
b752ccd1
AM
19674+ /*
19675+ * stop 'race'-ing between hardlinks under different
19676+ * parents.
19677+ */
521ced18
JR
19678+ if (hlinked) {
19679+ err = au_xinondir_enter(sb, btop, h_ino, &idx);
19680+ inode = ERR_PTR(err);
19681+ if (unlikely(err))
19682+ goto out;
19683+ }
b752ccd1 19684+
5afbbe0d 19685+ err = au_xino_read(sb, btop, h_ino, &ino);
4a4d8108
AM
19686+ inode = ERR_PTR(err);
19687+ if (unlikely(err))
521ced18 19688+ goto out_xinondir;
b752ccd1 19689+
4a4d8108
AM
19690+ if (!ino) {
19691+ ino = au_xino_new_ino(sb);
19692+ if (unlikely(!ino)) {
19693+ inode = ERR_PTR(-EIO);
521ced18 19694+ goto out_xinondir;
dece6358
AM
19695+ }
19696+ }
1facf9fc 19697+
4a4d8108
AM
19698+ AuDbg("i%lu\n", (unsigned long)ino);
19699+ inode = au_iget_locked(sb, ino);
19700+ err = PTR_ERR(inode);
19701+ if (IS_ERR(inode))
521ced18 19702+ goto out_xinondir;
1facf9fc 19703+
4a4d8108
AM
19704+ AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
19705+ if (inode->i_state & I_NEW) {
19706+ ii_write_lock_new_child(inode);
19707+ err = set_inode(inode, dentry);
19708+ if (!err) {
19709+ unlock_new_inode(inode);
521ced18 19710+ goto out_xinondir; /* success */
4a4d8108 19711+ }
1308ab2a 19712+
027c5e7a
AM
19713+ /*
19714+ * iget_failed() calls iput(), but we need to call
19715+ * ii_write_unlock() after iget_failed(). so dirty hack for
19716+ * i_count.
19717+ */
19718+ atomic_inc(&inode->i_count);
4a4d8108 19719+ iget_failed(inode);
027c5e7a 19720+ ii_write_unlock(inode);
5afbbe0d 19721+ au_xino_write(sb, btop, h_ino, /*ino*/0);
027c5e7a
AM
19722+ /* ignore this error */
19723+ goto out_iput;
19724+ } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
b752ccd1
AM
19725+ /*
19726+ * horrible race condition between lookup, readdir and copyup
19727+ * (or something).
19728+ */
521ced18
JR
19729+ if (hlinked && idx >= 0)
19730+ au_xinondir_leave(sb, btop, h_ino, idx);
027c5e7a
AM
19731+ err = reval_inode(inode, dentry);
19732+ if (unlikely(err < 0)) {
521ced18 19733+ hlinked = 0;
027c5e7a
AM
19734+ goto out_iput;
19735+ }
521ced18 19736+ if (!err)
4a4d8108 19737+ goto out; /* success */
521ced18
JR
19738+ else if (hlinked && idx >= 0) {
19739+ err = au_xinondir_enter(sb, btop, h_ino, &idx);
19740+ if (unlikely(err)) {
19741+ iput(inode);
19742+ inode = ERR_PTR(err);
19743+ goto out;
19744+ }
19745+ }
4a4d8108
AM
19746+ }
19747+
5527c038 19748+ if (unlikely(au_test_fs_unique_ino(h_inode)))
4a4d8108 19749+ AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
523b37e3 19750+ " b%d, %s, %pd, hi%lu, i%lu.\n",
5afbbe0d 19751+ btop, au_sbtype(h_dentry->d_sb), dentry,
4a4d8108
AM
19752+ (unsigned long)h_ino, (unsigned long)ino);
19753+ ino = 0;
5afbbe0d 19754+ err = au_xino_write(sb, btop, h_ino, /*ino*/0);
4a4d8108
AM
19755+ if (!err) {
19756+ iput(inode);
521ced18
JR
19757+ if (hlinked && idx >= 0)
19758+ au_xinondir_leave(sb, btop, h_ino, idx);
4a4d8108
AM
19759+ goto new_ino;
19760+ }
1308ab2a 19761+
4f0767ce 19762+out_iput:
4a4d8108 19763+ iput(inode);
4a4d8108 19764+ inode = ERR_PTR(err);
521ced18
JR
19765+out_xinondir:
19766+ if (hlinked && idx >= 0)
19767+ au_xinondir_leave(sb, btop, h_ino, idx);
4f0767ce 19768+out:
4a4d8108 19769+ return inode;
1facf9fc 19770+}
19771+
4a4d8108 19772+/* ---------------------------------------------------------------------- */
1facf9fc 19773+
4a4d8108
AM
19774+int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
19775+ struct inode *inode)
19776+{
19777+ int err;
076b876e 19778+ struct inode *hi;
1facf9fc 19779+
4a4d8108 19780+ err = au_br_rdonly(au_sbr(sb, bindex));
1facf9fc 19781+
4a4d8108
AM
19782+ /* pseudo-link after flushed may happen out of bounds */
19783+ if (!err
19784+ && inode
5afbbe0d
AM
19785+ && au_ibtop(inode) <= bindex
19786+ && bindex <= au_ibbot(inode)) {
4a4d8108
AM
19787+ /*
19788+ * permission check is unnecessary since vfsub routine
19789+ * will be called later
19790+ */
076b876e 19791+ hi = au_h_iptr(inode, bindex);
4a4d8108
AM
19792+ if (hi)
19793+ err = IS_IMMUTABLE(hi) ? -EROFS : 0;
1facf9fc 19794+ }
19795+
4a4d8108
AM
19796+ return err;
19797+}
dece6358 19798+
0b2a12c6
JR
19799+int au_test_h_perm(struct user_namespace *h_userns, struct inode *h_inode,
19800+ int mask)
4a4d8108 19801+{
2dfbb274 19802+ if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
4a4d8108 19803+ return 0;
0b2a12c6 19804+ return inode_permission(h_userns, h_inode, mask);
4a4d8108 19805+}
1facf9fc 19806+
0b2a12c6
JR
19807+int au_test_h_perm_sio(struct user_namespace *h_userns, struct inode *h_inode,
19808+ int mask)
4a4d8108
AM
19809+{
19810+ if (au_test_nfs(h_inode->i_sb)
19811+ && (mask & MAY_WRITE)
19812+ && S_ISDIR(h_inode->i_mode))
19813+ mask |= MAY_READ; /* force permission check */
0b2a12c6 19814+ return au_test_h_perm(h_userns, h_inode, mask);
1facf9fc 19815+}
7f207e10 19816diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
eca34b5c 19817--- /usr/share/empty/fs/aufs/inode.h 1970-01-01 01:00:00.000000000 +0100
2255d0fe 19818+++ linux/fs/aufs/inode.h 2022-12-17 09:21:34.799855195 +0100
0b2a12c6 19819@@ -0,0 +1,705 @@
062440b3 19820+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 19821+/*
0dcfbb52 19822+ * Copyright (C) 2005-2022 Junjiro R. Okajima
4a4d8108 19823+ *
0dcfbb52 19824+ * This program is free software; you can redistribute it and/or modify
4a4d8108
AM
19825+ * it under the terms of the GNU General Public License as published by
19826+ * the Free Software Foundation; either version 2 of the License, or
19827+ * (at your option) any later version.
19828+ *
19829+ * This program is distributed in the hope that it will be useful,
19830+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19831+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19832+ * GNU General Public License for more details.
19833+ *
19834+ * You should have received a copy of the GNU General Public License
523b37e3 19835+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 19836+ */
1facf9fc 19837+
1308ab2a 19838+/*
4a4d8108 19839+ * inode operations
1308ab2a 19840+ */
dece6358 19841+
4a4d8108
AM
19842+#ifndef __AUFS_INODE_H__
19843+#define __AUFS_INODE_H__
dece6358 19844+
4a4d8108 19845+#ifdef __KERNEL__
1308ab2a 19846+
4a4d8108 19847+#include <linux/fsnotify.h>
4a4d8108 19848+#include "rwsem.h"
1308ab2a 19849+
4a4d8108 19850+struct vfsmount;
1facf9fc 19851+
4a4d8108
AM
19852+struct au_hnotify {
19853+#ifdef CONFIG_AUFS_HNOTIFY
19854+#ifdef CONFIG_AUFS_HFSNOTIFY
7f207e10 19855+ /* never use fsnotify_add_vfsmount_mark() */
0c5527e5 19856+ struct fsnotify_mark hn_mark;
4a4d8108 19857+#endif
1c60b727 19858+ struct inode *hn_aufs_inode; /* no get/put */
9f237c51 19859+ struct rcu_head rcu;
4a4d8108
AM
19860+#endif
19861+} ____cacheline_aligned_in_smp;
1facf9fc 19862+
4a4d8108
AM
19863+struct au_hinode {
19864+ struct inode *hi_inode;
19865+ aufs_bindex_t hi_id;
19866+#ifdef CONFIG_AUFS_HNOTIFY
19867+ struct au_hnotify *hi_notify;
19868+#endif
dece6358 19869+
4a4d8108
AM
19870+ /* reference to the copied-up whiteout with get/put */
19871+ struct dentry *hi_whdentry;
19872+};
dece6358 19873+
537831f9
AM
19874+/* ig_flags */
19875+#define AuIG_HALF_REFRESHED 1
19876+#define au_ig_ftest(flags, name) ((flags) & AuIG_##name)
19877+#define au_ig_fset(flags, name) \
19878+ do { (flags) |= AuIG_##name; } while (0)
19879+#define au_ig_fclr(flags, name) \
19880+ do { (flags) &= ~AuIG_##name; } while (0)
19881+
19882+struct au_iigen {
be52b249 19883+ spinlock_t ig_spin;
537831f9
AM
19884+ __u32 ig_generation, ig_flags;
19885+};
19886+
4a4d8108
AM
19887+struct au_vdir;
19888+struct au_iinfo {
7a9e40b8 19889+ struct au_iigen ii_generation;
4a4d8108 19890+ struct super_block *ii_hsb1; /* no get/put */
1facf9fc 19891+
4a4d8108 19892+ struct au_rwsem ii_rwsem;
5afbbe0d 19893+ aufs_bindex_t ii_btop, ii_bbot;
4a4d8108
AM
19894+ __u32 ii_higen;
19895+ struct au_hinode *ii_hinode;
19896+ struct au_vdir *ii_vdir;
19897+};
1facf9fc 19898+
4a4d8108 19899+struct au_icntnr {
9f237c51
AM
19900+ struct au_iinfo iinfo;
19901+ struct inode vfs_inode;
19902+ struct hlist_bl_node plink;
19903+ struct rcu_head rcu;
4a4d8108 19904+} ____cacheline_aligned_in_smp;
1308ab2a 19905+
4a4d8108
AM
19906+/* au_pin flags */
19907+#define AuPin_DI_LOCKED 1
19908+#define AuPin_MNT_WRITE (1 << 1)
19909+#define au_ftest_pin(flags, name) ((flags) & AuPin_##name)
7f207e10
AM
19910+#define au_fset_pin(flags, name) \
19911+ do { (flags) |= AuPin_##name; } while (0)
19912+#define au_fclr_pin(flags, name) \
19913+ do { (flags) &= ~AuPin_##name; } while (0)
4a4d8108
AM
19914+
19915+struct au_pin {
19916+ /* input */
19917+ struct dentry *dentry;
19918+ unsigned int udba;
19919+ unsigned char lsc_di, lsc_hi, flags;
19920+ aufs_bindex_t bindex;
19921+
19922+ /* output */
19923+ struct dentry *parent;
19924+ struct au_hinode *hdir;
19925+ struct vfsmount *h_mnt;
86dc4139
AM
19926+
19927+ /* temporary unlock/relock for copyup */
19928+ struct dentry *h_dentry, *h_parent;
19929+ struct au_branch *br;
19930+ struct task_struct *task;
4a4d8108 19931+};
1facf9fc 19932+
86dc4139 19933+void au_pin_hdir_unlock(struct au_pin *p);
c1595e42 19934+int au_pin_hdir_lock(struct au_pin *p);
86dc4139 19935+int au_pin_hdir_relock(struct au_pin *p);
86dc4139
AM
19936+void au_pin_hdir_acquire_nest(struct au_pin *p);
19937+void au_pin_hdir_release(struct au_pin *p);
19938+
1308ab2a 19939+/* ---------------------------------------------------------------------- */
19940+
4a4d8108 19941+static inline struct au_iinfo *au_ii(struct inode *inode)
1facf9fc 19942+{
5afbbe0d
AM
19943+ BUG_ON(is_bad_inode(inode));
19944+ return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
4a4d8108 19945+}
1facf9fc 19946+
4a4d8108 19947+/* ---------------------------------------------------------------------- */
1facf9fc 19948+
4a4d8108
AM
19949+/* inode.c */
19950+struct inode *au_igrab(struct inode *inode);
b95c5147 19951+void au_refresh_iop(struct inode *inode, int force_getattr);
027c5e7a 19952+int au_refresh_hinode_self(struct inode *inode);
4a4d8108
AM
19953+int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
19954+int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
19955+ unsigned int d_type, ino_t *ino);
19956+struct inode *au_new_inode(struct dentry *dentry, int must_new);
19957+int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
19958+ struct inode *inode);
0b2a12c6
JR
19959+int au_test_h_perm(struct user_namespace *h_userns, struct inode *h_inode,
19960+ int mask);
19961+int au_test_h_perm_sio(struct user_namespace *h_userns, struct inode *h_inode,
19962+ int mask);
1facf9fc 19963+
4a4d8108
AM
19964+static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
19965+ ino_t h_ino, unsigned int d_type, ino_t *ino)
19966+{
19967+#ifdef CONFIG_AUFS_SHWH
19968+ return au_ino(sb, bindex, h_ino, d_type, ino);
19969+#else
19970+ return 0;
19971+#endif
19972+}
1facf9fc 19973+
4a4d8108 19974+/* i_op.c */
b95c5147
AM
19975+enum {
19976+ AuIop_SYMLINK,
19977+ AuIop_DIR,
19978+ AuIop_OTHER,
19979+ AuIop_Last
19980+};
43982f53 19981+extern struct inode_operations aufs_iop[AuIop_Last], /* not const */
b95c5147 19982+ aufs_iop_nogetattr[AuIop_Last];
1308ab2a 19983+
4a4d8108
AM
19984+/* au_wr_dir flags */
19985+#define AuWrDir_ADD_ENTRY 1
7e9cd9fe
AM
19986+#define AuWrDir_ISDIR (1 << 1)
19987+#define AuWrDir_TMPFILE (1 << 2)
4a4d8108 19988+#define au_ftest_wrdir(flags, name) ((flags) & AuWrDir_##name)
7f207e10
AM
19989+#define au_fset_wrdir(flags, name) \
19990+ do { (flags) |= AuWrDir_##name; } while (0)
19991+#define au_fclr_wrdir(flags, name) \
19992+ do { (flags) &= ~AuWrDir_##name; } while (0)
1facf9fc 19993+
4a4d8108
AM
19994+struct au_wr_dir_args {
19995+ aufs_bindex_t force_btgt;
19996+ unsigned char flags;
19997+};
19998+int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
19999+ struct au_wr_dir_args *args);
dece6358 20000+
4a4d8108
AM
20001+struct dentry *au_pinned_h_parent(struct au_pin *pin);
20002+void au_pin_init(struct au_pin *pin, struct dentry *dentry,
20003+ aufs_bindex_t bindex, int lsc_di, int lsc_hi,
20004+ unsigned int udba, unsigned char flags);
20005+int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
20006+ unsigned int udba, unsigned char flags) __must_check;
20007+int au_do_pin(struct au_pin *pin) __must_check;
20008+void au_unpin(struct au_pin *pin);
c1595e42
JR
20009+int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
20010+
20011+#define AuIcpup_DID_CPUP 1
20012+#define au_ftest_icpup(flags, name) ((flags) & AuIcpup_##name)
20013+#define au_fset_icpup(flags, name) \
20014+ do { (flags) |= AuIcpup_##name; } while (0)
20015+#define au_fclr_icpup(flags, name) \
20016+ do { (flags) &= ~AuIcpup_##name; } while (0)
20017+
20018+struct au_icpup_args {
20019+ unsigned char flags;
20020+ unsigned char pin_flags;
20021+ aufs_bindex_t btgt;
20022+ unsigned int udba;
20023+ struct au_pin pin;
20024+ struct path h_path;
20025+ struct inode *h_inode;
20026+};
20027+
20028+int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
20029+ struct au_icpup_args *a);
20030+
42b5c33a
AM
20031+int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
20032+ struct path *h_path, int locked);
1facf9fc 20033+
4a4d8108
AM
20034+/* i_op_add.c */
20035+int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
20036+ struct dentry *h_parent, int isdir);
0b2a12c6
JR
20037+int aufs_mknod(struct user_namespace *userns, struct inode *dir,
20038+ struct dentry *dentry, umode_t mode, dev_t dev);
20039+int aufs_symlink(struct user_namespace *userns, struct inode *dir,
20040+ struct dentry *dentry, const char *symname);
20041+int aufs_create(struct user_namespace *userns, struct inode *dir,
20042+ struct dentry *dentry, umode_t mode, bool want_excl);
b912730e
AM
20043+struct vfsub_aopen_args;
20044+int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
20045+ struct vfsub_aopen_args *args);
0b2a12c6 20046+int aufs_tmpfile(struct user_namespace *userns, struct inode *dir,
2255d0fe 20047+ struct file *file, umode_t mode);
4a4d8108
AM
20048+int aufs_link(struct dentry *src_dentry, struct inode *dir,
20049+ struct dentry *dentry);
0b2a12c6
JR
20050+int aufs_mkdir(struct user_namespace *userns, struct inode *dir,
20051+ struct dentry *dentry, umode_t mode);
1facf9fc 20052+
4a4d8108
AM
20053+/* i_op_del.c */
20054+int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
20055+int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
20056+ struct dentry *h_parent, int isdir);
20057+int aufs_unlink(struct inode *dir, struct dentry *dentry);
20058+int aufs_rmdir(struct inode *dir, struct dentry *dentry);
1308ab2a 20059+
4a4d8108
AM
20060+/* i_op_ren.c */
20061+int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
0b2a12c6
JR
20062+int aufs_rename(struct user_namespace *userns,
20063+ struct inode *_src_dir, struct dentry *_src_dentry,
20064+ struct inode *_dst_dir, struct dentry *_dst_dentry,
20065+ unsigned int _flags);
1facf9fc 20066+
4a4d8108
AM
20067+/* iinfo.c */
20068+struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
20069+void au_hiput(struct au_hinode *hinode);
20070+void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
20071+ struct dentry *h_wh);
20072+unsigned int au_hi_flags(struct inode *inode, int isdir);
1308ab2a 20073+
4a4d8108
AM
20074+/* hinode flags */
20075+#define AuHi_XINO 1
20076+#define AuHi_HNOTIFY (1 << 1)
20077+#define au_ftest_hi(flags, name) ((flags) & AuHi_##name)
7f207e10
AM
20078+#define au_fset_hi(flags, name) \
20079+ do { (flags) |= AuHi_##name; } while (0)
20080+#define au_fclr_hi(flags, name) \
20081+ do { (flags) &= ~AuHi_##name; } while (0)
1facf9fc 20082+
4a4d8108
AM
20083+#ifndef CONFIG_AUFS_HNOTIFY
20084+#undef AuHi_HNOTIFY
20085+#define AuHi_HNOTIFY 0
20086+#endif
1facf9fc 20087+
4a4d8108
AM
20088+void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
20089+ struct inode *h_inode, unsigned int flags);
1facf9fc 20090+
537831f9 20091+void au_update_iigen(struct inode *inode, int half);
4a4d8108 20092+void au_update_ibrange(struct inode *inode, int do_put_zero);
1facf9fc 20093+
4a4d8108 20094+void au_icntnr_init_once(void *_c);
5afbbe0d 20095+void au_hinode_init(struct au_hinode *hinode);
4a4d8108
AM
20096+int au_iinfo_init(struct inode *inode);
20097+void au_iinfo_fin(struct inode *inode);
e2f27e51 20098+int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
1308ab2a 20099+
e49829fe 20100+#ifdef CONFIG_PROC_FS
4a4d8108 20101+/* plink.c */
e49829fe 20102+int au_plink_maint(struct super_block *sb, int flags);
7e9cd9fe 20103+struct au_sbinfo;
e49829fe
JR
20104+void au_plink_maint_leave(struct au_sbinfo *sbinfo);
20105+int au_plink_maint_enter(struct super_block *sb);
4a4d8108
AM
20106+#ifdef CONFIG_AUFS_DEBUG
20107+void au_plink_list(struct super_block *sb);
20108+#else
20109+AuStubVoid(au_plink_list, struct super_block *sb)
20110+#endif
20111+int au_plink_test(struct inode *inode);
20112+struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
20113+void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
20114+ struct dentry *h_dentry);
e49829fe
JR
20115+void au_plink_put(struct super_block *sb, int verbose);
20116+void au_plink_clean(struct super_block *sb, int verbose);
4a4d8108 20117+void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
e49829fe
JR
20118+#else
20119+AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
20120+AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
20121+AuStubInt0(au_plink_maint_enter, struct super_block *sb);
20122+AuStubVoid(au_plink_list, struct super_block *sb);
20123+AuStubInt0(au_plink_test, struct inode *inode);
20124+AuStub(struct dentry *, au_plink_lkup, return NULL,
20125+ struct inode *inode, aufs_bindex_t bindex);
20126+AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
20127+ struct dentry *h_dentry);
20128+AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
20129+AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
20130+AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
20131+#endif /* CONFIG_PROC_FS */
1facf9fc 20132+
c1595e42
JR
20133+#ifdef CONFIG_AUFS_XATTR
20134+/* xattr.c */
0b2a12c6 20135+int au_cpup_xattr(struct path *h_dst, struct path *h_src, int ignore_flags,
7e9cd9fe 20136+ unsigned int verbose);
c1595e42 20137+ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
f2c43d5f 20138+void au_xattr_init(struct super_block *sb);
c1595e42 20139+#else
0b2a12c6 20140+AuStubInt0(au_cpup_xattr, struct path *h_dst, struct path *h_src,
7e9cd9fe 20141+ int ignore_flags, unsigned int verbose);
f2c43d5f 20142+AuStubVoid(au_xattr_init, struct super_block *sb);
c1595e42
JR
20143+#endif
20144+
20145+#ifdef CONFIG_FS_POSIX_ACL
758e9dad 20146+struct posix_acl *aufs_get_acl(struct inode *inode, int type, bool rcu);
0b2a12c6
JR
20147+int aufs_set_acl(struct user_namespace *userns, struct inode *inode,
20148+ struct posix_acl *acl, int type);
c1595e42
JR
20149+#endif
20150+
20151+#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
20152+enum {
20153+ AU_XATTR_SET,
c1595e42
JR
20154+ AU_ACL_SET
20155+};
20156+
f2c43d5f 20157+struct au_sxattr {
c1595e42
JR
20158+ int type;
20159+ union {
20160+ struct {
20161+ const char *name;
2255d0fe 20162+ const void *value;
c1595e42
JR
20163+ size_t size;
20164+ int flags;
20165+ } set;
20166+ struct {
c1595e42
JR
20167+ struct posix_acl *acl;
20168+ int type;
20169+ } acl_set;
20170+ } u;
20171+};
f2c43d5f
AM
20172+ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
20173+ struct au_sxattr *arg);
c1595e42
JR
20174+#endif
20175+
4a4d8108 20176+/* ---------------------------------------------------------------------- */
1308ab2a 20177+
4a4d8108
AM
20178+/* lock subclass for iinfo */
20179+enum {
20180+ AuLsc_II_CHILD, /* child first */
20181+ AuLsc_II_CHILD2, /* rename(2), link(2), and cpup at hnotify */
20182+ AuLsc_II_CHILD3, /* copyup dirs */
20183+ AuLsc_II_PARENT, /* see AuLsc_I_PARENT in vfsub.h */
20184+ AuLsc_II_PARENT2,
20185+ AuLsc_II_PARENT3, /* copyup dirs */
20186+ AuLsc_II_NEW_CHILD
20187+};
1308ab2a 20188+
1facf9fc 20189+/*
4a4d8108
AM
20190+ * ii_read_lock_child, ii_write_lock_child,
20191+ * ii_read_lock_child2, ii_write_lock_child2,
20192+ * ii_read_lock_child3, ii_write_lock_child3,
20193+ * ii_read_lock_parent, ii_write_lock_parent,
20194+ * ii_read_lock_parent2, ii_write_lock_parent2,
20195+ * ii_read_lock_parent3, ii_write_lock_parent3,
20196+ * ii_read_lock_new_child, ii_write_lock_new_child,
1facf9fc 20197+ */
4a4d8108
AM
20198+#define AuReadLockFunc(name, lsc) \
20199+static inline void ii_read_lock_##name(struct inode *i) \
20200+{ \
20201+ au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
20202+}
20203+
20204+#define AuWriteLockFunc(name, lsc) \
20205+static inline void ii_write_lock_##name(struct inode *i) \
20206+{ \
20207+ au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
20208+}
20209+
20210+#define AuRWLockFuncs(name, lsc) \
20211+ AuReadLockFunc(name, lsc) \
20212+ AuWriteLockFunc(name, lsc)
20213+
20214+AuRWLockFuncs(child, CHILD);
20215+AuRWLockFuncs(child2, CHILD2);
20216+AuRWLockFuncs(child3, CHILD3);
20217+AuRWLockFuncs(parent, PARENT);
20218+AuRWLockFuncs(parent2, PARENT2);
20219+AuRWLockFuncs(parent3, PARENT3);
20220+AuRWLockFuncs(new_child, NEW_CHILD);
20221+
20222+#undef AuReadLockFunc
20223+#undef AuWriteLockFunc
20224+#undef AuRWLockFuncs
1facf9fc 20225+
8b6a4947
AM
20226+#define ii_read_unlock(i) au_rw_read_unlock(&au_ii(i)->ii_rwsem)
20227+#define ii_write_unlock(i) au_rw_write_unlock(&au_ii(i)->ii_rwsem)
20228+#define ii_downgrade_lock(i) au_rw_dgrade_lock(&au_ii(i)->ii_rwsem)
1facf9fc 20229+
4a4d8108
AM
20230+#define IiMustNoWaiters(i) AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
20231+#define IiMustAnyLock(i) AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
20232+#define IiMustWriteLock(i) AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
1facf9fc 20233+
4a4d8108 20234+/* ---------------------------------------------------------------------- */
1308ab2a 20235+
027c5e7a
AM
20236+static inline void au_icntnr_init(struct au_icntnr *c)
20237+{
20238+#ifdef CONFIG_AUFS_DEBUG
20239+ c->vfs_inode.i_mode = 0;
20240+#endif
20241+}
20242+
cfc41e69 20243+static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
4a4d8108 20244+{
537831f9
AM
20245+ unsigned int gen;
20246+ struct au_iinfo *iinfo;
be52b249 20247+ struct au_iigen *iigen;
537831f9
AM
20248+
20249+ iinfo = au_ii(inode);
be52b249
AM
20250+ iigen = &iinfo->ii_generation;
20251+ spin_lock(&iigen->ig_spin);
cfc41e69
AM
20252+ if (igflags)
20253+ *igflags = iigen->ig_flags;
be52b249
AM
20254+ gen = iigen->ig_generation;
20255+ spin_unlock(&iigen->ig_spin);
537831f9
AM
20256+
20257+ return gen;
4a4d8108 20258+}
1308ab2a 20259+
4a4d8108
AM
20260+/* tiny test for inode number */
20261+/* tmpfs generation is too rough */
20262+static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
20263+{
20264+ struct au_iinfo *iinfo;
1308ab2a 20265+
4a4d8108
AM
20266+ iinfo = au_ii(inode);
20267+ AuRwMustAnyLock(&iinfo->ii_rwsem);
20268+ return !(iinfo->ii_hsb1 == h_inode->i_sb
20269+ && iinfo->ii_higen == h_inode->i_generation);
20270+}
1308ab2a 20271+
4a4d8108
AM
20272+static inline void au_iigen_dec(struct inode *inode)
20273+{
537831f9 20274+ struct au_iinfo *iinfo;
be52b249 20275+ struct au_iigen *iigen;
537831f9
AM
20276+
20277+ iinfo = au_ii(inode);
be52b249
AM
20278+ iigen = &iinfo->ii_generation;
20279+ spin_lock(&iigen->ig_spin);
20280+ iigen->ig_generation--;
20281+ spin_unlock(&iigen->ig_spin);
027c5e7a
AM
20282+}
20283+
20284+static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
20285+{
20286+ int err;
20287+
20288+ err = 0;
537831f9 20289+ if (unlikely(inode && au_iigen(inode, NULL) != sigen))
027c5e7a
AM
20290+ err = -EIO;
20291+
20292+ return err;
4a4d8108 20293+}
1308ab2a 20294+
4a4d8108 20295+/* ---------------------------------------------------------------------- */
1308ab2a 20296+
5afbbe0d
AM
20297+static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
20298+ aufs_bindex_t bindex)
20299+{
20300+ return iinfo->ii_hinode + bindex;
20301+}
20302+
20303+static inline int au_is_bad_inode(struct inode *inode)
20304+{
20305+ return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
20306+}
20307+
4a4d8108
AM
20308+static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
20309+ aufs_bindex_t bindex)
20310+{
20311+ IiMustAnyLock(inode);
5afbbe0d 20312+ return au_hinode(au_ii(inode), bindex)->hi_id;
4a4d8108 20313+}
1308ab2a 20314+
5afbbe0d 20315+static inline aufs_bindex_t au_ibtop(struct inode *inode)
4a4d8108
AM
20316+{
20317+ IiMustAnyLock(inode);
5afbbe0d 20318+ return au_ii(inode)->ii_btop;
4a4d8108 20319+}
1308ab2a 20320+
5afbbe0d 20321+static inline aufs_bindex_t au_ibbot(struct inode *inode)
4a4d8108
AM
20322+{
20323+ IiMustAnyLock(inode);
5afbbe0d 20324+ return au_ii(inode)->ii_bbot;
4a4d8108 20325+}
1308ab2a 20326+
4a4d8108
AM
20327+static inline struct au_vdir *au_ivdir(struct inode *inode)
20328+{
20329+ IiMustAnyLock(inode);
20330+ return au_ii(inode)->ii_vdir;
20331+}
1308ab2a 20332+
4a4d8108
AM
20333+static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
20334+{
20335+ IiMustAnyLock(inode);
5afbbe0d 20336+ return au_hinode(au_ii(inode), bindex)->hi_whdentry;
4a4d8108 20337+}
1308ab2a 20338+
5afbbe0d 20339+static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 20340+{
4a4d8108 20341+ IiMustWriteLock(inode);
5afbbe0d 20342+ au_ii(inode)->ii_btop = bindex;
4a4d8108 20343+}
1308ab2a 20344+
5afbbe0d 20345+static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
4a4d8108
AM
20346+{
20347+ IiMustWriteLock(inode);
5afbbe0d 20348+ au_ii(inode)->ii_bbot = bindex;
1308ab2a 20349+}
20350+
4a4d8108
AM
20351+static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
20352+{
20353+ IiMustWriteLock(inode);
20354+ au_ii(inode)->ii_vdir = vdir;
20355+}
1facf9fc 20356+
4a4d8108 20357+static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 20358+{
4a4d8108 20359+ IiMustAnyLock(inode);
5afbbe0d 20360+ return au_hinode(au_ii(inode), bindex);
4a4d8108 20361+}
dece6358 20362+
4a4d8108 20363+/* ---------------------------------------------------------------------- */
1facf9fc 20364+
4a4d8108
AM
20365+static inline struct dentry *au_pinned_parent(struct au_pin *pin)
20366+{
20367+ if (pin)
20368+ return pin->parent;
20369+ return NULL;
1facf9fc 20370+}
20371+
4a4d8108 20372+static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
1facf9fc 20373+{
4a4d8108
AM
20374+ if (pin && pin->hdir)
20375+ return pin->hdir->hi_inode;
20376+ return NULL;
1308ab2a 20377+}
1facf9fc 20378+
4a4d8108
AM
20379+static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
20380+{
20381+ if (pin)
20382+ return pin->hdir;
20383+ return NULL;
20384+}
1facf9fc 20385+
4a4d8108 20386+static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
1308ab2a 20387+{
4a4d8108
AM
20388+ if (pin)
20389+ pin->dentry = dentry;
20390+}
1308ab2a 20391+
4a4d8108
AM
20392+static inline void au_pin_set_parent_lflag(struct au_pin *pin,
20393+ unsigned char lflag)
20394+{
20395+ if (pin) {
7f207e10 20396+ if (lflag)
4a4d8108 20397+ au_fset_pin(pin->flags, DI_LOCKED);
7f207e10 20398+ else
4a4d8108 20399+ au_fclr_pin(pin->flags, DI_LOCKED);
1308ab2a 20400+ }
4a4d8108
AM
20401+}
20402+
7e9cd9fe 20403+#if 0 /* reserved */
4a4d8108
AM
20404+static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
20405+{
20406+ if (pin) {
20407+ dput(pin->parent);
20408+ pin->parent = dget(parent);
1facf9fc 20409+ }
4a4d8108 20410+}
7e9cd9fe 20411+#endif
1facf9fc 20412+
4a4d8108
AM
20413+/* ---------------------------------------------------------------------- */
20414+
027c5e7a 20415+struct au_branch;
4a4d8108
AM
20416+#ifdef CONFIG_AUFS_HNOTIFY
20417+struct au_hnotify_op {
20418+ void (*ctl)(struct au_hinode *hinode, int do_set);
027c5e7a 20419+ int (*alloc)(struct au_hinode *hinode);
7eafdf33
AM
20420+
20421+ /*
42a736d3 20422+ * if it returns true, the caller should free hinode->hi_notify,
7eafdf33
AM
20423+ * otherwise ->free() frees it.
20424+ */
20425+ int (*free)(struct au_hinode *hinode,
20426+ struct au_hnotify *hn) __must_check;
4a4d8108
AM
20427+
20428+ void (*fin)(void);
20429+ int (*init)(void);
027c5e7a
AM
20430+
20431+ int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
20432+ void (*fin_br)(struct au_branch *br);
20433+ int (*init_br)(struct au_branch *br, int perm);
4a4d8108
AM
20434+};
20435+
20436+/* hnotify.c */
027c5e7a 20437+int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
4a4d8108
AM
20438+void au_hn_free(struct au_hinode *hinode);
20439+void au_hn_ctl(struct au_hinode *hinode, int do_set);
20440+void au_hn_reset(struct inode *inode, unsigned int flags);
20441+int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
fbc438ed 20442+ const struct qstr *h_child_qstr, struct inode *h_child_inode);
027c5e7a
AM
20443+int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
20444+int au_hnotify_init_br(struct au_branch *br, int perm);
20445+void au_hnotify_fin_br(struct au_branch *br);
4a4d8108
AM
20446+int __init au_hnotify_init(void);
20447+void au_hnotify_fin(void);
20448+
7f207e10 20449+/* hfsnotify.c */
4a4d8108
AM
20450+extern const struct au_hnotify_op au_hnotify_op;
20451+
20452+static inline
20453+void au_hn_init(struct au_hinode *hinode)
20454+{
20455+ hinode->hi_notify = NULL;
1308ab2a 20456+}
20457+
53392da6
AM
20458+static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
20459+{
20460+ return hinode->hi_notify;
20461+}
20462+
4a4d8108 20463+#else
c1595e42
JR
20464+AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
20465+ struct au_hinode *hinode __maybe_unused,
20466+ struct inode *inode __maybe_unused)
20467+AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
4a4d8108
AM
20468+AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
20469+AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
20470+ int do_set __maybe_unused)
20471+AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
20472+ unsigned int flags __maybe_unused)
027c5e7a
AM
20473+AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
20474+ struct au_branch *br __maybe_unused,
20475+ int perm __maybe_unused)
20476+AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
20477+ int perm __maybe_unused)
20478+AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
4a4d8108
AM
20479+AuStubInt0(__init au_hnotify_init, void)
20480+AuStubVoid(au_hnotify_fin, void)
20481+AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
20482+#endif /* CONFIG_AUFS_HNOTIFY */
20483+
20484+static inline void au_hn_suspend(struct au_hinode *hdir)
20485+{
20486+ au_hn_ctl(hdir, /*do_set*/0);
1308ab2a 20487+}
20488+
4a4d8108 20489+static inline void au_hn_resume(struct au_hinode *hdir)
1308ab2a 20490+{
4a4d8108
AM
20491+ au_hn_ctl(hdir, /*do_set*/1);
20492+}
1308ab2a 20493+
5afbbe0d 20494+static inline void au_hn_inode_lock(struct au_hinode *hdir)
4a4d8108 20495+{
febd17d6 20496+ inode_lock(hdir->hi_inode);
4a4d8108
AM
20497+ au_hn_suspend(hdir);
20498+}
dece6358 20499+
5afbbe0d 20500+static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
4a4d8108
AM
20501+ unsigned int sc __maybe_unused)
20502+{
febd17d6 20503+ inode_lock_nested(hdir->hi_inode, sc);
4a4d8108 20504+ au_hn_suspend(hdir);
1facf9fc 20505+}
1facf9fc 20506+
8b6a4947
AM
20507+#if 0 /* unused */
20508+#include "vfsub.h"
3c1bdaff
AM
20509+static inline void au_hn_inode_lock_shared_nested(struct au_hinode *hdir,
20510+ unsigned int sc)
20511+{
be118d29 20512+ inode_lock_shared_nested(hdir->hi_inode, sc);
3c1bdaff
AM
20513+ au_hn_suspend(hdir);
20514+}
8b6a4947 20515+#endif
3c1bdaff 20516+
5afbbe0d 20517+static inline void au_hn_inode_unlock(struct au_hinode *hdir)
4a4d8108
AM
20518+{
20519+ au_hn_resume(hdir);
febd17d6 20520+ inode_unlock(hdir->hi_inode);
4a4d8108
AM
20521+}
20522+
20523+#endif /* __KERNEL__ */
20524+#endif /* __AUFS_INODE_H__ */
7f207e10 20525diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
eca34b5c 20526--- /usr/share/empty/fs/aufs/ioctl.c 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 20527+++ linux/fs/aufs/ioctl.c 2022-11-05 23:02:18.965889284 +0100
062440b3 20528@@ -0,0 +1,220 @@
cd7a4cd9 20529+// SPDX-License-Identifier: GPL-2.0
4a4d8108 20530+/*
0dcfbb52 20531+ * Copyright (C) 2005-2022 Junjiro R. Okajima
4a4d8108 20532+ *
0dcfbb52 20533+ * This program is free software; you can redistribute it and/or modify
4a4d8108
AM
20534+ * it under the terms of the GNU General Public License as published by
20535+ * the Free Software Foundation; either version 2 of the License, or
20536+ * (at your option) any later version.
20537+ *
20538+ * This program is distributed in the hope that it will be useful,
20539+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20540+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20541+ * GNU General Public License for more details.
20542+ *
20543+ * You should have received a copy of the GNU General Public License
523b37e3 20544+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
20545+ */
20546+
20547+/*
20548+ * ioctl
20549+ * plink-management and readdir in userspace.
20550+ * assist the pathconf(3) wrapper library.
c2b27bf2 20551+ * move-down
076b876e 20552+ * File-based Hierarchical Storage Management.
4a4d8108
AM
20553+ */
20554+
c2b27bf2
AM
20555+#include <linux/compat.h>
20556+#include <linux/file.h>
4a4d8108
AM
20557+#include "aufs.h"
20558+
1e00d052 20559+static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
4a4d8108
AM
20560+{
20561+ int err, fd;
5afbbe0d 20562+ aufs_bindex_t wbi, bindex, bbot;
4a4d8108
AM
20563+ struct file *h_file;
20564+ struct super_block *sb;
20565+ struct dentry *root;
1e00d052
AM
20566+ struct au_branch *br;
20567+ struct aufs_wbr_fd wbrfd = {
20568+ .oflags = au_dir_roflags,
20569+ .brid = -1
20570+ };
20571+ const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
20572+ | O_NOATIME | O_CLOEXEC;
4a4d8108 20573+
1e00d052
AM
20574+ AuDebugOn(wbrfd.oflags & ~valid);
20575+
20576+ if (arg) {
20577+ err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
20578+ if (unlikely(err)) {
20579+ err = -EFAULT;
20580+ goto out;
20581+ }
20582+
20583+ err = -EINVAL;
20584+ AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
20585+ wbrfd.oflags |= au_dir_roflags;
20586+ AuDbg("0%o\n", wbrfd.oflags);
20587+ if (unlikely(wbrfd.oflags & ~valid))
20588+ goto out;
20589+ }
20590+
2000de60 20591+ fd = get_unused_fd_flags(0);
1e00d052
AM
20592+ err = fd;
20593+ if (unlikely(fd < 0))
4a4d8108 20594+ goto out;
4a4d8108 20595+
1e00d052 20596+ h_file = ERR_PTR(-EINVAL);
4a4d8108 20597+ wbi = 0;
1e00d052 20598+ br = NULL;
4a4d8108
AM
20599+ sb = path->dentry->d_sb;
20600+ root = sb->s_root;
20601+ aufs_read_lock(root, AuLock_IR);
5afbbe0d 20602+ bbot = au_sbbot(sb);
1e00d052
AM
20603+ if (wbrfd.brid >= 0) {
20604+ wbi = au_br_index(sb, wbrfd.brid);
5afbbe0d 20605+ if (unlikely(wbi < 0 || wbi > bbot))
1e00d052
AM
20606+ goto out_unlock;
20607+ }
20608+
20609+ h_file = ERR_PTR(-ENOENT);
20610+ br = au_sbr(sb, wbi);
20611+ if (!au_br_writable(br->br_perm)) {
20612+ if (arg)
20613+ goto out_unlock;
20614+
20615+ bindex = wbi + 1;
20616+ wbi = -1;
5afbbe0d 20617+ for (; bindex <= bbot; bindex++) {
1e00d052
AM
20618+ br = au_sbr(sb, bindex);
20619+ if (au_br_writable(br->br_perm)) {
4a4d8108 20620+ wbi = bindex;
1e00d052 20621+ br = au_sbr(sb, wbi);
4a4d8108
AM
20622+ break;
20623+ }
20624+ }
4a4d8108
AM
20625+ }
20626+ AuDbg("wbi %d\n", wbi);
1e00d052 20627+ if (wbi >= 0)
392086de
AM
20628+ h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
20629+ /*force_wr*/0);
1e00d052
AM
20630+
20631+out_unlock:
4a4d8108
AM
20632+ aufs_read_unlock(root, AuLock_IR);
20633+ err = PTR_ERR(h_file);
20634+ if (IS_ERR(h_file))
20635+ goto out_fd;
20636+
acd2b654 20637+ au_lcnt_dec(&br->br_nfiles); /* cf. au_h_open() */
4a4d8108
AM
20638+ fd_install(fd, h_file);
20639+ err = fd;
20640+ goto out; /* success */
20641+
4f0767ce 20642+out_fd:
4a4d8108 20643+ put_unused_fd(fd);
4f0767ce 20644+out:
1e00d052 20645+ AuTraceErr(err);
4a4d8108
AM
20646+ return err;
20647+}
20648+
20649+/* ---------------------------------------------------------------------- */
20650+
20651+long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
20652+{
20653+ long err;
c1595e42 20654+ struct dentry *dentry;
4a4d8108
AM
20655+
20656+ switch (cmd) {
4a4d8108
AM
20657+ case AUFS_CTL_RDU:
20658+ case AUFS_CTL_RDU_INO:
20659+ err = au_rdu_ioctl(file, cmd, arg);
20660+ break;
20661+
20662+ case AUFS_CTL_WBR_FD:
1e00d052 20663+ err = au_wbr_fd(&file->f_path, (void __user *)arg);
4a4d8108
AM
20664+ break;
20665+
027c5e7a
AM
20666+ case AUFS_CTL_IBUSY:
20667+ err = au_ibusy_ioctl(file, arg);
20668+ break;
20669+
076b876e
AM
20670+ case AUFS_CTL_BRINFO:
20671+ err = au_brinfo_ioctl(file, arg);
20672+ break;
20673+
20674+ case AUFS_CTL_FHSM_FD:
2000de60 20675+ dentry = file->f_path.dentry;
c1595e42
JR
20676+ if (IS_ROOT(dentry))
20677+ err = au_fhsm_fd(dentry->d_sb, arg);
20678+ else
20679+ err = -ENOTTY;
076b876e
AM
20680+ break;
20681+
4a4d8108
AM
20682+ default:
20683+ /* do not call the lower */
20684+ AuDbg("0x%x\n", cmd);
20685+ err = -ENOTTY;
20686+ }
20687+
20688+ AuTraceErr(err);
20689+ return err;
20690+}
20691+
20692+long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
20693+{
20694+ long err;
20695+
20696+ switch (cmd) {
c2b27bf2 20697+ case AUFS_CTL_MVDOWN:
2000de60 20698+ err = au_mvdown(file->f_path.dentry, (void __user *)arg);
c2b27bf2
AM
20699+ break;
20700+
4a4d8108 20701+ case AUFS_CTL_WBR_FD:
1e00d052 20702+ err = au_wbr_fd(&file->f_path, (void __user *)arg);
4a4d8108
AM
20703+ break;
20704+
20705+ default:
20706+ /* do not call the lower */
20707+ AuDbg("0x%x\n", cmd);
20708+ err = -ENOTTY;
20709+ }
20710+
20711+ AuTraceErr(err);
20712+ return err;
20713+}
b752ccd1
AM
20714+
20715+#ifdef CONFIG_COMPAT
20716+long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
20717+ unsigned long arg)
20718+{
20719+ long err;
20720+
20721+ switch (cmd) {
20722+ case AUFS_CTL_RDU:
20723+ case AUFS_CTL_RDU_INO:
20724+ err = au_rdu_compat_ioctl(file, cmd, arg);
20725+ break;
20726+
027c5e7a
AM
20727+ case AUFS_CTL_IBUSY:
20728+ err = au_ibusy_compat_ioctl(file, arg);
20729+ break;
20730+
076b876e
AM
20731+ case AUFS_CTL_BRINFO:
20732+ err = au_brinfo_compat_ioctl(file, arg);
20733+ break;
20734+
b752ccd1
AM
20735+ default:
20736+ err = aufs_ioctl_dir(file, cmd, arg);
20737+ }
20738+
20739+ AuTraceErr(err);
20740+ return err;
20741+}
20742+
b752ccd1
AM
20743+long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
20744+ unsigned long arg)
20745+{
20746+ return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
20747+}
20748+#endif
7f207e10 20749diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
eca34b5c 20750--- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
2255d0fe
AM
20751+++ linux/fs/aufs/i_op_add.c 2022-12-17 09:21:34.799855195 +0100
20752@@ -0,0 +1,972 @@
cd7a4cd9 20753+// SPDX-License-Identifier: GPL-2.0
4a4d8108 20754+/*
0dcfbb52 20755+ * Copyright (C) 2005-2022 Junjiro R. Okajima
4a4d8108 20756+ *
0dcfbb52 20757+ * This program is free software; you can redistribute it and/or modify
4a4d8108
AM
20758+ * it under the terms of the GNU General Public License as published by
20759+ * the Free Software Foundation; either version 2 of the License, or
20760+ * (at your option) any later version.
20761+ *
20762+ * This program is distributed in the hope that it will be useful,
20763+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20764+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20765+ * GNU General Public License for more details.
20766+ *
20767+ * You should have received a copy of the GNU General Public License
523b37e3 20768+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
20769+ */
20770+
20771+/*
20772+ * inode operations (add entry)
20773+ */
20774+
eca801bf 20775+#include <linux/iversion.h>
4a4d8108
AM
20776+#include "aufs.h"
20777+
20778+/*
20779+ * final procedure of adding a new entry, except link(2).
20780+ * remove whiteout, instantiate, copyup the parent dir's times and size
20781+ * and update version.
20782+ * if it failed, re-create the removed whiteout.
20783+ */
20784+static int epilog(struct inode *dir, aufs_bindex_t bindex,
20785+ struct dentry *wh_dentry, struct dentry *dentry)
20786+{
20787+ int err, rerr;
20788+ aufs_bindex_t bwh;
20789+ struct path h_path;
076b876e 20790+ struct super_block *sb;
4a4d8108
AM
20791+ struct inode *inode, *h_dir;
20792+ struct dentry *wh;
20793+
20794+ bwh = -1;
076b876e 20795+ sb = dir->i_sb;
4a4d8108 20796+ if (wh_dentry) {
5527c038 20797+ h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
4a4d8108
AM
20798+ IMustLock(h_dir);
20799+ AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
20800+ bwh = au_dbwh(dentry);
20801+ h_path.dentry = wh_dentry;
076b876e 20802+ h_path.mnt = au_sbr_mnt(sb, bindex);
4a4d8108
AM
20803+ err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
20804+ dentry);
20805+ if (unlikely(err))
20806+ goto out;
20807+ }
20808+
20809+ inode = au_new_inode(dentry, /*must_new*/1);
20810+ if (!IS_ERR(inode)) {
20811+ d_instantiate(dentry, inode);
5527c038 20812+ dir = d_inode(dentry->d_parent); /* dir inode is locked */
4a4d8108 20813+ IMustLock(dir);
b912730e 20814+ au_dir_ts(dir, bindex);
be118d29 20815+ inode_inc_iversion(dir);
076b876e 20816+ au_fhsm_wrote(sb, bindex, /*force*/0);
4a4d8108
AM
20817+ return 0; /* success */
20818+ }
20819+
20820+ err = PTR_ERR(inode);
20821+ if (!wh_dentry)
20822+ goto out;
20823+
20824+ /* revert */
20825+ /* dir inode is locked */
20826+ wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
20827+ rerr = PTR_ERR(wh);
20828+ if (IS_ERR(wh)) {
523b37e3
AM
20829+ AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
20830+ dentry, err, rerr);
4a4d8108
AM
20831+ err = -EIO;
20832+ } else
20833+ dput(wh);
20834+
4f0767ce 20835+out:
4a4d8108
AM
20836+ return err;
20837+}
20838+
027c5e7a
AM
20839+static int au_d_may_add(struct dentry *dentry)
20840+{
20841+ int err;
20842+
20843+ err = 0;
20844+ if (unlikely(d_unhashed(dentry)))
20845+ err = -ENOENT;
5527c038 20846+ if (unlikely(d_really_is_positive(dentry)))
027c5e7a
AM
20847+ err = -EEXIST;
20848+ return err;
20849+}
20850+
4a4d8108
AM
20851+/*
20852+ * simple tests for the adding inode operations.
20853+ * following the checks in vfs, plus the parent-child relationship.
20854+ */
20855+int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
20856+ struct dentry *h_parent, int isdir)
20857+{
20858+ int err;
20859+ umode_t h_mode;
20860+ struct dentry *h_dentry;
20861+ struct inode *h_inode;
20862+
20863+ err = -ENAMETOOLONG;
20864+ if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20865+ goto out;
20866+
20867+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 20868+ if (d_really_is_negative(dentry)) {
4a4d8108 20869+ err = -EEXIST;
5527c038 20870+ if (unlikely(d_is_positive(h_dentry)))
4a4d8108
AM
20871+ goto out;
20872+ } else {
20873+ /* rename(2) case */
20874+ err = -EIO;
5527c038
JR
20875+ if (unlikely(d_is_negative(h_dentry)))
20876+ goto out;
20877+ h_inode = d_inode(h_dentry);
20878+ if (unlikely(!h_inode->i_nlink))
4a4d8108
AM
20879+ goto out;
20880+
20881+ h_mode = h_inode->i_mode;
20882+ if (!isdir) {
20883+ err = -EISDIR;
20884+ if (unlikely(S_ISDIR(h_mode)))
20885+ goto out;
20886+ } else if (unlikely(!S_ISDIR(h_mode))) {
20887+ err = -ENOTDIR;
20888+ goto out;
20889+ }
20890+ }
20891+
20892+ err = 0;
20893+ /* expected parent dir is locked */
20894+ if (unlikely(h_parent != h_dentry->d_parent))
20895+ err = -EIO;
20896+
4f0767ce 20897+out:
4a4d8108
AM
20898+ AuTraceErr(err);
20899+ return err;
20900+}
20901+
20902+/*
20903+ * initial procedure of adding a new entry.
20904+ * prepare writable branch and the parent dir, lock it,
20905+ * and lookup whiteout for the new entry.
20906+ */
20907+static struct dentry*
20908+lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
20909+ struct dentry *src_dentry, struct au_pin *pin,
20910+ struct au_wr_dir_args *wr_dir_args)
20911+{
20912+ struct dentry *wh_dentry, *h_parent;
20913+ struct super_block *sb;
20914+ struct au_branch *br;
20915+ int err;
20916+ unsigned int udba;
20917+ aufs_bindex_t bcpup;
20918+
523b37e3 20919+ AuDbg("%pd\n", dentry);
4a4d8108
AM
20920+
20921+ err = au_wr_dir(dentry, src_dentry, wr_dir_args);
20922+ bcpup = err;
20923+ wh_dentry = ERR_PTR(err);
20924+ if (unlikely(err < 0))
20925+ goto out;
20926+
20927+ sb = dentry->d_sb;
20928+ udba = au_opt_udba(sb);
20929+ err = au_pin(pin, dentry, bcpup, udba,
20930+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20931+ wh_dentry = ERR_PTR(err);
20932+ if (unlikely(err))
20933+ goto out;
20934+
20935+ h_parent = au_pinned_h_parent(pin);
20936+ if (udba != AuOpt_UDBA_NONE
5afbbe0d 20937+ && au_dbtop(dentry) == bcpup)
4a4d8108
AM
20938+ err = au_may_add(dentry, bcpup, h_parent,
20939+ au_ftest_wrdir(wr_dir_args->flags, ISDIR));
20940+ else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20941+ err = -ENAMETOOLONG;
20942+ wh_dentry = ERR_PTR(err);
20943+ if (unlikely(err))
20944+ goto out_unpin;
20945+
20946+ br = au_sbr(sb, bcpup);
20947+ if (dt) {
20948+ struct path tmp = {
20949+ .dentry = h_parent,
86dc4139 20950+ .mnt = au_br_mnt(br)
4a4d8108
AM
20951+ };
20952+ au_dtime_store(dt, au_pinned_parent(pin), &tmp);
20953+ }
20954+
20955+ wh_dentry = NULL;
20956+ if (bcpup != au_dbwh(dentry))
20957+ goto out; /* success */
20958+
2000de60
JR
20959+ /*
20960+ * ENAMETOOLONG here means that if we allowed create such name, then it
20961+ * would not be able to removed in the future. So we don't allow such
20962+ * name here and we don't handle ENAMETOOLONG differently here.
20963+ */
4a4d8108
AM
20964+ wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
20965+
4f0767ce 20966+out_unpin:
4a4d8108
AM
20967+ if (IS_ERR(wh_dentry))
20968+ au_unpin(pin);
4f0767ce 20969+out:
4a4d8108
AM
20970+ return wh_dentry;
20971+}
20972+
20973+/* ---------------------------------------------------------------------- */
20974+
20975+enum { Mknod, Symlink, Creat };
20976+struct simple_arg {
20977+ int type;
20978+ union {
20979+ struct {
b912730e
AM
20980+ umode_t mode;
20981+ bool want_excl;
20982+ bool try_aopen;
20983+ struct vfsub_aopen_args *aopen;
4a4d8108
AM
20984+ } c;
20985+ struct {
20986+ const char *symname;
20987+ } s;
20988+ struct {
7eafdf33 20989+ umode_t mode;
4a4d8108
AM
20990+ dev_t dev;
20991+ } m;
20992+ } u;
20993+};
20994+
20995+static int add_simple(struct inode *dir, struct dentry *dentry,
20996+ struct simple_arg *arg)
20997+{
076b876e 20998+ int err, rerr;
5afbbe0d 20999+ aufs_bindex_t btop;
4a4d8108 21000+ unsigned char created;
b912730e
AM
21001+ const unsigned char try_aopen
21002+ = (arg->type == Creat && arg->u.c.try_aopen);
acd2b654 21003+ struct vfsub_aopen_args *aopen = arg->u.c.aopen;
4a4d8108
AM
21004+ struct dentry *wh_dentry, *parent;
21005+ struct inode *h_dir;
b912730e
AM
21006+ struct super_block *sb;
21007+ struct au_branch *br;
acd2b654 21008+ /* to reduce stack size */
c2b27bf2
AM
21009+ struct {
21010+ struct au_dtime dt;
21011+ struct au_pin pin;
21012+ struct path h_path;
21013+ struct au_wr_dir_args wr_dir_args;
21014+ } *a;
4a4d8108 21015+
523b37e3 21016+ AuDbg("%pd\n", dentry);
4a4d8108
AM
21017+ IMustLock(dir);
21018+
c2b27bf2
AM
21019+ err = -ENOMEM;
21020+ a = kmalloc(sizeof(*a), GFP_NOFS);
21021+ if (unlikely(!a))
21022+ goto out;
21023+ a->wr_dir_args.force_btgt = -1;
21024+ a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
21025+
4a4d8108 21026+ parent = dentry->d_parent; /* dir inode is locked */
b912730e
AM
21027+ if (!try_aopen) {
21028+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
21029+ if (unlikely(err))
21030+ goto out_free;
21031+ }
027c5e7a
AM
21032+ err = au_d_may_add(dentry);
21033+ if (unlikely(err))
21034+ goto out_unlock;
b912730e
AM
21035+ if (!try_aopen)
21036+ di_write_lock_parent(parent);
c2b27bf2
AM
21037+ wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
21038+ &a->pin, &a->wr_dir_args);
4a4d8108
AM
21039+ err = PTR_ERR(wh_dentry);
21040+ if (IS_ERR(wh_dentry))
027c5e7a 21041+ goto out_parent;
4a4d8108 21042+
5afbbe0d 21043+ btop = au_dbtop(dentry);
b912730e 21044+ sb = dentry->d_sb;
5afbbe0d
AM
21045+ br = au_sbr(sb, btop);
21046+ a->h_path.dentry = au_h_dptr(dentry, btop);
b912730e 21047+ a->h_path.mnt = au_br_mnt(br);
c2b27bf2 21048+ h_dir = au_pinned_h_dir(&a->pin);
4a4d8108
AM
21049+ switch (arg->type) {
21050+ case Creat:
acd2b654 21051+ if (!try_aopen || !h_dir->i_op->atomic_open) {
b912730e
AM
21052+ err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
21053+ arg->u.c.want_excl);
acd2b654
AM
21054+ created = !err;
21055+ if (!err && try_aopen)
21056+ aopen->file->f_mode |= FMODE_CREATED;
21057+ } else {
21058+ aopen->br = br;
21059+ err = vfsub_atomic_open(h_dir, a->h_path.dentry, aopen);
21060+ AuDbg("err %d\n", err);
21061+ AuDbgFile(aopen->file);
21062+ created = err >= 0
21063+ && !!(aopen->file->f_mode & FMODE_CREATED);
21064+ }
4a4d8108
AM
21065+ break;
21066+ case Symlink:
c2b27bf2 21067+ err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
acd2b654 21068+ created = !err;
4a4d8108
AM
21069+ break;
21070+ case Mknod:
c2b27bf2
AM
21071+ err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
21072+ arg->u.m.dev);
acd2b654 21073+ created = !err;
4a4d8108
AM
21074+ break;
21075+ default:
21076+ BUG();
21077+ }
acd2b654
AM
21078+ if (unlikely(err < 0))
21079+ goto out_unpin;
21080+
21081+ err = epilog(dir, btop, wh_dentry, dentry);
4a4d8108 21082+ if (!err)
acd2b654 21083+ goto out_unpin; /* success */
4a4d8108
AM
21084+
21085+ /* revert */
acd2b654 21086+ if (created /* && d_is_positive(a->h_path.dentry) */) {
523b37e3
AM
21087+ /* no delegation since it is just created */
21088+ rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
21089+ /*force*/0);
4a4d8108 21090+ if (rerr) {
523b37e3
AM
21091+ AuIOErr("%pd revert failure(%d, %d)\n",
21092+ dentry, err, rerr);
4a4d8108
AM
21093+ err = -EIO;
21094+ }
c2b27bf2 21095+ au_dtime_revert(&a->dt);
4a4d8108 21096+ }
acd2b654
AM
21097+ if (try_aopen && h_dir->i_op->atomic_open
21098+ && (aopen->file->f_mode & FMODE_OPENED))
21099+ /* aopen->file is still opened */
21100+ au_lcnt_dec(&aopen->br->br_nfiles);
4a4d8108 21101+
acd2b654 21102+out_unpin:
c2b27bf2 21103+ au_unpin(&a->pin);
4a4d8108 21104+ dput(wh_dentry);
027c5e7a 21105+out_parent:
b912730e
AM
21106+ if (!try_aopen)
21107+ di_write_unlock(parent);
027c5e7a 21108+out_unlock:
4a4d8108 21109+ if (unlikely(err)) {
5afbbe0d 21110+ au_update_dbtop(dentry);
4a4d8108
AM
21111+ d_drop(dentry);
21112+ }
b912730e
AM
21113+ if (!try_aopen)
21114+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2 21115+out_free:
9f237c51 21116+ au_kfree_rcu(a);
027c5e7a 21117+out:
4a4d8108
AM
21118+ return err;
21119+}
21120+
0b2a12c6
JR
21121+int aufs_mknod(struct user_namespace *userns, struct inode *dir,
21122+ struct dentry *dentry, umode_t mode, dev_t dev)
4a4d8108
AM
21123+{
21124+ struct simple_arg arg = {
21125+ .type = Mknod,
21126+ .u.m = {
21127+ .mode = mode,
21128+ .dev = dev
21129+ }
21130+ };
21131+ return add_simple(dir, dentry, &arg);
21132+}
21133+
0b2a12c6
JR
21134+int aufs_symlink(struct user_namespace *userns, struct inode *dir,
21135+ struct dentry *dentry, const char *symname)
4a4d8108
AM
21136+{
21137+ struct simple_arg arg = {
21138+ .type = Symlink,
21139+ .u.s.symname = symname
21140+ };
21141+ return add_simple(dir, dentry, &arg);
21142+}
21143+
0b2a12c6
JR
21144+int aufs_create(struct user_namespace *userns, struct inode *dir,
21145+ struct dentry *dentry, umode_t mode, bool want_excl)
4a4d8108
AM
21146+{
21147+ struct simple_arg arg = {
21148+ .type = Creat,
21149+ .u.c = {
b4510431
AM
21150+ .mode = mode,
21151+ .want_excl = want_excl
4a4d8108
AM
21152+ }
21153+ };
21154+ return add_simple(dir, dentry, &arg);
21155+}
21156+
b912730e
AM
21157+int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
21158+ struct vfsub_aopen_args *aopen_args)
21159+{
21160+ struct simple_arg arg = {
21161+ .type = Creat,
21162+ .u.c = {
21163+ .mode = aopen_args->create_mode,
21164+ .want_excl = aopen_args->open_flag & O_EXCL,
21165+ .try_aopen = true,
21166+ .aopen = aopen_args
21167+ }
21168+ };
21169+ return add_simple(dir, dentry, &arg);
21170+}
21171+
0b2a12c6 21172+int aufs_tmpfile(struct user_namespace *userns, struct inode *dir,
2255d0fe 21173+ struct file *file, umode_t mode)
38d290e6
JR
21174+{
21175+ int err;
21176+ aufs_bindex_t bindex;
2255d0fe 21177+ struct path h_ppath;
38d290e6 21178+ struct super_block *sb;
2255d0fe
AM
21179+ struct au_branch *br;
21180+ struct dentry *dentry, *parent, *h_parent, *h_dentry;
38d290e6
JR
21181+ struct inode *h_dir, *inode;
21182+ struct vfsmount *h_mnt;
0b2a12c6 21183+ struct user_namespace *h_userns;
2255d0fe 21184+ struct file *h_file;
38d290e6
JR
21185+ struct au_wr_dir_args wr_dir_args = {
21186+ .force_btgt = -1,
21187+ .flags = AuWrDir_TMPFILE
21188+ };
21189+
21190+ /* copy-up may happen */
febd17d6 21191+ inode_lock(dir);
38d290e6 21192+
2255d0fe 21193+ h_file = NULL;
38d290e6
JR
21194+ sb = dir->i_sb;
21195+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21196+ if (unlikely(err))
21197+ goto out;
21198+
2255d0fe 21199+ dentry = file->f_path.dentry;
38d290e6
JR
21200+ err = au_di_init(dentry);
21201+ if (unlikely(err))
21202+ goto out_si;
21203+
21204+ err = -EBUSY;
21205+ parent = d_find_any_alias(dir);
21206+ AuDebugOn(!parent);
21207+ di_write_lock_parent(parent);
5527c038 21208+ if (unlikely(d_inode(parent) != dir))
38d290e6
JR
21209+ goto out_parent;
21210+
21211+ err = au_digen_test(parent, au_sigen(sb));
21212+ if (unlikely(err))
21213+ goto out_parent;
21214+
5afbbe0d
AM
21215+ bindex = au_dbtop(parent);
21216+ au_set_dbtop(dentry, bindex);
21217+ au_set_dbbot(dentry, bindex);
38d290e6
JR
21218+ err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
21219+ bindex = err;
21220+ if (unlikely(err < 0))
21221+ goto out_parent;
21222+
21223+ err = -EOPNOTSUPP;
21224+ h_dir = au_h_iptr(dir, bindex);
21225+ if (unlikely(!h_dir->i_op->tmpfile))
21226+ goto out_parent;
21227+
2255d0fe
AM
21228+ br = au_sbr(sb, bindex);
21229+ h_mnt = au_br_mnt(br);
38d290e6
JR
21230+ err = vfsub_mnt_want_write(h_mnt);
21231+ if (unlikely(err))
21232+ goto out_parent;
21233+
0b2a12c6 21234+ h_userns = mnt_user_ns(h_mnt);
38d290e6 21235+ h_parent = au_h_dptr(parent, bindex);
2255d0fe
AM
21236+ h_ppath.mnt = h_mnt;
21237+ h_ppath.dentry = h_parent;
21238+ h_file = vfs_tmpfile_open(h_userns, &h_ppath, mode, /*open_flag*/0,
21239+ /*cred*/NULL);
21240+ if (IS_ERR(h_file)) {
21241+ err = PTR_ERR(h_file);
21242+ h_file = NULL;
38d290e6 21243+ goto out_mnt;
521ced18 21244+ }
38d290e6 21245+
2255d0fe 21246+ h_dentry = h_file->f_path.dentry;
5afbbe0d
AM
21247+ au_set_dbtop(dentry, bindex);
21248+ au_set_dbbot(dentry, bindex);
38d290e6
JR
21249+ au_set_h_dptr(dentry, bindex, dget(h_dentry));
21250+ inode = au_new_inode(dentry, /*must_new*/1);
21251+ if (IS_ERR(inode)) {
21252+ err = PTR_ERR(inode);
21253+ au_set_h_dptr(dentry, bindex, NULL);
5afbbe0d
AM
21254+ au_set_dbtop(dentry, -1);
21255+ au_set_dbbot(dentry, -1);
2255d0fe 21256+ goto out_h_file;
38d290e6 21257+ }
521ced18 21258+
2255d0fe
AM
21259+ if (!inode->i_nlink)
21260+ set_nlink(inode, 1);
21261+ d_tmpfile(file, inode);
21262+ au_di(dentry)->di_tmpfile = 1;
21263+ get_file(h_file);
21264+ au_di(dentry)->di_htmpfile = h_file;
21265+
21266+ /* update without i_mutex */
21267+ if (au_ibtop(dir) == au_dbtop(dentry))
21268+ au_cpup_attr_timesizes(dir);
21269+
21270+out_h_file:
21271+ fput(h_file);
38d290e6
JR
21272+out_mnt:
21273+ vfsub_mnt_drop_write(h_mnt);
21274+out_parent:
21275+ di_write_unlock(parent);
21276+ dput(parent);
21277+ di_write_unlock(dentry);
2255d0fe
AM
21278+ if (!err)
21279+ goto out_si;
21280+ if (h_file)
21281+ fput(h_file);
21282+ au_di(dentry)->di_htmpfile = NULL;
21283+ au_di_fin(dentry);
21284+ dentry->d_fsdata = NULL;
38d290e6
JR
21285+out_si:
21286+ si_read_unlock(sb);
2255d0fe
AM
21287+ if (!err && h_file) {
21288+ /* finally... */
21289+ err = finish_open_simple(file, err);
21290+ if (!err)
21291+ au_lcnt_inc(&br->br_nfiles);
21292+ else {
21293+ fput(h_file);
21294+ au_di(dentry)->di_htmpfile = NULL;
21295+ au_di_fin(dentry);
21296+ dentry->d_fsdata = NULL;
21297+ }
21298+ }
38d290e6 21299+out:
febd17d6 21300+ inode_unlock(dir);
2255d0fe 21301+ AuTraceErr(err);
38d290e6
JR
21302+ return err;
21303+}
21304+
4a4d8108
AM
21305+/* ---------------------------------------------------------------------- */
21306+
21307+struct au_link_args {
21308+ aufs_bindex_t bdst, bsrc;
21309+ struct au_pin pin;
21310+ struct path h_path;
21311+ struct dentry *src_parent, *parent;
21312+};
21313+
21314+static int au_cpup_before_link(struct dentry *src_dentry,
21315+ struct au_link_args *a)
21316+{
21317+ int err;
21318+ struct dentry *h_src_dentry;
c2b27bf2
AM
21319+ struct au_cp_generic cpg = {
21320+ .dentry = src_dentry,
21321+ .bdst = a->bdst,
21322+ .bsrc = a->bsrc,
21323+ .len = -1,
21324+ .pin = &a->pin,
21325+ .flags = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
21326+ };
4a4d8108
AM
21327+
21328+ di_read_lock_parent(a->src_parent, AuLock_IR);
21329+ err = au_test_and_cpup_dirs(src_dentry, a->bdst);
21330+ if (unlikely(err))
21331+ goto out;
21332+
21333+ h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
4a4d8108
AM
21334+ err = au_pin(&a->pin, src_dentry, a->bdst,
21335+ au_opt_udba(src_dentry->d_sb),
21336+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
21337+ if (unlikely(err))
21338+ goto out;
367653fa 21339+
c2b27bf2 21340+ err = au_sio_cpup_simple(&cpg);
4a4d8108
AM
21341+ au_unpin(&a->pin);
21342+
4f0767ce 21343+out:
4a4d8108
AM
21344+ di_read_unlock(a->src_parent, AuLock_IR);
21345+ return err;
21346+}
21347+
86dc4139
AM
21348+static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
21349+ struct au_link_args *a)
4a4d8108
AM
21350+{
21351+ int err;
21352+ unsigned char plink;
5afbbe0d 21353+ aufs_bindex_t bbot;
4a4d8108 21354+ struct dentry *h_src_dentry;
523b37e3 21355+ struct inode *h_inode, *inode, *delegated;
4a4d8108
AM
21356+ struct super_block *sb;
21357+ struct file *h_file;
21358+
21359+ plink = 0;
21360+ h_inode = NULL;
21361+ sb = src_dentry->d_sb;
5527c038 21362+ inode = d_inode(src_dentry);
5afbbe0d 21363+ if (au_ibtop(inode) <= a->bdst)
4a4d8108
AM
21364+ h_inode = au_h_iptr(inode, a->bdst);
21365+ if (!h_inode || !h_inode->i_nlink) {
21366+ /* copyup src_dentry as the name of dentry. */
5afbbe0d
AM
21367+ bbot = au_dbbot(dentry);
21368+ if (bbot < a->bsrc)
21369+ au_set_dbbot(dentry, a->bsrc);
86dc4139
AM
21370+ au_set_h_dptr(dentry, a->bsrc,
21371+ dget(au_h_dptr(src_dentry, a->bsrc)));
21372+ dget(a->h_path.dentry);
21373+ au_set_h_dptr(dentry, a->bdst, NULL);
c1595e42
JR
21374+ AuDbg("temporary d_inode...\n");
21375+ spin_lock(&dentry->d_lock);
5527c038 21376+ dentry->d_inode = d_inode(src_dentry); /* tmp */
c1595e42 21377+ spin_unlock(&dentry->d_lock);
392086de 21378+ h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
86dc4139 21379+ if (IS_ERR(h_file))
4a4d8108 21380+ err = PTR_ERR(h_file);
86dc4139 21381+ else {
c2b27bf2
AM
21382+ struct au_cp_generic cpg = {
21383+ .dentry = dentry,
21384+ .bdst = a->bdst,
21385+ .bsrc = -1,
21386+ .len = -1,
21387+ .pin = &a->pin,
21388+ .flags = AuCpup_KEEPLINO
21389+ };
21390+ err = au_sio_cpup_simple(&cpg);
86dc4139
AM
21391+ au_h_open_post(dentry, a->bsrc, h_file);
21392+ if (!err) {
21393+ dput(a->h_path.dentry);
21394+ a->h_path.dentry = au_h_dptr(dentry, a->bdst);
21395+ } else
21396+ au_set_h_dptr(dentry, a->bdst,
21397+ a->h_path.dentry);
21398+ }
c1595e42 21399+ spin_lock(&dentry->d_lock);
86dc4139 21400+ dentry->d_inode = NULL; /* restore */
c1595e42
JR
21401+ spin_unlock(&dentry->d_lock);
21402+ AuDbg("temporary d_inode...done\n");
86dc4139 21403+ au_set_h_dptr(dentry, a->bsrc, NULL);
5afbbe0d 21404+ au_set_dbbot(dentry, bbot);
4a4d8108
AM
21405+ } else {
21406+ /* the inode of src_dentry already exists on a.bdst branch */
21407+ h_src_dentry = d_find_alias(h_inode);
21408+ if (!h_src_dentry && au_plink_test(inode)) {
21409+ plink = 1;
21410+ h_src_dentry = au_plink_lkup(inode, a->bdst);
21411+ err = PTR_ERR(h_src_dentry);
21412+ if (IS_ERR(h_src_dentry))
21413+ goto out;
21414+
5527c038 21415+ if (unlikely(d_is_negative(h_src_dentry))) {
4a4d8108
AM
21416+ dput(h_src_dentry);
21417+ h_src_dentry = NULL;
21418+ }
21419+
21420+ }
21421+ if (h_src_dentry) {
523b37e3 21422+ delegated = NULL;
4a4d8108 21423+ err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
523b37e3
AM
21424+ &a->h_path, &delegated);
21425+ if (unlikely(err == -EWOULDBLOCK)) {
21426+ pr_warn("cannot retry for NFSv4 delegation"
21427+ " for an internal link\n");
21428+ iput(delegated);
21429+ }
4a4d8108
AM
21430+ dput(h_src_dentry);
21431+ } else {
21432+ AuIOErr("no dentry found for hi%lu on b%d\n",
21433+ h_inode->i_ino, a->bdst);
21434+ err = -EIO;
21435+ }
21436+ }
21437+
21438+ if (!err && !plink)
21439+ au_plink_append(inode, a->bdst, a->h_path.dentry);
21440+
21441+out:
2cbb1c4b 21442+ AuTraceErr(err);
4a4d8108
AM
21443+ return err;
21444+}
21445+
21446+int aufs_link(struct dentry *src_dentry, struct inode *dir,
21447+ struct dentry *dentry)
21448+{
21449+ int err, rerr;
21450+ struct au_dtime dt;
21451+ struct au_link_args *a;
21452+ struct dentry *wh_dentry, *h_src_dentry;
523b37e3 21453+ struct inode *inode, *delegated;
4a4d8108
AM
21454+ struct super_block *sb;
21455+ struct au_wr_dir_args wr_dir_args = {
21456+ /* .force_btgt = -1, */
21457+ .flags = AuWrDir_ADD_ENTRY
21458+ };
21459+
21460+ IMustLock(dir);
5527c038 21461+ inode = d_inode(src_dentry);
4a4d8108
AM
21462+ IMustLock(inode);
21463+
4a4d8108
AM
21464+ err = -ENOMEM;
21465+ a = kzalloc(sizeof(*a), GFP_NOFS);
21466+ if (unlikely(!a))
21467+ goto out;
21468+
21469+ a->parent = dentry->d_parent; /* dir inode is locked */
027c5e7a
AM
21470+ err = aufs_read_and_write_lock2(dentry, src_dentry,
21471+ AuLock_NOPLM | AuLock_GEN);
e49829fe
JR
21472+ if (unlikely(err))
21473+ goto out_kfree;
38d290e6 21474+ err = au_d_linkable(src_dentry);
027c5e7a
AM
21475+ if (unlikely(err))
21476+ goto out_unlock;
21477+ err = au_d_may_add(dentry);
21478+ if (unlikely(err))
21479+ goto out_unlock;
e49829fe 21480+
4a4d8108 21481+ a->src_parent = dget_parent(src_dentry);
5afbbe0d 21482+ wr_dir_args.force_btgt = au_ibtop(inode);
4a4d8108
AM
21483+
21484+ di_write_lock_parent(a->parent);
21485+ wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
21486+ wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
21487+ &wr_dir_args);
21488+ err = PTR_ERR(wh_dentry);
21489+ if (IS_ERR(wh_dentry))
027c5e7a 21490+ goto out_parent;
4a4d8108
AM
21491+
21492+ err = 0;
21493+ sb = dentry->d_sb;
5afbbe0d 21494+ a->bdst = au_dbtop(dentry);
4a4d8108
AM
21495+ a->h_path.dentry = au_h_dptr(dentry, a->bdst);
21496+ a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
5afbbe0d 21497+ a->bsrc = au_ibtop(inode);
2cbb1c4b 21498+ h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
38d290e6
JR
21499+ if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
21500+ h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
2cbb1c4b 21501+ if (!h_src_dentry) {
5afbbe0d 21502+ a->bsrc = au_dbtop(src_dentry);
2cbb1c4b
JR
21503+ h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
21504+ AuDebugOn(!h_src_dentry);
38d290e6
JR
21505+ } else if (IS_ERR(h_src_dentry)) {
21506+ err = PTR_ERR(h_src_dentry);
2cbb1c4b 21507+ goto out_parent;
38d290e6 21508+ }
2cbb1c4b 21509+
f2c43d5f
AM
21510+ /*
21511+ * aufs doesn't touch the credential so
acd2b654 21512+ * security_dentry_create_files_as() is unnecessary.
f2c43d5f 21513+ */
4a4d8108
AM
21514+ if (au_opt_test(au_mntflags(sb), PLINK)) {
21515+ if (a->bdst < a->bsrc
21516+ /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
86dc4139 21517+ err = au_cpup_or_link(src_dentry, dentry, a);
523b37e3
AM
21518+ else {
21519+ delegated = NULL;
4a4d8108 21520+ err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
523b37e3
AM
21521+ &a->h_path, &delegated);
21522+ if (unlikely(err == -EWOULDBLOCK)) {
21523+ pr_warn("cannot retry for NFSv4 delegation"
21524+ " for an internal link\n");
21525+ iput(delegated);
21526+ }
21527+ }
2cbb1c4b 21528+ dput(h_src_dentry);
4a4d8108
AM
21529+ } else {
21530+ /*
21531+ * copyup src_dentry to the branch we process,
21532+ * and then link(2) to it.
21533+ */
2cbb1c4b 21534+ dput(h_src_dentry);
4a4d8108
AM
21535+ if (a->bdst < a->bsrc
21536+ /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
21537+ au_unpin(&a->pin);
21538+ di_write_unlock(a->parent);
21539+ err = au_cpup_before_link(src_dentry, a);
21540+ di_write_lock_parent(a->parent);
21541+ if (!err)
21542+ err = au_pin(&a->pin, dentry, a->bdst,
21543+ au_opt_udba(sb),
21544+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
21545+ if (unlikely(err))
21546+ goto out_wh;
21547+ }
21548+ if (!err) {
21549+ h_src_dentry = au_h_dptr(src_dentry, a->bdst);
21550+ err = -ENOENT;
5527c038 21551+ if (h_src_dentry && d_is_positive(h_src_dentry)) {
523b37e3 21552+ delegated = NULL;
4a4d8108
AM
21553+ err = vfsub_link(h_src_dentry,
21554+ au_pinned_h_dir(&a->pin),
523b37e3
AM
21555+ &a->h_path, &delegated);
21556+ if (unlikely(err == -EWOULDBLOCK)) {
21557+ pr_warn("cannot retry"
21558+ " for NFSv4 delegation"
21559+ " for an internal link\n");
21560+ iput(delegated);
21561+ }
21562+ }
4a4d8108
AM
21563+ }
21564+ }
21565+ if (unlikely(err))
21566+ goto out_unpin;
21567+
21568+ if (wh_dentry) {
21569+ a->h_path.dentry = wh_dentry;
21570+ err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
21571+ dentry);
21572+ if (unlikely(err))
21573+ goto out_revert;
21574+ }
21575+
b912730e 21576+ au_dir_ts(dir, a->bdst);
be118d29 21577+ inode_inc_iversion(dir);
4a4d8108
AM
21578+ inc_nlink(inode);
21579+ inode->i_ctime = dir->i_ctime;
027c5e7a
AM
21580+ d_instantiate(dentry, au_igrab(inode));
21581+ if (d_unhashed(a->h_path.dentry))
4a4d8108
AM
21582+ /* some filesystem calls d_drop() */
21583+ d_drop(dentry);
076b876e
AM
21584+ /* some filesystems consume an inode even hardlink */
21585+ au_fhsm_wrote(sb, a->bdst, /*force*/0);
4a4d8108
AM
21586+ goto out_unpin; /* success */
21587+
4f0767ce 21588+out_revert:
523b37e3
AM
21589+ /* no delegation since it is just created */
21590+ rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
21591+ /*delegated*/NULL, /*force*/0);
027c5e7a 21592+ if (unlikely(rerr)) {
523b37e3 21593+ AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
027c5e7a
AM
21594+ err = -EIO;
21595+ }
4a4d8108 21596+ au_dtime_revert(&dt);
4f0767ce 21597+out_unpin:
4a4d8108 21598+ au_unpin(&a->pin);
4f0767ce 21599+out_wh:
4a4d8108 21600+ dput(wh_dentry);
027c5e7a
AM
21601+out_parent:
21602+ di_write_unlock(a->parent);
21603+ dput(a->src_parent);
4f0767ce 21604+out_unlock:
4a4d8108 21605+ if (unlikely(err)) {
5afbbe0d 21606+ au_update_dbtop(dentry);
4a4d8108
AM
21607+ d_drop(dentry);
21608+ }
4a4d8108 21609+ aufs_read_and_write_unlock2(dentry, src_dentry);
e49829fe 21610+out_kfree:
9f237c51 21611+ au_kfree_rcu(a);
4f0767ce 21612+out:
86dc4139 21613+ AuTraceErr(err);
4a4d8108
AM
21614+ return err;
21615+}
21616+
0b2a12c6
JR
21617+int aufs_mkdir(struct user_namespace *userns, struct inode *dir,
21618+ struct dentry *dentry, umode_t mode)
4a4d8108
AM
21619+{
21620+ int err, rerr;
21621+ aufs_bindex_t bindex;
21622+ unsigned char diropq;
21623+ struct path h_path;
21624+ struct dentry *wh_dentry, *parent, *opq_dentry;
febd17d6 21625+ struct inode *h_inode;
4a4d8108
AM
21626+ struct super_block *sb;
21627+ struct {
21628+ struct au_pin pin;
21629+ struct au_dtime dt;
21630+ } *a; /* reduce the stack usage */
21631+ struct au_wr_dir_args wr_dir_args = {
21632+ .force_btgt = -1,
21633+ .flags = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
21634+ };
21635+
21636+ IMustLock(dir);
21637+
21638+ err = -ENOMEM;
21639+ a = kmalloc(sizeof(*a), GFP_NOFS);
21640+ if (unlikely(!a))
21641+ goto out;
21642+
027c5e7a
AM
21643+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
21644+ if (unlikely(err))
21645+ goto out_free;
21646+ err = au_d_may_add(dentry);
21647+ if (unlikely(err))
21648+ goto out_unlock;
21649+
4a4d8108
AM
21650+ parent = dentry->d_parent; /* dir inode is locked */
21651+ di_write_lock_parent(parent);
21652+ wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
21653+ &a->pin, &wr_dir_args);
21654+ err = PTR_ERR(wh_dentry);
21655+ if (IS_ERR(wh_dentry))
027c5e7a 21656+ goto out_parent;
4a4d8108
AM
21657+
21658+ sb = dentry->d_sb;
5afbbe0d 21659+ bindex = au_dbtop(dentry);
4a4d8108
AM
21660+ h_path.dentry = au_h_dptr(dentry, bindex);
21661+ h_path.mnt = au_sbr_mnt(sb, bindex);
21662+ err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
21663+ if (unlikely(err))
027c5e7a 21664+ goto out_unpin;
4a4d8108
AM
21665+
21666+ /* make the dir opaque */
21667+ diropq = 0;
febd17d6 21668+ h_inode = d_inode(h_path.dentry);
4a4d8108
AM
21669+ if (wh_dentry
21670+ || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
febd17d6 21671+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 21672+ opq_dentry = au_diropq_create(dentry, bindex);
febd17d6 21673+ inode_unlock(h_inode);
4a4d8108
AM
21674+ err = PTR_ERR(opq_dentry);
21675+ if (IS_ERR(opq_dentry))
21676+ goto out_dir;
21677+ dput(opq_dentry);
21678+ diropq = 1;
21679+ }
21680+
21681+ err = epilog(dir, bindex, wh_dentry, dentry);
21682+ if (!err) {
21683+ inc_nlink(dir);
027c5e7a 21684+ goto out_unpin; /* success */
4a4d8108
AM
21685+ }
21686+
21687+ /* revert */
21688+ if (diropq) {
21689+ AuLabel(revert opq);
febd17d6 21690+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 21691+ rerr = au_diropq_remove(dentry, bindex);
febd17d6 21692+ inode_unlock(h_inode);
4a4d8108 21693+ if (rerr) {
523b37e3
AM
21694+ AuIOErr("%pd reverting diropq failed(%d, %d)\n",
21695+ dentry, err, rerr);
4a4d8108
AM
21696+ err = -EIO;
21697+ }
21698+ }
21699+
4f0767ce 21700+out_dir:
4a4d8108
AM
21701+ AuLabel(revert dir);
21702+ rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
21703+ if (rerr) {
523b37e3
AM
21704+ AuIOErr("%pd reverting dir failed(%d, %d)\n",
21705+ dentry, err, rerr);
4a4d8108
AM
21706+ err = -EIO;
21707+ }
4a4d8108 21708+ au_dtime_revert(&a->dt);
027c5e7a 21709+out_unpin:
4a4d8108
AM
21710+ au_unpin(&a->pin);
21711+ dput(wh_dentry);
027c5e7a
AM
21712+out_parent:
21713+ di_write_unlock(parent);
21714+out_unlock:
4a4d8108 21715+ if (unlikely(err)) {
5afbbe0d 21716+ au_update_dbtop(dentry);
4a4d8108
AM
21717+ d_drop(dentry);
21718+ }
4a4d8108 21719+ aufs_read_unlock(dentry, AuLock_DW);
027c5e7a 21720+out_free:
9f237c51 21721+ au_kfree_rcu(a);
4f0767ce 21722+out:
4a4d8108
AM
21723+ return err;
21724+}
7f207e10 21725diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
eca34b5c 21726--- /usr/share/empty/fs/aufs/i_op.c 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 21727+++ linux/fs/aufs/i_op.c 2022-11-05 23:02:18.965889284 +0100
f4d37d76 21728@@ -0,0 +1,1512 @@
cd7a4cd9 21729+// SPDX-License-Identifier: GPL-2.0
4a4d8108 21730+/*
0dcfbb52 21731+ * Copyright (C) 2005-2022 Junjiro R. Okajima
4a4d8108 21732+ *
0dcfbb52 21733+ * This program is free software; you can redistribute it and/or modify
4a4d8108
AM
21734+ * it under the terms of the GNU General Public License as published by
21735+ * the Free Software Foundation; either version 2 of the License, or
21736+ * (at your option) any later version.
21737+ *
21738+ * This program is distributed in the hope that it will be useful,
21739+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
21740+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21741+ * GNU General Public License for more details.
21742+ *
21743+ * You should have received a copy of the GNU General Public License
523b37e3 21744+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 21745+ */
1facf9fc 21746+
1308ab2a 21747+/*
4a4d8108 21748+ * inode operations (except add/del/rename)
1308ab2a 21749+ */
4a4d8108
AM
21750+
21751+#include <linux/device_cgroup.h>
21752+#include <linux/fs_stack.h>
eca801bf 21753+#include <linux/iversion.h>
4a4d8108 21754+#include <linux/security.h>
4a4d8108
AM
21755+#include "aufs.h"
21756+
1e00d052 21757+static int h_permission(struct inode *h_inode, int mask,
79b8bda9 21758+ struct path *h_path, int brperm)
1facf9fc 21759+{
1308ab2a 21760+ int err;
4a4d8108 21761+ const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
0b2a12c6 21762+ struct user_namespace *h_userns;
1facf9fc 21763+
e2f27e51
AM
21764+ err = -EPERM;
21765+ if (write_mask && IS_IMMUTABLE(h_inode))
21766+ goto out;
21767+
4a4d8108 21768+ err = -EACCES;
e2f27e51
AM
21769+ if (((mask & MAY_EXEC)
21770+ && S_ISREG(h_inode->i_mode)
21771+ && (path_noexec(h_path)
cd7a4cd9 21772+ || !(h_inode->i_mode & 0111))))
4a4d8108
AM
21773+ goto out;
21774+
21775+ /*
21776+ * - skip the lower fs test in the case of write to ro branch.
21777+ * - nfs dir permission write check is optimized, but a policy for
21778+ * link/rename requires a real check.
2121bcd9 21779+ * - nfs always sets SB_POSIXACL regardless its mount option 'noacl.'
b912730e 21780+ * in this case, generic_permission() returns -EOPNOTSUPP.
4a4d8108 21781+ */
0b2a12c6 21782+ h_userns = mnt_user_ns(h_path->mnt);
4a4d8108
AM
21783+ if ((write_mask && !au_br_writable(brperm))
21784+ || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
21785+ && write_mask && !(mask & MAY_READ))
21786+ || !h_inode->i_op->permission) {
21787+ /* AuLabel(generic_permission); */
062440b3 21788+ /* AuDbg("get_acl %ps\n", h_inode->i_op->get_acl); */
0b2a12c6 21789+ err = generic_permission(h_userns, h_inode, mask);
b912730e 21790+ if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
0b2a12c6
JR
21791+ err = h_inode->i_op->permission(h_userns, h_inode,
21792+ mask);
b912730e 21793+ AuTraceErr(err);
1308ab2a 21794+ } else {
4a4d8108 21795+ /* AuLabel(h_inode->permission); */
0b2a12c6 21796+ err = h_inode->i_op->permission(h_userns, h_inode, mask);
4a4d8108
AM
21797+ AuTraceErr(err);
21798+ }
1facf9fc 21799+
4a4d8108
AM
21800+ if (!err)
21801+ err = devcgroup_inode_permission(h_inode, mask);
7f207e10 21802+ if (!err)
4a4d8108 21803+ err = security_inode_permission(h_inode, mask);
4a4d8108 21804+
4f0767ce 21805+out:
1308ab2a 21806+ return err;
21807+}
dece6358 21808+
0b2a12c6
JR
21809+static int aufs_permission(struct user_namespace *userns, struct inode *inode,
21810+ int mask)
1308ab2a 21811+{
21812+ int err;
5afbbe0d 21813+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
21814+ const unsigned char isdir = !!S_ISDIR(inode->i_mode),
21815+ write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
21816+ struct inode *h_inode;
21817+ struct super_block *sb;
21818+ struct au_branch *br;
1facf9fc 21819+
027c5e7a 21820+ /* todo: support rcu-walk? */
1e00d052 21821+ if (mask & MAY_NOT_BLOCK)
027c5e7a
AM
21822+ return -ECHILD;
21823+
4a4d8108
AM
21824+ sb = inode->i_sb;
21825+ si_read_lock(sb, AuLock_FLUSH);
21826+ ii_read_lock_child(inode);
43982f53
AM
21827+#if 0 /* reserved for future use */
21828+ /*
21829+ * This test may be rather 'too much' since the test is essentially done
21830+ * in the aufs_lookup(). Theoretically it is possible that the inode
21831+ * generation doesn't match to the superblock's here. But it isn't a
21832+ * big deal I suppose.
21833+ */
027c5e7a
AM
21834+ err = au_iigen_test(inode, au_sigen(sb));
21835+ if (unlikely(err))
21836+ goto out;
21837+#endif
dece6358 21838+
076b876e
AM
21839+ if (!isdir
21840+ || write_mask
21841+ || au_opt_test(au_mntflags(sb), DIRPERM1)) {
4a4d8108 21842+ err = au_busy_or_stale();
5afbbe0d 21843+ h_inode = au_h_iptr(inode, au_ibtop(inode));
4a4d8108
AM
21844+ if (unlikely(!h_inode
21845+ || (h_inode->i_mode & S_IFMT)
21846+ != (inode->i_mode & S_IFMT)))
21847+ goto out;
1facf9fc 21848+
4a4d8108 21849+ err = 0;
5afbbe0d 21850+ bindex = au_ibtop(inode);
4a4d8108 21851+ br = au_sbr(sb, bindex);
79b8bda9 21852+ err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
4a4d8108
AM
21853+ if (write_mask
21854+ && !err
21855+ && !special_file(h_inode->i_mode)) {
21856+ /* test whether the upper writable branch exists */
21857+ err = -EROFS;
21858+ for (; bindex >= 0; bindex--)
21859+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
21860+ err = 0;
21861+ break;
21862+ }
21863+ }
21864+ goto out;
21865+ }
dece6358 21866+
4a4d8108 21867+ /* non-write to dir */
1308ab2a 21868+ err = 0;
5afbbe0d
AM
21869+ bbot = au_ibbot(inode);
21870+ for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
4a4d8108
AM
21871+ h_inode = au_h_iptr(inode, bindex);
21872+ if (h_inode) {
21873+ err = au_busy_or_stale();
21874+ if (unlikely(!S_ISDIR(h_inode->i_mode)))
21875+ break;
21876+
21877+ br = au_sbr(sb, bindex);
79b8bda9 21878+ err = h_permission(h_inode, mask, &br->br_path,
4a4d8108
AM
21879+ br->br_perm);
21880+ }
21881+ }
1308ab2a 21882+
4f0767ce 21883+out:
4a4d8108
AM
21884+ ii_read_unlock(inode);
21885+ si_read_unlock(sb);
1308ab2a 21886+ return err;
21887+}
21888+
4a4d8108 21889+/* ---------------------------------------------------------------------- */
1facf9fc 21890+
4a4d8108 21891+static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
b4510431 21892+ unsigned int flags)
4a4d8108
AM
21893+{
21894+ struct dentry *ret, *parent;
b752ccd1 21895+ struct inode *inode;
4a4d8108 21896+ struct super_block *sb;
1716fcea 21897+ int err, npositive;
dece6358 21898+
4a4d8108 21899+ IMustLock(dir);
1308ab2a 21900+
537831f9
AM
21901+ /* todo: support rcu-walk? */
21902+ ret = ERR_PTR(-ECHILD);
21903+ if (flags & LOOKUP_RCU)
21904+ goto out;
21905+
21906+ ret = ERR_PTR(-ENAMETOOLONG);
21907+ if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
21908+ goto out;
21909+
4a4d8108 21910+ sb = dir->i_sb;
7f207e10
AM
21911+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21912+ ret = ERR_PTR(err);
21913+ if (unlikely(err))
21914+ goto out;
21915+
4a4d8108
AM
21916+ err = au_di_init(dentry);
21917+ ret = ERR_PTR(err);
21918+ if (unlikely(err))
7f207e10 21919+ goto out_si;
1308ab2a 21920+
9dbd164d 21921+ inode = NULL;
027c5e7a 21922+ npositive = 0; /* suppress a warning */
4a4d8108
AM
21923+ parent = dentry->d_parent; /* dir inode is locked */
21924+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a
AM
21925+ err = au_alive_dir(parent);
21926+ if (!err)
21927+ err = au_digen_test(parent, au_sigen(sb));
21928+ if (!err) {
5afbbe0d
AM
21929+ /* regardless LOOKUP_CREATE, always ALLOW_NEG */
21930+ npositive = au_lkup_dentry(dentry, au_dbtop(parent),
21931+ AuLkup_ALLOW_NEG);
027c5e7a
AM
21932+ err = npositive;
21933+ }
4a4d8108 21934+ di_read_unlock(parent, AuLock_IR);
4a4d8108
AM
21935+ ret = ERR_PTR(err);
21936+ if (unlikely(err < 0))
21937+ goto out_unlock;
1308ab2a 21938+
4a4d8108 21939+ if (npositive) {
b752ccd1 21940+ inode = au_new_inode(dentry, /*must_new*/0);
c1595e42
JR
21941+ if (IS_ERR(inode)) {
21942+ ret = (void *)inode;
21943+ inode = NULL;
21944+ goto out_unlock;
21945+ }
9dbd164d 21946+ }
4a4d8108 21947+
c1595e42
JR
21948+ if (inode)
21949+ atomic_inc(&inode->i_count);
4a4d8108 21950+ ret = d_splice_alias(inode, dentry);
43982f53 21951+#if 0 /* reserved for future use */
537831f9
AM
21952+ if (unlikely(d_need_lookup(dentry))) {
21953+ spin_lock(&dentry->d_lock);
21954+ dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
21955+ spin_unlock(&dentry->d_lock);
21956+ } else
21957+#endif
c1595e42 21958+ if (inode) {
2000de60 21959+ if (!IS_ERR(ret)) {
c1595e42 21960+ iput(inode);
2000de60
JR
21961+ if (ret && ret != dentry)
21962+ ii_write_unlock(inode);
21963+ } else {
c1595e42
JR
21964+ ii_write_unlock(inode);
21965+ iput(inode);
21966+ inode = NULL;
21967+ }
7f207e10 21968+ }
1facf9fc 21969+
4f0767ce 21970+out_unlock:
4a4d8108 21971+ di_write_unlock(dentry);
7f207e10 21972+out_si:
4a4d8108 21973+ si_read_unlock(sb);
7f207e10 21974+out:
4a4d8108
AM
21975+ return ret;
21976+}
1facf9fc 21977+
4a4d8108 21978+/* ---------------------------------------------------------------------- */
1facf9fc 21979+
acd2b654
AM
21980+/*
21981+ * very dirty and complicated aufs ->atomic_open().
21982+ * aufs_atomic_open()
21983+ * + au_aopen_or_create()
21984+ * + add_simple()
21985+ * + vfsub_atomic_open()
21986+ * + branch fs ->atomic_open()
21987+ * may call the actual 'open' for h_file
21988+ * + inc br_nfiles only if opened
21989+ * + au_aopen_no_open() or au_aopen_do_open()
21990+ *
21991+ * au_aopen_do_open()
21992+ * + finish_open()
21993+ * + au_do_aopen()
21994+ * + au_do_open() the body of all 'open'
21995+ * + au_do_open_nondir()
21996+ * set the passed h_file
21997+ *
21998+ * au_aopen_no_open()
21999+ * + finish_no_open()
22000+ */
22001+
b912730e 22002+struct aopen_node {
8b6a4947 22003+ struct hlist_bl_node hblist;
b912730e
AM
22004+ struct file *file, *h_file;
22005+};
22006+
22007+static int au_do_aopen(struct inode *inode, struct file *file)
22008+{
8b6a4947
AM
22009+ struct hlist_bl_head *aopen;
22010+ struct hlist_bl_node *pos;
b912730e
AM
22011+ struct aopen_node *node;
22012+ struct au_do_open_args args = {
8b6a4947
AM
22013+ .aopen = 1,
22014+ .open = au_do_open_nondir
b912730e
AM
22015+ };
22016+
22017+ aopen = &au_sbi(inode->i_sb)->si_aopen;
8b6a4947
AM
22018+ hlist_bl_lock(aopen);
22019+ hlist_bl_for_each_entry(node, pos, aopen, hblist)
b912730e
AM
22020+ if (node->file == file) {
22021+ args.h_file = node->h_file;
22022+ break;
22023+ }
8b6a4947 22024+ hlist_bl_unlock(aopen);
b912730e
AM
22025+ /* AuDebugOn(!args.h_file); */
22026+
22027+ return au_do_open(file, &args);
22028+}
22029+
acd2b654
AM
22030+static int au_aopen_do_open(struct file *file, struct dentry *dentry,
22031+ struct aopen_node *aopen_node)
22032+{
22033+ int err;
22034+ struct hlist_bl_head *aopen;
22035+
22036+ AuLabel(here);
22037+ aopen = &au_sbi(dentry->d_sb)->si_aopen;
22038+ au_hbl_add(&aopen_node->hblist, aopen);
22039+ err = finish_open(file, dentry, au_do_aopen);
22040+ au_hbl_del(&aopen_node->hblist, aopen);
22041+ /* AuDbgFile(file); */
22042+ AuDbg("%pd%s%s\n", dentry,
22043+ (file->f_mode & FMODE_CREATED) ? " created" : "",
22044+ (file->f_mode & FMODE_OPENED) ? " opened" : "");
22045+
22046+ AuTraceErr(err);
22047+ return err;
22048+}
22049+
22050+static int au_aopen_no_open(struct file *file, struct dentry *dentry)
22051+{
22052+ int err;
22053+
22054+ AuLabel(here);
22055+ dget(dentry);
22056+ err = finish_no_open(file, dentry);
22057+
22058+ AuTraceErr(err);
22059+ return err;
22060+}
22061+
b912730e
AM
22062+static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
22063+ struct file *file, unsigned int open_flag,
acd2b654 22064+ umode_t create_mode)
b912730e 22065+{
acd2b654 22066+ int err, did_open;
5afbbe0d 22067+ unsigned int lkup_flags;
acd2b654
AM
22068+ aufs_bindex_t bindex;
22069+ struct super_block *sb;
f0c0a007 22070+ struct dentry *parent, *d;
b912730e
AM
22071+ struct vfsub_aopen_args args = {
22072+ .open_flag = open_flag,
acd2b654 22073+ .create_mode = create_mode
b912730e
AM
22074+ };
22075+ struct aopen_node aopen_node = {
22076+ .file = file
22077+ };
22078+
22079+ IMustLock(dir);
5afbbe0d 22080+ AuDbg("open_flag 0%o\n", open_flag);
b912730e
AM
22081+ AuDbgDentry(dentry);
22082+
22083+ err = 0;
22084+ if (!au_di(dentry)) {
5afbbe0d
AM
22085+ lkup_flags = LOOKUP_OPEN;
22086+ if (open_flag & O_CREAT)
22087+ lkup_flags |= LOOKUP_CREATE;
22088+ d = aufs_lookup(dir, dentry, lkup_flags);
b912730e
AM
22089+ if (IS_ERR(d)) {
22090+ err = PTR_ERR(d);
5afbbe0d 22091+ AuTraceErr(err);
b912730e
AM
22092+ goto out;
22093+ } else if (d) {
22094+ /*
22095+ * obsoleted dentry found.
22096+ * another error will be returned later.
22097+ */
22098+ d_drop(d);
b912730e 22099+ AuDbgDentry(d);
5afbbe0d 22100+ dput(d);
b912730e
AM
22101+ }
22102+ AuDbgDentry(dentry);
22103+ }
22104+
22105+ if (d_is_positive(dentry)
22106+ || d_unhashed(dentry)
22107+ || d_unlinked(dentry)
acd2b654
AM
22108+ || !(open_flag & O_CREAT)) {
22109+ err = au_aopen_no_open(file, dentry);
22110+ goto out; /* success */
22111+ }
b912730e
AM
22112+
22113+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
22114+ if (unlikely(err))
22115+ goto out;
22116+
acd2b654 22117+ sb = dentry->d_sb;
b912730e
AM
22118+ parent = dentry->d_parent; /* dir is locked */
22119+ di_write_lock_parent(parent);
5afbbe0d 22120+ err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
acd2b654
AM
22121+ if (unlikely(err < 0))
22122+ goto out_parent;
b912730e
AM
22123+
22124+ AuDbgDentry(dentry);
acd2b654
AM
22125+ if (d_is_positive(dentry)) {
22126+ err = au_aopen_no_open(file, dentry);
22127+ goto out_parent; /* success */
22128+ }
b912730e 22129+
acd2b654 22130+ args.file = alloc_empty_file(file->f_flags, current_cred());
b912730e
AM
22131+ err = PTR_ERR(args.file);
22132+ if (IS_ERR(args.file))
acd2b654 22133+ goto out_parent;
b912730e 22134+
acd2b654 22135+ bindex = au_dbtop(dentry);
b912730e
AM
22136+ err = au_aopen_or_create(dir, dentry, &args);
22137+ AuTraceErr(err);
22138+ AuDbgFile(args.file);
acd2b654
AM
22139+ file->f_mode = args.file->f_mode & ~FMODE_OPENED;
22140+ did_open = !!(args.file->f_mode & FMODE_OPENED);
22141+ if (!did_open) {
22142+ fput(args.file);
22143+ args.file = NULL;
b912730e 22144+ }
8b6a4947
AM
22145+ di_write_unlock(parent);
22146+ di_write_unlock(dentry);
acd2b654
AM
22147+ if (unlikely(err < 0)) {
22148+ if (args.file)
22149+ fput(args.file);
22150+ goto out_sb;
b912730e 22151+ }
b912730e 22152+
acd2b654
AM
22153+ if (!did_open)
22154+ err = au_aopen_no_open(file, dentry);
8b6a4947 22155+ else {
acd2b654
AM
22156+ aopen_node.h_file = args.file;
22157+ err = au_aopen_do_open(file, dentry, &aopen_node);
8b6a4947 22158+ }
acd2b654
AM
22159+ if (unlikely(err < 0)) {
22160+ if (args.file)
22161+ fput(args.file);
22162+ if (did_open)
22163+ au_lcnt_dec(&args.br->br_nfiles);
b912730e 22164+ }
acd2b654
AM
22165+ goto out_sb; /* success */
22166+
22167+out_parent:
22168+ di_write_unlock(parent);
22169+ di_write_unlock(dentry);
22170+out_sb:
22171+ si_read_unlock(sb);
b912730e 22172+out:
b912730e 22173+ AuTraceErr(err);
acd2b654 22174+ AuDbgFile(file);
b912730e
AM
22175+ return err;
22176+}
22177+
22178+
22179+/* ---------------------------------------------------------------------- */
22180+
4a4d8108
AM
22181+static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
22182+ const unsigned char add_entry, aufs_bindex_t bcpup,
5afbbe0d 22183+ aufs_bindex_t btop)
4a4d8108
AM
22184+{
22185+ int err;
22186+ struct dentry *h_parent;
22187+ struct inode *h_dir;
1facf9fc 22188+
027c5e7a 22189+ if (add_entry)
5527c038 22190+ IMustLock(d_inode(parent));
027c5e7a 22191+ else
4a4d8108
AM
22192+ di_write_lock_parent(parent);
22193+
22194+ err = 0;
22195+ if (!au_h_dptr(parent, bcpup)) {
5afbbe0d 22196+ if (btop > bcpup)
c2b27bf2 22197+ err = au_cpup_dirs(dentry, bcpup);
5afbbe0d 22198+ else if (btop < bcpup)
4a4d8108
AM
22199+ err = au_cpdown_dirs(dentry, bcpup);
22200+ else
c2b27bf2 22201+ BUG();
4a4d8108 22202+ }
38d290e6 22203+ if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
4a4d8108 22204+ h_parent = au_h_dptr(parent, bcpup);
5527c038 22205+ h_dir = d_inode(h_parent);
be118d29 22206+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
7e9cd9fe 22207+ err = au_lkup_neg(dentry, bcpup, /*wh*/0);
4a4d8108 22208+ /* todo: no unlock here */
3c1bdaff 22209+ inode_unlock_shared(h_dir);
027c5e7a
AM
22210+
22211+ AuDbg("bcpup %d\n", bcpup);
22212+ if (!err) {
5527c038 22213+ if (d_really_is_negative(dentry))
5afbbe0d 22214+ au_set_h_dptr(dentry, btop, NULL);
4a4d8108
AM
22215+ au_update_dbrange(dentry, /*do_put_zero*/0);
22216+ }
1308ab2a 22217+ }
1facf9fc 22218+
4a4d8108
AM
22219+ if (!add_entry)
22220+ di_write_unlock(parent);
22221+ if (!err)
22222+ err = bcpup; /* success */
1308ab2a 22223+
027c5e7a 22224+ AuTraceErr(err);
4a4d8108
AM
22225+ return err;
22226+}
1facf9fc 22227+
4a4d8108
AM
22228+/*
22229+ * decide the branch and the parent dir where we will create a new entry.
22230+ * returns new bindex or an error.
22231+ * copyup the parent dir if needed.
22232+ */
22233+int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
22234+ struct au_wr_dir_args *args)
22235+{
22236+ int err;
392086de 22237+ unsigned int flags;
5afbbe0d 22238+ aufs_bindex_t bcpup, btop, src_btop;
86dc4139
AM
22239+ const unsigned char add_entry
22240+ = au_ftest_wrdir(args->flags, ADD_ENTRY)
38d290e6 22241+ | au_ftest_wrdir(args->flags, TMPFILE);
4a4d8108
AM
22242+ struct super_block *sb;
22243+ struct dentry *parent;
22244+ struct au_sbinfo *sbinfo;
1facf9fc 22245+
4a4d8108
AM
22246+ sb = dentry->d_sb;
22247+ sbinfo = au_sbi(sb);
22248+ parent = dget_parent(dentry);
5afbbe0d
AM
22249+ btop = au_dbtop(dentry);
22250+ bcpup = btop;
4a4d8108
AM
22251+ if (args->force_btgt < 0) {
22252+ if (src_dentry) {
5afbbe0d
AM
22253+ src_btop = au_dbtop(src_dentry);
22254+ if (src_btop < btop)
22255+ bcpup = src_btop;
4a4d8108 22256+ } else if (add_entry) {
392086de
AM
22257+ flags = 0;
22258+ if (au_ftest_wrdir(args->flags, ISDIR))
22259+ au_fset_wbr(flags, DIR);
22260+ err = AuWbrCreate(sbinfo, dentry, flags);
4a4d8108
AM
22261+ bcpup = err;
22262+ }
1facf9fc 22263+
5527c038 22264+ if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
4a4d8108
AM
22265+ if (add_entry)
22266+ err = AuWbrCopyup(sbinfo, dentry);
22267+ else {
22268+ if (!IS_ROOT(dentry)) {
22269+ di_read_lock_parent(parent, !AuLock_IR);
22270+ err = AuWbrCopyup(sbinfo, dentry);
22271+ di_read_unlock(parent, !AuLock_IR);
22272+ } else
22273+ err = AuWbrCopyup(sbinfo, dentry);
22274+ }
22275+ bcpup = err;
22276+ if (unlikely(err < 0))
22277+ goto out;
22278+ }
22279+ } else {
22280+ bcpup = args->force_btgt;
5527c038 22281+ AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
1308ab2a 22282+ }
027c5e7a 22283+
5afbbe0d 22284+ AuDbg("btop %d, bcpup %d\n", btop, bcpup);
4a4d8108 22285+ err = bcpup;
5afbbe0d 22286+ if (bcpup == btop)
4a4d8108 22287+ goto out; /* success */
4a4d8108
AM
22288+
22289+ /* copyup the new parent into the branch we process */
5afbbe0d 22290+ err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
027c5e7a 22291+ if (err >= 0) {
5527c038 22292+ if (d_really_is_negative(dentry)) {
5afbbe0d
AM
22293+ au_set_h_dptr(dentry, btop, NULL);
22294+ au_set_dbtop(dentry, bcpup);
22295+ au_set_dbbot(dentry, bcpup);
027c5e7a 22296+ }
38d290e6
JR
22297+ AuDebugOn(add_entry
22298+ && !au_ftest_wrdir(args->flags, TMPFILE)
22299+ && !au_h_dptr(dentry, bcpup));
027c5e7a 22300+ }
86dc4139
AM
22301+
22302+out:
22303+ dput(parent);
22304+ return err;
22305+}
22306+
22307+/* ---------------------------------------------------------------------- */
22308+
22309+void au_pin_hdir_unlock(struct au_pin *p)
22310+{
22311+ if (p->hdir)
5afbbe0d 22312+ au_hn_inode_unlock(p->hdir);
86dc4139
AM
22313+}
22314+
c1595e42 22315+int au_pin_hdir_lock(struct au_pin *p)
86dc4139
AM
22316+{
22317+ int err;
22318+
22319+ err = 0;
22320+ if (!p->hdir)
22321+ goto out;
22322+
22323+ /* even if an error happens later, keep this lock */
5afbbe0d 22324+ au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
86dc4139
AM
22325+
22326+ err = -EBUSY;
5527c038 22327+ if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
86dc4139
AM
22328+ goto out;
22329+
22330+ err = 0;
22331+ if (p->h_dentry)
22332+ err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
22333+ p->h_parent, p->br);
22334+
22335+out:
22336+ return err;
22337+}
22338+
22339+int au_pin_hdir_relock(struct au_pin *p)
22340+{
22341+ int err, i;
22342+ struct inode *h_i;
22343+ struct dentry *h_d[] = {
22344+ p->h_dentry,
22345+ p->h_parent
22346+ };
22347+
22348+ err = au_pin_hdir_lock(p);
22349+ if (unlikely(err))
22350+ goto out;
22351+
22352+ for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
22353+ if (!h_d[i])
22354+ continue;
5527c038
JR
22355+ if (d_is_positive(h_d[i])) {
22356+ h_i = d_inode(h_d[i]);
86dc4139 22357+ err = !h_i->i_nlink;
5527c038 22358+ }
86dc4139
AM
22359+ }
22360+
22361+out:
22362+ return err;
22363+}
22364+
5afbbe0d 22365+static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
86dc4139 22366+{
eca34b5c 22367+ atomic_long_set(&p->hdir->hi_inode->i_rwsem.owner, (long)task);
86dc4139
AM
22368+}
22369+
22370+void au_pin_hdir_acquire_nest(struct au_pin *p)
22371+{
22372+ if (p->hdir) {
5afbbe0d 22373+ rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
86dc4139
AM
22374+ p->lsc_hi, 0, NULL, _RET_IP_);
22375+ au_pin_hdir_set_owner(p, current);
22376+ }
dece6358 22377+}
1facf9fc 22378+
86dc4139
AM
22379+void au_pin_hdir_release(struct au_pin *p)
22380+{
22381+ if (p->hdir) {
22382+ au_pin_hdir_set_owner(p, p->task);
d58c55f2 22383+ rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, _RET_IP_);
86dc4139
AM
22384+ }
22385+}
1308ab2a 22386+
4a4d8108 22387+struct dentry *au_pinned_h_parent(struct au_pin *pin)
1308ab2a 22388+{
4a4d8108
AM
22389+ if (pin && pin->parent)
22390+ return au_h_dptr(pin->parent, pin->bindex);
22391+ return NULL;
dece6358 22392+}
1facf9fc 22393+
4a4d8108 22394+void au_unpin(struct au_pin *p)
dece6358 22395+{
86dc4139
AM
22396+ if (p->hdir)
22397+ au_pin_hdir_unlock(p);
e49829fe 22398+ if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
b4510431 22399+ vfsub_mnt_drop_write(p->h_mnt);
4a4d8108
AM
22400+ if (!p->hdir)
22401+ return;
1facf9fc 22402+
4a4d8108
AM
22403+ if (!au_ftest_pin(p->flags, DI_LOCKED))
22404+ di_read_unlock(p->parent, AuLock_IR);
22405+ iput(p->hdir->hi_inode);
22406+ dput(p->parent);
22407+ p->parent = NULL;
22408+ p->hdir = NULL;
22409+ p->h_mnt = NULL;
86dc4139 22410+ /* do not clear p->task */
4a4d8108 22411+}
1308ab2a 22412+
4a4d8108
AM
22413+int au_do_pin(struct au_pin *p)
22414+{
22415+ int err;
22416+ struct super_block *sb;
4a4d8108
AM
22417+ struct inode *h_dir;
22418+
22419+ err = 0;
22420+ sb = p->dentry->d_sb;
86dc4139 22421+ p->br = au_sbr(sb, p->bindex);
4a4d8108
AM
22422+ if (IS_ROOT(p->dentry)) {
22423+ if (au_ftest_pin(p->flags, MNT_WRITE)) {
86dc4139 22424+ p->h_mnt = au_br_mnt(p->br);
b4510431 22425+ err = vfsub_mnt_want_write(p->h_mnt);
4a4d8108
AM
22426+ if (unlikely(err)) {
22427+ au_fclr_pin(p->flags, MNT_WRITE);
22428+ goto out_err;
22429+ }
22430+ }
dece6358 22431+ goto out;
1facf9fc 22432+ }
22433+
86dc4139 22434+ p->h_dentry = NULL;
5afbbe0d 22435+ if (p->bindex <= au_dbbot(p->dentry))
86dc4139 22436+ p->h_dentry = au_h_dptr(p->dentry, p->bindex);
dece6358 22437+
4a4d8108
AM
22438+ p->parent = dget_parent(p->dentry);
22439+ if (!au_ftest_pin(p->flags, DI_LOCKED))
22440+ di_read_lock(p->parent, AuLock_IR, p->lsc_di);
dece6358 22441+
4a4d8108 22442+ h_dir = NULL;
86dc4139 22443+ p->h_parent = au_h_dptr(p->parent, p->bindex);
5527c038 22444+ p->hdir = au_hi(d_inode(p->parent), p->bindex);
4a4d8108
AM
22445+ if (p->hdir)
22446+ h_dir = p->hdir->hi_inode;
dece6358 22447+
b752ccd1
AM
22448+ /*
22449+ * udba case, or
22450+ * if DI_LOCKED is not set, then p->parent may be different
22451+ * and h_parent can be NULL.
22452+ */
86dc4139 22453+ if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
e49829fe 22454+ err = -EBUSY;
4a4d8108
AM
22455+ if (!au_ftest_pin(p->flags, DI_LOCKED))
22456+ di_read_unlock(p->parent, AuLock_IR);
22457+ dput(p->parent);
22458+ p->parent = NULL;
22459+ goto out_err;
22460+ }
1308ab2a 22461+
4a4d8108 22462+ if (au_ftest_pin(p->flags, MNT_WRITE)) {
86dc4139 22463+ p->h_mnt = au_br_mnt(p->br);
b4510431 22464+ err = vfsub_mnt_want_write(p->h_mnt);
dece6358 22465+ if (unlikely(err)) {
4a4d8108 22466+ au_fclr_pin(p->flags, MNT_WRITE);
86dc4139
AM
22467+ if (!au_ftest_pin(p->flags, DI_LOCKED))
22468+ di_read_unlock(p->parent, AuLock_IR);
22469+ dput(p->parent);
22470+ p->parent = NULL;
22471+ goto out_err;
dece6358
AM
22472+ }
22473+ }
4a4d8108 22474+
86dc4139
AM
22475+ au_igrab(h_dir);
22476+ err = au_pin_hdir_lock(p);
22477+ if (!err)
22478+ goto out; /* success */
22479+
076b876e
AM
22480+ au_unpin(p);
22481+
4f0767ce 22482+out_err:
4a4d8108
AM
22483+ pr_err("err %d\n", err);
22484+ err = au_busy_or_stale();
4f0767ce 22485+out:
1facf9fc 22486+ return err;
22487+}
22488+
4a4d8108
AM
22489+void au_pin_init(struct au_pin *p, struct dentry *dentry,
22490+ aufs_bindex_t bindex, int lsc_di, int lsc_hi,
22491+ unsigned int udba, unsigned char flags)
22492+{
22493+ p->dentry = dentry;
22494+ p->udba = udba;
22495+ p->lsc_di = lsc_di;
22496+ p->lsc_hi = lsc_hi;
22497+ p->flags = flags;
22498+ p->bindex = bindex;
22499+
22500+ p->parent = NULL;
22501+ p->hdir = NULL;
22502+ p->h_mnt = NULL;
86dc4139
AM
22503+
22504+ p->h_dentry = NULL;
22505+ p->h_parent = NULL;
22506+ p->br = NULL;
22507+ p->task = current;
4a4d8108
AM
22508+}
22509+
22510+int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
22511+ unsigned int udba, unsigned char flags)
22512+{
22513+ au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
22514+ udba, flags);
22515+ return au_do_pin(pin);
22516+}
22517+
dece6358
AM
22518+/* ---------------------------------------------------------------------- */
22519+
1308ab2a 22520+/*
4a4d8108
AM
22521+ * ->setattr() and ->getattr() are called in various cases.
22522+ * chmod, stat: dentry is revalidated.
22523+ * fchmod, fstat: file and dentry are not revalidated, additionally they may be
22524+ * unhashed.
22525+ * for ->setattr(), ia->ia_file is passed from ftruncate only.
1308ab2a 22526+ */
027c5e7a 22527+/* todo: consolidate with do_refresh() and simple_reval_dpath() */
c1595e42 22528+int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
1facf9fc 22529+{
4a4d8108 22530+ int err;
4a4d8108 22531+ struct dentry *parent;
1facf9fc 22532+
1308ab2a 22533+ err = 0;
027c5e7a 22534+ if (au_digen_test(dentry, sigen)) {
4a4d8108
AM
22535+ parent = dget_parent(dentry);
22536+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 22537+ err = au_refresh_dentry(dentry, parent);
4a4d8108
AM
22538+ di_read_unlock(parent, AuLock_IR);
22539+ dput(parent);
dece6358 22540+ }
1facf9fc 22541+
4a4d8108 22542+ AuTraceErr(err);
1308ab2a 22543+ return err;
22544+}
dece6358 22545+
c1595e42
JR
22546+int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
22547+ struct au_icpup_args *a)
1308ab2a 22548+{
22549+ int err;
4a4d8108 22550+ loff_t sz;
5afbbe0d 22551+ aufs_bindex_t btop, ibtop;
4a4d8108
AM
22552+ struct dentry *hi_wh, *parent;
22553+ struct inode *inode;
4a4d8108
AM
22554+ struct au_wr_dir_args wr_dir_args = {
22555+ .force_btgt = -1,
22556+ .flags = 0
22557+ };
22558+
2000de60 22559+ if (d_is_dir(dentry))
4a4d8108
AM
22560+ au_fset_wrdir(wr_dir_args.flags, ISDIR);
22561+ /* plink or hi_wh() case */
5afbbe0d 22562+ btop = au_dbtop(dentry);
5527c038 22563+ inode = d_inode(dentry);
5afbbe0d
AM
22564+ ibtop = au_ibtop(inode);
22565+ if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
22566+ wr_dir_args.force_btgt = ibtop;
4a4d8108
AM
22567+ err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
22568+ if (unlikely(err < 0))
22569+ goto out;
22570+ a->btgt = err;
5afbbe0d 22571+ if (err != btop)
4a4d8108
AM
22572+ au_fset_icpup(a->flags, DID_CPUP);
22573+
22574+ err = 0;
22575+ a->pin_flags = AuPin_MNT_WRITE;
22576+ parent = NULL;
22577+ if (!IS_ROOT(dentry)) {
22578+ au_fset_pin(a->pin_flags, DI_LOCKED);
22579+ parent = dget_parent(dentry);
22580+ di_write_lock_parent(parent);
22581+ }
22582+
22583+ err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
22584+ if (unlikely(err))
22585+ goto out_parent;
22586+
4a4d8108 22587+ sz = -1;
5afbbe0d 22588+ a->h_path.dentry = au_h_dptr(dentry, btop);
5527c038 22589+ a->h_inode = d_inode(a->h_path.dentry);
c1595e42 22590+ if (ia && (ia->ia_valid & ATTR_SIZE)) {
be118d29 22591+ inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
c1595e42
JR
22592+ if (ia->ia_size < i_size_read(a->h_inode))
22593+ sz = ia->ia_size;
3c1bdaff 22594+ inode_unlock_shared(a->h_inode);
c1595e42 22595+ }
4a4d8108 22596+
4a4d8108 22597+ hi_wh = NULL;
027c5e7a 22598+ if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
4a4d8108
AM
22599+ hi_wh = au_hi_wh(inode, a->btgt);
22600+ if (!hi_wh) {
c2b27bf2
AM
22601+ struct au_cp_generic cpg = {
22602+ .dentry = dentry,
22603+ .bdst = a->btgt,
22604+ .bsrc = -1,
22605+ .len = sz,
22606+ .pin = &a->pin
22607+ };
22608+ err = au_sio_cpup_wh(&cpg, /*file*/NULL);
4a4d8108
AM
22609+ if (unlikely(err))
22610+ goto out_unlock;
22611+ hi_wh = au_hi_wh(inode, a->btgt);
22612+ /* todo: revalidate hi_wh? */
22613+ }
22614+ }
22615+
22616+ if (parent) {
22617+ au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
22618+ di_downgrade_lock(parent, AuLock_IR);
22619+ dput(parent);
22620+ parent = NULL;
22621+ }
22622+ if (!au_ftest_icpup(a->flags, DID_CPUP))
22623+ goto out; /* success */
22624+
22625+ if (!d_unhashed(dentry)) {
c2b27bf2
AM
22626+ struct au_cp_generic cpg = {
22627+ .dentry = dentry,
22628+ .bdst = a->btgt,
5afbbe0d 22629+ .bsrc = btop,
c2b27bf2
AM
22630+ .len = sz,
22631+ .pin = &a->pin,
22632+ .flags = AuCpup_DTIME | AuCpup_HOPEN
22633+ };
22634+ err = au_sio_cpup_simple(&cpg);
4a4d8108
AM
22635+ if (!err)
22636+ a->h_path.dentry = au_h_dptr(dentry, a->btgt);
22637+ } else if (!hi_wh)
22638+ a->h_path.dentry = au_h_dptr(dentry, a->btgt);
22639+ else
22640+ a->h_path.dentry = hi_wh; /* do not dget here */
1308ab2a 22641+
4f0767ce 22642+out_unlock:
5527c038 22643+ a->h_inode = d_inode(a->h_path.dentry);
86dc4139 22644+ if (!err)
dece6358 22645+ goto out; /* success */
4a4d8108 22646+ au_unpin(&a->pin);
4f0767ce 22647+out_parent:
4a4d8108
AM
22648+ if (parent) {
22649+ di_write_unlock(parent);
22650+ dput(parent);
22651+ }
4f0767ce 22652+out:
86dc4139 22653+ if (!err)
febd17d6 22654+ inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
1facf9fc 22655+ return err;
22656+}
22657+
0b2a12c6
JR
22658+static int aufs_setattr(struct user_namespace *userns, struct dentry *dentry,
22659+ struct iattr *ia)
1facf9fc 22660+{
4a4d8108 22661+ int err;
523b37e3 22662+ struct inode *inode, *delegated;
4a4d8108
AM
22663+ struct super_block *sb;
22664+ struct file *file;
22665+ struct au_icpup_args *a;
0b2a12c6 22666+ struct user_namespace *h_userns;
1facf9fc 22667+
5527c038 22668+ inode = d_inode(dentry);
4a4d8108 22669+ IMustLock(inode);
dece6358 22670+
0b2a12c6 22671+ err = setattr_prepare(userns, dentry, ia);
f2c43d5f
AM
22672+ if (unlikely(err))
22673+ goto out;
22674+
4a4d8108
AM
22675+ err = -ENOMEM;
22676+ a = kzalloc(sizeof(*a), GFP_NOFS);
22677+ if (unlikely(!a))
22678+ goto out;
1facf9fc 22679+
4a4d8108
AM
22680+ if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
22681+ ia->ia_valid &= ~ATTR_MODE;
dece6358 22682+
4a4d8108
AM
22683+ file = NULL;
22684+ sb = dentry->d_sb;
e49829fe
JR
22685+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
22686+ if (unlikely(err))
22687+ goto out_kfree;
22688+
4a4d8108
AM
22689+ if (ia->ia_valid & ATTR_FILE) {
22690+ /* currently ftruncate(2) only */
7e9cd9fe 22691+ AuDebugOn(!d_is_reg(dentry));
4a4d8108 22692+ file = ia->ia_file;
521ced18
JR
22693+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
22694+ /*fi_lsc*/0);
4a4d8108
AM
22695+ if (unlikely(err))
22696+ goto out_si;
22697+ ia->ia_file = au_hf_top(file);
22698+ a->udba = AuOpt_UDBA_NONE;
22699+ } else {
22700+ /* fchmod() doesn't pass ia_file */
22701+ a->udba = au_opt_udba(sb);
027c5e7a
AM
22702+ di_write_lock_child(dentry);
22703+ /* no d_unlinked(), to set UDBA_NONE for root */
4a4d8108
AM
22704+ if (d_unhashed(dentry))
22705+ a->udba = AuOpt_UDBA_NONE;
4a4d8108
AM
22706+ if (a->udba != AuOpt_UDBA_NONE) {
22707+ AuDebugOn(IS_ROOT(dentry));
22708+ err = au_reval_for_attr(dentry, au_sigen(sb));
22709+ if (unlikely(err))
22710+ goto out_dentry;
22711+ }
dece6358 22712+ }
dece6358 22713+
4a4d8108
AM
22714+ err = au_pin_and_icpup(dentry, ia, a);
22715+ if (unlikely(err < 0))
22716+ goto out_dentry;
22717+ if (au_ftest_icpup(a->flags, DID_CPUP)) {
22718+ ia->ia_file = NULL;
22719+ ia->ia_valid &= ~ATTR_FILE;
1308ab2a 22720+ }
dece6358 22721+
4a4d8108
AM
22722+ a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
22723+ if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
22724+ == (ATTR_MODE | ATTR_CTIME)) {
7eafdf33 22725+ err = security_path_chmod(&a->h_path, ia->ia_mode);
4a4d8108
AM
22726+ if (unlikely(err))
22727+ goto out_unlock;
22728+ } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
22729+ && (ia->ia_valid & ATTR_CTIME)) {
86dc4139 22730+ err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
4a4d8108
AM
22731+ if (unlikely(err))
22732+ goto out_unlock;
22733+ }
dece6358 22734+
4a4d8108
AM
22735+ if (ia->ia_valid & ATTR_SIZE) {
22736+ struct file *f;
1308ab2a 22737+
953406b4 22738+ if (ia->ia_size < i_size_read(inode))
4a4d8108 22739+ /* unmap only */
953406b4 22740+ truncate_setsize(inode, ia->ia_size);
1308ab2a 22741+
4a4d8108
AM
22742+ f = NULL;
22743+ if (ia->ia_valid & ATTR_FILE)
22744+ f = ia->ia_file;
febd17d6 22745+ inode_unlock(a->h_inode);
4a4d8108 22746+ err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
febd17d6 22747+ inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
523b37e3
AM
22748+ } else {
22749+ delegated = NULL;
22750+ while (1) {
22751+ err = vfsub_notify_change(&a->h_path, ia, &delegated);
22752+ if (delegated) {
22753+ err = break_deleg_wait(&delegated);
22754+ if (!err)
22755+ continue;
22756+ }
22757+ break;
22758+ }
22759+ }
8cdd5066
JR
22760+ /*
22761+ * regardless aufs 'acl' option setting.
22762+ * why don't all acl-aware fs call this func from their ->setattr()?
22763+ */
0b2a12c6
JR
22764+ if (!err && (ia->ia_valid & ATTR_MODE)) {
22765+ h_userns = mnt_user_ns(a->h_path.mnt);
22766+ err = vfsub_acl_chmod(h_userns, a->h_inode, ia->ia_mode);
22767+ }
4a4d8108
AM
22768+ if (!err)
22769+ au_cpup_attr_changeable(inode);
1308ab2a 22770+
4f0767ce 22771+out_unlock:
febd17d6 22772+ inode_unlock(a->h_inode);
4a4d8108 22773+ au_unpin(&a->pin);
027c5e7a 22774+ if (unlikely(err))
5afbbe0d 22775+ au_update_dbtop(dentry);
4f0767ce 22776+out_dentry:
4a4d8108
AM
22777+ di_write_unlock(dentry);
22778+ if (file) {
22779+ fi_write_unlock(file);
22780+ ia->ia_file = file;
22781+ ia->ia_valid |= ATTR_FILE;
22782+ }
4f0767ce 22783+out_si:
4a4d8108 22784+ si_read_unlock(sb);
e49829fe 22785+out_kfree:
9f237c51 22786+ au_kfree_rcu(a);
4f0767ce 22787+out:
4a4d8108
AM
22788+ AuTraceErr(err);
22789+ return err;
1facf9fc 22790+}
22791+
c1595e42
JR
22792+#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
22793+static int au_h_path_to_set_attr(struct dentry *dentry,
22794+ struct au_icpup_args *a, struct path *h_path)
22795+{
22796+ int err;
22797+ struct super_block *sb;
22798+
22799+ sb = dentry->d_sb;
22800+ a->udba = au_opt_udba(sb);
22801+ /* no d_unlinked(), to set UDBA_NONE for root */
22802+ if (d_unhashed(dentry))
22803+ a->udba = AuOpt_UDBA_NONE;
22804+ if (a->udba != AuOpt_UDBA_NONE) {
22805+ AuDebugOn(IS_ROOT(dentry));
22806+ err = au_reval_for_attr(dentry, au_sigen(sb));
22807+ if (unlikely(err))
22808+ goto out;
22809+ }
22810+ err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
22811+ if (unlikely(err < 0))
22812+ goto out;
22813+
22814+ h_path->dentry = a->h_path.dentry;
22815+ h_path->mnt = au_sbr_mnt(sb, a->btgt);
22816+
22817+out:
22818+ return err;
22819+}
22820+
f2c43d5f
AM
22821+ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
22822+ struct au_sxattr *arg)
c1595e42
JR
22823+{
22824+ int err;
22825+ struct path h_path;
22826+ struct super_block *sb;
22827+ struct au_icpup_args *a;
5afbbe0d 22828+ struct inode *h_inode;
0b2a12c6 22829+ struct user_namespace *h_userns;
c1595e42 22830+
c1595e42
JR
22831+ IMustLock(inode);
22832+
22833+ err = -ENOMEM;
22834+ a = kzalloc(sizeof(*a), GFP_NOFS);
22835+ if (unlikely(!a))
22836+ goto out;
22837+
22838+ sb = dentry->d_sb;
22839+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
22840+ if (unlikely(err))
22841+ goto out_kfree;
22842+
22843+ h_path.dentry = NULL; /* silence gcc */
22844+ di_write_lock_child(dentry);
22845+ err = au_h_path_to_set_attr(dentry, a, &h_path);
22846+ if (unlikely(err))
22847+ goto out_di;
0b2a12c6 22848+ h_userns = mnt_user_ns(h_path.mnt);
c1595e42 22849+
febd17d6 22850+ inode_unlock(a->h_inode);
c1595e42
JR
22851+ switch (arg->type) {
22852+ case AU_XATTR_SET:
5afbbe0d 22853+ AuDebugOn(d_is_negative(h_path.dentry));
0b2a12c6 22854+ err = vfsub_setxattr(h_userns, h_path.dentry,
c1595e42
JR
22855+ arg->u.set.name, arg->u.set.value,
22856+ arg->u.set.size, arg->u.set.flags);
22857+ break;
c1595e42
JR
22858+ case AU_ACL_SET:
22859+ err = -EOPNOTSUPP;
5527c038 22860+ h_inode = d_inode(h_path.dentry);
0b2a12c6 22861+ if (h_inode->i_op->set_acl) {
f2c43d5f 22862+ /* this will call posix_acl_update_mode */
0b2a12c6 22863+ err = h_inode->i_op->set_acl(h_userns, h_inode,
c1595e42
JR
22864+ arg->u.acl_set.acl,
22865+ arg->u.acl_set.type);
0b2a12c6 22866+ }
c1595e42
JR
22867+ break;
22868+ }
22869+ if (!err)
22870+ au_cpup_attr_timesizes(inode);
22871+
22872+ au_unpin(&a->pin);
22873+ if (unlikely(err))
5afbbe0d 22874+ au_update_dbtop(dentry);
c1595e42
JR
22875+
22876+out_di:
22877+ di_write_unlock(dentry);
22878+ si_read_unlock(sb);
22879+out_kfree:
9f237c51 22880+ au_kfree_rcu(a);
c1595e42
JR
22881+out:
22882+ AuTraceErr(err);
22883+ return err;
22884+}
22885+#endif
22886+
4a4d8108
AM
22887+static void au_refresh_iattr(struct inode *inode, struct kstat *st,
22888+ unsigned int nlink)
1facf9fc 22889+{
9dbd164d
AM
22890+ unsigned int n;
22891+
4a4d8108 22892+ inode->i_mode = st->mode;
86dc4139
AM
22893+ /* don't i_[ug]id_write() here */
22894+ inode->i_uid = st->uid;
22895+ inode->i_gid = st->gid;
4a4d8108
AM
22896+ inode->i_atime = st->atime;
22897+ inode->i_mtime = st->mtime;
22898+ inode->i_ctime = st->ctime;
1facf9fc 22899+
4a4d8108
AM
22900+ au_cpup_attr_nlink(inode, /*force*/0);
22901+ if (S_ISDIR(inode->i_mode)) {
9dbd164d
AM
22902+ n = inode->i_nlink;
22903+ n -= nlink;
22904+ n += st->nlink;
f6b6e03d 22905+ smp_mb(); /* for i_nlink */
7eafdf33 22906+ /* 0 can happen */
92d182d2 22907+ set_nlink(inode, n);
4a4d8108 22908+ }
1facf9fc 22909+
4a4d8108
AM
22910+ spin_lock(&inode->i_lock);
22911+ inode->i_blocks = st->blocks;
22912+ i_size_write(inode, st->size);
22913+ spin_unlock(&inode->i_lock);
1facf9fc 22914+}
22915+
c1595e42 22916+/*
f2c43d5f 22917+ * common routine for aufs_getattr() and au_getxattr().
c1595e42
JR
22918+ * returns zero or negative (an error).
22919+ * @dentry will be read-locked in success.
22920+ */
42b5c33a
AM
22921+int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
22922+ struct path *h_path, int locked)
1facf9fc 22923+{
4a4d8108 22924+ int err;
076b876e 22925+ unsigned int mnt_flags, sigen;
c1595e42 22926+ unsigned char udba_none;
4a4d8108 22927+ aufs_bindex_t bindex;
4a4d8108 22928+ struct super_block *sb, *h_sb;
1facf9fc 22929+
c1595e42
JR
22930+ h_path->mnt = NULL;
22931+ h_path->dentry = NULL;
22932+
22933+ err = 0;
4a4d8108 22934+ sb = dentry->d_sb;
4a4d8108
AM
22935+ mnt_flags = au_mntflags(sb);
22936+ udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
1facf9fc 22937+
a2654f78
AM
22938+ if (unlikely(locked))
22939+ goto body; /* skip locking dinfo */
22940+
4a4d8108 22941+ /* support fstat(2) */
027c5e7a 22942+ if (!d_unlinked(dentry) && !udba_none) {
076b876e 22943+ sigen = au_sigen(sb);
027c5e7a
AM
22944+ err = au_digen_test(dentry, sigen);
22945+ if (!err) {
4a4d8108 22946+ di_read_lock_child(dentry, AuLock_IR);
027c5e7a 22947+ err = au_dbrange_test(dentry);
c1595e42
JR
22948+ if (unlikely(err)) {
22949+ di_read_unlock(dentry, AuLock_IR);
22950+ goto out;
22951+ }
027c5e7a 22952+ } else {
4a4d8108
AM
22953+ AuDebugOn(IS_ROOT(dentry));
22954+ di_write_lock_child(dentry);
027c5e7a
AM
22955+ err = au_dbrange_test(dentry);
22956+ if (!err)
22957+ err = au_reval_for_attr(dentry, sigen);
c1595e42
JR
22958+ if (!err)
22959+ di_downgrade_lock(dentry, AuLock_IR);
22960+ else {
22961+ di_write_unlock(dentry);
22962+ goto out;
22963+ }
4a4d8108
AM
22964+ }
22965+ } else
22966+ di_read_lock_child(dentry, AuLock_IR);
1facf9fc 22967+
a2654f78 22968+body:
42b5c33a
AM
22969+ if (!inode) {
22970+ inode = d_inode(dentry);
22971+ if (unlikely(!inode))
22972+ goto out;
22973+ }
5afbbe0d 22974+ bindex = au_ibtop(inode);
c1595e42
JR
22975+ h_path->mnt = au_sbr_mnt(sb, bindex);
22976+ h_sb = h_path->mnt->mnt_sb;
22977+ if (!force
22978+ && !au_test_fs_bad_iattr(h_sb)
22979+ && udba_none)
22980+ goto out; /* success */
1facf9fc 22981+
5afbbe0d 22982+ if (au_dbtop(dentry) == bindex)
c1595e42 22983+ h_path->dentry = au_h_dptr(dentry, bindex);
4a4d8108 22984+ else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
c1595e42
JR
22985+ h_path->dentry = au_plink_lkup(inode, bindex);
22986+ if (IS_ERR(h_path->dentry))
22987+ /* pretending success */
22988+ h_path->dentry = NULL;
22989+ else
22990+ dput(h_path->dentry);
4a4d8108 22991+ }
c1595e42
JR
22992+
22993+out:
22994+ return err;
22995+}
22996+
0b2a12c6
JR
22997+static int aufs_getattr(struct user_namespace *userns, const struct path *path,
22998+ struct kstat *st, u32 request, unsigned int query)
c1595e42
JR
22999+{
23000+ int err;
23001+ unsigned char positive;
23002+ struct path h_path;
521ced18 23003+ struct dentry *dentry;
c1595e42
JR
23004+ struct inode *inode;
23005+ struct super_block *sb;
23006+
521ced18 23007+ dentry = path->dentry;
5527c038 23008+ inode = d_inode(dentry);
c1595e42
JR
23009+ sb = dentry->d_sb;
23010+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
23011+ if (unlikely(err))
23012+ goto out;
42b5c33a
AM
23013+ err = au_h_path_getattr(dentry, /*inode*/NULL, /*force*/0, &h_path,
23014+ /*locked*/0);
c1595e42
JR
23015+ if (unlikely(err))
23016+ goto out_si;
c06a8ce3 23017+ if (unlikely(!h_path.dentry))
c1595e42 23018+ /* illegally overlapped or something */
4a4d8108
AM
23019+ goto out_fill; /* pretending success */
23020+
5527c038 23021+ positive = d_is_positive(h_path.dentry);
4a4d8108 23022+ if (positive)
521ced18
JR
23023+ /* no vfsub version */
23024+ err = vfs_getattr(&h_path, st, request, query);
4a4d8108
AM
23025+ if (!err) {
23026+ if (positive)
c06a8ce3 23027+ au_refresh_iattr(inode, st,
5527c038 23028+ d_inode(h_path.dentry)->i_nlink);
4a4d8108 23029+ goto out_fill; /* success */
1facf9fc 23030+ }
7f207e10 23031+ AuTraceErr(err);
c1595e42 23032+ goto out_di;
4a4d8108 23033+
4f0767ce 23034+out_fill:
0b2a12c6 23035+ generic_fillattr(userns, inode, st);
c1595e42 23036+out_di:
4a4d8108 23037+ di_read_unlock(dentry, AuLock_IR);
c1595e42 23038+out_si:
4a4d8108 23039+ si_read_unlock(sb);
7f207e10
AM
23040+out:
23041+ AuTraceErr(err);
4a4d8108 23042+ return err;
1facf9fc 23043+}
23044+
23045+/* ---------------------------------------------------------------------- */
23046+
febd17d6
JR
23047+static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
23048+ struct delayed_call *done)
4a4d8108 23049+{
c2c0f25c 23050+ const char *ret;
c2c0f25c 23051+ struct dentry *h_dentry;
febd17d6 23052+ struct inode *h_inode;
4a4d8108 23053+ int err;
c2c0f25c 23054+ aufs_bindex_t bindex;
1facf9fc 23055+
79b8bda9 23056+ ret = NULL; /* suppress a warning */
febd17d6
JR
23057+ err = -ECHILD;
23058+ if (!dentry)
23059+ goto out;
23060+
027c5e7a
AM
23061+ err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
23062+ if (unlikely(err))
c2c0f25c 23063+ goto out;
027c5e7a
AM
23064+
23065+ err = au_d_hashed_positive(dentry);
c2c0f25c
AM
23066+ if (unlikely(err))
23067+ goto out_unlock;
23068+
23069+ err = -EINVAL;
23070+ inode = d_inode(dentry);
5afbbe0d 23071+ bindex = au_ibtop(inode);
c2c0f25c 23072+ h_inode = au_h_iptr(inode, bindex);
febd17d6 23073+ if (unlikely(!h_inode->i_op->get_link))
c2c0f25c
AM
23074+ goto out_unlock;
23075+
23076+ err = -EBUSY;
23077+ h_dentry = NULL;
5afbbe0d 23078+ if (au_dbtop(dentry) <= bindex) {
c2c0f25c
AM
23079+ h_dentry = au_h_dptr(dentry, bindex);
23080+ if (h_dentry)
23081+ dget(h_dentry);
027c5e7a 23082+ }
c2c0f25c
AM
23083+ if (!h_dentry) {
23084+ h_dentry = d_find_any_alias(h_inode);
23085+ if (IS_ERR(h_dentry)) {
23086+ err = PTR_ERR(h_dentry);
febd17d6 23087+ goto out_unlock;
c2c0f25c
AM
23088+ }
23089+ }
23090+ if (unlikely(!h_dentry))
febd17d6 23091+ goto out_unlock;
1facf9fc 23092+
c2c0f25c 23093+ err = 0;
062440b3 23094+ AuDbg("%ps\n", h_inode->i_op->get_link);
c2c0f25c 23095+ AuDbgDentry(h_dentry);
f2c43d5f 23096+ ret = vfs_get_link(h_dentry, done);
c2c0f25c 23097+ dput(h_dentry);
febd17d6
JR
23098+ if (IS_ERR(ret))
23099+ err = PTR_ERR(ret);
c2c0f25c 23100+
c2c0f25c
AM
23101+out_unlock:
23102+ aufs_read_unlock(dentry, AuLock_IR);
4f0767ce 23103+out:
c2c0f25c
AM
23104+ if (unlikely(err))
23105+ ret = ERR_PTR(err);
23106+ AuTraceErrPtr(ret);
23107+ return ret;
4a4d8108 23108+}
1facf9fc 23109+
4a4d8108 23110+/* ---------------------------------------------------------------------- */
1facf9fc 23111+
e2f27e51
AM
23112+static int au_is_special(struct inode *inode)
23113+{
23114+ return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
23115+}
23116+
acd2b654
AM
23117+static int aufs_update_time(struct inode *inode, struct timespec64 *ts,
23118+ int flags)
4a4d8108 23119+{
0c3ec466 23120+ int err;
e2f27e51 23121+ aufs_bindex_t bindex;
0c3ec466
AM
23122+ struct super_block *sb;
23123+ struct inode *h_inode;
e2f27e51 23124+ struct vfsmount *h_mnt;
0c3ec466
AM
23125+
23126+ sb = inode->i_sb;
e2f27e51
AM
23127+ WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
23128+ "unexpected s_flags 0x%lx", sb->s_flags);
23129+
0c3ec466
AM
23130+ /* mmap_sem might be acquired already, cf. aufs_mmap() */
23131+ lockdep_off();
23132+ si_read_lock(sb, AuLock_FLUSH);
23133+ ii_write_lock_child(inode);
e2f27e51
AM
23134+
23135+ err = 0;
23136+ bindex = au_ibtop(inode);
23137+ h_inode = au_h_iptr(inode, bindex);
23138+ if (!au_test_ro(sb, bindex, inode)) {
23139+ h_mnt = au_sbr_mnt(sb, bindex);
23140+ err = vfsub_mnt_want_write(h_mnt);
23141+ if (!err) {
23142+ err = vfsub_update_time(h_inode, ts, flags);
23143+ vfsub_mnt_drop_write(h_mnt);
23144+ }
23145+ } else if (au_is_special(h_inode)) {
23146+ /*
23147+ * Never copy-up here.
23148+ * These special files may already be opened and used for
23149+ * communicating. If we copied it up, then the communication
23150+ * would be corrupted.
23151+ */
23152+ AuWarn1("timestamps for i%lu are ignored "
23153+ "since it is on readonly branch (hi%lu).\n",
23154+ inode->i_ino, h_inode->i_ino);
23155+ } else if (flags & ~S_ATIME) {
23156+ err = -EIO;
23157+ AuIOErr1("unexpected flags 0x%x\n", flags);
23158+ AuDebugOn(1);
23159+ }
23160+
38d290e6
JR
23161+ if (!err)
23162+ au_cpup_attr_timesizes(inode);
0c3ec466
AM
23163+ ii_write_unlock(inode);
23164+ si_read_unlock(sb);
23165+ lockdep_on();
38d290e6
JR
23166+
23167+ if (!err && (flags & S_VERSION))
23168+ inode_inc_iversion(inode);
23169+
0c3ec466 23170+ return err;
4a4d8108 23171+}
1facf9fc 23172+
4a4d8108 23173+/* ---------------------------------------------------------------------- */
1308ab2a 23174+
b95c5147
AM
23175+/* no getattr version will be set by module.c:aufs_init() */
23176+struct inode_operations aufs_iop_nogetattr[AuIop_Last],
23177+ aufs_iop[] = {
23178+ [AuIop_SYMLINK] = {
23179+ .permission = aufs_permission,
c1595e42 23180+#ifdef CONFIG_FS_POSIX_ACL
b95c5147
AM
23181+ .get_acl = aufs_get_acl,
23182+ .set_acl = aufs_set_acl, /* unsupport for symlink? */
c1595e42
JR
23183+#endif
23184+
b95c5147
AM
23185+ .setattr = aufs_setattr,
23186+ .getattr = aufs_getattr,
0c3ec466 23187+
c1595e42 23188+#ifdef CONFIG_AUFS_XATTR
b95c5147 23189+ .listxattr = aufs_listxattr,
c1595e42
JR
23190+#endif
23191+
0dcfbb52 23192+ .get_link = aufs_get_link
0c3ec466 23193+
b95c5147
AM
23194+ /* .update_time = aufs_update_time */
23195+ },
23196+ [AuIop_DIR] = {
23197+ .create = aufs_create,
23198+ .lookup = aufs_lookup,
23199+ .link = aufs_link,
23200+ .unlink = aufs_unlink,
23201+ .symlink = aufs_symlink,
23202+ .mkdir = aufs_mkdir,
23203+ .rmdir = aufs_rmdir,
23204+ .mknod = aufs_mknod,
23205+ .rename = aufs_rename,
23206+
23207+ .permission = aufs_permission,
c1595e42 23208+#ifdef CONFIG_FS_POSIX_ACL
b95c5147
AM
23209+ .get_acl = aufs_get_acl,
23210+ .set_acl = aufs_set_acl,
c1595e42
JR
23211+#endif
23212+
b95c5147
AM
23213+ .setattr = aufs_setattr,
23214+ .getattr = aufs_getattr,
0c3ec466 23215+
c1595e42 23216+#ifdef CONFIG_AUFS_XATTR
b95c5147 23217+ .listxattr = aufs_listxattr,
c1595e42
JR
23218+#endif
23219+
b95c5147
AM
23220+ .update_time = aufs_update_time,
23221+ .atomic_open = aufs_atomic_open,
23222+ .tmpfile = aufs_tmpfile
23223+ },
23224+ [AuIop_OTHER] = {
23225+ .permission = aufs_permission,
c1595e42 23226+#ifdef CONFIG_FS_POSIX_ACL
b95c5147
AM
23227+ .get_acl = aufs_get_acl,
23228+ .set_acl = aufs_set_acl,
c1595e42
JR
23229+#endif
23230+
b95c5147
AM
23231+ .setattr = aufs_setattr,
23232+ .getattr = aufs_getattr,
0c3ec466 23233+
c1595e42 23234+#ifdef CONFIG_AUFS_XATTR
b95c5147 23235+ .listxattr = aufs_listxattr,
c1595e42
JR
23236+#endif
23237+
b95c5147
AM
23238+ .update_time = aufs_update_time
23239+ }
4a4d8108 23240+};
7f207e10 23241diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
eca34b5c 23242--- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 23243+++ linux/fs/aufs/i_op_del.c 2022-11-05 23:02:18.965889284 +0100
758e9dad 23244@@ -0,0 +1,522 @@
cd7a4cd9 23245+// SPDX-License-Identifier: GPL-2.0
1facf9fc 23246+/*
0dcfbb52 23247+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 23248+ *
0dcfbb52 23249+ * This program is free software; you can redistribute it and/or modify
1facf9fc 23250+ * it under the terms of the GNU General Public License as published by
23251+ * the Free Software Foundation; either version 2 of the License, or
23252+ * (at your option) any later version.
dece6358
AM
23253+ *
23254+ * This program is distributed in the hope that it will be useful,
23255+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
23256+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23257+ * GNU General Public License for more details.
23258+ *
23259+ * You should have received a copy of the GNU General Public License
523b37e3 23260+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 23261+ */
23262+
23263+/*
4a4d8108 23264+ * inode operations (del entry)
1308ab2a 23265+ */
dece6358 23266+
eca801bf 23267+#include <linux/iversion.h>
1308ab2a 23268+#include "aufs.h"
dece6358 23269+
4a4d8108
AM
23270+/*
23271+ * decide if a new whiteout for @dentry is necessary or not.
23272+ * when it is necessary, prepare the parent dir for the upper branch whose
23273+ * branch index is @bcpup for creation. the actual creation of the whiteout will
23274+ * be done by caller.
23275+ * return value:
23276+ * 0: wh is unnecessary
23277+ * plus: wh is necessary
23278+ * minus: error
23279+ */
23280+int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
1308ab2a 23281+{
4a4d8108 23282+ int need_wh, err;
5afbbe0d 23283+ aufs_bindex_t btop;
4a4d8108 23284+ struct super_block *sb;
dece6358 23285+
4a4d8108 23286+ sb = dentry->d_sb;
5afbbe0d 23287+ btop = au_dbtop(dentry);
4a4d8108 23288+ if (*bcpup < 0) {
5afbbe0d
AM
23289+ *bcpup = btop;
23290+ if (au_test_ro(sb, btop, d_inode(dentry))) {
4a4d8108
AM
23291+ err = AuWbrCopyup(au_sbi(sb), dentry);
23292+ *bcpup = err;
23293+ if (unlikely(err < 0))
23294+ goto out;
23295+ }
23296+ } else
5afbbe0d 23297+ AuDebugOn(btop < *bcpup
5527c038 23298+ || au_test_ro(sb, *bcpup, d_inode(dentry)));
5afbbe0d 23299+ AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
1308ab2a 23300+
5afbbe0d 23301+ if (*bcpup != btop) {
4a4d8108
AM
23302+ err = au_cpup_dirs(dentry, *bcpup);
23303+ if (unlikely(err))
23304+ goto out;
23305+ need_wh = 1;
23306+ } else {
027c5e7a 23307+ struct au_dinfo *dinfo, *tmp;
4a4d8108 23308+
027c5e7a
AM
23309+ need_wh = -ENOMEM;
23310+ dinfo = au_di(dentry);
23311+ tmp = au_di_alloc(sb, AuLsc_DI_TMP);
23312+ if (tmp) {
23313+ au_di_cp(tmp, dinfo);
23314+ au_di_swap(tmp, dinfo);
23315+ /* returns the number of positive dentries */
5afbbe0d
AM
23316+ need_wh = au_lkup_dentry(dentry, btop + 1,
23317+ /* AuLkup_IGNORE_PERM */ 0);
027c5e7a
AM
23318+ au_di_swap(tmp, dinfo);
23319+ au_rw_write_unlock(&tmp->di_rwsem);
23320+ au_di_free(tmp);
4a4d8108
AM
23321+ }
23322+ }
23323+ AuDbg("need_wh %d\n", need_wh);
23324+ err = need_wh;
23325+
4f0767ce 23326+out:
4a4d8108 23327+ return err;
1facf9fc 23328+}
23329+
4a4d8108
AM
23330+/*
23331+ * simple tests for the del-entry operations.
23332+ * following the checks in vfs, plus the parent-child relationship.
23333+ */
23334+int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
23335+ struct dentry *h_parent, int isdir)
1facf9fc 23336+{
4a4d8108
AM
23337+ int err;
23338+ umode_t h_mode;
23339+ struct dentry *h_dentry, *h_latest;
1308ab2a 23340+ struct inode *h_inode;
758e9dad
AM
23341+ struct path h_ppath;
23342+ struct super_block *sb;
23343+ struct au_branch *br;
0b2a12c6 23344+ struct user_namespace *h_userns;
1facf9fc 23345+
4a4d8108 23346+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 23347+ if (d_really_is_positive(dentry)) {
4a4d8108 23348+ err = -ENOENT;
5527c038
JR
23349+ if (unlikely(d_is_negative(h_dentry)))
23350+ goto out;
23351+ h_inode = d_inode(h_dentry);
23352+ if (unlikely(!h_inode->i_nlink))
4a4d8108 23353+ goto out;
1facf9fc 23354+
4a4d8108
AM
23355+ h_mode = h_inode->i_mode;
23356+ if (!isdir) {
23357+ err = -EISDIR;
23358+ if (unlikely(S_ISDIR(h_mode)))
23359+ goto out;
23360+ } else if (unlikely(!S_ISDIR(h_mode))) {
23361+ err = -ENOTDIR;
23362+ goto out;
23363+ }
23364+ } else {
23365+ /* rename(2) case */
23366+ err = -EIO;
5527c038 23367+ if (unlikely(d_is_positive(h_dentry)))
4a4d8108
AM
23368+ goto out;
23369+ }
1facf9fc 23370+
4a4d8108
AM
23371+ err = -ENOENT;
23372+ /* expected parent dir is locked */
23373+ if (unlikely(h_parent != h_dentry->d_parent))
23374+ goto out;
23375+ err = 0;
23376+
23377+ /*
23378+ * rmdir a dir may break the consistency on some filesystem.
23379+ * let's try heavy test.
23380+ */
23381+ err = -EACCES;
758e9dad
AM
23382+ sb = dentry->d_sb;
23383+ br = au_sbr(sb, bindex);
23384+ h_userns = au_br_userns(br);
23385+ if (unlikely(!au_opt_test(au_mntflags(sb), DIRPERM1)
0b2a12c6 23386+ && au_test_h_perm(h_userns, d_inode(h_parent),
076b876e 23387+ MAY_EXEC | MAY_WRITE)))
4a4d8108
AM
23388+ goto out;
23389+
758e9dad
AM
23390+ h_ppath.dentry = h_parent;
23391+ h_ppath.mnt = au_br_mnt(br);
23392+ h_latest = au_sio_lkup_one(h_userns, &dentry->d_name, &h_ppath);
4a4d8108
AM
23393+ err = -EIO;
23394+ if (IS_ERR(h_latest))
23395+ goto out;
23396+ if (h_latest == h_dentry)
23397+ err = 0;
23398+ dput(h_latest);
23399+
4f0767ce 23400+out:
4a4d8108 23401+ return err;
1308ab2a 23402+}
1facf9fc 23403+
4a4d8108
AM
23404+/*
23405+ * decide the branch where we operate for @dentry. the branch index will be set
acd2b654 23406+ * @rbcpup. after deciding it, 'pin' it and store the timestamps of the parent
4a4d8108
AM
23407+ * dir for reverting.
23408+ * when a new whiteout is necessary, create it.
23409+ */
23410+static struct dentry*
23411+lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
23412+ struct au_dtime *dt, struct au_pin *pin)
1308ab2a 23413+{
4a4d8108
AM
23414+ struct dentry *wh_dentry;
23415+ struct super_block *sb;
23416+ struct path h_path;
23417+ int err, need_wh;
23418+ unsigned int udba;
23419+ aufs_bindex_t bcpup;
dece6358 23420+
4a4d8108
AM
23421+ need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
23422+ wh_dentry = ERR_PTR(need_wh);
23423+ if (unlikely(need_wh < 0))
23424+ goto out;
23425+
23426+ sb = dentry->d_sb;
23427+ udba = au_opt_udba(sb);
23428+ bcpup = *rbcpup;
23429+ err = au_pin(pin, dentry, bcpup, udba,
23430+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23431+ wh_dentry = ERR_PTR(err);
23432+ if (unlikely(err))
23433+ goto out;
23434+
23435+ h_path.dentry = au_pinned_h_parent(pin);
23436+ if (udba != AuOpt_UDBA_NONE
5afbbe0d 23437+ && au_dbtop(dentry) == bcpup) {
4a4d8108
AM
23438+ err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
23439+ wh_dentry = ERR_PTR(err);
23440+ if (unlikely(err))
23441+ goto out_unpin;
23442+ }
23443+
23444+ h_path.mnt = au_sbr_mnt(sb, bcpup);
23445+ au_dtime_store(dt, au_pinned_parent(pin), &h_path);
23446+ wh_dentry = NULL;
23447+ if (!need_wh)
23448+ goto out; /* success, no need to create whiteout */
23449+
23450+ wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
23451+ if (IS_ERR(wh_dentry))
23452+ goto out_unpin;
23453+
23454+ /* returns with the parent is locked and wh_dentry is dget-ed */
23455+ goto out; /* success */
23456+
4f0767ce 23457+out_unpin:
4a4d8108 23458+ au_unpin(pin);
4f0767ce 23459+out:
4a4d8108 23460+ return wh_dentry;
1facf9fc 23461+}
23462+
4a4d8108
AM
23463+/*
23464+ * when removing a dir, rename it to a unique temporary whiteout-ed name first
23465+ * in order to be revertible and save time for removing many child whiteouts
23466+ * under the dir.
23467+ * returns 1 when there are too many child whiteout and caller should remove
23468+ * them asynchronously. returns 0 when the number of children is enough small to
23469+ * remove now or the branch fs is a remote fs.
23470+ * otherwise return an error.
23471+ */
23472+static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
23473+ struct au_nhash *whlist, struct inode *dir)
1facf9fc 23474+{
4a4d8108
AM
23475+ int rmdir_later, err, dirwh;
23476+ struct dentry *h_dentry;
23477+ struct super_block *sb;
5527c038 23478+ struct inode *inode;
4a4d8108
AM
23479+
23480+ sb = dentry->d_sb;
23481+ SiMustAnyLock(sb);
23482+ h_dentry = au_h_dptr(dentry, bindex);
23483+ err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
23484+ if (unlikely(err))
23485+ goto out;
23486+
23487+ /* stop monitoring */
5527c038
JR
23488+ inode = d_inode(dentry);
23489+ au_hn_free(au_hi(inode, bindex));
4a4d8108
AM
23490+
23491+ if (!au_test_fs_remote(h_dentry->d_sb)) {
23492+ dirwh = au_sbi(sb)->si_dirwh;
23493+ rmdir_later = (dirwh <= 1);
23494+ if (!rmdir_later)
23495+ rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
23496+ dirwh);
23497+ if (rmdir_later)
23498+ return rmdir_later;
23499+ }
1facf9fc 23500+
4a4d8108
AM
23501+ err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
23502+ if (unlikely(err)) {
523b37e3
AM
23503+ AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
23504+ h_dentry, bindex, err);
4a4d8108
AM
23505+ err = 0;
23506+ }
dece6358 23507+
4f0767ce 23508+out:
4a4d8108
AM
23509+ AuTraceErr(err);
23510+ return err;
23511+}
1308ab2a 23512+
4a4d8108
AM
23513+/*
23514+ * final procedure for deleting a entry.
23515+ * maintain dentry and iattr.
23516+ */
23517+static void epilog(struct inode *dir, struct dentry *dentry,
23518+ aufs_bindex_t bindex)
23519+{
23520+ struct inode *inode;
1308ab2a 23521+
5527c038 23522+ inode = d_inode(dentry);
4a4d8108
AM
23523+ d_drop(dentry);
23524+ inode->i_ctime = dir->i_ctime;
1308ab2a 23525+
b912730e 23526+ au_dir_ts(dir, bindex);
be118d29 23527+ inode_inc_iversion(dir);
1facf9fc 23528+}
23529+
4a4d8108
AM
23530+/*
23531+ * when an error happened, remove the created whiteout and revert everything.
23532+ */
7f207e10
AM
23533+static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
23534+ aufs_bindex_t bwh, struct dentry *wh_dentry,
23535+ struct dentry *dentry, struct au_dtime *dt)
1facf9fc 23536+{
4a4d8108
AM
23537+ int rerr;
23538+ struct path h_path = {
23539+ .dentry = wh_dentry,
7f207e10 23540+ .mnt = au_sbr_mnt(dir->i_sb, bindex)
4a4d8108 23541+ };
dece6358 23542+
7f207e10 23543+ rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
4a4d8108
AM
23544+ if (!rerr) {
23545+ au_set_dbwh(dentry, bwh);
23546+ au_dtime_revert(dt);
23547+ return 0;
23548+ }
dece6358 23549+
523b37e3 23550+ AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
4a4d8108 23551+ return -EIO;
1facf9fc 23552+}
23553+
4a4d8108 23554+/* ---------------------------------------------------------------------- */
1facf9fc 23555+
4a4d8108 23556+int aufs_unlink(struct inode *dir, struct dentry *dentry)
1308ab2a 23557+{
4a4d8108 23558+ int err;
5afbbe0d 23559+ aufs_bindex_t bwh, bindex, btop;
523b37e3 23560+ struct inode *inode, *h_dir, *delegated;
4a4d8108 23561+ struct dentry *parent, *wh_dentry;
acd2b654 23562+ /* to reduce stack size */
c2b27bf2
AM
23563+ struct {
23564+ struct au_dtime dt;
23565+ struct au_pin pin;
23566+ struct path h_path;
23567+ } *a;
1facf9fc 23568+
4a4d8108 23569+ IMustLock(dir);
027c5e7a 23570+
c2b27bf2
AM
23571+ err = -ENOMEM;
23572+ a = kmalloc(sizeof(*a), GFP_NOFS);
23573+ if (unlikely(!a))
23574+ goto out;
23575+
027c5e7a
AM
23576+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
23577+ if (unlikely(err))
c2b27bf2 23578+ goto out_free;
027c5e7a
AM
23579+ err = au_d_hashed_positive(dentry);
23580+ if (unlikely(err))
23581+ goto out_unlock;
5527c038 23582+ inode = d_inode(dentry);
4a4d8108 23583+ IMustLock(inode);
027c5e7a 23584+ err = -EISDIR;
2000de60 23585+ if (unlikely(d_is_dir(dentry)))
027c5e7a 23586+ goto out_unlock; /* possible? */
1facf9fc 23587+
5afbbe0d 23588+ btop = au_dbtop(dentry);
4a4d8108
AM
23589+ bwh = au_dbwh(dentry);
23590+ bindex = -1;
027c5e7a
AM
23591+ parent = dentry->d_parent; /* dir inode is locked */
23592+ di_write_lock_parent(parent);
c2b27bf2
AM
23593+ wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
23594+ &a->pin);
4a4d8108
AM
23595+ err = PTR_ERR(wh_dentry);
23596+ if (IS_ERR(wh_dentry))
027c5e7a 23597+ goto out_parent;
1facf9fc 23598+
5afbbe0d
AM
23599+ a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
23600+ a->h_path.dentry = au_h_dptr(dentry, btop);
c2b27bf2 23601+ dget(a->h_path.dentry);
5afbbe0d 23602+ if (bindex == btop) {
c2b27bf2 23603+ h_dir = au_pinned_h_dir(&a->pin);
523b37e3
AM
23604+ delegated = NULL;
23605+ err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
23606+ if (unlikely(err == -EWOULDBLOCK)) {
23607+ pr_warn("cannot retry for NFSv4 delegation"
23608+ " for an internal unlink\n");
23609+ iput(delegated);
23610+ }
4a4d8108
AM
23611+ } else {
23612+ /* dir inode is locked */
5527c038 23613+ h_dir = d_inode(wh_dentry->d_parent);
4a4d8108
AM
23614+ IMustLock(h_dir);
23615+ err = 0;
23616+ }
dece6358 23617+
4a4d8108 23618+ if (!err) {
7f207e10 23619+ vfsub_drop_nlink(inode);
4a4d8108
AM
23620+ epilog(dir, dentry, bindex);
23621+
23622+ /* update target timestamps */
5afbbe0d 23623+ if (bindex == btop) {
c2b27bf2
AM
23624+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
23625+ /*ignore*/
5527c038 23626+ inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
4a4d8108
AM
23627+ } else
23628+ /* todo: this timestamp may be reverted later */
23629+ inode->i_ctime = h_dir->i_ctime;
027c5e7a 23630+ goto out_unpin; /* success */
1facf9fc 23631+ }
23632+
4a4d8108
AM
23633+ /* revert */
23634+ if (wh_dentry) {
23635+ int rerr;
23636+
c2b27bf2
AM
23637+ rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
23638+ &a->dt);
4a4d8108
AM
23639+ if (rerr)
23640+ err = rerr;
dece6358 23641+ }
1facf9fc 23642+
027c5e7a 23643+out_unpin:
c2b27bf2 23644+ au_unpin(&a->pin);
4a4d8108 23645+ dput(wh_dentry);
c2b27bf2 23646+ dput(a->h_path.dentry);
027c5e7a 23647+out_parent:
4a4d8108 23648+ di_write_unlock(parent);
027c5e7a 23649+out_unlock:
4a4d8108 23650+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2 23651+out_free:
9f237c51 23652+ au_kfree_rcu(a);
027c5e7a 23653+out:
4a4d8108 23654+ return err;
dece6358
AM
23655+}
23656+
4a4d8108 23657+int aufs_rmdir(struct inode *dir, struct dentry *dentry)
1308ab2a 23658+{
4a4d8108 23659+ int err, rmdir_later;
5afbbe0d 23660+ aufs_bindex_t bwh, bindex, btop;
4a4d8108
AM
23661+ struct inode *inode;
23662+ struct dentry *parent, *wh_dentry, *h_dentry;
23663+ struct au_whtmp_rmdir *args;
acd2b654 23664+ /* to reduce stack size */
c2b27bf2
AM
23665+ struct {
23666+ struct au_dtime dt;
23667+ struct au_pin pin;
23668+ } *a;
1facf9fc 23669+
4a4d8108 23670+ IMustLock(dir);
027c5e7a 23671+
c2b27bf2
AM
23672+ err = -ENOMEM;
23673+ a = kmalloc(sizeof(*a), GFP_NOFS);
23674+ if (unlikely(!a))
23675+ goto out;
23676+
027c5e7a
AM
23677+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
23678+ if (unlikely(err))
c2b27bf2 23679+ goto out_free;
53392da6
AM
23680+ err = au_alive_dir(dentry);
23681+ if (unlikely(err))
027c5e7a 23682+ goto out_unlock;
5527c038 23683+ inode = d_inode(dentry);
4a4d8108 23684+ IMustLock(inode);
027c5e7a 23685+ err = -ENOTDIR;
2000de60 23686+ if (unlikely(!d_is_dir(dentry)))
027c5e7a 23687+ goto out_unlock; /* possible? */
dece6358 23688+
4a4d8108
AM
23689+ err = -ENOMEM;
23690+ args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
23691+ if (unlikely(!args))
23692+ goto out_unlock;
dece6358 23693+
4a4d8108
AM
23694+ parent = dentry->d_parent; /* dir inode is locked */
23695+ di_write_lock_parent(parent);
23696+ err = au_test_empty(dentry, &args->whlist);
23697+ if (unlikely(err))
027c5e7a 23698+ goto out_parent;
1facf9fc 23699+
5afbbe0d 23700+ btop = au_dbtop(dentry);
4a4d8108
AM
23701+ bwh = au_dbwh(dentry);
23702+ bindex = -1;
c2b27bf2
AM
23703+ wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
23704+ &a->pin);
4a4d8108
AM
23705+ err = PTR_ERR(wh_dentry);
23706+ if (IS_ERR(wh_dentry))
027c5e7a 23707+ goto out_parent;
1facf9fc 23708+
5afbbe0d 23709+ h_dentry = au_h_dptr(dentry, btop);
4a4d8108
AM
23710+ dget(h_dentry);
23711+ rmdir_later = 0;
5afbbe0d
AM
23712+ if (bindex == btop) {
23713+ err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
4a4d8108
AM
23714+ if (err > 0) {
23715+ rmdir_later = err;
23716+ err = 0;
23717+ }
23718+ } else {
23719+ /* stop monitoring */
5afbbe0d 23720+ au_hn_free(au_hi(inode, btop));
4a4d8108
AM
23721+
23722+ /* dir inode is locked */
5527c038 23723+ IMustLock(d_inode(wh_dentry->d_parent));
1facf9fc 23724+ err = 0;
23725+ }
23726+
4a4d8108 23727+ if (!err) {
027c5e7a 23728+ vfsub_dead_dir(inode);
4a4d8108
AM
23729+ au_set_dbdiropq(dentry, -1);
23730+ epilog(dir, dentry, bindex);
1308ab2a 23731+
4a4d8108 23732+ if (rmdir_later) {
5afbbe0d 23733+ au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
4a4d8108
AM
23734+ args = NULL;
23735+ }
1308ab2a 23736+
4a4d8108 23737+ goto out_unpin; /* success */
1facf9fc 23738+ }
23739+
4a4d8108
AM
23740+ /* revert */
23741+ AuLabel(revert);
23742+ if (wh_dentry) {
23743+ int rerr;
1308ab2a 23744+
c2b27bf2
AM
23745+ rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
23746+ &a->dt);
4a4d8108
AM
23747+ if (rerr)
23748+ err = rerr;
1facf9fc 23749+ }
23750+
4f0767ce 23751+out_unpin:
c2b27bf2 23752+ au_unpin(&a->pin);
4a4d8108
AM
23753+ dput(wh_dentry);
23754+ dput(h_dentry);
027c5e7a 23755+out_parent:
4a4d8108
AM
23756+ di_write_unlock(parent);
23757+ if (args)
23758+ au_whtmp_rmdir_free(args);
4f0767ce 23759+out_unlock:
4a4d8108 23760+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2 23761+out_free:
9f237c51 23762+ au_kfree_rcu(a);
4f0767ce 23763+out:
4a4d8108
AM
23764+ AuTraceErr(err);
23765+ return err;
dece6358 23766+}
7f207e10 23767diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
eca34b5c 23768--- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 23769+++ linux/fs/aufs/i_op_ren.c 2022-11-05 23:02:18.965889284 +0100
758e9dad 23770@@ -0,0 +1,1257 @@
cd7a4cd9 23771+// SPDX-License-Identifier: GPL-2.0
1facf9fc 23772+/*
0dcfbb52 23773+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 23774+ *
0dcfbb52 23775+ * This program is free software; you can redistribute it and/or modify
1facf9fc 23776+ * it under the terms of the GNU General Public License as published by
23777+ * the Free Software Foundation; either version 2 of the License, or
23778+ * (at your option) any later version.
dece6358
AM
23779+ *
23780+ * This program is distributed in the hope that it will be useful,
23781+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
23782+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23783+ * GNU General Public License for more details.
23784+ *
23785+ * You should have received a copy of the GNU General Public License
523b37e3 23786+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 23787+ */
23788+
23789+/*
4a4d8108
AM
23790+ * inode operation (rename entry)
23791+ * todo: this is crazy monster
1facf9fc 23792+ */
23793+
eca801bf 23794+#include <linux/iversion.h>
1facf9fc 23795+#include "aufs.h"
23796+
4a4d8108
AM
23797+enum { AuSRC, AuDST, AuSrcDst };
23798+enum { AuPARENT, AuCHILD, AuParentChild };
1facf9fc 23799+
f2c43d5f
AM
23800+#define AuRen_ISDIR_SRC 1
23801+#define AuRen_ISDIR_DST (1 << 1)
23802+#define AuRen_ISSAMEDIR (1 << 2)
23803+#define AuRen_WHSRC (1 << 3)
23804+#define AuRen_WHDST (1 << 4)
23805+#define AuRen_MNT_WRITE (1 << 5)
23806+#define AuRen_DT_DSTDIR (1 << 6)
23807+#define AuRen_DIROPQ_SRC (1 << 7)
23808+#define AuRen_DIROPQ_DST (1 << 8)
8b6a4947
AM
23809+#define AuRen_DIRREN (1 << 9)
23810+#define AuRen_DROPPED_SRC (1 << 10)
23811+#define AuRen_DROPPED_DST (1 << 11)
4a4d8108 23812+#define au_ftest_ren(flags, name) ((flags) & AuRen_##name)
7f207e10
AM
23813+#define au_fset_ren(flags, name) \
23814+ do { (flags) |= AuRen_##name; } while (0)
23815+#define au_fclr_ren(flags, name) \
23816+ do { (flags) &= ~AuRen_##name; } while (0)
1facf9fc 23817+
8b6a4947
AM
23818+#ifndef CONFIG_AUFS_DIRREN
23819+#undef AuRen_DIRREN
23820+#define AuRen_DIRREN 0
23821+#endif
23822+
4a4d8108
AM
23823+struct au_ren_args {
23824+ struct {
23825+ struct dentry *dentry, *h_dentry, *parent, *h_parent,
23826+ *wh_dentry;
23827+ struct inode *dir, *inode;
f2c43d5f 23828+ struct au_hinode *hdir, *hinode;
4a4d8108 23829+ struct au_dtime dt[AuParentChild];
f2c43d5f 23830+ aufs_bindex_t btop, bdiropq;
4a4d8108 23831+ } sd[AuSrcDst];
1facf9fc 23832+
4a4d8108
AM
23833+#define src_dentry sd[AuSRC].dentry
23834+#define src_dir sd[AuSRC].dir
23835+#define src_inode sd[AuSRC].inode
23836+#define src_h_dentry sd[AuSRC].h_dentry
23837+#define src_parent sd[AuSRC].parent
23838+#define src_h_parent sd[AuSRC].h_parent
23839+#define src_wh_dentry sd[AuSRC].wh_dentry
23840+#define src_hdir sd[AuSRC].hdir
f2c43d5f 23841+#define src_hinode sd[AuSRC].hinode
4a4d8108
AM
23842+#define src_h_dir sd[AuSRC].hdir->hi_inode
23843+#define src_dt sd[AuSRC].dt
5afbbe0d 23844+#define src_btop sd[AuSRC].btop
f2c43d5f 23845+#define src_bdiropq sd[AuSRC].bdiropq
1facf9fc 23846+
4a4d8108
AM
23847+#define dst_dentry sd[AuDST].dentry
23848+#define dst_dir sd[AuDST].dir
23849+#define dst_inode sd[AuDST].inode
23850+#define dst_h_dentry sd[AuDST].h_dentry
23851+#define dst_parent sd[AuDST].parent
23852+#define dst_h_parent sd[AuDST].h_parent
23853+#define dst_wh_dentry sd[AuDST].wh_dentry
23854+#define dst_hdir sd[AuDST].hdir
f2c43d5f 23855+#define dst_hinode sd[AuDST].hinode
4a4d8108
AM
23856+#define dst_h_dir sd[AuDST].hdir->hi_inode
23857+#define dst_dt sd[AuDST].dt
5afbbe0d 23858+#define dst_btop sd[AuDST].btop
f2c43d5f 23859+#define dst_bdiropq sd[AuDST].bdiropq
4a4d8108
AM
23860+
23861+ struct dentry *h_trap;
23862+ struct au_branch *br;
4a4d8108
AM
23863+ struct path h_path;
23864+ struct au_nhash whlist;
f2c43d5f 23865+ aufs_bindex_t btgt, src_bwh;
1facf9fc 23866+
f2c43d5f
AM
23867+ struct {
23868+ unsigned short auren_flags;
23869+ unsigned char flags; /* syscall parameter */
23870+ unsigned char exchange;
23871+ } __packed;
1facf9fc 23872+
4a4d8108
AM
23873+ struct au_whtmp_rmdir *thargs;
23874+ struct dentry *h_dst;
8b6a4947 23875+ struct au_hinode *h_root;
4a4d8108 23876+};
1308ab2a 23877+
4a4d8108 23878+/* ---------------------------------------------------------------------- */
1308ab2a 23879+
4a4d8108
AM
23880+/*
23881+ * functions for reverting.
23882+ * when an error happened in a single rename systemcall, we should revert
79b8bda9 23883+ * everything as if nothing happened.
4a4d8108
AM
23884+ * we don't need to revert the copied-up/down the parent dir since they are
23885+ * harmless.
23886+ */
1facf9fc 23887+
4a4d8108
AM
23888+#define RevertFailure(fmt, ...) do { \
23889+ AuIOErr("revert failure: " fmt " (%d, %d)\n", \
23890+ ##__VA_ARGS__, err, rerr); \
23891+ err = -EIO; \
23892+} while (0)
1facf9fc 23893+
f2c43d5f 23894+static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
1facf9fc 23895+{
4a4d8108 23896+ int rerr;
f2c43d5f
AM
23897+ struct dentry *d;
23898+#define src_or_dst(member) a->sd[idx].member
1facf9fc 23899+
f2c43d5f
AM
23900+ d = src_or_dst(dentry); /* {src,dst}_dentry */
23901+ au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
23902+ rerr = au_diropq_remove(d, a->btgt);
23903+ au_hn_inode_unlock(src_or_dst(hinode));
23904+ au_set_dbdiropq(d, src_or_dst(bdiropq));
4a4d8108 23905+ if (rerr)
f2c43d5f
AM
23906+ RevertFailure("remove diropq %pd", d);
23907+
23908+#undef src_or_dst_
23909+}
23910+
23911+static void au_ren_rev_diropq(int err, struct au_ren_args *a)
23912+{
23913+ if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
23914+ au_ren_do_rev_diropq(err, a, AuSRC);
23915+ if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
23916+ au_ren_do_rev_diropq(err, a, AuDST);
4a4d8108 23917+}
1facf9fc 23918+
4a4d8108
AM
23919+static void au_ren_rev_rename(int err, struct au_ren_args *a)
23920+{
23921+ int rerr;
523b37e3 23922+ struct inode *delegated;
758e9dad
AM
23923+ struct path h_ppath = {
23924+ .dentry = a->src_h_parent,
23925+ .mnt = a->h_path.mnt
23926+ };
1facf9fc 23927+
758e9dad 23928+ a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name, &h_ppath);
4a4d8108
AM
23929+ rerr = PTR_ERR(a->h_path.dentry);
23930+ if (IS_ERR(a->h_path.dentry)) {
523b37e3 23931+ RevertFailure("lkup one %pd", a->src_dentry);
4a4d8108 23932+ return;
1facf9fc 23933+ }
23934+
523b37e3 23935+ delegated = NULL;
4a4d8108
AM
23936+ rerr = vfsub_rename(a->dst_h_dir,
23937+ au_h_dptr(a->src_dentry, a->btgt),
f2c43d5f 23938+ a->src_h_dir, &a->h_path, &delegated, a->flags);
523b37e3
AM
23939+ if (unlikely(rerr == -EWOULDBLOCK)) {
23940+ pr_warn("cannot retry for NFSv4 delegation"
23941+ " for an internal rename\n");
23942+ iput(delegated);
23943+ }
4a4d8108
AM
23944+ d_drop(a->h_path.dentry);
23945+ dput(a->h_path.dentry);
23946+ /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
23947+ if (rerr)
523b37e3 23948+ RevertFailure("rename %pd", a->src_dentry);
1facf9fc 23949+}
23950+
4a4d8108 23951+static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
1facf9fc 23952+{
4a4d8108 23953+ int rerr;
523b37e3 23954+ struct inode *delegated;
758e9dad
AM
23955+ struct path h_ppath = {
23956+ .dentry = a->dst_h_parent,
23957+ .mnt = a->h_path.mnt
23958+ };
dece6358 23959+
758e9dad 23960+ a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name, &h_ppath);
4a4d8108
AM
23961+ rerr = PTR_ERR(a->h_path.dentry);
23962+ if (IS_ERR(a->h_path.dentry)) {
523b37e3 23963+ RevertFailure("lkup one %pd", a->dst_dentry);
4a4d8108
AM
23964+ return;
23965+ }
5527c038 23966+ if (d_is_positive(a->h_path.dentry)) {
4a4d8108
AM
23967+ d_drop(a->h_path.dentry);
23968+ dput(a->h_path.dentry);
23969+ return;
dece6358
AM
23970+ }
23971+
523b37e3
AM
23972+ delegated = NULL;
23973+ rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
f2c43d5f 23974+ &delegated, a->flags);
523b37e3
AM
23975+ if (unlikely(rerr == -EWOULDBLOCK)) {
23976+ pr_warn("cannot retry for NFSv4 delegation"
23977+ " for an internal rename\n");
23978+ iput(delegated);
23979+ }
4a4d8108
AM
23980+ d_drop(a->h_path.dentry);
23981+ dput(a->h_path.dentry);
23982+ if (!rerr)
23983+ au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
23984+ else
523b37e3 23985+ RevertFailure("rename %pd", a->h_dst);
4a4d8108 23986+}
1308ab2a 23987+
4a4d8108
AM
23988+static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
23989+{
23990+ int rerr;
1308ab2a 23991+
4a4d8108
AM
23992+ a->h_path.dentry = a->src_wh_dentry;
23993+ rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
027c5e7a 23994+ au_set_dbwh(a->src_dentry, a->src_bwh);
4a4d8108 23995+ if (rerr)
523b37e3 23996+ RevertFailure("unlink %pd", a->src_wh_dentry);
4a4d8108 23997+}
4a4d8108 23998+#undef RevertFailure
1facf9fc 23999+
1308ab2a 24000+/* ---------------------------------------------------------------------- */
24001+
4a4d8108
AM
24002+/*
24003+ * when we have to copyup the renaming entry, do it with the rename-target name
24004+ * in order to minimize the cost (the later actual rename is unnecessary).
24005+ * otherwise rename it on the target branch.
24006+ */
24007+static int au_ren_or_cpup(struct au_ren_args *a)
1facf9fc 24008+{
dece6358 24009+ int err;
4a4d8108 24010+ struct dentry *d;
523b37e3 24011+ struct inode *delegated;
1facf9fc 24012+
4a4d8108 24013+ d = a->src_dentry;
5afbbe0d 24014+ if (au_dbtop(d) == a->btgt) {
4a4d8108 24015+ a->h_path.dentry = a->dst_h_dentry;
5afbbe0d 24016+ AuDebugOn(au_dbtop(d) != a->btgt);
523b37e3 24017+ delegated = NULL;
4a4d8108 24018+ err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
f2c43d5f
AM
24019+ a->dst_h_dir, &a->h_path, &delegated,
24020+ a->flags);
523b37e3
AM
24021+ if (unlikely(err == -EWOULDBLOCK)) {
24022+ pr_warn("cannot retry for NFSv4 delegation"
24023+ " for an internal rename\n");
24024+ iput(delegated);
24025+ }
c2b27bf2 24026+ } else
86dc4139 24027+ BUG();
1308ab2a 24028+
027c5e7a
AM
24029+ if (!err && a->h_dst)
24030+ /* it will be set to dinfo later */
24031+ dget(a->h_dst);
1facf9fc 24032+
dece6358
AM
24033+ return err;
24034+}
1facf9fc 24035+
4a4d8108
AM
24036+/* cf. aufs_rmdir() */
24037+static int au_ren_del_whtmp(struct au_ren_args *a)
dece6358 24038+{
4a4d8108
AM
24039+ int err;
24040+ struct inode *dir;
1facf9fc 24041+
4a4d8108
AM
24042+ dir = a->dst_dir;
24043+ SiMustAnyLock(dir->i_sb);
24044+ if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
24045+ au_sbi(dir->i_sb)->si_dirwh)
24046+ || au_test_fs_remote(a->h_dst->d_sb)) {
24047+ err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
24048+ if (unlikely(err))
523b37e3
AM
24049+ pr_warn("failed removing whtmp dir %pd (%d), "
24050+ "ignored.\n", a->h_dst, err);
4a4d8108
AM
24051+ } else {
24052+ au_nhash_wh_free(&a->thargs->whlist);
24053+ a->thargs->whlist = a->whlist;
24054+ a->whlist.nh_num = 0;
24055+ au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
24056+ dput(a->h_dst);
24057+ a->thargs = NULL;
24058+ }
24059+
24060+ return 0;
1308ab2a 24061+}
1facf9fc 24062+
4a4d8108 24063+/* make it 'opaque' dir. */
f2c43d5f 24064+static int au_ren_do_diropq(struct au_ren_args *a, int idx)
4a4d8108
AM
24065+{
24066+ int err;
f2c43d5f
AM
24067+ struct dentry *d, *diropq;
24068+#define src_or_dst(member) a->sd[idx].member
1facf9fc 24069+
4a4d8108 24070+ err = 0;
f2c43d5f
AM
24071+ d = src_or_dst(dentry); /* {src,dst}_dentry */
24072+ src_or_dst(bdiropq) = au_dbdiropq(d);
24073+ src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
24074+ au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
24075+ diropq = au_diropq_create(d, a->btgt);
24076+ au_hn_inode_unlock(src_or_dst(hinode));
4a4d8108
AM
24077+ if (IS_ERR(diropq))
24078+ err = PTR_ERR(diropq);
076b876e
AM
24079+ else
24080+ dput(diropq);
1facf9fc 24081+
f2c43d5f 24082+#undef src_or_dst_
4a4d8108
AM
24083+ return err;
24084+}
1facf9fc 24085+
f2c43d5f 24086+static int au_ren_diropq(struct au_ren_args *a)
4a4d8108
AM
24087+{
24088+ int err;
f2c43d5f
AM
24089+ unsigned char always;
24090+ struct dentry *d;
1facf9fc 24091+
f2c43d5f
AM
24092+ err = 0;
24093+ d = a->dst_dentry; /* already renamed on the branch */
24094+ always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
24095+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
8b6a4947 24096+ && !au_ftest_ren(a->auren_flags, DIRREN)
f2c43d5f
AM
24097+ && a->btgt != au_dbdiropq(a->src_dentry)
24098+ && (a->dst_wh_dentry
24099+ || a->btgt <= au_dbdiropq(d)
24100+ /* hide the lower to keep xino */
24101+ /* the lowers may not be a dir, but we hide them anyway */
24102+ || a->btgt < au_dbbot(d)
24103+ || always)) {
24104+ AuDbg("here\n");
24105+ err = au_ren_do_diropq(a, AuSRC);
24106+ if (unlikely(err))
4a4d8108 24107+ goto out;
f2c43d5f 24108+ au_fset_ren(a->auren_flags, DIROPQ_SRC);
4a4d8108 24109+ }
f2c43d5f
AM
24110+ if (!a->exchange)
24111+ goto out; /* success */
1facf9fc 24112+
f2c43d5f
AM
24113+ d = a->src_dentry; /* already renamed on the branch */
24114+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)
24115+ && a->btgt != au_dbdiropq(a->dst_dentry)
24116+ && (a->btgt < au_dbdiropq(d)
24117+ || a->btgt < au_dbbot(d)
24118+ || always)) {
24119+ AuDbgDentry(a->src_dentry);
24120+ AuDbgDentry(a->dst_dentry);
24121+ err = au_ren_do_diropq(a, AuDST);
4a4d8108 24122+ if (unlikely(err))
f2c43d5f
AM
24123+ goto out_rev_src;
24124+ au_fset_ren(a->auren_flags, DIROPQ_DST);
24125+ }
24126+ goto out; /* success */
dece6358 24127+
f2c43d5f
AM
24128+out_rev_src:
24129+ AuDbg("err %d, reverting src\n", err);
24130+ au_ren_rev_diropq(err, a);
24131+out:
24132+ return err;
24133+}
24134+
24135+static int do_rename(struct au_ren_args *a)
24136+{
24137+ int err;
24138+ struct dentry *d, *h_d;
24139+
24140+ if (!a->exchange) {
24141+ /* prepare workqueue args for asynchronous rmdir */
24142+ h_d = a->dst_h_dentry;
24143+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)
8b6a4947 24144+ /* && !au_ftest_ren(a->auren_flags, DIRREN) */
f2c43d5f
AM
24145+ && d_is_positive(h_d)) {
24146+ err = -ENOMEM;
24147+ a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
24148+ GFP_NOFS);
24149+ if (unlikely(!a->thargs))
24150+ goto out;
24151+ a->h_dst = dget(h_d);
24152+ }
24153+
24154+ /* create whiteout for src_dentry */
24155+ if (au_ftest_ren(a->auren_flags, WHSRC)) {
24156+ a->src_bwh = au_dbwh(a->src_dentry);
24157+ AuDebugOn(a->src_bwh >= 0);
24158+ a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
24159+ a->src_h_parent);
24160+ err = PTR_ERR(a->src_wh_dentry);
24161+ if (IS_ERR(a->src_wh_dentry))
24162+ goto out_thargs;
24163+ }
24164+
24165+ /* lookup whiteout for dentry */
24166+ if (au_ftest_ren(a->auren_flags, WHDST)) {
24167+ h_d = au_wh_lkup(a->dst_h_parent,
24168+ &a->dst_dentry->d_name, a->br);
24169+ err = PTR_ERR(h_d);
24170+ if (IS_ERR(h_d))
24171+ goto out_whsrc;
24172+ if (d_is_negative(h_d))
24173+ dput(h_d);
24174+ else
24175+ a->dst_wh_dentry = h_d;
24176+ }
24177+
24178+ /* rename dentry to tmpwh */
24179+ if (a->thargs) {
24180+ err = au_whtmp_ren(a->dst_h_dentry, a->br);
24181+ if (unlikely(err))
24182+ goto out_whdst;
24183+
24184+ d = a->dst_dentry;
24185+ au_set_h_dptr(d, a->btgt, NULL);
24186+ err = au_lkup_neg(d, a->btgt, /*wh*/0);
24187+ if (unlikely(err))
24188+ goto out_whtmp;
24189+ a->dst_h_dentry = au_h_dptr(d, a->btgt);
24190+ }
4a4d8108 24191+ }
1facf9fc 24192+
5afbbe0d 24193+ BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
43982f53 24194+#if 0 /* debugging */
8b6a4947
AM
24195+ BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN)
24196+ && d_is_positive(a->dst_h_dentry)
24197+ && a->src_btop != a->btgt);
24198+#endif
1facf9fc 24199+
4a4d8108 24200+ /* rename by vfs_rename or cpup */
4a4d8108
AM
24201+ err = au_ren_or_cpup(a);
24202+ if (unlikely(err))
24203+ /* leave the copied-up one */
24204+ goto out_whtmp;
1308ab2a 24205+
4a4d8108 24206+ /* make dir opaque */
f2c43d5f
AM
24207+ err = au_ren_diropq(a);
24208+ if (unlikely(err))
24209+ goto out_rename;
1308ab2a 24210+
4a4d8108 24211+ /* update target timestamps */
f2c43d5f
AM
24212+ if (a->exchange) {
24213+ AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
24214+ a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
24215+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
24216+ a->dst_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
24217+ }
5afbbe0d 24218+ AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
4a4d8108
AM
24219+ a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
24220+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
5527c038 24221+ a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
1facf9fc 24222+
f2c43d5f
AM
24223+ if (!a->exchange) {
24224+ /* remove whiteout for dentry */
24225+ if (a->dst_wh_dentry) {
24226+ a->h_path.dentry = a->dst_wh_dentry;
24227+ err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
24228+ a->dst_dentry);
24229+ if (unlikely(err))
24230+ goto out_diropq;
24231+ }
1facf9fc 24232+
f2c43d5f
AM
24233+ /* remove whtmp */
24234+ if (a->thargs)
24235+ au_ren_del_whtmp(a); /* ignore this error */
1308ab2a 24236+
f2c43d5f
AM
24237+ au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
24238+ }
4a4d8108
AM
24239+ err = 0;
24240+ goto out_success;
24241+
4f0767ce 24242+out_diropq:
f2c43d5f 24243+ au_ren_rev_diropq(err, a);
4f0767ce 24244+out_rename:
7e9cd9fe 24245+ au_ren_rev_rename(err, a);
027c5e7a 24246+ dput(a->h_dst);
4f0767ce 24247+out_whtmp:
4a4d8108
AM
24248+ if (a->thargs)
24249+ au_ren_rev_whtmp(err, a);
4f0767ce 24250+out_whdst:
4a4d8108
AM
24251+ dput(a->dst_wh_dentry);
24252+ a->dst_wh_dentry = NULL;
4f0767ce 24253+out_whsrc:
4a4d8108
AM
24254+ if (a->src_wh_dentry)
24255+ au_ren_rev_whsrc(err, a);
4f0767ce 24256+out_success:
4a4d8108
AM
24257+ dput(a->src_wh_dentry);
24258+ dput(a->dst_wh_dentry);
4f0767ce 24259+out_thargs:
4a4d8108
AM
24260+ if (a->thargs) {
24261+ dput(a->h_dst);
24262+ au_whtmp_rmdir_free(a->thargs);
24263+ a->thargs = NULL;
24264+ }
4f0767ce 24265+out:
4a4d8108 24266+ return err;
dece6358 24267+}
1facf9fc 24268+
1308ab2a 24269+/* ---------------------------------------------------------------------- */
1facf9fc 24270+
4a4d8108
AM
24271+/*
24272+ * test if @dentry dir can be rename destination or not.
24273+ * success means, it is a logically empty dir.
24274+ */
24275+static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
1308ab2a 24276+{
4a4d8108 24277+ return au_test_empty(dentry, whlist);
1308ab2a 24278+}
1facf9fc 24279+
4a4d8108 24280+/*
8b6a4947
AM
24281+ * test if @a->src_dentry dir can be rename source or not.
24282+ * if it can, return 0.
4a4d8108
AM
24283+ * success means,
24284+ * - it is a logically empty dir.
24285+ * - or, it exists on writable branch and has no children including whiteouts
8b6a4947 24286+ * on the lower branch unless DIRREN is on.
4a4d8108 24287+ */
8b6a4947 24288+static int may_rename_srcdir(struct au_ren_args *a)
4a4d8108
AM
24289+{
24290+ int err;
24291+ unsigned int rdhash;
8b6a4947
AM
24292+ aufs_bindex_t btop, btgt;
24293+ struct dentry *dentry;
24294+ struct super_block *sb;
24295+ struct au_sbinfo *sbinfo;
1facf9fc 24296+
8b6a4947
AM
24297+ dentry = a->src_dentry;
24298+ sb = dentry->d_sb;
24299+ sbinfo = au_sbi(sb);
24300+ if (au_opt_test(sbinfo->si_mntflags, DIRREN))
24301+ au_fset_ren(a->auren_flags, DIRREN);
24302+
24303+ btgt = a->btgt;
5afbbe0d
AM
24304+ btop = au_dbtop(dentry);
24305+ if (btop != btgt) {
4a4d8108 24306+ struct au_nhash whlist;
dece6358 24307+
8b6a4947
AM
24308+ SiMustAnyLock(sb);
24309+ rdhash = sbinfo->si_rdhash;
4a4d8108
AM
24310+ if (!rdhash)
24311+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
24312+ dentry));
24313+ err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
24314+ if (unlikely(err))
24315+ goto out;
24316+ err = au_test_empty(dentry, &whlist);
24317+ au_nhash_wh_free(&whlist);
24318+ goto out;
24319+ }
dece6358 24320+
5afbbe0d 24321+ if (btop == au_dbtaildir(dentry))
4a4d8108 24322+ return 0; /* success */
dece6358 24323+
4a4d8108 24324+ err = au_test_empty_lower(dentry);
1facf9fc 24325+
4f0767ce 24326+out:
4a4d8108 24327+ if (err == -ENOTEMPTY) {
8b6a4947
AM
24328+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
24329+ err = 0;
24330+ } else {
24331+ AuWarn1("renaming dir who has child(ren) on multiple "
24332+ "branches, is not supported\n");
24333+ err = -EXDEV;
24334+ }
4a4d8108
AM
24335+ }
24336+ return err;
24337+}
1308ab2a 24338+
4a4d8108
AM
24339+/* side effect: sets whlist and h_dentry */
24340+static int au_ren_may_dir(struct au_ren_args *a)
1308ab2a 24341+{
4a4d8108
AM
24342+ int err;
24343+ unsigned int rdhash;
24344+ struct dentry *d;
1facf9fc 24345+
4a4d8108
AM
24346+ d = a->dst_dentry;
24347+ SiMustAnyLock(d->d_sb);
1facf9fc 24348+
4a4d8108 24349+ err = 0;
f2c43d5f 24350+ if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
4a4d8108
AM
24351+ rdhash = au_sbi(d->d_sb)->si_rdhash;
24352+ if (!rdhash)
24353+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
24354+ err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
24355+ if (unlikely(err))
24356+ goto out;
1308ab2a 24357+
f2c43d5f
AM
24358+ if (!a->exchange) {
24359+ au_set_dbtop(d, a->dst_btop);
24360+ err = may_rename_dstdir(d, &a->whlist);
24361+ au_set_dbtop(d, a->btgt);
24362+ } else
8b6a4947 24363+ err = may_rename_srcdir(a);
4a4d8108 24364+ }
5afbbe0d 24365+ a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
4a4d8108
AM
24366+ if (unlikely(err))
24367+ goto out;
24368+
24369+ d = a->src_dentry;
5afbbe0d 24370+ a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
f2c43d5f 24371+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
8b6a4947 24372+ err = may_rename_srcdir(a);
4a4d8108
AM
24373+ if (unlikely(err)) {
24374+ au_nhash_wh_free(&a->whlist);
24375+ a->whlist.nh_num = 0;
24376+ }
24377+ }
4f0767ce 24378+out:
4a4d8108 24379+ return err;
1facf9fc 24380+}
24381+
4a4d8108 24382+/* ---------------------------------------------------------------------- */
1facf9fc 24383+
4a4d8108
AM
24384+/*
24385+ * simple tests for rename.
24386+ * following the checks in vfs, plus the parent-child relationship.
24387+ */
24388+static int au_may_ren(struct au_ren_args *a)
24389+{
24390+ int err, isdir;
24391+ struct inode *h_inode;
1facf9fc 24392+
5afbbe0d 24393+ if (a->src_btop == a->btgt) {
4a4d8108 24394+ err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
f2c43d5f 24395+ au_ftest_ren(a->auren_flags, ISDIR_SRC));
4a4d8108
AM
24396+ if (unlikely(err))
24397+ goto out;
24398+ err = -EINVAL;
24399+ if (unlikely(a->src_h_dentry == a->h_trap))
24400+ goto out;
24401+ }
1facf9fc 24402+
4a4d8108 24403+ err = 0;
5afbbe0d 24404+ if (a->dst_btop != a->btgt)
4a4d8108 24405+ goto out;
1facf9fc 24406+
027c5e7a
AM
24407+ err = -ENOTEMPTY;
24408+ if (unlikely(a->dst_h_dentry == a->h_trap))
24409+ goto out;
24410+
4a4d8108 24411+ err = -EIO;
f2c43d5f 24412+ isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
5527c038
JR
24413+ if (d_really_is_negative(a->dst_dentry)) {
24414+ if (d_is_negative(a->dst_h_dentry))
24415+ err = au_may_add(a->dst_dentry, a->btgt,
24416+ a->dst_h_parent, isdir);
4a4d8108 24417+ } else {
5527c038 24418+ if (unlikely(d_is_negative(a->dst_h_dentry)))
4a4d8108 24419+ goto out;
5527c038
JR
24420+ h_inode = d_inode(a->dst_h_dentry);
24421+ if (h_inode->i_nlink)
24422+ err = au_may_del(a->dst_dentry, a->btgt,
24423+ a->dst_h_parent, isdir);
4a4d8108 24424+ }
1facf9fc 24425+
4f0767ce 24426+out:
4a4d8108
AM
24427+ if (unlikely(err == -ENOENT || err == -EEXIST))
24428+ err = -EIO;
24429+ AuTraceErr(err);
24430+ return err;
24431+}
1facf9fc 24432+
1308ab2a 24433+/* ---------------------------------------------------------------------- */
1facf9fc 24434+
4a4d8108
AM
24435+/*
24436+ * locking order
24437+ * (VFS)
24438+ * - src_dir and dir by lock_rename()
acd2b654 24439+ * - inode if exists
4a4d8108
AM
24440+ * (aufs)
24441+ * - lock all
24442+ * + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
24443+ * + si_read_lock
24444+ * + di_write_lock2_child()
24445+ * + di_write_lock_child()
24446+ * + ii_write_lock_child()
24447+ * + di_write_lock_child2()
24448+ * + ii_write_lock_child2()
24449+ * + src_parent and parent
24450+ * + di_write_lock_parent()
24451+ * + ii_write_lock_parent()
24452+ * + di_write_lock_parent2()
24453+ * + ii_write_lock_parent2()
24454+ * + lower src_dir and dir by vfsub_lock_rename()
24455+ * + verify the every relationships between child and parent. if any
24456+ * of them failed, unlock all and return -EBUSY.
24457+ */
24458+static void au_ren_unlock(struct au_ren_args *a)
1308ab2a 24459+{
4a4d8108
AM
24460+ vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
24461+ a->dst_h_parent, a->dst_hdir);
8b6a4947
AM
24462+ if (au_ftest_ren(a->auren_flags, DIRREN)
24463+ && a->h_root)
24464+ au_hn_inode_unlock(a->h_root);
f2c43d5f 24465+ if (au_ftest_ren(a->auren_flags, MNT_WRITE))
86dc4139 24466+ vfsub_mnt_drop_write(au_br_mnt(a->br));
1308ab2a 24467+}
24468+
4a4d8108 24469+static int au_ren_lock(struct au_ren_args *a)
1308ab2a 24470+{
4a4d8108
AM
24471+ int err;
24472+ unsigned int udba;
1308ab2a 24473+
4a4d8108
AM
24474+ err = 0;
24475+ a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
24476+ a->src_hdir = au_hi(a->src_dir, a->btgt);
24477+ a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
24478+ a->dst_hdir = au_hi(a->dst_dir, a->btgt);
86dc4139
AM
24479+
24480+ err = vfsub_mnt_want_write(au_br_mnt(a->br));
24481+ if (unlikely(err))
24482+ goto out;
f2c43d5f 24483+ au_fset_ren(a->auren_flags, MNT_WRITE);
8b6a4947
AM
24484+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
24485+ struct dentry *root;
24486+ struct inode *dir;
24487+
24488+ /*
24489+ * sbinfo is already locked, so this ii_read_lock is
24490+ * unnecessary. but our debugging feature checks it.
24491+ */
24492+ root = a->src_inode->i_sb->s_root;
24493+ if (root != a->src_parent && root != a->dst_parent) {
24494+ dir = d_inode(root);
24495+ ii_read_lock_parent3(dir);
24496+ a->h_root = au_hi(dir, a->btgt);
24497+ ii_read_unlock(dir);
24498+ au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3);
24499+ }
24500+ }
4a4d8108
AM
24501+ a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
24502+ a->dst_h_parent, a->dst_hdir);
24503+ udba = au_opt_udba(a->src_dentry->d_sb);
5527c038
JR
24504+ if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
24505+ || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
4a4d8108 24506+ err = au_busy_or_stale();
5afbbe0d 24507+ if (!err && au_dbtop(a->src_dentry) == a->btgt)
4a4d8108 24508+ err = au_h_verify(a->src_h_dentry, udba,
5527c038 24509+ d_inode(a->src_h_parent), a->src_h_parent,
4a4d8108 24510+ a->br);
5afbbe0d 24511+ if (!err && au_dbtop(a->dst_dentry) == a->btgt)
4a4d8108 24512+ err = au_h_verify(a->dst_h_dentry, udba,
5527c038 24513+ d_inode(a->dst_h_parent), a->dst_h_parent,
4a4d8108 24514+ a->br);
86dc4139 24515+ if (!err)
4a4d8108 24516+ goto out; /* success */
4a4d8108
AM
24517+
24518+ err = au_busy_or_stale();
4a4d8108 24519+ au_ren_unlock(a);
86dc4139 24520+
4f0767ce 24521+out:
4a4d8108 24522+ return err;
1facf9fc 24523+}
24524+
24525+/* ---------------------------------------------------------------------- */
24526+
4a4d8108 24527+static void au_ren_refresh_dir(struct au_ren_args *a)
1facf9fc 24528+{
4a4d8108 24529+ struct inode *dir;
dece6358 24530+
4a4d8108 24531+ dir = a->dst_dir;
be118d29 24532+ inode_inc_iversion(dir);
f2c43d5f 24533+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
4a4d8108
AM
24534+ /* is this updating defined in POSIX? */
24535+ au_cpup_attr_timesizes(a->src_inode);
24536+ au_cpup_attr_nlink(dir, /*force*/1);
4a4d8108 24537+ }
b912730e 24538+ au_dir_ts(dir, a->btgt);
dece6358 24539+
f2c43d5f
AM
24540+ if (a->exchange) {
24541+ dir = a->src_dir;
be118d29 24542+ inode_inc_iversion(dir);
f2c43d5f
AM
24543+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
24544+ /* is this updating defined in POSIX? */
24545+ au_cpup_attr_timesizes(a->dst_inode);
24546+ au_cpup_attr_nlink(dir, /*force*/1);
24547+ }
24548+ au_dir_ts(dir, a->btgt);
24549+ }
24550+
24551+ if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
4a4d8108 24552+ return;
dece6358 24553+
4a4d8108 24554+ dir = a->src_dir;
be118d29 24555+ inode_inc_iversion(dir);
f2c43d5f 24556+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
4a4d8108 24557+ au_cpup_attr_nlink(dir, /*force*/1);
b912730e 24558+ au_dir_ts(dir, a->btgt);
1facf9fc 24559+}
24560+
4a4d8108 24561+static void au_ren_refresh(struct au_ren_args *a)
1facf9fc 24562+{
5afbbe0d 24563+ aufs_bindex_t bbot, bindex;
4a4d8108
AM
24564+ struct dentry *d, *h_d;
24565+ struct inode *i, *h_i;
24566+ struct super_block *sb;
dece6358 24567+
027c5e7a
AM
24568+ d = a->dst_dentry;
24569+ d_drop(d);
24570+ if (a->h_dst)
24571+ /* already dget-ed by au_ren_or_cpup() */
24572+ au_set_h_dptr(d, a->btgt, a->h_dst);
24573+
24574+ i = a->dst_inode;
24575+ if (i) {
f2c43d5f
AM
24576+ if (!a->exchange) {
24577+ if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
24578+ vfsub_drop_nlink(i);
24579+ else {
24580+ vfsub_dead_dir(i);
24581+ au_cpup_attr_timesizes(i);
24582+ }
24583+ au_update_dbrange(d, /*do_put_zero*/1);
24584+ } else
24585+ au_cpup_attr_nlink(i, /*force*/1);
027c5e7a 24586+ } else {
5afbbe0d
AM
24587+ bbot = a->btgt;
24588+ for (bindex = au_dbtop(d); bindex < bbot; bindex++)
027c5e7a 24589+ au_set_h_dptr(d, bindex, NULL);
5afbbe0d
AM
24590+ bbot = au_dbbot(d);
24591+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
027c5e7a
AM
24592+ au_set_h_dptr(d, bindex, NULL);
24593+ au_update_dbrange(d, /*do_put_zero*/0);
24594+ }
24595+
8b6a4947
AM
24596+ if (a->exchange
24597+ || au_ftest_ren(a->auren_flags, DIRREN)) {
24598+ d_drop(a->src_dentry);
24599+ if (au_ftest_ren(a->auren_flags, DIRREN))
24600+ au_set_dbwh(a->src_dentry, -1);
24601+ return;
24602+ }
24603+
4a4d8108 24604+ d = a->src_dentry;
8b6a4947
AM
24605+ au_set_dbwh(d, -1);
24606+ bbot = au_dbbot(d);
24607+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
24608+ h_d = au_h_dptr(d, bindex);
24609+ if (h_d)
24610+ au_set_h_dptr(d, bindex, NULL);
24611+ }
24612+ au_set_dbbot(d, a->btgt);
4a4d8108 24613+
8b6a4947
AM
24614+ sb = d->d_sb;
24615+ i = a->src_inode;
24616+ if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
24617+ return; /* success */
4a4d8108 24618+
8b6a4947
AM
24619+ bbot = au_ibbot(i);
24620+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
24621+ h_i = au_h_iptr(i, bindex);
24622+ if (h_i) {
24623+ au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
24624+ /* ignore this error */
24625+ au_set_h_iptr(i, bindex, NULL, 0);
4a4d8108
AM
24626+ }
24627+ }
8b6a4947 24628+ au_set_ibbot(i, a->btgt);
1308ab2a 24629+}
dece6358 24630+
4a4d8108
AM
24631+/* ---------------------------------------------------------------------- */
24632+
24633+/* mainly for link(2) and rename(2) */
24634+int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
1308ab2a 24635+{
4a4d8108
AM
24636+ aufs_bindex_t bdiropq, bwh;
24637+ struct dentry *parent;
24638+ struct au_branch *br;
24639+
24640+ parent = dentry->d_parent;
5527c038 24641+ IMustLock(d_inode(parent)); /* dir is locked */
4a4d8108
AM
24642+
24643+ bdiropq = au_dbdiropq(parent);
24644+ bwh = au_dbwh(dentry);
24645+ br = au_sbr(dentry->d_sb, btgt);
24646+ if (au_br_rdonly(br)
24647+ || (0 <= bdiropq && bdiropq < btgt)
24648+ || (0 <= bwh && bwh < btgt))
24649+ btgt = -1;
24650+
24651+ AuDbg("btgt %d\n", btgt);
24652+ return btgt;
1facf9fc 24653+}
24654+
5afbbe0d 24655+/* sets src_btop, dst_btop and btgt */
4a4d8108 24656+static int au_ren_wbr(struct au_ren_args *a)
1facf9fc 24657+{
4a4d8108
AM
24658+ int err;
24659+ struct au_wr_dir_args wr_dir_args = {
24660+ /* .force_btgt = -1, */
24661+ .flags = AuWrDir_ADD_ENTRY
24662+ };
dece6358 24663+
5afbbe0d
AM
24664+ a->src_btop = au_dbtop(a->src_dentry);
24665+ a->dst_btop = au_dbtop(a->dst_dentry);
f2c43d5f
AM
24666+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
24667+ || au_ftest_ren(a->auren_flags, ISDIR_DST))
4a4d8108 24668+ au_fset_wrdir(wr_dir_args.flags, ISDIR);
5afbbe0d
AM
24669+ wr_dir_args.force_btgt = a->src_btop;
24670+ if (a->dst_inode && a->dst_btop < a->src_btop)
24671+ wr_dir_args.force_btgt = a->dst_btop;
4a4d8108
AM
24672+ wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
24673+ err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
24674+ a->btgt = err;
f2c43d5f
AM
24675+ if (a->exchange)
24676+ au_update_dbtop(a->dst_dentry);
dece6358 24677+
4a4d8108 24678+ return err;
1facf9fc 24679+}
24680+
4a4d8108 24681+static void au_ren_dt(struct au_ren_args *a)
1facf9fc 24682+{
4a4d8108
AM
24683+ a->h_path.dentry = a->src_h_parent;
24684+ au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
f2c43d5f 24685+ if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
4a4d8108
AM
24686+ a->h_path.dentry = a->dst_h_parent;
24687+ au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
24688+ }
1facf9fc 24689+
f2c43d5f
AM
24690+ au_fclr_ren(a->auren_flags, DT_DSTDIR);
24691+ if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
24692+ && !a->exchange)
4a4d8108 24693+ return;
dece6358 24694+
4a4d8108
AM
24695+ a->h_path.dentry = a->src_h_dentry;
24696+ au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
5527c038 24697+ if (d_is_positive(a->dst_h_dentry)) {
f2c43d5f 24698+ au_fset_ren(a->auren_flags, DT_DSTDIR);
4a4d8108
AM
24699+ a->h_path.dentry = a->dst_h_dentry;
24700+ au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
24701+ }
1308ab2a 24702+}
dece6358 24703+
4a4d8108 24704+static void au_ren_rev_dt(int err, struct au_ren_args *a)
1308ab2a 24705+{
4a4d8108 24706+ struct dentry *h_d;
febd17d6 24707+ struct inode *h_inode;
4a4d8108
AM
24708+
24709+ au_dtime_revert(a->src_dt + AuPARENT);
f2c43d5f 24710+ if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
4a4d8108
AM
24711+ au_dtime_revert(a->dst_dt + AuPARENT);
24712+
f2c43d5f 24713+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
4a4d8108 24714+ h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
febd17d6
JR
24715+ h_inode = d_inode(h_d);
24716+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 24717+ au_dtime_revert(a->src_dt + AuCHILD);
febd17d6 24718+ inode_unlock(h_inode);
4a4d8108 24719+
f2c43d5f 24720+ if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
4a4d8108 24721+ h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
febd17d6
JR
24722+ h_inode = d_inode(h_d);
24723+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 24724+ au_dtime_revert(a->dst_dt + AuCHILD);
febd17d6 24725+ inode_unlock(h_inode);
1facf9fc 24726+ }
24727+ }
24728+}
24729+
4a4d8108
AM
24730+/* ---------------------------------------------------------------------- */
24731+
0b2a12c6
JR
24732+int aufs_rename(struct user_namespace *userns,
24733+ struct inode *_src_dir, struct dentry *_src_dentry,
f2c43d5f
AM
24734+ struct inode *_dst_dir, struct dentry *_dst_dentry,
24735+ unsigned int _flags)
1facf9fc 24736+{
f2c43d5f 24737+ int err, lock_flags;
8b6a4947 24738+ void *rev;
4a4d8108
AM
24739+ /* reduce stack space */
24740+ struct au_ren_args *a;
f2c43d5f 24741+ struct au_pin pin;
4a4d8108 24742+
f2c43d5f 24743+ AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
4a4d8108
AM
24744+ IMustLock(_src_dir);
24745+ IMustLock(_dst_dir);
24746+
f2c43d5f
AM
24747+ err = -EINVAL;
24748+ if (unlikely(_flags & RENAME_WHITEOUT))
24749+ goto out;
24750+
4a4d8108
AM
24751+ err = -ENOMEM;
24752+ BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
24753+ a = kzalloc(sizeof(*a), GFP_NOFS);
24754+ if (unlikely(!a))
24755+ goto out;
24756+
f2c43d5f 24757+ a->flags = _flags;
acd2b654
AM
24758+ BUILD_BUG_ON(sizeof(a->exchange) == sizeof(u8)
24759+ && RENAME_EXCHANGE > U8_MAX);
f2c43d5f 24760+ a->exchange = _flags & RENAME_EXCHANGE;
4a4d8108
AM
24761+ a->src_dir = _src_dir;
24762+ a->src_dentry = _src_dentry;
5527c038
JR
24763+ a->src_inode = NULL;
24764+ if (d_really_is_positive(a->src_dentry))
24765+ a->src_inode = d_inode(a->src_dentry);
4a4d8108
AM
24766+ a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
24767+ a->dst_dir = _dst_dir;
24768+ a->dst_dentry = _dst_dentry;
5527c038
JR
24769+ a->dst_inode = NULL;
24770+ if (d_really_is_positive(a->dst_dentry))
24771+ a->dst_inode = d_inode(a->dst_dentry);
4a4d8108
AM
24772+ a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
24773+ if (a->dst_inode) {
f2c43d5f
AM
24774+ /*
24775+ * if EXCHANGE && src is non-dir && dst is dir,
24776+ * dst is not locked.
24777+ */
24778+ /* IMustLock(a->dst_inode); */
4a4d8108 24779+ au_igrab(a->dst_inode);
1facf9fc 24780+ }
1facf9fc 24781+
4a4d8108 24782+ err = -ENOTDIR;
f2c43d5f 24783+ lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
2000de60 24784+ if (d_is_dir(a->src_dentry)) {
f2c43d5f
AM
24785+ au_fset_ren(a->auren_flags, ISDIR_SRC);
24786+ if (unlikely(!a->exchange
24787+ && d_really_is_positive(a->dst_dentry)
2000de60 24788+ && !d_is_dir(a->dst_dentry)))
4a4d8108 24789+ goto out_free;
f2c43d5f
AM
24790+ lock_flags |= AuLock_DIRS;
24791+ }
24792+ if (a->dst_inode && d_is_dir(a->dst_dentry)) {
24793+ au_fset_ren(a->auren_flags, ISDIR_DST);
24794+ if (unlikely(!a->exchange
24795+ && d_really_is_positive(a->src_dentry)
24796+ && !d_is_dir(a->src_dentry)))
24797+ goto out_free;
24798+ lock_flags |= AuLock_DIRS;
b95c5147 24799+ }
8b6a4947
AM
24800+ err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
24801+ lock_flags);
e49829fe
JR
24802+ if (unlikely(err))
24803+ goto out_free;
1facf9fc 24804+
027c5e7a
AM
24805+ err = au_d_hashed_positive(a->src_dentry);
24806+ if (unlikely(err))
24807+ goto out_unlock;
24808+ err = -ENOENT;
24809+ if (a->dst_inode) {
24810+ /*
f2c43d5f 24811+ * If it is a dir, VFS unhash it before this
027c5e7a
AM
24812+ * function. It means we cannot rely upon d_unhashed().
24813+ */
24814+ if (unlikely(!a->dst_inode->i_nlink))
24815+ goto out_unlock;
f2c43d5f 24816+ if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
027c5e7a 24817+ err = au_d_hashed_positive(a->dst_dentry);
f2c43d5f 24818+ if (unlikely(err && !a->exchange))
027c5e7a
AM
24819+ goto out_unlock;
24820+ } else if (unlikely(IS_DEADDIR(a->dst_inode)))
24821+ goto out_unlock;
24822+ } else if (unlikely(d_unhashed(a->dst_dentry)))
24823+ goto out_unlock;
24824+
7eafdf33
AM
24825+ /*
24826+ * is it possible?
79b8bda9 24827+ * yes, it happened (in linux-3.3-rcN) but I don't know why.
7eafdf33
AM
24828+ * there may exist a problem somewhere else.
24829+ */
24830+ err = -EINVAL;
5527c038 24831+ if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
7eafdf33
AM
24832+ goto out_unlock;
24833+
f2c43d5f 24834+ au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
4a4d8108 24835+ di_write_lock_parent(a->dst_parent);
1facf9fc 24836+
4a4d8108
AM
24837+ /* which branch we process */
24838+ err = au_ren_wbr(a);
24839+ if (unlikely(err < 0))
027c5e7a 24840+ goto out_parent;
4a4d8108 24841+ a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
86dc4139 24842+ a->h_path.mnt = au_br_mnt(a->br);
1facf9fc 24843+
4a4d8108
AM
24844+ /* are they available to be renamed */
24845+ err = au_ren_may_dir(a);
24846+ if (unlikely(err))
24847+ goto out_children;
1facf9fc 24848+
4a4d8108 24849+ /* prepare the writable parent dir on the same branch */
5afbbe0d 24850+ if (a->dst_btop == a->btgt) {
f2c43d5f 24851+ au_fset_ren(a->auren_flags, WHDST);
4a4d8108
AM
24852+ } else {
24853+ err = au_cpup_dirs(a->dst_dentry, a->btgt);
24854+ if (unlikely(err))
24855+ goto out_children;
24856+ }
1facf9fc 24857+
f2c43d5f
AM
24858+ err = 0;
24859+ if (!a->exchange) {
24860+ if (a->src_dir != a->dst_dir) {
24861+ /*
24862+ * this temporary unlock is safe,
24863+ * because both dir->i_mutex are locked.
24864+ */
24865+ di_write_unlock(a->dst_parent);
24866+ di_write_lock_parent(a->src_parent);
24867+ err = au_wr_dir_need_wh(a->src_dentry,
24868+ au_ftest_ren(a->auren_flags,
24869+ ISDIR_SRC),
24870+ &a->btgt);
24871+ di_write_unlock(a->src_parent);
24872+ di_write_lock2_parent(a->src_parent, a->dst_parent,
24873+ /*isdir*/1);
24874+ au_fclr_ren(a->auren_flags, ISSAMEDIR);
24875+ } else
24876+ err = au_wr_dir_need_wh(a->src_dentry,
24877+ au_ftest_ren(a->auren_flags,
24878+ ISDIR_SRC),
24879+ &a->btgt);
24880+ }
4a4d8108
AM
24881+ if (unlikely(err < 0))
24882+ goto out_children;
24883+ if (err)
f2c43d5f 24884+ au_fset_ren(a->auren_flags, WHSRC);
1facf9fc 24885+
86dc4139 24886+ /* cpup src */
5afbbe0d 24887+ if (a->src_btop != a->btgt) {
86dc4139
AM
24888+ err = au_pin(&pin, a->src_dentry, a->btgt,
24889+ au_opt_udba(a->src_dentry->d_sb),
24890+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
367653fa 24891+ if (!err) {
c2b27bf2
AM
24892+ struct au_cp_generic cpg = {
24893+ .dentry = a->src_dentry,
24894+ .bdst = a->btgt,
5afbbe0d 24895+ .bsrc = a->src_btop,
c2b27bf2
AM
24896+ .len = -1,
24897+ .pin = &pin,
24898+ .flags = AuCpup_DTIME | AuCpup_HOPEN
24899+ };
5afbbe0d 24900+ AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
c2b27bf2 24901+ err = au_sio_cpup_simple(&cpg);
367653fa 24902+ au_unpin(&pin);
86dc4139 24903+ }
86dc4139
AM
24904+ if (unlikely(err))
24905+ goto out_children;
5afbbe0d 24906+ a->src_btop = a->btgt;
86dc4139 24907+ a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
f2c43d5f
AM
24908+ if (!a->exchange)
24909+ au_fset_ren(a->auren_flags, WHSRC);
24910+ }
24911+
24912+ /* cpup dst */
24913+ if (a->exchange && a->dst_inode
24914+ && a->dst_btop != a->btgt) {
24915+ err = au_pin(&pin, a->dst_dentry, a->btgt,
24916+ au_opt_udba(a->dst_dentry->d_sb),
24917+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
24918+ if (!err) {
24919+ struct au_cp_generic cpg = {
24920+ .dentry = a->dst_dentry,
24921+ .bdst = a->btgt,
24922+ .bsrc = a->dst_btop,
24923+ .len = -1,
24924+ .pin = &pin,
24925+ .flags = AuCpup_DTIME | AuCpup_HOPEN
24926+ };
24927+ err = au_sio_cpup_simple(&cpg);
24928+ au_unpin(&pin);
24929+ }
24930+ if (unlikely(err))
24931+ goto out_children;
24932+ a->dst_btop = a->btgt;
24933+ a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
86dc4139
AM
24934+ }
24935+
4a4d8108
AM
24936+ /* lock them all */
24937+ err = au_ren_lock(a);
24938+ if (unlikely(err))
86dc4139 24939+ /* leave the copied-up one */
4a4d8108 24940+ goto out_children;
1facf9fc 24941+
f2c43d5f
AM
24942+ if (!a->exchange) {
24943+ if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
24944+ err = au_may_ren(a);
24945+ else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
24946+ err = -ENAMETOOLONG;
24947+ if (unlikely(err))
24948+ goto out_hdir;
24949+ }
1facf9fc 24950+
4a4d8108
AM
24951+ /* store timestamps to be revertible */
24952+ au_ren_dt(a);
1facf9fc 24953+
8b6a4947
AM
24954+ /* store dirren info */
24955+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
24956+ err = au_dr_rename(a->src_dentry, a->btgt,
24957+ &a->dst_dentry->d_name, &rev);
24958+ AuTraceErr(err);
24959+ if (unlikely(err))
24960+ goto out_dt;
24961+ }
24962+
4a4d8108
AM
24963+ /* here we go */
24964+ err = do_rename(a);
24965+ if (unlikely(err))
8b6a4947
AM
24966+ goto out_dirren;
24967+
24968+ if (au_ftest_ren(a->auren_flags, DIRREN))
24969+ au_dr_rename_fin(a->src_dentry, a->btgt, rev);
4a4d8108
AM
24970+
24971+ /* update dir attributes */
24972+ au_ren_refresh_dir(a);
24973+
24974+ /* dput/iput all lower dentries */
24975+ au_ren_refresh(a);
24976+
24977+ goto out_hdir; /* success */
24978+
8b6a4947
AM
24979+out_dirren:
24980+ if (au_ftest_ren(a->auren_flags, DIRREN))
24981+ au_dr_rename_rev(a->src_dentry, a->btgt, rev);
4f0767ce 24982+out_dt:
4a4d8108 24983+ au_ren_rev_dt(err, a);
4f0767ce 24984+out_hdir:
4a4d8108 24985+ au_ren_unlock(a);
4f0767ce 24986+out_children:
4a4d8108 24987+ au_nhash_wh_free(&a->whlist);
5afbbe0d
AM
24988+ if (err && a->dst_inode && a->dst_btop != a->btgt) {
24989+ AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
027c5e7a 24990+ au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
5afbbe0d 24991+ au_set_dbtop(a->dst_dentry, a->dst_btop);
4a4d8108 24992+ }
027c5e7a 24993+out_parent:
f2c43d5f 24994+ if (!err) {
8b6a4947
AM
24995+ if (d_unhashed(a->src_dentry))
24996+ au_fset_ren(a->auren_flags, DROPPED_SRC);
24997+ if (d_unhashed(a->dst_dentry))
24998+ au_fset_ren(a->auren_flags, DROPPED_DST);
f2c43d5f
AM
24999+ if (!a->exchange)
25000+ d_move(a->src_dentry, a->dst_dentry);
8b6a4947 25001+ else {
f2c43d5f 25002+ d_exchange(a->src_dentry, a->dst_dentry);
8b6a4947
AM
25003+ if (au_ftest_ren(a->auren_flags, DROPPED_DST))
25004+ d_drop(a->dst_dentry);
25005+ }
25006+ if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
25007+ d_drop(a->src_dentry);
f2c43d5f 25008+ } else {
5afbbe0d 25009+ au_update_dbtop(a->dst_dentry);
027c5e7a
AM
25010+ if (!a->dst_inode)
25011+ d_drop(a->dst_dentry);
25012+ }
f2c43d5f 25013+ if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
4a4d8108
AM
25014+ di_write_unlock(a->dst_parent);
25015+ else
25016+ di_write_unlock2(a->src_parent, a->dst_parent);
027c5e7a 25017+out_unlock:
4a4d8108 25018+ aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
4f0767ce 25019+out_free:
4a4d8108
AM
25020+ iput(a->dst_inode);
25021+ if (a->thargs)
25022+ au_whtmp_rmdir_free(a->thargs);
9f237c51 25023+ au_kfree_rcu(a);
4f0767ce 25024+out:
4a4d8108
AM
25025+ AuTraceErr(err);
25026+ return err;
1308ab2a 25027+}
7f207e10 25028diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
eca34b5c 25029--- /usr/share/empty/fs/aufs/Kconfig 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 25030+++ linux/fs/aufs/Kconfig 2022-11-05 23:02:18.959222617 +0100
2121bcd9
AM
25031@@ -0,0 +1,199 @@
25032+# SPDX-License-Identifier: GPL-2.0
4a4d8108
AM
25033+config AUFS_FS
25034+ tristate "Aufs (Advanced multi layered unification filesystem) support"
4a4d8108
AM
25035+ help
25036+ Aufs is a stackable unification filesystem such as Unionfs,
25037+ which unifies several directories and provides a merged single
25038+ directory.
25039+ In the early days, aufs was entirely re-designed and
25040+ re-implemented Unionfs Version 1.x series. Introducing many
25041+ original ideas, approaches and improvements, it becomes totally
25042+ different from Unionfs while keeping the basic features.
1facf9fc 25043+
4a4d8108
AM
25044+if AUFS_FS
25045+choice
25046+ prompt "Maximum number of branches"
25047+ default AUFS_BRANCH_MAX_127
25048+ help
25049+ Specifies the maximum number of branches (or member directories)
25050+ in a single aufs. The larger value consumes more system
25051+ resources and has a minor impact to performance.
25052+config AUFS_BRANCH_MAX_127
25053+ bool "127"
25054+ help
25055+ Specifies the maximum number of branches (or member directories)
25056+ in a single aufs. The larger value consumes more system
25057+ resources and has a minor impact to performance.
25058+config AUFS_BRANCH_MAX_511
25059+ bool "511"
25060+ help
25061+ Specifies the maximum number of branches (or member directories)
25062+ in a single aufs. The larger value consumes more system
25063+ resources and has a minor impact to performance.
25064+config AUFS_BRANCH_MAX_1023
25065+ bool "1023"
25066+ help
25067+ Specifies the maximum number of branches (or member directories)
25068+ in a single aufs. The larger value consumes more system
25069+ resources and has a minor impact to performance.
25070+config AUFS_BRANCH_MAX_32767
25071+ bool "32767"
25072+ help
25073+ Specifies the maximum number of branches (or member directories)
25074+ in a single aufs. The larger value consumes more system
25075+ resources and has a minor impact to performance.
25076+endchoice
1facf9fc 25077+
e49829fe
JR
25078+config AUFS_SBILIST
25079+ bool
25080+ depends on AUFS_MAGIC_SYSRQ || PROC_FS
25081+ default y
25082+ help
25083+ Automatic configuration for internal use.
25084+ When aufs supports Magic SysRq or /proc, enabled automatically.
25085+
4a4d8108
AM
25086+config AUFS_HNOTIFY
25087+ bool "Detect direct branch access (bypassing aufs)"
25088+ help
25089+ If you want to modify files on branches directly, eg. bypassing aufs,
25090+ and want aufs to detect the changes of them fully, then enable this
25091+ option and use 'udba=notify' mount option.
7f207e10 25092+ Currently there is only one available configuration, "fsnotify".
4a4d8108
AM
25093+ It will have a negative impact to the performance.
25094+ See detail in aufs.5.
dece6358 25095+
4a4d8108
AM
25096+choice
25097+ prompt "method" if AUFS_HNOTIFY
25098+ default AUFS_HFSNOTIFY
25099+config AUFS_HFSNOTIFY
25100+ bool "fsnotify"
25101+ select FSNOTIFY
4a4d8108 25102+endchoice
1facf9fc 25103+
4a4d8108
AM
25104+config AUFS_EXPORT
25105+ bool "NFS-exportable aufs"
2cbb1c4b 25106+ depends on EXPORTFS
4a4d8108
AM
25107+ help
25108+ If you want to export your mounted aufs via NFS, then enable this
25109+ option. There are several requirements for this configuration.
25110+ See detail in aufs.5.
1facf9fc 25111+
4a4d8108
AM
25112+config AUFS_INO_T_64
25113+ bool
25114+ depends on AUFS_EXPORT
25115+ depends on 64BIT && !(ALPHA || S390)
25116+ default y
25117+ help
25118+ Automatic configuration for internal use.
25119+ /* typedef unsigned long/int __kernel_ino_t */
25120+ /* alpha and s390x are int */
1facf9fc 25121+
c1595e42
JR
25122+config AUFS_XATTR
25123+ bool "support for XATTR/EA (including Security Labels)"
25124+ help
25125+ If your branch fs supports XATTR/EA and you want to make them
25126+ available in aufs too, then enable this opsion and specify the
25127+ branch attributes for EA.
25128+ See detail in aufs.5.
25129+
076b876e
AM
25130+config AUFS_FHSM
25131+ bool "File-based Hierarchical Storage Management"
25132+ help
25133+ Hierarchical Storage Management (or HSM) is a well-known feature
25134+ in the storage world. Aufs provides this feature as file-based.
25135+ with multiple branches.
25136+ These multiple branches are prioritized, ie. the topmost one
25137+ should be the fastest drive and be used heavily.
25138+
4a4d8108
AM
25139+config AUFS_RDU
25140+ bool "Readdir in userspace"
25141+ help
25142+ Aufs has two methods to provide a merged view for a directory,
25143+ by a user-space library and by kernel-space natively. The latter
25144+ is always enabled but sometimes large and slow.
25145+ If you enable this option, install the library in aufs2-util
25146+ package, and set some environment variables for your readdir(3),
25147+ then the work will be handled in user-space which generally
25148+ shows better performance in most cases.
25149+ See detail in aufs.5.
1facf9fc 25150+
8b6a4947
AM
25151+config AUFS_DIRREN
25152+ bool "Workaround for rename(2)-ing a directory"
25153+ help
25154+ By default, aufs returns EXDEV error in renameing a dir who has
25155+ his child on the lower branch, since it is a bad idea to issue
25156+ rename(2) internally for every lower branch. But user may not
25157+ accept this behaviour. So here is a workaround to allow such
22319442 25158+ rename(2) and store some extra information on the writable
8b6a4947
AM
25159+ branch. Obviously this costs high (and I don't like it).
25160+ To use this feature, you need to enable this configuration AND
25161+ to specify the mount option `dirren.'
25162+ See details in aufs.5 and the design documents.
25163+
4a4d8108
AM
25164+config AUFS_SHWH
25165+ bool "Show whiteouts"
25166+ help
25167+ If you want to make the whiteouts in aufs visible, then enable
25168+ this option and specify 'shwh' mount option. Although it may
25169+ sounds like philosophy or something, but in technically it
25170+ simply shows the name of whiteout with keeping its behaviour.
1facf9fc 25171+
4a4d8108
AM
25172+config AUFS_BR_RAMFS
25173+ bool "Ramfs (initramfs/rootfs) as an aufs branch"
25174+ help
25175+ If you want to use ramfs as an aufs branch fs, then enable this
25176+ option. Generally tmpfs is recommended.
25177+ Aufs prohibited them to be a branch fs by default, because
25178+ initramfs becomes unusable after switch_root or something
25179+ generally. If you sets initramfs as an aufs branch and boot your
25180+ system by switch_root, you will meet a problem easily since the
25181+ files in initramfs may be inaccessible.
25182+ Unless you are going to use ramfs as an aufs branch fs without
25183+ switch_root or something, leave it N.
1facf9fc 25184+
4a4d8108
AM
25185+config AUFS_BR_FUSE
25186+ bool "Fuse fs as an aufs branch"
25187+ depends on FUSE_FS
25188+ select AUFS_POLL
25189+ help
25190+ If you want to use fuse-based userspace filesystem as an aufs
25191+ branch fs, then enable this option.
25192+ It implements the internal poll(2) operation which is
25193+ implemented by fuse only (curretnly).
1facf9fc 25194+
4a4d8108
AM
25195+config AUFS_POLL
25196+ bool
25197+ help
25198+ Automatic configuration for internal use.
1facf9fc 25199+
4a4d8108
AM
25200+config AUFS_BR_HFSPLUS
25201+ bool "Hfsplus as an aufs branch"
25202+ depends on HFSPLUS_FS
25203+ default y
25204+ help
25205+ If you want to use hfsplus fs as an aufs branch fs, then enable
25206+ this option. This option introduces a small overhead at
25207+ copying-up a file on hfsplus.
1facf9fc 25208+
4a4d8108
AM
25209+config AUFS_BDEV_LOOP
25210+ bool
25211+ depends on BLK_DEV_LOOP
25212+ default y
25213+ help
25214+ Automatic configuration for internal use.
25215+ Convert =[ym] into =y.
1308ab2a 25216+
4a4d8108
AM
25217+config AUFS_DEBUG
25218+ bool "Debug aufs"
25219+ help
25220+ Enable this to compile aufs internal debug code.
25221+ It will have a negative impact to the performance.
25222+
25223+config AUFS_MAGIC_SYSRQ
25224+ bool
25225+ depends on AUFS_DEBUG && MAGIC_SYSRQ
25226+ default y
25227+ help
25228+ Automatic configuration for internal use.
25229+ When aufs supports Magic SysRq, enabled automatically.
25230+endif
acd2b654 25231diff -urN /usr/share/empty/fs/aufs/lcnt.h linux/fs/aufs/lcnt.h
eca34b5c 25232--- /usr/share/empty/fs/aufs/lcnt.h 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 25233+++ linux/fs/aufs/lcnt.h 2022-11-05 23:02:18.965889284 +0100
acd2b654
AM
25234@@ -0,0 +1,186 @@
25235+/* SPDX-License-Identifier: GPL-2.0 */
25236+/*
0dcfbb52 25237+ * Copyright (C) 2018-2022 Junjiro R. Okajima
acd2b654 25238+ *
0dcfbb52 25239+ * This program is free software; you can redistribute it and/or modify
acd2b654
AM
25240+ * it under the terms of the GNU General Public License as published by
25241+ * the Free Software Foundation; either version 2 of the License, or
25242+ * (at your option) any later version.
25243+ *
25244+ * This program is distributed in the hope that it will be useful,
25245+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
25246+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25247+ * GNU General Public License for more details.
25248+ *
25249+ * You should have received a copy of the GNU General Public License
25250+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
25251+ */
25252+
25253+/*
25254+ * simple long counter wrapper
25255+ */
25256+
25257+#ifndef __AUFS_LCNT_H__
25258+#define __AUFS_LCNT_H__
25259+
25260+#ifdef __KERNEL__
25261+
25262+#include "debug.h"
25263+
25264+#define AuLCntATOMIC 1
25265+#define AuLCntPCPUCNT 2
25266+/*
25267+ * why does percpu_refcount require extra synchronize_rcu()s in
25268+ * au_br_do_free()
25269+ */
25270+#define AuLCntPCPUREF 3
25271+
25272+/* #define AuLCntChosen AuLCntATOMIC */
25273+#define AuLCntChosen AuLCntPCPUCNT
25274+/* #define AuLCntChosen AuLCntPCPUREF */
25275+
25276+#if AuLCntChosen == AuLCntATOMIC
25277+#include <linux/atomic.h>
25278+
25279+typedef atomic_long_t au_lcnt_t;
25280+
25281+static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
25282+{
25283+ atomic_long_set(cnt, 0);
25284+ return 0;
25285+}
25286+
25287+static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
25288+{
25289+ /* empty */
25290+}
25291+
25292+static inline void au_lcnt_fin(au_lcnt_t *cnt __maybe_unused,
25293+ int do_sync __maybe_unused)
25294+{
25295+ /* empty */
25296+}
25297+
25298+static inline void au_lcnt_inc(au_lcnt_t *cnt)
25299+{
25300+ atomic_long_inc(cnt);
25301+}
25302+
25303+static inline void au_lcnt_dec(au_lcnt_t *cnt)
25304+{
25305+ atomic_long_dec(cnt);
25306+}
25307+
25308+static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
25309+{
25310+ return atomic_long_read(cnt);
25311+}
25312+#endif
25313+
25314+#if AuLCntChosen == AuLCntPCPUCNT
25315+#include <linux/percpu_counter.h>
25316+
25317+typedef struct percpu_counter au_lcnt_t;
25318+
25319+static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
25320+{
25321+ return percpu_counter_init(cnt, 0, GFP_NOFS);
25322+}
25323+
25324+static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
25325+{
25326+ /* empty */
25327+}
25328+
25329+static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync __maybe_unused)
25330+{
25331+ percpu_counter_destroy(cnt);
25332+}
25333+
25334+static inline void au_lcnt_inc(au_lcnt_t *cnt)
25335+{
25336+ percpu_counter_inc(cnt);
25337+}
25338+
25339+static inline void au_lcnt_dec(au_lcnt_t *cnt)
25340+{
25341+ percpu_counter_dec(cnt);
25342+}
25343+
25344+static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
25345+{
25346+ s64 n;
25347+
25348+ n = percpu_counter_sum(cnt);
25349+ BUG_ON(n < 0);
25350+ if (LONG_MAX != LLONG_MAX
25351+ && n > LONG_MAX)
25352+ AuWarn1("%s\n", "wrap-around");
25353+
25354+ return n;
25355+}
25356+#endif
25357+
25358+#if AuLCntChosen == AuLCntPCPUREF
25359+#include <linux/percpu-refcount.h>
25360+
25361+typedef struct percpu_ref au_lcnt_t;
25362+
25363+static inline int au_lcnt_init(au_lcnt_t *cnt, percpu_ref_func_t *release)
25364+{
25365+ if (!release)
25366+ release = percpu_ref_exit;
25367+ return percpu_ref_init(cnt, release, /*percpu mode*/0, GFP_NOFS);
25368+}
25369+
25370+static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
25371+{
25372+ synchronize_rcu();
25373+}
25374+
25375+static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync)
25376+{
25377+ percpu_ref_kill(cnt);
25378+ if (do_sync)
25379+ au_lcnt_wait_for_fin(cnt);
25380+}
25381+
25382+static inline void au_lcnt_inc(au_lcnt_t *cnt)
25383+{
25384+ percpu_ref_get(cnt);
25385+}
25386+
25387+static inline void au_lcnt_dec(au_lcnt_t *cnt)
25388+{
25389+ percpu_ref_put(cnt);
25390+}
25391+
25392+/*
25393+ * avoid calling this func as possible.
25394+ */
25395+static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev)
25396+{
25397+ long l;
25398+
25399+ percpu_ref_switch_to_atomic_sync(cnt);
25400+ l = atomic_long_read(&cnt->count);
25401+ if (do_rev)
25402+ percpu_ref_switch_to_percpu(cnt);
25403+
25404+ /* percpu_ref is initialized by 1 instead of 0 */
25405+ return l - 1;
25406+}
25407+#endif
25408+
25409+#ifdef CONFIG_AUFS_DEBUG
25410+#define AuLCntZero(val) do { \
25411+ long l = val; \
25412+ if (l) \
25413+ AuDbg("%s = %ld\n", #val, l); \
25414+} while (0)
25415+#else
25416+#define AuLCntZero(val) do {} while (0)
25417+#endif
25418+
25419+#endif /* __KERNEL__ */
25420+#endif /* __AUFS_LCNT_H__ */
7f207e10 25421diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
eca34b5c 25422--- /usr/share/empty/fs/aufs/loop.c 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 25423+++ linux/fs/aufs/loop.c 2022-11-05 23:02:18.965889284 +0100
062440b3 25424@@ -0,0 +1,148 @@
cd7a4cd9 25425+// SPDX-License-Identifier: GPL-2.0
1facf9fc 25426+/*
0dcfbb52 25427+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 25428+ *
0dcfbb52 25429+ * This program is free software; you can redistribute it and/or modify
1facf9fc 25430+ * it under the terms of the GNU General Public License as published by
25431+ * the Free Software Foundation; either version 2 of the License, or
25432+ * (at your option) any later version.
dece6358
AM
25433+ *
25434+ * This program is distributed in the hope that it will be useful,
25435+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
25436+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25437+ * GNU General Public License for more details.
25438+ *
25439+ * You should have received a copy of the GNU General Public License
523b37e3 25440+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 25441+ */
25442+
25443+/*
25444+ * support for loopback block device as a branch
25445+ */
25446+
1facf9fc 25447+#include "aufs.h"
25448+
392086de
AM
25449+/* added into drivers/block/loop.c */
25450+static struct file *(*backing_file_func)(struct super_block *sb);
25451+
1facf9fc 25452+/*
25453+ * test if two lower dentries have overlapping branches.
25454+ */
b752ccd1 25455+int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
1facf9fc 25456+{
b752ccd1 25457+ struct super_block *h_sb;
392086de
AM
25458+ struct file *backing_file;
25459+
25460+ if (unlikely(!backing_file_func)) {
25461+ /* don't load "loop" module here */
25462+ backing_file_func = symbol_get(loop_backing_file);
25463+ if (unlikely(!backing_file_func))
25464+ /* "loop" module is not loaded */
25465+ return 0;
25466+ }
1facf9fc 25467+
b752ccd1 25468+ h_sb = h_adding->d_sb;
392086de
AM
25469+ backing_file = backing_file_func(h_sb);
25470+ if (!backing_file)
1facf9fc 25471+ return 0;
25472+
2000de60 25473+ h_adding = backing_file->f_path.dentry;
b752ccd1
AM
25474+ /*
25475+ * h_adding can be local NFS.
25476+ * in this case aufs cannot detect the loop.
25477+ */
25478+ if (unlikely(h_adding->d_sb == sb))
1facf9fc 25479+ return 1;
b752ccd1 25480+ return !!au_test_subdir(h_adding, sb->s_root);
1facf9fc 25481+}
25482+
25483+/* true if a kernel thread named 'loop[0-9].*' accesses a file */
25484+int au_test_loopback_kthread(void)
25485+{
b752ccd1
AM
25486+ int ret;
25487+ struct task_struct *tsk = current;
a2a7ad62 25488+ char c, comm[sizeof(tsk->comm)];
b752ccd1
AM
25489+
25490+ ret = 0;
25491+ if (tsk->flags & PF_KTHREAD) {
a2a7ad62
AM
25492+ get_task_comm(comm, tsk);
25493+ c = comm[4];
b752ccd1 25494+ ret = ('0' <= c && c <= '9'
a2a7ad62 25495+ && !strncmp(comm, "loop", 4));
b752ccd1 25496+ }
1facf9fc 25497+
b752ccd1 25498+ return ret;
1facf9fc 25499+}
87a755f4
AM
25500+
25501+/* ---------------------------------------------------------------------- */
25502+
25503+#define au_warn_loopback_step 16
25504+static int au_warn_loopback_nelem = au_warn_loopback_step;
25505+static unsigned long *au_warn_loopback_array;
25506+
25507+void au_warn_loopback(struct super_block *h_sb)
25508+{
25509+ int i, new_nelem;
25510+ unsigned long *a, magic;
25511+ static DEFINE_SPINLOCK(spin);
25512+
25513+ magic = h_sb->s_magic;
25514+ spin_lock(&spin);
25515+ a = au_warn_loopback_array;
25516+ for (i = 0; i < au_warn_loopback_nelem && *a; i++)
25517+ if (a[i] == magic) {
25518+ spin_unlock(&spin);
25519+ return;
25520+ }
25521+
25522+ /* h_sb is new to us, print it */
25523+ if (i < au_warn_loopback_nelem) {
25524+ a[i] = magic;
25525+ goto pr;
25526+ }
25527+
25528+ /* expand the array */
25529+ new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
25530+ a = au_kzrealloc(au_warn_loopback_array,
25531+ au_warn_loopback_nelem * sizeof(unsigned long),
e2f27e51
AM
25532+ new_nelem * sizeof(unsigned long), GFP_ATOMIC,
25533+ /*may_shrink*/0);
87a755f4
AM
25534+ if (a) {
25535+ au_warn_loopback_nelem = new_nelem;
25536+ au_warn_loopback_array = a;
25537+ a[i] = magic;
25538+ goto pr;
25539+ }
25540+
25541+ spin_unlock(&spin);
25542+ AuWarn1("realloc failed, ignored\n");
25543+ return;
25544+
25545+pr:
25546+ spin_unlock(&spin);
0c3ec466
AM
25547+ pr_warn("you may want to try another patch for loopback file "
25548+ "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
87a755f4
AM
25549+}
25550+
25551+int au_loopback_init(void)
25552+{
25553+ int err;
25554+ struct super_block *sb __maybe_unused;
25555+
fbc438ed 25556+ BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(*au_warn_loopback_array));
87a755f4
AM
25557+
25558+ err = 0;
25559+ au_warn_loopback_array = kcalloc(au_warn_loopback_step,
25560+ sizeof(unsigned long), GFP_NOFS);
25561+ if (unlikely(!au_warn_loopback_array))
25562+ err = -ENOMEM;
25563+
25564+ return err;
25565+}
25566+
25567+void au_loopback_fin(void)
25568+{
79b8bda9
AM
25569+ if (backing_file_func)
25570+ symbol_put(loop_backing_file);
9f237c51 25571+ au_kfree_try_rcu(au_warn_loopback_array);
87a755f4 25572+}
7f207e10 25573diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
eca34b5c 25574--- /usr/share/empty/fs/aufs/loop.h 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 25575+++ linux/fs/aufs/loop.h 2022-11-05 23:02:18.965889284 +0100
eca801bf 25576@@ -0,0 +1,55 @@
062440b3 25577+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 25578+/*
0dcfbb52 25579+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 25580+ *
0dcfbb52 25581+ * This program is free software; you can redistribute it and/or modify
1facf9fc 25582+ * it under the terms of the GNU General Public License as published by
25583+ * the Free Software Foundation; either version 2 of the License, or
25584+ * (at your option) any later version.
dece6358
AM
25585+ *
25586+ * This program is distributed in the hope that it will be useful,
25587+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
25588+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25589+ * GNU General Public License for more details.
25590+ *
25591+ * You should have received a copy of the GNU General Public License
523b37e3 25592+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 25593+ */
25594+
25595+/*
25596+ * support for loopback mount as a branch
25597+ */
25598+
25599+#ifndef __AUFS_LOOP_H__
25600+#define __AUFS_LOOP_H__
25601+
25602+#ifdef __KERNEL__
25603+
dece6358
AM
25604+struct dentry;
25605+struct super_block;
1facf9fc 25606+
25607+#ifdef CONFIG_AUFS_BDEV_LOOP
392086de
AM
25608+/* drivers/block/loop.c */
25609+struct file *loop_backing_file(struct super_block *sb);
25610+
1facf9fc 25611+/* loop.c */
b752ccd1 25612+int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
1facf9fc 25613+int au_test_loopback_kthread(void);
87a755f4
AM
25614+void au_warn_loopback(struct super_block *h_sb);
25615+
25616+int au_loopback_init(void);
25617+void au_loopback_fin(void);
1facf9fc 25618+#else
83b672a5 25619+AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
eca801bf 25620+
4a4d8108 25621+AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
b752ccd1 25622+ struct dentry *h_adding)
4a4d8108 25623+AuStubInt0(au_test_loopback_kthread, void)
87a755f4
AM
25624+AuStubVoid(au_warn_loopback, struct super_block *h_sb)
25625+
25626+AuStubInt0(au_loopback_init, void)
25627+AuStubVoid(au_loopback_fin, void)
1facf9fc 25628+#endif /* BLK_DEV_LOOP */
25629+
25630+#endif /* __KERNEL__ */
25631+#endif /* __AUFS_LOOP_H__ */
7f207e10 25632diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
eca34b5c 25633--- /usr/share/empty/fs/aufs/magic.mk 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 25634+++ linux/fs/aufs/magic.mk 2022-11-05 23:02:18.965889284 +0100
2121bcd9
AM
25635@@ -0,0 +1,31 @@
25636+# SPDX-License-Identifier: GPL-2.0
1facf9fc 25637+
25638+# defined in ${srctree}/fs/fuse/inode.c
25639+# tristate
25640+ifdef CONFIG_FUSE_FS
25641+ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
25642+endif
25643+
1facf9fc 25644+# defined in ${srctree}/fs/xfs/xfs_sb.h
25645+# tristate
25646+ifdef CONFIG_XFS_FS
25647+ccflags-y += -DXFS_SB_MAGIC=0x58465342
25648+endif
25649+
25650+# defined in ${srctree}/fs/configfs/mount.c
25651+# tristate
25652+ifdef CONFIG_CONFIGFS_FS
25653+ccflags-y += -DCONFIGFS_MAGIC=0x62656570
25654+endif
25655+
1facf9fc 25656+# defined in ${srctree}/fs/ubifs/ubifs.h
25657+# tristate
25658+ifdef CONFIG_UBIFS_FS
25659+ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
25660+endif
4a4d8108
AM
25661+
25662+# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
25663+# tristate
25664+ifdef CONFIG_HFSPLUS_FS
25665+ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
25666+endif
7f207e10 25667diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
eca34b5c 25668--- /usr/share/empty/fs/aufs/Makefile 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 25669+++ linux/fs/aufs/Makefile 2022-11-05 23:02:18.959222617 +0100
2121bcd9
AM
25670@@ -0,0 +1,46 @@
25671+# SPDX-License-Identifier: GPL-2.0
4a4d8108
AM
25672+
25673+include ${src}/magic.mk
25674+ifeq (${CONFIG_AUFS_FS},m)
25675+include ${src}/conf.mk
25676+endif
25677+-include ${src}/priv_def.mk
25678+
25679+# cf. include/linux/kernel.h
25680+# enable pr_debug
25681+ccflags-y += -DDEBUG
f6c5ef8b
AM
25682+# sparse requires the full pathname
25683+ifdef M
523b37e3 25684+ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
f6c5ef8b 25685+else
523b37e3 25686+ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
f6c5ef8b 25687+endif
4a4d8108
AM
25688+
25689+obj-$(CONFIG_AUFS_FS) += aufs.o
f4d37d76 25690+aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o fsctx.o \
4a4d8108 25691+ wkq.o vfsub.o dcsub.o \
e49829fe 25692+ cpup.o whout.o wbr_policy.o \
4a4d8108
AM
25693+ dinfo.o dentry.o \
25694+ dynop.o \
25695+ finfo.o file.o f_op.o \
25696+ dir.o vdir.o \
25697+ iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
c2b27bf2 25698+ mvdown.o ioctl.o
4a4d8108
AM
25699+
25700+# all are boolean
e49829fe 25701+aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
4a4d8108
AM
25702+aufs-$(CONFIG_SYSFS) += sysfs.o
25703+aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
25704+aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
25705+aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
25706+aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
4a4d8108 25707+aufs-$(CONFIG_AUFS_EXPORT) += export.o
c1595e42
JR
25708+aufs-$(CONFIG_AUFS_XATTR) += xattr.o
25709+aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
8b6a4947 25710+aufs-$(CONFIG_AUFS_DIRREN) += dirren.o
076b876e 25711+aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
4a4d8108
AM
25712+aufs-$(CONFIG_AUFS_POLL) += poll.o
25713+aufs-$(CONFIG_AUFS_RDU) += rdu.o
4a4d8108
AM
25714+aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
25715+aufs-$(CONFIG_AUFS_DEBUG) += debug.o
25716+aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
7f207e10 25717diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
eca34b5c 25718--- /usr/share/empty/fs/aufs/module.c 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 25719+++ linux/fs/aufs/module.c 2022-11-05 23:02:18.965889284 +0100
062440b3 25720@@ -0,0 +1,273 @@
cd7a4cd9 25721+// SPDX-License-Identifier: GPL-2.0
1facf9fc 25722+/*
0dcfbb52 25723+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 25724+ *
0dcfbb52 25725+ * This program is free software; you can redistribute it and/or modify
1facf9fc 25726+ * it under the terms of the GNU General Public License as published by
25727+ * the Free Software Foundation; either version 2 of the License, or
25728+ * (at your option) any later version.
dece6358
AM
25729+ *
25730+ * This program is distributed in the hope that it will be useful,
25731+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
25732+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25733+ * GNU General Public License for more details.
25734+ *
25735+ * You should have received a copy of the GNU General Public License
523b37e3 25736+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 25737+ */
25738+
25739+/*
25740+ * module global variables and operations
25741+ */
25742+
25743+#include <linux/module.h>
25744+#include <linux/seq_file.h>
25745+#include "aufs.h"
25746+
e2f27e51
AM
25747+/* shrinkable realloc */
25748+void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
1facf9fc 25749+{
e2f27e51
AM
25750+ size_t sz;
25751+ int diff;
1facf9fc 25752+
e2f27e51
AM
25753+ sz = 0;
25754+ diff = -1;
25755+ if (p) {
25756+#if 0 /* unused */
25757+ if (!new_sz) {
9f237c51 25758+ au_kfree_rcu(p);
e2f27e51
AM
25759+ p = NULL;
25760+ goto out;
25761+ }
25762+#else
25763+ AuDebugOn(!new_sz);
25764+#endif
25765+ sz = ksize(p);
25766+ diff = au_kmidx_sub(sz, new_sz);
25767+ }
25768+ if (sz && !diff)
25769+ goto out;
25770+
25771+ if (sz < new_sz)
25772+ /* expand or SLOB */
25773+ p = krealloc(p, new_sz, gfp);
25774+ else if (new_sz < sz && may_shrink) {
25775+ /* shrink */
25776+ void *q;
25777+
25778+ q = kmalloc(new_sz, gfp);
25779+ if (q) {
25780+ if (p) {
25781+ memcpy(q, p, new_sz);
9f237c51 25782+ au_kfree_try_rcu(p);
e2f27e51
AM
25783+ }
25784+ p = q;
25785+ } else
25786+ p = NULL;
25787+ }
25788+
25789+out:
25790+ return p;
25791+}
25792+
25793+void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
25794+ int may_shrink)
25795+{
25796+ p = au_krealloc(p, new_sz, gfp, may_shrink);
25797+ if (p && new_sz > nused)
1facf9fc 25798+ memset(p + nused, 0, new_sz - nused);
25799+ return p;
25800+}
25801+
25802+/* ---------------------------------------------------------------------- */
1facf9fc 25803+/*
25804+ * aufs caches
25805+ */
1c60b727 25806+struct kmem_cache *au_cache[AuCache_Last];
5afbbe0d
AM
25807+
25808+static void au_cache_fin(void)
25809+{
25810+ int i;
25811+
25812+ /*
25813+ * Make sure all delayed rcu free inodes are flushed before we
25814+ * destroy cache.
25815+ */
25816+ rcu_barrier();
25817+
25818+ /* excluding AuCache_HNOTIFY */
25819+ BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
25820+ for (i = 0; i < AuCache_HNOTIFY; i++) {
1c60b727
AM
25821+ kmem_cache_destroy(au_cache[i]);
25822+ au_cache[i] = NULL;
5afbbe0d
AM
25823+ }
25824+}
25825+
1facf9fc 25826+static int __init au_cache_init(void)
25827+{
1c60b727
AM
25828+ au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
25829+ if (au_cache[AuCache_DINFO])
027c5e7a 25830+ /* SLAB_DESTROY_BY_RCU */
1c60b727 25831+ au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
f0c0a007 25832+ au_icntnr_init_once);
1c60b727
AM
25833+ if (au_cache[AuCache_ICNTNR])
25834+ au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
f0c0a007 25835+ au_fi_init_once);
1c60b727
AM
25836+ if (au_cache[AuCache_FINFO])
25837+ au_cache[AuCache_VDIR] = AuCache(au_vdir);
25838+ if (au_cache[AuCache_VDIR])
25839+ au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
25840+ if (au_cache[AuCache_DEHSTR])
1facf9fc 25841+ return 0;
25842+
5afbbe0d 25843+ au_cache_fin();
1facf9fc 25844+ return -ENOMEM;
25845+}
25846+
1facf9fc 25847+/* ---------------------------------------------------------------------- */
25848+
25849+int au_dir_roflags;
25850+
e49829fe 25851+#ifdef CONFIG_AUFS_SBILIST
1e00d052
AM
25852+/*
25853+ * iterate_supers_type() doesn't protect us from
25854+ * remounting (branch management)
25855+ */
8b6a4947 25856+struct hlist_bl_head au_sbilist;
e49829fe
JR
25857+#endif
25858+
1facf9fc 25859+/*
25860+ * functions for module interface.
25861+ */
25862+MODULE_LICENSE("GPL");
25863+/* MODULE_LICENSE("GPL v2"); */
dece6358 25864+MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
1facf9fc 25865+MODULE_DESCRIPTION(AUFS_NAME
25866+ " -- Advanced multi layered unification filesystem");
25867+MODULE_VERSION(AUFS_VERSION);
c06a8ce3 25868+MODULE_ALIAS_FS(AUFS_NAME);
1facf9fc 25869+
1facf9fc 25870+/* this module parameter has no meaning when SYSFS is disabled */
25871+int sysaufs_brs = 1;
25872+MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
cd7a4cd9 25873+module_param_named(brs, sysaufs_brs, int, 0444);
1facf9fc 25874+
076b876e 25875+/* this module parameter has no meaning when USER_NS is disabled */
8cdd5066 25876+bool au_userns;
076b876e 25877+MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
cd7a4cd9 25878+module_param_named(allow_userns, au_userns, bool, 0444);
076b876e 25879+
1facf9fc 25880+/* ---------------------------------------------------------------------- */
25881+
25882+static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
25883+
25884+int au_seq_path(struct seq_file *seq, struct path *path)
25885+{
79b8bda9
AM
25886+ int err;
25887+
25888+ err = seq_path(seq, path, au_esc_chars);
1c60b727 25889+ if (err >= 0)
79b8bda9 25890+ err = 0;
1c60b727 25891+ else
79b8bda9
AM
25892+ err = -ENOMEM;
25893+
25894+ return err;
1facf9fc 25895+}
25896+
25897+/* ---------------------------------------------------------------------- */
25898+
25899+static int __init aufs_init(void)
25900+{
25901+ int err, i;
25902+ char *p;
25903+
25904+ p = au_esc_chars;
25905+ for (i = 1; i <= ' '; i++)
25906+ *p++ = i;
25907+ *p++ = '\\';
25908+ *p++ = '\x7f';
25909+ *p = 0;
25910+
25911+ au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
25912+
b95c5147
AM
25913+ memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
25914+ for (i = 0; i < AuIop_Last; i++)
25915+ aufs_iop_nogetattr[i].getattr = NULL;
25916+
1c60b727 25917+ memset(au_cache, 0, sizeof(au_cache)); /* including hnotify */
f0c0a007 25918+
e49829fe 25919+ au_sbilist_init();
1facf9fc 25920+ sysaufs_brs_init();
25921+ au_debug_init();
4a4d8108 25922+ au_dy_init();
1facf9fc 25923+ err = sysaufs_init();
25924+ if (unlikely(err))
25925+ goto out;
062440b3 25926+ err = dbgaufs_init();
4f0767ce 25927+ if (unlikely(err))
953406b4 25928+ goto out_sysaufs;
062440b3
AM
25929+ err = au_procfs_init();
25930+ if (unlikely(err))
25931+ goto out_dbgaufs;
e49829fe
JR
25932+ err = au_wkq_init();
25933+ if (unlikely(err))
25934+ goto out_procfs;
87a755f4 25935+ err = au_loopback_init();
1facf9fc 25936+ if (unlikely(err))
25937+ goto out_wkq;
87a755f4
AM
25938+ err = au_hnotify_init();
25939+ if (unlikely(err))
25940+ goto out_loopback;
1facf9fc 25941+ err = au_sysrq_init();
25942+ if (unlikely(err))
25943+ goto out_hin;
25944+ err = au_cache_init();
25945+ if (unlikely(err))
25946+ goto out_sysrq;
076b876e
AM
25947+
25948+ aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
1facf9fc 25949+ err = register_filesystem(&aufs_fs_type);
25950+ if (unlikely(err))
25951+ goto out_cache;
076b876e 25952+
4a4d8108
AM
25953+ /* since we define pr_fmt, call printk directly */
25954+ printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
1facf9fc 25955+ goto out; /* success */
25956+
4f0767ce 25957+out_cache:
1facf9fc 25958+ au_cache_fin();
4f0767ce 25959+out_sysrq:
1facf9fc 25960+ au_sysrq_fin();
4f0767ce 25961+out_hin:
4a4d8108 25962+ au_hnotify_fin();
87a755f4
AM
25963+out_loopback:
25964+ au_loopback_fin();
4f0767ce 25965+out_wkq:
1facf9fc 25966+ au_wkq_fin();
e49829fe
JR
25967+out_procfs:
25968+ au_procfs_fin();
062440b3
AM
25969+out_dbgaufs:
25970+ dbgaufs_fin();
4f0767ce 25971+out_sysaufs:
1facf9fc 25972+ sysaufs_fin();
4a4d8108 25973+ au_dy_fin();
4f0767ce 25974+out:
1facf9fc 25975+ return err;
25976+}
25977+
25978+static void __exit aufs_exit(void)
25979+{
25980+ unregister_filesystem(&aufs_fs_type);
25981+ au_cache_fin();
25982+ au_sysrq_fin();
4a4d8108 25983+ au_hnotify_fin();
87a755f4 25984+ au_loopback_fin();
1facf9fc 25985+ au_wkq_fin();
e49829fe 25986+ au_procfs_fin();
062440b3 25987+ dbgaufs_fin();
1facf9fc 25988+ sysaufs_fin();
4a4d8108 25989+ au_dy_fin();
1facf9fc 25990+}
25991+
25992+module_init(aufs_init);
25993+module_exit(aufs_exit);
7f207e10 25994diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
eca34b5c 25995--- /usr/share/empty/fs/aufs/module.h 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 25996+++ linux/fs/aufs/module.h 2022-11-05 23:02:18.969222617 +0100
26eb093c 25997@@ -0,0 +1,180 @@
062440b3 25998+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 25999+/*
0dcfbb52 26000+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 26001+ *
0dcfbb52 26002+ * This program is free software; you can redistribute it and/or modify
1facf9fc 26003+ * it under the terms of the GNU General Public License as published by
26004+ * the Free Software Foundation; either version 2 of the License, or
26005+ * (at your option) any later version.
dece6358
AM
26006+ *
26007+ * This program is distributed in the hope that it will be useful,
26008+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
26009+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26010+ * GNU General Public License for more details.
26011+ *
26012+ * You should have received a copy of the GNU General Public License
523b37e3 26013+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 26014+ */
26015+
26016+/*
26017+ * module initialization and module-global
26018+ */
26019+
26020+#ifndef __AUFS_MODULE_H__
26021+#define __AUFS_MODULE_H__
26022+
26023+#ifdef __KERNEL__
26024+
26025+#include <linux/slab.h>
9f237c51
AM
26026+#include "debug.h"
26027+#include "dentry.h"
26028+#include "dir.h"
26029+#include "file.h"
26030+#include "inode.h"
1facf9fc 26031+
dece6358
AM
26032+struct path;
26033+struct seq_file;
26034+
1facf9fc 26035+/* module parameters */
1facf9fc 26036+extern int sysaufs_brs;
8cdd5066 26037+extern bool au_userns;
1facf9fc 26038+
26039+/* ---------------------------------------------------------------------- */
26040+
26041+extern int au_dir_roflags;
26042+
e2f27e51
AM
26043+void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
26044+void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
26045+ int may_shrink);
26046+
9f237c51
AM
26047+/*
26048+ * Comparing the size of the object with sizeof(struct rcu_head)
26049+ * case 1: object is always larger
26050+ * --> au_kfree_rcu() or au_kfree_do_rcu()
26051+ * case 2: object is always smaller
26052+ * --> au_kfree_small()
26053+ * case 3: object can be any size
26054+ * --> au_kfree_try_rcu()
26055+ */
26056+
26057+static inline void au_kfree_do_rcu(const void *p)
26058+{
26059+ struct {
26060+ struct rcu_head rcu;
26061+ } *a = (void *)p;
26062+
26063+ kfree_rcu(a, rcu);
26064+}
26065+
26066+#define au_kfree_rcu(_p) do { \
26067+ typeof(_p) p = (_p); \
26068+ BUILD_BUG_ON(sizeof(*p) < sizeof(struct rcu_head)); \
26069+ if (p) \
26070+ au_kfree_do_rcu(p); \
26071+ } while (0)
26072+
26073+#define au_kfree_do_sz_test(sz) (sz >= sizeof(struct rcu_head))
26074+#define au_kfree_sz_test(p) (p && au_kfree_do_sz_test(ksize(p)))
26075+
26076+static inline void au_kfree_try_rcu(const void *p)
26077+{
26078+ if (!p)
26079+ return;
26080+ if (au_kfree_sz_test(p))
26081+ au_kfree_do_rcu(p);
26082+ else
26083+ kfree(p);
26084+}
26085+
26086+static inline void au_kfree_small(const void *p)
26087+{
26088+ if (!p)
26089+ return;
26090+ AuDebugOn(au_kfree_sz_test(p));
26091+ kfree(p);
26092+}
26093+
e2f27e51
AM
26094+static inline int au_kmidx_sub(size_t sz, size_t new_sz)
26095+{
26096+#ifndef CONFIG_SLOB
6d176551 26097+ return __kmalloc_index(sz, false) - __kmalloc_index(new_sz, false);
e2f27e51
AM
26098+#else
26099+ return -1; /* SLOB is untested */
26100+#endif
26101+}
26102+
1facf9fc 26103+int au_seq_path(struct seq_file *seq, struct path *path);
26104+
e49829fe
JR
26105+#ifdef CONFIG_PROC_FS
26106+/* procfs.c */
26107+int __init au_procfs_init(void);
26108+void au_procfs_fin(void);
26109+#else
26110+AuStubInt0(au_procfs_init, void);
26111+AuStubVoid(au_procfs_fin, void);
26112+#endif
26113+
4f0767ce
JR
26114+/* ---------------------------------------------------------------------- */
26115+
1c60b727 26116+/* kmem cache */
1facf9fc 26117+enum {
26118+ AuCache_DINFO,
26119+ AuCache_ICNTNR,
26120+ AuCache_FINFO,
26121+ AuCache_VDIR,
26122+ AuCache_DEHSTR,
7eafdf33 26123+ AuCache_HNOTIFY, /* must be last */
1facf9fc 26124+ AuCache_Last
26125+};
26126+
1c60b727 26127+extern struct kmem_cache *au_cache[AuCache_Last];
f0c0a007 26128+
4a4d8108
AM
26129+#define AuCacheFlags (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
26130+#define AuCache(type) KMEM_CACHE(type, AuCacheFlags)
26131+#define AuCacheCtor(type, ctor) \
26132+ kmem_cache_create(#type, sizeof(struct type), \
26133+ __alignof__(struct type), AuCacheFlags, ctor)
1facf9fc 26134+
26eb093c 26135+#define AuCacheFuncAlloc(name, index) \
9f237c51 26136+ static inline struct au_##name *au_cache_alloc_##name(void) \
26eb093c
AM
26137+ { return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); }
26138+
26139+#define AuCacheFuncs(name, index) \
9f237c51
AM
26140+ static inline void au_cache_free_##name##_norcu(struct au_##name *p) \
26141+ { kmem_cache_free(au_cache[AuCache_##index], p); } \
26142+ \
26143+ static inline void au_cache_free_##name##_rcu_cb(struct rcu_head *rcu) \
26144+ { void *p = rcu; \
26145+ p -= offsetof(struct au_##name, rcu); \
26146+ kmem_cache_free(au_cache[AuCache_##index], p); } \
26147+ static inline void au_cache_free_##name##_rcu(struct au_##name *p) \
26148+ { BUILD_BUG_ON(sizeof(struct au_##name) < sizeof(struct rcu_head)); \
26149+ call_rcu(&p->rcu, au_cache_free_##name##_rcu_cb); } \
26150+ \
26151+ static inline void au_cache_free_##name(struct au_##name *p) \
26152+ { /* au_cache_free_##name##_norcu(p); */ \
26153+ au_cache_free_##name##_rcu(p); }
1facf9fc 26154+
26155+AuCacheFuncs(dinfo, DINFO);
26eb093c
AM
26156+AuCacheFuncAlloc(dinfo, DINFO);
26157+
1facf9fc 26158+AuCacheFuncs(icntnr, ICNTNR);
26eb093c
AM
26159+static inline struct au_icntnr *au_cache_alloc_icntnr(struct super_block *sb)
26160+{ return alloc_inode_sb(sb, au_cache[AuCache_ICNTNR], GFP_NOFS); }
26161+
1facf9fc 26162+AuCacheFuncs(finfo, FINFO);
26eb093c
AM
26163+AuCacheFuncAlloc(finfo, FINFO);
26164+
1facf9fc 26165+AuCacheFuncs(vdir, VDIR);
26eb093c
AM
26166+AuCacheFuncAlloc(vdir, VDIR);
26167+
4a4d8108 26168+AuCacheFuncs(vdir_dehstr, DEHSTR);
26eb093c
AM
26169+AuCacheFuncAlloc(vdir_dehstr, DEHSTR);
26170+
4a4d8108
AM
26171+#ifdef CONFIG_AUFS_HNOTIFY
26172+AuCacheFuncs(hnotify, HNOTIFY);
26eb093c 26173+AuCacheFuncAlloc(hnotify, HNOTIFY);
4a4d8108 26174+#endif
1facf9fc 26175+
4a4d8108
AM
26176+#endif /* __KERNEL__ */
26177+#endif /* __AUFS_MODULE_H__ */
c2b27bf2 26178diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
eca34b5c 26179--- /usr/share/empty/fs/aufs/mvdown.c 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 26180+++ linux/fs/aufs/mvdown.c 2022-11-05 23:02:18.969222617 +0100
ba1aed25 26181@@ -0,0 +1,706 @@
cd7a4cd9 26182+// SPDX-License-Identifier: GPL-2.0
c2b27bf2 26183+/*
0dcfbb52 26184+ * Copyright (C) 2011-2022 Junjiro R. Okajima
c2b27bf2 26185+ *
0dcfbb52 26186+ * This program is free software; you can redistribute it and/or modify
c2b27bf2
AM
26187+ * it under the terms of the GNU General Public License as published by
26188+ * the Free Software Foundation; either version 2 of the License, or
26189+ * (at your option) any later version.
26190+ *
26191+ * This program is distributed in the hope that it will be useful,
26192+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
26193+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26194+ * GNU General Public License for more details.
26195+ *
26196+ * You should have received a copy of the GNU General Public License
523b37e3
AM
26197+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
26198+ */
26199+
26200+/*
26201+ * move-down, opposite of copy-up
c2b27bf2
AM
26202+ */
26203+
26204+#include "aufs.h"
26205+
c2b27bf2
AM
26206+struct au_mvd_args {
26207+ struct {
c2b27bf2
AM
26208+ struct super_block *h_sb;
26209+ struct dentry *h_parent;
26210+ struct au_hinode *hdir;
392086de 26211+ struct inode *h_dir, *h_inode;
c1595e42 26212+ struct au_pin pin;
c2b27bf2
AM
26213+ } info[AUFS_MVDOWN_NARRAY];
26214+
26215+ struct aufs_mvdown mvdown;
26216+ struct dentry *dentry, *parent;
26217+ struct inode *inode, *dir;
26218+ struct super_block *sb;
26219+ aufs_bindex_t bopq, bwh, bfound;
26220+ unsigned char rename_lock;
c2b27bf2
AM
26221+};
26222+
392086de 26223+#define mvd_errno mvdown.au_errno
076b876e
AM
26224+#define mvd_bsrc mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
26225+#define mvd_src_brid mvdown.stbr[AUFS_MVDOWN_UPPER].brid
26226+#define mvd_bdst mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
26227+#define mvd_dst_brid mvdown.stbr[AUFS_MVDOWN_LOWER].brid
c2b27bf2 26228+
392086de
AM
26229+#define mvd_h_src_sb info[AUFS_MVDOWN_UPPER].h_sb
26230+#define mvd_h_src_parent info[AUFS_MVDOWN_UPPER].h_parent
26231+#define mvd_hdir_src info[AUFS_MVDOWN_UPPER].hdir
26232+#define mvd_h_src_dir info[AUFS_MVDOWN_UPPER].h_dir
26233+#define mvd_h_src_inode info[AUFS_MVDOWN_UPPER].h_inode
c1595e42 26234+#define mvd_pin_src info[AUFS_MVDOWN_UPPER].pin
392086de
AM
26235+
26236+#define mvd_h_dst_sb info[AUFS_MVDOWN_LOWER].h_sb
26237+#define mvd_h_dst_parent info[AUFS_MVDOWN_LOWER].h_parent
26238+#define mvd_hdir_dst info[AUFS_MVDOWN_LOWER].hdir
26239+#define mvd_h_dst_dir info[AUFS_MVDOWN_LOWER].h_dir
26240+#define mvd_h_dst_inode info[AUFS_MVDOWN_LOWER].h_inode
c1595e42 26241+#define mvd_pin_dst info[AUFS_MVDOWN_LOWER].pin
c2b27bf2
AM
26242+
26243+#define AU_MVD_PR(flag, ...) do { \
26244+ if (flag) \
26245+ pr_err(__VA_ARGS__); \
26246+ } while (0)
26247+
076b876e
AM
26248+static int find_lower_writable(struct au_mvd_args *a)
26249+{
26250+ struct super_block *sb;
5afbbe0d 26251+ aufs_bindex_t bindex, bbot;
076b876e
AM
26252+ struct au_branch *br;
26253+
26254+ sb = a->sb;
26255+ bindex = a->mvd_bsrc;
5afbbe0d 26256+ bbot = au_sbbot(sb);
076b876e 26257+ if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
5afbbe0d 26258+ for (bindex++; bindex <= bbot; bindex++) {
076b876e
AM
26259+ br = au_sbr(sb, bindex);
26260+ if (au_br_fhsm(br->br_perm)
8b6a4947 26261+ && !sb_rdonly(au_br_sb(br)))
076b876e
AM
26262+ return bindex;
26263+ }
26264+ else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
5afbbe0d 26265+ for (bindex++; bindex <= bbot; bindex++) {
076b876e
AM
26266+ br = au_sbr(sb, bindex);
26267+ if (!au_br_rdonly(br))
26268+ return bindex;
26269+ }
26270+ else
5afbbe0d 26271+ for (bindex++; bindex <= bbot; bindex++) {
076b876e 26272+ br = au_sbr(sb, bindex);
8b6a4947 26273+ if (!sb_rdonly(au_br_sb(br))) {
076b876e
AM
26274+ if (au_br_rdonly(br))
26275+ a->mvdown.flags
26276+ |= AUFS_MVDOWN_ROLOWER_R;
26277+ return bindex;
26278+ }
26279+ }
26280+
26281+ return -1;
26282+}
26283+
c2b27bf2 26284+/* make the parent dir on bdst */
392086de 26285+static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
26286+{
26287+ int err;
26288+
26289+ err = 0;
26290+ a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
26291+ a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
26292+ a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
26293+ a->mvd_h_dst_parent = NULL;
5afbbe0d 26294+ if (au_dbbot(a->parent) >= a->mvd_bdst)
c2b27bf2
AM
26295+ a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
26296+ if (!a->mvd_h_dst_parent) {
26297+ err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
26298+ if (unlikely(err)) {
392086de 26299+ AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
c2b27bf2
AM
26300+ goto out;
26301+ }
26302+ a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
26303+ }
26304+
26305+out:
26306+ AuTraceErr(err);
26307+ return err;
26308+}
26309+
26310+/* lock them all */
392086de 26311+static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
26312+{
26313+ int err;
26314+ struct dentry *h_trap;
26315+
26316+ a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
26317+ a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
c1595e42
JR
26318+ err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
26319+ au_opt_udba(a->sb),
26320+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
26321+ AuTraceErr(err);
26322+ if (unlikely(err)) {
26323+ AU_MVD_PR(dmsg, "pin_dst failed\n");
26324+ goto out;
26325+ }
26326+
c2b27bf2
AM
26327+ if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
26328+ a->rename_lock = 0;
c1595e42
JR
26329+ au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
26330+ AuLsc_DI_PARENT, AuLsc_I_PARENT3,
26331+ au_opt_udba(a->sb),
26332+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
26333+ err = au_do_pin(&a->mvd_pin_src);
26334+ AuTraceErr(err);
5527c038 26335+ a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
c1595e42
JR
26336+ if (unlikely(err)) {
26337+ AU_MVD_PR(dmsg, "pin_src failed\n");
26338+ goto out_dst;
26339+ }
26340+ goto out; /* success */
c2b27bf2
AM
26341+ }
26342+
c2b27bf2 26343+ a->rename_lock = 1;
c1595e42
JR
26344+ au_pin_hdir_unlock(&a->mvd_pin_dst);
26345+ err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
26346+ au_opt_udba(a->sb),
26347+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
26348+ AuTraceErr(err);
5527c038 26349+ a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
c1595e42
JR
26350+ if (unlikely(err)) {
26351+ AU_MVD_PR(dmsg, "pin_src failed\n");
26352+ au_pin_hdir_lock(&a->mvd_pin_dst);
26353+ goto out_dst;
26354+ }
26355+ au_pin_hdir_unlock(&a->mvd_pin_src);
c2b27bf2
AM
26356+ h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
26357+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
26358+ if (h_trap) {
26359+ err = (h_trap != a->mvd_h_src_parent);
26360+ if (err)
26361+ err = (h_trap != a->mvd_h_dst_parent);
26362+ }
26363+ BUG_ON(err); /* it should never happen */
c1595e42
JR
26364+ if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
26365+ err = -EBUSY;
26366+ AuTraceErr(err);
26367+ vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
26368+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
26369+ au_pin_hdir_lock(&a->mvd_pin_src);
26370+ au_unpin(&a->mvd_pin_src);
26371+ au_pin_hdir_lock(&a->mvd_pin_dst);
26372+ goto out_dst;
26373+ }
26374+ goto out; /* success */
c2b27bf2 26375+
c1595e42
JR
26376+out_dst:
26377+ au_unpin(&a->mvd_pin_dst);
c2b27bf2
AM
26378+out:
26379+ AuTraceErr(err);
26380+ return err;
26381+}
26382+
392086de 26383+static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2 26384+{
c1595e42
JR
26385+ if (!a->rename_lock)
26386+ au_unpin(&a->mvd_pin_src);
26387+ else {
c2b27bf2
AM
26388+ vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
26389+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
c1595e42
JR
26390+ au_pin_hdir_lock(&a->mvd_pin_src);
26391+ au_unpin(&a->mvd_pin_src);
26392+ au_pin_hdir_lock(&a->mvd_pin_dst);
26393+ }
26394+ au_unpin(&a->mvd_pin_dst);
c2b27bf2
AM
26395+}
26396+
26397+/* copy-down the file */
392086de 26398+static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
26399+{
26400+ int err;
26401+ struct au_cp_generic cpg = {
26402+ .dentry = a->dentry,
26403+ .bdst = a->mvd_bdst,
26404+ .bsrc = a->mvd_bsrc,
26405+ .len = -1,
c1595e42 26406+ .pin = &a->mvd_pin_dst,
c2b27bf2
AM
26407+ .flags = AuCpup_DTIME | AuCpup_HOPEN
26408+ };
26409+
26410+ AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
392086de
AM
26411+ if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
26412+ au_fset_cpup(cpg.flags, OVERWRITE);
26413+ if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
26414+ au_fset_cpup(cpg.flags, RWDST);
c2b27bf2
AM
26415+ err = au_sio_cpdown_simple(&cpg);
26416+ if (unlikely(err))
392086de 26417+ AU_MVD_PR(dmsg, "cpdown failed\n");
c2b27bf2
AM
26418+
26419+ AuTraceErr(err);
26420+ return err;
26421+}
26422+
26423+/*
26424+ * unlink the whiteout on bdst if exist which may be created by UDBA while we
26425+ * were sleeping
26426+ */
392086de 26427+static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
26428+{
26429+ int err;
26430+ struct path h_path;
26431+ struct au_branch *br;
523b37e3 26432+ struct inode *delegated;
c2b27bf2
AM
26433+
26434+ br = au_sbr(a->sb, a->mvd_bdst);
26435+ h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
26436+ err = PTR_ERR(h_path.dentry);
26437+ if (IS_ERR(h_path.dentry)) {
392086de 26438+ AU_MVD_PR(dmsg, "wh_lkup failed\n");
c2b27bf2
AM
26439+ goto out;
26440+ }
26441+
26442+ err = 0;
5527c038 26443+ if (d_is_positive(h_path.dentry)) {
c2b27bf2 26444+ h_path.mnt = au_br_mnt(br);
523b37e3 26445+ delegated = NULL;
5527c038 26446+ err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
523b37e3
AM
26447+ &delegated, /*force*/0);
26448+ if (unlikely(err == -EWOULDBLOCK)) {
26449+ pr_warn("cannot retry for NFSv4 delegation"
26450+ " for an internal unlink\n");
26451+ iput(delegated);
26452+ }
c2b27bf2 26453+ if (unlikely(err))
392086de 26454+ AU_MVD_PR(dmsg, "wh_unlink failed\n");
c2b27bf2
AM
26455+ }
26456+ dput(h_path.dentry);
26457+
26458+out:
26459+ AuTraceErr(err);
26460+ return err;
26461+}
26462+
26463+/*
26464+ * unlink the topmost h_dentry
c2b27bf2 26465+ */
392086de 26466+static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
26467+{
26468+ int err;
26469+ struct path h_path;
523b37e3 26470+ struct inode *delegated;
c2b27bf2
AM
26471+
26472+ h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
26473+ h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
523b37e3
AM
26474+ delegated = NULL;
26475+ err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
26476+ if (unlikely(err == -EWOULDBLOCK)) {
26477+ pr_warn("cannot retry for NFSv4 delegation"
26478+ " for an internal unlink\n");
26479+ iput(delegated);
26480+ }
c2b27bf2 26481+ if (unlikely(err))
392086de 26482+ AU_MVD_PR(dmsg, "unlink failed\n");
c2b27bf2
AM
26483+
26484+ AuTraceErr(err);
26485+ return err;
26486+}
26487+
076b876e
AM
26488+/* Since mvdown succeeded, we ignore an error of this function */
26489+static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
26490+{
26491+ int err;
26492+ struct au_branch *br;
26493+
26494+ a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
26495+ br = au_sbr(a->sb, a->mvd_bsrc);
26496+ err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
26497+ if (!err) {
26498+ br = au_sbr(a->sb, a->mvd_bdst);
26499+ a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
26500+ err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
26501+ }
26502+ if (!err)
26503+ a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
26504+ else
26505+ AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
26506+}
26507+
c2b27bf2
AM
26508+/*
26509+ * copy-down the file and unlink the bsrc file.
26510+ * - unlink the bdst whout if exist
26511+ * - copy-down the file (with whtmp name and rename)
26512+ * - unlink the bsrc file
26513+ */
392086de 26514+static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
26515+{
26516+ int err;
26517+
392086de 26518+ err = au_do_mkdir(dmsg, a);
c2b27bf2 26519+ if (!err)
392086de 26520+ err = au_do_lock(dmsg, a);
c2b27bf2
AM
26521+ if (unlikely(err))
26522+ goto out;
26523+
26524+ /*
26525+ * do not revert the activities we made on bdst since they should be
26526+ * harmless in aufs.
26527+ */
26528+
392086de 26529+ err = au_do_cpdown(dmsg, a);
c2b27bf2 26530+ if (!err)
392086de
AM
26531+ err = au_do_unlink_wh(dmsg, a);
26532+ if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
26533+ err = au_do_unlink(dmsg, a);
c2b27bf2
AM
26534+ if (unlikely(err))
26535+ goto out_unlock;
26536+
c1595e42
JR
26537+ AuDbg("%pd2, 0x%x, %d --> %d\n",
26538+ a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
076b876e
AM
26539+ if (find_lower_writable(a) < 0)
26540+ a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
26541+
26542+ if (a->mvdown.flags & AUFS_MVDOWN_STFS)
26543+ au_do_stfs(dmsg, a);
26544+
c2b27bf2 26545+ /* maintain internal array */
392086de
AM
26546+ if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
26547+ au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
5afbbe0d 26548+ au_set_dbtop(a->dentry, a->mvd_bdst);
392086de 26549+ au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
5afbbe0d 26550+ au_set_ibtop(a->inode, a->mvd_bdst);
79b8bda9
AM
26551+ } else {
26552+ /* hide the lower */
26553+ au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
5afbbe0d 26554+ au_set_dbbot(a->dentry, a->mvd_bsrc);
79b8bda9 26555+ au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
5afbbe0d 26556+ au_set_ibbot(a->inode, a->mvd_bsrc);
392086de 26557+ }
5afbbe0d
AM
26558+ if (au_dbbot(a->dentry) < a->mvd_bdst)
26559+ au_set_dbbot(a->dentry, a->mvd_bdst);
26560+ if (au_ibbot(a->inode) < a->mvd_bdst)
26561+ au_set_ibbot(a->inode, a->mvd_bdst);
c2b27bf2
AM
26562+
26563+out_unlock:
392086de 26564+ au_do_unlock(dmsg, a);
c2b27bf2
AM
26565+out:
26566+ AuTraceErr(err);
26567+ return err;
26568+}
26569+
26570+/* ---------------------------------------------------------------------- */
26571+
c2b27bf2 26572+/* make sure the file is idle */
392086de 26573+static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
26574+{
26575+ int err, plinked;
c2b27bf2
AM
26576+
26577+ err = 0;
c2b27bf2 26578+ plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
5afbbe0d 26579+ if (au_dbtop(a->dentry) == a->mvd_bsrc
c1595e42 26580+ && au_dcount(a->dentry) == 1
c2b27bf2 26581+ && atomic_read(&a->inode->i_count) == 1
392086de 26582+ /* && a->mvd_h_src_inode->i_nlink == 1 */
c2b27bf2
AM
26583+ && (!plinked || !au_plink_test(a->inode))
26584+ && a->inode->i_nlink == 1)
26585+ goto out;
26586+
26587+ err = -EBUSY;
392086de 26588+ AU_MVD_PR(dmsg,
c1595e42 26589+ "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
5afbbe0d 26590+ a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
c2b27bf2 26591+ atomic_read(&a->inode->i_count), a->inode->i_nlink,
392086de 26592+ a->mvd_h_src_inode->i_nlink,
c2b27bf2
AM
26593+ plinked, plinked ? au_plink_test(a->inode) : 0);
26594+
26595+out:
26596+ AuTraceErr(err);
26597+ return err;
26598+}
26599+
26600+/* make sure the parent dir is fine */
392086de 26601+static int au_mvd_args_parent(const unsigned char dmsg,
c2b27bf2
AM
26602+ struct au_mvd_args *a)
26603+{
26604+ int err;
26605+ aufs_bindex_t bindex;
26606+
26607+ err = 0;
26608+ if (unlikely(au_alive_dir(a->parent))) {
26609+ err = -ENOENT;
392086de 26610+ AU_MVD_PR(dmsg, "parent dir is dead\n");
c2b27bf2
AM
26611+ goto out;
26612+ }
26613+
26614+ a->bopq = au_dbdiropq(a->parent);
26615+ bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
26616+ AuDbg("b%d\n", bindex);
26617+ if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
26618+ || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
26619+ err = -EINVAL;
392086de
AM
26620+ a->mvd_errno = EAU_MVDOWN_OPAQUE;
26621+ AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
c2b27bf2
AM
26622+ a->bopq, a->mvd_bdst);
26623+ }
26624+
26625+out:
26626+ AuTraceErr(err);
26627+ return err;
26628+}
26629+
392086de 26630+static int au_mvd_args_intermediate(const unsigned char dmsg,
c2b27bf2
AM
26631+ struct au_mvd_args *a)
26632+{
26633+ int err;
26634+ struct au_dinfo *dinfo, *tmp;
26635+
26636+ /* lookup the next lower positive entry */
26637+ err = -ENOMEM;
26638+ tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
26639+ if (unlikely(!tmp))
26640+ goto out;
26641+
26642+ a->bfound = -1;
26643+ a->bwh = -1;
26644+ dinfo = au_di(a->dentry);
26645+ au_di_cp(tmp, dinfo);
26646+ au_di_swap(tmp, dinfo);
26647+
26648+ /* returns the number of positive dentries */
5afbbe0d
AM
26649+ err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
26650+ /* AuLkup_IGNORE_PERM */ 0);
c2b27bf2
AM
26651+ if (!err)
26652+ a->bwh = au_dbwh(a->dentry);
26653+ else if (err > 0)
5afbbe0d 26654+ a->bfound = au_dbtop(a->dentry);
c2b27bf2
AM
26655+
26656+ au_di_swap(tmp, dinfo);
26657+ au_rw_write_unlock(&tmp->di_rwsem);
26658+ au_di_free(tmp);
26659+ if (unlikely(err < 0))
392086de 26660+ AU_MVD_PR(dmsg, "failed look-up lower\n");
c2b27bf2
AM
26661+
26662+ /*
26663+ * here, we have these cases.
26664+ * bfound == -1
26665+ * no positive dentry under bsrc. there are more sub-cases.
26666+ * bwh < 0
26667+ * there no whiteout, we can safely move-down.
26668+ * bwh <= bsrc
26669+ * impossible
26670+ * bsrc < bwh && bwh < bdst
26671+ * there is a whiteout on RO branch. cannot proceed.
26672+ * bwh == bdst
26673+ * there is a whiteout on the RW target branch. it should
26674+ * be removed.
26675+ * bdst < bwh
26676+ * there is a whiteout somewhere unrelated branch.
26677+ * -1 < bfound && bfound <= bsrc
26678+ * impossible.
26679+ * bfound < bdst
26680+ * found, but it is on RO branch between bsrc and bdst. cannot
26681+ * proceed.
26682+ * bfound == bdst
26683+ * found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
26684+ * error.
26685+ * bdst < bfound
26686+ * found, after we create the file on bdst, it will be hidden.
26687+ */
26688+
26689+ AuDebugOn(a->bfound == -1
26690+ && a->bwh != -1
26691+ && a->bwh <= a->mvd_bsrc);
26692+ AuDebugOn(-1 < a->bfound
26693+ && a->bfound <= a->mvd_bsrc);
26694+
26695+ err = -EINVAL;
26696+ if (a->bfound == -1
26697+ && a->mvd_bsrc < a->bwh
26698+ && a->bwh != -1
26699+ && a->bwh < a->mvd_bdst) {
392086de
AM
26700+ a->mvd_errno = EAU_MVDOWN_WHITEOUT;
26701+ AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
c2b27bf2
AM
26702+ a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
26703+ goto out;
26704+ } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
392086de
AM
26705+ a->mvd_errno = EAU_MVDOWN_UPPER;
26706+ AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
c2b27bf2
AM
26707+ a->mvd_bdst, a->bfound);
26708+ goto out;
26709+ }
26710+
26711+ err = 0; /* success */
26712+
26713+out:
26714+ AuTraceErr(err);
26715+ return err;
26716+}
26717+
392086de 26718+static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
26719+{
26720+ int err;
26721+
392086de
AM
26722+ err = 0;
26723+ if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
26724+ && a->bfound == a->mvd_bdst)
26725+ err = -EEXIST;
c2b27bf2
AM
26726+ AuTraceErr(err);
26727+ return err;
26728+}
26729+
392086de 26730+static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
26731+{
26732+ int err;
26733+ struct au_branch *br;
26734+
26735+ err = -EISDIR;
26736+ if (unlikely(S_ISDIR(a->inode->i_mode)))
26737+ goto out;
26738+
26739+ err = -EINVAL;
392086de 26740+ if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
5afbbe0d 26741+ a->mvd_bsrc = au_ibtop(a->inode);
392086de
AM
26742+ else {
26743+ a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
26744+ if (unlikely(a->mvd_bsrc < 0
5afbbe0d
AM
26745+ || (a->mvd_bsrc < au_dbtop(a->dentry)
26746+ || au_dbbot(a->dentry) < a->mvd_bsrc
392086de 26747+ || !au_h_dptr(a->dentry, a->mvd_bsrc))
5afbbe0d
AM
26748+ || (a->mvd_bsrc < au_ibtop(a->inode)
26749+ || au_ibbot(a->inode) < a->mvd_bsrc
392086de
AM
26750+ || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
26751+ a->mvd_errno = EAU_MVDOWN_NOUPPER;
26752+ AU_MVD_PR(dmsg, "no upper\n");
26753+ goto out;
26754+ }
26755+ }
5afbbe0d 26756+ if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
392086de
AM
26757+ a->mvd_errno = EAU_MVDOWN_BOTTOM;
26758+ AU_MVD_PR(dmsg, "on the bottom\n");
c2b27bf2
AM
26759+ goto out;
26760+ }
392086de 26761+ a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
c2b27bf2
AM
26762+ br = au_sbr(a->sb, a->mvd_bsrc);
26763+ err = au_br_rdonly(br);
392086de
AM
26764+ if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
26765+ if (unlikely(err))
26766+ goto out;
26767+ } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
26768+ || IS_APPEND(a->mvd_h_src_inode))) {
26769+ if (err)
26770+ a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
26771+ /* go on */
26772+ } else
c2b27bf2
AM
26773+ goto out;
26774+
26775+ err = -EINVAL;
392086de
AM
26776+ if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
26777+ a->mvd_bdst = find_lower_writable(a);
26778+ if (unlikely(a->mvd_bdst < 0)) {
26779+ a->mvd_errno = EAU_MVDOWN_BOTTOM;
26780+ AU_MVD_PR(dmsg, "no writable lower branch\n");
26781+ goto out;
26782+ }
26783+ } else {
26784+ a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
26785+ if (unlikely(a->mvd_bdst < 0
5afbbe0d 26786+ || au_sbbot(a->sb) < a->mvd_bdst)) {
392086de
AM
26787+ a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
26788+ AU_MVD_PR(dmsg, "no lower brid\n");
26789+ goto out;
26790+ }
c2b27bf2
AM
26791+ }
26792+
392086de 26793+ err = au_mvd_args_busy(dmsg, a);
c2b27bf2 26794+ if (!err)
392086de 26795+ err = au_mvd_args_parent(dmsg, a);
c2b27bf2 26796+ if (!err)
392086de 26797+ err = au_mvd_args_intermediate(dmsg, a);
c2b27bf2 26798+ if (!err)
392086de 26799+ err = au_mvd_args_exist(dmsg, a);
c2b27bf2
AM
26800+ if (!err)
26801+ AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
26802+
26803+out:
26804+ AuTraceErr(err);
26805+ return err;
26806+}
26807+
26808+int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
26809+{
392086de
AM
26810+ int err, e;
26811+ unsigned char dmsg;
26812+ struct au_mvd_args *args;
79b8bda9 26813+ struct inode *inode;
c2b27bf2 26814+
79b8bda9 26815+ inode = d_inode(dentry);
c2b27bf2
AM
26816+ err = -EPERM;
26817+ if (unlikely(!capable(CAP_SYS_ADMIN)))
26818+ goto out;
26819+
392086de
AM
26820+ err = -ENOMEM;
26821+ args = kmalloc(sizeof(*args), GFP_NOFS);
26822+ if (unlikely(!args))
26823+ goto out;
26824+
26825+ err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
26826+ if (!err)
ba1aed25
AM
26827+ /* VERIFY_WRITE */
26828+ err = !access_ok(uarg, sizeof(*uarg));
c2b27bf2
AM
26829+ if (unlikely(err)) {
26830+ err = -EFAULT;
392086de
AM
26831+ AuTraceErr(err);
26832+ goto out_free;
c2b27bf2 26833+ }
392086de
AM
26834+ AuDbg("flags 0x%x\n", args->mvdown.flags);
26835+ args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
26836+ args->mvdown.au_errno = 0;
26837+ args->dentry = dentry;
79b8bda9 26838+ args->inode = inode;
392086de 26839+ args->sb = dentry->d_sb;
c2b27bf2 26840+
392086de
AM
26841+ err = -ENOENT;
26842+ dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
26843+ args->parent = dget_parent(dentry);
5527c038 26844+ args->dir = d_inode(args->parent);
febd17d6 26845+ inode_lock_nested(args->dir, I_MUTEX_PARENT);
392086de
AM
26846+ dput(args->parent);
26847+ if (unlikely(args->parent != dentry->d_parent)) {
26848+ AU_MVD_PR(dmsg, "parent dir is moved\n");
c2b27bf2
AM
26849+ goto out_dir;
26850+ }
26851+
febd17d6 26852+ inode_lock_nested(inode, I_MUTEX_CHILD);
b95c5147 26853+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
c2b27bf2
AM
26854+ if (unlikely(err))
26855+ goto out_inode;
26856+
392086de
AM
26857+ di_write_lock_parent(args->parent);
26858+ err = au_mvd_args(dmsg, args);
c2b27bf2
AM
26859+ if (unlikely(err))
26860+ goto out_parent;
26861+
392086de 26862+ err = au_do_mvdown(dmsg, args);
c2b27bf2
AM
26863+ if (unlikely(err))
26864+ goto out_parent;
c2b27bf2 26865+
392086de 26866+ au_cpup_attr_timesizes(args->dir);
79b8bda9
AM
26867+ au_cpup_attr_timesizes(inode);
26868+ if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
26869+ au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
c2b27bf2
AM
26870+ /* au_digen_dec(dentry); */
26871+
26872+out_parent:
392086de 26873+ di_write_unlock(args->parent);
c2b27bf2
AM
26874+ aufs_read_unlock(dentry, AuLock_DW);
26875+out_inode:
febd17d6 26876+ inode_unlock(inode);
c2b27bf2 26877+out_dir:
febd17d6 26878+ inode_unlock(args->dir);
392086de
AM
26879+out_free:
26880+ e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
26881+ if (unlikely(e))
26882+ err = -EFAULT;
9f237c51 26883+ au_kfree_rcu(args);
c2b27bf2
AM
26884+out:
26885+ AuTraceErr(err);
26886+ return err;
26887+}
26888diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
eca34b5c 26889--- /usr/share/empty/fs/aufs/opts.c 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 26890+++ linux/fs/aufs/opts.c 2022-11-05 23:02:18.969222617 +0100
f4d37d76 26891@@ -0,0 +1,1032 @@
cd7a4cd9 26892+// SPDX-License-Identifier: GPL-2.0
1facf9fc 26893+/*
0dcfbb52 26894+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 26895+ *
0dcfbb52 26896+ * This program is free software; you can redistribute it and/or modify
1facf9fc 26897+ * it under the terms of the GNU General Public License as published by
26898+ * the Free Software Foundation; either version 2 of the License, or
26899+ * (at your option) any later version.
dece6358
AM
26900+ *
26901+ * This program is distributed in the hope that it will be useful,
26902+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
26903+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26904+ * GNU General Public License for more details.
26905+ *
26906+ * You should have received a copy of the GNU General Public License
523b37e3 26907+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 26908+ */
26909+
26910+/*
26911+ * mount options/flags
26912+ */
26913+
26914+#include <linux/types.h> /* a distribution requires */
26915+#include <linux/parser.h>
26916+#include "aufs.h"
26917+
26918+/* ---------------------------------------------------------------------- */
26919+
076b876e 26920+static const char *au_parser_pattern(int val, match_table_t tbl)
1facf9fc 26921+{
076b876e
AM
26922+ struct match_token *p;
26923+
26924+ p = tbl;
26925+ while (p->pattern) {
26926+ if (p->token == val)
26927+ return p->pattern;
26928+ p++;
1facf9fc 26929+ }
26930+ BUG();
26931+ return "??";
26932+}
26933+
076b876e
AM
26934+static const char *au_optstr(int *val, match_table_t tbl)
26935+{
26936+ struct match_token *p;
26937+ int v;
26938+
26939+ v = *val;
2000de60
JR
26940+ if (!v)
26941+ goto out;
076b876e 26942+ p = tbl;
2000de60
JR
26943+ while (p->pattern) {
26944+ if (p->token
26945+ && (v & p->token) == p->token) {
076b876e
AM
26946+ *val &= ~p->token;
26947+ return p->pattern;
26948+ }
26949+ p++;
26950+ }
2000de60
JR
26951+
26952+out:
076b876e
AM
26953+ return NULL;
26954+}
26955+
1facf9fc 26956+/* ---------------------------------------------------------------------- */
26957+
1e00d052 26958+static match_table_t brperm = {
1facf9fc 26959+ {AuBrPerm_RO, AUFS_BRPERM_RO},
26960+ {AuBrPerm_RR, AUFS_BRPERM_RR},
26961+ {AuBrPerm_RW, AUFS_BRPERM_RW},
1e00d052
AM
26962+ {0, NULL}
26963+};
1facf9fc 26964+
86dc4139 26965+static match_table_t brattr = {
076b876e
AM
26966+ /* general */
26967+ {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
26968+ {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
c1595e42 26969+ /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
86dc4139 26970+ {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
2000de60 26971+#ifdef CONFIG_AUFS_FHSM
076b876e 26972+ {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
2000de60
JR
26973+#endif
26974+#ifdef CONFIG_AUFS_XATTR
c1595e42
JR
26975+ {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
26976+ {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
26977+ {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
26978+ {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
26979+ {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
26980+ {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
2000de60 26981+#endif
076b876e
AM
26982+
26983+ /* ro/rr branch */
1e00d052 26984+ {AuBrRAttr_WH, AUFS_BRRATTR_WH},
076b876e
AM
26985+
26986+ /* rw branch */
26987+ {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
1e00d052 26988+ {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
076b876e 26989+
1e00d052 26990+ {0, NULL}
1facf9fc 26991+};
26992+
1e00d052
AM
26993+static int br_attr_val(char *str, match_table_t table, substring_t args[])
26994+{
26995+ int attr, v;
26996+ char *p;
26997+
26998+ attr = 0;
26999+ do {
27000+ p = strchr(str, '+');
27001+ if (p)
27002+ *p = 0;
27003+ v = match_token(str, table, args);
076b876e
AM
27004+ if (v) {
27005+ if (v & AuBrAttr_CMOO_Mask)
27006+ attr &= ~AuBrAttr_CMOO_Mask;
1e00d052 27007+ attr |= v;
076b876e 27008+ } else {
1e00d052
AM
27009+ if (p)
27010+ *p = '+';
0c3ec466 27011+ pr_warn("ignored branch attribute %s\n", str);
1e00d052
AM
27012+ break;
27013+ }
27014+ if (p)
27015+ str = p + 1;
27016+ } while (p);
27017+
27018+ return attr;
27019+}
27020+
076b876e
AM
27021+static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
27022+{
27023+ int sz;
27024+ const char *p;
27025+ char *q;
27026+
076b876e
AM
27027+ q = str->a;
27028+ *q = 0;
27029+ p = au_optstr(&perm, brattr);
27030+ if (p) {
27031+ sz = strlen(p);
27032+ memcpy(q, p, sz + 1);
27033+ q += sz;
27034+ } else
27035+ goto out;
27036+
27037+ do {
27038+ p = au_optstr(&perm, brattr);
27039+ if (p) {
27040+ *q++ = '+';
27041+ sz = strlen(p);
27042+ memcpy(q, p, sz + 1);
27043+ q += sz;
27044+ }
27045+ } while (p);
27046+
27047+out:
c1595e42 27048+ return q - str->a;
076b876e
AM
27049+}
27050+
f4d37d76 27051+int au_br_perm_val(char *perm)
1facf9fc 27052+{
076b876e
AM
27053+ int val, bad, sz;
27054+ char *p;
1facf9fc 27055+ substring_t args[MAX_OPT_ARGS];
076b876e 27056+ au_br_perm_str_t attr;
1facf9fc 27057+
1e00d052
AM
27058+ p = strchr(perm, '+');
27059+ if (p)
27060+ *p = 0;
27061+ val = match_token(perm, brperm, args);
27062+ if (!val) {
27063+ if (p)
27064+ *p = '+';
0c3ec466 27065+ pr_warn("ignored branch permission %s\n", perm);
1e00d052
AM
27066+ val = AuBrPerm_RO;
27067+ goto out;
27068+ }
27069+ if (!p)
27070+ goto out;
27071+
076b876e
AM
27072+ val |= br_attr_val(p + 1, brattr, args);
27073+
27074+ bad = 0;
86dc4139 27075+ switch (val & AuBrPerm_Mask) {
1e00d052
AM
27076+ case AuBrPerm_RO:
27077+ case AuBrPerm_RR:
076b876e
AM
27078+ bad = val & AuBrWAttr_Mask;
27079+ val &= ~AuBrWAttr_Mask;
1e00d052
AM
27080+ break;
27081+ case AuBrPerm_RW:
076b876e
AM
27082+ bad = val & AuBrRAttr_Mask;
27083+ val &= ~AuBrRAttr_Mask;
1e00d052
AM
27084+ break;
27085+ }
c1595e42
JR
27086+
27087+ /*
27088+ * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
27089+ * does not treat it as an error, just warning.
27090+ * this is a tiny guard for the user operation.
27091+ */
27092+ if (val & AuBrAttr_UNPIN) {
27093+ bad |= AuBrAttr_UNPIN;
27094+ val &= ~AuBrAttr_UNPIN;
27095+ }
27096+
076b876e
AM
27097+ if (unlikely(bad)) {
27098+ sz = au_do_optstr_br_attr(&attr, bad);
27099+ AuDebugOn(!sz);
27100+ pr_warn("ignored branch attribute %s\n", attr.a);
27101+ }
1e00d052
AM
27102+
27103+out:
1facf9fc 27104+ return val;
27105+}
27106+
076b876e 27107+void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
1facf9fc 27108+{
076b876e
AM
27109+ au_br_perm_str_t attr;
27110+ const char *p;
27111+ char *q;
1e00d052
AM
27112+ int sz;
27113+
076b876e
AM
27114+ q = str->a;
27115+ p = au_optstr(&perm, brperm);
27116+ AuDebugOn(!p || !*p);
27117+ sz = strlen(p);
27118+ memcpy(q, p, sz + 1);
27119+ q += sz;
1e00d052 27120+
076b876e
AM
27121+ sz = au_do_optstr_br_attr(&attr, perm);
27122+ if (sz) {
27123+ *q++ = '+';
27124+ memcpy(q, attr.a, sz + 1);
1e00d052
AM
27125+ }
27126+
076b876e 27127+ AuDebugOn(strlen(str->a) >= sizeof(str->a));
1facf9fc 27128+}
27129+
27130+/* ---------------------------------------------------------------------- */
27131+
27132+static match_table_t udbalevel = {
27133+ {AuOpt_UDBA_REVAL, "reval"},
27134+ {AuOpt_UDBA_NONE, "none"},
4a4d8108
AM
27135+#ifdef CONFIG_AUFS_HNOTIFY
27136+ {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
27137+#ifdef CONFIG_AUFS_HFSNOTIFY
27138+ {AuOpt_UDBA_HNOTIFY, "fsnotify"},
4a4d8108 27139+#endif
1facf9fc 27140+#endif
27141+ {-1, NULL}
27142+};
27143+
f4d37d76 27144+int au_udba_val(char *str)
1facf9fc 27145+{
27146+ substring_t args[MAX_OPT_ARGS];
27147+
7f207e10 27148+ return match_token(str, udbalevel, args);
1facf9fc 27149+}
27150+
27151+const char *au_optstr_udba(int udba)
27152+{
076b876e 27153+ return au_parser_pattern(udba, udbalevel);
1facf9fc 27154+}
27155+
27156+/* ---------------------------------------------------------------------- */
27157+
27158+static match_table_t au_wbr_create_policy = {
27159+ {AuWbrCreate_TDP, "tdp"},
27160+ {AuWbrCreate_TDP, "top-down-parent"},
27161+ {AuWbrCreate_RR, "rr"},
27162+ {AuWbrCreate_RR, "round-robin"},
27163+ {AuWbrCreate_MFS, "mfs"},
27164+ {AuWbrCreate_MFS, "most-free-space"},
27165+ {AuWbrCreate_MFSV, "mfs:%d"},
27166+ {AuWbrCreate_MFSV, "most-free-space:%d"},
27167+
f2c43d5f
AM
27168+ /* top-down regardless the parent, and then mfs */
27169+ {AuWbrCreate_TDMFS, "tdmfs:%d"},
27170+ {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
27171+
1facf9fc 27172+ {AuWbrCreate_MFSRR, "mfsrr:%d"},
27173+ {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
27174+ {AuWbrCreate_PMFS, "pmfs"},
27175+ {AuWbrCreate_PMFSV, "pmfs:%d"},
392086de
AM
27176+ {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
27177+ {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
1facf9fc 27178+
27179+ {-1, NULL}
27180+};
27181+
1facf9fc 27182+static int au_wbr_mfs_wmark(substring_t *arg, char *str,
27183+ struct au_opt_wbr_create *create)
27184+{
27185+ int err;
27186+ unsigned long long ull;
27187+
27188+ err = 0;
a2654f78 27189+ if (!match_u64(arg, &ull))
1facf9fc 27190+ create->mfsrr_watermark = ull;
27191+ else {
4a4d8108 27192+ pr_err("bad integer in %s\n", str);
1facf9fc 27193+ err = -EINVAL;
27194+ }
27195+
27196+ return err;
27197+}
27198+
27199+static int au_wbr_mfs_sec(substring_t *arg, char *str,
27200+ struct au_opt_wbr_create *create)
27201+{
27202+ int n, err;
27203+
27204+ err = 0;
027c5e7a 27205+ if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
1facf9fc 27206+ create->mfs_second = n;
27207+ else {
4a4d8108 27208+ pr_err("bad integer in %s\n", str);
1facf9fc 27209+ err = -EINVAL;
27210+ }
27211+
27212+ return err;
27213+}
27214+
f4d37d76 27215+int au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
1facf9fc 27216+{
27217+ int err, e;
27218+ substring_t args[MAX_OPT_ARGS];
27219+
27220+ err = match_token(str, au_wbr_create_policy, args);
27221+ create->wbr_create = err;
27222+ switch (err) {
27223+ case AuWbrCreate_MFSRRV:
f2c43d5f 27224+ case AuWbrCreate_TDMFSV:
392086de 27225+ case AuWbrCreate_PMFSRRV:
1facf9fc 27226+ e = au_wbr_mfs_wmark(&args[0], str, create);
27227+ if (!e)
27228+ e = au_wbr_mfs_sec(&args[1], str, create);
27229+ if (unlikely(e))
27230+ err = e;
27231+ break;
27232+ case AuWbrCreate_MFSRR:
f2c43d5f 27233+ case AuWbrCreate_TDMFS:
392086de 27234+ case AuWbrCreate_PMFSRR:
1facf9fc 27235+ e = au_wbr_mfs_wmark(&args[0], str, create);
27236+ if (unlikely(e)) {
27237+ err = e;
27238+ break;
27239+ }
42a736d3 27240+ fallthrough;
1facf9fc 27241+ case AuWbrCreate_MFS:
27242+ case AuWbrCreate_PMFS:
027c5e7a 27243+ create->mfs_second = AUFS_MFS_DEF_SEC;
1facf9fc 27244+ break;
27245+ case AuWbrCreate_MFSV:
27246+ case AuWbrCreate_PMFSV:
27247+ e = au_wbr_mfs_sec(&args[0], str, create);
27248+ if (unlikely(e))
27249+ err = e;
27250+ break;
27251+ }
27252+
27253+ return err;
27254+}
27255+
27256+const char *au_optstr_wbr_create(int wbr_create)
27257+{
076b876e 27258+ return au_parser_pattern(wbr_create, au_wbr_create_policy);
1facf9fc 27259+}
27260+
27261+static match_table_t au_wbr_copyup_policy = {
27262+ {AuWbrCopyup_TDP, "tdp"},
27263+ {AuWbrCopyup_TDP, "top-down-parent"},
27264+ {AuWbrCopyup_BUP, "bup"},
27265+ {AuWbrCopyup_BUP, "bottom-up-parent"},
27266+ {AuWbrCopyup_BU, "bu"},
27267+ {AuWbrCopyup_BU, "bottom-up"},
27268+ {-1, NULL}
27269+};
27270+
f4d37d76 27271+int au_wbr_copyup_val(char *str)
1facf9fc 27272+{
27273+ substring_t args[MAX_OPT_ARGS];
27274+
27275+ return match_token(str, au_wbr_copyup_policy, args);
27276+}
27277+
27278+const char *au_optstr_wbr_copyup(int wbr_copyup)
27279+{
076b876e 27280+ return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
1facf9fc 27281+}
27282+
27283+/* ---------------------------------------------------------------------- */
27284+
f4d37d76
AM
27285+int au_opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
27286+ aufs_bindex_t bindex)
1facf9fc 27287+{
27288+ int err;
27289+ struct au_opt_add *add = &opt->add;
27290+ char *p;
27291+
27292+ add->bindex = bindex;
1e00d052 27293+ add->perm = AuBrPerm_RO;
1facf9fc 27294+ add->pathname = opt_str;
27295+ p = strchr(opt_str, '=');
27296+ if (p) {
27297+ *p++ = 0;
27298+ if (*p)
f4d37d76 27299+ add->perm = au_br_perm_val(p);
1facf9fc 27300+ }
27301+
f4d37d76 27302+ err = vfsub_kern_path(add->pathname, AuOpt_LkupDirFlags, &add->path);
1facf9fc 27303+ if (!err) {
27304+ if (!p) {
27305+ add->perm = AuBrPerm_RO;
27306+ if (au_test_fs_rr(add->path.dentry->d_sb))
27307+ add->perm = AuBrPerm_RR;
2121bcd9 27308+ else if (!bindex && !(sb_flags & SB_RDONLY))
1facf9fc 27309+ add->perm = AuBrPerm_RW;
27310+ }
27311+ opt->type = Opt_add;
27312+ goto out;
27313+ }
4a4d8108 27314+ pr_err("lookup failed %s (%d)\n", add->pathname, err);
1facf9fc 27315+ err = -EINVAL;
27316+
4f0767ce 27317+out:
1facf9fc 27318+ return err;
27319+}
27320+
1facf9fc 27321+static int au_opt_wbr_create(struct super_block *sb,
27322+ struct au_opt_wbr_create *create)
27323+{
27324+ int err;
27325+ struct au_sbinfo *sbinfo;
27326+
dece6358
AM
27327+ SiMustWriteLock(sb);
27328+
1facf9fc 27329+ err = 1; /* handled */
27330+ sbinfo = au_sbi(sb);
27331+ if (sbinfo->si_wbr_create_ops->fin) {
27332+ err = sbinfo->si_wbr_create_ops->fin(sb);
27333+ if (!err)
27334+ err = 1;
27335+ }
27336+
27337+ sbinfo->si_wbr_create = create->wbr_create;
27338+ sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
27339+ switch (create->wbr_create) {
27340+ case AuWbrCreate_MFSRRV:
27341+ case AuWbrCreate_MFSRR:
f2c43d5f
AM
27342+ case AuWbrCreate_TDMFS:
27343+ case AuWbrCreate_TDMFSV:
392086de
AM
27344+ case AuWbrCreate_PMFSRR:
27345+ case AuWbrCreate_PMFSRRV:
1facf9fc 27346+ sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
42a736d3 27347+ fallthrough;
1facf9fc 27348+ case AuWbrCreate_MFS:
27349+ case AuWbrCreate_MFSV:
27350+ case AuWbrCreate_PMFS:
27351+ case AuWbrCreate_PMFSV:
e49829fe
JR
27352+ sbinfo->si_wbr_mfs.mfs_expire
27353+ = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
1facf9fc 27354+ break;
27355+ }
27356+
27357+ if (sbinfo->si_wbr_create_ops->init)
27358+ sbinfo->si_wbr_create_ops->init(sb); /* ignore */
27359+
27360+ return err;
27361+}
27362+
27363+/*
27364+ * returns,
27365+ * plus: processed without an error
27366+ * zero: unprocessed
27367+ */
27368+static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
27369+ struct au_opts *opts)
27370+{
27371+ int err;
27372+ struct au_sbinfo *sbinfo;
27373+
dece6358
AM
27374+ SiMustWriteLock(sb);
27375+
1facf9fc 27376+ err = 1; /* handled */
27377+ sbinfo = au_sbi(sb);
27378+ switch (opt->type) {
27379+ case Opt_udba:
27380+ sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27381+ sbinfo->si_mntflags |= opt->udba;
27382+ opts->given_udba |= opt->udba;
27383+ break;
27384+
27385+ case Opt_plink:
f4d37d76
AM
27386+ if (opt->tf)
27387+ au_opt_set(sbinfo->si_mntflags, PLINK);
27388+ else {
27389+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
27390+ au_plink_put(sb, /*verbose*/1);
27391+ au_opt_clr(sbinfo->si_mntflags, PLINK);
27392+ }
1facf9fc 27393+ break;
27394+ case Opt_list_plink:
27395+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
27396+ au_plink_list(sb);
27397+ break;
27398+
4a4d8108 27399+ case Opt_dio:
f4d37d76
AM
27400+ if (opt->tf) {
27401+ au_opt_set(sbinfo->si_mntflags, DIO);
27402+ au_fset_opts(opts->flags, REFRESH_DYAOP);
27403+ } else {
27404+ au_opt_clr(sbinfo->si_mntflags, DIO);
27405+ au_fset_opts(opts->flags, REFRESH_DYAOP);
27406+ }
4a4d8108
AM
27407+ break;
27408+
076b876e
AM
27409+ case Opt_fhsm_sec:
27410+ au_fhsm_set(sbinfo, opt->fhsm_second);
27411+ break;
27412+
1facf9fc 27413+ case Opt_diropq_a:
27414+ au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27415+ break;
27416+ case Opt_diropq_w:
27417+ au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27418+ break;
27419+
27420+ case Opt_warn_perm:
f4d37d76
AM
27421+ if (opt->tf)
27422+ au_opt_set(sbinfo->si_mntflags, WARN_PERM);
27423+ else
27424+ au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
1facf9fc 27425+ break;
27426+
1facf9fc 27427+ case Opt_verbose:
f4d37d76
AM
27428+ if (opt->tf)
27429+ au_opt_set(sbinfo->si_mntflags, VERBOSE);
27430+ else
27431+ au_opt_clr(sbinfo->si_mntflags, VERBOSE);
1facf9fc 27432+ break;
27433+
27434+ case Opt_sum:
f4d37d76
AM
27435+ if (opt->tf)
27436+ au_opt_set(sbinfo->si_mntflags, SUM);
27437+ else {
27438+ au_opt_clr(sbinfo->si_mntflags, SUM);
27439+ au_opt_clr(sbinfo->si_mntflags, SUM_W);
27440+ }
1facf9fc 27441+ break;
27442+ case Opt_wsum:
27443+ au_opt_clr(sbinfo->si_mntflags, SUM);
27444+ au_opt_set(sbinfo->si_mntflags, SUM_W);
eca34b5c 27445+ break;
1facf9fc 27446+
27447+ case Opt_wbr_create:
27448+ err = au_opt_wbr_create(sb, &opt->wbr_create);
27449+ break;
27450+ case Opt_wbr_copyup:
27451+ sbinfo->si_wbr_copyup = opt->wbr_copyup;
27452+ sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
27453+ break;
27454+
27455+ case Opt_dirwh:
27456+ sbinfo->si_dirwh = opt->dirwh;
27457+ break;
27458+
27459+ case Opt_rdcache:
e49829fe
JR
27460+ sbinfo->si_rdcache
27461+ = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
1facf9fc 27462+ break;
27463+ case Opt_rdblk:
27464+ sbinfo->si_rdblk = opt->rdblk;
27465+ break;
27466+ case Opt_rdhash:
27467+ sbinfo->si_rdhash = opt->rdhash;
27468+ break;
dece6358
AM
27469+
27470+ case Opt_shwh:
f4d37d76
AM
27471+ if (opt->tf)
27472+ au_opt_set(sbinfo->si_mntflags, SHWH);
27473+ else
27474+ au_opt_clr(sbinfo->si_mntflags, SHWH);
dece6358 27475+ break;
1facf9fc 27476+
076b876e 27477+ case Opt_dirperm1:
f4d37d76
AM
27478+ if (opt->tf)
27479+ au_opt_set(sbinfo->si_mntflags, DIRPERM1);
27480+ else
27481+ au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
076b876e
AM
27482+ break;
27483+
1facf9fc 27484+ case Opt_trunc_xino:
f4d37d76
AM
27485+ if (opt->tf)
27486+ au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
27487+ else
27488+ au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
1facf9fc 27489+ break;
27490+
27491+ case Opt_trunc_xino_path:
27492+ case Opt_itrunc_xino:
acd2b654
AM
27493+ err = au_xino_trunc(sb, opt->xino_itrunc.bindex,
27494+ /*idx_begin*/0);
1facf9fc 27495+ if (!err)
27496+ err = 1;
27497+ break;
27498+
27499+ case Opt_trunc_xib:
f4d37d76
AM
27500+ if (opt->tf)
27501+ au_fset_opts(opts->flags, TRUNC_XIB);
27502+ else
27503+ au_fclr_opts(opts->flags, TRUNC_XIB);
1facf9fc 27504+ break;
27505+
8b6a4947
AM
27506+ case Opt_dirren:
27507+ err = 1;
f4d37d76
AM
27508+ if (opt->tf) {
27509+ if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27510+ err = au_dr_opt_set(sb);
27511+ if (!err)
27512+ err = 1;
27513+ }
27514+ if (err == 1)
27515+ au_opt_set(sbinfo->si_mntflags, DIRREN);
27516+ } else {
27517+ if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27518+ err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
27519+ DR_FLUSHED));
27520+ if (!err)
27521+ err = 1;
27522+ }
27523+ if (err == 1)
27524+ au_opt_clr(sbinfo->si_mntflags, DIRREN);
8b6a4947 27525+ }
8b6a4947
AM
27526+ break;
27527+
c1595e42 27528+ case Opt_acl:
f4d37d76
AM
27529+ if (opt->tf)
27530+ sb->s_flags |= SB_POSIXACL;
27531+ else
27532+ sb->s_flags &= ~SB_POSIXACL;
c1595e42
JR
27533+ break;
27534+
1facf9fc 27535+ default:
27536+ err = 0;
27537+ break;
27538+ }
27539+
27540+ return err;
27541+}
27542+
27543+/*
27544+ * returns tri-state.
27545+ * plus: processed without an error
27546+ * zero: unprocessed
27547+ * minus: error
27548+ */
27549+static int au_opt_br(struct super_block *sb, struct au_opt *opt,
27550+ struct au_opts *opts)
27551+{
27552+ int err, do_refresh;
27553+
27554+ err = 0;
27555+ switch (opt->type) {
27556+ case Opt_append:
5afbbe0d 27557+ opt->add.bindex = au_sbbot(sb) + 1;
1facf9fc 27558+ if (opt->add.bindex < 0)
27559+ opt->add.bindex = 0;
27560+ goto add;
eca34b5c 27561+ /* Always goto add, not fallthrough */
1facf9fc 27562+ case Opt_prepend:
27563+ opt->add.bindex = 0;
42a736d3 27564+ fallthrough;
f6b6e03d 27565+ add: /* indented label */
1facf9fc 27566+ case Opt_add:
27567+ err = au_br_add(sb, &opt->add,
27568+ au_ftest_opts(opts->flags, REMOUNT));
27569+ if (!err) {
27570+ err = 1;
027c5e7a 27571+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 27572+ }
27573+ break;
27574+
27575+ case Opt_del:
27576+ case Opt_idel:
27577+ err = au_br_del(sb, &opt->del,
27578+ au_ftest_opts(opts->flags, REMOUNT));
27579+ if (!err) {
27580+ err = 1;
27581+ au_fset_opts(opts->flags, TRUNC_XIB);
027c5e7a 27582+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 27583+ }
27584+ break;
27585+
27586+ case Opt_mod:
27587+ case Opt_imod:
27588+ err = au_br_mod(sb, &opt->mod,
27589+ au_ftest_opts(opts->flags, REMOUNT),
27590+ &do_refresh);
27591+ if (!err) {
27592+ err = 1;
027c5e7a
AM
27593+ if (do_refresh)
27594+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 27595+ }
27596+ break;
27597+ }
1facf9fc 27598+ return err;
27599+}
27600+
27601+static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
27602+ struct au_opt_xino **opt_xino,
27603+ struct au_opts *opts)
27604+{
27605+ int err;
1facf9fc 27606+
27607+ err = 0;
27608+ switch (opt->type) {
27609+ case Opt_xino:
062440b3
AM
27610+ err = au_xino_set(sb, &opt->xino,
27611+ !!au_ftest_opts(opts->flags, REMOUNT));
f4d37d76
AM
27612+ if (!err)
27613+ *opt_xino = &opt->xino;
1facf9fc 27614+ break;
1facf9fc 27615+ case Opt_noxino:
27616+ au_xino_clr(sb);
1facf9fc 27617+ *opt_xino = (void *)-1;
27618+ break;
27619+ }
27620+
27621+ return err;
27622+}
27623+
27624+int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27625+ unsigned int pending)
27626+{
076b876e 27627+ int err, fhsm;
5afbbe0d 27628+ aufs_bindex_t bindex, bbot;
79b8bda9 27629+ unsigned char do_plink, skip, do_free, can_no_dreval;
1facf9fc 27630+ struct au_branch *br;
27631+ struct au_wbr *wbr;
79b8bda9 27632+ struct dentry *root, *dentry;
1facf9fc 27633+ struct inode *dir, *h_dir;
27634+ struct au_sbinfo *sbinfo;
27635+ struct au_hinode *hdir;
27636+
dece6358
AM
27637+ SiMustAnyLock(sb);
27638+
1facf9fc 27639+ sbinfo = au_sbi(sb);
27640+ AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
27641+
2121bcd9 27642+ if (!(sb_flags & SB_RDONLY)) {
dece6358 27643+ if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
0c3ec466 27644+ pr_warn("first branch should be rw\n");
dece6358 27645+ if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
febd17d6 27646+ pr_warn_once("shwh should be used with ro\n");
dece6358 27647+ }
1facf9fc 27648+
4a4d8108 27649+ if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
1facf9fc 27650+ && !au_opt_test(sbinfo->si_mntflags, XINO))
febd17d6 27651+ pr_warn_once("udba=*notify requires xino\n");
1facf9fc 27652+
076b876e 27653+ if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
febd17d6
JR
27654+ pr_warn_once("dirperm1 breaks the protection"
27655+ " by the permission bits on the lower branch\n");
076b876e 27656+
1facf9fc 27657+ err = 0;
076b876e 27658+ fhsm = 0;
1facf9fc 27659+ root = sb->s_root;
5527c038 27660+ dir = d_inode(root);
1facf9fc 27661+ do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
79b8bda9
AM
27662+ can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
27663+ UDBA_NONE);
5afbbe0d
AM
27664+ bbot = au_sbbot(sb);
27665+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
1facf9fc 27666+ skip = 0;
27667+ h_dir = au_h_iptr(dir, bindex);
27668+ br = au_sbr(sb, bindex);
1facf9fc 27669+
c1595e42
JR
27670+ if ((br->br_perm & AuBrAttr_ICEX)
27671+ && !h_dir->i_op->listxattr)
27672+ br->br_perm &= ~AuBrAttr_ICEX;
43982f53 27673+#if 0 /* untested */
c1595e42 27674+ if ((br->br_perm & AuBrAttr_ICEX_SEC)
2121bcd9 27675+ && (au_br_sb(br)->s_flags & SB_NOSEC))
c1595e42
JR
27676+ br->br_perm &= ~AuBrAttr_ICEX_SEC;
27677+#endif
27678+
27679+ do_free = 0;
1facf9fc 27680+ wbr = br->br_wbr;
27681+ if (wbr)
27682+ wbr_wh_read_lock(wbr);
27683+
1e00d052 27684+ if (!au_br_writable(br->br_perm)) {
1facf9fc 27685+ do_free = !!wbr;
27686+ skip = (!wbr
27687+ || (!wbr->wbr_whbase
27688+ && !wbr->wbr_plink
27689+ && !wbr->wbr_orph));
1e00d052 27690+ } else if (!au_br_wh_linkable(br->br_perm)) {
1facf9fc 27691+ /* skip = (!br->br_whbase && !br->br_orph); */
27692+ skip = (!wbr || !wbr->wbr_whbase);
27693+ if (skip && wbr) {
27694+ if (do_plink)
27695+ skip = !!wbr->wbr_plink;
27696+ else
27697+ skip = !wbr->wbr_plink;
27698+ }
1e00d052 27699+ } else {
1facf9fc 27700+ /* skip = (br->br_whbase && br->br_ohph); */
27701+ skip = (wbr && wbr->wbr_whbase);
27702+ if (skip) {
27703+ if (do_plink)
27704+ skip = !!wbr->wbr_plink;
27705+ else
27706+ skip = !wbr->wbr_plink;
27707+ }
1facf9fc 27708+ }
27709+ if (wbr)
27710+ wbr_wh_read_unlock(wbr);
27711+
79b8bda9
AM
27712+ if (can_no_dreval) {
27713+ dentry = br->br_path.dentry;
27714+ spin_lock(&dentry->d_lock);
27715+ if (dentry->d_flags &
27716+ (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
27717+ can_no_dreval = 0;
27718+ spin_unlock(&dentry->d_lock);
27719+ }
27720+
076b876e
AM
27721+ if (au_br_fhsm(br->br_perm)) {
27722+ fhsm++;
27723+ AuDebugOn(!br->br_fhsm);
27724+ }
27725+
1facf9fc 27726+ if (skip)
27727+ continue;
27728+
27729+ hdir = au_hi(dir, bindex);
5afbbe0d 27730+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 27731+ if (wbr)
27732+ wbr_wh_write_lock(wbr);
86dc4139 27733+ err = au_wh_init(br, sb);
1facf9fc 27734+ if (wbr)
27735+ wbr_wh_write_unlock(wbr);
5afbbe0d 27736+ au_hn_inode_unlock(hdir);
1facf9fc 27737+
27738+ if (!err && do_free) {
9f237c51 27739+ au_kfree_rcu(wbr);
1facf9fc 27740+ br->br_wbr = NULL;
27741+ }
27742+ }
27743+
79b8bda9
AM
27744+ if (can_no_dreval)
27745+ au_fset_si(sbinfo, NO_DREVAL);
27746+ else
27747+ au_fclr_si(sbinfo, NO_DREVAL);
27748+
c1595e42 27749+ if (fhsm >= 2) {
076b876e 27750+ au_fset_si(sbinfo, FHSM);
5afbbe0d 27751+ for (bindex = bbot; bindex >= 0; bindex--) {
c1595e42
JR
27752+ br = au_sbr(sb, bindex);
27753+ if (au_br_fhsm(br->br_perm)) {
27754+ au_fhsm_set_bottom(sb, bindex);
27755+ break;
27756+ }
27757+ }
27758+ } else {
076b876e 27759+ au_fclr_si(sbinfo, FHSM);
c1595e42
JR
27760+ au_fhsm_set_bottom(sb, -1);
27761+ }
076b876e 27762+
1facf9fc 27763+ return err;
27764+}
27765+
27766+int au_opts_mount(struct super_block *sb, struct au_opts *opts)
27767+{
27768+ int err;
27769+ unsigned int tmp;
5afbbe0d 27770+ aufs_bindex_t bindex, bbot;
1facf9fc 27771+ struct au_opt *opt;
27772+ struct au_opt_xino *opt_xino, xino;
27773+ struct au_sbinfo *sbinfo;
027c5e7a 27774+ struct au_branch *br;
076b876e 27775+ struct inode *dir;
1facf9fc 27776+
dece6358
AM
27777+ SiMustWriteLock(sb);
27778+
1facf9fc 27779+ err = 0;
27780+ opt_xino = NULL;
27781+ opt = opts->opt;
27782+ while (err >= 0 && opt->type != Opt_tail)
27783+ err = au_opt_simple(sb, opt++, opts);
27784+ if (err > 0)
27785+ err = 0;
27786+ else if (unlikely(err < 0))
27787+ goto out;
27788+
27789+ /* disable xino and udba temporary */
27790+ sbinfo = au_sbi(sb);
27791+ tmp = sbinfo->si_mntflags;
27792+ au_opt_clr(sbinfo->si_mntflags, XINO);
27793+ au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
27794+
27795+ opt = opts->opt;
27796+ while (err >= 0 && opt->type != Opt_tail)
27797+ err = au_opt_br(sb, opt++, opts);
27798+ if (err > 0)
27799+ err = 0;
27800+ else if (unlikely(err < 0))
27801+ goto out;
27802+
5afbbe0d
AM
27803+ bbot = au_sbbot(sb);
27804+ if (unlikely(bbot < 0)) {
1facf9fc 27805+ err = -EINVAL;
4a4d8108 27806+ pr_err("no branches\n");
1facf9fc 27807+ goto out;
27808+ }
27809+
27810+ if (au_opt_test(tmp, XINO))
27811+ au_opt_set(sbinfo->si_mntflags, XINO);
27812+ opt = opts->opt;
27813+ while (!err && opt->type != Opt_tail)
27814+ err = au_opt_xino(sb, opt++, &opt_xino, opts);
27815+ if (unlikely(err))
27816+ goto out;
27817+
27818+ err = au_opts_verify(sb, sb->s_flags, tmp);
27819+ if (unlikely(err))
27820+ goto out;
27821+
27822+ /* restore xino */
27823+ if (au_opt_test(tmp, XINO) && !opt_xino) {
27824+ xino.file = au_xino_def(sb);
27825+ err = PTR_ERR(xino.file);
27826+ if (IS_ERR(xino.file))
27827+ goto out;
27828+
27829+ err = au_xino_set(sb, &xino, /*remount*/0);
27830+ fput(xino.file);
27831+ if (unlikely(err))
27832+ goto out;
27833+ }
27834+
27835+ /* restore udba */
027c5e7a 27836+ tmp &= AuOptMask_UDBA;
1facf9fc 27837+ sbinfo->si_mntflags &= ~AuOptMask_UDBA;
027c5e7a 27838+ sbinfo->si_mntflags |= tmp;
5afbbe0d
AM
27839+ bbot = au_sbbot(sb);
27840+ for (bindex = 0; bindex <= bbot; bindex++) {
027c5e7a
AM
27841+ br = au_sbr(sb, bindex);
27842+ err = au_hnotify_reset_br(tmp, br, br->br_perm);
27843+ if (unlikely(err))
27844+ AuIOErr("hnotify failed on br %d, %d, ignored\n",
27845+ bindex, err);
27846+ /* go on even if err */
27847+ }
4a4d8108 27848+ if (au_opt_test(tmp, UDBA_HNOTIFY)) {
5527c038 27849+ dir = d_inode(sb->s_root);
4a4d8108 27850+ au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
1facf9fc 27851+ }
27852+
4f0767ce 27853+out:
1facf9fc 27854+ return err;
27855+}
27856+
27857+int au_opts_remount(struct super_block *sb, struct au_opts *opts)
27858+{
27859+ int err, rerr;
79b8bda9 27860+ unsigned char no_dreval;
1facf9fc 27861+ struct inode *dir;
27862+ struct au_opt_xino *opt_xino;
27863+ struct au_opt *opt;
27864+ struct au_sbinfo *sbinfo;
27865+
dece6358
AM
27866+ SiMustWriteLock(sb);
27867+
8b6a4947
AM
27868+ err = au_dr_opt_flush(sb);
27869+ if (unlikely(err))
27870+ goto out;
27871+ au_fset_opts(opts->flags, DR_FLUSHED);
27872+
5527c038 27873+ dir = d_inode(sb->s_root);
1facf9fc 27874+ sbinfo = au_sbi(sb);
1facf9fc 27875+ opt_xino = NULL;
27876+ opt = opts->opt;
27877+ while (err >= 0 && opt->type != Opt_tail) {
27878+ err = au_opt_simple(sb, opt, opts);
27879+ if (!err)
27880+ err = au_opt_br(sb, opt, opts);
27881+ if (!err)
27882+ err = au_opt_xino(sb, opt, &opt_xino, opts);
27883+ opt++;
27884+ }
27885+ if (err > 0)
27886+ err = 0;
27887+ AuTraceErr(err);
27888+ /* go on even err */
27889+
79b8bda9 27890+ no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
1facf9fc 27891+ rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
27892+ if (unlikely(rerr && !err))
27893+ err = rerr;
27894+
79b8bda9 27895+ if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
b95c5147 27896+ au_fset_opts(opts->flags, REFRESH_IDOP);
79b8bda9 27897+
1facf9fc 27898+ if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
27899+ rerr = au_xib_trunc(sb);
27900+ if (unlikely(rerr && !err))
27901+ err = rerr;
27902+ }
27903+
27904+ /* will be handled by the caller */
027c5e7a 27905+ if (!au_ftest_opts(opts->flags, REFRESH)
79b8bda9
AM
27906+ && (opts->given_udba
27907+ || au_opt_test(sbinfo->si_mntflags, XINO)
b95c5147 27908+ || au_ftest_opts(opts->flags, REFRESH_IDOP)
79b8bda9 27909+ ))
027c5e7a 27910+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 27911+
27912+ AuDbg("status 0x%x\n", opts->flags);
8b6a4947
AM
27913+
27914+out:
1facf9fc 27915+ return err;
27916+}
27917+
27918+/* ---------------------------------------------------------------------- */
27919+
27920+unsigned int au_opt_udba(struct super_block *sb)
27921+{
27922+ return au_mntflags(sb) & AuOptMask_UDBA;
27923+}
7f207e10 27924diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
eca34b5c 27925--- /usr/share/empty/fs/aufs/opts.h 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 27926+++ linux/fs/aufs/opts.h 2022-11-05 23:02:18.969222617 +0100
f4d37d76 27927@@ -0,0 +1,263 @@
062440b3 27928+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 27929+/*
0dcfbb52 27930+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 27931+ *
0dcfbb52 27932+ * This program is free software; you can redistribute it and/or modify
1facf9fc 27933+ * it under the terms of the GNU General Public License as published by
27934+ * the Free Software Foundation; either version 2 of the License, or
27935+ * (at your option) any later version.
dece6358
AM
27936+ *
27937+ * This program is distributed in the hope that it will be useful,
27938+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
27939+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27940+ * GNU General Public License for more details.
27941+ *
27942+ * You should have received a copy of the GNU General Public License
523b37e3 27943+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 27944+ */
27945+
27946+/*
27947+ * mount options/flags
27948+ */
27949+
27950+#ifndef __AUFS_OPTS_H__
27951+#define __AUFS_OPTS_H__
27952+
27953+#ifdef __KERNEL__
27954+
f4d37d76
AM
27955+#include <linux/fs_parser.h>
27956+#include <linux/namei.h>
dece6358 27957+#include <linux/path.h>
1facf9fc 27958+
f4d37d76
AM
27959+enum {
27960+ Opt_br,
27961+ Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
27962+ Opt_idel, Opt_imod,
27963+ Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
27964+ Opt_xino, Opt_noxino,
27965+ Opt_trunc_xino, Opt_trunc_xino_v,
27966+ Opt_trunc_xino_path, Opt_itrunc_xino,
27967+ Opt_trunc_xib,
27968+ Opt_shwh,
27969+ Opt_plink, Opt_list_plink,
27970+ Opt_udba,
27971+ Opt_dio,
27972+ Opt_diropq, Opt_diropq_a, Opt_diropq_w,
27973+ Opt_warn_perm,
27974+ Opt_wbr_copyup, Opt_wbr_create,
27975+ Opt_fhsm_sec,
27976+ Opt_verbose, Opt_noverbose,
27977+ Opt_sum, Opt_wsum,
27978+ Opt_dirperm1,
27979+ Opt_dirren,
27980+ Opt_acl,
27981+ Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
27982+};
dece6358 27983+
1facf9fc 27984+/* ---------------------------------------------------------------------- */
27985+
27986+/* mount flags */
27987+#define AuOpt_XINO 1 /* external inode number bitmap
27988+ and translation table */
27989+#define AuOpt_TRUNC_XINO (1 << 1) /* truncate xino files */
27990+#define AuOpt_UDBA_NONE (1 << 2) /* users direct branch access */
27991+#define AuOpt_UDBA_REVAL (1 << 3)
4a4d8108 27992+#define AuOpt_UDBA_HNOTIFY (1 << 4)
dece6358
AM
27993+#define AuOpt_SHWH (1 << 5) /* show whiteout */
27994+#define AuOpt_PLINK (1 << 6) /* pseudo-link */
076b876e
AM
27995+#define AuOpt_DIRPERM1 (1 << 7) /* ignore the lower dir's perm
27996+ bits */
dece6358
AM
27997+#define AuOpt_ALWAYS_DIROPQ (1 << 9) /* policy to creating diropq */
27998+#define AuOpt_SUM (1 << 10) /* summation for statfs(2) */
27999+#define AuOpt_SUM_W (1 << 11) /* unimplemented */
28000+#define AuOpt_WARN_PERM (1 << 12) /* warn when add-branch */
eca801bf 28001+#define AuOpt_VERBOSE (1 << 13) /* print the cause of error */
4a4d8108 28002+#define AuOpt_DIO (1 << 14) /* direct io */
8b6a4947 28003+#define AuOpt_DIRREN (1 << 15) /* directory rename */
1facf9fc 28004+
4a4d8108
AM
28005+#ifndef CONFIG_AUFS_HNOTIFY
28006+#undef AuOpt_UDBA_HNOTIFY
28007+#define AuOpt_UDBA_HNOTIFY 0
1facf9fc 28008+#endif
8b6a4947
AM
28009+#ifndef CONFIG_AUFS_DIRREN
28010+#undef AuOpt_DIRREN
28011+#define AuOpt_DIRREN 0
28012+#endif
dece6358
AM
28013+#ifndef CONFIG_AUFS_SHWH
28014+#undef AuOpt_SHWH
28015+#define AuOpt_SHWH 0
28016+#endif
1facf9fc 28017+
28018+#define AuOpt_Def (AuOpt_XINO \
28019+ | AuOpt_UDBA_REVAL \
28020+ | AuOpt_PLINK \
28021+ /* | AuOpt_DIRPERM1 */ \
28022+ | AuOpt_WARN_PERM)
28023+#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
28024+ | AuOpt_UDBA_REVAL \
4a4d8108 28025+ | AuOpt_UDBA_HNOTIFY)
1facf9fc 28026+
f4d37d76
AM
28027+#define AuOpt_LkupDirFlags (LOOKUP_FOLLOW | LOOKUP_DIRECTORY)
28028+
1facf9fc 28029+#define au_opt_test(flags, name) (flags & AuOpt_##name)
28030+#define au_opt_set(flags, name) do { \
28031+ BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
28032+ ((flags) |= AuOpt_##name); \
28033+} while (0)
28034+#define au_opt_set_udba(flags, name) do { \
28035+ (flags) &= ~AuOptMask_UDBA; \
28036+ ((flags) |= AuOpt_##name); \
28037+} while (0)
7f207e10
AM
28038+#define au_opt_clr(flags, name) do { \
28039+ ((flags) &= ~AuOpt_##name); \
28040+} while (0)
1facf9fc 28041+
e49829fe
JR
28042+static inline unsigned int au_opts_plink(unsigned int mntflags)
28043+{
28044+#ifdef CONFIG_PROC_FS
28045+ return mntflags;
28046+#else
28047+ return mntflags & ~AuOpt_PLINK;
28048+#endif
28049+}
28050+
1facf9fc 28051+/* ---------------------------------------------------------------------- */
28052+
28053+/* policies to select one among multiple writable branches */
28054+enum {
28055+ AuWbrCreate_TDP, /* top down parent */
28056+ AuWbrCreate_RR, /* round robin */
28057+ AuWbrCreate_MFS, /* most free space */
28058+ AuWbrCreate_MFSV, /* mfs with seconds */
28059+ AuWbrCreate_MFSRR, /* mfs then rr */
28060+ AuWbrCreate_MFSRRV, /* mfs then rr with seconds */
f2c43d5f
AM
28061+ AuWbrCreate_TDMFS, /* top down regardless parent and mfs */
28062+ AuWbrCreate_TDMFSV, /* top down regardless parent and mfs */
1facf9fc 28063+ AuWbrCreate_PMFS, /* parent and mfs */
28064+ AuWbrCreate_PMFSV, /* parent and mfs with seconds */
392086de
AM
28065+ AuWbrCreate_PMFSRR, /* parent, mfs and round-robin */
28066+ AuWbrCreate_PMFSRRV, /* plus seconds */
1facf9fc 28067+
28068+ AuWbrCreate_Def = AuWbrCreate_TDP
28069+};
28070+
28071+enum {
28072+ AuWbrCopyup_TDP, /* top down parent */
28073+ AuWbrCopyup_BUP, /* bottom up parent */
28074+ AuWbrCopyup_BU, /* bottom up */
28075+
28076+ AuWbrCopyup_Def = AuWbrCopyup_TDP
28077+};
28078+
28079+/* ---------------------------------------------------------------------- */
28080+
f4d37d76
AM
28081+struct file;
28082+
1facf9fc 28083+struct au_opt_add {
28084+ aufs_bindex_t bindex;
28085+ char *pathname;
28086+ int perm;
28087+ struct path path;
28088+};
28089+
28090+struct au_opt_del {
28091+ char *pathname;
28092+ struct path h_path;
28093+};
28094+
28095+struct au_opt_mod {
28096+ char *path;
28097+ int perm;
28098+ struct dentry *h_root;
28099+};
28100+
28101+struct au_opt_xino {
28102+ char *path;
28103+ struct file *file;
28104+};
28105+
28106+struct au_opt_xino_itrunc {
28107+ aufs_bindex_t bindex;
28108+};
28109+
28110+struct au_opt_wbr_create {
28111+ int wbr_create;
28112+ int mfs_second;
28113+ unsigned long long mfsrr_watermark;
28114+};
28115+
28116+struct au_opt {
28117+ int type;
28118+ union {
28119+ struct au_opt_xino xino;
28120+ struct au_opt_xino_itrunc xino_itrunc;
28121+ struct au_opt_add add;
28122+ struct au_opt_del del;
28123+ struct au_opt_mod mod;
28124+ int dirwh;
28125+ int rdcache;
28126+ unsigned int rdblk;
28127+ unsigned int rdhash;
28128+ int udba;
28129+ struct au_opt_wbr_create wbr_create;
28130+ int wbr_copyup;
076b876e 28131+ unsigned int fhsm_second;
f4d37d76 28132+ bool tf; /* generic flag, true or false */
1facf9fc 28133+ };
28134+};
28135+
28136+/* opts flags */
28137+#define AuOpts_REMOUNT 1
027c5e7a
AM
28138+#define AuOpts_REFRESH (1 << 1)
28139+#define AuOpts_TRUNC_XIB (1 << 2)
28140+#define AuOpts_REFRESH_DYAOP (1 << 3)
b95c5147 28141+#define AuOpts_REFRESH_IDOP (1 << 4)
8b6a4947 28142+#define AuOpts_DR_FLUSHED (1 << 5)
1facf9fc 28143+#define au_ftest_opts(flags, name) ((flags) & AuOpts_##name)
7f207e10
AM
28144+#define au_fset_opts(flags, name) \
28145+ do { (flags) |= AuOpts_##name; } while (0)
28146+#define au_fclr_opts(flags, name) \
28147+ do { (flags) &= ~AuOpts_##name; } while (0)
1facf9fc 28148+
8b6a4947
AM
28149+#ifndef CONFIG_AUFS_DIRREN
28150+#undef AuOpts_DR_FLUSHED
28151+#define AuOpts_DR_FLUSHED 0
28152+#endif
28153+
1facf9fc 28154+struct au_opts {
28155+ struct au_opt *opt;
28156+ int max_opt;
28157+
28158+ unsigned int given_udba;
28159+ unsigned int flags;
28160+ unsigned long sb_flags;
28161+};
28162+
28163+/* ---------------------------------------------------------------------- */
28164+
7e9cd9fe 28165+/* opts.c */
f4d37d76 28166+int au_br_perm_val(char *perm);
076b876e 28167+void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
f4d37d76 28168+int au_udba_val(char *str);
1facf9fc 28169+const char *au_optstr_udba(int udba);
f4d37d76 28170+int au_wbr_create_val(char *str, struct au_opt_wbr_create *create);
1facf9fc 28171+const char *au_optstr_wbr_create(int wbr_create);
f4d37d76
AM
28172+int au_wbr_copyup_val(char *str);
28173+const char *au_optstr_wbr_copyup(int wbr_copyup);
1facf9fc 28174+
f4d37d76
AM
28175+int au_opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
28176+ aufs_bindex_t bindex);
3c1bdaff 28177+struct super_block;
1facf9fc 28178+int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
28179+ unsigned int pending);
28180+int au_opts_mount(struct super_block *sb, struct au_opts *opts);
28181+int au_opts_remount(struct super_block *sb, struct au_opts *opts);
28182+
28183+unsigned int au_opt_udba(struct super_block *sb);
28184+
f4d37d76
AM
28185+/* fsctx.c */
28186+int aufs_fsctx_init(struct fs_context *fc);
28187+extern const struct fs_parameter_spec aufs_fsctx_paramspec[];
28188+
1facf9fc 28189+#endif /* __KERNEL__ */
28190+#endif /* __AUFS_OPTS_H__ */
7f207e10 28191diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
eca34b5c 28192--- /usr/share/empty/fs/aufs/plink.c 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 28193+++ linux/fs/aufs/plink.c 2022-11-05 23:02:18.969222617 +0100
062440b3 28194@@ -0,0 +1,516 @@
cd7a4cd9 28195+// SPDX-License-Identifier: GPL-2.0
1facf9fc 28196+/*
0dcfbb52 28197+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 28198+ *
0dcfbb52 28199+ * This program is free software; you can redistribute it and/or modify
1facf9fc 28200+ * it under the terms of the GNU General Public License as published by
28201+ * the Free Software Foundation; either version 2 of the License, or
28202+ * (at your option) any later version.
dece6358
AM
28203+ *
28204+ * This program is distributed in the hope that it will be useful,
28205+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28206+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28207+ * GNU General Public License for more details.
28208+ *
28209+ * You should have received a copy of the GNU General Public License
523b37e3 28210+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 28211+ */
28212+
28213+/*
28214+ * pseudo-link
28215+ */
28216+
28217+#include "aufs.h"
28218+
28219+/*
e49829fe 28220+ * the pseudo-link maintenance mode.
1facf9fc 28221+ * during a user process maintains the pseudo-links,
28222+ * prohibit adding a new plink and branch manipulation.
e49829fe
JR
28223+ *
28224+ * Flags
28225+ * NOPLM:
28226+ * For entry functions which will handle plink, and i_mutex is already held
28227+ * in VFS.
28228+ * They cannot wait and should return an error at once.
28229+ * Callers has to check the error.
28230+ * NOPLMW:
28231+ * For entry functions which will handle plink, but i_mutex is not held
28232+ * in VFS.
28233+ * They can wait the plink maintenance mode to finish.
28234+ *
28235+ * They behave like F_SETLK and F_SETLKW.
28236+ * If the caller never handle plink, then both flags are unnecessary.
1facf9fc 28237+ */
e49829fe
JR
28238+
28239+int au_plink_maint(struct super_block *sb, int flags)
1facf9fc 28240+{
e49829fe
JR
28241+ int err;
28242+ pid_t pid, ppid;
f0c0a007 28243+ struct task_struct *parent, *prev;
e49829fe 28244+ struct au_sbinfo *sbi;
dece6358
AM
28245+
28246+ SiMustAnyLock(sb);
28247+
e49829fe
JR
28248+ err = 0;
28249+ if (!au_opt_test(au_mntflags(sb), PLINK))
28250+ goto out;
28251+
28252+ sbi = au_sbi(sb);
28253+ pid = sbi->si_plink_maint_pid;
28254+ if (!pid || pid == current->pid)
28255+ goto out;
28256+
28257+ /* todo: it highly depends upon /sbin/mount.aufs */
f0c0a007
AM
28258+ prev = NULL;
28259+ parent = current;
28260+ ppid = 0;
e49829fe 28261+ rcu_read_lock();
f0c0a007
AM
28262+ while (1) {
28263+ parent = rcu_dereference(parent->real_parent);
28264+ if (parent == prev)
28265+ break;
28266+ ppid = task_pid_vnr(parent);
28267+ if (pid == ppid) {
28268+ rcu_read_unlock();
28269+ goto out;
28270+ }
28271+ prev = parent;
28272+ }
e49829fe 28273+ rcu_read_unlock();
e49829fe
JR
28274+
28275+ if (au_ftest_lock(flags, NOPLMW)) {
027c5e7a
AM
28276+ /* if there is no i_mutex lock in VFS, we don't need to wait */
28277+ /* AuDebugOn(!lockdep_depth(current)); */
e49829fe
JR
28278+ while (sbi->si_plink_maint_pid) {
28279+ si_read_unlock(sb);
28280+ /* gave up wake_up_bit() */
28281+ wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
28282+
28283+ if (au_ftest_lock(flags, FLUSH))
28284+ au_nwt_flush(&sbi->si_nowait);
28285+ si_noflush_read_lock(sb);
28286+ }
28287+ } else if (au_ftest_lock(flags, NOPLM)) {
28288+ AuDbg("ppid %d, pid %d\n", ppid, pid);
28289+ err = -EAGAIN;
28290+ }
28291+
28292+out:
28293+ return err;
4a4d8108
AM
28294+}
28295+
e49829fe 28296+void au_plink_maint_leave(struct au_sbinfo *sbinfo)
4a4d8108 28297+{
4a4d8108 28298+ spin_lock(&sbinfo->si_plink_maint_lock);
027c5e7a 28299+ sbinfo->si_plink_maint_pid = 0;
4a4d8108 28300+ spin_unlock(&sbinfo->si_plink_maint_lock);
027c5e7a 28301+ wake_up_all(&sbinfo->si_plink_wq);
4a4d8108
AM
28302+}
28303+
e49829fe 28304+int au_plink_maint_enter(struct super_block *sb)
4a4d8108
AM
28305+{
28306+ int err;
4a4d8108
AM
28307+ struct au_sbinfo *sbinfo;
28308+
28309+ err = 0;
4a4d8108
AM
28310+ sbinfo = au_sbi(sb);
28311+ /* make sure i am the only one in this fs */
e49829fe
JR
28312+ si_write_lock(sb, AuLock_FLUSH);
28313+ if (au_opt_test(au_mntflags(sb), PLINK)) {
28314+ spin_lock(&sbinfo->si_plink_maint_lock);
28315+ if (!sbinfo->si_plink_maint_pid)
28316+ sbinfo->si_plink_maint_pid = current->pid;
28317+ else
28318+ err = -EBUSY;
28319+ spin_unlock(&sbinfo->si_plink_maint_lock);
28320+ }
4a4d8108
AM
28321+ si_write_unlock(sb);
28322+
28323+ return err;
1facf9fc 28324+}
28325+
28326+/* ---------------------------------------------------------------------- */
28327+
1facf9fc 28328+#ifdef CONFIG_AUFS_DEBUG
28329+void au_plink_list(struct super_block *sb)
28330+{
86dc4139 28331+ int i;
1facf9fc 28332+ struct au_sbinfo *sbinfo;
8b6a4947
AM
28333+ struct hlist_bl_head *hbl;
28334+ struct hlist_bl_node *pos;
5afbbe0d 28335+ struct au_icntnr *icntnr;
1facf9fc 28336+
dece6358
AM
28337+ SiMustAnyLock(sb);
28338+
1facf9fc 28339+ sbinfo = au_sbi(sb);
28340+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 28341+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 28342+
86dc4139 28343+ for (i = 0; i < AuPlink_NHASH; i++) {
8b6a4947
AM
28344+ hbl = sbinfo->si_plink + i;
28345+ hlist_bl_lock(hbl);
28346+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
5afbbe0d 28347+ AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
8b6a4947 28348+ hlist_bl_unlock(hbl);
86dc4139 28349+ }
1facf9fc 28350+}
28351+#endif
28352+
28353+/* is the inode pseudo-linked? */
28354+int au_plink_test(struct inode *inode)
28355+{
86dc4139 28356+ int found, i;
1facf9fc 28357+ struct au_sbinfo *sbinfo;
8b6a4947
AM
28358+ struct hlist_bl_head *hbl;
28359+ struct hlist_bl_node *pos;
5afbbe0d 28360+ struct au_icntnr *icntnr;
1facf9fc 28361+
28362+ sbinfo = au_sbi(inode->i_sb);
dece6358 28363+ AuRwMustAnyLock(&sbinfo->si_rwsem);
1facf9fc 28364+ AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
e49829fe 28365+ AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
1facf9fc 28366+
28367+ found = 0;
86dc4139 28368+ i = au_plink_hash(inode->i_ino);
8b6a4947
AM
28369+ hbl = sbinfo->si_plink + i;
28370+ hlist_bl_lock(hbl);
28371+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
5afbbe0d 28372+ if (&icntnr->vfs_inode == inode) {
1facf9fc 28373+ found = 1;
28374+ break;
28375+ }
8b6a4947 28376+ hlist_bl_unlock(hbl);
1facf9fc 28377+ return found;
28378+}
28379+
28380+/* ---------------------------------------------------------------------- */
28381+
28382+/*
28383+ * generate a name for plink.
28384+ * the file will be stored under AUFS_WH_PLINKDIR.
28385+ */
28386+/* 20 is max digits length of ulong 64 */
28387+#define PLINK_NAME_LEN ((20 + 1) * 2)
28388+
28389+static int plink_name(char *name, int len, struct inode *inode,
28390+ aufs_bindex_t bindex)
28391+{
28392+ int rlen;
28393+ struct inode *h_inode;
28394+
28395+ h_inode = au_h_iptr(inode, bindex);
28396+ rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
28397+ return rlen;
28398+}
28399+
7f207e10
AM
28400+struct au_do_plink_lkup_args {
28401+ struct dentry **errp;
28402+ struct qstr *tgtname;
758e9dad 28403+ struct path *h_ppath;
7f207e10
AM
28404+};
28405+
28406+static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
758e9dad 28407+ struct path *h_ppath)
7f207e10
AM
28408+{
28409+ struct dentry *h_dentry;
febd17d6 28410+ struct inode *h_inode;
7f207e10 28411+
758e9dad 28412+ h_inode = d_inode(h_ppath->dentry);
be118d29 28413+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD2);
758e9dad 28414+ h_dentry = vfsub_lkup_one(tgtname, h_ppath);
3c1bdaff 28415+ inode_unlock_shared(h_inode);
758e9dad 28416+
7f207e10
AM
28417+ return h_dentry;
28418+}
28419+
28420+static void au_call_do_plink_lkup(void *args)
28421+{
28422+ struct au_do_plink_lkup_args *a = args;
758e9dad 28423+ *a->errp = au_do_plink_lkup(a->tgtname, a->h_ppath);
7f207e10
AM
28424+}
28425+
1facf9fc 28426+/* lookup the plink-ed @inode under the branch at @bindex */
28427+struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
28428+{
758e9dad 28429+ struct dentry *h_dentry;
1facf9fc 28430+ struct au_branch *br;
758e9dad 28431+ struct path h_ppath;
7f207e10 28432+ int wkq_err;
1facf9fc 28433+ char a[PLINK_NAME_LEN];
0c3ec466 28434+ struct qstr tgtname = QSTR_INIT(a, 0);
1facf9fc 28435+
e49829fe
JR
28436+ AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
28437+
1facf9fc 28438+ br = au_sbr(inode->i_sb, bindex);
758e9dad
AM
28439+ h_ppath.dentry = br->br_wbr->wbr_plink;
28440+ h_ppath.mnt = au_br_mnt(br);
1facf9fc 28441+ tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28442+
2dfbb274 28443+ if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
7f207e10
AM
28444+ struct au_do_plink_lkup_args args = {
28445+ .errp = &h_dentry,
28446+ .tgtname = &tgtname,
758e9dad 28447+ .h_ppath = &h_ppath
7f207e10
AM
28448+ };
28449+
28450+ wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
28451+ if (unlikely(wkq_err))
28452+ h_dentry = ERR_PTR(wkq_err);
28453+ } else
758e9dad 28454+ h_dentry = au_do_plink_lkup(&tgtname, &h_ppath);
7f207e10 28455+
1facf9fc 28456+ return h_dentry;
28457+}
28458+
28459+/* create a pseudo-link */
758e9dad
AM
28460+static int do_whplink(struct qstr *tgt, struct path *h_ppath,
28461+ struct dentry *h_dentry)
1facf9fc 28462+{
28463+ int err;
758e9dad 28464+ struct path h_path;
523b37e3 28465+ struct inode *h_dir, *delegated;
1facf9fc 28466+
758e9dad 28467+ h_dir = d_inode(h_ppath->dentry);
febd17d6 28468+ inode_lock_nested(h_dir, AuLsc_I_CHILD2);
758e9dad 28469+ h_path.mnt = h_ppath->mnt;
4f0767ce 28470+again:
758e9dad 28471+ h_path.dentry = vfsub_lkup_one(tgt, h_ppath);
1facf9fc 28472+ err = PTR_ERR(h_path.dentry);
28473+ if (IS_ERR(h_path.dentry))
28474+ goto out;
28475+
28476+ err = 0;
28477+ /* wh.plink dir is not monitored */
7f207e10 28478+ /* todo: is it really safe? */
5527c038
JR
28479+ if (d_is_positive(h_path.dentry)
28480+ && d_inode(h_path.dentry) != d_inode(h_dentry)) {
523b37e3
AM
28481+ delegated = NULL;
28482+ err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
28483+ if (unlikely(err == -EWOULDBLOCK)) {
28484+ pr_warn("cannot retry for NFSv4 delegation"
28485+ " for an internal unlink\n");
28486+ iput(delegated);
28487+ }
1facf9fc 28488+ dput(h_path.dentry);
28489+ h_path.dentry = NULL;
28490+ if (!err)
28491+ goto again;
28492+ }
5527c038 28493+ if (!err && d_is_negative(h_path.dentry)) {
523b37e3
AM
28494+ delegated = NULL;
28495+ err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
28496+ if (unlikely(err == -EWOULDBLOCK)) {
28497+ pr_warn("cannot retry for NFSv4 delegation"
28498+ " for an internal link\n");
28499+ iput(delegated);
28500+ }
28501+ }
1facf9fc 28502+ dput(h_path.dentry);
28503+
4f0767ce 28504+out:
febd17d6 28505+ inode_unlock(h_dir);
1facf9fc 28506+ return err;
28507+}
28508+
28509+struct do_whplink_args {
28510+ int *errp;
28511+ struct qstr *tgt;
758e9dad 28512+ struct path *h_ppath;
1facf9fc 28513+ struct dentry *h_dentry;
1facf9fc 28514+};
28515+
28516+static void call_do_whplink(void *args)
28517+{
28518+ struct do_whplink_args *a = args;
758e9dad 28519+ *a->errp = do_whplink(a->tgt, a->h_ppath, a->h_dentry);
1facf9fc 28520+}
28521+
28522+static int whplink(struct dentry *h_dentry, struct inode *inode,
758e9dad 28523+ aufs_bindex_t bindex)
1facf9fc 28524+{
28525+ int err, wkq_err;
758e9dad 28526+ struct au_branch *br;
1facf9fc 28527+ struct au_wbr *wbr;
758e9dad 28528+ struct path h_ppath;
1facf9fc 28529+ char a[PLINK_NAME_LEN];
0c3ec466 28530+ struct qstr tgtname = QSTR_INIT(a, 0);
1facf9fc 28531+
758e9dad
AM
28532+ br = au_sbr(inode->i_sb, bindex);
28533+ wbr = br->br_wbr;
28534+ h_ppath.dentry = wbr->wbr_plink;
28535+ h_ppath.mnt = au_br_mnt(br);
1facf9fc 28536+ tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28537+
28538+ /* always superio. */
2dfbb274 28539+ if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
1facf9fc 28540+ struct do_whplink_args args = {
28541+ .errp = &err,
28542+ .tgt = &tgtname,
758e9dad
AM
28543+ .h_ppath = &h_ppath,
28544+ .h_dentry = h_dentry
1facf9fc 28545+ };
28546+ wkq_err = au_wkq_wait(call_do_whplink, &args);
28547+ if (unlikely(wkq_err))
28548+ err = wkq_err;
28549+ } else
758e9dad 28550+ err = do_whplink(&tgtname, &h_ppath, h_dentry);
1facf9fc 28551+
28552+ return err;
28553+}
28554+
1facf9fc 28555+/*
28556+ * create a new pseudo-link for @h_dentry on @bindex.
28557+ * the linked inode is held in aufs @inode.
28558+ */
28559+void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
28560+ struct dentry *h_dentry)
28561+{
28562+ struct super_block *sb;
28563+ struct au_sbinfo *sbinfo;
8b6a4947
AM
28564+ struct hlist_bl_head *hbl;
28565+ struct hlist_bl_node *pos;
5afbbe0d 28566+ struct au_icntnr *icntnr;
86dc4139 28567+ int found, err, cnt, i;
1facf9fc 28568+
28569+ sb = inode->i_sb;
28570+ sbinfo = au_sbi(sb);
28571+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 28572+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 28573+
86dc4139 28574+ found = au_plink_test(inode);
4a4d8108 28575+ if (found)
1facf9fc 28576+ return;
4a4d8108 28577+
86dc4139 28578+ i = au_plink_hash(inode->i_ino);
8b6a4947 28579+ hbl = sbinfo->si_plink + i;
5afbbe0d 28580+ au_igrab(inode);
1facf9fc 28581+
8b6a4947
AM
28582+ hlist_bl_lock(hbl);
28583+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink) {
5afbbe0d 28584+ if (&icntnr->vfs_inode == inode) {
4a4d8108
AM
28585+ found = 1;
28586+ break;
28587+ }
1facf9fc 28588+ }
5afbbe0d
AM
28589+ if (!found) {
28590+ icntnr = container_of(inode, struct au_icntnr, vfs_inode);
8b6a4947 28591+ hlist_bl_add_head(&icntnr->plink, hbl);
5afbbe0d 28592+ }
8b6a4947 28593+ hlist_bl_unlock(hbl);
4a4d8108 28594+ if (!found) {
8b6a4947 28595+ cnt = au_hbl_count(hbl);
acd2b654 28596+#define msg "unexpectedly unbalanced or too many pseudo-links"
86dc4139
AM
28597+ if (cnt > AUFS_PLINK_WARN)
28598+ AuWarn1(msg ", %d\n", cnt);
28599+#undef msg
758e9dad 28600+ err = whplink(h_dentry, inode, bindex);
5afbbe0d
AM
28601+ if (unlikely(err)) {
28602+ pr_warn("err %d, damaged pseudo link.\n", err);
8b6a4947 28603+ au_hbl_del(&icntnr->plink, hbl);
5afbbe0d 28604+ iput(&icntnr->vfs_inode);
4a4d8108 28605+ }
5afbbe0d
AM
28606+ } else
28607+ iput(&icntnr->vfs_inode);
1facf9fc 28608+}
28609+
28610+/* free all plinks */
e49829fe 28611+void au_plink_put(struct super_block *sb, int verbose)
1facf9fc 28612+{
86dc4139 28613+ int i, warned;
1facf9fc 28614+ struct au_sbinfo *sbinfo;
8b6a4947
AM
28615+ struct hlist_bl_head *hbl;
28616+ struct hlist_bl_node *pos, *tmp;
5afbbe0d 28617+ struct au_icntnr *icntnr;
1facf9fc 28618+
dece6358
AM
28619+ SiMustWriteLock(sb);
28620+
1facf9fc 28621+ sbinfo = au_sbi(sb);
28622+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 28623+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 28624+
1facf9fc 28625+ /* no spin_lock since sbinfo is write-locked */
86dc4139
AM
28626+ warned = 0;
28627+ for (i = 0; i < AuPlink_NHASH; i++) {
8b6a4947
AM
28628+ hbl = sbinfo->si_plink + i;
28629+ if (!warned && verbose && !hlist_bl_empty(hbl)) {
86dc4139
AM
28630+ pr_warn("pseudo-link is not flushed");
28631+ warned = 1;
28632+ }
8b6a4947 28633+ hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink)
5afbbe0d 28634+ iput(&icntnr->vfs_inode);
8b6a4947 28635+ INIT_HLIST_BL_HEAD(hbl);
86dc4139 28636+ }
1facf9fc 28637+}
28638+
e49829fe
JR
28639+void au_plink_clean(struct super_block *sb, int verbose)
28640+{
28641+ struct dentry *root;
28642+
28643+ root = sb->s_root;
28644+ aufs_write_lock(root);
28645+ if (au_opt_test(au_mntflags(sb), PLINK))
28646+ au_plink_put(sb, verbose);
28647+ aufs_write_unlock(root);
28648+}
28649+
86dc4139
AM
28650+static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
28651+{
28652+ int do_put;
5afbbe0d 28653+ aufs_bindex_t btop, bbot, bindex;
86dc4139
AM
28654+
28655+ do_put = 0;
5afbbe0d
AM
28656+ btop = au_ibtop(inode);
28657+ bbot = au_ibbot(inode);
28658+ if (btop >= 0) {
28659+ for (bindex = btop; bindex <= bbot; bindex++) {
86dc4139
AM
28660+ if (!au_h_iptr(inode, bindex)
28661+ || au_ii_br_id(inode, bindex) != br_id)
28662+ continue;
28663+ au_set_h_iptr(inode, bindex, NULL, 0);
28664+ do_put = 1;
28665+ break;
28666+ }
28667+ if (do_put)
5afbbe0d 28668+ for (bindex = btop; bindex <= bbot; bindex++)
86dc4139
AM
28669+ if (au_h_iptr(inode, bindex)) {
28670+ do_put = 0;
28671+ break;
28672+ }
28673+ } else
28674+ do_put = 1;
28675+
28676+ return do_put;
28677+}
28678+
1facf9fc 28679+/* free the plinks on a branch specified by @br_id */
28680+void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
28681+{
28682+ struct au_sbinfo *sbinfo;
8b6a4947
AM
28683+ struct hlist_bl_head *hbl;
28684+ struct hlist_bl_node *pos, *tmp;
5afbbe0d 28685+ struct au_icntnr *icntnr;
1facf9fc 28686+ struct inode *inode;
86dc4139 28687+ int i, do_put;
1facf9fc 28688+
dece6358
AM
28689+ SiMustWriteLock(sb);
28690+
1facf9fc 28691+ sbinfo = au_sbi(sb);
28692+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 28693+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 28694+
8b6a4947 28695+ /* no bit_lock since sbinfo is write-locked */
86dc4139 28696+ for (i = 0; i < AuPlink_NHASH; i++) {
8b6a4947
AM
28697+ hbl = sbinfo->si_plink + i;
28698+ hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink) {
5afbbe0d 28699+ inode = au_igrab(&icntnr->vfs_inode);
86dc4139
AM
28700+ ii_write_lock_child(inode);
28701+ do_put = au_plink_do_half_refresh(inode, br_id);
5afbbe0d 28702+ if (do_put) {
8b6a4947 28703+ hlist_bl_del(&icntnr->plink);
5afbbe0d
AM
28704+ iput(inode);
28705+ }
86dc4139
AM
28706+ ii_write_unlock(inode);
28707+ iput(inode);
dece6358 28708+ }
dece6358
AM
28709+ }
28710+}
7f207e10 28711diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
eca34b5c 28712--- /usr/share/empty/fs/aufs/poll.c 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 28713+++ linux/fs/aufs/poll.c 2022-11-05 23:02:18.969222617 +0100
cd7a4cd9
AM
28714@@ -0,0 +1,51 @@
28715+// SPDX-License-Identifier: GPL-2.0
dece6358 28716+/*
0dcfbb52 28717+ * Copyright (C) 2005-2022 Junjiro R. Okajima
dece6358 28718+ *
0dcfbb52 28719+ * This program is free software; you can redistribute it and/or modify
dece6358
AM
28720+ * it under the terms of the GNU General Public License as published by
28721+ * the Free Software Foundation; either version 2 of the License, or
28722+ * (at your option) any later version.
28723+ *
28724+ * This program is distributed in the hope that it will be useful,
28725+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28726+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28727+ * GNU General Public License for more details.
28728+ *
28729+ * You should have received a copy of the GNU General Public License
523b37e3 28730+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358
AM
28731+ */
28732+
1308ab2a 28733+/*
28734+ * poll operation
28735+ * There is only one filesystem which implements ->poll operation, currently.
28736+ */
28737+
28738+#include "aufs.h"
28739+
cd7a4cd9 28740+__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt)
1308ab2a 28741+{
be118d29 28742+ __poll_t mask;
1308ab2a 28743+ struct file *h_file;
1308ab2a 28744+ struct super_block *sb;
28745+
28746+ /* We should pretend an error happened. */
be118d29 28747+ mask = EPOLLERR /* | EPOLLIN | EPOLLOUT */;
b912730e 28748+ sb = file->f_path.dentry->d_sb;
e49829fe 28749+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e 28750+
521ced18 28751+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
062440b3
AM
28752+ if (IS_ERR(h_file)) {
28753+ AuDbg("h_file %ld\n", PTR_ERR(h_file));
1308ab2a 28754+ goto out;
062440b3 28755+ }
1308ab2a 28756+
cd7a4cd9 28757+ mask = vfs_poll(h_file, pt);
b912730e 28758+ fput(h_file); /* instead of au_read_post() */
1308ab2a 28759+
4f0767ce 28760+out:
1308ab2a 28761+ si_read_unlock(sb);
062440b3 28762+ if (mask & EPOLLERR)
b00004a5 28763+ AuDbg("mask 0x%x\n", mask);
1308ab2a 28764+ return mask;
28765+}
c1595e42 28766diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
eca34b5c 28767--- /usr/share/empty/fs/aufs/posix_acl.c 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 28768+++ linux/fs/aufs/posix_acl.c 2022-11-05 23:02:18.969222617 +0100
758e9dad 28769@@ -0,0 +1,111 @@
cd7a4cd9 28770+// SPDX-License-Identifier: GPL-2.0
c1595e42 28771+/*
0dcfbb52 28772+ * Copyright (C) 2014-2022 Junjiro R. Okajima
c1595e42 28773+ *
0dcfbb52 28774+ * This program is free software; you can redistribute it and/or modify
c1595e42
JR
28775+ * it under the terms of the GNU General Public License as published by
28776+ * the Free Software Foundation; either version 2 of the License, or
28777+ * (at your option) any later version.
28778+ *
28779+ * This program is distributed in the hope that it will be useful,
28780+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28781+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28782+ * GNU General Public License for more details.
28783+ *
28784+ * You should have received a copy of the GNU General Public License
28785+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
28786+ */
28787+
28788+/*
28789+ * posix acl operations
28790+ */
28791+
28792+#include <linux/fs.h>
c1595e42
JR
28793+#include "aufs.h"
28794+
758e9dad 28795+struct posix_acl *aufs_get_acl(struct inode *inode, int type, bool rcu)
c1595e42
JR
28796+{
28797+ struct posix_acl *acl;
28798+ int err;
28799+ aufs_bindex_t bindex;
28800+ struct inode *h_inode;
28801+ struct super_block *sb;
28802+
758e9dad
AM
28803+ acl = ERR_PTR(-ECHILD);
28804+ if (rcu)
28805+ goto out;
28806+
c1595e42
JR
28807+ acl = NULL;
28808+ sb = inode->i_sb;
28809+ si_read_lock(sb, AuLock_FLUSH);
28810+ ii_read_lock_child(inode);
2121bcd9 28811+ if (!(sb->s_flags & SB_POSIXACL))
758e9dad 28812+ goto unlock;
c1595e42 28813+
5afbbe0d 28814+ bindex = au_ibtop(inode);
c1595e42
JR
28815+ h_inode = au_h_iptr(inode, bindex);
28816+ if (unlikely(!h_inode
28817+ || ((h_inode->i_mode & S_IFMT)
28818+ != (inode->i_mode & S_IFMT)))) {
28819+ err = au_busy_or_stale();
28820+ acl = ERR_PTR(err);
758e9dad 28821+ goto unlock;
c1595e42
JR
28822+ }
28823+
28824+ /* always topmost only */
28825+ acl = get_acl(h_inode, type);
9f00928d
AM
28826+ if (IS_ERR(acl))
28827+ forget_cached_acl(inode, type);
28828+ else
a2654f78 28829+ set_cached_acl(inode, type, acl);
c1595e42 28830+
758e9dad 28831+unlock:
c1595e42
JR
28832+ ii_read_unlock(inode);
28833+ si_read_unlock(sb);
28834+
758e9dad 28835+out:
c1595e42
JR
28836+ AuTraceErrPtr(acl);
28837+ return acl;
28838+}
28839+
0b2a12c6
JR
28840+int aufs_set_acl(struct user_namespace *userns, struct inode *inode,
28841+ struct posix_acl *acl, int type)
c1595e42
JR
28842+{
28843+ int err;
28844+ ssize_t ssz;
28845+ struct dentry *dentry;
f2c43d5f 28846+ struct au_sxattr arg = {
c1595e42
JR
28847+ .type = AU_ACL_SET,
28848+ .u.acl_set = {
28849+ .acl = acl,
28850+ .type = type
28851+ },
28852+ };
28853+
5afbbe0d
AM
28854+ IMustLock(inode);
28855+
c1595e42
JR
28856+ if (inode->i_ino == AUFS_ROOT_INO)
28857+ dentry = dget(inode->i_sb->s_root);
28858+ else {
28859+ dentry = d_find_alias(inode);
28860+ if (!dentry)
28861+ dentry = d_find_any_alias(inode);
28862+ if (!dentry) {
28863+ pr_warn("cannot handle this inode, "
28864+ "please report to aufs-users ML\n");
28865+ err = -ENOENT;
28866+ goto out;
28867+ }
28868+ }
28869+
f2c43d5f 28870+ ssz = au_sxattr(dentry, inode, &arg);
9f00928d
AM
28871+ /* forget even it if succeeds since the branch might set differently */
28872+ forget_cached_acl(inode, type);
c1595e42
JR
28873+ dput(dentry);
28874+ err = ssz;
9f00928d 28875+ if (ssz >= 0)
c1595e42
JR
28876+ err = 0;
28877+
28878+out:
c1595e42
JR
28879+ return err;
28880+}
7f207e10 28881diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
eca34b5c 28882--- /usr/share/empty/fs/aufs/procfs.c 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 28883+++ linux/fs/aufs/procfs.c 2022-11-05 23:02:18.969222617 +0100
394e211a 28884@@ -0,0 +1,170 @@
cd7a4cd9 28885+// SPDX-License-Identifier: GPL-2.0
e49829fe 28886+/*
0dcfbb52 28887+ * Copyright (C) 2010-2022 Junjiro R. Okajima
e49829fe 28888+ *
0dcfbb52 28889+ * This program is free software; you can redistribute it and/or modify
e49829fe
JR
28890+ * it under the terms of the GNU General Public License as published by
28891+ * the Free Software Foundation; either version 2 of the License, or
28892+ * (at your option) any later version.
28893+ *
28894+ * This program is distributed in the hope that it will be useful,
28895+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28896+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28897+ * GNU General Public License for more details.
28898+ *
28899+ * You should have received a copy of the GNU General Public License
523b37e3 28900+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
e49829fe
JR
28901+ */
28902+
28903+/*
28904+ * procfs interfaces
28905+ */
28906+
28907+#include <linux/proc_fs.h>
28908+#include "aufs.h"
28909+
28910+static int au_procfs_plm_release(struct inode *inode, struct file *file)
28911+{
28912+ struct au_sbinfo *sbinfo;
28913+
28914+ sbinfo = file->private_data;
28915+ if (sbinfo) {
28916+ au_plink_maint_leave(sbinfo);
28917+ kobject_put(&sbinfo->si_kobj);
28918+ }
28919+
28920+ return 0;
28921+}
28922+
28923+static void au_procfs_plm_write_clean(struct file *file)
28924+{
28925+ struct au_sbinfo *sbinfo;
28926+
28927+ sbinfo = file->private_data;
28928+ if (sbinfo)
28929+ au_plink_clean(sbinfo->si_sb, /*verbose*/0);
28930+}
28931+
28932+static int au_procfs_plm_write_si(struct file *file, unsigned long id)
28933+{
28934+ int err;
28935+ struct super_block *sb;
28936+ struct au_sbinfo *sbinfo;
8b6a4947 28937+ struct hlist_bl_node *pos;
e49829fe
JR
28938+
28939+ err = -EBUSY;
28940+ if (unlikely(file->private_data))
28941+ goto out;
28942+
28943+ sb = NULL;
53392da6 28944+ /* don't use au_sbilist_lock() here */
8b6a4947
AM
28945+ hlist_bl_lock(&au_sbilist);
28946+ hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
e49829fe 28947+ if (id == sysaufs_si_id(sbinfo)) {
83b672a5
AM
28948+ if (kobject_get_unless_zero(&sbinfo->si_kobj))
28949+ sb = sbinfo->si_sb;
e49829fe
JR
28950+ break;
28951+ }
8b6a4947 28952+ hlist_bl_unlock(&au_sbilist);
e49829fe
JR
28953+
28954+ err = -EINVAL;
28955+ if (unlikely(!sb))
28956+ goto out;
28957+
28958+ err = au_plink_maint_enter(sb);
28959+ if (!err)
28960+ /* keep kobject_get() */
28961+ file->private_data = sbinfo;
28962+ else
28963+ kobject_put(&sbinfo->si_kobj);
28964+out:
28965+ return err;
28966+}
28967+
28968+/*
28969+ * Accept a valid "si=xxxx" only.
28970+ * Once it is accepted successfully, accept "clean" too.
28971+ */
28972+static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
28973+ size_t count, loff_t *ppos)
28974+{
28975+ ssize_t err;
28976+ unsigned long id;
28977+ /* last newline is allowed */
28978+ char buf[3 + sizeof(unsigned long) * 2 + 1];
28979+
28980+ err = -EACCES;
28981+ if (unlikely(!capable(CAP_SYS_ADMIN)))
28982+ goto out;
28983+
28984+ err = -EINVAL;
28985+ if (unlikely(count > sizeof(buf)))
28986+ goto out;
28987+
28988+ err = copy_from_user(buf, ubuf, count);
28989+ if (unlikely(err)) {
28990+ err = -EFAULT;
28991+ goto out;
28992+ }
28993+ buf[count] = 0;
28994+
28995+ err = -EINVAL;
28996+ if (!strcmp("clean", buf)) {
28997+ au_procfs_plm_write_clean(file);
28998+ goto out_success;
28999+ } else if (unlikely(strncmp("si=", buf, 3)))
29000+ goto out;
29001+
9dbd164d 29002+ err = kstrtoul(buf + 3, 16, &id);
e49829fe
JR
29003+ if (unlikely(err))
29004+ goto out;
29005+
29006+ err = au_procfs_plm_write_si(file, id);
29007+ if (unlikely(err))
29008+ goto out;
29009+
29010+out_success:
29011+ err = count; /* success */
29012+out:
29013+ return err;
29014+}
29015+
394e211a
AM
29016+static const struct proc_ops au_procfs_plm_op = {
29017+ .proc_write = au_procfs_plm_write,
29018+ .proc_release = au_procfs_plm_release
e49829fe
JR
29019+};
29020+
29021+/* ---------------------------------------------------------------------- */
29022+
29023+static struct proc_dir_entry *au_procfs_dir;
29024+
29025+void au_procfs_fin(void)
29026+{
29027+ remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
29028+ remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
29029+}
29030+
29031+int __init au_procfs_init(void)
29032+{
29033+ int err;
29034+ struct proc_dir_entry *entry;
29035+
29036+ err = -ENOMEM;
29037+ au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
29038+ if (unlikely(!au_procfs_dir))
29039+ goto out;
29040+
cd7a4cd9 29041+ entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | 0200,
394e211a 29042+ au_procfs_dir, &au_procfs_plm_op);
e49829fe
JR
29043+ if (unlikely(!entry))
29044+ goto out_dir;
29045+
29046+ err = 0;
29047+ goto out; /* success */
29048+
29049+
29050+out_dir:
29051+ remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
29052+out:
29053+ return err;
29054+}
7f207e10 29055diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
eca34b5c 29056--- /usr/share/empty/fs/aufs/rdu.c 1970-01-01 01:00:00.000000000 +0100
2255d0fe 29057+++ linux/fs/aufs/rdu.c 2022-12-17 09:21:34.799855195 +0100
ba1aed25 29058@@ -0,0 +1,384 @@
cd7a4cd9 29059+// SPDX-License-Identifier: GPL-2.0
1308ab2a 29060+/*
0dcfbb52 29061+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1308ab2a 29062+ *
0dcfbb52 29063+ * This program is free software; you can redistribute it and/or modify
1308ab2a 29064+ * it under the terms of the GNU General Public License as published by
29065+ * the Free Software Foundation; either version 2 of the License, or
29066+ * (at your option) any later version.
29067+ *
29068+ * This program is distributed in the hope that it will be useful,
29069+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
29070+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29071+ * GNU General Public License for more details.
29072+ *
29073+ * You should have received a copy of the GNU General Public License
523b37e3 29074+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1308ab2a 29075+ */
29076+
29077+/*
29078+ * readdir in userspace.
29079+ */
29080+
b752ccd1 29081+#include <linux/compat.h>
4a4d8108 29082+#include <linux/fs_stack.h>
1308ab2a 29083+#include <linux/security.h>
1308ab2a 29084+#include "aufs.h"
29085+
29086+/* bits for struct aufs_rdu.flags */
29087+#define AuRdu_CALLED 1
29088+#define AuRdu_CONT (1 << 1)
29089+#define AuRdu_FULL (1 << 2)
29090+#define au_ftest_rdu(flags, name) ((flags) & AuRdu_##name)
7f207e10
AM
29091+#define au_fset_rdu(flags, name) \
29092+ do { (flags) |= AuRdu_##name; } while (0)
29093+#define au_fclr_rdu(flags, name) \
29094+ do { (flags) &= ~AuRdu_##name; } while (0)
1308ab2a 29095+
29096+struct au_rdu_arg {
392086de 29097+ struct dir_context ctx;
1308ab2a 29098+ struct aufs_rdu *rdu;
29099+ union au_rdu_ent_ul ent;
29100+ unsigned long end;
29101+
29102+ struct super_block *sb;
29103+ int err;
29104+};
29105+
2255d0fe
AM
29106+static bool au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
29107+ loff_t offset, u64 h_ino, unsigned int d_type)
1308ab2a 29108+{
29109+ int err, len;
392086de 29110+ struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
1308ab2a 29111+ struct aufs_rdu *rdu = arg->rdu;
29112+ struct au_rdu_ent ent;
29113+
29114+ err = 0;
29115+ arg->err = 0;
29116+ au_fset_rdu(rdu->cookie.flags, CALLED);
29117+ len = au_rdu_len(nlen);
29118+ if (arg->ent.ul + len < arg->end) {
29119+ ent.ino = h_ino;
29120+ ent.bindex = rdu->cookie.bindex;
29121+ ent.type = d_type;
29122+ ent.nlen = nlen;
4a4d8108
AM
29123+ if (unlikely(nlen > AUFS_MAX_NAMELEN))
29124+ ent.type = DT_UNKNOWN;
1308ab2a 29125+
9dbd164d 29126+ /* unnecessary to support mmap_sem since this is a dir */
1308ab2a 29127+ err = -EFAULT;
29128+ if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
29129+ goto out;
29130+ if (copy_to_user(arg->ent.e->name, name, nlen))
29131+ goto out;
29132+ /* the terminating NULL */
29133+ if (__put_user(0, arg->ent.e->name + nlen))
29134+ goto out;
29135+ err = 0;
29136+ /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
29137+ arg->ent.ul += len;
29138+ rdu->rent++;
29139+ } else {
29140+ err = -EFAULT;
29141+ au_fset_rdu(rdu->cookie.flags, FULL);
29142+ rdu->full = 1;
29143+ rdu->tail = arg->ent;
29144+ }
29145+
4f0767ce 29146+out:
1308ab2a 29147+ /* AuTraceErr(err); */
2255d0fe 29148+ return !err;
1308ab2a 29149+}
29150+
29151+static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
29152+{
29153+ int err;
29154+ loff_t offset;
29155+ struct au_rdu_cookie *cookie = &arg->rdu->cookie;
29156+
92d182d2 29157+ /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
1308ab2a 29158+ offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
29159+ err = offset;
29160+ if (unlikely(offset != cookie->h_pos))
29161+ goto out;
29162+
29163+ err = 0;
29164+ do {
29165+ arg->err = 0;
29166+ au_fclr_rdu(cookie->flags, CALLED);
29167+ /* smp_mb(); */
392086de 29168+ err = vfsub_iterate_dir(h_file, &arg->ctx);
1308ab2a 29169+ if (err >= 0)
29170+ err = arg->err;
29171+ } while (!err
29172+ && au_ftest_rdu(cookie->flags, CALLED)
29173+ && !au_ftest_rdu(cookie->flags, FULL));
29174+ cookie->h_pos = h_file->f_pos;
29175+
4f0767ce 29176+out:
1308ab2a 29177+ AuTraceErr(err);
29178+ return err;
29179+}
29180+
29181+static int au_rdu(struct file *file, struct aufs_rdu *rdu)
29182+{
29183+ int err;
5afbbe0d 29184+ aufs_bindex_t bbot;
392086de
AM
29185+ struct au_rdu_arg arg = {
29186+ .ctx = {
2000de60 29187+ .actor = au_rdu_fill
392086de
AM
29188+ }
29189+ };
1308ab2a 29190+ struct dentry *dentry;
29191+ struct inode *inode;
29192+ struct file *h_file;
29193+ struct au_rdu_cookie *cookie = &rdu->cookie;
29194+
ba1aed25
AM
29195+ /* VERIFY_WRITE */
29196+ err = !access_ok(rdu->ent.e, rdu->sz);
1308ab2a 29197+ if (unlikely(err)) {
29198+ err = -EFAULT;
29199+ AuTraceErr(err);
29200+ goto out;
29201+ }
29202+ rdu->rent = 0;
29203+ rdu->tail = rdu->ent;
29204+ rdu->full = 0;
29205+ arg.rdu = rdu;
29206+ arg.ent = rdu->ent;
29207+ arg.end = arg.ent.ul;
29208+ arg.end += rdu->sz;
29209+
29210+ err = -ENOTDIR;
5afbbe0d 29211+ if (unlikely(!file->f_op->iterate && !file->f_op->iterate_shared))
1308ab2a 29212+ goto out;
29213+
29214+ err = security_file_permission(file, MAY_READ);
29215+ AuTraceErr(err);
29216+ if (unlikely(err))
29217+ goto out;
29218+
2000de60 29219+ dentry = file->f_path.dentry;
5527c038 29220+ inode = d_inode(dentry);
5afbbe0d 29221+ inode_lock_shared(inode);
1308ab2a 29222+
29223+ arg.sb = inode->i_sb;
e49829fe
JR
29224+ err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
29225+ if (unlikely(err))
29226+ goto out_mtx;
027c5e7a
AM
29227+ err = au_alive_dir(dentry);
29228+ if (unlikely(err))
29229+ goto out_si;
e49829fe 29230+ /* todo: reval? */
1308ab2a 29231+ fi_read_lock(file);
29232+
29233+ err = -EAGAIN;
29234+ if (unlikely(au_ftest_rdu(cookie->flags, CONT)
29235+ && cookie->generation != au_figen(file)))
29236+ goto out_unlock;
29237+
29238+ err = 0;
29239+ if (!rdu->blk) {
29240+ rdu->blk = au_sbi(arg.sb)->si_rdblk;
29241+ if (!rdu->blk)
29242+ rdu->blk = au_dir_size(file, /*dentry*/NULL);
29243+ }
5afbbe0d
AM
29244+ bbot = au_fbtop(file);
29245+ if (cookie->bindex < bbot)
29246+ cookie->bindex = bbot;
29247+ bbot = au_fbbot_dir(file);
29248+ /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
29249+ for (; !err && cookie->bindex <= bbot;
1308ab2a 29250+ cookie->bindex++, cookie->h_pos = 0) {
4a4d8108 29251+ h_file = au_hf_dir(file, cookie->bindex);
1308ab2a 29252+ if (!h_file)
29253+ continue;
29254+
29255+ au_fclr_rdu(cookie->flags, FULL);
29256+ err = au_rdu_do(h_file, &arg);
29257+ AuTraceErr(err);
29258+ if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
29259+ break;
29260+ }
29261+ AuDbg("rent %llu\n", rdu->rent);
29262+
29263+ if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
29264+ rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
29265+ au_fset_rdu(cookie->flags, CONT);
29266+ cookie->generation = au_figen(file);
29267+ }
29268+
29269+ ii_read_lock_child(inode);
5afbbe0d 29270+ fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
1308ab2a 29271+ ii_read_unlock(inode);
29272+
4f0767ce 29273+out_unlock:
1308ab2a 29274+ fi_read_unlock(file);
027c5e7a 29275+out_si:
1308ab2a 29276+ si_read_unlock(arg.sb);
4f0767ce 29277+out_mtx:
5afbbe0d 29278+ inode_unlock_shared(inode);
4f0767ce 29279+out:
1308ab2a 29280+ AuTraceErr(err);
29281+ return err;
29282+}
29283+
29284+static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
29285+{
29286+ int err;
29287+ ino_t ino;
29288+ unsigned long long nent;
29289+ union au_rdu_ent_ul *u;
29290+ struct au_rdu_ent ent;
29291+ struct super_block *sb;
29292+
29293+ err = 0;
29294+ nent = rdu->nent;
29295+ u = &rdu->ent;
2000de60 29296+ sb = file->f_path.dentry->d_sb;
1308ab2a 29297+ si_read_lock(sb, AuLock_FLUSH);
29298+ while (nent-- > 0) {
9dbd164d 29299+ /* unnecessary to support mmap_sem since this is a dir */
1308ab2a 29300+ err = copy_from_user(&ent, u->e, sizeof(ent));
4a4d8108 29301+ if (!err)
ba1aed25
AM
29302+ /* VERIFY_WRITE */
29303+ err = !access_ok(&u->e->ino, sizeof(ino));
1308ab2a 29304+ if (unlikely(err)) {
29305+ err = -EFAULT;
29306+ AuTraceErr(err);
29307+ break;
29308+ }
29309+
29310+ /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
29311+ if (!ent.wh)
29312+ err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
29313+ else
29314+ err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
29315+ &ino);
29316+ if (unlikely(err)) {
29317+ AuTraceErr(err);
29318+ break;
29319+ }
29320+
29321+ err = __put_user(ino, &u->e->ino);
29322+ if (unlikely(err)) {
29323+ err = -EFAULT;
29324+ AuTraceErr(err);
29325+ break;
29326+ }
29327+ u->ul += au_rdu_len(ent.nlen);
29328+ }
29329+ si_read_unlock(sb);
29330+
29331+ return err;
29332+}
29333+
29334+/* ---------------------------------------------------------------------- */
29335+
29336+static int au_rdu_verify(struct aufs_rdu *rdu)
29337+{
b752ccd1 29338+ AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
1308ab2a 29339+ "%llu, b%d, 0x%x, g%u}\n",
b752ccd1 29340+ rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
1308ab2a 29341+ rdu->blk,
29342+ rdu->rent, rdu->shwh, rdu->full,
29343+ rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
29344+ rdu->cookie.generation);
dece6358 29345+
b752ccd1 29346+ if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
1308ab2a 29347+ return 0;
dece6358 29348+
b752ccd1
AM
29349+ AuDbg("%u:%u\n",
29350+ rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
1308ab2a 29351+ return -EINVAL;
29352+}
29353+
29354+long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
dece6358 29355+{
1308ab2a 29356+ long err, e;
29357+ struct aufs_rdu rdu;
29358+ void __user *p = (void __user *)arg;
dece6358 29359+
1308ab2a 29360+ err = copy_from_user(&rdu, p, sizeof(rdu));
29361+ if (unlikely(err)) {
29362+ err = -EFAULT;
29363+ AuTraceErr(err);
29364+ goto out;
29365+ }
29366+ err = au_rdu_verify(&rdu);
dece6358
AM
29367+ if (unlikely(err))
29368+ goto out;
29369+
1308ab2a 29370+ switch (cmd) {
29371+ case AUFS_CTL_RDU:
29372+ err = au_rdu(file, &rdu);
29373+ if (unlikely(err))
29374+ break;
dece6358 29375+
1308ab2a 29376+ e = copy_to_user(p, &rdu, sizeof(rdu));
29377+ if (unlikely(e)) {
29378+ err = -EFAULT;
29379+ AuTraceErr(err);
29380+ }
29381+ break;
29382+ case AUFS_CTL_RDU_INO:
29383+ err = au_rdu_ino(file, &rdu);
29384+ break;
29385+
29386+ default:
4a4d8108 29387+ /* err = -ENOTTY; */
1308ab2a 29388+ err = -EINVAL;
29389+ }
dece6358 29390+
4f0767ce 29391+out:
1308ab2a 29392+ AuTraceErr(err);
29393+ return err;
1facf9fc 29394+}
b752ccd1
AM
29395+
29396+#ifdef CONFIG_COMPAT
29397+long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
29398+{
29399+ long err, e;
29400+ struct aufs_rdu rdu;
29401+ void __user *p = compat_ptr(arg);
29402+
29403+ /* todo: get_user()? */
29404+ err = copy_from_user(&rdu, p, sizeof(rdu));
29405+ if (unlikely(err)) {
29406+ err = -EFAULT;
29407+ AuTraceErr(err);
29408+ goto out;
29409+ }
29410+ rdu.ent.e = compat_ptr(rdu.ent.ul);
29411+ err = au_rdu_verify(&rdu);
29412+ if (unlikely(err))
29413+ goto out;
29414+
29415+ switch (cmd) {
29416+ case AUFS_CTL_RDU:
29417+ err = au_rdu(file, &rdu);
29418+ if (unlikely(err))
29419+ break;
29420+
29421+ rdu.ent.ul = ptr_to_compat(rdu.ent.e);
29422+ rdu.tail.ul = ptr_to_compat(rdu.tail.e);
29423+ e = copy_to_user(p, &rdu, sizeof(rdu));
29424+ if (unlikely(e)) {
29425+ err = -EFAULT;
29426+ AuTraceErr(err);
29427+ }
29428+ break;
29429+ case AUFS_CTL_RDU_INO:
29430+ err = au_rdu_ino(file, &rdu);
29431+ break;
29432+
29433+ default:
29434+ /* err = -ENOTTY; */
29435+ err = -EINVAL;
29436+ }
29437+
4f0767ce 29438+out:
b752ccd1
AM
29439+ AuTraceErr(err);
29440+ return err;
29441+}
29442+#endif
7f207e10 29443diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
eca34b5c 29444--- /usr/share/empty/fs/aufs/rwsem.h 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 29445+++ linux/fs/aufs/rwsem.h 2022-11-05 23:02:18.969222617 +0100
42a736d3 29446@@ -0,0 +1,85 @@
062440b3 29447+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 29448+/*
0dcfbb52 29449+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 29450+ *
0dcfbb52 29451+ * This program is free software; you can redistribute it and/or modify
1facf9fc 29452+ * it under the terms of the GNU General Public License as published by
29453+ * the Free Software Foundation; either version 2 of the License, or
29454+ * (at your option) any later version.
dece6358
AM
29455+ *
29456+ * This program is distributed in the hope that it will be useful,
29457+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
29458+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29459+ * GNU General Public License for more details.
29460+ *
29461+ * You should have received a copy of the GNU General Public License
523b37e3 29462+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 29463+ */
29464+
29465+/*
29466+ * simple read-write semaphore wrappers
29467+ */
29468+
29469+#ifndef __AUFS_RWSEM_H__
29470+#define __AUFS_RWSEM_H__
29471+
29472+#ifdef __KERNEL__
29473+
4a4d8108 29474+#include "debug.h"
dece6358 29475+
acd2b654 29476+/* in the future, the name 'au_rwsem' will be totally gone */
8b6a4947 29477+#define au_rwsem rw_semaphore
dece6358
AM
29478+
29479+/* to debug easier, do not make them inlined functions */
8b6a4947 29480+#define AuRwMustNoWaiters(rw) AuDebugOn(rwsem_is_contended(rw))
42a736d3
AM
29481+
29482+#ifdef CONFIG_LOCKDEP
dece6358 29483+/* rwsem_is_locked() is unusable */
42a736d3
AM
29484+#define AuRwMustReadLock(rw) AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP) \
29485+ && !lockdep_recursing(current) \
29486+ && debug_locks \
8b6a4947 29487+ && !lockdep_is_held_type(rw, 1))
42a736d3
AM
29488+#define AuRwMustWriteLock(rw) AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP) \
29489+ && !lockdep_recursing(current) \
29490+ && debug_locks \
8b6a4947 29491+ && !lockdep_is_held_type(rw, 0))
42a736d3
AM
29492+#define AuRwMustAnyLock(rw) AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP) \
29493+ && !lockdep_recursing(current) \
29494+ && debug_locks \
8b6a4947 29495+ && !lockdep_is_held(rw))
42a736d3
AM
29496+#define AuRwDestroy(rw) AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP) \
29497+ && !lockdep_recursing(current) \
29498+ && debug_locks \
8b6a4947 29499+ && lockdep_is_held(rw))
42a736d3
AM
29500+#else
29501+#define AuRwMustReadLock(rw) do {} while (0)
29502+#define AuRwMustWriteLock(rw) do {} while (0)
29503+#define AuRwMustAnyLock(rw) do {} while (0)
29504+#define AuRwDestroy(rw) do {} while (0)
29505+#endif
8b6a4947
AM
29506+
29507+#define au_rw_init(rw) init_rwsem(rw)
dece6358 29508+
5afbbe0d
AM
29509+#define au_rw_init_wlock(rw) do { \
29510+ au_rw_init(rw); \
8b6a4947 29511+ down_write(rw); \
5afbbe0d 29512+ } while (0)
dece6358 29513+
8b6a4947
AM
29514+#define au_rw_init_wlock_nested(rw, lsc) do { \
29515+ au_rw_init(rw); \
29516+ down_write_nested(rw, lsc); \
5afbbe0d 29517+ } while (0)
dece6358 29518+
8b6a4947
AM
29519+#define au_rw_read_lock(rw) down_read(rw)
29520+#define au_rw_read_lock_nested(rw, lsc) down_read_nested(rw, lsc)
29521+#define au_rw_read_unlock(rw) up_read(rw)
29522+#define au_rw_dgrade_lock(rw) downgrade_write(rw)
29523+#define au_rw_write_lock(rw) down_write(rw)
29524+#define au_rw_write_lock_nested(rw, lsc) down_write_nested(rw, lsc)
29525+#define au_rw_write_unlock(rw) up_write(rw)
29526+/* why is not _nested version defined? */
29527+#define au_rw_read_trylock(rw) down_read_trylock(rw)
29528+#define au_rw_write_trylock(rw) down_write_trylock(rw)
1facf9fc 29529+
29530+#endif /* __KERNEL__ */
29531+#endif /* __AUFS_RWSEM_H__ */
7f207e10 29532diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
eca34b5c 29533--- /usr/share/empty/fs/aufs/sbinfo.c 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 29534+++ linux/fs/aufs/sbinfo.c 2022-11-05 23:02:18.969222617 +0100
f4d37d76 29535@@ -0,0 +1,316 @@
cd7a4cd9 29536+// SPDX-License-Identifier: GPL-2.0
1facf9fc 29537+/*
0dcfbb52 29538+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 29539+ *
0dcfbb52 29540+ * This program is free software; you can redistribute it and/or modify
1facf9fc 29541+ * it under the terms of the GNU General Public License as published by
29542+ * the Free Software Foundation; either version 2 of the License, or
29543+ * (at your option) any later version.
dece6358
AM
29544+ *
29545+ * This program is distributed in the hope that it will be useful,
29546+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
29547+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29548+ * GNU General Public License for more details.
29549+ *
29550+ * You should have received a copy of the GNU General Public License
523b37e3 29551+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 29552+ */
29553+
29554+/*
29555+ * superblock private data
29556+ */
29557+
eca801bf 29558+#include <linux/iversion.h>
1facf9fc 29559+#include "aufs.h"
29560+
29561+/*
29562+ * they are necessary regardless sysfs is disabled.
29563+ */
29564+void au_si_free(struct kobject *kobj)
29565+{
86dc4139 29566+ int i;
1facf9fc 29567+ struct au_sbinfo *sbinfo;
b752ccd1 29568+ char *locked __maybe_unused; /* debug only */
1facf9fc 29569+
29570+ sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
86dc4139 29571+ for (i = 0; i < AuPlink_NHASH; i++)
8b6a4947 29572+ AuDebugOn(!hlist_bl_empty(sbinfo->si_plink + i));
f0c0a007 29573+ AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
5afbbe0d 29574+
acd2b654
AM
29575+ AuLCntZero(au_lcnt_read(&sbinfo->si_ninodes, /*do_rev*/0));
29576+ au_lcnt_fin(&sbinfo->si_ninodes, /*do_sync*/0);
29577+ AuLCntZero(au_lcnt_read(&sbinfo->si_nfiles, /*do_rev*/0));
29578+ au_lcnt_fin(&sbinfo->si_nfiles, /*do_sync*/0);
1facf9fc 29579+
062440b3 29580+ dbgaufs_si_fin(sbinfo);
e49829fe 29581+ au_rw_write_lock(&sbinfo->si_rwsem);
1facf9fc 29582+ au_br_free(sbinfo);
e49829fe 29583+ au_rw_write_unlock(&sbinfo->si_rwsem);
b752ccd1 29584+
9f237c51 29585+ au_kfree_try_rcu(sbinfo->si_branch);
1facf9fc 29586+ mutex_destroy(&sbinfo->si_xib_mtx);
dece6358 29587+ AuRwDestroy(&sbinfo->si_rwsem);
1facf9fc 29588+
acd2b654
AM
29589+ au_lcnt_wait_for_fin(&sbinfo->si_ninodes);
29590+ /* si_nfiles is waited too */
9f237c51 29591+ au_kfree_rcu(sbinfo);
1facf9fc 29592+}
29593+
f4d37d76 29594+struct au_sbinfo *au_si_alloc(struct super_block *sb)
1facf9fc 29595+{
1facf9fc 29596+ struct au_sbinfo *sbinfo;
f4d37d76 29597+ int err, i;
1facf9fc 29598+
29599+ err = -ENOMEM;
4a4d8108 29600+ sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
1facf9fc 29601+ if (unlikely(!sbinfo))
29602+ goto out;
29603+
29604+ /* will be reallocated separately */
29605+ sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
29606+ if (unlikely(!sbinfo->si_branch))
febd17d6 29607+ goto out_sbinfo;
1facf9fc 29608+
1facf9fc 29609+ err = sysaufs_si_init(sbinfo);
062440b3
AM
29610+ if (!err) {
29611+ dbgaufs_si_null(sbinfo);
29612+ err = dbgaufs_si_init(sbinfo);
29613+ if (unlikely(err))
29614+ kobject_put(&sbinfo->si_kobj);
29615+ }
1facf9fc 29616+ if (unlikely(err))
29617+ goto out_br;
29618+
29619+ au_nwt_init(&sbinfo->si_nowait);
dece6358 29620+ au_rw_init_wlock(&sbinfo->si_rwsem);
b752ccd1 29621+
acd2b654
AM
29622+ au_lcnt_init(&sbinfo->si_ninodes, /*release*/NULL);
29623+ au_lcnt_init(&sbinfo->si_nfiles, /*release*/NULL);
7f207e10 29624+
5afbbe0d 29625+ sbinfo->si_bbot = -1;
392086de 29626+ sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
1facf9fc 29627+
29628+ sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
29629+ sbinfo->si_wbr_create = AuWbrCreate_Def;
4a4d8108
AM
29630+ sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
29631+ sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
1facf9fc 29632+
076b876e
AM
29633+ au_fhsm_init(sbinfo);
29634+
e49829fe 29635+ sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
1facf9fc 29636+
392086de
AM
29637+ sbinfo->si_xino_jiffy = jiffies;
29638+ sbinfo->si_xino_expire
29639+ = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
1facf9fc 29640+ mutex_init(&sbinfo->si_xib_mtx);
1facf9fc 29641+ /* leave si_xib_last_pindex and si_xib_next_bit */
29642+
8b6a4947 29643+ INIT_HLIST_BL_HEAD(&sbinfo->si_aopen);
b912730e 29644+
e49829fe 29645+ sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
1facf9fc 29646+ sbinfo->si_rdblk = AUFS_RDBLK_DEF;
29647+ sbinfo->si_rdhash = AUFS_RDHASH_DEF;
29648+ sbinfo->si_dirwh = AUFS_DIRWH_DEF;
29649+
86dc4139 29650+ for (i = 0; i < AuPlink_NHASH; i++)
8b6a4947 29651+ INIT_HLIST_BL_HEAD(sbinfo->si_plink + i);
1facf9fc 29652+ init_waitqueue_head(&sbinfo->si_plink_wq);
4a4d8108 29653+ spin_lock_init(&sbinfo->si_plink_maint_lock);
1facf9fc 29654+
8b6a4947 29655+ INIT_HLIST_BL_HEAD(&sbinfo->si_files);
523b37e3 29656+
b95c5147
AM
29657+ /* with getattr by default */
29658+ sbinfo->si_iop_array = aufs_iop;
29659+
1facf9fc 29660+ /* leave other members for sysaufs and si_mnt. */
29661+ sbinfo->si_sb = sb;
f4d37d76
AM
29662+ if (sb) {
29663+ sb->s_fs_info = sbinfo;
29664+ si_pid_set(sb);
29665+ }
29666+ return sbinfo; /* success */
1facf9fc 29667+
4f0767ce 29668+out_br:
9f237c51 29669+ au_kfree_try_rcu(sbinfo->si_branch);
4f0767ce 29670+out_sbinfo:
9f237c51 29671+ au_kfree_rcu(sbinfo);
4f0767ce 29672+out:
f4d37d76 29673+ return ERR_PTR(err);
1facf9fc 29674+}
29675+
e2f27e51 29676+int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
1facf9fc 29677+{
29678+ int err, sz;
29679+ struct au_branch **brp;
29680+
dece6358
AM
29681+ AuRwMustWriteLock(&sbinfo->si_rwsem);
29682+
1facf9fc 29683+ err = -ENOMEM;
5afbbe0d 29684+ sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
1facf9fc 29685+ if (unlikely(!sz))
29686+ sz = sizeof(*brp);
e2f27e51
AM
29687+ brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
29688+ may_shrink);
1facf9fc 29689+ if (brp) {
29690+ sbinfo->si_branch = brp;
29691+ err = 0;
29692+ }
29693+
29694+ return err;
29695+}
29696+
29697+/* ---------------------------------------------------------------------- */
29698+
29699+unsigned int au_sigen_inc(struct super_block *sb)
29700+{
29701+ unsigned int gen;
5527c038 29702+ struct inode *inode;
1facf9fc 29703+
dece6358
AM
29704+ SiMustWriteLock(sb);
29705+
1facf9fc 29706+ gen = ++au_sbi(sb)->si_generation;
29707+ au_update_digen(sb->s_root);
5527c038
JR
29708+ inode = d_inode(sb->s_root);
29709+ au_update_iigen(inode, /*half*/0);
be118d29 29710+ inode_inc_iversion(inode);
1facf9fc 29711+ return gen;
29712+}
29713+
29714+aufs_bindex_t au_new_br_id(struct super_block *sb)
29715+{
29716+ aufs_bindex_t br_id;
29717+ int i;
29718+ struct au_sbinfo *sbinfo;
29719+
dece6358
AM
29720+ SiMustWriteLock(sb);
29721+
1facf9fc 29722+ sbinfo = au_sbi(sb);
29723+ for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
29724+ br_id = ++sbinfo->si_last_br_id;
7f207e10 29725+ AuDebugOn(br_id < 0);
1facf9fc 29726+ if (br_id && au_br_index(sb, br_id) < 0)
29727+ return br_id;
29728+ }
29729+
29730+ return -1;
29731+}
29732+
29733+/* ---------------------------------------------------------------------- */
29734+
e49829fe
JR
29735+/* it is ok that new 'nwt' tasks are appended while we are sleeping */
29736+int si_read_lock(struct super_block *sb, int flags)
29737+{
29738+ int err;
29739+
29740+ err = 0;
29741+ if (au_ftest_lock(flags, FLUSH))
29742+ au_nwt_flush(&au_sbi(sb)->si_nowait);
29743+
29744+ si_noflush_read_lock(sb);
29745+ err = au_plink_maint(sb, flags);
29746+ if (unlikely(err))
29747+ si_read_unlock(sb);
29748+
29749+ return err;
29750+}
29751+
29752+int si_write_lock(struct super_block *sb, int flags)
29753+{
29754+ int err;
29755+
29756+ if (au_ftest_lock(flags, FLUSH))
29757+ au_nwt_flush(&au_sbi(sb)->si_nowait);
29758+
29759+ si_noflush_write_lock(sb);
29760+ err = au_plink_maint(sb, flags);
29761+ if (unlikely(err))
29762+ si_write_unlock(sb);
29763+
29764+ return err;
29765+}
29766+
1facf9fc 29767+/* dentry and super_block lock. call at entry point */
e49829fe 29768+int aufs_read_lock(struct dentry *dentry, int flags)
1facf9fc 29769+{
e49829fe 29770+ int err;
027c5e7a 29771+ struct super_block *sb;
e49829fe 29772+
027c5e7a
AM
29773+ sb = dentry->d_sb;
29774+ err = si_read_lock(sb, flags);
29775+ if (unlikely(err))
29776+ goto out;
29777+
29778+ if (au_ftest_lock(flags, DW))
29779+ di_write_lock_child(dentry);
29780+ else
29781+ di_read_lock_child(dentry, flags);
29782+
29783+ if (au_ftest_lock(flags, GEN)) {
29784+ err = au_digen_test(dentry, au_sigen(sb));
79b8bda9
AM
29785+ if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
29786+ AuDebugOn(!err && au_dbrange_test(dentry));
29787+ else if (!err)
29788+ err = au_dbrange_test(dentry);
027c5e7a
AM
29789+ if (unlikely(err))
29790+ aufs_read_unlock(dentry, flags);
e49829fe
JR
29791+ }
29792+
027c5e7a 29793+out:
e49829fe 29794+ return err;
1facf9fc 29795+}
29796+
29797+void aufs_read_unlock(struct dentry *dentry, int flags)
29798+{
29799+ if (au_ftest_lock(flags, DW))
29800+ di_write_unlock(dentry);
29801+ else
29802+ di_read_unlock(dentry, flags);
29803+ si_read_unlock(dentry->d_sb);
29804+}
29805+
29806+void aufs_write_lock(struct dentry *dentry)
29807+{
e49829fe 29808+ si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
1facf9fc 29809+ di_write_lock_child(dentry);
29810+}
29811+
29812+void aufs_write_unlock(struct dentry *dentry)
29813+{
29814+ di_write_unlock(dentry);
29815+ si_write_unlock(dentry->d_sb);
29816+}
29817+
e49829fe 29818+int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
1facf9fc 29819+{
e49829fe 29820+ int err;
027c5e7a
AM
29821+ unsigned int sigen;
29822+ struct super_block *sb;
e49829fe 29823+
027c5e7a
AM
29824+ sb = d1->d_sb;
29825+ err = si_read_lock(sb, flags);
29826+ if (unlikely(err))
29827+ goto out;
29828+
b95c5147 29829+ di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
027c5e7a
AM
29830+
29831+ if (au_ftest_lock(flags, GEN)) {
29832+ sigen = au_sigen(sb);
29833+ err = au_digen_test(d1, sigen);
29834+ AuDebugOn(!err && au_dbrange_test(d1));
29835+ if (!err) {
29836+ err = au_digen_test(d2, sigen);
29837+ AuDebugOn(!err && au_dbrange_test(d2));
29838+ }
29839+ if (unlikely(err))
29840+ aufs_read_and_write_unlock2(d1, d2);
29841+ }
29842+
29843+out:
e49829fe 29844+ return err;
1facf9fc 29845+}
29846+
29847+void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
29848+{
29849+ di_write_unlock2(d1, d2);
29850+ si_read_unlock(d1->d_sb);
29851+}
7f207e10 29852diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
eca34b5c 29853--- /usr/share/empty/fs/aufs/super.c 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 29854+++ linux/fs/aufs/super.c 2022-11-05 23:02:18.969222617 +0100
f4d37d76 29855@@ -0,0 +1,871 @@
cd7a4cd9 29856+// SPDX-License-Identifier: GPL-2.0
1facf9fc 29857+/*
0dcfbb52 29858+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 29859+ *
0dcfbb52 29860+ * This program is free software; you can redistribute it and/or modify
1facf9fc 29861+ * it under the terms of the GNU General Public License as published by
29862+ * the Free Software Foundation; either version 2 of the License, or
29863+ * (at your option) any later version.
dece6358
AM
29864+ *
29865+ * This program is distributed in the hope that it will be useful,
29866+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
29867+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29868+ * GNU General Public License for more details.
29869+ *
29870+ * You should have received a copy of the GNU General Public License
523b37e3 29871+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 29872+ */
29873+
29874+/*
29875+ * mount and super_block operations
29876+ */
29877+
eca801bf 29878+#include <linux/iversion.h>
f6c5ef8b 29879+#include <linux/mm.h>
1facf9fc 29880+#include <linux/seq_file.h>
29881+#include <linux/statfs.h>
7f207e10 29882+#include <linux/vmalloc.h>
1facf9fc 29883+#include "aufs.h"
29884+
29885+/*
29886+ * super_operations
29887+ */
29888+static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
29889+{
29890+ struct au_icntnr *c;
29891+
26eb093c 29892+ c = au_cache_alloc_icntnr(sb);
1facf9fc 29893+ if (c) {
027c5e7a 29894+ au_icntnr_init(c);
be118d29 29895+ inode_set_iversion(&c->vfs_inode, 1); /* sigen(sb); */
1facf9fc 29896+ c->iinfo.ii_hinode = NULL;
29897+ return &c->vfs_inode;
29898+ }
29899+ return NULL;
29900+}
29901+
29902+static void aufs_destroy_inode(struct inode *inode)
29903+{
5afbbe0d
AM
29904+ if (!au_is_bad_inode(inode))
29905+ au_iinfo_fin(inode);
fbc438ed
JR
29906+}
29907+
29908+static void aufs_free_inode(struct inode *inode)
29909+{
29910+ au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
1facf9fc 29911+}
29912+
29913+struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
29914+{
29915+ struct inode *inode;
29916+ int err;
29917+
29918+ inode = iget_locked(sb, ino);
29919+ if (unlikely(!inode)) {
29920+ inode = ERR_PTR(-ENOMEM);
29921+ goto out;
29922+ }
29923+ if (!(inode->i_state & I_NEW))
29924+ goto out;
29925+
29926+ err = au_xigen_new(inode);
29927+ if (!err)
29928+ err = au_iinfo_init(inode);
29929+ if (!err)
be118d29 29930+ inode_inc_iversion(inode);
1facf9fc 29931+ else {
29932+ iget_failed(inode);
29933+ inode = ERR_PTR(err);
29934+ }
29935+
4f0767ce 29936+out:
1facf9fc 29937+ /* never return NULL */
29938+ AuDebugOn(!inode);
29939+ AuTraceErrPtr(inode);
29940+ return inode;
29941+}
29942+
29943+/* lock free root dinfo */
29944+static int au_show_brs(struct seq_file *seq, struct super_block *sb)
29945+{
29946+ int err;
5afbbe0d 29947+ aufs_bindex_t bindex, bbot;
1facf9fc 29948+ struct path path;
4a4d8108 29949+ struct au_hdentry *hdp;
1facf9fc 29950+ struct au_branch *br;
076b876e 29951+ au_br_perm_str_t perm;
1facf9fc 29952+
29953+ err = 0;
5afbbe0d
AM
29954+ bbot = au_sbbot(sb);
29955+ bindex = 0;
29956+ hdp = au_hdentry(au_di(sb->s_root), bindex);
29957+ for (; !err && bindex <= bbot; bindex++, hdp++) {
1facf9fc 29958+ br = au_sbr(sb, bindex);
86dc4139 29959+ path.mnt = au_br_mnt(br);
5afbbe0d 29960+ path.dentry = hdp->hd_dentry;
1facf9fc 29961+ err = au_seq_path(seq, &path);
79b8bda9 29962+ if (!err) {
076b876e 29963+ au_optstr_br_perm(&perm, br->br_perm);
79b8bda9 29964+ seq_printf(seq, "=%s", perm.a);
5afbbe0d 29965+ if (bindex != bbot)
79b8bda9 29966+ seq_putc(seq, ':');
1e00d052 29967+ }
1facf9fc 29968+ }
79b8bda9
AM
29969+ if (unlikely(err || seq_has_overflowed(seq)))
29970+ err = -E2BIG;
1facf9fc 29971+
29972+ return err;
29973+}
29974+
f2c43d5f
AM
29975+static void au_gen_fmt(char *fmt, int len __maybe_unused, const char *pat,
29976+ const char *append)
29977+{
29978+ char *p;
29979+
29980+ p = fmt;
29981+ while (*pat != ':')
29982+ *p++ = *pat++;
29983+ *p++ = *pat++;
29984+ strcpy(p, append);
29985+ AuDebugOn(strlen(fmt) >= len);
29986+}
29987+
1facf9fc 29988+static void au_show_wbr_create(struct seq_file *m, int v,
29989+ struct au_sbinfo *sbinfo)
29990+{
29991+ const char *pat;
f2c43d5f
AM
29992+ char fmt[32];
29993+ struct au_wbr_mfs *mfs;
1facf9fc 29994+
dece6358
AM
29995+ AuRwMustAnyLock(&sbinfo->si_rwsem);
29996+
c2b27bf2 29997+ seq_puts(m, ",create=");
1facf9fc 29998+ pat = au_optstr_wbr_create(v);
f2c43d5f 29999+ mfs = &sbinfo->si_wbr_mfs;
1facf9fc 30000+ switch (v) {
30001+ case AuWbrCreate_TDP:
30002+ case AuWbrCreate_RR:
30003+ case AuWbrCreate_MFS:
30004+ case AuWbrCreate_PMFS:
c2b27bf2 30005+ seq_puts(m, pat);
1facf9fc 30006+ break;
f2c43d5f
AM
30007+ case AuWbrCreate_MFSRR:
30008+ case AuWbrCreate_TDMFS:
30009+ case AuWbrCreate_PMFSRR:
30010+ au_gen_fmt(fmt, sizeof(fmt), pat, "%llu");
30011+ seq_printf(m, fmt, mfs->mfsrr_watermark);
1facf9fc 30012+ break;
f2c43d5f 30013+ case AuWbrCreate_MFSV:
1facf9fc 30014+ case AuWbrCreate_PMFSV:
f2c43d5f
AM
30015+ au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
30016+ seq_printf(m, fmt,
30017+ jiffies_to_msecs(mfs->mfs_expire)
e49829fe 30018+ / MSEC_PER_SEC);
1facf9fc 30019+ break;
1facf9fc 30020+ case AuWbrCreate_MFSRRV:
f2c43d5f 30021+ case AuWbrCreate_TDMFSV:
392086de 30022+ case AuWbrCreate_PMFSRRV:
f2c43d5f
AM
30023+ au_gen_fmt(fmt, sizeof(fmt), pat, "%llu:%lu");
30024+ seq_printf(m, fmt, mfs->mfsrr_watermark,
30025+ jiffies_to_msecs(mfs->mfs_expire) / MSEC_PER_SEC);
392086de 30026+ break;
f2c43d5f
AM
30027+ default:
30028+ BUG();
1facf9fc 30029+ }
30030+}
30031+
7eafdf33 30032+static int au_show_xino(struct seq_file *seq, struct super_block *sb)
1facf9fc 30033+{
30034+#ifdef CONFIG_SYSFS
30035+ return 0;
30036+#else
30037+ int err;
30038+ const int len = sizeof(AUFS_XINO_FNAME) - 1;
30039+ aufs_bindex_t bindex, brid;
1facf9fc 30040+ struct qstr *name;
30041+ struct file *f;
30042+ struct dentry *d, *h_root;
acd2b654 30043+ struct au_branch *br;
1facf9fc 30044+
dece6358
AM
30045+ AuRwMustAnyLock(&sbinfo->si_rwsem);
30046+
1facf9fc 30047+ err = 0;
1facf9fc 30048+ f = au_sbi(sb)->si_xib;
30049+ if (!f)
30050+ goto out;
30051+
30052+ /* stop printing the default xino path on the first writable branch */
30053+ h_root = NULL;
acd2b654
AM
30054+ bindex = au_xi_root(sb, f->f_path.dentry);
30055+ if (bindex >= 0) {
30056+ br = au_sbr_sb(sb, bindex);
30057+ h_root = au_br_dentry(br);
1facf9fc 30058+ }
acd2b654 30059+
2000de60 30060+ d = f->f_path.dentry;
1facf9fc 30061+ name = &d->d_name;
30062+ /* safe ->d_parent because the file is unlinked */
30063+ if (d->d_parent == h_root
30064+ && name->len == len
30065+ && !memcmp(name->name, AUFS_XINO_FNAME, len))
30066+ goto out;
30067+
30068+ seq_puts(seq, ",xino=");
30069+ err = au_xino_path(seq, f);
30070+
4f0767ce 30071+out:
1facf9fc 30072+ return err;
30073+#endif
30074+}
30075+
30076+/* seq_file will re-call me in case of too long string */
7eafdf33 30077+static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
1facf9fc 30078+{
027c5e7a 30079+ int err;
1facf9fc 30080+ unsigned int mnt_flags, v;
30081+ struct super_block *sb;
30082+ struct au_sbinfo *sbinfo;
30083+
30084+#define AuBool(name, str) do { \
30085+ v = au_opt_test(mnt_flags, name); \
30086+ if (v != au_opt_test(AuOpt_Def, name)) \
30087+ seq_printf(m, ",%s" #str, v ? "" : "no"); \
30088+} while (0)
30089+
30090+#define AuStr(name, str) do { \
30091+ v = mnt_flags & AuOptMask_##name; \
30092+ if (v != (AuOpt_Def & AuOptMask_##name)) \
30093+ seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
30094+} while (0)
30095+
30096+#define AuUInt(name, str, val) do { \
30097+ if (val != AUFS_##name##_DEF) \
30098+ seq_printf(m, "," #str "=%u", val); \
30099+} while (0)
30100+
7eafdf33 30101+ sb = dentry->d_sb;
2121bcd9 30102+ if (sb->s_flags & SB_POSIXACL)
c1595e42 30103+ seq_puts(m, ",acl");
43982f53 30104+#if 0 /* reserved for future use */
be118d29
JR
30105+ if (sb->s_flags & SB_I_VERSION)
30106+ seq_puts(m, ",i_version");
30107+#endif
c1595e42
JR
30108+
30109+ /* lock free root dinfo */
1facf9fc 30110+ si_noflush_read_lock(sb);
30111+ sbinfo = au_sbi(sb);
30112+ seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
30113+
30114+ mnt_flags = au_mntflags(sb);
30115+ if (au_opt_test(mnt_flags, XINO)) {
7eafdf33 30116+ err = au_show_xino(m, sb);
1facf9fc 30117+ if (unlikely(err))
30118+ goto out;
30119+ } else
30120+ seq_puts(m, ",noxino");
30121+
30122+ AuBool(TRUNC_XINO, trunc_xino);
30123+ AuStr(UDBA, udba);
dece6358 30124+ AuBool(SHWH, shwh);
1facf9fc 30125+ AuBool(PLINK, plink);
4a4d8108 30126+ AuBool(DIO, dio);
076b876e 30127+ AuBool(DIRPERM1, dirperm1);
1facf9fc 30128+
30129+ v = sbinfo->si_wbr_create;
30130+ if (v != AuWbrCreate_Def)
30131+ au_show_wbr_create(m, v, sbinfo);
30132+
30133+ v = sbinfo->si_wbr_copyup;
30134+ if (v != AuWbrCopyup_Def)
30135+ seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
30136+
30137+ v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
30138+ if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
30139+ seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
30140+
30141+ AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
30142+
027c5e7a
AM
30143+ v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
30144+ AuUInt(RDCACHE, rdcache, v);
1facf9fc 30145+
30146+ AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
30147+ AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
30148+
076b876e
AM
30149+ au_fhsm_show(m, sbinfo);
30150+
8b6a4947 30151+ AuBool(DIRREN, dirren);
1facf9fc 30152+ AuBool(SUM, sum);
30153+ /* AuBool(SUM_W, wsum); */
30154+ AuBool(WARN_PERM, warn_perm);
30155+ AuBool(VERBOSE, verbose);
30156+
4f0767ce 30157+out:
1facf9fc 30158+ /* be sure to print "br:" last */
30159+ if (!sysaufs_brs) {
30160+ seq_puts(m, ",br:");
30161+ au_show_brs(m, sb);
30162+ }
30163+ si_read_unlock(sb);
30164+ return 0;
30165+
1facf9fc 30166+#undef AuBool
30167+#undef AuStr
4a4d8108 30168+#undef AuUInt
1facf9fc 30169+}
30170+
30171+/* ---------------------------------------------------------------------- */
30172+
30173+/* sum mode which returns the summation for statfs(2) */
30174+
30175+static u64 au_add_till_max(u64 a, u64 b)
30176+{
30177+ u64 old;
30178+
30179+ old = a;
30180+ a += b;
92d182d2
AM
30181+ if (old <= a)
30182+ return a;
30183+ return ULLONG_MAX;
30184+}
30185+
30186+static u64 au_mul_till_max(u64 a, long mul)
30187+{
30188+ u64 old;
30189+
30190+ old = a;
30191+ a *= mul;
30192+ if (old <= a)
1facf9fc 30193+ return a;
30194+ return ULLONG_MAX;
30195+}
30196+
30197+static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
30198+{
30199+ int err;
92d182d2 30200+ long bsize, factor;
1facf9fc 30201+ u64 blocks, bfree, bavail, files, ffree;
5afbbe0d 30202+ aufs_bindex_t bbot, bindex, i;
1facf9fc 30203+ unsigned char shared;
7f207e10 30204+ struct path h_path;
1facf9fc 30205+ struct super_block *h_sb;
30206+
92d182d2
AM
30207+ err = 0;
30208+ bsize = LONG_MAX;
30209+ files = 0;
30210+ ffree = 0;
1facf9fc 30211+ blocks = 0;
30212+ bfree = 0;
30213+ bavail = 0;
5afbbe0d
AM
30214+ bbot = au_sbbot(sb);
30215+ for (bindex = 0; bindex <= bbot; bindex++) {
7f207e10
AM
30216+ h_path.mnt = au_sbr_mnt(sb, bindex);
30217+ h_sb = h_path.mnt->mnt_sb;
1facf9fc 30218+ shared = 0;
92d182d2 30219+ for (i = 0; !shared && i < bindex; i++)
1facf9fc 30220+ shared = (au_sbr_sb(sb, i) == h_sb);
30221+ if (shared)
30222+ continue;
30223+
30224+ /* sb->s_root for NFS is unreliable */
7f207e10
AM
30225+ h_path.dentry = h_path.mnt->mnt_root;
30226+ err = vfs_statfs(&h_path, buf);
1facf9fc 30227+ if (unlikely(err))
30228+ goto out;
30229+
92d182d2
AM
30230+ if (bsize > buf->f_bsize) {
30231+ /*
30232+ * we will reduce bsize, so we have to expand blocks
30233+ * etc. to match them again
30234+ */
30235+ factor = (bsize / buf->f_bsize);
30236+ blocks = au_mul_till_max(blocks, factor);
30237+ bfree = au_mul_till_max(bfree, factor);
30238+ bavail = au_mul_till_max(bavail, factor);
30239+ bsize = buf->f_bsize;
30240+ }
30241+
30242+ factor = (buf->f_bsize / bsize);
30243+ blocks = au_add_till_max(blocks,
30244+ au_mul_till_max(buf->f_blocks, factor));
30245+ bfree = au_add_till_max(bfree,
30246+ au_mul_till_max(buf->f_bfree, factor));
30247+ bavail = au_add_till_max(bavail,
30248+ au_mul_till_max(buf->f_bavail, factor));
1facf9fc 30249+ files = au_add_till_max(files, buf->f_files);
30250+ ffree = au_add_till_max(ffree, buf->f_ffree);
30251+ }
30252+
92d182d2 30253+ buf->f_bsize = bsize;
1facf9fc 30254+ buf->f_blocks = blocks;
30255+ buf->f_bfree = bfree;
30256+ buf->f_bavail = bavail;
30257+ buf->f_files = files;
30258+ buf->f_ffree = ffree;
92d182d2 30259+ buf->f_frsize = 0;
1facf9fc 30260+
4f0767ce 30261+out:
1facf9fc 30262+ return err;
30263+}
30264+
30265+static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
30266+{
30267+ int err;
7f207e10 30268+ struct path h_path;
1facf9fc 30269+ struct super_block *sb;
30270+
30271+ /* lock free root dinfo */
30272+ sb = dentry->d_sb;
30273+ si_noflush_read_lock(sb);
7f207e10 30274+ if (!au_opt_test(au_mntflags(sb), SUM)) {
1facf9fc 30275+ /* sb->s_root for NFS is unreliable */
7f207e10
AM
30276+ h_path.mnt = au_sbr_mnt(sb, 0);
30277+ h_path.dentry = h_path.mnt->mnt_root;
30278+ err = vfs_statfs(&h_path, buf);
30279+ } else
1facf9fc 30280+ err = au_statfs_sum(sb, buf);
30281+ si_read_unlock(sb);
30282+
30283+ if (!err) {
30284+ buf->f_type = AUFS_SUPER_MAGIC;
4a4d8108 30285+ buf->f_namelen = AUFS_MAX_NAMELEN;
1facf9fc 30286+ memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
30287+ }
30288+ /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
30289+
30290+ return err;
30291+}
30292+
30293+/* ---------------------------------------------------------------------- */
30294+
537831f9
AM
30295+static int aufs_sync_fs(struct super_block *sb, int wait)
30296+{
30297+ int err, e;
5afbbe0d 30298+ aufs_bindex_t bbot, bindex;
537831f9
AM
30299+ struct au_branch *br;
30300+ struct super_block *h_sb;
30301+
30302+ err = 0;
30303+ si_noflush_read_lock(sb);
5afbbe0d
AM
30304+ bbot = au_sbbot(sb);
30305+ for (bindex = 0; bindex <= bbot; bindex++) {
537831f9
AM
30306+ br = au_sbr(sb, bindex);
30307+ if (!au_br_writable(br->br_perm))
30308+ continue;
30309+
30310+ h_sb = au_sbr_sb(sb, bindex);
cd233405 30311+ e = vfsub_sync_filesystem(h_sb);
a2654f78
AM
30312+ if (unlikely(e && !err))
30313+ err = e;
30314+ /* go on even if an error happens */
537831f9
AM
30315+ }
30316+ si_read_unlock(sb);
30317+
30318+ return err;
30319+}
30320+
30321+/* ---------------------------------------------------------------------- */
30322+
1facf9fc 30323+/* final actions when unmounting a file system */
30324+static void aufs_put_super(struct super_block *sb)
30325+{
30326+ struct au_sbinfo *sbinfo;
30327+
30328+ sbinfo = au_sbi(sb);
062440b3
AM
30329+ if (sbinfo)
30330+ kobject_put(&sbinfo->si_kobj);
1facf9fc 30331+}
30332+
30333+/* ---------------------------------------------------------------------- */
30334+
79b8bda9
AM
30335+void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30336+ struct super_block *sb, void *arg)
7f207e10
AM
30337+{
30338+ void *array;
076b876e 30339+ unsigned long long n, sz;
7f207e10
AM
30340+
30341+ array = NULL;
30342+ n = 0;
30343+ if (!*hint)
30344+ goto out;
30345+
30346+ if (*hint > ULLONG_MAX / sizeof(array)) {
30347+ array = ERR_PTR(-EMFILE);
30348+ pr_err("hint %llu\n", *hint);
30349+ goto out;
30350+ }
30351+
076b876e
AM
30352+ sz = sizeof(array) * *hint;
30353+ array = kzalloc(sz, GFP_NOFS);
7f207e10 30354+ if (unlikely(!array))
076b876e 30355+ array = vzalloc(sz);
7f207e10
AM
30356+ if (unlikely(!array)) {
30357+ array = ERR_PTR(-ENOMEM);
30358+ goto out;
30359+ }
30360+
79b8bda9 30361+ n = cb(sb, array, *hint, arg);
7f207e10
AM
30362+ AuDebugOn(n > *hint);
30363+
30364+out:
30365+ *hint = n;
30366+ return array;
30367+}
30368+
79b8bda9 30369+static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
7f207e10
AM
30370+ unsigned long long max __maybe_unused,
30371+ void *arg)
30372+{
30373+ unsigned long long n;
30374+ struct inode **p, *inode;
30375+ struct list_head *head;
30376+
30377+ n = 0;
30378+ p = a;
30379+ head = arg;
79b8bda9 30380+ spin_lock(&sb->s_inode_list_lock);
7f207e10 30381+ list_for_each_entry(inode, head, i_sb_list) {
5afbbe0d
AM
30382+ if (!au_is_bad_inode(inode)
30383+ && au_ii(inode)->ii_btop >= 0) {
2cbb1c4b
JR
30384+ spin_lock(&inode->i_lock);
30385+ if (atomic_read(&inode->i_count)) {
30386+ au_igrab(inode);
30387+ *p++ = inode;
30388+ n++;
30389+ AuDebugOn(n > max);
30390+ }
30391+ spin_unlock(&inode->i_lock);
7f207e10
AM
30392+ }
30393+ }
79b8bda9 30394+ spin_unlock(&sb->s_inode_list_lock);
7f207e10
AM
30395+
30396+ return n;
30397+}
30398+
30399+struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
30400+{
acd2b654
AM
30401+ struct au_sbinfo *sbi;
30402+
30403+ sbi = au_sbi(sb);
30404+ *max = au_lcnt_read(&sbi->si_ninodes, /*do_rev*/1);
79b8bda9 30405+ return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
7f207e10
AM
30406+}
30407+
30408+void au_iarray_free(struct inode **a, unsigned long long max)
30409+{
30410+ unsigned long long ull;
30411+
30412+ for (ull = 0; ull < max; ull++)
30413+ iput(a[ull]);
be52b249 30414+ kvfree(a);
7f207e10
AM
30415+}
30416+
30417+/* ---------------------------------------------------------------------- */
30418+
1facf9fc 30419+/*
30420+ * refresh dentry and inode at remount time.
30421+ */
027c5e7a
AM
30422+/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
30423+static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
30424+ struct dentry *parent)
1facf9fc 30425+{
30426+ int err;
1facf9fc 30427+
30428+ di_write_lock_child(dentry);
1facf9fc 30429+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a
AM
30430+ err = au_refresh_dentry(dentry, parent);
30431+ if (!err && dir_flags)
5527c038 30432+ au_hn_reset(d_inode(dentry), dir_flags);
1facf9fc 30433+ di_read_unlock(parent, AuLock_IR);
1facf9fc 30434+ di_write_unlock(dentry);
30435+
30436+ return err;
30437+}
30438+
027c5e7a
AM
30439+static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
30440+ struct au_sbinfo *sbinfo,
b95c5147 30441+ const unsigned int dir_flags, unsigned int do_idop)
1facf9fc 30442+{
027c5e7a
AM
30443+ int err;
30444+ struct dentry *parent;
027c5e7a
AM
30445+
30446+ err = 0;
30447+ parent = dget_parent(dentry);
30448+ if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
5527c038
JR
30449+ if (d_really_is_positive(dentry)) {
30450+ if (!d_is_dir(dentry))
027c5e7a
AM
30451+ err = au_do_refresh(dentry, /*dir_flags*/0,
30452+ parent);
30453+ else {
30454+ err = au_do_refresh(dentry, dir_flags, parent);
30455+ if (unlikely(err))
30456+ au_fset_si(sbinfo, FAILED_REFRESH_DIR);
30457+ }
30458+ } else
30459+ err = au_do_refresh(dentry, /*dir_flags*/0, parent);
30460+ AuDbgDentry(dentry);
30461+ }
30462+ dput(parent);
30463+
79b8bda9 30464+ if (!err) {
b95c5147 30465+ if (do_idop)
79b8bda9
AM
30466+ au_refresh_dop(dentry, /*force_reval*/0);
30467+ } else
30468+ au_refresh_dop(dentry, /*force_reval*/1);
30469+
027c5e7a
AM
30470+ AuTraceErr(err);
30471+ return err;
1facf9fc 30472+}
30473+
b95c5147 30474+static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
1facf9fc 30475+{
30476+ int err, i, j, ndentry, e;
027c5e7a 30477+ unsigned int sigen;
1facf9fc 30478+ struct au_dcsub_pages dpages;
30479+ struct au_dpage *dpage;
027c5e7a
AM
30480+ struct dentry **dentries, *d;
30481+ struct au_sbinfo *sbinfo;
30482+ struct dentry *root = sb->s_root;
5527c038 30483+ const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
1facf9fc 30484+
b95c5147 30485+ if (do_idop)
79b8bda9
AM
30486+ au_refresh_dop(root, /*force_reval*/0);
30487+
027c5e7a
AM
30488+ err = au_dpages_init(&dpages, GFP_NOFS);
30489+ if (unlikely(err))
1facf9fc 30490+ goto out;
027c5e7a
AM
30491+ err = au_dcsub_pages(&dpages, root, NULL, NULL);
30492+ if (unlikely(err))
1facf9fc 30493+ goto out_dpages;
1facf9fc 30494+
027c5e7a
AM
30495+ sigen = au_sigen(sb);
30496+ sbinfo = au_sbi(sb);
30497+ for (i = 0; i < dpages.ndpage; i++) {
1facf9fc 30498+ dpage = dpages.dpages + i;
30499+ dentries = dpage->dentries;
30500+ ndentry = dpage->ndentry;
027c5e7a 30501+ for (j = 0; j < ndentry; j++) {
1facf9fc 30502+ d = dentries[j];
79b8bda9 30503+ e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
b95c5147 30504+ do_idop);
027c5e7a
AM
30505+ if (unlikely(e && !err))
30506+ err = e;
30507+ /* go on even err */
1facf9fc 30508+ }
30509+ }
30510+
4f0767ce 30511+out_dpages:
1facf9fc 30512+ au_dpages_free(&dpages);
4f0767ce 30513+out:
1facf9fc 30514+ return err;
30515+}
30516+
b95c5147 30517+static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
1facf9fc 30518+{
027c5e7a
AM
30519+ int err, e;
30520+ unsigned int sigen;
30521+ unsigned long long max, ull;
30522+ struct inode *inode, **array;
1facf9fc 30523+
027c5e7a
AM
30524+ array = au_iarray_alloc(sb, &max);
30525+ err = PTR_ERR(array);
30526+ if (IS_ERR(array))
30527+ goto out;
1facf9fc 30528+
30529+ err = 0;
027c5e7a
AM
30530+ sigen = au_sigen(sb);
30531+ for (ull = 0; ull < max; ull++) {
30532+ inode = array[ull];
076b876e
AM
30533+ if (unlikely(!inode))
30534+ break;
b95c5147
AM
30535+
30536+ e = 0;
30537+ ii_write_lock_child(inode);
537831f9 30538+ if (au_iigen(inode, NULL) != sigen) {
027c5e7a 30539+ e = au_refresh_hinode_self(inode);
1facf9fc 30540+ if (unlikely(e)) {
b95c5147 30541+ au_refresh_iop(inode, /*force_getattr*/1);
027c5e7a 30542+ pr_err("error %d, i%lu\n", e, inode->i_ino);
1facf9fc 30543+ if (!err)
30544+ err = e;
30545+ /* go on even if err */
30546+ }
30547+ }
b95c5147
AM
30548+ if (!e && do_idop)
30549+ au_refresh_iop(inode, /*force_getattr*/0);
30550+ ii_write_unlock(inode);
1facf9fc 30551+ }
30552+
027c5e7a 30553+ au_iarray_free(array, max);
1facf9fc 30554+
4f0767ce 30555+out:
1facf9fc 30556+ return err;
30557+}
30558+
f4d37d76 30559+void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
1facf9fc 30560+{
027c5e7a
AM
30561+ int err, e;
30562+ unsigned int udba;
5afbbe0d 30563+ aufs_bindex_t bindex, bbot;
1facf9fc 30564+ struct dentry *root;
30565+ struct inode *inode;
027c5e7a 30566+ struct au_branch *br;
79b8bda9 30567+ struct au_sbinfo *sbi;
1facf9fc 30568+
30569+ au_sigen_inc(sb);
79b8bda9
AM
30570+ sbi = au_sbi(sb);
30571+ au_fclr_si(sbi, FAILED_REFRESH_DIR);
1facf9fc 30572+
30573+ root = sb->s_root;
30574+ DiMustNoWaiters(root);
5527c038 30575+ inode = d_inode(root);
1facf9fc 30576+ IiMustNoWaiters(inode);
1facf9fc 30577+
027c5e7a 30578+ udba = au_opt_udba(sb);
5afbbe0d
AM
30579+ bbot = au_sbbot(sb);
30580+ for (bindex = 0; bindex <= bbot; bindex++) {
027c5e7a
AM
30581+ br = au_sbr(sb, bindex);
30582+ err = au_hnotify_reset_br(udba, br, br->br_perm);
1facf9fc 30583+ if (unlikely(err))
027c5e7a
AM
30584+ AuIOErr("hnotify failed on br %d, %d, ignored\n",
30585+ bindex, err);
30586+ /* go on even if err */
1facf9fc 30587+ }
027c5e7a 30588+ au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
1facf9fc 30589+
b95c5147 30590+ if (do_idop) {
79b8bda9
AM
30591+ if (au_ftest_si(sbi, NO_DREVAL)) {
30592+ AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
30593+ sb->s_d_op = &aufs_dop_noreval;
b95c5147
AM
30594+ AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
30595+ sbi->si_iop_array = aufs_iop_nogetattr;
79b8bda9
AM
30596+ } else {
30597+ AuDebugOn(sb->s_d_op == &aufs_dop);
30598+ sb->s_d_op = &aufs_dop;
b95c5147
AM
30599+ AuDebugOn(sbi->si_iop_array == aufs_iop);
30600+ sbi->si_iop_array = aufs_iop;
79b8bda9 30601+ }
062440b3 30602+ pr_info("reset to %ps and %ps\n",
b95c5147 30603+ sb->s_d_op, sbi->si_iop_array);
79b8bda9
AM
30604+ }
30605+
027c5e7a 30606+ di_write_unlock(root);
b95c5147
AM
30607+ err = au_refresh_d(sb, do_idop);
30608+ e = au_refresh_i(sb, do_idop);
027c5e7a
AM
30609+ if (unlikely(e && !err))
30610+ err = e;
1facf9fc 30611+ /* aufs_write_lock() calls ..._child() */
30612+ di_write_lock_child(root);
027c5e7a
AM
30613+
30614+ au_cpup_attr_all(inode, /*force*/1);
30615+
30616+ if (unlikely(err))
30617+ AuIOErr("refresh failed, ignored, %d\n", err);
1facf9fc 30618+}
30619+
f4d37d76 30620+const struct super_operations aufs_sop = {
1facf9fc 30621+ .alloc_inode = aufs_alloc_inode,
30622+ .destroy_inode = aufs_destroy_inode,
fbc438ed 30623+ .free_inode = aufs_free_inode,
b752ccd1 30624+ /* always deleting, no clearing */
1facf9fc 30625+ .drop_inode = generic_delete_inode,
30626+ .show_options = aufs_show_options,
30627+ .statfs = aufs_statfs,
30628+ .put_super = aufs_put_super,
f4d37d76 30629+ .sync_fs = aufs_sync_fs
1facf9fc 30630+};
30631+
30632+/* ---------------------------------------------------------------------- */
30633+
f4d37d76 30634+int au_alloc_root(struct super_block *sb)
1facf9fc 30635+{
30636+ int err;
30637+ struct inode *inode;
30638+ struct dentry *root;
30639+
30640+ err = -ENOMEM;
30641+ inode = au_iget_locked(sb, AUFS_ROOT_INO);
30642+ err = PTR_ERR(inode);
30643+ if (IS_ERR(inode))
30644+ goto out;
30645+
b95c5147 30646+ inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
1facf9fc 30647+ inode->i_fop = &aufs_dir_fop;
30648+ inode->i_mode = S_IFDIR;
9dbd164d 30649+ set_nlink(inode, 2);
1facf9fc 30650+ unlock_new_inode(inode);
30651+
92d182d2 30652+ root = d_make_root(inode);
1facf9fc 30653+ if (unlikely(!root))
92d182d2 30654+ goto out;
1facf9fc 30655+ err = PTR_ERR(root);
30656+ if (IS_ERR(root))
92d182d2 30657+ goto out;
1facf9fc 30658+
4a4d8108 30659+ err = au_di_init(root);
1facf9fc 30660+ if (!err) {
30661+ sb->s_root = root;
30662+ return 0; /* success */
30663+ }
30664+ dput(root);
1facf9fc 30665+
4f0767ce 30666+out:
1facf9fc 30667+ return err;
1facf9fc 30668+}
30669+
f4d37d76
AM
30670+/* ---------------------------------------------------------------------- */
30671+
30672+static void aufs_kill_sb(struct super_block *sb)
1facf9fc 30673+{
79b8bda9 30674+ struct au_sbinfo *sbinfo;
1facf9fc 30675+ struct dentry *root;
1facf9fc 30676+
79b8bda9 30677+ sbinfo = au_sbi(sb);
f4d37d76
AM
30678+ if (!sbinfo)
30679+ goto out;
1facf9fc 30680+
f4d37d76 30681+ au_sbilist_del(sb);
1facf9fc 30682+
1facf9fc 30683+ root = sb->s_root;
f4d37d76
AM
30684+ if (root)
30685+ aufs_write_lock(root);
30686+ else
30687+ __si_write_lock(sb);
1facf9fc 30688+
f4d37d76
AM
30689+ au_fhsm_fin(sb);
30690+ if (sbinfo->si_wbr_create_ops->fin)
30691+ sbinfo->si_wbr_create_ops->fin(sb);
30692+ if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
30693+ au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
30694+ au_remount_refresh(sb, /*do_idop*/0);
79b8bda9 30695+ }
f4d37d76
AM
30696+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
30697+ au_plink_put(sb, /*verbose*/1);
30698+ au_xino_clr(sb);
30699+ if (root)
30700+ au_dr_opt_flush(sb);
1facf9fc 30701+
f4d37d76
AM
30702+ if (root)
30703+ aufs_write_unlock(root);
30704+ else
30705+ __si_write_unlock(sb);
027c5e7a 30706+
f4d37d76
AM
30707+ sbinfo->si_sb = NULL;
30708+ au_nwt_flush(&sbinfo->si_nowait);
027c5e7a
AM
30709+
30710+out:
98d9a5b1 30711+ kill_anon_super(sb);
e49829fe
JR
30712+}
30713+
1facf9fc 30714+struct file_system_type aufs_fs_type = {
30715+ .name = AUFS_FSTYPE,
c06a8ce3 30716+ /* a race between rename and others */
0b2a12c6
JR
30717+ .fs_flags = FS_RENAME_DOES_D_MOVE
30718+ /* untested */
30719+ /*| FS_ALLOW_IDMAP*/
30720+ ,
f4d37d76
AM
30721+ .init_fs_context = aufs_fsctx_init,
30722+ .parameters = aufs_fsctx_paramspec,
e49829fe 30723+ .kill_sb = aufs_kill_sb,
1facf9fc 30724+ /* no need to __module_get() and module_put(). */
30725+ .owner = THIS_MODULE,
30726+};
7f207e10 30727diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
eca34b5c 30728--- /usr/share/empty/fs/aufs/super.h 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 30729+++ linux/fs/aufs/super.h 2022-11-05 23:02:18.969222617 +0100
f4d37d76 30730@@ -0,0 +1,592 @@
062440b3 30731+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 30732+/*
0dcfbb52 30733+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 30734+ *
0dcfbb52 30735+ * This program is free software; you can redistribute it and/or modify
1facf9fc 30736+ * it under the terms of the GNU General Public License as published by
30737+ * the Free Software Foundation; either version 2 of the License, or
30738+ * (at your option) any later version.
dece6358
AM
30739+ *
30740+ * This program is distributed in the hope that it will be useful,
30741+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
30742+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30743+ * GNU General Public License for more details.
30744+ *
30745+ * You should have received a copy of the GNU General Public License
523b37e3 30746+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 30747+ */
30748+
30749+/*
30750+ * super_block operations
30751+ */
30752+
30753+#ifndef __AUFS_SUPER_H__
30754+#define __AUFS_SUPER_H__
30755+
30756+#ifdef __KERNEL__
30757+
30758+#include <linux/fs.h>
5527c038 30759+#include <linux/kobject.h>
8b6a4947 30760+#include "hbl.h"
acd2b654 30761+#include "lcnt.h"
1facf9fc 30762+#include "rwsem.h"
1facf9fc 30763+#include "wkq.h"
30764+
1facf9fc 30765+/* policies to select one among multiple writable branches */
30766+struct au_wbr_copyup_operations {
30767+ int (*copyup)(struct dentry *dentry);
30768+};
30769+
392086de
AM
30770+#define AuWbr_DIR 1 /* target is a dir */
30771+#define AuWbr_PARENT (1 << 1) /* always require a parent */
30772+
30773+#define au_ftest_wbr(flags, name) ((flags) & AuWbr_##name)
30774+#define au_fset_wbr(flags, name) { (flags) |= AuWbr_##name; }
30775+#define au_fclr_wbr(flags, name) { (flags) &= ~AuWbr_##name; }
30776+
1facf9fc 30777+struct au_wbr_create_operations {
392086de 30778+ int (*create)(struct dentry *dentry, unsigned int flags);
1facf9fc 30779+ int (*init)(struct super_block *sb);
30780+ int (*fin)(struct super_block *sb);
30781+};
30782+
30783+struct au_wbr_mfs {
30784+ struct mutex mfs_lock; /* protect this structure */
30785+ unsigned long mfs_jiffy;
30786+ unsigned long mfs_expire;
30787+ aufs_bindex_t mfs_bindex;
30788+
30789+ unsigned long long mfsrr_bytes;
30790+ unsigned long long mfsrr_watermark;
30791+};
30792+
86dc4139
AM
30793+#define AuPlink_NHASH 100
30794+static inline int au_plink_hash(ino_t ino)
30795+{
30796+ return ino % AuPlink_NHASH;
30797+}
30798+
076b876e
AM
30799+/* File-based Hierarchical Storage Management */
30800+struct au_fhsm {
30801+#ifdef CONFIG_AUFS_FHSM
30802+ /* allow only one process who can receive the notification */
30803+ spinlock_t fhsm_spin;
30804+ pid_t fhsm_pid;
30805+ wait_queue_head_t fhsm_wqh;
30806+ atomic_t fhsm_readable;
30807+
c1595e42 30808+ /* these are protected by si_rwsem */
076b876e 30809+ unsigned long fhsm_expire;
c1595e42 30810+ aufs_bindex_t fhsm_bottom;
076b876e
AM
30811+#endif
30812+};
30813+
1facf9fc 30814+struct au_branch;
30815+struct au_sbinfo {
30816+ /* nowait tasks in the system-wide workqueue */
30817+ struct au_nowait_tasks si_nowait;
30818+
b752ccd1 30819+ /*
acd2b654 30820+ * tried sb->s_umount, but failed due to the dependency between i_mutex.
b752ccd1
AM
30821+ * rwsem for au_sbinfo is necessary.
30822+ */
dece6358 30823+ struct au_rwsem si_rwsem;
1facf9fc 30824+
7f207e10 30825+ /*
523b37e3
AM
30826+ * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
30827+ * remount.
7f207e10 30828+ */
acd2b654 30829+ au_lcnt_t si_ninodes, si_nfiles;
7f207e10 30830+
1facf9fc 30831+ /* branch management */
30832+ unsigned int si_generation;
30833+
2000de60 30834+ /* see AuSi_ flags */
1facf9fc 30835+ unsigned char au_si_status;
30836+
5afbbe0d 30837+ aufs_bindex_t si_bbot;
7f207e10
AM
30838+
30839+ /* dirty trick to keep br_id plus */
30840+ unsigned int si_last_br_id :
30841+ sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
1facf9fc 30842+ struct au_branch **si_branch;
30843+
30844+ /* policy to select a writable branch */
30845+ unsigned char si_wbr_copyup;
30846+ unsigned char si_wbr_create;
30847+ struct au_wbr_copyup_operations *si_wbr_copyup_ops;
30848+ struct au_wbr_create_operations *si_wbr_create_ops;
30849+
30850+ /* round robin */
30851+ atomic_t si_wbr_rr_next;
30852+
30853+ /* most free space */
30854+ struct au_wbr_mfs si_wbr_mfs;
30855+
076b876e
AM
30856+ /* File-based Hierarchical Storage Management */
30857+ struct au_fhsm si_fhsm;
30858+
1facf9fc 30859+ /* mount flags */
30860+ /* include/asm-ia64/siginfo.h defines a macro named si_flags */
30861+ unsigned int si_mntflags;
30862+
30863+ /* external inode number (bitmap and translation table) */
acd2b654
AM
30864+ loff_t si_ximaxent; /* max entries in a xino */
30865+
1facf9fc 30866+ struct file *si_xib;
30867+ struct mutex si_xib_mtx; /* protect xib members */
30868+ unsigned long *si_xib_buf;
30869+ unsigned long si_xib_last_pindex;
30870+ int si_xib_next_bit;
acd2b654 30871+
392086de
AM
30872+ unsigned long si_xino_jiffy;
30873+ unsigned long si_xino_expire;
1facf9fc 30874+ /* reserved for future use */
30875+ /* unsigned long long si_xib_limit; */ /* Max xib file size */
30876+
30877+#ifdef CONFIG_AUFS_EXPORT
30878+ /* i_generation */
acd2b654 30879+ /* todo: make xigen file an array to support many inode numbers */
1facf9fc 30880+ struct file *si_xigen;
30881+ atomic_t si_xigen_next;
30882+#endif
30883+
acd2b654 30884+ /* dirty trick to support atomic_open */
8b6a4947 30885+ struct hlist_bl_head si_aopen;
b912730e 30886+
1facf9fc 30887+ /* vdir parameters */
e49829fe 30888+ unsigned long si_rdcache; /* max cache time in jiffies */
1facf9fc 30889+ unsigned int si_rdblk; /* deblk size */
30890+ unsigned int si_rdhash; /* hash size */
30891+
30892+ /*
30893+ * If the number of whiteouts are larger than si_dirwh, leave all of
30894+ * them after au_whtmp_ren to reduce the cost of rmdir(2).
30895+ * future fsck.aufs or kernel thread will remove them later.
30896+ * Otherwise, remove all whiteouts and the dir in rmdir(2).
30897+ */
30898+ unsigned int si_dirwh;
30899+
1facf9fc 30900+ /* pseudo_link list */
8b6a4947 30901+ struct hlist_bl_head si_plink[AuPlink_NHASH];
1facf9fc 30902+ wait_queue_head_t si_plink_wq;
4a4d8108 30903+ spinlock_t si_plink_maint_lock;
e49829fe 30904+ pid_t si_plink_maint_pid;
1facf9fc 30905+
523b37e3 30906+ /* file list */
8b6a4947 30907+ struct hlist_bl_head si_files;
523b37e3 30908+
b95c5147 30909+ /* with/without getattr, brother of sb->s_d_op */
43982f53 30910+ const struct inode_operations *si_iop_array;
b95c5147 30911+
1facf9fc 30912+ /*
30913+ * sysfs and lifetime management.
30914+ * this is not a small structure and it may be a waste of memory in case
acd2b654 30915+ * of sysfs is disabled, particularly when many aufs-es are mounted.
1facf9fc 30916+ * but using sysfs is majority.
30917+ */
30918+ struct kobject si_kobj;
30919+#ifdef CONFIG_DEBUG_FS
86dc4139
AM
30920+ struct dentry *si_dbgaufs;
30921+ struct dentry *si_dbgaufs_plink;
30922+ struct dentry *si_dbgaufs_xib;
1facf9fc 30923+#ifdef CONFIG_AUFS_EXPORT
30924+ struct dentry *si_dbgaufs_xigen;
30925+#endif
30926+#endif
30927+
e49829fe 30928+#ifdef CONFIG_AUFS_SBILIST
8b6a4947 30929+ struct hlist_bl_node si_list;
e49829fe
JR
30930+#endif
30931+
1facf9fc 30932+ /* dirty, necessary for unmounting, sysfs and sysrq */
30933+ struct super_block *si_sb;
30934+};
30935+
dece6358
AM
30936+/* sbinfo status flags */
30937+/*
30938+ * set true when refresh_dirs() failed at remount time.
30939+ * then try refreshing dirs at access time again.
062440b3 30940+ * if it is false, refreshing dirs at access time is unnecessary
dece6358 30941+ */
027c5e7a 30942+#define AuSi_FAILED_REFRESH_DIR 1
076b876e 30943+#define AuSi_FHSM (1 << 1) /* fhsm is active now */
79b8bda9 30944+#define AuSi_NO_DREVAL (1 << 2) /* disable all d_revalidate */
076b876e
AM
30945+
30946+#ifndef CONFIG_AUFS_FHSM
30947+#undef AuSi_FHSM
30948+#define AuSi_FHSM 0
30949+#endif
30950+
dece6358
AM
30951+static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
30952+ unsigned int flag)
30953+{
30954+ AuRwMustAnyLock(&sbi->si_rwsem);
30955+ return sbi->au_si_status & flag;
30956+}
30957+#define au_ftest_si(sbinfo, name) au_do_ftest_si(sbinfo, AuSi_##name)
30958+#define au_fset_si(sbinfo, name) do { \
30959+ AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30960+ (sbinfo)->au_si_status |= AuSi_##name; \
30961+} while (0)
30962+#define au_fclr_si(sbinfo, name) do { \
30963+ AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30964+ (sbinfo)->au_si_status &= ~AuSi_##name; \
30965+} while (0)
30966+
1facf9fc 30967+/* ---------------------------------------------------------------------- */
30968+
30969+/* policy to select one among writable branches */
4a4d8108
AM
30970+#define AuWbrCopyup(sbinfo, ...) \
30971+ ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
30972+#define AuWbrCreate(sbinfo, ...) \
30973+ ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
1facf9fc 30974+
30975+/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
30976+#define AuLock_DW 1 /* write-lock dentry */
30977+#define AuLock_IR (1 << 1) /* read-lock inode */
30978+#define AuLock_IW (1 << 2) /* write-lock inode */
30979+#define AuLock_FLUSH (1 << 3) /* wait for 'nowait' tasks */
b95c5147 30980+#define AuLock_DIRS (1 << 4) /* target is a pair of dirs */
f2c43d5f 30981+ /* except RENAME_EXCHANGE */
e49829fe
JR
30982+#define AuLock_NOPLM (1 << 5) /* return err in plm mode */
30983+#define AuLock_NOPLMW (1 << 6) /* wait for plm mode ends */
027c5e7a 30984+#define AuLock_GEN (1 << 7) /* test digen/iigen */
1facf9fc 30985+#define au_ftest_lock(flags, name) ((flags) & AuLock_##name)
7f207e10
AM
30986+#define au_fset_lock(flags, name) \
30987+ do { (flags) |= AuLock_##name; } while (0)
30988+#define au_fclr_lock(flags, name) \
30989+ do { (flags) &= ~AuLock_##name; } while (0)
1facf9fc 30990+
30991+/* ---------------------------------------------------------------------- */
30992+
30993+/* super.c */
1facf9fc 30994+struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
f4d37d76 30995+
79b8bda9
AM
30996+typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
30997+ unsigned long long max, void *arg);
79b8bda9
AM
30998+void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30999+ struct super_block *sb, void *arg);
7f207e10
AM
31000+struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
31001+void au_iarray_free(struct inode **a, unsigned long long max);
1facf9fc 31002+
f4d37d76
AM
31003+void au_remount_refresh(struct super_block *sb, unsigned int do_idop);
31004+extern const struct super_operations aufs_sop;
31005+int au_alloc_root(struct super_block *sb);
31006+extern struct file_system_type aufs_fs_type;
31007+
1facf9fc 31008+/* sbinfo.c */
31009+void au_si_free(struct kobject *kobj);
f4d37d76 31010+struct au_sbinfo *au_si_alloc(struct super_block *sb);
e2f27e51 31011+int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
1facf9fc 31012+
31013+unsigned int au_sigen_inc(struct super_block *sb);
31014+aufs_bindex_t au_new_br_id(struct super_block *sb);
31015+
e49829fe
JR
31016+int si_read_lock(struct super_block *sb, int flags);
31017+int si_write_lock(struct super_block *sb, int flags);
31018+int aufs_read_lock(struct dentry *dentry, int flags);
1facf9fc 31019+void aufs_read_unlock(struct dentry *dentry, int flags);
31020+void aufs_write_lock(struct dentry *dentry);
31021+void aufs_write_unlock(struct dentry *dentry);
e49829fe 31022+int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
1facf9fc 31023+void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
31024+
31025+/* wbr_policy.c */
31026+extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
31027+extern struct au_wbr_create_operations au_wbr_create_ops[];
31028+int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
c2b27bf2 31029+int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
5afbbe0d 31030+int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
c2b27bf2
AM
31031+
31032+/* mvdown.c */
31033+int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
1facf9fc 31034+
076b876e
AM
31035+#ifdef CONFIG_AUFS_FHSM
31036+/* fhsm.c */
31037+
31038+static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
31039+{
31040+ pid_t pid;
31041+
31042+ spin_lock(&fhsm->fhsm_spin);
31043+ pid = fhsm->fhsm_pid;
31044+ spin_unlock(&fhsm->fhsm_spin);
31045+
31046+ return pid;
31047+}
31048+
31049+void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
31050+void au_fhsm_wrote_all(struct super_block *sb, int force);
31051+int au_fhsm_fd(struct super_block *sb, int oflags);
31052+int au_fhsm_br_alloc(struct au_branch *br);
c1595e42 31053+void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
076b876e
AM
31054+void au_fhsm_fin(struct super_block *sb);
31055+void au_fhsm_init(struct au_sbinfo *sbinfo);
31056+void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
31057+void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
31058+#else
31059+AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
31060+ int force)
31061+AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
31062+AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
c1595e42
JR
31063+AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
31064+AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
31065+AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
076b876e
AM
31066+AuStubVoid(au_fhsm_fin, struct super_block *sb)
31067+AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
31068+AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
31069+AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
31070+#endif
31071+
1facf9fc 31072+/* ---------------------------------------------------------------------- */
31073+
31074+static inline struct au_sbinfo *au_sbi(struct super_block *sb)
31075+{
31076+ return sb->s_fs_info;
31077+}
31078+
31079+/* ---------------------------------------------------------------------- */
31080+
31081+#ifdef CONFIG_AUFS_EXPORT
a2a7ad62 31082+int au_test_nfsd(void);
1facf9fc 31083+void au_export_init(struct super_block *sb);
b752ccd1 31084+void au_xigen_inc(struct inode *inode);
1facf9fc 31085+int au_xigen_new(struct inode *inode);
062440b3 31086+int au_xigen_set(struct super_block *sb, struct path *path);
1facf9fc 31087+void au_xigen_clr(struct super_block *sb);
31088+
31089+static inline int au_busy_or_stale(void)
31090+{
b752ccd1 31091+ if (!au_test_nfsd())
1facf9fc 31092+ return -EBUSY;
31093+ return -ESTALE;
31094+}
31095+#else
b752ccd1 31096+AuStubInt0(au_test_nfsd, void)
a2a7ad62 31097+AuStubVoid(au_export_init, struct super_block *sb)
b752ccd1 31098+AuStubVoid(au_xigen_inc, struct inode *inode)
4a4d8108 31099+AuStubInt0(au_xigen_new, struct inode *inode)
062440b3 31100+AuStubInt0(au_xigen_set, struct super_block *sb, struct path *path)
4a4d8108 31101+AuStubVoid(au_xigen_clr, struct super_block *sb)
c1595e42 31102+AuStub(int, au_busy_or_stale, return -EBUSY, void)
1facf9fc 31103+#endif /* CONFIG_AUFS_EXPORT */
31104+
31105+/* ---------------------------------------------------------------------- */
31106+
e49829fe
JR
31107+#ifdef CONFIG_AUFS_SBILIST
31108+/* module.c */
8b6a4947 31109+extern struct hlist_bl_head au_sbilist;
e49829fe
JR
31110+
31111+static inline void au_sbilist_init(void)
31112+{
8b6a4947 31113+ INIT_HLIST_BL_HEAD(&au_sbilist);
e49829fe
JR
31114+}
31115+
31116+static inline void au_sbilist_add(struct super_block *sb)
31117+{
8b6a4947 31118+ au_hbl_add(&au_sbi(sb)->si_list, &au_sbilist);
e49829fe
JR
31119+}
31120+
31121+static inline void au_sbilist_del(struct super_block *sb)
31122+{
8b6a4947 31123+ au_hbl_del(&au_sbi(sb)->si_list, &au_sbilist);
e49829fe 31124+}
53392da6
AM
31125+
31126+#ifdef CONFIG_AUFS_MAGIC_SYSRQ
31127+static inline void au_sbilist_lock(void)
31128+{
8b6a4947 31129+ hlist_bl_lock(&au_sbilist);
53392da6
AM
31130+}
31131+
31132+static inline void au_sbilist_unlock(void)
31133+{
8b6a4947 31134+ hlist_bl_unlock(&au_sbilist);
53392da6
AM
31135+}
31136+#define AuGFP_SBILIST GFP_ATOMIC
31137+#else
31138+AuStubVoid(au_sbilist_lock, void)
31139+AuStubVoid(au_sbilist_unlock, void)
31140+#define AuGFP_SBILIST GFP_NOFS
31141+#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
e49829fe
JR
31142+#else
31143+AuStubVoid(au_sbilist_init, void)
c1595e42
JR
31144+AuStubVoid(au_sbilist_add, struct super_block *sb)
31145+AuStubVoid(au_sbilist_del, struct super_block *sb)
53392da6
AM
31146+AuStubVoid(au_sbilist_lock, void)
31147+AuStubVoid(au_sbilist_unlock, void)
31148+#define AuGFP_SBILIST GFP_NOFS
e49829fe
JR
31149+#endif
31150+
31151+/* ---------------------------------------------------------------------- */
31152+
1facf9fc 31153+static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
31154+{
dece6358 31155+ /*
c1595e42 31156+ * This function is a dynamic '__init' function actually,
dece6358
AM
31157+ * so the tiny check for si_rwsem is unnecessary.
31158+ */
31159+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
1facf9fc 31160+#ifdef CONFIG_DEBUG_FS
31161+ sbinfo->si_dbgaufs = NULL;
86dc4139 31162+ sbinfo->si_dbgaufs_plink = NULL;
1facf9fc 31163+ sbinfo->si_dbgaufs_xib = NULL;
31164+#ifdef CONFIG_AUFS_EXPORT
31165+ sbinfo->si_dbgaufs_xigen = NULL;
31166+#endif
31167+#endif
31168+}
31169+
31170+/* ---------------------------------------------------------------------- */
31171+
a2654f78
AM
31172+/* current->atomic_flags */
31173+/* this value should never corrupt the ones defined in linux/sched.h */
fbc438ed 31174+#define PFA_AUFS 0x10
a2654f78
AM
31175+
31176+TASK_PFA_TEST(AUFS, test_aufs) /* task_test_aufs */
31177+TASK_PFA_SET(AUFS, aufs) /* task_set_aufs */
31178+TASK_PFA_CLEAR(AUFS, aufs) /* task_clear_aufs */
b752ccd1
AM
31179+
31180+static inline int si_pid_test(struct super_block *sb)
31181+{
a2654f78 31182+ return !!task_test_aufs(current);
b752ccd1
AM
31183+}
31184+
31185+static inline void si_pid_clr(struct super_block *sb)
31186+{
a2654f78
AM
31187+ AuDebugOn(!task_test_aufs(current));
31188+ task_clear_aufs(current);
b752ccd1
AM
31189+}
31190+
a2654f78
AM
31191+static inline void si_pid_set(struct super_block *sb)
31192+{
31193+ AuDebugOn(task_test_aufs(current));
31194+ task_set_aufs(current);
31195+}
febd17d6 31196+
b752ccd1
AM
31197+/* ---------------------------------------------------------------------- */
31198+
1facf9fc 31199+/* lock superblock. mainly for entry point functions */
8b6a4947
AM
31200+#define __si_read_lock(sb) au_rw_read_lock(&au_sbi(sb)->si_rwsem)
31201+#define __si_write_lock(sb) au_rw_write_lock(&au_sbi(sb)->si_rwsem)
31202+#define __si_read_trylock(sb) au_rw_read_trylock(&au_sbi(sb)->si_rwsem)
31203+#define __si_write_trylock(sb) au_rw_write_trylock(&au_sbi(sb)->si_rwsem)
31204+/*
31205+#define __si_read_trylock_nested(sb) \
31206+ au_rw_read_trylock_nested(&au_sbi(sb)->si_rwsem)
31207+#define __si_write_trylock_nested(sb) \
31208+ au_rw_write_trylock_nested(&au_sbi(sb)->si_rwsem)
31209+*/
31210+
31211+#define __si_read_unlock(sb) au_rw_read_unlock(&au_sbi(sb)->si_rwsem)
31212+#define __si_write_unlock(sb) au_rw_write_unlock(&au_sbi(sb)->si_rwsem)
31213+#define __si_downgrade_lock(sb) au_rw_dgrade_lock(&au_sbi(sb)->si_rwsem)
1facf9fc 31214+
dece6358
AM
31215+#define SiMustNoWaiters(sb) AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
31216+#define SiMustAnyLock(sb) AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
31217+#define SiMustWriteLock(sb) AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
31218+
b752ccd1
AM
31219+static inline void si_noflush_read_lock(struct super_block *sb)
31220+{
31221+ __si_read_lock(sb);
31222+ si_pid_set(sb);
31223+}
31224+
31225+static inline int si_noflush_read_trylock(struct super_block *sb)
31226+{
076b876e
AM
31227+ int locked;
31228+
31229+ locked = __si_read_trylock(sb);
b752ccd1
AM
31230+ if (locked)
31231+ si_pid_set(sb);
31232+ return locked;
31233+}
31234+
31235+static inline void si_noflush_write_lock(struct super_block *sb)
31236+{
31237+ __si_write_lock(sb);
31238+ si_pid_set(sb);
31239+}
31240+
31241+static inline int si_noflush_write_trylock(struct super_block *sb)
31242+{
076b876e
AM
31243+ int locked;
31244+
31245+ locked = __si_write_trylock(sb);
b752ccd1
AM
31246+ if (locked)
31247+ si_pid_set(sb);
31248+ return locked;
31249+}
31250+
7e9cd9fe 31251+#if 0 /* reserved */
1facf9fc 31252+static inline int si_read_trylock(struct super_block *sb, int flags)
31253+{
31254+ if (au_ftest_lock(flags, FLUSH))
31255+ au_nwt_flush(&au_sbi(sb)->si_nowait);
31256+ return si_noflush_read_trylock(sb);
31257+}
e49829fe 31258+#endif
1facf9fc 31259+
b752ccd1
AM
31260+static inline void si_read_unlock(struct super_block *sb)
31261+{
31262+ si_pid_clr(sb);
31263+ __si_read_unlock(sb);
31264+}
31265+
7e9cd9fe 31266+#if 0 /* reserved */
1facf9fc 31267+static inline int si_write_trylock(struct super_block *sb, int flags)
31268+{
31269+ if (au_ftest_lock(flags, FLUSH))
31270+ au_nwt_flush(&au_sbi(sb)->si_nowait);
31271+ return si_noflush_write_trylock(sb);
31272+}
b752ccd1
AM
31273+#endif
31274+
31275+static inline void si_write_unlock(struct super_block *sb)
31276+{
31277+ si_pid_clr(sb);
31278+ __si_write_unlock(sb);
31279+}
31280+
7e9cd9fe 31281+#if 0 /* reserved */
b752ccd1
AM
31282+static inline void si_downgrade_lock(struct super_block *sb)
31283+{
31284+ __si_downgrade_lock(sb);
31285+}
31286+#endif
1facf9fc 31287+
31288+/* ---------------------------------------------------------------------- */
31289+
5afbbe0d 31290+static inline aufs_bindex_t au_sbbot(struct super_block *sb)
1facf9fc 31291+{
dece6358 31292+ SiMustAnyLock(sb);
5afbbe0d 31293+ return au_sbi(sb)->si_bbot;
1facf9fc 31294+}
31295+
31296+static inline unsigned int au_mntflags(struct super_block *sb)
31297+{
dece6358 31298+ SiMustAnyLock(sb);
1facf9fc 31299+ return au_sbi(sb)->si_mntflags;
31300+}
31301+
31302+static inline unsigned int au_sigen(struct super_block *sb)
31303+{
dece6358 31304+ SiMustAnyLock(sb);
1facf9fc 31305+ return au_sbi(sb)->si_generation;
31306+}
31307+
31308+static inline struct au_branch *au_sbr(struct super_block *sb,
31309+ aufs_bindex_t bindex)
31310+{
dece6358 31311+ SiMustAnyLock(sb);
1facf9fc 31312+ return au_sbi(sb)->si_branch[0 + bindex];
31313+}
31314+
acd2b654 31315+static inline loff_t au_xi_maxent(struct super_block *sb)
1facf9fc 31316+{
dece6358 31317+ SiMustAnyLock(sb);
acd2b654 31318+ return au_sbi(sb)->si_ximaxent;
1facf9fc 31319+}
31320+
31321+#endif /* __KERNEL__ */
31322+#endif /* __AUFS_SUPER_H__ */
7f207e10 31323diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
eca34b5c 31324--- /usr/share/empty/fs/aufs/sysaufs.c 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 31325+++ linux/fs/aufs/sysaufs.c 2022-11-05 23:02:18.969222617 +0100
26eb093c 31326@@ -0,0 +1,94 @@
cd7a4cd9 31327+// SPDX-License-Identifier: GPL-2.0
1facf9fc 31328+/*
0dcfbb52 31329+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 31330+ *
0dcfbb52 31331+ * This program is free software; you can redistribute it and/or modify
1facf9fc 31332+ * it under the terms of the GNU General Public License as published by
31333+ * the Free Software Foundation; either version 2 of the License, or
31334+ * (at your option) any later version.
dece6358
AM
31335+ *
31336+ * This program is distributed in the hope that it will be useful,
31337+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31338+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31339+ * GNU General Public License for more details.
31340+ *
31341+ * You should have received a copy of the GNU General Public License
523b37e3 31342+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31343+ */
31344+
31345+/*
31346+ * sysfs interface and lifetime management
31347+ * they are necessary regardless sysfs is disabled.
31348+ */
31349+
1facf9fc 31350+#include <linux/random.h>
1facf9fc 31351+#include "aufs.h"
31352+
31353+unsigned long sysaufs_si_mask;
e49829fe 31354+struct kset *sysaufs_kset;
1facf9fc 31355+
31356+#define AuSiAttr(_name) { \
31357+ .attr = { .name = __stringify(_name), .mode = 0444 }, \
31358+ .show = sysaufs_si_##_name, \
31359+}
31360+
31361+static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
31362+struct attribute *sysaufs_si_attrs[] = {
31363+ &sysaufs_si_attr_xi_path.attr,
31364+ NULL,
31365+};
26eb093c 31366+ATTRIBUTE_GROUPS(sysaufs_si);
1facf9fc 31367+
4a4d8108 31368+static const struct sysfs_ops au_sbi_ops = {
1facf9fc 31369+ .show = sysaufs_si_show
31370+};
31371+
31372+static struct kobj_type au_sbi_ktype = {
31373+ .release = au_si_free,
31374+ .sysfs_ops = &au_sbi_ops,
26eb093c 31375+ .default_groups = sysaufs_si_groups
1facf9fc 31376+};
31377+
31378+/* ---------------------------------------------------------------------- */
31379+
31380+int sysaufs_si_init(struct au_sbinfo *sbinfo)
31381+{
31382+ int err;
31383+
e49829fe 31384+ sbinfo->si_kobj.kset = sysaufs_kset;
1facf9fc 31385+ /* cf. sysaufs_name() */
31386+ err = kobject_init_and_add
e49829fe 31387+ (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
1facf9fc 31388+ SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
31389+
1facf9fc 31390+ return err;
31391+}
31392+
31393+void sysaufs_fin(void)
31394+{
e49829fe
JR
31395+ sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31396+ kset_unregister(sysaufs_kset);
1facf9fc 31397+}
31398+
31399+int __init sysaufs_init(void)
31400+{
31401+ int err;
31402+
31403+ do {
31404+ get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
31405+ } while (!sysaufs_si_mask);
31406+
4a4d8108 31407+ err = -EINVAL;
e49829fe
JR
31408+ sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
31409+ if (unlikely(!sysaufs_kset))
4a4d8108 31410+ goto out;
e49829fe
JR
31411+ err = PTR_ERR(sysaufs_kset);
31412+ if (IS_ERR(sysaufs_kset))
1facf9fc 31413+ goto out;
e49829fe 31414+ err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
062440b3 31415+ if (unlikely(err))
e49829fe 31416+ kset_unregister(sysaufs_kset);
1facf9fc 31417+
4f0767ce 31418+out:
1facf9fc 31419+ return err;
31420+}
7f207e10 31421diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
eca34b5c 31422--- /usr/share/empty/fs/aufs/sysaufs.h 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 31423+++ linux/fs/aufs/sysaufs.h 2022-11-05 23:02:18.969222617 +0100
062440b3
AM
31424@@ -0,0 +1,102 @@
31425+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 31426+/*
0dcfbb52 31427+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 31428+ *
0dcfbb52 31429+ * This program is free software; you can redistribute it and/or modify
1facf9fc 31430+ * it under the terms of the GNU General Public License as published by
31431+ * the Free Software Foundation; either version 2 of the License, or
31432+ * (at your option) any later version.
dece6358
AM
31433+ *
31434+ * This program is distributed in the hope that it will be useful,
31435+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31436+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31437+ * GNU General Public License for more details.
31438+ *
31439+ * You should have received a copy of the GNU General Public License
523b37e3 31440+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31441+ */
31442+
31443+/*
31444+ * sysfs interface and mount lifetime management
31445+ */
31446+
31447+#ifndef __SYSAUFS_H__
31448+#define __SYSAUFS_H__
31449+
31450+#ifdef __KERNEL__
31451+
1facf9fc 31452+#include <linux/sysfs.h>
1facf9fc 31453+#include "module.h"
31454+
dece6358
AM
31455+struct super_block;
31456+struct au_sbinfo;
31457+
1facf9fc 31458+struct sysaufs_si_attr {
31459+ struct attribute attr;
31460+ int (*show)(struct seq_file *seq, struct super_block *sb);
31461+};
31462+
31463+/* ---------------------------------------------------------------------- */
31464+
31465+/* sysaufs.c */
31466+extern unsigned long sysaufs_si_mask;
e49829fe 31467+extern struct kset *sysaufs_kset;
1facf9fc 31468+extern struct attribute *sysaufs_si_attrs[];
31469+int sysaufs_si_init(struct au_sbinfo *sbinfo);
31470+int __init sysaufs_init(void);
31471+void sysaufs_fin(void);
31472+
31473+/* ---------------------------------------------------------------------- */
31474+
31475+/* some people doesn't like to show a pointer in kernel */
31476+static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
31477+{
31478+ return sysaufs_si_mask ^ (unsigned long)sbinfo;
31479+}
31480+
31481+#define SysaufsSiNamePrefix "si_"
31482+#define SysaufsSiNameLen (sizeof(SysaufsSiNamePrefix) + 16)
31483+static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
31484+{
31485+ snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
31486+ sysaufs_si_id(sbinfo));
31487+}
31488+
31489+struct au_branch;
31490+#ifdef CONFIG_SYSFS
31491+/* sysfs.c */
31492+extern struct attribute_group *sysaufs_attr_group;
31493+
31494+int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
31495+ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31496+ char *buf);
076b876e
AM
31497+long au_brinfo_ioctl(struct file *file, unsigned long arg);
31498+#ifdef CONFIG_COMPAT
31499+long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
31500+#endif
1facf9fc 31501+
31502+void sysaufs_br_init(struct au_branch *br);
31503+void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
31504+void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
31505+
31506+#define sysaufs_brs_init() do {} while (0)
31507+
31508+#else
31509+#define sysaufs_attr_group NULL
31510+
4a4d8108 31511+AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
c1595e42
JR
31512+AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
31513+ struct attribute *attr, char *buf)
4a4d8108
AM
31514+AuStubVoid(sysaufs_br_init, struct au_branch *br)
31515+AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
31516+AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
1facf9fc 31517+
31518+static inline void sysaufs_brs_init(void)
31519+{
31520+ sysaufs_brs = 0;
31521+}
31522+
31523+#endif /* CONFIG_SYSFS */
31524+
31525+#endif /* __KERNEL__ */
31526+#endif /* __SYSAUFS_H__ */
7f207e10 31527diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
eca34b5c 31528--- /usr/share/empty/fs/aufs/sysfs.c 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 31529+++ linux/fs/aufs/sysfs.c 2022-11-05 23:02:18.969222617 +0100
ba1aed25 31530@@ -0,0 +1,374 @@
cd7a4cd9 31531+// SPDX-License-Identifier: GPL-2.0
1facf9fc 31532+/*
0dcfbb52 31533+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 31534+ *
0dcfbb52 31535+ * This program is free software; you can redistribute it and/or modify
1facf9fc 31536+ * it under the terms of the GNU General Public License as published by
31537+ * the Free Software Foundation; either version 2 of the License, or
31538+ * (at your option) any later version.
dece6358
AM
31539+ *
31540+ * This program is distributed in the hope that it will be useful,
31541+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31542+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31543+ * GNU General Public License for more details.
31544+ *
31545+ * You should have received a copy of the GNU General Public License
523b37e3 31546+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31547+ */
31548+
31549+/*
31550+ * sysfs interface
31551+ */
31552+
076b876e 31553+#include <linux/compat.h>
1facf9fc 31554+#include <linux/seq_file.h>
1facf9fc 31555+#include "aufs.h"
31556+
4a4d8108
AM
31557+#ifdef CONFIG_AUFS_FS_MODULE
31558+/* this entry violates the "one line per file" policy of sysfs */
31559+static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
31560+ char *buf)
31561+{
31562+ ssize_t err;
31563+ static char *conf =
31564+/* this file is generated at compiling */
31565+#include "conf.str"
31566+ ;
31567+
31568+ err = snprintf(buf, PAGE_SIZE, conf);
31569+ if (unlikely(err >= PAGE_SIZE))
31570+ err = -EFBIG;
31571+ return err;
31572+}
31573+
31574+static struct kobj_attribute au_config_attr = __ATTR_RO(config);
31575+#endif
31576+
1facf9fc 31577+static struct attribute *au_attr[] = {
4a4d8108
AM
31578+#ifdef CONFIG_AUFS_FS_MODULE
31579+ &au_config_attr.attr,
31580+#endif
1facf9fc 31581+ NULL, /* need to NULL terminate the list of attributes */
31582+};
31583+
31584+static struct attribute_group sysaufs_attr_group_body = {
31585+ .attrs = au_attr
31586+};
31587+
31588+struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
31589+
31590+/* ---------------------------------------------------------------------- */
31591+
31592+int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
31593+{
31594+ int err;
31595+
dece6358
AM
31596+ SiMustAnyLock(sb);
31597+
1facf9fc 31598+ err = 0;
31599+ if (au_opt_test(au_mntflags(sb), XINO)) {
31600+ err = au_xino_path(seq, au_sbi(sb)->si_xib);
31601+ seq_putc(seq, '\n');
31602+ }
31603+ return err;
31604+}
31605+
31606+/*
31607+ * the lifetime of branch is independent from the entry under sysfs.
31608+ * sysfs handles the lifetime of the entry, and never call ->show() after it is
31609+ * unlinked.
31610+ */
31611+static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
392086de 31612+ aufs_bindex_t bindex, int idx)
1facf9fc 31613+{
1e00d052 31614+ int err;
1facf9fc 31615+ struct path path;
31616+ struct dentry *root;
31617+ struct au_branch *br;
076b876e 31618+ au_br_perm_str_t perm;
1facf9fc 31619+
31620+ AuDbg("b%d\n", bindex);
31621+
1e00d052 31622+ err = 0;
1facf9fc 31623+ root = sb->s_root;
31624+ di_read_lock_parent(root, !AuLock_IR);
31625+ br = au_sbr(sb, bindex);
392086de
AM
31626+
31627+ switch (idx) {
31628+ case AuBrSysfs_BR:
31629+ path.mnt = au_br_mnt(br);
31630+ path.dentry = au_h_dptr(root, bindex);
79b8bda9
AM
31631+ err = au_seq_path(seq, &path);
31632+ if (!err) {
31633+ au_optstr_br_perm(&perm, br->br_perm);
31634+ seq_printf(seq, "=%s\n", perm.a);
31635+ }
392086de
AM
31636+ break;
31637+ case AuBrSysfs_BRID:
79b8bda9 31638+ seq_printf(seq, "%d\n", br->br_id);
392086de
AM
31639+ break;
31640+ }
076b876e 31641+ di_read_unlock(root, !AuLock_IR);
79b8bda9 31642+ if (unlikely(err || seq_has_overflowed(seq)))
076b876e 31643+ err = -E2BIG;
392086de 31644+
1e00d052 31645+ return err;
1facf9fc 31646+}
31647+
31648+/* ---------------------------------------------------------------------- */
31649+
31650+static struct seq_file *au_seq(char *p, ssize_t len)
31651+{
31652+ struct seq_file *seq;
31653+
31654+ seq = kzalloc(sizeof(*seq), GFP_NOFS);
31655+ if (seq) {
31656+ /* mutex_init(&seq.lock); */
31657+ seq->buf = p;
31658+ seq->size = len;
31659+ return seq; /* success */
31660+ }
31661+
31662+ seq = ERR_PTR(-ENOMEM);
31663+ return seq;
31664+}
31665+
392086de
AM
31666+#define SysaufsBr_PREFIX "br"
31667+#define SysaufsBrid_PREFIX "brid"
1facf9fc 31668+
31669+/* todo: file size may exceed PAGE_SIZE */
31670+ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
1308ab2a 31671+ char *buf)
1facf9fc 31672+{
31673+ ssize_t err;
392086de 31674+ int idx;
1facf9fc 31675+ long l;
5afbbe0d 31676+ aufs_bindex_t bbot;
1facf9fc 31677+ struct au_sbinfo *sbinfo;
31678+ struct super_block *sb;
31679+ struct seq_file *seq;
31680+ char *name;
31681+ struct attribute **cattr;
31682+
31683+ sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
31684+ sb = sbinfo->si_sb;
1308ab2a 31685+
31686+ /*
31687+ * prevent a race condition between sysfs and aufs.
31688+ * for instance, sysfs_file_read() calls sysfs_get_active_two() which
31689+ * prohibits maintaining the sysfs entries.
31690+ * hew we acquire read lock after sysfs_get_active_two().
31691+ * on the other hand, the remount process may maintain the sysfs/aufs
31692+ * entries after acquiring write lock.
31693+ * it can cause a deadlock.
31694+ * simply we gave up processing read here.
31695+ */
31696+ err = -EBUSY;
31697+ if (unlikely(!si_noflush_read_trylock(sb)))
31698+ goto out;
1facf9fc 31699+
31700+ seq = au_seq(buf, PAGE_SIZE);
31701+ err = PTR_ERR(seq);
31702+ if (IS_ERR(seq))
1308ab2a 31703+ goto out_unlock;
1facf9fc 31704+
31705+ name = (void *)attr->name;
31706+ cattr = sysaufs_si_attrs;
31707+ while (*cattr) {
31708+ if (!strcmp(name, (*cattr)->name)) {
31709+ err = container_of(*cattr, struct sysaufs_si_attr, attr)
31710+ ->show(seq, sb);
31711+ goto out_seq;
31712+ }
31713+ cattr++;
31714+ }
31715+
392086de
AM
31716+ if (!strncmp(name, SysaufsBrid_PREFIX,
31717+ sizeof(SysaufsBrid_PREFIX) - 1)) {
31718+ idx = AuBrSysfs_BRID;
31719+ name += sizeof(SysaufsBrid_PREFIX) - 1;
31720+ } else if (!strncmp(name, SysaufsBr_PREFIX,
31721+ sizeof(SysaufsBr_PREFIX) - 1)) {
31722+ idx = AuBrSysfs_BR;
1facf9fc 31723+ name += sizeof(SysaufsBr_PREFIX) - 1;
392086de
AM
31724+ } else
31725+ BUG();
31726+
31727+ err = kstrtol(name, 10, &l);
31728+ if (!err) {
5afbbe0d
AM
31729+ bbot = au_sbbot(sb);
31730+ if (l <= bbot)
392086de
AM
31731+ err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
31732+ else
31733+ err = -ENOENT;
1facf9fc 31734+ }
1facf9fc 31735+
4f0767ce 31736+out_seq:
1facf9fc 31737+ if (!err) {
31738+ err = seq->count;
31739+ /* sysfs limit */
31740+ if (unlikely(err == PAGE_SIZE))
31741+ err = -EFBIG;
31742+ }
9f237c51 31743+ au_kfree_rcu(seq);
4f0767ce 31744+out_unlock:
1facf9fc 31745+ si_read_unlock(sb);
4f0767ce 31746+out:
1facf9fc 31747+ return err;
31748+}
31749+
31750+/* ---------------------------------------------------------------------- */
31751+
076b876e
AM
31752+static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
31753+{
31754+ int err;
31755+ int16_t brid;
5afbbe0d 31756+ aufs_bindex_t bindex, bbot;
076b876e
AM
31757+ size_t sz;
31758+ char *buf;
31759+ struct seq_file *seq;
31760+ struct au_branch *br;
31761+
31762+ si_read_lock(sb, AuLock_FLUSH);
5afbbe0d
AM
31763+ bbot = au_sbbot(sb);
31764+ err = bbot + 1;
076b876e
AM
31765+ if (!arg)
31766+ goto out;
31767+
31768+ err = -ENOMEM;
31769+ buf = (void *)__get_free_page(GFP_NOFS);
31770+ if (unlikely(!buf))
31771+ goto out;
31772+
31773+ seq = au_seq(buf, PAGE_SIZE);
31774+ err = PTR_ERR(seq);
31775+ if (IS_ERR(seq))
31776+ goto out_buf;
31777+
31778+ sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
5afbbe0d 31779+ for (bindex = 0; bindex <= bbot; bindex++, arg++) {
ba1aed25
AM
31780+ /* VERIFY_WRITE */
31781+ err = !access_ok(arg, sizeof(*arg));
076b876e
AM
31782+ if (unlikely(err))
31783+ break;
31784+
31785+ br = au_sbr(sb, bindex);
31786+ brid = br->br_id;
31787+ BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
31788+ err = __put_user(brid, &arg->id);
31789+ if (unlikely(err))
31790+ break;
31791+
31792+ BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
31793+ err = __put_user(br->br_perm, &arg->perm);
31794+ if (unlikely(err))
31795+ break;
31796+
79b8bda9
AM
31797+ err = au_seq_path(seq, &br->br_path);
31798+ if (unlikely(err))
31799+ break;
31800+ seq_putc(seq, '\0');
31801+ if (!seq_has_overflowed(seq)) {
076b876e
AM
31802+ err = copy_to_user(arg->path, seq->buf, seq->count);
31803+ seq->count = 0;
31804+ if (unlikely(err))
31805+ break;
31806+ } else {
31807+ err = -E2BIG;
31808+ goto out_seq;
31809+ }
31810+ }
31811+ if (unlikely(err))
31812+ err = -EFAULT;
31813+
31814+out_seq:
9f237c51 31815+ au_kfree_rcu(seq);
076b876e 31816+out_buf:
1c60b727 31817+ free_page((unsigned long)buf);
076b876e
AM
31818+out:
31819+ si_read_unlock(sb);
31820+ return err;
31821+}
31822+
31823+long au_brinfo_ioctl(struct file *file, unsigned long arg)
31824+{
2000de60 31825+ return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
076b876e
AM
31826+}
31827+
31828+#ifdef CONFIG_COMPAT
31829+long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
31830+{
2000de60 31831+ return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
076b876e
AM
31832+}
31833+#endif
31834+
31835+/* ---------------------------------------------------------------------- */
31836+
1facf9fc 31837+void sysaufs_br_init(struct au_branch *br)
31838+{
392086de
AM
31839+ int i;
31840+ struct au_brsysfs *br_sysfs;
31841+ struct attribute *attr;
4a4d8108 31842+
392086de
AM
31843+ br_sysfs = br->br_sysfs;
31844+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31845+ attr = &br_sysfs->attr;
31846+ sysfs_attr_init(attr);
31847+ attr->name = br_sysfs->name;
cd7a4cd9 31848+ attr->mode = 0444;
392086de
AM
31849+ br_sysfs++;
31850+ }
1facf9fc 31851+}
31852+
31853+void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
31854+{
31855+ struct au_branch *br;
31856+ struct kobject *kobj;
392086de
AM
31857+ struct au_brsysfs *br_sysfs;
31858+ int i;
5afbbe0d 31859+ aufs_bindex_t bbot;
1facf9fc 31860+
1facf9fc 31861+ if (!sysaufs_brs)
31862+ return;
31863+
31864+ kobj = &au_sbi(sb)->si_kobj;
5afbbe0d
AM
31865+ bbot = au_sbbot(sb);
31866+ for (; bindex <= bbot; bindex++) {
1facf9fc 31867+ br = au_sbr(sb, bindex);
392086de
AM
31868+ br_sysfs = br->br_sysfs;
31869+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31870+ sysfs_remove_file(kobj, &br_sysfs->attr);
31871+ br_sysfs++;
31872+ }
1facf9fc 31873+ }
31874+}
31875+
31876+void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
31877+{
392086de 31878+ int err, i;
5afbbe0d 31879+ aufs_bindex_t bbot;
1facf9fc 31880+ struct kobject *kobj;
31881+ struct au_branch *br;
392086de 31882+ struct au_brsysfs *br_sysfs;
1facf9fc 31883+
1facf9fc 31884+ if (!sysaufs_brs)
31885+ return;
31886+
31887+ kobj = &au_sbi(sb)->si_kobj;
5afbbe0d
AM
31888+ bbot = au_sbbot(sb);
31889+ for (; bindex <= bbot; bindex++) {
1facf9fc 31890+ br = au_sbr(sb, bindex);
392086de
AM
31891+ br_sysfs = br->br_sysfs;
31892+ snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
31893+ SysaufsBr_PREFIX "%d", bindex);
31894+ snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
31895+ SysaufsBrid_PREFIX "%d", bindex);
31896+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31897+ err = sysfs_create_file(kobj, &br_sysfs->attr);
31898+ if (unlikely(err))
31899+ pr_warn("failed %s under sysfs(%d)\n",
31900+ br_sysfs->name, err);
31901+ br_sysfs++;
31902+ }
1facf9fc 31903+ }
31904+}
7f207e10 31905diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
eca34b5c 31906--- /usr/share/empty/fs/aufs/sysrq.c 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 31907+++ linux/fs/aufs/sysrq.c 2022-11-05 23:02:18.969222617 +0100
43982f53 31908@@ -0,0 +1,149 @@
cd7a4cd9 31909+// SPDX-License-Identifier: GPL-2.0
1facf9fc 31910+/*
0dcfbb52 31911+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 31912+ *
0dcfbb52 31913+ * This program is free software; you can redistribute it and/or modify
1facf9fc 31914+ * it under the terms of the GNU General Public License as published by
31915+ * the Free Software Foundation; either version 2 of the License, or
31916+ * (at your option) any later version.
dece6358
AM
31917+ *
31918+ * This program is distributed in the hope that it will be useful,
31919+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31920+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31921+ * GNU General Public License for more details.
31922+ *
31923+ * You should have received a copy of the GNU General Public License
523b37e3 31924+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31925+ */
31926+
31927+/*
acd2b654 31928+ * magic sysrq handler
1facf9fc 31929+ */
31930+
1facf9fc 31931+/* #include <linux/sysrq.h> */
027c5e7a 31932+#include <linux/writeback.h>
1facf9fc 31933+#include "aufs.h"
31934+
31935+/* ---------------------------------------------------------------------- */
31936+
31937+static void sysrq_sb(struct super_block *sb)
31938+{
31939+ char *plevel;
31940+ struct au_sbinfo *sbinfo;
31941+ struct file *file;
8b6a4947
AM
31942+ struct hlist_bl_head *files;
31943+ struct hlist_bl_node *pos;
523b37e3 31944+ struct au_finfo *finfo;
43982f53 31945+ struct inode *i;
1facf9fc 31946+
31947+ plevel = au_plevel;
31948+ au_plevel = KERN_WARNING;
1facf9fc 31949+
4a4d8108 31950+ /* since we define pr_fmt, call printk directly */
c06a8ce3
AM
31951+#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
31952+
31953+ sbinfo = au_sbi(sb);
4a4d8108 31954+ printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
c06a8ce3 31955+ pr("superblock\n");
1facf9fc 31956+ au_dpri_sb(sb);
027c5e7a 31957+
43982f53 31958+#if 0 /* reserved */
027c5e7a
AM
31959+ do {
31960+ int err, i, j, ndentry;
31961+ struct au_dcsub_pages dpages;
31962+ struct au_dpage *dpage;
31963+
31964+ err = au_dpages_init(&dpages, GFP_ATOMIC);
31965+ if (unlikely(err))
31966+ break;
31967+ err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
31968+ if (!err)
31969+ for (i = 0; i < dpages.ndpage; i++) {
31970+ dpage = dpages.dpages + i;
31971+ ndentry = dpage->ndentry;
31972+ for (j = 0; j < ndentry; j++)
31973+ au_dpri_dentry(dpage->dentries[j]);
31974+ }
31975+ au_dpages_free(&dpages);
31976+ } while (0);
31977+#endif
31978+
43982f53
AM
31979+ pr("isolated inode\n");
31980+ spin_lock(&sb->s_inode_list_lock);
31981+ list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
31982+ spin_lock(&i->i_lock);
31983+ if (hlist_empty(&i->i_dentry))
31984+ au_dpri_inode(i);
31985+ spin_unlock(&i->i_lock);
027c5e7a 31986+ }
43982f53
AM
31987+ spin_unlock(&sb->s_inode_list_lock);
31988+
c06a8ce3 31989+ pr("files\n");
523b37e3 31990+ files = &au_sbi(sb)->si_files;
8b6a4947
AM
31991+ hlist_bl_lock(files);
31992+ hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
4a4d8108 31993+ umode_t mode;
076b876e 31994+
523b37e3 31995+ file = finfo->fi_file;
c06a8ce3 31996+ mode = file_inode(file)->i_mode;
38d290e6 31997+ if (!special_file(mode))
1facf9fc 31998+ au_dpri_file(file);
523b37e3 31999+ }
8b6a4947 32000+ hlist_bl_unlock(files);
c06a8ce3 32001+ pr("done\n");
1facf9fc 32002+
c06a8ce3 32003+#undef pr
1facf9fc 32004+ au_plevel = plevel;
1facf9fc 32005+}
32006+
32007+/* ---------------------------------------------------------------------- */
32008+
32009+/* module parameter */
32010+static char *aufs_sysrq_key = "a";
cd7a4cd9 32011+module_param_named(sysrq, aufs_sysrq_key, charp, 0444);
1facf9fc 32012+MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
32013+
0c5527e5 32014+static void au_sysrq(int key __maybe_unused)
1facf9fc 32015+{
1facf9fc 32016+ struct au_sbinfo *sbinfo;
8b6a4947 32017+ struct hlist_bl_node *pos;
1facf9fc 32018+
027c5e7a 32019+ lockdep_off();
53392da6 32020+ au_sbilist_lock();
8b6a4947 32021+ hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
1facf9fc 32022+ sysrq_sb(sbinfo->si_sb);
53392da6 32023+ au_sbilist_unlock();
027c5e7a 32024+ lockdep_on();
1facf9fc 32025+}
32026+
32027+static struct sysrq_key_op au_sysrq_op = {
32028+ .handler = au_sysrq,
32029+ .help_msg = "Aufs",
32030+ .action_msg = "Aufs",
32031+ .enable_mask = SYSRQ_ENABLE_DUMP
32032+};
32033+
32034+/* ---------------------------------------------------------------------- */
32035+
32036+int __init au_sysrq_init(void)
32037+{
32038+ int err;
32039+ char key;
32040+
32041+ err = -1;
32042+ key = *aufs_sysrq_key;
32043+ if ('a' <= key && key <= 'z')
32044+ err = register_sysrq_key(key, &au_sysrq_op);
32045+ if (unlikely(err))
4a4d8108 32046+ pr_err("err %d, sysrq=%c\n", err, key);
1facf9fc 32047+ return err;
32048+}
32049+
32050+void au_sysrq_fin(void)
32051+{
32052+ int err;
076b876e 32053+
1facf9fc 32054+ err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
32055+ if (unlikely(err))
4a4d8108 32056+ pr_err("err %d (ignored)\n", err);
1facf9fc 32057+}
7f207e10 32058diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
eca34b5c 32059--- /usr/share/empty/fs/aufs/vdir.c 1970-01-01 01:00:00.000000000 +0100
2255d0fe 32060+++ linux/fs/aufs/vdir.c 2022-12-17 09:21:34.799855195 +0100
eca801bf 32061@@ -0,0 +1,896 @@
cd7a4cd9 32062+// SPDX-License-Identifier: GPL-2.0
1facf9fc 32063+/*
0dcfbb52 32064+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 32065+ *
0dcfbb52 32066+ * This program is free software; you can redistribute it and/or modify
1facf9fc 32067+ * it under the terms of the GNU General Public License as published by
32068+ * the Free Software Foundation; either version 2 of the License, or
32069+ * (at your option) any later version.
dece6358
AM
32070+ *
32071+ * This program is distributed in the hope that it will be useful,
32072+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
32073+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32074+ * GNU General Public License for more details.
32075+ *
32076+ * You should have received a copy of the GNU General Public License
523b37e3 32077+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 32078+ */
32079+
32080+/*
32081+ * virtual or vertical directory
32082+ */
32083+
eca801bf 32084+#include <linux/iversion.h>
1facf9fc 32085+#include "aufs.h"
32086+
dece6358 32087+static unsigned int calc_size(int nlen)
1facf9fc 32088+{
dece6358 32089+ return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
1facf9fc 32090+}
32091+
32092+static int set_deblk_end(union au_vdir_deblk_p *p,
32093+ union au_vdir_deblk_p *deblk_end)
32094+{
32095+ if (calc_size(0) <= deblk_end->deblk - p->deblk) {
32096+ p->de->de_str.len = 0;
32097+ /* smp_mb(); */
32098+ return 0;
32099+ }
32100+ return -1; /* error */
32101+}
32102+
32103+/* returns true or false */
32104+static int is_deblk_end(union au_vdir_deblk_p *p,
32105+ union au_vdir_deblk_p *deblk_end)
32106+{
32107+ if (calc_size(0) <= deblk_end->deblk - p->deblk)
32108+ return !p->de->de_str.len;
32109+ return 1;
32110+}
32111+
32112+static unsigned char *last_deblk(struct au_vdir *vdir)
32113+{
32114+ return vdir->vd_deblk[vdir->vd_nblk - 1];
32115+}
32116+
32117+/* ---------------------------------------------------------------------- */
32118+
79b8bda9 32119+/* estimate the appropriate size for name hash table */
1308ab2a 32120+unsigned int au_rdhash_est(loff_t sz)
32121+{
32122+ unsigned int n;
32123+
32124+ n = UINT_MAX;
32125+ sz >>= 10;
32126+ if (sz < n)
32127+ n = sz;
32128+ if (sz < AUFS_RDHASH_DEF)
32129+ n = AUFS_RDHASH_DEF;
4a4d8108 32130+ /* pr_info("n %u\n", n); */
1308ab2a 32131+ return n;
32132+}
32133+
1facf9fc 32134+/*
32135+ * the allocated memory has to be freed by
dece6358 32136+ * au_nhash_wh_free() or au_nhash_de_free().
1facf9fc 32137+ */
dece6358 32138+int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
1facf9fc 32139+{
1facf9fc 32140+ struct hlist_head *head;
dece6358 32141+ unsigned int u;
076b876e 32142+ size_t sz;
1facf9fc 32143+
076b876e
AM
32144+ sz = sizeof(*nhash->nh_head) * num_hash;
32145+ head = kmalloc(sz, gfp);
dece6358
AM
32146+ if (head) {
32147+ nhash->nh_num = num_hash;
32148+ nhash->nh_head = head;
32149+ for (u = 0; u < num_hash; u++)
1facf9fc 32150+ INIT_HLIST_HEAD(head++);
dece6358 32151+ return 0; /* success */
1facf9fc 32152+ }
1facf9fc 32153+
dece6358 32154+ return -ENOMEM;
1facf9fc 32155+}
32156+
dece6358
AM
32157+static void nhash_count(struct hlist_head *head)
32158+{
43982f53 32159+#if 0 /* debugging */
dece6358
AM
32160+ unsigned long n;
32161+ struct hlist_node *pos;
32162+
32163+ n = 0;
32164+ hlist_for_each(pos, head)
32165+ n++;
4a4d8108 32166+ pr_info("%lu\n", n);
dece6358
AM
32167+#endif
32168+}
32169+
32170+static void au_nhash_wh_do_free(struct hlist_head *head)
1facf9fc 32171+{
c06a8ce3
AM
32172+ struct au_vdir_wh *pos;
32173+ struct hlist_node *node;
1facf9fc 32174+
c06a8ce3 32175+ hlist_for_each_entry_safe(pos, node, head, wh_hash)
9f237c51 32176+ au_kfree_rcu(pos);
1facf9fc 32177+}
32178+
dece6358 32179+static void au_nhash_de_do_free(struct hlist_head *head)
1facf9fc 32180+{
c06a8ce3
AM
32181+ struct au_vdir_dehstr *pos;
32182+ struct hlist_node *node;
1facf9fc 32183+
c06a8ce3 32184+ hlist_for_each_entry_safe(pos, node, head, hash)
1c60b727 32185+ au_cache_free_vdir_dehstr(pos);
1facf9fc 32186+}
32187+
dece6358
AM
32188+static void au_nhash_do_free(struct au_nhash *nhash,
32189+ void (*free)(struct hlist_head *head))
1facf9fc 32190+{
1308ab2a 32191+ unsigned int n;
1facf9fc 32192+ struct hlist_head *head;
1facf9fc 32193+
dece6358 32194+ n = nhash->nh_num;
1308ab2a 32195+ if (!n)
32196+ return;
32197+
dece6358 32198+ head = nhash->nh_head;
1308ab2a 32199+ while (n-- > 0) {
dece6358
AM
32200+ nhash_count(head);
32201+ free(head++);
1facf9fc 32202+ }
9f237c51 32203+ au_kfree_try_rcu(nhash->nh_head);
1facf9fc 32204+}
32205+
dece6358 32206+void au_nhash_wh_free(struct au_nhash *whlist)
1facf9fc 32207+{
dece6358
AM
32208+ au_nhash_do_free(whlist, au_nhash_wh_do_free);
32209+}
1facf9fc 32210+
dece6358
AM
32211+static void au_nhash_de_free(struct au_nhash *delist)
32212+{
32213+ au_nhash_do_free(delist, au_nhash_de_do_free);
1facf9fc 32214+}
32215+
32216+/* ---------------------------------------------------------------------- */
32217+
32218+int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
32219+ int limit)
32220+{
32221+ int num;
32222+ unsigned int u, n;
32223+ struct hlist_head *head;
c06a8ce3 32224+ struct au_vdir_wh *pos;
1facf9fc 32225+
32226+ num = 0;
32227+ n = whlist->nh_num;
32228+ head = whlist->nh_head;
1308ab2a 32229+ for (u = 0; u < n; u++, head++)
c06a8ce3
AM
32230+ hlist_for_each_entry(pos, head, wh_hash)
32231+ if (pos->wh_bindex == btgt && ++num > limit)
1facf9fc 32232+ return 1;
1facf9fc 32233+ return 0;
32234+}
32235+
32236+static struct hlist_head *au_name_hash(struct au_nhash *nhash,
dece6358 32237+ unsigned char *name,
1facf9fc 32238+ unsigned int len)
32239+{
dece6358
AM
32240+ unsigned int v;
32241+ /* const unsigned int magic_bit = 12; */
32242+
1308ab2a 32243+ AuDebugOn(!nhash->nh_num || !nhash->nh_head);
32244+
dece6358 32245+ v = 0;
f0c0a007
AM
32246+ if (len > 8)
32247+ len = 8;
dece6358
AM
32248+ while (len--)
32249+ v += *name++;
32250+ /* v = hash_long(v, magic_bit); */
32251+ v %= nhash->nh_num;
32252+ return nhash->nh_head + v;
32253+}
32254+
32255+static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
32256+ int nlen)
32257+{
32258+ return str->len == nlen && !memcmp(str->name, name, nlen);
1facf9fc 32259+}
32260+
32261+/* returns found or not */
dece6358 32262+int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
1facf9fc 32263+{
32264+ struct hlist_head *head;
c06a8ce3 32265+ struct au_vdir_wh *pos;
1facf9fc 32266+ struct au_vdir_destr *str;
32267+
dece6358 32268+ head = au_name_hash(whlist, name, nlen);
c06a8ce3
AM
32269+ hlist_for_each_entry(pos, head, wh_hash) {
32270+ str = &pos->wh_str;
1facf9fc 32271+ AuDbg("%.*s\n", str->len, str->name);
dece6358
AM
32272+ if (au_nhash_test_name(str, name, nlen))
32273+ return 1;
32274+ }
32275+ return 0;
32276+}
32277+
32278+/* returns found(true) or not */
32279+static int test_known(struct au_nhash *delist, char *name, int nlen)
32280+{
32281+ struct hlist_head *head;
c06a8ce3 32282+ struct au_vdir_dehstr *pos;
dece6358
AM
32283+ struct au_vdir_destr *str;
32284+
32285+ head = au_name_hash(delist, name, nlen);
c06a8ce3
AM
32286+ hlist_for_each_entry(pos, head, hash) {
32287+ str = pos->str;
dece6358
AM
32288+ AuDbg("%.*s\n", str->len, str->name);
32289+ if (au_nhash_test_name(str, name, nlen))
1facf9fc 32290+ return 1;
32291+ }
32292+ return 0;
32293+}
32294+
dece6358
AM
32295+static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
32296+ unsigned char d_type)
32297+{
32298+#ifdef CONFIG_AUFS_SHWH
32299+ wh->wh_ino = ino;
32300+ wh->wh_type = d_type;
32301+#endif
32302+}
32303+
32304+/* ---------------------------------------------------------------------- */
32305+
32306+int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
32307+ unsigned int d_type, aufs_bindex_t bindex,
32308+ unsigned char shwh)
1facf9fc 32309+{
32310+ int err;
32311+ struct au_vdir_destr *str;
32312+ struct au_vdir_wh *wh;
32313+
dece6358 32314+ AuDbg("%.*s\n", nlen, name);
1308ab2a 32315+ AuDebugOn(!whlist->nh_num || !whlist->nh_head);
32316+
1facf9fc 32317+ err = -ENOMEM;
dece6358 32318+ wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
1facf9fc 32319+ if (unlikely(!wh))
32320+ goto out;
32321+
32322+ err = 0;
32323+ wh->wh_bindex = bindex;
dece6358
AM
32324+ if (shwh)
32325+ au_shwh_init_wh(wh, ino, d_type);
1facf9fc 32326+ str = &wh->wh_str;
dece6358
AM
32327+ str->len = nlen;
32328+ memcpy(str->name, name, nlen);
32329+ hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
1facf9fc 32330+ /* smp_mb(); */
32331+
4f0767ce 32332+out:
1facf9fc 32333+ return err;
32334+}
32335+
1facf9fc 32336+static int append_deblk(struct au_vdir *vdir)
32337+{
32338+ int err;
dece6358 32339+ unsigned long ul;
1facf9fc 32340+ const unsigned int deblk_sz = vdir->vd_deblk_sz;
32341+ union au_vdir_deblk_p p, deblk_end;
32342+ unsigned char **o;
32343+
32344+ err = -ENOMEM;
e2f27e51
AM
32345+ o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
32346+ GFP_NOFS, /*may_shrink*/0);
1facf9fc 32347+ if (unlikely(!o))
32348+ goto out;
32349+
32350+ vdir->vd_deblk = o;
32351+ p.deblk = kmalloc(deblk_sz, GFP_NOFS);
32352+ if (p.deblk) {
32353+ ul = vdir->vd_nblk++;
32354+ vdir->vd_deblk[ul] = p.deblk;
32355+ vdir->vd_last.ul = ul;
32356+ vdir->vd_last.p.deblk = p.deblk;
32357+ deblk_end.deblk = p.deblk + deblk_sz;
32358+ err = set_deblk_end(&p, &deblk_end);
32359+ }
32360+
4f0767ce 32361+out:
1facf9fc 32362+ return err;
32363+}
32364+
dece6358
AM
32365+static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
32366+ unsigned int d_type, struct au_nhash *delist)
32367+{
32368+ int err;
32369+ unsigned int sz;
32370+ const unsigned int deblk_sz = vdir->vd_deblk_sz;
32371+ union au_vdir_deblk_p p, *room, deblk_end;
32372+ struct au_vdir_dehstr *dehstr;
32373+
32374+ p.deblk = last_deblk(vdir);
32375+ deblk_end.deblk = p.deblk + deblk_sz;
32376+ room = &vdir->vd_last.p;
32377+ AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
32378+ || !is_deblk_end(room, &deblk_end));
32379+
32380+ sz = calc_size(nlen);
32381+ if (unlikely(sz > deblk_end.deblk - room->deblk)) {
32382+ err = append_deblk(vdir);
32383+ if (unlikely(err))
32384+ goto out;
32385+
32386+ p.deblk = last_deblk(vdir);
32387+ deblk_end.deblk = p.deblk + deblk_sz;
32388+ /* smp_mb(); */
32389+ AuDebugOn(room->deblk != p.deblk);
32390+ }
32391+
32392+ err = -ENOMEM;
4a4d8108 32393+ dehstr = au_cache_alloc_vdir_dehstr();
dece6358
AM
32394+ if (unlikely(!dehstr))
32395+ goto out;
32396+
32397+ dehstr->str = &room->de->de_str;
32398+ hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
32399+ room->de->de_ino = ino;
32400+ room->de->de_type = d_type;
32401+ room->de->de_str.len = nlen;
32402+ memcpy(room->de->de_str.name, name, nlen);
32403+
32404+ err = 0;
32405+ room->deblk += sz;
32406+ if (unlikely(set_deblk_end(room, &deblk_end)))
32407+ err = append_deblk(vdir);
32408+ /* smp_mb(); */
32409+
4f0767ce 32410+out:
dece6358
AM
32411+ return err;
32412+}
32413+
32414+/* ---------------------------------------------------------------------- */
32415+
1c60b727 32416+void au_vdir_free(struct au_vdir *vdir)
dece6358
AM
32417+{
32418+ unsigned char **deblk;
32419+
32420+ deblk = vdir->vd_deblk;
1c60b727 32421+ while (vdir->vd_nblk--)
9f237c51
AM
32422+ au_kfree_try_rcu(*deblk++);
32423+ au_kfree_try_rcu(vdir->vd_deblk);
1c60b727 32424+ au_cache_free_vdir(vdir);
dece6358
AM
32425+}
32426+
1308ab2a 32427+static struct au_vdir *alloc_vdir(struct file *file)
1facf9fc 32428+{
32429+ struct au_vdir *vdir;
1308ab2a 32430+ struct super_block *sb;
1facf9fc 32431+ int err;
32432+
2000de60 32433+ sb = file->f_path.dentry->d_sb;
dece6358
AM
32434+ SiMustAnyLock(sb);
32435+
1facf9fc 32436+ err = -ENOMEM;
32437+ vdir = au_cache_alloc_vdir();
32438+ if (unlikely(!vdir))
32439+ goto out;
32440+
32441+ vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
32442+ if (unlikely(!vdir->vd_deblk))
32443+ goto out_free;
32444+
32445+ vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
1308ab2a 32446+ if (!vdir->vd_deblk_sz) {
79b8bda9 32447+ /* estimate the appropriate size for deblk */
1308ab2a 32448+ vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
4a4d8108 32449+ /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
1308ab2a 32450+ }
1facf9fc 32451+ vdir->vd_nblk = 0;
32452+ vdir->vd_version = 0;
32453+ vdir->vd_jiffy = 0;
32454+ err = append_deblk(vdir);
32455+ if (!err)
32456+ return vdir; /* success */
32457+
9f237c51 32458+ au_kfree_try_rcu(vdir->vd_deblk);
1facf9fc 32459+
4f0767ce 32460+out_free:
1c60b727 32461+ au_cache_free_vdir(vdir);
4f0767ce 32462+out:
1facf9fc 32463+ vdir = ERR_PTR(err);
32464+ return vdir;
32465+}
32466+
32467+static int reinit_vdir(struct au_vdir *vdir)
32468+{
32469+ int err;
32470+ union au_vdir_deblk_p p, deblk_end;
32471+
32472+ while (vdir->vd_nblk > 1) {
9f237c51 32473+ au_kfree_try_rcu(vdir->vd_deblk[vdir->vd_nblk - 1]);
1facf9fc 32474+ /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
32475+ vdir->vd_nblk--;
32476+ }
32477+ p.deblk = vdir->vd_deblk[0];
32478+ deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
32479+ err = set_deblk_end(&p, &deblk_end);
32480+ /* keep vd_dblk_sz */
32481+ vdir->vd_last.ul = 0;
32482+ vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32483+ vdir->vd_version = 0;
32484+ vdir->vd_jiffy = 0;
32485+ /* smp_mb(); */
32486+ return err;
32487+}
32488+
32489+/* ---------------------------------------------------------------------- */
32490+
1facf9fc 32491+#define AuFillVdir_CALLED 1
32492+#define AuFillVdir_WHABLE (1 << 1)
dece6358 32493+#define AuFillVdir_SHWH (1 << 2)
1facf9fc 32494+#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
7f207e10
AM
32495+#define au_fset_fillvdir(flags, name) \
32496+ do { (flags) |= AuFillVdir_##name; } while (0)
32497+#define au_fclr_fillvdir(flags, name) \
32498+ do { (flags) &= ~AuFillVdir_##name; } while (0)
1facf9fc 32499+
dece6358
AM
32500+#ifndef CONFIG_AUFS_SHWH
32501+#undef AuFillVdir_SHWH
32502+#define AuFillVdir_SHWH 0
32503+#endif
32504+
1facf9fc 32505+struct fillvdir_arg {
392086de 32506+ struct dir_context ctx;
1facf9fc 32507+ struct file *file;
32508+ struct au_vdir *vdir;
dece6358
AM
32509+ struct au_nhash delist;
32510+ struct au_nhash whlist;
1facf9fc 32511+ aufs_bindex_t bindex;
32512+ unsigned int flags;
32513+ int err;
32514+};
32515+
2255d0fe 32516+static bool fillvdir(struct dir_context *ctx, const char *__name, int nlen,
1facf9fc 32517+ loff_t offset __maybe_unused, u64 h_ino,
32518+ unsigned int d_type)
32519+{
392086de 32520+ struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
1facf9fc 32521+ char *name = (void *)__name;
32522+ struct super_block *sb;
1facf9fc 32523+ ino_t ino;
dece6358 32524+ const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
1facf9fc 32525+
1facf9fc 32526+ arg->err = 0;
2000de60 32527+ sb = arg->file->f_path.dentry->d_sb;
1facf9fc 32528+ au_fset_fillvdir(arg->flags, CALLED);
32529+ /* smp_mb(); */
dece6358 32530+ if (nlen <= AUFS_WH_PFX_LEN
1facf9fc 32531+ || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
dece6358
AM
32532+ if (test_known(&arg->delist, name, nlen)
32533+ || au_nhash_test_known_wh(&arg->whlist, name, nlen))
32534+ goto out; /* already exists or whiteouted */
1facf9fc 32535+
dece6358 32536+ arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
4a4d8108
AM
32537+ if (!arg->err) {
32538+ if (unlikely(nlen > AUFS_MAX_NAMELEN))
32539+ d_type = DT_UNKNOWN;
dece6358
AM
32540+ arg->err = append_de(arg->vdir, name, nlen, ino,
32541+ d_type, &arg->delist);
4a4d8108 32542+ }
1facf9fc 32543+ } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
32544+ name += AUFS_WH_PFX_LEN;
dece6358
AM
32545+ nlen -= AUFS_WH_PFX_LEN;
32546+ if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
32547+ goto out; /* already whiteouted */
1facf9fc 32548+
acd2b654 32549+ ino = 0; /* just to suppress a warning */
dece6358
AM
32550+ if (shwh)
32551+ arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
32552+ &ino);
4a4d8108
AM
32553+ if (!arg->err) {
32554+ if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
32555+ d_type = DT_UNKNOWN;
1facf9fc 32556+ arg->err = au_nhash_append_wh
dece6358
AM
32557+ (&arg->whlist, name, nlen, ino, d_type,
32558+ arg->bindex, shwh);
4a4d8108 32559+ }
1facf9fc 32560+ }
32561+
4f0767ce 32562+out:
1facf9fc 32563+ if (!arg->err)
32564+ arg->vdir->vd_jiffy = jiffies;
32565+ /* smp_mb(); */
32566+ AuTraceErr(arg->err);
2255d0fe 32567+ return !arg->err;
1facf9fc 32568+}
32569+
dece6358
AM
32570+static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
32571+ struct au_nhash *whlist, struct au_nhash *delist)
32572+{
32573+#ifdef CONFIG_AUFS_SHWH
32574+ int err;
32575+ unsigned int nh, u;
32576+ struct hlist_head *head;
c06a8ce3
AM
32577+ struct au_vdir_wh *pos;
32578+ struct hlist_node *n;
dece6358
AM
32579+ char *p, *o;
32580+ struct au_vdir_destr *destr;
32581+
32582+ AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
32583+
32584+ err = -ENOMEM;
537831f9 32585+ o = p = (void *)__get_free_page(GFP_NOFS);
dece6358
AM
32586+ if (unlikely(!p))
32587+ goto out;
32588+
32589+ err = 0;
32590+ nh = whlist->nh_num;
32591+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32592+ p += AUFS_WH_PFX_LEN;
32593+ for (u = 0; u < nh; u++) {
32594+ head = whlist->nh_head + u;
c06a8ce3
AM
32595+ hlist_for_each_entry_safe(pos, n, head, wh_hash) {
32596+ destr = &pos->wh_str;
dece6358
AM
32597+ memcpy(p, destr->name, destr->len);
32598+ err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
c06a8ce3 32599+ pos->wh_ino, pos->wh_type, delist);
dece6358
AM
32600+ if (unlikely(err))
32601+ break;
32602+ }
32603+ }
32604+
1c60b727 32605+ free_page((unsigned long)o);
dece6358 32606+
4f0767ce 32607+out:
dece6358
AM
32608+ AuTraceErr(err);
32609+ return err;
32610+#else
32611+ return 0;
32612+#endif
32613+}
32614+
1facf9fc 32615+static int au_do_read_vdir(struct fillvdir_arg *arg)
32616+{
32617+ int err;
dece6358 32618+ unsigned int rdhash;
1facf9fc 32619+ loff_t offset;
5afbbe0d 32620+ aufs_bindex_t bbot, bindex, btop;
dece6358 32621+ unsigned char shwh;
1facf9fc 32622+ struct file *hf, *file;
32623+ struct super_block *sb;
32624+
1facf9fc 32625+ file = arg->file;
2000de60 32626+ sb = file->f_path.dentry->d_sb;
dece6358
AM
32627+ SiMustAnyLock(sb);
32628+
32629+ rdhash = au_sbi(sb)->si_rdhash;
1308ab2a 32630+ if (!rdhash)
32631+ rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
dece6358
AM
32632+ err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
32633+ if (unlikely(err))
1facf9fc 32634+ goto out;
dece6358
AM
32635+ err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
32636+ if (unlikely(err))
1facf9fc 32637+ goto out_delist;
32638+
32639+ err = 0;
32640+ arg->flags = 0;
dece6358
AM
32641+ shwh = 0;
32642+ if (au_opt_test(au_mntflags(sb), SHWH)) {
32643+ shwh = 1;
32644+ au_fset_fillvdir(arg->flags, SHWH);
32645+ }
5afbbe0d
AM
32646+ btop = au_fbtop(file);
32647+ bbot = au_fbbot_dir(file);
32648+ for (bindex = btop; !err && bindex <= bbot; bindex++) {
4a4d8108 32649+ hf = au_hf_dir(file, bindex);
1facf9fc 32650+ if (!hf)
32651+ continue;
32652+
32653+ offset = vfsub_llseek(hf, 0, SEEK_SET);
32654+ err = offset;
32655+ if (unlikely(offset))
32656+ break;
32657+
32658+ arg->bindex = bindex;
32659+ au_fclr_fillvdir(arg->flags, WHABLE);
dece6358 32660+ if (shwh
5afbbe0d 32661+ || (bindex != bbot
dece6358 32662+ && au_br_whable(au_sbr_perm(sb, bindex))))
1facf9fc 32663+ au_fset_fillvdir(arg->flags, WHABLE);
32664+ do {
32665+ arg->err = 0;
32666+ au_fclr_fillvdir(arg->flags, CALLED);
32667+ /* smp_mb(); */
392086de 32668+ err = vfsub_iterate_dir(hf, &arg->ctx);
1facf9fc 32669+ if (err >= 0)
32670+ err = arg->err;
32671+ } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
392086de
AM
32672+
32673+ /*
32674+ * dir_relax() may be good for concurrency, but aufs should not
32675+ * use it since it will cause a lockdep problem.
32676+ */
1facf9fc 32677+ }
dece6358
AM
32678+
32679+ if (!err && shwh)
32680+ err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
32681+
32682+ au_nhash_wh_free(&arg->whlist);
1facf9fc 32683+
4f0767ce 32684+out_delist:
dece6358 32685+ au_nhash_de_free(&arg->delist);
4f0767ce 32686+out:
1facf9fc 32687+ return err;
32688+}
32689+
32690+static int read_vdir(struct file *file, int may_read)
32691+{
32692+ int err;
32693+ unsigned long expire;
32694+ unsigned char do_read;
392086de
AM
32695+ struct fillvdir_arg arg = {
32696+ .ctx = {
2000de60 32697+ .actor = fillvdir
392086de
AM
32698+ }
32699+ };
1facf9fc 32700+ struct inode *inode;
32701+ struct au_vdir *vdir, *allocated;
32702+
32703+ err = 0;
c06a8ce3 32704+ inode = file_inode(file);
1facf9fc 32705+ IMustLock(inode);
5afbbe0d 32706+ IiMustWriteLock(inode);
dece6358
AM
32707+ SiMustAnyLock(inode->i_sb);
32708+
1facf9fc 32709+ allocated = NULL;
32710+ do_read = 0;
32711+ expire = au_sbi(inode->i_sb)->si_rdcache;
32712+ vdir = au_ivdir(inode);
32713+ if (!vdir) {
32714+ do_read = 1;
1308ab2a 32715+ vdir = alloc_vdir(file);
1facf9fc 32716+ err = PTR_ERR(vdir);
32717+ if (IS_ERR(vdir))
32718+ goto out;
32719+ err = 0;
32720+ allocated = vdir;
32721+ } else if (may_read
be118d29 32722+ && (!inode_eq_iversion(inode, vdir->vd_version)
1facf9fc 32723+ || time_after(jiffies, vdir->vd_jiffy + expire))) {
32724+ do_read = 1;
32725+ err = reinit_vdir(vdir);
32726+ if (unlikely(err))
32727+ goto out;
32728+ }
32729+
32730+ if (!do_read)
32731+ return 0; /* success */
32732+
32733+ arg.file = file;
32734+ arg.vdir = vdir;
32735+ err = au_do_read_vdir(&arg);
32736+ if (!err) {
392086de 32737+ /* file->f_pos = 0; */ /* todo: ctx->pos? */
be118d29 32738+ vdir->vd_version = inode_query_iversion(inode);
1facf9fc 32739+ vdir->vd_last.ul = 0;
32740+ vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32741+ if (allocated)
32742+ au_set_ivdir(inode, allocated);
32743+ } else if (allocated)
1c60b727 32744+ au_vdir_free(allocated);
1facf9fc 32745+
4f0767ce 32746+out:
1facf9fc 32747+ return err;
32748+}
32749+
32750+static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
32751+{
32752+ int err, rerr;
32753+ unsigned long ul, n;
32754+ const unsigned int deblk_sz = src->vd_deblk_sz;
32755+
32756+ AuDebugOn(tgt->vd_nblk != 1);
32757+
32758+ err = -ENOMEM;
32759+ if (tgt->vd_nblk < src->vd_nblk) {
32760+ unsigned char **p;
32761+
e2f27e51
AM
32762+ p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
32763+ GFP_NOFS, /*may_shrink*/0);
1facf9fc 32764+ if (unlikely(!p))
32765+ goto out;
32766+ tgt->vd_deblk = p;
32767+ }
32768+
1308ab2a 32769+ if (tgt->vd_deblk_sz != deblk_sz) {
32770+ unsigned char *p;
32771+
32772+ tgt->vd_deblk_sz = deblk_sz;
e2f27e51
AM
32773+ p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
32774+ /*may_shrink*/1);
1308ab2a 32775+ if (unlikely(!p))
32776+ goto out;
32777+ tgt->vd_deblk[0] = p;
32778+ }
1facf9fc 32779+ memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
1facf9fc 32780+ tgt->vd_version = src->vd_version;
32781+ tgt->vd_jiffy = src->vd_jiffy;
32782+
32783+ n = src->vd_nblk;
32784+ for (ul = 1; ul < n; ul++) {
dece6358
AM
32785+ tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
32786+ GFP_NOFS);
32787+ if (unlikely(!tgt->vd_deblk[ul]))
1facf9fc 32788+ goto out;
1308ab2a 32789+ tgt->vd_nblk++;
1facf9fc 32790+ }
1308ab2a 32791+ tgt->vd_nblk = n;
32792+ tgt->vd_last.ul = tgt->vd_last.ul;
32793+ tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
32794+ tgt->vd_last.p.deblk += src->vd_last.p.deblk
32795+ - src->vd_deblk[src->vd_last.ul];
1facf9fc 32796+ /* smp_mb(); */
32797+ return 0; /* success */
32798+
4f0767ce 32799+out:
1facf9fc 32800+ rerr = reinit_vdir(tgt);
32801+ BUG_ON(rerr);
32802+ return err;
32803+}
32804+
32805+int au_vdir_init(struct file *file)
32806+{
32807+ int err;
32808+ struct inode *inode;
32809+ struct au_vdir *vdir_cache, *allocated;
32810+
392086de 32811+ /* test file->f_pos here instead of ctx->pos */
1facf9fc 32812+ err = read_vdir(file, !file->f_pos);
32813+ if (unlikely(err))
32814+ goto out;
32815+
32816+ allocated = NULL;
32817+ vdir_cache = au_fvdir_cache(file);
32818+ if (!vdir_cache) {
1308ab2a 32819+ vdir_cache = alloc_vdir(file);
1facf9fc 32820+ err = PTR_ERR(vdir_cache);
32821+ if (IS_ERR(vdir_cache))
32822+ goto out;
32823+ allocated = vdir_cache;
32824+ } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
392086de 32825+ /* test file->f_pos here instead of ctx->pos */
1facf9fc 32826+ err = reinit_vdir(vdir_cache);
32827+ if (unlikely(err))
32828+ goto out;
32829+ } else
32830+ return 0; /* success */
32831+
c06a8ce3 32832+ inode = file_inode(file);
1facf9fc 32833+ err = copy_vdir(vdir_cache, au_ivdir(inode));
32834+ if (!err) {
be118d29 32835+ file->f_version = inode_query_iversion(inode);
1facf9fc 32836+ if (allocated)
32837+ au_set_fvdir_cache(file, allocated);
32838+ } else if (allocated)
1c60b727 32839+ au_vdir_free(allocated);
1facf9fc 32840+
4f0767ce 32841+out:
1facf9fc 32842+ return err;
32843+}
32844+
32845+static loff_t calc_offset(struct au_vdir *vdir)
32846+{
32847+ loff_t offset;
32848+ union au_vdir_deblk_p p;
32849+
32850+ p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
32851+ offset = vdir->vd_last.p.deblk - p.deblk;
32852+ offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
32853+ return offset;
32854+}
32855+
32856+/* returns true or false */
392086de 32857+static int seek_vdir(struct file *file, struct dir_context *ctx)
1facf9fc 32858+{
32859+ int valid;
32860+ unsigned int deblk_sz;
32861+ unsigned long ul, n;
32862+ loff_t offset;
32863+ union au_vdir_deblk_p p, deblk_end;
32864+ struct au_vdir *vdir_cache;
32865+
32866+ valid = 1;
32867+ vdir_cache = au_fvdir_cache(file);
32868+ offset = calc_offset(vdir_cache);
32869+ AuDbg("offset %lld\n", offset);
392086de 32870+ if (ctx->pos == offset)
1facf9fc 32871+ goto out;
32872+
32873+ vdir_cache->vd_last.ul = 0;
32874+ vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
392086de 32875+ if (!ctx->pos)
1facf9fc 32876+ goto out;
32877+
32878+ valid = 0;
32879+ deblk_sz = vdir_cache->vd_deblk_sz;
392086de 32880+ ul = div64_u64(ctx->pos, deblk_sz);
1facf9fc 32881+ AuDbg("ul %lu\n", ul);
32882+ if (ul >= vdir_cache->vd_nblk)
32883+ goto out;
32884+
32885+ n = vdir_cache->vd_nblk;
32886+ for (; ul < n; ul++) {
32887+ p.deblk = vdir_cache->vd_deblk[ul];
32888+ deblk_end.deblk = p.deblk + deblk_sz;
32889+ offset = ul;
32890+ offset *= deblk_sz;
392086de 32891+ while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
1facf9fc 32892+ unsigned int l;
32893+
32894+ l = calc_size(p.de->de_str.len);
32895+ offset += l;
32896+ p.deblk += l;
32897+ }
32898+ if (!is_deblk_end(&p, &deblk_end)) {
32899+ valid = 1;
32900+ vdir_cache->vd_last.ul = ul;
32901+ vdir_cache->vd_last.p = p;
32902+ break;
32903+ }
32904+ }
32905+
4f0767ce 32906+out:
1facf9fc 32907+ /* smp_mb(); */
b00004a5
AM
32908+ if (!valid)
32909+ AuDbg("valid %d\n", !valid);
1facf9fc 32910+ return valid;
32911+}
32912+
392086de 32913+int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
1facf9fc 32914+{
1facf9fc 32915+ unsigned int l, deblk_sz;
32916+ union au_vdir_deblk_p deblk_end;
32917+ struct au_vdir *vdir_cache;
32918+ struct au_vdir_de *de;
32919+
392086de 32920+ if (!seek_vdir(file, ctx))
1facf9fc 32921+ return 0;
32922+
acd2b654 32923+ vdir_cache = au_fvdir_cache(file);
1facf9fc 32924+ deblk_sz = vdir_cache->vd_deblk_sz;
32925+ while (1) {
32926+ deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32927+ deblk_end.deblk += deblk_sz;
32928+ while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
32929+ de = vdir_cache->vd_last.p.de;
32930+ AuDbg("%.*s, off%lld, i%lu, dt%d\n",
392086de 32931+ de->de_str.len, de->de_str.name, ctx->pos,
1facf9fc 32932+ (unsigned long)de->de_ino, de->de_type);
392086de
AM
32933+ if (unlikely(!dir_emit(ctx, de->de_str.name,
32934+ de->de_str.len, de->de_ino,
32935+ de->de_type))) {
1facf9fc 32936+ /* todo: ignore the error caused by udba? */
32937+ /* return err; */
32938+ return 0;
32939+ }
32940+
32941+ l = calc_size(de->de_str.len);
32942+ vdir_cache->vd_last.p.deblk += l;
392086de 32943+ ctx->pos += l;
1facf9fc 32944+ }
32945+ if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
32946+ vdir_cache->vd_last.ul++;
32947+ vdir_cache->vd_last.p.deblk
32948+ = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
392086de 32949+ ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
1facf9fc 32950+ continue;
32951+ }
32952+ break;
32953+ }
32954+
32955+ /* smp_mb(); */
32956+ return 0;
32957+}
7f207e10 32958diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
eca34b5c 32959--- /usr/share/empty/fs/aufs/vfsub.c 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 32960+++ linux/fs/aufs/vfsub.c 2022-11-05 23:02:18.969222617 +0100
f4d37d76 32961@@ -0,0 +1,918 @@
cd7a4cd9 32962+// SPDX-License-Identifier: GPL-2.0
1facf9fc 32963+/*
0dcfbb52 32964+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 32965+ *
0dcfbb52 32966+ * This program is free software; you can redistribute it and/or modify
1facf9fc 32967+ * it under the terms of the GNU General Public License as published by
32968+ * the Free Software Foundation; either version 2 of the License, or
32969+ * (at your option) any later version.
dece6358
AM
32970+ *
32971+ * This program is distributed in the hope that it will be useful,
32972+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
32973+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32974+ * GNU General Public License for more details.
32975+ *
32976+ * You should have received a copy of the GNU General Public License
523b37e3 32977+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 32978+ */
32979+
32980+/*
32981+ * sub-routines for VFS
32982+ */
32983+
8b6a4947 32984+#include <linux/mnt_namespace.h>
8cdd5066 32985+#include <linux/nsproxy.h>
dece6358
AM
32986+#include <linux/security.h>
32987+#include <linux/splice.h>
1facf9fc 32988+#include "aufs.h"
32989+
8cdd5066
JR
32990+#ifdef CONFIG_AUFS_BR_FUSE
32991+int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
32992+{
8cdd5066
JR
32993+ if (!au_test_fuse(h_sb) || !au_userns)
32994+ return 0;
32995+
8b6a4947 32996+ return is_current_mnt_ns(mnt) ? 0 : -EACCES;
8cdd5066
JR
32997+}
32998+#endif
32999+
cd233405 33000+int vfsub_sync_filesystem(struct super_block *h_sb)
a2654f78
AM
33001+{
33002+ int err;
33003+
33004+ lockdep_off();
33005+ down_read(&h_sb->s_umount);
cd233405 33006+ err = sync_filesystem(h_sb);
a2654f78
AM
33007+ up_read(&h_sb->s_umount);
33008+ lockdep_on();
33009+
33010+ return err;
33011+}
33012+
8cdd5066
JR
33013+/* ---------------------------------------------------------------------- */
33014+
1facf9fc 33015+int vfsub_update_h_iattr(struct path *h_path, int *did)
33016+{
33017+ int err;
33018+ struct kstat st;
33019+ struct super_block *h_sb;
33020+
758e9dad
AM
33021+ /*
33022+ * Always needs h_path->mnt for LSM or FUSE branch.
33023+ */
33024+ AuDebugOn(!h_path->mnt);
33025+
1facf9fc 33026+ /* for remote fs, leave work for its getattr or d_revalidate */
33027+ /* for bad i_attr fs, handle them in aufs_getattr() */
33028+ /* still some fs may acquire i_mutex. we need to skip them */
33029+ err = 0;
33030+ if (!did)
33031+ did = &err;
33032+ h_sb = h_path->dentry->d_sb;
33033+ *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
33034+ if (*did)
521ced18 33035+ err = vfsub_getattr(h_path, &st);
1facf9fc 33036+
33037+ return err;
33038+}
33039+
33040+/* ---------------------------------------------------------------------- */
33041+
4a4d8108 33042+struct file *vfsub_dentry_open(struct path *path, int flags)
1308ab2a 33043+{
42b5c33a 33044+ return dentry_open(path, flags /* | __FMODE_NONOTIFY */,
7f207e10 33045+ current_cred());
1308ab2a 33046+}
33047+
1facf9fc 33048+struct file *vfsub_filp_open(const char *path, int oflags, int mode)
33049+{
33050+ struct file *file;
33051+
2cbb1c4b 33052+ lockdep_off();
7f207e10 33053+ file = filp_open(path,
2cbb1c4b 33054+ oflags /* | __FMODE_NONOTIFY */,
7f207e10 33055+ mode);
2cbb1c4b 33056+ lockdep_on();
1facf9fc 33057+ if (IS_ERR(file))
33058+ goto out;
33059+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33060+
4f0767ce 33061+out:
1facf9fc 33062+ return file;
33063+}
33064+
b912730e
AM
33065+/*
33066+ * Ideally this function should call VFS:do_last() in order to keep all its
33067+ * checkings. But it is very hard for aufs to regenerate several VFS internal
33068+ * structure such as nameidata. This is a second (or third) best approach.
33069+ * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
33070+ */
33071+int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
acd2b654 33072+ struct vfsub_aopen_args *args)
b912730e
AM
33073+{
33074+ int err;
acd2b654 33075+ struct au_branch *br = args->br;
b912730e
AM
33076+ struct file *file = args->file;
33077+ /* copied from linux/fs/namei.c:atomic_open() */
33078+ struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
33079+
33080+ IMustLock(dir);
33081+ AuDebugOn(!dir->i_op->atomic_open);
33082+
33083+ err = au_br_test_oflag(args->open_flag, br);
33084+ if (unlikely(err))
33085+ goto out;
33086+
acd2b654
AM
33087+ au_lcnt_inc(&br->br_nfiles);
33088+ file->f_path.dentry = DENTRY_NOT_SET;
33089+ file->f_path.mnt = au_br_mnt(br);
33090+ AuDbg("%ps\n", dir->i_op->atomic_open);
b912730e 33091+ err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
acd2b654
AM
33092+ args->create_mode);
33093+ if (unlikely(err < 0)) {
33094+ au_lcnt_dec(&br->br_nfiles);
b912730e 33095+ goto out;
acd2b654 33096+ }
b912730e 33097+
acd2b654
AM
33098+ /* temporary workaround for nfsv4 branch */
33099+ if (au_test_nfs(dir->i_sb))
33100+ nfs_mark_for_revalidate(dir);
b912730e 33101+
acd2b654
AM
33102+ if (file->f_mode & FMODE_CREATED)
33103+ fsnotify_create(dir, dentry);
33104+ if (!(file->f_mode & FMODE_OPENED)) {
33105+ au_lcnt_dec(&br->br_nfiles);
33106+ goto out;
b912730e
AM
33107+ }
33108+
acd2b654
AM
33109+ /* todo: call VFS:may_open() here */
33110+ /* todo: ima_file_check() too? */
33111+ if (!err && (args->open_flag & __FMODE_EXEC))
33112+ err = deny_write_access(file);
33113+ if (!err)
33114+ fsnotify_open(file);
33115+ else
33116+ au_lcnt_dec(&br->br_nfiles);
33117+ /* note that the file is created and still opened */
b912730e
AM
33118+
33119+out:
33120+ return err;
33121+}
33122+
1facf9fc 33123+int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
33124+{
33125+ int err;
33126+
1facf9fc 33127+ err = kern_path(name, flags, path);
5527c038 33128+ if (!err && d_is_positive(path->dentry))
1facf9fc 33129+ vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33130+ return err;
33131+}
33132+
febd17d6 33133+struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
758e9dad 33134+ struct path *ppath, int len)
febd17d6 33135+{
758e9dad 33136+ struct path path;
febd17d6 33137+
758e9dad 33138+ path.dentry = lookup_one_len_unlocked(name, ppath->dentry, len);
febd17d6
JR
33139+ if (IS_ERR(path.dentry))
33140+ goto out;
758e9dad
AM
33141+ if (d_is_positive(path.dentry)) {
33142+ path.mnt = ppath->mnt;
febd17d6 33143+ vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
758e9dad 33144+ }
febd17d6
JR
33145+
33146+out:
33147+ AuTraceErrPtr(path.dentry);
33148+ return path.dentry;
33149+}
33150+
758e9dad 33151+struct dentry *vfsub_lookup_one_len(const char *name, struct path *ppath,
1facf9fc 33152+ int len)
33153+{
758e9dad 33154+ struct path path;
1facf9fc 33155+
1308ab2a 33156+ /* VFS checks it too, but by WARN_ON_ONCE() */
758e9dad 33157+ IMustLock(d_inode(ppath->dentry));
1facf9fc 33158+
758e9dad 33159+ path.dentry = lookup_one_len(name, ppath->dentry, len);
1facf9fc 33160+ if (IS_ERR(path.dentry))
33161+ goto out;
758e9dad
AM
33162+ if (d_is_positive(path.dentry)) {
33163+ path.mnt = ppath->mnt;
1facf9fc 33164+ vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
758e9dad 33165+ }
1facf9fc 33166+
4f0767ce 33167+out:
4a4d8108 33168+ AuTraceErrPtr(path.dentry);
1facf9fc 33169+ return path.dentry;
33170+}
33171+
b4510431 33172+void vfsub_call_lkup_one(void *args)
2cbb1c4b 33173+{
b4510431 33174+ struct vfsub_lkup_one_args *a = args;
758e9dad 33175+ *a->errp = vfsub_lkup_one(a->name, a->ppath);
2cbb1c4b
JR
33176+}
33177+
1facf9fc 33178+/* ---------------------------------------------------------------------- */
33179+
33180+struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33181+ struct dentry *d2, struct au_hinode *hdir2)
33182+{
33183+ struct dentry *d;
33184+
2cbb1c4b 33185+ lockdep_off();
1facf9fc 33186+ d = lock_rename(d1, d2);
2cbb1c4b 33187+ lockdep_on();
4a4d8108 33188+ au_hn_suspend(hdir1);
1facf9fc 33189+ if (hdir1 != hdir2)
4a4d8108 33190+ au_hn_suspend(hdir2);
1facf9fc 33191+
33192+ return d;
33193+}
33194+
33195+void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33196+ struct dentry *d2, struct au_hinode *hdir2)
33197+{
4a4d8108 33198+ au_hn_resume(hdir1);
1facf9fc 33199+ if (hdir1 != hdir2)
4a4d8108 33200+ au_hn_resume(hdir2);
2cbb1c4b 33201+ lockdep_off();
1facf9fc 33202+ unlock_rename(d1, d2);
2cbb1c4b 33203+ lockdep_on();
1facf9fc 33204+}
33205+
33206+/* ---------------------------------------------------------------------- */
33207+
b4510431 33208+int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
1facf9fc 33209+{
33210+ int err;
33211+ struct dentry *d;
0b2a12c6 33212+ struct user_namespace *userns;
1facf9fc 33213+
33214+ IMustLock(dir);
33215+
33216+ d = path->dentry;
33217+ path->dentry = d->d_parent;
b752ccd1 33218+ err = security_path_mknod(path, d, mode, 0);
1facf9fc 33219+ path->dentry = d;
33220+ if (unlikely(err))
33221+ goto out;
0b2a12c6 33222+ userns = mnt_user_ns(path->mnt);
1facf9fc 33223+
c1595e42 33224+ lockdep_off();
0b2a12c6 33225+ err = vfs_create(userns, dir, path->dentry, mode, want_excl);
c1595e42 33226+ lockdep_on();
1facf9fc 33227+ if (!err) {
33228+ struct path tmp = *path;
33229+ int did;
33230+
33231+ vfsub_update_h_iattr(&tmp, &did);
33232+ if (did) {
33233+ tmp.dentry = path->dentry->d_parent;
33234+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33235+ }
33236+ /*ignore*/
33237+ }
33238+
4f0767ce 33239+out:
1facf9fc 33240+ return err;
33241+}
33242+
33243+int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
33244+{
33245+ int err;
33246+ struct dentry *d;
0b2a12c6 33247+ struct user_namespace *userns;
1facf9fc 33248+
33249+ IMustLock(dir);
33250+
33251+ d = path->dentry;
33252+ path->dentry = d->d_parent;
b752ccd1 33253+ err = security_path_symlink(path, d, symname);
1facf9fc 33254+ path->dentry = d;
33255+ if (unlikely(err))
33256+ goto out;
0b2a12c6 33257+ userns = mnt_user_ns(path->mnt);
1facf9fc 33258+
c1595e42 33259+ lockdep_off();
0b2a12c6 33260+ err = vfs_symlink(userns, dir, path->dentry, symname);
c1595e42 33261+ lockdep_on();
1facf9fc 33262+ if (!err) {
33263+ struct path tmp = *path;
33264+ int did;
33265+
33266+ vfsub_update_h_iattr(&tmp, &did);
33267+ if (did) {
33268+ tmp.dentry = path->dentry->d_parent;
33269+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33270+ }
33271+ /*ignore*/
33272+ }
33273+
4f0767ce 33274+out:
1facf9fc 33275+ return err;
33276+}
33277+
33278+int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
33279+{
33280+ int err;
33281+ struct dentry *d;
0b2a12c6 33282+ struct user_namespace *userns;
1facf9fc 33283+
33284+ IMustLock(dir);
33285+
33286+ d = path->dentry;
33287+ path->dentry = d->d_parent;
027c5e7a 33288+ err = security_path_mknod(path, d, mode, new_encode_dev(dev));
1facf9fc 33289+ path->dentry = d;
33290+ if (unlikely(err))
33291+ goto out;
0b2a12c6 33292+ userns = mnt_user_ns(path->mnt);
1facf9fc 33293+
c1595e42 33294+ lockdep_off();
0b2a12c6 33295+ err = vfs_mknod(userns, dir, path->dentry, mode, dev);
c1595e42 33296+ lockdep_on();
1facf9fc 33297+ if (!err) {
33298+ struct path tmp = *path;
33299+ int did;
33300+
33301+ vfsub_update_h_iattr(&tmp, &did);
33302+ if (did) {
33303+ tmp.dentry = path->dentry->d_parent;
33304+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33305+ }
33306+ /*ignore*/
33307+ }
33308+
4f0767ce 33309+out:
1facf9fc 33310+ return err;
33311+}
33312+
33313+static int au_test_nlink(struct inode *inode)
33314+{
33315+ const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
33316+
33317+ if (!au_test_fs_no_limit_nlink(inode->i_sb)
33318+ || inode->i_nlink < link_max)
33319+ return 0;
33320+ return -EMLINK;
33321+}
33322+
523b37e3
AM
33323+int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
33324+ struct inode **delegated_inode)
1facf9fc 33325+{
33326+ int err;
33327+ struct dentry *d;
0b2a12c6 33328+ struct user_namespace *userns;
1facf9fc 33329+
33330+ IMustLock(dir);
33331+
5527c038 33332+ err = au_test_nlink(d_inode(src_dentry));
1facf9fc 33333+ if (unlikely(err))
33334+ return err;
33335+
b4510431 33336+ /* we don't call may_linkat() */
1facf9fc 33337+ d = path->dentry;
33338+ path->dentry = d->d_parent;
b752ccd1 33339+ err = security_path_link(src_dentry, path, d);
1facf9fc 33340+ path->dentry = d;
33341+ if (unlikely(err))
33342+ goto out;
0b2a12c6 33343+ userns = mnt_user_ns(path->mnt);
1facf9fc 33344+
2cbb1c4b 33345+ lockdep_off();
0b2a12c6 33346+ err = vfs_link(src_dentry, userns, dir, path->dentry, delegated_inode);
2cbb1c4b 33347+ lockdep_on();
1facf9fc 33348+ if (!err) {
33349+ struct path tmp = *path;
33350+ int did;
33351+
33352+ /* fuse has different memory inode for the same inumber */
33353+ vfsub_update_h_iattr(&tmp, &did);
33354+ if (did) {
33355+ tmp.dentry = path->dentry->d_parent;
33356+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33357+ tmp.dentry = src_dentry;
33358+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33359+ }
33360+ /*ignore*/
33361+ }
33362+
4f0767ce 33363+out:
1facf9fc 33364+ return err;
33365+}
33366+
33367+int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
523b37e3 33368+ struct inode *dir, struct path *path,
f2c43d5f 33369+ struct inode **delegated_inode, unsigned int flags)
1facf9fc 33370+{
33371+ int err;
0b2a12c6 33372+ struct renamedata rd;
1facf9fc 33373+ struct path tmp = {
33374+ .mnt = path->mnt
33375+ };
33376+ struct dentry *d;
33377+
33378+ IMustLock(dir);
33379+ IMustLock(src_dir);
33380+
33381+ d = path->dentry;
33382+ path->dentry = d->d_parent;
33383+ tmp.dentry = src_dentry->d_parent;
38d290e6 33384+ err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
1facf9fc 33385+ path->dentry = d;
33386+ if (unlikely(err))
33387+ goto out;
33388+
0b2a12c6
JR
33389+ rd.old_mnt_userns = mnt_user_ns(path->mnt);
33390+ rd.old_dir = src_dir;
33391+ rd.old_dentry = src_dentry;
33392+ rd.new_mnt_userns = rd.old_mnt_userns;
33393+ rd.new_dir = dir;
33394+ rd.new_dentry = path->dentry;
33395+ rd.delegated_inode = delegated_inode;
33396+ rd.flags = flags;
2cbb1c4b 33397+ lockdep_off();
0b2a12c6 33398+ err = vfs_rename(&rd);
2cbb1c4b 33399+ lockdep_on();
1facf9fc 33400+ if (!err) {
33401+ int did;
33402+
33403+ tmp.dentry = d->d_parent;
33404+ vfsub_update_h_iattr(&tmp, &did);
33405+ if (did) {
33406+ tmp.dentry = src_dentry;
33407+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33408+ tmp.dentry = src_dentry->d_parent;
33409+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33410+ }
33411+ /*ignore*/
33412+ }
33413+
4f0767ce 33414+out:
1facf9fc 33415+ return err;
33416+}
33417+
33418+int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
33419+{
33420+ int err;
33421+ struct dentry *d;
0b2a12c6 33422+ struct user_namespace *userns;
1facf9fc 33423+
33424+ IMustLock(dir);
33425+
33426+ d = path->dentry;
33427+ path->dentry = d->d_parent;
b752ccd1 33428+ err = security_path_mkdir(path, d, mode);
1facf9fc 33429+ path->dentry = d;
33430+ if (unlikely(err))
33431+ goto out;
0b2a12c6 33432+ userns = mnt_user_ns(path->mnt);
1facf9fc 33433+
c1595e42 33434+ lockdep_off();
0b2a12c6 33435+ err = vfs_mkdir(userns, dir, path->dentry, mode);
c1595e42 33436+ lockdep_on();
1facf9fc 33437+ if (!err) {
33438+ struct path tmp = *path;
33439+ int did;
33440+
33441+ vfsub_update_h_iattr(&tmp, &did);
33442+ if (did) {
33443+ tmp.dentry = path->dentry->d_parent;
33444+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33445+ }
33446+ /*ignore*/
33447+ }
33448+
4f0767ce 33449+out:
1facf9fc 33450+ return err;
33451+}
33452+
33453+int vfsub_rmdir(struct inode *dir, struct path *path)
33454+{
33455+ int err;
33456+ struct dentry *d;
0b2a12c6 33457+ struct user_namespace *userns;
1facf9fc 33458+
33459+ IMustLock(dir);
33460+
33461+ d = path->dentry;
33462+ path->dentry = d->d_parent;
b752ccd1 33463+ err = security_path_rmdir(path, d);
1facf9fc 33464+ path->dentry = d;
33465+ if (unlikely(err))
33466+ goto out;
0b2a12c6 33467+ userns = mnt_user_ns(path->mnt);
1facf9fc 33468+
2cbb1c4b 33469+ lockdep_off();
0b2a12c6 33470+ err = vfs_rmdir(userns, dir, path->dentry);
2cbb1c4b 33471+ lockdep_on();
1facf9fc 33472+ if (!err) {
33473+ struct path tmp = {
33474+ .dentry = path->dentry->d_parent,
33475+ .mnt = path->mnt
33476+ };
33477+
33478+ vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33479+ }
33480+
4f0767ce 33481+out:
1facf9fc 33482+ return err;
33483+}
33484+
33485+/* ---------------------------------------------------------------------- */
33486+
9dbd164d 33487+/* todo: support mmap_sem? */
1facf9fc 33488+ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33489+ loff_t *ppos)
33490+{
33491+ ssize_t err;
33492+
2cbb1c4b 33493+ lockdep_off();
1facf9fc 33494+ err = vfs_read(file, ubuf, count, ppos);
2cbb1c4b 33495+ lockdep_on();
1facf9fc 33496+ if (err >= 0)
33497+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33498+ return err;
33499+}
33500+
1facf9fc 33501+ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33502+ loff_t *ppos)
33503+{
33504+ ssize_t err;
1facf9fc 33505+
e37dd06a
AM
33506+ lockdep_off();
33507+ err = kernel_read(file, kbuf, count, ppos);
33508+ lockdep_on();
33509+ AuTraceErr(err);
33510+ if (err >= 0)
33511+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
1facf9fc 33512+ return err;
33513+}
33514+
33515+ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33516+ loff_t *ppos)
33517+{
33518+ ssize_t err;
33519+
2cbb1c4b 33520+ lockdep_off();
1facf9fc 33521+ err = vfs_write(file, ubuf, count, ppos);
2cbb1c4b 33522+ lockdep_on();
1facf9fc 33523+ if (err >= 0)
33524+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33525+ return err;
33526+}
33527+
33528+ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
33529+{
33530+ ssize_t err;
1facf9fc 33531+
e37dd06a
AM
33532+ lockdep_off();
33533+ err = kernel_write(file, kbuf, count, ppos);
33534+ lockdep_on();
33535+ if (err >= 0)
33536+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
1facf9fc 33537+ return err;
33538+}
33539+
4a4d8108
AM
33540+int vfsub_flush(struct file *file, fl_owner_t id)
33541+{
33542+ int err;
33543+
33544+ err = 0;
523b37e3 33545+ if (file->f_op->flush) {
2000de60 33546+ if (!au_test_nfs(file->f_path.dentry->d_sb))
2cbb1c4b
JR
33547+ err = file->f_op->flush(file, id);
33548+ else {
33549+ lockdep_off();
33550+ err = file->f_op->flush(file, id);
33551+ lockdep_on();
33552+ }
4a4d8108
AM
33553+ if (!err)
33554+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
33555+ /*ignore*/
33556+ }
33557+ return err;
33558+}
33559+
392086de 33560+int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
1facf9fc 33561+{
33562+ int err;
33563+
062440b3 33564+ AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
392086de 33565+
2cbb1c4b 33566+ lockdep_off();
392086de 33567+ err = iterate_dir(file, ctx);
2cbb1c4b 33568+ lockdep_on();
1facf9fc 33569+ if (err >= 0)
33570+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
1c60b727 33571+
1facf9fc 33572+ return err;
33573+}
33574+
33575+long vfsub_splice_to(struct file *in, loff_t *ppos,
33576+ struct pipe_inode_info *pipe, size_t len,
33577+ unsigned int flags)
33578+{
33579+ long err;
33580+
2cbb1c4b 33581+ lockdep_off();
0fc653ad 33582+ err = do_splice_to(in, ppos, pipe, len, flags);
2cbb1c4b 33583+ lockdep_on();
4a4d8108 33584+ file_accessed(in);
1facf9fc 33585+ if (err >= 0)
33586+ vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
33587+ return err;
33588+}
33589+
33590+long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33591+ loff_t *ppos, size_t len, unsigned int flags)
33592+{
33593+ long err;
33594+
2cbb1c4b 33595+ lockdep_off();
0fc653ad 33596+ err = do_splice_from(pipe, out, ppos, len, flags);
2cbb1c4b 33597+ lockdep_on();
1facf9fc 33598+ if (err >= 0)
33599+ vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
33600+ return err;
33601+}
33602+
53392da6
AM
33603+int vfsub_fsync(struct file *file, struct path *path, int datasync)
33604+{
33605+ int err;
33606+
33607+ /* file can be NULL */
33608+ lockdep_off();
33609+ err = vfs_fsync(file, datasync);
33610+ lockdep_on();
33611+ if (!err) {
33612+ if (!path) {
33613+ AuDebugOn(!file);
33614+ path = &file->f_path;
33615+ }
33616+ vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33617+ }
33618+ return err;
33619+}
33620+
1facf9fc 33621+/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
33622+int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33623+ struct file *h_file)
33624+{
33625+ int err;
33626+ struct inode *h_inode;
c06a8ce3 33627+ struct super_block *h_sb;
0b2a12c6 33628+ struct user_namespace *h_userns;
1facf9fc 33629+
1facf9fc 33630+ if (!h_file) {
c06a8ce3
AM
33631+ err = vfsub_truncate(h_path, length);
33632+ goto out;
1facf9fc 33633+ }
33634+
5527c038 33635+ h_inode = d_inode(h_path->dentry);
c06a8ce3
AM
33636+ h_sb = h_inode->i_sb;
33637+ lockdep_off();
33638+ sb_start_write(h_sb);
33639+ lockdep_on();
758e9dad 33640+ err = security_path_truncate(h_path);
2cbb1c4b 33641+ if (!err) {
0b2a12c6 33642+ h_userns = mnt_user_ns(h_path->mnt);
2cbb1c4b 33643+ lockdep_off();
0b2a12c6
JR
33644+ err = do_truncate(h_userns, h_path->dentry, length, attr,
33645+ h_file);
2cbb1c4b
JR
33646+ lockdep_on();
33647+ }
c06a8ce3
AM
33648+ lockdep_off();
33649+ sb_end_write(h_sb);
33650+ lockdep_on();
1facf9fc 33651+
4f0767ce 33652+out:
1facf9fc 33653+ return err;
33654+}
33655+
33656+/* ---------------------------------------------------------------------- */
33657+
33658+struct au_vfsub_mkdir_args {
33659+ int *errp;
33660+ struct inode *dir;
33661+ struct path *path;
33662+ int mode;
33663+};
33664+
33665+static void au_call_vfsub_mkdir(void *args)
33666+{
33667+ struct au_vfsub_mkdir_args *a = args;
33668+ *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
33669+}
33670+
33671+int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
33672+{
33673+ int err, do_sio, wkq_err;
0b2a12c6 33674+ struct user_namespace *userns;
1facf9fc 33675+
0b2a12c6
JR
33676+ userns = mnt_user_ns(path->mnt);
33677+ do_sio = au_test_h_perm_sio(userns, dir, MAY_EXEC | MAY_WRITE);
c1595e42
JR
33678+ if (!do_sio) {
33679+ lockdep_off();
1facf9fc 33680+ err = vfsub_mkdir(dir, path, mode);
c1595e42
JR
33681+ lockdep_on();
33682+ } else {
1facf9fc 33683+ struct au_vfsub_mkdir_args args = {
33684+ .errp = &err,
33685+ .dir = dir,
33686+ .path = path,
33687+ .mode = mode
33688+ };
33689+ wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
33690+ if (unlikely(wkq_err))
33691+ err = wkq_err;
33692+ }
33693+
33694+ return err;
33695+}
33696+
33697+struct au_vfsub_rmdir_args {
33698+ int *errp;
33699+ struct inode *dir;
33700+ struct path *path;
33701+};
33702+
33703+static void au_call_vfsub_rmdir(void *args)
33704+{
33705+ struct au_vfsub_rmdir_args *a = args;
33706+ *a->errp = vfsub_rmdir(a->dir, a->path);
33707+}
33708+
33709+int vfsub_sio_rmdir(struct inode *dir, struct path *path)
33710+{
33711+ int err, do_sio, wkq_err;
0b2a12c6 33712+ struct user_namespace *userns;
1facf9fc 33713+
0b2a12c6
JR
33714+ userns = mnt_user_ns(path->mnt);
33715+ do_sio = au_test_h_perm_sio(userns, dir, MAY_EXEC | MAY_WRITE);
c1595e42
JR
33716+ if (!do_sio) {
33717+ lockdep_off();
1facf9fc 33718+ err = vfsub_rmdir(dir, path);
c1595e42
JR
33719+ lockdep_on();
33720+ } else {
1facf9fc 33721+ struct au_vfsub_rmdir_args args = {
33722+ .errp = &err,
33723+ .dir = dir,
33724+ .path = path
33725+ };
33726+ wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
33727+ if (unlikely(wkq_err))
33728+ err = wkq_err;
33729+ }
33730+
33731+ return err;
33732+}
33733+
33734+/* ---------------------------------------------------------------------- */
33735+
33736+struct notify_change_args {
33737+ int *errp;
33738+ struct path *path;
33739+ struct iattr *ia;
523b37e3 33740+ struct inode **delegated_inode;
1facf9fc 33741+};
33742+
33743+static void call_notify_change(void *args)
33744+{
33745+ struct notify_change_args *a = args;
33746+ struct inode *h_inode;
0b2a12c6 33747+ struct user_namespace *userns;
1facf9fc 33748+
5527c038 33749+ h_inode = d_inode(a->path->dentry);
1facf9fc 33750+ IMustLock(h_inode);
33751+
33752+ *a->errp = -EPERM;
33753+ if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
0b2a12c6 33754+ userns = mnt_user_ns(a->path->mnt);
c1595e42 33755+ lockdep_off();
0b2a12c6 33756+ *a->errp = notify_change(userns, a->path->dentry, a->ia,
523b37e3 33757+ a->delegated_inode);
c1595e42 33758+ lockdep_on();
1facf9fc 33759+ if (!*a->errp)
33760+ vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
33761+ }
33762+ AuTraceErr(*a->errp);
33763+}
33764+
523b37e3
AM
33765+int vfsub_notify_change(struct path *path, struct iattr *ia,
33766+ struct inode **delegated_inode)
1facf9fc 33767+{
33768+ int err;
33769+ struct notify_change_args args = {
523b37e3
AM
33770+ .errp = &err,
33771+ .path = path,
33772+ .ia = ia,
33773+ .delegated_inode = delegated_inode
1facf9fc 33774+ };
33775+
33776+ call_notify_change(&args);
33777+
33778+ return err;
33779+}
33780+
523b37e3
AM
33781+int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33782+ struct inode **delegated_inode)
1facf9fc 33783+{
33784+ int err, wkq_err;
33785+ struct notify_change_args args = {
523b37e3
AM
33786+ .errp = &err,
33787+ .path = path,
33788+ .ia = ia,
33789+ .delegated_inode = delegated_inode
1facf9fc 33790+ };
33791+
33792+ wkq_err = au_wkq_wait(call_notify_change, &args);
33793+ if (unlikely(wkq_err))
33794+ err = wkq_err;
33795+
33796+ return err;
33797+}
33798+
33799+/* ---------------------------------------------------------------------- */
33800+
33801+struct unlink_args {
33802+ int *errp;
33803+ struct inode *dir;
33804+ struct path *path;
523b37e3 33805+ struct inode **delegated_inode;
1facf9fc 33806+};
33807+
33808+static void call_unlink(void *args)
33809+{
33810+ struct unlink_args *a = args;
33811+ struct dentry *d = a->path->dentry;
33812+ struct inode *h_inode;
0b2a12c6 33813+ struct user_namespace *userns;
1facf9fc 33814+ const int stop_sillyrename = (au_test_nfs(d->d_sb)
c1595e42 33815+ && au_dcount(d) == 1);
1facf9fc 33816+
33817+ IMustLock(a->dir);
33818+
33819+ a->path->dentry = d->d_parent;
33820+ *a->errp = security_path_unlink(a->path, d);
33821+ a->path->dentry = d;
33822+ if (unlikely(*a->errp))
33823+ return;
33824+
33825+ if (!stop_sillyrename)
33826+ dget(d);
5527c038
JR
33827+ h_inode = NULL;
33828+ if (d_is_positive(d)) {
33829+ h_inode = d_inode(d);
027c5e7a 33830+ ihold(h_inode);
5527c038 33831+ }
1facf9fc 33832+
0b2a12c6 33833+ userns = mnt_user_ns(a->path->mnt);
2cbb1c4b 33834+ lockdep_off();
0b2a12c6 33835+ *a->errp = vfs_unlink(userns, a->dir, d, a->delegated_inode);
2cbb1c4b 33836+ lockdep_on();
1facf9fc 33837+ if (!*a->errp) {
33838+ struct path tmp = {
33839+ .dentry = d->d_parent,
33840+ .mnt = a->path->mnt
33841+ };
33842+ vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33843+ }
33844+
33845+ if (!stop_sillyrename)
33846+ dput(d);
33847+ if (h_inode)
33848+ iput(h_inode);
33849+
33850+ AuTraceErr(*a->errp);
33851+}
33852+
33853+/*
33854+ * @dir: must be locked.
33855+ * @dentry: target dentry.
33856+ */
523b37e3
AM
33857+int vfsub_unlink(struct inode *dir, struct path *path,
33858+ struct inode **delegated_inode, int force)
1facf9fc 33859+{
33860+ int err;
33861+ struct unlink_args args = {
523b37e3
AM
33862+ .errp = &err,
33863+ .dir = dir,
33864+ .path = path,
33865+ .delegated_inode = delegated_inode
1facf9fc 33866+ };
33867+
33868+ if (!force)
33869+ call_unlink(&args);
33870+ else {
33871+ int wkq_err;
33872+
33873+ wkq_err = au_wkq_wait(call_unlink, &args);
33874+ if (unlikely(wkq_err))
33875+ err = wkq_err;
33876+ }
33877+
33878+ return err;
33879+}
7f207e10 33880diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
eca34b5c 33881--- /usr/share/empty/fs/aufs/vfsub.h 1970-01-01 01:00:00.000000000 +0100
2255d0fe
AM
33882+++ linux/fs/aufs/vfsub.h 2022-12-17 09:21:34.799855195 +0100
33883@@ -0,0 +1,358 @@
062440b3 33884+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 33885+/*
0dcfbb52 33886+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 33887+ *
0dcfbb52 33888+ * This program is free software; you can redistribute it and/or modify
1facf9fc 33889+ * it under the terms of the GNU General Public License as published by
33890+ * the Free Software Foundation; either version 2 of the License, or
33891+ * (at your option) any later version.
dece6358
AM
33892+ *
33893+ * This program is distributed in the hope that it will be useful,
33894+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
33895+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33896+ * GNU General Public License for more details.
33897+ *
33898+ * You should have received a copy of the GNU General Public License
523b37e3 33899+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 33900+ */
33901+
33902+/*
33903+ * sub-routines for VFS
33904+ */
33905+
33906+#ifndef __AUFS_VFSUB_H__
33907+#define __AUFS_VFSUB_H__
33908+
33909+#ifdef __KERNEL__
33910+
33911+#include <linux/fs.h>
b4510431 33912+#include <linux/mount.h>
8cdd5066 33913+#include <linux/posix_acl.h>
c1595e42 33914+#include <linux/xattr.h>
7f207e10 33915+#include "debug.h"
1facf9fc 33916+
7f207e10 33917+/* copied from linux/fs/internal.h */
2cbb1c4b 33918+/* todo: BAD approach!! */
c06a8ce3 33919+extern void __mnt_drop_write(struct vfsmount *);
acd2b654 33920+extern struct file *alloc_empty_file(int, const struct cred *);
7f207e10
AM
33921+
33922+/* ---------------------------------------------------------------------- */
1facf9fc 33923+
33924+/* lock subclass for lower inode */
33925+/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
33926+/* reduce? gave up. */
33927+enum {
c1595e42 33928+ AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
1facf9fc 33929+ AuLsc_I_PARENT, /* lower inode, parent first */
33930+ AuLsc_I_PARENT2, /* copyup dirs */
dece6358 33931+ AuLsc_I_PARENT3, /* copyup wh */
1facf9fc 33932+ AuLsc_I_CHILD,
33933+ AuLsc_I_CHILD2,
33934+ AuLsc_I_End
33935+};
33936+
33937+/* to debug easier, do not make them inlined functions */
33938+#define MtxMustLock(mtx) AuDebugOn(!mutex_is_locked(mtx))
febd17d6 33939+#define IMustLock(i) AuDebugOn(!inode_is_locked(i))
1facf9fc 33940+
33941+/* ---------------------------------------------------------------------- */
33942+
7f207e10
AM
33943+static inline void vfsub_drop_nlink(struct inode *inode)
33944+{
33945+ AuDebugOn(!inode->i_nlink);
33946+ drop_nlink(inode);
33947+}
33948+
027c5e7a
AM
33949+static inline void vfsub_dead_dir(struct inode *inode)
33950+{
33951+ AuDebugOn(!S_ISDIR(inode->i_mode));
33952+ inode->i_flags |= S_DEAD;
33953+ clear_nlink(inode);
33954+}
33955+
392086de
AM
33956+static inline int vfsub_native_ro(struct inode *inode)
33957+{
8b6a4947 33958+ return sb_rdonly(inode->i_sb)
392086de
AM
33959+ || IS_RDONLY(inode)
33960+ /* || IS_APPEND(inode) */
33961+ || IS_IMMUTABLE(inode);
33962+}
33963+
8cdd5066
JR
33964+#ifdef CONFIG_AUFS_BR_FUSE
33965+int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
33966+#else
33967+AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
33968+#endif
33969+
cd233405 33970+int vfsub_sync_filesystem(struct super_block *h_sb);
a2654f78 33971+
7f207e10
AM
33972+/* ---------------------------------------------------------------------- */
33973+
33974+int vfsub_update_h_iattr(struct path *h_path, int *did);
33975+struct file *vfsub_dentry_open(struct path *path, int flags);
33976+struct file *vfsub_filp_open(const char *path, int oflags, int mode);
acd2b654 33977+struct au_branch;
b912730e 33978+struct vfsub_aopen_args {
acd2b654
AM
33979+ struct file *file;
33980+ unsigned int open_flag;
33981+ umode_t create_mode;
33982+ struct au_branch *br;
b912730e 33983+};
b912730e 33984+int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
acd2b654 33985+ struct vfsub_aopen_args *args);
1facf9fc 33986+int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
b4510431 33987+
febd17d6 33988+struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
758e9dad
AM
33989+ struct path *ppath, int len);
33990+struct dentry *vfsub_lookup_one_len(const char *name, struct path *ppath,
1facf9fc 33991+ int len);
b4510431
AM
33992+
33993+struct vfsub_lkup_one_args {
33994+ struct dentry **errp;
33995+ struct qstr *name;
758e9dad 33996+ struct path *ppath;
b4510431
AM
33997+};
33998+
33999+static inline struct dentry *vfsub_lkup_one(struct qstr *name,
758e9dad 34000+ struct path *ppath)
b4510431 34001+{
758e9dad 34002+ return vfsub_lookup_one_len(name->name, ppath, name->len);
b4510431
AM
34003+}
34004+
34005+void vfsub_call_lkup_one(void *args);
34006+
34007+/* ---------------------------------------------------------------------- */
34008+
34009+static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
34010+{
34011+ int err;
076b876e 34012+
b4510431
AM
34013+ lockdep_off();
34014+ err = mnt_want_write(mnt);
34015+ lockdep_on();
34016+ return err;
34017+}
34018+
34019+static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
34020+{
34021+ lockdep_off();
34022+ mnt_drop_write(mnt);
34023+ lockdep_on();
34024+}
1facf9fc 34025+
7e9cd9fe 34026+#if 0 /* reserved */
c06a8ce3
AM
34027+static inline void vfsub_mnt_drop_write_file(struct file *file)
34028+{
34029+ lockdep_off();
34030+ mnt_drop_write_file(file);
34031+ lockdep_on();
34032+}
7e9cd9fe 34033+#endif
c06a8ce3 34034+
1facf9fc 34035+/* ---------------------------------------------------------------------- */
34036+
34037+struct au_hinode;
34038+struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
34039+ struct dentry *d2, struct au_hinode *hdir2);
34040+void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
34041+ struct dentry *d2, struct au_hinode *hdir2);
34042+
537831f9
AM
34043+int vfsub_create(struct inode *dir, struct path *path, int mode,
34044+ bool want_excl);
1facf9fc 34045+int vfsub_symlink(struct inode *dir, struct path *path,
34046+ const char *symname);
34047+int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
34048+int vfsub_link(struct dentry *src_dentry, struct inode *dir,
523b37e3 34049+ struct path *path, struct inode **delegated_inode);
1facf9fc 34050+int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
523b37e3 34051+ struct inode *hdir, struct path *path,
f2c43d5f 34052+ struct inode **delegated_inode, unsigned int flags);
1facf9fc 34053+int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
34054+int vfsub_rmdir(struct inode *dir, struct path *path);
34055+
34056+/* ---------------------------------------------------------------------- */
34057+
34058+ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
34059+ loff_t *ppos);
34060+ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
34061+ loff_t *ppos);
34062+ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
34063+ loff_t *ppos);
34064+ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
34065+ loff_t *ppos);
4a4d8108 34066+int vfsub_flush(struct file *file, fl_owner_t id);
392086de
AM
34067+int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
34068+
c06a8ce3
AM
34069+static inline loff_t vfsub_f_size_read(struct file *file)
34070+{
34071+ return i_size_read(file_inode(file));
34072+}
34073+
4a4d8108
AM
34074+static inline unsigned int vfsub_file_flags(struct file *file)
34075+{
34076+ unsigned int flags;
34077+
34078+ spin_lock(&file->f_lock);
34079+ flags = file->f_flags;
34080+ spin_unlock(&file->f_lock);
34081+
34082+ return flags;
34083+}
1308ab2a 34084+
f0c0a007
AM
34085+static inline int vfsub_file_execed(struct file *file)
34086+{
34087+ /* todo: direct access f_flags */
34088+ return !!(vfsub_file_flags(file) & __FMODE_EXEC);
34089+}
34090+
7e9cd9fe 34091+#if 0 /* reserved */
1facf9fc 34092+static inline void vfsub_file_accessed(struct file *h_file)
34093+{
34094+ file_accessed(h_file);
34095+ vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
34096+}
7e9cd9fe 34097+#endif
1facf9fc 34098+
79b8bda9 34099+#if 0 /* reserved */
1facf9fc 34100+static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
34101+ struct dentry *h_dentry)
34102+{
34103+ struct path h_path = {
34104+ .dentry = h_dentry,
34105+ .mnt = h_mnt
34106+ };
92d182d2 34107+ touch_atime(&h_path);
1facf9fc 34108+ vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
34109+}
79b8bda9 34110+#endif
1facf9fc 34111+
cd7a4cd9
AM
34112+static inline int vfsub_update_time(struct inode *h_inode,
34113+ struct timespec64 *ts, int flags)
0c3ec466 34114+{
614c9df0 34115+ return inode_update_time(h_inode, ts, flags);
0c3ec466
AM
34116+ /* no vfsub_update_h_iattr() since we don't have struct path */
34117+}
34118+
8cdd5066 34119+#ifdef CONFIG_FS_POSIX_ACL
0b2a12c6
JR
34120+static inline int vfsub_acl_chmod(struct user_namespace *h_userns,
34121+ struct inode *h_inode, umode_t h_mode)
8cdd5066
JR
34122+{
34123+ int err;
34124+
0b2a12c6 34125+ err = posix_acl_chmod(h_userns, h_inode, h_mode);
8cdd5066
JR
34126+ if (err == -EOPNOTSUPP)
34127+ err = 0;
34128+ return err;
34129+}
34130+#else
0b2a12c6
JR
34131+AuStubInt0(vfsub_acl_chmod, struct user_namespace *h_userns,
34132+ struct inode *h_inode, umode_t h_mode);
8cdd5066
JR
34133+#endif
34134+
4a4d8108
AM
34135+long vfsub_splice_to(struct file *in, loff_t *ppos,
34136+ struct pipe_inode_info *pipe, size_t len,
34137+ unsigned int flags);
34138+long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
34139+ loff_t *ppos, size_t len, unsigned int flags);
c06a8ce3
AM
34140+
34141+static inline long vfsub_truncate(struct path *path, loff_t length)
34142+{
34143+ long err;
076b876e 34144+
c06a8ce3
AM
34145+ lockdep_off();
34146+ err = vfs_truncate(path, length);
34147+ lockdep_on();
34148+ return err;
34149+}
34150+
4a4d8108
AM
34151+int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
34152+ struct file *h_file);
53392da6 34153+int vfsub_fsync(struct file *file, struct path *path, int datasync);
4a4d8108 34154+
521ced18
JR
34155+/*
34156+ * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
34157+ * ioctl.
34158+ */
9f237c51
AM
34159+static inline loff_t vfsub_clone_file_range(struct file *src, struct file *dst,
34160+ loff_t len)
521ced18 34161+{
9f237c51 34162+ loff_t err;
521ced18
JR
34163+
34164+ lockdep_off();
9f237c51 34165+ err = vfs_clone_file_range(src, 0, dst, 0, len, /*remap_flags*/0);
521ced18
JR
34166+ lockdep_on();
34167+
34168+ return err;
34169+}
34170+
34171+/* copy_file_range(2) is a systemcall */
34172+static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
34173+ struct file *dst, loff_t dst_pos,
34174+ size_t len, unsigned int flags)
34175+{
34176+ ssize_t ssz;
34177+
34178+ lockdep_off();
34179+ ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
34180+ lockdep_on();
34181+
34182+ return ssz;
34183+}
34184+
1facf9fc 34185+/* ---------------------------------------------------------------------- */
34186+
34187+static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
34188+{
34189+ loff_t err;
34190+
2cbb1c4b 34191+ lockdep_off();
1facf9fc 34192+ err = vfs_llseek(file, offset, origin);
2cbb1c4b 34193+ lockdep_on();
1facf9fc 34194+ return err;
34195+}
34196+
34197+/* ---------------------------------------------------------------------- */
34198+
4a4d8108
AM
34199+int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
34200+int vfsub_sio_rmdir(struct inode *dir, struct path *path);
523b37e3
AM
34201+int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
34202+ struct inode **delegated_inode);
34203+int vfsub_notify_change(struct path *path, struct iattr *ia,
34204+ struct inode **delegated_inode);
34205+int vfsub_unlink(struct inode *dir, struct path *path,
34206+ struct inode **delegated_inode, int force);
4a4d8108 34207+
521ced18
JR
34208+static inline int vfsub_getattr(const struct path *path, struct kstat *st)
34209+{
34210+ return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
34211+}
34212+
c1595e42
JR
34213+/* ---------------------------------------------------------------------- */
34214+
0b2a12c6
JR
34215+static inline int vfsub_setxattr(struct user_namespace *userns,
34216+ struct dentry *dentry, const char *name,
2255d0fe 34217+ const void *value, size_t size, int flags)
c1595e42
JR
34218+{
34219+ int err;
34220+
34221+ lockdep_off();
0b2a12c6 34222+ err = vfs_setxattr(userns, dentry, name, value, size, flags);
c1595e42
JR
34223+ lockdep_on();
34224+
34225+ return err;
34226+}
34227+
0b2a12c6
JR
34228+static inline int vfsub_removexattr(struct user_namespace *userns,
34229+ struct dentry *dentry, const char *name)
c1595e42
JR
34230+{
34231+ int err;
34232+
34233+ lockdep_off();
0b2a12c6 34234+ err = vfs_removexattr(userns, dentry, name);
c1595e42
JR
34235+ lockdep_on();
34236+
34237+ return err;
34238+}
34239+
1facf9fc 34240+#endif /* __KERNEL__ */
34241+#endif /* __AUFS_VFSUB_H__ */
7f207e10 34242diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
eca34b5c 34243--- /usr/share/empty/fs/aufs/wbr_policy.c 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 34244+++ linux/fs/aufs/wbr_policy.c 2022-11-05 23:02:18.969222617 +0100
cd7a4cd9
AM
34245@@ -0,0 +1,830 @@
34246+// SPDX-License-Identifier: GPL-2.0
1facf9fc 34247+/*
0dcfbb52 34248+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 34249+ *
0dcfbb52 34250+ * This program is free software; you can redistribute it and/or modify
1facf9fc 34251+ * it under the terms of the GNU General Public License as published by
34252+ * the Free Software Foundation; either version 2 of the License, or
34253+ * (at your option) any later version.
dece6358
AM
34254+ *
34255+ * This program is distributed in the hope that it will be useful,
34256+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
34257+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34258+ * GNU General Public License for more details.
34259+ *
34260+ * You should have received a copy of the GNU General Public License
523b37e3 34261+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 34262+ */
34263+
34264+/*
34265+ * policies for selecting one among multiple writable branches
34266+ */
34267+
34268+#include <linux/statfs.h>
34269+#include "aufs.h"
34270+
34271+/* subset of cpup_attr() */
34272+static noinline_for_stack
34273+int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
34274+{
34275+ int err, sbits;
34276+ struct iattr ia;
34277+ struct inode *h_isrc;
34278+
5527c038 34279+ h_isrc = d_inode(h_src);
1facf9fc 34280+ ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
34281+ ia.ia_mode = h_isrc->i_mode;
34282+ ia.ia_uid = h_isrc->i_uid;
34283+ ia.ia_gid = h_isrc->i_gid;
34284+ sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
5527c038 34285+ au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
523b37e3
AM
34286+ /* no delegation since it is just created */
34287+ err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
1facf9fc 34288+
34289+ /* is this nfs only? */
34290+ if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
34291+ ia.ia_valid = ATTR_FORCE | ATTR_MODE;
34292+ ia.ia_mode = h_isrc->i_mode;
523b37e3 34293+ err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
1facf9fc 34294+ }
34295+
34296+ return err;
34297+}
34298+
34299+#define AuCpdown_PARENT_OPQ 1
34300+#define AuCpdown_WHED (1 << 1)
34301+#define AuCpdown_MADE_DIR (1 << 2)
34302+#define AuCpdown_DIROPQ (1 << 3)
34303+#define au_ftest_cpdown(flags, name) ((flags) & AuCpdown_##name)
7f207e10
AM
34304+#define au_fset_cpdown(flags, name) \
34305+ do { (flags) |= AuCpdown_##name; } while (0)
34306+#define au_fclr_cpdown(flags, name) \
34307+ do { (flags) &= ~AuCpdown_##name; } while (0)
1facf9fc 34308+
1facf9fc 34309+static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
c2b27bf2 34310+ unsigned int *flags)
1facf9fc 34311+{
34312+ int err;
34313+ struct dentry *opq_dentry;
34314+
34315+ opq_dentry = au_diropq_create(dentry, bdst);
34316+ err = PTR_ERR(opq_dentry);
34317+ if (IS_ERR(opq_dentry))
34318+ goto out;
34319+ dput(opq_dentry);
c2b27bf2 34320+ au_fset_cpdown(*flags, DIROPQ);
1facf9fc 34321+
4f0767ce 34322+out:
1facf9fc 34323+ return err;
34324+}
34325+
34326+static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
34327+ struct inode *dir, aufs_bindex_t bdst)
34328+{
34329+ int err;
34330+ struct path h_path;
34331+ struct au_branch *br;
34332+
34333+ br = au_sbr(dentry->d_sb, bdst);
34334+ h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
34335+ err = PTR_ERR(h_path.dentry);
34336+ if (IS_ERR(h_path.dentry))
34337+ goto out;
34338+
34339+ err = 0;
5527c038 34340+ if (d_is_positive(h_path.dentry)) {
86dc4139 34341+ h_path.mnt = au_br_mnt(br);
1facf9fc 34342+ err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
34343+ dentry);
34344+ }
34345+ dput(h_path.dentry);
34346+
4f0767ce 34347+out:
1facf9fc 34348+ return err;
34349+}
34350+
34351+static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 34352+ struct au_pin *pin,
1facf9fc 34353+ struct dentry *h_parent, void *arg)
34354+{
34355+ int err, rerr;
5afbbe0d 34356+ aufs_bindex_t bopq, btop;
1facf9fc 34357+ struct path h_path;
34358+ struct dentry *parent;
34359+ struct inode *h_dir, *h_inode, *inode, *dir;
c2b27bf2 34360+ unsigned int *flags = arg;
1facf9fc 34361+
5afbbe0d 34362+ btop = au_dbtop(dentry);
1facf9fc 34363+ /* dentry is di-locked */
34364+ parent = dget_parent(dentry);
5527c038
JR
34365+ dir = d_inode(parent);
34366+ h_dir = d_inode(h_parent);
1facf9fc 34367+ AuDebugOn(h_dir != au_h_iptr(dir, bdst));
34368+ IMustLock(h_dir);
34369+
86dc4139 34370+ err = au_lkup_neg(dentry, bdst, /*wh*/0);
1facf9fc 34371+ if (unlikely(err < 0))
34372+ goto out;
34373+ h_path.dentry = au_h_dptr(dentry, bdst);
34374+ h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
cd7a4cd9 34375+ err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path, 0755);
1facf9fc 34376+ if (unlikely(err))
34377+ goto out_put;
c2b27bf2 34378+ au_fset_cpdown(*flags, MADE_DIR);
1facf9fc 34379+
1facf9fc 34380+ bopq = au_dbdiropq(dentry);
c2b27bf2
AM
34381+ au_fclr_cpdown(*flags, WHED);
34382+ au_fclr_cpdown(*flags, DIROPQ);
1facf9fc 34383+ if (au_dbwh(dentry) == bdst)
c2b27bf2
AM
34384+ au_fset_cpdown(*flags, WHED);
34385+ if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
34386+ au_fset_cpdown(*flags, PARENT_OPQ);
5527c038 34387+ h_inode = d_inode(h_path.dentry);
febd17d6 34388+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
c2b27bf2
AM
34389+ if (au_ftest_cpdown(*flags, WHED)) {
34390+ err = au_cpdown_dir_opq(dentry, bdst, flags);
1facf9fc 34391+ if (unlikely(err)) {
febd17d6 34392+ inode_unlock(h_inode);
1facf9fc 34393+ goto out_dir;
34394+ }
34395+ }
34396+
5afbbe0d 34397+ err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
febd17d6 34398+ inode_unlock(h_inode);
1facf9fc 34399+ if (unlikely(err))
34400+ goto out_opq;
34401+
c2b27bf2 34402+ if (au_ftest_cpdown(*flags, WHED)) {
1facf9fc 34403+ err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
34404+ if (unlikely(err))
34405+ goto out_opq;
34406+ }
34407+
5527c038 34408+ inode = d_inode(dentry);
5afbbe0d
AM
34409+ if (au_ibbot(inode) < bdst)
34410+ au_set_ibbot(inode, bdst);
1facf9fc 34411+ au_set_h_iptr(inode, bdst, au_igrab(h_inode),
34412+ au_hi_flags(inode, /*isdir*/1));
076b876e 34413+ au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
1facf9fc 34414+ goto out; /* success */
34415+
34416+ /* revert */
4f0767ce 34417+out_opq:
c2b27bf2 34418+ if (au_ftest_cpdown(*flags, DIROPQ)) {
febd17d6 34419+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
1facf9fc 34420+ rerr = au_diropq_remove(dentry, bdst);
febd17d6 34421+ inode_unlock(h_inode);
1facf9fc 34422+ if (unlikely(rerr)) {
523b37e3
AM
34423+ AuIOErr("failed removing diropq for %pd b%d (%d)\n",
34424+ dentry, bdst, rerr);
1facf9fc 34425+ err = -EIO;
34426+ goto out;
34427+ }
34428+ }
4f0767ce 34429+out_dir:
c2b27bf2 34430+ if (au_ftest_cpdown(*flags, MADE_DIR)) {
1facf9fc 34431+ rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
34432+ if (unlikely(rerr)) {
523b37e3
AM
34433+ AuIOErr("failed removing %pd b%d (%d)\n",
34434+ dentry, bdst, rerr);
1facf9fc 34435+ err = -EIO;
34436+ }
34437+ }
4f0767ce 34438+out_put:
1facf9fc 34439+ au_set_h_dptr(dentry, bdst, NULL);
5afbbe0d
AM
34440+ if (au_dbbot(dentry) == bdst)
34441+ au_update_dbbot(dentry);
4f0767ce 34442+out:
1facf9fc 34443+ dput(parent);
34444+ return err;
34445+}
34446+
34447+int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
34448+{
34449+ int err;
c2b27bf2 34450+ unsigned int flags;
1facf9fc 34451+
c2b27bf2
AM
34452+ flags = 0;
34453+ err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
1facf9fc 34454+
34455+ return err;
34456+}
34457+
34458+/* ---------------------------------------------------------------------- */
34459+
34460+/* policies for create */
34461+
c2b27bf2 34462+int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
4a4d8108
AM
34463+{
34464+ int err, i, j, ndentry;
34465+ aufs_bindex_t bopq;
34466+ struct au_dcsub_pages dpages;
34467+ struct au_dpage *dpage;
34468+ struct dentry **dentries, *parent, *d;
34469+
34470+ err = au_dpages_init(&dpages, GFP_NOFS);
34471+ if (unlikely(err))
34472+ goto out;
34473+ parent = dget_parent(dentry);
027c5e7a 34474+ err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
4a4d8108
AM
34475+ if (unlikely(err))
34476+ goto out_free;
34477+
34478+ err = bindex;
34479+ for (i = 0; i < dpages.ndpage; i++) {
34480+ dpage = dpages.dpages + i;
34481+ dentries = dpage->dentries;
34482+ ndentry = dpage->ndentry;
34483+ for (j = 0; j < ndentry; j++) {
34484+ d = dentries[j];
34485+ di_read_lock_parent2(d, !AuLock_IR);
34486+ bopq = au_dbdiropq(d);
34487+ di_read_unlock(d, !AuLock_IR);
34488+ if (bopq >= 0 && bopq < err)
34489+ err = bopq;
34490+ }
34491+ }
34492+
34493+out_free:
34494+ dput(parent);
34495+ au_dpages_free(&dpages);
34496+out:
34497+ return err;
34498+}
34499+
1facf9fc 34500+static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
34501+{
34502+ for (; bindex >= 0; bindex--)
34503+ if (!au_br_rdonly(au_sbr(sb, bindex)))
34504+ return bindex;
34505+ return -EROFS;
34506+}
34507+
34508+/* top down parent */
392086de
AM
34509+static int au_wbr_create_tdp(struct dentry *dentry,
34510+ unsigned int flags __maybe_unused)
1facf9fc 34511+{
34512+ int err;
5afbbe0d 34513+ aufs_bindex_t btop, bindex;
1facf9fc 34514+ struct super_block *sb;
34515+ struct dentry *parent, *h_parent;
34516+
34517+ sb = dentry->d_sb;
5afbbe0d
AM
34518+ btop = au_dbtop(dentry);
34519+ err = btop;
34520+ if (!au_br_rdonly(au_sbr(sb, btop)))
1facf9fc 34521+ goto out;
34522+
34523+ err = -EROFS;
34524+ parent = dget_parent(dentry);
5afbbe0d 34525+ for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
1facf9fc 34526+ h_parent = au_h_dptr(parent, bindex);
5527c038 34527+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 34528+ continue;
34529+
34530+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
34531+ err = bindex;
34532+ break;
34533+ }
34534+ }
34535+ dput(parent);
34536+
34537+ /* bottom up here */
4a4d8108 34538+ if (unlikely(err < 0)) {
5afbbe0d 34539+ err = au_wbr_bu(sb, btop - 1);
4a4d8108
AM
34540+ if (err >= 0)
34541+ err = au_wbr_nonopq(dentry, err);
34542+ }
1facf9fc 34543+
4f0767ce 34544+out:
1facf9fc 34545+ AuDbg("b%d\n", err);
34546+ return err;
34547+}
34548+
34549+/* ---------------------------------------------------------------------- */
34550+
34551+/* an exception for the policy other than tdp */
34552+static int au_wbr_create_exp(struct dentry *dentry)
34553+{
34554+ int err;
34555+ aufs_bindex_t bwh, bdiropq;
34556+ struct dentry *parent;
34557+
34558+ err = -1;
34559+ bwh = au_dbwh(dentry);
34560+ parent = dget_parent(dentry);
34561+ bdiropq = au_dbdiropq(parent);
34562+ if (bwh >= 0) {
34563+ if (bdiropq >= 0)
34564+ err = min(bdiropq, bwh);
34565+ else
34566+ err = bwh;
34567+ AuDbg("%d\n", err);
34568+ } else if (bdiropq >= 0) {
34569+ err = bdiropq;
34570+ AuDbg("%d\n", err);
34571+ }
34572+ dput(parent);
34573+
4a4d8108
AM
34574+ if (err >= 0)
34575+ err = au_wbr_nonopq(dentry, err);
34576+
1facf9fc 34577+ if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
34578+ err = -1;
34579+
34580+ AuDbg("%d\n", err);
34581+ return err;
34582+}
34583+
34584+/* ---------------------------------------------------------------------- */
34585+
34586+/* round robin */
34587+static int au_wbr_create_init_rr(struct super_block *sb)
34588+{
34589+ int err;
34590+
5afbbe0d 34591+ err = au_wbr_bu(sb, au_sbbot(sb));
1facf9fc 34592+ atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
dece6358 34593+ /* smp_mb(); */
1facf9fc 34594+
34595+ AuDbg("b%d\n", err);
34596+ return err;
34597+}
34598+
392086de 34599+static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
1facf9fc 34600+{
34601+ int err, nbr;
34602+ unsigned int u;
5afbbe0d 34603+ aufs_bindex_t bindex, bbot;
1facf9fc 34604+ struct super_block *sb;
34605+ atomic_t *next;
34606+
34607+ err = au_wbr_create_exp(dentry);
34608+ if (err >= 0)
34609+ goto out;
34610+
34611+ sb = dentry->d_sb;
34612+ next = &au_sbi(sb)->si_wbr_rr_next;
5afbbe0d
AM
34613+ bbot = au_sbbot(sb);
34614+ nbr = bbot + 1;
34615+ for (bindex = 0; bindex <= bbot; bindex++) {
392086de 34616+ if (!au_ftest_wbr(flags, DIR)) {
1facf9fc 34617+ err = atomic_dec_return(next) + 1;
34618+ /* modulo for 0 is meaningless */
34619+ if (unlikely(!err))
34620+ err = atomic_dec_return(next) + 1;
34621+ } else
34622+ err = atomic_read(next);
34623+ AuDbg("%d\n", err);
34624+ u = err;
34625+ err = u % nbr;
34626+ AuDbg("%d\n", err);
34627+ if (!au_br_rdonly(au_sbr(sb, err)))
34628+ break;
34629+ err = -EROFS;
34630+ }
34631+
4a4d8108
AM
34632+ if (err >= 0)
34633+ err = au_wbr_nonopq(dentry, err);
34634+
4f0767ce 34635+out:
1facf9fc 34636+ AuDbg("%d\n", err);
34637+ return err;
34638+}
34639+
34640+/* ---------------------------------------------------------------------- */
34641+
34642+/* most free space */
392086de 34643+static void au_mfs(struct dentry *dentry, struct dentry *parent)
1facf9fc 34644+{
34645+ struct super_block *sb;
34646+ struct au_branch *br;
34647+ struct au_wbr_mfs *mfs;
392086de 34648+ struct dentry *h_parent;
5afbbe0d 34649+ aufs_bindex_t bindex, bbot;
1facf9fc 34650+ int err;
34651+ unsigned long long b, bavail;
7f207e10 34652+ struct path h_path;
1facf9fc 34653+ /* reduce the stack usage */
34654+ struct kstatfs *st;
34655+
34656+ st = kmalloc(sizeof(*st), GFP_NOFS);
34657+ if (unlikely(!st)) {
34658+ AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
34659+ return;
34660+ }
34661+
34662+ bavail = 0;
34663+ sb = dentry->d_sb;
34664+ mfs = &au_sbi(sb)->si_wbr_mfs;
dece6358 34665+ MtxMustLock(&mfs->mfs_lock);
1facf9fc 34666+ mfs->mfs_bindex = -EROFS;
34667+ mfs->mfsrr_bytes = 0;
392086de
AM
34668+ if (!parent) {
34669+ bindex = 0;
5afbbe0d 34670+ bbot = au_sbbot(sb);
392086de 34671+ } else {
5afbbe0d
AM
34672+ bindex = au_dbtop(parent);
34673+ bbot = au_dbtaildir(parent);
392086de
AM
34674+ }
34675+
5afbbe0d 34676+ for (; bindex <= bbot; bindex++) {
392086de
AM
34677+ if (parent) {
34678+ h_parent = au_h_dptr(parent, bindex);
5527c038 34679+ if (!h_parent || d_is_negative(h_parent))
392086de
AM
34680+ continue;
34681+ }
1facf9fc 34682+ br = au_sbr(sb, bindex);
34683+ if (au_br_rdonly(br))
34684+ continue;
34685+
34686+ /* sb->s_root for NFS is unreliable */
86dc4139 34687+ h_path.mnt = au_br_mnt(br);
7f207e10
AM
34688+ h_path.dentry = h_path.mnt->mnt_root;
34689+ err = vfs_statfs(&h_path, st);
1facf9fc 34690+ if (unlikely(err)) {
34691+ AuWarn1("failed statfs, b%d, %d\n", bindex, err);
34692+ continue;
34693+ }
34694+
34695+ /* when the available size is equal, select the lower one */
34696+ BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
34697+ || sizeof(b) < sizeof(st->f_bsize));
34698+ b = st->f_bavail * st->f_bsize;
34699+ br->br_wbr->wbr_bytes = b;
34700+ if (b >= bavail) {
34701+ bavail = b;
34702+ mfs->mfs_bindex = bindex;
34703+ mfs->mfs_jiffy = jiffies;
34704+ }
34705+ }
34706+
34707+ mfs->mfsrr_bytes = bavail;
34708+ AuDbg("b%d\n", mfs->mfs_bindex);
9f237c51 34709+ au_kfree_rcu(st);
1facf9fc 34710+}
34711+
392086de 34712+static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
1facf9fc 34713+{
34714+ int err;
392086de 34715+ struct dentry *parent;
1facf9fc 34716+ struct super_block *sb;
34717+ struct au_wbr_mfs *mfs;
34718+
34719+ err = au_wbr_create_exp(dentry);
34720+ if (err >= 0)
34721+ goto out;
34722+
34723+ sb = dentry->d_sb;
392086de
AM
34724+ parent = NULL;
34725+ if (au_ftest_wbr(flags, PARENT))
34726+ parent = dget_parent(dentry);
1facf9fc 34727+ mfs = &au_sbi(sb)->si_wbr_mfs;
34728+ mutex_lock(&mfs->mfs_lock);
34729+ if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34730+ || mfs->mfs_bindex < 0
34731+ || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
392086de 34732+ au_mfs(dentry, parent);
1facf9fc 34733+ mutex_unlock(&mfs->mfs_lock);
34734+ err = mfs->mfs_bindex;
392086de 34735+ dput(parent);
1facf9fc 34736+
4a4d8108
AM
34737+ if (err >= 0)
34738+ err = au_wbr_nonopq(dentry, err);
34739+
4f0767ce 34740+out:
1facf9fc 34741+ AuDbg("b%d\n", err);
34742+ return err;
34743+}
34744+
34745+static int au_wbr_create_init_mfs(struct super_block *sb)
34746+{
34747+ struct au_wbr_mfs *mfs;
34748+
34749+ mfs = &au_sbi(sb)->si_wbr_mfs;
34750+ mutex_init(&mfs->mfs_lock);
34751+ mfs->mfs_jiffy = 0;
34752+ mfs->mfs_bindex = -EROFS;
34753+
34754+ return 0;
34755+}
34756+
34757+static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
34758+{
34759+ mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
34760+ return 0;
34761+}
34762+
34763+/* ---------------------------------------------------------------------- */
34764+
f2c43d5f
AM
34765+/* top down regardless parent, and then mfs */
34766+static int au_wbr_create_tdmfs(struct dentry *dentry,
34767+ unsigned int flags __maybe_unused)
34768+{
34769+ int err;
34770+ aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
34771+ unsigned long long watermark;
34772+ struct super_block *sb;
34773+ struct au_wbr_mfs *mfs;
34774+ struct au_branch *br;
34775+ struct dentry *parent;
34776+
34777+ sb = dentry->d_sb;
34778+ mfs = &au_sbi(sb)->si_wbr_mfs;
34779+ mutex_lock(&mfs->mfs_lock);
34780+ if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34781+ || mfs->mfs_bindex < 0)
34782+ au_mfs(dentry, /*parent*/NULL);
34783+ watermark = mfs->mfsrr_watermark;
34784+ bmfs = mfs->mfs_bindex;
34785+ mutex_unlock(&mfs->mfs_lock);
34786+
34787+ /* another style of au_wbr_create_exp() */
34788+ bwh = au_dbwh(dentry);
34789+ parent = dget_parent(dentry);
34790+ btail = au_dbtaildir(parent);
34791+ if (bwh >= 0 && bwh < btail)
34792+ btail = bwh;
34793+
34794+ err = au_wbr_nonopq(dentry, btail);
34795+ if (unlikely(err < 0))
34796+ goto out;
34797+ btail = err;
34798+ bfound = -1;
34799+ for (bindex = 0; bindex <= btail; bindex++) {
34800+ br = au_sbr(sb, bindex);
34801+ if (au_br_rdonly(br))
34802+ continue;
34803+ if (br->br_wbr->wbr_bytes > watermark) {
34804+ bfound = bindex;
34805+ break;
34806+ }
34807+ }
34808+ err = bfound;
34809+ if (err < 0)
34810+ err = bmfs;
34811+
34812+out:
34813+ dput(parent);
34814+ AuDbg("b%d\n", err);
34815+ return err;
34816+}
34817+
34818+/* ---------------------------------------------------------------------- */
34819+
1facf9fc 34820+/* most free space and then round robin */
392086de 34821+static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
1facf9fc 34822+{
34823+ int err;
34824+ struct au_wbr_mfs *mfs;
34825+
392086de 34826+ err = au_wbr_create_mfs(dentry, flags);
1facf9fc 34827+ if (err >= 0) {
34828+ mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
dece6358 34829+ mutex_lock(&mfs->mfs_lock);
1facf9fc 34830+ if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
392086de 34831+ err = au_wbr_create_rr(dentry, flags);
dece6358 34832+ mutex_unlock(&mfs->mfs_lock);
1facf9fc 34833+ }
34834+
34835+ AuDbg("b%d\n", err);
34836+ return err;
34837+}
34838+
34839+static int au_wbr_create_init_mfsrr(struct super_block *sb)
34840+{
34841+ int err;
34842+
34843+ au_wbr_create_init_mfs(sb); /* ignore */
34844+ err = au_wbr_create_init_rr(sb);
34845+
34846+ return err;
34847+}
34848+
34849+/* ---------------------------------------------------------------------- */
34850+
34851+/* top down parent and most free space */
392086de 34852+static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
1facf9fc 34853+{
34854+ int err, e2;
34855+ unsigned long long b;
5afbbe0d 34856+ aufs_bindex_t bindex, btop, bbot;
1facf9fc 34857+ struct super_block *sb;
34858+ struct dentry *parent, *h_parent;
34859+ struct au_branch *br;
34860+
392086de 34861+ err = au_wbr_create_tdp(dentry, flags);
1facf9fc 34862+ if (unlikely(err < 0))
34863+ goto out;
34864+ parent = dget_parent(dentry);
5afbbe0d
AM
34865+ btop = au_dbtop(parent);
34866+ bbot = au_dbtaildir(parent);
34867+ if (btop == bbot)
1facf9fc 34868+ goto out_parent; /* success */
34869+
392086de 34870+ e2 = au_wbr_create_mfs(dentry, flags);
1facf9fc 34871+ if (e2 < 0)
34872+ goto out_parent; /* success */
34873+
34874+ /* when the available size is equal, select upper one */
34875+ sb = dentry->d_sb;
34876+ br = au_sbr(sb, err);
34877+ b = br->br_wbr->wbr_bytes;
34878+ AuDbg("b%d, %llu\n", err, b);
34879+
5afbbe0d 34880+ for (bindex = btop; bindex <= bbot; bindex++) {
1facf9fc 34881+ h_parent = au_h_dptr(parent, bindex);
5527c038 34882+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 34883+ continue;
34884+
34885+ br = au_sbr(sb, bindex);
34886+ if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
34887+ b = br->br_wbr->wbr_bytes;
34888+ err = bindex;
34889+ AuDbg("b%d, %llu\n", err, b);
34890+ }
34891+ }
34892+
4a4d8108
AM
34893+ if (err >= 0)
34894+ err = au_wbr_nonopq(dentry, err);
34895+
4f0767ce 34896+out_parent:
1facf9fc 34897+ dput(parent);
4f0767ce 34898+out:
1facf9fc 34899+ AuDbg("b%d\n", err);
34900+ return err;
34901+}
34902+
34903+/* ---------------------------------------------------------------------- */
34904+
392086de
AM
34905+/*
34906+ * - top down parent
34907+ * - most free space with parent
34908+ * - most free space round-robin regardless parent
34909+ */
34910+static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
34911+{
34912+ int err;
34913+ unsigned long long watermark;
34914+ struct super_block *sb;
34915+ struct au_branch *br;
34916+ struct au_wbr_mfs *mfs;
34917+
34918+ err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
34919+ if (unlikely(err < 0))
34920+ goto out;
34921+
34922+ sb = dentry->d_sb;
34923+ br = au_sbr(sb, err);
34924+ mfs = &au_sbi(sb)->si_wbr_mfs;
34925+ mutex_lock(&mfs->mfs_lock);
34926+ watermark = mfs->mfsrr_watermark;
34927+ mutex_unlock(&mfs->mfs_lock);
34928+ if (br->br_wbr->wbr_bytes < watermark)
34929+ /* regardless the parent dir */
34930+ err = au_wbr_create_mfsrr(dentry, flags);
34931+
34932+out:
34933+ AuDbg("b%d\n", err);
34934+ return err;
34935+}
34936+
34937+/* ---------------------------------------------------------------------- */
34938+
1facf9fc 34939+/* policies for copyup */
34940+
34941+/* top down parent */
34942+static int au_wbr_copyup_tdp(struct dentry *dentry)
34943+{
392086de 34944+ return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
1facf9fc 34945+}
34946+
34947+/* bottom up parent */
34948+static int au_wbr_copyup_bup(struct dentry *dentry)
34949+{
34950+ int err;
5afbbe0d 34951+ aufs_bindex_t bindex, btop;
1facf9fc 34952+ struct dentry *parent, *h_parent;
34953+ struct super_block *sb;
34954+
34955+ err = -EROFS;
34956+ sb = dentry->d_sb;
34957+ parent = dget_parent(dentry);
5afbbe0d
AM
34958+ btop = au_dbtop(parent);
34959+ for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
1facf9fc 34960+ h_parent = au_h_dptr(parent, bindex);
5527c038 34961+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 34962+ continue;
34963+
34964+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
34965+ err = bindex;
34966+ break;
34967+ }
34968+ }
34969+ dput(parent);
34970+
34971+ /* bottom up here */
34972+ if (unlikely(err < 0))
5afbbe0d 34973+ err = au_wbr_bu(sb, btop - 1);
1facf9fc 34974+
34975+ AuDbg("b%d\n", err);
34976+ return err;
34977+}
34978+
34979+/* bottom up */
5afbbe0d 34980+int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
1facf9fc 34981+{
34982+ int err;
34983+
5afbbe0d 34984+ err = au_wbr_bu(dentry->d_sb, btop);
4a4d8108 34985+ AuDbg("b%d\n", err);
5afbbe0d 34986+ if (err > btop)
4a4d8108 34987+ err = au_wbr_nonopq(dentry, err);
1facf9fc 34988+
34989+ AuDbg("b%d\n", err);
34990+ return err;
34991+}
34992+
076b876e
AM
34993+static int au_wbr_copyup_bu(struct dentry *dentry)
34994+{
34995+ int err;
5afbbe0d 34996+ aufs_bindex_t btop;
076b876e 34997+
5afbbe0d
AM
34998+ btop = au_dbtop(dentry);
34999+ err = au_wbr_do_copyup_bu(dentry, btop);
076b876e
AM
35000+ return err;
35001+}
35002+
1facf9fc 35003+/* ---------------------------------------------------------------------- */
35004+
35005+struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
35006+ [AuWbrCopyup_TDP] = {
35007+ .copyup = au_wbr_copyup_tdp
35008+ },
35009+ [AuWbrCopyup_BUP] = {
35010+ .copyup = au_wbr_copyup_bup
35011+ },
35012+ [AuWbrCopyup_BU] = {
35013+ .copyup = au_wbr_copyup_bu
35014+ }
35015+};
35016+
35017+struct au_wbr_create_operations au_wbr_create_ops[] = {
35018+ [AuWbrCreate_TDP] = {
35019+ .create = au_wbr_create_tdp
35020+ },
35021+ [AuWbrCreate_RR] = {
35022+ .create = au_wbr_create_rr,
35023+ .init = au_wbr_create_init_rr
35024+ },
35025+ [AuWbrCreate_MFS] = {
35026+ .create = au_wbr_create_mfs,
35027+ .init = au_wbr_create_init_mfs,
35028+ .fin = au_wbr_create_fin_mfs
35029+ },
35030+ [AuWbrCreate_MFSV] = {
35031+ .create = au_wbr_create_mfs,
35032+ .init = au_wbr_create_init_mfs,
35033+ .fin = au_wbr_create_fin_mfs
35034+ },
35035+ [AuWbrCreate_MFSRR] = {
35036+ .create = au_wbr_create_mfsrr,
35037+ .init = au_wbr_create_init_mfsrr,
35038+ .fin = au_wbr_create_fin_mfs
35039+ },
35040+ [AuWbrCreate_MFSRRV] = {
35041+ .create = au_wbr_create_mfsrr,
35042+ .init = au_wbr_create_init_mfsrr,
35043+ .fin = au_wbr_create_fin_mfs
35044+ },
f2c43d5f
AM
35045+ [AuWbrCreate_TDMFS] = {
35046+ .create = au_wbr_create_tdmfs,
35047+ .init = au_wbr_create_init_mfs,
35048+ .fin = au_wbr_create_fin_mfs
35049+ },
35050+ [AuWbrCreate_TDMFSV] = {
35051+ .create = au_wbr_create_tdmfs,
35052+ .init = au_wbr_create_init_mfs,
35053+ .fin = au_wbr_create_fin_mfs
35054+ },
1facf9fc 35055+ [AuWbrCreate_PMFS] = {
35056+ .create = au_wbr_create_pmfs,
35057+ .init = au_wbr_create_init_mfs,
35058+ .fin = au_wbr_create_fin_mfs
35059+ },
35060+ [AuWbrCreate_PMFSV] = {
35061+ .create = au_wbr_create_pmfs,
35062+ .init = au_wbr_create_init_mfs,
35063+ .fin = au_wbr_create_fin_mfs
392086de
AM
35064+ },
35065+ [AuWbrCreate_PMFSRR] = {
35066+ .create = au_wbr_create_pmfsrr,
35067+ .init = au_wbr_create_init_mfsrr,
35068+ .fin = au_wbr_create_fin_mfs
35069+ },
35070+ [AuWbrCreate_PMFSRRV] = {
35071+ .create = au_wbr_create_pmfsrr,
35072+ .init = au_wbr_create_init_mfsrr,
35073+ .fin = au_wbr_create_fin_mfs
1facf9fc 35074+ }
35075+};
7f207e10 35076diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
eca34b5c 35077--- /usr/share/empty/fs/aufs/whout.c 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 35078+++ linux/fs/aufs/whout.c 2022-11-05 23:02:18.972555950 +0100
758e9dad 35079@@ -0,0 +1,1072 @@
cd7a4cd9 35080+// SPDX-License-Identifier: GPL-2.0
1facf9fc 35081+/*
0dcfbb52 35082+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 35083+ *
0dcfbb52 35084+ * This program is free software; you can redistribute it and/or modify
1facf9fc 35085+ * it under the terms of the GNU General Public License as published by
35086+ * the Free Software Foundation; either version 2 of the License, or
35087+ * (at your option) any later version.
dece6358
AM
35088+ *
35089+ * This program is distributed in the hope that it will be useful,
35090+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
35091+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35092+ * GNU General Public License for more details.
35093+ *
35094+ * You should have received a copy of the GNU General Public License
523b37e3 35095+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 35096+ */
35097+
35098+/*
35099+ * whiteout for logical deletion and opaque directory
35100+ */
35101+
1facf9fc 35102+#include "aufs.h"
35103+
cd7a4cd9 35104+#define WH_MASK 0444
1facf9fc 35105+
35106+/*
35107+ * If a directory contains this file, then it is opaque. We start with the
35108+ * .wh. flag so that it is blocked by lookup.
35109+ */
0c3ec466
AM
35110+static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
35111+ sizeof(AUFS_WH_DIROPQ) - 1);
1facf9fc 35112+
35113+/*
35114+ * generate whiteout name, which is NOT terminated by NULL.
35115+ * @name: original d_name.name
35116+ * @len: original d_name.len
35117+ * @wh: whiteout qstr
35118+ * returns zero when succeeds, otherwise error.
35119+ * succeeded value as wh->name should be freed by kfree().
35120+ */
35121+int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
35122+{
35123+ char *p;
35124+
35125+ if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
35126+ return -ENAMETOOLONG;
35127+
35128+ wh->len = name->len + AUFS_WH_PFX_LEN;
35129+ p = kmalloc(wh->len, GFP_NOFS);
35130+ wh->name = p;
35131+ if (p) {
35132+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35133+ memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
35134+ /* smp_mb(); */
35135+ return 0;
35136+ }
35137+ return -ENOMEM;
35138+}
35139+
35140+/* ---------------------------------------------------------------------- */
35141+
35142+/*
758e9dad 35143+ * test if the @wh_name exists under @h_ppath.
1facf9fc 35144+ * @try_sio specifies the necessary of super-io.
35145+ */
758e9dad 35146+int au_wh_test(struct user_namespace *h_userns, struct path *h_ppath,
0b2a12c6 35147+ struct qstr *wh_name, int try_sio)
1facf9fc 35148+{
35149+ int err;
35150+ struct dentry *wh_dentry;
1facf9fc 35151+
1facf9fc 35152+ if (!try_sio)
758e9dad 35153+ wh_dentry = vfsub_lkup_one(wh_name, h_ppath);
1facf9fc 35154+ else
758e9dad 35155+ wh_dentry = au_sio_lkup_one(h_userns, wh_name, h_ppath);
1facf9fc 35156+ err = PTR_ERR(wh_dentry);
2000de60
JR
35157+ if (IS_ERR(wh_dentry)) {
35158+ if (err == -ENAMETOOLONG)
35159+ err = 0;
1facf9fc 35160+ goto out;
2000de60 35161+ }
1facf9fc 35162+
35163+ err = 0;
5527c038 35164+ if (d_is_negative(wh_dentry))
1facf9fc 35165+ goto out_wh; /* success */
35166+
35167+ err = 1;
7e9cd9fe 35168+ if (d_is_reg(wh_dentry))
1facf9fc 35169+ goto out_wh; /* success */
35170+
35171+ err = -EIO;
523b37e3 35172+ AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
5527c038 35173+ wh_dentry, d_inode(wh_dentry)->i_mode);
1facf9fc 35174+
4f0767ce 35175+out_wh:
1facf9fc 35176+ dput(wh_dentry);
4f0767ce 35177+out:
1facf9fc 35178+ return err;
35179+}
35180+
35181+/*
758e9dad 35182+ * test if the @h_path->dentry sets opaque or not.
1facf9fc 35183+ */
758e9dad 35184+int au_diropq_test(struct user_namespace *h_userns, struct path *h_path)
1facf9fc 35185+{
35186+ int err;
35187+ struct inode *h_dir;
35188+
758e9dad
AM
35189+ h_dir = d_inode(h_path->dentry);
35190+ err = au_wh_test(h_userns, h_path, &diropq_name,
0b2a12c6 35191+ au_test_h_perm_sio(h_userns, h_dir, MAY_EXEC));
1facf9fc 35192+ return err;
35193+}
35194+
35195+/*
35196+ * returns a negative dentry whose name is unique and temporary.
35197+ */
35198+struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
35199+ struct qstr *prefix)
35200+{
1facf9fc 35201+ struct dentry *dentry;
35202+ int i;
027c5e7a 35203+ char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
4a4d8108 35204+ *name, *p;
027c5e7a 35205+ /* strict atomic_t is unnecessary here */
1facf9fc 35206+ static unsigned short cnt;
35207+ struct qstr qs;
758e9dad 35208+ struct path h_ppath;
0b2a12c6 35209+ struct user_namespace *h_userns;
1facf9fc 35210+
4a4d8108
AM
35211+ BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
35212+
1facf9fc 35213+ name = defname;
027c5e7a
AM
35214+ qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
35215+ if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
1facf9fc 35216+ dentry = ERR_PTR(-ENAMETOOLONG);
4a4d8108 35217+ if (unlikely(qs.len > NAME_MAX))
1facf9fc 35218+ goto out;
35219+ dentry = ERR_PTR(-ENOMEM);
35220+ name = kmalloc(qs.len + 1, GFP_NOFS);
35221+ if (unlikely(!name))
35222+ goto out;
35223+ }
35224+
35225+ /* doubly whiteout-ed */
35226+ memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
35227+ p = name + AUFS_WH_PFX_LEN * 2;
35228+ memcpy(p, prefix->name, prefix->len);
35229+ p += prefix->len;
35230+ *p++ = '.';
4a4d8108 35231+ AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
1facf9fc 35232+
758e9dad
AM
35233+ h_ppath.dentry = h_parent;
35234+ h_ppath.mnt = au_br_mnt(br);
0b2a12c6 35235+ h_userns = au_br_userns(br);
1facf9fc 35236+ qs.name = name;
35237+ for (i = 0; i < 3; i++) {
b752ccd1 35238+ sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
758e9dad 35239+ dentry = au_sio_lkup_one(h_userns, &qs, &h_ppath);
5527c038 35240+ if (IS_ERR(dentry) || d_is_negative(dentry))
1facf9fc 35241+ goto out_name;
35242+ dput(dentry);
35243+ }
0c3ec466 35244+ /* pr_warn("could not get random name\n"); */
1facf9fc 35245+ dentry = ERR_PTR(-EEXIST);
35246+ AuDbg("%.*s\n", AuLNPair(&qs));
35247+ BUG();
35248+
4f0767ce 35249+out_name:
1facf9fc 35250+ if (name != defname)
9f237c51 35251+ au_kfree_try_rcu(name);
4f0767ce 35252+out:
4a4d8108 35253+ AuTraceErrPtr(dentry);
1facf9fc 35254+ return dentry;
1facf9fc 35255+}
35256+
35257+/*
35258+ * rename the @h_dentry on @br to the whiteouted temporary name.
35259+ */
35260+int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
35261+{
35262+ int err;
35263+ struct path h_path = {
86dc4139 35264+ .mnt = au_br_mnt(br)
1facf9fc 35265+ };
523b37e3 35266+ struct inode *h_dir, *delegated;
1facf9fc 35267+ struct dentry *h_parent;
35268+
35269+ h_parent = h_dentry->d_parent; /* dir inode is locked */
5527c038 35270+ h_dir = d_inode(h_parent);
1facf9fc 35271+ IMustLock(h_dir);
35272+
35273+ h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
35274+ err = PTR_ERR(h_path.dentry);
35275+ if (IS_ERR(h_path.dentry))
35276+ goto out;
35277+
35278+ /* under the same dir, no need to lock_rename() */
523b37e3 35279+ delegated = NULL;
f2c43d5f
AM
35280+ err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
35281+ /*flags*/0);
1facf9fc 35282+ AuTraceErr(err);
523b37e3
AM
35283+ if (unlikely(err == -EWOULDBLOCK)) {
35284+ pr_warn("cannot retry for NFSv4 delegation"
35285+ " for an internal rename\n");
35286+ iput(delegated);
35287+ }
1facf9fc 35288+ dput(h_path.dentry);
35289+
4f0767ce 35290+out:
4a4d8108 35291+ AuTraceErr(err);
1facf9fc 35292+ return err;
35293+}
35294+
35295+/* ---------------------------------------------------------------------- */
35296+/*
35297+ * functions for removing a whiteout
35298+ */
35299+
35300+static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
35301+{
523b37e3
AM
35302+ int err, force;
35303+ struct inode *delegated;
1facf9fc 35304+
35305+ /*
35306+ * forces superio when the dir has a sticky bit.
35307+ * this may be a violation of unix fs semantics.
35308+ */
35309+ force = (h_dir->i_mode & S_ISVTX)
5527c038 35310+ && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
523b37e3
AM
35311+ delegated = NULL;
35312+ err = vfsub_unlink(h_dir, h_path, &delegated, force);
35313+ if (unlikely(err == -EWOULDBLOCK)) {
35314+ pr_warn("cannot retry for NFSv4 delegation"
35315+ " for an internal unlink\n");
35316+ iput(delegated);
35317+ }
35318+ return err;
1facf9fc 35319+}
35320+
35321+int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35322+ struct dentry *dentry)
35323+{
35324+ int err;
35325+
35326+ err = do_unlink_wh(h_dir, h_path);
35327+ if (!err && dentry)
35328+ au_set_dbwh(dentry, -1);
35329+
35330+ return err;
35331+}
35332+
758e9dad 35333+static int unlink_wh_name(struct path *h_ppath, struct qstr *wh)
1facf9fc 35334+{
35335+ int err;
758e9dad 35336+ struct path h_path;
1facf9fc 35337+
35338+ err = 0;
758e9dad 35339+ h_path.dentry = vfsub_lkup_one(wh, h_ppath);
1facf9fc 35340+ if (IS_ERR(h_path.dentry))
35341+ err = PTR_ERR(h_path.dentry);
35342+ else {
758e9dad
AM
35343+ if (d_is_reg(h_path.dentry)) {
35344+ h_path.mnt = h_ppath->mnt;
35345+ err = do_unlink_wh(d_inode(h_ppath->dentry), &h_path);
35346+ }
1facf9fc 35347+ dput(h_path.dentry);
35348+ }
35349+
35350+ return err;
35351+}
35352+
35353+/* ---------------------------------------------------------------------- */
35354+/*
35355+ * initialize/clean whiteout for a branch
35356+ */
35357+
35358+static void au_wh_clean(struct inode *h_dir, struct path *whpath,
35359+ const int isdir)
35360+{
35361+ int err;
523b37e3 35362+ struct inode *delegated;
1facf9fc 35363+
5527c038 35364+ if (d_is_negative(whpath->dentry))
1facf9fc 35365+ return;
35366+
86dc4139
AM
35367+ if (isdir)
35368+ err = vfsub_rmdir(h_dir, whpath);
523b37e3
AM
35369+ else {
35370+ delegated = NULL;
35371+ err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
35372+ if (unlikely(err == -EWOULDBLOCK)) {
35373+ pr_warn("cannot retry for NFSv4 delegation"
35374+ " for an internal unlink\n");
35375+ iput(delegated);
35376+ }
35377+ }
1facf9fc 35378+ if (unlikely(err))
523b37e3
AM
35379+ pr_warn("failed removing %pd (%d), ignored.\n",
35380+ whpath->dentry, err);
1facf9fc 35381+}
35382+
35383+static int test_linkable(struct dentry *h_root)
35384+{
5527c038 35385+ struct inode *h_dir = d_inode(h_root);
1facf9fc 35386+
35387+ if (h_dir->i_op->link)
35388+ return 0;
35389+
523b37e3
AM
35390+ pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
35391+ h_root, au_sbtype(h_root->d_sb));
43982f53 35392+ return -ENOSYS; /* the branch doesn't have its ->link() */
1facf9fc 35393+}
35394+
35395+/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
35396+static int au_whdir(struct inode *h_dir, struct path *path)
35397+{
35398+ int err;
35399+
35400+ err = -EEXIST;
5527c038 35401+ if (d_is_negative(path->dentry)) {
cd7a4cd9 35402+ int mode = 0700;
1facf9fc 35403+
35404+ if (au_test_nfs(path->dentry->d_sb))
cd7a4cd9 35405+ mode |= 0111;
86dc4139 35406+ err = vfsub_mkdir(h_dir, path, mode);
2000de60 35407+ } else if (d_is_dir(path->dentry))
1facf9fc 35408+ err = 0;
35409+ else
523b37e3 35410+ pr_err("unknown %pd exists\n", path->dentry);
1facf9fc 35411+
35412+ return err;
35413+}
35414+
35415+struct au_wh_base {
35416+ const struct qstr *name;
35417+ struct dentry *dentry;
35418+};
35419+
35420+static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
35421+ struct path *h_path)
35422+{
35423+ h_path->dentry = base[AuBrWh_BASE].dentry;
35424+ au_wh_clean(h_dir, h_path, /*isdir*/0);
35425+ h_path->dentry = base[AuBrWh_PLINK].dentry;
35426+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35427+ h_path->dentry = base[AuBrWh_ORPH].dentry;
35428+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35429+}
35430+
35431+/*
35432+ * returns tri-state,
c1595e42 35433+ * minus: error, caller should print the message
acd2b654 35434+ * zero: success
c1595e42 35435+ * plus: error, caller should NOT print the message
1facf9fc 35436+ */
35437+static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
35438+ int do_plink, struct au_wh_base base[],
35439+ struct path *h_path)
35440+{
35441+ int err;
35442+ struct inode *h_dir;
35443+
5527c038 35444+ h_dir = d_inode(h_root);
1facf9fc 35445+ h_path->dentry = base[AuBrWh_BASE].dentry;
35446+ au_wh_clean(h_dir, h_path, /*isdir*/0);
35447+ h_path->dentry = base[AuBrWh_PLINK].dentry;
35448+ if (do_plink) {
35449+ err = test_linkable(h_root);
35450+ if (unlikely(err)) {
35451+ err = 1;
35452+ goto out;
35453+ }
35454+
35455+ err = au_whdir(h_dir, h_path);
35456+ if (unlikely(err))
35457+ goto out;
35458+ wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35459+ } else
35460+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35461+ h_path->dentry = base[AuBrWh_ORPH].dentry;
35462+ err = au_whdir(h_dir, h_path);
35463+ if (unlikely(err))
35464+ goto out;
35465+ wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35466+
4f0767ce 35467+out:
1facf9fc 35468+ return err;
35469+}
35470+
35471+/*
35472+ * for the moment, aufs supports the branch filesystem which does not support
35473+ * link(2). testing on FAT which does not support i_op->setattr() fully either,
35474+ * copyup failed. finally, such filesystem will not be used as the writable
35475+ * branch.
35476+ *
35477+ * returns tri-state, see above.
35478+ */
35479+static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
35480+ int do_plink, struct au_wh_base base[],
35481+ struct path *h_path)
35482+{
35483+ int err;
35484+ struct inode *h_dir;
35485+
1308ab2a 35486+ WbrWhMustWriteLock(wbr);
35487+
1facf9fc 35488+ err = test_linkable(h_root);
35489+ if (unlikely(err)) {
35490+ err = 1;
35491+ goto out;
35492+ }
35493+
35494+ /*
35495+ * todo: should this create be done in /sbin/mount.aufs helper?
35496+ */
35497+ err = -EEXIST;
5527c038
JR
35498+ h_dir = d_inode(h_root);
35499+ if (d_is_negative(base[AuBrWh_BASE].dentry)) {
86dc4139
AM
35500+ h_path->dentry = base[AuBrWh_BASE].dentry;
35501+ err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
7e9cd9fe 35502+ } else if (d_is_reg(base[AuBrWh_BASE].dentry))
1facf9fc 35503+ err = 0;
35504+ else
523b37e3 35505+ pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
1facf9fc 35506+ if (unlikely(err))
35507+ goto out;
35508+
35509+ h_path->dentry = base[AuBrWh_PLINK].dentry;
35510+ if (do_plink) {
35511+ err = au_whdir(h_dir, h_path);
35512+ if (unlikely(err))
35513+ goto out;
35514+ wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35515+ } else
35516+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35517+ wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
35518+
35519+ h_path->dentry = base[AuBrWh_ORPH].dentry;
35520+ err = au_whdir(h_dir, h_path);
35521+ if (unlikely(err))
35522+ goto out;
35523+ wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35524+
4f0767ce 35525+out:
1facf9fc 35526+ return err;
35527+}
35528+
35529+/*
35530+ * initialize the whiteout base file/dir for @br.
35531+ */
86dc4139 35532+int au_wh_init(struct au_branch *br, struct super_block *sb)
1facf9fc 35533+{
35534+ int err, i;
35535+ const unsigned char do_plink
35536+ = !!au_opt_test(au_mntflags(sb), PLINK);
1facf9fc 35537+ struct inode *h_dir;
86dc4139
AM
35538+ struct path path = br->br_path;
35539+ struct dentry *h_root = path.dentry;
1facf9fc 35540+ struct au_wbr *wbr = br->br_wbr;
35541+ static const struct qstr base_name[] = {
0c3ec466
AM
35542+ [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
35543+ sizeof(AUFS_BASE_NAME) - 1),
35544+ [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
35545+ sizeof(AUFS_PLINKDIR_NAME) - 1),
35546+ [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
35547+ sizeof(AUFS_ORPHDIR_NAME) - 1)
1facf9fc 35548+ };
35549+ struct au_wh_base base[] = {
35550+ [AuBrWh_BASE] = {
35551+ .name = base_name + AuBrWh_BASE,
35552+ .dentry = NULL
35553+ },
35554+ [AuBrWh_PLINK] = {
35555+ .name = base_name + AuBrWh_PLINK,
35556+ .dentry = NULL
35557+ },
35558+ [AuBrWh_ORPH] = {
35559+ .name = base_name + AuBrWh_ORPH,
35560+ .dentry = NULL
35561+ }
35562+ };
35563+
1308ab2a 35564+ if (wbr)
35565+ WbrWhMustWriteLock(wbr);
1facf9fc 35566+
1facf9fc 35567+ for (i = 0; i < AuBrWh_Last; i++) {
35568+ /* doubly whiteouted */
35569+ struct dentry *d;
35570+
35571+ d = au_wh_lkup(h_root, (void *)base[i].name, br);
35572+ err = PTR_ERR(d);
35573+ if (IS_ERR(d))
35574+ goto out;
35575+
35576+ base[i].dentry = d;
35577+ AuDebugOn(wbr
35578+ && wbr->wbr_wh[i]
35579+ && wbr->wbr_wh[i] != base[i].dentry);
35580+ }
35581+
35582+ if (wbr)
35583+ for (i = 0; i < AuBrWh_Last; i++) {
35584+ dput(wbr->wbr_wh[i]);
35585+ wbr->wbr_wh[i] = NULL;
35586+ }
35587+
35588+ err = 0;
1e00d052 35589+ if (!au_br_writable(br->br_perm)) {
5527c038 35590+ h_dir = d_inode(h_root);
1facf9fc 35591+ au_wh_init_ro(h_dir, base, &path);
1e00d052 35592+ } else if (!au_br_wh_linkable(br->br_perm)) {
1facf9fc 35593+ err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
35594+ if (err > 0)
35595+ goto out;
35596+ else if (err)
35597+ goto out_err;
1e00d052 35598+ } else {
1facf9fc 35599+ err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
35600+ if (err > 0)
35601+ goto out;
35602+ else if (err)
35603+ goto out_err;
1facf9fc 35604+ }
35605+ goto out; /* success */
35606+
4f0767ce 35607+out_err:
523b37e3
AM
35608+ pr_err("an error(%d) on the writable branch %pd(%s)\n",
35609+ err, h_root, au_sbtype(h_root->d_sb));
4f0767ce 35610+out:
1facf9fc 35611+ for (i = 0; i < AuBrWh_Last; i++)
35612+ dput(base[i].dentry);
35613+ return err;
35614+}
35615+
35616+/* ---------------------------------------------------------------------- */
35617+/*
35618+ * whiteouts are all hard-linked usually.
35619+ * when its link count reaches a ceiling, we create a new whiteout base
35620+ * asynchronously.
35621+ */
35622+
35623+struct reinit_br_wh {
35624+ struct super_block *sb;
35625+ struct au_branch *br;
35626+};
35627+
35628+static void reinit_br_wh(void *arg)
35629+{
35630+ int err;
35631+ aufs_bindex_t bindex;
35632+ struct path h_path;
35633+ struct reinit_br_wh *a = arg;
35634+ struct au_wbr *wbr;
523b37e3 35635+ struct inode *dir, *delegated;
1facf9fc 35636+ struct dentry *h_root;
35637+ struct au_hinode *hdir;
35638+
35639+ err = 0;
35640+ wbr = a->br->br_wbr;
35641+ /* big aufs lock */
35642+ si_noflush_write_lock(a->sb);
35643+ if (!au_br_writable(a->br->br_perm))
35644+ goto out;
35645+ bindex = au_br_index(a->sb, a->br->br_id);
35646+ if (unlikely(bindex < 0))
35647+ goto out;
35648+
1308ab2a 35649+ di_read_lock_parent(a->sb->s_root, AuLock_IR);
5527c038 35650+ dir = d_inode(a->sb->s_root);
1facf9fc 35651+ hdir = au_hi(dir, bindex);
35652+ h_root = au_h_dptr(a->sb->s_root, bindex);
86dc4139 35653+ AuDebugOn(h_root != au_br_dentry(a->br));
1facf9fc 35654+
5afbbe0d 35655+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 35656+ wbr_wh_write_lock(wbr);
35657+ err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
35658+ h_root, a->br);
35659+ if (!err) {
86dc4139
AM
35660+ h_path.dentry = wbr->wbr_whbase;
35661+ h_path.mnt = au_br_mnt(a->br);
523b37e3
AM
35662+ delegated = NULL;
35663+ err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
35664+ /*force*/0);
35665+ if (unlikely(err == -EWOULDBLOCK)) {
35666+ pr_warn("cannot retry for NFSv4 delegation"
35667+ " for an internal unlink\n");
35668+ iput(delegated);
35669+ }
1facf9fc 35670+ } else {
523b37e3 35671+ pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
1facf9fc 35672+ err = 0;
35673+ }
35674+ dput(wbr->wbr_whbase);
35675+ wbr->wbr_whbase = NULL;
35676+ if (!err)
86dc4139 35677+ err = au_wh_init(a->br, a->sb);
1facf9fc 35678+ wbr_wh_write_unlock(wbr);
5afbbe0d 35679+ au_hn_inode_unlock(hdir);
1308ab2a 35680+ di_read_unlock(a->sb->s_root, AuLock_IR);
076b876e
AM
35681+ if (!err)
35682+ au_fhsm_wrote(a->sb, bindex, /*force*/0);
1facf9fc 35683+
4f0767ce 35684+out:
1facf9fc 35685+ if (wbr)
35686+ atomic_dec(&wbr->wbr_wh_running);
acd2b654 35687+ au_lcnt_dec(&a->br->br_count);
1facf9fc 35688+ si_write_unlock(a->sb);
027c5e7a 35689+ au_nwt_done(&au_sbi(a->sb)->si_nowait);
9f237c51 35690+ au_kfree_rcu(a);
1facf9fc 35691+ if (unlikely(err))
35692+ AuIOErr("err %d\n", err);
35693+}
35694+
35695+static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
35696+{
35697+ int do_dec, wkq_err;
35698+ struct reinit_br_wh *arg;
35699+
35700+ do_dec = 1;
35701+ if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
35702+ goto out;
35703+
35704+ /* ignore ENOMEM */
35705+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
35706+ if (arg) {
35707+ /*
35708+ * dec(wh_running), kfree(arg) and dec(br_count)
35709+ * in reinit function
35710+ */
35711+ arg->sb = sb;
35712+ arg->br = br;
acd2b654 35713+ au_lcnt_inc(&br->br_count);
53392da6 35714+ wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
1facf9fc 35715+ if (unlikely(wkq_err)) {
35716+ atomic_dec(&br->br_wbr->wbr_wh_running);
acd2b654 35717+ au_lcnt_dec(&br->br_count);
9f237c51 35718+ au_kfree_rcu(arg);
1facf9fc 35719+ }
35720+ do_dec = 0;
35721+ }
35722+
4f0767ce 35723+out:
1facf9fc 35724+ if (do_dec)
35725+ atomic_dec(&br->br_wbr->wbr_wh_running);
35726+}
35727+
35728+/* ---------------------------------------------------------------------- */
35729+
35730+/*
35731+ * create the whiteout @wh.
35732+ */
35733+static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
35734+ struct dentry *wh)
35735+{
35736+ int err;
35737+ struct path h_path = {
35738+ .dentry = wh
35739+ };
35740+ struct au_branch *br;
35741+ struct au_wbr *wbr;
35742+ struct dentry *h_parent;
523b37e3 35743+ struct inode *h_dir, *delegated;
1facf9fc 35744+
35745+ h_parent = wh->d_parent; /* dir inode is locked */
5527c038 35746+ h_dir = d_inode(h_parent);
1facf9fc 35747+ IMustLock(h_dir);
35748+
35749+ br = au_sbr(sb, bindex);
86dc4139 35750+ h_path.mnt = au_br_mnt(br);
1facf9fc 35751+ wbr = br->br_wbr;
35752+ wbr_wh_read_lock(wbr);
35753+ if (wbr->wbr_whbase) {
523b37e3
AM
35754+ delegated = NULL;
35755+ err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
35756+ if (unlikely(err == -EWOULDBLOCK)) {
35757+ pr_warn("cannot retry for NFSv4 delegation"
35758+ " for an internal link\n");
35759+ iput(delegated);
35760+ }
1facf9fc 35761+ if (!err || err != -EMLINK)
35762+ goto out;
35763+
35764+ /* link count full. re-initialize br_whbase. */
35765+ kick_reinit_br_wh(sb, br);
35766+ }
35767+
35768+ /* return this error in this context */
b4510431 35769+ err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
076b876e
AM
35770+ if (!err)
35771+ au_fhsm_wrote(sb, bindex, /*force*/0);
1facf9fc 35772+
4f0767ce 35773+out:
1facf9fc 35774+ wbr_wh_read_unlock(wbr);
35775+ return err;
35776+}
35777+
35778+/* ---------------------------------------------------------------------- */
35779+
35780+/*
35781+ * create or remove the diropq.
35782+ */
35783+static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
35784+ unsigned int flags)
35785+{
758e9dad 35786+ struct dentry *opq_dentry;
1facf9fc 35787+ struct super_block *sb;
35788+ struct au_branch *br;
758e9dad 35789+ struct path h_path;
1facf9fc 35790+ int err;
35791+
35792+ sb = dentry->d_sb;
35793+ br = au_sbr(sb, bindex);
758e9dad
AM
35794+ h_path.dentry = au_h_dptr(dentry, bindex);
35795+ h_path.mnt = au_br_mnt(br);
35796+ opq_dentry = vfsub_lkup_one(&diropq_name, &h_path);
1facf9fc 35797+ if (IS_ERR(opq_dentry))
35798+ goto out;
35799+
35800+ if (au_ftest_diropq(flags, CREATE)) {
35801+ err = link_or_create_wh(sb, bindex, opq_dentry);
35802+ if (!err) {
35803+ au_set_dbdiropq(dentry, bindex);
35804+ goto out; /* success */
35805+ }
35806+ } else {
758e9dad
AM
35807+ h_path.dentry = opq_dentry;
35808+ err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &h_path);
1facf9fc 35809+ if (!err)
35810+ au_set_dbdiropq(dentry, -1);
35811+ }
35812+ dput(opq_dentry);
35813+ opq_dentry = ERR_PTR(err);
35814+
4f0767ce 35815+out:
1facf9fc 35816+ return opq_dentry;
35817+}
35818+
35819+struct do_diropq_args {
35820+ struct dentry **errp;
35821+ struct dentry *dentry;
35822+ aufs_bindex_t bindex;
35823+ unsigned int flags;
35824+};
35825+
35826+static void call_do_diropq(void *args)
35827+{
35828+ struct do_diropq_args *a = args;
35829+ *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
35830+}
35831+
35832+struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35833+ unsigned int flags)
35834+{
35835+ struct dentry *diropq, *h_dentry;
0b2a12c6 35836+ struct user_namespace *h_userns;
1facf9fc 35837+
0b2a12c6 35838+ h_userns = au_sbr_userns(dentry->d_sb, bindex);
1facf9fc 35839+ h_dentry = au_h_dptr(dentry, bindex);
0b2a12c6
JR
35840+ if (!au_test_h_perm_sio(h_userns, d_inode(h_dentry),
35841+ MAY_EXEC | MAY_WRITE))
1facf9fc 35842+ diropq = do_diropq(dentry, bindex, flags);
35843+ else {
35844+ int wkq_err;
35845+ struct do_diropq_args args = {
35846+ .errp = &diropq,
35847+ .dentry = dentry,
35848+ .bindex = bindex,
35849+ .flags = flags
35850+ };
35851+
35852+ wkq_err = au_wkq_wait(call_do_diropq, &args);
35853+ if (unlikely(wkq_err))
35854+ diropq = ERR_PTR(wkq_err);
35855+ }
35856+
35857+ return diropq;
35858+}
35859+
35860+/* ---------------------------------------------------------------------- */
35861+
35862+/*
35863+ * lookup whiteout dentry.
35864+ * @h_parent: lower parent dentry which must exist and be locked
35865+ * @base_name: name of dentry which will be whiteouted
35866+ * returns dentry for whiteout.
35867+ */
35868+struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35869+ struct au_branch *br)
35870+{
35871+ int err;
35872+ struct qstr wh_name;
35873+ struct dentry *wh_dentry;
758e9dad 35874+ struct path h_path;
1facf9fc 35875+
35876+ err = au_wh_name_alloc(&wh_name, base_name);
35877+ wh_dentry = ERR_PTR(err);
35878+ if (!err) {
758e9dad
AM
35879+ h_path.dentry = h_parent;
35880+ h_path.mnt = au_br_mnt(br);
35881+ wh_dentry = vfsub_lkup_one(&wh_name, &h_path);
9f237c51 35882+ au_kfree_try_rcu(wh_name.name);
1facf9fc 35883+ }
35884+ return wh_dentry;
35885+}
35886+
35887+/*
35888+ * link/create a whiteout for @dentry on @bindex.
35889+ */
35890+struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35891+ struct dentry *h_parent)
35892+{
35893+ struct dentry *wh_dentry;
35894+ struct super_block *sb;
35895+ int err;
35896+
35897+ sb = dentry->d_sb;
35898+ wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
5527c038 35899+ if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
1facf9fc 35900+ err = link_or_create_wh(sb, bindex, wh_dentry);
076b876e 35901+ if (!err) {
1facf9fc 35902+ au_set_dbwh(dentry, bindex);
076b876e
AM
35903+ au_fhsm_wrote(sb, bindex, /*force*/0);
35904+ } else {
1facf9fc 35905+ dput(wh_dentry);
35906+ wh_dentry = ERR_PTR(err);
35907+ }
35908+ }
35909+
35910+ return wh_dentry;
35911+}
35912+
35913+/* ---------------------------------------------------------------------- */
35914+
35915+/* Delete all whiteouts in this directory on branch bindex. */
758e9dad
AM
35916+static int del_wh_children(struct path *h_path, struct au_nhash *whlist,
35917+ aufs_bindex_t bindex)
1facf9fc 35918+{
35919+ int err;
35920+ unsigned long ul, n;
35921+ struct qstr wh_name;
35922+ char *p;
35923+ struct hlist_head *head;
c06a8ce3 35924+ struct au_vdir_wh *pos;
1facf9fc 35925+ struct au_vdir_destr *str;
35926+
35927+ err = -ENOMEM;
537831f9 35928+ p = (void *)__get_free_page(GFP_NOFS);
1facf9fc 35929+ wh_name.name = p;
35930+ if (unlikely(!wh_name.name))
35931+ goto out;
35932+
35933+ err = 0;
35934+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35935+ p += AUFS_WH_PFX_LEN;
35936+ n = whlist->nh_num;
35937+ head = whlist->nh_head;
35938+ for (ul = 0; !err && ul < n; ul++, head++) {
c06a8ce3
AM
35939+ hlist_for_each_entry(pos, head, wh_hash) {
35940+ if (pos->wh_bindex != bindex)
1facf9fc 35941+ continue;
35942+
c06a8ce3 35943+ str = &pos->wh_str;
1facf9fc 35944+ if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
35945+ memcpy(p, str->name, str->len);
35946+ wh_name.len = AUFS_WH_PFX_LEN + str->len;
758e9dad 35947+ err = unlink_wh_name(h_path, &wh_name);
1facf9fc 35948+ if (!err)
35949+ continue;
35950+ break;
35951+ }
35952+ AuIOErr("whiteout name too long %.*s\n",
35953+ str->len, str->name);
35954+ err = -EIO;
35955+ break;
35956+ }
35957+ }
1c60b727 35958+ free_page((unsigned long)wh_name.name);
1facf9fc 35959+
4f0767ce 35960+out:
1facf9fc 35961+ return err;
35962+}
35963+
35964+struct del_wh_children_args {
35965+ int *errp;
758e9dad 35966+ struct path *h_path;
1308ab2a 35967+ struct au_nhash *whlist;
1facf9fc 35968+ aufs_bindex_t bindex;
1facf9fc 35969+};
35970+
35971+static void call_del_wh_children(void *args)
35972+{
35973+ struct del_wh_children_args *a = args;
758e9dad 35974+ *a->errp = del_wh_children(a->h_path, a->whlist, a->bindex);
1facf9fc 35975+}
35976+
35977+/* ---------------------------------------------------------------------- */
35978+
35979+struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
35980+{
35981+ struct au_whtmp_rmdir *whtmp;
dece6358 35982+ int err;
1308ab2a 35983+ unsigned int rdhash;
dece6358
AM
35984+
35985+ SiMustAnyLock(sb);
1facf9fc 35986+
be52b249 35987+ whtmp = kzalloc(sizeof(*whtmp), gfp);
dece6358
AM
35988+ if (unlikely(!whtmp)) {
35989+ whtmp = ERR_PTR(-ENOMEM);
1facf9fc 35990+ goto out;
dece6358 35991+ }
1facf9fc 35992+
1308ab2a 35993+ /* no estimation for dir size */
35994+ rdhash = au_sbi(sb)->si_rdhash;
35995+ if (!rdhash)
35996+ rdhash = AUFS_RDHASH_DEF;
35997+ err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
35998+ if (unlikely(err)) {
9f237c51 35999+ au_kfree_rcu(whtmp);
1308ab2a 36000+ whtmp = ERR_PTR(err);
36001+ }
dece6358 36002+
4f0767ce 36003+out:
dece6358 36004+ return whtmp;
1facf9fc 36005+}
36006+
36007+void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
36008+{
027c5e7a 36009+ if (whtmp->br)
acd2b654 36010+ au_lcnt_dec(&whtmp->br->br_count);
1facf9fc 36011+ dput(whtmp->wh_dentry);
36012+ iput(whtmp->dir);
dece6358 36013+ au_nhash_wh_free(&whtmp->whlist);
9f237c51 36014+ au_kfree_rcu(whtmp);
1facf9fc 36015+}
36016+
36017+/*
36018+ * rmdir the whiteouted temporary named dir @h_dentry.
36019+ * @whlist: whiteouted children.
36020+ */
36021+int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
36022+ struct dentry *wh_dentry, struct au_nhash *whlist)
36023+{
36024+ int err;
2000de60 36025+ unsigned int h_nlink;
758e9dad 36026+ struct path wh_path;
1facf9fc 36027+ struct inode *wh_inode, *h_dir;
36028+ struct au_branch *br;
0b2a12c6 36029+ struct user_namespace *h_userns;
1facf9fc 36030+
5527c038 36031+ h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
1facf9fc 36032+ IMustLock(h_dir);
36033+
36034+ br = au_sbr(dir->i_sb, bindex);
758e9dad
AM
36035+ wh_path.dentry = wh_dentry;
36036+ wh_path.mnt = au_br_mnt(br);
0b2a12c6 36037+ h_userns = au_br_userns(br);
5527c038 36038+ wh_inode = d_inode(wh_dentry);
febd17d6 36039+ inode_lock_nested(wh_inode, AuLsc_I_CHILD);
1facf9fc 36040+
36041+ /*
36042+ * someone else might change some whiteouts while we were sleeping.
36043+ * it means this whlist may have an obsoleted entry.
36044+ */
0b2a12c6 36045+ if (!au_test_h_perm_sio(h_userns, wh_inode, MAY_EXEC | MAY_WRITE))
758e9dad 36046+ err = del_wh_children(&wh_path, whlist, bindex);
1facf9fc 36047+ else {
36048+ int wkq_err;
36049+ struct del_wh_children_args args = {
36050+ .errp = &err,
758e9dad 36051+ .h_path = &wh_path,
1308ab2a 36052+ .whlist = whlist,
758e9dad 36053+ .bindex = bindex
1facf9fc 36054+ };
36055+
36056+ wkq_err = au_wkq_wait(call_del_wh_children, &args);
36057+ if (unlikely(wkq_err))
36058+ err = wkq_err;
36059+ }
febd17d6 36060+ inode_unlock(wh_inode);
1facf9fc 36061+
36062+ if (!err) {
2000de60 36063+ h_nlink = h_dir->i_nlink;
758e9dad 36064+ err = vfsub_rmdir(h_dir, &wh_path);
2000de60
JR
36065+ /* some fs doesn't change the parent nlink in some cases */
36066+ h_nlink -= h_dir->i_nlink;
1facf9fc 36067+ }
36068+
36069+ if (!err) {
5afbbe0d 36070+ if (au_ibtop(dir) == bindex) {
7f207e10 36071+ /* todo: dir->i_mutex is necessary */
1facf9fc 36072+ au_cpup_attr_timesizes(dir);
2000de60
JR
36073+ if (h_nlink)
36074+ vfsub_drop_nlink(dir);
1facf9fc 36075+ }
36076+ return 0; /* success */
36077+ }
36078+
523b37e3 36079+ pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
1facf9fc 36080+ return err;
36081+}
36082+
36083+static void call_rmdir_whtmp(void *args)
36084+{
36085+ int err;
e49829fe 36086+ aufs_bindex_t bindex;
1facf9fc 36087+ struct au_whtmp_rmdir *a = args;
36088+ struct super_block *sb;
36089+ struct dentry *h_parent;
36090+ struct inode *h_dir;
1facf9fc 36091+ struct au_hinode *hdir;
36092+
36093+ /* rmdir by nfsd may cause deadlock with this i_mutex */
febd17d6 36094+ /* inode_lock(a->dir); */
e49829fe 36095+ err = -EROFS;
1facf9fc 36096+ sb = a->dir->i_sb;
e49829fe
JR
36097+ si_read_lock(sb, !AuLock_FLUSH);
36098+ if (!au_br_writable(a->br->br_perm))
36099+ goto out;
36100+ bindex = au_br_index(sb, a->br->br_id);
36101+ if (unlikely(bindex < 0))
1facf9fc 36102+ goto out;
36103+
36104+ err = -EIO;
1facf9fc 36105+ ii_write_lock_parent(a->dir);
36106+ h_parent = dget_parent(a->wh_dentry);
5527c038 36107+ h_dir = d_inode(h_parent);
e49829fe 36108+ hdir = au_hi(a->dir, bindex);
86dc4139
AM
36109+ err = vfsub_mnt_want_write(au_br_mnt(a->br));
36110+ if (unlikely(err))
36111+ goto out_mnt;
5afbbe0d 36112+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
e49829fe
JR
36113+ err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
36114+ a->br);
86dc4139
AM
36115+ if (!err)
36116+ err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
5afbbe0d 36117+ au_hn_inode_unlock(hdir);
86dc4139
AM
36118+ vfsub_mnt_drop_write(au_br_mnt(a->br));
36119+
36120+out_mnt:
1facf9fc 36121+ dput(h_parent);
36122+ ii_write_unlock(a->dir);
4f0767ce 36123+out:
febd17d6 36124+ /* inode_unlock(a->dir); */
1facf9fc 36125+ au_whtmp_rmdir_free(a);
027c5e7a
AM
36126+ si_read_unlock(sb);
36127+ au_nwt_done(&au_sbi(sb)->si_nowait);
1facf9fc 36128+ if (unlikely(err))
36129+ AuIOErr("err %d\n", err);
36130+}
36131+
36132+void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
36133+ struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
36134+{
36135+ int wkq_err;
e49829fe 36136+ struct super_block *sb;
1facf9fc 36137+
36138+ IMustLock(dir);
36139+
36140+ /* all post-process will be done in do_rmdir_whtmp(). */
e49829fe 36141+ sb = dir->i_sb;
1facf9fc 36142+ args->dir = au_igrab(dir);
e49829fe 36143+ args->br = au_sbr(sb, bindex);
acd2b654 36144+ au_lcnt_inc(&args->br->br_count);
1facf9fc 36145+ args->wh_dentry = dget(wh_dentry);
53392da6 36146+ wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
1facf9fc 36147+ if (unlikely(wkq_err)) {
523b37e3 36148+ pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
1facf9fc 36149+ au_whtmp_rmdir_free(args);
36150+ }
36151+}
7f207e10 36152diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
eca34b5c 36153--- /usr/share/empty/fs/aufs/whout.h 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 36154+++ linux/fs/aufs/whout.h 2022-11-05 23:02:18.972555950 +0100
0b2a12c6 36155@@ -0,0 +1,87 @@
062440b3 36156+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 36157+/*
0dcfbb52 36158+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 36159+ *
0dcfbb52 36160+ * This program is free software; you can redistribute it and/or modify
1facf9fc 36161+ * it under the terms of the GNU General Public License as published by
36162+ * the Free Software Foundation; either version 2 of the License, or
36163+ * (at your option) any later version.
dece6358
AM
36164+ *
36165+ * This program is distributed in the hope that it will be useful,
36166+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36167+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36168+ * GNU General Public License for more details.
36169+ *
36170+ * You should have received a copy of the GNU General Public License
523b37e3 36171+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 36172+ */
36173+
36174+/*
36175+ * whiteout for logical deletion and opaque directory
36176+ */
36177+
36178+#ifndef __AUFS_WHOUT_H__
36179+#define __AUFS_WHOUT_H__
36180+
36181+#ifdef __KERNEL__
36182+
1facf9fc 36183+#include "dir.h"
36184+
36185+/* whout.c */
36186+int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
758e9dad 36187+int au_wh_test(struct user_namespace *h_userns, struct path *h_ppath,
0b2a12c6 36188+ struct qstr *wh_name, int try_sio);
758e9dad 36189+int au_diropq_test(struct user_namespace *h_userns, struct path *h_path);
3c1bdaff 36190+struct au_branch;
1facf9fc 36191+struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
36192+ struct qstr *prefix);
36193+int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
36194+int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
36195+ struct dentry *dentry);
86dc4139 36196+int au_wh_init(struct au_branch *br, struct super_block *sb);
1facf9fc 36197+
36198+/* diropq flags */
36199+#define AuDiropq_CREATE 1
36200+#define au_ftest_diropq(flags, name) ((flags) & AuDiropq_##name)
7f207e10
AM
36201+#define au_fset_diropq(flags, name) \
36202+ do { (flags) |= AuDiropq_##name; } while (0)
36203+#define au_fclr_diropq(flags, name) \
36204+ do { (flags) &= ~AuDiropq_##name; } while (0)
1facf9fc 36205+
36206+struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
36207+ unsigned int flags);
36208+struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
36209+ struct au_branch *br);
36210+struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
36211+ struct dentry *h_parent);
36212+
36213+/* real rmdir for the whiteout-ed dir */
36214+struct au_whtmp_rmdir {
36215+ struct inode *dir;
e49829fe 36216+ struct au_branch *br;
1facf9fc 36217+ struct dentry *wh_dentry;
dece6358 36218+ struct au_nhash whlist;
1facf9fc 36219+};
36220+
36221+struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
36222+void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
36223+int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
36224+ struct dentry *wh_dentry, struct au_nhash *whlist);
36225+void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
36226+ struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
36227+
36228+/* ---------------------------------------------------------------------- */
36229+
36230+static inline struct dentry *au_diropq_create(struct dentry *dentry,
36231+ aufs_bindex_t bindex)
36232+{
36233+ return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
36234+}
36235+
36236+static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
36237+{
36238+ return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
36239+}
36240+
36241+#endif /* __KERNEL__ */
36242+#endif /* __AUFS_WHOUT_H__ */
7f207e10 36243diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
eca34b5c 36244--- /usr/share/empty/fs/aufs/wkq.c 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 36245+++ linux/fs/aufs/wkq.c 2022-11-05 23:02:18.972555950 +0100
43982f53 36246@@ -0,0 +1,372 @@
cd7a4cd9 36247+// SPDX-License-Identifier: GPL-2.0
1facf9fc 36248+/*
0dcfbb52 36249+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 36250+ *
0dcfbb52 36251+ * This program is free software; you can redistribute it and/or modify
1facf9fc 36252+ * it under the terms of the GNU General Public License as published by
36253+ * the Free Software Foundation; either version 2 of the License, or
36254+ * (at your option) any later version.
dece6358
AM
36255+ *
36256+ * This program is distributed in the hope that it will be useful,
36257+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36258+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36259+ * GNU General Public License for more details.
36260+ *
36261+ * You should have received a copy of the GNU General Public License
523b37e3 36262+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 36263+ */
36264+
36265+/*
36266+ * workqueue for asynchronous/super-io operations
acd2b654 36267+ * todo: try new credential scheme
1facf9fc 36268+ */
36269+
dece6358 36270+#include <linux/module.h>
1facf9fc 36271+#include "aufs.h"
36272+
9dbd164d 36273+/* internal workqueue named AUFS_WKQ_NAME */
b752ccd1 36274+
9dbd164d 36275+static struct workqueue_struct *au_wkq;
1facf9fc 36276+
36277+struct au_wkinfo {
36278+ struct work_struct wk;
7f207e10 36279+ struct kobject *kobj;
1facf9fc 36280+
36281+ unsigned int flags; /* see wkq.h */
36282+
36283+ au_wkq_func_t func;
36284+ void *args;
36285+
8b6a4947
AM
36286+#ifdef CONFIG_LOCKDEP
36287+ int dont_check;
36288+ struct held_lock **hlock;
36289+#endif
36290+
1facf9fc 36291+ struct completion *comp;
36292+};
36293+
36294+/* ---------------------------------------------------------------------- */
8b6a4947
AM
36295+/*
36296+ * Aufs passes some operations to the workqueue such as the internal copyup.
36297+ * This scheme looks rather unnatural for LOCKDEP debugging feature, since the
36298+ * job run by workqueue depends upon the locks acquired in the other task.
36299+ * Delegating a small operation to the workqueue, aufs passes its lockdep
36300+ * information too. And the job in the workqueue restores the info in order to
36301+ * pretend as if it acquired those locks. This is just to make LOCKDEP work
36302+ * correctly and expectedly.
36303+ */
36304+
36305+#ifndef CONFIG_LOCKDEP
36306+AuStubInt0(au_wkq_lockdep_alloc, struct au_wkinfo *wkinfo);
36307+AuStubVoid(au_wkq_lockdep_free, struct au_wkinfo *wkinfo);
36308+AuStubVoid(au_wkq_lockdep_pre, struct au_wkinfo *wkinfo);
36309+AuStubVoid(au_wkq_lockdep_post, struct au_wkinfo *wkinfo);
36310+AuStubVoid(au_wkq_lockdep_init, struct au_wkinfo *wkinfo);
36311+#else
36312+static void au_wkq_lockdep_init(struct au_wkinfo *wkinfo)
36313+{
36314+ wkinfo->hlock = NULL;
36315+ wkinfo->dont_check = 0;
36316+}
36317+
36318+/*
36319+ * 1: matched
36320+ * 0: unmatched
36321+ */
36322+static int au_wkq_lockdep_test(struct lock_class_key *key, const char *name)
36323+{
36324+ static DEFINE_SPINLOCK(spin);
36325+ static struct {
36326+ char *name;
36327+ struct lock_class_key *key;
36328+ } a[] = {
36329+ { .name = "&sbinfo->si_rwsem" },
36330+ { .name = "&finfo->fi_rwsem" },
36331+ { .name = "&dinfo->di_rwsem" },
36332+ { .name = "&iinfo->ii_rwsem" }
36333+ };
36334+ static int set;
36335+ int i;
36336+
36337+ /* lockless read from 'set.' see below */
36338+ if (set == ARRAY_SIZE(a)) {
36339+ for (i = 0; i < ARRAY_SIZE(a); i++)
36340+ if (a[i].key == key)
36341+ goto match;
36342+ goto unmatch;
36343+ }
36344+
36345+ spin_lock(&spin);
36346+ if (set)
36347+ for (i = 0; i < ARRAY_SIZE(a); i++)
36348+ if (a[i].key == key) {
36349+ spin_unlock(&spin);
36350+ goto match;
36351+ }
36352+ for (i = 0; i < ARRAY_SIZE(a); i++) {
36353+ if (a[i].key) {
36354+ if (unlikely(a[i].key == key)) { /* rare but possible */
36355+ spin_unlock(&spin);
36356+ goto match;
36357+ } else
36358+ continue;
36359+ }
36360+ if (strstr(a[i].name, name)) {
36361+ /*
36362+ * the order of these three lines is important for the
36363+ * lockless read above.
36364+ */
36365+ a[i].key = key;
36366+ spin_unlock(&spin);
36367+ set++;
36368+ /* AuDbg("%d, %s\n", set, name); */
36369+ goto match;
36370+ }
36371+ }
36372+ spin_unlock(&spin);
36373+ goto unmatch;
36374+
36375+match:
36376+ return 1;
36377+unmatch:
36378+ return 0;
36379+}
36380+
36381+static int au_wkq_lockdep_alloc(struct au_wkinfo *wkinfo)
36382+{
36383+ int err, n;
36384+ struct task_struct *curr;
36385+ struct held_lock **hl, *held_locks, *p;
36386+
36387+ err = 0;
36388+ curr = current;
36389+ wkinfo->dont_check = lockdep_recursing(curr);
36390+ if (wkinfo->dont_check)
36391+ goto out;
36392+ n = curr->lockdep_depth;
36393+ if (!n)
36394+ goto out;
36395+
36396+ err = -ENOMEM;
36397+ wkinfo->hlock = kmalloc_array(n + 1, sizeof(*wkinfo->hlock), GFP_NOFS);
36398+ if (unlikely(!wkinfo->hlock))
36399+ goto out;
36400+
36401+ err = 0;
43982f53
AM
36402+#if 0 /* left for debugging */
36403+ if (0 && au_debug_test())
8b6a4947
AM
36404+ lockdep_print_held_locks(curr);
36405+#endif
36406+ held_locks = curr->held_locks;
36407+ hl = wkinfo->hlock;
36408+ while (n--) {
36409+ p = held_locks++;
36410+ if (au_wkq_lockdep_test(p->instance->key, p->instance->name))
36411+ *hl++ = p;
36412+ }
36413+ *hl = NULL;
36414+
36415+out:
36416+ return err;
36417+}
36418+
36419+static void au_wkq_lockdep_free(struct au_wkinfo *wkinfo)
36420+{
9f237c51 36421+ au_kfree_try_rcu(wkinfo->hlock);
8b6a4947
AM
36422+}
36423+
36424+static void au_wkq_lockdep_pre(struct au_wkinfo *wkinfo)
36425+{
36426+ struct held_lock *p, **hl = wkinfo->hlock;
36427+ int subclass;
36428+
36429+ if (wkinfo->dont_check)
36430+ lockdep_off();
36431+ if (!hl)
36432+ return;
36433+ while ((p = *hl++)) { /* assignment */
36434+ subclass = lockdep_hlock_class(p)->subclass;
36435+ /* AuDbg("%s, %d\n", p->instance->name, subclass); */
36436+ if (p->read)
36437+ rwsem_acquire_read(p->instance, subclass, 0,
36438+ /*p->acquire_ip*/_RET_IP_);
36439+ else
36440+ rwsem_acquire(p->instance, subclass, 0,
36441+ /*p->acquire_ip*/_RET_IP_);
36442+ }
36443+}
36444+
36445+static void au_wkq_lockdep_post(struct au_wkinfo *wkinfo)
36446+{
36447+ struct held_lock *p, **hl = wkinfo->hlock;
36448+
36449+ if (wkinfo->dont_check)
36450+ lockdep_on();
36451+ if (!hl)
36452+ return;
36453+ while ((p = *hl++)) /* assignment */
d58c55f2 36454+ rwsem_release(p->instance, /*p->acquire_ip*/_RET_IP_);
8b6a4947
AM
36455+}
36456+#endif
1facf9fc 36457+
1facf9fc 36458+static void wkq_func(struct work_struct *wk)
36459+{
36460+ struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
36461+
2dfbb274 36462+ AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
7f207e10
AM
36463+ AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
36464+
8b6a4947 36465+ au_wkq_lockdep_pre(wkinfo);
1facf9fc 36466+ wkinfo->func(wkinfo->args);
8b6a4947 36467+ au_wkq_lockdep_post(wkinfo);
1facf9fc 36468+ if (au_ftest_wkq(wkinfo->flags, WAIT))
36469+ complete(wkinfo->comp);
36470+ else {
7f207e10 36471+ kobject_put(wkinfo->kobj);
9dbd164d 36472+ module_put(THIS_MODULE); /* todo: ?? */
9f237c51 36473+ au_kfree_rcu(wkinfo);
1facf9fc 36474+ }
36475+}
36476+
36477+/*
36478+ * Since struct completion is large, try allocating it dynamically.
36479+ */
1facf9fc 36480+#define AuWkqCompDeclare(name) struct completion *comp = NULL
36481+
36482+static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
36483+{
36484+ *comp = kmalloc(sizeof(**comp), GFP_NOFS);
36485+ if (*comp) {
36486+ init_completion(*comp);
36487+ wkinfo->comp = *comp;
36488+ return 0;
36489+ }
36490+ return -ENOMEM;
36491+}
36492+
36493+static void au_wkq_comp_free(struct completion *comp)
36494+{
9f237c51 36495+ au_kfree_rcu(comp);
1facf9fc 36496+}
36497+
53392da6 36498+static void au_wkq_run(struct au_wkinfo *wkinfo)
1facf9fc 36499+{
53392da6
AM
36500+ if (au_ftest_wkq(wkinfo->flags, NEST)) {
36501+ if (au_wkq_test()) {
38d290e6 36502+ AuWarn1("wkq from wkq, unless silly-rename on NFS,"
acd2b654
AM
36503+ " due to a dead dir by UDBA,"
36504+ " or async xino write?\n");
53392da6
AM
36505+ AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
36506+ }
36507+ } else
36508+ au_dbg_verify_kthread();
36509+
36510+ if (au_ftest_wkq(wkinfo->flags, WAIT)) {
a1f66529 36511+ INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
9dbd164d 36512+ queue_work(au_wkq, &wkinfo->wk);
4a4d8108
AM
36513+ } else {
36514+ INIT_WORK(&wkinfo->wk, wkq_func);
36515+ schedule_work(&wkinfo->wk);
36516+ }
1facf9fc 36517+}
36518+
7f207e10
AM
36519+/*
36520+ * Be careful. It is easy to make deadlock happen.
36521+ * processA: lock, wkq and wait
36522+ * processB: wkq and wait, lock in wkq
36523+ * --> deadlock
36524+ */
b752ccd1 36525+int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
1facf9fc 36526+{
36527+ int err;
36528+ AuWkqCompDeclare(comp);
36529+ struct au_wkinfo wkinfo = {
b752ccd1 36530+ .flags = flags,
1facf9fc 36531+ .func = func,
36532+ .args = args
36533+ };
36534+
36535+ err = au_wkq_comp_alloc(&wkinfo, &comp);
8b6a4947
AM
36536+ if (unlikely(err))
36537+ goto out;
36538+ err = au_wkq_lockdep_alloc(&wkinfo);
36539+ if (unlikely(err))
36540+ goto out_comp;
1facf9fc 36541+ if (!err) {
53392da6 36542+ au_wkq_run(&wkinfo);
1facf9fc 36543+ /* no timeout, no interrupt */
36544+ wait_for_completion(wkinfo.comp);
1facf9fc 36545+ }
8b6a4947 36546+ au_wkq_lockdep_free(&wkinfo);
1facf9fc 36547+
8b6a4947
AM
36548+out_comp:
36549+ au_wkq_comp_free(comp);
36550+out:
36551+ destroy_work_on_stack(&wkinfo.wk);
1facf9fc 36552+ return err;
1facf9fc 36553+}
36554+
027c5e7a
AM
36555+/*
36556+ * Note: dget/dput() in func for aufs dentries are not supported. It will be a
36557+ * problem in a concurrent umounting.
36558+ */
53392da6
AM
36559+int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36560+ unsigned int flags)
1facf9fc 36561+{
36562+ int err;
36563+ struct au_wkinfo *wkinfo;
36564+
f0c0a007 36565+ atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
1facf9fc 36566+
36567+ /*
36568+ * wkq_func() must free this wkinfo.
36569+ * it highly depends upon the implementation of workqueue.
36570+ */
36571+ err = 0;
36572+ wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
36573+ if (wkinfo) {
7f207e10 36574+ wkinfo->kobj = &au_sbi(sb)->si_kobj;
53392da6 36575+ wkinfo->flags = flags & ~AuWkq_WAIT;
1facf9fc 36576+ wkinfo->func = func;
36577+ wkinfo->args = args;
36578+ wkinfo->comp = NULL;
8b6a4947 36579+ au_wkq_lockdep_init(wkinfo);
7f207e10 36580+ kobject_get(wkinfo->kobj);
9dbd164d 36581+ __module_get(THIS_MODULE); /* todo: ?? */
1facf9fc 36582+
53392da6 36583+ au_wkq_run(wkinfo);
1facf9fc 36584+ } else {
36585+ err = -ENOMEM;
e49829fe 36586+ au_nwt_done(&au_sbi(sb)->si_nowait);
1facf9fc 36587+ }
36588+
36589+ return err;
36590+}
36591+
36592+/* ---------------------------------------------------------------------- */
36593+
36594+void au_nwt_init(struct au_nowait_tasks *nwt)
36595+{
f0c0a007
AM
36596+ atomic_set(&nwt->nw_len, 0);
36597+ /* smp_mb(); */ /* atomic_set */
1facf9fc 36598+ init_waitqueue_head(&nwt->nw_wq);
36599+}
36600+
36601+void au_wkq_fin(void)
36602+{
9dbd164d 36603+ destroy_workqueue(au_wkq);
1facf9fc 36604+}
36605+
36606+int __init au_wkq_init(void)
36607+{
9dbd164d 36608+ int err;
b752ccd1
AM
36609+
36610+ err = 0;
86dc4139 36611+ au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
9dbd164d
AM
36612+ if (IS_ERR(au_wkq))
36613+ err = PTR_ERR(au_wkq);
36614+ else if (!au_wkq)
36615+ err = -ENOMEM;
b752ccd1
AM
36616+
36617+ return err;
1facf9fc 36618+}
7f207e10 36619diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
eca34b5c 36620--- /usr/share/empty/fs/aufs/wkq.h 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 36621+++ linux/fs/aufs/wkq.h 2022-11-05 23:02:18.972555950 +0100
acd2b654 36622@@ -0,0 +1,89 @@
062440b3 36623+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 36624+/*
0dcfbb52 36625+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 36626+ *
0dcfbb52 36627+ * This program is free software; you can redistribute it and/or modify
1facf9fc 36628+ * it under the terms of the GNU General Public License as published by
36629+ * the Free Software Foundation; either version 2 of the License, or
36630+ * (at your option) any later version.
dece6358
AM
36631+ *
36632+ * This program is distributed in the hope that it will be useful,
36633+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36634+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36635+ * GNU General Public License for more details.
36636+ *
36637+ * You should have received a copy of the GNU General Public License
523b37e3 36638+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 36639+ */
36640+
36641+/*
36642+ * workqueue for asynchronous/super-io operations
36643+ * todo: try new credentials management scheme
36644+ */
36645+
36646+#ifndef __AUFS_WKQ_H__
36647+#define __AUFS_WKQ_H__
36648+
36649+#ifdef __KERNEL__
36650+
8b6a4947 36651+#include <linux/wait.h>
5afbbe0d 36652+
dece6358
AM
36653+struct super_block;
36654+
1facf9fc 36655+/* ---------------------------------------------------------------------- */
36656+
36657+/*
36658+ * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
36659+ */
36660+struct au_nowait_tasks {
f0c0a007 36661+ atomic_t nw_len;
1facf9fc 36662+ wait_queue_head_t nw_wq;
36663+};
36664+
36665+/* ---------------------------------------------------------------------- */
36666+
36667+typedef void (*au_wkq_func_t)(void *args);
36668+
36669+/* wkq flags */
36670+#define AuWkq_WAIT 1
9dbd164d 36671+#define AuWkq_NEST (1 << 1)
1facf9fc 36672+#define au_ftest_wkq(flags, name) ((flags) & AuWkq_##name)
7f207e10
AM
36673+#define au_fset_wkq(flags, name) \
36674+ do { (flags) |= AuWkq_##name; } while (0)
36675+#define au_fclr_wkq(flags, name) \
36676+ do { (flags) &= ~AuWkq_##name; } while (0)
1facf9fc 36677+
36678+/* wkq.c */
b752ccd1 36679+int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
53392da6
AM
36680+int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36681+ unsigned int flags);
1facf9fc 36682+void au_nwt_init(struct au_nowait_tasks *nwt);
36683+int __init au_wkq_init(void);
36684+void au_wkq_fin(void);
36685+
36686+/* ---------------------------------------------------------------------- */
36687+
53392da6
AM
36688+static inline int au_wkq_test(void)
36689+{
36690+ return current->flags & PF_WQ_WORKER;
36691+}
36692+
b752ccd1 36693+static inline int au_wkq_wait(au_wkq_func_t func, void *args)
1facf9fc 36694+{
b752ccd1 36695+ return au_wkq_do_wait(AuWkq_WAIT, func, args);
1facf9fc 36696+}
36697+
36698+static inline void au_nwt_done(struct au_nowait_tasks *nwt)
36699+{
f0c0a007 36700+ if (atomic_dec_and_test(&nwt->nw_len))
1facf9fc 36701+ wake_up_all(&nwt->nw_wq);
36702+}
36703+
36704+static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
36705+{
f0c0a007 36706+ wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
1facf9fc 36707+ return 0;
36708+}
36709+
36710+#endif /* __KERNEL__ */
36711+#endif /* __AUFS_WKQ_H__ */
c1595e42 36712diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
eca34b5c 36713--- /usr/share/empty/fs/aufs/xattr.c 1970-01-01 01:00:00.000000000 +0100
2255d0fe
AM
36714+++ linux/fs/aufs/xattr.c 2022-12-17 09:21:34.799855195 +0100
36715@@ -0,0 +1,368 @@
cd7a4cd9 36716+// SPDX-License-Identifier: GPL-2.0
c1595e42 36717+/*
0dcfbb52 36718+ * Copyright (C) 2014-2022 Junjiro R. Okajima
c1595e42 36719+ *
0dcfbb52 36720+ * This program is free software; you can redistribute it and/or modify
c1595e42
JR
36721+ * it under the terms of the GNU General Public License as published by
36722+ * the Free Software Foundation; either version 2 of the License, or
36723+ * (at your option) any later version.
36724+ *
36725+ * This program is distributed in the hope that it will be useful,
36726+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36727+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36728+ * GNU General Public License for more details.
36729+ *
36730+ * You should have received a copy of the GNU General Public License
36731+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
36732+ */
36733+
36734+/*
36735+ * handling xattr functions
36736+ */
36737+
a2654f78
AM
36738+#include <linux/fs.h>
36739+#include <linux/posix_acl_xattr.h>
c1595e42
JR
36740+#include <linux/xattr.h>
36741+#include "aufs.h"
36742+
36743+static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
36744+{
36745+ if (!ignore_flags)
36746+ goto out;
36747+ switch (err) {
36748+ case -ENOMEM:
36749+ case -EDQUOT:
36750+ goto out;
36751+ }
36752+
36753+ if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
36754+ err = 0;
36755+ goto out;
36756+ }
36757+
36758+#define cmp(brattr, prefix) do { \
36759+ if (!strncmp(name, XATTR_##prefix##_PREFIX, \
36760+ XATTR_##prefix##_PREFIX_LEN)) { \
36761+ if (ignore_flags & AuBrAttr_ICEX_##brattr) \
36762+ err = 0; \
36763+ goto out; \
36764+ } \
36765+ } while (0)
36766+
36767+ cmp(SEC, SECURITY);
36768+ cmp(SYS, SYSTEM);
36769+ cmp(TR, TRUSTED);
36770+ cmp(USR, USER);
36771+#undef cmp
36772+
36773+ if (ignore_flags & AuBrAttr_ICEX_OTH)
36774+ err = 0;
36775+
36776+out:
36777+ return err;
36778+}
36779+
36780+static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
36781+
0b2a12c6 36782+static int au_do_cpup_xattr(struct path *h_dst, struct path *h_src,
7e9cd9fe
AM
36783+ char *name, char **buf, unsigned int ignore_flags,
36784+ unsigned int verbose)
c1595e42
JR
36785+{
36786+ int err;
36787+ ssize_t ssz;
36788+ struct inode *h_idst;
0b2a12c6
JR
36789+ struct dentry *h_dst_dentry, *h_src_dentry;
36790+ struct user_namespace *h_dst_userns, *h_src_userns;
c1595e42 36791+
0b2a12c6
JR
36792+ h_src_userns = mnt_user_ns(h_src->mnt);
36793+ h_src_dentry = h_src->dentry;
36794+ ssz = vfs_getxattr_alloc(h_src_userns, h_src_dentry, name, buf, 0,
36795+ GFP_NOFS);
c1595e42
JR
36796+ err = ssz;
36797+ if (unlikely(err <= 0)) {
c1595e42
JR
36798+ if (err == -ENODATA
36799+ || (err == -EOPNOTSUPP
b912730e 36800+ && ((ignore_flags & au_xattr_out_of_list)
0b2a12c6 36801+ || (au_test_nfs_noacl(d_inode(h_src_dentry))
b912730e
AM
36802+ && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
36803+ || !strcmp(name,
36804+ XATTR_NAME_POSIX_ACL_DEFAULT))))
36805+ ))
c1595e42 36806+ err = 0;
b912730e
AM
36807+ if (err && (verbose || au_debug_test()))
36808+ pr_err("%s, err %d\n", name, err);
c1595e42
JR
36809+ goto out;
36810+ }
36811+
36812+ /* unlock it temporary */
0b2a12c6
JR
36813+ h_dst_userns = mnt_user_ns(h_dst->mnt);
36814+ h_dst_dentry = h_dst->dentry;
36815+ h_idst = d_inode(h_dst_dentry);
febd17d6 36816+ inode_unlock(h_idst);
0b2a12c6
JR
36817+ err = vfsub_setxattr(h_dst_userns, h_dst_dentry, name, *buf, ssz,
36818+ /*flags*/0);
febd17d6 36819+ inode_lock_nested(h_idst, AuLsc_I_CHILD2);
c1595e42 36820+ if (unlikely(err)) {
7e9cd9fe
AM
36821+ if (verbose || au_debug_test())
36822+ pr_err("%s, err %d\n", name, err);
c1595e42
JR
36823+ err = au_xattr_ignore(err, name, ignore_flags);
36824+ }
36825+
36826+out:
36827+ return err;
36828+}
36829+
0b2a12c6 36830+int au_cpup_xattr(struct path *h_dst, struct path *h_src, int ignore_flags,
7e9cd9fe 36831+ unsigned int verbose)
c1595e42
JR
36832+{
36833+ int err, unlocked, acl_access, acl_default;
36834+ ssize_t ssz;
0b2a12c6 36835+ struct dentry *h_dst_dentry, *h_src_dentry;
c1595e42
JR
36836+ struct inode *h_isrc, *h_idst;
36837+ char *value, *p, *o, *e;
36838+
36839+ /* try stopping to update the source inode while we are referencing */
7e9cd9fe 36840+ /* there should not be the parent-child relationship between them */
0b2a12c6
JR
36841+ h_dst_dentry = h_dst->dentry;
36842+ h_idst = d_inode(h_dst_dentry);
36843+ h_src_dentry = h_src->dentry;
36844+ h_isrc = d_inode(h_src_dentry);
febd17d6 36845+ inode_unlock(h_idst);
be118d29 36846+ inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
febd17d6 36847+ inode_lock_nested(h_idst, AuLsc_I_CHILD2);
c1595e42
JR
36848+ unlocked = 0;
36849+
36850+ /* some filesystems don't list POSIX ACL, for example tmpfs */
0b2a12c6 36851+ ssz = vfs_listxattr(h_src_dentry, NULL, 0);
c1595e42
JR
36852+ err = ssz;
36853+ if (unlikely(err < 0)) {
36854+ AuTraceErr(err);
36855+ if (err == -ENODATA
36856+ || err == -EOPNOTSUPP)
36857+ err = 0; /* ignore */
36858+ goto out;
36859+ }
36860+
36861+ err = 0;
36862+ p = NULL;
36863+ o = NULL;
36864+ if (ssz) {
36865+ err = -ENOMEM;
36866+ p = kmalloc(ssz, GFP_NOFS);
36867+ o = p;
36868+ if (unlikely(!p))
36869+ goto out;
0b2a12c6 36870+ err = vfs_listxattr(h_src_dentry, p, ssz);
c1595e42 36871+ }
3c1bdaff 36872+ inode_unlock_shared(h_isrc);
c1595e42
JR
36873+ unlocked = 1;
36874+ AuDbg("err %d, ssz %zd\n", err, ssz);
36875+ if (unlikely(err < 0))
36876+ goto out_free;
36877+
36878+ err = 0;
36879+ e = p + ssz;
36880+ value = NULL;
36881+ acl_access = 0;
36882+ acl_default = 0;
36883+ while (!err && p < e) {
36884+ acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
36885+ sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
36886+ acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
36887+ sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
36888+ - 1);
7e9cd9fe
AM
36889+ err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
36890+ verbose);
c1595e42
JR
36891+ p += strlen(p) + 1;
36892+ }
36893+ AuTraceErr(err);
36894+ ignore_flags |= au_xattr_out_of_list;
36895+ if (!err && !acl_access) {
36896+ err = au_do_cpup_xattr(h_dst, h_src,
36897+ XATTR_NAME_POSIX_ACL_ACCESS, &value,
7e9cd9fe 36898+ ignore_flags, verbose);
c1595e42
JR
36899+ AuTraceErr(err);
36900+ }
36901+ if (!err && !acl_default) {
36902+ err = au_do_cpup_xattr(h_dst, h_src,
36903+ XATTR_NAME_POSIX_ACL_DEFAULT, &value,
7e9cd9fe 36904+ ignore_flags, verbose);
c1595e42
JR
36905+ AuTraceErr(err);
36906+ }
36907+
9f237c51 36908+ au_kfree_try_rcu(value);
c1595e42
JR
36909+
36910+out_free:
9f237c51 36911+ au_kfree_try_rcu(o);
c1595e42
JR
36912+out:
36913+ if (!unlocked)
3c1bdaff 36914+ inode_unlock_shared(h_isrc);
c1595e42
JR
36915+ AuTraceErr(err);
36916+ return err;
36917+}
36918+
36919+/* ---------------------------------------------------------------------- */
36920+
a2654f78
AM
36921+static int au_smack_reentering(struct super_block *sb)
36922+{
42b5c33a 36923+#if IS_ENABLED(CONFIG_SECURITY_SMACK) || IS_ENABLED(CONFIG_SECURITY_SELINUX)
a2654f78
AM
36924+ /*
36925+ * as a part of lookup, smack_d_instantiate() is called, and it calls
36926+ * i_op->getxattr(). ouch.
36927+ */
36928+ return si_pid_test(sb);
36929+#else
36930+ return 0;
36931+#endif
36932+}
36933+
c1595e42
JR
36934+enum {
36935+ AU_XATTR_LIST,
36936+ AU_XATTR_GET
36937+};
36938+
36939+struct au_lgxattr {
36940+ int type;
36941+ union {
36942+ struct {
36943+ char *list;
36944+ size_t size;
36945+ } list;
36946+ struct {
36947+ const char *name;
36948+ void *value;
36949+ size_t size;
36950+ } get;
36951+ } u;
36952+};
36953+
42b5c33a
AM
36954+static ssize_t au_lgxattr(struct dentry *dentry, struct inode *inode,
36955+ struct au_lgxattr *arg)
c1595e42
JR
36956+{
36957+ ssize_t err;
a2654f78 36958+ int reenter;
c1595e42
JR
36959+ struct path h_path;
36960+ struct super_block *sb;
36961+
36962+ sb = dentry->d_sb;
a2654f78
AM
36963+ reenter = au_smack_reentering(sb);
36964+ if (!reenter) {
36965+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
36966+ if (unlikely(err))
36967+ goto out;
36968+ }
42b5c33a 36969+ err = au_h_path_getattr(dentry, inode, /*force*/1, &h_path, reenter);
c1595e42
JR
36970+ if (unlikely(err))
36971+ goto out_si;
36972+ if (unlikely(!h_path.dentry))
36973+ /* illegally overlapped or something */
36974+ goto out_di; /* pretending success */
36975+
36976+ /* always topmost entry only */
36977+ switch (arg->type) {
36978+ case AU_XATTR_LIST:
36979+ err = vfs_listxattr(h_path.dentry,
36980+ arg->u.list.list, arg->u.list.size);
36981+ break;
36982+ case AU_XATTR_GET:
5afbbe0d 36983+ AuDebugOn(d_is_negative(h_path.dentry));
0b2a12c6 36984+ err = vfs_getxattr(mnt_user_ns(h_path.mnt), h_path.dentry,
c1595e42
JR
36985+ arg->u.get.name, arg->u.get.value,
36986+ arg->u.get.size);
36987+ break;
36988+ }
36989+
36990+out_di:
a2654f78
AM
36991+ if (!reenter)
36992+ di_read_unlock(dentry, AuLock_IR);
c1595e42 36993+out_si:
a2654f78
AM
36994+ if (!reenter)
36995+ si_read_unlock(sb);
c1595e42
JR
36996+out:
36997+ AuTraceErr(err);
36998+ return err;
36999+}
37000+
37001+ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
37002+{
37003+ struct au_lgxattr arg = {
37004+ .type = AU_XATTR_LIST,
37005+ .u.list = {
37006+ .list = list,
37007+ .size = size
37008+ },
37009+ };
37010+
42b5c33a 37011+ return au_lgxattr(dentry, /*inode*/NULL, &arg);
c1595e42
JR
37012+}
37013+
42b5c33a 37014+static ssize_t au_getxattr(struct dentry *dentry, struct inode *inode,
f2c43d5f 37015+ const char *name, void *value, size_t size)
c1595e42
JR
37016+{
37017+ struct au_lgxattr arg = {
37018+ .type = AU_XATTR_GET,
37019+ .u.get = {
37020+ .name = name,
37021+ .value = value,
37022+ .size = size
37023+ },
37024+ };
37025+
42b5c33a 37026+ return au_lgxattr(dentry, inode, &arg);
c1595e42
JR
37027+}
37028+
f2c43d5f 37029+static int au_setxattr(struct dentry *dentry, struct inode *inode,
2255d0fe 37030+ const char *name, const void *value, size_t size,
f2c43d5f 37031+ int flags)
c1595e42 37032+{
f2c43d5f 37033+ struct au_sxattr arg = {
c1595e42
JR
37034+ .type = AU_XATTR_SET,
37035+ .u.set = {
37036+ .name = name,
37037+ .value = value,
37038+ .size = size,
37039+ .flags = flags
37040+ },
37041+ };
37042+
f2c43d5f 37043+ return au_sxattr(dentry, inode, &arg);
c1595e42
JR
37044+}
37045+
37046+/* ---------------------------------------------------------------------- */
37047+
f2c43d5f
AM
37048+static int au_xattr_get(const struct xattr_handler *handler,
37049+ struct dentry *dentry, struct inode *inode,
37050+ const char *name, void *buffer, size_t size)
c1595e42 37051+{
f2c43d5f 37052+ return au_getxattr(dentry, inode, name, buffer, size);
c1595e42
JR
37053+}
37054+
f2c43d5f 37055+static int au_xattr_set(const struct xattr_handler *handler,
0b2a12c6 37056+ struct user_namespace *userns,
f2c43d5f
AM
37057+ struct dentry *dentry, struct inode *inode,
37058+ const char *name, const void *value, size_t size,
37059+ int flags)
c1595e42 37060+{
2255d0fe 37061+ return au_setxattr(dentry, inode, name, value, size, flags);
c1595e42
JR
37062+}
37063+
37064+static const struct xattr_handler au_xattr_handler = {
f2c43d5f
AM
37065+ .name = "",
37066+ .prefix = "",
c1595e42
JR
37067+ .get = au_xattr_get,
37068+ .set = au_xattr_set
c1595e42
JR
37069+};
37070+
37071+static const struct xattr_handler *au_xattr_handlers[] = {
a2654f78
AM
37072+#ifdef CONFIG_FS_POSIX_ACL
37073+ &posix_acl_access_xattr_handler,
37074+ &posix_acl_default_xattr_handler,
37075+#endif
37076+ &au_xattr_handler, /* must be last */
f2c43d5f 37077+ NULL
c1595e42
JR
37078+};
37079+
37080+void au_xattr_init(struct super_block *sb)
37081+{
f2c43d5f 37082+ sb->s_xattr = au_xattr_handlers;
c1595e42 37083+}
7f207e10 37084diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
eca34b5c 37085--- /usr/share/empty/fs/aufs/xino.c 1970-01-01 01:00:00.000000000 +0100
0dcfbb52 37086+++ linux/fs/aufs/xino.c 2022-11-05 23:02:18.972555950 +0100
758e9dad 37087@@ -0,0 +1,1926 @@
cd7a4cd9 37088+// SPDX-License-Identifier: GPL-2.0
1facf9fc 37089+/*
0dcfbb52 37090+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 37091+ *
0dcfbb52 37092+ * This program is free software; you can redistribute it and/or modify
1facf9fc 37093+ * it under the terms of the GNU General Public License as published by
37094+ * the Free Software Foundation; either version 2 of the License, or
37095+ * (at your option) any later version.
dece6358
AM
37096+ *
37097+ * This program is distributed in the hope that it will be useful,
37098+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
37099+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37100+ * GNU General Public License for more details.
37101+ *
37102+ * You should have received a copy of the GNU General Public License
523b37e3 37103+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 37104+ */
37105+
37106+/*
37107+ * external inode number translation table and bitmap
062440b3
AM
37108+ *
37109+ * things to consider
37110+ * - the lifetime
37111+ * + au_xino object
37112+ * + XINO files (xino, xib, xigen)
37113+ * + dynamic debugfs entries (xiN)
37114+ * + static debugfs entries (xib, xigen)
37115+ * + static sysfs entry (xi_path)
37116+ * - several entry points to handle them.
37117+ * + mount(2) without xino option (default)
37118+ * + mount(2) with xino option
37119+ * + mount(2) with noxino option
37120+ * + umount(2)
37121+ * + remount with add/del branches
37122+ * + remount with xino/noxino options
1facf9fc 37123+ */
37124+
37125+#include <linux/seq_file.h>
392086de 37126+#include <linux/statfs.h>
1facf9fc 37127+#include "aufs.h"
37128+
062440b3
AM
37129+static aufs_bindex_t sbr_find_shared(struct super_block *sb, aufs_bindex_t btop,
37130+ aufs_bindex_t bbot,
37131+ struct super_block *h_sb)
1facf9fc 37132+{
062440b3
AM
37133+ /* todo: try binary-search if the branches are many */
37134+ for (; btop <= bbot; btop++)
37135+ if (h_sb == au_sbr_sb(sb, btop))
37136+ return btop;
37137+ return -1;
be52b249
AM
37138+}
37139+
062440b3
AM
37140+/*
37141+ * find another branch who is on the same filesystem of the specified
37142+ * branch{@btgt}. search until @bbot.
37143+ */
37144+static aufs_bindex_t is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
37145+ aufs_bindex_t bbot)
1facf9fc 37146+{
062440b3
AM
37147+ aufs_bindex_t bindex;
37148+ struct super_block *tgt_sb;
1facf9fc 37149+
062440b3
AM
37150+ tgt_sb = au_sbr_sb(sb, btgt);
37151+ bindex = sbr_find_shared(sb, /*btop*/0, btgt - 1, tgt_sb);
37152+ if (bindex < 0)
37153+ bindex = sbr_find_shared(sb, btgt + 1, bbot, tgt_sb);
1facf9fc 37154+
062440b3 37155+ return bindex;
1facf9fc 37156+}
37157+
37158+/* ---------------------------------------------------------------------- */
37159+
37160+/*
062440b3 37161+ * stop unnecessary notify events at creating xino files
1facf9fc 37162+ */
acd2b654
AM
37163+
37164+aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry)
37165+{
37166+ aufs_bindex_t bfound, bindex, bbot;
37167+ struct dentry *parent;
37168+ struct au_branch *br;
37169+
37170+ bfound = -1;
37171+ parent = dentry->d_parent; /* safe d_parent access */
37172+ bbot = au_sbbot(sb);
37173+ for (bindex = 0; bindex <= bbot; bindex++) {
37174+ br = au_sbr(sb, bindex);
37175+ if (au_br_dentry(br) == parent) {
37176+ bfound = bindex;
37177+ break;
37178+ }
37179+ }
37180+
37181+ AuDbg("bfound b%d\n", bfound);
37182+ return bfound;
37183+}
37184+
062440b3
AM
37185+struct au_xino_lock_dir {
37186+ struct au_hinode *hdir;
37187+ struct dentry *parent;
37188+ struct inode *dir;
37189+};
37190+
37191+static struct dentry *au_dget_parent_lock(struct dentry *dentry,
37192+ unsigned int lsc)
1facf9fc 37193+{
062440b3
AM
37194+ struct dentry *parent;
37195+ struct inode *dir;
1facf9fc 37196+
062440b3 37197+ parent = dget_parent(dentry);
5527c038 37198+ dir = d_inode(parent);
062440b3
AM
37199+ inode_lock_nested(dir, lsc);
37200+#if 0 /* it should not happen */
37201+ spin_lock(&dentry->d_lock);
37202+ if (unlikely(dentry->d_parent != parent)) {
37203+ spin_unlock(&dentry->d_lock);
37204+ inode_unlock(dir);
37205+ dput(parent);
37206+ parent = NULL;
1facf9fc 37207+ goto out;
37208+ }
062440b3 37209+ spin_unlock(&dentry->d_lock);
1facf9fc 37210+
4f0767ce 37211+out:
062440b3
AM
37212+#endif
37213+ return parent;
1facf9fc 37214+}
37215+
062440b3 37216+static void au_xino_lock_dir(struct super_block *sb, struct path *xipath,
1facf9fc 37217+ struct au_xino_lock_dir *ldir)
37218+{
acd2b654 37219+ aufs_bindex_t bindex;
1facf9fc 37220+
37221+ ldir->hdir = NULL;
acd2b654 37222+ bindex = au_xi_root(sb, xipath->dentry);
1facf9fc 37223+ if (bindex >= 0) {
062440b3 37224+ /* rw branch root */
5527c038 37225+ ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
5afbbe0d 37226+ au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
1facf9fc 37227+ } else {
062440b3
AM
37228+ /* other */
37229+ ldir->parent = au_dget_parent_lock(xipath->dentry,
37230+ AuLsc_I_PARENT);
febd17d6 37231+ ldir->dir = d_inode(ldir->parent);
1facf9fc 37232+ }
37233+}
37234+
37235+static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
37236+{
37237+ if (ldir->hdir)
5afbbe0d 37238+ au_hn_inode_unlock(ldir->hdir);
1facf9fc 37239+ else {
febd17d6 37240+ inode_unlock(ldir->dir);
1facf9fc 37241+ dput(ldir->parent);
37242+ }
37243+}
37244+
37245+/* ---------------------------------------------------------------------- */
37246+
062440b3
AM
37247+/*
37248+ * create and set a new xino file
37249+ */
83b672a5
AM
37250+struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
37251+ int wbrtop)
062440b3
AM
37252+{
37253+ struct file *file;
37254+ struct dentry *h_parent, *d;
37255+ struct inode *h_dir, *inode;
37256+ int err;
83b672a5 37257+ static DEFINE_MUTEX(mtx);
062440b3
AM
37258+
37259+ /*
37260+ * at mount-time, and the xino file is the default path,
37261+ * hnotify is disabled so we have no notify events to ignore.
37262+ * when a user specified the xino, we cannot get au_hdir to be ignored.
37263+ */
83b672a5
AM
37264+ if (!wbrtop)
37265+ mutex_lock(&mtx);
062440b3
AM
37266+ file = vfsub_filp_open(fpath, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37267+ /* | __FMODE_NONOTIFY */,
cd7a4cd9 37268+ 0666);
062440b3 37269+ if (IS_ERR(file)) {
83b672a5
AM
37270+ if (!wbrtop)
37271+ mutex_unlock(&mtx);
062440b3
AM
37272+ if (!silent)
37273+ pr_err("open %s(%ld)\n", fpath, PTR_ERR(file));
37274+ return file;
37275+ }
37276+
37277+ /* keep file count */
37278+ err = 0;
37279+ d = file->f_path.dentry;
37280+ h_parent = au_dget_parent_lock(d, AuLsc_I_PARENT);
83b672a5
AM
37281+ if (!wbrtop)
37282+ mutex_unlock(&mtx);
062440b3
AM
37283+ /* mnt_want_write() is unnecessary here */
37284+ h_dir = d_inode(h_parent);
37285+ inode = file_inode(file);
37286+ /* no delegation since it is just created */
37287+ if (inode->i_nlink)
37288+ err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
37289+ /*force*/0);
37290+ inode_unlock(h_dir);
37291+ dput(h_parent);
37292+ if (unlikely(err)) {
37293+ if (!silent)
37294+ pr_err("unlink %s(%d)\n", fpath, err);
37295+ goto out;
37296+ }
37297+
37298+ err = -EINVAL;
f4d37d76 37299+ if (unlikely(sb && sb == d->d_sb)) {
062440b3
AM
37300+ if (!silent)
37301+ pr_err("%s must be outside\n", fpath);
37302+ goto out;
37303+ }
37304+ if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
37305+ if (!silent)
37306+ pr_err("xino doesn't support %s(%s)\n",
37307+ fpath, au_sbtype(d->d_sb));
37308+ goto out;
37309+ }
37310+ return file; /* success */
37311+
37312+out:
37313+ fput(file);
37314+ file = ERR_PTR(err);
37315+ return file;
37316+}
37317+
37318+/*
37319+ * create a new xinofile at the same place/path as @base.
37320+ */
37321+struct file *au_xino_create2(struct super_block *sb, struct path *base,
37322+ struct file *copy_src)
37323+{
37324+ struct file *file;
758e9dad 37325+ struct dentry *dentry;
062440b3
AM
37326+ struct inode *dir, *delegated;
37327+ struct qstr *name;
758e9dad 37328+ struct path ppath, path;
062440b3
AM
37329+ int err, do_unlock;
37330+ struct au_xino_lock_dir ldir;
37331+
37332+ do_unlock = 1;
37333+ au_xino_lock_dir(sb, base, &ldir);
37334+ dentry = base->dentry;
758e9dad
AM
37335+ ppath.dentry = dentry->d_parent; /* dir inode is locked */
37336+ ppath.mnt = base->mnt;
37337+ dir = d_inode(ppath.dentry);
062440b3
AM
37338+ IMustLock(dir);
37339+
37340+ name = &dentry->d_name;
758e9dad 37341+ path.dentry = vfsub_lookup_one_len(name->name, &ppath, name->len);
062440b3
AM
37342+ if (IS_ERR(path.dentry)) {
37343+ file = (void *)path.dentry;
37344+ pr_err("%pd lookup err %ld\n", dentry, PTR_ERR(path.dentry));
37345+ goto out;
37346+ }
37347+
37348+ /* no need to mnt_want_write() since we call dentry_open() later */
0b2a12c6 37349+ err = vfs_create(mnt_user_ns(base->mnt), dir, path.dentry, 0666, NULL);
062440b3
AM
37350+ if (unlikely(err)) {
37351+ file = ERR_PTR(err);
37352+ pr_err("%pd create err %d\n", dentry, err);
37353+ goto out_dput;
37354+ }
37355+
37356+ path.mnt = base->mnt;
37357+ file = vfsub_dentry_open(&path,
37358+ O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37359+ /* | __FMODE_NONOTIFY */);
37360+ if (IS_ERR(file)) {
37361+ pr_err("%pd open err %ld\n", dentry, PTR_ERR(file));
37362+ goto out_dput;
37363+ }
37364+
37365+ delegated = NULL;
37366+ err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
37367+ au_xino_unlock_dir(&ldir);
37368+ do_unlock = 0;
37369+ if (unlikely(err == -EWOULDBLOCK)) {
37370+ pr_warn("cannot retry for NFSv4 delegation"
37371+ " for an internal unlink\n");
37372+ iput(delegated);
37373+ }
37374+ if (unlikely(err)) {
37375+ pr_err("%pd unlink err %d\n", dentry, err);
37376+ goto out_fput;
37377+ }
37378+
37379+ if (copy_src) {
37380+ /* no one can touch copy_src xino */
37381+ err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
37382+ if (unlikely(err)) {
37383+ pr_err("%pd copy err %d\n", dentry, err);
37384+ goto out_fput;
37385+ }
37386+ }
37387+ goto out_dput; /* success */
1facf9fc 37388+
062440b3
AM
37389+out_fput:
37390+ fput(file);
37391+ file = ERR_PTR(err);
37392+out_dput:
37393+ dput(path.dentry);
37394+out:
37395+ if (do_unlock)
37396+ au_xino_unlock_dir(&ldir);
37397+ return file;
37398+}
37399+
acd2b654
AM
37400+struct file *au_xino_file1(struct au_xino *xi)
37401+{
37402+ struct file *file;
37403+ unsigned int u, nfile;
37404+
37405+ file = NULL;
37406+ nfile = xi->xi_nfile;
37407+ for (u = 0; u < nfile; u++) {
37408+ file = xi->xi_file[u];
37409+ if (file)
37410+ break;
37411+ }
37412+
37413+ return file;
37414+}
37415+
37416+static int au_xino_file_set(struct au_xino *xi, int idx, struct file *file)
37417+{
37418+ int err;
37419+ struct file *f;
37420+ void *p;
37421+
37422+ if (file)
37423+ get_file(file);
37424+
37425+ err = 0;
37426+ f = NULL;
37427+ if (idx < xi->xi_nfile) {
37428+ f = xi->xi_file[idx];
37429+ if (f)
37430+ fput(f);
37431+ } else {
37432+ p = au_kzrealloc(xi->xi_file,
37433+ sizeof(*xi->xi_file) * xi->xi_nfile,
37434+ sizeof(*xi->xi_file) * (idx + 1),
37435+ GFP_NOFS, /*may_shrink*/0);
37436+ if (p) {
37437+ MtxMustLock(&xi->xi_mtx);
37438+ xi->xi_file = p;
37439+ xi->xi_nfile = idx + 1;
37440+ } else {
37441+ err = -ENOMEM;
37442+ if (file)
37443+ fput(file);
37444+ goto out;
37445+ }
37446+ }
37447+ xi->xi_file[idx] = file;
37448+
37449+out:
37450+ return err;
37451+}
37452+
37453+/*
37454+ * if @xinew->xi is not set, then create new xigen file.
37455+ */
37456+struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew)
37457+{
37458+ struct file *file;
37459+ int err;
37460+
37461+ SiMustAnyLock(sb);
37462+
37463+ file = au_xino_create2(sb, xinew->base, xinew->copy_src);
37464+ if (IS_ERR(file)) {
37465+ err = PTR_ERR(file);
37466+ pr_err("%s[%d], err %d\n",
37467+ xinew->xi ? "xino" : "xigen",
37468+ xinew->idx, err);
37469+ goto out;
37470+ }
37471+
37472+ if (xinew->xi)
37473+ err = au_xino_file_set(xinew->xi, xinew->idx, file);
37474+ else {
37475+ BUG();
37476+ /* todo: make xigen file an array */
37477+ /* err = au_xigen_file_set(sb, xinew->idx, file); */
37478+ }
37479+ fput(file);
37480+ if (unlikely(err))
37481+ file = ERR_PTR(err);
37482+
37483+out:
37484+ return file;
37485+}
37486+
062440b3
AM
37487+/* ---------------------------------------------------------------------- */
37488+
37489+/*
37490+ * truncate xino files
37491+ */
acd2b654
AM
37492+static int au_xino_do_trunc(struct super_block *sb, aufs_bindex_t bindex,
37493+ int idx, struct kstatfs *st)
1facf9fc 37494+{
37495+ int err;
392086de 37496+ blkcnt_t blocks;
acd2b654
AM
37497+ struct file *file, *new_xino;
37498+ struct au_xi_new xinew = {
37499+ .idx = idx
37500+ };
37501+
37502+ err = 0;
37503+ xinew.xi = au_sbr(sb, bindex)->br_xino;
37504+ file = au_xino_file(xinew.xi, idx);
37505+ if (!file)
37506+ goto out;
37507+
37508+ xinew.base = &file->f_path;
37509+ err = vfs_statfs(xinew.base, st);
37510+ if (unlikely(err)) {
37511+ AuErr1("statfs err %d, ignored\n", err);
37512+ err = 0;
37513+ goto out;
37514+ }
37515+
37516+ blocks = file_inode(file)->i_blocks;
37517+ pr_info("begin truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37518+ bindex, idx, (u64)blocks, st->f_bfree, st->f_blocks);
37519+
37520+ xinew.copy_src = file;
37521+ new_xino = au_xi_new(sb, &xinew);
37522+ if (IS_ERR(new_xino)) {
37523+ err = PTR_ERR(new_xino);
37524+ pr_err("xino(b%d-%d), err %d, ignored\n", bindex, idx, err);
37525+ goto out;
37526+ }
37527+
37528+ err = vfs_statfs(&new_xino->f_path, st);
37529+ if (!err)
37530+ pr_info("end truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37531+ bindex, idx, (u64)file_inode(new_xino)->i_blocks,
37532+ st->f_bfree, st->f_blocks);
37533+ else {
37534+ AuErr1("statfs err %d, ignored\n", err);
37535+ err = 0;
37536+ }
37537+
37538+out:
37539+ return err;
37540+}
37541+
37542+int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin)
37543+{
37544+ int err, i;
37545+ unsigned long jiffy;
062440b3 37546+ aufs_bindex_t bbot;
392086de 37547+ struct kstatfs *st;
1facf9fc 37548+ struct au_branch *br;
acd2b654 37549+ struct au_xino *xi;
1facf9fc 37550+
392086de 37551+ err = -ENOMEM;
be52b249 37552+ st = kmalloc(sizeof(*st), GFP_NOFS);
392086de
AM
37553+ if (unlikely(!st))
37554+ goto out;
37555+
1facf9fc 37556+ err = -EINVAL;
5afbbe0d
AM
37557+ bbot = au_sbbot(sb);
37558+ if (unlikely(bindex < 0 || bbot < bindex))
392086de 37559+ goto out_st;
392086de 37560+
1facf9fc 37561+ err = 0;
acd2b654
AM
37562+ jiffy = jiffies;
37563+ br = au_sbr(sb, bindex);
37564+ xi = br->br_xino;
37565+ for (i = idx_begin; !err && i < xi->xi_nfile; i++)
37566+ err = au_xino_do_trunc(sb, bindex, i, st);
37567+ if (!err)
37568+ au_sbi(sb)->si_xino_jiffy = jiffy;
392086de
AM
37569+
37570+out_st:
9f237c51 37571+ au_kfree_rcu(st);
4f0767ce 37572+out:
1facf9fc 37573+ return err;
37574+}
37575+
37576+struct xino_do_trunc_args {
37577+ struct super_block *sb;
37578+ struct au_branch *br;
acd2b654 37579+ int idx;
1facf9fc 37580+};
37581+
37582+static void xino_do_trunc(void *_args)
37583+{
37584+ struct xino_do_trunc_args *args = _args;
37585+ struct super_block *sb;
37586+ struct au_branch *br;
37587+ struct inode *dir;
acd2b654 37588+ int err, idx;
1facf9fc 37589+ aufs_bindex_t bindex;
37590+
37591+ err = 0;
37592+ sb = args->sb;
5527c038 37593+ dir = d_inode(sb->s_root);
1facf9fc 37594+ br = args->br;
acd2b654 37595+ idx = args->idx;
1facf9fc 37596+
37597+ si_noflush_write_lock(sb);
37598+ ii_read_lock_parent(dir);
37599+ bindex = au_br_index(sb, br->br_id);
acd2b654 37600+ err = au_xino_trunc(sb, bindex, idx);
1facf9fc 37601+ ii_read_unlock(dir);
37602+ if (unlikely(err))
392086de 37603+ pr_warn("err b%d, (%d)\n", bindex, err);
062440b3 37604+ atomic_dec(&br->br_xino->xi_truncating);
acd2b654 37605+ au_lcnt_dec(&br->br_count);
1facf9fc 37606+ si_write_unlock(sb);
027c5e7a 37607+ au_nwt_done(&au_sbi(sb)->si_nowait);
9f237c51 37608+ au_kfree_rcu(args);
1facf9fc 37609+}
37610+
acd2b654
AM
37611+/*
37612+ * returns the index in the xi_file array whose corresponding file is necessary
37613+ * to truncate, or -1 which means no need to truncate.
37614+ */
392086de
AM
37615+static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
37616+{
37617+ int err;
acd2b654 37618+ unsigned int u;
392086de
AM
37619+ struct kstatfs st;
37620+ struct au_sbinfo *sbinfo;
acd2b654 37621+ struct au_xino *xi;
062440b3 37622+ struct file *file;
392086de
AM
37623+
37624+ /* todo: si_xino_expire and the ratio should be customizable */
37625+ sbinfo = au_sbi(sb);
37626+ if (time_before(jiffies,
37627+ sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
acd2b654 37628+ return -1;
392086de
AM
37629+
37630+ /* truncation border */
acd2b654
AM
37631+ xi = br->br_xino;
37632+ for (u = 0; u < xi->xi_nfile; u++) {
37633+ file = au_xino_file(xi, u);
37634+ if (!file)
37635+ continue;
37636+
37637+ err = vfs_statfs(&file->f_path, &st);
37638+ if (unlikely(err)) {
37639+ AuErr1("statfs err %d, ignored\n", err);
37640+ return -1;
37641+ }
37642+ if (div64_u64(st.f_bfree * 100, st.f_blocks)
37643+ >= AUFS_XINO_DEF_TRUNC)
37644+ return u;
392086de 37645+ }
392086de 37646+
acd2b654 37647+ return -1;
392086de
AM
37648+}
37649+
1facf9fc 37650+static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
37651+{
acd2b654 37652+ int idx;
1facf9fc 37653+ struct xino_do_trunc_args *args;
37654+ int wkq_err;
37655+
acd2b654
AM
37656+ idx = xino_trunc_test(sb, br);
37657+ if (idx < 0)
1facf9fc 37658+ return;
37659+
062440b3 37660+ if (atomic_inc_return(&br->br_xino->xi_truncating) > 1)
1facf9fc 37661+ goto out;
37662+
37663+ /* lock and kfree() will be called in trunc_xino() */
37664+ args = kmalloc(sizeof(*args), GFP_NOFS);
37665+ if (unlikely(!args)) {
37666+ AuErr1("no memory\n");
f0c0a007 37667+ goto out;
1facf9fc 37668+ }
37669+
acd2b654 37670+ au_lcnt_inc(&br->br_count);
1facf9fc 37671+ args->sb = sb;
37672+ args->br = br;
acd2b654 37673+ args->idx = idx;
53392da6 37674+ wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
1facf9fc 37675+ if (!wkq_err)
37676+ return; /* success */
37677+
4a4d8108 37678+ pr_err("wkq %d\n", wkq_err);
acd2b654 37679+ au_lcnt_dec(&br->br_count);
9f237c51 37680+ au_kfree_rcu(args);
1facf9fc 37681+
4f0767ce 37682+out:
062440b3 37683+ atomic_dec(&br->br_xino->xi_truncating);
1facf9fc 37684+}
37685+
37686+/* ---------------------------------------------------------------------- */
37687+
acd2b654
AM
37688+struct au_xi_calc {
37689+ int idx;
37690+ loff_t pos;
37691+};
37692+
37693+static void au_xi_calc(struct super_block *sb, ino_t h_ino,
37694+ struct au_xi_calc *calc)
37695+{
37696+ loff_t maxent;
37697+
37698+ maxent = au_xi_maxent(sb);
37699+ calc->idx = div64_u64_rem(h_ino, maxent, &calc->pos);
37700+ calc->pos *= sizeof(ino_t);
37701+}
37702+
37703+static int au_xino_do_new_async(struct super_block *sb, struct au_branch *br,
37704+ struct au_xi_calc *calc)
37705+{
37706+ int err;
37707+ struct file *file;
37708+ struct au_xino *xi = br->br_xino;
37709+ struct au_xi_new xinew = {
37710+ .xi = xi
37711+ };
37712+
37713+ SiMustAnyLock(sb);
37714+
37715+ err = 0;
37716+ if (!xi)
37717+ goto out;
37718+
37719+ mutex_lock(&xi->xi_mtx);
37720+ file = au_xino_file(xi, calc->idx);
37721+ if (file)
37722+ goto out_mtx;
37723+
37724+ file = au_xino_file(xi, /*idx*/-1);
37725+ AuDebugOn(!file);
37726+ xinew.idx = calc->idx;
37727+ xinew.base = &file->f_path;
37728+ /* xinew.copy_src = NULL; */
37729+ file = au_xi_new(sb, &xinew);
37730+ if (IS_ERR(file))
37731+ err = PTR_ERR(file);
37732+
37733+out_mtx:
37734+ mutex_unlock(&xi->xi_mtx);
37735+out:
37736+ return err;
37737+}
37738+
37739+struct au_xino_do_new_async_args {
37740+ struct super_block *sb;
37741+ struct au_branch *br;
37742+ struct au_xi_calc calc;
37743+ ino_t ino;
37744+};
37745+
9f237c51
AM
37746+struct au_xi_writing {
37747+ struct hlist_bl_node node;
37748+ ino_t h_ino, ino;
37749+};
37750+
e37dd06a
AM
37751+static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37752+ ino_t ino);
acd2b654
AM
37753+
37754+static void au_xino_call_do_new_async(void *args)
37755+{
37756+ struct au_xino_do_new_async_args *a = args;
37757+ struct au_branch *br;
37758+ struct super_block *sb;
37759+ struct au_sbinfo *sbi;
37760+ struct inode *root;
37761+ struct file *file;
9f237c51
AM
37762+ struct au_xi_writing *del, *p;
37763+ struct hlist_bl_head *hbl;
37764+ struct hlist_bl_node *pos;
acd2b654
AM
37765+ int err;
37766+
37767+ br = a->br;
37768+ sb = a->sb;
37769+ sbi = au_sbi(sb);
37770+ si_noflush_read_lock(sb);
37771+ root = d_inode(sb->s_root);
37772+ ii_read_lock_child(root);
37773+ err = au_xino_do_new_async(sb, br, &a->calc);
9f237c51
AM
37774+ if (unlikely(err)) {
37775+ AuIOErr("err %d\n", err);
37776+ goto out;
37777+ }
37778+
37779+ file = au_xino_file(br->br_xino, a->calc.idx);
37780+ AuDebugOn(!file);
e37dd06a 37781+ err = au_xino_do_write(file, &a->calc, a->ino);
9f237c51 37782+ if (unlikely(err)) {
acd2b654 37783+ AuIOErr("err %d\n", err);
9f237c51
AM
37784+ goto out;
37785+ }
37786+
37787+ del = NULL;
37788+ hbl = &br->br_xino->xi_writing;
37789+ hlist_bl_lock(hbl);
37790+ au_hbl_for_each(pos, hbl) {
37791+ p = container_of(pos, struct au_xi_writing, node);
37792+ if (p->ino == a->ino) {
37793+ del = p;
37794+ hlist_bl_del(&p->node);
37795+ break;
37796+ }
37797+ }
37798+ hlist_bl_unlock(hbl);
37799+ au_kfree_rcu(del);
37800+
37801+out:
acd2b654
AM
37802+ au_lcnt_dec(&br->br_count);
37803+ ii_read_unlock(root);
37804+ si_read_unlock(sb);
37805+ au_nwt_done(&sbi->si_nowait);
9f237c51 37806+ au_kfree_rcu(a);
acd2b654
AM
37807+}
37808+
37809+/*
37810+ * create a new xino file asynchronously
37811+ */
37812+static int au_xino_new_async(struct super_block *sb, struct au_branch *br,
37813+ struct au_xi_calc *calc, ino_t ino)
37814+{
37815+ int err;
37816+ struct au_xino_do_new_async_args *arg;
37817+
37818+ err = -ENOMEM;
37819+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
37820+ if (unlikely(!arg))
37821+ goto out;
37822+
37823+ arg->sb = sb;
37824+ arg->br = br;
37825+ arg->calc = *calc;
37826+ arg->ino = ino;
37827+ au_lcnt_inc(&br->br_count);
37828+ err = au_wkq_nowait(au_xino_call_do_new_async, arg, sb, AuWkq_NEST);
37829+ if (unlikely(err)) {
37830+ pr_err("wkq %d\n", err);
37831+ au_lcnt_dec(&br->br_count);
9f237c51 37832+ au_kfree_rcu(arg);
acd2b654
AM
37833+ }
37834+
37835+out:
37836+ return err;
37837+}
37838+
062440b3
AM
37839+/*
37840+ * read @ino from xinofile for the specified branch{@sb, @bindex}
37841+ * at the position of @h_ino.
37842+ */
37843+int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37844+ ino_t *ino)
37845+{
37846+ int err;
37847+ ssize_t sz;
acd2b654 37848+ struct au_xi_calc calc;
062440b3 37849+ struct au_sbinfo *sbinfo;
acd2b654 37850+ struct file *file;
9f237c51
AM
37851+ struct au_xino *xi;
37852+ struct hlist_bl_head *hbl;
37853+ struct hlist_bl_node *pos;
37854+ struct au_xi_writing *p;
062440b3
AM
37855+
37856+ *ino = 0;
37857+ if (!au_opt_test(au_mntflags(sb), XINO))
37858+ return 0; /* no xino */
37859+
37860+ err = 0;
acd2b654 37861+ au_xi_calc(sb, h_ino, &calc);
9f237c51
AM
37862+ xi = au_sbr(sb, bindex)->br_xino;
37863+ file = au_xino_file(xi, calc.idx);
37864+ if (!file) {
37865+ hbl = &xi->xi_writing;
37866+ hlist_bl_lock(hbl);
37867+ au_hbl_for_each(pos, hbl) {
37868+ p = container_of(pos, struct au_xi_writing, node);
37869+ if (p->h_ino == h_ino) {
37870+ AuDbg("hi%llu, i%llu, found\n",
37871+ (u64)p->h_ino, (u64)p->ino);
37872+ *ino = p->ino;
37873+ break;
37874+ }
37875+ }
37876+ hlist_bl_unlock(hbl);
37877+ return 0;
37878+ } else if (vfsub_f_size_read(file) < calc.pos + sizeof(*ino))
37879+ return 0; /* no xino */
062440b3 37880+
acd2b654 37881+ sbinfo = au_sbi(sb);
e37dd06a 37882+ sz = xino_fread(file, ino, sizeof(*ino), &calc.pos);
062440b3
AM
37883+ if (sz == sizeof(*ino))
37884+ return 0; /* success */
37885+
37886+ err = sz;
37887+ if (unlikely(sz >= 0)) {
37888+ err = -EIO;
37889+ AuIOErr("xino read error (%zd)\n", sz);
37890+ }
062440b3
AM
37891+ return err;
37892+}
37893+
e37dd06a
AM
37894+static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37895+ ino_t ino)
1facf9fc 37896+{
1facf9fc 37897+ ssize_t sz;
37898+
e37dd06a 37899+ sz = xino_fwrite(file, &ino, sizeof(ino), &calc->pos);
1facf9fc 37900+ if (sz == sizeof(ino))
37901+ return 0; /* success */
37902+
37903+ AuIOErr("write failed (%zd)\n", sz);
37904+ return -EIO;
37905+}
37906+
37907+/*
37908+ * write @ino to the xinofile for the specified branch{@sb, @bindex}
37909+ * at the position of @h_ino.
37910+ * even if @ino is zero, it is written to the xinofile and means no entry.
37911+ * if the size of the xino file on a specific filesystem exceeds the watermark,
37912+ * try truncating it.
37913+ */
37914+int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37915+ ino_t ino)
37916+{
37917+ int err;
37918+ unsigned int mnt_flags;
acd2b654
AM
37919+ struct au_xi_calc calc;
37920+ struct file *file;
1facf9fc 37921+ struct au_branch *br;
acd2b654 37922+ struct au_xino *xi;
9f237c51 37923+ struct au_xi_writing *p;
1facf9fc 37924+
dece6358 37925+ SiMustAnyLock(sb);
1facf9fc 37926+
37927+ mnt_flags = au_mntflags(sb);
37928+ if (!au_opt_test(mnt_flags, XINO))
37929+ return 0;
37930+
acd2b654 37931+ au_xi_calc(sb, h_ino, &calc);
1facf9fc 37932+ br = au_sbr(sb, bindex);
acd2b654
AM
37933+ xi = br->br_xino;
37934+ file = au_xino_file(xi, calc.idx);
37935+ if (!file) {
9f237c51
AM
37936+ /* store the inum pair into the list */
37937+ p = kmalloc(sizeof(*p), GFP_NOFS | __GFP_NOFAIL);
37938+ p->h_ino = h_ino;
37939+ p->ino = ino;
37940+ au_hbl_add(&p->node, &xi->xi_writing);
37941+
acd2b654
AM
37942+ /* create and write a new xino file asynchronously */
37943+ err = au_xino_new_async(sb, br, &calc, ino);
37944+ if (!err)
37945+ return 0; /* success */
37946+ goto out;
37947+ }
37948+
e37dd06a 37949+ err = au_xino_do_write(file, &calc, ino);
1facf9fc 37950+ if (!err) {
acd2b654 37951+ br = au_sbr(sb, bindex);
1facf9fc 37952+ if (au_opt_test(mnt_flags, TRUNC_XINO)
86dc4139 37953+ && au_test_fs_trunc_xino(au_br_sb(br)))
1facf9fc 37954+ xino_try_trunc(sb, br);
37955+ return 0; /* success */
37956+ }
37957+
acd2b654 37958+out:
1facf9fc 37959+ AuIOErr("write failed (%d)\n", err);
37960+ return -EIO;
37961+}
37962+
e37dd06a
AM
37963+static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
37964+ loff_t *pos);
1facf9fc 37965+
062440b3 37966+/* todo: unnecessary to support mmap_sem since kernel-space? */
e37dd06a 37967+ssize_t xino_fread(struct file *file, void *kbuf, size_t size, loff_t *pos)
062440b3
AM
37968+{
37969+ ssize_t err;
062440b3
AM
37970+ int i;
37971+ const int prevent_endless = 10;
1facf9fc 37972+
062440b3 37973+ i = 0;
062440b3 37974+ do {
e37dd06a 37975+ err = vfsub_read_k(file, kbuf, size, pos);
062440b3
AM
37976+ if (err == -EINTR
37977+ && !au_wkq_test()
37978+ && fatal_signal_pending(current)) {
e37dd06a 37979+ err = xino_fread_wkq(file, kbuf, size, pos);
062440b3 37980+ BUG_ON(err == -EINTR);
062440b3
AM
37981+ }
37982+ } while (i++ < prevent_endless
37983+ && (err == -EAGAIN || err == -EINTR));
062440b3
AM
37984+
37985+#if 0 /* reserved for future use */
37986+ if (err > 0)
37987+ fsnotify_access(file->f_path.dentry);
37988+#endif
37989+
37990+ return err;
37991+}
37992+
37993+struct xino_fread_args {
37994+ ssize_t *errp;
062440b3
AM
37995+ struct file *file;
37996+ void *buf;
37997+ size_t size;
37998+ loff_t *pos;
37999+};
38000+
38001+static void call_xino_fread(void *args)
38002+{
38003+ struct xino_fread_args *a = args;
e37dd06a 38004+ *a->errp = xino_fread(a->file, a->buf, a->size, a->pos);
062440b3
AM
38005+}
38006+
e37dd06a
AM
38007+static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
38008+ loff_t *pos)
062440b3
AM
38009+{
38010+ ssize_t err;
38011+ int wkq_err;
38012+ struct xino_fread_args args = {
38013+ .errp = &err,
062440b3
AM
38014+ .file = file,
38015+ .buf = buf,
38016+ .size = size,
38017+ .pos = pos
38018+ };
38019+
38020+ wkq_err = au_wkq_wait(call_xino_fread, &args);
38021+ if (unlikely(wkq_err))
38022+ err = wkq_err;
38023+
38024+ return err;
38025+}
38026+
e37dd06a
AM
38027+static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
38028+ loff_t *pos);
062440b3 38029+
e37dd06a
AM
38030+static ssize_t do_xino_fwrite(struct file *file, void *kbuf, size_t size,
38031+ loff_t *pos)
062440b3
AM
38032+{
38033+ ssize_t err;
062440b3
AM
38034+ int i;
38035+ const int prevent_endless = 10;
38036+
38037+ i = 0;
062440b3 38038+ do {
e37dd06a 38039+ err = vfsub_write_k(file, kbuf, size, pos);
062440b3
AM
38040+ if (err == -EINTR
38041+ && !au_wkq_test()
38042+ && fatal_signal_pending(current)) {
e37dd06a 38043+ err = xino_fwrite_wkq(file, kbuf, size, pos);
062440b3 38044+ BUG_ON(err == -EINTR);
062440b3
AM
38045+ }
38046+ } while (i++ < prevent_endless
38047+ && (err == -EAGAIN || err == -EINTR));
062440b3
AM
38048+
38049+#if 0 /* reserved for future use */
38050+ if (err > 0)
38051+ fsnotify_modify(file->f_path.dentry);
38052+#endif
38053+
38054+ return err;
38055+}
38056+
38057+struct do_xino_fwrite_args {
38058+ ssize_t *errp;
062440b3
AM
38059+ struct file *file;
38060+ void *buf;
38061+ size_t size;
38062+ loff_t *pos;
38063+};
38064+
38065+static void call_do_xino_fwrite(void *args)
38066+{
38067+ struct do_xino_fwrite_args *a = args;
e37dd06a 38068+ *a->errp = do_xino_fwrite(a->file, a->buf, a->size, a->pos);
062440b3
AM
38069+}
38070+
e37dd06a
AM
38071+static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
38072+ loff_t *pos)
062440b3
AM
38073+{
38074+ ssize_t err;
38075+ int wkq_err;
38076+ struct do_xino_fwrite_args args = {
38077+ .errp = &err,
062440b3
AM
38078+ .file = file,
38079+ .buf = buf,
38080+ .size = size,
38081+ .pos = pos
38082+ };
38083+
38084+ /*
38085+ * it breaks RLIMIT_FSIZE and normal user's limit,
38086+ * users should care about quota and real 'filesystem full.'
38087+ */
38088+ wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
38089+ if (unlikely(wkq_err))
38090+ err = wkq_err;
38091+
38092+ return err;
38093+}
38094+
e37dd06a 38095+ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos)
062440b3
AM
38096+{
38097+ ssize_t err;
38098+
38099+ if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
38100+ lockdep_off();
e37dd06a 38101+ err = do_xino_fwrite(file, buf, size, pos);
062440b3
AM
38102+ lockdep_on();
38103+ } else {
38104+ lockdep_off();
e37dd06a 38105+ err = xino_fwrite_wkq(file, buf, size, pos);
062440b3
AM
38106+ lockdep_on();
38107+ }
38108+
38109+ return err;
38110+}
38111+
38112+/* ---------------------------------------------------------------------- */
38113+
38114+/*
38115+ * inode number bitmap
38116+ */
1facf9fc 38117+static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
38118+static ino_t xib_calc_ino(unsigned long pindex, int bit)
38119+{
38120+ ino_t ino;
38121+
38122+ AuDebugOn(bit < 0 || page_bits <= bit);
38123+ ino = AUFS_FIRST_INO + pindex * page_bits + bit;
38124+ return ino;
38125+}
38126+
38127+static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
38128+{
38129+ AuDebugOn(ino < AUFS_FIRST_INO);
38130+ ino -= AUFS_FIRST_INO;
38131+ *pindex = ino / page_bits;
38132+ *bit = ino % page_bits;
38133+}
38134+
38135+static int xib_pindex(struct super_block *sb, unsigned long pindex)
38136+{
38137+ int err;
38138+ loff_t pos;
38139+ ssize_t sz;
38140+ struct au_sbinfo *sbinfo;
38141+ struct file *xib;
38142+ unsigned long *p;
38143+
38144+ sbinfo = au_sbi(sb);
38145+ MtxMustLock(&sbinfo->si_xib_mtx);
38146+ AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
38147+ || !au_opt_test(sbinfo->si_mntflags, XINO));
38148+
38149+ if (pindex == sbinfo->si_xib_last_pindex)
38150+ return 0;
38151+
38152+ xib = sbinfo->si_xib;
38153+ p = sbinfo->si_xib_buf;
38154+ pos = sbinfo->si_xib_last_pindex;
38155+ pos *= PAGE_SIZE;
e37dd06a 38156+ sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
1facf9fc 38157+ if (unlikely(sz != PAGE_SIZE))
38158+ goto out;
38159+
38160+ pos = pindex;
38161+ pos *= PAGE_SIZE;
c06a8ce3 38162+ if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
e37dd06a 38163+ sz = xino_fread(xib, p, PAGE_SIZE, &pos);
1facf9fc 38164+ else {
38165+ memset(p, 0, PAGE_SIZE);
e37dd06a 38166+ sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
1facf9fc 38167+ }
38168+ if (sz == PAGE_SIZE) {
38169+ sbinfo->si_xib_last_pindex = pindex;
38170+ return 0; /* success */
38171+ }
38172+
4f0767ce 38173+out:
b752ccd1
AM
38174+ AuIOErr1("write failed (%zd)\n", sz);
38175+ err = sz;
38176+ if (sz >= 0)
38177+ err = -EIO;
38178+ return err;
38179+}
38180+
b752ccd1
AM
38181+static void au_xib_clear_bit(struct inode *inode)
38182+{
38183+ int err, bit;
38184+ unsigned long pindex;
38185+ struct super_block *sb;
38186+ struct au_sbinfo *sbinfo;
38187+
38188+ AuDebugOn(inode->i_nlink);
38189+
38190+ sb = inode->i_sb;
38191+ xib_calc_bit(inode->i_ino, &pindex, &bit);
38192+ AuDebugOn(page_bits <= bit);
38193+ sbinfo = au_sbi(sb);
38194+ mutex_lock(&sbinfo->si_xib_mtx);
38195+ err = xib_pindex(sb, pindex);
38196+ if (!err) {
38197+ clear_bit(bit, sbinfo->si_xib_buf);
38198+ sbinfo->si_xib_next_bit = bit;
38199+ }
38200+ mutex_unlock(&sbinfo->si_xib_mtx);
38201+}
38202+
1facf9fc 38203+/* ---------------------------------------------------------------------- */
38204+
1facf9fc 38205+/*
062440b3 38206+ * truncate a xino bitmap file
1facf9fc 38207+ */
1facf9fc 38208+
38209+/* todo: slow */
38210+static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
38211+{
38212+ int err, bit;
38213+ ssize_t sz;
38214+ unsigned long pindex;
38215+ loff_t pos, pend;
38216+ struct au_sbinfo *sbinfo;
1facf9fc 38217+ ino_t *ino;
38218+ unsigned long *p;
38219+
38220+ err = 0;
38221+ sbinfo = au_sbi(sb);
dece6358 38222+ MtxMustLock(&sbinfo->si_xib_mtx);
1facf9fc 38223+ p = sbinfo->si_xib_buf;
c06a8ce3 38224+ pend = vfsub_f_size_read(file);
1facf9fc 38225+ pos = 0;
38226+ while (pos < pend) {
e37dd06a 38227+ sz = xino_fread(file, page, PAGE_SIZE, &pos);
1facf9fc 38228+ err = sz;
38229+ if (unlikely(sz <= 0))
38230+ goto out;
38231+
38232+ err = 0;
38233+ for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
38234+ if (unlikely(*ino < AUFS_FIRST_INO))
38235+ continue;
38236+
38237+ xib_calc_bit(*ino, &pindex, &bit);
38238+ AuDebugOn(page_bits <= bit);
38239+ err = xib_pindex(sb, pindex);
38240+ if (!err)
38241+ set_bit(bit, p);
38242+ else
38243+ goto out;
38244+ }
38245+ }
38246+
4f0767ce 38247+out:
1facf9fc 38248+ return err;
38249+}
38250+
38251+static int xib_restore(struct super_block *sb)
38252+{
acd2b654
AM
38253+ int err, i;
38254+ unsigned int nfile;
5afbbe0d 38255+ aufs_bindex_t bindex, bbot;
1facf9fc 38256+ void *page;
062440b3 38257+ struct au_branch *br;
acd2b654
AM
38258+ struct au_xino *xi;
38259+ struct file *file;
1facf9fc 38260+
38261+ err = -ENOMEM;
38262+ page = (void *)__get_free_page(GFP_NOFS);
38263+ if (unlikely(!page))
38264+ goto out;
38265+
38266+ err = 0;
5afbbe0d
AM
38267+ bbot = au_sbbot(sb);
38268+ for (bindex = 0; !err && bindex <= bbot; bindex++)
062440b3
AM
38269+ if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0) {
38270+ br = au_sbr(sb, bindex);
acd2b654
AM
38271+ xi = br->br_xino;
38272+ nfile = xi->xi_nfile;
38273+ for (i = 0; i < nfile; i++) {
38274+ file = au_xino_file(xi, i);
38275+ if (file)
38276+ err = do_xib_restore(sb, file, page);
38277+ }
062440b3
AM
38278+ } else
38279+ AuDbg("skip shared b%d\n", bindex);
1c60b727 38280+ free_page((unsigned long)page);
1facf9fc 38281+
4f0767ce 38282+out:
1facf9fc 38283+ return err;
38284+}
38285+
38286+int au_xib_trunc(struct super_block *sb)
38287+{
38288+ int err;
38289+ ssize_t sz;
38290+ loff_t pos;
1facf9fc 38291+ struct au_sbinfo *sbinfo;
38292+ unsigned long *p;
38293+ struct file *file;
38294+
dece6358
AM
38295+ SiMustWriteLock(sb);
38296+
1facf9fc 38297+ err = 0;
38298+ sbinfo = au_sbi(sb);
38299+ if (!au_opt_test(sbinfo->si_mntflags, XINO))
38300+ goto out;
38301+
38302+ file = sbinfo->si_xib;
c06a8ce3 38303+ if (vfsub_f_size_read(file) <= PAGE_SIZE)
1facf9fc 38304+ goto out;
38305+
062440b3 38306+ file = au_xino_create2(sb, &sbinfo->si_xib->f_path, NULL);
1facf9fc 38307+ err = PTR_ERR(file);
38308+ if (IS_ERR(file))
38309+ goto out;
38310+ fput(sbinfo->si_xib);
38311+ sbinfo->si_xib = file;
38312+
38313+ p = sbinfo->si_xib_buf;
38314+ memset(p, 0, PAGE_SIZE);
38315+ pos = 0;
e37dd06a 38316+ sz = xino_fwrite(sbinfo->si_xib, p, PAGE_SIZE, &pos);
1facf9fc 38317+ if (unlikely(sz != PAGE_SIZE)) {
38318+ err = sz;
38319+ AuIOErr("err %d\n", err);
38320+ if (sz >= 0)
38321+ err = -EIO;
38322+ goto out;
38323+ }
38324+
38325+ mutex_lock(&sbinfo->si_xib_mtx);
38326+ /* mnt_want_write() is unnecessary here */
38327+ err = xib_restore(sb);
38328+ mutex_unlock(&sbinfo->si_xib_mtx);
38329+
38330+out:
38331+ return err;
38332+}
38333+
38334+/* ---------------------------------------------------------------------- */
38335+
acd2b654 38336+struct au_xino *au_xino_alloc(unsigned int nfile)
062440b3
AM
38337+{
38338+ struct au_xino *xi;
38339+
38340+ xi = kzalloc(sizeof(*xi), GFP_NOFS);
38341+ if (unlikely(!xi))
38342+ goto out;
acd2b654
AM
38343+ xi->xi_nfile = nfile;
38344+ xi->xi_file = kcalloc(nfile, sizeof(*xi->xi_file), GFP_NOFS);
38345+ if (unlikely(!xi->xi_file))
38346+ goto out_free;
062440b3
AM
38347+
38348+ xi->xi_nondir.total = 8; /* initial size */
38349+ xi->xi_nondir.array = kcalloc(xi->xi_nondir.total, sizeof(ino_t),
38350+ GFP_NOFS);
38351+ if (unlikely(!xi->xi_nondir.array))
acd2b654 38352+ goto out_file;
062440b3
AM
38353+
38354+ spin_lock_init(&xi->xi_nondir.spin);
38355+ init_waitqueue_head(&xi->xi_nondir.wqh);
acd2b654 38356+ mutex_init(&xi->xi_mtx);
9f237c51 38357+ INIT_HLIST_BL_HEAD(&xi->xi_writing);
062440b3
AM
38358+ atomic_set(&xi->xi_truncating, 0);
38359+ kref_init(&xi->xi_kref);
38360+ goto out; /* success */
38361+
acd2b654 38362+out_file:
9f237c51 38363+ au_kfree_try_rcu(xi->xi_file);
062440b3 38364+out_free:
9f237c51 38365+ au_kfree_rcu(xi);
062440b3
AM
38366+ xi = NULL;
38367+out:
38368+ return xi;
38369+}
38370+
acd2b654 38371+static int au_xino_init(struct au_branch *br, int idx, struct file *file)
062440b3
AM
38372+{
38373+ int err;
38374+ struct au_xino *xi;
38375+
38376+ err = 0;
acd2b654 38377+ xi = au_xino_alloc(idx + 1);
062440b3
AM
38378+ if (unlikely(!xi)) {
38379+ err = -ENOMEM;
38380+ goto out;
38381+ }
38382+
acd2b654
AM
38383+ if (file)
38384+ get_file(file);
38385+ xi->xi_file[idx] = file;
062440b3
AM
38386+ AuDebugOn(br->br_xino);
38387+ br->br_xino = xi;
38388+
38389+out:
38390+ return err;
38391+}
38392+
38393+static void au_xino_release(struct kref *kref)
38394+{
38395+ struct au_xino *xi;
38396+ int i;
9f237c51
AM
38397+ unsigned long ul;
38398+ struct hlist_bl_head *hbl;
38399+ struct hlist_bl_node *pos, *n;
38400+ struct au_xi_writing *p;
062440b3
AM
38401+
38402+ xi = container_of(kref, struct au_xino, xi_kref);
acd2b654
AM
38403+ for (i = 0; i < xi->xi_nfile; i++)
38404+ if (xi->xi_file[i])
38405+ fput(xi->xi_file[i]);
062440b3
AM
38406+ for (i = xi->xi_nondir.total - 1; i >= 0; i--)
38407+ AuDebugOn(xi->xi_nondir.array[i]);
acd2b654 38408+ mutex_destroy(&xi->xi_mtx);
9f237c51
AM
38409+ hbl = &xi->xi_writing;
38410+ ul = au_hbl_count(hbl);
38411+ if (unlikely(ul)) {
38412+ pr_warn("xi_writing %lu\n", ul);
38413+ hlist_bl_lock(hbl);
43982f53 38414+ hlist_bl_for_each_entry_safe(p, pos, n, hbl, node) {
9f237c51 38415+ hlist_bl_del(&p->node);
394e211a
AM
38416+ /* kmemleak reported au_kfree_rcu() doesn't free it */
38417+ kfree(p);
9f237c51
AM
38418+ }
38419+ hlist_bl_unlock(hbl);
38420+ }
38421+ au_kfree_try_rcu(xi->xi_file);
38422+ au_kfree_try_rcu(xi->xi_nondir.array);
38423+ au_kfree_rcu(xi);
062440b3
AM
38424+}
38425+
38426+int au_xino_put(struct au_branch *br)
38427+{
38428+ int ret;
38429+ struct au_xino *xi;
38430+
38431+ ret = 0;
38432+ xi = br->br_xino;
38433+ if (xi) {
38434+ br->br_xino = NULL;
38435+ ret = kref_put(&xi->xi_kref, au_xino_release);
38436+ }
38437+
38438+ return ret;
38439+}
38440+
062440b3
AM
38441+/* ---------------------------------------------------------------------- */
38442+
1facf9fc 38443+/*
38444+ * xino mount option handlers
38445+ */
1facf9fc 38446+
38447+/* xino bitmap */
38448+static void xino_clear_xib(struct super_block *sb)
38449+{
38450+ struct au_sbinfo *sbinfo;
38451+
dece6358
AM
38452+ SiMustWriteLock(sb);
38453+
1facf9fc 38454+ sbinfo = au_sbi(sb);
1facf9fc 38455+ if (sbinfo->si_xib)
38456+ fput(sbinfo->si_xib);
38457+ sbinfo->si_xib = NULL;
f0c0a007 38458+ if (sbinfo->si_xib_buf)
1c60b727 38459+ free_page((unsigned long)sbinfo->si_xib_buf);
1facf9fc 38460+ sbinfo->si_xib_buf = NULL;
38461+}
38462+
062440b3 38463+static int au_xino_set_xib(struct super_block *sb, struct path *path)
1facf9fc 38464+{
38465+ int err;
38466+ loff_t pos;
38467+ struct au_sbinfo *sbinfo;
38468+ struct file *file;
acd2b654 38469+ struct super_block *xi_sb;
1facf9fc 38470+
dece6358
AM
38471+ SiMustWriteLock(sb);
38472+
1facf9fc 38473+ sbinfo = au_sbi(sb);
062440b3 38474+ file = au_xino_create2(sb, path, sbinfo->si_xib);
1facf9fc 38475+ err = PTR_ERR(file);
38476+ if (IS_ERR(file))
38477+ goto out;
38478+ if (sbinfo->si_xib)
38479+ fput(sbinfo->si_xib);
38480+ sbinfo->si_xib = file;
acd2b654
AM
38481+ xi_sb = file_inode(file)->i_sb;
38482+ sbinfo->si_ximaxent = xi_sb->s_maxbytes;
38483+ if (unlikely(sbinfo->si_ximaxent < PAGE_SIZE)) {
38484+ err = -EIO;
38485+ pr_err("s_maxbytes(%llu) on %s is too small\n",
38486+ (u64)sbinfo->si_ximaxent, au_sbtype(xi_sb));
38487+ goto out_unset;
38488+ }
38489+ sbinfo->si_ximaxent /= sizeof(ino_t);
1facf9fc 38490+
38491+ err = -ENOMEM;
38492+ if (!sbinfo->si_xib_buf)
38493+ sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
38494+ if (unlikely(!sbinfo->si_xib_buf))
38495+ goto out_unset;
38496+
38497+ sbinfo->si_xib_last_pindex = 0;
38498+ sbinfo->si_xib_next_bit = 0;
c06a8ce3 38499+ if (vfsub_f_size_read(file) < PAGE_SIZE) {
1facf9fc 38500+ pos = 0;
e37dd06a 38501+ err = xino_fwrite(file, sbinfo->si_xib_buf, PAGE_SIZE, &pos);
1facf9fc 38502+ if (unlikely(err != PAGE_SIZE))
38503+ goto out_free;
38504+ }
38505+ err = 0;
38506+ goto out; /* success */
38507+
4f0767ce 38508+out_free:
f0c0a007 38509+ if (sbinfo->si_xib_buf)
1c60b727 38510+ free_page((unsigned long)sbinfo->si_xib_buf);
b752ccd1
AM
38511+ sbinfo->si_xib_buf = NULL;
38512+ if (err >= 0)
38513+ err = -EIO;
4f0767ce 38514+out_unset:
b752ccd1
AM
38515+ fput(sbinfo->si_xib);
38516+ sbinfo->si_xib = NULL;
4f0767ce 38517+out:
062440b3 38518+ AuTraceErr(err);
b752ccd1 38519+ return err;
1facf9fc 38520+}
38521+
b752ccd1
AM
38522+/* xino for each branch */
38523+static void xino_clear_br(struct super_block *sb)
38524+{
5afbbe0d 38525+ aufs_bindex_t bindex, bbot;
b752ccd1 38526+ struct au_branch *br;
1facf9fc 38527+
5afbbe0d
AM
38528+ bbot = au_sbbot(sb);
38529+ for (bindex = 0; bindex <= bbot; bindex++) {
b752ccd1 38530+ br = au_sbr(sb, bindex);
062440b3
AM
38531+ AuDebugOn(!br);
38532+ au_xino_put(br);
38533+ }
38534+}
38535+
38536+static void au_xino_set_br_shared(struct super_block *sb, struct au_branch *br,
38537+ aufs_bindex_t bshared)
38538+{
38539+ struct au_branch *brshared;
b752ccd1 38540+
062440b3
AM
38541+ brshared = au_sbr(sb, bshared);
38542+ AuDebugOn(!brshared->br_xino);
38543+ AuDebugOn(!brshared->br_xino->xi_file);
38544+ if (br->br_xino != brshared->br_xino) {
38545+ au_xino_get(brshared);
38546+ au_xino_put(br);
38547+ br->br_xino = brshared->br_xino;
b752ccd1
AM
38548+ }
38549+}
38550+
062440b3 38551+struct au_xino_do_set_br {
062440b3
AM
38552+ struct au_branch *br;
38553+ ino_t h_ino;
38554+ aufs_bindex_t bshared;
38555+};
38556+
38557+static int au_xino_do_set_br(struct super_block *sb, struct path *path,
38558+ struct au_xino_do_set_br *args)
1facf9fc 38559+{
38560+ int err;
acd2b654 38561+ struct au_xi_calc calc;
062440b3 38562+ struct file *file;
acd2b654
AM
38563+ struct au_branch *br;
38564+ struct au_xi_new xinew = {
38565+ .base = path
38566+ };
062440b3 38567+
acd2b654
AM
38568+ br = args->br;
38569+ xinew.xi = br->br_xino;
38570+ au_xi_calc(sb, args->h_ino, &calc);
38571+ xinew.copy_src = au_xino_file(xinew.xi, calc.idx);
38572+ if (args->bshared >= 0)
062440b3 38573+ /* shared xino */
acd2b654
AM
38574+ au_xino_set_br_shared(sb, br, args->bshared);
38575+ else if (!xinew.xi) {
38576+ /* new xino */
38577+ err = au_xino_init(br, calc.idx, xinew.copy_src);
38578+ if (unlikely(err))
38579+ goto out;
062440b3
AM
38580+ }
38581+
acd2b654
AM
38582+ /* force re-creating */
38583+ xinew.xi = br->br_xino;
38584+ xinew.idx = calc.idx;
38585+ mutex_lock(&xinew.xi->xi_mtx);
38586+ file = au_xi_new(sb, &xinew);
38587+ mutex_unlock(&xinew.xi->xi_mtx);
062440b3
AM
38588+ err = PTR_ERR(file);
38589+ if (IS_ERR(file))
38590+ goto out;
acd2b654
AM
38591+ AuDebugOn(!file);
38592+
e37dd06a 38593+ err = au_xino_do_write(file, &calc, AUFS_ROOT_INO);
acd2b654
AM
38594+ if (unlikely(err))
38595+ au_xino_put(br);
062440b3 38596+
062440b3
AM
38597+out:
38598+ AuTraceErr(err);
38599+ return err;
38600+}
38601+
38602+static int au_xino_set_br(struct super_block *sb, struct path *path)
38603+{
38604+ int err;
38605+ aufs_bindex_t bindex, bbot;
38606+ struct au_xino_do_set_br args;
b752ccd1 38607+ struct inode *inode;
1facf9fc 38608+
b752ccd1
AM
38609+ SiMustWriteLock(sb);
38610+
5afbbe0d 38611+ bbot = au_sbbot(sb);
5527c038 38612+ inode = d_inode(sb->s_root);
062440b3
AM
38613+ for (bindex = 0; bindex <= bbot; bindex++) {
38614+ args.h_ino = au_h_iptr(inode, bindex)->i_ino;
38615+ args.br = au_sbr(sb, bindex);
38616+ args.bshared = is_sb_shared(sb, bindex, bindex - 1);
38617+ err = au_xino_do_set_br(sb, path, &args);
b752ccd1 38618+ if (unlikely(err))
062440b3 38619+ break;
b752ccd1 38620+ }
1facf9fc 38621+
062440b3 38622+ AuTraceErr(err);
1facf9fc 38623+ return err;
38624+}
b752ccd1
AM
38625+
38626+void au_xino_clr(struct super_block *sb)
38627+{
38628+ struct au_sbinfo *sbinfo;
38629+
38630+ au_xigen_clr(sb);
38631+ xino_clear_xib(sb);
38632+ xino_clear_br(sb);
062440b3 38633+ dbgaufs_brs_del(sb, 0);
b752ccd1
AM
38634+ sbinfo = au_sbi(sb);
38635+ /* lvalue, do not call au_mntflags() */
38636+ au_opt_clr(sbinfo->si_mntflags, XINO);
38637+}
38638+
062440b3 38639+int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount)
b752ccd1
AM
38640+{
38641+ int err, skip;
062440b3 38642+ struct dentry *dentry, *parent, *cur_dentry, *cur_parent;
b752ccd1
AM
38643+ struct qstr *dname, *cur_name;
38644+ struct file *cur_xino;
b752ccd1 38645+ struct au_sbinfo *sbinfo;
062440b3 38646+ struct path *path, *cur_path;
b752ccd1
AM
38647+
38648+ SiMustWriteLock(sb);
38649+
38650+ err = 0;
38651+ sbinfo = au_sbi(sb);
062440b3
AM
38652+ path = &xiopt->file->f_path;
38653+ dentry = path->dentry;
38654+ parent = dget_parent(dentry);
b752ccd1
AM
38655+ if (remount) {
38656+ skip = 0;
b752ccd1
AM
38657+ cur_xino = sbinfo->si_xib;
38658+ if (cur_xino) {
062440b3
AM
38659+ cur_path = &cur_xino->f_path;
38660+ cur_dentry = cur_path->dentry;
38661+ cur_parent = dget_parent(cur_dentry);
38662+ cur_name = &cur_dentry->d_name;
38663+ dname = &dentry->d_name;
b752ccd1 38664+ skip = (cur_parent == parent
38d290e6 38665+ && au_qstreq(dname, cur_name));
b752ccd1
AM
38666+ dput(cur_parent);
38667+ }
38668+ if (skip)
38669+ goto out;
38670+ }
38671+
38672+ au_opt_set(sbinfo->si_mntflags, XINO);
062440b3
AM
38673+ err = au_xino_set_xib(sb, path);
38674+ /* si_x{read,write} are set */
b752ccd1 38675+ if (!err)
062440b3 38676+ err = au_xigen_set(sb, path);
b752ccd1 38677+ if (!err)
062440b3
AM
38678+ err = au_xino_set_br(sb, path);
38679+ if (!err) {
38680+ dbgaufs_brs_add(sb, 0, /*topdown*/1);
b752ccd1 38681+ goto out; /* success */
062440b3 38682+ }
b752ccd1
AM
38683+
38684+ /* reset all */
062440b3
AM
38685+ AuIOErr("failed setting xino(%d).\n", err);
38686+ au_xino_clr(sb);
b752ccd1 38687+
4f0767ce 38688+out:
b752ccd1
AM
38689+ dput(parent);
38690+ return err;
38691+}
38692+
b752ccd1
AM
38693+/*
38694+ * create a xinofile at the default place/path.
38695+ */
38696+struct file *au_xino_def(struct super_block *sb)
38697+{
38698+ struct file *file;
38699+ char *page, *p;
38700+ struct au_branch *br;
38701+ struct super_block *h_sb;
38702+ struct path path;
5afbbe0d 38703+ aufs_bindex_t bbot, bindex, bwr;
b752ccd1
AM
38704+
38705+ br = NULL;
5afbbe0d 38706+ bbot = au_sbbot(sb);
b752ccd1 38707+ bwr = -1;
5afbbe0d 38708+ for (bindex = 0; bindex <= bbot; bindex++) {
b752ccd1
AM
38709+ br = au_sbr(sb, bindex);
38710+ if (au_br_writable(br->br_perm)
86dc4139 38711+ && !au_test_fs_bad_xino(au_br_sb(br))) {
b752ccd1
AM
38712+ bwr = bindex;
38713+ break;
38714+ }
38715+ }
38716+
7f207e10
AM
38717+ if (bwr >= 0) {
38718+ file = ERR_PTR(-ENOMEM);
537831f9 38719+ page = (void *)__get_free_page(GFP_NOFS);
7f207e10
AM
38720+ if (unlikely(!page))
38721+ goto out;
86dc4139 38722+ path.mnt = au_br_mnt(br);
7f207e10
AM
38723+ path.dentry = au_h_dptr(sb->s_root, bwr);
38724+ p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
38725+ file = (void *)p;
38726+ if (!IS_ERR(p)) {
38727+ strcat(p, "/" AUFS_XINO_FNAME);
38728+ AuDbg("%s\n", p);
83b672a5 38729+ file = au_xino_create(sb, p, /*silent*/0, /*wbrtop*/1);
7f207e10 38730+ }
1c60b727 38731+ free_page((unsigned long)page);
7f207e10 38732+ } else {
83b672a5
AM
38733+ file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0,
38734+ /*wbrtop*/0);
7f207e10
AM
38735+ if (IS_ERR(file))
38736+ goto out;
2000de60 38737+ h_sb = file->f_path.dentry->d_sb;
7f207e10
AM
38738+ if (unlikely(au_test_fs_bad_xino(h_sb))) {
38739+ pr_err("xino doesn't support %s(%s)\n",
38740+ AUFS_XINO_DEFPATH, au_sbtype(h_sb));
38741+ fput(file);
38742+ file = ERR_PTR(-EINVAL);
38743+ }
7f207e10 38744+ }
0c5527e5 38745+
7f207e10
AM
38746+out:
38747+ return file;
38748+}
38749+
38750+/* ---------------------------------------------------------------------- */
38751+
062440b3
AM
38752+/*
38753+ * initialize the xinofile for the specified branch @br
38754+ * at the place/path where @base_file indicates.
38755+ * test whether another branch is on the same filesystem or not,
38756+ * if found then share the xinofile with another branch.
38757+ */
38758+int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
38759+ struct path *base)
7f207e10
AM
38760+{
38761+ int err;
062440b3
AM
38762+ struct au_xino_do_set_br args = {
38763+ .h_ino = h_ino,
38764+ .br = br
38765+ };
7f207e10 38766+
062440b3
AM
38767+ args.bshared = sbr_find_shared(sb, /*btop*/0, au_sbbot(sb),
38768+ au_br_sb(br));
38769+ err = au_xino_do_set_br(sb, base, &args);
79b8bda9 38770+ if (unlikely(err))
062440b3 38771+ au_xino_put(br);
7f207e10 38772+
7f207e10
AM
38773+ return err;
38774+}
521ced18
JR
38775+
38776+/* ---------------------------------------------------------------------- */
38777+
062440b3
AM
38778+/*
38779+ * get an unused inode number from bitmap
38780+ */
38781+ino_t au_xino_new_ino(struct super_block *sb)
38782+{
38783+ ino_t ino;
38784+ unsigned long *p, pindex, ul, pend;
38785+ struct au_sbinfo *sbinfo;
38786+ struct file *file;
38787+ int free_bit, err;
38788+
38789+ if (!au_opt_test(au_mntflags(sb), XINO))
38790+ return iunique(sb, AUFS_FIRST_INO);
38791+
38792+ sbinfo = au_sbi(sb);
38793+ mutex_lock(&sbinfo->si_xib_mtx);
38794+ p = sbinfo->si_xib_buf;
38795+ free_bit = sbinfo->si_xib_next_bit;
38796+ if (free_bit < page_bits && !test_bit(free_bit, p))
38797+ goto out; /* success */
38798+ free_bit = find_first_zero_bit(p, page_bits);
38799+ if (free_bit < page_bits)
38800+ goto out; /* success */
38801+
38802+ pindex = sbinfo->si_xib_last_pindex;
38803+ for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
38804+ err = xib_pindex(sb, ul);
38805+ if (unlikely(err))
38806+ goto out_err;
38807+ free_bit = find_first_zero_bit(p, page_bits);
38808+ if (free_bit < page_bits)
38809+ goto out; /* success */
38810+ }
38811+
38812+ file = sbinfo->si_xib;
38813+ pend = vfsub_f_size_read(file) / PAGE_SIZE;
38814+ for (ul = pindex + 1; ul <= pend; ul++) {
38815+ err = xib_pindex(sb, ul);
38816+ if (unlikely(err))
38817+ goto out_err;
38818+ free_bit = find_first_zero_bit(p, page_bits);
38819+ if (free_bit < page_bits)
38820+ goto out; /* success */
38821+ }
38822+ BUG();
38823+
38824+out:
38825+ set_bit(free_bit, p);
38826+ sbinfo->si_xib_next_bit = free_bit + 1;
38827+ pindex = sbinfo->si_xib_last_pindex;
38828+ mutex_unlock(&sbinfo->si_xib_mtx);
38829+ ino = xib_calc_ino(pindex, free_bit);
38830+ AuDbg("i%lu\n", (unsigned long)ino);
38831+ return ino;
38832+out_err:
38833+ mutex_unlock(&sbinfo->si_xib_mtx);
38834+ AuDbg("i0\n");
38835+ return 0;
38836+}
38837+
38838+/* for s_op->delete_inode() */
38839+void au_xino_delete_inode(struct inode *inode, const int unlinked)
521ced18 38840+{
062440b3
AM
38841+ int err;
38842+ unsigned int mnt_flags;
38843+ aufs_bindex_t bindex, bbot, bi;
38844+ unsigned char try_trunc;
38845+ struct au_iinfo *iinfo;
38846+ struct super_block *sb;
38847+ struct au_hinode *hi;
38848+ struct inode *h_inode;
38849+ struct au_branch *br;
acd2b654
AM
38850+ struct au_xi_calc calc;
38851+ struct file *file;
521ced18 38852+
062440b3 38853+ AuDebugOn(au_is_bad_inode(inode));
521ced18 38854+
062440b3
AM
38855+ sb = inode->i_sb;
38856+ mnt_flags = au_mntflags(sb);
38857+ if (!au_opt_test(mnt_flags, XINO)
38858+ || inode->i_ino == AUFS_ROOT_INO)
38859+ return;
38860+
38861+ if (unlinked) {
38862+ au_xigen_inc(inode);
38863+ au_xib_clear_bit(inode);
38864+ }
38865+
38866+ iinfo = au_ii(inode);
38867+ bindex = iinfo->ii_btop;
38868+ if (bindex < 0)
38869+ return;
38870+
062440b3
AM
38871+ try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
38872+ hi = au_hinode(iinfo, bindex);
38873+ bbot = iinfo->ii_bbot;
38874+ for (; bindex <= bbot; bindex++, hi++) {
38875+ h_inode = hi->hi_inode;
38876+ if (!h_inode
38877+ || (!unlinked && h_inode->i_nlink))
38878+ continue;
38879+
38880+ /* inode may not be revalidated */
38881+ bi = au_br_index(sb, hi->hi_id);
38882+ if (bi < 0)
38883+ continue;
38884+
38885+ br = au_sbr(sb, bi);
acd2b654
AM
38886+ au_xi_calc(sb, h_inode->i_ino, &calc);
38887+ file = au_xino_file(br->br_xino, calc.idx);
38888+ if (IS_ERR_OR_NULL(file))
38889+ continue;
38890+
e37dd06a 38891+ err = au_xino_do_write(file, &calc, /*ino*/0);
062440b3
AM
38892+ if (!err && try_trunc
38893+ && au_test_fs_trunc_xino(au_br_sb(br)))
38894+ xino_try_trunc(sb, br);
38895+ }
521ced18
JR
38896+}
38897+
062440b3
AM
38898+/* ---------------------------------------------------------------------- */
38899+
38900+static int au_xinondir_find(struct au_xino *xi, ino_t h_ino)
521ced18
JR
38901+{
38902+ int found, total, i;
38903+
38904+ found = -1;
062440b3 38905+ total = xi->xi_nondir.total;
521ced18 38906+ for (i = 0; i < total; i++) {
062440b3 38907+ if (xi->xi_nondir.array[i] != h_ino)
521ced18
JR
38908+ continue;
38909+ found = i;
38910+ break;
38911+ }
38912+
38913+ return found;
38914+}
38915+
062440b3 38916+static int au_xinondir_expand(struct au_xino *xi)
521ced18
JR
38917+{
38918+ int err, sz;
38919+ ino_t *p;
38920+
38921+ BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
38922+
38923+ err = -ENOMEM;
062440b3 38924+ sz = xi->xi_nondir.total * sizeof(ino_t);
521ced18
JR
38925+ if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
38926+ goto out;
062440b3 38927+ p = au_kzrealloc(xi->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
521ced18
JR
38928+ /*may_shrink*/0);
38929+ if (p) {
062440b3
AM
38930+ xi->xi_nondir.array = p;
38931+ xi->xi_nondir.total <<= 1;
38932+ AuDbg("xi_nondir.total %d\n", xi->xi_nondir.total);
521ced18
JR
38933+ err = 0;
38934+ }
38935+
38936+out:
38937+ return err;
38938+}
38939+
062440b3
AM
38940+void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
38941+ ino_t h_ino, int idx)
38942+{
38943+ struct au_xino *xi;
38944+
38945+ AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
38946+ xi = au_sbr(sb, bindex)->br_xino;
38947+ AuDebugOn(idx < 0 || xi->xi_nondir.total <= idx);
38948+
38949+ spin_lock(&xi->xi_nondir.spin);
38950+ AuDebugOn(xi->xi_nondir.array[idx] != h_ino);
38951+ xi->xi_nondir.array[idx] = 0;
38952+ spin_unlock(&xi->xi_nondir.spin);
38953+ wake_up_all(&xi->xi_nondir.wqh);
38954+}
38955+
521ced18
JR
38956+int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
38957+ int *idx)
38958+{
38959+ int err, found, empty;
062440b3 38960+ struct au_xino *xi;
521ced18
JR
38961+
38962+ err = 0;
38963+ *idx = -1;
38964+ if (!au_opt_test(au_mntflags(sb), XINO))
38965+ goto out; /* no xino */
38966+
062440b3 38967+ xi = au_sbr(sb, bindex)->br_xino;
521ced18
JR
38968+
38969+again:
062440b3
AM
38970+ spin_lock(&xi->xi_nondir.spin);
38971+ found = au_xinondir_find(xi, h_ino);
521ced18 38972+ if (found == -1) {
062440b3 38973+ empty = au_xinondir_find(xi, /*h_ino*/0);
521ced18 38974+ if (empty == -1) {
062440b3
AM
38975+ empty = xi->xi_nondir.total;
38976+ err = au_xinondir_expand(xi);
521ced18
JR
38977+ if (unlikely(err))
38978+ goto out_unlock;
38979+ }
062440b3 38980+ xi->xi_nondir.array[empty] = h_ino;
521ced18
JR
38981+ *idx = empty;
38982+ } else {
062440b3
AM
38983+ spin_unlock(&xi->xi_nondir.spin);
38984+ wait_event(xi->xi_nondir.wqh,
38985+ xi->xi_nondir.array[found] != h_ino);
521ced18
JR
38986+ goto again;
38987+ }
38988+
38989+out_unlock:
062440b3
AM
38990+ spin_unlock(&xi->xi_nondir.spin);
38991+out:
38992+ return err;
38993+}
38994+
38995+/* ---------------------------------------------------------------------- */
38996+
38997+int au_xino_path(struct seq_file *seq, struct file *file)
38998+{
38999+ int err;
39000+
39001+ err = au_seq_path(seq, &file->f_path);
39002+ if (unlikely(err))
39003+ goto out;
39004+
39005+#define Deleted "\\040(deleted)"
39006+ seq->count -= sizeof(Deleted) - 1;
39007+ AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
39008+ sizeof(Deleted) - 1));
39009+#undef Deleted
39010+
521ced18
JR
39011+out:
39012+ return err;
39013+}
537831f9 39014diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
eca34b5c 39015--- /usr/share/empty/include/uapi/linux/aufs_type.h 1970-01-01 01:00:00.000000000 +0100
2255d0fe 39016+++ linux/include/uapi/linux/aufs_type.h 2022-12-17 09:21:34.803188528 +0100
eca34b5c
AM
39017@@ -0,0 +1,452 @@
39018+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
7f207e10 39019+/*
0dcfbb52 39020+ * Copyright (C) 2005-2022 Junjiro R. Okajima
7f207e10 39021+ *
0dcfbb52 39022+ * This program is free software; you can redistribute it and/or modify
7f207e10
AM
39023+ * it under the terms of the GNU General Public License as published by
39024+ * the Free Software Foundation; either version 2 of the License, or
39025+ * (at your option) any later version.
39026+ *
39027+ * This program is distributed in the hope that it will be useful,
39028+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
39029+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39030+ * GNU General Public License for more details.
39031+ *
39032+ * You should have received a copy of the GNU General Public License
523b37e3 39033+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7f207e10
AM
39034+ */
39035+
39036+#ifndef __AUFS_TYPE_H__
39037+#define __AUFS_TYPE_H__
39038+
f6c5ef8b
AM
39039+#define AUFS_NAME "aufs"
39040+
9dbd164d 39041+#ifdef __KERNEL__
f6c5ef8b
AM
39042+/*
39043+ * define it before including all other headers.
39044+ * sched.h may use pr_* macros before defining "current", so define the
39045+ * no-current version first, and re-define later.
39046+ */
39047+#define pr_fmt(fmt) AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
39048+#include <linux/sched.h>
39049+#undef pr_fmt
a2a7ad62
AM
39050+#define pr_fmt(fmt) \
39051+ AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
39052+ (int)sizeof(current->comm), current->comm, current->pid
43982f53 39053+#include <linux/limits.h>
9dbd164d
AM
39054+#else
39055+#include <stdint.h>
39056+#include <sys/types.h>
43982f53 39057+#include <limits.h>
f6c5ef8b 39058+#endif /* __KERNEL__ */
7f207e10 39059+
2255d0fe 39060+#define AUFS_VERSION "6.x-rcN-20221107"
7f207e10
AM
39061+
39062+/* todo? move this to linux-2.6.19/include/magic.h */
39063+#define AUFS_SUPER_MAGIC ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
39064+
39065+/* ---------------------------------------------------------------------- */
39066+
43982f53 39067+#ifdef __KERNEL__
7f207e10 39068+#ifdef CONFIG_AUFS_BRANCH_MAX_127
9dbd164d 39069+typedef int8_t aufs_bindex_t;
7f207e10
AM
39070+#define AUFS_BRANCH_MAX 127
39071+#else
9dbd164d 39072+typedef int16_t aufs_bindex_t;
7f207e10
AM
39073+#ifdef CONFIG_AUFS_BRANCH_MAX_511
39074+#define AUFS_BRANCH_MAX 511
39075+#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
39076+#define AUFS_BRANCH_MAX 1023
39077+#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
39078+#define AUFS_BRANCH_MAX 32767
39079+#endif
39080+#endif
39081+
7f207e10
AM
39082+#ifndef AUFS_BRANCH_MAX
39083+#error unknown CONFIG_AUFS_BRANCH_MAX value
39084+#endif
39085+#endif /* __KERNEL__ */
39086+
39087+/* ---------------------------------------------------------------------- */
39088+
7f207e10
AM
39089+#define AUFS_FSTYPE AUFS_NAME
39090+
39091+#define AUFS_ROOT_INO 2
39092+#define AUFS_FIRST_INO 11
39093+
39094+#define AUFS_WH_PFX ".wh."
39095+#define AUFS_WH_PFX_LEN ((int)sizeof(AUFS_WH_PFX) - 1)
39096+#define AUFS_WH_TMP_LEN 4
86dc4139 39097+/* a limit for rmdir/rename a dir and copyup */
7f207e10
AM
39098+#define AUFS_MAX_NAMELEN (NAME_MAX \
39099+ - AUFS_WH_PFX_LEN * 2 /* doubly whiteouted */\
39100+ - 1 /* dot */\
39101+ - AUFS_WH_TMP_LEN) /* hex */
39102+#define AUFS_XINO_FNAME "." AUFS_NAME ".xino"
39103+#define AUFS_XINO_DEFPATH "/tmp/" AUFS_XINO_FNAME
392086de
AM
39104+#define AUFS_XINO_DEF_SEC 30 /* seconds */
39105+#define AUFS_XINO_DEF_TRUNC 45 /* percentage */
7f207e10
AM
39106+#define AUFS_DIRWH_DEF 3
39107+#define AUFS_RDCACHE_DEF 10 /* seconds */
027c5e7a 39108+#define AUFS_RDCACHE_MAX 3600 /* seconds */
7f207e10
AM
39109+#define AUFS_RDBLK_DEF 512 /* bytes */
39110+#define AUFS_RDHASH_DEF 32
39111+#define AUFS_WKQ_NAME AUFS_NAME "d"
027c5e7a
AM
39112+#define AUFS_MFS_DEF_SEC 30 /* seconds */
39113+#define AUFS_MFS_MAX_SEC 3600 /* seconds */
076b876e 39114+#define AUFS_FHSM_CACHE_DEF_SEC 30 /* seconds */
86dc4139 39115+#define AUFS_PLINK_WARN 50 /* number of plinks in a single bucket */
7f207e10
AM
39116+
39117+/* pseudo-link maintenace under /proc */
39118+#define AUFS_PLINK_MAINT_NAME "plink_maint"
39119+#define AUFS_PLINK_MAINT_DIR "fs/" AUFS_NAME
39120+#define AUFS_PLINK_MAINT_PATH AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
39121+
8b6a4947
AM
39122+/* dirren, renamed dir */
39123+#define AUFS_DR_INFO_PFX AUFS_WH_PFX ".dr."
39124+#define AUFS_DR_BRHINO_NAME AUFS_WH_PFX "hino"
39125+/* whiteouted doubly */
39126+#define AUFS_WH_DR_INFO_PFX AUFS_WH_PFX AUFS_DR_INFO_PFX
39127+#define AUFS_WH_DR_BRHINO AUFS_WH_PFX AUFS_DR_BRHINO_NAME
39128+
7f207e10
AM
39129+#define AUFS_DIROPQ_NAME AUFS_WH_PFX ".opq" /* whiteouted doubly */
39130+#define AUFS_WH_DIROPQ AUFS_WH_PFX AUFS_DIROPQ_NAME
39131+
39132+#define AUFS_BASE_NAME AUFS_WH_PFX AUFS_NAME
39133+#define AUFS_PLINKDIR_NAME AUFS_WH_PFX "plnk"
39134+#define AUFS_ORPHDIR_NAME AUFS_WH_PFX "orph"
39135+
39136+/* doubly whiteouted */
39137+#define AUFS_WH_BASE AUFS_WH_PFX AUFS_BASE_NAME
39138+#define AUFS_WH_PLINKDIR AUFS_WH_PFX AUFS_PLINKDIR_NAME
39139+#define AUFS_WH_ORPHDIR AUFS_WH_PFX AUFS_ORPHDIR_NAME
39140+
1e00d052 39141+/* branch permissions and attributes */
7f207e10
AM
39142+#define AUFS_BRPERM_RW "rw"
39143+#define AUFS_BRPERM_RO "ro"
39144+#define AUFS_BRPERM_RR "rr"
076b876e
AM
39145+#define AUFS_BRATTR_COO_REG "coo_reg"
39146+#define AUFS_BRATTR_COO_ALL "coo_all"
39147+#define AUFS_BRATTR_FHSM "fhsm"
39148+#define AUFS_BRATTR_UNPIN "unpin"
c1595e42
JR
39149+#define AUFS_BRATTR_ICEX "icex"
39150+#define AUFS_BRATTR_ICEX_SEC "icexsec"
39151+#define AUFS_BRATTR_ICEX_SYS "icexsys"
39152+#define AUFS_BRATTR_ICEX_TR "icextr"
39153+#define AUFS_BRATTR_ICEX_USR "icexusr"
39154+#define AUFS_BRATTR_ICEX_OTH "icexoth"
1e00d052
AM
39155+#define AUFS_BRRATTR_WH "wh"
39156+#define AUFS_BRWATTR_NLWH "nolwh"
076b876e
AM
39157+#define AUFS_BRWATTR_MOO "moo"
39158+
39159+#define AuBrPerm_RW 1 /* writable, hardlinkable wh */
39160+#define AuBrPerm_RO (1 << 1) /* readonly */
39161+#define AuBrPerm_RR (1 << 2) /* natively readonly */
39162+#define AuBrPerm_Mask (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
39163+
39164+#define AuBrAttr_COO_REG (1 << 3) /* copy-up on open */
39165+#define AuBrAttr_COO_ALL (1 << 4)
39166+#define AuBrAttr_COO_Mask (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
39167+
39168+#define AuBrAttr_FHSM (1 << 5) /* file-based hsm */
39169+#define AuBrAttr_UNPIN (1 << 6) /* rename-able top dir of
c1595e42
JR
39170+ branch. meaningless since
39171+ linux-3.18-rc1 */
39172+
39173+/* ignore error in copying XATTR */
39174+#define AuBrAttr_ICEX_SEC (1 << 7)
39175+#define AuBrAttr_ICEX_SYS (1 << 8)
39176+#define AuBrAttr_ICEX_TR (1 << 9)
39177+#define AuBrAttr_ICEX_USR (1 << 10)
39178+#define AuBrAttr_ICEX_OTH (1 << 11)
39179+#define AuBrAttr_ICEX (AuBrAttr_ICEX_SEC \
39180+ | AuBrAttr_ICEX_SYS \
39181+ | AuBrAttr_ICEX_TR \
39182+ | AuBrAttr_ICEX_USR \
39183+ | AuBrAttr_ICEX_OTH)
39184+
39185+#define AuBrRAttr_WH (1 << 12) /* whiteout-able */
076b876e
AM
39186+#define AuBrRAttr_Mask AuBrRAttr_WH
39187+
c1595e42
JR
39188+#define AuBrWAttr_NoLinkWH (1 << 13) /* un-hardlinkable whiteouts */
39189+#define AuBrWAttr_MOO (1 << 14) /* move-up on open */
076b876e
AM
39190+#define AuBrWAttr_Mask (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
39191+
39192+#define AuBrAttr_CMOO_Mask (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
39193+
c1595e42 39194+/* #warning test userspace */
076b876e
AM
39195+#ifdef __KERNEL__
39196+#ifndef CONFIG_AUFS_FHSM
39197+#undef AuBrAttr_FHSM
39198+#define AuBrAttr_FHSM 0
39199+#endif
c1595e42
JR
39200+#ifndef CONFIG_AUFS_XATTR
39201+#undef AuBrAttr_ICEX
39202+#define AuBrAttr_ICEX 0
39203+#undef AuBrAttr_ICEX_SEC
39204+#define AuBrAttr_ICEX_SEC 0
39205+#undef AuBrAttr_ICEX_SYS
39206+#define AuBrAttr_ICEX_SYS 0
39207+#undef AuBrAttr_ICEX_TR
39208+#define AuBrAttr_ICEX_TR 0
39209+#undef AuBrAttr_ICEX_USR
39210+#define AuBrAttr_ICEX_USR 0
39211+#undef AuBrAttr_ICEX_OTH
39212+#define AuBrAttr_ICEX_OTH 0
39213+#endif
076b876e
AM
39214+#endif
39215+
39216+/* the longest combination */
c1595e42
JR
39217+/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
39218+#define AuBrPermStrSz sizeof(AUFS_BRPERM_RW \
39219+ "+" AUFS_BRATTR_COO_REG \
39220+ "+" AUFS_BRATTR_FHSM \
39221+ "+" AUFS_BRATTR_UNPIN \
7e9cd9fe
AM
39222+ "+" AUFS_BRATTR_ICEX_SEC \
39223+ "+" AUFS_BRATTR_ICEX_SYS \
39224+ "+" AUFS_BRATTR_ICEX_USR \
39225+ "+" AUFS_BRATTR_ICEX_OTH \
076b876e
AM
39226+ "+" AUFS_BRWATTR_NLWH)
39227+
39228+typedef struct {
39229+ char a[AuBrPermStrSz];
39230+} au_br_perm_str_t;
39231+
39232+static inline int au_br_writable(int brperm)
39233+{
39234+ return brperm & AuBrPerm_RW;
39235+}
39236+
39237+static inline int au_br_whable(int brperm)
39238+{
39239+ return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
39240+}
39241+
39242+static inline int au_br_wh_linkable(int brperm)
39243+{
39244+ return !(brperm & AuBrWAttr_NoLinkWH);
39245+}
39246+
39247+static inline int au_br_cmoo(int brperm)
39248+{
39249+ return brperm & AuBrAttr_CMOO_Mask;
39250+}
39251+
39252+static inline int au_br_fhsm(int brperm)
39253+{
39254+ return brperm & AuBrAttr_FHSM;
39255+}
7f207e10
AM
39256+
39257+/* ---------------------------------------------------------------------- */
39258+
39259+/* ioctl */
39260+enum {
39261+ /* readdir in userspace */
39262+ AuCtl_RDU,
39263+ AuCtl_RDU_INO,
39264+
076b876e
AM
39265+ AuCtl_WBR_FD, /* pathconf wrapper */
39266+ AuCtl_IBUSY, /* busy inode */
39267+ AuCtl_MVDOWN, /* move-down */
39268+ AuCtl_BR, /* info about branches */
39269+ AuCtl_FHSM_FD /* connection for fhsm */
7f207e10
AM
39270+};
39271+
39272+/* borrowed from linux/include/linux/kernel.h */
39273+#ifndef ALIGN
eca34b5c 39274+#ifdef _GNU_SOURCE
7f207e10 39275+#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
eca34b5c
AM
39276+#else
39277+#define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
39278+#endif
7f207e10
AM
39279+#define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
39280+#endif
39281+
39282+/* borrowed from linux/include/linux/compiler-gcc3.h */
39283+#ifndef __aligned
39284+#define __aligned(x) __attribute__((aligned(x)))
53392da6
AM
39285+#endif
39286+
39287+#ifdef __KERNEL__
39288+#ifndef __packed
7f207e10
AM
39289+#define __packed __attribute__((packed))
39290+#endif
53392da6 39291+#endif
7f207e10
AM
39292+
39293+struct au_rdu_cookie {
9dbd164d
AM
39294+ uint64_t h_pos;
39295+ int16_t bindex;
39296+ uint8_t flags;
39297+ uint8_t pad;
39298+ uint32_t generation;
7f207e10
AM
39299+} __aligned(8);
39300+
39301+struct au_rdu_ent {
9dbd164d
AM
39302+ uint64_t ino;
39303+ int16_t bindex;
39304+ uint8_t type;
39305+ uint8_t nlen;
39306+ uint8_t wh;
42b5c33a 39307+ char name[];
7f207e10
AM
39308+} __aligned(8);
39309+
39310+static inline int au_rdu_len(int nlen)
39311+{
39312+ /* include the terminating NULL */
39313+ return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
9dbd164d 39314+ sizeof(uint64_t));
7f207e10
AM
39315+}
39316+
39317+union au_rdu_ent_ul {
39318+ struct au_rdu_ent __user *e;
9dbd164d 39319+ uint64_t ul;
7f207e10
AM
39320+};
39321+
39322+enum {
39323+ AufsCtlRduV_SZ,
39324+ AufsCtlRduV_End
39325+};
39326+
39327+struct aufs_rdu {
39328+ /* input */
39329+ union {
9dbd164d
AM
39330+ uint64_t sz; /* AuCtl_RDU */
39331+ uint64_t nent; /* AuCtl_RDU_INO */
7f207e10
AM
39332+ };
39333+ union au_rdu_ent_ul ent;
9dbd164d 39334+ uint16_t verify[AufsCtlRduV_End];
7f207e10
AM
39335+
39336+ /* input/output */
9dbd164d 39337+ uint32_t blk;
7f207e10
AM
39338+
39339+ /* output */
39340+ union au_rdu_ent_ul tail;
39341+ /* number of entries which were added in a single call */
9dbd164d
AM
39342+ uint64_t rent;
39343+ uint8_t full;
39344+ uint8_t shwh;
7f207e10
AM
39345+
39346+ struct au_rdu_cookie cookie;
39347+} __aligned(8);
39348+
1e00d052
AM
39349+/* ---------------------------------------------------------------------- */
39350+
8b6a4947
AM
39351+/* dirren. the branch is identified by the filename who contains this */
39352+struct au_drinfo {
39353+ uint64_t ino;
39354+ union {
39355+ uint8_t oldnamelen;
39356+ uint64_t _padding;
39357+ };
42b5c33a 39358+ uint8_t oldname[];
8b6a4947
AM
39359+} __aligned(8);
39360+
39361+struct au_drinfo_fdata {
39362+ uint32_t magic;
39363+ struct au_drinfo drinfo;
39364+} __aligned(8);
39365+
39366+#define AUFS_DRINFO_MAGIC_V1 ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x01)
39367+/* future */
39368+#define AUFS_DRINFO_MAGIC_V2 ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x02)
39369+
39370+/* ---------------------------------------------------------------------- */
39371+
1e00d052 39372+struct aufs_wbr_fd {
9dbd164d
AM
39373+ uint32_t oflags;
39374+ int16_t brid;
1e00d052
AM
39375+} __aligned(8);
39376+
39377+/* ---------------------------------------------------------------------- */
39378+
027c5e7a 39379+struct aufs_ibusy {
9dbd164d
AM
39380+ uint64_t ino, h_ino;
39381+ int16_t bindex;
027c5e7a
AM
39382+} __aligned(8);
39383+
1e00d052
AM
39384+/* ---------------------------------------------------------------------- */
39385+
392086de
AM
39386+/* error code for move-down */
39387+/* the actual message strings are implemented in aufs-util.git */
39388+enum {
39389+ EAU_MVDOWN_OPAQUE = 1,
39390+ EAU_MVDOWN_WHITEOUT,
39391+ EAU_MVDOWN_UPPER,
39392+ EAU_MVDOWN_BOTTOM,
39393+ EAU_MVDOWN_NOUPPER,
39394+ EAU_MVDOWN_NOLOWERBR,
39395+ EAU_Last
39396+};
39397+
c2b27bf2 39398+/* flags for move-down */
392086de
AM
39399+#define AUFS_MVDOWN_DMSG 1
39400+#define AUFS_MVDOWN_OWLOWER (1 << 1) /* overwrite lower */
39401+#define AUFS_MVDOWN_KUPPER (1 << 2) /* keep upper */
39402+#define AUFS_MVDOWN_ROLOWER (1 << 3) /* do even if lower is RO */
39403+#define AUFS_MVDOWN_ROLOWER_R (1 << 4) /* did on lower RO */
39404+#define AUFS_MVDOWN_ROUPPER (1 << 5) /* do even if upper is RO */
39405+#define AUFS_MVDOWN_ROUPPER_R (1 << 6) /* did on upper RO */
39406+#define AUFS_MVDOWN_BRID_UPPER (1 << 7) /* upper brid */
39407+#define AUFS_MVDOWN_BRID_LOWER (1 << 8) /* lower brid */
076b876e
AM
39408+#define AUFS_MVDOWN_FHSM_LOWER (1 << 9) /* find fhsm attr for lower */
39409+#define AUFS_MVDOWN_STFS (1 << 10) /* req. stfs */
39410+#define AUFS_MVDOWN_STFS_FAILED (1 << 11) /* output: stfs is unusable */
39411+#define AUFS_MVDOWN_BOTTOM (1 << 12) /* output: no more lowers */
c2b27bf2 39412+
076b876e 39413+/* index for move-down */
392086de
AM
39414+enum {
39415+ AUFS_MVDOWN_UPPER,
39416+ AUFS_MVDOWN_LOWER,
39417+ AUFS_MVDOWN_NARRAY
39418+};
39419+
076b876e
AM
39420+/*
39421+ * additional info of move-down
39422+ * number of free blocks and inodes.
39423+ * subset of struct kstatfs, but smaller and always 64bit.
39424+ */
39425+struct aufs_stfs {
39426+ uint64_t f_blocks;
39427+ uint64_t f_bavail;
39428+ uint64_t f_files;
39429+ uint64_t f_ffree;
39430+};
39431+
39432+struct aufs_stbr {
39433+ int16_t brid; /* optional input */
39434+ int16_t bindex; /* output */
39435+ struct aufs_stfs stfs; /* output when AUFS_MVDOWN_STFS set */
39436+} __aligned(8);
39437+
c2b27bf2 39438+struct aufs_mvdown {
076b876e
AM
39439+ uint32_t flags; /* input/output */
39440+ struct aufs_stbr stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
39441+ int8_t au_errno; /* output */
39442+} __aligned(8);
39443+
39444+/* ---------------------------------------------------------------------- */
39445+
39446+union aufs_brinfo {
39447+ /* PATH_MAX may differ between kernel-space and user-space */
39448+ char _spacer[4096];
392086de 39449+ struct {
076b876e
AM
39450+ int16_t id;
39451+ int perm;
42b5c33a 39452+ char path[];
076b876e 39453+ };
c2b27bf2
AM
39454+} __aligned(8);
39455+
39456+/* ---------------------------------------------------------------------- */
39457+
7f207e10
AM
39458+#define AuCtlType 'A'
39459+#define AUFS_CTL_RDU _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
39460+#define AUFS_CTL_RDU_INO _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
1e00d052
AM
39461+#define AUFS_CTL_WBR_FD _IOW(AuCtlType, AuCtl_WBR_FD, \
39462+ struct aufs_wbr_fd)
027c5e7a 39463+#define AUFS_CTL_IBUSY _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
392086de
AM
39464+#define AUFS_CTL_MVDOWN _IOWR(AuCtlType, AuCtl_MVDOWN, \
39465+ struct aufs_mvdown)
076b876e
AM
39466+#define AUFS_CTL_BRINFO _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
39467+#define AUFS_CTL_FHSM_FD _IOW(AuCtlType, AuCtl_FHSM_FD, int)
7f207e10
AM
39468+
39469+#endif /* __AUFS_TYPE_H__ */
2121bcd9 39470SPDX-License-Identifier: GPL-2.0
2255d0fe 39471aufs6.x-rcN loopback patch
5527c038
JR
39472
39473diff --git a/drivers/block/loop.c b/drivers/block/loop.c
82da57cb 39474index 8ca56da3ca69..9ac6f67edf09 100644
5527c038
JR
39475--- a/drivers/block/loop.c
39476+++ b/drivers/block/loop.c
22319442
JP
39477@@ -54,7 +54,7 @@ struct loop_device {
39478 int lo_flags;
39479 char lo_file_name[LO_NAME_SIZE];
39480
39481- struct file * lo_backing_file;
39482+ struct file *lo_backing_file, *lo_backing_virt_file;
39483 struct block_device *lo_device;
39484
39485 gfp_t old_gfp_mask;
39486@@ -510,6 +510,15 @@ static inline void loop_update_dio(struct loop_device *lo)
42a736d3 39487 lo->use_dio);
5527c038
JR
39488 }
39489
5527c038
JR
39490+static struct file *loop_real_file(struct file *file)
39491+{
39492+ struct file *f = NULL;
39493+
39494+ if (file->f_path.dentry->d_sb->s_op->real_loop)
39495+ f = file->f_path.dentry->d_sb->s_op->real_loop(file);
39496+ return f;
8b6a4947
AM
39497+}
39498+
6d176551 39499 static void loop_reread_partitions(struct loop_device *lo)
8b6a4947 39500 {
6d176551 39501 int rc;
22319442 39502@@ -567,6 +576,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
5527c038 39503 {
6d176551
JR
39504 struct file *file = fget(arg);
39505 struct file *old_file;
39506+ struct file *f, *virt_file = NULL, *old_virt_file;
39507 int error;
39508 bool partscan;
39509 bool is_loop;
22319442 39510@@ -590,11 +600,19 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
6d176551 39511 if (!(lo->lo_flags & LO_FLAGS_READ_ONLY))
e1106418 39512 goto out_err;
6d176551 39513
5527c038
JR
39514+ f = loop_real_file(file);
39515+ if (f) {
39516+ virt_file = file;
39517+ file = f;
39518+ get_file(file);
39519+ }
6d176551 39520+
cd7a4cd9
AM
39521 error = loop_validate_file(file, bdev);
39522 if (error)
e1106418 39523 goto out_err;
cd7a4cd9 39524
5527c038
JR
39525 old_file = lo->lo_backing_file;
39526+ old_virt_file = lo->lo_backing_virt_file;
39527
39528 error = -EINVAL;
39529
22319442 39530@@ -607,6 +625,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
8b6a4947
AM
39531 blk_mq_freeze_queue(lo->lo_queue);
39532 mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
39533 lo->lo_backing_file = file;
39534+ lo->lo_backing_virt_file = virt_file;
39535 lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
39536 mapping_set_gfp_mask(file->f_mapping,
39537 lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
22319442 39538@@ -629,6 +648,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
e1106418
JR
39539 * dependency.
39540 */
5527c038
JR
39541 fput(old_file);
39542+ if (old_virt_file)
39543+ fput(old_virt_file);
e1106418 39544 if (partscan)
6d176551 39545 loop_reread_partitions(lo);
22319442
JP
39546
39547@@ -642,6 +663,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
6d176551
JR
39548 loop_global_unlock(lo, is_loop);
39549 out_putf:
39550 fput(file);
5527c038
JR
39551+ if (virt_file)
39552+ fput(virt_file);
22319442 39553 goto done;
5527c038 39554 }
e1106418 39555
82da57cb 39556@@ -1013,6 +1036,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
42a736d3 39557 const struct loop_config *config)
5527c038 39558 {
6d176551
JR
39559 struct file *file = fget(config->fd);
39560+ struct file *f, *virt_file = NULL;
39561 struct inode *inode;
5527c038 39562 struct address_space *mapping;
6d176551 39563 int error;
82da57cb 39564@@ -1031,6 +1055,13 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
c8b12053
JR
39565 /* This is safe, since we have a reference from open(). */
39566 __module_get(THIS_MODULE);
6d176551 39567
5527c038
JR
39568+ f = loop_real_file(file);
39569+ if (f) {
39570+ virt_file = file;
39571+ file = f;
39572+ get_file(file);
39573+ }
6d176551 39574+
fbc438ed
JR
39575 /*
39576 * If we don't hold exclusive handle for the device, upgrade to it
6d176551 39577 * here to avoid changing device under exclusive owner.
82da57cb 39578@@ -1091,6 +1122,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
f24b0ddd 39579 lo->use_dio = lo->lo_flags & LO_FLAGS_DIRECT_IO;
5527c038 39580 lo->lo_device = bdev;
5527c038
JR
39581 lo->lo_backing_file = file;
39582+ lo->lo_backing_virt_file = virt_file;
f24b0ddd
JR
39583 lo->old_gfp_mask = mapping_gfp_mask(mapping);
39584 mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39585
82da57cb 39586@@ -1146,6 +1178,8 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
42a736d3 39587 bd_abort_claiming(bdev, loop_configure);
e1106418 39588 out_putf:
5527c038
JR
39589 fput(file);
39590+ if (virt_file)
39591+ fput(virt_file);
5527c038
JR
39592 /* This is safe: open() is still holding a reference. */
39593 module_put(THIS_MODULE);
c8b12053 39594 return error;
82da57cb 39595@@ -1154,6 +1188,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
f4d37d76 39596 static void __loop_clr_fd(struct loop_device *lo, bool release)
5527c038 39597 {
f4d37d76 39598 struct file *filp;
5527c038
JR
39599+ struct file *virt_filp = lo->lo_backing_virt_file;
39600 gfp_t gfp = lo->old_gfp_mask;
5527c038 39601
22319442 39602 if (test_bit(QUEUE_FLAG_WC, &lo->lo_queue->queue_flags))
82da57cb 39603@@ -1170,6 +1205,7 @@ static void __loop_clr_fd(struct loop_device *lo, bool release)
5527c038 39604 spin_lock_irq(&lo->lo_lock);
f4d37d76 39605 filp = lo->lo_backing_file;
5527c038
JR
39606 lo->lo_backing_file = NULL;
39607+ lo->lo_backing_virt_file = NULL;
39608 spin_unlock_irq(&lo->lo_lock);
39609
cd233405 39610 lo->lo_device = NULL;
82da57cb 39611@@ -1232,6 +1268,8 @@ static void __loop_clr_fd(struct loop_device *lo, bool release)
f4d37d76 39612 * fput can take open_mutex which is usually taken before lo_mutex.
5527c038 39613 */
f4d37d76 39614 fput(filp);
5527c038
JR
39615+ if (virt_filp)
39616+ fput(virt_filp);
5527c038
JR
39617 }
39618
f4d37d76 39619 static int loop_clr_fd(struct loop_device *lo)
5527c038 39620diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
2255d0fe 39621index 66ddf86a3331..6cbef597d5b9 100644
5527c038
JR
39622--- a/fs/aufs/f_op.c
39623+++ b/fs/aufs/f_op.c
2255d0fe 39624@@ -309,7 +309,7 @@ static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
5527c038
JR
39625 if (IS_ERR(h_file))
39626 goto out;
39627
39628- if (au_test_loopback_kthread()) {
39629+ if (0 && au_test_loopback_kthread()) {
39630 au_warn_loopback(h_file->f_path.dentry->d_sb);
39631 if (file->f_mapping != h_file->f_mapping) {
39632 file->f_mapping = h_file->f_mapping;
39633diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
0dcfbb52 39634index 58043e31e5f3..e2bfae6f9d59 100644
5527c038
JR
39635--- a/fs/aufs/loop.c
39636+++ b/fs/aufs/loop.c
2121bcd9 39637@@ -133,3 +133,19 @@ void au_loopback_fin(void)
79b8bda9 39638 symbol_put(loop_backing_file);
9f237c51 39639 au_kfree_try_rcu(au_warn_loopback_array);
5527c038
JR
39640 }
39641+
39642+/* ---------------------------------------------------------------------- */
39643+
39644+/* support the loopback block device insude aufs */
39645+
39646+struct file *aufs_real_loop(struct file *file)
39647+{
39648+ struct file *f;
39649+
39650+ BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
39651+ fi_read_lock(file);
39652+ f = au_hf_top(file);
39653+ fi_read_unlock(file);
39654+ AuDebugOn(!f);
39655+ return f;
39656+}
39657diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
0dcfbb52 39658index 03d4908a6c03..34d356e181d5 100644
5527c038
JR
39659--- a/fs/aufs/loop.h
39660+++ b/fs/aufs/loop.h
eca801bf 39661@@ -26,6 +26,8 @@ void au_warn_loopback(struct super_block *h_sb);
5527c038
JR
39662
39663 int au_loopback_init(void);
39664 void au_loopback_fin(void);
39665+
39666+struct file *aufs_real_loop(struct file *file);
39667 #else
83b672a5 39668 AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
eca801bf
AM
39669
39670@@ -36,6 +38,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
5527c038
JR
39671
39672 AuStubInt0(au_loopback_init, void)
39673 AuStubVoid(au_loopback_fin, void)
39674+
39675+AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
39676 #endif /* BLK_DEV_LOOP */
39677
39678 #endif /* __KERNEL__ */
39679diff --git a/fs/aufs/super.c b/fs/aufs/super.c
0dcfbb52 39680index 81922d4faf54..c8a62c267d72 100644
5527c038
JR
39681--- a/fs/aufs/super.c
39682+++ b/fs/aufs/super.c
f4d37d76
AM
39683@@ -758,7 +758,10 @@ const struct super_operations aufs_sop = {
39684 .show_options = aufs_show_options,
5527c038
JR
39685 .statfs = aufs_statfs,
39686 .put_super = aufs_put_super,
f4d37d76
AM
39687- .sync_fs = aufs_sync_fs
39688+ .sync_fs = aufs_sync_fs,
5527c038
JR
39689+#ifdef CONFIG_AUFS_BDEV_LOOP
39690+ .real_loop = aufs_real_loop
39691+#endif
39692 };
39693
39694 /* ---------------------------------------------------------------------- */
39695diff --git a/include/linux/fs.h b/include/linux/fs.h
2255d0fe 39696index 433a4f08dab4..0b17b7e5ee11 100644
5527c038
JR
39697--- a/include/linux/fs.h
39698+++ b/include/linux/fs.h
2255d0fe 39699@@ -2257,6 +2257,10 @@ struct super_operations {
5527c038
JR
39700 struct shrink_control *);
39701 long (*free_cached_objects)(struct super_block *,
39702 struct shrink_control *);
43982f53 39703+#if IS_ENABLED(CONFIG_BLK_DEV_LOOP) || IS_ENABLED(CONFIG_BLK_DEV_LOOP_MODULE)
5527c038
JR
39704+ /* and aufs */
39705+ struct file *(*real_loop)(struct file *);
39706+#endif
39707 };
39708
39709 /*
This page took 8.060461 seconds and 4 git commands to generate.