]> git.pld-linux.org Git - packages/kernel.git/blame - kernel-aufs.patch
up to 6.6.5
[packages/kernel.git] / kernel-aufs.patch
CommitLineData
2121bcd9 1SPDX-License-Identifier: GPL-2.0
631230db 2aufs6.x-rcN kbuild patch
7f207e10 3
fbc438ed 4diff --git a/fs/Kconfig b/fs/Kconfig
631230db 5index aa7e03cc1941..bf780967b6c4 100644
fbc438ed
JR
6--- a/fs/Kconfig
7+++ b/fs/Kconfig
631230db 8@@ -331,6 +331,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
631230db 17index f9541f40be4e..3a0e13ee39e7 100644
7f207e10
AM
18--- a/fs/Makefile
19+++ b/fs/Makefile
171c7011 20@@ -129,3 +129,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
631230db 26aufs6.x-rcN base patch
7f207e10 27
c1595e42 28diff --git a/MAINTAINERS b/MAINTAINERS
631230db 29index bf0f54c24f81..39b1844bce57 100644
c1595e42
JR
30--- a/MAINTAINERS
31+++ b/MAINTAINERS
631230db 32@@ -3316,6 +3316,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+
0a7871ab
JR
49 AUXILIARY BUS DRIVER
50 M: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
51 R: Dave Ertman <david.m.ertman@intel.com>
392086de 52diff --git a/drivers/block/loop.c b/drivers/block/loop.c
631230db 53index 9f2d412fc560..1fefc6a8d049 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
631230db 82index 25ac74d30bff..6c930ceed526 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
631230db 95index e871009f6c88..d62e114c1b1a 100644
febd17d6
JR
96--- a/fs/fcntl.c
97+++ b/fs/fcntl.c
5db017eb 98@@ -34,7 +34,7 @@
febd17d6
JR
99
100 #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME)
101
631230db
AM
102-static int setfl(int fd, struct file * filp, unsigned int arg)
103+int setfl(int fd, struct file * filp, unsigned int arg)
febd17d6
JR
104 {
105 struct inode * inode = file_inode(filp);
106 int error = 0;
631230db 107@@ -64,6 +64,8 @@ static int setfl(int fd, struct file * filp, unsigned int 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
171c7011 117index e157efc54023..6c57487f126b 100644
8b6a4947
AM
118--- a/fs/namespace.c
119+++ b/fs/namespace.c
171c7011 120@@ -872,6 +872,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
631230db 134index d983d375ff11..7216ef993b5f 100644
7f207e10
AM
135--- a/fs/splice.c
136+++ b/fs/splice.c
631230db 137@@ -925,8 +925,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");
5527c038 148diff --git a/include/linux/fs.h b/include/linux/fs.h
631230db 149index 4aeb3fa11927..dd5871d0c429 100644
5527c038
JR
150--- a/include/linux/fs.h
151+++ b/include/linux/fs.h
631230db 152@@ -1099,6 +1099,7 @@ extern void fasync_free(struct fasync_struct *);
febd17d6
JR
153 /* can be called from interrupts */
154 extern void kill_fasync(struct fasync_struct **, int, int);
155
631230db 156+extern int setfl(int fd, struct file *filp, unsigned int arg);
febd17d6 157 extern void __f_setown(struct file *filp, struct pid *, enum pid_type, int force);
631230db 158 extern int f_setown(struct file *filp, int who, int force);
febd17d6 159 extern void f_delown(struct file *filp);
631230db 160@@ -1901,6 +1902,7 @@ struct file_operations {
0a7871ab 161 int (*lock) (struct file *, int, struct file_lock *);
febd17d6
JR
162 unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
163 int (*check_flags)(int);
164+ int (*setfl)(struct file *, unsigned long);
165 int (*flock) (struct file *, int, struct file_lock *);
166 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
167 ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
8b6a4947 168diff --git a/include/linux/lockdep.h b/include/linux/lockdep.h
631230db 169index dc2844b071c2..069ffb776c2c 100644
8b6a4947
AM
170--- a/include/linux/lockdep.h
171+++ b/include/linux/lockdep.h
594d0238 172@@ -249,6 +249,8 @@ static inline int lockdep_match_key(struct lockdep_map *lock,
8b6a4947
AM
173 return lock->key == key;
174 }
175
176+struct lock_class *lockdep_hlock_class(struct held_lock *hlock);
177+
178 /*
179 * Acquire a lock.
180 *
8b6a4947 181diff --git a/include/linux/mnt_namespace.h b/include/linux/mnt_namespace.h
26eb093c 182index 8f882f5881e8..6b9808f09843 100644
8b6a4947
AM
183--- a/include/linux/mnt_namespace.h
184+++ b/include/linux/mnt_namespace.h
42a736d3 185@@ -7,12 +7,15 @@ struct mnt_namespace;
8b6a4947
AM
186 struct fs_struct;
187 struct user_namespace;
f24b0ddd 188 struct ns_common;
8b6a4947
AM
189+struct vfsmount;
190
191 extern struct mnt_namespace *copy_mnt_ns(unsigned long, struct mnt_namespace *,
192 struct user_namespace *, struct fs_struct *);
193 extern void put_mnt_ns(struct mnt_namespace *ns);
f24b0ddd 194 extern struct ns_common *from_mnt_ns(struct mnt_namespace *);
8b6a4947
AM
195
196+extern int is_current_mnt_ns(struct vfsmount *mnt);
197+
198 extern const struct file_operations proc_mounts_operations;
199 extern const struct file_operations proc_mountinfo_operations;
200 extern const struct file_operations proc_mountstats_operations;
1e00d052 201diff --git a/include/linux/splice.h b/include/linux/splice.h
171c7011 202index 6c461573434d..7416cf375ad1 100644
1e00d052
AM
203--- a/include/linux/splice.h
204+++ b/include/linux/splice.h
171c7011 205@@ -99,4 +99,10 @@ extern void splice_shrink_spd(struct splice_pipe_desc *);
4b3da204
AM
206
207 extern const struct pipe_buf_operations page_cache_pipe_buf_ops;
106341ce 208 extern const struct pipe_buf_operations default_pipe_buf_ops;
1e00d052
AM
209+
210+extern long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
211+ loff_t *ppos, size_t len, unsigned int flags);
171c7011
AM
212+extern long do_splice_to(struct file *in, loff_t *ppos,
213+ struct pipe_inode_info *pipe, size_t len,
214+ unsigned int flags);
1e00d052 215 #endif
8b6a4947 216diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
631230db 217index e85b5ad3e206..db4297f60bd3 100644
8b6a4947
AM
218--- a/kernel/locking/lockdep.c
219+++ b/kernel/locking/lockdep.c
5db017eb 220@@ -218,7 +218,7 @@ unsigned long max_lock_class_idx;
9f237c51 221 struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
46cb2ab5 222 DECLARE_BITMAP(lock_classes_in_use, MAX_LOCKDEP_KEYS);
8b6a4947
AM
223
224-static inline struct lock_class *hlock_class(struct held_lock *hlock)
225+inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
226 {
eca34b5c
AM
227 unsigned int class_idx = hlock->class_idx;
228
5db017eb 229@@ -239,6 +239,7 @@ static inline struct lock_class *hlock_class(struct held_lock *hlock)
eca34b5c
AM
230 */
231 return lock_classes + class_idx;
8b6a4947
AM
232 }
233+#define hlock_class(hlock) lockdep_hlock_class(hlock)
234
235 #ifdef CONFIG_LOCK_STAT
236 static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
2121bcd9 237SPDX-License-Identifier: GPL-2.0
631230db 238aufs6.x-rcN mmap patch
fb47a38f 239
c1595e42 240diff --git a/fs/proc/base.c b/fs/proc/base.c
631230db 241index ffd54617c354..29ec720c8038 100644
c1595e42
JR
242--- a/fs/proc/base.c
243+++ b/fs/proc/base.c
82da57cb 244@@ -2218,7 +2218,7 @@ static int map_files_get_link(struct dentry *dentry, struct path *path)
eca34b5c 245 rc = -ENOENT;
c1595e42
JR
246 vma = find_exact_vma(mm, vm_start, vm_end);
247 if (vma && vma->vm_file) {
248- *path = vma->vm_file->f_path;
249+ *path = vma_pr_or_file(vma)->f_path;
250 path_get(path);
251 rc = 0;
252 }
fb47a38f 253diff --git a/fs/proc/nommu.c b/fs/proc/nommu.c
82da57cb 254index 4d3493579458..42edd9a42c78 100644
fb47a38f
JR
255--- a/fs/proc/nommu.c
256+++ b/fs/proc/nommu.c
82da57cb 257@@ -39,7 +39,10 @@ static int nommu_region_show(struct seq_file *m, struct vm_region *region)
fb47a38f
JR
258 file = region->vm_file;
259
260 if (file) {
261- struct inode *inode = file_inode(region->vm_file);
262+ struct inode *inode;
076b876e 263+
fb47a38f
JR
264+ file = vmr_pr_or_file(region);
265+ inode = file_inode(file);
266 dev = inode->i_sb->s_dev;
267 ino = inode->i_ino;
268 }
269diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
631230db 270index 3dd5be96691b..40d9d970b308 100644
fb47a38f
JR
271--- a/fs/proc/task_mmu.c
272+++ b/fs/proc/task_mmu.c
631230db 273@@ -271,7 +271,10 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
fb47a38f
JR
274 const char *name = NULL;
275
276 if (file) {
277- struct inode *inode = file_inode(vma->vm_file);
278+ struct inode *inode;
076b876e 279+
fb47a38f
JR
280+ file = vma_pr_or_file(vma);
281+ inode = file_inode(file);
282 dev = inode->i_sb->s_dev;
283 ino = inode->i_ino;
284 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
631230db 285@@ -1943,7 +1946,7 @@ static int show_numa_map(struct seq_file *m, void *v)
076b876e
AM
286 struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
287 struct vm_area_struct *vma = v;
288 struct numa_maps *md = &numa_priv->md;
289- struct file *file = vma->vm_file;
290+ struct file *file = vma_pr_or_file(vma);
076b876e 291 struct mm_struct *mm = vma->vm_mm;
43982f53
AM
292 struct mempolicy *pol;
293 char buffer[64];
fb47a38f 294diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c
631230db 295index a8ac0dd8041e..9fca456e2259 100644
fb47a38f
JR
296--- a/fs/proc/task_nommu.c
297+++ b/fs/proc/task_nommu.c
631230db 298@@ -137,7 +137,10 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma)
fb47a38f
JR
299 file = vma->vm_file;
300
301 if (file) {
302- struct inode *inode = file_inode(vma->vm_file);
303+ struct inode *inode;
076b876e 304+
b912730e 305+ file = vma_pr_or_file(vma);
fb47a38f
JR
306+ inode = file_inode(file);
307 dev = inode->i_sb->s_dev;
308 ino = inode->i_ino;
309 pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
310diff --git a/include/linux/mm.h b/include/linux/mm.h
631230db 311index bf5d0b1b16f4..94b956eff452 100644
fb47a38f
JR
312--- a/include/linux/mm.h
313+++ b/include/linux/mm.h
631230db 314@@ -2409,6 +2409,43 @@ static inline void unmap_shared_mapping_range(struct address_space *mapping,
171c7011
AM
315 static inline struct vm_area_struct *vma_lookup(struct mm_struct *mm,
316 unsigned long addr);
fb47a38f 317
41e6ec2c 318+#if 1 /* IS_ENABLED(CONFIG_AUFS_FS) */
076b876e
AM
319+extern void vma_do_file_update_time(struct vm_area_struct *, const char[], int);
320+extern struct file *vma_do_pr_or_file(struct vm_area_struct *, const char[],
f2c43d5f 321+ int);
076b876e
AM
322+extern void vma_do_get_file(struct vm_area_struct *, const char[], int);
323+extern void vma_do_fput(struct vm_area_struct *, const char[], int);
fb47a38f 324+
f2c43d5f
AM
325+#define vma_file_update_time(vma) vma_do_file_update_time(vma, __func__, \
326+ __LINE__)
327+#define vma_pr_or_file(vma) vma_do_pr_or_file(vma, __func__, \
328+ __LINE__)
329+#define vma_get_file(vma) vma_do_get_file(vma, __func__, __LINE__)
330+#define vma_fput(vma) vma_do_fput(vma, __func__, __LINE__)
b912730e
AM
331+
332+#ifndef CONFIG_MMU
076b876e
AM
333+extern struct file *vmr_do_pr_or_file(struct vm_region *, const char[], int);
334+extern void vmr_do_fput(struct vm_region *, const char[], int);
335+
f2c43d5f
AM
336+#define vmr_pr_or_file(region) vmr_do_pr_or_file(region, __func__, \
337+ __LINE__)
338+#define vmr_fput(region) vmr_do_fput(region, __func__, __LINE__)
b912730e 339+#endif /* !CONFIG_MMU */
26eb093c
AM
340+
341+#else
342+
343+#define vma_file_update_time(vma) file_update_time((vma)->vm_file)
344+#define vma_pr_or_file(vma) (vma)->vm_file
345+#define vma_get_file(vma) get_file((vma)->vm_file)
346+#define vma_fput(vma) fput((vma)->vm_file)
347+
348+#ifndef CONFIG_MMU
349+#define vmr_pr_or_file(region) (region)->vm_file
350+#define vmr_fput(region) fput((region)->vm_file)
351+#endif /* !CONFIG_MMU */
352+
353+#endif /* CONFIG_AUFS_FS */
fb47a38f 354+
171c7011
AM
355 extern int access_process_vm(struct task_struct *tsk, unsigned long addr,
356 void *buf, int len, unsigned int gup_flags);
357 extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
fb47a38f 358diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
631230db 359index 36c5b43999e6..ce93e97f76a6 100644
fb47a38f
JR
360--- a/include/linux/mm_types.h
361+++ b/include/linux/mm_types.h
631230db 362@@ -524,6 +524,9 @@ struct vm_region {
fb47a38f
JR
363 unsigned long vm_top; /* region allocated to here */
364 unsigned long vm_pgoff; /* the offset in vm_file corresponding to vm_start */
365 struct file *vm_file; /* the backing file or NULL */
41e6ec2c 366+#if 1 /* IS_ENABLED(CONFIG_AUFS_FS) */
fb47a38f 367+ struct file *vm_prfile; /* the virtual backing file or NULL */
26eb093c 368+#endif
fb47a38f
JR
369
370 int vm_usage; /* region usage count (access under nommu_region_sem) */
371 bool vm_icache_flushed : 1; /* true if the icache has been flushed for
631230db 372@@ -637,6 +640,9 @@ struct vm_area_struct {
fb47a38f 373 unsigned long vm_pgoff; /* Offset (within vm_file) in PAGE_SIZE
5afbbe0d 374 units */
fb47a38f 375 struct file * vm_file; /* File we map to (can be NULL). */
41e6ec2c 376+#if 1 /* IS_ENABLED(CONFIG_AUFS_FS) */
fb47a38f 377+ struct file *vm_prfile; /* shadow of vm_file */
26eb093c 378+#endif
fb47a38f
JR
379 void * vm_private_data; /* was vm_pte (shared mem) */
380
aec24f17 381 #ifdef CONFIG_ANON_VMA_NAME
fb47a38f 382diff --git a/kernel/fork.c b/kernel/fork.c
631230db 383index 3b6d20dfb9a8..ccad0325cfa9 100644
fb47a38f
JR
384--- a/kernel/fork.c
385+++ b/kernel/fork.c
171c7011 386@@ -731,7 +731,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
758e9dad 387 if (file) {
fb47a38f
JR
388 struct address_space *mapping = file->f_mapping;
389
390- get_file(file);
391+ vma_get_file(tmp);
2000de60 392 i_mmap_lock_write(mapping);
758e9dad
AM
393 if (tmp->vm_flags & VM_SHARED)
394 mapping_allow_writable(mapping);
076b876e 395diff --git a/mm/Makefile b/mm/Makefile
631230db 396index ec65984e2ade..d59461647ccd 100644
076b876e
AM
397--- a/mm/Makefile
398+++ b/mm/Makefile
631230db 399@@ -138,3 +138,4 @@ obj-$(CONFIG_IO_MAPPING) += io-mapping.o
26eb093c
AM
400 obj-$(CONFIG_HAVE_BOOTMEM_INFO_NODE) += bootmem_info.o
401 obj-$(CONFIG_GENERIC_IOREMAP) += ioremap.o
82da57cb 402 obj-$(CONFIG_SHRINKER_DEBUG) += shrinker_debug.o
41e6ec2c 403+obj-y += prfile.o
fb47a38f 404diff --git a/mm/filemap.c b/mm/filemap.c
631230db 405index 582f5317ff71..c024ebb30073 100644
fb47a38f
JR
406--- a/mm/filemap.c
407+++ b/mm/filemap.c
631230db 408@@ -3599,7 +3599,7 @@ vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
cd7a4cd9 409 vm_fault_t ret = VM_FAULT_LOCKED;
fb47a38f 410
42a736d3 411 sb_start_pagefault(mapping->host->i_sb);
521ced18
JR
412- file_update_time(vmf->vma->vm_file);
413+ vma_file_update_time(vmf->vma);
f4d37d76
AM
414 folio_lock(folio);
415 if (folio->mapping != mapping) {
416 folio_unlock(folio);
fb47a38f 417diff --git a/mm/mmap.c b/mm/mmap.c
631230db 418index b56a7f0c9f85..5eb114409e07 100644
fb47a38f
JR
419--- a/mm/mmap.c
420+++ b/mm/mmap.c
594d0238 421@@ -140,7 +140,7 @@ static void remove_vma(struct vm_area_struct *vma, bool unreachable)
fb47a38f
JR
422 if (vma->vm_ops && vma->vm_ops->close)
423 vma->vm_ops->close(vma);
424 if (vma->vm_file)
425- fput(vma->vm_file);
426+ vma_fput(vma);
427 mpol_put(vma_policy(vma));
594d0238
AM
428 if (unreachable)
429 __vm_area_free(vma);
631230db 430@@ -554,7 +554,7 @@ static inline void vma_complete(struct vma_prepare *vp,
5d4d15ba
AM
431 if (vp->file) {
432 uprobe_munmap(vp->remove, vp->remove->vm_start,
433 vp->remove->vm_end);
434- fput(vp->file);
435+ vma_fput(vp->vma);
436 }
437 if (vp->remove->anon_vma)
438 anon_vma_merge(vp->vma, vp->remove);
631230db 439@@ -2364,7 +2364,7 @@ int __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,
fb47a38f
JR
440 goto out_free_mpol;
441
442 if (new->vm_file)
443- get_file(new->vm_file);
444+ vma_get_file(new);
445
446 if (new->vm_ops && new->vm_ops->open)
447 new->vm_ops->open(new);
631230db 448@@ -2781,7 +2781,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
5d4d15ba
AM
449 * and cause general protection fault
450 * ultimately.
451 */
452- fput(vma->vm_file);
453+ vma_fput(vma);
454 vm_area_free(vma);
455 vma = merge;
456 /* Update vm_flags to pick up the change. */
631230db 457@@ -2876,7 +2876,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
5db017eb
AM
458
459 if (file || vma->vm_file) {
2255d0fe 460 unmap_and_free_vma:
5db017eb
AM
461- fput(vma->vm_file);
462+ vma_fput(vma);
463 vma->vm_file = NULL;
2255d0fe 464
631230db
AM
465 vma_iter_set(&vmi, vma->vm_end);
466@@ -2938,6 +2938,9 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
7e9cd9fe
AM
467 unsigned long populate = 0;
468 unsigned long ret = -EINVAL;
26eb093c 469 struct file *file;
41e6ec2c 470+#if 1 /* IS_ENABLED(CONFIG_AUFS_FS) */
26eb093c
AM
471+ struct file *prfile;
472+#endif
7e9cd9fe 473
82da57cb 474 pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/mm/remap_file_pages.rst.\n",
5afbbe0d 475 current->comm, current->pid);
631230db 476@@ -2996,10 +2999,34 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
46016270
AM
477 if (vma->vm_flags & VM_LOCKED)
478 flags |= MAP_LOCKED;
7e9cd9fe 479
41e6ec2c 480+#if 1 /* IS_ENABLED(CONFIG_AUFS_FS) */
7e9cd9fe 481+ vma_get_file(vma);
5afbbe0d
AM
482+ file = vma->vm_file;
483+ prfile = vma->vm_prfile;
26eb093c 484+ ret = do_mmap(vma->vm_file, start, size,
631230db 485+ prot, flags, /*vm_flags*/0, pgoff, &populate, NULL);
5afbbe0d
AM
486+ if (!IS_ERR_VALUE(ret) && file && prfile) {
487+ struct vm_area_struct *new_vma;
488+
489+ new_vma = find_vma(mm, ret);
490+ if (!new_vma->vm_prfile)
491+ new_vma->vm_prfile = prfile;
5d4d15ba 492+ if (prfile)
5afbbe0d
AM
493+ get_file(prfile);
494+ }
495+ /*
496+ * two fput()s instead of vma_fput(vma),
497+ * coz vma may not be available anymore.
498+ */
26eb093c 499+ fput(file);
5afbbe0d
AM
500+ if (prfile)
501+ fput(prfile);
26eb093c
AM
502+#else
503 file = get_file(vma->vm_file);
504 ret = do_mmap(vma->vm_file, start, size,
631230db 505 prot, flags, 0, pgoff, &populate, NULL);
26eb093c
AM
506 fput(file);
507+#endif /* CONFIG_AUFS_FS */
7e9cd9fe 508 out:
42a736d3 509 mmap_write_unlock(mm);
7e9cd9fe 510 if (populate)
631230db 511@@ -3350,7 +3377,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
79b8bda9
AM
512 if (anon_vma_clone(new_vma, vma))
513 goto out_free_mempol;
514 if (new_vma->vm_file)
515- get_file(new_vma->vm_file);
516+ vma_get_file(new_vma);
517 if (new_vma->vm_ops && new_vma->vm_ops->open)
518 new_vma->vm_ops->open(new_vma);
631230db
AM
519 if (vma_link(mm, new_vma))
520@@ -3364,7 +3391,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
5db017eb
AM
521 new_vma->vm_ops->close(new_vma);
522
523 if (new_vma->vm_file)
524- fput(new_vma->vm_file);
525+ vma_fput(new_vma);
526
527 unlink_anon_vmas(new_vma);
528 out_free_mempol:
fb47a38f 529diff --git a/mm/nommu.c b/mm/nommu.c
631230db 530index 7f9e9e5a0e12..69663f2bd4c4 100644
fb47a38f
JR
531--- a/mm/nommu.c
532+++ b/mm/nommu.c
2255d0fe 533@@ -523,7 +523,7 @@ static void __put_nommu_region(struct vm_region *region)
fb47a38f
JR
534 up_write(&nommu_region_sem);
535
536 if (region->vm_file)
537- fput(region->vm_file);
538+ vmr_fput(region);
539
540 /* IO memory and memory shared directly out of the pagecache
541 * from ramfs/tmpfs mustn't be released here */
631230db 542@@ -603,7 +603,7 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
fb47a38f
JR
543 if (vma->vm_ops && vma->vm_ops->close)
544 vma->vm_ops->close(vma);
545 if (vma->vm_file)
546- fput(vma->vm_file);
547+ vma_fput(vma);
548 put_nommu_region(vma->vm_region);
cd7a4cd9 549 vm_area_free(vma);
fb47a38f 550 }
631230db 551@@ -1135,7 +1135,7 @@ unsigned long do_mmap(struct file *file,
fb47a38f
JR
552 goto error_just_free;
553 }
554 }
555- fput(region->vm_file);
556+ vmr_fput(region);
557 kmem_cache_free(vm_region_jar, region);
558 region = pregion;
559 result = start;
631230db 560@@ -1221,10 +1221,10 @@ unsigned long do_mmap(struct file *file,
fb47a38f 561 error:
5db017eb 562 vma_iter_free(&vmi);
fb47a38f
JR
563 if (region->vm_file)
564- fput(region->vm_file);
41e6ec2c 565+ vmr_fput(region);
fb47a38f
JR
566 kmem_cache_free(vm_region_jar, region);
567 if (vma->vm_file)
41e6ec2c
JR
568- fput(vma->vm_file);
569+ vma_fput(vma);
eca801bf
AM
570 vm_area_free(vma);
571 return ret;
572
076b876e
AM
573diff --git a/mm/prfile.c b/mm/prfile.c
574new file mode 100644
0dcfbb52 575index 000000000000..8f820a235364
076b876e
AM
576--- /dev/null
577+++ b/mm/prfile.c
2121bcd9 578@@ -0,0 +1,86 @@
cd7a4cd9 579+// SPDX-License-Identifier: GPL-2.0
076b876e 580+/*
1c60b727
AM
581+ * Mainly for aufs which mmap(2) different file and wants to print different
582+ * path in /proc/PID/maps.
076b876e
AM
583+ * Call these functions via macros defined in linux/mm.h.
584+ *
585+ * See Documentation/filesystems/aufs/design/06mmap.txt
586+ *
0dcfbb52 587+ * Copyright (c) 2014-2022 Junjro R. Okajima
076b876e
AM
588+ * Copyright (c) 2014 Ian Campbell
589+ */
590+
591+#include <linux/mm.h>
592+#include <linux/file.h>
593+#include <linux/fs.h>
594+
595+/* #define PRFILE_TRACE */
596+static inline void prfile_trace(struct file *f, struct file *pr,
597+ const char func[], int line, const char func2[])
598+{
599+#ifdef PRFILE_TRACE
600+ if (pr)
1c60b727 601+ pr_info("%s:%d: %s, %pD2\n", func, line, func2, f);
076b876e
AM
602+#endif
603+}
604+
076b876e
AM
605+void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
606+ int line)
607+{
608+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
609+
610+ prfile_trace(f, pr, func, line, __func__);
611+ file_update_time(f);
612+ if (f && pr)
613+ file_update_time(pr);
614+}
615+
616+struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
617+ int line)
618+{
619+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
620+
621+ prfile_trace(f, pr, func, line, __func__);
622+ return (f && pr) ? pr : f;
623+}
624+
625+void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
626+{
627+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
628+
629+ prfile_trace(f, pr, func, line, __func__);
630+ get_file(f);
631+ if (f && pr)
632+ get_file(pr);
633+}
634+
635+void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
636+{
637+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
638+
639+ prfile_trace(f, pr, func, line, __func__);
640+ fput(f);
641+ if (f && pr)
642+ fput(pr);
643+}
b912730e
AM
644+
645+#ifndef CONFIG_MMU
076b876e
AM
646+struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
647+ int line)
648+{
649+ struct file *f = region->vm_file, *pr = region->vm_prfile;
650+
651+ prfile_trace(f, pr, func, line, __func__);
652+ return (f && pr) ? pr : f;
653+}
654+
655+void vmr_do_fput(struct vm_region *region, const char func[], int line)
656+{
657+ struct file *f = region->vm_file, *pr = region->vm_prfile;
658+
659+ prfile_trace(f, pr, func, line, __func__);
660+ fput(f);
661+ if (f && pr)
662+ fput(pr);
663+}
b912730e 664+#endif /* !CONFIG_MMU */
2121bcd9 665SPDX-License-Identifier: GPL-2.0
631230db 666aufs6.x-rcN standalone patch
7f207e10 667
c1595e42 668diff --git a/fs/dcache.c b/fs/dcache.c
631230db 669index 6c930ceed526..576ad162cdec 100644
c1595e42
JR
670--- a/fs/dcache.c
671+++ b/fs/dcache.c
f4d37d76 672@@ -1450,6 +1450,7 @@ void d_walk(struct dentry *parent, void *data,
c1595e42
JR
673 seq = 1;
674 goto again;
675 }
febd17d6 676+EXPORT_SYMBOL_GPL(d_walk);
c1595e42 677
a2654f78
AM
678 struct check_mount {
679 struct vfsmount *mnt;
631230db 680@@ -3051,6 +3052,7 @@ void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
f2c43d5f
AM
681
682 write_sequnlock(&rename_lock);
683 }
684+EXPORT_SYMBOL_GPL(d_exchange);
685
686 /**
687 * d_ancestor - search for an ancestor
79b8bda9 688diff --git a/fs/exec.c b/fs/exec.c
631230db 689index 6518e33ea813..b67efac6a1ad 100644
79b8bda9
AM
690--- a/fs/exec.c
691+++ b/fs/exec.c
594d0238 692@@ -112,6 +112,7 @@ bool path_noexec(const struct path *path)
79b8bda9
AM
693 return (path->mnt->mnt_flags & MNT_NOEXEC) ||
694 (path->mnt->mnt_sb->s_iflags & SB_I_NOEXEC);
695 }
febd17d6 696+EXPORT_SYMBOL_GPL(path_noexec);
79b8bda9
AM
697
698 #ifdef CONFIG_USELIB
699 /*
febd17d6 700diff --git a/fs/fcntl.c b/fs/fcntl.c
631230db 701index d62e114c1b1a..ceef001775bd 100644
febd17d6
JR
702--- a/fs/fcntl.c
703+++ b/fs/fcntl.c
631230db 704@@ -87,6 +87,7 @@ int setfl(int fd, struct file * filp, unsigned int arg)
febd17d6
JR
705 out:
706 return error;
707 }
708+EXPORT_SYMBOL_GPL(setfl);
709
710 static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
711 int force)
b912730e 712diff --git a/fs/file_table.c b/fs/file_table.c
631230db 713index ee21b3da9d08..c45ac36795dd 100644
b912730e
AM
714--- a/fs/file_table.c
715+++ b/fs/file_table.c
171c7011 716@@ -225,6 +225,7 @@ struct file *alloc_empty_file(int flags, const struct cred *cred)
b912730e
AM
717 }
718 return ERR_PTR(-ENFILE);
719 }
acd2b654 720+EXPORT_SYMBOL_GPL(alloc_empty_file);
b912730e 721
acd2b654
AM
722 /*
723 * Variant of alloc_empty_file() that doesn't check and modify nr_files.
7f207e10 724diff --git a/fs/namespace.c b/fs/namespace.c
171c7011 725index 6c57487f126b..16be9ac1c734 100644
7f207e10
AM
726--- a/fs/namespace.c
727+++ b/fs/namespace.c
171c7011 728@@ -466,6 +466,7 @@ void __mnt_drop_write(struct vfsmount *mnt)
c06a8ce3
AM
729 mnt_dec_writers(real_mount(mnt));
730 preempt_enable();
731 }
732+EXPORT_SYMBOL_GPL(__mnt_drop_write);
733
734 /**
735 * mnt_drop_write - give up write access to a mount
171c7011 736@@ -877,6 +878,7 @@ int is_current_mnt_ns(struct vfsmount *mnt)
8b6a4947
AM
737 {
738 return check_mnt(real_mount(mnt));
739 }
740+EXPORT_SYMBOL_GPL(is_current_mnt_ns);
741
742 /*
743 * vfsmount lock must be held for write
171c7011 744@@ -2152,6 +2154,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
7f207e10
AM
745 }
746 return 0;
747 }
febd17d6 748+EXPORT_SYMBOL_GPL(iterate_mounts);
7f207e10 749
fbc438ed 750 static void lock_mnt_tree(struct mount *mnt)
7f207e10 751 {
43982f53 752diff --git a/fs/notify/group.c b/fs/notify/group.c
22319442 753index 1de6631a3925..3008eb37a18d 100644
43982f53
AM
754--- a/fs/notify/group.c
755+++ b/fs/notify/group.c
42a736d3 756@@ -100,6 +100,7 @@ void fsnotify_get_group(struct fsnotify_group *group)
1716fcea 757 {
2121bcd9 758 refcount_inc(&group->refcnt);
1716fcea 759 }
febd17d6 760+EXPORT_SYMBOL_GPL(fsnotify_get_group);
1716fcea
AM
761
762 /*
763 * Drop a reference to a group. Free it if it's through.
7f207e10 764diff --git a/fs/open.c b/fs/open.c
631230db 765index 98f6601fbac6..8624e4ffa15b 100644
7f207e10
AM
766--- a/fs/open.c
767+++ b/fs/open.c
5db017eb 768@@ -67,6 +67,7 @@ int do_truncate(struct mnt_idmap *idmap, struct dentry *dentry,
febd17d6 769 inode_unlock(dentry->d_inode);
7f207e10
AM
770 return ret;
771 }
febd17d6 772+EXPORT_SYMBOL_GPL(do_truncate);
7f207e10 773
5afbbe0d 774 long vfs_truncate(const struct path *path, loff_t length)
7f207e10 775 {
5527c038 776diff --git a/fs/read_write.c b/fs/read_write.c
631230db 777index 4771701c896b..c79270aba792 100644
5527c038
JR
778--- a/fs/read_write.c
779+++ b/fs/read_write.c
82da57cb 780@@ -477,6 +477,7 @@ ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
42a736d3 781 inc_syscr(current);
8b6a4947
AM
782 return ret;
783 }
784+EXPORT_SYMBOL_GPL(vfs_read);
785
786 static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
787 {
0dcfbb52 788@@ -592,6 +593,7 @@ ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_
42a736d3 789 file_end_write(file);
8b6a4947
AM
790 return ret;
791 }
792+EXPORT_SYMBOL_GPL(vfs_write);
793
fbc438ed
JR
794 /* file_ppos returns &file->f_pos or NULL if file is stream */
795 static inline loff_t *file_ppos(struct file *file)
7f207e10 796diff --git a/fs/splice.c b/fs/splice.c
631230db 797index 7216ef993b5f..7ce1f1bc4268 100644
7f207e10
AM
798--- a/fs/splice.c
799+++ b/fs/splice.c
631230db 800@@ -932,6 +932,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
42a736d3
AM
801 return warn_unsupported(out, "write");
802 return out->f_op->splice_write(pipe, out, ppos, len, flags);
7f207e10 803 }
febd17d6 804+EXPORT_SYMBOL_GPL(do_splice_from);
7f207e10
AM
805
806 /*
0a7871ab 807 * Indicate to the caller that there was a premature EOF when reading from the
c1595e42 808diff --git a/fs/xattr.c b/fs/xattr.c
631230db 809index efd4736bc94b..ce1a2c39ab23 100644
c1595e42
JR
810--- a/fs/xattr.c
811+++ b/fs/xattr.c
594d0238 812@@ -406,6 +406,7 @@ vfs_getxattr_alloc(struct mnt_idmap *idmap, struct dentry *dentry,
c1595e42
JR
813 *xattr_value = value;
814 return error;
815 }
febd17d6 816+EXPORT_SYMBOL_GPL(vfs_getxattr_alloc);
c1595e42 817
febd17d6 818 ssize_t
f2c43d5f 819 __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
8b6a4947 820diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
631230db 821index db4297f60bd3..9aca18312afb 100644
8b6a4947
AM
822--- a/kernel/locking/lockdep.c
823+++ b/kernel/locking/lockdep.c
5db017eb 824@@ -239,6 +239,7 @@ inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
eca34b5c
AM
825 */
826 return lock_classes + class_idx;
8b6a4947
AM
827 }
828+EXPORT_SYMBOL_GPL(lockdep_hlock_class);
829 #define hlock_class(hlock) lockdep_hlock_class(hlock)
830
831 #ifdef CONFIG_LOCK_STAT
8cdd5066 832diff --git a/kernel/task_work.c b/kernel/task_work.c
2255d0fe 833index 065e1ef8fc8d..c623c6f0c645 100644
8cdd5066
JR
834--- a/kernel/task_work.c
835+++ b/kernel/task_work.c
2255d0fe 836@@ -182,3 +182,4 @@ void task_work_run(void)
8cdd5066
JR
837 } while (work);
838 }
839 }
febd17d6 840+EXPORT_SYMBOL_GPL(task_work_run);
7f207e10 841diff --git a/security/security.c b/security/security.c
631230db 842index 23b129d482a7..fca4c5707a1c 100644
7f207e10
AM
843--- a/security/security.c
844+++ b/security/security.c
631230db 845@@ -1750,6 +1750,7 @@ int security_path_rmdir(const struct path *dir, struct dentry *dentry)
7f207e10 846 return 0;
c2c0f25c 847 return call_int_hook(path_rmdir, 0, dir, dentry);
7f207e10 848 }
febd17d6 849+EXPORT_SYMBOL_GPL(security_path_rmdir);
7f207e10 850
594d0238
AM
851 /**
852 * security_path_unlink() - Check if removing a hard link is allowed
631230db 853@@ -1785,6 +1786,7 @@ int security_path_symlink(const struct path *dir, struct dentry *dentry,
7f207e10 854 return 0;
c2c0f25c 855 return call_int_hook(path_symlink, 0, dir, dentry, old_name);
7f207e10 856 }
febd17d6 857+EXPORT_SYMBOL_GPL(security_path_symlink);
7f207e10 858
594d0238
AM
859 /**
860 * security_path_link - Check if creating a hard link is allowed
631230db 861@@ -1803,6 +1805,7 @@ int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
7f207e10 862 return 0;
c2c0f25c 863 return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
7f207e10 864 }
febd17d6 865+EXPORT_SYMBOL_GPL(security_path_link);
7f207e10 866
594d0238
AM
867 /**
868 * security_path_rename() - Check if renaming a file is allowed
631230db 869@@ -1864,6 +1867,7 @@ int security_path_chmod(const struct path *path, umode_t mode)
7f207e10 870 return 0;
c2c0f25c 871 return call_int_hook(path_chmod, 0, path, mode);
7f207e10 872 }
febd17d6 873+EXPORT_SYMBOL_GPL(security_path_chmod);
7f207e10 874
594d0238
AM
875 /**
876 * security_path_chown() - Check if changing the file's owner/group is allowed
631230db 877@@ -1881,6 +1885,7 @@ int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
7f207e10 878 return 0;
c2c0f25c 879 return call_int_hook(path_chown, 0, path, uid, gid);
7f207e10 880 }
febd17d6 881+EXPORT_SYMBOL_GPL(security_path_chown);
7f207e10 882
594d0238
AM
883 /**
884 * security_path_chroot() - Check if changing the root directory is allowed
631230db 885@@ -2110,6 +2115,7 @@ int security_inode_permission(struct inode *inode, int mask)
7f207e10 886 return 0;
c2c0f25c 887 return call_int_hook(inode_permission, 0, inode, mask);
7f207e10 888 }
febd17d6 889+EXPORT_SYMBOL_GPL(security_inode_permission);
7f207e10 890
594d0238
AM
891 /**
892 * security_inode_setattr() - Check if setting file attributes is allowed
631230db 893@@ -2588,6 +2594,7 @@ int security_file_permission(struct file *file, int mask)
7f207e10
AM
894
895 return fsnotify_perm(file, mask);
896 }
febd17d6 897+EXPORT_SYMBOL_GPL(security_file_permission);
7f207e10 898
594d0238
AM
899 /**
900 * security_file_alloc() - Allocate and init a file's LSM blob
631230db 901@@ -2854,6 +2861,7 @@ int security_file_truncate(struct file *file)
aec24f17
AM
902 {
903 return call_int_hook(file_truncate, 0, file);
904 }
905+EXPORT_SYMBOL_GPL(security_file_truncate);
906
594d0238
AM
907 /**
908 * security_task_alloc() - Allocate a task's LSM blob
7f207e10 909diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
eca34b5c 910--- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs 1970-01-01 01:00:00.000000000 +0100
594d0238 911+++ linux/Documentation/ABI/testing/debugfs-aufs 2022-11-05 23:02:18.955889283 +0100
062440b3 912@@ -0,0 +1,55 @@
7f207e10
AM
913+What: /debug/aufs/si_<id>/
914+Date: March 2009
f6b6e03d 915+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
916+Description:
917+ Under /debug/aufs, a directory named si_<id> is created
918+ per aufs mount, where <id> is a unique id generated
919+ internally.
1facf9fc 920+
86dc4139
AM
921+What: /debug/aufs/si_<id>/plink
922+Date: Apr 2013
f6b6e03d 923+Contact: J. R. Okajima <hooanon05g@gmail.com>
86dc4139
AM
924+Description:
925+ It has three lines and shows the information about the
926+ pseudo-link. The first line is a single number
927+ representing a number of buckets. The second line is a
928+ number of pseudo-links per buckets (separated by a
929+ blank). The last line is a single number representing a
930+ total number of psedo-links.
931+ When the aufs mount option 'noplink' is specified, it
932+ will show "1\n0\n0\n".
933+
7f207e10
AM
934+What: /debug/aufs/si_<id>/xib
935+Date: March 2009
f6b6e03d 936+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
937+Description:
938+ It shows the consumed blocks by xib (External Inode Number
939+ Bitmap), its block size and file size.
940+ When the aufs mount option 'noxino' is specified, it
941+ will be empty. About XINO files, see the aufs manual.
942+
0b2a12c6 943+What: /debug/aufs/si_<id>/xi<branch-index>
7f207e10 944+Date: March 2009
f6b6e03d 945+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
946+Description:
947+ It shows the consumed blocks by xino (External Inode Number
948+ Translation Table), its link count, block size and file
949+ size.
062440b3
AM
950+ Due to the file size limit, there may exist multiple
951+ xino files per branch. In this case, "-N" is added to
952+ the filename and it corresponds to the index of the
953+ internal xino array. "-0" is omitted.
954+ When the aufs mount option 'noxino' is specified, Those
955+ entries won't exist. About XINO files, see the aufs
956+ manual.
7f207e10
AM
957+
958+What: /debug/aufs/si_<id>/xigen
959+Date: March 2009
f6b6e03d 960+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
961+Description:
962+ It shows the consumed blocks by xigen (External Inode
963+ Generation Table), its block size and file size.
964+ If CONFIG_AUFS_EXPORT is disabled, this entry will not
965+ be created.
966+ When the aufs mount option 'noxino' is specified, it
967+ will be empty. About XINO files, see the aufs manual.
968diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
eca34b5c 969--- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs 1970-01-01 01:00:00.000000000 +0100
594d0238 970+++ linux/Documentation/ABI/testing/sysfs-aufs 2022-11-05 23:02:18.955889283 +0100
392086de 971@@ -0,0 +1,31 @@
7f207e10
AM
972+What: /sys/fs/aufs/si_<id>/
973+Date: March 2009
f6b6e03d 974+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
975+Description:
976+ Under /sys/fs/aufs, a directory named si_<id> is created
977+ per aufs mount, where <id> is a unique id generated
978+ internally.
979+
0b2a12c6 980+What: /sys/fs/aufs/si_<id>/br<idx>
7f207e10 981+Date: March 2009
f6b6e03d 982+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
983+Description:
984+ It shows the abolute path of a member directory (which
985+ is called branch) in aufs, and its permission.
986+
0b2a12c6 987+What: /sys/fs/aufs/si_<id>/brid<idx>
392086de 988+Date: July 2013
f6b6e03d 989+Contact: J. R. Okajima <hooanon05g@gmail.com>
392086de
AM
990+Description:
991+ It shows the id of a member directory (which is called
992+ branch) in aufs.
993+
7f207e10
AM
994+What: /sys/fs/aufs/si_<id>/xi_path
995+Date: March 2009
f6b6e03d 996+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
997+Description:
998+ It shows the abolute path of XINO (External Inode Number
999+ Bitmap, Translation Table and Generation Table) file
1000+ even if it is the default path.
1001+ When the aufs mount option 'noxino' is specified, it
1002+ will be empty. About XINO files, see the aufs manual.
53392da6 1003diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
eca34b5c 1004--- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt 1970-01-01 01:00:00.000000000 +0100
594d0238 1005+++ linux/Documentation/filesystems/aufs/design/01intro.txt 2022-11-05 23:02:18.955889283 +0100
1c60b727 1006@@ -0,0 +1,171 @@
53392da6 1007+
0dcfbb52
AM
1008+# Copyright (C) 2005-2022 Junjiro R. Okajima
1009+#
53392da6
AM
1010+# This program is free software; you can redistribute it and/or modify
1011+# it under the terms of the GNU General Public License as published by
1012+# the Free Software Foundation; either version 2 of the License, or
1013+# (at your option) any later version.
0dcfbb52 1014+#
53392da6
AM
1015+# This program is distributed in the hope that it will be useful,
1016+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1017+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1018+# GNU General Public License for more details.
0dcfbb52 1019+#
53392da6 1020+# You should have received a copy of the GNU General Public License
523b37e3 1021+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1022+
1023+Introduction
1024+----------------------------------------
1025+
3c1bdaff 1026+aufs [ei ju: ef es] | /ey-yoo-ef-es/ | [a u f s]
53392da6
AM
1027+1. abbrev. for "advanced multi-layered unification filesystem".
1028+2. abbrev. for "another unionfs".
1029+3. abbrev. for "auf das" in German which means "on the" in English.
1030+ Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
1031+ But "Filesystem aufs Filesystem" is hard to understand.
1c60b727 1032+4. abbrev. for "African Urban Fashion Show".
53392da6
AM
1033+
1034+AUFS is a filesystem with features:
1035+- multi layered stackable unification filesystem, the member directory
1036+ is called as a branch.
1037+- branch permission and attribute, 'readonly', 'real-readonly',
7e9cd9fe 1038+ 'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
53392da6
AM
1039+ combination.
1040+- internal "file copy-on-write".
1041+- logical deletion, whiteout.
1042+- dynamic branch manipulation, adding, deleting and changing permission.
1043+- allow bypassing aufs, user's direct branch access.
1044+- external inode number translation table and bitmap which maintains the
1045+ persistent aufs inode number.
1046+- seekable directory, including NFS readdir.
1047+- file mapping, mmap and sharing pages.
1048+- pseudo-link, hardlink over branches.
1049+- loopback mounted filesystem as a branch.
1050+- several policies to select one among multiple writable branches.
1051+- revert a single systemcall when an error occurs in aufs.
1052+- and more...
1053+
1054+
1055+Multi Layered Stackable Unification Filesystem
1056+----------------------------------------------------------------------
1057+Most people already knows what it is.
1058+It is a filesystem which unifies several directories and provides a
1059+merged single directory. When users access a file, the access will be
1060+passed/re-directed/converted (sorry, I am not sure which English word is
1061+correct) to the real file on the member filesystem. The member
1062+filesystem is called 'lower filesystem' or 'branch' and has a mode
1063+'readonly' and 'readwrite.' And the deletion for a file on the lower
1064+readonly branch is handled by creating 'whiteout' on the upper writable
1065+branch.
1066+
1067+On LKML, there have been discussions about UnionMount (Jan Blunck,
1068+Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
1069+different approaches to implement the merged-view.
1070+The former tries putting it into VFS, and the latter implements as a
1071+separate filesystem.
1072+(If I misunderstand about these implementations, please let me know and
1073+I shall correct it. Because it is a long time ago when I read their
1074+source files last time).
1075+
1076+UnionMount's approach will be able to small, but may be hard to share
1077+branches between several UnionMount since the whiteout in it is
1078+implemented in the inode on branch filesystem and always
1079+shared. According to Bharata's post, readdir does not seems to be
1080+finished yet.
1081+There are several missing features known in this implementations such as
1082+- for users, the inode number may change silently. eg. copy-up.
1083+- link(2) may break by copy-up.
1084+- read(2) may get an obsoleted filedata (fstat(2) too).
1085+- fcntl(F_SETLK) may be broken by copy-up.
1086+- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
1087+ open(O_RDWR).
1088+
7e9cd9fe
AM
1089+In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
1090+merged into mainline. This is another implementation of UnionMount as a
1091+separated filesystem. All the limitations and known problems which
1092+UnionMount are equally inherited to "overlay" filesystem.
1093+
1094+Unionfs has a longer history. When I started implementing a stackable
1095+filesystem (Aug 2005), it already existed. It has virtual super_block,
1096+inode, dentry and file objects and they have an array pointing lower
1097+same kind objects. After contributing many patches for Unionfs, I
1098+re-started my project AUFS (Jun 2006).
53392da6
AM
1099+
1100+In AUFS, the structure of filesystem resembles to Unionfs, but I
1101+implemented my own ideas, approaches and enhancements and it became
1102+totally different one.
1103+
1104+Comparing DM snapshot and fs based implementation
1105+- the number of bytes to be copied between devices is much smaller.
1106+- the type of filesystem must be one and only.
1107+- the fs must be writable, no readonly fs, even for the lower original
1108+ device. so the compression fs will not be usable. but if we use
1109+ loopback mount, we may address this issue.
1110+ for instance,
1111+ mount /cdrom/squashfs.img /sq
1112+ losetup /sq/ext2.img
1113+ losetup /somewhere/cow
1114+ dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1115+- it will be difficult (or needs more operations) to extract the
1116+ difference between the original device and COW.
1117+- DM snapshot-merge may help a lot when users try merging. in the
1118+ fs-layer union, users will use rsync(1).
1119+
7e9cd9fe
AM
1120+You may want to read my old paper "Filesystems in LiveCD"
1121+(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
53392da6 1122+
7e9cd9fe
AM
1123+
1124+Several characters/aspects/persona of aufs
53392da6
AM
1125+----------------------------------------------------------------------
1126+
7e9cd9fe 1127+Aufs has several characters, aspects or persona.
53392da6
AM
1128+1. a filesystem, callee of VFS helper
1129+2. sub-VFS, caller of VFS helper for branches
1130+3. a virtual filesystem which maintains persistent inode number
1131+4. reader/writer of files on branches such like an application
1132+
1133+1. Callee of VFS Helper
1134+As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1135+unlink(2) from an application reaches sys_unlink() kernel function and
1136+then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1137+calls filesystem specific unlink operation. Actually aufs implements the
1138+unlink operation but it behaves like a redirector.
1139+
1140+2. Caller of VFS Helper for Branches
1141+aufs_unlink() passes the unlink request to the branch filesystem as if
1142+it were called from VFS. So the called unlink operation of the branch
1143+filesystem acts as usual. As a caller of VFS helper, aufs should handle
1144+every necessary pre/post operation for the branch filesystem.
1145+- acquire the lock for the parent dir on a branch
1146+- lookup in a branch
1147+- revalidate dentry on a branch
1148+- mnt_want_write() for a branch
1149+- vfs_unlink() for a branch
1150+- mnt_drop_write() for a branch
1151+- release the lock on a branch
1152+
1153+3. Persistent Inode Number
1154+One of the most important issue for a filesystem is to maintain inode
1155+numbers. This is particularly important to support exporting a
1156+filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1157+backend block device for its own. But some storage is necessary to
7e9cd9fe
AM
1158+keep and maintain the inode numbers. It may be a large space and may not
1159+suit to keep in memory. Aufs rents some space from its first writable
1160+branch filesystem (by default) and creates file(s) on it. These files
1161+are created by aufs internally and removed soon (currently) keeping
1162+opened.
53392da6
AM
1163+Note: Because these files are removed, they are totally gone after
1164+ unmounting aufs. It means the inode numbers are not persistent
1165+ across unmount or reboot. I have a plan to make them really
1166+ persistent which will be important for aufs on NFS server.
1167+
1168+4. Read/Write Files Internally (copy-on-write)
1169+Because a branch can be readonly, when you write a file on it, aufs will
1170+"copy-up" it to the upper writable branch internally. And then write the
1171+originally requested thing to the file. Generally kernel doesn't
1172+open/read/write file actively. In aufs, even a single write may cause a
1173+internal "file copy". This behaviour is very similar to cp(1) command.
1174+
1175+Some people may think it is better to pass such work to user space
1176+helper, instead of doing in kernel space. Actually I am still thinking
1177+about it. But currently I have implemented it in kernel space.
1178diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
eca34b5c 1179--- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
594d0238 1180+++ linux/Documentation/filesystems/aufs/design/02struct.txt 2022-11-05 23:02:18.955889283 +0100
7e9cd9fe 1181@@ -0,0 +1,258 @@
53392da6 1182+
0dcfbb52
AM
1183+# Copyright (C) 2005-2022 Junjiro R. Okajima
1184+#
53392da6
AM
1185+# This program is free software; you can redistribute it and/or modify
1186+# it under the terms of the GNU General Public License as published by
1187+# the Free Software Foundation; either version 2 of the License, or
1188+# (at your option) any later version.
0dcfbb52 1189+#
53392da6
AM
1190+# This program is distributed in the hope that it will be useful,
1191+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1192+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1193+# GNU General Public License for more details.
0dcfbb52 1194+#
53392da6 1195+# You should have received a copy of the GNU General Public License
523b37e3 1196+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1197+
1198+Basic Aufs Internal Structure
1199+
1200+Superblock/Inode/Dentry/File Objects
1201+----------------------------------------------------------------------
1202+As like an ordinary filesystem, aufs has its own
1203+superblock/inode/dentry/file objects. All these objects have a
1204+dynamically allocated array and store the same kind of pointers to the
1205+lower filesystem, branch.
1206+For example, when you build a union with one readwrite branch and one
1207+readonly, mounted /au, /rw and /ro respectively.
1208+- /au = /rw + /ro
1209+- /ro/fileA exists but /rw/fileA
1210+
1211+Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1212+pointers are stored in a aufs dentry. The array in aufs dentry will be,
7e9cd9fe 1213+- [0] = NULL (because /rw/fileA doesn't exist)
53392da6
AM
1214+- [1] = /ro/fileA
1215+
1216+This style of an array is essentially same to the aufs
1217+superblock/inode/dentry/file objects.
1218+
1219+Because aufs supports manipulating branches, ie. add/delete/change
7e9cd9fe
AM
1220+branches dynamically, these objects has its own generation. When
1221+branches are changed, the generation in aufs superblock is
1222+incremented. And a generation in other object are compared when it is
1223+accessed. When a generation in other objects are obsoleted, aufs
1224+refreshes the internal array.
53392da6
AM
1225+
1226+
1227+Superblock
1228+----------------------------------------------------------------------
1229+Additionally aufs superblock has some data for policies to select one
1230+among multiple writable branches, XIB files, pseudo-links and kobject.
1231+See below in detail.
7e9cd9fe
AM
1232+About the policies which supports copy-down a directory, see
1233+wbr_policy.txt too.
53392da6
AM
1234+
1235+
1236+Branch and XINO(External Inode Number Translation Table)
1237+----------------------------------------------------------------------
1238+Every branch has its own xino (external inode number translation table)
1239+file. The xino file is created and unlinked by aufs internally. When two
1240+members of a union exist on the same filesystem, they share the single
1241+xino file.
1242+The struct of a xino file is simple, just a sequence of aufs inode
1243+numbers which is indexed by the lower inode number.
1244+In the above sample, assume the inode number of /ro/fileA is i111 and
1245+aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1246+4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1247+
1248+When the inode numbers are not contiguous, the xino file will be sparse
1249+which has a hole in it and doesn't consume as much disk space as it
1250+might appear. If your branch filesystem consumes disk space for such
1251+holes, then you should specify 'xino=' option at mounting aufs.
1252+
7e9cd9fe
AM
1253+Aufs has a mount option to free the disk blocks for such holes in XINO
1254+files on tmpfs or ramdisk. But it is not so effective actually. If you
1255+meet a problem of disk shortage due to XINO files, then you should try
1256+"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1257+The patch localizes the assignment inumbers per tmpfs-mount and avoid
1258+the holes in XINO files.
1259+
53392da6 1260+Also a writable branch has three kinds of "whiteout bases". All these
7e9cd9fe 1261+are existed when the branch is joined to aufs, and their names are
53392da6
AM
1262+whiteout-ed doubly, so that users will never see their names in aufs
1263+hierarchy.
7e9cd9fe 1264+1. a regular file which will be hardlinked to all whiteouts.
53392da6 1265+2. a directory to store a pseudo-link.
7e9cd9fe 1266+3. a directory to store an "orphan"-ed file temporary.
53392da6
AM
1267+
1268+1. Whiteout Base
1269+ When you remove a file on a readonly branch, aufs handles it as a
1270+ logical deletion and creates a whiteout on the upper writable branch
1271+ as a hardlink of this file in order not to consume inode on the
1272+ writable branch.
1273+2. Pseudo-link Dir
1274+ See below, Pseudo-link.
1275+3. Step-Parent Dir
1276+ When "fileC" exists on the lower readonly branch only and it is
1277+ opened and removed with its parent dir, and then user writes
1278+ something into it, then aufs copies-up fileC to this
1279+ directory. Because there is no other dir to store fileC. After
1280+ creating a file under this dir, the file is unlinked.
1281+
1282+Because aufs supports manipulating branches, ie. add/delete/change
7e9cd9fe
AM
1283+dynamically, a branch has its own id. When the branch order changes,
1284+aufs finds the new index by searching the branch id.
53392da6
AM
1285+
1286+
1287+Pseudo-link
1288+----------------------------------------------------------------------
1289+Assume "fileA" exists on the lower readonly branch only and it is
1290+hardlinked to "fileB" on the branch. When you write something to fileA,
1291+aufs copies-up it to the upper writable branch. Additionally aufs
1292+creates a hardlink under the Pseudo-link Directory of the writable
1293+branch. The inode of a pseudo-link is kept in aufs super_block as a
1294+simple list. If fileB is read after unlinking fileA, aufs returns
1295+filedata from the pseudo-link instead of the lower readonly
1296+branch. Because the pseudo-link is based upon the inode, to keep the
7e9cd9fe 1297+inode number by xino (see above) is essentially necessary.
53392da6
AM
1298+
1299+All the hardlinks under the Pseudo-link Directory of the writable branch
1300+should be restored in a proper location later. Aufs provides a utility
1301+to do this. The userspace helpers executed at remounting and unmounting
1302+aufs by default.
1303+During this utility is running, it puts aufs into the pseudo-link
1304+maintenance mode. In this mode, only the process which began the
1305+maintenance mode (and its child processes) is allowed to operate in
1306+aufs. Some other processes which are not related to the pseudo-link will
1307+be allowed to run too, but the rest have to return an error or wait
1308+until the maintenance mode ends. If a process already acquires an inode
1309+mutex (in VFS), it has to return an error.
1310+
1311+
1312+XIB(external inode number bitmap)
1313+----------------------------------------------------------------------
1314+Addition to the xino file per a branch, aufs has an external inode number
7e9cd9fe
AM
1315+bitmap in a superblock object. It is also an internal file such like a
1316+xino file.
53392da6
AM
1317+It is a simple bitmap to mark whether the aufs inode number is in-use or
1318+not.
1319+To reduce the file I/O, aufs prepares a single memory page to cache xib.
1320+
7e9cd9fe 1321+As well as XINO files, aufs has a feature to truncate/refresh XIB to
53392da6
AM
1322+reduce the number of consumed disk blocks for these files.
1323+
1324+
1325+Virtual or Vertical Dir, and Readdir in Userspace
1326+----------------------------------------------------------------------
1327+In order to support multiple layers (branches), aufs readdir operation
1328+constructs a virtual dir block on memory. For readdir, aufs calls
1329+vfs_readdir() internally for each dir on branches, merges their entries
1330+with eliminating the whiteout-ed ones, and sets it to file (dir)
1331+object. So the file object has its entry list until it is closed. The
1332+entry list will be updated when the file position is zero and becomes
7e9cd9fe 1333+obsoleted. This decision is made in aufs automatically.
53392da6
AM
1334+
1335+The dynamically allocated memory block for the name of entries has a
1336+unit of 512 bytes (by default) and stores the names contiguously (no
1337+padding). Another block for each entry is handled by kmem_cache too.
1338+During building dir blocks, aufs creates hash list and judging whether
1339+the entry is whiteouted by its upper branch or already listed.
1340+The merged result is cached in the corresponding inode object and
1341+maintained by a customizable life-time option.
1342+
1343+Some people may call it can be a security hole or invite DoS attack
1344+since the opened and once readdir-ed dir (file object) holds its entry
1345+list and becomes a pressure for system memory. But I'd say it is similar
1346+to files under /proc or /sys. The virtual files in them also holds a
1347+memory page (generally) while they are opened. When an idea to reduce
1348+memory for them is introduced, it will be applied to aufs too.
1349+For those who really hate this situation, I've developed readdir(3)
1350+library which operates this merging in userspace. You just need to set
1351+LD_PRELOAD environment variable, and aufs will not consume no memory in
1352+kernel space for readdir(3).
1353+
1354+
1355+Workqueue
1356+----------------------------------------------------------------------
1357+Aufs sometimes requires privilege access to a branch. For instance,
1358+in copy-up/down operation. When a user process is going to make changes
1359+to a file which exists in the lower readonly branch only, and the mode
1360+of one of ancestor directories may not be writable by a user
1361+process. Here aufs copy-up the file with its ancestors and they may
1362+require privilege to set its owner/group/mode/etc.
1363+This is a typical case of a application character of aufs (see
1364+Introduction).
1365+
1366+Aufs uses workqueue synchronously for this case. It creates its own
1367+workqueue. The workqueue is a kernel thread and has privilege. Aufs
1368+passes the request to call mkdir or write (for example), and wait for
1369+its completion. This approach solves a problem of a signal handler
1370+simply.
1371+If aufs didn't adopt the workqueue and changed the privilege of the
7e9cd9fe
AM
1372+process, then the process may receive the unexpected SIGXFSZ or other
1373+signals.
53392da6
AM
1374+
1375+Also aufs uses the system global workqueue ("events" kernel thread) too
1376+for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1377+whiteout base and etc. This is unrelated to a privilege.
1378+Most of aufs operation tries acquiring a rw_semaphore for aufs
1379+superblock at the beginning, at the same time waits for the completion
1380+of all queued asynchronous tasks.
1381+
1382+
1383+Whiteout
1384+----------------------------------------------------------------------
1385+The whiteout in aufs is very similar to Unionfs's. That is represented
1386+by its filename. UnionMount takes an approach of a file mode, but I am
1387+afraid several utilities (find(1) or something) will have to support it.
1388+
1389+Basically the whiteout represents "logical deletion" which stops aufs to
1390+lookup further, but also it represents "dir is opaque" which also stop
7e9cd9fe 1391+further lookup.
53392da6
AM
1392+
1393+In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1394+In order to make several functions in a single systemcall to be
1395+revertible, aufs adopts an approach to rename a directory to a temporary
1396+unique whiteouted name.
1397+For example, in rename(2) dir where the target dir already existed, aufs
1398+renames the target dir to a temporary unique whiteouted name before the
7e9cd9fe 1399+actual rename on a branch, and then handles other actions (make it opaque,
53392da6
AM
1400+update the attributes, etc). If an error happens in these actions, aufs
1401+simply renames the whiteouted name back and returns an error. If all are
1402+succeeded, aufs registers a function to remove the whiteouted unique
1403+temporary name completely and asynchronously to the system global
1404+workqueue.
1405+
1406+
1407+Copy-up
1408+----------------------------------------------------------------------
1409+It is a well-known feature or concept.
1410+When user modifies a file on a readonly branch, aufs operate "copy-up"
1411+internally and makes change to the new file on the upper writable branch.
1412+When the trigger systemcall does not update the timestamps of the parent
1413+dir, aufs reverts it after copy-up.
c2b27bf2
AM
1414+
1415+
1416+Move-down (aufs3.9 and later)
1417+----------------------------------------------------------------------
1418+"Copy-up" is one of the essential feature in aufs. It copies a file from
1419+the lower readonly branch to the upper writable branch when a user
1420+changes something about the file.
1421+"Move-down" is an opposite action of copy-up. Basically this action is
1422+ran manually instead of automatically and internally.
076b876e
AM
1423+For desgin and implementation, aufs has to consider these issues.
1424+- whiteout for the file may exist on the lower branch.
1425+- ancestor directories may not exist on the lower branch.
1426+- diropq for the ancestor directories may exist on the upper branch.
1427+- free space on the lower branch will reduce.
1428+- another access to the file may happen during moving-down, including
7e9cd9fe 1429+ UDBA (see "Revalidate Dentry and UDBA").
076b876e
AM
1430+- the file should not be hard-linked nor pseudo-linked. they should be
1431+ handled by auplink utility later.
c2b27bf2
AM
1432+
1433+Sometimes users want to move-down a file from the upper writable branch
1434+to the lower readonly or writable branch. For instance,
1435+- the free space of the upper writable branch is going to run out.
1436+- create a new intermediate branch between the upper and lower branch.
1437+- etc.
1438+
1439+For this purpose, use "aumvdown" command in aufs-util.git.
b912730e 1440diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt linux/Documentation/filesystems/aufs/design/03atomic_open.txt
eca34b5c 1441--- /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt 1970-01-01 01:00:00.000000000 +0100
594d0238 1442+++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt 2022-11-05 23:02:18.955889283 +0100
b912730e
AM
1443@@ -0,0 +1,85 @@
1444+
0dcfbb52
AM
1445+# Copyright (C) 2015-2022 Junjiro R. Okajima
1446+#
b912730e
AM
1447+# This program is free software; you can redistribute it and/or modify
1448+# it under the terms of the GNU General Public License as published by
1449+# the Free Software Foundation; either version 2 of the License, or
1450+# (at your option) any later version.
0dcfbb52 1451+#
b912730e
AM
1452+# This program is distributed in the hope that it will be useful,
1453+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1454+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1455+# GNU General Public License for more details.
0dcfbb52 1456+#
b912730e
AM
1457+# You should have received a copy of the GNU General Public License
1458+# along with this program. If not, see <http://www.gnu.org/licenses/>.
1459+
1460+Support for a branch who has its ->atomic_open()
1461+----------------------------------------------------------------------
1462+The filesystems who implement its ->atomic_open() are not majority. For
1463+example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
1464+particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
1465+->atomic_open(), NFSv4 returns an error for this open(2). While I am not
1466+sure whether all filesystems who have ->atomic_open() behave like this,
1467+but NFSv4 surely returns the error.
1468+
1469+In order to support ->atomic_open() for aufs, there are a few
1470+approaches.
1471+
1472+A. Introduce aufs_atomic_open()
1473+ - calls one of VFS:do_last(), lookup_open() or atomic_open() for
1474+ branch fs.
1475+B. Introduce aufs_atomic_open() calling create, open and chmod. this is
1476+ an aufs user Pip Cet's approach
1477+ - calls aufs_create(), VFS finish_open() and notify_change().
1478+ - pass fake-mode to finish_open(), and then correct the mode by
1479+ notify_change().
1480+C. Extend aufs_open() to call branch fs's ->atomic_open()
1481+ - no aufs_atomic_open().
1482+ - aufs_lookup() registers the TID to an aufs internal object.
1483+ - aufs_create() does nothing when the matching TID is registered, but
1484+ registers the mode.
1485+ - aufs_open() calls branch fs's ->atomic_open() when the matching
1486+ TID is registered.
1487+D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
1488+ credential
1489+ - no aufs_atomic_open().
1490+ - aufs_create() registers the TID to an internal object. this info
1491+ represents "this process created this file just now."
1492+ - when aufs gets EACCES from branch fs's ->open(), then confirm the
1493+ registered TID and re-try open() with superuser's credential.
1494+
1495+Pros and cons for each approach.
1496+
1497+A.
1498+ - straightforward but highly depends upon VFS internal.
1499+ - the atomic behavaiour is kept.
1500+ - some of parameters such as nameidata are hard to reproduce for
1501+ branch fs.
1502+ - large overhead.
1503+B.
1504+ - easy to implement.
1505+ - the atomic behavaiour is lost.
1506+C.
1507+ - the atomic behavaiour is kept.
1508+ - dirty and tricky.
1509+ - VFS checks whether the file is created correctly after calling
1510+ ->create(), which means this approach doesn't work.
1511+D.
1512+ - easy to implement.
1513+ - the atomic behavaiour is lost.
1514+ - to open a file with superuser's credential and give it to a user
1515+ process is a bad idea, since the file object keeps the credential
1516+ in it. It may affect LSM or something. This approach doesn't work
1517+ either.
1518+
1519+The approach A is ideal, but it hard to implement. So here is a
1520+variation of A, which is to be implemented.
1521+
1522+A-1. Introduce aufs_atomic_open()
1523+ - calls branch fs ->atomic_open() if exists. otherwise calls
1524+ vfs_create() and finish_open().
1525+ - the demerit is that the several checks after branch fs
1526+ ->atomic_open() are lost. in the ordinary case, the checks are
1527+ done by VFS:do_last(), lookup_open() and atomic_open(). some can
1528+ be implemented in aufs, but not all I am afraid.
53392da6 1529diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
eca34b5c 1530--- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
594d0238 1531+++ linux/Documentation/filesystems/aufs/design/03lookup.txt 2022-11-05 23:02:18.959222617 +0100
7e9cd9fe 1532@@ -0,0 +1,113 @@
53392da6 1533+
0dcfbb52
AM
1534+# Copyright (C) 2005-2022 Junjiro R. Okajima
1535+#
53392da6
AM
1536+# This program is free software; you can redistribute it and/or modify
1537+# it under the terms of the GNU General Public License as published by
1538+# the Free Software Foundation; either version 2 of the License, or
1539+# (at your option) any later version.
0dcfbb52 1540+#
53392da6
AM
1541+# This program is distributed in the hope that it will be useful,
1542+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1543+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1544+# GNU General Public License for more details.
0dcfbb52 1545+#
53392da6 1546+# You should have received a copy of the GNU General Public License
523b37e3 1547+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1548+
1549+Lookup in a Branch
1550+----------------------------------------------------------------------
1551+Since aufs has a character of sub-VFS (see Introduction), it operates
7e9cd9fe
AM
1552+lookup for branches as VFS does. It may be a heavy work. But almost all
1553+lookup operation in aufs is the simplest case, ie. lookup only an entry
1554+directly connected to its parent. Digging down the directory hierarchy
1555+is unnecessary. VFS has a function lookup_one_len() for that use, and
1556+aufs calls it.
1557+
1558+When a branch is a remote filesystem, aufs basically relies upon its
53392da6
AM
1559+->d_revalidate(), also aufs forces the hardest revalidate tests for
1560+them.
1561+For d_revalidate, aufs implements three levels of revalidate tests. See
1562+"Revalidate Dentry and UDBA" in detail.
1563+
1564+
076b876e
AM
1565+Test Only the Highest One for the Directory Permission (dirperm1 option)
1566+----------------------------------------------------------------------
1567+Let's try case study.
1568+- aufs has two branches, upper readwrite and lower readonly.
1569+ /au = /rw + /ro
1570+- "dirA" exists under /ro, but /rw. and its mode is 0700.
1571+- user invoked "chmod a+rx /au/dirA"
1572+- the internal copy-up is activated and "/rw/dirA" is created and its
7e9cd9fe 1573+ permission bits are set to world readable.
076b876e
AM
1574+- then "/au/dirA" becomes world readable?
1575+
1576+In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1577+or it may be a natively readonly filesystem. If aufs respects the lower
1578+branch, it should not respond readdir request from other users. But user
1579+allowed it by chmod. Should really aufs rejects showing the entries
1580+under /ro/dirA?
1581+
7e9cd9fe
AM
1582+To be honest, I don't have a good solution for this case. So aufs
1583+implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1584+users.
076b876e
AM
1585+When dirperm1 is specified, aufs checks only the highest one for the
1586+directory permission, and shows the entries. Otherwise, as usual, checks
1587+every dir existing on all branches and rejects the request.
1588+
1589+As a side effect, dirperm1 option improves the performance of aufs
1590+because the number of permission check is reduced when the number of
1591+branch is many.
1592+
1593+
53392da6
AM
1594+Revalidate Dentry and UDBA (User's Direct Branch Access)
1595+----------------------------------------------------------------------
1596+Generally VFS helpers re-validate a dentry as a part of lookup.
1597+0. digging down the directory hierarchy.
1598+1. lock the parent dir by its i_mutex.
1599+2. lookup the final (child) entry.
1600+3. revalidate it.
1601+4. call the actual operation (create, unlink, etc.)
1602+5. unlock the parent dir
1603+
1604+If the filesystem implements its ->d_revalidate() (step 3), then it is
1605+called. Actually aufs implements it and checks the dentry on a branch is
1606+still valid.
1607+But it is not enough. Because aufs has to release the lock for the
1608+parent dir on a branch at the end of ->lookup() (step 2) and
1609+->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1610+held by VFS.
1611+If the file on a branch is changed directly, eg. bypassing aufs, after
1612+aufs released the lock, then the subsequent operation may cause
1613+something unpleasant result.
1614+
1615+This situation is a result of VFS architecture, ->lookup() and
1616+->d_revalidate() is separated. But I never say it is wrong. It is a good
1617+design from VFS's point of view. It is just not suitable for sub-VFS
1618+character in aufs.
1619+
1620+Aufs supports such case by three level of revalidation which is
1621+selectable by user.
1622+1. Simple Revalidate
1623+ Addition to the native flow in VFS's, confirm the child-parent
1624+ relationship on the branch just after locking the parent dir on the
1625+ branch in the "actual operation" (step 4). When this validation
1626+ fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1627+ checks the validation of the dentry on branches.
1628+2. Monitor Changes Internally by Inotify/Fsnotify
1629+ Addition to above, in the "actual operation" (step 4) aufs re-lookup
1630+ the dentry on the branch, and returns EBUSY if it finds different
1631+ dentry.
1632+ Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1633+ during it is in cache. When the event is notified, aufs registers a
1634+ function to kernel 'events' thread by schedule_work(). And the
1635+ function sets some special status to the cached aufs dentry and inode
1636+ private data. If they are not cached, then aufs has nothing to
1637+ do. When the same file is accessed through aufs (step 0-3) later,
1638+ aufs will detect the status and refresh all necessary data.
1639+ In this mode, aufs has to ignore the event which is fired by aufs
1640+ itself.
1641+3. No Extra Validation
1642+ This is the simplest test and doesn't add any additional revalidation
7e9cd9fe 1643+ test, and skip the revalidation in step 4. It is useful and improves
53392da6
AM
1644+ aufs performance when system surely hide the aufs branches from user,
1645+ by over-mounting something (or another method).
1646diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
eca34b5c 1647--- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
594d0238 1648+++ linux/Documentation/filesystems/aufs/design/04branch.txt 2022-11-05 23:02:18.959222617 +0100
7e9cd9fe 1649@@ -0,0 +1,74 @@
53392da6 1650+
0dcfbb52
AM
1651+# Copyright (C) 2005-2022 Junjiro R. Okajima
1652+#
53392da6
AM
1653+# This program is free software; you can redistribute it and/or modify
1654+# it under the terms of the GNU General Public License as published by
1655+# the Free Software Foundation; either version 2 of the License, or
1656+# (at your option) any later version.
0dcfbb52 1657+#
53392da6
AM
1658+# This program is distributed in the hope that it will be useful,
1659+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1660+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1661+# GNU General Public License for more details.
0dcfbb52 1662+#
53392da6 1663+# You should have received a copy of the GNU General Public License
523b37e3 1664+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1665+
1666+Branch Manipulation
1667+
1668+Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1669+and changing its permission/attribute, there are a lot of works to do.
1670+
1671+
1672+Add a Branch
1673+----------------------------------------------------------------------
1674+o Confirm the adding dir exists outside of aufs, including loopback
7e9cd9fe 1675+ mount, and its various attributes.
53392da6
AM
1676+o Initialize the xino file and whiteout bases if necessary.
1677+ See struct.txt.
1678+
1679+o Check the owner/group/mode of the directory
1680+ When the owner/group/mode of the adding directory differs from the
1681+ existing branch, aufs issues a warning because it may impose a
1682+ security risk.
1683+ For example, when a upper writable branch has a world writable empty
1684+ top directory, a malicious user can create any files on the writable
1685+ branch directly, like copy-up and modify manually. If something like
1686+ /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1687+ writable branch, and the writable branch is world-writable, then a
1688+ malicious guy may create /etc/passwd on the writable branch directly
1689+ and the infected file will be valid in aufs.
7e9cd9fe 1690+ I am afraid it can be a security issue, but aufs can do nothing except
53392da6
AM
1691+ producing a warning.
1692+
1693+
1694+Delete a Branch
1695+----------------------------------------------------------------------
1696+o Confirm the deleting branch is not busy
1697+ To be general, there is one merit to adopt "remount" interface to
1698+ manipulate branches. It is to discard caches. At deleting a branch,
1699+ aufs checks the still cached (and connected) dentries and inodes. If
1700+ there are any, then they are all in-use. An inode without its
1701+ corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1702+
1703+ For the cached one, aufs checks whether the same named entry exists on
1704+ other branches.
1705+ If the cached one is a directory, because aufs provides a merged view
1706+ to users, as long as one dir is left on any branch aufs can show the
1707+ dir to users. In this case, the branch can be removed from aufs.
1708+ Otherwise aufs rejects deleting the branch.
1709+
1710+ If any file on the deleting branch is opened by aufs, then aufs
1711+ rejects deleting.
1712+
1713+
1714+Modify the Permission of a Branch
1715+----------------------------------------------------------------------
1716+o Re-initialize or remove the xino file and whiteout bases if necessary.
1717+ See struct.txt.
1718+
1719+o rw --> ro: Confirm the modifying branch is not busy
1720+ Aufs rejects the request if any of these conditions are true.
1721+ - a file on the branch is mmap-ed.
1722+ - a regular file on the branch is opened for write and there is no
1723+ same named entry on the upper branch.
1724diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
eca34b5c 1725--- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt 1970-01-01 01:00:00.000000000 +0100
594d0238 1726+++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt 2022-11-05 23:02:18.959222617 +0100
523b37e3 1727@@ -0,0 +1,64 @@
53392da6 1728+
0dcfbb52
AM
1729+# Copyright (C) 2005-2022 Junjiro R. Okajima
1730+#
53392da6
AM
1731+# This program is free software; you can redistribute it and/or modify
1732+# it under the terms of the GNU General Public License as published by
1733+# the Free Software Foundation; either version 2 of the License, or
1734+# (at your option) any later version.
0dcfbb52 1735+#
53392da6
AM
1736+# This program is distributed in the hope that it will be useful,
1737+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1738+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1739+# GNU General Public License for more details.
0dcfbb52 1740+#
53392da6 1741+# You should have received a copy of the GNU General Public License
523b37e3 1742+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1743+
1744+Policies to Select One among Multiple Writable Branches
1745+----------------------------------------------------------------------
1746+When the number of writable branch is more than one, aufs has to decide
1747+the target branch for file creation or copy-up. By default, the highest
1748+writable branch which has the parent (or ancestor) dir of the target
1749+file is chosen (top-down-parent policy).
1750+By user's request, aufs implements some other policies to select the
7e9cd9fe
AM
1751+writable branch, for file creation several policies, round-robin,
1752+most-free-space, and other policies. For copy-up, top-down-parent,
1753+bottom-up-parent, bottom-up and others.
53392da6
AM
1754+
1755+As expected, the round-robin policy selects the branch in circular. When
1756+you have two writable branches and creates 10 new files, 5 files will be
1757+created for each branch. mkdir(2) systemcall is an exception. When you
1758+create 10 new directories, all will be created on the same branch.
1759+And the most-free-space policy selects the one which has most free
1760+space among the writable branches. The amount of free space will be
1761+checked by aufs internally, and users can specify its time interval.
1762+
1763+The policies for copy-up is more simple,
1764+top-down-parent is equivalent to the same named on in create policy,
1765+bottom-up-parent selects the writable branch where the parent dir
1766+exists and the nearest upper one from the copyup-source,
1767+bottom-up selects the nearest upper writable branch from the
1768+copyup-source, regardless the existence of the parent dir.
1769+
1770+There are some rules or exceptions to apply these policies.
1771+- If there is a readonly branch above the policy-selected branch and
1772+ the parent dir is marked as opaque (a variation of whiteout), or the
1773+ target (creating) file is whiteout-ed on the upper readonly branch,
1774+ then the result of the policy is ignored and the target file will be
1775+ created on the nearest upper writable branch than the readonly branch.
1776+- If there is a writable branch above the policy-selected branch and
1777+ the parent dir is marked as opaque or the target file is whiteouted
1778+ on the branch, then the result of the policy is ignored and the target
1779+ file will be created on the highest one among the upper writable
1780+ branches who has diropq or whiteout. In case of whiteout, aufs removes
1781+ it as usual.
1782+- link(2) and rename(2) systemcalls are exceptions in every policy.
1783+ They try selecting the branch where the source exists as possible
1784+ since copyup a large file will take long time. If it can't be,
1785+ ie. the branch where the source exists is readonly, then they will
1786+ follow the copyup policy.
1787+- There is an exception for rename(2) when the target exists.
1788+ If the rename target exists, aufs compares the index of the branches
1789+ where the source and the target exists and selects the higher
1790+ one. If the selected branch is readonly, then aufs follows the
1791+ copyup policy.
8b6a4947 1792diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot linux/Documentation/filesystems/aufs/design/06dirren.dot
eca34b5c 1793--- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot 1970-01-01 01:00:00.000000000 +0100
594d0238 1794+++ linux/Documentation/filesystems/aufs/design/06dirren.dot 2022-11-05 23:02:18.959222617 +0100
0dcfbb52 1795@@ -0,0 +1,44 @@
8b6a4947
AM
1796+
1797+// to view this graph, run dot(1) command in GRAPHVIZ.
0dcfbb52
AM
1798+//
1799+// This program is free software; you can redistribute it and/or modify
1800+// it under the terms of the GNU General Public License as published by
1801+// the Free Software Foundation; either version 2 of the License, or
1802+// (at your option) any later version.
1803+//
1804+// This program is distributed in the hope that it will be useful,
1805+// but WITHOUT ANY WARRANTY; without even the implied warranty of
1806+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1807+// GNU General Public License for more details.
1808+//
1809+// You should have received a copy of the GNU General Public License
1810+// along with this program. If not, see <http://www.gnu.org/licenses/>.
8b6a4947
AM
1811+
1812+digraph G {
1813+node [shape=box];
1814+whinfo [label="detailed info file\n(lower_brid_root-hinum, h_inum, namelen, old name)"];
1815+
1816+node [shape=oval];
1817+
1818+aufs_rename -> whinfo [label="store/remove"];
1819+
1820+node [shape=oval];
1821+inode_list [label="h_inum list in branch\ncache"];
1822+
1823+node [shape=box];
1824+whinode [label="h_inum list file"];
1825+
1826+node [shape=oval];
1827+brmgmt [label="br_add/del/mod/umount"];
1828+
1829+brmgmt -> inode_list [label="create/remove"];
1830+brmgmt -> whinode [label="load/store"];
1831+
1832+inode_list -> whinode [style=dashed,dir=both];
1833+
1834+aufs_rename -> inode_list [label="add/del"];
1835+
1836+aufs_lookup -> inode_list [label="search"];
1837+
1838+aufs_lookup -> whinfo [label="load/remove"];
1839+}
1840diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt linux/Documentation/filesystems/aufs/design/06dirren.txt
eca34b5c 1841--- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt 1970-01-01 01:00:00.000000000 +0100
594d0238 1842+++ linux/Documentation/filesystems/aufs/design/06dirren.txt 2022-11-05 23:02:18.959222617 +0100
8b6a4947
AM
1843@@ -0,0 +1,102 @@
1844+
0dcfbb52 1845+# Copyright (C) 2017-2022 Junjiro R. Okajima
8b6a4947
AM
1846+#
1847+# This program is free software; you can redistribute it and/or modify
1848+# it under the terms of the GNU General Public License as published by
1849+# the Free Software Foundation; either version 2 of the License, or
1850+# (at your option) any later version.
1851+#
1852+# This program is distributed in the hope that it will be useful,
1853+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1854+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1855+# GNU General Public License for more details.
1856+#
1857+# You should have received a copy of the GNU General Public License
1858+# along with this program. If not, see <http://www.gnu.org/licenses/>.
1859+
1860+Special handling for renaming a directory (DIRREN)
1861+----------------------------------------------------------------------
1862+First, let's assume we have a simple usecase.
1863+
1864+- /u = /rw + /ro
1865+- /rw/dirA exists
1866+- /ro/dirA and /ro/dirA/file exist too
1867+- there is no dirB on both branches
1868+- a user issues rename("dirA", "dirB")
1869+
1870+Now, what should aufs behave against this rename(2)?
1871+There are a few possible cases.
1872+
1873+A. returns EROFS.
1874+ since dirA exists on a readonly branch which cannot be renamed.
1875+B. returns EXDEV.
1876+ it is possible to copy-up dirA (only the dir itself), but the child
1877+ entries ("file" in this case) should not be. it must be a bad
1878+ approach to copy-up recursively.
1879+C. returns a success.
1880+ even the branch /ro is readonly, aufs tries renaming it. Obviously it
1881+ is a violation of aufs' policy.
1882+D. construct an extra information which indicates that /ro/dirA should
1883+ be handled as the name of dirB.
1884+ overlayfs has a similar feature called REDIRECT.
1885+
1886+Until now, aufs implements the case B only which returns EXDEV, and
1887+expects the userspace application behaves like mv(1) which tries
1888+issueing rename(2) recursively.
1889+
1890+A new aufs feature called DIRREN is introduced which implements the case
1891+D. There are several "extra information" added.
1892+
1893+1. detailed info per renamed directory
1894+ path: /rw/dirB/$AUFS_WH_DR_INFO_PFX.<lower branch-id>
1895+2. the inode-number list of directories on a branch
1896+ path: /rw/dirB/$AUFS_WH_DR_BRHINO
1897+
1898+The filename of "detailed info per directory" represents the lower
1899+branch, and its format is
1900+- a type of the branch id
1901+ one of these.
1902+ + uuid (not implemented yet)
1903+ + fsid
1904+ + dev
1905+- the inode-number of the branch root dir
1906+
1907+And it contains these info in a single regular file.
1908+- magic number
1909+- branch's inode-number of the logically renamed dir
1910+- the name of the before-renamed dir
1911+
1912+The "detailed info per directory" file is created in aufs rename(2), and
1913+loaded in any lookup.
1914+The info is considered in lookup for the matching case only. Here
1915+"matching" means that the root of branch (in the info filename) is same
1916+to the current looking-up branch. After looking-up the before-renamed
1917+name, the inode-number is compared. And the matched dentry is used.
1918+
1919+The "inode-number list of directories" is a regular file which contains
1920+simply the inode-numbers on the branch. The file is created or updated
1921+in removing the branch, and loaded in adding the branch. Its lifetime is
1922+equal to the branch.
22319442 1923+The list is referred in lookup, and when the current target inode is
8b6a4947
AM
1924+found in the list, the aufs tries loading the "detailed info per
1925+directory" and get the changed and valid name of the dir.
1926+
1927+Theoretically these "extra informaiton" may be able to be put into XATTR
1928+in the dir inode. But aufs doesn't choose this way because
1929+1. XATTR may not be supported by the branch (or its configuration)
1930+2. XATTR may have its size limit.
1931+3. XATTR may be less easy to convert than a regular file, when the
1932+ format of the info is changed in the future.
1933+At the same time, I agree that the regular file approach is much slower
1934+than XATTR approach. So, in the future, aufs may take the XATTR or other
1935+better approach.
1936+
1937+This DIRREN feature is enabled by aufs configuration, and is activated
1938+by a new mount option.
1939+
1940+For the more complicated case, there is a work with UDBA option, which
1941+is to dected the direct access to the branches (by-passing aufs) and to
1942+maintain the cashes in aufs. Since a single cached aufs dentry may
1943+contains two names, before- and after-rename, the name comparision in
1944+UDBA handler may not work correctly. In this case, the behaviour will be
1945+equivalen to udba=reval case.
076b876e 1946diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt linux/Documentation/filesystems/aufs/design/06fhsm.txt
eca34b5c 1947--- /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt 1970-01-01 01:00:00.000000000 +0100
594d0238 1948+++ linux/Documentation/filesystems/aufs/design/06fhsm.txt 2022-11-05 23:02:18.959222617 +0100
0dcfbb52 1949@@ -0,0 +1,118 @@
076b876e 1950+
0dcfbb52
AM
1951+# Copyright (C) 2011-2022 Junjiro R. Okajima
1952+#
076b876e
AM
1953+# This program is free software; you can redistribute it and/or modify
1954+# it under the terms of the GNU General Public License as published by
1955+# the Free Software Foundation; either version 2 of the License, or
1956+# (at your option) any later version.
0dcfbb52 1957+#
076b876e
AM
1958+# This program is distributed in the hope that it will be useful,
1959+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1960+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1961+# GNU General Public License for more details.
0dcfbb52 1962+#
076b876e 1963+# You should have received a copy of the GNU General Public License
0dcfbb52 1964+# along with this program. If not, see <http://www.gnu.org/licenses/>.
076b876e
AM
1965+
1966+File-based Hierarchical Storage Management (FHSM)
1967+----------------------------------------------------------------------
1968+Hierarchical Storage Management (or HSM) is a well-known feature in the
1969+storage world. Aufs provides this feature as file-based with multiple
7e9cd9fe 1970+writable branches, based upon the principle of "Colder, the Lower".
076b876e 1971+Here the word "colder" means that the less used files, and "lower" means
7e9cd9fe 1972+that the position in the order of the stacked branches vertically.
076b876e
AM
1973+These multiple writable branches are prioritized, ie. the topmost one
1974+should be the fastest drive and be used heavily.
1975+
1976+o Characters in aufs FHSM story
1977+- aufs itself and a new branch attribute.
1978+- a new ioctl interface to move-down and to establish a connection with
1979+ the daemon ("move-down" is a converse of "copy-up").
1980+- userspace tool and daemon.
1981+
1982+The userspace daemon establishes a connection with aufs and waits for
1983+the notification. The notified information is very similar to struct
1984+statfs containing the number of consumed blocks and inodes.
1985+When the consumed blocks/inodes of a branch exceeds the user-specified
1986+upper watermark, the daemon activates its move-down process until the
1987+consumed blocks/inodes reaches the user-specified lower watermark.
1988+
1989+The actual move-down is done by aufs based upon the request from
1990+user-space since we need to maintain the inode number and the internal
1991+pointer arrays in aufs.
1992+
1993+Currently aufs FHSM handles the regular files only. Additionally they
1994+must not be hard-linked nor pseudo-linked.
1995+
1996+
1997+o Cowork of aufs and the user-space daemon
1998+ During the userspace daemon established the connection, aufs sends a
1999+ small notification to it whenever aufs writes something into the
2000+ writable branch. But it may cost high since aufs issues statfs(2)
2001+ internally. So user can specify a new option to cache the
2002+ info. Actually the notification is controlled by these factors.
2003+ + the specified cache time.
2004+ + classified as "force" by aufs internally.
2005+ Until the specified time expires, aufs doesn't send the info
2006+ except the forced cases. When aufs decide forcing, the info is always
2007+ notified to userspace.
2008+ For example, the number of free inodes is generally large enough and
2009+ the shortage of it happens rarely. So aufs doesn't force the
2010+ notification when creating a new file, directory and others. This is
2011+ the typical case which aufs doesn't force.
2012+ When aufs writes the actual filedata and the files consumes any of new
2013+ blocks, the aufs forces notifying.
2014+
2015+
2016+o Interfaces in aufs
2017+- New branch attribute.
2018+ + fhsm
2019+ Specifies that the branch is managed by FHSM feature. In other word,
2020+ participant in the FHSM.
2021+ When nofhsm is set to the branch, it will not be the source/target
2022+ branch of the move-down operation. This attribute is set
2023+ independently from coo and moo attributes, and if you want full
2024+ FHSM, you should specify them as well.
2025+- New mount option.
2026+ + fhsm_sec
2027+ Specifies a second to suppress many less important info to be
2028+ notified.
2029+- New ioctl.
2030+ + AUFS_CTL_FHSM_FD
2031+ create a new file descriptor which userspace can read the notification
2032+ (a subset of struct statfs) from aufs.
2033+- Module parameter 'brs'
2034+ It has to be set to 1. Otherwise the new mount option 'fhsm' will not
2035+ be set.
2036+- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
2037+ When there are two or more branches with fhsm attributes,
2038+ /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
2039+ terminates it. As a result of remounting and branch-manipulation, the
2040+ number of branches with fhsm attribute can be one. In this case,
2041+ /sbin/mount.aufs will terminate the user-space daemon.
2042+
2043+
2044+Finally the operation is done as these steps in kernel-space.
2045+- make sure that,
2046+ + no one else is using the file.
2047+ + the file is not hard-linked.
2048+ + the file is not pseudo-linked.
2049+ + the file is a regular file.
2050+ + the parent dir is not opaqued.
2051+- find the target writable branch.
2052+- make sure the file is not whiteout-ed by the upper (than the target)
2053+ branch.
2054+- make the parent dir on the target branch.
2055+- mutex lock the inode on the branch.
2056+- unlink the whiteout on the target branch (if exists).
2057+- lookup and create the whiteout-ed temporary name on the target branch.
2058+- copy the file as the whiteout-ed temporary name on the target branch.
2059+- rename the whiteout-ed temporary name to the original name.
2060+- unlink the file on the source branch.
2061+- maintain the internal pointer array and the external inode number
2062+ table (XINO).
2063+- maintain the timestamps and other attributes of the parent dir and the
2064+ file.
2065+
2066+And of course, in every step, an error may happen. So the operation
2067+should restore the original file state after an error happens.
53392da6 2068diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
eca34b5c 2069--- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt 1970-01-01 01:00:00.000000000 +0100
594d0238 2070+++ linux/Documentation/filesystems/aufs/design/06mmap.txt 2022-11-05 23:02:18.959222617 +0100
b912730e 2071@@ -0,0 +1,72 @@
53392da6 2072+
0dcfbb52
AM
2073+# Copyright (C) 2005-2022 Junjiro R. Okajima
2074+#
53392da6
AM
2075+# This program is free software; you can redistribute it and/or modify
2076+# it under the terms of the GNU General Public License as published by
2077+# the Free Software Foundation; either version 2 of the License, or
2078+# (at your option) any later version.
0dcfbb52 2079+#
53392da6
AM
2080+# This program is distributed in the hope that it will be useful,
2081+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2082+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2083+# GNU General Public License for more details.
0dcfbb52 2084+#
53392da6 2085+# You should have received a copy of the GNU General Public License
523b37e3 2086+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2087+
2088+mmap(2) -- File Memory Mapping
2089+----------------------------------------------------------------------
2090+In aufs, the file-mapped pages are handled by a branch fs directly, no
2091+interaction with aufs. It means aufs_mmap() calls the branch fs's
2092+->mmap().
2093+This approach is simple and good, but there is one problem.
7e9cd9fe 2094+Under /proc, several entries show the mmapped files by its path (with
53392da6
AM
2095+device and inode number), and the printed path will be the path on the
2096+branch fs's instead of virtual aufs's.
2097+This is not a problem in most cases, but some utilities lsof(1) (and its
2098+user) may expect the path on aufs.
2099+
2100+To address this issue, aufs adds a new member called vm_prfile in struct
2101+vm_area_struct (and struct vm_region). The original vm_file points to
2102+the file on the branch fs in order to handle everything correctly as
2103+usual. The new vm_prfile points to a virtual file in aufs, and the
2104+show-functions in procfs refers to vm_prfile if it is set.
2105+Also we need to maintain several other places where touching vm_file
2106+such like
2107+- fork()/clone() copies vma and the reference count of vm_file is
2108+ incremented.
2109+- merging vma maintains the ref count too.
2110+
7e9cd9fe 2111+This is not a good approach. It just fakes the printed path. But it
53392da6
AM
2112+leaves all behaviour around f_mapping unchanged. This is surely an
2113+advantage.
2114+Actually aufs had adopted another complicated approach which calls
2115+generic_file_mmap() and handles struct vm_operations_struct. In this
2116+approach, aufs met a hard problem and I could not solve it without
2117+switching the approach.
b912730e
AM
2118+
2119+There may be one more another approach which is
2120+- bind-mount the branch-root onto the aufs-root internally
2121+- grab the new vfsmount (ie. struct mount)
2122+- lazy-umount the branch-root internally
2123+- in open(2) the aufs-file, open the branch-file with the hidden
2124+ vfsmount (instead of the original branch's vfsmount)
2125+- ideally this "bind-mount and lazy-umount" should be done atomically,
2126+ but it may be possible from userspace by the mount helper.
2127+
2128+Adding the internal hidden vfsmount and using it in opening a file, the
2129+file path under /proc will be printed correctly. This approach looks
2130+smarter, but is not possible I am afraid.
2131+- aufs-root may be bind-mount later. when it happens, another hidden
2132+ vfsmount will be required.
2133+- it is hard to get the chance to bind-mount and lazy-umount
2134+ + in kernel-space, FS can have vfsmount in open(2) via
2135+ file->f_path, and aufs can know its vfsmount. But several locks are
2136+ already acquired, and if aufs tries to bind-mount and lazy-umount
2137+ here, then it may cause a deadlock.
2138+ + in user-space, bind-mount doesn't invoke the mount helper.
2139+- since /proc shows dev and ino, aufs has to give vma these info. it
2140+ means a new member vm_prinode will be necessary. this is essentially
2141+ equivalent to vm_prfile described above.
2142+
2143+I have to give up this "looks-smater" approach.
c1595e42 2144diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt linux/Documentation/filesystems/aufs/design/06xattr.txt
eca34b5c 2145--- /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt 1970-01-01 01:00:00.000000000 +0100
594d0238 2146+++ linux/Documentation/filesystems/aufs/design/06xattr.txt 2022-11-05 23:02:18.959222617 +0100
0dcfbb52 2147@@ -0,0 +1,94 @@
c1595e42 2148+
0dcfbb52 2149+# Copyright (C) 2014-2022 Junjiro R. Okajima
c1595e42
JR
2150+#
2151+# This program is free software; you can redistribute it and/or modify
2152+# it under the terms of the GNU General Public License as published by
2153+# the Free Software Foundation; either version 2 of the License, or
2154+# (at your option) any later version.
2155+#
2156+# This program is distributed in the hope that it will be useful,
2157+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2158+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2159+# GNU General Public License for more details.
2160+#
2161+# You should have received a copy of the GNU General Public License
0dcfbb52 2162+# along with this program. If not, see <http://www.gnu.org/licenses/>.
c1595e42
JR
2163+
2164+Listing XATTR/EA and getting the value
2165+----------------------------------------------------------------------
2166+For the inode standard attributes (owner, group, timestamps, etc.), aufs
2167+shows the values from the topmost existing file. This behaviour is good
7e9cd9fe 2168+for the non-dir entries since the bahaviour exactly matches the shown
c1595e42
JR
2169+information. But for the directories, aufs considers all the same named
2170+entries on the lower branches. Which means, if one of the lower entry
2171+rejects readdir call, then aufs returns an error even if the topmost
2172+entry allows it. This behaviour is necessary to respect the branch fs's
2173+security, but can make users confused since the user-visible standard
2174+attributes don't match the behaviour.
2175+To address this issue, aufs has a mount option called dirperm1 which
2176+checks the permission for the topmost entry only, and ignores the lower
2177+entry's permission.
2178+
2179+A similar issue can happen around XATTR.
2180+getxattr(2) and listxattr(2) families behave as if dirperm1 option is
7e9cd9fe
AM
2181+always set. Otherwise these very unpleasant situation would happen.
2182+- listxattr(2) may return the duplicated entries.
c1595e42
JR
2183+- users may not be able to remove or reset the XATTR forever,
2184+
2185+
2186+XATTR/EA support in the internal (copy,move)-(up,down)
2187+----------------------------------------------------------------------
7e9cd9fe 2188+Generally the extended attributes of inode are categorized as these.
c1595e42
JR
2189+- "security" for LSM and capability.
2190+- "system" for posix ACL, 'acl' mount option is required for the branch
2191+ fs generally.
2192+- "trusted" for userspace, CAP_SYS_ADMIN is required.
2193+- "user" for userspace, 'user_xattr' mount option is required for the
2194+ branch fs generally.
2195+
2196+Moreover there are some other categories. Aufs handles these rather
2197+unpopular categories as the ordinary ones, ie. there is no special
2198+condition nor exception.
2199+
2200+In copy-up, the support for XATTR on the dst branch may differ from the
2201+src branch. In this case, the copy-up operation will get an error and
7e9cd9fe
AM
2202+the original user operation which triggered the copy-up will fail. It
2203+can happen that even all copy-up will fail.
c1595e42
JR
2204+When both of src and dst branches support XATTR and if an error occurs
2205+during copying XATTR, then the copy-up should fail obviously. That is a
2206+good reason and aufs should return an error to userspace. But when only
7e9cd9fe 2207+the src branch support that XATTR, aufs should not return an error.
c1595e42
JR
2208+For example, the src branch supports ACL but the dst branch doesn't
2209+because the dst branch may natively un-support it or temporary
2210+un-support it due to "noacl" mount option. Of course, the dst branch fs
2211+may NOT return an error even if the XATTR is not supported. It is
2212+totally up to the branch fs.
2213+
2214+Anyway when the aufs internal copy-up gets an error from the dst branch
2215+fs, then aufs tries removing the just copied entry and returns the error
2216+to the userspace. The worst case of this situation will be all copy-up
2217+will fail.
2218+
2219+For the copy-up operation, there two basic approaches.
2220+- copy the specified XATTR only (by category above), and return the
7e9cd9fe 2221+ error unconditionally if it happens.
c1595e42
JR
2222+- copy all XATTR, and ignore the error on the specified category only.
2223+
2224+In order to support XATTR and to implement the correct behaviour, aufs
7e9cd9fe
AM
2225+chooses the latter approach and introduces some new branch attributes,
2226+"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
c1595e42 2227+They correspond to the XATTR namespaces (see above). Additionally, to be
7e9cd9fe
AM
2228+convenient, "icex" is also provided which means all "icex*" attributes
2229+are set (here the word "icex" stands for "ignore copy-error on XATTR").
c1595e42
JR
2230+
2231+The meaning of these attributes is to ignore the error from setting
2232+XATTR on that branch.
2233+Note that aufs tries copying all XATTR unconditionally, and ignores the
2234+error from the dst branch according to the specified attributes.
2235+
2236+Some XATTR may have its default value. The default value may come from
2237+the parent dir or the environment. If the default value is set at the
2238+file creating-time, it will be overwritten by copy-up.
2239+Some contradiction may happen I am afraid.
2240+Do we need another attribute to stop copying XATTR? I am unsure. For
2241+now, aufs implements the branch attributes to ignore the error.
53392da6 2242diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
eca34b5c 2243--- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
594d0238 2244+++ linux/Documentation/filesystems/aufs/design/07export.txt 2022-11-05 23:02:18.959222617 +0100
523b37e3 2245@@ -0,0 +1,58 @@
53392da6 2246+
0dcfbb52
AM
2247+# Copyright (C) 2005-2022 Junjiro R. Okajima
2248+#
53392da6
AM
2249+# This program is free software; you can redistribute it and/or modify
2250+# it under the terms of the GNU General Public License as published by
2251+# the Free Software Foundation; either version 2 of the License, or
2252+# (at your option) any later version.
0dcfbb52 2253+#
53392da6
AM
2254+# This program is distributed in the hope that it will be useful,
2255+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2256+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2257+# GNU General Public License for more details.
0dcfbb52 2258+#
53392da6 2259+# You should have received a copy of the GNU General Public License
523b37e3 2260+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2261+
2262+Export Aufs via NFS
2263+----------------------------------------------------------------------
2264+Here is an approach.
2265+- like xino/xib, add a new file 'xigen' which stores aufs inode
2266+ generation.
2267+- iget_locked(): initialize aufs inode generation for a new inode, and
2268+ store it in xigen file.
2269+- destroy_inode(): increment aufs inode generation and store it in xigen
2270+ file. it is necessary even if it is not unlinked, because any data of
2271+ inode may be changed by UDBA.
2272+- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2273+ build file handle by
2274+ + branch id (4 bytes)
2275+ + superblock generation (4 bytes)
2276+ + inode number (4 or 8 bytes)
2277+ + parent dir inode number (4 or 8 bytes)
2278+ + inode generation (4 bytes))
2279+ + return value of exportfs_encode_fh() for the parent on a branch (4
2280+ bytes)
2281+ + file handle for a branch (by exportfs_encode_fh())
2282+- fh_to_dentry():
2283+ + find the index of a branch from its id in handle, and check it is
2284+ still exist in aufs.
2285+ + 1st level: get the inode number from handle and search it in cache.
7e9cd9fe
AM
2286+ + 2nd level: if not found in cache, get the parent inode number from
2287+ the handle and search it in cache. and then open the found parent
2288+ dir, find the matching inode number by vfs_readdir() and get its
2289+ name, and call lookup_one_len() for the target dentry.
53392da6
AM
2290+ + 3rd level: if the parent dir is not cached, call
2291+ exportfs_decode_fh() for a branch and get the parent on a branch,
2292+ build a pathname of it, convert it a pathname in aufs, call
2293+ path_lookup(). now aufs gets a parent dir dentry, then handle it as
2294+ the 2nd level.
2295+ + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2296+ for every branch, but not itself. to get this, (currently) aufs
2297+ searches in current->nsproxy->mnt_ns list. it may not be a good
2298+ idea, but I didn't get other approach.
2299+ + test the generation of the gotten inode.
2300+- every inode operation: they may get EBUSY due to UDBA. in this case,
2301+ convert it into ESTALE for NFSD.
2302+- readdir(): call lockdep_on/off() because filldir in NFSD calls
2303+ lookup_one_len(), vfs_getattr(), encode_fh() and others.
2304diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
eca34b5c 2305--- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt 1970-01-01 01:00:00.000000000 +0100
594d0238 2306+++ linux/Documentation/filesystems/aufs/design/08shwh.txt 2022-11-05 23:02:18.959222617 +0100
523b37e3 2307@@ -0,0 +1,52 @@
53392da6 2308+
0dcfbb52
AM
2309+# Copyright (C) 2005-2022 Junjiro R. Okajima
2310+#
53392da6
AM
2311+# This program is free software; you can redistribute it and/or modify
2312+# it under the terms of the GNU General Public License as published by
2313+# the Free Software Foundation; either version 2 of the License, or
2314+# (at your option) any later version.
0dcfbb52 2315+#
53392da6
AM
2316+# This program is distributed in the hope that it will be useful,
2317+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2318+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2319+# GNU General Public License for more details.
0dcfbb52 2320+#
53392da6 2321+# You should have received a copy of the GNU General Public License
523b37e3 2322+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2323+
2324+Show Whiteout Mode (shwh)
2325+----------------------------------------------------------------------
2326+Generally aufs hides the name of whiteouts. But in some cases, to show
2327+them is very useful for users. For instance, creating a new middle layer
2328+(branch) by merging existing layers.
2329+
2330+(borrowing aufs1 HOW-TO from a user, Michael Towers)
2331+When you have three branches,
2332+- Bottom: 'system', squashfs (underlying base system), read-only
2333+- Middle: 'mods', squashfs, read-only
2334+- Top: 'overlay', ram (tmpfs), read-write
2335+
2336+The top layer is loaded at boot time and saved at shutdown, to preserve
2337+the changes made to the system during the session.
2338+When larger changes have been made, or smaller changes have accumulated,
2339+the size of the saved top layer data grows. At this point, it would be
2340+nice to be able to merge the two overlay branches ('mods' and 'overlay')
2341+and rewrite the 'mods' squashfs, clearing the top layer and thus
2342+restoring save and load speed.
2343+
2344+This merging is simplified by the use of another aufs mount, of just the
2345+two overlay branches using the 'shwh' option.
2346+# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2347+ aufs /livesys/merge_union
2348+
2349+A merged view of these two branches is then available at
2350+/livesys/merge_union, and the new feature is that the whiteouts are
2351+visible!
2352+Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2353+writing to all branches. Also the default mode for all branches is 'ro'.
2354+It is now possible to save the combined contents of the two overlay
2355+branches to a new squashfs, e.g.:
2356+# mksquashfs /livesys/merge_union /path/to/newmods.squash
2357+
2358+This new squashfs archive can be stored on the boot device and the
2359+initramfs will use it to replace the old one at the next boot.
2360diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
eca34b5c 2361--- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt 1970-01-01 01:00:00.000000000 +0100
594d0238 2362+++ linux/Documentation/filesystems/aufs/design/10dynop.txt 2022-11-05 23:02:18.959222617 +0100
7e9cd9fe 2363@@ -0,0 +1,47 @@
53392da6 2364+
0dcfbb52 2365+# Copyright (C) 2010-2022 Junjiro R. Okajima
53392da6
AM
2366+#
2367+# This program is free software; you can redistribute it and/or modify
2368+# it under the terms of the GNU General Public License as published by
2369+# the Free Software Foundation; either version 2 of the License, or
2370+# (at your option) any later version.
2371+#
2372+# This program is distributed in the hope that it will be useful,
2373+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2374+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2375+# GNU General Public License for more details.
2376+#
2377+# You should have received a copy of the GNU General Public License
523b37e3 2378+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2379+
2380+Dynamically customizable FS operations
2381+----------------------------------------------------------------------
2382+Generally FS operations (struct inode_operations, struct
2383+address_space_operations, struct file_operations, etc.) are defined as
2384+"static const", but it never means that FS have only one set of
2385+operation. Some FS have multiple sets of them. For instance, ext2 has
2386+three sets, one for XIP, for NOBH, and for normal.
2387+Since aufs overrides and redirects these operations, sometimes aufs has
7e9cd9fe 2388+to change its behaviour according to the branch FS type. More importantly
53392da6
AM
2389+VFS acts differently if a function (member in the struct) is set or
2390+not. It means aufs should have several sets of operations and select one
2391+among them according to the branch FS definition.
2392+
7e9cd9fe 2393+In order to solve this problem and not to affect the behaviour of VFS,
53392da6 2394+aufs defines these operations dynamically. For instance, aufs defines
7e9cd9fe
AM
2395+dummy direct_IO function for struct address_space_operations, but it may
2396+not be set to the address_space_operations actually. When the branch FS
2397+doesn't have it, aufs doesn't set it to its address_space_operations
2398+while the function definition itself is still alive. So the behaviour
2399+itself will not change, and it will return an error when direct_IO is
2400+not set.
53392da6
AM
2401+
2402+The lifetime of these dynamically generated operation object is
2403+maintained by aufs branch object. When the branch is removed from aufs,
2404+the reference counter of the object is decremented. When it reaches
2405+zero, the dynamically generated operation object will be freed.
2406+
7e9cd9fe
AM
2407+This approach is designed to support AIO (io_submit), Direct I/O and
2408+XIP (DAX) mainly.
2409+Currently this approach is applied to address_space_operations for
2410+regular files only.
53392da6 2411diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
eca34b5c 2412--- /usr/share/empty/Documentation/filesystems/aufs/README 1970-01-01 01:00:00.000000000 +0100
171c7011 2413+++ linux/Documentation/filesystems/aufs/README 2023-09-02 12:00:06.376642958 +0200
5d4d15ba 2414@@ -0,0 +1,409 @@
53392da6 2415+
0dcfbb52 2416+Aufs6 -- advanced multi layered unification filesystem version 6.x
53392da6
AM
2417+http://aufs.sf.net
2418+Junjiro R. Okajima
2419+
2420+
2421+0. Introduction
2422+----------------------------------------
2423+In the early days, aufs was entirely re-designed and re-implemented
7e9cd9fe 2424+Unionfs Version 1.x series. Adding many original ideas, approaches,
fbc438ed 2425+improvements and implementations, it became totally different from
53392da6 2426+Unionfs while keeping the basic features.
fbc438ed 2427+Later, Unionfs Version 2.x series began taking some of the same
53392da6 2428+approaches to aufs1's.
fbc438ed 2429+Unionfs was being developed by Professor Erez Zadok at Stony Brook
53392da6
AM
2430+University and his team.
2431+
0dcfbb52
AM
2432+Aufs6 supports linux-v6.0 and later, try aufs6.0 branch in
2433+aufs-linux.git or aufs-standalone.git.
2434+If you want older kernel version support,
2435+- for linux-v5.x series, try aufs-linux.git or aufs-standalone.git
fbc438ed
JR
2436+- for linux-v4.x series, try aufs4-linux.git or aufs4-standalone.git
2437+- for linux-v3.x series, try aufs3-linux.git or aufs3-standalone.git
2438+- for linux-v2.6.16 and later, try aufs2-2.6.git, aufs2-standalone.git
2439+ or aufs1 from CVS on SourceForge.
53392da6 2440+
0dcfbb52
AM
2441+Note: the name of aufs5-linux.git and aufs5-standalone.git on github
2442+ were changed. Now they are aufs-linux.git and
2443+ aufs-standalone.git and they contain aufs5 and later branches.
2444+
53392da6 2445+Note: it becomes clear that "Aufs was rejected. Let's give it up."
38d290e6
JR
2446+ According to Christoph Hellwig, linux rejects all union-type
2447+ filesystems but UnionMount.
53392da6
AM
2448+<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
2449+
38d290e6
JR
2450+PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
2451+ UnionMount, and he pointed out an issue around a directory mutex
2452+ lock and aufs addressed it. But it is still unsure whether aufs will
2453+ be merged (or any other union solution).
076b876e 2454+<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
38d290e6 2455+
53392da6
AM
2456+
2457+1. Features
2458+----------------------------------------
2459+- unite several directories into a single virtual filesystem. The member
2460+ directory is called as a branch.
2461+- you can specify the permission flags to the branch, which are 'readonly',
2462+ 'readwrite' and 'whiteout-able.'
2463+- by upper writable branch, internal copyup and whiteout, files/dirs on
2464+ readonly branch are modifiable logically.
2465+- dynamic branch manipulation, add, del.
2466+- etc...
2467+
7e9cd9fe
AM
2468+Also there are many enhancements in aufs, such as:
2469+- test only the highest one for the directory permission (dirperm1)
2470+- copyup on open (coo=)
2471+- 'move' policy for copy-up between two writable branches, after
2472+ checking free space.
2473+- xattr, acl
53392da6
AM
2474+- readdir(3) in userspace.
2475+- keep inode number by external inode number table
2476+- keep the timestamps of file/dir in internal copyup operation
2477+- seekable directory, supporting NFS readdir.
2478+- whiteout is hardlinked in order to reduce the consumption of inodes
2479+ on branch
2480+- do not copyup, nor create a whiteout when it is unnecessary
2481+- revert a single systemcall when an error occurs in aufs
2482+- remount interface instead of ioctl
2483+- maintain /etc/mtab by an external command, /sbin/mount.aufs.
2484+- loopback mounted filesystem as a branch
2485+- kernel thread for removing the dir who has a plenty of whiteouts
2486+- support copyup sparse file (a file which has a 'hole' in it)
2487+- default permission flags for branches
2488+- selectable permission flags for ro branch, whether whiteout can
2489+ exist or not
2490+- export via NFS.
2491+- support <sysfs>/fs/aufs and <debugfs>/aufs.
2492+- support multiple writable branches, some policies to select one
2493+ among multiple writable branches.
2494+- a new semantics for link(2) and rename(2) to support multiple
2495+ writable branches.
2496+- no glibc changes are required.
2497+- pseudo hardlink (hardlink over branches)
2498+- allow a direct access manually to a file on branch, e.g. bypassing aufs.
2499+ including NFS or remote filesystem branch.
2500+- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
2501+- and more...
2502+
0dcfbb52 2503+Currently these features are dropped temporary from aufs6.
53392da6 2504+See design/08plan.txt in detail.
53392da6
AM
2505+- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
2506+ (robr)
2507+- statistics of aufs thread (/sys/fs/aufs/stat)
53392da6
AM
2508+
2509+Features or just an idea in the future (see also design/*.txt),
2510+- reorder the branch index without del/re-add.
2511+- permanent xino files for NFSD
2512+- an option for refreshing the opened files after add/del branches
53392da6
AM
2513+- light version, without branch manipulation. (unnecessary?)
2514+- copyup in userspace
2515+- inotify in userspace
2516+- readv/writev
53392da6
AM
2517+
2518+
2519+2. Download
2520+----------------------------------------
aec24f17
AM
2521+There are three GIT trees for aufs6, aufs-linux.git,
2522+aufs-standalone.git, and aufs-util.git.
2523+While the aufs-util is always necessary, you need either of aufs-linux
2524+or aufs-standalone.
1e00d052 2525+
aec24f17 2526+The aufs-linux tree includes the whole linux mainline GIT tree,
1e00d052
AM
2527+git://git.kernel.org/.../torvalds/linux.git.
2528+And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
0dcfbb52 2529+build aufs6 as an external kernel module.
2000de60 2530+Several extra patches are not included in this tree. Only
aec24f17 2531+aufs-standalone tree contains them. They are described in the later
2000de60 2532+section "Configuration and Compilation."
1e00d052 2533+
aec24f17 2534+On the other hand, the aufs-standalone tree has only aufs source files
53392da6 2535+and necessary patches, and you can select CONFIG_AUFS_FS=m.
2000de60 2536+But you need to apply all aufs patches manually.
53392da6 2537+
0dcfbb52
AM
2538+You will find GIT branches whose name is in form of "aufs6.x" where "x"
2539+represents the linux kernel version, "linux-6.x". For instance,
2540+"aufs6.0" is for linux-6.0. For latest "linux-6.x-rcN", use
2541+"aufs6.x-rcN" branch.
1e00d052 2542+
aec24f17 2543+o aufs-linux tree
1e00d052 2544+$ git clone --reference /your/linux/git/tree \
aec24f17 2545+ git://github.com/sfjro/aufs-linux.git aufs-linux.git
1e00d052 2546+- if you don't have linux GIT tree, then remove "--reference ..."
aec24f17 2547+$ cd aufs-linux.git
0dcfbb52 2548+$ git checkout origin/aufs6.0
53392da6 2549+
2000de60
JR
2550+Or You may want to directly git-pull aufs into your linux GIT tree, and
2551+leave the patch-work to GIT.
2552+$ cd /your/linux/git/tree
aec24f17
AM
2553+$ git remote add aufs git://github.com/sfjro/aufs-linux.git
2554+$ git fetch aufs
0dcfbb52 2555+$ git checkout -b my6.0 v6.0
5527c038 2556+$ (add your local change...)
aec24f17 2557+$ git pull aufs aufs6.0
0dcfbb52 2558+- now you have v6.0 + your_changes + aufs6.0 in you my6.0 branch.
2000de60 2559+- you may need to solve some conflicts between your_changes and
0dcfbb52
AM
2560+ aufs6.0. in this case, git-rerere is recommended so that you can
2561+ solve the similar conflicts automatically when you upgrade to 6.1 or
2000de60
JR
2562+ later in the future.
2563+
aec24f17
AM
2564+o aufs-standalone tree
2565+$ git clone git://github.com/sfjro/aufs-standalone.git aufs-standalone.git
2566+$ cd aufs-standalone.git
0dcfbb52 2567+$ git checkout origin/aufs6.0
53392da6
AM
2568+
2569+o aufs-util tree
5527c038
JR
2570+$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
2571+- note that the public aufs-util.git is on SourceForge instead of
2572+ GitHUB.
53392da6 2573+$ cd aufs-util.git
0dcfbb52 2574+$ git checkout origin/aufs6.0
53392da6 2575+
0dcfbb52
AM
2576+Note: The 6.x-rcN branch is to be used with `rc' kernel versions ONLY.
2577+The minor version number, 'x' in '6.x', of aufs may not always
9dbd164d
AM
2578+follow the minor version number of the kernel.
2579+Because changes in the kernel that cause the use of a new
2580+minor version number do not always require changes to aufs-util.
2581+
2582+Since aufs-util has its own minor version number, you may not be
2583+able to find a GIT branch in aufs-util for your kernel's
2584+exact minor version number.
2585+In this case, you should git-checkout the branch for the
53392da6 2586+nearest lower number.
9dbd164d
AM
2587+
2588+For (an unreleased) example:
0dcfbb52
AM
2589+If you are using "linux-6.10" and the "aufs6.10" branch
2590+does not exist in aufs-util repository, then "aufs6.9", "aufs6.8"
9dbd164d
AM
2591+or something numerically smaller is the branch for your kernel.
2592+
53392da6
AM
2593+Also you can view all branches by
2594+ $ git branch -a
2595+
2596+
2597+3. Configuration and Compilation
2598+----------------------------------------
2599+Make sure you have git-checkout'ed the correct branch.
2600+
aec24f17 2601+For aufs-linux tree,
c06a8ce3 2602+- enable CONFIG_AUFS_FS.
1e00d052 2603+- set other aufs configurations if necessary.
46016270
AM
2604+- for aufs5.13 and later
2605+ Because aufs is not only an ordinary filesystem (callee of VFS), but
2606+ also a caller of VFS functions for branch filesystems, subclassing of
2607+ the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
2608+ feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
2609+ need to customize some LOCKDEP numbers. Here are what I use on my
2610+ test environment.
2611+ CONFIG_LOCKDEP_BITS=21
2612+ CONFIG_LOCKDEP_CHAINS_BITS=21
2613+ CONFIG_LOCKDEP_STACK_TRACE_BITS=24
aec24f17
AM
2614+ Also you will need to expand some constant values in LOCKDEP. Refer
2615+ to lockdep-debug.patch in aufs-standalone.git.
1e00d052 2616+
aec24f17 2617+For aufs-standalone tree,
53392da6
AM
2618+There are several ways to build.
2619+
2620+1.
0dcfbb52
AM
2621+- apply ./aufs6-kbuild.patch to your kernel source files.
2622+- apply ./aufs6-base.patch too.
2623+- apply ./aufs6-mmap.patch too.
2624+- apply ./aufs6-standalone.patch too, if you have a plan to set
aec24f17 2625+ CONFIG_AUFS_FS=m. otherwise you don't need ./aufs-standalone.patch.
537831f9
AM
2626+- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2627+ kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
c06a8ce3 2628+- enable CONFIG_AUFS_FS, you can select either
53392da6
AM
2629+ =m or =y.
2630+- and build your kernel as usual.
2631+- install the built kernel.
7eafdf33
AM
2632+- install the header files too by "make headers_install" to the
2633+ directory where you specify. By default, it is $PWD/usr.
b4510431 2634+ "make help" shows a brief note for headers_install.
53392da6
AM
2635+- and reboot your system.
2636+
2637+2.
2638+- module only (CONFIG_AUFS_FS=m).
0dcfbb52
AM
2639+- apply ./aufs6-base.patch to your kernel source files.
2640+- apply ./aufs6-mmap.patch too.
2641+- apply ./aufs6-standalone.patch too.
53392da6
AM
2642+- build your kernel, don't forget "make headers_install", and reboot.
2643+- edit ./config.mk and set other aufs configurations if necessary.
b4510431 2644+ Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
53392da6
AM
2645+ every aufs configurations.
2646+- build the module by simple "make".
2647+- you can specify ${KDIR} make variable which points to your kernel
2648+ source tree.
2649+- install the files
2650+ + run "make install" to install the aufs module, or copy the built
b4510431
AM
2651+ $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2652+ + run "make install_headers" (instead of headers_install) to install
2653+ the modified aufs header file (you can specify DESTDIR which is
2654+ available in aufs standalone version's Makefile only), or copy
2655+ $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2656+ you like manually. By default, the target directory is $PWD/usr.
0dcfbb52 2657+- no need to apply aufs6-kbuild.patch, nor copying source files to your
53392da6
AM
2658+ kernel source tree.
2659+
b4510431 2660+Note: The header file aufs_type.h is necessary to build aufs-util
53392da6
AM
2661+ as well as "make headers_install" in the kernel source tree.
2662+ headers_install is subject to be forgotten, but it is essentially
2663+ necessary, not only for building aufs-util.
2664+ You may not meet problems without headers_install in some older
2665+ version though.
2666+
2667+And then,
2668+- read README in aufs-util, build and install it
9dbd164d
AM
2669+- note that your distribution may contain an obsoleted version of
2670+ aufs_type.h in /usr/include/linux or something. When you build aufs
2671+ utilities, make sure that your compiler refers the correct aufs header
2672+ file which is built by "make headers_install."
53392da6
AM
2673+- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2674+ then run "make install_ulib" too. And refer to the aufs manual in
2675+ detail.
2676+
aec24f17 2677+There several other patches in aufs-standalone.git. They are all
38d290e6 2678+optional. When you meet some problems, they will help you.
0dcfbb52 2679+- aufs6-loopback.patch
38d290e6
JR
2680+ Supports a nested loopback mount in a branch-fs. This patch is
2681+ unnecessary until aufs produces a message like "you may want to try
2682+ another patch for loopback file".
2683+- vfs-ino.patch
2684+ Modifies a system global kernel internal function get_next_ino() in
2685+ order to stop assigning 0 for an inode-number. Not directly related to
2686+ aufs, but recommended generally.
2687+- tmpfs-idr.patch
2688+ Keeps the tmpfs inode number as the lowest value. Effective to reduce
2689+ the size of aufs XINO files for tmpfs branch. Also it prevents the
2690+ duplication of inode number, which is important for backup tools and
2691+ other utilities. When you find aufs XINO files for tmpfs branch
2692+ growing too much, try this patch.
aec24f17
AM
2693+- lockdep-debug.patch
2694+ Similar to some kernel configurations for LOCKDEP (see the top of
2695+ this section), you will need expand some constants in LOCKDEP for
2696+ aufs if you enable CONFIG_LOCKDEP.
38d290e6 2697+
53392da6
AM
2698+
2699+4. Usage
2700+----------------------------------------
2701+At first, make sure aufs-util are installed, and please read the aufs
aec24f17
AM
2702+manual, aufs.5 in aufs-util.git tree.
2703+$ man -l aufs.5
53392da6
AM
2704+
2705+And then,
2706+$ mkdir /tmp/rw /tmp/aufs
2707+# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2708+
2709+Here is another example. The result is equivalent.
2710+# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2711+ Or
2712+# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2713+# mount -o remount,append:${HOME} /tmp/aufs
2714+
2715+Then, you can see whole tree of your home dir through /tmp/aufs. If
2716+you modify a file under /tmp/aufs, the one on your home directory is
2717+not affected, instead the same named file will be newly created under
2718+/tmp/rw. And all of your modification to a file will be applied to
2719+the one under /tmp/rw. This is called the file based Copy on Write
2720+(COW) method.
aec24f17 2721+Aufs mount options are described in aufs.5.
53392da6
AM
2722+If you run chroot or something and make your aufs as a root directory,
2723+then you need to customize the shutdown script. See the aufs manual in
2724+detail.
2725+
2726+Additionally, there are some sample usages of aufs which are a
2727+diskless system with network booting, and LiveCD over NFS.
2728+See sample dir in CVS tree on SourceForge.
2729+
2730+
2731+5. Contact
2732+----------------------------------------
2733+When you have any problems or strange behaviour in aufs, please let me
2734+know with:
2735+- /proc/mounts (instead of the output of mount(8))
2736+- /sys/module/aufs/*
2737+- /sys/fs/aufs/* (if you have them)
2738+- /debug/aufs/* (if you have them)
2739+- linux kernel version
2740+ if your kernel is not plain, for example modified by distributor,
2741+ the url where i can download its source is necessary too.
2742+- aufs version which was printed at loading the module or booting the
2743+ system, instead of the date you downloaded.
2744+- configuration (define/undefine CONFIG_AUFS_xxx)
2745+- kernel configuration or /proc/config.gz (if you have it)
43982f53 2746+- LSM (linux security module, if you are using)
53392da6
AM
2747+- behaviour which you think to be incorrect
2748+- actual operation, reproducible one is better
2749+- mailto: aufs-users at lists.sourceforge.net
2750+
2751+Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
2752+and Feature Requests) on SourceForge. Please join and write to
2753+aufs-users ML.
2754+
2755+
2756+6. Acknowledgements
2757+----------------------------------------
2758+Thanks to everyone who have tried and are using aufs, whoever
2759+have reported a bug or any feedback.
2760+
2761+Especially donators:
2762+Tomas Matejicek(slax.org) made a donation (much more than once).
2763+ Since Apr 2010, Tomas M (the author of Slax and Linux Live
2764+ scripts) is making "doubling" donations.
2765+ Unfortunately I cannot list all of the donators, but I really
b4510431 2766+ appreciate.
53392da6
AM
2767+ It ends Aug 2010, but the ordinary donation URL is still available.
2768+ <http://sourceforge.net/donate/index.php?group_id=167503>
2769+Dai Itasaka made a donation (2007/8).
2770+Chuck Smith made a donation (2008/4, 10 and 12).
2771+Henk Schoneveld made a donation (2008/9).
2772+Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
2773+Francois Dupoux made a donation (2008/11).
2774+Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
2775+ aufs2 GIT tree (2009/2).
2776+William Grant made a donation (2009/3).
2777+Patrick Lane made a donation (2009/4).
2778+The Mail Archive (mail-archive.com) made donations (2009/5).
2779+Nippy Networks (Ed Wildgoose) made a donation (2009/7).
2780+New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
2781+Pavel Pronskiy made a donation (2011/2).
2782+Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
2783+ Networks (Ed Wildgoose) made a donation for hardware (2011/3).
537831f9
AM
2784+Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
2785+11).
1e00d052 2786+Sam Liddicott made a donation (2011/9).
86dc4139
AM
2787+Era Scarecrow made a donation (2013/4).
2788+Bor Ratajc made a donation (2013/4).
2789+Alessandro Gorreta made a donation (2013/4).
2790+POIRETTE Marc made a donation (2013/4).
2791+Alessandro Gorreta made a donation (2013/4).
2792+lauri kasvandik made a donation (2013/5).
392086de 2793+"pemasu from Finland" made a donation (2013/7).
523b37e3
AM
2794+The Parted Magic Project made a donation (2013/9 and 11).
2795+Pavel Barta made a donation (2013/10).
38d290e6 2796+Nikolay Pertsev made a donation (2014/5).
f4d37d76 2797+James B made a donation (2014/7, 2015/7, and 2021/12).
076b876e 2798+Stefano Di Biase made a donation (2014/8).
2000de60 2799+Daniel Epellei made a donation (2015/1).
c4adf169 2800+OmegaPhil made a donation (2016/1, 2018/4).
5afbbe0d 2801+Tomasz Szewczyk made a donation (2016/4).
f2c43d5f 2802+James Burry made a donation (2016/12).
acd2b654 2803+Carsten Rose made a donation (2018/9).
9f237c51 2804+Porteus Kiosk made a donation (2018/10).
5d4d15ba
AM
2805+huronOS team: Enya Quetzalli made donations (2022/5, 2023/5 and 8).
2806+Vasily Mikhaylichenko made a donation (2023/5).
53392da6
AM
2807+
2808+Thank you very much.
2809+Donations are always, including future donations, very important and
2810+helpful for me to keep on developing aufs.
2811+
2812+
2813+7.
2814+----------------------------------------
2815+If you are an experienced user, no explanation is needed. Aufs is
2816+just a linux filesystem.
2817+
2818+
2819+Enjoy!
2820+
2821+# Local variables: ;
2822+# mode: text;
2823+# End: ;
7f207e10 2824diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
eca34b5c 2825--- /usr/share/empty/fs/aufs/aufs.h 1970-01-01 01:00:00.000000000 +0100
594d0238 2826+++ linux/fs/aufs/aufs.h 2022-11-05 23:02:18.959222617 +0100
acd2b654 2827@@ -0,0 +1,62 @@
062440b3 2828+/* SPDX-License-Identifier: GPL-2.0 */
7f207e10 2829+/*
0dcfbb52 2830+ * Copyright (C) 2005-2022 Junjiro R. Okajima
7f207e10 2831+ *
0dcfbb52 2832+ * This program is free software; you can redistribute it and/or modify
7f207e10
AM
2833+ * it under the terms of the GNU General Public License as published by
2834+ * the Free Software Foundation; either version 2 of the License, or
2835+ * (at your option) any later version.
2836+ *
2837+ * This program is distributed in the hope that it will be useful,
2838+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
2839+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2840+ * GNU General Public License for more details.
2841+ *
2842+ * You should have received a copy of the GNU General Public License
523b37e3 2843+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7f207e10
AM
2844+ */
2845+
2846+/*
2847+ * all header files
2848+ */
2849+
2850+#ifndef __AUFS_H__
2851+#define __AUFS_H__
2852+
2853+#ifdef __KERNEL__
2854+
2855+#define AuStub(type, name, body, ...) \
2856+ static inline type name(__VA_ARGS__) { body; }
2857+
2858+#define AuStubVoid(name, ...) \
2859+ AuStub(void, name, , __VA_ARGS__)
2860+#define AuStubInt0(name, ...) \
2861+ AuStub(int, name, return 0, __VA_ARGS__)
2862+
2863+#include "debug.h"
2864+
2865+#include "branch.h"
2866+#include "cpup.h"
2867+#include "dcsub.h"
2868+#include "dbgaufs.h"
2869+#include "dentry.h"
2870+#include "dir.h"
8b6a4947 2871+#include "dirren.h"
7f207e10
AM
2872+#include "dynop.h"
2873+#include "file.h"
2874+#include "fstype.h"
8b6a4947 2875+#include "hbl.h"
7f207e10 2876+#include "inode.h"
acd2b654 2877+#include "lcnt.h"
7f207e10
AM
2878+#include "loop.h"
2879+#include "module.h"
7f207e10
AM
2880+#include "opts.h"
2881+#include "rwsem.h"
7f207e10
AM
2882+#include "super.h"
2883+#include "sysaufs.h"
2884+#include "vfsub.h"
2885+#include "whout.h"
2886+#include "wkq.h"
2887+
2888+#endif /* __KERNEL__ */
2889+#endif /* __AUFS_H__ */
2890diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
eca34b5c 2891--- /usr/share/empty/fs/aufs/branch.c 1970-01-01 01:00:00.000000000 +0100
594d0238 2892+++ linux/fs/aufs/branch.c 2022-11-05 23:02:18.959222617 +0100
42b5c33a 2893@@ -0,0 +1,1427 @@
cd7a4cd9 2894+// SPDX-License-Identifier: GPL-2.0
7f207e10 2895+/*
0dcfbb52 2896+ * Copyright (C) 2005-2022 Junjiro R. Okajima
7f207e10 2897+ *
0dcfbb52 2898+ * This program is free software; you can redistribute it and/or modify
7f207e10
AM
2899+ * it under the terms of the GNU General Public License as published by
2900+ * the Free Software Foundation; either version 2 of the License, or
2901+ * (at your option) any later version.
2902+ *
2903+ * This program is distributed in the hope that it will be useful,
2904+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
2905+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2906+ * GNU General Public License for more details.
2907+ *
2908+ * You should have received a copy of the GNU General Public License
523b37e3 2909+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7f207e10
AM
2910+ */
2911+
2912+/*
2913+ * branch management
2914+ */
2915+
027c5e7a 2916+#include <linux/compat.h>
7f207e10
AM
2917+#include <linux/statfs.h>
2918+#include "aufs.h"
2919+
2920+/*
2921+ * free a single branch
1facf9fc 2922+ */
2923+static void au_br_do_free(struct au_branch *br)
2924+{
2925+ int i;
2926+ struct au_wbr *wbr;
4a4d8108 2927+ struct au_dykey **key;
1facf9fc 2928+
027c5e7a 2929+ au_hnotify_fin_br(br);
8b6a4947
AM
2930+ /* always, regardless the mount option */
2931+ au_dr_hino_free(&br->br_dirren);
062440b3 2932+ au_xino_put(br);
1facf9fc 2933+
acd2b654
AM
2934+ AuLCntZero(au_lcnt_read(&br->br_nfiles, /*do_rev*/0));
2935+ au_lcnt_fin(&br->br_nfiles, /*do_sync*/0);
2936+ AuLCntZero(au_lcnt_read(&br->br_count, /*do_rev*/0));
2937+ au_lcnt_fin(&br->br_count, /*do_sync*/0);
1facf9fc 2938+
2939+ wbr = br->br_wbr;
2940+ if (wbr) {
2941+ for (i = 0; i < AuBrWh_Last; i++)
2942+ dput(wbr->wbr_wh[i]);
2943+ AuDebugOn(atomic_read(&wbr->wbr_wh_running));
dece6358 2944+ AuRwDestroy(&wbr->wbr_wh_rwsem);
1facf9fc 2945+ }
2946+
076b876e
AM
2947+ if (br->br_fhsm) {
2948+ au_br_fhsm_fin(br->br_fhsm);
9f237c51 2949+ au_kfree_try_rcu(br->br_fhsm);
076b876e
AM
2950+ }
2951+
4a4d8108
AM
2952+ key = br->br_dykey;
2953+ for (i = 0; i < AuBrDynOp; i++, key++)
2954+ if (*key)
2955+ au_dy_put(*key);
2956+ else
2957+ break;
2958+
537831f9 2959+ /* recursive lock, s_umount of branch's */
acd2b654 2960+ /* synchronize_rcu(); */ /* why? */
537831f9 2961+ lockdep_off();
86dc4139 2962+ path_put(&br->br_path);
537831f9 2963+ lockdep_on();
9f237c51 2964+ au_kfree_rcu(wbr);
acd2b654
AM
2965+ au_lcnt_wait_for_fin(&br->br_nfiles);
2966+ au_lcnt_wait_for_fin(&br->br_count);
2967+ /* I don't know why, but percpu_refcount requires this */
2968+ /* synchronize_rcu(); */
9f237c51 2969+ au_kfree_rcu(br);
1facf9fc 2970+}
2971+
2972+/*
2973+ * frees all branches
2974+ */
2975+void au_br_free(struct au_sbinfo *sbinfo)
2976+{
2977+ aufs_bindex_t bmax;
2978+ struct au_branch **br;
2979+
dece6358
AM
2980+ AuRwMustWriteLock(&sbinfo->si_rwsem);
2981+
5afbbe0d 2982+ bmax = sbinfo->si_bbot + 1;
1facf9fc 2983+ br = sbinfo->si_branch;
2984+ while (bmax--)
2985+ au_br_do_free(*br++);
2986+}
2987+
2988+/*
2989+ * find the index of a branch which is specified by @br_id.
2990+ */
2991+int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
2992+{
5afbbe0d 2993+ aufs_bindex_t bindex, bbot;
1facf9fc 2994+
5afbbe0d
AM
2995+ bbot = au_sbbot(sb);
2996+ for (bindex = 0; bindex <= bbot; bindex++)
1facf9fc 2997+ if (au_sbr_id(sb, bindex) == br_id)
2998+ return bindex;
2999+ return -1;
3000+}
3001+
3002+/* ---------------------------------------------------------------------- */
3003+
3004+/*
3005+ * add a branch
3006+ */
3007+
b752ccd1
AM
3008+static int test_overlap(struct super_block *sb, struct dentry *h_adding,
3009+ struct dentry *h_root)
1facf9fc 3010+{
b752ccd1
AM
3011+ if (unlikely(h_adding == h_root
3012+ || au_test_loopback_overlap(sb, h_adding)))
1facf9fc 3013+ return 1;
b752ccd1
AM
3014+ if (h_adding->d_sb != h_root->d_sb)
3015+ return 0;
3016+ return au_test_subdir(h_adding, h_root)
3017+ || au_test_subdir(h_root, h_adding);
1facf9fc 3018+}
3019+
3020+/*
3021+ * returns a newly allocated branch. @new_nbranch is a number of branches
3022+ * after adding a branch.
3023+ */
3024+static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
3025+ int perm)
3026+{
3027+ struct au_branch *add_branch;
3028+ struct dentry *root;
5527c038 3029+ struct inode *inode;
4a4d8108 3030+ int err;
1facf9fc 3031+
4a4d8108 3032+ err = -ENOMEM;
be52b249 3033+ add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
1facf9fc 3034+ if (unlikely(!add_branch))
3035+ goto out;
acd2b654 3036+ add_branch->br_xino = au_xino_alloc(/*nfile*/1);
062440b3 3037+ if (unlikely(!add_branch->br_xino))
521ced18 3038+ goto out_br;
027c5e7a
AM
3039+ err = au_hnotify_init_br(add_branch, perm);
3040+ if (unlikely(err))
062440b3 3041+ goto out_xino;
027c5e7a 3042+
1facf9fc 3043+ if (au_br_writable(perm)) {
3044+ /* may be freed separately at changing the branch permission */
be52b249 3045+ add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
1facf9fc 3046+ GFP_NOFS);
3047+ if (unlikely(!add_branch->br_wbr))
027c5e7a 3048+ goto out_hnotify;
1facf9fc 3049+ }
3050+
076b876e
AM
3051+ if (au_br_fhsm(perm)) {
3052+ err = au_fhsm_br_alloc(add_branch);
3053+ if (unlikely(err))
3054+ goto out_wbr;
3055+ }
3056+
521ced18 3057+ root = sb->s_root;
e2f27e51 3058+ err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
4a4d8108 3059+ if (!err)
e2f27e51 3060+ err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
5527c038
JR
3061+ if (!err) {
3062+ inode = d_inode(root);
1c60b727
AM
3063+ err = au_hinode_realloc(au_ii(inode), new_nbranch,
3064+ /*may_shrink*/0);
5527c038 3065+ }
4a4d8108
AM
3066+ if (!err)
3067+ return add_branch; /* success */
1facf9fc 3068+
076b876e 3069+out_wbr:
9f237c51 3070+ au_kfree_rcu(add_branch->br_wbr);
027c5e7a
AM
3071+out_hnotify:
3072+ au_hnotify_fin_br(add_branch);
062440b3
AM
3073+out_xino:
3074+ au_xino_put(add_branch);
4f0767ce 3075+out_br:
9f237c51 3076+ au_kfree_rcu(add_branch);
4f0767ce 3077+out:
4a4d8108 3078+ return ERR_PTR(err);
1facf9fc 3079+}
3080+
3081+/*
3082+ * test if the branch permission is legal or not.
3083+ */
3084+static int test_br(struct inode *inode, int brperm, char *path)
3085+{
3086+ int err;
3087+
4a4d8108
AM
3088+ err = (au_br_writable(brperm) && IS_RDONLY(inode));
3089+ if (!err)
3090+ goto out;
1facf9fc 3091+
4a4d8108
AM
3092+ err = -EINVAL;
3093+ pr_err("write permission for readonly mount or inode, %s\n", path);
3094+
4f0767ce 3095+out:
1facf9fc 3096+ return err;
3097+}
3098+
3099+/*
3100+ * returns:
3101+ * 0: success, the caller will add it
3102+ * plus: success, it is already unified, the caller should ignore it
3103+ * minus: error
3104+ */
3105+static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3106+{
3107+ int err;
5afbbe0d 3108+ aufs_bindex_t bbot, bindex;
5527c038 3109+ struct dentry *root, *h_dentry;
1facf9fc 3110+ struct inode *inode, *h_inode;
3111+
3112+ root = sb->s_root;
5afbbe0d
AM
3113+ bbot = au_sbbot(sb);
3114+ if (unlikely(bbot >= 0
1facf9fc 3115+ && au_find_dbindex(root, add->path.dentry) >= 0)) {
3116+ err = 1;
3117+ if (!remount) {
3118+ err = -EINVAL;
4a4d8108 3119+ pr_err("%s duplicated\n", add->pathname);
1facf9fc 3120+ }
3121+ goto out;
3122+ }
3123+
3124+ err = -ENOSPC; /* -E2BIG; */
3125+ if (unlikely(AUFS_BRANCH_MAX <= add->bindex
5afbbe0d 3126+ || AUFS_BRANCH_MAX - 1 <= bbot)) {
4a4d8108 3127+ pr_err("number of branches exceeded %s\n", add->pathname);
1facf9fc 3128+ goto out;
3129+ }
3130+
3131+ err = -EDOM;
5afbbe0d 3132+ if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
4a4d8108 3133+ pr_err("bad index %d\n", add->bindex);
1facf9fc 3134+ goto out;
3135+ }
3136+
5527c038 3137+ inode = d_inode(add->path.dentry);
1facf9fc 3138+ err = -ENOENT;
3139+ if (unlikely(!inode->i_nlink)) {
4a4d8108 3140+ pr_err("no existence %s\n", add->pathname);
1facf9fc 3141+ goto out;
3142+ }
3143+
3144+ err = -EINVAL;
3145+ if (unlikely(inode->i_sb == sb)) {
4a4d8108 3146+ pr_err("%s must be outside\n", add->pathname);
1facf9fc 3147+ goto out;
3148+ }
3149+
3150+ if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
4a4d8108
AM
3151+ pr_err("unsupported filesystem, %s (%s)\n",
3152+ add->pathname, au_sbtype(inode->i_sb));
1facf9fc 3153+ goto out;
3154+ }
3155+
c1595e42
JR
3156+ if (unlikely(inode->i_sb->s_stack_depth)) {
3157+ pr_err("already stacked, %s (%s)\n",
3158+ add->pathname, au_sbtype(inode->i_sb));
3159+ goto out;
3160+ }
3161+
5527c038 3162+ err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
1facf9fc 3163+ if (unlikely(err))
3164+ goto out;
3165+
5afbbe0d 3166+ if (bbot < 0)
1facf9fc 3167+ return 0; /* success */
3168+
3169+ err = -EINVAL;
5afbbe0d 3170+ for (bindex = 0; bindex <= bbot; bindex++)
1facf9fc 3171+ if (unlikely(test_overlap(sb, add->path.dentry,
3172+ au_h_dptr(root, bindex)))) {
4a4d8108 3173+ pr_err("%s is overlapped\n", add->pathname);
1facf9fc 3174+ goto out;
3175+ }
3176+
3177+ err = 0;
3178+ if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
5527c038
JR
3179+ h_dentry = au_h_dptr(root, 0);
3180+ h_inode = d_inode(h_dentry);
1facf9fc 3181+ if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
0c3ec466
AM
3182+ || !uid_eq(h_inode->i_uid, inode->i_uid)
3183+ || !gid_eq(h_inode->i_gid, inode->i_gid))
3184+ pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3185+ add->pathname,
3186+ i_uid_read(inode), i_gid_read(inode),
3187+ (inode->i_mode & S_IALLUGO),
3188+ i_uid_read(h_inode), i_gid_read(h_inode),
3189+ (h_inode->i_mode & S_IALLUGO));
1facf9fc 3190+ }
3191+
4f0767ce 3192+out:
1facf9fc 3193+ return err;
3194+}
3195+
3196+/*
3197+ * initialize or clean the whiteouts for an adding branch
3198+ */
3199+static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
86dc4139 3200+ int new_perm)
1facf9fc 3201+{
3202+ int err, old_perm;
3203+ aufs_bindex_t bindex;
febd17d6 3204+ struct inode *h_inode;
1facf9fc 3205+ struct au_wbr *wbr;
3206+ struct au_hinode *hdir;
5527c038 3207+ struct dentry *h_dentry;
1facf9fc 3208+
86dc4139
AM
3209+ err = vfsub_mnt_want_write(au_br_mnt(br));
3210+ if (unlikely(err))
3211+ goto out;
3212+
1facf9fc 3213+ wbr = br->br_wbr;
3214+ old_perm = br->br_perm;
3215+ br->br_perm = new_perm;
3216+ hdir = NULL;
febd17d6 3217+ h_inode = NULL;
1facf9fc 3218+ bindex = au_br_index(sb, br->br_id);
3219+ if (0 <= bindex) {
5527c038 3220+ hdir = au_hi(d_inode(sb->s_root), bindex);
5afbbe0d 3221+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 3222+ } else {
5527c038 3223+ h_dentry = au_br_dentry(br);
febd17d6
JR
3224+ h_inode = d_inode(h_dentry);
3225+ inode_lock_nested(h_inode, AuLsc_I_PARENT);
1facf9fc 3226+ }
3227+ if (!wbr)
86dc4139 3228+ err = au_wh_init(br, sb);
1facf9fc 3229+ else {
3230+ wbr_wh_write_lock(wbr);
86dc4139 3231+ err = au_wh_init(br, sb);
1facf9fc 3232+ wbr_wh_write_unlock(wbr);
3233+ }
3234+ if (hdir)
5afbbe0d 3235+ au_hn_inode_unlock(hdir);
1facf9fc 3236+ else
febd17d6 3237+ inode_unlock(h_inode);
86dc4139 3238+ vfsub_mnt_drop_write(au_br_mnt(br));
1facf9fc 3239+ br->br_perm = old_perm;
3240+
3241+ if (!err && wbr && !au_br_writable(new_perm)) {
9f237c51 3242+ au_kfree_rcu(wbr);
1facf9fc 3243+ br->br_wbr = NULL;
3244+ }
3245+
86dc4139 3246+out:
1facf9fc 3247+ return err;
3248+}
3249+
3250+static int au_wbr_init(struct au_branch *br, struct super_block *sb,
86dc4139 3251+ int perm)
1facf9fc 3252+{
3253+ int err;
4a4d8108 3254+ struct kstatfs kst;
1facf9fc 3255+ struct au_wbr *wbr;
3256+
3257+ wbr = br->br_wbr;
dece6358 3258+ au_rw_init(&wbr->wbr_wh_rwsem);
1facf9fc 3259+ atomic_set(&wbr->wbr_wh_running, 0);
1facf9fc 3260+
4a4d8108
AM
3261+ /*
3262+ * a limit for rmdir/rename a dir
523b37e3 3263+ * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
4a4d8108 3264+ */
86dc4139 3265+ err = vfs_statfs(&br->br_path, &kst);
4a4d8108
AM
3266+ if (unlikely(err))
3267+ goto out;
3268+ err = -EINVAL;
3269+ if (kst.f_namelen >= NAME_MAX)
86dc4139 3270+ err = au_br_init_wh(sb, br, perm);
4a4d8108 3271+ else
523b37e3
AM
3272+ pr_err("%pd(%s), unsupported namelen %ld\n",
3273+ au_br_dentry(br),
86dc4139 3274+ au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
1facf9fc 3275+
4f0767ce 3276+out:
1facf9fc 3277+ return err;
3278+}
3279+
c1595e42 3280+/* initialize a new branch */
1facf9fc 3281+static int au_br_init(struct au_branch *br, struct super_block *sb,
3282+ struct au_opt_add *add)
3283+{
3284+ int err;
062440b3
AM
3285+ struct au_branch *brbase;
3286+ struct file *xf;
5527c038 3287+ struct inode *h_inode;
1facf9fc 3288+
3289+ err = 0;
1facf9fc 3290+ br->br_perm = add->perm;
86dc4139 3291+ br->br_path = add->path; /* set first, path_get() later */
4a4d8108 3292+ spin_lock_init(&br->br_dykey_lock);
acd2b654
AM
3293+ au_lcnt_init(&br->br_nfiles, /*release*/NULL);
3294+ au_lcnt_init(&br->br_count, /*release*/NULL);
1facf9fc 3295+ br->br_id = au_new_br_id(sb);
7f207e10 3296+ AuDebugOn(br->br_id < 0);
1facf9fc 3297+
8b6a4947
AM
3298+ /* always, regardless the given option */
3299+ err = au_dr_br_init(sb, br, &add->path);
3300+ if (unlikely(err))
3301+ goto out_err;
3302+
1facf9fc 3303+ if (au_br_writable(add->perm)) {
86dc4139 3304+ err = au_wbr_init(br, sb, add->perm);
1facf9fc 3305+ if (unlikely(err))
b752ccd1 3306+ goto out_err;
1facf9fc 3307+ }
3308+
3309+ if (au_opt_test(au_mntflags(sb), XINO)) {
062440b3 3310+ brbase = au_sbr(sb, 0);
acd2b654 3311+ xf = au_xino_file(brbase->br_xino, /*idx*/-1);
062440b3 3312+ AuDebugOn(!xf);
5527c038 3313+ h_inode = d_inode(add->path.dentry);
062440b3 3314+ err = au_xino_init_br(sb, br, h_inode->i_ino, &xf->f_path);
1facf9fc 3315+ if (unlikely(err)) {
acd2b654 3316+ AuDebugOn(au_xino_file(br->br_xino, /*idx*/-1));
b752ccd1 3317+ goto out_err;
1facf9fc 3318+ }
3319+ }
3320+
3321+ sysaufs_br_init(br);
86dc4139 3322+ path_get(&br->br_path);
b752ccd1 3323+ goto out; /* success */
1facf9fc 3324+
4f0767ce 3325+out_err:
86dc4139 3326+ memset(&br->br_path, 0, sizeof(br->br_path));
4f0767ce 3327+out:
1facf9fc 3328+ return err;
3329+}
3330+
3331+static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
5afbbe0d 3332+ struct au_branch *br, aufs_bindex_t bbot,
1facf9fc 3333+ aufs_bindex_t amount)
3334+{
3335+ struct au_branch **brp;
3336+
dece6358
AM
3337+ AuRwMustWriteLock(&sbinfo->si_rwsem);
3338+
1facf9fc 3339+ brp = sbinfo->si_branch + bindex;
3340+ memmove(brp + 1, brp, sizeof(*brp) * amount);
3341+ *brp = br;
5afbbe0d
AM
3342+ sbinfo->si_bbot++;
3343+ if (unlikely(bbot < 0))
3344+ sbinfo->si_bbot = 0;
1facf9fc 3345+}
3346+
3347+static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
5afbbe0d 3348+ aufs_bindex_t bbot, aufs_bindex_t amount)
1facf9fc 3349+{
3350+ struct au_hdentry *hdp;
3351+
1308ab2a 3352+ AuRwMustWriteLock(&dinfo->di_rwsem);
3353+
5afbbe0d 3354+ hdp = au_hdentry(dinfo, bindex);
1facf9fc 3355+ memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3356+ au_h_dentry_init(hdp);
5afbbe0d
AM
3357+ dinfo->di_bbot++;
3358+ if (unlikely(bbot < 0))
3359+ dinfo->di_btop = 0;
1facf9fc 3360+}
3361+
3362+static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
5afbbe0d 3363+ aufs_bindex_t bbot, aufs_bindex_t amount)
1facf9fc 3364+{
3365+ struct au_hinode *hip;
3366+
1308ab2a 3367+ AuRwMustWriteLock(&iinfo->ii_rwsem);
3368+
5afbbe0d 3369+ hip = au_hinode(iinfo, bindex);
1facf9fc 3370+ memmove(hip + 1, hip, sizeof(*hip) * amount);
5afbbe0d
AM
3371+ au_hinode_init(hip);
3372+ iinfo->ii_bbot++;
3373+ if (unlikely(bbot < 0))
3374+ iinfo->ii_btop = 0;
1facf9fc 3375+}
3376+
86dc4139
AM
3377+static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3378+ aufs_bindex_t bindex)
1facf9fc 3379+{
86dc4139 3380+ struct dentry *root, *h_dentry;
5527c038 3381+ struct inode *root_inode, *h_inode;
5afbbe0d 3382+ aufs_bindex_t bbot, amount;
1facf9fc 3383+
3384+ root = sb->s_root;
5527c038 3385+ root_inode = d_inode(root);
5afbbe0d
AM
3386+ bbot = au_sbbot(sb);
3387+ amount = bbot + 1 - bindex;
86dc4139 3388+ h_dentry = au_br_dentry(br);
53392da6 3389+ au_sbilist_lock();
5afbbe0d
AM
3390+ au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
3391+ au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
3392+ au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
1facf9fc 3393+ au_set_h_dptr(root, bindex, dget(h_dentry));
5527c038
JR
3394+ h_inode = d_inode(h_dentry);
3395+ au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
53392da6 3396+ au_sbilist_unlock();
1facf9fc 3397+}
3398+
3399+int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3400+{
3401+ int err;
5afbbe0d 3402+ aufs_bindex_t bbot, add_bindex;
1facf9fc 3403+ struct dentry *root, *h_dentry;
3404+ struct inode *root_inode;
3405+ struct au_branch *add_branch;
3406+
3407+ root = sb->s_root;
5527c038 3408+ root_inode = d_inode(root);
1facf9fc 3409+ IMustLock(root_inode);
5afbbe0d 3410+ IiMustWriteLock(root_inode);
1facf9fc 3411+ err = test_add(sb, add, remount);
3412+ if (unlikely(err < 0))
3413+ goto out;
3414+ if (err) {
3415+ err = 0;
3416+ goto out; /* success */
3417+ }
3418+
5afbbe0d
AM
3419+ bbot = au_sbbot(sb);
3420+ add_branch = au_br_alloc(sb, bbot + 2, add->perm);
1facf9fc 3421+ err = PTR_ERR(add_branch);
3422+ if (IS_ERR(add_branch))
3423+ goto out;
3424+
3425+ err = au_br_init(add_branch, sb, add);
3426+ if (unlikely(err)) {
3427+ au_br_do_free(add_branch);
3428+ goto out;
3429+ }
3430+
3431+ add_bindex = add->bindex;
062440b3
AM
3432+ sysaufs_brs_del(sb, add_bindex); /* remove successors */
3433+ au_br_do_add(sb, add_branch, add_bindex);
3434+ sysaufs_brs_add(sb, add_bindex); /* append successors */
3435+ dbgaufs_brs_add(sb, add_bindex, /*topdown*/0); /* rename successors */
1facf9fc 3436+
86dc4139 3437+ h_dentry = add->path.dentry;
1308ab2a 3438+ if (!add_bindex) {
1facf9fc 3439+ au_cpup_attr_all(root_inode, /*force*/1);
1308ab2a 3440+ sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3441+ } else
5527c038 3442+ au_add_nlink(root_inode, d_inode(h_dentry));
1facf9fc 3443+
4f0767ce 3444+out:
1facf9fc 3445+ return err;
3446+}
3447+
3448+/* ---------------------------------------------------------------------- */
3449+
79b8bda9 3450+static unsigned long long au_farray_cb(struct super_block *sb, void *a,
076b876e
AM
3451+ unsigned long long max __maybe_unused,
3452+ void *arg)
3453+{
3454+ unsigned long long n;
3455+ struct file **p, *f;
8b6a4947
AM
3456+ struct hlist_bl_head *files;
3457+ struct hlist_bl_node *pos;
076b876e 3458+ struct au_finfo *finfo;
076b876e
AM
3459+
3460+ n = 0;
3461+ p = a;
3462+ files = &au_sbi(sb)->si_files;
8b6a4947
AM
3463+ hlist_bl_lock(files);
3464+ hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
076b876e
AM
3465+ f = finfo->fi_file;
3466+ if (file_count(f)
3467+ && !special_file(file_inode(f)->i_mode)) {
3468+ get_file(f);
3469+ *p++ = f;
3470+ n++;
3471+ AuDebugOn(n > max);
3472+ }
3473+ }
8b6a4947 3474+ hlist_bl_unlock(files);
076b876e
AM
3475+
3476+ return n;
3477+}
3478+
3479+static struct file **au_farray_alloc(struct super_block *sb,
3480+ unsigned long long *max)
3481+{
acd2b654
AM
3482+ struct au_sbinfo *sbi;
3483+
3484+ sbi = au_sbi(sb);
3485+ *max = au_lcnt_read(&sbi->si_nfiles, /*do_rev*/1);
79b8bda9 3486+ return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
076b876e
AM
3487+}
3488+
3489+static void au_farray_free(struct file **a, unsigned long long max)
3490+{
3491+ unsigned long long ull;
3492+
3493+ for (ull = 0; ull < max; ull++)
3494+ if (a[ull])
3495+ fput(a[ull]);
be52b249 3496+ kvfree(a);
076b876e
AM
3497+}
3498+
3499+/* ---------------------------------------------------------------------- */
3500+
1facf9fc 3501+/*
3502+ * delete a branch
3503+ */
3504+
3505+/* to show the line number, do not make it inlined function */
4a4d8108 3506+#define AuVerbose(do_info, fmt, ...) do { \
1facf9fc 3507+ if (do_info) \
4a4d8108 3508+ pr_info(fmt, ##__VA_ARGS__); \
1facf9fc 3509+} while (0)
3510+
5afbbe0d
AM
3511+static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
3512+ aufs_bindex_t bbot)
027c5e7a 3513+{
5afbbe0d 3514+ return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
027c5e7a
AM
3515+}
3516+
5afbbe0d
AM
3517+static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
3518+ aufs_bindex_t bbot)
027c5e7a 3519+{
5afbbe0d 3520+ return au_test_ibusy(d_inode(dentry), btop, bbot);
027c5e7a
AM
3521+}
3522+
1facf9fc 3523+/*
3524+ * test if the branch is deletable or not.
3525+ */
3526+static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
b752ccd1 3527+ unsigned int sigen, const unsigned int verbose)
1facf9fc 3528+{
3529+ int err, i, j, ndentry;
5afbbe0d 3530+ aufs_bindex_t btop, bbot;
1facf9fc 3531+ struct au_dcsub_pages dpages;
3532+ struct au_dpage *dpage;
3533+ struct dentry *d;
1facf9fc 3534+
3535+ err = au_dpages_init(&dpages, GFP_NOFS);
3536+ if (unlikely(err))
3537+ goto out;
3538+ err = au_dcsub_pages(&dpages, root, NULL, NULL);
3539+ if (unlikely(err))
3540+ goto out_dpages;
3541+
1facf9fc 3542+ for (i = 0; !err && i < dpages.ndpage; i++) {
3543+ dpage = dpages.dpages + i;
3544+ ndentry = dpage->ndentry;
3545+ for (j = 0; !err && j < ndentry; j++) {
3546+ d = dpage->dentries[j];
c1595e42 3547+ AuDebugOn(au_dcount(d) <= 0);
027c5e7a 3548+ if (!au_digen_test(d, sigen)) {
1facf9fc 3549+ di_read_lock_child(d, AuLock_IR);
027c5e7a
AM
3550+ if (unlikely(au_dbrange_test(d))) {
3551+ di_read_unlock(d, AuLock_IR);
3552+ continue;
3553+ }
3554+ } else {
1facf9fc 3555+ di_write_lock_child(d);
027c5e7a
AM
3556+ if (unlikely(au_dbrange_test(d))) {
3557+ di_write_unlock(d);
3558+ continue;
3559+ }
1facf9fc 3560+ err = au_reval_dpath(d, sigen);
3561+ if (!err)
3562+ di_downgrade_lock(d, AuLock_IR);
3563+ else {
3564+ di_write_unlock(d);
3565+ break;
3566+ }
3567+ }
3568+
027c5e7a 3569+ /* AuDbgDentry(d); */
5afbbe0d
AM
3570+ btop = au_dbtop(d);
3571+ bbot = au_dbbot(d);
3572+ if (btop <= bindex
3573+ && bindex <= bbot
1facf9fc 3574+ && au_h_dptr(d, bindex)
5afbbe0d 3575+ && au_test_dbusy(d, btop, bbot)) {
1facf9fc 3576+ err = -EBUSY;
523b37e3 3577+ AuVerbose(verbose, "busy %pd\n", d);
027c5e7a 3578+ AuDbgDentry(d);
1facf9fc 3579+ }
3580+ di_read_unlock(d, AuLock_IR);
3581+ }
3582+ }
3583+
4f0767ce 3584+out_dpages:
1facf9fc 3585+ au_dpages_free(&dpages);
4f0767ce 3586+out:
1facf9fc 3587+ return err;
3588+}
3589+
3590+static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
b752ccd1 3591+ unsigned int sigen, const unsigned int verbose)
1facf9fc 3592+{
3593+ int err;
7f207e10
AM
3594+ unsigned long long max, ull;
3595+ struct inode *i, **array;
5afbbe0d 3596+ aufs_bindex_t btop, bbot;
1facf9fc 3597+
7f207e10
AM
3598+ array = au_iarray_alloc(sb, &max);
3599+ err = PTR_ERR(array);
3600+ if (IS_ERR(array))
3601+ goto out;
3602+
1facf9fc 3603+ err = 0;
7f207e10
AM
3604+ AuDbg("b%d\n", bindex);
3605+ for (ull = 0; !err && ull < max; ull++) {
3606+ i = array[ull];
076b876e
AM
3607+ if (unlikely(!i))
3608+ break;
7f207e10 3609+ if (i->i_ino == AUFS_ROOT_INO)
1facf9fc 3610+ continue;
3611+
7f207e10 3612+ /* AuDbgInode(i); */
537831f9 3613+ if (au_iigen(i, NULL) == sigen)
1facf9fc 3614+ ii_read_lock_child(i);
3615+ else {
3616+ ii_write_lock_child(i);
027c5e7a
AM
3617+ err = au_refresh_hinode_self(i);
3618+ au_iigen_dec(i);
1facf9fc 3619+ if (!err)
3620+ ii_downgrade_lock(i);
3621+ else {
3622+ ii_write_unlock(i);
3623+ break;
3624+ }
3625+ }
3626+
5afbbe0d
AM
3627+ btop = au_ibtop(i);
3628+ bbot = au_ibbot(i);
3629+ if (btop <= bindex
3630+ && bindex <= bbot
1facf9fc 3631+ && au_h_iptr(i, bindex)
5afbbe0d 3632+ && au_test_ibusy(i, btop, bbot)) {
1facf9fc 3633+ err = -EBUSY;
3634+ AuVerbose(verbose, "busy i%lu\n", i->i_ino);
7f207e10 3635+ AuDbgInode(i);
1facf9fc 3636+ }
3637+ ii_read_unlock(i);
3638+ }
7f207e10 3639+ au_iarray_free(array, max);
1facf9fc 3640+
7f207e10 3641+out:
1facf9fc 3642+ return err;
3643+}
3644+
b752ccd1
AM
3645+static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3646+ const unsigned int verbose)
1facf9fc 3647+{
3648+ int err;
3649+ unsigned int sigen;
3650+
3651+ sigen = au_sigen(root->d_sb);
3652+ DiMustNoWaiters(root);
5527c038 3653+ IiMustNoWaiters(d_inode(root));
1facf9fc 3654+ di_write_unlock(root);
b752ccd1 3655+ err = test_dentry_busy(root, bindex, sigen, verbose);
1facf9fc 3656+ if (!err)
b752ccd1 3657+ err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
1facf9fc 3658+ di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3659+
3660+ return err;
3661+}
3662+
076b876e
AM
3663+static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3664+ struct file **to_free, int *idx)
3665+{
3666+ int err;
c1595e42 3667+ unsigned char matched, root;
5afbbe0d 3668+ aufs_bindex_t bindex, bbot;
076b876e
AM
3669+ struct au_fidir *fidir;
3670+ struct au_hfile *hfile;
3671+
3672+ err = 0;
2000de60 3673+ root = IS_ROOT(file->f_path.dentry);
c1595e42
JR
3674+ if (root) {
3675+ get_file(file);
3676+ to_free[*idx] = file;
3677+ (*idx)++;
3678+ goto out;
3679+ }
3680+
076b876e 3681+ matched = 0;
076b876e
AM
3682+ fidir = au_fi(file)->fi_hdir;
3683+ AuDebugOn(!fidir);
5afbbe0d
AM
3684+ bbot = au_fbbot_dir(file);
3685+ for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
076b876e
AM
3686+ hfile = fidir->fd_hfile + bindex;
3687+ if (!hfile->hf_file)
3688+ continue;
3689+
c1595e42 3690+ if (hfile->hf_br->br_id == br_id) {
076b876e 3691+ matched = 1;
076b876e 3692+ break;
c1595e42 3693+ }
076b876e 3694+ }
c1595e42 3695+ if (matched)
076b876e
AM
3696+ err = -EBUSY;
3697+
3698+out:
3699+ return err;
3700+}
3701+
3702+static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3703+ struct file **to_free, int opened)
3704+{
3705+ int err, idx;
3706+ unsigned long long ull, max;
5afbbe0d 3707+ aufs_bindex_t btop;
076b876e 3708+ struct file *file, **array;
076b876e
AM
3709+ struct dentry *root;
3710+ struct au_hfile *hfile;
3711+
3712+ array = au_farray_alloc(sb, &max);
3713+ err = PTR_ERR(array);
3714+ if (IS_ERR(array))
3715+ goto out;
3716+
3717+ err = 0;
3718+ idx = 0;
3719+ root = sb->s_root;
3720+ di_write_unlock(root);
3721+ for (ull = 0; ull < max; ull++) {
3722+ file = array[ull];
3723+ if (unlikely(!file))
3724+ break;
3725+
3726+ /* AuDbg("%pD\n", file); */
3727+ fi_read_lock(file);
5afbbe0d 3728+ btop = au_fbtop(file);
2000de60 3729+ if (!d_is_dir(file->f_path.dentry)) {
076b876e
AM
3730+ hfile = &au_fi(file)->fi_htop;
3731+ if (hfile->hf_br->br_id == br_id)
3732+ err = -EBUSY;
3733+ } else
3734+ err = test_dir_busy(file, br_id, to_free, &idx);
3735+ fi_read_unlock(file);
3736+ if (unlikely(err))
3737+ break;
3738+ }
3739+ di_write_lock_child(root);
3740+ au_farray_free(array, max);
3741+ AuDebugOn(idx > opened);
3742+
3743+out:
3744+ return err;
3745+}
3746+
3747+static void br_del_file(struct file **to_free, unsigned long long opened,
062440b3 3748+ aufs_bindex_t br_id)
076b876e
AM
3749+{
3750+ unsigned long long ull;
5afbbe0d 3751+ aufs_bindex_t bindex, btop, bbot, bfound;
076b876e
AM
3752+ struct file *file;
3753+ struct au_fidir *fidir;
3754+ struct au_hfile *hfile;
3755+
3756+ for (ull = 0; ull < opened; ull++) {
3757+ file = to_free[ull];
3758+ if (unlikely(!file))
3759+ break;
3760+
3761+ /* AuDbg("%pD\n", file); */
2000de60 3762+ AuDebugOn(!d_is_dir(file->f_path.dentry));
076b876e
AM
3763+ bfound = -1;
3764+ fidir = au_fi(file)->fi_hdir;
3765+ AuDebugOn(!fidir);
3766+ fi_write_lock(file);
5afbbe0d
AM
3767+ btop = au_fbtop(file);
3768+ bbot = au_fbbot_dir(file);
3769+ for (bindex = btop; bindex <= bbot; bindex++) {
076b876e
AM
3770+ hfile = fidir->fd_hfile + bindex;
3771+ if (!hfile->hf_file)
3772+ continue;
3773+
3774+ if (hfile->hf_br->br_id == br_id) {
3775+ bfound = bindex;
3776+ break;
3777+ }
3778+ }
3779+ AuDebugOn(bfound < 0);
3780+ au_set_h_fptr(file, bfound, NULL);
5afbbe0d
AM
3781+ if (bfound == btop) {
3782+ for (btop++; btop <= bbot; btop++)
3783+ if (au_hf_dir(file, btop)) {
3784+ au_set_fbtop(file, btop);
076b876e
AM
3785+ break;
3786+ }
3787+ }
3788+ fi_write_unlock(file);
3789+ }
3790+}
3791+
1facf9fc 3792+static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
3793+ const aufs_bindex_t bindex,
5afbbe0d 3794+ const aufs_bindex_t bbot)
1facf9fc 3795+{
3796+ struct au_branch **brp, **p;
3797+
dece6358
AM
3798+ AuRwMustWriteLock(&sbinfo->si_rwsem);
3799+
1facf9fc 3800+ brp = sbinfo->si_branch + bindex;
5afbbe0d
AM
3801+ if (bindex < bbot)
3802+ memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
3803+ sbinfo->si_branch[0 + bbot] = NULL;
3804+ sbinfo->si_bbot--;
1facf9fc 3805+
e2f27e51
AM
3806+ p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
3807+ /*may_shrink*/1);
1facf9fc 3808+ if (p)
3809+ sbinfo->si_branch = p;
4a4d8108 3810+ /* harmless error */
1facf9fc 3811+}
3812+
3813+static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
5afbbe0d 3814+ const aufs_bindex_t bbot)
1facf9fc 3815+{
3816+ struct au_hdentry *hdp, *p;
3817+
1308ab2a 3818+ AuRwMustWriteLock(&dinfo->di_rwsem);
3819+
5afbbe0d
AM
3820+ hdp = au_hdentry(dinfo, bindex);
3821+ if (bindex < bbot)
3822+ memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
3823+ /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
3824+ dinfo->di_bbot--;
1facf9fc 3825+
e2f27e51
AM
3826+ p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
3827+ /*may_shrink*/1);
1facf9fc 3828+ if (p)
3829+ dinfo->di_hdentry = p;
4a4d8108 3830+ /* harmless error */
1facf9fc 3831+}
3832+
3833+static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
5afbbe0d 3834+ const aufs_bindex_t bbot)
1facf9fc 3835+{
3836+ struct au_hinode *hip, *p;
3837+
1308ab2a 3838+ AuRwMustWriteLock(&iinfo->ii_rwsem);
3839+
5afbbe0d
AM
3840+ hip = au_hinode(iinfo, bindex);
3841+ if (bindex < bbot)
3842+ memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
3843+ /* au_hinode_init(au_hinode(iinfo, bbot)); */
3844+ iinfo->ii_bbot--;
1facf9fc 3845+
e2f27e51
AM
3846+ p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
3847+ /*may_shrink*/1);
1facf9fc 3848+ if (p)
3849+ iinfo->ii_hinode = p;
4a4d8108 3850+ /* harmless error */
1facf9fc 3851+}
3852+
3853+static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
3854+ struct au_branch *br)
3855+{
5afbbe0d 3856+ aufs_bindex_t bbot;
1facf9fc 3857+ struct au_sbinfo *sbinfo;
53392da6
AM
3858+ struct dentry *root, *h_root;
3859+ struct inode *inode, *h_inode;
3860+ struct au_hinode *hinode;
1facf9fc 3861+
dece6358
AM
3862+ SiMustWriteLock(sb);
3863+
1facf9fc 3864+ root = sb->s_root;
5527c038 3865+ inode = d_inode(root);
1facf9fc 3866+ sbinfo = au_sbi(sb);
5afbbe0d 3867+ bbot = sbinfo->si_bbot;
1facf9fc 3868+
53392da6
AM
3869+ h_root = au_h_dptr(root, bindex);
3870+ hinode = au_hi(inode, bindex);
3871+ h_inode = au_igrab(hinode->hi_inode);
3872+ au_hiput(hinode);
1facf9fc 3873+
53392da6 3874+ au_sbilist_lock();
5afbbe0d
AM
3875+ au_br_do_del_brp(sbinfo, bindex, bbot);
3876+ au_br_do_del_hdp(au_di(root), bindex, bbot);
3877+ au_br_do_del_hip(au_ii(inode), bindex, bbot);
53392da6
AM
3878+ au_sbilist_unlock();
3879+
8b6a4947
AM
3880+ /* ignore an error */
3881+ au_dr_br_fin(sb, br); /* always, regardless the mount option */
3882+
53392da6
AM
3883+ dput(h_root);
3884+ iput(h_inode);
3885+ au_br_do_free(br);
1facf9fc 3886+}
3887+
79b8bda9
AM
3888+static unsigned long long empty_cb(struct super_block *sb, void *array,
3889+ unsigned long long max, void *arg)
076b876e
AM
3890+{
3891+ return max;
3892+}
3893+
1facf9fc 3894+int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
3895+{
3896+ int err, rerr, i;
076b876e 3897+ unsigned long long opened;
1facf9fc 3898+ unsigned int mnt_flags;
5afbbe0d 3899+ aufs_bindex_t bindex, bbot, br_id;
1facf9fc 3900+ unsigned char do_wh, verbose;
3901+ struct au_branch *br;
3902+ struct au_wbr *wbr;
076b876e
AM
3903+ struct dentry *root;
3904+ struct file **to_free;
1facf9fc 3905+
3906+ err = 0;
076b876e
AM
3907+ opened = 0;
3908+ to_free = NULL;
3909+ root = sb->s_root;
3910+ bindex = au_find_dbindex(root, del->h_path.dentry);
1facf9fc 3911+ if (bindex < 0) {
3912+ if (remount)
3913+ goto out; /* success */
3914+ err = -ENOENT;
4a4d8108 3915+ pr_err("%s no such branch\n", del->pathname);
1facf9fc 3916+ goto out;
3917+ }
3918+ AuDbg("bindex b%d\n", bindex);
3919+
3920+ err = -EBUSY;
3921+ mnt_flags = au_mntflags(sb);
3922+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
5afbbe0d
AM
3923+ bbot = au_sbbot(sb);
3924+ if (unlikely(!bbot)) {
1facf9fc 3925+ AuVerbose(verbose, "no more branches left\n");
3926+ goto out;
3927+ }
acd2b654 3928+
1facf9fc 3929+ br = au_sbr(sb, bindex);
86dc4139 3930+ AuDebugOn(!path_equal(&br->br_path, &del->h_path));
acd2b654
AM
3931+ if (unlikely(au_lcnt_read(&br->br_count, /*do_rev*/1))) {
3932+ AuVerbose(verbose, "br %pd2 is busy now\n", del->h_path.dentry);
3933+ goto out;
3934+ }
076b876e
AM
3935+
3936+ br_id = br->br_id;
acd2b654 3937+ opened = au_lcnt_read(&br->br_nfiles, /*do_rev*/1);
076b876e 3938+ if (unlikely(opened)) {
79b8bda9 3939+ to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
076b876e
AM
3940+ err = PTR_ERR(to_free);
3941+ if (IS_ERR(to_free))
3942+ goto out;
3943+
3944+ err = test_file_busy(sb, br_id, to_free, opened);
3945+ if (unlikely(err)) {
3946+ AuVerbose(verbose, "%llu file(s) opened\n", opened);
3947+ goto out;
3948+ }
1facf9fc 3949+ }
3950+
3951+ wbr = br->br_wbr;
3952+ do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
3953+ if (do_wh) {
1308ab2a 3954+ /* instead of WbrWhMustWriteLock(wbr) */
3955+ SiMustWriteLock(sb);
1facf9fc 3956+ for (i = 0; i < AuBrWh_Last; i++) {
3957+ dput(wbr->wbr_wh[i]);
3958+ wbr->wbr_wh[i] = NULL;
3959+ }
3960+ }
3961+
076b876e 3962+ err = test_children_busy(root, bindex, verbose);
1facf9fc 3963+ if (unlikely(err)) {
3964+ if (do_wh)
3965+ goto out_wh;
3966+ goto out;
3967+ }
3968+
3969+ err = 0;
076b876e
AM
3970+ if (to_free) {
3971+ /*
3972+ * now we confirmed the branch is deletable.
3973+ * let's free the remaining opened dirs on the branch.
3974+ */
3975+ di_write_unlock(root);
3976+ br_del_file(to_free, opened, br_id);
3977+ di_write_lock_child(root);
3978+ }
3979+
062440b3
AM
3980+ sysaufs_brs_del(sb, bindex); /* remove successors */
3981+ dbgaufs_xino_del(br); /* remove one */
3982+ au_br_do_del(sb, bindex, br);
3983+ sysaufs_brs_add(sb, bindex); /* append successors */
3984+ dbgaufs_brs_add(sb, bindex, /*topdown*/1); /* rename successors */
1facf9fc 3985+
1308ab2a 3986+ if (!bindex) {
5527c038 3987+ au_cpup_attr_all(d_inode(root), /*force*/1);
1308ab2a 3988+ sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
3989+ } else
5527c038 3990+ au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
1facf9fc 3991+ if (au_opt_test(mnt_flags, PLINK))
3992+ au_plink_half_refresh(sb, br_id);
3993+
1facf9fc 3994+ goto out; /* success */
3995+
4f0767ce 3996+out_wh:
1facf9fc 3997+ /* revert */
86dc4139 3998+ rerr = au_br_init_wh(sb, br, br->br_perm);
1facf9fc 3999+ if (rerr)
0c3ec466
AM
4000+ pr_warn("failed re-creating base whiteout, %s. (%d)\n",
4001+ del->pathname, rerr);
4f0767ce 4002+out:
076b876e
AM
4003+ if (to_free)
4004+ au_farray_free(to_free, opened);
1facf9fc 4005+ return err;
4006+}
4007+
4008+/* ---------------------------------------------------------------------- */
4009+
027c5e7a
AM
4010+static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
4011+{
4012+ int err;
5afbbe0d 4013+ aufs_bindex_t btop, bbot;
027c5e7a
AM
4014+ struct aufs_ibusy ibusy;
4015+ struct inode *inode, *h_inode;
4016+
4017+ err = -EPERM;
4018+ if (unlikely(!capable(CAP_SYS_ADMIN)))
4019+ goto out;
4020+
4021+ err = copy_from_user(&ibusy, arg, sizeof(ibusy));
4022+ if (!err)
ba1aed25
AM
4023+ /* VERIFY_WRITE */
4024+ err = !access_ok(&arg->h_ino, sizeof(arg->h_ino));
027c5e7a
AM
4025+ if (unlikely(err)) {
4026+ err = -EFAULT;
4027+ AuTraceErr(err);
4028+ goto out;
4029+ }
4030+
4031+ err = -EINVAL;
4032+ si_read_lock(sb, AuLock_FLUSH);
5afbbe0d 4033+ if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
027c5e7a
AM
4034+ goto out_unlock;
4035+
4036+ err = 0;
4037+ ibusy.h_ino = 0; /* invalid */
4038+ inode = ilookup(sb, ibusy.ino);
4039+ if (!inode
4040+ || inode->i_ino == AUFS_ROOT_INO
5afbbe0d 4041+ || au_is_bad_inode(inode))
027c5e7a
AM
4042+ goto out_unlock;
4043+
4044+ ii_read_lock_child(inode);
5afbbe0d
AM
4045+ btop = au_ibtop(inode);
4046+ bbot = au_ibbot(inode);
4047+ if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
027c5e7a 4048+ h_inode = au_h_iptr(inode, ibusy.bindex);
5afbbe0d 4049+ if (h_inode && au_test_ibusy(inode, btop, bbot))
027c5e7a
AM
4050+ ibusy.h_ino = h_inode->i_ino;
4051+ }
4052+ ii_read_unlock(inode);
4053+ iput(inode);
4054+
4055+out_unlock:
4056+ si_read_unlock(sb);
4057+ if (!err) {
4058+ err = __put_user(ibusy.h_ino, &arg->h_ino);
4059+ if (unlikely(err)) {
4060+ err = -EFAULT;
4061+ AuTraceErr(err);
4062+ }
4063+ }
4064+out:
4065+ return err;
4066+}
4067+
4068+long au_ibusy_ioctl(struct file *file, unsigned long arg)
4069+{
2000de60 4070+ return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
027c5e7a
AM
4071+}
4072+
4073+#ifdef CONFIG_COMPAT
4074+long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
4075+{
2000de60 4076+ return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
027c5e7a
AM
4077+}
4078+#endif
4079+
4080+/* ---------------------------------------------------------------------- */
4081+
1facf9fc 4082+/*
4083+ * change a branch permission
4084+ */
4085+
dece6358
AM
4086+static void au_warn_ima(void)
4087+{
4088+#ifdef CONFIG_IMA
1308ab2a 4089+ /* since it doesn't support mark_files_ro() */
027c5e7a 4090+ AuWarn1("RW -> RO makes IMA to produce wrong message\n");
dece6358
AM
4091+#endif
4092+}
4093+
1facf9fc 4094+static int do_need_sigen_inc(int a, int b)
4095+{
4096+ return au_br_whable(a) && !au_br_whable(b);
4097+}
4098+
4099+static int need_sigen_inc(int old, int new)
4100+{
4101+ return do_need_sigen_inc(old, new)
4102+ || do_need_sigen_inc(new, old);
4103+}
4104+
4105+static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
4106+{
7f207e10 4107+ int err, do_warn;
027c5e7a 4108+ unsigned int mnt_flags;
7f207e10 4109+ unsigned long long ull, max;
e49829fe 4110+ aufs_bindex_t br_id;
38d290e6 4111+ unsigned char verbose, writer;
7f207e10 4112+ struct file *file, *hf, **array;
e49829fe 4113+ struct au_hfile *hfile;
eca34b5c 4114+ struct inode *h_inode;
1facf9fc 4115+
027c5e7a
AM
4116+ mnt_flags = au_mntflags(sb);
4117+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
4118+
7f207e10
AM
4119+ array = au_farray_alloc(sb, &max);
4120+ err = PTR_ERR(array);
4121+ if (IS_ERR(array))
1facf9fc 4122+ goto out;
4123+
7f207e10 4124+ do_warn = 0;
e49829fe 4125+ br_id = au_sbr_id(sb, bindex);
7f207e10
AM
4126+ for (ull = 0; ull < max; ull++) {
4127+ file = array[ull];
076b876e
AM
4128+ if (unlikely(!file))
4129+ break;
1facf9fc 4130+
523b37e3 4131+ /* AuDbg("%pD\n", file); */
1facf9fc 4132+ fi_read_lock(file);
4133+ if (unlikely(au_test_mmapped(file))) {
4134+ err = -EBUSY;
523b37e3 4135+ AuVerbose(verbose, "mmapped %pD\n", file);
7f207e10 4136+ AuDbgFile(file);
1facf9fc 4137+ FiMustNoWaiters(file);
4138+ fi_read_unlock(file);
7f207e10 4139+ goto out_array;
1facf9fc 4140+ }
4141+
e49829fe
JR
4142+ hfile = &au_fi(file)->fi_htop;
4143+ hf = hfile->hf_file;
7e9cd9fe 4144+ if (!d_is_reg(file->f_path.dentry)
1facf9fc 4145+ || !(file->f_mode & FMODE_WRITE)
e49829fe 4146+ || hfile->hf_br->br_id != br_id
7f207e10
AM
4147+ || !(hf->f_mode & FMODE_WRITE))
4148+ array[ull] = NULL;
4149+ else {
4150+ do_warn = 1;
4151+ get_file(file);
1facf9fc 4152+ }
4153+
1facf9fc 4154+ FiMustNoWaiters(file);
4155+ fi_read_unlock(file);
7f207e10
AM
4156+ fput(file);
4157+ }
1facf9fc 4158+
4159+ err = 0;
7f207e10 4160+ if (do_warn)
dece6358 4161+ au_warn_ima();
7f207e10
AM
4162+
4163+ for (ull = 0; ull < max; ull++) {
4164+ file = array[ull];
4165+ if (!file)
4166+ continue;
4167+
1facf9fc 4168+ /* todo: already flushed? */
523b37e3
AM
4169+ /*
4170+ * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4171+ * approach which resets f_mode and calls mnt_drop_write() and
4172+ * file_release_write() for each file, because the branch
4173+ * attribute in aufs world is totally different from the native
4174+ * fs rw/ro mode.
22319442 4175+ */
7f207e10
AM
4176+ /* fi_read_lock(file); */
4177+ hfile = &au_fi(file)->fi_htop;
4178+ hf = hfile->hf_file;
4179+ /* fi_read_unlock(file); */
027c5e7a 4180+ spin_lock(&hf->f_lock);
38d290e6
JR
4181+ writer = !!(hf->f_mode & FMODE_WRITER);
4182+ hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
027c5e7a 4183+ spin_unlock(&hf->f_lock);
38d290e6 4184+ if (writer) {
eca34b5c 4185+ h_inode = file_inode(hf);
42b5c33a
AM
4186+ if (hf->f_mode & FMODE_READ)
4187+ i_readcount_inc(h_inode);
eca34b5c 4188+ put_write_access(h_inode);
c06a8ce3 4189+ __mnt_drop_write(hf->f_path.mnt);
1facf9fc 4190+ }
4191+ }
4192+
7f207e10
AM
4193+out_array:
4194+ au_farray_free(array, max);
4f0767ce 4195+out:
7f207e10 4196+ AuTraceErr(err);
1facf9fc 4197+ return err;
4198+}
4199+
4200+int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
7f207e10 4201+ int *do_refresh)
1facf9fc 4202+{
4203+ int err, rerr;
4204+ aufs_bindex_t bindex;
4205+ struct dentry *root;
4206+ struct au_branch *br;
076b876e 4207+ struct au_br_fhsm *bf;
1facf9fc 4208+
4209+ root = sb->s_root;
1facf9fc 4210+ bindex = au_find_dbindex(root, mod->h_root);
4211+ if (bindex < 0) {
4212+ if (remount)
4213+ return 0; /* success */
4214+ err = -ENOENT;
4a4d8108 4215+ pr_err("%s no such branch\n", mod->path);
1facf9fc 4216+ goto out;
4217+ }
4218+ AuDbg("bindex b%d\n", bindex);
4219+
5527c038 4220+ err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
1facf9fc 4221+ if (unlikely(err))
4222+ goto out;
4223+
4224+ br = au_sbr(sb, bindex);
86dc4139 4225+ AuDebugOn(mod->h_root != au_br_dentry(br));
1facf9fc 4226+ if (br->br_perm == mod->perm)
4227+ return 0; /* success */
4228+
076b876e
AM
4229+ /* pre-allocate for non-fhsm --> fhsm */
4230+ bf = NULL;
4231+ if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4232+ err = au_fhsm_br_alloc(br);
4233+ if (unlikely(err))
4234+ goto out;
4235+ bf = br->br_fhsm;
4236+ br->br_fhsm = NULL;
4237+ }
4238+
1facf9fc 4239+ if (au_br_writable(br->br_perm)) {
4240+ /* remove whiteout base */
86dc4139 4241+ err = au_br_init_wh(sb, br, mod->perm);
1facf9fc 4242+ if (unlikely(err))
076b876e 4243+ goto out_bf;
1facf9fc 4244+
4245+ if (!au_br_writable(mod->perm)) {
4246+ /* rw --> ro, file might be mmapped */
4247+ DiMustNoWaiters(root);
5527c038 4248+ IiMustNoWaiters(d_inode(root));
1facf9fc 4249+ di_write_unlock(root);
4250+ err = au_br_mod_files_ro(sb, bindex);
4251+ /* aufs_write_lock() calls ..._child() */
4252+ di_write_lock_child(root);
4253+
4254+ if (unlikely(err)) {
4255+ rerr = -ENOMEM;
be52b249 4256+ br->br_wbr = kzalloc(sizeof(*br->br_wbr),
1facf9fc 4257+ GFP_NOFS);
86dc4139
AM
4258+ if (br->br_wbr)
4259+ rerr = au_wbr_init(br, sb, br->br_perm);
1facf9fc 4260+ if (unlikely(rerr)) {
4261+ AuIOErr("nested error %d (%d)\n",
4262+ rerr, err);
4263+ br->br_perm = mod->perm;
4264+ }
4265+ }
4266+ }
4267+ } else if (au_br_writable(mod->perm)) {
4268+ /* ro --> rw */
4269+ err = -ENOMEM;
be52b249 4270+ br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
1facf9fc 4271+ if (br->br_wbr) {
86dc4139 4272+ err = au_wbr_init(br, sb, mod->perm);
1facf9fc 4273+ if (unlikely(err)) {
9f237c51 4274+ au_kfree_rcu(br->br_wbr);
1facf9fc 4275+ br->br_wbr = NULL;
4276+ }
4277+ }
4278+ }
076b876e
AM
4279+ if (unlikely(err))
4280+ goto out_bf;
4281+
4282+ if (au_br_fhsm(br->br_perm)) {
4283+ if (!au_br_fhsm(mod->perm)) {
4284+ /* fhsm --> non-fhsm */
4285+ au_br_fhsm_fin(br->br_fhsm);
9f237c51 4286+ au_kfree_rcu(br->br_fhsm);
076b876e
AM
4287+ br->br_fhsm = NULL;
4288+ }
4289+ } else if (au_br_fhsm(mod->perm))
4290+ /* non-fhsm --> fhsm */
4291+ br->br_fhsm = bf;
4292+
076b876e
AM
4293+ *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4294+ br->br_perm = mod->perm;
4295+ goto out; /* success */
1facf9fc 4296+
076b876e 4297+out_bf:
9f237c51 4298+ au_kfree_try_rcu(bf);
076b876e
AM
4299+out:
4300+ AuTraceErr(err);
4301+ return err;
4302+}
4303+
4304+/* ---------------------------------------------------------------------- */
4305+
4306+int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4307+{
4308+ int err;
4309+ struct kstatfs kstfs;
4310+
4311+ err = vfs_statfs(&br->br_path, &kstfs);
1facf9fc 4312+ if (!err) {
076b876e
AM
4313+ stfs->f_blocks = kstfs.f_blocks;
4314+ stfs->f_bavail = kstfs.f_bavail;
4315+ stfs->f_files = kstfs.f_files;
4316+ stfs->f_ffree = kstfs.f_ffree;
1facf9fc 4317+ }
4318+
1facf9fc 4319+ return err;
4320+}
7f207e10 4321diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
eca34b5c 4322--- /usr/share/empty/fs/aufs/branch.h 1970-01-01 01:00:00.000000000 +0100
631230db 4323+++ linux/fs/aufs/branch.h 2023-10-31 09:31:04.196547417 +0100
0b2a12c6 4324@@ -0,0 +1,375 @@
062440b3 4325+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 4326+/*
0dcfbb52 4327+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 4328+ *
0dcfbb52 4329+ * This program is free software; you can redistribute it and/or modify
1facf9fc 4330+ * it under the terms of the GNU General Public License as published by
4331+ * the Free Software Foundation; either version 2 of the License, or
4332+ * (at your option) any later version.
dece6358
AM
4333+ *
4334+ * This program is distributed in the hope that it will be useful,
4335+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
4336+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4337+ * GNU General Public License for more details.
4338+ *
4339+ * You should have received a copy of the GNU General Public License
523b37e3 4340+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 4341+ */
4342+
4343+/*
4344+ * branch filesystems and xino for them
4345+ */
4346+
4347+#ifndef __AUFS_BRANCH_H__
4348+#define __AUFS_BRANCH_H__
4349+
4350+#ifdef __KERNEL__
4351+
1facf9fc 4352+#include <linux/mount.h>
8b6a4947 4353+#include "dirren.h"
4a4d8108 4354+#include "dynop.h"
acd2b654 4355+#include "lcnt.h"
1facf9fc 4356+#include "rwsem.h"
4357+#include "super.h"
4358+
4359+/* ---------------------------------------------------------------------- */
4360+
4361+/* a xino file */
062440b3 4362+struct au_xino {
acd2b654
AM
4363+ struct file **xi_file;
4364+ unsigned int xi_nfile;
4365+
521ced18
JR
4366+ struct {
4367+ spinlock_t spin;
4368+ ino_t *array;
4369+ int total;
4370+ /* reserved for future use */
4371+ /* unsigned long *bitmap; */
4372+ wait_queue_head_t wqh;
4373+ } xi_nondir;
1facf9fc 4374+
acd2b654 4375+ struct mutex xi_mtx; /* protects xi_file array */
9f237c51 4376+ struct hlist_bl_head xi_writing;
acd2b654 4377+
062440b3 4378+ atomic_t xi_truncating;
1facf9fc 4379+
062440b3 4380+ struct kref xi_kref;
1facf9fc 4381+};
4382+
076b876e
AM
4383+/* File-based Hierarchical Storage Management */
4384+struct au_br_fhsm {
4385+#ifdef CONFIG_AUFS_FHSM
4386+ struct mutex bf_lock;
4387+ unsigned long bf_jiffy;
4388+ struct aufs_stfs bf_stfs;
4389+ int bf_readable;
4390+#endif
4391+};
4392+
1facf9fc 4393+/* members for writable branch only */
4394+enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4395+struct au_wbr {
dece6358 4396+ struct au_rwsem wbr_wh_rwsem;
1facf9fc 4397+ struct dentry *wbr_wh[AuBrWh_Last];
4a4d8108 4398+ atomic_t wbr_wh_running;
1facf9fc 4399+#define wbr_whbase wbr_wh[AuBrWh_BASE] /* whiteout base */
4400+#define wbr_plink wbr_wh[AuBrWh_PLINK] /* pseudo-link dir */
4401+#define wbr_orph wbr_wh[AuBrWh_ORPH] /* dir for orphans */
4402+
4403+ /* mfs mode */
4404+ unsigned long long wbr_bytes;
4405+};
4406+
4a4d8108
AM
4407+/* ext2 has 3 types of operations at least, ext3 has 4 */
4408+#define AuBrDynOp (AuDyLast * 4)
4409+
1716fcea
AM
4410+#ifdef CONFIG_AUFS_HFSNOTIFY
4411+/* support for asynchronous destruction */
4412+struct au_br_hfsnotify {
4413+ struct fsnotify_group *hfsn_group;
4414+};
4415+#endif
4416+
392086de
AM
4417+/* sysfs entries */
4418+struct au_brsysfs {
4419+ char name[16];
4420+ struct attribute attr;
4421+};
4422+
4423+enum {
4424+ AuBrSysfs_BR,
4425+ AuBrSysfs_BRID,
4426+ AuBrSysfs_Last
4427+};
4428+
1facf9fc 4429+/* protected by superblock rwsem */
4430+struct au_branch {
062440b3 4431+ struct au_xino *br_xino;
1facf9fc 4432+
4433+ aufs_bindex_t br_id;
4434+
4435+ int br_perm;
86dc4139 4436+ struct path br_path;
4a4d8108
AM
4437+ spinlock_t br_dykey_lock;
4438+ struct au_dykey *br_dykey[AuBrDynOp];
acd2b654
AM
4439+ au_lcnt_t br_nfiles; /* opened files */
4440+ au_lcnt_t br_count; /* in-use for other */
1facf9fc 4441+
4442+ struct au_wbr *br_wbr;
076b876e 4443+ struct au_br_fhsm *br_fhsm;
1facf9fc 4444+
027c5e7a 4445+#ifdef CONFIG_AUFS_HFSNOTIFY
1716fcea 4446+ struct au_br_hfsnotify *br_hfsn;
027c5e7a
AM
4447+#endif
4448+
1facf9fc 4449+#ifdef CONFIG_SYSFS
392086de
AM
4450+ /* entries under sysfs per mount-point */
4451+ struct au_brsysfs br_sysfs[AuBrSysfs_Last];
1facf9fc 4452+#endif
8b6a4947 4453+
062440b3
AM
4454+#ifdef CONFIG_DEBUG_FS
4455+ struct dentry *br_dbgaufs; /* xino */
4456+#endif
4457+
8b6a4947 4458+ struct au_dr_br br_dirren;
1facf9fc 4459+};
4460+
4461+/* ---------------------------------------------------------------------- */
4462+
86dc4139
AM
4463+static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4464+{
4465+ return br->br_path.mnt;
4466+}
4467+
4468+static inline struct dentry *au_br_dentry(struct au_branch *br)
4469+{
4470+ return br->br_path.dentry;
4471+}
4472+
5db017eb 4473+static inline struct mnt_idmap *au_br_idmap(struct au_branch *br)
0b2a12c6 4474+{
5db017eb 4475+ return mnt_idmap(br->br_path.mnt);
0b2a12c6
JR
4476+}
4477+
86dc4139
AM
4478+static inline struct super_block *au_br_sb(struct au_branch *br)
4479+{
4480+ return au_br_mnt(br)->mnt_sb;
4481+}
4482+
1facf9fc 4483+static inline int au_br_rdonly(struct au_branch *br)
4484+{
8b6a4947 4485+ return (sb_rdonly(au_br_sb(br))
1facf9fc 4486+ || !au_br_writable(br->br_perm))
4487+ ? -EROFS : 0;
4488+}
4489+
4a4d8108 4490+static inline int au_br_hnotifyable(int brperm __maybe_unused)
1facf9fc 4491+{
4a4d8108 4492+#ifdef CONFIG_AUFS_HNOTIFY
1e00d052 4493+ return !(brperm & AuBrPerm_RR);
1facf9fc 4494+#else
4495+ return 0;
4496+#endif
4497+}
4498+
b912730e
AM
4499+static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4500+{
4501+ int err, exec_flag;
4502+
4503+ err = 0;
4504+ exec_flag = oflag & __FMODE_EXEC;
79b8bda9 4505+ if (unlikely(exec_flag && path_noexec(&br->br_path)))
b912730e
AM
4506+ err = -EACCES;
4507+
4508+ return err;
4509+}
4510+
062440b3
AM
4511+static inline void au_xino_get(struct au_branch *br)
4512+{
4513+ struct au_xino *xi;
4514+
4515+ xi = br->br_xino;
4516+ if (xi)
4517+ kref_get(&xi->xi_kref);
4518+}
4519+
4520+static inline int au_xino_count(struct au_branch *br)
4521+{
4522+ int v;
4523+ struct au_xino *xi;
4524+
4525+ v = 0;
4526+ xi = br->br_xino;
4527+ if (xi)
4528+ v = kref_read(&xi->xi_kref);
4529+
4530+ return v;
4531+}
4532+
1facf9fc 4533+/* ---------------------------------------------------------------------- */
4534+
4535+/* branch.c */
4536+struct au_sbinfo;
4537+void au_br_free(struct au_sbinfo *sinfo);
4538+int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4539+struct au_opt_add;
4540+int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4541+struct au_opt_del;
4542+int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
027c5e7a
AM
4543+long au_ibusy_ioctl(struct file *file, unsigned long arg);
4544+#ifdef CONFIG_COMPAT
4545+long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4546+#endif
1facf9fc 4547+struct au_opt_mod;
4548+int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
7f207e10 4549+ int *do_refresh);
076b876e
AM
4550+struct aufs_stfs;
4551+int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
1facf9fc 4552+
4553+/* xino.c */
4554+static const loff_t au_loff_max = LLONG_MAX;
4555+
acd2b654 4556+aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry);
83b672a5
AM
4557+struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
4558+ int wbrtop);
062440b3
AM
4559+struct file *au_xino_create2(struct super_block *sb, struct path *base,
4560+ struct file *copy_src);
acd2b654
AM
4561+struct au_xi_new {
4562+ struct au_xino *xi; /* switch between xino and xigen */
4563+ int idx;
4564+ struct path *base;
4565+ struct file *copy_src;
4566+};
4567+struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew);
062440b3
AM
4568+
4569+int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4570+ ino_t *ino);
4571+int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4572+ ino_t ino);
e37dd06a
AM
4573+ssize_t xino_fread(struct file *file, void *buf, size_t size, loff_t *pos);
4574+ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos);
062440b3
AM
4575+
4576+int au_xib_trunc(struct super_block *sb);
acd2b654 4577+int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin);
1facf9fc 4578+
acd2b654 4579+struct au_xino *au_xino_alloc(unsigned int nfile);
062440b3 4580+int au_xino_put(struct au_branch *br);
acd2b654 4581+struct file *au_xino_file1(struct au_xino *xi);
062440b3 4582+
1facf9fc 4583+struct au_opt_xino;
1facf9fc 4584+void au_xino_clr(struct super_block *sb);
062440b3 4585+int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount);
1facf9fc 4586+struct file *au_xino_def(struct super_block *sb);
062440b3
AM
4587+int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4588+ struct path *base);
4589+
4590+ino_t au_xino_new_ino(struct super_block *sb);
4591+void au_xino_delete_inode(struct inode *inode, const int unlinked);
1facf9fc 4592+
521ced18
JR
4593+void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
4594+ ino_t h_ino, int idx);
4595+int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4596+ int *idx);
4597+
062440b3
AM
4598+int au_xino_path(struct seq_file *seq, struct file *file);
4599+
1facf9fc 4600+/* ---------------------------------------------------------------------- */
4601+
acd2b654
AM
4602+/* @idx is signed to accept -1 meaning the first file */
4603+static inline struct file *au_xino_file(struct au_xino *xi, int idx)
4604+{
4605+ struct file *file;
4606+
4607+ file = NULL;
4608+ if (!xi)
4609+ goto out;
4610+
4611+ if (idx >= 0) {
4612+ if (idx < xi->xi_nfile)
4613+ file = xi->xi_file[idx];
4614+ } else
4615+ file = au_xino_file1(xi);
4616+
4617+out:
4618+ return file;
4619+}
4620+
4621+/* ---------------------------------------------------------------------- */
4622+
1facf9fc 4623+/* Superblock to branch */
4624+static inline
4625+aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4626+{
4627+ return au_sbr(sb, bindex)->br_id;
4628+}
4629+
4630+static inline
4631+struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4632+{
86dc4139 4633+ return au_br_mnt(au_sbr(sb, bindex));
1facf9fc 4634+}
4635+
4636+static inline
5db017eb 4637+struct mnt_idmap *au_sbr_idmap(struct super_block *sb, aufs_bindex_t bindex)
0b2a12c6 4638+{
5db017eb 4639+ return au_br_idmap(au_sbr(sb, bindex));
0b2a12c6
JR
4640+}
4641+
4642+static inline
1facf9fc 4643+struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4644+{
86dc4139 4645+ return au_br_sb(au_sbr(sb, bindex));
1facf9fc 4646+}
4647+
1facf9fc 4648+static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4649+{
4650+ return au_sbr(sb, bindex)->br_perm;
4651+}
4652+
4653+static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4654+{
4655+ return au_br_whable(au_sbr_perm(sb, bindex));
4656+}
4657+
4658+/* ---------------------------------------------------------------------- */
4659+
8b6a4947
AM
4660+#define wbr_wh_read_lock(wbr) au_rw_read_lock(&(wbr)->wbr_wh_rwsem)
4661+#define wbr_wh_write_lock(wbr) au_rw_write_lock(&(wbr)->wbr_wh_rwsem)
4662+#define wbr_wh_read_trylock(wbr) au_rw_read_trylock(&(wbr)->wbr_wh_rwsem)
4663+#define wbr_wh_write_trylock(wbr) au_rw_write_trylock(&(wbr)->wbr_wh_rwsem)
1facf9fc 4664+/*
8b6a4947
AM
4665+#define wbr_wh_read_trylock_nested(wbr) \
4666+ au_rw_read_trylock_nested(&(wbr)->wbr_wh_rwsem)
4667+#define wbr_wh_write_trylock_nested(wbr) \
4668+ au_rw_write_trylock_nested(&(wbr)->wbr_wh_rwsem)
4669+*/
1facf9fc 4670+
8b6a4947
AM
4671+#define wbr_wh_read_unlock(wbr) au_rw_read_unlock(&(wbr)->wbr_wh_rwsem)
4672+#define wbr_wh_write_unlock(wbr) au_rw_write_unlock(&(wbr)->wbr_wh_rwsem)
4673+#define wbr_wh_downgrade_lock(wbr) au_rw_dgrade_lock(&(wbr)->wbr_wh_rwsem)
4674+
4675+#define WbrWhMustNoWaiters(wbr) AuRwMustNoWaiters(&(wbr)->wbr_wh_rwsem)
4676+#define WbrWhMustAnyLock(wbr) AuRwMustAnyLock(&(wbr)->wbr_wh_rwsem)
4677+#define WbrWhMustWriteLock(wbr) AuRwMustWriteLock(&(wbr)->wbr_wh_rwsem)
dece6358 4678+
076b876e
AM
4679+/* ---------------------------------------------------------------------- */
4680+
4681+#ifdef CONFIG_AUFS_FHSM
4682+static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4683+{
4684+ mutex_init(&brfhsm->bf_lock);
4685+ brfhsm->bf_jiffy = 0;
4686+ brfhsm->bf_readable = 0;
4687+}
4688+
4689+static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4690+{
4691+ mutex_destroy(&brfhsm->bf_lock);
4692+}
4693+#else
4694+AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4695+AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4696+#endif
4697+
1facf9fc 4698+#endif /* __KERNEL__ */
4699+#endif /* __AUFS_BRANCH_H__ */
7f207e10 4700diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
eca34b5c 4701--- /usr/share/empty/fs/aufs/conf.mk 1970-01-01 01:00:00.000000000 +0100
594d0238 4702+++ linux/fs/aufs/conf.mk 2022-11-05 23:02:18.959222617 +0100
2121bcd9
AM
4703@@ -0,0 +1,40 @@
4704+# SPDX-License-Identifier: GPL-2.0
4a4d8108
AM
4705+
4706+AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4707+
4708+define AuConf
4709+ifdef ${1}
4710+AuConfStr += ${1}=${${1}}
4711+endif
4712+endef
4713+
b752ccd1 4714+AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
e49829fe 4715+ SBILIST \
7f207e10 4716+ HNOTIFY HFSNOTIFY \
4a4d8108 4717+ EXPORT INO_T_64 \
c1595e42 4718+ XATTR \
076b876e 4719+ FHSM \
4a4d8108 4720+ RDU \
8b6a4947 4721+ DIRREN \
4a4d8108
AM
4722+ SHWH \
4723+ BR_RAMFS \
4724+ BR_FUSE POLL \
4725+ BR_HFSPLUS \
4726+ BDEV_LOOP \
b752ccd1
AM
4727+ DEBUG MAGIC_SYSRQ
4728+$(foreach i, ${AuConfAll}, \
4a4d8108
AM
4729+ $(eval $(call AuConf,CONFIG_AUFS_${i})))
4730+
4731+AuConfName = ${obj}/conf.str
4732+${AuConfName}.tmp: FORCE
4733+ @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4734+${AuConfName}: ${AuConfName}.tmp
4735+ @diff -q $< $@ > /dev/null 2>&1 || { \
4736+ echo ' GEN ' $@; \
4737+ cp -p $< $@; \
4738+ }
4739+FORCE:
4740+clean-files += ${AuConfName} ${AuConfName}.tmp
4741+${obj}/sysfs.o: ${AuConfName}
b752ccd1
AM
4742+
4743+-include ${srctree}/${src}/conf_priv.mk
7f207e10 4744diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
eca34b5c 4745--- /usr/share/empty/fs/aufs/cpup.c 1970-01-01 01:00:00.000000000 +0100
631230db 4746+++ linux/fs/aufs/cpup.c 2023-10-31 09:31:04.196547417 +0100
758e9dad 4747@@ -0,0 +1,1459 @@
cd7a4cd9 4748+// SPDX-License-Identifier: GPL-2.0
1facf9fc 4749+/*
0dcfbb52 4750+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 4751+ *
0dcfbb52 4752+ * This program is free software; you can redistribute it and/or modify
1facf9fc 4753+ * it under the terms of the GNU General Public License as published by
4754+ * the Free Software Foundation; either version 2 of the License, or
4755+ * (at your option) any later version.
dece6358
AM
4756+ *
4757+ * This program is distributed in the hope that it will be useful,
4758+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
4759+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4760+ * GNU General Public License for more details.
4761+ *
4762+ * You should have received a copy of the GNU General Public License
523b37e3 4763+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 4764+ */
4765+
4766+/*
4767+ * copy-up functions, see wbr_policy.c for copy-down
4768+ */
4769+
4770+#include <linux/fs_stack.h>
dece6358 4771+#include <linux/mm.h>
8cdd5066 4772+#include <linux/task_work.h>
1facf9fc 4773+#include "aufs.h"
4774+
86dc4139 4775+void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
1facf9fc 4776+{
4777+ const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
367653fa 4778+ | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
1facf9fc 4779+
86dc4139
AM
4780+ BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
4781+
4782+ dst->i_flags |= iflags & ~mask;
1facf9fc 4783+ if (au_test_fs_notime(dst->i_sb))
4784+ dst->i_flags |= S_NOATIME | S_NOCMTIME;
4785+}
4786+
4787+void au_cpup_attr_timesizes(struct inode *inode)
4788+{
4789+ struct inode *h_inode;
4790+
5afbbe0d 4791+ h_inode = au_h_iptr(inode, au_ibtop(inode));
1facf9fc 4792+ fsstack_copy_attr_times(inode, h_inode);
4a4d8108 4793+ fsstack_copy_inode_size(inode, h_inode);
1facf9fc 4794+}
4795+
4796+void au_cpup_attr_nlink(struct inode *inode, int force)
4797+{
4798+ struct inode *h_inode;
4799+ struct super_block *sb;
5afbbe0d 4800+ aufs_bindex_t bindex, bbot;
1facf9fc 4801+
4802+ sb = inode->i_sb;
5afbbe0d 4803+ bindex = au_ibtop(inode);
1facf9fc 4804+ h_inode = au_h_iptr(inode, bindex);
4805+ if (!force
4806+ && !S_ISDIR(h_inode->i_mode)
4807+ && au_opt_test(au_mntflags(sb), PLINK)
4808+ && au_plink_test(inode))
4809+ return;
4810+
7eafdf33
AM
4811+ /*
4812+ * 0 can happen in revalidating.
38d290e6
JR
4813+ * h_inode->i_mutex may not be held here, but it is harmless since once
4814+ * i_nlink reaches 0, it will never become positive except O_TMPFILE
4815+ * case.
4816+ * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
4817+ * the incorrect link count.
7eafdf33 4818+ */
92d182d2 4819+ set_nlink(inode, h_inode->i_nlink);
1facf9fc 4820+
4821+ /*
4822+ * fewer nlink makes find(1) noisy, but larger nlink doesn't.
4823+ * it may includes whplink directory.
4824+ */
4825+ if (S_ISDIR(h_inode->i_mode)) {
5afbbe0d
AM
4826+ bbot = au_ibbot(inode);
4827+ for (bindex++; bindex <= bbot; bindex++) {
1facf9fc 4828+ h_inode = au_h_iptr(inode, bindex);
4829+ if (h_inode)
4830+ au_add_nlink(inode, h_inode);
4831+ }
4832+ }
4833+}
4834+
4835+void au_cpup_attr_changeable(struct inode *inode)
4836+{
4837+ struct inode *h_inode;
4838+
5afbbe0d 4839+ h_inode = au_h_iptr(inode, au_ibtop(inode));
1facf9fc 4840+ inode->i_mode = h_inode->i_mode;
4841+ inode->i_uid = h_inode->i_uid;
4842+ inode->i_gid = h_inode->i_gid;
4843+ au_cpup_attr_timesizes(inode);
86dc4139 4844+ au_cpup_attr_flags(inode, h_inode->i_flags);
1facf9fc 4845+}
4846+
4847+void au_cpup_igen(struct inode *inode, struct inode *h_inode)
4848+{
4849+ struct au_iinfo *iinfo = au_ii(inode);
4850+
1308ab2a 4851+ IiMustWriteLock(inode);
4852+
1facf9fc 4853+ iinfo->ii_higen = h_inode->i_generation;
4854+ iinfo->ii_hsb1 = h_inode->i_sb;
4855+}
4856+
4857+void au_cpup_attr_all(struct inode *inode, int force)
4858+{
4859+ struct inode *h_inode;
4860+
5afbbe0d 4861+ h_inode = au_h_iptr(inode, au_ibtop(inode));
1facf9fc 4862+ au_cpup_attr_changeable(inode);
4863+ if (inode->i_nlink > 0)
4864+ au_cpup_attr_nlink(inode, force);
4865+ inode->i_rdev = h_inode->i_rdev;
4866+ inode->i_blkbits = h_inode->i_blkbits;
4867+ au_cpup_igen(inode, h_inode);
4868+}
4869+
4870+/* ---------------------------------------------------------------------- */
4871+
4872+/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
4873+
4874+/* keep the timestamps of the parent dir when cpup */
4875+void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
4876+ struct path *h_path)
4877+{
4878+ struct inode *h_inode;
4879+
4880+ dt->dt_dentry = dentry;
4881+ dt->dt_h_path = *h_path;
5527c038 4882+ h_inode = d_inode(h_path->dentry);
1facf9fc 4883+ dt->dt_atime = h_inode->i_atime;
4884+ dt->dt_mtime = h_inode->i_mtime;
4885+ /* smp_mb(); */
4886+}
4887+
4888+void au_dtime_revert(struct au_dtime *dt)
4889+{
4890+ struct iattr attr;
4891+ int err;
4892+
4893+ attr.ia_atime = dt->dt_atime;
4894+ attr.ia_mtime = dt->dt_mtime;
4895+ attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
4896+ | ATTR_ATIME | ATTR_ATIME_SET;
4897+
523b37e3
AM
4898+ /* no delegation since this is a directory */
4899+ err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
1facf9fc 4900+ if (unlikely(err))
0c3ec466 4901+ pr_warn("restoring timestamps failed(%d). ignored\n", err);
1facf9fc 4902+}
4903+
4904+/* ---------------------------------------------------------------------- */
4905+
86dc4139
AM
4906+/* internal use only */
4907+struct au_cpup_reg_attr {
4908+ int valid;
4909+ struct kstat st;
4910+ unsigned int iflags; /* inode->i_flags */
4911+};
4912+
1facf9fc 4913+static noinline_for_stack
0b2a12c6 4914+int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct path *h_src,
86dc4139 4915+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 4916+{
c1595e42 4917+ int err, sbits, icex;
7e9cd9fe
AM
4918+ unsigned int mnt_flags;
4919+ unsigned char verbose;
1facf9fc 4920+ struct iattr ia;
4921+ struct path h_path;
1308ab2a 4922+ struct inode *h_isrc, *h_idst;
86dc4139 4923+ struct kstat *h_st;
c1595e42 4924+ struct au_branch *br;
1facf9fc 4925+
c1595e42
JR
4926+ br = au_sbr(dst->d_sb, bindex);
4927+ h_path.mnt = au_br_mnt(br);
0b2a12c6
JR
4928+ h_path.dentry = au_h_dptr(dst, bindex);
4929+ h_idst = d_inode(h_path.dentry);
4930+ h_isrc = d_inode(h_src->dentry);
1308ab2a 4931+ ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
1facf9fc 4932+ | ATTR_ATIME | ATTR_MTIME
4933+ | ATTR_ATIME_SET | ATTR_MTIME_SET;
86dc4139
AM
4934+ if (h_src_attr && h_src_attr->valid) {
4935+ h_st = &h_src_attr->st;
4936+ ia.ia_uid = h_st->uid;
4937+ ia.ia_gid = h_st->gid;
4938+ ia.ia_atime = h_st->atime;
4939+ ia.ia_mtime = h_st->mtime;
4940+ if (h_idst->i_mode != h_st->mode
4941+ && !S_ISLNK(h_idst->i_mode)) {
4942+ ia.ia_valid |= ATTR_MODE;
4943+ ia.ia_mode = h_st->mode;
4944+ }
4945+ sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
4946+ au_cpup_attr_flags(h_idst, h_src_attr->iflags);
4947+ } else {
4948+ ia.ia_uid = h_isrc->i_uid;
4949+ ia.ia_gid = h_isrc->i_gid;
4950+ ia.ia_atime = h_isrc->i_atime;
4951+ ia.ia_mtime = h_isrc->i_mtime;
4952+ if (h_idst->i_mode != h_isrc->i_mode
4953+ && !S_ISLNK(h_idst->i_mode)) {
4954+ ia.ia_valid |= ATTR_MODE;
4955+ ia.ia_mode = h_isrc->i_mode;
4956+ }
4957+ sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
4958+ au_cpup_attr_flags(h_idst, h_isrc->i_flags);
1308ab2a 4959+ }
523b37e3
AM
4960+ /* no delegation since it is just created */
4961+ err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
1facf9fc 4962+
4963+ /* is this nfs only? */
4964+ if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
4965+ ia.ia_valid = ATTR_FORCE | ATTR_MODE;
4966+ ia.ia_mode = h_isrc->i_mode;
523b37e3 4967+ err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
1facf9fc 4968+ }
4969+
c1595e42 4970+ icex = br->br_perm & AuBrAttr_ICEX;
7e9cd9fe
AM
4971+ if (!err) {
4972+ mnt_flags = au_mntflags(dst->d_sb);
4973+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
0b2a12c6 4974+ err = au_cpup_xattr(&h_path, h_src, icex, verbose);
7e9cd9fe 4975+ }
c1595e42 4976+
1facf9fc 4977+ return err;
4978+}
4979+
4980+/* ---------------------------------------------------------------------- */
4981+
4982+static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
4983+ char *buf, unsigned long blksize)
4984+{
4985+ int err;
4986+ size_t sz, rbytes, wbytes;
4987+ unsigned char all_zero;
4988+ char *p, *zp;
febd17d6 4989+ struct inode *h_inode;
1facf9fc 4990+ /* reduce stack usage */
4991+ struct iattr *ia;
4992+
4993+ zp = page_address(ZERO_PAGE(0));
4994+ if (unlikely(!zp))
4995+ return -ENOMEM; /* possible? */
4996+
4997+ err = 0;
4998+ all_zero = 0;
4999+ while (len) {
5000+ AuDbg("len %lld\n", len);
5001+ sz = blksize;
5002+ if (len < blksize)
5003+ sz = len;
5004+
5005+ rbytes = 0;
5006+ /* todo: signal_pending? */
5007+ while (!rbytes || err == -EAGAIN || err == -EINTR) {
5008+ rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
5009+ err = rbytes;
5010+ }
5011+ if (unlikely(err < 0))
5012+ break;
5013+
5014+ all_zero = 0;
5015+ if (len >= rbytes && rbytes == blksize)
5016+ all_zero = !memcmp(buf, zp, rbytes);
5017+ if (!all_zero) {
5018+ wbytes = rbytes;
5019+ p = buf;
5020+ while (wbytes) {
5021+ size_t b;
5022+
5023+ b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
5024+ err = b;
5025+ /* todo: signal_pending? */
5026+ if (unlikely(err == -EAGAIN || err == -EINTR))
5027+ continue;
5028+ if (unlikely(err < 0))
5029+ break;
5030+ wbytes -= b;
5031+ p += b;
5032+ }
392086de
AM
5033+ if (unlikely(err < 0))
5034+ break;
1facf9fc 5035+ } else {
5036+ loff_t res;
5037+
5038+ AuLabel(hole);
5039+ res = vfsub_llseek(dst, rbytes, SEEK_CUR);
5040+ err = res;
5041+ if (unlikely(res < 0))
5042+ break;
5043+ }
5044+ len -= rbytes;
5045+ err = 0;
5046+ }
5047+
5048+ /* the last block may be a hole */
5049+ if (!err && all_zero) {
5050+ AuLabel(last hole);
5051+
5052+ err = 1;
2000de60 5053+ if (au_test_nfs(dst->f_path.dentry->d_sb)) {
1facf9fc 5054+ /* nfs requires this step to make last hole */
5055+ /* is this only nfs? */
5056+ do {
5057+ /* todo: signal_pending? */
5058+ err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
5059+ } while (err == -EAGAIN || err == -EINTR);
5060+ if (err == 1)
5061+ dst->f_pos--;
5062+ }
5063+
5064+ if (err == 1) {
5065+ ia = (void *)buf;
5066+ ia->ia_size = dst->f_pos;
5067+ ia->ia_valid = ATTR_SIZE | ATTR_FILE;
5068+ ia->ia_file = dst;
febd17d6
JR
5069+ h_inode = file_inode(dst);
5070+ inode_lock_nested(h_inode, AuLsc_I_CHILD2);
523b37e3
AM
5071+ /* no delegation since it is just created */
5072+ err = vfsub_notify_change(&dst->f_path, ia,
5073+ /*delegated*/NULL);
febd17d6 5074+ inode_unlock(h_inode);
1facf9fc 5075+ }
5076+ }
5077+
5078+ return err;
5079+}
5080+
5081+int au_copy_file(struct file *dst, struct file *src, loff_t len)
5082+{
5083+ int err;
5084+ unsigned long blksize;
5085+ unsigned char do_kfree;
5086+ char *buf;
9f237c51 5087+ struct super_block *h_sb;
1facf9fc 5088+
5089+ err = -ENOMEM;
9f237c51
AM
5090+ h_sb = file_inode(dst)->i_sb;
5091+ blksize = h_sb->s_blocksize;
1facf9fc 5092+ if (!blksize || PAGE_SIZE < blksize)
5093+ blksize = PAGE_SIZE;
5094+ AuDbg("blksize %lu\n", blksize);
5095+ do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
5096+ if (do_kfree)
5097+ buf = kmalloc(blksize, GFP_NOFS);
5098+ else
5099+ buf = (void *)__get_free_page(GFP_NOFS);
5100+ if (unlikely(!buf))
5101+ goto out;
5102+
5103+ if (len > (1 << 22))
5104+ AuDbg("copying a large file %lld\n", (long long)len);
5105+
5106+ src->f_pos = 0;
5107+ dst->f_pos = 0;
5108+ err = au_do_copy_file(dst, src, len, buf, blksize);
9f237c51
AM
5109+ if (do_kfree) {
5110+ AuDebugOn(!au_kfree_do_sz_test(blksize));
5111+ au_kfree_do_rcu(buf);
5112+ } else
1c60b727 5113+ free_page((unsigned long)buf);
1facf9fc 5114+
4f0767ce 5115+out:
1facf9fc 5116+ return err;
5117+}
5118+
1c60b727
AM
5119+static int au_do_copy(struct file *dst, struct file *src, loff_t len)
5120+{
5121+ int err;
5122+ struct super_block *h_src_sb;
5123+ struct inode *h_src_inode;
5124+
5125+ h_src_inode = file_inode(src);
5126+ h_src_sb = h_src_inode->i_sb;
5127+
5128+ /* XFS acquires inode_lock */
5129+ if (!au_test_xfs(h_src_sb))
5130+ err = au_copy_file(dst, src, len);
5131+ else {
3c1bdaff 5132+ inode_unlock_shared(h_src_inode);
1c60b727 5133+ err = au_copy_file(dst, src, len);
be118d29 5134+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
1c60b727
AM
5135+ }
5136+
5137+ return err;
5138+}
5139+
5140+static int au_clone_or_copy(struct file *dst, struct file *src, loff_t len)
5141+{
5142+ int err;
9f237c51 5143+ loff_t lo;
1c60b727
AM
5144+ struct super_block *h_src_sb;
5145+ struct inode *h_src_inode;
5146+
5147+ h_src_inode = file_inode(src);
5148+ h_src_sb = h_src_inode->i_sb;
5149+ if (h_src_sb != file_inode(dst)->i_sb
9f237c51 5150+ || !dst->f_op->remap_file_range) {
1c60b727
AM
5151+ err = au_do_copy(dst, src, len);
5152+ goto out;
5153+ }
5154+
5155+ if (!au_test_nfs(h_src_sb)) {
3c1bdaff 5156+ inode_unlock_shared(h_src_inode);
9f237c51 5157+ lo = vfsub_clone_file_range(src, dst, len);
be118d29 5158+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
1c60b727 5159+ } else
9f237c51
AM
5160+ lo = vfsub_clone_file_range(src, dst, len);
5161+ if (lo == len) {
5162+ err = 0;
5163+ goto out; /* success */
5164+ } else if (lo >= 0)
5165+ /* todo: possible? */
5166+ /* paritially succeeded */
5167+ AuDbg("lo %lld, len %lld. Retrying.\n", lo, len);
5168+ else if (lo != -EOPNOTSUPP) {
5169+ /* older XFS has a condition in cloning */
5170+ err = lo;
1c60b727 5171+ goto out;
9f237c51 5172+ }
1c60b727
AM
5173+
5174+ /* the backend fs on NFS may not support cloning */
5175+ err = au_do_copy(dst, src, len);
5176+
5177+out:
5178+ AuTraceErr(err);
5179+ return err;
5180+}
5181+
1facf9fc 5182+/*
5183+ * to support a sparse file which is opened with O_APPEND,
5184+ * we need to close the file.
5185+ */
c2b27bf2 5186+static int au_cp_regular(struct au_cp_generic *cpg)
1facf9fc 5187+{
5188+ int err, i;
5189+ enum { SRC, DST };
5190+ struct {
5191+ aufs_bindex_t bindex;
5192+ unsigned int flags;
5193+ struct dentry *dentry;
392086de 5194+ int force_wr;
1facf9fc 5195+ struct file *file;
1facf9fc 5196+ } *f, file[] = {
5197+ {
c2b27bf2 5198+ .bindex = cpg->bsrc,
1facf9fc 5199+ .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
1facf9fc 5200+ },
5201+ {
c2b27bf2 5202+ .bindex = cpg->bdst,
1facf9fc 5203+ .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
392086de 5204+ .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
1facf9fc 5205+ }
5206+ };
acd2b654 5207+ struct au_branch *br;
521ced18 5208+ struct super_block *sb, *h_src_sb;
e2f27e51 5209+ struct inode *h_src_inode;
8cdd5066 5210+ struct task_struct *tsk = current;
1facf9fc 5211+
5212+ /* bsrc branch can be ro/rw. */
c2b27bf2 5213+ sb = cpg->dentry->d_sb;
1facf9fc 5214+ f = file;
5215+ for (i = 0; i < 2; i++, f++) {
c2b27bf2
AM
5216+ f->dentry = au_h_dptr(cpg->dentry, f->bindex);
5217+ f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
392086de 5218+ /*file*/NULL, f->force_wr);
9f237c51
AM
5219+ if (IS_ERR(f->file)) {
5220+ err = PTR_ERR(f->file);
5221+ if (i == SRC)
5222+ goto out;
5223+ else
5224+ goto out_src;
5225+ }
1facf9fc 5226+ }
5227+
5228+ /* try stopping to update while we copyup */
e2f27e51 5229+ h_src_inode = d_inode(file[SRC].dentry);
521ced18
JR
5230+ h_src_sb = h_src_inode->i_sb;
5231+ if (!au_test_nfs(h_src_sb))
e2f27e51 5232+ IMustLock(h_src_inode);
1c60b727 5233+ err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
1facf9fc 5234+
8cdd5066
JR
5235+ /* i wonder if we had O_NO_DELAY_FPUT flag */
5236+ if (tsk->flags & PF_KTHREAD)
5237+ __fput_sync(file[DST].file);
5238+ else {
062440b3 5239+ /* it happened actually */
8cdd5066
JR
5240+ fput(file[DST].file);
5241+ /*
5242+ * too bad.
5243+ * we have to call both since we don't know which place the file
5244+ * was added to.
5245+ */
5246+ task_work_run();
5247+ flush_delayed_fput();
5248+ }
acd2b654
AM
5249+ br = au_sbr(sb, file[DST].bindex);
5250+ au_lcnt_dec(&br->br_nfiles);
523b37e3 5251+
4f0767ce 5252+out_src:
1facf9fc 5253+ fput(file[SRC].file);
acd2b654
AM
5254+ br = au_sbr(sb, file[SRC].bindex);
5255+ au_lcnt_dec(&br->br_nfiles);
4f0767ce 5256+out:
1facf9fc 5257+ return err;
5258+}
5259+
c2b27bf2 5260+static int au_do_cpup_regular(struct au_cp_generic *cpg,
86dc4139 5261+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 5262+{
5263+ int err, rerr;
5264+ loff_t l;
86dc4139 5265+ struct path h_path;
38d290e6 5266+ struct inode *h_src_inode, *h_dst_inode;
1facf9fc 5267+
5268+ err = 0;
5527c038 5269+ h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
86dc4139 5270+ l = i_size_read(h_src_inode);
c2b27bf2
AM
5271+ if (cpg->len == -1 || l < cpg->len)
5272+ cpg->len = l;
5273+ if (cpg->len) {
86dc4139 5274+ /* try stopping to update while we are referencing */
be118d29 5275+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
c2b27bf2 5276+ au_pin_hdir_unlock(cpg->pin);
1facf9fc 5277+
c2b27bf2
AM
5278+ h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5279+ h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
86dc4139 5280+ h_src_attr->iflags = h_src_inode->i_flags;
5527c038 5281+ if (!au_test_nfs(h_src_inode->i_sb))
521ced18 5282+ err = vfsub_getattr(&h_path, &h_src_attr->st);
5527c038 5283+ else {
3c1bdaff 5284+ inode_unlock_shared(h_src_inode);
521ced18 5285+ err = vfsub_getattr(&h_path, &h_src_attr->st);
be118d29 5286+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5527c038 5287+ }
86dc4139 5288+ if (unlikely(err)) {
3c1bdaff 5289+ inode_unlock_shared(h_src_inode);
86dc4139
AM
5290+ goto out;
5291+ }
5292+ h_src_attr->valid = 1;
e2f27e51
AM
5293+ if (!au_test_nfs(h_src_inode->i_sb)) {
5294+ err = au_cp_regular(cpg);
3c1bdaff 5295+ inode_unlock_shared(h_src_inode);
e2f27e51 5296+ } else {
3c1bdaff 5297+ inode_unlock_shared(h_src_inode);
e2f27e51
AM
5298+ err = au_cp_regular(cpg);
5299+ }
c2b27bf2 5300+ rerr = au_pin_hdir_relock(cpg->pin);
86dc4139
AM
5301+ if (!err && rerr)
5302+ err = rerr;
1facf9fc 5303+ }
38d290e6
JR
5304+ if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5305+ h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5527c038 5306+ h_dst_inode = d_inode(h_path.dentry);
38d290e6
JR
5307+ spin_lock(&h_dst_inode->i_lock);
5308+ h_dst_inode->i_state |= I_LINKABLE;
5309+ spin_unlock(&h_dst_inode->i_lock);
5310+ }
1facf9fc 5311+
4f0767ce 5312+out:
1facf9fc 5313+ return err;
5314+}
5315+
5316+static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5317+ struct inode *h_dir)
5318+{
e37dd06a
AM
5319+ int err;
5320+ DEFINE_DELAYED_CALL(done);
5321+ const char *sym;
1facf9fc 5322+
e37dd06a
AM
5323+ sym = vfs_get_link(h_src, &done);
5324+ err = PTR_ERR(sym);
5325+ if (IS_ERR(sym))
1facf9fc 5326+ goto out;
5327+
e37dd06a 5328+ err = vfsub_symlink(h_dir, h_path, sym);
1facf9fc 5329+
4f0767ce 5330+out:
e37dd06a 5331+ do_delayed_call(&done);
1facf9fc 5332+ return err;
5333+}
5334+
8cdd5066
JR
5335+/*
5336+ * regardless 'acl' option, reset all ACL.
5337+ * All ACL will be copied up later from the original entry on the lower branch.
5338+ */
aec24f17 5339+static int au_reset_acl(struct path *h_path, umode_t mode)
8cdd5066
JR
5340+{
5341+ int err;
5342+ struct dentry *h_dentry;
aec24f17 5343+ /* struct inode *h_inode; */
5db017eb 5344+ struct mnt_idmap *h_idmap;
8cdd5066 5345+
5db017eb 5346+ h_idmap = mnt_idmap(h_path->mnt);
8cdd5066 5347+ h_dentry = h_path->dentry;
aec24f17 5348+ /* h_inode = d_inode(h_dentry); */
8cdd5066 5349+ /* forget_all_cached_acls(h_inode)); */
5db017eb 5350+ err = vfsub_remove_acl(h_idmap, h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
8cdd5066
JR
5351+ AuTraceErr(err);
5352+ if (err == -EOPNOTSUPP)
5353+ err = 0;
5354+ if (!err)
5db017eb 5355+ err = vfsub_acl_chmod(h_idmap, h_dentry, mode);
8cdd5066
JR
5356+
5357+ AuTraceErr(err);
5358+ return err;
5359+}
5360+
5361+static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5362+ struct inode *h_dir, struct path *h_path)
5363+{
5364+ int err;
5365+ struct inode *dir, *inode;
5db017eb 5366+ struct mnt_idmap *h_idmap;
8cdd5066 5367+
5db017eb
AM
5368+ h_idmap = mnt_idmap(h_path->mnt);
5369+ err = vfsub_remove_acl(h_idmap, h_path->dentry,
aec24f17 5370+ XATTR_NAME_POSIX_ACL_DEFAULT);
8cdd5066
JR
5371+ AuTraceErr(err);
5372+ if (err == -EOPNOTSUPP)
5373+ err = 0;
5374+ if (unlikely(err))
5375+ goto out;
5376+
5377+ /*
5378+ * strange behaviour from the users view,
acd2b654 5379+ * particularly setattr case
8cdd5066
JR
5380+ */
5381+ dir = d_inode(dst_parent);
5afbbe0d 5382+ if (au_ibtop(dir) == cpg->bdst)
8cdd5066
JR
5383+ au_cpup_attr_nlink(dir, /*force*/1);
5384+ inode = d_inode(cpg->dentry);
5385+ au_cpup_attr_nlink(inode, /*force*/1);
5386+
5387+out:
5388+ return err;
5389+}
5390+
1facf9fc 5391+static noinline_for_stack
c2b27bf2 5392+int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
86dc4139 5393+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 5394+{
5395+ int err;
5396+ umode_t mode;
5397+ unsigned int mnt_flags;
076b876e 5398+ unsigned char isdir, isreg, force;
c2b27bf2 5399+ const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
1facf9fc 5400+ struct au_dtime dt;
5401+ struct path h_path;
5402+ struct dentry *h_src, *h_dst, *h_parent;
8cdd5066 5403+ struct inode *h_inode, *h_dir;
1facf9fc 5404+ struct super_block *sb;
5405+
5406+ /* bsrc branch can be ro/rw. */
c2b27bf2 5407+ h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038
JR
5408+ h_inode = d_inode(h_src);
5409+ AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
1facf9fc 5410+
5411+ /* try stopping to be referenced while we are creating */
c2b27bf2
AM
5412+ h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5413+ if (au_ftest_cpup(cpg->flags, RENAME))
86dc4139
AM
5414+ AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5415+ AUFS_WH_PFX_LEN));
1facf9fc 5416+ h_parent = h_dst->d_parent; /* dir inode is locked */
5527c038 5417+ h_dir = d_inode(h_parent);
1facf9fc 5418+ IMustLock(h_dir);
5419+ AuDebugOn(h_parent != h_dst->d_parent);
5420+
c2b27bf2
AM
5421+ sb = cpg->dentry->d_sb;
5422+ h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
1facf9fc 5423+ if (do_dt) {
5424+ h_path.dentry = h_parent;
5425+ au_dtime_store(&dt, dst_parent, &h_path);
5426+ }
5427+ h_path.dentry = h_dst;
5428+
076b876e 5429+ isreg = 0;
1facf9fc 5430+ isdir = 0;
5431+ mode = h_inode->i_mode;
5432+ switch (mode & S_IFMT) {
5433+ case S_IFREG:
076b876e 5434+ isreg = 1;
cd7a4cd9 5435+ err = vfsub_create(h_dir, &h_path, 0600, /*want_excl*/true);
1facf9fc 5436+ if (!err)
c2b27bf2 5437+ err = au_do_cpup_regular(cpg, h_src_attr);
1facf9fc 5438+ break;
5439+ case S_IFDIR:
5440+ isdir = 1;
5441+ err = vfsub_mkdir(h_dir, &h_path, mode);
8cdd5066
JR
5442+ if (!err)
5443+ err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
1facf9fc 5444+ break;
5445+ case S_IFLNK:
5446+ err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5447+ break;
5448+ case S_IFCHR:
5449+ case S_IFBLK:
5450+ AuDebugOn(!capable(CAP_MKNOD));
42a736d3 5451+ fallthrough;
1facf9fc 5452+ case S_IFIFO:
5453+ case S_IFSOCK:
5454+ err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5455+ break;
5456+ default:
5457+ AuIOErr("Unknown inode type 0%o\n", mode);
5458+ err = -EIO;
5459+ }
8cdd5066 5460+ if (!err)
aec24f17 5461+ err = au_reset_acl(&h_path, mode);
1facf9fc 5462+
5463+ mnt_flags = au_mntflags(sb);
5464+ if (!au_opt_test(mnt_flags, UDBA_NONE)
5465+ && !isdir
5466+ && au_opt_test(mnt_flags, XINO)
38d290e6
JR
5467+ && (h_inode->i_nlink == 1
5468+ || (h_inode->i_state & I_LINKABLE))
1facf9fc 5469+ /* todo: unnecessary? */
5527c038 5470+ /* && d_inode(cpg->dentry)->i_nlink == 1 */
c2b27bf2
AM
5471+ && cpg->bdst < cpg->bsrc
5472+ && !au_ftest_cpup(cpg->flags, KEEPLINO))
5473+ au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
1facf9fc 5474+ /* ignore this error */
5475+
076b876e
AM
5476+ if (!err) {
5477+ force = 0;
5478+ if (isreg) {
5479+ force = !!cpg->len;
5480+ if (cpg->len == -1)
5481+ force = !!i_size_read(h_inode);
5482+ }
5483+ au_fhsm_wrote(sb, cpg->bdst, force);
5484+ }
5485+
1facf9fc 5486+ if (do_dt)
5487+ au_dtime_revert(&dt);
5488+ return err;
5489+}
5490+
392086de 5491+static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
86dc4139
AM
5492+{
5493+ int err;
392086de 5494+ struct dentry *dentry, *h_dentry, *h_parent, *parent;
758e9dad 5495+ struct path h_ppath;
86dc4139 5496+ struct inode *h_dir;
392086de 5497+ aufs_bindex_t bdst;
86dc4139 5498+
392086de
AM
5499+ dentry = cpg->dentry;
5500+ bdst = cpg->bdst;
758e9dad 5501+ h_ppath.mnt = au_sbr_mnt(dentry->d_sb, bdst);
392086de
AM
5502+ h_dentry = au_h_dptr(dentry, bdst);
5503+ if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5504+ dget(h_dentry);
5505+ au_set_h_dptr(dentry, bdst, NULL);
5506+ err = au_lkup_neg(dentry, bdst, /*wh*/0);
5507+ if (!err)
5508+ h_path->dentry = dget(au_h_dptr(dentry, bdst));
86dc4139 5509+ au_set_h_dptr(dentry, bdst, h_dentry);
392086de
AM
5510+ } else {
5511+ err = 0;
5512+ parent = dget_parent(dentry);
758e9dad 5513+ h_ppath.dentry = au_h_dptr(parent, bdst);
392086de 5514+ dput(parent);
758e9dad 5515+ h_path->dentry = vfsub_lkup_one(&dentry->d_name, &h_ppath);
392086de
AM
5516+ if (IS_ERR(h_path->dentry))
5517+ err = PTR_ERR(h_path->dentry);
86dc4139 5518+ }
392086de
AM
5519+ if (unlikely(err))
5520+ goto out;
86dc4139 5521+
86dc4139 5522+ h_parent = h_dentry->d_parent; /* dir inode is locked */
5527c038 5523+ h_dir = d_inode(h_parent);
86dc4139 5524+ IMustLock(h_dir);
523b37e3
AM
5525+ AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5526+ /* no delegation since it is just created */
f2c43d5f
AM
5527+ err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
5528+ /*flags*/0);
86dc4139
AM
5529+ dput(h_path->dentry);
5530+
5531+out:
5532+ return err;
5533+}
5534+
1facf9fc 5535+/*
5536+ * copyup the @dentry from @bsrc to @bdst.
5537+ * the caller must set the both of lower dentries.
5538+ * @len is for truncating when it is -1 copyup the entire file.
5539+ * in link/rename cases, @dst_parent may be different from the real one.
c2b27bf2 5540+ * basic->bsrc can be larger than basic->bdst.
f2c43d5f 5541+ * aufs doesn't touch the credential so
acd2b654 5542+ * security_inode_copy_up{,_xattr}() are unnecessary.
1facf9fc 5543+ */
c2b27bf2 5544+static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
1facf9fc 5545+{
5546+ int err, rerr;
5afbbe0d 5547+ aufs_bindex_t old_ibtop;
1facf9fc 5548+ unsigned char isdir, plink;
1facf9fc 5549+ struct dentry *h_src, *h_dst, *h_parent;
5527c038 5550+ struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
1facf9fc 5551+ struct super_block *sb;
86dc4139 5552+ struct au_branch *br;
0b2a12c6 5553+ struct path h_src_path;
acd2b654 5554+ /* to reduce stack size */
c2b27bf2
AM
5555+ struct {
5556+ struct au_dtime dt;
5557+ struct path h_path;
5558+ struct au_cpup_reg_attr h_src_attr;
5559+ } *a;
1facf9fc 5560+
c2b27bf2
AM
5561+ err = -ENOMEM;
5562+ a = kmalloc(sizeof(*a), GFP_NOFS);
5563+ if (unlikely(!a))
5564+ goto out;
5565+ a->h_src_attr.valid = 0;
1facf9fc 5566+
c2b27bf2
AM
5567+ sb = cpg->dentry->d_sb;
5568+ br = au_sbr(sb, cpg->bdst);
5569+ a->h_path.mnt = au_br_mnt(br);
5570+ h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
1facf9fc 5571+ h_parent = h_dst->d_parent; /* dir inode is locked */
5527c038 5572+ h_dir = d_inode(h_parent);
1facf9fc 5573+ IMustLock(h_dir);
5574+
c2b27bf2 5575+ h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038 5576+ inode = d_inode(cpg->dentry);
1facf9fc 5577+
5578+ if (!dst_parent)
c2b27bf2 5579+ dst_parent = dget_parent(cpg->dentry);
1facf9fc 5580+ else
5581+ dget(dst_parent);
5582+
5583+ plink = !!au_opt_test(au_mntflags(sb), PLINK);
c2b27bf2 5584+ dst_inode = au_h_iptr(inode, cpg->bdst);
1facf9fc 5585+ if (dst_inode) {
5586+ if (unlikely(!plink)) {
5587+ err = -EIO;
027c5e7a
AM
5588+ AuIOErr("hi%lu(i%lu) exists on b%d "
5589+ "but plink is disabled\n",
c2b27bf2
AM
5590+ dst_inode->i_ino, inode->i_ino, cpg->bdst);
5591+ goto out_parent;
1facf9fc 5592+ }
5593+
5594+ if (dst_inode->i_nlink) {
c2b27bf2 5595+ const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
1facf9fc 5596+
c2b27bf2 5597+ h_src = au_plink_lkup(inode, cpg->bdst);
1facf9fc 5598+ err = PTR_ERR(h_src);
5599+ if (IS_ERR(h_src))
c2b27bf2 5600+ goto out_parent;
5527c038 5601+ if (unlikely(d_is_negative(h_src))) {
1facf9fc 5602+ err = -EIO;
79b8bda9 5603+ AuIOErr("i%lu exists on b%d "
027c5e7a 5604+ "but not pseudo-linked\n",
79b8bda9 5605+ inode->i_ino, cpg->bdst);
1facf9fc 5606+ dput(h_src);
c2b27bf2 5607+ goto out_parent;
1facf9fc 5608+ }
5609+
5610+ if (do_dt) {
c2b27bf2
AM
5611+ a->h_path.dentry = h_parent;
5612+ au_dtime_store(&a->dt, dst_parent, &a->h_path);
1facf9fc 5613+ }
86dc4139 5614+
c2b27bf2 5615+ a->h_path.dentry = h_dst;
523b37e3
AM
5616+ delegated = NULL;
5617+ err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
c2b27bf2 5618+ if (!err && au_ftest_cpup(cpg->flags, RENAME))
392086de 5619+ err = au_do_ren_after_cpup(cpg, &a->h_path);
1facf9fc 5620+ if (do_dt)
c2b27bf2 5621+ au_dtime_revert(&a->dt);
523b37e3
AM
5622+ if (unlikely(err == -EWOULDBLOCK)) {
5623+ pr_warn("cannot retry for NFSv4 delegation"
5624+ " for an internal link\n");
5625+ iput(delegated);
5626+ }
1facf9fc 5627+ dput(h_src);
c2b27bf2 5628+ goto out_parent;
1facf9fc 5629+ } else
5630+ /* todo: cpup_wh_file? */
5631+ /* udba work */
4a4d8108 5632+ au_update_ibrange(inode, /*do_put_zero*/1);
1facf9fc 5633+ }
5634+
86dc4139 5635+ isdir = S_ISDIR(inode->i_mode);
5afbbe0d 5636+ old_ibtop = au_ibtop(inode);
c2b27bf2 5637+ err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
1facf9fc 5638+ if (unlikely(err))
86dc4139 5639+ goto out_rev;
5527c038 5640+ dst_inode = d_inode(h_dst);
febd17d6 5641+ inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
86dc4139 5642+ /* todo: necessary? */
c2b27bf2 5643+ /* au_pin_hdir_unlock(cpg->pin); */
1facf9fc 5644+
0b2a12c6
JR
5645+ h_src_path.dentry = h_src;
5646+ h_src_path.mnt = au_sbr_mnt(sb, cpg->bsrc);
5647+ err = cpup_iattr(cpg->dentry, cpg->bdst, &h_src_path, &a->h_src_attr);
86dc4139
AM
5648+ if (unlikely(err)) {
5649+ /* todo: necessary? */
c2b27bf2 5650+ /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
febd17d6 5651+ inode_unlock(dst_inode);
86dc4139
AM
5652+ goto out_rev;
5653+ }
5654+
5afbbe0d 5655+ if (cpg->bdst < old_ibtop) {
86dc4139 5656+ if (S_ISREG(inode->i_mode)) {
c2b27bf2 5657+ err = au_dy_iaop(inode, cpg->bdst, dst_inode);
86dc4139 5658+ if (unlikely(err)) {
c2b27bf2
AM
5659+ /* ignore an error */
5660+ /* au_pin_hdir_relock(cpg->pin); */
febd17d6 5661+ inode_unlock(dst_inode);
86dc4139 5662+ goto out_rev;
4a4d8108 5663+ }
4a4d8108 5664+ }
5afbbe0d 5665+ au_set_ibtop(inode, cpg->bdst);
c2b27bf2 5666+ } else
5afbbe0d 5667+ au_set_ibbot(inode, cpg->bdst);
c2b27bf2 5668+ au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
86dc4139
AM
5669+ au_hi_flags(inode, isdir));
5670+
5671+ /* todo: necessary? */
c2b27bf2 5672+ /* err = au_pin_hdir_relock(cpg->pin); */
febd17d6 5673+ inode_unlock(dst_inode);
86dc4139
AM
5674+ if (unlikely(err))
5675+ goto out_rev;
5676+
5527c038 5677+ src_inode = d_inode(h_src);
86dc4139 5678+ if (!isdir
5527c038
JR
5679+ && (src_inode->i_nlink > 1
5680+ || src_inode->i_state & I_LINKABLE)
86dc4139 5681+ && plink)
c2b27bf2 5682+ au_plink_append(inode, cpg->bdst, h_dst);
86dc4139 5683+
c2b27bf2
AM
5684+ if (au_ftest_cpup(cpg->flags, RENAME)) {
5685+ a->h_path.dentry = h_dst;
392086de 5686+ err = au_do_ren_after_cpup(cpg, &a->h_path);
86dc4139
AM
5687+ }
5688+ if (!err)
c2b27bf2 5689+ goto out_parent; /* success */
1facf9fc 5690+
5691+ /* revert */
4a4d8108 5692+out_rev:
c2b27bf2
AM
5693+ a->h_path.dentry = h_parent;
5694+ au_dtime_store(&a->dt, dst_parent, &a->h_path);
5695+ a->h_path.dentry = h_dst;
86dc4139 5696+ rerr = 0;
5527c038 5697+ if (d_is_positive(h_dst)) {
523b37e3
AM
5698+ if (!isdir) {
5699+ /* no delegation since it is just created */
5700+ rerr = vfsub_unlink(h_dir, &a->h_path,
5701+ /*delegated*/NULL, /*force*/0);
5702+ } else
c2b27bf2 5703+ rerr = vfsub_rmdir(h_dir, &a->h_path);
86dc4139 5704+ }
c2b27bf2 5705+ au_dtime_revert(&a->dt);
1facf9fc 5706+ if (rerr) {
5707+ AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5708+ err = -EIO;
5709+ }
c2b27bf2 5710+out_parent:
1facf9fc 5711+ dput(dst_parent);
9f237c51 5712+ au_kfree_rcu(a);
c2b27bf2 5713+out:
1facf9fc 5714+ return err;
5715+}
5716+
7e9cd9fe 5717+#if 0 /* reserved */
1facf9fc 5718+struct au_cpup_single_args {
5719+ int *errp;
c2b27bf2 5720+ struct au_cp_generic *cpg;
1facf9fc 5721+ struct dentry *dst_parent;
5722+};
5723+
5724+static void au_call_cpup_single(void *args)
5725+{
5726+ struct au_cpup_single_args *a = args;
86dc4139 5727+
c2b27bf2
AM
5728+ au_pin_hdir_acquire_nest(a->cpg->pin);
5729+ *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5730+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 5731+}
c2b27bf2 5732+#endif
1facf9fc 5733+
53392da6
AM
5734+/*
5735+ * prevent SIGXFSZ in copy-up.
5736+ * testing CAP_MKNOD is for generic fs,
5737+ * but CAP_FSETID is for xfs only, currently.
5738+ */
86dc4139 5739+static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
53392da6
AM
5740+{
5741+ int do_sio;
86dc4139
AM
5742+ struct super_block *sb;
5743+ struct inode *h_dir;
53392da6
AM
5744+
5745+ do_sio = 0;
86dc4139 5746+ sb = au_pinned_parent(pin)->d_sb;
53392da6
AM
5747+ if (!au_wkq_test()
5748+ && (!au_sbi(sb)->si_plink_maint_pid
5749+ || au_plink_maint(sb, AuLock_NOPLM))) {
5750+ switch (mode & S_IFMT) {
5751+ case S_IFREG:
5752+ /* no condition about RLIMIT_FSIZE and the file size */
5753+ do_sio = 1;
5754+ break;
5755+ case S_IFCHR:
5756+ case S_IFBLK:
5757+ do_sio = !capable(CAP_MKNOD);
5758+ break;
5759+ }
5760+ if (!do_sio)
5761+ do_sio = ((mode & (S_ISUID | S_ISGID))
5762+ && !capable(CAP_FSETID));
86dc4139
AM
5763+ /* this workaround may be removed in the future */
5764+ if (!do_sio) {
5765+ h_dir = au_pinned_h_dir(pin);
5766+ do_sio = h_dir->i_mode & S_ISVTX;
5767+ }
53392da6
AM
5768+ }
5769+
5770+ return do_sio;
5771+}
5772+
7e9cd9fe 5773+#if 0 /* reserved */
c2b27bf2 5774+int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
1facf9fc 5775+{
5776+ int err, wkq_err;
1facf9fc 5777+ struct dentry *h_dentry;
5778+
c2b27bf2 5779+ h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038 5780+ if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
c2b27bf2 5781+ err = au_cpup_single(cpg, dst_parent);
1facf9fc 5782+ else {
5783+ struct au_cpup_single_args args = {
5784+ .errp = &err,
c2b27bf2
AM
5785+ .cpg = cpg,
5786+ .dst_parent = dst_parent
1facf9fc 5787+ };
5788+ wkq_err = au_wkq_wait(au_call_cpup_single, &args);
5789+ if (unlikely(wkq_err))
5790+ err = wkq_err;
5791+ }
5792+
5793+ return err;
5794+}
c2b27bf2 5795+#endif
1facf9fc 5796+
5797+/*
5798+ * copyup the @dentry from the first active lower branch to @bdst,
5799+ * using au_cpup_single().
5800+ */
c2b27bf2 5801+static int au_cpup_simple(struct au_cp_generic *cpg)
1facf9fc 5802+{
5803+ int err;
c2b27bf2
AM
5804+ unsigned int flags_orig;
5805+ struct dentry *dentry;
5806+
5807+ AuDebugOn(cpg->bsrc < 0);
1facf9fc 5808+
c2b27bf2 5809+ dentry = cpg->dentry;
86dc4139 5810+ DiMustWriteLock(dentry);
1facf9fc 5811+
c2b27bf2 5812+ err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
1facf9fc 5813+ if (!err) {
c2b27bf2
AM
5814+ flags_orig = cpg->flags;
5815+ au_fset_cpup(cpg->flags, RENAME);
5816+ err = au_cpup_single(cpg, NULL);
5817+ cpg->flags = flags_orig;
1facf9fc 5818+ if (!err)
5819+ return 0; /* success */
5820+
5821+ /* revert */
c2b27bf2 5822+ au_set_h_dptr(dentry, cpg->bdst, NULL);
5afbbe0d 5823+ au_set_dbtop(dentry, cpg->bsrc);
1facf9fc 5824+ }
5825+
5826+ return err;
5827+}
5828+
5829+struct au_cpup_simple_args {
5830+ int *errp;
c2b27bf2 5831+ struct au_cp_generic *cpg;
1facf9fc 5832+};
5833+
5834+static void au_call_cpup_simple(void *args)
5835+{
5836+ struct au_cpup_simple_args *a = args;
86dc4139 5837+
c2b27bf2
AM
5838+ au_pin_hdir_acquire_nest(a->cpg->pin);
5839+ *a->errp = au_cpup_simple(a->cpg);
5840+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 5841+}
5842+
c2b27bf2 5843+static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
1facf9fc 5844+{
5845+ int err, wkq_err;
c2b27bf2
AM
5846+ struct dentry *dentry, *parent;
5847+ struct file *h_file;
1facf9fc 5848+ struct inode *h_dir;
5db017eb 5849+ struct mnt_idmap *h_idmap;
1facf9fc 5850+
c2b27bf2
AM
5851+ dentry = cpg->dentry;
5852+ h_file = NULL;
5853+ if (au_ftest_cpup(cpg->flags, HOPEN)) {
5854+ AuDebugOn(cpg->bsrc < 0);
392086de 5855+ h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
c2b27bf2
AM
5856+ err = PTR_ERR(h_file);
5857+ if (IS_ERR(h_file))
5858+ goto out;
5859+ }
5860+
1facf9fc 5861+ parent = dget_parent(dentry);
5527c038 5862+ h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
5db017eb
AM
5863+ h_idmap = au_sbr_idmap(dentry->d_sb, cpg->bdst);
5864+ if (!au_test_h_perm_sio(h_idmap, h_dir, MAY_EXEC | MAY_WRITE)
5527c038 5865+ && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
c2b27bf2 5866+ err = au_cpup_simple(cpg);
1facf9fc 5867+ else {
5868+ struct au_cpup_simple_args args = {
5869+ .errp = &err,
c2b27bf2 5870+ .cpg = cpg
1facf9fc 5871+ };
5872+ wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
5873+ if (unlikely(wkq_err))
5874+ err = wkq_err;
5875+ }
5876+
5877+ dput(parent);
c2b27bf2
AM
5878+ if (h_file)
5879+ au_h_open_post(dentry, cpg->bsrc, h_file);
5880+
5881+out:
1facf9fc 5882+ return err;
5883+}
5884+
c2b27bf2 5885+int au_sio_cpup_simple(struct au_cp_generic *cpg)
367653fa 5886+{
5afbbe0d 5887+ aufs_bindex_t bsrc, bbot;
c2b27bf2 5888+ struct dentry *dentry, *h_dentry;
367653fa 5889+
c2b27bf2
AM
5890+ if (cpg->bsrc < 0) {
5891+ dentry = cpg->dentry;
5afbbe0d
AM
5892+ bbot = au_dbbot(dentry);
5893+ for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
c2b27bf2
AM
5894+ h_dentry = au_h_dptr(dentry, bsrc);
5895+ if (h_dentry) {
5527c038 5896+ AuDebugOn(d_is_negative(h_dentry));
c2b27bf2
AM
5897+ break;
5898+ }
5899+ }
5afbbe0d 5900+ AuDebugOn(bsrc > bbot);
c2b27bf2 5901+ cpg->bsrc = bsrc;
367653fa 5902+ }
c2b27bf2
AM
5903+ AuDebugOn(cpg->bsrc <= cpg->bdst);
5904+ return au_do_sio_cpup_simple(cpg);
5905+}
367653fa 5906+
c2b27bf2
AM
5907+int au_sio_cpdown_simple(struct au_cp_generic *cpg)
5908+{
5909+ AuDebugOn(cpg->bdst <= cpg->bsrc);
5910+ return au_do_sio_cpup_simple(cpg);
367653fa
AM
5911+}
5912+
1facf9fc 5913+/* ---------------------------------------------------------------------- */
5914+
5915+/*
5916+ * copyup the deleted file for writing.
5917+ */
c2b27bf2
AM
5918+static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
5919+ struct file *file)
1facf9fc 5920+{
5921+ int err;
c2b27bf2
AM
5922+ unsigned int flags_orig;
5923+ aufs_bindex_t bsrc_orig;
c2b27bf2 5924+ struct au_dinfo *dinfo;
5afbbe0d
AM
5925+ struct {
5926+ struct au_hdentry *hd;
5927+ struct dentry *h_dentry;
5928+ } hdst, hsrc;
1facf9fc 5929+
c2b27bf2 5930+ dinfo = au_di(cpg->dentry);
1308ab2a 5931+ AuRwMustWriteLock(&dinfo->di_rwsem);
5932+
c2b27bf2 5933+ bsrc_orig = cpg->bsrc;
5afbbe0d
AM
5934+ cpg->bsrc = dinfo->di_btop;
5935+ hdst.hd = au_hdentry(dinfo, cpg->bdst);
5936+ hdst.h_dentry = hdst.hd->hd_dentry;
5937+ hdst.hd->hd_dentry = wh_dentry;
5938+ dinfo->di_btop = cpg->bdst;
5939+
5940+ hsrc.h_dentry = NULL;
027c5e7a 5941+ if (file) {
5afbbe0d
AM
5942+ hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
5943+ hsrc.h_dentry = hsrc.hd->hd_dentry;
5944+ hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
027c5e7a 5945+ }
c2b27bf2
AM
5946+ flags_orig = cpg->flags;
5947+ cpg->flags = !AuCpup_DTIME;
5948+ err = au_cpup_single(cpg, /*h_parent*/NULL);
5949+ cpg->flags = flags_orig;
027c5e7a
AM
5950+ if (file) {
5951+ if (!err)
5952+ err = au_reopen_nondir(file);
5afbbe0d 5953+ hsrc.hd->hd_dentry = hsrc.h_dentry;
1facf9fc 5954+ }
5afbbe0d
AM
5955+ hdst.hd->hd_dentry = hdst.h_dentry;
5956+ dinfo->di_btop = cpg->bsrc;
c2b27bf2 5957+ cpg->bsrc = bsrc_orig;
1facf9fc 5958+
5959+ return err;
5960+}
5961+
c2b27bf2 5962+static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
1facf9fc 5963+{
5964+ int err;
c2b27bf2 5965+ aufs_bindex_t bdst;
1facf9fc 5966+ struct au_dtime dt;
c2b27bf2 5967+ struct dentry *dentry, *parent, *h_parent, *wh_dentry;
1facf9fc 5968+ struct au_branch *br;
5969+ struct path h_path;
5970+
c2b27bf2
AM
5971+ dentry = cpg->dentry;
5972+ bdst = cpg->bdst;
1facf9fc 5973+ br = au_sbr(dentry->d_sb, bdst);
5974+ parent = dget_parent(dentry);
5975+ h_parent = au_h_dptr(parent, bdst);
5976+ wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
5977+ err = PTR_ERR(wh_dentry);
5978+ if (IS_ERR(wh_dentry))
5979+ goto out;
5980+
5981+ h_path.dentry = h_parent;
86dc4139 5982+ h_path.mnt = au_br_mnt(br);
1facf9fc 5983+ au_dtime_store(&dt, parent, &h_path);
c2b27bf2 5984+ err = au_do_cpup_wh(cpg, wh_dentry, file);
1facf9fc 5985+ if (unlikely(err))
5986+ goto out_wh;
5987+
5988+ dget(wh_dentry);
5989+ h_path.dentry = wh_dentry;
2000de60 5990+ if (!d_is_dir(wh_dentry)) {
523b37e3 5991+ /* no delegation since it is just created */
5527c038 5992+ err = vfsub_unlink(d_inode(h_parent), &h_path,
523b37e3
AM
5993+ /*delegated*/NULL, /*force*/0);
5994+ } else
5527c038 5995+ err = vfsub_rmdir(d_inode(h_parent), &h_path);
1facf9fc 5996+ if (unlikely(err)) {
523b37e3
AM
5997+ AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
5998+ wh_dentry, err);
1facf9fc 5999+ err = -EIO;
6000+ }
6001+ au_dtime_revert(&dt);
5527c038 6002+ au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
1facf9fc 6003+
4f0767ce 6004+out_wh:
1facf9fc 6005+ dput(wh_dentry);
4f0767ce 6006+out:
1facf9fc 6007+ dput(parent);
6008+ return err;
6009+}
6010+
6011+struct au_cpup_wh_args {
6012+ int *errp;
c2b27bf2 6013+ struct au_cp_generic *cpg;
1facf9fc 6014+ struct file *file;
6015+};
6016+
6017+static void au_call_cpup_wh(void *args)
6018+{
6019+ struct au_cpup_wh_args *a = args;
86dc4139 6020+
c2b27bf2
AM
6021+ au_pin_hdir_acquire_nest(a->cpg->pin);
6022+ *a->errp = au_cpup_wh(a->cpg, a->file);
6023+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 6024+}
6025+
c2b27bf2 6026+int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
1facf9fc 6027+{
6028+ int err, wkq_err;
c2b27bf2 6029+ aufs_bindex_t bdst;
c1595e42 6030+ struct dentry *dentry, *parent, *h_orph, *h_parent;
86dc4139 6031+ struct inode *dir, *h_dir, *h_tmpdir;
1facf9fc 6032+ struct au_wbr *wbr;
c2b27bf2 6033+ struct au_pin wh_pin, *pin_orig;
5db017eb 6034+ struct mnt_idmap *h_idmap;
1facf9fc 6035+
c2b27bf2
AM
6036+ dentry = cpg->dentry;
6037+ bdst = cpg->bdst;
1facf9fc 6038+ parent = dget_parent(dentry);
5527c038 6039+ dir = d_inode(parent);
1facf9fc 6040+ h_orph = NULL;
6041+ h_parent = NULL;
6042+ h_dir = au_igrab(au_h_iptr(dir, bdst));
6043+ h_tmpdir = h_dir;
c2b27bf2 6044+ pin_orig = NULL;
1facf9fc 6045+ if (!h_dir->i_nlink) {
6046+ wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
6047+ h_orph = wbr->wbr_orph;
6048+
6049+ h_parent = dget(au_h_dptr(parent, bdst));
1facf9fc 6050+ au_set_h_dptr(parent, bdst, dget(h_orph));
5527c038 6051+ h_tmpdir = d_inode(h_orph);
1facf9fc 6052+ au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
6053+
febd17d6 6054+ inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
4a4d8108 6055+ /* todo: au_h_open_pre()? */
86dc4139 6056+
c2b27bf2 6057+ pin_orig = cpg->pin;
86dc4139 6058+ au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
c2b27bf2
AM
6059+ AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
6060+ cpg->pin = &wh_pin;
1facf9fc 6061+ }
6062+
5db017eb
AM
6063+ h_idmap = au_sbr_idmap(dentry->d_sb, bdst);
6064+ if (!au_test_h_perm_sio(h_idmap, h_tmpdir, MAY_EXEC | MAY_WRITE)
5527c038 6065+ && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
c2b27bf2 6066+ err = au_cpup_wh(cpg, file);
1facf9fc 6067+ else {
6068+ struct au_cpup_wh_args args = {
6069+ .errp = &err,
c2b27bf2
AM
6070+ .cpg = cpg,
6071+ .file = file
1facf9fc 6072+ };
6073+ wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
6074+ if (unlikely(wkq_err))
6075+ err = wkq_err;
6076+ }
6077+
6078+ if (h_orph) {
febd17d6 6079+ inode_unlock(h_tmpdir);
4a4d8108 6080+ /* todo: au_h_open_post()? */
1facf9fc 6081+ au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
1facf9fc 6082+ au_set_h_dptr(parent, bdst, h_parent);
c2b27bf2
AM
6083+ AuDebugOn(!pin_orig);
6084+ cpg->pin = pin_orig;
1facf9fc 6085+ }
6086+ iput(h_dir);
6087+ dput(parent);
6088+
6089+ return err;
6090+}
6091+
6092+/* ---------------------------------------------------------------------- */
6093+
6094+/*
6095+ * generic routine for both of copy-up and copy-down.
6096+ */
6097+/* cf. revalidate function in file.c */
6098+int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6099+ int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 6100+ struct au_pin *pin,
1facf9fc 6101+ struct dentry *h_parent, void *arg),
6102+ void *arg)
6103+{
6104+ int err;
6105+ struct au_pin pin;
5527c038 6106+ struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
1facf9fc 6107+
6108+ err = 0;
6109+ parent = dget_parent(dentry);
6110+ if (IS_ROOT(parent))
6111+ goto out;
6112+
6113+ au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
6114+ au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
6115+
6116+ /* do not use au_dpage */
6117+ real_parent = parent;
6118+ while (1) {
6119+ dput(parent);
6120+ parent = dget_parent(dentry);
6121+ h_parent = au_h_dptr(parent, bdst);
6122+ if (h_parent)
6123+ goto out; /* success */
6124+
6125+ /* find top dir which is necessary to cpup */
6126+ do {
6127+ d = parent;
6128+ dput(parent);
6129+ parent = dget_parent(d);
6130+ di_read_lock_parent3(parent, !AuLock_IR);
6131+ h_parent = au_h_dptr(parent, bdst);
6132+ di_read_unlock(parent, !AuLock_IR);
6133+ } while (!h_parent);
6134+
6135+ if (d != real_parent)
6136+ di_write_lock_child3(d);
6137+
6138+ /* somebody else might create while we were sleeping */
5527c038
JR
6139+ h_dentry = au_h_dptr(d, bdst);
6140+ if (!h_dentry || d_is_negative(h_dentry)) {
6141+ if (h_dentry)
5afbbe0d 6142+ au_update_dbtop(d);
1facf9fc 6143+
6144+ au_pin_set_dentry(&pin, d);
6145+ err = au_do_pin(&pin);
6146+ if (!err) {
86dc4139 6147+ err = cp(d, bdst, &pin, h_parent, arg);
1facf9fc 6148+ au_unpin(&pin);
6149+ }
6150+ }
6151+
6152+ if (d != real_parent)
6153+ di_write_unlock(d);
6154+ if (unlikely(err))
6155+ break;
6156+ }
6157+
4f0767ce 6158+out:
1facf9fc 6159+ dput(parent);
6160+ return err;
6161+}
6162+
6163+static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 6164+ struct au_pin *pin,
2000de60 6165+ struct dentry *h_parent __maybe_unused,
1facf9fc 6166+ void *arg __maybe_unused)
6167+{
c2b27bf2
AM
6168+ struct au_cp_generic cpg = {
6169+ .dentry = dentry,
6170+ .bdst = bdst,
6171+ .bsrc = -1,
6172+ .len = 0,
6173+ .pin = pin,
6174+ .flags = AuCpup_DTIME
6175+ };
6176+ return au_sio_cpup_simple(&cpg);
1facf9fc 6177+}
6178+
6179+int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6180+{
6181+ return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
6182+}
6183+
6184+int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6185+{
6186+ int err;
6187+ struct dentry *parent;
6188+ struct inode *dir;
6189+
6190+ parent = dget_parent(dentry);
5527c038 6191+ dir = d_inode(parent);
1facf9fc 6192+ err = 0;
6193+ if (au_h_iptr(dir, bdst))
6194+ goto out;
6195+
6196+ di_read_unlock(parent, AuLock_IR);
6197+ di_write_lock_parent(parent);
6198+ /* someone else might change our inode while we were sleeping */
6199+ if (!au_h_iptr(dir, bdst))
6200+ err = au_cpup_dirs(dentry, bdst);
6201+ di_downgrade_lock(parent, AuLock_IR);
6202+
4f0767ce 6203+out:
1facf9fc 6204+ dput(parent);
6205+ return err;
6206+}
7f207e10 6207diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
eca34b5c 6208--- /usr/share/empty/fs/aufs/cpup.h 1970-01-01 01:00:00.000000000 +0100
594d0238 6209+++ linux/fs/aufs/cpup.h 2022-11-05 23:02:18.962555950 +0100
062440b3
AM
6210@@ -0,0 +1,100 @@
6211+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 6212+/*
0dcfbb52 6213+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 6214+ *
0dcfbb52 6215+ * This program is free software; you can redistribute it and/or modify
1facf9fc 6216+ * it under the terms of the GNU General Public License as published by
6217+ * the Free Software Foundation; either version 2 of the License, or
6218+ * (at your option) any later version.
dece6358
AM
6219+ *
6220+ * This program is distributed in the hope that it will be useful,
6221+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6222+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6223+ * GNU General Public License for more details.
6224+ *
6225+ * You should have received a copy of the GNU General Public License
523b37e3 6226+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6227+ */
6228+
6229+/*
6230+ * copy-up/down functions
6231+ */
6232+
6233+#ifndef __AUFS_CPUP_H__
6234+#define __AUFS_CPUP_H__
6235+
6236+#ifdef __KERNEL__
6237+
dece6358 6238+#include <linux/path.h>
1facf9fc 6239+
dece6358
AM
6240+struct inode;
6241+struct file;
86dc4139 6242+struct au_pin;
dece6358 6243+
86dc4139 6244+void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
1facf9fc 6245+void au_cpup_attr_timesizes(struct inode *inode);
6246+void au_cpup_attr_nlink(struct inode *inode, int force);
6247+void au_cpup_attr_changeable(struct inode *inode);
6248+void au_cpup_igen(struct inode *inode, struct inode *h_inode);
6249+void au_cpup_attr_all(struct inode *inode, int force);
6250+
6251+/* ---------------------------------------------------------------------- */
6252+
c2b27bf2
AM
6253+struct au_cp_generic {
6254+ struct dentry *dentry;
6255+ aufs_bindex_t bdst, bsrc;
6256+ loff_t len;
6257+ struct au_pin *pin;
6258+ unsigned int flags;
6259+};
6260+
1facf9fc 6261+/* cpup flags */
392086de
AM
6262+#define AuCpup_DTIME 1 /* do dtime_store/revert */
6263+#define AuCpup_KEEPLINO (1 << 1) /* do not clear the lower xino,
6264+ for link(2) */
6265+#define AuCpup_RENAME (1 << 2) /* rename after cpup */
6266+#define AuCpup_HOPEN (1 << 3) /* call h_open_pre/post() in
6267+ cpup */
6268+#define AuCpup_OVERWRITE (1 << 4) /* allow overwriting the
6269+ existing entry */
6270+#define AuCpup_RWDST (1 << 5) /* force write target even if
6271+ the branch is marked as RO */
c2b27bf2 6272+
8b6a4947
AM
6273+#ifndef CONFIG_AUFS_BR_HFSPLUS
6274+#undef AuCpup_HOPEN
6275+#define AuCpup_HOPEN 0
6276+#endif
6277+
1facf9fc 6278+#define au_ftest_cpup(flags, name) ((flags) & AuCpup_##name)
7f207e10
AM
6279+#define au_fset_cpup(flags, name) \
6280+ do { (flags) |= AuCpup_##name; } while (0)
6281+#define au_fclr_cpup(flags, name) \
6282+ do { (flags) &= ~AuCpup_##name; } while (0)
1facf9fc 6283+
6284+int au_copy_file(struct file *dst, struct file *src, loff_t len);
c2b27bf2
AM
6285+int au_sio_cpup_simple(struct au_cp_generic *cpg);
6286+int au_sio_cpdown_simple(struct au_cp_generic *cpg);
6287+int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
1facf9fc 6288+
6289+int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6290+ int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 6291+ struct au_pin *pin,
1facf9fc 6292+ struct dentry *h_parent, void *arg),
6293+ void *arg);
6294+int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6295+int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6296+
6297+/* ---------------------------------------------------------------------- */
6298+
6299+/* keep timestamps when copyup */
6300+struct au_dtime {
6301+ struct dentry *dt_dentry;
6302+ struct path dt_h_path;
cd7a4cd9 6303+ struct timespec64 dt_atime, dt_mtime;
1facf9fc 6304+};
6305+void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6306+ struct path *h_path);
6307+void au_dtime_revert(struct au_dtime *dt);
6308+
6309+#endif /* __KERNEL__ */
6310+#endif /* __AUFS_CPUP_H__ */
7f207e10 6311diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
eca34b5c 6312--- /usr/share/empty/fs/aufs/dbgaufs.c 1970-01-01 01:00:00.000000000 +0100
631230db 6313+++ linux/fs/aufs/dbgaufs.c 2023-10-10 22:51:18.033248030 +0200
9f237c51 6314@@ -0,0 +1,526 @@
cd7a4cd9 6315+// SPDX-License-Identifier: GPL-2.0
1facf9fc 6316+/*
0dcfbb52 6317+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 6318+ *
0dcfbb52 6319+ * This program is free software; you can redistribute it and/or modify
1facf9fc 6320+ * it under the terms of the GNU General Public License as published by
6321+ * the Free Software Foundation; either version 2 of the License, or
6322+ * (at your option) any later version.
dece6358
AM
6323+ *
6324+ * This program is distributed in the hope that it will be useful,
6325+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6326+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6327+ * GNU General Public License for more details.
6328+ *
6329+ * You should have received a copy of the GNU General Public License
523b37e3 6330+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6331+ */
6332+
6333+/*
6334+ * debugfs interface
6335+ */
6336+
6337+#include <linux/debugfs.h>
6338+#include "aufs.h"
6339+
6340+#ifndef CONFIG_SYSFS
6341+#error DEBUG_FS depends upon SYSFS
6342+#endif
6343+
6344+static struct dentry *dbgaufs;
cd7a4cd9 6345+static const mode_t dbgaufs_mode = 0444;
1facf9fc 6346+
6347+/* 20 is max digits length of ulong 64 */
6348+struct dbgaufs_arg {
6349+ int n;
6350+ char a[20 * 4];
6351+};
6352+
6353+/*
6354+ * common function for all XINO files
6355+ */
6356+static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6357+ struct file *file)
6358+{
9f237c51
AM
6359+ void *p;
6360+
6361+ p = file->private_data;
6362+ if (p) {
6363+ /* this is struct dbgaufs_arg */
6364+ AuDebugOn(!au_kfree_sz_test(p));
6365+ au_kfree_do_rcu(p);
6366+ }
1facf9fc 6367+ return 0;
6368+}
6369+
062440b3
AM
6370+static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt,
6371+ int cnt)
1facf9fc 6372+{
6373+ int err;
6374+ struct kstat st;
6375+ struct dbgaufs_arg *p;
6376+
6377+ err = -ENOMEM;
6378+ p = kmalloc(sizeof(*p), GFP_NOFS);
6379+ if (unlikely(!p))
6380+ goto out;
6381+
6382+ err = 0;
6383+ p->n = 0;
6384+ file->private_data = p;
6385+ if (!xf)
6386+ goto out;
6387+
521ced18 6388+ err = vfsub_getattr(&xf->f_path, &st);
1facf9fc 6389+ if (!err) {
6390+ if (do_fcnt)
6391+ p->n = snprintf
062440b3
AM
6392+ (p->a, sizeof(p->a), "%d, %llux%u %lld\n",
6393+ cnt, st.blocks, st.blksize,
1facf9fc 6394+ (long long)st.size);
6395+ else
521ced18 6396+ p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
1facf9fc 6397+ st.blocks, st.blksize,
6398+ (long long)st.size);
6399+ AuDebugOn(p->n >= sizeof(p->a));
6400+ } else {
6401+ p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6402+ err = 0;
6403+ }
6404+
4f0767ce 6405+out:
1facf9fc 6406+ return err;
1facf9fc 6407+}
6408+
6409+static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6410+ size_t count, loff_t *ppos)
6411+{
6412+ struct dbgaufs_arg *p;
6413+
6414+ p = file->private_data;
6415+ return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6416+}
6417+
6418+/* ---------------------------------------------------------------------- */
6419+
86dc4139
AM
6420+struct dbgaufs_plink_arg {
6421+ int n;
6422+ char a[];
6423+};
6424+
6425+static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6426+ struct file *file)
6427+{
1c60b727 6428+ free_page((unsigned long)file->private_data);
86dc4139
AM
6429+ return 0;
6430+}
6431+
6432+static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6433+{
6434+ int err, i, limit;
6435+ unsigned long n, sum;
6436+ struct dbgaufs_plink_arg *p;
6437+ struct au_sbinfo *sbinfo;
6438+ struct super_block *sb;
8b6a4947 6439+ struct hlist_bl_head *hbl;
86dc4139
AM
6440+
6441+ err = -ENOMEM;
6442+ p = (void *)get_zeroed_page(GFP_NOFS);
6443+ if (unlikely(!p))
6444+ goto out;
6445+
6446+ err = -EFBIG;
6447+ sbinfo = inode->i_private;
6448+ sb = sbinfo->si_sb;
6449+ si_noflush_read_lock(sb);
6450+ if (au_opt_test(au_mntflags(sb), PLINK)) {
6451+ limit = PAGE_SIZE - sizeof(p->n);
6452+
6453+ /* the number of buckets */
6454+ n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6455+ p->n += n;
6456+ limit -= n;
6457+
6458+ sum = 0;
8b6a4947
AM
6459+ for (i = 0, hbl = sbinfo->si_plink; i < AuPlink_NHASH;
6460+ i++, hbl++) {
6461+ n = au_hbl_count(hbl);
86dc4139
AM
6462+ sum += n;
6463+
6464+ n = snprintf(p->a + p->n, limit, "%lu ", n);
6465+ p->n += n;
6466+ limit -= n;
6467+ if (unlikely(limit <= 0))
6468+ goto out_free;
6469+ }
6470+ p->a[p->n - 1] = '\n';
6471+
6472+ /* the sum of plinks */
6473+ n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6474+ p->n += n;
6475+ limit -= n;
6476+ if (unlikely(limit <= 0))
6477+ goto out_free;
6478+ } else {
6479+#define str "1\n0\n0\n"
6480+ p->n = sizeof(str) - 1;
631230db 6481+ strscpy(p->a, str, sizeof(str));
86dc4139
AM
6482+#undef str
6483+ }
6484+ si_read_unlock(sb);
6485+
6486+ err = 0;
6487+ file->private_data = p;
6488+ goto out; /* success */
6489+
6490+out_free:
1c60b727 6491+ free_page((unsigned long)p);
86dc4139
AM
6492+out:
6493+ return err;
6494+}
6495+
6496+static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6497+ size_t count, loff_t *ppos)
6498+{
6499+ struct dbgaufs_plink_arg *p;
6500+
6501+ p = file->private_data;
6502+ return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6503+}
6504+
6505+static const struct file_operations dbgaufs_plink_fop = {
6506+ .owner = THIS_MODULE,
6507+ .open = dbgaufs_plink_open,
6508+ .release = dbgaufs_plink_release,
6509+ .read = dbgaufs_plink_read
6510+};
6511+
6512+/* ---------------------------------------------------------------------- */
6513+
1facf9fc 6514+static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6515+{
6516+ int err;
6517+ struct au_sbinfo *sbinfo;
6518+ struct super_block *sb;
6519+
6520+ sbinfo = inode->i_private;
6521+ sb = sbinfo->si_sb;
6522+ si_noflush_read_lock(sb);
062440b3 6523+ err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0, /*cnt*/0);
1facf9fc 6524+ si_read_unlock(sb);
6525+ return err;
6526+}
6527+
6528+static const struct file_operations dbgaufs_xib_fop = {
4a4d8108 6529+ .owner = THIS_MODULE,
1facf9fc 6530+ .open = dbgaufs_xib_open,
6531+ .release = dbgaufs_xi_release,
6532+ .read = dbgaufs_xi_read
6533+};
6534+
6535+/* ---------------------------------------------------------------------- */
6536+
6537+#define DbgaufsXi_PREFIX "xi"
6538+
6539+static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6540+{
acd2b654 6541+ int err, idx;
1facf9fc 6542+ long l;
acd2b654
AM
6543+ aufs_bindex_t bindex;
6544+ char *p, a[sizeof(DbgaufsXi_PREFIX) + 8];
1facf9fc 6545+ struct au_sbinfo *sbinfo;
6546+ struct super_block *sb;
acd2b654 6547+ struct au_xino *xi;
1facf9fc 6548+ struct file *xf;
6549+ struct qstr *name;
062440b3 6550+ struct au_branch *br;
1facf9fc 6551+
6552+ err = -ENOENT;
2000de60 6553+ name = &file->f_path.dentry->d_name;
1facf9fc 6554+ if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6555+ || memcmp(name->name, DbgaufsXi_PREFIX,
6556+ sizeof(DbgaufsXi_PREFIX) - 1)))
6557+ goto out;
acd2b654
AM
6558+
6559+ AuDebugOn(name->len >= sizeof(a));
6560+ memcpy(a, name->name, name->len);
6561+ a[name->len] = '\0';
6562+ p = strchr(a, '-');
6563+ if (p)
6564+ *p = '\0';
6565+ err = kstrtol(a + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
1facf9fc 6566+ if (unlikely(err))
6567+ goto out;
acd2b654
AM
6568+ bindex = l;
6569+ idx = 0;
6570+ if (p) {
6571+ err = kstrtol(p + 1, 10, &l);
6572+ if (unlikely(err))
6573+ goto out;
6574+ idx = l;
6575+ }
1facf9fc 6576+
acd2b654 6577+ err = -ENOENT;
1facf9fc 6578+ sbinfo = inode->i_private;
6579+ sb = sbinfo->si_sb;
6580+ si_noflush_read_lock(sb);
acd2b654
AM
6581+ if (unlikely(bindex < 0 || bindex > au_sbbot(sb)))
6582+ goto out_si;
6583+ br = au_sbr(sb, bindex);
6584+ xi = br->br_xino;
6585+ if (unlikely(idx >= xi->xi_nfile))
6586+ goto out_si;
6587+ xf = au_xino_file(xi, idx);
6588+ if (xf)
062440b3
AM
6589+ err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1,
6590+ au_xino_count(br));
1facf9fc 6591+
acd2b654
AM
6592+out_si:
6593+ si_read_unlock(sb);
4f0767ce 6594+out:
acd2b654 6595+ AuTraceErr(err);
1facf9fc 6596+ return err;
6597+}
6598+
6599+static const struct file_operations dbgaufs_xino_fop = {
4a4d8108 6600+ .owner = THIS_MODULE,
1facf9fc 6601+ .open = dbgaufs_xino_open,
6602+ .release = dbgaufs_xi_release,
6603+ .read = dbgaufs_xi_read
6604+};
6605+
062440b3
AM
6606+void dbgaufs_xino_del(struct au_branch *br)
6607+{
6608+ struct dentry *dbgaufs;
6609+
6610+ dbgaufs = br->br_dbgaufs;
6611+ if (!dbgaufs)
6612+ return;
6613+
6614+ br->br_dbgaufs = NULL;
6615+ /* debugfs acquires the parent i_mutex */
6616+ lockdep_off();
6617+ debugfs_remove(dbgaufs);
6618+ lockdep_on();
6619+}
6620+
1facf9fc 6621+void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6622+{
5afbbe0d 6623+ aufs_bindex_t bbot;
1facf9fc 6624+ struct au_branch *br;
1facf9fc 6625+
6626+ if (!au_sbi(sb)->si_dbgaufs)
6627+ return;
6628+
5afbbe0d
AM
6629+ bbot = au_sbbot(sb);
6630+ for (; bindex <= bbot; bindex++) {
1facf9fc 6631+ br = au_sbr(sb, bindex);
062440b3
AM
6632+ dbgaufs_xino_del(br);
6633+ }
6634+}
6635+
acd2b654
AM
6636+static void dbgaufs_br_do_add(struct super_block *sb, aufs_bindex_t bindex,
6637+ unsigned int idx, struct dentry *parent,
6638+ struct au_sbinfo *sbinfo)
062440b3
AM
6639+{
6640+ struct au_branch *br;
6641+ struct dentry *d;
acd2b654
AM
6642+ /* "xi" bindex(5) "-" idx(2) NULL */
6643+ char name[sizeof(DbgaufsXi_PREFIX) + 8];
062440b3 6644+
acd2b654
AM
6645+ if (!idx)
6646+ snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6647+ else
6648+ snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d-%u",
6649+ bindex, idx);
062440b3
AM
6650+ br = au_sbr(sb, bindex);
6651+ if (br->br_dbgaufs) {
6652+ struct qstr qstr = QSTR_INIT(name, strlen(name));
6653+
6654+ if (!au_qstreq(&br->br_dbgaufs->d_name, &qstr)) {
6655+ /* debugfs acquires the parent i_mutex */
6656+ lockdep_off();
6657+ d = debugfs_rename(parent, br->br_dbgaufs, parent,
6658+ name);
6659+ lockdep_on();
6660+ if (unlikely(!d))
6661+ pr_warn("failed renaming %pd/%s, ignored.\n",
6662+ parent, name);
6663+ }
6664+ } else {
e2f27e51 6665+ lockdep_off();
062440b3
AM
6666+ br->br_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6667+ sbinfo, &dbgaufs_xino_fop);
e2f27e51 6668+ lockdep_on();
062440b3 6669+ if (unlikely(!br->br_dbgaufs))
acd2b654 6670+ pr_warn("failed creating %pd/%s, ignored.\n",
062440b3 6671+ parent, name);
1facf9fc 6672+ }
6673+}
6674+
acd2b654
AM
6675+static void dbgaufs_br_add(struct super_block *sb, aufs_bindex_t bindex,
6676+ struct dentry *parent, struct au_sbinfo *sbinfo)
6677+{
6678+ struct au_branch *br;
6679+ struct au_xino *xi;
6680+ unsigned int u;
6681+
6682+ br = au_sbr(sb, bindex);
6683+ xi = br->br_xino;
6684+ for (u = 0; u < xi->xi_nfile; u++)
6685+ dbgaufs_br_do_add(sb, bindex, u, parent, sbinfo);
6686+}
6687+
062440b3 6688+void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown)
1facf9fc 6689+{
6690+ struct au_sbinfo *sbinfo;
6691+ struct dentry *parent;
5afbbe0d 6692+ aufs_bindex_t bbot;
062440b3
AM
6693+
6694+ if (!au_opt_test(au_mntflags(sb), XINO))
6695+ return;
1facf9fc 6696+
6697+ sbinfo = au_sbi(sb);
6698+ parent = sbinfo->si_dbgaufs;
6699+ if (!parent)
6700+ return;
6701+
5afbbe0d 6702+ bbot = au_sbbot(sb);
062440b3
AM
6703+ if (topdown)
6704+ for (; bindex <= bbot; bindex++)
6705+ dbgaufs_br_add(sb, bindex, parent, sbinfo);
6706+ else
6707+ for (; bbot >= bindex; bbot--)
6708+ dbgaufs_br_add(sb, bbot, parent, sbinfo);
1facf9fc 6709+}
6710+
6711+/* ---------------------------------------------------------------------- */
6712+
6713+#ifdef CONFIG_AUFS_EXPORT
6714+static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6715+{
6716+ int err;
6717+ struct au_sbinfo *sbinfo;
6718+ struct super_block *sb;
6719+
6720+ sbinfo = inode->i_private;
6721+ sb = sbinfo->si_sb;
6722+ si_noflush_read_lock(sb);
062440b3 6723+ err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0, /*cnt*/0);
1facf9fc 6724+ si_read_unlock(sb);
6725+ return err;
6726+}
6727+
6728+static const struct file_operations dbgaufs_xigen_fop = {
4a4d8108 6729+ .owner = THIS_MODULE,
1facf9fc 6730+ .open = dbgaufs_xigen_open,
6731+ .release = dbgaufs_xi_release,
6732+ .read = dbgaufs_xi_read
6733+};
6734+
6735+static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6736+{
6737+ int err;
6738+
dece6358 6739+ /*
c1595e42 6740+ * This function is a dynamic '__init' function actually,
dece6358
AM
6741+ * so the tiny check for si_rwsem is unnecessary.
6742+ */
6743+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6744+
1facf9fc 6745+ err = -EIO;
6746+ sbinfo->si_dbgaufs_xigen = debugfs_create_file
6747+ ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6748+ &dbgaufs_xigen_fop);
6749+ if (sbinfo->si_dbgaufs_xigen)
6750+ err = 0;
6751+
6752+ return err;
6753+}
6754+#else
6755+static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6756+{
6757+ return 0;
6758+}
6759+#endif /* CONFIG_AUFS_EXPORT */
6760+
6761+/* ---------------------------------------------------------------------- */
6762+
6763+void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6764+{
dece6358 6765+ /*
7e9cd9fe 6766+ * This function is a dynamic '__fin' function actually,
dece6358
AM
6767+ * so the tiny check for si_rwsem is unnecessary.
6768+ */
6769+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6770+
1facf9fc 6771+ debugfs_remove_recursive(sbinfo->si_dbgaufs);
6772+ sbinfo->si_dbgaufs = NULL;
1facf9fc 6773+}
6774+
6775+int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6776+{
6777+ int err;
6778+ char name[SysaufsSiNameLen];
6779+
dece6358 6780+ /*
c1595e42 6781+ * This function is a dynamic '__init' function actually,
dece6358
AM
6782+ * so the tiny check for si_rwsem is unnecessary.
6783+ */
6784+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6785+
1facf9fc 6786+ err = -ENOENT;
6787+ if (!dbgaufs) {
6788+ AuErr1("/debug/aufs is uninitialized\n");
6789+ goto out;
6790+ }
6791+
6792+ err = -EIO;
6793+ sysaufs_name(sbinfo, name);
6794+ sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6795+ if (unlikely(!sbinfo->si_dbgaufs))
6796+ goto out;
1facf9fc 6797+
062440b3 6798+ /* regardless plink/noplink option */
86dc4139
AM
6799+ sbinfo->si_dbgaufs_plink = debugfs_create_file
6800+ ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6801+ &dbgaufs_plink_fop);
6802+ if (unlikely(!sbinfo->si_dbgaufs_plink))
6803+ goto out_dir;
6804+
062440b3
AM
6805+ /* regardless xino/noxino option */
6806+ sbinfo->si_dbgaufs_xib = debugfs_create_file
6807+ ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6808+ &dbgaufs_xib_fop);
6809+ if (unlikely(!sbinfo->si_dbgaufs_xib))
6810+ goto out_dir;
6811+
1facf9fc 6812+ err = dbgaufs_xigen_init(sbinfo);
6813+ if (!err)
6814+ goto out; /* success */
6815+
4f0767ce 6816+out_dir:
1facf9fc 6817+ dbgaufs_si_fin(sbinfo);
4f0767ce 6818+out:
062440b3
AM
6819+ if (unlikely(err))
6820+ pr_err("debugfs/aufs failed\n");
1facf9fc 6821+ return err;
6822+}
6823+
6824+/* ---------------------------------------------------------------------- */
6825+
6826+void dbgaufs_fin(void)
6827+{
6828+ debugfs_remove(dbgaufs);
6829+}
6830+
6831+int __init dbgaufs_init(void)
6832+{
6833+ int err;
6834+
6835+ err = -EIO;
6836+ dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
6837+ if (dbgaufs)
6838+ err = 0;
6839+ return err;
6840+}
7f207e10 6841diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
eca34b5c 6842--- /usr/share/empty/fs/aufs/dbgaufs.h 1970-01-01 01:00:00.000000000 +0100
594d0238 6843+++ linux/fs/aufs/dbgaufs.h 2022-11-05 23:02:18.962555950 +0100
062440b3
AM
6844@@ -0,0 +1,53 @@
6845+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 6846+/*
0dcfbb52 6847+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 6848+ *
0dcfbb52 6849+ * This program is free software; you can redistribute it and/or modify
1facf9fc 6850+ * it under the terms of the GNU General Public License as published by
6851+ * the Free Software Foundation; either version 2 of the License, or
6852+ * (at your option) any later version.
dece6358
AM
6853+ *
6854+ * This program is distributed in the hope that it will be useful,
6855+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6856+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6857+ * GNU General Public License for more details.
6858+ *
6859+ * You should have received a copy of the GNU General Public License
523b37e3 6860+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6861+ */
6862+
6863+/*
6864+ * debugfs interface
6865+ */
6866+
6867+#ifndef __DBGAUFS_H__
6868+#define __DBGAUFS_H__
6869+
6870+#ifdef __KERNEL__
6871+
dece6358 6872+struct super_block;
1facf9fc 6873+struct au_sbinfo;
062440b3 6874+struct au_branch;
dece6358 6875+
1facf9fc 6876+#ifdef CONFIG_DEBUG_FS
6877+/* dbgaufs.c */
062440b3 6878+void dbgaufs_xino_del(struct au_branch *br);
1facf9fc 6879+void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
062440b3 6880+void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown);
1facf9fc 6881+void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
6882+int dbgaufs_si_init(struct au_sbinfo *sbinfo);
6883+void dbgaufs_fin(void);
6884+int __init dbgaufs_init(void);
1facf9fc 6885+#else
062440b3 6886+AuStubVoid(dbgaufs_xino_del, struct au_branch *br)
4a4d8108 6887+AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
062440b3
AM
6888+AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex,
6889+ int topdown)
4a4d8108
AM
6890+AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
6891+AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
6892+AuStubVoid(dbgaufs_fin, void)
6893+AuStubInt0(__init dbgaufs_init, void)
1facf9fc 6894+#endif /* CONFIG_DEBUG_FS */
6895+
6896+#endif /* __KERNEL__ */
6897+#endif /* __DBGAUFS_H__ */
7f207e10 6898diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
eca34b5c 6899--- /usr/share/empty/fs/aufs/dcsub.c 1970-01-01 01:00:00.000000000 +0100
594d0238 6900+++ linux/fs/aufs/dcsub.c 2022-11-05 23:02:18.962555950 +0100
cd7a4cd9
AM
6901@@ -0,0 +1,225 @@
6902+// SPDX-License-Identifier: GPL-2.0
1facf9fc 6903+/*
0dcfbb52 6904+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 6905+ *
0dcfbb52 6906+ * This program is free software; you can redistribute it and/or modify
1facf9fc 6907+ * it under the terms of the GNU General Public License as published by
6908+ * the Free Software Foundation; either version 2 of the License, or
6909+ * (at your option) any later version.
dece6358
AM
6910+ *
6911+ * This program is distributed in the hope that it will be useful,
6912+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6913+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6914+ * GNU General Public License for more details.
6915+ *
6916+ * You should have received a copy of the GNU General Public License
523b37e3 6917+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6918+ */
6919+
6920+/*
6921+ * sub-routines for dentry cache
6922+ */
6923+
6924+#include "aufs.h"
6925+
6926+static void au_dpage_free(struct au_dpage *dpage)
6927+{
6928+ int i;
6929+ struct dentry **p;
6930+
6931+ p = dpage->dentries;
6932+ for (i = 0; i < dpage->ndentry; i++)
6933+ dput(*p++);
1c60b727 6934+ free_page((unsigned long)dpage->dentries);
1facf9fc 6935+}
6936+
6937+int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
6938+{
6939+ int err;
6940+ void *p;
6941+
6942+ err = -ENOMEM;
6943+ dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
6944+ if (unlikely(!dpages->dpages))
6945+ goto out;
6946+
6947+ p = (void *)__get_free_page(gfp);
6948+ if (unlikely(!p))
6949+ goto out_dpages;
6950+
6951+ dpages->dpages[0].ndentry = 0;
6952+ dpages->dpages[0].dentries = p;
6953+ dpages->ndpage = 1;
6954+ return 0; /* success */
6955+
4f0767ce 6956+out_dpages:
9f237c51 6957+ au_kfree_try_rcu(dpages->dpages);
4f0767ce 6958+out:
1facf9fc 6959+ return err;
6960+}
6961+
6962+void au_dpages_free(struct au_dcsub_pages *dpages)
6963+{
6964+ int i;
6965+ struct au_dpage *p;
6966+
6967+ p = dpages->dpages;
6968+ for (i = 0; i < dpages->ndpage; i++)
6969+ au_dpage_free(p++);
9f237c51 6970+ au_kfree_try_rcu(dpages->dpages);
1facf9fc 6971+}
6972+
6973+static int au_dpages_append(struct au_dcsub_pages *dpages,
6974+ struct dentry *dentry, gfp_t gfp)
6975+{
6976+ int err, sz;
6977+ struct au_dpage *dpage;
6978+ void *p;
6979+
6980+ dpage = dpages->dpages + dpages->ndpage - 1;
6981+ sz = PAGE_SIZE / sizeof(dentry);
6982+ if (unlikely(dpage->ndentry >= sz)) {
6983+ AuLabel(new dpage);
6984+ err = -ENOMEM;
6985+ sz = dpages->ndpage * sizeof(*dpages->dpages);
6986+ p = au_kzrealloc(dpages->dpages, sz,
e2f27e51
AM
6987+ sz + sizeof(*dpages->dpages), gfp,
6988+ /*may_shrink*/0);
1facf9fc 6989+ if (unlikely(!p))
6990+ goto out;
6991+
6992+ dpages->dpages = p;
6993+ dpage = dpages->dpages + dpages->ndpage;
6994+ p = (void *)__get_free_page(gfp);
6995+ if (unlikely(!p))
6996+ goto out;
6997+
6998+ dpage->ndentry = 0;
6999+ dpage->dentries = p;
7000+ dpages->ndpage++;
7001+ }
7002+
c1595e42 7003+ AuDebugOn(au_dcount(dentry) <= 0);
027c5e7a 7004+ dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
1facf9fc 7005+ return 0; /* success */
7006+
4f0767ce 7007+out:
1facf9fc 7008+ return err;
7009+}
7010+
c1595e42
JR
7011+/* todo: BAD approach */
7012+/* copied from linux/fs/dcache.c */
7013+enum d_walk_ret {
7014+ D_WALK_CONTINUE,
7015+ D_WALK_QUIT,
7016+ D_WALK_NORETRY,
7017+ D_WALK_SKIP,
7018+};
7019+
7020+extern void d_walk(struct dentry *parent, void *data,
cd7a4cd9 7021+ enum d_walk_ret (*enter)(void *, struct dentry *));
c1595e42
JR
7022+
7023+struct ac_dpages_arg {
1facf9fc 7024+ int err;
c1595e42
JR
7025+ struct au_dcsub_pages *dpages;
7026+ struct super_block *sb;
7027+ au_dpages_test test;
7028+ void *arg;
7029+};
1facf9fc 7030+
c1595e42
JR
7031+static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
7032+{
7033+ enum d_walk_ret ret;
7034+ struct ac_dpages_arg *arg = _arg;
1facf9fc 7035+
c1595e42
JR
7036+ ret = D_WALK_CONTINUE;
7037+ if (dentry->d_sb == arg->sb
7038+ && !IS_ROOT(dentry)
7039+ && au_dcount(dentry) > 0
7040+ && au_di(dentry)
7041+ && (!arg->test || arg->test(dentry, arg->arg))) {
7042+ arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
7043+ if (unlikely(arg->err))
7044+ ret = D_WALK_QUIT;
1facf9fc 7045+ }
7046+
c1595e42
JR
7047+ return ret;
7048+}
027c5e7a 7049+
c1595e42
JR
7050+int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7051+ au_dpages_test test, void *arg)
7052+{
7053+ struct ac_dpages_arg args = {
7054+ .err = 0,
7055+ .dpages = dpages,
7056+ .sb = root->d_sb,
7057+ .test = test,
7058+ .arg = arg
7059+ };
027c5e7a 7060+
cd7a4cd9 7061+ d_walk(root, &args, au_call_dpages_append);
c1595e42
JR
7062+
7063+ return args.err;
1facf9fc 7064+}
7065+
7066+int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7067+ int do_include, au_dpages_test test, void *arg)
7068+{
7069+ int err;
7070+
7071+ err = 0;
027c5e7a
AM
7072+ write_seqlock(&rename_lock);
7073+ spin_lock(&dentry->d_lock);
7074+ if (do_include
c1595e42 7075+ && au_dcount(dentry) > 0
027c5e7a 7076+ && (!test || test(dentry, arg)))
1facf9fc 7077+ err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
027c5e7a
AM
7078+ spin_unlock(&dentry->d_lock);
7079+ if (unlikely(err))
7080+ goto out;
7081+
7082+ /*
523b37e3 7083+ * RCU for vfsmount is unnecessary since this is a traverse in a single
027c5e7a
AM
7084+ * mount
7085+ */
1facf9fc 7086+ while (!IS_ROOT(dentry)) {
027c5e7a
AM
7087+ dentry = dentry->d_parent; /* rename_lock is locked */
7088+ spin_lock(&dentry->d_lock);
c1595e42 7089+ if (au_dcount(dentry) > 0
027c5e7a 7090+ && (!test || test(dentry, arg)))
1facf9fc 7091+ err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
027c5e7a
AM
7092+ spin_unlock(&dentry->d_lock);
7093+ if (unlikely(err))
7094+ break;
1facf9fc 7095+ }
7096+
4f0767ce 7097+out:
027c5e7a 7098+ write_sequnlock(&rename_lock);
1facf9fc 7099+ return err;
7100+}
7101+
027c5e7a
AM
7102+static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
7103+{
7104+ return au_di(dentry) && dentry->d_sb == arg;
7105+}
7106+
7107+int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7108+ struct dentry *dentry, int do_include)
7109+{
7110+ return au_dcsub_pages_rev(dpages, dentry, do_include,
7111+ au_dcsub_dpages_aufs, dentry->d_sb);
7112+}
7113+
4a4d8108 7114+int au_test_subdir(struct dentry *d1, struct dentry *d2)
1facf9fc 7115+{
4a4d8108
AM
7116+ struct path path[2] = {
7117+ {
7118+ .dentry = d1
7119+ },
7120+ {
7121+ .dentry = d2
7122+ }
7123+ };
1facf9fc 7124+
4a4d8108 7125+ return path_is_under(path + 0, path + 1);
1facf9fc 7126+}
7f207e10 7127diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
eca34b5c 7128--- /usr/share/empty/fs/aufs/dcsub.h 1970-01-01 01:00:00.000000000 +0100
594d0238 7129+++ linux/fs/aufs/dcsub.h 2022-11-05 23:02:18.962555950 +0100
062440b3
AM
7130@@ -0,0 +1,137 @@
7131+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 7132+/*
0dcfbb52 7133+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 7134+ *
0dcfbb52 7135+ * This program is free software; you can redistribute it and/or modify
1facf9fc 7136+ * it under the terms of the GNU General Public License as published by
7137+ * the Free Software Foundation; either version 2 of the License, or
7138+ * (at your option) any later version.
dece6358
AM
7139+ *
7140+ * This program is distributed in the hope that it will be useful,
7141+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7142+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7143+ * GNU General Public License for more details.
7144+ *
7145+ * You should have received a copy of the GNU General Public License
523b37e3 7146+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7147+ */
7148+
7149+/*
7150+ * sub-routines for dentry cache
7151+ */
7152+
7153+#ifndef __AUFS_DCSUB_H__
7154+#define __AUFS_DCSUB_H__
7155+
7156+#ifdef __KERNEL__
7157+
7f207e10 7158+#include <linux/dcache.h>
027c5e7a 7159+#include <linux/fs.h>
dece6358 7160+
1facf9fc 7161+struct au_dpage {
7162+ int ndentry;
7163+ struct dentry **dentries;
7164+};
7165+
7166+struct au_dcsub_pages {
7167+ int ndpage;
7168+ struct au_dpage *dpages;
7169+};
7170+
7171+/* ---------------------------------------------------------------------- */
7172+
7f207e10 7173+/* dcsub.c */
1facf9fc 7174+int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
7175+void au_dpages_free(struct au_dcsub_pages *dpages);
7176+typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
7177+int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7178+ au_dpages_test test, void *arg);
7179+int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7180+ int do_include, au_dpages_test test, void *arg);
027c5e7a
AM
7181+int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7182+ struct dentry *dentry, int do_include);
4a4d8108 7183+int au_test_subdir(struct dentry *d1, struct dentry *d2);
1facf9fc 7184+
7f207e10
AM
7185+/* ---------------------------------------------------------------------- */
7186+
523b37e3
AM
7187+/*
7188+ * todo: in linux-3.13, several similar (but faster) helpers are added to
7189+ * include/linux/dcache.h. Try them (in the future).
7190+ */
7191+
027c5e7a
AM
7192+static inline int au_d_hashed_positive(struct dentry *d)
7193+{
7194+ int err;
5527c038 7195+ struct inode *inode = d_inode(d);
076b876e 7196+
027c5e7a 7197+ err = 0;
5527c038
JR
7198+ if (unlikely(d_unhashed(d)
7199+ || d_is_negative(d)
7200+ || !inode->i_nlink))
027c5e7a
AM
7201+ err = -ENOENT;
7202+ return err;
7203+}
7204+
38d290e6
JR
7205+static inline int au_d_linkable(struct dentry *d)
7206+{
7207+ int err;
5527c038 7208+ struct inode *inode = d_inode(d);
076b876e 7209+
38d290e6
JR
7210+ err = au_d_hashed_positive(d);
7211+ if (err
5527c038 7212+ && d_is_positive(d)
38d290e6
JR
7213+ && (inode->i_state & I_LINKABLE))
7214+ err = 0;
7215+ return err;
7216+}
7217+
027c5e7a
AM
7218+static inline int au_d_alive(struct dentry *d)
7219+{
7220+ int err;
7221+ struct inode *inode;
076b876e 7222+
027c5e7a
AM
7223+ err = 0;
7224+ if (!IS_ROOT(d))
7225+ err = au_d_hashed_positive(d);
7226+ else {
5527c038
JR
7227+ inode = d_inode(d);
7228+ if (unlikely(d_unlinked(d)
7229+ || d_is_negative(d)
7230+ || !inode->i_nlink))
027c5e7a
AM
7231+ err = -ENOENT;
7232+ }
7233+ return err;
7234+}
7235+
7236+static inline int au_alive_dir(struct dentry *d)
7f207e10 7237+{
027c5e7a 7238+ int err;
076b876e 7239+
027c5e7a 7240+ err = au_d_alive(d);
5527c038 7241+ if (unlikely(err || IS_DEADDIR(d_inode(d))))
027c5e7a
AM
7242+ err = -ENOENT;
7243+ return err;
7f207e10
AM
7244+}
7245+
38d290e6
JR
7246+static inline int au_qstreq(struct qstr *a, struct qstr *b)
7247+{
7248+ return a->len == b->len
7249+ && !memcmp(a->name, b->name, a->len);
7250+}
7251+
7e9cd9fe
AM
7252+/*
7253+ * by the commit
7254+ * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
7255+ * taking d_lock
7256+ * the type of d_lockref.count became int, but the inlined function d_count()
7257+ * still returns unsigned int.
7258+ * I don't know why. Maybe it is for every d_count() users?
7259+ * Anyway au_dcount() lives on.
7260+ */
c1595e42
JR
7261+static inline int au_dcount(struct dentry *d)
7262+{
7263+ return (int)d_count(d);
7264+}
7265+
1facf9fc 7266+#endif /* __KERNEL__ */
7267+#endif /* __AUFS_DCSUB_H__ */
7f207e10 7268diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
eca34b5c 7269--- /usr/share/empty/fs/aufs/debug.c 1970-01-01 01:00:00.000000000 +0100
631230db
AM
7270+++ linux/fs/aufs/debug.c 2023-10-31 09:31:04.196547417 +0100
7271@@ -0,0 +1,448 @@
cd7a4cd9 7272+// SPDX-License-Identifier: GPL-2.0
1facf9fc 7273+/*
0dcfbb52 7274+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 7275+ *
0dcfbb52 7276+ * This program is free software; you can redistribute it and/or modify
1facf9fc 7277+ * it under the terms of the GNU General Public License as published by
7278+ * the Free Software Foundation; either version 2 of the License, or
7279+ * (at your option) any later version.
dece6358
AM
7280+ *
7281+ * This program is distributed in the hope that it will be useful,
7282+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7283+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7284+ * GNU General Public License for more details.
7285+ *
7286+ * You should have received a copy of the GNU General Public License
523b37e3 7287+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7288+ */
7289+
7290+/*
7291+ * debug print functions
7292+ */
7293+
eca801bf 7294+#include <linux/iversion.h>
1facf9fc 7295+#include "aufs.h"
7296+
392086de
AM
7297+/* Returns 0, or -errno. arg is in kp->arg. */
7298+static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
7299+{
7300+ int err, n;
7301+
7302+ err = kstrtoint(val, 0, &n);
7303+ if (!err) {
7304+ if (n > 0)
7305+ au_debug_on();
7306+ else
7307+ au_debug_off();
7308+ }
7309+ return err;
7310+}
7311+
7312+/* Returns length written or -errno. Buffer is 4k (ie. be short!) */
7313+static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
7314+{
7315+ atomic_t *a;
7316+
7317+ a = kp->arg;
7318+ return sprintf(buffer, "%d", atomic_read(a));
7319+}
7320+
22319442 7321+static const struct kernel_param_ops param_ops_atomic_t = {
392086de
AM
7322+ .set = param_atomic_t_set,
7323+ .get = param_atomic_t_get
7324+ /* void (*free)(void *arg) */
7325+};
7326+
7327+atomic_t aufs_debug = ATOMIC_INIT(0);
1facf9fc 7328+MODULE_PARM_DESC(debug, "debug print");
cd7a4cd9 7329+module_param_named(debug, aufs_debug, atomic_t, 0664);
1facf9fc 7330+
c1595e42 7331+DEFINE_MUTEX(au_dbg_mtx); /* just to serialize the dbg msgs */
1facf9fc 7332+char *au_plevel = KERN_DEBUG;
e49829fe
JR
7333+#define dpri(fmt, ...) do { \
7334+ if ((au_plevel \
7335+ && strcmp(au_plevel, KERN_DEBUG)) \
7336+ || au_debug_test()) \
7337+ printk("%s" fmt, au_plevel, ##__VA_ARGS__); \
1facf9fc 7338+} while (0)
7339+
7340+/* ---------------------------------------------------------------------- */
7341+
7342+void au_dpri_whlist(struct au_nhash *whlist)
7343+{
7344+ unsigned long ul, n;
7345+ struct hlist_head *head;
c06a8ce3 7346+ struct au_vdir_wh *pos;
1facf9fc 7347+
7348+ n = whlist->nh_num;
7349+ head = whlist->nh_head;
7350+ for (ul = 0; ul < n; ul++) {
c06a8ce3 7351+ hlist_for_each_entry(pos, head, wh_hash)
1facf9fc 7352+ dpri("b%d, %.*s, %d\n",
c06a8ce3
AM
7353+ pos->wh_bindex,
7354+ pos->wh_str.len, pos->wh_str.name,
7355+ pos->wh_str.len);
1facf9fc 7356+ head++;
7357+ }
7358+}
7359+
7360+void au_dpri_vdir(struct au_vdir *vdir)
7361+{
7362+ unsigned long ul;
7363+ union au_vdir_deblk_p p;
7364+ unsigned char *o;
7365+
7366+ if (!vdir || IS_ERR(vdir)) {
7367+ dpri("err %ld\n", PTR_ERR(vdir));
7368+ return;
7369+ }
7370+
be118d29 7371+ dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %llu\n",
1facf9fc 7372+ vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
7373+ vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
7374+ for (ul = 0; ul < vdir->vd_nblk; ul++) {
7375+ p.deblk = vdir->vd_deblk[ul];
7376+ o = p.deblk;
7377+ dpri("[%lu]: %p\n", ul, o);
7378+ }
7379+}
7380+
53392da6 7381+static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
1facf9fc 7382+ struct dentry *wh)
7383+{
7384+ char *n = NULL;
7385+ int l = 0;
631230db 7386+ struct timespec64 ctime;
1facf9fc 7387+
7388+ if (!inode || IS_ERR(inode)) {
7389+ dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7390+ return -1;
7391+ }
7392+
c2b27bf2 7393+ /* the type of i_blocks depends upon CONFIG_LBDAF */
1facf9fc 7394+ BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7395+ && sizeof(inode->i_blocks) != sizeof(u64));
7396+ if (wh) {
7397+ n = (void *)wh->d_name.name;
7398+ l = wh->d_name.len;
7399+ }
7400+
631230db 7401+ ctime = inode_get_ctime(inode);
53392da6 7402+ dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
aec24f17 7403+ " acl %p, def_acl %p,"
53392da6
AM
7404+ " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7405+ bindex, inode,
1facf9fc 7406+ inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7407+ atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7408+ i_size_read(inode), (unsigned long long)inode->i_blocks,
aec24f17 7409+ inode->i_acl, inode->i_default_acl,
631230db 7410+ hn, (long long)timespec64_to_ns(&ctime) & 0x0ffff,
1facf9fc 7411+ inode->i_mapping ? inode->i_mapping->nrpages : 0,
be118d29 7412+ inode->i_state, inode->i_flags, inode_peek_iversion(inode),
b752ccd1 7413+ inode->i_generation,
1facf9fc 7414+ l ? ", wh " : "", l, n);
7415+ return 0;
7416+}
7417+
7418+void au_dpri_inode(struct inode *inode)
7419+{
7420+ struct au_iinfo *iinfo;
5afbbe0d 7421+ struct au_hinode *hi;
1facf9fc 7422+ aufs_bindex_t bindex;
53392da6 7423+ int err, hn;
1facf9fc 7424+
53392da6 7425+ err = do_pri_inode(-1, inode, -1, NULL);
5afbbe0d 7426+ if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
1facf9fc 7427+ return;
7428+
7429+ iinfo = au_ii(inode);
5afbbe0d
AM
7430+ dpri("i-1: btop %d, bbot %d, gen %d\n",
7431+ iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
7432+ if (iinfo->ii_btop < 0)
1facf9fc 7433+ return;
53392da6 7434+ hn = 0;
5afbbe0d
AM
7435+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
7436+ hi = au_hinode(iinfo, bindex);
7437+ hn = !!au_hn(hi);
7438+ do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
53392da6 7439+ }
1facf9fc 7440+}
7441+
2cbb1c4b
JR
7442+void au_dpri_dalias(struct inode *inode)
7443+{
7444+ struct dentry *d;
7445+
7446+ spin_lock(&inode->i_lock);
c1595e42 7447+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
2cbb1c4b
JR
7448+ au_dpri_dentry(d);
7449+ spin_unlock(&inode->i_lock);
7450+}
7451+
1facf9fc 7452+static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7453+{
7454+ struct dentry *wh = NULL;
53392da6 7455+ int hn;
5afbbe0d 7456+ struct inode *inode;
076b876e 7457+ struct au_iinfo *iinfo;
5afbbe0d 7458+ struct au_hinode *hi;
1facf9fc 7459+
7460+ if (!dentry || IS_ERR(dentry)) {
7461+ dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7462+ return -1;
7463+ }
7464+ /* do not call dget_parent() here */
027c5e7a 7465+ /* note: access d_xxx without d_lock */
523b37e3
AM
7466+ dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7467+ bindex, dentry, dentry,
1facf9fc 7468+ dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
c1595e42 7469+ au_dcount(dentry), dentry->d_flags,
523b37e3 7470+ d_unhashed(dentry) ? "un" : "");
53392da6 7471+ hn = -1;
5afbbe0d
AM
7472+ inode = NULL;
7473+ if (d_is_positive(dentry))
7474+ inode = d_inode(dentry);
7475+ if (inode
7476+ && au_test_aufs(dentry->d_sb)
7477+ && bindex >= 0
7478+ && !au_is_bad_inode(inode)) {
7479+ iinfo = au_ii(inode);
7480+ hi = au_hinode(iinfo, bindex);
7481+ hn = !!au_hn(hi);
7482+ wh = hi->hi_whdentry;
7483+ }
7484+ do_pri_inode(bindex, inode, hn, wh);
1facf9fc 7485+ return 0;
7486+}
7487+
7488+void au_dpri_dentry(struct dentry *dentry)
7489+{
7490+ struct au_dinfo *dinfo;
7491+ aufs_bindex_t bindex;
7492+ int err;
7493+
7494+ err = do_pri_dentry(-1, dentry);
7495+ if (err || !au_test_aufs(dentry->d_sb))
7496+ return;
7497+
7498+ dinfo = au_di(dentry);
7499+ if (!dinfo)
7500+ return;
5afbbe0d
AM
7501+ dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7502+ dinfo->di_btop, dinfo->di_bbot,
38d290e6
JR
7503+ dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7504+ dinfo->di_tmpfile);
5afbbe0d 7505+ if (dinfo->di_btop < 0)
1facf9fc 7506+ return;
5afbbe0d
AM
7507+ for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
7508+ do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
1facf9fc 7509+}
7510+
7511+static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7512+{
7513+ char a[32];
7514+
7515+ if (!file || IS_ERR(file)) {
7516+ dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7517+ return -1;
7518+ }
7519+ a[0] = 0;
7520+ if (bindex < 0
b912730e 7521+ && !IS_ERR_OR_NULL(file->f_path.dentry)
2000de60 7522+ && au_test_aufs(file->f_path.dentry->d_sb)
1facf9fc 7523+ && au_fi(file))
e49829fe 7524+ snprintf(a, sizeof(a), ", gen %d, mmapped %d",
2cbb1c4b 7525+ au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
b752ccd1 7526+ dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
1facf9fc 7527+ bindex, file->f_mode, file->f_flags, (long)file_count(file),
b752ccd1 7528+ file->f_version, file->f_pos, a);
b912730e 7529+ if (!IS_ERR_OR_NULL(file->f_path.dentry))
2000de60 7530+ do_pri_dentry(bindex, file->f_path.dentry);
1facf9fc 7531+ return 0;
7532+}
7533+
7534+void au_dpri_file(struct file *file)
7535+{
7536+ struct au_finfo *finfo;
4a4d8108
AM
7537+ struct au_fidir *fidir;
7538+ struct au_hfile *hfile;
1facf9fc 7539+ aufs_bindex_t bindex;
7540+ int err;
7541+
7542+ err = do_pri_file(-1, file);
2000de60 7543+ if (err
b912730e 7544+ || IS_ERR_OR_NULL(file->f_path.dentry)
2000de60 7545+ || !au_test_aufs(file->f_path.dentry->d_sb))
1facf9fc 7546+ return;
7547+
7548+ finfo = au_fi(file);
7549+ if (!finfo)
7550+ return;
4a4d8108 7551+ if (finfo->fi_btop < 0)
1facf9fc 7552+ return;
4a4d8108
AM
7553+ fidir = finfo->fi_hdir;
7554+ if (!fidir)
7555+ do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7556+ else
e49829fe
JR
7557+ for (bindex = finfo->fi_btop;
7558+ bindex >= 0 && bindex <= fidir->fd_bbot;
4a4d8108
AM
7559+ bindex++) {
7560+ hfile = fidir->fd_hfile + bindex;
7561+ do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7562+ }
1facf9fc 7563+}
7564+
7565+static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7566+{
7567+ struct vfsmount *mnt;
7568+ struct super_block *sb;
7569+
7570+ if (!br || IS_ERR(br))
7571+ goto out;
86dc4139 7572+ mnt = au_br_mnt(br);
1facf9fc 7573+ if (!mnt || IS_ERR(mnt))
7574+ goto out;
7575+ sb = mnt->mnt_sb;
7576+ if (!sb || IS_ERR(sb))
7577+ goto out;
7578+
acd2b654 7579+ dpri("s%d: {perm 0x%x, id %d, wbr %p}, "
b752ccd1 7580+ "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
1facf9fc 7581+ "xino %d\n",
acd2b654
AM
7582+ bindex, br->br_perm, br->br_id, br->br_wbr,
7583+ au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
b752ccd1 7584+ sb->s_flags, sb->s_count,
acd2b654
AM
7585+ atomic_read(&sb->s_active),
7586+ !!au_xino_file(br->br_xino, /*idx*/-1));
1facf9fc 7587+ return 0;
7588+
4f0767ce 7589+out:
1facf9fc 7590+ dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7591+ return -1;
7592+}
7593+
7594+void au_dpri_sb(struct super_block *sb)
7595+{
7596+ struct au_sbinfo *sbinfo;
7597+ aufs_bindex_t bindex;
7598+ int err;
acd2b654 7599+ /* to reduce stack size */
1facf9fc 7600+ struct {
7601+ struct vfsmount mnt;
7602+ struct au_branch fake;
7603+ } *a;
7604+
7605+ /* this function can be called from magic sysrq */
7606+ a = kzalloc(sizeof(*a), GFP_ATOMIC);
7607+ if (unlikely(!a)) {
7608+ dpri("no memory\n");
7609+ return;
7610+ }
7611+
7612+ a->mnt.mnt_sb = sb;
86dc4139 7613+ a->fake.br_path.mnt = &a->mnt;
1facf9fc 7614+ err = do_pri_br(-1, &a->fake);
9f237c51 7615+ au_kfree_rcu(a);
1facf9fc 7616+ dpri("dev 0x%x\n", sb->s_dev);
7617+ if (err || !au_test_aufs(sb))
7618+ return;
7619+
7620+ sbinfo = au_sbi(sb);
7621+ if (!sbinfo)
7622+ return;
f0c0a007
AM
7623+ dpri("nw %d, gen %u, kobj %d\n",
7624+ atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
521ced18 7625+ kref_read(&sbinfo->si_kobj.kref));
5afbbe0d 7626+ for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
1facf9fc 7627+ do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7628+}
7629+
7630+/* ---------------------------------------------------------------------- */
7631+
027c5e7a
AM
7632+void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7633+{
5527c038 7634+ struct inode *h_inode, *inode = d_inode(dentry);
027c5e7a 7635+ struct dentry *h_dentry;
5afbbe0d 7636+ aufs_bindex_t bindex, bbot, bi;
027c5e7a
AM
7637+
7638+ if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7639+ return;
7640+
5afbbe0d
AM
7641+ bbot = au_dbbot(dentry);
7642+ bi = au_ibbot(inode);
7643+ if (bi < bbot)
7644+ bbot = bi;
7645+ bindex = au_dbtop(dentry);
7646+ bi = au_ibtop(inode);
027c5e7a
AM
7647+ if (bi > bindex)
7648+ bindex = bi;
7649+
5afbbe0d 7650+ for (; bindex <= bbot; bindex++) {
027c5e7a
AM
7651+ h_dentry = au_h_dptr(dentry, bindex);
7652+ if (!h_dentry)
7653+ continue;
7654+ h_inode = au_h_iptr(inode, bindex);
5527c038 7655+ if (unlikely(h_inode != d_inode(h_dentry))) {
392086de 7656+ au_debug_on();
027c5e7a
AM
7657+ AuDbg("b%d, %s:%d\n", bindex, func, line);
7658+ AuDbgDentry(dentry);
7659+ AuDbgInode(inode);
392086de 7660+ au_debug_off();
758e9dad
AM
7661+ if (au_test_fuse(h_inode->i_sb))
7662+ WARN_ON_ONCE(1);
7663+ else
7664+ BUG();
027c5e7a
AM
7665+ }
7666+ }
7667+}
7668+
1facf9fc 7669+void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7670+{
7671+ int err, i, j;
7672+ struct au_dcsub_pages dpages;
7673+ struct au_dpage *dpage;
7674+ struct dentry **dentries;
7675+
7676+ err = au_dpages_init(&dpages, GFP_NOFS);
7677+ AuDebugOn(err);
027c5e7a 7678+ err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
1facf9fc 7679+ AuDebugOn(err);
7680+ for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7681+ dpage = dpages.dpages + i;
7682+ dentries = dpage->dentries;
7683+ for (j = dpage->ndentry - 1; !err && j >= 0; j--)
027c5e7a 7684+ AuDebugOn(au_digen_test(dentries[j], sigen));
1facf9fc 7685+ }
7686+ au_dpages_free(&dpages);
7687+}
7688+
1facf9fc 7689+void au_dbg_verify_kthread(void)
7690+{
53392da6 7691+ if (au_wkq_test()) {
1facf9fc 7692+ au_dbg_blocked();
1e00d052
AM
7693+ /*
7694+ * It may be recursive, but udba=notify between two aufs mounts,
7695+ * where a single ro branch is shared, is not a problem.
7696+ */
7697+ /* WARN_ON(1); */
1facf9fc 7698+ }
7699+}
7700+
7701+/* ---------------------------------------------------------------------- */
7702+
1facf9fc 7703+int __init au_debug_init(void)
7704+{
7705+ aufs_bindex_t bindex;
7706+ struct au_vdir_destr destr;
7707+
7708+ bindex = -1;
7709+ AuDebugOn(bindex >= 0);
7710+
7711+ destr.len = -1;
7712+ AuDebugOn(destr.len < NAME_MAX);
7713+
7714+#ifdef CONFIG_4KSTACKS
0c3ec466 7715+ pr_warn("CONFIG_4KSTACKS is defined.\n");
1facf9fc 7716+#endif
7717+
1facf9fc 7718+ return 0;
7719+}
7f207e10 7720diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
eca34b5c 7721--- /usr/share/empty/fs/aufs/debug.h 1970-01-01 01:00:00.000000000 +0100
594d0238 7722+++ linux/fs/aufs/debug.h 2022-11-05 23:02:18.962555950 +0100
062440b3
AM
7723@@ -0,0 +1,226 @@
7724+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 7725+/*
0dcfbb52 7726+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 7727+ *
0dcfbb52 7728+ * This program is free software; you can redistribute it and/or modify
1facf9fc 7729+ * it under the terms of the GNU General Public License as published by
7730+ * the Free Software Foundation; either version 2 of the License, or
7731+ * (at your option) any later version.
dece6358
AM
7732+ *
7733+ * This program is distributed in the hope that it will be useful,
7734+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7735+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7736+ * GNU General Public License for more details.
7737+ *
7738+ * You should have received a copy of the GNU General Public License
523b37e3 7739+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7740+ */
7741+
7742+/*
7743+ * debug print functions
7744+ */
7745+
7746+#ifndef __AUFS_DEBUG_H__
7747+#define __AUFS_DEBUG_H__
7748+
7749+#ifdef __KERNEL__
7750+
392086de 7751+#include <linux/atomic.h>
4a4d8108
AM
7752+#include <linux/module.h>
7753+#include <linux/kallsyms.h>
1facf9fc 7754+#include <linux/sysrq.h>
4a4d8108 7755+
1facf9fc 7756+#ifdef CONFIG_AUFS_DEBUG
7757+#define AuDebugOn(a) BUG_ON(a)
7758+
7759+/* module parameter */
392086de
AM
7760+extern atomic_t aufs_debug;
7761+static inline void au_debug_on(void)
1facf9fc 7762+{
392086de
AM
7763+ atomic_inc(&aufs_debug);
7764+}
7765+static inline void au_debug_off(void)
7766+{
7767+ atomic_dec_if_positive(&aufs_debug);
1facf9fc 7768+}
7769+
7770+static inline int au_debug_test(void)
7771+{
392086de 7772+ return atomic_read(&aufs_debug) > 0;
1facf9fc 7773+}
7774+#else
7775+#define AuDebugOn(a) do {} while (0)
392086de
AM
7776+AuStubVoid(au_debug_on, void)
7777+AuStubVoid(au_debug_off, void)
4a4d8108 7778+AuStubInt0(au_debug_test, void)
1facf9fc 7779+#endif /* CONFIG_AUFS_DEBUG */
7780+
392086de
AM
7781+#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7782+
1facf9fc 7783+/* ---------------------------------------------------------------------- */
7784+
7785+/* debug print */
7786+
4a4d8108 7787+#define AuDbg(fmt, ...) do { \
1facf9fc 7788+ if (au_debug_test()) \
4a4d8108 7789+ pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
1facf9fc 7790+} while (0)
4a4d8108
AM
7791+#define AuLabel(l) AuDbg(#l "\n")
7792+#define AuIOErr(fmt, ...) pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7793+#define AuWarn1(fmt, ...) do { \
1facf9fc 7794+ static unsigned char _c; \
7795+ if (!_c++) \
0c3ec466 7796+ pr_warn(fmt, ##__VA_ARGS__); \
1facf9fc 7797+} while (0)
7798+
4a4d8108 7799+#define AuErr1(fmt, ...) do { \
1facf9fc 7800+ static unsigned char _c; \
7801+ if (!_c++) \
4a4d8108 7802+ pr_err(fmt, ##__VA_ARGS__); \
1facf9fc 7803+} while (0)
7804+
4a4d8108 7805+#define AuIOErr1(fmt, ...) do { \
1facf9fc 7806+ static unsigned char _c; \
7807+ if (!_c++) \
4a4d8108 7808+ AuIOErr(fmt, ##__VA_ARGS__); \
1facf9fc 7809+} while (0)
7810+
7811+#define AuUnsupportMsg "This operation is not supported." \
7812+ " Please report this application to aufs-users ML."
4a4d8108
AM
7813+#define AuUnsupport(fmt, ...) do { \
7814+ pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
1facf9fc 7815+ dump_stack(); \
7816+} while (0)
7817+
7818+#define AuTraceErr(e) do { \
7819+ if (unlikely((e) < 0)) \
7820+ AuDbg("err %d\n", (int)(e)); \
7821+} while (0)
7822+
7823+#define AuTraceErrPtr(p) do { \
7824+ if (IS_ERR(p)) \
7825+ AuDbg("err %ld\n", PTR_ERR(p)); \
7826+} while (0)
7827+
7828+/* dirty macros for debug print, use with "%.*s" and caution */
7829+#define AuLNPair(qstr) (qstr)->len, (qstr)->name
1facf9fc 7830+
7831+/* ---------------------------------------------------------------------- */
7832+
dece6358 7833+struct dentry;
1facf9fc 7834+#ifdef CONFIG_AUFS_DEBUG
c1595e42 7835+extern struct mutex au_dbg_mtx;
1facf9fc 7836+extern char *au_plevel;
7837+struct au_nhash;
7838+void au_dpri_whlist(struct au_nhash *whlist);
7839+struct au_vdir;
7840+void au_dpri_vdir(struct au_vdir *vdir);
dece6358 7841+struct inode;
1facf9fc 7842+void au_dpri_inode(struct inode *inode);
2cbb1c4b 7843+void au_dpri_dalias(struct inode *inode);
1facf9fc 7844+void au_dpri_dentry(struct dentry *dentry);
dece6358 7845+struct file;
1facf9fc 7846+void au_dpri_file(struct file *filp);
dece6358 7847+struct super_block;
1facf9fc 7848+void au_dpri_sb(struct super_block *sb);
7849+
027c5e7a
AM
7850+#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
7851+void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
1facf9fc 7852+void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
1facf9fc 7853+void au_dbg_verify_kthread(void);
7854+
7855+int __init au_debug_init(void);
7e9cd9fe 7856+
1facf9fc 7857+#define AuDbgWhlist(w) do { \
c1595e42 7858+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7859+ AuDbg(#w "\n"); \
7860+ au_dpri_whlist(w); \
c1595e42 7861+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7862+} while (0)
7863+
7864+#define AuDbgVdir(v) do { \
c1595e42 7865+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7866+ AuDbg(#v "\n"); \
7867+ au_dpri_vdir(v); \
c1595e42 7868+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7869+} while (0)
7870+
7871+#define AuDbgInode(i) do { \
c1595e42 7872+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7873+ AuDbg(#i "\n"); \
7874+ au_dpri_inode(i); \
c1595e42 7875+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7876+} while (0)
7877+
2cbb1c4b 7878+#define AuDbgDAlias(i) do { \
c1595e42 7879+ mutex_lock(&au_dbg_mtx); \
2cbb1c4b
JR
7880+ AuDbg(#i "\n"); \
7881+ au_dpri_dalias(i); \
c1595e42 7882+ mutex_unlock(&au_dbg_mtx); \
2cbb1c4b
JR
7883+} while (0)
7884+
1facf9fc 7885+#define AuDbgDentry(d) do { \
c1595e42 7886+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7887+ AuDbg(#d "\n"); \
7888+ au_dpri_dentry(d); \
c1595e42 7889+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7890+} while (0)
7891+
7892+#define AuDbgFile(f) do { \
c1595e42 7893+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7894+ AuDbg(#f "\n"); \
7895+ au_dpri_file(f); \
c1595e42 7896+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7897+} while (0)
7898+
7899+#define AuDbgSb(sb) do { \
c1595e42 7900+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7901+ AuDbg(#sb "\n"); \
7902+ au_dpri_sb(sb); \
c1595e42 7903+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7904+} while (0)
7905+
4a4d8108
AM
7906+#define AuDbgSym(addr) do { \
7907+ char sym[KSYM_SYMBOL_LEN]; \
7908+ sprint_symbol(sym, (unsigned long)addr); \
7909+ AuDbg("%s\n", sym); \
7910+} while (0)
1facf9fc 7911+#else
027c5e7a 7912+AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
4a4d8108
AM
7913+AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
7914+AuStubVoid(au_dbg_verify_kthread, void)
7915+AuStubInt0(__init au_debug_init, void)
1facf9fc 7916+
1facf9fc 7917+#define AuDbgWhlist(w) do {} while (0)
7918+#define AuDbgVdir(v) do {} while (0)
7919+#define AuDbgInode(i) do {} while (0)
2cbb1c4b 7920+#define AuDbgDAlias(i) do {} while (0)
1facf9fc 7921+#define AuDbgDentry(d) do {} while (0)
7922+#define AuDbgFile(f) do {} while (0)
7923+#define AuDbgSb(sb) do {} while (0)
4a4d8108 7924+#define AuDbgSym(addr) do {} while (0)
1facf9fc 7925+#endif /* CONFIG_AUFS_DEBUG */
7926+
7927+/* ---------------------------------------------------------------------- */
7928+
7929+#ifdef CONFIG_AUFS_MAGIC_SYSRQ
7930+int __init au_sysrq_init(void);
7931+void au_sysrq_fin(void);
7932+
7933+#ifdef CONFIG_HW_CONSOLE
7934+#define au_dbg_blocked() do { \
7935+ WARN_ON(1); \
0c5527e5 7936+ handle_sysrq('w'); \
1facf9fc 7937+} while (0)
7938+#else
4a4d8108 7939+AuStubVoid(au_dbg_blocked, void)
1facf9fc 7940+#endif
7941+
7942+#else
4a4d8108
AM
7943+AuStubInt0(__init au_sysrq_init, void)
7944+AuStubVoid(au_sysrq_fin, void)
7945+AuStubVoid(au_dbg_blocked, void)
1facf9fc 7946+#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
7947+
7948+#endif /* __KERNEL__ */
7949+#endif /* __AUFS_DEBUG_H__ */
7f207e10 7950diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
eca34b5c 7951--- /usr/share/empty/fs/aufs/dentry.c 1970-01-01 01:00:00.000000000 +0100
631230db 7952+++ linux/fs/aufs/dentry.c 2023-10-31 09:31:04.196547417 +0100
f4d37d76 7953@@ -0,0 +1,1168 @@
cd7a4cd9 7954+// SPDX-License-Identifier: GPL-2.0
1facf9fc 7955+/*
0dcfbb52 7956+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 7957+ *
0dcfbb52 7958+ * This program is free software; you can redistribute it and/or modify
1facf9fc 7959+ * it under the terms of the GNU General Public License as published by
7960+ * the Free Software Foundation; either version 2 of the License, or
7961+ * (at your option) any later version.
dece6358
AM
7962+ *
7963+ * This program is distributed in the hope that it will be useful,
7964+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7965+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7966+ * GNU General Public License for more details.
7967+ *
7968+ * You should have received a copy of the GNU General Public License
523b37e3 7969+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7970+ */
7971+
7972+/*
7973+ * lookup and dentry operations
7974+ */
7975+
eca801bf 7976+#include <linux/iversion.h>
1facf9fc 7977+#include "aufs.h"
7978+
1facf9fc 7979+/*
7980+ * returns positive/negative dentry, NULL or an error.
7981+ * NULL means whiteout-ed or not-found.
7982+ */
7983+static struct dentry*
7984+au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
8b6a4947 7985+ aufs_bindex_t bindex, struct au_do_lookup_args *args)
1facf9fc 7986+{
7987+ struct dentry *h_dentry;
2000de60 7988+ struct inode *h_inode;
1facf9fc 7989+ struct au_branch *br;
5db017eb 7990+ struct mnt_idmap *h_idmap;
758e9dad 7991+ struct path h_path;
1facf9fc 7992+ int wh_found, opq;
7993+ unsigned char wh_able;
7994+ const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
076b876e
AM
7995+ const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
7996+ IGNORE_PERM);
1facf9fc 7997+
1facf9fc 7998+ wh_found = 0;
7999+ br = au_sbr(dentry->d_sb, bindex);
758e9dad
AM
8000+ h_path.dentry = h_parent;
8001+ h_path.mnt = au_br_mnt(br);
5db017eb 8002+ h_idmap = au_br_idmap(br);
1facf9fc 8003+ wh_able = !!au_br_whable(br->br_perm);
8004+ if (wh_able)
5db017eb 8005+ wh_found = au_wh_test(h_idmap, &h_path, &args->whname,
0b2a12c6 8006+ ignore_perm);
1facf9fc 8007+ h_dentry = ERR_PTR(wh_found);
8008+ if (!wh_found)
8009+ goto real_lookup;
8010+ if (unlikely(wh_found < 0))
8011+ goto out;
8012+
8013+ /* We found a whiteout */
5afbbe0d 8014+ /* au_set_dbbot(dentry, bindex); */
1facf9fc 8015+ au_set_dbwh(dentry, bindex);
8016+ if (!allow_neg)
8017+ return NULL; /* success */
8018+
4f0767ce 8019+real_lookup:
076b876e 8020+ if (!ignore_perm)
758e9dad 8021+ h_dentry = vfsub_lkup_one(args->name, &h_path);
076b876e 8022+ else
5db017eb 8023+ h_dentry = au_sio_lkup_one(h_idmap, args->name, &h_path);
2000de60
JR
8024+ if (IS_ERR(h_dentry)) {
8025+ if (PTR_ERR(h_dentry) == -ENAMETOOLONG
8026+ && !allow_neg)
8027+ h_dentry = NULL;
1facf9fc 8028+ goto out;
2000de60 8029+ }
1facf9fc 8030+
5527c038
JR
8031+ h_inode = d_inode(h_dentry);
8032+ if (d_is_negative(h_dentry)) {
1facf9fc 8033+ if (!allow_neg)
8034+ goto out_neg;
8035+ } else if (wh_found
8036+ || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
8037+ goto out_neg;
8b6a4947
AM
8038+ else if (au_ftest_lkup(args->flags, DIRREN)
8039+ /* && h_inode */
8040+ && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
8041+ AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
8042+ (unsigned long long)h_inode->i_ino);
8043+ goto out_neg;
8044+ }
1facf9fc 8045+
5afbbe0d
AM
8046+ if (au_dbbot(dentry) <= bindex)
8047+ au_set_dbbot(dentry, bindex);
8048+ if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
8049+ au_set_dbtop(dentry, bindex);
1facf9fc 8050+ au_set_h_dptr(dentry, bindex, h_dentry);
8051+
2000de60
JR
8052+ if (!d_is_dir(h_dentry)
8053+ || !wh_able
5527c038 8054+ || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
1facf9fc 8055+ goto out; /* success */
8056+
758e9dad 8057+ h_path.dentry = h_dentry;
be118d29 8058+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
5db017eb 8059+ opq = au_diropq_test(h_idmap, &h_path);
3c1bdaff 8060+ inode_unlock_shared(h_inode);
1facf9fc 8061+ if (opq > 0)
8062+ au_set_dbdiropq(dentry, bindex);
8063+ else if (unlikely(opq < 0)) {
8064+ au_set_h_dptr(dentry, bindex, NULL);
8065+ h_dentry = ERR_PTR(opq);
8066+ }
8067+ goto out;
8068+
4f0767ce 8069+out_neg:
1facf9fc 8070+ dput(h_dentry);
8071+ h_dentry = NULL;
4f0767ce 8072+out:
1facf9fc 8073+ return h_dentry;
8074+}
8075+
dece6358
AM
8076+static int au_test_shwh(struct super_block *sb, const struct qstr *name)
8077+{
8078+ if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
8079+ && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
8080+ return -EPERM;
8081+ return 0;
8082+}
8083+
1facf9fc 8084+/*
8085+ * returns the number of lower positive dentries,
8086+ * otherwise an error.
8087+ * can be called at unlinking with @type is zero.
8088+ */
5afbbe0d
AM
8089+int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
8090+ unsigned int flags)
1facf9fc 8091+{
8092+ int npositive, err;
8093+ aufs_bindex_t bindex, btail, bdiropq;
8b6a4947 8094+ unsigned char isdir, dirperm1, dirren;
1facf9fc 8095+ struct au_do_lookup_args args = {
8b6a4947
AM
8096+ .flags = flags,
8097+ .name = &dentry->d_name
1facf9fc 8098+ };
1facf9fc 8099+ struct dentry *parent;
076b876e 8100+ struct super_block *sb;
1facf9fc 8101+
076b876e 8102+ sb = dentry->d_sb;
8b6a4947 8103+ err = au_test_shwh(sb, args.name);
dece6358 8104+ if (unlikely(err))
1facf9fc 8105+ goto out;
8106+
8b6a4947 8107+ err = au_wh_name_alloc(&args.whname, args.name);
1facf9fc 8108+ if (unlikely(err))
8109+ goto out;
8110+
2000de60 8111+ isdir = !!d_is_dir(dentry);
076b876e 8112+ dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
8b6a4947
AM
8113+ dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
8114+ if (dirren)
8115+ au_fset_lkup(args.flags, DIRREN);
1facf9fc 8116+
8117+ npositive = 0;
4a4d8108 8118+ parent = dget_parent(dentry);
1facf9fc 8119+ btail = au_dbtaildir(parent);
5afbbe0d 8120+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 8121+ struct dentry *h_parent, *h_dentry;
8122+ struct inode *h_inode, *h_dir;
8b6a4947 8123+ struct au_branch *br;
1facf9fc 8124+
8125+ h_dentry = au_h_dptr(dentry, bindex);
8126+ if (h_dentry) {
5527c038 8127+ if (d_is_positive(h_dentry))
1facf9fc 8128+ npositive++;
5afbbe0d 8129+ break;
1facf9fc 8130+ }
8131+ h_parent = au_h_dptr(parent, bindex);
2000de60 8132+ if (!h_parent || !d_is_dir(h_parent))
1facf9fc 8133+ continue;
8134+
8b6a4947
AM
8135+ if (dirren) {
8136+ /* if the inum matches, then use the prepared name */
8137+ err = au_dr_lkup_name(&args, bindex);
8138+ if (unlikely(err))
8139+ goto out_parent;
8140+ }
8141+
5527c038 8142+ h_dir = d_inode(h_parent);
be118d29 8143+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8b6a4947 8144+ h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
3c1bdaff 8145+ inode_unlock_shared(h_dir);
1facf9fc 8146+ err = PTR_ERR(h_dentry);
8147+ if (IS_ERR(h_dentry))
4a4d8108 8148+ goto out_parent;
2000de60
JR
8149+ if (h_dentry)
8150+ au_fclr_lkup(args.flags, ALLOW_NEG);
076b876e
AM
8151+ if (dirperm1)
8152+ au_fset_lkup(args.flags, IGNORE_PERM);
1facf9fc 8153+
79b8bda9 8154+ if (au_dbwh(dentry) == bindex)
1facf9fc 8155+ break;
8156+ if (!h_dentry)
8157+ continue;
5527c038 8158+ if (d_is_negative(h_dentry))
1facf9fc 8159+ continue;
5527c038 8160+ h_inode = d_inode(h_dentry);
1facf9fc 8161+ npositive++;
8162+ if (!args.type)
8163+ args.type = h_inode->i_mode & S_IFMT;
8164+ if (args.type != S_IFDIR)
8165+ break;
8166+ else if (isdir) {
8167+ /* the type of lower may be different */
8168+ bdiropq = au_dbdiropq(dentry);
8169+ if (bdiropq >= 0 && bdiropq <= bindex)
8170+ break;
8171+ }
8b6a4947
AM
8172+ br = au_sbr(sb, bindex);
8173+ if (dirren
8174+ && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
8175+ /*add_ent*/NULL)) {
8176+ /* prepare next name to lookup */
8177+ err = au_dr_lkup(&args, dentry, bindex);
8178+ if (unlikely(err))
8179+ goto out_parent;
8180+ }
1facf9fc 8181+ }
8182+
8183+ if (npositive) {
8184+ AuLabel(positive);
5afbbe0d 8185+ au_update_dbtop(dentry);
1facf9fc 8186+ }
8187+ err = npositive;
076b876e 8188+ if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
5afbbe0d 8189+ && au_dbtop(dentry) < 0)) {
1facf9fc 8190+ err = -EIO;
523b37e3
AM
8191+ AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
8192+ dentry, err);
027c5e7a 8193+ }
1facf9fc 8194+
4f0767ce 8195+out_parent:
4a4d8108 8196+ dput(parent);
9f237c51 8197+ au_kfree_try_rcu(args.whname.name);
8b6a4947
AM
8198+ if (dirren)
8199+ au_dr_lkup_fin(&args);
4f0767ce 8200+out:
1facf9fc 8201+ return err;
8202+}
8203+
5db017eb 8204+struct dentry *au_sio_lkup_one(struct mnt_idmap *idmap, struct qstr *name,
758e9dad 8205+ struct path *ppath)
1facf9fc 8206+{
8207+ struct dentry *dentry;
8208+ int wkq_err;
8209+
5db017eb 8210+ if (!au_test_h_perm_sio(idmap, d_inode(ppath->dentry), MAY_EXEC))
758e9dad 8211+ dentry = vfsub_lkup_one(name, ppath);
1facf9fc 8212+ else {
b4510431
AM
8213+ struct vfsub_lkup_one_args args = {
8214+ .errp = &dentry,
8215+ .name = name,
758e9dad 8216+ .ppath = ppath
1facf9fc 8217+ };
8218+
b4510431 8219+ wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
1facf9fc 8220+ if (unlikely(wkq_err))
8221+ dentry = ERR_PTR(wkq_err);
8222+ }
8223+
8224+ return dentry;
8225+}
8226+
8227+/*
8228+ * lookup @dentry on @bindex which should be negative.
8229+ */
86dc4139 8230+int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
1facf9fc 8231+{
8232+ int err;
758e9dad 8233+ struct dentry *parent, *h_dentry;
86dc4139 8234+ struct au_branch *br;
5db017eb 8235+ struct mnt_idmap *h_idmap;
758e9dad 8236+ struct path h_ppath;
1facf9fc 8237+
1facf9fc 8238+ parent = dget_parent(dentry);
86dc4139 8239+ br = au_sbr(dentry->d_sb, bindex);
758e9dad
AM
8240+ h_ppath.dentry = au_h_dptr(parent, bindex);
8241+ h_ppath.mnt = au_br_mnt(br);
5db017eb 8242+ h_idmap = au_br_idmap(br);
86dc4139 8243+ if (wh)
758e9dad 8244+ h_dentry = au_whtmp_lkup(h_ppath.dentry, br, &dentry->d_name);
86dc4139 8245+ else
5db017eb 8246+ h_dentry = au_sio_lkup_one(h_idmap, &dentry->d_name, &h_ppath);
1facf9fc 8247+ err = PTR_ERR(h_dentry);
8248+ if (IS_ERR(h_dentry))
8249+ goto out;
5527c038 8250+ if (unlikely(d_is_positive(h_dentry))) {
1facf9fc 8251+ err = -EIO;
523b37e3 8252+ AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
1facf9fc 8253+ dput(h_dentry);
8254+ goto out;
8255+ }
8256+
4a4d8108 8257+ err = 0;
5afbbe0d
AM
8258+ if (bindex < au_dbtop(dentry))
8259+ au_set_dbtop(dentry, bindex);
8260+ if (au_dbbot(dentry) < bindex)
8261+ au_set_dbbot(dentry, bindex);
1facf9fc 8262+ au_set_h_dptr(dentry, bindex, h_dentry);
1facf9fc 8263+
4f0767ce 8264+out:
1facf9fc 8265+ dput(parent);
8266+ return err;
8267+}
8268+
8269+/* ---------------------------------------------------------------------- */
8270+
8271+/* subset of struct inode */
8272+struct au_iattr {
8273+ unsigned long i_ino;
8274+ /* unsigned int i_nlink; */
0c3ec466
AM
8275+ kuid_t i_uid;
8276+ kgid_t i_gid;
1facf9fc 8277+ u64 i_version;
8278+/*
8279+ loff_t i_size;
8280+ blkcnt_t i_blocks;
8281+*/
8282+ umode_t i_mode;
8283+};
8284+
8285+static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
8286+{
8287+ ia->i_ino = h_inode->i_ino;
8288+ /* ia->i_nlink = h_inode->i_nlink; */
8289+ ia->i_uid = h_inode->i_uid;
8290+ ia->i_gid = h_inode->i_gid;
be118d29 8291+ ia->i_version = inode_query_iversion(h_inode);
1facf9fc 8292+/*
8293+ ia->i_size = h_inode->i_size;
8294+ ia->i_blocks = h_inode->i_blocks;
8295+*/
8296+ ia->i_mode = (h_inode->i_mode & S_IFMT);
8297+}
8298+
8299+static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
8300+{
8301+ return ia->i_ino != h_inode->i_ino
8302+ /* || ia->i_nlink != h_inode->i_nlink */
0c3ec466 8303+ || !uid_eq(ia->i_uid, h_inode->i_uid)
2dfbb274 8304+ || !gid_eq(ia->i_gid, h_inode->i_gid)
be118d29 8305+ || !inode_eq_iversion(h_inode, ia->i_version)
1facf9fc 8306+/*
8307+ || ia->i_size != h_inode->i_size
8308+ || ia->i_blocks != h_inode->i_blocks
8309+*/
8310+ || ia->i_mode != (h_inode->i_mode & S_IFMT);
8311+}
8312+
8313+static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
8314+ struct au_branch *br)
8315+{
8316+ int err;
8317+ struct au_iattr ia;
8318+ struct inode *h_inode;
8319+ struct dentry *h_d;
8320+ struct super_block *h_sb;
758e9dad 8321+ struct path h_ppath;
1facf9fc 8322+
8323+ err = 0;
8324+ memset(&ia, -1, sizeof(ia));
8325+ h_sb = h_dentry->d_sb;
5527c038
JR
8326+ h_inode = NULL;
8327+ if (d_is_positive(h_dentry)) {
8328+ h_inode = d_inode(h_dentry);
1facf9fc 8329+ au_iattr_save(&ia, h_inode);
5527c038 8330+ } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
1facf9fc 8331+ /* nfs d_revalidate may return 0 for negative dentry */
8332+ /* fuse d_revalidate always return 0 for negative dentry */
8333+ goto out;
8334+
8335+ /* main purpose is namei.c:cached_lookup() and d_revalidate */
758e9dad
AM
8336+ h_ppath.dentry = h_parent;
8337+ h_ppath.mnt = au_br_mnt(br);
8338+ h_d = vfsub_lkup_one(&h_dentry->d_name, &h_ppath);
1facf9fc 8339+ err = PTR_ERR(h_d);
8340+ if (IS_ERR(h_d))
8341+ goto out;
8342+
8343+ err = 0;
8344+ if (unlikely(h_d != h_dentry
5527c038 8345+ || d_inode(h_d) != h_inode
1facf9fc 8346+ || (h_inode && au_iattr_test(&ia, h_inode))))
8347+ err = au_busy_or_stale();
8348+ dput(h_d);
8349+
4f0767ce 8350+out:
1facf9fc 8351+ AuTraceErr(err);
8352+ return err;
8353+}
8354+
8355+int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8356+ struct dentry *h_parent, struct au_branch *br)
8357+{
8358+ int err;
8359+
8360+ err = 0;
027c5e7a
AM
8361+ if (udba == AuOpt_UDBA_REVAL
8362+ && !au_test_fs_remote(h_dentry->d_sb)) {
1facf9fc 8363+ IMustLock(h_dir);
5527c038 8364+ err = (d_inode(h_dentry->d_parent) != h_dir);
027c5e7a 8365+ } else if (udba != AuOpt_UDBA_NONE)
1facf9fc 8366+ err = au_h_verify_dentry(h_dentry, h_parent, br);
8367+
8368+ return err;
8369+}
8370+
8371+/* ---------------------------------------------------------------------- */
8372+
027c5e7a 8373+static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
1facf9fc 8374+{
027c5e7a 8375+ int err;
5afbbe0d 8376+ aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
027c5e7a
AM
8377+ struct au_hdentry tmp, *p, *q;
8378+ struct au_dinfo *dinfo;
8379+ struct super_block *sb;
1facf9fc 8380+
027c5e7a 8381+ DiMustWriteLock(dentry);
1308ab2a 8382+
027c5e7a
AM
8383+ sb = dentry->d_sb;
8384+ dinfo = au_di(dentry);
5afbbe0d 8385+ bbot = dinfo->di_bbot;
1facf9fc 8386+ bwh = dinfo->di_bwh;
8387+ bdiropq = dinfo->di_bdiropq;
5afbbe0d
AM
8388+ bindex = dinfo->di_btop;
8389+ p = au_hdentry(dinfo, bindex);
8390+ for (; bindex <= bbot; bindex++, p++) {
027c5e7a 8391+ if (!p->hd_dentry)
1facf9fc 8392+ continue;
8393+
027c5e7a
AM
8394+ new_bindex = au_br_index(sb, p->hd_id);
8395+ if (new_bindex == bindex)
1facf9fc 8396+ continue;
1facf9fc 8397+
1facf9fc 8398+ if (dinfo->di_bwh == bindex)
8399+ bwh = new_bindex;
8400+ if (dinfo->di_bdiropq == bindex)
8401+ bdiropq = new_bindex;
8402+ if (new_bindex < 0) {
8403+ au_hdput(p);
8404+ p->hd_dentry = NULL;
8405+ continue;
8406+ }
8407+
8408+ /* swap two lower dentries, and loop again */
5afbbe0d 8409+ q = au_hdentry(dinfo, new_bindex);
1facf9fc 8410+ tmp = *q;
8411+ *q = *p;
8412+ *p = tmp;
8413+ if (tmp.hd_dentry) {
8414+ bindex--;
8415+ p--;
8416+ }
8417+ }
8418+
1facf9fc 8419+ dinfo->di_bwh = -1;
5afbbe0d 8420+ if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
1facf9fc 8421+ dinfo->di_bwh = bwh;
8422+
8423+ dinfo->di_bdiropq = -1;
8424+ if (bdiropq >= 0
5afbbe0d 8425+ && bdiropq <= au_sbbot(sb)
1facf9fc 8426+ && au_sbr_whable(sb, bdiropq))
8427+ dinfo->di_bdiropq = bdiropq;
8428+
027c5e7a 8429+ err = -EIO;
5afbbe0d
AM
8430+ dinfo->di_btop = -1;
8431+ dinfo->di_bbot = -1;
8432+ bbot = au_dbbot(parent);
8433+ bindex = 0;
8434+ p = au_hdentry(dinfo, bindex);
8435+ for (; bindex <= bbot; bindex++, p++)
1facf9fc 8436+ if (p->hd_dentry) {
5afbbe0d 8437+ dinfo->di_btop = bindex;
1facf9fc 8438+ break;
8439+ }
8440+
5afbbe0d
AM
8441+ if (dinfo->di_btop >= 0) {
8442+ bindex = bbot;
8443+ p = au_hdentry(dinfo, bindex);
8444+ for (; bindex >= 0; bindex--, p--)
027c5e7a 8445+ if (p->hd_dentry) {
5afbbe0d 8446+ dinfo->di_bbot = bindex;
027c5e7a
AM
8447+ err = 0;
8448+ break;
8449+ }
8450+ }
8451+
8452+ return err;
1facf9fc 8453+}
8454+
027c5e7a 8455+static void au_do_hide(struct dentry *dentry)
1facf9fc 8456+{
027c5e7a 8457+ struct inode *inode;
1facf9fc 8458+
5527c038
JR
8459+ if (d_really_is_positive(dentry)) {
8460+ inode = d_inode(dentry);
8461+ if (!d_is_dir(dentry)) {
027c5e7a
AM
8462+ if (inode->i_nlink && !d_unhashed(dentry))
8463+ drop_nlink(inode);
8464+ } else {
8465+ clear_nlink(inode);
8466+ /* stop next lookup */
8467+ inode->i_flags |= S_DEAD;
8468+ }
8469+ smp_mb(); /* necessary? */
8470+ }
8471+ d_drop(dentry);
8472+}
1308ab2a 8473+
027c5e7a
AM
8474+static int au_hide_children(struct dentry *parent)
8475+{
8476+ int err, i, j, ndentry;
8477+ struct au_dcsub_pages dpages;
8478+ struct au_dpage *dpage;
8479+ struct dentry *dentry;
1facf9fc 8480+
027c5e7a 8481+ err = au_dpages_init(&dpages, GFP_NOFS);
1facf9fc 8482+ if (unlikely(err))
8483+ goto out;
027c5e7a
AM
8484+ err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8485+ if (unlikely(err))
8486+ goto out_dpages;
1facf9fc 8487+
027c5e7a
AM
8488+ /* in reverse order */
8489+ for (i = dpages.ndpage - 1; i >= 0; i--) {
8490+ dpage = dpages.dpages + i;
8491+ ndentry = dpage->ndentry;
8492+ for (j = ndentry - 1; j >= 0; j--) {
8493+ dentry = dpage->dentries[j];
8494+ if (dentry != parent)
8495+ au_do_hide(dentry);
8496+ }
8497+ }
1facf9fc 8498+
027c5e7a
AM
8499+out_dpages:
8500+ au_dpages_free(&dpages);
4f0767ce 8501+out:
027c5e7a 8502+ return err;
1facf9fc 8503+}
8504+
027c5e7a 8505+static void au_hide(struct dentry *dentry)
1facf9fc 8506+{
027c5e7a 8507+ int err;
1facf9fc 8508+
027c5e7a 8509+ AuDbgDentry(dentry);
2000de60 8510+ if (d_is_dir(dentry)) {
027c5e7a
AM
8511+ /* shrink_dcache_parent(dentry); */
8512+ err = au_hide_children(dentry);
8513+ if (unlikely(err))
523b37e3
AM
8514+ AuIOErr("%pd, failed hiding children, ignored %d\n",
8515+ dentry, err);
027c5e7a
AM
8516+ }
8517+ au_do_hide(dentry);
8518+}
1facf9fc 8519+
027c5e7a
AM
8520+/*
8521+ * By adding a dirty branch, a cached dentry may be affected in various ways.
8522+ *
8523+ * a dirty branch is added
8524+ * - on the top of layers
8525+ * - in the middle of layers
8526+ * - to the bottom of layers
8527+ *
8528+ * on the added branch there exists
8529+ * - a whiteout
8530+ * - a diropq
8531+ * - a same named entry
8532+ * + exist
8533+ * * negative --> positive
8534+ * * positive --> positive
8535+ * - type is unchanged
8536+ * - type is changed
8537+ * + doesn't exist
8538+ * * negative --> negative
8539+ * * positive --> negative (rejected by au_br_del() for non-dir case)
8540+ * - none
8541+ */
8542+static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8543+ struct au_dinfo *tmp)
8544+{
8545+ int err;
5afbbe0d 8546+ aufs_bindex_t bindex, bbot;
027c5e7a
AM
8547+ struct {
8548+ struct dentry *dentry;
8549+ struct inode *inode;
8550+ mode_t mode;
be52b249
AM
8551+ } orig_h, tmp_h = {
8552+ .dentry = NULL
8553+ };
027c5e7a
AM
8554+ struct au_hdentry *hd;
8555+ struct inode *inode, *h_inode;
8556+ struct dentry *h_dentry;
8557+
8558+ err = 0;
5afbbe0d 8559+ AuDebugOn(dinfo->di_btop < 0);
027c5e7a 8560+ orig_h.mode = 0;
5afbbe0d 8561+ orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
5527c038
JR
8562+ orig_h.inode = NULL;
8563+ if (d_is_positive(orig_h.dentry)) {
8564+ orig_h.inode = d_inode(orig_h.dentry);
027c5e7a 8565+ orig_h.mode = orig_h.inode->i_mode & S_IFMT;
5527c038 8566+ }
5afbbe0d
AM
8567+ if (tmp->di_btop >= 0) {
8568+ tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
5527c038
JR
8569+ if (d_is_positive(tmp_h.dentry)) {
8570+ tmp_h.inode = d_inode(tmp_h.dentry);
027c5e7a 8571+ tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
5527c038 8572+ }
027c5e7a
AM
8573+ }
8574+
5527c038
JR
8575+ inode = NULL;
8576+ if (d_really_is_positive(dentry))
8577+ inode = d_inode(dentry);
027c5e7a 8578+ if (!orig_h.inode) {
acd2b654 8579+ AuDbg("negative originally\n");
027c5e7a
AM
8580+ if (inode) {
8581+ au_hide(dentry);
8582+ goto out;
8583+ }
8584+ AuDebugOn(inode);
5afbbe0d 8585+ AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
027c5e7a
AM
8586+ AuDebugOn(dinfo->di_bdiropq != -1);
8587+
8588+ if (!tmp_h.inode) {
8589+ AuDbg("negative --> negative\n");
8590+ /* should have only one negative lower */
5afbbe0d
AM
8591+ if (tmp->di_btop >= 0
8592+ && tmp->di_btop < dinfo->di_btop) {
8593+ AuDebugOn(tmp->di_btop != tmp->di_bbot);
8594+ AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8595+ au_set_h_dptr(dentry, dinfo->di_btop, NULL);
027c5e7a 8596+ au_di_cp(dinfo, tmp);
5afbbe0d
AM
8597+ hd = au_hdentry(tmp, tmp->di_btop);
8598+ au_set_h_dptr(dentry, tmp->di_btop,
027c5e7a
AM
8599+ dget(hd->hd_dentry));
8600+ }
8601+ au_dbg_verify_dinode(dentry);
8602+ } else {
8603+ AuDbg("negative --> positive\n");
8604+ /*
8605+ * similar to the behaviour of creating with bypassing
8606+ * aufs.
8607+ * unhash it in order to force an error in the
8608+ * succeeding create operation.
8609+ * we should not set S_DEAD here.
8610+ */
8611+ d_drop(dentry);
8612+ /* au_di_swap(tmp, dinfo); */
8613+ au_dbg_verify_dinode(dentry);
8614+ }
8615+ } else {
8616+ AuDbg("positive originally\n");
8617+ /* inode may be NULL */
8618+ AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8619+ if (!tmp_h.inode) {
8620+ AuDbg("positive --> negative\n");
8621+ /* or bypassing aufs */
8622+ au_hide(dentry);
5afbbe0d 8623+ if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
027c5e7a
AM
8624+ dinfo->di_bwh = tmp->di_bwh;
8625+ if (inode)
8626+ err = au_refresh_hinode_self(inode);
8627+ au_dbg_verify_dinode(dentry);
8628+ } else if (orig_h.mode == tmp_h.mode) {
8629+ AuDbg("positive --> positive, same type\n");
8630+ if (!S_ISDIR(orig_h.mode)
5afbbe0d 8631+ && dinfo->di_btop > tmp->di_btop) {
027c5e7a
AM
8632+ /*
8633+ * similar to the behaviour of removing and
8634+ * creating.
8635+ */
8636+ au_hide(dentry);
8637+ if (inode)
8638+ err = au_refresh_hinode_self(inode);
8639+ au_dbg_verify_dinode(dentry);
8640+ } else {
8641+ /* fill empty slots */
5afbbe0d
AM
8642+ if (dinfo->di_btop > tmp->di_btop)
8643+ dinfo->di_btop = tmp->di_btop;
8644+ if (dinfo->di_bbot < tmp->di_bbot)
8645+ dinfo->di_bbot = tmp->di_bbot;
027c5e7a
AM
8646+ dinfo->di_bwh = tmp->di_bwh;
8647+ dinfo->di_bdiropq = tmp->di_bdiropq;
5afbbe0d
AM
8648+ bbot = dinfo->di_bbot;
8649+ bindex = tmp->di_btop;
8650+ hd = au_hdentry(tmp, bindex);
8651+ for (; bindex <= bbot; bindex++, hd++) {
027c5e7a
AM
8652+ if (au_h_dptr(dentry, bindex))
8653+ continue;
5afbbe0d 8654+ h_dentry = hd->hd_dentry;
027c5e7a
AM
8655+ if (!h_dentry)
8656+ continue;
5527c038
JR
8657+ AuDebugOn(d_is_negative(h_dentry));
8658+ h_inode = d_inode(h_dentry);
027c5e7a
AM
8659+ AuDebugOn(orig_h.mode
8660+ != (h_inode->i_mode
8661+ & S_IFMT));
8662+ au_set_h_dptr(dentry, bindex,
8663+ dget(h_dentry));
8664+ }
5afbbe0d
AM
8665+ if (inode)
8666+ err = au_refresh_hinode(inode, dentry);
027c5e7a
AM
8667+ au_dbg_verify_dinode(dentry);
8668+ }
8669+ } else {
8670+ AuDbg("positive --> positive, different type\n");
8671+ /* similar to the behaviour of removing and creating */
8672+ au_hide(dentry);
8673+ if (inode)
8674+ err = au_refresh_hinode_self(inode);
8675+ au_dbg_verify_dinode(dentry);
8676+ }
8677+ }
8678+
8679+out:
8680+ return err;
8681+}
8682+
79b8bda9
AM
8683+void au_refresh_dop(struct dentry *dentry, int force_reval)
8684+{
8685+ const struct dentry_operations *dop
8686+ = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8687+ static const unsigned int mask
8688+ = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8689+
8690+ BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8691+
8692+ if (dentry->d_op == dop)
8693+ return;
8694+
8695+ AuDbg("%pd\n", dentry);
8696+ spin_lock(&dentry->d_lock);
8697+ if (dop == &aufs_dop)
8698+ dentry->d_flags |= mask;
8699+ else
8700+ dentry->d_flags &= ~mask;
8701+ dentry->d_op = dop;
8702+ spin_unlock(&dentry->d_lock);
8703+}
8704+
027c5e7a
AM
8705+int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8706+{
e2f27e51 8707+ int err, ebrange, nbr;
027c5e7a
AM
8708+ unsigned int sigen;
8709+ struct au_dinfo *dinfo, *tmp;
8710+ struct super_block *sb;
8711+ struct inode *inode;
8712+
8713+ DiMustWriteLock(dentry);
8714+ AuDebugOn(IS_ROOT(dentry));
5527c038 8715+ AuDebugOn(d_really_is_negative(parent));
027c5e7a
AM
8716+
8717+ sb = dentry->d_sb;
027c5e7a
AM
8718+ sigen = au_sigen(sb);
8719+ err = au_digen_test(parent, sigen);
8720+ if (unlikely(err))
8721+ goto out;
8722+
e2f27e51 8723+ nbr = au_sbbot(sb) + 1;
027c5e7a 8724+ dinfo = au_di(dentry);
e2f27e51 8725+ err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
027c5e7a
AM
8726+ if (unlikely(err))
8727+ goto out;
8728+ ebrange = au_dbrange_test(dentry);
8729+ if (!ebrange)
8730+ ebrange = au_do_refresh_hdentry(dentry, parent);
8731+
38d290e6 8732+ if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
5afbbe0d 8733+ AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
5527c038
JR
8734+ if (d_really_is_positive(dentry)) {
8735+ inode = d_inode(dentry);
027c5e7a 8736+ err = au_refresh_hinode_self(inode);
5527c038 8737+ }
027c5e7a
AM
8738+ au_dbg_verify_dinode(dentry);
8739+ if (!err)
8740+ goto out_dgen; /* success */
8741+ goto out;
8742+ }
8743+
8744+ /* temporary dinfo */
8745+ AuDbgDentry(dentry);
8746+ err = -ENOMEM;
8747+ tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8748+ if (unlikely(!tmp))
8749+ goto out;
8750+ au_di_swap(tmp, dinfo);
8751+ /* returns the number of positive dentries */
8752+ /*
8753+ * if current working dir is removed, it returns an error.
8754+ * but the dentry is legal.
8755+ */
5afbbe0d 8756+ err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
027c5e7a
AM
8757+ AuDbgDentry(dentry);
8758+ au_di_swap(tmp, dinfo);
8759+ if (err == -ENOENT)
8760+ err = 0;
8761+ if (err >= 0) {
8762+ /* compare/refresh by dinfo */
8763+ AuDbgDentry(dentry);
8764+ err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8765+ au_dbg_verify_dinode(dentry);
8766+ AuTraceErr(err);
8767+ }
e2f27e51 8768+ au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
027c5e7a
AM
8769+ au_rw_write_unlock(&tmp->di_rwsem);
8770+ au_di_free(tmp);
8771+ if (unlikely(err))
8772+ goto out;
8773+
8774+out_dgen:
8775+ au_update_digen(dentry);
8776+out:
8777+ if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
523b37e3 8778+ AuIOErr("failed refreshing %pd, %d\n", dentry, err);
027c5e7a
AM
8779+ AuDbgDentry(dentry);
8780+ }
8781+ AuTraceErr(err);
8782+ return err;
8783+}
8784+
b4510431
AM
8785+static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8786+ struct dentry *dentry, aufs_bindex_t bindex)
027c5e7a
AM
8787+{
8788+ int err, valid;
027c5e7a
AM
8789+
8790+ err = 0;
8791+ if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8792+ goto out;
027c5e7a
AM
8793+
8794+ AuDbg("b%d\n", bindex);
b4510431
AM
8795+ /*
8796+ * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8797+ * due to whiteout and branch permission.
8798+ */
8799+ flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8800+ | LOOKUP_FOLLOW | LOOKUP_EXCL);
8801+ /* it may return tri-state */
8802+ valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
1facf9fc 8803+
8804+ if (unlikely(valid < 0))
8805+ err = valid;
8806+ else if (!valid)
8807+ err = -EINVAL;
8808+
4f0767ce 8809+out:
1facf9fc 8810+ AuTraceErr(err);
8811+ return err;
8812+}
8813+
8814+/* todo: remove this */
8815+static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8b6a4947 8816+ unsigned int flags, int do_udba, int dirren)
1facf9fc 8817+{
8818+ int err;
8819+ umode_t mode, h_mode;
5afbbe0d 8820+ aufs_bindex_t bindex, btail, btop, ibs, ibe;
38d290e6 8821+ unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
4a4d8108 8822+ struct inode *h_inode, *h_cached_inode;
1facf9fc 8823+ struct dentry *h_dentry;
8824+ struct qstr *name, *h_name;
8825+
8826+ err = 0;
8827+ plus = 0;
8828+ mode = 0;
1facf9fc 8829+ ibs = -1;
8830+ ibe = -1;
8831+ unhashed = !!d_unhashed(dentry);
8832+ is_root = !!IS_ROOT(dentry);
8833+ name = &dentry->d_name;
38d290e6 8834+ tmpfile = au_di(dentry)->di_tmpfile;
1facf9fc 8835+
8836+ /*
7f207e10
AM
8837+ * Theoretically, REVAL test should be unnecessary in case of
8838+ * {FS,I}NOTIFY.
8839+ * But {fs,i}notify doesn't fire some necessary events,
1facf9fc 8840+ * IN_ATTRIB for atime/nlink/pageio
1facf9fc 8841+ * Let's do REVAL test too.
8842+ */
8843+ if (do_udba && inode) {
8844+ mode = (inode->i_mode & S_IFMT);
8845+ plus = (inode->i_nlink > 0);
5afbbe0d
AM
8846+ ibs = au_ibtop(inode);
8847+ ibe = au_ibbot(inode);
1facf9fc 8848+ }
8849+
5afbbe0d
AM
8850+ btop = au_dbtop(dentry);
8851+ btail = btop;
1facf9fc 8852+ if (inode && S_ISDIR(inode->i_mode))
8853+ btail = au_dbtaildir(dentry);
5afbbe0d 8854+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 8855+ h_dentry = au_h_dptr(dentry, bindex);
8856+ if (!h_dentry)
8857+ continue;
8858+
523b37e3
AM
8859+ AuDbg("b%d, %pd\n", bindex, h_dentry);
8860+ h_nfs = !!au_test_nfs(h_dentry->d_sb);
027c5e7a 8861+ spin_lock(&h_dentry->d_lock);
1facf9fc 8862+ h_name = &h_dentry->d_name;
8863+ if (unlikely(do_udba
8864+ && !is_root
523b37e3
AM
8865+ && ((!h_nfs
8866+ && (unhashed != !!d_unhashed(h_dentry)
8b6a4947 8867+ || (!tmpfile && !dirren
38d290e6
JR
8868+ && !au_qstreq(name, h_name))
8869+ ))
523b37e3
AM
8870+ || (h_nfs
8871+ && !(flags & LOOKUP_OPEN)
8872+ && (h_dentry->d_flags
8873+ & DCACHE_NFSFS_RENAMED)))
1facf9fc 8874+ )) {
38d290e6
JR
8875+ int h_unhashed;
8876+
8877+ h_unhashed = d_unhashed(h_dentry);
027c5e7a 8878+ spin_unlock(&h_dentry->d_lock);
38d290e6
JR
8879+ AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
8880+ unhashed, h_unhashed, dentry, h_dentry);
1facf9fc 8881+ goto err;
8882+ }
027c5e7a 8883+ spin_unlock(&h_dentry->d_lock);
1facf9fc 8884+
b4510431 8885+ err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
1facf9fc 8886+ if (unlikely(err))
8887+ /* do not goto err, to keep the errno */
8888+ break;
8889+
8890+ /* todo: plink too? */
8891+ if (!do_udba)
8892+ continue;
8893+
8894+ /* UDBA tests */
5527c038 8895+ if (unlikely(!!inode != d_is_positive(h_dentry)))
1facf9fc 8896+ goto err;
8897+
5527c038
JR
8898+ h_inode = NULL;
8899+ if (d_is_positive(h_dentry))
8900+ h_inode = d_inode(h_dentry);
1facf9fc 8901+ h_plus = plus;
8902+ h_mode = mode;
8903+ h_cached_inode = h_inode;
8904+ if (h_inode) {
8905+ h_mode = (h_inode->i_mode & S_IFMT);
8906+ h_plus = (h_inode->i_nlink > 0);
8907+ }
8908+ if (inode && ibs <= bindex && bindex <= ibe)
8909+ h_cached_inode = au_h_iptr(inode, bindex);
8910+
523b37e3 8911+ if (!h_nfs) {
38d290e6 8912+ if (unlikely(plus != h_plus && !tmpfile))
523b37e3
AM
8913+ goto err;
8914+ } else {
8915+ if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
8916+ && !is_root
8917+ && !IS_ROOT(h_dentry)
8918+ && unhashed != d_unhashed(h_dentry)))
8919+ goto err;
8920+ }
8921+ if (unlikely(mode != h_mode
1facf9fc 8922+ || h_cached_inode != h_inode))
8923+ goto err;
8924+ continue;
8925+
f6b6e03d 8926+err:
1facf9fc 8927+ err = -EINVAL;
8928+ break;
8929+ }
8930+
523b37e3 8931+ AuTraceErr(err);
1facf9fc 8932+ return err;
8933+}
8934+
027c5e7a 8935+/* todo: consolidate with do_refresh() and au_reval_for_attr() */
1facf9fc 8936+static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
8937+{
8938+ int err;
8939+ struct dentry *parent;
1facf9fc 8940+
027c5e7a 8941+ if (!au_digen_test(dentry, sigen))
1facf9fc 8942+ return 0;
8943+
8944+ parent = dget_parent(dentry);
8945+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 8946+ AuDebugOn(au_digen_test(parent, sigen));
1facf9fc 8947+ au_dbg_verify_gen(parent, sigen);
027c5e7a 8948+ err = au_refresh_dentry(dentry, parent);
1facf9fc 8949+ di_read_unlock(parent, AuLock_IR);
8950+ dput(parent);
027c5e7a 8951+ AuTraceErr(err);
1facf9fc 8952+ return err;
8953+}
8954+
8955+int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
8956+{
8957+ int err;
8958+ struct dentry *d, *parent;
1facf9fc 8959+
027c5e7a 8960+ if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
1facf9fc 8961+ return simple_reval_dpath(dentry, sigen);
8962+
8963+ /* slow loop, keep it simple and stupid */
8964+ /* cf: au_cpup_dirs() */
8965+ err = 0;
8966+ parent = NULL;
027c5e7a 8967+ while (au_digen_test(dentry, sigen)) {
1facf9fc 8968+ d = dentry;
8969+ while (1) {
8970+ dput(parent);
8971+ parent = dget_parent(d);
027c5e7a 8972+ if (!au_digen_test(parent, sigen))
1facf9fc 8973+ break;
8974+ d = parent;
8975+ }
8976+
1facf9fc 8977+ if (d != dentry)
027c5e7a 8978+ di_write_lock_child2(d);
1facf9fc 8979+
8980+ /* someone might update our dentry while we were sleeping */
027c5e7a
AM
8981+ if (au_digen_test(d, sigen)) {
8982+ /*
8983+ * todo: consolidate with simple_reval_dpath(),
8984+ * do_refresh() and au_reval_for_attr().
8985+ */
1facf9fc 8986+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 8987+ err = au_refresh_dentry(d, parent);
1facf9fc 8988+ di_read_unlock(parent, AuLock_IR);
8989+ }
8990+
8991+ if (d != dentry)
8992+ di_write_unlock(d);
8993+ dput(parent);
8994+ if (unlikely(err))
8995+ break;
8996+ }
8997+
8998+ return err;
8999+}
9000+
9001+/*
9002+ * if valid returns 1, otherwise 0.
9003+ */
b4510431 9004+static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
1facf9fc 9005+{
9006+ int valid, err;
9007+ unsigned int sigen;
8b6a4947 9008+ unsigned char do_udba, dirren;
1facf9fc 9009+ struct super_block *sb;
9010+ struct inode *inode;
9011+
027c5e7a 9012+ /* todo: support rcu-walk? */
b4510431 9013+ if (flags & LOOKUP_RCU)
027c5e7a
AM
9014+ return -ECHILD;
9015+
9016+ valid = 0;
9017+ if (unlikely(!au_di(dentry)))
9018+ goto out;
9019+
e49829fe 9020+ valid = 1;
1facf9fc 9021+ sb = dentry->d_sb;
e49829fe
JR
9022+ /*
9023+ * todo: very ugly
9024+ * i_mutex of parent dir may be held,
9025+ * but we should not return 'invalid' due to busy.
9026+ */
9027+ err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
9028+ if (unlikely(err)) {
9029+ valid = err;
027c5e7a 9030+ AuTraceErr(err);
e49829fe
JR
9031+ goto out;
9032+ }
5527c038
JR
9033+ inode = NULL;
9034+ if (d_really_is_positive(dentry))
9035+ inode = d_inode(dentry);
5afbbe0d 9036+ if (unlikely(inode && au_is_bad_inode(inode))) {
c1595e42
JR
9037+ err = -EINVAL;
9038+ AuTraceErr(err);
9039+ goto out_dgrade;
9040+ }
027c5e7a
AM
9041+ if (unlikely(au_dbrange_test(dentry))) {
9042+ err = -EINVAL;
9043+ AuTraceErr(err);
9044+ goto out_dgrade;
1facf9fc 9045+ }
027c5e7a
AM
9046+
9047+ sigen = au_sigen(sb);
9048+ if (au_digen_test(dentry, sigen)) {
1facf9fc 9049+ AuDebugOn(IS_ROOT(dentry));
027c5e7a
AM
9050+ err = au_reval_dpath(dentry, sigen);
9051+ if (unlikely(err)) {
9052+ AuTraceErr(err);
1facf9fc 9053+ goto out_dgrade;
027c5e7a 9054+ }
1facf9fc 9055+ }
9056+ di_downgrade_lock(dentry, AuLock_IR);
9057+
1facf9fc 9058+ err = -EINVAL;
c1595e42 9059+ if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
523b37e3 9060+ && inode
38d290e6 9061+ && !(inode->i_state && I_LINKABLE)
79b8bda9
AM
9062+ && (IS_DEADDIR(inode) || !inode->i_nlink)) {
9063+ AuTraceErr(err);
027c5e7a 9064+ goto out_inval;
79b8bda9 9065+ }
027c5e7a 9066+
1facf9fc 9067+ do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
9068+ if (do_udba && inode) {
5afbbe0d 9069+ aufs_bindex_t btop = au_ibtop(inode);
027c5e7a 9070+ struct inode *h_inode;
1facf9fc 9071+
5afbbe0d
AM
9072+ if (btop >= 0) {
9073+ h_inode = au_h_iptr(inode, btop);
79b8bda9
AM
9074+ if (h_inode && au_test_higen(inode, h_inode)) {
9075+ AuTraceErr(err);
027c5e7a 9076+ goto out_inval;
79b8bda9 9077+ }
027c5e7a 9078+ }
1facf9fc 9079+ }
9080+
8b6a4947
AM
9081+ dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
9082+ err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
5afbbe0d 9083+ if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
1facf9fc 9084+ err = -EIO;
523b37e3
AM
9085+ AuDbg("both of real entry and whiteout found, %p, err %d\n",
9086+ dentry, err);
027c5e7a 9087+ }
e49829fe 9088+ goto out_inval;
1facf9fc 9089+
4f0767ce 9090+out_dgrade:
1facf9fc 9091+ di_downgrade_lock(dentry, AuLock_IR);
e49829fe 9092+out_inval:
1facf9fc 9093+ aufs_read_unlock(dentry, AuLock_IR);
9094+ AuTraceErr(err);
9095+ valid = !err;
e49829fe 9096+out:
027c5e7a 9097+ if (!valid) {
523b37e3 9098+ AuDbg("%pd invalid, %d\n", dentry, valid);
027c5e7a
AM
9099+ d_drop(dentry);
9100+ }
1facf9fc 9101+ return valid;
9102+}
9103+
9104+static void aufs_d_release(struct dentry *dentry)
9105+{
027c5e7a 9106+ if (au_di(dentry)) {
4a4d8108
AM
9107+ au_di_fin(dentry);
9108+ au_hn_di_reinit(dentry);
1facf9fc 9109+ }
1facf9fc 9110+}
9111+
4a4d8108 9112+const struct dentry_operations aufs_dop = {
c06a8ce3
AM
9113+ .d_revalidate = aufs_d_revalidate,
9114+ .d_weak_revalidate = aufs_d_revalidate,
9115+ .d_release = aufs_d_release
1facf9fc 9116+};
79b8bda9
AM
9117+
9118+/* aufs_dop without d_revalidate */
9119+const struct dentry_operations aufs_dop_noreval = {
9120+ .d_release = aufs_d_release
9121+};
7f207e10 9122diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
eca34b5c 9123--- /usr/share/empty/fs/aufs/dentry.h 1970-01-01 01:00:00.000000000 +0100
631230db 9124+++ linux/fs/aufs/dentry.h 2023-10-31 09:31:04.196547417 +0100
2255d0fe 9125@@ -0,0 +1,270 @@
062440b3 9126+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 9127+/*
0dcfbb52 9128+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 9129+ *
0dcfbb52 9130+ * This program is free software; you can redistribute it and/or modify
1facf9fc 9131+ * it under the terms of the GNU General Public License as published by
9132+ * the Free Software Foundation; either version 2 of the License, or
9133+ * (at your option) any later version.
dece6358
AM
9134+ *
9135+ * This program is distributed in the hope that it will be useful,
9136+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9137+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9138+ * GNU General Public License for more details.
9139+ *
9140+ * You should have received a copy of the GNU General Public License
523b37e3 9141+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 9142+ */
9143+
9144+/*
9145+ * lookup and dentry operations
9146+ */
9147+
9148+#ifndef __AUFS_DENTRY_H__
9149+#define __AUFS_DENTRY_H__
9150+
9151+#ifdef __KERNEL__
9152+
dece6358 9153+#include <linux/dcache.h>
8b6a4947 9154+#include "dirren.h"
1facf9fc 9155+#include "rwsem.h"
9156+
1facf9fc 9157+struct au_hdentry {
9158+ struct dentry *hd_dentry;
027c5e7a 9159+ aufs_bindex_t hd_id;
1facf9fc 9160+};
9161+
9162+struct au_dinfo {
9163+ atomic_t di_generation;
9164+
dece6358 9165+ struct au_rwsem di_rwsem;
5afbbe0d 9166+ aufs_bindex_t di_btop, di_bbot, di_bwh, di_bdiropq;
38d290e6 9167+ unsigned char di_tmpfile; /* to allow the different name */
1c60b727 9168+ struct au_hdentry *di_hdentry;
2255d0fe 9169+ struct file *di_htmpfile;
9f237c51 9170+ struct rcu_head rcu;
4a4d8108 9171+} ____cacheline_aligned_in_smp;
1facf9fc 9172+
9173+/* ---------------------------------------------------------------------- */
9174+
5afbbe0d
AM
9175+/* flags for au_lkup_dentry() */
9176+#define AuLkup_ALLOW_NEG 1
9177+#define AuLkup_IGNORE_PERM (1 << 1)
8b6a4947 9178+#define AuLkup_DIRREN (1 << 2)
5afbbe0d
AM
9179+#define au_ftest_lkup(flags, name) ((flags) & AuLkup_##name)
9180+#define au_fset_lkup(flags, name) \
9181+ do { (flags) |= AuLkup_##name; } while (0)
9182+#define au_fclr_lkup(flags, name) \
9183+ do { (flags) &= ~AuLkup_##name; } while (0)
9184+
8b6a4947
AM
9185+#ifndef CONFIG_AUFS_DIRREN
9186+#undef AuLkup_DIRREN
9187+#define AuLkup_DIRREN 0
9188+#endif
9189+
9190+struct au_do_lookup_args {
9191+ unsigned int flags;
9192+ mode_t type;
9193+ struct qstr whname, *name;
9194+ struct au_dr_lookup dirren;
9195+};
9196+
5afbbe0d
AM
9197+/* ---------------------------------------------------------------------- */
9198+
1facf9fc 9199+/* dentry.c */
79b8bda9 9200+extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
1facf9fc 9201+struct au_branch;
5db017eb 9202+struct dentry *au_sio_lkup_one(struct mnt_idmap *idmap, struct qstr *name,
758e9dad 9203+ struct path *ppath);
1facf9fc 9204+int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
9205+ struct dentry *h_parent, struct au_branch *br);
9206+
5afbbe0d
AM
9207+int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
9208+ unsigned int flags);
86dc4139 9209+int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
027c5e7a 9210+int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
1facf9fc 9211+int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
79b8bda9 9212+void au_refresh_dop(struct dentry *dentry, int force_reval);
1facf9fc 9213+
9214+/* dinfo.c */
4a4d8108 9215+void au_di_init_once(void *_di);
027c5e7a
AM
9216+struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
9217+void au_di_free(struct au_dinfo *dinfo);
9218+void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
9219+void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
4a4d8108
AM
9220+int au_di_init(struct dentry *dentry);
9221+void au_di_fin(struct dentry *dentry);
e2f27e51 9222+int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
1facf9fc 9223+
9224+void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
9225+void di_read_unlock(struct dentry *d, int flags);
9226+void di_downgrade_lock(struct dentry *d, int flags);
9227+void di_write_lock(struct dentry *d, unsigned int lsc);
9228+void di_write_unlock(struct dentry *d);
9229+void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
9230+void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
9231+void di_write_unlock2(struct dentry *d1, struct dentry *d2);
9232+
9233+struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
2cbb1c4b 9234+struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
1facf9fc 9235+aufs_bindex_t au_dbtail(struct dentry *dentry);
9236+aufs_bindex_t au_dbtaildir(struct dentry *dentry);
9237+
9238+void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9239+ struct dentry *h_dentry);
027c5e7a
AM
9240+int au_digen_test(struct dentry *dentry, unsigned int sigen);
9241+int au_dbrange_test(struct dentry *dentry);
1facf9fc 9242+void au_update_digen(struct dentry *dentry);
9243+void au_update_dbrange(struct dentry *dentry, int do_put_zero);
5afbbe0d
AM
9244+void au_update_dbtop(struct dentry *dentry);
9245+void au_update_dbbot(struct dentry *dentry);
1facf9fc 9246+int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
9247+
9248+/* ---------------------------------------------------------------------- */
9249+
9250+static inline struct au_dinfo *au_di(struct dentry *dentry)
9251+{
9252+ return dentry->d_fsdata;
9253+}
9254+
9255+/* ---------------------------------------------------------------------- */
9256+
9257+/* lock subclass for dinfo */
9258+enum {
9259+ AuLsc_DI_CHILD, /* child first */
4a4d8108 9260+ AuLsc_DI_CHILD2, /* rename(2), link(2), and cpup at hnotify */
1facf9fc 9261+ AuLsc_DI_CHILD3, /* copyup dirs */
9262+ AuLsc_DI_PARENT,
9263+ AuLsc_DI_PARENT2,
027c5e7a
AM
9264+ AuLsc_DI_PARENT3,
9265+ AuLsc_DI_TMP /* temp for replacing dinfo */
1facf9fc 9266+};
9267+
9268+/*
9269+ * di_read_lock_child, di_write_lock_child,
9270+ * di_read_lock_child2, di_write_lock_child2,
9271+ * di_read_lock_child3, di_write_lock_child3,
9272+ * di_read_lock_parent, di_write_lock_parent,
9273+ * di_read_lock_parent2, di_write_lock_parent2,
9274+ * di_read_lock_parent3, di_write_lock_parent3,
9275+ */
9276+#define AuReadLockFunc(name, lsc) \
9277+static inline void di_read_lock_##name(struct dentry *d, int flags) \
9278+{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
9279+
9280+#define AuWriteLockFunc(name, lsc) \
9281+static inline void di_write_lock_##name(struct dentry *d) \
9282+{ di_write_lock(d, AuLsc_DI_##lsc); }
9283+
9284+#define AuRWLockFuncs(name, lsc) \
9285+ AuReadLockFunc(name, lsc) \
9286+ AuWriteLockFunc(name, lsc)
9287+
9288+AuRWLockFuncs(child, CHILD);
9289+AuRWLockFuncs(child2, CHILD2);
9290+AuRWLockFuncs(child3, CHILD3);
9291+AuRWLockFuncs(parent, PARENT);
9292+AuRWLockFuncs(parent2, PARENT2);
9293+AuRWLockFuncs(parent3, PARENT3);
9294+
9295+#undef AuReadLockFunc
9296+#undef AuWriteLockFunc
9297+#undef AuRWLockFuncs
9298+
9299+#define DiMustNoWaiters(d) AuRwMustNoWaiters(&au_di(d)->di_rwsem)
dece6358
AM
9300+#define DiMustAnyLock(d) AuRwMustAnyLock(&au_di(d)->di_rwsem)
9301+#define DiMustWriteLock(d) AuRwMustWriteLock(&au_di(d)->di_rwsem)
1facf9fc 9302+
9303+/* ---------------------------------------------------------------------- */
9304+
9305+/* todo: memory barrier? */
9306+static inline unsigned int au_digen(struct dentry *d)
9307+{
9308+ return atomic_read(&au_di(d)->di_generation);
9309+}
9310+
9311+static inline void au_h_dentry_init(struct au_hdentry *hdentry)
9312+{
9313+ hdentry->hd_dentry = NULL;
9314+}
9315+
5afbbe0d
AM
9316+static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
9317+ aufs_bindex_t bindex)
9318+{
9319+ return di->di_hdentry + bindex;
9320+}
9321+
1facf9fc 9322+static inline void au_hdput(struct au_hdentry *hd)
9323+{
4a4d8108
AM
9324+ if (hd)
9325+ dput(hd->hd_dentry);
1facf9fc 9326+}
9327+
5afbbe0d 9328+static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
1facf9fc 9329+{
1308ab2a 9330+ DiMustAnyLock(dentry);
5afbbe0d 9331+ return au_di(dentry)->di_btop;
1facf9fc 9332+}
9333+
5afbbe0d 9334+static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
1facf9fc 9335+{
1308ab2a 9336+ DiMustAnyLock(dentry);
5afbbe0d 9337+ return au_di(dentry)->di_bbot;
1facf9fc 9338+}
9339+
9340+static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
9341+{
1308ab2a 9342+ DiMustAnyLock(dentry);
1facf9fc 9343+ return au_di(dentry)->di_bwh;
9344+}
9345+
9346+static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
9347+{
1308ab2a 9348+ DiMustAnyLock(dentry);
1facf9fc 9349+ return au_di(dentry)->di_bdiropq;
9350+}
9351+
9352+/* todo: hard/soft set? */
5afbbe0d 9353+static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
1facf9fc 9354+{
1308ab2a 9355+ DiMustWriteLock(dentry);
5afbbe0d 9356+ au_di(dentry)->di_btop = bindex;
1facf9fc 9357+}
9358+
5afbbe0d 9359+static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
1facf9fc 9360+{
1308ab2a 9361+ DiMustWriteLock(dentry);
5afbbe0d 9362+ au_di(dentry)->di_bbot = bindex;
1facf9fc 9363+}
9364+
9365+static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
9366+{
1308ab2a 9367+ DiMustWriteLock(dentry);
5afbbe0d 9368+ /* dbwh can be outside of btop - bbot range */
1facf9fc 9369+ au_di(dentry)->di_bwh = bindex;
9370+}
9371+
9372+static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
9373+{
1308ab2a 9374+ DiMustWriteLock(dentry);
1facf9fc 9375+ au_di(dentry)->di_bdiropq = bindex;
9376+}
9377+
9378+/* ---------------------------------------------------------------------- */
9379+
4a4d8108 9380+#ifdef CONFIG_AUFS_HNOTIFY
1facf9fc 9381+static inline void au_digen_dec(struct dentry *d)
9382+{
e49829fe 9383+ atomic_dec(&au_di(d)->di_generation);
1facf9fc 9384+}
9385+
4a4d8108 9386+static inline void au_hn_di_reinit(struct dentry *dentry)
1facf9fc 9387+{
9388+ dentry->d_fsdata = NULL;
9389+}
9390+#else
4a4d8108
AM
9391+AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
9392+#endif /* CONFIG_AUFS_HNOTIFY */
1facf9fc 9393+
9394+#endif /* __KERNEL__ */
9395+#endif /* __AUFS_DENTRY_H__ */
7f207e10 9396diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
eca34b5c 9397--- /usr/share/empty/fs/aufs/dinfo.c 1970-01-01 01:00:00.000000000 +0100
594d0238 9398+++ linux/fs/aufs/dinfo.c 2022-12-17 09:21:34.796521861 +0100
2255d0fe 9399@@ -0,0 +1,555 @@
cd7a4cd9 9400+// SPDX-License-Identifier: GPL-2.0
1facf9fc 9401+/*
0dcfbb52 9402+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 9403+ *
0dcfbb52 9404+ * This program is free software; you can redistribute it and/or modify
1facf9fc 9405+ * it under the terms of the GNU General Public License as published by
9406+ * the Free Software Foundation; either version 2 of the License, or
9407+ * (at your option) any later version.
dece6358
AM
9408+ *
9409+ * This program is distributed in the hope that it will be useful,
9410+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9411+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9412+ * GNU General Public License for more details.
9413+ *
9414+ * You should have received a copy of the GNU General Public License
523b37e3 9415+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 9416+ */
9417+
9418+/*
9419+ * dentry private data
9420+ */
9421+
9422+#include "aufs.h"
9423+
e49829fe 9424+void au_di_init_once(void *_dinfo)
4a4d8108 9425+{
e49829fe 9426+ struct au_dinfo *dinfo = _dinfo;
4a4d8108 9427+
e49829fe 9428+ au_rw_init(&dinfo->di_rwsem);
4a4d8108
AM
9429+}
9430+
027c5e7a 9431+struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
1facf9fc 9432+{
9433+ struct au_dinfo *dinfo;
027c5e7a 9434+ int nbr, i;
1facf9fc 9435+
9436+ dinfo = au_cache_alloc_dinfo();
9437+ if (unlikely(!dinfo))
9438+ goto out;
9439+
5afbbe0d 9440+ nbr = au_sbbot(sb) + 1;
1facf9fc 9441+ if (nbr <= 0)
9442+ nbr = 1;
9443+ dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
027c5e7a
AM
9444+ if (dinfo->di_hdentry) {
9445+ au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
5afbbe0d
AM
9446+ dinfo->di_btop = -1;
9447+ dinfo->di_bbot = -1;
027c5e7a
AM
9448+ dinfo->di_bwh = -1;
9449+ dinfo->di_bdiropq = -1;
38d290e6 9450+ dinfo->di_tmpfile = 0;
027c5e7a
AM
9451+ for (i = 0; i < nbr; i++)
9452+ dinfo->di_hdentry[i].hd_id = -1;
2255d0fe 9453+ dinfo->di_htmpfile = NULL;
027c5e7a
AM
9454+ goto out;
9455+ }
1facf9fc 9456+
1c60b727 9457+ au_cache_free_dinfo(dinfo);
027c5e7a
AM
9458+ dinfo = NULL;
9459+
4f0767ce 9460+out:
027c5e7a 9461+ return dinfo;
1facf9fc 9462+}
9463+
027c5e7a 9464+void au_di_free(struct au_dinfo *dinfo)
4a4d8108 9465+{
4a4d8108 9466+ struct au_hdentry *p;
5afbbe0d 9467+ aufs_bindex_t bbot, bindex;
4a4d8108
AM
9468+
9469+ /* dentry may not be revalidated */
5afbbe0d 9470+ bindex = dinfo->di_btop;
4a4d8108 9471+ if (bindex >= 0) {
5afbbe0d
AM
9472+ bbot = dinfo->di_bbot;
9473+ p = au_hdentry(dinfo, bindex);
9474+ while (bindex++ <= bbot)
4a4d8108
AM
9475+ au_hdput(p++);
9476+ }
9f237c51 9477+ au_kfree_try_rcu(dinfo->di_hdentry);
1c60b727 9478+ au_cache_free_dinfo(dinfo);
027c5e7a
AM
9479+}
9480+
9481+void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9482+{
9483+ struct au_hdentry *p;
9484+ aufs_bindex_t bi;
9485+
9486+ AuRwMustWriteLock(&a->di_rwsem);
9487+ AuRwMustWriteLock(&b->di_rwsem);
9488+
9489+#define DiSwap(v, name) \
9490+ do { \
9491+ v = a->di_##name; \
9492+ a->di_##name = b->di_##name; \
9493+ b->di_##name = v; \
9494+ } while (0)
9495+
9496+ DiSwap(p, hdentry);
5afbbe0d
AM
9497+ DiSwap(bi, btop);
9498+ DiSwap(bi, bbot);
027c5e7a
AM
9499+ DiSwap(bi, bwh);
9500+ DiSwap(bi, bdiropq);
9501+ /* smp_mb(); */
9502+
9503+#undef DiSwap
9504+}
9505+
9506+void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9507+{
9508+ AuRwMustWriteLock(&dst->di_rwsem);
9509+ AuRwMustWriteLock(&src->di_rwsem);
9510+
5afbbe0d
AM
9511+ dst->di_btop = src->di_btop;
9512+ dst->di_bbot = src->di_bbot;
027c5e7a
AM
9513+ dst->di_bwh = src->di_bwh;
9514+ dst->di_bdiropq = src->di_bdiropq;
9515+ /* smp_mb(); */
9516+}
9517+
9518+int au_di_init(struct dentry *dentry)
9519+{
9520+ int err;
9521+ struct super_block *sb;
9522+ struct au_dinfo *dinfo;
9523+
9524+ err = 0;
9525+ sb = dentry->d_sb;
9526+ dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9527+ if (dinfo) {
9528+ atomic_set(&dinfo->di_generation, au_sigen(sb));
9529+ /* smp_mb(); */ /* atomic_set */
9530+ dentry->d_fsdata = dinfo;
9531+ } else
9532+ err = -ENOMEM;
9533+
9534+ return err;
9535+}
9536+
9537+void au_di_fin(struct dentry *dentry)
9538+{
9539+ struct au_dinfo *dinfo;
9540+
9541+ dinfo = au_di(dentry);
9542+ AuRwDestroy(&dinfo->di_rwsem);
9543+ au_di_free(dinfo);
4a4d8108
AM
9544+}
9545+
e2f27e51 9546+int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
1facf9fc 9547+{
9548+ int err, sz;
9549+ struct au_hdentry *hdp;
9550+
1308ab2a 9551+ AuRwMustWriteLock(&dinfo->di_rwsem);
9552+
1facf9fc 9553+ err = -ENOMEM;
5afbbe0d 9554+ sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
1facf9fc 9555+ if (!sz)
9556+ sz = sizeof(*hdp);
e2f27e51
AM
9557+ hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
9558+ may_shrink);
1facf9fc 9559+ if (hdp) {
9560+ dinfo->di_hdentry = hdp;
9561+ err = 0;
9562+ }
9563+
9564+ return err;
9565+}
9566+
9567+/* ---------------------------------------------------------------------- */
9568+
9569+static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9570+{
9571+ switch (lsc) {
9572+ case AuLsc_DI_CHILD:
9573+ ii_write_lock_child(inode);
9574+ break;
9575+ case AuLsc_DI_CHILD2:
9576+ ii_write_lock_child2(inode);
9577+ break;
9578+ case AuLsc_DI_CHILD3:
9579+ ii_write_lock_child3(inode);
9580+ break;
9581+ case AuLsc_DI_PARENT:
9582+ ii_write_lock_parent(inode);
9583+ break;
9584+ case AuLsc_DI_PARENT2:
9585+ ii_write_lock_parent2(inode);
9586+ break;
9587+ case AuLsc_DI_PARENT3:
9588+ ii_write_lock_parent3(inode);
9589+ break;
9590+ default:
9591+ BUG();
9592+ }
9593+}
9594+
9595+static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9596+{
9597+ switch (lsc) {
9598+ case AuLsc_DI_CHILD:
9599+ ii_read_lock_child(inode);
9600+ break;
9601+ case AuLsc_DI_CHILD2:
9602+ ii_read_lock_child2(inode);
9603+ break;
9604+ case AuLsc_DI_CHILD3:
9605+ ii_read_lock_child3(inode);
9606+ break;
9607+ case AuLsc_DI_PARENT:
9608+ ii_read_lock_parent(inode);
9609+ break;
9610+ case AuLsc_DI_PARENT2:
9611+ ii_read_lock_parent2(inode);
9612+ break;
9613+ case AuLsc_DI_PARENT3:
9614+ ii_read_lock_parent3(inode);
9615+ break;
9616+ default:
9617+ BUG();
9618+ }
9619+}
9620+
9621+void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9622+{
5527c038
JR
9623+ struct inode *inode;
9624+
dece6358 9625+ au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
5527c038
JR
9626+ if (d_really_is_positive(d)) {
9627+ inode = d_inode(d);
1facf9fc 9628+ if (au_ftest_lock(flags, IW))
5527c038 9629+ do_ii_write_lock(inode, lsc);
1facf9fc 9630+ else if (au_ftest_lock(flags, IR))
5527c038 9631+ do_ii_read_lock(inode, lsc);
1facf9fc 9632+ }
9633+}
9634+
9635+void di_read_unlock(struct dentry *d, int flags)
9636+{
5527c038
JR
9637+ struct inode *inode;
9638+
9639+ if (d_really_is_positive(d)) {
9640+ inode = d_inode(d);
027c5e7a
AM
9641+ if (au_ftest_lock(flags, IW)) {
9642+ au_dbg_verify_dinode(d);
5527c038 9643+ ii_write_unlock(inode);
027c5e7a
AM
9644+ } else if (au_ftest_lock(flags, IR)) {
9645+ au_dbg_verify_dinode(d);
5527c038 9646+ ii_read_unlock(inode);
027c5e7a 9647+ }
1facf9fc 9648+ }
dece6358 9649+ au_rw_read_unlock(&au_di(d)->di_rwsem);
1facf9fc 9650+}
9651+
9652+void di_downgrade_lock(struct dentry *d, int flags)
9653+{
5527c038
JR
9654+ if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9655+ ii_downgrade_lock(d_inode(d));
dece6358 9656+ au_rw_dgrade_lock(&au_di(d)->di_rwsem);
1facf9fc 9657+}
9658+
9659+void di_write_lock(struct dentry *d, unsigned int lsc)
9660+{
dece6358 9661+ au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
5527c038
JR
9662+ if (d_really_is_positive(d))
9663+ do_ii_write_lock(d_inode(d), lsc);
1facf9fc 9664+}
9665+
9666+void di_write_unlock(struct dentry *d)
9667+{
027c5e7a 9668+ au_dbg_verify_dinode(d);
5527c038
JR
9669+ if (d_really_is_positive(d))
9670+ ii_write_unlock(d_inode(d));
dece6358 9671+ au_rw_write_unlock(&au_di(d)->di_rwsem);
1facf9fc 9672+}
9673+
9674+void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9675+{
9676+ AuDebugOn(d1 == d2
5527c038 9677+ || d_inode(d1) == d_inode(d2)
1facf9fc 9678+ || d1->d_sb != d2->d_sb);
9679+
521ced18
JR
9680+ if ((isdir && au_test_subdir(d1, d2))
9681+ || d1 < d2) {
1facf9fc 9682+ di_write_lock_child(d1);
9683+ di_write_lock_child2(d2);
9684+ } else {
1facf9fc 9685+ di_write_lock_child(d2);
9686+ di_write_lock_child2(d1);
9687+ }
9688+}
9689+
9690+void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9691+{
9692+ AuDebugOn(d1 == d2
5527c038 9693+ || d_inode(d1) == d_inode(d2)
1facf9fc 9694+ || d1->d_sb != d2->d_sb);
9695+
521ced18
JR
9696+ if ((isdir && au_test_subdir(d1, d2))
9697+ || d1 < d2) {
1facf9fc 9698+ di_write_lock_parent(d1);
9699+ di_write_lock_parent2(d2);
9700+ } else {
1facf9fc 9701+ di_write_lock_parent(d2);
9702+ di_write_lock_parent2(d1);
9703+ }
9704+}
9705+
9706+void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9707+{
9708+ di_write_unlock(d1);
5527c038 9709+ if (d_inode(d1) == d_inode(d2))
dece6358 9710+ au_rw_write_unlock(&au_di(d2)->di_rwsem);
1facf9fc 9711+ else
9712+ di_write_unlock(d2);
9713+}
9714+
9715+/* ---------------------------------------------------------------------- */
9716+
9717+struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9718+{
9719+ struct dentry *d;
9720+
1308ab2a 9721+ DiMustAnyLock(dentry);
9722+
5afbbe0d 9723+ if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
1facf9fc 9724+ return NULL;
9725+ AuDebugOn(bindex < 0);
5afbbe0d 9726+ d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
c1595e42 9727+ AuDebugOn(d && au_dcount(d) <= 0);
1facf9fc 9728+ return d;
9729+}
9730+
2cbb1c4b
JR
9731+/*
9732+ * extended version of au_h_dptr().
38d290e6
JR
9733+ * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9734+ * error.
2cbb1c4b
JR
9735+ */
9736+struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9737+{
9738+ struct dentry *h_dentry;
9739+ struct inode *inode, *h_inode;
9740+
5527c038 9741+ AuDebugOn(d_really_is_negative(dentry));
2cbb1c4b
JR
9742+
9743+ h_dentry = NULL;
5afbbe0d
AM
9744+ if (au_dbtop(dentry) <= bindex
9745+ && bindex <= au_dbbot(dentry))
2cbb1c4b 9746+ h_dentry = au_h_dptr(dentry, bindex);
38d290e6 9747+ if (h_dentry && !au_d_linkable(h_dentry)) {
2cbb1c4b
JR
9748+ dget(h_dentry);
9749+ goto out; /* success */
9750+ }
9751+
5527c038 9752+ inode = d_inode(dentry);
5afbbe0d
AM
9753+ AuDebugOn(bindex < au_ibtop(inode));
9754+ AuDebugOn(au_ibbot(inode) < bindex);
2cbb1c4b
JR
9755+ h_inode = au_h_iptr(inode, bindex);
9756+ h_dentry = d_find_alias(h_inode);
9757+ if (h_dentry) {
9758+ if (!IS_ERR(h_dentry)) {
38d290e6 9759+ if (!au_d_linkable(h_dentry))
2cbb1c4b
JR
9760+ goto out; /* success */
9761+ dput(h_dentry);
9762+ } else
9763+ goto out;
9764+ }
9765+
9766+ if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9767+ h_dentry = au_plink_lkup(inode, bindex);
9768+ AuDebugOn(!h_dentry);
9769+ if (!IS_ERR(h_dentry)) {
9770+ if (!au_d_hashed_positive(h_dentry))
9771+ goto out; /* success */
9772+ dput(h_dentry);
9773+ h_dentry = NULL;
9774+ }
9775+ }
9776+
9777+out:
9778+ AuDbgDentry(h_dentry);
9779+ return h_dentry;
9780+}
9781+
1facf9fc 9782+aufs_bindex_t au_dbtail(struct dentry *dentry)
9783+{
5afbbe0d 9784+ aufs_bindex_t bbot, bwh;
1facf9fc 9785+
5afbbe0d
AM
9786+ bbot = au_dbbot(dentry);
9787+ if (0 <= bbot) {
1facf9fc 9788+ bwh = au_dbwh(dentry);
9789+ if (!bwh)
9790+ return bwh;
5afbbe0d 9791+ if (0 < bwh && bwh < bbot)
1facf9fc 9792+ return bwh - 1;
9793+ }
5afbbe0d 9794+ return bbot;
1facf9fc 9795+}
9796+
9797+aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9798+{
5afbbe0d 9799+ aufs_bindex_t bbot, bopq;
1facf9fc 9800+
5afbbe0d
AM
9801+ bbot = au_dbtail(dentry);
9802+ if (0 <= bbot) {
1facf9fc 9803+ bopq = au_dbdiropq(dentry);
5afbbe0d
AM
9804+ if (0 <= bopq && bopq < bbot)
9805+ bbot = bopq;
1facf9fc 9806+ }
5afbbe0d 9807+ return bbot;
1facf9fc 9808+}
9809+
9810+/* ---------------------------------------------------------------------- */
9811+
9812+void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9813+ struct dentry *h_dentry)
9814+{
5afbbe0d
AM
9815+ struct au_dinfo *dinfo;
9816+ struct au_hdentry *hd;
027c5e7a 9817+ struct au_branch *br;
1facf9fc 9818+
1308ab2a 9819+ DiMustWriteLock(dentry);
9820+
5afbbe0d
AM
9821+ dinfo = au_di(dentry);
9822+ hd = au_hdentry(dinfo, bindex);
4a4d8108 9823+ au_hdput(hd);
1facf9fc 9824+ hd->hd_dentry = h_dentry;
027c5e7a
AM
9825+ if (h_dentry) {
9826+ br = au_sbr(dentry->d_sb, bindex);
9827+ hd->hd_id = br->br_id;
9828+ }
9829+}
9830+
9831+int au_dbrange_test(struct dentry *dentry)
9832+{
9833+ int err;
5afbbe0d 9834+ aufs_bindex_t btop, bbot;
027c5e7a
AM
9835+
9836+ err = 0;
5afbbe0d
AM
9837+ btop = au_dbtop(dentry);
9838+ bbot = au_dbbot(dentry);
9839+ if (btop >= 0)
9840+ AuDebugOn(bbot < 0 && btop > bbot);
027c5e7a
AM
9841+ else {
9842+ err = -EIO;
5afbbe0d 9843+ AuDebugOn(bbot >= 0);
027c5e7a
AM
9844+ }
9845+
9846+ return err;
9847+}
9848+
9849+int au_digen_test(struct dentry *dentry, unsigned int sigen)
9850+{
9851+ int err;
9852+
9853+ err = 0;
9854+ if (unlikely(au_digen(dentry) != sigen
5527c038 9855+ || au_iigen_test(d_inode(dentry), sigen)))
027c5e7a
AM
9856+ err = -EIO;
9857+
9858+ return err;
1facf9fc 9859+}
9860+
9861+void au_update_digen(struct dentry *dentry)
9862+{
9863+ atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
9864+ /* smp_mb(); */ /* atomic_set */
9865+}
9866+
9867+void au_update_dbrange(struct dentry *dentry, int do_put_zero)
9868+{
9869+ struct au_dinfo *dinfo;
9870+ struct dentry *h_d;
4a4d8108 9871+ struct au_hdentry *hdp;
5afbbe0d 9872+ aufs_bindex_t bindex, bbot;
1facf9fc 9873+
1308ab2a 9874+ DiMustWriteLock(dentry);
9875+
1facf9fc 9876+ dinfo = au_di(dentry);
5afbbe0d 9877+ if (!dinfo || dinfo->di_btop < 0)
1facf9fc 9878+ return;
9879+
9880+ if (do_put_zero) {
5afbbe0d
AM
9881+ bbot = dinfo->di_bbot;
9882+ bindex = dinfo->di_btop;
9883+ hdp = au_hdentry(dinfo, bindex);
9884+ for (; bindex <= bbot; bindex++, hdp++) {
9885+ h_d = hdp->hd_dentry;
5527c038 9886+ if (h_d && d_is_negative(h_d))
1facf9fc 9887+ au_set_h_dptr(dentry, bindex, NULL);
9888+ }
9889+ }
9890+
5afbbe0d
AM
9891+ dinfo->di_btop = 0;
9892+ hdp = au_hdentry(dinfo, dinfo->di_btop);
9893+ for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
9894+ if (hdp->hd_dentry)
1facf9fc 9895+ break;
5afbbe0d
AM
9896+ if (dinfo->di_btop > dinfo->di_bbot) {
9897+ dinfo->di_btop = -1;
9898+ dinfo->di_bbot = -1;
1facf9fc 9899+ return;
9900+ }
9901+
5afbbe0d
AM
9902+ hdp = au_hdentry(dinfo, dinfo->di_bbot);
9903+ for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
9904+ if (hdp->hd_dentry)
1facf9fc 9905+ break;
5afbbe0d 9906+ AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
1facf9fc 9907+}
9908+
5afbbe0d 9909+void au_update_dbtop(struct dentry *dentry)
1facf9fc 9910+{
5afbbe0d 9911+ aufs_bindex_t bindex, bbot;
1facf9fc 9912+ struct dentry *h_dentry;
9913+
5afbbe0d
AM
9914+ bbot = au_dbbot(dentry);
9915+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
1facf9fc 9916+ h_dentry = au_h_dptr(dentry, bindex);
9917+ if (!h_dentry)
9918+ continue;
5527c038 9919+ if (d_is_positive(h_dentry)) {
5afbbe0d 9920+ au_set_dbtop(dentry, bindex);
1facf9fc 9921+ return;
9922+ }
9923+ au_set_h_dptr(dentry, bindex, NULL);
9924+ }
9925+}
9926+
5afbbe0d 9927+void au_update_dbbot(struct dentry *dentry)
1facf9fc 9928+{
5afbbe0d 9929+ aufs_bindex_t bindex, btop;
1facf9fc 9930+ struct dentry *h_dentry;
9931+
5afbbe0d
AM
9932+ btop = au_dbtop(dentry);
9933+ for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
1facf9fc 9934+ h_dentry = au_h_dptr(dentry, bindex);
9935+ if (!h_dentry)
9936+ continue;
5527c038 9937+ if (d_is_positive(h_dentry)) {
5afbbe0d 9938+ au_set_dbbot(dentry, bindex);
1facf9fc 9939+ return;
9940+ }
9941+ au_set_h_dptr(dentry, bindex, NULL);
9942+ }
9943+}
9944+
9945+int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
9946+{
5afbbe0d 9947+ aufs_bindex_t bindex, bbot;
1facf9fc 9948+
5afbbe0d
AM
9949+ bbot = au_dbbot(dentry);
9950+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
1facf9fc 9951+ if (au_h_dptr(dentry, bindex) == h_dentry)
9952+ return bindex;
9953+ return -1;
9954+}
7f207e10 9955diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
eca34b5c 9956--- /usr/share/empty/fs/aufs/dir.c 1970-01-01 01:00:00.000000000 +0100
631230db 9957+++ linux/fs/aufs/dir.c 2023-10-31 09:31:04.196547417 +0100
0b2a12c6 9958@@ -0,0 +1,765 @@
cd7a4cd9 9959+// SPDX-License-Identifier: GPL-2.0
1facf9fc 9960+/*
0dcfbb52 9961+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 9962+ *
0dcfbb52 9963+ * This program is free software; you can redistribute it and/or modify
1facf9fc 9964+ * it under the terms of the GNU General Public License as published by
9965+ * the Free Software Foundation; either version 2 of the License, or
9966+ * (at your option) any later version.
dece6358
AM
9967+ *
9968+ * This program is distributed in the hope that it will be useful,
9969+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9970+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9971+ * GNU General Public License for more details.
9972+ *
9973+ * You should have received a copy of the GNU General Public License
523b37e3 9974+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 9975+ */
9976+
9977+/*
9978+ * directory operations
9979+ */
9980+
9981+#include <linux/fs_stack.h>
eca801bf 9982+#include <linux/iversion.h>
1facf9fc 9983+#include "aufs.h"
9984+
9985+void au_add_nlink(struct inode *dir, struct inode *h_dir)
9986+{
9dbd164d
AM
9987+ unsigned int nlink;
9988+
1facf9fc 9989+ AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
9990+
9dbd164d
AM
9991+ nlink = dir->i_nlink;
9992+ nlink += h_dir->i_nlink - 2;
1facf9fc 9993+ if (h_dir->i_nlink < 2)
9dbd164d 9994+ nlink += 2;
f6b6e03d 9995+ smp_mb(); /* for i_nlink */
7eafdf33 9996+ /* 0 can happen in revaliding */
92d182d2 9997+ set_nlink(dir, nlink);
1facf9fc 9998+}
9999+
10000+void au_sub_nlink(struct inode *dir, struct inode *h_dir)
10001+{
9dbd164d
AM
10002+ unsigned int nlink;
10003+
1facf9fc 10004+ AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10005+
9dbd164d
AM
10006+ nlink = dir->i_nlink;
10007+ nlink -= h_dir->i_nlink - 2;
1facf9fc 10008+ if (h_dir->i_nlink < 2)
9dbd164d 10009+ nlink -= 2;
f6b6e03d 10010+ smp_mb(); /* for i_nlink */
92d182d2 10011+ /* nlink == 0 means the branch-fs is broken */
9dbd164d 10012+ set_nlink(dir, nlink);
1facf9fc 10013+}
10014+
1308ab2a 10015+loff_t au_dir_size(struct file *file, struct dentry *dentry)
10016+{
10017+ loff_t sz;
5afbbe0d 10018+ aufs_bindex_t bindex, bbot;
1308ab2a 10019+ struct file *h_file;
10020+ struct dentry *h_dentry;
10021+
10022+ sz = 0;
10023+ if (file) {
2000de60 10024+ AuDebugOn(!d_is_dir(file->f_path.dentry));
1308ab2a 10025+
5afbbe0d
AM
10026+ bbot = au_fbbot_dir(file);
10027+ for (bindex = au_fbtop(file);
10028+ bindex <= bbot && sz < KMALLOC_MAX_SIZE;
1308ab2a 10029+ bindex++) {
4a4d8108 10030+ h_file = au_hf_dir(file, bindex);
c06a8ce3
AM
10031+ if (h_file && file_inode(h_file))
10032+ sz += vfsub_f_size_read(h_file);
1308ab2a 10033+ }
10034+ } else {
10035+ AuDebugOn(!dentry);
2000de60 10036+ AuDebugOn(!d_is_dir(dentry));
1308ab2a 10037+
5afbbe0d
AM
10038+ bbot = au_dbtaildir(dentry);
10039+ for (bindex = au_dbtop(dentry);
10040+ bindex <= bbot && sz < KMALLOC_MAX_SIZE;
1308ab2a 10041+ bindex++) {
10042+ h_dentry = au_h_dptr(dentry, bindex);
5527c038
JR
10043+ if (h_dentry && d_is_positive(h_dentry))
10044+ sz += i_size_read(d_inode(h_dentry));
1308ab2a 10045+ }
10046+ }
10047+ if (sz < KMALLOC_MAX_SIZE)
10048+ sz = roundup_pow_of_two(sz);
10049+ if (sz > KMALLOC_MAX_SIZE)
10050+ sz = KMALLOC_MAX_SIZE;
10051+ else if (sz < NAME_MAX) {
10052+ BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
10053+ sz = AUFS_RDBLK_DEF;
10054+ }
10055+ return sz;
10056+}
10057+
b912730e
AM
10058+struct au_dir_ts_arg {
10059+ struct dentry *dentry;
10060+ aufs_bindex_t brid;
10061+};
10062+
10063+static void au_do_dir_ts(void *arg)
10064+{
10065+ struct au_dir_ts_arg *a = arg;
10066+ struct au_dtime dt;
10067+ struct path h_path;
10068+ struct inode *dir, *h_dir;
10069+ struct super_block *sb;
10070+ struct au_branch *br;
10071+ struct au_hinode *hdir;
10072+ int err;
5afbbe0d 10073+ aufs_bindex_t btop, bindex;
b912730e
AM
10074+
10075+ sb = a->dentry->d_sb;
5527c038 10076+ if (d_really_is_negative(a->dentry))
b912730e 10077+ goto out;
5527c038 10078+ /* no dir->i_mutex lock */
b95c5147
AM
10079+ aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
10080+
5527c038 10081+ dir = d_inode(a->dentry);
5afbbe0d 10082+ btop = au_ibtop(dir);
b912730e 10083+ bindex = au_br_index(sb, a->brid);
5afbbe0d 10084+ if (bindex < btop)
b912730e
AM
10085+ goto out_unlock;
10086+
10087+ br = au_sbr(sb, bindex);
10088+ h_path.dentry = au_h_dptr(a->dentry, bindex);
10089+ if (!h_path.dentry)
10090+ goto out_unlock;
10091+ h_path.mnt = au_br_mnt(br);
10092+ au_dtime_store(&dt, a->dentry, &h_path);
10093+
5afbbe0d 10094+ br = au_sbr(sb, btop);
b912730e
AM
10095+ if (!au_br_writable(br->br_perm))
10096+ goto out_unlock;
5afbbe0d 10097+ h_path.dentry = au_h_dptr(a->dentry, btop);
b912730e
AM
10098+ h_path.mnt = au_br_mnt(br);
10099+ err = vfsub_mnt_want_write(h_path.mnt);
10100+ if (err)
10101+ goto out_unlock;
5afbbe0d
AM
10102+ hdir = au_hi(dir, btop);
10103+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
10104+ h_dir = au_h_iptr(dir, btop);
b912730e 10105+ if (h_dir->i_nlink
cd7a4cd9 10106+ && timespec64_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
b912730e
AM
10107+ dt.dt_h_path = h_path;
10108+ au_dtime_revert(&dt);
10109+ }
5afbbe0d 10110+ au_hn_inode_unlock(hdir);
b912730e
AM
10111+ vfsub_mnt_drop_write(h_path.mnt);
10112+ au_cpup_attr_timesizes(dir);
10113+
10114+out_unlock:
10115+ aufs_read_unlock(a->dentry, AuLock_DW);
10116+out:
10117+ dput(a->dentry);
10118+ au_nwt_done(&au_sbi(sb)->si_nowait);
9f237c51 10119+ au_kfree_try_rcu(arg);
b912730e
AM
10120+}
10121+
10122+void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
10123+{
10124+ int perm, wkq_err;
5afbbe0d 10125+ aufs_bindex_t btop;
b912730e
AM
10126+ struct au_dir_ts_arg *arg;
10127+ struct dentry *dentry;
10128+ struct super_block *sb;
10129+
10130+ IMustLock(dir);
10131+
10132+ dentry = d_find_any_alias(dir);
10133+ AuDebugOn(!dentry);
10134+ sb = dentry->d_sb;
5afbbe0d
AM
10135+ btop = au_ibtop(dir);
10136+ if (btop == bindex) {
b912730e
AM
10137+ au_cpup_attr_timesizes(dir);
10138+ goto out;
10139+ }
10140+
5afbbe0d 10141+ perm = au_sbr_perm(sb, btop);
b912730e
AM
10142+ if (!au_br_writable(perm))
10143+ goto out;
10144+
10145+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
10146+ if (!arg)
10147+ goto out;
10148+
10149+ arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
10150+ arg->brid = au_sbr_id(sb, bindex);
10151+ wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
10152+ if (unlikely(wkq_err)) {
10153+ pr_err("wkq %d\n", wkq_err);
10154+ dput(dentry);
9f237c51 10155+ au_kfree_try_rcu(arg);
b912730e
AM
10156+ }
10157+
10158+out:
10159+ dput(dentry);
10160+}
10161+
1facf9fc 10162+/* ---------------------------------------------------------------------- */
10163+
10164+static int reopen_dir(struct file *file)
10165+{
10166+ int err;
10167+ unsigned int flags;
5afbbe0d 10168+ aufs_bindex_t bindex, btail, btop;
1facf9fc 10169+ struct dentry *dentry, *h_dentry;
10170+ struct file *h_file;
10171+
10172+ /* open all lower dirs */
2000de60 10173+ dentry = file->f_path.dentry;
5afbbe0d
AM
10174+ btop = au_dbtop(dentry);
10175+ for (bindex = au_fbtop(file); bindex < btop; bindex++)
1facf9fc 10176+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d 10177+ au_set_fbtop(file, btop);
1facf9fc 10178+
10179+ btail = au_dbtaildir(dentry);
5afbbe0d 10180+ for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
1facf9fc 10181+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d 10182+ au_set_fbbot_dir(file, btail);
1facf9fc 10183+
4a4d8108 10184+ flags = vfsub_file_flags(file);
5afbbe0d 10185+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 10186+ h_dentry = au_h_dptr(dentry, bindex);
10187+ if (!h_dentry)
10188+ continue;
4a4d8108 10189+ h_file = au_hf_dir(file, bindex);
1facf9fc 10190+ if (h_file)
10191+ continue;
10192+
392086de 10193+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
1facf9fc 10194+ err = PTR_ERR(h_file);
10195+ if (IS_ERR(h_file))
10196+ goto out; /* close all? */
10197+ au_set_h_fptr(file, bindex, h_file);
10198+ }
10199+ au_update_figen(file);
10200+ /* todo: necessary? */
10201+ /* file->f_ra = h_file->f_ra; */
10202+ err = 0;
10203+
4f0767ce 10204+out:
1facf9fc 10205+ return err;
10206+}
10207+
b912730e 10208+static int do_open_dir(struct file *file, int flags, struct file *h_file)
1facf9fc 10209+{
10210+ int err;
10211+ aufs_bindex_t bindex, btail;
10212+ struct dentry *dentry, *h_dentry;
8cdd5066 10213+ struct vfsmount *mnt;
1facf9fc 10214+
1308ab2a 10215+ FiMustWriteLock(file);
b912730e 10216+ AuDebugOn(h_file);
1308ab2a 10217+
523b37e3 10218+ err = 0;
8cdd5066 10219+ mnt = file->f_path.mnt;
2000de60 10220+ dentry = file->f_path.dentry;
be118d29 10221+ file->f_version = inode_query_iversion(d_inode(dentry));
5afbbe0d
AM
10222+ bindex = au_dbtop(dentry);
10223+ au_set_fbtop(file, bindex);
1facf9fc 10224+ btail = au_dbtaildir(dentry);
5afbbe0d 10225+ au_set_fbbot_dir(file, btail);
1facf9fc 10226+ for (; !err && bindex <= btail; bindex++) {
10227+ h_dentry = au_h_dptr(dentry, bindex);
10228+ if (!h_dentry)
10229+ continue;
10230+
8cdd5066
JR
10231+ err = vfsub_test_mntns(mnt, h_dentry->d_sb);
10232+ if (unlikely(err))
10233+ break;
392086de 10234+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
1facf9fc 10235+ if (IS_ERR(h_file)) {
10236+ err = PTR_ERR(h_file);
10237+ break;
10238+ }
10239+ au_set_h_fptr(file, bindex, h_file);
10240+ }
10241+ au_update_figen(file);
10242+ /* todo: necessary? */
10243+ /* file->f_ra = h_file->f_ra; */
10244+ if (!err)
10245+ return 0; /* success */
10246+
10247+ /* close all */
5afbbe0d 10248+ for (bindex = au_fbtop(file); bindex <= btail; bindex++)
1facf9fc 10249+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d
AM
10250+ au_set_fbtop(file, -1);
10251+ au_set_fbbot_dir(file, -1);
4a4d8108 10252+
1facf9fc 10253+ return err;
10254+}
10255+
10256+static int aufs_open_dir(struct inode *inode __maybe_unused,
10257+ struct file *file)
10258+{
4a4d8108
AM
10259+ int err;
10260+ struct super_block *sb;
10261+ struct au_fidir *fidir;
10262+
10263+ err = -ENOMEM;
2000de60 10264+ sb = file->f_path.dentry->d_sb;
4a4d8108 10265+ si_read_lock(sb, AuLock_FLUSH);
e49829fe 10266+ fidir = au_fidir_alloc(sb);
4a4d8108 10267+ if (fidir) {
b912730e
AM
10268+ struct au_do_open_args args = {
10269+ .open = do_open_dir,
10270+ .fidir = fidir
10271+ };
10272+ err = au_do_open(file, &args);
4a4d8108 10273+ if (unlikely(err))
9f237c51 10274+ au_kfree_rcu(fidir);
4a4d8108
AM
10275+ }
10276+ si_read_unlock(sb);
10277+ return err;
1facf9fc 10278+}
10279+
10280+static int aufs_release_dir(struct inode *inode __maybe_unused,
10281+ struct file *file)
10282+{
10283+ struct au_vdir *vdir_cache;
4a4d8108
AM
10284+ struct au_finfo *finfo;
10285+ struct au_fidir *fidir;
f0c0a007 10286+ struct au_hfile *hf;
5afbbe0d 10287+ aufs_bindex_t bindex, bbot;
1facf9fc 10288+
4a4d8108
AM
10289+ finfo = au_fi(file);
10290+ fidir = finfo->fi_hdir;
10291+ if (fidir) {
8b6a4947
AM
10292+ au_hbl_del(&finfo->fi_hlist,
10293+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
4a4d8108
AM
10294+ vdir_cache = fidir->fd_vdir_cache; /* lock-free */
10295+ if (vdir_cache)
1c60b727 10296+ au_vdir_free(vdir_cache);
4a4d8108
AM
10297+
10298+ bindex = finfo->fi_btop;
10299+ if (bindex >= 0) {
f0c0a007 10300+ hf = fidir->fd_hfile + bindex;
4a4d8108
AM
10301+ /*
10302+ * calls fput() instead of filp_close(),
10303+ * since no dnotify or lock for the lower file.
10304+ */
5afbbe0d 10305+ bbot = fidir->fd_bbot;
f0c0a007
AM
10306+ for (; bindex <= bbot; bindex++, hf++)
10307+ if (hf->hf_file)
1c60b727 10308+ au_hfput(hf, /*execed*/0);
4a4d8108 10309+ }
9f237c51 10310+ au_kfree_rcu(fidir);
4a4d8108 10311+ finfo->fi_hdir = NULL;
1facf9fc 10312+ }
1c60b727 10313+ au_finfo_fin(file);
1facf9fc 10314+ return 0;
10315+}
10316+
10317+/* ---------------------------------------------------------------------- */
10318+
4a4d8108
AM
10319+static int au_do_flush_dir(struct file *file, fl_owner_t id)
10320+{
10321+ int err;
5afbbe0d 10322+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
10323+ struct file *h_file;
10324+
10325+ err = 0;
5afbbe0d
AM
10326+ bbot = au_fbbot_dir(file);
10327+ for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
4a4d8108
AM
10328+ h_file = au_hf_dir(file, bindex);
10329+ if (h_file)
10330+ err = vfsub_flush(h_file, id);
10331+ }
10332+ return err;
10333+}
10334+
10335+static int aufs_flush_dir(struct file *file, fl_owner_t id)
10336+{
10337+ return au_do_flush(file, id, au_do_flush_dir);
10338+}
10339+
10340+/* ---------------------------------------------------------------------- */
10341+
1facf9fc 10342+static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
10343+{
10344+ int err;
5afbbe0d 10345+ aufs_bindex_t bbot, bindex;
1facf9fc 10346+ struct inode *inode;
10347+ struct super_block *sb;
10348+
10349+ err = 0;
10350+ sb = dentry->d_sb;
5527c038 10351+ inode = d_inode(dentry);
1facf9fc 10352+ IMustLock(inode);
5afbbe0d
AM
10353+ bbot = au_dbbot(dentry);
10354+ for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
1facf9fc 10355+ struct path h_path;
1facf9fc 10356+
10357+ if (au_test_ro(sb, bindex, inode))
10358+ continue;
10359+ h_path.dentry = au_h_dptr(dentry, bindex);
10360+ if (!h_path.dentry)
10361+ continue;
1facf9fc 10362+
1facf9fc 10363+ h_path.mnt = au_sbr_mnt(sb, bindex);
53392da6 10364+ err = vfsub_fsync(NULL, &h_path, datasync);
1facf9fc 10365+ }
10366+
10367+ return err;
10368+}
10369+
10370+static int au_do_fsync_dir(struct file *file, int datasync)
10371+{
10372+ int err;
5afbbe0d 10373+ aufs_bindex_t bbot, bindex;
1facf9fc 10374+ struct file *h_file;
10375+ struct super_block *sb;
10376+ struct inode *inode;
1facf9fc 10377+
521ced18 10378+ err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
1facf9fc 10379+ if (unlikely(err))
10380+ goto out;
10381+
c06a8ce3 10382+ inode = file_inode(file);
b912730e 10383+ sb = inode->i_sb;
5afbbe0d
AM
10384+ bbot = au_fbbot_dir(file);
10385+ for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
4a4d8108 10386+ h_file = au_hf_dir(file, bindex);
1facf9fc 10387+ if (!h_file || au_test_ro(sb, bindex, inode))
10388+ continue;
10389+
53392da6 10390+ err = vfsub_fsync(h_file, &h_file->f_path, datasync);
1facf9fc 10391+ }
10392+
4f0767ce 10393+out:
1facf9fc 10394+ return err;
10395+}
10396+
10397+/*
10398+ * @file may be NULL
10399+ */
1e00d052
AM
10400+static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
10401+ int datasync)
1facf9fc 10402+{
10403+ int err;
b752ccd1 10404+ struct dentry *dentry;
5527c038 10405+ struct inode *inode;
1facf9fc 10406+ struct super_block *sb;
1facf9fc 10407+
10408+ err = 0;
2000de60 10409+ dentry = file->f_path.dentry;
5527c038 10410+ inode = d_inode(dentry);
febd17d6 10411+ inode_lock(inode);
1facf9fc 10412+ sb = dentry->d_sb;
10413+ si_noflush_read_lock(sb);
10414+ if (file)
10415+ err = au_do_fsync_dir(file, datasync);
10416+ else {
10417+ di_write_lock_child(dentry);
10418+ err = au_do_fsync_dir_no_file(dentry, datasync);
10419+ }
5527c038 10420+ au_cpup_attr_timesizes(inode);
1facf9fc 10421+ di_write_unlock(dentry);
10422+ if (file)
10423+ fi_write_unlock(file);
10424+
10425+ si_read_unlock(sb);
febd17d6 10426+ inode_unlock(inode);
1facf9fc 10427+ return err;
10428+}
10429+
10430+/* ---------------------------------------------------------------------- */
10431+
5afbbe0d 10432+static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
1facf9fc 10433+{
10434+ int err;
10435+ struct dentry *dentry;
9dbd164d 10436+ struct inode *inode, *h_inode;
1facf9fc 10437+ struct super_block *sb;
10438+
062440b3 10439+ AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
392086de 10440+
2000de60 10441+ dentry = file->f_path.dentry;
5527c038 10442+ inode = d_inode(dentry);
1facf9fc 10443+ IMustLock(inode);
10444+
10445+ sb = dentry->d_sb;
10446+ si_read_lock(sb, AuLock_FLUSH);
521ced18 10447+ err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
1facf9fc 10448+ if (unlikely(err))
10449+ goto out;
027c5e7a
AM
10450+ err = au_alive_dir(dentry);
10451+ if (!err)
10452+ err = au_vdir_init(file);
1facf9fc 10453+ di_downgrade_lock(dentry, AuLock_IR);
10454+ if (unlikely(err))
10455+ goto out_unlock;
10456+
5afbbe0d 10457+ h_inode = au_h_iptr(inode, au_ibtop(inode));
b752ccd1 10458+ if (!au_test_nfsd()) {
392086de 10459+ err = au_vdir_fill_de(file, ctx);
9dbd164d 10460+ fsstack_copy_attr_atime(inode, h_inode);
1facf9fc 10461+ } else {
10462+ /*
10463+ * nfsd filldir may call lookup_one_len(), vfs_getattr(),
10464+ * encode_fh() and others.
10465+ */
9dbd164d 10466+ atomic_inc(&h_inode->i_count);
1facf9fc 10467+ di_read_unlock(dentry, AuLock_IR);
10468+ si_read_unlock(sb);
392086de 10469+ err = au_vdir_fill_de(file, ctx);
1facf9fc 10470+ fsstack_copy_attr_atime(inode, h_inode);
10471+ fi_write_unlock(file);
9dbd164d 10472+ iput(h_inode);
1facf9fc 10473+
10474+ AuTraceErr(err);
10475+ return err;
10476+ }
10477+
4f0767ce 10478+out_unlock:
1facf9fc 10479+ di_read_unlock(dentry, AuLock_IR);
10480+ fi_write_unlock(file);
4f0767ce 10481+out:
1facf9fc 10482+ si_read_unlock(sb);
10483+ return err;
10484+}
10485+
10486+/* ---------------------------------------------------------------------- */
10487+
10488+#define AuTestEmpty_WHONLY 1
dece6358
AM
10489+#define AuTestEmpty_CALLED (1 << 1)
10490+#define AuTestEmpty_SHWH (1 << 2)
1facf9fc 10491+#define au_ftest_testempty(flags, name) ((flags) & AuTestEmpty_##name)
7f207e10
AM
10492+#define au_fset_testempty(flags, name) \
10493+ do { (flags) |= AuTestEmpty_##name; } while (0)
10494+#define au_fclr_testempty(flags, name) \
10495+ do { (flags) &= ~AuTestEmpty_##name; } while (0)
1facf9fc 10496+
dece6358
AM
10497+#ifndef CONFIG_AUFS_SHWH
10498+#undef AuTestEmpty_SHWH
10499+#define AuTestEmpty_SHWH 0
10500+#endif
10501+
1facf9fc 10502+struct test_empty_arg {
392086de 10503+ struct dir_context ctx;
1308ab2a 10504+ struct au_nhash *whlist;
1facf9fc 10505+ unsigned int flags;
10506+ int err;
10507+ aufs_bindex_t bindex;
10508+};
10509+
2255d0fe
AM
10510+static bool test_empty_cb(struct dir_context *ctx, const char *__name,
10511+ int namelen, loff_t offset __maybe_unused, u64 ino,
10512+ unsigned int d_type)
1facf9fc 10513+{
392086de
AM
10514+ struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10515+ ctx);
1facf9fc 10516+ char *name = (void *)__name;
10517+
10518+ arg->err = 0;
10519+ au_fset_testempty(arg->flags, CALLED);
10520+ /* smp_mb(); */
10521+ if (name[0] == '.'
10522+ && (namelen == 1 || (name[1] == '.' && namelen == 2)))
10523+ goto out; /* success */
10524+
10525+ if (namelen <= AUFS_WH_PFX_LEN
10526+ || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10527+ if (au_ftest_testempty(arg->flags, WHONLY)
1308ab2a 10528+ && !au_nhash_test_known_wh(arg->whlist, name, namelen))
1facf9fc 10529+ arg->err = -ENOTEMPTY;
10530+ goto out;
10531+ }
10532+
10533+ name += AUFS_WH_PFX_LEN;
10534+ namelen -= AUFS_WH_PFX_LEN;
1308ab2a 10535+ if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
1facf9fc 10536+ arg->err = au_nhash_append_wh
1308ab2a 10537+ (arg->whlist, name, namelen, ino, d_type, arg->bindex,
dece6358 10538+ au_ftest_testempty(arg->flags, SHWH));
1facf9fc 10539+
4f0767ce 10540+out:
1facf9fc 10541+ /* smp_mb(); */
10542+ AuTraceErr(arg->err);
2255d0fe 10543+ return !arg->err;
1facf9fc 10544+}
10545+
10546+static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10547+{
10548+ int err;
10549+ struct file *h_file;
acd2b654 10550+ struct au_branch *br;
1facf9fc 10551+
10552+ h_file = au_h_open(dentry, arg->bindex,
10553+ O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
392086de 10554+ /*file*/NULL, /*force_wr*/0);
1facf9fc 10555+ err = PTR_ERR(h_file);
10556+ if (IS_ERR(h_file))
10557+ goto out;
10558+
10559+ err = 0;
10560+ if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
c06a8ce3 10561+ && !file_inode(h_file)->i_nlink)
1facf9fc 10562+ goto out_put;
10563+
10564+ do {
10565+ arg->err = 0;
10566+ au_fclr_testempty(arg->flags, CALLED);
10567+ /* smp_mb(); */
392086de 10568+ err = vfsub_iterate_dir(h_file, &arg->ctx);
1facf9fc 10569+ if (err >= 0)
10570+ err = arg->err;
10571+ } while (!err && au_ftest_testempty(arg->flags, CALLED));
10572+
4f0767ce 10573+out_put:
1facf9fc 10574+ fput(h_file);
acd2b654
AM
10575+ br = au_sbr(dentry->d_sb, arg->bindex);
10576+ au_lcnt_dec(&br->br_nfiles);
4f0767ce 10577+out:
1facf9fc 10578+ return err;
10579+}
10580+
10581+struct do_test_empty_args {
10582+ int *errp;
10583+ struct dentry *dentry;
10584+ struct test_empty_arg *arg;
10585+};
10586+
10587+static void call_do_test_empty(void *args)
10588+{
10589+ struct do_test_empty_args *a = args;
10590+ *a->errp = do_test_empty(a->dentry, a->arg);
10591+}
10592+
10593+static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10594+{
10595+ int err, wkq_err;
10596+ struct dentry *h_dentry;
10597+ struct inode *h_inode;
5db017eb 10598+ struct mnt_idmap *h_idmap;
1facf9fc 10599+
5db017eb 10600+ h_idmap = au_sbr_idmap(dentry->d_sb, arg->bindex);
1facf9fc 10601+ h_dentry = au_h_dptr(dentry, arg->bindex);
5527c038 10602+ h_inode = d_inode(h_dentry);
53392da6 10603+ /* todo: i_mode changes anytime? */
be118d29 10604+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
5db017eb 10605+ err = au_test_h_perm_sio(h_idmap, h_inode, MAY_EXEC | MAY_READ);
3c1bdaff 10606+ inode_unlock_shared(h_inode);
1facf9fc 10607+ if (!err)
10608+ err = do_test_empty(dentry, arg);
10609+ else {
10610+ struct do_test_empty_args args = {
10611+ .errp = &err,
10612+ .dentry = dentry,
10613+ .arg = arg
10614+ };
10615+ unsigned int flags = arg->flags;
10616+
10617+ wkq_err = au_wkq_wait(call_do_test_empty, &args);
10618+ if (unlikely(wkq_err))
10619+ err = wkq_err;
10620+ arg->flags = flags;
10621+ }
10622+
10623+ return err;
10624+}
10625+
10626+int au_test_empty_lower(struct dentry *dentry)
10627+{
10628+ int err;
1308ab2a 10629+ unsigned int rdhash;
5afbbe0d 10630+ aufs_bindex_t bindex, btop, btail;
1308ab2a 10631+ struct au_nhash whlist;
392086de
AM
10632+ struct test_empty_arg arg = {
10633+ .ctx = {
2000de60 10634+ .actor = test_empty_cb
392086de
AM
10635+ }
10636+ };
076b876e 10637+ int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
1facf9fc 10638+
dece6358
AM
10639+ SiMustAnyLock(dentry->d_sb);
10640+
1308ab2a 10641+ rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10642+ if (!rdhash)
10643+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10644+ err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
dece6358 10645+ if (unlikely(err))
1facf9fc 10646+ goto out;
10647+
1facf9fc 10648+ arg.flags = 0;
1308ab2a 10649+ arg.whlist = &whlist;
5afbbe0d 10650+ btop = au_dbtop(dentry);
dece6358
AM
10651+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10652+ au_fset_testempty(arg.flags, SHWH);
076b876e
AM
10653+ test_empty = do_test_empty;
10654+ if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10655+ test_empty = sio_test_empty;
5afbbe0d 10656+ arg.bindex = btop;
076b876e 10657+ err = test_empty(dentry, &arg);
1facf9fc 10658+ if (unlikely(err))
10659+ goto out_whlist;
10660+
10661+ au_fset_testempty(arg.flags, WHONLY);
10662+ btail = au_dbtaildir(dentry);
5afbbe0d 10663+ for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
1facf9fc 10664+ struct dentry *h_dentry;
10665+
10666+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 10667+ if (h_dentry && d_is_positive(h_dentry)) {
1facf9fc 10668+ arg.bindex = bindex;
076b876e 10669+ err = test_empty(dentry, &arg);
1facf9fc 10670+ }
10671+ }
10672+
4f0767ce 10673+out_whlist:
1308ab2a 10674+ au_nhash_wh_free(&whlist);
4f0767ce 10675+out:
1facf9fc 10676+ return err;
10677+}
10678+
10679+int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10680+{
10681+ int err;
392086de
AM
10682+ struct test_empty_arg arg = {
10683+ .ctx = {
2000de60 10684+ .actor = test_empty_cb
392086de
AM
10685+ }
10686+ };
1facf9fc 10687+ aufs_bindex_t bindex, btail;
10688+
10689+ err = 0;
1308ab2a 10690+ arg.whlist = whlist;
1facf9fc 10691+ arg.flags = AuTestEmpty_WHONLY;
dece6358
AM
10692+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10693+ au_fset_testempty(arg.flags, SHWH);
1facf9fc 10694+ btail = au_dbtaildir(dentry);
5afbbe0d 10695+ for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
1facf9fc 10696+ struct dentry *h_dentry;
10697+
10698+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 10699+ if (h_dentry && d_is_positive(h_dentry)) {
1facf9fc 10700+ arg.bindex = bindex;
10701+ err = sio_test_empty(dentry, &arg);
10702+ }
10703+ }
10704+
10705+ return err;
10706+}
10707+
10708+/* ---------------------------------------------------------------------- */
10709+
10710+const struct file_operations aufs_dir_fop = {
4a4d8108 10711+ .owner = THIS_MODULE,
027c5e7a 10712+ .llseek = default_llseek,
1facf9fc 10713+ .read = generic_read_dir,
5afbbe0d 10714+ .iterate_shared = aufs_iterate_shared,
1facf9fc 10715+ .unlocked_ioctl = aufs_ioctl_dir,
b752ccd1
AM
10716+#ifdef CONFIG_COMPAT
10717+ .compat_ioctl = aufs_compat_ioctl_dir,
10718+#endif
1facf9fc 10719+ .open = aufs_open_dir,
10720+ .release = aufs_release_dir,
4a4d8108 10721+ .flush = aufs_flush_dir,
1facf9fc 10722+ .fsync = aufs_fsync_dir
10723+};
7f207e10 10724diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
eca34b5c 10725--- /usr/share/empty/fs/aufs/dir.h 1970-01-01 01:00:00.000000000 +0100
594d0238 10726+++ linux/fs/aufs/dir.h 2022-11-05 23:02:18.962555950 +0100
9f237c51 10727@@ -0,0 +1,134 @@
062440b3 10728+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 10729+/*
0dcfbb52 10730+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 10731+ *
0dcfbb52 10732+ * This program is free software; you can redistribute it and/or modify
1facf9fc 10733+ * it under the terms of the GNU General Public License as published by
10734+ * the Free Software Foundation; either version 2 of the License, or
10735+ * (at your option) any later version.
dece6358
AM
10736+ *
10737+ * This program is distributed in the hope that it will be useful,
10738+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10739+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10740+ * GNU General Public License for more details.
10741+ *
10742+ * You should have received a copy of the GNU General Public License
523b37e3 10743+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 10744+ */
10745+
10746+/*
10747+ * directory operations
10748+ */
10749+
10750+#ifndef __AUFS_DIR_H__
10751+#define __AUFS_DIR_H__
10752+
10753+#ifdef __KERNEL__
10754+
10755+#include <linux/fs.h>
1facf9fc 10756+
10757+/* ---------------------------------------------------------------------- */
10758+
10759+/* need to be faster and smaller */
10760+
10761+struct au_nhash {
dece6358
AM
10762+ unsigned int nh_num;
10763+ struct hlist_head *nh_head;
1facf9fc 10764+};
10765+
10766+struct au_vdir_destr {
10767+ unsigned char len;
42b5c33a 10768+ unsigned char name[];
1facf9fc 10769+} __packed;
10770+
10771+struct au_vdir_dehstr {
10772+ struct hlist_node hash;
1c60b727 10773+ struct au_vdir_destr *str;
9f237c51 10774+ struct rcu_head rcu;
4a4d8108 10775+} ____cacheline_aligned_in_smp;
1facf9fc 10776+
10777+struct au_vdir_de {
10778+ ino_t de_ino;
10779+ unsigned char de_type;
10780+ /* caution: packed */
10781+ struct au_vdir_destr de_str;
10782+} __packed;
10783+
10784+struct au_vdir_wh {
10785+ struct hlist_node wh_hash;
dece6358
AM
10786+#ifdef CONFIG_AUFS_SHWH
10787+ ino_t wh_ino;
1facf9fc 10788+ aufs_bindex_t wh_bindex;
dece6358
AM
10789+ unsigned char wh_type;
10790+#else
10791+ aufs_bindex_t wh_bindex;
10792+#endif
10793+ /* caution: packed */
1facf9fc 10794+ struct au_vdir_destr wh_str;
10795+} __packed;
10796+
10797+union au_vdir_deblk_p {
10798+ unsigned char *deblk;
10799+ struct au_vdir_de *de;
10800+};
10801+
10802+struct au_vdir {
10803+ unsigned char **vd_deblk;
10804+ unsigned long vd_nblk;
1facf9fc 10805+ struct {
10806+ unsigned long ul;
10807+ union au_vdir_deblk_p p;
10808+ } vd_last;
10809+
be118d29 10810+ u64 vd_version;
dece6358 10811+ unsigned int vd_deblk_sz;
9f237c51
AM
10812+ unsigned long vd_jiffy;
10813+ struct rcu_head rcu;
4a4d8108 10814+} ____cacheline_aligned_in_smp;
1facf9fc 10815+
10816+/* ---------------------------------------------------------------------- */
10817+
10818+/* dir.c */
10819+extern const struct file_operations aufs_dir_fop;
10820+void au_add_nlink(struct inode *dir, struct inode *h_dir);
10821+void au_sub_nlink(struct inode *dir, struct inode *h_dir);
1308ab2a 10822+loff_t au_dir_size(struct file *file, struct dentry *dentry);
b912730e 10823+void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
1facf9fc 10824+int au_test_empty_lower(struct dentry *dentry);
10825+int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10826+
10827+/* vdir.c */
1308ab2a 10828+unsigned int au_rdhash_est(loff_t sz);
dece6358
AM
10829+int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
10830+void au_nhash_wh_free(struct au_nhash *whlist);
1facf9fc 10831+int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
10832+ int limit);
dece6358
AM
10833+int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
10834+int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
10835+ unsigned int d_type, aufs_bindex_t bindex,
10836+ unsigned char shwh);
1c60b727 10837+void au_vdir_free(struct au_vdir *vdir);
1facf9fc 10838+int au_vdir_init(struct file *file);
392086de 10839+int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
1facf9fc 10840+
10841+/* ioctl.c */
10842+long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
10843+
1308ab2a 10844+#ifdef CONFIG_AUFS_RDU
10845+/* rdu.c */
10846+long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
b752ccd1
AM
10847+#ifdef CONFIG_COMPAT
10848+long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
10849+ unsigned long arg);
10850+#endif
1308ab2a 10851+#else
c1595e42
JR
10852+AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
10853+ unsigned int cmd, unsigned long arg)
b752ccd1 10854+#ifdef CONFIG_COMPAT
c1595e42
JR
10855+AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
10856+ unsigned int cmd, unsigned long arg)
b752ccd1 10857+#endif
1308ab2a 10858+#endif
10859+
1facf9fc 10860+#endif /* __KERNEL__ */
10861+#endif /* __AUFS_DIR_H__ */
8b6a4947 10862diff -urN /usr/share/empty/fs/aufs/dirren.c linux/fs/aufs/dirren.c
eca34b5c 10863--- /usr/share/empty/fs/aufs/dirren.c 1970-01-01 01:00:00.000000000 +0100
631230db 10864+++ linux/fs/aufs/dirren.c 2023-10-10 22:51:18.033248030 +0200
758e9dad 10865@@ -0,0 +1,1315 @@
cd7a4cd9 10866+// SPDX-License-Identifier: GPL-2.0
8b6a4947 10867+/*
0dcfbb52 10868+ * Copyright (C) 2017-2022 Junjiro R. Okajima
8b6a4947 10869+ *
0dcfbb52 10870+ * This program is free software; you can redistribute it and/or modify
8b6a4947
AM
10871+ * it under the terms of the GNU General Public License as published by
10872+ * the Free Software Foundation; either version 2 of the License, or
10873+ * (at your option) any later version.
10874+ *
10875+ * This program is distributed in the hope that it will be useful,
10876+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10877+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10878+ * GNU General Public License for more details.
10879+ *
10880+ * You should have received a copy of the GNU General Public License
10881+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
10882+ */
10883+
10884+/*
acd2b654 10885+ * special handling in renaming a directory
8b6a4947
AM
10886+ * in order to support looking-up the before-renamed name on the lower readonly
10887+ * branches
10888+ */
10889+
10890+#include <linux/byteorder/generic.h>
10891+#include "aufs.h"
10892+
10893+static void au_dr_hino_del(struct au_dr_br *dr, struct au_dr_hino *ent)
10894+{
10895+ int idx;
10896+
10897+ idx = au_dr_ihash(ent->dr_h_ino);
10898+ au_hbl_del(&ent->dr_hnode, dr->dr_h_ino + idx);
10899+}
10900+
10901+static int au_dr_hino_test_empty(struct au_dr_br *dr)
10902+{
10903+ int ret, i;
10904+ struct hlist_bl_head *hbl;
10905+
10906+ ret = 1;
10907+ for (i = 0; ret && i < AuDirren_NHASH; i++) {
10908+ hbl = dr->dr_h_ino + i;
10909+ hlist_bl_lock(hbl);
10910+ ret &= hlist_bl_empty(hbl);
10911+ hlist_bl_unlock(hbl);
10912+ }
10913+
10914+ return ret;
10915+}
10916+
10917+static struct au_dr_hino *au_dr_hino_find(struct au_dr_br *dr, ino_t ino)
10918+{
10919+ struct au_dr_hino *found, *ent;
10920+ struct hlist_bl_head *hbl;
10921+ struct hlist_bl_node *pos;
10922+ int idx;
10923+
10924+ found = NULL;
10925+ idx = au_dr_ihash(ino);
10926+ hbl = dr->dr_h_ino + idx;
10927+ hlist_bl_lock(hbl);
10928+ hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10929+ if (ent->dr_h_ino == ino) {
10930+ found = ent;
10931+ break;
10932+ }
10933+ hlist_bl_unlock(hbl);
10934+
10935+ return found;
10936+}
10937+
10938+int au_dr_hino_test_add(struct au_dr_br *dr, ino_t ino,
10939+ struct au_dr_hino *add_ent)
10940+{
10941+ int found, idx;
10942+ struct hlist_bl_head *hbl;
10943+ struct hlist_bl_node *pos;
10944+ struct au_dr_hino *ent;
10945+
10946+ found = 0;
10947+ idx = au_dr_ihash(ino);
10948+ hbl = dr->dr_h_ino + idx;
43982f53 10949+#if 0 /* debug print */
8b6a4947
AM
10950+ {
10951+ struct hlist_bl_node *tmp;
10952+
10953+ hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
10954+ AuDbg("hi%llu\n", (unsigned long long)ent->dr_h_ino);
10955+ }
10956+#endif
10957+ hlist_bl_lock(hbl);
10958+ hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10959+ if (ent->dr_h_ino == ino) {
10960+ found = 1;
10961+ break;
10962+ }
10963+ if (!found && add_ent)
10964+ hlist_bl_add_head(&add_ent->dr_hnode, hbl);
10965+ hlist_bl_unlock(hbl);
10966+
10967+ if (!found && add_ent)
10968+ AuDbg("i%llu added\n", (unsigned long long)add_ent->dr_h_ino);
10969+
10970+ return found;
10971+}
10972+
10973+void au_dr_hino_free(struct au_dr_br *dr)
10974+{
10975+ int i;
10976+ struct hlist_bl_head *hbl;
10977+ struct hlist_bl_node *pos, *tmp;
10978+ struct au_dr_hino *ent;
10979+
10980+ /* SiMustWriteLock(sb); */
10981+
10982+ for (i = 0; i < AuDirren_NHASH; i++) {
10983+ hbl = dr->dr_h_ino + i;
10984+ /* no spinlock since sbinfo must be write-locked */
10985+ hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
9f237c51 10986+ au_kfree_rcu(ent);
8b6a4947
AM
10987+ INIT_HLIST_BL_HEAD(hbl);
10988+ }
10989+}
10990+
10991+/* returns the number of inodes or an error */
10992+static int au_dr_hino_store(struct super_block *sb, struct au_branch *br,
10993+ struct file *hinofile)
10994+{
10995+ int err, i;
10996+ ssize_t ssz;
10997+ loff_t pos, oldsize;
10998+ __be64 u64;
10999+ struct inode *hinoinode;
11000+ struct hlist_bl_head *hbl;
11001+ struct hlist_bl_node *n1, *n2;
11002+ struct au_dr_hino *ent;
11003+
11004+ SiMustWriteLock(sb);
11005+ AuDebugOn(!au_br_writable(br->br_perm));
11006+
11007+ hinoinode = file_inode(hinofile);
11008+ oldsize = i_size_read(hinoinode);
11009+
11010+ err = 0;
11011+ pos = 0;
11012+ hbl = br->br_dirren.dr_h_ino;
11013+ for (i = 0; !err && i < AuDirren_NHASH; i++, hbl++) {
11014+ /* no bit-lock since sbinfo must be write-locked */
11015+ hlist_bl_for_each_entry_safe(ent, n1, n2, hbl, dr_hnode) {
11016+ AuDbg("hi%llu, %pD2\n",
11017+ (unsigned long long)ent->dr_h_ino, hinofile);
11018+ u64 = cpu_to_be64(ent->dr_h_ino);
11019+ ssz = vfsub_write_k(hinofile, &u64, sizeof(u64), &pos);
11020+ if (ssz == sizeof(u64))
11021+ continue;
11022+
11023+ /* write error */
11024+ pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11025+ err = -ENOSPC;
11026+ if (ssz < 0)
11027+ err = ssz;
11028+ break;
11029+ }
11030+ }
11031+ /* regardless the error */
11032+ if (pos < oldsize) {
11033+ err = vfsub_trunc(&hinofile->f_path, pos, /*attr*/0, hinofile);
11034+ AuTraceErr(err);
11035+ }
11036+
11037+ AuTraceErr(err);
11038+ return err;
11039+}
11040+
11041+static int au_dr_hino_load(struct au_dr_br *dr, struct file *hinofile)
11042+{
11043+ int err, hidx;
11044+ ssize_t ssz;
11045+ size_t sz, n;
11046+ loff_t pos;
11047+ uint64_t u64;
11048+ struct au_dr_hino *ent;
11049+ struct inode *hinoinode;
11050+ struct hlist_bl_head *hbl;
11051+
11052+ err = 0;
11053+ pos = 0;
11054+ hbl = dr->dr_h_ino;
11055+ hinoinode = file_inode(hinofile);
11056+ sz = i_size_read(hinoinode);
11057+ AuDebugOn(sz % sizeof(u64));
11058+ n = sz / sizeof(u64);
11059+ while (n--) {
11060+ ssz = vfsub_read_k(hinofile, &u64, sizeof(u64), &pos);
11061+ if (unlikely(ssz != sizeof(u64))) {
11062+ pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11063+ err = -EINVAL;
11064+ if (ssz < 0)
11065+ err = ssz;
11066+ goto out_free;
11067+ }
11068+
11069+ ent = kmalloc(sizeof(*ent), GFP_NOFS);
11070+ if (!ent) {
11071+ err = -ENOMEM;
11072+ AuTraceErr(err);
11073+ goto out_free;
11074+ }
11075+ ent->dr_h_ino = be64_to_cpu((__force __be64)u64);
11076+ AuDbg("hi%llu, %pD2\n",
11077+ (unsigned long long)ent->dr_h_ino, hinofile);
11078+ hidx = au_dr_ihash(ent->dr_h_ino);
11079+ au_hbl_add(&ent->dr_hnode, hbl + hidx);
11080+ }
11081+ goto out; /* success */
11082+
11083+out_free:
11084+ au_dr_hino_free(dr);
11085+out:
11086+ AuTraceErr(err);
11087+ return err;
11088+}
11089+
11090+/*
11091+ * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
11092+ * @path is a switch to distinguish load and store.
11093+ */
11094+static int au_dr_hino(struct super_block *sb, aufs_bindex_t bindex,
11095+ struct au_branch *br, const struct path *path)
11096+{
11097+ int err, flags;
11098+ unsigned char load, suspend;
11099+ struct file *hinofile;
11100+ struct au_hinode *hdir;
11101+ struct inode *dir, *delegated;
11102+ struct path hinopath;
11103+ struct qstr hinoname = QSTR_INIT(AUFS_WH_DR_BRHINO,
11104+ sizeof(AUFS_WH_DR_BRHINO) - 1);
11105+
11106+ AuDebugOn(bindex < 0 && !br);
11107+ AuDebugOn(bindex >= 0 && br);
11108+
11109+ err = -EINVAL;
11110+ suspend = !br;
11111+ if (suspend)
11112+ br = au_sbr(sb, bindex);
11113+ load = !!path;
11114+ if (!load) {
11115+ path = &br->br_path;
11116+ AuDebugOn(!au_br_writable(br->br_perm));
11117+ if (unlikely(!au_br_writable(br->br_perm)))
11118+ goto out;
11119+ }
11120+
11121+ hdir = NULL;
11122+ if (suspend) {
11123+ dir = d_inode(sb->s_root);
11124+ hdir = au_hinode(au_ii(dir), bindex);
11125+ dir = hdir->hi_inode;
11126+ au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11127+ } else {
11128+ dir = d_inode(path->dentry);
11129+ inode_lock_nested(dir, AuLsc_I_CHILD);
11130+ }
758e9dad
AM
11131+ hinopath.mnt = path->mnt;
11132+ hinopath.dentry = vfsub_lkup_one(&hinoname, (struct path *)path);
8b6a4947
AM
11133+ err = PTR_ERR(hinopath.dentry);
11134+ if (IS_ERR(hinopath.dentry))
11135+ goto out_unlock;
11136+
11137+ err = 0;
11138+ flags = O_RDONLY;
11139+ if (load) {
11140+ if (d_is_negative(hinopath.dentry))
11141+ goto out_dput; /* success */
11142+ } else {
11143+ if (au_dr_hino_test_empty(&br->br_dirren)) {
11144+ if (d_is_positive(hinopath.dentry)) {
11145+ delegated = NULL;
11146+ err = vfsub_unlink(dir, &hinopath, &delegated,
11147+ /*force*/0);
11148+ AuTraceErr(err);
11149+ if (unlikely(err))
11150+ pr_err("ignored err %d, %pd2\n",
11151+ err, hinopath.dentry);
11152+ if (unlikely(err == -EWOULDBLOCK))
11153+ iput(delegated);
11154+ err = 0;
11155+ }
11156+ goto out_dput;
11157+ } else if (!d_is_positive(hinopath.dentry)) {
11158+ err = vfsub_create(dir, &hinopath, 0600,
11159+ /*want_excl*/false);
11160+ AuTraceErr(err);
11161+ if (unlikely(err))
11162+ goto out_dput;
11163+ }
11164+ flags = O_WRONLY;
11165+ }
8b6a4947
AM
11166+ hinofile = vfsub_dentry_open(&hinopath, flags);
11167+ if (suspend)
11168+ au_hn_inode_unlock(hdir);
11169+ else
11170+ inode_unlock(dir);
11171+ dput(hinopath.dentry);
11172+ AuTraceErrPtr(hinofile);
11173+ if (IS_ERR(hinofile)) {
11174+ err = PTR_ERR(hinofile);
11175+ goto out;
11176+ }
11177+
11178+ if (load)
11179+ err = au_dr_hino_load(&br->br_dirren, hinofile);
11180+ else
11181+ err = au_dr_hino_store(sb, br, hinofile);
11182+ fput(hinofile);
11183+ goto out;
11184+
11185+out_dput:
11186+ dput(hinopath.dentry);
11187+out_unlock:
11188+ if (suspend)
11189+ au_hn_inode_unlock(hdir);
11190+ else
11191+ inode_unlock(dir);
11192+out:
11193+ AuTraceErr(err);
11194+ return err;
11195+}
11196+
11197+/* ---------------------------------------------------------------------- */
11198+
11199+static int au_dr_brid_init(struct au_dr_brid *brid, const struct path *path)
11200+{
11201+ int err;
11202+ struct kstatfs kstfs;
11203+ dev_t dev;
11204+ struct dentry *dentry;
11205+ struct super_block *sb;
11206+
11207+ err = vfs_statfs((void *)path, &kstfs);
11208+ AuTraceErr(err);
11209+ if (unlikely(err))
11210+ goto out;
11211+
11212+ /* todo: support for UUID */
11213+
11214+ if (kstfs.f_fsid.val[0] || kstfs.f_fsid.val[1]) {
11215+ brid->type = AuBrid_FSID;
11216+ brid->fsid = kstfs.f_fsid;
11217+ } else {
11218+ dentry = path->dentry;
11219+ sb = dentry->d_sb;
11220+ dev = sb->s_dev;
11221+ if (dev) {
11222+ brid->type = AuBrid_DEV;
11223+ brid->dev = dev;
11224+ }
11225+ }
11226+
11227+out:
11228+ return err;
11229+}
11230+
11231+int au_dr_br_init(struct super_block *sb, struct au_branch *br,
11232+ const struct path *path)
11233+{
11234+ int err, i;
11235+ struct au_dr_br *dr;
11236+ struct hlist_bl_head *hbl;
11237+
11238+ dr = &br->br_dirren;
11239+ hbl = dr->dr_h_ino;
11240+ for (i = 0; i < AuDirren_NHASH; i++, hbl++)
11241+ INIT_HLIST_BL_HEAD(hbl);
11242+
11243+ err = au_dr_brid_init(&dr->dr_brid, path);
11244+ if (unlikely(err))
11245+ goto out;
11246+
11247+ if (au_opt_test(au_mntflags(sb), DIRREN))
11248+ err = au_dr_hino(sb, /*bindex*/-1, br, path);
11249+
11250+out:
11251+ AuTraceErr(err);
11252+ return err;
11253+}
11254+
11255+int au_dr_br_fin(struct super_block *sb, struct au_branch *br)
11256+{
11257+ int err;
11258+
11259+ err = 0;
11260+ if (au_br_writable(br->br_perm))
11261+ err = au_dr_hino(sb, /*bindex*/-1, br, /*path*/NULL);
11262+ if (!err)
11263+ au_dr_hino_free(&br->br_dirren);
11264+
11265+ return err;
11266+}
11267+
11268+/* ---------------------------------------------------------------------- */
11269+
11270+static int au_brid_str(struct au_dr_brid *brid, struct inode *h_inode,
11271+ char *buf, size_t sz)
11272+{
11273+ int err;
11274+ unsigned int major, minor;
11275+ char *p;
11276+
11277+ p = buf;
11278+ err = snprintf(p, sz, "%d_", brid->type);
11279+ AuDebugOn(err > sz);
11280+ p += err;
11281+ sz -= err;
11282+ switch (brid->type) {
11283+ case AuBrid_Unset:
11284+ return -EINVAL;
11285+ case AuBrid_UUID:
11286+ err = snprintf(p, sz, "%pU", brid->uuid.b);
11287+ break;
11288+ case AuBrid_FSID:
11289+ err = snprintf(p, sz, "%08x-%08x",
11290+ brid->fsid.val[0], brid->fsid.val[1]);
11291+ break;
11292+ case AuBrid_DEV:
11293+ major = MAJOR(brid->dev);
11294+ minor = MINOR(brid->dev);
11295+ if (major <= 0xff && minor <= 0xff)
11296+ err = snprintf(p, sz, "%02x%02x", major, minor);
11297+ else
11298+ err = snprintf(p, sz, "%03x:%05x", major, minor);
11299+ break;
11300+ }
11301+ AuDebugOn(err > sz);
11302+ p += err;
11303+ sz -= err;
11304+ err = snprintf(p, sz, "_%llu", (unsigned long long)h_inode->i_ino);
11305+ AuDebugOn(err > sz);
11306+ p += err;
11307+ sz -= err;
11308+
11309+ return p - buf;
11310+}
11311+
11312+static int au_drinfo_name(struct au_branch *br, char *name, int len)
11313+{
11314+ int rlen;
11315+ struct dentry *br_dentry;
11316+ struct inode *br_inode;
11317+
11318+ br_dentry = au_br_dentry(br);
11319+ br_inode = d_inode(br_dentry);
11320+ rlen = au_brid_str(&br->br_dirren.dr_brid, br_inode, name, len);
11321+ AuDebugOn(rlen >= AUFS_DIRREN_ENV_VAL_SZ);
11322+ AuDebugOn(rlen > len);
11323+
11324+ return rlen;
11325+}
11326+
11327+/* ---------------------------------------------------------------------- */
11328+
11329+/*
11330+ * from the given @h_dentry, construct drinfo at @*fdata.
11331+ * when the size of @*fdata is not enough, reallocate and return new @fdata and
11332+ * @allocated.
11333+ */
11334+static int au_drinfo_construct(struct au_drinfo_fdata **fdata,
11335+ struct dentry *h_dentry,
11336+ unsigned char *allocated)
11337+{
11338+ int err, v;
11339+ struct au_drinfo_fdata *f, *p;
11340+ struct au_drinfo *drinfo;
11341+ struct inode *h_inode;
11342+ struct qstr *qname;
11343+
11344+ err = 0;
11345+ f = *fdata;
11346+ h_inode = d_inode(h_dentry);
11347+ qname = &h_dentry->d_name;
11348+ drinfo = &f->drinfo;
11349+ drinfo->ino = (__force uint64_t)cpu_to_be64(h_inode->i_ino);
11350+ drinfo->oldnamelen = qname->len;
11351+ if (*allocated < sizeof(*f) + qname->len) {
11352+ v = roundup_pow_of_two(*allocated + qname->len);
11353+ p = au_krealloc(f, v, GFP_NOFS, /*may_shrink*/0);
11354+ if (unlikely(!p)) {
11355+ err = -ENOMEM;
11356+ AuTraceErr(err);
11357+ goto out;
11358+ }
11359+ f = p;
11360+ *fdata = f;
11361+ *allocated = v;
11362+ drinfo = &f->drinfo;
11363+ }
11364+ memcpy(drinfo->oldname, qname->name, qname->len);
11365+ AuDbg("i%llu, %.*s\n",
11366+ be64_to_cpu((__force __be64)drinfo->ino), drinfo->oldnamelen,
11367+ drinfo->oldname);
11368+
11369+out:
11370+ AuTraceErr(err);
11371+ return err;
11372+}
11373+
11374+/* callers have to free the return value */
11375+static struct au_drinfo *au_drinfo_read_k(struct file *file, ino_t h_ino)
11376+{
11377+ struct au_drinfo *ret, *drinfo;
11378+ struct au_drinfo_fdata fdata;
11379+ int len;
11380+ loff_t pos;
11381+ ssize_t ssz;
11382+
11383+ ret = ERR_PTR(-EIO);
11384+ pos = 0;
11385+ ssz = vfsub_read_k(file, &fdata, sizeof(fdata), &pos);
11386+ if (unlikely(ssz != sizeof(fdata))) {
11387+ AuIOErr("ssz %zd, %u, %pD2\n",
11388+ ssz, (unsigned int)sizeof(fdata), file);
11389+ goto out;
11390+ }
11391+
11392+ fdata.magic = ntohl((__force __be32)fdata.magic);
11393+ switch (fdata.magic) {
11394+ case AUFS_DRINFO_MAGIC_V1:
11395+ break;
11396+ default:
11397+ AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
11398+ fdata.magic, AUFS_DRINFO_MAGIC_V1, file);
11399+ goto out;
11400+ }
11401+
11402+ drinfo = &fdata.drinfo;
11403+ len = drinfo->oldnamelen;
11404+ if (!len) {
11405+ AuIOErr("broken drinfo %pD2\n", file);
11406+ goto out;
11407+ }
11408+
11409+ ret = NULL;
11410+ drinfo->ino = be64_to_cpu((__force __be64)drinfo->ino);
11411+ if (unlikely(h_ino && drinfo->ino != h_ino)) {
11412+ AuDbg("ignored i%llu, i%llu, %pD2\n",
11413+ (unsigned long long)drinfo->ino,
11414+ (unsigned long long)h_ino, file);
11415+ goto out; /* success */
11416+ }
11417+
11418+ ret = kmalloc(sizeof(*ret) + len, GFP_NOFS);
11419+ if (unlikely(!ret)) {
11420+ ret = ERR_PTR(-ENOMEM);
11421+ AuTraceErrPtr(ret);
11422+ goto out;
11423+ }
11424+
11425+ *ret = *drinfo;
11426+ ssz = vfsub_read_k(file, (void *)ret->oldname, len, &pos);
11427+ if (unlikely(ssz != len)) {
9f237c51 11428+ au_kfree_rcu(ret);
8b6a4947
AM
11429+ ret = ERR_PTR(-EIO);
11430+ AuIOErr("ssz %zd, %u, %pD2\n", ssz, len, file);
11431+ goto out;
11432+ }
11433+
11434+ AuDbg("oldname %.*s\n", ret->oldnamelen, ret->oldname);
11435+
11436+out:
11437+ return ret;
11438+}
11439+
11440+/* ---------------------------------------------------------------------- */
11441+
11442+/* in order to be revertible */
11443+struct au_drinfo_rev_elm {
11444+ int created;
11445+ struct dentry *info_dentry;
11446+ struct au_drinfo *info_last;
11447+};
11448+
11449+struct au_drinfo_rev {
11450+ unsigned char already;
11451+ aufs_bindex_t nelm;
42b5c33a 11452+ struct au_drinfo_rev_elm elm[];
8b6a4947
AM
11453+};
11454+
11455+/* todo: isn't it too large? */
11456+struct au_drinfo_store {
11457+ struct path h_ppath;
11458+ struct dentry *h_dentry;
11459+ struct au_drinfo_fdata *fdata;
11460+ char *infoname; /* inside of whname, just after PFX */
11461+ char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ];
11462+ aufs_bindex_t btgt, btail;
11463+ unsigned char no_sio,
11464+ allocated, /* current size of *fdata */
11465+ infonamelen, /* room size for p */
acd2b654 11466+ whnamelen, /* length of the generated name */
8b6a4947
AM
11467+ renameback; /* renamed back */
11468+};
11469+
11470+/* on rename(2) error, the caller should revert it using @elm */
11471+static int au_drinfo_do_store(struct au_drinfo_store *w,
11472+ struct au_drinfo_rev_elm *elm)
11473+{
11474+ int err, len;
11475+ ssize_t ssz;
11476+ loff_t pos;
11477+ struct path infopath = {
11478+ .mnt = w->h_ppath.mnt
11479+ };
11480+ struct inode *h_dir, *h_inode, *delegated;
11481+ struct file *infofile;
11482+ struct qstr *qname;
11483+
11484+ AuDebugOn(elm
11485+ && memcmp(elm, page_address(ZERO_PAGE(0)), sizeof(*elm)));
11486+
758e9dad 11487+ infopath.dentry = vfsub_lookup_one_len(w->whname, &w->h_ppath,
8b6a4947
AM
11488+ w->whnamelen);
11489+ AuTraceErrPtr(infopath.dentry);
11490+ if (IS_ERR(infopath.dentry)) {
11491+ err = PTR_ERR(infopath.dentry);
11492+ goto out;
11493+ }
11494+
11495+ err = 0;
11496+ h_dir = d_inode(w->h_ppath.dentry);
11497+ if (elm && d_is_negative(infopath.dentry)) {
11498+ err = vfsub_create(h_dir, &infopath, 0600, /*want_excl*/true);
11499+ AuTraceErr(err);
11500+ if (unlikely(err))
11501+ goto out_dput;
11502+ elm->created = 1;
11503+ elm->info_dentry = dget(infopath.dentry);
11504+ }
11505+
11506+ infofile = vfsub_dentry_open(&infopath, O_RDWR);
11507+ AuTraceErrPtr(infofile);
11508+ if (IS_ERR(infofile)) {
11509+ err = PTR_ERR(infofile);
11510+ goto out_dput;
11511+ }
11512+
11513+ h_inode = d_inode(infopath.dentry);
11514+ if (elm && i_size_read(h_inode)) {
11515+ h_inode = d_inode(w->h_dentry);
11516+ elm->info_last = au_drinfo_read_k(infofile, h_inode->i_ino);
11517+ AuTraceErrPtr(elm->info_last);
11518+ if (IS_ERR(elm->info_last)) {
11519+ err = PTR_ERR(elm->info_last);
11520+ elm->info_last = NULL;
11521+ AuDebugOn(elm->info_dentry);
11522+ goto out_fput;
11523+ }
11524+ }
11525+
11526+ if (elm && w->renameback) {
11527+ delegated = NULL;
11528+ err = vfsub_unlink(h_dir, &infopath, &delegated, /*force*/0);
11529+ AuTraceErr(err);
11530+ if (unlikely(err == -EWOULDBLOCK))
11531+ iput(delegated);
11532+ goto out_fput;
11533+ }
11534+
11535+ pos = 0;
11536+ qname = &w->h_dentry->d_name;
11537+ len = sizeof(*w->fdata) + qname->len;
11538+ if (!elm)
11539+ len = sizeof(*w->fdata) + w->fdata->drinfo.oldnamelen;
11540+ ssz = vfsub_write_k(infofile, w->fdata, len, &pos);
11541+ if (ssz == len) {
11542+ AuDbg("hi%llu, %.*s\n", w->fdata->drinfo.ino,
11543+ w->fdata->drinfo.oldnamelen, w->fdata->drinfo.oldname);
11544+ goto out_fput; /* success */
11545+ } else {
11546+ err = -EIO;
11547+ if (ssz < 0)
11548+ err = ssz;
11549+ /* the caller should revert it using @elm */
11550+ }
11551+
11552+out_fput:
11553+ fput(infofile);
11554+out_dput:
11555+ dput(infopath.dentry);
11556+out:
11557+ AuTraceErr(err);
11558+ return err;
11559+}
11560+
11561+struct au_call_drinfo_do_store_args {
11562+ int *errp;
11563+ struct au_drinfo_store *w;
11564+ struct au_drinfo_rev_elm *elm;
11565+};
11566+
11567+static void au_call_drinfo_do_store(void *args)
11568+{
11569+ struct au_call_drinfo_do_store_args *a = args;
11570+
11571+ *a->errp = au_drinfo_do_store(a->w, a->elm);
11572+}
11573+
11574+static int au_drinfo_store_sio(struct au_drinfo_store *w,
11575+ struct au_drinfo_rev_elm *elm)
11576+{
11577+ int err, wkq_err;
11578+
11579+ if (w->no_sio)
11580+ err = au_drinfo_do_store(w, elm);
11581+ else {
11582+ struct au_call_drinfo_do_store_args a = {
11583+ .errp = &err,
11584+ .w = w,
11585+ .elm = elm
11586+ };
11587+ wkq_err = au_wkq_wait(au_call_drinfo_do_store, &a);
11588+ if (unlikely(wkq_err))
11589+ err = wkq_err;
11590+ }
11591+ AuTraceErr(err);
11592+
11593+ return err;
11594+}
11595+
11596+static int au_drinfo_store_work_init(struct au_drinfo_store *w,
11597+ aufs_bindex_t btgt)
11598+{
11599+ int err;
11600+
11601+ memset(w, 0, sizeof(*w));
11602+ w->allocated = roundup_pow_of_two(sizeof(*w->fdata) + 40);
631230db 11603+ strscpy(w->whname, AUFS_WH_DR_INFO_PFX, sizeof(AUFS_WH_DR_INFO_PFX));
8b6a4947
AM
11604+ w->infoname = w->whname + sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11605+ w->infonamelen = sizeof(w->whname) - sizeof(AUFS_WH_DR_INFO_PFX);
11606+ w->btgt = btgt;
11607+ w->no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
11608+
11609+ err = -ENOMEM;
11610+ w->fdata = kcalloc(1, w->allocated, GFP_NOFS);
11611+ if (unlikely(!w->fdata)) {
11612+ AuTraceErr(err);
11613+ goto out;
11614+ }
11615+ w->fdata->magic = (__force uint32_t)htonl(AUFS_DRINFO_MAGIC_V1);
11616+ err = 0;
11617+
11618+out:
11619+ return err;
11620+}
11621+
11622+static void au_drinfo_store_work_fin(struct au_drinfo_store *w)
11623+{
9f237c51 11624+ au_kfree_rcu(w->fdata);
8b6a4947
AM
11625+}
11626+
11627+static void au_drinfo_store_rev(struct au_drinfo_rev *rev,
11628+ struct au_drinfo_store *w)
11629+{
11630+ struct au_drinfo_rev_elm *elm;
11631+ struct inode *h_dir, *delegated;
11632+ int err, nelm;
11633+ struct path infopath = {
11634+ .mnt = w->h_ppath.mnt
11635+ };
11636+
11637+ h_dir = d_inode(w->h_ppath.dentry);
11638+ IMustLock(h_dir);
11639+
11640+ err = 0;
11641+ elm = rev->elm;
11642+ for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11643+ AuDebugOn(elm->created && elm->info_last);
11644+ if (elm->created) {
11645+ AuDbg("here\n");
11646+ delegated = NULL;
11647+ infopath.dentry = elm->info_dentry;
11648+ err = vfsub_unlink(h_dir, &infopath, &delegated,
11649+ !w->no_sio);
11650+ AuTraceErr(err);
11651+ if (unlikely(err == -EWOULDBLOCK))
11652+ iput(delegated);
11653+ dput(elm->info_dentry);
11654+ } else if (elm->info_last) {
11655+ AuDbg("here\n");
11656+ w->fdata->drinfo = *elm->info_last;
11657+ memcpy(w->fdata->drinfo.oldname,
11658+ elm->info_last->oldname,
11659+ elm->info_last->oldnamelen);
11660+ err = au_drinfo_store_sio(w, /*elm*/NULL);
9f237c51 11661+ au_kfree_rcu(elm->info_last);
8b6a4947
AM
11662+ }
11663+ if (unlikely(err))
11664+ AuIOErr("%d, %s\n", err, w->whname);
11665+ /* go on even if err */
11666+ }
11667+}
11668+
11669+/* caller has to call au_dr_rename_fin() later */
11670+static int au_drinfo_store(struct dentry *dentry, aufs_bindex_t btgt,
11671+ struct qstr *dst_name, void *_rev)
11672+{
11673+ int err, sz, nelm;
11674+ aufs_bindex_t bindex, btail;
11675+ struct au_drinfo_store work;
11676+ struct au_drinfo_rev *rev, **p;
11677+ struct au_drinfo_rev_elm *elm;
11678+ struct super_block *sb;
11679+ struct au_branch *br;
11680+ struct au_hinode *hdir;
11681+
11682+ err = au_drinfo_store_work_init(&work, btgt);
11683+ AuTraceErr(err);
11684+ if (unlikely(err))
11685+ goto out;
11686+
11687+ err = -ENOMEM;
11688+ btail = au_dbtaildir(dentry);
11689+ nelm = btail - btgt;
11690+ sz = sizeof(*rev) + sizeof(*elm) * nelm;
11691+ rev = kcalloc(1, sz, GFP_NOFS);
11692+ if (unlikely(!rev)) {
11693+ AuTraceErr(err);
11694+ goto out_args;
11695+ }
11696+ rev->nelm = nelm;
11697+ elm = rev->elm;
11698+ p = _rev;
11699+ *p = rev;
11700+
11701+ err = 0;
11702+ sb = dentry->d_sb;
11703+ work.h_ppath.dentry = au_h_dptr(dentry, btgt);
11704+ work.h_ppath.mnt = au_sbr_mnt(sb, btgt);
11705+ hdir = au_hi(d_inode(dentry), btgt);
11706+ au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11707+ for (bindex = btgt + 1; bindex <= btail; bindex++, elm++) {
11708+ work.h_dentry = au_h_dptr(dentry, bindex);
11709+ if (!work.h_dentry)
11710+ continue;
11711+
11712+ err = au_drinfo_construct(&work.fdata, work.h_dentry,
11713+ &work.allocated);
11714+ AuTraceErr(err);
11715+ if (unlikely(err))
11716+ break;
11717+
11718+ work.renameback = au_qstreq(&work.h_dentry->d_name, dst_name);
11719+ br = au_sbr(sb, bindex);
11720+ work.whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11721+ work.whnamelen += au_drinfo_name(br, work.infoname,
11722+ work.infonamelen);
11723+ AuDbg("whname %.*s, i%llu, %.*s\n",
11724+ work.whnamelen, work.whname,
11725+ be64_to_cpu((__force __be64)work.fdata->drinfo.ino),
11726+ work.fdata->drinfo.oldnamelen,
11727+ work.fdata->drinfo.oldname);
11728+
11729+ err = au_drinfo_store_sio(&work, elm);
11730+ AuTraceErr(err);
11731+ if (unlikely(err))
11732+ break;
11733+ }
11734+ if (unlikely(err)) {
11735+ /* revert all drinfo */
11736+ au_drinfo_store_rev(rev, &work);
9f237c51 11737+ au_kfree_try_rcu(rev);
8b6a4947
AM
11738+ *p = NULL;
11739+ }
11740+ au_hn_inode_unlock(hdir);
11741+
11742+out_args:
11743+ au_drinfo_store_work_fin(&work);
11744+out:
11745+ return err;
11746+}
11747+
11748+/* ---------------------------------------------------------------------- */
11749+
11750+int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
11751+ struct qstr *dst_name, void *_rev)
11752+{
11753+ int err, already;
11754+ ino_t ino;
11755+ struct super_block *sb;
11756+ struct au_branch *br;
11757+ struct au_dr_br *dr;
11758+ struct dentry *h_dentry;
11759+ struct inode *h_inode;
11760+ struct au_dr_hino *ent;
11761+ struct au_drinfo_rev *rev, **p;
11762+
11763+ AuDbg("bindex %d\n", bindex);
11764+
11765+ err = -ENOMEM;
11766+ ent = kmalloc(sizeof(*ent), GFP_NOFS);
11767+ if (unlikely(!ent))
11768+ goto out;
11769+
11770+ sb = src->d_sb;
11771+ br = au_sbr(sb, bindex);
11772+ dr = &br->br_dirren;
11773+ h_dentry = au_h_dptr(src, bindex);
11774+ h_inode = d_inode(h_dentry);
11775+ ino = h_inode->i_ino;
11776+ ent->dr_h_ino = ino;
11777+ already = au_dr_hino_test_add(dr, ino, ent);
11778+ AuDbg("b%d, hi%llu, already %d\n",
11779+ bindex, (unsigned long long)ino, already);
11780+
11781+ err = au_drinfo_store(src, bindex, dst_name, _rev);
11782+ AuTraceErr(err);
11783+ if (!err) {
11784+ p = _rev;
11785+ rev = *p;
11786+ rev->already = already;
11787+ goto out; /* success */
11788+ }
11789+
11790+ /* revert */
11791+ if (!already)
11792+ au_dr_hino_del(dr, ent);
9f237c51 11793+ au_kfree_rcu(ent);
8b6a4947
AM
11794+
11795+out:
11796+ AuTraceErr(err);
11797+ return err;
11798+}
11799+
11800+void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11801+{
11802+ struct au_drinfo_rev *rev;
11803+ struct au_drinfo_rev_elm *elm;
11804+ int nelm;
11805+
11806+ rev = _rev;
11807+ elm = rev->elm;
11808+ for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11809+ dput(elm->info_dentry);
9f237c51 11810+ au_kfree_rcu(elm->info_last);
8b6a4947 11811+ }
9f237c51 11812+ au_kfree_try_rcu(rev);
8b6a4947
AM
11813+}
11814+
11815+void au_dr_rename_rev(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11816+{
11817+ int err;
11818+ struct au_drinfo_store work;
11819+ struct au_drinfo_rev *rev = _rev;
11820+ struct super_block *sb;
11821+ struct au_branch *br;
11822+ struct inode *h_inode;
11823+ struct au_dr_br *dr;
11824+ struct au_dr_hino *ent;
11825+
11826+ err = au_drinfo_store_work_init(&work, btgt);
11827+ if (unlikely(err))
11828+ goto out;
11829+
11830+ sb = src->d_sb;
11831+ br = au_sbr(sb, btgt);
11832+ work.h_ppath.dentry = au_h_dptr(src, btgt);
11833+ work.h_ppath.mnt = au_br_mnt(br);
11834+ au_drinfo_store_rev(rev, &work);
11835+ au_drinfo_store_work_fin(&work);
11836+ if (rev->already)
11837+ goto out;
11838+
11839+ dr = &br->br_dirren;
11840+ h_inode = d_inode(work.h_ppath.dentry);
11841+ ent = au_dr_hino_find(dr, h_inode->i_ino);
11842+ BUG_ON(!ent);
11843+ au_dr_hino_del(dr, ent);
9f237c51 11844+ au_kfree_rcu(ent);
8b6a4947
AM
11845+
11846+out:
9f237c51 11847+ au_kfree_try_rcu(rev);
8b6a4947
AM
11848+ if (unlikely(err))
11849+ pr_err("failed to remove dirren info\n");
11850+}
11851+
11852+/* ---------------------------------------------------------------------- */
11853+
11854+static struct au_drinfo *au_drinfo_do_load(struct path *h_ppath,
11855+ char *whname, int whnamelen,
11856+ struct dentry **info_dentry)
11857+{
11858+ struct au_drinfo *drinfo;
11859+ struct file *f;
11860+ struct inode *h_dir;
11861+ struct path infopath;
11862+ int unlocked;
11863+
11864+ AuDbg("%pd/%.*s\n", h_ppath->dentry, whnamelen, whname);
11865+
11866+ *info_dentry = NULL;
11867+ drinfo = NULL;
11868+ unlocked = 0;
11869+ h_dir = d_inode(h_ppath->dentry);
be118d29 11870+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
758e9dad 11871+ infopath.dentry = vfsub_lookup_one_len(whname, h_ppath, whnamelen);
8b6a4947
AM
11872+ if (IS_ERR(infopath.dentry)) {
11873+ drinfo = (void *)infopath.dentry;
11874+ goto out;
11875+ }
11876+
11877+ if (d_is_negative(infopath.dentry))
11878+ goto out_dput; /* success */
11879+
11880+ infopath.mnt = h_ppath->mnt;
11881+ f = vfsub_dentry_open(&infopath, O_RDONLY);
11882+ inode_unlock_shared(h_dir);
11883+ unlocked = 1;
11884+ if (IS_ERR(f)) {
11885+ drinfo = (void *)f;
11886+ goto out_dput;
11887+ }
11888+
11889+ drinfo = au_drinfo_read_k(f, /*h_ino*/0);
11890+ if (IS_ERR_OR_NULL(drinfo))
11891+ goto out_fput;
11892+
11893+ AuDbg("oldname %.*s\n", drinfo->oldnamelen, drinfo->oldname);
11894+ *info_dentry = dget(infopath.dentry); /* keep it alive */
11895+
11896+out_fput:
11897+ fput(f);
11898+out_dput:
11899+ dput(infopath.dentry);
11900+out:
11901+ if (!unlocked)
11902+ inode_unlock_shared(h_dir);
11903+ AuTraceErrPtr(drinfo);
11904+ return drinfo;
11905+}
11906+
11907+struct au_drinfo_do_load_args {
11908+ struct au_drinfo **drinfop;
11909+ struct path *h_ppath;
11910+ char *whname;
11911+ int whnamelen;
11912+ struct dentry **info_dentry;
11913+};
11914+
11915+static void au_call_drinfo_do_load(void *args)
11916+{
11917+ struct au_drinfo_do_load_args *a = args;
11918+
11919+ *a->drinfop = au_drinfo_do_load(a->h_ppath, a->whname, a->whnamelen,
11920+ a->info_dentry);
11921+}
11922+
11923+struct au_drinfo_load {
11924+ struct path h_ppath;
11925+ struct qstr *qname;
11926+ unsigned char no_sio;
11927+
11928+ aufs_bindex_t ninfo;
11929+ struct au_drinfo **drinfo;
11930+};
11931+
11932+static int au_drinfo_load(struct au_drinfo_load *w, aufs_bindex_t bindex,
11933+ struct au_branch *br)
11934+{
11935+ int err, wkq_err, whnamelen, e;
11936+ char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ]
11937+ = AUFS_WH_DR_INFO_PFX;
11938+ struct au_drinfo *drinfo;
11939+ struct qstr oldname;
11940+ struct inode *h_dir, *delegated;
11941+ struct dentry *info_dentry;
11942+ struct path infopath;
11943+
11944+ whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11945+ whnamelen += au_drinfo_name(br, whname + whnamelen,
11946+ sizeof(whname) - whnamelen);
11947+ if (w->no_sio)
11948+ drinfo = au_drinfo_do_load(&w->h_ppath, whname, whnamelen,
11949+ &info_dentry);
11950+ else {
11951+ struct au_drinfo_do_load_args args = {
11952+ .drinfop = &drinfo,
11953+ .h_ppath = &w->h_ppath,
11954+ .whname = whname,
11955+ .whnamelen = whnamelen,
11956+ .info_dentry = &info_dentry
11957+ };
11958+ wkq_err = au_wkq_wait(au_call_drinfo_do_load, &args);
11959+ if (unlikely(wkq_err))
11960+ drinfo = ERR_PTR(wkq_err);
11961+ }
11962+ err = PTR_ERR(drinfo);
11963+ if (IS_ERR_OR_NULL(drinfo))
11964+ goto out;
11965+
11966+ err = 0;
11967+ oldname.len = drinfo->oldnamelen;
11968+ oldname.name = drinfo->oldname;
11969+ if (au_qstreq(w->qname, &oldname)) {
11970+ /* the name is renamed back */
9f237c51 11971+ au_kfree_rcu(drinfo);
8b6a4947
AM
11972+ drinfo = NULL;
11973+
11974+ infopath.dentry = info_dentry;
11975+ infopath.mnt = w->h_ppath.mnt;
11976+ h_dir = d_inode(w->h_ppath.dentry);
11977+ delegated = NULL;
11978+ inode_lock_nested(h_dir, AuLsc_I_PARENT);
11979+ e = vfsub_unlink(h_dir, &infopath, &delegated, !w->no_sio);
11980+ inode_unlock(h_dir);
11981+ if (unlikely(e))
11982+ AuIOErr("ignored %d, %pd2\n", e, &infopath.dentry);
11983+ if (unlikely(e == -EWOULDBLOCK))
11984+ iput(delegated);
11985+ }
9f237c51 11986+ au_kfree_rcu(w->drinfo[bindex]);
8b6a4947
AM
11987+ w->drinfo[bindex] = drinfo;
11988+ dput(info_dentry);
11989+
11990+out:
11991+ AuTraceErr(err);
11992+ return err;
11993+}
11994+
11995+/* ---------------------------------------------------------------------- */
11996+
11997+static void au_dr_lkup_free(struct au_drinfo **drinfo, int n)
11998+{
11999+ struct au_drinfo **p = drinfo;
12000+
12001+ while (n-- > 0)
9f237c51
AM
12002+ au_kfree_rcu(*drinfo++);
12003+ au_kfree_try_rcu(p);
8b6a4947
AM
12004+}
12005+
12006+int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12007+ aufs_bindex_t btgt)
12008+{
12009+ int err, ninfo;
12010+ struct au_drinfo_load w;
12011+ aufs_bindex_t bindex, bbot;
12012+ struct au_branch *br;
12013+ struct inode *h_dir;
12014+ struct au_dr_hino *ent;
12015+ struct super_block *sb;
12016+
12017+ AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
12018+ AuLNPair(&dentry->d_name), AuLNPair(&lkup->dirren.dr_name),
12019+ AuLNPair(&lkup->whname), btgt);
12020+
12021+ sb = dentry->d_sb;
12022+ bbot = au_sbbot(sb);
12023+ w.ninfo = bbot + 1;
12024+ if (!lkup->dirren.drinfo) {
12025+ lkup->dirren.drinfo = kcalloc(w.ninfo,
12026+ sizeof(*lkup->dirren.drinfo),
12027+ GFP_NOFS);
12028+ if (unlikely(!lkup->dirren.drinfo)) {
12029+ err = -ENOMEM;
12030+ goto out;
12031+ }
12032+ lkup->dirren.ninfo = w.ninfo;
12033+ }
12034+ w.drinfo = lkup->dirren.drinfo;
12035+ w.no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
12036+ w.h_ppath.dentry = au_h_dptr(dentry, btgt);
12037+ AuDebugOn(!w.h_ppath.dentry);
12038+ w.h_ppath.mnt = au_sbr_mnt(sb, btgt);
12039+ w.qname = &dentry->d_name;
12040+
12041+ ninfo = 0;
12042+ for (bindex = btgt + 1; bindex <= bbot; bindex++) {
12043+ br = au_sbr(sb, bindex);
12044+ err = au_drinfo_load(&w, bindex, br);
12045+ if (unlikely(err))
12046+ goto out_free;
12047+ if (w.drinfo[bindex])
12048+ ninfo++;
12049+ }
12050+ if (!ninfo) {
12051+ br = au_sbr(sb, btgt);
12052+ h_dir = d_inode(w.h_ppath.dentry);
12053+ ent = au_dr_hino_find(&br->br_dirren, h_dir->i_ino);
12054+ AuDebugOn(!ent);
12055+ au_dr_hino_del(&br->br_dirren, ent);
9f237c51 12056+ au_kfree_rcu(ent);
8b6a4947
AM
12057+ }
12058+ goto out; /* success */
12059+
12060+out_free:
12061+ au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12062+ lkup->dirren.ninfo = 0;
12063+ lkup->dirren.drinfo = NULL;
12064+out:
12065+ AuTraceErr(err);
12066+ return err;
12067+}
12068+
12069+void au_dr_lkup_fin(struct au_do_lookup_args *lkup)
12070+{
12071+ au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12072+}
12073+
12074+int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt)
12075+{
12076+ int err;
12077+ struct au_drinfo *drinfo;
12078+
12079+ err = 0;
12080+ if (!lkup->dirren.drinfo)
12081+ goto out;
43982f53
AM
12082+ AuDebugOn(lkup->dirren.ninfo <= btgt);
12083+ drinfo = lkup->dirren.drinfo[btgt];
8b6a4947
AM
12084+ if (!drinfo)
12085+ goto out;
12086+
9f237c51 12087+ au_kfree_try_rcu(lkup->whname.name);
8b6a4947
AM
12088+ lkup->whname.name = NULL;
12089+ lkup->dirren.dr_name.len = drinfo->oldnamelen;
12090+ lkup->dirren.dr_name.name = drinfo->oldname;
12091+ lkup->name = &lkup->dirren.dr_name;
12092+ err = au_wh_name_alloc(&lkup->whname, lkup->name);
12093+ if (!err)
12094+ AuDbg("name %.*s, whname %.*s, b%d\n",
12095+ AuLNPair(lkup->name), AuLNPair(&lkup->whname),
12096+ btgt);
12097+
12098+out:
12099+ AuTraceErr(err);
12100+ return err;
12101+}
12102+
12103+int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12104+ ino_t h_ino)
12105+{
12106+ int match;
12107+ struct au_drinfo *drinfo;
12108+
12109+ match = 1;
12110+ if (!lkup->dirren.drinfo)
12111+ goto out;
43982f53
AM
12112+ AuDebugOn(lkup->dirren.ninfo <= bindex);
12113+ drinfo = lkup->dirren.drinfo[bindex];
8b6a4947
AM
12114+ if (!drinfo)
12115+ goto out;
12116+
12117+ match = (drinfo->ino == h_ino);
12118+ AuDbg("match %d\n", match);
12119+
12120+out:
12121+ return match;
12122+}
12123+
12124+/* ---------------------------------------------------------------------- */
12125+
12126+int au_dr_opt_set(struct super_block *sb)
12127+{
12128+ int err;
12129+ aufs_bindex_t bindex, bbot;
12130+ struct au_branch *br;
12131+
12132+ err = 0;
12133+ bbot = au_sbbot(sb);
12134+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
12135+ br = au_sbr(sb, bindex);
12136+ err = au_dr_hino(sb, bindex, /*br*/NULL, &br->br_path);
12137+ }
12138+
12139+ return err;
12140+}
12141+
12142+int au_dr_opt_flush(struct super_block *sb)
12143+{
12144+ int err;
12145+ aufs_bindex_t bindex, bbot;
12146+ struct au_branch *br;
12147+
12148+ err = 0;
12149+ bbot = au_sbbot(sb);
12150+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
12151+ br = au_sbr(sb, bindex);
12152+ if (au_br_writable(br->br_perm))
12153+ err = au_dr_hino(sb, bindex, /*br*/NULL, /*path*/NULL);
12154+ }
12155+
12156+ return err;
12157+}
12158+
12159+int au_dr_opt_clr(struct super_block *sb, int no_flush)
12160+{
12161+ int err;
12162+ aufs_bindex_t bindex, bbot;
12163+ struct au_branch *br;
12164+
12165+ err = 0;
12166+ if (!no_flush) {
12167+ err = au_dr_opt_flush(sb);
12168+ if (unlikely(err))
12169+ goto out;
12170+ }
12171+
12172+ bbot = au_sbbot(sb);
12173+ for (bindex = 0; bindex <= bbot; bindex++) {
12174+ br = au_sbr(sb, bindex);
12175+ au_dr_hino_free(&br->br_dirren);
12176+ }
12177+
12178+out:
12179+ return err;
12180+}
12181diff -urN /usr/share/empty/fs/aufs/dirren.h linux/fs/aufs/dirren.h
eca34b5c 12182--- /usr/share/empty/fs/aufs/dirren.h 1970-01-01 01:00:00.000000000 +0100
594d0238 12183+++ linux/fs/aufs/dirren.h 2022-11-05 23:02:18.962555950 +0100
062440b3
AM
12184@@ -0,0 +1,140 @@
12185+/* SPDX-License-Identifier: GPL-2.0 */
8b6a4947 12186+/*
0dcfbb52 12187+ * Copyright (C) 2017-2022 Junjiro R. Okajima
8b6a4947 12188+ *
0dcfbb52 12189+ * This program is free software; you can redistribute it and/or modify
8b6a4947
AM
12190+ * it under the terms of the GNU General Public License as published by
12191+ * the Free Software Foundation; either version 2 of the License, or
12192+ * (at your option) any later version.
12193+ *
12194+ * This program is distributed in the hope that it will be useful,
12195+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12196+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12197+ * GNU General Public License for more details.
12198+ *
12199+ * You should have received a copy of the GNU General Public License
12200+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
12201+ */
12202+
12203+/*
12204+ * renamed dir info
12205+ */
12206+
12207+#ifndef __AUFS_DIRREN_H__
12208+#define __AUFS_DIRREN_H__
12209+
12210+#ifdef __KERNEL__
12211+
12212+#include <linux/dcache.h>
12213+#include <linux/statfs.h>
12214+#include <linux/uuid.h>
12215+#include "hbl.h"
12216+
12217+#define AuDirren_NHASH 100
12218+
12219+#ifdef CONFIG_AUFS_DIRREN
12220+enum au_brid_type {
12221+ AuBrid_Unset,
12222+ AuBrid_UUID,
12223+ AuBrid_FSID,
12224+ AuBrid_DEV
12225+};
12226+
12227+struct au_dr_brid {
12228+ enum au_brid_type type;
12229+ union {
12230+ uuid_t uuid; /* unimplemented yet */
12231+ fsid_t fsid;
12232+ dev_t dev;
12233+ };
12234+};
12235+
12236+/* 20 is the max digits length of ulong 64 */
12237+/* brid-type "_" uuid "_" inum */
12238+#define AUFS_DIRREN_FNAME_SZ (1 + 1 + UUID_STRING_LEN + 20)
12239+#define AUFS_DIRREN_ENV_VAL_SZ (AUFS_DIRREN_FNAME_SZ + 1 + 20)
12240+
12241+struct au_dr_hino {
12242+ struct hlist_bl_node dr_hnode;
12243+ ino_t dr_h_ino;
12244+};
12245+
12246+struct au_dr_br {
12247+ struct hlist_bl_head dr_h_ino[AuDirren_NHASH];
12248+ struct au_dr_brid dr_brid;
12249+};
12250+
12251+struct au_dr_lookup {
12252+ /* dr_name is pointed by struct au_do_lookup_args.name */
12253+ struct qstr dr_name; /* subset of dr_info */
12254+ aufs_bindex_t ninfo;
12255+ struct au_drinfo **drinfo;
12256+};
12257+#else
12258+struct au_dr_hino;
12259+/* empty */
12260+struct au_dr_br { };
12261+struct au_dr_lookup { };
12262+#endif
12263+
12264+/* ---------------------------------------------------------------------- */
12265+
12266+struct au_branch;
12267+struct au_do_lookup_args;
12268+struct au_hinode;
12269+#ifdef CONFIG_AUFS_DIRREN
12270+int au_dr_hino_test_add(struct au_dr_br *dr, ino_t h_ino,
12271+ struct au_dr_hino *add_ent);
12272+void au_dr_hino_free(struct au_dr_br *dr);
12273+int au_dr_br_init(struct super_block *sb, struct au_branch *br,
12274+ const struct path *path);
12275+int au_dr_br_fin(struct super_block *sb, struct au_branch *br);
12276+int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
12277+ struct qstr *dst_name, void *_rev);
12278+void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *rev);
12279+void au_dr_rename_rev(struct dentry *src, aufs_bindex_t bindex, void *rev);
12280+int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12281+ aufs_bindex_t bindex);
12282+int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12283+int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12284+ ino_t h_ino);
12285+void au_dr_lkup_fin(struct au_do_lookup_args *lkup);
12286+int au_dr_opt_set(struct super_block *sb);
12287+int au_dr_opt_flush(struct super_block *sb);
12288+int au_dr_opt_clr(struct super_block *sb, int no_flush);
12289+#else
12290+AuStubInt0(au_dr_hino_test_add, struct au_dr_br *dr, ino_t h_ino,
12291+ struct au_dr_hino *add_ent);
12292+AuStubVoid(au_dr_hino_free, struct au_dr_br *dr);
12293+AuStubInt0(au_dr_br_init, struct super_block *sb, struct au_branch *br,
12294+ const struct path *path);
12295+AuStubInt0(au_dr_br_fin, struct super_block *sb, struct au_branch *br);
12296+AuStubInt0(au_dr_rename, struct dentry *src, aufs_bindex_t bindex,
12297+ struct qstr *dst_name, void *_rev);
12298+AuStubVoid(au_dr_rename_fin, struct dentry *src, aufs_bindex_t btgt, void *rev);
12299+AuStubVoid(au_dr_rename_rev, struct dentry *src, aufs_bindex_t bindex,
12300+ void *rev);
12301+AuStubInt0(au_dr_lkup, struct au_do_lookup_args *lkup, struct dentry *dentry,
12302+ aufs_bindex_t bindex);
12303+AuStubInt0(au_dr_lkup_name, struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12304+AuStubInt0(au_dr_lkup_h_ino, struct au_do_lookup_args *lkup,
12305+ aufs_bindex_t bindex, ino_t h_ino);
12306+AuStubVoid(au_dr_lkup_fin, struct au_do_lookup_args *lkup);
12307+AuStubInt0(au_dr_opt_set, struct super_block *sb);
12308+AuStubInt0(au_dr_opt_flush, struct super_block *sb);
12309+AuStubInt0(au_dr_opt_clr, struct super_block *sb, int no_flush);
12310+#endif
12311+
12312+/* ---------------------------------------------------------------------- */
12313+
12314+#ifdef CONFIG_AUFS_DIRREN
12315+static inline int au_dr_ihash(ino_t h_ino)
12316+{
12317+ return h_ino % AuDirren_NHASH;
12318+}
12319+#else
12320+AuStubInt0(au_dr_ihash, ino_t h_ino);
12321+#endif
12322+
12323+#endif /* __KERNEL__ */
12324+#endif /* __AUFS_DIRREN_H__ */
7f207e10 12325diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
eca34b5c 12326--- /usr/share/empty/fs/aufs/dynop.c 1970-01-01 01:00:00.000000000 +0100
594d0238 12327+++ linux/fs/aufs/dynop.c 2022-11-05 23:02:18.962555950 +0100
82da57cb 12328@@ -0,0 +1,366 @@
cd7a4cd9 12329+// SPDX-License-Identifier: GPL-2.0
1facf9fc 12330+/*
0dcfbb52 12331+ * Copyright (C) 2010-2022 Junjiro R. Okajima
1facf9fc 12332+ *
0dcfbb52 12333+ * This program is free software; you can redistribute it and/or modify
1facf9fc 12334+ * it under the terms of the GNU General Public License as published by
12335+ * the Free Software Foundation; either version 2 of the License, or
12336+ * (at your option) any later version.
dece6358
AM
12337+ *
12338+ * This program is distributed in the hope that it will be useful,
12339+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12340+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12341+ * GNU General Public License for more details.
12342+ *
12343+ * You should have received a copy of the GNU General Public License
523b37e3 12344+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 12345+ */
12346+
12347+/*
4a4d8108 12348+ * dynamically customizable operations for regular files
1facf9fc 12349+ */
12350+
1facf9fc 12351+#include "aufs.h"
12352+
4a4d8108 12353+#define DyPrSym(key) AuDbgSym(key->dk_op.dy_hop)
1facf9fc 12354+
4a4d8108
AM
12355+/*
12356+ * How large will these lists be?
12357+ * Usually just a few elements, 20-30 at most for each, I guess.
12358+ */
8b6a4947 12359+static struct hlist_bl_head dynop[AuDyLast];
4a4d8108 12360+
8b6a4947
AM
12361+static struct au_dykey *dy_gfind_get(struct hlist_bl_head *hbl,
12362+ const void *h_op)
1facf9fc 12363+{
4a4d8108 12364+ struct au_dykey *key, *tmp;
8b6a4947 12365+ struct hlist_bl_node *pos;
1facf9fc 12366+
4a4d8108 12367+ key = NULL;
8b6a4947
AM
12368+ hlist_bl_lock(hbl);
12369+ hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
4a4d8108 12370+ if (tmp->dk_op.dy_hop == h_op) {
83b672a5
AM
12371+ if (kref_get_unless_zero(&tmp->dk_kref))
12372+ key = tmp;
4a4d8108
AM
12373+ break;
12374+ }
8b6a4947 12375+ hlist_bl_unlock(hbl);
4a4d8108
AM
12376+
12377+ return key;
1facf9fc 12378+}
12379+
4a4d8108 12380+static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
1facf9fc 12381+{
4a4d8108
AM
12382+ struct au_dykey **k, *found;
12383+ const void *h_op = key->dk_op.dy_hop;
12384+ int i;
1facf9fc 12385+
4a4d8108
AM
12386+ found = NULL;
12387+ k = br->br_dykey;
12388+ for (i = 0; i < AuBrDynOp; i++)
12389+ if (k[i]) {
12390+ if (k[i]->dk_op.dy_hop == h_op) {
12391+ found = k[i];
12392+ break;
12393+ }
12394+ } else
12395+ break;
12396+ if (!found) {
12397+ spin_lock(&br->br_dykey_lock);
12398+ for (; i < AuBrDynOp; i++)
12399+ if (k[i]) {
12400+ if (k[i]->dk_op.dy_hop == h_op) {
12401+ found = k[i];
12402+ break;
12403+ }
12404+ } else {
12405+ k[i] = key;
12406+ break;
12407+ }
12408+ spin_unlock(&br->br_dykey_lock);
12409+ BUG_ON(i == AuBrDynOp); /* expand the array */
12410+ }
12411+
12412+ return found;
1facf9fc 12413+}
12414+
4a4d8108 12415+/* kref_get() if @key is already added */
8b6a4947 12416+static struct au_dykey *dy_gadd(struct hlist_bl_head *hbl, struct au_dykey *key)
4a4d8108
AM
12417+{
12418+ struct au_dykey *tmp, *found;
8b6a4947 12419+ struct hlist_bl_node *pos;
4a4d8108 12420+ const void *h_op = key->dk_op.dy_hop;
1facf9fc 12421+
4a4d8108 12422+ found = NULL;
8b6a4947
AM
12423+ hlist_bl_lock(hbl);
12424+ hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
4a4d8108 12425+ if (tmp->dk_op.dy_hop == h_op) {
83b672a5
AM
12426+ if (kref_get_unless_zero(&tmp->dk_kref))
12427+ found = tmp;
4a4d8108
AM
12428+ break;
12429+ }
12430+ if (!found)
8b6a4947
AM
12431+ hlist_bl_add_head(&key->dk_hnode, hbl);
12432+ hlist_bl_unlock(hbl);
1facf9fc 12433+
4a4d8108
AM
12434+ if (!found)
12435+ DyPrSym(key);
12436+ return found;
12437+}
12438+
12439+static void dy_free_rcu(struct rcu_head *rcu)
1facf9fc 12440+{
4a4d8108
AM
12441+ struct au_dykey *key;
12442+
12443+ key = container_of(rcu, struct au_dykey, dk_rcu);
12444+ DyPrSym(key);
83b672a5 12445+ kfree(key);
1facf9fc 12446+}
12447+
4a4d8108
AM
12448+static void dy_free(struct kref *kref)
12449+{
12450+ struct au_dykey *key;
8b6a4947 12451+ struct hlist_bl_head *hbl;
1facf9fc 12452+
4a4d8108 12453+ key = container_of(kref, struct au_dykey, dk_kref);
8b6a4947
AM
12454+ hbl = dynop + key->dk_op.dy_type;
12455+ au_hbl_del(&key->dk_hnode, hbl);
4a4d8108
AM
12456+ call_rcu(&key->dk_rcu, dy_free_rcu);
12457+}
12458+
12459+void au_dy_put(struct au_dykey *key)
1facf9fc 12460+{
4a4d8108
AM
12461+ kref_put(&key->dk_kref, dy_free);
12462+}
1facf9fc 12463+
4a4d8108
AM
12464+/* ---------------------------------------------------------------------- */
12465+
12466+#define DyDbgSize(cnt, op) AuDebugOn(cnt != sizeof(op)/sizeof(void *))
12467+
12468+#ifdef CONFIG_AUFS_DEBUG
12469+#define DyDbgDeclare(cnt) unsigned int cnt = 0
4f0767ce 12470+#define DyDbgInc(cnt) do { cnt++; } while (0)
4a4d8108
AM
12471+#else
12472+#define DyDbgDeclare(cnt) do {} while (0)
12473+#define DyDbgInc(cnt) do {} while (0)
12474+#endif
12475+
12476+#define DySet(func, dst, src, h_op, h_sb) do { \
12477+ DyDbgInc(cnt); \
12478+ if (h_op->func) { \
12479+ if (src.func) \
12480+ dst.func = src.func; \
12481+ else \
12482+ AuDbg("%s %s\n", au_sbtype(h_sb), #func); \
12483+ } \
12484+} while (0)
12485+
12486+#define DySetForce(func, dst, src) do { \
12487+ AuDebugOn(!src.func); \
12488+ DyDbgInc(cnt); \
12489+ dst.func = src.func; \
12490+} while (0)
12491+
12492+#define DySetAop(func) \
12493+ DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
12494+#define DySetAopForce(func) \
12495+ DySetForce(func, dyaop->da_op, aufs_aop)
12496+
12497+static void dy_aop(struct au_dykey *key, const void *h_op,
12498+ struct super_block *h_sb __maybe_unused)
12499+{
12500+ struct au_dyaop *dyaop = (void *)key;
12501+ const struct address_space_operations *h_aop = h_op;
12502+ DyDbgDeclare(cnt);
12503+
12504+ AuDbg("%s\n", au_sbtype(h_sb));
12505+
12506+ DySetAop(writepage);
22319442 12507+ DySetAopForce(read_folio); /* force */
4a4d8108 12508+ DySetAop(writepages);
26eb093c
AM
12509+ DySetAop(dirty_folio);
12510+ DySetAop(invalidate_folio);
e37dd06a 12511+ DySetAop(readahead);
4a4d8108
AM
12512+ DySetAop(write_begin);
12513+ DySetAop(write_end);
12514+ DySetAop(bmap);
22319442
JP
12515+ DySetAop(release_folio);
12516+ DySetAop(free_folio);
7e9cd9fe 12517+ /* this one will be changed according to an aufs mount option */
4a4d8108 12518+ DySetAop(direct_IO);
82da57cb 12519+ DySetAop(migrate_folio);
26eb093c 12520+ DySetAop(launder_folio);
4a4d8108 12521+ DySetAop(is_partially_uptodate);
392086de 12522+ DySetAop(is_dirty_writeback);
4a4d8108 12523+ DySetAop(error_remove_page);
b4510431
AM
12524+ DySetAop(swap_activate);
12525+ DySetAop(swap_deactivate);
22319442 12526+ DySetAop(swap_rw);
4a4d8108
AM
12527+
12528+ DyDbgSize(cnt, *h_aop);
4a4d8108
AM
12529+}
12530+
4a4d8108
AM
12531+/* ---------------------------------------------------------------------- */
12532+
12533+static void dy_bug(struct kref *kref)
12534+{
12535+ BUG();
12536+}
12537+
12538+static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
12539+{
12540+ struct au_dykey *key, *old;
8b6a4947 12541+ struct hlist_bl_head *hbl;
b752ccd1 12542+ struct op {
4a4d8108 12543+ unsigned int sz;
b752ccd1
AM
12544+ void (*set)(struct au_dykey *key, const void *h_op,
12545+ struct super_block *h_sb __maybe_unused);
12546+ };
12547+ static const struct op a[] = {
4a4d8108
AM
12548+ [AuDy_AOP] = {
12549+ .sz = sizeof(struct au_dyaop),
b752ccd1 12550+ .set = dy_aop
4a4d8108 12551+ }
b752ccd1
AM
12552+ };
12553+ const struct op *p;
4a4d8108 12554+
8b6a4947
AM
12555+ hbl = dynop + op->dy_type;
12556+ key = dy_gfind_get(hbl, op->dy_hop);
4a4d8108
AM
12557+ if (key)
12558+ goto out_add; /* success */
12559+
12560+ p = a + op->dy_type;
12561+ key = kzalloc(p->sz, GFP_NOFS);
12562+ if (unlikely(!key)) {
12563+ key = ERR_PTR(-ENOMEM);
12564+ goto out;
12565+ }
12566+
12567+ key->dk_op.dy_hop = op->dy_hop;
12568+ kref_init(&key->dk_kref);
86dc4139 12569+ p->set(key, op->dy_hop, au_br_sb(br));
8b6a4947 12570+ old = dy_gadd(hbl, key);
4a4d8108 12571+ if (old) {
9f237c51 12572+ au_kfree_rcu(key);
4a4d8108
AM
12573+ key = old;
12574+ }
12575+
12576+out_add:
12577+ old = dy_bradd(br, key);
12578+ if (old)
12579+ /* its ref-count should never be zero here */
12580+ kref_put(&key->dk_kref, dy_bug);
12581+out:
12582+ return key;
12583+}
12584+
12585+/* ---------------------------------------------------------------------- */
12586+/*
acd2b654 12587+ * Aufs prohibits O_DIRECT by default even if the branch supports it.
c1595e42 12588+ * This behaviour is necessary to return an error from open(O_DIRECT) instead
4a4d8108
AM
12589+ * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
12590+ * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
12591+ * See the aufs manual in detail.
4a4d8108
AM
12592+ */
12593+static void dy_adx(struct au_dyaop *dyaop, int do_dx)
12594+{
7e9cd9fe 12595+ if (!do_dx)
4a4d8108 12596+ dyaop->da_op.direct_IO = NULL;
7e9cd9fe 12597+ else
4a4d8108 12598+ dyaop->da_op.direct_IO = aufs_aop.direct_IO;
4a4d8108
AM
12599+}
12600+
12601+static struct au_dyaop *dy_aget(struct au_branch *br,
12602+ const struct address_space_operations *h_aop,
12603+ int do_dx)
12604+{
12605+ struct au_dyaop *dyaop;
12606+ struct au_dynop op;
12607+
12608+ op.dy_type = AuDy_AOP;
12609+ op.dy_haop = h_aop;
12610+ dyaop = (void *)dy_get(&op, br);
12611+ if (IS_ERR(dyaop))
12612+ goto out;
12613+ dy_adx(dyaop, do_dx);
12614+
12615+out:
12616+ return dyaop;
12617+}
12618+
12619+int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12620+ struct inode *h_inode)
12621+{
12622+ int err, do_dx;
12623+ struct super_block *sb;
12624+ struct au_branch *br;
12625+ struct au_dyaop *dyaop;
12626+
12627+ AuDebugOn(!S_ISREG(h_inode->i_mode));
12628+ IiMustWriteLock(inode);
12629+
12630+ sb = inode->i_sb;
12631+ br = au_sbr(sb, bindex);
12632+ do_dx = !!au_opt_test(au_mntflags(sb), DIO);
12633+ dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
12634+ err = PTR_ERR(dyaop);
12635+ if (IS_ERR(dyaop))
12636+ /* unnecessary to call dy_fput() */
12637+ goto out;
12638+
12639+ err = 0;
12640+ inode->i_mapping->a_ops = &dyaop->da_op;
12641+
12642+out:
12643+ return err;
12644+}
12645+
b752ccd1
AM
12646+/*
12647+ * Is it safe to replace a_ops during the inode/file is in operation?
12648+ * Yes, I hope so.
12649+ */
12650+int au_dy_irefresh(struct inode *inode)
12651+{
12652+ int err;
5afbbe0d 12653+ aufs_bindex_t btop;
b752ccd1
AM
12654+ struct inode *h_inode;
12655+
12656+ err = 0;
12657+ if (S_ISREG(inode->i_mode)) {
5afbbe0d
AM
12658+ btop = au_ibtop(inode);
12659+ h_inode = au_h_iptr(inode, btop);
12660+ err = au_dy_iaop(inode, btop, h_inode);
b752ccd1
AM
12661+ }
12662+ return err;
12663+}
12664+
4a4d8108
AM
12665+void au_dy_arefresh(int do_dx)
12666+{
8b6a4947
AM
12667+ struct hlist_bl_head *hbl;
12668+ struct hlist_bl_node *pos;
4a4d8108
AM
12669+ struct au_dykey *key;
12670+
8b6a4947
AM
12671+ hbl = dynop + AuDy_AOP;
12672+ hlist_bl_lock(hbl);
12673+ hlist_bl_for_each_entry(key, pos, hbl, dk_hnode)
4a4d8108 12674+ dy_adx((void *)key, do_dx);
8b6a4947 12675+ hlist_bl_unlock(hbl);
4a4d8108
AM
12676+}
12677+
4a4d8108
AM
12678+/* ---------------------------------------------------------------------- */
12679+
12680+void __init au_dy_init(void)
12681+{
12682+ int i;
12683+
4a4d8108 12684+ for (i = 0; i < AuDyLast; i++)
8b6a4947 12685+ INIT_HLIST_BL_HEAD(dynop + i);
4a4d8108
AM
12686+}
12687+
12688+void au_dy_fin(void)
12689+{
12690+ int i;
12691+
12692+ for (i = 0; i < AuDyLast; i++)
8b6a4947 12693+ WARN_ON(!hlist_bl_empty(dynop + i));
4a4d8108 12694+}
7f207e10 12695diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
eca34b5c 12696--- /usr/share/empty/fs/aufs/dynop.h 1970-01-01 01:00:00.000000000 +0100
594d0238 12697+++ linux/fs/aufs/dynop.h 2022-11-05 23:02:18.962555950 +0100
fbc438ed 12698@@ -0,0 +1,77 @@
062440b3 12699+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 12700+/*
0dcfbb52 12701+ * Copyright (C) 2010-2022 Junjiro R. Okajima
4a4d8108 12702+ *
0dcfbb52 12703+ * This program is free software; you can redistribute it and/or modify
4a4d8108
AM
12704+ * it under the terms of the GNU General Public License as published by
12705+ * the Free Software Foundation; either version 2 of the License, or
12706+ * (at your option) any later version.
12707+ *
12708+ * This program is distributed in the hope that it will be useful,
12709+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12710+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12711+ * GNU General Public License for more details.
12712+ *
12713+ * You should have received a copy of the GNU General Public License
523b37e3 12714+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
12715+ */
12716+
12717+/*
12718+ * dynamically customizable operations (for regular files only)
12719+ */
12720+
12721+#ifndef __AUFS_DYNOP_H__
12722+#define __AUFS_DYNOP_H__
12723+
12724+#ifdef __KERNEL__
12725+
7e9cd9fe
AM
12726+#include <linux/fs.h>
12727+#include <linux/kref.h>
4a4d8108 12728+
2cbb1c4b 12729+enum {AuDy_AOP, AuDyLast};
4a4d8108
AM
12730+
12731+struct au_dynop {
12732+ int dy_type;
12733+ union {
12734+ const void *dy_hop;
12735+ const struct address_space_operations *dy_haop;
4a4d8108
AM
12736+ };
12737+};
12738+
12739+struct au_dykey {
12740+ union {
8b6a4947 12741+ struct hlist_bl_node dk_hnode;
4a4d8108
AM
12742+ struct rcu_head dk_rcu;
12743+ };
12744+ struct au_dynop dk_op;
12745+
12746+ /*
12747+ * during I am in the branch local array, kref is gotten. when the
12748+ * branch is removed, kref is put.
12749+ */
12750+ struct kref dk_kref;
12751+};
12752+
12753+/* stop unioning since their sizes are very different from each other */
12754+struct au_dyaop {
12755+ struct au_dykey da_key;
12756+ struct address_space_operations da_op; /* not const */
4a4d8108 12757+};
fbc438ed
JR
12758+/* make sure that 'struct au_dykey *' can be any type */
12759+static_assert(!offsetof(struct au_dyaop, da_key));
4a4d8108 12760+
4a4d8108
AM
12761+/* ---------------------------------------------------------------------- */
12762+
12763+/* dynop.c */
12764+struct au_branch;
12765+void au_dy_put(struct au_dykey *key);
12766+int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12767+ struct inode *h_inode);
b752ccd1 12768+int au_dy_irefresh(struct inode *inode);
4a4d8108 12769+void au_dy_arefresh(int do_dio);
4a4d8108
AM
12770+
12771+void __init au_dy_init(void);
12772+void au_dy_fin(void);
12773+
4a4d8108
AM
12774+#endif /* __KERNEL__ */
12775+#endif /* __AUFS_DYNOP_H__ */
7f207e10 12776diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
eca34b5c 12777--- /usr/share/empty/fs/aufs/export.c 1970-01-01 01:00:00.000000000 +0100
594d0238 12778+++ linux/fs/aufs/export.c 2022-12-17 09:21:34.796521861 +0100
f4d37d76 12779@@ -0,0 +1,830 @@
cd7a4cd9 12780+// SPDX-License-Identifier: GPL-2.0
4a4d8108 12781+/*
0dcfbb52 12782+ * Copyright (C) 2005-2022 Junjiro R. Okajima
4a4d8108 12783+ *
0dcfbb52 12784+ * This program is free software; you can redistribute it and/or modify
4a4d8108
AM
12785+ * it under the terms of the GNU General Public License as published by
12786+ * the Free Software Foundation; either version 2 of the License, or
12787+ * (at your option) any later version.
12788+ *
12789+ * This program is distributed in the hope that it will be useful,
12790+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12791+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12792+ * GNU General Public License for more details.
12793+ *
12794+ * You should have received a copy of the GNU General Public License
523b37e3 12795+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
12796+ */
12797+
12798+/*
12799+ * export via nfs
12800+ */
12801+
12802+#include <linux/exportfs.h>
7eafdf33 12803+#include <linux/fs_struct.h>
4a4d8108
AM
12804+#include <linux/nsproxy.h>
12805+#include <linux/random.h>
12806+#include <linux/writeback.h>
12807+#include "aufs.h"
12808+
12809+union conv {
12810+#ifdef CONFIG_AUFS_INO_T_64
12811+ __u32 a[2];
12812+#else
12813+ __u32 a[1];
12814+#endif
12815+ ino_t ino;
12816+};
12817+
12818+static ino_t decode_ino(__u32 *a)
12819+{
12820+ union conv u;
12821+
12822+ BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
12823+ u.a[0] = a[0];
12824+#ifdef CONFIG_AUFS_INO_T_64
12825+ u.a[1] = a[1];
12826+#endif
12827+ return u.ino;
12828+}
12829+
12830+static void encode_ino(__u32 *a, ino_t ino)
12831+{
12832+ union conv u;
12833+
12834+ u.ino = ino;
12835+ a[0] = u.a[0];
12836+#ifdef CONFIG_AUFS_INO_T_64
12837+ a[1] = u.a[1];
12838+#endif
12839+}
12840+
12841+/* NFS file handle */
12842+enum {
12843+ Fh_br_id,
12844+ Fh_sigen,
12845+#ifdef CONFIG_AUFS_INO_T_64
12846+ /* support 64bit inode number */
12847+ Fh_ino1,
12848+ Fh_ino2,
12849+ Fh_dir_ino1,
12850+ Fh_dir_ino2,
12851+#else
12852+ Fh_ino1,
12853+ Fh_dir_ino1,
12854+#endif
12855+ Fh_igen,
12856+ Fh_h_type,
12857+ Fh_tail,
12858+
12859+ Fh_ino = Fh_ino1,
12860+ Fh_dir_ino = Fh_dir_ino1
12861+};
12862+
12863+static int au_test_anon(struct dentry *dentry)
12864+{
027c5e7a 12865+ /* note: read d_flags without d_lock */
4a4d8108
AM
12866+ return !!(dentry->d_flags & DCACHE_DISCONNECTED);
12867+}
12868+
a2a7ad62
AM
12869+int au_test_nfsd(void)
12870+{
12871+ int ret;
12872+ struct task_struct *tsk = current;
12873+ char comm[sizeof(tsk->comm)];
12874+
12875+ ret = 0;
12876+ if (tsk->flags & PF_KTHREAD) {
12877+ get_task_comm(comm, tsk);
12878+ ret = !strcmp(comm, "nfsd");
12879+ }
12880+
12881+ return ret;
12882+}
12883+
4a4d8108
AM
12884+/* ---------------------------------------------------------------------- */
12885+/* inode generation external table */
12886+
b752ccd1 12887+void au_xigen_inc(struct inode *inode)
4a4d8108 12888+{
4a4d8108
AM
12889+ loff_t pos;
12890+ ssize_t sz;
12891+ __u32 igen;
12892+ struct super_block *sb;
12893+ struct au_sbinfo *sbinfo;
12894+
4a4d8108 12895+ sb = inode->i_sb;
b752ccd1 12896+ AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
1facf9fc 12897+
b752ccd1 12898+ sbinfo = au_sbi(sb);
1facf9fc 12899+ pos = inode->i_ino;
12900+ pos *= sizeof(igen);
12901+ igen = inode->i_generation + 1;
e37dd06a 12902+ sz = xino_fwrite(sbinfo->si_xigen, &igen, sizeof(igen), &pos);
1facf9fc 12903+ if (sz == sizeof(igen))
b752ccd1 12904+ return; /* success */
1facf9fc 12905+
b752ccd1 12906+ if (unlikely(sz >= 0))
1facf9fc 12907+ AuIOErr("xigen error (%zd)\n", sz);
1facf9fc 12908+}
12909+
12910+int au_xigen_new(struct inode *inode)
12911+{
12912+ int err;
12913+ loff_t pos;
12914+ ssize_t sz;
12915+ struct super_block *sb;
12916+ struct au_sbinfo *sbinfo;
12917+ struct file *file;
12918+
12919+ err = 0;
12920+ /* todo: dirty, at mount time */
12921+ if (inode->i_ino == AUFS_ROOT_INO)
12922+ goto out;
12923+ sb = inode->i_sb;
dece6358 12924+ SiMustAnyLock(sb);
1facf9fc 12925+ if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
12926+ goto out;
12927+
12928+ err = -EFBIG;
12929+ pos = inode->i_ino;
12930+ if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
12931+ AuIOErr1("too large i%lld\n", pos);
12932+ goto out;
12933+ }
12934+ pos *= sizeof(inode->i_generation);
12935+
12936+ err = 0;
12937+ sbinfo = au_sbi(sb);
12938+ file = sbinfo->si_xigen;
12939+ BUG_ON(!file);
12940+
c06a8ce3 12941+ if (vfsub_f_size_read(file)
1facf9fc 12942+ < pos + sizeof(inode->i_generation)) {
12943+ inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
e37dd06a 12944+ sz = xino_fwrite(file, &inode->i_generation,
1facf9fc 12945+ sizeof(inode->i_generation), &pos);
12946+ } else
e37dd06a 12947+ sz = xino_fread(file, &inode->i_generation,
1facf9fc 12948+ sizeof(inode->i_generation), &pos);
12949+ if (sz == sizeof(inode->i_generation))
12950+ goto out; /* success */
12951+
12952+ err = sz;
12953+ if (unlikely(sz >= 0)) {
12954+ err = -EIO;
12955+ AuIOErr("xigen error (%zd)\n", sz);
12956+ }
12957+
4f0767ce 12958+out:
1facf9fc 12959+ return err;
12960+}
12961+
062440b3 12962+int au_xigen_set(struct super_block *sb, struct path *path)
1facf9fc 12963+{
12964+ int err;
12965+ struct au_sbinfo *sbinfo;
12966+ struct file *file;
12967+
dece6358
AM
12968+ SiMustWriteLock(sb);
12969+
1facf9fc 12970+ sbinfo = au_sbi(sb);
062440b3 12971+ file = au_xino_create2(sb, path, sbinfo->si_xigen);
1facf9fc 12972+ err = PTR_ERR(file);
12973+ if (IS_ERR(file))
12974+ goto out;
12975+ err = 0;
12976+ if (sbinfo->si_xigen)
12977+ fput(sbinfo->si_xigen);
12978+ sbinfo->si_xigen = file;
12979+
4f0767ce 12980+out:
062440b3 12981+ AuTraceErr(err);
1facf9fc 12982+ return err;
12983+}
12984+
12985+void au_xigen_clr(struct super_block *sb)
12986+{
12987+ struct au_sbinfo *sbinfo;
12988+
dece6358
AM
12989+ SiMustWriteLock(sb);
12990+
1facf9fc 12991+ sbinfo = au_sbi(sb);
12992+ if (sbinfo->si_xigen) {
12993+ fput(sbinfo->si_xigen);
12994+ sbinfo->si_xigen = NULL;
12995+ }
12996+}
12997+
12998+/* ---------------------------------------------------------------------- */
12999+
13000+static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
13001+ ino_t dir_ino)
13002+{
13003+ struct dentry *dentry, *d;
13004+ struct inode *inode;
13005+ unsigned int sigen;
13006+
13007+ dentry = NULL;
13008+ inode = ilookup(sb, ino);
13009+ if (!inode)
13010+ goto out;
13011+
13012+ dentry = ERR_PTR(-ESTALE);
13013+ sigen = au_sigen(sb);
5afbbe0d 13014+ if (unlikely(au_is_bad_inode(inode)
1facf9fc 13015+ || IS_DEADDIR(inode)
537831f9 13016+ || sigen != au_iigen(inode, NULL)))
1facf9fc 13017+ goto out_iput;
13018+
13019+ dentry = NULL;
13020+ if (!dir_ino || S_ISDIR(inode->i_mode))
13021+ dentry = d_find_alias(inode);
13022+ else {
027c5e7a 13023+ spin_lock(&inode->i_lock);
c1595e42 13024+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
027c5e7a 13025+ spin_lock(&d->d_lock);
1facf9fc 13026+ if (!au_test_anon(d)
5527c038 13027+ && d_inode(d->d_parent)->i_ino == dir_ino) {
027c5e7a
AM
13028+ dentry = dget_dlock(d);
13029+ spin_unlock(&d->d_lock);
1facf9fc 13030+ break;
13031+ }
027c5e7a
AM
13032+ spin_unlock(&d->d_lock);
13033+ }
13034+ spin_unlock(&inode->i_lock);
1facf9fc 13035+ }
027c5e7a 13036+ if (unlikely(dentry && au_digen_test(dentry, sigen))) {
2cbb1c4b 13037+ /* need to refresh */
1facf9fc 13038+ dput(dentry);
2cbb1c4b 13039+ dentry = NULL;
1facf9fc 13040+ }
13041+
4f0767ce 13042+out_iput:
1facf9fc 13043+ iput(inode);
4f0767ce 13044+out:
2cbb1c4b 13045+ AuTraceErrPtr(dentry);
1facf9fc 13046+ return dentry;
13047+}
13048+
13049+/* ---------------------------------------------------------------------- */
13050+
13051+/* todo: dirty? */
13052+/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
4a4d8108
AM
13053+
13054+struct au_compare_mnt_args {
13055+ /* input */
13056+ struct super_block *sb;
13057+
13058+ /* output */
13059+ struct vfsmount *mnt;
13060+};
13061+
13062+static int au_compare_mnt(struct vfsmount *mnt, void *arg)
13063+{
13064+ struct au_compare_mnt_args *a = arg;
13065+
13066+ if (mnt->mnt_sb != a->sb)
13067+ return 0;
13068+ a->mnt = mntget(mnt);
13069+ return 1;
13070+}
13071+
1facf9fc 13072+static struct vfsmount *au_mnt_get(struct super_block *sb)
13073+{
4a4d8108 13074+ int err;
7eafdf33 13075+ struct path root;
4a4d8108
AM
13076+ struct au_compare_mnt_args args = {
13077+ .sb = sb
13078+ };
1facf9fc 13079+
7eafdf33 13080+ get_fs_root(current->fs, &root);
523b37e3 13081+ rcu_read_lock();
7eafdf33 13082+ err = iterate_mounts(au_compare_mnt, &args, root.mnt);
523b37e3 13083+ rcu_read_unlock();
7eafdf33 13084+ path_put(&root);
4a4d8108
AM
13085+ AuDebugOn(!err);
13086+ AuDebugOn(!args.mnt);
13087+ return args.mnt;
1facf9fc 13088+}
13089+
13090+struct au_nfsd_si_lock {
4a4d8108 13091+ unsigned int sigen;
027c5e7a 13092+ aufs_bindex_t bindex, br_id;
1facf9fc 13093+ unsigned char force_lock;
13094+};
13095+
027c5e7a
AM
13096+static int si_nfsd_read_lock(struct super_block *sb,
13097+ struct au_nfsd_si_lock *nsi_lock)
1facf9fc 13098+{
027c5e7a 13099+ int err;
1facf9fc 13100+ aufs_bindex_t bindex;
13101+
13102+ si_read_lock(sb, AuLock_FLUSH);
13103+
13104+ /* branch id may be wrapped around */
027c5e7a 13105+ err = 0;
1facf9fc 13106+ bindex = au_br_index(sb, nsi_lock->br_id);
13107+ if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
13108+ goto out; /* success */
13109+
027c5e7a
AM
13110+ err = -ESTALE;
13111+ bindex = -1;
1facf9fc 13112+ if (!nsi_lock->force_lock)
13113+ si_read_unlock(sb);
1facf9fc 13114+
4f0767ce 13115+out:
027c5e7a
AM
13116+ nsi_lock->bindex = bindex;
13117+ return err;
1facf9fc 13118+}
13119+
13120+struct find_name_by_ino {
392086de 13121+ struct dir_context ctx;
1facf9fc 13122+ int called, found;
13123+ ino_t ino;
13124+ char *name;
13125+ int namelen;
13126+};
13127+
2255d0fe 13128+static bool
392086de
AM
13129+find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
13130+ loff_t offset, u64 ino, unsigned int d_type)
1facf9fc 13131+{
392086de
AM
13132+ struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
13133+ ctx);
1facf9fc 13134+
13135+ a->called++;
13136+ if (a->ino != ino)
2255d0fe 13137+ return true;
1facf9fc 13138+
13139+ memcpy(a->name, name, namelen);
13140+ a->namelen = namelen;
13141+ a->found = 1;
2255d0fe 13142+ return false;
1facf9fc 13143+}
13144+
13145+static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
13146+ struct au_nfsd_si_lock *nsi_lock)
13147+{
13148+ struct dentry *dentry, *parent;
13149+ struct file *file;
13150+ struct inode *dir;
392086de
AM
13151+ struct find_name_by_ino arg = {
13152+ .ctx = {
2000de60 13153+ .actor = find_name_by_ino
392086de
AM
13154+ }
13155+ };
1facf9fc 13156+ int err;
13157+
13158+ parent = path->dentry;
13159+ if (nsi_lock)
13160+ si_read_unlock(parent->d_sb);
4a4d8108 13161+ file = vfsub_dentry_open(path, au_dir_roflags);
1facf9fc 13162+ dentry = (void *)file;
13163+ if (IS_ERR(file))
13164+ goto out;
13165+
13166+ dentry = ERR_PTR(-ENOMEM);
537831f9 13167+ arg.name = (void *)__get_free_page(GFP_NOFS);
1facf9fc 13168+ if (unlikely(!arg.name))
13169+ goto out_file;
13170+ arg.ino = ino;
13171+ arg.found = 0;
13172+ do {
13173+ arg.called = 0;
13174+ /* smp_mb(); */
392086de 13175+ err = vfsub_iterate_dir(file, &arg.ctx);
1facf9fc 13176+ } while (!err && !arg.found && arg.called);
13177+ dentry = ERR_PTR(err);
13178+ if (unlikely(err))
13179+ goto out_name;
1716fcea
AM
13180+ /* instead of ENOENT */
13181+ dentry = ERR_PTR(-ESTALE);
1facf9fc 13182+ if (!arg.found)
13183+ goto out_name;
13184+
b4510431 13185+ /* do not call vfsub_lkup_one() */
5527c038 13186+ dir = d_inode(parent);
758e9dad 13187+ dentry = vfsub_lookup_one_len_unlocked(arg.name, path, arg.namelen);
1facf9fc 13188+ AuTraceErrPtr(dentry);
13189+ if (IS_ERR(dentry))
13190+ goto out_name;
13191+ AuDebugOn(au_test_anon(dentry));
5527c038 13192+ if (unlikely(d_really_is_negative(dentry))) {
1facf9fc 13193+ dput(dentry);
13194+ dentry = ERR_PTR(-ENOENT);
13195+ }
13196+
4f0767ce 13197+out_name:
1c60b727 13198+ free_page((unsigned long)arg.name);
4f0767ce 13199+out_file:
1facf9fc 13200+ fput(file);
4f0767ce 13201+out:
1facf9fc 13202+ if (unlikely(nsi_lock
13203+ && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
13204+ if (!IS_ERR(dentry)) {
13205+ dput(dentry);
13206+ dentry = ERR_PTR(-ESTALE);
13207+ }
13208+ AuTraceErrPtr(dentry);
13209+ return dentry;
13210+}
13211+
13212+static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
13213+ ino_t dir_ino,
13214+ struct au_nfsd_si_lock *nsi_lock)
13215+{
13216+ struct dentry *dentry;
13217+ struct path path;
13218+
13219+ if (dir_ino != AUFS_ROOT_INO) {
13220+ path.dentry = decode_by_ino(sb, dir_ino, 0);
13221+ dentry = path.dentry;
13222+ if (!path.dentry || IS_ERR(path.dentry))
13223+ goto out;
13224+ AuDebugOn(au_test_anon(path.dentry));
13225+ } else
13226+ path.dentry = dget(sb->s_root);
13227+
13228+ path.mnt = au_mnt_get(sb);
13229+ dentry = au_lkup_by_ino(&path, ino, nsi_lock);
13230+ path_put(&path);
13231+
4f0767ce 13232+out:
1facf9fc 13233+ AuTraceErrPtr(dentry);
13234+ return dentry;
13235+}
13236+
13237+/* ---------------------------------------------------------------------- */
13238+
13239+static int h_acceptable(void *expv, struct dentry *dentry)
13240+{
13241+ return 1;
13242+}
13243+
13244+static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
13245+ char *buf, int len, struct super_block *sb)
13246+{
13247+ char *p;
13248+ int n;
13249+ struct path path;
13250+
13251+ p = d_path(h_rootpath, buf, len);
13252+ if (IS_ERR(p))
13253+ goto out;
13254+ n = strlen(p);
13255+
13256+ path.mnt = h_rootpath->mnt;
13257+ path.dentry = h_parent;
13258+ p = d_path(&path, buf, len);
13259+ if (IS_ERR(p))
13260+ goto out;
13261+ if (n != 1)
13262+ p += n;
13263+
13264+ path.mnt = au_mnt_get(sb);
13265+ path.dentry = sb->s_root;
13266+ p = d_path(&path, buf, len - strlen(p));
13267+ mntput(path.mnt);
13268+ if (IS_ERR(p))
13269+ goto out;
13270+ if (n != 1)
13271+ p[strlen(p)] = '/';
13272+
4f0767ce 13273+out:
1facf9fc 13274+ AuTraceErrPtr(p);
13275+ return p;
13276+}
13277+
13278+static
027c5e7a
AM
13279+struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
13280+ int fh_len, struct au_nfsd_si_lock *nsi_lock)
1facf9fc 13281+{
13282+ struct dentry *dentry, *h_parent, *root;
13283+ struct super_block *h_sb;
13284+ char *pathname, *p;
13285+ struct vfsmount *h_mnt;
13286+ struct au_branch *br;
13287+ int err;
13288+ struct path path;
13289+
027c5e7a 13290+ br = au_sbr(sb, nsi_lock->bindex);
86dc4139 13291+ h_mnt = au_br_mnt(br);
1facf9fc 13292+ h_sb = h_mnt->mnt_sb;
13293+ /* todo: call lower fh_to_dentry()? fh_to_parent()? */
5afbbe0d 13294+ lockdep_off();
1facf9fc 13295+ h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
13296+ fh_len - Fh_tail, fh[Fh_h_type],
13297+ h_acceptable, /*context*/NULL);
5afbbe0d 13298+ lockdep_on();
1facf9fc 13299+ dentry = h_parent;
13300+ if (unlikely(!h_parent || IS_ERR(h_parent))) {
13301+ AuWarn1("%s decode_fh failed, %ld\n",
13302+ au_sbtype(h_sb), PTR_ERR(h_parent));
13303+ goto out;
13304+ }
13305+ dentry = NULL;
13306+ if (unlikely(au_test_anon(h_parent))) {
13307+ AuWarn1("%s decode_fh returned a disconnected dentry\n",
13308+ au_sbtype(h_sb));
13309+ goto out_h_parent;
13310+ }
13311+
13312+ dentry = ERR_PTR(-ENOMEM);
13313+ pathname = (void *)__get_free_page(GFP_NOFS);
13314+ if (unlikely(!pathname))
13315+ goto out_h_parent;
13316+
13317+ root = sb->s_root;
13318+ path.mnt = h_mnt;
13319+ di_read_lock_parent(root, !AuLock_IR);
027c5e7a 13320+ path.dentry = au_h_dptr(root, nsi_lock->bindex);
1facf9fc 13321+ di_read_unlock(root, !AuLock_IR);
13322+ p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
13323+ dentry = (void *)p;
13324+ if (IS_ERR(p))
13325+ goto out_pathname;
13326+
13327+ si_read_unlock(sb);
13328+ err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
13329+ dentry = ERR_PTR(err);
13330+ if (unlikely(err))
13331+ goto out_relock;
13332+
13333+ dentry = ERR_PTR(-ENOENT);
13334+ AuDebugOn(au_test_anon(path.dentry));
5527c038 13335+ if (unlikely(d_really_is_negative(path.dentry)))
1facf9fc 13336+ goto out_path;
13337+
5527c038 13338+ if (ino != d_inode(path.dentry)->i_ino)
1facf9fc 13339+ dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
13340+ else
13341+ dentry = dget(path.dentry);
13342+
4f0767ce 13343+out_path:
1facf9fc 13344+ path_put(&path);
4f0767ce 13345+out_relock:
1facf9fc 13346+ if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
13347+ if (!IS_ERR(dentry)) {
13348+ dput(dentry);
13349+ dentry = ERR_PTR(-ESTALE);
13350+ }
4f0767ce 13351+out_pathname:
1c60b727 13352+ free_page((unsigned long)pathname);
4f0767ce 13353+out_h_parent:
1facf9fc 13354+ dput(h_parent);
4f0767ce 13355+out:
1facf9fc 13356+ AuTraceErrPtr(dentry);
13357+ return dentry;
13358+}
13359+
13360+/* ---------------------------------------------------------------------- */
13361+
13362+static struct dentry *
13363+aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
13364+ int fh_type)
13365+{
13366+ struct dentry *dentry;
13367+ __u32 *fh = fid->raw;
027c5e7a 13368+ struct au_branch *br;
1facf9fc 13369+ ino_t ino, dir_ino;
1facf9fc 13370+ struct au_nfsd_si_lock nsi_lock = {
1facf9fc 13371+ .force_lock = 0
13372+ };
13373+
1facf9fc 13374+ dentry = ERR_PTR(-ESTALE);
4a4d8108
AM
13375+ /* it should never happen, but the file handle is unreliable */
13376+ if (unlikely(fh_len < Fh_tail))
13377+ goto out;
13378+ nsi_lock.sigen = fh[Fh_sigen];
13379+ nsi_lock.br_id = fh[Fh_br_id];
13380+
1facf9fc 13381+ /* branch id may be wrapped around */
027c5e7a
AM
13382+ br = NULL;
13383+ if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
1facf9fc 13384+ goto out;
13385+ nsi_lock.force_lock = 1;
13386+
13387+ /* is this inode still cached? */
13388+ ino = decode_ino(fh + Fh_ino);
4a4d8108
AM
13389+ /* it should never happen */
13390+ if (unlikely(ino == AUFS_ROOT_INO))
8cdd5066 13391+ goto out_unlock;
4a4d8108 13392+
1facf9fc 13393+ dir_ino = decode_ino(fh + Fh_dir_ino);
13394+ dentry = decode_by_ino(sb, ino, dir_ino);
13395+ if (IS_ERR(dentry))
13396+ goto out_unlock;
13397+ if (dentry)
13398+ goto accept;
13399+
13400+ /* is the parent dir cached? */
027c5e7a 13401+ br = au_sbr(sb, nsi_lock.bindex);
acd2b654 13402+ au_lcnt_inc(&br->br_nfiles);
1facf9fc 13403+ dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
13404+ if (IS_ERR(dentry))
13405+ goto out_unlock;
13406+ if (dentry)
13407+ goto accept;
13408+
13409+ /* lookup path */
027c5e7a 13410+ dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
1facf9fc 13411+ if (IS_ERR(dentry))
13412+ goto out_unlock;
13413+ if (unlikely(!dentry))
13414+ /* todo?: make it ESTALE */
13415+ goto out_unlock;
13416+
4f0767ce 13417+accept:
027c5e7a 13418+ if (!au_digen_test(dentry, au_sigen(sb))
5527c038 13419+ && d_inode(dentry)->i_generation == fh[Fh_igen])
1facf9fc 13420+ goto out_unlock; /* success */
13421+
13422+ dput(dentry);
13423+ dentry = ERR_PTR(-ESTALE);
4f0767ce 13424+out_unlock:
027c5e7a 13425+ if (br)
acd2b654 13426+ au_lcnt_dec(&br->br_nfiles);
1facf9fc 13427+ si_read_unlock(sb);
4f0767ce 13428+out:
1facf9fc 13429+ AuTraceErrPtr(dentry);
13430+ return dentry;
13431+}
13432+
13433+#if 0 /* reserved for future use */
13434+/* support subtreecheck option */
13435+static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
13436+ int fh_len, int fh_type)
13437+{
13438+ struct dentry *parent;
13439+ __u32 *fh = fid->raw;
13440+ ino_t dir_ino;
13441+
13442+ dir_ino = decode_ino(fh + Fh_dir_ino);
13443+ parent = decode_by_ino(sb, dir_ino, 0);
13444+ if (IS_ERR(parent))
13445+ goto out;
13446+ if (!parent)
13447+ parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
13448+ dir_ino, fh, fh_len);
13449+
4f0767ce 13450+out:
1facf9fc 13451+ AuTraceErrPtr(parent);
13452+ return parent;
13453+}
13454+#endif
13455+
13456+/* ---------------------------------------------------------------------- */
13457+
0c3ec466
AM
13458+static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
13459+ struct inode *dir)
1facf9fc 13460+{
13461+ int err;
0c3ec466 13462+ aufs_bindex_t bindex;
1facf9fc 13463+ struct super_block *sb, *h_sb;
0c3ec466
AM
13464+ struct dentry *dentry, *parent, *h_parent;
13465+ struct inode *h_dir;
1facf9fc 13466+ struct au_branch *br;
13467+
1facf9fc 13468+ err = -ENOSPC;
13469+ if (unlikely(*max_len <= Fh_tail)) {
13470+ AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
13471+ goto out;
13472+ }
13473+
13474+ err = FILEID_ROOT;
0c3ec466
AM
13475+ if (inode->i_ino == AUFS_ROOT_INO) {
13476+ AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
1facf9fc 13477+ goto out;
13478+ }
13479+
1facf9fc 13480+ h_parent = NULL;
0c3ec466
AM
13481+ sb = inode->i_sb;
13482+ err = si_read_lock(sb, AuLock_FLUSH);
027c5e7a
AM
13483+ if (unlikely(err))
13484+ goto out;
13485+
1facf9fc 13486+#ifdef CONFIG_AUFS_DEBUG
13487+ if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13488+ AuWarn1("NFS-exporting requires xino\n");
13489+#endif
027c5e7a 13490+ err = -EIO;
0c3ec466
AM
13491+ parent = NULL;
13492+ ii_read_lock_child(inode);
5afbbe0d 13493+ bindex = au_ibtop(inode);
0c3ec466 13494+ if (!dir) {
c1595e42 13495+ dentry = d_find_any_alias(inode);
0c3ec466
AM
13496+ if (unlikely(!dentry))
13497+ goto out_unlock;
13498+ AuDebugOn(au_test_anon(dentry));
13499+ parent = dget_parent(dentry);
13500+ dput(dentry);
13501+ if (unlikely(!parent))
13502+ goto out_unlock;
5527c038
JR
13503+ if (d_really_is_positive(parent))
13504+ dir = d_inode(parent);
1facf9fc 13505+ }
0c3ec466
AM
13506+
13507+ ii_read_lock_parent(dir);
13508+ h_dir = au_h_iptr(dir, bindex);
13509+ ii_read_unlock(dir);
13510+ if (unlikely(!h_dir))
13511+ goto out_parent;
c1595e42 13512+ h_parent = d_find_any_alias(h_dir);
1facf9fc 13513+ if (unlikely(!h_parent))
0c3ec466 13514+ goto out_hparent;
1facf9fc 13515+
13516+ err = -EPERM;
13517+ br = au_sbr(sb, bindex);
86dc4139 13518+ h_sb = au_br_sb(br);
1facf9fc 13519+ if (unlikely(!h_sb->s_export_op)) {
13520+ AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
0c3ec466 13521+ goto out_hparent;
1facf9fc 13522+ }
13523+
13524+ fh[Fh_br_id] = br->br_id;
13525+ fh[Fh_sigen] = au_sigen(sb);
13526+ encode_ino(fh + Fh_ino, inode->i_ino);
0c3ec466 13527+ encode_ino(fh + Fh_dir_ino, dir->i_ino);
1facf9fc 13528+ fh[Fh_igen] = inode->i_generation;
13529+
13530+ *max_len -= Fh_tail;
13531+ fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
13532+ max_len,
13533+ /*connectable or subtreecheck*/0);
13534+ err = fh[Fh_h_type];
13535+ *max_len += Fh_tail;
13536+ /* todo: macros? */
1716fcea 13537+ if (err != FILEID_INVALID)
1facf9fc 13538+ err = 99;
13539+ else
13540+ AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
13541+
0c3ec466 13542+out_hparent:
1facf9fc 13543+ dput(h_parent);
0c3ec466 13544+out_parent:
1facf9fc 13545+ dput(parent);
0c3ec466
AM
13546+out_unlock:
13547+ ii_read_unlock(inode);
13548+ si_read_unlock(sb);
4f0767ce 13549+out:
1facf9fc 13550+ if (unlikely(err < 0))
1716fcea 13551+ err = FILEID_INVALID;
1facf9fc 13552+ return err;
13553+}
13554+
13555+/* ---------------------------------------------------------------------- */
13556+
4a4d8108
AM
13557+static int aufs_commit_metadata(struct inode *inode)
13558+{
13559+ int err;
13560+ aufs_bindex_t bindex;
13561+ struct super_block *sb;
13562+ struct inode *h_inode;
13563+ int (*f)(struct inode *inode);
13564+
13565+ sb = inode->i_sb;
e49829fe 13566+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
4a4d8108 13567+ ii_write_lock_child(inode);
5afbbe0d 13568+ bindex = au_ibtop(inode);
4a4d8108
AM
13569+ AuDebugOn(bindex < 0);
13570+ h_inode = au_h_iptr(inode, bindex);
13571+
13572+ f = h_inode->i_sb->s_export_op->commit_metadata;
13573+ if (f)
13574+ err = f(h_inode);
758e9dad
AM
13575+ else
13576+ err = sync_inode_metadata(h_inode, /*wait*/1);
4a4d8108
AM
13577+
13578+ au_cpup_attr_timesizes(inode);
13579+ ii_write_unlock(inode);
13580+ si_read_unlock(sb);
13581+ return err;
13582+}
13583+
13584+/* ---------------------------------------------------------------------- */
13585+
1facf9fc 13586+static struct export_operations aufs_export_op = {
4a4d8108 13587+ .fh_to_dentry = aufs_fh_to_dentry,
1facf9fc 13588+ /* .fh_to_parent = aufs_fh_to_parent, */
4a4d8108
AM
13589+ .encode_fh = aufs_encode_fh,
13590+ .commit_metadata = aufs_commit_metadata
1facf9fc 13591+};
13592+
13593+void au_export_init(struct super_block *sb)
13594+{
13595+ struct au_sbinfo *sbinfo;
13596+ __u32 u;
13597+
5afbbe0d
AM
13598+ BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
13599+ && IS_MODULE(CONFIG_EXPORTFS),
13600+ AUFS_NAME ": unsupported configuration "
13601+ "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
13602+
1facf9fc 13603+ sb->s_export_op = &aufs_export_op;
13604+ sbinfo = au_sbi(sb);
13605+ sbinfo->si_xigen = NULL;
13606+ get_random_bytes(&u, sizeof(u));
13607+ BUILD_BUG_ON(sizeof(u) != sizeof(int));
13608+ atomic_set(&sbinfo->si_xigen_next, u);
13609+}
076b876e 13610diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
eca34b5c 13611--- /usr/share/empty/fs/aufs/fhsm.c 1970-01-01 01:00:00.000000000 +0100
594d0238 13612+++ linux/fs/aufs/fhsm.c 2022-11-05 23:02:18.962555950 +0100
0dcfbb52 13613@@ -0,0 +1,426 @@
cd7a4cd9 13614+// SPDX-License-Identifier: GPL-2.0
076b876e 13615+/*
0dcfbb52 13616+ * Copyright (C) 2011-2022 Junjiro R. Okajima
076b876e 13617+ *
0dcfbb52 13618+ * This program is free software; you can redistribute it and/or modify
076b876e
AM
13619+ * it under the terms of the GNU General Public License as published by
13620+ * the Free Software Foundation; either version 2 of the License, or
13621+ * (at your option) any later version.
13622+ *
13623+ * This program is distributed in the hope that it will be useful,
13624+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13625+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13626+ * GNU General Public License for more details.
13627+ *
13628+ * You should have received a copy of the GNU General Public License
0dcfbb52 13629+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
076b876e
AM
13630+ */
13631+
13632+/*
13633+ * File-based Hierarchy Storage Management
13634+ */
13635+
13636+#include <linux/anon_inodes.h>
13637+#include <linux/poll.h>
13638+#include <linux/seq_file.h>
13639+#include <linux/statfs.h>
13640+#include "aufs.h"
13641+
c1595e42
JR
13642+static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
13643+{
13644+ struct au_sbinfo *sbinfo;
13645+ struct au_fhsm *fhsm;
13646+
13647+ SiMustAnyLock(sb);
13648+
13649+ sbinfo = au_sbi(sb);
13650+ fhsm = &sbinfo->si_fhsm;
13651+ AuDebugOn(!fhsm);
13652+ return fhsm->fhsm_bottom;
13653+}
13654+
13655+void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
13656+{
13657+ struct au_sbinfo *sbinfo;
13658+ struct au_fhsm *fhsm;
13659+
13660+ SiMustWriteLock(sb);
13661+
13662+ sbinfo = au_sbi(sb);
13663+ fhsm = &sbinfo->si_fhsm;
13664+ AuDebugOn(!fhsm);
13665+ fhsm->fhsm_bottom = bindex;
13666+}
13667+
13668+/* ---------------------------------------------------------------------- */
13669+
076b876e
AM
13670+static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
13671+{
13672+ struct au_br_fhsm *bf;
13673+
13674+ bf = br->br_fhsm;
13675+ MtxMustLock(&bf->bf_lock);
13676+
13677+ return !bf->bf_readable
13678+ || time_after(jiffies,
13679+ bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
13680+}
13681+
13682+/* ---------------------------------------------------------------------- */
13683+
13684+static void au_fhsm_notify(struct super_block *sb, int val)
13685+{
13686+ struct au_sbinfo *sbinfo;
13687+ struct au_fhsm *fhsm;
13688+
13689+ SiMustAnyLock(sb);
13690+
13691+ sbinfo = au_sbi(sb);
13692+ fhsm = &sbinfo->si_fhsm;
13693+ if (au_fhsm_pid(fhsm)
13694+ && atomic_read(&fhsm->fhsm_readable) != -1) {
13695+ atomic_set(&fhsm->fhsm_readable, val);
13696+ if (val)
13697+ wake_up(&fhsm->fhsm_wqh);
13698+ }
13699+}
13700+
13701+static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
13702+ struct aufs_stfs *rstfs, int do_lock, int do_notify)
13703+{
13704+ int err;
13705+ struct au_branch *br;
13706+ struct au_br_fhsm *bf;
13707+
13708+ br = au_sbr(sb, bindex);
13709+ AuDebugOn(au_br_rdonly(br));
13710+ bf = br->br_fhsm;
13711+ AuDebugOn(!bf);
13712+
13713+ if (do_lock)
13714+ mutex_lock(&bf->bf_lock);
13715+ else
13716+ MtxMustLock(&bf->bf_lock);
13717+
13718+ /* sb->s_root for NFS is unreliable */
13719+ err = au_br_stfs(br, &bf->bf_stfs);
13720+ if (unlikely(err)) {
13721+ AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
13722+ goto out;
13723+ }
13724+
13725+ bf->bf_jiffy = jiffies;
13726+ bf->bf_readable = 1;
13727+ if (do_notify)
13728+ au_fhsm_notify(sb, /*val*/1);
13729+ if (rstfs)
13730+ *rstfs = bf->bf_stfs;
13731+
13732+out:
13733+ if (do_lock)
13734+ mutex_unlock(&bf->bf_lock);
13735+ au_fhsm_notify(sb, /*val*/1);
13736+
13737+ return err;
13738+}
13739+
13740+void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
13741+{
13742+ int err;
076b876e
AM
13743+ struct au_sbinfo *sbinfo;
13744+ struct au_fhsm *fhsm;
13745+ struct au_branch *br;
13746+ struct au_br_fhsm *bf;
13747+
13748+ AuDbg("b%d, force %d\n", bindex, force);
13749+ SiMustAnyLock(sb);
13750+
13751+ sbinfo = au_sbi(sb);
13752+ fhsm = &sbinfo->si_fhsm;
c1595e42
JR
13753+ if (!au_ftest_si(sbinfo, FHSM)
13754+ || fhsm->fhsm_bottom == bindex)
076b876e
AM
13755+ return;
13756+
13757+ br = au_sbr(sb, bindex);
13758+ bf = br->br_fhsm;
13759+ AuDebugOn(!bf);
13760+ mutex_lock(&bf->bf_lock);
13761+ if (force
13762+ || au_fhsm_pid(fhsm)
13763+ || au_fhsm_test_jiffy(sbinfo, br))
13764+ err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
13765+ /*do_notify*/1);
13766+ mutex_unlock(&bf->bf_lock);
13767+}
13768+
13769+void au_fhsm_wrote_all(struct super_block *sb, int force)
13770+{
5afbbe0d 13771+ aufs_bindex_t bindex, bbot;
076b876e
AM
13772+ struct au_branch *br;
13773+
13774+ /* exclude the bottom */
5afbbe0d
AM
13775+ bbot = au_fhsm_bottom(sb);
13776+ for (bindex = 0; bindex < bbot; bindex++) {
076b876e
AM
13777+ br = au_sbr(sb, bindex);
13778+ if (au_br_fhsm(br->br_perm))
13779+ au_fhsm_wrote(sb, bindex, force);
13780+ }
13781+}
13782+
13783+/* ---------------------------------------------------------------------- */
13784+
be118d29 13785+static __poll_t au_fhsm_poll(struct file *file, struct poll_table_struct *wait)
076b876e 13786+{
be118d29 13787+ __poll_t mask;
076b876e
AM
13788+ struct au_sbinfo *sbinfo;
13789+ struct au_fhsm *fhsm;
13790+
13791+ mask = 0;
13792+ sbinfo = file->private_data;
13793+ fhsm = &sbinfo->si_fhsm;
13794+ poll_wait(file, &fhsm->fhsm_wqh, wait);
13795+ if (atomic_read(&fhsm->fhsm_readable))
be118d29 13796+ mask = EPOLLIN /* | EPOLLRDNORM */;
076b876e 13797+
b00004a5
AM
13798+ if (!mask)
13799+ AuDbg("mask 0x%x\n", mask);
076b876e
AM
13800+ return mask;
13801+}
13802+
13803+static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
13804+ struct aufs_stfs *stfs, __s16 brid)
13805+{
13806+ int err;
13807+
13808+ err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
13809+ if (!err)
13810+ err = __put_user(brid, &stbr->brid);
13811+ if (unlikely(err))
13812+ err = -EFAULT;
13813+
13814+ return err;
13815+}
13816+
13817+static ssize_t au_fhsm_do_read(struct super_block *sb,
13818+ struct aufs_stbr __user *stbr, size_t count)
13819+{
13820+ ssize_t err;
13821+ int nstbr;
5afbbe0d 13822+ aufs_bindex_t bindex, bbot;
076b876e
AM
13823+ struct au_branch *br;
13824+ struct au_br_fhsm *bf;
13825+
13826+ /* except the bottom branch */
13827+ err = 0;
13828+ nstbr = 0;
5afbbe0d
AM
13829+ bbot = au_fhsm_bottom(sb);
13830+ for (bindex = 0; !err && bindex < bbot; bindex++) {
076b876e
AM
13831+ br = au_sbr(sb, bindex);
13832+ if (!au_br_fhsm(br->br_perm))
13833+ continue;
13834+
13835+ bf = br->br_fhsm;
13836+ mutex_lock(&bf->bf_lock);
13837+ if (bf->bf_readable) {
13838+ err = -EFAULT;
13839+ if (count >= sizeof(*stbr))
13840+ err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
13841+ br->br_id);
13842+ if (!err) {
13843+ bf->bf_readable = 0;
13844+ count -= sizeof(*stbr);
13845+ nstbr++;
13846+ }
13847+ }
13848+ mutex_unlock(&bf->bf_lock);
13849+ }
13850+ if (!err)
13851+ err = sizeof(*stbr) * nstbr;
13852+
13853+ return err;
13854+}
13855+
13856+static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
13857+ loff_t *pos)
13858+{
13859+ ssize_t err;
13860+ int readable;
5afbbe0d 13861+ aufs_bindex_t nfhsm, bindex, bbot;
076b876e
AM
13862+ struct au_sbinfo *sbinfo;
13863+ struct au_fhsm *fhsm;
13864+ struct au_branch *br;
13865+ struct super_block *sb;
13866+
13867+ err = 0;
13868+ sbinfo = file->private_data;
13869+ fhsm = &sbinfo->si_fhsm;
13870+need_data:
13871+ spin_lock_irq(&fhsm->fhsm_wqh.lock);
13872+ if (!atomic_read(&fhsm->fhsm_readable)) {
13873+ if (vfsub_file_flags(file) & O_NONBLOCK)
13874+ err = -EAGAIN;
13875+ else
13876+ err = wait_event_interruptible_locked_irq
13877+ (fhsm->fhsm_wqh,
13878+ atomic_read(&fhsm->fhsm_readable));
13879+ }
13880+ spin_unlock_irq(&fhsm->fhsm_wqh.lock);
13881+ if (unlikely(err))
13882+ goto out;
13883+
13884+ /* sb may already be dead */
13885+ au_rw_read_lock(&sbinfo->si_rwsem);
13886+ readable = atomic_read(&fhsm->fhsm_readable);
13887+ if (readable > 0) {
13888+ sb = sbinfo->si_sb;
13889+ AuDebugOn(!sb);
13890+ /* exclude the bottom branch */
13891+ nfhsm = 0;
5afbbe0d
AM
13892+ bbot = au_fhsm_bottom(sb);
13893+ for (bindex = 0; bindex < bbot; bindex++) {
076b876e
AM
13894+ br = au_sbr(sb, bindex);
13895+ if (au_br_fhsm(br->br_perm))
13896+ nfhsm++;
13897+ }
13898+ err = -EMSGSIZE;
13899+ if (nfhsm * sizeof(struct aufs_stbr) <= count) {
13900+ atomic_set(&fhsm->fhsm_readable, 0);
13901+ err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
13902+ count);
13903+ }
13904+ }
13905+ au_rw_read_unlock(&sbinfo->si_rwsem);
13906+ if (!readable)
13907+ goto need_data;
13908+
13909+out:
13910+ return err;
13911+}
13912+
13913+static int au_fhsm_release(struct inode *inode, struct file *file)
13914+{
13915+ struct au_sbinfo *sbinfo;
13916+ struct au_fhsm *fhsm;
13917+
13918+ /* sb may already be dead */
13919+ sbinfo = file->private_data;
13920+ fhsm = &sbinfo->si_fhsm;
13921+ spin_lock(&fhsm->fhsm_spin);
13922+ fhsm->fhsm_pid = 0;
13923+ spin_unlock(&fhsm->fhsm_spin);
13924+ kobject_put(&sbinfo->si_kobj);
13925+
13926+ return 0;
13927+}
13928+
13929+static const struct file_operations au_fhsm_fops = {
13930+ .owner = THIS_MODULE,
13931+ .llseek = noop_llseek,
13932+ .read = au_fhsm_read,
13933+ .poll = au_fhsm_poll,
13934+ .release = au_fhsm_release
13935+};
13936+
13937+int au_fhsm_fd(struct super_block *sb, int oflags)
13938+{
13939+ int err, fd;
13940+ struct au_sbinfo *sbinfo;
13941+ struct au_fhsm *fhsm;
13942+
13943+ err = -EPERM;
13944+ if (unlikely(!capable(CAP_SYS_ADMIN)))
13945+ goto out;
13946+
13947+ err = -EINVAL;
13948+ if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
13949+ goto out;
13950+
13951+ err = 0;
13952+ sbinfo = au_sbi(sb);
13953+ fhsm = &sbinfo->si_fhsm;
13954+ spin_lock(&fhsm->fhsm_spin);
13955+ if (!fhsm->fhsm_pid)
13956+ fhsm->fhsm_pid = current->pid;
13957+ else
13958+ err = -EBUSY;
13959+ spin_unlock(&fhsm->fhsm_spin);
13960+ if (unlikely(err))
13961+ goto out;
13962+
13963+ oflags |= O_RDONLY;
13964+ /* oflags |= FMODE_NONOTIFY; */
13965+ fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
13966+ err = fd;
13967+ if (unlikely(fd < 0))
13968+ goto out_pid;
13969+
acd2b654 13970+ /* succeed regardless 'fhsm' status */
076b876e
AM
13971+ kobject_get(&sbinfo->si_kobj);
13972+ si_noflush_read_lock(sb);
13973+ if (au_ftest_si(sbinfo, FHSM))
13974+ au_fhsm_wrote_all(sb, /*force*/0);
13975+ si_read_unlock(sb);
13976+ goto out; /* success */
13977+
13978+out_pid:
13979+ spin_lock(&fhsm->fhsm_spin);
13980+ fhsm->fhsm_pid = 0;
13981+ spin_unlock(&fhsm->fhsm_spin);
13982+out:
13983+ AuTraceErr(err);
13984+ return err;
13985+}
13986+
13987+/* ---------------------------------------------------------------------- */
13988+
13989+int au_fhsm_br_alloc(struct au_branch *br)
13990+{
13991+ int err;
13992+
13993+ err = 0;
13994+ br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
13995+ if (br->br_fhsm)
13996+ au_br_fhsm_init(br->br_fhsm);
13997+ else
13998+ err = -ENOMEM;
13999+
14000+ return err;
14001+}
14002+
14003+/* ---------------------------------------------------------------------- */
14004+
14005+void au_fhsm_fin(struct super_block *sb)
14006+{
14007+ au_fhsm_notify(sb, /*val*/-1);
14008+}
14009+
14010+void au_fhsm_init(struct au_sbinfo *sbinfo)
14011+{
14012+ struct au_fhsm *fhsm;
14013+
14014+ fhsm = &sbinfo->si_fhsm;
14015+ spin_lock_init(&fhsm->fhsm_spin);
14016+ init_waitqueue_head(&fhsm->fhsm_wqh);
14017+ atomic_set(&fhsm->fhsm_readable, 0);
14018+ fhsm->fhsm_expire
14019+ = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
c1595e42 14020+ fhsm->fhsm_bottom = -1;
076b876e
AM
14021+}
14022+
14023+void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
14024+{
14025+ sbinfo->si_fhsm.fhsm_expire
14026+ = msecs_to_jiffies(sec * MSEC_PER_SEC);
14027+}
14028+
14029+void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
14030+{
14031+ unsigned int u;
14032+
14033+ if (!au_ftest_si(sbinfo, FHSM))
14034+ return;
14035+
14036+ u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
14037+ if (u != AUFS_FHSM_CACHE_DEF_SEC)
14038+ seq_printf(seq, ",fhsm_sec=%u", u);
14039+}
7f207e10 14040diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
eca34b5c 14041--- /usr/share/empty/fs/aufs/file.c 1970-01-01 01:00:00.000000000 +0100
631230db 14042+++ linux/fs/aufs/file.c 2023-10-31 09:31:04.199880750 +0100
171c7011 14043@@ -0,0 +1,865 @@
cd7a4cd9 14044+// SPDX-License-Identifier: GPL-2.0
1facf9fc 14045+/*
0dcfbb52 14046+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 14047+ *
0dcfbb52 14048+ * This program is free software; you can redistribute it and/or modify
1facf9fc 14049+ * it under the terms of the GNU General Public License as published by
14050+ * the Free Software Foundation; either version 2 of the License, or
14051+ * (at your option) any later version.
dece6358
AM
14052+ *
14053+ * This program is distributed in the hope that it will be useful,
14054+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14055+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14056+ * GNU General Public License for more details.
14057+ *
14058+ * You should have received a copy of the GNU General Public License
523b37e3 14059+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 14060+ */
14061+
14062+/*
4a4d8108 14063+ * handling file/dir, and address_space operation
1facf9fc 14064+ */
14065+
7eafdf33
AM
14066+#ifdef CONFIG_AUFS_DEBUG
14067+#include <linux/migrate.h>
14068+#endif
4a4d8108 14069+#include <linux/pagemap.h>
1facf9fc 14070+#include "aufs.h"
14071+
4a4d8108
AM
14072+/* drop flags for writing */
14073+unsigned int au_file_roflags(unsigned int flags)
14074+{
14075+ flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
14076+ flags |= O_RDONLY | O_NOATIME;
14077+ return flags;
14078+}
14079+
14080+/* common functions to regular file and dir */
14081+struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
392086de 14082+ struct file *file, int force_wr)
1facf9fc 14083+{
1308ab2a 14084+ struct file *h_file;
4a4d8108
AM
14085+ struct dentry *h_dentry;
14086+ struct inode *h_inode;
14087+ struct super_block *sb;
14088+ struct au_branch *br;
14089+ struct path h_path;
b912730e 14090+ int err;
1facf9fc 14091+
4a4d8108
AM
14092+ /* a race condition can happen between open and unlink/rmdir */
14093+ h_file = ERR_PTR(-ENOENT);
14094+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 14095+ if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
4a4d8108 14096+ goto out;
5527c038 14097+ h_inode = d_inode(h_dentry);
027c5e7a
AM
14098+ spin_lock(&h_dentry->d_lock);
14099+ err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
5527c038 14100+ /* || !d_inode(dentry)->i_nlink */
027c5e7a
AM
14101+ ;
14102+ spin_unlock(&h_dentry->d_lock);
14103+ if (unlikely(err))
4a4d8108 14104+ goto out;
1facf9fc 14105+
4a4d8108
AM
14106+ sb = dentry->d_sb;
14107+ br = au_sbr(sb, bindex);
b912730e
AM
14108+ err = au_br_test_oflag(flags, br);
14109+ h_file = ERR_PTR(err);
14110+ if (unlikely(err))
027c5e7a 14111+ goto out;
1facf9fc 14112+
4a4d8108 14113+ /* drop flags for writing */
5527c038 14114+ if (au_test_ro(sb, bindex, d_inode(dentry))) {
392086de
AM
14115+ if (force_wr && !(flags & O_WRONLY))
14116+ force_wr = 0;
4a4d8108 14117+ flags = au_file_roflags(flags);
392086de
AM
14118+ if (force_wr) {
14119+ h_file = ERR_PTR(-EROFS);
14120+ flags = au_file_roflags(flags);
14121+ if (unlikely(vfsub_native_ro(h_inode)
14122+ || IS_APPEND(h_inode)))
14123+ goto out;
14124+ flags &= ~O_ACCMODE;
14125+ flags |= O_WRONLY;
14126+ }
14127+ }
4a4d8108 14128+ flags &= ~O_CREAT;
acd2b654 14129+ au_lcnt_inc(&br->br_nfiles);
4a4d8108 14130+ h_path.dentry = h_dentry;
86dc4139 14131+ h_path.mnt = au_br_mnt(br);
171c7011
AM
14132+ /*
14133+ * vfs::backing_file_open() looks promising since it can get rid of
14134+ * mm::vm_prfile approach from my mind.
14135+ * but I keep current approach for a while.
14136+ */
38d290e6 14137+ h_file = vfsub_dentry_open(&h_path, flags);
4a4d8108
AM
14138+ if (IS_ERR(h_file))
14139+ goto out_br;
dece6358 14140+
b912730e 14141+ if (flags & __FMODE_EXEC) {
4a4d8108
AM
14142+ err = deny_write_access(h_file);
14143+ if (unlikely(err)) {
14144+ fput(h_file);
14145+ h_file = ERR_PTR(err);
14146+ goto out_br;
14147+ }
14148+ }
953406b4 14149+ fsnotify_open(h_file);
4a4d8108 14150+ goto out; /* success */
1facf9fc 14151+
4f0767ce 14152+out_br:
acd2b654 14153+ au_lcnt_dec(&br->br_nfiles);
4f0767ce 14154+out:
4a4d8108
AM
14155+ return h_file;
14156+}
1308ab2a 14157+
076b876e
AM
14158+static int au_cmoo(struct dentry *dentry)
14159+{
8b6a4947 14160+ int err, cmoo, matched;
076b876e
AM
14161+ unsigned int udba;
14162+ struct path h_path;
14163+ struct au_pin pin;
14164+ struct au_cp_generic cpg = {
14165+ .dentry = dentry,
14166+ .bdst = -1,
14167+ .bsrc = -1,
14168+ .len = -1,
14169+ .pin = &pin,
14170+ .flags = AuCpup_DTIME | AuCpup_HOPEN
14171+ };
7e9cd9fe 14172+ struct inode *delegated;
076b876e
AM
14173+ struct super_block *sb;
14174+ struct au_sbinfo *sbinfo;
14175+ struct au_fhsm *fhsm;
14176+ pid_t pid;
14177+ struct au_branch *br;
14178+ struct dentry *parent;
14179+ struct au_hinode *hdir;
14180+
14181+ DiMustWriteLock(dentry);
5527c038 14182+ IiMustWriteLock(d_inode(dentry));
076b876e
AM
14183+
14184+ err = 0;
14185+ if (IS_ROOT(dentry))
14186+ goto out;
5afbbe0d 14187+ cpg.bsrc = au_dbtop(dentry);
076b876e
AM
14188+ if (!cpg.bsrc)
14189+ goto out;
14190+
14191+ sb = dentry->d_sb;
14192+ sbinfo = au_sbi(sb);
14193+ fhsm = &sbinfo->si_fhsm;
14194+ pid = au_fhsm_pid(fhsm);
8b6a4947
AM
14195+ rcu_read_lock();
14196+ matched = (pid
14197+ && (current->pid == pid
14198+ || rcu_dereference(current->real_parent)->pid == pid));
14199+ rcu_read_unlock();
14200+ if (matched)
076b876e
AM
14201+ goto out;
14202+
14203+ br = au_sbr(sb, cpg.bsrc);
14204+ cmoo = au_br_cmoo(br->br_perm);
14205+ if (!cmoo)
14206+ goto out;
7e9cd9fe 14207+ if (!d_is_reg(dentry))
076b876e
AM
14208+ cmoo &= AuBrAttr_COO_ALL;
14209+ if (!cmoo)
14210+ goto out;
14211+
14212+ parent = dget_parent(dentry);
14213+ di_write_lock_parent(parent);
14214+ err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
14215+ cpg.bdst = err;
14216+ if (unlikely(err < 0)) {
14217+ err = 0; /* there is no upper writable branch */
14218+ goto out_dgrade;
14219+ }
14220+ AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
14221+
14222+ /* do not respect the coo attrib for the target branch */
14223+ err = au_cpup_dirs(dentry, cpg.bdst);
14224+ if (unlikely(err))
14225+ goto out_dgrade;
14226+
14227+ di_downgrade_lock(parent, AuLock_IR);
14228+ udba = au_opt_udba(sb);
14229+ err = au_pin(&pin, dentry, cpg.bdst, udba,
14230+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14231+ if (unlikely(err))
14232+ goto out_parent;
14233+
14234+ err = au_sio_cpup_simple(&cpg);
14235+ au_unpin(&pin);
14236+ if (unlikely(err))
14237+ goto out_parent;
14238+ if (!(cmoo & AuBrWAttr_MOO))
14239+ goto out_parent; /* success */
14240+
14241+ err = au_pin(&pin, dentry, cpg.bsrc, udba,
14242+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14243+ if (unlikely(err))
14244+ goto out_parent;
14245+
14246+ h_path.mnt = au_br_mnt(br);
14247+ h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
5527c038 14248+ hdir = au_hi(d_inode(parent), cpg.bsrc);
076b876e
AM
14249+ delegated = NULL;
14250+ err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
14251+ au_unpin(&pin);
14252+ /* todo: keep h_dentry or not? */
14253+ if (unlikely(err == -EWOULDBLOCK)) {
14254+ pr_warn("cannot retry for NFSv4 delegation"
14255+ " for an internal unlink\n");
14256+ iput(delegated);
14257+ }
14258+ if (unlikely(err)) {
14259+ pr_err("unlink %pd after coo failed (%d), ignored\n",
14260+ dentry, err);
14261+ err = 0;
14262+ }
14263+ goto out_parent; /* success */
14264+
14265+out_dgrade:
14266+ di_downgrade_lock(parent, AuLock_IR);
14267+out_parent:
14268+ di_read_unlock(parent, AuLock_IR);
14269+ dput(parent);
14270+out:
14271+ AuTraceErr(err);
14272+ return err;
14273+}
14274+
b912730e 14275+int au_do_open(struct file *file, struct au_do_open_args *args)
1facf9fc 14276+{
8b6a4947 14277+ int err, aopen = args->aopen;
1facf9fc 14278+ struct dentry *dentry;
076b876e 14279+ struct au_finfo *finfo;
1308ab2a 14280+
8b6a4947 14281+ if (!aopen)
b912730e
AM
14282+ err = au_finfo_init(file, args->fidir);
14283+ else {
14284+ lockdep_off();
14285+ err = au_finfo_init(file, args->fidir);
14286+ lockdep_on();
14287+ }
4a4d8108
AM
14288+ if (unlikely(err))
14289+ goto out;
1facf9fc 14290+
2000de60 14291+ dentry = file->f_path.dentry;
b912730e 14292+ AuDebugOn(IS_ERR_OR_NULL(dentry));
8b6a4947
AM
14293+ di_write_lock_child(dentry);
14294+ err = au_cmoo(dentry);
8b6a4947 14295+ if (!err) {
2255d0fe
AM
14296+ if (!aopen) {
14297+ err = args->open(file, vfsub_file_flags(file),
14298+ au_di(dentry)->di_htmpfile);
14299+ di_write_unlock(dentry);
14300+ } else {
14301+ di_downgrade_lock(dentry, AuLock_IR);
8b6a4947 14302+ lockdep_off();
acd2b654
AM
14303+ err = args->open(file, vfsub_file_flags(file),
14304+ args->h_file);
8b6a4947 14305+ lockdep_on();
2255d0fe 14306+ di_read_unlock(dentry, AuLock_IR);
8b6a4947 14307+ }
b912730e 14308+ }
1facf9fc 14309+
076b876e
AM
14310+ finfo = au_fi(file);
14311+ if (!err) {
14312+ finfo->fi_file = file;
8b6a4947
AM
14313+ au_hbl_add(&finfo->fi_hlist,
14314+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
076b876e 14315+ }
8b6a4947 14316+ if (!aopen)
b912730e
AM
14317+ fi_write_unlock(file);
14318+ else {
14319+ lockdep_off();
14320+ fi_write_unlock(file);
14321+ lockdep_on();
14322+ }
4a4d8108 14323+ if (unlikely(err)) {
076b876e 14324+ finfo->fi_hdir = NULL;
1c60b727 14325+ au_finfo_fin(file);
1308ab2a 14326+ }
4a4d8108 14327+
4f0767ce 14328+out:
8b6a4947 14329+ AuTraceErr(err);
1308ab2a 14330+ return err;
14331+}
dece6358 14332+
4a4d8108 14333+int au_reopen_nondir(struct file *file)
1308ab2a 14334+{
4a4d8108 14335+ int err;
5afbbe0d 14336+ aufs_bindex_t btop;
4a4d8108 14337+ struct dentry *dentry;
acd2b654 14338+ struct au_branch *br;
4a4d8108 14339+ struct file *h_file, *h_file_tmp;
1308ab2a 14340+
2000de60 14341+ dentry = file->f_path.dentry;
5afbbe0d 14342+ btop = au_dbtop(dentry);
acd2b654 14343+ br = au_sbr(dentry->d_sb, btop);
4a4d8108 14344+ h_file_tmp = NULL;
5afbbe0d 14345+ if (au_fbtop(file) == btop) {
4a4d8108
AM
14346+ h_file = au_hf_top(file);
14347+ if (file->f_mode == h_file->f_mode)
14348+ return 0; /* success */
14349+ h_file_tmp = h_file;
14350+ get_file(h_file_tmp);
acd2b654 14351+ au_lcnt_inc(&br->br_nfiles);
5afbbe0d 14352+ au_set_h_fptr(file, btop, NULL);
4a4d8108
AM
14353+ }
14354+ AuDebugOn(au_fi(file)->fi_hdir);
86dc4139
AM
14355+ /*
14356+ * it can happen
14357+ * file exists on both of rw and ro
5afbbe0d 14358+ * open --> dbtop and fbtop are both 0
86dc4139
AM
14359+ * prepend a branch as rw, "rw" become ro
14360+ * remove rw/file
14361+ * delete the top branch, "rw" becomes rw again
5afbbe0d
AM
14362+ * --> dbtop is 1, fbtop is still 0
14363+ * write --> fbtop is 0 but dbtop is 1
86dc4139 14364+ */
5afbbe0d 14365+ /* AuDebugOn(au_fbtop(file) < btop); */
1308ab2a 14366+
5afbbe0d 14367+ h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
392086de 14368+ file, /*force_wr*/0);
4a4d8108 14369+ err = PTR_ERR(h_file);
86dc4139
AM
14370+ if (IS_ERR(h_file)) {
14371+ if (h_file_tmp) {
acd2b654 14372+ /* revert */
5afbbe0d 14373+ au_set_h_fptr(file, btop, h_file_tmp);
86dc4139
AM
14374+ h_file_tmp = NULL;
14375+ }
4a4d8108 14376+ goto out; /* todo: close all? */
86dc4139 14377+ }
4a4d8108
AM
14378+
14379+ err = 0;
5afbbe0d
AM
14380+ au_set_fbtop(file, btop);
14381+ au_set_h_fptr(file, btop, h_file);
4a4d8108
AM
14382+ au_update_figen(file);
14383+ /* todo: necessary? */
14384+ /* file->f_ra = h_file->f_ra; */
14385+
4f0767ce 14386+out:
acd2b654 14387+ if (h_file_tmp) {
4a4d8108 14388+ fput(h_file_tmp);
acd2b654
AM
14389+ au_lcnt_dec(&br->br_nfiles);
14390+ }
4a4d8108 14391+ return err;
1facf9fc 14392+}
14393+
1308ab2a 14394+/* ---------------------------------------------------------------------- */
14395+
4a4d8108
AM
14396+static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
14397+ struct dentry *hi_wh)
1facf9fc 14398+{
4a4d8108 14399+ int err;
5afbbe0d 14400+ aufs_bindex_t btop;
4a4d8108
AM
14401+ struct au_dinfo *dinfo;
14402+ struct dentry *h_dentry;
14403+ struct au_hdentry *hdp;
1facf9fc 14404+
2000de60 14405+ dinfo = au_di(file->f_path.dentry);
4a4d8108 14406+ AuRwMustWriteLock(&dinfo->di_rwsem);
dece6358 14407+
5afbbe0d
AM
14408+ btop = dinfo->di_btop;
14409+ dinfo->di_btop = btgt;
14410+ hdp = au_hdentry(dinfo, btgt);
14411+ h_dentry = hdp->hd_dentry;
14412+ hdp->hd_dentry = hi_wh;
4a4d8108 14413+ err = au_reopen_nondir(file);
5afbbe0d
AM
14414+ hdp->hd_dentry = h_dentry;
14415+ dinfo->di_btop = btop;
1facf9fc 14416+
1facf9fc 14417+ return err;
14418+}
14419+
4a4d8108 14420+static int au_ready_to_write_wh(struct file *file, loff_t len,
86dc4139 14421+ aufs_bindex_t bcpup, struct au_pin *pin)
1facf9fc 14422+{
4a4d8108 14423+ int err;
027c5e7a 14424+ struct inode *inode, *h_inode;
c2b27bf2
AM
14425+ struct dentry *h_dentry, *hi_wh;
14426+ struct au_cp_generic cpg = {
2000de60 14427+ .dentry = file->f_path.dentry,
c2b27bf2
AM
14428+ .bdst = bcpup,
14429+ .bsrc = -1,
14430+ .len = len,
14431+ .pin = pin
14432+ };
1facf9fc 14433+
5afbbe0d 14434+ au_update_dbtop(cpg.dentry);
5527c038 14435+ inode = d_inode(cpg.dentry);
027c5e7a 14436+ h_inode = NULL;
5afbbe0d
AM
14437+ if (au_dbtop(cpg.dentry) <= bcpup
14438+ && au_dbbot(cpg.dentry) >= bcpup) {
c2b27bf2 14439+ h_dentry = au_h_dptr(cpg.dentry, bcpup);
5527c038
JR
14440+ if (h_dentry && d_is_positive(h_dentry))
14441+ h_inode = d_inode(h_dentry);
027c5e7a 14442+ }
4a4d8108 14443+ hi_wh = au_hi_wh(inode, bcpup);
027c5e7a 14444+ if (!hi_wh && !h_inode)
c2b27bf2 14445+ err = au_sio_cpup_wh(&cpg, file);
4a4d8108
AM
14446+ else
14447+ /* already copied-up after unlink */
14448+ err = au_reopen_wh(file, bcpup, hi_wh);
1facf9fc 14449+
4a4d8108 14450+ if (!err
38d290e6
JR
14451+ && (inode->i_nlink > 1
14452+ || (inode->i_state & I_LINKABLE))
c2b27bf2
AM
14453+ && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
14454+ au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
1308ab2a 14455+
dece6358 14456+ return err;
1facf9fc 14457+}
14458+
4a4d8108
AM
14459+/*
14460+ * prepare the @file for writing.
14461+ */
14462+int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
1facf9fc 14463+{
4a4d8108 14464+ int err;
5afbbe0d 14465+ aufs_bindex_t dbtop;
c1595e42 14466+ struct dentry *parent;
86dc4139 14467+ struct inode *inode;
1facf9fc 14468+ struct super_block *sb;
4a4d8108 14469+ struct file *h_file;
c2b27bf2 14470+ struct au_cp_generic cpg = {
2000de60 14471+ .dentry = file->f_path.dentry,
c2b27bf2
AM
14472+ .bdst = -1,
14473+ .bsrc = -1,
14474+ .len = len,
14475+ .pin = pin,
14476+ .flags = AuCpup_DTIME
14477+ };
1facf9fc 14478+
c2b27bf2 14479+ sb = cpg.dentry->d_sb;
5527c038 14480+ inode = d_inode(cpg.dentry);
5afbbe0d 14481+ cpg.bsrc = au_fbtop(file);
c2b27bf2 14482+ err = au_test_ro(sb, cpg.bsrc, inode);
4a4d8108 14483+ if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
c2b27bf2
AM
14484+ err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
14485+ /*flags*/0);
1facf9fc 14486+ goto out;
4a4d8108 14487+ }
1facf9fc 14488+
027c5e7a 14489+ /* need to cpup or reopen */
c2b27bf2 14490+ parent = dget_parent(cpg.dentry);
4a4d8108 14491+ di_write_lock_parent(parent);
c2b27bf2
AM
14492+ err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14493+ cpg.bdst = err;
4a4d8108
AM
14494+ if (unlikely(err < 0))
14495+ goto out_dgrade;
14496+ err = 0;
14497+
c2b27bf2
AM
14498+ if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
14499+ err = au_cpup_dirs(cpg.dentry, cpg.bdst);
1facf9fc 14500+ if (unlikely(err))
4a4d8108
AM
14501+ goto out_dgrade;
14502+ }
14503+
c2b27bf2 14504+ err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
4a4d8108
AM
14505+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14506+ if (unlikely(err))
14507+ goto out_dgrade;
14508+
5afbbe0d
AM
14509+ dbtop = au_dbtop(cpg.dentry);
14510+ if (dbtop <= cpg.bdst)
c2b27bf2 14511+ cpg.bsrc = cpg.bdst;
027c5e7a 14512+
5afbbe0d 14513+ if (dbtop <= cpg.bdst /* just reopen */
c2b27bf2 14514+ || !d_unhashed(cpg.dentry) /* copyup and reopen */
027c5e7a 14515+ ) {
392086de 14516+ h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
86dc4139 14517+ if (IS_ERR(h_file))
027c5e7a 14518+ err = PTR_ERR(h_file);
86dc4139 14519+ else {
027c5e7a 14520+ di_downgrade_lock(parent, AuLock_IR);
5afbbe0d 14521+ if (dbtop > cpg.bdst)
c2b27bf2 14522+ err = au_sio_cpup_simple(&cpg);
027c5e7a
AM
14523+ if (!err)
14524+ err = au_reopen_nondir(file);
c2b27bf2 14525+ au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
027c5e7a 14526+ }
027c5e7a
AM
14527+ } else { /* copyup as wh and reopen */
14528+ /*
14529+ * since writable hfsplus branch is not supported,
14530+ * h_open_pre/post() are unnecessary.
14531+ */
c2b27bf2 14532+ err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
4a4d8108 14533+ di_downgrade_lock(parent, AuLock_IR);
4a4d8108 14534+ }
4a4d8108
AM
14535+
14536+ if (!err) {
14537+ au_pin_set_parent_lflag(pin, /*lflag*/0);
14538+ goto out_dput; /* success */
14539+ }
14540+ au_unpin(pin);
14541+ goto out_unlock;
1facf9fc 14542+
4f0767ce 14543+out_dgrade:
4a4d8108 14544+ di_downgrade_lock(parent, AuLock_IR);
4f0767ce 14545+out_unlock:
4a4d8108 14546+ di_read_unlock(parent, AuLock_IR);
4f0767ce 14547+out_dput:
4a4d8108 14548+ dput(parent);
4f0767ce 14549+out:
1facf9fc 14550+ return err;
14551+}
14552+
4a4d8108
AM
14553+/* ---------------------------------------------------------------------- */
14554+
14555+int au_do_flush(struct file *file, fl_owner_t id,
14556+ int (*flush)(struct file *file, fl_owner_t id))
1facf9fc 14557+{
4a4d8108 14558+ int err;
1facf9fc 14559+ struct super_block *sb;
4a4d8108 14560+ struct inode *inode;
1facf9fc 14561+
c06a8ce3
AM
14562+ inode = file_inode(file);
14563+ sb = inode->i_sb;
4a4d8108
AM
14564+ si_noflush_read_lock(sb);
14565+ fi_read_lock(file);
b752ccd1 14566+ ii_read_lock_child(inode);
1facf9fc 14567+
4a4d8108
AM
14568+ err = flush(file, id);
14569+ au_cpup_attr_timesizes(inode);
1facf9fc 14570+
b752ccd1 14571+ ii_read_unlock(inode);
4a4d8108 14572+ fi_read_unlock(file);
1308ab2a 14573+ si_read_unlock(sb);
dece6358 14574+ return err;
1facf9fc 14575+}
14576+
4a4d8108
AM
14577+/* ---------------------------------------------------------------------- */
14578+
14579+static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
1facf9fc 14580+{
4a4d8108 14581+ int err;
4a4d8108
AM
14582+ struct au_pin pin;
14583+ struct au_finfo *finfo;
c2b27bf2 14584+ struct dentry *parent, *hi_wh;
4a4d8108 14585+ struct inode *inode;
1facf9fc 14586+ struct super_block *sb;
c2b27bf2 14587+ struct au_cp_generic cpg = {
2000de60 14588+ .dentry = file->f_path.dentry,
c2b27bf2
AM
14589+ .bdst = -1,
14590+ .bsrc = -1,
14591+ .len = -1,
14592+ .pin = &pin,
14593+ .flags = AuCpup_DTIME
14594+ };
1facf9fc 14595+
4a4d8108
AM
14596+ FiMustWriteLock(file);
14597+
14598+ err = 0;
14599+ finfo = au_fi(file);
c2b27bf2 14600+ sb = cpg.dentry->d_sb;
5527c038 14601+ inode = d_inode(cpg.dentry);
5afbbe0d 14602+ cpg.bdst = au_ibtop(inode);
c2b27bf2 14603+ if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
1308ab2a 14604+ goto out;
dece6358 14605+
c2b27bf2
AM
14606+ parent = dget_parent(cpg.dentry);
14607+ if (au_test_ro(sb, cpg.bdst, inode)) {
4a4d8108 14608+ di_read_lock_parent(parent, !AuLock_IR);
c2b27bf2
AM
14609+ err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14610+ cpg.bdst = err;
4a4d8108
AM
14611+ di_read_unlock(parent, !AuLock_IR);
14612+ if (unlikely(err < 0))
14613+ goto out_parent;
14614+ err = 0;
1facf9fc 14615+ }
1facf9fc 14616+
4a4d8108 14617+ di_read_lock_parent(parent, AuLock_IR);
c2b27bf2 14618+ hi_wh = au_hi_wh(inode, cpg.bdst);
7f207e10
AM
14619+ if (!S_ISDIR(inode->i_mode)
14620+ && au_opt_test(au_mntflags(sb), PLINK)
4a4d8108 14621+ && au_plink_test(inode)
c2b27bf2 14622+ && !d_unhashed(cpg.dentry)
5afbbe0d 14623+ && cpg.bdst < au_dbtop(cpg.dentry)) {
c2b27bf2 14624+ err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
4a4d8108
AM
14625+ if (unlikely(err))
14626+ goto out_unlock;
14627+
14628+ /* always superio. */
c2b27bf2 14629+ err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
4a4d8108 14630+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
367653fa 14631+ if (!err) {
c2b27bf2 14632+ err = au_sio_cpup_simple(&cpg);
367653fa
AM
14633+ au_unpin(&pin);
14634+ }
4a4d8108
AM
14635+ } else if (hi_wh) {
14636+ /* already copied-up after unlink */
c2b27bf2 14637+ err = au_reopen_wh(file, cpg.bdst, hi_wh);
4a4d8108
AM
14638+ *need_reopen = 0;
14639+ }
1facf9fc 14640+
4f0767ce 14641+out_unlock:
4a4d8108 14642+ di_read_unlock(parent, AuLock_IR);
4f0767ce 14643+out_parent:
4a4d8108 14644+ dput(parent);
4f0767ce 14645+out:
1308ab2a 14646+ return err;
dece6358 14647+}
1facf9fc 14648+
4a4d8108 14649+static void au_do_refresh_dir(struct file *file)
dece6358 14650+{
5afbbe0d 14651+ aufs_bindex_t bindex, bbot, new_bindex, brid;
4a4d8108
AM
14652+ struct au_hfile *p, tmp, *q;
14653+ struct au_finfo *finfo;
1308ab2a 14654+ struct super_block *sb;
4a4d8108 14655+ struct au_fidir *fidir;
1facf9fc 14656+
4a4d8108 14657+ FiMustWriteLock(file);
1facf9fc 14658+
2000de60 14659+ sb = file->f_path.dentry->d_sb;
4a4d8108
AM
14660+ finfo = au_fi(file);
14661+ fidir = finfo->fi_hdir;
14662+ AuDebugOn(!fidir);
14663+ p = fidir->fd_hfile + finfo->fi_btop;
14664+ brid = p->hf_br->br_id;
5afbbe0d
AM
14665+ bbot = fidir->fd_bbot;
14666+ for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
4a4d8108
AM
14667+ if (!p->hf_file)
14668+ continue;
1308ab2a 14669+
4a4d8108
AM
14670+ new_bindex = au_br_index(sb, p->hf_br->br_id);
14671+ if (new_bindex == bindex)
14672+ continue;
14673+ if (new_bindex < 0) {
14674+ au_set_h_fptr(file, bindex, NULL);
14675+ continue;
14676+ }
1308ab2a 14677+
4a4d8108
AM
14678+ /* swap two lower inode, and loop again */
14679+ q = fidir->fd_hfile + new_bindex;
14680+ tmp = *q;
14681+ *q = *p;
14682+ *p = tmp;
14683+ if (tmp.hf_file) {
14684+ bindex--;
14685+ p--;
14686+ }
14687+ }
1308ab2a 14688+
4a4d8108 14689+ p = fidir->fd_hfile;
2000de60 14690+ if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
5afbbe0d
AM
14691+ bbot = au_sbbot(sb);
14692+ for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
4a4d8108
AM
14693+ finfo->fi_btop++, p++)
14694+ if (p->hf_file) {
c06a8ce3 14695+ if (file_inode(p->hf_file))
4a4d8108 14696+ break;
1c60b727 14697+ au_hfput(p, /*execed*/0);
4a4d8108
AM
14698+ }
14699+ } else {
5afbbe0d
AM
14700+ bbot = au_br_index(sb, brid);
14701+ for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
4a4d8108
AM
14702+ finfo->fi_btop++, p++)
14703+ if (p->hf_file)
1c60b727 14704+ au_hfput(p, /*execed*/0);
5afbbe0d 14705+ bbot = au_sbbot(sb);
4a4d8108 14706+ }
1308ab2a 14707+
5afbbe0d
AM
14708+ p = fidir->fd_hfile + bbot;
14709+ for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
4a4d8108
AM
14710+ fidir->fd_bbot--, p--)
14711+ if (p->hf_file) {
c06a8ce3 14712+ if (file_inode(p->hf_file))
4a4d8108 14713+ break;
1c60b727 14714+ au_hfput(p, /*execed*/0);
4a4d8108
AM
14715+ }
14716+ AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
1308ab2a 14717+}
14718+
4a4d8108
AM
14719+/*
14720+ * after branch manipulating, refresh the file.
14721+ */
14722+static int refresh_file(struct file *file, int (*reopen)(struct file *file))
1facf9fc 14723+{
e2f27e51 14724+ int err, need_reopen, nbr;
5afbbe0d 14725+ aufs_bindex_t bbot, bindex;
4a4d8108 14726+ struct dentry *dentry;
e2f27e51 14727+ struct super_block *sb;
1308ab2a 14728+ struct au_finfo *finfo;
4a4d8108 14729+ struct au_hfile *hfile;
1facf9fc 14730+
2000de60 14731+ dentry = file->f_path.dentry;
e2f27e51
AM
14732+ sb = dentry->d_sb;
14733+ nbr = au_sbbot(sb) + 1;
1308ab2a 14734+ finfo = au_fi(file);
4a4d8108
AM
14735+ if (!finfo->fi_hdir) {
14736+ hfile = &finfo->fi_htop;
14737+ AuDebugOn(!hfile->hf_file);
e2f27e51 14738+ bindex = au_br_index(sb, hfile->hf_br->br_id);
4a4d8108
AM
14739+ AuDebugOn(bindex < 0);
14740+ if (bindex != finfo->fi_btop)
5afbbe0d 14741+ au_set_fbtop(file, bindex);
4a4d8108 14742+ } else {
e2f27e51 14743+ err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
4a4d8108
AM
14744+ if (unlikely(err))
14745+ goto out;
14746+ au_do_refresh_dir(file);
14747+ }
1facf9fc 14748+
4a4d8108
AM
14749+ err = 0;
14750+ need_reopen = 1;
14751+ if (!au_test_mmapped(file))
14752+ err = au_file_refresh_by_inode(file, &need_reopen);
e2f27e51
AM
14753+ if (finfo->fi_hdir)
14754+ /* harmless if err */
14755+ au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
027c5e7a 14756+ if (!err && need_reopen && !d_unlinked(dentry))
4a4d8108
AM
14757+ err = reopen(file);
14758+ if (!err) {
14759+ au_update_figen(file);
14760+ goto out; /* success */
14761+ }
14762+
14763+ /* error, close all lower files */
14764+ if (finfo->fi_hdir) {
5afbbe0d
AM
14765+ bbot = au_fbbot_dir(file);
14766+ for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
4a4d8108
AM
14767+ au_set_h_fptr(file, bindex, NULL);
14768+ }
1facf9fc 14769+
4f0767ce 14770+out:
1facf9fc 14771+ return err;
14772+}
14773+
4a4d8108
AM
14774+/* common function to regular file and dir */
14775+int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
521ced18 14776+ int wlock, unsigned int fi_lsc)
dece6358 14777+{
1308ab2a 14778+ int err;
4a4d8108 14779+ unsigned int sigen, figen;
5afbbe0d 14780+ aufs_bindex_t btop;
4a4d8108
AM
14781+ unsigned char pseudo_link;
14782+ struct dentry *dentry;
14783+ struct inode *inode;
1facf9fc 14784+
4a4d8108 14785+ err = 0;
2000de60 14786+ dentry = file->f_path.dentry;
5527c038 14787+ inode = d_inode(dentry);
4a4d8108 14788+ sigen = au_sigen(dentry->d_sb);
521ced18 14789+ fi_write_lock_nested(file, fi_lsc);
4a4d8108 14790+ figen = au_figen(file);
521ced18
JR
14791+ if (!fi_lsc)
14792+ di_write_lock_child(dentry);
14793+ else
14794+ di_write_lock_child2(dentry);
5afbbe0d
AM
14795+ btop = au_dbtop(dentry);
14796+ pseudo_link = (btop != au_ibtop(inode));
14797+ if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
4a4d8108
AM
14798+ if (!wlock) {
14799+ di_downgrade_lock(dentry, AuLock_IR);
14800+ fi_downgrade_lock(file);
14801+ }
14802+ goto out; /* success */
14803+ }
dece6358 14804+
4a4d8108 14805+ AuDbg("sigen %d, figen %d\n", sigen, figen);
027c5e7a 14806+ if (au_digen_test(dentry, sigen)) {
4a4d8108 14807+ err = au_reval_dpath(dentry, sigen);
027c5e7a 14808+ AuDebugOn(!err && au_digen_test(dentry, sigen));
4a4d8108 14809+ }
dece6358 14810+
027c5e7a
AM
14811+ if (!err)
14812+ err = refresh_file(file, reopen);
4a4d8108
AM
14813+ if (!err) {
14814+ if (!wlock) {
14815+ di_downgrade_lock(dentry, AuLock_IR);
14816+ fi_downgrade_lock(file);
14817+ }
14818+ } else {
14819+ di_write_unlock(dentry);
14820+ fi_write_unlock(file);
14821+ }
1facf9fc 14822+
4f0767ce 14823+out:
1308ab2a 14824+ return err;
14825+}
1facf9fc 14826+
4a4d8108
AM
14827+/* ---------------------------------------------------------------------- */
14828+
14829+/* cf. aufs_nopage() */
14830+/* for madvise(2) */
22319442 14831+static int aufs_read_folio(struct file *file __maybe_unused, struct folio *folio)
1308ab2a 14832+{
22319442 14833+ folio_unlock(folio);
4a4d8108
AM
14834+ return 0;
14835+}
1facf9fc 14836+
4a4d8108 14837+/* it will never be called, but necessary to support O_DIRECT */
5afbbe0d 14838+static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
4a4d8108 14839+{ BUG(); return 0; }
1facf9fc 14840+
4a4d8108
AM
14841+/* they will never be called. */
14842+#ifdef CONFIG_AUFS_DEBUG
14843+static int aufs_write_begin(struct file *file, struct address_space *mapping,
22319442 14844+ loff_t pos, unsigned len,
4a4d8108
AM
14845+ struct page **pagep, void **fsdata)
14846+{ AuUnsupport(); return 0; }
14847+static int aufs_write_end(struct file *file, struct address_space *mapping,
14848+ loff_t pos, unsigned len, unsigned copied,
14849+ struct page *page, void *fsdata)
14850+{ AuUnsupport(); return 0; }
14851+static int aufs_writepage(struct page *page, struct writeback_control *wbc)
14852+{ AuUnsupport(); return 0; }
1308ab2a 14853+
26eb093c
AM
14854+static bool aufs_dirty_folio(struct address_space *mapping, struct folio *folio)
14855+{ AuUnsupport(); return true; }
14856+static void aufs_invalidate_folio(struct folio *folio, size_t offset, size_t len)
4a4d8108 14857+{ AuUnsupport(); }
22319442
JP
14858+static bool aufs_release_folio(struct folio *folio, gfp_t gfp)
14859+{ AuUnsupport(); return true; }
79b8bda9 14860+#if 0 /* called by memory compaction regardless file */
82da57cb
AM
14861+static int aufs_migrate_folio(struct address_space *mapping, struct folio *dst,
14862+ struct folio *src, enum migrate_mode mode)
4a4d8108 14863+{ AuUnsupport(); return 0; }
79b8bda9 14864+#endif
26eb093c 14865+static int aufs_launder_folio(struct folio *folio)
4a4d8108 14866+{ AuUnsupport(); return 0; }
26eb093c
AM
14867+static bool aufs_is_partially_uptodate(struct folio *folio, size_t from,
14868+ size_t count)
14869+{ AuUnsupport(); return true; }
22319442 14870+static void aufs_is_dirty_writeback(struct folio *folio, bool *dirty,
392086de
AM
14871+ bool *writeback)
14872+{ AuUnsupport(); }
4a4d8108
AM
14873+static int aufs_error_remove_page(struct address_space *mapping,
14874+ struct page *page)
14875+{ AuUnsupport(); return 0; }
b4510431
AM
14876+static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
14877+ sector_t *span)
14878+{ AuUnsupport(); return 0; }
14879+static void aufs_swap_deactivate(struct file *file)
14880+{ AuUnsupport(); }
22319442
JP
14881+static int aufs_swap_rw(struct kiocb *iocb, struct iov_iter *iter)
14882+{ AuUnsupport(); return 0; }
4a4d8108
AM
14883+#endif /* CONFIG_AUFS_DEBUG */
14884+
14885+const struct address_space_operations aufs_aop = {
22319442 14886+ .read_folio = aufs_read_folio,
4a4d8108 14887+ .direct_IO = aufs_direct_IO,
4a4d8108
AM
14888+#ifdef CONFIG_AUFS_DEBUG
14889+ .writepage = aufs_writepage,
4a4d8108 14890+ /* no writepages, because of writepage */
26eb093c 14891+ .dirty_folio = aufs_dirty_folio,
4a4d8108
AM
14892+ /* no readpages, because of readpage */
14893+ .write_begin = aufs_write_begin,
14894+ .write_end = aufs_write_end,
14895+ /* no bmap, no block device */
26eb093c 14896+ .invalidate_folio = aufs_invalidate_folio,
22319442 14897+ .release_folio = aufs_release_folio,
79b8bda9 14898+ /* is fallback_migrate_page ok? */
82da57cb 14899+ /* .migrate_folio = aufs_migrate_folio, */
26eb093c 14900+ .launder_folio = aufs_launder_folio,
4a4d8108 14901+ .is_partially_uptodate = aufs_is_partially_uptodate,
392086de 14902+ .is_dirty_writeback = aufs_is_dirty_writeback,
b4510431
AM
14903+ .error_remove_page = aufs_error_remove_page,
14904+ .swap_activate = aufs_swap_activate,
22319442
JP
14905+ .swap_deactivate = aufs_swap_deactivate,
14906+ .swap_rw = aufs_swap_rw
4a4d8108 14907+#endif /* CONFIG_AUFS_DEBUG */
dece6358 14908+};
7f207e10 14909diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
eca34b5c 14910--- /usr/share/empty/fs/aufs/file.h 1970-01-01 01:00:00.000000000 +0100
594d0238 14911+++ linux/fs/aufs/file.h 2022-11-05 23:02:18.965889284 +0100
9f237c51 14912@@ -0,0 +1,342 @@
062440b3 14913+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 14914+/*
0dcfbb52 14915+ * Copyright (C) 2005-2022 Junjiro R. Okajima
4a4d8108 14916+ *
0dcfbb52 14917+ * This program is free software; you can redistribute it and/or modify
4a4d8108
AM
14918+ * it under the terms of the GNU General Public License as published by
14919+ * the Free Software Foundation; either version 2 of the License, or
14920+ * (at your option) any later version.
14921+ *
14922+ * This program is distributed in the hope that it will be useful,
14923+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14924+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14925+ * GNU General Public License for more details.
14926+ *
14927+ * You should have received a copy of the GNU General Public License
523b37e3 14928+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 14929+ */
1facf9fc 14930+
4a4d8108
AM
14931+/*
14932+ * file operations
14933+ */
1facf9fc 14934+
4a4d8108
AM
14935+#ifndef __AUFS_FILE_H__
14936+#define __AUFS_FILE_H__
1facf9fc 14937+
4a4d8108 14938+#ifdef __KERNEL__
1facf9fc 14939+
2cbb1c4b 14940+#include <linux/file.h>
4a4d8108 14941+#include <linux/fs.h>
3c1bdaff 14942+#include <linux/mm_types.h>
4a4d8108 14943+#include <linux/poll.h>
4a4d8108 14944+#include "rwsem.h"
1facf9fc 14945+
4a4d8108
AM
14946+struct au_branch;
14947+struct au_hfile {
14948+ struct file *hf_file;
14949+ struct au_branch *hf_br;
14950+};
1facf9fc 14951+
4a4d8108
AM
14952+struct au_vdir;
14953+struct au_fidir {
14954+ aufs_bindex_t fd_bbot;
14955+ aufs_bindex_t fd_nent;
14956+ struct au_vdir *fd_vdir_cache;
14957+ struct au_hfile fd_hfile[];
14958+};
1facf9fc 14959+
4a4d8108 14960+static inline int au_fidir_sz(int nent)
dece6358 14961+{
4f0767ce
JR
14962+ AuDebugOn(nent < 0);
14963+ return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
4a4d8108 14964+}
1facf9fc 14965+
4a4d8108
AM
14966+struct au_finfo {
14967+ atomic_t fi_generation;
dece6358 14968+
4a4d8108
AM
14969+ struct au_rwsem fi_rwsem;
14970+ aufs_bindex_t fi_btop;
14971+
14972+ /* do not union them */
14973+ struct { /* for non-dir */
14974+ struct au_hfile fi_htop;
2cbb1c4b 14975+ atomic_t fi_mmapped;
4a4d8108
AM
14976+ };
14977+ struct au_fidir *fi_hdir; /* for dir only */
523b37e3 14978+
8b6a4947 14979+ struct hlist_bl_node fi_hlist;
1c60b727 14980+ struct file *fi_file; /* very ugly */
9f237c51 14981+ struct rcu_head rcu;
4a4d8108 14982+} ____cacheline_aligned_in_smp;
1facf9fc 14983+
4a4d8108 14984+/* ---------------------------------------------------------------------- */
1facf9fc 14985+
4a4d8108
AM
14986+/* file.c */
14987+extern const struct address_space_operations aufs_aop;
14988+unsigned int au_file_roflags(unsigned int flags);
14989+struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
392086de 14990+ struct file *file, int force_wr);
b912730e 14991+struct au_do_open_args {
8b6a4947 14992+ int aopen;
b912730e
AM
14993+ int (*open)(struct file *file, int flags,
14994+ struct file *h_file);
14995+ struct au_fidir *fidir;
14996+ struct file *h_file;
14997+};
14998+int au_do_open(struct file *file, struct au_do_open_args *args);
4a4d8108
AM
14999+int au_reopen_nondir(struct file *file);
15000+struct au_pin;
15001+int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
15002+int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
521ced18 15003+ int wlock, unsigned int fi_lsc);
4a4d8108
AM
15004+int au_do_flush(struct file *file, fl_owner_t id,
15005+ int (*flush)(struct file *file, fl_owner_t id));
1facf9fc 15006+
4a4d8108
AM
15007+/* poll.c */
15008+#ifdef CONFIG_AUFS_POLL
cd7a4cd9 15009+__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt);
4a4d8108 15010+#endif
1facf9fc 15011+
4a4d8108
AM
15012+#ifdef CONFIG_AUFS_BR_HFSPLUS
15013+/* hfsplus.c */
392086de
AM
15014+struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
15015+ int force_wr);
4a4d8108
AM
15016+void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
15017+ struct file *h_file);
15018+#else
c1595e42
JR
15019+AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
15020+ aufs_bindex_t bindex, int force_wr)
4a4d8108
AM
15021+AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
15022+ struct file *h_file);
15023+#endif
1facf9fc 15024+
4a4d8108
AM
15025+/* f_op.c */
15026+extern const struct file_operations aufs_file_fop;
b912730e 15027+int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
4a4d8108 15028+int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
521ced18 15029+struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
4a4d8108 15030+
4a4d8108 15031+/* finfo.c */
f0c0a007 15032+void au_hfput(struct au_hfile *hf, int execed);
4a4d8108
AM
15033+void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
15034+ struct file *h_file);
1facf9fc 15035+
4a4d8108 15036+void au_update_figen(struct file *file);
4a4d8108 15037+struct au_fidir *au_fidir_alloc(struct super_block *sb);
e2f27e51 15038+int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
1facf9fc 15039+
4a4d8108 15040+void au_fi_init_once(void *_fi);
1c60b727 15041+void au_finfo_fin(struct file *file);
4a4d8108 15042+int au_finfo_init(struct file *file, struct au_fidir *fidir);
1facf9fc 15043+
4a4d8108
AM
15044+/* ioctl.c */
15045+long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
b752ccd1
AM
15046+#ifdef CONFIG_COMPAT
15047+long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
15048+ unsigned long arg);
c2b27bf2
AM
15049+long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
15050+ unsigned long arg);
b752ccd1 15051+#endif
1facf9fc 15052+
4a4d8108 15053+/* ---------------------------------------------------------------------- */
1facf9fc 15054+
4a4d8108
AM
15055+static inline struct au_finfo *au_fi(struct file *file)
15056+{
38d290e6 15057+ return file->private_data;
4a4d8108 15058+}
1facf9fc 15059+
4a4d8108 15060+/* ---------------------------------------------------------------------- */
1facf9fc 15061+
8b6a4947
AM
15062+#define fi_read_lock(f) au_rw_read_lock(&au_fi(f)->fi_rwsem)
15063+#define fi_write_lock(f) au_rw_write_lock(&au_fi(f)->fi_rwsem)
15064+#define fi_read_trylock(f) au_rw_read_trylock(&au_fi(f)->fi_rwsem)
15065+#define fi_write_trylock(f) au_rw_write_trylock(&au_fi(f)->fi_rwsem)
4a4d8108 15066+/*
8b6a4947
AM
15067+#define fi_read_trylock_nested(f) \
15068+ au_rw_read_trylock_nested(&au_fi(f)->fi_rwsem)
15069+#define fi_write_trylock_nested(f) \
15070+ au_rw_write_trylock_nested(&au_fi(f)->fi_rwsem)
15071+*/
15072+
15073+#define fi_read_unlock(f) au_rw_read_unlock(&au_fi(f)->fi_rwsem)
15074+#define fi_write_unlock(f) au_rw_write_unlock(&au_fi(f)->fi_rwsem)
15075+#define fi_downgrade_lock(f) au_rw_dgrade_lock(&au_fi(f)->fi_rwsem)
1308ab2a 15076+
521ced18
JR
15077+/* lock subclass for finfo */
15078+enum {
15079+ AuLsc_FI_1,
15080+ AuLsc_FI_2
15081+};
15082+
15083+static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
15084+{
15085+ au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15086+}
15087+
15088+static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
15089+{
15090+ au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15091+}
15092+
15093+/*
15094+ * fi_read_lock_1, fi_write_lock_1,
15095+ * fi_read_lock_2, fi_write_lock_2
15096+ */
15097+#define AuReadLockFunc(name) \
15098+static inline void fi_read_lock_##name(struct file *f) \
15099+{ fi_read_lock_nested(f, AuLsc_FI_##name); }
15100+
15101+#define AuWriteLockFunc(name) \
15102+static inline void fi_write_lock_##name(struct file *f) \
15103+{ fi_write_lock_nested(f, AuLsc_FI_##name); }
15104+
15105+#define AuRWLockFuncs(name) \
15106+ AuReadLockFunc(name) \
15107+ AuWriteLockFunc(name)
15108+
15109+AuRWLockFuncs(1);
15110+AuRWLockFuncs(2);
15111+
15112+#undef AuReadLockFunc
15113+#undef AuWriteLockFunc
15114+#undef AuRWLockFuncs
15115+
4a4d8108
AM
15116+#define FiMustNoWaiters(f) AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
15117+#define FiMustAnyLock(f) AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
15118+#define FiMustWriteLock(f) AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
1facf9fc 15119+
1308ab2a 15120+/* ---------------------------------------------------------------------- */
15121+
4a4d8108 15122+/* todo: hard/soft set? */
5afbbe0d 15123+static inline aufs_bindex_t au_fbtop(struct file *file)
dece6358 15124+{
4a4d8108
AM
15125+ FiMustAnyLock(file);
15126+ return au_fi(file)->fi_btop;
15127+}
dece6358 15128+
5afbbe0d 15129+static inline aufs_bindex_t au_fbbot_dir(struct file *file)
4a4d8108
AM
15130+{
15131+ FiMustAnyLock(file);
15132+ AuDebugOn(!au_fi(file)->fi_hdir);
15133+ return au_fi(file)->fi_hdir->fd_bbot;
15134+}
1facf9fc 15135+
4a4d8108
AM
15136+static inline struct au_vdir *au_fvdir_cache(struct file *file)
15137+{
15138+ FiMustAnyLock(file);
15139+ AuDebugOn(!au_fi(file)->fi_hdir);
15140+ return au_fi(file)->fi_hdir->fd_vdir_cache;
15141+}
1facf9fc 15142+
5afbbe0d 15143+static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
4a4d8108
AM
15144+{
15145+ FiMustWriteLock(file);
15146+ au_fi(file)->fi_btop = bindex;
15147+}
1facf9fc 15148+
5afbbe0d 15149+static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
4a4d8108
AM
15150+{
15151+ FiMustWriteLock(file);
15152+ AuDebugOn(!au_fi(file)->fi_hdir);
15153+ au_fi(file)->fi_hdir->fd_bbot = bindex;
15154+}
1308ab2a 15155+
4a4d8108
AM
15156+static inline void au_set_fvdir_cache(struct file *file,
15157+ struct au_vdir *vdir_cache)
15158+{
15159+ FiMustWriteLock(file);
15160+ AuDebugOn(!au_fi(file)->fi_hdir);
15161+ au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
15162+}
dece6358 15163+
4a4d8108
AM
15164+static inline struct file *au_hf_top(struct file *file)
15165+{
15166+ FiMustAnyLock(file);
15167+ AuDebugOn(au_fi(file)->fi_hdir);
15168+ return au_fi(file)->fi_htop.hf_file;
15169+}
1facf9fc 15170+
4a4d8108
AM
15171+static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
15172+{
15173+ FiMustAnyLock(file);
15174+ AuDebugOn(!au_fi(file)->fi_hdir);
15175+ return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
dece6358
AM
15176+}
15177+
4a4d8108
AM
15178+/* todo: memory barrier? */
15179+static inline unsigned int au_figen(struct file *f)
dece6358 15180+{
4a4d8108
AM
15181+ return atomic_read(&au_fi(f)->fi_generation);
15182+}
dece6358 15183+
2cbb1c4b
JR
15184+static inline void au_set_mmapped(struct file *f)
15185+{
15186+ if (atomic_inc_return(&au_fi(f)->fi_mmapped))
15187+ return;
0c3ec466 15188+ pr_warn("fi_mmapped wrapped around\n");
2cbb1c4b
JR
15189+ while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
15190+ ;
15191+}
15192+
15193+static inline void au_unset_mmapped(struct file *f)
15194+{
15195+ atomic_dec(&au_fi(f)->fi_mmapped);
15196+}
15197+
4a4d8108
AM
15198+static inline int au_test_mmapped(struct file *f)
15199+{
2cbb1c4b
JR
15200+ return atomic_read(&au_fi(f)->fi_mmapped);
15201+}
15202+
15203+/* customize vma->vm_file */
15204+
15205+static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
15206+ struct file *file)
15207+{
53392da6
AM
15208+ struct file *f;
15209+
15210+ f = vma->vm_file;
2cbb1c4b
JR
15211+ get_file(file);
15212+ vma->vm_file = file;
53392da6 15213+ fput(f);
2cbb1c4b
JR
15214+}
15215+
15216+#ifdef CONFIG_MMU
15217+#define AuDbgVmRegion(file, vma) do {} while (0)
15218+
15219+static inline void au_vm_file_reset(struct vm_area_struct *vma,
15220+ struct file *file)
15221+{
15222+ au_do_vm_file_reset(vma, file);
15223+}
15224+#else
15225+#define AuDbgVmRegion(file, vma) \
15226+ AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
15227+
15228+static inline void au_vm_file_reset(struct vm_area_struct *vma,
15229+ struct file *file)
15230+{
53392da6
AM
15231+ struct file *f;
15232+
2cbb1c4b 15233+ au_do_vm_file_reset(vma, file);
53392da6 15234+ f = vma->vm_region->vm_file;
2cbb1c4b
JR
15235+ get_file(file);
15236+ vma->vm_region->vm_file = file;
53392da6 15237+ fput(f);
2cbb1c4b
JR
15238+}
15239+#endif /* CONFIG_MMU */
15240+
15241+/* handle vma->vm_prfile */
fb47a38f 15242+static inline void au_vm_prfile_set(struct vm_area_struct *vma,
2cbb1c4b
JR
15243+ struct file *file)
15244+{
2cbb1c4b
JR
15245+ get_file(file);
15246+ vma->vm_prfile = file;
15247+#ifndef CONFIG_MMU
15248+ get_file(file);
15249+ vma->vm_region->vm_prfile = file;
15250+#endif
fb47a38f 15251+}
1308ab2a 15252+
4a4d8108
AM
15253+#endif /* __KERNEL__ */
15254+#endif /* __AUFS_FILE_H__ */
7f207e10 15255diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
eca34b5c 15256--- /usr/share/empty/fs/aufs/finfo.c 1970-01-01 01:00:00.000000000 +0100
594d0238 15257+++ linux/fs/aufs/finfo.c 2022-11-05 23:02:18.965889284 +0100
062440b3 15258@@ -0,0 +1,149 @@
cd7a4cd9 15259+// SPDX-License-Identifier: GPL-2.0
4a4d8108 15260+/*
0dcfbb52 15261+ * Copyright (C) 2005-2022 Junjiro R. Okajima
4a4d8108 15262+ *
0dcfbb52 15263+ * This program is free software; you can redistribute it and/or modify
4a4d8108
AM
15264+ * it under the terms of the GNU General Public License as published by
15265+ * the Free Software Foundation; either version 2 of the License, or
15266+ * (at your option) any later version.
15267+ *
15268+ * This program is distributed in the hope that it will be useful,
15269+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15270+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15271+ * GNU General Public License for more details.
15272+ *
15273+ * You should have received a copy of the GNU General Public License
523b37e3 15274+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 15275+ */
1308ab2a 15276+
4a4d8108
AM
15277+/*
15278+ * file private data
15279+ */
1facf9fc 15280+
4a4d8108 15281+#include "aufs.h"
1facf9fc 15282+
f0c0a007 15283+void au_hfput(struct au_hfile *hf, int execed)
4a4d8108 15284+{
f0c0a007 15285+ if (execed)
4a4d8108
AM
15286+ allow_write_access(hf->hf_file);
15287+ fput(hf->hf_file);
15288+ hf->hf_file = NULL;
acd2b654 15289+ au_lcnt_dec(&hf->hf_br->br_nfiles);
4a4d8108
AM
15290+ hf->hf_br = NULL;
15291+}
1facf9fc 15292+
4a4d8108
AM
15293+void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
15294+{
15295+ struct au_finfo *finfo = au_fi(file);
15296+ struct au_hfile *hf;
15297+ struct au_fidir *fidir;
15298+
15299+ fidir = finfo->fi_hdir;
15300+ if (!fidir) {
15301+ AuDebugOn(finfo->fi_btop != bindex);
15302+ hf = &finfo->fi_htop;
15303+ } else
15304+ hf = fidir->fd_hfile + bindex;
15305+
15306+ if (hf && hf->hf_file)
f0c0a007 15307+ au_hfput(hf, vfsub_file_execed(file));
4a4d8108
AM
15308+ if (val) {
15309+ FiMustWriteLock(file);
b912730e 15310+ AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
4a4d8108 15311+ hf->hf_file = val;
2000de60 15312+ hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
1308ab2a 15313+ }
4a4d8108 15314+}
1facf9fc 15315+
4a4d8108
AM
15316+void au_update_figen(struct file *file)
15317+{
2000de60 15318+ atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
4a4d8108 15319+ /* smp_mb(); */ /* atomic_set */
1facf9fc 15320+}
15321+
4a4d8108
AM
15322+/* ---------------------------------------------------------------------- */
15323+
4a4d8108
AM
15324+struct au_fidir *au_fidir_alloc(struct super_block *sb)
15325+{
15326+ struct au_fidir *fidir;
15327+ int nbr;
15328+
5afbbe0d 15329+ nbr = au_sbbot(sb) + 1;
4a4d8108
AM
15330+ if (nbr < 2)
15331+ nbr = 2; /* initial allocate for 2 branches */
15332+ fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
15333+ if (fidir) {
15334+ fidir->fd_bbot = -1;
15335+ fidir->fd_nent = nbr;
4a4d8108
AM
15336+ }
15337+
15338+ return fidir;
15339+}
15340+
e2f27e51 15341+int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
4a4d8108
AM
15342+{
15343+ int err;
15344+ struct au_fidir *fidir, *p;
15345+
15346+ AuRwMustWriteLock(&finfo->fi_rwsem);
15347+ fidir = finfo->fi_hdir;
15348+ AuDebugOn(!fidir);
15349+
15350+ err = -ENOMEM;
15351+ p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
e2f27e51 15352+ GFP_NOFS, may_shrink);
4a4d8108
AM
15353+ if (p) {
15354+ p->fd_nent = nbr;
15355+ finfo->fi_hdir = p;
15356+ err = 0;
15357+ }
1facf9fc 15358+
dece6358 15359+ return err;
1facf9fc 15360+}
1308ab2a 15361+
15362+/* ---------------------------------------------------------------------- */
15363+
1c60b727 15364+void au_finfo_fin(struct file *file)
1308ab2a 15365+{
4a4d8108
AM
15366+ struct au_finfo *finfo;
15367+
acd2b654 15368+ au_lcnt_dec(&au_sbi(file->f_path.dentry->d_sb)->si_nfiles);
7f207e10 15369+
4a4d8108
AM
15370+ finfo = au_fi(file);
15371+ AuDebugOn(finfo->fi_hdir);
15372+ AuRwDestroy(&finfo->fi_rwsem);
1c60b727 15373+ au_cache_free_finfo(finfo);
1308ab2a 15374+}
1308ab2a 15375+
e49829fe 15376+void au_fi_init_once(void *_finfo)
4a4d8108 15377+{
e49829fe 15378+ struct au_finfo *finfo = _finfo;
1308ab2a 15379+
e49829fe 15380+ au_rw_init(&finfo->fi_rwsem);
4a4d8108 15381+}
1308ab2a 15382+
4a4d8108
AM
15383+int au_finfo_init(struct file *file, struct au_fidir *fidir)
15384+{
1716fcea 15385+ int err;
4a4d8108
AM
15386+ struct au_finfo *finfo;
15387+ struct dentry *dentry;
15388+
15389+ err = -ENOMEM;
2000de60 15390+ dentry = file->f_path.dentry;
4a4d8108
AM
15391+ finfo = au_cache_alloc_finfo();
15392+ if (unlikely(!finfo))
15393+ goto out;
15394+
15395+ err = 0;
acd2b654 15396+ au_lcnt_inc(&au_sbi(dentry->d_sb)->si_nfiles);
4a4d8108
AM
15397+ au_rw_write_lock(&finfo->fi_rwsem);
15398+ finfo->fi_btop = -1;
15399+ finfo->fi_hdir = fidir;
15400+ atomic_set(&finfo->fi_generation, au_digen(dentry));
15401+ /* smp_mb(); */ /* atomic_set */
15402+
15403+ file->private_data = finfo;
15404+
15405+out:
15406+ return err;
15407+}
7f207e10 15408diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
eca34b5c 15409--- /usr/share/empty/fs/aufs/f_op.c 1970-01-01 01:00:00.000000000 +0100
631230db
AM
15410+++ linux/fs/aufs/f_op.c 2023-10-31 09:31:04.196547417 +0100
15411@@ -0,0 +1,771 @@
cd7a4cd9 15412+// SPDX-License-Identifier: GPL-2.0
dece6358 15413+/*
0dcfbb52 15414+ * Copyright (C) 2005-2022 Junjiro R. Okajima
dece6358 15415+ *
0dcfbb52 15416+ * This program is free software; you can redistribute it and/or modify
dece6358
AM
15417+ * it under the terms of the GNU General Public License as published by
15418+ * the Free Software Foundation; either version 2 of the License, or
15419+ * (at your option) any later version.
15420+ *
15421+ * This program is distributed in the hope that it will be useful,
15422+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15423+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15424+ * GNU General Public License for more details.
15425+ *
15426+ * You should have received a copy of the GNU General Public License
523b37e3 15427+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358 15428+ */
1facf9fc 15429+
15430+/*
4a4d8108 15431+ * file and vm operations
1facf9fc 15432+ */
dece6358 15433+
86dc4139 15434+#include <linux/aio.h>
4a4d8108
AM
15435+#include <linux/fs_stack.h>
15436+#include <linux/mman.h>
4a4d8108 15437+#include <linux/security.h>
dece6358
AM
15438+#include "aufs.h"
15439+
b912730e 15440+int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
1facf9fc 15441+{
4a4d8108
AM
15442+ int err;
15443+ aufs_bindex_t bindex;
8cdd5066 15444+ struct dentry *dentry, *h_dentry;
4a4d8108 15445+ struct au_finfo *finfo;
38d290e6 15446+ struct inode *h_inode;
4a4d8108
AM
15447+
15448+ FiMustWriteLock(file);
15449+
523b37e3 15450+ err = 0;
2000de60 15451+ dentry = file->f_path.dentry;
b912730e 15452+ AuDebugOn(IS_ERR_OR_NULL(dentry));
4a4d8108
AM
15453+ finfo = au_fi(file);
15454+ memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
2cbb1c4b 15455+ atomic_set(&finfo->fi_mmapped, 0);
5afbbe0d 15456+ bindex = au_dbtop(dentry);
8cdd5066
JR
15457+ if (!h_file) {
15458+ h_dentry = au_h_dptr(dentry, bindex);
15459+ err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15460+ if (unlikely(err))
15461+ goto out;
b912730e 15462+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
acd2b654
AM
15463+ if (IS_ERR(h_file)) {
15464+ err = PTR_ERR(h_file);
15465+ goto out;
15466+ }
8cdd5066
JR
15467+ } else {
15468+ h_dentry = h_file->f_path.dentry;
15469+ err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15470+ if (unlikely(err))
15471+ goto out;
acd2b654 15472+ /* br ref is already inc-ed */
8cdd5066 15473+ }
acd2b654 15474+
2255d0fe
AM
15475+ if (flags & __O_TMPFILE) {
15476+ AuDebugOn(!h_file);
15477+ AuDebugOn(h_file != au_di(dentry)->di_htmpfile);
15478+ au_di(dentry)->di_htmpfile = NULL;
15479+
15480+ if (!(flags & O_EXCL)) {
15481+ h_inode = file_inode(h_file);
15482+ spin_lock(&h_inode->i_lock);
15483+ h_inode->i_state |= I_LINKABLE;
15484+ spin_unlock(&h_inode->i_lock);
15485+ }
4a4d8108 15486+ }
acd2b654
AM
15487+ au_set_fbtop(file, bindex);
15488+ au_set_h_fptr(file, bindex, h_file);
15489+ au_update_figen(file);
15490+ /* todo: necessary? */
15491+ /* file->f_ra = h_file->f_ra; */
027c5e7a 15492+
8cdd5066 15493+out:
4a4d8108 15494+ return err;
1facf9fc 15495+}
15496+
4a4d8108
AM
15497+static int aufs_open_nondir(struct inode *inode __maybe_unused,
15498+ struct file *file)
1facf9fc 15499+{
4a4d8108 15500+ int err;
1308ab2a 15501+ struct super_block *sb;
b912730e
AM
15502+ struct au_do_open_args args = {
15503+ .open = au_do_open_nondir
15504+ };
1facf9fc 15505+
523b37e3
AM
15506+ AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
15507+ file, vfsub_file_flags(file), file->f_mode);
1facf9fc 15508+
2000de60 15509+ sb = file->f_path.dentry->d_sb;
4a4d8108 15510+ si_read_lock(sb, AuLock_FLUSH);
b912730e 15511+ err = au_do_open(file, &args);
4a4d8108
AM
15512+ si_read_unlock(sb);
15513+ return err;
15514+}
1facf9fc 15515+
4a4d8108
AM
15516+int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
15517+{
15518+ struct au_finfo *finfo;
15519+ aufs_bindex_t bindex;
1facf9fc 15520+
4a4d8108 15521+ finfo = au_fi(file);
8b6a4947
AM
15522+ au_hbl_del(&finfo->fi_hlist,
15523+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
4a4d8108 15524+ bindex = finfo->fi_btop;
b4510431 15525+ if (bindex >= 0)
4a4d8108 15526+ au_set_h_fptr(file, bindex, NULL);
7f207e10 15527+
1c60b727 15528+ au_finfo_fin(file);
4a4d8108 15529+ return 0;
1facf9fc 15530+}
15531+
4a4d8108
AM
15532+/* ---------------------------------------------------------------------- */
15533+
15534+static int au_do_flush_nondir(struct file *file, fl_owner_t id)
dece6358 15535+{
1308ab2a 15536+ int err;
4a4d8108
AM
15537+ struct file *h_file;
15538+
15539+ err = 0;
15540+ h_file = au_hf_top(file);
15541+ if (h_file)
15542+ err = vfsub_flush(h_file, id);
15543+ return err;
15544+}
15545+
15546+static int aufs_flush_nondir(struct file *file, fl_owner_t id)
15547+{
15548+ return au_do_flush(file, id, au_do_flush_nondir);
15549+}
15550+
15551+/* ---------------------------------------------------------------------- */
9dbd164d
AM
15552+/*
15553+ * read and write functions acquire [fdi]_rwsem once, but release before
15554+ * mmap_sem. This is because to stop a race condition between mmap(2).
acd2b654 15555+ * Releasing these aufs-rwsem should be safe, no branch-management (by keeping
9dbd164d
AM
15556+ * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
15557+ * read functions after [fdi]_rwsem are released, but it should be harmless.
15558+ */
4a4d8108 15559+
b912730e 15560+/* Callers should call au_read_post() or fput() in the end */
521ced18 15561+struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
4a4d8108 15562+{
4a4d8108 15563+ struct file *h_file;
b912730e 15564+ int err;
1facf9fc 15565+
521ced18 15566+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
b912730e
AM
15567+ if (!err) {
15568+ di_read_unlock(file->f_path.dentry, AuLock_IR);
15569+ h_file = au_hf_top(file);
15570+ get_file(h_file);
15571+ if (!keep_fi)
15572+ fi_read_unlock(file);
15573+ } else
15574+ h_file = ERR_PTR(err);
15575+
15576+ return h_file;
15577+}
15578+
15579+static void au_read_post(struct inode *inode, struct file *h_file)
15580+{
15581+ /* update without lock, I don't think it a problem */
15582+ fsstack_copy_attr_atime(inode, file_inode(h_file));
15583+ fput(h_file);
15584+}
15585+
15586+struct au_write_pre {
521ced18
JR
15587+ /* input */
15588+ unsigned int lsc;
15589+
15590+ /* output */
b912730e 15591+ blkcnt_t blks;
5afbbe0d 15592+ aufs_bindex_t btop;
b912730e
AM
15593+};
15594+
15595+/*
15596+ * return with iinfo is write-locked
15597+ * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
15598+ * end
15599+ */
15600+static struct file *au_write_pre(struct file *file, int do_ready,
15601+ struct au_write_pre *wpre)
15602+{
15603+ struct file *h_file;
15604+ struct dentry *dentry;
15605+ int err;
521ced18 15606+ unsigned int lsc;
b912730e
AM
15607+ struct au_pin pin;
15608+
521ced18
JR
15609+ lsc = 0;
15610+ if (wpre)
15611+ lsc = wpre->lsc;
15612+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
b912730e 15613+ h_file = ERR_PTR(err);
dece6358
AM
15614+ if (unlikely(err))
15615+ goto out;
1facf9fc 15616+
b912730e
AM
15617+ dentry = file->f_path.dentry;
15618+ if (do_ready) {
15619+ err = au_ready_to_write(file, -1, &pin);
15620+ if (unlikely(err)) {
15621+ h_file = ERR_PTR(err);
15622+ di_write_unlock(dentry);
15623+ goto out_fi;
15624+ }
15625+ }
15626+
15627+ di_downgrade_lock(dentry, /*flags*/0);
15628+ if (wpre)
5afbbe0d 15629+ wpre->btop = au_fbtop(file);
4a4d8108 15630+ h_file = au_hf_top(file);
9dbd164d 15631+ get_file(h_file);
b912730e
AM
15632+ if (wpre)
15633+ wpre->blks = file_inode(h_file)->i_blocks;
15634+ if (do_ready)
15635+ au_unpin(&pin);
15636+ di_read_unlock(dentry, /*flags*/0);
631230db 15637+ vfsub_file_start_write(h_file);
b912730e
AM
15638+
15639+out_fi:
15640+ fi_write_unlock(file);
15641+out:
15642+ return h_file;
15643+}
15644+
15645+static void au_write_post(struct inode *inode, struct file *h_file,
15646+ struct au_write_pre *wpre, ssize_t written)
15647+{
15648+ struct inode *h_inode;
15649+
631230db 15650+ vfsub_file_end_write(h_file);
b912730e 15651+ au_cpup_attr_timesizes(inode);
5afbbe0d 15652+ AuDebugOn(au_ibtop(inode) != wpre->btop);
b912730e
AM
15653+ h_inode = file_inode(h_file);
15654+ inode->i_mode = h_inode->i_mode;
15655+ ii_write_unlock(inode);
b912730e
AM
15656+ /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
15657+ if (written > 0)
5afbbe0d 15658+ au_fhsm_wrote(inode->i_sb, wpre->btop,
b912730e 15659+ /*force*/h_inode->i_blocks > wpre->blks);
1c60b727 15660+ fput(h_file);
b912730e
AM
15661+}
15662+
e49829fe
JR
15663+/*
15664+ * todo: very ugly
15665+ * it locks both of i_mutex and si_rwsem for read in safe.
15666+ * if the plink maintenance mode continues forever (that is the problem),
15667+ * may loop forever.
15668+ */
15669+static void au_mtx_and_read_lock(struct inode *inode)
15670+{
15671+ int err;
15672+ struct super_block *sb = inode->i_sb;
15673+
15674+ while (1) {
febd17d6 15675+ inode_lock(inode);
e49829fe
JR
15676+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
15677+ if (!err)
15678+ break;
febd17d6 15679+ inode_unlock(inode);
e49829fe
JR
15680+ si_read_lock(sb, AuLock_NOPLMW);
15681+ si_read_unlock(sb);
15682+ }
15683+}
15684+
076b876e
AM
15685+static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
15686+ struct iov_iter *iov_iter)
dece6358 15687+{
4a4d8108
AM
15688+ ssize_t err;
15689+ struct file *file;
076b876e 15690+ ssize_t (*iter)(struct kiocb *, struct iov_iter *);
1facf9fc 15691+
4a4d8108
AM
15692+ err = security_file_permission(h_file, rw);
15693+ if (unlikely(err))
15694+ goto out;
1facf9fc 15695+
43982f53 15696+ err = -ENOSYS; /* the branch doesn't have its ->(read|write)_iter() */
076b876e 15697+ iter = NULL;
5527c038 15698+ if (rw == MAY_READ)
076b876e 15699+ iter = h_file->f_op->read_iter;
5527c038 15700+ else if (rw == MAY_WRITE)
076b876e 15701+ iter = h_file->f_op->write_iter;
076b876e
AM
15702+
15703+ file = kio->ki_filp;
15704+ kio->ki_filp = h_file;
15705+ if (iter) {
2cbb1c4b 15706+ lockdep_off();
076b876e
AM
15707+ err = iter(kio, iov_iter);
15708+ lockdep_on();
4a4d8108
AM
15709+ } else
15710+ /* currently there is no such fs */
15711+ WARN_ON_ONCE(1);
076b876e 15712+ kio->ki_filp = file;
1facf9fc 15713+
4f0767ce 15714+out:
dece6358
AM
15715+ return err;
15716+}
1facf9fc 15717+
076b876e 15718+static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
1facf9fc 15719+{
4a4d8108
AM
15720+ ssize_t err;
15721+ struct file *file, *h_file;
b912730e 15722+ struct inode *inode;
dece6358 15723+ struct super_block *sb;
1facf9fc 15724+
4a4d8108 15725+ file = kio->ki_filp;
b912730e
AM
15726+ inode = file_inode(file);
15727+ sb = inode->i_sb;
e49829fe 15728+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
4a4d8108 15729+
521ced18 15730+ h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
b912730e
AM
15731+ err = PTR_ERR(h_file);
15732+ if (IS_ERR(h_file))
15733+ goto out;
9dbd164d 15734+
5afbbe0d
AM
15735+ if (au_test_loopback_kthread()) {
15736+ au_warn_loopback(h_file->f_path.dentry->d_sb);
15737+ if (file->f_mapping != h_file->f_mapping) {
15738+ file->f_mapping = h_file->f_mapping;
15739+ smp_mb(); /* unnecessary? */
15740+ }
15741+ }
15742+ fi_read_unlock(file);
15743+
076b876e 15744+ err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
4a4d8108
AM
15745+ /* todo: necessary? */
15746+ /* file->f_ra = h_file->f_ra; */
b912730e 15747+ au_read_post(inode, h_file);
1facf9fc 15748+
4f0767ce 15749+out:
4a4d8108 15750+ si_read_unlock(sb);
1308ab2a 15751+ return err;
15752+}
1facf9fc 15753+
076b876e 15754+static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
1308ab2a 15755+{
4a4d8108 15756+ ssize_t err;
b912730e
AM
15757+ struct au_write_pre wpre;
15758+ struct inode *inode;
4a4d8108 15759+ struct file *file, *h_file;
1308ab2a 15760+
4a4d8108 15761+ file = kio->ki_filp;
b912730e 15762+ inode = file_inode(file);
e49829fe
JR
15763+ au_mtx_and_read_lock(inode);
15764+
521ced18 15765+ wpre.lsc = 0;
b912730e
AM
15766+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15767+ err = PTR_ERR(h_file);
15768+ if (IS_ERR(h_file))
9dbd164d 15769+ goto out;
9dbd164d 15770+
076b876e 15771+ err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
b912730e 15772+ au_write_post(inode, h_file, &wpre, err);
1facf9fc 15773+
4f0767ce 15774+out:
b912730e 15775+ si_read_unlock(inode->i_sb);
febd17d6 15776+ inode_unlock(inode);
dece6358 15777+ return err;
1facf9fc 15778+}
15779+
0b2a12c6
JR
15780+/*
15781+ * We may be able to remove aufs_splice_{read,write}() since almost all FSes
15782+ * don't have their own .splice_{read,write} implimentations, and they use
15783+ * generic_file_splice_read() and iter_file_splice_write() who can act like the
15784+ * simple converters to f_op->iter_read() and ->iter_write().
15785+ * But we keep our own implementations because some non-mainlined FSes may have
15786+ * their own .splice_{read,write} implimentations and aufs doesn't want to take
15787+ * away an opportunity to co-work with aufs from them.
15788+ */
4a4d8108
AM
15789+static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
15790+ struct pipe_inode_info *pipe, size_t len,
15791+ unsigned int flags)
1facf9fc 15792+{
4a4d8108
AM
15793+ ssize_t err;
15794+ struct file *h_file;
b912730e 15795+ struct inode *inode;
dece6358 15796+ struct super_block *sb;
1facf9fc 15797+
b912730e
AM
15798+ inode = file_inode(file);
15799+ sb = inode->i_sb;
e49829fe 15800+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e 15801+
521ced18 15802+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
b912730e
AM
15803+ err = PTR_ERR(h_file);
15804+ if (IS_ERR(h_file))
dece6358 15805+ goto out;
1facf9fc 15806+
171c7011 15807+ err = vfsub_splice_read(h_file, ppos, pipe, len, flags);
acd2b654 15808+ /* todo: necessary? */
4a4d8108 15809+ /* file->f_ra = h_file->f_ra; */
b912730e 15810+ au_read_post(inode, h_file);
1facf9fc 15811+
4f0767ce 15812+out:
4a4d8108 15813+ si_read_unlock(sb);
dece6358 15814+ return err;
1facf9fc 15815+}
15816+
4a4d8108
AM
15817+static ssize_t
15818+aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
15819+ size_t len, unsigned int flags)
1facf9fc 15820+{
4a4d8108 15821+ ssize_t err;
b912730e
AM
15822+ struct au_write_pre wpre;
15823+ struct inode *inode;
076b876e 15824+ struct file *h_file;
1facf9fc 15825+
b912730e 15826+ inode = file_inode(file);
e49829fe 15827+ au_mtx_and_read_lock(inode);
9dbd164d 15828+
521ced18 15829+ wpre.lsc = 0;
b912730e
AM
15830+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15831+ err = PTR_ERR(h_file);
15832+ if (IS_ERR(h_file))
9dbd164d 15833+ goto out;
9dbd164d 15834+
4a4d8108 15835+ err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
b912730e 15836+ au_write_post(inode, h_file, &wpre, err);
1facf9fc 15837+
4f0767ce 15838+out:
b912730e 15839+ si_read_unlock(inode->i_sb);
febd17d6 15840+ inode_unlock(inode);
4a4d8108
AM
15841+ return err;
15842+}
1facf9fc 15843+
38d290e6
JR
15844+static long aufs_fallocate(struct file *file, int mode, loff_t offset,
15845+ loff_t len)
15846+{
15847+ long err;
b912730e 15848+ struct au_write_pre wpre;
38d290e6
JR
15849+ struct inode *inode;
15850+ struct file *h_file;
15851+
b912730e 15852+ inode = file_inode(file);
38d290e6
JR
15853+ au_mtx_and_read_lock(inode);
15854+
521ced18 15855+ wpre.lsc = 0;
b912730e
AM
15856+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15857+ err = PTR_ERR(h_file);
15858+ if (IS_ERR(h_file))
38d290e6 15859+ goto out;
38d290e6
JR
15860+
15861+ lockdep_off();
03673fb0 15862+ err = vfs_fallocate(h_file, mode, offset, len);
38d290e6 15863+ lockdep_on();
aec24f17
AM
15864+ /*
15865+ * we don't need to call file_modifed() here since au_write_post()
15866+ * is equivalent and copies-up all timestamps and permission bits.
15867+ */
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+
f4d37d76
AM
16152+const struct file_operations aufs_file_fop = {
16153+ .owner = THIS_MODULE,
16154+
16155+ .llseek = default_llseek,
16156+
16157+ .read_iter = aufs_read_iter,
16158+ .write_iter = aufs_write_iter,
16159+
16160+#ifdef CONFIG_AUFS_POLL
16161+ .poll = aufs_poll,
16162+#endif
16163+ .unlocked_ioctl = aufs_ioctl_nondir,
16164+#ifdef CONFIG_COMPAT
16165+ .compat_ioctl = aufs_compat_ioctl_nondir,
16166+#endif
16167+ .mmap = aufs_mmap,
16168+ .open = aufs_open_nondir,
16169+ .flush = aufs_flush_nondir,
16170+ .release = aufs_release_nondir,
16171+ .fsync = aufs_fsync_nondir,
16172+ .fasync = aufs_fasync,
f4d37d76
AM
16173+ .setfl = aufs_setfl,
16174+ .splice_write = aufs_splice_write,
16175+ .splice_read = aufs_splice_read,
16176+#if 0 /* reserved for future use */
16177+ .aio_splice_write = aufs_aio_splice_write,
16178+ .aio_splice_read = aufs_aio_splice_read,
16179+#endif
16180+ .fallocate = aufs_fallocate,
16181+ .copy_file_range = aufs_copy_file_range
16182+};
16183diff -urN /usr/share/empty/fs/aufs/fsctx.c linux/fs/aufs/fsctx.c
16184--- /usr/share/empty/fs/aufs/fsctx.c 1970-01-01 01:00:00.000000000 +0100
594d0238 16185+++ linux/fs/aufs/fsctx.c 2022-11-05 23:02:18.965889284 +0100
f4d37d76
AM
16186@@ -0,0 +1,1242 @@
16187+// SPDX-License-Identifier: GPL-2.0
16188+/*
16189+ * Copyright (C) 2022 Junjiro R. Okajima
16190+ *
0dcfbb52 16191+ * This program is free software; you can redistribute it and/or modify
f4d37d76
AM
16192+ * it under the terms of the GNU General Public License as published by
16193+ * the Free Software Foundation; either version 2 of the License, or
16194+ * (at your option) any later version.
16195+ *
16196+ * This program is distributed in the hope that it will be useful,
16197+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16198+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16199+ * GNU General Public License for more details.
16200+ *
16201+ * You should have received a copy of the GNU General Public License
16202+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
16203+ */
16204+
16205+/*
16206+ * fs context, aka new mount api
16207+ */
16208+
16209+#include <linux/fs_context.h>
16210+#include "aufs.h"
16211+
16212+struct au_fsctx_opts {
16213+ aufs_bindex_t bindex;
16214+ unsigned char skipped;
16215+ struct au_opt *opt, *opt_tail;
16216+ struct super_block *sb;
16217+ struct au_sbinfo *sbinfo;
16218+ struct au_opts opts;
16219+};
16220+
16221+/* stop extra interpretation of errno in mount(8), and strange error messages */
16222+static int cvt_err(int err)
16223+{
16224+ AuTraceErr(err);
16225+
16226+ switch (err) {
16227+ case -ENOENT:
16228+ case -ENOTDIR:
16229+ case -EEXIST:
16230+ case -EIO:
16231+ err = -EINVAL;
16232+ }
16233+ return err;
16234+}
16235+
16236+static int au_fsctx_reconfigure(struct fs_context *fc)
16237+{
16238+ int err, do_dx;
16239+ unsigned int mntflags;
16240+ struct dentry *root;
16241+ struct super_block *sb;
16242+ struct inode *inode;
16243+ struct au_fsctx_opts *a = fc->fs_private;
16244+
16245+ AuDbg("fc %p\n", fc);
16246+
16247+ root = fc->root;
16248+ sb = root->d_sb;
16249+ err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
16250+ if (!err) {
16251+ di_write_lock_child(root);
16252+ err = au_opts_verify(sb, fc->sb_flags, /*pending*/0);
16253+ aufs_write_unlock(root);
16254+ }
16255+
16256+ inode = d_inode(root);
16257+ inode_lock(inode);
16258+ err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
16259+ if (unlikely(err))
16260+ goto out;
16261+ di_write_lock_child(root);
16262+
16263+ /* au_opts_remount() may return an error */
16264+ err = au_opts_remount(sb, &a->opts);
16265+
16266+ if (au_ftest_opts(a->opts.flags, REFRESH))
16267+ au_remount_refresh(sb, au_ftest_opts(a->opts.flags,
16268+ REFRESH_IDOP));
16269+
16270+ if (au_ftest_opts(a->opts.flags, REFRESH_DYAOP)) {
16271+ mntflags = au_mntflags(sb);
16272+ do_dx = !!au_opt_test(mntflags, DIO);
16273+ au_dy_arefresh(do_dx);
16274+ }
16275+
16276+ au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
16277+ aufs_write_unlock(root);
16278+
16279+out:
16280+ inode_unlock(inode);
16281+ err = cvt_err(err);
16282+ AuTraceErr(err);
16283+
16284+ return err;
16285+}
16286+
16287+/* ---------------------------------------------------------------------- */
16288+
16289+static int au_fsctx_fill_super(struct super_block *sb, struct fs_context *fc)
16290+{
16291+ int err;
16292+ struct au_fsctx_opts *a = fc->fs_private;
16293+ struct au_sbinfo *sbinfo = a->sbinfo;
16294+ struct dentry *root;
16295+ struct inode *inode;
16296+
16297+ sbinfo->si_sb = sb;
16298+ sb->s_fs_info = sbinfo;
16299+ kobject_get(&sbinfo->si_kobj);
16300+
16301+ __si_write_lock(sb);
16302+ si_pid_set(sb);
16303+ au_sbilist_add(sb);
16304+
16305+ /* all timestamps always follow the ones on the branch */
16306+ sb->s_flags |= SB_NOATIME | SB_NODIRATIME;
16307+ sb->s_flags |= SB_I_VERSION; /* do we really need this? */
16308+ sb->s_op = &aufs_sop;
16309+ sb->s_d_op = &aufs_dop;
16310+ sb->s_magic = AUFS_SUPER_MAGIC;
16311+ sb->s_maxbytes = 0;
16312+ sb->s_stack_depth = 1;
16313+ au_export_init(sb);
16314+ au_xattr_init(sb);
16315+
16316+ err = au_alloc_root(sb);
16317+ if (unlikely(err)) {
16318+ si_write_unlock(sb);
16319+ goto out;
16320+ }
16321+ root = sb->s_root;
16322+ inode = d_inode(root);
16323+ ii_write_lock_parent(inode);
16324+ aufs_write_unlock(root);
16325+
16326+ /* lock vfs_inode first, then aufs. */
16327+ inode_lock(inode);
16328+ aufs_write_lock(root);
16329+ err = au_opts_mount(sb, &a->opts);
16330+ AuTraceErr(err);
16331+ if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
16332+ sb->s_d_op = &aufs_dop_noreval;
16333+ /* infofc(fc, "%ps", sb->s_d_op); */
16334+ pr_info("%ps\n", sb->s_d_op);
16335+ au_refresh_dop(root, /*force_reval*/0);
16336+ sbinfo->si_iop_array = aufs_iop_nogetattr;
16337+ au_refresh_iop(inode, /*force_getattr*/0);
16338+ }
16339+ aufs_write_unlock(root);
16340+ inode_unlock(inode);
16341+ if (!err)
16342+ goto out; /* success */
16343+
16344+ dput(root);
16345+ sb->s_root = NULL;
16346+
16347+out:
16348+ if (unlikely(err))
16349+ kobject_put(&sbinfo->si_kobj);
16350+ AuTraceErr(err);
16351+ err = cvt_err(err);
16352+ AuTraceErr(err);
16353+ return err;
16354+}
16355+
16356+static int au_fsctx_get_tree(struct fs_context *fc)
16357+{
16358+ int err;
16359+
16360+ AuDbg("fc %p\n", fc);
16361+ err = get_tree_nodev(fc, au_fsctx_fill_super);
16362+
16363+ AuTraceErr(err);
16364+ return err;
16365+}
16366+
16367+/* ---------------------------------------------------------------------- */
16368+
16369+static void au_fsctx_dump(struct au_opts *opts)
16370+{
16371+#ifdef CONFIG_AUFS_DEBUG
16372+ /* reduce stack space */
16373+ union {
16374+ struct au_opt_add *add;
16375+ struct au_opt_del *del;
16376+ struct au_opt_mod *mod;
16377+ struct au_opt_xino *xino;
16378+ struct au_opt_xino_itrunc *xino_itrunc;
16379+ struct au_opt_wbr_create *create;
16380+ } u;
16381+ struct au_opt *opt;
16382+
16383+ opt = opts->opt;
16384+ while (opt->type != Opt_tail) {
16385+ switch (opt->type) {
16386+ case Opt_add:
16387+ u.add = &opt->add;
16388+ AuDbg("add {b%d, %s, 0x%x, %p}\n",
16389+ u.add->bindex, u.add->pathname, u.add->perm,
16390+ u.add->path.dentry);
16391+ break;
16392+ case Opt_del:
16393+ fallthrough;
16394+ case Opt_idel:
16395+ u.del = &opt->del;
16396+ AuDbg("del {%s, %p}\n",
16397+ u.del->pathname, u.del->h_path.dentry);
16398+ break;
16399+ case Opt_mod:
16400+ fallthrough;
16401+ case Opt_imod:
16402+ u.mod = &opt->mod;
16403+ AuDbg("mod {%s, 0x%x, %p}\n",
16404+ u.mod->path, u.mod->perm, u.mod->h_root);
16405+ break;
16406+ case Opt_append:
16407+ u.add = &opt->add;
16408+ AuDbg("append {b%d, %s, 0x%x, %p}\n",
16409+ u.add->bindex, u.add->pathname, u.add->perm,
16410+ u.add->path.dentry);
16411+ break;
16412+ case Opt_prepend:
16413+ u.add = &opt->add;
16414+ AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
16415+ u.add->bindex, u.add->pathname, u.add->perm,
16416+ u.add->path.dentry);
16417+ break;
16418+
16419+ case Opt_dirwh:
16420+ AuDbg("dirwh %d\n", opt->dirwh);
16421+ break;
16422+ case Opt_rdcache:
16423+ AuDbg("rdcache %d\n", opt->rdcache);
16424+ break;
16425+ case Opt_rdblk:
16426+ AuDbg("rdblk %d\n", opt->rdblk);
16427+ break;
16428+ case Opt_rdhash:
16429+ AuDbg("rdhash %u\n", opt->rdhash);
16430+ break;
16431+
16432+ case Opt_xino:
16433+ u.xino = &opt->xino;
16434+ AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
16435+ break;
16436+
16437+#define au_fsctx_TF(name) \
16438+ case Opt_##name: \
16439+ if (opt->tf) \
16440+ AuLabel(name); \
16441+ else \
16442+ AuLabel(no##name); \
16443+ break;
16444+
16445+ /* simple true/false flag */
16446+ au_fsctx_TF(trunc_xino);
16447+ au_fsctx_TF(trunc_xib);
16448+ au_fsctx_TF(dirperm1);
16449+ au_fsctx_TF(plink);
16450+ au_fsctx_TF(shwh);
16451+ au_fsctx_TF(dio);
16452+ au_fsctx_TF(warn_perm);
16453+ au_fsctx_TF(verbose);
16454+ au_fsctx_TF(sum);
16455+ au_fsctx_TF(dirren);
16456+ au_fsctx_TF(acl);
16457+#undef au_fsctx_TF
16458+
16459+ case Opt_trunc_xino_path:
16460+ fallthrough;
16461+ case Opt_itrunc_xino:
16462+ u.xino_itrunc = &opt->xino_itrunc;
16463+ AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
16464+ break;
16465+ case Opt_noxino:
16466+ AuLabel(noxino);
16467+ break;
16468+
16469+ case Opt_list_plink:
16470+ AuLabel(list_plink);
16471+ break;
16472+ case Opt_udba:
16473+ AuDbg("udba %d, %s\n",
16474+ opt->udba, au_optstr_udba(opt->udba));
16475+ break;
16476+ case Opt_diropq_a:
16477+ AuLabel(diropq_a);
16478+ break;
16479+ case Opt_diropq_w:
16480+ AuLabel(diropq_w);
16481+ break;
16482+ case Opt_wsum:
16483+ AuLabel(wsum);
16484+ break;
16485+ case Opt_wbr_create:
16486+ u.create = &opt->wbr_create;
16487+ AuDbg("create %d, %s\n", u.create->wbr_create,
16488+ au_optstr_wbr_create(u.create->wbr_create));
16489+ switch (u.create->wbr_create) {
16490+ case AuWbrCreate_MFSV:
16491+ fallthrough;
16492+ case AuWbrCreate_PMFSV:
16493+ AuDbg("%d sec\n", u.create->mfs_second);
16494+ break;
16495+ case AuWbrCreate_MFSRR:
16496+ fallthrough;
16497+ case AuWbrCreate_TDMFS:
16498+ AuDbg("%llu watermark\n",
16499+ u.create->mfsrr_watermark);
16500+ break;
16501+ case AuWbrCreate_MFSRRV:
16502+ fallthrough;
16503+ case AuWbrCreate_TDMFSV:
16504+ fallthrough;
16505+ case AuWbrCreate_PMFSRRV:
16506+ AuDbg("%llu watermark, %d sec\n",
16507+ u.create->mfsrr_watermark,
16508+ u.create->mfs_second);
16509+ break;
16510+ }
16511+ break;
16512+ case Opt_wbr_copyup:
16513+ AuDbg("copyup %d, %s\n", opt->wbr_copyup,
16514+ au_optstr_wbr_copyup(opt->wbr_copyup));
16515+ break;
16516+ case Opt_fhsm_sec:
16517+ AuDbg("fhsm_sec %u\n", opt->fhsm_second);
16518+ break;
16519+
16520+ default:
16521+ AuDbg("type %d\n", opt->type);
16522+ BUG();
16523+ }
16524+ opt++;
16525+ }
16526+#endif
16527+}
16528+
16529+/* ---------------------------------------------------------------------- */
16530+
16531+/*
16532+ * For conditionally compiled mount options.
16533+ * Instead of fsparam_flag_no(), use this macro to distinguish ignore_silent.
16534+ */
16535+#define au_ignore_flag(name, action) \
16536+ fsparam_flag(name, action), \
16537+ fsparam_flag("no" name, Opt_ignore_silent)
16538+
16539+const struct fs_parameter_spec aufs_fsctx_paramspec[] = {
16540+ fsparam_string("br", Opt_br),
16541+
16542+ /* "add=%d:%s" or "ins=%d:%s" */
16543+ fsparam_string("add", Opt_add),
16544+ fsparam_string("ins", Opt_add),
16545+ fsparam_path("append", Opt_append),
16546+ fsparam_path("prepend", Opt_prepend),
16547+
16548+ fsparam_path("del", Opt_del),
16549+ /* fsparam_s32("idel", Opt_idel), */
16550+ fsparam_path("mod", Opt_mod),
16551+ /* fsparam_string("imod", Opt_imod), */
16552+
16553+ fsparam_s32("dirwh", Opt_dirwh),
16554+
16555+ fsparam_path("xino", Opt_xino),
16556+ fsparam_flag("noxino", Opt_noxino),
16557+ fsparam_flag_no("trunc_xino", Opt_trunc_xino),
16558+ /* "trunc_xino_v=%d:%d" */
16559+ /* fsparam_string("trunc_xino_v", Opt_trunc_xino_v), */
16560+ fsparam_path("trunc_xino", Opt_trunc_xino_path),
16561+ fsparam_s32("itrunc_xino", Opt_itrunc_xino),
16562+ /* fsparam_path("zxino", Opt_zxino), */
16563+ fsparam_flag_no("trunc_xib", Opt_trunc_xib),
16564+
16565+#ifdef CONFIG_PROC_FS
16566+ fsparam_flag_no("plink", Opt_plink),
16567+#else
16568+ au_ignore_flag("plink", Opt_ignore),
16569+#endif
16570+
16571+#ifdef CONFIG_AUFS_DEBUG
16572+ fsparam_flag("list_plink", Opt_list_plink),
16573+#endif
16574+
16575+ fsparam_string("udba", Opt_udba),
16576+
16577+ fsparam_flag_no("dio", Opt_dio),
16578+
16579+#ifdef CONFIG_AUFS_DIRREN
16580+ fsparam_flag_no("dirren", Opt_dirren),
16581+#else
16582+ au_ignore_flag("dirren", Opt_ignore),
16583+#endif
16584+
16585+#ifdef CONFIG_AUFS_FHSM
16586+ fsparam_s32("fhsm_sec", Opt_fhsm_sec),
16587+#else
16588+ fsparam_s32("fhsm_sec", Opt_ignore),
16589+#endif
16590+
16591+ /* always | a | whiteouted | w */
16592+ fsparam_string("diropq", Opt_diropq),
16593+
16594+ fsparam_flag_no("warn_perm", Opt_warn_perm),
16595+
16596+#ifdef CONFIG_AUFS_SHWH
16597+ fsparam_flag_no("shwh", Opt_shwh),
16598+#else
16599+ au_ignore_flag("shwh", Opt_err),
16600+#endif
16601+
16602+ fsparam_flag_no("dirperm1", Opt_dirperm1),
16603+
16604+ fsparam_flag_no("verbose", Opt_verbose),
16605+ fsparam_flag("v", Opt_verbose),
16606+ fsparam_flag("quiet", Opt_noverbose),
16607+ fsparam_flag("q", Opt_noverbose),
16608+ /* user-space may handle this */
16609+ fsparam_flag("silent", Opt_noverbose),
16610+
16611+ fsparam_flag_no("sum", Opt_sum),
16612+ fsparam_flag("wsum", Opt_wsum),
16613+
16614+ fsparam_s32("rdcache", Opt_rdcache),
16615+ /* "def" or s32 */
16616+ fsparam_string("rdblk", Opt_rdblk),
16617+ /* "def" or s32 */
16618+ fsparam_string("rdhash", Opt_rdhash),
16619+
16620+ fsparam_string("create", Opt_wbr_create),
16621+ fsparam_string("create_policy", Opt_wbr_create),
16622+ fsparam_string("cpup", Opt_wbr_copyup),
16623+ fsparam_string("copyup", Opt_wbr_copyup),
16624+ fsparam_string("copyup_policy", Opt_wbr_copyup),
16625+
16626+ /* generic VFS flag */
16627+#ifdef CONFIG_FS_POSIX_ACL
16628+ fsparam_flag_no("acl", Opt_acl),
16629+#else
22319442 16630+ au_ignore_flag("acl", Opt_ignore),
f4d37d76
AM
16631+#endif
16632+
16633+ /* internal use for the scripts */
16634+ fsparam_string("si", Opt_ignore_silent),
16635+
16636+ /* obsoleted, keep them temporary */
16637+ fsparam_flag("nodlgt", Opt_ignore_silent),
16638+ fsparam_flag("clean_plink", Opt_ignore),
16639+ fsparam_string("dirs", Opt_br),
16640+ fsparam_u32("debug", Opt_ignore),
16641+ /* "whiteout" or "all" */
16642+ fsparam_string("delete", Opt_ignore),
16643+ fsparam_string("imap", Opt_ignore),
16644+
16645+ /* temporary workaround, due to old mount(8)? */
16646+ fsparam_flag("relatime", Opt_ignore_silent),
16647+
16648+ {}
16649+};
16650+
16651+static int au_fsctx_parse_do_add(struct fs_context *fc, struct au_opt *opt,
16652+ char *brspec, size_t speclen,
16653+ aufs_bindex_t bindex)
16654+{
16655+ int err;
16656+ char *p;
16657+
16658+ AuDbg("brspec %s\n", brspec);
16659+
16660+ err = -ENOMEM;
16661+ if (!speclen)
16662+ speclen = strlen(brspec);
16663+ /* will be freed by au_fsctx_free() */
16664+ p = kmemdup_nul(brspec, speclen, GFP_NOFS);
16665+ if (unlikely(!p)) {
16666+ errorfc(fc, "failed in %s", brspec);
16667+ goto out;
16668+ }
16669+ err = au_opt_add(opt, p, fc->sb_flags, bindex);
16670+
16671+out:
16672+ AuTraceErr(err);
16673+ return err;
16674+}
16675+
16676+static int au_fsctx_parse_br(struct fs_context *fc, char *brspec)
16677+{
16678+ int err;
16679+ char *p;
16680+ struct au_fsctx_opts *a = fc->fs_private;
16681+ struct au_opt *opt = a->opt;
16682+ aufs_bindex_t bindex = a->bindex;
16683+
16684+ AuDbg("brspec %s\n", brspec);
16685+
16686+ err = -EINVAL;
16687+ while ((p = strsep(&brspec, ":")) && *p) {
16688+ err = au_fsctx_parse_do_add(fc, opt, p, /*len*/0, bindex);
16689+ AuTraceErr(err);
16690+ if (unlikely(err))
16691+ break;
16692+ bindex++;
16693+ opt++;
16694+ if (unlikely(opt > a->opt_tail)) {
16695+ err = -E2BIG;
16696+ bindex--;
16697+ opt--;
16698+ break;
16699+ }
16700+ opt->type = Opt_tail;
16701+ a->skipped = 1;
16702+ }
16703+ a->bindex = bindex;
16704+ a->opt = opt;
16705+
16706+ AuTraceErr(err);
16707+ return err;
16708+}
16709+
16710+static int au_fsctx_parse_add(struct fs_context *fc, char *addspec)
16711+{
16712+ int err, n;
16713+ char *p;
16714+ struct au_fsctx_opts *a = fc->fs_private;
16715+ struct au_opt *opt = a->opt;
16716+
16717+ err = -EINVAL;
16718+ p = strchr(addspec, ':');
16719+ if (unlikely(!p)) {
16720+ errorfc(fc, "bad arg in %s", addspec);
16721+ goto out;
16722+ }
16723+ *p++ = '\0';
16724+ err = kstrtoint(addspec, 0, &n);
16725+ if (unlikely(err)) {
16726+ errorfc(fc, "bad integer in %s", addspec);
16727+ goto out;
16728+ }
16729+ AuDbg("n %d\n", n);
16730+ err = au_fsctx_parse_do_add(fc, opt, p, /*len*/0, n);
16731+
16732+out:
16733+ AuTraceErr(err);
16734+ return err;
16735+}
16736+
16737+static int au_fsctx_parse_del(struct fs_context *fc, struct au_opt_del *del,
16738+ struct fs_parameter *param)
16739+{
16740+ int err;
16741+
16742+ err = -ENOMEM;
16743+ /* will be freed by au_fsctx_free() */
16744+ del->pathname = kmemdup_nul(param->string, param->size, GFP_NOFS);
16745+ if (unlikely(!del->pathname))
16746+ goto out;
16747+ AuDbg("del %s\n", del->pathname);
16748+ err = vfsub_kern_path(del->pathname, AuOpt_LkupDirFlags, &del->h_path);
16749+ if (unlikely(err))
16750+ errorfc(fc, "lookup failed %s (%d)", del->pathname, err);
16751+
16752+out:
16753+ AuTraceErr(err);
16754+ return err;
16755+}
16756+
16757+#if 0 /* reserved for future use */
16758+static int au_fsctx_parse_idel(struct fs_context *fc, struct au_opt_del *del,
16759+ aufs_bindex_t bindex)
16760+{
16761+ int err;
16762+ struct super_block *sb;
16763+ struct dentry *root;
16764+ struct au_fsctx_opts *a = fc->fs_private;
16765+
16766+ sb = a->sb;
16767+ AuDebugOn(!sb);
16768+
16769+ err = -EINVAL;
16770+ root = sb->s_root;
16771+ aufs_read_lock(root, AuLock_FLUSH);
16772+ if (bindex < 0 || au_sbbot(sb) < bindex) {
16773+ errorfc(fc, "out of bounds, %d", bindex);
16774+ goto out;
16775+ }
16776+
16777+ err = 0;
16778+ del->h_path.dentry = dget(au_h_dptr(root, bindex));
16779+ del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
16780+
16781+out:
16782+ aufs_read_unlock(root, !AuLock_IR);
16783+ AuTraceErr(err);
16784+ return err;
16785+}
16786+#endif
16787+
16788+static int au_fsctx_parse_mod(struct fs_context *fc, struct au_opt_mod *mod,
16789+ struct fs_parameter *param)
16790+{
16791+ int err;
16792+ struct path path;
16793+ char *p;
16794+
16795+ err = -ENOMEM;
16796+ /* will be freed by au_fsctx_free() */
16797+ mod->path = kmemdup_nul(param->string, param->size, GFP_NOFS);
16798+ if (unlikely(!mod->path))
16799+ goto out;
16800+
16801+ err = -EINVAL;
16802+ p = strchr(mod->path, '=');
16803+ if (unlikely(!p)) {
16804+ errorfc(fc, "no permission %s", mod->path);
16805+ goto out;
16806+ }
16807+
16808+ *p++ = 0;
16809+ err = vfsub_kern_path(mod->path, AuOpt_LkupDirFlags, &path);
16810+ if (unlikely(err)) {
16811+ errorfc(fc, "lookup failed %s (%d)", mod->path, err);
16812+ goto out;
16813+ }
16814+
16815+ mod->perm = au_br_perm_val(p);
16816+ AuDbg("mod path %s, perm 0x%x, %s", mod->path, mod->perm, p);
16817+ mod->h_root = dget(path.dentry);
16818+ path_put(&path);
16819+
16820+out:
16821+ AuTraceErr(err);
16822+ return err;
16823+}
16824+
16825+#if 0 /* reserved for future use */
16826+static int au_fsctx_parse_imod(struct fs_context *fc, struct au_opt_mod *mod,
16827+ char *ibrspec)
16828+{
16829+ int err, n;
16830+ char *p;
16831+ struct super_block *sb;
16832+ struct dentry *root;
16833+ struct au_fsctx_opts *a = fc->fs_private;
16834+
16835+ sb = a->sb;
16836+ AuDebugOn(!sb);
16837+
16838+ err = -EINVAL;
16839+ p = strchr(ibrspec, ':');
16840+ if (unlikely(!p)) {
16841+ errorfc(fc, "no index, %s", ibrspec);
16842+ goto out;
16843+ }
16844+ *p++ = '\0';
16845+ err = kstrtoint(ibrspec, 0, &n);
16846+ if (unlikely(err)) {
16847+ errorfc(fc, "bad integer in %s", ibrspec);
16848+ goto out;
16849+ }
16850+ AuDbg("n %d\n", n);
16851+
16852+ root = sb->s_root;
16853+ aufs_read_lock(root, AuLock_FLUSH);
16854+ if (n < 0 || au_sbbot(sb) < n) {
16855+ errorfc(fc, "out of bounds, %d", bindex);
16856+ goto out_root;
16857+ }
16858+
16859+ err = 0;
16860+ mod->perm = au_br_perm_val(p);
16861+ AuDbg("mod path %s, perm 0x%x, %s\n",
16862+ mod->path, mod->perm, p);
16863+ mod->h_root = dget(au_h_dptr(root, bindex));
16864+
16865+out_root:
16866+ aufs_read_unlock(root, !AuLock_IR);
16867+out:
16868+ AuTraceErr(err);
16869+ return err;
16870+}
16871+#endif
16872+
16873+static int au_fsctx_parse_xino(struct fs_context *fc,
16874+ struct au_opt_xino *xino,
16875+ struct fs_parameter *param)
16876+{
16877+ int err;
16878+ struct au_fsctx_opts *a = fc->fs_private;
16879+
16880+ err = -ENOMEM;
16881+ /* will be freed by au_opts_free() */
16882+ xino->path = kmemdup_nul(param->string, param->size, GFP_NOFS);
16883+ if (unlikely(!xino->path))
16884+ goto out;
16885+ AuDbg("path %s\n", xino->path);
16886+
16887+ xino->file = au_xino_create(a->sb, xino->path, /*silent*/0,
16888+ /*wbrtop*/0);
16889+ err = PTR_ERR(xino->file);
16890+ if (IS_ERR(xino->file)) {
16891+ xino->file = NULL;
16892+ goto out;
16893+ }
16894+
16895+ err = 0;
16896+ if (unlikely(a->sb && xino->file->f_path.dentry->d_sb == a->sb)) {
16897+ err = -EINVAL;
16898+ errorfc(fc, "%s must be outside", xino->path);
16899+ }
16900+
16901+out:
16902+ AuTraceErr(err);
16903+ return err;
16904+}
16905+
16906+static
16907+int au_fsctx_parse_xino_itrunc_path(struct fs_context *fc,
16908+ struct au_opt_xino_itrunc *xino_itrunc,
16909+ char *pathname)
16910+{
16911+ int err;
16912+ aufs_bindex_t bbot, bindex;
16913+ struct path path;
16914+ struct dentry *root;
16915+ struct au_fsctx_opts *a = fc->fs_private;
16916+
16917+ AuDebugOn(!a->sb);
16918+
16919+ err = vfsub_kern_path(pathname, AuOpt_LkupDirFlags, &path);
16920+ if (unlikely(err)) {
16921+ errorfc(fc, "lookup failed %s (%d)", pathname, err);
16922+ goto out;
16923+ }
16924+
16925+ xino_itrunc->bindex = -1;
16926+ root = a->sb->s_root;
16927+ aufs_read_lock(root, AuLock_FLUSH);
16928+ bbot = au_sbbot(a->sb);
16929+ for (bindex = 0; bindex <= bbot; bindex++) {
16930+ if (au_h_dptr(root, bindex) == path.dentry) {
16931+ xino_itrunc->bindex = bindex;
16932+ break;
16933+ }
16934+ }
16935+ aufs_read_unlock(root, !AuLock_IR);
16936+ path_put(&path);
16937+
16938+ if (unlikely(xino_itrunc->bindex < 0)) {
16939+ err = -EINVAL;
16940+ errorfc(fc, "no such branch %s", pathname);
16941+ }
16942+
16943+out:
16944+ AuTraceErr(err);
16945+ return err;
16946+}
16947+
16948+static int au_fsctx_parse_xino_itrunc(struct fs_context *fc,
16949+ struct au_opt_xino_itrunc *xino_itrunc,
16950+ unsigned int bindex)
16951+{
16952+ int err;
16953+ aufs_bindex_t bbot;
16954+ struct super_block *sb;
16955+ struct au_fsctx_opts *a = fc->fs_private;
16956+
16957+ sb = a->sb;
16958+ AuDebugOn(!sb);
16959+
16960+ err = 0;
16961+ si_noflush_read_lock(sb);
16962+ bbot = au_sbbot(sb);
16963+ si_read_unlock(sb);
16964+ if (bindex <= bbot)
16965+ xino_itrunc->bindex = bindex;
16966+ else {
16967+ err = -EINVAL;
16968+ errorfc(fc, "out of bounds, %u", bindex);
16969+ }
16970+
16971+ AuTraceErr(err);
16972+ return err;
16973+}
16974+
16975+static int au_fsctx_parse_param(struct fs_context *fc, struct fs_parameter *param)
16976+{
16977+ int err, token;
16978+ struct fs_parse_result result;
16979+ struct au_fsctx_opts *a = fc->fs_private;
16980+ struct au_opt *opt = a->opt;
16981+
16982+ AuDbg("fc %p, param {key %s, string %s}\n",
16983+ fc, param->key, param->string);
16984+ err = fs_parse(fc, aufs_fsctx_paramspec, param, &result);
16985+ if (unlikely(err < 0))
16986+ goto out;
16987+ token = err;
16988+ AuDbg("token %d, res{negated %d, uint64 %llu}\n",
16989+ token, result.negated, result.uint_64);
16990+
16991+ err = -EINVAL;
16992+ a->skipped = 0;
16993+ switch (token) {
16994+ case Opt_br:
16995+ err = au_fsctx_parse_br(fc, param->string);
16996+ break;
16997+ case Opt_add:
16998+ err = au_fsctx_parse_add(fc, param->string);
16999+ break;
17000+ case Opt_append:
17001+ err = au_fsctx_parse_do_add(fc, opt, param->string, param->size,
17002+ /*dummy bindex*/1);
17003+ break;
17004+ case Opt_prepend:
17005+ err = au_fsctx_parse_do_add(fc, opt, param->string, param->size,
17006+ /*bindex*/0);
17007+ break;
17008+
17009+ case Opt_del:
17010+ err = au_fsctx_parse_del(fc, &opt->del, param);
17011+ break;
17012+#if 0 /* reserved for future use */
17013+ case Opt_idel:
17014+ if (!a->sb) {
17015+ err = 0;
17016+ a->skipped = 1;
17017+ break;
17018+ }
17019+ del->pathname = "(indexed)";
17020+ err = au_opts_parse_idel(fc, &opt->del, result.uint_32);
17021+ break;
17022+#endif
17023+
17024+ case Opt_mod:
17025+ err = au_fsctx_parse_mod(fc, &opt->mod, param);
17026+ break;
17027+#ifdef IMOD /* reserved for future use */
17028+ case Opt_imod:
17029+ if (!a->sb) {
17030+ err = 0;
17031+ a->skipped = 1;
17032+ break;
17033+ }
17034+ u.mod->path = "(indexed)";
17035+ err = au_opts_parse_imod(fc, &opt->mod, param->string);
17036+ break;
17037+#endif
17038+
17039+ case Opt_xino:
17040+ err = au_fsctx_parse_xino(fc, &opt->xino, param);
17041+ break;
17042+ case Opt_trunc_xino_path:
17043+ if (!a->sb) {
17044+ errorfc(fc, "no such branch %s", param->string);
17045+ break;
17046+ }
17047+ err = au_fsctx_parse_xino_itrunc_path(fc, &opt->xino_itrunc,
17048+ param->string);
17049+ break;
17050+#if 0
17051+ case Opt_trunc_xino_v:
17052+ if (!a->sb) {
17053+ err = 0;
17054+ a->skipped = 1;
17055+ break;
17056+ }
17057+ err = au_fsctx_parse_xino_itrunc_path(fc, &opt->xino_itrunc,
17058+ param->string);
17059+ break;
17060+#endif
17061+ case Opt_itrunc_xino:
17062+ if (!a->sb) {
17063+ errorfc(fc, "out of bounds %s", param->string);
17064+ break;
17065+ }
17066+ err = au_fsctx_parse_xino_itrunc(fc, &opt->xino_itrunc,
17067+ result.int_32);
17068+ break;
17069+
17070+ case Opt_dirwh:
17071+ err = 0;
17072+ opt->dirwh = result.int_32;
17073+ break;
17074+
17075+ case Opt_rdcache:
17076+ if (unlikely(result.int_32 > AUFS_RDCACHE_MAX)) {
17077+ errorfc(fc, "rdcache must be smaller than %d",
17078+ AUFS_RDCACHE_MAX);
17079+ break;
17080+ }
17081+ err = 0;
17082+ opt->rdcache = result.int_32;
17083+ break;
17084+
17085+ case Opt_rdblk:
17086+ err = 0;
17087+ opt->rdblk = AUFS_RDBLK_DEF;
17088+ if (!strcmp(param->string, "def"))
17089+ break;
17090+
17091+ err = kstrtoint(param->string, 0, &result.int_32);
17092+ if (unlikely(err)) {
17093+ errorfc(fc, "bad value in %s", param->key);
17094+ break;
17095+ }
17096+ err = -EINVAL;
17097+ if (unlikely(result.int_32 < 0
17098+ || result.int_32 > KMALLOC_MAX_SIZE)) {
17099+ errorfc(fc, "bad value in %s", param->key);
17100+ break;
17101+ }
17102+ if (unlikely(result.int_32 && result.int_32 < NAME_MAX)) {
17103+ errorfc(fc, "rdblk must be larger than %d", NAME_MAX);
17104+ break;
17105+ }
17106+ err = 0;
17107+ opt->rdblk = result.int_32;
17108+ break;
17109+
17110+ case Opt_rdhash:
17111+ err = 0;
17112+ opt->rdhash = AUFS_RDHASH_DEF;
17113+ if (!strcmp(param->string, "def"))
17114+ break;
17115+
17116+ err = kstrtoint(param->string, 0, &result.int_32);
17117+ if (unlikely(err)) {
17118+ errorfc(fc, "bad value in %s", param->key);
17119+ break;
17120+ }
17121+ /* how about zero? */
17122+ if (result.int_32 < 0
17123+ || result.int_32 * sizeof(struct hlist_head)
17124+ > KMALLOC_MAX_SIZE) {
17125+ err = -EINVAL;
17126+ errorfc(fc, "bad integer in %s", param->key);
17127+ break;
17128+ }
17129+ opt->rdhash = result.int_32;
17130+ break;
17131+
17132+ case Opt_diropq:
17133+ /*
17134+ * As other options, fs/aufs/opts.c can handle these strings by
17135+ * match_token(). But "diropq=" is deprecated now and will
17136+ * never have other value. So simple strcmp() is enough here.
17137+ */
17138+ if (!strcmp(param->string, "a") ||
17139+ !strcmp(param->string, "always")) {
17140+ err = 0;
17141+ opt->type = Opt_diropq_a;
17142+ } else if (!strcmp(param->string, "w") ||
17143+ !strcmp(param->string, "whiteouted")) {
17144+ err = 0;
17145+ opt->type = Opt_diropq_w;
17146+ } else
17147+ errorfc(fc, "unknown value %s", param->string);
17148+ break;
17149+
17150+ case Opt_udba:
17151+ opt->udba = au_udba_val(param->string);
17152+ if (opt->udba >= 0)
17153+ err = 0;
17154+ else
17155+ errorf(fc, "wrong value, %s", param->string);
17156+ break;
17157+
17158+ case Opt_wbr_create:
17159+ opt->wbr_create.wbr_create
17160+ = au_wbr_create_val(param->string, &opt->wbr_create);
17161+ if (opt->wbr_create.wbr_create >= 0)
17162+ err = 0;
17163+ else
17164+ errorf(fc, "wrong value, %s", param->key);
17165+ break;
17166+
17167+ case Opt_wbr_copyup:
17168+ opt->wbr_copyup = au_wbr_copyup_val(param->string);
17169+ if (opt->wbr_copyup >= 0)
17170+ err = 0;
17171+ else
17172+ errorfc(fc, "wrong value, %s", param->key);
17173+ break;
17174+
17175+ case Opt_fhsm_sec:
17176+ if (unlikely(result.int_32 < 0)) {
17177+ errorfc(fc, "bad integer in %s\n", param->key);
17178+ break;
17179+ }
17180+ err = 0;
17181+ if (sysaufs_brs)
17182+ opt->fhsm_second = result.int_32;
17183+ else
17184+ warnfc(fc, "ignored %s %s", param->key, param->string);
17185+ break;
17186+
17187+ /* simple true/false flag */
17188+#define au_fsctx_TF(name) \
17189+ case Opt_##name: \
17190+ err = 0; \
17191+ opt->tf = !result.negated; \
22319442 17192+ break
f4d37d76
AM
17193+ au_fsctx_TF(trunc_xino);
17194+ au_fsctx_TF(trunc_xib);
17195+ au_fsctx_TF(dirperm1);
17196+ au_fsctx_TF(plink);
17197+ au_fsctx_TF(shwh);
17198+ au_fsctx_TF(dio);
17199+ au_fsctx_TF(warn_perm);
17200+ au_fsctx_TF(verbose);
17201+ au_fsctx_TF(sum);
17202+ au_fsctx_TF(dirren);
17203+ au_fsctx_TF(acl);
17204+#undef au_fsctx_TF
17205+
17206+ case Opt_noverbose:
17207+ err = 0;
17208+ opt->type = Opt_verbose;
17209+ opt->tf = false;
17210+ break;
17211+
17212+ case Opt_noxino:
17213+ fallthrough;
17214+ case Opt_list_plink:
17215+ fallthrough;
17216+ case Opt_wsum:
17217+ err = 0;
17218+ break;
17219+
17220+ case Opt_ignore:
17221+ warnfc(fc, "ignored %s", param->key);
17222+ fallthrough;
17223+ case Opt_ignore_silent:
17224+ a->skipped = 1;
17225+ err = 0;
17226+ break;
17227+ default:
17228+ a->skipped = 1;
17229+ err = -ENOPARAM;
17230+ break;
17231+ }
17232+ if (unlikely(err))
17233+ goto out;
17234+ if (a->skipped)
17235+ goto out;
17236+
17237+ switch (token) {
17238+ case Opt_br:
17239+ fallthrough;
17240+ case Opt_noverbose:
17241+ fallthrough;
17242+ case Opt_diropq:
17243+ break;
17244+ default:
17245+ opt->type = token;
17246+ break;
521ced18 17247+ }
f4d37d76
AM
17248+ opt++;
17249+ if (unlikely(opt > a->opt_tail)) {
17250+ err = -E2BIG;
17251+ opt--;
17252+ }
17253+ opt->type = Opt_tail;
17254+ a->opt = opt;
521ced18 17255+
521ced18
JR
17256+out:
17257+ return err;
521ced18
JR
17258+}
17259+
9dbd164d 17260+/*
f4d37d76
AM
17261+ * these options accept both 'name=val' and 'name:val' form.
17262+ * some accept optional '=' in its value.
17263+ * eg. br:/br1=rw:/br2=ro and br=/br1=rw:/br2=ro
9dbd164d 17264+ */
f4d37d76 17265+static inline unsigned int is_colonopt(char *str)
2dfbb274 17266+{
f4d37d76
AM
17267+#define do_test(name) \
17268+ if (!strncmp(str, name ":", sizeof(name))) \
22319442 17269+ return sizeof(name) - 1
f4d37d76
AM
17270+ do_test("br");
17271+ do_test("add");
17272+ do_test("ins");
17273+ do_test("append");
17274+ do_test("prepend");
17275+ do_test("del");
17276+ /* do_test("idel"); */
17277+ do_test("mod");
17278+ /* do_test("imod"); */
17279+#undef do_test
2dfbb274 17280+
f4d37d76 17281+ return 0;
2dfbb274
AM
17282+}
17283+
f4d37d76 17284+static int au_fsctx_parse_monolithic(struct fs_context *fc, void *data)
dece6358 17285+{
4a4d8108 17286+ int err;
f4d37d76
AM
17287+ unsigned int u;
17288+ char *str;
17289+ struct au_fsctx_opts *a = fc->fs_private;
17290+
17291+ str = data;
17292+ AuDbg("str %s\n", str);
17293+ while (str) {
17294+ u = is_colonopt(str);
17295+ if (u)
17296+ str[u] = '=';
17297+ str = strchr(str, ',');
17298+ if (!str)
17299+ break;
17300+ str++;
b912730e 17301+ }
f4d37d76
AM
17302+ str = data;
17303+ AuDbg("str %s\n", str);
b912730e 17304+
f4d37d76 17305+ err = generic_parse_monolithic(fc, str);
9dbd164d 17306+ AuTraceErr(err);
f4d37d76
AM
17307+ au_fsctx_dump(&a->opts);
17308+
4a4d8108
AM
17309+ return err;
17310+}
17311+
17312+/* ---------------------------------------------------------------------- */
17313+
f4d37d76 17314+static void au_fsctx_opts_free(struct au_opts *opts)
4a4d8108 17315+{
f4d37d76 17316+ struct au_opt *opt;
4a4d8108 17317+
f4d37d76
AM
17318+ opt = opts->opt;
17319+ while (opt->type != Opt_tail) {
17320+ switch (opt->type) {
17321+ case Opt_add:
17322+ fallthrough;
17323+ case Opt_append:
17324+ fallthrough;
17325+ case Opt_prepend:
17326+ kfree(opt->add.pathname);
17327+ path_put(&opt->add.path);
17328+ break;
17329+ case Opt_del:
17330+ kfree(opt->del.pathname);
17331+ fallthrough;
17332+ case Opt_idel:
17333+ path_put(&opt->del.h_path);
17334+ break;
17335+ case Opt_mod:
17336+ kfree(opt->mod.path);
17337+ fallthrough;
17338+ case Opt_imod:
17339+ dput(opt->mod.h_root);
17340+ break;
17341+ case Opt_xino:
17342+ kfree(opt->xino.path);
17343+ fput(opt->xino.file);
17344+ break;
17345+ }
17346+ opt++;
17347+ }
dece6358
AM
17348+}
17349+
f4d37d76 17350+static void au_fsctx_free(struct fs_context *fc)
dece6358 17351+{
f4d37d76 17352+ struct au_fsctx_opts *a = fc->fs_private;
4a4d8108 17353+
f4d37d76
AM
17354+ /* fs_type=%p, root=%pD */
17355+ AuDbg("fc %p{sb_flags 0x%x, sb_flags_mask 0x%x, purpose %u\n",
17356+ fc, fc->sb_flags, fc->sb_flags_mask, fc->purpose);
1308ab2a 17357+
f4d37d76
AM
17358+ kobject_put(&a->sbinfo->si_kobj);
17359+ au_fsctx_opts_free(&a->opts);
17360+ free_page((unsigned long)a->opts.opt);
17361+ au_kfree_rcu(a);
dece6358 17362+}
4a4d8108 17363+
f4d37d76
AM
17364+static const struct fs_context_operations au_fsctx_ops = {
17365+ .free = au_fsctx_free,
17366+ .parse_param = au_fsctx_parse_param,
17367+ .parse_monolithic = au_fsctx_parse_monolithic,
17368+ .get_tree = au_fsctx_get_tree,
17369+ .reconfigure = au_fsctx_reconfigure
17370+ /*
17371+ * nfs4 requires ->dup()? No.
17372+ * I don't know what is this ->dup() for.
17373+ */
17374+};
17375+
17376+int aufs_fsctx_init(struct fs_context *fc)
febd17d6
JR
17377+{
17378+ int err;
f4d37d76 17379+ struct au_fsctx_opts *a;
febd17d6 17380+
f4d37d76
AM
17381+ /* fs_type=%p, root=%pD */
17382+ AuDbg("fc %p{sb_flags 0x%x, sb_flags_mask 0x%x, purpose %u\n",
17383+ fc, fc->sb_flags, fc->sb_flags_mask, fc->purpose);
febd17d6 17384+
f4d37d76
AM
17385+ /* they will be freed by au_fsctx_free() */
17386+ err = -ENOMEM;
17387+ a = kzalloc(sizeof(*a), GFP_NOFS);
17388+ if (unlikely(!a))
febd17d6 17389+ goto out;
f4d37d76
AM
17390+ a->bindex = 0;
17391+ a->opts.opt = (void *)__get_free_page(GFP_NOFS);
17392+ if (unlikely(!a->opts.opt))
17393+ goto out_a;
17394+ a->opt = a->opts.opt;
17395+ a->opt->type = Opt_tail;
17396+ a->opts.max_opt = PAGE_SIZE / sizeof(*a->opts.opt);
17397+ a->opt_tail = a->opt + a->opts.max_opt - 1;
17398+ a->opts.sb_flags = fc->sb_flags;
17399+
17400+ a->sb = NULL;
17401+ if (fc->root) {
17402+ AuDebugOn(fc->purpose != FS_CONTEXT_FOR_RECONFIGURE);
17403+ a->opts.flags = AuOpts_REMOUNT;
17404+ a->sb = fc->root->d_sb;
17405+ a->sbinfo = au_sbi(a->sb);
17406+ kobject_get(&a->sbinfo->si_kobj);
17407+ } else {
17408+ a->sbinfo = au_si_alloc(a->sb);
17409+ AuDebugOn(!a->sbinfo);
17410+ err = PTR_ERR(a->sbinfo);
17411+ if (IS_ERR(a->sbinfo))
17412+ goto out_opt;
17413+ au_rw_write_unlock(&a->sbinfo->si_rwsem);
17414+ }
febd17d6 17415+
f4d37d76
AM
17416+ err = 0;
17417+ fc->fs_private = a;
17418+ fc->ops = &au_fsctx_ops;
17419+ goto out; /* success */
febd17d6 17420+
f4d37d76
AM
17421+out_opt:
17422+ free_page((unsigned long)a->opts.opt);
17423+out_a:
17424+ au_kfree_rcu(a);
febd17d6 17425+out:
f4d37d76 17426+ AuTraceErr(err);
febd17d6
JR
17427+ return err;
17428+}
7f207e10 17429diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
eca34b5c 17430--- /usr/share/empty/fs/aufs/fstype.h 1970-01-01 01:00:00.000000000 +0100
594d0238 17431+++ linux/fs/aufs/fstype.h 2022-11-05 23:02:18.965889284 +0100
062440b3
AM
17432@@ -0,0 +1,401 @@
17433+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 17434+/*
0dcfbb52 17435+ * Copyright (C) 2005-2022 Junjiro R. Okajima
4a4d8108 17436+ *
0dcfbb52 17437+ * This program is free software; you can redistribute it and/or modify
4a4d8108
AM
17438+ * it under the terms of the GNU General Public License as published by
17439+ * the Free Software Foundation; either version 2 of the License, or
17440+ * (at your option) any later version.
17441+ *
17442+ * This program is distributed in the hope that it will be useful,
17443+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17444+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17445+ * GNU General Public License for more details.
17446+ *
17447+ * You should have received a copy of the GNU General Public License
523b37e3 17448+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
17449+ */
17450+
17451+/*
17452+ * judging filesystem type
17453+ */
17454+
17455+#ifndef __AUFS_FSTYPE_H__
17456+#define __AUFS_FSTYPE_H__
17457+
17458+#ifdef __KERNEL__
17459+
17460+#include <linux/fs.h>
17461+#include <linux/magic.h>
b912730e 17462+#include <linux/nfs_fs.h>
b95c5147 17463+#include <linux/romfs_fs.h>
4a4d8108
AM
17464+
17465+static inline int au_test_aufs(struct super_block *sb)
17466+{
17467+ return sb->s_magic == AUFS_SUPER_MAGIC;
17468+}
17469+
17470+static inline const char *au_sbtype(struct super_block *sb)
17471+{
17472+ return sb->s_type->name;
17473+}
1308ab2a 17474+
17475+static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
17476+{
f0c0a007 17477+#if IS_ENABLED(CONFIG_ISO9660_FS)
2000de60 17478+ return sb->s_magic == ISOFS_SUPER_MAGIC;
dece6358
AM
17479+#else
17480+ return 0;
17481+#endif
17482+}
17483+
1308ab2a 17484+static inline int au_test_romfs(struct super_block *sb __maybe_unused)
dece6358 17485+{
f0c0a007 17486+#if IS_ENABLED(CONFIG_ROMFS_FS)
2000de60 17487+ return sb->s_magic == ROMFS_MAGIC;
dece6358
AM
17488+#else
17489+ return 0;
17490+#endif
17491+}
17492+
1308ab2a 17493+static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
dece6358 17494+{
f0c0a007 17495+#if IS_ENABLED(CONFIG_CRAMFS)
1308ab2a 17496+ return sb->s_magic == CRAMFS_MAGIC;
17497+#endif
17498+ return 0;
17499+}
17500+
17501+static inline int au_test_nfs(struct super_block *sb __maybe_unused)
17502+{
f0c0a007 17503+#if IS_ENABLED(CONFIG_NFS_FS)
1308ab2a 17504+ return sb->s_magic == NFS_SUPER_MAGIC;
dece6358
AM
17505+#else
17506+ return 0;
17507+#endif
17508+}
17509+
1308ab2a 17510+static inline int au_test_fuse(struct super_block *sb __maybe_unused)
dece6358 17511+{
f0c0a007 17512+#if IS_ENABLED(CONFIG_FUSE_FS)
1308ab2a 17513+ return sb->s_magic == FUSE_SUPER_MAGIC;
dece6358
AM
17514+#else
17515+ return 0;
17516+#endif
17517+}
17518+
1308ab2a 17519+static inline int au_test_xfs(struct super_block *sb __maybe_unused)
dece6358 17520+{
f0c0a007 17521+#if IS_ENABLED(CONFIG_XFS_FS)
1308ab2a 17522+ return sb->s_magic == XFS_SB_MAGIC;
dece6358
AM
17523+#else
17524+ return 0;
17525+#endif
17526+}
17527+
1308ab2a 17528+static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
dece6358 17529+{
1308ab2a 17530+#ifdef CONFIG_TMPFS
17531+ return sb->s_magic == TMPFS_MAGIC;
17532+#else
17533+ return 0;
dece6358 17534+#endif
dece6358
AM
17535+}
17536+
1308ab2a 17537+static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
1facf9fc 17538+{
f0c0a007 17539+#if IS_ENABLED(CONFIG_ECRYPT_FS)
1308ab2a 17540+ return !strcmp(au_sbtype(sb), "ecryptfs");
17541+#else
17542+ return 0;
17543+#endif
1facf9fc 17544+}
17545+
1308ab2a 17546+static inline int au_test_ramfs(struct super_block *sb)
17547+{
17548+ return sb->s_magic == RAMFS_MAGIC;
17549+}
17550+
17551+static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
17552+{
f0c0a007 17553+#if IS_ENABLED(CONFIG_UBIFS_FS)
1308ab2a 17554+ return sb->s_magic == UBIFS_SUPER_MAGIC;
17555+#else
17556+ return 0;
17557+#endif
17558+}
17559+
17560+static inline int au_test_procfs(struct super_block *sb __maybe_unused)
17561+{
17562+#ifdef CONFIG_PROC_FS
17563+ return sb->s_magic == PROC_SUPER_MAGIC;
17564+#else
17565+ return 0;
17566+#endif
17567+}
17568+
17569+static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
17570+{
17571+#ifdef CONFIG_SYSFS
17572+ return sb->s_magic == SYSFS_MAGIC;
17573+#else
17574+ return 0;
17575+#endif
17576+}
17577+
17578+static inline int au_test_configfs(struct super_block *sb __maybe_unused)
17579+{
f0c0a007 17580+#if IS_ENABLED(CONFIG_CONFIGFS_FS)
1308ab2a 17581+ return sb->s_magic == CONFIGFS_MAGIC;
17582+#else
17583+ return 0;
17584+#endif
17585+}
17586+
17587+static inline int au_test_minix(struct super_block *sb __maybe_unused)
17588+{
f0c0a007 17589+#if IS_ENABLED(CONFIG_MINIX_FS)
1308ab2a 17590+ return sb->s_magic == MINIX3_SUPER_MAGIC
17591+ || sb->s_magic == MINIX2_SUPER_MAGIC
17592+ || sb->s_magic == MINIX2_SUPER_MAGIC2
17593+ || sb->s_magic == MINIX_SUPER_MAGIC
17594+ || sb->s_magic == MINIX_SUPER_MAGIC2;
17595+#else
17596+ return 0;
17597+#endif
17598+}
17599+
1308ab2a 17600+static inline int au_test_fat(struct super_block *sb __maybe_unused)
17601+{
f0c0a007 17602+#if IS_ENABLED(CONFIG_FAT_FS)
1308ab2a 17603+ return sb->s_magic == MSDOS_SUPER_MAGIC;
17604+#else
17605+ return 0;
17606+#endif
17607+}
17608+
17609+static inline int au_test_msdos(struct super_block *sb)
17610+{
17611+ return au_test_fat(sb);
17612+}
17613+
17614+static inline int au_test_vfat(struct super_block *sb)
17615+{
17616+ return au_test_fat(sb);
17617+}
17618+
17619+static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
17620+{
17621+#ifdef CONFIG_SECURITYFS
17622+ return sb->s_magic == SECURITYFS_MAGIC;
17623+#else
17624+ return 0;
17625+#endif
17626+}
17627+
17628+static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
17629+{
f0c0a007 17630+#if IS_ENABLED(CONFIG_SQUASHFS)
1308ab2a 17631+ return sb->s_magic == SQUASHFS_MAGIC;
17632+#else
17633+ return 0;
17634+#endif
17635+}
17636+
17637+static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
17638+{
f0c0a007 17639+#if IS_ENABLED(CONFIG_BTRFS_FS)
1308ab2a 17640+ return sb->s_magic == BTRFS_SUPER_MAGIC;
17641+#else
17642+ return 0;
17643+#endif
17644+}
17645+
17646+static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
17647+{
f0c0a007 17648+#if IS_ENABLED(CONFIG_XENFS)
1308ab2a 17649+ return sb->s_magic == XENFS_SUPER_MAGIC;
17650+#else
17651+ return 0;
17652+#endif
17653+}
17654+
17655+static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
17656+{
17657+#ifdef CONFIG_DEBUG_FS
17658+ return sb->s_magic == DEBUGFS_MAGIC;
17659+#else
17660+ return 0;
17661+#endif
17662+}
17663+
17664+static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
17665+{
f0c0a007 17666+#if IS_ENABLED(CONFIG_NILFS)
1308ab2a 17667+ return sb->s_magic == NILFS_SUPER_MAGIC;
17668+#else
17669+ return 0;
17670+#endif
17671+}
17672+
4a4d8108
AM
17673+static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
17674+{
f0c0a007 17675+#if IS_ENABLED(CONFIG_HFSPLUS_FS)
4a4d8108
AM
17676+ return sb->s_magic == HFSPLUS_SUPER_MAGIC;
17677+#else
17678+ return 0;
17679+#endif
17680+}
17681+
1308ab2a 17682+/* ---------------------------------------------------------------------- */
17683+/*
17684+ * they can't be an aufs branch.
17685+ */
17686+static inline int au_test_fs_unsuppoted(struct super_block *sb)
17687+{
17688+ return
17689+#ifndef CONFIG_AUFS_BR_RAMFS
17690+ au_test_ramfs(sb) ||
17691+#endif
17692+ au_test_procfs(sb)
17693+ || au_test_sysfs(sb)
17694+ || au_test_configfs(sb)
17695+ || au_test_debugfs(sb)
17696+ || au_test_securityfs(sb)
17697+ || au_test_xenfs(sb)
17698+ || au_test_ecryptfs(sb)
17699+ /* || !strcmp(au_sbtype(sb), "unionfs") */
17700+ || au_test_aufs(sb); /* will be supported in next version */
17701+}
17702+
1308ab2a 17703+static inline int au_test_fs_remote(struct super_block *sb)
17704+{
17705+ return !au_test_tmpfs(sb)
17706+#ifdef CONFIG_AUFS_BR_RAMFS
17707+ && !au_test_ramfs(sb)
17708+#endif
17709+ && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
17710+}
17711+
17712+/* ---------------------------------------------------------------------- */
17713+
17714+/*
17715+ * Note: these functions (below) are created after reading ->getattr() in all
17716+ * filesystems under linux/fs. it means we have to do so in every update...
17717+ */
17718+
17719+/*
17720+ * some filesystems require getattr to refresh the inode attributes before
17721+ * referencing.
17722+ * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
17723+ * and leave the work for d_revalidate()
17724+ */
17725+static inline int au_test_fs_refresh_iattr(struct super_block *sb)
17726+{
17727+ return au_test_nfs(sb)
17728+ || au_test_fuse(sb)
1308ab2a 17729+ /* || au_test_btrfs(sb) */ /* untested */
1308ab2a 17730+ ;
17731+}
17732+
17733+/*
17734+ * filesystems which don't maintain i_size or i_blocks.
17735+ */
17736+static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
17737+{
17738+ return au_test_xfs(sb)
4a4d8108
AM
17739+ || au_test_btrfs(sb)
17740+ || au_test_ubifs(sb)
17741+ || au_test_hfsplus(sb) /* maintained, but incorrect */
1308ab2a 17742+ /* || au_test_minix(sb) */ /* untested */
17743+ ;
17744+}
17745+
17746+/*
17747+ * filesystems which don't store the correct value in some of their inode
17748+ * attributes.
17749+ */
17750+static inline int au_test_fs_bad_iattr(struct super_block *sb)
17751+{
17752+ return au_test_fs_bad_iattr_size(sb)
1308ab2a 17753+ || au_test_fat(sb)
17754+ || au_test_msdos(sb)
17755+ || au_test_vfat(sb);
1facf9fc 17756+}
17757+
17758+/* they don't check i_nlink in link(2) */
17759+static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
17760+{
17761+ return au_test_tmpfs(sb)
17762+#ifdef CONFIG_AUFS_BR_RAMFS
17763+ || au_test_ramfs(sb)
17764+#endif
4a4d8108 17765+ || au_test_ubifs(sb)
4a4d8108 17766+ || au_test_hfsplus(sb);
1facf9fc 17767+}
17768+
17769+/*
17770+ * filesystems which sets S_NOATIME and S_NOCMTIME.
17771+ */
17772+static inline int au_test_fs_notime(struct super_block *sb)
17773+{
17774+ return au_test_nfs(sb)
17775+ || au_test_fuse(sb)
dece6358 17776+ || au_test_ubifs(sb)
1facf9fc 17777+ ;
17778+}
17779+
1facf9fc 17780+/* temporary support for i#1 in cramfs */
17781+static inline int au_test_fs_unique_ino(struct inode *inode)
17782+{
17783+ if (au_test_cramfs(inode->i_sb))
17784+ return inode->i_ino != 1;
17785+ return 1;
17786+}
17787+
17788+/* ---------------------------------------------------------------------- */
17789+
17790+/*
17791+ * the filesystem where the xino files placed must support i/o after unlink and
17792+ * maintain i_size and i_blocks.
17793+ */
17794+static inline int au_test_fs_bad_xino(struct super_block *sb)
17795+{
17796+ return au_test_fs_remote(sb)
17797+ || au_test_fs_bad_iattr_size(sb)
1facf9fc 17798+ /* don't want unnecessary work for xino */
17799+ || au_test_aufs(sb)
1308ab2a 17800+ || au_test_ecryptfs(sb)
17801+ || au_test_nilfs(sb);
1facf9fc 17802+}
17803+
17804+static inline int au_test_fs_trunc_xino(struct super_block *sb)
17805+{
17806+ return au_test_tmpfs(sb)
17807+ || au_test_ramfs(sb);
17808+}
17809+
17810+/*
17811+ * test if the @sb is real-readonly.
17812+ */
17813+static inline int au_test_fs_rr(struct super_block *sb)
17814+{
17815+ return au_test_squashfs(sb)
17816+ || au_test_iso9660(sb)
17817+ || au_test_cramfs(sb)
17818+ || au_test_romfs(sb);
17819+}
17820+
b912730e
AM
17821+/*
17822+ * test if the @inode is nfs with 'noacl' option
2121bcd9 17823+ * NFS always sets SB_POSIXACL regardless its mount option 'noacl.'
b912730e
AM
17824+ */
17825+static inline int au_test_nfs_noacl(struct inode *inode)
17826+{
17827+ return au_test_nfs(inode->i_sb)
17828+ /* && IS_POSIXACL(inode) */
17829+ && !nfs_server_capable(inode, NFS_CAP_ACLS);
17830+}
17831+
1facf9fc 17832+#endif /* __KERNEL__ */
17833+#endif /* __AUFS_FSTYPE_H__ */
8b6a4947 17834diff -urN /usr/share/empty/fs/aufs/hbl.h linux/fs/aufs/hbl.h
eca34b5c 17835--- /usr/share/empty/fs/aufs/hbl.h 1970-01-01 01:00:00.000000000 +0100
594d0238 17836+++ linux/fs/aufs/hbl.h 2022-11-05 23:02:18.965889284 +0100
062440b3
AM
17837@@ -0,0 +1,65 @@
17838+/* SPDX-License-Identifier: GPL-2.0 */
8b6a4947 17839+/*
0dcfbb52 17840+ * Copyright (C) 2017-2022 Junjiro R. Okajima
8b6a4947 17841+ *
0dcfbb52 17842+ * This program is free software; you can redistribute it and/or modify
8b6a4947
AM
17843+ * it under the terms of the GNU General Public License as published by
17844+ * the Free Software Foundation; either version 2 of the License, or
17845+ * (at your option) any later version.
17846+ *
17847+ * This program is distributed in the hope that it will be useful,
17848+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17849+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17850+ * GNU General Public License for more details.
17851+ *
17852+ * You should have received a copy of the GNU General Public License
17853+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
17854+ */
17855+
17856+/*
17857+ * helpers for hlist_bl.h
17858+ */
17859+
17860+#ifndef __AUFS_HBL_H__
17861+#define __AUFS_HBL_H__
17862+
17863+#ifdef __KERNEL__
17864+
17865+#include <linux/list_bl.h>
17866+
17867+static inline void au_hbl_add(struct hlist_bl_node *node,
17868+ struct hlist_bl_head *hbl)
17869+{
17870+ hlist_bl_lock(hbl);
17871+ hlist_bl_add_head(node, hbl);
17872+ hlist_bl_unlock(hbl);
17873+}
17874+
17875+static inline void au_hbl_del(struct hlist_bl_node *node,
17876+ struct hlist_bl_head *hbl)
17877+{
17878+ hlist_bl_lock(hbl);
17879+ hlist_bl_del(node);
17880+ hlist_bl_unlock(hbl);
17881+}
17882+
17883+#define au_hbl_for_each(pos, head) \
17884+ for (pos = hlist_bl_first(head); \
17885+ pos; \
17886+ pos = pos->next)
17887+
17888+static inline unsigned long au_hbl_count(struct hlist_bl_head *hbl)
17889+{
17890+ unsigned long cnt;
17891+ struct hlist_bl_node *pos;
17892+
17893+ cnt = 0;
17894+ hlist_bl_lock(hbl);
17895+ au_hbl_for_each(pos, hbl)
17896+ cnt++;
17897+ hlist_bl_unlock(hbl);
17898+ return cnt;
17899+}
17900+
17901+#endif /* __KERNEL__ */
17902+#endif /* __AUFS_HBL_H__ */
7f207e10 17903diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
eca34b5c 17904--- /usr/share/empty/fs/aufs/hfsnotify.c 1970-01-01 01:00:00.000000000 +0100
594d0238 17905+++ linux/fs/aufs/hfsnotify.c 2022-11-05 23:02:18.965889284 +0100
22319442 17906@@ -0,0 +1,290 @@
cd7a4cd9 17907+// SPDX-License-Identifier: GPL-2.0
1facf9fc 17908+/*
0dcfbb52 17909+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 17910+ *
0dcfbb52 17911+ * This program is free software; you can redistribute it and/or modify
1facf9fc 17912+ * it under the terms of the GNU General Public License as published by
17913+ * the Free Software Foundation; either version 2 of the License, or
17914+ * (at your option) any later version.
dece6358
AM
17915+ *
17916+ * This program is distributed in the hope that it will be useful,
17917+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17918+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17919+ * GNU General Public License for more details.
17920+ *
17921+ * You should have received a copy of the GNU General Public License
523b37e3 17922+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 17923+ */
17924+
17925+/*
4a4d8108 17926+ * fsnotify for the lower directories
1facf9fc 17927+ */
17928+
17929+#include "aufs.h"
17930+
4a4d8108
AM
17931+/* FS_IN_IGNORED is unnecessary */
17932+static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
17933+ | FS_CREATE | FS_EVENT_ON_CHILD);
7f207e10 17934+static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
7eafdf33 17935+static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
1facf9fc 17936+
0c5527e5 17937+static void au_hfsn_free_mark(struct fsnotify_mark *mark)
1facf9fc 17938+{
0c5527e5
AM
17939+ struct au_hnotify *hn = container_of(mark, struct au_hnotify,
17940+ hn_mark);
5afbbe0d 17941+ /* AuDbg("here\n"); */
1c60b727 17942+ au_cache_free_hnotify(hn);
8b6a4947 17943+ smp_mb__before_atomic(); /* for atomic64_dec */
1716fcea
AM
17944+ if (atomic64_dec_and_test(&au_hfsn_ifree))
17945+ wake_up(&au_hfsn_wq);
4a4d8108 17946+}
1facf9fc 17947+
027c5e7a 17948+static int au_hfsn_alloc(struct au_hinode *hinode)
4a4d8108 17949+{
1716fcea 17950+ int err;
027c5e7a
AM
17951+ struct au_hnotify *hn;
17952+ struct super_block *sb;
17953+ struct au_branch *br;
0c5527e5 17954+ struct fsnotify_mark *mark;
027c5e7a 17955+ aufs_bindex_t bindex;
1facf9fc 17956+
027c5e7a
AM
17957+ hn = hinode->hi_notify;
17958+ sb = hn->hn_aufs_inode->i_sb;
17959+ bindex = au_br_index(sb, hinode->hi_id);
17960+ br = au_sbr(sb, bindex);
1716fcea
AM
17961+ AuDebugOn(!br->br_hfsn);
17962+
0c5527e5 17963+ mark = &hn->hn_mark;
ffa93bbd 17964+ fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
0c5527e5 17965+ mark->mask = AuHfsnMask;
7f207e10
AM
17966+ /*
17967+ * by udba rename or rmdir, aufs assign a new inode to the known
17968+ * h_inode, so specify 1 to allow dups.
17969+ */
c1595e42 17970+ lockdep_off();
acd2b654 17971+ err = fsnotify_add_inode_mark(mark, hinode->hi_inode, /*allow_dups*/1);
c1595e42 17972+ lockdep_on();
1716fcea
AM
17973+
17974+ return err;
1facf9fc 17975+}
17976+
7eafdf33 17977+static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
1facf9fc 17978+{
0c5527e5 17979+ struct fsnotify_mark *mark;
7eafdf33 17980+ unsigned long long ull;
1716fcea 17981+ struct fsnotify_group *group;
7eafdf33
AM
17982+
17983+ ull = atomic64_inc_return(&au_hfsn_ifree);
17984+ BUG_ON(!ull);
953406b4 17985+
0c5527e5 17986+ mark = &hn->hn_mark;
1716fcea
AM
17987+ spin_lock(&mark->lock);
17988+ group = mark->group;
17989+ fsnotify_get_group(group);
17990+ spin_unlock(&mark->lock);
c1595e42 17991+ lockdep_off();
1716fcea 17992+ fsnotify_destroy_mark(mark, group);
5afbbe0d 17993+ fsnotify_put_mark(mark);
1716fcea 17994+ fsnotify_put_group(group);
c1595e42 17995+ lockdep_on();
7f207e10 17996+
7eafdf33
AM
17997+ /* free hn by myself */
17998+ return 0;
1facf9fc 17999+}
18000+
18001+/* ---------------------------------------------------------------------- */
18002+
4a4d8108 18003+static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
1facf9fc 18004+{
0c5527e5 18005+ struct fsnotify_mark *mark;
1facf9fc 18006+
0c5527e5
AM
18007+ mark = &hinode->hi_notify->hn_mark;
18008+ spin_lock(&mark->lock);
1facf9fc 18009+ if (do_set) {
0c5527e5
AM
18010+ AuDebugOn(mark->mask & AuHfsnMask);
18011+ mark->mask |= AuHfsnMask;
1facf9fc 18012+ } else {
0c5527e5
AM
18013+ AuDebugOn(!(mark->mask & AuHfsnMask));
18014+ mark->mask &= ~AuHfsnMask;
1facf9fc 18015+ }
0c5527e5 18016+ spin_unlock(&mark->lock);
4a4d8108 18017+ /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
1facf9fc 18018+}
18019+
4a4d8108 18020+/* ---------------------------------------------------------------------- */
1facf9fc 18021+
4a4d8108
AM
18022+/* #define AuDbgHnotify */
18023+#ifdef AuDbgHnotify
18024+static char *au_hfsn_name(u32 mask)
18025+{
18026+#ifdef CONFIG_AUFS_DEBUG
c06a8ce3
AM
18027+#define test_ret(flag) \
18028+ do { \
18029+ if (mask & flag) \
18030+ return #flag; \
18031+ } while (0)
4a4d8108
AM
18032+ test_ret(FS_ACCESS);
18033+ test_ret(FS_MODIFY);
18034+ test_ret(FS_ATTRIB);
18035+ test_ret(FS_CLOSE_WRITE);
18036+ test_ret(FS_CLOSE_NOWRITE);
18037+ test_ret(FS_OPEN);
18038+ test_ret(FS_MOVED_FROM);
18039+ test_ret(FS_MOVED_TO);
18040+ test_ret(FS_CREATE);
18041+ test_ret(FS_DELETE);
18042+ test_ret(FS_DELETE_SELF);
18043+ test_ret(FS_MOVE_SELF);
18044+ test_ret(FS_UNMOUNT);
18045+ test_ret(FS_Q_OVERFLOW);
18046+ test_ret(FS_IN_IGNORED);
b912730e 18047+ test_ret(FS_ISDIR);
4a4d8108
AM
18048+ test_ret(FS_IN_ONESHOT);
18049+ test_ret(FS_EVENT_ON_CHILD);
18050+ return "";
18051+#undef test_ret
18052+#else
18053+ return "??";
18054+#endif
1facf9fc 18055+}
4a4d8108 18056+#endif
1facf9fc 18057+
18058+/* ---------------------------------------------------------------------- */
18059+
1716fcea
AM
18060+static void au_hfsn_free_group(struct fsnotify_group *group)
18061+{
18062+ struct au_br_hfsnotify *hfsn = group->private;
18063+
5afbbe0d 18064+ /* AuDbg("here\n"); */
9f237c51 18065+ au_kfree_try_rcu(hfsn);
1716fcea
AM
18066+}
18067+
4a4d8108 18068+static int au_hfsn_handle_event(struct fsnotify_group *group,
a2654f78 18069+ u32 mask, const void *data, int data_type,
e37dd06a 18070+ struct inode *dir,
83b672a5 18071+ const struct qstr *file_name, u32 cookie,
ffa93bbd 18072+ struct fsnotify_iter_info *iter_info)
1facf9fc 18073+{
18074+ int err;
4a4d8108
AM
18075+ struct au_hnotify *hnotify;
18076+ struct inode *h_dir, *h_inode;
cd7a4cd9 18077+ struct fsnotify_mark *inode_mark;
4a4d8108 18078+
cd233405
AM
18079+ AuDebugOn(!(data_type == FSNOTIFY_EVENT_INODE
18080+ || data_type == FSNOTIFY_EVENT_DENTRY));
1facf9fc 18081+
18082+ err = 0;
0c5527e5 18083+ /* if FS_UNMOUNT happens, there must be another bug */
4a4d8108 18084+ AuDebugOn(mask & FS_UNMOUNT);
0c5527e5 18085+ if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
1facf9fc 18086+ goto out;
1facf9fc 18087+
e37dd06a 18088+ h_dir = dir;
fb47a38f 18089+ h_inode = NULL;
4a4d8108 18090+#ifdef AuDbgHnotify
392086de 18091+ au_debug_on();
0dcfbb52
AM
18092+ if (1 || file_name.len != sizeof(AUFS_XINO_FNAME) - 1
18093+ || strncmp(file_name.name, AUFS_XINO_FNAME, file_name.len)) {
4a4d8108
AM
18094+ AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
18095+ h_dir->i_ino, mask, au_hfsn_name(mask),
0dcfbb52 18096+ AuLNPair(file_name), h_inode ? h_inode->i_ino : 0);
4a4d8108 18097+ /* WARN_ON(1); */
1facf9fc 18098+ }
392086de 18099+ au_debug_off();
1facf9fc 18100+#endif
4a4d8108 18101+
cd7a4cd9 18102+ inode_mark = fsnotify_iter_inode_mark(iter_info);
0c5527e5
AM
18103+ AuDebugOn(!inode_mark);
18104+ hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
b62f1e65 18105+ err = au_hnotify(h_dir, hnotify, mask, file_name, h_inode);
1facf9fc 18106+
4a4d8108
AM
18107+out:
18108+ return err;
18109+}
1facf9fc 18110+
4a4d8108 18111+static struct fsnotify_ops au_hfsn_ops = {
1716fcea 18112+ .handle_event = au_hfsn_handle_event,
ffa93bbd
AM
18113+ .free_group_priv = au_hfsn_free_group,
18114+ .free_mark = au_hfsn_free_mark
4a4d8108
AM
18115+};
18116+
18117+/* ---------------------------------------------------------------------- */
18118+
027c5e7a
AM
18119+static void au_hfsn_fin_br(struct au_branch *br)
18120+{
1716fcea 18121+ struct au_br_hfsnotify *hfsn;
027c5e7a 18122+
1716fcea 18123+ hfsn = br->br_hfsn;
c1595e42
JR
18124+ if (hfsn) {
18125+ lockdep_off();
1716fcea 18126+ fsnotify_put_group(hfsn->hfsn_group);
c1595e42
JR
18127+ lockdep_on();
18128+ }
027c5e7a
AM
18129+}
18130+
1716fcea 18131+static int au_hfsn_init_br(struct au_branch *br, int perm)
4a4d8108
AM
18132+{
18133+ int err;
1716fcea
AM
18134+ struct fsnotify_group *group;
18135+ struct au_br_hfsnotify *hfsn;
1facf9fc 18136+
4a4d8108 18137+ err = 0;
1716fcea
AM
18138+ br->br_hfsn = NULL;
18139+ if (!au_br_hnotifyable(perm))
027c5e7a 18140+ goto out;
027c5e7a 18141+
1716fcea
AM
18142+ err = -ENOMEM;
18143+ hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
18144+ if (unlikely(!hfsn))
027c5e7a
AM
18145+ goto out;
18146+
1716fcea 18147+ err = 0;
22319442
JP
18148+ group = fsnotify_alloc_group(&au_hfsn_ops,
18149+ /*flags - not for userspace*/0);
1716fcea
AM
18150+ if (IS_ERR(group)) {
18151+ err = PTR_ERR(group);
0c5527e5 18152+ pr_err("fsnotify_alloc_group() failed, %d\n", err);
1716fcea 18153+ goto out_hfsn;
4a4d8108 18154+ }
1facf9fc 18155+
1716fcea
AM
18156+ group->private = hfsn;
18157+ hfsn->hfsn_group = group;
18158+ br->br_hfsn = hfsn;
18159+ goto out; /* success */
18160+
18161+out_hfsn:
9f237c51 18162+ au_kfree_try_rcu(hfsn);
027c5e7a 18163+out:
1716fcea
AM
18164+ return err;
18165+}
18166+
18167+static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
18168+{
18169+ int err;
18170+
18171+ err = 0;
18172+ if (!br->br_hfsn)
18173+ err = au_hfsn_init_br(br, perm);
18174+
1facf9fc 18175+ return err;
18176+}
18177+
7eafdf33
AM
18178+/* ---------------------------------------------------------------------- */
18179+
18180+static void au_hfsn_fin(void)
18181+{
18182+ AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
18183+ wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
18184+}
18185+
4a4d8108
AM
18186+const struct au_hnotify_op au_hnotify_op = {
18187+ .ctl = au_hfsn_ctl,
18188+ .alloc = au_hfsn_alloc,
18189+ .free = au_hfsn_free,
1facf9fc 18190+
7eafdf33
AM
18191+ .fin = au_hfsn_fin,
18192+
027c5e7a
AM
18193+ .reset_br = au_hfsn_reset_br,
18194+ .fin_br = au_hfsn_fin_br,
18195+ .init_br = au_hfsn_init_br
4a4d8108 18196+};
7f207e10 18197diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
eca34b5c 18198--- /usr/share/empty/fs/aufs/hfsplus.c 1970-01-01 01:00:00.000000000 +0100
594d0238 18199+++ linux/fs/aufs/hfsplus.c 2022-11-05 23:02:18.965889284 +0100
acd2b654 18200@@ -0,0 +1,60 @@
cd7a4cd9 18201+// SPDX-License-Identifier: GPL-2.0
4a4d8108 18202+/*
0dcfbb52 18203+ * Copyright (C) 2010-2022 Junjiro R. Okajima
4a4d8108 18204+ *
0dcfbb52 18205+ * This program is free software; you can redistribute it and/or modify
4a4d8108
AM
18206+ * it under the terms of the GNU General Public License as published by
18207+ * the Free Software Foundation; either version 2 of the License, or
18208+ * (at your option) any later version.
18209+ *
18210+ * This program is distributed in the hope that it will be useful,
18211+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18212+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18213+ * GNU General Public License for more details.
18214+ *
18215+ * You should have received a copy of the GNU General Public License
523b37e3 18216+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 18217+ */
1facf9fc 18218+
4a4d8108 18219+/*
acd2b654 18220+ * special support for filesystems which acquires an inode mutex
4a4d8108
AM
18221+ * at final closing a file, eg, hfsplus.
18222+ *
18223+ * This trick is very simple and stupid, just to open the file before really
acd2b654 18224+ * necessary open to tell hfsplus that this is not the final closing.
4a4d8108
AM
18225+ * The caller should call au_h_open_pre() after acquiring the inode mutex,
18226+ * and au_h_open_post() after releasing it.
18227+ */
1facf9fc 18228+
4a4d8108 18229+#include "aufs.h"
1facf9fc 18230+
392086de
AM
18231+struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
18232+ int force_wr)
4a4d8108
AM
18233+{
18234+ struct file *h_file;
18235+ struct dentry *h_dentry;
1facf9fc 18236+
4a4d8108
AM
18237+ h_dentry = au_h_dptr(dentry, bindex);
18238+ AuDebugOn(!h_dentry);
5527c038 18239+ AuDebugOn(d_is_negative(h_dentry));
4a4d8108
AM
18240+
18241+ h_file = NULL;
18242+ if (au_test_hfsplus(h_dentry->d_sb)
7e9cd9fe 18243+ && d_is_reg(h_dentry))
4a4d8108
AM
18244+ h_file = au_h_open(dentry, bindex,
18245+ O_RDONLY | O_NOATIME | O_LARGEFILE,
392086de 18246+ /*file*/NULL, force_wr);
4a4d8108 18247+ return h_file;
1facf9fc 18248+}
18249+
4a4d8108
AM
18250+void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
18251+ struct file *h_file)
18252+{
acd2b654
AM
18253+ struct au_branch *br;
18254+
4a4d8108
AM
18255+ if (h_file) {
18256+ fput(h_file);
acd2b654
AM
18257+ br = au_sbr(dentry->d_sb, bindex);
18258+ au_lcnt_dec(&br->br_nfiles);
4a4d8108
AM
18259+ }
18260+}
7f207e10 18261diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
eca34b5c 18262--- /usr/share/empty/fs/aufs/hnotify.c 1970-01-01 01:00:00.000000000 +0100
594d0238 18263+++ linux/fs/aufs/hnotify.c 2022-11-05 23:02:18.965889284 +0100
43982f53 18264@@ -0,0 +1,715 @@
cd7a4cd9 18265+// SPDX-License-Identifier: GPL-2.0
e49829fe 18266+/*
0dcfbb52 18267+ * Copyright (C) 2005-2022 Junjiro R. Okajima
e49829fe 18268+ *
0dcfbb52 18269+ * This program is free software; you can redistribute it and/or modify
e49829fe
JR
18270+ * it under the terms of the GNU General Public License as published by
18271+ * the Free Software Foundation; either version 2 of the License, or
18272+ * (at your option) any later version.
18273+ *
18274+ * This program is distributed in the hope that it will be useful,
18275+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18276+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18277+ * GNU General Public License for more details.
18278+ *
18279+ * You should have received a copy of the GNU General Public License
523b37e3 18280+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
e49829fe
JR
18281+ */
18282+
18283+/*
7f207e10 18284+ * abstraction to notify the direct changes on lower directories
e49829fe
JR
18285+ */
18286+
eca801bf 18287+/* #include <linux/iversion.h> */
e49829fe
JR
18288+#include "aufs.h"
18289+
027c5e7a 18290+int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
e49829fe
JR
18291+{
18292+ int err;
7f207e10 18293+ struct au_hnotify *hn;
1facf9fc 18294+
4a4d8108
AM
18295+ err = -ENOMEM;
18296+ hn = au_cache_alloc_hnotify();
18297+ if (hn) {
18298+ hn->hn_aufs_inode = inode;
027c5e7a
AM
18299+ hinode->hi_notify = hn;
18300+ err = au_hnotify_op.alloc(hinode);
18301+ AuTraceErr(err);
18302+ if (unlikely(err)) {
18303+ hinode->hi_notify = NULL;
1c60b727 18304+ au_cache_free_hnotify(hn);
4a4d8108
AM
18305+ /*
18306+ * The upper dir was removed by udba, but the same named
acd2b654 18307+ * dir left. In this case, aufs assigns a new inode
4a4d8108 18308+ * number and set the monitor again.
acd2b654 18309+ * For the lower dir, the old monitor is still left.
4a4d8108
AM
18310+ */
18311+ if (err == -EEXIST)
18312+ err = 0;
18313+ }
1308ab2a 18314+ }
1308ab2a 18315+
027c5e7a 18316+ AuTraceErr(err);
1308ab2a 18317+ return err;
dece6358 18318+}
1facf9fc 18319+
4a4d8108 18320+void au_hn_free(struct au_hinode *hinode)
dece6358 18321+{
4a4d8108 18322+ struct au_hnotify *hn;
1facf9fc 18323+
4a4d8108
AM
18324+ hn = hinode->hi_notify;
18325+ if (hn) {
4a4d8108 18326+ hinode->hi_notify = NULL;
7eafdf33 18327+ if (au_hnotify_op.free(hinode, hn))
1c60b727 18328+ au_cache_free_hnotify(hn);
4a4d8108
AM
18329+ }
18330+}
dece6358 18331+
4a4d8108 18332+/* ---------------------------------------------------------------------- */
dece6358 18333+
4a4d8108
AM
18334+void au_hn_ctl(struct au_hinode *hinode, int do_set)
18335+{
18336+ if (hinode->hi_notify)
18337+ au_hnotify_op.ctl(hinode, do_set);
18338+}
18339+
18340+void au_hn_reset(struct inode *inode, unsigned int flags)
18341+{
5afbbe0d 18342+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
18343+ struct inode *hi;
18344+ struct dentry *iwhdentry;
1facf9fc 18345+
5afbbe0d
AM
18346+ bbot = au_ibbot(inode);
18347+ for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
4a4d8108
AM
18348+ hi = au_h_iptr(inode, bindex);
18349+ if (!hi)
18350+ continue;
1308ab2a 18351+
febd17d6 18352+ /* inode_lock_nested(hi, AuLsc_I_CHILD); */
4a4d8108
AM
18353+ iwhdentry = au_hi_wh(inode, bindex);
18354+ if (iwhdentry)
18355+ dget(iwhdentry);
18356+ au_igrab(hi);
18357+ au_set_h_iptr(inode, bindex, NULL, 0);
18358+ au_set_h_iptr(inode, bindex, au_igrab(hi),
18359+ flags & ~AuHi_XINO);
18360+ iput(hi);
18361+ dput(iwhdentry);
febd17d6 18362+ /* inode_unlock(hi); */
1facf9fc 18363+ }
1facf9fc 18364+}
18365+
1308ab2a 18366+/* ---------------------------------------------------------------------- */
1facf9fc 18367+
4a4d8108 18368+static int hn_xino(struct inode *inode, struct inode *h_inode)
1facf9fc 18369+{
4a4d8108 18370+ int err;
5afbbe0d 18371+ aufs_bindex_t bindex, bbot, bfound, btop;
4a4d8108 18372+ struct inode *h_i;
1facf9fc 18373+
4a4d8108
AM
18374+ err = 0;
18375+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 18376+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
18377+ goto out;
18378+ }
1facf9fc 18379+
4a4d8108 18380+ bfound = -1;
5afbbe0d
AM
18381+ bbot = au_ibbot(inode);
18382+ btop = au_ibtop(inode);
4a4d8108 18383+#if 0 /* reserved for future use */
5afbbe0d 18384+ if (bindex == bbot) {
4a4d8108
AM
18385+ /* keep this ino in rename case */
18386+ goto out;
18387+ }
18388+#endif
5afbbe0d 18389+ for (bindex = btop; bindex <= bbot; bindex++)
4a4d8108
AM
18390+ if (au_h_iptr(inode, bindex) == h_inode) {
18391+ bfound = bindex;
18392+ break;
18393+ }
18394+ if (bfound < 0)
1308ab2a 18395+ goto out;
1facf9fc 18396+
5afbbe0d 18397+ for (bindex = btop; bindex <= bbot; bindex++) {
4a4d8108
AM
18398+ h_i = au_h_iptr(inode, bindex);
18399+ if (!h_i)
18400+ continue;
1facf9fc 18401+
4a4d8108
AM
18402+ err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
18403+ /* ignore this error */
18404+ /* bad action? */
1facf9fc 18405+ }
1facf9fc 18406+
4a4d8108 18407+ /* children inode number will be broken */
1facf9fc 18408+
4f0767ce 18409+out:
4a4d8108
AM
18410+ AuTraceErr(err);
18411+ return err;
1facf9fc 18412+}
18413+
4a4d8108 18414+static int hn_gen_tree(struct dentry *dentry)
1facf9fc 18415+{
4a4d8108
AM
18416+ int err, i, j, ndentry;
18417+ struct au_dcsub_pages dpages;
18418+ struct au_dpage *dpage;
18419+ struct dentry **dentries;
1facf9fc 18420+
4a4d8108
AM
18421+ err = au_dpages_init(&dpages, GFP_NOFS);
18422+ if (unlikely(err))
18423+ goto out;
18424+ err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
18425+ if (unlikely(err))
18426+ goto out_dpages;
1facf9fc 18427+
4a4d8108
AM
18428+ for (i = 0; i < dpages.ndpage; i++) {
18429+ dpage = dpages.dpages + i;
18430+ dentries = dpage->dentries;
18431+ ndentry = dpage->ndentry;
18432+ for (j = 0; j < ndentry; j++) {
18433+ struct dentry *d;
18434+
18435+ d = dentries[j];
18436+ if (IS_ROOT(d))
18437+ continue;
18438+
4a4d8108 18439+ au_digen_dec(d);
5527c038 18440+ if (d_really_is_positive(d))
4a4d8108
AM
18441+ /* todo: reset children xino?
18442+ cached children only? */
5527c038 18443+ au_iigen_dec(d_inode(d));
1308ab2a 18444+ }
dece6358 18445+ }
1facf9fc 18446+
4f0767ce 18447+out_dpages:
4a4d8108 18448+ au_dpages_free(&dpages);
4f0767ce 18449+out:
dece6358
AM
18450+ return err;
18451+}
18452+
1308ab2a 18453+/*
4a4d8108 18454+ * return 0 if processed.
1308ab2a 18455+ */
4a4d8108
AM
18456+static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
18457+ const unsigned int isdir)
dece6358 18458+{
1308ab2a 18459+ int err;
4a4d8108
AM
18460+ struct dentry *d;
18461+ struct qstr *dname;
1facf9fc 18462+
4a4d8108
AM
18463+ err = 1;
18464+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 18465+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
18466+ err = 0;
18467+ goto out;
18468+ }
dece6358 18469+
4a4d8108
AM
18470+ if (!isdir) {
18471+ AuDebugOn(!name);
18472+ au_iigen_dec(inode);
027c5e7a 18473+ spin_lock(&inode->i_lock);
c1595e42 18474+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
027c5e7a 18475+ spin_lock(&d->d_lock);
4a4d8108
AM
18476+ dname = &d->d_name;
18477+ if (dname->len != nlen
027c5e7a
AM
18478+ && memcmp(dname->name, name, nlen)) {
18479+ spin_unlock(&d->d_lock);
4a4d8108 18480+ continue;
027c5e7a 18481+ }
4a4d8108 18482+ err = 0;
4a4d8108
AM
18483+ au_digen_dec(d);
18484+ spin_unlock(&d->d_lock);
18485+ break;
1facf9fc 18486+ }
027c5e7a 18487+ spin_unlock(&inode->i_lock);
1308ab2a 18488+ } else {
027c5e7a 18489+ au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
c1595e42 18490+ d = d_find_any_alias(inode);
4a4d8108
AM
18491+ if (!d) {
18492+ au_iigen_dec(inode);
18493+ goto out;
18494+ }
1facf9fc 18495+
027c5e7a 18496+ spin_lock(&d->d_lock);
4a4d8108 18497+ dname = &d->d_name;
027c5e7a
AM
18498+ if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
18499+ spin_unlock(&d->d_lock);
4a4d8108 18500+ err = hn_gen_tree(d);
027c5e7a
AM
18501+ spin_lock(&d->d_lock);
18502+ }
18503+ spin_unlock(&d->d_lock);
4a4d8108
AM
18504+ dput(d);
18505+ }
1facf9fc 18506+
4f0767ce 18507+out:
4a4d8108 18508+ AuTraceErr(err);
1308ab2a 18509+ return err;
18510+}
dece6358 18511+
4a4d8108 18512+static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
1facf9fc 18513+{
4a4d8108 18514+ int err;
1facf9fc 18515+
5527c038 18516+ if (IS_ROOT(dentry)) {
0c3ec466 18517+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
18518+ return 0;
18519+ }
1308ab2a 18520+
4a4d8108
AM
18521+ err = 0;
18522+ if (!isdir) {
4a4d8108 18523+ au_digen_dec(dentry);
5527c038
JR
18524+ if (d_really_is_positive(dentry))
18525+ au_iigen_dec(d_inode(dentry));
4a4d8108 18526+ } else {
027c5e7a 18527+ au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
5527c038 18528+ if (d_really_is_positive(dentry))
4a4d8108
AM
18529+ err = hn_gen_tree(dentry);
18530+ }
18531+
18532+ AuTraceErr(err);
18533+ return err;
1facf9fc 18534+}
18535+
4a4d8108 18536+/* ---------------------------------------------------------------------- */
1facf9fc 18537+
4a4d8108
AM
18538+/* hnotify job flags */
18539+#define AuHnJob_XINO0 1
18540+#define AuHnJob_GEN (1 << 1)
18541+#define AuHnJob_DIRENT (1 << 2)
18542+#define AuHnJob_ISDIR (1 << 3)
18543+#define AuHnJob_TRYXINO0 (1 << 4)
18544+#define AuHnJob_MNTPNT (1 << 5)
18545+#define au_ftest_hnjob(flags, name) ((flags) & AuHnJob_##name)
7f207e10
AM
18546+#define au_fset_hnjob(flags, name) \
18547+ do { (flags) |= AuHnJob_##name; } while (0)
18548+#define au_fclr_hnjob(flags, name) \
18549+ do { (flags) &= ~AuHnJob_##name; } while (0)
1facf9fc 18550+
4a4d8108
AM
18551+enum {
18552+ AuHn_CHILD,
18553+ AuHn_PARENT,
18554+ AuHnLast
18555+};
1facf9fc 18556+
4a4d8108
AM
18557+struct au_hnotify_args {
18558+ struct inode *h_dir, *dir, *h_child_inode;
18559+ u32 mask;
18560+ unsigned int flags[AuHnLast];
18561+ unsigned int h_child_nlen;
18562+ char h_child_name[];
18563+};
1facf9fc 18564+
4a4d8108
AM
18565+struct hn_job_args {
18566+ unsigned int flags;
18567+ struct inode *inode, *h_inode, *dir, *h_dir;
18568+ struct dentry *dentry;
18569+ char *h_name;
18570+ int h_nlen;
18571+};
1308ab2a 18572+
4a4d8108
AM
18573+static int hn_job(struct hn_job_args *a)
18574+{
18575+ const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
076b876e 18576+ int e;
1308ab2a 18577+
4a4d8108
AM
18578+ /* reset xino */
18579+ if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
18580+ hn_xino(a->inode, a->h_inode); /* ignore this error */
1308ab2a 18581+
4a4d8108
AM
18582+ if (au_ftest_hnjob(a->flags, TRYXINO0)
18583+ && a->inode
18584+ && a->h_inode) {
be118d29 18585+ inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
38d290e6
JR
18586+ if (!a->h_inode->i_nlink
18587+ && !(a->h_inode->i_state & I_LINKABLE))
4a4d8108 18588+ hn_xino(a->inode, a->h_inode); /* ignore this error */
3c1bdaff 18589+ inode_unlock_shared(a->h_inode);
1308ab2a 18590+ }
1facf9fc 18591+
4a4d8108
AM
18592+ /* make the generation obsolete */
18593+ if (au_ftest_hnjob(a->flags, GEN)) {
076b876e 18594+ e = -1;
4a4d8108 18595+ if (a->inode)
076b876e 18596+ e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
4a4d8108 18597+ isdir);
076b876e 18598+ if (e && a->dentry)
4a4d8108
AM
18599+ hn_gen_by_name(a->dentry, isdir);
18600+ /* ignore this error */
1facf9fc 18601+ }
1facf9fc 18602+
4a4d8108
AM
18603+ /* make dir entries obsolete */
18604+ if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
18605+ struct au_vdir *vdir;
1facf9fc 18606+
4a4d8108
AM
18607+ vdir = au_ivdir(a->inode);
18608+ if (vdir)
18609+ vdir->vd_jiffy = 0;
18610+ /* IMustLock(a->inode); */
be118d29 18611+ /* inode_inc_iversion(a->inode); */
4a4d8108 18612+ }
1facf9fc 18613+
4a4d8108
AM
18614+ /* can do nothing but warn */
18615+ if (au_ftest_hnjob(a->flags, MNTPNT)
18616+ && a->dentry
18617+ && d_mountpoint(a->dentry))
523b37e3 18618+ pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
1facf9fc 18619+
4a4d8108 18620+ return 0;
1308ab2a 18621+}
1facf9fc 18622+
1308ab2a 18623+/* ---------------------------------------------------------------------- */
1facf9fc 18624+
4a4d8108
AM
18625+static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
18626+ struct inode *dir)
1308ab2a 18627+{
4a4d8108
AM
18628+ struct dentry *dentry, *d, *parent;
18629+ struct qstr *dname;
1308ab2a 18630+
c1595e42 18631+ parent = d_find_any_alias(dir);
4a4d8108
AM
18632+ if (!parent)
18633+ return NULL;
1308ab2a 18634+
4a4d8108 18635+ dentry = NULL;
027c5e7a 18636+ spin_lock(&parent->d_lock);
c1595e42 18637+ list_for_each_entry(d, &parent->d_subdirs, d_child) {
523b37e3 18638+ /* AuDbg("%pd\n", d); */
027c5e7a 18639+ spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
4a4d8108
AM
18640+ dname = &d->d_name;
18641+ if (dname->len != nlen || memcmp(dname->name, name, nlen))
027c5e7a
AM
18642+ goto cont_unlock;
18643+ if (au_di(d))
18644+ au_digen_dec(d);
18645+ else
18646+ goto cont_unlock;
c1595e42 18647+ if (au_dcount(d) > 0) {
027c5e7a 18648+ dentry = dget_dlock(d);
4a4d8108 18649+ spin_unlock(&d->d_lock);
027c5e7a 18650+ break;
dece6358 18651+ }
1facf9fc 18652+
f6b6e03d 18653+cont_unlock:
027c5e7a 18654+ spin_unlock(&d->d_lock);
1308ab2a 18655+ }
027c5e7a 18656+ spin_unlock(&parent->d_lock);
4a4d8108 18657+ dput(parent);
1facf9fc 18658+
4a4d8108
AM
18659+ if (dentry)
18660+ di_write_lock_child(dentry);
1308ab2a 18661+
4a4d8108
AM
18662+ return dentry;
18663+}
dece6358 18664+
4a4d8108
AM
18665+static struct inode *lookup_wlock_by_ino(struct super_block *sb,
18666+ aufs_bindex_t bindex, ino_t h_ino)
18667+{
18668+ struct inode *inode;
18669+ ino_t ino;
18670+ int err;
18671+
18672+ inode = NULL;
18673+ err = au_xino_read(sb, bindex, h_ino, &ino);
18674+ if (!err && ino)
18675+ inode = ilookup(sb, ino);
18676+ if (!inode)
18677+ goto out;
18678+
18679+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 18680+ pr_warn("wrong root branch\n");
4a4d8108
AM
18681+ iput(inode);
18682+ inode = NULL;
18683+ goto out;
1308ab2a 18684+ }
18685+
4a4d8108 18686+ ii_write_lock_child(inode);
1308ab2a 18687+
4f0767ce 18688+out:
4a4d8108 18689+ return inode;
dece6358
AM
18690+}
18691+
4a4d8108 18692+static void au_hn_bh(void *_args)
1facf9fc 18693+{
4a4d8108
AM
18694+ struct au_hnotify_args *a = _args;
18695+ struct super_block *sb;
5afbbe0d 18696+ aufs_bindex_t bindex, bbot, bfound;
4a4d8108 18697+ unsigned char xino, try_iput;
1facf9fc 18698+ int err;
1308ab2a 18699+ struct inode *inode;
4a4d8108
AM
18700+ ino_t h_ino;
18701+ struct hn_job_args args;
18702+ struct dentry *dentry;
18703+ struct au_sbinfo *sbinfo;
1facf9fc 18704+
4a4d8108
AM
18705+ AuDebugOn(!_args);
18706+ AuDebugOn(!a->h_dir);
18707+ AuDebugOn(!a->dir);
18708+ AuDebugOn(!a->mask);
18709+ AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
18710+ a->mask, a->dir->i_ino, a->h_dir->i_ino,
18711+ a->h_child_inode ? a->h_child_inode->i_ino : 0);
1facf9fc 18712+
4a4d8108
AM
18713+ inode = NULL;
18714+ dentry = NULL;
18715+ /*
18716+ * do not lock a->dir->i_mutex here
18717+ * because of d_revalidate() may cause a deadlock.
18718+ */
18719+ sb = a->dir->i_sb;
18720+ AuDebugOn(!sb);
18721+ sbinfo = au_sbi(sb);
18722+ AuDebugOn(!sbinfo);
7f207e10 18723+ si_write_lock(sb, AuLock_NOPLMW);
1facf9fc 18724+
8b6a4947
AM
18725+ if (au_opt_test(sbinfo->si_mntflags, DIRREN))
18726+ switch (a->mask & FS_EVENTS_POSS_ON_CHILD) {
18727+ case FS_MOVED_FROM:
18728+ case FS_MOVED_TO:
18729+ AuWarn1("DIRREN with UDBA may not work correctly "
18730+ "for the direct rename(2)\n");
18731+ }
18732+
4a4d8108
AM
18733+ ii_read_lock_parent(a->dir);
18734+ bfound = -1;
5afbbe0d
AM
18735+ bbot = au_ibbot(a->dir);
18736+ for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
4a4d8108
AM
18737+ if (au_h_iptr(a->dir, bindex) == a->h_dir) {
18738+ bfound = bindex;
18739+ break;
18740+ }
18741+ ii_read_unlock(a->dir);
18742+ if (unlikely(bfound < 0))
18743+ goto out;
1facf9fc 18744+
4a4d8108
AM
18745+ xino = !!au_opt_test(au_mntflags(sb), XINO);
18746+ h_ino = 0;
18747+ if (a->h_child_inode)
18748+ h_ino = a->h_child_inode->i_ino;
1facf9fc 18749+
4a4d8108
AM
18750+ if (a->h_child_nlen
18751+ && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
18752+ || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
18753+ dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
18754+ a->dir);
18755+ try_iput = 0;
5527c038
JR
18756+ if (dentry && d_really_is_positive(dentry))
18757+ inode = d_inode(dentry);
4a4d8108
AM
18758+ if (xino && !inode && h_ino
18759+ && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
18760+ || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
18761+ || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
18762+ inode = lookup_wlock_by_ino(sb, bfound, h_ino);
18763+ try_iput = 1;
f0c0a007 18764+ }
1facf9fc 18765+
4a4d8108
AM
18766+ args.flags = a->flags[AuHn_CHILD];
18767+ args.dentry = dentry;
18768+ args.inode = inode;
18769+ args.h_inode = a->h_child_inode;
18770+ args.dir = a->dir;
18771+ args.h_dir = a->h_dir;
18772+ args.h_name = a->h_child_name;
18773+ args.h_nlen = a->h_child_nlen;
18774+ err = hn_job(&args);
18775+ if (dentry) {
027c5e7a 18776+ if (au_di(dentry))
4a4d8108
AM
18777+ di_write_unlock(dentry);
18778+ dput(dentry);
18779+ }
18780+ if (inode && try_iput) {
18781+ ii_write_unlock(inode);
18782+ iput(inode);
18783+ }
1facf9fc 18784+
4a4d8108
AM
18785+ ii_write_lock_parent(a->dir);
18786+ args.flags = a->flags[AuHn_PARENT];
18787+ args.dentry = NULL;
18788+ args.inode = a->dir;
18789+ args.h_inode = a->h_dir;
18790+ args.dir = NULL;
18791+ args.h_dir = NULL;
18792+ args.h_name = NULL;
18793+ args.h_nlen = 0;
18794+ err = hn_job(&args);
18795+ ii_write_unlock(a->dir);
1facf9fc 18796+
4f0767ce 18797+out:
4a4d8108
AM
18798+ iput(a->h_child_inode);
18799+ iput(a->h_dir);
18800+ iput(a->dir);
027c5e7a
AM
18801+ si_write_unlock(sb);
18802+ au_nwt_done(&sbinfo->si_nowait);
9f237c51 18803+ au_kfree_rcu(a);
dece6358 18804+}
1facf9fc 18805+
4a4d8108
AM
18806+/* ---------------------------------------------------------------------- */
18807+
18808+int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
b62f1e65 18809+ const struct qstr *h_child_qstr, struct inode *h_child_inode)
dece6358 18810+{
4a4d8108 18811+ int err, len;
53392da6 18812+ unsigned int flags[AuHnLast], f;
4a4d8108
AM
18813+ unsigned char isdir, isroot, wh;
18814+ struct inode *dir;
18815+ struct au_hnotify_args *args;
18816+ char *p, *h_child_name;
dece6358 18817+
1308ab2a 18818+ err = 0;
4a4d8108
AM
18819+ AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
18820+ dir = igrab(hnotify->hn_aufs_inode);
18821+ if (!dir)
18822+ goto out;
1facf9fc 18823+
4a4d8108
AM
18824+ isroot = (dir->i_ino == AUFS_ROOT_INO);
18825+ wh = 0;
18826+ h_child_name = (void *)h_child_qstr->name;
18827+ len = h_child_qstr->len;
18828+ if (h_child_name) {
18829+ if (len > AUFS_WH_PFX_LEN
18830+ && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
18831+ h_child_name += AUFS_WH_PFX_LEN;
18832+ len -= AUFS_WH_PFX_LEN;
18833+ wh = 1;
18834+ }
1facf9fc 18835+ }
dece6358 18836+
4a4d8108
AM
18837+ isdir = 0;
18838+ if (h_child_inode)
18839+ isdir = !!S_ISDIR(h_child_inode->i_mode);
18840+ flags[AuHn_PARENT] = AuHnJob_ISDIR;
18841+ flags[AuHn_CHILD] = 0;
18842+ if (isdir)
18843+ flags[AuHn_CHILD] = AuHnJob_ISDIR;
18844+ au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
18845+ au_fset_hnjob(flags[AuHn_CHILD], GEN);
fbc438ed 18846+ switch (mask & ALL_FSNOTIFY_DIRENT_EVENTS) {
4a4d8108
AM
18847+ case FS_MOVED_FROM:
18848+ case FS_MOVED_TO:
18849+ au_fset_hnjob(flags[AuHn_CHILD], XINO0);
18850+ au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
42a736d3 18851+ fallthrough;
4a4d8108 18852+ case FS_CREATE:
fb47a38f 18853+ AuDebugOn(!h_child_name);
4a4d8108 18854+ break;
1facf9fc 18855+
4a4d8108
AM
18856+ case FS_DELETE:
18857+ /*
18858+ * aufs never be able to get this child inode.
18859+ * revalidation should be in d_revalidate()
18860+ * by checking i_nlink, i_generation or d_unhashed().
18861+ */
18862+ AuDebugOn(!h_child_name);
18863+ au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
18864+ au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
18865+ break;
dece6358 18866+
4a4d8108
AM
18867+ default:
18868+ AuDebugOn(1);
18869+ }
1308ab2a 18870+
4a4d8108
AM
18871+ if (wh)
18872+ h_child_inode = NULL;
1308ab2a 18873+
4a4d8108
AM
18874+ err = -ENOMEM;
18875+ /* iput() and kfree() will be called in au_hnotify() */
4a4d8108 18876+ args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
4a4d8108
AM
18877+ if (unlikely(!args)) {
18878+ AuErr1("no memory\n");
18879+ iput(dir);
18880+ goto out;
18881+ }
18882+ args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
18883+ args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
18884+ args->mask = mask;
18885+ args->dir = dir;
18886+ args->h_dir = igrab(h_dir);
18887+ if (h_child_inode)
18888+ h_child_inode = igrab(h_child_inode); /* can be NULL */
18889+ args->h_child_inode = h_child_inode;
18890+ args->h_child_nlen = len;
18891+ if (len) {
18892+ p = (void *)args;
18893+ p += sizeof(*args);
18894+ memcpy(p, h_child_name, len);
18895+ p[len] = 0;
1308ab2a 18896+ }
1308ab2a 18897+
38d290e6 18898+ /* NFS fires the event for silly-renamed one from kworker */
53392da6 18899+ f = 0;
38d290e6
JR
18900+ if (!dir->i_nlink
18901+ || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
53392da6
AM
18902+ f = AuWkq_NEST;
18903+ err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
4a4d8108
AM
18904+ if (unlikely(err)) {
18905+ pr_err("wkq %d\n", err);
18906+ iput(args->h_child_inode);
18907+ iput(args->h_dir);
18908+ iput(args->dir);
9f237c51 18909+ au_kfree_rcu(args);
1facf9fc 18910+ }
1facf9fc 18911+
4a4d8108 18912+out:
1facf9fc 18913+ return err;
18914+}
18915+
027c5e7a
AM
18916+/* ---------------------------------------------------------------------- */
18917+
18918+int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
18919+{
18920+ int err;
18921+
18922+ AuDebugOn(!(udba & AuOptMask_UDBA));
18923+
18924+ err = 0;
18925+ if (au_hnotify_op.reset_br)
18926+ err = au_hnotify_op.reset_br(udba, br, perm);
18927+
18928+ return err;
18929+}
18930+
18931+int au_hnotify_init_br(struct au_branch *br, int perm)
18932+{
18933+ int err;
18934+
18935+ err = 0;
18936+ if (au_hnotify_op.init_br)
18937+ err = au_hnotify_op.init_br(br, perm);
18938+
18939+ return err;
18940+}
18941+
18942+void au_hnotify_fin_br(struct au_branch *br)
18943+{
18944+ if (au_hnotify_op.fin_br)
18945+ au_hnotify_op.fin_br(br);
18946+}
18947+
4a4d8108
AM
18948+static void au_hn_destroy_cache(void)
18949+{
1c60b727
AM
18950+ kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
18951+ au_cache[AuCache_HNOTIFY] = NULL;
4a4d8108 18952+}
1308ab2a 18953+
4a4d8108 18954+int __init au_hnotify_init(void)
1facf9fc 18955+{
1308ab2a 18956+ int err;
1308ab2a 18957+
4a4d8108 18958+ err = -ENOMEM;
1c60b727
AM
18959+ au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
18960+ if (au_cache[AuCache_HNOTIFY]) {
027c5e7a
AM
18961+ err = 0;
18962+ if (au_hnotify_op.init)
18963+ err = au_hnotify_op.init();
4a4d8108
AM
18964+ if (unlikely(err))
18965+ au_hn_destroy_cache();
1308ab2a 18966+ }
1308ab2a 18967+ AuTraceErr(err);
4a4d8108 18968+ return err;
1308ab2a 18969+}
18970+
4a4d8108 18971+void au_hnotify_fin(void)
1308ab2a 18972+{
027c5e7a
AM
18973+ if (au_hnotify_op.fin)
18974+ au_hnotify_op.fin();
f0c0a007 18975+
4a4d8108 18976+ /* cf. au_cache_fin() */
1c60b727 18977+ if (au_cache[AuCache_HNOTIFY])
4a4d8108 18978+ au_hn_destroy_cache();
dece6358 18979+}
7f207e10 18980diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
eca34b5c 18981--- /usr/share/empty/fs/aufs/iinfo.c 1970-01-01 01:00:00.000000000 +0100
594d0238 18982+++ linux/fs/aufs/iinfo.c 2022-11-05 23:02:18.965889284 +0100
062440b3 18983@@ -0,0 +1,286 @@
cd7a4cd9 18984+// SPDX-License-Identifier: GPL-2.0
dece6358 18985+/*
0dcfbb52 18986+ * Copyright (C) 2005-2022 Junjiro R. Okajima
dece6358 18987+ *
0dcfbb52 18988+ * This program is free software; you can redistribute it and/or modify
dece6358
AM
18989+ * it under the terms of the GNU General Public License as published by
18990+ * the Free Software Foundation; either version 2 of the License, or
18991+ * (at your option) any later version.
18992+ *
18993+ * This program is distributed in the hope that it will be useful,
18994+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18995+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18996+ * GNU General Public License for more details.
18997+ *
18998+ * You should have received a copy of the GNU General Public License
523b37e3 18999+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358 19000+ */
1facf9fc 19001+
dece6358 19002+/*
4a4d8108 19003+ * inode private data
dece6358 19004+ */
1facf9fc 19005+
1308ab2a 19006+#include "aufs.h"
1facf9fc 19007+
4a4d8108 19008+struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 19009+{
4a4d8108 19010+ struct inode *h_inode;
5afbbe0d 19011+ struct au_hinode *hinode;
1facf9fc 19012+
4a4d8108 19013+ IiMustAnyLock(inode);
1facf9fc 19014+
5afbbe0d
AM
19015+ hinode = au_hinode(au_ii(inode), bindex);
19016+ h_inode = hinode->hi_inode;
4a4d8108
AM
19017+ AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
19018+ return h_inode;
19019+}
1facf9fc 19020+
4a4d8108
AM
19021+/* todo: hard/soft set? */
19022+void au_hiput(struct au_hinode *hinode)
19023+{
19024+ au_hn_free(hinode);
19025+ dput(hinode->hi_whdentry);
19026+ iput(hinode->hi_inode);
19027+}
1facf9fc 19028+
4a4d8108
AM
19029+unsigned int au_hi_flags(struct inode *inode, int isdir)
19030+{
19031+ unsigned int flags;
19032+ const unsigned int mnt_flags = au_mntflags(inode->i_sb);
1facf9fc 19033+
4a4d8108
AM
19034+ flags = 0;
19035+ if (au_opt_test(mnt_flags, XINO))
19036+ au_fset_hi(flags, XINO);
19037+ if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
19038+ au_fset_hi(flags, HNOTIFY);
19039+ return flags;
1facf9fc 19040+}
19041+
4a4d8108
AM
19042+void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
19043+ struct inode *h_inode, unsigned int flags)
1308ab2a 19044+{
4a4d8108
AM
19045+ struct au_hinode *hinode;
19046+ struct inode *hi;
19047+ struct au_iinfo *iinfo = au_ii(inode);
1facf9fc 19048+
4a4d8108 19049+ IiMustWriteLock(inode);
dece6358 19050+
5afbbe0d 19051+ hinode = au_hinode(iinfo, bindex);
4a4d8108
AM
19052+ hi = hinode->hi_inode;
19053+ AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
19054+
19055+ if (hi)
19056+ au_hiput(hinode);
19057+ hinode->hi_inode = h_inode;
19058+ if (h_inode) {
19059+ int err;
19060+ struct super_block *sb = inode->i_sb;
19061+ struct au_branch *br;
19062+
027c5e7a
AM
19063+ AuDebugOn(inode->i_mode
19064+ && (h_inode->i_mode & S_IFMT)
19065+ != (inode->i_mode & S_IFMT));
5afbbe0d 19066+ if (bindex == iinfo->ii_btop)
4a4d8108
AM
19067+ au_cpup_igen(inode, h_inode);
19068+ br = au_sbr(sb, bindex);
19069+ hinode->hi_id = br->br_id;
19070+ if (au_ftest_hi(flags, XINO)) {
19071+ err = au_xino_write(sb, bindex, h_inode->i_ino,
19072+ inode->i_ino);
19073+ if (unlikely(err))
19074+ AuIOErr1("failed au_xino_write() %d\n", err);
19075+ }
19076+
19077+ if (au_ftest_hi(flags, HNOTIFY)
19078+ && au_br_hnotifyable(br->br_perm)) {
027c5e7a 19079+ err = au_hn_alloc(hinode, inode);
4a4d8108
AM
19080+ if (unlikely(err))
19081+ AuIOErr1("au_hn_alloc() %d\n", err);
1308ab2a 19082+ }
19083+ }
4a4d8108 19084+}
dece6358 19085+
4a4d8108
AM
19086+void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
19087+ struct dentry *h_wh)
19088+{
19089+ struct au_hinode *hinode;
dece6358 19090+
4a4d8108
AM
19091+ IiMustWriteLock(inode);
19092+
5afbbe0d 19093+ hinode = au_hinode(au_ii(inode), bindex);
4a4d8108
AM
19094+ AuDebugOn(hinode->hi_whdentry);
19095+ hinode->hi_whdentry = h_wh;
1facf9fc 19096+}
19097+
537831f9 19098+void au_update_iigen(struct inode *inode, int half)
1308ab2a 19099+{
537831f9
AM
19100+ struct au_iinfo *iinfo;
19101+ struct au_iigen *iigen;
19102+ unsigned int sigen;
19103+
19104+ sigen = au_sigen(inode->i_sb);
19105+ iinfo = au_ii(inode);
19106+ iigen = &iinfo->ii_generation;
be52b249 19107+ spin_lock(&iigen->ig_spin);
537831f9
AM
19108+ iigen->ig_generation = sigen;
19109+ if (half)
19110+ au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
19111+ else
19112+ au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
be52b249 19113+ spin_unlock(&iigen->ig_spin);
4a4d8108 19114+}
1facf9fc 19115+
4a4d8108
AM
19116+/* it may be called at remount time, too */
19117+void au_update_ibrange(struct inode *inode, int do_put_zero)
19118+{
19119+ struct au_iinfo *iinfo;
5afbbe0d 19120+ aufs_bindex_t bindex, bbot;
1facf9fc 19121+
5afbbe0d 19122+ AuDebugOn(au_is_bad_inode(inode));
4a4d8108 19123+ IiMustWriteLock(inode);
1facf9fc 19124+
5afbbe0d
AM
19125+ iinfo = au_ii(inode);
19126+ if (do_put_zero && iinfo->ii_btop >= 0) {
19127+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
4a4d8108
AM
19128+ bindex++) {
19129+ struct inode *h_i;
1facf9fc 19130+
5afbbe0d 19131+ h_i = au_hinode(iinfo, bindex)->hi_inode;
38d290e6
JR
19132+ if (h_i
19133+ && !h_i->i_nlink
19134+ && !(h_i->i_state & I_LINKABLE))
027c5e7a
AM
19135+ au_set_h_iptr(inode, bindex, NULL, 0);
19136+ }
4a4d8108
AM
19137+ }
19138+
5afbbe0d
AM
19139+ iinfo->ii_btop = -1;
19140+ iinfo->ii_bbot = -1;
19141+ bbot = au_sbbot(inode->i_sb);
19142+ for (bindex = 0; bindex <= bbot; bindex++)
19143+ if (au_hinode(iinfo, bindex)->hi_inode) {
19144+ iinfo->ii_btop = bindex;
4a4d8108 19145+ break;
027c5e7a 19146+ }
5afbbe0d
AM
19147+ if (iinfo->ii_btop >= 0)
19148+ for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
19149+ if (au_hinode(iinfo, bindex)->hi_inode) {
19150+ iinfo->ii_bbot = bindex;
027c5e7a
AM
19151+ break;
19152+ }
5afbbe0d 19153+ AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
1308ab2a 19154+}
1facf9fc 19155+
dece6358 19156+/* ---------------------------------------------------------------------- */
1facf9fc 19157+
4a4d8108 19158+void au_icntnr_init_once(void *_c)
dece6358 19159+{
4a4d8108
AM
19160+ struct au_icntnr *c = _c;
19161+ struct au_iinfo *iinfo = &c->iinfo;
1facf9fc 19162+
be52b249 19163+ spin_lock_init(&iinfo->ii_generation.ig_spin);
4a4d8108
AM
19164+ au_rw_init(&iinfo->ii_rwsem);
19165+ inode_init_once(&c->vfs_inode);
19166+}
1facf9fc 19167+
5afbbe0d
AM
19168+void au_hinode_init(struct au_hinode *hinode)
19169+{
19170+ hinode->hi_inode = NULL;
19171+ hinode->hi_id = -1;
19172+ au_hn_init(hinode);
19173+ hinode->hi_whdentry = NULL;
19174+}
19175+
4a4d8108
AM
19176+int au_iinfo_init(struct inode *inode)
19177+{
19178+ struct au_iinfo *iinfo;
19179+ struct super_block *sb;
5afbbe0d 19180+ struct au_hinode *hi;
4a4d8108 19181+ int nbr, i;
1facf9fc 19182+
4a4d8108
AM
19183+ sb = inode->i_sb;
19184+ iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
5afbbe0d 19185+ nbr = au_sbbot(sb) + 1;
4a4d8108
AM
19186+ if (unlikely(nbr <= 0))
19187+ nbr = 1;
5afbbe0d
AM
19188+ hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
19189+ if (hi) {
acd2b654 19190+ au_lcnt_inc(&au_sbi(sb)->si_ninodes);
5afbbe0d
AM
19191+
19192+ iinfo->ii_hinode = hi;
19193+ for (i = 0; i < nbr; i++, hi++)
19194+ au_hinode_init(hi);
1facf9fc 19195+
537831f9 19196+ iinfo->ii_generation.ig_generation = au_sigen(sb);
5afbbe0d
AM
19197+ iinfo->ii_btop = -1;
19198+ iinfo->ii_bbot = -1;
4a4d8108
AM
19199+ iinfo->ii_vdir = NULL;
19200+ return 0;
1308ab2a 19201+ }
4a4d8108
AM
19202+ return -ENOMEM;
19203+}
1facf9fc 19204+
e2f27e51 19205+int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
4a4d8108 19206+{
5afbbe0d 19207+ int err, i;
4a4d8108 19208+ struct au_hinode *hip;
1facf9fc 19209+
4a4d8108
AM
19210+ AuRwMustWriteLock(&iinfo->ii_rwsem);
19211+
19212+ err = -ENOMEM;
e2f27e51
AM
19213+ hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
19214+ may_shrink);
4a4d8108
AM
19215+ if (hip) {
19216+ iinfo->ii_hinode = hip;
5afbbe0d
AM
19217+ i = iinfo->ii_bbot + 1;
19218+ hip += i;
19219+ for (; i < nbr; i++, hip++)
19220+ au_hinode_init(hip);
4a4d8108 19221+ err = 0;
1308ab2a 19222+ }
4a4d8108 19223+
1308ab2a 19224+ return err;
1facf9fc 19225+}
19226+
4a4d8108 19227+void au_iinfo_fin(struct inode *inode)
1facf9fc 19228+{
4a4d8108
AM
19229+ struct au_iinfo *iinfo;
19230+ struct au_hinode *hi;
19231+ struct super_block *sb;
5afbbe0d 19232+ aufs_bindex_t bindex, bbot;
b752ccd1 19233+ const unsigned char unlinked = !inode->i_nlink;
1308ab2a 19234+
5afbbe0d 19235+ AuDebugOn(au_is_bad_inode(inode));
1308ab2a 19236+
b752ccd1 19237+ sb = inode->i_sb;
acd2b654 19238+ au_lcnt_dec(&au_sbi(sb)->si_ninodes);
b752ccd1
AM
19239+ if (si_pid_test(sb))
19240+ au_xino_delete_inode(inode, unlinked);
19241+ else {
19242+ /*
19243+ * it is safe to hide the dependency between sbinfo and
19244+ * sb->s_umount.
19245+ */
19246+ lockdep_off();
19247+ si_noflush_read_lock(sb);
19248+ au_xino_delete_inode(inode, unlinked);
19249+ si_read_unlock(sb);
19250+ lockdep_on();
19251+ }
19252+
5afbbe0d 19253+ iinfo = au_ii(inode);
4a4d8108 19254+ if (iinfo->ii_vdir)
1c60b727 19255+ au_vdir_free(iinfo->ii_vdir);
1308ab2a 19256+
5afbbe0d 19257+ bindex = iinfo->ii_btop;
b752ccd1 19258+ if (bindex >= 0) {
5afbbe0d
AM
19259+ hi = au_hinode(iinfo, bindex);
19260+ bbot = iinfo->ii_bbot;
19261+ while (bindex++ <= bbot) {
b752ccd1 19262+ if (hi->hi_inode)
4a4d8108 19263+ au_hiput(hi);
4a4d8108
AM
19264+ hi++;
19265+ }
19266+ }
9f237c51 19267+ au_kfree_rcu(iinfo->ii_hinode);
4a4d8108 19268+ AuRwDestroy(&iinfo->ii_rwsem);
dece6358 19269+}
7f207e10 19270diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
eca34b5c 19271--- /usr/share/empty/fs/aufs/inode.c 1970-01-01 01:00:00.000000000 +0100
631230db 19272+++ linux/fs/aufs/inode.c 2023-10-31 09:31:04.199880750 +0100
0b2a12c6 19273@@ -0,0 +1,531 @@
cd7a4cd9 19274+// SPDX-License-Identifier: GPL-2.0
4a4d8108 19275+/*
0dcfbb52 19276+ * Copyright (C) 2005-2022 Junjiro R. Okajima
4a4d8108 19277+ *
0dcfbb52 19278+ * This program is free software; you can redistribute it and/or modify
4a4d8108
AM
19279+ * it under the terms of the GNU General Public License as published by
19280+ * the Free Software Foundation; either version 2 of the License, or
19281+ * (at your option) any later version.
19282+ *
19283+ * This program is distributed in the hope that it will be useful,
19284+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19285+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19286+ * GNU General Public License for more details.
19287+ *
19288+ * You should have received a copy of the GNU General Public License
523b37e3 19289+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 19290+ */
1facf9fc 19291+
4a4d8108
AM
19292+/*
19293+ * inode functions
19294+ */
1facf9fc 19295+
eca801bf 19296+#include <linux/iversion.h>
4a4d8108 19297+#include "aufs.h"
1308ab2a 19298+
4a4d8108
AM
19299+struct inode *au_igrab(struct inode *inode)
19300+{
19301+ if (inode) {
19302+ AuDebugOn(!atomic_read(&inode->i_count));
027c5e7a 19303+ ihold(inode);
1facf9fc 19304+ }
4a4d8108
AM
19305+ return inode;
19306+}
1facf9fc 19307+
4a4d8108
AM
19308+static void au_refresh_hinode_attr(struct inode *inode, int do_version)
19309+{
19310+ au_cpup_attr_all(inode, /*force*/0);
537831f9 19311+ au_update_iigen(inode, /*half*/1);
4a4d8108 19312+ if (do_version)
be118d29 19313+ inode_inc_iversion(inode);
dece6358 19314+}
1facf9fc 19315+
027c5e7a 19316+static int au_ii_refresh(struct inode *inode, int *update)
dece6358 19317+{
e2f27e51 19318+ int err, e, nbr;
027c5e7a 19319+ umode_t type;
4a4d8108 19320+ aufs_bindex_t bindex, new_bindex;
1308ab2a 19321+ struct super_block *sb;
4a4d8108 19322+ struct au_iinfo *iinfo;
027c5e7a 19323+ struct au_hinode *p, *q, tmp;
1facf9fc 19324+
5afbbe0d 19325+ AuDebugOn(au_is_bad_inode(inode));
4a4d8108 19326+ IiMustWriteLock(inode);
1facf9fc 19327+
027c5e7a 19328+ *update = 0;
4a4d8108 19329+ sb = inode->i_sb;
e2f27e51 19330+ nbr = au_sbbot(sb) + 1;
027c5e7a 19331+ type = inode->i_mode & S_IFMT;
4a4d8108 19332+ iinfo = au_ii(inode);
e2f27e51 19333+ err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
4a4d8108 19334+ if (unlikely(err))
1308ab2a 19335+ goto out;
1facf9fc 19336+
5afbbe0d
AM
19337+ AuDebugOn(iinfo->ii_btop < 0);
19338+ p = au_hinode(iinfo, iinfo->ii_btop);
19339+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
4a4d8108
AM
19340+ bindex++, p++) {
19341+ if (!p->hi_inode)
19342+ continue;
1facf9fc 19343+
027c5e7a 19344+ AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
4a4d8108
AM
19345+ new_bindex = au_br_index(sb, p->hi_id);
19346+ if (new_bindex == bindex)
19347+ continue;
1facf9fc 19348+
4a4d8108 19349+ if (new_bindex < 0) {
027c5e7a 19350+ *update = 1;
4a4d8108
AM
19351+ au_hiput(p);
19352+ p->hi_inode = NULL;
19353+ continue;
1308ab2a 19354+ }
4a4d8108 19355+
5afbbe0d
AM
19356+ if (new_bindex < iinfo->ii_btop)
19357+ iinfo->ii_btop = new_bindex;
19358+ if (iinfo->ii_bbot < new_bindex)
19359+ iinfo->ii_bbot = new_bindex;
4a4d8108 19360+ /* swap two lower inode, and loop again */
5afbbe0d 19361+ q = au_hinode(iinfo, new_bindex);
4a4d8108
AM
19362+ tmp = *q;
19363+ *q = *p;
19364+ *p = tmp;
19365+ if (tmp.hi_inode) {
19366+ bindex--;
19367+ p--;
1308ab2a 19368+ }
19369+ }
4a4d8108 19370+ au_update_ibrange(inode, /*do_put_zero*/0);
e2f27e51 19371+ au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
4a4d8108
AM
19372+ e = au_dy_irefresh(inode);
19373+ if (unlikely(e && !err))
19374+ err = e;
1facf9fc 19375+
4f0767ce 19376+out:
027c5e7a
AM
19377+ AuTraceErr(err);
19378+ return err;
19379+}
19380+
b95c5147
AM
19381+void au_refresh_iop(struct inode *inode, int force_getattr)
19382+{
19383+ int type;
19384+ struct au_sbinfo *sbi = au_sbi(inode->i_sb);
19385+ const struct inode_operations *iop
19386+ = force_getattr ? aufs_iop : sbi->si_iop_array;
19387+
19388+ if (inode->i_op == iop)
19389+ return;
19390+
19391+ switch (inode->i_mode & S_IFMT) {
19392+ case S_IFDIR:
19393+ type = AuIop_DIR;
19394+ break;
19395+ case S_IFLNK:
19396+ type = AuIop_SYMLINK;
19397+ break;
19398+ default:
19399+ type = AuIop_OTHER;
19400+ break;
19401+ }
19402+
19403+ inode->i_op = iop + type;
19404+ /* unnecessary smp_wmb() */
19405+}
19406+
027c5e7a
AM
19407+int au_refresh_hinode_self(struct inode *inode)
19408+{
19409+ int err, update;
19410+
19411+ err = au_ii_refresh(inode, &update);
19412+ if (!err)
19413+ au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
19414+
19415+ AuTraceErr(err);
4a4d8108
AM
19416+ return err;
19417+}
1facf9fc 19418+
4a4d8108
AM
19419+int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
19420+{
027c5e7a 19421+ int err, e, update;
4a4d8108 19422+ unsigned int flags;
027c5e7a 19423+ umode_t mode;
5afbbe0d 19424+ aufs_bindex_t bindex, bbot;
027c5e7a 19425+ unsigned char isdir;
4a4d8108
AM
19426+ struct au_hinode *p;
19427+ struct au_iinfo *iinfo;
1facf9fc 19428+
027c5e7a 19429+ err = au_ii_refresh(inode, &update);
4a4d8108
AM
19430+ if (unlikely(err))
19431+ goto out;
19432+
19433+ update = 0;
19434+ iinfo = au_ii(inode);
5afbbe0d 19435+ p = au_hinode(iinfo, iinfo->ii_btop);
027c5e7a
AM
19436+ mode = (inode->i_mode & S_IFMT);
19437+ isdir = S_ISDIR(mode);
4a4d8108 19438+ flags = au_hi_flags(inode, isdir);
5afbbe0d
AM
19439+ bbot = au_dbbot(dentry);
19440+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
5527c038 19441+ struct inode *h_i, *h_inode;
4a4d8108
AM
19442+ struct dentry *h_d;
19443+
19444+ h_d = au_h_dptr(dentry, bindex);
5527c038 19445+ if (!h_d || d_is_negative(h_d))
4a4d8108
AM
19446+ continue;
19447+
5527c038
JR
19448+ h_inode = d_inode(h_d);
19449+ AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
5afbbe0d 19450+ if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
4a4d8108
AM
19451+ h_i = au_h_iptr(inode, bindex);
19452+ if (h_i) {
5527c038 19453+ if (h_i == h_inode)
4a4d8108
AM
19454+ continue;
19455+ err = -EIO;
19456+ break;
19457+ }
19458+ }
5afbbe0d
AM
19459+ if (bindex < iinfo->ii_btop)
19460+ iinfo->ii_btop = bindex;
19461+ if (iinfo->ii_bbot < bindex)
19462+ iinfo->ii_bbot = bindex;
5527c038 19463+ au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
4a4d8108 19464+ update = 1;
1308ab2a 19465+ }
4a4d8108
AM
19466+ au_update_ibrange(inode, /*do_put_zero*/0);
19467+ e = au_dy_irefresh(inode);
19468+ if (unlikely(e && !err))
19469+ err = e;
027c5e7a
AM
19470+ if (!err)
19471+ au_refresh_hinode_attr(inode, update && isdir);
4a4d8108 19472+
4f0767ce 19473+out:
4a4d8108 19474+ AuTraceErr(err);
1308ab2a 19475+ return err;
dece6358
AM
19476+}
19477+
4a4d8108 19478+static int set_inode(struct inode *inode, struct dentry *dentry)
dece6358 19479+{
4a4d8108
AM
19480+ int err;
19481+ unsigned int flags;
19482+ umode_t mode;
5afbbe0d 19483+ aufs_bindex_t bindex, btop, btail;
4a4d8108
AM
19484+ unsigned char isdir;
19485+ struct dentry *h_dentry;
19486+ struct inode *h_inode;
19487+ struct au_iinfo *iinfo;
43982f53 19488+ const struct inode_operations *iop;
dece6358 19489+
4a4d8108 19490+ IiMustWriteLock(inode);
dece6358 19491+
4a4d8108
AM
19492+ err = 0;
19493+ isdir = 0;
b95c5147 19494+ iop = au_sbi(inode->i_sb)->si_iop_array;
5afbbe0d
AM
19495+ btop = au_dbtop(dentry);
19496+ h_dentry = au_h_dptr(dentry, btop);
5527c038 19497+ h_inode = d_inode(h_dentry);
4a4d8108
AM
19498+ mode = h_inode->i_mode;
19499+ switch (mode & S_IFMT) {
19500+ case S_IFREG:
19501+ btail = au_dbtail(dentry);
b95c5147 19502+ inode->i_op = iop + AuIop_OTHER;
4a4d8108 19503+ inode->i_fop = &aufs_file_fop;
5afbbe0d 19504+ err = au_dy_iaop(inode, btop, h_inode);
4a4d8108
AM
19505+ if (unlikely(err))
19506+ goto out;
19507+ break;
19508+ case S_IFDIR:
19509+ isdir = 1;
19510+ btail = au_dbtaildir(dentry);
b95c5147 19511+ inode->i_op = iop + AuIop_DIR;
4a4d8108
AM
19512+ inode->i_fop = &aufs_dir_fop;
19513+ break;
19514+ case S_IFLNK:
19515+ btail = au_dbtail(dentry);
b95c5147 19516+ inode->i_op = iop + AuIop_SYMLINK;
4a4d8108
AM
19517+ break;
19518+ case S_IFBLK:
19519+ case S_IFCHR:
19520+ case S_IFIFO:
19521+ case S_IFSOCK:
19522+ btail = au_dbtail(dentry);
b95c5147 19523+ inode->i_op = iop + AuIop_OTHER;
38d290e6 19524+ init_special_inode(inode, mode, h_inode->i_rdev);
4a4d8108
AM
19525+ break;
19526+ default:
19527+ AuIOErr("Unknown file type 0%o\n", mode);
19528+ err = -EIO;
1308ab2a 19529+ goto out;
4a4d8108 19530+ }
dece6358 19531+
4a4d8108
AM
19532+ /* do not set hnotify for whiteouted dirs (SHWH mode) */
19533+ flags = au_hi_flags(inode, isdir);
19534+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
19535+ && au_ftest_hi(flags, HNOTIFY)
19536+ && dentry->d_name.len > AUFS_WH_PFX_LEN
19537+ && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
19538+ au_fclr_hi(flags, HNOTIFY);
19539+ iinfo = au_ii(inode);
5afbbe0d
AM
19540+ iinfo->ii_btop = btop;
19541+ iinfo->ii_bbot = btail;
19542+ for (bindex = btop; bindex <= btail; bindex++) {
4a4d8108
AM
19543+ h_dentry = au_h_dptr(dentry, bindex);
19544+ if (h_dentry)
19545+ au_set_h_iptr(inode, bindex,
5527c038 19546+ au_igrab(d_inode(h_dentry)), flags);
4a4d8108
AM
19547+ }
19548+ au_cpup_attr_all(inode, /*force*/1);
c1595e42 19549+ /*
aec24f17 19550+ * to force calling aufs_get_inode_acl() every time,
c1595e42
JR
19551+ * do not call cache_no_acl() for aufs inode.
19552+ */
dece6358 19553+
4f0767ce 19554+out:
4a4d8108
AM
19555+ return err;
19556+}
dece6358 19557+
027c5e7a
AM
19558+/*
19559+ * successful returns with iinfo write_locked
19560+ * minus: errno
19561+ * zero: success, matched
19562+ * plus: no error, but unmatched
19563+ */
19564+static int reval_inode(struct inode *inode, struct dentry *dentry)
4a4d8108
AM
19565+{
19566+ int err;
cfc41e69 19567+ unsigned int gen, igflags;
5afbbe0d 19568+ aufs_bindex_t bindex, bbot;
4a4d8108 19569+ struct inode *h_inode, *h_dinode;
5527c038 19570+ struct dentry *h_dentry;
dece6358 19571+
4a4d8108
AM
19572+ /*
19573+ * before this function, if aufs got any iinfo lock, it must be only
19574+ * one, the parent dir.
19575+ * it can happen by UDBA and the obsoleted inode number.
19576+ */
19577+ err = -EIO;
19578+ if (unlikely(inode->i_ino == parent_ino(dentry)))
19579+ goto out;
19580+
027c5e7a 19581+ err = 1;
4a4d8108 19582+ ii_write_lock_new_child(inode);
5afbbe0d 19583+ h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
5527c038 19584+ h_dinode = d_inode(h_dentry);
5afbbe0d
AM
19585+ bbot = au_ibbot(inode);
19586+ for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
4a4d8108 19587+ h_inode = au_h_iptr(inode, bindex);
537831f9
AM
19588+ if (!h_inode || h_inode != h_dinode)
19589+ continue;
19590+
19591+ err = 0;
cfc41e69 19592+ gen = au_iigen(inode, &igflags);
537831f9 19593+ if (gen == au_digen(dentry)
cfc41e69 19594+ && !au_ig_ftest(igflags, HALF_REFRESHED))
4a4d8108 19595+ break;
537831f9
AM
19596+
19597+ /* fully refresh inode using dentry */
19598+ err = au_refresh_hinode(inode, dentry);
19599+ if (!err)
19600+ au_update_iigen(inode, /*half*/0);
19601+ break;
1facf9fc 19602+ }
dece6358 19603+
4a4d8108
AM
19604+ if (unlikely(err))
19605+ ii_write_unlock(inode);
4f0767ce 19606+out:
1facf9fc 19607+ return err;
19608+}
1facf9fc 19609+
4a4d8108
AM
19610+int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
19611+ unsigned int d_type, ino_t *ino)
1facf9fc 19612+{
521ced18
JR
19613+ int err, idx;
19614+ const int isnondir = d_type != DT_DIR;
1facf9fc 19615+
b752ccd1 19616+ /* prevent hardlinked inode number from race condition */
521ced18
JR
19617+ if (isnondir) {
19618+ err = au_xinondir_enter(sb, bindex, h_ino, &idx);
19619+ if (unlikely(err))
19620+ goto out;
4a4d8108 19621+ }
521ced18 19622+
4a4d8108
AM
19623+ err = au_xino_read(sb, bindex, h_ino, ino);
19624+ if (unlikely(err))
521ced18 19625+ goto out_xinondir;
1308ab2a 19626+
4a4d8108
AM
19627+ if (!*ino) {
19628+ err = -EIO;
19629+ *ino = au_xino_new_ino(sb);
19630+ if (unlikely(!*ino))
521ced18 19631+ goto out_xinondir;
4a4d8108
AM
19632+ err = au_xino_write(sb, bindex, h_ino, *ino);
19633+ if (unlikely(err))
521ced18 19634+ goto out_xinondir;
1308ab2a 19635+ }
1facf9fc 19636+
521ced18
JR
19637+out_xinondir:
19638+ if (isnondir && idx >= 0)
19639+ au_xinondir_leave(sb, bindex, h_ino, idx);
4f0767ce 19640+out:
1facf9fc 19641+ return err;
19642+}
19643+
4a4d8108
AM
19644+/* successful returns with iinfo write_locked */
19645+/* todo: return with unlocked? */
19646+struct inode *au_new_inode(struct dentry *dentry, int must_new)
1facf9fc 19647+{
5527c038 19648+ struct inode *inode, *h_inode;
4a4d8108
AM
19649+ struct dentry *h_dentry;
19650+ struct super_block *sb;
19651+ ino_t h_ino, ino;
521ced18 19652+ int err, idx, hlinked;
5afbbe0d 19653+ aufs_bindex_t btop;
1facf9fc 19654+
4a4d8108 19655+ sb = dentry->d_sb;
5afbbe0d
AM
19656+ btop = au_dbtop(dentry);
19657+ h_dentry = au_h_dptr(dentry, btop);
5527c038
JR
19658+ h_inode = d_inode(h_dentry);
19659+ h_ino = h_inode->i_ino;
521ced18 19660+ hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
b752ccd1 19661+
521ced18 19662+new_ino:
b752ccd1
AM
19663+ /*
19664+ * stop 'race'-ing between hardlinks under different
19665+ * parents.
19666+ */
521ced18
JR
19667+ if (hlinked) {
19668+ err = au_xinondir_enter(sb, btop, h_ino, &idx);
19669+ inode = ERR_PTR(err);
19670+ if (unlikely(err))
19671+ goto out;
19672+ }
b752ccd1 19673+
5afbbe0d 19674+ err = au_xino_read(sb, btop, h_ino, &ino);
4a4d8108
AM
19675+ inode = ERR_PTR(err);
19676+ if (unlikely(err))
521ced18 19677+ goto out_xinondir;
b752ccd1 19678+
4a4d8108
AM
19679+ if (!ino) {
19680+ ino = au_xino_new_ino(sb);
19681+ if (unlikely(!ino)) {
19682+ inode = ERR_PTR(-EIO);
521ced18 19683+ goto out_xinondir;
dece6358
AM
19684+ }
19685+ }
1facf9fc 19686+
4a4d8108
AM
19687+ AuDbg("i%lu\n", (unsigned long)ino);
19688+ inode = au_iget_locked(sb, ino);
19689+ err = PTR_ERR(inode);
19690+ if (IS_ERR(inode))
521ced18 19691+ goto out_xinondir;
1facf9fc 19692+
4a4d8108
AM
19693+ AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
19694+ if (inode->i_state & I_NEW) {
19695+ ii_write_lock_new_child(inode);
19696+ err = set_inode(inode, dentry);
19697+ if (!err) {
19698+ unlock_new_inode(inode);
521ced18 19699+ goto out_xinondir; /* success */
4a4d8108 19700+ }
1308ab2a 19701+
027c5e7a
AM
19702+ /*
19703+ * iget_failed() calls iput(), but we need to call
19704+ * ii_write_unlock() after iget_failed(). so dirty hack for
19705+ * i_count.
19706+ */
19707+ atomic_inc(&inode->i_count);
4a4d8108 19708+ iget_failed(inode);
027c5e7a 19709+ ii_write_unlock(inode);
5afbbe0d 19710+ au_xino_write(sb, btop, h_ino, /*ino*/0);
027c5e7a
AM
19711+ /* ignore this error */
19712+ goto out_iput;
19713+ } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
b752ccd1
AM
19714+ /*
19715+ * horrible race condition between lookup, readdir and copyup
19716+ * (or something).
19717+ */
521ced18
JR
19718+ if (hlinked && idx >= 0)
19719+ au_xinondir_leave(sb, btop, h_ino, idx);
027c5e7a
AM
19720+ err = reval_inode(inode, dentry);
19721+ if (unlikely(err < 0)) {
521ced18 19722+ hlinked = 0;
027c5e7a
AM
19723+ goto out_iput;
19724+ }
521ced18 19725+ if (!err)
4a4d8108 19726+ goto out; /* success */
521ced18
JR
19727+ else if (hlinked && idx >= 0) {
19728+ err = au_xinondir_enter(sb, btop, h_ino, &idx);
19729+ if (unlikely(err)) {
19730+ iput(inode);
19731+ inode = ERR_PTR(err);
19732+ goto out;
19733+ }
19734+ }
4a4d8108
AM
19735+ }
19736+
5527c038 19737+ if (unlikely(au_test_fs_unique_ino(h_inode)))
4a4d8108 19738+ AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
523b37e3 19739+ " b%d, %s, %pd, hi%lu, i%lu.\n",
5afbbe0d 19740+ btop, au_sbtype(h_dentry->d_sb), dentry,
4a4d8108
AM
19741+ (unsigned long)h_ino, (unsigned long)ino);
19742+ ino = 0;
5afbbe0d 19743+ err = au_xino_write(sb, btop, h_ino, /*ino*/0);
4a4d8108
AM
19744+ if (!err) {
19745+ iput(inode);
521ced18
JR
19746+ if (hlinked && idx >= 0)
19747+ au_xinondir_leave(sb, btop, h_ino, idx);
4a4d8108
AM
19748+ goto new_ino;
19749+ }
1308ab2a 19750+
4f0767ce 19751+out_iput:
4a4d8108 19752+ iput(inode);
4a4d8108 19753+ inode = ERR_PTR(err);
521ced18
JR
19754+out_xinondir:
19755+ if (hlinked && idx >= 0)
19756+ au_xinondir_leave(sb, btop, h_ino, idx);
4f0767ce 19757+out:
4a4d8108 19758+ return inode;
1facf9fc 19759+}
19760+
4a4d8108 19761+/* ---------------------------------------------------------------------- */
1facf9fc 19762+
4a4d8108
AM
19763+int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
19764+ struct inode *inode)
19765+{
19766+ int err;
076b876e 19767+ struct inode *hi;
1facf9fc 19768+
4a4d8108 19769+ err = au_br_rdonly(au_sbr(sb, bindex));
1facf9fc 19770+
4a4d8108
AM
19771+ /* pseudo-link after flushed may happen out of bounds */
19772+ if (!err
19773+ && inode
5afbbe0d
AM
19774+ && au_ibtop(inode) <= bindex
19775+ && bindex <= au_ibbot(inode)) {
4a4d8108
AM
19776+ /*
19777+ * permission check is unnecessary since vfsub routine
19778+ * will be called later
19779+ */
076b876e 19780+ hi = au_h_iptr(inode, bindex);
4a4d8108
AM
19781+ if (hi)
19782+ err = IS_IMMUTABLE(hi) ? -EROFS : 0;
1facf9fc 19783+ }
19784+
4a4d8108
AM
19785+ return err;
19786+}
dece6358 19787+
5db017eb 19788+int au_test_h_perm(struct mnt_idmap *h_idmap, struct inode *h_inode,
0b2a12c6 19789+ int mask)
4a4d8108 19790+{
2dfbb274 19791+ if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
4a4d8108 19792+ return 0;
5db017eb 19793+ return inode_permission(h_idmap, h_inode, mask);
4a4d8108 19794+}
1facf9fc 19795+
5db017eb 19796+int au_test_h_perm_sio(struct mnt_idmap *h_idmap, struct inode *h_inode,
0b2a12c6 19797+ int mask)
4a4d8108
AM
19798+{
19799+ if (au_test_nfs(h_inode->i_sb)
19800+ && (mask & MAY_WRITE)
19801+ && S_ISDIR(h_inode->i_mode))
19802+ mask |= MAY_READ; /* force permission check */
5db017eb 19803+ return au_test_h_perm(h_idmap, h_inode, mask);
1facf9fc 19804+}
7f207e10 19805diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
eca34b5c 19806--- /usr/share/empty/fs/aufs/inode.h 1970-01-01 01:00:00.000000000 +0100
631230db 19807+++ linux/fs/aufs/inode.h 2023-10-31 09:31:04.199880750 +0100
aec24f17 19808@@ -0,0 +1,707 @@
062440b3 19809+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 19810+/*
0dcfbb52 19811+ * Copyright (C) 2005-2022 Junjiro R. Okajima
4a4d8108 19812+ *
0dcfbb52 19813+ * This program is free software; you can redistribute it and/or modify
4a4d8108
AM
19814+ * it under the terms of the GNU General Public License as published by
19815+ * the Free Software Foundation; either version 2 of the License, or
19816+ * (at your option) any later version.
19817+ *
19818+ * This program is distributed in the hope that it will be useful,
19819+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19820+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19821+ * GNU General Public License for more details.
19822+ *
19823+ * You should have received a copy of the GNU General Public License
523b37e3 19824+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 19825+ */
1facf9fc 19826+
1308ab2a 19827+/*
4a4d8108 19828+ * inode operations
1308ab2a 19829+ */
dece6358 19830+
4a4d8108
AM
19831+#ifndef __AUFS_INODE_H__
19832+#define __AUFS_INODE_H__
dece6358 19833+
4a4d8108 19834+#ifdef __KERNEL__
1308ab2a 19835+
4a4d8108 19836+#include <linux/fsnotify.h>
4a4d8108 19837+#include "rwsem.h"
1308ab2a 19838+
4a4d8108 19839+struct vfsmount;
1facf9fc 19840+
4a4d8108
AM
19841+struct au_hnotify {
19842+#ifdef CONFIG_AUFS_HNOTIFY
19843+#ifdef CONFIG_AUFS_HFSNOTIFY
7f207e10 19844+ /* never use fsnotify_add_vfsmount_mark() */
0c5527e5 19845+ struct fsnotify_mark hn_mark;
4a4d8108 19846+#endif
1c60b727 19847+ struct inode *hn_aufs_inode; /* no get/put */
9f237c51 19848+ struct rcu_head rcu;
4a4d8108
AM
19849+#endif
19850+} ____cacheline_aligned_in_smp;
1facf9fc 19851+
4a4d8108
AM
19852+struct au_hinode {
19853+ struct inode *hi_inode;
19854+ aufs_bindex_t hi_id;
19855+#ifdef CONFIG_AUFS_HNOTIFY
19856+ struct au_hnotify *hi_notify;
19857+#endif
dece6358 19858+
4a4d8108
AM
19859+ /* reference to the copied-up whiteout with get/put */
19860+ struct dentry *hi_whdentry;
19861+};
dece6358 19862+
537831f9
AM
19863+/* ig_flags */
19864+#define AuIG_HALF_REFRESHED 1
19865+#define au_ig_ftest(flags, name) ((flags) & AuIG_##name)
19866+#define au_ig_fset(flags, name) \
19867+ do { (flags) |= AuIG_##name; } while (0)
19868+#define au_ig_fclr(flags, name) \
19869+ do { (flags) &= ~AuIG_##name; } while (0)
19870+
19871+struct au_iigen {
be52b249 19872+ spinlock_t ig_spin;
537831f9
AM
19873+ __u32 ig_generation, ig_flags;
19874+};
19875+
4a4d8108
AM
19876+struct au_vdir;
19877+struct au_iinfo {
7a9e40b8 19878+ struct au_iigen ii_generation;
4a4d8108 19879+ struct super_block *ii_hsb1; /* no get/put */
1facf9fc 19880+
4a4d8108 19881+ struct au_rwsem ii_rwsem;
5afbbe0d 19882+ aufs_bindex_t ii_btop, ii_bbot;
4a4d8108
AM
19883+ __u32 ii_higen;
19884+ struct au_hinode *ii_hinode;
19885+ struct au_vdir *ii_vdir;
19886+};
1facf9fc 19887+
4a4d8108 19888+struct au_icntnr {
9f237c51
AM
19889+ struct au_iinfo iinfo;
19890+ struct inode vfs_inode;
19891+ struct hlist_bl_node plink;
19892+ struct rcu_head rcu;
4a4d8108 19893+} ____cacheline_aligned_in_smp;
1308ab2a 19894+
4a4d8108
AM
19895+/* au_pin flags */
19896+#define AuPin_DI_LOCKED 1
19897+#define AuPin_MNT_WRITE (1 << 1)
19898+#define au_ftest_pin(flags, name) ((flags) & AuPin_##name)
7f207e10
AM
19899+#define au_fset_pin(flags, name) \
19900+ do { (flags) |= AuPin_##name; } while (0)
19901+#define au_fclr_pin(flags, name) \
19902+ do { (flags) &= ~AuPin_##name; } while (0)
4a4d8108
AM
19903+
19904+struct au_pin {
19905+ /* input */
19906+ struct dentry *dentry;
19907+ unsigned int udba;
19908+ unsigned char lsc_di, lsc_hi, flags;
19909+ aufs_bindex_t bindex;
19910+
19911+ /* output */
19912+ struct dentry *parent;
19913+ struct au_hinode *hdir;
19914+ struct vfsmount *h_mnt;
86dc4139
AM
19915+
19916+ /* temporary unlock/relock for copyup */
19917+ struct dentry *h_dentry, *h_parent;
19918+ struct au_branch *br;
19919+ struct task_struct *task;
4a4d8108 19920+};
1facf9fc 19921+
86dc4139 19922+void au_pin_hdir_unlock(struct au_pin *p);
c1595e42 19923+int au_pin_hdir_lock(struct au_pin *p);
86dc4139 19924+int au_pin_hdir_relock(struct au_pin *p);
86dc4139
AM
19925+void au_pin_hdir_acquire_nest(struct au_pin *p);
19926+void au_pin_hdir_release(struct au_pin *p);
19927+
1308ab2a 19928+/* ---------------------------------------------------------------------- */
19929+
4a4d8108 19930+static inline struct au_iinfo *au_ii(struct inode *inode)
1facf9fc 19931+{
5afbbe0d
AM
19932+ BUG_ON(is_bad_inode(inode));
19933+ return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
4a4d8108 19934+}
1facf9fc 19935+
4a4d8108 19936+/* ---------------------------------------------------------------------- */
1facf9fc 19937+
4a4d8108
AM
19938+/* inode.c */
19939+struct inode *au_igrab(struct inode *inode);
b95c5147 19940+void au_refresh_iop(struct inode *inode, int force_getattr);
027c5e7a 19941+int au_refresh_hinode_self(struct inode *inode);
4a4d8108
AM
19942+int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
19943+int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
19944+ unsigned int d_type, ino_t *ino);
19945+struct inode *au_new_inode(struct dentry *dentry, int must_new);
19946+int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
19947+ struct inode *inode);
5db017eb 19948+int au_test_h_perm(struct mnt_idmap *h_idmap, struct inode *h_inode,
0b2a12c6 19949+ int mask);
5db017eb 19950+int au_test_h_perm_sio(struct mnt_idmap *h_idmap, struct inode *h_inode,
0b2a12c6 19951+ int mask);
1facf9fc 19952+
4a4d8108
AM
19953+static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
19954+ ino_t h_ino, unsigned int d_type, ino_t *ino)
19955+{
19956+#ifdef CONFIG_AUFS_SHWH
19957+ return au_ino(sb, bindex, h_ino, d_type, ino);
19958+#else
19959+ return 0;
19960+#endif
19961+}
1facf9fc 19962+
4a4d8108 19963+/* i_op.c */
b95c5147
AM
19964+enum {
19965+ AuIop_SYMLINK,
19966+ AuIop_DIR,
19967+ AuIop_OTHER,
19968+ AuIop_Last
19969+};
43982f53 19970+extern struct inode_operations aufs_iop[AuIop_Last], /* not const */
b95c5147 19971+ aufs_iop_nogetattr[AuIop_Last];
1308ab2a 19972+
4a4d8108
AM
19973+/* au_wr_dir flags */
19974+#define AuWrDir_ADD_ENTRY 1
7e9cd9fe
AM
19975+#define AuWrDir_ISDIR (1 << 1)
19976+#define AuWrDir_TMPFILE (1 << 2)
4a4d8108 19977+#define au_ftest_wrdir(flags, name) ((flags) & AuWrDir_##name)
7f207e10
AM
19978+#define au_fset_wrdir(flags, name) \
19979+ do { (flags) |= AuWrDir_##name; } while (0)
19980+#define au_fclr_wrdir(flags, name) \
19981+ do { (flags) &= ~AuWrDir_##name; } while (0)
1facf9fc 19982+
4a4d8108
AM
19983+struct au_wr_dir_args {
19984+ aufs_bindex_t force_btgt;
19985+ unsigned char flags;
19986+};
19987+int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
19988+ struct au_wr_dir_args *args);
dece6358 19989+
4a4d8108
AM
19990+struct dentry *au_pinned_h_parent(struct au_pin *pin);
19991+void au_pin_init(struct au_pin *pin, struct dentry *dentry,
19992+ aufs_bindex_t bindex, int lsc_di, int lsc_hi,
19993+ unsigned int udba, unsigned char flags);
19994+int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
19995+ unsigned int udba, unsigned char flags) __must_check;
19996+int au_do_pin(struct au_pin *pin) __must_check;
19997+void au_unpin(struct au_pin *pin);
c1595e42
JR
19998+int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
19999+
20000+#define AuIcpup_DID_CPUP 1
20001+#define au_ftest_icpup(flags, name) ((flags) & AuIcpup_##name)
20002+#define au_fset_icpup(flags, name) \
20003+ do { (flags) |= AuIcpup_##name; } while (0)
20004+#define au_fclr_icpup(flags, name) \
20005+ do { (flags) &= ~AuIcpup_##name; } while (0)
20006+
20007+struct au_icpup_args {
20008+ unsigned char flags;
20009+ unsigned char pin_flags;
20010+ aufs_bindex_t btgt;
20011+ unsigned int udba;
20012+ struct au_pin pin;
20013+ struct path h_path;
20014+ struct inode *h_inode;
20015+};
20016+
20017+int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
20018+ struct au_icpup_args *a);
20019+
42b5c33a
AM
20020+int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
20021+ struct path *h_path, int locked);
1facf9fc 20022+
4a4d8108
AM
20023+/* i_op_add.c */
20024+int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
20025+ struct dentry *h_parent, int isdir);
5db017eb 20026+int aufs_mknod(struct mnt_idmap *idmap, struct inode *dir,
0b2a12c6 20027+ struct dentry *dentry, umode_t mode, dev_t dev);
5db017eb 20028+int aufs_symlink(struct mnt_idmap *idmap, struct inode *dir,
0b2a12c6 20029+ struct dentry *dentry, const char *symname);
5db017eb 20030+int aufs_create(struct mnt_idmap *idmap, struct inode *dir,
0b2a12c6 20031+ struct dentry *dentry, umode_t mode, bool want_excl);
b912730e
AM
20032+struct vfsub_aopen_args;
20033+int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
20034+ struct vfsub_aopen_args *args);
5db017eb 20035+int aufs_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
2255d0fe 20036+ struct file *file, umode_t mode);
4a4d8108
AM
20037+int aufs_link(struct dentry *src_dentry, struct inode *dir,
20038+ struct dentry *dentry);
5db017eb 20039+int aufs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
0b2a12c6 20040+ struct dentry *dentry, umode_t mode);
1facf9fc 20041+
4a4d8108
AM
20042+/* i_op_del.c */
20043+int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
20044+int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
20045+ struct dentry *h_parent, int isdir);
20046+int aufs_unlink(struct inode *dir, struct dentry *dentry);
20047+int aufs_rmdir(struct inode *dir, struct dentry *dentry);
1308ab2a 20048+
4a4d8108
AM
20049+/* i_op_ren.c */
20050+int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
5db017eb 20051+int aufs_rename(struct mnt_idmap *idmap,
0b2a12c6
JR
20052+ struct inode *_src_dir, struct dentry *_src_dentry,
20053+ struct inode *_dst_dir, struct dentry *_dst_dentry,
20054+ unsigned int _flags);
1facf9fc 20055+
4a4d8108
AM
20056+/* iinfo.c */
20057+struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
20058+void au_hiput(struct au_hinode *hinode);
20059+void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
20060+ struct dentry *h_wh);
20061+unsigned int au_hi_flags(struct inode *inode, int isdir);
1308ab2a 20062+
4a4d8108
AM
20063+/* hinode flags */
20064+#define AuHi_XINO 1
20065+#define AuHi_HNOTIFY (1 << 1)
20066+#define au_ftest_hi(flags, name) ((flags) & AuHi_##name)
7f207e10
AM
20067+#define au_fset_hi(flags, name) \
20068+ do { (flags) |= AuHi_##name; } while (0)
20069+#define au_fclr_hi(flags, name) \
20070+ do { (flags) &= ~AuHi_##name; } while (0)
1facf9fc 20071+
4a4d8108
AM
20072+#ifndef CONFIG_AUFS_HNOTIFY
20073+#undef AuHi_HNOTIFY
20074+#define AuHi_HNOTIFY 0
20075+#endif
1facf9fc 20076+
4a4d8108
AM
20077+void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
20078+ struct inode *h_inode, unsigned int flags);
1facf9fc 20079+
537831f9 20080+void au_update_iigen(struct inode *inode, int half);
4a4d8108 20081+void au_update_ibrange(struct inode *inode, int do_put_zero);
1facf9fc 20082+
4a4d8108 20083+void au_icntnr_init_once(void *_c);
5afbbe0d 20084+void au_hinode_init(struct au_hinode *hinode);
4a4d8108
AM
20085+int au_iinfo_init(struct inode *inode);
20086+void au_iinfo_fin(struct inode *inode);
e2f27e51 20087+int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
1308ab2a 20088+
e49829fe 20089+#ifdef CONFIG_PROC_FS
4a4d8108 20090+/* plink.c */
e49829fe 20091+int au_plink_maint(struct super_block *sb, int flags);
7e9cd9fe 20092+struct au_sbinfo;
e49829fe
JR
20093+void au_plink_maint_leave(struct au_sbinfo *sbinfo);
20094+int au_plink_maint_enter(struct super_block *sb);
4a4d8108
AM
20095+#ifdef CONFIG_AUFS_DEBUG
20096+void au_plink_list(struct super_block *sb);
20097+#else
20098+AuStubVoid(au_plink_list, struct super_block *sb)
20099+#endif
20100+int au_plink_test(struct inode *inode);
20101+struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
20102+void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
20103+ struct dentry *h_dentry);
e49829fe
JR
20104+void au_plink_put(struct super_block *sb, int verbose);
20105+void au_plink_clean(struct super_block *sb, int verbose);
4a4d8108 20106+void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
e49829fe
JR
20107+#else
20108+AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
20109+AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
20110+AuStubInt0(au_plink_maint_enter, struct super_block *sb);
20111+AuStubVoid(au_plink_list, struct super_block *sb);
20112+AuStubInt0(au_plink_test, struct inode *inode);
20113+AuStub(struct dentry *, au_plink_lkup, return NULL,
20114+ struct inode *inode, aufs_bindex_t bindex);
20115+AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
20116+ struct dentry *h_dentry);
20117+AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
20118+AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
20119+AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
20120+#endif /* CONFIG_PROC_FS */
1facf9fc 20121+
c1595e42
JR
20122+#ifdef CONFIG_AUFS_XATTR
20123+/* xattr.c */
0b2a12c6 20124+int au_cpup_xattr(struct path *h_dst, struct path *h_src, int ignore_flags,
7e9cd9fe 20125+ unsigned int verbose);
c1595e42 20126+ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
f2c43d5f 20127+void au_xattr_init(struct super_block *sb);
c1595e42 20128+#else
0b2a12c6 20129+AuStubInt0(au_cpup_xattr, struct path *h_dst, struct path *h_src,
7e9cd9fe 20130+ int ignore_flags, unsigned int verbose);
f2c43d5f 20131+AuStubVoid(au_xattr_init, struct super_block *sb);
c1595e42
JR
20132+#endif
20133+
20134+#ifdef CONFIG_FS_POSIX_ACL
aec24f17 20135+struct posix_acl *aufs_get_inode_acl(struct inode *inode, int type, bool rcu);
5db017eb 20136+struct posix_acl *aufs_get_acl(struct mnt_idmap *idmap,
aec24f17 20137+ struct dentry *dentry, int type);
5db017eb 20138+int aufs_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
0b2a12c6 20139+ struct posix_acl *acl, int type);
c1595e42
JR
20140+#endif
20141+
20142+#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
20143+enum {
20144+ AU_XATTR_SET,
c1595e42
JR
20145+ AU_ACL_SET
20146+};
20147+
f2c43d5f 20148+struct au_sxattr {
c1595e42
JR
20149+ int type;
20150+ union {
20151+ struct {
20152+ const char *name;
2255d0fe 20153+ const void *value;
c1595e42
JR
20154+ size_t size;
20155+ int flags;
20156+ } set;
20157+ struct {
c1595e42
JR
20158+ struct posix_acl *acl;
20159+ int type;
20160+ } acl_set;
20161+ } u;
20162+};
f2c43d5f
AM
20163+ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
20164+ struct au_sxattr *arg);
c1595e42
JR
20165+#endif
20166+
4a4d8108 20167+/* ---------------------------------------------------------------------- */
1308ab2a 20168+
4a4d8108
AM
20169+/* lock subclass for iinfo */
20170+enum {
20171+ AuLsc_II_CHILD, /* child first */
20172+ AuLsc_II_CHILD2, /* rename(2), link(2), and cpup at hnotify */
20173+ AuLsc_II_CHILD3, /* copyup dirs */
20174+ AuLsc_II_PARENT, /* see AuLsc_I_PARENT in vfsub.h */
20175+ AuLsc_II_PARENT2,
20176+ AuLsc_II_PARENT3, /* copyup dirs */
20177+ AuLsc_II_NEW_CHILD
20178+};
1308ab2a 20179+
1facf9fc 20180+/*
4a4d8108
AM
20181+ * ii_read_lock_child, ii_write_lock_child,
20182+ * ii_read_lock_child2, ii_write_lock_child2,
20183+ * ii_read_lock_child3, ii_write_lock_child3,
20184+ * ii_read_lock_parent, ii_write_lock_parent,
20185+ * ii_read_lock_parent2, ii_write_lock_parent2,
20186+ * ii_read_lock_parent3, ii_write_lock_parent3,
20187+ * ii_read_lock_new_child, ii_write_lock_new_child,
1facf9fc 20188+ */
4a4d8108
AM
20189+#define AuReadLockFunc(name, lsc) \
20190+static inline void ii_read_lock_##name(struct inode *i) \
20191+{ \
20192+ au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
20193+}
20194+
20195+#define AuWriteLockFunc(name, lsc) \
20196+static inline void ii_write_lock_##name(struct inode *i) \
20197+{ \
20198+ au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
20199+}
20200+
20201+#define AuRWLockFuncs(name, lsc) \
20202+ AuReadLockFunc(name, lsc) \
20203+ AuWriteLockFunc(name, lsc)
20204+
20205+AuRWLockFuncs(child, CHILD);
20206+AuRWLockFuncs(child2, CHILD2);
20207+AuRWLockFuncs(child3, CHILD3);
20208+AuRWLockFuncs(parent, PARENT);
20209+AuRWLockFuncs(parent2, PARENT2);
20210+AuRWLockFuncs(parent3, PARENT3);
20211+AuRWLockFuncs(new_child, NEW_CHILD);
20212+
20213+#undef AuReadLockFunc
20214+#undef AuWriteLockFunc
20215+#undef AuRWLockFuncs
1facf9fc 20216+
8b6a4947
AM
20217+#define ii_read_unlock(i) au_rw_read_unlock(&au_ii(i)->ii_rwsem)
20218+#define ii_write_unlock(i) au_rw_write_unlock(&au_ii(i)->ii_rwsem)
20219+#define ii_downgrade_lock(i) au_rw_dgrade_lock(&au_ii(i)->ii_rwsem)
1facf9fc 20220+
4a4d8108
AM
20221+#define IiMustNoWaiters(i) AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
20222+#define IiMustAnyLock(i) AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
20223+#define IiMustWriteLock(i) AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
1facf9fc 20224+
4a4d8108 20225+/* ---------------------------------------------------------------------- */
1308ab2a 20226+
027c5e7a
AM
20227+static inline void au_icntnr_init(struct au_icntnr *c)
20228+{
20229+#ifdef CONFIG_AUFS_DEBUG
20230+ c->vfs_inode.i_mode = 0;
20231+#endif
20232+}
20233+
cfc41e69 20234+static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
4a4d8108 20235+{
537831f9
AM
20236+ unsigned int gen;
20237+ struct au_iinfo *iinfo;
be52b249 20238+ struct au_iigen *iigen;
537831f9
AM
20239+
20240+ iinfo = au_ii(inode);
be52b249
AM
20241+ iigen = &iinfo->ii_generation;
20242+ spin_lock(&iigen->ig_spin);
cfc41e69
AM
20243+ if (igflags)
20244+ *igflags = iigen->ig_flags;
be52b249
AM
20245+ gen = iigen->ig_generation;
20246+ spin_unlock(&iigen->ig_spin);
537831f9
AM
20247+
20248+ return gen;
4a4d8108 20249+}
1308ab2a 20250+
4a4d8108
AM
20251+/* tiny test for inode number */
20252+/* tmpfs generation is too rough */
20253+static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
20254+{
20255+ struct au_iinfo *iinfo;
1308ab2a 20256+
4a4d8108
AM
20257+ iinfo = au_ii(inode);
20258+ AuRwMustAnyLock(&iinfo->ii_rwsem);
20259+ return !(iinfo->ii_hsb1 == h_inode->i_sb
20260+ && iinfo->ii_higen == h_inode->i_generation);
20261+}
1308ab2a 20262+
4a4d8108
AM
20263+static inline void au_iigen_dec(struct inode *inode)
20264+{
537831f9 20265+ struct au_iinfo *iinfo;
be52b249 20266+ struct au_iigen *iigen;
537831f9
AM
20267+
20268+ iinfo = au_ii(inode);
be52b249
AM
20269+ iigen = &iinfo->ii_generation;
20270+ spin_lock(&iigen->ig_spin);
20271+ iigen->ig_generation--;
20272+ spin_unlock(&iigen->ig_spin);
027c5e7a
AM
20273+}
20274+
20275+static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
20276+{
20277+ int err;
20278+
20279+ err = 0;
537831f9 20280+ if (unlikely(inode && au_iigen(inode, NULL) != sigen))
027c5e7a
AM
20281+ err = -EIO;
20282+
20283+ return err;
4a4d8108 20284+}
1308ab2a 20285+
4a4d8108 20286+/* ---------------------------------------------------------------------- */
1308ab2a 20287+
5afbbe0d
AM
20288+static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
20289+ aufs_bindex_t bindex)
20290+{
20291+ return iinfo->ii_hinode + bindex;
20292+}
20293+
20294+static inline int au_is_bad_inode(struct inode *inode)
20295+{
20296+ return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
20297+}
20298+
4a4d8108
AM
20299+static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
20300+ aufs_bindex_t bindex)
20301+{
20302+ IiMustAnyLock(inode);
5afbbe0d 20303+ return au_hinode(au_ii(inode), bindex)->hi_id;
4a4d8108 20304+}
1308ab2a 20305+
5afbbe0d 20306+static inline aufs_bindex_t au_ibtop(struct inode *inode)
4a4d8108
AM
20307+{
20308+ IiMustAnyLock(inode);
5afbbe0d 20309+ return au_ii(inode)->ii_btop;
4a4d8108 20310+}
1308ab2a 20311+
5afbbe0d 20312+static inline aufs_bindex_t au_ibbot(struct inode *inode)
4a4d8108
AM
20313+{
20314+ IiMustAnyLock(inode);
5afbbe0d 20315+ return au_ii(inode)->ii_bbot;
4a4d8108 20316+}
1308ab2a 20317+
4a4d8108
AM
20318+static inline struct au_vdir *au_ivdir(struct inode *inode)
20319+{
20320+ IiMustAnyLock(inode);
20321+ return au_ii(inode)->ii_vdir;
20322+}
1308ab2a 20323+
4a4d8108
AM
20324+static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
20325+{
20326+ IiMustAnyLock(inode);
5afbbe0d 20327+ return au_hinode(au_ii(inode), bindex)->hi_whdentry;
4a4d8108 20328+}
1308ab2a 20329+
5afbbe0d 20330+static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 20331+{
4a4d8108 20332+ IiMustWriteLock(inode);
5afbbe0d 20333+ au_ii(inode)->ii_btop = bindex;
4a4d8108 20334+}
1308ab2a 20335+
5afbbe0d 20336+static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
4a4d8108
AM
20337+{
20338+ IiMustWriteLock(inode);
5afbbe0d 20339+ au_ii(inode)->ii_bbot = bindex;
1308ab2a 20340+}
20341+
4a4d8108
AM
20342+static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
20343+{
20344+ IiMustWriteLock(inode);
20345+ au_ii(inode)->ii_vdir = vdir;
20346+}
1facf9fc 20347+
4a4d8108 20348+static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 20349+{
4a4d8108 20350+ IiMustAnyLock(inode);
5afbbe0d 20351+ return au_hinode(au_ii(inode), bindex);
4a4d8108 20352+}
dece6358 20353+
4a4d8108 20354+/* ---------------------------------------------------------------------- */
1facf9fc 20355+
4a4d8108
AM
20356+static inline struct dentry *au_pinned_parent(struct au_pin *pin)
20357+{
20358+ if (pin)
20359+ return pin->parent;
20360+ return NULL;
1facf9fc 20361+}
20362+
4a4d8108 20363+static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
1facf9fc 20364+{
4a4d8108
AM
20365+ if (pin && pin->hdir)
20366+ return pin->hdir->hi_inode;
20367+ return NULL;
1308ab2a 20368+}
1facf9fc 20369+
4a4d8108
AM
20370+static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
20371+{
20372+ if (pin)
20373+ return pin->hdir;
20374+ return NULL;
20375+}
1facf9fc 20376+
4a4d8108 20377+static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
1308ab2a 20378+{
4a4d8108
AM
20379+ if (pin)
20380+ pin->dentry = dentry;
20381+}
1308ab2a 20382+
4a4d8108
AM
20383+static inline void au_pin_set_parent_lflag(struct au_pin *pin,
20384+ unsigned char lflag)
20385+{
20386+ if (pin) {
7f207e10 20387+ if (lflag)
4a4d8108 20388+ au_fset_pin(pin->flags, DI_LOCKED);
7f207e10 20389+ else
4a4d8108 20390+ au_fclr_pin(pin->flags, DI_LOCKED);
1308ab2a 20391+ }
4a4d8108
AM
20392+}
20393+
7e9cd9fe 20394+#if 0 /* reserved */
4a4d8108
AM
20395+static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
20396+{
20397+ if (pin) {
20398+ dput(pin->parent);
20399+ pin->parent = dget(parent);
1facf9fc 20400+ }
4a4d8108 20401+}
7e9cd9fe 20402+#endif
1facf9fc 20403+
4a4d8108
AM
20404+/* ---------------------------------------------------------------------- */
20405+
027c5e7a 20406+struct au_branch;
4a4d8108
AM
20407+#ifdef CONFIG_AUFS_HNOTIFY
20408+struct au_hnotify_op {
20409+ void (*ctl)(struct au_hinode *hinode, int do_set);
027c5e7a 20410+ int (*alloc)(struct au_hinode *hinode);
7eafdf33
AM
20411+
20412+ /*
42a736d3 20413+ * if it returns true, the caller should free hinode->hi_notify,
7eafdf33
AM
20414+ * otherwise ->free() frees it.
20415+ */
20416+ int (*free)(struct au_hinode *hinode,
20417+ struct au_hnotify *hn) __must_check;
4a4d8108
AM
20418+
20419+ void (*fin)(void);
20420+ int (*init)(void);
027c5e7a
AM
20421+
20422+ int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
20423+ void (*fin_br)(struct au_branch *br);
20424+ int (*init_br)(struct au_branch *br, int perm);
4a4d8108
AM
20425+};
20426+
20427+/* hnotify.c */
027c5e7a 20428+int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
4a4d8108
AM
20429+void au_hn_free(struct au_hinode *hinode);
20430+void au_hn_ctl(struct au_hinode *hinode, int do_set);
20431+void au_hn_reset(struct inode *inode, unsigned int flags);
20432+int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
fbc438ed 20433+ const struct qstr *h_child_qstr, struct inode *h_child_inode);
027c5e7a
AM
20434+int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
20435+int au_hnotify_init_br(struct au_branch *br, int perm);
20436+void au_hnotify_fin_br(struct au_branch *br);
4a4d8108
AM
20437+int __init au_hnotify_init(void);
20438+void au_hnotify_fin(void);
20439+
7f207e10 20440+/* hfsnotify.c */
4a4d8108
AM
20441+extern const struct au_hnotify_op au_hnotify_op;
20442+
20443+static inline
20444+void au_hn_init(struct au_hinode *hinode)
20445+{
20446+ hinode->hi_notify = NULL;
1308ab2a 20447+}
20448+
53392da6
AM
20449+static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
20450+{
20451+ return hinode->hi_notify;
20452+}
20453+
4a4d8108 20454+#else
c1595e42
JR
20455+AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
20456+ struct au_hinode *hinode __maybe_unused,
20457+ struct inode *inode __maybe_unused)
20458+AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
4a4d8108
AM
20459+AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
20460+AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
20461+ int do_set __maybe_unused)
20462+AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
20463+ unsigned int flags __maybe_unused)
027c5e7a
AM
20464+AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
20465+ struct au_branch *br __maybe_unused,
20466+ int perm __maybe_unused)
20467+AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
20468+ int perm __maybe_unused)
20469+AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
4a4d8108
AM
20470+AuStubInt0(__init au_hnotify_init, void)
20471+AuStubVoid(au_hnotify_fin, void)
20472+AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
20473+#endif /* CONFIG_AUFS_HNOTIFY */
20474+
20475+static inline void au_hn_suspend(struct au_hinode *hdir)
20476+{
20477+ au_hn_ctl(hdir, /*do_set*/0);
1308ab2a 20478+}
20479+
4a4d8108 20480+static inline void au_hn_resume(struct au_hinode *hdir)
1308ab2a 20481+{
4a4d8108
AM
20482+ au_hn_ctl(hdir, /*do_set*/1);
20483+}
1308ab2a 20484+
5afbbe0d 20485+static inline void au_hn_inode_lock(struct au_hinode *hdir)
4a4d8108 20486+{
febd17d6 20487+ inode_lock(hdir->hi_inode);
4a4d8108
AM
20488+ au_hn_suspend(hdir);
20489+}
dece6358 20490+
5afbbe0d 20491+static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
4a4d8108
AM
20492+ unsigned int sc __maybe_unused)
20493+{
febd17d6 20494+ inode_lock_nested(hdir->hi_inode, sc);
4a4d8108 20495+ au_hn_suspend(hdir);
1facf9fc 20496+}
1facf9fc 20497+
8b6a4947
AM
20498+#if 0 /* unused */
20499+#include "vfsub.h"
3c1bdaff
AM
20500+static inline void au_hn_inode_lock_shared_nested(struct au_hinode *hdir,
20501+ unsigned int sc)
20502+{
be118d29 20503+ inode_lock_shared_nested(hdir->hi_inode, sc);
3c1bdaff
AM
20504+ au_hn_suspend(hdir);
20505+}
8b6a4947 20506+#endif
3c1bdaff 20507+
5afbbe0d 20508+static inline void au_hn_inode_unlock(struct au_hinode *hdir)
4a4d8108
AM
20509+{
20510+ au_hn_resume(hdir);
febd17d6 20511+ inode_unlock(hdir->hi_inode);
4a4d8108
AM
20512+}
20513+
20514+#endif /* __KERNEL__ */
20515+#endif /* __AUFS_INODE_H__ */
7f207e10 20516diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
eca34b5c 20517--- /usr/share/empty/fs/aufs/ioctl.c 1970-01-01 01:00:00.000000000 +0100
594d0238 20518+++ linux/fs/aufs/ioctl.c 2022-11-05 23:02:18.965889284 +0100
062440b3 20519@@ -0,0 +1,220 @@
cd7a4cd9 20520+// SPDX-License-Identifier: GPL-2.0
4a4d8108 20521+/*
0dcfbb52 20522+ * Copyright (C) 2005-2022 Junjiro R. Okajima
4a4d8108 20523+ *
0dcfbb52 20524+ * This program is free software; you can redistribute it and/or modify
4a4d8108
AM
20525+ * it under the terms of the GNU General Public License as published by
20526+ * the Free Software Foundation; either version 2 of the License, or
20527+ * (at your option) any later version.
20528+ *
20529+ * This program is distributed in the hope that it will be useful,
20530+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20531+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20532+ * GNU General Public License for more details.
20533+ *
20534+ * You should have received a copy of the GNU General Public License
523b37e3 20535+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
20536+ */
20537+
20538+/*
20539+ * ioctl
20540+ * plink-management and readdir in userspace.
20541+ * assist the pathconf(3) wrapper library.
c2b27bf2 20542+ * move-down
076b876e 20543+ * File-based Hierarchical Storage Management.
4a4d8108
AM
20544+ */
20545+
c2b27bf2
AM
20546+#include <linux/compat.h>
20547+#include <linux/file.h>
4a4d8108
AM
20548+#include "aufs.h"
20549+
1e00d052 20550+static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
4a4d8108
AM
20551+{
20552+ int err, fd;
5afbbe0d 20553+ aufs_bindex_t wbi, bindex, bbot;
4a4d8108
AM
20554+ struct file *h_file;
20555+ struct super_block *sb;
20556+ struct dentry *root;
1e00d052
AM
20557+ struct au_branch *br;
20558+ struct aufs_wbr_fd wbrfd = {
20559+ .oflags = au_dir_roflags,
20560+ .brid = -1
20561+ };
20562+ const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
20563+ | O_NOATIME | O_CLOEXEC;
4a4d8108 20564+
1e00d052
AM
20565+ AuDebugOn(wbrfd.oflags & ~valid);
20566+
20567+ if (arg) {
20568+ err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
20569+ if (unlikely(err)) {
20570+ err = -EFAULT;
20571+ goto out;
20572+ }
20573+
20574+ err = -EINVAL;
20575+ AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
20576+ wbrfd.oflags |= au_dir_roflags;
20577+ AuDbg("0%o\n", wbrfd.oflags);
20578+ if (unlikely(wbrfd.oflags & ~valid))
20579+ goto out;
20580+ }
20581+
2000de60 20582+ fd = get_unused_fd_flags(0);
1e00d052
AM
20583+ err = fd;
20584+ if (unlikely(fd < 0))
4a4d8108 20585+ goto out;
4a4d8108 20586+
1e00d052 20587+ h_file = ERR_PTR(-EINVAL);
4a4d8108 20588+ wbi = 0;
1e00d052 20589+ br = NULL;
4a4d8108
AM
20590+ sb = path->dentry->d_sb;
20591+ root = sb->s_root;
20592+ aufs_read_lock(root, AuLock_IR);
5afbbe0d 20593+ bbot = au_sbbot(sb);
1e00d052
AM
20594+ if (wbrfd.brid >= 0) {
20595+ wbi = au_br_index(sb, wbrfd.brid);
5afbbe0d 20596+ if (unlikely(wbi < 0 || wbi > bbot))
1e00d052
AM
20597+ goto out_unlock;
20598+ }
20599+
20600+ h_file = ERR_PTR(-ENOENT);
20601+ br = au_sbr(sb, wbi);
20602+ if (!au_br_writable(br->br_perm)) {
20603+ if (arg)
20604+ goto out_unlock;
20605+
20606+ bindex = wbi + 1;
20607+ wbi = -1;
5afbbe0d 20608+ for (; bindex <= bbot; bindex++) {
1e00d052
AM
20609+ br = au_sbr(sb, bindex);
20610+ if (au_br_writable(br->br_perm)) {
4a4d8108 20611+ wbi = bindex;
1e00d052 20612+ br = au_sbr(sb, wbi);
4a4d8108
AM
20613+ break;
20614+ }
20615+ }
4a4d8108
AM
20616+ }
20617+ AuDbg("wbi %d\n", wbi);
1e00d052 20618+ if (wbi >= 0)
392086de
AM
20619+ h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
20620+ /*force_wr*/0);
1e00d052
AM
20621+
20622+out_unlock:
4a4d8108
AM
20623+ aufs_read_unlock(root, AuLock_IR);
20624+ err = PTR_ERR(h_file);
20625+ if (IS_ERR(h_file))
20626+ goto out_fd;
20627+
acd2b654 20628+ au_lcnt_dec(&br->br_nfiles); /* cf. au_h_open() */
4a4d8108
AM
20629+ fd_install(fd, h_file);
20630+ err = fd;
20631+ goto out; /* success */
20632+
4f0767ce 20633+out_fd:
4a4d8108 20634+ put_unused_fd(fd);
4f0767ce 20635+out:
1e00d052 20636+ AuTraceErr(err);
4a4d8108
AM
20637+ return err;
20638+}
20639+
20640+/* ---------------------------------------------------------------------- */
20641+
20642+long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
20643+{
20644+ long err;
c1595e42 20645+ struct dentry *dentry;
4a4d8108
AM
20646+
20647+ switch (cmd) {
4a4d8108
AM
20648+ case AUFS_CTL_RDU:
20649+ case AUFS_CTL_RDU_INO:
20650+ err = au_rdu_ioctl(file, cmd, arg);
20651+ break;
20652+
20653+ case AUFS_CTL_WBR_FD:
1e00d052 20654+ err = au_wbr_fd(&file->f_path, (void __user *)arg);
4a4d8108
AM
20655+ break;
20656+
027c5e7a
AM
20657+ case AUFS_CTL_IBUSY:
20658+ err = au_ibusy_ioctl(file, arg);
20659+ break;
20660+
076b876e
AM
20661+ case AUFS_CTL_BRINFO:
20662+ err = au_brinfo_ioctl(file, arg);
20663+ break;
20664+
20665+ case AUFS_CTL_FHSM_FD:
2000de60 20666+ dentry = file->f_path.dentry;
c1595e42
JR
20667+ if (IS_ROOT(dentry))
20668+ err = au_fhsm_fd(dentry->d_sb, arg);
20669+ else
20670+ err = -ENOTTY;
076b876e
AM
20671+ break;
20672+
4a4d8108
AM
20673+ default:
20674+ /* do not call the lower */
20675+ AuDbg("0x%x\n", cmd);
20676+ err = -ENOTTY;
20677+ }
20678+
20679+ AuTraceErr(err);
20680+ return err;
20681+}
20682+
20683+long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
20684+{
20685+ long err;
20686+
20687+ switch (cmd) {
c2b27bf2 20688+ case AUFS_CTL_MVDOWN:
2000de60 20689+ err = au_mvdown(file->f_path.dentry, (void __user *)arg);
c2b27bf2
AM
20690+ break;
20691+
4a4d8108 20692+ case AUFS_CTL_WBR_FD:
1e00d052 20693+ err = au_wbr_fd(&file->f_path, (void __user *)arg);
4a4d8108
AM
20694+ break;
20695+
20696+ default:
20697+ /* do not call the lower */
20698+ AuDbg("0x%x\n", cmd);
20699+ err = -ENOTTY;
20700+ }
20701+
20702+ AuTraceErr(err);
20703+ return err;
20704+}
b752ccd1
AM
20705+
20706+#ifdef CONFIG_COMPAT
20707+long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
20708+ unsigned long arg)
20709+{
20710+ long err;
20711+
20712+ switch (cmd) {
20713+ case AUFS_CTL_RDU:
20714+ case AUFS_CTL_RDU_INO:
20715+ err = au_rdu_compat_ioctl(file, cmd, arg);
20716+ break;
20717+
027c5e7a
AM
20718+ case AUFS_CTL_IBUSY:
20719+ err = au_ibusy_compat_ioctl(file, arg);
20720+ break;
20721+
076b876e
AM
20722+ case AUFS_CTL_BRINFO:
20723+ err = au_brinfo_compat_ioctl(file, arg);
20724+ break;
20725+
b752ccd1
AM
20726+ default:
20727+ err = aufs_ioctl_dir(file, cmd, arg);
20728+ }
20729+
20730+ AuTraceErr(err);
20731+ return err;
20732+}
20733+
b752ccd1
AM
20734+long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
20735+ unsigned long arg)
20736+{
20737+ return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
20738+}
20739+#endif
7f207e10 20740diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
eca34b5c 20741--- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
631230db 20742+++ linux/fs/aufs/i_op_add.c 2023-10-31 09:31:04.199880750 +0100
2255d0fe 20743@@ -0,0 +1,972 @@
cd7a4cd9 20744+// SPDX-License-Identifier: GPL-2.0
4a4d8108 20745+/*
0dcfbb52 20746+ * Copyright (C) 2005-2022 Junjiro R. Okajima
4a4d8108 20747+ *
0dcfbb52 20748+ * This program is free software; you can redistribute it and/or modify
4a4d8108
AM
20749+ * it under the terms of the GNU General Public License as published by
20750+ * the Free Software Foundation; either version 2 of the License, or
20751+ * (at your option) any later version.
20752+ *
20753+ * This program is distributed in the hope that it will be useful,
20754+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20755+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20756+ * GNU General Public License for more details.
20757+ *
20758+ * You should have received a copy of the GNU General Public License
523b37e3 20759+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
20760+ */
20761+
20762+/*
20763+ * inode operations (add entry)
20764+ */
20765+
eca801bf 20766+#include <linux/iversion.h>
4a4d8108
AM
20767+#include "aufs.h"
20768+
20769+/*
20770+ * final procedure of adding a new entry, except link(2).
20771+ * remove whiteout, instantiate, copyup the parent dir's times and size
20772+ * and update version.
20773+ * if it failed, re-create the removed whiteout.
20774+ */
20775+static int epilog(struct inode *dir, aufs_bindex_t bindex,
20776+ struct dentry *wh_dentry, struct dentry *dentry)
20777+{
20778+ int err, rerr;
20779+ aufs_bindex_t bwh;
20780+ struct path h_path;
076b876e 20781+ struct super_block *sb;
4a4d8108
AM
20782+ struct inode *inode, *h_dir;
20783+ struct dentry *wh;
20784+
20785+ bwh = -1;
076b876e 20786+ sb = dir->i_sb;
4a4d8108 20787+ if (wh_dentry) {
5527c038 20788+ h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
4a4d8108
AM
20789+ IMustLock(h_dir);
20790+ AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
20791+ bwh = au_dbwh(dentry);
20792+ h_path.dentry = wh_dentry;
076b876e 20793+ h_path.mnt = au_sbr_mnt(sb, bindex);
4a4d8108
AM
20794+ err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
20795+ dentry);
20796+ if (unlikely(err))
20797+ goto out;
20798+ }
20799+
20800+ inode = au_new_inode(dentry, /*must_new*/1);
20801+ if (!IS_ERR(inode)) {
20802+ d_instantiate(dentry, inode);
5527c038 20803+ dir = d_inode(dentry->d_parent); /* dir inode is locked */
4a4d8108 20804+ IMustLock(dir);
b912730e 20805+ au_dir_ts(dir, bindex);
be118d29 20806+ inode_inc_iversion(dir);
076b876e 20807+ au_fhsm_wrote(sb, bindex, /*force*/0);
4a4d8108
AM
20808+ return 0; /* success */
20809+ }
20810+
20811+ err = PTR_ERR(inode);
20812+ if (!wh_dentry)
20813+ goto out;
20814+
20815+ /* revert */
20816+ /* dir inode is locked */
20817+ wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
20818+ rerr = PTR_ERR(wh);
20819+ if (IS_ERR(wh)) {
523b37e3
AM
20820+ AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
20821+ dentry, err, rerr);
4a4d8108
AM
20822+ err = -EIO;
20823+ } else
20824+ dput(wh);
20825+
4f0767ce 20826+out:
4a4d8108
AM
20827+ return err;
20828+}
20829+
027c5e7a
AM
20830+static int au_d_may_add(struct dentry *dentry)
20831+{
20832+ int err;
20833+
20834+ err = 0;
20835+ if (unlikely(d_unhashed(dentry)))
20836+ err = -ENOENT;
5527c038 20837+ if (unlikely(d_really_is_positive(dentry)))
027c5e7a
AM
20838+ err = -EEXIST;
20839+ return err;
20840+}
20841+
4a4d8108
AM
20842+/*
20843+ * simple tests for the adding inode operations.
20844+ * following the checks in vfs, plus the parent-child relationship.
20845+ */
20846+int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
20847+ struct dentry *h_parent, int isdir)
20848+{
20849+ int err;
20850+ umode_t h_mode;
20851+ struct dentry *h_dentry;
20852+ struct inode *h_inode;
20853+
20854+ err = -ENAMETOOLONG;
20855+ if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20856+ goto out;
20857+
20858+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 20859+ if (d_really_is_negative(dentry)) {
4a4d8108 20860+ err = -EEXIST;
5527c038 20861+ if (unlikely(d_is_positive(h_dentry)))
4a4d8108
AM
20862+ goto out;
20863+ } else {
20864+ /* rename(2) case */
20865+ err = -EIO;
5527c038
JR
20866+ if (unlikely(d_is_negative(h_dentry)))
20867+ goto out;
20868+ h_inode = d_inode(h_dentry);
20869+ if (unlikely(!h_inode->i_nlink))
4a4d8108
AM
20870+ goto out;
20871+
20872+ h_mode = h_inode->i_mode;
20873+ if (!isdir) {
20874+ err = -EISDIR;
20875+ if (unlikely(S_ISDIR(h_mode)))
20876+ goto out;
20877+ } else if (unlikely(!S_ISDIR(h_mode))) {
20878+ err = -ENOTDIR;
20879+ goto out;
20880+ }
20881+ }
20882+
20883+ err = 0;
20884+ /* expected parent dir is locked */
20885+ if (unlikely(h_parent != h_dentry->d_parent))
20886+ err = -EIO;
20887+
4f0767ce 20888+out:
4a4d8108
AM
20889+ AuTraceErr(err);
20890+ return err;
20891+}
20892+
20893+/*
20894+ * initial procedure of adding a new entry.
20895+ * prepare writable branch and the parent dir, lock it,
20896+ * and lookup whiteout for the new entry.
20897+ */
20898+static struct dentry*
20899+lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
20900+ struct dentry *src_dentry, struct au_pin *pin,
20901+ struct au_wr_dir_args *wr_dir_args)
20902+{
20903+ struct dentry *wh_dentry, *h_parent;
20904+ struct super_block *sb;
20905+ struct au_branch *br;
20906+ int err;
20907+ unsigned int udba;
20908+ aufs_bindex_t bcpup;
20909+
523b37e3 20910+ AuDbg("%pd\n", dentry);
4a4d8108
AM
20911+
20912+ err = au_wr_dir(dentry, src_dentry, wr_dir_args);
20913+ bcpup = err;
20914+ wh_dentry = ERR_PTR(err);
20915+ if (unlikely(err < 0))
20916+ goto out;
20917+
20918+ sb = dentry->d_sb;
20919+ udba = au_opt_udba(sb);
20920+ err = au_pin(pin, dentry, bcpup, udba,
20921+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20922+ wh_dentry = ERR_PTR(err);
20923+ if (unlikely(err))
20924+ goto out;
20925+
20926+ h_parent = au_pinned_h_parent(pin);
20927+ if (udba != AuOpt_UDBA_NONE
5afbbe0d 20928+ && au_dbtop(dentry) == bcpup)
4a4d8108
AM
20929+ err = au_may_add(dentry, bcpup, h_parent,
20930+ au_ftest_wrdir(wr_dir_args->flags, ISDIR));
20931+ else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20932+ err = -ENAMETOOLONG;
20933+ wh_dentry = ERR_PTR(err);
20934+ if (unlikely(err))
20935+ goto out_unpin;
20936+
20937+ br = au_sbr(sb, bcpup);
20938+ if (dt) {
20939+ struct path tmp = {
20940+ .dentry = h_parent,
86dc4139 20941+ .mnt = au_br_mnt(br)
4a4d8108
AM
20942+ };
20943+ au_dtime_store(dt, au_pinned_parent(pin), &tmp);
20944+ }
20945+
20946+ wh_dentry = NULL;
20947+ if (bcpup != au_dbwh(dentry))
20948+ goto out; /* success */
20949+
2000de60
JR
20950+ /*
20951+ * ENAMETOOLONG here means that if we allowed create such name, then it
20952+ * would not be able to removed in the future. So we don't allow such
20953+ * name here and we don't handle ENAMETOOLONG differently here.
20954+ */
4a4d8108
AM
20955+ wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
20956+
4f0767ce 20957+out_unpin:
4a4d8108
AM
20958+ if (IS_ERR(wh_dentry))
20959+ au_unpin(pin);
4f0767ce 20960+out:
4a4d8108
AM
20961+ return wh_dentry;
20962+}
20963+
20964+/* ---------------------------------------------------------------------- */
20965+
20966+enum { Mknod, Symlink, Creat };
20967+struct simple_arg {
20968+ int type;
20969+ union {
20970+ struct {
b912730e
AM
20971+ umode_t mode;
20972+ bool want_excl;
20973+ bool try_aopen;
20974+ struct vfsub_aopen_args *aopen;
4a4d8108
AM
20975+ } c;
20976+ struct {
20977+ const char *symname;
20978+ } s;
20979+ struct {
7eafdf33 20980+ umode_t mode;
4a4d8108
AM
20981+ dev_t dev;
20982+ } m;
20983+ } u;
20984+};
20985+
20986+static int add_simple(struct inode *dir, struct dentry *dentry,
20987+ struct simple_arg *arg)
20988+{
076b876e 20989+ int err, rerr;
5afbbe0d 20990+ aufs_bindex_t btop;
4a4d8108 20991+ unsigned char created;
b912730e
AM
20992+ const unsigned char try_aopen
20993+ = (arg->type == Creat && arg->u.c.try_aopen);
acd2b654 20994+ struct vfsub_aopen_args *aopen = arg->u.c.aopen;
4a4d8108
AM
20995+ struct dentry *wh_dentry, *parent;
20996+ struct inode *h_dir;
b912730e
AM
20997+ struct super_block *sb;
20998+ struct au_branch *br;
acd2b654 20999+ /* to reduce stack size */
c2b27bf2
AM
21000+ struct {
21001+ struct au_dtime dt;
21002+ struct au_pin pin;
21003+ struct path h_path;
21004+ struct au_wr_dir_args wr_dir_args;
21005+ } *a;
4a4d8108 21006+
523b37e3 21007+ AuDbg("%pd\n", dentry);
4a4d8108
AM
21008+ IMustLock(dir);
21009+
c2b27bf2
AM
21010+ err = -ENOMEM;
21011+ a = kmalloc(sizeof(*a), GFP_NOFS);
21012+ if (unlikely(!a))
21013+ goto out;
21014+ a->wr_dir_args.force_btgt = -1;
21015+ a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
21016+
4a4d8108 21017+ parent = dentry->d_parent; /* dir inode is locked */
b912730e
AM
21018+ if (!try_aopen) {
21019+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
21020+ if (unlikely(err))
21021+ goto out_free;
21022+ }
027c5e7a
AM
21023+ err = au_d_may_add(dentry);
21024+ if (unlikely(err))
21025+ goto out_unlock;
b912730e
AM
21026+ if (!try_aopen)
21027+ di_write_lock_parent(parent);
c2b27bf2
AM
21028+ wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
21029+ &a->pin, &a->wr_dir_args);
4a4d8108
AM
21030+ err = PTR_ERR(wh_dentry);
21031+ if (IS_ERR(wh_dentry))
027c5e7a 21032+ goto out_parent;
4a4d8108 21033+
5afbbe0d 21034+ btop = au_dbtop(dentry);
b912730e 21035+ sb = dentry->d_sb;
5afbbe0d
AM
21036+ br = au_sbr(sb, btop);
21037+ a->h_path.dentry = au_h_dptr(dentry, btop);
b912730e 21038+ a->h_path.mnt = au_br_mnt(br);
c2b27bf2 21039+ h_dir = au_pinned_h_dir(&a->pin);
4a4d8108
AM
21040+ switch (arg->type) {
21041+ case Creat:
acd2b654 21042+ if (!try_aopen || !h_dir->i_op->atomic_open) {
b912730e
AM
21043+ err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
21044+ arg->u.c.want_excl);
acd2b654
AM
21045+ created = !err;
21046+ if (!err && try_aopen)
21047+ aopen->file->f_mode |= FMODE_CREATED;
21048+ } else {
21049+ aopen->br = br;
21050+ err = vfsub_atomic_open(h_dir, a->h_path.dentry, aopen);
21051+ AuDbg("err %d\n", err);
21052+ AuDbgFile(aopen->file);
21053+ created = err >= 0
21054+ && !!(aopen->file->f_mode & FMODE_CREATED);
21055+ }
4a4d8108
AM
21056+ break;
21057+ case Symlink:
c2b27bf2 21058+ err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
acd2b654 21059+ created = !err;
4a4d8108
AM
21060+ break;
21061+ case Mknod:
c2b27bf2
AM
21062+ err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
21063+ arg->u.m.dev);
acd2b654 21064+ created = !err;
4a4d8108
AM
21065+ break;
21066+ default:
21067+ BUG();
21068+ }
acd2b654
AM
21069+ if (unlikely(err < 0))
21070+ goto out_unpin;
21071+
21072+ err = epilog(dir, btop, wh_dentry, dentry);
4a4d8108 21073+ if (!err)
acd2b654 21074+ goto out_unpin; /* success */
4a4d8108
AM
21075+
21076+ /* revert */
acd2b654 21077+ if (created /* && d_is_positive(a->h_path.dentry) */) {
523b37e3
AM
21078+ /* no delegation since it is just created */
21079+ rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
21080+ /*force*/0);
4a4d8108 21081+ if (rerr) {
523b37e3
AM
21082+ AuIOErr("%pd revert failure(%d, %d)\n",
21083+ dentry, err, rerr);
4a4d8108
AM
21084+ err = -EIO;
21085+ }
c2b27bf2 21086+ au_dtime_revert(&a->dt);
4a4d8108 21087+ }
acd2b654
AM
21088+ if (try_aopen && h_dir->i_op->atomic_open
21089+ && (aopen->file->f_mode & FMODE_OPENED))
21090+ /* aopen->file is still opened */
21091+ au_lcnt_dec(&aopen->br->br_nfiles);
4a4d8108 21092+
acd2b654 21093+out_unpin:
c2b27bf2 21094+ au_unpin(&a->pin);
4a4d8108 21095+ dput(wh_dentry);
027c5e7a 21096+out_parent:
b912730e
AM
21097+ if (!try_aopen)
21098+ di_write_unlock(parent);
027c5e7a 21099+out_unlock:
4a4d8108 21100+ if (unlikely(err)) {
5afbbe0d 21101+ au_update_dbtop(dentry);
4a4d8108
AM
21102+ d_drop(dentry);
21103+ }
b912730e
AM
21104+ if (!try_aopen)
21105+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2 21106+out_free:
9f237c51 21107+ au_kfree_rcu(a);
027c5e7a 21108+out:
4a4d8108
AM
21109+ return err;
21110+}
21111+
5db017eb 21112+int aufs_mknod(struct mnt_idmap *idmap, struct inode *dir,
0b2a12c6 21113+ struct dentry *dentry, umode_t mode, dev_t dev)
4a4d8108
AM
21114+{
21115+ struct simple_arg arg = {
21116+ .type = Mknod,
21117+ .u.m = {
21118+ .mode = mode,
21119+ .dev = dev
21120+ }
21121+ };
21122+ return add_simple(dir, dentry, &arg);
21123+}
21124+
5db017eb 21125+int aufs_symlink(struct mnt_idmap *idmap, struct inode *dir,
0b2a12c6 21126+ struct dentry *dentry, const char *symname)
4a4d8108
AM
21127+{
21128+ struct simple_arg arg = {
21129+ .type = Symlink,
21130+ .u.s.symname = symname
21131+ };
21132+ return add_simple(dir, dentry, &arg);
21133+}
21134+
5db017eb 21135+int aufs_create(struct mnt_idmap *idmap, struct inode *dir,
0b2a12c6 21136+ struct dentry *dentry, umode_t mode, bool want_excl)
4a4d8108
AM
21137+{
21138+ struct simple_arg arg = {
21139+ .type = Creat,
21140+ .u.c = {
b4510431
AM
21141+ .mode = mode,
21142+ .want_excl = want_excl
4a4d8108
AM
21143+ }
21144+ };
21145+ return add_simple(dir, dentry, &arg);
21146+}
21147+
b912730e
AM
21148+int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
21149+ struct vfsub_aopen_args *aopen_args)
21150+{
21151+ struct simple_arg arg = {
21152+ .type = Creat,
21153+ .u.c = {
21154+ .mode = aopen_args->create_mode,
21155+ .want_excl = aopen_args->open_flag & O_EXCL,
21156+ .try_aopen = true,
21157+ .aopen = aopen_args
21158+ }
21159+ };
21160+ return add_simple(dir, dentry, &arg);
21161+}
21162+
5db017eb 21163+int aufs_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
2255d0fe 21164+ struct file *file, umode_t mode)
38d290e6
JR
21165+{
21166+ int err;
21167+ aufs_bindex_t bindex;
2255d0fe 21168+ struct path h_ppath;
38d290e6 21169+ struct super_block *sb;
2255d0fe
AM
21170+ struct au_branch *br;
21171+ struct dentry *dentry, *parent, *h_parent, *h_dentry;
38d290e6
JR
21172+ struct inode *h_dir, *inode;
21173+ struct vfsmount *h_mnt;
5db017eb 21174+ struct mnt_idmap *h_idmap;
2255d0fe 21175+ struct file *h_file;
38d290e6
JR
21176+ struct au_wr_dir_args wr_dir_args = {
21177+ .force_btgt = -1,
21178+ .flags = AuWrDir_TMPFILE
21179+ };
21180+
21181+ /* copy-up may happen */
febd17d6 21182+ inode_lock(dir);
38d290e6 21183+
2255d0fe 21184+ h_file = NULL;
38d290e6
JR
21185+ sb = dir->i_sb;
21186+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21187+ if (unlikely(err))
21188+ goto out;
21189+
2255d0fe 21190+ dentry = file->f_path.dentry;
38d290e6
JR
21191+ err = au_di_init(dentry);
21192+ if (unlikely(err))
21193+ goto out_si;
21194+
21195+ err = -EBUSY;
21196+ parent = d_find_any_alias(dir);
21197+ AuDebugOn(!parent);
21198+ di_write_lock_parent(parent);
5527c038 21199+ if (unlikely(d_inode(parent) != dir))
38d290e6
JR
21200+ goto out_parent;
21201+
21202+ err = au_digen_test(parent, au_sigen(sb));
21203+ if (unlikely(err))
21204+ goto out_parent;
21205+
5afbbe0d
AM
21206+ bindex = au_dbtop(parent);
21207+ au_set_dbtop(dentry, bindex);
21208+ au_set_dbbot(dentry, bindex);
38d290e6
JR
21209+ err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
21210+ bindex = err;
21211+ if (unlikely(err < 0))
21212+ goto out_parent;
21213+
21214+ err = -EOPNOTSUPP;
21215+ h_dir = au_h_iptr(dir, bindex);
21216+ if (unlikely(!h_dir->i_op->tmpfile))
21217+ goto out_parent;
21218+
2255d0fe
AM
21219+ br = au_sbr(sb, bindex);
21220+ h_mnt = au_br_mnt(br);
38d290e6
JR
21221+ err = vfsub_mnt_want_write(h_mnt);
21222+ if (unlikely(err))
21223+ goto out_parent;
21224+
5db017eb 21225+ h_idmap = mnt_idmap(h_mnt);
38d290e6 21226+ h_parent = au_h_dptr(parent, bindex);
2255d0fe
AM
21227+ h_ppath.mnt = h_mnt;
21228+ h_ppath.dentry = h_parent;
60f9c79c
JR
21229+ h_file = kernel_tmpfile_open(h_idmap, &h_ppath, mode, /*open_flag*/0,
21230+ current_cred());
2255d0fe
AM
21231+ if (IS_ERR(h_file)) {
21232+ err = PTR_ERR(h_file);
21233+ h_file = NULL;
38d290e6 21234+ goto out_mnt;
521ced18 21235+ }
38d290e6 21236+
2255d0fe 21237+ h_dentry = h_file->f_path.dentry;
5afbbe0d
AM
21238+ au_set_dbtop(dentry, bindex);
21239+ au_set_dbbot(dentry, bindex);
38d290e6
JR
21240+ au_set_h_dptr(dentry, bindex, dget(h_dentry));
21241+ inode = au_new_inode(dentry, /*must_new*/1);
21242+ if (IS_ERR(inode)) {
21243+ err = PTR_ERR(inode);
21244+ au_set_h_dptr(dentry, bindex, NULL);
5afbbe0d
AM
21245+ au_set_dbtop(dentry, -1);
21246+ au_set_dbbot(dentry, -1);
2255d0fe 21247+ goto out_h_file;
38d290e6 21248+ }
521ced18 21249+
2255d0fe
AM
21250+ if (!inode->i_nlink)
21251+ set_nlink(inode, 1);
21252+ d_tmpfile(file, inode);
21253+ au_di(dentry)->di_tmpfile = 1;
21254+ get_file(h_file);
21255+ au_di(dentry)->di_htmpfile = h_file;
21256+
21257+ /* update without i_mutex */
21258+ if (au_ibtop(dir) == au_dbtop(dentry))
21259+ au_cpup_attr_timesizes(dir);
21260+
21261+out_h_file:
21262+ fput(h_file);
38d290e6
JR
21263+out_mnt:
21264+ vfsub_mnt_drop_write(h_mnt);
21265+out_parent:
21266+ di_write_unlock(parent);
21267+ dput(parent);
21268+ di_write_unlock(dentry);
2255d0fe
AM
21269+ if (!err)
21270+ goto out_si;
21271+ if (h_file)
21272+ fput(h_file);
21273+ au_di(dentry)->di_htmpfile = NULL;
21274+ au_di_fin(dentry);
21275+ dentry->d_fsdata = NULL;
38d290e6
JR
21276+out_si:
21277+ si_read_unlock(sb);
2255d0fe
AM
21278+ if (!err && h_file) {
21279+ /* finally... */
21280+ err = finish_open_simple(file, err);
21281+ if (!err)
21282+ au_lcnt_inc(&br->br_nfiles);
21283+ else {
21284+ fput(h_file);
21285+ au_di(dentry)->di_htmpfile = NULL;
21286+ au_di_fin(dentry);
21287+ dentry->d_fsdata = NULL;
21288+ }
21289+ }
38d290e6 21290+out:
febd17d6 21291+ inode_unlock(dir);
2255d0fe 21292+ AuTraceErr(err);
38d290e6
JR
21293+ return err;
21294+}
21295+
4a4d8108
AM
21296+/* ---------------------------------------------------------------------- */
21297+
21298+struct au_link_args {
21299+ aufs_bindex_t bdst, bsrc;
21300+ struct au_pin pin;
21301+ struct path h_path;
21302+ struct dentry *src_parent, *parent;
21303+};
21304+
21305+static int au_cpup_before_link(struct dentry *src_dentry,
21306+ struct au_link_args *a)
21307+{
21308+ int err;
21309+ struct dentry *h_src_dentry;
c2b27bf2
AM
21310+ struct au_cp_generic cpg = {
21311+ .dentry = src_dentry,
21312+ .bdst = a->bdst,
21313+ .bsrc = a->bsrc,
21314+ .len = -1,
21315+ .pin = &a->pin,
21316+ .flags = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
21317+ };
4a4d8108
AM
21318+
21319+ di_read_lock_parent(a->src_parent, AuLock_IR);
21320+ err = au_test_and_cpup_dirs(src_dentry, a->bdst);
21321+ if (unlikely(err))
21322+ goto out;
21323+
21324+ h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
4a4d8108
AM
21325+ err = au_pin(&a->pin, src_dentry, a->bdst,
21326+ au_opt_udba(src_dentry->d_sb),
21327+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
21328+ if (unlikely(err))
21329+ goto out;
367653fa 21330+
c2b27bf2 21331+ err = au_sio_cpup_simple(&cpg);
4a4d8108
AM
21332+ au_unpin(&a->pin);
21333+
4f0767ce 21334+out:
4a4d8108
AM
21335+ di_read_unlock(a->src_parent, AuLock_IR);
21336+ return err;
21337+}
21338+
86dc4139
AM
21339+static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
21340+ struct au_link_args *a)
4a4d8108
AM
21341+{
21342+ int err;
21343+ unsigned char plink;
5afbbe0d 21344+ aufs_bindex_t bbot;
4a4d8108 21345+ struct dentry *h_src_dentry;
523b37e3 21346+ struct inode *h_inode, *inode, *delegated;
4a4d8108
AM
21347+ struct super_block *sb;
21348+ struct file *h_file;
21349+
21350+ plink = 0;
21351+ h_inode = NULL;
21352+ sb = src_dentry->d_sb;
5527c038 21353+ inode = d_inode(src_dentry);
5afbbe0d 21354+ if (au_ibtop(inode) <= a->bdst)
4a4d8108
AM
21355+ h_inode = au_h_iptr(inode, a->bdst);
21356+ if (!h_inode || !h_inode->i_nlink) {
21357+ /* copyup src_dentry as the name of dentry. */
5afbbe0d
AM
21358+ bbot = au_dbbot(dentry);
21359+ if (bbot < a->bsrc)
21360+ au_set_dbbot(dentry, a->bsrc);
86dc4139
AM
21361+ au_set_h_dptr(dentry, a->bsrc,
21362+ dget(au_h_dptr(src_dentry, a->bsrc)));
21363+ dget(a->h_path.dentry);
21364+ au_set_h_dptr(dentry, a->bdst, NULL);
c1595e42
JR
21365+ AuDbg("temporary d_inode...\n");
21366+ spin_lock(&dentry->d_lock);
5527c038 21367+ dentry->d_inode = d_inode(src_dentry); /* tmp */
c1595e42 21368+ spin_unlock(&dentry->d_lock);
392086de 21369+ h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
86dc4139 21370+ if (IS_ERR(h_file))
4a4d8108 21371+ err = PTR_ERR(h_file);
86dc4139 21372+ else {
c2b27bf2
AM
21373+ struct au_cp_generic cpg = {
21374+ .dentry = dentry,
21375+ .bdst = a->bdst,
21376+ .bsrc = -1,
21377+ .len = -1,
21378+ .pin = &a->pin,
21379+ .flags = AuCpup_KEEPLINO
21380+ };
21381+ err = au_sio_cpup_simple(&cpg);
86dc4139
AM
21382+ au_h_open_post(dentry, a->bsrc, h_file);
21383+ if (!err) {
21384+ dput(a->h_path.dentry);
21385+ a->h_path.dentry = au_h_dptr(dentry, a->bdst);
21386+ } else
21387+ au_set_h_dptr(dentry, a->bdst,
21388+ a->h_path.dentry);
21389+ }
c1595e42 21390+ spin_lock(&dentry->d_lock);
86dc4139 21391+ dentry->d_inode = NULL; /* restore */
c1595e42
JR
21392+ spin_unlock(&dentry->d_lock);
21393+ AuDbg("temporary d_inode...done\n");
86dc4139 21394+ au_set_h_dptr(dentry, a->bsrc, NULL);
5afbbe0d 21395+ au_set_dbbot(dentry, bbot);
4a4d8108
AM
21396+ } else {
21397+ /* the inode of src_dentry already exists on a.bdst branch */
21398+ h_src_dentry = d_find_alias(h_inode);
21399+ if (!h_src_dentry && au_plink_test(inode)) {
21400+ plink = 1;
21401+ h_src_dentry = au_plink_lkup(inode, a->bdst);
21402+ err = PTR_ERR(h_src_dentry);
21403+ if (IS_ERR(h_src_dentry))
21404+ goto out;
21405+
5527c038 21406+ if (unlikely(d_is_negative(h_src_dentry))) {
4a4d8108
AM
21407+ dput(h_src_dentry);
21408+ h_src_dentry = NULL;
21409+ }
21410+
21411+ }
21412+ if (h_src_dentry) {
523b37e3 21413+ delegated = NULL;
4a4d8108 21414+ err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
523b37e3
AM
21415+ &a->h_path, &delegated);
21416+ if (unlikely(err == -EWOULDBLOCK)) {
21417+ pr_warn("cannot retry for NFSv4 delegation"
21418+ " for an internal link\n");
21419+ iput(delegated);
21420+ }
4a4d8108
AM
21421+ dput(h_src_dentry);
21422+ } else {
21423+ AuIOErr("no dentry found for hi%lu on b%d\n",
21424+ h_inode->i_ino, a->bdst);
21425+ err = -EIO;
21426+ }
21427+ }
21428+
21429+ if (!err && !plink)
21430+ au_plink_append(inode, a->bdst, a->h_path.dentry);
21431+
21432+out:
2cbb1c4b 21433+ AuTraceErr(err);
4a4d8108
AM
21434+ return err;
21435+}
21436+
21437+int aufs_link(struct dentry *src_dentry, struct inode *dir,
21438+ struct dentry *dentry)
21439+{
21440+ int err, rerr;
21441+ struct au_dtime dt;
21442+ struct au_link_args *a;
21443+ struct dentry *wh_dentry, *h_src_dentry;
523b37e3 21444+ struct inode *inode, *delegated;
4a4d8108
AM
21445+ struct super_block *sb;
21446+ struct au_wr_dir_args wr_dir_args = {
21447+ /* .force_btgt = -1, */
21448+ .flags = AuWrDir_ADD_ENTRY
21449+ };
21450+
21451+ IMustLock(dir);
5527c038 21452+ inode = d_inode(src_dentry);
4a4d8108
AM
21453+ IMustLock(inode);
21454+
4a4d8108
AM
21455+ err = -ENOMEM;
21456+ a = kzalloc(sizeof(*a), GFP_NOFS);
21457+ if (unlikely(!a))
21458+ goto out;
21459+
21460+ a->parent = dentry->d_parent; /* dir inode is locked */
027c5e7a
AM
21461+ err = aufs_read_and_write_lock2(dentry, src_dentry,
21462+ AuLock_NOPLM | AuLock_GEN);
e49829fe
JR
21463+ if (unlikely(err))
21464+ goto out_kfree;
38d290e6 21465+ err = au_d_linkable(src_dentry);
027c5e7a
AM
21466+ if (unlikely(err))
21467+ goto out_unlock;
21468+ err = au_d_may_add(dentry);
21469+ if (unlikely(err))
21470+ goto out_unlock;
e49829fe 21471+
4a4d8108 21472+ a->src_parent = dget_parent(src_dentry);
5afbbe0d 21473+ wr_dir_args.force_btgt = au_ibtop(inode);
4a4d8108
AM
21474+
21475+ di_write_lock_parent(a->parent);
21476+ wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
21477+ wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
21478+ &wr_dir_args);
21479+ err = PTR_ERR(wh_dentry);
21480+ if (IS_ERR(wh_dentry))
027c5e7a 21481+ goto out_parent;
4a4d8108
AM
21482+
21483+ err = 0;
21484+ sb = dentry->d_sb;
5afbbe0d 21485+ a->bdst = au_dbtop(dentry);
4a4d8108
AM
21486+ a->h_path.dentry = au_h_dptr(dentry, a->bdst);
21487+ a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
5afbbe0d 21488+ a->bsrc = au_ibtop(inode);
2cbb1c4b 21489+ h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
38d290e6
JR
21490+ if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
21491+ h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
2cbb1c4b 21492+ if (!h_src_dentry) {
5afbbe0d 21493+ a->bsrc = au_dbtop(src_dentry);
2cbb1c4b
JR
21494+ h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
21495+ AuDebugOn(!h_src_dentry);
38d290e6
JR
21496+ } else if (IS_ERR(h_src_dentry)) {
21497+ err = PTR_ERR(h_src_dentry);
2cbb1c4b 21498+ goto out_parent;
38d290e6 21499+ }
2cbb1c4b 21500+
f2c43d5f
AM
21501+ /*
21502+ * aufs doesn't touch the credential so
acd2b654 21503+ * security_dentry_create_files_as() is unnecessary.
f2c43d5f 21504+ */
4a4d8108
AM
21505+ if (au_opt_test(au_mntflags(sb), PLINK)) {
21506+ if (a->bdst < a->bsrc
21507+ /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
86dc4139 21508+ err = au_cpup_or_link(src_dentry, dentry, a);
523b37e3
AM
21509+ else {
21510+ delegated = NULL;
4a4d8108 21511+ err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
523b37e3
AM
21512+ &a->h_path, &delegated);
21513+ if (unlikely(err == -EWOULDBLOCK)) {
21514+ pr_warn("cannot retry for NFSv4 delegation"
21515+ " for an internal link\n");
21516+ iput(delegated);
21517+ }
21518+ }
2cbb1c4b 21519+ dput(h_src_dentry);
4a4d8108
AM
21520+ } else {
21521+ /*
21522+ * copyup src_dentry to the branch we process,
21523+ * and then link(2) to it.
21524+ */
2cbb1c4b 21525+ dput(h_src_dentry);
4a4d8108
AM
21526+ if (a->bdst < a->bsrc
21527+ /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
21528+ au_unpin(&a->pin);
21529+ di_write_unlock(a->parent);
21530+ err = au_cpup_before_link(src_dentry, a);
21531+ di_write_lock_parent(a->parent);
21532+ if (!err)
21533+ err = au_pin(&a->pin, dentry, a->bdst,
21534+ au_opt_udba(sb),
21535+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
21536+ if (unlikely(err))
21537+ goto out_wh;
21538+ }
21539+ if (!err) {
21540+ h_src_dentry = au_h_dptr(src_dentry, a->bdst);
21541+ err = -ENOENT;
5527c038 21542+ if (h_src_dentry && d_is_positive(h_src_dentry)) {
523b37e3 21543+ delegated = NULL;
4a4d8108
AM
21544+ err = vfsub_link(h_src_dentry,
21545+ au_pinned_h_dir(&a->pin),
523b37e3
AM
21546+ &a->h_path, &delegated);
21547+ if (unlikely(err == -EWOULDBLOCK)) {
21548+ pr_warn("cannot retry"
21549+ " for NFSv4 delegation"
21550+ " for an internal link\n");
21551+ iput(delegated);
21552+ }
21553+ }
4a4d8108
AM
21554+ }
21555+ }
21556+ if (unlikely(err))
21557+ goto out_unpin;
21558+
21559+ if (wh_dentry) {
21560+ a->h_path.dentry = wh_dentry;
21561+ err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
21562+ dentry);
21563+ if (unlikely(err))
21564+ goto out_revert;
21565+ }
21566+
b912730e 21567+ au_dir_ts(dir, a->bdst);
be118d29 21568+ inode_inc_iversion(dir);
4a4d8108 21569+ inc_nlink(inode);
631230db 21570+ inode_set_ctime_to_ts(inode, inode_get_ctime(dir));
027c5e7a
AM
21571+ d_instantiate(dentry, au_igrab(inode));
21572+ if (d_unhashed(a->h_path.dentry))
4a4d8108
AM
21573+ /* some filesystem calls d_drop() */
21574+ d_drop(dentry);
076b876e
AM
21575+ /* some filesystems consume an inode even hardlink */
21576+ au_fhsm_wrote(sb, a->bdst, /*force*/0);
4a4d8108
AM
21577+ goto out_unpin; /* success */
21578+
4f0767ce 21579+out_revert:
523b37e3
AM
21580+ /* no delegation since it is just created */
21581+ rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
21582+ /*delegated*/NULL, /*force*/0);
027c5e7a 21583+ if (unlikely(rerr)) {
523b37e3 21584+ AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
027c5e7a
AM
21585+ err = -EIO;
21586+ }
4a4d8108 21587+ au_dtime_revert(&dt);
4f0767ce 21588+out_unpin:
4a4d8108 21589+ au_unpin(&a->pin);
4f0767ce 21590+out_wh:
4a4d8108 21591+ dput(wh_dentry);
027c5e7a
AM
21592+out_parent:
21593+ di_write_unlock(a->parent);
21594+ dput(a->src_parent);
4f0767ce 21595+out_unlock:
4a4d8108 21596+ if (unlikely(err)) {
5afbbe0d 21597+ au_update_dbtop(dentry);
4a4d8108
AM
21598+ d_drop(dentry);
21599+ }
4a4d8108 21600+ aufs_read_and_write_unlock2(dentry, src_dentry);
e49829fe 21601+out_kfree:
9f237c51 21602+ au_kfree_rcu(a);
4f0767ce 21603+out:
86dc4139 21604+ AuTraceErr(err);
4a4d8108
AM
21605+ return err;
21606+}
21607+
5db017eb 21608+int aufs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
0b2a12c6 21609+ struct dentry *dentry, umode_t mode)
4a4d8108
AM
21610+{
21611+ int err, rerr;
21612+ aufs_bindex_t bindex;
21613+ unsigned char diropq;
21614+ struct path h_path;
21615+ struct dentry *wh_dentry, *parent, *opq_dentry;
febd17d6 21616+ struct inode *h_inode;
4a4d8108
AM
21617+ struct super_block *sb;
21618+ struct {
21619+ struct au_pin pin;
21620+ struct au_dtime dt;
21621+ } *a; /* reduce the stack usage */
21622+ struct au_wr_dir_args wr_dir_args = {
21623+ .force_btgt = -1,
21624+ .flags = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
21625+ };
21626+
21627+ IMustLock(dir);
21628+
21629+ err = -ENOMEM;
21630+ a = kmalloc(sizeof(*a), GFP_NOFS);
21631+ if (unlikely(!a))
21632+ goto out;
21633+
027c5e7a
AM
21634+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
21635+ if (unlikely(err))
21636+ goto out_free;
21637+ err = au_d_may_add(dentry);
21638+ if (unlikely(err))
21639+ goto out_unlock;
21640+
4a4d8108
AM
21641+ parent = dentry->d_parent; /* dir inode is locked */
21642+ di_write_lock_parent(parent);
21643+ wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
21644+ &a->pin, &wr_dir_args);
21645+ err = PTR_ERR(wh_dentry);
21646+ if (IS_ERR(wh_dentry))
027c5e7a 21647+ goto out_parent;
4a4d8108
AM
21648+
21649+ sb = dentry->d_sb;
5afbbe0d 21650+ bindex = au_dbtop(dentry);
4a4d8108
AM
21651+ h_path.dentry = au_h_dptr(dentry, bindex);
21652+ h_path.mnt = au_sbr_mnt(sb, bindex);
21653+ err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
21654+ if (unlikely(err))
027c5e7a 21655+ goto out_unpin;
4a4d8108
AM
21656+
21657+ /* make the dir opaque */
21658+ diropq = 0;
febd17d6 21659+ h_inode = d_inode(h_path.dentry);
4a4d8108
AM
21660+ if (wh_dentry
21661+ || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
febd17d6 21662+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 21663+ opq_dentry = au_diropq_create(dentry, bindex);
febd17d6 21664+ inode_unlock(h_inode);
4a4d8108
AM
21665+ err = PTR_ERR(opq_dentry);
21666+ if (IS_ERR(opq_dentry))
21667+ goto out_dir;
21668+ dput(opq_dentry);
21669+ diropq = 1;
21670+ }
21671+
21672+ err = epilog(dir, bindex, wh_dentry, dentry);
21673+ if (!err) {
21674+ inc_nlink(dir);
027c5e7a 21675+ goto out_unpin; /* success */
4a4d8108
AM
21676+ }
21677+
21678+ /* revert */
21679+ if (diropq) {
21680+ AuLabel(revert opq);
febd17d6 21681+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 21682+ rerr = au_diropq_remove(dentry, bindex);
febd17d6 21683+ inode_unlock(h_inode);
4a4d8108 21684+ if (rerr) {
523b37e3
AM
21685+ AuIOErr("%pd reverting diropq failed(%d, %d)\n",
21686+ dentry, err, rerr);
4a4d8108
AM
21687+ err = -EIO;
21688+ }
21689+ }
21690+
4f0767ce 21691+out_dir:
4a4d8108
AM
21692+ AuLabel(revert dir);
21693+ rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
21694+ if (rerr) {
523b37e3
AM
21695+ AuIOErr("%pd reverting dir failed(%d, %d)\n",
21696+ dentry, err, rerr);
4a4d8108
AM
21697+ err = -EIO;
21698+ }
4a4d8108 21699+ au_dtime_revert(&a->dt);
027c5e7a 21700+out_unpin:
4a4d8108
AM
21701+ au_unpin(&a->pin);
21702+ dput(wh_dentry);
027c5e7a
AM
21703+out_parent:
21704+ di_write_unlock(parent);
21705+out_unlock:
4a4d8108 21706+ if (unlikely(err)) {
5afbbe0d 21707+ au_update_dbtop(dentry);
4a4d8108
AM
21708+ d_drop(dentry);
21709+ }
4a4d8108 21710+ aufs_read_unlock(dentry, AuLock_DW);
027c5e7a 21711+out_free:
9f237c51 21712+ au_kfree_rcu(a);
4f0767ce 21713+out:
4a4d8108
AM
21714+ return err;
21715+}
7f207e10 21716diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
eca34b5c 21717--- /usr/share/empty/fs/aufs/i_op.c 1970-01-01 01:00:00.000000000 +0100
631230db
AM
21718+++ linux/fs/aufs/i_op.c 2023-10-31 09:31:04.199880750 +0100
21719@@ -0,0 +1,1516 @@
cd7a4cd9 21720+// SPDX-License-Identifier: GPL-2.0
4a4d8108 21721+/*
0dcfbb52 21722+ * Copyright (C) 2005-2022 Junjiro R. Okajima
4a4d8108 21723+ *
0dcfbb52 21724+ * This program is free software; you can redistribute it and/or modify
4a4d8108
AM
21725+ * it under the terms of the GNU General Public License as published by
21726+ * the Free Software Foundation; either version 2 of the License, or
21727+ * (at your option) any later version.
21728+ *
21729+ * This program is distributed in the hope that it will be useful,
21730+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
21731+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21732+ * GNU General Public License for more details.
21733+ *
21734+ * You should have received a copy of the GNU General Public License
523b37e3 21735+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 21736+ */
1facf9fc 21737+
1308ab2a 21738+/*
4a4d8108 21739+ * inode operations (except add/del/rename)
1308ab2a 21740+ */
4a4d8108
AM
21741+
21742+#include <linux/device_cgroup.h>
5db017eb 21743+#include <linux/filelock.h>
4a4d8108 21744+#include <linux/fs_stack.h>
eca801bf 21745+#include <linux/iversion.h>
4a4d8108 21746+#include <linux/security.h>
4a4d8108
AM
21747+#include "aufs.h"
21748+
1e00d052 21749+static int h_permission(struct inode *h_inode, int mask,
79b8bda9 21750+ struct path *h_path, int brperm)
1facf9fc 21751+{
1308ab2a 21752+ int err;
4a4d8108 21753+ const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
5db017eb 21754+ struct mnt_idmap *h_idmap;
1facf9fc 21755+
e2f27e51
AM
21756+ err = -EPERM;
21757+ if (write_mask && IS_IMMUTABLE(h_inode))
21758+ goto out;
21759+
4a4d8108 21760+ err = -EACCES;
e2f27e51
AM
21761+ if (((mask & MAY_EXEC)
21762+ && S_ISREG(h_inode->i_mode)
21763+ && (path_noexec(h_path)
cd7a4cd9 21764+ || !(h_inode->i_mode & 0111))))
4a4d8108
AM
21765+ goto out;
21766+
21767+ /*
21768+ * - skip the lower fs test in the case of write to ro branch.
21769+ * - nfs dir permission write check is optimized, but a policy for
21770+ * link/rename requires a real check.
2121bcd9 21771+ * - nfs always sets SB_POSIXACL regardless its mount option 'noacl.'
b912730e 21772+ * in this case, generic_permission() returns -EOPNOTSUPP.
4a4d8108 21773+ */
5db017eb 21774+ h_idmap = mnt_idmap(h_path->mnt);
4a4d8108
AM
21775+ if ((write_mask && !au_br_writable(brperm))
21776+ || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
21777+ && write_mask && !(mask & MAY_READ))
21778+ || !h_inode->i_op->permission) {
21779+ /* AuLabel(generic_permission); */
aec24f17
AM
21780+ /* AuDbg("get_inode_acl %ps\n",
21781+ h_inode->i_op->get_inode_acl); */
5db017eb 21782+ err = generic_permission(h_idmap, h_inode, mask);
b912730e 21783+ if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
5db017eb 21784+ err = h_inode->i_op->permission(h_idmap, h_inode,
0b2a12c6 21785+ mask);
b912730e 21786+ AuTraceErr(err);
1308ab2a 21787+ } else {
4a4d8108 21788+ /* AuLabel(h_inode->permission); */
5db017eb 21789+ err = h_inode->i_op->permission(h_idmap, h_inode, mask);
4a4d8108
AM
21790+ AuTraceErr(err);
21791+ }
1facf9fc 21792+
4a4d8108
AM
21793+ if (!err)
21794+ err = devcgroup_inode_permission(h_inode, mask);
7f207e10 21795+ if (!err)
4a4d8108 21796+ err = security_inode_permission(h_inode, mask);
4a4d8108 21797+
4f0767ce 21798+out:
1308ab2a 21799+ return err;
21800+}
dece6358 21801+
5db017eb 21802+static int aufs_permission(struct mnt_idmap *idmap, struct inode *inode,
0b2a12c6 21803+ int mask)
1308ab2a 21804+{
21805+ int err;
5afbbe0d 21806+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
21807+ const unsigned char isdir = !!S_ISDIR(inode->i_mode),
21808+ write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
21809+ struct inode *h_inode;
21810+ struct super_block *sb;
21811+ struct au_branch *br;
1facf9fc 21812+
027c5e7a 21813+ /* todo: support rcu-walk? */
1e00d052 21814+ if (mask & MAY_NOT_BLOCK)
027c5e7a
AM
21815+ return -ECHILD;
21816+
4a4d8108
AM
21817+ sb = inode->i_sb;
21818+ si_read_lock(sb, AuLock_FLUSH);
21819+ ii_read_lock_child(inode);
43982f53
AM
21820+#if 0 /* reserved for future use */
21821+ /*
21822+ * This test may be rather 'too much' since the test is essentially done
21823+ * in the aufs_lookup(). Theoretically it is possible that the inode
21824+ * generation doesn't match to the superblock's here. But it isn't a
21825+ * big deal I suppose.
21826+ */
027c5e7a
AM
21827+ err = au_iigen_test(inode, au_sigen(sb));
21828+ if (unlikely(err))
21829+ goto out;
21830+#endif
dece6358 21831+
076b876e
AM
21832+ if (!isdir
21833+ || write_mask
21834+ || au_opt_test(au_mntflags(sb), DIRPERM1)) {
4a4d8108 21835+ err = au_busy_or_stale();
5afbbe0d 21836+ h_inode = au_h_iptr(inode, au_ibtop(inode));
4a4d8108
AM
21837+ if (unlikely(!h_inode
21838+ || (h_inode->i_mode & S_IFMT)
21839+ != (inode->i_mode & S_IFMT)))
21840+ goto out;
1facf9fc 21841+
4a4d8108 21842+ err = 0;
5afbbe0d 21843+ bindex = au_ibtop(inode);
4a4d8108 21844+ br = au_sbr(sb, bindex);
79b8bda9 21845+ err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
4a4d8108
AM
21846+ if (write_mask
21847+ && !err
21848+ && !special_file(h_inode->i_mode)) {
21849+ /* test whether the upper writable branch exists */
21850+ err = -EROFS;
21851+ for (; bindex >= 0; bindex--)
21852+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
21853+ err = 0;
21854+ break;
21855+ }
21856+ }
21857+ goto out;
21858+ }
dece6358 21859+
4a4d8108 21860+ /* non-write to dir */
1308ab2a 21861+ err = 0;
5afbbe0d
AM
21862+ bbot = au_ibbot(inode);
21863+ for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
4a4d8108
AM
21864+ h_inode = au_h_iptr(inode, bindex);
21865+ if (h_inode) {
21866+ err = au_busy_or_stale();
21867+ if (unlikely(!S_ISDIR(h_inode->i_mode)))
21868+ break;
21869+
21870+ br = au_sbr(sb, bindex);
79b8bda9 21871+ err = h_permission(h_inode, mask, &br->br_path,
4a4d8108
AM
21872+ br->br_perm);
21873+ }
21874+ }
1308ab2a 21875+
4f0767ce 21876+out:
4a4d8108
AM
21877+ ii_read_unlock(inode);
21878+ si_read_unlock(sb);
1308ab2a 21879+ return err;
21880+}
21881+
4a4d8108 21882+/* ---------------------------------------------------------------------- */
1facf9fc 21883+
4a4d8108 21884+static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
b4510431 21885+ unsigned int flags)
4a4d8108
AM
21886+{
21887+ struct dentry *ret, *parent;
b752ccd1 21888+ struct inode *inode;
4a4d8108 21889+ struct super_block *sb;
1716fcea 21890+ int err, npositive;
dece6358 21891+
4a4d8108 21892+ IMustLock(dir);
1308ab2a 21893+
537831f9
AM
21894+ /* todo: support rcu-walk? */
21895+ ret = ERR_PTR(-ECHILD);
21896+ if (flags & LOOKUP_RCU)
21897+ goto out;
21898+
21899+ ret = ERR_PTR(-ENAMETOOLONG);
21900+ if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
21901+ goto out;
21902+
4a4d8108 21903+ sb = dir->i_sb;
7f207e10
AM
21904+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21905+ ret = ERR_PTR(err);
21906+ if (unlikely(err))
21907+ goto out;
21908+
4a4d8108
AM
21909+ err = au_di_init(dentry);
21910+ ret = ERR_PTR(err);
21911+ if (unlikely(err))
7f207e10 21912+ goto out_si;
1308ab2a 21913+
9dbd164d 21914+ inode = NULL;
027c5e7a 21915+ npositive = 0; /* suppress a warning */
4a4d8108
AM
21916+ parent = dentry->d_parent; /* dir inode is locked */
21917+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a
AM
21918+ err = au_alive_dir(parent);
21919+ if (!err)
21920+ err = au_digen_test(parent, au_sigen(sb));
21921+ if (!err) {
5afbbe0d
AM
21922+ /* regardless LOOKUP_CREATE, always ALLOW_NEG */
21923+ npositive = au_lkup_dentry(dentry, au_dbtop(parent),
21924+ AuLkup_ALLOW_NEG);
027c5e7a
AM
21925+ err = npositive;
21926+ }
4a4d8108 21927+ di_read_unlock(parent, AuLock_IR);
4a4d8108
AM
21928+ ret = ERR_PTR(err);
21929+ if (unlikely(err < 0))
21930+ goto out_unlock;
1308ab2a 21931+
4a4d8108 21932+ if (npositive) {
b752ccd1 21933+ inode = au_new_inode(dentry, /*must_new*/0);
c1595e42
JR
21934+ if (IS_ERR(inode)) {
21935+ ret = (void *)inode;
21936+ inode = NULL;
21937+ goto out_unlock;
21938+ }
9dbd164d 21939+ }
4a4d8108 21940+
c1595e42
JR
21941+ if (inode)
21942+ atomic_inc(&inode->i_count);
4a4d8108 21943+ ret = d_splice_alias(inode, dentry);
43982f53 21944+#if 0 /* reserved for future use */
537831f9
AM
21945+ if (unlikely(d_need_lookup(dentry))) {
21946+ spin_lock(&dentry->d_lock);
21947+ dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
21948+ spin_unlock(&dentry->d_lock);
21949+ } else
21950+#endif
c1595e42 21951+ if (inode) {
2000de60 21952+ if (!IS_ERR(ret)) {
c1595e42 21953+ iput(inode);
2000de60
JR
21954+ if (ret && ret != dentry)
21955+ ii_write_unlock(inode);
21956+ } else {
c1595e42
JR
21957+ ii_write_unlock(inode);
21958+ iput(inode);
21959+ inode = NULL;
21960+ }
7f207e10 21961+ }
1facf9fc 21962+
4f0767ce 21963+out_unlock:
4a4d8108 21964+ di_write_unlock(dentry);
7f207e10 21965+out_si:
4a4d8108 21966+ si_read_unlock(sb);
7f207e10 21967+out:
4a4d8108
AM
21968+ return ret;
21969+}
1facf9fc 21970+
4a4d8108 21971+/* ---------------------------------------------------------------------- */
1facf9fc 21972+
acd2b654
AM
21973+/*
21974+ * very dirty and complicated aufs ->atomic_open().
21975+ * aufs_atomic_open()
21976+ * + au_aopen_or_create()
21977+ * + add_simple()
21978+ * + vfsub_atomic_open()
21979+ * + branch fs ->atomic_open()
21980+ * may call the actual 'open' for h_file
21981+ * + inc br_nfiles only if opened
21982+ * + au_aopen_no_open() or au_aopen_do_open()
21983+ *
21984+ * au_aopen_do_open()
21985+ * + finish_open()
21986+ * + au_do_aopen()
21987+ * + au_do_open() the body of all 'open'
21988+ * + au_do_open_nondir()
21989+ * set the passed h_file
21990+ *
21991+ * au_aopen_no_open()
21992+ * + finish_no_open()
21993+ */
21994+
b912730e 21995+struct aopen_node {
8b6a4947 21996+ struct hlist_bl_node hblist;
b912730e
AM
21997+ struct file *file, *h_file;
21998+};
21999+
22000+static int au_do_aopen(struct inode *inode, struct file *file)
22001+{
8b6a4947
AM
22002+ struct hlist_bl_head *aopen;
22003+ struct hlist_bl_node *pos;
b912730e
AM
22004+ struct aopen_node *node;
22005+ struct au_do_open_args args = {
8b6a4947
AM
22006+ .aopen = 1,
22007+ .open = au_do_open_nondir
b912730e
AM
22008+ };
22009+
22010+ aopen = &au_sbi(inode->i_sb)->si_aopen;
8b6a4947
AM
22011+ hlist_bl_lock(aopen);
22012+ hlist_bl_for_each_entry(node, pos, aopen, hblist)
b912730e
AM
22013+ if (node->file == file) {
22014+ args.h_file = node->h_file;
22015+ break;
22016+ }
8b6a4947 22017+ hlist_bl_unlock(aopen);
b912730e
AM
22018+ /* AuDebugOn(!args.h_file); */
22019+
22020+ return au_do_open(file, &args);
22021+}
22022+
acd2b654
AM
22023+static int au_aopen_do_open(struct file *file, struct dentry *dentry,
22024+ struct aopen_node *aopen_node)
22025+{
22026+ int err;
22027+ struct hlist_bl_head *aopen;
22028+
22029+ AuLabel(here);
22030+ aopen = &au_sbi(dentry->d_sb)->si_aopen;
22031+ au_hbl_add(&aopen_node->hblist, aopen);
22032+ err = finish_open(file, dentry, au_do_aopen);
22033+ au_hbl_del(&aopen_node->hblist, aopen);
22034+ /* AuDbgFile(file); */
22035+ AuDbg("%pd%s%s\n", dentry,
22036+ (file->f_mode & FMODE_CREATED) ? " created" : "",
22037+ (file->f_mode & FMODE_OPENED) ? " opened" : "");
22038+
22039+ AuTraceErr(err);
22040+ return err;
22041+}
22042+
22043+static int au_aopen_no_open(struct file *file, struct dentry *dentry)
22044+{
22045+ int err;
22046+
22047+ AuLabel(here);
22048+ dget(dentry);
22049+ err = finish_no_open(file, dentry);
22050+
22051+ AuTraceErr(err);
22052+ return err;
22053+}
22054+
b912730e
AM
22055+static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
22056+ struct file *file, unsigned int open_flag,
acd2b654 22057+ umode_t create_mode)
b912730e 22058+{
acd2b654 22059+ int err, did_open;
5afbbe0d 22060+ unsigned int lkup_flags;
acd2b654
AM
22061+ aufs_bindex_t bindex;
22062+ struct super_block *sb;
f0c0a007 22063+ struct dentry *parent, *d;
b912730e
AM
22064+ struct vfsub_aopen_args args = {
22065+ .open_flag = open_flag,
acd2b654 22066+ .create_mode = create_mode
b912730e
AM
22067+ };
22068+ struct aopen_node aopen_node = {
22069+ .file = file
22070+ };
22071+
22072+ IMustLock(dir);
5afbbe0d 22073+ AuDbg("open_flag 0%o\n", open_flag);
b912730e
AM
22074+ AuDbgDentry(dentry);
22075+
22076+ err = 0;
22077+ if (!au_di(dentry)) {
5afbbe0d
AM
22078+ lkup_flags = LOOKUP_OPEN;
22079+ if (open_flag & O_CREAT)
22080+ lkup_flags |= LOOKUP_CREATE;
22081+ d = aufs_lookup(dir, dentry, lkup_flags);
b912730e
AM
22082+ if (IS_ERR(d)) {
22083+ err = PTR_ERR(d);
5afbbe0d 22084+ AuTraceErr(err);
b912730e
AM
22085+ goto out;
22086+ } else if (d) {
22087+ /*
22088+ * obsoleted dentry found.
22089+ * another error will be returned later.
22090+ */
22091+ d_drop(d);
b912730e 22092+ AuDbgDentry(d);
5afbbe0d 22093+ dput(d);
b912730e
AM
22094+ }
22095+ AuDbgDentry(dentry);
22096+ }
22097+
22098+ if (d_is_positive(dentry)
22099+ || d_unhashed(dentry)
22100+ || d_unlinked(dentry)
acd2b654
AM
22101+ || !(open_flag & O_CREAT)) {
22102+ err = au_aopen_no_open(file, dentry);
22103+ goto out; /* success */
22104+ }
b912730e
AM
22105+
22106+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
22107+ if (unlikely(err))
22108+ goto out;
22109+
acd2b654 22110+ sb = dentry->d_sb;
b912730e
AM
22111+ parent = dentry->d_parent; /* dir is locked */
22112+ di_write_lock_parent(parent);
5afbbe0d 22113+ err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
acd2b654
AM
22114+ if (unlikely(err < 0))
22115+ goto out_parent;
b912730e
AM
22116+
22117+ AuDbgDentry(dentry);
acd2b654
AM
22118+ if (d_is_positive(dentry)) {
22119+ err = au_aopen_no_open(file, dentry);
22120+ goto out_parent; /* success */
22121+ }
b912730e 22122+
acd2b654 22123+ args.file = alloc_empty_file(file->f_flags, current_cred());
b912730e
AM
22124+ err = PTR_ERR(args.file);
22125+ if (IS_ERR(args.file))
acd2b654 22126+ goto out_parent;
b912730e 22127+
acd2b654 22128+ bindex = au_dbtop(dentry);
b912730e
AM
22129+ err = au_aopen_or_create(dir, dentry, &args);
22130+ AuTraceErr(err);
22131+ AuDbgFile(args.file);
acd2b654
AM
22132+ file->f_mode = args.file->f_mode & ~FMODE_OPENED;
22133+ did_open = !!(args.file->f_mode & FMODE_OPENED);
22134+ if (!did_open) {
22135+ fput(args.file);
22136+ args.file = NULL;
b912730e 22137+ }
8b6a4947
AM
22138+ di_write_unlock(parent);
22139+ di_write_unlock(dentry);
acd2b654
AM
22140+ if (unlikely(err < 0)) {
22141+ if (args.file)
22142+ fput(args.file);
22143+ goto out_sb;
b912730e 22144+ }
b912730e 22145+
acd2b654
AM
22146+ if (!did_open)
22147+ err = au_aopen_no_open(file, dentry);
8b6a4947 22148+ else {
acd2b654
AM
22149+ aopen_node.h_file = args.file;
22150+ err = au_aopen_do_open(file, dentry, &aopen_node);
8b6a4947 22151+ }
acd2b654
AM
22152+ if (unlikely(err < 0)) {
22153+ if (args.file)
22154+ fput(args.file);
22155+ if (did_open)
22156+ au_lcnt_dec(&args.br->br_nfiles);
b912730e 22157+ }
acd2b654
AM
22158+ goto out_sb; /* success */
22159+
22160+out_parent:
22161+ di_write_unlock(parent);
22162+ di_write_unlock(dentry);
22163+out_sb:
22164+ si_read_unlock(sb);
b912730e 22165+out:
b912730e 22166+ AuTraceErr(err);
acd2b654 22167+ AuDbgFile(file);
b912730e
AM
22168+ return err;
22169+}
22170+
22171+
22172+/* ---------------------------------------------------------------------- */
22173+
4a4d8108
AM
22174+static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
22175+ const unsigned char add_entry, aufs_bindex_t bcpup,
5afbbe0d 22176+ aufs_bindex_t btop)
4a4d8108
AM
22177+{
22178+ int err;
22179+ struct dentry *h_parent;
22180+ struct inode *h_dir;
1facf9fc 22181+
027c5e7a 22182+ if (add_entry)
5527c038 22183+ IMustLock(d_inode(parent));
027c5e7a 22184+ else
4a4d8108
AM
22185+ di_write_lock_parent(parent);
22186+
22187+ err = 0;
22188+ if (!au_h_dptr(parent, bcpup)) {
5afbbe0d 22189+ if (btop > bcpup)
c2b27bf2 22190+ err = au_cpup_dirs(dentry, bcpup);
5afbbe0d 22191+ else if (btop < bcpup)
4a4d8108
AM
22192+ err = au_cpdown_dirs(dentry, bcpup);
22193+ else
c2b27bf2 22194+ BUG();
4a4d8108 22195+ }
38d290e6 22196+ if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
4a4d8108 22197+ h_parent = au_h_dptr(parent, bcpup);
5527c038 22198+ h_dir = d_inode(h_parent);
be118d29 22199+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
7e9cd9fe 22200+ err = au_lkup_neg(dentry, bcpup, /*wh*/0);
4a4d8108 22201+ /* todo: no unlock here */
3c1bdaff 22202+ inode_unlock_shared(h_dir);
027c5e7a
AM
22203+
22204+ AuDbg("bcpup %d\n", bcpup);
22205+ if (!err) {
5527c038 22206+ if (d_really_is_negative(dentry))
5afbbe0d 22207+ au_set_h_dptr(dentry, btop, NULL);
4a4d8108
AM
22208+ au_update_dbrange(dentry, /*do_put_zero*/0);
22209+ }
1308ab2a 22210+ }
1facf9fc 22211+
4a4d8108
AM
22212+ if (!add_entry)
22213+ di_write_unlock(parent);
22214+ if (!err)
22215+ err = bcpup; /* success */
1308ab2a 22216+
027c5e7a 22217+ AuTraceErr(err);
4a4d8108
AM
22218+ return err;
22219+}
1facf9fc 22220+
4a4d8108
AM
22221+/*
22222+ * decide the branch and the parent dir where we will create a new entry.
22223+ * returns new bindex or an error.
22224+ * copyup the parent dir if needed.
22225+ */
22226+int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
22227+ struct au_wr_dir_args *args)
22228+{
22229+ int err;
392086de 22230+ unsigned int flags;
5afbbe0d 22231+ aufs_bindex_t bcpup, btop, src_btop;
86dc4139
AM
22232+ const unsigned char add_entry
22233+ = au_ftest_wrdir(args->flags, ADD_ENTRY)
38d290e6 22234+ | au_ftest_wrdir(args->flags, TMPFILE);
4a4d8108
AM
22235+ struct super_block *sb;
22236+ struct dentry *parent;
22237+ struct au_sbinfo *sbinfo;
1facf9fc 22238+
4a4d8108
AM
22239+ sb = dentry->d_sb;
22240+ sbinfo = au_sbi(sb);
22241+ parent = dget_parent(dentry);
5afbbe0d
AM
22242+ btop = au_dbtop(dentry);
22243+ bcpup = btop;
4a4d8108
AM
22244+ if (args->force_btgt < 0) {
22245+ if (src_dentry) {
5afbbe0d
AM
22246+ src_btop = au_dbtop(src_dentry);
22247+ if (src_btop < btop)
22248+ bcpup = src_btop;
4a4d8108 22249+ } else if (add_entry) {
392086de
AM
22250+ flags = 0;
22251+ if (au_ftest_wrdir(args->flags, ISDIR))
22252+ au_fset_wbr(flags, DIR);
22253+ err = AuWbrCreate(sbinfo, dentry, flags);
4a4d8108
AM
22254+ bcpup = err;
22255+ }
1facf9fc 22256+
5527c038 22257+ if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
4a4d8108
AM
22258+ if (add_entry)
22259+ err = AuWbrCopyup(sbinfo, dentry);
22260+ else {
22261+ if (!IS_ROOT(dentry)) {
22262+ di_read_lock_parent(parent, !AuLock_IR);
22263+ err = AuWbrCopyup(sbinfo, dentry);
22264+ di_read_unlock(parent, !AuLock_IR);
22265+ } else
22266+ err = AuWbrCopyup(sbinfo, dentry);
22267+ }
22268+ bcpup = err;
22269+ if (unlikely(err < 0))
22270+ goto out;
22271+ }
22272+ } else {
22273+ bcpup = args->force_btgt;
5527c038 22274+ AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
1308ab2a 22275+ }
027c5e7a 22276+
5afbbe0d 22277+ AuDbg("btop %d, bcpup %d\n", btop, bcpup);
4a4d8108 22278+ err = bcpup;
5afbbe0d 22279+ if (bcpup == btop)
4a4d8108 22280+ goto out; /* success */
4a4d8108
AM
22281+
22282+ /* copyup the new parent into the branch we process */
5afbbe0d 22283+ err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
027c5e7a 22284+ if (err >= 0) {
5527c038 22285+ if (d_really_is_negative(dentry)) {
5afbbe0d
AM
22286+ au_set_h_dptr(dentry, btop, NULL);
22287+ au_set_dbtop(dentry, bcpup);
22288+ au_set_dbbot(dentry, bcpup);
027c5e7a 22289+ }
38d290e6
JR
22290+ AuDebugOn(add_entry
22291+ && !au_ftest_wrdir(args->flags, TMPFILE)
22292+ && !au_h_dptr(dentry, bcpup));
027c5e7a 22293+ }
86dc4139
AM
22294+
22295+out:
22296+ dput(parent);
22297+ return err;
22298+}
22299+
22300+/* ---------------------------------------------------------------------- */
22301+
22302+void au_pin_hdir_unlock(struct au_pin *p)
22303+{
22304+ if (p->hdir)
5afbbe0d 22305+ au_hn_inode_unlock(p->hdir);
86dc4139
AM
22306+}
22307+
c1595e42 22308+int au_pin_hdir_lock(struct au_pin *p)
86dc4139
AM
22309+{
22310+ int err;
22311+
22312+ err = 0;
22313+ if (!p->hdir)
22314+ goto out;
22315+
22316+ /* even if an error happens later, keep this lock */
5afbbe0d 22317+ au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
86dc4139
AM
22318+
22319+ err = -EBUSY;
5527c038 22320+ if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
86dc4139
AM
22321+ goto out;
22322+
22323+ err = 0;
22324+ if (p->h_dentry)
22325+ err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
22326+ p->h_parent, p->br);
22327+
22328+out:
22329+ return err;
22330+}
22331+
22332+int au_pin_hdir_relock(struct au_pin *p)
22333+{
22334+ int err, i;
22335+ struct inode *h_i;
22336+ struct dentry *h_d[] = {
22337+ p->h_dentry,
22338+ p->h_parent
22339+ };
22340+
22341+ err = au_pin_hdir_lock(p);
22342+ if (unlikely(err))
22343+ goto out;
22344+
22345+ for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
22346+ if (!h_d[i])
22347+ continue;
5527c038
JR
22348+ if (d_is_positive(h_d[i])) {
22349+ h_i = d_inode(h_d[i]);
86dc4139 22350+ err = !h_i->i_nlink;
5527c038 22351+ }
86dc4139
AM
22352+ }
22353+
22354+out:
22355+ return err;
22356+}
22357+
5afbbe0d 22358+static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
86dc4139 22359+{
eca34b5c 22360+ atomic_long_set(&p->hdir->hi_inode->i_rwsem.owner, (long)task);
86dc4139
AM
22361+}
22362+
22363+void au_pin_hdir_acquire_nest(struct au_pin *p)
22364+{
22365+ if (p->hdir) {
5afbbe0d 22366+ rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
86dc4139
AM
22367+ p->lsc_hi, 0, NULL, _RET_IP_);
22368+ au_pin_hdir_set_owner(p, current);
22369+ }
dece6358 22370+}
1facf9fc 22371+
86dc4139
AM
22372+void au_pin_hdir_release(struct au_pin *p)
22373+{
22374+ if (p->hdir) {
22375+ au_pin_hdir_set_owner(p, p->task);
d58c55f2 22376+ rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, _RET_IP_);
86dc4139
AM
22377+ }
22378+}
1308ab2a 22379+
4a4d8108 22380+struct dentry *au_pinned_h_parent(struct au_pin *pin)
1308ab2a 22381+{
4a4d8108
AM
22382+ if (pin && pin->parent)
22383+ return au_h_dptr(pin->parent, pin->bindex);
22384+ return NULL;
dece6358 22385+}
1facf9fc 22386+
4a4d8108 22387+void au_unpin(struct au_pin *p)
dece6358 22388+{
86dc4139
AM
22389+ if (p->hdir)
22390+ au_pin_hdir_unlock(p);
e49829fe 22391+ if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
b4510431 22392+ vfsub_mnt_drop_write(p->h_mnt);
4a4d8108
AM
22393+ if (!p->hdir)
22394+ return;
1facf9fc 22395+
4a4d8108
AM
22396+ if (!au_ftest_pin(p->flags, DI_LOCKED))
22397+ di_read_unlock(p->parent, AuLock_IR);
22398+ iput(p->hdir->hi_inode);
22399+ dput(p->parent);
22400+ p->parent = NULL;
22401+ p->hdir = NULL;
22402+ p->h_mnt = NULL;
86dc4139 22403+ /* do not clear p->task */
4a4d8108 22404+}
1308ab2a 22405+
4a4d8108
AM
22406+int au_do_pin(struct au_pin *p)
22407+{
22408+ int err;
22409+ struct super_block *sb;
4a4d8108
AM
22410+ struct inode *h_dir;
22411+
22412+ err = 0;
22413+ sb = p->dentry->d_sb;
86dc4139 22414+ p->br = au_sbr(sb, p->bindex);
4a4d8108
AM
22415+ if (IS_ROOT(p->dentry)) {
22416+ if (au_ftest_pin(p->flags, MNT_WRITE)) {
86dc4139 22417+ p->h_mnt = au_br_mnt(p->br);
b4510431 22418+ err = vfsub_mnt_want_write(p->h_mnt);
4a4d8108
AM
22419+ if (unlikely(err)) {
22420+ au_fclr_pin(p->flags, MNT_WRITE);
22421+ goto out_err;
22422+ }
22423+ }
dece6358 22424+ goto out;
1facf9fc 22425+ }
22426+
86dc4139 22427+ p->h_dentry = NULL;
5afbbe0d 22428+ if (p->bindex <= au_dbbot(p->dentry))
86dc4139 22429+ p->h_dentry = au_h_dptr(p->dentry, p->bindex);
dece6358 22430+
4a4d8108
AM
22431+ p->parent = dget_parent(p->dentry);
22432+ if (!au_ftest_pin(p->flags, DI_LOCKED))
22433+ di_read_lock(p->parent, AuLock_IR, p->lsc_di);
dece6358 22434+
4a4d8108 22435+ h_dir = NULL;
86dc4139 22436+ p->h_parent = au_h_dptr(p->parent, p->bindex);
5527c038 22437+ p->hdir = au_hi(d_inode(p->parent), p->bindex);
4a4d8108
AM
22438+ if (p->hdir)
22439+ h_dir = p->hdir->hi_inode;
dece6358 22440+
b752ccd1
AM
22441+ /*
22442+ * udba case, or
22443+ * if DI_LOCKED is not set, then p->parent may be different
22444+ * and h_parent can be NULL.
22445+ */
86dc4139 22446+ if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
e49829fe 22447+ err = -EBUSY;
4a4d8108
AM
22448+ if (!au_ftest_pin(p->flags, DI_LOCKED))
22449+ di_read_unlock(p->parent, AuLock_IR);
22450+ dput(p->parent);
22451+ p->parent = NULL;
22452+ goto out_err;
22453+ }
1308ab2a 22454+
4a4d8108 22455+ if (au_ftest_pin(p->flags, MNT_WRITE)) {
86dc4139 22456+ p->h_mnt = au_br_mnt(p->br);
b4510431 22457+ err = vfsub_mnt_want_write(p->h_mnt);
dece6358 22458+ if (unlikely(err)) {
4a4d8108 22459+ au_fclr_pin(p->flags, MNT_WRITE);
86dc4139
AM
22460+ if (!au_ftest_pin(p->flags, DI_LOCKED))
22461+ di_read_unlock(p->parent, AuLock_IR);
22462+ dput(p->parent);
22463+ p->parent = NULL;
22464+ goto out_err;
dece6358
AM
22465+ }
22466+ }
4a4d8108 22467+
86dc4139
AM
22468+ au_igrab(h_dir);
22469+ err = au_pin_hdir_lock(p);
22470+ if (!err)
22471+ goto out; /* success */
22472+
076b876e
AM
22473+ au_unpin(p);
22474+
4f0767ce 22475+out_err:
4a4d8108
AM
22476+ pr_err("err %d\n", err);
22477+ err = au_busy_or_stale();
4f0767ce 22478+out:
1facf9fc 22479+ return err;
22480+}
22481+
4a4d8108
AM
22482+void au_pin_init(struct au_pin *p, struct dentry *dentry,
22483+ aufs_bindex_t bindex, int lsc_di, int lsc_hi,
22484+ unsigned int udba, unsigned char flags)
22485+{
22486+ p->dentry = dentry;
22487+ p->udba = udba;
22488+ p->lsc_di = lsc_di;
22489+ p->lsc_hi = lsc_hi;
22490+ p->flags = flags;
22491+ p->bindex = bindex;
22492+
22493+ p->parent = NULL;
22494+ p->hdir = NULL;
22495+ p->h_mnt = NULL;
86dc4139
AM
22496+
22497+ p->h_dentry = NULL;
22498+ p->h_parent = NULL;
22499+ p->br = NULL;
22500+ p->task = current;
4a4d8108
AM
22501+}
22502+
22503+int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
22504+ unsigned int udba, unsigned char flags)
22505+{
22506+ au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
22507+ udba, flags);
22508+ return au_do_pin(pin);
22509+}
22510+
dece6358
AM
22511+/* ---------------------------------------------------------------------- */
22512+
1308ab2a 22513+/*
4a4d8108
AM
22514+ * ->setattr() and ->getattr() are called in various cases.
22515+ * chmod, stat: dentry is revalidated.
22516+ * fchmod, fstat: file and dentry are not revalidated, additionally they may be
22517+ * unhashed.
22518+ * for ->setattr(), ia->ia_file is passed from ftruncate only.
1308ab2a 22519+ */
027c5e7a 22520+/* todo: consolidate with do_refresh() and simple_reval_dpath() */
c1595e42 22521+int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
1facf9fc 22522+{
4a4d8108 22523+ int err;
4a4d8108 22524+ struct dentry *parent;
1facf9fc 22525+
1308ab2a 22526+ err = 0;
027c5e7a 22527+ if (au_digen_test(dentry, sigen)) {
4a4d8108
AM
22528+ parent = dget_parent(dentry);
22529+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 22530+ err = au_refresh_dentry(dentry, parent);
4a4d8108
AM
22531+ di_read_unlock(parent, AuLock_IR);
22532+ dput(parent);
dece6358 22533+ }
1facf9fc 22534+
4a4d8108 22535+ AuTraceErr(err);
1308ab2a 22536+ return err;
22537+}
dece6358 22538+
c1595e42
JR
22539+int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
22540+ struct au_icpup_args *a)
1308ab2a 22541+{
22542+ int err;
4a4d8108 22543+ loff_t sz;
5afbbe0d 22544+ aufs_bindex_t btop, ibtop;
4a4d8108
AM
22545+ struct dentry *hi_wh, *parent;
22546+ struct inode *inode;
4a4d8108
AM
22547+ struct au_wr_dir_args wr_dir_args = {
22548+ .force_btgt = -1,
22549+ .flags = 0
22550+ };
22551+
2000de60 22552+ if (d_is_dir(dentry))
4a4d8108
AM
22553+ au_fset_wrdir(wr_dir_args.flags, ISDIR);
22554+ /* plink or hi_wh() case */
5afbbe0d 22555+ btop = au_dbtop(dentry);
5527c038 22556+ inode = d_inode(dentry);
5afbbe0d
AM
22557+ ibtop = au_ibtop(inode);
22558+ if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
22559+ wr_dir_args.force_btgt = ibtop;
4a4d8108
AM
22560+ err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
22561+ if (unlikely(err < 0))
22562+ goto out;
22563+ a->btgt = err;
5afbbe0d 22564+ if (err != btop)
4a4d8108
AM
22565+ au_fset_icpup(a->flags, DID_CPUP);
22566+
22567+ err = 0;
22568+ a->pin_flags = AuPin_MNT_WRITE;
22569+ parent = NULL;
22570+ if (!IS_ROOT(dentry)) {
22571+ au_fset_pin(a->pin_flags, DI_LOCKED);
22572+ parent = dget_parent(dentry);
22573+ di_write_lock_parent(parent);
22574+ }
22575+
22576+ err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
22577+ if (unlikely(err))
22578+ goto out_parent;
22579+
4a4d8108 22580+ sz = -1;
5afbbe0d 22581+ a->h_path.dentry = au_h_dptr(dentry, btop);
5527c038 22582+ a->h_inode = d_inode(a->h_path.dentry);
c1595e42 22583+ if (ia && (ia->ia_valid & ATTR_SIZE)) {
be118d29 22584+ inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
c1595e42
JR
22585+ if (ia->ia_size < i_size_read(a->h_inode))
22586+ sz = ia->ia_size;
3c1bdaff 22587+ inode_unlock_shared(a->h_inode);
c1595e42 22588+ }
4a4d8108 22589+
4a4d8108 22590+ hi_wh = NULL;
027c5e7a 22591+ if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
4a4d8108
AM
22592+ hi_wh = au_hi_wh(inode, a->btgt);
22593+ if (!hi_wh) {
c2b27bf2
AM
22594+ struct au_cp_generic cpg = {
22595+ .dentry = dentry,
22596+ .bdst = a->btgt,
22597+ .bsrc = -1,
22598+ .len = sz,
22599+ .pin = &a->pin
22600+ };
22601+ err = au_sio_cpup_wh(&cpg, /*file*/NULL);
4a4d8108
AM
22602+ if (unlikely(err))
22603+ goto out_unlock;
22604+ hi_wh = au_hi_wh(inode, a->btgt);
22605+ /* todo: revalidate hi_wh? */
22606+ }
22607+ }
22608+
22609+ if (parent) {
22610+ au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
22611+ di_downgrade_lock(parent, AuLock_IR);
22612+ dput(parent);
22613+ parent = NULL;
22614+ }
22615+ if (!au_ftest_icpup(a->flags, DID_CPUP))
22616+ goto out; /* success */
22617+
22618+ if (!d_unhashed(dentry)) {
c2b27bf2
AM
22619+ struct au_cp_generic cpg = {
22620+ .dentry = dentry,
22621+ .bdst = a->btgt,
5afbbe0d 22622+ .bsrc = btop,
c2b27bf2
AM
22623+ .len = sz,
22624+ .pin = &a->pin,
22625+ .flags = AuCpup_DTIME | AuCpup_HOPEN
22626+ };
22627+ err = au_sio_cpup_simple(&cpg);
4a4d8108
AM
22628+ if (!err)
22629+ a->h_path.dentry = au_h_dptr(dentry, a->btgt);
22630+ } else if (!hi_wh)
22631+ a->h_path.dentry = au_h_dptr(dentry, a->btgt);
22632+ else
22633+ a->h_path.dentry = hi_wh; /* do not dget here */
1308ab2a 22634+
4f0767ce 22635+out_unlock:
5527c038 22636+ a->h_inode = d_inode(a->h_path.dentry);
86dc4139 22637+ if (!err)
dece6358 22638+ goto out; /* success */
4a4d8108 22639+ au_unpin(&a->pin);
4f0767ce 22640+out_parent:
4a4d8108
AM
22641+ if (parent) {
22642+ di_write_unlock(parent);
22643+ dput(parent);
22644+ }
4f0767ce 22645+out:
86dc4139 22646+ if (!err)
febd17d6 22647+ inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
1facf9fc 22648+ return err;
22649+}
22650+
5db017eb 22651+static int aufs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
0b2a12c6 22652+ struct iattr *ia)
1facf9fc 22653+{
4a4d8108 22654+ int err;
523b37e3 22655+ struct inode *inode, *delegated;
4a4d8108
AM
22656+ struct super_block *sb;
22657+ struct file *file;
22658+ struct au_icpup_args *a;
5db017eb 22659+ struct mnt_idmap *h_idmap;
1facf9fc 22660+
5527c038 22661+ inode = d_inode(dentry);
4a4d8108 22662+ IMustLock(inode);
dece6358 22663+
5db017eb 22664+ err = setattr_prepare(idmap, dentry, ia);
f2c43d5f
AM
22665+ if (unlikely(err))
22666+ goto out;
22667+
4a4d8108
AM
22668+ err = -ENOMEM;
22669+ a = kzalloc(sizeof(*a), GFP_NOFS);
22670+ if (unlikely(!a))
22671+ goto out;
1facf9fc 22672+
4a4d8108
AM
22673+ if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
22674+ ia->ia_valid &= ~ATTR_MODE;
dece6358 22675+
4a4d8108
AM
22676+ file = NULL;
22677+ sb = dentry->d_sb;
e49829fe
JR
22678+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
22679+ if (unlikely(err))
22680+ goto out_kfree;
22681+
4a4d8108
AM
22682+ if (ia->ia_valid & ATTR_FILE) {
22683+ /* currently ftruncate(2) only */
7e9cd9fe 22684+ AuDebugOn(!d_is_reg(dentry));
4a4d8108 22685+ file = ia->ia_file;
521ced18
JR
22686+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
22687+ /*fi_lsc*/0);
4a4d8108
AM
22688+ if (unlikely(err))
22689+ goto out_si;
22690+ ia->ia_file = au_hf_top(file);
22691+ a->udba = AuOpt_UDBA_NONE;
22692+ } else {
22693+ /* fchmod() doesn't pass ia_file */
22694+ a->udba = au_opt_udba(sb);
027c5e7a
AM
22695+ di_write_lock_child(dentry);
22696+ /* no d_unlinked(), to set UDBA_NONE for root */
4a4d8108
AM
22697+ if (d_unhashed(dentry))
22698+ a->udba = AuOpt_UDBA_NONE;
4a4d8108
AM
22699+ if (a->udba != AuOpt_UDBA_NONE) {
22700+ AuDebugOn(IS_ROOT(dentry));
22701+ err = au_reval_for_attr(dentry, au_sigen(sb));
22702+ if (unlikely(err))
22703+ goto out_dentry;
22704+ }
dece6358 22705+ }
dece6358 22706+
4a4d8108
AM
22707+ err = au_pin_and_icpup(dentry, ia, a);
22708+ if (unlikely(err < 0))
22709+ goto out_dentry;
22710+ if (au_ftest_icpup(a->flags, DID_CPUP)) {
22711+ ia->ia_file = NULL;
22712+ ia->ia_valid &= ~ATTR_FILE;
1308ab2a 22713+ }
dece6358 22714+
4a4d8108
AM
22715+ a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
22716+ if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
22717+ == (ATTR_MODE | ATTR_CTIME)) {
7eafdf33 22718+ err = security_path_chmod(&a->h_path, ia->ia_mode);
4a4d8108
AM
22719+ if (unlikely(err))
22720+ goto out_unlock;
22721+ } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
22722+ && (ia->ia_valid & ATTR_CTIME)) {
86dc4139 22723+ err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
4a4d8108
AM
22724+ if (unlikely(err))
22725+ goto out_unlock;
22726+ }
dece6358 22727+
4a4d8108
AM
22728+ if (ia->ia_valid & ATTR_SIZE) {
22729+ struct file *f;
1308ab2a 22730+
953406b4 22731+ if (ia->ia_size < i_size_read(inode))
4a4d8108 22732+ /* unmap only */
953406b4 22733+ truncate_setsize(inode, ia->ia_size);
1308ab2a 22734+
4a4d8108
AM
22735+ f = NULL;
22736+ if (ia->ia_valid & ATTR_FILE)
22737+ f = ia->ia_file;
febd17d6 22738+ inode_unlock(a->h_inode);
4a4d8108 22739+ err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
febd17d6 22740+ inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
523b37e3
AM
22741+ } else {
22742+ delegated = NULL;
22743+ while (1) {
22744+ err = vfsub_notify_change(&a->h_path, ia, &delegated);
22745+ if (delegated) {
22746+ err = break_deleg_wait(&delegated);
22747+ if (!err)
22748+ continue;
22749+ }
22750+ break;
22751+ }
22752+ }
8cdd5066
JR
22753+ /*
22754+ * regardless aufs 'acl' option setting.
22755+ * why don't all acl-aware fs call this func from their ->setattr()?
22756+ */
0b2a12c6 22757+ if (!err && (ia->ia_valid & ATTR_MODE)) {
5db017eb
AM
22758+ h_idmap = mnt_idmap(a->h_path.mnt);
22759+ err = vfsub_acl_chmod(h_idmap, a->h_path.dentry, ia->ia_mode);
0b2a12c6 22760+ }
4a4d8108
AM
22761+ if (!err)
22762+ au_cpup_attr_changeable(inode);
1308ab2a 22763+
4f0767ce 22764+out_unlock:
febd17d6 22765+ inode_unlock(a->h_inode);
4a4d8108 22766+ au_unpin(&a->pin);
027c5e7a 22767+ if (unlikely(err))
5afbbe0d 22768+ au_update_dbtop(dentry);
4f0767ce 22769+out_dentry:
4a4d8108
AM
22770+ di_write_unlock(dentry);
22771+ if (file) {
22772+ fi_write_unlock(file);
22773+ ia->ia_file = file;
22774+ ia->ia_valid |= ATTR_FILE;
22775+ }
4f0767ce 22776+out_si:
4a4d8108 22777+ si_read_unlock(sb);
e49829fe 22778+out_kfree:
9f237c51 22779+ au_kfree_rcu(a);
4f0767ce 22780+out:
4a4d8108
AM
22781+ AuTraceErr(err);
22782+ return err;
1facf9fc 22783+}
22784+
c1595e42
JR
22785+#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
22786+static int au_h_path_to_set_attr(struct dentry *dentry,
22787+ struct au_icpup_args *a, struct path *h_path)
22788+{
22789+ int err;
22790+ struct super_block *sb;
22791+
22792+ sb = dentry->d_sb;
22793+ a->udba = au_opt_udba(sb);
22794+ /* no d_unlinked(), to set UDBA_NONE for root */
22795+ if (d_unhashed(dentry))
22796+ a->udba = AuOpt_UDBA_NONE;
22797+ if (a->udba != AuOpt_UDBA_NONE) {
22798+ AuDebugOn(IS_ROOT(dentry));
22799+ err = au_reval_for_attr(dentry, au_sigen(sb));
22800+ if (unlikely(err))
22801+ goto out;
22802+ }
22803+ err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
22804+ if (unlikely(err < 0))
22805+ goto out;
22806+
22807+ h_path->dentry = a->h_path.dentry;
22808+ h_path->mnt = au_sbr_mnt(sb, a->btgt);
22809+
22810+out:
22811+ return err;
22812+}
22813+
f2c43d5f
AM
22814+ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
22815+ struct au_sxattr *arg)
c1595e42
JR
22816+{
22817+ int err;
22818+ struct path h_path;
22819+ struct super_block *sb;
22820+ struct au_icpup_args *a;
5afbbe0d 22821+ struct inode *h_inode;
5db017eb 22822+ struct mnt_idmap *h_idmap;
c1595e42 22823+
c1595e42
JR
22824+ IMustLock(inode);
22825+
22826+ err = -ENOMEM;
22827+ a = kzalloc(sizeof(*a), GFP_NOFS);
22828+ if (unlikely(!a))
22829+ goto out;
22830+
22831+ sb = dentry->d_sb;
22832+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
22833+ if (unlikely(err))
22834+ goto out_kfree;
22835+
22836+ h_path.dentry = NULL; /* silence gcc */
22837+ di_write_lock_child(dentry);
22838+ err = au_h_path_to_set_attr(dentry, a, &h_path);
22839+ if (unlikely(err))
22840+ goto out_di;
5db017eb 22841+ h_idmap = mnt_idmap(h_path.mnt);
c1595e42 22842+
febd17d6 22843+ inode_unlock(a->h_inode);
c1595e42
JR
22844+ switch (arg->type) {
22845+ case AU_XATTR_SET:
5afbbe0d 22846+ AuDebugOn(d_is_negative(h_path.dentry));
5db017eb 22847+ err = vfsub_setxattr(h_idmap, h_path.dentry,
c1595e42
JR
22848+ arg->u.set.name, arg->u.set.value,
22849+ arg->u.set.size, arg->u.set.flags);
22850+ break;
c1595e42
JR
22851+ case AU_ACL_SET:
22852+ err = -EOPNOTSUPP;
5527c038 22853+ h_inode = d_inode(h_path.dentry);
0b2a12c6 22854+ if (h_inode->i_op->set_acl) {
f2c43d5f 22855+ /* this will call posix_acl_update_mode */
5db017eb 22856+ err = h_inode->i_op->set_acl(h_idmap, h_path.dentry,
c1595e42
JR
22857+ arg->u.acl_set.acl,
22858+ arg->u.acl_set.type);
0b2a12c6 22859+ }
c1595e42
JR
22860+ break;
22861+ }
22862+ if (!err)
22863+ au_cpup_attr_timesizes(inode);
22864+
22865+ au_unpin(&a->pin);
22866+ if (unlikely(err))
5afbbe0d 22867+ au_update_dbtop(dentry);
c1595e42
JR
22868+
22869+out_di:
22870+ di_write_unlock(dentry);
22871+ si_read_unlock(sb);
22872+out_kfree:
9f237c51 22873+ au_kfree_rcu(a);
c1595e42
JR
22874+out:
22875+ AuTraceErr(err);
22876+ return err;
22877+}
22878+#endif
22879+
4a4d8108
AM
22880+static void au_refresh_iattr(struct inode *inode, struct kstat *st,
22881+ unsigned int nlink)
1facf9fc 22882+{
9dbd164d
AM
22883+ unsigned int n;
22884+
4a4d8108 22885+ inode->i_mode = st->mode;
86dc4139
AM
22886+ /* don't i_[ug]id_write() here */
22887+ inode->i_uid = st->uid;
22888+ inode->i_gid = st->gid;
4a4d8108
AM
22889+ inode->i_atime = st->atime;
22890+ inode->i_mtime = st->mtime;
631230db 22891+ inode_set_ctime_to_ts(inode, st->ctime);
1facf9fc 22892+
4a4d8108
AM
22893+ au_cpup_attr_nlink(inode, /*force*/0);
22894+ if (S_ISDIR(inode->i_mode)) {
9dbd164d
AM
22895+ n = inode->i_nlink;
22896+ n -= nlink;
22897+ n += st->nlink;
f6b6e03d 22898+ smp_mb(); /* for i_nlink */
7eafdf33 22899+ /* 0 can happen */
92d182d2 22900+ set_nlink(inode, n);
4a4d8108 22901+ }
1facf9fc 22902+
4a4d8108
AM
22903+ spin_lock(&inode->i_lock);
22904+ inode->i_blocks = st->blocks;
22905+ i_size_write(inode, st->size);
22906+ spin_unlock(&inode->i_lock);
1facf9fc 22907+}
22908+
c1595e42 22909+/*
f2c43d5f 22910+ * common routine for aufs_getattr() and au_getxattr().
c1595e42
JR
22911+ * returns zero or negative (an error).
22912+ * @dentry will be read-locked in success.
22913+ */
42b5c33a
AM
22914+int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
22915+ struct path *h_path, int locked)
1facf9fc 22916+{
4a4d8108 22917+ int err;
076b876e 22918+ unsigned int mnt_flags, sigen;
c1595e42 22919+ unsigned char udba_none;
4a4d8108 22920+ aufs_bindex_t bindex;
4a4d8108 22921+ struct super_block *sb, *h_sb;
1facf9fc 22922+
c1595e42
JR
22923+ h_path->mnt = NULL;
22924+ h_path->dentry = NULL;
22925+
22926+ err = 0;
4a4d8108 22927+ sb = dentry->d_sb;
4a4d8108
AM
22928+ mnt_flags = au_mntflags(sb);
22929+ udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
1facf9fc 22930+
a2654f78
AM
22931+ if (unlikely(locked))
22932+ goto body; /* skip locking dinfo */
22933+
4a4d8108 22934+ /* support fstat(2) */
027c5e7a 22935+ if (!d_unlinked(dentry) && !udba_none) {
076b876e 22936+ sigen = au_sigen(sb);
027c5e7a
AM
22937+ err = au_digen_test(dentry, sigen);
22938+ if (!err) {
4a4d8108 22939+ di_read_lock_child(dentry, AuLock_IR);
027c5e7a 22940+ err = au_dbrange_test(dentry);
c1595e42
JR
22941+ if (unlikely(err)) {
22942+ di_read_unlock(dentry, AuLock_IR);
22943+ goto out;
22944+ }
027c5e7a 22945+ } else {
4a4d8108
AM
22946+ AuDebugOn(IS_ROOT(dentry));
22947+ di_write_lock_child(dentry);
027c5e7a
AM
22948+ err = au_dbrange_test(dentry);
22949+ if (!err)
22950+ err = au_reval_for_attr(dentry, sigen);
c1595e42
JR
22951+ if (!err)
22952+ di_downgrade_lock(dentry, AuLock_IR);
22953+ else {
22954+ di_write_unlock(dentry);
22955+ goto out;
22956+ }
4a4d8108
AM
22957+ }
22958+ } else
22959+ di_read_lock_child(dentry, AuLock_IR);
1facf9fc 22960+
a2654f78 22961+body:
42b5c33a
AM
22962+ if (!inode) {
22963+ inode = d_inode(dentry);
22964+ if (unlikely(!inode))
22965+ goto out;
22966+ }
5afbbe0d 22967+ bindex = au_ibtop(inode);
c1595e42
JR
22968+ h_path->mnt = au_sbr_mnt(sb, bindex);
22969+ h_sb = h_path->mnt->mnt_sb;
22970+ if (!force
22971+ && !au_test_fs_bad_iattr(h_sb)
22972+ && udba_none)
22973+ goto out; /* success */
1facf9fc 22974+
5afbbe0d 22975+ if (au_dbtop(dentry) == bindex)
c1595e42 22976+ h_path->dentry = au_h_dptr(dentry, bindex);
4a4d8108 22977+ else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
c1595e42
JR
22978+ h_path->dentry = au_plink_lkup(inode, bindex);
22979+ if (IS_ERR(h_path->dentry))
22980+ /* pretending success */
22981+ h_path->dentry = NULL;
22982+ else
22983+ dput(h_path->dentry);
4a4d8108 22984+ }
c1595e42
JR
22985+
22986+out:
22987+ return err;
22988+}
22989+
5db017eb 22990+static int aufs_getattr(struct mnt_idmap *idmap, const struct path *path,
0b2a12c6 22991+ struct kstat *st, u32 request, unsigned int query)
c1595e42
JR
22992+{
22993+ int err;
22994+ unsigned char positive;
22995+ struct path h_path;
521ced18 22996+ struct dentry *dentry;
c1595e42
JR
22997+ struct inode *inode;
22998+ struct super_block *sb;
22999+
521ced18 23000+ dentry = path->dentry;
5527c038 23001+ inode = d_inode(dentry);
c1595e42
JR
23002+ sb = dentry->d_sb;
23003+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
23004+ if (unlikely(err))
23005+ goto out;
42b5c33a
AM
23006+ err = au_h_path_getattr(dentry, /*inode*/NULL, /*force*/0, &h_path,
23007+ /*locked*/0);
c1595e42
JR
23008+ if (unlikely(err))
23009+ goto out_si;
c06a8ce3 23010+ if (unlikely(!h_path.dentry))
c1595e42 23011+ /* illegally overlapped or something */
4a4d8108
AM
23012+ goto out_fill; /* pretending success */
23013+
5527c038 23014+ positive = d_is_positive(h_path.dentry);
4a4d8108 23015+ if (positive)
521ced18
JR
23016+ /* no vfsub version */
23017+ err = vfs_getattr(&h_path, st, request, query);
4a4d8108
AM
23018+ if (!err) {
23019+ if (positive)
c06a8ce3 23020+ au_refresh_iattr(inode, st,
5527c038 23021+ d_inode(h_path.dentry)->i_nlink);
4a4d8108 23022+ goto out_fill; /* success */
1facf9fc 23023+ }
7f207e10 23024+ AuTraceErr(err);
c1595e42 23025+ goto out_di;
4a4d8108 23026+
4f0767ce 23027+out_fill:
631230db 23028+ generic_fillattr(idmap, request, inode, st);
c1595e42 23029+out_di:
4a4d8108 23030+ di_read_unlock(dentry, AuLock_IR);
c1595e42 23031+out_si:
4a4d8108 23032+ si_read_unlock(sb);
7f207e10
AM
23033+out:
23034+ AuTraceErr(err);
4a4d8108 23035+ return err;
1facf9fc 23036+}
23037+
23038+/* ---------------------------------------------------------------------- */
23039+
febd17d6
JR
23040+static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
23041+ struct delayed_call *done)
4a4d8108 23042+{
c2c0f25c 23043+ const char *ret;
c2c0f25c 23044+ struct dentry *h_dentry;
febd17d6 23045+ struct inode *h_inode;
4a4d8108 23046+ int err;
c2c0f25c 23047+ aufs_bindex_t bindex;
1facf9fc 23048+
79b8bda9 23049+ ret = NULL; /* suppress a warning */
febd17d6
JR
23050+ err = -ECHILD;
23051+ if (!dentry)
23052+ goto out;
23053+
027c5e7a
AM
23054+ err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
23055+ if (unlikely(err))
c2c0f25c 23056+ goto out;
027c5e7a
AM
23057+
23058+ err = au_d_hashed_positive(dentry);
c2c0f25c
AM
23059+ if (unlikely(err))
23060+ goto out_unlock;
23061+
23062+ err = -EINVAL;
23063+ inode = d_inode(dentry);
5afbbe0d 23064+ bindex = au_ibtop(inode);
c2c0f25c 23065+ h_inode = au_h_iptr(inode, bindex);
febd17d6 23066+ if (unlikely(!h_inode->i_op->get_link))
c2c0f25c
AM
23067+ goto out_unlock;
23068+
23069+ err = -EBUSY;
23070+ h_dentry = NULL;
5afbbe0d 23071+ if (au_dbtop(dentry) <= bindex) {
c2c0f25c
AM
23072+ h_dentry = au_h_dptr(dentry, bindex);
23073+ if (h_dentry)
23074+ dget(h_dentry);
027c5e7a 23075+ }
c2c0f25c
AM
23076+ if (!h_dentry) {
23077+ h_dentry = d_find_any_alias(h_inode);
23078+ if (IS_ERR(h_dentry)) {
23079+ err = PTR_ERR(h_dentry);
febd17d6 23080+ goto out_unlock;
c2c0f25c
AM
23081+ }
23082+ }
23083+ if (unlikely(!h_dentry))
febd17d6 23084+ goto out_unlock;
1facf9fc 23085+
c2c0f25c 23086+ err = 0;
062440b3 23087+ AuDbg("%ps\n", h_inode->i_op->get_link);
c2c0f25c 23088+ AuDbgDentry(h_dentry);
f2c43d5f 23089+ ret = vfs_get_link(h_dentry, done);
c2c0f25c 23090+ dput(h_dentry);
febd17d6
JR
23091+ if (IS_ERR(ret))
23092+ err = PTR_ERR(ret);
c2c0f25c 23093+
c2c0f25c
AM
23094+out_unlock:
23095+ aufs_read_unlock(dentry, AuLock_IR);
4f0767ce 23096+out:
c2c0f25c
AM
23097+ if (unlikely(err))
23098+ ret = ERR_PTR(err);
23099+ AuTraceErrPtr(ret);
23100+ return ret;
4a4d8108 23101+}
1facf9fc 23102+
4a4d8108 23103+/* ---------------------------------------------------------------------- */
1facf9fc 23104+
e2f27e51
AM
23105+static int au_is_special(struct inode *inode)
23106+{
23107+ return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
23108+}
23109+
631230db 23110+static int aufs_update_time(struct inode *inode, int flags)
4a4d8108 23111+{
0c3ec466 23112+ int err;
e2f27e51 23113+ aufs_bindex_t bindex;
0c3ec466
AM
23114+ struct super_block *sb;
23115+ struct inode *h_inode;
e2f27e51 23116+ struct vfsmount *h_mnt;
0c3ec466
AM
23117+
23118+ sb = inode->i_sb;
e2f27e51
AM
23119+ WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
23120+ "unexpected s_flags 0x%lx", sb->s_flags);
23121+
0c3ec466
AM
23122+ /* mmap_sem might be acquired already, cf. aufs_mmap() */
23123+ lockdep_off();
23124+ si_read_lock(sb, AuLock_FLUSH);
23125+ ii_write_lock_child(inode);
e2f27e51
AM
23126+
23127+ err = 0;
23128+ bindex = au_ibtop(inode);
23129+ h_inode = au_h_iptr(inode, bindex);
23130+ if (!au_test_ro(sb, bindex, inode)) {
23131+ h_mnt = au_sbr_mnt(sb, bindex);
23132+ err = vfsub_mnt_want_write(h_mnt);
23133+ if (!err) {
631230db 23134+ err = vfsub_update_time(h_inode, flags);
e2f27e51
AM
23135+ vfsub_mnt_drop_write(h_mnt);
23136+ }
23137+ } else if (au_is_special(h_inode)) {
23138+ /*
23139+ * Never copy-up here.
23140+ * These special files may already be opened and used for
23141+ * communicating. If we copied it up, then the communication
23142+ * would be corrupted.
23143+ */
23144+ AuWarn1("timestamps for i%lu are ignored "
23145+ "since it is on readonly branch (hi%lu).\n",
23146+ inode->i_ino, h_inode->i_ino);
23147+ } else if (flags & ~S_ATIME) {
23148+ err = -EIO;
23149+ AuIOErr1("unexpected flags 0x%x\n", flags);
23150+ AuDebugOn(1);
23151+ }
23152+
38d290e6
JR
23153+ if (!err)
23154+ au_cpup_attr_timesizes(inode);
0c3ec466
AM
23155+ ii_write_unlock(inode);
23156+ si_read_unlock(sb);
23157+ lockdep_on();
38d290e6
JR
23158+
23159+ if (!err && (flags & S_VERSION))
23160+ inode_inc_iversion(inode);
23161+
0c3ec466 23162+ return err;
4a4d8108 23163+}
1facf9fc 23164+
4a4d8108 23165+/* ---------------------------------------------------------------------- */
1308ab2a 23166+
b95c5147
AM
23167+/* no getattr version will be set by module.c:aufs_init() */
23168+struct inode_operations aufs_iop_nogetattr[AuIop_Last],
23169+ aufs_iop[] = {
23170+ [AuIop_SYMLINK] = {
23171+ .permission = aufs_permission,
c1595e42 23172+#ifdef CONFIG_FS_POSIX_ACL
aec24f17 23173+ .get_inode_acl = aufs_get_inode_acl,
b95c5147
AM
23174+ .get_acl = aufs_get_acl,
23175+ .set_acl = aufs_set_acl, /* unsupport for symlink? */
c1595e42
JR
23176+#endif
23177+
b95c5147
AM
23178+ .setattr = aufs_setattr,
23179+ .getattr = aufs_getattr,
0c3ec466 23180+
c1595e42 23181+#ifdef CONFIG_AUFS_XATTR
b95c5147 23182+ .listxattr = aufs_listxattr,
c1595e42
JR
23183+#endif
23184+
0dcfbb52 23185+ .get_link = aufs_get_link
0c3ec466 23186+
b95c5147
AM
23187+ /* .update_time = aufs_update_time */
23188+ },
23189+ [AuIop_DIR] = {
23190+ .create = aufs_create,
23191+ .lookup = aufs_lookup,
23192+ .link = aufs_link,
23193+ .unlink = aufs_unlink,
23194+ .symlink = aufs_symlink,
23195+ .mkdir = aufs_mkdir,
23196+ .rmdir = aufs_rmdir,
23197+ .mknod = aufs_mknod,
23198+ .rename = aufs_rename,
23199+
23200+ .permission = aufs_permission,
c1595e42 23201+#ifdef CONFIG_FS_POSIX_ACL
aec24f17 23202+ .get_inode_acl = aufs_get_inode_acl,
b95c5147
AM
23203+ .get_acl = aufs_get_acl,
23204+ .set_acl = aufs_set_acl,
c1595e42
JR
23205+#endif
23206+
b95c5147
AM
23207+ .setattr = aufs_setattr,
23208+ .getattr = aufs_getattr,
0c3ec466 23209+
c1595e42 23210+#ifdef CONFIG_AUFS_XATTR
b95c5147 23211+ .listxattr = aufs_listxattr,
c1595e42
JR
23212+#endif
23213+
b95c5147
AM
23214+ .update_time = aufs_update_time,
23215+ .atomic_open = aufs_atomic_open,
23216+ .tmpfile = aufs_tmpfile
23217+ },
23218+ [AuIop_OTHER] = {
23219+ .permission = aufs_permission,
c1595e42 23220+#ifdef CONFIG_FS_POSIX_ACL
aec24f17 23221+ .get_inode_acl = aufs_get_inode_acl,
b95c5147
AM
23222+ .get_acl = aufs_get_acl,
23223+ .set_acl = aufs_set_acl,
c1595e42
JR
23224+#endif
23225+
b95c5147
AM
23226+ .setattr = aufs_setattr,
23227+ .getattr = aufs_getattr,
0c3ec466 23228+
c1595e42 23229+#ifdef CONFIG_AUFS_XATTR
b95c5147 23230+ .listxattr = aufs_listxattr,
c1595e42
JR
23231+#endif
23232+
b95c5147
AM
23233+ .update_time = aufs_update_time
23234+ }
4a4d8108 23235+};
7f207e10 23236diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
eca34b5c 23237--- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
631230db
AM
23238+++ linux/fs/aufs/i_op_del.c 2023-10-31 09:31:04.199880750 +0100
23239@@ -0,0 +1,523 @@
cd7a4cd9 23240+// SPDX-License-Identifier: GPL-2.0
1facf9fc 23241+/*
0dcfbb52 23242+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 23243+ *
0dcfbb52 23244+ * This program is free software; you can redistribute it and/or modify
1facf9fc 23245+ * it under the terms of the GNU General Public License as published by
23246+ * the Free Software Foundation; either version 2 of the License, or
23247+ * (at your option) any later version.
dece6358
AM
23248+ *
23249+ * This program is distributed in the hope that it will be useful,
23250+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
23251+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23252+ * GNU General Public License for more details.
23253+ *
23254+ * You should have received a copy of the GNU General Public License
523b37e3 23255+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 23256+ */
23257+
23258+/*
4a4d8108 23259+ * inode operations (del entry)
1308ab2a 23260+ */
dece6358 23261+
eca801bf 23262+#include <linux/iversion.h>
1308ab2a 23263+#include "aufs.h"
dece6358 23264+
4a4d8108
AM
23265+/*
23266+ * decide if a new whiteout for @dentry is necessary or not.
23267+ * when it is necessary, prepare the parent dir for the upper branch whose
23268+ * branch index is @bcpup for creation. the actual creation of the whiteout will
23269+ * be done by caller.
23270+ * return value:
23271+ * 0: wh is unnecessary
23272+ * plus: wh is necessary
23273+ * minus: error
23274+ */
23275+int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
1308ab2a 23276+{
4a4d8108 23277+ int need_wh, err;
5afbbe0d 23278+ aufs_bindex_t btop;
4a4d8108 23279+ struct super_block *sb;
dece6358 23280+
4a4d8108 23281+ sb = dentry->d_sb;
5afbbe0d 23282+ btop = au_dbtop(dentry);
4a4d8108 23283+ if (*bcpup < 0) {
5afbbe0d
AM
23284+ *bcpup = btop;
23285+ if (au_test_ro(sb, btop, d_inode(dentry))) {
4a4d8108
AM
23286+ err = AuWbrCopyup(au_sbi(sb), dentry);
23287+ *bcpup = err;
23288+ if (unlikely(err < 0))
23289+ goto out;
23290+ }
23291+ } else
5afbbe0d 23292+ AuDebugOn(btop < *bcpup
5527c038 23293+ || au_test_ro(sb, *bcpup, d_inode(dentry)));
5afbbe0d 23294+ AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
1308ab2a 23295+
5afbbe0d 23296+ if (*bcpup != btop) {
4a4d8108
AM
23297+ err = au_cpup_dirs(dentry, *bcpup);
23298+ if (unlikely(err))
23299+ goto out;
23300+ need_wh = 1;
23301+ } else {
027c5e7a 23302+ struct au_dinfo *dinfo, *tmp;
4a4d8108 23303+
027c5e7a
AM
23304+ need_wh = -ENOMEM;
23305+ dinfo = au_di(dentry);
23306+ tmp = au_di_alloc(sb, AuLsc_DI_TMP);
23307+ if (tmp) {
23308+ au_di_cp(tmp, dinfo);
23309+ au_di_swap(tmp, dinfo);
23310+ /* returns the number of positive dentries */
5afbbe0d
AM
23311+ need_wh = au_lkup_dentry(dentry, btop + 1,
23312+ /* AuLkup_IGNORE_PERM */ 0);
027c5e7a
AM
23313+ au_di_swap(tmp, dinfo);
23314+ au_rw_write_unlock(&tmp->di_rwsem);
23315+ au_di_free(tmp);
4a4d8108
AM
23316+ }
23317+ }
23318+ AuDbg("need_wh %d\n", need_wh);
23319+ err = need_wh;
23320+
4f0767ce 23321+out:
4a4d8108 23322+ return err;
1facf9fc 23323+}
23324+
4a4d8108
AM
23325+/*
23326+ * simple tests for the del-entry operations.
23327+ * following the checks in vfs, plus the parent-child relationship.
23328+ */
23329+int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
23330+ struct dentry *h_parent, int isdir)
1facf9fc 23331+{
4a4d8108
AM
23332+ int err;
23333+ umode_t h_mode;
23334+ struct dentry *h_dentry, *h_latest;
1308ab2a 23335+ struct inode *h_inode;
758e9dad
AM
23336+ struct path h_ppath;
23337+ struct super_block *sb;
23338+ struct au_branch *br;
5db017eb 23339+ struct mnt_idmap *h_idmap;
1facf9fc 23340+
4a4d8108 23341+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 23342+ if (d_really_is_positive(dentry)) {
4a4d8108 23343+ err = -ENOENT;
5527c038
JR
23344+ if (unlikely(d_is_negative(h_dentry)))
23345+ goto out;
23346+ h_inode = d_inode(h_dentry);
23347+ if (unlikely(!h_inode->i_nlink))
4a4d8108 23348+ goto out;
1facf9fc 23349+
4a4d8108
AM
23350+ h_mode = h_inode->i_mode;
23351+ if (!isdir) {
23352+ err = -EISDIR;
23353+ if (unlikely(S_ISDIR(h_mode)))
23354+ goto out;
23355+ } else if (unlikely(!S_ISDIR(h_mode))) {
23356+ err = -ENOTDIR;
23357+ goto out;
23358+ }
23359+ } else {
23360+ /* rename(2) case */
23361+ err = -EIO;
5527c038 23362+ if (unlikely(d_is_positive(h_dentry)))
4a4d8108
AM
23363+ goto out;
23364+ }
1facf9fc 23365+
4a4d8108
AM
23366+ err = -ENOENT;
23367+ /* expected parent dir is locked */
23368+ if (unlikely(h_parent != h_dentry->d_parent))
23369+ goto out;
23370+ err = 0;
23371+
23372+ /*
23373+ * rmdir a dir may break the consistency on some filesystem.
23374+ * let's try heavy test.
23375+ */
23376+ err = -EACCES;
758e9dad
AM
23377+ sb = dentry->d_sb;
23378+ br = au_sbr(sb, bindex);
5db017eb 23379+ h_idmap = au_br_idmap(br);
758e9dad 23380+ if (unlikely(!au_opt_test(au_mntflags(sb), DIRPERM1)
5db017eb 23381+ && au_test_h_perm(h_idmap, d_inode(h_parent),
076b876e 23382+ MAY_EXEC | MAY_WRITE)))
4a4d8108
AM
23383+ goto out;
23384+
758e9dad
AM
23385+ h_ppath.dentry = h_parent;
23386+ h_ppath.mnt = au_br_mnt(br);
5db017eb 23387+ h_latest = au_sio_lkup_one(h_idmap, &dentry->d_name, &h_ppath);
4a4d8108
AM
23388+ err = -EIO;
23389+ if (IS_ERR(h_latest))
23390+ goto out;
23391+ if (h_latest == h_dentry)
23392+ err = 0;
23393+ dput(h_latest);
23394+
4f0767ce 23395+out:
4a4d8108 23396+ return err;
1308ab2a 23397+}
1facf9fc 23398+
4a4d8108
AM
23399+/*
23400+ * decide the branch where we operate for @dentry. the branch index will be set
acd2b654 23401+ * @rbcpup. after deciding it, 'pin' it and store the timestamps of the parent
4a4d8108
AM
23402+ * dir for reverting.
23403+ * when a new whiteout is necessary, create it.
23404+ */
23405+static struct dentry*
23406+lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
23407+ struct au_dtime *dt, struct au_pin *pin)
1308ab2a 23408+{
4a4d8108
AM
23409+ struct dentry *wh_dentry;
23410+ struct super_block *sb;
23411+ struct path h_path;
23412+ int err, need_wh;
23413+ unsigned int udba;
23414+ aufs_bindex_t bcpup;
dece6358 23415+
4a4d8108
AM
23416+ need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
23417+ wh_dentry = ERR_PTR(need_wh);
23418+ if (unlikely(need_wh < 0))
23419+ goto out;
23420+
23421+ sb = dentry->d_sb;
23422+ udba = au_opt_udba(sb);
23423+ bcpup = *rbcpup;
23424+ err = au_pin(pin, dentry, bcpup, udba,
23425+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23426+ wh_dentry = ERR_PTR(err);
23427+ if (unlikely(err))
23428+ goto out;
23429+
23430+ h_path.dentry = au_pinned_h_parent(pin);
23431+ if (udba != AuOpt_UDBA_NONE
5afbbe0d 23432+ && au_dbtop(dentry) == bcpup) {
4a4d8108
AM
23433+ err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
23434+ wh_dentry = ERR_PTR(err);
23435+ if (unlikely(err))
23436+ goto out_unpin;
23437+ }
23438+
23439+ h_path.mnt = au_sbr_mnt(sb, bcpup);
23440+ au_dtime_store(dt, au_pinned_parent(pin), &h_path);
23441+ wh_dentry = NULL;
23442+ if (!need_wh)
23443+ goto out; /* success, no need to create whiteout */
23444+
23445+ wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
23446+ if (IS_ERR(wh_dentry))
23447+ goto out_unpin;
23448+
23449+ /* returns with the parent is locked and wh_dentry is dget-ed */
23450+ goto out; /* success */
23451+
4f0767ce 23452+out_unpin:
4a4d8108 23453+ au_unpin(pin);
4f0767ce 23454+out:
4a4d8108 23455+ return wh_dentry;
1facf9fc 23456+}
23457+
4a4d8108
AM
23458+/*
23459+ * when removing a dir, rename it to a unique temporary whiteout-ed name first
23460+ * in order to be revertible and save time for removing many child whiteouts
23461+ * under the dir.
23462+ * returns 1 when there are too many child whiteout and caller should remove
23463+ * them asynchronously. returns 0 when the number of children is enough small to
23464+ * remove now or the branch fs is a remote fs.
23465+ * otherwise return an error.
23466+ */
23467+static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
23468+ struct au_nhash *whlist, struct inode *dir)
1facf9fc 23469+{
4a4d8108
AM
23470+ int rmdir_later, err, dirwh;
23471+ struct dentry *h_dentry;
23472+ struct super_block *sb;
5527c038 23473+ struct inode *inode;
4a4d8108
AM
23474+
23475+ sb = dentry->d_sb;
23476+ SiMustAnyLock(sb);
23477+ h_dentry = au_h_dptr(dentry, bindex);
23478+ err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
23479+ if (unlikely(err))
23480+ goto out;
23481+
23482+ /* stop monitoring */
5527c038
JR
23483+ inode = d_inode(dentry);
23484+ au_hn_free(au_hi(inode, bindex));
4a4d8108
AM
23485+
23486+ if (!au_test_fs_remote(h_dentry->d_sb)) {
23487+ dirwh = au_sbi(sb)->si_dirwh;
23488+ rmdir_later = (dirwh <= 1);
23489+ if (!rmdir_later)
23490+ rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
23491+ dirwh);
23492+ if (rmdir_later)
23493+ return rmdir_later;
23494+ }
1facf9fc 23495+
4a4d8108
AM
23496+ err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
23497+ if (unlikely(err)) {
523b37e3
AM
23498+ AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
23499+ h_dentry, bindex, err);
4a4d8108
AM
23500+ err = 0;
23501+ }
dece6358 23502+
4f0767ce 23503+out:
4a4d8108
AM
23504+ AuTraceErr(err);
23505+ return err;
23506+}
1308ab2a 23507+
4a4d8108
AM
23508+/*
23509+ * final procedure for deleting a entry.
23510+ * maintain dentry and iattr.
23511+ */
23512+static void epilog(struct inode *dir, struct dentry *dentry,
23513+ aufs_bindex_t bindex)
23514+{
23515+ struct inode *inode;
1308ab2a 23516+
5527c038 23517+ inode = d_inode(dentry);
4a4d8108 23518+ d_drop(dentry);
631230db 23519+ inode_set_ctime_to_ts(inode, inode_get_ctime(dir));
1308ab2a 23520+
b912730e 23521+ au_dir_ts(dir, bindex);
be118d29 23522+ inode_inc_iversion(dir);
1facf9fc 23523+}
23524+
4a4d8108
AM
23525+/*
23526+ * when an error happened, remove the created whiteout and revert everything.
23527+ */
7f207e10
AM
23528+static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
23529+ aufs_bindex_t bwh, struct dentry *wh_dentry,
23530+ struct dentry *dentry, struct au_dtime *dt)
1facf9fc 23531+{
4a4d8108
AM
23532+ int rerr;
23533+ struct path h_path = {
23534+ .dentry = wh_dentry,
7f207e10 23535+ .mnt = au_sbr_mnt(dir->i_sb, bindex)
4a4d8108 23536+ };
dece6358 23537+
7f207e10 23538+ rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
4a4d8108
AM
23539+ if (!rerr) {
23540+ au_set_dbwh(dentry, bwh);
23541+ au_dtime_revert(dt);
23542+ return 0;
23543+ }
dece6358 23544+
523b37e3 23545+ AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
4a4d8108 23546+ return -EIO;
1facf9fc 23547+}
23548+
4a4d8108 23549+/* ---------------------------------------------------------------------- */
1facf9fc 23550+
4a4d8108 23551+int aufs_unlink(struct inode *dir, struct dentry *dentry)
1308ab2a 23552+{
4a4d8108 23553+ int err;
5afbbe0d 23554+ aufs_bindex_t bwh, bindex, btop;
631230db 23555+ struct inode *inode, *h_dir, *delegated, *h_inode;
4a4d8108 23556+ struct dentry *parent, *wh_dentry;
acd2b654 23557+ /* to reduce stack size */
c2b27bf2
AM
23558+ struct {
23559+ struct au_dtime dt;
23560+ struct au_pin pin;
23561+ struct path h_path;
23562+ } *a;
1facf9fc 23563+
4a4d8108 23564+ IMustLock(dir);
027c5e7a 23565+
c2b27bf2
AM
23566+ err = -ENOMEM;
23567+ a = kmalloc(sizeof(*a), GFP_NOFS);
23568+ if (unlikely(!a))
23569+ goto out;
23570+
027c5e7a
AM
23571+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
23572+ if (unlikely(err))
c2b27bf2 23573+ goto out_free;
027c5e7a
AM
23574+ err = au_d_hashed_positive(dentry);
23575+ if (unlikely(err))
23576+ goto out_unlock;
5527c038 23577+ inode = d_inode(dentry);
4a4d8108 23578+ IMustLock(inode);
027c5e7a 23579+ err = -EISDIR;
2000de60 23580+ if (unlikely(d_is_dir(dentry)))
027c5e7a 23581+ goto out_unlock; /* possible? */
1facf9fc 23582+
5afbbe0d 23583+ btop = au_dbtop(dentry);
4a4d8108
AM
23584+ bwh = au_dbwh(dentry);
23585+ bindex = -1;
027c5e7a
AM
23586+ parent = dentry->d_parent; /* dir inode is locked */
23587+ di_write_lock_parent(parent);
c2b27bf2
AM
23588+ wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
23589+ &a->pin);
4a4d8108
AM
23590+ err = PTR_ERR(wh_dentry);
23591+ if (IS_ERR(wh_dentry))
027c5e7a 23592+ goto out_parent;
1facf9fc 23593+
5afbbe0d
AM
23594+ a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
23595+ a->h_path.dentry = au_h_dptr(dentry, btop);
c2b27bf2 23596+ dget(a->h_path.dentry);
5afbbe0d 23597+ if (bindex == btop) {
c2b27bf2 23598+ h_dir = au_pinned_h_dir(&a->pin);
523b37e3
AM
23599+ delegated = NULL;
23600+ err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
23601+ if (unlikely(err == -EWOULDBLOCK)) {
23602+ pr_warn("cannot retry for NFSv4 delegation"
23603+ " for an internal unlink\n");
23604+ iput(delegated);
23605+ }
4a4d8108
AM
23606+ } else {
23607+ /* dir inode is locked */
5527c038 23608+ h_dir = d_inode(wh_dentry->d_parent);
4a4d8108
AM
23609+ IMustLock(h_dir);
23610+ err = 0;
23611+ }
dece6358 23612+
4a4d8108 23613+ if (!err) {
7f207e10 23614+ vfsub_drop_nlink(inode);
4a4d8108
AM
23615+ epilog(dir, dentry, bindex);
23616+
23617+ /* update target timestamps */
5afbbe0d 23618+ if (bindex == btop) {
c2b27bf2
AM
23619+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
23620+ /*ignore*/
631230db
AM
23621+ h_inode = d_inode(a->h_path.dentry);
23622+ inode_set_ctime_to_ts(inode, inode_get_ctime(h_inode));
4a4d8108
AM
23623+ } else
23624+ /* todo: this timestamp may be reverted later */
631230db 23625+ inode_set_ctime_to_ts(inode, inode_get_ctime(h_dir));
027c5e7a 23626+ goto out_unpin; /* success */
1facf9fc 23627+ }
23628+
4a4d8108
AM
23629+ /* revert */
23630+ if (wh_dentry) {
23631+ int rerr;
23632+
c2b27bf2
AM
23633+ rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
23634+ &a->dt);
4a4d8108
AM
23635+ if (rerr)
23636+ err = rerr;
dece6358 23637+ }
1facf9fc 23638+
027c5e7a 23639+out_unpin:
c2b27bf2 23640+ au_unpin(&a->pin);
4a4d8108 23641+ dput(wh_dentry);
c2b27bf2 23642+ dput(a->h_path.dentry);
027c5e7a 23643+out_parent:
4a4d8108 23644+ di_write_unlock(parent);
027c5e7a 23645+out_unlock:
4a4d8108 23646+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2 23647+out_free:
9f237c51 23648+ au_kfree_rcu(a);
027c5e7a 23649+out:
4a4d8108 23650+ return err;
dece6358
AM
23651+}
23652+
4a4d8108 23653+int aufs_rmdir(struct inode *dir, struct dentry *dentry)
1308ab2a 23654+{
4a4d8108 23655+ int err, rmdir_later;
5afbbe0d 23656+ aufs_bindex_t bwh, bindex, btop;
4a4d8108
AM
23657+ struct inode *inode;
23658+ struct dentry *parent, *wh_dentry, *h_dentry;
23659+ struct au_whtmp_rmdir *args;
acd2b654 23660+ /* to reduce stack size */
c2b27bf2
AM
23661+ struct {
23662+ struct au_dtime dt;
23663+ struct au_pin pin;
23664+ } *a;
1facf9fc 23665+
4a4d8108 23666+ IMustLock(dir);
027c5e7a 23667+
c2b27bf2
AM
23668+ err = -ENOMEM;
23669+ a = kmalloc(sizeof(*a), GFP_NOFS);
23670+ if (unlikely(!a))
23671+ goto out;
23672+
027c5e7a
AM
23673+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
23674+ if (unlikely(err))
c2b27bf2 23675+ goto out_free;
53392da6
AM
23676+ err = au_alive_dir(dentry);
23677+ if (unlikely(err))
027c5e7a 23678+ goto out_unlock;
5527c038 23679+ inode = d_inode(dentry);
4a4d8108 23680+ IMustLock(inode);
027c5e7a 23681+ err = -ENOTDIR;
2000de60 23682+ if (unlikely(!d_is_dir(dentry)))
027c5e7a 23683+ goto out_unlock; /* possible? */
dece6358 23684+
4a4d8108
AM
23685+ err = -ENOMEM;
23686+ args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
23687+ if (unlikely(!args))
23688+ goto out_unlock;
dece6358 23689+
4a4d8108
AM
23690+ parent = dentry->d_parent; /* dir inode is locked */
23691+ di_write_lock_parent(parent);
23692+ err = au_test_empty(dentry, &args->whlist);
23693+ if (unlikely(err))
027c5e7a 23694+ goto out_parent;
1facf9fc 23695+
5afbbe0d 23696+ btop = au_dbtop(dentry);
4a4d8108
AM
23697+ bwh = au_dbwh(dentry);
23698+ bindex = -1;
c2b27bf2
AM
23699+ wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
23700+ &a->pin);
4a4d8108
AM
23701+ err = PTR_ERR(wh_dentry);
23702+ if (IS_ERR(wh_dentry))
027c5e7a 23703+ goto out_parent;
1facf9fc 23704+
5afbbe0d 23705+ h_dentry = au_h_dptr(dentry, btop);
4a4d8108
AM
23706+ dget(h_dentry);
23707+ rmdir_later = 0;
5afbbe0d
AM
23708+ if (bindex == btop) {
23709+ err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
4a4d8108
AM
23710+ if (err > 0) {
23711+ rmdir_later = err;
23712+ err = 0;
23713+ }
23714+ } else {
23715+ /* stop monitoring */
5afbbe0d 23716+ au_hn_free(au_hi(inode, btop));
4a4d8108
AM
23717+
23718+ /* dir inode is locked */
5527c038 23719+ IMustLock(d_inode(wh_dentry->d_parent));
1facf9fc 23720+ err = 0;
23721+ }
23722+
4a4d8108 23723+ if (!err) {
027c5e7a 23724+ vfsub_dead_dir(inode);
4a4d8108
AM
23725+ au_set_dbdiropq(dentry, -1);
23726+ epilog(dir, dentry, bindex);
1308ab2a 23727+
4a4d8108 23728+ if (rmdir_later) {
5afbbe0d 23729+ au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
4a4d8108
AM
23730+ args = NULL;
23731+ }
1308ab2a 23732+
4a4d8108 23733+ goto out_unpin; /* success */
1facf9fc 23734+ }
23735+
4a4d8108
AM
23736+ /* revert */
23737+ AuLabel(revert);
23738+ if (wh_dentry) {
23739+ int rerr;
1308ab2a 23740+
c2b27bf2
AM
23741+ rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
23742+ &a->dt);
4a4d8108
AM
23743+ if (rerr)
23744+ err = rerr;
1facf9fc 23745+ }
23746+
4f0767ce 23747+out_unpin:
c2b27bf2 23748+ au_unpin(&a->pin);
4a4d8108
AM
23749+ dput(wh_dentry);
23750+ dput(h_dentry);
027c5e7a 23751+out_parent:
4a4d8108
AM
23752+ di_write_unlock(parent);
23753+ if (args)
23754+ au_whtmp_rmdir_free(args);
4f0767ce 23755+out_unlock:
4a4d8108 23756+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2 23757+out_free:
9f237c51 23758+ au_kfree_rcu(a);
4f0767ce 23759+out:
4a4d8108
AM
23760+ AuTraceErr(err);
23761+ return err;
dece6358 23762+}
7f207e10 23763diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
eca34b5c 23764--- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
631230db
AM
23765+++ linux/fs/aufs/i_op_ren.c 2023-10-31 09:31:04.199880750 +0100
23766@@ -0,0 +1,1260 @@
cd7a4cd9 23767+// SPDX-License-Identifier: GPL-2.0
1facf9fc 23768+/*
0dcfbb52 23769+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 23770+ *
0dcfbb52 23771+ * This program is free software; you can redistribute it and/or modify
1facf9fc 23772+ * it under the terms of the GNU General Public License as published by
23773+ * the Free Software Foundation; either version 2 of the License, or
23774+ * (at your option) any later version.
dece6358
AM
23775+ *
23776+ * This program is distributed in the hope that it will be useful,
23777+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
23778+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23779+ * GNU General Public License for more details.
23780+ *
23781+ * You should have received a copy of the GNU General Public License
523b37e3 23782+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 23783+ */
23784+
23785+/*
4a4d8108
AM
23786+ * inode operation (rename entry)
23787+ * todo: this is crazy monster
1facf9fc 23788+ */
23789+
eca801bf 23790+#include <linux/iversion.h>
1facf9fc 23791+#include "aufs.h"
23792+
4a4d8108
AM
23793+enum { AuSRC, AuDST, AuSrcDst };
23794+enum { AuPARENT, AuCHILD, AuParentChild };
1facf9fc 23795+
f2c43d5f
AM
23796+#define AuRen_ISDIR_SRC 1
23797+#define AuRen_ISDIR_DST (1 << 1)
23798+#define AuRen_ISSAMEDIR (1 << 2)
23799+#define AuRen_WHSRC (1 << 3)
23800+#define AuRen_WHDST (1 << 4)
23801+#define AuRen_MNT_WRITE (1 << 5)
23802+#define AuRen_DT_DSTDIR (1 << 6)
23803+#define AuRen_DIROPQ_SRC (1 << 7)
23804+#define AuRen_DIROPQ_DST (1 << 8)
8b6a4947
AM
23805+#define AuRen_DIRREN (1 << 9)
23806+#define AuRen_DROPPED_SRC (1 << 10)
23807+#define AuRen_DROPPED_DST (1 << 11)
4a4d8108 23808+#define au_ftest_ren(flags, name) ((flags) & AuRen_##name)
7f207e10
AM
23809+#define au_fset_ren(flags, name) \
23810+ do { (flags) |= AuRen_##name; } while (0)
23811+#define au_fclr_ren(flags, name) \
23812+ do { (flags) &= ~AuRen_##name; } while (0)
1facf9fc 23813+
8b6a4947
AM
23814+#ifndef CONFIG_AUFS_DIRREN
23815+#undef AuRen_DIRREN
23816+#define AuRen_DIRREN 0
23817+#endif
23818+
4a4d8108
AM
23819+struct au_ren_args {
23820+ struct {
23821+ struct dentry *dentry, *h_dentry, *parent, *h_parent,
23822+ *wh_dentry;
23823+ struct inode *dir, *inode;
f2c43d5f 23824+ struct au_hinode *hdir, *hinode;
4a4d8108 23825+ struct au_dtime dt[AuParentChild];
f2c43d5f 23826+ aufs_bindex_t btop, bdiropq;
4a4d8108 23827+ } sd[AuSrcDst];
1facf9fc 23828+
4a4d8108
AM
23829+#define src_dentry sd[AuSRC].dentry
23830+#define src_dir sd[AuSRC].dir
23831+#define src_inode sd[AuSRC].inode
23832+#define src_h_dentry sd[AuSRC].h_dentry
23833+#define src_parent sd[AuSRC].parent
23834+#define src_h_parent sd[AuSRC].h_parent
23835+#define src_wh_dentry sd[AuSRC].wh_dentry
23836+#define src_hdir sd[AuSRC].hdir
f2c43d5f 23837+#define src_hinode sd[AuSRC].hinode
4a4d8108
AM
23838+#define src_h_dir sd[AuSRC].hdir->hi_inode
23839+#define src_dt sd[AuSRC].dt
5afbbe0d 23840+#define src_btop sd[AuSRC].btop
f2c43d5f 23841+#define src_bdiropq sd[AuSRC].bdiropq
1facf9fc 23842+
4a4d8108
AM
23843+#define dst_dentry sd[AuDST].dentry
23844+#define dst_dir sd[AuDST].dir
23845+#define dst_inode sd[AuDST].inode
23846+#define dst_h_dentry sd[AuDST].h_dentry
23847+#define dst_parent sd[AuDST].parent
23848+#define dst_h_parent sd[AuDST].h_parent
23849+#define dst_wh_dentry sd[AuDST].wh_dentry
23850+#define dst_hdir sd[AuDST].hdir
f2c43d5f 23851+#define dst_hinode sd[AuDST].hinode
4a4d8108
AM
23852+#define dst_h_dir sd[AuDST].hdir->hi_inode
23853+#define dst_dt sd[AuDST].dt
5afbbe0d 23854+#define dst_btop sd[AuDST].btop
f2c43d5f 23855+#define dst_bdiropq sd[AuDST].bdiropq
4a4d8108
AM
23856+
23857+ struct dentry *h_trap;
23858+ struct au_branch *br;
4a4d8108
AM
23859+ struct path h_path;
23860+ struct au_nhash whlist;
f2c43d5f 23861+ aufs_bindex_t btgt, src_bwh;
1facf9fc 23862+
f2c43d5f
AM
23863+ struct {
23864+ unsigned short auren_flags;
23865+ unsigned char flags; /* syscall parameter */
23866+ unsigned char exchange;
23867+ } __packed;
1facf9fc 23868+
4a4d8108
AM
23869+ struct au_whtmp_rmdir *thargs;
23870+ struct dentry *h_dst;
8b6a4947 23871+ struct au_hinode *h_root;
4a4d8108 23872+};
1308ab2a 23873+
4a4d8108 23874+/* ---------------------------------------------------------------------- */
1308ab2a 23875+
4a4d8108
AM
23876+/*
23877+ * functions for reverting.
23878+ * when an error happened in a single rename systemcall, we should revert
79b8bda9 23879+ * everything as if nothing happened.
4a4d8108
AM
23880+ * we don't need to revert the copied-up/down the parent dir since they are
23881+ * harmless.
23882+ */
1facf9fc 23883+
4a4d8108
AM
23884+#define RevertFailure(fmt, ...) do { \
23885+ AuIOErr("revert failure: " fmt " (%d, %d)\n", \
23886+ ##__VA_ARGS__, err, rerr); \
23887+ err = -EIO; \
23888+} while (0)
1facf9fc 23889+
f2c43d5f 23890+static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
1facf9fc 23891+{
4a4d8108 23892+ int rerr;
f2c43d5f
AM
23893+ struct dentry *d;
23894+#define src_or_dst(member) a->sd[idx].member
1facf9fc 23895+
f2c43d5f
AM
23896+ d = src_or_dst(dentry); /* {src,dst}_dentry */
23897+ au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
23898+ rerr = au_diropq_remove(d, a->btgt);
23899+ au_hn_inode_unlock(src_or_dst(hinode));
23900+ au_set_dbdiropq(d, src_or_dst(bdiropq));
4a4d8108 23901+ if (rerr)
f2c43d5f
AM
23902+ RevertFailure("remove diropq %pd", d);
23903+
23904+#undef src_or_dst_
23905+}
23906+
23907+static void au_ren_rev_diropq(int err, struct au_ren_args *a)
23908+{
23909+ if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
23910+ au_ren_do_rev_diropq(err, a, AuSRC);
23911+ if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
23912+ au_ren_do_rev_diropq(err, a, AuDST);
4a4d8108 23913+}
1facf9fc 23914+
4a4d8108
AM
23915+static void au_ren_rev_rename(int err, struct au_ren_args *a)
23916+{
23917+ int rerr;
523b37e3 23918+ struct inode *delegated;
758e9dad
AM
23919+ struct path h_ppath = {
23920+ .dentry = a->src_h_parent,
23921+ .mnt = a->h_path.mnt
23922+ };
1facf9fc 23923+
758e9dad 23924+ a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name, &h_ppath);
4a4d8108
AM
23925+ rerr = PTR_ERR(a->h_path.dentry);
23926+ if (IS_ERR(a->h_path.dentry)) {
523b37e3 23927+ RevertFailure("lkup one %pd", a->src_dentry);
4a4d8108 23928+ return;
1facf9fc 23929+ }
23930+
523b37e3 23931+ delegated = NULL;
4a4d8108
AM
23932+ rerr = vfsub_rename(a->dst_h_dir,
23933+ au_h_dptr(a->src_dentry, a->btgt),
f2c43d5f 23934+ a->src_h_dir, &a->h_path, &delegated, a->flags);
523b37e3
AM
23935+ if (unlikely(rerr == -EWOULDBLOCK)) {
23936+ pr_warn("cannot retry for NFSv4 delegation"
23937+ " for an internal rename\n");
23938+ iput(delegated);
23939+ }
4a4d8108
AM
23940+ d_drop(a->h_path.dentry);
23941+ dput(a->h_path.dentry);
23942+ /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
23943+ if (rerr)
523b37e3 23944+ RevertFailure("rename %pd", a->src_dentry);
1facf9fc 23945+}
23946+
4a4d8108 23947+static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
1facf9fc 23948+{
4a4d8108 23949+ int rerr;
523b37e3 23950+ struct inode *delegated;
758e9dad
AM
23951+ struct path h_ppath = {
23952+ .dentry = a->dst_h_parent,
23953+ .mnt = a->h_path.mnt
23954+ };
dece6358 23955+
758e9dad 23956+ a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name, &h_ppath);
4a4d8108
AM
23957+ rerr = PTR_ERR(a->h_path.dentry);
23958+ if (IS_ERR(a->h_path.dentry)) {
523b37e3 23959+ RevertFailure("lkup one %pd", a->dst_dentry);
4a4d8108
AM
23960+ return;
23961+ }
5527c038 23962+ if (d_is_positive(a->h_path.dentry)) {
4a4d8108
AM
23963+ d_drop(a->h_path.dentry);
23964+ dput(a->h_path.dentry);
23965+ return;
dece6358
AM
23966+ }
23967+
523b37e3
AM
23968+ delegated = NULL;
23969+ rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
f2c43d5f 23970+ &delegated, a->flags);
523b37e3
AM
23971+ if (unlikely(rerr == -EWOULDBLOCK)) {
23972+ pr_warn("cannot retry for NFSv4 delegation"
23973+ " for an internal rename\n");
23974+ iput(delegated);
23975+ }
4a4d8108
AM
23976+ d_drop(a->h_path.dentry);
23977+ dput(a->h_path.dentry);
23978+ if (!rerr)
23979+ au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
23980+ else
523b37e3 23981+ RevertFailure("rename %pd", a->h_dst);
4a4d8108 23982+}
1308ab2a 23983+
4a4d8108
AM
23984+static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
23985+{
23986+ int rerr;
1308ab2a 23987+
4a4d8108
AM
23988+ a->h_path.dentry = a->src_wh_dentry;
23989+ rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
027c5e7a 23990+ au_set_dbwh(a->src_dentry, a->src_bwh);
4a4d8108 23991+ if (rerr)
523b37e3 23992+ RevertFailure("unlink %pd", a->src_wh_dentry);
4a4d8108 23993+}
4a4d8108 23994+#undef RevertFailure
1facf9fc 23995+
1308ab2a 23996+/* ---------------------------------------------------------------------- */
23997+
4a4d8108
AM
23998+/*
23999+ * when we have to copyup the renaming entry, do it with the rename-target name
24000+ * in order to minimize the cost (the later actual rename is unnecessary).
24001+ * otherwise rename it on the target branch.
24002+ */
24003+static int au_ren_or_cpup(struct au_ren_args *a)
1facf9fc 24004+{
dece6358 24005+ int err;
4a4d8108 24006+ struct dentry *d;
523b37e3 24007+ struct inode *delegated;
1facf9fc 24008+
4a4d8108 24009+ d = a->src_dentry;
5afbbe0d 24010+ if (au_dbtop(d) == a->btgt) {
4a4d8108 24011+ a->h_path.dentry = a->dst_h_dentry;
5afbbe0d 24012+ AuDebugOn(au_dbtop(d) != a->btgt);
523b37e3 24013+ delegated = NULL;
4a4d8108 24014+ err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
f2c43d5f
AM
24015+ a->dst_h_dir, &a->h_path, &delegated,
24016+ a->flags);
523b37e3
AM
24017+ if (unlikely(err == -EWOULDBLOCK)) {
24018+ pr_warn("cannot retry for NFSv4 delegation"
24019+ " for an internal rename\n");
24020+ iput(delegated);
24021+ }
c2b27bf2 24022+ } else
86dc4139 24023+ BUG();
1308ab2a 24024+
027c5e7a
AM
24025+ if (!err && a->h_dst)
24026+ /* it will be set to dinfo later */
24027+ dget(a->h_dst);
1facf9fc 24028+
dece6358
AM
24029+ return err;
24030+}
1facf9fc 24031+
4a4d8108
AM
24032+/* cf. aufs_rmdir() */
24033+static int au_ren_del_whtmp(struct au_ren_args *a)
dece6358 24034+{
4a4d8108
AM
24035+ int err;
24036+ struct inode *dir;
1facf9fc 24037+
4a4d8108
AM
24038+ dir = a->dst_dir;
24039+ SiMustAnyLock(dir->i_sb);
24040+ if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
24041+ au_sbi(dir->i_sb)->si_dirwh)
24042+ || au_test_fs_remote(a->h_dst->d_sb)) {
24043+ err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
24044+ if (unlikely(err))
523b37e3
AM
24045+ pr_warn("failed removing whtmp dir %pd (%d), "
24046+ "ignored.\n", a->h_dst, err);
4a4d8108
AM
24047+ } else {
24048+ au_nhash_wh_free(&a->thargs->whlist);
24049+ a->thargs->whlist = a->whlist;
24050+ a->whlist.nh_num = 0;
24051+ au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
24052+ dput(a->h_dst);
24053+ a->thargs = NULL;
24054+ }
24055+
24056+ return 0;
1308ab2a 24057+}
1facf9fc 24058+
4a4d8108 24059+/* make it 'opaque' dir. */
f2c43d5f 24060+static int au_ren_do_diropq(struct au_ren_args *a, int idx)
4a4d8108
AM
24061+{
24062+ int err;
f2c43d5f
AM
24063+ struct dentry *d, *diropq;
24064+#define src_or_dst(member) a->sd[idx].member
1facf9fc 24065+
4a4d8108 24066+ err = 0;
f2c43d5f
AM
24067+ d = src_or_dst(dentry); /* {src,dst}_dentry */
24068+ src_or_dst(bdiropq) = au_dbdiropq(d);
24069+ src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
24070+ au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
24071+ diropq = au_diropq_create(d, a->btgt);
24072+ au_hn_inode_unlock(src_or_dst(hinode));
4a4d8108
AM
24073+ if (IS_ERR(diropq))
24074+ err = PTR_ERR(diropq);
076b876e
AM
24075+ else
24076+ dput(diropq);
1facf9fc 24077+
f2c43d5f 24078+#undef src_or_dst_
4a4d8108
AM
24079+ return err;
24080+}
1facf9fc 24081+
f2c43d5f 24082+static int au_ren_diropq(struct au_ren_args *a)
4a4d8108
AM
24083+{
24084+ int err;
f2c43d5f
AM
24085+ unsigned char always;
24086+ struct dentry *d;
1facf9fc 24087+
f2c43d5f
AM
24088+ err = 0;
24089+ d = a->dst_dentry; /* already renamed on the branch */
24090+ always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
24091+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
8b6a4947 24092+ && !au_ftest_ren(a->auren_flags, DIRREN)
f2c43d5f
AM
24093+ && a->btgt != au_dbdiropq(a->src_dentry)
24094+ && (a->dst_wh_dentry
24095+ || a->btgt <= au_dbdiropq(d)
24096+ /* hide the lower to keep xino */
24097+ /* the lowers may not be a dir, but we hide them anyway */
24098+ || a->btgt < au_dbbot(d)
24099+ || always)) {
24100+ AuDbg("here\n");
24101+ err = au_ren_do_diropq(a, AuSRC);
24102+ if (unlikely(err))
4a4d8108 24103+ goto out;
f2c43d5f 24104+ au_fset_ren(a->auren_flags, DIROPQ_SRC);
4a4d8108 24105+ }
f2c43d5f
AM
24106+ if (!a->exchange)
24107+ goto out; /* success */
1facf9fc 24108+
f2c43d5f
AM
24109+ d = a->src_dentry; /* already renamed on the branch */
24110+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)
24111+ && a->btgt != au_dbdiropq(a->dst_dentry)
24112+ && (a->btgt < au_dbdiropq(d)
24113+ || a->btgt < au_dbbot(d)
24114+ || always)) {
24115+ AuDbgDentry(a->src_dentry);
24116+ AuDbgDentry(a->dst_dentry);
24117+ err = au_ren_do_diropq(a, AuDST);
4a4d8108 24118+ if (unlikely(err))
f2c43d5f
AM
24119+ goto out_rev_src;
24120+ au_fset_ren(a->auren_flags, DIROPQ_DST);
24121+ }
24122+ goto out; /* success */
dece6358 24123+
f2c43d5f
AM
24124+out_rev_src:
24125+ AuDbg("err %d, reverting src\n", err);
24126+ au_ren_rev_diropq(err, a);
24127+out:
24128+ return err;
24129+}
24130+
24131+static int do_rename(struct au_ren_args *a)
24132+{
24133+ int err;
24134+ struct dentry *d, *h_d;
631230db 24135+ struct inode *h_inode;
f2c43d5f
AM
24136+
24137+ if (!a->exchange) {
24138+ /* prepare workqueue args for asynchronous rmdir */
24139+ h_d = a->dst_h_dentry;
24140+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)
8b6a4947 24141+ /* && !au_ftest_ren(a->auren_flags, DIRREN) */
f2c43d5f
AM
24142+ && d_is_positive(h_d)) {
24143+ err = -ENOMEM;
24144+ a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
24145+ GFP_NOFS);
24146+ if (unlikely(!a->thargs))
24147+ goto out;
24148+ a->h_dst = dget(h_d);
24149+ }
24150+
24151+ /* create whiteout for src_dentry */
24152+ if (au_ftest_ren(a->auren_flags, WHSRC)) {
24153+ a->src_bwh = au_dbwh(a->src_dentry);
24154+ AuDebugOn(a->src_bwh >= 0);
24155+ a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
24156+ a->src_h_parent);
24157+ err = PTR_ERR(a->src_wh_dentry);
24158+ if (IS_ERR(a->src_wh_dentry))
24159+ goto out_thargs;
24160+ }
24161+
24162+ /* lookup whiteout for dentry */
24163+ if (au_ftest_ren(a->auren_flags, WHDST)) {
24164+ h_d = au_wh_lkup(a->dst_h_parent,
24165+ &a->dst_dentry->d_name, a->br);
24166+ err = PTR_ERR(h_d);
24167+ if (IS_ERR(h_d))
24168+ goto out_whsrc;
24169+ if (d_is_negative(h_d))
24170+ dput(h_d);
24171+ else
24172+ a->dst_wh_dentry = h_d;
24173+ }
24174+
24175+ /* rename dentry to tmpwh */
24176+ if (a->thargs) {
24177+ err = au_whtmp_ren(a->dst_h_dentry, a->br);
24178+ if (unlikely(err))
24179+ goto out_whdst;
24180+
24181+ d = a->dst_dentry;
24182+ au_set_h_dptr(d, a->btgt, NULL);
24183+ err = au_lkup_neg(d, a->btgt, /*wh*/0);
24184+ if (unlikely(err))
24185+ goto out_whtmp;
24186+ a->dst_h_dentry = au_h_dptr(d, a->btgt);
24187+ }
4a4d8108 24188+ }
1facf9fc 24189+
5afbbe0d 24190+ BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
43982f53 24191+#if 0 /* debugging */
8b6a4947
AM
24192+ BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN)
24193+ && d_is_positive(a->dst_h_dentry)
24194+ && a->src_btop != a->btgt);
24195+#endif
1facf9fc 24196+
4a4d8108 24197+ /* rename by vfs_rename or cpup */
4a4d8108
AM
24198+ err = au_ren_or_cpup(a);
24199+ if (unlikely(err))
24200+ /* leave the copied-up one */
24201+ goto out_whtmp;
1308ab2a 24202+
4a4d8108 24203+ /* make dir opaque */
f2c43d5f
AM
24204+ err = au_ren_diropq(a);
24205+ if (unlikely(err))
24206+ goto out_rename;
1308ab2a 24207+
4a4d8108 24208+ /* update target timestamps */
f2c43d5f
AM
24209+ if (a->exchange) {
24210+ AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
24211+ a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
24212+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
631230db
AM
24213+ h_inode = d_inode(a->h_path.dentry);
24214+ inode_set_ctime_to_ts(a->dst_inode, inode_get_ctime(h_inode));
f2c43d5f 24215+ }
5afbbe0d 24216+ AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
4a4d8108
AM
24217+ a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
24218+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
631230db
AM
24219+ h_inode = d_inode(a->h_path.dentry);
24220+ inode_set_ctime_to_ts(a->src_inode, inode_get_ctime(h_inode));
1facf9fc 24221+
f2c43d5f
AM
24222+ if (!a->exchange) {
24223+ /* remove whiteout for dentry */
24224+ if (a->dst_wh_dentry) {
24225+ a->h_path.dentry = a->dst_wh_dentry;
24226+ err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
24227+ a->dst_dentry);
24228+ if (unlikely(err))
24229+ goto out_diropq;
24230+ }
1facf9fc 24231+
f2c43d5f
AM
24232+ /* remove whtmp */
24233+ if (a->thargs)
24234+ au_ren_del_whtmp(a); /* ignore this error */
1308ab2a 24235+
f2c43d5f
AM
24236+ au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
24237+ }
4a4d8108
AM
24238+ err = 0;
24239+ goto out_success;
24240+
4f0767ce 24241+out_diropq:
f2c43d5f 24242+ au_ren_rev_diropq(err, a);
4f0767ce 24243+out_rename:
7e9cd9fe 24244+ au_ren_rev_rename(err, a);
027c5e7a 24245+ dput(a->h_dst);
4f0767ce 24246+out_whtmp:
4a4d8108
AM
24247+ if (a->thargs)
24248+ au_ren_rev_whtmp(err, a);
4f0767ce 24249+out_whdst:
4a4d8108
AM
24250+ dput(a->dst_wh_dentry);
24251+ a->dst_wh_dentry = NULL;
4f0767ce 24252+out_whsrc:
4a4d8108
AM
24253+ if (a->src_wh_dentry)
24254+ au_ren_rev_whsrc(err, a);
4f0767ce 24255+out_success:
4a4d8108
AM
24256+ dput(a->src_wh_dentry);
24257+ dput(a->dst_wh_dentry);
4f0767ce 24258+out_thargs:
4a4d8108
AM
24259+ if (a->thargs) {
24260+ dput(a->h_dst);
24261+ au_whtmp_rmdir_free(a->thargs);
24262+ a->thargs = NULL;
24263+ }
4f0767ce 24264+out:
4a4d8108 24265+ return err;
dece6358 24266+}
1facf9fc 24267+
1308ab2a 24268+/* ---------------------------------------------------------------------- */
1facf9fc 24269+
4a4d8108
AM
24270+/*
24271+ * test if @dentry dir can be rename destination or not.
24272+ * success means, it is a logically empty dir.
24273+ */
24274+static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
1308ab2a 24275+{
4a4d8108 24276+ return au_test_empty(dentry, whlist);
1308ab2a 24277+}
1facf9fc 24278+
4a4d8108 24279+/*
8b6a4947
AM
24280+ * test if @a->src_dentry dir can be rename source or not.
24281+ * if it can, return 0.
4a4d8108
AM
24282+ * success means,
24283+ * - it is a logically empty dir.
24284+ * - or, it exists on writable branch and has no children including whiteouts
8b6a4947 24285+ * on the lower branch unless DIRREN is on.
4a4d8108 24286+ */
8b6a4947 24287+static int may_rename_srcdir(struct au_ren_args *a)
4a4d8108
AM
24288+{
24289+ int err;
24290+ unsigned int rdhash;
8b6a4947
AM
24291+ aufs_bindex_t btop, btgt;
24292+ struct dentry *dentry;
24293+ struct super_block *sb;
24294+ struct au_sbinfo *sbinfo;
1facf9fc 24295+
8b6a4947
AM
24296+ dentry = a->src_dentry;
24297+ sb = dentry->d_sb;
24298+ sbinfo = au_sbi(sb);
24299+ if (au_opt_test(sbinfo->si_mntflags, DIRREN))
24300+ au_fset_ren(a->auren_flags, DIRREN);
24301+
24302+ btgt = a->btgt;
5afbbe0d
AM
24303+ btop = au_dbtop(dentry);
24304+ if (btop != btgt) {
4a4d8108 24305+ struct au_nhash whlist;
dece6358 24306+
8b6a4947
AM
24307+ SiMustAnyLock(sb);
24308+ rdhash = sbinfo->si_rdhash;
4a4d8108
AM
24309+ if (!rdhash)
24310+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
24311+ dentry));
24312+ err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
24313+ if (unlikely(err))
24314+ goto out;
24315+ err = au_test_empty(dentry, &whlist);
24316+ au_nhash_wh_free(&whlist);
24317+ goto out;
24318+ }
dece6358 24319+
5afbbe0d 24320+ if (btop == au_dbtaildir(dentry))
4a4d8108 24321+ return 0; /* success */
dece6358 24322+
4a4d8108 24323+ err = au_test_empty_lower(dentry);
1facf9fc 24324+
4f0767ce 24325+out:
4a4d8108 24326+ if (err == -ENOTEMPTY) {
8b6a4947
AM
24327+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
24328+ err = 0;
24329+ } else {
24330+ AuWarn1("renaming dir who has child(ren) on multiple "
24331+ "branches, is not supported\n");
24332+ err = -EXDEV;
24333+ }
4a4d8108
AM
24334+ }
24335+ return err;
24336+}
1308ab2a 24337+
4a4d8108
AM
24338+/* side effect: sets whlist and h_dentry */
24339+static int au_ren_may_dir(struct au_ren_args *a)
1308ab2a 24340+{
4a4d8108
AM
24341+ int err;
24342+ unsigned int rdhash;
24343+ struct dentry *d;
1facf9fc 24344+
4a4d8108
AM
24345+ d = a->dst_dentry;
24346+ SiMustAnyLock(d->d_sb);
1facf9fc 24347+
4a4d8108 24348+ err = 0;
f2c43d5f 24349+ if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
4a4d8108
AM
24350+ rdhash = au_sbi(d->d_sb)->si_rdhash;
24351+ if (!rdhash)
24352+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
24353+ err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
24354+ if (unlikely(err))
24355+ goto out;
1308ab2a 24356+
f2c43d5f
AM
24357+ if (!a->exchange) {
24358+ au_set_dbtop(d, a->dst_btop);
24359+ err = may_rename_dstdir(d, &a->whlist);
24360+ au_set_dbtop(d, a->btgt);
24361+ } else
8b6a4947 24362+ err = may_rename_srcdir(a);
4a4d8108 24363+ }
5afbbe0d 24364+ a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
4a4d8108
AM
24365+ if (unlikely(err))
24366+ goto out;
24367+
24368+ d = a->src_dentry;
5afbbe0d 24369+ a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
f2c43d5f 24370+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
8b6a4947 24371+ err = may_rename_srcdir(a);
4a4d8108
AM
24372+ if (unlikely(err)) {
24373+ au_nhash_wh_free(&a->whlist);
24374+ a->whlist.nh_num = 0;
24375+ }
24376+ }
4f0767ce 24377+out:
4a4d8108 24378+ return err;
1facf9fc 24379+}
24380+
4a4d8108 24381+/* ---------------------------------------------------------------------- */
1facf9fc 24382+
4a4d8108
AM
24383+/*
24384+ * simple tests for rename.
24385+ * following the checks in vfs, plus the parent-child relationship.
24386+ */
24387+static int au_may_ren(struct au_ren_args *a)
24388+{
24389+ int err, isdir;
24390+ struct inode *h_inode;
1facf9fc 24391+
5afbbe0d 24392+ if (a->src_btop == a->btgt) {
4a4d8108 24393+ err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
f2c43d5f 24394+ au_ftest_ren(a->auren_flags, ISDIR_SRC));
4a4d8108
AM
24395+ if (unlikely(err))
24396+ goto out;
24397+ err = -EINVAL;
24398+ if (unlikely(a->src_h_dentry == a->h_trap))
24399+ goto out;
24400+ }
1facf9fc 24401+
4a4d8108 24402+ err = 0;
5afbbe0d 24403+ if (a->dst_btop != a->btgt)
4a4d8108 24404+ goto out;
1facf9fc 24405+
027c5e7a
AM
24406+ err = -ENOTEMPTY;
24407+ if (unlikely(a->dst_h_dentry == a->h_trap))
24408+ goto out;
24409+
4a4d8108 24410+ err = -EIO;
f2c43d5f 24411+ isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
5527c038
JR
24412+ if (d_really_is_negative(a->dst_dentry)) {
24413+ if (d_is_negative(a->dst_h_dentry))
24414+ err = au_may_add(a->dst_dentry, a->btgt,
24415+ a->dst_h_parent, isdir);
4a4d8108 24416+ } else {
5527c038 24417+ if (unlikely(d_is_negative(a->dst_h_dentry)))
4a4d8108 24418+ goto out;
5527c038
JR
24419+ h_inode = d_inode(a->dst_h_dentry);
24420+ if (h_inode->i_nlink)
24421+ err = au_may_del(a->dst_dentry, a->btgt,
24422+ a->dst_h_parent, isdir);
4a4d8108 24423+ }
1facf9fc 24424+
4f0767ce 24425+out:
4a4d8108
AM
24426+ if (unlikely(err == -ENOENT || err == -EEXIST))
24427+ err = -EIO;
24428+ AuTraceErr(err);
24429+ return err;
24430+}
1facf9fc 24431+
1308ab2a 24432+/* ---------------------------------------------------------------------- */
1facf9fc 24433+
4a4d8108
AM
24434+/*
24435+ * locking order
24436+ * (VFS)
24437+ * - src_dir and dir by lock_rename()
acd2b654 24438+ * - inode if exists
4a4d8108
AM
24439+ * (aufs)
24440+ * - lock all
24441+ * + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
24442+ * + si_read_lock
24443+ * + di_write_lock2_child()
24444+ * + di_write_lock_child()
24445+ * + ii_write_lock_child()
24446+ * + di_write_lock_child2()
24447+ * + ii_write_lock_child2()
24448+ * + src_parent and parent
24449+ * + di_write_lock_parent()
24450+ * + ii_write_lock_parent()
24451+ * + di_write_lock_parent2()
24452+ * + ii_write_lock_parent2()
24453+ * + lower src_dir and dir by vfsub_lock_rename()
24454+ * + verify the every relationships between child and parent. if any
24455+ * of them failed, unlock all and return -EBUSY.
24456+ */
24457+static void au_ren_unlock(struct au_ren_args *a)
1308ab2a 24458+{
4a4d8108
AM
24459+ vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
24460+ a->dst_h_parent, a->dst_hdir);
8b6a4947
AM
24461+ if (au_ftest_ren(a->auren_flags, DIRREN)
24462+ && a->h_root)
24463+ au_hn_inode_unlock(a->h_root);
f2c43d5f 24464+ if (au_ftest_ren(a->auren_flags, MNT_WRITE))
86dc4139 24465+ vfsub_mnt_drop_write(au_br_mnt(a->br));
1308ab2a 24466+}
24467+
4a4d8108 24468+static int au_ren_lock(struct au_ren_args *a)
1308ab2a 24469+{
4a4d8108
AM
24470+ int err;
24471+ unsigned int udba;
1308ab2a 24472+
4a4d8108
AM
24473+ err = 0;
24474+ a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
24475+ a->src_hdir = au_hi(a->src_dir, a->btgt);
24476+ a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
24477+ a->dst_hdir = au_hi(a->dst_dir, a->btgt);
86dc4139
AM
24478+
24479+ err = vfsub_mnt_want_write(au_br_mnt(a->br));
24480+ if (unlikely(err))
24481+ goto out;
f2c43d5f 24482+ au_fset_ren(a->auren_flags, MNT_WRITE);
8b6a4947
AM
24483+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
24484+ struct dentry *root;
24485+ struct inode *dir;
24486+
24487+ /*
24488+ * sbinfo is already locked, so this ii_read_lock is
24489+ * unnecessary. but our debugging feature checks it.
24490+ */
24491+ root = a->src_inode->i_sb->s_root;
24492+ if (root != a->src_parent && root != a->dst_parent) {
24493+ dir = d_inode(root);
24494+ ii_read_lock_parent3(dir);
24495+ a->h_root = au_hi(dir, a->btgt);
24496+ ii_read_unlock(dir);
24497+ au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3);
24498+ }
24499+ }
4a4d8108
AM
24500+ a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
24501+ a->dst_h_parent, a->dst_hdir);
24502+ udba = au_opt_udba(a->src_dentry->d_sb);
5527c038
JR
24503+ if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
24504+ || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
4a4d8108 24505+ err = au_busy_or_stale();
5afbbe0d 24506+ if (!err && au_dbtop(a->src_dentry) == a->btgt)
4a4d8108 24507+ err = au_h_verify(a->src_h_dentry, udba,
5527c038 24508+ d_inode(a->src_h_parent), a->src_h_parent,
4a4d8108 24509+ a->br);
5afbbe0d 24510+ if (!err && au_dbtop(a->dst_dentry) == a->btgt)
4a4d8108 24511+ err = au_h_verify(a->dst_h_dentry, udba,
5527c038 24512+ d_inode(a->dst_h_parent), a->dst_h_parent,
4a4d8108 24513+ a->br);
86dc4139 24514+ if (!err)
4a4d8108 24515+ goto out; /* success */
4a4d8108
AM
24516+
24517+ err = au_busy_or_stale();
4a4d8108 24518+ au_ren_unlock(a);
86dc4139 24519+
4f0767ce 24520+out:
4a4d8108 24521+ return err;
1facf9fc 24522+}
24523+
24524+/* ---------------------------------------------------------------------- */
24525+
4a4d8108 24526+static void au_ren_refresh_dir(struct au_ren_args *a)
1facf9fc 24527+{
4a4d8108 24528+ struct inode *dir;
dece6358 24529+
4a4d8108 24530+ dir = a->dst_dir;
be118d29 24531+ inode_inc_iversion(dir);
f2c43d5f 24532+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
4a4d8108
AM
24533+ /* is this updating defined in POSIX? */
24534+ au_cpup_attr_timesizes(a->src_inode);
24535+ au_cpup_attr_nlink(dir, /*force*/1);
4a4d8108 24536+ }
b912730e 24537+ au_dir_ts(dir, a->btgt);
dece6358 24538+
f2c43d5f
AM
24539+ if (a->exchange) {
24540+ dir = a->src_dir;
be118d29 24541+ inode_inc_iversion(dir);
f2c43d5f
AM
24542+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
24543+ /* is this updating defined in POSIX? */
24544+ au_cpup_attr_timesizes(a->dst_inode);
24545+ au_cpup_attr_nlink(dir, /*force*/1);
24546+ }
24547+ au_dir_ts(dir, a->btgt);
24548+ }
24549+
24550+ if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
4a4d8108 24551+ return;
dece6358 24552+
4a4d8108 24553+ dir = a->src_dir;
be118d29 24554+ inode_inc_iversion(dir);
f2c43d5f 24555+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
4a4d8108 24556+ au_cpup_attr_nlink(dir, /*force*/1);
b912730e 24557+ au_dir_ts(dir, a->btgt);
1facf9fc 24558+}
24559+
4a4d8108 24560+static void au_ren_refresh(struct au_ren_args *a)
1facf9fc 24561+{
5afbbe0d 24562+ aufs_bindex_t bbot, bindex;
4a4d8108
AM
24563+ struct dentry *d, *h_d;
24564+ struct inode *i, *h_i;
24565+ struct super_block *sb;
dece6358 24566+
027c5e7a
AM
24567+ d = a->dst_dentry;
24568+ d_drop(d);
24569+ if (a->h_dst)
24570+ /* already dget-ed by au_ren_or_cpup() */
24571+ au_set_h_dptr(d, a->btgt, a->h_dst);
24572+
24573+ i = a->dst_inode;
24574+ if (i) {
f2c43d5f
AM
24575+ if (!a->exchange) {
24576+ if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
24577+ vfsub_drop_nlink(i);
24578+ else {
24579+ vfsub_dead_dir(i);
24580+ au_cpup_attr_timesizes(i);
24581+ }
24582+ au_update_dbrange(d, /*do_put_zero*/1);
24583+ } else
24584+ au_cpup_attr_nlink(i, /*force*/1);
027c5e7a 24585+ } else {
5afbbe0d
AM
24586+ bbot = a->btgt;
24587+ for (bindex = au_dbtop(d); bindex < bbot; bindex++)
027c5e7a 24588+ au_set_h_dptr(d, bindex, NULL);
5afbbe0d
AM
24589+ bbot = au_dbbot(d);
24590+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
027c5e7a
AM
24591+ au_set_h_dptr(d, bindex, NULL);
24592+ au_update_dbrange(d, /*do_put_zero*/0);
24593+ }
24594+
8b6a4947
AM
24595+ if (a->exchange
24596+ || au_ftest_ren(a->auren_flags, DIRREN)) {
24597+ d_drop(a->src_dentry);
24598+ if (au_ftest_ren(a->auren_flags, DIRREN))
24599+ au_set_dbwh(a->src_dentry, -1);
24600+ return;
24601+ }
24602+
4a4d8108 24603+ d = a->src_dentry;
8b6a4947
AM
24604+ au_set_dbwh(d, -1);
24605+ bbot = au_dbbot(d);
24606+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
24607+ h_d = au_h_dptr(d, bindex);
24608+ if (h_d)
24609+ au_set_h_dptr(d, bindex, NULL);
24610+ }
24611+ au_set_dbbot(d, a->btgt);
4a4d8108 24612+
8b6a4947
AM
24613+ sb = d->d_sb;
24614+ i = a->src_inode;
24615+ if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
24616+ return; /* success */
4a4d8108 24617+
8b6a4947
AM
24618+ bbot = au_ibbot(i);
24619+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
24620+ h_i = au_h_iptr(i, bindex);
24621+ if (h_i) {
24622+ au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
24623+ /* ignore this error */
24624+ au_set_h_iptr(i, bindex, NULL, 0);
4a4d8108
AM
24625+ }
24626+ }
8b6a4947 24627+ au_set_ibbot(i, a->btgt);
1308ab2a 24628+}
dece6358 24629+
4a4d8108
AM
24630+/* ---------------------------------------------------------------------- */
24631+
24632+/* mainly for link(2) and rename(2) */
24633+int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
1308ab2a 24634+{
4a4d8108
AM
24635+ aufs_bindex_t bdiropq, bwh;
24636+ struct dentry *parent;
24637+ struct au_branch *br;
24638+
24639+ parent = dentry->d_parent;
5527c038 24640+ IMustLock(d_inode(parent)); /* dir is locked */
4a4d8108
AM
24641+
24642+ bdiropq = au_dbdiropq(parent);
24643+ bwh = au_dbwh(dentry);
24644+ br = au_sbr(dentry->d_sb, btgt);
24645+ if (au_br_rdonly(br)
24646+ || (0 <= bdiropq && bdiropq < btgt)
24647+ || (0 <= bwh && bwh < btgt))
24648+ btgt = -1;
24649+
24650+ AuDbg("btgt %d\n", btgt);
24651+ return btgt;
1facf9fc 24652+}
24653+
5afbbe0d 24654+/* sets src_btop, dst_btop and btgt */
4a4d8108 24655+static int au_ren_wbr(struct au_ren_args *a)
1facf9fc 24656+{
4a4d8108
AM
24657+ int err;
24658+ struct au_wr_dir_args wr_dir_args = {
24659+ /* .force_btgt = -1, */
24660+ .flags = AuWrDir_ADD_ENTRY
24661+ };
dece6358 24662+
5afbbe0d
AM
24663+ a->src_btop = au_dbtop(a->src_dentry);
24664+ a->dst_btop = au_dbtop(a->dst_dentry);
f2c43d5f
AM
24665+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
24666+ || au_ftest_ren(a->auren_flags, ISDIR_DST))
4a4d8108 24667+ au_fset_wrdir(wr_dir_args.flags, ISDIR);
5afbbe0d
AM
24668+ wr_dir_args.force_btgt = a->src_btop;
24669+ if (a->dst_inode && a->dst_btop < a->src_btop)
24670+ wr_dir_args.force_btgt = a->dst_btop;
4a4d8108
AM
24671+ wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
24672+ err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
24673+ a->btgt = err;
f2c43d5f
AM
24674+ if (a->exchange)
24675+ au_update_dbtop(a->dst_dentry);
dece6358 24676+
4a4d8108 24677+ return err;
1facf9fc 24678+}
24679+
4a4d8108 24680+static void au_ren_dt(struct au_ren_args *a)
1facf9fc 24681+{
4a4d8108
AM
24682+ a->h_path.dentry = a->src_h_parent;
24683+ au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
f2c43d5f 24684+ if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
4a4d8108
AM
24685+ a->h_path.dentry = a->dst_h_parent;
24686+ au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
24687+ }
1facf9fc 24688+
f2c43d5f
AM
24689+ au_fclr_ren(a->auren_flags, DT_DSTDIR);
24690+ if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
24691+ && !a->exchange)
4a4d8108 24692+ return;
dece6358 24693+
4a4d8108
AM
24694+ a->h_path.dentry = a->src_h_dentry;
24695+ au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
5527c038 24696+ if (d_is_positive(a->dst_h_dentry)) {
f2c43d5f 24697+ au_fset_ren(a->auren_flags, DT_DSTDIR);
4a4d8108
AM
24698+ a->h_path.dentry = a->dst_h_dentry;
24699+ au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
24700+ }
1308ab2a 24701+}
dece6358 24702+
4a4d8108 24703+static void au_ren_rev_dt(int err, struct au_ren_args *a)
1308ab2a 24704+{
4a4d8108 24705+ struct dentry *h_d;
febd17d6 24706+ struct inode *h_inode;
4a4d8108
AM
24707+
24708+ au_dtime_revert(a->src_dt + AuPARENT);
f2c43d5f 24709+ if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
4a4d8108
AM
24710+ au_dtime_revert(a->dst_dt + AuPARENT);
24711+
f2c43d5f 24712+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
4a4d8108 24713+ h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
febd17d6
JR
24714+ h_inode = d_inode(h_d);
24715+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 24716+ au_dtime_revert(a->src_dt + AuCHILD);
febd17d6 24717+ inode_unlock(h_inode);
4a4d8108 24718+
f2c43d5f 24719+ if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
4a4d8108 24720+ h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
febd17d6
JR
24721+ h_inode = d_inode(h_d);
24722+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 24723+ au_dtime_revert(a->dst_dt + AuCHILD);
febd17d6 24724+ inode_unlock(h_inode);
1facf9fc 24725+ }
24726+ }
24727+}
24728+
4a4d8108
AM
24729+/* ---------------------------------------------------------------------- */
24730+
5db017eb 24731+int aufs_rename(struct mnt_idmap *idmap,
0b2a12c6 24732+ struct inode *_src_dir, struct dentry *_src_dentry,
f2c43d5f
AM
24733+ struct inode *_dst_dir, struct dentry *_dst_dentry,
24734+ unsigned int _flags)
1facf9fc 24735+{
f2c43d5f 24736+ int err, lock_flags;
8b6a4947 24737+ void *rev;
4a4d8108
AM
24738+ /* reduce stack space */
24739+ struct au_ren_args *a;
f2c43d5f 24740+ struct au_pin pin;
4a4d8108 24741+
f2c43d5f 24742+ AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
4a4d8108
AM
24743+ IMustLock(_src_dir);
24744+ IMustLock(_dst_dir);
24745+
f2c43d5f
AM
24746+ err = -EINVAL;
24747+ if (unlikely(_flags & RENAME_WHITEOUT))
24748+ goto out;
24749+
4a4d8108
AM
24750+ err = -ENOMEM;
24751+ BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
24752+ a = kzalloc(sizeof(*a), GFP_NOFS);
24753+ if (unlikely(!a))
24754+ goto out;
24755+
f2c43d5f 24756+ a->flags = _flags;
acd2b654
AM
24757+ BUILD_BUG_ON(sizeof(a->exchange) == sizeof(u8)
24758+ && RENAME_EXCHANGE > U8_MAX);
f2c43d5f 24759+ a->exchange = _flags & RENAME_EXCHANGE;
4a4d8108
AM
24760+ a->src_dir = _src_dir;
24761+ a->src_dentry = _src_dentry;
5527c038
JR
24762+ a->src_inode = NULL;
24763+ if (d_really_is_positive(a->src_dentry))
24764+ a->src_inode = d_inode(a->src_dentry);
4a4d8108
AM
24765+ a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
24766+ a->dst_dir = _dst_dir;
24767+ a->dst_dentry = _dst_dentry;
5527c038
JR
24768+ a->dst_inode = NULL;
24769+ if (d_really_is_positive(a->dst_dentry))
24770+ a->dst_inode = d_inode(a->dst_dentry);
4a4d8108
AM
24771+ a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
24772+ if (a->dst_inode) {
f2c43d5f
AM
24773+ /*
24774+ * if EXCHANGE && src is non-dir && dst is dir,
24775+ * dst is not locked.
24776+ */
24777+ /* IMustLock(a->dst_inode); */
4a4d8108 24778+ au_igrab(a->dst_inode);
1facf9fc 24779+ }
1facf9fc 24780+
4a4d8108 24781+ err = -ENOTDIR;
f2c43d5f 24782+ lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
2000de60 24783+ if (d_is_dir(a->src_dentry)) {
f2c43d5f
AM
24784+ au_fset_ren(a->auren_flags, ISDIR_SRC);
24785+ if (unlikely(!a->exchange
24786+ && d_really_is_positive(a->dst_dentry)
2000de60 24787+ && !d_is_dir(a->dst_dentry)))
4a4d8108 24788+ goto out_free;
f2c43d5f
AM
24789+ lock_flags |= AuLock_DIRS;
24790+ }
24791+ if (a->dst_inode && d_is_dir(a->dst_dentry)) {
24792+ au_fset_ren(a->auren_flags, ISDIR_DST);
24793+ if (unlikely(!a->exchange
24794+ && d_really_is_positive(a->src_dentry)
24795+ && !d_is_dir(a->src_dentry)))
24796+ goto out_free;
24797+ lock_flags |= AuLock_DIRS;
b95c5147 24798+ }
8b6a4947
AM
24799+ err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
24800+ lock_flags);
e49829fe
JR
24801+ if (unlikely(err))
24802+ goto out_free;
1facf9fc 24803+
027c5e7a
AM
24804+ err = au_d_hashed_positive(a->src_dentry);
24805+ if (unlikely(err))
24806+ goto out_unlock;
24807+ err = -ENOENT;
24808+ if (a->dst_inode) {
24809+ /*
f2c43d5f 24810+ * If it is a dir, VFS unhash it before this
027c5e7a
AM
24811+ * function. It means we cannot rely upon d_unhashed().
24812+ */
24813+ if (unlikely(!a->dst_inode->i_nlink))
24814+ goto out_unlock;
f2c43d5f 24815+ if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
027c5e7a 24816+ err = au_d_hashed_positive(a->dst_dentry);
f2c43d5f 24817+ if (unlikely(err && !a->exchange))
027c5e7a
AM
24818+ goto out_unlock;
24819+ } else if (unlikely(IS_DEADDIR(a->dst_inode)))
24820+ goto out_unlock;
24821+ } else if (unlikely(d_unhashed(a->dst_dentry)))
24822+ goto out_unlock;
24823+
7eafdf33
AM
24824+ /*
24825+ * is it possible?
79b8bda9 24826+ * yes, it happened (in linux-3.3-rcN) but I don't know why.
7eafdf33
AM
24827+ * there may exist a problem somewhere else.
24828+ */
24829+ err = -EINVAL;
5527c038 24830+ if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
7eafdf33
AM
24831+ goto out_unlock;
24832+
f2c43d5f 24833+ au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
4a4d8108 24834+ di_write_lock_parent(a->dst_parent);
1facf9fc 24835+
4a4d8108
AM
24836+ /* which branch we process */
24837+ err = au_ren_wbr(a);
24838+ if (unlikely(err < 0))
027c5e7a 24839+ goto out_parent;
4a4d8108 24840+ a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
86dc4139 24841+ a->h_path.mnt = au_br_mnt(a->br);
1facf9fc 24842+
4a4d8108
AM
24843+ /* are they available to be renamed */
24844+ err = au_ren_may_dir(a);
24845+ if (unlikely(err))
24846+ goto out_children;
1facf9fc 24847+
4a4d8108 24848+ /* prepare the writable parent dir on the same branch */
5afbbe0d 24849+ if (a->dst_btop == a->btgt) {
f2c43d5f 24850+ au_fset_ren(a->auren_flags, WHDST);
4a4d8108
AM
24851+ } else {
24852+ err = au_cpup_dirs(a->dst_dentry, a->btgt);
24853+ if (unlikely(err))
24854+ goto out_children;
24855+ }
1facf9fc 24856+
f2c43d5f
AM
24857+ err = 0;
24858+ if (!a->exchange) {
24859+ if (a->src_dir != a->dst_dir) {
24860+ /*
24861+ * this temporary unlock is safe,
24862+ * because both dir->i_mutex are locked.
24863+ */
24864+ di_write_unlock(a->dst_parent);
24865+ di_write_lock_parent(a->src_parent);
24866+ err = au_wr_dir_need_wh(a->src_dentry,
24867+ au_ftest_ren(a->auren_flags,
24868+ ISDIR_SRC),
24869+ &a->btgt);
24870+ di_write_unlock(a->src_parent);
24871+ di_write_lock2_parent(a->src_parent, a->dst_parent,
24872+ /*isdir*/1);
24873+ au_fclr_ren(a->auren_flags, ISSAMEDIR);
24874+ } else
24875+ err = au_wr_dir_need_wh(a->src_dentry,
24876+ au_ftest_ren(a->auren_flags,
24877+ ISDIR_SRC),
24878+ &a->btgt);
24879+ }
4a4d8108
AM
24880+ if (unlikely(err < 0))
24881+ goto out_children;
24882+ if (err)
f2c43d5f 24883+ au_fset_ren(a->auren_flags, WHSRC);
1facf9fc 24884+
86dc4139 24885+ /* cpup src */
5afbbe0d 24886+ if (a->src_btop != a->btgt) {
86dc4139
AM
24887+ err = au_pin(&pin, a->src_dentry, a->btgt,
24888+ au_opt_udba(a->src_dentry->d_sb),
24889+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
367653fa 24890+ if (!err) {
c2b27bf2
AM
24891+ struct au_cp_generic cpg = {
24892+ .dentry = a->src_dentry,
24893+ .bdst = a->btgt,
5afbbe0d 24894+ .bsrc = a->src_btop,
c2b27bf2
AM
24895+ .len = -1,
24896+ .pin = &pin,
24897+ .flags = AuCpup_DTIME | AuCpup_HOPEN
24898+ };
5afbbe0d 24899+ AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
c2b27bf2 24900+ err = au_sio_cpup_simple(&cpg);
367653fa 24901+ au_unpin(&pin);
86dc4139 24902+ }
86dc4139
AM
24903+ if (unlikely(err))
24904+ goto out_children;
5afbbe0d 24905+ a->src_btop = a->btgt;
86dc4139 24906+ a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
f2c43d5f
AM
24907+ if (!a->exchange)
24908+ au_fset_ren(a->auren_flags, WHSRC);
24909+ }
24910+
24911+ /* cpup dst */
24912+ if (a->exchange && a->dst_inode
24913+ && a->dst_btop != a->btgt) {
24914+ err = au_pin(&pin, a->dst_dentry, a->btgt,
24915+ au_opt_udba(a->dst_dentry->d_sb),
24916+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
24917+ if (!err) {
24918+ struct au_cp_generic cpg = {
24919+ .dentry = a->dst_dentry,
24920+ .bdst = a->btgt,
24921+ .bsrc = a->dst_btop,
24922+ .len = -1,
24923+ .pin = &pin,
24924+ .flags = AuCpup_DTIME | AuCpup_HOPEN
24925+ };
24926+ err = au_sio_cpup_simple(&cpg);
24927+ au_unpin(&pin);
24928+ }
24929+ if (unlikely(err))
24930+ goto out_children;
24931+ a->dst_btop = a->btgt;
24932+ a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
86dc4139
AM
24933+ }
24934+
4a4d8108
AM
24935+ /* lock them all */
24936+ err = au_ren_lock(a);
24937+ if (unlikely(err))
86dc4139 24938+ /* leave the copied-up one */
4a4d8108 24939+ goto out_children;
1facf9fc 24940+
f2c43d5f
AM
24941+ if (!a->exchange) {
24942+ if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
24943+ err = au_may_ren(a);
24944+ else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
24945+ err = -ENAMETOOLONG;
24946+ if (unlikely(err))
24947+ goto out_hdir;
24948+ }
1facf9fc 24949+
4a4d8108
AM
24950+ /* store timestamps to be revertible */
24951+ au_ren_dt(a);
1facf9fc 24952+
8b6a4947
AM
24953+ /* store dirren info */
24954+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
24955+ err = au_dr_rename(a->src_dentry, a->btgt,
24956+ &a->dst_dentry->d_name, &rev);
24957+ AuTraceErr(err);
24958+ if (unlikely(err))
24959+ goto out_dt;
24960+ }
24961+
4a4d8108
AM
24962+ /* here we go */
24963+ err = do_rename(a);
24964+ if (unlikely(err))
8b6a4947
AM
24965+ goto out_dirren;
24966+
24967+ if (au_ftest_ren(a->auren_flags, DIRREN))
24968+ au_dr_rename_fin(a->src_dentry, a->btgt, rev);
4a4d8108
AM
24969+
24970+ /* update dir attributes */
24971+ au_ren_refresh_dir(a);
24972+
24973+ /* dput/iput all lower dentries */
24974+ au_ren_refresh(a);
24975+
24976+ goto out_hdir; /* success */
24977+
8b6a4947
AM
24978+out_dirren:
24979+ if (au_ftest_ren(a->auren_flags, DIRREN))
24980+ au_dr_rename_rev(a->src_dentry, a->btgt, rev);
4f0767ce 24981+out_dt:
4a4d8108 24982+ au_ren_rev_dt(err, a);
4f0767ce 24983+out_hdir:
4a4d8108 24984+ au_ren_unlock(a);
4f0767ce 24985+out_children:
4a4d8108 24986+ au_nhash_wh_free(&a->whlist);
5afbbe0d
AM
24987+ if (err && a->dst_inode && a->dst_btop != a->btgt) {
24988+ AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
027c5e7a 24989+ au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
5afbbe0d 24990+ au_set_dbtop(a->dst_dentry, a->dst_btop);
4a4d8108 24991+ }
027c5e7a 24992+out_parent:
f2c43d5f 24993+ if (!err) {
8b6a4947
AM
24994+ if (d_unhashed(a->src_dentry))
24995+ au_fset_ren(a->auren_flags, DROPPED_SRC);
24996+ if (d_unhashed(a->dst_dentry))
24997+ au_fset_ren(a->auren_flags, DROPPED_DST);
f2c43d5f
AM
24998+ if (!a->exchange)
24999+ d_move(a->src_dentry, a->dst_dentry);
8b6a4947 25000+ else {
f2c43d5f 25001+ d_exchange(a->src_dentry, a->dst_dentry);
8b6a4947
AM
25002+ if (au_ftest_ren(a->auren_flags, DROPPED_DST))
25003+ d_drop(a->dst_dentry);
25004+ }
25005+ if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
25006+ d_drop(a->src_dentry);
f2c43d5f 25007+ } else {
5afbbe0d 25008+ au_update_dbtop(a->dst_dentry);
027c5e7a
AM
25009+ if (!a->dst_inode)
25010+ d_drop(a->dst_dentry);
25011+ }
f2c43d5f 25012+ if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
4a4d8108
AM
25013+ di_write_unlock(a->dst_parent);
25014+ else
25015+ di_write_unlock2(a->src_parent, a->dst_parent);
027c5e7a 25016+out_unlock:
4a4d8108 25017+ aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
4f0767ce 25018+out_free:
4a4d8108
AM
25019+ iput(a->dst_inode);
25020+ if (a->thargs)
25021+ au_whtmp_rmdir_free(a->thargs);
9f237c51 25022+ au_kfree_rcu(a);
4f0767ce 25023+out:
4a4d8108
AM
25024+ AuTraceErr(err);
25025+ return err;
1308ab2a 25026+}
7f207e10 25027diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
eca34b5c 25028--- /usr/share/empty/fs/aufs/Kconfig 1970-01-01 01:00:00.000000000 +0100
594d0238 25029+++ linux/fs/aufs/Kconfig 2022-11-05 23:02:18.959222617 +0100
2121bcd9
AM
25030@@ -0,0 +1,199 @@
25031+# SPDX-License-Identifier: GPL-2.0
4a4d8108
AM
25032+config AUFS_FS
25033+ tristate "Aufs (Advanced multi layered unification filesystem) support"
4a4d8108
AM
25034+ help
25035+ Aufs is a stackable unification filesystem such as Unionfs,
25036+ which unifies several directories and provides a merged single
25037+ directory.
25038+ In the early days, aufs was entirely re-designed and
25039+ re-implemented Unionfs Version 1.x series. Introducing many
25040+ original ideas, approaches and improvements, it becomes totally
25041+ different from Unionfs while keeping the basic features.
1facf9fc 25042+
4a4d8108
AM
25043+if AUFS_FS
25044+choice
25045+ prompt "Maximum number of branches"
25046+ default AUFS_BRANCH_MAX_127
25047+ help
25048+ Specifies the maximum number of branches (or member directories)
25049+ in a single aufs. The larger value consumes more system
25050+ resources and has a minor impact to performance.
25051+config AUFS_BRANCH_MAX_127
25052+ bool "127"
25053+ help
25054+ Specifies the maximum number of branches (or member directories)
25055+ in a single aufs. The larger value consumes more system
25056+ resources and has a minor impact to performance.
25057+config AUFS_BRANCH_MAX_511
25058+ bool "511"
25059+ help
25060+ Specifies the maximum number of branches (or member directories)
25061+ in a single aufs. The larger value consumes more system
25062+ resources and has a minor impact to performance.
25063+config AUFS_BRANCH_MAX_1023
25064+ bool "1023"
25065+ help
25066+ Specifies the maximum number of branches (or member directories)
25067+ in a single aufs. The larger value consumes more system
25068+ resources and has a minor impact to performance.
25069+config AUFS_BRANCH_MAX_32767
25070+ bool "32767"
25071+ help
25072+ Specifies the maximum number of branches (or member directories)
25073+ in a single aufs. The larger value consumes more system
25074+ resources and has a minor impact to performance.
25075+endchoice
1facf9fc 25076+
e49829fe
JR
25077+config AUFS_SBILIST
25078+ bool
25079+ depends on AUFS_MAGIC_SYSRQ || PROC_FS
25080+ default y
25081+ help
25082+ Automatic configuration for internal use.
25083+ When aufs supports Magic SysRq or /proc, enabled automatically.
25084+
4a4d8108
AM
25085+config AUFS_HNOTIFY
25086+ bool "Detect direct branch access (bypassing aufs)"
25087+ help
25088+ If you want to modify files on branches directly, eg. bypassing aufs,
25089+ and want aufs to detect the changes of them fully, then enable this
25090+ option and use 'udba=notify' mount option.
7f207e10 25091+ Currently there is only one available configuration, "fsnotify".
4a4d8108
AM
25092+ It will have a negative impact to the performance.
25093+ See detail in aufs.5.
dece6358 25094+
4a4d8108
AM
25095+choice
25096+ prompt "method" if AUFS_HNOTIFY
25097+ default AUFS_HFSNOTIFY
25098+config AUFS_HFSNOTIFY
25099+ bool "fsnotify"
25100+ select FSNOTIFY
4a4d8108 25101+endchoice
1facf9fc 25102+
4a4d8108
AM
25103+config AUFS_EXPORT
25104+ bool "NFS-exportable aufs"
2cbb1c4b 25105+ depends on EXPORTFS
4a4d8108
AM
25106+ help
25107+ If you want to export your mounted aufs via NFS, then enable this
25108+ option. There are several requirements for this configuration.
25109+ See detail in aufs.5.
1facf9fc 25110+
4a4d8108
AM
25111+config AUFS_INO_T_64
25112+ bool
25113+ depends on AUFS_EXPORT
25114+ depends on 64BIT && !(ALPHA || S390)
25115+ default y
25116+ help
25117+ Automatic configuration for internal use.
25118+ /* typedef unsigned long/int __kernel_ino_t */
25119+ /* alpha and s390x are int */
1facf9fc 25120+
c1595e42
JR
25121+config AUFS_XATTR
25122+ bool "support for XATTR/EA (including Security Labels)"
25123+ help
25124+ If your branch fs supports XATTR/EA and you want to make them
25125+ available in aufs too, then enable this opsion and specify the
25126+ branch attributes for EA.
25127+ See detail in aufs.5.
25128+
076b876e
AM
25129+config AUFS_FHSM
25130+ bool "File-based Hierarchical Storage Management"
25131+ help
25132+ Hierarchical Storage Management (or HSM) is a well-known feature
25133+ in the storage world. Aufs provides this feature as file-based.
25134+ with multiple branches.
25135+ These multiple branches are prioritized, ie. the topmost one
25136+ should be the fastest drive and be used heavily.
25137+
4a4d8108
AM
25138+config AUFS_RDU
25139+ bool "Readdir in userspace"
25140+ help
25141+ Aufs has two methods to provide a merged view for a directory,
25142+ by a user-space library and by kernel-space natively. The latter
25143+ is always enabled but sometimes large and slow.
25144+ If you enable this option, install the library in aufs2-util
25145+ package, and set some environment variables for your readdir(3),
25146+ then the work will be handled in user-space which generally
25147+ shows better performance in most cases.
25148+ See detail in aufs.5.
1facf9fc 25149+
8b6a4947
AM
25150+config AUFS_DIRREN
25151+ bool "Workaround for rename(2)-ing a directory"
25152+ help
25153+ By default, aufs returns EXDEV error in renameing a dir who has
25154+ his child on the lower branch, since it is a bad idea to issue
25155+ rename(2) internally for every lower branch. But user may not
25156+ accept this behaviour. So here is a workaround to allow such
22319442 25157+ rename(2) and store some extra information on the writable
8b6a4947
AM
25158+ branch. Obviously this costs high (and I don't like it).
25159+ To use this feature, you need to enable this configuration AND
25160+ to specify the mount option `dirren.'
25161+ See details in aufs.5 and the design documents.
25162+
4a4d8108
AM
25163+config AUFS_SHWH
25164+ bool "Show whiteouts"
25165+ help
25166+ If you want to make the whiteouts in aufs visible, then enable
25167+ this option and specify 'shwh' mount option. Although it may
25168+ sounds like philosophy or something, but in technically it
25169+ simply shows the name of whiteout with keeping its behaviour.
1facf9fc 25170+
4a4d8108
AM
25171+config AUFS_BR_RAMFS
25172+ bool "Ramfs (initramfs/rootfs) as an aufs branch"
25173+ help
25174+ If you want to use ramfs as an aufs branch fs, then enable this
25175+ option. Generally tmpfs is recommended.
25176+ Aufs prohibited them to be a branch fs by default, because
25177+ initramfs becomes unusable after switch_root or something
25178+ generally. If you sets initramfs as an aufs branch and boot your
25179+ system by switch_root, you will meet a problem easily since the
25180+ files in initramfs may be inaccessible.
25181+ Unless you are going to use ramfs as an aufs branch fs without
25182+ switch_root or something, leave it N.
1facf9fc 25183+
4a4d8108
AM
25184+config AUFS_BR_FUSE
25185+ bool "Fuse fs as an aufs branch"
25186+ depends on FUSE_FS
25187+ select AUFS_POLL
25188+ help
25189+ If you want to use fuse-based userspace filesystem as an aufs
25190+ branch fs, then enable this option.
25191+ It implements the internal poll(2) operation which is
25192+ implemented by fuse only (curretnly).
1facf9fc 25193+
4a4d8108
AM
25194+config AUFS_POLL
25195+ bool
25196+ help
25197+ Automatic configuration for internal use.
1facf9fc 25198+
4a4d8108
AM
25199+config AUFS_BR_HFSPLUS
25200+ bool "Hfsplus as an aufs branch"
25201+ depends on HFSPLUS_FS
25202+ default y
25203+ help
25204+ If you want to use hfsplus fs as an aufs branch fs, then enable
25205+ this option. This option introduces a small overhead at
25206+ copying-up a file on hfsplus.
1facf9fc 25207+
4a4d8108
AM
25208+config AUFS_BDEV_LOOP
25209+ bool
25210+ depends on BLK_DEV_LOOP
25211+ default y
25212+ help
25213+ Automatic configuration for internal use.
25214+ Convert =[ym] into =y.
1308ab2a 25215+
4a4d8108
AM
25216+config AUFS_DEBUG
25217+ bool "Debug aufs"
25218+ help
25219+ Enable this to compile aufs internal debug code.
25220+ It will have a negative impact to the performance.
25221+
25222+config AUFS_MAGIC_SYSRQ
25223+ bool
25224+ depends on AUFS_DEBUG && MAGIC_SYSRQ
25225+ default y
25226+ help
25227+ Automatic configuration for internal use.
25228+ When aufs supports Magic SysRq, enabled automatically.
25229+endif
acd2b654 25230diff -urN /usr/share/empty/fs/aufs/lcnt.h linux/fs/aufs/lcnt.h
eca34b5c 25231--- /usr/share/empty/fs/aufs/lcnt.h 1970-01-01 01:00:00.000000000 +0100
594d0238 25232+++ linux/fs/aufs/lcnt.h 2022-11-05 23:02:18.965889284 +0100
acd2b654
AM
25233@@ -0,0 +1,186 @@
25234+/* SPDX-License-Identifier: GPL-2.0 */
25235+/*
0dcfbb52 25236+ * Copyright (C) 2018-2022 Junjiro R. Okajima
acd2b654 25237+ *
0dcfbb52 25238+ * This program is free software; you can redistribute it and/or modify
acd2b654
AM
25239+ * it under the terms of the GNU General Public License as published by
25240+ * the Free Software Foundation; either version 2 of the License, or
25241+ * (at your option) any later version.
25242+ *
25243+ * This program is distributed in the hope that it will be useful,
25244+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
25245+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25246+ * GNU General Public License for more details.
25247+ *
25248+ * You should have received a copy of the GNU General Public License
25249+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
25250+ */
25251+
25252+/*
25253+ * simple long counter wrapper
25254+ */
25255+
25256+#ifndef __AUFS_LCNT_H__
25257+#define __AUFS_LCNT_H__
25258+
25259+#ifdef __KERNEL__
25260+
25261+#include "debug.h"
25262+
25263+#define AuLCntATOMIC 1
25264+#define AuLCntPCPUCNT 2
25265+/*
25266+ * why does percpu_refcount require extra synchronize_rcu()s in
25267+ * au_br_do_free()
25268+ */
25269+#define AuLCntPCPUREF 3
25270+
25271+/* #define AuLCntChosen AuLCntATOMIC */
25272+#define AuLCntChosen AuLCntPCPUCNT
25273+/* #define AuLCntChosen AuLCntPCPUREF */
25274+
25275+#if AuLCntChosen == AuLCntATOMIC
25276+#include <linux/atomic.h>
25277+
25278+typedef atomic_long_t au_lcnt_t;
25279+
25280+static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
25281+{
25282+ atomic_long_set(cnt, 0);
25283+ return 0;
25284+}
25285+
25286+static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
25287+{
25288+ /* empty */
25289+}
25290+
25291+static inline void au_lcnt_fin(au_lcnt_t *cnt __maybe_unused,
25292+ int do_sync __maybe_unused)
25293+{
25294+ /* empty */
25295+}
25296+
25297+static inline void au_lcnt_inc(au_lcnt_t *cnt)
25298+{
25299+ atomic_long_inc(cnt);
25300+}
25301+
25302+static inline void au_lcnt_dec(au_lcnt_t *cnt)
25303+{
25304+ atomic_long_dec(cnt);
25305+}
25306+
25307+static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
25308+{
25309+ return atomic_long_read(cnt);
25310+}
25311+#endif
25312+
25313+#if AuLCntChosen == AuLCntPCPUCNT
25314+#include <linux/percpu_counter.h>
25315+
25316+typedef struct percpu_counter au_lcnt_t;
25317+
25318+static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
25319+{
25320+ return percpu_counter_init(cnt, 0, GFP_NOFS);
25321+}
25322+
25323+static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
25324+{
25325+ /* empty */
25326+}
25327+
25328+static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync __maybe_unused)
25329+{
25330+ percpu_counter_destroy(cnt);
25331+}
25332+
25333+static inline void au_lcnt_inc(au_lcnt_t *cnt)
25334+{
25335+ percpu_counter_inc(cnt);
25336+}
25337+
25338+static inline void au_lcnt_dec(au_lcnt_t *cnt)
25339+{
25340+ percpu_counter_dec(cnt);
25341+}
25342+
25343+static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
25344+{
25345+ s64 n;
25346+
25347+ n = percpu_counter_sum(cnt);
25348+ BUG_ON(n < 0);
25349+ if (LONG_MAX != LLONG_MAX
25350+ && n > LONG_MAX)
25351+ AuWarn1("%s\n", "wrap-around");
25352+
25353+ return n;
25354+}
25355+#endif
25356+
25357+#if AuLCntChosen == AuLCntPCPUREF
25358+#include <linux/percpu-refcount.h>
25359+
25360+typedef struct percpu_ref au_lcnt_t;
25361+
25362+static inline int au_lcnt_init(au_lcnt_t *cnt, percpu_ref_func_t *release)
25363+{
25364+ if (!release)
25365+ release = percpu_ref_exit;
25366+ return percpu_ref_init(cnt, release, /*percpu mode*/0, GFP_NOFS);
25367+}
25368+
25369+static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
25370+{
25371+ synchronize_rcu();
25372+}
25373+
25374+static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync)
25375+{
25376+ percpu_ref_kill(cnt);
25377+ if (do_sync)
25378+ au_lcnt_wait_for_fin(cnt);
25379+}
25380+
25381+static inline void au_lcnt_inc(au_lcnt_t *cnt)
25382+{
25383+ percpu_ref_get(cnt);
25384+}
25385+
25386+static inline void au_lcnt_dec(au_lcnt_t *cnt)
25387+{
25388+ percpu_ref_put(cnt);
25389+}
25390+
25391+/*
25392+ * avoid calling this func as possible.
25393+ */
25394+static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev)
25395+{
25396+ long l;
25397+
25398+ percpu_ref_switch_to_atomic_sync(cnt);
25399+ l = atomic_long_read(&cnt->count);
25400+ if (do_rev)
25401+ percpu_ref_switch_to_percpu(cnt);
25402+
25403+ /* percpu_ref is initialized by 1 instead of 0 */
25404+ return l - 1;
25405+}
25406+#endif
25407+
25408+#ifdef CONFIG_AUFS_DEBUG
25409+#define AuLCntZero(val) do { \
25410+ long l = val; \
25411+ if (l) \
25412+ AuDbg("%s = %ld\n", #val, l); \
25413+} while (0)
25414+#else
25415+#define AuLCntZero(val) do {} while (0)
25416+#endif
25417+
25418+#endif /* __KERNEL__ */
25419+#endif /* __AUFS_LCNT_H__ */
7f207e10 25420diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
eca34b5c 25421--- /usr/share/empty/fs/aufs/loop.c 1970-01-01 01:00:00.000000000 +0100
594d0238 25422+++ linux/fs/aufs/loop.c 2022-11-05 23:02:18.965889284 +0100
062440b3 25423@@ -0,0 +1,148 @@
cd7a4cd9 25424+// SPDX-License-Identifier: GPL-2.0
1facf9fc 25425+/*
0dcfbb52 25426+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 25427+ *
0dcfbb52 25428+ * This program is free software; you can redistribute it and/or modify
1facf9fc 25429+ * it under the terms of the GNU General Public License as published by
25430+ * the Free Software Foundation; either version 2 of the License, or
25431+ * (at your option) any later version.
dece6358
AM
25432+ *
25433+ * This program is distributed in the hope that it will be useful,
25434+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
25435+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25436+ * GNU General Public License for more details.
25437+ *
25438+ * You should have received a copy of the GNU General Public License
523b37e3 25439+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 25440+ */
25441+
25442+/*
25443+ * support for loopback block device as a branch
25444+ */
25445+
1facf9fc 25446+#include "aufs.h"
25447+
392086de
AM
25448+/* added into drivers/block/loop.c */
25449+static struct file *(*backing_file_func)(struct super_block *sb);
25450+
1facf9fc 25451+/*
25452+ * test if two lower dentries have overlapping branches.
25453+ */
b752ccd1 25454+int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
1facf9fc 25455+{
b752ccd1 25456+ struct super_block *h_sb;
392086de
AM
25457+ struct file *backing_file;
25458+
25459+ if (unlikely(!backing_file_func)) {
25460+ /* don't load "loop" module here */
25461+ backing_file_func = symbol_get(loop_backing_file);
25462+ if (unlikely(!backing_file_func))
25463+ /* "loop" module is not loaded */
25464+ return 0;
25465+ }
1facf9fc 25466+
b752ccd1 25467+ h_sb = h_adding->d_sb;
392086de
AM
25468+ backing_file = backing_file_func(h_sb);
25469+ if (!backing_file)
1facf9fc 25470+ return 0;
25471+
2000de60 25472+ h_adding = backing_file->f_path.dentry;
b752ccd1
AM
25473+ /*
25474+ * h_adding can be local NFS.
25475+ * in this case aufs cannot detect the loop.
25476+ */
25477+ if (unlikely(h_adding->d_sb == sb))
1facf9fc 25478+ return 1;
b752ccd1 25479+ return !!au_test_subdir(h_adding, sb->s_root);
1facf9fc 25480+}
25481+
25482+/* true if a kernel thread named 'loop[0-9].*' accesses a file */
25483+int au_test_loopback_kthread(void)
25484+{
b752ccd1
AM
25485+ int ret;
25486+ struct task_struct *tsk = current;
a2a7ad62 25487+ char c, comm[sizeof(tsk->comm)];
b752ccd1
AM
25488+
25489+ ret = 0;
25490+ if (tsk->flags & PF_KTHREAD) {
a2a7ad62
AM
25491+ get_task_comm(comm, tsk);
25492+ c = comm[4];
b752ccd1 25493+ ret = ('0' <= c && c <= '9'
a2a7ad62 25494+ && !strncmp(comm, "loop", 4));
b752ccd1 25495+ }
1facf9fc 25496+
b752ccd1 25497+ return ret;
1facf9fc 25498+}
87a755f4
AM
25499+
25500+/* ---------------------------------------------------------------------- */
25501+
25502+#define au_warn_loopback_step 16
25503+static int au_warn_loopback_nelem = au_warn_loopback_step;
25504+static unsigned long *au_warn_loopback_array;
25505+
25506+void au_warn_loopback(struct super_block *h_sb)
25507+{
25508+ int i, new_nelem;
25509+ unsigned long *a, magic;
25510+ static DEFINE_SPINLOCK(spin);
25511+
25512+ magic = h_sb->s_magic;
25513+ spin_lock(&spin);
25514+ a = au_warn_loopback_array;
25515+ for (i = 0; i < au_warn_loopback_nelem && *a; i++)
25516+ if (a[i] == magic) {
25517+ spin_unlock(&spin);
25518+ return;
25519+ }
25520+
25521+ /* h_sb is new to us, print it */
25522+ if (i < au_warn_loopback_nelem) {
25523+ a[i] = magic;
25524+ goto pr;
25525+ }
25526+
25527+ /* expand the array */
25528+ new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
25529+ a = au_kzrealloc(au_warn_loopback_array,
25530+ au_warn_loopback_nelem * sizeof(unsigned long),
e2f27e51
AM
25531+ new_nelem * sizeof(unsigned long), GFP_ATOMIC,
25532+ /*may_shrink*/0);
87a755f4
AM
25533+ if (a) {
25534+ au_warn_loopback_nelem = new_nelem;
25535+ au_warn_loopback_array = a;
25536+ a[i] = magic;
25537+ goto pr;
25538+ }
25539+
25540+ spin_unlock(&spin);
25541+ AuWarn1("realloc failed, ignored\n");
25542+ return;
25543+
25544+pr:
25545+ spin_unlock(&spin);
0c3ec466
AM
25546+ pr_warn("you may want to try another patch for loopback file "
25547+ "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
87a755f4
AM
25548+}
25549+
25550+int au_loopback_init(void)
25551+{
25552+ int err;
25553+ struct super_block *sb __maybe_unused;
25554+
fbc438ed 25555+ BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(*au_warn_loopback_array));
87a755f4
AM
25556+
25557+ err = 0;
25558+ au_warn_loopback_array = kcalloc(au_warn_loopback_step,
25559+ sizeof(unsigned long), GFP_NOFS);
25560+ if (unlikely(!au_warn_loopback_array))
25561+ err = -ENOMEM;
25562+
25563+ return err;
25564+}
25565+
25566+void au_loopback_fin(void)
25567+{
79b8bda9
AM
25568+ if (backing_file_func)
25569+ symbol_put(loop_backing_file);
9f237c51 25570+ au_kfree_try_rcu(au_warn_loopback_array);
87a755f4 25571+}
7f207e10 25572diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
eca34b5c 25573--- /usr/share/empty/fs/aufs/loop.h 1970-01-01 01:00:00.000000000 +0100
594d0238 25574+++ linux/fs/aufs/loop.h 2022-11-05 23:02:18.965889284 +0100
eca801bf 25575@@ -0,0 +1,55 @@
062440b3 25576+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 25577+/*
0dcfbb52 25578+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 25579+ *
0dcfbb52 25580+ * This program is free software; you can redistribute it and/or modify
1facf9fc 25581+ * it under the terms of the GNU General Public License as published by
25582+ * the Free Software Foundation; either version 2 of the License, or
25583+ * (at your option) any later version.
dece6358
AM
25584+ *
25585+ * This program is distributed in the hope that it will be useful,
25586+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
25587+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25588+ * GNU General Public License for more details.
25589+ *
25590+ * You should have received a copy of the GNU General Public License
523b37e3 25591+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 25592+ */
25593+
25594+/*
25595+ * support for loopback mount as a branch
25596+ */
25597+
25598+#ifndef __AUFS_LOOP_H__
25599+#define __AUFS_LOOP_H__
25600+
25601+#ifdef __KERNEL__
25602+
dece6358
AM
25603+struct dentry;
25604+struct super_block;
1facf9fc 25605+
25606+#ifdef CONFIG_AUFS_BDEV_LOOP
392086de
AM
25607+/* drivers/block/loop.c */
25608+struct file *loop_backing_file(struct super_block *sb);
25609+
1facf9fc 25610+/* loop.c */
b752ccd1 25611+int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
1facf9fc 25612+int au_test_loopback_kthread(void);
87a755f4
AM
25613+void au_warn_loopback(struct super_block *h_sb);
25614+
25615+int au_loopback_init(void);
25616+void au_loopback_fin(void);
1facf9fc 25617+#else
83b672a5 25618+AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
eca801bf 25619+
4a4d8108 25620+AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
b752ccd1 25621+ struct dentry *h_adding)
4a4d8108 25622+AuStubInt0(au_test_loopback_kthread, void)
87a755f4
AM
25623+AuStubVoid(au_warn_loopback, struct super_block *h_sb)
25624+
25625+AuStubInt0(au_loopback_init, void)
25626+AuStubVoid(au_loopback_fin, void)
1facf9fc 25627+#endif /* BLK_DEV_LOOP */
25628+
25629+#endif /* __KERNEL__ */
25630+#endif /* __AUFS_LOOP_H__ */
7f207e10 25631diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
eca34b5c 25632--- /usr/share/empty/fs/aufs/magic.mk 1970-01-01 01:00:00.000000000 +0100
594d0238 25633+++ linux/fs/aufs/magic.mk 2022-11-05 23:02:18.965889284 +0100
2121bcd9
AM
25634@@ -0,0 +1,31 @@
25635+# SPDX-License-Identifier: GPL-2.0
1facf9fc 25636+
25637+# defined in ${srctree}/fs/fuse/inode.c
25638+# tristate
25639+ifdef CONFIG_FUSE_FS
25640+ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
25641+endif
25642+
1facf9fc 25643+# defined in ${srctree}/fs/xfs/xfs_sb.h
25644+# tristate
25645+ifdef CONFIG_XFS_FS
25646+ccflags-y += -DXFS_SB_MAGIC=0x58465342
25647+endif
25648+
25649+# defined in ${srctree}/fs/configfs/mount.c
25650+# tristate
25651+ifdef CONFIG_CONFIGFS_FS
25652+ccflags-y += -DCONFIGFS_MAGIC=0x62656570
25653+endif
25654+
1facf9fc 25655+# defined in ${srctree}/fs/ubifs/ubifs.h
25656+# tristate
25657+ifdef CONFIG_UBIFS_FS
25658+ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
25659+endif
4a4d8108
AM
25660+
25661+# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
25662+# tristate
25663+ifdef CONFIG_HFSPLUS_FS
25664+ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
25665+endif
7f207e10 25666diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
eca34b5c 25667--- /usr/share/empty/fs/aufs/Makefile 1970-01-01 01:00:00.000000000 +0100
594d0238 25668+++ linux/fs/aufs/Makefile 2022-11-05 23:02:18.959222617 +0100
2121bcd9
AM
25669@@ -0,0 +1,46 @@
25670+# SPDX-License-Identifier: GPL-2.0
4a4d8108
AM
25671+
25672+include ${src}/magic.mk
25673+ifeq (${CONFIG_AUFS_FS},m)
25674+include ${src}/conf.mk
25675+endif
25676+-include ${src}/priv_def.mk
25677+
25678+# cf. include/linux/kernel.h
25679+# enable pr_debug
25680+ccflags-y += -DDEBUG
f6c5ef8b
AM
25681+# sparse requires the full pathname
25682+ifdef M
523b37e3 25683+ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
f6c5ef8b 25684+else
523b37e3 25685+ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
f6c5ef8b 25686+endif
4a4d8108
AM
25687+
25688+obj-$(CONFIG_AUFS_FS) += aufs.o
f4d37d76 25689+aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o fsctx.o \
4a4d8108 25690+ wkq.o vfsub.o dcsub.o \
e49829fe 25691+ cpup.o whout.o wbr_policy.o \
4a4d8108
AM
25692+ dinfo.o dentry.o \
25693+ dynop.o \
25694+ finfo.o file.o f_op.o \
25695+ dir.o vdir.o \
25696+ iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
c2b27bf2 25697+ mvdown.o ioctl.o
4a4d8108
AM
25698+
25699+# all are boolean
e49829fe 25700+aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
4a4d8108
AM
25701+aufs-$(CONFIG_SYSFS) += sysfs.o
25702+aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
25703+aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
25704+aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
25705+aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
4a4d8108 25706+aufs-$(CONFIG_AUFS_EXPORT) += export.o
c1595e42
JR
25707+aufs-$(CONFIG_AUFS_XATTR) += xattr.o
25708+aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
8b6a4947 25709+aufs-$(CONFIG_AUFS_DIRREN) += dirren.o
076b876e 25710+aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
4a4d8108
AM
25711+aufs-$(CONFIG_AUFS_POLL) += poll.o
25712+aufs-$(CONFIG_AUFS_RDU) += rdu.o
4a4d8108
AM
25713+aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
25714+aufs-$(CONFIG_AUFS_DEBUG) += debug.o
25715+aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
7f207e10 25716diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
eca34b5c 25717--- /usr/share/empty/fs/aufs/module.c 1970-01-01 01:00:00.000000000 +0100
594d0238 25718+++ linux/fs/aufs/module.c 2022-11-05 23:02:18.965889284 +0100
062440b3 25719@@ -0,0 +1,273 @@
cd7a4cd9 25720+// SPDX-License-Identifier: GPL-2.0
1facf9fc 25721+/*
0dcfbb52 25722+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 25723+ *
0dcfbb52 25724+ * This program is free software; you can redistribute it and/or modify
1facf9fc 25725+ * it under the terms of the GNU General Public License as published by
25726+ * the Free Software Foundation; either version 2 of the License, or
25727+ * (at your option) any later version.
dece6358
AM
25728+ *
25729+ * This program is distributed in the hope that it will be useful,
25730+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
25731+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25732+ * GNU General Public License for more details.
25733+ *
25734+ * You should have received a copy of the GNU General Public License
523b37e3 25735+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 25736+ */
25737+
25738+/*
25739+ * module global variables and operations
25740+ */
25741+
25742+#include <linux/module.h>
25743+#include <linux/seq_file.h>
25744+#include "aufs.h"
25745+
e2f27e51
AM
25746+/* shrinkable realloc */
25747+void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
1facf9fc 25748+{
e2f27e51
AM
25749+ size_t sz;
25750+ int diff;
1facf9fc 25751+
e2f27e51
AM
25752+ sz = 0;
25753+ diff = -1;
25754+ if (p) {
25755+#if 0 /* unused */
25756+ if (!new_sz) {
9f237c51 25757+ au_kfree_rcu(p);
e2f27e51
AM
25758+ p = NULL;
25759+ goto out;
25760+ }
25761+#else
25762+ AuDebugOn(!new_sz);
25763+#endif
25764+ sz = ksize(p);
25765+ diff = au_kmidx_sub(sz, new_sz);
25766+ }
25767+ if (sz && !diff)
25768+ goto out;
25769+
25770+ if (sz < new_sz)
25771+ /* expand or SLOB */
25772+ p = krealloc(p, new_sz, gfp);
25773+ else if (new_sz < sz && may_shrink) {
25774+ /* shrink */
25775+ void *q;
25776+
25777+ q = kmalloc(new_sz, gfp);
25778+ if (q) {
25779+ if (p) {
25780+ memcpy(q, p, new_sz);
9f237c51 25781+ au_kfree_try_rcu(p);
e2f27e51
AM
25782+ }
25783+ p = q;
25784+ } else
25785+ p = NULL;
25786+ }
25787+
25788+out:
25789+ return p;
25790+}
25791+
25792+void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
25793+ int may_shrink)
25794+{
25795+ p = au_krealloc(p, new_sz, gfp, may_shrink);
25796+ if (p && new_sz > nused)
1facf9fc 25797+ memset(p + nused, 0, new_sz - nused);
25798+ return p;
25799+}
25800+
25801+/* ---------------------------------------------------------------------- */
1facf9fc 25802+/*
25803+ * aufs caches
25804+ */
1c60b727 25805+struct kmem_cache *au_cache[AuCache_Last];
5afbbe0d
AM
25806+
25807+static void au_cache_fin(void)
25808+{
25809+ int i;
25810+
25811+ /*
25812+ * Make sure all delayed rcu free inodes are flushed before we
25813+ * destroy cache.
25814+ */
25815+ rcu_barrier();
25816+
25817+ /* excluding AuCache_HNOTIFY */
25818+ BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
25819+ for (i = 0; i < AuCache_HNOTIFY; i++) {
1c60b727
AM
25820+ kmem_cache_destroy(au_cache[i]);
25821+ au_cache[i] = NULL;
5afbbe0d
AM
25822+ }
25823+}
25824+
1facf9fc 25825+static int __init au_cache_init(void)
25826+{
1c60b727
AM
25827+ au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
25828+ if (au_cache[AuCache_DINFO])
027c5e7a 25829+ /* SLAB_DESTROY_BY_RCU */
1c60b727 25830+ au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
f0c0a007 25831+ au_icntnr_init_once);
1c60b727
AM
25832+ if (au_cache[AuCache_ICNTNR])
25833+ au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
f0c0a007 25834+ au_fi_init_once);
1c60b727
AM
25835+ if (au_cache[AuCache_FINFO])
25836+ au_cache[AuCache_VDIR] = AuCache(au_vdir);
25837+ if (au_cache[AuCache_VDIR])
25838+ au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
25839+ if (au_cache[AuCache_DEHSTR])
1facf9fc 25840+ return 0;
25841+
5afbbe0d 25842+ au_cache_fin();
1facf9fc 25843+ return -ENOMEM;
25844+}
25845+
1facf9fc 25846+/* ---------------------------------------------------------------------- */
25847+
25848+int au_dir_roflags;
25849+
e49829fe 25850+#ifdef CONFIG_AUFS_SBILIST
1e00d052
AM
25851+/*
25852+ * iterate_supers_type() doesn't protect us from
25853+ * remounting (branch management)
25854+ */
8b6a4947 25855+struct hlist_bl_head au_sbilist;
e49829fe
JR
25856+#endif
25857+
1facf9fc 25858+/*
25859+ * functions for module interface.
25860+ */
25861+MODULE_LICENSE("GPL");
25862+/* MODULE_LICENSE("GPL v2"); */
dece6358 25863+MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
1facf9fc 25864+MODULE_DESCRIPTION(AUFS_NAME
25865+ " -- Advanced multi layered unification filesystem");
25866+MODULE_VERSION(AUFS_VERSION);
c06a8ce3 25867+MODULE_ALIAS_FS(AUFS_NAME);
1facf9fc 25868+
1facf9fc 25869+/* this module parameter has no meaning when SYSFS is disabled */
25870+int sysaufs_brs = 1;
25871+MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
cd7a4cd9 25872+module_param_named(brs, sysaufs_brs, int, 0444);
1facf9fc 25873+
076b876e 25874+/* this module parameter has no meaning when USER_NS is disabled */
8cdd5066 25875+bool au_userns;
076b876e 25876+MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
cd7a4cd9 25877+module_param_named(allow_userns, au_userns, bool, 0444);
076b876e 25878+
1facf9fc 25879+/* ---------------------------------------------------------------------- */
25880+
25881+static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
25882+
25883+int au_seq_path(struct seq_file *seq, struct path *path)
25884+{
79b8bda9
AM
25885+ int err;
25886+
25887+ err = seq_path(seq, path, au_esc_chars);
1c60b727 25888+ if (err >= 0)
79b8bda9 25889+ err = 0;
1c60b727 25890+ else
79b8bda9
AM
25891+ err = -ENOMEM;
25892+
25893+ return err;
1facf9fc 25894+}
25895+
25896+/* ---------------------------------------------------------------------- */
25897+
25898+static int __init aufs_init(void)
25899+{
25900+ int err, i;
25901+ char *p;
25902+
25903+ p = au_esc_chars;
25904+ for (i = 1; i <= ' '; i++)
25905+ *p++ = i;
25906+ *p++ = '\\';
25907+ *p++ = '\x7f';
25908+ *p = 0;
25909+
25910+ au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
25911+
b95c5147
AM
25912+ memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
25913+ for (i = 0; i < AuIop_Last; i++)
25914+ aufs_iop_nogetattr[i].getattr = NULL;
25915+
1c60b727 25916+ memset(au_cache, 0, sizeof(au_cache)); /* including hnotify */
f0c0a007 25917+
e49829fe 25918+ au_sbilist_init();
1facf9fc 25919+ sysaufs_brs_init();
25920+ au_debug_init();
4a4d8108 25921+ au_dy_init();
1facf9fc 25922+ err = sysaufs_init();
25923+ if (unlikely(err))
25924+ goto out;
062440b3 25925+ err = dbgaufs_init();
4f0767ce 25926+ if (unlikely(err))
953406b4 25927+ goto out_sysaufs;
062440b3
AM
25928+ err = au_procfs_init();
25929+ if (unlikely(err))
25930+ goto out_dbgaufs;
e49829fe
JR
25931+ err = au_wkq_init();
25932+ if (unlikely(err))
25933+ goto out_procfs;
87a755f4 25934+ err = au_loopback_init();
1facf9fc 25935+ if (unlikely(err))
25936+ goto out_wkq;
87a755f4
AM
25937+ err = au_hnotify_init();
25938+ if (unlikely(err))
25939+ goto out_loopback;
1facf9fc 25940+ err = au_sysrq_init();
25941+ if (unlikely(err))
25942+ goto out_hin;
25943+ err = au_cache_init();
25944+ if (unlikely(err))
25945+ goto out_sysrq;
076b876e
AM
25946+
25947+ aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
1facf9fc 25948+ err = register_filesystem(&aufs_fs_type);
25949+ if (unlikely(err))
25950+ goto out_cache;
076b876e 25951+
4a4d8108
AM
25952+ /* since we define pr_fmt, call printk directly */
25953+ printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
1facf9fc 25954+ goto out; /* success */
25955+
4f0767ce 25956+out_cache:
1facf9fc 25957+ au_cache_fin();
4f0767ce 25958+out_sysrq:
1facf9fc 25959+ au_sysrq_fin();
4f0767ce 25960+out_hin:
4a4d8108 25961+ au_hnotify_fin();
87a755f4
AM
25962+out_loopback:
25963+ au_loopback_fin();
4f0767ce 25964+out_wkq:
1facf9fc 25965+ au_wkq_fin();
e49829fe
JR
25966+out_procfs:
25967+ au_procfs_fin();
062440b3
AM
25968+out_dbgaufs:
25969+ dbgaufs_fin();
4f0767ce 25970+out_sysaufs:
1facf9fc 25971+ sysaufs_fin();
4a4d8108 25972+ au_dy_fin();
4f0767ce 25973+out:
1facf9fc 25974+ return err;
25975+}
25976+
25977+static void __exit aufs_exit(void)
25978+{
25979+ unregister_filesystem(&aufs_fs_type);
25980+ au_cache_fin();
25981+ au_sysrq_fin();
4a4d8108 25982+ au_hnotify_fin();
87a755f4 25983+ au_loopback_fin();
1facf9fc 25984+ au_wkq_fin();
e49829fe 25985+ au_procfs_fin();
062440b3 25986+ dbgaufs_fin();
1facf9fc 25987+ sysaufs_fin();
4a4d8108 25988+ au_dy_fin();
1facf9fc 25989+}
25990+
25991+module_init(aufs_init);
25992+module_exit(aufs_exit);
7f207e10 25993diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
eca34b5c 25994--- /usr/share/empty/fs/aufs/module.h 1970-01-01 01:00:00.000000000 +0100
594d0238 25995+++ linux/fs/aufs/module.h 2022-11-05 23:02:18.969222617 +0100
26eb093c 25996@@ -0,0 +1,180 @@
062440b3 25997+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 25998+/*
0dcfbb52 25999+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 26000+ *
0dcfbb52 26001+ * This program is free software; you can redistribute it and/or modify
1facf9fc 26002+ * it under the terms of the GNU General Public License as published by
26003+ * the Free Software Foundation; either version 2 of the License, or
26004+ * (at your option) any later version.
dece6358
AM
26005+ *
26006+ * This program is distributed in the hope that it will be useful,
26007+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
26008+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26009+ * GNU General Public License for more details.
26010+ *
26011+ * You should have received a copy of the GNU General Public License
523b37e3 26012+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 26013+ */
26014+
26015+/*
26016+ * module initialization and module-global
26017+ */
26018+
26019+#ifndef __AUFS_MODULE_H__
26020+#define __AUFS_MODULE_H__
26021+
26022+#ifdef __KERNEL__
26023+
26024+#include <linux/slab.h>
9f237c51
AM
26025+#include "debug.h"
26026+#include "dentry.h"
26027+#include "dir.h"
26028+#include "file.h"
26029+#include "inode.h"
1facf9fc 26030+
dece6358
AM
26031+struct path;
26032+struct seq_file;
26033+
1facf9fc 26034+/* module parameters */
1facf9fc 26035+extern int sysaufs_brs;
8cdd5066 26036+extern bool au_userns;
1facf9fc 26037+
26038+/* ---------------------------------------------------------------------- */
26039+
26040+extern int au_dir_roflags;
26041+
e2f27e51
AM
26042+void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
26043+void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
26044+ int may_shrink);
26045+
9f237c51
AM
26046+/*
26047+ * Comparing the size of the object with sizeof(struct rcu_head)
26048+ * case 1: object is always larger
26049+ * --> au_kfree_rcu() or au_kfree_do_rcu()
26050+ * case 2: object is always smaller
26051+ * --> au_kfree_small()
26052+ * case 3: object can be any size
26053+ * --> au_kfree_try_rcu()
26054+ */
26055+
26056+static inline void au_kfree_do_rcu(const void *p)
26057+{
26058+ struct {
26059+ struct rcu_head rcu;
26060+ } *a = (void *)p;
26061+
26062+ kfree_rcu(a, rcu);
26063+}
26064+
26065+#define au_kfree_rcu(_p) do { \
26066+ typeof(_p) p = (_p); \
26067+ BUILD_BUG_ON(sizeof(*p) < sizeof(struct rcu_head)); \
26068+ if (p) \
26069+ au_kfree_do_rcu(p); \
26070+ } while (0)
26071+
26072+#define au_kfree_do_sz_test(sz) (sz >= sizeof(struct rcu_head))
26073+#define au_kfree_sz_test(p) (p && au_kfree_do_sz_test(ksize(p)))
26074+
26075+static inline void au_kfree_try_rcu(const void *p)
26076+{
26077+ if (!p)
26078+ return;
26079+ if (au_kfree_sz_test(p))
26080+ au_kfree_do_rcu(p);
26081+ else
26082+ kfree(p);
26083+}
26084+
26085+static inline void au_kfree_small(const void *p)
26086+{
26087+ if (!p)
26088+ return;
26089+ AuDebugOn(au_kfree_sz_test(p));
26090+ kfree(p);
26091+}
26092+
e2f27e51
AM
26093+static inline int au_kmidx_sub(size_t sz, size_t new_sz)
26094+{
26095+#ifndef CONFIG_SLOB
6d176551 26096+ return __kmalloc_index(sz, false) - __kmalloc_index(new_sz, false);
e2f27e51
AM
26097+#else
26098+ return -1; /* SLOB is untested */
26099+#endif
26100+}
26101+
1facf9fc 26102+int au_seq_path(struct seq_file *seq, struct path *path);
26103+
e49829fe
JR
26104+#ifdef CONFIG_PROC_FS
26105+/* procfs.c */
26106+int __init au_procfs_init(void);
26107+void au_procfs_fin(void);
26108+#else
26109+AuStubInt0(au_procfs_init, void);
26110+AuStubVoid(au_procfs_fin, void);
26111+#endif
26112+
4f0767ce
JR
26113+/* ---------------------------------------------------------------------- */
26114+
1c60b727 26115+/* kmem cache */
1facf9fc 26116+enum {
26117+ AuCache_DINFO,
26118+ AuCache_ICNTNR,
26119+ AuCache_FINFO,
26120+ AuCache_VDIR,
26121+ AuCache_DEHSTR,
7eafdf33 26122+ AuCache_HNOTIFY, /* must be last */
1facf9fc 26123+ AuCache_Last
26124+};
26125+
1c60b727 26126+extern struct kmem_cache *au_cache[AuCache_Last];
f0c0a007 26127+
4a4d8108
AM
26128+#define AuCacheFlags (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
26129+#define AuCache(type) KMEM_CACHE(type, AuCacheFlags)
26130+#define AuCacheCtor(type, ctor) \
26131+ kmem_cache_create(#type, sizeof(struct type), \
26132+ __alignof__(struct type), AuCacheFlags, ctor)
1facf9fc 26133+
26eb093c 26134+#define AuCacheFuncAlloc(name, index) \
9f237c51 26135+ static inline struct au_##name *au_cache_alloc_##name(void) \
26eb093c
AM
26136+ { return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); }
26137+
26138+#define AuCacheFuncs(name, index) \
9f237c51
AM
26139+ static inline void au_cache_free_##name##_norcu(struct au_##name *p) \
26140+ { kmem_cache_free(au_cache[AuCache_##index], p); } \
26141+ \
26142+ static inline void au_cache_free_##name##_rcu_cb(struct rcu_head *rcu) \
26143+ { void *p = rcu; \
26144+ p -= offsetof(struct au_##name, rcu); \
26145+ kmem_cache_free(au_cache[AuCache_##index], p); } \
26146+ static inline void au_cache_free_##name##_rcu(struct au_##name *p) \
26147+ { BUILD_BUG_ON(sizeof(struct au_##name) < sizeof(struct rcu_head)); \
26148+ call_rcu(&p->rcu, au_cache_free_##name##_rcu_cb); } \
26149+ \
26150+ static inline void au_cache_free_##name(struct au_##name *p) \
26151+ { /* au_cache_free_##name##_norcu(p); */ \
26152+ au_cache_free_##name##_rcu(p); }
1facf9fc 26153+
26154+AuCacheFuncs(dinfo, DINFO);
26eb093c
AM
26155+AuCacheFuncAlloc(dinfo, DINFO);
26156+
1facf9fc 26157+AuCacheFuncs(icntnr, ICNTNR);
26eb093c
AM
26158+static inline struct au_icntnr *au_cache_alloc_icntnr(struct super_block *sb)
26159+{ return alloc_inode_sb(sb, au_cache[AuCache_ICNTNR], GFP_NOFS); }
26160+
1facf9fc 26161+AuCacheFuncs(finfo, FINFO);
26eb093c
AM
26162+AuCacheFuncAlloc(finfo, FINFO);
26163+
1facf9fc 26164+AuCacheFuncs(vdir, VDIR);
26eb093c
AM
26165+AuCacheFuncAlloc(vdir, VDIR);
26166+
4a4d8108 26167+AuCacheFuncs(vdir_dehstr, DEHSTR);
26eb093c
AM
26168+AuCacheFuncAlloc(vdir_dehstr, DEHSTR);
26169+
4a4d8108
AM
26170+#ifdef CONFIG_AUFS_HNOTIFY
26171+AuCacheFuncs(hnotify, HNOTIFY);
26eb093c 26172+AuCacheFuncAlloc(hnotify, HNOTIFY);
4a4d8108 26173+#endif
1facf9fc 26174+
4a4d8108
AM
26175+#endif /* __KERNEL__ */
26176+#endif /* __AUFS_MODULE_H__ */
c2b27bf2 26177diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
eca34b5c 26178--- /usr/share/empty/fs/aufs/mvdown.c 1970-01-01 01:00:00.000000000 +0100
594d0238 26179+++ linux/fs/aufs/mvdown.c 2022-11-05 23:02:18.969222617 +0100
ba1aed25 26180@@ -0,0 +1,706 @@
cd7a4cd9 26181+// SPDX-License-Identifier: GPL-2.0
c2b27bf2 26182+/*
0dcfbb52 26183+ * Copyright (C) 2011-2022 Junjiro R. Okajima
c2b27bf2 26184+ *
0dcfbb52 26185+ * This program is free software; you can redistribute it and/or modify
c2b27bf2
AM
26186+ * it under the terms of the GNU General Public License as published by
26187+ * the Free Software Foundation; either version 2 of the License, or
26188+ * (at your option) any later version.
26189+ *
26190+ * This program is distributed in the hope that it will be useful,
26191+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
26192+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26193+ * GNU General Public License for more details.
26194+ *
26195+ * You should have received a copy of the GNU General Public License
523b37e3
AM
26196+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
26197+ */
26198+
26199+/*
26200+ * move-down, opposite of copy-up
c2b27bf2
AM
26201+ */
26202+
26203+#include "aufs.h"
26204+
c2b27bf2
AM
26205+struct au_mvd_args {
26206+ struct {
c2b27bf2
AM
26207+ struct super_block *h_sb;
26208+ struct dentry *h_parent;
26209+ struct au_hinode *hdir;
392086de 26210+ struct inode *h_dir, *h_inode;
c1595e42 26211+ struct au_pin pin;
c2b27bf2
AM
26212+ } info[AUFS_MVDOWN_NARRAY];
26213+
26214+ struct aufs_mvdown mvdown;
26215+ struct dentry *dentry, *parent;
26216+ struct inode *inode, *dir;
26217+ struct super_block *sb;
26218+ aufs_bindex_t bopq, bwh, bfound;
26219+ unsigned char rename_lock;
c2b27bf2
AM
26220+};
26221+
392086de 26222+#define mvd_errno mvdown.au_errno
076b876e
AM
26223+#define mvd_bsrc mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
26224+#define mvd_src_brid mvdown.stbr[AUFS_MVDOWN_UPPER].brid
26225+#define mvd_bdst mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
26226+#define mvd_dst_brid mvdown.stbr[AUFS_MVDOWN_LOWER].brid
c2b27bf2 26227+
392086de
AM
26228+#define mvd_h_src_sb info[AUFS_MVDOWN_UPPER].h_sb
26229+#define mvd_h_src_parent info[AUFS_MVDOWN_UPPER].h_parent
26230+#define mvd_hdir_src info[AUFS_MVDOWN_UPPER].hdir
26231+#define mvd_h_src_dir info[AUFS_MVDOWN_UPPER].h_dir
26232+#define mvd_h_src_inode info[AUFS_MVDOWN_UPPER].h_inode
c1595e42 26233+#define mvd_pin_src info[AUFS_MVDOWN_UPPER].pin
392086de
AM
26234+
26235+#define mvd_h_dst_sb info[AUFS_MVDOWN_LOWER].h_sb
26236+#define mvd_h_dst_parent info[AUFS_MVDOWN_LOWER].h_parent
26237+#define mvd_hdir_dst info[AUFS_MVDOWN_LOWER].hdir
26238+#define mvd_h_dst_dir info[AUFS_MVDOWN_LOWER].h_dir
26239+#define mvd_h_dst_inode info[AUFS_MVDOWN_LOWER].h_inode
c1595e42 26240+#define mvd_pin_dst info[AUFS_MVDOWN_LOWER].pin
c2b27bf2
AM
26241+
26242+#define AU_MVD_PR(flag, ...) do { \
26243+ if (flag) \
26244+ pr_err(__VA_ARGS__); \
26245+ } while (0)
26246+
076b876e
AM
26247+static int find_lower_writable(struct au_mvd_args *a)
26248+{
26249+ struct super_block *sb;
5afbbe0d 26250+ aufs_bindex_t bindex, bbot;
076b876e
AM
26251+ struct au_branch *br;
26252+
26253+ sb = a->sb;
26254+ bindex = a->mvd_bsrc;
5afbbe0d 26255+ bbot = au_sbbot(sb);
076b876e 26256+ if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
5afbbe0d 26257+ for (bindex++; bindex <= bbot; bindex++) {
076b876e
AM
26258+ br = au_sbr(sb, bindex);
26259+ if (au_br_fhsm(br->br_perm)
8b6a4947 26260+ && !sb_rdonly(au_br_sb(br)))
076b876e
AM
26261+ return bindex;
26262+ }
26263+ else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
5afbbe0d 26264+ for (bindex++; bindex <= bbot; bindex++) {
076b876e
AM
26265+ br = au_sbr(sb, bindex);
26266+ if (!au_br_rdonly(br))
26267+ return bindex;
26268+ }
26269+ else
5afbbe0d 26270+ for (bindex++; bindex <= bbot; bindex++) {
076b876e 26271+ br = au_sbr(sb, bindex);
8b6a4947 26272+ if (!sb_rdonly(au_br_sb(br))) {
076b876e
AM
26273+ if (au_br_rdonly(br))
26274+ a->mvdown.flags
26275+ |= AUFS_MVDOWN_ROLOWER_R;
26276+ return bindex;
26277+ }
26278+ }
26279+
26280+ return -1;
26281+}
26282+
c2b27bf2 26283+/* make the parent dir on bdst */
392086de 26284+static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
26285+{
26286+ int err;
26287+
26288+ err = 0;
26289+ a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
26290+ a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
26291+ a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
26292+ a->mvd_h_dst_parent = NULL;
5afbbe0d 26293+ if (au_dbbot(a->parent) >= a->mvd_bdst)
c2b27bf2
AM
26294+ a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
26295+ if (!a->mvd_h_dst_parent) {
26296+ err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
26297+ if (unlikely(err)) {
392086de 26298+ AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
c2b27bf2
AM
26299+ goto out;
26300+ }
26301+ a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
26302+ }
26303+
26304+out:
26305+ AuTraceErr(err);
26306+ return err;
26307+}
26308+
26309+/* lock them all */
392086de 26310+static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
26311+{
26312+ int err;
26313+ struct dentry *h_trap;
26314+
26315+ a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
26316+ a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
c1595e42
JR
26317+ err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
26318+ au_opt_udba(a->sb),
26319+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
26320+ AuTraceErr(err);
26321+ if (unlikely(err)) {
26322+ AU_MVD_PR(dmsg, "pin_dst failed\n");
26323+ goto out;
26324+ }
26325+
c2b27bf2
AM
26326+ if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
26327+ a->rename_lock = 0;
c1595e42
JR
26328+ au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
26329+ AuLsc_DI_PARENT, AuLsc_I_PARENT3,
26330+ au_opt_udba(a->sb),
26331+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
26332+ err = au_do_pin(&a->mvd_pin_src);
26333+ AuTraceErr(err);
5527c038 26334+ a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
c1595e42
JR
26335+ if (unlikely(err)) {
26336+ AU_MVD_PR(dmsg, "pin_src failed\n");
26337+ goto out_dst;
26338+ }
26339+ goto out; /* success */
c2b27bf2
AM
26340+ }
26341+
c2b27bf2 26342+ a->rename_lock = 1;
c1595e42
JR
26343+ au_pin_hdir_unlock(&a->mvd_pin_dst);
26344+ err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
26345+ au_opt_udba(a->sb),
26346+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
26347+ AuTraceErr(err);
5527c038 26348+ a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
c1595e42
JR
26349+ if (unlikely(err)) {
26350+ AU_MVD_PR(dmsg, "pin_src failed\n");
26351+ au_pin_hdir_lock(&a->mvd_pin_dst);
26352+ goto out_dst;
26353+ }
26354+ au_pin_hdir_unlock(&a->mvd_pin_src);
c2b27bf2
AM
26355+ h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
26356+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
26357+ if (h_trap) {
26358+ err = (h_trap != a->mvd_h_src_parent);
26359+ if (err)
26360+ err = (h_trap != a->mvd_h_dst_parent);
26361+ }
26362+ BUG_ON(err); /* it should never happen */
c1595e42
JR
26363+ if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
26364+ err = -EBUSY;
26365+ AuTraceErr(err);
26366+ vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
26367+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
26368+ au_pin_hdir_lock(&a->mvd_pin_src);
26369+ au_unpin(&a->mvd_pin_src);
26370+ au_pin_hdir_lock(&a->mvd_pin_dst);
26371+ goto out_dst;
26372+ }
26373+ goto out; /* success */
c2b27bf2 26374+
c1595e42
JR
26375+out_dst:
26376+ au_unpin(&a->mvd_pin_dst);
c2b27bf2
AM
26377+out:
26378+ AuTraceErr(err);
26379+ return err;
26380+}
26381+
392086de 26382+static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2 26383+{
c1595e42
JR
26384+ if (!a->rename_lock)
26385+ au_unpin(&a->mvd_pin_src);
26386+ else {
c2b27bf2
AM
26387+ vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
26388+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
c1595e42
JR
26389+ au_pin_hdir_lock(&a->mvd_pin_src);
26390+ au_unpin(&a->mvd_pin_src);
26391+ au_pin_hdir_lock(&a->mvd_pin_dst);
26392+ }
26393+ au_unpin(&a->mvd_pin_dst);
c2b27bf2
AM
26394+}
26395+
26396+/* copy-down the file */
392086de 26397+static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
26398+{
26399+ int err;
26400+ struct au_cp_generic cpg = {
26401+ .dentry = a->dentry,
26402+ .bdst = a->mvd_bdst,
26403+ .bsrc = a->mvd_bsrc,
26404+ .len = -1,
c1595e42 26405+ .pin = &a->mvd_pin_dst,
c2b27bf2
AM
26406+ .flags = AuCpup_DTIME | AuCpup_HOPEN
26407+ };
26408+
26409+ AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
392086de
AM
26410+ if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
26411+ au_fset_cpup(cpg.flags, OVERWRITE);
26412+ if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
26413+ au_fset_cpup(cpg.flags, RWDST);
c2b27bf2
AM
26414+ err = au_sio_cpdown_simple(&cpg);
26415+ if (unlikely(err))
392086de 26416+ AU_MVD_PR(dmsg, "cpdown failed\n");
c2b27bf2
AM
26417+
26418+ AuTraceErr(err);
26419+ return err;
26420+}
26421+
26422+/*
26423+ * unlink the whiteout on bdst if exist which may be created by UDBA while we
26424+ * were sleeping
26425+ */
392086de 26426+static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
26427+{
26428+ int err;
26429+ struct path h_path;
26430+ struct au_branch *br;
523b37e3 26431+ struct inode *delegated;
c2b27bf2
AM
26432+
26433+ br = au_sbr(a->sb, a->mvd_bdst);
26434+ h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
26435+ err = PTR_ERR(h_path.dentry);
26436+ if (IS_ERR(h_path.dentry)) {
392086de 26437+ AU_MVD_PR(dmsg, "wh_lkup failed\n");
c2b27bf2
AM
26438+ goto out;
26439+ }
26440+
26441+ err = 0;
5527c038 26442+ if (d_is_positive(h_path.dentry)) {
c2b27bf2 26443+ h_path.mnt = au_br_mnt(br);
523b37e3 26444+ delegated = NULL;
5527c038 26445+ err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
523b37e3
AM
26446+ &delegated, /*force*/0);
26447+ if (unlikely(err == -EWOULDBLOCK)) {
26448+ pr_warn("cannot retry for NFSv4 delegation"
26449+ " for an internal unlink\n");
26450+ iput(delegated);
26451+ }
c2b27bf2 26452+ if (unlikely(err))
392086de 26453+ AU_MVD_PR(dmsg, "wh_unlink failed\n");
c2b27bf2
AM
26454+ }
26455+ dput(h_path.dentry);
26456+
26457+out:
26458+ AuTraceErr(err);
26459+ return err;
26460+}
26461+
26462+/*
26463+ * unlink the topmost h_dentry
c2b27bf2 26464+ */
392086de 26465+static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
26466+{
26467+ int err;
26468+ struct path h_path;
523b37e3 26469+ struct inode *delegated;
c2b27bf2
AM
26470+
26471+ h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
26472+ h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
523b37e3
AM
26473+ delegated = NULL;
26474+ err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
26475+ if (unlikely(err == -EWOULDBLOCK)) {
26476+ pr_warn("cannot retry for NFSv4 delegation"
26477+ " for an internal unlink\n");
26478+ iput(delegated);
26479+ }
c2b27bf2 26480+ if (unlikely(err))
392086de 26481+ AU_MVD_PR(dmsg, "unlink failed\n");
c2b27bf2
AM
26482+
26483+ AuTraceErr(err);
26484+ return err;
26485+}
26486+
076b876e
AM
26487+/* Since mvdown succeeded, we ignore an error of this function */
26488+static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
26489+{
26490+ int err;
26491+ struct au_branch *br;
26492+
26493+ a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
26494+ br = au_sbr(a->sb, a->mvd_bsrc);
26495+ err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
26496+ if (!err) {
26497+ br = au_sbr(a->sb, a->mvd_bdst);
26498+ a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
26499+ err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
26500+ }
26501+ if (!err)
26502+ a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
26503+ else
26504+ AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
26505+}
26506+
c2b27bf2
AM
26507+/*
26508+ * copy-down the file and unlink the bsrc file.
26509+ * - unlink the bdst whout if exist
26510+ * - copy-down the file (with whtmp name and rename)
26511+ * - unlink the bsrc file
26512+ */
392086de 26513+static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
26514+{
26515+ int err;
26516+
392086de 26517+ err = au_do_mkdir(dmsg, a);
c2b27bf2 26518+ if (!err)
392086de 26519+ err = au_do_lock(dmsg, a);
c2b27bf2
AM
26520+ if (unlikely(err))
26521+ goto out;
26522+
26523+ /*
26524+ * do not revert the activities we made on bdst since they should be
26525+ * harmless in aufs.
26526+ */
26527+
392086de 26528+ err = au_do_cpdown(dmsg, a);
c2b27bf2 26529+ if (!err)
392086de
AM
26530+ err = au_do_unlink_wh(dmsg, a);
26531+ if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
26532+ err = au_do_unlink(dmsg, a);
c2b27bf2
AM
26533+ if (unlikely(err))
26534+ goto out_unlock;
26535+
c1595e42
JR
26536+ AuDbg("%pd2, 0x%x, %d --> %d\n",
26537+ a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
076b876e
AM
26538+ if (find_lower_writable(a) < 0)
26539+ a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
26540+
26541+ if (a->mvdown.flags & AUFS_MVDOWN_STFS)
26542+ au_do_stfs(dmsg, a);
26543+
c2b27bf2 26544+ /* maintain internal array */
392086de
AM
26545+ if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
26546+ au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
5afbbe0d 26547+ au_set_dbtop(a->dentry, a->mvd_bdst);
392086de 26548+ au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
5afbbe0d 26549+ au_set_ibtop(a->inode, a->mvd_bdst);
79b8bda9
AM
26550+ } else {
26551+ /* hide the lower */
26552+ au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
5afbbe0d 26553+ au_set_dbbot(a->dentry, a->mvd_bsrc);
79b8bda9 26554+ au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
5afbbe0d 26555+ au_set_ibbot(a->inode, a->mvd_bsrc);
392086de 26556+ }
5afbbe0d
AM
26557+ if (au_dbbot(a->dentry) < a->mvd_bdst)
26558+ au_set_dbbot(a->dentry, a->mvd_bdst);
26559+ if (au_ibbot(a->inode) < a->mvd_bdst)
26560+ au_set_ibbot(a->inode, a->mvd_bdst);
c2b27bf2
AM
26561+
26562+out_unlock:
392086de 26563+ au_do_unlock(dmsg, a);
c2b27bf2
AM
26564+out:
26565+ AuTraceErr(err);
26566+ return err;
26567+}
26568+
26569+/* ---------------------------------------------------------------------- */
26570+
c2b27bf2 26571+/* make sure the file is idle */
392086de 26572+static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
26573+{
26574+ int err, plinked;
c2b27bf2
AM
26575+
26576+ err = 0;
c2b27bf2 26577+ plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
5afbbe0d 26578+ if (au_dbtop(a->dentry) == a->mvd_bsrc
c1595e42 26579+ && au_dcount(a->dentry) == 1
c2b27bf2 26580+ && atomic_read(&a->inode->i_count) == 1
392086de 26581+ /* && a->mvd_h_src_inode->i_nlink == 1 */
c2b27bf2
AM
26582+ && (!plinked || !au_plink_test(a->inode))
26583+ && a->inode->i_nlink == 1)
26584+ goto out;
26585+
26586+ err = -EBUSY;
392086de 26587+ AU_MVD_PR(dmsg,
c1595e42 26588+ "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
5afbbe0d 26589+ a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
c2b27bf2 26590+ atomic_read(&a->inode->i_count), a->inode->i_nlink,
392086de 26591+ a->mvd_h_src_inode->i_nlink,
c2b27bf2
AM
26592+ plinked, plinked ? au_plink_test(a->inode) : 0);
26593+
26594+out:
26595+ AuTraceErr(err);
26596+ return err;
26597+}
26598+
26599+/* make sure the parent dir is fine */
392086de 26600+static int au_mvd_args_parent(const unsigned char dmsg,
c2b27bf2
AM
26601+ struct au_mvd_args *a)
26602+{
26603+ int err;
26604+ aufs_bindex_t bindex;
26605+
26606+ err = 0;
26607+ if (unlikely(au_alive_dir(a->parent))) {
26608+ err = -ENOENT;
392086de 26609+ AU_MVD_PR(dmsg, "parent dir is dead\n");
c2b27bf2
AM
26610+ goto out;
26611+ }
26612+
26613+ a->bopq = au_dbdiropq(a->parent);
26614+ bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
26615+ AuDbg("b%d\n", bindex);
26616+ if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
26617+ || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
26618+ err = -EINVAL;
392086de
AM
26619+ a->mvd_errno = EAU_MVDOWN_OPAQUE;
26620+ AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
c2b27bf2
AM
26621+ a->bopq, a->mvd_bdst);
26622+ }
26623+
26624+out:
26625+ AuTraceErr(err);
26626+ return err;
26627+}
26628+
392086de 26629+static int au_mvd_args_intermediate(const unsigned char dmsg,
c2b27bf2
AM
26630+ struct au_mvd_args *a)
26631+{
26632+ int err;
26633+ struct au_dinfo *dinfo, *tmp;
26634+
26635+ /* lookup the next lower positive entry */
26636+ err = -ENOMEM;
26637+ tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
26638+ if (unlikely(!tmp))
26639+ goto out;
26640+
26641+ a->bfound = -1;
26642+ a->bwh = -1;
26643+ dinfo = au_di(a->dentry);
26644+ au_di_cp(tmp, dinfo);
26645+ au_di_swap(tmp, dinfo);
26646+
26647+ /* returns the number of positive dentries */
5afbbe0d
AM
26648+ err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
26649+ /* AuLkup_IGNORE_PERM */ 0);
c2b27bf2
AM
26650+ if (!err)
26651+ a->bwh = au_dbwh(a->dentry);
26652+ else if (err > 0)
5afbbe0d 26653+ a->bfound = au_dbtop(a->dentry);
c2b27bf2
AM
26654+
26655+ au_di_swap(tmp, dinfo);
26656+ au_rw_write_unlock(&tmp->di_rwsem);
26657+ au_di_free(tmp);
26658+ if (unlikely(err < 0))
392086de 26659+ AU_MVD_PR(dmsg, "failed look-up lower\n");
c2b27bf2
AM
26660+
26661+ /*
26662+ * here, we have these cases.
26663+ * bfound == -1
26664+ * no positive dentry under bsrc. there are more sub-cases.
26665+ * bwh < 0
26666+ * there no whiteout, we can safely move-down.
26667+ * bwh <= bsrc
26668+ * impossible
26669+ * bsrc < bwh && bwh < bdst
26670+ * there is a whiteout on RO branch. cannot proceed.
26671+ * bwh == bdst
26672+ * there is a whiteout on the RW target branch. it should
26673+ * be removed.
26674+ * bdst < bwh
26675+ * there is a whiteout somewhere unrelated branch.
26676+ * -1 < bfound && bfound <= bsrc
26677+ * impossible.
26678+ * bfound < bdst
26679+ * found, but it is on RO branch between bsrc and bdst. cannot
26680+ * proceed.
26681+ * bfound == bdst
26682+ * found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
26683+ * error.
26684+ * bdst < bfound
26685+ * found, after we create the file on bdst, it will be hidden.
26686+ */
26687+
26688+ AuDebugOn(a->bfound == -1
26689+ && a->bwh != -1
26690+ && a->bwh <= a->mvd_bsrc);
26691+ AuDebugOn(-1 < a->bfound
26692+ && a->bfound <= a->mvd_bsrc);
26693+
26694+ err = -EINVAL;
26695+ if (a->bfound == -1
26696+ && a->mvd_bsrc < a->bwh
26697+ && a->bwh != -1
26698+ && a->bwh < a->mvd_bdst) {
392086de
AM
26699+ a->mvd_errno = EAU_MVDOWN_WHITEOUT;
26700+ AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
c2b27bf2
AM
26701+ a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
26702+ goto out;
26703+ } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
392086de
AM
26704+ a->mvd_errno = EAU_MVDOWN_UPPER;
26705+ AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
c2b27bf2
AM
26706+ a->mvd_bdst, a->bfound);
26707+ goto out;
26708+ }
26709+
26710+ err = 0; /* success */
26711+
26712+out:
26713+ AuTraceErr(err);
26714+ return err;
26715+}
26716+
392086de 26717+static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
26718+{
26719+ int err;
26720+
392086de
AM
26721+ err = 0;
26722+ if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
26723+ && a->bfound == a->mvd_bdst)
26724+ err = -EEXIST;
c2b27bf2
AM
26725+ AuTraceErr(err);
26726+ return err;
26727+}
26728+
392086de 26729+static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
26730+{
26731+ int err;
26732+ struct au_branch *br;
26733+
26734+ err = -EISDIR;
26735+ if (unlikely(S_ISDIR(a->inode->i_mode)))
26736+ goto out;
26737+
26738+ err = -EINVAL;
392086de 26739+ if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
5afbbe0d 26740+ a->mvd_bsrc = au_ibtop(a->inode);
392086de
AM
26741+ else {
26742+ a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
26743+ if (unlikely(a->mvd_bsrc < 0
5afbbe0d
AM
26744+ || (a->mvd_bsrc < au_dbtop(a->dentry)
26745+ || au_dbbot(a->dentry) < a->mvd_bsrc
392086de 26746+ || !au_h_dptr(a->dentry, a->mvd_bsrc))
5afbbe0d
AM
26747+ || (a->mvd_bsrc < au_ibtop(a->inode)
26748+ || au_ibbot(a->inode) < a->mvd_bsrc
392086de
AM
26749+ || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
26750+ a->mvd_errno = EAU_MVDOWN_NOUPPER;
26751+ AU_MVD_PR(dmsg, "no upper\n");
26752+ goto out;
26753+ }
26754+ }
5afbbe0d 26755+ if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
392086de
AM
26756+ a->mvd_errno = EAU_MVDOWN_BOTTOM;
26757+ AU_MVD_PR(dmsg, "on the bottom\n");
c2b27bf2
AM
26758+ goto out;
26759+ }
392086de 26760+ a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
c2b27bf2
AM
26761+ br = au_sbr(a->sb, a->mvd_bsrc);
26762+ err = au_br_rdonly(br);
392086de
AM
26763+ if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
26764+ if (unlikely(err))
26765+ goto out;
26766+ } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
26767+ || IS_APPEND(a->mvd_h_src_inode))) {
26768+ if (err)
26769+ a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
26770+ /* go on */
26771+ } else
c2b27bf2
AM
26772+ goto out;
26773+
26774+ err = -EINVAL;
392086de
AM
26775+ if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
26776+ a->mvd_bdst = find_lower_writable(a);
26777+ if (unlikely(a->mvd_bdst < 0)) {
26778+ a->mvd_errno = EAU_MVDOWN_BOTTOM;
26779+ AU_MVD_PR(dmsg, "no writable lower branch\n");
26780+ goto out;
26781+ }
26782+ } else {
26783+ a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
26784+ if (unlikely(a->mvd_bdst < 0
5afbbe0d 26785+ || au_sbbot(a->sb) < a->mvd_bdst)) {
392086de
AM
26786+ a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
26787+ AU_MVD_PR(dmsg, "no lower brid\n");
26788+ goto out;
26789+ }
c2b27bf2
AM
26790+ }
26791+
392086de 26792+ err = au_mvd_args_busy(dmsg, a);
c2b27bf2 26793+ if (!err)
392086de 26794+ err = au_mvd_args_parent(dmsg, a);
c2b27bf2 26795+ if (!err)
392086de 26796+ err = au_mvd_args_intermediate(dmsg, a);
c2b27bf2 26797+ if (!err)
392086de 26798+ err = au_mvd_args_exist(dmsg, a);
c2b27bf2
AM
26799+ if (!err)
26800+ AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
26801+
26802+out:
26803+ AuTraceErr(err);
26804+ return err;
26805+}
26806+
26807+int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
26808+{
392086de
AM
26809+ int err, e;
26810+ unsigned char dmsg;
26811+ struct au_mvd_args *args;
79b8bda9 26812+ struct inode *inode;
c2b27bf2 26813+
79b8bda9 26814+ inode = d_inode(dentry);
c2b27bf2
AM
26815+ err = -EPERM;
26816+ if (unlikely(!capable(CAP_SYS_ADMIN)))
26817+ goto out;
26818+
392086de
AM
26819+ err = -ENOMEM;
26820+ args = kmalloc(sizeof(*args), GFP_NOFS);
26821+ if (unlikely(!args))
26822+ goto out;
26823+
26824+ err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
26825+ if (!err)
ba1aed25
AM
26826+ /* VERIFY_WRITE */
26827+ err = !access_ok(uarg, sizeof(*uarg));
c2b27bf2
AM
26828+ if (unlikely(err)) {
26829+ err = -EFAULT;
392086de
AM
26830+ AuTraceErr(err);
26831+ goto out_free;
c2b27bf2 26832+ }
392086de
AM
26833+ AuDbg("flags 0x%x\n", args->mvdown.flags);
26834+ args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
26835+ args->mvdown.au_errno = 0;
26836+ args->dentry = dentry;
79b8bda9 26837+ args->inode = inode;
392086de 26838+ args->sb = dentry->d_sb;
c2b27bf2 26839+
392086de
AM
26840+ err = -ENOENT;
26841+ dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
26842+ args->parent = dget_parent(dentry);
5527c038 26843+ args->dir = d_inode(args->parent);
febd17d6 26844+ inode_lock_nested(args->dir, I_MUTEX_PARENT);
392086de
AM
26845+ dput(args->parent);
26846+ if (unlikely(args->parent != dentry->d_parent)) {
26847+ AU_MVD_PR(dmsg, "parent dir is moved\n");
c2b27bf2
AM
26848+ goto out_dir;
26849+ }
26850+
febd17d6 26851+ inode_lock_nested(inode, I_MUTEX_CHILD);
b95c5147 26852+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
c2b27bf2
AM
26853+ if (unlikely(err))
26854+ goto out_inode;
26855+
392086de
AM
26856+ di_write_lock_parent(args->parent);
26857+ err = au_mvd_args(dmsg, args);
c2b27bf2
AM
26858+ if (unlikely(err))
26859+ goto out_parent;
26860+
392086de 26861+ err = au_do_mvdown(dmsg, args);
c2b27bf2
AM
26862+ if (unlikely(err))
26863+ goto out_parent;
c2b27bf2 26864+
392086de 26865+ au_cpup_attr_timesizes(args->dir);
79b8bda9
AM
26866+ au_cpup_attr_timesizes(inode);
26867+ if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
26868+ au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
c2b27bf2
AM
26869+ /* au_digen_dec(dentry); */
26870+
26871+out_parent:
392086de 26872+ di_write_unlock(args->parent);
c2b27bf2
AM
26873+ aufs_read_unlock(dentry, AuLock_DW);
26874+out_inode:
febd17d6 26875+ inode_unlock(inode);
c2b27bf2 26876+out_dir:
febd17d6 26877+ inode_unlock(args->dir);
392086de
AM
26878+out_free:
26879+ e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
26880+ if (unlikely(e))
26881+ err = -EFAULT;
9f237c51 26882+ au_kfree_rcu(args);
c2b27bf2
AM
26883+out:
26884+ AuTraceErr(err);
26885+ return err;
26886+}
26887diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
eca34b5c 26888--- /usr/share/empty/fs/aufs/opts.c 1970-01-01 01:00:00.000000000 +0100
594d0238 26889+++ linux/fs/aufs/opts.c 2022-11-05 23:02:18.969222617 +0100
f4d37d76 26890@@ -0,0 +1,1032 @@
cd7a4cd9 26891+// SPDX-License-Identifier: GPL-2.0
1facf9fc 26892+/*
0dcfbb52 26893+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 26894+ *
0dcfbb52 26895+ * This program is free software; you can redistribute it and/or modify
1facf9fc 26896+ * it under the terms of the GNU General Public License as published by
26897+ * the Free Software Foundation; either version 2 of the License, or
26898+ * (at your option) any later version.
dece6358
AM
26899+ *
26900+ * This program is distributed in the hope that it will be useful,
26901+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
26902+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26903+ * GNU General Public License for more details.
26904+ *
26905+ * You should have received a copy of the GNU General Public License
523b37e3 26906+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 26907+ */
26908+
26909+/*
26910+ * mount options/flags
26911+ */
26912+
26913+#include <linux/types.h> /* a distribution requires */
26914+#include <linux/parser.h>
26915+#include "aufs.h"
26916+
26917+/* ---------------------------------------------------------------------- */
26918+
076b876e 26919+static const char *au_parser_pattern(int val, match_table_t tbl)
1facf9fc 26920+{
076b876e
AM
26921+ struct match_token *p;
26922+
26923+ p = tbl;
26924+ while (p->pattern) {
26925+ if (p->token == val)
26926+ return p->pattern;
26927+ p++;
1facf9fc 26928+ }
26929+ BUG();
26930+ return "??";
26931+}
26932+
076b876e
AM
26933+static const char *au_optstr(int *val, match_table_t tbl)
26934+{
26935+ struct match_token *p;
26936+ int v;
26937+
26938+ v = *val;
2000de60
JR
26939+ if (!v)
26940+ goto out;
076b876e 26941+ p = tbl;
2000de60
JR
26942+ while (p->pattern) {
26943+ if (p->token
26944+ && (v & p->token) == p->token) {
076b876e
AM
26945+ *val &= ~p->token;
26946+ return p->pattern;
26947+ }
26948+ p++;
26949+ }
2000de60
JR
26950+
26951+out:
076b876e
AM
26952+ return NULL;
26953+}
26954+
1facf9fc 26955+/* ---------------------------------------------------------------------- */
26956+
1e00d052 26957+static match_table_t brperm = {
1facf9fc 26958+ {AuBrPerm_RO, AUFS_BRPERM_RO},
26959+ {AuBrPerm_RR, AUFS_BRPERM_RR},
26960+ {AuBrPerm_RW, AUFS_BRPERM_RW},
1e00d052
AM
26961+ {0, NULL}
26962+};
1facf9fc 26963+
86dc4139 26964+static match_table_t brattr = {
076b876e
AM
26965+ /* general */
26966+ {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
26967+ {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
c1595e42 26968+ /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
86dc4139 26969+ {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
2000de60 26970+#ifdef CONFIG_AUFS_FHSM
076b876e 26971+ {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
2000de60
JR
26972+#endif
26973+#ifdef CONFIG_AUFS_XATTR
c1595e42
JR
26974+ {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
26975+ {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
26976+ {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
26977+ {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
26978+ {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
26979+ {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
2000de60 26980+#endif
076b876e
AM
26981+
26982+ /* ro/rr branch */
1e00d052 26983+ {AuBrRAttr_WH, AUFS_BRRATTR_WH},
076b876e
AM
26984+
26985+ /* rw branch */
26986+ {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
1e00d052 26987+ {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
076b876e 26988+
1e00d052 26989+ {0, NULL}
1facf9fc 26990+};
26991+
1e00d052
AM
26992+static int br_attr_val(char *str, match_table_t table, substring_t args[])
26993+{
26994+ int attr, v;
26995+ char *p;
26996+
26997+ attr = 0;
26998+ do {
26999+ p = strchr(str, '+');
27000+ if (p)
27001+ *p = 0;
27002+ v = match_token(str, table, args);
076b876e
AM
27003+ if (v) {
27004+ if (v & AuBrAttr_CMOO_Mask)
27005+ attr &= ~AuBrAttr_CMOO_Mask;
1e00d052 27006+ attr |= v;
076b876e 27007+ } else {
1e00d052
AM
27008+ if (p)
27009+ *p = '+';
0c3ec466 27010+ pr_warn("ignored branch attribute %s\n", str);
1e00d052
AM
27011+ break;
27012+ }
27013+ if (p)
27014+ str = p + 1;
27015+ } while (p);
27016+
27017+ return attr;
27018+}
27019+
076b876e
AM
27020+static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
27021+{
27022+ int sz;
27023+ const char *p;
27024+ char *q;
27025+
076b876e
AM
27026+ q = str->a;
27027+ *q = 0;
27028+ p = au_optstr(&perm, brattr);
27029+ if (p) {
27030+ sz = strlen(p);
27031+ memcpy(q, p, sz + 1);
27032+ q += sz;
27033+ } else
27034+ goto out;
27035+
27036+ do {
27037+ p = au_optstr(&perm, brattr);
27038+ if (p) {
27039+ *q++ = '+';
27040+ sz = strlen(p);
27041+ memcpy(q, p, sz + 1);
27042+ q += sz;
27043+ }
27044+ } while (p);
27045+
27046+out:
c1595e42 27047+ return q - str->a;
076b876e
AM
27048+}
27049+
f4d37d76 27050+int au_br_perm_val(char *perm)
1facf9fc 27051+{
076b876e
AM
27052+ int val, bad, sz;
27053+ char *p;
1facf9fc 27054+ substring_t args[MAX_OPT_ARGS];
076b876e 27055+ au_br_perm_str_t attr;
1facf9fc 27056+
1e00d052
AM
27057+ p = strchr(perm, '+');
27058+ if (p)
27059+ *p = 0;
27060+ val = match_token(perm, brperm, args);
27061+ if (!val) {
27062+ if (p)
27063+ *p = '+';
0c3ec466 27064+ pr_warn("ignored branch permission %s\n", perm);
1e00d052
AM
27065+ val = AuBrPerm_RO;
27066+ goto out;
27067+ }
27068+ if (!p)
27069+ goto out;
27070+
076b876e
AM
27071+ val |= br_attr_val(p + 1, brattr, args);
27072+
27073+ bad = 0;
86dc4139 27074+ switch (val & AuBrPerm_Mask) {
1e00d052
AM
27075+ case AuBrPerm_RO:
27076+ case AuBrPerm_RR:
076b876e
AM
27077+ bad = val & AuBrWAttr_Mask;
27078+ val &= ~AuBrWAttr_Mask;
1e00d052
AM
27079+ break;
27080+ case AuBrPerm_RW:
076b876e
AM
27081+ bad = val & AuBrRAttr_Mask;
27082+ val &= ~AuBrRAttr_Mask;
1e00d052
AM
27083+ break;
27084+ }
c1595e42
JR
27085+
27086+ /*
27087+ * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
27088+ * does not treat it as an error, just warning.
27089+ * this is a tiny guard for the user operation.
27090+ */
27091+ if (val & AuBrAttr_UNPIN) {
27092+ bad |= AuBrAttr_UNPIN;
27093+ val &= ~AuBrAttr_UNPIN;
27094+ }
27095+
076b876e
AM
27096+ if (unlikely(bad)) {
27097+ sz = au_do_optstr_br_attr(&attr, bad);
27098+ AuDebugOn(!sz);
27099+ pr_warn("ignored branch attribute %s\n", attr.a);
27100+ }
1e00d052
AM
27101+
27102+out:
1facf9fc 27103+ return val;
27104+}
27105+
076b876e 27106+void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
1facf9fc 27107+{
076b876e
AM
27108+ au_br_perm_str_t attr;
27109+ const char *p;
27110+ char *q;
1e00d052
AM
27111+ int sz;
27112+
076b876e
AM
27113+ q = str->a;
27114+ p = au_optstr(&perm, brperm);
27115+ AuDebugOn(!p || !*p);
27116+ sz = strlen(p);
27117+ memcpy(q, p, sz + 1);
27118+ q += sz;
1e00d052 27119+
076b876e
AM
27120+ sz = au_do_optstr_br_attr(&attr, perm);
27121+ if (sz) {
27122+ *q++ = '+';
27123+ memcpy(q, attr.a, sz + 1);
1e00d052
AM
27124+ }
27125+
076b876e 27126+ AuDebugOn(strlen(str->a) >= sizeof(str->a));
1facf9fc 27127+}
27128+
27129+/* ---------------------------------------------------------------------- */
27130+
27131+static match_table_t udbalevel = {
27132+ {AuOpt_UDBA_REVAL, "reval"},
27133+ {AuOpt_UDBA_NONE, "none"},
4a4d8108
AM
27134+#ifdef CONFIG_AUFS_HNOTIFY
27135+ {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
27136+#ifdef CONFIG_AUFS_HFSNOTIFY
27137+ {AuOpt_UDBA_HNOTIFY, "fsnotify"},
4a4d8108 27138+#endif
1facf9fc 27139+#endif
27140+ {-1, NULL}
27141+};
27142+
f4d37d76 27143+int au_udba_val(char *str)
1facf9fc 27144+{
27145+ substring_t args[MAX_OPT_ARGS];
27146+
7f207e10 27147+ return match_token(str, udbalevel, args);
1facf9fc 27148+}
27149+
27150+const char *au_optstr_udba(int udba)
27151+{
076b876e 27152+ return au_parser_pattern(udba, udbalevel);
1facf9fc 27153+}
27154+
27155+/* ---------------------------------------------------------------------- */
27156+
27157+static match_table_t au_wbr_create_policy = {
27158+ {AuWbrCreate_TDP, "tdp"},
27159+ {AuWbrCreate_TDP, "top-down-parent"},
27160+ {AuWbrCreate_RR, "rr"},
27161+ {AuWbrCreate_RR, "round-robin"},
27162+ {AuWbrCreate_MFS, "mfs"},
27163+ {AuWbrCreate_MFS, "most-free-space"},
27164+ {AuWbrCreate_MFSV, "mfs:%d"},
27165+ {AuWbrCreate_MFSV, "most-free-space:%d"},
27166+
f2c43d5f
AM
27167+ /* top-down regardless the parent, and then mfs */
27168+ {AuWbrCreate_TDMFS, "tdmfs:%d"},
27169+ {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
27170+
1facf9fc 27171+ {AuWbrCreate_MFSRR, "mfsrr:%d"},
27172+ {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
27173+ {AuWbrCreate_PMFS, "pmfs"},
27174+ {AuWbrCreate_PMFSV, "pmfs:%d"},
392086de
AM
27175+ {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
27176+ {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
1facf9fc 27177+
27178+ {-1, NULL}
27179+};
27180+
1facf9fc 27181+static int au_wbr_mfs_wmark(substring_t *arg, char *str,
27182+ struct au_opt_wbr_create *create)
27183+{
27184+ int err;
27185+ unsigned long long ull;
27186+
27187+ err = 0;
a2654f78 27188+ if (!match_u64(arg, &ull))
1facf9fc 27189+ create->mfsrr_watermark = ull;
27190+ else {
4a4d8108 27191+ pr_err("bad integer in %s\n", str);
1facf9fc 27192+ err = -EINVAL;
27193+ }
27194+
27195+ return err;
27196+}
27197+
27198+static int au_wbr_mfs_sec(substring_t *arg, char *str,
27199+ struct au_opt_wbr_create *create)
27200+{
27201+ int n, err;
27202+
27203+ err = 0;
027c5e7a 27204+ if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
1facf9fc 27205+ create->mfs_second = n;
27206+ else {
4a4d8108 27207+ pr_err("bad integer in %s\n", str);
1facf9fc 27208+ err = -EINVAL;
27209+ }
27210+
27211+ return err;
27212+}
27213+
f4d37d76 27214+int au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
1facf9fc 27215+{
27216+ int err, e;
27217+ substring_t args[MAX_OPT_ARGS];
27218+
27219+ err = match_token(str, au_wbr_create_policy, args);
27220+ create->wbr_create = err;
27221+ switch (err) {
27222+ case AuWbrCreate_MFSRRV:
f2c43d5f 27223+ case AuWbrCreate_TDMFSV:
392086de 27224+ case AuWbrCreate_PMFSRRV:
1facf9fc 27225+ e = au_wbr_mfs_wmark(&args[0], str, create);
27226+ if (!e)
27227+ e = au_wbr_mfs_sec(&args[1], str, create);
27228+ if (unlikely(e))
27229+ err = e;
27230+ break;
27231+ case AuWbrCreate_MFSRR:
f2c43d5f 27232+ case AuWbrCreate_TDMFS:
392086de 27233+ case AuWbrCreate_PMFSRR:
1facf9fc 27234+ e = au_wbr_mfs_wmark(&args[0], str, create);
27235+ if (unlikely(e)) {
27236+ err = e;
27237+ break;
27238+ }
42a736d3 27239+ fallthrough;
1facf9fc 27240+ case AuWbrCreate_MFS:
27241+ case AuWbrCreate_PMFS:
027c5e7a 27242+ create->mfs_second = AUFS_MFS_DEF_SEC;
1facf9fc 27243+ break;
27244+ case AuWbrCreate_MFSV:
27245+ case AuWbrCreate_PMFSV:
27246+ e = au_wbr_mfs_sec(&args[0], str, create);
27247+ if (unlikely(e))
27248+ err = e;
27249+ break;
27250+ }
27251+
27252+ return err;
27253+}
27254+
27255+const char *au_optstr_wbr_create(int wbr_create)
27256+{
076b876e 27257+ return au_parser_pattern(wbr_create, au_wbr_create_policy);
1facf9fc 27258+}
27259+
27260+static match_table_t au_wbr_copyup_policy = {
27261+ {AuWbrCopyup_TDP, "tdp"},
27262+ {AuWbrCopyup_TDP, "top-down-parent"},
27263+ {AuWbrCopyup_BUP, "bup"},
27264+ {AuWbrCopyup_BUP, "bottom-up-parent"},
27265+ {AuWbrCopyup_BU, "bu"},
27266+ {AuWbrCopyup_BU, "bottom-up"},
27267+ {-1, NULL}
27268+};
27269+
f4d37d76 27270+int au_wbr_copyup_val(char *str)
1facf9fc 27271+{
27272+ substring_t args[MAX_OPT_ARGS];
27273+
27274+ return match_token(str, au_wbr_copyup_policy, args);
27275+}
27276+
27277+const char *au_optstr_wbr_copyup(int wbr_copyup)
27278+{
076b876e 27279+ return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
1facf9fc 27280+}
27281+
27282+/* ---------------------------------------------------------------------- */
27283+
f4d37d76
AM
27284+int au_opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
27285+ aufs_bindex_t bindex)
1facf9fc 27286+{
27287+ int err;
27288+ struct au_opt_add *add = &opt->add;
27289+ char *p;
27290+
27291+ add->bindex = bindex;
1e00d052 27292+ add->perm = AuBrPerm_RO;
1facf9fc 27293+ add->pathname = opt_str;
27294+ p = strchr(opt_str, '=');
27295+ if (p) {
27296+ *p++ = 0;
27297+ if (*p)
f4d37d76 27298+ add->perm = au_br_perm_val(p);
1facf9fc 27299+ }
27300+
f4d37d76 27301+ err = vfsub_kern_path(add->pathname, AuOpt_LkupDirFlags, &add->path);
1facf9fc 27302+ if (!err) {
27303+ if (!p) {
27304+ add->perm = AuBrPerm_RO;
27305+ if (au_test_fs_rr(add->path.dentry->d_sb))
27306+ add->perm = AuBrPerm_RR;
2121bcd9 27307+ else if (!bindex && !(sb_flags & SB_RDONLY))
1facf9fc 27308+ add->perm = AuBrPerm_RW;
27309+ }
27310+ opt->type = Opt_add;
27311+ goto out;
27312+ }
4a4d8108 27313+ pr_err("lookup failed %s (%d)\n", add->pathname, err);
1facf9fc 27314+ err = -EINVAL;
27315+
4f0767ce 27316+out:
1facf9fc 27317+ return err;
27318+}
27319+
1facf9fc 27320+static int au_opt_wbr_create(struct super_block *sb,
27321+ struct au_opt_wbr_create *create)
27322+{
27323+ int err;
27324+ struct au_sbinfo *sbinfo;
27325+
dece6358
AM
27326+ SiMustWriteLock(sb);
27327+
1facf9fc 27328+ err = 1; /* handled */
27329+ sbinfo = au_sbi(sb);
27330+ if (sbinfo->si_wbr_create_ops->fin) {
27331+ err = sbinfo->si_wbr_create_ops->fin(sb);
27332+ if (!err)
27333+ err = 1;
27334+ }
27335+
27336+ sbinfo->si_wbr_create = create->wbr_create;
27337+ sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
27338+ switch (create->wbr_create) {
27339+ case AuWbrCreate_MFSRRV:
27340+ case AuWbrCreate_MFSRR:
f2c43d5f
AM
27341+ case AuWbrCreate_TDMFS:
27342+ case AuWbrCreate_TDMFSV:
392086de
AM
27343+ case AuWbrCreate_PMFSRR:
27344+ case AuWbrCreate_PMFSRRV:
1facf9fc 27345+ sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
42a736d3 27346+ fallthrough;
1facf9fc 27347+ case AuWbrCreate_MFS:
27348+ case AuWbrCreate_MFSV:
27349+ case AuWbrCreate_PMFS:
27350+ case AuWbrCreate_PMFSV:
e49829fe
JR
27351+ sbinfo->si_wbr_mfs.mfs_expire
27352+ = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
1facf9fc 27353+ break;
27354+ }
27355+
27356+ if (sbinfo->si_wbr_create_ops->init)
27357+ sbinfo->si_wbr_create_ops->init(sb); /* ignore */
27358+
27359+ return err;
27360+}
27361+
27362+/*
27363+ * returns,
27364+ * plus: processed without an error
27365+ * zero: unprocessed
27366+ */
27367+static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
27368+ struct au_opts *opts)
27369+{
27370+ int err;
27371+ struct au_sbinfo *sbinfo;
27372+
dece6358
AM
27373+ SiMustWriteLock(sb);
27374+
1facf9fc 27375+ err = 1; /* handled */
27376+ sbinfo = au_sbi(sb);
27377+ switch (opt->type) {
27378+ case Opt_udba:
27379+ sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27380+ sbinfo->si_mntflags |= opt->udba;
27381+ opts->given_udba |= opt->udba;
27382+ break;
27383+
27384+ case Opt_plink:
f4d37d76
AM
27385+ if (opt->tf)
27386+ au_opt_set(sbinfo->si_mntflags, PLINK);
27387+ else {
27388+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
27389+ au_plink_put(sb, /*verbose*/1);
27390+ au_opt_clr(sbinfo->si_mntflags, PLINK);
27391+ }
1facf9fc 27392+ break;
27393+ case Opt_list_plink:
27394+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
27395+ au_plink_list(sb);
27396+ break;
27397+
4a4d8108 27398+ case Opt_dio:
f4d37d76
AM
27399+ if (opt->tf) {
27400+ au_opt_set(sbinfo->si_mntflags, DIO);
27401+ au_fset_opts(opts->flags, REFRESH_DYAOP);
27402+ } else {
27403+ au_opt_clr(sbinfo->si_mntflags, DIO);
27404+ au_fset_opts(opts->flags, REFRESH_DYAOP);
27405+ }
4a4d8108
AM
27406+ break;
27407+
076b876e
AM
27408+ case Opt_fhsm_sec:
27409+ au_fhsm_set(sbinfo, opt->fhsm_second);
27410+ break;
27411+
1facf9fc 27412+ case Opt_diropq_a:
27413+ au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27414+ break;
27415+ case Opt_diropq_w:
27416+ au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27417+ break;
27418+
27419+ case Opt_warn_perm:
f4d37d76
AM
27420+ if (opt->tf)
27421+ au_opt_set(sbinfo->si_mntflags, WARN_PERM);
27422+ else
27423+ au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
1facf9fc 27424+ break;
27425+
1facf9fc 27426+ case Opt_verbose:
f4d37d76
AM
27427+ if (opt->tf)
27428+ au_opt_set(sbinfo->si_mntflags, VERBOSE);
27429+ else
27430+ au_opt_clr(sbinfo->si_mntflags, VERBOSE);
1facf9fc 27431+ break;
27432+
27433+ case Opt_sum:
f4d37d76
AM
27434+ if (opt->tf)
27435+ au_opt_set(sbinfo->si_mntflags, SUM);
27436+ else {
27437+ au_opt_clr(sbinfo->si_mntflags, SUM);
27438+ au_opt_clr(sbinfo->si_mntflags, SUM_W);
27439+ }
1facf9fc 27440+ break;
27441+ case Opt_wsum:
27442+ au_opt_clr(sbinfo->si_mntflags, SUM);
27443+ au_opt_set(sbinfo->si_mntflags, SUM_W);
eca34b5c 27444+ break;
1facf9fc 27445+
27446+ case Opt_wbr_create:
27447+ err = au_opt_wbr_create(sb, &opt->wbr_create);
27448+ break;
27449+ case Opt_wbr_copyup:
27450+ sbinfo->si_wbr_copyup = opt->wbr_copyup;
27451+ sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
27452+ break;
27453+
27454+ case Opt_dirwh:
27455+ sbinfo->si_dirwh = opt->dirwh;
27456+ break;
27457+
27458+ case Opt_rdcache:
e49829fe
JR
27459+ sbinfo->si_rdcache
27460+ = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
1facf9fc 27461+ break;
27462+ case Opt_rdblk:
27463+ sbinfo->si_rdblk = opt->rdblk;
27464+ break;
27465+ case Opt_rdhash:
27466+ sbinfo->si_rdhash = opt->rdhash;
27467+ break;
dece6358
AM
27468+
27469+ case Opt_shwh:
f4d37d76
AM
27470+ if (opt->tf)
27471+ au_opt_set(sbinfo->si_mntflags, SHWH);
27472+ else
27473+ au_opt_clr(sbinfo->si_mntflags, SHWH);
dece6358 27474+ break;
1facf9fc 27475+
076b876e 27476+ case Opt_dirperm1:
f4d37d76
AM
27477+ if (opt->tf)
27478+ au_opt_set(sbinfo->si_mntflags, DIRPERM1);
27479+ else
27480+ au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
076b876e
AM
27481+ break;
27482+
1facf9fc 27483+ case Opt_trunc_xino:
f4d37d76
AM
27484+ if (opt->tf)
27485+ au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
27486+ else
27487+ au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
1facf9fc 27488+ break;
27489+
27490+ case Opt_trunc_xino_path:
27491+ case Opt_itrunc_xino:
acd2b654
AM
27492+ err = au_xino_trunc(sb, opt->xino_itrunc.bindex,
27493+ /*idx_begin*/0);
1facf9fc 27494+ if (!err)
27495+ err = 1;
27496+ break;
27497+
27498+ case Opt_trunc_xib:
f4d37d76
AM
27499+ if (opt->tf)
27500+ au_fset_opts(opts->flags, TRUNC_XIB);
27501+ else
27502+ au_fclr_opts(opts->flags, TRUNC_XIB);
1facf9fc 27503+ break;
27504+
8b6a4947
AM
27505+ case Opt_dirren:
27506+ err = 1;
f4d37d76
AM
27507+ if (opt->tf) {
27508+ if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27509+ err = au_dr_opt_set(sb);
27510+ if (!err)
27511+ err = 1;
27512+ }
27513+ if (err == 1)
27514+ au_opt_set(sbinfo->si_mntflags, DIRREN);
27515+ } else {
27516+ if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27517+ err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
27518+ DR_FLUSHED));
27519+ if (!err)
27520+ err = 1;
27521+ }
27522+ if (err == 1)
27523+ au_opt_clr(sbinfo->si_mntflags, DIRREN);
8b6a4947 27524+ }
8b6a4947
AM
27525+ break;
27526+
c1595e42 27527+ case Opt_acl:
f4d37d76
AM
27528+ if (opt->tf)
27529+ sb->s_flags |= SB_POSIXACL;
27530+ else
27531+ sb->s_flags &= ~SB_POSIXACL;
c1595e42
JR
27532+ break;
27533+
1facf9fc 27534+ default:
27535+ err = 0;
27536+ break;
27537+ }
27538+
27539+ return err;
27540+}
27541+
27542+/*
27543+ * returns tri-state.
27544+ * plus: processed without an error
27545+ * zero: unprocessed
27546+ * minus: error
27547+ */
27548+static int au_opt_br(struct super_block *sb, struct au_opt *opt,
27549+ struct au_opts *opts)
27550+{
27551+ int err, do_refresh;
27552+
27553+ err = 0;
27554+ switch (opt->type) {
27555+ case Opt_append:
5afbbe0d 27556+ opt->add.bindex = au_sbbot(sb) + 1;
1facf9fc 27557+ if (opt->add.bindex < 0)
27558+ opt->add.bindex = 0;
27559+ goto add;
eca34b5c 27560+ /* Always goto add, not fallthrough */
1facf9fc 27561+ case Opt_prepend:
27562+ opt->add.bindex = 0;
42a736d3 27563+ fallthrough;
f6b6e03d 27564+ add: /* indented label */
1facf9fc 27565+ case Opt_add:
27566+ err = au_br_add(sb, &opt->add,
27567+ au_ftest_opts(opts->flags, REMOUNT));
27568+ if (!err) {
27569+ err = 1;
027c5e7a 27570+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 27571+ }
27572+ break;
27573+
27574+ case Opt_del:
27575+ case Opt_idel:
27576+ err = au_br_del(sb, &opt->del,
27577+ au_ftest_opts(opts->flags, REMOUNT));
27578+ if (!err) {
27579+ err = 1;
27580+ au_fset_opts(opts->flags, TRUNC_XIB);
027c5e7a 27581+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 27582+ }
27583+ break;
27584+
27585+ case Opt_mod:
27586+ case Opt_imod:
27587+ err = au_br_mod(sb, &opt->mod,
27588+ au_ftest_opts(opts->flags, REMOUNT),
27589+ &do_refresh);
27590+ if (!err) {
27591+ err = 1;
027c5e7a
AM
27592+ if (do_refresh)
27593+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 27594+ }
27595+ break;
27596+ }
1facf9fc 27597+ return err;
27598+}
27599+
27600+static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
27601+ struct au_opt_xino **opt_xino,
27602+ struct au_opts *opts)
27603+{
27604+ int err;
1facf9fc 27605+
27606+ err = 0;
27607+ switch (opt->type) {
27608+ case Opt_xino:
062440b3
AM
27609+ err = au_xino_set(sb, &opt->xino,
27610+ !!au_ftest_opts(opts->flags, REMOUNT));
f4d37d76
AM
27611+ if (!err)
27612+ *opt_xino = &opt->xino;
1facf9fc 27613+ break;
1facf9fc 27614+ case Opt_noxino:
27615+ au_xino_clr(sb);
1facf9fc 27616+ *opt_xino = (void *)-1;
27617+ break;
27618+ }
27619+
27620+ return err;
27621+}
27622+
27623+int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27624+ unsigned int pending)
27625+{
076b876e 27626+ int err, fhsm;
5afbbe0d 27627+ aufs_bindex_t bindex, bbot;
79b8bda9 27628+ unsigned char do_plink, skip, do_free, can_no_dreval;
1facf9fc 27629+ struct au_branch *br;
27630+ struct au_wbr *wbr;
79b8bda9 27631+ struct dentry *root, *dentry;
1facf9fc 27632+ struct inode *dir, *h_dir;
27633+ struct au_sbinfo *sbinfo;
27634+ struct au_hinode *hdir;
27635+
dece6358
AM
27636+ SiMustAnyLock(sb);
27637+
1facf9fc 27638+ sbinfo = au_sbi(sb);
27639+ AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
27640+
2121bcd9 27641+ if (!(sb_flags & SB_RDONLY)) {
dece6358 27642+ if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
0c3ec466 27643+ pr_warn("first branch should be rw\n");
dece6358 27644+ if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
febd17d6 27645+ pr_warn_once("shwh should be used with ro\n");
dece6358 27646+ }
1facf9fc 27647+
4a4d8108 27648+ if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
1facf9fc 27649+ && !au_opt_test(sbinfo->si_mntflags, XINO))
febd17d6 27650+ pr_warn_once("udba=*notify requires xino\n");
1facf9fc 27651+
076b876e 27652+ if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
febd17d6
JR
27653+ pr_warn_once("dirperm1 breaks the protection"
27654+ " by the permission bits on the lower branch\n");
076b876e 27655+
1facf9fc 27656+ err = 0;
076b876e 27657+ fhsm = 0;
1facf9fc 27658+ root = sb->s_root;
5527c038 27659+ dir = d_inode(root);
1facf9fc 27660+ do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
79b8bda9
AM
27661+ can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
27662+ UDBA_NONE);
5afbbe0d
AM
27663+ bbot = au_sbbot(sb);
27664+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
1facf9fc 27665+ skip = 0;
27666+ h_dir = au_h_iptr(dir, bindex);
27667+ br = au_sbr(sb, bindex);
1facf9fc 27668+
c1595e42
JR
27669+ if ((br->br_perm & AuBrAttr_ICEX)
27670+ && !h_dir->i_op->listxattr)
27671+ br->br_perm &= ~AuBrAttr_ICEX;
43982f53 27672+#if 0 /* untested */
c1595e42 27673+ if ((br->br_perm & AuBrAttr_ICEX_SEC)
2121bcd9 27674+ && (au_br_sb(br)->s_flags & SB_NOSEC))
c1595e42
JR
27675+ br->br_perm &= ~AuBrAttr_ICEX_SEC;
27676+#endif
27677+
27678+ do_free = 0;
1facf9fc 27679+ wbr = br->br_wbr;
27680+ if (wbr)
27681+ wbr_wh_read_lock(wbr);
27682+
1e00d052 27683+ if (!au_br_writable(br->br_perm)) {
1facf9fc 27684+ do_free = !!wbr;
27685+ skip = (!wbr
27686+ || (!wbr->wbr_whbase
27687+ && !wbr->wbr_plink
27688+ && !wbr->wbr_orph));
1e00d052 27689+ } else if (!au_br_wh_linkable(br->br_perm)) {
1facf9fc 27690+ /* skip = (!br->br_whbase && !br->br_orph); */
27691+ skip = (!wbr || !wbr->wbr_whbase);
27692+ if (skip && wbr) {
27693+ if (do_plink)
27694+ skip = !!wbr->wbr_plink;
27695+ else
27696+ skip = !wbr->wbr_plink;
27697+ }
1e00d052 27698+ } else {
1facf9fc 27699+ /* skip = (br->br_whbase && br->br_ohph); */
27700+ skip = (wbr && wbr->wbr_whbase);
27701+ if (skip) {
27702+ if (do_plink)
27703+ skip = !!wbr->wbr_plink;
27704+ else
27705+ skip = !wbr->wbr_plink;
27706+ }
1facf9fc 27707+ }
27708+ if (wbr)
27709+ wbr_wh_read_unlock(wbr);
27710+
79b8bda9
AM
27711+ if (can_no_dreval) {
27712+ dentry = br->br_path.dentry;
27713+ spin_lock(&dentry->d_lock);
27714+ if (dentry->d_flags &
27715+ (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
27716+ can_no_dreval = 0;
27717+ spin_unlock(&dentry->d_lock);
27718+ }
27719+
076b876e
AM
27720+ if (au_br_fhsm(br->br_perm)) {
27721+ fhsm++;
27722+ AuDebugOn(!br->br_fhsm);
27723+ }
27724+
1facf9fc 27725+ if (skip)
27726+ continue;
27727+
27728+ hdir = au_hi(dir, bindex);
5afbbe0d 27729+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 27730+ if (wbr)
27731+ wbr_wh_write_lock(wbr);
86dc4139 27732+ err = au_wh_init(br, sb);
1facf9fc 27733+ if (wbr)
27734+ wbr_wh_write_unlock(wbr);
5afbbe0d 27735+ au_hn_inode_unlock(hdir);
1facf9fc 27736+
27737+ if (!err && do_free) {
9f237c51 27738+ au_kfree_rcu(wbr);
1facf9fc 27739+ br->br_wbr = NULL;
27740+ }
27741+ }
27742+
79b8bda9
AM
27743+ if (can_no_dreval)
27744+ au_fset_si(sbinfo, NO_DREVAL);
27745+ else
27746+ au_fclr_si(sbinfo, NO_DREVAL);
27747+
c1595e42 27748+ if (fhsm >= 2) {
076b876e 27749+ au_fset_si(sbinfo, FHSM);
5afbbe0d 27750+ for (bindex = bbot; bindex >= 0; bindex--) {
c1595e42
JR
27751+ br = au_sbr(sb, bindex);
27752+ if (au_br_fhsm(br->br_perm)) {
27753+ au_fhsm_set_bottom(sb, bindex);
27754+ break;
27755+ }
27756+ }
27757+ } else {
076b876e 27758+ au_fclr_si(sbinfo, FHSM);
c1595e42
JR
27759+ au_fhsm_set_bottom(sb, -1);
27760+ }
076b876e 27761+
1facf9fc 27762+ return err;
27763+}
27764+
27765+int au_opts_mount(struct super_block *sb, struct au_opts *opts)
27766+{
27767+ int err;
27768+ unsigned int tmp;
5afbbe0d 27769+ aufs_bindex_t bindex, bbot;
1facf9fc 27770+ struct au_opt *opt;
27771+ struct au_opt_xino *opt_xino, xino;
27772+ struct au_sbinfo *sbinfo;
027c5e7a 27773+ struct au_branch *br;
076b876e 27774+ struct inode *dir;
1facf9fc 27775+
dece6358
AM
27776+ SiMustWriteLock(sb);
27777+
1facf9fc 27778+ err = 0;
27779+ opt_xino = NULL;
27780+ opt = opts->opt;
27781+ while (err >= 0 && opt->type != Opt_tail)
27782+ err = au_opt_simple(sb, opt++, opts);
27783+ if (err > 0)
27784+ err = 0;
27785+ else if (unlikely(err < 0))
27786+ goto out;
27787+
27788+ /* disable xino and udba temporary */
27789+ sbinfo = au_sbi(sb);
27790+ tmp = sbinfo->si_mntflags;
27791+ au_opt_clr(sbinfo->si_mntflags, XINO);
27792+ au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
27793+
27794+ opt = opts->opt;
27795+ while (err >= 0 && opt->type != Opt_tail)
27796+ err = au_opt_br(sb, opt++, opts);
27797+ if (err > 0)
27798+ err = 0;
27799+ else if (unlikely(err < 0))
27800+ goto out;
27801+
5afbbe0d
AM
27802+ bbot = au_sbbot(sb);
27803+ if (unlikely(bbot < 0)) {
1facf9fc 27804+ err = -EINVAL;
4a4d8108 27805+ pr_err("no branches\n");
1facf9fc 27806+ goto out;
27807+ }
27808+
27809+ if (au_opt_test(tmp, XINO))
27810+ au_opt_set(sbinfo->si_mntflags, XINO);
27811+ opt = opts->opt;
27812+ while (!err && opt->type != Opt_tail)
27813+ err = au_opt_xino(sb, opt++, &opt_xino, opts);
27814+ if (unlikely(err))
27815+ goto out;
27816+
27817+ err = au_opts_verify(sb, sb->s_flags, tmp);
27818+ if (unlikely(err))
27819+ goto out;
27820+
27821+ /* restore xino */
27822+ if (au_opt_test(tmp, XINO) && !opt_xino) {
27823+ xino.file = au_xino_def(sb);
27824+ err = PTR_ERR(xino.file);
27825+ if (IS_ERR(xino.file))
27826+ goto out;
27827+
27828+ err = au_xino_set(sb, &xino, /*remount*/0);
27829+ fput(xino.file);
27830+ if (unlikely(err))
27831+ goto out;
27832+ }
27833+
27834+ /* restore udba */
027c5e7a 27835+ tmp &= AuOptMask_UDBA;
1facf9fc 27836+ sbinfo->si_mntflags &= ~AuOptMask_UDBA;
027c5e7a 27837+ sbinfo->si_mntflags |= tmp;
5afbbe0d
AM
27838+ bbot = au_sbbot(sb);
27839+ for (bindex = 0; bindex <= bbot; bindex++) {
027c5e7a
AM
27840+ br = au_sbr(sb, bindex);
27841+ err = au_hnotify_reset_br(tmp, br, br->br_perm);
27842+ if (unlikely(err))
27843+ AuIOErr("hnotify failed on br %d, %d, ignored\n",
27844+ bindex, err);
27845+ /* go on even if err */
27846+ }
4a4d8108 27847+ if (au_opt_test(tmp, UDBA_HNOTIFY)) {
5527c038 27848+ dir = d_inode(sb->s_root);
4a4d8108 27849+ au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
1facf9fc 27850+ }
27851+
4f0767ce 27852+out:
1facf9fc 27853+ return err;
27854+}
27855+
27856+int au_opts_remount(struct super_block *sb, struct au_opts *opts)
27857+{
27858+ int err, rerr;
79b8bda9 27859+ unsigned char no_dreval;
1facf9fc 27860+ struct inode *dir;
27861+ struct au_opt_xino *opt_xino;
27862+ struct au_opt *opt;
27863+ struct au_sbinfo *sbinfo;
27864+
dece6358
AM
27865+ SiMustWriteLock(sb);
27866+
8b6a4947
AM
27867+ err = au_dr_opt_flush(sb);
27868+ if (unlikely(err))
27869+ goto out;
27870+ au_fset_opts(opts->flags, DR_FLUSHED);
27871+
5527c038 27872+ dir = d_inode(sb->s_root);
1facf9fc 27873+ sbinfo = au_sbi(sb);
1facf9fc 27874+ opt_xino = NULL;
27875+ opt = opts->opt;
27876+ while (err >= 0 && opt->type != Opt_tail) {
27877+ err = au_opt_simple(sb, opt, opts);
27878+ if (!err)
27879+ err = au_opt_br(sb, opt, opts);
27880+ if (!err)
27881+ err = au_opt_xino(sb, opt, &opt_xino, opts);
27882+ opt++;
27883+ }
27884+ if (err > 0)
27885+ err = 0;
27886+ AuTraceErr(err);
27887+ /* go on even err */
27888+
79b8bda9 27889+ no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
1facf9fc 27890+ rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
27891+ if (unlikely(rerr && !err))
27892+ err = rerr;
27893+
79b8bda9 27894+ if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
b95c5147 27895+ au_fset_opts(opts->flags, REFRESH_IDOP);
79b8bda9 27896+
1facf9fc 27897+ if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
27898+ rerr = au_xib_trunc(sb);
27899+ if (unlikely(rerr && !err))
27900+ err = rerr;
27901+ }
27902+
27903+ /* will be handled by the caller */
027c5e7a 27904+ if (!au_ftest_opts(opts->flags, REFRESH)
79b8bda9
AM
27905+ && (opts->given_udba
27906+ || au_opt_test(sbinfo->si_mntflags, XINO)
b95c5147 27907+ || au_ftest_opts(opts->flags, REFRESH_IDOP)
79b8bda9 27908+ ))
027c5e7a 27909+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 27910+
27911+ AuDbg("status 0x%x\n", opts->flags);
8b6a4947
AM
27912+
27913+out:
1facf9fc 27914+ return err;
27915+}
27916+
27917+/* ---------------------------------------------------------------------- */
27918+
27919+unsigned int au_opt_udba(struct super_block *sb)
27920+{
27921+ return au_mntflags(sb) & AuOptMask_UDBA;
27922+}
7f207e10 27923diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
eca34b5c 27924--- /usr/share/empty/fs/aufs/opts.h 1970-01-01 01:00:00.000000000 +0100
594d0238 27925+++ linux/fs/aufs/opts.h 2022-11-05 23:02:18.969222617 +0100
f4d37d76 27926@@ -0,0 +1,263 @@
062440b3 27927+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 27928+/*
0dcfbb52 27929+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 27930+ *
0dcfbb52 27931+ * This program is free software; you can redistribute it and/or modify
1facf9fc 27932+ * it under the terms of the GNU General Public License as published by
27933+ * the Free Software Foundation; either version 2 of the License, or
27934+ * (at your option) any later version.
dece6358
AM
27935+ *
27936+ * This program is distributed in the hope that it will be useful,
27937+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
27938+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27939+ * GNU General Public License for more details.
27940+ *
27941+ * You should have received a copy of the GNU General Public License
523b37e3 27942+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 27943+ */
27944+
27945+/*
27946+ * mount options/flags
27947+ */
27948+
27949+#ifndef __AUFS_OPTS_H__
27950+#define __AUFS_OPTS_H__
27951+
27952+#ifdef __KERNEL__
27953+
f4d37d76
AM
27954+#include <linux/fs_parser.h>
27955+#include <linux/namei.h>
dece6358 27956+#include <linux/path.h>
1facf9fc 27957+
f4d37d76
AM
27958+enum {
27959+ Opt_br,
27960+ Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
27961+ Opt_idel, Opt_imod,
27962+ Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
27963+ Opt_xino, Opt_noxino,
27964+ Opt_trunc_xino, Opt_trunc_xino_v,
27965+ Opt_trunc_xino_path, Opt_itrunc_xino,
27966+ Opt_trunc_xib,
27967+ Opt_shwh,
27968+ Opt_plink, Opt_list_plink,
27969+ Opt_udba,
27970+ Opt_dio,
27971+ Opt_diropq, Opt_diropq_a, Opt_diropq_w,
27972+ Opt_warn_perm,
27973+ Opt_wbr_copyup, Opt_wbr_create,
27974+ Opt_fhsm_sec,
27975+ Opt_verbose, Opt_noverbose,
27976+ Opt_sum, Opt_wsum,
27977+ Opt_dirperm1,
27978+ Opt_dirren,
27979+ Opt_acl,
27980+ Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
27981+};
dece6358 27982+
1facf9fc 27983+/* ---------------------------------------------------------------------- */
27984+
27985+/* mount flags */
27986+#define AuOpt_XINO 1 /* external inode number bitmap
27987+ and translation table */
27988+#define AuOpt_TRUNC_XINO (1 << 1) /* truncate xino files */
27989+#define AuOpt_UDBA_NONE (1 << 2) /* users direct branch access */
27990+#define AuOpt_UDBA_REVAL (1 << 3)
4a4d8108 27991+#define AuOpt_UDBA_HNOTIFY (1 << 4)
dece6358
AM
27992+#define AuOpt_SHWH (1 << 5) /* show whiteout */
27993+#define AuOpt_PLINK (1 << 6) /* pseudo-link */
076b876e
AM
27994+#define AuOpt_DIRPERM1 (1 << 7) /* ignore the lower dir's perm
27995+ bits */
dece6358
AM
27996+#define AuOpt_ALWAYS_DIROPQ (1 << 9) /* policy to creating diropq */
27997+#define AuOpt_SUM (1 << 10) /* summation for statfs(2) */
27998+#define AuOpt_SUM_W (1 << 11) /* unimplemented */
27999+#define AuOpt_WARN_PERM (1 << 12) /* warn when add-branch */
eca801bf 28000+#define AuOpt_VERBOSE (1 << 13) /* print the cause of error */
4a4d8108 28001+#define AuOpt_DIO (1 << 14) /* direct io */
8b6a4947 28002+#define AuOpt_DIRREN (1 << 15) /* directory rename */
1facf9fc 28003+
4a4d8108
AM
28004+#ifndef CONFIG_AUFS_HNOTIFY
28005+#undef AuOpt_UDBA_HNOTIFY
28006+#define AuOpt_UDBA_HNOTIFY 0
1facf9fc 28007+#endif
8b6a4947
AM
28008+#ifndef CONFIG_AUFS_DIRREN
28009+#undef AuOpt_DIRREN
28010+#define AuOpt_DIRREN 0
28011+#endif
dece6358
AM
28012+#ifndef CONFIG_AUFS_SHWH
28013+#undef AuOpt_SHWH
28014+#define AuOpt_SHWH 0
28015+#endif
1facf9fc 28016+
28017+#define AuOpt_Def (AuOpt_XINO \
28018+ | AuOpt_UDBA_REVAL \
28019+ | AuOpt_PLINK \
28020+ /* | AuOpt_DIRPERM1 */ \
28021+ | AuOpt_WARN_PERM)
28022+#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
28023+ | AuOpt_UDBA_REVAL \
4a4d8108 28024+ | AuOpt_UDBA_HNOTIFY)
1facf9fc 28025+
f4d37d76
AM
28026+#define AuOpt_LkupDirFlags (LOOKUP_FOLLOW | LOOKUP_DIRECTORY)
28027+
1facf9fc 28028+#define au_opt_test(flags, name) (flags & AuOpt_##name)
28029+#define au_opt_set(flags, name) do { \
28030+ BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
28031+ ((flags) |= AuOpt_##name); \
28032+} while (0)
28033+#define au_opt_set_udba(flags, name) do { \
28034+ (flags) &= ~AuOptMask_UDBA; \
28035+ ((flags) |= AuOpt_##name); \
28036+} while (0)
7f207e10
AM
28037+#define au_opt_clr(flags, name) do { \
28038+ ((flags) &= ~AuOpt_##name); \
28039+} while (0)
1facf9fc 28040+
e49829fe
JR
28041+static inline unsigned int au_opts_plink(unsigned int mntflags)
28042+{
28043+#ifdef CONFIG_PROC_FS
28044+ return mntflags;
28045+#else
28046+ return mntflags & ~AuOpt_PLINK;
28047+#endif
28048+}
28049+
1facf9fc 28050+/* ---------------------------------------------------------------------- */
28051+
28052+/* policies to select one among multiple writable branches */
28053+enum {
28054+ AuWbrCreate_TDP, /* top down parent */
28055+ AuWbrCreate_RR, /* round robin */
28056+ AuWbrCreate_MFS, /* most free space */
28057+ AuWbrCreate_MFSV, /* mfs with seconds */
28058+ AuWbrCreate_MFSRR, /* mfs then rr */
28059+ AuWbrCreate_MFSRRV, /* mfs then rr with seconds */
f2c43d5f
AM
28060+ AuWbrCreate_TDMFS, /* top down regardless parent and mfs */
28061+ AuWbrCreate_TDMFSV, /* top down regardless parent and mfs */
1facf9fc 28062+ AuWbrCreate_PMFS, /* parent and mfs */
28063+ AuWbrCreate_PMFSV, /* parent and mfs with seconds */
392086de
AM
28064+ AuWbrCreate_PMFSRR, /* parent, mfs and round-robin */
28065+ AuWbrCreate_PMFSRRV, /* plus seconds */
1facf9fc 28066+
28067+ AuWbrCreate_Def = AuWbrCreate_TDP
28068+};
28069+
28070+enum {
28071+ AuWbrCopyup_TDP, /* top down parent */
28072+ AuWbrCopyup_BUP, /* bottom up parent */
28073+ AuWbrCopyup_BU, /* bottom up */
28074+
28075+ AuWbrCopyup_Def = AuWbrCopyup_TDP
28076+};
28077+
28078+/* ---------------------------------------------------------------------- */
28079+
f4d37d76
AM
28080+struct file;
28081+
1facf9fc 28082+struct au_opt_add {
28083+ aufs_bindex_t bindex;
28084+ char *pathname;
28085+ int perm;
28086+ struct path path;
28087+};
28088+
28089+struct au_opt_del {
28090+ char *pathname;
28091+ struct path h_path;
28092+};
28093+
28094+struct au_opt_mod {
28095+ char *path;
28096+ int perm;
28097+ struct dentry *h_root;
28098+};
28099+
28100+struct au_opt_xino {
28101+ char *path;
28102+ struct file *file;
28103+};
28104+
28105+struct au_opt_xino_itrunc {
28106+ aufs_bindex_t bindex;
28107+};
28108+
28109+struct au_opt_wbr_create {
28110+ int wbr_create;
28111+ int mfs_second;
28112+ unsigned long long mfsrr_watermark;
28113+};
28114+
28115+struct au_opt {
28116+ int type;
28117+ union {
28118+ struct au_opt_xino xino;
28119+ struct au_opt_xino_itrunc xino_itrunc;
28120+ struct au_opt_add add;
28121+ struct au_opt_del del;
28122+ struct au_opt_mod mod;
28123+ int dirwh;
28124+ int rdcache;
28125+ unsigned int rdblk;
28126+ unsigned int rdhash;
28127+ int udba;
28128+ struct au_opt_wbr_create wbr_create;
28129+ int wbr_copyup;
076b876e 28130+ unsigned int fhsm_second;
f4d37d76 28131+ bool tf; /* generic flag, true or false */
1facf9fc 28132+ };
28133+};
28134+
28135+/* opts flags */
28136+#define AuOpts_REMOUNT 1
027c5e7a
AM
28137+#define AuOpts_REFRESH (1 << 1)
28138+#define AuOpts_TRUNC_XIB (1 << 2)
28139+#define AuOpts_REFRESH_DYAOP (1 << 3)
b95c5147 28140+#define AuOpts_REFRESH_IDOP (1 << 4)
8b6a4947 28141+#define AuOpts_DR_FLUSHED (1 << 5)
1facf9fc 28142+#define au_ftest_opts(flags, name) ((flags) & AuOpts_##name)
7f207e10
AM
28143+#define au_fset_opts(flags, name) \
28144+ do { (flags) |= AuOpts_##name; } while (0)
28145+#define au_fclr_opts(flags, name) \
28146+ do { (flags) &= ~AuOpts_##name; } while (0)
1facf9fc 28147+
8b6a4947
AM
28148+#ifndef CONFIG_AUFS_DIRREN
28149+#undef AuOpts_DR_FLUSHED
28150+#define AuOpts_DR_FLUSHED 0
28151+#endif
28152+
1facf9fc 28153+struct au_opts {
28154+ struct au_opt *opt;
28155+ int max_opt;
28156+
28157+ unsigned int given_udba;
28158+ unsigned int flags;
28159+ unsigned long sb_flags;
28160+};
28161+
28162+/* ---------------------------------------------------------------------- */
28163+
7e9cd9fe 28164+/* opts.c */
f4d37d76 28165+int au_br_perm_val(char *perm);
076b876e 28166+void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
f4d37d76 28167+int au_udba_val(char *str);
1facf9fc 28168+const char *au_optstr_udba(int udba);
f4d37d76 28169+int au_wbr_create_val(char *str, struct au_opt_wbr_create *create);
1facf9fc 28170+const char *au_optstr_wbr_create(int wbr_create);
f4d37d76
AM
28171+int au_wbr_copyup_val(char *str);
28172+const char *au_optstr_wbr_copyup(int wbr_copyup);
1facf9fc 28173+
f4d37d76
AM
28174+int au_opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
28175+ aufs_bindex_t bindex);
3c1bdaff 28176+struct super_block;
1facf9fc 28177+int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
28178+ unsigned int pending);
28179+int au_opts_mount(struct super_block *sb, struct au_opts *opts);
28180+int au_opts_remount(struct super_block *sb, struct au_opts *opts);
28181+
28182+unsigned int au_opt_udba(struct super_block *sb);
28183+
f4d37d76
AM
28184+/* fsctx.c */
28185+int aufs_fsctx_init(struct fs_context *fc);
28186+extern const struct fs_parameter_spec aufs_fsctx_paramspec[];
28187+
1facf9fc 28188+#endif /* __KERNEL__ */
28189+#endif /* __AUFS_OPTS_H__ */
7f207e10 28190diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
eca34b5c 28191--- /usr/share/empty/fs/aufs/plink.c 1970-01-01 01:00:00.000000000 +0100
594d0238 28192+++ linux/fs/aufs/plink.c 2022-11-05 23:02:18.969222617 +0100
062440b3 28193@@ -0,0 +1,516 @@
cd7a4cd9 28194+// SPDX-License-Identifier: GPL-2.0
1facf9fc 28195+/*
0dcfbb52 28196+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 28197+ *
0dcfbb52 28198+ * This program is free software; you can redistribute it and/or modify
1facf9fc 28199+ * it under the terms of the GNU General Public License as published by
28200+ * the Free Software Foundation; either version 2 of the License, or
28201+ * (at your option) any later version.
dece6358
AM
28202+ *
28203+ * This program is distributed in the hope that it will be useful,
28204+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28205+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28206+ * GNU General Public License for more details.
28207+ *
28208+ * You should have received a copy of the GNU General Public License
523b37e3 28209+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 28210+ */
28211+
28212+/*
28213+ * pseudo-link
28214+ */
28215+
28216+#include "aufs.h"
28217+
28218+/*
e49829fe 28219+ * the pseudo-link maintenance mode.
1facf9fc 28220+ * during a user process maintains the pseudo-links,
28221+ * prohibit adding a new plink and branch manipulation.
e49829fe
JR
28222+ *
28223+ * Flags
28224+ * NOPLM:
28225+ * For entry functions which will handle plink, and i_mutex is already held
28226+ * in VFS.
28227+ * They cannot wait and should return an error at once.
28228+ * Callers has to check the error.
28229+ * NOPLMW:
28230+ * For entry functions which will handle plink, but i_mutex is not held
28231+ * in VFS.
28232+ * They can wait the plink maintenance mode to finish.
28233+ *
28234+ * They behave like F_SETLK and F_SETLKW.
28235+ * If the caller never handle plink, then both flags are unnecessary.
1facf9fc 28236+ */
e49829fe
JR
28237+
28238+int au_plink_maint(struct super_block *sb, int flags)
1facf9fc 28239+{
e49829fe
JR
28240+ int err;
28241+ pid_t pid, ppid;
f0c0a007 28242+ struct task_struct *parent, *prev;
e49829fe 28243+ struct au_sbinfo *sbi;
dece6358
AM
28244+
28245+ SiMustAnyLock(sb);
28246+
e49829fe
JR
28247+ err = 0;
28248+ if (!au_opt_test(au_mntflags(sb), PLINK))
28249+ goto out;
28250+
28251+ sbi = au_sbi(sb);
28252+ pid = sbi->si_plink_maint_pid;
28253+ if (!pid || pid == current->pid)
28254+ goto out;
28255+
28256+ /* todo: it highly depends upon /sbin/mount.aufs */
f0c0a007
AM
28257+ prev = NULL;
28258+ parent = current;
28259+ ppid = 0;
e49829fe 28260+ rcu_read_lock();
f0c0a007
AM
28261+ while (1) {
28262+ parent = rcu_dereference(parent->real_parent);
28263+ if (parent == prev)
28264+ break;
28265+ ppid = task_pid_vnr(parent);
28266+ if (pid == ppid) {
28267+ rcu_read_unlock();
28268+ goto out;
28269+ }
28270+ prev = parent;
28271+ }
e49829fe 28272+ rcu_read_unlock();
e49829fe
JR
28273+
28274+ if (au_ftest_lock(flags, NOPLMW)) {
027c5e7a
AM
28275+ /* if there is no i_mutex lock in VFS, we don't need to wait */
28276+ /* AuDebugOn(!lockdep_depth(current)); */
e49829fe
JR
28277+ while (sbi->si_plink_maint_pid) {
28278+ si_read_unlock(sb);
28279+ /* gave up wake_up_bit() */
28280+ wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
28281+
28282+ if (au_ftest_lock(flags, FLUSH))
28283+ au_nwt_flush(&sbi->si_nowait);
28284+ si_noflush_read_lock(sb);
28285+ }
28286+ } else if (au_ftest_lock(flags, NOPLM)) {
28287+ AuDbg("ppid %d, pid %d\n", ppid, pid);
28288+ err = -EAGAIN;
28289+ }
28290+
28291+out:
28292+ return err;
4a4d8108
AM
28293+}
28294+
e49829fe 28295+void au_plink_maint_leave(struct au_sbinfo *sbinfo)
4a4d8108 28296+{
4a4d8108 28297+ spin_lock(&sbinfo->si_plink_maint_lock);
027c5e7a 28298+ sbinfo->si_plink_maint_pid = 0;
4a4d8108 28299+ spin_unlock(&sbinfo->si_plink_maint_lock);
027c5e7a 28300+ wake_up_all(&sbinfo->si_plink_wq);
4a4d8108
AM
28301+}
28302+
e49829fe 28303+int au_plink_maint_enter(struct super_block *sb)
4a4d8108
AM
28304+{
28305+ int err;
4a4d8108
AM
28306+ struct au_sbinfo *sbinfo;
28307+
28308+ err = 0;
4a4d8108
AM
28309+ sbinfo = au_sbi(sb);
28310+ /* make sure i am the only one in this fs */
e49829fe
JR
28311+ si_write_lock(sb, AuLock_FLUSH);
28312+ if (au_opt_test(au_mntflags(sb), PLINK)) {
28313+ spin_lock(&sbinfo->si_plink_maint_lock);
28314+ if (!sbinfo->si_plink_maint_pid)
28315+ sbinfo->si_plink_maint_pid = current->pid;
28316+ else
28317+ err = -EBUSY;
28318+ spin_unlock(&sbinfo->si_plink_maint_lock);
28319+ }
4a4d8108
AM
28320+ si_write_unlock(sb);
28321+
28322+ return err;
1facf9fc 28323+}
28324+
28325+/* ---------------------------------------------------------------------- */
28326+
1facf9fc 28327+#ifdef CONFIG_AUFS_DEBUG
28328+void au_plink_list(struct super_block *sb)
28329+{
86dc4139 28330+ int i;
1facf9fc 28331+ struct au_sbinfo *sbinfo;
8b6a4947
AM
28332+ struct hlist_bl_head *hbl;
28333+ struct hlist_bl_node *pos;
5afbbe0d 28334+ struct au_icntnr *icntnr;
1facf9fc 28335+
dece6358
AM
28336+ SiMustAnyLock(sb);
28337+
1facf9fc 28338+ sbinfo = au_sbi(sb);
28339+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 28340+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 28341+
86dc4139 28342+ for (i = 0; i < AuPlink_NHASH; i++) {
8b6a4947
AM
28343+ hbl = sbinfo->si_plink + i;
28344+ hlist_bl_lock(hbl);
28345+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
5afbbe0d 28346+ AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
8b6a4947 28347+ hlist_bl_unlock(hbl);
86dc4139 28348+ }
1facf9fc 28349+}
28350+#endif
28351+
28352+/* is the inode pseudo-linked? */
28353+int au_plink_test(struct inode *inode)
28354+{
86dc4139 28355+ int found, i;
1facf9fc 28356+ struct au_sbinfo *sbinfo;
8b6a4947
AM
28357+ struct hlist_bl_head *hbl;
28358+ struct hlist_bl_node *pos;
5afbbe0d 28359+ struct au_icntnr *icntnr;
1facf9fc 28360+
28361+ sbinfo = au_sbi(inode->i_sb);
dece6358 28362+ AuRwMustAnyLock(&sbinfo->si_rwsem);
1facf9fc 28363+ AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
e49829fe 28364+ AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
1facf9fc 28365+
28366+ found = 0;
86dc4139 28367+ i = au_plink_hash(inode->i_ino);
8b6a4947
AM
28368+ hbl = sbinfo->si_plink + i;
28369+ hlist_bl_lock(hbl);
28370+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
5afbbe0d 28371+ if (&icntnr->vfs_inode == inode) {
1facf9fc 28372+ found = 1;
28373+ break;
28374+ }
8b6a4947 28375+ hlist_bl_unlock(hbl);
1facf9fc 28376+ return found;
28377+}
28378+
28379+/* ---------------------------------------------------------------------- */
28380+
28381+/*
28382+ * generate a name for plink.
28383+ * the file will be stored under AUFS_WH_PLINKDIR.
28384+ */
28385+/* 20 is max digits length of ulong 64 */
28386+#define PLINK_NAME_LEN ((20 + 1) * 2)
28387+
28388+static int plink_name(char *name, int len, struct inode *inode,
28389+ aufs_bindex_t bindex)
28390+{
28391+ int rlen;
28392+ struct inode *h_inode;
28393+
28394+ h_inode = au_h_iptr(inode, bindex);
28395+ rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
28396+ return rlen;
28397+}
28398+
7f207e10
AM
28399+struct au_do_plink_lkup_args {
28400+ struct dentry **errp;
28401+ struct qstr *tgtname;
758e9dad 28402+ struct path *h_ppath;
7f207e10
AM
28403+};
28404+
28405+static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
758e9dad 28406+ struct path *h_ppath)
7f207e10
AM
28407+{
28408+ struct dentry *h_dentry;
febd17d6 28409+ struct inode *h_inode;
7f207e10 28410+
758e9dad 28411+ h_inode = d_inode(h_ppath->dentry);
be118d29 28412+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD2);
758e9dad 28413+ h_dentry = vfsub_lkup_one(tgtname, h_ppath);
3c1bdaff 28414+ inode_unlock_shared(h_inode);
758e9dad 28415+
7f207e10
AM
28416+ return h_dentry;
28417+}
28418+
28419+static void au_call_do_plink_lkup(void *args)
28420+{
28421+ struct au_do_plink_lkup_args *a = args;
758e9dad 28422+ *a->errp = au_do_plink_lkup(a->tgtname, a->h_ppath);
7f207e10
AM
28423+}
28424+
1facf9fc 28425+/* lookup the plink-ed @inode under the branch at @bindex */
28426+struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
28427+{
758e9dad 28428+ struct dentry *h_dentry;
1facf9fc 28429+ struct au_branch *br;
758e9dad 28430+ struct path h_ppath;
7f207e10 28431+ int wkq_err;
1facf9fc 28432+ char a[PLINK_NAME_LEN];
0c3ec466 28433+ struct qstr tgtname = QSTR_INIT(a, 0);
1facf9fc 28434+
e49829fe
JR
28435+ AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
28436+
1facf9fc 28437+ br = au_sbr(inode->i_sb, bindex);
758e9dad
AM
28438+ h_ppath.dentry = br->br_wbr->wbr_plink;
28439+ h_ppath.mnt = au_br_mnt(br);
1facf9fc 28440+ tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28441+
2dfbb274 28442+ if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
7f207e10
AM
28443+ struct au_do_plink_lkup_args args = {
28444+ .errp = &h_dentry,
28445+ .tgtname = &tgtname,
758e9dad 28446+ .h_ppath = &h_ppath
7f207e10
AM
28447+ };
28448+
28449+ wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
28450+ if (unlikely(wkq_err))
28451+ h_dentry = ERR_PTR(wkq_err);
28452+ } else
758e9dad 28453+ h_dentry = au_do_plink_lkup(&tgtname, &h_ppath);
7f207e10 28454+
1facf9fc 28455+ return h_dentry;
28456+}
28457+
28458+/* create a pseudo-link */
758e9dad
AM
28459+static int do_whplink(struct qstr *tgt, struct path *h_ppath,
28460+ struct dentry *h_dentry)
1facf9fc 28461+{
28462+ int err;
758e9dad 28463+ struct path h_path;
523b37e3 28464+ struct inode *h_dir, *delegated;
1facf9fc 28465+
758e9dad 28466+ h_dir = d_inode(h_ppath->dentry);
febd17d6 28467+ inode_lock_nested(h_dir, AuLsc_I_CHILD2);
758e9dad 28468+ h_path.mnt = h_ppath->mnt;
4f0767ce 28469+again:
758e9dad 28470+ h_path.dentry = vfsub_lkup_one(tgt, h_ppath);
1facf9fc 28471+ err = PTR_ERR(h_path.dentry);
28472+ if (IS_ERR(h_path.dentry))
28473+ goto out;
28474+
28475+ err = 0;
28476+ /* wh.plink dir is not monitored */
7f207e10 28477+ /* todo: is it really safe? */
5527c038
JR
28478+ if (d_is_positive(h_path.dentry)
28479+ && d_inode(h_path.dentry) != d_inode(h_dentry)) {
523b37e3
AM
28480+ delegated = NULL;
28481+ err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
28482+ if (unlikely(err == -EWOULDBLOCK)) {
28483+ pr_warn("cannot retry for NFSv4 delegation"
28484+ " for an internal unlink\n");
28485+ iput(delegated);
28486+ }
1facf9fc 28487+ dput(h_path.dentry);
28488+ h_path.dentry = NULL;
28489+ if (!err)
28490+ goto again;
28491+ }
5527c038 28492+ if (!err && d_is_negative(h_path.dentry)) {
523b37e3
AM
28493+ delegated = NULL;
28494+ err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
28495+ if (unlikely(err == -EWOULDBLOCK)) {
28496+ pr_warn("cannot retry for NFSv4 delegation"
28497+ " for an internal link\n");
28498+ iput(delegated);
28499+ }
28500+ }
1facf9fc 28501+ dput(h_path.dentry);
28502+
4f0767ce 28503+out:
febd17d6 28504+ inode_unlock(h_dir);
1facf9fc 28505+ return err;
28506+}
28507+
28508+struct do_whplink_args {
28509+ int *errp;
28510+ struct qstr *tgt;
758e9dad 28511+ struct path *h_ppath;
1facf9fc 28512+ struct dentry *h_dentry;
1facf9fc 28513+};
28514+
28515+static void call_do_whplink(void *args)
28516+{
28517+ struct do_whplink_args *a = args;
758e9dad 28518+ *a->errp = do_whplink(a->tgt, a->h_ppath, a->h_dentry);
1facf9fc 28519+}
28520+
28521+static int whplink(struct dentry *h_dentry, struct inode *inode,
758e9dad 28522+ aufs_bindex_t bindex)
1facf9fc 28523+{
28524+ int err, wkq_err;
758e9dad 28525+ struct au_branch *br;
1facf9fc 28526+ struct au_wbr *wbr;
758e9dad 28527+ struct path h_ppath;
1facf9fc 28528+ char a[PLINK_NAME_LEN];
0c3ec466 28529+ struct qstr tgtname = QSTR_INIT(a, 0);
1facf9fc 28530+
758e9dad
AM
28531+ br = au_sbr(inode->i_sb, bindex);
28532+ wbr = br->br_wbr;
28533+ h_ppath.dentry = wbr->wbr_plink;
28534+ h_ppath.mnt = au_br_mnt(br);
1facf9fc 28535+ tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28536+
28537+ /* always superio. */
2dfbb274 28538+ if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
1facf9fc 28539+ struct do_whplink_args args = {
28540+ .errp = &err,
28541+ .tgt = &tgtname,
758e9dad
AM
28542+ .h_ppath = &h_ppath,
28543+ .h_dentry = h_dentry
1facf9fc 28544+ };
28545+ wkq_err = au_wkq_wait(call_do_whplink, &args);
28546+ if (unlikely(wkq_err))
28547+ err = wkq_err;
28548+ } else
758e9dad 28549+ err = do_whplink(&tgtname, &h_ppath, h_dentry);
1facf9fc 28550+
28551+ return err;
28552+}
28553+
1facf9fc 28554+/*
28555+ * create a new pseudo-link for @h_dentry on @bindex.
28556+ * the linked inode is held in aufs @inode.
28557+ */
28558+void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
28559+ struct dentry *h_dentry)
28560+{
28561+ struct super_block *sb;
28562+ struct au_sbinfo *sbinfo;
8b6a4947
AM
28563+ struct hlist_bl_head *hbl;
28564+ struct hlist_bl_node *pos;
5afbbe0d 28565+ struct au_icntnr *icntnr;
86dc4139 28566+ int found, err, cnt, i;
1facf9fc 28567+
28568+ sb = inode->i_sb;
28569+ sbinfo = au_sbi(sb);
28570+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 28571+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 28572+
86dc4139 28573+ found = au_plink_test(inode);
4a4d8108 28574+ if (found)
1facf9fc 28575+ return;
4a4d8108 28576+
86dc4139 28577+ i = au_plink_hash(inode->i_ino);
8b6a4947 28578+ hbl = sbinfo->si_plink + i;
5afbbe0d 28579+ au_igrab(inode);
1facf9fc 28580+
8b6a4947
AM
28581+ hlist_bl_lock(hbl);
28582+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink) {
5afbbe0d 28583+ if (&icntnr->vfs_inode == inode) {
4a4d8108
AM
28584+ found = 1;
28585+ break;
28586+ }
1facf9fc 28587+ }
5afbbe0d
AM
28588+ if (!found) {
28589+ icntnr = container_of(inode, struct au_icntnr, vfs_inode);
8b6a4947 28590+ hlist_bl_add_head(&icntnr->plink, hbl);
5afbbe0d 28591+ }
8b6a4947 28592+ hlist_bl_unlock(hbl);
4a4d8108 28593+ if (!found) {
8b6a4947 28594+ cnt = au_hbl_count(hbl);
acd2b654 28595+#define msg "unexpectedly unbalanced or too many pseudo-links"
86dc4139
AM
28596+ if (cnt > AUFS_PLINK_WARN)
28597+ AuWarn1(msg ", %d\n", cnt);
28598+#undef msg
758e9dad 28599+ err = whplink(h_dentry, inode, bindex);
5afbbe0d
AM
28600+ if (unlikely(err)) {
28601+ pr_warn("err %d, damaged pseudo link.\n", err);
8b6a4947 28602+ au_hbl_del(&icntnr->plink, hbl);
5afbbe0d 28603+ iput(&icntnr->vfs_inode);
4a4d8108 28604+ }
5afbbe0d
AM
28605+ } else
28606+ iput(&icntnr->vfs_inode);
1facf9fc 28607+}
28608+
28609+/* free all plinks */
e49829fe 28610+void au_plink_put(struct super_block *sb, int verbose)
1facf9fc 28611+{
86dc4139 28612+ int i, warned;
1facf9fc 28613+ struct au_sbinfo *sbinfo;
8b6a4947
AM
28614+ struct hlist_bl_head *hbl;
28615+ struct hlist_bl_node *pos, *tmp;
5afbbe0d 28616+ struct au_icntnr *icntnr;
1facf9fc 28617+
dece6358
AM
28618+ SiMustWriteLock(sb);
28619+
1facf9fc 28620+ sbinfo = au_sbi(sb);
28621+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 28622+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 28623+
1facf9fc 28624+ /* no spin_lock since sbinfo is write-locked */
86dc4139
AM
28625+ warned = 0;
28626+ for (i = 0; i < AuPlink_NHASH; i++) {
8b6a4947
AM
28627+ hbl = sbinfo->si_plink + i;
28628+ if (!warned && verbose && !hlist_bl_empty(hbl)) {
86dc4139
AM
28629+ pr_warn("pseudo-link is not flushed");
28630+ warned = 1;
28631+ }
8b6a4947 28632+ hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink)
5afbbe0d 28633+ iput(&icntnr->vfs_inode);
8b6a4947 28634+ INIT_HLIST_BL_HEAD(hbl);
86dc4139 28635+ }
1facf9fc 28636+}
28637+
e49829fe
JR
28638+void au_plink_clean(struct super_block *sb, int verbose)
28639+{
28640+ struct dentry *root;
28641+
28642+ root = sb->s_root;
28643+ aufs_write_lock(root);
28644+ if (au_opt_test(au_mntflags(sb), PLINK))
28645+ au_plink_put(sb, verbose);
28646+ aufs_write_unlock(root);
28647+}
28648+
86dc4139
AM
28649+static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
28650+{
28651+ int do_put;
5afbbe0d 28652+ aufs_bindex_t btop, bbot, bindex;
86dc4139
AM
28653+
28654+ do_put = 0;
5afbbe0d
AM
28655+ btop = au_ibtop(inode);
28656+ bbot = au_ibbot(inode);
28657+ if (btop >= 0) {
28658+ for (bindex = btop; bindex <= bbot; bindex++) {
86dc4139
AM
28659+ if (!au_h_iptr(inode, bindex)
28660+ || au_ii_br_id(inode, bindex) != br_id)
28661+ continue;
28662+ au_set_h_iptr(inode, bindex, NULL, 0);
28663+ do_put = 1;
28664+ break;
28665+ }
28666+ if (do_put)
5afbbe0d 28667+ for (bindex = btop; bindex <= bbot; bindex++)
86dc4139
AM
28668+ if (au_h_iptr(inode, bindex)) {
28669+ do_put = 0;
28670+ break;
28671+ }
28672+ } else
28673+ do_put = 1;
28674+
28675+ return do_put;
28676+}
28677+
1facf9fc 28678+/* free the plinks on a branch specified by @br_id */
28679+void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
28680+{
28681+ struct au_sbinfo *sbinfo;
8b6a4947
AM
28682+ struct hlist_bl_head *hbl;
28683+ struct hlist_bl_node *pos, *tmp;
5afbbe0d 28684+ struct au_icntnr *icntnr;
1facf9fc 28685+ struct inode *inode;
86dc4139 28686+ int i, do_put;
1facf9fc 28687+
dece6358
AM
28688+ SiMustWriteLock(sb);
28689+
1facf9fc 28690+ sbinfo = au_sbi(sb);
28691+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 28692+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 28693+
8b6a4947 28694+ /* no bit_lock since sbinfo is write-locked */
86dc4139 28695+ for (i = 0; i < AuPlink_NHASH; i++) {
8b6a4947
AM
28696+ hbl = sbinfo->si_plink + i;
28697+ hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink) {
5afbbe0d 28698+ inode = au_igrab(&icntnr->vfs_inode);
86dc4139
AM
28699+ ii_write_lock_child(inode);
28700+ do_put = au_plink_do_half_refresh(inode, br_id);
5afbbe0d 28701+ if (do_put) {
8b6a4947 28702+ hlist_bl_del(&icntnr->plink);
5afbbe0d
AM
28703+ iput(inode);
28704+ }
86dc4139
AM
28705+ ii_write_unlock(inode);
28706+ iput(inode);
dece6358 28707+ }
dece6358
AM
28708+ }
28709+}
7f207e10 28710diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
eca34b5c 28711--- /usr/share/empty/fs/aufs/poll.c 1970-01-01 01:00:00.000000000 +0100
594d0238 28712+++ linux/fs/aufs/poll.c 2022-11-05 23:02:18.969222617 +0100
cd7a4cd9
AM
28713@@ -0,0 +1,51 @@
28714+// SPDX-License-Identifier: GPL-2.0
dece6358 28715+/*
0dcfbb52 28716+ * Copyright (C) 2005-2022 Junjiro R. Okajima
dece6358 28717+ *
0dcfbb52 28718+ * This program is free software; you can redistribute it and/or modify
dece6358
AM
28719+ * it under the terms of the GNU General Public License as published by
28720+ * the Free Software Foundation; either version 2 of the License, or
28721+ * (at your option) any later version.
28722+ *
28723+ * This program is distributed in the hope that it will be useful,
28724+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28725+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28726+ * GNU General Public License for more details.
28727+ *
28728+ * You should have received a copy of the GNU General Public License
523b37e3 28729+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358
AM
28730+ */
28731+
1308ab2a 28732+/*
28733+ * poll operation
28734+ * There is only one filesystem which implements ->poll operation, currently.
28735+ */
28736+
28737+#include "aufs.h"
28738+
cd7a4cd9 28739+__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt)
1308ab2a 28740+{
be118d29 28741+ __poll_t mask;
1308ab2a 28742+ struct file *h_file;
1308ab2a 28743+ struct super_block *sb;
28744+
28745+ /* We should pretend an error happened. */
be118d29 28746+ mask = EPOLLERR /* | EPOLLIN | EPOLLOUT */;
b912730e 28747+ sb = file->f_path.dentry->d_sb;
e49829fe 28748+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e 28749+
521ced18 28750+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
062440b3
AM
28751+ if (IS_ERR(h_file)) {
28752+ AuDbg("h_file %ld\n", PTR_ERR(h_file));
1308ab2a 28753+ goto out;
062440b3 28754+ }
1308ab2a 28755+
cd7a4cd9 28756+ mask = vfs_poll(h_file, pt);
b912730e 28757+ fput(h_file); /* instead of au_read_post() */
1308ab2a 28758+
4f0767ce 28759+out:
1308ab2a 28760+ si_read_unlock(sb);
062440b3 28761+ if (mask & EPOLLERR)
b00004a5 28762+ AuDbg("mask 0x%x\n", mask);
1308ab2a 28763+ return mask;
28764+}
c1595e42 28765diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
eca34b5c 28766--- /usr/share/empty/fs/aufs/posix_acl.c 1970-01-01 01:00:00.000000000 +0100
631230db 28767+++ linux/fs/aufs/posix_acl.c 2023-10-31 09:31:04.199880750 +0100
aec24f17 28768@@ -0,0 +1,108 @@
cd7a4cd9 28769+// SPDX-License-Identifier: GPL-2.0
c1595e42 28770+/*
0dcfbb52 28771+ * Copyright (C) 2014-2022 Junjiro R. Okajima
c1595e42 28772+ *
0dcfbb52 28773+ * This program is free software; you can redistribute it and/or modify
c1595e42
JR
28774+ * it under the terms of the GNU General Public License as published by
28775+ * the Free Software Foundation; either version 2 of the License, or
28776+ * (at your option) any later version.
28777+ *
28778+ * This program is distributed in the hope that it will be useful,
28779+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28780+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28781+ * GNU General Public License for more details.
28782+ *
28783+ * You should have received a copy of the GNU General Public License
28784+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
28785+ */
28786+
28787+/*
28788+ * posix acl operations
28789+ */
28790+
28791+#include <linux/fs.h>
c1595e42
JR
28792+#include "aufs.h"
28793+
aec24f17 28794+struct posix_acl *aufs_get_inode_acl(struct inode *inode, int type, bool rcu)
c1595e42
JR
28795+{
28796+ struct posix_acl *acl;
28797+ int err;
28798+ aufs_bindex_t bindex;
28799+ struct inode *h_inode;
28800+ struct super_block *sb;
28801+
758e9dad
AM
28802+ acl = ERR_PTR(-ECHILD);
28803+ if (rcu)
28804+ goto out;
28805+
c1595e42
JR
28806+ acl = NULL;
28807+ sb = inode->i_sb;
28808+ si_read_lock(sb, AuLock_FLUSH);
28809+ ii_read_lock_child(inode);
2121bcd9 28810+ if (!(sb->s_flags & SB_POSIXACL))
758e9dad 28811+ goto unlock;
c1595e42 28812+
5afbbe0d 28813+ bindex = au_ibtop(inode);
c1595e42
JR
28814+ h_inode = au_h_iptr(inode, bindex);
28815+ if (unlikely(!h_inode
28816+ || ((h_inode->i_mode & S_IFMT)
28817+ != (inode->i_mode & S_IFMT)))) {
28818+ err = au_busy_or_stale();
28819+ acl = ERR_PTR(err);
758e9dad 28820+ goto unlock;
c1595e42
JR
28821+ }
28822+
28823+ /* always topmost only */
aec24f17 28824+ acl = get_inode_acl(h_inode, type);
9f00928d
AM
28825+ if (IS_ERR(acl))
28826+ forget_cached_acl(inode, type);
28827+ else
a2654f78 28828+ set_cached_acl(inode, type, acl);
c1595e42 28829+
758e9dad 28830+unlock:
c1595e42
JR
28831+ ii_read_unlock(inode);
28832+ si_read_unlock(sb);
28833+
758e9dad 28834+out:
c1595e42
JR
28835+ AuTraceErrPtr(acl);
28836+ return acl;
28837+}
28838+
5db017eb 28839+struct posix_acl *aufs_get_acl(struct mnt_idmap *idmap,
aec24f17
AM
28840+ struct dentry *dentry, int type)
28841+{
28842+ struct posix_acl *acl;
28843+ struct inode *inode;
28844+
28845+ inode = d_inode(dentry);
28846+ acl = aufs_get_inode_acl(inode, type, /*rcu*/false);
28847+
28848+ return acl;
28849+}
28850+
5db017eb 28851+int aufs_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
0b2a12c6 28852+ struct posix_acl *acl, int type)
c1595e42
JR
28853+{
28854+ int err;
28855+ ssize_t ssz;
aec24f17 28856+ struct inode *inode;
f2c43d5f 28857+ struct au_sxattr arg = {
c1595e42
JR
28858+ .type = AU_ACL_SET,
28859+ .u.acl_set = {
28860+ .acl = acl,
28861+ .type = type
28862+ },
28863+ };
28864+
aec24f17 28865+ inode = d_inode(dentry);
5afbbe0d
AM
28866+ IMustLock(inode);
28867+
f2c43d5f 28868+ ssz = au_sxattr(dentry, inode, &arg);
9f00928d
AM
28869+ /* forget even it if succeeds since the branch might set differently */
28870+ forget_cached_acl(inode, type);
c1595e42 28871+ err = ssz;
9f00928d 28872+ if (ssz >= 0)
c1595e42
JR
28873+ err = 0;
28874+
c1595e42
JR
28875+ return err;
28876+}
7f207e10 28877diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
eca34b5c 28878--- /usr/share/empty/fs/aufs/procfs.c 1970-01-01 01:00:00.000000000 +0100
594d0238 28879+++ linux/fs/aufs/procfs.c 2022-11-05 23:02:18.969222617 +0100
394e211a 28880@@ -0,0 +1,170 @@
cd7a4cd9 28881+// SPDX-License-Identifier: GPL-2.0
e49829fe 28882+/*
0dcfbb52 28883+ * Copyright (C) 2010-2022 Junjiro R. Okajima
e49829fe 28884+ *
0dcfbb52 28885+ * This program is free software; you can redistribute it and/or modify
e49829fe
JR
28886+ * it under the terms of the GNU General Public License as published by
28887+ * the Free Software Foundation; either version 2 of the License, or
28888+ * (at your option) any later version.
28889+ *
28890+ * This program is distributed in the hope that it will be useful,
28891+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28892+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28893+ * GNU General Public License for more details.
28894+ *
28895+ * You should have received a copy of the GNU General Public License
523b37e3 28896+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
e49829fe
JR
28897+ */
28898+
28899+/*
28900+ * procfs interfaces
28901+ */
28902+
28903+#include <linux/proc_fs.h>
28904+#include "aufs.h"
28905+
28906+static int au_procfs_plm_release(struct inode *inode, struct file *file)
28907+{
28908+ struct au_sbinfo *sbinfo;
28909+
28910+ sbinfo = file->private_data;
28911+ if (sbinfo) {
28912+ au_plink_maint_leave(sbinfo);
28913+ kobject_put(&sbinfo->si_kobj);
28914+ }
28915+
28916+ return 0;
28917+}
28918+
28919+static void au_procfs_plm_write_clean(struct file *file)
28920+{
28921+ struct au_sbinfo *sbinfo;
28922+
28923+ sbinfo = file->private_data;
28924+ if (sbinfo)
28925+ au_plink_clean(sbinfo->si_sb, /*verbose*/0);
28926+}
28927+
28928+static int au_procfs_plm_write_si(struct file *file, unsigned long id)
28929+{
28930+ int err;
28931+ struct super_block *sb;
28932+ struct au_sbinfo *sbinfo;
8b6a4947 28933+ struct hlist_bl_node *pos;
e49829fe
JR
28934+
28935+ err = -EBUSY;
28936+ if (unlikely(file->private_data))
28937+ goto out;
28938+
28939+ sb = NULL;
53392da6 28940+ /* don't use au_sbilist_lock() here */
8b6a4947
AM
28941+ hlist_bl_lock(&au_sbilist);
28942+ hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
e49829fe 28943+ if (id == sysaufs_si_id(sbinfo)) {
83b672a5
AM
28944+ if (kobject_get_unless_zero(&sbinfo->si_kobj))
28945+ sb = sbinfo->si_sb;
e49829fe
JR
28946+ break;
28947+ }
8b6a4947 28948+ hlist_bl_unlock(&au_sbilist);
e49829fe
JR
28949+
28950+ err = -EINVAL;
28951+ if (unlikely(!sb))
28952+ goto out;
28953+
28954+ err = au_plink_maint_enter(sb);
28955+ if (!err)
28956+ /* keep kobject_get() */
28957+ file->private_data = sbinfo;
28958+ else
28959+ kobject_put(&sbinfo->si_kobj);
28960+out:
28961+ return err;
28962+}
28963+
28964+/*
28965+ * Accept a valid "si=xxxx" only.
28966+ * Once it is accepted successfully, accept "clean" too.
28967+ */
28968+static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
28969+ size_t count, loff_t *ppos)
28970+{
28971+ ssize_t err;
28972+ unsigned long id;
28973+ /* last newline is allowed */
28974+ char buf[3 + sizeof(unsigned long) * 2 + 1];
28975+
28976+ err = -EACCES;
28977+ if (unlikely(!capable(CAP_SYS_ADMIN)))
28978+ goto out;
28979+
28980+ err = -EINVAL;
28981+ if (unlikely(count > sizeof(buf)))
28982+ goto out;
28983+
28984+ err = copy_from_user(buf, ubuf, count);
28985+ if (unlikely(err)) {
28986+ err = -EFAULT;
28987+ goto out;
28988+ }
28989+ buf[count] = 0;
28990+
28991+ err = -EINVAL;
28992+ if (!strcmp("clean", buf)) {
28993+ au_procfs_plm_write_clean(file);
28994+ goto out_success;
28995+ } else if (unlikely(strncmp("si=", buf, 3)))
28996+ goto out;
28997+
9dbd164d 28998+ err = kstrtoul(buf + 3, 16, &id);
e49829fe
JR
28999+ if (unlikely(err))
29000+ goto out;
29001+
29002+ err = au_procfs_plm_write_si(file, id);
29003+ if (unlikely(err))
29004+ goto out;
29005+
29006+out_success:
29007+ err = count; /* success */
29008+out:
29009+ return err;
29010+}
29011+
394e211a
AM
29012+static const struct proc_ops au_procfs_plm_op = {
29013+ .proc_write = au_procfs_plm_write,
29014+ .proc_release = au_procfs_plm_release
e49829fe
JR
29015+};
29016+
29017+/* ---------------------------------------------------------------------- */
29018+
29019+static struct proc_dir_entry *au_procfs_dir;
29020+
29021+void au_procfs_fin(void)
29022+{
29023+ remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
29024+ remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
29025+}
29026+
29027+int __init au_procfs_init(void)
29028+{
29029+ int err;
29030+ struct proc_dir_entry *entry;
29031+
29032+ err = -ENOMEM;
29033+ au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
29034+ if (unlikely(!au_procfs_dir))
29035+ goto out;
29036+
cd7a4cd9 29037+ entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | 0200,
394e211a 29038+ au_procfs_dir, &au_procfs_plm_op);
e49829fe
JR
29039+ if (unlikely(!entry))
29040+ goto out_dir;
29041+
29042+ err = 0;
29043+ goto out; /* success */
29044+
29045+
29046+out_dir:
29047+ remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
29048+out:
29049+ return err;
29050+}
7f207e10 29051diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
eca34b5c 29052--- /usr/share/empty/fs/aufs/rdu.c 1970-01-01 01:00:00.000000000 +0100
631230db 29053+++ linux/fs/aufs/rdu.c 2023-10-31 09:31:04.199880750 +0100
ba1aed25 29054@@ -0,0 +1,384 @@
cd7a4cd9 29055+// SPDX-License-Identifier: GPL-2.0
1308ab2a 29056+/*
0dcfbb52 29057+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1308ab2a 29058+ *
0dcfbb52 29059+ * This program is free software; you can redistribute it and/or modify
1308ab2a 29060+ * it under the terms of the GNU General Public License as published by
29061+ * the Free Software Foundation; either version 2 of the License, or
29062+ * (at your option) any later version.
29063+ *
29064+ * This program is distributed in the hope that it will be useful,
29065+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
29066+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29067+ * GNU General Public License for more details.
29068+ *
29069+ * You should have received a copy of the GNU General Public License
523b37e3 29070+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1308ab2a 29071+ */
29072+
29073+/*
29074+ * readdir in userspace.
29075+ */
29076+
b752ccd1 29077+#include <linux/compat.h>
4a4d8108 29078+#include <linux/fs_stack.h>
1308ab2a 29079+#include <linux/security.h>
1308ab2a 29080+#include "aufs.h"
29081+
29082+/* bits for struct aufs_rdu.flags */
29083+#define AuRdu_CALLED 1
29084+#define AuRdu_CONT (1 << 1)
29085+#define AuRdu_FULL (1 << 2)
29086+#define au_ftest_rdu(flags, name) ((flags) & AuRdu_##name)
7f207e10
AM
29087+#define au_fset_rdu(flags, name) \
29088+ do { (flags) |= AuRdu_##name; } while (0)
29089+#define au_fclr_rdu(flags, name) \
29090+ do { (flags) &= ~AuRdu_##name; } while (0)
1308ab2a 29091+
29092+struct au_rdu_arg {
392086de 29093+ struct dir_context ctx;
1308ab2a 29094+ struct aufs_rdu *rdu;
29095+ union au_rdu_ent_ul ent;
29096+ unsigned long end;
29097+
29098+ struct super_block *sb;
29099+ int err;
29100+};
29101+
2255d0fe
AM
29102+static bool au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
29103+ loff_t offset, u64 h_ino, unsigned int d_type)
1308ab2a 29104+{
29105+ int err, len;
392086de 29106+ struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
1308ab2a 29107+ struct aufs_rdu *rdu = arg->rdu;
29108+ struct au_rdu_ent ent;
29109+
29110+ err = 0;
29111+ arg->err = 0;
29112+ au_fset_rdu(rdu->cookie.flags, CALLED);
29113+ len = au_rdu_len(nlen);
29114+ if (arg->ent.ul + len < arg->end) {
29115+ ent.ino = h_ino;
29116+ ent.bindex = rdu->cookie.bindex;
29117+ ent.type = d_type;
29118+ ent.nlen = nlen;
4a4d8108
AM
29119+ if (unlikely(nlen > AUFS_MAX_NAMELEN))
29120+ ent.type = DT_UNKNOWN;
1308ab2a 29121+
9dbd164d 29122+ /* unnecessary to support mmap_sem since this is a dir */
1308ab2a 29123+ err = -EFAULT;
29124+ if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
29125+ goto out;
29126+ if (copy_to_user(arg->ent.e->name, name, nlen))
29127+ goto out;
29128+ /* the terminating NULL */
29129+ if (__put_user(0, arg->ent.e->name + nlen))
29130+ goto out;
29131+ err = 0;
29132+ /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
29133+ arg->ent.ul += len;
29134+ rdu->rent++;
29135+ } else {
29136+ err = -EFAULT;
29137+ au_fset_rdu(rdu->cookie.flags, FULL);
29138+ rdu->full = 1;
29139+ rdu->tail = arg->ent;
29140+ }
29141+
4f0767ce 29142+out:
1308ab2a 29143+ /* AuTraceErr(err); */
2255d0fe 29144+ return !err;
1308ab2a 29145+}
29146+
29147+static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
29148+{
29149+ int err;
29150+ loff_t offset;
29151+ struct au_rdu_cookie *cookie = &arg->rdu->cookie;
29152+
92d182d2 29153+ /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
1308ab2a 29154+ offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
29155+ err = offset;
29156+ if (unlikely(offset != cookie->h_pos))
29157+ goto out;
29158+
29159+ err = 0;
29160+ do {
29161+ arg->err = 0;
29162+ au_fclr_rdu(cookie->flags, CALLED);
29163+ /* smp_mb(); */
392086de 29164+ err = vfsub_iterate_dir(h_file, &arg->ctx);
1308ab2a 29165+ if (err >= 0)
29166+ err = arg->err;
29167+ } while (!err
29168+ && au_ftest_rdu(cookie->flags, CALLED)
29169+ && !au_ftest_rdu(cookie->flags, FULL));
29170+ cookie->h_pos = h_file->f_pos;
29171+
4f0767ce 29172+out:
1308ab2a 29173+ AuTraceErr(err);
29174+ return err;
29175+}
29176+
29177+static int au_rdu(struct file *file, struct aufs_rdu *rdu)
29178+{
29179+ int err;
5afbbe0d 29180+ aufs_bindex_t bbot;
392086de
AM
29181+ struct au_rdu_arg arg = {
29182+ .ctx = {
2000de60 29183+ .actor = au_rdu_fill
392086de
AM
29184+ }
29185+ };
1308ab2a 29186+ struct dentry *dentry;
29187+ struct inode *inode;
29188+ struct file *h_file;
29189+ struct au_rdu_cookie *cookie = &rdu->cookie;
29190+
ba1aed25
AM
29191+ /* VERIFY_WRITE */
29192+ err = !access_ok(rdu->ent.e, rdu->sz);
1308ab2a 29193+ if (unlikely(err)) {
29194+ err = -EFAULT;
29195+ AuTraceErr(err);
29196+ goto out;
29197+ }
29198+ rdu->rent = 0;
29199+ rdu->tail = rdu->ent;
29200+ rdu->full = 0;
29201+ arg.rdu = rdu;
29202+ arg.ent = rdu->ent;
29203+ arg.end = arg.ent.ul;
29204+ arg.end += rdu->sz;
29205+
29206+ err = -ENOTDIR;
171c7011 29207+ if (unlikely(!file->f_op->iterate_shared))
1308ab2a 29208+ goto out;
29209+
29210+ err = security_file_permission(file, MAY_READ);
29211+ AuTraceErr(err);
29212+ if (unlikely(err))
29213+ goto out;
29214+
2000de60 29215+ dentry = file->f_path.dentry;
5527c038 29216+ inode = d_inode(dentry);
5afbbe0d 29217+ inode_lock_shared(inode);
1308ab2a 29218+
29219+ arg.sb = inode->i_sb;
e49829fe
JR
29220+ err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
29221+ if (unlikely(err))
29222+ goto out_mtx;
027c5e7a
AM
29223+ err = au_alive_dir(dentry);
29224+ if (unlikely(err))
29225+ goto out_si;
e49829fe 29226+ /* todo: reval? */
1308ab2a 29227+ fi_read_lock(file);
29228+
29229+ err = -EAGAIN;
29230+ if (unlikely(au_ftest_rdu(cookie->flags, CONT)
29231+ && cookie->generation != au_figen(file)))
29232+ goto out_unlock;
29233+
29234+ err = 0;
29235+ if (!rdu->blk) {
29236+ rdu->blk = au_sbi(arg.sb)->si_rdblk;
29237+ if (!rdu->blk)
29238+ rdu->blk = au_dir_size(file, /*dentry*/NULL);
29239+ }
5afbbe0d
AM
29240+ bbot = au_fbtop(file);
29241+ if (cookie->bindex < bbot)
29242+ cookie->bindex = bbot;
29243+ bbot = au_fbbot_dir(file);
29244+ /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
29245+ for (; !err && cookie->bindex <= bbot;
1308ab2a 29246+ cookie->bindex++, cookie->h_pos = 0) {
4a4d8108 29247+ h_file = au_hf_dir(file, cookie->bindex);
1308ab2a 29248+ if (!h_file)
29249+ continue;
29250+
29251+ au_fclr_rdu(cookie->flags, FULL);
29252+ err = au_rdu_do(h_file, &arg);
29253+ AuTraceErr(err);
29254+ if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
29255+ break;
29256+ }
29257+ AuDbg("rent %llu\n", rdu->rent);
29258+
29259+ if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
29260+ rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
29261+ au_fset_rdu(cookie->flags, CONT);
29262+ cookie->generation = au_figen(file);
29263+ }
29264+
29265+ ii_read_lock_child(inode);
5afbbe0d 29266+ fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
1308ab2a 29267+ ii_read_unlock(inode);
29268+
4f0767ce 29269+out_unlock:
1308ab2a 29270+ fi_read_unlock(file);
027c5e7a 29271+out_si:
1308ab2a 29272+ si_read_unlock(arg.sb);
4f0767ce 29273+out_mtx:
5afbbe0d 29274+ inode_unlock_shared(inode);
4f0767ce 29275+out:
1308ab2a 29276+ AuTraceErr(err);
29277+ return err;
29278+}
29279+
29280+static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
29281+{
29282+ int err;
29283+ ino_t ino;
29284+ unsigned long long nent;
29285+ union au_rdu_ent_ul *u;
29286+ struct au_rdu_ent ent;
29287+ struct super_block *sb;
29288+
29289+ err = 0;
29290+ nent = rdu->nent;
29291+ u = &rdu->ent;
2000de60 29292+ sb = file->f_path.dentry->d_sb;
1308ab2a 29293+ si_read_lock(sb, AuLock_FLUSH);
29294+ while (nent-- > 0) {
9dbd164d 29295+ /* unnecessary to support mmap_sem since this is a dir */
1308ab2a 29296+ err = copy_from_user(&ent, u->e, sizeof(ent));
4a4d8108 29297+ if (!err)
ba1aed25
AM
29298+ /* VERIFY_WRITE */
29299+ err = !access_ok(&u->e->ino, sizeof(ino));
1308ab2a 29300+ if (unlikely(err)) {
29301+ err = -EFAULT;
29302+ AuTraceErr(err);
29303+ break;
29304+ }
29305+
29306+ /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
29307+ if (!ent.wh)
29308+ err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
29309+ else
29310+ err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
29311+ &ino);
29312+ if (unlikely(err)) {
29313+ AuTraceErr(err);
29314+ break;
29315+ }
29316+
29317+ err = __put_user(ino, &u->e->ino);
29318+ if (unlikely(err)) {
29319+ err = -EFAULT;
29320+ AuTraceErr(err);
29321+ break;
29322+ }
29323+ u->ul += au_rdu_len(ent.nlen);
29324+ }
29325+ si_read_unlock(sb);
29326+
29327+ return err;
29328+}
29329+
29330+/* ---------------------------------------------------------------------- */
29331+
29332+static int au_rdu_verify(struct aufs_rdu *rdu)
29333+{
b752ccd1 29334+ AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
1308ab2a 29335+ "%llu, b%d, 0x%x, g%u}\n",
b752ccd1 29336+ rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
1308ab2a 29337+ rdu->blk,
29338+ rdu->rent, rdu->shwh, rdu->full,
29339+ rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
29340+ rdu->cookie.generation);
dece6358 29341+
b752ccd1 29342+ if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
1308ab2a 29343+ return 0;
dece6358 29344+
b752ccd1
AM
29345+ AuDbg("%u:%u\n",
29346+ rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
1308ab2a 29347+ return -EINVAL;
29348+}
29349+
29350+long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
dece6358 29351+{
1308ab2a 29352+ long err, e;
29353+ struct aufs_rdu rdu;
29354+ void __user *p = (void __user *)arg;
dece6358 29355+
1308ab2a 29356+ err = copy_from_user(&rdu, p, sizeof(rdu));
29357+ if (unlikely(err)) {
29358+ err = -EFAULT;
29359+ AuTraceErr(err);
29360+ goto out;
29361+ }
29362+ err = au_rdu_verify(&rdu);
dece6358
AM
29363+ if (unlikely(err))
29364+ goto out;
29365+
1308ab2a 29366+ switch (cmd) {
29367+ case AUFS_CTL_RDU:
29368+ err = au_rdu(file, &rdu);
29369+ if (unlikely(err))
29370+ break;
dece6358 29371+
1308ab2a 29372+ e = copy_to_user(p, &rdu, sizeof(rdu));
29373+ if (unlikely(e)) {
29374+ err = -EFAULT;
29375+ AuTraceErr(err);
29376+ }
29377+ break;
29378+ case AUFS_CTL_RDU_INO:
29379+ err = au_rdu_ino(file, &rdu);
29380+ break;
29381+
29382+ default:
4a4d8108 29383+ /* err = -ENOTTY; */
1308ab2a 29384+ err = -EINVAL;
29385+ }
dece6358 29386+
4f0767ce 29387+out:
1308ab2a 29388+ AuTraceErr(err);
29389+ return err;
1facf9fc 29390+}
b752ccd1
AM
29391+
29392+#ifdef CONFIG_COMPAT
29393+long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
29394+{
29395+ long err, e;
29396+ struct aufs_rdu rdu;
29397+ void __user *p = compat_ptr(arg);
29398+
29399+ /* todo: get_user()? */
29400+ err = copy_from_user(&rdu, p, sizeof(rdu));
29401+ if (unlikely(err)) {
29402+ err = -EFAULT;
29403+ AuTraceErr(err);
29404+ goto out;
29405+ }
29406+ rdu.ent.e = compat_ptr(rdu.ent.ul);
29407+ err = au_rdu_verify(&rdu);
29408+ if (unlikely(err))
29409+ goto out;
29410+
29411+ switch (cmd) {
29412+ case AUFS_CTL_RDU:
29413+ err = au_rdu(file, &rdu);
29414+ if (unlikely(err))
29415+ break;
29416+
29417+ rdu.ent.ul = ptr_to_compat(rdu.ent.e);
29418+ rdu.tail.ul = ptr_to_compat(rdu.tail.e);
29419+ e = copy_to_user(p, &rdu, sizeof(rdu));
29420+ if (unlikely(e)) {
29421+ err = -EFAULT;
29422+ AuTraceErr(err);
29423+ }
29424+ break;
29425+ case AUFS_CTL_RDU_INO:
29426+ err = au_rdu_ino(file, &rdu);
29427+ break;
29428+
29429+ default:
29430+ /* err = -ENOTTY; */
29431+ err = -EINVAL;
29432+ }
29433+
4f0767ce 29434+out:
b752ccd1
AM
29435+ AuTraceErr(err);
29436+ return err;
29437+}
29438+#endif
7f207e10 29439diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
eca34b5c 29440--- /usr/share/empty/fs/aufs/rwsem.h 1970-01-01 01:00:00.000000000 +0100
594d0238 29441+++ linux/fs/aufs/rwsem.h 2022-11-05 23:02:18.969222617 +0100
42a736d3 29442@@ -0,0 +1,85 @@
062440b3 29443+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 29444+/*
0dcfbb52 29445+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 29446+ *
0dcfbb52 29447+ * This program is free software; you can redistribute it and/or modify
1facf9fc 29448+ * it under the terms of the GNU General Public License as published by
29449+ * the Free Software Foundation; either version 2 of the License, or
29450+ * (at your option) any later version.
dece6358
AM
29451+ *
29452+ * This program is distributed in the hope that it will be useful,
29453+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
29454+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29455+ * GNU General Public License for more details.
29456+ *
29457+ * You should have received a copy of the GNU General Public License
523b37e3 29458+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 29459+ */
29460+
29461+/*
29462+ * simple read-write semaphore wrappers
29463+ */
29464+
29465+#ifndef __AUFS_RWSEM_H__
29466+#define __AUFS_RWSEM_H__
29467+
29468+#ifdef __KERNEL__
29469+
4a4d8108 29470+#include "debug.h"
dece6358 29471+
acd2b654 29472+/* in the future, the name 'au_rwsem' will be totally gone */
8b6a4947 29473+#define au_rwsem rw_semaphore
dece6358
AM
29474+
29475+/* to debug easier, do not make them inlined functions */
8b6a4947 29476+#define AuRwMustNoWaiters(rw) AuDebugOn(rwsem_is_contended(rw))
42a736d3
AM
29477+
29478+#ifdef CONFIG_LOCKDEP
dece6358 29479+/* rwsem_is_locked() is unusable */
42a736d3
AM
29480+#define AuRwMustReadLock(rw) AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP) \
29481+ && !lockdep_recursing(current) \
29482+ && debug_locks \
8b6a4947 29483+ && !lockdep_is_held_type(rw, 1))
42a736d3
AM
29484+#define AuRwMustWriteLock(rw) AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP) \
29485+ && !lockdep_recursing(current) \
29486+ && debug_locks \
8b6a4947 29487+ && !lockdep_is_held_type(rw, 0))
42a736d3
AM
29488+#define AuRwMustAnyLock(rw) AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP) \
29489+ && !lockdep_recursing(current) \
29490+ && debug_locks \
8b6a4947 29491+ && !lockdep_is_held(rw))
42a736d3
AM
29492+#define AuRwDestroy(rw) AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP) \
29493+ && !lockdep_recursing(current) \
29494+ && debug_locks \
8b6a4947 29495+ && lockdep_is_held(rw))
42a736d3
AM
29496+#else
29497+#define AuRwMustReadLock(rw) do {} while (0)
29498+#define AuRwMustWriteLock(rw) do {} while (0)
29499+#define AuRwMustAnyLock(rw) do {} while (0)
29500+#define AuRwDestroy(rw) do {} while (0)
29501+#endif
8b6a4947
AM
29502+
29503+#define au_rw_init(rw) init_rwsem(rw)
dece6358 29504+
5afbbe0d
AM
29505+#define au_rw_init_wlock(rw) do { \
29506+ au_rw_init(rw); \
8b6a4947 29507+ down_write(rw); \
5afbbe0d 29508+ } while (0)
dece6358 29509+
8b6a4947
AM
29510+#define au_rw_init_wlock_nested(rw, lsc) do { \
29511+ au_rw_init(rw); \
29512+ down_write_nested(rw, lsc); \
5afbbe0d 29513+ } while (0)
dece6358 29514+
8b6a4947
AM
29515+#define au_rw_read_lock(rw) down_read(rw)
29516+#define au_rw_read_lock_nested(rw, lsc) down_read_nested(rw, lsc)
29517+#define au_rw_read_unlock(rw) up_read(rw)
29518+#define au_rw_dgrade_lock(rw) downgrade_write(rw)
29519+#define au_rw_write_lock(rw) down_write(rw)
29520+#define au_rw_write_lock_nested(rw, lsc) down_write_nested(rw, lsc)
29521+#define au_rw_write_unlock(rw) up_write(rw)
29522+/* why is not _nested version defined? */
29523+#define au_rw_read_trylock(rw) down_read_trylock(rw)
29524+#define au_rw_write_trylock(rw) down_write_trylock(rw)
1facf9fc 29525+
29526+#endif /* __KERNEL__ */
29527+#endif /* __AUFS_RWSEM_H__ */
7f207e10 29528diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
eca34b5c 29529--- /usr/share/empty/fs/aufs/sbinfo.c 1970-01-01 01:00:00.000000000 +0100
594d0238 29530+++ linux/fs/aufs/sbinfo.c 2022-11-05 23:02:18.969222617 +0100
f4d37d76 29531@@ -0,0 +1,316 @@
cd7a4cd9 29532+// SPDX-License-Identifier: GPL-2.0
1facf9fc 29533+/*
0dcfbb52 29534+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 29535+ *
0dcfbb52 29536+ * This program is free software; you can redistribute it and/or modify
1facf9fc 29537+ * it under the terms of the GNU General Public License as published by
29538+ * the Free Software Foundation; either version 2 of the License, or
29539+ * (at your option) any later version.
dece6358
AM
29540+ *
29541+ * This program is distributed in the hope that it will be useful,
29542+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
29543+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29544+ * GNU General Public License for more details.
29545+ *
29546+ * You should have received a copy of the GNU General Public License
523b37e3 29547+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 29548+ */
29549+
29550+/*
29551+ * superblock private data
29552+ */
29553+
eca801bf 29554+#include <linux/iversion.h>
1facf9fc 29555+#include "aufs.h"
29556+
29557+/*
29558+ * they are necessary regardless sysfs is disabled.
29559+ */
29560+void au_si_free(struct kobject *kobj)
29561+{
86dc4139 29562+ int i;
1facf9fc 29563+ struct au_sbinfo *sbinfo;
b752ccd1 29564+ char *locked __maybe_unused; /* debug only */
1facf9fc 29565+
29566+ sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
86dc4139 29567+ for (i = 0; i < AuPlink_NHASH; i++)
8b6a4947 29568+ AuDebugOn(!hlist_bl_empty(sbinfo->si_plink + i));
f0c0a007 29569+ AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
5afbbe0d 29570+
acd2b654
AM
29571+ AuLCntZero(au_lcnt_read(&sbinfo->si_ninodes, /*do_rev*/0));
29572+ au_lcnt_fin(&sbinfo->si_ninodes, /*do_sync*/0);
29573+ AuLCntZero(au_lcnt_read(&sbinfo->si_nfiles, /*do_rev*/0));
29574+ au_lcnt_fin(&sbinfo->si_nfiles, /*do_sync*/0);
1facf9fc 29575+
062440b3 29576+ dbgaufs_si_fin(sbinfo);
e49829fe 29577+ au_rw_write_lock(&sbinfo->si_rwsem);
1facf9fc 29578+ au_br_free(sbinfo);
e49829fe 29579+ au_rw_write_unlock(&sbinfo->si_rwsem);
b752ccd1 29580+
9f237c51 29581+ au_kfree_try_rcu(sbinfo->si_branch);
1facf9fc 29582+ mutex_destroy(&sbinfo->si_xib_mtx);
dece6358 29583+ AuRwDestroy(&sbinfo->si_rwsem);
1facf9fc 29584+
acd2b654
AM
29585+ au_lcnt_wait_for_fin(&sbinfo->si_ninodes);
29586+ /* si_nfiles is waited too */
9f237c51 29587+ au_kfree_rcu(sbinfo);
1facf9fc 29588+}
29589+
f4d37d76 29590+struct au_sbinfo *au_si_alloc(struct super_block *sb)
1facf9fc 29591+{
1facf9fc 29592+ struct au_sbinfo *sbinfo;
f4d37d76 29593+ int err, i;
1facf9fc 29594+
29595+ err = -ENOMEM;
4a4d8108 29596+ sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
1facf9fc 29597+ if (unlikely(!sbinfo))
29598+ goto out;
29599+
29600+ /* will be reallocated separately */
29601+ sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
29602+ if (unlikely(!sbinfo->si_branch))
febd17d6 29603+ goto out_sbinfo;
1facf9fc 29604+
1facf9fc 29605+ err = sysaufs_si_init(sbinfo);
062440b3
AM
29606+ if (!err) {
29607+ dbgaufs_si_null(sbinfo);
29608+ err = dbgaufs_si_init(sbinfo);
29609+ if (unlikely(err))
29610+ kobject_put(&sbinfo->si_kobj);
29611+ }
1facf9fc 29612+ if (unlikely(err))
29613+ goto out_br;
29614+
29615+ au_nwt_init(&sbinfo->si_nowait);
dece6358 29616+ au_rw_init_wlock(&sbinfo->si_rwsem);
b752ccd1 29617+
acd2b654
AM
29618+ au_lcnt_init(&sbinfo->si_ninodes, /*release*/NULL);
29619+ au_lcnt_init(&sbinfo->si_nfiles, /*release*/NULL);
7f207e10 29620+
5afbbe0d 29621+ sbinfo->si_bbot = -1;
392086de 29622+ sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
1facf9fc 29623+
29624+ sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
29625+ sbinfo->si_wbr_create = AuWbrCreate_Def;
4a4d8108
AM
29626+ sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
29627+ sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
1facf9fc 29628+
076b876e
AM
29629+ au_fhsm_init(sbinfo);
29630+
e49829fe 29631+ sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
1facf9fc 29632+
392086de
AM
29633+ sbinfo->si_xino_jiffy = jiffies;
29634+ sbinfo->si_xino_expire
29635+ = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
1facf9fc 29636+ mutex_init(&sbinfo->si_xib_mtx);
1facf9fc 29637+ /* leave si_xib_last_pindex and si_xib_next_bit */
29638+
8b6a4947 29639+ INIT_HLIST_BL_HEAD(&sbinfo->si_aopen);
b912730e 29640+
e49829fe 29641+ sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
1facf9fc 29642+ sbinfo->si_rdblk = AUFS_RDBLK_DEF;
29643+ sbinfo->si_rdhash = AUFS_RDHASH_DEF;
29644+ sbinfo->si_dirwh = AUFS_DIRWH_DEF;
29645+
86dc4139 29646+ for (i = 0; i < AuPlink_NHASH; i++)
8b6a4947 29647+ INIT_HLIST_BL_HEAD(sbinfo->si_plink + i);
1facf9fc 29648+ init_waitqueue_head(&sbinfo->si_plink_wq);
4a4d8108 29649+ spin_lock_init(&sbinfo->si_plink_maint_lock);
1facf9fc 29650+
8b6a4947 29651+ INIT_HLIST_BL_HEAD(&sbinfo->si_files);
523b37e3 29652+
b95c5147
AM
29653+ /* with getattr by default */
29654+ sbinfo->si_iop_array = aufs_iop;
29655+
1facf9fc 29656+ /* leave other members for sysaufs and si_mnt. */
29657+ sbinfo->si_sb = sb;
f4d37d76
AM
29658+ if (sb) {
29659+ sb->s_fs_info = sbinfo;
29660+ si_pid_set(sb);
29661+ }
29662+ return sbinfo; /* success */
1facf9fc 29663+
4f0767ce 29664+out_br:
9f237c51 29665+ au_kfree_try_rcu(sbinfo->si_branch);
4f0767ce 29666+out_sbinfo:
9f237c51 29667+ au_kfree_rcu(sbinfo);
4f0767ce 29668+out:
f4d37d76 29669+ return ERR_PTR(err);
1facf9fc 29670+}
29671+
e2f27e51 29672+int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
1facf9fc 29673+{
29674+ int err, sz;
29675+ struct au_branch **brp;
29676+
dece6358
AM
29677+ AuRwMustWriteLock(&sbinfo->si_rwsem);
29678+
1facf9fc 29679+ err = -ENOMEM;
5afbbe0d 29680+ sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
1facf9fc 29681+ if (unlikely(!sz))
29682+ sz = sizeof(*brp);
e2f27e51
AM
29683+ brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
29684+ may_shrink);
1facf9fc 29685+ if (brp) {
29686+ sbinfo->si_branch = brp;
29687+ err = 0;
29688+ }
29689+
29690+ return err;
29691+}
29692+
29693+/* ---------------------------------------------------------------------- */
29694+
29695+unsigned int au_sigen_inc(struct super_block *sb)
29696+{
29697+ unsigned int gen;
5527c038 29698+ struct inode *inode;
1facf9fc 29699+
dece6358
AM
29700+ SiMustWriteLock(sb);
29701+
1facf9fc 29702+ gen = ++au_sbi(sb)->si_generation;
29703+ au_update_digen(sb->s_root);
5527c038
JR
29704+ inode = d_inode(sb->s_root);
29705+ au_update_iigen(inode, /*half*/0);
be118d29 29706+ inode_inc_iversion(inode);
1facf9fc 29707+ return gen;
29708+}
29709+
29710+aufs_bindex_t au_new_br_id(struct super_block *sb)
29711+{
29712+ aufs_bindex_t br_id;
29713+ int i;
29714+ struct au_sbinfo *sbinfo;
29715+
dece6358
AM
29716+ SiMustWriteLock(sb);
29717+
1facf9fc 29718+ sbinfo = au_sbi(sb);
29719+ for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
29720+ br_id = ++sbinfo->si_last_br_id;
7f207e10 29721+ AuDebugOn(br_id < 0);
1facf9fc 29722+ if (br_id && au_br_index(sb, br_id) < 0)
29723+ return br_id;
29724+ }
29725+
29726+ return -1;
29727+}
29728+
29729+/* ---------------------------------------------------------------------- */
29730+
e49829fe
JR
29731+/* it is ok that new 'nwt' tasks are appended while we are sleeping */
29732+int si_read_lock(struct super_block *sb, int flags)
29733+{
29734+ int err;
29735+
29736+ err = 0;
29737+ if (au_ftest_lock(flags, FLUSH))
29738+ au_nwt_flush(&au_sbi(sb)->si_nowait);
29739+
29740+ si_noflush_read_lock(sb);
29741+ err = au_plink_maint(sb, flags);
29742+ if (unlikely(err))
29743+ si_read_unlock(sb);
29744+
29745+ return err;
29746+}
29747+
29748+int si_write_lock(struct super_block *sb, int flags)
29749+{
29750+ int err;
29751+
29752+ if (au_ftest_lock(flags, FLUSH))
29753+ au_nwt_flush(&au_sbi(sb)->si_nowait);
29754+
29755+ si_noflush_write_lock(sb);
29756+ err = au_plink_maint(sb, flags);
29757+ if (unlikely(err))
29758+ si_write_unlock(sb);
29759+
29760+ return err;
29761+}
29762+
1facf9fc 29763+/* dentry and super_block lock. call at entry point */
e49829fe 29764+int aufs_read_lock(struct dentry *dentry, int flags)
1facf9fc 29765+{
e49829fe 29766+ int err;
027c5e7a 29767+ struct super_block *sb;
e49829fe 29768+
027c5e7a
AM
29769+ sb = dentry->d_sb;
29770+ err = si_read_lock(sb, flags);
29771+ if (unlikely(err))
29772+ goto out;
29773+
29774+ if (au_ftest_lock(flags, DW))
29775+ di_write_lock_child(dentry);
29776+ else
29777+ di_read_lock_child(dentry, flags);
29778+
29779+ if (au_ftest_lock(flags, GEN)) {
29780+ err = au_digen_test(dentry, au_sigen(sb));
79b8bda9
AM
29781+ if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
29782+ AuDebugOn(!err && au_dbrange_test(dentry));
29783+ else if (!err)
29784+ err = au_dbrange_test(dentry);
027c5e7a
AM
29785+ if (unlikely(err))
29786+ aufs_read_unlock(dentry, flags);
e49829fe
JR
29787+ }
29788+
027c5e7a 29789+out:
e49829fe 29790+ return err;
1facf9fc 29791+}
29792+
29793+void aufs_read_unlock(struct dentry *dentry, int flags)
29794+{
29795+ if (au_ftest_lock(flags, DW))
29796+ di_write_unlock(dentry);
29797+ else
29798+ di_read_unlock(dentry, flags);
29799+ si_read_unlock(dentry->d_sb);
29800+}
29801+
29802+void aufs_write_lock(struct dentry *dentry)
29803+{
e49829fe 29804+ si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
1facf9fc 29805+ di_write_lock_child(dentry);
29806+}
29807+
29808+void aufs_write_unlock(struct dentry *dentry)
29809+{
29810+ di_write_unlock(dentry);
29811+ si_write_unlock(dentry->d_sb);
29812+}
29813+
e49829fe 29814+int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
1facf9fc 29815+{
e49829fe 29816+ int err;
027c5e7a
AM
29817+ unsigned int sigen;
29818+ struct super_block *sb;
e49829fe 29819+
027c5e7a
AM
29820+ sb = d1->d_sb;
29821+ err = si_read_lock(sb, flags);
29822+ if (unlikely(err))
29823+ goto out;
29824+
b95c5147 29825+ di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
027c5e7a
AM
29826+
29827+ if (au_ftest_lock(flags, GEN)) {
29828+ sigen = au_sigen(sb);
29829+ err = au_digen_test(d1, sigen);
29830+ AuDebugOn(!err && au_dbrange_test(d1));
29831+ if (!err) {
29832+ err = au_digen_test(d2, sigen);
29833+ AuDebugOn(!err && au_dbrange_test(d2));
29834+ }
29835+ if (unlikely(err))
29836+ aufs_read_and_write_unlock2(d1, d2);
29837+ }
29838+
29839+out:
e49829fe 29840+ return err;
1facf9fc 29841+}
29842+
29843+void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
29844+{
29845+ di_write_unlock2(d1, d2);
29846+ si_read_unlock(d1->d_sb);
29847+}
7f207e10 29848diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
eca34b5c 29849--- /usr/share/empty/fs/aufs/super.c 1970-01-01 01:00:00.000000000 +0100
631230db 29850+++ linux/fs/aufs/super.c 2023-10-10 22:51:18.033248030 +0200
f4d37d76 29851@@ -0,0 +1,871 @@
cd7a4cd9 29852+// SPDX-License-Identifier: GPL-2.0
1facf9fc 29853+/*
0dcfbb52 29854+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 29855+ *
0dcfbb52 29856+ * This program is free software; you can redistribute it and/or modify
1facf9fc 29857+ * it under the terms of the GNU General Public License as published by
29858+ * the Free Software Foundation; either version 2 of the License, or
29859+ * (at your option) any later version.
dece6358
AM
29860+ *
29861+ * This program is distributed in the hope that it will be useful,
29862+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
29863+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29864+ * GNU General Public License for more details.
29865+ *
29866+ * You should have received a copy of the GNU General Public License
523b37e3 29867+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 29868+ */
29869+
29870+/*
29871+ * mount and super_block operations
29872+ */
29873+
eca801bf 29874+#include <linux/iversion.h>
f6c5ef8b 29875+#include <linux/mm.h>
1facf9fc 29876+#include <linux/seq_file.h>
29877+#include <linux/statfs.h>
7f207e10 29878+#include <linux/vmalloc.h>
1facf9fc 29879+#include "aufs.h"
29880+
29881+/*
29882+ * super_operations
29883+ */
29884+static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
29885+{
29886+ struct au_icntnr *c;
29887+
26eb093c 29888+ c = au_cache_alloc_icntnr(sb);
1facf9fc 29889+ if (c) {
027c5e7a 29890+ au_icntnr_init(c);
be118d29 29891+ inode_set_iversion(&c->vfs_inode, 1); /* sigen(sb); */
1facf9fc 29892+ c->iinfo.ii_hinode = NULL;
29893+ return &c->vfs_inode;
29894+ }
29895+ return NULL;
29896+}
29897+
29898+static void aufs_destroy_inode(struct inode *inode)
29899+{
5afbbe0d
AM
29900+ if (!au_is_bad_inode(inode))
29901+ au_iinfo_fin(inode);
fbc438ed
JR
29902+}
29903+
29904+static void aufs_free_inode(struct inode *inode)
29905+{
29906+ au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
1facf9fc 29907+}
29908+
29909+struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
29910+{
29911+ struct inode *inode;
29912+ int err;
29913+
29914+ inode = iget_locked(sb, ino);
29915+ if (unlikely(!inode)) {
29916+ inode = ERR_PTR(-ENOMEM);
29917+ goto out;
29918+ }
29919+ if (!(inode->i_state & I_NEW))
29920+ goto out;
29921+
29922+ err = au_xigen_new(inode);
29923+ if (!err)
29924+ err = au_iinfo_init(inode);
29925+ if (!err)
be118d29 29926+ inode_inc_iversion(inode);
1facf9fc 29927+ else {
29928+ iget_failed(inode);
29929+ inode = ERR_PTR(err);
29930+ }
29931+
4f0767ce 29932+out:
1facf9fc 29933+ /* never return NULL */
29934+ AuDebugOn(!inode);
29935+ AuTraceErrPtr(inode);
29936+ return inode;
29937+}
29938+
29939+/* lock free root dinfo */
29940+static int au_show_brs(struct seq_file *seq, struct super_block *sb)
29941+{
29942+ int err;
5afbbe0d 29943+ aufs_bindex_t bindex, bbot;
1facf9fc 29944+ struct path path;
4a4d8108 29945+ struct au_hdentry *hdp;
1facf9fc 29946+ struct au_branch *br;
076b876e 29947+ au_br_perm_str_t perm;
1facf9fc 29948+
29949+ err = 0;
5afbbe0d
AM
29950+ bbot = au_sbbot(sb);
29951+ bindex = 0;
29952+ hdp = au_hdentry(au_di(sb->s_root), bindex);
29953+ for (; !err && bindex <= bbot; bindex++, hdp++) {
1facf9fc 29954+ br = au_sbr(sb, bindex);
86dc4139 29955+ path.mnt = au_br_mnt(br);
5afbbe0d 29956+ path.dentry = hdp->hd_dentry;
1facf9fc 29957+ err = au_seq_path(seq, &path);
79b8bda9 29958+ if (!err) {
076b876e 29959+ au_optstr_br_perm(&perm, br->br_perm);
79b8bda9 29960+ seq_printf(seq, "=%s", perm.a);
5afbbe0d 29961+ if (bindex != bbot)
79b8bda9 29962+ seq_putc(seq, ':');
1e00d052 29963+ }
1facf9fc 29964+ }
79b8bda9
AM
29965+ if (unlikely(err || seq_has_overflowed(seq)))
29966+ err = -E2BIG;
1facf9fc 29967+
29968+ return err;
29969+}
29970+
631230db 29971+static void au_gen_fmt(char *fmt, int len, const char *pat,
f2c43d5f
AM
29972+ const char *append)
29973+{
29974+ char *p;
29975+
29976+ p = fmt;
29977+ while (*pat != ':')
29978+ *p++ = *pat++;
29979+ *p++ = *pat++;
631230db 29980+ strscpy(p, append, len - (p - fmt));
f2c43d5f
AM
29981+ AuDebugOn(strlen(fmt) >= len);
29982+}
29983+
1facf9fc 29984+static void au_show_wbr_create(struct seq_file *m, int v,
29985+ struct au_sbinfo *sbinfo)
29986+{
29987+ const char *pat;
f2c43d5f
AM
29988+ char fmt[32];
29989+ struct au_wbr_mfs *mfs;
1facf9fc 29990+
dece6358
AM
29991+ AuRwMustAnyLock(&sbinfo->si_rwsem);
29992+
c2b27bf2 29993+ seq_puts(m, ",create=");
1facf9fc 29994+ pat = au_optstr_wbr_create(v);
f2c43d5f 29995+ mfs = &sbinfo->si_wbr_mfs;
1facf9fc 29996+ switch (v) {
29997+ case AuWbrCreate_TDP:
29998+ case AuWbrCreate_RR:
29999+ case AuWbrCreate_MFS:
30000+ case AuWbrCreate_PMFS:
c2b27bf2 30001+ seq_puts(m, pat);
1facf9fc 30002+ break;
f2c43d5f
AM
30003+ case AuWbrCreate_MFSRR:
30004+ case AuWbrCreate_TDMFS:
30005+ case AuWbrCreate_PMFSRR:
30006+ au_gen_fmt(fmt, sizeof(fmt), pat, "%llu");
30007+ seq_printf(m, fmt, mfs->mfsrr_watermark);
1facf9fc 30008+ break;
f2c43d5f 30009+ case AuWbrCreate_MFSV:
1facf9fc 30010+ case AuWbrCreate_PMFSV:
f2c43d5f
AM
30011+ au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
30012+ seq_printf(m, fmt,
30013+ jiffies_to_msecs(mfs->mfs_expire)
e49829fe 30014+ / MSEC_PER_SEC);
1facf9fc 30015+ break;
1facf9fc 30016+ case AuWbrCreate_MFSRRV:
f2c43d5f 30017+ case AuWbrCreate_TDMFSV:
392086de 30018+ case AuWbrCreate_PMFSRRV:
f2c43d5f
AM
30019+ au_gen_fmt(fmt, sizeof(fmt), pat, "%llu:%lu");
30020+ seq_printf(m, fmt, mfs->mfsrr_watermark,
30021+ jiffies_to_msecs(mfs->mfs_expire) / MSEC_PER_SEC);
392086de 30022+ break;
f2c43d5f
AM
30023+ default:
30024+ BUG();
1facf9fc 30025+ }
30026+}
30027+
7eafdf33 30028+static int au_show_xino(struct seq_file *seq, struct super_block *sb)
1facf9fc 30029+{
30030+#ifdef CONFIG_SYSFS
30031+ return 0;
30032+#else
30033+ int err;
30034+ const int len = sizeof(AUFS_XINO_FNAME) - 1;
30035+ aufs_bindex_t bindex, brid;
1facf9fc 30036+ struct qstr *name;
30037+ struct file *f;
30038+ struct dentry *d, *h_root;
acd2b654 30039+ struct au_branch *br;
1facf9fc 30040+
dece6358
AM
30041+ AuRwMustAnyLock(&sbinfo->si_rwsem);
30042+
1facf9fc 30043+ err = 0;
1facf9fc 30044+ f = au_sbi(sb)->si_xib;
30045+ if (!f)
30046+ goto out;
30047+
30048+ /* stop printing the default xino path on the first writable branch */
30049+ h_root = NULL;
acd2b654
AM
30050+ bindex = au_xi_root(sb, f->f_path.dentry);
30051+ if (bindex >= 0) {
30052+ br = au_sbr_sb(sb, bindex);
30053+ h_root = au_br_dentry(br);
1facf9fc 30054+ }
acd2b654 30055+
2000de60 30056+ d = f->f_path.dentry;
1facf9fc 30057+ name = &d->d_name;
30058+ /* safe ->d_parent because the file is unlinked */
30059+ if (d->d_parent == h_root
30060+ && name->len == len
30061+ && !memcmp(name->name, AUFS_XINO_FNAME, len))
30062+ goto out;
30063+
30064+ seq_puts(seq, ",xino=");
30065+ err = au_xino_path(seq, f);
30066+
4f0767ce 30067+out:
1facf9fc 30068+ return err;
30069+#endif
30070+}
30071+
30072+/* seq_file will re-call me in case of too long string */
7eafdf33 30073+static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
1facf9fc 30074+{
027c5e7a 30075+ int err;
1facf9fc 30076+ unsigned int mnt_flags, v;
30077+ struct super_block *sb;
30078+ struct au_sbinfo *sbinfo;
30079+
30080+#define AuBool(name, str) do { \
30081+ v = au_opt_test(mnt_flags, name); \
30082+ if (v != au_opt_test(AuOpt_Def, name)) \
30083+ seq_printf(m, ",%s" #str, v ? "" : "no"); \
30084+} while (0)
30085+
30086+#define AuStr(name, str) do { \
30087+ v = mnt_flags & AuOptMask_##name; \
30088+ if (v != (AuOpt_Def & AuOptMask_##name)) \
30089+ seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
30090+} while (0)
30091+
30092+#define AuUInt(name, str, val) do { \
30093+ if (val != AUFS_##name##_DEF) \
30094+ seq_printf(m, "," #str "=%u", val); \
30095+} while (0)
30096+
7eafdf33 30097+ sb = dentry->d_sb;
2121bcd9 30098+ if (sb->s_flags & SB_POSIXACL)
c1595e42 30099+ seq_puts(m, ",acl");
43982f53 30100+#if 0 /* reserved for future use */
be118d29
JR
30101+ if (sb->s_flags & SB_I_VERSION)
30102+ seq_puts(m, ",i_version");
30103+#endif
c1595e42
JR
30104+
30105+ /* lock free root dinfo */
1facf9fc 30106+ si_noflush_read_lock(sb);
30107+ sbinfo = au_sbi(sb);
30108+ seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
30109+
30110+ mnt_flags = au_mntflags(sb);
30111+ if (au_opt_test(mnt_flags, XINO)) {
7eafdf33 30112+ err = au_show_xino(m, sb);
1facf9fc 30113+ if (unlikely(err))
30114+ goto out;
30115+ } else
30116+ seq_puts(m, ",noxino");
30117+
30118+ AuBool(TRUNC_XINO, trunc_xino);
30119+ AuStr(UDBA, udba);
dece6358 30120+ AuBool(SHWH, shwh);
1facf9fc 30121+ AuBool(PLINK, plink);
4a4d8108 30122+ AuBool(DIO, dio);
076b876e 30123+ AuBool(DIRPERM1, dirperm1);
1facf9fc 30124+
30125+ v = sbinfo->si_wbr_create;
30126+ if (v != AuWbrCreate_Def)
30127+ au_show_wbr_create(m, v, sbinfo);
30128+
30129+ v = sbinfo->si_wbr_copyup;
30130+ if (v != AuWbrCopyup_Def)
30131+ seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
30132+
30133+ v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
30134+ if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
30135+ seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
30136+
30137+ AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
30138+
027c5e7a
AM
30139+ v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
30140+ AuUInt(RDCACHE, rdcache, v);
1facf9fc 30141+
30142+ AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
30143+ AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
30144+
076b876e
AM
30145+ au_fhsm_show(m, sbinfo);
30146+
8b6a4947 30147+ AuBool(DIRREN, dirren);
1facf9fc 30148+ AuBool(SUM, sum);
30149+ /* AuBool(SUM_W, wsum); */
30150+ AuBool(WARN_PERM, warn_perm);
30151+ AuBool(VERBOSE, verbose);
30152+
4f0767ce 30153+out:
1facf9fc 30154+ /* be sure to print "br:" last */
30155+ if (!sysaufs_brs) {
30156+ seq_puts(m, ",br:");
30157+ au_show_brs(m, sb);
30158+ }
30159+ si_read_unlock(sb);
30160+ return 0;
30161+
1facf9fc 30162+#undef AuBool
30163+#undef AuStr
4a4d8108 30164+#undef AuUInt
1facf9fc 30165+}
30166+
30167+/* ---------------------------------------------------------------------- */
30168+
30169+/* sum mode which returns the summation for statfs(2) */
30170+
30171+static u64 au_add_till_max(u64 a, u64 b)
30172+{
30173+ u64 old;
30174+
30175+ old = a;
30176+ a += b;
92d182d2
AM
30177+ if (old <= a)
30178+ return a;
30179+ return ULLONG_MAX;
30180+}
30181+
30182+static u64 au_mul_till_max(u64 a, long mul)
30183+{
30184+ u64 old;
30185+
30186+ old = a;
30187+ a *= mul;
30188+ if (old <= a)
1facf9fc 30189+ return a;
30190+ return ULLONG_MAX;
30191+}
30192+
30193+static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
30194+{
30195+ int err;
92d182d2 30196+ long bsize, factor;
1facf9fc 30197+ u64 blocks, bfree, bavail, files, ffree;
5afbbe0d 30198+ aufs_bindex_t bbot, bindex, i;
1facf9fc 30199+ unsigned char shared;
7f207e10 30200+ struct path h_path;
1facf9fc 30201+ struct super_block *h_sb;
30202+
92d182d2
AM
30203+ err = 0;
30204+ bsize = LONG_MAX;
30205+ files = 0;
30206+ ffree = 0;
1facf9fc 30207+ blocks = 0;
30208+ bfree = 0;
30209+ bavail = 0;
5afbbe0d
AM
30210+ bbot = au_sbbot(sb);
30211+ for (bindex = 0; bindex <= bbot; bindex++) {
7f207e10
AM
30212+ h_path.mnt = au_sbr_mnt(sb, bindex);
30213+ h_sb = h_path.mnt->mnt_sb;
1facf9fc 30214+ shared = 0;
92d182d2 30215+ for (i = 0; !shared && i < bindex; i++)
1facf9fc 30216+ shared = (au_sbr_sb(sb, i) == h_sb);
30217+ if (shared)
30218+ continue;
30219+
30220+ /* sb->s_root for NFS is unreliable */
7f207e10
AM
30221+ h_path.dentry = h_path.mnt->mnt_root;
30222+ err = vfs_statfs(&h_path, buf);
1facf9fc 30223+ if (unlikely(err))
30224+ goto out;
30225+
92d182d2
AM
30226+ if (bsize > buf->f_bsize) {
30227+ /*
30228+ * we will reduce bsize, so we have to expand blocks
30229+ * etc. to match them again
30230+ */
30231+ factor = (bsize / buf->f_bsize);
30232+ blocks = au_mul_till_max(blocks, factor);
30233+ bfree = au_mul_till_max(bfree, factor);
30234+ bavail = au_mul_till_max(bavail, factor);
30235+ bsize = buf->f_bsize;
30236+ }
30237+
30238+ factor = (buf->f_bsize / bsize);
30239+ blocks = au_add_till_max(blocks,
30240+ au_mul_till_max(buf->f_blocks, factor));
30241+ bfree = au_add_till_max(bfree,
30242+ au_mul_till_max(buf->f_bfree, factor));
30243+ bavail = au_add_till_max(bavail,
30244+ au_mul_till_max(buf->f_bavail, factor));
1facf9fc 30245+ files = au_add_till_max(files, buf->f_files);
30246+ ffree = au_add_till_max(ffree, buf->f_ffree);
30247+ }
30248+
92d182d2 30249+ buf->f_bsize = bsize;
1facf9fc 30250+ buf->f_blocks = blocks;
30251+ buf->f_bfree = bfree;
30252+ buf->f_bavail = bavail;
30253+ buf->f_files = files;
30254+ buf->f_ffree = ffree;
92d182d2 30255+ buf->f_frsize = 0;
1facf9fc 30256+
4f0767ce 30257+out:
1facf9fc 30258+ return err;
30259+}
30260+
30261+static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
30262+{
30263+ int err;
7f207e10 30264+ struct path h_path;
1facf9fc 30265+ struct super_block *sb;
30266+
30267+ /* lock free root dinfo */
30268+ sb = dentry->d_sb;
30269+ si_noflush_read_lock(sb);
7f207e10 30270+ if (!au_opt_test(au_mntflags(sb), SUM)) {
1facf9fc 30271+ /* sb->s_root for NFS is unreliable */
7f207e10
AM
30272+ h_path.mnt = au_sbr_mnt(sb, 0);
30273+ h_path.dentry = h_path.mnt->mnt_root;
30274+ err = vfs_statfs(&h_path, buf);
30275+ } else
1facf9fc 30276+ err = au_statfs_sum(sb, buf);
30277+ si_read_unlock(sb);
30278+
30279+ if (!err) {
30280+ buf->f_type = AUFS_SUPER_MAGIC;
4a4d8108 30281+ buf->f_namelen = AUFS_MAX_NAMELEN;
1facf9fc 30282+ memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
30283+ }
30284+ /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
30285+
30286+ return err;
30287+}
30288+
30289+/* ---------------------------------------------------------------------- */
30290+
537831f9
AM
30291+static int aufs_sync_fs(struct super_block *sb, int wait)
30292+{
30293+ int err, e;
5afbbe0d 30294+ aufs_bindex_t bbot, bindex;
537831f9
AM
30295+ struct au_branch *br;
30296+ struct super_block *h_sb;
30297+
30298+ err = 0;
30299+ si_noflush_read_lock(sb);
5afbbe0d
AM
30300+ bbot = au_sbbot(sb);
30301+ for (bindex = 0; bindex <= bbot; bindex++) {
537831f9
AM
30302+ br = au_sbr(sb, bindex);
30303+ if (!au_br_writable(br->br_perm))
30304+ continue;
30305+
30306+ h_sb = au_sbr_sb(sb, bindex);
cd233405 30307+ e = vfsub_sync_filesystem(h_sb);
a2654f78
AM
30308+ if (unlikely(e && !err))
30309+ err = e;
30310+ /* go on even if an error happens */
537831f9
AM
30311+ }
30312+ si_read_unlock(sb);
30313+
30314+ return err;
30315+}
30316+
30317+/* ---------------------------------------------------------------------- */
30318+
1facf9fc 30319+/* final actions when unmounting a file system */
30320+static void aufs_put_super(struct super_block *sb)
30321+{
30322+ struct au_sbinfo *sbinfo;
30323+
30324+ sbinfo = au_sbi(sb);
062440b3
AM
30325+ if (sbinfo)
30326+ kobject_put(&sbinfo->si_kobj);
1facf9fc 30327+}
30328+
30329+/* ---------------------------------------------------------------------- */
30330+
79b8bda9
AM
30331+void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30332+ struct super_block *sb, void *arg)
7f207e10
AM
30333+{
30334+ void *array;
076b876e 30335+ unsigned long long n, sz;
7f207e10
AM
30336+
30337+ array = NULL;
30338+ n = 0;
30339+ if (!*hint)
30340+ goto out;
30341+
30342+ if (*hint > ULLONG_MAX / sizeof(array)) {
30343+ array = ERR_PTR(-EMFILE);
30344+ pr_err("hint %llu\n", *hint);
30345+ goto out;
30346+ }
30347+
076b876e
AM
30348+ sz = sizeof(array) * *hint;
30349+ array = kzalloc(sz, GFP_NOFS);
7f207e10 30350+ if (unlikely(!array))
076b876e 30351+ array = vzalloc(sz);
7f207e10
AM
30352+ if (unlikely(!array)) {
30353+ array = ERR_PTR(-ENOMEM);
30354+ goto out;
30355+ }
30356+
79b8bda9 30357+ n = cb(sb, array, *hint, arg);
7f207e10
AM
30358+ AuDebugOn(n > *hint);
30359+
30360+out:
30361+ *hint = n;
30362+ return array;
30363+}
30364+
79b8bda9 30365+static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
7f207e10
AM
30366+ unsigned long long max __maybe_unused,
30367+ void *arg)
30368+{
30369+ unsigned long long n;
30370+ struct inode **p, *inode;
30371+ struct list_head *head;
30372+
30373+ n = 0;
30374+ p = a;
30375+ head = arg;
79b8bda9 30376+ spin_lock(&sb->s_inode_list_lock);
7f207e10 30377+ list_for_each_entry(inode, head, i_sb_list) {
5afbbe0d
AM
30378+ if (!au_is_bad_inode(inode)
30379+ && au_ii(inode)->ii_btop >= 0) {
2cbb1c4b
JR
30380+ spin_lock(&inode->i_lock);
30381+ if (atomic_read(&inode->i_count)) {
30382+ au_igrab(inode);
30383+ *p++ = inode;
30384+ n++;
30385+ AuDebugOn(n > max);
30386+ }
30387+ spin_unlock(&inode->i_lock);
7f207e10
AM
30388+ }
30389+ }
79b8bda9 30390+ spin_unlock(&sb->s_inode_list_lock);
7f207e10
AM
30391+
30392+ return n;
30393+}
30394+
30395+struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
30396+{
acd2b654
AM
30397+ struct au_sbinfo *sbi;
30398+
30399+ sbi = au_sbi(sb);
30400+ *max = au_lcnt_read(&sbi->si_ninodes, /*do_rev*/1);
79b8bda9 30401+ return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
7f207e10
AM
30402+}
30403+
30404+void au_iarray_free(struct inode **a, unsigned long long max)
30405+{
30406+ unsigned long long ull;
30407+
30408+ for (ull = 0; ull < max; ull++)
30409+ iput(a[ull]);
be52b249 30410+ kvfree(a);
7f207e10
AM
30411+}
30412+
30413+/* ---------------------------------------------------------------------- */
30414+
1facf9fc 30415+/*
30416+ * refresh dentry and inode at remount time.
30417+ */
027c5e7a
AM
30418+/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
30419+static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
30420+ struct dentry *parent)
1facf9fc 30421+{
30422+ int err;
1facf9fc 30423+
30424+ di_write_lock_child(dentry);
1facf9fc 30425+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a
AM
30426+ err = au_refresh_dentry(dentry, parent);
30427+ if (!err && dir_flags)
5527c038 30428+ au_hn_reset(d_inode(dentry), dir_flags);
1facf9fc 30429+ di_read_unlock(parent, AuLock_IR);
1facf9fc 30430+ di_write_unlock(dentry);
30431+
30432+ return err;
30433+}
30434+
027c5e7a
AM
30435+static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
30436+ struct au_sbinfo *sbinfo,
b95c5147 30437+ const unsigned int dir_flags, unsigned int do_idop)
1facf9fc 30438+{
027c5e7a
AM
30439+ int err;
30440+ struct dentry *parent;
027c5e7a
AM
30441+
30442+ err = 0;
30443+ parent = dget_parent(dentry);
30444+ if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
5527c038
JR
30445+ if (d_really_is_positive(dentry)) {
30446+ if (!d_is_dir(dentry))
027c5e7a
AM
30447+ err = au_do_refresh(dentry, /*dir_flags*/0,
30448+ parent);
30449+ else {
30450+ err = au_do_refresh(dentry, dir_flags, parent);
30451+ if (unlikely(err))
30452+ au_fset_si(sbinfo, FAILED_REFRESH_DIR);
30453+ }
30454+ } else
30455+ err = au_do_refresh(dentry, /*dir_flags*/0, parent);
30456+ AuDbgDentry(dentry);
30457+ }
30458+ dput(parent);
30459+
79b8bda9 30460+ if (!err) {
b95c5147 30461+ if (do_idop)
79b8bda9
AM
30462+ au_refresh_dop(dentry, /*force_reval*/0);
30463+ } else
30464+ au_refresh_dop(dentry, /*force_reval*/1);
30465+
027c5e7a
AM
30466+ AuTraceErr(err);
30467+ return err;
1facf9fc 30468+}
30469+
b95c5147 30470+static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
1facf9fc 30471+{
30472+ int err, i, j, ndentry, e;
027c5e7a 30473+ unsigned int sigen;
1facf9fc 30474+ struct au_dcsub_pages dpages;
30475+ struct au_dpage *dpage;
027c5e7a
AM
30476+ struct dentry **dentries, *d;
30477+ struct au_sbinfo *sbinfo;
30478+ struct dentry *root = sb->s_root;
5527c038 30479+ const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
1facf9fc 30480+
b95c5147 30481+ if (do_idop)
79b8bda9
AM
30482+ au_refresh_dop(root, /*force_reval*/0);
30483+
027c5e7a
AM
30484+ err = au_dpages_init(&dpages, GFP_NOFS);
30485+ if (unlikely(err))
1facf9fc 30486+ goto out;
027c5e7a
AM
30487+ err = au_dcsub_pages(&dpages, root, NULL, NULL);
30488+ if (unlikely(err))
1facf9fc 30489+ goto out_dpages;
1facf9fc 30490+
027c5e7a
AM
30491+ sigen = au_sigen(sb);
30492+ sbinfo = au_sbi(sb);
30493+ for (i = 0; i < dpages.ndpage; i++) {
1facf9fc 30494+ dpage = dpages.dpages + i;
30495+ dentries = dpage->dentries;
30496+ ndentry = dpage->ndentry;
027c5e7a 30497+ for (j = 0; j < ndentry; j++) {
1facf9fc 30498+ d = dentries[j];
79b8bda9 30499+ e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
b95c5147 30500+ do_idop);
027c5e7a
AM
30501+ if (unlikely(e && !err))
30502+ err = e;
30503+ /* go on even err */
1facf9fc 30504+ }
30505+ }
30506+
4f0767ce 30507+out_dpages:
1facf9fc 30508+ au_dpages_free(&dpages);
4f0767ce 30509+out:
1facf9fc 30510+ return err;
30511+}
30512+
b95c5147 30513+static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
1facf9fc 30514+{
027c5e7a
AM
30515+ int err, e;
30516+ unsigned int sigen;
30517+ unsigned long long max, ull;
30518+ struct inode *inode, **array;
1facf9fc 30519+
027c5e7a
AM
30520+ array = au_iarray_alloc(sb, &max);
30521+ err = PTR_ERR(array);
30522+ if (IS_ERR(array))
30523+ goto out;
1facf9fc 30524+
30525+ err = 0;
027c5e7a
AM
30526+ sigen = au_sigen(sb);
30527+ for (ull = 0; ull < max; ull++) {
30528+ inode = array[ull];
076b876e
AM
30529+ if (unlikely(!inode))
30530+ break;
b95c5147
AM
30531+
30532+ e = 0;
30533+ ii_write_lock_child(inode);
537831f9 30534+ if (au_iigen(inode, NULL) != sigen) {
027c5e7a 30535+ e = au_refresh_hinode_self(inode);
1facf9fc 30536+ if (unlikely(e)) {
b95c5147 30537+ au_refresh_iop(inode, /*force_getattr*/1);
027c5e7a 30538+ pr_err("error %d, i%lu\n", e, inode->i_ino);
1facf9fc 30539+ if (!err)
30540+ err = e;
30541+ /* go on even if err */
30542+ }
30543+ }
b95c5147
AM
30544+ if (!e && do_idop)
30545+ au_refresh_iop(inode, /*force_getattr*/0);
30546+ ii_write_unlock(inode);
1facf9fc 30547+ }
30548+
027c5e7a 30549+ au_iarray_free(array, max);
1facf9fc 30550+
4f0767ce 30551+out:
1facf9fc 30552+ return err;
30553+}
30554+
f4d37d76 30555+void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
1facf9fc 30556+{
027c5e7a
AM
30557+ int err, e;
30558+ unsigned int udba;
5afbbe0d 30559+ aufs_bindex_t bindex, bbot;
1facf9fc 30560+ struct dentry *root;
30561+ struct inode *inode;
027c5e7a 30562+ struct au_branch *br;
79b8bda9 30563+ struct au_sbinfo *sbi;
1facf9fc 30564+
30565+ au_sigen_inc(sb);
79b8bda9
AM
30566+ sbi = au_sbi(sb);
30567+ au_fclr_si(sbi, FAILED_REFRESH_DIR);
1facf9fc 30568+
30569+ root = sb->s_root;
30570+ DiMustNoWaiters(root);
5527c038 30571+ inode = d_inode(root);
1facf9fc 30572+ IiMustNoWaiters(inode);
1facf9fc 30573+
027c5e7a 30574+ udba = au_opt_udba(sb);
5afbbe0d
AM
30575+ bbot = au_sbbot(sb);
30576+ for (bindex = 0; bindex <= bbot; bindex++) {
027c5e7a
AM
30577+ br = au_sbr(sb, bindex);
30578+ err = au_hnotify_reset_br(udba, br, br->br_perm);
1facf9fc 30579+ if (unlikely(err))
027c5e7a
AM
30580+ AuIOErr("hnotify failed on br %d, %d, ignored\n",
30581+ bindex, err);
30582+ /* go on even if err */
1facf9fc 30583+ }
027c5e7a 30584+ au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
1facf9fc 30585+
b95c5147 30586+ if (do_idop) {
79b8bda9
AM
30587+ if (au_ftest_si(sbi, NO_DREVAL)) {
30588+ AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
30589+ sb->s_d_op = &aufs_dop_noreval;
b95c5147
AM
30590+ AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
30591+ sbi->si_iop_array = aufs_iop_nogetattr;
79b8bda9
AM
30592+ } else {
30593+ AuDebugOn(sb->s_d_op == &aufs_dop);
30594+ sb->s_d_op = &aufs_dop;
b95c5147
AM
30595+ AuDebugOn(sbi->si_iop_array == aufs_iop);
30596+ sbi->si_iop_array = aufs_iop;
79b8bda9 30597+ }
062440b3 30598+ pr_info("reset to %ps and %ps\n",
b95c5147 30599+ sb->s_d_op, sbi->si_iop_array);
79b8bda9
AM
30600+ }
30601+
027c5e7a 30602+ di_write_unlock(root);
b95c5147
AM
30603+ err = au_refresh_d(sb, do_idop);
30604+ e = au_refresh_i(sb, do_idop);
027c5e7a
AM
30605+ if (unlikely(e && !err))
30606+ err = e;
1facf9fc 30607+ /* aufs_write_lock() calls ..._child() */
30608+ di_write_lock_child(root);
027c5e7a
AM
30609+
30610+ au_cpup_attr_all(inode, /*force*/1);
30611+
30612+ if (unlikely(err))
30613+ AuIOErr("refresh failed, ignored, %d\n", err);
1facf9fc 30614+}
30615+
f4d37d76 30616+const struct super_operations aufs_sop = {
1facf9fc 30617+ .alloc_inode = aufs_alloc_inode,
30618+ .destroy_inode = aufs_destroy_inode,
fbc438ed 30619+ .free_inode = aufs_free_inode,
b752ccd1 30620+ /* always deleting, no clearing */
1facf9fc 30621+ .drop_inode = generic_delete_inode,
30622+ .show_options = aufs_show_options,
30623+ .statfs = aufs_statfs,
30624+ .put_super = aufs_put_super,
f4d37d76 30625+ .sync_fs = aufs_sync_fs
1facf9fc 30626+};
30627+
30628+/* ---------------------------------------------------------------------- */
30629+
f4d37d76 30630+int au_alloc_root(struct super_block *sb)
1facf9fc 30631+{
30632+ int err;
30633+ struct inode *inode;
30634+ struct dentry *root;
30635+
30636+ err = -ENOMEM;
30637+ inode = au_iget_locked(sb, AUFS_ROOT_INO);
30638+ err = PTR_ERR(inode);
30639+ if (IS_ERR(inode))
30640+ goto out;
30641+
b95c5147 30642+ inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
1facf9fc 30643+ inode->i_fop = &aufs_dir_fop;
30644+ inode->i_mode = S_IFDIR;
9dbd164d 30645+ set_nlink(inode, 2);
1facf9fc 30646+ unlock_new_inode(inode);
30647+
92d182d2 30648+ root = d_make_root(inode);
1facf9fc 30649+ if (unlikely(!root))
92d182d2 30650+ goto out;
1facf9fc 30651+ err = PTR_ERR(root);
30652+ if (IS_ERR(root))
92d182d2 30653+ goto out;
1facf9fc 30654+
4a4d8108 30655+ err = au_di_init(root);
1facf9fc 30656+ if (!err) {
30657+ sb->s_root = root;
30658+ return 0; /* success */
30659+ }
30660+ dput(root);
1facf9fc 30661+
4f0767ce 30662+out:
1facf9fc 30663+ return err;
1facf9fc 30664+}
30665+
f4d37d76
AM
30666+/* ---------------------------------------------------------------------- */
30667+
30668+static void aufs_kill_sb(struct super_block *sb)
1facf9fc 30669+{
79b8bda9 30670+ struct au_sbinfo *sbinfo;
1facf9fc 30671+ struct dentry *root;
1facf9fc 30672+
79b8bda9 30673+ sbinfo = au_sbi(sb);
f4d37d76
AM
30674+ if (!sbinfo)
30675+ goto out;
1facf9fc 30676+
f4d37d76 30677+ au_sbilist_del(sb);
1facf9fc 30678+
1facf9fc 30679+ root = sb->s_root;
f4d37d76
AM
30680+ if (root)
30681+ aufs_write_lock(root);
30682+ else
30683+ __si_write_lock(sb);
1facf9fc 30684+
f4d37d76
AM
30685+ au_fhsm_fin(sb);
30686+ if (sbinfo->si_wbr_create_ops->fin)
30687+ sbinfo->si_wbr_create_ops->fin(sb);
30688+ if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
30689+ au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
30690+ au_remount_refresh(sb, /*do_idop*/0);
79b8bda9 30691+ }
f4d37d76
AM
30692+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
30693+ au_plink_put(sb, /*verbose*/1);
30694+ au_xino_clr(sb);
30695+ if (root)
30696+ au_dr_opt_flush(sb);
1facf9fc 30697+
f4d37d76
AM
30698+ if (root)
30699+ aufs_write_unlock(root);
30700+ else
30701+ __si_write_unlock(sb);
027c5e7a 30702+
f4d37d76
AM
30703+ sbinfo->si_sb = NULL;
30704+ au_nwt_flush(&sbinfo->si_nowait);
027c5e7a
AM
30705+
30706+out:
98d9a5b1 30707+ kill_anon_super(sb);
e49829fe
JR
30708+}
30709+
1facf9fc 30710+struct file_system_type aufs_fs_type = {
30711+ .name = AUFS_FSTYPE,
c06a8ce3 30712+ /* a race between rename and others */
0b2a12c6
JR
30713+ .fs_flags = FS_RENAME_DOES_D_MOVE
30714+ /* untested */
30715+ /*| FS_ALLOW_IDMAP*/
30716+ ,
f4d37d76
AM
30717+ .init_fs_context = aufs_fsctx_init,
30718+ .parameters = aufs_fsctx_paramspec,
e49829fe 30719+ .kill_sb = aufs_kill_sb,
1facf9fc 30720+ /* no need to __module_get() and module_put(). */
30721+ .owner = THIS_MODULE,
30722+};
7f207e10 30723diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
eca34b5c 30724--- /usr/share/empty/fs/aufs/super.h 1970-01-01 01:00:00.000000000 +0100
594d0238 30725+++ linux/fs/aufs/super.h 2022-11-05 23:02:18.969222617 +0100
f4d37d76 30726@@ -0,0 +1,592 @@
062440b3 30727+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 30728+/*
0dcfbb52 30729+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 30730+ *
0dcfbb52 30731+ * This program is free software; you can redistribute it and/or modify
1facf9fc 30732+ * it under the terms of the GNU General Public License as published by
30733+ * the Free Software Foundation; either version 2 of the License, or
30734+ * (at your option) any later version.
dece6358
AM
30735+ *
30736+ * This program is distributed in the hope that it will be useful,
30737+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
30738+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30739+ * GNU General Public License for more details.
30740+ *
30741+ * You should have received a copy of the GNU General Public License
523b37e3 30742+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 30743+ */
30744+
30745+/*
30746+ * super_block operations
30747+ */
30748+
30749+#ifndef __AUFS_SUPER_H__
30750+#define __AUFS_SUPER_H__
30751+
30752+#ifdef __KERNEL__
30753+
30754+#include <linux/fs.h>
5527c038 30755+#include <linux/kobject.h>
8b6a4947 30756+#include "hbl.h"
acd2b654 30757+#include "lcnt.h"
1facf9fc 30758+#include "rwsem.h"
1facf9fc 30759+#include "wkq.h"
30760+
1facf9fc 30761+/* policies to select one among multiple writable branches */
30762+struct au_wbr_copyup_operations {
30763+ int (*copyup)(struct dentry *dentry);
30764+};
30765+
392086de
AM
30766+#define AuWbr_DIR 1 /* target is a dir */
30767+#define AuWbr_PARENT (1 << 1) /* always require a parent */
30768+
30769+#define au_ftest_wbr(flags, name) ((flags) & AuWbr_##name)
30770+#define au_fset_wbr(flags, name) { (flags) |= AuWbr_##name; }
30771+#define au_fclr_wbr(flags, name) { (flags) &= ~AuWbr_##name; }
30772+
1facf9fc 30773+struct au_wbr_create_operations {
392086de 30774+ int (*create)(struct dentry *dentry, unsigned int flags);
1facf9fc 30775+ int (*init)(struct super_block *sb);
30776+ int (*fin)(struct super_block *sb);
30777+};
30778+
30779+struct au_wbr_mfs {
30780+ struct mutex mfs_lock; /* protect this structure */
30781+ unsigned long mfs_jiffy;
30782+ unsigned long mfs_expire;
30783+ aufs_bindex_t mfs_bindex;
30784+
30785+ unsigned long long mfsrr_bytes;
30786+ unsigned long long mfsrr_watermark;
30787+};
30788+
86dc4139
AM
30789+#define AuPlink_NHASH 100
30790+static inline int au_plink_hash(ino_t ino)
30791+{
30792+ return ino % AuPlink_NHASH;
30793+}
30794+
076b876e
AM
30795+/* File-based Hierarchical Storage Management */
30796+struct au_fhsm {
30797+#ifdef CONFIG_AUFS_FHSM
30798+ /* allow only one process who can receive the notification */
30799+ spinlock_t fhsm_spin;
30800+ pid_t fhsm_pid;
30801+ wait_queue_head_t fhsm_wqh;
30802+ atomic_t fhsm_readable;
30803+
c1595e42 30804+ /* these are protected by si_rwsem */
076b876e 30805+ unsigned long fhsm_expire;
c1595e42 30806+ aufs_bindex_t fhsm_bottom;
076b876e
AM
30807+#endif
30808+};
30809+
1facf9fc 30810+struct au_branch;
30811+struct au_sbinfo {
30812+ /* nowait tasks in the system-wide workqueue */
30813+ struct au_nowait_tasks si_nowait;
30814+
b752ccd1 30815+ /*
acd2b654 30816+ * tried sb->s_umount, but failed due to the dependency between i_mutex.
b752ccd1
AM
30817+ * rwsem for au_sbinfo is necessary.
30818+ */
dece6358 30819+ struct au_rwsem si_rwsem;
1facf9fc 30820+
7f207e10 30821+ /*
523b37e3
AM
30822+ * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
30823+ * remount.
7f207e10 30824+ */
acd2b654 30825+ au_lcnt_t si_ninodes, si_nfiles;
7f207e10 30826+
1facf9fc 30827+ /* branch management */
30828+ unsigned int si_generation;
30829+
2000de60 30830+ /* see AuSi_ flags */
1facf9fc 30831+ unsigned char au_si_status;
30832+
5afbbe0d 30833+ aufs_bindex_t si_bbot;
7f207e10
AM
30834+
30835+ /* dirty trick to keep br_id plus */
30836+ unsigned int si_last_br_id :
30837+ sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
1facf9fc 30838+ struct au_branch **si_branch;
30839+
30840+ /* policy to select a writable branch */
30841+ unsigned char si_wbr_copyup;
30842+ unsigned char si_wbr_create;
30843+ struct au_wbr_copyup_operations *si_wbr_copyup_ops;
30844+ struct au_wbr_create_operations *si_wbr_create_ops;
30845+
30846+ /* round robin */
30847+ atomic_t si_wbr_rr_next;
30848+
30849+ /* most free space */
30850+ struct au_wbr_mfs si_wbr_mfs;
30851+
076b876e
AM
30852+ /* File-based Hierarchical Storage Management */
30853+ struct au_fhsm si_fhsm;
30854+
1facf9fc 30855+ /* mount flags */
30856+ /* include/asm-ia64/siginfo.h defines a macro named si_flags */
30857+ unsigned int si_mntflags;
30858+
30859+ /* external inode number (bitmap and translation table) */
acd2b654
AM
30860+ loff_t si_ximaxent; /* max entries in a xino */
30861+
1facf9fc 30862+ struct file *si_xib;
30863+ struct mutex si_xib_mtx; /* protect xib members */
30864+ unsigned long *si_xib_buf;
30865+ unsigned long si_xib_last_pindex;
30866+ int si_xib_next_bit;
acd2b654 30867+
392086de
AM
30868+ unsigned long si_xino_jiffy;
30869+ unsigned long si_xino_expire;
1facf9fc 30870+ /* reserved for future use */
30871+ /* unsigned long long si_xib_limit; */ /* Max xib file size */
30872+
30873+#ifdef CONFIG_AUFS_EXPORT
30874+ /* i_generation */
acd2b654 30875+ /* todo: make xigen file an array to support many inode numbers */
1facf9fc 30876+ struct file *si_xigen;
30877+ atomic_t si_xigen_next;
30878+#endif
30879+
acd2b654 30880+ /* dirty trick to support atomic_open */
8b6a4947 30881+ struct hlist_bl_head si_aopen;
b912730e 30882+
1facf9fc 30883+ /* vdir parameters */
e49829fe 30884+ unsigned long si_rdcache; /* max cache time in jiffies */
1facf9fc 30885+ unsigned int si_rdblk; /* deblk size */
30886+ unsigned int si_rdhash; /* hash size */
30887+
30888+ /*
30889+ * If the number of whiteouts are larger than si_dirwh, leave all of
30890+ * them after au_whtmp_ren to reduce the cost of rmdir(2).
30891+ * future fsck.aufs or kernel thread will remove them later.
30892+ * Otherwise, remove all whiteouts and the dir in rmdir(2).
30893+ */
30894+ unsigned int si_dirwh;
30895+
1facf9fc 30896+ /* pseudo_link list */
8b6a4947 30897+ struct hlist_bl_head si_plink[AuPlink_NHASH];
1facf9fc 30898+ wait_queue_head_t si_plink_wq;
4a4d8108 30899+ spinlock_t si_plink_maint_lock;
e49829fe 30900+ pid_t si_plink_maint_pid;
1facf9fc 30901+
523b37e3 30902+ /* file list */
8b6a4947 30903+ struct hlist_bl_head si_files;
523b37e3 30904+
b95c5147 30905+ /* with/without getattr, brother of sb->s_d_op */
43982f53 30906+ const struct inode_operations *si_iop_array;
b95c5147 30907+
1facf9fc 30908+ /*
30909+ * sysfs and lifetime management.
30910+ * this is not a small structure and it may be a waste of memory in case
acd2b654 30911+ * of sysfs is disabled, particularly when many aufs-es are mounted.
1facf9fc 30912+ * but using sysfs is majority.
30913+ */
30914+ struct kobject si_kobj;
30915+#ifdef CONFIG_DEBUG_FS
86dc4139
AM
30916+ struct dentry *si_dbgaufs;
30917+ struct dentry *si_dbgaufs_plink;
30918+ struct dentry *si_dbgaufs_xib;
1facf9fc 30919+#ifdef CONFIG_AUFS_EXPORT
30920+ struct dentry *si_dbgaufs_xigen;
30921+#endif
30922+#endif
30923+
e49829fe 30924+#ifdef CONFIG_AUFS_SBILIST
8b6a4947 30925+ struct hlist_bl_node si_list;
e49829fe
JR
30926+#endif
30927+
1facf9fc 30928+ /* dirty, necessary for unmounting, sysfs and sysrq */
30929+ struct super_block *si_sb;
30930+};
30931+
dece6358
AM
30932+/* sbinfo status flags */
30933+/*
30934+ * set true when refresh_dirs() failed at remount time.
30935+ * then try refreshing dirs at access time again.
062440b3 30936+ * if it is false, refreshing dirs at access time is unnecessary
dece6358 30937+ */
027c5e7a 30938+#define AuSi_FAILED_REFRESH_DIR 1
076b876e 30939+#define AuSi_FHSM (1 << 1) /* fhsm is active now */
79b8bda9 30940+#define AuSi_NO_DREVAL (1 << 2) /* disable all d_revalidate */
076b876e
AM
30941+
30942+#ifndef CONFIG_AUFS_FHSM
30943+#undef AuSi_FHSM
30944+#define AuSi_FHSM 0
30945+#endif
30946+
dece6358
AM
30947+static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
30948+ unsigned int flag)
30949+{
30950+ AuRwMustAnyLock(&sbi->si_rwsem);
30951+ return sbi->au_si_status & flag;
30952+}
30953+#define au_ftest_si(sbinfo, name) au_do_ftest_si(sbinfo, AuSi_##name)
30954+#define au_fset_si(sbinfo, name) do { \
30955+ AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30956+ (sbinfo)->au_si_status |= AuSi_##name; \
30957+} while (0)
30958+#define au_fclr_si(sbinfo, name) do { \
30959+ AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30960+ (sbinfo)->au_si_status &= ~AuSi_##name; \
30961+} while (0)
30962+
1facf9fc 30963+/* ---------------------------------------------------------------------- */
30964+
30965+/* policy to select one among writable branches */
4a4d8108
AM
30966+#define AuWbrCopyup(sbinfo, ...) \
30967+ ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
30968+#define AuWbrCreate(sbinfo, ...) \
30969+ ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
1facf9fc 30970+
30971+/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
30972+#define AuLock_DW 1 /* write-lock dentry */
30973+#define AuLock_IR (1 << 1) /* read-lock inode */
30974+#define AuLock_IW (1 << 2) /* write-lock inode */
30975+#define AuLock_FLUSH (1 << 3) /* wait for 'nowait' tasks */
b95c5147 30976+#define AuLock_DIRS (1 << 4) /* target is a pair of dirs */
f2c43d5f 30977+ /* except RENAME_EXCHANGE */
e49829fe
JR
30978+#define AuLock_NOPLM (1 << 5) /* return err in plm mode */
30979+#define AuLock_NOPLMW (1 << 6) /* wait for plm mode ends */
027c5e7a 30980+#define AuLock_GEN (1 << 7) /* test digen/iigen */
1facf9fc 30981+#define au_ftest_lock(flags, name) ((flags) & AuLock_##name)
7f207e10
AM
30982+#define au_fset_lock(flags, name) \
30983+ do { (flags) |= AuLock_##name; } while (0)
30984+#define au_fclr_lock(flags, name) \
30985+ do { (flags) &= ~AuLock_##name; } while (0)
1facf9fc 30986+
30987+/* ---------------------------------------------------------------------- */
30988+
30989+/* super.c */
1facf9fc 30990+struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
f4d37d76 30991+
79b8bda9
AM
30992+typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
30993+ unsigned long long max, void *arg);
79b8bda9
AM
30994+void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30995+ struct super_block *sb, void *arg);
7f207e10
AM
30996+struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
30997+void au_iarray_free(struct inode **a, unsigned long long max);
1facf9fc 30998+
f4d37d76
AM
30999+void au_remount_refresh(struct super_block *sb, unsigned int do_idop);
31000+extern const struct super_operations aufs_sop;
31001+int au_alloc_root(struct super_block *sb);
31002+extern struct file_system_type aufs_fs_type;
31003+
1facf9fc 31004+/* sbinfo.c */
31005+void au_si_free(struct kobject *kobj);
f4d37d76 31006+struct au_sbinfo *au_si_alloc(struct super_block *sb);
e2f27e51 31007+int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
1facf9fc 31008+
31009+unsigned int au_sigen_inc(struct super_block *sb);
31010+aufs_bindex_t au_new_br_id(struct super_block *sb);
31011+
e49829fe
JR
31012+int si_read_lock(struct super_block *sb, int flags);
31013+int si_write_lock(struct super_block *sb, int flags);
31014+int aufs_read_lock(struct dentry *dentry, int flags);
1facf9fc 31015+void aufs_read_unlock(struct dentry *dentry, int flags);
31016+void aufs_write_lock(struct dentry *dentry);
31017+void aufs_write_unlock(struct dentry *dentry);
e49829fe 31018+int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
1facf9fc 31019+void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
31020+
31021+/* wbr_policy.c */
31022+extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
31023+extern struct au_wbr_create_operations au_wbr_create_ops[];
31024+int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
c2b27bf2 31025+int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
5afbbe0d 31026+int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
c2b27bf2
AM
31027+
31028+/* mvdown.c */
31029+int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
1facf9fc 31030+
076b876e
AM
31031+#ifdef CONFIG_AUFS_FHSM
31032+/* fhsm.c */
31033+
31034+static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
31035+{
31036+ pid_t pid;
31037+
31038+ spin_lock(&fhsm->fhsm_spin);
31039+ pid = fhsm->fhsm_pid;
31040+ spin_unlock(&fhsm->fhsm_spin);
31041+
31042+ return pid;
31043+}
31044+
31045+void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
31046+void au_fhsm_wrote_all(struct super_block *sb, int force);
31047+int au_fhsm_fd(struct super_block *sb, int oflags);
31048+int au_fhsm_br_alloc(struct au_branch *br);
c1595e42 31049+void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
076b876e
AM
31050+void au_fhsm_fin(struct super_block *sb);
31051+void au_fhsm_init(struct au_sbinfo *sbinfo);
31052+void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
31053+void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
31054+#else
31055+AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
31056+ int force)
31057+AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
31058+AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
c1595e42
JR
31059+AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
31060+AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
31061+AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
076b876e
AM
31062+AuStubVoid(au_fhsm_fin, struct super_block *sb)
31063+AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
31064+AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
31065+AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
31066+#endif
31067+
1facf9fc 31068+/* ---------------------------------------------------------------------- */
31069+
31070+static inline struct au_sbinfo *au_sbi(struct super_block *sb)
31071+{
31072+ return sb->s_fs_info;
31073+}
31074+
31075+/* ---------------------------------------------------------------------- */
31076+
31077+#ifdef CONFIG_AUFS_EXPORT
a2a7ad62 31078+int au_test_nfsd(void);
1facf9fc 31079+void au_export_init(struct super_block *sb);
b752ccd1 31080+void au_xigen_inc(struct inode *inode);
1facf9fc 31081+int au_xigen_new(struct inode *inode);
062440b3 31082+int au_xigen_set(struct super_block *sb, struct path *path);
1facf9fc 31083+void au_xigen_clr(struct super_block *sb);
31084+
31085+static inline int au_busy_or_stale(void)
31086+{
b752ccd1 31087+ if (!au_test_nfsd())
1facf9fc 31088+ return -EBUSY;
31089+ return -ESTALE;
31090+}
31091+#else
b752ccd1 31092+AuStubInt0(au_test_nfsd, void)
a2a7ad62 31093+AuStubVoid(au_export_init, struct super_block *sb)
b752ccd1 31094+AuStubVoid(au_xigen_inc, struct inode *inode)
4a4d8108 31095+AuStubInt0(au_xigen_new, struct inode *inode)
062440b3 31096+AuStubInt0(au_xigen_set, struct super_block *sb, struct path *path)
4a4d8108 31097+AuStubVoid(au_xigen_clr, struct super_block *sb)
c1595e42 31098+AuStub(int, au_busy_or_stale, return -EBUSY, void)
1facf9fc 31099+#endif /* CONFIG_AUFS_EXPORT */
31100+
31101+/* ---------------------------------------------------------------------- */
31102+
e49829fe
JR
31103+#ifdef CONFIG_AUFS_SBILIST
31104+/* module.c */
8b6a4947 31105+extern struct hlist_bl_head au_sbilist;
e49829fe
JR
31106+
31107+static inline void au_sbilist_init(void)
31108+{
8b6a4947 31109+ INIT_HLIST_BL_HEAD(&au_sbilist);
e49829fe
JR
31110+}
31111+
31112+static inline void au_sbilist_add(struct super_block *sb)
31113+{
8b6a4947 31114+ au_hbl_add(&au_sbi(sb)->si_list, &au_sbilist);
e49829fe
JR
31115+}
31116+
31117+static inline void au_sbilist_del(struct super_block *sb)
31118+{
8b6a4947 31119+ au_hbl_del(&au_sbi(sb)->si_list, &au_sbilist);
e49829fe 31120+}
53392da6
AM
31121+
31122+#ifdef CONFIG_AUFS_MAGIC_SYSRQ
31123+static inline void au_sbilist_lock(void)
31124+{
8b6a4947 31125+ hlist_bl_lock(&au_sbilist);
53392da6
AM
31126+}
31127+
31128+static inline void au_sbilist_unlock(void)
31129+{
8b6a4947 31130+ hlist_bl_unlock(&au_sbilist);
53392da6
AM
31131+}
31132+#define AuGFP_SBILIST GFP_ATOMIC
31133+#else
31134+AuStubVoid(au_sbilist_lock, void)
31135+AuStubVoid(au_sbilist_unlock, void)
31136+#define AuGFP_SBILIST GFP_NOFS
31137+#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
e49829fe
JR
31138+#else
31139+AuStubVoid(au_sbilist_init, void)
c1595e42
JR
31140+AuStubVoid(au_sbilist_add, struct super_block *sb)
31141+AuStubVoid(au_sbilist_del, struct super_block *sb)
53392da6
AM
31142+AuStubVoid(au_sbilist_lock, void)
31143+AuStubVoid(au_sbilist_unlock, void)
31144+#define AuGFP_SBILIST GFP_NOFS
e49829fe
JR
31145+#endif
31146+
31147+/* ---------------------------------------------------------------------- */
31148+
1facf9fc 31149+static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
31150+{
dece6358 31151+ /*
c1595e42 31152+ * This function is a dynamic '__init' function actually,
dece6358
AM
31153+ * so the tiny check for si_rwsem is unnecessary.
31154+ */
31155+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
1facf9fc 31156+#ifdef CONFIG_DEBUG_FS
31157+ sbinfo->si_dbgaufs = NULL;
86dc4139 31158+ sbinfo->si_dbgaufs_plink = NULL;
1facf9fc 31159+ sbinfo->si_dbgaufs_xib = NULL;
31160+#ifdef CONFIG_AUFS_EXPORT
31161+ sbinfo->si_dbgaufs_xigen = NULL;
31162+#endif
31163+#endif
31164+}
31165+
31166+/* ---------------------------------------------------------------------- */
31167+
a2654f78
AM
31168+/* current->atomic_flags */
31169+/* this value should never corrupt the ones defined in linux/sched.h */
fbc438ed 31170+#define PFA_AUFS 0x10
a2654f78
AM
31171+
31172+TASK_PFA_TEST(AUFS, test_aufs) /* task_test_aufs */
31173+TASK_PFA_SET(AUFS, aufs) /* task_set_aufs */
31174+TASK_PFA_CLEAR(AUFS, aufs) /* task_clear_aufs */
b752ccd1
AM
31175+
31176+static inline int si_pid_test(struct super_block *sb)
31177+{
a2654f78 31178+ return !!task_test_aufs(current);
b752ccd1
AM
31179+}
31180+
31181+static inline void si_pid_clr(struct super_block *sb)
31182+{
a2654f78
AM
31183+ AuDebugOn(!task_test_aufs(current));
31184+ task_clear_aufs(current);
b752ccd1
AM
31185+}
31186+
a2654f78
AM
31187+static inline void si_pid_set(struct super_block *sb)
31188+{
31189+ AuDebugOn(task_test_aufs(current));
31190+ task_set_aufs(current);
31191+}
febd17d6 31192+
b752ccd1
AM
31193+/* ---------------------------------------------------------------------- */
31194+
1facf9fc 31195+/* lock superblock. mainly for entry point functions */
8b6a4947
AM
31196+#define __si_read_lock(sb) au_rw_read_lock(&au_sbi(sb)->si_rwsem)
31197+#define __si_write_lock(sb) au_rw_write_lock(&au_sbi(sb)->si_rwsem)
31198+#define __si_read_trylock(sb) au_rw_read_trylock(&au_sbi(sb)->si_rwsem)
31199+#define __si_write_trylock(sb) au_rw_write_trylock(&au_sbi(sb)->si_rwsem)
31200+/*
31201+#define __si_read_trylock_nested(sb) \
31202+ au_rw_read_trylock_nested(&au_sbi(sb)->si_rwsem)
31203+#define __si_write_trylock_nested(sb) \
31204+ au_rw_write_trylock_nested(&au_sbi(sb)->si_rwsem)
31205+*/
31206+
31207+#define __si_read_unlock(sb) au_rw_read_unlock(&au_sbi(sb)->si_rwsem)
31208+#define __si_write_unlock(sb) au_rw_write_unlock(&au_sbi(sb)->si_rwsem)
31209+#define __si_downgrade_lock(sb) au_rw_dgrade_lock(&au_sbi(sb)->si_rwsem)
1facf9fc 31210+
dece6358
AM
31211+#define SiMustNoWaiters(sb) AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
31212+#define SiMustAnyLock(sb) AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
31213+#define SiMustWriteLock(sb) AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
31214+
b752ccd1
AM
31215+static inline void si_noflush_read_lock(struct super_block *sb)
31216+{
31217+ __si_read_lock(sb);
31218+ si_pid_set(sb);
31219+}
31220+
31221+static inline int si_noflush_read_trylock(struct super_block *sb)
31222+{
076b876e
AM
31223+ int locked;
31224+
31225+ locked = __si_read_trylock(sb);
b752ccd1
AM
31226+ if (locked)
31227+ si_pid_set(sb);
31228+ return locked;
31229+}
31230+
31231+static inline void si_noflush_write_lock(struct super_block *sb)
31232+{
31233+ __si_write_lock(sb);
31234+ si_pid_set(sb);
31235+}
31236+
31237+static inline int si_noflush_write_trylock(struct super_block *sb)
31238+{
076b876e
AM
31239+ int locked;
31240+
31241+ locked = __si_write_trylock(sb);
b752ccd1
AM
31242+ if (locked)
31243+ si_pid_set(sb);
31244+ return locked;
31245+}
31246+
7e9cd9fe 31247+#if 0 /* reserved */
1facf9fc 31248+static inline int si_read_trylock(struct super_block *sb, int flags)
31249+{
31250+ if (au_ftest_lock(flags, FLUSH))
31251+ au_nwt_flush(&au_sbi(sb)->si_nowait);
31252+ return si_noflush_read_trylock(sb);
31253+}
e49829fe 31254+#endif
1facf9fc 31255+
b752ccd1
AM
31256+static inline void si_read_unlock(struct super_block *sb)
31257+{
31258+ si_pid_clr(sb);
31259+ __si_read_unlock(sb);
31260+}
31261+
7e9cd9fe 31262+#if 0 /* reserved */
1facf9fc 31263+static inline int si_write_trylock(struct super_block *sb, int flags)
31264+{
31265+ if (au_ftest_lock(flags, FLUSH))
31266+ au_nwt_flush(&au_sbi(sb)->si_nowait);
31267+ return si_noflush_write_trylock(sb);
31268+}
b752ccd1
AM
31269+#endif
31270+
31271+static inline void si_write_unlock(struct super_block *sb)
31272+{
31273+ si_pid_clr(sb);
31274+ __si_write_unlock(sb);
31275+}
31276+
7e9cd9fe 31277+#if 0 /* reserved */
b752ccd1
AM
31278+static inline void si_downgrade_lock(struct super_block *sb)
31279+{
31280+ __si_downgrade_lock(sb);
31281+}
31282+#endif
1facf9fc 31283+
31284+/* ---------------------------------------------------------------------- */
31285+
5afbbe0d 31286+static inline aufs_bindex_t au_sbbot(struct super_block *sb)
1facf9fc 31287+{
dece6358 31288+ SiMustAnyLock(sb);
5afbbe0d 31289+ return au_sbi(sb)->si_bbot;
1facf9fc 31290+}
31291+
31292+static inline unsigned int au_mntflags(struct super_block *sb)
31293+{
dece6358 31294+ SiMustAnyLock(sb);
1facf9fc 31295+ return au_sbi(sb)->si_mntflags;
31296+}
31297+
31298+static inline unsigned int au_sigen(struct super_block *sb)
31299+{
dece6358 31300+ SiMustAnyLock(sb);
1facf9fc 31301+ return au_sbi(sb)->si_generation;
31302+}
31303+
31304+static inline struct au_branch *au_sbr(struct super_block *sb,
31305+ aufs_bindex_t bindex)
31306+{
dece6358 31307+ SiMustAnyLock(sb);
1facf9fc 31308+ return au_sbi(sb)->si_branch[0 + bindex];
31309+}
31310+
acd2b654 31311+static inline loff_t au_xi_maxent(struct super_block *sb)
1facf9fc 31312+{
dece6358 31313+ SiMustAnyLock(sb);
acd2b654 31314+ return au_sbi(sb)->si_ximaxent;
1facf9fc 31315+}
31316+
31317+#endif /* __KERNEL__ */
31318+#endif /* __AUFS_SUPER_H__ */
7f207e10 31319diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
eca34b5c 31320--- /usr/share/empty/fs/aufs/sysaufs.c 1970-01-01 01:00:00.000000000 +0100
631230db 31321+++ linux/fs/aufs/sysaufs.c 2023-10-10 22:51:18.033248030 +0200
26eb093c 31322@@ -0,0 +1,94 @@
cd7a4cd9 31323+// SPDX-License-Identifier: GPL-2.0
1facf9fc 31324+/*
0dcfbb52 31325+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 31326+ *
0dcfbb52 31327+ * This program is free software; you can redistribute it and/or modify
1facf9fc 31328+ * it under the terms of the GNU General Public License as published by
31329+ * the Free Software Foundation; either version 2 of the License, or
31330+ * (at your option) any later version.
dece6358
AM
31331+ *
31332+ * This program is distributed in the hope that it will be useful,
31333+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31334+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31335+ * GNU General Public License for more details.
31336+ *
31337+ * You should have received a copy of the GNU General Public License
523b37e3 31338+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31339+ */
31340+
31341+/*
31342+ * sysfs interface and lifetime management
31343+ * they are necessary regardless sysfs is disabled.
31344+ */
31345+
1facf9fc 31346+#include <linux/random.h>
1facf9fc 31347+#include "aufs.h"
31348+
31349+unsigned long sysaufs_si_mask;
e49829fe 31350+struct kset *sysaufs_kset;
1facf9fc 31351+
31352+#define AuSiAttr(_name) { \
31353+ .attr = { .name = __stringify(_name), .mode = 0444 }, \
31354+ .show = sysaufs_si_##_name, \
31355+}
31356+
31357+static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
31358+struct attribute *sysaufs_si_attrs[] = {
31359+ &sysaufs_si_attr_xi_path.attr,
31360+ NULL,
31361+};
26eb093c 31362+ATTRIBUTE_GROUPS(sysaufs_si);
1facf9fc 31363+
4a4d8108 31364+static const struct sysfs_ops au_sbi_ops = {
1facf9fc 31365+ .show = sysaufs_si_show
31366+};
31367+
631230db 31368+static const struct kobj_type au_sbi_ktype = {
1facf9fc 31369+ .release = au_si_free,
31370+ .sysfs_ops = &au_sbi_ops,
26eb093c 31371+ .default_groups = sysaufs_si_groups
1facf9fc 31372+};
31373+
31374+/* ---------------------------------------------------------------------- */
31375+
31376+int sysaufs_si_init(struct au_sbinfo *sbinfo)
31377+{
31378+ int err;
31379+
e49829fe 31380+ sbinfo->si_kobj.kset = sysaufs_kset;
1facf9fc 31381+ /* cf. sysaufs_name() */
31382+ err = kobject_init_and_add
e49829fe 31383+ (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
1facf9fc 31384+ SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
31385+
1facf9fc 31386+ return err;
31387+}
31388+
31389+void sysaufs_fin(void)
31390+{
e49829fe
JR
31391+ sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31392+ kset_unregister(sysaufs_kset);
1facf9fc 31393+}
31394+
31395+int __init sysaufs_init(void)
31396+{
31397+ int err;
31398+
31399+ do {
31400+ get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
31401+ } while (!sysaufs_si_mask);
31402+
4a4d8108 31403+ err = -EINVAL;
e49829fe
JR
31404+ sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
31405+ if (unlikely(!sysaufs_kset))
4a4d8108 31406+ goto out;
e49829fe
JR
31407+ err = PTR_ERR(sysaufs_kset);
31408+ if (IS_ERR(sysaufs_kset))
1facf9fc 31409+ goto out;
e49829fe 31410+ err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
062440b3 31411+ if (unlikely(err))
e49829fe 31412+ kset_unregister(sysaufs_kset);
1facf9fc 31413+
4f0767ce 31414+out:
1facf9fc 31415+ return err;
31416+}
7f207e10 31417diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
eca34b5c 31418--- /usr/share/empty/fs/aufs/sysaufs.h 1970-01-01 01:00:00.000000000 +0100
594d0238 31419+++ linux/fs/aufs/sysaufs.h 2022-11-05 23:02:18.969222617 +0100
062440b3
AM
31420@@ -0,0 +1,102 @@
31421+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 31422+/*
0dcfbb52 31423+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 31424+ *
0dcfbb52 31425+ * This program is free software; you can redistribute it and/or modify
1facf9fc 31426+ * it under the terms of the GNU General Public License as published by
31427+ * the Free Software Foundation; either version 2 of the License, or
31428+ * (at your option) any later version.
dece6358
AM
31429+ *
31430+ * This program is distributed in the hope that it will be useful,
31431+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31432+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31433+ * GNU General Public License for more details.
31434+ *
31435+ * You should have received a copy of the GNU General Public License
523b37e3 31436+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31437+ */
31438+
31439+/*
31440+ * sysfs interface and mount lifetime management
31441+ */
31442+
31443+#ifndef __SYSAUFS_H__
31444+#define __SYSAUFS_H__
31445+
31446+#ifdef __KERNEL__
31447+
1facf9fc 31448+#include <linux/sysfs.h>
1facf9fc 31449+#include "module.h"
31450+
dece6358
AM
31451+struct super_block;
31452+struct au_sbinfo;
31453+
1facf9fc 31454+struct sysaufs_si_attr {
31455+ struct attribute attr;
31456+ int (*show)(struct seq_file *seq, struct super_block *sb);
31457+};
31458+
31459+/* ---------------------------------------------------------------------- */
31460+
31461+/* sysaufs.c */
31462+extern unsigned long sysaufs_si_mask;
e49829fe 31463+extern struct kset *sysaufs_kset;
1facf9fc 31464+extern struct attribute *sysaufs_si_attrs[];
31465+int sysaufs_si_init(struct au_sbinfo *sbinfo);
31466+int __init sysaufs_init(void);
31467+void sysaufs_fin(void);
31468+
31469+/* ---------------------------------------------------------------------- */
31470+
31471+/* some people doesn't like to show a pointer in kernel */
31472+static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
31473+{
31474+ return sysaufs_si_mask ^ (unsigned long)sbinfo;
31475+}
31476+
31477+#define SysaufsSiNamePrefix "si_"
31478+#define SysaufsSiNameLen (sizeof(SysaufsSiNamePrefix) + 16)
31479+static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
31480+{
31481+ snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
31482+ sysaufs_si_id(sbinfo));
31483+}
31484+
31485+struct au_branch;
31486+#ifdef CONFIG_SYSFS
31487+/* sysfs.c */
31488+extern struct attribute_group *sysaufs_attr_group;
31489+
31490+int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
31491+ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31492+ char *buf);
076b876e
AM
31493+long au_brinfo_ioctl(struct file *file, unsigned long arg);
31494+#ifdef CONFIG_COMPAT
31495+long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
31496+#endif
1facf9fc 31497+
31498+void sysaufs_br_init(struct au_branch *br);
31499+void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
31500+void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
31501+
31502+#define sysaufs_brs_init() do {} while (0)
31503+
31504+#else
31505+#define sysaufs_attr_group NULL
31506+
4a4d8108 31507+AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
c1595e42
JR
31508+AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
31509+ struct attribute *attr, char *buf)
4a4d8108
AM
31510+AuStubVoid(sysaufs_br_init, struct au_branch *br)
31511+AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
31512+AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
1facf9fc 31513+
31514+static inline void sysaufs_brs_init(void)
31515+{
31516+ sysaufs_brs = 0;
31517+}
31518+
31519+#endif /* CONFIG_SYSFS */
31520+
31521+#endif /* __KERNEL__ */
31522+#endif /* __SYSAUFS_H__ */
7f207e10 31523diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
eca34b5c 31524--- /usr/share/empty/fs/aufs/sysfs.c 1970-01-01 01:00:00.000000000 +0100
594d0238 31525+++ linux/fs/aufs/sysfs.c 2022-11-05 23:02:18.969222617 +0100
ba1aed25 31526@@ -0,0 +1,374 @@
cd7a4cd9 31527+// SPDX-License-Identifier: GPL-2.0
1facf9fc 31528+/*
0dcfbb52 31529+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 31530+ *
0dcfbb52 31531+ * This program is free software; you can redistribute it and/or modify
1facf9fc 31532+ * it under the terms of the GNU General Public License as published by
31533+ * the Free Software Foundation; either version 2 of the License, or
31534+ * (at your option) any later version.
dece6358
AM
31535+ *
31536+ * This program is distributed in the hope that it will be useful,
31537+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31538+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31539+ * GNU General Public License for more details.
31540+ *
31541+ * You should have received a copy of the GNU General Public License
523b37e3 31542+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31543+ */
31544+
31545+/*
31546+ * sysfs interface
31547+ */
31548+
076b876e 31549+#include <linux/compat.h>
1facf9fc 31550+#include <linux/seq_file.h>
1facf9fc 31551+#include "aufs.h"
31552+
4a4d8108
AM
31553+#ifdef CONFIG_AUFS_FS_MODULE
31554+/* this entry violates the "one line per file" policy of sysfs */
31555+static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
31556+ char *buf)
31557+{
31558+ ssize_t err;
31559+ static char *conf =
31560+/* this file is generated at compiling */
31561+#include "conf.str"
31562+ ;
31563+
31564+ err = snprintf(buf, PAGE_SIZE, conf);
31565+ if (unlikely(err >= PAGE_SIZE))
31566+ err = -EFBIG;
31567+ return err;
31568+}
31569+
31570+static struct kobj_attribute au_config_attr = __ATTR_RO(config);
31571+#endif
31572+
1facf9fc 31573+static struct attribute *au_attr[] = {
4a4d8108
AM
31574+#ifdef CONFIG_AUFS_FS_MODULE
31575+ &au_config_attr.attr,
31576+#endif
1facf9fc 31577+ NULL, /* need to NULL terminate the list of attributes */
31578+};
31579+
31580+static struct attribute_group sysaufs_attr_group_body = {
31581+ .attrs = au_attr
31582+};
31583+
31584+struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
31585+
31586+/* ---------------------------------------------------------------------- */
31587+
31588+int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
31589+{
31590+ int err;
31591+
dece6358
AM
31592+ SiMustAnyLock(sb);
31593+
1facf9fc 31594+ err = 0;
31595+ if (au_opt_test(au_mntflags(sb), XINO)) {
31596+ err = au_xino_path(seq, au_sbi(sb)->si_xib);
31597+ seq_putc(seq, '\n');
31598+ }
31599+ return err;
31600+}
31601+
31602+/*
31603+ * the lifetime of branch is independent from the entry under sysfs.
31604+ * sysfs handles the lifetime of the entry, and never call ->show() after it is
31605+ * unlinked.
31606+ */
31607+static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
392086de 31608+ aufs_bindex_t bindex, int idx)
1facf9fc 31609+{
1e00d052 31610+ int err;
1facf9fc 31611+ struct path path;
31612+ struct dentry *root;
31613+ struct au_branch *br;
076b876e 31614+ au_br_perm_str_t perm;
1facf9fc 31615+
31616+ AuDbg("b%d\n", bindex);
31617+
1e00d052 31618+ err = 0;
1facf9fc 31619+ root = sb->s_root;
31620+ di_read_lock_parent(root, !AuLock_IR);
31621+ br = au_sbr(sb, bindex);
392086de
AM
31622+
31623+ switch (idx) {
31624+ case AuBrSysfs_BR:
31625+ path.mnt = au_br_mnt(br);
31626+ path.dentry = au_h_dptr(root, bindex);
79b8bda9
AM
31627+ err = au_seq_path(seq, &path);
31628+ if (!err) {
31629+ au_optstr_br_perm(&perm, br->br_perm);
31630+ seq_printf(seq, "=%s\n", perm.a);
31631+ }
392086de
AM
31632+ break;
31633+ case AuBrSysfs_BRID:
79b8bda9 31634+ seq_printf(seq, "%d\n", br->br_id);
392086de
AM
31635+ break;
31636+ }
076b876e 31637+ di_read_unlock(root, !AuLock_IR);
79b8bda9 31638+ if (unlikely(err || seq_has_overflowed(seq)))
076b876e 31639+ err = -E2BIG;
392086de 31640+
1e00d052 31641+ return err;
1facf9fc 31642+}
31643+
31644+/* ---------------------------------------------------------------------- */
31645+
31646+static struct seq_file *au_seq(char *p, ssize_t len)
31647+{
31648+ struct seq_file *seq;
31649+
31650+ seq = kzalloc(sizeof(*seq), GFP_NOFS);
31651+ if (seq) {
31652+ /* mutex_init(&seq.lock); */
31653+ seq->buf = p;
31654+ seq->size = len;
31655+ return seq; /* success */
31656+ }
31657+
31658+ seq = ERR_PTR(-ENOMEM);
31659+ return seq;
31660+}
31661+
392086de
AM
31662+#define SysaufsBr_PREFIX "br"
31663+#define SysaufsBrid_PREFIX "brid"
1facf9fc 31664+
31665+/* todo: file size may exceed PAGE_SIZE */
31666+ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
1308ab2a 31667+ char *buf)
1facf9fc 31668+{
31669+ ssize_t err;
392086de 31670+ int idx;
1facf9fc 31671+ long l;
5afbbe0d 31672+ aufs_bindex_t bbot;
1facf9fc 31673+ struct au_sbinfo *sbinfo;
31674+ struct super_block *sb;
31675+ struct seq_file *seq;
31676+ char *name;
31677+ struct attribute **cattr;
31678+
31679+ sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
31680+ sb = sbinfo->si_sb;
1308ab2a 31681+
31682+ /*
31683+ * prevent a race condition between sysfs and aufs.
31684+ * for instance, sysfs_file_read() calls sysfs_get_active_two() which
31685+ * prohibits maintaining the sysfs entries.
31686+ * hew we acquire read lock after sysfs_get_active_two().
31687+ * on the other hand, the remount process may maintain the sysfs/aufs
31688+ * entries after acquiring write lock.
31689+ * it can cause a deadlock.
31690+ * simply we gave up processing read here.
31691+ */
31692+ err = -EBUSY;
31693+ if (unlikely(!si_noflush_read_trylock(sb)))
31694+ goto out;
1facf9fc 31695+
31696+ seq = au_seq(buf, PAGE_SIZE);
31697+ err = PTR_ERR(seq);
31698+ if (IS_ERR(seq))
1308ab2a 31699+ goto out_unlock;
1facf9fc 31700+
31701+ name = (void *)attr->name;
31702+ cattr = sysaufs_si_attrs;
31703+ while (*cattr) {
31704+ if (!strcmp(name, (*cattr)->name)) {
31705+ err = container_of(*cattr, struct sysaufs_si_attr, attr)
31706+ ->show(seq, sb);
31707+ goto out_seq;
31708+ }
31709+ cattr++;
31710+ }
31711+
392086de
AM
31712+ if (!strncmp(name, SysaufsBrid_PREFIX,
31713+ sizeof(SysaufsBrid_PREFIX) - 1)) {
31714+ idx = AuBrSysfs_BRID;
31715+ name += sizeof(SysaufsBrid_PREFIX) - 1;
31716+ } else if (!strncmp(name, SysaufsBr_PREFIX,
31717+ sizeof(SysaufsBr_PREFIX) - 1)) {
31718+ idx = AuBrSysfs_BR;
1facf9fc 31719+ name += sizeof(SysaufsBr_PREFIX) - 1;
392086de
AM
31720+ } else
31721+ BUG();
31722+
31723+ err = kstrtol(name, 10, &l);
31724+ if (!err) {
5afbbe0d
AM
31725+ bbot = au_sbbot(sb);
31726+ if (l <= bbot)
392086de
AM
31727+ err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
31728+ else
31729+ err = -ENOENT;
1facf9fc 31730+ }
1facf9fc 31731+
4f0767ce 31732+out_seq:
1facf9fc 31733+ if (!err) {
31734+ err = seq->count;
31735+ /* sysfs limit */
31736+ if (unlikely(err == PAGE_SIZE))
31737+ err = -EFBIG;
31738+ }
9f237c51 31739+ au_kfree_rcu(seq);
4f0767ce 31740+out_unlock:
1facf9fc 31741+ si_read_unlock(sb);
4f0767ce 31742+out:
1facf9fc 31743+ return err;
31744+}
31745+
31746+/* ---------------------------------------------------------------------- */
31747+
076b876e
AM
31748+static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
31749+{
31750+ int err;
31751+ int16_t brid;
5afbbe0d 31752+ aufs_bindex_t bindex, bbot;
076b876e
AM
31753+ size_t sz;
31754+ char *buf;
31755+ struct seq_file *seq;
31756+ struct au_branch *br;
31757+
31758+ si_read_lock(sb, AuLock_FLUSH);
5afbbe0d
AM
31759+ bbot = au_sbbot(sb);
31760+ err = bbot + 1;
076b876e
AM
31761+ if (!arg)
31762+ goto out;
31763+
31764+ err = -ENOMEM;
31765+ buf = (void *)__get_free_page(GFP_NOFS);
31766+ if (unlikely(!buf))
31767+ goto out;
31768+
31769+ seq = au_seq(buf, PAGE_SIZE);
31770+ err = PTR_ERR(seq);
31771+ if (IS_ERR(seq))
31772+ goto out_buf;
31773+
31774+ sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
5afbbe0d 31775+ for (bindex = 0; bindex <= bbot; bindex++, arg++) {
ba1aed25
AM
31776+ /* VERIFY_WRITE */
31777+ err = !access_ok(arg, sizeof(*arg));
076b876e
AM
31778+ if (unlikely(err))
31779+ break;
31780+
31781+ br = au_sbr(sb, bindex);
31782+ brid = br->br_id;
31783+ BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
31784+ err = __put_user(brid, &arg->id);
31785+ if (unlikely(err))
31786+ break;
31787+
31788+ BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
31789+ err = __put_user(br->br_perm, &arg->perm);
31790+ if (unlikely(err))
31791+ break;
31792+
79b8bda9
AM
31793+ err = au_seq_path(seq, &br->br_path);
31794+ if (unlikely(err))
31795+ break;
31796+ seq_putc(seq, '\0');
31797+ if (!seq_has_overflowed(seq)) {
076b876e
AM
31798+ err = copy_to_user(arg->path, seq->buf, seq->count);
31799+ seq->count = 0;
31800+ if (unlikely(err))
31801+ break;
31802+ } else {
31803+ err = -E2BIG;
31804+ goto out_seq;
31805+ }
31806+ }
31807+ if (unlikely(err))
31808+ err = -EFAULT;
31809+
31810+out_seq:
9f237c51 31811+ au_kfree_rcu(seq);
076b876e 31812+out_buf:
1c60b727 31813+ free_page((unsigned long)buf);
076b876e
AM
31814+out:
31815+ si_read_unlock(sb);
31816+ return err;
31817+}
31818+
31819+long au_brinfo_ioctl(struct file *file, unsigned long arg)
31820+{
2000de60 31821+ return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
076b876e
AM
31822+}
31823+
31824+#ifdef CONFIG_COMPAT
31825+long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
31826+{
2000de60 31827+ return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
076b876e
AM
31828+}
31829+#endif
31830+
31831+/* ---------------------------------------------------------------------- */
31832+
1facf9fc 31833+void sysaufs_br_init(struct au_branch *br)
31834+{
392086de
AM
31835+ int i;
31836+ struct au_brsysfs *br_sysfs;
31837+ struct attribute *attr;
4a4d8108 31838+
392086de
AM
31839+ br_sysfs = br->br_sysfs;
31840+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31841+ attr = &br_sysfs->attr;
31842+ sysfs_attr_init(attr);
31843+ attr->name = br_sysfs->name;
cd7a4cd9 31844+ attr->mode = 0444;
392086de
AM
31845+ br_sysfs++;
31846+ }
1facf9fc 31847+}
31848+
31849+void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
31850+{
31851+ struct au_branch *br;
31852+ struct kobject *kobj;
392086de
AM
31853+ struct au_brsysfs *br_sysfs;
31854+ int i;
5afbbe0d 31855+ aufs_bindex_t bbot;
1facf9fc 31856+
1facf9fc 31857+ if (!sysaufs_brs)
31858+ return;
31859+
31860+ kobj = &au_sbi(sb)->si_kobj;
5afbbe0d
AM
31861+ bbot = au_sbbot(sb);
31862+ for (; bindex <= bbot; bindex++) {
1facf9fc 31863+ br = au_sbr(sb, bindex);
392086de
AM
31864+ br_sysfs = br->br_sysfs;
31865+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31866+ sysfs_remove_file(kobj, &br_sysfs->attr);
31867+ br_sysfs++;
31868+ }
1facf9fc 31869+ }
31870+}
31871+
31872+void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
31873+{
392086de 31874+ int err, i;
5afbbe0d 31875+ aufs_bindex_t bbot;
1facf9fc 31876+ struct kobject *kobj;
31877+ struct au_branch *br;
392086de 31878+ struct au_brsysfs *br_sysfs;
1facf9fc 31879+
1facf9fc 31880+ if (!sysaufs_brs)
31881+ return;
31882+
31883+ kobj = &au_sbi(sb)->si_kobj;
5afbbe0d
AM
31884+ bbot = au_sbbot(sb);
31885+ for (; bindex <= bbot; bindex++) {
1facf9fc 31886+ br = au_sbr(sb, bindex);
392086de
AM
31887+ br_sysfs = br->br_sysfs;
31888+ snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
31889+ SysaufsBr_PREFIX "%d", bindex);
31890+ snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
31891+ SysaufsBrid_PREFIX "%d", bindex);
31892+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31893+ err = sysfs_create_file(kobj, &br_sysfs->attr);
31894+ if (unlikely(err))
31895+ pr_warn("failed %s under sysfs(%d)\n",
31896+ br_sysfs->name, err);
31897+ br_sysfs++;
31898+ }
1facf9fc 31899+ }
31900+}
7f207e10 31901diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
eca34b5c 31902--- /usr/share/empty/fs/aufs/sysrq.c 1970-01-01 01:00:00.000000000 +0100
631230db 31903+++ linux/fs/aufs/sysrq.c 2023-10-31 09:31:04.199880750 +0100
43982f53 31904@@ -0,0 +1,149 @@
cd7a4cd9 31905+// SPDX-License-Identifier: GPL-2.0
1facf9fc 31906+/*
0dcfbb52 31907+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 31908+ *
0dcfbb52 31909+ * This program is free software; you can redistribute it and/or modify
1facf9fc 31910+ * it under the terms of the GNU General Public License as published by
31911+ * the Free Software Foundation; either version 2 of the License, or
31912+ * (at your option) any later version.
dece6358
AM
31913+ *
31914+ * This program is distributed in the hope that it will be useful,
31915+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31916+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31917+ * GNU General Public License for more details.
31918+ *
31919+ * You should have received a copy of the GNU General Public License
523b37e3 31920+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31921+ */
31922+
31923+/*
acd2b654 31924+ * magic sysrq handler
1facf9fc 31925+ */
31926+
1facf9fc 31927+/* #include <linux/sysrq.h> */
027c5e7a 31928+#include <linux/writeback.h>
1facf9fc 31929+#include "aufs.h"
31930+
31931+/* ---------------------------------------------------------------------- */
31932+
31933+static void sysrq_sb(struct super_block *sb)
31934+{
31935+ char *plevel;
31936+ struct au_sbinfo *sbinfo;
31937+ struct file *file;
8b6a4947
AM
31938+ struct hlist_bl_head *files;
31939+ struct hlist_bl_node *pos;
523b37e3 31940+ struct au_finfo *finfo;
43982f53 31941+ struct inode *i;
1facf9fc 31942+
31943+ plevel = au_plevel;
31944+ au_plevel = KERN_WARNING;
1facf9fc 31945+
4a4d8108 31946+ /* since we define pr_fmt, call printk directly */
c06a8ce3
AM
31947+#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
31948+
31949+ sbinfo = au_sbi(sb);
4a4d8108 31950+ printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
c06a8ce3 31951+ pr("superblock\n");
1facf9fc 31952+ au_dpri_sb(sb);
027c5e7a 31953+
43982f53 31954+#if 0 /* reserved */
027c5e7a
AM
31955+ do {
31956+ int err, i, j, ndentry;
31957+ struct au_dcsub_pages dpages;
31958+ struct au_dpage *dpage;
31959+
31960+ err = au_dpages_init(&dpages, GFP_ATOMIC);
31961+ if (unlikely(err))
31962+ break;
31963+ err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
31964+ if (!err)
31965+ for (i = 0; i < dpages.ndpage; i++) {
31966+ dpage = dpages.dpages + i;
31967+ ndentry = dpage->ndentry;
31968+ for (j = 0; j < ndentry; j++)
31969+ au_dpri_dentry(dpage->dentries[j]);
31970+ }
31971+ au_dpages_free(&dpages);
31972+ } while (0);
31973+#endif
31974+
43982f53
AM
31975+ pr("isolated inode\n");
31976+ spin_lock(&sb->s_inode_list_lock);
31977+ list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
31978+ spin_lock(&i->i_lock);
31979+ if (hlist_empty(&i->i_dentry))
31980+ au_dpri_inode(i);
31981+ spin_unlock(&i->i_lock);
027c5e7a 31982+ }
43982f53
AM
31983+ spin_unlock(&sb->s_inode_list_lock);
31984+
c06a8ce3 31985+ pr("files\n");
523b37e3 31986+ files = &au_sbi(sb)->si_files;
8b6a4947
AM
31987+ hlist_bl_lock(files);
31988+ hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
4a4d8108 31989+ umode_t mode;
076b876e 31990+
523b37e3 31991+ file = finfo->fi_file;
c06a8ce3 31992+ mode = file_inode(file)->i_mode;
38d290e6 31993+ if (!special_file(mode))
1facf9fc 31994+ au_dpri_file(file);
523b37e3 31995+ }
8b6a4947 31996+ hlist_bl_unlock(files);
c06a8ce3 31997+ pr("done\n");
1facf9fc 31998+
c06a8ce3 31999+#undef pr
1facf9fc 32000+ au_plevel = plevel;
1facf9fc 32001+}
32002+
32003+/* ---------------------------------------------------------------------- */
32004+
32005+/* module parameter */
32006+static char *aufs_sysrq_key = "a";
cd7a4cd9 32007+module_param_named(sysrq, aufs_sysrq_key, charp, 0444);
1facf9fc 32008+MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
32009+
631230db 32010+static void au_sysrq(u8 key __maybe_unused)
1facf9fc 32011+{
1facf9fc 32012+ struct au_sbinfo *sbinfo;
8b6a4947 32013+ struct hlist_bl_node *pos;
1facf9fc 32014+
027c5e7a 32015+ lockdep_off();
53392da6 32016+ au_sbilist_lock();
8b6a4947 32017+ hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
1facf9fc 32018+ sysrq_sb(sbinfo->si_sb);
53392da6 32019+ au_sbilist_unlock();
027c5e7a 32020+ lockdep_on();
1facf9fc 32021+}
32022+
32023+static struct sysrq_key_op au_sysrq_op = {
32024+ .handler = au_sysrq,
32025+ .help_msg = "Aufs",
32026+ .action_msg = "Aufs",
32027+ .enable_mask = SYSRQ_ENABLE_DUMP
32028+};
32029+
32030+/* ---------------------------------------------------------------------- */
32031+
32032+int __init au_sysrq_init(void)
32033+{
32034+ int err;
32035+ char key;
32036+
32037+ err = -1;
32038+ key = *aufs_sysrq_key;
32039+ if ('a' <= key && key <= 'z')
32040+ err = register_sysrq_key(key, &au_sysrq_op);
32041+ if (unlikely(err))
4a4d8108 32042+ pr_err("err %d, sysrq=%c\n", err, key);
1facf9fc 32043+ return err;
32044+}
32045+
32046+void au_sysrq_fin(void)
32047+{
32048+ int err;
076b876e 32049+
1facf9fc 32050+ err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
32051+ if (unlikely(err))
4a4d8108 32052+ pr_err("err %d (ignored)\n", err);
1facf9fc 32053+}
7f207e10 32054diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
eca34b5c 32055--- /usr/share/empty/fs/aufs/vdir.c 1970-01-01 01:00:00.000000000 +0100
594d0238 32056+++ linux/fs/aufs/vdir.c 2022-12-17 09:21:34.799855195 +0100
eca801bf 32057@@ -0,0 +1,896 @@
cd7a4cd9 32058+// SPDX-License-Identifier: GPL-2.0
1facf9fc 32059+/*
0dcfbb52 32060+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 32061+ *
0dcfbb52 32062+ * This program is free software; you can redistribute it and/or modify
1facf9fc 32063+ * it under the terms of the GNU General Public License as published by
32064+ * the Free Software Foundation; either version 2 of the License, or
32065+ * (at your option) any later version.
dece6358
AM
32066+ *
32067+ * This program is distributed in the hope that it will be useful,
32068+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
32069+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32070+ * GNU General Public License for more details.
32071+ *
32072+ * You should have received a copy of the GNU General Public License
523b37e3 32073+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 32074+ */
32075+
32076+/*
32077+ * virtual or vertical directory
32078+ */
32079+
eca801bf 32080+#include <linux/iversion.h>
1facf9fc 32081+#include "aufs.h"
32082+
dece6358 32083+static unsigned int calc_size(int nlen)
1facf9fc 32084+{
dece6358 32085+ return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
1facf9fc 32086+}
32087+
32088+static int set_deblk_end(union au_vdir_deblk_p *p,
32089+ union au_vdir_deblk_p *deblk_end)
32090+{
32091+ if (calc_size(0) <= deblk_end->deblk - p->deblk) {
32092+ p->de->de_str.len = 0;
32093+ /* smp_mb(); */
32094+ return 0;
32095+ }
32096+ return -1; /* error */
32097+}
32098+
32099+/* returns true or false */
32100+static int is_deblk_end(union au_vdir_deblk_p *p,
32101+ union au_vdir_deblk_p *deblk_end)
32102+{
32103+ if (calc_size(0) <= deblk_end->deblk - p->deblk)
32104+ return !p->de->de_str.len;
32105+ return 1;
32106+}
32107+
32108+static unsigned char *last_deblk(struct au_vdir *vdir)
32109+{
32110+ return vdir->vd_deblk[vdir->vd_nblk - 1];
32111+}
32112+
32113+/* ---------------------------------------------------------------------- */
32114+
79b8bda9 32115+/* estimate the appropriate size for name hash table */
1308ab2a 32116+unsigned int au_rdhash_est(loff_t sz)
32117+{
32118+ unsigned int n;
32119+
32120+ n = UINT_MAX;
32121+ sz >>= 10;
32122+ if (sz < n)
32123+ n = sz;
32124+ if (sz < AUFS_RDHASH_DEF)
32125+ n = AUFS_RDHASH_DEF;
4a4d8108 32126+ /* pr_info("n %u\n", n); */
1308ab2a 32127+ return n;
32128+}
32129+
1facf9fc 32130+/*
32131+ * the allocated memory has to be freed by
dece6358 32132+ * au_nhash_wh_free() or au_nhash_de_free().
1facf9fc 32133+ */
dece6358 32134+int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
1facf9fc 32135+{
1facf9fc 32136+ struct hlist_head *head;
dece6358 32137+ unsigned int u;
076b876e 32138+ size_t sz;
1facf9fc 32139+
076b876e
AM
32140+ sz = sizeof(*nhash->nh_head) * num_hash;
32141+ head = kmalloc(sz, gfp);
dece6358
AM
32142+ if (head) {
32143+ nhash->nh_num = num_hash;
32144+ nhash->nh_head = head;
32145+ for (u = 0; u < num_hash; u++)
1facf9fc 32146+ INIT_HLIST_HEAD(head++);
dece6358 32147+ return 0; /* success */
1facf9fc 32148+ }
1facf9fc 32149+
dece6358 32150+ return -ENOMEM;
1facf9fc 32151+}
32152+
dece6358
AM
32153+static void nhash_count(struct hlist_head *head)
32154+{
43982f53 32155+#if 0 /* debugging */
dece6358
AM
32156+ unsigned long n;
32157+ struct hlist_node *pos;
32158+
32159+ n = 0;
32160+ hlist_for_each(pos, head)
32161+ n++;
4a4d8108 32162+ pr_info("%lu\n", n);
dece6358
AM
32163+#endif
32164+}
32165+
32166+static void au_nhash_wh_do_free(struct hlist_head *head)
1facf9fc 32167+{
c06a8ce3
AM
32168+ struct au_vdir_wh *pos;
32169+ struct hlist_node *node;
1facf9fc 32170+
c06a8ce3 32171+ hlist_for_each_entry_safe(pos, node, head, wh_hash)
9f237c51 32172+ au_kfree_rcu(pos);
1facf9fc 32173+}
32174+
dece6358 32175+static void au_nhash_de_do_free(struct hlist_head *head)
1facf9fc 32176+{
c06a8ce3
AM
32177+ struct au_vdir_dehstr *pos;
32178+ struct hlist_node *node;
1facf9fc 32179+
c06a8ce3 32180+ hlist_for_each_entry_safe(pos, node, head, hash)
1c60b727 32181+ au_cache_free_vdir_dehstr(pos);
1facf9fc 32182+}
32183+
dece6358
AM
32184+static void au_nhash_do_free(struct au_nhash *nhash,
32185+ void (*free)(struct hlist_head *head))
1facf9fc 32186+{
1308ab2a 32187+ unsigned int n;
1facf9fc 32188+ struct hlist_head *head;
1facf9fc 32189+
dece6358 32190+ n = nhash->nh_num;
1308ab2a 32191+ if (!n)
32192+ return;
32193+
dece6358 32194+ head = nhash->nh_head;
1308ab2a 32195+ while (n-- > 0) {
dece6358
AM
32196+ nhash_count(head);
32197+ free(head++);
1facf9fc 32198+ }
9f237c51 32199+ au_kfree_try_rcu(nhash->nh_head);
1facf9fc 32200+}
32201+
dece6358 32202+void au_nhash_wh_free(struct au_nhash *whlist)
1facf9fc 32203+{
dece6358
AM
32204+ au_nhash_do_free(whlist, au_nhash_wh_do_free);
32205+}
1facf9fc 32206+
dece6358
AM
32207+static void au_nhash_de_free(struct au_nhash *delist)
32208+{
32209+ au_nhash_do_free(delist, au_nhash_de_do_free);
1facf9fc 32210+}
32211+
32212+/* ---------------------------------------------------------------------- */
32213+
32214+int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
32215+ int limit)
32216+{
32217+ int num;
32218+ unsigned int u, n;
32219+ struct hlist_head *head;
c06a8ce3 32220+ struct au_vdir_wh *pos;
1facf9fc 32221+
32222+ num = 0;
32223+ n = whlist->nh_num;
32224+ head = whlist->nh_head;
1308ab2a 32225+ for (u = 0; u < n; u++, head++)
c06a8ce3
AM
32226+ hlist_for_each_entry(pos, head, wh_hash)
32227+ if (pos->wh_bindex == btgt && ++num > limit)
1facf9fc 32228+ return 1;
1facf9fc 32229+ return 0;
32230+}
32231+
32232+static struct hlist_head *au_name_hash(struct au_nhash *nhash,
dece6358 32233+ unsigned char *name,
1facf9fc 32234+ unsigned int len)
32235+{
dece6358
AM
32236+ unsigned int v;
32237+ /* const unsigned int magic_bit = 12; */
32238+
1308ab2a 32239+ AuDebugOn(!nhash->nh_num || !nhash->nh_head);
32240+
dece6358 32241+ v = 0;
f0c0a007
AM
32242+ if (len > 8)
32243+ len = 8;
dece6358
AM
32244+ while (len--)
32245+ v += *name++;
32246+ /* v = hash_long(v, magic_bit); */
32247+ v %= nhash->nh_num;
32248+ return nhash->nh_head + v;
32249+}
32250+
32251+static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
32252+ int nlen)
32253+{
32254+ return str->len == nlen && !memcmp(str->name, name, nlen);
1facf9fc 32255+}
32256+
32257+/* returns found or not */
dece6358 32258+int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
1facf9fc 32259+{
32260+ struct hlist_head *head;
c06a8ce3 32261+ struct au_vdir_wh *pos;
1facf9fc 32262+ struct au_vdir_destr *str;
32263+
dece6358 32264+ head = au_name_hash(whlist, name, nlen);
c06a8ce3
AM
32265+ hlist_for_each_entry(pos, head, wh_hash) {
32266+ str = &pos->wh_str;
1facf9fc 32267+ AuDbg("%.*s\n", str->len, str->name);
dece6358
AM
32268+ if (au_nhash_test_name(str, name, nlen))
32269+ return 1;
32270+ }
32271+ return 0;
32272+}
32273+
32274+/* returns found(true) or not */
32275+static int test_known(struct au_nhash *delist, char *name, int nlen)
32276+{
32277+ struct hlist_head *head;
c06a8ce3 32278+ struct au_vdir_dehstr *pos;
dece6358
AM
32279+ struct au_vdir_destr *str;
32280+
32281+ head = au_name_hash(delist, name, nlen);
c06a8ce3
AM
32282+ hlist_for_each_entry(pos, head, hash) {
32283+ str = pos->str;
dece6358
AM
32284+ AuDbg("%.*s\n", str->len, str->name);
32285+ if (au_nhash_test_name(str, name, nlen))
1facf9fc 32286+ return 1;
32287+ }
32288+ return 0;
32289+}
32290+
dece6358
AM
32291+static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
32292+ unsigned char d_type)
32293+{
32294+#ifdef CONFIG_AUFS_SHWH
32295+ wh->wh_ino = ino;
32296+ wh->wh_type = d_type;
32297+#endif
32298+}
32299+
32300+/* ---------------------------------------------------------------------- */
32301+
32302+int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
32303+ unsigned int d_type, aufs_bindex_t bindex,
32304+ unsigned char shwh)
1facf9fc 32305+{
32306+ int err;
32307+ struct au_vdir_destr *str;
32308+ struct au_vdir_wh *wh;
32309+
dece6358 32310+ AuDbg("%.*s\n", nlen, name);
1308ab2a 32311+ AuDebugOn(!whlist->nh_num || !whlist->nh_head);
32312+
1facf9fc 32313+ err = -ENOMEM;
dece6358 32314+ wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
1facf9fc 32315+ if (unlikely(!wh))
32316+ goto out;
32317+
32318+ err = 0;
32319+ wh->wh_bindex = bindex;
dece6358
AM
32320+ if (shwh)
32321+ au_shwh_init_wh(wh, ino, d_type);
1facf9fc 32322+ str = &wh->wh_str;
dece6358
AM
32323+ str->len = nlen;
32324+ memcpy(str->name, name, nlen);
32325+ hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
1facf9fc 32326+ /* smp_mb(); */
32327+
4f0767ce 32328+out:
1facf9fc 32329+ return err;
32330+}
32331+
1facf9fc 32332+static int append_deblk(struct au_vdir *vdir)
32333+{
32334+ int err;
dece6358 32335+ unsigned long ul;
1facf9fc 32336+ const unsigned int deblk_sz = vdir->vd_deblk_sz;
32337+ union au_vdir_deblk_p p, deblk_end;
32338+ unsigned char **o;
32339+
32340+ err = -ENOMEM;
e2f27e51
AM
32341+ o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
32342+ GFP_NOFS, /*may_shrink*/0);
1facf9fc 32343+ if (unlikely(!o))
32344+ goto out;
32345+
32346+ vdir->vd_deblk = o;
32347+ p.deblk = kmalloc(deblk_sz, GFP_NOFS);
32348+ if (p.deblk) {
32349+ ul = vdir->vd_nblk++;
32350+ vdir->vd_deblk[ul] = p.deblk;
32351+ vdir->vd_last.ul = ul;
32352+ vdir->vd_last.p.deblk = p.deblk;
32353+ deblk_end.deblk = p.deblk + deblk_sz;
32354+ err = set_deblk_end(&p, &deblk_end);
32355+ }
32356+
4f0767ce 32357+out:
1facf9fc 32358+ return err;
32359+}
32360+
dece6358
AM
32361+static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
32362+ unsigned int d_type, struct au_nhash *delist)
32363+{
32364+ int err;
32365+ unsigned int sz;
32366+ const unsigned int deblk_sz = vdir->vd_deblk_sz;
32367+ union au_vdir_deblk_p p, *room, deblk_end;
32368+ struct au_vdir_dehstr *dehstr;
32369+
32370+ p.deblk = last_deblk(vdir);
32371+ deblk_end.deblk = p.deblk + deblk_sz;
32372+ room = &vdir->vd_last.p;
32373+ AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
32374+ || !is_deblk_end(room, &deblk_end));
32375+
32376+ sz = calc_size(nlen);
32377+ if (unlikely(sz > deblk_end.deblk - room->deblk)) {
32378+ err = append_deblk(vdir);
32379+ if (unlikely(err))
32380+ goto out;
32381+
32382+ p.deblk = last_deblk(vdir);
32383+ deblk_end.deblk = p.deblk + deblk_sz;
32384+ /* smp_mb(); */
32385+ AuDebugOn(room->deblk != p.deblk);
32386+ }
32387+
32388+ err = -ENOMEM;
4a4d8108 32389+ dehstr = au_cache_alloc_vdir_dehstr();
dece6358
AM
32390+ if (unlikely(!dehstr))
32391+ goto out;
32392+
32393+ dehstr->str = &room->de->de_str;
32394+ hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
32395+ room->de->de_ino = ino;
32396+ room->de->de_type = d_type;
32397+ room->de->de_str.len = nlen;
32398+ memcpy(room->de->de_str.name, name, nlen);
32399+
32400+ err = 0;
32401+ room->deblk += sz;
32402+ if (unlikely(set_deblk_end(room, &deblk_end)))
32403+ err = append_deblk(vdir);
32404+ /* smp_mb(); */
32405+
4f0767ce 32406+out:
dece6358
AM
32407+ return err;
32408+}
32409+
32410+/* ---------------------------------------------------------------------- */
32411+
1c60b727 32412+void au_vdir_free(struct au_vdir *vdir)
dece6358
AM
32413+{
32414+ unsigned char **deblk;
32415+
32416+ deblk = vdir->vd_deblk;
1c60b727 32417+ while (vdir->vd_nblk--)
9f237c51
AM
32418+ au_kfree_try_rcu(*deblk++);
32419+ au_kfree_try_rcu(vdir->vd_deblk);
1c60b727 32420+ au_cache_free_vdir(vdir);
dece6358
AM
32421+}
32422+
1308ab2a 32423+static struct au_vdir *alloc_vdir(struct file *file)
1facf9fc 32424+{
32425+ struct au_vdir *vdir;
1308ab2a 32426+ struct super_block *sb;
1facf9fc 32427+ int err;
32428+
2000de60 32429+ sb = file->f_path.dentry->d_sb;
dece6358
AM
32430+ SiMustAnyLock(sb);
32431+
1facf9fc 32432+ err = -ENOMEM;
32433+ vdir = au_cache_alloc_vdir();
32434+ if (unlikely(!vdir))
32435+ goto out;
32436+
32437+ vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
32438+ if (unlikely(!vdir->vd_deblk))
32439+ goto out_free;
32440+
32441+ vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
1308ab2a 32442+ if (!vdir->vd_deblk_sz) {
79b8bda9 32443+ /* estimate the appropriate size for deblk */
1308ab2a 32444+ vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
4a4d8108 32445+ /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
1308ab2a 32446+ }
1facf9fc 32447+ vdir->vd_nblk = 0;
32448+ vdir->vd_version = 0;
32449+ vdir->vd_jiffy = 0;
32450+ err = append_deblk(vdir);
32451+ if (!err)
32452+ return vdir; /* success */
32453+
9f237c51 32454+ au_kfree_try_rcu(vdir->vd_deblk);
1facf9fc 32455+
4f0767ce 32456+out_free:
1c60b727 32457+ au_cache_free_vdir(vdir);
4f0767ce 32458+out:
1facf9fc 32459+ vdir = ERR_PTR(err);
32460+ return vdir;
32461+}
32462+
32463+static int reinit_vdir(struct au_vdir *vdir)
32464+{
32465+ int err;
32466+ union au_vdir_deblk_p p, deblk_end;
32467+
32468+ while (vdir->vd_nblk > 1) {
9f237c51 32469+ au_kfree_try_rcu(vdir->vd_deblk[vdir->vd_nblk - 1]);
1facf9fc 32470+ /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
32471+ vdir->vd_nblk--;
32472+ }
32473+ p.deblk = vdir->vd_deblk[0];
32474+ deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
32475+ err = set_deblk_end(&p, &deblk_end);
32476+ /* keep vd_dblk_sz */
32477+ vdir->vd_last.ul = 0;
32478+ vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32479+ vdir->vd_version = 0;
32480+ vdir->vd_jiffy = 0;
32481+ /* smp_mb(); */
32482+ return err;
32483+}
32484+
32485+/* ---------------------------------------------------------------------- */
32486+
1facf9fc 32487+#define AuFillVdir_CALLED 1
32488+#define AuFillVdir_WHABLE (1 << 1)
dece6358 32489+#define AuFillVdir_SHWH (1 << 2)
1facf9fc 32490+#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
7f207e10
AM
32491+#define au_fset_fillvdir(flags, name) \
32492+ do { (flags) |= AuFillVdir_##name; } while (0)
32493+#define au_fclr_fillvdir(flags, name) \
32494+ do { (flags) &= ~AuFillVdir_##name; } while (0)
1facf9fc 32495+
dece6358
AM
32496+#ifndef CONFIG_AUFS_SHWH
32497+#undef AuFillVdir_SHWH
32498+#define AuFillVdir_SHWH 0
32499+#endif
32500+
1facf9fc 32501+struct fillvdir_arg {
392086de 32502+ struct dir_context ctx;
1facf9fc 32503+ struct file *file;
32504+ struct au_vdir *vdir;
dece6358
AM
32505+ struct au_nhash delist;
32506+ struct au_nhash whlist;
1facf9fc 32507+ aufs_bindex_t bindex;
32508+ unsigned int flags;
32509+ int err;
32510+};
32511+
2255d0fe 32512+static bool fillvdir(struct dir_context *ctx, const char *__name, int nlen,
1facf9fc 32513+ loff_t offset __maybe_unused, u64 h_ino,
32514+ unsigned int d_type)
32515+{
392086de 32516+ struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
1facf9fc 32517+ char *name = (void *)__name;
32518+ struct super_block *sb;
1facf9fc 32519+ ino_t ino;
dece6358 32520+ const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
1facf9fc 32521+
1facf9fc 32522+ arg->err = 0;
2000de60 32523+ sb = arg->file->f_path.dentry->d_sb;
1facf9fc 32524+ au_fset_fillvdir(arg->flags, CALLED);
32525+ /* smp_mb(); */
dece6358 32526+ if (nlen <= AUFS_WH_PFX_LEN
1facf9fc 32527+ || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
dece6358
AM
32528+ if (test_known(&arg->delist, name, nlen)
32529+ || au_nhash_test_known_wh(&arg->whlist, name, nlen))
32530+ goto out; /* already exists or whiteouted */
1facf9fc 32531+
dece6358 32532+ arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
4a4d8108
AM
32533+ if (!arg->err) {
32534+ if (unlikely(nlen > AUFS_MAX_NAMELEN))
32535+ d_type = DT_UNKNOWN;
dece6358
AM
32536+ arg->err = append_de(arg->vdir, name, nlen, ino,
32537+ d_type, &arg->delist);
4a4d8108 32538+ }
1facf9fc 32539+ } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
32540+ name += AUFS_WH_PFX_LEN;
dece6358
AM
32541+ nlen -= AUFS_WH_PFX_LEN;
32542+ if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
32543+ goto out; /* already whiteouted */
1facf9fc 32544+
acd2b654 32545+ ino = 0; /* just to suppress a warning */
dece6358
AM
32546+ if (shwh)
32547+ arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
32548+ &ino);
4a4d8108
AM
32549+ if (!arg->err) {
32550+ if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
32551+ d_type = DT_UNKNOWN;
1facf9fc 32552+ arg->err = au_nhash_append_wh
dece6358
AM
32553+ (&arg->whlist, name, nlen, ino, d_type,
32554+ arg->bindex, shwh);
4a4d8108 32555+ }
1facf9fc 32556+ }
32557+
4f0767ce 32558+out:
1facf9fc 32559+ if (!arg->err)
32560+ arg->vdir->vd_jiffy = jiffies;
32561+ /* smp_mb(); */
32562+ AuTraceErr(arg->err);
2255d0fe 32563+ return !arg->err;
1facf9fc 32564+}
32565+
dece6358
AM
32566+static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
32567+ struct au_nhash *whlist, struct au_nhash *delist)
32568+{
32569+#ifdef CONFIG_AUFS_SHWH
32570+ int err;
32571+ unsigned int nh, u;
32572+ struct hlist_head *head;
c06a8ce3
AM
32573+ struct au_vdir_wh *pos;
32574+ struct hlist_node *n;
dece6358
AM
32575+ char *p, *o;
32576+ struct au_vdir_destr *destr;
32577+
32578+ AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
32579+
32580+ err = -ENOMEM;
537831f9 32581+ o = p = (void *)__get_free_page(GFP_NOFS);
dece6358
AM
32582+ if (unlikely(!p))
32583+ goto out;
32584+
32585+ err = 0;
32586+ nh = whlist->nh_num;
32587+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32588+ p += AUFS_WH_PFX_LEN;
32589+ for (u = 0; u < nh; u++) {
32590+ head = whlist->nh_head + u;
c06a8ce3
AM
32591+ hlist_for_each_entry_safe(pos, n, head, wh_hash) {
32592+ destr = &pos->wh_str;
dece6358
AM
32593+ memcpy(p, destr->name, destr->len);
32594+ err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
c06a8ce3 32595+ pos->wh_ino, pos->wh_type, delist);
dece6358
AM
32596+ if (unlikely(err))
32597+ break;
32598+ }
32599+ }
32600+
1c60b727 32601+ free_page((unsigned long)o);
dece6358 32602+
4f0767ce 32603+out:
dece6358
AM
32604+ AuTraceErr(err);
32605+ return err;
32606+#else
32607+ return 0;
32608+#endif
32609+}
32610+
1facf9fc 32611+static int au_do_read_vdir(struct fillvdir_arg *arg)
32612+{
32613+ int err;
dece6358 32614+ unsigned int rdhash;
1facf9fc 32615+ loff_t offset;
5afbbe0d 32616+ aufs_bindex_t bbot, bindex, btop;
dece6358 32617+ unsigned char shwh;
1facf9fc 32618+ struct file *hf, *file;
32619+ struct super_block *sb;
32620+
1facf9fc 32621+ file = arg->file;
2000de60 32622+ sb = file->f_path.dentry->d_sb;
dece6358
AM
32623+ SiMustAnyLock(sb);
32624+
32625+ rdhash = au_sbi(sb)->si_rdhash;
1308ab2a 32626+ if (!rdhash)
32627+ rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
dece6358
AM
32628+ err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
32629+ if (unlikely(err))
1facf9fc 32630+ goto out;
dece6358
AM
32631+ err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
32632+ if (unlikely(err))
1facf9fc 32633+ goto out_delist;
32634+
32635+ err = 0;
32636+ arg->flags = 0;
dece6358
AM
32637+ shwh = 0;
32638+ if (au_opt_test(au_mntflags(sb), SHWH)) {
32639+ shwh = 1;
32640+ au_fset_fillvdir(arg->flags, SHWH);
32641+ }
5afbbe0d
AM
32642+ btop = au_fbtop(file);
32643+ bbot = au_fbbot_dir(file);
32644+ for (bindex = btop; !err && bindex <= bbot; bindex++) {
4a4d8108 32645+ hf = au_hf_dir(file, bindex);
1facf9fc 32646+ if (!hf)
32647+ continue;
32648+
32649+ offset = vfsub_llseek(hf, 0, SEEK_SET);
32650+ err = offset;
32651+ if (unlikely(offset))
32652+ break;
32653+
32654+ arg->bindex = bindex;
32655+ au_fclr_fillvdir(arg->flags, WHABLE);
dece6358 32656+ if (shwh
5afbbe0d 32657+ || (bindex != bbot
dece6358 32658+ && au_br_whable(au_sbr_perm(sb, bindex))))
1facf9fc 32659+ au_fset_fillvdir(arg->flags, WHABLE);
32660+ do {
32661+ arg->err = 0;
32662+ au_fclr_fillvdir(arg->flags, CALLED);
32663+ /* smp_mb(); */
392086de 32664+ err = vfsub_iterate_dir(hf, &arg->ctx);
1facf9fc 32665+ if (err >= 0)
32666+ err = arg->err;
32667+ } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
392086de
AM
32668+
32669+ /*
32670+ * dir_relax() may be good for concurrency, but aufs should not
32671+ * use it since it will cause a lockdep problem.
32672+ */
1facf9fc 32673+ }
dece6358
AM
32674+
32675+ if (!err && shwh)
32676+ err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
32677+
32678+ au_nhash_wh_free(&arg->whlist);
1facf9fc 32679+
4f0767ce 32680+out_delist:
dece6358 32681+ au_nhash_de_free(&arg->delist);
4f0767ce 32682+out:
1facf9fc 32683+ return err;
32684+}
32685+
32686+static int read_vdir(struct file *file, int may_read)
32687+{
32688+ int err;
32689+ unsigned long expire;
32690+ unsigned char do_read;
392086de
AM
32691+ struct fillvdir_arg arg = {
32692+ .ctx = {
2000de60 32693+ .actor = fillvdir
392086de
AM
32694+ }
32695+ };
1facf9fc 32696+ struct inode *inode;
32697+ struct au_vdir *vdir, *allocated;
32698+
32699+ err = 0;
c06a8ce3 32700+ inode = file_inode(file);
1facf9fc 32701+ IMustLock(inode);
5afbbe0d 32702+ IiMustWriteLock(inode);
dece6358
AM
32703+ SiMustAnyLock(inode->i_sb);
32704+
1facf9fc 32705+ allocated = NULL;
32706+ do_read = 0;
32707+ expire = au_sbi(inode->i_sb)->si_rdcache;
32708+ vdir = au_ivdir(inode);
32709+ if (!vdir) {
32710+ do_read = 1;
1308ab2a 32711+ vdir = alloc_vdir(file);
1facf9fc 32712+ err = PTR_ERR(vdir);
32713+ if (IS_ERR(vdir))
32714+ goto out;
32715+ err = 0;
32716+ allocated = vdir;
32717+ } else if (may_read
be118d29 32718+ && (!inode_eq_iversion(inode, vdir->vd_version)
1facf9fc 32719+ || time_after(jiffies, vdir->vd_jiffy + expire))) {
32720+ do_read = 1;
32721+ err = reinit_vdir(vdir);
32722+ if (unlikely(err))
32723+ goto out;
32724+ }
32725+
32726+ if (!do_read)
32727+ return 0; /* success */
32728+
32729+ arg.file = file;
32730+ arg.vdir = vdir;
32731+ err = au_do_read_vdir(&arg);
32732+ if (!err) {
392086de 32733+ /* file->f_pos = 0; */ /* todo: ctx->pos? */
be118d29 32734+ vdir->vd_version = inode_query_iversion(inode);
1facf9fc 32735+ vdir->vd_last.ul = 0;
32736+ vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32737+ if (allocated)
32738+ au_set_ivdir(inode, allocated);
32739+ } else if (allocated)
1c60b727 32740+ au_vdir_free(allocated);
1facf9fc 32741+
4f0767ce 32742+out:
1facf9fc 32743+ return err;
32744+}
32745+
32746+static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
32747+{
32748+ int err, rerr;
32749+ unsigned long ul, n;
32750+ const unsigned int deblk_sz = src->vd_deblk_sz;
32751+
32752+ AuDebugOn(tgt->vd_nblk != 1);
32753+
32754+ err = -ENOMEM;
32755+ if (tgt->vd_nblk < src->vd_nblk) {
32756+ unsigned char **p;
32757+
e2f27e51
AM
32758+ p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
32759+ GFP_NOFS, /*may_shrink*/0);
1facf9fc 32760+ if (unlikely(!p))
32761+ goto out;
32762+ tgt->vd_deblk = p;
32763+ }
32764+
1308ab2a 32765+ if (tgt->vd_deblk_sz != deblk_sz) {
32766+ unsigned char *p;
32767+
32768+ tgt->vd_deblk_sz = deblk_sz;
e2f27e51
AM
32769+ p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
32770+ /*may_shrink*/1);
1308ab2a 32771+ if (unlikely(!p))
32772+ goto out;
32773+ tgt->vd_deblk[0] = p;
32774+ }
1facf9fc 32775+ memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
1facf9fc 32776+ tgt->vd_version = src->vd_version;
32777+ tgt->vd_jiffy = src->vd_jiffy;
32778+
32779+ n = src->vd_nblk;
32780+ for (ul = 1; ul < n; ul++) {
dece6358
AM
32781+ tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
32782+ GFP_NOFS);
32783+ if (unlikely(!tgt->vd_deblk[ul]))
1facf9fc 32784+ goto out;
1308ab2a 32785+ tgt->vd_nblk++;
1facf9fc 32786+ }
1308ab2a 32787+ tgt->vd_nblk = n;
32788+ tgt->vd_last.ul = tgt->vd_last.ul;
32789+ tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
32790+ tgt->vd_last.p.deblk += src->vd_last.p.deblk
32791+ - src->vd_deblk[src->vd_last.ul];
1facf9fc 32792+ /* smp_mb(); */
32793+ return 0; /* success */
32794+
4f0767ce 32795+out:
1facf9fc 32796+ rerr = reinit_vdir(tgt);
32797+ BUG_ON(rerr);
32798+ return err;
32799+}
32800+
32801+int au_vdir_init(struct file *file)
32802+{
32803+ int err;
32804+ struct inode *inode;
32805+ struct au_vdir *vdir_cache, *allocated;
32806+
392086de 32807+ /* test file->f_pos here instead of ctx->pos */
1facf9fc 32808+ err = read_vdir(file, !file->f_pos);
32809+ if (unlikely(err))
32810+ goto out;
32811+
32812+ allocated = NULL;
32813+ vdir_cache = au_fvdir_cache(file);
32814+ if (!vdir_cache) {
1308ab2a 32815+ vdir_cache = alloc_vdir(file);
1facf9fc 32816+ err = PTR_ERR(vdir_cache);
32817+ if (IS_ERR(vdir_cache))
32818+ goto out;
32819+ allocated = vdir_cache;
32820+ } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
392086de 32821+ /* test file->f_pos here instead of ctx->pos */
1facf9fc 32822+ err = reinit_vdir(vdir_cache);
32823+ if (unlikely(err))
32824+ goto out;
32825+ } else
32826+ return 0; /* success */
32827+
c06a8ce3 32828+ inode = file_inode(file);
1facf9fc 32829+ err = copy_vdir(vdir_cache, au_ivdir(inode));
32830+ if (!err) {
be118d29 32831+ file->f_version = inode_query_iversion(inode);
1facf9fc 32832+ if (allocated)
32833+ au_set_fvdir_cache(file, allocated);
32834+ } else if (allocated)
1c60b727 32835+ au_vdir_free(allocated);
1facf9fc 32836+
4f0767ce 32837+out:
1facf9fc 32838+ return err;
32839+}
32840+
32841+static loff_t calc_offset(struct au_vdir *vdir)
32842+{
32843+ loff_t offset;
32844+ union au_vdir_deblk_p p;
32845+
32846+ p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
32847+ offset = vdir->vd_last.p.deblk - p.deblk;
32848+ offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
32849+ return offset;
32850+}
32851+
32852+/* returns true or false */
392086de 32853+static int seek_vdir(struct file *file, struct dir_context *ctx)
1facf9fc 32854+{
32855+ int valid;
32856+ unsigned int deblk_sz;
32857+ unsigned long ul, n;
32858+ loff_t offset;
32859+ union au_vdir_deblk_p p, deblk_end;
32860+ struct au_vdir *vdir_cache;
32861+
32862+ valid = 1;
32863+ vdir_cache = au_fvdir_cache(file);
32864+ offset = calc_offset(vdir_cache);
32865+ AuDbg("offset %lld\n", offset);
392086de 32866+ if (ctx->pos == offset)
1facf9fc 32867+ goto out;
32868+
32869+ vdir_cache->vd_last.ul = 0;
32870+ vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
392086de 32871+ if (!ctx->pos)
1facf9fc 32872+ goto out;
32873+
32874+ valid = 0;
32875+ deblk_sz = vdir_cache->vd_deblk_sz;
392086de 32876+ ul = div64_u64(ctx->pos, deblk_sz);
1facf9fc 32877+ AuDbg("ul %lu\n", ul);
32878+ if (ul >= vdir_cache->vd_nblk)
32879+ goto out;
32880+
32881+ n = vdir_cache->vd_nblk;
32882+ for (; ul < n; ul++) {
32883+ p.deblk = vdir_cache->vd_deblk[ul];
32884+ deblk_end.deblk = p.deblk + deblk_sz;
32885+ offset = ul;
32886+ offset *= deblk_sz;
392086de 32887+ while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
1facf9fc 32888+ unsigned int l;
32889+
32890+ l = calc_size(p.de->de_str.len);
32891+ offset += l;
32892+ p.deblk += l;
32893+ }
32894+ if (!is_deblk_end(&p, &deblk_end)) {
32895+ valid = 1;
32896+ vdir_cache->vd_last.ul = ul;
32897+ vdir_cache->vd_last.p = p;
32898+ break;
32899+ }
32900+ }
32901+
4f0767ce 32902+out:
1facf9fc 32903+ /* smp_mb(); */
b00004a5
AM
32904+ if (!valid)
32905+ AuDbg("valid %d\n", !valid);
1facf9fc 32906+ return valid;
32907+}
32908+
392086de 32909+int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
1facf9fc 32910+{
1facf9fc 32911+ unsigned int l, deblk_sz;
32912+ union au_vdir_deblk_p deblk_end;
32913+ struct au_vdir *vdir_cache;
32914+ struct au_vdir_de *de;
32915+
392086de 32916+ if (!seek_vdir(file, ctx))
1facf9fc 32917+ return 0;
32918+
acd2b654 32919+ vdir_cache = au_fvdir_cache(file);
1facf9fc 32920+ deblk_sz = vdir_cache->vd_deblk_sz;
32921+ while (1) {
32922+ deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32923+ deblk_end.deblk += deblk_sz;
32924+ while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
32925+ de = vdir_cache->vd_last.p.de;
32926+ AuDbg("%.*s, off%lld, i%lu, dt%d\n",
392086de 32927+ de->de_str.len, de->de_str.name, ctx->pos,
1facf9fc 32928+ (unsigned long)de->de_ino, de->de_type);
392086de
AM
32929+ if (unlikely(!dir_emit(ctx, de->de_str.name,
32930+ de->de_str.len, de->de_ino,
32931+ de->de_type))) {
1facf9fc 32932+ /* todo: ignore the error caused by udba? */
32933+ /* return err; */
32934+ return 0;
32935+ }
32936+
32937+ l = calc_size(de->de_str.len);
32938+ vdir_cache->vd_last.p.deblk += l;
392086de 32939+ ctx->pos += l;
1facf9fc 32940+ }
32941+ if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
32942+ vdir_cache->vd_last.ul++;
32943+ vdir_cache->vd_last.p.deblk
32944+ = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
392086de 32945+ ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
1facf9fc 32946+ continue;
32947+ }
32948+ break;
32949+ }
32950+
32951+ /* smp_mb(); */
32952+ return 0;
32953+}
7f207e10 32954diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
eca34b5c 32955--- /usr/share/empty/fs/aufs/vfsub.c 1970-01-01 01:00:00.000000000 +0100
631230db 32956+++ linux/fs/aufs/vfsub.c 2023-10-31 09:31:04.199880750 +0100
f4d37d76 32957@@ -0,0 +1,918 @@
cd7a4cd9 32958+// SPDX-License-Identifier: GPL-2.0
1facf9fc 32959+/*
0dcfbb52 32960+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 32961+ *
0dcfbb52 32962+ * This program is free software; you can redistribute it and/or modify
1facf9fc 32963+ * it under the terms of the GNU General Public License as published by
32964+ * the Free Software Foundation; either version 2 of the License, or
32965+ * (at your option) any later version.
dece6358
AM
32966+ *
32967+ * This program is distributed in the hope that it will be useful,
32968+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
32969+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32970+ * GNU General Public License for more details.
32971+ *
32972+ * You should have received a copy of the GNU General Public License
523b37e3 32973+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 32974+ */
32975+
32976+/*
32977+ * sub-routines for VFS
32978+ */
32979+
8b6a4947 32980+#include <linux/mnt_namespace.h>
8cdd5066 32981+#include <linux/nsproxy.h>
dece6358
AM
32982+#include <linux/security.h>
32983+#include <linux/splice.h>
1facf9fc 32984+#include "aufs.h"
32985+
8cdd5066
JR
32986+#ifdef CONFIG_AUFS_BR_FUSE
32987+int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
32988+{
8cdd5066
JR
32989+ if (!au_test_fuse(h_sb) || !au_userns)
32990+ return 0;
32991+
8b6a4947 32992+ return is_current_mnt_ns(mnt) ? 0 : -EACCES;
8cdd5066
JR
32993+}
32994+#endif
32995+
cd233405 32996+int vfsub_sync_filesystem(struct super_block *h_sb)
a2654f78
AM
32997+{
32998+ int err;
32999+
33000+ lockdep_off();
33001+ down_read(&h_sb->s_umount);
cd233405 33002+ err = sync_filesystem(h_sb);
a2654f78
AM
33003+ up_read(&h_sb->s_umount);
33004+ lockdep_on();
33005+
33006+ return err;
33007+}
33008+
8cdd5066
JR
33009+/* ---------------------------------------------------------------------- */
33010+
1facf9fc 33011+int vfsub_update_h_iattr(struct path *h_path, int *did)
33012+{
33013+ int err;
33014+ struct kstat st;
33015+ struct super_block *h_sb;
33016+
758e9dad
AM
33017+ /*
33018+ * Always needs h_path->mnt for LSM or FUSE branch.
33019+ */
33020+ AuDebugOn(!h_path->mnt);
33021+
1facf9fc 33022+ /* for remote fs, leave work for its getattr or d_revalidate */
33023+ /* for bad i_attr fs, handle them in aufs_getattr() */
33024+ /* still some fs may acquire i_mutex. we need to skip them */
33025+ err = 0;
33026+ if (!did)
33027+ did = &err;
33028+ h_sb = h_path->dentry->d_sb;
33029+ *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
33030+ if (*did)
521ced18 33031+ err = vfsub_getattr(h_path, &st);
1facf9fc 33032+
33033+ return err;
33034+}
33035+
33036+/* ---------------------------------------------------------------------- */
33037+
4a4d8108 33038+struct file *vfsub_dentry_open(struct path *path, int flags)
1308ab2a 33039+{
42b5c33a 33040+ return dentry_open(path, flags /* | __FMODE_NONOTIFY */,
7f207e10 33041+ current_cred());
1308ab2a 33042+}
33043+
1facf9fc 33044+struct file *vfsub_filp_open(const char *path, int oflags, int mode)
33045+{
33046+ struct file *file;
33047+
2cbb1c4b 33048+ lockdep_off();
7f207e10 33049+ file = filp_open(path,
2cbb1c4b 33050+ oflags /* | __FMODE_NONOTIFY */,
7f207e10 33051+ mode);
2cbb1c4b 33052+ lockdep_on();
1facf9fc 33053+ if (IS_ERR(file))
33054+ goto out;
33055+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33056+
4f0767ce 33057+out:
1facf9fc 33058+ return file;
33059+}
33060+
b912730e
AM
33061+/*
33062+ * Ideally this function should call VFS:do_last() in order to keep all its
33063+ * checkings. But it is very hard for aufs to regenerate several VFS internal
33064+ * structure such as nameidata. This is a second (or third) best approach.
33065+ * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
33066+ */
33067+int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
acd2b654 33068+ struct vfsub_aopen_args *args)
b912730e
AM
33069+{
33070+ int err;
acd2b654 33071+ struct au_branch *br = args->br;
b912730e
AM
33072+ struct file *file = args->file;
33073+ /* copied from linux/fs/namei.c:atomic_open() */
33074+ struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
33075+
33076+ IMustLock(dir);
33077+ AuDebugOn(!dir->i_op->atomic_open);
33078+
33079+ err = au_br_test_oflag(args->open_flag, br);
33080+ if (unlikely(err))
33081+ goto out;
33082+
acd2b654
AM
33083+ au_lcnt_inc(&br->br_nfiles);
33084+ file->f_path.dentry = DENTRY_NOT_SET;
33085+ file->f_path.mnt = au_br_mnt(br);
33086+ AuDbg("%ps\n", dir->i_op->atomic_open);
b912730e 33087+ err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
acd2b654
AM
33088+ args->create_mode);
33089+ if (unlikely(err < 0)) {
33090+ au_lcnt_dec(&br->br_nfiles);
b912730e 33091+ goto out;
acd2b654 33092+ }
b912730e 33093+
acd2b654
AM
33094+ /* temporary workaround for nfsv4 branch */
33095+ if (au_test_nfs(dir->i_sb))
33096+ nfs_mark_for_revalidate(dir);
b912730e 33097+
acd2b654
AM
33098+ if (file->f_mode & FMODE_CREATED)
33099+ fsnotify_create(dir, dentry);
33100+ if (!(file->f_mode & FMODE_OPENED)) {
33101+ au_lcnt_dec(&br->br_nfiles);
33102+ goto out;
b912730e
AM
33103+ }
33104+
acd2b654
AM
33105+ /* todo: call VFS:may_open() here */
33106+ /* todo: ima_file_check() too? */
33107+ if (!err && (args->open_flag & __FMODE_EXEC))
33108+ err = deny_write_access(file);
33109+ if (!err)
33110+ fsnotify_open(file);
33111+ else
33112+ au_lcnt_dec(&br->br_nfiles);
33113+ /* note that the file is created and still opened */
b912730e
AM
33114+
33115+out:
33116+ return err;
33117+}
33118+
1facf9fc 33119+int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
33120+{
33121+ int err;
33122+
1facf9fc 33123+ err = kern_path(name, flags, path);
5527c038 33124+ if (!err && d_is_positive(path->dentry))
1facf9fc 33125+ vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33126+ return err;
33127+}
33128+
febd17d6 33129+struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
758e9dad 33130+ struct path *ppath, int len)
febd17d6 33131+{
758e9dad 33132+ struct path path;
febd17d6 33133+
758e9dad 33134+ path.dentry = lookup_one_len_unlocked(name, ppath->dentry, len);
febd17d6
JR
33135+ if (IS_ERR(path.dentry))
33136+ goto out;
758e9dad
AM
33137+ if (d_is_positive(path.dentry)) {
33138+ path.mnt = ppath->mnt;
febd17d6 33139+ vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
758e9dad 33140+ }
febd17d6
JR
33141+
33142+out:
33143+ AuTraceErrPtr(path.dentry);
33144+ return path.dentry;
33145+}
33146+
758e9dad 33147+struct dentry *vfsub_lookup_one_len(const char *name, struct path *ppath,
1facf9fc 33148+ int len)
33149+{
758e9dad 33150+ struct path path;
1facf9fc 33151+
1308ab2a 33152+ /* VFS checks it too, but by WARN_ON_ONCE() */
758e9dad 33153+ IMustLock(d_inode(ppath->dentry));
1facf9fc 33154+
758e9dad 33155+ path.dentry = lookup_one_len(name, ppath->dentry, len);
1facf9fc 33156+ if (IS_ERR(path.dentry))
33157+ goto out;
758e9dad
AM
33158+ if (d_is_positive(path.dentry)) {
33159+ path.mnt = ppath->mnt;
1facf9fc 33160+ vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
758e9dad 33161+ }
1facf9fc 33162+
4f0767ce 33163+out:
4a4d8108 33164+ AuTraceErrPtr(path.dentry);
1facf9fc 33165+ return path.dentry;
33166+}
33167+
b4510431 33168+void vfsub_call_lkup_one(void *args)
2cbb1c4b 33169+{
b4510431 33170+ struct vfsub_lkup_one_args *a = args;
758e9dad 33171+ *a->errp = vfsub_lkup_one(a->name, a->ppath);
2cbb1c4b
JR
33172+}
33173+
1facf9fc 33174+/* ---------------------------------------------------------------------- */
33175+
33176+struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33177+ struct dentry *d2, struct au_hinode *hdir2)
33178+{
33179+ struct dentry *d;
33180+
2cbb1c4b 33181+ lockdep_off();
1facf9fc 33182+ d = lock_rename(d1, d2);
2cbb1c4b 33183+ lockdep_on();
4a4d8108 33184+ au_hn_suspend(hdir1);
1facf9fc 33185+ if (hdir1 != hdir2)
4a4d8108 33186+ au_hn_suspend(hdir2);
1facf9fc 33187+
33188+ return d;
33189+}
33190+
33191+void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33192+ struct dentry *d2, struct au_hinode *hdir2)
33193+{
4a4d8108 33194+ au_hn_resume(hdir1);
1facf9fc 33195+ if (hdir1 != hdir2)
4a4d8108 33196+ au_hn_resume(hdir2);
2cbb1c4b 33197+ lockdep_off();
1facf9fc 33198+ unlock_rename(d1, d2);
2cbb1c4b 33199+ lockdep_on();
1facf9fc 33200+}
33201+
33202+/* ---------------------------------------------------------------------- */
33203+
b4510431 33204+int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
1facf9fc 33205+{
33206+ int err;
33207+ struct dentry *d;
5db017eb 33208+ struct mnt_idmap *idmap;
1facf9fc 33209+
33210+ IMustLock(dir);
33211+
33212+ d = path->dentry;
33213+ path->dentry = d->d_parent;
b752ccd1 33214+ err = security_path_mknod(path, d, mode, 0);
1facf9fc 33215+ path->dentry = d;
33216+ if (unlikely(err))
33217+ goto out;
5db017eb 33218+ idmap = mnt_idmap(path->mnt);
1facf9fc 33219+
c1595e42 33220+ lockdep_off();
5db017eb 33221+ err = vfs_create(idmap, dir, path->dentry, mode, want_excl);
c1595e42 33222+ lockdep_on();
1facf9fc 33223+ if (!err) {
33224+ struct path tmp = *path;
33225+ int did;
33226+
33227+ vfsub_update_h_iattr(&tmp, &did);
33228+ if (did) {
33229+ tmp.dentry = path->dentry->d_parent;
33230+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33231+ }
33232+ /*ignore*/
33233+ }
33234+
4f0767ce 33235+out:
1facf9fc 33236+ return err;
33237+}
33238+
33239+int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
33240+{
33241+ int err;
33242+ struct dentry *d;
5db017eb 33243+ struct mnt_idmap *idmap;
1facf9fc 33244+
33245+ IMustLock(dir);
33246+
33247+ d = path->dentry;
33248+ path->dentry = d->d_parent;
b752ccd1 33249+ err = security_path_symlink(path, d, symname);
1facf9fc 33250+ path->dentry = d;
33251+ if (unlikely(err))
33252+ goto out;
5db017eb 33253+ idmap = mnt_idmap(path->mnt);
1facf9fc 33254+
c1595e42 33255+ lockdep_off();
5db017eb 33256+ err = vfs_symlink(idmap, dir, path->dentry, symname);
c1595e42 33257+ lockdep_on();
1facf9fc 33258+ if (!err) {
33259+ struct path tmp = *path;
33260+ int did;
33261+
33262+ vfsub_update_h_iattr(&tmp, &did);
33263+ if (did) {
33264+ tmp.dentry = path->dentry->d_parent;
33265+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33266+ }
33267+ /*ignore*/
33268+ }
33269+
4f0767ce 33270+out:
1facf9fc 33271+ return err;
33272+}
33273+
33274+int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
33275+{
33276+ int err;
33277+ struct dentry *d;
5db017eb 33278+ struct mnt_idmap *idmap;
1facf9fc 33279+
33280+ IMustLock(dir);
33281+
33282+ d = path->dentry;
33283+ path->dentry = d->d_parent;
027c5e7a 33284+ err = security_path_mknod(path, d, mode, new_encode_dev(dev));
1facf9fc 33285+ path->dentry = d;
33286+ if (unlikely(err))
33287+ goto out;
5db017eb 33288+ idmap = mnt_idmap(path->mnt);
1facf9fc 33289+
c1595e42 33290+ lockdep_off();
5db017eb 33291+ err = vfs_mknod(idmap, dir, path->dentry, mode, dev);
c1595e42 33292+ lockdep_on();
1facf9fc 33293+ if (!err) {
33294+ struct path tmp = *path;
33295+ int did;
33296+
33297+ vfsub_update_h_iattr(&tmp, &did);
33298+ if (did) {
33299+ tmp.dentry = path->dentry->d_parent;
33300+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33301+ }
33302+ /*ignore*/
33303+ }
33304+
4f0767ce 33305+out:
1facf9fc 33306+ return err;
33307+}
33308+
33309+static int au_test_nlink(struct inode *inode)
33310+{
33311+ const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
33312+
33313+ if (!au_test_fs_no_limit_nlink(inode->i_sb)
33314+ || inode->i_nlink < link_max)
33315+ return 0;
33316+ return -EMLINK;
33317+}
33318+
523b37e3
AM
33319+int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
33320+ struct inode **delegated_inode)
1facf9fc 33321+{
33322+ int err;
33323+ struct dentry *d;
5db017eb 33324+ struct mnt_idmap *idmap;
1facf9fc 33325+
33326+ IMustLock(dir);
33327+
5527c038 33328+ err = au_test_nlink(d_inode(src_dentry));
1facf9fc 33329+ if (unlikely(err))
33330+ return err;
33331+
b4510431 33332+ /* we don't call may_linkat() */
1facf9fc 33333+ d = path->dentry;
33334+ path->dentry = d->d_parent;
b752ccd1 33335+ err = security_path_link(src_dentry, path, d);
1facf9fc 33336+ path->dentry = d;
33337+ if (unlikely(err))
33338+ goto out;
5db017eb 33339+ idmap = mnt_idmap(path->mnt);
1facf9fc 33340+
2cbb1c4b 33341+ lockdep_off();
5db017eb 33342+ err = vfs_link(src_dentry, idmap, dir, path->dentry, delegated_inode);
2cbb1c4b 33343+ lockdep_on();
1facf9fc 33344+ if (!err) {
33345+ struct path tmp = *path;
33346+ int did;
33347+
33348+ /* fuse has different memory inode for the same inumber */
33349+ vfsub_update_h_iattr(&tmp, &did);
33350+ if (did) {
33351+ tmp.dentry = path->dentry->d_parent;
33352+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33353+ tmp.dentry = src_dentry;
33354+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33355+ }
33356+ /*ignore*/
33357+ }
33358+
4f0767ce 33359+out:
1facf9fc 33360+ return err;
33361+}
33362+
33363+int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
523b37e3 33364+ struct inode *dir, struct path *path,
f2c43d5f 33365+ struct inode **delegated_inode, unsigned int flags)
1facf9fc 33366+{
33367+ int err;
0b2a12c6 33368+ struct renamedata rd;
1facf9fc 33369+ struct path tmp = {
33370+ .mnt = path->mnt
33371+ };
33372+ struct dentry *d;
33373+
33374+ IMustLock(dir);
33375+ IMustLock(src_dir);
33376+
33377+ d = path->dentry;
33378+ path->dentry = d->d_parent;
33379+ tmp.dentry = src_dentry->d_parent;
38d290e6 33380+ err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
1facf9fc 33381+ path->dentry = d;
33382+ if (unlikely(err))
33383+ goto out;
33384+
5db017eb 33385+ rd.old_mnt_idmap = mnt_idmap(path->mnt);
0b2a12c6
JR
33386+ rd.old_dir = src_dir;
33387+ rd.old_dentry = src_dentry;
5db017eb 33388+ rd.new_mnt_idmap = rd.old_mnt_idmap;
0b2a12c6
JR
33389+ rd.new_dir = dir;
33390+ rd.new_dentry = path->dentry;
33391+ rd.delegated_inode = delegated_inode;
33392+ rd.flags = flags;
2cbb1c4b 33393+ lockdep_off();
0b2a12c6 33394+ err = vfs_rename(&rd);
2cbb1c4b 33395+ lockdep_on();
1facf9fc 33396+ if (!err) {
33397+ int did;
33398+
33399+ tmp.dentry = d->d_parent;
33400+ vfsub_update_h_iattr(&tmp, &did);
33401+ if (did) {
33402+ tmp.dentry = src_dentry;
33403+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33404+ tmp.dentry = src_dentry->d_parent;
33405+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33406+ }
33407+ /*ignore*/
33408+ }
33409+
4f0767ce 33410+out:
1facf9fc 33411+ return err;
33412+}
33413+
33414+int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
33415+{
33416+ int err;
33417+ struct dentry *d;
5db017eb 33418+ struct mnt_idmap *idmap;
1facf9fc 33419+
33420+ IMustLock(dir);
33421+
33422+ d = path->dentry;
33423+ path->dentry = d->d_parent;
b752ccd1 33424+ err = security_path_mkdir(path, d, mode);
1facf9fc 33425+ path->dentry = d;
33426+ if (unlikely(err))
33427+ goto out;
5db017eb 33428+ idmap = mnt_idmap(path->mnt);
1facf9fc 33429+
c1595e42 33430+ lockdep_off();
5db017eb 33431+ err = vfs_mkdir(idmap, dir, path->dentry, mode);
c1595e42 33432+ lockdep_on();
1facf9fc 33433+ if (!err) {
33434+ struct path tmp = *path;
33435+ int did;
33436+
33437+ vfsub_update_h_iattr(&tmp, &did);
33438+ if (did) {
33439+ tmp.dentry = path->dentry->d_parent;
33440+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33441+ }
33442+ /*ignore*/
33443+ }
33444+
4f0767ce 33445+out:
1facf9fc 33446+ return err;
33447+}
33448+
33449+int vfsub_rmdir(struct inode *dir, struct path *path)
33450+{
33451+ int err;
33452+ struct dentry *d;
5db017eb 33453+ struct mnt_idmap *idmap;
1facf9fc 33454+
33455+ IMustLock(dir);
33456+
33457+ d = path->dentry;
33458+ path->dentry = d->d_parent;
b752ccd1 33459+ err = security_path_rmdir(path, d);
1facf9fc 33460+ path->dentry = d;
33461+ if (unlikely(err))
33462+ goto out;
5db017eb 33463+ idmap = mnt_idmap(path->mnt);
1facf9fc 33464+
2cbb1c4b 33465+ lockdep_off();
5db017eb 33466+ err = vfs_rmdir(idmap, dir, path->dentry);
2cbb1c4b 33467+ lockdep_on();
1facf9fc 33468+ if (!err) {
33469+ struct path tmp = {
33470+ .dentry = path->dentry->d_parent,
33471+ .mnt = path->mnt
33472+ };
33473+
33474+ vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33475+ }
33476+
4f0767ce 33477+out:
1facf9fc 33478+ return err;
33479+}
33480+
33481+/* ---------------------------------------------------------------------- */
33482+
9dbd164d 33483+/* todo: support mmap_sem? */
1facf9fc 33484+ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33485+ loff_t *ppos)
33486+{
33487+ ssize_t err;
33488+
2cbb1c4b 33489+ lockdep_off();
1facf9fc 33490+ err = vfs_read(file, ubuf, count, ppos);
2cbb1c4b 33491+ lockdep_on();
1facf9fc 33492+ if (err >= 0)
33493+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33494+ return err;
33495+}
33496+
1facf9fc 33497+ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33498+ loff_t *ppos)
33499+{
33500+ ssize_t err;
1facf9fc 33501+
e37dd06a
AM
33502+ lockdep_off();
33503+ err = kernel_read(file, kbuf, count, ppos);
33504+ lockdep_on();
33505+ AuTraceErr(err);
33506+ if (err >= 0)
33507+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
1facf9fc 33508+ return err;
33509+}
33510+
33511+ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33512+ loff_t *ppos)
33513+{
33514+ ssize_t err;
33515+
2cbb1c4b 33516+ lockdep_off();
1facf9fc 33517+ err = vfs_write(file, ubuf, count, ppos);
2cbb1c4b 33518+ lockdep_on();
1facf9fc 33519+ if (err >= 0)
33520+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33521+ return err;
33522+}
33523+
33524+ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
33525+{
33526+ ssize_t err;
1facf9fc 33527+
e37dd06a
AM
33528+ lockdep_off();
33529+ err = kernel_write(file, kbuf, count, ppos);
33530+ lockdep_on();
33531+ if (err >= 0)
33532+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
1facf9fc 33533+ return err;
33534+}
33535+
4a4d8108
AM
33536+int vfsub_flush(struct file *file, fl_owner_t id)
33537+{
33538+ int err;
33539+
33540+ err = 0;
523b37e3 33541+ if (file->f_op->flush) {
2000de60 33542+ if (!au_test_nfs(file->f_path.dentry->d_sb))
2cbb1c4b
JR
33543+ err = file->f_op->flush(file, id);
33544+ else {
33545+ lockdep_off();
33546+ err = file->f_op->flush(file, id);
33547+ lockdep_on();
33548+ }
4a4d8108
AM
33549+ if (!err)
33550+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
33551+ /*ignore*/
33552+ }
33553+ return err;
33554+}
33555+
392086de 33556+int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
1facf9fc 33557+{
33558+ int err;
33559+
062440b3 33560+ AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
392086de 33561+
2cbb1c4b 33562+ lockdep_off();
392086de 33563+ err = iterate_dir(file, ctx);
2cbb1c4b 33564+ lockdep_on();
1facf9fc 33565+ if (err >= 0)
33566+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
1c60b727 33567+
1facf9fc 33568+ return err;
33569+}
33570+
171c7011
AM
33571+long vfsub_splice_read(struct file *in, loff_t *ppos,
33572+ struct pipe_inode_info *pipe, size_t len,
33573+ unsigned int flags)
1facf9fc 33574+{
33575+ long err;
33576+
2cbb1c4b 33577+ lockdep_off();
0a7871ab 33578+ err = vfs_splice_read(in, ppos, pipe, len, flags);
2cbb1c4b 33579+ lockdep_on();
4a4d8108 33580+ file_accessed(in);
1facf9fc 33581+ if (err >= 0)
33582+ vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
33583+ return err;
33584+}
33585+
33586+long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33587+ loff_t *ppos, size_t len, unsigned int flags)
33588+{
33589+ long err;
33590+
2cbb1c4b 33591+ lockdep_off();
0fc653ad 33592+ err = do_splice_from(pipe, out, ppos, len, flags);
2cbb1c4b 33593+ lockdep_on();
1facf9fc 33594+ if (err >= 0)
33595+ vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
33596+ return err;
33597+}
33598+
53392da6
AM
33599+int vfsub_fsync(struct file *file, struct path *path, int datasync)
33600+{
33601+ int err;
33602+
33603+ /* file can be NULL */
33604+ lockdep_off();
33605+ err = vfs_fsync(file, datasync);
33606+ lockdep_on();
33607+ if (!err) {
33608+ if (!path) {
33609+ AuDebugOn(!file);
33610+ path = &file->f_path;
33611+ }
33612+ vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33613+ }
33614+ return err;
33615+}
33616+
1facf9fc 33617+/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
33618+int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33619+ struct file *h_file)
33620+{
33621+ int err;
33622+ struct inode *h_inode;
c06a8ce3 33623+ struct super_block *h_sb;
5db017eb 33624+ struct mnt_idmap *h_idmap;
1facf9fc 33625+
1facf9fc 33626+ if (!h_file) {
c06a8ce3
AM
33627+ err = vfsub_truncate(h_path, length);
33628+ goto out;
1facf9fc 33629+ }
33630+
5527c038 33631+ h_inode = d_inode(h_path->dentry);
c06a8ce3
AM
33632+ h_sb = h_inode->i_sb;
33633+ lockdep_off();
33634+ sb_start_write(h_sb);
33635+ lockdep_on();
aec24f17 33636+ err = security_file_truncate(h_file);
2cbb1c4b 33637+ if (!err) {
5db017eb 33638+ h_idmap = mnt_idmap(h_path->mnt);
2cbb1c4b 33639+ lockdep_off();
5db017eb 33640+ err = do_truncate(h_idmap, h_path->dentry, length, attr,
0b2a12c6 33641+ h_file);
2cbb1c4b
JR
33642+ lockdep_on();
33643+ }
c06a8ce3
AM
33644+ lockdep_off();
33645+ sb_end_write(h_sb);
33646+ lockdep_on();
1facf9fc 33647+
4f0767ce 33648+out:
1facf9fc 33649+ return err;
33650+}
33651+
33652+/* ---------------------------------------------------------------------- */
33653+
33654+struct au_vfsub_mkdir_args {
33655+ int *errp;
33656+ struct inode *dir;
33657+ struct path *path;
33658+ int mode;
33659+};
33660+
33661+static void au_call_vfsub_mkdir(void *args)
33662+{
33663+ struct au_vfsub_mkdir_args *a = args;
33664+ *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
33665+}
33666+
33667+int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
33668+{
33669+ int err, do_sio, wkq_err;
5db017eb 33670+ struct mnt_idmap *idmap;
1facf9fc 33671+
5db017eb
AM
33672+ idmap = mnt_idmap(path->mnt);
33673+ do_sio = au_test_h_perm_sio(idmap, dir, MAY_EXEC | MAY_WRITE);
c1595e42
JR
33674+ if (!do_sio) {
33675+ lockdep_off();
1facf9fc 33676+ err = vfsub_mkdir(dir, path, mode);
c1595e42
JR
33677+ lockdep_on();
33678+ } else {
1facf9fc 33679+ struct au_vfsub_mkdir_args args = {
33680+ .errp = &err,
33681+ .dir = dir,
33682+ .path = path,
33683+ .mode = mode
33684+ };
33685+ wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
33686+ if (unlikely(wkq_err))
33687+ err = wkq_err;
33688+ }
33689+
33690+ return err;
33691+}
33692+
33693+struct au_vfsub_rmdir_args {
33694+ int *errp;
33695+ struct inode *dir;
33696+ struct path *path;
33697+};
33698+
33699+static void au_call_vfsub_rmdir(void *args)
33700+{
33701+ struct au_vfsub_rmdir_args *a = args;
33702+ *a->errp = vfsub_rmdir(a->dir, a->path);
33703+}
33704+
33705+int vfsub_sio_rmdir(struct inode *dir, struct path *path)
33706+{
33707+ int err, do_sio, wkq_err;
5db017eb 33708+ struct mnt_idmap *idmap;
1facf9fc 33709+
5db017eb
AM
33710+ idmap = mnt_idmap(path->mnt);
33711+ do_sio = au_test_h_perm_sio(idmap, dir, MAY_EXEC | MAY_WRITE);
c1595e42
JR
33712+ if (!do_sio) {
33713+ lockdep_off();
1facf9fc 33714+ err = vfsub_rmdir(dir, path);
c1595e42
JR
33715+ lockdep_on();
33716+ } else {
1facf9fc 33717+ struct au_vfsub_rmdir_args args = {
33718+ .errp = &err,
33719+ .dir = dir,
33720+ .path = path
33721+ };
33722+ wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
33723+ if (unlikely(wkq_err))
33724+ err = wkq_err;
33725+ }
33726+
33727+ return err;
33728+}
33729+
33730+/* ---------------------------------------------------------------------- */
33731+
33732+struct notify_change_args {
33733+ int *errp;
33734+ struct path *path;
33735+ struct iattr *ia;
523b37e3 33736+ struct inode **delegated_inode;
1facf9fc 33737+};
33738+
33739+static void call_notify_change(void *args)
33740+{
33741+ struct notify_change_args *a = args;
33742+ struct inode *h_inode;
5db017eb 33743+ struct mnt_idmap *idmap;
1facf9fc 33744+
5527c038 33745+ h_inode = d_inode(a->path->dentry);
1facf9fc 33746+ IMustLock(h_inode);
33747+
33748+ *a->errp = -EPERM;
33749+ if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
5db017eb 33750+ idmap = mnt_idmap(a->path->mnt);
c1595e42 33751+ lockdep_off();
5db017eb 33752+ *a->errp = notify_change(idmap, a->path->dentry, a->ia,
523b37e3 33753+ a->delegated_inode);
c1595e42 33754+ lockdep_on();
1facf9fc 33755+ if (!*a->errp)
33756+ vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
33757+ }
33758+ AuTraceErr(*a->errp);
33759+}
33760+
523b37e3
AM
33761+int vfsub_notify_change(struct path *path, struct iattr *ia,
33762+ struct inode **delegated_inode)
1facf9fc 33763+{
33764+ int err;
33765+ struct notify_change_args args = {
523b37e3
AM
33766+ .errp = &err,
33767+ .path = path,
33768+ .ia = ia,
33769+ .delegated_inode = delegated_inode
1facf9fc 33770+ };
33771+
33772+ call_notify_change(&args);
33773+
33774+ return err;
33775+}
33776+
523b37e3
AM
33777+int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33778+ struct inode **delegated_inode)
1facf9fc 33779+{
33780+ int err, wkq_err;
33781+ struct notify_change_args args = {
523b37e3
AM
33782+ .errp = &err,
33783+ .path = path,
33784+ .ia = ia,
33785+ .delegated_inode = delegated_inode
1facf9fc 33786+ };
33787+
33788+ wkq_err = au_wkq_wait(call_notify_change, &args);
33789+ if (unlikely(wkq_err))
33790+ err = wkq_err;
33791+
33792+ return err;
33793+}
33794+
33795+/* ---------------------------------------------------------------------- */
33796+
33797+struct unlink_args {
33798+ int *errp;
33799+ struct inode *dir;
33800+ struct path *path;
523b37e3 33801+ struct inode **delegated_inode;
1facf9fc 33802+};
33803+
33804+static void call_unlink(void *args)
33805+{
33806+ struct unlink_args *a = args;
33807+ struct dentry *d = a->path->dentry;
33808+ struct inode *h_inode;
5db017eb 33809+ struct mnt_idmap *idmap;
1facf9fc 33810+ const int stop_sillyrename = (au_test_nfs(d->d_sb)
c1595e42 33811+ && au_dcount(d) == 1);
1facf9fc 33812+
33813+ IMustLock(a->dir);
33814+
33815+ a->path->dentry = d->d_parent;
33816+ *a->errp = security_path_unlink(a->path, d);
33817+ a->path->dentry = d;
33818+ if (unlikely(*a->errp))
33819+ return;
33820+
33821+ if (!stop_sillyrename)
33822+ dget(d);
5527c038
JR
33823+ h_inode = NULL;
33824+ if (d_is_positive(d)) {
33825+ h_inode = d_inode(d);
027c5e7a 33826+ ihold(h_inode);
5527c038 33827+ }
1facf9fc 33828+
5db017eb 33829+ idmap = mnt_idmap(a->path->mnt);
2cbb1c4b 33830+ lockdep_off();
5db017eb 33831+ *a->errp = vfs_unlink(idmap, a->dir, d, a->delegated_inode);
2cbb1c4b 33832+ lockdep_on();
1facf9fc 33833+ if (!*a->errp) {
33834+ struct path tmp = {
33835+ .dentry = d->d_parent,
33836+ .mnt = a->path->mnt
33837+ };
33838+ vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33839+ }
33840+
33841+ if (!stop_sillyrename)
33842+ dput(d);
33843+ if (h_inode)
33844+ iput(h_inode);
33845+
33846+ AuTraceErr(*a->errp);
33847+}
33848+
33849+/*
33850+ * @dir: must be locked.
33851+ * @dentry: target dentry.
33852+ */
523b37e3
AM
33853+int vfsub_unlink(struct inode *dir, struct path *path,
33854+ struct inode **delegated_inode, int force)
1facf9fc 33855+{
33856+ int err;
33857+ struct unlink_args args = {
523b37e3
AM
33858+ .errp = &err,
33859+ .dir = dir,
33860+ .path = path,
33861+ .delegated_inode = delegated_inode
1facf9fc 33862+ };
33863+
33864+ if (!force)
33865+ call_unlink(&args);
33866+ else {
33867+ int wkq_err;
33868+
33869+ wkq_err = au_wkq_wait(call_unlink, &args);
33870+ if (unlikely(wkq_err))
33871+ err = wkq_err;
33872+ }
33873+
33874+ return err;
33875+}
7f207e10 33876diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
eca34b5c 33877--- /usr/share/empty/fs/aufs/vfsub.h 1970-01-01 01:00:00.000000000 +0100
631230db
AM
33878+++ linux/fs/aufs/vfsub.h 2023-10-31 09:31:04.199880750 +0100
33879@@ -0,0 +1,403 @@
062440b3 33880+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 33881+/*
0dcfbb52 33882+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 33883+ *
0dcfbb52 33884+ * This program is free software; you can redistribute it and/or modify
1facf9fc 33885+ * it under the terms of the GNU General Public License as published by
33886+ * the Free Software Foundation; either version 2 of the License, or
33887+ * (at your option) any later version.
dece6358
AM
33888+ *
33889+ * This program is distributed in the hope that it will be useful,
33890+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
33891+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33892+ * GNU General Public License for more details.
33893+ *
33894+ * You should have received a copy of the GNU General Public License
523b37e3 33895+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 33896+ */
33897+
33898+/*
33899+ * sub-routines for VFS
33900+ */
33901+
33902+#ifndef __AUFS_VFSUB_H__
33903+#define __AUFS_VFSUB_H__
33904+
33905+#ifdef __KERNEL__
33906+
33907+#include <linux/fs.h>
b4510431 33908+#include <linux/mount.h>
8cdd5066 33909+#include <linux/posix_acl.h>
c1595e42 33910+#include <linux/xattr.h>
7f207e10 33911+#include "debug.h"
1facf9fc 33912+
7f207e10 33913+/* copied from linux/fs/internal.h */
2cbb1c4b 33914+/* todo: BAD approach!! */
c06a8ce3 33915+extern void __mnt_drop_write(struct vfsmount *);
acd2b654 33916+extern struct file *alloc_empty_file(int, const struct cred *);
7f207e10
AM
33917+
33918+/* ---------------------------------------------------------------------- */
1facf9fc 33919+
33920+/* lock subclass for lower inode */
33921+/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
33922+/* reduce? gave up. */
33923+enum {
c1595e42 33924+ AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
1facf9fc 33925+ AuLsc_I_PARENT, /* lower inode, parent first */
33926+ AuLsc_I_PARENT2, /* copyup dirs */
dece6358 33927+ AuLsc_I_PARENT3, /* copyup wh */
1facf9fc 33928+ AuLsc_I_CHILD,
33929+ AuLsc_I_CHILD2,
33930+ AuLsc_I_End
33931+};
33932+
33933+/* to debug easier, do not make them inlined functions */
33934+#define MtxMustLock(mtx) AuDebugOn(!mutex_is_locked(mtx))
febd17d6 33935+#define IMustLock(i) AuDebugOn(!inode_is_locked(i))
1facf9fc 33936+
33937+/* ---------------------------------------------------------------------- */
33938+
7f207e10
AM
33939+static inline void vfsub_drop_nlink(struct inode *inode)
33940+{
33941+ AuDebugOn(!inode->i_nlink);
33942+ drop_nlink(inode);
33943+}
33944+
027c5e7a
AM
33945+static inline void vfsub_dead_dir(struct inode *inode)
33946+{
33947+ AuDebugOn(!S_ISDIR(inode->i_mode));
33948+ inode->i_flags |= S_DEAD;
33949+ clear_nlink(inode);
33950+}
33951+
392086de
AM
33952+static inline int vfsub_native_ro(struct inode *inode)
33953+{
8b6a4947 33954+ return sb_rdonly(inode->i_sb)
392086de
AM
33955+ || IS_RDONLY(inode)
33956+ /* || IS_APPEND(inode) */
33957+ || IS_IMMUTABLE(inode);
33958+}
33959+
8cdd5066
JR
33960+#ifdef CONFIG_AUFS_BR_FUSE
33961+int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
33962+#else
33963+AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
33964+#endif
33965+
cd233405 33966+int vfsub_sync_filesystem(struct super_block *h_sb);
a2654f78 33967+
7f207e10
AM
33968+/* ---------------------------------------------------------------------- */
33969+
33970+int vfsub_update_h_iattr(struct path *h_path, int *did);
33971+struct file *vfsub_dentry_open(struct path *path, int flags);
33972+struct file *vfsub_filp_open(const char *path, int oflags, int mode);
acd2b654 33973+struct au_branch;
b912730e 33974+struct vfsub_aopen_args {
acd2b654
AM
33975+ struct file *file;
33976+ unsigned int open_flag;
33977+ umode_t create_mode;
33978+ struct au_branch *br;
b912730e 33979+};
b912730e 33980+int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
acd2b654 33981+ struct vfsub_aopen_args *args);
1facf9fc 33982+int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
b4510431 33983+
febd17d6 33984+struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
758e9dad
AM
33985+ struct path *ppath, int len);
33986+struct dentry *vfsub_lookup_one_len(const char *name, struct path *ppath,
1facf9fc 33987+ int len);
b4510431
AM
33988+
33989+struct vfsub_lkup_one_args {
33990+ struct dentry **errp;
33991+ struct qstr *name;
758e9dad 33992+ struct path *ppath;
b4510431
AM
33993+};
33994+
33995+static inline struct dentry *vfsub_lkup_one(struct qstr *name,
758e9dad 33996+ struct path *ppath)
b4510431 33997+{
758e9dad 33998+ return vfsub_lookup_one_len(name->name, ppath, name->len);
b4510431
AM
33999+}
34000+
34001+void vfsub_call_lkup_one(void *args);
34002+
34003+/* ---------------------------------------------------------------------- */
34004+
34005+static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
34006+{
34007+ int err;
076b876e 34008+
b4510431
AM
34009+ lockdep_off();
34010+ err = mnt_want_write(mnt);
34011+ lockdep_on();
34012+ return err;
34013+}
34014+
34015+static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
34016+{
34017+ lockdep_off();
34018+ mnt_drop_write(mnt);
34019+ lockdep_on();
34020+}
1facf9fc 34021+
7e9cd9fe 34022+#if 0 /* reserved */
c06a8ce3
AM
34023+static inline void vfsub_mnt_drop_write_file(struct file *file)
34024+{
34025+ lockdep_off();
34026+ mnt_drop_write_file(file);
34027+ lockdep_on();
34028+}
7e9cd9fe 34029+#endif
c06a8ce3 34030+
631230db
AM
34031+static inline void vfsub_file_start_write(struct file *file)
34032+{
34033+ lockdep_off();
34034+ file_start_write(file);
34035+ lockdep_on();
34036+}
34037+
34038+static inline void vfsub_file_end_write(struct file *file)
34039+{
34040+ lockdep_off();
34041+ file_end_write(file);
34042+ lockdep_on();
34043+}
34044+
1facf9fc 34045+/* ---------------------------------------------------------------------- */
34046+
34047+struct au_hinode;
34048+struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
34049+ struct dentry *d2, struct au_hinode *hdir2);
34050+void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
34051+ struct dentry *d2, struct au_hinode *hdir2);
34052+
537831f9
AM
34053+int vfsub_create(struct inode *dir, struct path *path, int mode,
34054+ bool want_excl);
1facf9fc 34055+int vfsub_symlink(struct inode *dir, struct path *path,
34056+ const char *symname);
34057+int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
34058+int vfsub_link(struct dentry *src_dentry, struct inode *dir,
523b37e3 34059+ struct path *path, struct inode **delegated_inode);
1facf9fc 34060+int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
523b37e3 34061+ struct inode *hdir, struct path *path,
f2c43d5f 34062+ struct inode **delegated_inode, unsigned int flags);
1facf9fc 34063+int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
34064+int vfsub_rmdir(struct inode *dir, struct path *path);
34065+
34066+/* ---------------------------------------------------------------------- */
34067+
34068+ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
34069+ loff_t *ppos);
34070+ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
34071+ loff_t *ppos);
34072+ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
34073+ loff_t *ppos);
34074+ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
34075+ loff_t *ppos);
4a4d8108 34076+int vfsub_flush(struct file *file, fl_owner_t id);
392086de
AM
34077+int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
34078+
c06a8ce3
AM
34079+static inline loff_t vfsub_f_size_read(struct file *file)
34080+{
34081+ return i_size_read(file_inode(file));
34082+}
34083+
4a4d8108
AM
34084+static inline unsigned int vfsub_file_flags(struct file *file)
34085+{
34086+ unsigned int flags;
34087+
34088+ spin_lock(&file->f_lock);
34089+ flags = file->f_flags;
34090+ spin_unlock(&file->f_lock);
34091+
34092+ return flags;
34093+}
1308ab2a 34094+
f0c0a007
AM
34095+static inline int vfsub_file_execed(struct file *file)
34096+{
34097+ /* todo: direct access f_flags */
34098+ return !!(vfsub_file_flags(file) & __FMODE_EXEC);
34099+}
34100+
7e9cd9fe 34101+#if 0 /* reserved */
1facf9fc 34102+static inline void vfsub_file_accessed(struct file *h_file)
34103+{
34104+ file_accessed(h_file);
34105+ vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
34106+}
7e9cd9fe 34107+#endif
1facf9fc 34108+
79b8bda9 34109+#if 0 /* reserved */
1facf9fc 34110+static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
34111+ struct dentry *h_dentry)
34112+{
34113+ struct path h_path = {
34114+ .dentry = h_dentry,
34115+ .mnt = h_mnt
34116+ };
92d182d2 34117+ touch_atime(&h_path);
1facf9fc 34118+ vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
34119+}
79b8bda9 34120+#endif
1facf9fc 34121+
631230db 34122+static inline int vfsub_update_time(struct inode *h_inode, int flags)
0c3ec466 34123+{
631230db 34124+ return inode_update_time(h_inode, flags);
0c3ec466
AM
34125+ /* no vfsub_update_h_iattr() since we don't have struct path */
34126+}
34127+
8cdd5066 34128+#ifdef CONFIG_FS_POSIX_ACL
5db017eb 34129+static inline int vfsub_acl_chmod(struct mnt_idmap *h_idmap,
aec24f17 34130+ struct dentry *h_dentry, umode_t h_mode)
8cdd5066
JR
34131+{
34132+ int err;
34133+
5db017eb 34134+ err = posix_acl_chmod(h_idmap, h_dentry, h_mode);
8cdd5066
JR
34135+ if (err == -EOPNOTSUPP)
34136+ err = 0;
34137+ return err;
34138+}
34139+#else
5db017eb 34140+AuStubInt0(vfsub_acl_chmod, struct mnt_idmap *h_idmap,
aec24f17 34141+ struct dentry *h_dentry, umode_t h_mode);
8cdd5066
JR
34142+#endif
34143+
171c7011
AM
34144+long vfsub_splice_read(struct file *in, loff_t *ppos,
34145+ struct pipe_inode_info *pipe, size_t len,
34146+ unsigned int flags);
4a4d8108
AM
34147+long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
34148+ loff_t *ppos, size_t len, unsigned int flags);
c06a8ce3
AM
34149+
34150+static inline long vfsub_truncate(struct path *path, loff_t length)
34151+{
34152+ long err;
076b876e 34153+
c06a8ce3
AM
34154+ lockdep_off();
34155+ err = vfs_truncate(path, length);
34156+ lockdep_on();
34157+ return err;
34158+}
34159+
4a4d8108
AM
34160+int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
34161+ struct file *h_file);
53392da6 34162+int vfsub_fsync(struct file *file, struct path *path, int datasync);
4a4d8108 34163+
521ced18
JR
34164+/*
34165+ * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
34166+ * ioctl.
34167+ */
9f237c51
AM
34168+static inline loff_t vfsub_clone_file_range(struct file *src, struct file *dst,
34169+ loff_t len)
521ced18 34170+{
9f237c51 34171+ loff_t err;
521ced18
JR
34172+
34173+ lockdep_off();
9f237c51 34174+ err = vfs_clone_file_range(src, 0, dst, 0, len, /*remap_flags*/0);
521ced18
JR
34175+ lockdep_on();
34176+
34177+ return err;
34178+}
34179+
34180+/* copy_file_range(2) is a systemcall */
34181+static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
34182+ struct file *dst, loff_t dst_pos,
34183+ size_t len, unsigned int flags)
34184+{
34185+ ssize_t ssz;
34186+
34187+ lockdep_off();
34188+ ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
34189+ lockdep_on();
34190+
34191+ return ssz;
34192+}
34193+
1facf9fc 34194+/* ---------------------------------------------------------------------- */
34195+
34196+static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
34197+{
34198+ loff_t err;
34199+
2cbb1c4b 34200+ lockdep_off();
1facf9fc 34201+ err = vfs_llseek(file, offset, origin);
2cbb1c4b 34202+ lockdep_on();
1facf9fc 34203+ return err;
34204+}
34205+
34206+/* ---------------------------------------------------------------------- */
34207+
4a4d8108
AM
34208+int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
34209+int vfsub_sio_rmdir(struct inode *dir, struct path *path);
523b37e3
AM
34210+int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
34211+ struct inode **delegated_inode);
34212+int vfsub_notify_change(struct path *path, struct iattr *ia,
34213+ struct inode **delegated_inode);
34214+int vfsub_unlink(struct inode *dir, struct path *path,
34215+ struct inode **delegated_inode, int force);
4a4d8108 34216+
521ced18
JR
34217+static inline int vfsub_getattr(const struct path *path, struct kstat *st)
34218+{
34219+ return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
34220+}
34221+
c1595e42
JR
34222+/* ---------------------------------------------------------------------- */
34223+
5db017eb 34224+static inline int vfsub_setxattr(struct mnt_idmap *idmap,
0b2a12c6 34225+ struct dentry *dentry, const char *name,
2255d0fe 34226+ const void *value, size_t size, int flags)
c1595e42
JR
34227+{
34228+ int err;
34229+
34230+ lockdep_off();
5db017eb 34231+ err = vfs_setxattr(idmap, dentry, name, value, size, flags);
c1595e42
JR
34232+ lockdep_on();
34233+
34234+ return err;
34235+}
34236+
5db017eb 34237+static inline int vfsub_removexattr(struct mnt_idmap *idmap,
0b2a12c6 34238+ struct dentry *dentry, const char *name)
c1595e42
JR
34239+{
34240+ int err;
34241+
34242+ lockdep_off();
5db017eb 34243+ err = vfs_removexattr(idmap, dentry, name);
c1595e42
JR
34244+ lockdep_on();
34245+
34246+ return err;
34247+}
34248+
aec24f17 34249+#ifdef CONFIG_FS_POSIX_ACL
5db017eb 34250+static inline int vfsub_set_acl(struct mnt_idmap *idmap,
aec24f17
AM
34251+ struct dentry *dentry, const char *name,
34252+ struct posix_acl *acl)
34253+{
34254+ int err;
34255+
34256+ lockdep_off();
5db017eb 34257+ err = vfs_set_acl(idmap, dentry, name, acl);
aec24f17
AM
34258+ lockdep_on();
34259+
34260+ return err;
34261+}
34262+
5db017eb 34263+static inline int vfsub_remove_acl(struct mnt_idmap *idmap,
aec24f17
AM
34264+ struct dentry *dentry, const char *name)
34265+{
34266+ int err;
34267+
34268+ lockdep_off();
5db017eb 34269+ err = vfs_remove_acl(idmap, dentry, name);
aec24f17
AM
34270+ lockdep_on();
34271+
34272+ return err;
34273+}
34274+#else
5db017eb 34275+AuStubInt0(vfsub_set_acl, struct mnt_idmap *idmap, struct dentry *dentry,
aec24f17 34276+ const char *name, struct posix_acl *acl);
5db017eb 34277+AuStubInt0(vfsub_remove_acl, struct mnt_idmap *idmap,
aec24f17
AM
34278+ struct dentry *dentry, const char *name);
34279+#endif
34280+
1facf9fc 34281+#endif /* __KERNEL__ */
34282+#endif /* __AUFS_VFSUB_H__ */
7f207e10 34283diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
eca34b5c 34284--- /usr/share/empty/fs/aufs/wbr_policy.c 1970-01-01 01:00:00.000000000 +0100
594d0238 34285+++ linux/fs/aufs/wbr_policy.c 2022-11-05 23:02:18.969222617 +0100
cd7a4cd9
AM
34286@@ -0,0 +1,830 @@
34287+// SPDX-License-Identifier: GPL-2.0
1facf9fc 34288+/*
0dcfbb52 34289+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 34290+ *
0dcfbb52 34291+ * This program is free software; you can redistribute it and/or modify
1facf9fc 34292+ * it under the terms of the GNU General Public License as published by
34293+ * the Free Software Foundation; either version 2 of the License, or
34294+ * (at your option) any later version.
dece6358
AM
34295+ *
34296+ * This program is distributed in the hope that it will be useful,
34297+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
34298+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34299+ * GNU General Public License for more details.
34300+ *
34301+ * You should have received a copy of the GNU General Public License
523b37e3 34302+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 34303+ */
34304+
34305+/*
34306+ * policies for selecting one among multiple writable branches
34307+ */
34308+
34309+#include <linux/statfs.h>
34310+#include "aufs.h"
34311+
34312+/* subset of cpup_attr() */
34313+static noinline_for_stack
34314+int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
34315+{
34316+ int err, sbits;
34317+ struct iattr ia;
34318+ struct inode *h_isrc;
34319+
5527c038 34320+ h_isrc = d_inode(h_src);
1facf9fc 34321+ ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
34322+ ia.ia_mode = h_isrc->i_mode;
34323+ ia.ia_uid = h_isrc->i_uid;
34324+ ia.ia_gid = h_isrc->i_gid;
34325+ sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
5527c038 34326+ au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
523b37e3
AM
34327+ /* no delegation since it is just created */
34328+ err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
1facf9fc 34329+
34330+ /* is this nfs only? */
34331+ if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
34332+ ia.ia_valid = ATTR_FORCE | ATTR_MODE;
34333+ ia.ia_mode = h_isrc->i_mode;
523b37e3 34334+ err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
1facf9fc 34335+ }
34336+
34337+ return err;
34338+}
34339+
34340+#define AuCpdown_PARENT_OPQ 1
34341+#define AuCpdown_WHED (1 << 1)
34342+#define AuCpdown_MADE_DIR (1 << 2)
34343+#define AuCpdown_DIROPQ (1 << 3)
34344+#define au_ftest_cpdown(flags, name) ((flags) & AuCpdown_##name)
7f207e10
AM
34345+#define au_fset_cpdown(flags, name) \
34346+ do { (flags) |= AuCpdown_##name; } while (0)
34347+#define au_fclr_cpdown(flags, name) \
34348+ do { (flags) &= ~AuCpdown_##name; } while (0)
1facf9fc 34349+
1facf9fc 34350+static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
c2b27bf2 34351+ unsigned int *flags)
1facf9fc 34352+{
34353+ int err;
34354+ struct dentry *opq_dentry;
34355+
34356+ opq_dentry = au_diropq_create(dentry, bdst);
34357+ err = PTR_ERR(opq_dentry);
34358+ if (IS_ERR(opq_dentry))
34359+ goto out;
34360+ dput(opq_dentry);
c2b27bf2 34361+ au_fset_cpdown(*flags, DIROPQ);
1facf9fc 34362+
4f0767ce 34363+out:
1facf9fc 34364+ return err;
34365+}
34366+
34367+static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
34368+ struct inode *dir, aufs_bindex_t bdst)
34369+{
34370+ int err;
34371+ struct path h_path;
34372+ struct au_branch *br;
34373+
34374+ br = au_sbr(dentry->d_sb, bdst);
34375+ h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
34376+ err = PTR_ERR(h_path.dentry);
34377+ if (IS_ERR(h_path.dentry))
34378+ goto out;
34379+
34380+ err = 0;
5527c038 34381+ if (d_is_positive(h_path.dentry)) {
86dc4139 34382+ h_path.mnt = au_br_mnt(br);
1facf9fc 34383+ err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
34384+ dentry);
34385+ }
34386+ dput(h_path.dentry);
34387+
4f0767ce 34388+out:
1facf9fc 34389+ return err;
34390+}
34391+
34392+static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 34393+ struct au_pin *pin,
1facf9fc 34394+ struct dentry *h_parent, void *arg)
34395+{
34396+ int err, rerr;
5afbbe0d 34397+ aufs_bindex_t bopq, btop;
1facf9fc 34398+ struct path h_path;
34399+ struct dentry *parent;
34400+ struct inode *h_dir, *h_inode, *inode, *dir;
c2b27bf2 34401+ unsigned int *flags = arg;
1facf9fc 34402+
5afbbe0d 34403+ btop = au_dbtop(dentry);
1facf9fc 34404+ /* dentry is di-locked */
34405+ parent = dget_parent(dentry);
5527c038
JR
34406+ dir = d_inode(parent);
34407+ h_dir = d_inode(h_parent);
1facf9fc 34408+ AuDebugOn(h_dir != au_h_iptr(dir, bdst));
34409+ IMustLock(h_dir);
34410+
86dc4139 34411+ err = au_lkup_neg(dentry, bdst, /*wh*/0);
1facf9fc 34412+ if (unlikely(err < 0))
34413+ goto out;
34414+ h_path.dentry = au_h_dptr(dentry, bdst);
34415+ h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
cd7a4cd9 34416+ err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path, 0755);
1facf9fc 34417+ if (unlikely(err))
34418+ goto out_put;
c2b27bf2 34419+ au_fset_cpdown(*flags, MADE_DIR);
1facf9fc 34420+
1facf9fc 34421+ bopq = au_dbdiropq(dentry);
c2b27bf2
AM
34422+ au_fclr_cpdown(*flags, WHED);
34423+ au_fclr_cpdown(*flags, DIROPQ);
1facf9fc 34424+ if (au_dbwh(dentry) == bdst)
c2b27bf2
AM
34425+ au_fset_cpdown(*flags, WHED);
34426+ if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
34427+ au_fset_cpdown(*flags, PARENT_OPQ);
5527c038 34428+ h_inode = d_inode(h_path.dentry);
febd17d6 34429+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
c2b27bf2
AM
34430+ if (au_ftest_cpdown(*flags, WHED)) {
34431+ err = au_cpdown_dir_opq(dentry, bdst, flags);
1facf9fc 34432+ if (unlikely(err)) {
febd17d6 34433+ inode_unlock(h_inode);
1facf9fc 34434+ goto out_dir;
34435+ }
34436+ }
34437+
5afbbe0d 34438+ err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
febd17d6 34439+ inode_unlock(h_inode);
1facf9fc 34440+ if (unlikely(err))
34441+ goto out_opq;
34442+
c2b27bf2 34443+ if (au_ftest_cpdown(*flags, WHED)) {
1facf9fc 34444+ err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
34445+ if (unlikely(err))
34446+ goto out_opq;
34447+ }
34448+
5527c038 34449+ inode = d_inode(dentry);
5afbbe0d
AM
34450+ if (au_ibbot(inode) < bdst)
34451+ au_set_ibbot(inode, bdst);
1facf9fc 34452+ au_set_h_iptr(inode, bdst, au_igrab(h_inode),
34453+ au_hi_flags(inode, /*isdir*/1));
076b876e 34454+ au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
1facf9fc 34455+ goto out; /* success */
34456+
34457+ /* revert */
4f0767ce 34458+out_opq:
c2b27bf2 34459+ if (au_ftest_cpdown(*flags, DIROPQ)) {
febd17d6 34460+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
1facf9fc 34461+ rerr = au_diropq_remove(dentry, bdst);
febd17d6 34462+ inode_unlock(h_inode);
1facf9fc 34463+ if (unlikely(rerr)) {
523b37e3
AM
34464+ AuIOErr("failed removing diropq for %pd b%d (%d)\n",
34465+ dentry, bdst, rerr);
1facf9fc 34466+ err = -EIO;
34467+ goto out;
34468+ }
34469+ }
4f0767ce 34470+out_dir:
c2b27bf2 34471+ if (au_ftest_cpdown(*flags, MADE_DIR)) {
1facf9fc 34472+ rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
34473+ if (unlikely(rerr)) {
523b37e3
AM
34474+ AuIOErr("failed removing %pd b%d (%d)\n",
34475+ dentry, bdst, rerr);
1facf9fc 34476+ err = -EIO;
34477+ }
34478+ }
4f0767ce 34479+out_put:
1facf9fc 34480+ au_set_h_dptr(dentry, bdst, NULL);
5afbbe0d
AM
34481+ if (au_dbbot(dentry) == bdst)
34482+ au_update_dbbot(dentry);
4f0767ce 34483+out:
1facf9fc 34484+ dput(parent);
34485+ return err;
34486+}
34487+
34488+int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
34489+{
34490+ int err;
c2b27bf2 34491+ unsigned int flags;
1facf9fc 34492+
c2b27bf2
AM
34493+ flags = 0;
34494+ err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
1facf9fc 34495+
34496+ return err;
34497+}
34498+
34499+/* ---------------------------------------------------------------------- */
34500+
34501+/* policies for create */
34502+
c2b27bf2 34503+int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
4a4d8108
AM
34504+{
34505+ int err, i, j, ndentry;
34506+ aufs_bindex_t bopq;
34507+ struct au_dcsub_pages dpages;
34508+ struct au_dpage *dpage;
34509+ struct dentry **dentries, *parent, *d;
34510+
34511+ err = au_dpages_init(&dpages, GFP_NOFS);
34512+ if (unlikely(err))
34513+ goto out;
34514+ parent = dget_parent(dentry);
027c5e7a 34515+ err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
4a4d8108
AM
34516+ if (unlikely(err))
34517+ goto out_free;
34518+
34519+ err = bindex;
34520+ for (i = 0; i < dpages.ndpage; i++) {
34521+ dpage = dpages.dpages + i;
34522+ dentries = dpage->dentries;
34523+ ndentry = dpage->ndentry;
34524+ for (j = 0; j < ndentry; j++) {
34525+ d = dentries[j];
34526+ di_read_lock_parent2(d, !AuLock_IR);
34527+ bopq = au_dbdiropq(d);
34528+ di_read_unlock(d, !AuLock_IR);
34529+ if (bopq >= 0 && bopq < err)
34530+ err = bopq;
34531+ }
34532+ }
34533+
34534+out_free:
34535+ dput(parent);
34536+ au_dpages_free(&dpages);
34537+out:
34538+ return err;
34539+}
34540+
1facf9fc 34541+static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
34542+{
34543+ for (; bindex >= 0; bindex--)
34544+ if (!au_br_rdonly(au_sbr(sb, bindex)))
34545+ return bindex;
34546+ return -EROFS;
34547+}
34548+
34549+/* top down parent */
392086de
AM
34550+static int au_wbr_create_tdp(struct dentry *dentry,
34551+ unsigned int flags __maybe_unused)
1facf9fc 34552+{
34553+ int err;
5afbbe0d 34554+ aufs_bindex_t btop, bindex;
1facf9fc 34555+ struct super_block *sb;
34556+ struct dentry *parent, *h_parent;
34557+
34558+ sb = dentry->d_sb;
5afbbe0d
AM
34559+ btop = au_dbtop(dentry);
34560+ err = btop;
34561+ if (!au_br_rdonly(au_sbr(sb, btop)))
1facf9fc 34562+ goto out;
34563+
34564+ err = -EROFS;
34565+ parent = dget_parent(dentry);
5afbbe0d 34566+ for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
1facf9fc 34567+ h_parent = au_h_dptr(parent, bindex);
5527c038 34568+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 34569+ continue;
34570+
34571+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
34572+ err = bindex;
34573+ break;
34574+ }
34575+ }
34576+ dput(parent);
34577+
34578+ /* bottom up here */
4a4d8108 34579+ if (unlikely(err < 0)) {
5afbbe0d 34580+ err = au_wbr_bu(sb, btop - 1);
4a4d8108
AM
34581+ if (err >= 0)
34582+ err = au_wbr_nonopq(dentry, err);
34583+ }
1facf9fc 34584+
4f0767ce 34585+out:
1facf9fc 34586+ AuDbg("b%d\n", err);
34587+ return err;
34588+}
34589+
34590+/* ---------------------------------------------------------------------- */
34591+
34592+/* an exception for the policy other than tdp */
34593+static int au_wbr_create_exp(struct dentry *dentry)
34594+{
34595+ int err;
34596+ aufs_bindex_t bwh, bdiropq;
34597+ struct dentry *parent;
34598+
34599+ err = -1;
34600+ bwh = au_dbwh(dentry);
34601+ parent = dget_parent(dentry);
34602+ bdiropq = au_dbdiropq(parent);
34603+ if (bwh >= 0) {
34604+ if (bdiropq >= 0)
34605+ err = min(bdiropq, bwh);
34606+ else
34607+ err = bwh;
34608+ AuDbg("%d\n", err);
34609+ } else if (bdiropq >= 0) {
34610+ err = bdiropq;
34611+ AuDbg("%d\n", err);
34612+ }
34613+ dput(parent);
34614+
4a4d8108
AM
34615+ if (err >= 0)
34616+ err = au_wbr_nonopq(dentry, err);
34617+
1facf9fc 34618+ if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
34619+ err = -1;
34620+
34621+ AuDbg("%d\n", err);
34622+ return err;
34623+}
34624+
34625+/* ---------------------------------------------------------------------- */
34626+
34627+/* round robin */
34628+static int au_wbr_create_init_rr(struct super_block *sb)
34629+{
34630+ int err;
34631+
5afbbe0d 34632+ err = au_wbr_bu(sb, au_sbbot(sb));
1facf9fc 34633+ atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
dece6358 34634+ /* smp_mb(); */
1facf9fc 34635+
34636+ AuDbg("b%d\n", err);
34637+ return err;
34638+}
34639+
392086de 34640+static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
1facf9fc 34641+{
34642+ int err, nbr;
34643+ unsigned int u;
5afbbe0d 34644+ aufs_bindex_t bindex, bbot;
1facf9fc 34645+ struct super_block *sb;
34646+ atomic_t *next;
34647+
34648+ err = au_wbr_create_exp(dentry);
34649+ if (err >= 0)
34650+ goto out;
34651+
34652+ sb = dentry->d_sb;
34653+ next = &au_sbi(sb)->si_wbr_rr_next;
5afbbe0d
AM
34654+ bbot = au_sbbot(sb);
34655+ nbr = bbot + 1;
34656+ for (bindex = 0; bindex <= bbot; bindex++) {
392086de 34657+ if (!au_ftest_wbr(flags, DIR)) {
1facf9fc 34658+ err = atomic_dec_return(next) + 1;
34659+ /* modulo for 0 is meaningless */
34660+ if (unlikely(!err))
34661+ err = atomic_dec_return(next) + 1;
34662+ } else
34663+ err = atomic_read(next);
34664+ AuDbg("%d\n", err);
34665+ u = err;
34666+ err = u % nbr;
34667+ AuDbg("%d\n", err);
34668+ if (!au_br_rdonly(au_sbr(sb, err)))
34669+ break;
34670+ err = -EROFS;
34671+ }
34672+
4a4d8108
AM
34673+ if (err >= 0)
34674+ err = au_wbr_nonopq(dentry, err);
34675+
4f0767ce 34676+out:
1facf9fc 34677+ AuDbg("%d\n", err);
34678+ return err;
34679+}
34680+
34681+/* ---------------------------------------------------------------------- */
34682+
34683+/* most free space */
392086de 34684+static void au_mfs(struct dentry *dentry, struct dentry *parent)
1facf9fc 34685+{
34686+ struct super_block *sb;
34687+ struct au_branch *br;
34688+ struct au_wbr_mfs *mfs;
392086de 34689+ struct dentry *h_parent;
5afbbe0d 34690+ aufs_bindex_t bindex, bbot;
1facf9fc 34691+ int err;
34692+ unsigned long long b, bavail;
7f207e10 34693+ struct path h_path;
1facf9fc 34694+ /* reduce the stack usage */
34695+ struct kstatfs *st;
34696+
34697+ st = kmalloc(sizeof(*st), GFP_NOFS);
34698+ if (unlikely(!st)) {
34699+ AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
34700+ return;
34701+ }
34702+
34703+ bavail = 0;
34704+ sb = dentry->d_sb;
34705+ mfs = &au_sbi(sb)->si_wbr_mfs;
dece6358 34706+ MtxMustLock(&mfs->mfs_lock);
1facf9fc 34707+ mfs->mfs_bindex = -EROFS;
34708+ mfs->mfsrr_bytes = 0;
392086de
AM
34709+ if (!parent) {
34710+ bindex = 0;
5afbbe0d 34711+ bbot = au_sbbot(sb);
392086de 34712+ } else {
5afbbe0d
AM
34713+ bindex = au_dbtop(parent);
34714+ bbot = au_dbtaildir(parent);
392086de
AM
34715+ }
34716+
5afbbe0d 34717+ for (; bindex <= bbot; bindex++) {
392086de
AM
34718+ if (parent) {
34719+ h_parent = au_h_dptr(parent, bindex);
5527c038 34720+ if (!h_parent || d_is_negative(h_parent))
392086de
AM
34721+ continue;
34722+ }
1facf9fc 34723+ br = au_sbr(sb, bindex);
34724+ if (au_br_rdonly(br))
34725+ continue;
34726+
34727+ /* sb->s_root for NFS is unreliable */
86dc4139 34728+ h_path.mnt = au_br_mnt(br);
7f207e10
AM
34729+ h_path.dentry = h_path.mnt->mnt_root;
34730+ err = vfs_statfs(&h_path, st);
1facf9fc 34731+ if (unlikely(err)) {
34732+ AuWarn1("failed statfs, b%d, %d\n", bindex, err);
34733+ continue;
34734+ }
34735+
34736+ /* when the available size is equal, select the lower one */
34737+ BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
34738+ || sizeof(b) < sizeof(st->f_bsize));
34739+ b = st->f_bavail * st->f_bsize;
34740+ br->br_wbr->wbr_bytes = b;
34741+ if (b >= bavail) {
34742+ bavail = b;
34743+ mfs->mfs_bindex = bindex;
34744+ mfs->mfs_jiffy = jiffies;
34745+ }
34746+ }
34747+
34748+ mfs->mfsrr_bytes = bavail;
34749+ AuDbg("b%d\n", mfs->mfs_bindex);
9f237c51 34750+ au_kfree_rcu(st);
1facf9fc 34751+}
34752+
392086de 34753+static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
1facf9fc 34754+{
34755+ int err;
392086de 34756+ struct dentry *parent;
1facf9fc 34757+ struct super_block *sb;
34758+ struct au_wbr_mfs *mfs;
34759+
34760+ err = au_wbr_create_exp(dentry);
34761+ if (err >= 0)
34762+ goto out;
34763+
34764+ sb = dentry->d_sb;
392086de
AM
34765+ parent = NULL;
34766+ if (au_ftest_wbr(flags, PARENT))
34767+ parent = dget_parent(dentry);
1facf9fc 34768+ mfs = &au_sbi(sb)->si_wbr_mfs;
34769+ mutex_lock(&mfs->mfs_lock);
34770+ if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34771+ || mfs->mfs_bindex < 0
34772+ || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
392086de 34773+ au_mfs(dentry, parent);
1facf9fc 34774+ mutex_unlock(&mfs->mfs_lock);
34775+ err = mfs->mfs_bindex;
392086de 34776+ dput(parent);
1facf9fc 34777+
4a4d8108
AM
34778+ if (err >= 0)
34779+ err = au_wbr_nonopq(dentry, err);
34780+
4f0767ce 34781+out:
1facf9fc 34782+ AuDbg("b%d\n", err);
34783+ return err;
34784+}
34785+
34786+static int au_wbr_create_init_mfs(struct super_block *sb)
34787+{
34788+ struct au_wbr_mfs *mfs;
34789+
34790+ mfs = &au_sbi(sb)->si_wbr_mfs;
34791+ mutex_init(&mfs->mfs_lock);
34792+ mfs->mfs_jiffy = 0;
34793+ mfs->mfs_bindex = -EROFS;
34794+
34795+ return 0;
34796+}
34797+
34798+static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
34799+{
34800+ mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
34801+ return 0;
34802+}
34803+
34804+/* ---------------------------------------------------------------------- */
34805+
f2c43d5f
AM
34806+/* top down regardless parent, and then mfs */
34807+static int au_wbr_create_tdmfs(struct dentry *dentry,
34808+ unsigned int flags __maybe_unused)
34809+{
34810+ int err;
34811+ aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
34812+ unsigned long long watermark;
34813+ struct super_block *sb;
34814+ struct au_wbr_mfs *mfs;
34815+ struct au_branch *br;
34816+ struct dentry *parent;
34817+
34818+ sb = dentry->d_sb;
34819+ mfs = &au_sbi(sb)->si_wbr_mfs;
34820+ mutex_lock(&mfs->mfs_lock);
34821+ if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34822+ || mfs->mfs_bindex < 0)
34823+ au_mfs(dentry, /*parent*/NULL);
34824+ watermark = mfs->mfsrr_watermark;
34825+ bmfs = mfs->mfs_bindex;
34826+ mutex_unlock(&mfs->mfs_lock);
34827+
34828+ /* another style of au_wbr_create_exp() */
34829+ bwh = au_dbwh(dentry);
34830+ parent = dget_parent(dentry);
34831+ btail = au_dbtaildir(parent);
34832+ if (bwh >= 0 && bwh < btail)
34833+ btail = bwh;
34834+
34835+ err = au_wbr_nonopq(dentry, btail);
34836+ if (unlikely(err < 0))
34837+ goto out;
34838+ btail = err;
34839+ bfound = -1;
34840+ for (bindex = 0; bindex <= btail; bindex++) {
34841+ br = au_sbr(sb, bindex);
34842+ if (au_br_rdonly(br))
34843+ continue;
34844+ if (br->br_wbr->wbr_bytes > watermark) {
34845+ bfound = bindex;
34846+ break;
34847+ }
34848+ }
34849+ err = bfound;
34850+ if (err < 0)
34851+ err = bmfs;
34852+
34853+out:
34854+ dput(parent);
34855+ AuDbg("b%d\n", err);
34856+ return err;
34857+}
34858+
34859+/* ---------------------------------------------------------------------- */
34860+
1facf9fc 34861+/* most free space and then round robin */
392086de 34862+static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
1facf9fc 34863+{
34864+ int err;
34865+ struct au_wbr_mfs *mfs;
34866+
392086de 34867+ err = au_wbr_create_mfs(dentry, flags);
1facf9fc 34868+ if (err >= 0) {
34869+ mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
dece6358 34870+ mutex_lock(&mfs->mfs_lock);
1facf9fc 34871+ if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
392086de 34872+ err = au_wbr_create_rr(dentry, flags);
dece6358 34873+ mutex_unlock(&mfs->mfs_lock);
1facf9fc 34874+ }
34875+
34876+ AuDbg("b%d\n", err);
34877+ return err;
34878+}
34879+
34880+static int au_wbr_create_init_mfsrr(struct super_block *sb)
34881+{
34882+ int err;
34883+
34884+ au_wbr_create_init_mfs(sb); /* ignore */
34885+ err = au_wbr_create_init_rr(sb);
34886+
34887+ return err;
34888+}
34889+
34890+/* ---------------------------------------------------------------------- */
34891+
34892+/* top down parent and most free space */
392086de 34893+static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
1facf9fc 34894+{
34895+ int err, e2;
34896+ unsigned long long b;
5afbbe0d 34897+ aufs_bindex_t bindex, btop, bbot;
1facf9fc 34898+ struct super_block *sb;
34899+ struct dentry *parent, *h_parent;
34900+ struct au_branch *br;
34901+
392086de 34902+ err = au_wbr_create_tdp(dentry, flags);
1facf9fc 34903+ if (unlikely(err < 0))
34904+ goto out;
34905+ parent = dget_parent(dentry);
5afbbe0d
AM
34906+ btop = au_dbtop(parent);
34907+ bbot = au_dbtaildir(parent);
34908+ if (btop == bbot)
1facf9fc 34909+ goto out_parent; /* success */
34910+
392086de 34911+ e2 = au_wbr_create_mfs(dentry, flags);
1facf9fc 34912+ if (e2 < 0)
34913+ goto out_parent; /* success */
34914+
34915+ /* when the available size is equal, select upper one */
34916+ sb = dentry->d_sb;
34917+ br = au_sbr(sb, err);
34918+ b = br->br_wbr->wbr_bytes;
34919+ AuDbg("b%d, %llu\n", err, b);
34920+
5afbbe0d 34921+ for (bindex = btop; bindex <= bbot; bindex++) {
1facf9fc 34922+ h_parent = au_h_dptr(parent, bindex);
5527c038 34923+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 34924+ continue;
34925+
34926+ br = au_sbr(sb, bindex);
34927+ if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
34928+ b = br->br_wbr->wbr_bytes;
34929+ err = bindex;
34930+ AuDbg("b%d, %llu\n", err, b);
34931+ }
34932+ }
34933+
4a4d8108
AM
34934+ if (err >= 0)
34935+ err = au_wbr_nonopq(dentry, err);
34936+
4f0767ce 34937+out_parent:
1facf9fc 34938+ dput(parent);
4f0767ce 34939+out:
1facf9fc 34940+ AuDbg("b%d\n", err);
34941+ return err;
34942+}
34943+
34944+/* ---------------------------------------------------------------------- */
34945+
392086de
AM
34946+/*
34947+ * - top down parent
34948+ * - most free space with parent
34949+ * - most free space round-robin regardless parent
34950+ */
34951+static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
34952+{
34953+ int err;
34954+ unsigned long long watermark;
34955+ struct super_block *sb;
34956+ struct au_branch *br;
34957+ struct au_wbr_mfs *mfs;
34958+
34959+ err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
34960+ if (unlikely(err < 0))
34961+ goto out;
34962+
34963+ sb = dentry->d_sb;
34964+ br = au_sbr(sb, err);
34965+ mfs = &au_sbi(sb)->si_wbr_mfs;
34966+ mutex_lock(&mfs->mfs_lock);
34967+ watermark = mfs->mfsrr_watermark;
34968+ mutex_unlock(&mfs->mfs_lock);
34969+ if (br->br_wbr->wbr_bytes < watermark)
34970+ /* regardless the parent dir */
34971+ err = au_wbr_create_mfsrr(dentry, flags);
34972+
34973+out:
34974+ AuDbg("b%d\n", err);
34975+ return err;
34976+}
34977+
34978+/* ---------------------------------------------------------------------- */
34979+
1facf9fc 34980+/* policies for copyup */
34981+
34982+/* top down parent */
34983+static int au_wbr_copyup_tdp(struct dentry *dentry)
34984+{
392086de 34985+ return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
1facf9fc 34986+}
34987+
34988+/* bottom up parent */
34989+static int au_wbr_copyup_bup(struct dentry *dentry)
34990+{
34991+ int err;
5afbbe0d 34992+ aufs_bindex_t bindex, btop;
1facf9fc 34993+ struct dentry *parent, *h_parent;
34994+ struct super_block *sb;
34995+
34996+ err = -EROFS;
34997+ sb = dentry->d_sb;
34998+ parent = dget_parent(dentry);
5afbbe0d
AM
34999+ btop = au_dbtop(parent);
35000+ for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
1facf9fc 35001+ h_parent = au_h_dptr(parent, bindex);
5527c038 35002+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 35003+ continue;
35004+
35005+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
35006+ err = bindex;
35007+ break;
35008+ }
35009+ }
35010+ dput(parent);
35011+
35012+ /* bottom up here */
35013+ if (unlikely(err < 0))
5afbbe0d 35014+ err = au_wbr_bu(sb, btop - 1);
1facf9fc 35015+
35016+ AuDbg("b%d\n", err);
35017+ return err;
35018+}
35019+
35020+/* bottom up */
5afbbe0d 35021+int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
1facf9fc 35022+{
35023+ int err;
35024+
5afbbe0d 35025+ err = au_wbr_bu(dentry->d_sb, btop);
4a4d8108 35026+ AuDbg("b%d\n", err);
5afbbe0d 35027+ if (err > btop)
4a4d8108 35028+ err = au_wbr_nonopq(dentry, err);
1facf9fc 35029+
35030+ AuDbg("b%d\n", err);
35031+ return err;
35032+}
35033+
076b876e
AM
35034+static int au_wbr_copyup_bu(struct dentry *dentry)
35035+{
35036+ int err;
5afbbe0d 35037+ aufs_bindex_t btop;
076b876e 35038+
5afbbe0d
AM
35039+ btop = au_dbtop(dentry);
35040+ err = au_wbr_do_copyup_bu(dentry, btop);
076b876e
AM
35041+ return err;
35042+}
35043+
1facf9fc 35044+/* ---------------------------------------------------------------------- */
35045+
35046+struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
35047+ [AuWbrCopyup_TDP] = {
35048+ .copyup = au_wbr_copyup_tdp
35049+ },
35050+ [AuWbrCopyup_BUP] = {
35051+ .copyup = au_wbr_copyup_bup
35052+ },
35053+ [AuWbrCopyup_BU] = {
35054+ .copyup = au_wbr_copyup_bu
35055+ }
35056+};
35057+
35058+struct au_wbr_create_operations au_wbr_create_ops[] = {
35059+ [AuWbrCreate_TDP] = {
35060+ .create = au_wbr_create_tdp
35061+ },
35062+ [AuWbrCreate_RR] = {
35063+ .create = au_wbr_create_rr,
35064+ .init = au_wbr_create_init_rr
35065+ },
35066+ [AuWbrCreate_MFS] = {
35067+ .create = au_wbr_create_mfs,
35068+ .init = au_wbr_create_init_mfs,
35069+ .fin = au_wbr_create_fin_mfs
35070+ },
35071+ [AuWbrCreate_MFSV] = {
35072+ .create = au_wbr_create_mfs,
35073+ .init = au_wbr_create_init_mfs,
35074+ .fin = au_wbr_create_fin_mfs
35075+ },
35076+ [AuWbrCreate_MFSRR] = {
35077+ .create = au_wbr_create_mfsrr,
35078+ .init = au_wbr_create_init_mfsrr,
35079+ .fin = au_wbr_create_fin_mfs
35080+ },
35081+ [AuWbrCreate_MFSRRV] = {
35082+ .create = au_wbr_create_mfsrr,
35083+ .init = au_wbr_create_init_mfsrr,
35084+ .fin = au_wbr_create_fin_mfs
35085+ },
f2c43d5f
AM
35086+ [AuWbrCreate_TDMFS] = {
35087+ .create = au_wbr_create_tdmfs,
35088+ .init = au_wbr_create_init_mfs,
35089+ .fin = au_wbr_create_fin_mfs
35090+ },
35091+ [AuWbrCreate_TDMFSV] = {
35092+ .create = au_wbr_create_tdmfs,
35093+ .init = au_wbr_create_init_mfs,
35094+ .fin = au_wbr_create_fin_mfs
35095+ },
1facf9fc 35096+ [AuWbrCreate_PMFS] = {
35097+ .create = au_wbr_create_pmfs,
35098+ .init = au_wbr_create_init_mfs,
35099+ .fin = au_wbr_create_fin_mfs
35100+ },
35101+ [AuWbrCreate_PMFSV] = {
35102+ .create = au_wbr_create_pmfs,
35103+ .init = au_wbr_create_init_mfs,
35104+ .fin = au_wbr_create_fin_mfs
392086de
AM
35105+ },
35106+ [AuWbrCreate_PMFSRR] = {
35107+ .create = au_wbr_create_pmfsrr,
35108+ .init = au_wbr_create_init_mfsrr,
35109+ .fin = au_wbr_create_fin_mfs
35110+ },
35111+ [AuWbrCreate_PMFSRRV] = {
35112+ .create = au_wbr_create_pmfsrr,
35113+ .init = au_wbr_create_init_mfsrr,
35114+ .fin = au_wbr_create_fin_mfs
1facf9fc 35115+ }
35116+};
7f207e10 35117diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
eca34b5c 35118--- /usr/share/empty/fs/aufs/whout.c 1970-01-01 01:00:00.000000000 +0100
631230db 35119+++ linux/fs/aufs/whout.c 2023-10-31 09:31:04.199880750 +0100
758e9dad 35120@@ -0,0 +1,1072 @@
cd7a4cd9 35121+// SPDX-License-Identifier: GPL-2.0
1facf9fc 35122+/*
0dcfbb52 35123+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 35124+ *
0dcfbb52 35125+ * This program is free software; you can redistribute it and/or modify
1facf9fc 35126+ * it under the terms of the GNU General Public License as published by
35127+ * the Free Software Foundation; either version 2 of the License, or
35128+ * (at your option) any later version.
dece6358
AM
35129+ *
35130+ * This program is distributed in the hope that it will be useful,
35131+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
35132+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35133+ * GNU General Public License for more details.
35134+ *
35135+ * You should have received a copy of the GNU General Public License
523b37e3 35136+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 35137+ */
35138+
35139+/*
35140+ * whiteout for logical deletion and opaque directory
35141+ */
35142+
1facf9fc 35143+#include "aufs.h"
35144+
cd7a4cd9 35145+#define WH_MASK 0444
1facf9fc 35146+
35147+/*
35148+ * If a directory contains this file, then it is opaque. We start with the
35149+ * .wh. flag so that it is blocked by lookup.
35150+ */
0c3ec466
AM
35151+static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
35152+ sizeof(AUFS_WH_DIROPQ) - 1);
1facf9fc 35153+
35154+/*
35155+ * generate whiteout name, which is NOT terminated by NULL.
35156+ * @name: original d_name.name
35157+ * @len: original d_name.len
35158+ * @wh: whiteout qstr
35159+ * returns zero when succeeds, otherwise error.
35160+ * succeeded value as wh->name should be freed by kfree().
35161+ */
35162+int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
35163+{
35164+ char *p;
35165+
35166+ if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
35167+ return -ENAMETOOLONG;
35168+
35169+ wh->len = name->len + AUFS_WH_PFX_LEN;
35170+ p = kmalloc(wh->len, GFP_NOFS);
35171+ wh->name = p;
35172+ if (p) {
35173+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35174+ memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
35175+ /* smp_mb(); */
35176+ return 0;
35177+ }
35178+ return -ENOMEM;
35179+}
35180+
35181+/* ---------------------------------------------------------------------- */
35182+
35183+/*
758e9dad 35184+ * test if the @wh_name exists under @h_ppath.
1facf9fc 35185+ * @try_sio specifies the necessary of super-io.
35186+ */
5db017eb 35187+int au_wh_test(struct mnt_idmap *h_idmap, struct path *h_ppath,
0b2a12c6 35188+ struct qstr *wh_name, int try_sio)
1facf9fc 35189+{
35190+ int err;
35191+ struct dentry *wh_dentry;
1facf9fc 35192+
1facf9fc 35193+ if (!try_sio)
758e9dad 35194+ wh_dentry = vfsub_lkup_one(wh_name, h_ppath);
1facf9fc 35195+ else
5db017eb 35196+ wh_dentry = au_sio_lkup_one(h_idmap, wh_name, h_ppath);
1facf9fc 35197+ err = PTR_ERR(wh_dentry);
2000de60
JR
35198+ if (IS_ERR(wh_dentry)) {
35199+ if (err == -ENAMETOOLONG)
35200+ err = 0;
1facf9fc 35201+ goto out;
2000de60 35202+ }
1facf9fc 35203+
35204+ err = 0;
5527c038 35205+ if (d_is_negative(wh_dentry))
1facf9fc 35206+ goto out_wh; /* success */
35207+
35208+ err = 1;
7e9cd9fe 35209+ if (d_is_reg(wh_dentry))
1facf9fc 35210+ goto out_wh; /* success */
35211+
35212+ err = -EIO;
523b37e3 35213+ AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
5527c038 35214+ wh_dentry, d_inode(wh_dentry)->i_mode);
1facf9fc 35215+
4f0767ce 35216+out_wh:
1facf9fc 35217+ dput(wh_dentry);
4f0767ce 35218+out:
1facf9fc 35219+ return err;
35220+}
35221+
35222+/*
758e9dad 35223+ * test if the @h_path->dentry sets opaque or not.
1facf9fc 35224+ */
5db017eb 35225+int au_diropq_test(struct mnt_idmap *h_idmap, struct path *h_path)
1facf9fc 35226+{
35227+ int err;
35228+ struct inode *h_dir;
35229+
758e9dad 35230+ h_dir = d_inode(h_path->dentry);
5db017eb
AM
35231+ err = au_wh_test(h_idmap, h_path, &diropq_name,
35232+ au_test_h_perm_sio(h_idmap, h_dir, MAY_EXEC));
1facf9fc 35233+ return err;
35234+}
35235+
35236+/*
35237+ * returns a negative dentry whose name is unique and temporary.
35238+ */
35239+struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
35240+ struct qstr *prefix)
35241+{
1facf9fc 35242+ struct dentry *dentry;
35243+ int i;
027c5e7a 35244+ char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
4a4d8108 35245+ *name, *p;
027c5e7a 35246+ /* strict atomic_t is unnecessary here */
1facf9fc 35247+ static unsigned short cnt;
35248+ struct qstr qs;
758e9dad 35249+ struct path h_ppath;
5db017eb 35250+ struct mnt_idmap *h_idmap;
1facf9fc 35251+
4a4d8108
AM
35252+ BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
35253+
1facf9fc 35254+ name = defname;
027c5e7a
AM
35255+ qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
35256+ if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
1facf9fc 35257+ dentry = ERR_PTR(-ENAMETOOLONG);
4a4d8108 35258+ if (unlikely(qs.len > NAME_MAX))
1facf9fc 35259+ goto out;
35260+ dentry = ERR_PTR(-ENOMEM);
35261+ name = kmalloc(qs.len + 1, GFP_NOFS);
35262+ if (unlikely(!name))
35263+ goto out;
35264+ }
35265+
35266+ /* doubly whiteout-ed */
35267+ memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
35268+ p = name + AUFS_WH_PFX_LEN * 2;
35269+ memcpy(p, prefix->name, prefix->len);
35270+ p += prefix->len;
35271+ *p++ = '.';
4a4d8108 35272+ AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
1facf9fc 35273+
758e9dad
AM
35274+ h_ppath.dentry = h_parent;
35275+ h_ppath.mnt = au_br_mnt(br);
5db017eb 35276+ h_idmap = au_br_idmap(br);
1facf9fc 35277+ qs.name = name;
35278+ for (i = 0; i < 3; i++) {
b752ccd1 35279+ sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
5db017eb 35280+ dentry = au_sio_lkup_one(h_idmap, &qs, &h_ppath);
5527c038 35281+ if (IS_ERR(dentry) || d_is_negative(dentry))
1facf9fc 35282+ goto out_name;
35283+ dput(dentry);
35284+ }
0c3ec466 35285+ /* pr_warn("could not get random name\n"); */
1facf9fc 35286+ dentry = ERR_PTR(-EEXIST);
35287+ AuDbg("%.*s\n", AuLNPair(&qs));
35288+ BUG();
35289+
4f0767ce 35290+out_name:
1facf9fc 35291+ if (name != defname)
9f237c51 35292+ au_kfree_try_rcu(name);
4f0767ce 35293+out:
4a4d8108 35294+ AuTraceErrPtr(dentry);
1facf9fc 35295+ return dentry;
1facf9fc 35296+}
35297+
35298+/*
35299+ * rename the @h_dentry on @br to the whiteouted temporary name.
35300+ */
35301+int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
35302+{
35303+ int err;
35304+ struct path h_path = {
86dc4139 35305+ .mnt = au_br_mnt(br)
1facf9fc 35306+ };
523b37e3 35307+ struct inode *h_dir, *delegated;
1facf9fc 35308+ struct dentry *h_parent;
35309+
35310+ h_parent = h_dentry->d_parent; /* dir inode is locked */
5527c038 35311+ h_dir = d_inode(h_parent);
1facf9fc 35312+ IMustLock(h_dir);
35313+
35314+ h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
35315+ err = PTR_ERR(h_path.dentry);
35316+ if (IS_ERR(h_path.dentry))
35317+ goto out;
35318+
35319+ /* under the same dir, no need to lock_rename() */
523b37e3 35320+ delegated = NULL;
f2c43d5f
AM
35321+ err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
35322+ /*flags*/0);
1facf9fc 35323+ AuTraceErr(err);
523b37e3
AM
35324+ if (unlikely(err == -EWOULDBLOCK)) {
35325+ pr_warn("cannot retry for NFSv4 delegation"
35326+ " for an internal rename\n");
35327+ iput(delegated);
35328+ }
1facf9fc 35329+ dput(h_path.dentry);
35330+
4f0767ce 35331+out:
4a4d8108 35332+ AuTraceErr(err);
1facf9fc 35333+ return err;
35334+}
35335+
35336+/* ---------------------------------------------------------------------- */
35337+/*
35338+ * functions for removing a whiteout
35339+ */
35340+
35341+static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
35342+{
523b37e3
AM
35343+ int err, force;
35344+ struct inode *delegated;
1facf9fc 35345+
35346+ /*
35347+ * forces superio when the dir has a sticky bit.
35348+ * this may be a violation of unix fs semantics.
35349+ */
35350+ force = (h_dir->i_mode & S_ISVTX)
5527c038 35351+ && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
523b37e3
AM
35352+ delegated = NULL;
35353+ err = vfsub_unlink(h_dir, h_path, &delegated, force);
35354+ if (unlikely(err == -EWOULDBLOCK)) {
35355+ pr_warn("cannot retry for NFSv4 delegation"
35356+ " for an internal unlink\n");
35357+ iput(delegated);
35358+ }
35359+ return err;
1facf9fc 35360+}
35361+
35362+int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35363+ struct dentry *dentry)
35364+{
35365+ int err;
35366+
35367+ err = do_unlink_wh(h_dir, h_path);
35368+ if (!err && dentry)
35369+ au_set_dbwh(dentry, -1);
35370+
35371+ return err;
35372+}
35373+
758e9dad 35374+static int unlink_wh_name(struct path *h_ppath, struct qstr *wh)
1facf9fc 35375+{
35376+ int err;
758e9dad 35377+ struct path h_path;
1facf9fc 35378+
35379+ err = 0;
758e9dad 35380+ h_path.dentry = vfsub_lkup_one(wh, h_ppath);
1facf9fc 35381+ if (IS_ERR(h_path.dentry))
35382+ err = PTR_ERR(h_path.dentry);
35383+ else {
758e9dad
AM
35384+ if (d_is_reg(h_path.dentry)) {
35385+ h_path.mnt = h_ppath->mnt;
35386+ err = do_unlink_wh(d_inode(h_ppath->dentry), &h_path);
35387+ }
1facf9fc 35388+ dput(h_path.dentry);
35389+ }
35390+
35391+ return err;
35392+}
35393+
35394+/* ---------------------------------------------------------------------- */
35395+/*
35396+ * initialize/clean whiteout for a branch
35397+ */
35398+
35399+static void au_wh_clean(struct inode *h_dir, struct path *whpath,
35400+ const int isdir)
35401+{
35402+ int err;
523b37e3 35403+ struct inode *delegated;
1facf9fc 35404+
5527c038 35405+ if (d_is_negative(whpath->dentry))
1facf9fc 35406+ return;
35407+
86dc4139
AM
35408+ if (isdir)
35409+ err = vfsub_rmdir(h_dir, whpath);
523b37e3
AM
35410+ else {
35411+ delegated = NULL;
35412+ err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
35413+ if (unlikely(err == -EWOULDBLOCK)) {
35414+ pr_warn("cannot retry for NFSv4 delegation"
35415+ " for an internal unlink\n");
35416+ iput(delegated);
35417+ }
35418+ }
1facf9fc 35419+ if (unlikely(err))
523b37e3
AM
35420+ pr_warn("failed removing %pd (%d), ignored.\n",
35421+ whpath->dentry, err);
1facf9fc 35422+}
35423+
35424+static int test_linkable(struct dentry *h_root)
35425+{
5527c038 35426+ struct inode *h_dir = d_inode(h_root);
1facf9fc 35427+
35428+ if (h_dir->i_op->link)
35429+ return 0;
35430+
523b37e3
AM
35431+ pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
35432+ h_root, au_sbtype(h_root->d_sb));
43982f53 35433+ return -ENOSYS; /* the branch doesn't have its ->link() */
1facf9fc 35434+}
35435+
35436+/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
35437+static int au_whdir(struct inode *h_dir, struct path *path)
35438+{
35439+ int err;
35440+
35441+ err = -EEXIST;
5527c038 35442+ if (d_is_negative(path->dentry)) {
cd7a4cd9 35443+ int mode = 0700;
1facf9fc 35444+
35445+ if (au_test_nfs(path->dentry->d_sb))
cd7a4cd9 35446+ mode |= 0111;
86dc4139 35447+ err = vfsub_mkdir(h_dir, path, mode);
2000de60 35448+ } else if (d_is_dir(path->dentry))
1facf9fc 35449+ err = 0;
35450+ else
523b37e3 35451+ pr_err("unknown %pd exists\n", path->dentry);
1facf9fc 35452+
35453+ return err;
35454+}
35455+
35456+struct au_wh_base {
35457+ const struct qstr *name;
35458+ struct dentry *dentry;
35459+};
35460+
35461+static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
35462+ struct path *h_path)
35463+{
35464+ h_path->dentry = base[AuBrWh_BASE].dentry;
35465+ au_wh_clean(h_dir, h_path, /*isdir*/0);
35466+ h_path->dentry = base[AuBrWh_PLINK].dentry;
35467+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35468+ h_path->dentry = base[AuBrWh_ORPH].dentry;
35469+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35470+}
35471+
35472+/*
35473+ * returns tri-state,
c1595e42 35474+ * minus: error, caller should print the message
acd2b654 35475+ * zero: success
c1595e42 35476+ * plus: error, caller should NOT print the message
1facf9fc 35477+ */
35478+static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
35479+ int do_plink, struct au_wh_base base[],
35480+ struct path *h_path)
35481+{
35482+ int err;
35483+ struct inode *h_dir;
35484+
5527c038 35485+ h_dir = d_inode(h_root);
1facf9fc 35486+ h_path->dentry = base[AuBrWh_BASE].dentry;
35487+ au_wh_clean(h_dir, h_path, /*isdir*/0);
35488+ h_path->dentry = base[AuBrWh_PLINK].dentry;
35489+ if (do_plink) {
35490+ err = test_linkable(h_root);
35491+ if (unlikely(err)) {
35492+ err = 1;
35493+ goto out;
35494+ }
35495+
35496+ err = au_whdir(h_dir, h_path);
35497+ if (unlikely(err))
35498+ goto out;
35499+ wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35500+ } else
35501+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35502+ h_path->dentry = base[AuBrWh_ORPH].dentry;
35503+ err = au_whdir(h_dir, h_path);
35504+ if (unlikely(err))
35505+ goto out;
35506+ wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35507+
4f0767ce 35508+out:
1facf9fc 35509+ return err;
35510+}
35511+
35512+/*
35513+ * for the moment, aufs supports the branch filesystem which does not support
35514+ * link(2). testing on FAT which does not support i_op->setattr() fully either,
35515+ * copyup failed. finally, such filesystem will not be used as the writable
35516+ * branch.
35517+ *
35518+ * returns tri-state, see above.
35519+ */
35520+static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
35521+ int do_plink, struct au_wh_base base[],
35522+ struct path *h_path)
35523+{
35524+ int err;
35525+ struct inode *h_dir;
35526+
1308ab2a 35527+ WbrWhMustWriteLock(wbr);
35528+
1facf9fc 35529+ err = test_linkable(h_root);
35530+ if (unlikely(err)) {
35531+ err = 1;
35532+ goto out;
35533+ }
35534+
35535+ /*
35536+ * todo: should this create be done in /sbin/mount.aufs helper?
35537+ */
35538+ err = -EEXIST;
5527c038
JR
35539+ h_dir = d_inode(h_root);
35540+ if (d_is_negative(base[AuBrWh_BASE].dentry)) {
86dc4139
AM
35541+ h_path->dentry = base[AuBrWh_BASE].dentry;
35542+ err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
7e9cd9fe 35543+ } else if (d_is_reg(base[AuBrWh_BASE].dentry))
1facf9fc 35544+ err = 0;
35545+ else
523b37e3 35546+ pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
1facf9fc 35547+ if (unlikely(err))
35548+ goto out;
35549+
35550+ h_path->dentry = base[AuBrWh_PLINK].dentry;
35551+ if (do_plink) {
35552+ err = au_whdir(h_dir, h_path);
35553+ if (unlikely(err))
35554+ goto out;
35555+ wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35556+ } else
35557+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35558+ wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
35559+
35560+ h_path->dentry = base[AuBrWh_ORPH].dentry;
35561+ err = au_whdir(h_dir, h_path);
35562+ if (unlikely(err))
35563+ goto out;
35564+ wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35565+
4f0767ce 35566+out:
1facf9fc 35567+ return err;
35568+}
35569+
35570+/*
35571+ * initialize the whiteout base file/dir for @br.
35572+ */
86dc4139 35573+int au_wh_init(struct au_branch *br, struct super_block *sb)
1facf9fc 35574+{
35575+ int err, i;
35576+ const unsigned char do_plink
35577+ = !!au_opt_test(au_mntflags(sb), PLINK);
1facf9fc 35578+ struct inode *h_dir;
86dc4139
AM
35579+ struct path path = br->br_path;
35580+ struct dentry *h_root = path.dentry;
1facf9fc 35581+ struct au_wbr *wbr = br->br_wbr;
35582+ static const struct qstr base_name[] = {
0c3ec466
AM
35583+ [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
35584+ sizeof(AUFS_BASE_NAME) - 1),
35585+ [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
35586+ sizeof(AUFS_PLINKDIR_NAME) - 1),
35587+ [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
35588+ sizeof(AUFS_ORPHDIR_NAME) - 1)
1facf9fc 35589+ };
35590+ struct au_wh_base base[] = {
35591+ [AuBrWh_BASE] = {
35592+ .name = base_name + AuBrWh_BASE,
35593+ .dentry = NULL
35594+ },
35595+ [AuBrWh_PLINK] = {
35596+ .name = base_name + AuBrWh_PLINK,
35597+ .dentry = NULL
35598+ },
35599+ [AuBrWh_ORPH] = {
35600+ .name = base_name + AuBrWh_ORPH,
35601+ .dentry = NULL
35602+ }
35603+ };
35604+
1308ab2a 35605+ if (wbr)
35606+ WbrWhMustWriteLock(wbr);
1facf9fc 35607+
1facf9fc 35608+ for (i = 0; i < AuBrWh_Last; i++) {
35609+ /* doubly whiteouted */
35610+ struct dentry *d;
35611+
35612+ d = au_wh_lkup(h_root, (void *)base[i].name, br);
35613+ err = PTR_ERR(d);
35614+ if (IS_ERR(d))
35615+ goto out;
35616+
35617+ base[i].dentry = d;
35618+ AuDebugOn(wbr
35619+ && wbr->wbr_wh[i]
35620+ && wbr->wbr_wh[i] != base[i].dentry);
35621+ }
35622+
35623+ if (wbr)
35624+ for (i = 0; i < AuBrWh_Last; i++) {
35625+ dput(wbr->wbr_wh[i]);
35626+ wbr->wbr_wh[i] = NULL;
35627+ }
35628+
35629+ err = 0;
1e00d052 35630+ if (!au_br_writable(br->br_perm)) {
5527c038 35631+ h_dir = d_inode(h_root);
1facf9fc 35632+ au_wh_init_ro(h_dir, base, &path);
1e00d052 35633+ } else if (!au_br_wh_linkable(br->br_perm)) {
1facf9fc 35634+ err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
35635+ if (err > 0)
35636+ goto out;
35637+ else if (err)
35638+ goto out_err;
1e00d052 35639+ } else {
1facf9fc 35640+ err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
35641+ if (err > 0)
35642+ goto out;
35643+ else if (err)
35644+ goto out_err;
1facf9fc 35645+ }
35646+ goto out; /* success */
35647+
4f0767ce 35648+out_err:
523b37e3
AM
35649+ pr_err("an error(%d) on the writable branch %pd(%s)\n",
35650+ err, h_root, au_sbtype(h_root->d_sb));
4f0767ce 35651+out:
1facf9fc 35652+ for (i = 0; i < AuBrWh_Last; i++)
35653+ dput(base[i].dentry);
35654+ return err;
35655+}
35656+
35657+/* ---------------------------------------------------------------------- */
35658+/*
35659+ * whiteouts are all hard-linked usually.
35660+ * when its link count reaches a ceiling, we create a new whiteout base
35661+ * asynchronously.
35662+ */
35663+
35664+struct reinit_br_wh {
35665+ struct super_block *sb;
35666+ struct au_branch *br;
35667+};
35668+
35669+static void reinit_br_wh(void *arg)
35670+{
35671+ int err;
35672+ aufs_bindex_t bindex;
35673+ struct path h_path;
35674+ struct reinit_br_wh *a = arg;
35675+ struct au_wbr *wbr;
523b37e3 35676+ struct inode *dir, *delegated;
1facf9fc 35677+ struct dentry *h_root;
35678+ struct au_hinode *hdir;
35679+
35680+ err = 0;
35681+ wbr = a->br->br_wbr;
35682+ /* big aufs lock */
35683+ si_noflush_write_lock(a->sb);
35684+ if (!au_br_writable(a->br->br_perm))
35685+ goto out;
35686+ bindex = au_br_index(a->sb, a->br->br_id);
35687+ if (unlikely(bindex < 0))
35688+ goto out;
35689+
1308ab2a 35690+ di_read_lock_parent(a->sb->s_root, AuLock_IR);
5527c038 35691+ dir = d_inode(a->sb->s_root);
1facf9fc 35692+ hdir = au_hi(dir, bindex);
35693+ h_root = au_h_dptr(a->sb->s_root, bindex);
86dc4139 35694+ AuDebugOn(h_root != au_br_dentry(a->br));
1facf9fc 35695+
5afbbe0d 35696+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 35697+ wbr_wh_write_lock(wbr);
35698+ err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
35699+ h_root, a->br);
35700+ if (!err) {
86dc4139
AM
35701+ h_path.dentry = wbr->wbr_whbase;
35702+ h_path.mnt = au_br_mnt(a->br);
523b37e3
AM
35703+ delegated = NULL;
35704+ err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
35705+ /*force*/0);
35706+ if (unlikely(err == -EWOULDBLOCK)) {
35707+ pr_warn("cannot retry for NFSv4 delegation"
35708+ " for an internal unlink\n");
35709+ iput(delegated);
35710+ }
1facf9fc 35711+ } else {
523b37e3 35712+ pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
1facf9fc 35713+ err = 0;
35714+ }
35715+ dput(wbr->wbr_whbase);
35716+ wbr->wbr_whbase = NULL;
35717+ if (!err)
86dc4139 35718+ err = au_wh_init(a->br, a->sb);
1facf9fc 35719+ wbr_wh_write_unlock(wbr);
5afbbe0d 35720+ au_hn_inode_unlock(hdir);
1308ab2a 35721+ di_read_unlock(a->sb->s_root, AuLock_IR);
076b876e
AM
35722+ if (!err)
35723+ au_fhsm_wrote(a->sb, bindex, /*force*/0);
1facf9fc 35724+
4f0767ce 35725+out:
1facf9fc 35726+ if (wbr)
35727+ atomic_dec(&wbr->wbr_wh_running);
acd2b654 35728+ au_lcnt_dec(&a->br->br_count);
1facf9fc 35729+ si_write_unlock(a->sb);
027c5e7a 35730+ au_nwt_done(&au_sbi(a->sb)->si_nowait);
9f237c51 35731+ au_kfree_rcu(a);
1facf9fc 35732+ if (unlikely(err))
35733+ AuIOErr("err %d\n", err);
35734+}
35735+
35736+static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
35737+{
35738+ int do_dec, wkq_err;
35739+ struct reinit_br_wh *arg;
35740+
35741+ do_dec = 1;
35742+ if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
35743+ goto out;
35744+
35745+ /* ignore ENOMEM */
35746+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
35747+ if (arg) {
35748+ /*
35749+ * dec(wh_running), kfree(arg) and dec(br_count)
35750+ * in reinit function
35751+ */
35752+ arg->sb = sb;
35753+ arg->br = br;
acd2b654 35754+ au_lcnt_inc(&br->br_count);
53392da6 35755+ wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
1facf9fc 35756+ if (unlikely(wkq_err)) {
35757+ atomic_dec(&br->br_wbr->wbr_wh_running);
acd2b654 35758+ au_lcnt_dec(&br->br_count);
9f237c51 35759+ au_kfree_rcu(arg);
1facf9fc 35760+ }
35761+ do_dec = 0;
35762+ }
35763+
4f0767ce 35764+out:
1facf9fc 35765+ if (do_dec)
35766+ atomic_dec(&br->br_wbr->wbr_wh_running);
35767+}
35768+
35769+/* ---------------------------------------------------------------------- */
35770+
35771+/*
35772+ * create the whiteout @wh.
35773+ */
35774+static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
35775+ struct dentry *wh)
35776+{
35777+ int err;
35778+ struct path h_path = {
35779+ .dentry = wh
35780+ };
35781+ struct au_branch *br;
35782+ struct au_wbr *wbr;
35783+ struct dentry *h_parent;
523b37e3 35784+ struct inode *h_dir, *delegated;
1facf9fc 35785+
35786+ h_parent = wh->d_parent; /* dir inode is locked */
5527c038 35787+ h_dir = d_inode(h_parent);
1facf9fc 35788+ IMustLock(h_dir);
35789+
35790+ br = au_sbr(sb, bindex);
86dc4139 35791+ h_path.mnt = au_br_mnt(br);
1facf9fc 35792+ wbr = br->br_wbr;
35793+ wbr_wh_read_lock(wbr);
35794+ if (wbr->wbr_whbase) {
523b37e3
AM
35795+ delegated = NULL;
35796+ err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
35797+ if (unlikely(err == -EWOULDBLOCK)) {
35798+ pr_warn("cannot retry for NFSv4 delegation"
35799+ " for an internal link\n");
35800+ iput(delegated);
35801+ }
1facf9fc 35802+ if (!err || err != -EMLINK)
35803+ goto out;
35804+
35805+ /* link count full. re-initialize br_whbase. */
35806+ kick_reinit_br_wh(sb, br);
35807+ }
35808+
35809+ /* return this error in this context */
b4510431 35810+ err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
076b876e
AM
35811+ if (!err)
35812+ au_fhsm_wrote(sb, bindex, /*force*/0);
1facf9fc 35813+
4f0767ce 35814+out:
1facf9fc 35815+ wbr_wh_read_unlock(wbr);
35816+ return err;
35817+}
35818+
35819+/* ---------------------------------------------------------------------- */
35820+
35821+/*
35822+ * create or remove the diropq.
35823+ */
35824+static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
35825+ unsigned int flags)
35826+{
758e9dad 35827+ struct dentry *opq_dentry;
1facf9fc 35828+ struct super_block *sb;
35829+ struct au_branch *br;
758e9dad 35830+ struct path h_path;
1facf9fc 35831+ int err;
35832+
35833+ sb = dentry->d_sb;
35834+ br = au_sbr(sb, bindex);
758e9dad
AM
35835+ h_path.dentry = au_h_dptr(dentry, bindex);
35836+ h_path.mnt = au_br_mnt(br);
35837+ opq_dentry = vfsub_lkup_one(&diropq_name, &h_path);
1facf9fc 35838+ if (IS_ERR(opq_dentry))
35839+ goto out;
35840+
35841+ if (au_ftest_diropq(flags, CREATE)) {
35842+ err = link_or_create_wh(sb, bindex, opq_dentry);
35843+ if (!err) {
35844+ au_set_dbdiropq(dentry, bindex);
35845+ goto out; /* success */
35846+ }
35847+ } else {
758e9dad
AM
35848+ h_path.dentry = opq_dentry;
35849+ err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &h_path);
1facf9fc 35850+ if (!err)
35851+ au_set_dbdiropq(dentry, -1);
35852+ }
35853+ dput(opq_dentry);
35854+ opq_dentry = ERR_PTR(err);
35855+
4f0767ce 35856+out:
1facf9fc 35857+ return opq_dentry;
35858+}
35859+
35860+struct do_diropq_args {
35861+ struct dentry **errp;
35862+ struct dentry *dentry;
35863+ aufs_bindex_t bindex;
35864+ unsigned int flags;
35865+};
35866+
35867+static void call_do_diropq(void *args)
35868+{
35869+ struct do_diropq_args *a = args;
35870+ *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
35871+}
35872+
35873+struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35874+ unsigned int flags)
35875+{
35876+ struct dentry *diropq, *h_dentry;
5db017eb 35877+ struct mnt_idmap *h_idmap;
1facf9fc 35878+
5db017eb 35879+ h_idmap = au_sbr_idmap(dentry->d_sb, bindex);
1facf9fc 35880+ h_dentry = au_h_dptr(dentry, bindex);
5db017eb 35881+ if (!au_test_h_perm_sio(h_idmap, d_inode(h_dentry),
0b2a12c6 35882+ MAY_EXEC | MAY_WRITE))
1facf9fc 35883+ diropq = do_diropq(dentry, bindex, flags);
35884+ else {
35885+ int wkq_err;
35886+ struct do_diropq_args args = {
35887+ .errp = &diropq,
35888+ .dentry = dentry,
35889+ .bindex = bindex,
35890+ .flags = flags
35891+ };
35892+
35893+ wkq_err = au_wkq_wait(call_do_diropq, &args);
35894+ if (unlikely(wkq_err))
35895+ diropq = ERR_PTR(wkq_err);
35896+ }
35897+
35898+ return diropq;
35899+}
35900+
35901+/* ---------------------------------------------------------------------- */
35902+
35903+/*
35904+ * lookup whiteout dentry.
35905+ * @h_parent: lower parent dentry which must exist and be locked
35906+ * @base_name: name of dentry which will be whiteouted
35907+ * returns dentry for whiteout.
35908+ */
35909+struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35910+ struct au_branch *br)
35911+{
35912+ int err;
35913+ struct qstr wh_name;
35914+ struct dentry *wh_dentry;
758e9dad 35915+ struct path h_path;
1facf9fc 35916+
35917+ err = au_wh_name_alloc(&wh_name, base_name);
35918+ wh_dentry = ERR_PTR(err);
35919+ if (!err) {
758e9dad
AM
35920+ h_path.dentry = h_parent;
35921+ h_path.mnt = au_br_mnt(br);
35922+ wh_dentry = vfsub_lkup_one(&wh_name, &h_path);
9f237c51 35923+ au_kfree_try_rcu(wh_name.name);
1facf9fc 35924+ }
35925+ return wh_dentry;
35926+}
35927+
35928+/*
35929+ * link/create a whiteout for @dentry on @bindex.
35930+ */
35931+struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35932+ struct dentry *h_parent)
35933+{
35934+ struct dentry *wh_dentry;
35935+ struct super_block *sb;
35936+ int err;
35937+
35938+ sb = dentry->d_sb;
35939+ wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
5527c038 35940+ if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
1facf9fc 35941+ err = link_or_create_wh(sb, bindex, wh_dentry);
076b876e 35942+ if (!err) {
1facf9fc 35943+ au_set_dbwh(dentry, bindex);
076b876e
AM
35944+ au_fhsm_wrote(sb, bindex, /*force*/0);
35945+ } else {
1facf9fc 35946+ dput(wh_dentry);
35947+ wh_dentry = ERR_PTR(err);
35948+ }
35949+ }
35950+
35951+ return wh_dentry;
35952+}
35953+
35954+/* ---------------------------------------------------------------------- */
35955+
35956+/* Delete all whiteouts in this directory on branch bindex. */
758e9dad
AM
35957+static int del_wh_children(struct path *h_path, struct au_nhash *whlist,
35958+ aufs_bindex_t bindex)
1facf9fc 35959+{
35960+ int err;
35961+ unsigned long ul, n;
35962+ struct qstr wh_name;
35963+ char *p;
35964+ struct hlist_head *head;
c06a8ce3 35965+ struct au_vdir_wh *pos;
1facf9fc 35966+ struct au_vdir_destr *str;
35967+
35968+ err = -ENOMEM;
537831f9 35969+ p = (void *)__get_free_page(GFP_NOFS);
1facf9fc 35970+ wh_name.name = p;
35971+ if (unlikely(!wh_name.name))
35972+ goto out;
35973+
35974+ err = 0;
35975+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35976+ p += AUFS_WH_PFX_LEN;
35977+ n = whlist->nh_num;
35978+ head = whlist->nh_head;
35979+ for (ul = 0; !err && ul < n; ul++, head++) {
c06a8ce3
AM
35980+ hlist_for_each_entry(pos, head, wh_hash) {
35981+ if (pos->wh_bindex != bindex)
1facf9fc 35982+ continue;
35983+
c06a8ce3 35984+ str = &pos->wh_str;
1facf9fc 35985+ if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
35986+ memcpy(p, str->name, str->len);
35987+ wh_name.len = AUFS_WH_PFX_LEN + str->len;
758e9dad 35988+ err = unlink_wh_name(h_path, &wh_name);
1facf9fc 35989+ if (!err)
35990+ continue;
35991+ break;
35992+ }
35993+ AuIOErr("whiteout name too long %.*s\n",
35994+ str->len, str->name);
35995+ err = -EIO;
35996+ break;
35997+ }
35998+ }
1c60b727 35999+ free_page((unsigned long)wh_name.name);
1facf9fc 36000+
4f0767ce 36001+out:
1facf9fc 36002+ return err;
36003+}
36004+
36005+struct del_wh_children_args {
36006+ int *errp;
758e9dad 36007+ struct path *h_path;
1308ab2a 36008+ struct au_nhash *whlist;
1facf9fc 36009+ aufs_bindex_t bindex;
1facf9fc 36010+};
36011+
36012+static void call_del_wh_children(void *args)
36013+{
36014+ struct del_wh_children_args *a = args;
758e9dad 36015+ *a->errp = del_wh_children(a->h_path, a->whlist, a->bindex);
1facf9fc 36016+}
36017+
36018+/* ---------------------------------------------------------------------- */
36019+
36020+struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
36021+{
36022+ struct au_whtmp_rmdir *whtmp;
dece6358 36023+ int err;
1308ab2a 36024+ unsigned int rdhash;
dece6358
AM
36025+
36026+ SiMustAnyLock(sb);
1facf9fc 36027+
be52b249 36028+ whtmp = kzalloc(sizeof(*whtmp), gfp);
dece6358
AM
36029+ if (unlikely(!whtmp)) {
36030+ whtmp = ERR_PTR(-ENOMEM);
1facf9fc 36031+ goto out;
dece6358 36032+ }
1facf9fc 36033+
1308ab2a 36034+ /* no estimation for dir size */
36035+ rdhash = au_sbi(sb)->si_rdhash;
36036+ if (!rdhash)
36037+ rdhash = AUFS_RDHASH_DEF;
36038+ err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
36039+ if (unlikely(err)) {
9f237c51 36040+ au_kfree_rcu(whtmp);
1308ab2a 36041+ whtmp = ERR_PTR(err);
36042+ }
dece6358 36043+
4f0767ce 36044+out:
dece6358 36045+ return whtmp;
1facf9fc 36046+}
36047+
36048+void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
36049+{
027c5e7a 36050+ if (whtmp->br)
acd2b654 36051+ au_lcnt_dec(&whtmp->br->br_count);
1facf9fc 36052+ dput(whtmp->wh_dentry);
36053+ iput(whtmp->dir);
dece6358 36054+ au_nhash_wh_free(&whtmp->whlist);
9f237c51 36055+ au_kfree_rcu(whtmp);
1facf9fc 36056+}
36057+
36058+/*
36059+ * rmdir the whiteouted temporary named dir @h_dentry.
36060+ * @whlist: whiteouted children.
36061+ */
36062+int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
36063+ struct dentry *wh_dentry, struct au_nhash *whlist)
36064+{
36065+ int err;
2000de60 36066+ unsigned int h_nlink;
758e9dad 36067+ struct path wh_path;
1facf9fc 36068+ struct inode *wh_inode, *h_dir;
36069+ struct au_branch *br;
5db017eb 36070+ struct mnt_idmap *h_idmap;
1facf9fc 36071+
5527c038 36072+ h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
1facf9fc 36073+ IMustLock(h_dir);
36074+
36075+ br = au_sbr(dir->i_sb, bindex);
758e9dad
AM
36076+ wh_path.dentry = wh_dentry;
36077+ wh_path.mnt = au_br_mnt(br);
5db017eb 36078+ h_idmap = au_br_idmap(br);
5527c038 36079+ wh_inode = d_inode(wh_dentry);
febd17d6 36080+ inode_lock_nested(wh_inode, AuLsc_I_CHILD);
1facf9fc 36081+
36082+ /*
36083+ * someone else might change some whiteouts while we were sleeping.
36084+ * it means this whlist may have an obsoleted entry.
36085+ */
5db017eb 36086+ if (!au_test_h_perm_sio(h_idmap, wh_inode, MAY_EXEC | MAY_WRITE))
758e9dad 36087+ err = del_wh_children(&wh_path, whlist, bindex);
1facf9fc 36088+ else {
36089+ int wkq_err;
36090+ struct del_wh_children_args args = {
36091+ .errp = &err,
758e9dad 36092+ .h_path = &wh_path,
1308ab2a 36093+ .whlist = whlist,
758e9dad 36094+ .bindex = bindex
1facf9fc 36095+ };
36096+
36097+ wkq_err = au_wkq_wait(call_del_wh_children, &args);
36098+ if (unlikely(wkq_err))
36099+ err = wkq_err;
36100+ }
febd17d6 36101+ inode_unlock(wh_inode);
1facf9fc 36102+
36103+ if (!err) {
2000de60 36104+ h_nlink = h_dir->i_nlink;
758e9dad 36105+ err = vfsub_rmdir(h_dir, &wh_path);
2000de60
JR
36106+ /* some fs doesn't change the parent nlink in some cases */
36107+ h_nlink -= h_dir->i_nlink;
1facf9fc 36108+ }
36109+
36110+ if (!err) {
5afbbe0d 36111+ if (au_ibtop(dir) == bindex) {
7f207e10 36112+ /* todo: dir->i_mutex is necessary */
1facf9fc 36113+ au_cpup_attr_timesizes(dir);
2000de60
JR
36114+ if (h_nlink)
36115+ vfsub_drop_nlink(dir);
1facf9fc 36116+ }
36117+ return 0; /* success */
36118+ }
36119+
523b37e3 36120+ pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
1facf9fc 36121+ return err;
36122+}
36123+
36124+static void call_rmdir_whtmp(void *args)
36125+{
36126+ int err;
e49829fe 36127+ aufs_bindex_t bindex;
1facf9fc 36128+ struct au_whtmp_rmdir *a = args;
36129+ struct super_block *sb;
36130+ struct dentry *h_parent;
36131+ struct inode *h_dir;
1facf9fc 36132+ struct au_hinode *hdir;
36133+
36134+ /* rmdir by nfsd may cause deadlock with this i_mutex */
febd17d6 36135+ /* inode_lock(a->dir); */
e49829fe 36136+ err = -EROFS;
1facf9fc 36137+ sb = a->dir->i_sb;
e49829fe
JR
36138+ si_read_lock(sb, !AuLock_FLUSH);
36139+ if (!au_br_writable(a->br->br_perm))
36140+ goto out;
36141+ bindex = au_br_index(sb, a->br->br_id);
36142+ if (unlikely(bindex < 0))
1facf9fc 36143+ goto out;
36144+
36145+ err = -EIO;
1facf9fc 36146+ ii_write_lock_parent(a->dir);
36147+ h_parent = dget_parent(a->wh_dentry);
5527c038 36148+ h_dir = d_inode(h_parent);
e49829fe 36149+ hdir = au_hi(a->dir, bindex);
86dc4139
AM
36150+ err = vfsub_mnt_want_write(au_br_mnt(a->br));
36151+ if (unlikely(err))
36152+ goto out_mnt;
5afbbe0d 36153+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
e49829fe
JR
36154+ err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
36155+ a->br);
86dc4139
AM
36156+ if (!err)
36157+ err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
5afbbe0d 36158+ au_hn_inode_unlock(hdir);
86dc4139
AM
36159+ vfsub_mnt_drop_write(au_br_mnt(a->br));
36160+
36161+out_mnt:
1facf9fc 36162+ dput(h_parent);
36163+ ii_write_unlock(a->dir);
4f0767ce 36164+out:
febd17d6 36165+ /* inode_unlock(a->dir); */
1facf9fc 36166+ au_whtmp_rmdir_free(a);
027c5e7a
AM
36167+ si_read_unlock(sb);
36168+ au_nwt_done(&au_sbi(sb)->si_nowait);
1facf9fc 36169+ if (unlikely(err))
36170+ AuIOErr("err %d\n", err);
36171+}
36172+
36173+void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
36174+ struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
36175+{
36176+ int wkq_err;
e49829fe 36177+ struct super_block *sb;
1facf9fc 36178+
36179+ IMustLock(dir);
36180+
36181+ /* all post-process will be done in do_rmdir_whtmp(). */
e49829fe 36182+ sb = dir->i_sb;
1facf9fc 36183+ args->dir = au_igrab(dir);
e49829fe 36184+ args->br = au_sbr(sb, bindex);
acd2b654 36185+ au_lcnt_inc(&args->br->br_count);
1facf9fc 36186+ args->wh_dentry = dget(wh_dentry);
53392da6 36187+ wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
1facf9fc 36188+ if (unlikely(wkq_err)) {
523b37e3 36189+ pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
1facf9fc 36190+ au_whtmp_rmdir_free(args);
36191+ }
36192+}
7f207e10 36193diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
eca34b5c 36194--- /usr/share/empty/fs/aufs/whout.h 1970-01-01 01:00:00.000000000 +0100
631230db 36195+++ linux/fs/aufs/whout.h 2023-10-31 09:31:04.199880750 +0100
0b2a12c6 36196@@ -0,0 +1,87 @@
062440b3 36197+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 36198+/*
0dcfbb52 36199+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 36200+ *
0dcfbb52 36201+ * This program is free software; you can redistribute it and/or modify
1facf9fc 36202+ * it under the terms of the GNU General Public License as published by
36203+ * the Free Software Foundation; either version 2 of the License, or
36204+ * (at your option) any later version.
dece6358
AM
36205+ *
36206+ * This program is distributed in the hope that it will be useful,
36207+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36208+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36209+ * GNU General Public License for more details.
36210+ *
36211+ * You should have received a copy of the GNU General Public License
523b37e3 36212+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 36213+ */
36214+
36215+/*
36216+ * whiteout for logical deletion and opaque directory
36217+ */
36218+
36219+#ifndef __AUFS_WHOUT_H__
36220+#define __AUFS_WHOUT_H__
36221+
36222+#ifdef __KERNEL__
36223+
1facf9fc 36224+#include "dir.h"
36225+
36226+/* whout.c */
36227+int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
5db017eb 36228+int au_wh_test(struct mnt_idmap *h_idmap, struct path *h_ppath,
0b2a12c6 36229+ struct qstr *wh_name, int try_sio);
5db017eb 36230+int au_diropq_test(struct mnt_idmap *h_idmap, struct path *h_path);
3c1bdaff 36231+struct au_branch;
1facf9fc 36232+struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
36233+ struct qstr *prefix);
36234+int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
36235+int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
36236+ struct dentry *dentry);
86dc4139 36237+int au_wh_init(struct au_branch *br, struct super_block *sb);
1facf9fc 36238+
36239+/* diropq flags */
36240+#define AuDiropq_CREATE 1
36241+#define au_ftest_diropq(flags, name) ((flags) & AuDiropq_##name)
7f207e10
AM
36242+#define au_fset_diropq(flags, name) \
36243+ do { (flags) |= AuDiropq_##name; } while (0)
36244+#define au_fclr_diropq(flags, name) \
36245+ do { (flags) &= ~AuDiropq_##name; } while (0)
1facf9fc 36246+
36247+struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
36248+ unsigned int flags);
36249+struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
36250+ struct au_branch *br);
36251+struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
36252+ struct dentry *h_parent);
36253+
36254+/* real rmdir for the whiteout-ed dir */
36255+struct au_whtmp_rmdir {
36256+ struct inode *dir;
e49829fe 36257+ struct au_branch *br;
1facf9fc 36258+ struct dentry *wh_dentry;
dece6358 36259+ struct au_nhash whlist;
1facf9fc 36260+};
36261+
36262+struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
36263+void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
36264+int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
36265+ struct dentry *wh_dentry, struct au_nhash *whlist);
36266+void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
36267+ struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
36268+
36269+/* ---------------------------------------------------------------------- */
36270+
36271+static inline struct dentry *au_diropq_create(struct dentry *dentry,
36272+ aufs_bindex_t bindex)
36273+{
36274+ return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
36275+}
36276+
36277+static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
36278+{
36279+ return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
36280+}
36281+
36282+#endif /* __KERNEL__ */
36283+#endif /* __AUFS_WHOUT_H__ */
7f207e10 36284diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
eca34b5c 36285--- /usr/share/empty/fs/aufs/wkq.c 1970-01-01 01:00:00.000000000 +0100
594d0238 36286+++ linux/fs/aufs/wkq.c 2022-11-05 23:02:18.972555950 +0100
43982f53 36287@@ -0,0 +1,372 @@
cd7a4cd9 36288+// SPDX-License-Identifier: GPL-2.0
1facf9fc 36289+/*
0dcfbb52 36290+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 36291+ *
0dcfbb52 36292+ * This program is free software; you can redistribute it and/or modify
1facf9fc 36293+ * it under the terms of the GNU General Public License as published by
36294+ * the Free Software Foundation; either version 2 of the License, or
36295+ * (at your option) any later version.
dece6358
AM
36296+ *
36297+ * This program is distributed in the hope that it will be useful,
36298+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36299+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36300+ * GNU General Public License for more details.
36301+ *
36302+ * You should have received a copy of the GNU General Public License
523b37e3 36303+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 36304+ */
36305+
36306+/*
36307+ * workqueue for asynchronous/super-io operations
acd2b654 36308+ * todo: try new credential scheme
1facf9fc 36309+ */
36310+
dece6358 36311+#include <linux/module.h>
1facf9fc 36312+#include "aufs.h"
36313+
9dbd164d 36314+/* internal workqueue named AUFS_WKQ_NAME */
b752ccd1 36315+
9dbd164d 36316+static struct workqueue_struct *au_wkq;
1facf9fc 36317+
36318+struct au_wkinfo {
36319+ struct work_struct wk;
7f207e10 36320+ struct kobject *kobj;
1facf9fc 36321+
36322+ unsigned int flags; /* see wkq.h */
36323+
36324+ au_wkq_func_t func;
36325+ void *args;
36326+
8b6a4947
AM
36327+#ifdef CONFIG_LOCKDEP
36328+ int dont_check;
36329+ struct held_lock **hlock;
36330+#endif
36331+
1facf9fc 36332+ struct completion *comp;
36333+};
36334+
36335+/* ---------------------------------------------------------------------- */
8b6a4947
AM
36336+/*
36337+ * Aufs passes some operations to the workqueue such as the internal copyup.
36338+ * This scheme looks rather unnatural for LOCKDEP debugging feature, since the
36339+ * job run by workqueue depends upon the locks acquired in the other task.
36340+ * Delegating a small operation to the workqueue, aufs passes its lockdep
36341+ * information too. And the job in the workqueue restores the info in order to
36342+ * pretend as if it acquired those locks. This is just to make LOCKDEP work
36343+ * correctly and expectedly.
36344+ */
36345+
36346+#ifndef CONFIG_LOCKDEP
36347+AuStubInt0(au_wkq_lockdep_alloc, struct au_wkinfo *wkinfo);
36348+AuStubVoid(au_wkq_lockdep_free, struct au_wkinfo *wkinfo);
36349+AuStubVoid(au_wkq_lockdep_pre, struct au_wkinfo *wkinfo);
36350+AuStubVoid(au_wkq_lockdep_post, struct au_wkinfo *wkinfo);
36351+AuStubVoid(au_wkq_lockdep_init, struct au_wkinfo *wkinfo);
36352+#else
36353+static void au_wkq_lockdep_init(struct au_wkinfo *wkinfo)
36354+{
36355+ wkinfo->hlock = NULL;
36356+ wkinfo->dont_check = 0;
36357+}
36358+
36359+/*
36360+ * 1: matched
36361+ * 0: unmatched
36362+ */
36363+static int au_wkq_lockdep_test(struct lock_class_key *key, const char *name)
36364+{
36365+ static DEFINE_SPINLOCK(spin);
36366+ static struct {
36367+ char *name;
36368+ struct lock_class_key *key;
36369+ } a[] = {
36370+ { .name = "&sbinfo->si_rwsem" },
36371+ { .name = "&finfo->fi_rwsem" },
36372+ { .name = "&dinfo->di_rwsem" },
36373+ { .name = "&iinfo->ii_rwsem" }
36374+ };
36375+ static int set;
36376+ int i;
36377+
36378+ /* lockless read from 'set.' see below */
36379+ if (set == ARRAY_SIZE(a)) {
36380+ for (i = 0; i < ARRAY_SIZE(a); i++)
36381+ if (a[i].key == key)
36382+ goto match;
36383+ goto unmatch;
36384+ }
36385+
36386+ spin_lock(&spin);
36387+ if (set)
36388+ for (i = 0; i < ARRAY_SIZE(a); i++)
36389+ if (a[i].key == key) {
36390+ spin_unlock(&spin);
36391+ goto match;
36392+ }
36393+ for (i = 0; i < ARRAY_SIZE(a); i++) {
36394+ if (a[i].key) {
36395+ if (unlikely(a[i].key == key)) { /* rare but possible */
36396+ spin_unlock(&spin);
36397+ goto match;
36398+ } else
36399+ continue;
36400+ }
36401+ if (strstr(a[i].name, name)) {
36402+ /*
36403+ * the order of these three lines is important for the
36404+ * lockless read above.
36405+ */
36406+ a[i].key = key;
36407+ spin_unlock(&spin);
36408+ set++;
36409+ /* AuDbg("%d, %s\n", set, name); */
36410+ goto match;
36411+ }
36412+ }
36413+ spin_unlock(&spin);
36414+ goto unmatch;
36415+
36416+match:
36417+ return 1;
36418+unmatch:
36419+ return 0;
36420+}
36421+
36422+static int au_wkq_lockdep_alloc(struct au_wkinfo *wkinfo)
36423+{
36424+ int err, n;
36425+ struct task_struct *curr;
36426+ struct held_lock **hl, *held_locks, *p;
36427+
36428+ err = 0;
36429+ curr = current;
36430+ wkinfo->dont_check = lockdep_recursing(curr);
36431+ if (wkinfo->dont_check)
36432+ goto out;
36433+ n = curr->lockdep_depth;
36434+ if (!n)
36435+ goto out;
36436+
36437+ err = -ENOMEM;
36438+ wkinfo->hlock = kmalloc_array(n + 1, sizeof(*wkinfo->hlock), GFP_NOFS);
36439+ if (unlikely(!wkinfo->hlock))
36440+ goto out;
36441+
36442+ err = 0;
43982f53
AM
36443+#if 0 /* left for debugging */
36444+ if (0 && au_debug_test())
8b6a4947
AM
36445+ lockdep_print_held_locks(curr);
36446+#endif
36447+ held_locks = curr->held_locks;
36448+ hl = wkinfo->hlock;
36449+ while (n--) {
36450+ p = held_locks++;
36451+ if (au_wkq_lockdep_test(p->instance->key, p->instance->name))
36452+ *hl++ = p;
36453+ }
36454+ *hl = NULL;
36455+
36456+out:
36457+ return err;
36458+}
36459+
36460+static void au_wkq_lockdep_free(struct au_wkinfo *wkinfo)
36461+{
9f237c51 36462+ au_kfree_try_rcu(wkinfo->hlock);
8b6a4947
AM
36463+}
36464+
36465+static void au_wkq_lockdep_pre(struct au_wkinfo *wkinfo)
36466+{
36467+ struct held_lock *p, **hl = wkinfo->hlock;
36468+ int subclass;
36469+
36470+ if (wkinfo->dont_check)
36471+ lockdep_off();
36472+ if (!hl)
36473+ return;
36474+ while ((p = *hl++)) { /* assignment */
36475+ subclass = lockdep_hlock_class(p)->subclass;
36476+ /* AuDbg("%s, %d\n", p->instance->name, subclass); */
36477+ if (p->read)
36478+ rwsem_acquire_read(p->instance, subclass, 0,
36479+ /*p->acquire_ip*/_RET_IP_);
36480+ else
36481+ rwsem_acquire(p->instance, subclass, 0,
36482+ /*p->acquire_ip*/_RET_IP_);
36483+ }
36484+}
36485+
36486+static void au_wkq_lockdep_post(struct au_wkinfo *wkinfo)
36487+{
36488+ struct held_lock *p, **hl = wkinfo->hlock;
36489+
36490+ if (wkinfo->dont_check)
36491+ lockdep_on();
36492+ if (!hl)
36493+ return;
36494+ while ((p = *hl++)) /* assignment */
d58c55f2 36495+ rwsem_release(p->instance, /*p->acquire_ip*/_RET_IP_);
8b6a4947
AM
36496+}
36497+#endif
1facf9fc 36498+
1facf9fc 36499+static void wkq_func(struct work_struct *wk)
36500+{
36501+ struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
36502+
2dfbb274 36503+ AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
7f207e10
AM
36504+ AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
36505+
8b6a4947 36506+ au_wkq_lockdep_pre(wkinfo);
1facf9fc 36507+ wkinfo->func(wkinfo->args);
8b6a4947 36508+ au_wkq_lockdep_post(wkinfo);
1facf9fc 36509+ if (au_ftest_wkq(wkinfo->flags, WAIT))
36510+ complete(wkinfo->comp);
36511+ else {
7f207e10 36512+ kobject_put(wkinfo->kobj);
9dbd164d 36513+ module_put(THIS_MODULE); /* todo: ?? */
9f237c51 36514+ au_kfree_rcu(wkinfo);
1facf9fc 36515+ }
36516+}
36517+
36518+/*
36519+ * Since struct completion is large, try allocating it dynamically.
36520+ */
1facf9fc 36521+#define AuWkqCompDeclare(name) struct completion *comp = NULL
36522+
36523+static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
36524+{
36525+ *comp = kmalloc(sizeof(**comp), GFP_NOFS);
36526+ if (*comp) {
36527+ init_completion(*comp);
36528+ wkinfo->comp = *comp;
36529+ return 0;
36530+ }
36531+ return -ENOMEM;
36532+}
36533+
36534+static void au_wkq_comp_free(struct completion *comp)
36535+{
9f237c51 36536+ au_kfree_rcu(comp);
1facf9fc 36537+}
36538+
53392da6 36539+static void au_wkq_run(struct au_wkinfo *wkinfo)
1facf9fc 36540+{
53392da6
AM
36541+ if (au_ftest_wkq(wkinfo->flags, NEST)) {
36542+ if (au_wkq_test()) {
38d290e6 36543+ AuWarn1("wkq from wkq, unless silly-rename on NFS,"
acd2b654
AM
36544+ " due to a dead dir by UDBA,"
36545+ " or async xino write?\n");
53392da6
AM
36546+ AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
36547+ }
36548+ } else
36549+ au_dbg_verify_kthread();
36550+
36551+ if (au_ftest_wkq(wkinfo->flags, WAIT)) {
a1f66529 36552+ INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
9dbd164d 36553+ queue_work(au_wkq, &wkinfo->wk);
4a4d8108
AM
36554+ } else {
36555+ INIT_WORK(&wkinfo->wk, wkq_func);
36556+ schedule_work(&wkinfo->wk);
36557+ }
1facf9fc 36558+}
36559+
7f207e10
AM
36560+/*
36561+ * Be careful. It is easy to make deadlock happen.
36562+ * processA: lock, wkq and wait
36563+ * processB: wkq and wait, lock in wkq
36564+ * --> deadlock
36565+ */
b752ccd1 36566+int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
1facf9fc 36567+{
36568+ int err;
36569+ AuWkqCompDeclare(comp);
36570+ struct au_wkinfo wkinfo = {
b752ccd1 36571+ .flags = flags,
1facf9fc 36572+ .func = func,
36573+ .args = args
36574+ };
36575+
36576+ err = au_wkq_comp_alloc(&wkinfo, &comp);
8b6a4947
AM
36577+ if (unlikely(err))
36578+ goto out;
36579+ err = au_wkq_lockdep_alloc(&wkinfo);
36580+ if (unlikely(err))
36581+ goto out_comp;
1facf9fc 36582+ if (!err) {
53392da6 36583+ au_wkq_run(&wkinfo);
1facf9fc 36584+ /* no timeout, no interrupt */
36585+ wait_for_completion(wkinfo.comp);
1facf9fc 36586+ }
8b6a4947 36587+ au_wkq_lockdep_free(&wkinfo);
1facf9fc 36588+
8b6a4947
AM
36589+out_comp:
36590+ au_wkq_comp_free(comp);
36591+out:
36592+ destroy_work_on_stack(&wkinfo.wk);
1facf9fc 36593+ return err;
1facf9fc 36594+}
36595+
027c5e7a
AM
36596+/*
36597+ * Note: dget/dput() in func for aufs dentries are not supported. It will be a
36598+ * problem in a concurrent umounting.
36599+ */
53392da6
AM
36600+int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36601+ unsigned int flags)
1facf9fc 36602+{
36603+ int err;
36604+ struct au_wkinfo *wkinfo;
36605+
f0c0a007 36606+ atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
1facf9fc 36607+
36608+ /*
36609+ * wkq_func() must free this wkinfo.
36610+ * it highly depends upon the implementation of workqueue.
36611+ */
36612+ err = 0;
36613+ wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
36614+ if (wkinfo) {
7f207e10 36615+ wkinfo->kobj = &au_sbi(sb)->si_kobj;
53392da6 36616+ wkinfo->flags = flags & ~AuWkq_WAIT;
1facf9fc 36617+ wkinfo->func = func;
36618+ wkinfo->args = args;
36619+ wkinfo->comp = NULL;
8b6a4947 36620+ au_wkq_lockdep_init(wkinfo);
7f207e10 36621+ kobject_get(wkinfo->kobj);
9dbd164d 36622+ __module_get(THIS_MODULE); /* todo: ?? */
1facf9fc 36623+
53392da6 36624+ au_wkq_run(wkinfo);
1facf9fc 36625+ } else {
36626+ err = -ENOMEM;
e49829fe 36627+ au_nwt_done(&au_sbi(sb)->si_nowait);
1facf9fc 36628+ }
36629+
36630+ return err;
36631+}
36632+
36633+/* ---------------------------------------------------------------------- */
36634+
36635+void au_nwt_init(struct au_nowait_tasks *nwt)
36636+{
f0c0a007
AM
36637+ atomic_set(&nwt->nw_len, 0);
36638+ /* smp_mb(); */ /* atomic_set */
1facf9fc 36639+ init_waitqueue_head(&nwt->nw_wq);
36640+}
36641+
36642+void au_wkq_fin(void)
36643+{
9dbd164d 36644+ destroy_workqueue(au_wkq);
1facf9fc 36645+}
36646+
36647+int __init au_wkq_init(void)
36648+{
9dbd164d 36649+ int err;
b752ccd1
AM
36650+
36651+ err = 0;
86dc4139 36652+ au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
9dbd164d
AM
36653+ if (IS_ERR(au_wkq))
36654+ err = PTR_ERR(au_wkq);
36655+ else if (!au_wkq)
36656+ err = -ENOMEM;
b752ccd1
AM
36657+
36658+ return err;
1facf9fc 36659+}
7f207e10 36660diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
eca34b5c 36661--- /usr/share/empty/fs/aufs/wkq.h 1970-01-01 01:00:00.000000000 +0100
594d0238 36662+++ linux/fs/aufs/wkq.h 2022-11-05 23:02:18.972555950 +0100
acd2b654 36663@@ -0,0 +1,89 @@
062440b3 36664+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 36665+/*
0dcfbb52 36666+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 36667+ *
0dcfbb52 36668+ * This program is free software; you can redistribute it and/or modify
1facf9fc 36669+ * it under the terms of the GNU General Public License as published by
36670+ * the Free Software Foundation; either version 2 of the License, or
36671+ * (at your option) any later version.
dece6358
AM
36672+ *
36673+ * This program is distributed in the hope that it will be useful,
36674+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36675+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36676+ * GNU General Public License for more details.
36677+ *
36678+ * You should have received a copy of the GNU General Public License
523b37e3 36679+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 36680+ */
36681+
36682+/*
36683+ * workqueue for asynchronous/super-io operations
36684+ * todo: try new credentials management scheme
36685+ */
36686+
36687+#ifndef __AUFS_WKQ_H__
36688+#define __AUFS_WKQ_H__
36689+
36690+#ifdef __KERNEL__
36691+
8b6a4947 36692+#include <linux/wait.h>
5afbbe0d 36693+
dece6358
AM
36694+struct super_block;
36695+
1facf9fc 36696+/* ---------------------------------------------------------------------- */
36697+
36698+/*
36699+ * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
36700+ */
36701+struct au_nowait_tasks {
f0c0a007 36702+ atomic_t nw_len;
1facf9fc 36703+ wait_queue_head_t nw_wq;
36704+};
36705+
36706+/* ---------------------------------------------------------------------- */
36707+
36708+typedef void (*au_wkq_func_t)(void *args);
36709+
36710+/* wkq flags */
36711+#define AuWkq_WAIT 1
9dbd164d 36712+#define AuWkq_NEST (1 << 1)
1facf9fc 36713+#define au_ftest_wkq(flags, name) ((flags) & AuWkq_##name)
7f207e10
AM
36714+#define au_fset_wkq(flags, name) \
36715+ do { (flags) |= AuWkq_##name; } while (0)
36716+#define au_fclr_wkq(flags, name) \
36717+ do { (flags) &= ~AuWkq_##name; } while (0)
1facf9fc 36718+
36719+/* wkq.c */
b752ccd1 36720+int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
53392da6
AM
36721+int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36722+ unsigned int flags);
1facf9fc 36723+void au_nwt_init(struct au_nowait_tasks *nwt);
36724+int __init au_wkq_init(void);
36725+void au_wkq_fin(void);
36726+
36727+/* ---------------------------------------------------------------------- */
36728+
53392da6
AM
36729+static inline int au_wkq_test(void)
36730+{
36731+ return current->flags & PF_WQ_WORKER;
36732+}
36733+
b752ccd1 36734+static inline int au_wkq_wait(au_wkq_func_t func, void *args)
1facf9fc 36735+{
b752ccd1 36736+ return au_wkq_do_wait(AuWkq_WAIT, func, args);
1facf9fc 36737+}
36738+
36739+static inline void au_nwt_done(struct au_nowait_tasks *nwt)
36740+{
f0c0a007 36741+ if (atomic_dec_and_test(&nwt->nw_len))
1facf9fc 36742+ wake_up_all(&nwt->nw_wq);
36743+}
36744+
36745+static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
36746+{
f0c0a007 36747+ wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
1facf9fc 36748+ return 0;
36749+}
36750+
36751+#endif /* __KERNEL__ */
36752+#endif /* __AUFS_WKQ_H__ */
c1595e42 36753diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
eca34b5c 36754--- /usr/share/empty/fs/aufs/xattr.c 1970-01-01 01:00:00.000000000 +0100
631230db 36755+++ linux/fs/aufs/xattr.c 2023-10-31 09:31:04.203214083 +0100
594d0238 36756@@ -0,0 +1,360 @@
cd7a4cd9 36757+// SPDX-License-Identifier: GPL-2.0
c1595e42 36758+/*
0dcfbb52 36759+ * Copyright (C) 2014-2022 Junjiro R. Okajima
c1595e42 36760+ *
0dcfbb52 36761+ * This program is free software; you can redistribute it and/or modify
c1595e42
JR
36762+ * it under the terms of the GNU General Public License as published by
36763+ * the Free Software Foundation; either version 2 of the License, or
36764+ * (at your option) any later version.
36765+ *
36766+ * This program is distributed in the hope that it will be useful,
36767+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36768+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36769+ * GNU General Public License for more details.
36770+ *
36771+ * You should have received a copy of the GNU General Public License
36772+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
36773+ */
36774+
36775+/*
36776+ * handling xattr functions
36777+ */
36778+
a2654f78 36779+#include <linux/fs.h>
c1595e42
JR
36780+#include <linux/xattr.h>
36781+#include "aufs.h"
36782+
36783+static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
36784+{
36785+ if (!ignore_flags)
36786+ goto out;
36787+ switch (err) {
36788+ case -ENOMEM:
36789+ case -EDQUOT:
36790+ goto out;
36791+ }
36792+
36793+ if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
36794+ err = 0;
36795+ goto out;
36796+ }
36797+
36798+#define cmp(brattr, prefix) do { \
36799+ if (!strncmp(name, XATTR_##prefix##_PREFIX, \
36800+ XATTR_##prefix##_PREFIX_LEN)) { \
36801+ if (ignore_flags & AuBrAttr_ICEX_##brattr) \
36802+ err = 0; \
36803+ goto out; \
36804+ } \
36805+ } while (0)
36806+
36807+ cmp(SEC, SECURITY);
36808+ cmp(SYS, SYSTEM);
36809+ cmp(TR, TRUSTED);
36810+ cmp(USR, USER);
36811+#undef cmp
36812+
36813+ if (ignore_flags & AuBrAttr_ICEX_OTH)
36814+ err = 0;
36815+
36816+out:
36817+ return err;
36818+}
36819+
36820+static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
36821+
0b2a12c6 36822+static int au_do_cpup_xattr(struct path *h_dst, struct path *h_src,
7e9cd9fe
AM
36823+ char *name, char **buf, unsigned int ignore_flags,
36824+ unsigned int verbose)
c1595e42 36825+{
aec24f17 36826+ int err, is_acl;
c1595e42
JR
36827+ ssize_t ssz;
36828+ struct inode *h_idst;
0b2a12c6 36829+ struct dentry *h_dst_dentry, *h_src_dentry;
5db017eb 36830+ struct mnt_idmap *h_dst_idmap, *h_src_idmap;
aec24f17 36831+ struct posix_acl *acl;
c1595e42 36832+
aec24f17 36833+ is_acl = !!is_posix_acl_xattr(name);
5db017eb 36834+ h_src_idmap = mnt_idmap(h_src->mnt);
0b2a12c6 36835+ h_src_dentry = h_src->dentry;
aec24f17 36836+ if (is_acl) {
5db017eb 36837+ acl = vfs_get_acl(h_src_idmap, h_src_dentry, name);
aec24f17
AM
36838+ AuDebugOn(!acl);
36839+ if (unlikely(IS_ERR(acl))) {
36840+ err = PTR_ERR(acl);
36841+ if (err == -ENODATA)
36842+ err = 0;
36843+ else if (err == -EOPNOTSUPP
36844+ && au_test_nfs_noacl(d_inode(h_src_dentry)))
36845+ err = 0;
36846+ else if (verbose || au_debug_test())
36847+ pr_err("%s, err %d\n", name, err);
36848+ goto out;
36849+ }
36850+ } else {
5db017eb 36851+ ssz = vfs_getxattr_alloc(h_src_idmap, h_src_dentry, name, buf,
aec24f17
AM
36852+ 0, GFP_NOFS);
36853+ if (unlikely(ssz <= 0)) {
36854+ err = ssz;
36855+ if (err == -ENODATA)
36856+ err = 0;
36857+ else if (err == -EOPNOTSUPP
36858+ && (ignore_flags & au_xattr_out_of_list))
36859+ err = 0;
36860+ else if (err && (verbose || au_debug_test()))
36861+ pr_err("%s, err %d\n", name, err);
36862+ goto out;
36863+ }
c1595e42
JR
36864+ }
36865+
36866+ /* unlock it temporary */
5db017eb 36867+ h_dst_idmap = mnt_idmap(h_dst->mnt);
0b2a12c6
JR
36868+ h_dst_dentry = h_dst->dentry;
36869+ h_idst = d_inode(h_dst_dentry);
febd17d6 36870+ inode_unlock(h_idst);
aec24f17 36871+ if (is_acl) {
5db017eb 36872+ err = vfsub_set_acl(h_dst_idmap, h_dst_dentry, name, acl);
aec24f17
AM
36873+ posix_acl_release(acl);
36874+ } else
5db017eb 36875+ err = vfsub_setxattr(h_dst_idmap, h_dst_dentry, name, *buf,
aec24f17 36876+ ssz, /*flags*/0);
febd17d6 36877+ inode_lock_nested(h_idst, AuLsc_I_CHILD2);
c1595e42 36878+ if (unlikely(err)) {
7e9cd9fe
AM
36879+ if (verbose || au_debug_test())
36880+ pr_err("%s, err %d\n", name, err);
c1595e42
JR
36881+ err = au_xattr_ignore(err, name, ignore_flags);
36882+ }
36883+
36884+out:
36885+ return err;
36886+}
36887+
0b2a12c6 36888+int au_cpup_xattr(struct path *h_dst, struct path *h_src, int ignore_flags,
7e9cd9fe 36889+ unsigned int verbose)
c1595e42 36890+{
aec24f17 36891+ int err, unlocked;
c1595e42 36892+ ssize_t ssz;
0b2a12c6 36893+ struct dentry *h_dst_dentry, *h_src_dentry;
c1595e42
JR
36894+ struct inode *h_isrc, *h_idst;
36895+ char *value, *p, *o, *e;
36896+
36897+ /* try stopping to update the source inode while we are referencing */
7e9cd9fe 36898+ /* there should not be the parent-child relationship between them */
0b2a12c6
JR
36899+ h_dst_dentry = h_dst->dentry;
36900+ h_idst = d_inode(h_dst_dentry);
36901+ h_src_dentry = h_src->dentry;
36902+ h_isrc = d_inode(h_src_dentry);
febd17d6 36903+ inode_unlock(h_idst);
be118d29 36904+ inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
febd17d6 36905+ inode_lock_nested(h_idst, AuLsc_I_CHILD2);
c1595e42
JR
36906+ unlocked = 0;
36907+
36908+ /* some filesystems don't list POSIX ACL, for example tmpfs */
0b2a12c6 36909+ ssz = vfs_listxattr(h_src_dentry, NULL, 0);
c1595e42
JR
36910+ err = ssz;
36911+ if (unlikely(err < 0)) {
36912+ AuTraceErr(err);
36913+ if (err == -ENODATA
36914+ || err == -EOPNOTSUPP)
36915+ err = 0; /* ignore */
36916+ goto out;
36917+ }
36918+
36919+ err = 0;
36920+ p = NULL;
36921+ o = NULL;
36922+ if (ssz) {
36923+ err = -ENOMEM;
36924+ p = kmalloc(ssz, GFP_NOFS);
36925+ o = p;
36926+ if (unlikely(!p))
36927+ goto out;
0b2a12c6 36928+ err = vfs_listxattr(h_src_dentry, p, ssz);
c1595e42 36929+ }
3c1bdaff 36930+ inode_unlock_shared(h_isrc);
c1595e42
JR
36931+ unlocked = 1;
36932+ AuDbg("err %d, ssz %zd\n", err, ssz);
36933+ if (unlikely(err < 0))
36934+ goto out_free;
36935+
36936+ err = 0;
36937+ e = p + ssz;
36938+ value = NULL;
aec24f17 36939+ ignore_flags |= au_xattr_out_of_list;
c1595e42 36940+ while (!err && p < e) {
7e9cd9fe
AM
36941+ err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
36942+ verbose);
c1595e42
JR
36943+ p += strlen(p) + 1;
36944+ }
9f237c51 36945+ au_kfree_try_rcu(value);
c1595e42
JR
36946+
36947+out_free:
9f237c51 36948+ au_kfree_try_rcu(o);
c1595e42
JR
36949+out:
36950+ if (!unlocked)
3c1bdaff 36951+ inode_unlock_shared(h_isrc);
c1595e42
JR
36952+ AuTraceErr(err);
36953+ return err;
36954+}
36955+
36956+/* ---------------------------------------------------------------------- */
36957+
a2654f78
AM
36958+static int au_smack_reentering(struct super_block *sb)
36959+{
42b5c33a 36960+#if IS_ENABLED(CONFIG_SECURITY_SMACK) || IS_ENABLED(CONFIG_SECURITY_SELINUX)
a2654f78
AM
36961+ /*
36962+ * as a part of lookup, smack_d_instantiate() is called, and it calls
36963+ * i_op->getxattr(). ouch.
36964+ */
36965+ return si_pid_test(sb);
36966+#else
36967+ return 0;
36968+#endif
36969+}
36970+
c1595e42
JR
36971+enum {
36972+ AU_XATTR_LIST,
36973+ AU_XATTR_GET
36974+};
36975+
36976+struct au_lgxattr {
36977+ int type;
36978+ union {
36979+ struct {
36980+ char *list;
36981+ size_t size;
36982+ } list;
36983+ struct {
36984+ const char *name;
36985+ void *value;
36986+ size_t size;
36987+ } get;
36988+ } u;
36989+};
36990+
42b5c33a
AM
36991+static ssize_t au_lgxattr(struct dentry *dentry, struct inode *inode,
36992+ struct au_lgxattr *arg)
c1595e42
JR
36993+{
36994+ ssize_t err;
a2654f78 36995+ int reenter;
c1595e42
JR
36996+ struct path h_path;
36997+ struct super_block *sb;
36998+
36999+ sb = dentry->d_sb;
a2654f78
AM
37000+ reenter = au_smack_reentering(sb);
37001+ if (!reenter) {
37002+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
37003+ if (unlikely(err))
37004+ goto out;
37005+ }
42b5c33a 37006+ err = au_h_path_getattr(dentry, inode, /*force*/1, &h_path, reenter);
c1595e42
JR
37007+ if (unlikely(err))
37008+ goto out_si;
37009+ if (unlikely(!h_path.dentry))
37010+ /* illegally overlapped or something */
37011+ goto out_di; /* pretending success */
37012+
37013+ /* always topmost entry only */
37014+ switch (arg->type) {
37015+ case AU_XATTR_LIST:
37016+ err = vfs_listxattr(h_path.dentry,
37017+ arg->u.list.list, arg->u.list.size);
37018+ break;
37019+ case AU_XATTR_GET:
5afbbe0d 37020+ AuDebugOn(d_is_negative(h_path.dentry));
5db017eb 37021+ err = vfs_getxattr(mnt_idmap(h_path.mnt), h_path.dentry,
c1595e42
JR
37022+ arg->u.get.name, arg->u.get.value,
37023+ arg->u.get.size);
37024+ break;
37025+ }
37026+
37027+out_di:
a2654f78
AM
37028+ if (!reenter)
37029+ di_read_unlock(dentry, AuLock_IR);
c1595e42 37030+out_si:
a2654f78
AM
37031+ if (!reenter)
37032+ si_read_unlock(sb);
c1595e42
JR
37033+out:
37034+ AuTraceErr(err);
37035+ return err;
37036+}
37037+
37038+ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
37039+{
37040+ struct au_lgxattr arg = {
37041+ .type = AU_XATTR_LIST,
37042+ .u.list = {
37043+ .list = list,
37044+ .size = size
37045+ },
37046+ };
37047+
42b5c33a 37048+ return au_lgxattr(dentry, /*inode*/NULL, &arg);
c1595e42
JR
37049+}
37050+
42b5c33a 37051+static ssize_t au_getxattr(struct dentry *dentry, struct inode *inode,
f2c43d5f 37052+ const char *name, void *value, size_t size)
c1595e42
JR
37053+{
37054+ struct au_lgxattr arg = {
37055+ .type = AU_XATTR_GET,
37056+ .u.get = {
37057+ .name = name,
37058+ .value = value,
37059+ .size = size
37060+ },
37061+ };
37062+
42b5c33a 37063+ return au_lgxattr(dentry, inode, &arg);
c1595e42
JR
37064+}
37065+
f2c43d5f 37066+static int au_setxattr(struct dentry *dentry, struct inode *inode,
2255d0fe 37067+ const char *name, const void *value, size_t size,
f2c43d5f 37068+ int flags)
c1595e42 37069+{
f2c43d5f 37070+ struct au_sxattr arg = {
c1595e42
JR
37071+ .type = AU_XATTR_SET,
37072+ .u.set = {
37073+ .name = name,
37074+ .value = value,
37075+ .size = size,
37076+ .flags = flags
37077+ },
37078+ };
37079+
f2c43d5f 37080+ return au_sxattr(dentry, inode, &arg);
c1595e42
JR
37081+}
37082+
37083+/* ---------------------------------------------------------------------- */
37084+
f2c43d5f
AM
37085+static int au_xattr_get(const struct xattr_handler *handler,
37086+ struct dentry *dentry, struct inode *inode,
37087+ const char *name, void *buffer, size_t size)
c1595e42 37088+{
f2c43d5f 37089+ return au_getxattr(dentry, inode, name, buffer, size);
c1595e42
JR
37090+}
37091+
f2c43d5f 37092+static int au_xattr_set(const struct xattr_handler *handler,
5db017eb 37093+ struct mnt_idmap *idmap,
f2c43d5f
AM
37094+ struct dentry *dentry, struct inode *inode,
37095+ const char *name, const void *value, size_t size,
37096+ int flags)
c1595e42 37097+{
2255d0fe 37098+ return au_setxattr(dentry, inode, name, value, size, flags);
c1595e42
JR
37099+}
37100+
37101+static const struct xattr_handler au_xattr_handler = {
f2c43d5f
AM
37102+ .name = "",
37103+ .prefix = "",
c1595e42
JR
37104+ .get = au_xattr_get,
37105+ .set = au_xattr_set
c1595e42
JR
37106+};
37107+
37108+static const struct xattr_handler *au_xattr_handlers[] = {
a2654f78 37109+ &au_xattr_handler, /* must be last */
f2c43d5f 37110+ NULL
c1595e42
JR
37111+};
37112+
37113+void au_xattr_init(struct super_block *sb)
37114+{
f2c43d5f 37115+ sb->s_xattr = au_xattr_handlers;
c1595e42 37116+}
7f207e10 37117diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
eca34b5c 37118--- /usr/share/empty/fs/aufs/xino.c 1970-01-01 01:00:00.000000000 +0100
631230db 37119+++ linux/fs/aufs/xino.c 2023-10-31 09:31:04.203214083 +0100
758e9dad 37120@@ -0,0 +1,1926 @@
cd7a4cd9 37121+// SPDX-License-Identifier: GPL-2.0
1facf9fc 37122+/*
0dcfbb52 37123+ * Copyright (C) 2005-2022 Junjiro R. Okajima
1facf9fc 37124+ *
0dcfbb52 37125+ * This program is free software; you can redistribute it and/or modify
1facf9fc 37126+ * it under the terms of the GNU General Public License as published by
37127+ * the Free Software Foundation; either version 2 of the License, or
37128+ * (at your option) any later version.
dece6358
AM
37129+ *
37130+ * This program is distributed in the hope that it will be useful,
37131+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
37132+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37133+ * GNU General Public License for more details.
37134+ *
37135+ * You should have received a copy of the GNU General Public License
523b37e3 37136+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 37137+ */
37138+
37139+/*
37140+ * external inode number translation table and bitmap
062440b3
AM
37141+ *
37142+ * things to consider
37143+ * - the lifetime
37144+ * + au_xino object
37145+ * + XINO files (xino, xib, xigen)
37146+ * + dynamic debugfs entries (xiN)
37147+ * + static debugfs entries (xib, xigen)
37148+ * + static sysfs entry (xi_path)
37149+ * - several entry points to handle them.
37150+ * + mount(2) without xino option (default)
37151+ * + mount(2) with xino option
37152+ * + mount(2) with noxino option
37153+ * + umount(2)
37154+ * + remount with add/del branches
37155+ * + remount with xino/noxino options
1facf9fc 37156+ */
37157+
37158+#include <linux/seq_file.h>
392086de 37159+#include <linux/statfs.h>
1facf9fc 37160+#include "aufs.h"
37161+
062440b3
AM
37162+static aufs_bindex_t sbr_find_shared(struct super_block *sb, aufs_bindex_t btop,
37163+ aufs_bindex_t bbot,
37164+ struct super_block *h_sb)
1facf9fc 37165+{
062440b3
AM
37166+ /* todo: try binary-search if the branches are many */
37167+ for (; btop <= bbot; btop++)
37168+ if (h_sb == au_sbr_sb(sb, btop))
37169+ return btop;
37170+ return -1;
be52b249
AM
37171+}
37172+
062440b3
AM
37173+/*
37174+ * find another branch who is on the same filesystem of the specified
37175+ * branch{@btgt}. search until @bbot.
37176+ */
37177+static aufs_bindex_t is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
37178+ aufs_bindex_t bbot)
1facf9fc 37179+{
062440b3
AM
37180+ aufs_bindex_t bindex;
37181+ struct super_block *tgt_sb;
1facf9fc 37182+
062440b3
AM
37183+ tgt_sb = au_sbr_sb(sb, btgt);
37184+ bindex = sbr_find_shared(sb, /*btop*/0, btgt - 1, tgt_sb);
37185+ if (bindex < 0)
37186+ bindex = sbr_find_shared(sb, btgt + 1, bbot, tgt_sb);
1facf9fc 37187+
062440b3 37188+ return bindex;
1facf9fc 37189+}
37190+
37191+/* ---------------------------------------------------------------------- */
37192+
37193+/*
062440b3 37194+ * stop unnecessary notify events at creating xino files
1facf9fc 37195+ */
acd2b654
AM
37196+
37197+aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry)
37198+{
37199+ aufs_bindex_t bfound, bindex, bbot;
37200+ struct dentry *parent;
37201+ struct au_branch *br;
37202+
37203+ bfound = -1;
37204+ parent = dentry->d_parent; /* safe d_parent access */
37205+ bbot = au_sbbot(sb);
37206+ for (bindex = 0; bindex <= bbot; bindex++) {
37207+ br = au_sbr(sb, bindex);
37208+ if (au_br_dentry(br) == parent) {
37209+ bfound = bindex;
37210+ break;
37211+ }
37212+ }
37213+
37214+ AuDbg("bfound b%d\n", bfound);
37215+ return bfound;
37216+}
37217+
062440b3
AM
37218+struct au_xino_lock_dir {
37219+ struct au_hinode *hdir;
37220+ struct dentry *parent;
37221+ struct inode *dir;
37222+};
37223+
37224+static struct dentry *au_dget_parent_lock(struct dentry *dentry,
37225+ unsigned int lsc)
1facf9fc 37226+{
062440b3
AM
37227+ struct dentry *parent;
37228+ struct inode *dir;
1facf9fc 37229+
062440b3 37230+ parent = dget_parent(dentry);
5527c038 37231+ dir = d_inode(parent);
062440b3
AM
37232+ inode_lock_nested(dir, lsc);
37233+#if 0 /* it should not happen */
37234+ spin_lock(&dentry->d_lock);
37235+ if (unlikely(dentry->d_parent != parent)) {
37236+ spin_unlock(&dentry->d_lock);
37237+ inode_unlock(dir);
37238+ dput(parent);
37239+ parent = NULL;
1facf9fc 37240+ goto out;
37241+ }
062440b3 37242+ spin_unlock(&dentry->d_lock);
1facf9fc 37243+
4f0767ce 37244+out:
062440b3
AM
37245+#endif
37246+ return parent;
1facf9fc 37247+}
37248+
062440b3 37249+static void au_xino_lock_dir(struct super_block *sb, struct path *xipath,
1facf9fc 37250+ struct au_xino_lock_dir *ldir)
37251+{
acd2b654 37252+ aufs_bindex_t bindex;
1facf9fc 37253+
37254+ ldir->hdir = NULL;
acd2b654 37255+ bindex = au_xi_root(sb, xipath->dentry);
1facf9fc 37256+ if (bindex >= 0) {
062440b3 37257+ /* rw branch root */
5527c038 37258+ ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
5afbbe0d 37259+ au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
1facf9fc 37260+ } else {
062440b3
AM
37261+ /* other */
37262+ ldir->parent = au_dget_parent_lock(xipath->dentry,
37263+ AuLsc_I_PARENT);
febd17d6 37264+ ldir->dir = d_inode(ldir->parent);
1facf9fc 37265+ }
37266+}
37267+
37268+static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
37269+{
37270+ if (ldir->hdir)
5afbbe0d 37271+ au_hn_inode_unlock(ldir->hdir);
1facf9fc 37272+ else {
febd17d6 37273+ inode_unlock(ldir->dir);
1facf9fc 37274+ dput(ldir->parent);
37275+ }
37276+}
37277+
37278+/* ---------------------------------------------------------------------- */
37279+
062440b3
AM
37280+/*
37281+ * create and set a new xino file
37282+ */
83b672a5
AM
37283+struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
37284+ int wbrtop)
062440b3
AM
37285+{
37286+ struct file *file;
37287+ struct dentry *h_parent, *d;
37288+ struct inode *h_dir, *inode;
37289+ int err;
83b672a5 37290+ static DEFINE_MUTEX(mtx);
062440b3
AM
37291+
37292+ /*
37293+ * at mount-time, and the xino file is the default path,
37294+ * hnotify is disabled so we have no notify events to ignore.
37295+ * when a user specified the xino, we cannot get au_hdir to be ignored.
37296+ */
83b672a5
AM
37297+ if (!wbrtop)
37298+ mutex_lock(&mtx);
062440b3
AM
37299+ file = vfsub_filp_open(fpath, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37300+ /* | __FMODE_NONOTIFY */,
cd7a4cd9 37301+ 0666);
062440b3 37302+ if (IS_ERR(file)) {
83b672a5
AM
37303+ if (!wbrtop)
37304+ mutex_unlock(&mtx);
062440b3
AM
37305+ if (!silent)
37306+ pr_err("open %s(%ld)\n", fpath, PTR_ERR(file));
37307+ return file;
37308+ }
37309+
37310+ /* keep file count */
37311+ err = 0;
37312+ d = file->f_path.dentry;
37313+ h_parent = au_dget_parent_lock(d, AuLsc_I_PARENT);
83b672a5
AM
37314+ if (!wbrtop)
37315+ mutex_unlock(&mtx);
062440b3
AM
37316+ /* mnt_want_write() is unnecessary here */
37317+ h_dir = d_inode(h_parent);
37318+ inode = file_inode(file);
37319+ /* no delegation since it is just created */
37320+ if (inode->i_nlink)
37321+ err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
37322+ /*force*/0);
37323+ inode_unlock(h_dir);
37324+ dput(h_parent);
37325+ if (unlikely(err)) {
37326+ if (!silent)
37327+ pr_err("unlink %s(%d)\n", fpath, err);
37328+ goto out;
37329+ }
37330+
37331+ err = -EINVAL;
f4d37d76 37332+ if (unlikely(sb && sb == d->d_sb)) {
062440b3
AM
37333+ if (!silent)
37334+ pr_err("%s must be outside\n", fpath);
37335+ goto out;
37336+ }
37337+ if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
37338+ if (!silent)
37339+ pr_err("xino doesn't support %s(%s)\n",
37340+ fpath, au_sbtype(d->d_sb));
37341+ goto out;
37342+ }
37343+ return file; /* success */
37344+
37345+out:
37346+ fput(file);
37347+ file = ERR_PTR(err);
37348+ return file;
37349+}
37350+
37351+/*
37352+ * create a new xinofile at the same place/path as @base.
37353+ */
37354+struct file *au_xino_create2(struct super_block *sb, struct path *base,
37355+ struct file *copy_src)
37356+{
37357+ struct file *file;
758e9dad 37358+ struct dentry *dentry;
062440b3
AM
37359+ struct inode *dir, *delegated;
37360+ struct qstr *name;
758e9dad 37361+ struct path ppath, path;
062440b3
AM
37362+ int err, do_unlock;
37363+ struct au_xino_lock_dir ldir;
37364+
37365+ do_unlock = 1;
37366+ au_xino_lock_dir(sb, base, &ldir);
37367+ dentry = base->dentry;
758e9dad
AM
37368+ ppath.dentry = dentry->d_parent; /* dir inode is locked */
37369+ ppath.mnt = base->mnt;
37370+ dir = d_inode(ppath.dentry);
062440b3
AM
37371+ IMustLock(dir);
37372+
37373+ name = &dentry->d_name;
758e9dad 37374+ path.dentry = vfsub_lookup_one_len(name->name, &ppath, name->len);
062440b3
AM
37375+ if (IS_ERR(path.dentry)) {
37376+ file = (void *)path.dentry;
37377+ pr_err("%pd lookup err %ld\n", dentry, PTR_ERR(path.dentry));
37378+ goto out;
37379+ }
37380+
37381+ /* no need to mnt_want_write() since we call dentry_open() later */
5db017eb 37382+ err = vfs_create(mnt_idmap(base->mnt), dir, path.dentry, 0666, NULL);
062440b3
AM
37383+ if (unlikely(err)) {
37384+ file = ERR_PTR(err);
37385+ pr_err("%pd create err %d\n", dentry, err);
37386+ goto out_dput;
37387+ }
37388+
37389+ path.mnt = base->mnt;
37390+ file = vfsub_dentry_open(&path,
37391+ O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37392+ /* | __FMODE_NONOTIFY */);
37393+ if (IS_ERR(file)) {
37394+ pr_err("%pd open err %ld\n", dentry, PTR_ERR(file));
37395+ goto out_dput;
37396+ }
37397+
37398+ delegated = NULL;
37399+ err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
37400+ au_xino_unlock_dir(&ldir);
37401+ do_unlock = 0;
37402+ if (unlikely(err == -EWOULDBLOCK)) {
37403+ pr_warn("cannot retry for NFSv4 delegation"
37404+ " for an internal unlink\n");
37405+ iput(delegated);
37406+ }
37407+ if (unlikely(err)) {
37408+ pr_err("%pd unlink err %d\n", dentry, err);
37409+ goto out_fput;
37410+ }
37411+
37412+ if (copy_src) {
37413+ /* no one can touch copy_src xino */
37414+ err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
37415+ if (unlikely(err)) {
37416+ pr_err("%pd copy err %d\n", dentry, err);
37417+ goto out_fput;
37418+ }
37419+ }
37420+ goto out_dput; /* success */
1facf9fc 37421+
062440b3
AM
37422+out_fput:
37423+ fput(file);
37424+ file = ERR_PTR(err);
37425+out_dput:
37426+ dput(path.dentry);
37427+out:
37428+ if (do_unlock)
37429+ au_xino_unlock_dir(&ldir);
37430+ return file;
37431+}
37432+
acd2b654
AM
37433+struct file *au_xino_file1(struct au_xino *xi)
37434+{
37435+ struct file *file;
37436+ unsigned int u, nfile;
37437+
37438+ file = NULL;
37439+ nfile = xi->xi_nfile;
37440+ for (u = 0; u < nfile; u++) {
37441+ file = xi->xi_file[u];
37442+ if (file)
37443+ break;
37444+ }
37445+
37446+ return file;
37447+}
37448+
37449+static int au_xino_file_set(struct au_xino *xi, int idx, struct file *file)
37450+{
37451+ int err;
37452+ struct file *f;
37453+ void *p;
37454+
37455+ if (file)
37456+ get_file(file);
37457+
37458+ err = 0;
37459+ f = NULL;
37460+ if (idx < xi->xi_nfile) {
37461+ f = xi->xi_file[idx];
37462+ if (f)
37463+ fput(f);
37464+ } else {
37465+ p = au_kzrealloc(xi->xi_file,
37466+ sizeof(*xi->xi_file) * xi->xi_nfile,
37467+ sizeof(*xi->xi_file) * (idx + 1),
37468+ GFP_NOFS, /*may_shrink*/0);
37469+ if (p) {
37470+ MtxMustLock(&xi->xi_mtx);
37471+ xi->xi_file = p;
37472+ xi->xi_nfile = idx + 1;
37473+ } else {
37474+ err = -ENOMEM;
37475+ if (file)
37476+ fput(file);
37477+ goto out;
37478+ }
37479+ }
37480+ xi->xi_file[idx] = file;
37481+
37482+out:
37483+ return err;
37484+}
37485+
37486+/*
37487+ * if @xinew->xi is not set, then create new xigen file.
37488+ */
37489+struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew)
37490+{
37491+ struct file *file;
37492+ int err;
37493+
37494+ SiMustAnyLock(sb);
37495+
37496+ file = au_xino_create2(sb, xinew->base, xinew->copy_src);
37497+ if (IS_ERR(file)) {
37498+ err = PTR_ERR(file);
37499+ pr_err("%s[%d], err %d\n",
37500+ xinew->xi ? "xino" : "xigen",
37501+ xinew->idx, err);
37502+ goto out;
37503+ }
37504+
37505+ if (xinew->xi)
37506+ err = au_xino_file_set(xinew->xi, xinew->idx, file);
37507+ else {
37508+ BUG();
37509+ /* todo: make xigen file an array */
37510+ /* err = au_xigen_file_set(sb, xinew->idx, file); */
37511+ }
37512+ fput(file);
37513+ if (unlikely(err))
37514+ file = ERR_PTR(err);
37515+
37516+out:
37517+ return file;
37518+}
37519+
062440b3
AM
37520+/* ---------------------------------------------------------------------- */
37521+
37522+/*
37523+ * truncate xino files
37524+ */
acd2b654
AM
37525+static int au_xino_do_trunc(struct super_block *sb, aufs_bindex_t bindex,
37526+ int idx, struct kstatfs *st)
1facf9fc 37527+{
37528+ int err;
392086de 37529+ blkcnt_t blocks;
acd2b654
AM
37530+ struct file *file, *new_xino;
37531+ struct au_xi_new xinew = {
37532+ .idx = idx
37533+ };
37534+
37535+ err = 0;
37536+ xinew.xi = au_sbr(sb, bindex)->br_xino;
37537+ file = au_xino_file(xinew.xi, idx);
37538+ if (!file)
37539+ goto out;
37540+
37541+ xinew.base = &file->f_path;
37542+ err = vfs_statfs(xinew.base, st);
37543+ if (unlikely(err)) {
37544+ AuErr1("statfs err %d, ignored\n", err);
37545+ err = 0;
37546+ goto out;
37547+ }
37548+
37549+ blocks = file_inode(file)->i_blocks;
37550+ pr_info("begin truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37551+ bindex, idx, (u64)blocks, st->f_bfree, st->f_blocks);
37552+
37553+ xinew.copy_src = file;
37554+ new_xino = au_xi_new(sb, &xinew);
37555+ if (IS_ERR(new_xino)) {
37556+ err = PTR_ERR(new_xino);
37557+ pr_err("xino(b%d-%d), err %d, ignored\n", bindex, idx, err);
37558+ goto out;
37559+ }
37560+
37561+ err = vfs_statfs(&new_xino->f_path, st);
37562+ if (!err)
37563+ pr_info("end truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37564+ bindex, idx, (u64)file_inode(new_xino)->i_blocks,
37565+ st->f_bfree, st->f_blocks);
37566+ else {
37567+ AuErr1("statfs err %d, ignored\n", err);
37568+ err = 0;
37569+ }
37570+
37571+out:
37572+ return err;
37573+}
37574+
37575+int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin)
37576+{
37577+ int err, i;
37578+ unsigned long jiffy;
062440b3 37579+ aufs_bindex_t bbot;
392086de 37580+ struct kstatfs *st;
1facf9fc 37581+ struct au_branch *br;
acd2b654 37582+ struct au_xino *xi;
1facf9fc 37583+
392086de 37584+ err = -ENOMEM;
be52b249 37585+ st = kmalloc(sizeof(*st), GFP_NOFS);
392086de
AM
37586+ if (unlikely(!st))
37587+ goto out;
37588+
1facf9fc 37589+ err = -EINVAL;
5afbbe0d
AM
37590+ bbot = au_sbbot(sb);
37591+ if (unlikely(bindex < 0 || bbot < bindex))
392086de 37592+ goto out_st;
392086de 37593+
1facf9fc 37594+ err = 0;
acd2b654
AM
37595+ jiffy = jiffies;
37596+ br = au_sbr(sb, bindex);
37597+ xi = br->br_xino;
37598+ for (i = idx_begin; !err && i < xi->xi_nfile; i++)
37599+ err = au_xino_do_trunc(sb, bindex, i, st);
37600+ if (!err)
37601+ au_sbi(sb)->si_xino_jiffy = jiffy;
392086de
AM
37602+
37603+out_st:
9f237c51 37604+ au_kfree_rcu(st);
4f0767ce 37605+out:
1facf9fc 37606+ return err;
37607+}
37608+
37609+struct xino_do_trunc_args {
37610+ struct super_block *sb;
37611+ struct au_branch *br;
acd2b654 37612+ int idx;
1facf9fc 37613+};
37614+
37615+static void xino_do_trunc(void *_args)
37616+{
37617+ struct xino_do_trunc_args *args = _args;
37618+ struct super_block *sb;
37619+ struct au_branch *br;
37620+ struct inode *dir;
acd2b654 37621+ int err, idx;
1facf9fc 37622+ aufs_bindex_t bindex;
37623+
37624+ err = 0;
37625+ sb = args->sb;
5527c038 37626+ dir = d_inode(sb->s_root);
1facf9fc 37627+ br = args->br;
acd2b654 37628+ idx = args->idx;
1facf9fc 37629+
37630+ si_noflush_write_lock(sb);
37631+ ii_read_lock_parent(dir);
37632+ bindex = au_br_index(sb, br->br_id);
acd2b654 37633+ err = au_xino_trunc(sb, bindex, idx);
1facf9fc 37634+ ii_read_unlock(dir);
37635+ if (unlikely(err))
392086de 37636+ pr_warn("err b%d, (%d)\n", bindex, err);
062440b3 37637+ atomic_dec(&br->br_xino->xi_truncating);
acd2b654 37638+ au_lcnt_dec(&br->br_count);
1facf9fc 37639+ si_write_unlock(sb);
027c5e7a 37640+ au_nwt_done(&au_sbi(sb)->si_nowait);
9f237c51 37641+ au_kfree_rcu(args);
1facf9fc 37642+}
37643+
acd2b654
AM
37644+/*
37645+ * returns the index in the xi_file array whose corresponding file is necessary
37646+ * to truncate, or -1 which means no need to truncate.
37647+ */
392086de
AM
37648+static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
37649+{
37650+ int err;
acd2b654 37651+ unsigned int u;
392086de
AM
37652+ struct kstatfs st;
37653+ struct au_sbinfo *sbinfo;
acd2b654 37654+ struct au_xino *xi;
062440b3 37655+ struct file *file;
392086de
AM
37656+
37657+ /* todo: si_xino_expire and the ratio should be customizable */
37658+ sbinfo = au_sbi(sb);
37659+ if (time_before(jiffies,
37660+ sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
acd2b654 37661+ return -1;
392086de
AM
37662+
37663+ /* truncation border */
acd2b654
AM
37664+ xi = br->br_xino;
37665+ for (u = 0; u < xi->xi_nfile; u++) {
37666+ file = au_xino_file(xi, u);
37667+ if (!file)
37668+ continue;
37669+
37670+ err = vfs_statfs(&file->f_path, &st);
37671+ if (unlikely(err)) {
37672+ AuErr1("statfs err %d, ignored\n", err);
37673+ return -1;
37674+ }
37675+ if (div64_u64(st.f_bfree * 100, st.f_blocks)
37676+ >= AUFS_XINO_DEF_TRUNC)
37677+ return u;
392086de 37678+ }
392086de 37679+
acd2b654 37680+ return -1;
392086de
AM
37681+}
37682+
1facf9fc 37683+static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
37684+{
acd2b654 37685+ int idx;
1facf9fc 37686+ struct xino_do_trunc_args *args;
37687+ int wkq_err;
37688+
acd2b654
AM
37689+ idx = xino_trunc_test(sb, br);
37690+ if (idx < 0)
1facf9fc 37691+ return;
37692+
062440b3 37693+ if (atomic_inc_return(&br->br_xino->xi_truncating) > 1)
1facf9fc 37694+ goto out;
37695+
37696+ /* lock and kfree() will be called in trunc_xino() */
37697+ args = kmalloc(sizeof(*args), GFP_NOFS);
37698+ if (unlikely(!args)) {
37699+ AuErr1("no memory\n");
f0c0a007 37700+ goto out;
1facf9fc 37701+ }
37702+
acd2b654 37703+ au_lcnt_inc(&br->br_count);
1facf9fc 37704+ args->sb = sb;
37705+ args->br = br;
acd2b654 37706+ args->idx = idx;
53392da6 37707+ wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
1facf9fc 37708+ if (!wkq_err)
37709+ return; /* success */
37710+
4a4d8108 37711+ pr_err("wkq %d\n", wkq_err);
acd2b654 37712+ au_lcnt_dec(&br->br_count);
9f237c51 37713+ au_kfree_rcu(args);
1facf9fc 37714+
4f0767ce 37715+out:
062440b3 37716+ atomic_dec(&br->br_xino->xi_truncating);
1facf9fc 37717+}
37718+
37719+/* ---------------------------------------------------------------------- */
37720+
acd2b654
AM
37721+struct au_xi_calc {
37722+ int idx;
37723+ loff_t pos;
37724+};
37725+
37726+static void au_xi_calc(struct super_block *sb, ino_t h_ino,
37727+ struct au_xi_calc *calc)
37728+{
37729+ loff_t maxent;
37730+
37731+ maxent = au_xi_maxent(sb);
37732+ calc->idx = div64_u64_rem(h_ino, maxent, &calc->pos);
37733+ calc->pos *= sizeof(ino_t);
37734+}
37735+
37736+static int au_xino_do_new_async(struct super_block *sb, struct au_branch *br,
37737+ struct au_xi_calc *calc)
37738+{
37739+ int err;
37740+ struct file *file;
37741+ struct au_xino *xi = br->br_xino;
37742+ struct au_xi_new xinew = {
37743+ .xi = xi
37744+ };
37745+
37746+ SiMustAnyLock(sb);
37747+
37748+ err = 0;
37749+ if (!xi)
37750+ goto out;
37751+
37752+ mutex_lock(&xi->xi_mtx);
37753+ file = au_xino_file(xi, calc->idx);
37754+ if (file)
37755+ goto out_mtx;
37756+
37757+ file = au_xino_file(xi, /*idx*/-1);
37758+ AuDebugOn(!file);
37759+ xinew.idx = calc->idx;
37760+ xinew.base = &file->f_path;
37761+ /* xinew.copy_src = NULL; */
37762+ file = au_xi_new(sb, &xinew);
37763+ if (IS_ERR(file))
37764+ err = PTR_ERR(file);
37765+
37766+out_mtx:
37767+ mutex_unlock(&xi->xi_mtx);
37768+out:
37769+ return err;
37770+}
37771+
37772+struct au_xino_do_new_async_args {
37773+ struct super_block *sb;
37774+ struct au_branch *br;
37775+ struct au_xi_calc calc;
37776+ ino_t ino;
37777+};
37778+
9f237c51
AM
37779+struct au_xi_writing {
37780+ struct hlist_bl_node node;
37781+ ino_t h_ino, ino;
37782+};
37783+
e37dd06a
AM
37784+static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37785+ ino_t ino);
acd2b654
AM
37786+
37787+static void au_xino_call_do_new_async(void *args)
37788+{
37789+ struct au_xino_do_new_async_args *a = args;
37790+ struct au_branch *br;
37791+ struct super_block *sb;
37792+ struct au_sbinfo *sbi;
37793+ struct inode *root;
37794+ struct file *file;
9f237c51
AM
37795+ struct au_xi_writing *del, *p;
37796+ struct hlist_bl_head *hbl;
37797+ struct hlist_bl_node *pos;
acd2b654
AM
37798+ int err;
37799+
37800+ br = a->br;
37801+ sb = a->sb;
37802+ sbi = au_sbi(sb);
37803+ si_noflush_read_lock(sb);
37804+ root = d_inode(sb->s_root);
37805+ ii_read_lock_child(root);
37806+ err = au_xino_do_new_async(sb, br, &a->calc);
9f237c51
AM
37807+ if (unlikely(err)) {
37808+ AuIOErr("err %d\n", err);
37809+ goto out;
37810+ }
37811+
37812+ file = au_xino_file(br->br_xino, a->calc.idx);
37813+ AuDebugOn(!file);
e37dd06a 37814+ err = au_xino_do_write(file, &a->calc, a->ino);
9f237c51 37815+ if (unlikely(err)) {
acd2b654 37816+ AuIOErr("err %d\n", err);
9f237c51
AM
37817+ goto out;
37818+ }
37819+
37820+ del = NULL;
37821+ hbl = &br->br_xino->xi_writing;
37822+ hlist_bl_lock(hbl);
37823+ au_hbl_for_each(pos, hbl) {
37824+ p = container_of(pos, struct au_xi_writing, node);
37825+ if (p->ino == a->ino) {
37826+ del = p;
37827+ hlist_bl_del(&p->node);
37828+ break;
37829+ }
37830+ }
37831+ hlist_bl_unlock(hbl);
37832+ au_kfree_rcu(del);
37833+
37834+out:
acd2b654
AM
37835+ au_lcnt_dec(&br->br_count);
37836+ ii_read_unlock(root);
37837+ si_read_unlock(sb);
37838+ au_nwt_done(&sbi->si_nowait);
9f237c51 37839+ au_kfree_rcu(a);
acd2b654
AM
37840+}
37841+
37842+/*
37843+ * create a new xino file asynchronously
37844+ */
37845+static int au_xino_new_async(struct super_block *sb, struct au_branch *br,
37846+ struct au_xi_calc *calc, ino_t ino)
37847+{
37848+ int err;
37849+ struct au_xino_do_new_async_args *arg;
37850+
37851+ err = -ENOMEM;
37852+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
37853+ if (unlikely(!arg))
37854+ goto out;
37855+
37856+ arg->sb = sb;
37857+ arg->br = br;
37858+ arg->calc = *calc;
37859+ arg->ino = ino;
37860+ au_lcnt_inc(&br->br_count);
37861+ err = au_wkq_nowait(au_xino_call_do_new_async, arg, sb, AuWkq_NEST);
37862+ if (unlikely(err)) {
37863+ pr_err("wkq %d\n", err);
37864+ au_lcnt_dec(&br->br_count);
9f237c51 37865+ au_kfree_rcu(arg);
acd2b654
AM
37866+ }
37867+
37868+out:
37869+ return err;
37870+}
37871+
062440b3
AM
37872+/*
37873+ * read @ino from xinofile for the specified branch{@sb, @bindex}
37874+ * at the position of @h_ino.
37875+ */
37876+int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37877+ ino_t *ino)
37878+{
37879+ int err;
37880+ ssize_t sz;
acd2b654 37881+ struct au_xi_calc calc;
062440b3 37882+ struct au_sbinfo *sbinfo;
acd2b654 37883+ struct file *file;
9f237c51
AM
37884+ struct au_xino *xi;
37885+ struct hlist_bl_head *hbl;
37886+ struct hlist_bl_node *pos;
37887+ struct au_xi_writing *p;
062440b3
AM
37888+
37889+ *ino = 0;
37890+ if (!au_opt_test(au_mntflags(sb), XINO))
37891+ return 0; /* no xino */
37892+
37893+ err = 0;
acd2b654 37894+ au_xi_calc(sb, h_ino, &calc);
9f237c51
AM
37895+ xi = au_sbr(sb, bindex)->br_xino;
37896+ file = au_xino_file(xi, calc.idx);
37897+ if (!file) {
37898+ hbl = &xi->xi_writing;
37899+ hlist_bl_lock(hbl);
37900+ au_hbl_for_each(pos, hbl) {
37901+ p = container_of(pos, struct au_xi_writing, node);
37902+ if (p->h_ino == h_ino) {
37903+ AuDbg("hi%llu, i%llu, found\n",
37904+ (u64)p->h_ino, (u64)p->ino);
37905+ *ino = p->ino;
37906+ break;
37907+ }
37908+ }
37909+ hlist_bl_unlock(hbl);
37910+ return 0;
37911+ } else if (vfsub_f_size_read(file) < calc.pos + sizeof(*ino))
37912+ return 0; /* no xino */
062440b3 37913+
acd2b654 37914+ sbinfo = au_sbi(sb);
e37dd06a 37915+ sz = xino_fread(file, ino, sizeof(*ino), &calc.pos);
062440b3
AM
37916+ if (sz == sizeof(*ino))
37917+ return 0; /* success */
37918+
37919+ err = sz;
37920+ if (unlikely(sz >= 0)) {
37921+ err = -EIO;
37922+ AuIOErr("xino read error (%zd)\n", sz);
37923+ }
062440b3
AM
37924+ return err;
37925+}
37926+
e37dd06a
AM
37927+static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37928+ ino_t ino)
1facf9fc 37929+{
1facf9fc 37930+ ssize_t sz;
37931+
e37dd06a 37932+ sz = xino_fwrite(file, &ino, sizeof(ino), &calc->pos);
1facf9fc 37933+ if (sz == sizeof(ino))
37934+ return 0; /* success */
37935+
37936+ AuIOErr("write failed (%zd)\n", sz);
37937+ return -EIO;
37938+}
37939+
37940+/*
37941+ * write @ino to the xinofile for the specified branch{@sb, @bindex}
37942+ * at the position of @h_ino.
37943+ * even if @ino is zero, it is written to the xinofile and means no entry.
37944+ * if the size of the xino file on a specific filesystem exceeds the watermark,
37945+ * try truncating it.
37946+ */
37947+int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37948+ ino_t ino)
37949+{
37950+ int err;
37951+ unsigned int mnt_flags;
acd2b654
AM
37952+ struct au_xi_calc calc;
37953+ struct file *file;
1facf9fc 37954+ struct au_branch *br;
acd2b654 37955+ struct au_xino *xi;
9f237c51 37956+ struct au_xi_writing *p;
1facf9fc 37957+
dece6358 37958+ SiMustAnyLock(sb);
1facf9fc 37959+
37960+ mnt_flags = au_mntflags(sb);
37961+ if (!au_opt_test(mnt_flags, XINO))
37962+ return 0;
37963+
acd2b654 37964+ au_xi_calc(sb, h_ino, &calc);
1facf9fc 37965+ br = au_sbr(sb, bindex);
acd2b654
AM
37966+ xi = br->br_xino;
37967+ file = au_xino_file(xi, calc.idx);
37968+ if (!file) {
9f237c51
AM
37969+ /* store the inum pair into the list */
37970+ p = kmalloc(sizeof(*p), GFP_NOFS | __GFP_NOFAIL);
37971+ p->h_ino = h_ino;
37972+ p->ino = ino;
37973+ au_hbl_add(&p->node, &xi->xi_writing);
37974+
acd2b654
AM
37975+ /* create and write a new xino file asynchronously */
37976+ err = au_xino_new_async(sb, br, &calc, ino);
37977+ if (!err)
37978+ return 0; /* success */
37979+ goto out;
37980+ }
37981+
e37dd06a 37982+ err = au_xino_do_write(file, &calc, ino);
1facf9fc 37983+ if (!err) {
acd2b654 37984+ br = au_sbr(sb, bindex);
1facf9fc 37985+ if (au_opt_test(mnt_flags, TRUNC_XINO)
86dc4139 37986+ && au_test_fs_trunc_xino(au_br_sb(br)))
1facf9fc 37987+ xino_try_trunc(sb, br);
37988+ return 0; /* success */
37989+ }
37990+
acd2b654 37991+out:
1facf9fc 37992+ AuIOErr("write failed (%d)\n", err);
37993+ return -EIO;
37994+}
37995+
e37dd06a
AM
37996+static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
37997+ loff_t *pos);
1facf9fc 37998+
062440b3 37999+/* todo: unnecessary to support mmap_sem since kernel-space? */
e37dd06a 38000+ssize_t xino_fread(struct file *file, void *kbuf, size_t size, loff_t *pos)
062440b3
AM
38001+{
38002+ ssize_t err;
062440b3
AM
38003+ int i;
38004+ const int prevent_endless = 10;
1facf9fc 38005+
062440b3 38006+ i = 0;
062440b3 38007+ do {
e37dd06a 38008+ err = vfsub_read_k(file, kbuf, size, pos);
062440b3
AM
38009+ if (err == -EINTR
38010+ && !au_wkq_test()
38011+ && fatal_signal_pending(current)) {
e37dd06a 38012+ err = xino_fread_wkq(file, kbuf, size, pos);
062440b3 38013+ BUG_ON(err == -EINTR);
062440b3
AM
38014+ }
38015+ } while (i++ < prevent_endless
38016+ && (err == -EAGAIN || err == -EINTR));
062440b3
AM
38017+
38018+#if 0 /* reserved for future use */
38019+ if (err > 0)
38020+ fsnotify_access(file->f_path.dentry);
38021+#endif
38022+
38023+ return err;
38024+}
38025+
38026+struct xino_fread_args {
38027+ ssize_t *errp;
062440b3
AM
38028+ struct file *file;
38029+ void *buf;
38030+ size_t size;
38031+ loff_t *pos;
38032+};
38033+
38034+static void call_xino_fread(void *args)
38035+{
38036+ struct xino_fread_args *a = args;
e37dd06a 38037+ *a->errp = xino_fread(a->file, a->buf, a->size, a->pos);
062440b3
AM
38038+}
38039+
e37dd06a
AM
38040+static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
38041+ loff_t *pos)
062440b3
AM
38042+{
38043+ ssize_t err;
38044+ int wkq_err;
38045+ struct xino_fread_args args = {
38046+ .errp = &err,
062440b3
AM
38047+ .file = file,
38048+ .buf = buf,
38049+ .size = size,
38050+ .pos = pos
38051+ };
38052+
38053+ wkq_err = au_wkq_wait(call_xino_fread, &args);
38054+ if (unlikely(wkq_err))
38055+ err = wkq_err;
38056+
38057+ return err;
38058+}
38059+
e37dd06a
AM
38060+static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
38061+ loff_t *pos);
062440b3 38062+
e37dd06a
AM
38063+static ssize_t do_xino_fwrite(struct file *file, void *kbuf, size_t size,
38064+ loff_t *pos)
062440b3
AM
38065+{
38066+ ssize_t err;
062440b3
AM
38067+ int i;
38068+ const int prevent_endless = 10;
38069+
38070+ i = 0;
062440b3 38071+ do {
e37dd06a 38072+ err = vfsub_write_k(file, kbuf, size, pos);
062440b3
AM
38073+ if (err == -EINTR
38074+ && !au_wkq_test()
38075+ && fatal_signal_pending(current)) {
e37dd06a 38076+ err = xino_fwrite_wkq(file, kbuf, size, pos);
062440b3 38077+ BUG_ON(err == -EINTR);
062440b3
AM
38078+ }
38079+ } while (i++ < prevent_endless
38080+ && (err == -EAGAIN || err == -EINTR));
062440b3
AM
38081+
38082+#if 0 /* reserved for future use */
38083+ if (err > 0)
38084+ fsnotify_modify(file->f_path.dentry);
38085+#endif
38086+
38087+ return err;
38088+}
38089+
38090+struct do_xino_fwrite_args {
38091+ ssize_t *errp;
062440b3
AM
38092+ struct file *file;
38093+ void *buf;
38094+ size_t size;
38095+ loff_t *pos;
38096+};
38097+
38098+static void call_do_xino_fwrite(void *args)
38099+{
38100+ struct do_xino_fwrite_args *a = args;
e37dd06a 38101+ *a->errp = do_xino_fwrite(a->file, a->buf, a->size, a->pos);
062440b3
AM
38102+}
38103+
e37dd06a
AM
38104+static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
38105+ loff_t *pos)
062440b3
AM
38106+{
38107+ ssize_t err;
38108+ int wkq_err;
38109+ struct do_xino_fwrite_args args = {
38110+ .errp = &err,
062440b3
AM
38111+ .file = file,
38112+ .buf = buf,
38113+ .size = size,
38114+ .pos = pos
38115+ };
38116+
38117+ /*
38118+ * it breaks RLIMIT_FSIZE and normal user's limit,
38119+ * users should care about quota and real 'filesystem full.'
38120+ */
38121+ wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
38122+ if (unlikely(wkq_err))
38123+ err = wkq_err;
38124+
38125+ return err;
38126+}
38127+
e37dd06a 38128+ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos)
062440b3
AM
38129+{
38130+ ssize_t err;
38131+
38132+ if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
38133+ lockdep_off();
e37dd06a 38134+ err = do_xino_fwrite(file, buf, size, pos);
062440b3
AM
38135+ lockdep_on();
38136+ } else {
38137+ lockdep_off();
e37dd06a 38138+ err = xino_fwrite_wkq(file, buf, size, pos);
062440b3
AM
38139+ lockdep_on();
38140+ }
38141+
38142+ return err;
38143+}
38144+
38145+/* ---------------------------------------------------------------------- */
38146+
38147+/*
38148+ * inode number bitmap
38149+ */
1facf9fc 38150+static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
38151+static ino_t xib_calc_ino(unsigned long pindex, int bit)
38152+{
38153+ ino_t ino;
38154+
38155+ AuDebugOn(bit < 0 || page_bits <= bit);
38156+ ino = AUFS_FIRST_INO + pindex * page_bits + bit;
38157+ return ino;
38158+}
38159+
38160+static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
38161+{
38162+ AuDebugOn(ino < AUFS_FIRST_INO);
38163+ ino -= AUFS_FIRST_INO;
38164+ *pindex = ino / page_bits;
38165+ *bit = ino % page_bits;
38166+}
38167+
38168+static int xib_pindex(struct super_block *sb, unsigned long pindex)
38169+{
38170+ int err;
38171+ loff_t pos;
38172+ ssize_t sz;
38173+ struct au_sbinfo *sbinfo;
38174+ struct file *xib;
38175+ unsigned long *p;
38176+
38177+ sbinfo = au_sbi(sb);
38178+ MtxMustLock(&sbinfo->si_xib_mtx);
38179+ AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
38180+ || !au_opt_test(sbinfo->si_mntflags, XINO));
38181+
38182+ if (pindex == sbinfo->si_xib_last_pindex)
38183+ return 0;
38184+
38185+ xib = sbinfo->si_xib;
38186+ p = sbinfo->si_xib_buf;
38187+ pos = sbinfo->si_xib_last_pindex;
38188+ pos *= PAGE_SIZE;
e37dd06a 38189+ sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
1facf9fc 38190+ if (unlikely(sz != PAGE_SIZE))
38191+ goto out;
38192+
38193+ pos = pindex;
38194+ pos *= PAGE_SIZE;
c06a8ce3 38195+ if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
e37dd06a 38196+ sz = xino_fread(xib, p, PAGE_SIZE, &pos);
1facf9fc 38197+ else {
38198+ memset(p, 0, PAGE_SIZE);
e37dd06a 38199+ sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
1facf9fc 38200+ }
38201+ if (sz == PAGE_SIZE) {
38202+ sbinfo->si_xib_last_pindex = pindex;
38203+ return 0; /* success */
38204+ }
38205+
4f0767ce 38206+out:
b752ccd1
AM
38207+ AuIOErr1("write failed (%zd)\n", sz);
38208+ err = sz;
38209+ if (sz >= 0)
38210+ err = -EIO;
38211+ return err;
38212+}
38213+
b752ccd1
AM
38214+static void au_xib_clear_bit(struct inode *inode)
38215+{
38216+ int err, bit;
38217+ unsigned long pindex;
38218+ struct super_block *sb;
38219+ struct au_sbinfo *sbinfo;
38220+
38221+ AuDebugOn(inode->i_nlink);
38222+
38223+ sb = inode->i_sb;
38224+ xib_calc_bit(inode->i_ino, &pindex, &bit);
38225+ AuDebugOn(page_bits <= bit);
38226+ sbinfo = au_sbi(sb);
38227+ mutex_lock(&sbinfo->si_xib_mtx);
38228+ err = xib_pindex(sb, pindex);
38229+ if (!err) {
38230+ clear_bit(bit, sbinfo->si_xib_buf);
38231+ sbinfo->si_xib_next_bit = bit;
38232+ }
38233+ mutex_unlock(&sbinfo->si_xib_mtx);
38234+}
38235+
1facf9fc 38236+/* ---------------------------------------------------------------------- */
38237+
1facf9fc 38238+/*
062440b3 38239+ * truncate a xino bitmap file
1facf9fc 38240+ */
1facf9fc 38241+
38242+/* todo: slow */
38243+static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
38244+{
38245+ int err, bit;
38246+ ssize_t sz;
38247+ unsigned long pindex;
38248+ loff_t pos, pend;
38249+ struct au_sbinfo *sbinfo;
1facf9fc 38250+ ino_t *ino;
38251+ unsigned long *p;
38252+
38253+ err = 0;
38254+ sbinfo = au_sbi(sb);
dece6358 38255+ MtxMustLock(&sbinfo->si_xib_mtx);
1facf9fc 38256+ p = sbinfo->si_xib_buf;
c06a8ce3 38257+ pend = vfsub_f_size_read(file);
1facf9fc 38258+ pos = 0;
38259+ while (pos < pend) {
e37dd06a 38260+ sz = xino_fread(file, page, PAGE_SIZE, &pos);
1facf9fc 38261+ err = sz;
38262+ if (unlikely(sz <= 0))
38263+ goto out;
38264+
38265+ err = 0;
38266+ for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
38267+ if (unlikely(*ino < AUFS_FIRST_INO))
38268+ continue;
38269+
38270+ xib_calc_bit(*ino, &pindex, &bit);
38271+ AuDebugOn(page_bits <= bit);
38272+ err = xib_pindex(sb, pindex);
38273+ if (!err)
38274+ set_bit(bit, p);
38275+ else
38276+ goto out;
38277+ }
38278+ }
38279+
4f0767ce 38280+out:
1facf9fc 38281+ return err;
38282+}
38283+
38284+static int xib_restore(struct super_block *sb)
38285+{
acd2b654
AM
38286+ int err, i;
38287+ unsigned int nfile;
5afbbe0d 38288+ aufs_bindex_t bindex, bbot;
1facf9fc 38289+ void *page;
062440b3 38290+ struct au_branch *br;
acd2b654
AM
38291+ struct au_xino *xi;
38292+ struct file *file;
1facf9fc 38293+
38294+ err = -ENOMEM;
38295+ page = (void *)__get_free_page(GFP_NOFS);
38296+ if (unlikely(!page))
38297+ goto out;
38298+
38299+ err = 0;
5afbbe0d
AM
38300+ bbot = au_sbbot(sb);
38301+ for (bindex = 0; !err && bindex <= bbot; bindex++)
062440b3
AM
38302+ if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0) {
38303+ br = au_sbr(sb, bindex);
acd2b654
AM
38304+ xi = br->br_xino;
38305+ nfile = xi->xi_nfile;
38306+ for (i = 0; i < nfile; i++) {
38307+ file = au_xino_file(xi, i);
38308+ if (file)
38309+ err = do_xib_restore(sb, file, page);
38310+ }
062440b3
AM
38311+ } else
38312+ AuDbg("skip shared b%d\n", bindex);
1c60b727 38313+ free_page((unsigned long)page);
1facf9fc 38314+
4f0767ce 38315+out:
1facf9fc 38316+ return err;
38317+}
38318+
38319+int au_xib_trunc(struct super_block *sb)
38320+{
38321+ int err;
38322+ ssize_t sz;
38323+ loff_t pos;
1facf9fc 38324+ struct au_sbinfo *sbinfo;
38325+ unsigned long *p;
38326+ struct file *file;
38327+
dece6358
AM
38328+ SiMustWriteLock(sb);
38329+
1facf9fc 38330+ err = 0;
38331+ sbinfo = au_sbi(sb);
38332+ if (!au_opt_test(sbinfo->si_mntflags, XINO))
38333+ goto out;
38334+
38335+ file = sbinfo->si_xib;
c06a8ce3 38336+ if (vfsub_f_size_read(file) <= PAGE_SIZE)
1facf9fc 38337+ goto out;
38338+
062440b3 38339+ file = au_xino_create2(sb, &sbinfo->si_xib->f_path, NULL);
1facf9fc 38340+ err = PTR_ERR(file);
38341+ if (IS_ERR(file))
38342+ goto out;
38343+ fput(sbinfo->si_xib);
38344+ sbinfo->si_xib = file;
38345+
38346+ p = sbinfo->si_xib_buf;
38347+ memset(p, 0, PAGE_SIZE);
38348+ pos = 0;
e37dd06a 38349+ sz = xino_fwrite(sbinfo->si_xib, p, PAGE_SIZE, &pos);
1facf9fc 38350+ if (unlikely(sz != PAGE_SIZE)) {
38351+ err = sz;
38352+ AuIOErr("err %d\n", err);
38353+ if (sz >= 0)
38354+ err = -EIO;
38355+ goto out;
38356+ }
38357+
38358+ mutex_lock(&sbinfo->si_xib_mtx);
38359+ /* mnt_want_write() is unnecessary here */
38360+ err = xib_restore(sb);
38361+ mutex_unlock(&sbinfo->si_xib_mtx);
38362+
38363+out:
38364+ return err;
38365+}
38366+
38367+/* ---------------------------------------------------------------------- */
38368+
acd2b654 38369+struct au_xino *au_xino_alloc(unsigned int nfile)
062440b3
AM
38370+{
38371+ struct au_xino *xi;
38372+
38373+ xi = kzalloc(sizeof(*xi), GFP_NOFS);
38374+ if (unlikely(!xi))
38375+ goto out;
acd2b654
AM
38376+ xi->xi_nfile = nfile;
38377+ xi->xi_file = kcalloc(nfile, sizeof(*xi->xi_file), GFP_NOFS);
38378+ if (unlikely(!xi->xi_file))
38379+ goto out_free;
062440b3
AM
38380+
38381+ xi->xi_nondir.total = 8; /* initial size */
38382+ xi->xi_nondir.array = kcalloc(xi->xi_nondir.total, sizeof(ino_t),
38383+ GFP_NOFS);
38384+ if (unlikely(!xi->xi_nondir.array))
acd2b654 38385+ goto out_file;
062440b3
AM
38386+
38387+ spin_lock_init(&xi->xi_nondir.spin);
38388+ init_waitqueue_head(&xi->xi_nondir.wqh);
acd2b654 38389+ mutex_init(&xi->xi_mtx);
9f237c51 38390+ INIT_HLIST_BL_HEAD(&xi->xi_writing);
062440b3
AM
38391+ atomic_set(&xi->xi_truncating, 0);
38392+ kref_init(&xi->xi_kref);
38393+ goto out; /* success */
38394+
acd2b654 38395+out_file:
9f237c51 38396+ au_kfree_try_rcu(xi->xi_file);
062440b3 38397+out_free:
9f237c51 38398+ au_kfree_rcu(xi);
062440b3
AM
38399+ xi = NULL;
38400+out:
38401+ return xi;
38402+}
38403+
acd2b654 38404+static int au_xino_init(struct au_branch *br, int idx, struct file *file)
062440b3
AM
38405+{
38406+ int err;
38407+ struct au_xino *xi;
38408+
38409+ err = 0;
acd2b654 38410+ xi = au_xino_alloc(idx + 1);
062440b3
AM
38411+ if (unlikely(!xi)) {
38412+ err = -ENOMEM;
38413+ goto out;
38414+ }
38415+
acd2b654
AM
38416+ if (file)
38417+ get_file(file);
38418+ xi->xi_file[idx] = file;
062440b3
AM
38419+ AuDebugOn(br->br_xino);
38420+ br->br_xino = xi;
38421+
38422+out:
38423+ return err;
38424+}
38425+
38426+static void au_xino_release(struct kref *kref)
38427+{
38428+ struct au_xino *xi;
38429+ int i;
9f237c51
AM
38430+ unsigned long ul;
38431+ struct hlist_bl_head *hbl;
38432+ struct hlist_bl_node *pos, *n;
38433+ struct au_xi_writing *p;
062440b3
AM
38434+
38435+ xi = container_of(kref, struct au_xino, xi_kref);
acd2b654
AM
38436+ for (i = 0; i < xi->xi_nfile; i++)
38437+ if (xi->xi_file[i])
38438+ fput(xi->xi_file[i]);
062440b3
AM
38439+ for (i = xi->xi_nondir.total - 1; i >= 0; i--)
38440+ AuDebugOn(xi->xi_nondir.array[i]);
acd2b654 38441+ mutex_destroy(&xi->xi_mtx);
9f237c51
AM
38442+ hbl = &xi->xi_writing;
38443+ ul = au_hbl_count(hbl);
38444+ if (unlikely(ul)) {
38445+ pr_warn("xi_writing %lu\n", ul);
38446+ hlist_bl_lock(hbl);
43982f53 38447+ hlist_bl_for_each_entry_safe(p, pos, n, hbl, node) {
9f237c51 38448+ hlist_bl_del(&p->node);
394e211a
AM
38449+ /* kmemleak reported au_kfree_rcu() doesn't free it */
38450+ kfree(p);
9f237c51
AM
38451+ }
38452+ hlist_bl_unlock(hbl);
38453+ }
38454+ au_kfree_try_rcu(xi->xi_file);
38455+ au_kfree_try_rcu(xi->xi_nondir.array);
38456+ au_kfree_rcu(xi);
062440b3
AM
38457+}
38458+
38459+int au_xino_put(struct au_branch *br)
38460+{
38461+ int ret;
38462+ struct au_xino *xi;
38463+
38464+ ret = 0;
38465+ xi = br->br_xino;
38466+ if (xi) {
38467+ br->br_xino = NULL;
38468+ ret = kref_put(&xi->xi_kref, au_xino_release);
38469+ }
38470+
38471+ return ret;
38472+}
38473+
062440b3
AM
38474+/* ---------------------------------------------------------------------- */
38475+
1facf9fc 38476+/*
38477+ * xino mount option handlers
38478+ */
1facf9fc 38479+
38480+/* xino bitmap */
38481+static void xino_clear_xib(struct super_block *sb)
38482+{
38483+ struct au_sbinfo *sbinfo;
38484+
dece6358
AM
38485+ SiMustWriteLock(sb);
38486+
1facf9fc 38487+ sbinfo = au_sbi(sb);
1facf9fc 38488+ if (sbinfo->si_xib)
38489+ fput(sbinfo->si_xib);
38490+ sbinfo->si_xib = NULL;
f0c0a007 38491+ if (sbinfo->si_xib_buf)
1c60b727 38492+ free_page((unsigned long)sbinfo->si_xib_buf);
1facf9fc 38493+ sbinfo->si_xib_buf = NULL;
38494+}
38495+
062440b3 38496+static int au_xino_set_xib(struct super_block *sb, struct path *path)
1facf9fc 38497+{
38498+ int err;
38499+ loff_t pos;
38500+ struct au_sbinfo *sbinfo;
38501+ struct file *file;
acd2b654 38502+ struct super_block *xi_sb;
1facf9fc 38503+
dece6358
AM
38504+ SiMustWriteLock(sb);
38505+
1facf9fc 38506+ sbinfo = au_sbi(sb);
062440b3 38507+ file = au_xino_create2(sb, path, sbinfo->si_xib);
1facf9fc 38508+ err = PTR_ERR(file);
38509+ if (IS_ERR(file))
38510+ goto out;
38511+ if (sbinfo->si_xib)
38512+ fput(sbinfo->si_xib);
38513+ sbinfo->si_xib = file;
acd2b654
AM
38514+ xi_sb = file_inode(file)->i_sb;
38515+ sbinfo->si_ximaxent = xi_sb->s_maxbytes;
38516+ if (unlikely(sbinfo->si_ximaxent < PAGE_SIZE)) {
38517+ err = -EIO;
38518+ pr_err("s_maxbytes(%llu) on %s is too small\n",
38519+ (u64)sbinfo->si_ximaxent, au_sbtype(xi_sb));
38520+ goto out_unset;
38521+ }
38522+ sbinfo->si_ximaxent /= sizeof(ino_t);
1facf9fc 38523+
38524+ err = -ENOMEM;
38525+ if (!sbinfo->si_xib_buf)
38526+ sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
38527+ if (unlikely(!sbinfo->si_xib_buf))
38528+ goto out_unset;
38529+
38530+ sbinfo->si_xib_last_pindex = 0;
38531+ sbinfo->si_xib_next_bit = 0;
c06a8ce3 38532+ if (vfsub_f_size_read(file) < PAGE_SIZE) {
1facf9fc 38533+ pos = 0;
e37dd06a 38534+ err = xino_fwrite(file, sbinfo->si_xib_buf, PAGE_SIZE, &pos);
1facf9fc 38535+ if (unlikely(err != PAGE_SIZE))
38536+ goto out_free;
38537+ }
38538+ err = 0;
38539+ goto out; /* success */
38540+
4f0767ce 38541+out_free:
f0c0a007 38542+ if (sbinfo->si_xib_buf)
1c60b727 38543+ free_page((unsigned long)sbinfo->si_xib_buf);
b752ccd1
AM
38544+ sbinfo->si_xib_buf = NULL;
38545+ if (err >= 0)
38546+ err = -EIO;
4f0767ce 38547+out_unset:
b752ccd1
AM
38548+ fput(sbinfo->si_xib);
38549+ sbinfo->si_xib = NULL;
4f0767ce 38550+out:
062440b3 38551+ AuTraceErr(err);
b752ccd1 38552+ return err;
1facf9fc 38553+}
38554+
b752ccd1
AM
38555+/* xino for each branch */
38556+static void xino_clear_br(struct super_block *sb)
38557+{
5afbbe0d 38558+ aufs_bindex_t bindex, bbot;
b752ccd1 38559+ struct au_branch *br;
1facf9fc 38560+
5afbbe0d
AM
38561+ bbot = au_sbbot(sb);
38562+ for (bindex = 0; bindex <= bbot; bindex++) {
b752ccd1 38563+ br = au_sbr(sb, bindex);
062440b3
AM
38564+ AuDebugOn(!br);
38565+ au_xino_put(br);
38566+ }
38567+}
38568+
38569+static void au_xino_set_br_shared(struct super_block *sb, struct au_branch *br,
38570+ aufs_bindex_t bshared)
38571+{
38572+ struct au_branch *brshared;
b752ccd1 38573+
062440b3
AM
38574+ brshared = au_sbr(sb, bshared);
38575+ AuDebugOn(!brshared->br_xino);
38576+ AuDebugOn(!brshared->br_xino->xi_file);
38577+ if (br->br_xino != brshared->br_xino) {
38578+ au_xino_get(brshared);
38579+ au_xino_put(br);
38580+ br->br_xino = brshared->br_xino;
b752ccd1
AM
38581+ }
38582+}
38583+
062440b3 38584+struct au_xino_do_set_br {
062440b3
AM
38585+ struct au_branch *br;
38586+ ino_t h_ino;
38587+ aufs_bindex_t bshared;
38588+};
38589+
38590+static int au_xino_do_set_br(struct super_block *sb, struct path *path,
38591+ struct au_xino_do_set_br *args)
1facf9fc 38592+{
38593+ int err;
acd2b654 38594+ struct au_xi_calc calc;
062440b3 38595+ struct file *file;
acd2b654
AM
38596+ struct au_branch *br;
38597+ struct au_xi_new xinew = {
38598+ .base = path
38599+ };
062440b3 38600+
acd2b654
AM
38601+ br = args->br;
38602+ xinew.xi = br->br_xino;
38603+ au_xi_calc(sb, args->h_ino, &calc);
38604+ xinew.copy_src = au_xino_file(xinew.xi, calc.idx);
38605+ if (args->bshared >= 0)
062440b3 38606+ /* shared xino */
acd2b654
AM
38607+ au_xino_set_br_shared(sb, br, args->bshared);
38608+ else if (!xinew.xi) {
38609+ /* new xino */
38610+ err = au_xino_init(br, calc.idx, xinew.copy_src);
38611+ if (unlikely(err))
38612+ goto out;
062440b3
AM
38613+ }
38614+
acd2b654
AM
38615+ /* force re-creating */
38616+ xinew.xi = br->br_xino;
38617+ xinew.idx = calc.idx;
38618+ mutex_lock(&xinew.xi->xi_mtx);
38619+ file = au_xi_new(sb, &xinew);
38620+ mutex_unlock(&xinew.xi->xi_mtx);
062440b3
AM
38621+ err = PTR_ERR(file);
38622+ if (IS_ERR(file))
38623+ goto out;
acd2b654
AM
38624+ AuDebugOn(!file);
38625+
e37dd06a 38626+ err = au_xino_do_write(file, &calc, AUFS_ROOT_INO);
acd2b654
AM
38627+ if (unlikely(err))
38628+ au_xino_put(br);
062440b3 38629+
062440b3
AM
38630+out:
38631+ AuTraceErr(err);
38632+ return err;
38633+}
38634+
38635+static int au_xino_set_br(struct super_block *sb, struct path *path)
38636+{
38637+ int err;
38638+ aufs_bindex_t bindex, bbot;
38639+ struct au_xino_do_set_br args;
b752ccd1 38640+ struct inode *inode;
1facf9fc 38641+
b752ccd1
AM
38642+ SiMustWriteLock(sb);
38643+
5afbbe0d 38644+ bbot = au_sbbot(sb);
5527c038 38645+ inode = d_inode(sb->s_root);
062440b3
AM
38646+ for (bindex = 0; bindex <= bbot; bindex++) {
38647+ args.h_ino = au_h_iptr(inode, bindex)->i_ino;
38648+ args.br = au_sbr(sb, bindex);
38649+ args.bshared = is_sb_shared(sb, bindex, bindex - 1);
38650+ err = au_xino_do_set_br(sb, path, &args);
b752ccd1 38651+ if (unlikely(err))
062440b3 38652+ break;
b752ccd1 38653+ }
1facf9fc 38654+
062440b3 38655+ AuTraceErr(err);
1facf9fc 38656+ return err;
38657+}
b752ccd1
AM
38658+
38659+void au_xino_clr(struct super_block *sb)
38660+{
38661+ struct au_sbinfo *sbinfo;
38662+
38663+ au_xigen_clr(sb);
38664+ xino_clear_xib(sb);
38665+ xino_clear_br(sb);
062440b3 38666+ dbgaufs_brs_del(sb, 0);
b752ccd1
AM
38667+ sbinfo = au_sbi(sb);
38668+ /* lvalue, do not call au_mntflags() */
38669+ au_opt_clr(sbinfo->si_mntflags, XINO);
38670+}
38671+
062440b3 38672+int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount)
b752ccd1
AM
38673+{
38674+ int err, skip;
062440b3 38675+ struct dentry *dentry, *parent, *cur_dentry, *cur_parent;
b752ccd1
AM
38676+ struct qstr *dname, *cur_name;
38677+ struct file *cur_xino;
b752ccd1 38678+ struct au_sbinfo *sbinfo;
062440b3 38679+ struct path *path, *cur_path;
b752ccd1
AM
38680+
38681+ SiMustWriteLock(sb);
38682+
38683+ err = 0;
38684+ sbinfo = au_sbi(sb);
062440b3
AM
38685+ path = &xiopt->file->f_path;
38686+ dentry = path->dentry;
38687+ parent = dget_parent(dentry);
b752ccd1
AM
38688+ if (remount) {
38689+ skip = 0;
b752ccd1
AM
38690+ cur_xino = sbinfo->si_xib;
38691+ if (cur_xino) {
062440b3
AM
38692+ cur_path = &cur_xino->f_path;
38693+ cur_dentry = cur_path->dentry;
38694+ cur_parent = dget_parent(cur_dentry);
38695+ cur_name = &cur_dentry->d_name;
38696+ dname = &dentry->d_name;
b752ccd1 38697+ skip = (cur_parent == parent
38d290e6 38698+ && au_qstreq(dname, cur_name));
b752ccd1
AM
38699+ dput(cur_parent);
38700+ }
38701+ if (skip)
38702+ goto out;
38703+ }
38704+
38705+ au_opt_set(sbinfo->si_mntflags, XINO);
062440b3
AM
38706+ err = au_xino_set_xib(sb, path);
38707+ /* si_x{read,write} are set */
b752ccd1 38708+ if (!err)
062440b3 38709+ err = au_xigen_set(sb, path);
b752ccd1 38710+ if (!err)
062440b3
AM
38711+ err = au_xino_set_br(sb, path);
38712+ if (!err) {
38713+ dbgaufs_brs_add(sb, 0, /*topdown*/1);
b752ccd1 38714+ goto out; /* success */
062440b3 38715+ }
b752ccd1
AM
38716+
38717+ /* reset all */
062440b3
AM
38718+ AuIOErr("failed setting xino(%d).\n", err);
38719+ au_xino_clr(sb);
b752ccd1 38720+
4f0767ce 38721+out:
b752ccd1
AM
38722+ dput(parent);
38723+ return err;
38724+}
38725+
b752ccd1
AM
38726+/*
38727+ * create a xinofile at the default place/path.
38728+ */
38729+struct file *au_xino_def(struct super_block *sb)
38730+{
38731+ struct file *file;
38732+ char *page, *p;
38733+ struct au_branch *br;
38734+ struct super_block *h_sb;
38735+ struct path path;
5afbbe0d 38736+ aufs_bindex_t bbot, bindex, bwr;
b752ccd1
AM
38737+
38738+ br = NULL;
5afbbe0d 38739+ bbot = au_sbbot(sb);
b752ccd1 38740+ bwr = -1;
5afbbe0d 38741+ for (bindex = 0; bindex <= bbot; bindex++) {
b752ccd1
AM
38742+ br = au_sbr(sb, bindex);
38743+ if (au_br_writable(br->br_perm)
86dc4139 38744+ && !au_test_fs_bad_xino(au_br_sb(br))) {
b752ccd1
AM
38745+ bwr = bindex;
38746+ break;
38747+ }
38748+ }
38749+
7f207e10
AM
38750+ if (bwr >= 0) {
38751+ file = ERR_PTR(-ENOMEM);
537831f9 38752+ page = (void *)__get_free_page(GFP_NOFS);
7f207e10
AM
38753+ if (unlikely(!page))
38754+ goto out;
86dc4139 38755+ path.mnt = au_br_mnt(br);
7f207e10
AM
38756+ path.dentry = au_h_dptr(sb->s_root, bwr);
38757+ p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
38758+ file = (void *)p;
38759+ if (!IS_ERR(p)) {
38760+ strcat(p, "/" AUFS_XINO_FNAME);
38761+ AuDbg("%s\n", p);
83b672a5 38762+ file = au_xino_create(sb, p, /*silent*/0, /*wbrtop*/1);
7f207e10 38763+ }
1c60b727 38764+ free_page((unsigned long)page);
7f207e10 38765+ } else {
83b672a5
AM
38766+ file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0,
38767+ /*wbrtop*/0);
7f207e10
AM
38768+ if (IS_ERR(file))
38769+ goto out;
2000de60 38770+ h_sb = file->f_path.dentry->d_sb;
7f207e10
AM
38771+ if (unlikely(au_test_fs_bad_xino(h_sb))) {
38772+ pr_err("xino doesn't support %s(%s)\n",
38773+ AUFS_XINO_DEFPATH, au_sbtype(h_sb));
38774+ fput(file);
38775+ file = ERR_PTR(-EINVAL);
38776+ }
7f207e10 38777+ }
0c5527e5 38778+
7f207e10
AM
38779+out:
38780+ return file;
38781+}
38782+
38783+/* ---------------------------------------------------------------------- */
38784+
062440b3
AM
38785+/*
38786+ * initialize the xinofile for the specified branch @br
38787+ * at the place/path where @base_file indicates.
38788+ * test whether another branch is on the same filesystem or not,
38789+ * if found then share the xinofile with another branch.
38790+ */
38791+int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
38792+ struct path *base)
7f207e10
AM
38793+{
38794+ int err;
062440b3
AM
38795+ struct au_xino_do_set_br args = {
38796+ .h_ino = h_ino,
38797+ .br = br
38798+ };
7f207e10 38799+
062440b3
AM
38800+ args.bshared = sbr_find_shared(sb, /*btop*/0, au_sbbot(sb),
38801+ au_br_sb(br));
38802+ err = au_xino_do_set_br(sb, base, &args);
79b8bda9 38803+ if (unlikely(err))
062440b3 38804+ au_xino_put(br);
7f207e10 38805+
7f207e10
AM
38806+ return err;
38807+}
521ced18
JR
38808+
38809+/* ---------------------------------------------------------------------- */
38810+
062440b3
AM
38811+/*
38812+ * get an unused inode number from bitmap
38813+ */
38814+ino_t au_xino_new_ino(struct super_block *sb)
38815+{
38816+ ino_t ino;
38817+ unsigned long *p, pindex, ul, pend;
38818+ struct au_sbinfo *sbinfo;
38819+ struct file *file;
38820+ int free_bit, err;
38821+
38822+ if (!au_opt_test(au_mntflags(sb), XINO))
38823+ return iunique(sb, AUFS_FIRST_INO);
38824+
38825+ sbinfo = au_sbi(sb);
38826+ mutex_lock(&sbinfo->si_xib_mtx);
38827+ p = sbinfo->si_xib_buf;
38828+ free_bit = sbinfo->si_xib_next_bit;
38829+ if (free_bit < page_bits && !test_bit(free_bit, p))
38830+ goto out; /* success */
38831+ free_bit = find_first_zero_bit(p, page_bits);
38832+ if (free_bit < page_bits)
38833+ goto out; /* success */
38834+
38835+ pindex = sbinfo->si_xib_last_pindex;
38836+ for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
38837+ err = xib_pindex(sb, ul);
38838+ if (unlikely(err))
38839+ goto out_err;
38840+ free_bit = find_first_zero_bit(p, page_bits);
38841+ if (free_bit < page_bits)
38842+ goto out; /* success */
38843+ }
38844+
38845+ file = sbinfo->si_xib;
38846+ pend = vfsub_f_size_read(file) / PAGE_SIZE;
38847+ for (ul = pindex + 1; ul <= pend; ul++) {
38848+ err = xib_pindex(sb, ul);
38849+ if (unlikely(err))
38850+ goto out_err;
38851+ free_bit = find_first_zero_bit(p, page_bits);
38852+ if (free_bit < page_bits)
38853+ goto out; /* success */
38854+ }
38855+ BUG();
38856+
38857+out:
38858+ set_bit(free_bit, p);
38859+ sbinfo->si_xib_next_bit = free_bit + 1;
38860+ pindex = sbinfo->si_xib_last_pindex;
38861+ mutex_unlock(&sbinfo->si_xib_mtx);
38862+ ino = xib_calc_ino(pindex, free_bit);
38863+ AuDbg("i%lu\n", (unsigned long)ino);
38864+ return ino;
38865+out_err:
38866+ mutex_unlock(&sbinfo->si_xib_mtx);
38867+ AuDbg("i0\n");
38868+ return 0;
38869+}
38870+
38871+/* for s_op->delete_inode() */
38872+void au_xino_delete_inode(struct inode *inode, const int unlinked)
521ced18 38873+{
062440b3
AM
38874+ int err;
38875+ unsigned int mnt_flags;
38876+ aufs_bindex_t bindex, bbot, bi;
38877+ unsigned char try_trunc;
38878+ struct au_iinfo *iinfo;
38879+ struct super_block *sb;
38880+ struct au_hinode *hi;
38881+ struct inode *h_inode;
38882+ struct au_branch *br;
acd2b654
AM
38883+ struct au_xi_calc calc;
38884+ struct file *file;
521ced18 38885+
062440b3 38886+ AuDebugOn(au_is_bad_inode(inode));
521ced18 38887+
062440b3
AM
38888+ sb = inode->i_sb;
38889+ mnt_flags = au_mntflags(sb);
38890+ if (!au_opt_test(mnt_flags, XINO)
38891+ || inode->i_ino == AUFS_ROOT_INO)
38892+ return;
38893+
38894+ if (unlinked) {
38895+ au_xigen_inc(inode);
38896+ au_xib_clear_bit(inode);
38897+ }
38898+
38899+ iinfo = au_ii(inode);
38900+ bindex = iinfo->ii_btop;
38901+ if (bindex < 0)
38902+ return;
38903+
062440b3
AM
38904+ try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
38905+ hi = au_hinode(iinfo, bindex);
38906+ bbot = iinfo->ii_bbot;
38907+ for (; bindex <= bbot; bindex++, hi++) {
38908+ h_inode = hi->hi_inode;
38909+ if (!h_inode
38910+ || (!unlinked && h_inode->i_nlink))
38911+ continue;
38912+
38913+ /* inode may not be revalidated */
38914+ bi = au_br_index(sb, hi->hi_id);
38915+ if (bi < 0)
38916+ continue;
38917+
38918+ br = au_sbr(sb, bi);
acd2b654
AM
38919+ au_xi_calc(sb, h_inode->i_ino, &calc);
38920+ file = au_xino_file(br->br_xino, calc.idx);
38921+ if (IS_ERR_OR_NULL(file))
38922+ continue;
38923+
e37dd06a 38924+ err = au_xino_do_write(file, &calc, /*ino*/0);
062440b3
AM
38925+ if (!err && try_trunc
38926+ && au_test_fs_trunc_xino(au_br_sb(br)))
38927+ xino_try_trunc(sb, br);
38928+ }
521ced18
JR
38929+}
38930+
062440b3
AM
38931+/* ---------------------------------------------------------------------- */
38932+
38933+static int au_xinondir_find(struct au_xino *xi, ino_t h_ino)
521ced18
JR
38934+{
38935+ int found, total, i;
38936+
38937+ found = -1;
062440b3 38938+ total = xi->xi_nondir.total;
521ced18 38939+ for (i = 0; i < total; i++) {
062440b3 38940+ if (xi->xi_nondir.array[i] != h_ino)
521ced18
JR
38941+ continue;
38942+ found = i;
38943+ break;
38944+ }
38945+
38946+ return found;
38947+}
38948+
062440b3 38949+static int au_xinondir_expand(struct au_xino *xi)
521ced18
JR
38950+{
38951+ int err, sz;
38952+ ino_t *p;
38953+
38954+ BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
38955+
38956+ err = -ENOMEM;
062440b3 38957+ sz = xi->xi_nondir.total * sizeof(ino_t);
521ced18
JR
38958+ if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
38959+ goto out;
062440b3 38960+ p = au_kzrealloc(xi->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
521ced18
JR
38961+ /*may_shrink*/0);
38962+ if (p) {
062440b3
AM
38963+ xi->xi_nondir.array = p;
38964+ xi->xi_nondir.total <<= 1;
38965+ AuDbg("xi_nondir.total %d\n", xi->xi_nondir.total);
521ced18
JR
38966+ err = 0;
38967+ }
38968+
38969+out:
38970+ return err;
38971+}
38972+
062440b3
AM
38973+void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
38974+ ino_t h_ino, int idx)
38975+{
38976+ struct au_xino *xi;
38977+
38978+ AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
38979+ xi = au_sbr(sb, bindex)->br_xino;
38980+ AuDebugOn(idx < 0 || xi->xi_nondir.total <= idx);
38981+
38982+ spin_lock(&xi->xi_nondir.spin);
38983+ AuDebugOn(xi->xi_nondir.array[idx] != h_ino);
38984+ xi->xi_nondir.array[idx] = 0;
38985+ spin_unlock(&xi->xi_nondir.spin);
38986+ wake_up_all(&xi->xi_nondir.wqh);
38987+}
38988+
521ced18
JR
38989+int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
38990+ int *idx)
38991+{
38992+ int err, found, empty;
062440b3 38993+ struct au_xino *xi;
521ced18
JR
38994+
38995+ err = 0;
38996+ *idx = -1;
38997+ if (!au_opt_test(au_mntflags(sb), XINO))
38998+ goto out; /* no xino */
38999+
062440b3 39000+ xi = au_sbr(sb, bindex)->br_xino;
521ced18
JR
39001+
39002+again:
062440b3
AM
39003+ spin_lock(&xi->xi_nondir.spin);
39004+ found = au_xinondir_find(xi, h_ino);
521ced18 39005+ if (found == -1) {
062440b3 39006+ empty = au_xinondir_find(xi, /*h_ino*/0);
521ced18 39007+ if (empty == -1) {
062440b3
AM
39008+ empty = xi->xi_nondir.total;
39009+ err = au_xinondir_expand(xi);
521ced18
JR
39010+ if (unlikely(err))
39011+ goto out_unlock;
39012+ }
062440b3 39013+ xi->xi_nondir.array[empty] = h_ino;
521ced18
JR
39014+ *idx = empty;
39015+ } else {
062440b3
AM
39016+ spin_unlock(&xi->xi_nondir.spin);
39017+ wait_event(xi->xi_nondir.wqh,
39018+ xi->xi_nondir.array[found] != h_ino);
521ced18
JR
39019+ goto again;
39020+ }
39021+
39022+out_unlock:
062440b3
AM
39023+ spin_unlock(&xi->xi_nondir.spin);
39024+out:
39025+ return err;
39026+}
39027+
39028+/* ---------------------------------------------------------------------- */
39029+
39030+int au_xino_path(struct seq_file *seq, struct file *file)
39031+{
39032+ int err;
39033+
39034+ err = au_seq_path(seq, &file->f_path);
39035+ if (unlikely(err))
39036+ goto out;
39037+
39038+#define Deleted "\\040(deleted)"
39039+ seq->count -= sizeof(Deleted) - 1;
39040+ AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
39041+ sizeof(Deleted) - 1));
39042+#undef Deleted
39043+
521ced18
JR
39044+out:
39045+ return err;
39046+}
537831f9 39047diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
eca34b5c 39048--- /usr/share/empty/include/uapi/linux/aufs_type.h 1970-01-01 01:00:00.000000000 +0100
631230db 39049+++ linux/include/uapi/linux/aufs_type.h 2023-10-31 09:31:04.203214083 +0100
eca34b5c
AM
39050@@ -0,0 +1,452 @@
39051+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
7f207e10 39052+/*
0dcfbb52 39053+ * Copyright (C) 2005-2022 Junjiro R. Okajima
7f207e10 39054+ *
0dcfbb52 39055+ * This program is free software; you can redistribute it and/or modify
7f207e10
AM
39056+ * it under the terms of the GNU General Public License as published by
39057+ * the Free Software Foundation; either version 2 of the License, or
39058+ * (at your option) any later version.
39059+ *
39060+ * This program is distributed in the hope that it will be useful,
39061+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
39062+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39063+ * GNU General Public License for more details.
39064+ *
39065+ * You should have received a copy of the GNU General Public License
523b37e3 39066+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7f207e10
AM
39067+ */
39068+
39069+#ifndef __AUFS_TYPE_H__
39070+#define __AUFS_TYPE_H__
39071+
f6c5ef8b
AM
39072+#define AUFS_NAME "aufs"
39073+
9dbd164d 39074+#ifdef __KERNEL__
f6c5ef8b
AM
39075+/*
39076+ * define it before including all other headers.
39077+ * sched.h may use pr_* macros before defining "current", so define the
39078+ * no-current version first, and re-define later.
39079+ */
39080+#define pr_fmt(fmt) AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
39081+#include <linux/sched.h>
39082+#undef pr_fmt
a2a7ad62
AM
39083+#define pr_fmt(fmt) \
39084+ AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
39085+ (int)sizeof(current->comm), current->comm, current->pid
43982f53 39086+#include <linux/limits.h>
9dbd164d
AM
39087+#else
39088+#include <stdint.h>
39089+#include <sys/types.h>
43982f53 39090+#include <limits.h>
f6c5ef8b 39091+#endif /* __KERNEL__ */
7f207e10 39092+
631230db 39093+#define AUFS_VERSION "6.x-rcN-20230925"
7f207e10
AM
39094+
39095+/* todo? move this to linux-2.6.19/include/magic.h */
39096+#define AUFS_SUPER_MAGIC ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
39097+
39098+/* ---------------------------------------------------------------------- */
39099+
43982f53 39100+#ifdef __KERNEL__
7f207e10 39101+#ifdef CONFIG_AUFS_BRANCH_MAX_127
9dbd164d 39102+typedef int8_t aufs_bindex_t;
7f207e10
AM
39103+#define AUFS_BRANCH_MAX 127
39104+#else
9dbd164d 39105+typedef int16_t aufs_bindex_t;
7f207e10
AM
39106+#ifdef CONFIG_AUFS_BRANCH_MAX_511
39107+#define AUFS_BRANCH_MAX 511
39108+#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
39109+#define AUFS_BRANCH_MAX 1023
39110+#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
39111+#define AUFS_BRANCH_MAX 32767
39112+#endif
39113+#endif
39114+
7f207e10
AM
39115+#ifndef AUFS_BRANCH_MAX
39116+#error unknown CONFIG_AUFS_BRANCH_MAX value
39117+#endif
39118+#endif /* __KERNEL__ */
39119+
39120+/* ---------------------------------------------------------------------- */
39121+
7f207e10
AM
39122+#define AUFS_FSTYPE AUFS_NAME
39123+
39124+#define AUFS_ROOT_INO 2
39125+#define AUFS_FIRST_INO 11
39126+
39127+#define AUFS_WH_PFX ".wh."
39128+#define AUFS_WH_PFX_LEN ((int)sizeof(AUFS_WH_PFX) - 1)
39129+#define AUFS_WH_TMP_LEN 4
86dc4139 39130+/* a limit for rmdir/rename a dir and copyup */
7f207e10
AM
39131+#define AUFS_MAX_NAMELEN (NAME_MAX \
39132+ - AUFS_WH_PFX_LEN * 2 /* doubly whiteouted */\
39133+ - 1 /* dot */\
39134+ - AUFS_WH_TMP_LEN) /* hex */
39135+#define AUFS_XINO_FNAME "." AUFS_NAME ".xino"
39136+#define AUFS_XINO_DEFPATH "/tmp/" AUFS_XINO_FNAME
392086de
AM
39137+#define AUFS_XINO_DEF_SEC 30 /* seconds */
39138+#define AUFS_XINO_DEF_TRUNC 45 /* percentage */
7f207e10
AM
39139+#define AUFS_DIRWH_DEF 3
39140+#define AUFS_RDCACHE_DEF 10 /* seconds */
027c5e7a 39141+#define AUFS_RDCACHE_MAX 3600 /* seconds */
7f207e10
AM
39142+#define AUFS_RDBLK_DEF 512 /* bytes */
39143+#define AUFS_RDHASH_DEF 32
39144+#define AUFS_WKQ_NAME AUFS_NAME "d"
027c5e7a
AM
39145+#define AUFS_MFS_DEF_SEC 30 /* seconds */
39146+#define AUFS_MFS_MAX_SEC 3600 /* seconds */
076b876e 39147+#define AUFS_FHSM_CACHE_DEF_SEC 30 /* seconds */
86dc4139 39148+#define AUFS_PLINK_WARN 50 /* number of plinks in a single bucket */
7f207e10
AM
39149+
39150+/* pseudo-link maintenace under /proc */
39151+#define AUFS_PLINK_MAINT_NAME "plink_maint"
39152+#define AUFS_PLINK_MAINT_DIR "fs/" AUFS_NAME
39153+#define AUFS_PLINK_MAINT_PATH AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
39154+
8b6a4947
AM
39155+/* dirren, renamed dir */
39156+#define AUFS_DR_INFO_PFX AUFS_WH_PFX ".dr."
39157+#define AUFS_DR_BRHINO_NAME AUFS_WH_PFX "hino"
39158+/* whiteouted doubly */
39159+#define AUFS_WH_DR_INFO_PFX AUFS_WH_PFX AUFS_DR_INFO_PFX
39160+#define AUFS_WH_DR_BRHINO AUFS_WH_PFX AUFS_DR_BRHINO_NAME
39161+
7f207e10
AM
39162+#define AUFS_DIROPQ_NAME AUFS_WH_PFX ".opq" /* whiteouted doubly */
39163+#define AUFS_WH_DIROPQ AUFS_WH_PFX AUFS_DIROPQ_NAME
39164+
39165+#define AUFS_BASE_NAME AUFS_WH_PFX AUFS_NAME
39166+#define AUFS_PLINKDIR_NAME AUFS_WH_PFX "plnk"
39167+#define AUFS_ORPHDIR_NAME AUFS_WH_PFX "orph"
39168+
39169+/* doubly whiteouted */
39170+#define AUFS_WH_BASE AUFS_WH_PFX AUFS_BASE_NAME
39171+#define AUFS_WH_PLINKDIR AUFS_WH_PFX AUFS_PLINKDIR_NAME
39172+#define AUFS_WH_ORPHDIR AUFS_WH_PFX AUFS_ORPHDIR_NAME
39173+
1e00d052 39174+/* branch permissions and attributes */
7f207e10
AM
39175+#define AUFS_BRPERM_RW "rw"
39176+#define AUFS_BRPERM_RO "ro"
39177+#define AUFS_BRPERM_RR "rr"
076b876e
AM
39178+#define AUFS_BRATTR_COO_REG "coo_reg"
39179+#define AUFS_BRATTR_COO_ALL "coo_all"
39180+#define AUFS_BRATTR_FHSM "fhsm"
39181+#define AUFS_BRATTR_UNPIN "unpin"
c1595e42
JR
39182+#define AUFS_BRATTR_ICEX "icex"
39183+#define AUFS_BRATTR_ICEX_SEC "icexsec"
39184+#define AUFS_BRATTR_ICEX_SYS "icexsys"
39185+#define AUFS_BRATTR_ICEX_TR "icextr"
39186+#define AUFS_BRATTR_ICEX_USR "icexusr"
39187+#define AUFS_BRATTR_ICEX_OTH "icexoth"
1e00d052
AM
39188+#define AUFS_BRRATTR_WH "wh"
39189+#define AUFS_BRWATTR_NLWH "nolwh"
076b876e
AM
39190+#define AUFS_BRWATTR_MOO "moo"
39191+
39192+#define AuBrPerm_RW 1 /* writable, hardlinkable wh */
39193+#define AuBrPerm_RO (1 << 1) /* readonly */
39194+#define AuBrPerm_RR (1 << 2) /* natively readonly */
39195+#define AuBrPerm_Mask (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
39196+
39197+#define AuBrAttr_COO_REG (1 << 3) /* copy-up on open */
39198+#define AuBrAttr_COO_ALL (1 << 4)
39199+#define AuBrAttr_COO_Mask (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
39200+
39201+#define AuBrAttr_FHSM (1 << 5) /* file-based hsm */
39202+#define AuBrAttr_UNPIN (1 << 6) /* rename-able top dir of
c1595e42
JR
39203+ branch. meaningless since
39204+ linux-3.18-rc1 */
39205+
39206+/* ignore error in copying XATTR */
39207+#define AuBrAttr_ICEX_SEC (1 << 7)
39208+#define AuBrAttr_ICEX_SYS (1 << 8)
39209+#define AuBrAttr_ICEX_TR (1 << 9)
39210+#define AuBrAttr_ICEX_USR (1 << 10)
39211+#define AuBrAttr_ICEX_OTH (1 << 11)
39212+#define AuBrAttr_ICEX (AuBrAttr_ICEX_SEC \
39213+ | AuBrAttr_ICEX_SYS \
39214+ | AuBrAttr_ICEX_TR \
39215+ | AuBrAttr_ICEX_USR \
39216+ | AuBrAttr_ICEX_OTH)
39217+
39218+#define AuBrRAttr_WH (1 << 12) /* whiteout-able */
076b876e
AM
39219+#define AuBrRAttr_Mask AuBrRAttr_WH
39220+
c1595e42
JR
39221+#define AuBrWAttr_NoLinkWH (1 << 13) /* un-hardlinkable whiteouts */
39222+#define AuBrWAttr_MOO (1 << 14) /* move-up on open */
076b876e
AM
39223+#define AuBrWAttr_Mask (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
39224+
39225+#define AuBrAttr_CMOO_Mask (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
39226+
c1595e42 39227+/* #warning test userspace */
076b876e
AM
39228+#ifdef __KERNEL__
39229+#ifndef CONFIG_AUFS_FHSM
39230+#undef AuBrAttr_FHSM
39231+#define AuBrAttr_FHSM 0
39232+#endif
c1595e42
JR
39233+#ifndef CONFIG_AUFS_XATTR
39234+#undef AuBrAttr_ICEX
39235+#define AuBrAttr_ICEX 0
39236+#undef AuBrAttr_ICEX_SEC
39237+#define AuBrAttr_ICEX_SEC 0
39238+#undef AuBrAttr_ICEX_SYS
39239+#define AuBrAttr_ICEX_SYS 0
39240+#undef AuBrAttr_ICEX_TR
39241+#define AuBrAttr_ICEX_TR 0
39242+#undef AuBrAttr_ICEX_USR
39243+#define AuBrAttr_ICEX_USR 0
39244+#undef AuBrAttr_ICEX_OTH
39245+#define AuBrAttr_ICEX_OTH 0
39246+#endif
076b876e
AM
39247+#endif
39248+
39249+/* the longest combination */
c1595e42
JR
39250+/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
39251+#define AuBrPermStrSz sizeof(AUFS_BRPERM_RW \
39252+ "+" AUFS_BRATTR_COO_REG \
39253+ "+" AUFS_BRATTR_FHSM \
39254+ "+" AUFS_BRATTR_UNPIN \
7e9cd9fe
AM
39255+ "+" AUFS_BRATTR_ICEX_SEC \
39256+ "+" AUFS_BRATTR_ICEX_SYS \
39257+ "+" AUFS_BRATTR_ICEX_USR \
39258+ "+" AUFS_BRATTR_ICEX_OTH \
076b876e
AM
39259+ "+" AUFS_BRWATTR_NLWH)
39260+
39261+typedef struct {
39262+ char a[AuBrPermStrSz];
39263+} au_br_perm_str_t;
39264+
39265+static inline int au_br_writable(int brperm)
39266+{
39267+ return brperm & AuBrPerm_RW;
39268+}
39269+
39270+static inline int au_br_whable(int brperm)
39271+{
39272+ return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
39273+}
39274+
39275+static inline int au_br_wh_linkable(int brperm)
39276+{
39277+ return !(brperm & AuBrWAttr_NoLinkWH);
39278+}
39279+
39280+static inline int au_br_cmoo(int brperm)
39281+{
39282+ return brperm & AuBrAttr_CMOO_Mask;
39283+}
39284+
39285+static inline int au_br_fhsm(int brperm)
39286+{
39287+ return brperm & AuBrAttr_FHSM;
39288+}
7f207e10
AM
39289+
39290+/* ---------------------------------------------------------------------- */
39291+
39292+/* ioctl */
39293+enum {
39294+ /* readdir in userspace */
39295+ AuCtl_RDU,
39296+ AuCtl_RDU_INO,
39297+
076b876e
AM
39298+ AuCtl_WBR_FD, /* pathconf wrapper */
39299+ AuCtl_IBUSY, /* busy inode */
39300+ AuCtl_MVDOWN, /* move-down */
39301+ AuCtl_BR, /* info about branches */
39302+ AuCtl_FHSM_FD /* connection for fhsm */
7f207e10
AM
39303+};
39304+
39305+/* borrowed from linux/include/linux/kernel.h */
39306+#ifndef ALIGN
eca34b5c 39307+#ifdef _GNU_SOURCE
7f207e10 39308+#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
eca34b5c
AM
39309+#else
39310+#define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
39311+#endif
7f207e10
AM
39312+#define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
39313+#endif
39314+
39315+/* borrowed from linux/include/linux/compiler-gcc3.h */
39316+#ifndef __aligned
39317+#define __aligned(x) __attribute__((aligned(x)))
53392da6
AM
39318+#endif
39319+
39320+#ifdef __KERNEL__
39321+#ifndef __packed
7f207e10
AM
39322+#define __packed __attribute__((packed))
39323+#endif
53392da6 39324+#endif
7f207e10
AM
39325+
39326+struct au_rdu_cookie {
9dbd164d
AM
39327+ uint64_t h_pos;
39328+ int16_t bindex;
39329+ uint8_t flags;
39330+ uint8_t pad;
39331+ uint32_t generation;
7f207e10
AM
39332+} __aligned(8);
39333+
39334+struct au_rdu_ent {
9dbd164d
AM
39335+ uint64_t ino;
39336+ int16_t bindex;
39337+ uint8_t type;
39338+ uint8_t nlen;
39339+ uint8_t wh;
42b5c33a 39340+ char name[];
7f207e10
AM
39341+} __aligned(8);
39342+
39343+static inline int au_rdu_len(int nlen)
39344+{
39345+ /* include the terminating NULL */
39346+ return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
9dbd164d 39347+ sizeof(uint64_t));
7f207e10
AM
39348+}
39349+
39350+union au_rdu_ent_ul {
39351+ struct au_rdu_ent __user *e;
9dbd164d 39352+ uint64_t ul;
7f207e10
AM
39353+};
39354+
39355+enum {
39356+ AufsCtlRduV_SZ,
39357+ AufsCtlRduV_End
39358+};
39359+
39360+struct aufs_rdu {
39361+ /* input */
39362+ union {
9dbd164d
AM
39363+ uint64_t sz; /* AuCtl_RDU */
39364+ uint64_t nent; /* AuCtl_RDU_INO */
7f207e10
AM
39365+ };
39366+ union au_rdu_ent_ul ent;
9dbd164d 39367+ uint16_t verify[AufsCtlRduV_End];
7f207e10
AM
39368+
39369+ /* input/output */
9dbd164d 39370+ uint32_t blk;
7f207e10
AM
39371+
39372+ /* output */
39373+ union au_rdu_ent_ul tail;
39374+ /* number of entries which were added in a single call */
9dbd164d
AM
39375+ uint64_t rent;
39376+ uint8_t full;
39377+ uint8_t shwh;
7f207e10
AM
39378+
39379+ struct au_rdu_cookie cookie;
39380+} __aligned(8);
39381+
1e00d052
AM
39382+/* ---------------------------------------------------------------------- */
39383+
8b6a4947
AM
39384+/* dirren. the branch is identified by the filename who contains this */
39385+struct au_drinfo {
39386+ uint64_t ino;
39387+ union {
39388+ uint8_t oldnamelen;
39389+ uint64_t _padding;
39390+ };
42b5c33a 39391+ uint8_t oldname[];
8b6a4947
AM
39392+} __aligned(8);
39393+
39394+struct au_drinfo_fdata {
39395+ uint32_t magic;
39396+ struct au_drinfo drinfo;
39397+} __aligned(8);
39398+
39399+#define AUFS_DRINFO_MAGIC_V1 ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x01)
39400+/* future */
39401+#define AUFS_DRINFO_MAGIC_V2 ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x02)
39402+
39403+/* ---------------------------------------------------------------------- */
39404+
1e00d052 39405+struct aufs_wbr_fd {
9dbd164d
AM
39406+ uint32_t oflags;
39407+ int16_t brid;
1e00d052
AM
39408+} __aligned(8);
39409+
39410+/* ---------------------------------------------------------------------- */
39411+
027c5e7a 39412+struct aufs_ibusy {
9dbd164d
AM
39413+ uint64_t ino, h_ino;
39414+ int16_t bindex;
027c5e7a
AM
39415+} __aligned(8);
39416+
1e00d052
AM
39417+/* ---------------------------------------------------------------------- */
39418+
392086de
AM
39419+/* error code for move-down */
39420+/* the actual message strings are implemented in aufs-util.git */
39421+enum {
39422+ EAU_MVDOWN_OPAQUE = 1,
39423+ EAU_MVDOWN_WHITEOUT,
39424+ EAU_MVDOWN_UPPER,
39425+ EAU_MVDOWN_BOTTOM,
39426+ EAU_MVDOWN_NOUPPER,
39427+ EAU_MVDOWN_NOLOWERBR,
39428+ EAU_Last
39429+};
39430+
c2b27bf2 39431+/* flags for move-down */
392086de
AM
39432+#define AUFS_MVDOWN_DMSG 1
39433+#define AUFS_MVDOWN_OWLOWER (1 << 1) /* overwrite lower */
39434+#define AUFS_MVDOWN_KUPPER (1 << 2) /* keep upper */
39435+#define AUFS_MVDOWN_ROLOWER (1 << 3) /* do even if lower is RO */
39436+#define AUFS_MVDOWN_ROLOWER_R (1 << 4) /* did on lower RO */
39437+#define AUFS_MVDOWN_ROUPPER (1 << 5) /* do even if upper is RO */
39438+#define AUFS_MVDOWN_ROUPPER_R (1 << 6) /* did on upper RO */
39439+#define AUFS_MVDOWN_BRID_UPPER (1 << 7) /* upper brid */
39440+#define AUFS_MVDOWN_BRID_LOWER (1 << 8) /* lower brid */
076b876e
AM
39441+#define AUFS_MVDOWN_FHSM_LOWER (1 << 9) /* find fhsm attr for lower */
39442+#define AUFS_MVDOWN_STFS (1 << 10) /* req. stfs */
39443+#define AUFS_MVDOWN_STFS_FAILED (1 << 11) /* output: stfs is unusable */
39444+#define AUFS_MVDOWN_BOTTOM (1 << 12) /* output: no more lowers */
c2b27bf2 39445+
076b876e 39446+/* index for move-down */
392086de
AM
39447+enum {
39448+ AUFS_MVDOWN_UPPER,
39449+ AUFS_MVDOWN_LOWER,
39450+ AUFS_MVDOWN_NARRAY
39451+};
39452+
076b876e
AM
39453+/*
39454+ * additional info of move-down
39455+ * number of free blocks and inodes.
39456+ * subset of struct kstatfs, but smaller and always 64bit.
39457+ */
39458+struct aufs_stfs {
39459+ uint64_t f_blocks;
39460+ uint64_t f_bavail;
39461+ uint64_t f_files;
39462+ uint64_t f_ffree;
39463+};
39464+
39465+struct aufs_stbr {
39466+ int16_t brid; /* optional input */
39467+ int16_t bindex; /* output */
39468+ struct aufs_stfs stfs; /* output when AUFS_MVDOWN_STFS set */
39469+} __aligned(8);
39470+
c2b27bf2 39471+struct aufs_mvdown {
076b876e
AM
39472+ uint32_t flags; /* input/output */
39473+ struct aufs_stbr stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
39474+ int8_t au_errno; /* output */
39475+} __aligned(8);
39476+
39477+/* ---------------------------------------------------------------------- */
39478+
39479+union aufs_brinfo {
39480+ /* PATH_MAX may differ between kernel-space and user-space */
39481+ char _spacer[4096];
392086de 39482+ struct {
076b876e
AM
39483+ int16_t id;
39484+ int perm;
42b5c33a 39485+ char path[];
076b876e 39486+ };
c2b27bf2
AM
39487+} __aligned(8);
39488+
39489+/* ---------------------------------------------------------------------- */
39490+
7f207e10
AM
39491+#define AuCtlType 'A'
39492+#define AUFS_CTL_RDU _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
39493+#define AUFS_CTL_RDU_INO _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
1e00d052
AM
39494+#define AUFS_CTL_WBR_FD _IOW(AuCtlType, AuCtl_WBR_FD, \
39495+ struct aufs_wbr_fd)
027c5e7a 39496+#define AUFS_CTL_IBUSY _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
392086de
AM
39497+#define AUFS_CTL_MVDOWN _IOWR(AuCtlType, AuCtl_MVDOWN, \
39498+ struct aufs_mvdown)
076b876e
AM
39499+#define AUFS_CTL_BRINFO _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
39500+#define AUFS_CTL_FHSM_FD _IOW(AuCtlType, AuCtl_FHSM_FD, int)
7f207e10
AM
39501+
39502+#endif /* __AUFS_TYPE_H__ */
2121bcd9 39503SPDX-License-Identifier: GPL-2.0
631230db 39504aufs6.x-rcN loopback patch
5527c038
JR
39505
39506diff --git a/drivers/block/loop.c b/drivers/block/loop.c
631230db 39507index 1fefc6a8d049..86f1f66305d2 100644
5527c038
JR
39508--- a/drivers/block/loop.c
39509+++ b/drivers/block/loop.c
22319442
JP
39510@@ -54,7 +54,7 @@ struct loop_device {
39511 int lo_flags;
39512 char lo_file_name[LO_NAME_SIZE];
39513
39514- struct file * lo_backing_file;
39515+ struct file *lo_backing_file, *lo_backing_virt_file;
39516 struct block_device *lo_device;
39517
39518 gfp_t old_gfp_mask;
39519@@ -510,6 +510,15 @@ static inline void loop_update_dio(struct loop_device *lo)
42a736d3 39520 lo->use_dio);
5527c038
JR
39521 }
39522
5527c038
JR
39523+static struct file *loop_real_file(struct file *file)
39524+{
39525+ struct file *f = NULL;
39526+
39527+ if (file->f_path.dentry->d_sb->s_op->real_loop)
39528+ f = file->f_path.dentry->d_sb->s_op->real_loop(file);
39529+ return f;
8b6a4947
AM
39530+}
39531+
6d176551 39532 static void loop_reread_partitions(struct loop_device *lo)
8b6a4947 39533 {
6d176551 39534 int rc;
22319442 39535@@ -567,6 +576,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
5527c038 39536 {
6d176551
JR
39537 struct file *file = fget(arg);
39538 struct file *old_file;
39539+ struct file *f, *virt_file = NULL, *old_virt_file;
39540 int error;
39541 bool partscan;
39542 bool is_loop;
22319442 39543@@ -590,11 +600,19 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
6d176551 39544 if (!(lo->lo_flags & LO_FLAGS_READ_ONLY))
e1106418 39545 goto out_err;
6d176551 39546
5527c038
JR
39547+ f = loop_real_file(file);
39548+ if (f) {
39549+ virt_file = file;
39550+ file = f;
39551+ get_file(file);
39552+ }
6d176551 39553+
cd7a4cd9
AM
39554 error = loop_validate_file(file, bdev);
39555 if (error)
e1106418 39556 goto out_err;
cd7a4cd9 39557
5527c038
JR
39558 old_file = lo->lo_backing_file;
39559+ old_virt_file = lo->lo_backing_virt_file;
39560
39561 error = -EINVAL;
39562
22319442 39563@@ -607,6 +625,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
8b6a4947
AM
39564 blk_mq_freeze_queue(lo->lo_queue);
39565 mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
39566 lo->lo_backing_file = file;
39567+ lo->lo_backing_virt_file = virt_file;
39568 lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
39569 mapping_set_gfp_mask(file->f_mapping,
39570 lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
22319442 39571@@ -629,6 +648,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
e1106418
JR
39572 * dependency.
39573 */
5527c038
JR
39574 fput(old_file);
39575+ if (old_virt_file)
39576+ fput(old_virt_file);
e1106418 39577 if (partscan)
6d176551 39578 loop_reread_partitions(lo);
22319442
JP
39579
39580@@ -642,6 +663,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
6d176551
JR
39581 loop_global_unlock(lo, is_loop);
39582 out_putf:
39583 fput(file);
5527c038
JR
39584+ if (virt_file)
39585+ fput(virt_file);
22319442 39586 goto done;
5527c038 39587 }
e1106418 39588
171c7011 39589@@ -1013,6 +1036,7 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,
42a736d3 39590 const struct loop_config *config)
5527c038 39591 {
6d176551
JR
39592 struct file *file = fget(config->fd);
39593+ struct file *f, *virt_file = NULL;
39594 struct inode *inode;
5527c038 39595 struct address_space *mapping;
6d176551 39596 int error;
171c7011 39597@@ -1028,6 +1052,13 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,
41e6ec2c
JR
39598 /* This is safe, since we have a reference from open(). */
39599 __module_get(THIS_MODULE);
6d176551 39600
5527c038
JR
39601+ f = loop_real_file(file);
39602+ if (f) {
39603+ virt_file = file;
39604+ file = f;
39605+ get_file(file);
39606+ }
6d176551 39607+
fbc438ed
JR
39608 /*
39609 * If we don't hold exclusive handle for the device, upgrade to it
6d176551 39610 * here to avoid changing device under exclusive owner.
171c7011 39611@@ -1091,6 +1122,7 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,
f24b0ddd 39612 lo->use_dio = lo->lo_flags & LO_FLAGS_DIRECT_IO;
5527c038 39613 lo->lo_device = bdev;
5527c038
JR
39614 lo->lo_backing_file = file;
39615+ lo->lo_backing_virt_file = virt_file;
f24b0ddd
JR
39616 lo->old_gfp_mask = mapping_gfp_mask(mapping);
39617 mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39618
171c7011 39619@@ -1146,6 +1178,8 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,
42a736d3 39620 bd_abort_claiming(bdev, loop_configure);
e1106418 39621 out_putf:
5527c038
JR
39622 fput(file);
39623+ if (virt_file)
39624+ fput(virt_file);
5527c038
JR
39625 /* This is safe: open() is still holding a reference. */
39626 module_put(THIS_MODULE);
41e6ec2c 39627 return error;
171c7011 39628@@ -1154,6 +1188,7 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,
f4d37d76 39629 static void __loop_clr_fd(struct loop_device *lo, bool release)
5527c038 39630 {
f4d37d76 39631 struct file *filp;
5527c038
JR
39632+ struct file *virt_filp = lo->lo_backing_virt_file;
39633 gfp_t gfp = lo->old_gfp_mask;
5527c038 39634
22319442 39635 if (test_bit(QUEUE_FLAG_WC, &lo->lo_queue->queue_flags))
82da57cb 39636@@ -1170,6 +1205,7 @@ static void __loop_clr_fd(struct loop_device *lo, bool release)
5527c038 39637 spin_lock_irq(&lo->lo_lock);
f4d37d76 39638 filp = lo->lo_backing_file;
5527c038
JR
39639 lo->lo_backing_file = NULL;
39640+ lo->lo_backing_virt_file = NULL;
39641 spin_unlock_irq(&lo->lo_lock);
39642
cd233405 39643 lo->lo_device = NULL;
82da57cb 39644@@ -1232,6 +1268,8 @@ static void __loop_clr_fd(struct loop_device *lo, bool release)
f4d37d76 39645 * fput can take open_mutex which is usually taken before lo_mutex.
5527c038 39646 */
f4d37d76 39647 fput(filp);
5527c038
JR
39648+ if (virt_filp)
39649+ fput(virt_filp);
5527c038
JR
39650 }
39651
f4d37d76 39652 static int loop_clr_fd(struct loop_device *lo)
5527c038 39653diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
631230db 39654index fa8a517ffd0c..c18f7bcef81b 100644
5527c038
JR
39655--- a/fs/aufs/f_op.c
39656+++ b/fs/aufs/f_op.c
631230db 39657@@ -311,7 +311,7 @@ static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
5527c038
JR
39658 if (IS_ERR(h_file))
39659 goto out;
39660
39661- if (au_test_loopback_kthread()) {
39662+ if (0 && au_test_loopback_kthread()) {
39663 au_warn_loopback(h_file->f_path.dentry->d_sb);
39664 if (file->f_mapping != h_file->f_mapping) {
39665 file->f_mapping = h_file->f_mapping;
39666diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
0dcfbb52 39667index 58043e31e5f3..e2bfae6f9d59 100644
5527c038
JR
39668--- a/fs/aufs/loop.c
39669+++ b/fs/aufs/loop.c
2121bcd9 39670@@ -133,3 +133,19 @@ void au_loopback_fin(void)
79b8bda9 39671 symbol_put(loop_backing_file);
9f237c51 39672 au_kfree_try_rcu(au_warn_loopback_array);
5527c038
JR
39673 }
39674+
39675+/* ---------------------------------------------------------------------- */
39676+
39677+/* support the loopback block device insude aufs */
39678+
39679+struct file *aufs_real_loop(struct file *file)
39680+{
39681+ struct file *f;
39682+
39683+ BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
39684+ fi_read_lock(file);
39685+ f = au_hf_top(file);
39686+ fi_read_unlock(file);
39687+ AuDebugOn(!f);
39688+ return f;
39689+}
39690diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
0dcfbb52 39691index 03d4908a6c03..34d356e181d5 100644
5527c038
JR
39692--- a/fs/aufs/loop.h
39693+++ b/fs/aufs/loop.h
eca801bf 39694@@ -26,6 +26,8 @@ void au_warn_loopback(struct super_block *h_sb);
5527c038
JR
39695
39696 int au_loopback_init(void);
39697 void au_loopback_fin(void);
39698+
39699+struct file *aufs_real_loop(struct file *file);
39700 #else
83b672a5 39701 AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
eca801bf
AM
39702
39703@@ -36,6 +38,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
5527c038
JR
39704
39705 AuStubInt0(au_loopback_init, void)
39706 AuStubVoid(au_loopback_fin, void)
39707+
39708+AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
39709 #endif /* BLK_DEV_LOOP */
39710
39711 #endif /* __KERNEL__ */
39712diff --git a/fs/aufs/super.c b/fs/aufs/super.c
631230db 39713index 07d3412e950f..c4a00f620e57 100644
5527c038
JR
39714--- a/fs/aufs/super.c
39715+++ b/fs/aufs/super.c
f4d37d76
AM
39716@@ -758,7 +758,10 @@ const struct super_operations aufs_sop = {
39717 .show_options = aufs_show_options,
5527c038
JR
39718 .statfs = aufs_statfs,
39719 .put_super = aufs_put_super,
f4d37d76
AM
39720- .sync_fs = aufs_sync_fs
39721+ .sync_fs = aufs_sync_fs,
5527c038
JR
39722+#ifdef CONFIG_AUFS_BDEV_LOOP
39723+ .real_loop = aufs_real_loop
39724+#endif
39725 };
39726
39727 /* ---------------------------------------------------------------------- */
39728diff --git a/include/linux/fs.h b/include/linux/fs.h
631230db 39729index dd5871d0c429..748091221f2d 100644
5527c038
JR
39730--- a/include/linux/fs.h
39731+++ b/include/linux/fs.h
631230db 39732@@ -2056,6 +2056,11 @@ struct super_operations {
5527c038
JR
39733 long (*free_cached_objects)(struct super_block *,
39734 struct shrink_control *);
0a7871ab 39735 void (*shutdown)(struct super_block *sb);
171c7011 39736+
43982f53 39737+#if IS_ENABLED(CONFIG_BLK_DEV_LOOP) || IS_ENABLED(CONFIG_BLK_DEV_LOOP_MODULE)
5527c038
JR
39738+ /* and aufs */
39739+ struct file *(*real_loop)(struct file *);
39740+#endif
39741 };
39742
39743 /*
This page took 8.50681 seconds and 4 git commands to generate.