]> git.pld-linux.org Git - packages/kernel.git/blame - kernel-aufs5.patch
- parametrize aufs upstream branch
[packages/kernel.git] / kernel-aufs5.patch
CommitLineData
2121bcd9 1SPDX-License-Identifier: GPL-2.0
46016270 2aufs5.x-rcN kbuild patch
7f207e10 3
fbc438ed 4diff --git a/fs/Kconfig b/fs/Kconfig
46016270 5index 141a856c50e71..6741d695b1df8 100644
fbc438ed
JR
6--- a/fs/Kconfig
7+++ b/fs/Kconfig
46016270 8@@ -292,6 +292,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
46016270 17index 9c708e1fbe8fb..fde6be84a0830 100644
7f207e10
AM
18--- a/fs/Makefile
19+++ b/fs/Makefile
46016270 20@@ -136,3 +136,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
46016270 26aufs5.x-rcN base patch
7f207e10 27
c1595e42 28diff --git a/MAINTAINERS b/MAINTAINERS
46016270 29index bd7aff0c120f2..7c4bc08596e0b 100644
c1595e42
JR
30--- a/MAINTAINERS
31+++ b/MAINTAINERS
46016270 32@@ -3040,6 +3040,19 @@ F: include/uapi/linux/audit.h
c1595e42 33 F: kernel/audit*
46016270 34 F: lib/*audit.c
c1595e42
JR
35
36+AUFS (advanced multi layered unification filesystem) FILESYSTEM
37+M: "J. R. Okajima" <hooanon05g@gmail.com>
c1595e42 38+L: aufs-users@lists.sourceforge.net (members only)
eca801bf 39+L: linux-unionfs@vger.kernel.org
42a736d3 40+S: Supported
c1595e42 41+W: http://aufs.sourceforge.net
5527c038 42+T: git://github.com/sfjro/aufs4-linux.git
c1595e42
JR
43+F: Documentation/ABI/testing/debugfs-aufs
44+F: Documentation/ABI/testing/sysfs-aufs
42a736d3 45+F: Documentation/filesystems/aufs/
c1595e42
JR
46+F: fs/aufs/
47+F: include/uapi/linux/aufs_type.h
48+
49 AUXILIARY DISPLAY DRIVERS
13f235e5 50 M: Miguel Ojeda <ojeda@kernel.org>
cd7a4cd9 51 S: Maintained
392086de 52diff --git a/drivers/block/loop.c b/drivers/block/loop.c
46016270 53index d58d68f3c7cd0..794a8a1341989 100644
392086de
AM
54--- a/drivers/block/loop.c
55+++ b/drivers/block/loop.c
46016270 56@@ -762,6 +762,24 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
cd7a4cd9 57 return error;
392086de
AM
58 }
59
60+/*
61+ * for AUFS
62+ * no get/put for file.
63+ */
64+struct file *loop_backing_file(struct super_block *sb)
65+{
66+ struct file *ret;
67+ struct loop_device *l;
68+
69+ ret = NULL;
70+ if (MAJOR(sb->s_dev) == LOOP_MAJOR) {
71+ l = sb->s_bdev->bd_disk->private_data;
72+ ret = l->lo_backing_file;
73+ }
74+ return ret;
75+}
febd17d6 76+EXPORT_SYMBOL_GPL(loop_backing_file);
392086de
AM
77+
78 /* loop sysfs attributes */
79
80 static ssize_t loop_attr_show(struct device *dev, char *page,
c1595e42 81diff --git a/fs/dcache.c b/fs/dcache.c
46016270 82index cf871a81f4fdc..bc5095b734f58 100644
c1595e42
JR
83--- a/fs/dcache.c
84+++ b/fs/dcache.c
46016270 85@@ -1320,7 +1320,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
0b2a12c6 95index dfc72f15be7fc..d8a12eb63961d 100644
febd17d6
JR
96--- a/fs/fcntl.c
97+++ b/fs/fcntl.c
0b2a12c6 98@@ -33,7 +33,7 @@
febd17d6
JR
99
100 #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME)
101
102-static int setfl(int fd, struct file * filp, unsigned long arg)
43982f53 103+int setfl(int fd, struct file *filp, unsigned long arg)
febd17d6
JR
104 {
105 struct inode * inode = file_inode(filp);
106 int error = 0;
0b2a12c6 107@@ -64,6 +64,8 @@ static int setfl(int fd, struct file * filp, unsigned long arg)
febd17d6
JR
108
109 if (filp->f_op->check_flags)
110 error = filp->f_op->check_flags(arg);
111+ if (!error && filp->f_op->setfl)
112+ error = filp->f_op->setfl(filp, arg);
113 if (error)
114 return error;
115
5afbbe0d 116diff --git a/fs/inode.c b/fs/inode.c
46016270 117index c93500d84264d..f24d21236ad01 100644
5afbbe0d
AM
118--- a/fs/inode.c
119+++ b/fs/inode.c
46016270 120@@ -1778,7 +1778,7 @@ EXPORT_SYMBOL(generic_update_time);
5afbbe0d
AM
121 * This does the actual work of updating an inodes time or version. Must have
122 * had called mnt_want_write() before calling this.
123 */
cd7a4cd9
AM
124-static int update_time(struct inode *inode, struct timespec64 *time, int flags)
125+int update_time(struct inode *inode, struct timespec64 *time, int flags)
5afbbe0d 126 {
394e211a
AM
127 if (inode->i_op->update_time)
128 return inode->i_op->update_time(inode, time, flags);
8b6a4947 129diff --git a/fs/namespace.c b/fs/namespace.c
46016270 130index f63337828e1c4..099e89c81af7c 100644
8b6a4947
AM
131--- a/fs/namespace.c
132+++ b/fs/namespace.c
0b2a12c6 133@@ -807,6 +807,12 @@ static inline int check_mnt(struct mount *mnt)
8b6a4947
AM
134 return mnt->mnt_ns == current->nsproxy->mnt_ns;
135 }
136
137+/* for aufs, CONFIG_AUFS_BR_FUSE */
138+int is_current_mnt_ns(struct vfsmount *mnt)
139+{
140+ return check_mnt(real_mount(mnt));
141+}
142+
143 /*
144 * vfsmount lock must be held for write
145 */
7f207e10 146diff --git a/fs/splice.c b/fs/splice.c
0b2a12c6 147index 5dbce4dcc1a7d..3e6ba363b7775 100644
7f207e10
AM
148--- a/fs/splice.c
149+++ b/fs/splice.c
0b2a12c6 150@@ -759,8 +759,8 @@ static int warn_unsupported(struct file *file, const char *op)
7f207e10
AM
151 /*
152 * Attempt to initiate a splice from pipe to file.
153 */
154-static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
155- loff_t *ppos, size_t len, unsigned int flags)
156+long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
157+ loff_t *ppos, size_t len, unsigned int flags)
158 {
42a736d3
AM
159 if (unlikely(!out->f_op->splice_write))
160 return warn_unsupported(out, "write");
0b2a12c6 161@@ -770,9 +770,9 @@ static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
7f207e10
AM
162 /*
163 * Attempt to initiate a splice from a file to a pipe.
164 */
165-static long do_splice_to(struct file *in, loff_t *ppos,
166- struct pipe_inode_info *pipe, size_t len,
167- unsigned int flags)
168+long do_splice_to(struct file *in, loff_t *ppos,
169+ struct pipe_inode_info *pipe, size_t len,
170+ unsigned int flags)
171 {
13f235e5 172 unsigned int p_space;
42a736d3 173 int ret;
a2654f78 174diff --git a/fs/sync.c b/fs/sync.c
42a736d3 175index 1373a610dc784..b7b5a0a0df6ff 100644
a2654f78
AM
176--- a/fs/sync.c
177+++ b/fs/sync.c
2121bcd9 178@@ -28,7 +28,7 @@
a2654f78
AM
179 * wait == 1 case since in that case write_inode() functions do
180 * sync_dirty_buffer() and thus effectively write one block at a time.
181 */
182-static int __sync_filesystem(struct super_block *sb, int wait)
183+int __sync_filesystem(struct super_block *sb, int wait)
184 {
185 if (wait)
186 sync_inodes_sb(sb);
5527c038 187diff --git a/include/linux/fs.h b/include/linux/fs.h
46016270 188index c3c88fdb9b2a5..93eb43e002d97 100644
5527c038
JR
189--- a/include/linux/fs.h
190+++ b/include/linux/fs.h
46016270 191@@ -1335,6 +1335,7 @@ extern void fasync_free(struct fasync_struct *);
febd17d6
JR
192 /* can be called from interrupts */
193 extern void kill_fasync(struct fasync_struct **, int, int);
194
43982f53 195+extern int setfl(int fd, struct file *filp, unsigned long arg);
febd17d6 196 extern void __f_setown(struct file *filp, struct pid *, enum pid_type, int force);
3c1bdaff 197 extern int f_setown(struct file *filp, unsigned long arg, int force);
febd17d6 198 extern void f_delown(struct file *filp);
46016270 199@@ -2043,6 +2044,7 @@ struct file_operations {
febd17d6
JR
200 ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
201 unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
202 int (*check_flags)(int);
203+ int (*setfl)(struct file *, unsigned long);
204 int (*flock) (struct file *, int, struct file_lock *);
205 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
206 ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
46016270 207@@ -2565,6 +2567,7 @@ extern int current_umask(void);
5afbbe0d
AM
208 extern void ihold(struct inode * inode);
209 extern void iput(struct inode *);
cd7a4cd9
AM
210 extern int generic_update_time(struct inode *, struct timespec64 *, int);
211+extern int update_time(struct inode *, struct timespec64 *, int);
5afbbe0d
AM
212
213 /* /sys/fs */
214 extern struct kobject *fs_kobj;
46016270 215@@ -2805,6 +2808,7 @@ static inline bool sb_is_blkdev_sb(struct super_block *sb)
a2654f78 216 }
6803432e
JR
217
218 void emergency_thaw_all(void);
a2654f78
AM
219+extern int __sync_filesystem(struct super_block *, int);
220 extern int sync_filesystem(struct super_block *);
221 extern const struct file_operations def_blk_fops;
222 extern const struct file_operations def_chr_fops;
8b6a4947 223diff --git a/include/linux/lockdep.h b/include/linux/lockdep.h
46016270 224index 5cf3878137549..59b65e1a9e85c 100644
8b6a4947
AM
225--- a/include/linux/lockdep.h
226+++ b/include/linux/lockdep.h
0b2a12c6 227@@ -248,6 +248,8 @@ static inline int lockdep_match_key(struct lockdep_map *lock,
8b6a4947
AM
228 return lock->key == key;
229 }
230
231+struct lock_class *lockdep_hlock_class(struct held_lock *hlock);
232+
233 /*
234 * Acquire a lock.
235 *
8b6a4947 236diff --git a/include/linux/mnt_namespace.h b/include/linux/mnt_namespace.h
42a736d3 237index 8f882f5881e87..6b9808f098435 100644
8b6a4947
AM
238--- a/include/linux/mnt_namespace.h
239+++ b/include/linux/mnt_namespace.h
42a736d3 240@@ -7,12 +7,15 @@ struct mnt_namespace;
8b6a4947
AM
241 struct fs_struct;
242 struct user_namespace;
f24b0ddd 243 struct ns_common;
8b6a4947
AM
244+struct vfsmount;
245
246 extern struct mnt_namespace *copy_mnt_ns(unsigned long, struct mnt_namespace *,
247 struct user_namespace *, struct fs_struct *);
248 extern void put_mnt_ns(struct mnt_namespace *ns);
f24b0ddd 249 extern struct ns_common *from_mnt_ns(struct mnt_namespace *);
8b6a4947
AM
250
251+extern int is_current_mnt_ns(struct vfsmount *mnt);
252+
253 extern const struct file_operations proc_mounts_operations;
254 extern const struct file_operations proc_mountinfo_operations;
255 extern const struct file_operations proc_mountstats_operations;
1e00d052 256diff --git a/include/linux/splice.h b/include/linux/splice.h
42a736d3 257index a55179fd60fc3..8e21c53cf8831 100644
1e00d052
AM
258--- a/include/linux/splice.h
259+++ b/include/linux/splice.h
42a736d3 260@@ -93,4 +93,10 @@ extern void splice_shrink_spd(struct splice_pipe_desc *);
4b3da204
AM
261
262 extern const struct pipe_buf_operations page_cache_pipe_buf_ops;
106341ce 263 extern const struct pipe_buf_operations default_pipe_buf_ops;
1e00d052
AM
264+
265+extern long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
266+ loff_t *ppos, size_t len, unsigned int flags);
267+extern long do_splice_to(struct file *in, loff_t *ppos,
268+ struct pipe_inode_info *pipe, size_t len,
269+ unsigned int flags);
270 #endif
8b6a4947 271diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
46016270 272index 48d736aa03b24..dce278159546c 100644
8b6a4947
AM
273--- a/kernel/locking/lockdep.c
274+++ b/kernel/locking/lockdep.c
46016270 275@@ -189,7 +189,7 @@ static
9f237c51 276 struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
eca34b5c 277 static DECLARE_BITMAP(lock_classes_in_use, MAX_LOCKDEP_KEYS);
8b6a4947
AM
278
279-static inline struct lock_class *hlock_class(struct held_lock *hlock)
280+inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
281 {
eca34b5c
AM
282 unsigned int class_idx = hlock->class_idx;
283
46016270 284@@ -210,6 +210,7 @@ static inline struct lock_class *hlock_class(struct held_lock *hlock)
eca34b5c
AM
285 */
286 return lock_classes + class_idx;
8b6a4947
AM
287 }
288+#define hlock_class(hlock) lockdep_hlock_class(hlock)
289
290 #ifdef CONFIG_LOCK_STAT
291 static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
2121bcd9 292SPDX-License-Identifier: GPL-2.0
46016270 293aufs5.x-rcN mmap patch
fb47a38f 294
c1595e42 295diff --git a/fs/proc/base.c b/fs/proc/base.c
0b2a12c6 296index 3851bfcdba56e..d632ddd5f5ee8 100644
c1595e42
JR
297--- a/fs/proc/base.c
298+++ b/fs/proc/base.c
0b2a12c6 299@@ -2183,7 +2183,7 @@ static int map_files_get_link(struct dentry *dentry, struct path *path)
eca34b5c 300 rc = -ENOENT;
c1595e42
JR
301 vma = find_exact_vma(mm, vm_start, vm_end);
302 if (vma && vma->vm_file) {
303- *path = vma->vm_file->f_path;
304+ *path = vma_pr_or_file(vma)->f_path;
305 path_get(path);
306 rc = 0;
307 }
fb47a38f 308diff --git a/fs/proc/nommu.c b/fs/proc/nommu.c
42a736d3 309index 13452b32e2bd5..38acccfef9d49 100644
fb47a38f
JR
310--- a/fs/proc/nommu.c
311+++ b/fs/proc/nommu.c
42a736d3 312@@ -40,7 +40,10 @@ static int nommu_region_show(struct seq_file *m, struct vm_region *region)
fb47a38f
JR
313 file = region->vm_file;
314
315 if (file) {
316- struct inode *inode = file_inode(region->vm_file);
317+ struct inode *inode;
076b876e 318+
fb47a38f
JR
319+ file = vmr_pr_or_file(region);
320+ inode = file_inode(file);
321 dev = inode->i_sb->s_dev;
322 ino = inode->i_ino;
323 }
324diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
46016270 325index fc9784544b241..84ba06f5158e9 100644
fb47a38f
JR
326--- a/fs/proc/task_mmu.c
327+++ b/fs/proc/task_mmu.c
42b5c33a 328@@ -280,7 +280,10 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
fb47a38f
JR
329 const char *name = NULL;
330
331 if (file) {
332- struct inode *inode = file_inode(vma->vm_file);
333+ struct inode *inode;
076b876e 334+
fb47a38f
JR
335+ file = vma_pr_or_file(vma);
336+ inode = file_inode(file);
337 dev = inode->i_sb->s_dev;
338 ino = inode->i_ino;
339 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
46016270 340@@ -1864,7 +1867,7 @@ static int show_numa_map(struct seq_file *m, void *v)
076b876e
AM
341 struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
342 struct vm_area_struct *vma = v;
343 struct numa_maps *md = &numa_priv->md;
344- struct file *file = vma->vm_file;
345+ struct file *file = vma_pr_or_file(vma);
076b876e 346 struct mm_struct *mm = vma->vm_mm;
43982f53
AM
347 struct mempolicy *pol;
348 char buffer[64];
fb47a38f 349diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c
42a736d3 350index a6d21fc0033c6..02c2de31196e0 100644
fb47a38f
JR
351--- a/fs/proc/task_nommu.c
352+++ b/fs/proc/task_nommu.c
acd2b654 353@@ -155,7 +155,10 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma)
fb47a38f
JR
354 file = vma->vm_file;
355
356 if (file) {
357- struct inode *inode = file_inode(vma->vm_file);
358+ struct inode *inode;
076b876e 359+
b912730e 360+ file = vma_pr_or_file(vma);
fb47a38f
JR
361+ inode = file_inode(file);
362 dev = inode->i_sb->s_dev;
363 ino = inode->i_ino;
364 pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
365diff --git a/include/linux/mm.h b/include/linux/mm.h
46016270 366index 322ec61d0da79..ae6f0584e4c98 100644
fb47a38f
JR
367--- a/include/linux/mm.h
368+++ b/include/linux/mm.h
46016270 369@@ -1798,6 +1798,28 @@ static inline void unmap_shared_mapping_range(struct address_space *mapping,
a3232e84 370 unmap_mapping_range(mapping, holebegin, holelen, 0);
fb47a38f 371 }
fb47a38f 372
076b876e
AM
373+extern void vma_do_file_update_time(struct vm_area_struct *, const char[], int);
374+extern struct file *vma_do_pr_or_file(struct vm_area_struct *, const char[],
f2c43d5f 375+ int);
076b876e
AM
376+extern void vma_do_get_file(struct vm_area_struct *, const char[], int);
377+extern void vma_do_fput(struct vm_area_struct *, const char[], int);
fb47a38f 378+
f2c43d5f
AM
379+#define vma_file_update_time(vma) vma_do_file_update_time(vma, __func__, \
380+ __LINE__)
381+#define vma_pr_or_file(vma) vma_do_pr_or_file(vma, __func__, \
382+ __LINE__)
383+#define vma_get_file(vma) vma_do_get_file(vma, __func__, __LINE__)
384+#define vma_fput(vma) vma_do_fput(vma, __func__, __LINE__)
b912730e
AM
385+
386+#ifndef CONFIG_MMU
076b876e
AM
387+extern struct file *vmr_do_pr_or_file(struct vm_region *, const char[], int);
388+extern void vmr_do_fput(struct vm_region *, const char[], int);
389+
f2c43d5f
AM
390+#define vmr_pr_or_file(region) vmr_do_pr_or_file(region, __func__, \
391+ __LINE__)
392+#define vmr_fput(region) vmr_do_fput(region, __func__, __LINE__)
b912730e 393+#endif /* !CONFIG_MMU */
fb47a38f 394+
a3232e84
JR
395 extern int access_process_vm(struct task_struct *tsk, unsigned long addr,
396 void *buf, int len, unsigned int gup_flags);
fb47a38f 397 extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
fb47a38f 398diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
0b2a12c6 399index 6613b26a88946..e94df45b5483a 100644
fb47a38f
JR
400--- a/include/linux/mm_types.h
401+++ b/include/linux/mm_types.h
0b2a12c6 402@@ -279,6 +279,7 @@ struct vm_region {
fb47a38f
JR
403 unsigned long vm_top; /* region allocated to here */
404 unsigned long vm_pgoff; /* the offset in vm_file corresponding to vm_start */
405 struct file *vm_file; /* the backing file or NULL */
406+ struct file *vm_prfile; /* the virtual backing file or NULL */
407
408 int vm_usage; /* region usage count (access under nommu_region_sem) */
409 bool vm_icache_flushed : 1; /* true if the icache has been flushed for
0b2a12c6 410@@ -358,6 +359,7 @@ struct vm_area_struct {
fb47a38f 411 unsigned long vm_pgoff; /* Offset (within vm_file) in PAGE_SIZE
5afbbe0d 412 units */
fb47a38f
JR
413 struct file * vm_file; /* File we map to (can be NULL). */
414+ struct file *vm_prfile; /* shadow of vm_file */
415 void * vm_private_data; /* was vm_pte (shared mem) */
416
eca34b5c 417 #ifdef CONFIG_SWAP
fb47a38f 418diff --git a/kernel/fork.c b/kernel/fork.c
46016270 419index dc06afd725cbd..66f1486e63e35 100644
fb47a38f
JR
420--- a/kernel/fork.c
421+++ b/kernel/fork.c
46016270 422@@ -559,7 +559,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
fb47a38f
JR
423 struct inode *inode = file_inode(file);
424 struct address_space *mapping = file->f_mapping;
425
426- get_file(file);
427+ vma_get_file(tmp);
428 if (tmp->vm_flags & VM_DENYWRITE)
42a736d3 429 put_write_access(inode);
2000de60 430 i_mmap_lock_write(mapping);
076b876e 431diff --git a/mm/Makefile b/mm/Makefile
46016270 432index bf71e295e9f69..bd223b81c564c 100644
076b876e
AM
433--- a/mm/Makefile
434+++ b/mm/Makefile
42a736d3
AM
435@@ -52,7 +52,7 @@ obj-y := filemap.o mempool.o oom_kill.o fadvise.o \
436 mm_init.o percpu.o slab_common.o \
b00004a5 437 compaction.o vmacache.o \
076b876e 438 interval_tree.o list_lru.o workingset.o \
42a736d3
AM
439- debug.o gup.o mmap_lock.o $(mmu-y)
440+ prfile.o debug.o gup.o mmap_lock.o $(mmu-y)
076b876e 441
fbc438ed
JR
442 # Give 'page_alloc' its own module-parameter namespace
443 page-alloc-y := page_alloc.o
fb47a38f 444diff --git a/mm/filemap.c b/mm/filemap.c
46016270 445index 66f7e9fdfbc4f..f9a8ff48e697a 100644
fb47a38f
JR
446--- a/mm/filemap.c
447+++ b/mm/filemap.c
46016270 448@@ -3240,7 +3240,7 @@ vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
cd7a4cd9 449 vm_fault_t ret = VM_FAULT_LOCKED;
fb47a38f 450
42a736d3 451 sb_start_pagefault(mapping->host->i_sb);
521ced18
JR
452- file_update_time(vmf->vma->vm_file);
453+ vma_file_update_time(vmf->vma);
fb47a38f 454 lock_page(page);
42a736d3 455 if (page->mapping != mapping) {
fb47a38f 456 unlock_page(page);
fb47a38f 457diff --git a/mm/mmap.c b/mm/mmap.c
46016270 458index 0584e540246e1..8b2f082002a9c 100644
fb47a38f
JR
459--- a/mm/mmap.c
460+++ b/mm/mmap.c
46016270 461@@ -185,7 +185,7 @@ static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
fb47a38f
JR
462 if (vma->vm_ops && vma->vm_ops->close)
463 vma->vm_ops->close(vma);
464 if (vma->vm_file)
465- fput(vma->vm_file);
466+ vma_fput(vma);
467 mpol_put(vma_policy(vma));
cd7a4cd9 468 vm_area_free(vma);
fb47a38f 469 return next;
46016270 470@@ -955,7 +955,7 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
fb47a38f
JR
471 if (remove_next) {
472 if (file) {
473 uprobe_munmap(next, next->vm_start, next->vm_end);
474- fput(file);
475+ vma_fput(vma);
476 }
477 if (next->anon_vma)
478 anon_vma_merge(vma, next);
46016270 479@@ -1901,7 +1901,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
35939ee7
JR
480 return addr;
481
fb47a38f 482 unmap_and_free_vma:
42a736d3 483- fput(vma->vm_file);
fb47a38f
JR
484+ vma_fput(vma);
485 vma->vm_file = NULL;
fb47a38f
JR
486
487 /* Undo any partial mapping done by a device driver. */
46016270 488@@ -2761,7 +2761,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
fb47a38f
JR
489 goto out_free_mpol;
490
491 if (new->vm_file)
492- get_file(new->vm_file);
493+ vma_get_file(new);
494
495 if (new->vm_ops && new->vm_ops->open)
496 new->vm_ops->open(new);
46016270 497@@ -2780,7 +2780,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
fb47a38f
JR
498 if (new->vm_ops && new->vm_ops->close)
499 new->vm_ops->close(new);
500 if (new->vm_file)
501- fput(new->vm_file);
502+ vma_fput(new);
503 unlink_anon_vmas(new);
504 out_free_mpol:
505 mpol_put(vma_policy(new));
46016270 506@@ -2973,7 +2973,7 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
7e9cd9fe
AM
507 struct vm_area_struct *vma;
508 unsigned long populate = 0;
509 unsigned long ret = -EINVAL;
510- struct file *file;
5afbbe0d 511+ struct file *file, *prfile;
7e9cd9fe 512
cd7a4cd9 513 pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/vm/remap_file_pages.rst.\n",
5afbbe0d 514 current->comm, current->pid);
46016270
AM
515@@ -3032,10 +3032,27 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
516 if (vma->vm_flags & VM_LOCKED)
517 flags |= MAP_LOCKED;
7e9cd9fe
AM
518
519- file = get_file(vma->vm_file);
520+ vma_get_file(vma);
5afbbe0d
AM
521+ file = vma->vm_file;
522+ prfile = vma->vm_prfile;
6803432e 523 ret = do_mmap(vma->vm_file, start, size,
521ced18 524 prot, flags, pgoff, &populate, NULL);
5afbbe0d
AM
525+ if (!IS_ERR_VALUE(ret) && file && prfile) {
526+ struct vm_area_struct *new_vma;
527+
528+ new_vma = find_vma(mm, ret);
529+ if (!new_vma->vm_prfile)
530+ new_vma->vm_prfile = prfile;
531+ if (new_vma != vma)
532+ get_file(prfile);
533+ }
534+ /*
535+ * two fput()s instead of vma_fput(vma),
536+ * coz vma may not be available anymore.
537+ */
538 fput(file);
539+ if (prfile)
540+ fput(prfile);
7e9cd9fe 541 out:
42a736d3 542 mmap_write_unlock(mm);
7e9cd9fe 543 if (populate)
46016270 544@@ -3322,7 +3339,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
79b8bda9
AM
545 if (anon_vma_clone(new_vma, vma))
546 goto out_free_mempol;
547 if (new_vma->vm_file)
548- get_file(new_vma->vm_file);
549+ vma_get_file(new_vma);
550 if (new_vma->vm_ops && new_vma->vm_ops->open)
551 new_vma->vm_ops->open(new_vma);
552 vma_link(mm, new_vma, prev, rb_link, rb_parent);
fb47a38f 553diff --git a/mm/nommu.c b/mm/nommu.c
46016270 554index 85a3a68dffb68..a2bee44172ccc 100644
fb47a38f
JR
555--- a/mm/nommu.c
556+++ b/mm/nommu.c
46016270 557@@ -523,7 +523,7 @@ static void __put_nommu_region(struct vm_region *region)
fb47a38f
JR
558 up_write(&nommu_region_sem);
559
560 if (region->vm_file)
561- fput(region->vm_file);
562+ vmr_fput(region);
563
564 /* IO memory and memory shared directly out of the pagecache
565 * from ramfs/tmpfs mustn't be released here */
46016270 566@@ -655,7 +655,7 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
fb47a38f
JR
567 if (vma->vm_ops && vma->vm_ops->close)
568 vma->vm_ops->close(vma);
569 if (vma->vm_file)
570- fput(vma->vm_file);
571+ vma_fput(vma);
572 put_nommu_region(vma->vm_region);
cd7a4cd9 573 vm_area_free(vma);
fb47a38f 574 }
46016270 575@@ -1178,7 +1178,7 @@ unsigned long do_mmap(struct file *file,
fb47a38f
JR
576 goto error_just_free;
577 }
578 }
579- fput(region->vm_file);
580+ vmr_fput(region);
581 kmem_cache_free(vm_region_jar, region);
582 region = pregion;
583 result = start;
46016270 584@@ -1255,10 +1255,10 @@ unsigned long do_mmap(struct file *file,
fb47a38f
JR
585 up_write(&nommu_region_sem);
586 error:
587 if (region->vm_file)
588- fput(region->vm_file);
589+ vmr_fput(region);
590 kmem_cache_free(vm_region_jar, region);
591 if (vma->vm_file)
eca801bf
AM
592- fput(vma->vm_file);
593+ vma_fput(vma);
594 vm_area_free(vma);
595 return ret;
596
076b876e
AM
597diff --git a/mm/prfile.c b/mm/prfile.c
598new file mode 100644
42b5c33a 599index 0000000000000..00d51187c3250
076b876e
AM
600--- /dev/null
601+++ b/mm/prfile.c
2121bcd9 602@@ -0,0 +1,86 @@
cd7a4cd9 603+// SPDX-License-Identifier: GPL-2.0
076b876e 604+/*
1c60b727
AM
605+ * Mainly for aufs which mmap(2) different file and wants to print different
606+ * path in /proc/PID/maps.
076b876e
AM
607+ * Call these functions via macros defined in linux/mm.h.
608+ *
609+ * See Documentation/filesystems/aufs/design/06mmap.txt
610+ *
d58c55f2 611+ * Copyright (c) 2014-2020 Junjro R. Okajima
076b876e
AM
612+ * Copyright (c) 2014 Ian Campbell
613+ */
614+
615+#include <linux/mm.h>
616+#include <linux/file.h>
617+#include <linux/fs.h>
618+
619+/* #define PRFILE_TRACE */
620+static inline void prfile_trace(struct file *f, struct file *pr,
621+ const char func[], int line, const char func2[])
622+{
623+#ifdef PRFILE_TRACE
624+ if (pr)
1c60b727 625+ pr_info("%s:%d: %s, %pD2\n", func, line, func2, f);
076b876e
AM
626+#endif
627+}
628+
076b876e
AM
629+void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
630+ int line)
631+{
632+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
633+
634+ prfile_trace(f, pr, func, line, __func__);
635+ file_update_time(f);
636+ if (f && pr)
637+ file_update_time(pr);
638+}
639+
640+struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
641+ int line)
642+{
643+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
644+
645+ prfile_trace(f, pr, func, line, __func__);
646+ return (f && pr) ? pr : f;
647+}
648+
649+void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
650+{
651+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
652+
653+ prfile_trace(f, pr, func, line, __func__);
654+ get_file(f);
655+ if (f && pr)
656+ get_file(pr);
657+}
658+
659+void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
660+{
661+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
662+
663+ prfile_trace(f, pr, func, line, __func__);
664+ fput(f);
665+ if (f && pr)
666+ fput(pr);
667+}
b912730e
AM
668+
669+#ifndef CONFIG_MMU
076b876e
AM
670+struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
671+ int line)
672+{
673+ struct file *f = region->vm_file, *pr = region->vm_prfile;
674+
675+ prfile_trace(f, pr, func, line, __func__);
676+ return (f && pr) ? pr : f;
677+}
678+
679+void vmr_do_fput(struct vm_region *region, const char func[], int line)
680+{
681+ struct file *f = region->vm_file, *pr = region->vm_prfile;
682+
683+ prfile_trace(f, pr, func, line, __func__);
684+ fput(f);
685+ if (f && pr)
686+ fput(pr);
687+}
b912730e 688+#endif /* !CONFIG_MMU */
2121bcd9 689SPDX-License-Identifier: GPL-2.0
46016270 690aufs5.x-rcN standalone patch
7f207e10 691
c1595e42 692diff --git a/fs/dcache.c b/fs/dcache.c
46016270 693index bc5095b734f58..9508bd57a3bc0 100644
c1595e42
JR
694--- a/fs/dcache.c
695+++ b/fs/dcache.c
46016270 696@@ -1425,6 +1425,7 @@ void d_walk(struct dentry *parent, void *data,
c1595e42
JR
697 seq = 1;
698 goto again;
699 }
febd17d6 700+EXPORT_SYMBOL_GPL(d_walk);
c1595e42 701
a2654f78
AM
702 struct check_mount {
703 struct vfsmount *mnt;
46016270 704@@ -2970,6 +2971,7 @@ void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
f2c43d5f
AM
705
706 write_sequnlock(&rename_lock);
707 }
708+EXPORT_SYMBOL_GPL(d_exchange);
709
710 /**
711 * d_ancestor - search for an ancestor
79b8bda9 712diff --git a/fs/exec.c b/fs/exec.c
0b2a12c6 713index 18594f11c31fe..ac38c0424d2a3 100644
79b8bda9
AM
714--- a/fs/exec.c
715+++ b/fs/exec.c
42a736d3 716@@ -114,6 +114,7 @@ bool path_noexec(const struct path *path)
79b8bda9
AM
717 return (path->mnt->mnt_flags & MNT_NOEXEC) ||
718 (path->mnt->mnt_sb->s_iflags & SB_I_NOEXEC);
719 }
febd17d6 720+EXPORT_SYMBOL_GPL(path_noexec);
79b8bda9
AM
721
722 #ifdef CONFIG_USELIB
723 /*
febd17d6 724diff --git a/fs/fcntl.c b/fs/fcntl.c
0b2a12c6 725index d8a12eb63961d..0532996186311 100644
febd17d6
JR
726--- a/fs/fcntl.c
727+++ b/fs/fcntl.c
0b2a12c6 728@@ -86,6 +86,7 @@ int setfl(int fd, struct file *filp, unsigned long arg)
febd17d6
JR
729 out:
730 return error;
731 }
732+EXPORT_SYMBOL_GPL(setfl);
733
734 static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
735 int force)
b912730e 736diff --git a/fs/file_table.c b/fs/file_table.c
42a736d3 737index 45437f8e1003e..786af52904fcf 100644
b912730e
AM
738--- a/fs/file_table.c
739+++ b/fs/file_table.c
42a736d3 740@@ -161,6 +161,7 @@ struct file *alloc_empty_file(int flags, const struct cred *cred)
b912730e
AM
741 }
742 return ERR_PTR(-ENFILE);
743 }
acd2b654 744+EXPORT_SYMBOL_GPL(alloc_empty_file);
b912730e 745
acd2b654
AM
746 /*
747 * Variant of alloc_empty_file() that doesn't check and modify nr_files.
43982f53 748@@ -375,6 +376,7 @@ void __fput_sync(struct file *file)
8cdd5066
JR
749 }
750
751 EXPORT_SYMBOL(fput);
febd17d6 752+EXPORT_SYMBOL_GPL(__fput_sync);
8cdd5066 753
79b8bda9 754 void __init files_init(void)
8b6a4947 755 {
5afbbe0d 756diff --git a/fs/inode.c b/fs/inode.c
46016270 757index f24d21236ad01..78de5b5dc0840 100644
5afbbe0d
AM
758--- a/fs/inode.c
759+++ b/fs/inode.c
46016270 760@@ -1784,6 +1784,7 @@ int update_time(struct inode *inode, struct timespec64 *time, int flags)
394e211a
AM
761 return inode->i_op->update_time(inode, time, flags);
762 return generic_update_time(inode, time, flags);
5afbbe0d
AM
763 }
764+EXPORT_SYMBOL_GPL(update_time);
765
766 /**
13f235e5 767 * atime_needs_update - update the access time
7f207e10 768diff --git a/fs/namespace.c b/fs/namespace.c
46016270 769index 099e89c81af7c..3e46b53795b1e 100644
7f207e10
AM
770--- a/fs/namespace.c
771+++ b/fs/namespace.c
0b2a12c6 772@@ -438,6 +438,7 @@ void __mnt_drop_write(struct vfsmount *mnt)
c06a8ce3
AM
773 mnt_dec_writers(real_mount(mnt));
774 preempt_enable();
775 }
776+EXPORT_SYMBOL_GPL(__mnt_drop_write);
777
778 /**
779 * mnt_drop_write - give up write access to a mount
0b2a12c6 780@@ -812,6 +813,7 @@ int is_current_mnt_ns(struct vfsmount *mnt)
8b6a4947
AM
781 {
782 return check_mnt(real_mount(mnt));
783 }
784+EXPORT_SYMBOL_GPL(is_current_mnt_ns);
785
786 /*
787 * vfsmount lock must be held for write
46016270 788@@ -1987,6 +1989,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
7f207e10
AM
789 }
790 return 0;
791 }
febd17d6 792+EXPORT_SYMBOL_GPL(iterate_mounts);
7f207e10 793
fbc438ed 794 static void lock_mnt_tree(struct mount *mnt)
7f207e10 795 {
43982f53 796diff --git a/fs/notify/group.c b/fs/notify/group.c
46016270 797index fb89c351295d6..460ad19c2570a 100644
43982f53
AM
798--- a/fs/notify/group.c
799+++ b/fs/notify/group.c
42a736d3 800@@ -100,6 +100,7 @@ void fsnotify_get_group(struct fsnotify_group *group)
1716fcea 801 {
2121bcd9 802 refcount_inc(&group->refcnt);
1716fcea 803 }
febd17d6 804+EXPORT_SYMBOL_GPL(fsnotify_get_group);
1716fcea
AM
805
806 /*
807 * Drop a reference to a group. Free it if it's through.
7f207e10 808diff --git a/fs/open.c b/fs/open.c
0b2a12c6 809index e53af13b5835f..f37da065e6544 100644
7f207e10
AM
810--- a/fs/open.c
811+++ b/fs/open.c
0b2a12c6 812@@ -65,6 +65,7 @@ int do_truncate(struct user_namespace *mnt_userns, struct dentry *dentry,
febd17d6 813 inode_unlock(dentry->d_inode);
7f207e10
AM
814 return ret;
815 }
febd17d6 816+EXPORT_SYMBOL_GPL(do_truncate);
7f207e10 817
5afbbe0d 818 long vfs_truncate(const struct path *path, loff_t length)
7f207e10 819 {
5527c038 820diff --git a/fs/read_write.c b/fs/read_write.c
0b2a12c6 821index 9db7adf160d20..8dc93a57a933f 100644
5527c038
JR
822--- a/fs/read_write.c
823+++ b/fs/read_write.c
42a736d3
AM
824@@ -503,6 +503,7 @@ ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
825 inc_syscr(current);
8b6a4947
AM
826 return ret;
827 }
828+EXPORT_SYMBOL_GPL(vfs_read);
829
830 static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
831 {
42a736d3
AM
832@@ -613,6 +614,7 @@ ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_
833 file_end_write(file);
8b6a4947
AM
834 return ret;
835 }
836+EXPORT_SYMBOL_GPL(vfs_write);
837
fbc438ed
JR
838 /* file_ppos returns &file->f_pos or NULL if file is stream */
839 static inline loff_t *file_ppos(struct file *file)
7f207e10 840diff --git a/fs/splice.c b/fs/splice.c
0b2a12c6 841index 3e6ba363b7775..7c1be373eb7cd 100644
7f207e10
AM
842--- a/fs/splice.c
843+++ b/fs/splice.c
0b2a12c6 844@@ -766,6 +766,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
42a736d3
AM
845 return warn_unsupported(out, "write");
846 return out->f_op->splice_write(pipe, out, ppos, len, flags);
7f207e10 847 }
febd17d6 848+EXPORT_SYMBOL_GPL(do_splice_from);
7f207e10
AM
849
850 /*
851 * Attempt to initiate a splice from a file to a pipe.
0b2a12c6 852@@ -795,6 +796,7 @@ long do_splice_to(struct file *in, loff_t *ppos,
42a736d3
AM
853 return warn_unsupported(in, "read");
854 return in->f_op->splice_read(in, ppos, pipe, len, flags);
7f207e10 855 }
febd17d6 856+EXPORT_SYMBOL_GPL(do_splice_to);
7f207e10
AM
857
858 /**
859 * splice_direct_to_actor - splices data directly between two non-pipes
a2654f78 860diff --git a/fs/sync.c b/fs/sync.c
42a736d3 861index b7b5a0a0df6ff..fa5c7fba7f1ba 100644
a2654f78
AM
862--- a/fs/sync.c
863+++ b/fs/sync.c
2121bcd9 864@@ -39,6 +39,7 @@ int __sync_filesystem(struct super_block *sb, int wait)
a2654f78
AM
865 sb->s_op->sync_fs(sb, wait);
866 return __sync_blockdev(sb->s_bdev, wait);
867 }
868+EXPORT_SYMBOL_GPL(__sync_filesystem);
869
870 /*
871 * Write out and wait upon all dirty data associated with this
c1595e42 872diff --git a/fs/xattr.c b/fs/xattr.c
46016270 873index 5c8c5175b385c..ff7e9ff774b73 100644
c1595e42
JR
874--- a/fs/xattr.c
875+++ b/fs/xattr.c
46016270 876@@ -384,6 +384,7 @@ vfs_getxattr_alloc(struct user_namespace *mnt_userns, struct dentry *dentry,
c1595e42
JR
877 *xattr_value = value;
878 return error;
879 }
febd17d6 880+EXPORT_SYMBOL_GPL(vfs_getxattr_alloc);
c1595e42 881
febd17d6 882 ssize_t
f2c43d5f 883 __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
8b6a4947 884diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
46016270 885index dce278159546c..5b0c571dbca2f 100644
8b6a4947
AM
886--- a/kernel/locking/lockdep.c
887+++ b/kernel/locking/lockdep.c
46016270 888@@ -210,6 +210,7 @@ inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
eca34b5c
AM
889 */
890 return lock_classes + class_idx;
8b6a4947
AM
891 }
892+EXPORT_SYMBOL_GPL(lockdep_hlock_class);
893 #define hlock_class(hlock) lockdep_hlock_class(hlock)
894
895 #ifdef CONFIG_LOCK_STAT
8cdd5066 896diff --git a/kernel/task_work.c b/kernel/task_work.c
46016270 897index 1698fbe6f0e13..081b05acadf82 100644
8cdd5066
JR
898--- a/kernel/task_work.c
899+++ b/kernel/task_work.c
46016270 900@@ -167,3 +167,4 @@ void task_work_run(void)
8cdd5066
JR
901 } while (work);
902 }
903 }
febd17d6 904+EXPORT_SYMBOL_GPL(task_work_run);
7f207e10 905diff --git a/security/security.c b/security/security.c
46016270 906index b38155b2de83f..386c2741886ce 100644
7f207e10
AM
907--- a/security/security.c
908+++ b/security/security.c
46016270 909@@ -1146,6 +1146,7 @@ int security_path_rmdir(const struct path *dir, struct dentry *dentry)
7f207e10 910 return 0;
c2c0f25c 911 return call_int_hook(path_rmdir, 0, dir, dentry);
7f207e10 912 }
febd17d6 913+EXPORT_SYMBOL_GPL(security_path_rmdir);
7f207e10 914
5afbbe0d 915 int security_path_unlink(const struct path *dir, struct dentry *dentry)
7f207e10 916 {
46016270 917@@ -1162,6 +1163,7 @@ int security_path_symlink(const struct path *dir, struct dentry *dentry,
7f207e10 918 return 0;
c2c0f25c 919 return call_int_hook(path_symlink, 0, dir, dentry, old_name);
7f207e10 920 }
febd17d6 921+EXPORT_SYMBOL_GPL(security_path_symlink);
7f207e10 922
5afbbe0d 923 int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
7f207e10 924 struct dentry *new_dentry)
46016270 925@@ -1170,6 +1172,7 @@ int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
7f207e10 926 return 0;
c2c0f25c 927 return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
7f207e10 928 }
febd17d6 929+EXPORT_SYMBOL_GPL(security_path_link);
7f207e10 930
5afbbe0d
AM
931 int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
932 const struct path *new_dir, struct dentry *new_dentry,
46016270 933@@ -1197,6 +1200,7 @@ int security_path_truncate(const struct path *path)
7f207e10 934 return 0;
c2c0f25c 935 return call_int_hook(path_truncate, 0, path);
7f207e10 936 }
febd17d6 937+EXPORT_SYMBOL_GPL(security_path_truncate);
7f207e10 938
5afbbe0d 939 int security_path_chmod(const struct path *path, umode_t mode)
7eafdf33 940 {
46016270 941@@ -1204,6 +1208,7 @@ int security_path_chmod(const struct path *path, umode_t mode)
7f207e10 942 return 0;
c2c0f25c 943 return call_int_hook(path_chmod, 0, path, mode);
7f207e10 944 }
febd17d6 945+EXPORT_SYMBOL_GPL(security_path_chmod);
7f207e10 946
5afbbe0d 947 int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
7f207e10 948 {
46016270 949@@ -1211,6 +1216,7 @@ int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
7f207e10 950 return 0;
c2c0f25c 951 return call_int_hook(path_chown, 0, path, uid, gid);
7f207e10 952 }
febd17d6 953+EXPORT_SYMBOL_GPL(security_path_chown);
7f207e10 954
5afbbe0d 955 int security_path_chroot(const struct path *path)
7f207e10 956 {
46016270 957@@ -1311,6 +1317,7 @@ int security_inode_permission(struct inode *inode, int mask)
7f207e10 958 return 0;
c2c0f25c 959 return call_int_hook(inode_permission, 0, inode, mask);
7f207e10 960 }
febd17d6 961+EXPORT_SYMBOL_GPL(security_inode_permission);
7f207e10 962
1e00d052 963 int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
7f207e10 964 {
46016270 965@@ -1508,6 +1515,7 @@ int security_file_permission(struct file *file, int mask)
7f207e10
AM
966
967 return fsnotify_perm(file, mask);
968 }
febd17d6 969+EXPORT_SYMBOL_GPL(security_file_permission);
7f207e10
AM
970
971 int security_file_alloc(struct file *file)
972 {
7f207e10 973diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
eca34b5c 974--- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs 1970-01-01 01:00:00.000000000 +0100
46016270 975+++ linux/Documentation/ABI/testing/debugfs-aufs 2021-06-30 21:35:11.393873211 +0200
062440b3 976@@ -0,0 +1,55 @@
7f207e10
AM
977+What: /debug/aufs/si_<id>/
978+Date: March 2009
f6b6e03d 979+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
980+Description:
981+ Under /debug/aufs, a directory named si_<id> is created
982+ per aufs mount, where <id> is a unique id generated
983+ internally.
1facf9fc 984+
86dc4139
AM
985+What: /debug/aufs/si_<id>/plink
986+Date: Apr 2013
f6b6e03d 987+Contact: J. R. Okajima <hooanon05g@gmail.com>
86dc4139
AM
988+Description:
989+ It has three lines and shows the information about the
990+ pseudo-link. The first line is a single number
991+ representing a number of buckets. The second line is a
992+ number of pseudo-links per buckets (separated by a
993+ blank). The last line is a single number representing a
994+ total number of psedo-links.
995+ When the aufs mount option 'noplink' is specified, it
996+ will show "1\n0\n0\n".
997+
7f207e10
AM
998+What: /debug/aufs/si_<id>/xib
999+Date: March 2009
f6b6e03d 1000+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1001+Description:
1002+ It shows the consumed blocks by xib (External Inode Number
1003+ Bitmap), its block size and file size.
1004+ When the aufs mount option 'noxino' is specified, it
1005+ will be empty. About XINO files, see the aufs manual.
1006+
0b2a12c6 1007+What: /debug/aufs/si_<id>/xi<branch-index>
7f207e10 1008+Date: March 2009
f6b6e03d 1009+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1010+Description:
1011+ It shows the consumed blocks by xino (External Inode Number
1012+ Translation Table), its link count, block size and file
1013+ size.
062440b3
AM
1014+ Due to the file size limit, there may exist multiple
1015+ xino files per branch. In this case, "-N" is added to
1016+ the filename and it corresponds to the index of the
1017+ internal xino array. "-0" is omitted.
1018+ When the aufs mount option 'noxino' is specified, Those
1019+ entries won't exist. About XINO files, see the aufs
1020+ manual.
7f207e10
AM
1021+
1022+What: /debug/aufs/si_<id>/xigen
1023+Date: March 2009
f6b6e03d 1024+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1025+Description:
1026+ It shows the consumed blocks by xigen (External Inode
1027+ Generation Table), its block size and file size.
1028+ If CONFIG_AUFS_EXPORT is disabled, this entry will not
1029+ be created.
1030+ When the aufs mount option 'noxino' is specified, it
1031+ will be empty. About XINO files, see the aufs manual.
1032diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
eca34b5c 1033--- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs 1970-01-01 01:00:00.000000000 +0100
46016270 1034+++ linux/Documentation/ABI/testing/sysfs-aufs 2021-06-30 21:35:11.393873211 +0200
392086de 1035@@ -0,0 +1,31 @@
7f207e10
AM
1036+What: /sys/fs/aufs/si_<id>/
1037+Date: March 2009
f6b6e03d 1038+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1039+Description:
1040+ Under /sys/fs/aufs, a directory named si_<id> is created
1041+ per aufs mount, where <id> is a unique id generated
1042+ internally.
1043+
0b2a12c6 1044+What: /sys/fs/aufs/si_<id>/br<idx>
7f207e10 1045+Date: March 2009
f6b6e03d 1046+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1047+Description:
1048+ It shows the abolute path of a member directory (which
1049+ is called branch) in aufs, and its permission.
1050+
0b2a12c6 1051+What: /sys/fs/aufs/si_<id>/brid<idx>
392086de 1052+Date: July 2013
f6b6e03d 1053+Contact: J. R. Okajima <hooanon05g@gmail.com>
392086de
AM
1054+Description:
1055+ It shows the id of a member directory (which is called
1056+ branch) in aufs.
1057+
7f207e10
AM
1058+What: /sys/fs/aufs/si_<id>/xi_path
1059+Date: March 2009
f6b6e03d 1060+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1061+Description:
1062+ It shows the abolute path of XINO (External Inode Number
1063+ Bitmap, Translation Table and Generation Table) file
1064+ even if it is the default path.
1065+ When the aufs mount option 'noxino' is specified, it
1066+ will be empty. About XINO files, see the aufs manual.
53392da6 1067diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
eca34b5c 1068--- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt 1970-01-01 01:00:00.000000000 +0100
46016270 1069+++ linux/Documentation/filesystems/aufs/design/01intro.txt 2021-02-24 13:33:42.737680181 +0100
1c60b727 1070@@ -0,0 +1,171 @@
53392da6 1071+
d58c55f2 1072+# Copyright (C) 2005-2020 Junjiro R. Okajima
53392da6
AM
1073+#
1074+# This program is free software; you can redistribute it and/or modify
1075+# it under the terms of the GNU General Public License as published by
1076+# the Free Software Foundation; either version 2 of the License, or
1077+# (at your option) any later version.
1078+#
1079+# This program is distributed in the hope that it will be useful,
1080+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1081+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1082+# GNU General Public License for more details.
1083+#
1084+# You should have received a copy of the GNU General Public License
523b37e3 1085+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1086+
1087+Introduction
1088+----------------------------------------
1089+
3c1bdaff 1090+aufs [ei ju: ef es] | /ey-yoo-ef-es/ | [a u f s]
53392da6
AM
1091+1. abbrev. for "advanced multi-layered unification filesystem".
1092+2. abbrev. for "another unionfs".
1093+3. abbrev. for "auf das" in German which means "on the" in English.
1094+ Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
1095+ But "Filesystem aufs Filesystem" is hard to understand.
1c60b727 1096+4. abbrev. for "African Urban Fashion Show".
53392da6
AM
1097+
1098+AUFS is a filesystem with features:
1099+- multi layered stackable unification filesystem, the member directory
1100+ is called as a branch.
1101+- branch permission and attribute, 'readonly', 'real-readonly',
7e9cd9fe 1102+ 'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
53392da6
AM
1103+ combination.
1104+- internal "file copy-on-write".
1105+- logical deletion, whiteout.
1106+- dynamic branch manipulation, adding, deleting and changing permission.
1107+- allow bypassing aufs, user's direct branch access.
1108+- external inode number translation table and bitmap which maintains the
1109+ persistent aufs inode number.
1110+- seekable directory, including NFS readdir.
1111+- file mapping, mmap and sharing pages.
1112+- pseudo-link, hardlink over branches.
1113+- loopback mounted filesystem as a branch.
1114+- several policies to select one among multiple writable branches.
1115+- revert a single systemcall when an error occurs in aufs.
1116+- and more...
1117+
1118+
1119+Multi Layered Stackable Unification Filesystem
1120+----------------------------------------------------------------------
1121+Most people already knows what it is.
1122+It is a filesystem which unifies several directories and provides a
1123+merged single directory. When users access a file, the access will be
1124+passed/re-directed/converted (sorry, I am not sure which English word is
1125+correct) to the real file on the member filesystem. The member
1126+filesystem is called 'lower filesystem' or 'branch' and has a mode
1127+'readonly' and 'readwrite.' And the deletion for a file on the lower
1128+readonly branch is handled by creating 'whiteout' on the upper writable
1129+branch.
1130+
1131+On LKML, there have been discussions about UnionMount (Jan Blunck,
1132+Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
1133+different approaches to implement the merged-view.
1134+The former tries putting it into VFS, and the latter implements as a
1135+separate filesystem.
1136+(If I misunderstand about these implementations, please let me know and
1137+I shall correct it. Because it is a long time ago when I read their
1138+source files last time).
1139+
1140+UnionMount's approach will be able to small, but may be hard to share
1141+branches between several UnionMount since the whiteout in it is
1142+implemented in the inode on branch filesystem and always
1143+shared. According to Bharata's post, readdir does not seems to be
1144+finished yet.
1145+There are several missing features known in this implementations such as
1146+- for users, the inode number may change silently. eg. copy-up.
1147+- link(2) may break by copy-up.
1148+- read(2) may get an obsoleted filedata (fstat(2) too).
1149+- fcntl(F_SETLK) may be broken by copy-up.
1150+- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
1151+ open(O_RDWR).
1152+
7e9cd9fe
AM
1153+In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
1154+merged into mainline. This is another implementation of UnionMount as a
1155+separated filesystem. All the limitations and known problems which
1156+UnionMount are equally inherited to "overlay" filesystem.
1157+
1158+Unionfs has a longer history. When I started implementing a stackable
1159+filesystem (Aug 2005), it already existed. It has virtual super_block,
1160+inode, dentry and file objects and they have an array pointing lower
1161+same kind objects. After contributing many patches for Unionfs, I
1162+re-started my project AUFS (Jun 2006).
53392da6
AM
1163+
1164+In AUFS, the structure of filesystem resembles to Unionfs, but I
1165+implemented my own ideas, approaches and enhancements and it became
1166+totally different one.
1167+
1168+Comparing DM snapshot and fs based implementation
1169+- the number of bytes to be copied between devices is much smaller.
1170+- the type of filesystem must be one and only.
1171+- the fs must be writable, no readonly fs, even for the lower original
1172+ device. so the compression fs will not be usable. but if we use
1173+ loopback mount, we may address this issue.
1174+ for instance,
1175+ mount /cdrom/squashfs.img /sq
1176+ losetup /sq/ext2.img
1177+ losetup /somewhere/cow
1178+ dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1179+- it will be difficult (or needs more operations) to extract the
1180+ difference between the original device and COW.
1181+- DM snapshot-merge may help a lot when users try merging. in the
1182+ fs-layer union, users will use rsync(1).
1183+
7e9cd9fe
AM
1184+You may want to read my old paper "Filesystems in LiveCD"
1185+(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
53392da6 1186+
7e9cd9fe
AM
1187+
1188+Several characters/aspects/persona of aufs
53392da6
AM
1189+----------------------------------------------------------------------
1190+
7e9cd9fe 1191+Aufs has several characters, aspects or persona.
53392da6
AM
1192+1. a filesystem, callee of VFS helper
1193+2. sub-VFS, caller of VFS helper for branches
1194+3. a virtual filesystem which maintains persistent inode number
1195+4. reader/writer of files on branches such like an application
1196+
1197+1. Callee of VFS Helper
1198+As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1199+unlink(2) from an application reaches sys_unlink() kernel function and
1200+then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1201+calls filesystem specific unlink operation. Actually aufs implements the
1202+unlink operation but it behaves like a redirector.
1203+
1204+2. Caller of VFS Helper for Branches
1205+aufs_unlink() passes the unlink request to the branch filesystem as if
1206+it were called from VFS. So the called unlink operation of the branch
1207+filesystem acts as usual. As a caller of VFS helper, aufs should handle
1208+every necessary pre/post operation for the branch filesystem.
1209+- acquire the lock for the parent dir on a branch
1210+- lookup in a branch
1211+- revalidate dentry on a branch
1212+- mnt_want_write() for a branch
1213+- vfs_unlink() for a branch
1214+- mnt_drop_write() for a branch
1215+- release the lock on a branch
1216+
1217+3. Persistent Inode Number
1218+One of the most important issue for a filesystem is to maintain inode
1219+numbers. This is particularly important to support exporting a
1220+filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1221+backend block device for its own. But some storage is necessary to
7e9cd9fe
AM
1222+keep and maintain the inode numbers. It may be a large space and may not
1223+suit to keep in memory. Aufs rents some space from its first writable
1224+branch filesystem (by default) and creates file(s) on it. These files
1225+are created by aufs internally and removed soon (currently) keeping
1226+opened.
53392da6
AM
1227+Note: Because these files are removed, they are totally gone after
1228+ unmounting aufs. It means the inode numbers are not persistent
1229+ across unmount or reboot. I have a plan to make them really
1230+ persistent which will be important for aufs on NFS server.
1231+
1232+4. Read/Write Files Internally (copy-on-write)
1233+Because a branch can be readonly, when you write a file on it, aufs will
1234+"copy-up" it to the upper writable branch internally. And then write the
1235+originally requested thing to the file. Generally kernel doesn't
1236+open/read/write file actively. In aufs, even a single write may cause a
1237+internal "file copy". This behaviour is very similar to cp(1) command.
1238+
1239+Some people may think it is better to pass such work to user space
1240+helper, instead of doing in kernel space. Actually I am still thinking
1241+about it. But currently I have implemented it in kernel space.
1242diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
eca34b5c 1243--- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
46016270 1244+++ linux/Documentation/filesystems/aufs/design/02struct.txt 2021-02-24 13:33:42.737680181 +0100
7e9cd9fe 1245@@ -0,0 +1,258 @@
53392da6 1246+
d58c55f2 1247+# Copyright (C) 2005-2020 Junjiro R. Okajima
53392da6
AM
1248+#
1249+# This program is free software; you can redistribute it and/or modify
1250+# it under the terms of the GNU General Public License as published by
1251+# the Free Software Foundation; either version 2 of the License, or
1252+# (at your option) any later version.
1253+#
1254+# This program is distributed in the hope that it will be useful,
1255+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1256+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1257+# GNU General Public License for more details.
1258+#
1259+# You should have received a copy of the GNU General Public License
523b37e3 1260+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1261+
1262+Basic Aufs Internal Structure
1263+
1264+Superblock/Inode/Dentry/File Objects
1265+----------------------------------------------------------------------
1266+As like an ordinary filesystem, aufs has its own
1267+superblock/inode/dentry/file objects. All these objects have a
1268+dynamically allocated array and store the same kind of pointers to the
1269+lower filesystem, branch.
1270+For example, when you build a union with one readwrite branch and one
1271+readonly, mounted /au, /rw and /ro respectively.
1272+- /au = /rw + /ro
1273+- /ro/fileA exists but /rw/fileA
1274+
1275+Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1276+pointers are stored in a aufs dentry. The array in aufs dentry will be,
7e9cd9fe 1277+- [0] = NULL (because /rw/fileA doesn't exist)
53392da6
AM
1278+- [1] = /ro/fileA
1279+
1280+This style of an array is essentially same to the aufs
1281+superblock/inode/dentry/file objects.
1282+
1283+Because aufs supports manipulating branches, ie. add/delete/change
7e9cd9fe
AM
1284+branches dynamically, these objects has its own generation. When
1285+branches are changed, the generation in aufs superblock is
1286+incremented. And a generation in other object are compared when it is
1287+accessed. When a generation in other objects are obsoleted, aufs
1288+refreshes the internal array.
53392da6
AM
1289+
1290+
1291+Superblock
1292+----------------------------------------------------------------------
1293+Additionally aufs superblock has some data for policies to select one
1294+among multiple writable branches, XIB files, pseudo-links and kobject.
1295+See below in detail.
7e9cd9fe
AM
1296+About the policies which supports copy-down a directory, see
1297+wbr_policy.txt too.
53392da6
AM
1298+
1299+
1300+Branch and XINO(External Inode Number Translation Table)
1301+----------------------------------------------------------------------
1302+Every branch has its own xino (external inode number translation table)
1303+file. The xino file is created and unlinked by aufs internally. When two
1304+members of a union exist on the same filesystem, they share the single
1305+xino file.
1306+The struct of a xino file is simple, just a sequence of aufs inode
1307+numbers which is indexed by the lower inode number.
1308+In the above sample, assume the inode number of /ro/fileA is i111 and
1309+aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1310+4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1311+
1312+When the inode numbers are not contiguous, the xino file will be sparse
1313+which has a hole in it and doesn't consume as much disk space as it
1314+might appear. If your branch filesystem consumes disk space for such
1315+holes, then you should specify 'xino=' option at mounting aufs.
1316+
7e9cd9fe
AM
1317+Aufs has a mount option to free the disk blocks for such holes in XINO
1318+files on tmpfs or ramdisk. But it is not so effective actually. If you
1319+meet a problem of disk shortage due to XINO files, then you should try
1320+"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1321+The patch localizes the assignment inumbers per tmpfs-mount and avoid
1322+the holes in XINO files.
1323+
53392da6 1324+Also a writable branch has three kinds of "whiteout bases". All these
7e9cd9fe 1325+are existed when the branch is joined to aufs, and their names are
53392da6
AM
1326+whiteout-ed doubly, so that users will never see their names in aufs
1327+hierarchy.
7e9cd9fe 1328+1. a regular file which will be hardlinked to all whiteouts.
53392da6 1329+2. a directory to store a pseudo-link.
7e9cd9fe 1330+3. a directory to store an "orphan"-ed file temporary.
53392da6
AM
1331+
1332+1. Whiteout Base
1333+ When you remove a file on a readonly branch, aufs handles it as a
1334+ logical deletion and creates a whiteout on the upper writable branch
1335+ as a hardlink of this file in order not to consume inode on the
1336+ writable branch.
1337+2. Pseudo-link Dir
1338+ See below, Pseudo-link.
1339+3. Step-Parent Dir
1340+ When "fileC" exists on the lower readonly branch only and it is
1341+ opened and removed with its parent dir, and then user writes
1342+ something into it, then aufs copies-up fileC to this
1343+ directory. Because there is no other dir to store fileC. After
1344+ creating a file under this dir, the file is unlinked.
1345+
1346+Because aufs supports manipulating branches, ie. add/delete/change
7e9cd9fe
AM
1347+dynamically, a branch has its own id. When the branch order changes,
1348+aufs finds the new index by searching the branch id.
53392da6
AM
1349+
1350+
1351+Pseudo-link
1352+----------------------------------------------------------------------
1353+Assume "fileA" exists on the lower readonly branch only and it is
1354+hardlinked to "fileB" on the branch. When you write something to fileA,
1355+aufs copies-up it to the upper writable branch. Additionally aufs
1356+creates a hardlink under the Pseudo-link Directory of the writable
1357+branch. The inode of a pseudo-link is kept in aufs super_block as a
1358+simple list. If fileB is read after unlinking fileA, aufs returns
1359+filedata from the pseudo-link instead of the lower readonly
1360+branch. Because the pseudo-link is based upon the inode, to keep the
7e9cd9fe 1361+inode number by xino (see above) is essentially necessary.
53392da6
AM
1362+
1363+All the hardlinks under the Pseudo-link Directory of the writable branch
1364+should be restored in a proper location later. Aufs provides a utility
1365+to do this. The userspace helpers executed at remounting and unmounting
1366+aufs by default.
1367+During this utility is running, it puts aufs into the pseudo-link
1368+maintenance mode. In this mode, only the process which began the
1369+maintenance mode (and its child processes) is allowed to operate in
1370+aufs. Some other processes which are not related to the pseudo-link will
1371+be allowed to run too, but the rest have to return an error or wait
1372+until the maintenance mode ends. If a process already acquires an inode
1373+mutex (in VFS), it has to return an error.
1374+
1375+
1376+XIB(external inode number bitmap)
1377+----------------------------------------------------------------------
1378+Addition to the xino file per a branch, aufs has an external inode number
7e9cd9fe
AM
1379+bitmap in a superblock object. It is also an internal file such like a
1380+xino file.
53392da6
AM
1381+It is a simple bitmap to mark whether the aufs inode number is in-use or
1382+not.
1383+To reduce the file I/O, aufs prepares a single memory page to cache xib.
1384+
7e9cd9fe 1385+As well as XINO files, aufs has a feature to truncate/refresh XIB to
53392da6
AM
1386+reduce the number of consumed disk blocks for these files.
1387+
1388+
1389+Virtual or Vertical Dir, and Readdir in Userspace
1390+----------------------------------------------------------------------
1391+In order to support multiple layers (branches), aufs readdir operation
1392+constructs a virtual dir block on memory. For readdir, aufs calls
1393+vfs_readdir() internally for each dir on branches, merges their entries
1394+with eliminating the whiteout-ed ones, and sets it to file (dir)
1395+object. So the file object has its entry list until it is closed. The
1396+entry list will be updated when the file position is zero and becomes
7e9cd9fe 1397+obsoleted. This decision is made in aufs automatically.
53392da6
AM
1398+
1399+The dynamically allocated memory block for the name of entries has a
1400+unit of 512 bytes (by default) and stores the names contiguously (no
1401+padding). Another block for each entry is handled by kmem_cache too.
1402+During building dir blocks, aufs creates hash list and judging whether
1403+the entry is whiteouted by its upper branch or already listed.
1404+The merged result is cached in the corresponding inode object and
1405+maintained by a customizable life-time option.
1406+
1407+Some people may call it can be a security hole or invite DoS attack
1408+since the opened and once readdir-ed dir (file object) holds its entry
1409+list and becomes a pressure for system memory. But I'd say it is similar
1410+to files under /proc or /sys. The virtual files in them also holds a
1411+memory page (generally) while they are opened. When an idea to reduce
1412+memory for them is introduced, it will be applied to aufs too.
1413+For those who really hate this situation, I've developed readdir(3)
1414+library which operates this merging in userspace. You just need to set
1415+LD_PRELOAD environment variable, and aufs will not consume no memory in
1416+kernel space for readdir(3).
1417+
1418+
1419+Workqueue
1420+----------------------------------------------------------------------
1421+Aufs sometimes requires privilege access to a branch. For instance,
1422+in copy-up/down operation. When a user process is going to make changes
1423+to a file which exists in the lower readonly branch only, and the mode
1424+of one of ancestor directories may not be writable by a user
1425+process. Here aufs copy-up the file with its ancestors and they may
1426+require privilege to set its owner/group/mode/etc.
1427+This is a typical case of a application character of aufs (see
1428+Introduction).
1429+
1430+Aufs uses workqueue synchronously for this case. It creates its own
1431+workqueue. The workqueue is a kernel thread and has privilege. Aufs
1432+passes the request to call mkdir or write (for example), and wait for
1433+its completion. This approach solves a problem of a signal handler
1434+simply.
1435+If aufs didn't adopt the workqueue and changed the privilege of the
7e9cd9fe
AM
1436+process, then the process may receive the unexpected SIGXFSZ or other
1437+signals.
53392da6
AM
1438+
1439+Also aufs uses the system global workqueue ("events" kernel thread) too
1440+for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1441+whiteout base and etc. This is unrelated to a privilege.
1442+Most of aufs operation tries acquiring a rw_semaphore for aufs
1443+superblock at the beginning, at the same time waits for the completion
1444+of all queued asynchronous tasks.
1445+
1446+
1447+Whiteout
1448+----------------------------------------------------------------------
1449+The whiteout in aufs is very similar to Unionfs's. That is represented
1450+by its filename. UnionMount takes an approach of a file mode, but I am
1451+afraid several utilities (find(1) or something) will have to support it.
1452+
1453+Basically the whiteout represents "logical deletion" which stops aufs to
1454+lookup further, but also it represents "dir is opaque" which also stop
7e9cd9fe 1455+further lookup.
53392da6
AM
1456+
1457+In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1458+In order to make several functions in a single systemcall to be
1459+revertible, aufs adopts an approach to rename a directory to a temporary
1460+unique whiteouted name.
1461+For example, in rename(2) dir where the target dir already existed, aufs
1462+renames the target dir to a temporary unique whiteouted name before the
7e9cd9fe 1463+actual rename on a branch, and then handles other actions (make it opaque,
53392da6
AM
1464+update the attributes, etc). If an error happens in these actions, aufs
1465+simply renames the whiteouted name back and returns an error. If all are
1466+succeeded, aufs registers a function to remove the whiteouted unique
1467+temporary name completely and asynchronously to the system global
1468+workqueue.
1469+
1470+
1471+Copy-up
1472+----------------------------------------------------------------------
1473+It is a well-known feature or concept.
1474+When user modifies a file on a readonly branch, aufs operate "copy-up"
1475+internally and makes change to the new file on the upper writable branch.
1476+When the trigger systemcall does not update the timestamps of the parent
1477+dir, aufs reverts it after copy-up.
c2b27bf2
AM
1478+
1479+
1480+Move-down (aufs3.9 and later)
1481+----------------------------------------------------------------------
1482+"Copy-up" is one of the essential feature in aufs. It copies a file from
1483+the lower readonly branch to the upper writable branch when a user
1484+changes something about the file.
1485+"Move-down" is an opposite action of copy-up. Basically this action is
1486+ran manually instead of automatically and internally.
076b876e
AM
1487+For desgin and implementation, aufs has to consider these issues.
1488+- whiteout for the file may exist on the lower branch.
1489+- ancestor directories may not exist on the lower branch.
1490+- diropq for the ancestor directories may exist on the upper branch.
1491+- free space on the lower branch will reduce.
1492+- another access to the file may happen during moving-down, including
7e9cd9fe 1493+ UDBA (see "Revalidate Dentry and UDBA").
076b876e
AM
1494+- the file should not be hard-linked nor pseudo-linked. they should be
1495+ handled by auplink utility later.
c2b27bf2
AM
1496+
1497+Sometimes users want to move-down a file from the upper writable branch
1498+to the lower readonly or writable branch. For instance,
1499+- the free space of the upper writable branch is going to run out.
1500+- create a new intermediate branch between the upper and lower branch.
1501+- etc.
1502+
1503+For this purpose, use "aumvdown" command in aufs-util.git.
b912730e 1504diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt linux/Documentation/filesystems/aufs/design/03atomic_open.txt
eca34b5c 1505--- /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt 1970-01-01 01:00:00.000000000 +0100
46016270 1506+++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt 2021-02-24 13:33:42.737680181 +0100
b912730e
AM
1507@@ -0,0 +1,85 @@
1508+
d58c55f2 1509+# Copyright (C) 2015-2020 Junjiro R. Okajima
b912730e
AM
1510+#
1511+# This program is free software; you can redistribute it and/or modify
1512+# it under the terms of the GNU General Public License as published by
1513+# the Free Software Foundation; either version 2 of the License, or
1514+# (at your option) any later version.
1515+#
1516+# This program is distributed in the hope that it will be useful,
1517+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1518+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1519+# GNU General Public License for more details.
1520+#
1521+# You should have received a copy of the GNU General Public License
1522+# along with this program. If not, see <http://www.gnu.org/licenses/>.
1523+
1524+Support for a branch who has its ->atomic_open()
1525+----------------------------------------------------------------------
1526+The filesystems who implement its ->atomic_open() are not majority. For
1527+example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
1528+particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
1529+->atomic_open(), NFSv4 returns an error for this open(2). While I am not
1530+sure whether all filesystems who have ->atomic_open() behave like this,
1531+but NFSv4 surely returns the error.
1532+
1533+In order to support ->atomic_open() for aufs, there are a few
1534+approaches.
1535+
1536+A. Introduce aufs_atomic_open()
1537+ - calls one of VFS:do_last(), lookup_open() or atomic_open() for
1538+ branch fs.
1539+B. Introduce aufs_atomic_open() calling create, open and chmod. this is
1540+ an aufs user Pip Cet's approach
1541+ - calls aufs_create(), VFS finish_open() and notify_change().
1542+ - pass fake-mode to finish_open(), and then correct the mode by
1543+ notify_change().
1544+C. Extend aufs_open() to call branch fs's ->atomic_open()
1545+ - no aufs_atomic_open().
1546+ - aufs_lookup() registers the TID to an aufs internal object.
1547+ - aufs_create() does nothing when the matching TID is registered, but
1548+ registers the mode.
1549+ - aufs_open() calls branch fs's ->atomic_open() when the matching
1550+ TID is registered.
1551+D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
1552+ credential
1553+ - no aufs_atomic_open().
1554+ - aufs_create() registers the TID to an internal object. this info
1555+ represents "this process created this file just now."
1556+ - when aufs gets EACCES from branch fs's ->open(), then confirm the
1557+ registered TID and re-try open() with superuser's credential.
1558+
1559+Pros and cons for each approach.
1560+
1561+A.
1562+ - straightforward but highly depends upon VFS internal.
1563+ - the atomic behavaiour is kept.
1564+ - some of parameters such as nameidata are hard to reproduce for
1565+ branch fs.
1566+ - large overhead.
1567+B.
1568+ - easy to implement.
1569+ - the atomic behavaiour is lost.
1570+C.
1571+ - the atomic behavaiour is kept.
1572+ - dirty and tricky.
1573+ - VFS checks whether the file is created correctly after calling
1574+ ->create(), which means this approach doesn't work.
1575+D.
1576+ - easy to implement.
1577+ - the atomic behavaiour is lost.
1578+ - to open a file with superuser's credential and give it to a user
1579+ process is a bad idea, since the file object keeps the credential
1580+ in it. It may affect LSM or something. This approach doesn't work
1581+ either.
1582+
1583+The approach A is ideal, but it hard to implement. So here is a
1584+variation of A, which is to be implemented.
1585+
1586+A-1. Introduce aufs_atomic_open()
1587+ - calls branch fs ->atomic_open() if exists. otherwise calls
1588+ vfs_create() and finish_open().
1589+ - the demerit is that the several checks after branch fs
1590+ ->atomic_open() are lost. in the ordinary case, the checks are
1591+ done by VFS:do_last(), lookup_open() and atomic_open(). some can
1592+ be implemented in aufs, but not all I am afraid.
53392da6 1593diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
eca34b5c 1594--- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
46016270 1595+++ linux/Documentation/filesystems/aufs/design/03lookup.txt 2021-02-24 13:33:42.737680181 +0100
7e9cd9fe 1596@@ -0,0 +1,113 @@
53392da6 1597+
d58c55f2 1598+# Copyright (C) 2005-2020 Junjiro R. Okajima
53392da6
AM
1599+#
1600+# This program is free software; you can redistribute it and/or modify
1601+# it under the terms of the GNU General Public License as published by
1602+# the Free Software Foundation; either version 2 of the License, or
1603+# (at your option) any later version.
1604+#
1605+# This program is distributed in the hope that it will be useful,
1606+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1607+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1608+# GNU General Public License for more details.
1609+#
1610+# You should have received a copy of the GNU General Public License
523b37e3 1611+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1612+
1613+Lookup in a Branch
1614+----------------------------------------------------------------------
1615+Since aufs has a character of sub-VFS (see Introduction), it operates
7e9cd9fe
AM
1616+lookup for branches as VFS does. It may be a heavy work. But almost all
1617+lookup operation in aufs is the simplest case, ie. lookup only an entry
1618+directly connected to its parent. Digging down the directory hierarchy
1619+is unnecessary. VFS has a function lookup_one_len() for that use, and
1620+aufs calls it.
1621+
1622+When a branch is a remote filesystem, aufs basically relies upon its
53392da6
AM
1623+->d_revalidate(), also aufs forces the hardest revalidate tests for
1624+them.
1625+For d_revalidate, aufs implements three levels of revalidate tests. See
1626+"Revalidate Dentry and UDBA" in detail.
1627+
1628+
076b876e
AM
1629+Test Only the Highest One for the Directory Permission (dirperm1 option)
1630+----------------------------------------------------------------------
1631+Let's try case study.
1632+- aufs has two branches, upper readwrite and lower readonly.
1633+ /au = /rw + /ro
1634+- "dirA" exists under /ro, but /rw. and its mode is 0700.
1635+- user invoked "chmod a+rx /au/dirA"
1636+- the internal copy-up is activated and "/rw/dirA" is created and its
7e9cd9fe 1637+ permission bits are set to world readable.
076b876e
AM
1638+- then "/au/dirA" becomes world readable?
1639+
1640+In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1641+or it may be a natively readonly filesystem. If aufs respects the lower
1642+branch, it should not respond readdir request from other users. But user
1643+allowed it by chmod. Should really aufs rejects showing the entries
1644+under /ro/dirA?
1645+
7e9cd9fe
AM
1646+To be honest, I don't have a good solution for this case. So aufs
1647+implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1648+users.
076b876e
AM
1649+When dirperm1 is specified, aufs checks only the highest one for the
1650+directory permission, and shows the entries. Otherwise, as usual, checks
1651+every dir existing on all branches and rejects the request.
1652+
1653+As a side effect, dirperm1 option improves the performance of aufs
1654+because the number of permission check is reduced when the number of
1655+branch is many.
1656+
1657+
53392da6
AM
1658+Revalidate Dentry and UDBA (User's Direct Branch Access)
1659+----------------------------------------------------------------------
1660+Generally VFS helpers re-validate a dentry as a part of lookup.
1661+0. digging down the directory hierarchy.
1662+1. lock the parent dir by its i_mutex.
1663+2. lookup the final (child) entry.
1664+3. revalidate it.
1665+4. call the actual operation (create, unlink, etc.)
1666+5. unlock the parent dir
1667+
1668+If the filesystem implements its ->d_revalidate() (step 3), then it is
1669+called. Actually aufs implements it and checks the dentry on a branch is
1670+still valid.
1671+But it is not enough. Because aufs has to release the lock for the
1672+parent dir on a branch at the end of ->lookup() (step 2) and
1673+->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1674+held by VFS.
1675+If the file on a branch is changed directly, eg. bypassing aufs, after
1676+aufs released the lock, then the subsequent operation may cause
1677+something unpleasant result.
1678+
1679+This situation is a result of VFS architecture, ->lookup() and
1680+->d_revalidate() is separated. But I never say it is wrong. It is a good
1681+design from VFS's point of view. It is just not suitable for sub-VFS
1682+character in aufs.
1683+
1684+Aufs supports such case by three level of revalidation which is
1685+selectable by user.
1686+1. Simple Revalidate
1687+ Addition to the native flow in VFS's, confirm the child-parent
1688+ relationship on the branch just after locking the parent dir on the
1689+ branch in the "actual operation" (step 4). When this validation
1690+ fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1691+ checks the validation of the dentry on branches.
1692+2. Monitor Changes Internally by Inotify/Fsnotify
1693+ Addition to above, in the "actual operation" (step 4) aufs re-lookup
1694+ the dentry on the branch, and returns EBUSY if it finds different
1695+ dentry.
1696+ Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1697+ during it is in cache. When the event is notified, aufs registers a
1698+ function to kernel 'events' thread by schedule_work(). And the
1699+ function sets some special status to the cached aufs dentry and inode
1700+ private data. If they are not cached, then aufs has nothing to
1701+ do. When the same file is accessed through aufs (step 0-3) later,
1702+ aufs will detect the status and refresh all necessary data.
1703+ In this mode, aufs has to ignore the event which is fired by aufs
1704+ itself.
1705+3. No Extra Validation
1706+ This is the simplest test and doesn't add any additional revalidation
7e9cd9fe 1707+ test, and skip the revalidation in step 4. It is useful and improves
53392da6
AM
1708+ aufs performance when system surely hide the aufs branches from user,
1709+ by over-mounting something (or another method).
1710diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
eca34b5c 1711--- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
46016270 1712+++ linux/Documentation/filesystems/aufs/design/04branch.txt 2021-02-24 13:33:42.737680181 +0100
7e9cd9fe 1713@@ -0,0 +1,74 @@
53392da6 1714+
d58c55f2 1715+# Copyright (C) 2005-2020 Junjiro R. Okajima
53392da6
AM
1716+#
1717+# This program is free software; you can redistribute it and/or modify
1718+# it under the terms of the GNU General Public License as published by
1719+# the Free Software Foundation; either version 2 of the License, or
1720+# (at your option) any later version.
1721+#
1722+# This program is distributed in the hope that it will be useful,
1723+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1724+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1725+# GNU General Public License for more details.
1726+#
1727+# You should have received a copy of the GNU General Public License
523b37e3 1728+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1729+
1730+Branch Manipulation
1731+
1732+Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1733+and changing its permission/attribute, there are a lot of works to do.
1734+
1735+
1736+Add a Branch
1737+----------------------------------------------------------------------
1738+o Confirm the adding dir exists outside of aufs, including loopback
7e9cd9fe 1739+ mount, and its various attributes.
53392da6
AM
1740+o Initialize the xino file and whiteout bases if necessary.
1741+ See struct.txt.
1742+
1743+o Check the owner/group/mode of the directory
1744+ When the owner/group/mode of the adding directory differs from the
1745+ existing branch, aufs issues a warning because it may impose a
1746+ security risk.
1747+ For example, when a upper writable branch has a world writable empty
1748+ top directory, a malicious user can create any files on the writable
1749+ branch directly, like copy-up and modify manually. If something like
1750+ /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1751+ writable branch, and the writable branch is world-writable, then a
1752+ malicious guy may create /etc/passwd on the writable branch directly
1753+ and the infected file will be valid in aufs.
7e9cd9fe 1754+ I am afraid it can be a security issue, but aufs can do nothing except
53392da6
AM
1755+ producing a warning.
1756+
1757+
1758+Delete a Branch
1759+----------------------------------------------------------------------
1760+o Confirm the deleting branch is not busy
1761+ To be general, there is one merit to adopt "remount" interface to
1762+ manipulate branches. It is to discard caches. At deleting a branch,
1763+ aufs checks the still cached (and connected) dentries and inodes. If
1764+ there are any, then they are all in-use. An inode without its
1765+ corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1766+
1767+ For the cached one, aufs checks whether the same named entry exists on
1768+ other branches.
1769+ If the cached one is a directory, because aufs provides a merged view
1770+ to users, as long as one dir is left on any branch aufs can show the
1771+ dir to users. In this case, the branch can be removed from aufs.
1772+ Otherwise aufs rejects deleting the branch.
1773+
1774+ If any file on the deleting branch is opened by aufs, then aufs
1775+ rejects deleting.
1776+
1777+
1778+Modify the Permission of a Branch
1779+----------------------------------------------------------------------
1780+o Re-initialize or remove the xino file and whiteout bases if necessary.
1781+ See struct.txt.
1782+
1783+o rw --> ro: Confirm the modifying branch is not busy
1784+ Aufs rejects the request if any of these conditions are true.
1785+ - a file on the branch is mmap-ed.
1786+ - a regular file on the branch is opened for write and there is no
1787+ same named entry on the upper branch.
1788diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
eca34b5c 1789--- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt 1970-01-01 01:00:00.000000000 +0100
46016270 1790+++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt 2021-02-24 13:33:42.737680181 +0100
523b37e3 1791@@ -0,0 +1,64 @@
53392da6 1792+
d58c55f2 1793+# Copyright (C) 2005-2020 Junjiro R. Okajima
53392da6
AM
1794+#
1795+# This program is free software; you can redistribute it and/or modify
1796+# it under the terms of the GNU General Public License as published by
1797+# the Free Software Foundation; either version 2 of the License, or
1798+# (at your option) any later version.
1799+#
1800+# This program is distributed in the hope that it will be useful,
1801+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1802+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1803+# GNU General Public License for more details.
1804+#
1805+# You should have received a copy of the GNU General Public License
523b37e3 1806+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1807+
1808+Policies to Select One among Multiple Writable Branches
1809+----------------------------------------------------------------------
1810+When the number of writable branch is more than one, aufs has to decide
1811+the target branch for file creation or copy-up. By default, the highest
1812+writable branch which has the parent (or ancestor) dir of the target
1813+file is chosen (top-down-parent policy).
1814+By user's request, aufs implements some other policies to select the
7e9cd9fe
AM
1815+writable branch, for file creation several policies, round-robin,
1816+most-free-space, and other policies. For copy-up, top-down-parent,
1817+bottom-up-parent, bottom-up and others.
53392da6
AM
1818+
1819+As expected, the round-robin policy selects the branch in circular. When
1820+you have two writable branches and creates 10 new files, 5 files will be
1821+created for each branch. mkdir(2) systemcall is an exception. When you
1822+create 10 new directories, all will be created on the same branch.
1823+And the most-free-space policy selects the one which has most free
1824+space among the writable branches. The amount of free space will be
1825+checked by aufs internally, and users can specify its time interval.
1826+
1827+The policies for copy-up is more simple,
1828+top-down-parent is equivalent to the same named on in create policy,
1829+bottom-up-parent selects the writable branch where the parent dir
1830+exists and the nearest upper one from the copyup-source,
1831+bottom-up selects the nearest upper writable branch from the
1832+copyup-source, regardless the existence of the parent dir.
1833+
1834+There are some rules or exceptions to apply these policies.
1835+- If there is a readonly branch above the policy-selected branch and
1836+ the parent dir is marked as opaque (a variation of whiteout), or the
1837+ target (creating) file is whiteout-ed on the upper readonly branch,
1838+ then the result of the policy is ignored and the target file will be
1839+ created on the nearest upper writable branch than the readonly branch.
1840+- If there is a writable branch above the policy-selected branch and
1841+ the parent dir is marked as opaque or the target file is whiteouted
1842+ on the branch, then the result of the policy is ignored and the target
1843+ file will be created on the highest one among the upper writable
1844+ branches who has diropq or whiteout. In case of whiteout, aufs removes
1845+ it as usual.
1846+- link(2) and rename(2) systemcalls are exceptions in every policy.
1847+ They try selecting the branch where the source exists as possible
1848+ since copyup a large file will take long time. If it can't be,
1849+ ie. the branch where the source exists is readonly, then they will
1850+ follow the copyup policy.
1851+- There is an exception for rename(2) when the target exists.
1852+ If the rename target exists, aufs compares the index of the branches
1853+ where the source and the target exists and selects the higher
1854+ one. If the selected branch is readonly, then aufs follows the
1855+ copyup policy.
8b6a4947 1856diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot linux/Documentation/filesystems/aufs/design/06dirren.dot
eca34b5c 1857--- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot 1970-01-01 01:00:00.000000000 +0100
46016270 1858+++ linux/Documentation/filesystems/aufs/design/06dirren.dot 2021-02-24 13:33:42.737680181 +0100
8b6a4947
AM
1859@@ -0,0 +1,31 @@
1860+
1861+// to view this graph, run dot(1) command in GRAPHVIZ.
1862+
1863+digraph G {
1864+node [shape=box];
1865+whinfo [label="detailed info file\n(lower_brid_root-hinum, h_inum, namelen, old name)"];
1866+
1867+node [shape=oval];
1868+
1869+aufs_rename -> whinfo [label="store/remove"];
1870+
1871+node [shape=oval];
1872+inode_list [label="h_inum list in branch\ncache"];
1873+
1874+node [shape=box];
1875+whinode [label="h_inum list file"];
1876+
1877+node [shape=oval];
1878+brmgmt [label="br_add/del/mod/umount"];
1879+
1880+brmgmt -> inode_list [label="create/remove"];
1881+brmgmt -> whinode [label="load/store"];
1882+
1883+inode_list -> whinode [style=dashed,dir=both];
1884+
1885+aufs_rename -> inode_list [label="add/del"];
1886+
1887+aufs_lookup -> inode_list [label="search"];
1888+
1889+aufs_lookup -> whinfo [label="load/remove"];
1890+}
1891diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt linux/Documentation/filesystems/aufs/design/06dirren.txt
eca34b5c 1892--- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt 1970-01-01 01:00:00.000000000 +0100
46016270 1893+++ linux/Documentation/filesystems/aufs/design/06dirren.txt 2021-02-24 13:33:42.737680181 +0100
8b6a4947
AM
1894@@ -0,0 +1,102 @@
1895+
d58c55f2 1896+# Copyright (C) 2017-2020 Junjiro R. Okajima
8b6a4947
AM
1897+#
1898+# This program is free software; you can redistribute it and/or modify
1899+# it under the terms of the GNU General Public License as published by
1900+# the Free Software Foundation; either version 2 of the License, or
1901+# (at your option) any later version.
1902+#
1903+# This program is distributed in the hope that it will be useful,
1904+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1905+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1906+# GNU General Public License for more details.
1907+#
1908+# You should have received a copy of the GNU General Public License
1909+# along with this program. If not, see <http://www.gnu.org/licenses/>.
1910+
1911+Special handling for renaming a directory (DIRREN)
1912+----------------------------------------------------------------------
1913+First, let's assume we have a simple usecase.
1914+
1915+- /u = /rw + /ro
1916+- /rw/dirA exists
1917+- /ro/dirA and /ro/dirA/file exist too
1918+- there is no dirB on both branches
1919+- a user issues rename("dirA", "dirB")
1920+
1921+Now, what should aufs behave against this rename(2)?
1922+There are a few possible cases.
1923+
1924+A. returns EROFS.
1925+ since dirA exists on a readonly branch which cannot be renamed.
1926+B. returns EXDEV.
1927+ it is possible to copy-up dirA (only the dir itself), but the child
1928+ entries ("file" in this case) should not be. it must be a bad
1929+ approach to copy-up recursively.
1930+C. returns a success.
1931+ even the branch /ro is readonly, aufs tries renaming it. Obviously it
1932+ is a violation of aufs' policy.
1933+D. construct an extra information which indicates that /ro/dirA should
1934+ be handled as the name of dirB.
1935+ overlayfs has a similar feature called REDIRECT.
1936+
1937+Until now, aufs implements the case B only which returns EXDEV, and
1938+expects the userspace application behaves like mv(1) which tries
1939+issueing rename(2) recursively.
1940+
1941+A new aufs feature called DIRREN is introduced which implements the case
1942+D. There are several "extra information" added.
1943+
1944+1. detailed info per renamed directory
1945+ path: /rw/dirB/$AUFS_WH_DR_INFO_PFX.<lower branch-id>
1946+2. the inode-number list of directories on a branch
1947+ path: /rw/dirB/$AUFS_WH_DR_BRHINO
1948+
1949+The filename of "detailed info per directory" represents the lower
1950+branch, and its format is
1951+- a type of the branch id
1952+ one of these.
1953+ + uuid (not implemented yet)
1954+ + fsid
1955+ + dev
1956+- the inode-number of the branch root dir
1957+
1958+And it contains these info in a single regular file.
1959+- magic number
1960+- branch's inode-number of the logically renamed dir
1961+- the name of the before-renamed dir
1962+
1963+The "detailed info per directory" file is created in aufs rename(2), and
1964+loaded in any lookup.
1965+The info is considered in lookup for the matching case only. Here
1966+"matching" means that the root of branch (in the info filename) is same
1967+to the current looking-up branch. After looking-up the before-renamed
1968+name, the inode-number is compared. And the matched dentry is used.
1969+
1970+The "inode-number list of directories" is a regular file which contains
1971+simply the inode-numbers on the branch. The file is created or updated
1972+in removing the branch, and loaded in adding the branch. Its lifetime is
1973+equal to the branch.
1974+The list is refered in lookup, and when the current target inode is
1975+found in the list, the aufs tries loading the "detailed info per
1976+directory" and get the changed and valid name of the dir.
1977+
1978+Theoretically these "extra informaiton" may be able to be put into XATTR
1979+in the dir inode. But aufs doesn't choose this way because
1980+1. XATTR may not be supported by the branch (or its configuration)
1981+2. XATTR may have its size limit.
1982+3. XATTR may be less easy to convert than a regular file, when the
1983+ format of the info is changed in the future.
1984+At the same time, I agree that the regular file approach is much slower
1985+than XATTR approach. So, in the future, aufs may take the XATTR or other
1986+better approach.
1987+
1988+This DIRREN feature is enabled by aufs configuration, and is activated
1989+by a new mount option.
1990+
1991+For the more complicated case, there is a work with UDBA option, which
1992+is to dected the direct access to the branches (by-passing aufs) and to
1993+maintain the cashes in aufs. Since a single cached aufs dentry may
1994+contains two names, before- and after-rename, the name comparision in
1995+UDBA handler may not work correctly. In this case, the behaviour will be
1996+equivalen to udba=reval case.
076b876e 1997diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt linux/Documentation/filesystems/aufs/design/06fhsm.txt
eca34b5c 1998--- /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt 1970-01-01 01:00:00.000000000 +0100
46016270 1999+++ linux/Documentation/filesystems/aufs/design/06fhsm.txt 2021-02-24 13:33:42.737680181 +0100
076b876e
AM
2000@@ -0,0 +1,120 @@
2001+
d58c55f2 2002+# Copyright (C) 2011-2020 Junjiro R. Okajima
076b876e
AM
2003+#
2004+# This program is free software; you can redistribute it and/or modify
2005+# it under the terms of the GNU General Public License as published by
2006+# the Free Software Foundation; either version 2 of the License, or
2007+# (at your option) any later version.
2008+#
2009+# This program is distributed in the hope that it will be useful,
2010+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2011+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2012+# GNU General Public License for more details.
2013+#
2014+# You should have received a copy of the GNU General Public License
2015+# along with this program; if not, write to the Free Software
2016+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2017+
2018+
2019+File-based Hierarchical Storage Management (FHSM)
2020+----------------------------------------------------------------------
2021+Hierarchical Storage Management (or HSM) is a well-known feature in the
2022+storage world. Aufs provides this feature as file-based with multiple
7e9cd9fe 2023+writable branches, based upon the principle of "Colder, the Lower".
076b876e 2024+Here the word "colder" means that the less used files, and "lower" means
7e9cd9fe 2025+that the position in the order of the stacked branches vertically.
076b876e
AM
2026+These multiple writable branches are prioritized, ie. the topmost one
2027+should be the fastest drive and be used heavily.
2028+
2029+o Characters in aufs FHSM story
2030+- aufs itself and a new branch attribute.
2031+- a new ioctl interface to move-down and to establish a connection with
2032+ the daemon ("move-down" is a converse of "copy-up").
2033+- userspace tool and daemon.
2034+
2035+The userspace daemon establishes a connection with aufs and waits for
2036+the notification. The notified information is very similar to struct
2037+statfs containing the number of consumed blocks and inodes.
2038+When the consumed blocks/inodes of a branch exceeds the user-specified
2039+upper watermark, the daemon activates its move-down process until the
2040+consumed blocks/inodes reaches the user-specified lower watermark.
2041+
2042+The actual move-down is done by aufs based upon the request from
2043+user-space since we need to maintain the inode number and the internal
2044+pointer arrays in aufs.
2045+
2046+Currently aufs FHSM handles the regular files only. Additionally they
2047+must not be hard-linked nor pseudo-linked.
2048+
2049+
2050+o Cowork of aufs and the user-space daemon
2051+ During the userspace daemon established the connection, aufs sends a
2052+ small notification to it whenever aufs writes something into the
2053+ writable branch. But it may cost high since aufs issues statfs(2)
2054+ internally. So user can specify a new option to cache the
2055+ info. Actually the notification is controlled by these factors.
2056+ + the specified cache time.
2057+ + classified as "force" by aufs internally.
2058+ Until the specified time expires, aufs doesn't send the info
2059+ except the forced cases. When aufs decide forcing, the info is always
2060+ notified to userspace.
2061+ For example, the number of free inodes is generally large enough and
2062+ the shortage of it happens rarely. So aufs doesn't force the
2063+ notification when creating a new file, directory and others. This is
2064+ the typical case which aufs doesn't force.
2065+ When aufs writes the actual filedata and the files consumes any of new
2066+ blocks, the aufs forces notifying.
2067+
2068+
2069+o Interfaces in aufs
2070+- New branch attribute.
2071+ + fhsm
2072+ Specifies that the branch is managed by FHSM feature. In other word,
2073+ participant in the FHSM.
2074+ When nofhsm is set to the branch, it will not be the source/target
2075+ branch of the move-down operation. This attribute is set
2076+ independently from coo and moo attributes, and if you want full
2077+ FHSM, you should specify them as well.
2078+- New mount option.
2079+ + fhsm_sec
2080+ Specifies a second to suppress many less important info to be
2081+ notified.
2082+- New ioctl.
2083+ + AUFS_CTL_FHSM_FD
2084+ create a new file descriptor which userspace can read the notification
2085+ (a subset of struct statfs) from aufs.
2086+- Module parameter 'brs'
2087+ It has to be set to 1. Otherwise the new mount option 'fhsm' will not
2088+ be set.
2089+- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
2090+ When there are two or more branches with fhsm attributes,
2091+ /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
2092+ terminates it. As a result of remounting and branch-manipulation, the
2093+ number of branches with fhsm attribute can be one. In this case,
2094+ /sbin/mount.aufs will terminate the user-space daemon.
2095+
2096+
2097+Finally the operation is done as these steps in kernel-space.
2098+- make sure that,
2099+ + no one else is using the file.
2100+ + the file is not hard-linked.
2101+ + the file is not pseudo-linked.
2102+ + the file is a regular file.
2103+ + the parent dir is not opaqued.
2104+- find the target writable branch.
2105+- make sure the file is not whiteout-ed by the upper (than the target)
2106+ branch.
2107+- make the parent dir on the target branch.
2108+- mutex lock the inode on the branch.
2109+- unlink the whiteout on the target branch (if exists).
2110+- lookup and create the whiteout-ed temporary name on the target branch.
2111+- copy the file as the whiteout-ed temporary name on the target branch.
2112+- rename the whiteout-ed temporary name to the original name.
2113+- unlink the file on the source branch.
2114+- maintain the internal pointer array and the external inode number
2115+ table (XINO).
2116+- maintain the timestamps and other attributes of the parent dir and the
2117+ file.
2118+
2119+And of course, in every step, an error may happen. So the operation
2120+should restore the original file state after an error happens.
53392da6 2121diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
eca34b5c 2122--- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt 1970-01-01 01:00:00.000000000 +0100
46016270 2123+++ linux/Documentation/filesystems/aufs/design/06mmap.txt 2021-02-24 13:33:42.737680181 +0100
b912730e 2124@@ -0,0 +1,72 @@
53392da6 2125+
d58c55f2 2126+# Copyright (C) 2005-2020 Junjiro R. Okajima
53392da6
AM
2127+#
2128+# This program is free software; you can redistribute it and/or modify
2129+# it under the terms of the GNU General Public License as published by
2130+# the Free Software Foundation; either version 2 of the License, or
2131+# (at your option) any later version.
2132+#
2133+# This program is distributed in the hope that it will be useful,
2134+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2135+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2136+# GNU General Public License for more details.
2137+#
2138+# You should have received a copy of the GNU General Public License
523b37e3 2139+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2140+
2141+mmap(2) -- File Memory Mapping
2142+----------------------------------------------------------------------
2143+In aufs, the file-mapped pages are handled by a branch fs directly, no
2144+interaction with aufs. It means aufs_mmap() calls the branch fs's
2145+->mmap().
2146+This approach is simple and good, but there is one problem.
7e9cd9fe 2147+Under /proc, several entries show the mmapped files by its path (with
53392da6
AM
2148+device and inode number), and the printed path will be the path on the
2149+branch fs's instead of virtual aufs's.
2150+This is not a problem in most cases, but some utilities lsof(1) (and its
2151+user) may expect the path on aufs.
2152+
2153+To address this issue, aufs adds a new member called vm_prfile in struct
2154+vm_area_struct (and struct vm_region). The original vm_file points to
2155+the file on the branch fs in order to handle everything correctly as
2156+usual. The new vm_prfile points to a virtual file in aufs, and the
2157+show-functions in procfs refers to vm_prfile if it is set.
2158+Also we need to maintain several other places where touching vm_file
2159+such like
2160+- fork()/clone() copies vma and the reference count of vm_file is
2161+ incremented.
2162+- merging vma maintains the ref count too.
2163+
7e9cd9fe 2164+This is not a good approach. It just fakes the printed path. But it
53392da6
AM
2165+leaves all behaviour around f_mapping unchanged. This is surely an
2166+advantage.
2167+Actually aufs had adopted another complicated approach which calls
2168+generic_file_mmap() and handles struct vm_operations_struct. In this
2169+approach, aufs met a hard problem and I could not solve it without
2170+switching the approach.
b912730e
AM
2171+
2172+There may be one more another approach which is
2173+- bind-mount the branch-root onto the aufs-root internally
2174+- grab the new vfsmount (ie. struct mount)
2175+- lazy-umount the branch-root internally
2176+- in open(2) the aufs-file, open the branch-file with the hidden
2177+ vfsmount (instead of the original branch's vfsmount)
2178+- ideally this "bind-mount and lazy-umount" should be done atomically,
2179+ but it may be possible from userspace by the mount helper.
2180+
2181+Adding the internal hidden vfsmount and using it in opening a file, the
2182+file path under /proc will be printed correctly. This approach looks
2183+smarter, but is not possible I am afraid.
2184+- aufs-root may be bind-mount later. when it happens, another hidden
2185+ vfsmount will be required.
2186+- it is hard to get the chance to bind-mount and lazy-umount
2187+ + in kernel-space, FS can have vfsmount in open(2) via
2188+ file->f_path, and aufs can know its vfsmount. But several locks are
2189+ already acquired, and if aufs tries to bind-mount and lazy-umount
2190+ here, then it may cause a deadlock.
2191+ + in user-space, bind-mount doesn't invoke the mount helper.
2192+- since /proc shows dev and ino, aufs has to give vma these info. it
2193+ means a new member vm_prinode will be necessary. this is essentially
2194+ equivalent to vm_prfile described above.
2195+
2196+I have to give up this "looks-smater" approach.
c1595e42 2197diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt linux/Documentation/filesystems/aufs/design/06xattr.txt
eca34b5c 2198--- /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt 1970-01-01 01:00:00.000000000 +0100
46016270 2199+++ linux/Documentation/filesystems/aufs/design/06xattr.txt 2021-02-24 13:33:42.737680181 +0100
c1595e42
JR
2200@@ -0,0 +1,96 @@
2201+
d58c55f2 2202+# Copyright (C) 2014-2020 Junjiro R. Okajima
c1595e42
JR
2203+#
2204+# This program is free software; you can redistribute it and/or modify
2205+# it under the terms of the GNU General Public License as published by
2206+# the Free Software Foundation; either version 2 of the License, or
2207+# (at your option) any later version.
2208+#
2209+# This program is distributed in the hope that it will be useful,
2210+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2211+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2212+# GNU General Public License for more details.
2213+#
2214+# You should have received a copy of the GNU General Public License
2215+# along with this program; if not, write to the Free Software
2216+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2217+
2218+
2219+Listing XATTR/EA and getting the value
2220+----------------------------------------------------------------------
2221+For the inode standard attributes (owner, group, timestamps, etc.), aufs
2222+shows the values from the topmost existing file. This behaviour is good
7e9cd9fe 2223+for the non-dir entries since the bahaviour exactly matches the shown
c1595e42
JR
2224+information. But for the directories, aufs considers all the same named
2225+entries on the lower branches. Which means, if one of the lower entry
2226+rejects readdir call, then aufs returns an error even if the topmost
2227+entry allows it. This behaviour is necessary to respect the branch fs's
2228+security, but can make users confused since the user-visible standard
2229+attributes don't match the behaviour.
2230+To address this issue, aufs has a mount option called dirperm1 which
2231+checks the permission for the topmost entry only, and ignores the lower
2232+entry's permission.
2233+
2234+A similar issue can happen around XATTR.
2235+getxattr(2) and listxattr(2) families behave as if dirperm1 option is
7e9cd9fe
AM
2236+always set. Otherwise these very unpleasant situation would happen.
2237+- listxattr(2) may return the duplicated entries.
c1595e42
JR
2238+- users may not be able to remove or reset the XATTR forever,
2239+
2240+
2241+XATTR/EA support in the internal (copy,move)-(up,down)
2242+----------------------------------------------------------------------
7e9cd9fe 2243+Generally the extended attributes of inode are categorized as these.
c1595e42
JR
2244+- "security" for LSM and capability.
2245+- "system" for posix ACL, 'acl' mount option is required for the branch
2246+ fs generally.
2247+- "trusted" for userspace, CAP_SYS_ADMIN is required.
2248+- "user" for userspace, 'user_xattr' mount option is required for the
2249+ branch fs generally.
2250+
2251+Moreover there are some other categories. Aufs handles these rather
2252+unpopular categories as the ordinary ones, ie. there is no special
2253+condition nor exception.
2254+
2255+In copy-up, the support for XATTR on the dst branch may differ from the
2256+src branch. In this case, the copy-up operation will get an error and
7e9cd9fe
AM
2257+the original user operation which triggered the copy-up will fail. It
2258+can happen that even all copy-up will fail.
c1595e42
JR
2259+When both of src and dst branches support XATTR and if an error occurs
2260+during copying XATTR, then the copy-up should fail obviously. That is a
2261+good reason and aufs should return an error to userspace. But when only
7e9cd9fe 2262+the src branch support that XATTR, aufs should not return an error.
c1595e42
JR
2263+For example, the src branch supports ACL but the dst branch doesn't
2264+because the dst branch may natively un-support it or temporary
2265+un-support it due to "noacl" mount option. Of course, the dst branch fs
2266+may NOT return an error even if the XATTR is not supported. It is
2267+totally up to the branch fs.
2268+
2269+Anyway when the aufs internal copy-up gets an error from the dst branch
2270+fs, then aufs tries removing the just copied entry and returns the error
2271+to the userspace. The worst case of this situation will be all copy-up
2272+will fail.
2273+
2274+For the copy-up operation, there two basic approaches.
2275+- copy the specified XATTR only (by category above), and return the
7e9cd9fe 2276+ error unconditionally if it happens.
c1595e42
JR
2277+- copy all XATTR, and ignore the error on the specified category only.
2278+
2279+In order to support XATTR and to implement the correct behaviour, aufs
7e9cd9fe
AM
2280+chooses the latter approach and introduces some new branch attributes,
2281+"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
c1595e42 2282+They correspond to the XATTR namespaces (see above). Additionally, to be
7e9cd9fe
AM
2283+convenient, "icex" is also provided which means all "icex*" attributes
2284+are set (here the word "icex" stands for "ignore copy-error on XATTR").
c1595e42
JR
2285+
2286+The meaning of these attributes is to ignore the error from setting
2287+XATTR on that branch.
2288+Note that aufs tries copying all XATTR unconditionally, and ignores the
2289+error from the dst branch according to the specified attributes.
2290+
2291+Some XATTR may have its default value. The default value may come from
2292+the parent dir or the environment. If the default value is set at the
2293+file creating-time, it will be overwritten by copy-up.
2294+Some contradiction may happen I am afraid.
2295+Do we need another attribute to stop copying XATTR? I am unsure. For
2296+now, aufs implements the branch attributes to ignore the error.
53392da6 2297diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
eca34b5c 2298--- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
46016270 2299+++ linux/Documentation/filesystems/aufs/design/07export.txt 2021-02-24 13:33:42.737680181 +0100
523b37e3 2300@@ -0,0 +1,58 @@
53392da6 2301+
d58c55f2 2302+# Copyright (C) 2005-2020 Junjiro R. Okajima
53392da6
AM
2303+#
2304+# This program is free software; you can redistribute it and/or modify
2305+# it under the terms of the GNU General Public License as published by
2306+# the Free Software Foundation; either version 2 of the License, or
2307+# (at your option) any later version.
2308+#
2309+# This program is distributed in the hope that it will be useful,
2310+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2311+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2312+# GNU General Public License for more details.
2313+#
2314+# You should have received a copy of the GNU General Public License
523b37e3 2315+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2316+
2317+Export Aufs via NFS
2318+----------------------------------------------------------------------
2319+Here is an approach.
2320+- like xino/xib, add a new file 'xigen' which stores aufs inode
2321+ generation.
2322+- iget_locked(): initialize aufs inode generation for a new inode, and
2323+ store it in xigen file.
2324+- destroy_inode(): increment aufs inode generation and store it in xigen
2325+ file. it is necessary even if it is not unlinked, because any data of
2326+ inode may be changed by UDBA.
2327+- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2328+ build file handle by
2329+ + branch id (4 bytes)
2330+ + superblock generation (4 bytes)
2331+ + inode number (4 or 8 bytes)
2332+ + parent dir inode number (4 or 8 bytes)
2333+ + inode generation (4 bytes))
2334+ + return value of exportfs_encode_fh() for the parent on a branch (4
2335+ bytes)
2336+ + file handle for a branch (by exportfs_encode_fh())
2337+- fh_to_dentry():
2338+ + find the index of a branch from its id in handle, and check it is
2339+ still exist in aufs.
2340+ + 1st level: get the inode number from handle and search it in cache.
7e9cd9fe
AM
2341+ + 2nd level: if not found in cache, get the parent inode number from
2342+ the handle and search it in cache. and then open the found parent
2343+ dir, find the matching inode number by vfs_readdir() and get its
2344+ name, and call lookup_one_len() for the target dentry.
53392da6
AM
2345+ + 3rd level: if the parent dir is not cached, call
2346+ exportfs_decode_fh() for a branch and get the parent on a branch,
2347+ build a pathname of it, convert it a pathname in aufs, call
2348+ path_lookup(). now aufs gets a parent dir dentry, then handle it as
2349+ the 2nd level.
2350+ + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2351+ for every branch, but not itself. to get this, (currently) aufs
2352+ searches in current->nsproxy->mnt_ns list. it may not be a good
2353+ idea, but I didn't get other approach.
2354+ + test the generation of the gotten inode.
2355+- every inode operation: they may get EBUSY due to UDBA. in this case,
2356+ convert it into ESTALE for NFSD.
2357+- readdir(): call lockdep_on/off() because filldir in NFSD calls
2358+ lookup_one_len(), vfs_getattr(), encode_fh() and others.
2359diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
eca34b5c 2360--- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt 1970-01-01 01:00:00.000000000 +0100
46016270 2361+++ linux/Documentation/filesystems/aufs/design/08shwh.txt 2021-02-24 13:33:42.737680181 +0100
523b37e3 2362@@ -0,0 +1,52 @@
53392da6 2363+
d58c55f2 2364+# Copyright (C) 2005-2020 Junjiro R. Okajima
53392da6
AM
2365+#
2366+# This program is free software; you can redistribute it and/or modify
2367+# it under the terms of the GNU General Public License as published by
2368+# the Free Software Foundation; either version 2 of the License, or
2369+# (at your option) any later version.
2370+#
2371+# This program is distributed in the hope that it will be useful,
2372+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2373+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2374+# GNU General Public License for more details.
2375+#
2376+# You should have received a copy of the GNU General Public License
523b37e3 2377+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2378+
2379+Show Whiteout Mode (shwh)
2380+----------------------------------------------------------------------
2381+Generally aufs hides the name of whiteouts. But in some cases, to show
2382+them is very useful for users. For instance, creating a new middle layer
2383+(branch) by merging existing layers.
2384+
2385+(borrowing aufs1 HOW-TO from a user, Michael Towers)
2386+When you have three branches,
2387+- Bottom: 'system', squashfs (underlying base system), read-only
2388+- Middle: 'mods', squashfs, read-only
2389+- Top: 'overlay', ram (tmpfs), read-write
2390+
2391+The top layer is loaded at boot time and saved at shutdown, to preserve
2392+the changes made to the system during the session.
2393+When larger changes have been made, or smaller changes have accumulated,
2394+the size of the saved top layer data grows. At this point, it would be
2395+nice to be able to merge the two overlay branches ('mods' and 'overlay')
2396+and rewrite the 'mods' squashfs, clearing the top layer and thus
2397+restoring save and load speed.
2398+
2399+This merging is simplified by the use of another aufs mount, of just the
2400+two overlay branches using the 'shwh' option.
2401+# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2402+ aufs /livesys/merge_union
2403+
2404+A merged view of these two branches is then available at
2405+/livesys/merge_union, and the new feature is that the whiteouts are
2406+visible!
2407+Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2408+writing to all branches. Also the default mode for all branches is 'ro'.
2409+It is now possible to save the combined contents of the two overlay
2410+branches to a new squashfs, e.g.:
2411+# mksquashfs /livesys/merge_union /path/to/newmods.squash
2412+
2413+This new squashfs archive can be stored on the boot device and the
2414+initramfs will use it to replace the old one at the next boot.
2415diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
eca34b5c 2416--- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt 1970-01-01 01:00:00.000000000 +0100
46016270 2417+++ linux/Documentation/filesystems/aufs/design/10dynop.txt 2021-02-24 13:33:42.737680181 +0100
7e9cd9fe 2418@@ -0,0 +1,47 @@
53392da6 2419+
d58c55f2 2420+# Copyright (C) 2010-2020 Junjiro R. Okajima
53392da6
AM
2421+#
2422+# This program is free software; you can redistribute it and/or modify
2423+# it under the terms of the GNU General Public License as published by
2424+# the Free Software Foundation; either version 2 of the License, or
2425+# (at your option) any later version.
2426+#
2427+# This program is distributed in the hope that it will be useful,
2428+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2429+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2430+# GNU General Public License for more details.
2431+#
2432+# You should have received a copy of the GNU General Public License
523b37e3 2433+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2434+
2435+Dynamically customizable FS operations
2436+----------------------------------------------------------------------
2437+Generally FS operations (struct inode_operations, struct
2438+address_space_operations, struct file_operations, etc.) are defined as
2439+"static const", but it never means that FS have only one set of
2440+operation. Some FS have multiple sets of them. For instance, ext2 has
2441+three sets, one for XIP, for NOBH, and for normal.
2442+Since aufs overrides and redirects these operations, sometimes aufs has
7e9cd9fe 2443+to change its behaviour according to the branch FS type. More importantly
53392da6
AM
2444+VFS acts differently if a function (member in the struct) is set or
2445+not. It means aufs should have several sets of operations and select one
2446+among them according to the branch FS definition.
2447+
7e9cd9fe 2448+In order to solve this problem and not to affect the behaviour of VFS,
53392da6 2449+aufs defines these operations dynamically. For instance, aufs defines
7e9cd9fe
AM
2450+dummy direct_IO function for struct address_space_operations, but it may
2451+not be set to the address_space_operations actually. When the branch FS
2452+doesn't have it, aufs doesn't set it to its address_space_operations
2453+while the function definition itself is still alive. So the behaviour
2454+itself will not change, and it will return an error when direct_IO is
2455+not set.
53392da6
AM
2456+
2457+The lifetime of these dynamically generated operation object is
2458+maintained by aufs branch object. When the branch is removed from aufs,
2459+the reference counter of the object is decremented. When it reaches
2460+zero, the dynamically generated operation object will be freed.
2461+
7e9cd9fe
AM
2462+This approach is designed to support AIO (io_submit), Direct I/O and
2463+XIP (DAX) mainly.
2464+Currently this approach is applied to address_space_operations for
2465+regular files only.
53392da6 2466diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
eca34b5c 2467--- /usr/share/empty/Documentation/filesystems/aufs/README 1970-01-01 01:00:00.000000000 +0100
46016270
AM
2468+++ linux/Documentation/filesystems/aufs/README 2021-06-30 21:35:11.393873211 +0200
2469@@ -0,0 +1,396 @@
53392da6 2470+
fbc438ed 2471+Aufs5 -- advanced multi layered unification filesystem version 5.x
53392da6
AM
2472+http://aufs.sf.net
2473+Junjiro R. Okajima
2474+
2475+
2476+0. Introduction
2477+----------------------------------------
2478+In the early days, aufs was entirely re-designed and re-implemented
7e9cd9fe 2479+Unionfs Version 1.x series. Adding many original ideas, approaches,
fbc438ed 2480+improvements and implementations, it became totally different from
53392da6 2481+Unionfs while keeping the basic features.
fbc438ed 2482+Later, Unionfs Version 2.x series began taking some of the same
53392da6 2483+approaches to aufs1's.
fbc438ed 2484+Unionfs was being developed by Professor Erez Zadok at Stony Brook
53392da6
AM
2485+University and his team.
2486+
fbc438ed
JR
2487+Aufs5 supports linux-v5.0 and later, If you want older kernel version
2488+support,
2489+- for linux-v4.x series, try aufs4-linux.git or aufs4-standalone.git
2490+- for linux-v3.x series, try aufs3-linux.git or aufs3-standalone.git
2491+- for linux-v2.6.16 and later, try aufs2-2.6.git, aufs2-standalone.git
2492+ or aufs1 from CVS on SourceForge.
53392da6
AM
2493+
2494+Note: it becomes clear that "Aufs was rejected. Let's give it up."
38d290e6
JR
2495+ According to Christoph Hellwig, linux rejects all union-type
2496+ filesystems but UnionMount.
53392da6
AM
2497+<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
2498+
38d290e6
JR
2499+PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
2500+ UnionMount, and he pointed out an issue around a directory mutex
2501+ lock and aufs addressed it. But it is still unsure whether aufs will
2502+ be merged (or any other union solution).
076b876e 2503+<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
38d290e6 2504+
53392da6
AM
2505+
2506+1. Features
2507+----------------------------------------
2508+- unite several directories into a single virtual filesystem. The member
2509+ directory is called as a branch.
2510+- you can specify the permission flags to the branch, which are 'readonly',
2511+ 'readwrite' and 'whiteout-able.'
2512+- by upper writable branch, internal copyup and whiteout, files/dirs on
2513+ readonly branch are modifiable logically.
2514+- dynamic branch manipulation, add, del.
2515+- etc...
2516+
7e9cd9fe
AM
2517+Also there are many enhancements in aufs, such as:
2518+- test only the highest one for the directory permission (dirperm1)
2519+- copyup on open (coo=)
2520+- 'move' policy for copy-up between two writable branches, after
2521+ checking free space.
2522+- xattr, acl
53392da6
AM
2523+- readdir(3) in userspace.
2524+- keep inode number by external inode number table
2525+- keep the timestamps of file/dir in internal copyup operation
2526+- seekable directory, supporting NFS readdir.
2527+- whiteout is hardlinked in order to reduce the consumption of inodes
2528+ on branch
2529+- do not copyup, nor create a whiteout when it is unnecessary
2530+- revert a single systemcall when an error occurs in aufs
2531+- remount interface instead of ioctl
2532+- maintain /etc/mtab by an external command, /sbin/mount.aufs.
2533+- loopback mounted filesystem as a branch
2534+- kernel thread for removing the dir who has a plenty of whiteouts
2535+- support copyup sparse file (a file which has a 'hole' in it)
2536+- default permission flags for branches
2537+- selectable permission flags for ro branch, whether whiteout can
2538+ exist or not
2539+- export via NFS.
2540+- support <sysfs>/fs/aufs and <debugfs>/aufs.
2541+- support multiple writable branches, some policies to select one
2542+ among multiple writable branches.
2543+- a new semantics for link(2) and rename(2) to support multiple
2544+ writable branches.
2545+- no glibc changes are required.
2546+- pseudo hardlink (hardlink over branches)
2547+- allow a direct access manually to a file on branch, e.g. bypassing aufs.
2548+ including NFS or remote filesystem branch.
2549+- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
2550+- and more...
2551+
fbc438ed 2552+Currently these features are dropped temporary from aufs5.
53392da6 2553+See design/08plan.txt in detail.
53392da6
AM
2554+- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
2555+ (robr)
2556+- statistics of aufs thread (/sys/fs/aufs/stat)
53392da6
AM
2557+
2558+Features or just an idea in the future (see also design/*.txt),
2559+- reorder the branch index without del/re-add.
2560+- permanent xino files for NFSD
2561+- an option for refreshing the opened files after add/del branches
53392da6
AM
2562+- light version, without branch manipulation. (unnecessary?)
2563+- copyup in userspace
2564+- inotify in userspace
2565+- readv/writev
53392da6
AM
2566+
2567+
2568+2. Download
2569+----------------------------------------
fbc438ed
JR
2570+There are three GIT trees for aufs5, aufs5-linux.git,
2571+aufs5-standalone.git, and aufs-util.git. Note that there is no "5" in
1e00d052 2572+"aufs-util.git."
fbc438ed
JR
2573+While the aufs-util is always necessary, you need either of aufs5-linux
2574+or aufs5-standalone.
1e00d052 2575+
fbc438ed 2576+The aufs5-linux tree includes the whole linux mainline GIT tree,
1e00d052
AM
2577+git://git.kernel.org/.../torvalds/linux.git.
2578+And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
fbc438ed 2579+build aufs5 as an external kernel module.
2000de60 2580+Several extra patches are not included in this tree. Only
fbc438ed 2581+aufs5-standalone tree contains them. They are described in the later
2000de60 2582+section "Configuration and Compilation."
1e00d052 2583+
fbc438ed 2584+On the other hand, the aufs5-standalone tree has only aufs source files
53392da6 2585+and necessary patches, and you can select CONFIG_AUFS_FS=m.
2000de60 2586+But you need to apply all aufs patches manually.
53392da6 2587+
fbc438ed
JR
2588+You will find GIT branches whose name is in form of "aufs5.x" where "x"
2589+represents the linux kernel version, "linux-5.x". For instance,
2590+"aufs5.0" is for linux-5.0. For latest "linux-5.x-rcN", use
2591+"aufs5.x-rcN" branch.
1e00d052 2592+
fbc438ed 2593+o aufs5-linux tree
1e00d052 2594+$ git clone --reference /your/linux/git/tree \
fbc438ed 2595+ git://github.com/sfjro/aufs5-linux.git aufs5-linux.git
1e00d052 2596+- if you don't have linux GIT tree, then remove "--reference ..."
fbc438ed
JR
2597+$ cd aufs5-linux.git
2598+$ git checkout origin/aufs5.0
53392da6 2599+
2000de60
JR
2600+Or You may want to directly git-pull aufs into your linux GIT tree, and
2601+leave the patch-work to GIT.
2602+$ cd /your/linux/git/tree
fbc438ed
JR
2603+$ git remote add aufs5 git://github.com/sfjro/aufs5-linux.git
2604+$ git fetch aufs5
2605+$ git checkout -b my5.0 v5.0
5527c038 2606+$ (add your local change...)
fbc438ed
JR
2607+$ git pull aufs5 aufs5.0
2608+- now you have v5.0 + your_changes + aufs5.0 in you my5.0 branch.
2000de60 2609+- you may need to solve some conflicts between your_changes and
fbc438ed
JR
2610+ aufs5.0. in this case, git-rerere is recommended so that you can
2611+ solve the similar conflicts automatically when you upgrade to 5.1 or
2000de60
JR
2612+ later in the future.
2613+
fbc438ed
JR
2614+o aufs5-standalone tree
2615+$ git clone git://github.com/sfjro/aufs5-standalone.git aufs5-standalone.git
2616+$ cd aufs5-standalone.git
2617+$ git checkout origin/aufs5.0
53392da6
AM
2618+
2619+o aufs-util tree
5527c038
JR
2620+$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
2621+- note that the public aufs-util.git is on SourceForge instead of
2622+ GitHUB.
53392da6 2623+$ cd aufs-util.git
fbc438ed 2624+$ git checkout origin/aufs5.0
53392da6 2625+
fbc438ed
JR
2626+Note: The 5.x-rcN branch is to be used with `rc' kernel versions ONLY.
2627+The minor version number, 'x' in '5.x', of aufs may not always
9dbd164d
AM
2628+follow the minor version number of the kernel.
2629+Because changes in the kernel that cause the use of a new
2630+minor version number do not always require changes to aufs-util.
2631+
2632+Since aufs-util has its own minor version number, you may not be
2633+able to find a GIT branch in aufs-util for your kernel's
2634+exact minor version number.
2635+In this case, you should git-checkout the branch for the
53392da6 2636+nearest lower number.
9dbd164d
AM
2637+
2638+For (an unreleased) example:
fbc438ed
JR
2639+If you are using "linux-5.10" and the "aufs5.10" branch
2640+does not exist in aufs-util repository, then "aufs5.9", "aufs5.8"
9dbd164d
AM
2641+or something numerically smaller is the branch for your kernel.
2642+
53392da6
AM
2643+Also you can view all branches by
2644+ $ git branch -a
2645+
2646+
2647+3. Configuration and Compilation
2648+----------------------------------------
2649+Make sure you have git-checkout'ed the correct branch.
2650+
fbc438ed 2651+For aufs5-linux tree,
c06a8ce3 2652+- enable CONFIG_AUFS_FS.
1e00d052 2653+- set other aufs configurations if necessary.
46016270
AM
2654+- for aufs5.13 and later
2655+ Because aufs is not only an ordinary filesystem (callee of VFS), but
2656+ also a caller of VFS functions for branch filesystems, subclassing of
2657+ the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
2658+ feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
2659+ need to customize some LOCKDEP numbers. Here are what I use on my
2660+ test environment.
2661+ CONFIG_LOCKDEP_BITS=21
2662+ CONFIG_LOCKDEP_CHAINS_BITS=21
2663+ CONFIG_LOCKDEP_STACK_TRACE_BITS=24
1e00d052 2664+
fbc438ed 2665+For aufs5-standalone tree,
53392da6
AM
2666+There are several ways to build.
2667+
2668+1.
fbc438ed
JR
2669+- apply ./aufs5-kbuild.patch to your kernel source files.
2670+- apply ./aufs5-base.patch too.
2671+- apply ./aufs5-mmap.patch too.
2672+- apply ./aufs5-standalone.patch too, if you have a plan to set
2673+ CONFIG_AUFS_FS=m. otherwise you don't need ./aufs5-standalone.patch.
537831f9
AM
2674+- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2675+ kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
c06a8ce3 2676+- enable CONFIG_AUFS_FS, you can select either
53392da6
AM
2677+ =m or =y.
2678+- and build your kernel as usual.
2679+- install the built kernel.
7eafdf33
AM
2680+- install the header files too by "make headers_install" to the
2681+ directory where you specify. By default, it is $PWD/usr.
b4510431 2682+ "make help" shows a brief note for headers_install.
53392da6
AM
2683+- and reboot your system.
2684+
2685+2.
2686+- module only (CONFIG_AUFS_FS=m).
fbc438ed
JR
2687+- apply ./aufs5-base.patch to your kernel source files.
2688+- apply ./aufs5-mmap.patch too.
2689+- apply ./aufs5-standalone.patch too.
53392da6
AM
2690+- build your kernel, don't forget "make headers_install", and reboot.
2691+- edit ./config.mk and set other aufs configurations if necessary.
b4510431 2692+ Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
53392da6
AM
2693+ every aufs configurations.
2694+- build the module by simple "make".
2695+- you can specify ${KDIR} make variable which points to your kernel
2696+ source tree.
2697+- install the files
2698+ + run "make install" to install the aufs module, or copy the built
b4510431
AM
2699+ $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2700+ + run "make install_headers" (instead of headers_install) to install
2701+ the modified aufs header file (you can specify DESTDIR which is
2702+ available in aufs standalone version's Makefile only), or copy
2703+ $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2704+ you like manually. By default, the target directory is $PWD/usr.
fbc438ed 2705+- no need to apply aufs5-kbuild.patch, nor copying source files to your
53392da6
AM
2706+ kernel source tree.
2707+
b4510431 2708+Note: The header file aufs_type.h is necessary to build aufs-util
53392da6
AM
2709+ as well as "make headers_install" in the kernel source tree.
2710+ headers_install is subject to be forgotten, but it is essentially
2711+ necessary, not only for building aufs-util.
2712+ You may not meet problems without headers_install in some older
2713+ version though.
2714+
2715+And then,
2716+- read README in aufs-util, build and install it
9dbd164d
AM
2717+- note that your distribution may contain an obsoleted version of
2718+ aufs_type.h in /usr/include/linux or something. When you build aufs
2719+ utilities, make sure that your compiler refers the correct aufs header
2720+ file which is built by "make headers_install."
53392da6
AM
2721+- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2722+ then run "make install_ulib" too. And refer to the aufs manual in
2723+ detail.
2724+
fbc438ed 2725+There several other patches in aufs5-standalone.git. They are all
38d290e6 2726+optional. When you meet some problems, they will help you.
fbc438ed 2727+- aufs5-loopback.patch
38d290e6
JR
2728+ Supports a nested loopback mount in a branch-fs. This patch is
2729+ unnecessary until aufs produces a message like "you may want to try
2730+ another patch for loopback file".
2731+- vfs-ino.patch
2732+ Modifies a system global kernel internal function get_next_ino() in
2733+ order to stop assigning 0 for an inode-number. Not directly related to
2734+ aufs, but recommended generally.
2735+- tmpfs-idr.patch
2736+ Keeps the tmpfs inode number as the lowest value. Effective to reduce
2737+ the size of aufs XINO files for tmpfs branch. Also it prevents the
2738+ duplication of inode number, which is important for backup tools and
2739+ other utilities. When you find aufs XINO files for tmpfs branch
2740+ growing too much, try this patch.
2741+
53392da6
AM
2742+
2743+4. Usage
2744+----------------------------------------
2745+At first, make sure aufs-util are installed, and please read the aufs
2746+manual, aufs.5 in aufs-util.git tree.
2747+$ man -l aufs.5
2748+
2749+And then,
2750+$ mkdir /tmp/rw /tmp/aufs
2751+# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2752+
2753+Here is another example. The result is equivalent.
2754+# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2755+ Or
2756+# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2757+# mount -o remount,append:${HOME} /tmp/aufs
2758+
2759+Then, you can see whole tree of your home dir through /tmp/aufs. If
2760+you modify a file under /tmp/aufs, the one on your home directory is
2761+not affected, instead the same named file will be newly created under
2762+/tmp/rw. And all of your modification to a file will be applied to
2763+the one under /tmp/rw. This is called the file based Copy on Write
2764+(COW) method.
2765+Aufs mount options are described in aufs.5.
2766+If you run chroot or something and make your aufs as a root directory,
2767+then you need to customize the shutdown script. See the aufs manual in
2768+detail.
2769+
2770+Additionally, there are some sample usages of aufs which are a
2771+diskless system with network booting, and LiveCD over NFS.
2772+See sample dir in CVS tree on SourceForge.
2773+
2774+
2775+5. Contact
2776+----------------------------------------
2777+When you have any problems or strange behaviour in aufs, please let me
2778+know with:
2779+- /proc/mounts (instead of the output of mount(8))
2780+- /sys/module/aufs/*
2781+- /sys/fs/aufs/* (if you have them)
2782+- /debug/aufs/* (if you have them)
2783+- linux kernel version
2784+ if your kernel is not plain, for example modified by distributor,
2785+ the url where i can download its source is necessary too.
2786+- aufs version which was printed at loading the module or booting the
2787+ system, instead of the date you downloaded.
2788+- configuration (define/undefine CONFIG_AUFS_xxx)
2789+- kernel configuration or /proc/config.gz (if you have it)
43982f53 2790+- LSM (linux security module, if you are using)
53392da6
AM
2791+- behaviour which you think to be incorrect
2792+- actual operation, reproducible one is better
2793+- mailto: aufs-users at lists.sourceforge.net
2794+
2795+Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
2796+and Feature Requests) on SourceForge. Please join and write to
2797+aufs-users ML.
2798+
2799+
2800+6. Acknowledgements
2801+----------------------------------------
2802+Thanks to everyone who have tried and are using aufs, whoever
2803+have reported a bug or any feedback.
2804+
2805+Especially donators:
2806+Tomas Matejicek(slax.org) made a donation (much more than once).
2807+ Since Apr 2010, Tomas M (the author of Slax and Linux Live
2808+ scripts) is making "doubling" donations.
2809+ Unfortunately I cannot list all of the donators, but I really
b4510431 2810+ appreciate.
53392da6
AM
2811+ It ends Aug 2010, but the ordinary donation URL is still available.
2812+ <http://sourceforge.net/donate/index.php?group_id=167503>
2813+Dai Itasaka made a donation (2007/8).
2814+Chuck Smith made a donation (2008/4, 10 and 12).
2815+Henk Schoneveld made a donation (2008/9).
2816+Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
2817+Francois Dupoux made a donation (2008/11).
2818+Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
2819+ aufs2 GIT tree (2009/2).
2820+William Grant made a donation (2009/3).
2821+Patrick Lane made a donation (2009/4).
2822+The Mail Archive (mail-archive.com) made donations (2009/5).
2823+Nippy Networks (Ed Wildgoose) made a donation (2009/7).
2824+New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
2825+Pavel Pronskiy made a donation (2011/2).
2826+Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
2827+ Networks (Ed Wildgoose) made a donation for hardware (2011/3).
537831f9
AM
2828+Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
2829+11).
1e00d052 2830+Sam Liddicott made a donation (2011/9).
86dc4139
AM
2831+Era Scarecrow made a donation (2013/4).
2832+Bor Ratajc made a donation (2013/4).
2833+Alessandro Gorreta made a donation (2013/4).
2834+POIRETTE Marc made a donation (2013/4).
2835+Alessandro Gorreta made a donation (2013/4).
2836+lauri kasvandik made a donation (2013/5).
392086de 2837+"pemasu from Finland" made a donation (2013/7).
523b37e3
AM
2838+The Parted Magic Project made a donation (2013/9 and 11).
2839+Pavel Barta made a donation (2013/10).
38d290e6 2840+Nikolay Pertsev made a donation (2014/5).
c2c0f25c 2841+James B made a donation (2014/7 and 2015/7).
076b876e 2842+Stefano Di Biase made a donation (2014/8).
2000de60 2843+Daniel Epellei made a donation (2015/1).
c4adf169 2844+OmegaPhil made a donation (2016/1, 2018/4).
5afbbe0d 2845+Tomasz Szewczyk made a donation (2016/4).
f2c43d5f 2846+James Burry made a donation (2016/12).
acd2b654 2847+Carsten Rose made a donation (2018/9).
9f237c51 2848+Porteus Kiosk made a donation (2018/10).
53392da6
AM
2849+
2850+Thank you very much.
2851+Donations are always, including future donations, very important and
2852+helpful for me to keep on developing aufs.
2853+
2854+
2855+7.
2856+----------------------------------------
2857+If you are an experienced user, no explanation is needed. Aufs is
2858+just a linux filesystem.
2859+
2860+
2861+Enjoy!
2862+
2863+# Local variables: ;
2864+# mode: text;
2865+# End: ;
7f207e10 2866diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
eca34b5c 2867--- /usr/share/empty/fs/aufs/aufs.h 1970-01-01 01:00:00.000000000 +0100
46016270 2868+++ linux/fs/aufs/aufs.h 2021-02-24 13:33:42.741013619 +0100
acd2b654 2869@@ -0,0 +1,62 @@
062440b3 2870+/* SPDX-License-Identifier: GPL-2.0 */
7f207e10 2871+/*
d58c55f2 2872+ * Copyright (C) 2005-2020 Junjiro R. Okajima
7f207e10
AM
2873+ *
2874+ * This program, aufs is free software; you can redistribute it and/or modify
2875+ * it under the terms of the GNU General Public License as published by
2876+ * the Free Software Foundation; either version 2 of the License, or
2877+ * (at your option) any later version.
2878+ *
2879+ * This program is distributed in the hope that it will be useful,
2880+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
2881+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2882+ * GNU General Public License for more details.
2883+ *
2884+ * You should have received a copy of the GNU General Public License
523b37e3 2885+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7f207e10
AM
2886+ */
2887+
2888+/*
2889+ * all header files
2890+ */
2891+
2892+#ifndef __AUFS_H__
2893+#define __AUFS_H__
2894+
2895+#ifdef __KERNEL__
2896+
2897+#define AuStub(type, name, body, ...) \
2898+ static inline type name(__VA_ARGS__) { body; }
2899+
2900+#define AuStubVoid(name, ...) \
2901+ AuStub(void, name, , __VA_ARGS__)
2902+#define AuStubInt0(name, ...) \
2903+ AuStub(int, name, return 0, __VA_ARGS__)
2904+
2905+#include "debug.h"
2906+
2907+#include "branch.h"
2908+#include "cpup.h"
2909+#include "dcsub.h"
2910+#include "dbgaufs.h"
2911+#include "dentry.h"
2912+#include "dir.h"
8b6a4947 2913+#include "dirren.h"
7f207e10
AM
2914+#include "dynop.h"
2915+#include "file.h"
2916+#include "fstype.h"
8b6a4947 2917+#include "hbl.h"
7f207e10 2918+#include "inode.h"
acd2b654 2919+#include "lcnt.h"
7f207e10
AM
2920+#include "loop.h"
2921+#include "module.h"
7f207e10
AM
2922+#include "opts.h"
2923+#include "rwsem.h"
7f207e10
AM
2924+#include "super.h"
2925+#include "sysaufs.h"
2926+#include "vfsub.h"
2927+#include "whout.h"
2928+#include "wkq.h"
2929+
2930+#endif /* __KERNEL__ */
2931+#endif /* __AUFS_H__ */
2932diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
eca34b5c 2933--- /usr/share/empty/fs/aufs/branch.c 1970-01-01 01:00:00.000000000 +0100
46016270 2934+++ linux/fs/aufs/branch.c 2021-02-24 13:33:42.741013619 +0100
42b5c33a 2935@@ -0,0 +1,1427 @@
cd7a4cd9 2936+// SPDX-License-Identifier: GPL-2.0
7f207e10 2937+/*
d58c55f2 2938+ * Copyright (C) 2005-2020 Junjiro R. Okajima
7f207e10
AM
2939+ *
2940+ * This program, aufs is free software; you can redistribute it and/or modify
2941+ * it under the terms of the GNU General Public License as published by
2942+ * the Free Software Foundation; either version 2 of the License, or
2943+ * (at your option) any later version.
2944+ *
2945+ * This program is distributed in the hope that it will be useful,
2946+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
2947+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2948+ * GNU General Public License for more details.
2949+ *
2950+ * You should have received a copy of the GNU General Public License
523b37e3 2951+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7f207e10
AM
2952+ */
2953+
2954+/*
2955+ * branch management
2956+ */
2957+
027c5e7a 2958+#include <linux/compat.h>
7f207e10
AM
2959+#include <linux/statfs.h>
2960+#include "aufs.h"
2961+
2962+/*
2963+ * free a single branch
1facf9fc 2964+ */
2965+static void au_br_do_free(struct au_branch *br)
2966+{
2967+ int i;
2968+ struct au_wbr *wbr;
4a4d8108 2969+ struct au_dykey **key;
1facf9fc 2970+
027c5e7a 2971+ au_hnotify_fin_br(br);
8b6a4947
AM
2972+ /* always, regardless the mount option */
2973+ au_dr_hino_free(&br->br_dirren);
062440b3 2974+ au_xino_put(br);
1facf9fc 2975+
acd2b654
AM
2976+ AuLCntZero(au_lcnt_read(&br->br_nfiles, /*do_rev*/0));
2977+ au_lcnt_fin(&br->br_nfiles, /*do_sync*/0);
2978+ AuLCntZero(au_lcnt_read(&br->br_count, /*do_rev*/0));
2979+ au_lcnt_fin(&br->br_count, /*do_sync*/0);
1facf9fc 2980+
2981+ wbr = br->br_wbr;
2982+ if (wbr) {
2983+ for (i = 0; i < AuBrWh_Last; i++)
2984+ dput(wbr->wbr_wh[i]);
2985+ AuDebugOn(atomic_read(&wbr->wbr_wh_running));
dece6358 2986+ AuRwDestroy(&wbr->wbr_wh_rwsem);
1facf9fc 2987+ }
2988+
076b876e
AM
2989+ if (br->br_fhsm) {
2990+ au_br_fhsm_fin(br->br_fhsm);
9f237c51 2991+ au_kfree_try_rcu(br->br_fhsm);
076b876e
AM
2992+ }
2993+
4a4d8108
AM
2994+ key = br->br_dykey;
2995+ for (i = 0; i < AuBrDynOp; i++, key++)
2996+ if (*key)
2997+ au_dy_put(*key);
2998+ else
2999+ break;
3000+
537831f9 3001+ /* recursive lock, s_umount of branch's */
acd2b654 3002+ /* synchronize_rcu(); */ /* why? */
537831f9 3003+ lockdep_off();
86dc4139 3004+ path_put(&br->br_path);
537831f9 3005+ lockdep_on();
9f237c51 3006+ au_kfree_rcu(wbr);
acd2b654
AM
3007+ au_lcnt_wait_for_fin(&br->br_nfiles);
3008+ au_lcnt_wait_for_fin(&br->br_count);
3009+ /* I don't know why, but percpu_refcount requires this */
3010+ /* synchronize_rcu(); */
9f237c51 3011+ au_kfree_rcu(br);
1facf9fc 3012+}
3013+
3014+/*
3015+ * frees all branches
3016+ */
3017+void au_br_free(struct au_sbinfo *sbinfo)
3018+{
3019+ aufs_bindex_t bmax;
3020+ struct au_branch **br;
3021+
dece6358
AM
3022+ AuRwMustWriteLock(&sbinfo->si_rwsem);
3023+
5afbbe0d 3024+ bmax = sbinfo->si_bbot + 1;
1facf9fc 3025+ br = sbinfo->si_branch;
3026+ while (bmax--)
3027+ au_br_do_free(*br++);
3028+}
3029+
3030+/*
3031+ * find the index of a branch which is specified by @br_id.
3032+ */
3033+int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
3034+{
5afbbe0d 3035+ aufs_bindex_t bindex, bbot;
1facf9fc 3036+
5afbbe0d
AM
3037+ bbot = au_sbbot(sb);
3038+ for (bindex = 0; bindex <= bbot; bindex++)
1facf9fc 3039+ if (au_sbr_id(sb, bindex) == br_id)
3040+ return bindex;
3041+ return -1;
3042+}
3043+
3044+/* ---------------------------------------------------------------------- */
3045+
3046+/*
3047+ * add a branch
3048+ */
3049+
b752ccd1
AM
3050+static int test_overlap(struct super_block *sb, struct dentry *h_adding,
3051+ struct dentry *h_root)
1facf9fc 3052+{
b752ccd1
AM
3053+ if (unlikely(h_adding == h_root
3054+ || au_test_loopback_overlap(sb, h_adding)))
1facf9fc 3055+ return 1;
b752ccd1
AM
3056+ if (h_adding->d_sb != h_root->d_sb)
3057+ return 0;
3058+ return au_test_subdir(h_adding, h_root)
3059+ || au_test_subdir(h_root, h_adding);
1facf9fc 3060+}
3061+
3062+/*
3063+ * returns a newly allocated branch. @new_nbranch is a number of branches
3064+ * after adding a branch.
3065+ */
3066+static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
3067+ int perm)
3068+{
3069+ struct au_branch *add_branch;
3070+ struct dentry *root;
5527c038 3071+ struct inode *inode;
4a4d8108 3072+ int err;
1facf9fc 3073+
4a4d8108 3074+ err = -ENOMEM;
be52b249 3075+ add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
1facf9fc 3076+ if (unlikely(!add_branch))
3077+ goto out;
acd2b654 3078+ add_branch->br_xino = au_xino_alloc(/*nfile*/1);
062440b3 3079+ if (unlikely(!add_branch->br_xino))
521ced18 3080+ goto out_br;
027c5e7a
AM
3081+ err = au_hnotify_init_br(add_branch, perm);
3082+ if (unlikely(err))
062440b3 3083+ goto out_xino;
027c5e7a 3084+
1facf9fc 3085+ if (au_br_writable(perm)) {
3086+ /* may be freed separately at changing the branch permission */
be52b249 3087+ add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
1facf9fc 3088+ GFP_NOFS);
3089+ if (unlikely(!add_branch->br_wbr))
027c5e7a 3090+ goto out_hnotify;
1facf9fc 3091+ }
3092+
076b876e
AM
3093+ if (au_br_fhsm(perm)) {
3094+ err = au_fhsm_br_alloc(add_branch);
3095+ if (unlikely(err))
3096+ goto out_wbr;
3097+ }
3098+
521ced18 3099+ root = sb->s_root;
e2f27e51 3100+ err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
4a4d8108 3101+ if (!err)
e2f27e51 3102+ err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
5527c038
JR
3103+ if (!err) {
3104+ inode = d_inode(root);
1c60b727
AM
3105+ err = au_hinode_realloc(au_ii(inode), new_nbranch,
3106+ /*may_shrink*/0);
5527c038 3107+ }
4a4d8108
AM
3108+ if (!err)
3109+ return add_branch; /* success */
1facf9fc 3110+
076b876e 3111+out_wbr:
9f237c51 3112+ au_kfree_rcu(add_branch->br_wbr);
027c5e7a
AM
3113+out_hnotify:
3114+ au_hnotify_fin_br(add_branch);
062440b3
AM
3115+out_xino:
3116+ au_xino_put(add_branch);
4f0767ce 3117+out_br:
9f237c51 3118+ au_kfree_rcu(add_branch);
4f0767ce 3119+out:
4a4d8108 3120+ return ERR_PTR(err);
1facf9fc 3121+}
3122+
3123+/*
3124+ * test if the branch permission is legal or not.
3125+ */
3126+static int test_br(struct inode *inode, int brperm, char *path)
3127+{
3128+ int err;
3129+
4a4d8108
AM
3130+ err = (au_br_writable(brperm) && IS_RDONLY(inode));
3131+ if (!err)
3132+ goto out;
1facf9fc 3133+
4a4d8108
AM
3134+ err = -EINVAL;
3135+ pr_err("write permission for readonly mount or inode, %s\n", path);
3136+
4f0767ce 3137+out:
1facf9fc 3138+ return err;
3139+}
3140+
3141+/*
3142+ * returns:
3143+ * 0: success, the caller will add it
3144+ * plus: success, it is already unified, the caller should ignore it
3145+ * minus: error
3146+ */
3147+static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3148+{
3149+ int err;
5afbbe0d 3150+ aufs_bindex_t bbot, bindex;
5527c038 3151+ struct dentry *root, *h_dentry;
1facf9fc 3152+ struct inode *inode, *h_inode;
3153+
3154+ root = sb->s_root;
5afbbe0d
AM
3155+ bbot = au_sbbot(sb);
3156+ if (unlikely(bbot >= 0
1facf9fc 3157+ && au_find_dbindex(root, add->path.dentry) >= 0)) {
3158+ err = 1;
3159+ if (!remount) {
3160+ err = -EINVAL;
4a4d8108 3161+ pr_err("%s duplicated\n", add->pathname);
1facf9fc 3162+ }
3163+ goto out;
3164+ }
3165+
3166+ err = -ENOSPC; /* -E2BIG; */
3167+ if (unlikely(AUFS_BRANCH_MAX <= add->bindex
5afbbe0d 3168+ || AUFS_BRANCH_MAX - 1 <= bbot)) {
4a4d8108 3169+ pr_err("number of branches exceeded %s\n", add->pathname);
1facf9fc 3170+ goto out;
3171+ }
3172+
3173+ err = -EDOM;
5afbbe0d 3174+ if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
4a4d8108 3175+ pr_err("bad index %d\n", add->bindex);
1facf9fc 3176+ goto out;
3177+ }
3178+
5527c038 3179+ inode = d_inode(add->path.dentry);
1facf9fc 3180+ err = -ENOENT;
3181+ if (unlikely(!inode->i_nlink)) {
4a4d8108 3182+ pr_err("no existence %s\n", add->pathname);
1facf9fc 3183+ goto out;
3184+ }
3185+
3186+ err = -EINVAL;
3187+ if (unlikely(inode->i_sb == sb)) {
4a4d8108 3188+ pr_err("%s must be outside\n", add->pathname);
1facf9fc 3189+ goto out;
3190+ }
3191+
3192+ if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
4a4d8108
AM
3193+ pr_err("unsupported filesystem, %s (%s)\n",
3194+ add->pathname, au_sbtype(inode->i_sb));
1facf9fc 3195+ goto out;
3196+ }
3197+
c1595e42
JR
3198+ if (unlikely(inode->i_sb->s_stack_depth)) {
3199+ pr_err("already stacked, %s (%s)\n",
3200+ add->pathname, au_sbtype(inode->i_sb));
3201+ goto out;
3202+ }
3203+
5527c038 3204+ err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
1facf9fc 3205+ if (unlikely(err))
3206+ goto out;
3207+
5afbbe0d 3208+ if (bbot < 0)
1facf9fc 3209+ return 0; /* success */
3210+
3211+ err = -EINVAL;
5afbbe0d 3212+ for (bindex = 0; bindex <= bbot; bindex++)
1facf9fc 3213+ if (unlikely(test_overlap(sb, add->path.dentry,
3214+ au_h_dptr(root, bindex)))) {
4a4d8108 3215+ pr_err("%s is overlapped\n", add->pathname);
1facf9fc 3216+ goto out;
3217+ }
3218+
3219+ err = 0;
3220+ if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
5527c038
JR
3221+ h_dentry = au_h_dptr(root, 0);
3222+ h_inode = d_inode(h_dentry);
1facf9fc 3223+ if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
0c3ec466
AM
3224+ || !uid_eq(h_inode->i_uid, inode->i_uid)
3225+ || !gid_eq(h_inode->i_gid, inode->i_gid))
3226+ pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3227+ add->pathname,
3228+ i_uid_read(inode), i_gid_read(inode),
3229+ (inode->i_mode & S_IALLUGO),
3230+ i_uid_read(h_inode), i_gid_read(h_inode),
3231+ (h_inode->i_mode & S_IALLUGO));
1facf9fc 3232+ }
3233+
4f0767ce 3234+out:
1facf9fc 3235+ return err;
3236+}
3237+
3238+/*
3239+ * initialize or clean the whiteouts for an adding branch
3240+ */
3241+static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
86dc4139 3242+ int new_perm)
1facf9fc 3243+{
3244+ int err, old_perm;
3245+ aufs_bindex_t bindex;
febd17d6 3246+ struct inode *h_inode;
1facf9fc 3247+ struct au_wbr *wbr;
3248+ struct au_hinode *hdir;
5527c038 3249+ struct dentry *h_dentry;
1facf9fc 3250+
86dc4139
AM
3251+ err = vfsub_mnt_want_write(au_br_mnt(br));
3252+ if (unlikely(err))
3253+ goto out;
3254+
1facf9fc 3255+ wbr = br->br_wbr;
3256+ old_perm = br->br_perm;
3257+ br->br_perm = new_perm;
3258+ hdir = NULL;
febd17d6 3259+ h_inode = NULL;
1facf9fc 3260+ bindex = au_br_index(sb, br->br_id);
3261+ if (0 <= bindex) {
5527c038 3262+ hdir = au_hi(d_inode(sb->s_root), bindex);
5afbbe0d 3263+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 3264+ } else {
5527c038 3265+ h_dentry = au_br_dentry(br);
febd17d6
JR
3266+ h_inode = d_inode(h_dentry);
3267+ inode_lock_nested(h_inode, AuLsc_I_PARENT);
1facf9fc 3268+ }
3269+ if (!wbr)
86dc4139 3270+ err = au_wh_init(br, sb);
1facf9fc 3271+ else {
3272+ wbr_wh_write_lock(wbr);
86dc4139 3273+ err = au_wh_init(br, sb);
1facf9fc 3274+ wbr_wh_write_unlock(wbr);
3275+ }
3276+ if (hdir)
5afbbe0d 3277+ au_hn_inode_unlock(hdir);
1facf9fc 3278+ else
febd17d6 3279+ inode_unlock(h_inode);
86dc4139 3280+ vfsub_mnt_drop_write(au_br_mnt(br));
1facf9fc 3281+ br->br_perm = old_perm;
3282+
3283+ if (!err && wbr && !au_br_writable(new_perm)) {
9f237c51 3284+ au_kfree_rcu(wbr);
1facf9fc 3285+ br->br_wbr = NULL;
3286+ }
3287+
86dc4139 3288+out:
1facf9fc 3289+ return err;
3290+}
3291+
3292+static int au_wbr_init(struct au_branch *br, struct super_block *sb,
86dc4139 3293+ int perm)
1facf9fc 3294+{
3295+ int err;
4a4d8108 3296+ struct kstatfs kst;
1facf9fc 3297+ struct au_wbr *wbr;
3298+
3299+ wbr = br->br_wbr;
dece6358 3300+ au_rw_init(&wbr->wbr_wh_rwsem);
1facf9fc 3301+ atomic_set(&wbr->wbr_wh_running, 0);
1facf9fc 3302+
4a4d8108
AM
3303+ /*
3304+ * a limit for rmdir/rename a dir
523b37e3 3305+ * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
4a4d8108 3306+ */
86dc4139 3307+ err = vfs_statfs(&br->br_path, &kst);
4a4d8108
AM
3308+ if (unlikely(err))
3309+ goto out;
3310+ err = -EINVAL;
3311+ if (kst.f_namelen >= NAME_MAX)
86dc4139 3312+ err = au_br_init_wh(sb, br, perm);
4a4d8108 3313+ else
523b37e3
AM
3314+ pr_err("%pd(%s), unsupported namelen %ld\n",
3315+ au_br_dentry(br),
86dc4139 3316+ au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
1facf9fc 3317+
4f0767ce 3318+out:
1facf9fc 3319+ return err;
3320+}
3321+
c1595e42 3322+/* initialize a new branch */
1facf9fc 3323+static int au_br_init(struct au_branch *br, struct super_block *sb,
3324+ struct au_opt_add *add)
3325+{
3326+ int err;
062440b3
AM
3327+ struct au_branch *brbase;
3328+ struct file *xf;
5527c038 3329+ struct inode *h_inode;
1facf9fc 3330+
3331+ err = 0;
1facf9fc 3332+ br->br_perm = add->perm;
86dc4139 3333+ br->br_path = add->path; /* set first, path_get() later */
4a4d8108 3334+ spin_lock_init(&br->br_dykey_lock);
acd2b654
AM
3335+ au_lcnt_init(&br->br_nfiles, /*release*/NULL);
3336+ au_lcnt_init(&br->br_count, /*release*/NULL);
1facf9fc 3337+ br->br_id = au_new_br_id(sb);
7f207e10 3338+ AuDebugOn(br->br_id < 0);
1facf9fc 3339+
8b6a4947
AM
3340+ /* always, regardless the given option */
3341+ err = au_dr_br_init(sb, br, &add->path);
3342+ if (unlikely(err))
3343+ goto out_err;
3344+
1facf9fc 3345+ if (au_br_writable(add->perm)) {
86dc4139 3346+ err = au_wbr_init(br, sb, add->perm);
1facf9fc 3347+ if (unlikely(err))
b752ccd1 3348+ goto out_err;
1facf9fc 3349+ }
3350+
3351+ if (au_opt_test(au_mntflags(sb), XINO)) {
062440b3 3352+ brbase = au_sbr(sb, 0);
acd2b654 3353+ xf = au_xino_file(brbase->br_xino, /*idx*/-1);
062440b3 3354+ AuDebugOn(!xf);
5527c038 3355+ h_inode = d_inode(add->path.dentry);
062440b3 3356+ err = au_xino_init_br(sb, br, h_inode->i_ino, &xf->f_path);
1facf9fc 3357+ if (unlikely(err)) {
acd2b654 3358+ AuDebugOn(au_xino_file(br->br_xino, /*idx*/-1));
b752ccd1 3359+ goto out_err;
1facf9fc 3360+ }
3361+ }
3362+
3363+ sysaufs_br_init(br);
86dc4139 3364+ path_get(&br->br_path);
b752ccd1 3365+ goto out; /* success */
1facf9fc 3366+
4f0767ce 3367+out_err:
86dc4139 3368+ memset(&br->br_path, 0, sizeof(br->br_path));
4f0767ce 3369+out:
1facf9fc 3370+ return err;
3371+}
3372+
3373+static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
5afbbe0d 3374+ struct au_branch *br, aufs_bindex_t bbot,
1facf9fc 3375+ aufs_bindex_t amount)
3376+{
3377+ struct au_branch **brp;
3378+
dece6358
AM
3379+ AuRwMustWriteLock(&sbinfo->si_rwsem);
3380+
1facf9fc 3381+ brp = sbinfo->si_branch + bindex;
3382+ memmove(brp + 1, brp, sizeof(*brp) * amount);
3383+ *brp = br;
5afbbe0d
AM
3384+ sbinfo->si_bbot++;
3385+ if (unlikely(bbot < 0))
3386+ sbinfo->si_bbot = 0;
1facf9fc 3387+}
3388+
3389+static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
5afbbe0d 3390+ aufs_bindex_t bbot, aufs_bindex_t amount)
1facf9fc 3391+{
3392+ struct au_hdentry *hdp;
3393+
1308ab2a 3394+ AuRwMustWriteLock(&dinfo->di_rwsem);
3395+
5afbbe0d 3396+ hdp = au_hdentry(dinfo, bindex);
1facf9fc 3397+ memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3398+ au_h_dentry_init(hdp);
5afbbe0d
AM
3399+ dinfo->di_bbot++;
3400+ if (unlikely(bbot < 0))
3401+ dinfo->di_btop = 0;
1facf9fc 3402+}
3403+
3404+static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
5afbbe0d 3405+ aufs_bindex_t bbot, aufs_bindex_t amount)
1facf9fc 3406+{
3407+ struct au_hinode *hip;
3408+
1308ab2a 3409+ AuRwMustWriteLock(&iinfo->ii_rwsem);
3410+
5afbbe0d 3411+ hip = au_hinode(iinfo, bindex);
1facf9fc 3412+ memmove(hip + 1, hip, sizeof(*hip) * amount);
5afbbe0d
AM
3413+ au_hinode_init(hip);
3414+ iinfo->ii_bbot++;
3415+ if (unlikely(bbot < 0))
3416+ iinfo->ii_btop = 0;
1facf9fc 3417+}
3418+
86dc4139
AM
3419+static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3420+ aufs_bindex_t bindex)
1facf9fc 3421+{
86dc4139 3422+ struct dentry *root, *h_dentry;
5527c038 3423+ struct inode *root_inode, *h_inode;
5afbbe0d 3424+ aufs_bindex_t bbot, amount;
1facf9fc 3425+
3426+ root = sb->s_root;
5527c038 3427+ root_inode = d_inode(root);
5afbbe0d
AM
3428+ bbot = au_sbbot(sb);
3429+ amount = bbot + 1 - bindex;
86dc4139 3430+ h_dentry = au_br_dentry(br);
53392da6 3431+ au_sbilist_lock();
5afbbe0d
AM
3432+ au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
3433+ au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
3434+ au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
1facf9fc 3435+ au_set_h_dptr(root, bindex, dget(h_dentry));
5527c038
JR
3436+ h_inode = d_inode(h_dentry);
3437+ au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
53392da6 3438+ au_sbilist_unlock();
1facf9fc 3439+}
3440+
3441+int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3442+{
3443+ int err;
5afbbe0d 3444+ aufs_bindex_t bbot, add_bindex;
1facf9fc 3445+ struct dentry *root, *h_dentry;
3446+ struct inode *root_inode;
3447+ struct au_branch *add_branch;
3448+
3449+ root = sb->s_root;
5527c038 3450+ root_inode = d_inode(root);
1facf9fc 3451+ IMustLock(root_inode);
5afbbe0d 3452+ IiMustWriteLock(root_inode);
1facf9fc 3453+ err = test_add(sb, add, remount);
3454+ if (unlikely(err < 0))
3455+ goto out;
3456+ if (err) {
3457+ err = 0;
3458+ goto out; /* success */
3459+ }
3460+
5afbbe0d
AM
3461+ bbot = au_sbbot(sb);
3462+ add_branch = au_br_alloc(sb, bbot + 2, add->perm);
1facf9fc 3463+ err = PTR_ERR(add_branch);
3464+ if (IS_ERR(add_branch))
3465+ goto out;
3466+
3467+ err = au_br_init(add_branch, sb, add);
3468+ if (unlikely(err)) {
3469+ au_br_do_free(add_branch);
3470+ goto out;
3471+ }
3472+
3473+ add_bindex = add->bindex;
062440b3
AM
3474+ sysaufs_brs_del(sb, add_bindex); /* remove successors */
3475+ au_br_do_add(sb, add_branch, add_bindex);
3476+ sysaufs_brs_add(sb, add_bindex); /* append successors */
3477+ dbgaufs_brs_add(sb, add_bindex, /*topdown*/0); /* rename successors */
1facf9fc 3478+
86dc4139 3479+ h_dentry = add->path.dentry;
1308ab2a 3480+ if (!add_bindex) {
1facf9fc 3481+ au_cpup_attr_all(root_inode, /*force*/1);
1308ab2a 3482+ sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3483+ } else
5527c038 3484+ au_add_nlink(root_inode, d_inode(h_dentry));
1facf9fc 3485+
4f0767ce 3486+out:
1facf9fc 3487+ return err;
3488+}
3489+
3490+/* ---------------------------------------------------------------------- */
3491+
79b8bda9 3492+static unsigned long long au_farray_cb(struct super_block *sb, void *a,
076b876e
AM
3493+ unsigned long long max __maybe_unused,
3494+ void *arg)
3495+{
3496+ unsigned long long n;
3497+ struct file **p, *f;
8b6a4947
AM
3498+ struct hlist_bl_head *files;
3499+ struct hlist_bl_node *pos;
076b876e 3500+ struct au_finfo *finfo;
076b876e
AM
3501+
3502+ n = 0;
3503+ p = a;
3504+ files = &au_sbi(sb)->si_files;
8b6a4947
AM
3505+ hlist_bl_lock(files);
3506+ hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
076b876e
AM
3507+ f = finfo->fi_file;
3508+ if (file_count(f)
3509+ && !special_file(file_inode(f)->i_mode)) {
3510+ get_file(f);
3511+ *p++ = f;
3512+ n++;
3513+ AuDebugOn(n > max);
3514+ }
3515+ }
8b6a4947 3516+ hlist_bl_unlock(files);
076b876e
AM
3517+
3518+ return n;
3519+}
3520+
3521+static struct file **au_farray_alloc(struct super_block *sb,
3522+ unsigned long long *max)
3523+{
acd2b654
AM
3524+ struct au_sbinfo *sbi;
3525+
3526+ sbi = au_sbi(sb);
3527+ *max = au_lcnt_read(&sbi->si_nfiles, /*do_rev*/1);
79b8bda9 3528+ return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
076b876e
AM
3529+}
3530+
3531+static void au_farray_free(struct file **a, unsigned long long max)
3532+{
3533+ unsigned long long ull;
3534+
3535+ for (ull = 0; ull < max; ull++)
3536+ if (a[ull])
3537+ fput(a[ull]);
be52b249 3538+ kvfree(a);
076b876e
AM
3539+}
3540+
3541+/* ---------------------------------------------------------------------- */
3542+
1facf9fc 3543+/*
3544+ * delete a branch
3545+ */
3546+
3547+/* to show the line number, do not make it inlined function */
4a4d8108 3548+#define AuVerbose(do_info, fmt, ...) do { \
1facf9fc 3549+ if (do_info) \
4a4d8108 3550+ pr_info(fmt, ##__VA_ARGS__); \
1facf9fc 3551+} while (0)
3552+
5afbbe0d
AM
3553+static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
3554+ aufs_bindex_t bbot)
027c5e7a 3555+{
5afbbe0d 3556+ return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
027c5e7a
AM
3557+}
3558+
5afbbe0d
AM
3559+static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
3560+ aufs_bindex_t bbot)
027c5e7a 3561+{
5afbbe0d 3562+ return au_test_ibusy(d_inode(dentry), btop, bbot);
027c5e7a
AM
3563+}
3564+
1facf9fc 3565+/*
3566+ * test if the branch is deletable or not.
3567+ */
3568+static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
b752ccd1 3569+ unsigned int sigen, const unsigned int verbose)
1facf9fc 3570+{
3571+ int err, i, j, ndentry;
5afbbe0d 3572+ aufs_bindex_t btop, bbot;
1facf9fc 3573+ struct au_dcsub_pages dpages;
3574+ struct au_dpage *dpage;
3575+ struct dentry *d;
1facf9fc 3576+
3577+ err = au_dpages_init(&dpages, GFP_NOFS);
3578+ if (unlikely(err))
3579+ goto out;
3580+ err = au_dcsub_pages(&dpages, root, NULL, NULL);
3581+ if (unlikely(err))
3582+ goto out_dpages;
3583+
1facf9fc 3584+ for (i = 0; !err && i < dpages.ndpage; i++) {
3585+ dpage = dpages.dpages + i;
3586+ ndentry = dpage->ndentry;
3587+ for (j = 0; !err && j < ndentry; j++) {
3588+ d = dpage->dentries[j];
c1595e42 3589+ AuDebugOn(au_dcount(d) <= 0);
027c5e7a 3590+ if (!au_digen_test(d, sigen)) {
1facf9fc 3591+ di_read_lock_child(d, AuLock_IR);
027c5e7a
AM
3592+ if (unlikely(au_dbrange_test(d))) {
3593+ di_read_unlock(d, AuLock_IR);
3594+ continue;
3595+ }
3596+ } else {
1facf9fc 3597+ di_write_lock_child(d);
027c5e7a
AM
3598+ if (unlikely(au_dbrange_test(d))) {
3599+ di_write_unlock(d);
3600+ continue;
3601+ }
1facf9fc 3602+ err = au_reval_dpath(d, sigen);
3603+ if (!err)
3604+ di_downgrade_lock(d, AuLock_IR);
3605+ else {
3606+ di_write_unlock(d);
3607+ break;
3608+ }
3609+ }
3610+
027c5e7a 3611+ /* AuDbgDentry(d); */
5afbbe0d
AM
3612+ btop = au_dbtop(d);
3613+ bbot = au_dbbot(d);
3614+ if (btop <= bindex
3615+ && bindex <= bbot
1facf9fc 3616+ && au_h_dptr(d, bindex)
5afbbe0d 3617+ && au_test_dbusy(d, btop, bbot)) {
1facf9fc 3618+ err = -EBUSY;
523b37e3 3619+ AuVerbose(verbose, "busy %pd\n", d);
027c5e7a 3620+ AuDbgDentry(d);
1facf9fc 3621+ }
3622+ di_read_unlock(d, AuLock_IR);
3623+ }
3624+ }
3625+
4f0767ce 3626+out_dpages:
1facf9fc 3627+ au_dpages_free(&dpages);
4f0767ce 3628+out:
1facf9fc 3629+ return err;
3630+}
3631+
3632+static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
b752ccd1 3633+ unsigned int sigen, const unsigned int verbose)
1facf9fc 3634+{
3635+ int err;
7f207e10
AM
3636+ unsigned long long max, ull;
3637+ struct inode *i, **array;
5afbbe0d 3638+ aufs_bindex_t btop, bbot;
1facf9fc 3639+
7f207e10
AM
3640+ array = au_iarray_alloc(sb, &max);
3641+ err = PTR_ERR(array);
3642+ if (IS_ERR(array))
3643+ goto out;
3644+
1facf9fc 3645+ err = 0;
7f207e10
AM
3646+ AuDbg("b%d\n", bindex);
3647+ for (ull = 0; !err && ull < max; ull++) {
3648+ i = array[ull];
076b876e
AM
3649+ if (unlikely(!i))
3650+ break;
7f207e10 3651+ if (i->i_ino == AUFS_ROOT_INO)
1facf9fc 3652+ continue;
3653+
7f207e10 3654+ /* AuDbgInode(i); */
537831f9 3655+ if (au_iigen(i, NULL) == sigen)
1facf9fc 3656+ ii_read_lock_child(i);
3657+ else {
3658+ ii_write_lock_child(i);
027c5e7a
AM
3659+ err = au_refresh_hinode_self(i);
3660+ au_iigen_dec(i);
1facf9fc 3661+ if (!err)
3662+ ii_downgrade_lock(i);
3663+ else {
3664+ ii_write_unlock(i);
3665+ break;
3666+ }
3667+ }
3668+
5afbbe0d
AM
3669+ btop = au_ibtop(i);
3670+ bbot = au_ibbot(i);
3671+ if (btop <= bindex
3672+ && bindex <= bbot
1facf9fc 3673+ && au_h_iptr(i, bindex)
5afbbe0d 3674+ && au_test_ibusy(i, btop, bbot)) {
1facf9fc 3675+ err = -EBUSY;
3676+ AuVerbose(verbose, "busy i%lu\n", i->i_ino);
7f207e10 3677+ AuDbgInode(i);
1facf9fc 3678+ }
3679+ ii_read_unlock(i);
3680+ }
7f207e10 3681+ au_iarray_free(array, max);
1facf9fc 3682+
7f207e10 3683+out:
1facf9fc 3684+ return err;
3685+}
3686+
b752ccd1
AM
3687+static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3688+ const unsigned int verbose)
1facf9fc 3689+{
3690+ int err;
3691+ unsigned int sigen;
3692+
3693+ sigen = au_sigen(root->d_sb);
3694+ DiMustNoWaiters(root);
5527c038 3695+ IiMustNoWaiters(d_inode(root));
1facf9fc 3696+ di_write_unlock(root);
b752ccd1 3697+ err = test_dentry_busy(root, bindex, sigen, verbose);
1facf9fc 3698+ if (!err)
b752ccd1 3699+ err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
1facf9fc 3700+ di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3701+
3702+ return err;
3703+}
3704+
076b876e
AM
3705+static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3706+ struct file **to_free, int *idx)
3707+{
3708+ int err;
c1595e42 3709+ unsigned char matched, root;
5afbbe0d 3710+ aufs_bindex_t bindex, bbot;
076b876e
AM
3711+ struct au_fidir *fidir;
3712+ struct au_hfile *hfile;
3713+
3714+ err = 0;
2000de60 3715+ root = IS_ROOT(file->f_path.dentry);
c1595e42
JR
3716+ if (root) {
3717+ get_file(file);
3718+ to_free[*idx] = file;
3719+ (*idx)++;
3720+ goto out;
3721+ }
3722+
076b876e 3723+ matched = 0;
076b876e
AM
3724+ fidir = au_fi(file)->fi_hdir;
3725+ AuDebugOn(!fidir);
5afbbe0d
AM
3726+ bbot = au_fbbot_dir(file);
3727+ for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
076b876e
AM
3728+ hfile = fidir->fd_hfile + bindex;
3729+ if (!hfile->hf_file)
3730+ continue;
3731+
c1595e42 3732+ if (hfile->hf_br->br_id == br_id) {
076b876e 3733+ matched = 1;
076b876e 3734+ break;
c1595e42 3735+ }
076b876e 3736+ }
c1595e42 3737+ if (matched)
076b876e
AM
3738+ err = -EBUSY;
3739+
3740+out:
3741+ return err;
3742+}
3743+
3744+static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3745+ struct file **to_free, int opened)
3746+{
3747+ int err, idx;
3748+ unsigned long long ull, max;
5afbbe0d 3749+ aufs_bindex_t btop;
076b876e 3750+ struct file *file, **array;
076b876e
AM
3751+ struct dentry *root;
3752+ struct au_hfile *hfile;
3753+
3754+ array = au_farray_alloc(sb, &max);
3755+ err = PTR_ERR(array);
3756+ if (IS_ERR(array))
3757+ goto out;
3758+
3759+ err = 0;
3760+ idx = 0;
3761+ root = sb->s_root;
3762+ di_write_unlock(root);
3763+ for (ull = 0; ull < max; ull++) {
3764+ file = array[ull];
3765+ if (unlikely(!file))
3766+ break;
3767+
3768+ /* AuDbg("%pD\n", file); */
3769+ fi_read_lock(file);
5afbbe0d 3770+ btop = au_fbtop(file);
2000de60 3771+ if (!d_is_dir(file->f_path.dentry)) {
076b876e
AM
3772+ hfile = &au_fi(file)->fi_htop;
3773+ if (hfile->hf_br->br_id == br_id)
3774+ err = -EBUSY;
3775+ } else
3776+ err = test_dir_busy(file, br_id, to_free, &idx);
3777+ fi_read_unlock(file);
3778+ if (unlikely(err))
3779+ break;
3780+ }
3781+ di_write_lock_child(root);
3782+ au_farray_free(array, max);
3783+ AuDebugOn(idx > opened);
3784+
3785+out:
3786+ return err;
3787+}
3788+
3789+static void br_del_file(struct file **to_free, unsigned long long opened,
062440b3 3790+ aufs_bindex_t br_id)
076b876e
AM
3791+{
3792+ unsigned long long ull;
5afbbe0d 3793+ aufs_bindex_t bindex, btop, bbot, bfound;
076b876e
AM
3794+ struct file *file;
3795+ struct au_fidir *fidir;
3796+ struct au_hfile *hfile;
3797+
3798+ for (ull = 0; ull < opened; ull++) {
3799+ file = to_free[ull];
3800+ if (unlikely(!file))
3801+ break;
3802+
3803+ /* AuDbg("%pD\n", file); */
2000de60 3804+ AuDebugOn(!d_is_dir(file->f_path.dentry));
076b876e
AM
3805+ bfound = -1;
3806+ fidir = au_fi(file)->fi_hdir;
3807+ AuDebugOn(!fidir);
3808+ fi_write_lock(file);
5afbbe0d
AM
3809+ btop = au_fbtop(file);
3810+ bbot = au_fbbot_dir(file);
3811+ for (bindex = btop; bindex <= bbot; bindex++) {
076b876e
AM
3812+ hfile = fidir->fd_hfile + bindex;
3813+ if (!hfile->hf_file)
3814+ continue;
3815+
3816+ if (hfile->hf_br->br_id == br_id) {
3817+ bfound = bindex;
3818+ break;
3819+ }
3820+ }
3821+ AuDebugOn(bfound < 0);
3822+ au_set_h_fptr(file, bfound, NULL);
5afbbe0d
AM
3823+ if (bfound == btop) {
3824+ for (btop++; btop <= bbot; btop++)
3825+ if (au_hf_dir(file, btop)) {
3826+ au_set_fbtop(file, btop);
076b876e
AM
3827+ break;
3828+ }
3829+ }
3830+ fi_write_unlock(file);
3831+ }
3832+}
3833+
1facf9fc 3834+static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
3835+ const aufs_bindex_t bindex,
5afbbe0d 3836+ const aufs_bindex_t bbot)
1facf9fc 3837+{
3838+ struct au_branch **brp, **p;
3839+
dece6358
AM
3840+ AuRwMustWriteLock(&sbinfo->si_rwsem);
3841+
1facf9fc 3842+ brp = sbinfo->si_branch + bindex;
5afbbe0d
AM
3843+ if (bindex < bbot)
3844+ memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
3845+ sbinfo->si_branch[0 + bbot] = NULL;
3846+ sbinfo->si_bbot--;
1facf9fc 3847+
e2f27e51
AM
3848+ p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
3849+ /*may_shrink*/1);
1facf9fc 3850+ if (p)
3851+ sbinfo->si_branch = p;
4a4d8108 3852+ /* harmless error */
1facf9fc 3853+}
3854+
3855+static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
5afbbe0d 3856+ const aufs_bindex_t bbot)
1facf9fc 3857+{
3858+ struct au_hdentry *hdp, *p;
3859+
1308ab2a 3860+ AuRwMustWriteLock(&dinfo->di_rwsem);
3861+
5afbbe0d
AM
3862+ hdp = au_hdentry(dinfo, bindex);
3863+ if (bindex < bbot)
3864+ memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
3865+ /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
3866+ dinfo->di_bbot--;
1facf9fc 3867+
e2f27e51
AM
3868+ p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
3869+ /*may_shrink*/1);
1facf9fc 3870+ if (p)
3871+ dinfo->di_hdentry = p;
4a4d8108 3872+ /* harmless error */
1facf9fc 3873+}
3874+
3875+static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
5afbbe0d 3876+ const aufs_bindex_t bbot)
1facf9fc 3877+{
3878+ struct au_hinode *hip, *p;
3879+
1308ab2a 3880+ AuRwMustWriteLock(&iinfo->ii_rwsem);
3881+
5afbbe0d
AM
3882+ hip = au_hinode(iinfo, bindex);
3883+ if (bindex < bbot)
3884+ memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
3885+ /* au_hinode_init(au_hinode(iinfo, bbot)); */
3886+ iinfo->ii_bbot--;
1facf9fc 3887+
e2f27e51
AM
3888+ p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
3889+ /*may_shrink*/1);
1facf9fc 3890+ if (p)
3891+ iinfo->ii_hinode = p;
4a4d8108 3892+ /* harmless error */
1facf9fc 3893+}
3894+
3895+static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
3896+ struct au_branch *br)
3897+{
5afbbe0d 3898+ aufs_bindex_t bbot;
1facf9fc 3899+ struct au_sbinfo *sbinfo;
53392da6
AM
3900+ struct dentry *root, *h_root;
3901+ struct inode *inode, *h_inode;
3902+ struct au_hinode *hinode;
1facf9fc 3903+
dece6358
AM
3904+ SiMustWriteLock(sb);
3905+
1facf9fc 3906+ root = sb->s_root;
5527c038 3907+ inode = d_inode(root);
1facf9fc 3908+ sbinfo = au_sbi(sb);
5afbbe0d 3909+ bbot = sbinfo->si_bbot;
1facf9fc 3910+
53392da6
AM
3911+ h_root = au_h_dptr(root, bindex);
3912+ hinode = au_hi(inode, bindex);
3913+ h_inode = au_igrab(hinode->hi_inode);
3914+ au_hiput(hinode);
1facf9fc 3915+
53392da6 3916+ au_sbilist_lock();
5afbbe0d
AM
3917+ au_br_do_del_brp(sbinfo, bindex, bbot);
3918+ au_br_do_del_hdp(au_di(root), bindex, bbot);
3919+ au_br_do_del_hip(au_ii(inode), bindex, bbot);
53392da6
AM
3920+ au_sbilist_unlock();
3921+
8b6a4947
AM
3922+ /* ignore an error */
3923+ au_dr_br_fin(sb, br); /* always, regardless the mount option */
3924+
53392da6
AM
3925+ dput(h_root);
3926+ iput(h_inode);
3927+ au_br_do_free(br);
1facf9fc 3928+}
3929+
79b8bda9
AM
3930+static unsigned long long empty_cb(struct super_block *sb, void *array,
3931+ unsigned long long max, void *arg)
076b876e
AM
3932+{
3933+ return max;
3934+}
3935+
1facf9fc 3936+int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
3937+{
3938+ int err, rerr, i;
076b876e 3939+ unsigned long long opened;
1facf9fc 3940+ unsigned int mnt_flags;
5afbbe0d 3941+ aufs_bindex_t bindex, bbot, br_id;
1facf9fc 3942+ unsigned char do_wh, verbose;
3943+ struct au_branch *br;
3944+ struct au_wbr *wbr;
076b876e
AM
3945+ struct dentry *root;
3946+ struct file **to_free;
1facf9fc 3947+
3948+ err = 0;
076b876e
AM
3949+ opened = 0;
3950+ to_free = NULL;
3951+ root = sb->s_root;
3952+ bindex = au_find_dbindex(root, del->h_path.dentry);
1facf9fc 3953+ if (bindex < 0) {
3954+ if (remount)
3955+ goto out; /* success */
3956+ err = -ENOENT;
4a4d8108 3957+ pr_err("%s no such branch\n", del->pathname);
1facf9fc 3958+ goto out;
3959+ }
3960+ AuDbg("bindex b%d\n", bindex);
3961+
3962+ err = -EBUSY;
3963+ mnt_flags = au_mntflags(sb);
3964+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
5afbbe0d
AM
3965+ bbot = au_sbbot(sb);
3966+ if (unlikely(!bbot)) {
1facf9fc 3967+ AuVerbose(verbose, "no more branches left\n");
3968+ goto out;
3969+ }
acd2b654 3970+
1facf9fc 3971+ br = au_sbr(sb, bindex);
86dc4139 3972+ AuDebugOn(!path_equal(&br->br_path, &del->h_path));
acd2b654
AM
3973+ if (unlikely(au_lcnt_read(&br->br_count, /*do_rev*/1))) {
3974+ AuVerbose(verbose, "br %pd2 is busy now\n", del->h_path.dentry);
3975+ goto out;
3976+ }
076b876e
AM
3977+
3978+ br_id = br->br_id;
acd2b654 3979+ opened = au_lcnt_read(&br->br_nfiles, /*do_rev*/1);
076b876e 3980+ if (unlikely(opened)) {
79b8bda9 3981+ to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
076b876e
AM
3982+ err = PTR_ERR(to_free);
3983+ if (IS_ERR(to_free))
3984+ goto out;
3985+
3986+ err = test_file_busy(sb, br_id, to_free, opened);
3987+ if (unlikely(err)) {
3988+ AuVerbose(verbose, "%llu file(s) opened\n", opened);
3989+ goto out;
3990+ }
1facf9fc 3991+ }
3992+
3993+ wbr = br->br_wbr;
3994+ do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
3995+ if (do_wh) {
1308ab2a 3996+ /* instead of WbrWhMustWriteLock(wbr) */
3997+ SiMustWriteLock(sb);
1facf9fc 3998+ for (i = 0; i < AuBrWh_Last; i++) {
3999+ dput(wbr->wbr_wh[i]);
4000+ wbr->wbr_wh[i] = NULL;
4001+ }
4002+ }
4003+
076b876e 4004+ err = test_children_busy(root, bindex, verbose);
1facf9fc 4005+ if (unlikely(err)) {
4006+ if (do_wh)
4007+ goto out_wh;
4008+ goto out;
4009+ }
4010+
4011+ err = 0;
076b876e
AM
4012+ if (to_free) {
4013+ /*
4014+ * now we confirmed the branch is deletable.
4015+ * let's free the remaining opened dirs on the branch.
4016+ */
4017+ di_write_unlock(root);
4018+ br_del_file(to_free, opened, br_id);
4019+ di_write_lock_child(root);
4020+ }
4021+
062440b3
AM
4022+ sysaufs_brs_del(sb, bindex); /* remove successors */
4023+ dbgaufs_xino_del(br); /* remove one */
4024+ au_br_do_del(sb, bindex, br);
4025+ sysaufs_brs_add(sb, bindex); /* append successors */
4026+ dbgaufs_brs_add(sb, bindex, /*topdown*/1); /* rename successors */
1facf9fc 4027+
1308ab2a 4028+ if (!bindex) {
5527c038 4029+ au_cpup_attr_all(d_inode(root), /*force*/1);
1308ab2a 4030+ sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
4031+ } else
5527c038 4032+ au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
1facf9fc 4033+ if (au_opt_test(mnt_flags, PLINK))
4034+ au_plink_half_refresh(sb, br_id);
4035+
1facf9fc 4036+ goto out; /* success */
4037+
4f0767ce 4038+out_wh:
1facf9fc 4039+ /* revert */
86dc4139 4040+ rerr = au_br_init_wh(sb, br, br->br_perm);
1facf9fc 4041+ if (rerr)
0c3ec466
AM
4042+ pr_warn("failed re-creating base whiteout, %s. (%d)\n",
4043+ del->pathname, rerr);
4f0767ce 4044+out:
076b876e
AM
4045+ if (to_free)
4046+ au_farray_free(to_free, opened);
1facf9fc 4047+ return err;
4048+}
4049+
4050+/* ---------------------------------------------------------------------- */
4051+
027c5e7a
AM
4052+static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
4053+{
4054+ int err;
5afbbe0d 4055+ aufs_bindex_t btop, bbot;
027c5e7a
AM
4056+ struct aufs_ibusy ibusy;
4057+ struct inode *inode, *h_inode;
4058+
4059+ err = -EPERM;
4060+ if (unlikely(!capable(CAP_SYS_ADMIN)))
4061+ goto out;
4062+
4063+ err = copy_from_user(&ibusy, arg, sizeof(ibusy));
4064+ if (!err)
ba1aed25
AM
4065+ /* VERIFY_WRITE */
4066+ err = !access_ok(&arg->h_ino, sizeof(arg->h_ino));
027c5e7a
AM
4067+ if (unlikely(err)) {
4068+ err = -EFAULT;
4069+ AuTraceErr(err);
4070+ goto out;
4071+ }
4072+
4073+ err = -EINVAL;
4074+ si_read_lock(sb, AuLock_FLUSH);
5afbbe0d 4075+ if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
027c5e7a
AM
4076+ goto out_unlock;
4077+
4078+ err = 0;
4079+ ibusy.h_ino = 0; /* invalid */
4080+ inode = ilookup(sb, ibusy.ino);
4081+ if (!inode
4082+ || inode->i_ino == AUFS_ROOT_INO
5afbbe0d 4083+ || au_is_bad_inode(inode))
027c5e7a
AM
4084+ goto out_unlock;
4085+
4086+ ii_read_lock_child(inode);
5afbbe0d
AM
4087+ btop = au_ibtop(inode);
4088+ bbot = au_ibbot(inode);
4089+ if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
027c5e7a 4090+ h_inode = au_h_iptr(inode, ibusy.bindex);
5afbbe0d 4091+ if (h_inode && au_test_ibusy(inode, btop, bbot))
027c5e7a
AM
4092+ ibusy.h_ino = h_inode->i_ino;
4093+ }
4094+ ii_read_unlock(inode);
4095+ iput(inode);
4096+
4097+out_unlock:
4098+ si_read_unlock(sb);
4099+ if (!err) {
4100+ err = __put_user(ibusy.h_ino, &arg->h_ino);
4101+ if (unlikely(err)) {
4102+ err = -EFAULT;
4103+ AuTraceErr(err);
4104+ }
4105+ }
4106+out:
4107+ return err;
4108+}
4109+
4110+long au_ibusy_ioctl(struct file *file, unsigned long arg)
4111+{
2000de60 4112+ return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
027c5e7a
AM
4113+}
4114+
4115+#ifdef CONFIG_COMPAT
4116+long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
4117+{
2000de60 4118+ return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
027c5e7a
AM
4119+}
4120+#endif
4121+
4122+/* ---------------------------------------------------------------------- */
4123+
1facf9fc 4124+/*
4125+ * change a branch permission
4126+ */
4127+
dece6358
AM
4128+static void au_warn_ima(void)
4129+{
4130+#ifdef CONFIG_IMA
1308ab2a 4131+ /* since it doesn't support mark_files_ro() */
027c5e7a 4132+ AuWarn1("RW -> RO makes IMA to produce wrong message\n");
dece6358
AM
4133+#endif
4134+}
4135+
1facf9fc 4136+static int do_need_sigen_inc(int a, int b)
4137+{
4138+ return au_br_whable(a) && !au_br_whable(b);
4139+}
4140+
4141+static int need_sigen_inc(int old, int new)
4142+{
4143+ return do_need_sigen_inc(old, new)
4144+ || do_need_sigen_inc(new, old);
4145+}
4146+
4147+static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
4148+{
7f207e10 4149+ int err, do_warn;
027c5e7a 4150+ unsigned int mnt_flags;
7f207e10 4151+ unsigned long long ull, max;
e49829fe 4152+ aufs_bindex_t br_id;
38d290e6 4153+ unsigned char verbose, writer;
7f207e10 4154+ struct file *file, *hf, **array;
e49829fe 4155+ struct au_hfile *hfile;
eca34b5c 4156+ struct inode *h_inode;
1facf9fc 4157+
027c5e7a
AM
4158+ mnt_flags = au_mntflags(sb);
4159+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
4160+
7f207e10
AM
4161+ array = au_farray_alloc(sb, &max);
4162+ err = PTR_ERR(array);
4163+ if (IS_ERR(array))
1facf9fc 4164+ goto out;
4165+
7f207e10 4166+ do_warn = 0;
e49829fe 4167+ br_id = au_sbr_id(sb, bindex);
7f207e10
AM
4168+ for (ull = 0; ull < max; ull++) {
4169+ file = array[ull];
076b876e
AM
4170+ if (unlikely(!file))
4171+ break;
1facf9fc 4172+
523b37e3 4173+ /* AuDbg("%pD\n", file); */
1facf9fc 4174+ fi_read_lock(file);
4175+ if (unlikely(au_test_mmapped(file))) {
4176+ err = -EBUSY;
523b37e3 4177+ AuVerbose(verbose, "mmapped %pD\n", file);
7f207e10 4178+ AuDbgFile(file);
1facf9fc 4179+ FiMustNoWaiters(file);
4180+ fi_read_unlock(file);
7f207e10 4181+ goto out_array;
1facf9fc 4182+ }
4183+
e49829fe
JR
4184+ hfile = &au_fi(file)->fi_htop;
4185+ hf = hfile->hf_file;
7e9cd9fe 4186+ if (!d_is_reg(file->f_path.dentry)
1facf9fc 4187+ || !(file->f_mode & FMODE_WRITE)
e49829fe 4188+ || hfile->hf_br->br_id != br_id
7f207e10
AM
4189+ || !(hf->f_mode & FMODE_WRITE))
4190+ array[ull] = NULL;
4191+ else {
4192+ do_warn = 1;
4193+ get_file(file);
1facf9fc 4194+ }
4195+
1facf9fc 4196+ FiMustNoWaiters(file);
4197+ fi_read_unlock(file);
7f207e10
AM
4198+ fput(file);
4199+ }
1facf9fc 4200+
4201+ err = 0;
7f207e10 4202+ if (do_warn)
dece6358 4203+ au_warn_ima();
7f207e10
AM
4204+
4205+ for (ull = 0; ull < max; ull++) {
4206+ file = array[ull];
4207+ if (!file)
4208+ continue;
4209+
1facf9fc 4210+ /* todo: already flushed? */
523b37e3
AM
4211+ /*
4212+ * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4213+ * approach which resets f_mode and calls mnt_drop_write() and
4214+ * file_release_write() for each file, because the branch
4215+ * attribute in aufs world is totally different from the native
4216+ * fs rw/ro mode.
4217+ */
7f207e10
AM
4218+ /* fi_read_lock(file); */
4219+ hfile = &au_fi(file)->fi_htop;
4220+ hf = hfile->hf_file;
4221+ /* fi_read_unlock(file); */
027c5e7a 4222+ spin_lock(&hf->f_lock);
38d290e6
JR
4223+ writer = !!(hf->f_mode & FMODE_WRITER);
4224+ hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
027c5e7a 4225+ spin_unlock(&hf->f_lock);
38d290e6 4226+ if (writer) {
eca34b5c 4227+ h_inode = file_inode(hf);
42b5c33a
AM
4228+ if (hf->f_mode & FMODE_READ)
4229+ i_readcount_inc(h_inode);
eca34b5c 4230+ put_write_access(h_inode);
c06a8ce3 4231+ __mnt_drop_write(hf->f_path.mnt);
1facf9fc 4232+ }
4233+ }
4234+
7f207e10
AM
4235+out_array:
4236+ au_farray_free(array, max);
4f0767ce 4237+out:
7f207e10 4238+ AuTraceErr(err);
1facf9fc 4239+ return err;
4240+}
4241+
4242+int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
7f207e10 4243+ int *do_refresh)
1facf9fc 4244+{
4245+ int err, rerr;
4246+ aufs_bindex_t bindex;
4247+ struct dentry *root;
4248+ struct au_branch *br;
076b876e 4249+ struct au_br_fhsm *bf;
1facf9fc 4250+
4251+ root = sb->s_root;
1facf9fc 4252+ bindex = au_find_dbindex(root, mod->h_root);
4253+ if (bindex < 0) {
4254+ if (remount)
4255+ return 0; /* success */
4256+ err = -ENOENT;
4a4d8108 4257+ pr_err("%s no such branch\n", mod->path);
1facf9fc 4258+ goto out;
4259+ }
4260+ AuDbg("bindex b%d\n", bindex);
4261+
5527c038 4262+ err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
1facf9fc 4263+ if (unlikely(err))
4264+ goto out;
4265+
4266+ br = au_sbr(sb, bindex);
86dc4139 4267+ AuDebugOn(mod->h_root != au_br_dentry(br));
1facf9fc 4268+ if (br->br_perm == mod->perm)
4269+ return 0; /* success */
4270+
076b876e
AM
4271+ /* pre-allocate for non-fhsm --> fhsm */
4272+ bf = NULL;
4273+ if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4274+ err = au_fhsm_br_alloc(br);
4275+ if (unlikely(err))
4276+ goto out;
4277+ bf = br->br_fhsm;
4278+ br->br_fhsm = NULL;
4279+ }
4280+
1facf9fc 4281+ if (au_br_writable(br->br_perm)) {
4282+ /* remove whiteout base */
86dc4139 4283+ err = au_br_init_wh(sb, br, mod->perm);
1facf9fc 4284+ if (unlikely(err))
076b876e 4285+ goto out_bf;
1facf9fc 4286+
4287+ if (!au_br_writable(mod->perm)) {
4288+ /* rw --> ro, file might be mmapped */
4289+ DiMustNoWaiters(root);
5527c038 4290+ IiMustNoWaiters(d_inode(root));
1facf9fc 4291+ di_write_unlock(root);
4292+ err = au_br_mod_files_ro(sb, bindex);
4293+ /* aufs_write_lock() calls ..._child() */
4294+ di_write_lock_child(root);
4295+
4296+ if (unlikely(err)) {
4297+ rerr = -ENOMEM;
be52b249 4298+ br->br_wbr = kzalloc(sizeof(*br->br_wbr),
1facf9fc 4299+ GFP_NOFS);
86dc4139
AM
4300+ if (br->br_wbr)
4301+ rerr = au_wbr_init(br, sb, br->br_perm);
1facf9fc 4302+ if (unlikely(rerr)) {
4303+ AuIOErr("nested error %d (%d)\n",
4304+ rerr, err);
4305+ br->br_perm = mod->perm;
4306+ }
4307+ }
4308+ }
4309+ } else if (au_br_writable(mod->perm)) {
4310+ /* ro --> rw */
4311+ err = -ENOMEM;
be52b249 4312+ br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
1facf9fc 4313+ if (br->br_wbr) {
86dc4139 4314+ err = au_wbr_init(br, sb, mod->perm);
1facf9fc 4315+ if (unlikely(err)) {
9f237c51 4316+ au_kfree_rcu(br->br_wbr);
1facf9fc 4317+ br->br_wbr = NULL;
4318+ }
4319+ }
4320+ }
076b876e
AM
4321+ if (unlikely(err))
4322+ goto out_bf;
4323+
4324+ if (au_br_fhsm(br->br_perm)) {
4325+ if (!au_br_fhsm(mod->perm)) {
4326+ /* fhsm --> non-fhsm */
4327+ au_br_fhsm_fin(br->br_fhsm);
9f237c51 4328+ au_kfree_rcu(br->br_fhsm);
076b876e
AM
4329+ br->br_fhsm = NULL;
4330+ }
4331+ } else if (au_br_fhsm(mod->perm))
4332+ /* non-fhsm --> fhsm */
4333+ br->br_fhsm = bf;
4334+
076b876e
AM
4335+ *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4336+ br->br_perm = mod->perm;
4337+ goto out; /* success */
1facf9fc 4338+
076b876e 4339+out_bf:
9f237c51 4340+ au_kfree_try_rcu(bf);
076b876e
AM
4341+out:
4342+ AuTraceErr(err);
4343+ return err;
4344+}
4345+
4346+/* ---------------------------------------------------------------------- */
4347+
4348+int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4349+{
4350+ int err;
4351+ struct kstatfs kstfs;
4352+
4353+ err = vfs_statfs(&br->br_path, &kstfs);
1facf9fc 4354+ if (!err) {
076b876e
AM
4355+ stfs->f_blocks = kstfs.f_blocks;
4356+ stfs->f_bavail = kstfs.f_bavail;
4357+ stfs->f_files = kstfs.f_files;
4358+ stfs->f_ffree = kstfs.f_ffree;
1facf9fc 4359+ }
4360+
1facf9fc 4361+ return err;
4362+}
7f207e10 4363diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
eca34b5c 4364--- /usr/share/empty/fs/aufs/branch.h 1970-01-01 01:00:00.000000000 +0100
46016270 4365+++ linux/fs/aufs/branch.h 2021-06-30 21:35:11.393873211 +0200
0b2a12c6 4366@@ -0,0 +1,375 @@
062440b3 4367+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 4368+/*
d58c55f2 4369+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 4370+ *
4371+ * This program, aufs is free software; you can redistribute it and/or modify
4372+ * it under the terms of the GNU General Public License as published by
4373+ * the Free Software Foundation; either version 2 of the License, or
4374+ * (at your option) any later version.
dece6358
AM
4375+ *
4376+ * This program is distributed in the hope that it will be useful,
4377+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
4378+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4379+ * GNU General Public License for more details.
4380+ *
4381+ * You should have received a copy of the GNU General Public License
523b37e3 4382+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 4383+ */
4384+
4385+/*
4386+ * branch filesystems and xino for them
4387+ */
4388+
4389+#ifndef __AUFS_BRANCH_H__
4390+#define __AUFS_BRANCH_H__
4391+
4392+#ifdef __KERNEL__
4393+
1facf9fc 4394+#include <linux/mount.h>
8b6a4947 4395+#include "dirren.h"
4a4d8108 4396+#include "dynop.h"
acd2b654 4397+#include "lcnt.h"
1facf9fc 4398+#include "rwsem.h"
4399+#include "super.h"
4400+
4401+/* ---------------------------------------------------------------------- */
4402+
4403+/* a xino file */
062440b3 4404+struct au_xino {
acd2b654
AM
4405+ struct file **xi_file;
4406+ unsigned int xi_nfile;
4407+
521ced18
JR
4408+ struct {
4409+ spinlock_t spin;
4410+ ino_t *array;
4411+ int total;
4412+ /* reserved for future use */
4413+ /* unsigned long *bitmap; */
4414+ wait_queue_head_t wqh;
4415+ } xi_nondir;
1facf9fc 4416+
acd2b654 4417+ struct mutex xi_mtx; /* protects xi_file array */
9f237c51 4418+ struct hlist_bl_head xi_writing;
acd2b654 4419+
062440b3 4420+ atomic_t xi_truncating;
1facf9fc 4421+
062440b3 4422+ struct kref xi_kref;
1facf9fc 4423+};
4424+
076b876e
AM
4425+/* File-based Hierarchical Storage Management */
4426+struct au_br_fhsm {
4427+#ifdef CONFIG_AUFS_FHSM
4428+ struct mutex bf_lock;
4429+ unsigned long bf_jiffy;
4430+ struct aufs_stfs bf_stfs;
4431+ int bf_readable;
4432+#endif
4433+};
4434+
1facf9fc 4435+/* members for writable branch only */
4436+enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4437+struct au_wbr {
dece6358 4438+ struct au_rwsem wbr_wh_rwsem;
1facf9fc 4439+ struct dentry *wbr_wh[AuBrWh_Last];
4a4d8108 4440+ atomic_t wbr_wh_running;
1facf9fc 4441+#define wbr_whbase wbr_wh[AuBrWh_BASE] /* whiteout base */
4442+#define wbr_plink wbr_wh[AuBrWh_PLINK] /* pseudo-link dir */
4443+#define wbr_orph wbr_wh[AuBrWh_ORPH] /* dir for orphans */
4444+
4445+ /* mfs mode */
4446+ unsigned long long wbr_bytes;
4447+};
4448+
4a4d8108
AM
4449+/* ext2 has 3 types of operations at least, ext3 has 4 */
4450+#define AuBrDynOp (AuDyLast * 4)
4451+
1716fcea
AM
4452+#ifdef CONFIG_AUFS_HFSNOTIFY
4453+/* support for asynchronous destruction */
4454+struct au_br_hfsnotify {
4455+ struct fsnotify_group *hfsn_group;
4456+};
4457+#endif
4458+
392086de
AM
4459+/* sysfs entries */
4460+struct au_brsysfs {
4461+ char name[16];
4462+ struct attribute attr;
4463+};
4464+
4465+enum {
4466+ AuBrSysfs_BR,
4467+ AuBrSysfs_BRID,
4468+ AuBrSysfs_Last
4469+};
4470+
1facf9fc 4471+/* protected by superblock rwsem */
4472+struct au_branch {
062440b3 4473+ struct au_xino *br_xino;
1facf9fc 4474+
4475+ aufs_bindex_t br_id;
4476+
4477+ int br_perm;
86dc4139 4478+ struct path br_path;
4a4d8108
AM
4479+ spinlock_t br_dykey_lock;
4480+ struct au_dykey *br_dykey[AuBrDynOp];
acd2b654
AM
4481+ au_lcnt_t br_nfiles; /* opened files */
4482+ au_lcnt_t br_count; /* in-use for other */
1facf9fc 4483+
4484+ struct au_wbr *br_wbr;
076b876e 4485+ struct au_br_fhsm *br_fhsm;
1facf9fc 4486+
027c5e7a 4487+#ifdef CONFIG_AUFS_HFSNOTIFY
1716fcea 4488+ struct au_br_hfsnotify *br_hfsn;
027c5e7a
AM
4489+#endif
4490+
1facf9fc 4491+#ifdef CONFIG_SYSFS
392086de
AM
4492+ /* entries under sysfs per mount-point */
4493+ struct au_brsysfs br_sysfs[AuBrSysfs_Last];
1facf9fc 4494+#endif
8b6a4947 4495+
062440b3
AM
4496+#ifdef CONFIG_DEBUG_FS
4497+ struct dentry *br_dbgaufs; /* xino */
4498+#endif
4499+
8b6a4947 4500+ struct au_dr_br br_dirren;
1facf9fc 4501+};
4502+
4503+/* ---------------------------------------------------------------------- */
4504+
86dc4139
AM
4505+static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4506+{
4507+ return br->br_path.mnt;
4508+}
4509+
4510+static inline struct dentry *au_br_dentry(struct au_branch *br)
4511+{
4512+ return br->br_path.dentry;
4513+}
4514+
0b2a12c6
JR
4515+static inline struct user_namespace *au_br_userns(struct au_branch *br)
4516+{
4517+ return mnt_user_ns(br->br_path.mnt);
4518+}
4519+
86dc4139
AM
4520+static inline struct super_block *au_br_sb(struct au_branch *br)
4521+{
4522+ return au_br_mnt(br)->mnt_sb;
4523+}
4524+
1facf9fc 4525+static inline int au_br_rdonly(struct au_branch *br)
4526+{
8b6a4947 4527+ return (sb_rdonly(au_br_sb(br))
1facf9fc 4528+ || !au_br_writable(br->br_perm))
4529+ ? -EROFS : 0;
4530+}
4531+
4a4d8108 4532+static inline int au_br_hnotifyable(int brperm __maybe_unused)
1facf9fc 4533+{
4a4d8108 4534+#ifdef CONFIG_AUFS_HNOTIFY
1e00d052 4535+ return !(brperm & AuBrPerm_RR);
1facf9fc 4536+#else
4537+ return 0;
4538+#endif
4539+}
4540+
b912730e
AM
4541+static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4542+{
4543+ int err, exec_flag;
4544+
4545+ err = 0;
4546+ exec_flag = oflag & __FMODE_EXEC;
79b8bda9 4547+ if (unlikely(exec_flag && path_noexec(&br->br_path)))
b912730e
AM
4548+ err = -EACCES;
4549+
4550+ return err;
4551+}
4552+
062440b3
AM
4553+static inline void au_xino_get(struct au_branch *br)
4554+{
4555+ struct au_xino *xi;
4556+
4557+ xi = br->br_xino;
4558+ if (xi)
4559+ kref_get(&xi->xi_kref);
4560+}
4561+
4562+static inline int au_xino_count(struct au_branch *br)
4563+{
4564+ int v;
4565+ struct au_xino *xi;
4566+
4567+ v = 0;
4568+ xi = br->br_xino;
4569+ if (xi)
4570+ v = kref_read(&xi->xi_kref);
4571+
4572+ return v;
4573+}
4574+
1facf9fc 4575+/* ---------------------------------------------------------------------- */
4576+
4577+/* branch.c */
4578+struct au_sbinfo;
4579+void au_br_free(struct au_sbinfo *sinfo);
4580+int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4581+struct au_opt_add;
4582+int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4583+struct au_opt_del;
4584+int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
027c5e7a
AM
4585+long au_ibusy_ioctl(struct file *file, unsigned long arg);
4586+#ifdef CONFIG_COMPAT
4587+long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4588+#endif
1facf9fc 4589+struct au_opt_mod;
4590+int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
7f207e10 4591+ int *do_refresh);
076b876e
AM
4592+struct aufs_stfs;
4593+int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
1facf9fc 4594+
4595+/* xino.c */
4596+static const loff_t au_loff_max = LLONG_MAX;
4597+
acd2b654 4598+aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry);
83b672a5
AM
4599+struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
4600+ int wbrtop);
062440b3
AM
4601+struct file *au_xino_create2(struct super_block *sb, struct path *base,
4602+ struct file *copy_src);
acd2b654
AM
4603+struct au_xi_new {
4604+ struct au_xino *xi; /* switch between xino and xigen */
4605+ int idx;
4606+ struct path *base;
4607+ struct file *copy_src;
4608+};
4609+struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew);
062440b3
AM
4610+
4611+int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4612+ ino_t *ino);
4613+int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4614+ ino_t ino);
e37dd06a
AM
4615+ssize_t xino_fread(struct file *file, void *buf, size_t size, loff_t *pos);
4616+ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos);
062440b3
AM
4617+
4618+int au_xib_trunc(struct super_block *sb);
acd2b654 4619+int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin);
1facf9fc 4620+
acd2b654 4621+struct au_xino *au_xino_alloc(unsigned int nfile);
062440b3 4622+int au_xino_put(struct au_branch *br);
acd2b654 4623+struct file *au_xino_file1(struct au_xino *xi);
062440b3 4624+
1facf9fc 4625+struct au_opt_xino;
1facf9fc 4626+void au_xino_clr(struct super_block *sb);
062440b3 4627+int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount);
1facf9fc 4628+struct file *au_xino_def(struct super_block *sb);
062440b3
AM
4629+int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4630+ struct path *base);
4631+
4632+ino_t au_xino_new_ino(struct super_block *sb);
4633+void au_xino_delete_inode(struct inode *inode, const int unlinked);
1facf9fc 4634+
521ced18
JR
4635+void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
4636+ ino_t h_ino, int idx);
4637+int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4638+ int *idx);
4639+
062440b3
AM
4640+int au_xino_path(struct seq_file *seq, struct file *file);
4641+
1facf9fc 4642+/* ---------------------------------------------------------------------- */
4643+
acd2b654
AM
4644+/* @idx is signed to accept -1 meaning the first file */
4645+static inline struct file *au_xino_file(struct au_xino *xi, int idx)
4646+{
4647+ struct file *file;
4648+
4649+ file = NULL;
4650+ if (!xi)
4651+ goto out;
4652+
4653+ if (idx >= 0) {
4654+ if (idx < xi->xi_nfile)
4655+ file = xi->xi_file[idx];
4656+ } else
4657+ file = au_xino_file1(xi);
4658+
4659+out:
4660+ return file;
4661+}
4662+
4663+/* ---------------------------------------------------------------------- */
4664+
1facf9fc 4665+/* Superblock to branch */
4666+static inline
4667+aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4668+{
4669+ return au_sbr(sb, bindex)->br_id;
4670+}
4671+
4672+static inline
4673+struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4674+{
86dc4139 4675+ return au_br_mnt(au_sbr(sb, bindex));
1facf9fc 4676+}
4677+
4678+static inline
0b2a12c6
JR
4679+struct user_namespace *au_sbr_userns(struct super_block *sb, aufs_bindex_t bindex)
4680+{
4681+ return au_br_userns(au_sbr(sb, bindex));
4682+}
4683+
4684+static inline
1facf9fc 4685+struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4686+{
86dc4139 4687+ return au_br_sb(au_sbr(sb, bindex));
1facf9fc 4688+}
4689+
1facf9fc 4690+static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4691+{
4692+ return au_sbr(sb, bindex)->br_perm;
4693+}
4694+
4695+static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4696+{
4697+ return au_br_whable(au_sbr_perm(sb, bindex));
4698+}
4699+
4700+/* ---------------------------------------------------------------------- */
4701+
8b6a4947
AM
4702+#define wbr_wh_read_lock(wbr) au_rw_read_lock(&(wbr)->wbr_wh_rwsem)
4703+#define wbr_wh_write_lock(wbr) au_rw_write_lock(&(wbr)->wbr_wh_rwsem)
4704+#define wbr_wh_read_trylock(wbr) au_rw_read_trylock(&(wbr)->wbr_wh_rwsem)
4705+#define wbr_wh_write_trylock(wbr) au_rw_write_trylock(&(wbr)->wbr_wh_rwsem)
1facf9fc 4706+/*
8b6a4947
AM
4707+#define wbr_wh_read_trylock_nested(wbr) \
4708+ au_rw_read_trylock_nested(&(wbr)->wbr_wh_rwsem)
4709+#define wbr_wh_write_trylock_nested(wbr) \
4710+ au_rw_write_trylock_nested(&(wbr)->wbr_wh_rwsem)
4711+*/
1facf9fc 4712+
8b6a4947
AM
4713+#define wbr_wh_read_unlock(wbr) au_rw_read_unlock(&(wbr)->wbr_wh_rwsem)
4714+#define wbr_wh_write_unlock(wbr) au_rw_write_unlock(&(wbr)->wbr_wh_rwsem)
4715+#define wbr_wh_downgrade_lock(wbr) au_rw_dgrade_lock(&(wbr)->wbr_wh_rwsem)
4716+
4717+#define WbrWhMustNoWaiters(wbr) AuRwMustNoWaiters(&(wbr)->wbr_wh_rwsem)
4718+#define WbrWhMustAnyLock(wbr) AuRwMustAnyLock(&(wbr)->wbr_wh_rwsem)
4719+#define WbrWhMustWriteLock(wbr) AuRwMustWriteLock(&(wbr)->wbr_wh_rwsem)
dece6358 4720+
076b876e
AM
4721+/* ---------------------------------------------------------------------- */
4722+
4723+#ifdef CONFIG_AUFS_FHSM
4724+static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4725+{
4726+ mutex_init(&brfhsm->bf_lock);
4727+ brfhsm->bf_jiffy = 0;
4728+ brfhsm->bf_readable = 0;
4729+}
4730+
4731+static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4732+{
4733+ mutex_destroy(&brfhsm->bf_lock);
4734+}
4735+#else
4736+AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4737+AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4738+#endif
4739+
1facf9fc 4740+#endif /* __KERNEL__ */
4741+#endif /* __AUFS_BRANCH_H__ */
7f207e10 4742diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
eca34b5c 4743--- /usr/share/empty/fs/aufs/conf.mk 1970-01-01 01:00:00.000000000 +0100
46016270 4744+++ linux/fs/aufs/conf.mk 2021-02-24 13:33:42.741013619 +0100
2121bcd9
AM
4745@@ -0,0 +1,40 @@
4746+# SPDX-License-Identifier: GPL-2.0
4a4d8108
AM
4747+
4748+AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4749+
4750+define AuConf
4751+ifdef ${1}
4752+AuConfStr += ${1}=${${1}}
4753+endif
4754+endef
4755+
b752ccd1 4756+AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
e49829fe 4757+ SBILIST \
7f207e10 4758+ HNOTIFY HFSNOTIFY \
4a4d8108 4759+ EXPORT INO_T_64 \
c1595e42 4760+ XATTR \
076b876e 4761+ FHSM \
4a4d8108 4762+ RDU \
8b6a4947 4763+ DIRREN \
4a4d8108
AM
4764+ SHWH \
4765+ BR_RAMFS \
4766+ BR_FUSE POLL \
4767+ BR_HFSPLUS \
4768+ BDEV_LOOP \
b752ccd1
AM
4769+ DEBUG MAGIC_SYSRQ
4770+$(foreach i, ${AuConfAll}, \
4a4d8108
AM
4771+ $(eval $(call AuConf,CONFIG_AUFS_${i})))
4772+
4773+AuConfName = ${obj}/conf.str
4774+${AuConfName}.tmp: FORCE
4775+ @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4776+${AuConfName}: ${AuConfName}.tmp
4777+ @diff -q $< $@ > /dev/null 2>&1 || { \
4778+ echo ' GEN ' $@; \
4779+ cp -p $< $@; \
4780+ }
4781+FORCE:
4782+clean-files += ${AuConfName} ${AuConfName}.tmp
4783+${obj}/sysfs.o: ${AuConfName}
b752ccd1
AM
4784+
4785+-include ${srctree}/${src}/conf_priv.mk
7f207e10 4786diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
eca34b5c 4787--- /usr/share/empty/fs/aufs/cpup.c 1970-01-01 01:00:00.000000000 +0100
46016270 4788+++ linux/fs/aufs/cpup.c 2021-06-30 21:35:11.393873211 +0200
0b2a12c6 4789@@ -0,0 +1,1457 @@
cd7a4cd9 4790+// SPDX-License-Identifier: GPL-2.0
1facf9fc 4791+/*
d58c55f2 4792+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 4793+ *
4794+ * This program, aufs is free software; you can redistribute it and/or modify
4795+ * it under the terms of the GNU General Public License as published by
4796+ * the Free Software Foundation; either version 2 of the License, or
4797+ * (at your option) any later version.
dece6358
AM
4798+ *
4799+ * This program is distributed in the hope that it will be useful,
4800+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
4801+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4802+ * GNU General Public License for more details.
4803+ *
4804+ * You should have received a copy of the GNU General Public License
523b37e3 4805+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 4806+ */
4807+
4808+/*
4809+ * copy-up functions, see wbr_policy.c for copy-down
4810+ */
4811+
4812+#include <linux/fs_stack.h>
dece6358 4813+#include <linux/mm.h>
8cdd5066 4814+#include <linux/task_work.h>
1facf9fc 4815+#include "aufs.h"
4816+
86dc4139 4817+void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
1facf9fc 4818+{
4819+ const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
367653fa 4820+ | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
1facf9fc 4821+
86dc4139
AM
4822+ BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
4823+
4824+ dst->i_flags |= iflags & ~mask;
1facf9fc 4825+ if (au_test_fs_notime(dst->i_sb))
4826+ dst->i_flags |= S_NOATIME | S_NOCMTIME;
4827+}
4828+
4829+void au_cpup_attr_timesizes(struct inode *inode)
4830+{
4831+ struct inode *h_inode;
4832+
5afbbe0d 4833+ h_inode = au_h_iptr(inode, au_ibtop(inode));
1facf9fc 4834+ fsstack_copy_attr_times(inode, h_inode);
4a4d8108 4835+ fsstack_copy_inode_size(inode, h_inode);
1facf9fc 4836+}
4837+
4838+void au_cpup_attr_nlink(struct inode *inode, int force)
4839+{
4840+ struct inode *h_inode;
4841+ struct super_block *sb;
5afbbe0d 4842+ aufs_bindex_t bindex, bbot;
1facf9fc 4843+
4844+ sb = inode->i_sb;
5afbbe0d 4845+ bindex = au_ibtop(inode);
1facf9fc 4846+ h_inode = au_h_iptr(inode, bindex);
4847+ if (!force
4848+ && !S_ISDIR(h_inode->i_mode)
4849+ && au_opt_test(au_mntflags(sb), PLINK)
4850+ && au_plink_test(inode))
4851+ return;
4852+
7eafdf33
AM
4853+ /*
4854+ * 0 can happen in revalidating.
38d290e6
JR
4855+ * h_inode->i_mutex may not be held here, but it is harmless since once
4856+ * i_nlink reaches 0, it will never become positive except O_TMPFILE
4857+ * case.
4858+ * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
4859+ * the incorrect link count.
7eafdf33 4860+ */
92d182d2 4861+ set_nlink(inode, h_inode->i_nlink);
1facf9fc 4862+
4863+ /*
4864+ * fewer nlink makes find(1) noisy, but larger nlink doesn't.
4865+ * it may includes whplink directory.
4866+ */
4867+ if (S_ISDIR(h_inode->i_mode)) {
5afbbe0d
AM
4868+ bbot = au_ibbot(inode);
4869+ for (bindex++; bindex <= bbot; bindex++) {
1facf9fc 4870+ h_inode = au_h_iptr(inode, bindex);
4871+ if (h_inode)
4872+ au_add_nlink(inode, h_inode);
4873+ }
4874+ }
4875+}
4876+
4877+void au_cpup_attr_changeable(struct inode *inode)
4878+{
4879+ struct inode *h_inode;
4880+
5afbbe0d 4881+ h_inode = au_h_iptr(inode, au_ibtop(inode));
1facf9fc 4882+ inode->i_mode = h_inode->i_mode;
4883+ inode->i_uid = h_inode->i_uid;
4884+ inode->i_gid = h_inode->i_gid;
4885+ au_cpup_attr_timesizes(inode);
86dc4139 4886+ au_cpup_attr_flags(inode, h_inode->i_flags);
1facf9fc 4887+}
4888+
4889+void au_cpup_igen(struct inode *inode, struct inode *h_inode)
4890+{
4891+ struct au_iinfo *iinfo = au_ii(inode);
4892+
1308ab2a 4893+ IiMustWriteLock(inode);
4894+
1facf9fc 4895+ iinfo->ii_higen = h_inode->i_generation;
4896+ iinfo->ii_hsb1 = h_inode->i_sb;
4897+}
4898+
4899+void au_cpup_attr_all(struct inode *inode, int force)
4900+{
4901+ struct inode *h_inode;
4902+
5afbbe0d 4903+ h_inode = au_h_iptr(inode, au_ibtop(inode));
1facf9fc 4904+ au_cpup_attr_changeable(inode);
4905+ if (inode->i_nlink > 0)
4906+ au_cpup_attr_nlink(inode, force);
4907+ inode->i_rdev = h_inode->i_rdev;
4908+ inode->i_blkbits = h_inode->i_blkbits;
4909+ au_cpup_igen(inode, h_inode);
4910+}
4911+
4912+/* ---------------------------------------------------------------------- */
4913+
4914+/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
4915+
4916+/* keep the timestamps of the parent dir when cpup */
4917+void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
4918+ struct path *h_path)
4919+{
4920+ struct inode *h_inode;
4921+
4922+ dt->dt_dentry = dentry;
4923+ dt->dt_h_path = *h_path;
5527c038 4924+ h_inode = d_inode(h_path->dentry);
1facf9fc 4925+ dt->dt_atime = h_inode->i_atime;
4926+ dt->dt_mtime = h_inode->i_mtime;
4927+ /* smp_mb(); */
4928+}
4929+
4930+void au_dtime_revert(struct au_dtime *dt)
4931+{
4932+ struct iattr attr;
4933+ int err;
4934+
4935+ attr.ia_atime = dt->dt_atime;
4936+ attr.ia_mtime = dt->dt_mtime;
4937+ attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
4938+ | ATTR_ATIME | ATTR_ATIME_SET;
4939+
523b37e3
AM
4940+ /* no delegation since this is a directory */
4941+ err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
1facf9fc 4942+ if (unlikely(err))
0c3ec466 4943+ pr_warn("restoring timestamps failed(%d). ignored\n", err);
1facf9fc 4944+}
4945+
4946+/* ---------------------------------------------------------------------- */
4947+
86dc4139
AM
4948+/* internal use only */
4949+struct au_cpup_reg_attr {
4950+ int valid;
4951+ struct kstat st;
4952+ unsigned int iflags; /* inode->i_flags */
4953+};
4954+
1facf9fc 4955+static noinline_for_stack
0b2a12c6 4956+int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct path *h_src,
86dc4139 4957+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 4958+{
c1595e42 4959+ int err, sbits, icex;
7e9cd9fe
AM
4960+ unsigned int mnt_flags;
4961+ unsigned char verbose;
1facf9fc 4962+ struct iattr ia;
4963+ struct path h_path;
1308ab2a 4964+ struct inode *h_isrc, *h_idst;
86dc4139 4965+ struct kstat *h_st;
c1595e42 4966+ struct au_branch *br;
1facf9fc 4967+
c1595e42
JR
4968+ br = au_sbr(dst->d_sb, bindex);
4969+ h_path.mnt = au_br_mnt(br);
0b2a12c6
JR
4970+ h_path.dentry = au_h_dptr(dst, bindex);
4971+ h_idst = d_inode(h_path.dentry);
4972+ h_isrc = d_inode(h_src->dentry);
1308ab2a 4973+ ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
1facf9fc 4974+ | ATTR_ATIME | ATTR_MTIME
4975+ | ATTR_ATIME_SET | ATTR_MTIME_SET;
86dc4139
AM
4976+ if (h_src_attr && h_src_attr->valid) {
4977+ h_st = &h_src_attr->st;
4978+ ia.ia_uid = h_st->uid;
4979+ ia.ia_gid = h_st->gid;
4980+ ia.ia_atime = h_st->atime;
4981+ ia.ia_mtime = h_st->mtime;
4982+ if (h_idst->i_mode != h_st->mode
4983+ && !S_ISLNK(h_idst->i_mode)) {
4984+ ia.ia_valid |= ATTR_MODE;
4985+ ia.ia_mode = h_st->mode;
4986+ }
4987+ sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
4988+ au_cpup_attr_flags(h_idst, h_src_attr->iflags);
4989+ } else {
4990+ ia.ia_uid = h_isrc->i_uid;
4991+ ia.ia_gid = h_isrc->i_gid;
4992+ ia.ia_atime = h_isrc->i_atime;
4993+ ia.ia_mtime = h_isrc->i_mtime;
4994+ if (h_idst->i_mode != h_isrc->i_mode
4995+ && !S_ISLNK(h_idst->i_mode)) {
4996+ ia.ia_valid |= ATTR_MODE;
4997+ ia.ia_mode = h_isrc->i_mode;
4998+ }
4999+ sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
5000+ au_cpup_attr_flags(h_idst, h_isrc->i_flags);
1308ab2a 5001+ }
523b37e3
AM
5002+ /* no delegation since it is just created */
5003+ err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
1facf9fc 5004+
5005+ /* is this nfs only? */
5006+ if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
5007+ ia.ia_valid = ATTR_FORCE | ATTR_MODE;
5008+ ia.ia_mode = h_isrc->i_mode;
523b37e3 5009+ err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
1facf9fc 5010+ }
5011+
c1595e42 5012+ icex = br->br_perm & AuBrAttr_ICEX;
7e9cd9fe
AM
5013+ if (!err) {
5014+ mnt_flags = au_mntflags(dst->d_sb);
5015+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
0b2a12c6 5016+ err = au_cpup_xattr(&h_path, h_src, icex, verbose);
7e9cd9fe 5017+ }
c1595e42 5018+
1facf9fc 5019+ return err;
5020+}
5021+
5022+/* ---------------------------------------------------------------------- */
5023+
5024+static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
5025+ char *buf, unsigned long blksize)
5026+{
5027+ int err;
5028+ size_t sz, rbytes, wbytes;
5029+ unsigned char all_zero;
5030+ char *p, *zp;
febd17d6 5031+ struct inode *h_inode;
1facf9fc 5032+ /* reduce stack usage */
5033+ struct iattr *ia;
5034+
5035+ zp = page_address(ZERO_PAGE(0));
5036+ if (unlikely(!zp))
5037+ return -ENOMEM; /* possible? */
5038+
5039+ err = 0;
5040+ all_zero = 0;
5041+ while (len) {
5042+ AuDbg("len %lld\n", len);
5043+ sz = blksize;
5044+ if (len < blksize)
5045+ sz = len;
5046+
5047+ rbytes = 0;
5048+ /* todo: signal_pending? */
5049+ while (!rbytes || err == -EAGAIN || err == -EINTR) {
5050+ rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
5051+ err = rbytes;
5052+ }
5053+ if (unlikely(err < 0))
5054+ break;
5055+
5056+ all_zero = 0;
5057+ if (len >= rbytes && rbytes == blksize)
5058+ all_zero = !memcmp(buf, zp, rbytes);
5059+ if (!all_zero) {
5060+ wbytes = rbytes;
5061+ p = buf;
5062+ while (wbytes) {
5063+ size_t b;
5064+
5065+ b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
5066+ err = b;
5067+ /* todo: signal_pending? */
5068+ if (unlikely(err == -EAGAIN || err == -EINTR))
5069+ continue;
5070+ if (unlikely(err < 0))
5071+ break;
5072+ wbytes -= b;
5073+ p += b;
5074+ }
392086de
AM
5075+ if (unlikely(err < 0))
5076+ break;
1facf9fc 5077+ } else {
5078+ loff_t res;
5079+
5080+ AuLabel(hole);
5081+ res = vfsub_llseek(dst, rbytes, SEEK_CUR);
5082+ err = res;
5083+ if (unlikely(res < 0))
5084+ break;
5085+ }
5086+ len -= rbytes;
5087+ err = 0;
5088+ }
5089+
5090+ /* the last block may be a hole */
5091+ if (!err && all_zero) {
5092+ AuLabel(last hole);
5093+
5094+ err = 1;
2000de60 5095+ if (au_test_nfs(dst->f_path.dentry->d_sb)) {
1facf9fc 5096+ /* nfs requires this step to make last hole */
5097+ /* is this only nfs? */
5098+ do {
5099+ /* todo: signal_pending? */
5100+ err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
5101+ } while (err == -EAGAIN || err == -EINTR);
5102+ if (err == 1)
5103+ dst->f_pos--;
5104+ }
5105+
5106+ if (err == 1) {
5107+ ia = (void *)buf;
5108+ ia->ia_size = dst->f_pos;
5109+ ia->ia_valid = ATTR_SIZE | ATTR_FILE;
5110+ ia->ia_file = dst;
febd17d6
JR
5111+ h_inode = file_inode(dst);
5112+ inode_lock_nested(h_inode, AuLsc_I_CHILD2);
523b37e3
AM
5113+ /* no delegation since it is just created */
5114+ err = vfsub_notify_change(&dst->f_path, ia,
5115+ /*delegated*/NULL);
febd17d6 5116+ inode_unlock(h_inode);
1facf9fc 5117+ }
5118+ }
5119+
5120+ return err;
5121+}
5122+
5123+int au_copy_file(struct file *dst, struct file *src, loff_t len)
5124+{
5125+ int err;
5126+ unsigned long blksize;
5127+ unsigned char do_kfree;
5128+ char *buf;
9f237c51 5129+ struct super_block *h_sb;
1facf9fc 5130+
5131+ err = -ENOMEM;
9f237c51
AM
5132+ h_sb = file_inode(dst)->i_sb;
5133+ blksize = h_sb->s_blocksize;
1facf9fc 5134+ if (!blksize || PAGE_SIZE < blksize)
5135+ blksize = PAGE_SIZE;
5136+ AuDbg("blksize %lu\n", blksize);
5137+ do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
5138+ if (do_kfree)
5139+ buf = kmalloc(blksize, GFP_NOFS);
5140+ else
5141+ buf = (void *)__get_free_page(GFP_NOFS);
5142+ if (unlikely(!buf))
5143+ goto out;
5144+
5145+ if (len > (1 << 22))
5146+ AuDbg("copying a large file %lld\n", (long long)len);
5147+
5148+ src->f_pos = 0;
5149+ dst->f_pos = 0;
5150+ err = au_do_copy_file(dst, src, len, buf, blksize);
9f237c51
AM
5151+ if (do_kfree) {
5152+ AuDebugOn(!au_kfree_do_sz_test(blksize));
5153+ au_kfree_do_rcu(buf);
5154+ } else
1c60b727 5155+ free_page((unsigned long)buf);
1facf9fc 5156+
4f0767ce 5157+out:
1facf9fc 5158+ return err;
5159+}
5160+
1c60b727
AM
5161+static int au_do_copy(struct file *dst, struct file *src, loff_t len)
5162+{
5163+ int err;
5164+ struct super_block *h_src_sb;
5165+ struct inode *h_src_inode;
5166+
5167+ h_src_inode = file_inode(src);
5168+ h_src_sb = h_src_inode->i_sb;
5169+
5170+ /* XFS acquires inode_lock */
5171+ if (!au_test_xfs(h_src_sb))
5172+ err = au_copy_file(dst, src, len);
5173+ else {
3c1bdaff 5174+ inode_unlock_shared(h_src_inode);
1c60b727 5175+ err = au_copy_file(dst, src, len);
be118d29 5176+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
1c60b727
AM
5177+ }
5178+
5179+ return err;
5180+}
5181+
5182+static int au_clone_or_copy(struct file *dst, struct file *src, loff_t len)
5183+{
5184+ int err;
9f237c51 5185+ loff_t lo;
1c60b727
AM
5186+ struct super_block *h_src_sb;
5187+ struct inode *h_src_inode;
5188+
5189+ h_src_inode = file_inode(src);
5190+ h_src_sb = h_src_inode->i_sb;
5191+ if (h_src_sb != file_inode(dst)->i_sb
9f237c51 5192+ || !dst->f_op->remap_file_range) {
1c60b727
AM
5193+ err = au_do_copy(dst, src, len);
5194+ goto out;
5195+ }
5196+
5197+ if (!au_test_nfs(h_src_sb)) {
3c1bdaff 5198+ inode_unlock_shared(h_src_inode);
9f237c51 5199+ lo = vfsub_clone_file_range(src, dst, len);
be118d29 5200+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
1c60b727 5201+ } else
9f237c51
AM
5202+ lo = vfsub_clone_file_range(src, dst, len);
5203+ if (lo == len) {
5204+ err = 0;
5205+ goto out; /* success */
5206+ } else if (lo >= 0)
5207+ /* todo: possible? */
5208+ /* paritially succeeded */
5209+ AuDbg("lo %lld, len %lld. Retrying.\n", lo, len);
5210+ else if (lo != -EOPNOTSUPP) {
5211+ /* older XFS has a condition in cloning */
5212+ err = lo;
1c60b727 5213+ goto out;
9f237c51 5214+ }
1c60b727
AM
5215+
5216+ /* the backend fs on NFS may not support cloning */
5217+ err = au_do_copy(dst, src, len);
5218+
5219+out:
5220+ AuTraceErr(err);
5221+ return err;
5222+}
5223+
1facf9fc 5224+/*
5225+ * to support a sparse file which is opened with O_APPEND,
5226+ * we need to close the file.
5227+ */
c2b27bf2 5228+static int au_cp_regular(struct au_cp_generic *cpg)
1facf9fc 5229+{
5230+ int err, i;
5231+ enum { SRC, DST };
5232+ struct {
5233+ aufs_bindex_t bindex;
5234+ unsigned int flags;
5235+ struct dentry *dentry;
392086de 5236+ int force_wr;
1facf9fc 5237+ struct file *file;
1facf9fc 5238+ } *f, file[] = {
5239+ {
c2b27bf2 5240+ .bindex = cpg->bsrc,
1facf9fc 5241+ .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
1facf9fc 5242+ },
5243+ {
c2b27bf2 5244+ .bindex = cpg->bdst,
1facf9fc 5245+ .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
392086de 5246+ .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
1facf9fc 5247+ }
5248+ };
acd2b654 5249+ struct au_branch *br;
521ced18 5250+ struct super_block *sb, *h_src_sb;
e2f27e51 5251+ struct inode *h_src_inode;
8cdd5066 5252+ struct task_struct *tsk = current;
1facf9fc 5253+
5254+ /* bsrc branch can be ro/rw. */
c2b27bf2 5255+ sb = cpg->dentry->d_sb;
1facf9fc 5256+ f = file;
5257+ for (i = 0; i < 2; i++, f++) {
c2b27bf2
AM
5258+ f->dentry = au_h_dptr(cpg->dentry, f->bindex);
5259+ f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
392086de 5260+ /*file*/NULL, f->force_wr);
9f237c51
AM
5261+ if (IS_ERR(f->file)) {
5262+ err = PTR_ERR(f->file);
5263+ if (i == SRC)
5264+ goto out;
5265+ else
5266+ goto out_src;
5267+ }
1facf9fc 5268+ }
5269+
5270+ /* try stopping to update while we copyup */
e2f27e51 5271+ h_src_inode = d_inode(file[SRC].dentry);
521ced18
JR
5272+ h_src_sb = h_src_inode->i_sb;
5273+ if (!au_test_nfs(h_src_sb))
e2f27e51 5274+ IMustLock(h_src_inode);
1c60b727 5275+ err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
1facf9fc 5276+
8cdd5066
JR
5277+ /* i wonder if we had O_NO_DELAY_FPUT flag */
5278+ if (tsk->flags & PF_KTHREAD)
5279+ __fput_sync(file[DST].file);
5280+ else {
062440b3 5281+ /* it happened actually */
8cdd5066
JR
5282+ fput(file[DST].file);
5283+ /*
5284+ * too bad.
5285+ * we have to call both since we don't know which place the file
5286+ * was added to.
5287+ */
5288+ task_work_run();
5289+ flush_delayed_fput();
5290+ }
acd2b654
AM
5291+ br = au_sbr(sb, file[DST].bindex);
5292+ au_lcnt_dec(&br->br_nfiles);
523b37e3 5293+
4f0767ce 5294+out_src:
1facf9fc 5295+ fput(file[SRC].file);
acd2b654
AM
5296+ br = au_sbr(sb, file[SRC].bindex);
5297+ au_lcnt_dec(&br->br_nfiles);
4f0767ce 5298+out:
1facf9fc 5299+ return err;
5300+}
5301+
c2b27bf2 5302+static int au_do_cpup_regular(struct au_cp_generic *cpg,
86dc4139 5303+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 5304+{
5305+ int err, rerr;
5306+ loff_t l;
86dc4139 5307+ struct path h_path;
38d290e6 5308+ struct inode *h_src_inode, *h_dst_inode;
1facf9fc 5309+
5310+ err = 0;
5527c038 5311+ h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
86dc4139 5312+ l = i_size_read(h_src_inode);
c2b27bf2
AM
5313+ if (cpg->len == -1 || l < cpg->len)
5314+ cpg->len = l;
5315+ if (cpg->len) {
86dc4139 5316+ /* try stopping to update while we are referencing */
be118d29 5317+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
c2b27bf2 5318+ au_pin_hdir_unlock(cpg->pin);
1facf9fc 5319+
c2b27bf2
AM
5320+ h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5321+ h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
86dc4139 5322+ h_src_attr->iflags = h_src_inode->i_flags;
5527c038 5323+ if (!au_test_nfs(h_src_inode->i_sb))
521ced18 5324+ err = vfsub_getattr(&h_path, &h_src_attr->st);
5527c038 5325+ else {
3c1bdaff 5326+ inode_unlock_shared(h_src_inode);
521ced18 5327+ err = vfsub_getattr(&h_path, &h_src_attr->st);
be118d29 5328+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5527c038 5329+ }
86dc4139 5330+ if (unlikely(err)) {
3c1bdaff 5331+ inode_unlock_shared(h_src_inode);
86dc4139
AM
5332+ goto out;
5333+ }
5334+ h_src_attr->valid = 1;
e2f27e51
AM
5335+ if (!au_test_nfs(h_src_inode->i_sb)) {
5336+ err = au_cp_regular(cpg);
3c1bdaff 5337+ inode_unlock_shared(h_src_inode);
e2f27e51 5338+ } else {
3c1bdaff 5339+ inode_unlock_shared(h_src_inode);
e2f27e51
AM
5340+ err = au_cp_regular(cpg);
5341+ }
c2b27bf2 5342+ rerr = au_pin_hdir_relock(cpg->pin);
86dc4139
AM
5343+ if (!err && rerr)
5344+ err = rerr;
1facf9fc 5345+ }
38d290e6
JR
5346+ if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5347+ h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5527c038 5348+ h_dst_inode = d_inode(h_path.dentry);
38d290e6
JR
5349+ spin_lock(&h_dst_inode->i_lock);
5350+ h_dst_inode->i_state |= I_LINKABLE;
5351+ spin_unlock(&h_dst_inode->i_lock);
5352+ }
1facf9fc 5353+
4f0767ce 5354+out:
1facf9fc 5355+ return err;
5356+}
5357+
5358+static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5359+ struct inode *h_dir)
5360+{
e37dd06a
AM
5361+ int err;
5362+ DEFINE_DELAYED_CALL(done);
5363+ const char *sym;
1facf9fc 5364+
e37dd06a
AM
5365+ sym = vfs_get_link(h_src, &done);
5366+ err = PTR_ERR(sym);
5367+ if (IS_ERR(sym))
1facf9fc 5368+ goto out;
5369+
e37dd06a 5370+ err = vfsub_symlink(h_dir, h_path, sym);
1facf9fc 5371+
4f0767ce 5372+out:
e37dd06a 5373+ do_delayed_call(&done);
1facf9fc 5374+ return err;
5375+}
5376+
8cdd5066
JR
5377+/*
5378+ * regardless 'acl' option, reset all ACL.
5379+ * All ACL will be copied up later from the original entry on the lower branch.
5380+ */
5381+static int au_reset_acl(struct inode *h_dir, struct path *h_path, umode_t mode)
5382+{
5383+ int err;
5384+ struct dentry *h_dentry;
5385+ struct inode *h_inode;
0b2a12c6 5386+ struct user_namespace *h_userns;
8cdd5066 5387+
0b2a12c6 5388+ h_userns = mnt_user_ns(h_path->mnt);
8cdd5066
JR
5389+ h_dentry = h_path->dentry;
5390+ h_inode = d_inode(h_dentry);
5391+ /* forget_all_cached_acls(h_inode)); */
0b2a12c6 5392+ err = vfsub_removexattr(h_userns, h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
8cdd5066
JR
5393+ AuTraceErr(err);
5394+ if (err == -EOPNOTSUPP)
5395+ err = 0;
5396+ if (!err)
0b2a12c6 5397+ err = vfsub_acl_chmod(h_userns, h_inode, mode);
8cdd5066
JR
5398+
5399+ AuTraceErr(err);
5400+ return err;
5401+}
5402+
5403+static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5404+ struct inode *h_dir, struct path *h_path)
5405+{
5406+ int err;
5407+ struct inode *dir, *inode;
0b2a12c6 5408+ struct user_namespace *h_userns;
8cdd5066 5409+
0b2a12c6
JR
5410+ h_userns = mnt_user_ns(h_path->mnt);
5411+ err = vfsub_removexattr(h_userns, h_path->dentry,
5412+ XATTR_NAME_POSIX_ACL_DEFAULT);
8cdd5066
JR
5413+ AuTraceErr(err);
5414+ if (err == -EOPNOTSUPP)
5415+ err = 0;
5416+ if (unlikely(err))
5417+ goto out;
5418+
5419+ /*
5420+ * strange behaviour from the users view,
acd2b654 5421+ * particularly setattr case
8cdd5066
JR
5422+ */
5423+ dir = d_inode(dst_parent);
5afbbe0d 5424+ if (au_ibtop(dir) == cpg->bdst)
8cdd5066
JR
5425+ au_cpup_attr_nlink(dir, /*force*/1);
5426+ inode = d_inode(cpg->dentry);
5427+ au_cpup_attr_nlink(inode, /*force*/1);
5428+
5429+out:
5430+ return err;
5431+}
5432+
1facf9fc 5433+static noinline_for_stack
c2b27bf2 5434+int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
86dc4139 5435+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 5436+{
5437+ int err;
5438+ umode_t mode;
5439+ unsigned int mnt_flags;
076b876e 5440+ unsigned char isdir, isreg, force;
c2b27bf2 5441+ const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
1facf9fc 5442+ struct au_dtime dt;
5443+ struct path h_path;
5444+ struct dentry *h_src, *h_dst, *h_parent;
8cdd5066 5445+ struct inode *h_inode, *h_dir;
1facf9fc 5446+ struct super_block *sb;
5447+
5448+ /* bsrc branch can be ro/rw. */
c2b27bf2 5449+ h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038
JR
5450+ h_inode = d_inode(h_src);
5451+ AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
1facf9fc 5452+
5453+ /* try stopping to be referenced while we are creating */
c2b27bf2
AM
5454+ h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5455+ if (au_ftest_cpup(cpg->flags, RENAME))
86dc4139
AM
5456+ AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5457+ AUFS_WH_PFX_LEN));
1facf9fc 5458+ h_parent = h_dst->d_parent; /* dir inode is locked */
5527c038 5459+ h_dir = d_inode(h_parent);
1facf9fc 5460+ IMustLock(h_dir);
5461+ AuDebugOn(h_parent != h_dst->d_parent);
5462+
c2b27bf2
AM
5463+ sb = cpg->dentry->d_sb;
5464+ h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
1facf9fc 5465+ if (do_dt) {
5466+ h_path.dentry = h_parent;
5467+ au_dtime_store(&dt, dst_parent, &h_path);
5468+ }
5469+ h_path.dentry = h_dst;
5470+
076b876e 5471+ isreg = 0;
1facf9fc 5472+ isdir = 0;
5473+ mode = h_inode->i_mode;
5474+ switch (mode & S_IFMT) {
5475+ case S_IFREG:
076b876e 5476+ isreg = 1;
cd7a4cd9 5477+ err = vfsub_create(h_dir, &h_path, 0600, /*want_excl*/true);
1facf9fc 5478+ if (!err)
c2b27bf2 5479+ err = au_do_cpup_regular(cpg, h_src_attr);
1facf9fc 5480+ break;
5481+ case S_IFDIR:
5482+ isdir = 1;
5483+ err = vfsub_mkdir(h_dir, &h_path, mode);
8cdd5066
JR
5484+ if (!err)
5485+ err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
1facf9fc 5486+ break;
5487+ case S_IFLNK:
5488+ err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5489+ break;
5490+ case S_IFCHR:
5491+ case S_IFBLK:
5492+ AuDebugOn(!capable(CAP_MKNOD));
42a736d3 5493+ fallthrough;
1facf9fc 5494+ case S_IFIFO:
5495+ case S_IFSOCK:
5496+ err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5497+ break;
5498+ default:
5499+ AuIOErr("Unknown inode type 0%o\n", mode);
5500+ err = -EIO;
5501+ }
8cdd5066
JR
5502+ if (!err)
5503+ err = au_reset_acl(h_dir, &h_path, mode);
1facf9fc 5504+
5505+ mnt_flags = au_mntflags(sb);
5506+ if (!au_opt_test(mnt_flags, UDBA_NONE)
5507+ && !isdir
5508+ && au_opt_test(mnt_flags, XINO)
38d290e6
JR
5509+ && (h_inode->i_nlink == 1
5510+ || (h_inode->i_state & I_LINKABLE))
1facf9fc 5511+ /* todo: unnecessary? */
5527c038 5512+ /* && d_inode(cpg->dentry)->i_nlink == 1 */
c2b27bf2
AM
5513+ && cpg->bdst < cpg->bsrc
5514+ && !au_ftest_cpup(cpg->flags, KEEPLINO))
5515+ au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
1facf9fc 5516+ /* ignore this error */
5517+
076b876e
AM
5518+ if (!err) {
5519+ force = 0;
5520+ if (isreg) {
5521+ force = !!cpg->len;
5522+ if (cpg->len == -1)
5523+ force = !!i_size_read(h_inode);
5524+ }
5525+ au_fhsm_wrote(sb, cpg->bdst, force);
5526+ }
5527+
1facf9fc 5528+ if (do_dt)
5529+ au_dtime_revert(&dt);
5530+ return err;
5531+}
5532+
392086de 5533+static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
86dc4139
AM
5534+{
5535+ int err;
392086de 5536+ struct dentry *dentry, *h_dentry, *h_parent, *parent;
86dc4139 5537+ struct inode *h_dir;
392086de 5538+ aufs_bindex_t bdst;
86dc4139 5539+
392086de
AM
5540+ dentry = cpg->dentry;
5541+ bdst = cpg->bdst;
5542+ h_dentry = au_h_dptr(dentry, bdst);
5543+ if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5544+ dget(h_dentry);
5545+ au_set_h_dptr(dentry, bdst, NULL);
5546+ err = au_lkup_neg(dentry, bdst, /*wh*/0);
5547+ if (!err)
5548+ h_path->dentry = dget(au_h_dptr(dentry, bdst));
86dc4139 5549+ au_set_h_dptr(dentry, bdst, h_dentry);
392086de
AM
5550+ } else {
5551+ err = 0;
5552+ parent = dget_parent(dentry);
5553+ h_parent = au_h_dptr(parent, bdst);
5554+ dput(parent);
5555+ h_path->dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
5556+ if (IS_ERR(h_path->dentry))
5557+ err = PTR_ERR(h_path->dentry);
86dc4139 5558+ }
392086de
AM
5559+ if (unlikely(err))
5560+ goto out;
86dc4139 5561+
86dc4139 5562+ h_parent = h_dentry->d_parent; /* dir inode is locked */
5527c038 5563+ h_dir = d_inode(h_parent);
86dc4139 5564+ IMustLock(h_dir);
523b37e3
AM
5565+ AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5566+ /* no delegation since it is just created */
f2c43d5f
AM
5567+ err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
5568+ /*flags*/0);
86dc4139
AM
5569+ dput(h_path->dentry);
5570+
5571+out:
5572+ return err;
5573+}
5574+
1facf9fc 5575+/*
5576+ * copyup the @dentry from @bsrc to @bdst.
5577+ * the caller must set the both of lower dentries.
5578+ * @len is for truncating when it is -1 copyup the entire file.
5579+ * in link/rename cases, @dst_parent may be different from the real one.
c2b27bf2 5580+ * basic->bsrc can be larger than basic->bdst.
f2c43d5f 5581+ * aufs doesn't touch the credential so
acd2b654 5582+ * security_inode_copy_up{,_xattr}() are unnecessary.
1facf9fc 5583+ */
c2b27bf2 5584+static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
1facf9fc 5585+{
5586+ int err, rerr;
5afbbe0d 5587+ aufs_bindex_t old_ibtop;
1facf9fc 5588+ unsigned char isdir, plink;
1facf9fc 5589+ struct dentry *h_src, *h_dst, *h_parent;
5527c038 5590+ struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
1facf9fc 5591+ struct super_block *sb;
86dc4139 5592+ struct au_branch *br;
0b2a12c6 5593+ struct path h_src_path;
acd2b654 5594+ /* to reduce stack size */
c2b27bf2
AM
5595+ struct {
5596+ struct au_dtime dt;
5597+ struct path h_path;
5598+ struct au_cpup_reg_attr h_src_attr;
5599+ } *a;
1facf9fc 5600+
c2b27bf2
AM
5601+ err = -ENOMEM;
5602+ a = kmalloc(sizeof(*a), GFP_NOFS);
5603+ if (unlikely(!a))
5604+ goto out;
5605+ a->h_src_attr.valid = 0;
1facf9fc 5606+
c2b27bf2
AM
5607+ sb = cpg->dentry->d_sb;
5608+ br = au_sbr(sb, cpg->bdst);
5609+ a->h_path.mnt = au_br_mnt(br);
5610+ h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
1facf9fc 5611+ h_parent = h_dst->d_parent; /* dir inode is locked */
5527c038 5612+ h_dir = d_inode(h_parent);
1facf9fc 5613+ IMustLock(h_dir);
5614+
c2b27bf2 5615+ h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038 5616+ inode = d_inode(cpg->dentry);
1facf9fc 5617+
5618+ if (!dst_parent)
c2b27bf2 5619+ dst_parent = dget_parent(cpg->dentry);
1facf9fc 5620+ else
5621+ dget(dst_parent);
5622+
5623+ plink = !!au_opt_test(au_mntflags(sb), PLINK);
c2b27bf2 5624+ dst_inode = au_h_iptr(inode, cpg->bdst);
1facf9fc 5625+ if (dst_inode) {
5626+ if (unlikely(!plink)) {
5627+ err = -EIO;
027c5e7a
AM
5628+ AuIOErr("hi%lu(i%lu) exists on b%d "
5629+ "but plink is disabled\n",
c2b27bf2
AM
5630+ dst_inode->i_ino, inode->i_ino, cpg->bdst);
5631+ goto out_parent;
1facf9fc 5632+ }
5633+
5634+ if (dst_inode->i_nlink) {
c2b27bf2 5635+ const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
1facf9fc 5636+
c2b27bf2 5637+ h_src = au_plink_lkup(inode, cpg->bdst);
1facf9fc 5638+ err = PTR_ERR(h_src);
5639+ if (IS_ERR(h_src))
c2b27bf2 5640+ goto out_parent;
5527c038 5641+ if (unlikely(d_is_negative(h_src))) {
1facf9fc 5642+ err = -EIO;
79b8bda9 5643+ AuIOErr("i%lu exists on b%d "
027c5e7a 5644+ "but not pseudo-linked\n",
79b8bda9 5645+ inode->i_ino, cpg->bdst);
1facf9fc 5646+ dput(h_src);
c2b27bf2 5647+ goto out_parent;
1facf9fc 5648+ }
5649+
5650+ if (do_dt) {
c2b27bf2
AM
5651+ a->h_path.dentry = h_parent;
5652+ au_dtime_store(&a->dt, dst_parent, &a->h_path);
1facf9fc 5653+ }
86dc4139 5654+
c2b27bf2 5655+ a->h_path.dentry = h_dst;
523b37e3
AM
5656+ delegated = NULL;
5657+ err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
c2b27bf2 5658+ if (!err && au_ftest_cpup(cpg->flags, RENAME))
392086de 5659+ err = au_do_ren_after_cpup(cpg, &a->h_path);
1facf9fc 5660+ if (do_dt)
c2b27bf2 5661+ au_dtime_revert(&a->dt);
523b37e3
AM
5662+ if (unlikely(err == -EWOULDBLOCK)) {
5663+ pr_warn("cannot retry for NFSv4 delegation"
5664+ " for an internal link\n");
5665+ iput(delegated);
5666+ }
1facf9fc 5667+ dput(h_src);
c2b27bf2 5668+ goto out_parent;
1facf9fc 5669+ } else
5670+ /* todo: cpup_wh_file? */
5671+ /* udba work */
4a4d8108 5672+ au_update_ibrange(inode, /*do_put_zero*/1);
1facf9fc 5673+ }
5674+
86dc4139 5675+ isdir = S_ISDIR(inode->i_mode);
5afbbe0d 5676+ old_ibtop = au_ibtop(inode);
c2b27bf2 5677+ err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
1facf9fc 5678+ if (unlikely(err))
86dc4139 5679+ goto out_rev;
5527c038 5680+ dst_inode = d_inode(h_dst);
febd17d6 5681+ inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
86dc4139 5682+ /* todo: necessary? */
c2b27bf2 5683+ /* au_pin_hdir_unlock(cpg->pin); */
1facf9fc 5684+
0b2a12c6
JR
5685+ h_src_path.dentry = h_src;
5686+ h_src_path.mnt = au_sbr_mnt(sb, cpg->bsrc);
5687+ err = cpup_iattr(cpg->dentry, cpg->bdst, &h_src_path, &a->h_src_attr);
86dc4139
AM
5688+ if (unlikely(err)) {
5689+ /* todo: necessary? */
c2b27bf2 5690+ /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
febd17d6 5691+ inode_unlock(dst_inode);
86dc4139
AM
5692+ goto out_rev;
5693+ }
5694+
5afbbe0d 5695+ if (cpg->bdst < old_ibtop) {
86dc4139 5696+ if (S_ISREG(inode->i_mode)) {
c2b27bf2 5697+ err = au_dy_iaop(inode, cpg->bdst, dst_inode);
86dc4139 5698+ if (unlikely(err)) {
c2b27bf2
AM
5699+ /* ignore an error */
5700+ /* au_pin_hdir_relock(cpg->pin); */
febd17d6 5701+ inode_unlock(dst_inode);
86dc4139 5702+ goto out_rev;
4a4d8108 5703+ }
4a4d8108 5704+ }
5afbbe0d 5705+ au_set_ibtop(inode, cpg->bdst);
c2b27bf2 5706+ } else
5afbbe0d 5707+ au_set_ibbot(inode, cpg->bdst);
c2b27bf2 5708+ au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
86dc4139
AM
5709+ au_hi_flags(inode, isdir));
5710+
5711+ /* todo: necessary? */
c2b27bf2 5712+ /* err = au_pin_hdir_relock(cpg->pin); */
febd17d6 5713+ inode_unlock(dst_inode);
86dc4139
AM
5714+ if (unlikely(err))
5715+ goto out_rev;
5716+
5527c038 5717+ src_inode = d_inode(h_src);
86dc4139 5718+ if (!isdir
5527c038
JR
5719+ && (src_inode->i_nlink > 1
5720+ || src_inode->i_state & I_LINKABLE)
86dc4139 5721+ && plink)
c2b27bf2 5722+ au_plink_append(inode, cpg->bdst, h_dst);
86dc4139 5723+
c2b27bf2
AM
5724+ if (au_ftest_cpup(cpg->flags, RENAME)) {
5725+ a->h_path.dentry = h_dst;
392086de 5726+ err = au_do_ren_after_cpup(cpg, &a->h_path);
86dc4139
AM
5727+ }
5728+ if (!err)
c2b27bf2 5729+ goto out_parent; /* success */
1facf9fc 5730+
5731+ /* revert */
4a4d8108 5732+out_rev:
c2b27bf2
AM
5733+ a->h_path.dentry = h_parent;
5734+ au_dtime_store(&a->dt, dst_parent, &a->h_path);
5735+ a->h_path.dentry = h_dst;
86dc4139 5736+ rerr = 0;
5527c038 5737+ if (d_is_positive(h_dst)) {
523b37e3
AM
5738+ if (!isdir) {
5739+ /* no delegation since it is just created */
5740+ rerr = vfsub_unlink(h_dir, &a->h_path,
5741+ /*delegated*/NULL, /*force*/0);
5742+ } else
c2b27bf2 5743+ rerr = vfsub_rmdir(h_dir, &a->h_path);
86dc4139 5744+ }
c2b27bf2 5745+ au_dtime_revert(&a->dt);
1facf9fc 5746+ if (rerr) {
5747+ AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5748+ err = -EIO;
5749+ }
c2b27bf2 5750+out_parent:
1facf9fc 5751+ dput(dst_parent);
9f237c51 5752+ au_kfree_rcu(a);
c2b27bf2 5753+out:
1facf9fc 5754+ return err;
5755+}
5756+
7e9cd9fe 5757+#if 0 /* reserved */
1facf9fc 5758+struct au_cpup_single_args {
5759+ int *errp;
c2b27bf2 5760+ struct au_cp_generic *cpg;
1facf9fc 5761+ struct dentry *dst_parent;
5762+};
5763+
5764+static void au_call_cpup_single(void *args)
5765+{
5766+ struct au_cpup_single_args *a = args;
86dc4139 5767+
c2b27bf2
AM
5768+ au_pin_hdir_acquire_nest(a->cpg->pin);
5769+ *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5770+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 5771+}
c2b27bf2 5772+#endif
1facf9fc 5773+
53392da6
AM
5774+/*
5775+ * prevent SIGXFSZ in copy-up.
5776+ * testing CAP_MKNOD is for generic fs,
5777+ * but CAP_FSETID is for xfs only, currently.
5778+ */
86dc4139 5779+static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
53392da6
AM
5780+{
5781+ int do_sio;
86dc4139
AM
5782+ struct super_block *sb;
5783+ struct inode *h_dir;
53392da6
AM
5784+
5785+ do_sio = 0;
86dc4139 5786+ sb = au_pinned_parent(pin)->d_sb;
53392da6
AM
5787+ if (!au_wkq_test()
5788+ && (!au_sbi(sb)->si_plink_maint_pid
5789+ || au_plink_maint(sb, AuLock_NOPLM))) {
5790+ switch (mode & S_IFMT) {
5791+ case S_IFREG:
5792+ /* no condition about RLIMIT_FSIZE and the file size */
5793+ do_sio = 1;
5794+ break;
5795+ case S_IFCHR:
5796+ case S_IFBLK:
5797+ do_sio = !capable(CAP_MKNOD);
5798+ break;
5799+ }
5800+ if (!do_sio)
5801+ do_sio = ((mode & (S_ISUID | S_ISGID))
5802+ && !capable(CAP_FSETID));
86dc4139
AM
5803+ /* this workaround may be removed in the future */
5804+ if (!do_sio) {
5805+ h_dir = au_pinned_h_dir(pin);
5806+ do_sio = h_dir->i_mode & S_ISVTX;
5807+ }
53392da6
AM
5808+ }
5809+
5810+ return do_sio;
5811+}
5812+
7e9cd9fe 5813+#if 0 /* reserved */
c2b27bf2 5814+int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
1facf9fc 5815+{
5816+ int err, wkq_err;
1facf9fc 5817+ struct dentry *h_dentry;
5818+
c2b27bf2 5819+ h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038 5820+ if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
c2b27bf2 5821+ err = au_cpup_single(cpg, dst_parent);
1facf9fc 5822+ else {
5823+ struct au_cpup_single_args args = {
5824+ .errp = &err,
c2b27bf2
AM
5825+ .cpg = cpg,
5826+ .dst_parent = dst_parent
1facf9fc 5827+ };
5828+ wkq_err = au_wkq_wait(au_call_cpup_single, &args);
5829+ if (unlikely(wkq_err))
5830+ err = wkq_err;
5831+ }
5832+
5833+ return err;
5834+}
c2b27bf2 5835+#endif
1facf9fc 5836+
5837+/*
5838+ * copyup the @dentry from the first active lower branch to @bdst,
5839+ * using au_cpup_single().
5840+ */
c2b27bf2 5841+static int au_cpup_simple(struct au_cp_generic *cpg)
1facf9fc 5842+{
5843+ int err;
c2b27bf2
AM
5844+ unsigned int flags_orig;
5845+ struct dentry *dentry;
5846+
5847+ AuDebugOn(cpg->bsrc < 0);
1facf9fc 5848+
c2b27bf2 5849+ dentry = cpg->dentry;
86dc4139 5850+ DiMustWriteLock(dentry);
1facf9fc 5851+
c2b27bf2 5852+ err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
1facf9fc 5853+ if (!err) {
c2b27bf2
AM
5854+ flags_orig = cpg->flags;
5855+ au_fset_cpup(cpg->flags, RENAME);
5856+ err = au_cpup_single(cpg, NULL);
5857+ cpg->flags = flags_orig;
1facf9fc 5858+ if (!err)
5859+ return 0; /* success */
5860+
5861+ /* revert */
c2b27bf2 5862+ au_set_h_dptr(dentry, cpg->bdst, NULL);
5afbbe0d 5863+ au_set_dbtop(dentry, cpg->bsrc);
1facf9fc 5864+ }
5865+
5866+ return err;
5867+}
5868+
5869+struct au_cpup_simple_args {
5870+ int *errp;
c2b27bf2 5871+ struct au_cp_generic *cpg;
1facf9fc 5872+};
5873+
5874+static void au_call_cpup_simple(void *args)
5875+{
5876+ struct au_cpup_simple_args *a = args;
86dc4139 5877+
c2b27bf2
AM
5878+ au_pin_hdir_acquire_nest(a->cpg->pin);
5879+ *a->errp = au_cpup_simple(a->cpg);
5880+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 5881+}
5882+
c2b27bf2 5883+static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
1facf9fc 5884+{
5885+ int err, wkq_err;
c2b27bf2
AM
5886+ struct dentry *dentry, *parent;
5887+ struct file *h_file;
1facf9fc 5888+ struct inode *h_dir;
0b2a12c6 5889+ struct user_namespace *h_userns;
1facf9fc 5890+
c2b27bf2
AM
5891+ dentry = cpg->dentry;
5892+ h_file = NULL;
5893+ if (au_ftest_cpup(cpg->flags, HOPEN)) {
5894+ AuDebugOn(cpg->bsrc < 0);
392086de 5895+ h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
c2b27bf2
AM
5896+ err = PTR_ERR(h_file);
5897+ if (IS_ERR(h_file))
5898+ goto out;
5899+ }
5900+
1facf9fc 5901+ parent = dget_parent(dentry);
5527c038 5902+ h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
0b2a12c6
JR
5903+ h_userns = au_sbr_userns(dentry->d_sb, cpg->bdst);
5904+ if (!au_test_h_perm_sio(h_userns, h_dir, MAY_EXEC | MAY_WRITE)
5527c038 5905+ && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
c2b27bf2 5906+ err = au_cpup_simple(cpg);
1facf9fc 5907+ else {
5908+ struct au_cpup_simple_args args = {
5909+ .errp = &err,
c2b27bf2 5910+ .cpg = cpg
1facf9fc 5911+ };
5912+ wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
5913+ if (unlikely(wkq_err))
5914+ err = wkq_err;
5915+ }
5916+
5917+ dput(parent);
c2b27bf2
AM
5918+ if (h_file)
5919+ au_h_open_post(dentry, cpg->bsrc, h_file);
5920+
5921+out:
1facf9fc 5922+ return err;
5923+}
5924+
c2b27bf2 5925+int au_sio_cpup_simple(struct au_cp_generic *cpg)
367653fa 5926+{
5afbbe0d 5927+ aufs_bindex_t bsrc, bbot;
c2b27bf2 5928+ struct dentry *dentry, *h_dentry;
367653fa 5929+
c2b27bf2
AM
5930+ if (cpg->bsrc < 0) {
5931+ dentry = cpg->dentry;
5afbbe0d
AM
5932+ bbot = au_dbbot(dentry);
5933+ for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
c2b27bf2
AM
5934+ h_dentry = au_h_dptr(dentry, bsrc);
5935+ if (h_dentry) {
5527c038 5936+ AuDebugOn(d_is_negative(h_dentry));
c2b27bf2
AM
5937+ break;
5938+ }
5939+ }
5afbbe0d 5940+ AuDebugOn(bsrc > bbot);
c2b27bf2 5941+ cpg->bsrc = bsrc;
367653fa 5942+ }
c2b27bf2
AM
5943+ AuDebugOn(cpg->bsrc <= cpg->bdst);
5944+ return au_do_sio_cpup_simple(cpg);
5945+}
367653fa 5946+
c2b27bf2
AM
5947+int au_sio_cpdown_simple(struct au_cp_generic *cpg)
5948+{
5949+ AuDebugOn(cpg->bdst <= cpg->bsrc);
5950+ return au_do_sio_cpup_simple(cpg);
367653fa
AM
5951+}
5952+
1facf9fc 5953+/* ---------------------------------------------------------------------- */
5954+
5955+/*
5956+ * copyup the deleted file for writing.
5957+ */
c2b27bf2
AM
5958+static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
5959+ struct file *file)
1facf9fc 5960+{
5961+ int err;
c2b27bf2
AM
5962+ unsigned int flags_orig;
5963+ aufs_bindex_t bsrc_orig;
c2b27bf2 5964+ struct au_dinfo *dinfo;
5afbbe0d
AM
5965+ struct {
5966+ struct au_hdentry *hd;
5967+ struct dentry *h_dentry;
5968+ } hdst, hsrc;
1facf9fc 5969+
c2b27bf2 5970+ dinfo = au_di(cpg->dentry);
1308ab2a 5971+ AuRwMustWriteLock(&dinfo->di_rwsem);
5972+
c2b27bf2 5973+ bsrc_orig = cpg->bsrc;
5afbbe0d
AM
5974+ cpg->bsrc = dinfo->di_btop;
5975+ hdst.hd = au_hdentry(dinfo, cpg->bdst);
5976+ hdst.h_dentry = hdst.hd->hd_dentry;
5977+ hdst.hd->hd_dentry = wh_dentry;
5978+ dinfo->di_btop = cpg->bdst;
5979+
5980+ hsrc.h_dentry = NULL;
027c5e7a 5981+ if (file) {
5afbbe0d
AM
5982+ hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
5983+ hsrc.h_dentry = hsrc.hd->hd_dentry;
5984+ hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
027c5e7a 5985+ }
c2b27bf2
AM
5986+ flags_orig = cpg->flags;
5987+ cpg->flags = !AuCpup_DTIME;
5988+ err = au_cpup_single(cpg, /*h_parent*/NULL);
5989+ cpg->flags = flags_orig;
027c5e7a
AM
5990+ if (file) {
5991+ if (!err)
5992+ err = au_reopen_nondir(file);
5afbbe0d 5993+ hsrc.hd->hd_dentry = hsrc.h_dentry;
1facf9fc 5994+ }
5afbbe0d
AM
5995+ hdst.hd->hd_dentry = hdst.h_dentry;
5996+ dinfo->di_btop = cpg->bsrc;
c2b27bf2 5997+ cpg->bsrc = bsrc_orig;
1facf9fc 5998+
5999+ return err;
6000+}
6001+
c2b27bf2 6002+static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
1facf9fc 6003+{
6004+ int err;
c2b27bf2 6005+ aufs_bindex_t bdst;
1facf9fc 6006+ struct au_dtime dt;
c2b27bf2 6007+ struct dentry *dentry, *parent, *h_parent, *wh_dentry;
1facf9fc 6008+ struct au_branch *br;
6009+ struct path h_path;
6010+
c2b27bf2
AM
6011+ dentry = cpg->dentry;
6012+ bdst = cpg->bdst;
1facf9fc 6013+ br = au_sbr(dentry->d_sb, bdst);
6014+ parent = dget_parent(dentry);
6015+ h_parent = au_h_dptr(parent, bdst);
6016+ wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
6017+ err = PTR_ERR(wh_dentry);
6018+ if (IS_ERR(wh_dentry))
6019+ goto out;
6020+
6021+ h_path.dentry = h_parent;
86dc4139 6022+ h_path.mnt = au_br_mnt(br);
1facf9fc 6023+ au_dtime_store(&dt, parent, &h_path);
c2b27bf2 6024+ err = au_do_cpup_wh(cpg, wh_dentry, file);
1facf9fc 6025+ if (unlikely(err))
6026+ goto out_wh;
6027+
6028+ dget(wh_dentry);
6029+ h_path.dentry = wh_dentry;
2000de60 6030+ if (!d_is_dir(wh_dentry)) {
523b37e3 6031+ /* no delegation since it is just created */
5527c038 6032+ err = vfsub_unlink(d_inode(h_parent), &h_path,
523b37e3
AM
6033+ /*delegated*/NULL, /*force*/0);
6034+ } else
5527c038 6035+ err = vfsub_rmdir(d_inode(h_parent), &h_path);
1facf9fc 6036+ if (unlikely(err)) {
523b37e3
AM
6037+ AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
6038+ wh_dentry, err);
1facf9fc 6039+ err = -EIO;
6040+ }
6041+ au_dtime_revert(&dt);
5527c038 6042+ au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
1facf9fc 6043+
4f0767ce 6044+out_wh:
1facf9fc 6045+ dput(wh_dentry);
4f0767ce 6046+out:
1facf9fc 6047+ dput(parent);
6048+ return err;
6049+}
6050+
6051+struct au_cpup_wh_args {
6052+ int *errp;
c2b27bf2 6053+ struct au_cp_generic *cpg;
1facf9fc 6054+ struct file *file;
6055+};
6056+
6057+static void au_call_cpup_wh(void *args)
6058+{
6059+ struct au_cpup_wh_args *a = args;
86dc4139 6060+
c2b27bf2
AM
6061+ au_pin_hdir_acquire_nest(a->cpg->pin);
6062+ *a->errp = au_cpup_wh(a->cpg, a->file);
6063+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 6064+}
6065+
c2b27bf2 6066+int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
1facf9fc 6067+{
6068+ int err, wkq_err;
c2b27bf2 6069+ aufs_bindex_t bdst;
c1595e42 6070+ struct dentry *dentry, *parent, *h_orph, *h_parent;
86dc4139 6071+ struct inode *dir, *h_dir, *h_tmpdir;
1facf9fc 6072+ struct au_wbr *wbr;
c2b27bf2 6073+ struct au_pin wh_pin, *pin_orig;
0b2a12c6 6074+ struct user_namespace *h_userns;
1facf9fc 6075+
c2b27bf2
AM
6076+ dentry = cpg->dentry;
6077+ bdst = cpg->bdst;
1facf9fc 6078+ parent = dget_parent(dentry);
5527c038 6079+ dir = d_inode(parent);
1facf9fc 6080+ h_orph = NULL;
6081+ h_parent = NULL;
6082+ h_dir = au_igrab(au_h_iptr(dir, bdst));
6083+ h_tmpdir = h_dir;
c2b27bf2 6084+ pin_orig = NULL;
1facf9fc 6085+ if (!h_dir->i_nlink) {
6086+ wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
6087+ h_orph = wbr->wbr_orph;
6088+
6089+ h_parent = dget(au_h_dptr(parent, bdst));
1facf9fc 6090+ au_set_h_dptr(parent, bdst, dget(h_orph));
5527c038 6091+ h_tmpdir = d_inode(h_orph);
1facf9fc 6092+ au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
6093+
febd17d6 6094+ inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
4a4d8108 6095+ /* todo: au_h_open_pre()? */
86dc4139 6096+
c2b27bf2 6097+ pin_orig = cpg->pin;
86dc4139 6098+ au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
c2b27bf2
AM
6099+ AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
6100+ cpg->pin = &wh_pin;
1facf9fc 6101+ }
6102+
0b2a12c6
JR
6103+ h_userns = au_sbr_userns(dentry->d_sb, bdst);
6104+ if (!au_test_h_perm_sio(h_userns, h_tmpdir, MAY_EXEC | MAY_WRITE)
5527c038 6105+ && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
c2b27bf2 6106+ err = au_cpup_wh(cpg, file);
1facf9fc 6107+ else {
6108+ struct au_cpup_wh_args args = {
6109+ .errp = &err,
c2b27bf2
AM
6110+ .cpg = cpg,
6111+ .file = file
1facf9fc 6112+ };
6113+ wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
6114+ if (unlikely(wkq_err))
6115+ err = wkq_err;
6116+ }
6117+
6118+ if (h_orph) {
febd17d6 6119+ inode_unlock(h_tmpdir);
4a4d8108 6120+ /* todo: au_h_open_post()? */
1facf9fc 6121+ au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
1facf9fc 6122+ au_set_h_dptr(parent, bdst, h_parent);
c2b27bf2
AM
6123+ AuDebugOn(!pin_orig);
6124+ cpg->pin = pin_orig;
1facf9fc 6125+ }
6126+ iput(h_dir);
6127+ dput(parent);
6128+
6129+ return err;
6130+}
6131+
6132+/* ---------------------------------------------------------------------- */
6133+
6134+/*
6135+ * generic routine for both of copy-up and copy-down.
6136+ */
6137+/* cf. revalidate function in file.c */
6138+int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6139+ int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 6140+ struct au_pin *pin,
1facf9fc 6141+ struct dentry *h_parent, void *arg),
6142+ void *arg)
6143+{
6144+ int err;
6145+ struct au_pin pin;
5527c038 6146+ struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
1facf9fc 6147+
6148+ err = 0;
6149+ parent = dget_parent(dentry);
6150+ if (IS_ROOT(parent))
6151+ goto out;
6152+
6153+ au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
6154+ au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
6155+
6156+ /* do not use au_dpage */
6157+ real_parent = parent;
6158+ while (1) {
6159+ dput(parent);
6160+ parent = dget_parent(dentry);
6161+ h_parent = au_h_dptr(parent, bdst);
6162+ if (h_parent)
6163+ goto out; /* success */
6164+
6165+ /* find top dir which is necessary to cpup */
6166+ do {
6167+ d = parent;
6168+ dput(parent);
6169+ parent = dget_parent(d);
6170+ di_read_lock_parent3(parent, !AuLock_IR);
6171+ h_parent = au_h_dptr(parent, bdst);
6172+ di_read_unlock(parent, !AuLock_IR);
6173+ } while (!h_parent);
6174+
6175+ if (d != real_parent)
6176+ di_write_lock_child3(d);
6177+
6178+ /* somebody else might create while we were sleeping */
5527c038
JR
6179+ h_dentry = au_h_dptr(d, bdst);
6180+ if (!h_dentry || d_is_negative(h_dentry)) {
6181+ if (h_dentry)
5afbbe0d 6182+ au_update_dbtop(d);
1facf9fc 6183+
6184+ au_pin_set_dentry(&pin, d);
6185+ err = au_do_pin(&pin);
6186+ if (!err) {
86dc4139 6187+ err = cp(d, bdst, &pin, h_parent, arg);
1facf9fc 6188+ au_unpin(&pin);
6189+ }
6190+ }
6191+
6192+ if (d != real_parent)
6193+ di_write_unlock(d);
6194+ if (unlikely(err))
6195+ break;
6196+ }
6197+
4f0767ce 6198+out:
1facf9fc 6199+ dput(parent);
6200+ return err;
6201+}
6202+
6203+static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 6204+ struct au_pin *pin,
2000de60 6205+ struct dentry *h_parent __maybe_unused,
1facf9fc 6206+ void *arg __maybe_unused)
6207+{
c2b27bf2
AM
6208+ struct au_cp_generic cpg = {
6209+ .dentry = dentry,
6210+ .bdst = bdst,
6211+ .bsrc = -1,
6212+ .len = 0,
6213+ .pin = pin,
6214+ .flags = AuCpup_DTIME
6215+ };
6216+ return au_sio_cpup_simple(&cpg);
1facf9fc 6217+}
6218+
6219+int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6220+{
6221+ return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
6222+}
6223+
6224+int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6225+{
6226+ int err;
6227+ struct dentry *parent;
6228+ struct inode *dir;
6229+
6230+ parent = dget_parent(dentry);
5527c038 6231+ dir = d_inode(parent);
1facf9fc 6232+ err = 0;
6233+ if (au_h_iptr(dir, bdst))
6234+ goto out;
6235+
6236+ di_read_unlock(parent, AuLock_IR);
6237+ di_write_lock_parent(parent);
6238+ /* someone else might change our inode while we were sleeping */
6239+ if (!au_h_iptr(dir, bdst))
6240+ err = au_cpup_dirs(dentry, bdst);
6241+ di_downgrade_lock(parent, AuLock_IR);
6242+
4f0767ce 6243+out:
1facf9fc 6244+ dput(parent);
6245+ return err;
6246+}
7f207e10 6247diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
eca34b5c 6248--- /usr/share/empty/fs/aufs/cpup.h 1970-01-01 01:00:00.000000000 +0100
46016270 6249+++ linux/fs/aufs/cpup.h 2021-02-24 13:33:42.741013619 +0100
062440b3
AM
6250@@ -0,0 +1,100 @@
6251+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 6252+/*
d58c55f2 6253+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 6254+ *
6255+ * This program, aufs is free software; you can redistribute it and/or modify
6256+ * it under the terms of the GNU General Public License as published by
6257+ * the Free Software Foundation; either version 2 of the License, or
6258+ * (at your option) any later version.
dece6358
AM
6259+ *
6260+ * This program is distributed in the hope that it will be useful,
6261+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6262+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6263+ * GNU General Public License for more details.
6264+ *
6265+ * You should have received a copy of the GNU General Public License
523b37e3 6266+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6267+ */
6268+
6269+/*
6270+ * copy-up/down functions
6271+ */
6272+
6273+#ifndef __AUFS_CPUP_H__
6274+#define __AUFS_CPUP_H__
6275+
6276+#ifdef __KERNEL__
6277+
dece6358 6278+#include <linux/path.h>
1facf9fc 6279+
dece6358
AM
6280+struct inode;
6281+struct file;
86dc4139 6282+struct au_pin;
dece6358 6283+
86dc4139 6284+void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
1facf9fc 6285+void au_cpup_attr_timesizes(struct inode *inode);
6286+void au_cpup_attr_nlink(struct inode *inode, int force);
6287+void au_cpup_attr_changeable(struct inode *inode);
6288+void au_cpup_igen(struct inode *inode, struct inode *h_inode);
6289+void au_cpup_attr_all(struct inode *inode, int force);
6290+
6291+/* ---------------------------------------------------------------------- */
6292+
c2b27bf2
AM
6293+struct au_cp_generic {
6294+ struct dentry *dentry;
6295+ aufs_bindex_t bdst, bsrc;
6296+ loff_t len;
6297+ struct au_pin *pin;
6298+ unsigned int flags;
6299+};
6300+
1facf9fc 6301+/* cpup flags */
392086de
AM
6302+#define AuCpup_DTIME 1 /* do dtime_store/revert */
6303+#define AuCpup_KEEPLINO (1 << 1) /* do not clear the lower xino,
6304+ for link(2) */
6305+#define AuCpup_RENAME (1 << 2) /* rename after cpup */
6306+#define AuCpup_HOPEN (1 << 3) /* call h_open_pre/post() in
6307+ cpup */
6308+#define AuCpup_OVERWRITE (1 << 4) /* allow overwriting the
6309+ existing entry */
6310+#define AuCpup_RWDST (1 << 5) /* force write target even if
6311+ the branch is marked as RO */
c2b27bf2 6312+
8b6a4947
AM
6313+#ifndef CONFIG_AUFS_BR_HFSPLUS
6314+#undef AuCpup_HOPEN
6315+#define AuCpup_HOPEN 0
6316+#endif
6317+
1facf9fc 6318+#define au_ftest_cpup(flags, name) ((flags) & AuCpup_##name)
7f207e10
AM
6319+#define au_fset_cpup(flags, name) \
6320+ do { (flags) |= AuCpup_##name; } while (0)
6321+#define au_fclr_cpup(flags, name) \
6322+ do { (flags) &= ~AuCpup_##name; } while (0)
1facf9fc 6323+
6324+int au_copy_file(struct file *dst, struct file *src, loff_t len);
c2b27bf2
AM
6325+int au_sio_cpup_simple(struct au_cp_generic *cpg);
6326+int au_sio_cpdown_simple(struct au_cp_generic *cpg);
6327+int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
1facf9fc 6328+
6329+int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6330+ int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 6331+ struct au_pin *pin,
1facf9fc 6332+ struct dentry *h_parent, void *arg),
6333+ void *arg);
6334+int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6335+int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6336+
6337+/* ---------------------------------------------------------------------- */
6338+
6339+/* keep timestamps when copyup */
6340+struct au_dtime {
6341+ struct dentry *dt_dentry;
6342+ struct path dt_h_path;
cd7a4cd9 6343+ struct timespec64 dt_atime, dt_mtime;
1facf9fc 6344+};
6345+void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6346+ struct path *h_path);
6347+void au_dtime_revert(struct au_dtime *dt);
6348+
6349+#endif /* __KERNEL__ */
6350+#endif /* __AUFS_CPUP_H__ */
7f207e10 6351diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
eca34b5c 6352--- /usr/share/empty/fs/aufs/dbgaufs.c 1970-01-01 01:00:00.000000000 +0100
46016270 6353+++ linux/fs/aufs/dbgaufs.c 2021-02-24 13:33:42.741013619 +0100
9f237c51 6354@@ -0,0 +1,526 @@
cd7a4cd9 6355+// SPDX-License-Identifier: GPL-2.0
1facf9fc 6356+/*
d58c55f2 6357+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 6358+ *
6359+ * This program, aufs is free software; you can redistribute it and/or modify
6360+ * it under the terms of the GNU General Public License as published by
6361+ * the Free Software Foundation; either version 2 of the License, or
6362+ * (at your option) any later version.
dece6358
AM
6363+ *
6364+ * This program is distributed in the hope that it will be useful,
6365+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6366+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6367+ * GNU General Public License for more details.
6368+ *
6369+ * You should have received a copy of the GNU General Public License
523b37e3 6370+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6371+ */
6372+
6373+/*
6374+ * debugfs interface
6375+ */
6376+
6377+#include <linux/debugfs.h>
6378+#include "aufs.h"
6379+
6380+#ifndef CONFIG_SYSFS
6381+#error DEBUG_FS depends upon SYSFS
6382+#endif
6383+
6384+static struct dentry *dbgaufs;
cd7a4cd9 6385+static const mode_t dbgaufs_mode = 0444;
1facf9fc 6386+
6387+/* 20 is max digits length of ulong 64 */
6388+struct dbgaufs_arg {
6389+ int n;
6390+ char a[20 * 4];
6391+};
6392+
6393+/*
6394+ * common function for all XINO files
6395+ */
6396+static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6397+ struct file *file)
6398+{
9f237c51
AM
6399+ void *p;
6400+
6401+ p = file->private_data;
6402+ if (p) {
6403+ /* this is struct dbgaufs_arg */
6404+ AuDebugOn(!au_kfree_sz_test(p));
6405+ au_kfree_do_rcu(p);
6406+ }
1facf9fc 6407+ return 0;
6408+}
6409+
062440b3
AM
6410+static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt,
6411+ int cnt)
1facf9fc 6412+{
6413+ int err;
6414+ struct kstat st;
6415+ struct dbgaufs_arg *p;
6416+
6417+ err = -ENOMEM;
6418+ p = kmalloc(sizeof(*p), GFP_NOFS);
6419+ if (unlikely(!p))
6420+ goto out;
6421+
6422+ err = 0;
6423+ p->n = 0;
6424+ file->private_data = p;
6425+ if (!xf)
6426+ goto out;
6427+
521ced18 6428+ err = vfsub_getattr(&xf->f_path, &st);
1facf9fc 6429+ if (!err) {
6430+ if (do_fcnt)
6431+ p->n = snprintf
062440b3
AM
6432+ (p->a, sizeof(p->a), "%d, %llux%u %lld\n",
6433+ cnt, st.blocks, st.blksize,
1facf9fc 6434+ (long long)st.size);
6435+ else
521ced18 6436+ p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
1facf9fc 6437+ st.blocks, st.blksize,
6438+ (long long)st.size);
6439+ AuDebugOn(p->n >= sizeof(p->a));
6440+ } else {
6441+ p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6442+ err = 0;
6443+ }
6444+
4f0767ce 6445+out:
1facf9fc 6446+ return err;
1facf9fc 6447+}
6448+
6449+static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6450+ size_t count, loff_t *ppos)
6451+{
6452+ struct dbgaufs_arg *p;
6453+
6454+ p = file->private_data;
6455+ return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6456+}
6457+
6458+/* ---------------------------------------------------------------------- */
6459+
86dc4139
AM
6460+struct dbgaufs_plink_arg {
6461+ int n;
6462+ char a[];
6463+};
6464+
6465+static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6466+ struct file *file)
6467+{
1c60b727 6468+ free_page((unsigned long)file->private_data);
86dc4139
AM
6469+ return 0;
6470+}
6471+
6472+static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6473+{
6474+ int err, i, limit;
6475+ unsigned long n, sum;
6476+ struct dbgaufs_plink_arg *p;
6477+ struct au_sbinfo *sbinfo;
6478+ struct super_block *sb;
8b6a4947 6479+ struct hlist_bl_head *hbl;
86dc4139
AM
6480+
6481+ err = -ENOMEM;
6482+ p = (void *)get_zeroed_page(GFP_NOFS);
6483+ if (unlikely(!p))
6484+ goto out;
6485+
6486+ err = -EFBIG;
6487+ sbinfo = inode->i_private;
6488+ sb = sbinfo->si_sb;
6489+ si_noflush_read_lock(sb);
6490+ if (au_opt_test(au_mntflags(sb), PLINK)) {
6491+ limit = PAGE_SIZE - sizeof(p->n);
6492+
6493+ /* the number of buckets */
6494+ n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6495+ p->n += n;
6496+ limit -= n;
6497+
6498+ sum = 0;
8b6a4947
AM
6499+ for (i = 0, hbl = sbinfo->si_plink; i < AuPlink_NHASH;
6500+ i++, hbl++) {
6501+ n = au_hbl_count(hbl);
86dc4139
AM
6502+ sum += n;
6503+
6504+ n = snprintf(p->a + p->n, limit, "%lu ", n);
6505+ p->n += n;
6506+ limit -= n;
6507+ if (unlikely(limit <= 0))
6508+ goto out_free;
6509+ }
6510+ p->a[p->n - 1] = '\n';
6511+
6512+ /* the sum of plinks */
6513+ n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6514+ p->n += n;
6515+ limit -= n;
6516+ if (unlikely(limit <= 0))
6517+ goto out_free;
6518+ } else {
6519+#define str "1\n0\n0\n"
6520+ p->n = sizeof(str) - 1;
6521+ strcpy(p->a, str);
6522+#undef str
6523+ }
6524+ si_read_unlock(sb);
6525+
6526+ err = 0;
6527+ file->private_data = p;
6528+ goto out; /* success */
6529+
6530+out_free:
1c60b727 6531+ free_page((unsigned long)p);
86dc4139
AM
6532+out:
6533+ return err;
6534+}
6535+
6536+static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6537+ size_t count, loff_t *ppos)
6538+{
6539+ struct dbgaufs_plink_arg *p;
6540+
6541+ p = file->private_data;
6542+ return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6543+}
6544+
6545+static const struct file_operations dbgaufs_plink_fop = {
6546+ .owner = THIS_MODULE,
6547+ .open = dbgaufs_plink_open,
6548+ .release = dbgaufs_plink_release,
6549+ .read = dbgaufs_plink_read
6550+};
6551+
6552+/* ---------------------------------------------------------------------- */
6553+
1facf9fc 6554+static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6555+{
6556+ int err;
6557+ struct au_sbinfo *sbinfo;
6558+ struct super_block *sb;
6559+
6560+ sbinfo = inode->i_private;
6561+ sb = sbinfo->si_sb;
6562+ si_noflush_read_lock(sb);
062440b3 6563+ err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0, /*cnt*/0);
1facf9fc 6564+ si_read_unlock(sb);
6565+ return err;
6566+}
6567+
6568+static const struct file_operations dbgaufs_xib_fop = {
4a4d8108 6569+ .owner = THIS_MODULE,
1facf9fc 6570+ .open = dbgaufs_xib_open,
6571+ .release = dbgaufs_xi_release,
6572+ .read = dbgaufs_xi_read
6573+};
6574+
6575+/* ---------------------------------------------------------------------- */
6576+
6577+#define DbgaufsXi_PREFIX "xi"
6578+
6579+static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6580+{
acd2b654 6581+ int err, idx;
1facf9fc 6582+ long l;
acd2b654
AM
6583+ aufs_bindex_t bindex;
6584+ char *p, a[sizeof(DbgaufsXi_PREFIX) + 8];
1facf9fc 6585+ struct au_sbinfo *sbinfo;
6586+ struct super_block *sb;
acd2b654 6587+ struct au_xino *xi;
1facf9fc 6588+ struct file *xf;
6589+ struct qstr *name;
062440b3 6590+ struct au_branch *br;
1facf9fc 6591+
6592+ err = -ENOENT;
2000de60 6593+ name = &file->f_path.dentry->d_name;
1facf9fc 6594+ if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6595+ || memcmp(name->name, DbgaufsXi_PREFIX,
6596+ sizeof(DbgaufsXi_PREFIX) - 1)))
6597+ goto out;
acd2b654
AM
6598+
6599+ AuDebugOn(name->len >= sizeof(a));
6600+ memcpy(a, name->name, name->len);
6601+ a[name->len] = '\0';
6602+ p = strchr(a, '-');
6603+ if (p)
6604+ *p = '\0';
6605+ err = kstrtol(a + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
1facf9fc 6606+ if (unlikely(err))
6607+ goto out;
acd2b654
AM
6608+ bindex = l;
6609+ idx = 0;
6610+ if (p) {
6611+ err = kstrtol(p + 1, 10, &l);
6612+ if (unlikely(err))
6613+ goto out;
6614+ idx = l;
6615+ }
1facf9fc 6616+
acd2b654 6617+ err = -ENOENT;
1facf9fc 6618+ sbinfo = inode->i_private;
6619+ sb = sbinfo->si_sb;
6620+ si_noflush_read_lock(sb);
acd2b654
AM
6621+ if (unlikely(bindex < 0 || bindex > au_sbbot(sb)))
6622+ goto out_si;
6623+ br = au_sbr(sb, bindex);
6624+ xi = br->br_xino;
6625+ if (unlikely(idx >= xi->xi_nfile))
6626+ goto out_si;
6627+ xf = au_xino_file(xi, idx);
6628+ if (xf)
062440b3
AM
6629+ err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1,
6630+ au_xino_count(br));
1facf9fc 6631+
acd2b654
AM
6632+out_si:
6633+ si_read_unlock(sb);
4f0767ce 6634+out:
acd2b654 6635+ AuTraceErr(err);
1facf9fc 6636+ return err;
6637+}
6638+
6639+static const struct file_operations dbgaufs_xino_fop = {
4a4d8108 6640+ .owner = THIS_MODULE,
1facf9fc 6641+ .open = dbgaufs_xino_open,
6642+ .release = dbgaufs_xi_release,
6643+ .read = dbgaufs_xi_read
6644+};
6645+
062440b3
AM
6646+void dbgaufs_xino_del(struct au_branch *br)
6647+{
6648+ struct dentry *dbgaufs;
6649+
6650+ dbgaufs = br->br_dbgaufs;
6651+ if (!dbgaufs)
6652+ return;
6653+
6654+ br->br_dbgaufs = NULL;
6655+ /* debugfs acquires the parent i_mutex */
6656+ lockdep_off();
6657+ debugfs_remove(dbgaufs);
6658+ lockdep_on();
6659+}
6660+
1facf9fc 6661+void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6662+{
5afbbe0d 6663+ aufs_bindex_t bbot;
1facf9fc 6664+ struct au_branch *br;
1facf9fc 6665+
6666+ if (!au_sbi(sb)->si_dbgaufs)
6667+ return;
6668+
5afbbe0d
AM
6669+ bbot = au_sbbot(sb);
6670+ for (; bindex <= bbot; bindex++) {
1facf9fc 6671+ br = au_sbr(sb, bindex);
062440b3
AM
6672+ dbgaufs_xino_del(br);
6673+ }
6674+}
6675+
acd2b654
AM
6676+static void dbgaufs_br_do_add(struct super_block *sb, aufs_bindex_t bindex,
6677+ unsigned int idx, struct dentry *parent,
6678+ struct au_sbinfo *sbinfo)
062440b3
AM
6679+{
6680+ struct au_branch *br;
6681+ struct dentry *d;
acd2b654
AM
6682+ /* "xi" bindex(5) "-" idx(2) NULL */
6683+ char name[sizeof(DbgaufsXi_PREFIX) + 8];
062440b3 6684+
acd2b654
AM
6685+ if (!idx)
6686+ snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6687+ else
6688+ snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d-%u",
6689+ bindex, idx);
062440b3
AM
6690+ br = au_sbr(sb, bindex);
6691+ if (br->br_dbgaufs) {
6692+ struct qstr qstr = QSTR_INIT(name, strlen(name));
6693+
6694+ if (!au_qstreq(&br->br_dbgaufs->d_name, &qstr)) {
6695+ /* debugfs acquires the parent i_mutex */
6696+ lockdep_off();
6697+ d = debugfs_rename(parent, br->br_dbgaufs, parent,
6698+ name);
6699+ lockdep_on();
6700+ if (unlikely(!d))
6701+ pr_warn("failed renaming %pd/%s, ignored.\n",
6702+ parent, name);
6703+ }
6704+ } else {
e2f27e51 6705+ lockdep_off();
062440b3
AM
6706+ br->br_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6707+ sbinfo, &dbgaufs_xino_fop);
e2f27e51 6708+ lockdep_on();
062440b3 6709+ if (unlikely(!br->br_dbgaufs))
acd2b654 6710+ pr_warn("failed creating %pd/%s, ignored.\n",
062440b3 6711+ parent, name);
1facf9fc 6712+ }
6713+}
6714+
acd2b654
AM
6715+static void dbgaufs_br_add(struct super_block *sb, aufs_bindex_t bindex,
6716+ struct dentry *parent, struct au_sbinfo *sbinfo)
6717+{
6718+ struct au_branch *br;
6719+ struct au_xino *xi;
6720+ unsigned int u;
6721+
6722+ br = au_sbr(sb, bindex);
6723+ xi = br->br_xino;
6724+ for (u = 0; u < xi->xi_nfile; u++)
6725+ dbgaufs_br_do_add(sb, bindex, u, parent, sbinfo);
6726+}
6727+
062440b3 6728+void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown)
1facf9fc 6729+{
6730+ struct au_sbinfo *sbinfo;
6731+ struct dentry *parent;
5afbbe0d 6732+ aufs_bindex_t bbot;
062440b3
AM
6733+
6734+ if (!au_opt_test(au_mntflags(sb), XINO))
6735+ return;
1facf9fc 6736+
6737+ sbinfo = au_sbi(sb);
6738+ parent = sbinfo->si_dbgaufs;
6739+ if (!parent)
6740+ return;
6741+
5afbbe0d 6742+ bbot = au_sbbot(sb);
062440b3
AM
6743+ if (topdown)
6744+ for (; bindex <= bbot; bindex++)
6745+ dbgaufs_br_add(sb, bindex, parent, sbinfo);
6746+ else
6747+ for (; bbot >= bindex; bbot--)
6748+ dbgaufs_br_add(sb, bbot, parent, sbinfo);
1facf9fc 6749+}
6750+
6751+/* ---------------------------------------------------------------------- */
6752+
6753+#ifdef CONFIG_AUFS_EXPORT
6754+static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6755+{
6756+ int err;
6757+ struct au_sbinfo *sbinfo;
6758+ struct super_block *sb;
6759+
6760+ sbinfo = inode->i_private;
6761+ sb = sbinfo->si_sb;
6762+ si_noflush_read_lock(sb);
062440b3 6763+ err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0, /*cnt*/0);
1facf9fc 6764+ si_read_unlock(sb);
6765+ return err;
6766+}
6767+
6768+static const struct file_operations dbgaufs_xigen_fop = {
4a4d8108 6769+ .owner = THIS_MODULE,
1facf9fc 6770+ .open = dbgaufs_xigen_open,
6771+ .release = dbgaufs_xi_release,
6772+ .read = dbgaufs_xi_read
6773+};
6774+
6775+static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6776+{
6777+ int err;
6778+
dece6358 6779+ /*
c1595e42 6780+ * This function is a dynamic '__init' function actually,
dece6358
AM
6781+ * so the tiny check for si_rwsem is unnecessary.
6782+ */
6783+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6784+
1facf9fc 6785+ err = -EIO;
6786+ sbinfo->si_dbgaufs_xigen = debugfs_create_file
6787+ ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6788+ &dbgaufs_xigen_fop);
6789+ if (sbinfo->si_dbgaufs_xigen)
6790+ err = 0;
6791+
6792+ return err;
6793+}
6794+#else
6795+static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6796+{
6797+ return 0;
6798+}
6799+#endif /* CONFIG_AUFS_EXPORT */
6800+
6801+/* ---------------------------------------------------------------------- */
6802+
6803+void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6804+{
dece6358 6805+ /*
7e9cd9fe 6806+ * This function is a dynamic '__fin' function actually,
dece6358
AM
6807+ * so the tiny check for si_rwsem is unnecessary.
6808+ */
6809+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6810+
1facf9fc 6811+ debugfs_remove_recursive(sbinfo->si_dbgaufs);
6812+ sbinfo->si_dbgaufs = NULL;
1facf9fc 6813+}
6814+
6815+int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6816+{
6817+ int err;
6818+ char name[SysaufsSiNameLen];
6819+
dece6358 6820+ /*
c1595e42 6821+ * This function is a dynamic '__init' function actually,
dece6358
AM
6822+ * so the tiny check for si_rwsem is unnecessary.
6823+ */
6824+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6825+
1facf9fc 6826+ err = -ENOENT;
6827+ if (!dbgaufs) {
6828+ AuErr1("/debug/aufs is uninitialized\n");
6829+ goto out;
6830+ }
6831+
6832+ err = -EIO;
6833+ sysaufs_name(sbinfo, name);
6834+ sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6835+ if (unlikely(!sbinfo->si_dbgaufs))
6836+ goto out;
1facf9fc 6837+
062440b3 6838+ /* regardless plink/noplink option */
86dc4139
AM
6839+ sbinfo->si_dbgaufs_plink = debugfs_create_file
6840+ ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6841+ &dbgaufs_plink_fop);
6842+ if (unlikely(!sbinfo->si_dbgaufs_plink))
6843+ goto out_dir;
6844+
062440b3
AM
6845+ /* regardless xino/noxino option */
6846+ sbinfo->si_dbgaufs_xib = debugfs_create_file
6847+ ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6848+ &dbgaufs_xib_fop);
6849+ if (unlikely(!sbinfo->si_dbgaufs_xib))
6850+ goto out_dir;
6851+
1facf9fc 6852+ err = dbgaufs_xigen_init(sbinfo);
6853+ if (!err)
6854+ goto out; /* success */
6855+
4f0767ce 6856+out_dir:
1facf9fc 6857+ dbgaufs_si_fin(sbinfo);
4f0767ce 6858+out:
062440b3
AM
6859+ if (unlikely(err))
6860+ pr_err("debugfs/aufs failed\n");
1facf9fc 6861+ return err;
6862+}
6863+
6864+/* ---------------------------------------------------------------------- */
6865+
6866+void dbgaufs_fin(void)
6867+{
6868+ debugfs_remove(dbgaufs);
6869+}
6870+
6871+int __init dbgaufs_init(void)
6872+{
6873+ int err;
6874+
6875+ err = -EIO;
6876+ dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
6877+ if (dbgaufs)
6878+ err = 0;
6879+ return err;
6880+}
7f207e10 6881diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
eca34b5c 6882--- /usr/share/empty/fs/aufs/dbgaufs.h 1970-01-01 01:00:00.000000000 +0100
46016270 6883+++ linux/fs/aufs/dbgaufs.h 2021-02-24 13:33:42.741013619 +0100
062440b3
AM
6884@@ -0,0 +1,53 @@
6885+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 6886+/*
d58c55f2 6887+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 6888+ *
6889+ * This program, aufs is free software; you can redistribute it and/or modify
6890+ * it under the terms of the GNU General Public License as published by
6891+ * the Free Software Foundation; either version 2 of the License, or
6892+ * (at your option) any later version.
dece6358
AM
6893+ *
6894+ * This program is distributed in the hope that it will be useful,
6895+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6896+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6897+ * GNU General Public License for more details.
6898+ *
6899+ * You should have received a copy of the GNU General Public License
523b37e3 6900+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6901+ */
6902+
6903+/*
6904+ * debugfs interface
6905+ */
6906+
6907+#ifndef __DBGAUFS_H__
6908+#define __DBGAUFS_H__
6909+
6910+#ifdef __KERNEL__
6911+
dece6358 6912+struct super_block;
1facf9fc 6913+struct au_sbinfo;
062440b3 6914+struct au_branch;
dece6358 6915+
1facf9fc 6916+#ifdef CONFIG_DEBUG_FS
6917+/* dbgaufs.c */
062440b3 6918+void dbgaufs_xino_del(struct au_branch *br);
1facf9fc 6919+void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
062440b3 6920+void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown);
1facf9fc 6921+void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
6922+int dbgaufs_si_init(struct au_sbinfo *sbinfo);
6923+void dbgaufs_fin(void);
6924+int __init dbgaufs_init(void);
1facf9fc 6925+#else
062440b3 6926+AuStubVoid(dbgaufs_xino_del, struct au_branch *br)
4a4d8108 6927+AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
062440b3
AM
6928+AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex,
6929+ int topdown)
4a4d8108
AM
6930+AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
6931+AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
6932+AuStubVoid(dbgaufs_fin, void)
6933+AuStubInt0(__init dbgaufs_init, void)
1facf9fc 6934+#endif /* CONFIG_DEBUG_FS */
6935+
6936+#endif /* __KERNEL__ */
6937+#endif /* __DBGAUFS_H__ */
7f207e10 6938diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
eca34b5c 6939--- /usr/share/empty/fs/aufs/dcsub.c 1970-01-01 01:00:00.000000000 +0100
46016270 6940+++ linux/fs/aufs/dcsub.c 2021-02-24 13:33:42.741013619 +0100
cd7a4cd9
AM
6941@@ -0,0 +1,225 @@
6942+// SPDX-License-Identifier: GPL-2.0
1facf9fc 6943+/*
d58c55f2 6944+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 6945+ *
6946+ * This program, aufs is free software; you can redistribute it and/or modify
6947+ * it under the terms of the GNU General Public License as published by
6948+ * the Free Software Foundation; either version 2 of the License, or
6949+ * (at your option) any later version.
dece6358
AM
6950+ *
6951+ * This program is distributed in the hope that it will be useful,
6952+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6953+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6954+ * GNU General Public License for more details.
6955+ *
6956+ * You should have received a copy of the GNU General Public License
523b37e3 6957+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6958+ */
6959+
6960+/*
6961+ * sub-routines for dentry cache
6962+ */
6963+
6964+#include "aufs.h"
6965+
6966+static void au_dpage_free(struct au_dpage *dpage)
6967+{
6968+ int i;
6969+ struct dentry **p;
6970+
6971+ p = dpage->dentries;
6972+ for (i = 0; i < dpage->ndentry; i++)
6973+ dput(*p++);
1c60b727 6974+ free_page((unsigned long)dpage->dentries);
1facf9fc 6975+}
6976+
6977+int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
6978+{
6979+ int err;
6980+ void *p;
6981+
6982+ err = -ENOMEM;
6983+ dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
6984+ if (unlikely(!dpages->dpages))
6985+ goto out;
6986+
6987+ p = (void *)__get_free_page(gfp);
6988+ if (unlikely(!p))
6989+ goto out_dpages;
6990+
6991+ dpages->dpages[0].ndentry = 0;
6992+ dpages->dpages[0].dentries = p;
6993+ dpages->ndpage = 1;
6994+ return 0; /* success */
6995+
4f0767ce 6996+out_dpages:
9f237c51 6997+ au_kfree_try_rcu(dpages->dpages);
4f0767ce 6998+out:
1facf9fc 6999+ return err;
7000+}
7001+
7002+void au_dpages_free(struct au_dcsub_pages *dpages)
7003+{
7004+ int i;
7005+ struct au_dpage *p;
7006+
7007+ p = dpages->dpages;
7008+ for (i = 0; i < dpages->ndpage; i++)
7009+ au_dpage_free(p++);
9f237c51 7010+ au_kfree_try_rcu(dpages->dpages);
1facf9fc 7011+}
7012+
7013+static int au_dpages_append(struct au_dcsub_pages *dpages,
7014+ struct dentry *dentry, gfp_t gfp)
7015+{
7016+ int err, sz;
7017+ struct au_dpage *dpage;
7018+ void *p;
7019+
7020+ dpage = dpages->dpages + dpages->ndpage - 1;
7021+ sz = PAGE_SIZE / sizeof(dentry);
7022+ if (unlikely(dpage->ndentry >= sz)) {
7023+ AuLabel(new dpage);
7024+ err = -ENOMEM;
7025+ sz = dpages->ndpage * sizeof(*dpages->dpages);
7026+ p = au_kzrealloc(dpages->dpages, sz,
e2f27e51
AM
7027+ sz + sizeof(*dpages->dpages), gfp,
7028+ /*may_shrink*/0);
1facf9fc 7029+ if (unlikely(!p))
7030+ goto out;
7031+
7032+ dpages->dpages = p;
7033+ dpage = dpages->dpages + dpages->ndpage;
7034+ p = (void *)__get_free_page(gfp);
7035+ if (unlikely(!p))
7036+ goto out;
7037+
7038+ dpage->ndentry = 0;
7039+ dpage->dentries = p;
7040+ dpages->ndpage++;
7041+ }
7042+
c1595e42 7043+ AuDebugOn(au_dcount(dentry) <= 0);
027c5e7a 7044+ dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
1facf9fc 7045+ return 0; /* success */
7046+
4f0767ce 7047+out:
1facf9fc 7048+ return err;
7049+}
7050+
c1595e42
JR
7051+/* todo: BAD approach */
7052+/* copied from linux/fs/dcache.c */
7053+enum d_walk_ret {
7054+ D_WALK_CONTINUE,
7055+ D_WALK_QUIT,
7056+ D_WALK_NORETRY,
7057+ D_WALK_SKIP,
7058+};
7059+
7060+extern void d_walk(struct dentry *parent, void *data,
cd7a4cd9 7061+ enum d_walk_ret (*enter)(void *, struct dentry *));
c1595e42
JR
7062+
7063+struct ac_dpages_arg {
1facf9fc 7064+ int err;
c1595e42
JR
7065+ struct au_dcsub_pages *dpages;
7066+ struct super_block *sb;
7067+ au_dpages_test test;
7068+ void *arg;
7069+};
1facf9fc 7070+
c1595e42
JR
7071+static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
7072+{
7073+ enum d_walk_ret ret;
7074+ struct ac_dpages_arg *arg = _arg;
1facf9fc 7075+
c1595e42
JR
7076+ ret = D_WALK_CONTINUE;
7077+ if (dentry->d_sb == arg->sb
7078+ && !IS_ROOT(dentry)
7079+ && au_dcount(dentry) > 0
7080+ && au_di(dentry)
7081+ && (!arg->test || arg->test(dentry, arg->arg))) {
7082+ arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
7083+ if (unlikely(arg->err))
7084+ ret = D_WALK_QUIT;
1facf9fc 7085+ }
7086+
c1595e42
JR
7087+ return ret;
7088+}
027c5e7a 7089+
c1595e42
JR
7090+int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7091+ au_dpages_test test, void *arg)
7092+{
7093+ struct ac_dpages_arg args = {
7094+ .err = 0,
7095+ .dpages = dpages,
7096+ .sb = root->d_sb,
7097+ .test = test,
7098+ .arg = arg
7099+ };
027c5e7a 7100+
cd7a4cd9 7101+ d_walk(root, &args, au_call_dpages_append);
c1595e42
JR
7102+
7103+ return args.err;
1facf9fc 7104+}
7105+
7106+int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7107+ int do_include, au_dpages_test test, void *arg)
7108+{
7109+ int err;
7110+
7111+ err = 0;
027c5e7a
AM
7112+ write_seqlock(&rename_lock);
7113+ spin_lock(&dentry->d_lock);
7114+ if (do_include
c1595e42 7115+ && au_dcount(dentry) > 0
027c5e7a 7116+ && (!test || test(dentry, arg)))
1facf9fc 7117+ err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
027c5e7a
AM
7118+ spin_unlock(&dentry->d_lock);
7119+ if (unlikely(err))
7120+ goto out;
7121+
7122+ /*
523b37e3 7123+ * RCU for vfsmount is unnecessary since this is a traverse in a single
027c5e7a
AM
7124+ * mount
7125+ */
1facf9fc 7126+ while (!IS_ROOT(dentry)) {
027c5e7a
AM
7127+ dentry = dentry->d_parent; /* rename_lock is locked */
7128+ spin_lock(&dentry->d_lock);
c1595e42 7129+ if (au_dcount(dentry) > 0
027c5e7a 7130+ && (!test || test(dentry, arg)))
1facf9fc 7131+ err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
027c5e7a
AM
7132+ spin_unlock(&dentry->d_lock);
7133+ if (unlikely(err))
7134+ break;
1facf9fc 7135+ }
7136+
4f0767ce 7137+out:
027c5e7a 7138+ write_sequnlock(&rename_lock);
1facf9fc 7139+ return err;
7140+}
7141+
027c5e7a
AM
7142+static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
7143+{
7144+ return au_di(dentry) && dentry->d_sb == arg;
7145+}
7146+
7147+int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7148+ struct dentry *dentry, int do_include)
7149+{
7150+ return au_dcsub_pages_rev(dpages, dentry, do_include,
7151+ au_dcsub_dpages_aufs, dentry->d_sb);
7152+}
7153+
4a4d8108 7154+int au_test_subdir(struct dentry *d1, struct dentry *d2)
1facf9fc 7155+{
4a4d8108
AM
7156+ struct path path[2] = {
7157+ {
7158+ .dentry = d1
7159+ },
7160+ {
7161+ .dentry = d2
7162+ }
7163+ };
1facf9fc 7164+
4a4d8108 7165+ return path_is_under(path + 0, path + 1);
1facf9fc 7166+}
7f207e10 7167diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
eca34b5c 7168--- /usr/share/empty/fs/aufs/dcsub.h 1970-01-01 01:00:00.000000000 +0100
46016270 7169+++ linux/fs/aufs/dcsub.h 2021-02-24 13:33:42.741013619 +0100
062440b3
AM
7170@@ -0,0 +1,137 @@
7171+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 7172+/*
d58c55f2 7173+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 7174+ *
7175+ * This program, aufs is free software; you can redistribute it and/or modify
7176+ * it under the terms of the GNU General Public License as published by
7177+ * the Free Software Foundation; either version 2 of the License, or
7178+ * (at your option) any later version.
dece6358
AM
7179+ *
7180+ * This program is distributed in the hope that it will be useful,
7181+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7182+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7183+ * GNU General Public License for more details.
7184+ *
7185+ * You should have received a copy of the GNU General Public License
523b37e3 7186+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7187+ */
7188+
7189+/*
7190+ * sub-routines for dentry cache
7191+ */
7192+
7193+#ifndef __AUFS_DCSUB_H__
7194+#define __AUFS_DCSUB_H__
7195+
7196+#ifdef __KERNEL__
7197+
7f207e10 7198+#include <linux/dcache.h>
027c5e7a 7199+#include <linux/fs.h>
dece6358 7200+
1facf9fc 7201+struct au_dpage {
7202+ int ndentry;
7203+ struct dentry **dentries;
7204+};
7205+
7206+struct au_dcsub_pages {
7207+ int ndpage;
7208+ struct au_dpage *dpages;
7209+};
7210+
7211+/* ---------------------------------------------------------------------- */
7212+
7f207e10 7213+/* dcsub.c */
1facf9fc 7214+int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
7215+void au_dpages_free(struct au_dcsub_pages *dpages);
7216+typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
7217+int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7218+ au_dpages_test test, void *arg);
7219+int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7220+ int do_include, au_dpages_test test, void *arg);
027c5e7a
AM
7221+int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7222+ struct dentry *dentry, int do_include);
4a4d8108 7223+int au_test_subdir(struct dentry *d1, struct dentry *d2);
1facf9fc 7224+
7f207e10
AM
7225+/* ---------------------------------------------------------------------- */
7226+
523b37e3
AM
7227+/*
7228+ * todo: in linux-3.13, several similar (but faster) helpers are added to
7229+ * include/linux/dcache.h. Try them (in the future).
7230+ */
7231+
027c5e7a
AM
7232+static inline int au_d_hashed_positive(struct dentry *d)
7233+{
7234+ int err;
5527c038 7235+ struct inode *inode = d_inode(d);
076b876e 7236+
027c5e7a 7237+ err = 0;
5527c038
JR
7238+ if (unlikely(d_unhashed(d)
7239+ || d_is_negative(d)
7240+ || !inode->i_nlink))
027c5e7a
AM
7241+ err = -ENOENT;
7242+ return err;
7243+}
7244+
38d290e6
JR
7245+static inline int au_d_linkable(struct dentry *d)
7246+{
7247+ int err;
5527c038 7248+ struct inode *inode = d_inode(d);
076b876e 7249+
38d290e6
JR
7250+ err = au_d_hashed_positive(d);
7251+ if (err
5527c038 7252+ && d_is_positive(d)
38d290e6
JR
7253+ && (inode->i_state & I_LINKABLE))
7254+ err = 0;
7255+ return err;
7256+}
7257+
027c5e7a
AM
7258+static inline int au_d_alive(struct dentry *d)
7259+{
7260+ int err;
7261+ struct inode *inode;
076b876e 7262+
027c5e7a
AM
7263+ err = 0;
7264+ if (!IS_ROOT(d))
7265+ err = au_d_hashed_positive(d);
7266+ else {
5527c038
JR
7267+ inode = d_inode(d);
7268+ if (unlikely(d_unlinked(d)
7269+ || d_is_negative(d)
7270+ || !inode->i_nlink))
027c5e7a
AM
7271+ err = -ENOENT;
7272+ }
7273+ return err;
7274+}
7275+
7276+static inline int au_alive_dir(struct dentry *d)
7f207e10 7277+{
027c5e7a 7278+ int err;
076b876e 7279+
027c5e7a 7280+ err = au_d_alive(d);
5527c038 7281+ if (unlikely(err || IS_DEADDIR(d_inode(d))))
027c5e7a
AM
7282+ err = -ENOENT;
7283+ return err;
7f207e10
AM
7284+}
7285+
38d290e6
JR
7286+static inline int au_qstreq(struct qstr *a, struct qstr *b)
7287+{
7288+ return a->len == b->len
7289+ && !memcmp(a->name, b->name, a->len);
7290+}
7291+
7e9cd9fe
AM
7292+/*
7293+ * by the commit
7294+ * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
7295+ * taking d_lock
7296+ * the type of d_lockref.count became int, but the inlined function d_count()
7297+ * still returns unsigned int.
7298+ * I don't know why. Maybe it is for every d_count() users?
7299+ * Anyway au_dcount() lives on.
7300+ */
c1595e42
JR
7301+static inline int au_dcount(struct dentry *d)
7302+{
7303+ return (int)d_count(d);
7304+}
7305+
1facf9fc 7306+#endif /* __KERNEL__ */
7307+#endif /* __AUFS_DCSUB_H__ */
7f207e10 7308diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
eca34b5c 7309--- /usr/share/empty/fs/aufs/debug.c 1970-01-01 01:00:00.000000000 +0100
46016270 7310+++ linux/fs/aufs/debug.c 2021-02-24 13:33:42.741013619 +0100
eca801bf 7311@@ -0,0 +1,441 @@
cd7a4cd9 7312+// SPDX-License-Identifier: GPL-2.0
1facf9fc 7313+/*
d58c55f2 7314+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 7315+ *
7316+ * This program, aufs is free software; you can redistribute it and/or modify
7317+ * it under the terms of the GNU General Public License as published by
7318+ * the Free Software Foundation; either version 2 of the License, or
7319+ * (at your option) any later version.
dece6358
AM
7320+ *
7321+ * This program is distributed in the hope that it will be useful,
7322+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7323+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7324+ * GNU General Public License for more details.
7325+ *
7326+ * You should have received a copy of the GNU General Public License
523b37e3 7327+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7328+ */
7329+
7330+/*
7331+ * debug print functions
7332+ */
7333+
eca801bf 7334+#include <linux/iversion.h>
1facf9fc 7335+#include "aufs.h"
7336+
392086de
AM
7337+/* Returns 0, or -errno. arg is in kp->arg. */
7338+static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
7339+{
7340+ int err, n;
7341+
7342+ err = kstrtoint(val, 0, &n);
7343+ if (!err) {
7344+ if (n > 0)
7345+ au_debug_on();
7346+ else
7347+ au_debug_off();
7348+ }
7349+ return err;
7350+}
7351+
7352+/* Returns length written or -errno. Buffer is 4k (ie. be short!) */
7353+static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
7354+{
7355+ atomic_t *a;
7356+
7357+ a = kp->arg;
7358+ return sprintf(buffer, "%d", atomic_read(a));
7359+}
7360+
7361+static struct kernel_param_ops param_ops_atomic_t = {
7362+ .set = param_atomic_t_set,
7363+ .get = param_atomic_t_get
7364+ /* void (*free)(void *arg) */
7365+};
7366+
7367+atomic_t aufs_debug = ATOMIC_INIT(0);
1facf9fc 7368+MODULE_PARM_DESC(debug, "debug print");
cd7a4cd9 7369+module_param_named(debug, aufs_debug, atomic_t, 0664);
1facf9fc 7370+
c1595e42 7371+DEFINE_MUTEX(au_dbg_mtx); /* just to serialize the dbg msgs */
1facf9fc 7372+char *au_plevel = KERN_DEBUG;
e49829fe
JR
7373+#define dpri(fmt, ...) do { \
7374+ if ((au_plevel \
7375+ && strcmp(au_plevel, KERN_DEBUG)) \
7376+ || au_debug_test()) \
7377+ printk("%s" fmt, au_plevel, ##__VA_ARGS__); \
1facf9fc 7378+} while (0)
7379+
7380+/* ---------------------------------------------------------------------- */
7381+
7382+void au_dpri_whlist(struct au_nhash *whlist)
7383+{
7384+ unsigned long ul, n;
7385+ struct hlist_head *head;
c06a8ce3 7386+ struct au_vdir_wh *pos;
1facf9fc 7387+
7388+ n = whlist->nh_num;
7389+ head = whlist->nh_head;
7390+ for (ul = 0; ul < n; ul++) {
c06a8ce3 7391+ hlist_for_each_entry(pos, head, wh_hash)
1facf9fc 7392+ dpri("b%d, %.*s, %d\n",
c06a8ce3
AM
7393+ pos->wh_bindex,
7394+ pos->wh_str.len, pos->wh_str.name,
7395+ pos->wh_str.len);
1facf9fc 7396+ head++;
7397+ }
7398+}
7399+
7400+void au_dpri_vdir(struct au_vdir *vdir)
7401+{
7402+ unsigned long ul;
7403+ union au_vdir_deblk_p p;
7404+ unsigned char *o;
7405+
7406+ if (!vdir || IS_ERR(vdir)) {
7407+ dpri("err %ld\n", PTR_ERR(vdir));
7408+ return;
7409+ }
7410+
be118d29 7411+ dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %llu\n",
1facf9fc 7412+ vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
7413+ vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
7414+ for (ul = 0; ul < vdir->vd_nblk; ul++) {
7415+ p.deblk = vdir->vd_deblk[ul];
7416+ o = p.deblk;
7417+ dpri("[%lu]: %p\n", ul, o);
7418+ }
7419+}
7420+
53392da6 7421+static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
1facf9fc 7422+ struct dentry *wh)
7423+{
7424+ char *n = NULL;
7425+ int l = 0;
7426+
7427+ if (!inode || IS_ERR(inode)) {
7428+ dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7429+ return -1;
7430+ }
7431+
c2b27bf2 7432+ /* the type of i_blocks depends upon CONFIG_LBDAF */
1facf9fc 7433+ BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7434+ && sizeof(inode->i_blocks) != sizeof(u64));
7435+ if (wh) {
7436+ n = (void *)wh->d_name.name;
7437+ l = wh->d_name.len;
7438+ }
7439+
53392da6
AM
7440+ dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
7441+ " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7442+ bindex, inode,
1facf9fc 7443+ inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7444+ atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7445+ i_size_read(inode), (unsigned long long)inode->i_blocks,
cd7a4cd9 7446+ hn, (long long)timespec64_to_ns(&inode->i_ctime) & 0x0ffff,
1facf9fc 7447+ inode->i_mapping ? inode->i_mapping->nrpages : 0,
be118d29 7448+ inode->i_state, inode->i_flags, inode_peek_iversion(inode),
b752ccd1 7449+ inode->i_generation,
1facf9fc 7450+ l ? ", wh " : "", l, n);
7451+ return 0;
7452+}
7453+
7454+void au_dpri_inode(struct inode *inode)
7455+{
7456+ struct au_iinfo *iinfo;
5afbbe0d 7457+ struct au_hinode *hi;
1facf9fc 7458+ aufs_bindex_t bindex;
53392da6 7459+ int err, hn;
1facf9fc 7460+
53392da6 7461+ err = do_pri_inode(-1, inode, -1, NULL);
5afbbe0d 7462+ if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
1facf9fc 7463+ return;
7464+
7465+ iinfo = au_ii(inode);
5afbbe0d
AM
7466+ dpri("i-1: btop %d, bbot %d, gen %d\n",
7467+ iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
7468+ if (iinfo->ii_btop < 0)
1facf9fc 7469+ return;
53392da6 7470+ hn = 0;
5afbbe0d
AM
7471+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
7472+ hi = au_hinode(iinfo, bindex);
7473+ hn = !!au_hn(hi);
7474+ do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
53392da6 7475+ }
1facf9fc 7476+}
7477+
2cbb1c4b
JR
7478+void au_dpri_dalias(struct inode *inode)
7479+{
7480+ struct dentry *d;
7481+
7482+ spin_lock(&inode->i_lock);
c1595e42 7483+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
2cbb1c4b
JR
7484+ au_dpri_dentry(d);
7485+ spin_unlock(&inode->i_lock);
7486+}
7487+
1facf9fc 7488+static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7489+{
7490+ struct dentry *wh = NULL;
53392da6 7491+ int hn;
5afbbe0d 7492+ struct inode *inode;
076b876e 7493+ struct au_iinfo *iinfo;
5afbbe0d 7494+ struct au_hinode *hi;
1facf9fc 7495+
7496+ if (!dentry || IS_ERR(dentry)) {
7497+ dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7498+ return -1;
7499+ }
7500+ /* do not call dget_parent() here */
027c5e7a 7501+ /* note: access d_xxx without d_lock */
523b37e3
AM
7502+ dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7503+ bindex, dentry, dentry,
1facf9fc 7504+ dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
c1595e42 7505+ au_dcount(dentry), dentry->d_flags,
523b37e3 7506+ d_unhashed(dentry) ? "un" : "");
53392da6 7507+ hn = -1;
5afbbe0d
AM
7508+ inode = NULL;
7509+ if (d_is_positive(dentry))
7510+ inode = d_inode(dentry);
7511+ if (inode
7512+ && au_test_aufs(dentry->d_sb)
7513+ && bindex >= 0
7514+ && !au_is_bad_inode(inode)) {
7515+ iinfo = au_ii(inode);
7516+ hi = au_hinode(iinfo, bindex);
7517+ hn = !!au_hn(hi);
7518+ wh = hi->hi_whdentry;
7519+ }
7520+ do_pri_inode(bindex, inode, hn, wh);
1facf9fc 7521+ return 0;
7522+}
7523+
7524+void au_dpri_dentry(struct dentry *dentry)
7525+{
7526+ struct au_dinfo *dinfo;
7527+ aufs_bindex_t bindex;
7528+ int err;
7529+
7530+ err = do_pri_dentry(-1, dentry);
7531+ if (err || !au_test_aufs(dentry->d_sb))
7532+ return;
7533+
7534+ dinfo = au_di(dentry);
7535+ if (!dinfo)
7536+ return;
5afbbe0d
AM
7537+ dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7538+ dinfo->di_btop, dinfo->di_bbot,
38d290e6
JR
7539+ dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7540+ dinfo->di_tmpfile);
5afbbe0d 7541+ if (dinfo->di_btop < 0)
1facf9fc 7542+ return;
5afbbe0d
AM
7543+ for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
7544+ do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
1facf9fc 7545+}
7546+
7547+static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7548+{
7549+ char a[32];
7550+
7551+ if (!file || IS_ERR(file)) {
7552+ dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7553+ return -1;
7554+ }
7555+ a[0] = 0;
7556+ if (bindex < 0
b912730e 7557+ && !IS_ERR_OR_NULL(file->f_path.dentry)
2000de60 7558+ && au_test_aufs(file->f_path.dentry->d_sb)
1facf9fc 7559+ && au_fi(file))
e49829fe 7560+ snprintf(a, sizeof(a), ", gen %d, mmapped %d",
2cbb1c4b 7561+ au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
b752ccd1 7562+ dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
1facf9fc 7563+ bindex, file->f_mode, file->f_flags, (long)file_count(file),
b752ccd1 7564+ file->f_version, file->f_pos, a);
b912730e 7565+ if (!IS_ERR_OR_NULL(file->f_path.dentry))
2000de60 7566+ do_pri_dentry(bindex, file->f_path.dentry);
1facf9fc 7567+ return 0;
7568+}
7569+
7570+void au_dpri_file(struct file *file)
7571+{
7572+ struct au_finfo *finfo;
4a4d8108
AM
7573+ struct au_fidir *fidir;
7574+ struct au_hfile *hfile;
1facf9fc 7575+ aufs_bindex_t bindex;
7576+ int err;
7577+
7578+ err = do_pri_file(-1, file);
2000de60 7579+ if (err
b912730e 7580+ || IS_ERR_OR_NULL(file->f_path.dentry)
2000de60 7581+ || !au_test_aufs(file->f_path.dentry->d_sb))
1facf9fc 7582+ return;
7583+
7584+ finfo = au_fi(file);
7585+ if (!finfo)
7586+ return;
4a4d8108 7587+ if (finfo->fi_btop < 0)
1facf9fc 7588+ return;
4a4d8108
AM
7589+ fidir = finfo->fi_hdir;
7590+ if (!fidir)
7591+ do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7592+ else
e49829fe
JR
7593+ for (bindex = finfo->fi_btop;
7594+ bindex >= 0 && bindex <= fidir->fd_bbot;
4a4d8108
AM
7595+ bindex++) {
7596+ hfile = fidir->fd_hfile + bindex;
7597+ do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7598+ }
1facf9fc 7599+}
7600+
7601+static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7602+{
7603+ struct vfsmount *mnt;
7604+ struct super_block *sb;
7605+
7606+ if (!br || IS_ERR(br))
7607+ goto out;
86dc4139 7608+ mnt = au_br_mnt(br);
1facf9fc 7609+ if (!mnt || IS_ERR(mnt))
7610+ goto out;
7611+ sb = mnt->mnt_sb;
7612+ if (!sb || IS_ERR(sb))
7613+ goto out;
7614+
acd2b654 7615+ dpri("s%d: {perm 0x%x, id %d, wbr %p}, "
b752ccd1 7616+ "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
1facf9fc 7617+ "xino %d\n",
acd2b654
AM
7618+ bindex, br->br_perm, br->br_id, br->br_wbr,
7619+ au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
b752ccd1 7620+ sb->s_flags, sb->s_count,
acd2b654
AM
7621+ atomic_read(&sb->s_active),
7622+ !!au_xino_file(br->br_xino, /*idx*/-1));
1facf9fc 7623+ return 0;
7624+
4f0767ce 7625+out:
1facf9fc 7626+ dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7627+ return -1;
7628+}
7629+
7630+void au_dpri_sb(struct super_block *sb)
7631+{
7632+ struct au_sbinfo *sbinfo;
7633+ aufs_bindex_t bindex;
7634+ int err;
acd2b654 7635+ /* to reduce stack size */
1facf9fc 7636+ struct {
7637+ struct vfsmount mnt;
7638+ struct au_branch fake;
7639+ } *a;
7640+
7641+ /* this function can be called from magic sysrq */
7642+ a = kzalloc(sizeof(*a), GFP_ATOMIC);
7643+ if (unlikely(!a)) {
7644+ dpri("no memory\n");
7645+ return;
7646+ }
7647+
7648+ a->mnt.mnt_sb = sb;
86dc4139 7649+ a->fake.br_path.mnt = &a->mnt;
1facf9fc 7650+ err = do_pri_br(-1, &a->fake);
9f237c51 7651+ au_kfree_rcu(a);
1facf9fc 7652+ dpri("dev 0x%x\n", sb->s_dev);
7653+ if (err || !au_test_aufs(sb))
7654+ return;
7655+
7656+ sbinfo = au_sbi(sb);
7657+ if (!sbinfo)
7658+ return;
f0c0a007
AM
7659+ dpri("nw %d, gen %u, kobj %d\n",
7660+ atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
521ced18 7661+ kref_read(&sbinfo->si_kobj.kref));
5afbbe0d 7662+ for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
1facf9fc 7663+ do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7664+}
7665+
7666+/* ---------------------------------------------------------------------- */
7667+
027c5e7a
AM
7668+void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7669+{
5527c038 7670+ struct inode *h_inode, *inode = d_inode(dentry);
027c5e7a 7671+ struct dentry *h_dentry;
5afbbe0d 7672+ aufs_bindex_t bindex, bbot, bi;
027c5e7a
AM
7673+
7674+ if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7675+ return;
7676+
5afbbe0d
AM
7677+ bbot = au_dbbot(dentry);
7678+ bi = au_ibbot(inode);
7679+ if (bi < bbot)
7680+ bbot = bi;
7681+ bindex = au_dbtop(dentry);
7682+ bi = au_ibtop(inode);
027c5e7a
AM
7683+ if (bi > bindex)
7684+ bindex = bi;
7685+
5afbbe0d 7686+ for (; bindex <= bbot; bindex++) {
027c5e7a
AM
7687+ h_dentry = au_h_dptr(dentry, bindex);
7688+ if (!h_dentry)
7689+ continue;
7690+ h_inode = au_h_iptr(inode, bindex);
5527c038 7691+ if (unlikely(h_inode != d_inode(h_dentry))) {
392086de 7692+ au_debug_on();
027c5e7a
AM
7693+ AuDbg("b%d, %s:%d\n", bindex, func, line);
7694+ AuDbgDentry(dentry);
7695+ AuDbgInode(inode);
392086de 7696+ au_debug_off();
027c5e7a
AM
7697+ BUG();
7698+ }
7699+ }
7700+}
7701+
1facf9fc 7702+void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7703+{
7704+ int err, i, j;
7705+ struct au_dcsub_pages dpages;
7706+ struct au_dpage *dpage;
7707+ struct dentry **dentries;
7708+
7709+ err = au_dpages_init(&dpages, GFP_NOFS);
7710+ AuDebugOn(err);
027c5e7a 7711+ err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
1facf9fc 7712+ AuDebugOn(err);
7713+ for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7714+ dpage = dpages.dpages + i;
7715+ dentries = dpage->dentries;
7716+ for (j = dpage->ndentry - 1; !err && j >= 0; j--)
027c5e7a 7717+ AuDebugOn(au_digen_test(dentries[j], sigen));
1facf9fc 7718+ }
7719+ au_dpages_free(&dpages);
7720+}
7721+
1facf9fc 7722+void au_dbg_verify_kthread(void)
7723+{
53392da6 7724+ if (au_wkq_test()) {
1facf9fc 7725+ au_dbg_blocked();
1e00d052
AM
7726+ /*
7727+ * It may be recursive, but udba=notify between two aufs mounts,
7728+ * where a single ro branch is shared, is not a problem.
7729+ */
7730+ /* WARN_ON(1); */
1facf9fc 7731+ }
7732+}
7733+
7734+/* ---------------------------------------------------------------------- */
7735+
1facf9fc 7736+int __init au_debug_init(void)
7737+{
7738+ aufs_bindex_t bindex;
7739+ struct au_vdir_destr destr;
7740+
7741+ bindex = -1;
7742+ AuDebugOn(bindex >= 0);
7743+
7744+ destr.len = -1;
7745+ AuDebugOn(destr.len < NAME_MAX);
7746+
7747+#ifdef CONFIG_4KSTACKS
0c3ec466 7748+ pr_warn("CONFIG_4KSTACKS is defined.\n");
1facf9fc 7749+#endif
7750+
1facf9fc 7751+ return 0;
7752+}
7f207e10 7753diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
eca34b5c 7754--- /usr/share/empty/fs/aufs/debug.h 1970-01-01 01:00:00.000000000 +0100
46016270 7755+++ linux/fs/aufs/debug.h 2021-02-24 13:33:42.741013619 +0100
062440b3
AM
7756@@ -0,0 +1,226 @@
7757+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 7758+/*
d58c55f2 7759+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 7760+ *
7761+ * This program, aufs is free software; you can redistribute it and/or modify
7762+ * it under the terms of the GNU General Public License as published by
7763+ * the Free Software Foundation; either version 2 of the License, or
7764+ * (at your option) any later version.
dece6358
AM
7765+ *
7766+ * This program is distributed in the hope that it will be useful,
7767+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7768+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7769+ * GNU General Public License for more details.
7770+ *
7771+ * You should have received a copy of the GNU General Public License
523b37e3 7772+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7773+ */
7774+
7775+/*
7776+ * debug print functions
7777+ */
7778+
7779+#ifndef __AUFS_DEBUG_H__
7780+#define __AUFS_DEBUG_H__
7781+
7782+#ifdef __KERNEL__
7783+
392086de 7784+#include <linux/atomic.h>
4a4d8108
AM
7785+#include <linux/module.h>
7786+#include <linux/kallsyms.h>
1facf9fc 7787+#include <linux/sysrq.h>
4a4d8108 7788+
1facf9fc 7789+#ifdef CONFIG_AUFS_DEBUG
7790+#define AuDebugOn(a) BUG_ON(a)
7791+
7792+/* module parameter */
392086de
AM
7793+extern atomic_t aufs_debug;
7794+static inline void au_debug_on(void)
1facf9fc 7795+{
392086de
AM
7796+ atomic_inc(&aufs_debug);
7797+}
7798+static inline void au_debug_off(void)
7799+{
7800+ atomic_dec_if_positive(&aufs_debug);
1facf9fc 7801+}
7802+
7803+static inline int au_debug_test(void)
7804+{
392086de 7805+ return atomic_read(&aufs_debug) > 0;
1facf9fc 7806+}
7807+#else
7808+#define AuDebugOn(a) do {} while (0)
392086de
AM
7809+AuStubVoid(au_debug_on, void)
7810+AuStubVoid(au_debug_off, void)
4a4d8108 7811+AuStubInt0(au_debug_test, void)
1facf9fc 7812+#endif /* CONFIG_AUFS_DEBUG */
7813+
392086de
AM
7814+#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7815+
1facf9fc 7816+/* ---------------------------------------------------------------------- */
7817+
7818+/* debug print */
7819+
4a4d8108 7820+#define AuDbg(fmt, ...) do { \
1facf9fc 7821+ if (au_debug_test()) \
4a4d8108 7822+ pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
1facf9fc 7823+} while (0)
4a4d8108
AM
7824+#define AuLabel(l) AuDbg(#l "\n")
7825+#define AuIOErr(fmt, ...) pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7826+#define AuWarn1(fmt, ...) do { \
1facf9fc 7827+ static unsigned char _c; \
7828+ if (!_c++) \
0c3ec466 7829+ pr_warn(fmt, ##__VA_ARGS__); \
1facf9fc 7830+} while (0)
7831+
4a4d8108 7832+#define AuErr1(fmt, ...) do { \
1facf9fc 7833+ static unsigned char _c; \
7834+ if (!_c++) \
4a4d8108 7835+ pr_err(fmt, ##__VA_ARGS__); \
1facf9fc 7836+} while (0)
7837+
4a4d8108 7838+#define AuIOErr1(fmt, ...) do { \
1facf9fc 7839+ static unsigned char _c; \
7840+ if (!_c++) \
4a4d8108 7841+ AuIOErr(fmt, ##__VA_ARGS__); \
1facf9fc 7842+} while (0)
7843+
7844+#define AuUnsupportMsg "This operation is not supported." \
7845+ " Please report this application to aufs-users ML."
4a4d8108
AM
7846+#define AuUnsupport(fmt, ...) do { \
7847+ pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
1facf9fc 7848+ dump_stack(); \
7849+} while (0)
7850+
7851+#define AuTraceErr(e) do { \
7852+ if (unlikely((e) < 0)) \
7853+ AuDbg("err %d\n", (int)(e)); \
7854+} while (0)
7855+
7856+#define AuTraceErrPtr(p) do { \
7857+ if (IS_ERR(p)) \
7858+ AuDbg("err %ld\n", PTR_ERR(p)); \
7859+} while (0)
7860+
7861+/* dirty macros for debug print, use with "%.*s" and caution */
7862+#define AuLNPair(qstr) (qstr)->len, (qstr)->name
1facf9fc 7863+
7864+/* ---------------------------------------------------------------------- */
7865+
dece6358 7866+struct dentry;
1facf9fc 7867+#ifdef CONFIG_AUFS_DEBUG
c1595e42 7868+extern struct mutex au_dbg_mtx;
1facf9fc 7869+extern char *au_plevel;
7870+struct au_nhash;
7871+void au_dpri_whlist(struct au_nhash *whlist);
7872+struct au_vdir;
7873+void au_dpri_vdir(struct au_vdir *vdir);
dece6358 7874+struct inode;
1facf9fc 7875+void au_dpri_inode(struct inode *inode);
2cbb1c4b 7876+void au_dpri_dalias(struct inode *inode);
1facf9fc 7877+void au_dpri_dentry(struct dentry *dentry);
dece6358 7878+struct file;
1facf9fc 7879+void au_dpri_file(struct file *filp);
dece6358 7880+struct super_block;
1facf9fc 7881+void au_dpri_sb(struct super_block *sb);
7882+
027c5e7a
AM
7883+#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
7884+void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
1facf9fc 7885+void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
1facf9fc 7886+void au_dbg_verify_kthread(void);
7887+
7888+int __init au_debug_init(void);
7e9cd9fe 7889+
1facf9fc 7890+#define AuDbgWhlist(w) do { \
c1595e42 7891+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7892+ AuDbg(#w "\n"); \
7893+ au_dpri_whlist(w); \
c1595e42 7894+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7895+} while (0)
7896+
7897+#define AuDbgVdir(v) do { \
c1595e42 7898+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7899+ AuDbg(#v "\n"); \
7900+ au_dpri_vdir(v); \
c1595e42 7901+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7902+} while (0)
7903+
7904+#define AuDbgInode(i) do { \
c1595e42 7905+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7906+ AuDbg(#i "\n"); \
7907+ au_dpri_inode(i); \
c1595e42 7908+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7909+} while (0)
7910+
2cbb1c4b 7911+#define AuDbgDAlias(i) do { \
c1595e42 7912+ mutex_lock(&au_dbg_mtx); \
2cbb1c4b
JR
7913+ AuDbg(#i "\n"); \
7914+ au_dpri_dalias(i); \
c1595e42 7915+ mutex_unlock(&au_dbg_mtx); \
2cbb1c4b
JR
7916+} while (0)
7917+
1facf9fc 7918+#define AuDbgDentry(d) do { \
c1595e42 7919+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7920+ AuDbg(#d "\n"); \
7921+ au_dpri_dentry(d); \
c1595e42 7922+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7923+} while (0)
7924+
7925+#define AuDbgFile(f) do { \
c1595e42 7926+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7927+ AuDbg(#f "\n"); \
7928+ au_dpri_file(f); \
c1595e42 7929+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7930+} while (0)
7931+
7932+#define AuDbgSb(sb) do { \
c1595e42 7933+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7934+ AuDbg(#sb "\n"); \
7935+ au_dpri_sb(sb); \
c1595e42 7936+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7937+} while (0)
7938+
4a4d8108
AM
7939+#define AuDbgSym(addr) do { \
7940+ char sym[KSYM_SYMBOL_LEN]; \
7941+ sprint_symbol(sym, (unsigned long)addr); \
7942+ AuDbg("%s\n", sym); \
7943+} while (0)
1facf9fc 7944+#else
027c5e7a 7945+AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
4a4d8108
AM
7946+AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
7947+AuStubVoid(au_dbg_verify_kthread, void)
7948+AuStubInt0(__init au_debug_init, void)
1facf9fc 7949+
1facf9fc 7950+#define AuDbgWhlist(w) do {} while (0)
7951+#define AuDbgVdir(v) do {} while (0)
7952+#define AuDbgInode(i) do {} while (0)
2cbb1c4b 7953+#define AuDbgDAlias(i) do {} while (0)
1facf9fc 7954+#define AuDbgDentry(d) do {} while (0)
7955+#define AuDbgFile(f) do {} while (0)
7956+#define AuDbgSb(sb) do {} while (0)
4a4d8108 7957+#define AuDbgSym(addr) do {} while (0)
1facf9fc 7958+#endif /* CONFIG_AUFS_DEBUG */
7959+
7960+/* ---------------------------------------------------------------------- */
7961+
7962+#ifdef CONFIG_AUFS_MAGIC_SYSRQ
7963+int __init au_sysrq_init(void);
7964+void au_sysrq_fin(void);
7965+
7966+#ifdef CONFIG_HW_CONSOLE
7967+#define au_dbg_blocked() do { \
7968+ WARN_ON(1); \
0c5527e5 7969+ handle_sysrq('w'); \
1facf9fc 7970+} while (0)
7971+#else
4a4d8108 7972+AuStubVoid(au_dbg_blocked, void)
1facf9fc 7973+#endif
7974+
7975+#else
4a4d8108
AM
7976+AuStubInt0(__init au_sysrq_init, void)
7977+AuStubVoid(au_sysrq_fin, void)
7978+AuStubVoid(au_dbg_blocked, void)
1facf9fc 7979+#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
7980+
7981+#endif /* __KERNEL__ */
7982+#endif /* __AUFS_DEBUG_H__ */
7f207e10 7983diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
eca34b5c 7984--- /usr/share/empty/fs/aufs/dentry.c 1970-01-01 01:00:00.000000000 +0100
46016270 7985+++ linux/fs/aufs/dentry.c 2021-06-30 21:35:11.393873211 +0200
0b2a12c6 7986@@ -0,0 +1,1160 @@
cd7a4cd9 7987+// SPDX-License-Identifier: GPL-2.0
1facf9fc 7988+/*
d58c55f2 7989+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 7990+ *
7991+ * This program, aufs is free software; you can redistribute it and/or modify
7992+ * it under the terms of the GNU General Public License as published by
7993+ * the Free Software Foundation; either version 2 of the License, or
7994+ * (at your option) any later version.
dece6358
AM
7995+ *
7996+ * This program is distributed in the hope that it will be useful,
7997+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7998+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7999+ * GNU General Public License for more details.
8000+ *
8001+ * You should have received a copy of the GNU General Public License
523b37e3 8002+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 8003+ */
8004+
8005+/*
8006+ * lookup and dentry operations
8007+ */
8008+
eca801bf 8009+#include <linux/iversion.h>
dece6358 8010+#include <linux/namei.h>
1facf9fc 8011+#include "aufs.h"
8012+
1facf9fc 8013+/*
8014+ * returns positive/negative dentry, NULL or an error.
8015+ * NULL means whiteout-ed or not-found.
8016+ */
8017+static struct dentry*
8018+au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
8b6a4947 8019+ aufs_bindex_t bindex, struct au_do_lookup_args *args)
1facf9fc 8020+{
8021+ struct dentry *h_dentry;
2000de60 8022+ struct inode *h_inode;
1facf9fc 8023+ struct au_branch *br;
0b2a12c6 8024+ struct user_namespace *h_userns;
1facf9fc 8025+ int wh_found, opq;
8026+ unsigned char wh_able;
8027+ const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
076b876e
AM
8028+ const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
8029+ IGNORE_PERM);
1facf9fc 8030+
1facf9fc 8031+ wh_found = 0;
8032+ br = au_sbr(dentry->d_sb, bindex);
0b2a12c6 8033+ h_userns = au_br_userns(br);
1facf9fc 8034+ wh_able = !!au_br_whable(br->br_perm);
8035+ if (wh_able)
0b2a12c6
JR
8036+ wh_found = au_wh_test(h_userns, h_parent, &args->whname,
8037+ ignore_perm);
1facf9fc 8038+ h_dentry = ERR_PTR(wh_found);
8039+ if (!wh_found)
8040+ goto real_lookup;
8041+ if (unlikely(wh_found < 0))
8042+ goto out;
8043+
8044+ /* We found a whiteout */
5afbbe0d 8045+ /* au_set_dbbot(dentry, bindex); */
1facf9fc 8046+ au_set_dbwh(dentry, bindex);
8047+ if (!allow_neg)
8048+ return NULL; /* success */
8049+
4f0767ce 8050+real_lookup:
076b876e 8051+ if (!ignore_perm)
8b6a4947 8052+ h_dentry = vfsub_lkup_one(args->name, h_parent);
076b876e 8053+ else
0b2a12c6 8054+ h_dentry = au_sio_lkup_one(h_userns, args->name, h_parent);
2000de60
JR
8055+ if (IS_ERR(h_dentry)) {
8056+ if (PTR_ERR(h_dentry) == -ENAMETOOLONG
8057+ && !allow_neg)
8058+ h_dentry = NULL;
1facf9fc 8059+ goto out;
2000de60 8060+ }
1facf9fc 8061+
5527c038
JR
8062+ h_inode = d_inode(h_dentry);
8063+ if (d_is_negative(h_dentry)) {
1facf9fc 8064+ if (!allow_neg)
8065+ goto out_neg;
8066+ } else if (wh_found
8067+ || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
8068+ goto out_neg;
8b6a4947
AM
8069+ else if (au_ftest_lkup(args->flags, DIRREN)
8070+ /* && h_inode */
8071+ && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
8072+ AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
8073+ (unsigned long long)h_inode->i_ino);
8074+ goto out_neg;
8075+ }
1facf9fc 8076+
5afbbe0d
AM
8077+ if (au_dbbot(dentry) <= bindex)
8078+ au_set_dbbot(dentry, bindex);
8079+ if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
8080+ au_set_dbtop(dentry, bindex);
1facf9fc 8081+ au_set_h_dptr(dentry, bindex, h_dentry);
8082+
2000de60
JR
8083+ if (!d_is_dir(h_dentry)
8084+ || !wh_able
5527c038 8085+ || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
1facf9fc 8086+ goto out; /* success */
8087+
be118d29 8088+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
0b2a12c6 8089+ opq = au_diropq_test(h_userns, h_dentry);
3c1bdaff 8090+ inode_unlock_shared(h_inode);
1facf9fc 8091+ if (opq > 0)
8092+ au_set_dbdiropq(dentry, bindex);
8093+ else if (unlikely(opq < 0)) {
8094+ au_set_h_dptr(dentry, bindex, NULL);
8095+ h_dentry = ERR_PTR(opq);
8096+ }
8097+ goto out;
8098+
4f0767ce 8099+out_neg:
1facf9fc 8100+ dput(h_dentry);
8101+ h_dentry = NULL;
4f0767ce 8102+out:
1facf9fc 8103+ return h_dentry;
8104+}
8105+
dece6358
AM
8106+static int au_test_shwh(struct super_block *sb, const struct qstr *name)
8107+{
8108+ if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
8109+ && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
8110+ return -EPERM;
8111+ return 0;
8112+}
8113+
1facf9fc 8114+/*
8115+ * returns the number of lower positive dentries,
8116+ * otherwise an error.
8117+ * can be called at unlinking with @type is zero.
8118+ */
5afbbe0d
AM
8119+int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
8120+ unsigned int flags)
1facf9fc 8121+{
8122+ int npositive, err;
8123+ aufs_bindex_t bindex, btail, bdiropq;
8b6a4947 8124+ unsigned char isdir, dirperm1, dirren;
1facf9fc 8125+ struct au_do_lookup_args args = {
8b6a4947
AM
8126+ .flags = flags,
8127+ .name = &dentry->d_name
1facf9fc 8128+ };
1facf9fc 8129+ struct dentry *parent;
076b876e 8130+ struct super_block *sb;
1facf9fc 8131+
076b876e 8132+ sb = dentry->d_sb;
8b6a4947 8133+ err = au_test_shwh(sb, args.name);
dece6358 8134+ if (unlikely(err))
1facf9fc 8135+ goto out;
8136+
8b6a4947 8137+ err = au_wh_name_alloc(&args.whname, args.name);
1facf9fc 8138+ if (unlikely(err))
8139+ goto out;
8140+
2000de60 8141+ isdir = !!d_is_dir(dentry);
076b876e 8142+ dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
8b6a4947
AM
8143+ dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
8144+ if (dirren)
8145+ au_fset_lkup(args.flags, DIRREN);
1facf9fc 8146+
8147+ npositive = 0;
4a4d8108 8148+ parent = dget_parent(dentry);
1facf9fc 8149+ btail = au_dbtaildir(parent);
5afbbe0d 8150+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 8151+ struct dentry *h_parent, *h_dentry;
8152+ struct inode *h_inode, *h_dir;
8b6a4947 8153+ struct au_branch *br;
1facf9fc 8154+
8155+ h_dentry = au_h_dptr(dentry, bindex);
8156+ if (h_dentry) {
5527c038 8157+ if (d_is_positive(h_dentry))
1facf9fc 8158+ npositive++;
5afbbe0d 8159+ break;
1facf9fc 8160+ }
8161+ h_parent = au_h_dptr(parent, bindex);
2000de60 8162+ if (!h_parent || !d_is_dir(h_parent))
1facf9fc 8163+ continue;
8164+
8b6a4947
AM
8165+ if (dirren) {
8166+ /* if the inum matches, then use the prepared name */
8167+ err = au_dr_lkup_name(&args, bindex);
8168+ if (unlikely(err))
8169+ goto out_parent;
8170+ }
8171+
5527c038 8172+ h_dir = d_inode(h_parent);
be118d29 8173+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8b6a4947 8174+ h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
3c1bdaff 8175+ inode_unlock_shared(h_dir);
1facf9fc 8176+ err = PTR_ERR(h_dentry);
8177+ if (IS_ERR(h_dentry))
4a4d8108 8178+ goto out_parent;
2000de60
JR
8179+ if (h_dentry)
8180+ au_fclr_lkup(args.flags, ALLOW_NEG);
076b876e
AM
8181+ if (dirperm1)
8182+ au_fset_lkup(args.flags, IGNORE_PERM);
1facf9fc 8183+
79b8bda9 8184+ if (au_dbwh(dentry) == bindex)
1facf9fc 8185+ break;
8186+ if (!h_dentry)
8187+ continue;
5527c038 8188+ if (d_is_negative(h_dentry))
1facf9fc 8189+ continue;
5527c038 8190+ h_inode = d_inode(h_dentry);
1facf9fc 8191+ npositive++;
8192+ if (!args.type)
8193+ args.type = h_inode->i_mode & S_IFMT;
8194+ if (args.type != S_IFDIR)
8195+ break;
8196+ else if (isdir) {
8197+ /* the type of lower may be different */
8198+ bdiropq = au_dbdiropq(dentry);
8199+ if (bdiropq >= 0 && bdiropq <= bindex)
8200+ break;
8201+ }
8b6a4947
AM
8202+ br = au_sbr(sb, bindex);
8203+ if (dirren
8204+ && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
8205+ /*add_ent*/NULL)) {
8206+ /* prepare next name to lookup */
8207+ err = au_dr_lkup(&args, dentry, bindex);
8208+ if (unlikely(err))
8209+ goto out_parent;
8210+ }
1facf9fc 8211+ }
8212+
8213+ if (npositive) {
8214+ AuLabel(positive);
5afbbe0d 8215+ au_update_dbtop(dentry);
1facf9fc 8216+ }
8217+ err = npositive;
076b876e 8218+ if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
5afbbe0d 8219+ && au_dbtop(dentry) < 0)) {
1facf9fc 8220+ err = -EIO;
523b37e3
AM
8221+ AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
8222+ dentry, err);
027c5e7a 8223+ }
1facf9fc 8224+
4f0767ce 8225+out_parent:
4a4d8108 8226+ dput(parent);
9f237c51 8227+ au_kfree_try_rcu(args.whname.name);
8b6a4947
AM
8228+ if (dirren)
8229+ au_dr_lkup_fin(&args);
4f0767ce 8230+out:
1facf9fc 8231+ return err;
8232+}
8233+
0b2a12c6
JR
8234+struct dentry *au_sio_lkup_one(struct user_namespace *userns, struct qstr *name,
8235+ struct dentry *parent)
1facf9fc 8236+{
8237+ struct dentry *dentry;
8238+ int wkq_err;
8239+
0b2a12c6 8240+ if (!au_test_h_perm_sio(userns, d_inode(parent), MAY_EXEC))
b4510431 8241+ dentry = vfsub_lkup_one(name, parent);
1facf9fc 8242+ else {
b4510431
AM
8243+ struct vfsub_lkup_one_args args = {
8244+ .errp = &dentry,
8245+ .name = name,
8246+ .parent = parent
1facf9fc 8247+ };
8248+
b4510431 8249+ wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
1facf9fc 8250+ if (unlikely(wkq_err))
8251+ dentry = ERR_PTR(wkq_err);
8252+ }
8253+
8254+ return dentry;
8255+}
8256+
8257+/*
8258+ * lookup @dentry on @bindex which should be negative.
8259+ */
86dc4139 8260+int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
1facf9fc 8261+{
8262+ int err;
8263+ struct dentry *parent, *h_parent, *h_dentry;
86dc4139 8264+ struct au_branch *br;
0b2a12c6 8265+ struct user_namespace *h_userns;
1facf9fc 8266+
1facf9fc 8267+ parent = dget_parent(dentry);
8268+ h_parent = au_h_dptr(parent, bindex);
86dc4139 8269+ br = au_sbr(dentry->d_sb, bindex);
0b2a12c6 8270+ h_userns = au_br_userns(br);
86dc4139
AM
8271+ if (wh)
8272+ h_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
8273+ else
0b2a12c6 8274+ h_dentry = au_sio_lkup_one(h_userns, &dentry->d_name, h_parent);
1facf9fc 8275+ err = PTR_ERR(h_dentry);
8276+ if (IS_ERR(h_dentry))
8277+ goto out;
5527c038 8278+ if (unlikely(d_is_positive(h_dentry))) {
1facf9fc 8279+ err = -EIO;
523b37e3 8280+ AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
1facf9fc 8281+ dput(h_dentry);
8282+ goto out;
8283+ }
8284+
4a4d8108 8285+ err = 0;
5afbbe0d
AM
8286+ if (bindex < au_dbtop(dentry))
8287+ au_set_dbtop(dentry, bindex);
8288+ if (au_dbbot(dentry) < bindex)
8289+ au_set_dbbot(dentry, bindex);
1facf9fc 8290+ au_set_h_dptr(dentry, bindex, h_dentry);
1facf9fc 8291+
4f0767ce 8292+out:
1facf9fc 8293+ dput(parent);
8294+ return err;
8295+}
8296+
8297+/* ---------------------------------------------------------------------- */
8298+
8299+/* subset of struct inode */
8300+struct au_iattr {
8301+ unsigned long i_ino;
8302+ /* unsigned int i_nlink; */
0c3ec466
AM
8303+ kuid_t i_uid;
8304+ kgid_t i_gid;
1facf9fc 8305+ u64 i_version;
8306+/*
8307+ loff_t i_size;
8308+ blkcnt_t i_blocks;
8309+*/
8310+ umode_t i_mode;
8311+};
8312+
8313+static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
8314+{
8315+ ia->i_ino = h_inode->i_ino;
8316+ /* ia->i_nlink = h_inode->i_nlink; */
8317+ ia->i_uid = h_inode->i_uid;
8318+ ia->i_gid = h_inode->i_gid;
be118d29 8319+ ia->i_version = inode_query_iversion(h_inode);
1facf9fc 8320+/*
8321+ ia->i_size = h_inode->i_size;
8322+ ia->i_blocks = h_inode->i_blocks;
8323+*/
8324+ ia->i_mode = (h_inode->i_mode & S_IFMT);
8325+}
8326+
8327+static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
8328+{
8329+ return ia->i_ino != h_inode->i_ino
8330+ /* || ia->i_nlink != h_inode->i_nlink */
0c3ec466 8331+ || !uid_eq(ia->i_uid, h_inode->i_uid)
2dfbb274 8332+ || !gid_eq(ia->i_gid, h_inode->i_gid)
be118d29 8333+ || !inode_eq_iversion(h_inode, ia->i_version)
1facf9fc 8334+/*
8335+ || ia->i_size != h_inode->i_size
8336+ || ia->i_blocks != h_inode->i_blocks
8337+*/
8338+ || ia->i_mode != (h_inode->i_mode & S_IFMT);
8339+}
8340+
8341+static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
8342+ struct au_branch *br)
8343+{
8344+ int err;
8345+ struct au_iattr ia;
8346+ struct inode *h_inode;
8347+ struct dentry *h_d;
8348+ struct super_block *h_sb;
8349+
8350+ err = 0;
8351+ memset(&ia, -1, sizeof(ia));
8352+ h_sb = h_dentry->d_sb;
5527c038
JR
8353+ h_inode = NULL;
8354+ if (d_is_positive(h_dentry)) {
8355+ h_inode = d_inode(h_dentry);
1facf9fc 8356+ au_iattr_save(&ia, h_inode);
5527c038 8357+ } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
1facf9fc 8358+ /* nfs d_revalidate may return 0 for negative dentry */
8359+ /* fuse d_revalidate always return 0 for negative dentry */
8360+ goto out;
8361+
8362+ /* main purpose is namei.c:cached_lookup() and d_revalidate */
b4510431 8363+ h_d = vfsub_lkup_one(&h_dentry->d_name, h_parent);
1facf9fc 8364+ err = PTR_ERR(h_d);
8365+ if (IS_ERR(h_d))
8366+ goto out;
8367+
8368+ err = 0;
8369+ if (unlikely(h_d != h_dentry
5527c038 8370+ || d_inode(h_d) != h_inode
1facf9fc 8371+ || (h_inode && au_iattr_test(&ia, h_inode))))
8372+ err = au_busy_or_stale();
8373+ dput(h_d);
8374+
4f0767ce 8375+out:
1facf9fc 8376+ AuTraceErr(err);
8377+ return err;
8378+}
8379+
8380+int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8381+ struct dentry *h_parent, struct au_branch *br)
8382+{
8383+ int err;
8384+
8385+ err = 0;
027c5e7a
AM
8386+ if (udba == AuOpt_UDBA_REVAL
8387+ && !au_test_fs_remote(h_dentry->d_sb)) {
1facf9fc 8388+ IMustLock(h_dir);
5527c038 8389+ err = (d_inode(h_dentry->d_parent) != h_dir);
027c5e7a 8390+ } else if (udba != AuOpt_UDBA_NONE)
1facf9fc 8391+ err = au_h_verify_dentry(h_dentry, h_parent, br);
8392+
8393+ return err;
8394+}
8395+
8396+/* ---------------------------------------------------------------------- */
8397+
027c5e7a 8398+static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
1facf9fc 8399+{
027c5e7a 8400+ int err;
5afbbe0d 8401+ aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
027c5e7a
AM
8402+ struct au_hdentry tmp, *p, *q;
8403+ struct au_dinfo *dinfo;
8404+ struct super_block *sb;
1facf9fc 8405+
027c5e7a 8406+ DiMustWriteLock(dentry);
1308ab2a 8407+
027c5e7a
AM
8408+ sb = dentry->d_sb;
8409+ dinfo = au_di(dentry);
5afbbe0d 8410+ bbot = dinfo->di_bbot;
1facf9fc 8411+ bwh = dinfo->di_bwh;
8412+ bdiropq = dinfo->di_bdiropq;
5afbbe0d
AM
8413+ bindex = dinfo->di_btop;
8414+ p = au_hdentry(dinfo, bindex);
8415+ for (; bindex <= bbot; bindex++, p++) {
027c5e7a 8416+ if (!p->hd_dentry)
1facf9fc 8417+ continue;
8418+
027c5e7a
AM
8419+ new_bindex = au_br_index(sb, p->hd_id);
8420+ if (new_bindex == bindex)
1facf9fc 8421+ continue;
1facf9fc 8422+
1facf9fc 8423+ if (dinfo->di_bwh == bindex)
8424+ bwh = new_bindex;
8425+ if (dinfo->di_bdiropq == bindex)
8426+ bdiropq = new_bindex;
8427+ if (new_bindex < 0) {
8428+ au_hdput(p);
8429+ p->hd_dentry = NULL;
8430+ continue;
8431+ }
8432+
8433+ /* swap two lower dentries, and loop again */
5afbbe0d 8434+ q = au_hdentry(dinfo, new_bindex);
1facf9fc 8435+ tmp = *q;
8436+ *q = *p;
8437+ *p = tmp;
8438+ if (tmp.hd_dentry) {
8439+ bindex--;
8440+ p--;
8441+ }
8442+ }
8443+
1facf9fc 8444+ dinfo->di_bwh = -1;
5afbbe0d 8445+ if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
1facf9fc 8446+ dinfo->di_bwh = bwh;
8447+
8448+ dinfo->di_bdiropq = -1;
8449+ if (bdiropq >= 0
5afbbe0d 8450+ && bdiropq <= au_sbbot(sb)
1facf9fc 8451+ && au_sbr_whable(sb, bdiropq))
8452+ dinfo->di_bdiropq = bdiropq;
8453+
027c5e7a 8454+ err = -EIO;
5afbbe0d
AM
8455+ dinfo->di_btop = -1;
8456+ dinfo->di_bbot = -1;
8457+ bbot = au_dbbot(parent);
8458+ bindex = 0;
8459+ p = au_hdentry(dinfo, bindex);
8460+ for (; bindex <= bbot; bindex++, p++)
1facf9fc 8461+ if (p->hd_dentry) {
5afbbe0d 8462+ dinfo->di_btop = bindex;
1facf9fc 8463+ break;
8464+ }
8465+
5afbbe0d
AM
8466+ if (dinfo->di_btop >= 0) {
8467+ bindex = bbot;
8468+ p = au_hdentry(dinfo, bindex);
8469+ for (; bindex >= 0; bindex--, p--)
027c5e7a 8470+ if (p->hd_dentry) {
5afbbe0d 8471+ dinfo->di_bbot = bindex;
027c5e7a
AM
8472+ err = 0;
8473+ break;
8474+ }
8475+ }
8476+
8477+ return err;
1facf9fc 8478+}
8479+
027c5e7a 8480+static void au_do_hide(struct dentry *dentry)
1facf9fc 8481+{
027c5e7a 8482+ struct inode *inode;
1facf9fc 8483+
5527c038
JR
8484+ if (d_really_is_positive(dentry)) {
8485+ inode = d_inode(dentry);
8486+ if (!d_is_dir(dentry)) {
027c5e7a
AM
8487+ if (inode->i_nlink && !d_unhashed(dentry))
8488+ drop_nlink(inode);
8489+ } else {
8490+ clear_nlink(inode);
8491+ /* stop next lookup */
8492+ inode->i_flags |= S_DEAD;
8493+ }
8494+ smp_mb(); /* necessary? */
8495+ }
8496+ d_drop(dentry);
8497+}
1308ab2a 8498+
027c5e7a
AM
8499+static int au_hide_children(struct dentry *parent)
8500+{
8501+ int err, i, j, ndentry;
8502+ struct au_dcsub_pages dpages;
8503+ struct au_dpage *dpage;
8504+ struct dentry *dentry;
1facf9fc 8505+
027c5e7a 8506+ err = au_dpages_init(&dpages, GFP_NOFS);
1facf9fc 8507+ if (unlikely(err))
8508+ goto out;
027c5e7a
AM
8509+ err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8510+ if (unlikely(err))
8511+ goto out_dpages;
1facf9fc 8512+
027c5e7a
AM
8513+ /* in reverse order */
8514+ for (i = dpages.ndpage - 1; i >= 0; i--) {
8515+ dpage = dpages.dpages + i;
8516+ ndentry = dpage->ndentry;
8517+ for (j = ndentry - 1; j >= 0; j--) {
8518+ dentry = dpage->dentries[j];
8519+ if (dentry != parent)
8520+ au_do_hide(dentry);
8521+ }
8522+ }
1facf9fc 8523+
027c5e7a
AM
8524+out_dpages:
8525+ au_dpages_free(&dpages);
4f0767ce 8526+out:
027c5e7a 8527+ return err;
1facf9fc 8528+}
8529+
027c5e7a 8530+static void au_hide(struct dentry *dentry)
1facf9fc 8531+{
027c5e7a 8532+ int err;
1facf9fc 8533+
027c5e7a 8534+ AuDbgDentry(dentry);
2000de60 8535+ if (d_is_dir(dentry)) {
027c5e7a
AM
8536+ /* shrink_dcache_parent(dentry); */
8537+ err = au_hide_children(dentry);
8538+ if (unlikely(err))
523b37e3
AM
8539+ AuIOErr("%pd, failed hiding children, ignored %d\n",
8540+ dentry, err);
027c5e7a
AM
8541+ }
8542+ au_do_hide(dentry);
8543+}
1facf9fc 8544+
027c5e7a
AM
8545+/*
8546+ * By adding a dirty branch, a cached dentry may be affected in various ways.
8547+ *
8548+ * a dirty branch is added
8549+ * - on the top of layers
8550+ * - in the middle of layers
8551+ * - to the bottom of layers
8552+ *
8553+ * on the added branch there exists
8554+ * - a whiteout
8555+ * - a diropq
8556+ * - a same named entry
8557+ * + exist
8558+ * * negative --> positive
8559+ * * positive --> positive
8560+ * - type is unchanged
8561+ * - type is changed
8562+ * + doesn't exist
8563+ * * negative --> negative
8564+ * * positive --> negative (rejected by au_br_del() for non-dir case)
8565+ * - none
8566+ */
8567+static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8568+ struct au_dinfo *tmp)
8569+{
8570+ int err;
5afbbe0d 8571+ aufs_bindex_t bindex, bbot;
027c5e7a
AM
8572+ struct {
8573+ struct dentry *dentry;
8574+ struct inode *inode;
8575+ mode_t mode;
be52b249
AM
8576+ } orig_h, tmp_h = {
8577+ .dentry = NULL
8578+ };
027c5e7a
AM
8579+ struct au_hdentry *hd;
8580+ struct inode *inode, *h_inode;
8581+ struct dentry *h_dentry;
8582+
8583+ err = 0;
5afbbe0d 8584+ AuDebugOn(dinfo->di_btop < 0);
027c5e7a 8585+ orig_h.mode = 0;
5afbbe0d 8586+ orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
5527c038
JR
8587+ orig_h.inode = NULL;
8588+ if (d_is_positive(orig_h.dentry)) {
8589+ orig_h.inode = d_inode(orig_h.dentry);
027c5e7a 8590+ orig_h.mode = orig_h.inode->i_mode & S_IFMT;
5527c038 8591+ }
5afbbe0d
AM
8592+ if (tmp->di_btop >= 0) {
8593+ tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
5527c038
JR
8594+ if (d_is_positive(tmp_h.dentry)) {
8595+ tmp_h.inode = d_inode(tmp_h.dentry);
027c5e7a 8596+ tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
5527c038 8597+ }
027c5e7a
AM
8598+ }
8599+
5527c038
JR
8600+ inode = NULL;
8601+ if (d_really_is_positive(dentry))
8602+ inode = d_inode(dentry);
027c5e7a 8603+ if (!orig_h.inode) {
acd2b654 8604+ AuDbg("negative originally\n");
027c5e7a
AM
8605+ if (inode) {
8606+ au_hide(dentry);
8607+ goto out;
8608+ }
8609+ AuDebugOn(inode);
5afbbe0d 8610+ AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
027c5e7a
AM
8611+ AuDebugOn(dinfo->di_bdiropq != -1);
8612+
8613+ if (!tmp_h.inode) {
8614+ AuDbg("negative --> negative\n");
8615+ /* should have only one negative lower */
5afbbe0d
AM
8616+ if (tmp->di_btop >= 0
8617+ && tmp->di_btop < dinfo->di_btop) {
8618+ AuDebugOn(tmp->di_btop != tmp->di_bbot);
8619+ AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8620+ au_set_h_dptr(dentry, dinfo->di_btop, NULL);
027c5e7a 8621+ au_di_cp(dinfo, tmp);
5afbbe0d
AM
8622+ hd = au_hdentry(tmp, tmp->di_btop);
8623+ au_set_h_dptr(dentry, tmp->di_btop,
027c5e7a
AM
8624+ dget(hd->hd_dentry));
8625+ }
8626+ au_dbg_verify_dinode(dentry);
8627+ } else {
8628+ AuDbg("negative --> positive\n");
8629+ /*
8630+ * similar to the behaviour of creating with bypassing
8631+ * aufs.
8632+ * unhash it in order to force an error in the
8633+ * succeeding create operation.
8634+ * we should not set S_DEAD here.
8635+ */
8636+ d_drop(dentry);
8637+ /* au_di_swap(tmp, dinfo); */
8638+ au_dbg_verify_dinode(dentry);
8639+ }
8640+ } else {
8641+ AuDbg("positive originally\n");
8642+ /* inode may be NULL */
8643+ AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8644+ if (!tmp_h.inode) {
8645+ AuDbg("positive --> negative\n");
8646+ /* or bypassing aufs */
8647+ au_hide(dentry);
5afbbe0d 8648+ if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
027c5e7a
AM
8649+ dinfo->di_bwh = tmp->di_bwh;
8650+ if (inode)
8651+ err = au_refresh_hinode_self(inode);
8652+ au_dbg_verify_dinode(dentry);
8653+ } else if (orig_h.mode == tmp_h.mode) {
8654+ AuDbg("positive --> positive, same type\n");
8655+ if (!S_ISDIR(orig_h.mode)
5afbbe0d 8656+ && dinfo->di_btop > tmp->di_btop) {
027c5e7a
AM
8657+ /*
8658+ * similar to the behaviour of removing and
8659+ * creating.
8660+ */
8661+ au_hide(dentry);
8662+ if (inode)
8663+ err = au_refresh_hinode_self(inode);
8664+ au_dbg_verify_dinode(dentry);
8665+ } else {
8666+ /* fill empty slots */
5afbbe0d
AM
8667+ if (dinfo->di_btop > tmp->di_btop)
8668+ dinfo->di_btop = tmp->di_btop;
8669+ if (dinfo->di_bbot < tmp->di_bbot)
8670+ dinfo->di_bbot = tmp->di_bbot;
027c5e7a
AM
8671+ dinfo->di_bwh = tmp->di_bwh;
8672+ dinfo->di_bdiropq = tmp->di_bdiropq;
5afbbe0d
AM
8673+ bbot = dinfo->di_bbot;
8674+ bindex = tmp->di_btop;
8675+ hd = au_hdentry(tmp, bindex);
8676+ for (; bindex <= bbot; bindex++, hd++) {
027c5e7a
AM
8677+ if (au_h_dptr(dentry, bindex))
8678+ continue;
5afbbe0d 8679+ h_dentry = hd->hd_dentry;
027c5e7a
AM
8680+ if (!h_dentry)
8681+ continue;
5527c038
JR
8682+ AuDebugOn(d_is_negative(h_dentry));
8683+ h_inode = d_inode(h_dentry);
027c5e7a
AM
8684+ AuDebugOn(orig_h.mode
8685+ != (h_inode->i_mode
8686+ & S_IFMT));
8687+ au_set_h_dptr(dentry, bindex,
8688+ dget(h_dentry));
8689+ }
5afbbe0d
AM
8690+ if (inode)
8691+ err = au_refresh_hinode(inode, dentry);
027c5e7a
AM
8692+ au_dbg_verify_dinode(dentry);
8693+ }
8694+ } else {
8695+ AuDbg("positive --> positive, different type\n");
8696+ /* similar to the behaviour of removing and creating */
8697+ au_hide(dentry);
8698+ if (inode)
8699+ err = au_refresh_hinode_self(inode);
8700+ au_dbg_verify_dinode(dentry);
8701+ }
8702+ }
8703+
8704+out:
8705+ return err;
8706+}
8707+
79b8bda9
AM
8708+void au_refresh_dop(struct dentry *dentry, int force_reval)
8709+{
8710+ const struct dentry_operations *dop
8711+ = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8712+ static const unsigned int mask
8713+ = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8714+
8715+ BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8716+
8717+ if (dentry->d_op == dop)
8718+ return;
8719+
8720+ AuDbg("%pd\n", dentry);
8721+ spin_lock(&dentry->d_lock);
8722+ if (dop == &aufs_dop)
8723+ dentry->d_flags |= mask;
8724+ else
8725+ dentry->d_flags &= ~mask;
8726+ dentry->d_op = dop;
8727+ spin_unlock(&dentry->d_lock);
8728+}
8729+
027c5e7a
AM
8730+int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8731+{
e2f27e51 8732+ int err, ebrange, nbr;
027c5e7a
AM
8733+ unsigned int sigen;
8734+ struct au_dinfo *dinfo, *tmp;
8735+ struct super_block *sb;
8736+ struct inode *inode;
8737+
8738+ DiMustWriteLock(dentry);
8739+ AuDebugOn(IS_ROOT(dentry));
5527c038 8740+ AuDebugOn(d_really_is_negative(parent));
027c5e7a
AM
8741+
8742+ sb = dentry->d_sb;
027c5e7a
AM
8743+ sigen = au_sigen(sb);
8744+ err = au_digen_test(parent, sigen);
8745+ if (unlikely(err))
8746+ goto out;
8747+
e2f27e51 8748+ nbr = au_sbbot(sb) + 1;
027c5e7a 8749+ dinfo = au_di(dentry);
e2f27e51 8750+ err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
027c5e7a
AM
8751+ if (unlikely(err))
8752+ goto out;
8753+ ebrange = au_dbrange_test(dentry);
8754+ if (!ebrange)
8755+ ebrange = au_do_refresh_hdentry(dentry, parent);
8756+
38d290e6 8757+ if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
5afbbe0d 8758+ AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
5527c038
JR
8759+ if (d_really_is_positive(dentry)) {
8760+ inode = d_inode(dentry);
027c5e7a 8761+ err = au_refresh_hinode_self(inode);
5527c038 8762+ }
027c5e7a
AM
8763+ au_dbg_verify_dinode(dentry);
8764+ if (!err)
8765+ goto out_dgen; /* success */
8766+ goto out;
8767+ }
8768+
8769+ /* temporary dinfo */
8770+ AuDbgDentry(dentry);
8771+ err = -ENOMEM;
8772+ tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8773+ if (unlikely(!tmp))
8774+ goto out;
8775+ au_di_swap(tmp, dinfo);
8776+ /* returns the number of positive dentries */
8777+ /*
8778+ * if current working dir is removed, it returns an error.
8779+ * but the dentry is legal.
8780+ */
5afbbe0d 8781+ err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
027c5e7a
AM
8782+ AuDbgDentry(dentry);
8783+ au_di_swap(tmp, dinfo);
8784+ if (err == -ENOENT)
8785+ err = 0;
8786+ if (err >= 0) {
8787+ /* compare/refresh by dinfo */
8788+ AuDbgDentry(dentry);
8789+ err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8790+ au_dbg_verify_dinode(dentry);
8791+ AuTraceErr(err);
8792+ }
e2f27e51 8793+ au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
027c5e7a
AM
8794+ au_rw_write_unlock(&tmp->di_rwsem);
8795+ au_di_free(tmp);
8796+ if (unlikely(err))
8797+ goto out;
8798+
8799+out_dgen:
8800+ au_update_digen(dentry);
8801+out:
8802+ if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
523b37e3 8803+ AuIOErr("failed refreshing %pd, %d\n", dentry, err);
027c5e7a
AM
8804+ AuDbgDentry(dentry);
8805+ }
8806+ AuTraceErr(err);
8807+ return err;
8808+}
8809+
b4510431
AM
8810+static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8811+ struct dentry *dentry, aufs_bindex_t bindex)
027c5e7a
AM
8812+{
8813+ int err, valid;
027c5e7a
AM
8814+
8815+ err = 0;
8816+ if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8817+ goto out;
027c5e7a
AM
8818+
8819+ AuDbg("b%d\n", bindex);
b4510431
AM
8820+ /*
8821+ * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8822+ * due to whiteout and branch permission.
8823+ */
8824+ flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8825+ | LOOKUP_FOLLOW | LOOKUP_EXCL);
8826+ /* it may return tri-state */
8827+ valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
1facf9fc 8828+
8829+ if (unlikely(valid < 0))
8830+ err = valid;
8831+ else if (!valid)
8832+ err = -EINVAL;
8833+
4f0767ce 8834+out:
1facf9fc 8835+ AuTraceErr(err);
8836+ return err;
8837+}
8838+
8839+/* todo: remove this */
8840+static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8b6a4947 8841+ unsigned int flags, int do_udba, int dirren)
1facf9fc 8842+{
8843+ int err;
8844+ umode_t mode, h_mode;
5afbbe0d 8845+ aufs_bindex_t bindex, btail, btop, ibs, ibe;
38d290e6 8846+ unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
4a4d8108 8847+ struct inode *h_inode, *h_cached_inode;
1facf9fc 8848+ struct dentry *h_dentry;
8849+ struct qstr *name, *h_name;
8850+
8851+ err = 0;
8852+ plus = 0;
8853+ mode = 0;
1facf9fc 8854+ ibs = -1;
8855+ ibe = -1;
8856+ unhashed = !!d_unhashed(dentry);
8857+ is_root = !!IS_ROOT(dentry);
8858+ name = &dentry->d_name;
38d290e6 8859+ tmpfile = au_di(dentry)->di_tmpfile;
1facf9fc 8860+
8861+ /*
7f207e10
AM
8862+ * Theoretically, REVAL test should be unnecessary in case of
8863+ * {FS,I}NOTIFY.
8864+ * But {fs,i}notify doesn't fire some necessary events,
1facf9fc 8865+ * IN_ATTRIB for atime/nlink/pageio
1facf9fc 8866+ * Let's do REVAL test too.
8867+ */
8868+ if (do_udba && inode) {
8869+ mode = (inode->i_mode & S_IFMT);
8870+ plus = (inode->i_nlink > 0);
5afbbe0d
AM
8871+ ibs = au_ibtop(inode);
8872+ ibe = au_ibbot(inode);
1facf9fc 8873+ }
8874+
5afbbe0d
AM
8875+ btop = au_dbtop(dentry);
8876+ btail = btop;
1facf9fc 8877+ if (inode && S_ISDIR(inode->i_mode))
8878+ btail = au_dbtaildir(dentry);
5afbbe0d 8879+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 8880+ h_dentry = au_h_dptr(dentry, bindex);
8881+ if (!h_dentry)
8882+ continue;
8883+
523b37e3
AM
8884+ AuDbg("b%d, %pd\n", bindex, h_dentry);
8885+ h_nfs = !!au_test_nfs(h_dentry->d_sb);
027c5e7a 8886+ spin_lock(&h_dentry->d_lock);
1facf9fc 8887+ h_name = &h_dentry->d_name;
8888+ if (unlikely(do_udba
8889+ && !is_root
523b37e3
AM
8890+ && ((!h_nfs
8891+ && (unhashed != !!d_unhashed(h_dentry)
8b6a4947 8892+ || (!tmpfile && !dirren
38d290e6
JR
8893+ && !au_qstreq(name, h_name))
8894+ ))
523b37e3
AM
8895+ || (h_nfs
8896+ && !(flags & LOOKUP_OPEN)
8897+ && (h_dentry->d_flags
8898+ & DCACHE_NFSFS_RENAMED)))
1facf9fc 8899+ )) {
38d290e6
JR
8900+ int h_unhashed;
8901+
8902+ h_unhashed = d_unhashed(h_dentry);
027c5e7a 8903+ spin_unlock(&h_dentry->d_lock);
38d290e6
JR
8904+ AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
8905+ unhashed, h_unhashed, dentry, h_dentry);
1facf9fc 8906+ goto err;
8907+ }
027c5e7a 8908+ spin_unlock(&h_dentry->d_lock);
1facf9fc 8909+
b4510431 8910+ err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
1facf9fc 8911+ if (unlikely(err))
8912+ /* do not goto err, to keep the errno */
8913+ break;
8914+
8915+ /* todo: plink too? */
8916+ if (!do_udba)
8917+ continue;
8918+
8919+ /* UDBA tests */
5527c038 8920+ if (unlikely(!!inode != d_is_positive(h_dentry)))
1facf9fc 8921+ goto err;
8922+
5527c038
JR
8923+ h_inode = NULL;
8924+ if (d_is_positive(h_dentry))
8925+ h_inode = d_inode(h_dentry);
1facf9fc 8926+ h_plus = plus;
8927+ h_mode = mode;
8928+ h_cached_inode = h_inode;
8929+ if (h_inode) {
8930+ h_mode = (h_inode->i_mode & S_IFMT);
8931+ h_plus = (h_inode->i_nlink > 0);
8932+ }
8933+ if (inode && ibs <= bindex && bindex <= ibe)
8934+ h_cached_inode = au_h_iptr(inode, bindex);
8935+
523b37e3 8936+ if (!h_nfs) {
38d290e6 8937+ if (unlikely(plus != h_plus && !tmpfile))
523b37e3
AM
8938+ goto err;
8939+ } else {
8940+ if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
8941+ && !is_root
8942+ && !IS_ROOT(h_dentry)
8943+ && unhashed != d_unhashed(h_dentry)))
8944+ goto err;
8945+ }
8946+ if (unlikely(mode != h_mode
1facf9fc 8947+ || h_cached_inode != h_inode))
8948+ goto err;
8949+ continue;
8950+
f6b6e03d 8951+err:
1facf9fc 8952+ err = -EINVAL;
8953+ break;
8954+ }
8955+
523b37e3 8956+ AuTraceErr(err);
1facf9fc 8957+ return err;
8958+}
8959+
027c5e7a 8960+/* todo: consolidate with do_refresh() and au_reval_for_attr() */
1facf9fc 8961+static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
8962+{
8963+ int err;
8964+ struct dentry *parent;
1facf9fc 8965+
027c5e7a 8966+ if (!au_digen_test(dentry, sigen))
1facf9fc 8967+ return 0;
8968+
8969+ parent = dget_parent(dentry);
8970+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 8971+ AuDebugOn(au_digen_test(parent, sigen));
1facf9fc 8972+ au_dbg_verify_gen(parent, sigen);
027c5e7a 8973+ err = au_refresh_dentry(dentry, parent);
1facf9fc 8974+ di_read_unlock(parent, AuLock_IR);
8975+ dput(parent);
027c5e7a 8976+ AuTraceErr(err);
1facf9fc 8977+ return err;
8978+}
8979+
8980+int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
8981+{
8982+ int err;
8983+ struct dentry *d, *parent;
1facf9fc 8984+
027c5e7a 8985+ if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
1facf9fc 8986+ return simple_reval_dpath(dentry, sigen);
8987+
8988+ /* slow loop, keep it simple and stupid */
8989+ /* cf: au_cpup_dirs() */
8990+ err = 0;
8991+ parent = NULL;
027c5e7a 8992+ while (au_digen_test(dentry, sigen)) {
1facf9fc 8993+ d = dentry;
8994+ while (1) {
8995+ dput(parent);
8996+ parent = dget_parent(d);
027c5e7a 8997+ if (!au_digen_test(parent, sigen))
1facf9fc 8998+ break;
8999+ d = parent;
9000+ }
9001+
1facf9fc 9002+ if (d != dentry)
027c5e7a 9003+ di_write_lock_child2(d);
1facf9fc 9004+
9005+ /* someone might update our dentry while we were sleeping */
027c5e7a
AM
9006+ if (au_digen_test(d, sigen)) {
9007+ /*
9008+ * todo: consolidate with simple_reval_dpath(),
9009+ * do_refresh() and au_reval_for_attr().
9010+ */
1facf9fc 9011+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 9012+ err = au_refresh_dentry(d, parent);
1facf9fc 9013+ di_read_unlock(parent, AuLock_IR);
9014+ }
9015+
9016+ if (d != dentry)
9017+ di_write_unlock(d);
9018+ dput(parent);
9019+ if (unlikely(err))
9020+ break;
9021+ }
9022+
9023+ return err;
9024+}
9025+
9026+/*
9027+ * if valid returns 1, otherwise 0.
9028+ */
b4510431 9029+static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
1facf9fc 9030+{
9031+ int valid, err;
9032+ unsigned int sigen;
8b6a4947 9033+ unsigned char do_udba, dirren;
1facf9fc 9034+ struct super_block *sb;
9035+ struct inode *inode;
9036+
027c5e7a 9037+ /* todo: support rcu-walk? */
b4510431 9038+ if (flags & LOOKUP_RCU)
027c5e7a
AM
9039+ return -ECHILD;
9040+
9041+ valid = 0;
9042+ if (unlikely(!au_di(dentry)))
9043+ goto out;
9044+
e49829fe 9045+ valid = 1;
1facf9fc 9046+ sb = dentry->d_sb;
e49829fe
JR
9047+ /*
9048+ * todo: very ugly
9049+ * i_mutex of parent dir may be held,
9050+ * but we should not return 'invalid' due to busy.
9051+ */
9052+ err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
9053+ if (unlikely(err)) {
9054+ valid = err;
027c5e7a 9055+ AuTraceErr(err);
e49829fe
JR
9056+ goto out;
9057+ }
5527c038
JR
9058+ inode = NULL;
9059+ if (d_really_is_positive(dentry))
9060+ inode = d_inode(dentry);
5afbbe0d 9061+ if (unlikely(inode && au_is_bad_inode(inode))) {
c1595e42
JR
9062+ err = -EINVAL;
9063+ AuTraceErr(err);
9064+ goto out_dgrade;
9065+ }
027c5e7a
AM
9066+ if (unlikely(au_dbrange_test(dentry))) {
9067+ err = -EINVAL;
9068+ AuTraceErr(err);
9069+ goto out_dgrade;
1facf9fc 9070+ }
027c5e7a
AM
9071+
9072+ sigen = au_sigen(sb);
9073+ if (au_digen_test(dentry, sigen)) {
1facf9fc 9074+ AuDebugOn(IS_ROOT(dentry));
027c5e7a
AM
9075+ err = au_reval_dpath(dentry, sigen);
9076+ if (unlikely(err)) {
9077+ AuTraceErr(err);
1facf9fc 9078+ goto out_dgrade;
027c5e7a 9079+ }
1facf9fc 9080+ }
9081+ di_downgrade_lock(dentry, AuLock_IR);
9082+
1facf9fc 9083+ err = -EINVAL;
c1595e42 9084+ if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
523b37e3 9085+ && inode
38d290e6 9086+ && !(inode->i_state && I_LINKABLE)
79b8bda9
AM
9087+ && (IS_DEADDIR(inode) || !inode->i_nlink)) {
9088+ AuTraceErr(err);
027c5e7a 9089+ goto out_inval;
79b8bda9 9090+ }
027c5e7a 9091+
1facf9fc 9092+ do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
9093+ if (do_udba && inode) {
5afbbe0d 9094+ aufs_bindex_t btop = au_ibtop(inode);
027c5e7a 9095+ struct inode *h_inode;
1facf9fc 9096+
5afbbe0d
AM
9097+ if (btop >= 0) {
9098+ h_inode = au_h_iptr(inode, btop);
79b8bda9
AM
9099+ if (h_inode && au_test_higen(inode, h_inode)) {
9100+ AuTraceErr(err);
027c5e7a 9101+ goto out_inval;
79b8bda9 9102+ }
027c5e7a 9103+ }
1facf9fc 9104+ }
9105+
8b6a4947
AM
9106+ dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
9107+ err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
5afbbe0d 9108+ if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
1facf9fc 9109+ err = -EIO;
523b37e3
AM
9110+ AuDbg("both of real entry and whiteout found, %p, err %d\n",
9111+ dentry, err);
027c5e7a 9112+ }
e49829fe 9113+ goto out_inval;
1facf9fc 9114+
4f0767ce 9115+out_dgrade:
1facf9fc 9116+ di_downgrade_lock(dentry, AuLock_IR);
e49829fe 9117+out_inval:
1facf9fc 9118+ aufs_read_unlock(dentry, AuLock_IR);
9119+ AuTraceErr(err);
9120+ valid = !err;
e49829fe 9121+out:
027c5e7a 9122+ if (!valid) {
523b37e3 9123+ AuDbg("%pd invalid, %d\n", dentry, valid);
027c5e7a
AM
9124+ d_drop(dentry);
9125+ }
1facf9fc 9126+ return valid;
9127+}
9128+
9129+static void aufs_d_release(struct dentry *dentry)
9130+{
027c5e7a 9131+ if (au_di(dentry)) {
4a4d8108
AM
9132+ au_di_fin(dentry);
9133+ au_hn_di_reinit(dentry);
1facf9fc 9134+ }
1facf9fc 9135+}
9136+
4a4d8108 9137+const struct dentry_operations aufs_dop = {
c06a8ce3
AM
9138+ .d_revalidate = aufs_d_revalidate,
9139+ .d_weak_revalidate = aufs_d_revalidate,
9140+ .d_release = aufs_d_release
1facf9fc 9141+};
79b8bda9
AM
9142+
9143+/* aufs_dop without d_revalidate */
9144+const struct dentry_operations aufs_dop_noreval = {
9145+ .d_release = aufs_d_release
9146+};
7f207e10 9147diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
eca34b5c 9148--- /usr/share/empty/fs/aufs/dentry.h 1970-01-01 01:00:00.000000000 +0100
46016270 9149+++ linux/fs/aufs/dentry.h 2021-06-30 21:35:11.393873211 +0200
0b2a12c6 9150@@ -0,0 +1,269 @@
062440b3 9151+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 9152+/*
d58c55f2 9153+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 9154+ *
9155+ * This program, aufs is free software; you can redistribute it and/or modify
9156+ * it under the terms of the GNU General Public License as published by
9157+ * the Free Software Foundation; either version 2 of the License, or
9158+ * (at your option) any later version.
dece6358
AM
9159+ *
9160+ * This program is distributed in the hope that it will be useful,
9161+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9162+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9163+ * GNU General Public License for more details.
9164+ *
9165+ * You should have received a copy of the GNU General Public License
523b37e3 9166+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 9167+ */
9168+
9169+/*
9170+ * lookup and dentry operations
9171+ */
9172+
9173+#ifndef __AUFS_DENTRY_H__
9174+#define __AUFS_DENTRY_H__
9175+
9176+#ifdef __KERNEL__
9177+
dece6358 9178+#include <linux/dcache.h>
8b6a4947 9179+#include "dirren.h"
1facf9fc 9180+#include "rwsem.h"
9181+
1facf9fc 9182+struct au_hdentry {
9183+ struct dentry *hd_dentry;
027c5e7a 9184+ aufs_bindex_t hd_id;
1facf9fc 9185+};
9186+
9187+struct au_dinfo {
9188+ atomic_t di_generation;
9189+
dece6358 9190+ struct au_rwsem di_rwsem;
5afbbe0d 9191+ aufs_bindex_t di_btop, di_bbot, di_bwh, di_bdiropq;
38d290e6 9192+ unsigned char di_tmpfile; /* to allow the different name */
1c60b727 9193+ struct au_hdentry *di_hdentry;
9f237c51 9194+ struct rcu_head rcu;
4a4d8108 9195+} ____cacheline_aligned_in_smp;
1facf9fc 9196+
9197+/* ---------------------------------------------------------------------- */
9198+
5afbbe0d
AM
9199+/* flags for au_lkup_dentry() */
9200+#define AuLkup_ALLOW_NEG 1
9201+#define AuLkup_IGNORE_PERM (1 << 1)
8b6a4947 9202+#define AuLkup_DIRREN (1 << 2)
5afbbe0d
AM
9203+#define au_ftest_lkup(flags, name) ((flags) & AuLkup_##name)
9204+#define au_fset_lkup(flags, name) \
9205+ do { (flags) |= AuLkup_##name; } while (0)
9206+#define au_fclr_lkup(flags, name) \
9207+ do { (flags) &= ~AuLkup_##name; } while (0)
9208+
8b6a4947
AM
9209+#ifndef CONFIG_AUFS_DIRREN
9210+#undef AuLkup_DIRREN
9211+#define AuLkup_DIRREN 0
9212+#endif
9213+
9214+struct au_do_lookup_args {
9215+ unsigned int flags;
9216+ mode_t type;
9217+ struct qstr whname, *name;
9218+ struct au_dr_lookup dirren;
9219+};
9220+
5afbbe0d
AM
9221+/* ---------------------------------------------------------------------- */
9222+
1facf9fc 9223+/* dentry.c */
79b8bda9 9224+extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
1facf9fc 9225+struct au_branch;
0b2a12c6
JR
9226+struct dentry *au_sio_lkup_one(struct user_namespace *userns, struct qstr *name,
9227+ struct dentry *parent);
1facf9fc 9228+int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
9229+ struct dentry *h_parent, struct au_branch *br);
9230+
5afbbe0d
AM
9231+int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
9232+ unsigned int flags);
86dc4139 9233+int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
027c5e7a 9234+int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
1facf9fc 9235+int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
79b8bda9 9236+void au_refresh_dop(struct dentry *dentry, int force_reval);
1facf9fc 9237+
9238+/* dinfo.c */
4a4d8108 9239+void au_di_init_once(void *_di);
027c5e7a
AM
9240+struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
9241+void au_di_free(struct au_dinfo *dinfo);
9242+void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
9243+void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
4a4d8108
AM
9244+int au_di_init(struct dentry *dentry);
9245+void au_di_fin(struct dentry *dentry);
e2f27e51 9246+int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
1facf9fc 9247+
9248+void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
9249+void di_read_unlock(struct dentry *d, int flags);
9250+void di_downgrade_lock(struct dentry *d, int flags);
9251+void di_write_lock(struct dentry *d, unsigned int lsc);
9252+void di_write_unlock(struct dentry *d);
9253+void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
9254+void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
9255+void di_write_unlock2(struct dentry *d1, struct dentry *d2);
9256+
9257+struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
2cbb1c4b 9258+struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
1facf9fc 9259+aufs_bindex_t au_dbtail(struct dentry *dentry);
9260+aufs_bindex_t au_dbtaildir(struct dentry *dentry);
9261+
9262+void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9263+ struct dentry *h_dentry);
027c5e7a
AM
9264+int au_digen_test(struct dentry *dentry, unsigned int sigen);
9265+int au_dbrange_test(struct dentry *dentry);
1facf9fc 9266+void au_update_digen(struct dentry *dentry);
9267+void au_update_dbrange(struct dentry *dentry, int do_put_zero);
5afbbe0d
AM
9268+void au_update_dbtop(struct dentry *dentry);
9269+void au_update_dbbot(struct dentry *dentry);
1facf9fc 9270+int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
9271+
9272+/* ---------------------------------------------------------------------- */
9273+
9274+static inline struct au_dinfo *au_di(struct dentry *dentry)
9275+{
9276+ return dentry->d_fsdata;
9277+}
9278+
9279+/* ---------------------------------------------------------------------- */
9280+
9281+/* lock subclass for dinfo */
9282+enum {
9283+ AuLsc_DI_CHILD, /* child first */
4a4d8108 9284+ AuLsc_DI_CHILD2, /* rename(2), link(2), and cpup at hnotify */
1facf9fc 9285+ AuLsc_DI_CHILD3, /* copyup dirs */
9286+ AuLsc_DI_PARENT,
9287+ AuLsc_DI_PARENT2,
027c5e7a
AM
9288+ AuLsc_DI_PARENT3,
9289+ AuLsc_DI_TMP /* temp for replacing dinfo */
1facf9fc 9290+};
9291+
9292+/*
9293+ * di_read_lock_child, di_write_lock_child,
9294+ * di_read_lock_child2, di_write_lock_child2,
9295+ * di_read_lock_child3, di_write_lock_child3,
9296+ * di_read_lock_parent, di_write_lock_parent,
9297+ * di_read_lock_parent2, di_write_lock_parent2,
9298+ * di_read_lock_parent3, di_write_lock_parent3,
9299+ */
9300+#define AuReadLockFunc(name, lsc) \
9301+static inline void di_read_lock_##name(struct dentry *d, int flags) \
9302+{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
9303+
9304+#define AuWriteLockFunc(name, lsc) \
9305+static inline void di_write_lock_##name(struct dentry *d) \
9306+{ di_write_lock(d, AuLsc_DI_##lsc); }
9307+
9308+#define AuRWLockFuncs(name, lsc) \
9309+ AuReadLockFunc(name, lsc) \
9310+ AuWriteLockFunc(name, lsc)
9311+
9312+AuRWLockFuncs(child, CHILD);
9313+AuRWLockFuncs(child2, CHILD2);
9314+AuRWLockFuncs(child3, CHILD3);
9315+AuRWLockFuncs(parent, PARENT);
9316+AuRWLockFuncs(parent2, PARENT2);
9317+AuRWLockFuncs(parent3, PARENT3);
9318+
9319+#undef AuReadLockFunc
9320+#undef AuWriteLockFunc
9321+#undef AuRWLockFuncs
9322+
9323+#define DiMustNoWaiters(d) AuRwMustNoWaiters(&au_di(d)->di_rwsem)
dece6358
AM
9324+#define DiMustAnyLock(d) AuRwMustAnyLock(&au_di(d)->di_rwsem)
9325+#define DiMustWriteLock(d) AuRwMustWriteLock(&au_di(d)->di_rwsem)
1facf9fc 9326+
9327+/* ---------------------------------------------------------------------- */
9328+
9329+/* todo: memory barrier? */
9330+static inline unsigned int au_digen(struct dentry *d)
9331+{
9332+ return atomic_read(&au_di(d)->di_generation);
9333+}
9334+
9335+static inline void au_h_dentry_init(struct au_hdentry *hdentry)
9336+{
9337+ hdentry->hd_dentry = NULL;
9338+}
9339+
5afbbe0d
AM
9340+static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
9341+ aufs_bindex_t bindex)
9342+{
9343+ return di->di_hdentry + bindex;
9344+}
9345+
1facf9fc 9346+static inline void au_hdput(struct au_hdentry *hd)
9347+{
4a4d8108
AM
9348+ if (hd)
9349+ dput(hd->hd_dentry);
1facf9fc 9350+}
9351+
5afbbe0d 9352+static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
1facf9fc 9353+{
1308ab2a 9354+ DiMustAnyLock(dentry);
5afbbe0d 9355+ return au_di(dentry)->di_btop;
1facf9fc 9356+}
9357+
5afbbe0d 9358+static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
1facf9fc 9359+{
1308ab2a 9360+ DiMustAnyLock(dentry);
5afbbe0d 9361+ return au_di(dentry)->di_bbot;
1facf9fc 9362+}
9363+
9364+static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
9365+{
1308ab2a 9366+ DiMustAnyLock(dentry);
1facf9fc 9367+ return au_di(dentry)->di_bwh;
9368+}
9369+
9370+static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
9371+{
1308ab2a 9372+ DiMustAnyLock(dentry);
1facf9fc 9373+ return au_di(dentry)->di_bdiropq;
9374+}
9375+
9376+/* todo: hard/soft set? */
5afbbe0d 9377+static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
1facf9fc 9378+{
1308ab2a 9379+ DiMustWriteLock(dentry);
5afbbe0d 9380+ au_di(dentry)->di_btop = bindex;
1facf9fc 9381+}
9382+
5afbbe0d 9383+static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
1facf9fc 9384+{
1308ab2a 9385+ DiMustWriteLock(dentry);
5afbbe0d 9386+ au_di(dentry)->di_bbot = bindex;
1facf9fc 9387+}
9388+
9389+static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
9390+{
1308ab2a 9391+ DiMustWriteLock(dentry);
5afbbe0d 9392+ /* dbwh can be outside of btop - bbot range */
1facf9fc 9393+ au_di(dentry)->di_bwh = bindex;
9394+}
9395+
9396+static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
9397+{
1308ab2a 9398+ DiMustWriteLock(dentry);
1facf9fc 9399+ au_di(dentry)->di_bdiropq = bindex;
9400+}
9401+
9402+/* ---------------------------------------------------------------------- */
9403+
4a4d8108 9404+#ifdef CONFIG_AUFS_HNOTIFY
1facf9fc 9405+static inline void au_digen_dec(struct dentry *d)
9406+{
e49829fe 9407+ atomic_dec(&au_di(d)->di_generation);
1facf9fc 9408+}
9409+
4a4d8108 9410+static inline void au_hn_di_reinit(struct dentry *dentry)
1facf9fc 9411+{
9412+ dentry->d_fsdata = NULL;
9413+}
9414+#else
4a4d8108
AM
9415+AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
9416+#endif /* CONFIG_AUFS_HNOTIFY */
1facf9fc 9417+
9418+#endif /* __KERNEL__ */
9419+#endif /* __AUFS_DENTRY_H__ */
7f207e10 9420diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
eca34b5c 9421--- /usr/share/empty/fs/aufs/dinfo.c 1970-01-01 01:00:00.000000000 +0100
46016270 9422+++ linux/fs/aufs/dinfo.c 2021-02-24 13:33:42.741013619 +0100
062440b3 9423@@ -0,0 +1,554 @@
cd7a4cd9 9424+// SPDX-License-Identifier: GPL-2.0
1facf9fc 9425+/*
d58c55f2 9426+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 9427+ *
9428+ * This program, aufs is free software; you can redistribute it and/or modify
9429+ * it under the terms of the GNU General Public License as published by
9430+ * the Free Software Foundation; either version 2 of the License, or
9431+ * (at your option) any later version.
dece6358
AM
9432+ *
9433+ * This program is distributed in the hope that it will be useful,
9434+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9435+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9436+ * GNU General Public License for more details.
9437+ *
9438+ * You should have received a copy of the GNU General Public License
523b37e3 9439+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 9440+ */
9441+
9442+/*
9443+ * dentry private data
9444+ */
9445+
9446+#include "aufs.h"
9447+
e49829fe 9448+void au_di_init_once(void *_dinfo)
4a4d8108 9449+{
e49829fe 9450+ struct au_dinfo *dinfo = _dinfo;
4a4d8108 9451+
e49829fe 9452+ au_rw_init(&dinfo->di_rwsem);
4a4d8108
AM
9453+}
9454+
027c5e7a 9455+struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
1facf9fc 9456+{
9457+ struct au_dinfo *dinfo;
027c5e7a 9458+ int nbr, i;
1facf9fc 9459+
9460+ dinfo = au_cache_alloc_dinfo();
9461+ if (unlikely(!dinfo))
9462+ goto out;
9463+
5afbbe0d 9464+ nbr = au_sbbot(sb) + 1;
1facf9fc 9465+ if (nbr <= 0)
9466+ nbr = 1;
9467+ dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
027c5e7a
AM
9468+ if (dinfo->di_hdentry) {
9469+ au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
5afbbe0d
AM
9470+ dinfo->di_btop = -1;
9471+ dinfo->di_bbot = -1;
027c5e7a
AM
9472+ dinfo->di_bwh = -1;
9473+ dinfo->di_bdiropq = -1;
38d290e6 9474+ dinfo->di_tmpfile = 0;
027c5e7a
AM
9475+ for (i = 0; i < nbr; i++)
9476+ dinfo->di_hdentry[i].hd_id = -1;
9477+ goto out;
9478+ }
1facf9fc 9479+
1c60b727 9480+ au_cache_free_dinfo(dinfo);
027c5e7a
AM
9481+ dinfo = NULL;
9482+
4f0767ce 9483+out:
027c5e7a 9484+ return dinfo;
1facf9fc 9485+}
9486+
027c5e7a 9487+void au_di_free(struct au_dinfo *dinfo)
4a4d8108 9488+{
4a4d8108 9489+ struct au_hdentry *p;
5afbbe0d 9490+ aufs_bindex_t bbot, bindex;
4a4d8108
AM
9491+
9492+ /* dentry may not be revalidated */
5afbbe0d 9493+ bindex = dinfo->di_btop;
4a4d8108 9494+ if (bindex >= 0) {
5afbbe0d
AM
9495+ bbot = dinfo->di_bbot;
9496+ p = au_hdentry(dinfo, bindex);
9497+ while (bindex++ <= bbot)
4a4d8108
AM
9498+ au_hdput(p++);
9499+ }
9f237c51 9500+ au_kfree_try_rcu(dinfo->di_hdentry);
1c60b727 9501+ au_cache_free_dinfo(dinfo);
027c5e7a
AM
9502+}
9503+
9504+void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9505+{
9506+ struct au_hdentry *p;
9507+ aufs_bindex_t bi;
9508+
9509+ AuRwMustWriteLock(&a->di_rwsem);
9510+ AuRwMustWriteLock(&b->di_rwsem);
9511+
9512+#define DiSwap(v, name) \
9513+ do { \
9514+ v = a->di_##name; \
9515+ a->di_##name = b->di_##name; \
9516+ b->di_##name = v; \
9517+ } while (0)
9518+
9519+ DiSwap(p, hdentry);
5afbbe0d
AM
9520+ DiSwap(bi, btop);
9521+ DiSwap(bi, bbot);
027c5e7a
AM
9522+ DiSwap(bi, bwh);
9523+ DiSwap(bi, bdiropq);
9524+ /* smp_mb(); */
9525+
9526+#undef DiSwap
9527+}
9528+
9529+void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9530+{
9531+ AuRwMustWriteLock(&dst->di_rwsem);
9532+ AuRwMustWriteLock(&src->di_rwsem);
9533+
5afbbe0d
AM
9534+ dst->di_btop = src->di_btop;
9535+ dst->di_bbot = src->di_bbot;
027c5e7a
AM
9536+ dst->di_bwh = src->di_bwh;
9537+ dst->di_bdiropq = src->di_bdiropq;
9538+ /* smp_mb(); */
9539+}
9540+
9541+int au_di_init(struct dentry *dentry)
9542+{
9543+ int err;
9544+ struct super_block *sb;
9545+ struct au_dinfo *dinfo;
9546+
9547+ err = 0;
9548+ sb = dentry->d_sb;
9549+ dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9550+ if (dinfo) {
9551+ atomic_set(&dinfo->di_generation, au_sigen(sb));
9552+ /* smp_mb(); */ /* atomic_set */
9553+ dentry->d_fsdata = dinfo;
9554+ } else
9555+ err = -ENOMEM;
9556+
9557+ return err;
9558+}
9559+
9560+void au_di_fin(struct dentry *dentry)
9561+{
9562+ struct au_dinfo *dinfo;
9563+
9564+ dinfo = au_di(dentry);
9565+ AuRwDestroy(&dinfo->di_rwsem);
9566+ au_di_free(dinfo);
4a4d8108
AM
9567+}
9568+
e2f27e51 9569+int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
1facf9fc 9570+{
9571+ int err, sz;
9572+ struct au_hdentry *hdp;
9573+
1308ab2a 9574+ AuRwMustWriteLock(&dinfo->di_rwsem);
9575+
1facf9fc 9576+ err = -ENOMEM;
5afbbe0d 9577+ sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
1facf9fc 9578+ if (!sz)
9579+ sz = sizeof(*hdp);
e2f27e51
AM
9580+ hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
9581+ may_shrink);
1facf9fc 9582+ if (hdp) {
9583+ dinfo->di_hdentry = hdp;
9584+ err = 0;
9585+ }
9586+
9587+ return err;
9588+}
9589+
9590+/* ---------------------------------------------------------------------- */
9591+
9592+static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9593+{
9594+ switch (lsc) {
9595+ case AuLsc_DI_CHILD:
9596+ ii_write_lock_child(inode);
9597+ break;
9598+ case AuLsc_DI_CHILD2:
9599+ ii_write_lock_child2(inode);
9600+ break;
9601+ case AuLsc_DI_CHILD3:
9602+ ii_write_lock_child3(inode);
9603+ break;
9604+ case AuLsc_DI_PARENT:
9605+ ii_write_lock_parent(inode);
9606+ break;
9607+ case AuLsc_DI_PARENT2:
9608+ ii_write_lock_parent2(inode);
9609+ break;
9610+ case AuLsc_DI_PARENT3:
9611+ ii_write_lock_parent3(inode);
9612+ break;
9613+ default:
9614+ BUG();
9615+ }
9616+}
9617+
9618+static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9619+{
9620+ switch (lsc) {
9621+ case AuLsc_DI_CHILD:
9622+ ii_read_lock_child(inode);
9623+ break;
9624+ case AuLsc_DI_CHILD2:
9625+ ii_read_lock_child2(inode);
9626+ break;
9627+ case AuLsc_DI_CHILD3:
9628+ ii_read_lock_child3(inode);
9629+ break;
9630+ case AuLsc_DI_PARENT:
9631+ ii_read_lock_parent(inode);
9632+ break;
9633+ case AuLsc_DI_PARENT2:
9634+ ii_read_lock_parent2(inode);
9635+ break;
9636+ case AuLsc_DI_PARENT3:
9637+ ii_read_lock_parent3(inode);
9638+ break;
9639+ default:
9640+ BUG();
9641+ }
9642+}
9643+
9644+void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9645+{
5527c038
JR
9646+ struct inode *inode;
9647+
dece6358 9648+ au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
5527c038
JR
9649+ if (d_really_is_positive(d)) {
9650+ inode = d_inode(d);
1facf9fc 9651+ if (au_ftest_lock(flags, IW))
5527c038 9652+ do_ii_write_lock(inode, lsc);
1facf9fc 9653+ else if (au_ftest_lock(flags, IR))
5527c038 9654+ do_ii_read_lock(inode, lsc);
1facf9fc 9655+ }
9656+}
9657+
9658+void di_read_unlock(struct dentry *d, int flags)
9659+{
5527c038
JR
9660+ struct inode *inode;
9661+
9662+ if (d_really_is_positive(d)) {
9663+ inode = d_inode(d);
027c5e7a
AM
9664+ if (au_ftest_lock(flags, IW)) {
9665+ au_dbg_verify_dinode(d);
5527c038 9666+ ii_write_unlock(inode);
027c5e7a
AM
9667+ } else if (au_ftest_lock(flags, IR)) {
9668+ au_dbg_verify_dinode(d);
5527c038 9669+ ii_read_unlock(inode);
027c5e7a 9670+ }
1facf9fc 9671+ }
dece6358 9672+ au_rw_read_unlock(&au_di(d)->di_rwsem);
1facf9fc 9673+}
9674+
9675+void di_downgrade_lock(struct dentry *d, int flags)
9676+{
5527c038
JR
9677+ if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9678+ ii_downgrade_lock(d_inode(d));
dece6358 9679+ au_rw_dgrade_lock(&au_di(d)->di_rwsem);
1facf9fc 9680+}
9681+
9682+void di_write_lock(struct dentry *d, unsigned int lsc)
9683+{
dece6358 9684+ au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
5527c038
JR
9685+ if (d_really_is_positive(d))
9686+ do_ii_write_lock(d_inode(d), lsc);
1facf9fc 9687+}
9688+
9689+void di_write_unlock(struct dentry *d)
9690+{
027c5e7a 9691+ au_dbg_verify_dinode(d);
5527c038
JR
9692+ if (d_really_is_positive(d))
9693+ ii_write_unlock(d_inode(d));
dece6358 9694+ au_rw_write_unlock(&au_di(d)->di_rwsem);
1facf9fc 9695+}
9696+
9697+void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9698+{
9699+ AuDebugOn(d1 == d2
5527c038 9700+ || d_inode(d1) == d_inode(d2)
1facf9fc 9701+ || d1->d_sb != d2->d_sb);
9702+
521ced18
JR
9703+ if ((isdir && au_test_subdir(d1, d2))
9704+ || d1 < d2) {
1facf9fc 9705+ di_write_lock_child(d1);
9706+ di_write_lock_child2(d2);
9707+ } else {
1facf9fc 9708+ di_write_lock_child(d2);
9709+ di_write_lock_child2(d1);
9710+ }
9711+}
9712+
9713+void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9714+{
9715+ AuDebugOn(d1 == d2
5527c038 9716+ || d_inode(d1) == d_inode(d2)
1facf9fc 9717+ || d1->d_sb != d2->d_sb);
9718+
521ced18
JR
9719+ if ((isdir && au_test_subdir(d1, d2))
9720+ || d1 < d2) {
1facf9fc 9721+ di_write_lock_parent(d1);
9722+ di_write_lock_parent2(d2);
9723+ } else {
1facf9fc 9724+ di_write_lock_parent(d2);
9725+ di_write_lock_parent2(d1);
9726+ }
9727+}
9728+
9729+void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9730+{
9731+ di_write_unlock(d1);
5527c038 9732+ if (d_inode(d1) == d_inode(d2))
dece6358 9733+ au_rw_write_unlock(&au_di(d2)->di_rwsem);
1facf9fc 9734+ else
9735+ di_write_unlock(d2);
9736+}
9737+
9738+/* ---------------------------------------------------------------------- */
9739+
9740+struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9741+{
9742+ struct dentry *d;
9743+
1308ab2a 9744+ DiMustAnyLock(dentry);
9745+
5afbbe0d 9746+ if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
1facf9fc 9747+ return NULL;
9748+ AuDebugOn(bindex < 0);
5afbbe0d 9749+ d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
c1595e42 9750+ AuDebugOn(d && au_dcount(d) <= 0);
1facf9fc 9751+ return d;
9752+}
9753+
2cbb1c4b
JR
9754+/*
9755+ * extended version of au_h_dptr().
38d290e6
JR
9756+ * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9757+ * error.
2cbb1c4b
JR
9758+ */
9759+struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9760+{
9761+ struct dentry *h_dentry;
9762+ struct inode *inode, *h_inode;
9763+
5527c038 9764+ AuDebugOn(d_really_is_negative(dentry));
2cbb1c4b
JR
9765+
9766+ h_dentry = NULL;
5afbbe0d
AM
9767+ if (au_dbtop(dentry) <= bindex
9768+ && bindex <= au_dbbot(dentry))
2cbb1c4b 9769+ h_dentry = au_h_dptr(dentry, bindex);
38d290e6 9770+ if (h_dentry && !au_d_linkable(h_dentry)) {
2cbb1c4b
JR
9771+ dget(h_dentry);
9772+ goto out; /* success */
9773+ }
9774+
5527c038 9775+ inode = d_inode(dentry);
5afbbe0d
AM
9776+ AuDebugOn(bindex < au_ibtop(inode));
9777+ AuDebugOn(au_ibbot(inode) < bindex);
2cbb1c4b
JR
9778+ h_inode = au_h_iptr(inode, bindex);
9779+ h_dentry = d_find_alias(h_inode);
9780+ if (h_dentry) {
9781+ if (!IS_ERR(h_dentry)) {
38d290e6 9782+ if (!au_d_linkable(h_dentry))
2cbb1c4b
JR
9783+ goto out; /* success */
9784+ dput(h_dentry);
9785+ } else
9786+ goto out;
9787+ }
9788+
9789+ if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9790+ h_dentry = au_plink_lkup(inode, bindex);
9791+ AuDebugOn(!h_dentry);
9792+ if (!IS_ERR(h_dentry)) {
9793+ if (!au_d_hashed_positive(h_dentry))
9794+ goto out; /* success */
9795+ dput(h_dentry);
9796+ h_dentry = NULL;
9797+ }
9798+ }
9799+
9800+out:
9801+ AuDbgDentry(h_dentry);
9802+ return h_dentry;
9803+}
9804+
1facf9fc 9805+aufs_bindex_t au_dbtail(struct dentry *dentry)
9806+{
5afbbe0d 9807+ aufs_bindex_t bbot, bwh;
1facf9fc 9808+
5afbbe0d
AM
9809+ bbot = au_dbbot(dentry);
9810+ if (0 <= bbot) {
1facf9fc 9811+ bwh = au_dbwh(dentry);
9812+ if (!bwh)
9813+ return bwh;
5afbbe0d 9814+ if (0 < bwh && bwh < bbot)
1facf9fc 9815+ return bwh - 1;
9816+ }
5afbbe0d 9817+ return bbot;
1facf9fc 9818+}
9819+
9820+aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9821+{
5afbbe0d 9822+ aufs_bindex_t bbot, bopq;
1facf9fc 9823+
5afbbe0d
AM
9824+ bbot = au_dbtail(dentry);
9825+ if (0 <= bbot) {
1facf9fc 9826+ bopq = au_dbdiropq(dentry);
5afbbe0d
AM
9827+ if (0 <= bopq && bopq < bbot)
9828+ bbot = bopq;
1facf9fc 9829+ }
5afbbe0d 9830+ return bbot;
1facf9fc 9831+}
9832+
9833+/* ---------------------------------------------------------------------- */
9834+
9835+void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9836+ struct dentry *h_dentry)
9837+{
5afbbe0d
AM
9838+ struct au_dinfo *dinfo;
9839+ struct au_hdentry *hd;
027c5e7a 9840+ struct au_branch *br;
1facf9fc 9841+
1308ab2a 9842+ DiMustWriteLock(dentry);
9843+
5afbbe0d
AM
9844+ dinfo = au_di(dentry);
9845+ hd = au_hdentry(dinfo, bindex);
4a4d8108 9846+ au_hdput(hd);
1facf9fc 9847+ hd->hd_dentry = h_dentry;
027c5e7a
AM
9848+ if (h_dentry) {
9849+ br = au_sbr(dentry->d_sb, bindex);
9850+ hd->hd_id = br->br_id;
9851+ }
9852+}
9853+
9854+int au_dbrange_test(struct dentry *dentry)
9855+{
9856+ int err;
5afbbe0d 9857+ aufs_bindex_t btop, bbot;
027c5e7a
AM
9858+
9859+ err = 0;
5afbbe0d
AM
9860+ btop = au_dbtop(dentry);
9861+ bbot = au_dbbot(dentry);
9862+ if (btop >= 0)
9863+ AuDebugOn(bbot < 0 && btop > bbot);
027c5e7a
AM
9864+ else {
9865+ err = -EIO;
5afbbe0d 9866+ AuDebugOn(bbot >= 0);
027c5e7a
AM
9867+ }
9868+
9869+ return err;
9870+}
9871+
9872+int au_digen_test(struct dentry *dentry, unsigned int sigen)
9873+{
9874+ int err;
9875+
9876+ err = 0;
9877+ if (unlikely(au_digen(dentry) != sigen
5527c038 9878+ || au_iigen_test(d_inode(dentry), sigen)))
027c5e7a
AM
9879+ err = -EIO;
9880+
9881+ return err;
1facf9fc 9882+}
9883+
9884+void au_update_digen(struct dentry *dentry)
9885+{
9886+ atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
9887+ /* smp_mb(); */ /* atomic_set */
9888+}
9889+
9890+void au_update_dbrange(struct dentry *dentry, int do_put_zero)
9891+{
9892+ struct au_dinfo *dinfo;
9893+ struct dentry *h_d;
4a4d8108 9894+ struct au_hdentry *hdp;
5afbbe0d 9895+ aufs_bindex_t bindex, bbot;
1facf9fc 9896+
1308ab2a 9897+ DiMustWriteLock(dentry);
9898+
1facf9fc 9899+ dinfo = au_di(dentry);
5afbbe0d 9900+ if (!dinfo || dinfo->di_btop < 0)
1facf9fc 9901+ return;
9902+
9903+ if (do_put_zero) {
5afbbe0d
AM
9904+ bbot = dinfo->di_bbot;
9905+ bindex = dinfo->di_btop;
9906+ hdp = au_hdentry(dinfo, bindex);
9907+ for (; bindex <= bbot; bindex++, hdp++) {
9908+ h_d = hdp->hd_dentry;
5527c038 9909+ if (h_d && d_is_negative(h_d))
1facf9fc 9910+ au_set_h_dptr(dentry, bindex, NULL);
9911+ }
9912+ }
9913+
5afbbe0d
AM
9914+ dinfo->di_btop = 0;
9915+ hdp = au_hdentry(dinfo, dinfo->di_btop);
9916+ for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
9917+ if (hdp->hd_dentry)
1facf9fc 9918+ break;
5afbbe0d
AM
9919+ if (dinfo->di_btop > dinfo->di_bbot) {
9920+ dinfo->di_btop = -1;
9921+ dinfo->di_bbot = -1;
1facf9fc 9922+ return;
9923+ }
9924+
5afbbe0d
AM
9925+ hdp = au_hdentry(dinfo, dinfo->di_bbot);
9926+ for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
9927+ if (hdp->hd_dentry)
1facf9fc 9928+ break;
5afbbe0d 9929+ AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
1facf9fc 9930+}
9931+
5afbbe0d 9932+void au_update_dbtop(struct dentry *dentry)
1facf9fc 9933+{
5afbbe0d 9934+ aufs_bindex_t bindex, bbot;
1facf9fc 9935+ struct dentry *h_dentry;
9936+
5afbbe0d
AM
9937+ bbot = au_dbbot(dentry);
9938+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
1facf9fc 9939+ h_dentry = au_h_dptr(dentry, bindex);
9940+ if (!h_dentry)
9941+ continue;
5527c038 9942+ if (d_is_positive(h_dentry)) {
5afbbe0d 9943+ au_set_dbtop(dentry, bindex);
1facf9fc 9944+ return;
9945+ }
9946+ au_set_h_dptr(dentry, bindex, NULL);
9947+ }
9948+}
9949+
5afbbe0d 9950+void au_update_dbbot(struct dentry *dentry)
1facf9fc 9951+{
5afbbe0d 9952+ aufs_bindex_t bindex, btop;
1facf9fc 9953+ struct dentry *h_dentry;
9954+
5afbbe0d
AM
9955+ btop = au_dbtop(dentry);
9956+ for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
1facf9fc 9957+ h_dentry = au_h_dptr(dentry, bindex);
9958+ if (!h_dentry)
9959+ continue;
5527c038 9960+ if (d_is_positive(h_dentry)) {
5afbbe0d 9961+ au_set_dbbot(dentry, bindex);
1facf9fc 9962+ return;
9963+ }
9964+ au_set_h_dptr(dentry, bindex, NULL);
9965+ }
9966+}
9967+
9968+int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
9969+{
5afbbe0d 9970+ aufs_bindex_t bindex, bbot;
1facf9fc 9971+
5afbbe0d
AM
9972+ bbot = au_dbbot(dentry);
9973+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
1facf9fc 9974+ if (au_h_dptr(dentry, bindex) == h_dentry)
9975+ return bindex;
9976+ return -1;
9977+}
7f207e10 9978diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
eca34b5c 9979--- /usr/share/empty/fs/aufs/dir.c 1970-01-01 01:00:00.000000000 +0100
46016270 9980+++ linux/fs/aufs/dir.c 2021-06-30 21:35:11.393873211 +0200
0b2a12c6 9981@@ -0,0 +1,765 @@
cd7a4cd9 9982+// SPDX-License-Identifier: GPL-2.0
1facf9fc 9983+/*
d58c55f2 9984+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 9985+ *
9986+ * This program, aufs is free software; you can redistribute it and/or modify
9987+ * it under the terms of the GNU General Public License as published by
9988+ * the Free Software Foundation; either version 2 of the License, or
9989+ * (at your option) any later version.
dece6358
AM
9990+ *
9991+ * This program is distributed in the hope that it will be useful,
9992+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9993+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9994+ * GNU General Public License for more details.
9995+ *
9996+ * You should have received a copy of the GNU General Public License
523b37e3 9997+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 9998+ */
9999+
10000+/*
10001+ * directory operations
10002+ */
10003+
10004+#include <linux/fs_stack.h>
eca801bf 10005+#include <linux/iversion.h>
1facf9fc 10006+#include "aufs.h"
10007+
10008+void au_add_nlink(struct inode *dir, struct inode *h_dir)
10009+{
9dbd164d
AM
10010+ unsigned int nlink;
10011+
1facf9fc 10012+ AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10013+
9dbd164d
AM
10014+ nlink = dir->i_nlink;
10015+ nlink += h_dir->i_nlink - 2;
1facf9fc 10016+ if (h_dir->i_nlink < 2)
9dbd164d 10017+ nlink += 2;
f6b6e03d 10018+ smp_mb(); /* for i_nlink */
7eafdf33 10019+ /* 0 can happen in revaliding */
92d182d2 10020+ set_nlink(dir, nlink);
1facf9fc 10021+}
10022+
10023+void au_sub_nlink(struct inode *dir, struct inode *h_dir)
10024+{
9dbd164d
AM
10025+ unsigned int nlink;
10026+
1facf9fc 10027+ AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10028+
9dbd164d
AM
10029+ nlink = dir->i_nlink;
10030+ nlink -= h_dir->i_nlink - 2;
1facf9fc 10031+ if (h_dir->i_nlink < 2)
9dbd164d 10032+ nlink -= 2;
f6b6e03d 10033+ smp_mb(); /* for i_nlink */
92d182d2 10034+ /* nlink == 0 means the branch-fs is broken */
9dbd164d 10035+ set_nlink(dir, nlink);
1facf9fc 10036+}
10037+
1308ab2a 10038+loff_t au_dir_size(struct file *file, struct dentry *dentry)
10039+{
10040+ loff_t sz;
5afbbe0d 10041+ aufs_bindex_t bindex, bbot;
1308ab2a 10042+ struct file *h_file;
10043+ struct dentry *h_dentry;
10044+
10045+ sz = 0;
10046+ if (file) {
2000de60 10047+ AuDebugOn(!d_is_dir(file->f_path.dentry));
1308ab2a 10048+
5afbbe0d
AM
10049+ bbot = au_fbbot_dir(file);
10050+ for (bindex = au_fbtop(file);
10051+ bindex <= bbot && sz < KMALLOC_MAX_SIZE;
1308ab2a 10052+ bindex++) {
4a4d8108 10053+ h_file = au_hf_dir(file, bindex);
c06a8ce3
AM
10054+ if (h_file && file_inode(h_file))
10055+ sz += vfsub_f_size_read(h_file);
1308ab2a 10056+ }
10057+ } else {
10058+ AuDebugOn(!dentry);
2000de60 10059+ AuDebugOn(!d_is_dir(dentry));
1308ab2a 10060+
5afbbe0d
AM
10061+ bbot = au_dbtaildir(dentry);
10062+ for (bindex = au_dbtop(dentry);
10063+ bindex <= bbot && sz < KMALLOC_MAX_SIZE;
1308ab2a 10064+ bindex++) {
10065+ h_dentry = au_h_dptr(dentry, bindex);
5527c038
JR
10066+ if (h_dentry && d_is_positive(h_dentry))
10067+ sz += i_size_read(d_inode(h_dentry));
1308ab2a 10068+ }
10069+ }
10070+ if (sz < KMALLOC_MAX_SIZE)
10071+ sz = roundup_pow_of_two(sz);
10072+ if (sz > KMALLOC_MAX_SIZE)
10073+ sz = KMALLOC_MAX_SIZE;
10074+ else if (sz < NAME_MAX) {
10075+ BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
10076+ sz = AUFS_RDBLK_DEF;
10077+ }
10078+ return sz;
10079+}
10080+
b912730e
AM
10081+struct au_dir_ts_arg {
10082+ struct dentry *dentry;
10083+ aufs_bindex_t brid;
10084+};
10085+
10086+static void au_do_dir_ts(void *arg)
10087+{
10088+ struct au_dir_ts_arg *a = arg;
10089+ struct au_dtime dt;
10090+ struct path h_path;
10091+ struct inode *dir, *h_dir;
10092+ struct super_block *sb;
10093+ struct au_branch *br;
10094+ struct au_hinode *hdir;
10095+ int err;
5afbbe0d 10096+ aufs_bindex_t btop, bindex;
b912730e
AM
10097+
10098+ sb = a->dentry->d_sb;
5527c038 10099+ if (d_really_is_negative(a->dentry))
b912730e 10100+ goto out;
5527c038 10101+ /* no dir->i_mutex lock */
b95c5147
AM
10102+ aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
10103+
5527c038 10104+ dir = d_inode(a->dentry);
5afbbe0d 10105+ btop = au_ibtop(dir);
b912730e 10106+ bindex = au_br_index(sb, a->brid);
5afbbe0d 10107+ if (bindex < btop)
b912730e
AM
10108+ goto out_unlock;
10109+
10110+ br = au_sbr(sb, bindex);
10111+ h_path.dentry = au_h_dptr(a->dentry, bindex);
10112+ if (!h_path.dentry)
10113+ goto out_unlock;
10114+ h_path.mnt = au_br_mnt(br);
10115+ au_dtime_store(&dt, a->dentry, &h_path);
10116+
5afbbe0d 10117+ br = au_sbr(sb, btop);
b912730e
AM
10118+ if (!au_br_writable(br->br_perm))
10119+ goto out_unlock;
5afbbe0d 10120+ h_path.dentry = au_h_dptr(a->dentry, btop);
b912730e
AM
10121+ h_path.mnt = au_br_mnt(br);
10122+ err = vfsub_mnt_want_write(h_path.mnt);
10123+ if (err)
10124+ goto out_unlock;
5afbbe0d
AM
10125+ hdir = au_hi(dir, btop);
10126+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
10127+ h_dir = au_h_iptr(dir, btop);
b912730e 10128+ if (h_dir->i_nlink
cd7a4cd9 10129+ && timespec64_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
b912730e
AM
10130+ dt.dt_h_path = h_path;
10131+ au_dtime_revert(&dt);
10132+ }
5afbbe0d 10133+ au_hn_inode_unlock(hdir);
b912730e
AM
10134+ vfsub_mnt_drop_write(h_path.mnt);
10135+ au_cpup_attr_timesizes(dir);
10136+
10137+out_unlock:
10138+ aufs_read_unlock(a->dentry, AuLock_DW);
10139+out:
10140+ dput(a->dentry);
10141+ au_nwt_done(&au_sbi(sb)->si_nowait);
9f237c51 10142+ au_kfree_try_rcu(arg);
b912730e
AM
10143+}
10144+
10145+void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
10146+{
10147+ int perm, wkq_err;
5afbbe0d 10148+ aufs_bindex_t btop;
b912730e
AM
10149+ struct au_dir_ts_arg *arg;
10150+ struct dentry *dentry;
10151+ struct super_block *sb;
10152+
10153+ IMustLock(dir);
10154+
10155+ dentry = d_find_any_alias(dir);
10156+ AuDebugOn(!dentry);
10157+ sb = dentry->d_sb;
5afbbe0d
AM
10158+ btop = au_ibtop(dir);
10159+ if (btop == bindex) {
b912730e
AM
10160+ au_cpup_attr_timesizes(dir);
10161+ goto out;
10162+ }
10163+
5afbbe0d 10164+ perm = au_sbr_perm(sb, btop);
b912730e
AM
10165+ if (!au_br_writable(perm))
10166+ goto out;
10167+
10168+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
10169+ if (!arg)
10170+ goto out;
10171+
10172+ arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
10173+ arg->brid = au_sbr_id(sb, bindex);
10174+ wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
10175+ if (unlikely(wkq_err)) {
10176+ pr_err("wkq %d\n", wkq_err);
10177+ dput(dentry);
9f237c51 10178+ au_kfree_try_rcu(arg);
b912730e
AM
10179+ }
10180+
10181+out:
10182+ dput(dentry);
10183+}
10184+
1facf9fc 10185+/* ---------------------------------------------------------------------- */
10186+
10187+static int reopen_dir(struct file *file)
10188+{
10189+ int err;
10190+ unsigned int flags;
5afbbe0d 10191+ aufs_bindex_t bindex, btail, btop;
1facf9fc 10192+ struct dentry *dentry, *h_dentry;
10193+ struct file *h_file;
10194+
10195+ /* open all lower dirs */
2000de60 10196+ dentry = file->f_path.dentry;
5afbbe0d
AM
10197+ btop = au_dbtop(dentry);
10198+ for (bindex = au_fbtop(file); bindex < btop; bindex++)
1facf9fc 10199+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d 10200+ au_set_fbtop(file, btop);
1facf9fc 10201+
10202+ btail = au_dbtaildir(dentry);
5afbbe0d 10203+ for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
1facf9fc 10204+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d 10205+ au_set_fbbot_dir(file, btail);
1facf9fc 10206+
4a4d8108 10207+ flags = vfsub_file_flags(file);
5afbbe0d 10208+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 10209+ h_dentry = au_h_dptr(dentry, bindex);
10210+ if (!h_dentry)
10211+ continue;
4a4d8108 10212+ h_file = au_hf_dir(file, bindex);
1facf9fc 10213+ if (h_file)
10214+ continue;
10215+
392086de 10216+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
1facf9fc 10217+ err = PTR_ERR(h_file);
10218+ if (IS_ERR(h_file))
10219+ goto out; /* close all? */
10220+ au_set_h_fptr(file, bindex, h_file);
10221+ }
10222+ au_update_figen(file);
10223+ /* todo: necessary? */
10224+ /* file->f_ra = h_file->f_ra; */
10225+ err = 0;
10226+
4f0767ce 10227+out:
1facf9fc 10228+ return err;
10229+}
10230+
b912730e 10231+static int do_open_dir(struct file *file, int flags, struct file *h_file)
1facf9fc 10232+{
10233+ int err;
10234+ aufs_bindex_t bindex, btail;
10235+ struct dentry *dentry, *h_dentry;
8cdd5066 10236+ struct vfsmount *mnt;
1facf9fc 10237+
1308ab2a 10238+ FiMustWriteLock(file);
b912730e 10239+ AuDebugOn(h_file);
1308ab2a 10240+
523b37e3 10241+ err = 0;
8cdd5066 10242+ mnt = file->f_path.mnt;
2000de60 10243+ dentry = file->f_path.dentry;
be118d29 10244+ file->f_version = inode_query_iversion(d_inode(dentry));
5afbbe0d
AM
10245+ bindex = au_dbtop(dentry);
10246+ au_set_fbtop(file, bindex);
1facf9fc 10247+ btail = au_dbtaildir(dentry);
5afbbe0d 10248+ au_set_fbbot_dir(file, btail);
1facf9fc 10249+ for (; !err && bindex <= btail; bindex++) {
10250+ h_dentry = au_h_dptr(dentry, bindex);
10251+ if (!h_dentry)
10252+ continue;
10253+
8cdd5066
JR
10254+ err = vfsub_test_mntns(mnt, h_dentry->d_sb);
10255+ if (unlikely(err))
10256+ break;
392086de 10257+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
1facf9fc 10258+ if (IS_ERR(h_file)) {
10259+ err = PTR_ERR(h_file);
10260+ break;
10261+ }
10262+ au_set_h_fptr(file, bindex, h_file);
10263+ }
10264+ au_update_figen(file);
10265+ /* todo: necessary? */
10266+ /* file->f_ra = h_file->f_ra; */
10267+ if (!err)
10268+ return 0; /* success */
10269+
10270+ /* close all */
5afbbe0d 10271+ for (bindex = au_fbtop(file); bindex <= btail; bindex++)
1facf9fc 10272+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d
AM
10273+ au_set_fbtop(file, -1);
10274+ au_set_fbbot_dir(file, -1);
4a4d8108 10275+
1facf9fc 10276+ return err;
10277+}
10278+
10279+static int aufs_open_dir(struct inode *inode __maybe_unused,
10280+ struct file *file)
10281+{
4a4d8108
AM
10282+ int err;
10283+ struct super_block *sb;
10284+ struct au_fidir *fidir;
10285+
10286+ err = -ENOMEM;
2000de60 10287+ sb = file->f_path.dentry->d_sb;
4a4d8108 10288+ si_read_lock(sb, AuLock_FLUSH);
e49829fe 10289+ fidir = au_fidir_alloc(sb);
4a4d8108 10290+ if (fidir) {
b912730e
AM
10291+ struct au_do_open_args args = {
10292+ .open = do_open_dir,
10293+ .fidir = fidir
10294+ };
10295+ err = au_do_open(file, &args);
4a4d8108 10296+ if (unlikely(err))
9f237c51 10297+ au_kfree_rcu(fidir);
4a4d8108
AM
10298+ }
10299+ si_read_unlock(sb);
10300+ return err;
1facf9fc 10301+}
10302+
10303+static int aufs_release_dir(struct inode *inode __maybe_unused,
10304+ struct file *file)
10305+{
10306+ struct au_vdir *vdir_cache;
4a4d8108
AM
10307+ struct au_finfo *finfo;
10308+ struct au_fidir *fidir;
f0c0a007 10309+ struct au_hfile *hf;
5afbbe0d 10310+ aufs_bindex_t bindex, bbot;
1facf9fc 10311+
4a4d8108
AM
10312+ finfo = au_fi(file);
10313+ fidir = finfo->fi_hdir;
10314+ if (fidir) {
8b6a4947
AM
10315+ au_hbl_del(&finfo->fi_hlist,
10316+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
4a4d8108
AM
10317+ vdir_cache = fidir->fd_vdir_cache; /* lock-free */
10318+ if (vdir_cache)
1c60b727 10319+ au_vdir_free(vdir_cache);
4a4d8108
AM
10320+
10321+ bindex = finfo->fi_btop;
10322+ if (bindex >= 0) {
f0c0a007 10323+ hf = fidir->fd_hfile + bindex;
4a4d8108
AM
10324+ /*
10325+ * calls fput() instead of filp_close(),
10326+ * since no dnotify or lock for the lower file.
10327+ */
5afbbe0d 10328+ bbot = fidir->fd_bbot;
f0c0a007
AM
10329+ for (; bindex <= bbot; bindex++, hf++)
10330+ if (hf->hf_file)
1c60b727 10331+ au_hfput(hf, /*execed*/0);
4a4d8108 10332+ }
9f237c51 10333+ au_kfree_rcu(fidir);
4a4d8108 10334+ finfo->fi_hdir = NULL;
1facf9fc 10335+ }
1c60b727 10336+ au_finfo_fin(file);
1facf9fc 10337+ return 0;
10338+}
10339+
10340+/* ---------------------------------------------------------------------- */
10341+
4a4d8108
AM
10342+static int au_do_flush_dir(struct file *file, fl_owner_t id)
10343+{
10344+ int err;
5afbbe0d 10345+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
10346+ struct file *h_file;
10347+
10348+ err = 0;
5afbbe0d
AM
10349+ bbot = au_fbbot_dir(file);
10350+ for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
4a4d8108
AM
10351+ h_file = au_hf_dir(file, bindex);
10352+ if (h_file)
10353+ err = vfsub_flush(h_file, id);
10354+ }
10355+ return err;
10356+}
10357+
10358+static int aufs_flush_dir(struct file *file, fl_owner_t id)
10359+{
10360+ return au_do_flush(file, id, au_do_flush_dir);
10361+}
10362+
10363+/* ---------------------------------------------------------------------- */
10364+
1facf9fc 10365+static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
10366+{
10367+ int err;
5afbbe0d 10368+ aufs_bindex_t bbot, bindex;
1facf9fc 10369+ struct inode *inode;
10370+ struct super_block *sb;
10371+
10372+ err = 0;
10373+ sb = dentry->d_sb;
5527c038 10374+ inode = d_inode(dentry);
1facf9fc 10375+ IMustLock(inode);
5afbbe0d
AM
10376+ bbot = au_dbbot(dentry);
10377+ for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
1facf9fc 10378+ struct path h_path;
1facf9fc 10379+
10380+ if (au_test_ro(sb, bindex, inode))
10381+ continue;
10382+ h_path.dentry = au_h_dptr(dentry, bindex);
10383+ if (!h_path.dentry)
10384+ continue;
1facf9fc 10385+
1facf9fc 10386+ h_path.mnt = au_sbr_mnt(sb, bindex);
53392da6 10387+ err = vfsub_fsync(NULL, &h_path, datasync);
1facf9fc 10388+ }
10389+
10390+ return err;
10391+}
10392+
10393+static int au_do_fsync_dir(struct file *file, int datasync)
10394+{
10395+ int err;
5afbbe0d 10396+ aufs_bindex_t bbot, bindex;
1facf9fc 10397+ struct file *h_file;
10398+ struct super_block *sb;
10399+ struct inode *inode;
1facf9fc 10400+
521ced18 10401+ err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
1facf9fc 10402+ if (unlikely(err))
10403+ goto out;
10404+
c06a8ce3 10405+ inode = file_inode(file);
b912730e 10406+ sb = inode->i_sb;
5afbbe0d
AM
10407+ bbot = au_fbbot_dir(file);
10408+ for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
4a4d8108 10409+ h_file = au_hf_dir(file, bindex);
1facf9fc 10410+ if (!h_file || au_test_ro(sb, bindex, inode))
10411+ continue;
10412+
53392da6 10413+ err = vfsub_fsync(h_file, &h_file->f_path, datasync);
1facf9fc 10414+ }
10415+
4f0767ce 10416+out:
1facf9fc 10417+ return err;
10418+}
10419+
10420+/*
10421+ * @file may be NULL
10422+ */
1e00d052
AM
10423+static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
10424+ int datasync)
1facf9fc 10425+{
10426+ int err;
b752ccd1 10427+ struct dentry *dentry;
5527c038 10428+ struct inode *inode;
1facf9fc 10429+ struct super_block *sb;
1facf9fc 10430+
10431+ err = 0;
2000de60 10432+ dentry = file->f_path.dentry;
5527c038 10433+ inode = d_inode(dentry);
febd17d6 10434+ inode_lock(inode);
1facf9fc 10435+ sb = dentry->d_sb;
10436+ si_noflush_read_lock(sb);
10437+ if (file)
10438+ err = au_do_fsync_dir(file, datasync);
10439+ else {
10440+ di_write_lock_child(dentry);
10441+ err = au_do_fsync_dir_no_file(dentry, datasync);
10442+ }
5527c038 10443+ au_cpup_attr_timesizes(inode);
1facf9fc 10444+ di_write_unlock(dentry);
10445+ if (file)
10446+ fi_write_unlock(file);
10447+
10448+ si_read_unlock(sb);
febd17d6 10449+ inode_unlock(inode);
1facf9fc 10450+ return err;
10451+}
10452+
10453+/* ---------------------------------------------------------------------- */
10454+
5afbbe0d 10455+static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
1facf9fc 10456+{
10457+ int err;
10458+ struct dentry *dentry;
9dbd164d 10459+ struct inode *inode, *h_inode;
1facf9fc 10460+ struct super_block *sb;
10461+
062440b3 10462+ AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
392086de 10463+
2000de60 10464+ dentry = file->f_path.dentry;
5527c038 10465+ inode = d_inode(dentry);
1facf9fc 10466+ IMustLock(inode);
10467+
10468+ sb = dentry->d_sb;
10469+ si_read_lock(sb, AuLock_FLUSH);
521ced18 10470+ err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
1facf9fc 10471+ if (unlikely(err))
10472+ goto out;
027c5e7a
AM
10473+ err = au_alive_dir(dentry);
10474+ if (!err)
10475+ err = au_vdir_init(file);
1facf9fc 10476+ di_downgrade_lock(dentry, AuLock_IR);
10477+ if (unlikely(err))
10478+ goto out_unlock;
10479+
5afbbe0d 10480+ h_inode = au_h_iptr(inode, au_ibtop(inode));
b752ccd1 10481+ if (!au_test_nfsd()) {
392086de 10482+ err = au_vdir_fill_de(file, ctx);
9dbd164d 10483+ fsstack_copy_attr_atime(inode, h_inode);
1facf9fc 10484+ } else {
10485+ /*
10486+ * nfsd filldir may call lookup_one_len(), vfs_getattr(),
10487+ * encode_fh() and others.
10488+ */
9dbd164d 10489+ atomic_inc(&h_inode->i_count);
1facf9fc 10490+ di_read_unlock(dentry, AuLock_IR);
10491+ si_read_unlock(sb);
392086de 10492+ err = au_vdir_fill_de(file, ctx);
1facf9fc 10493+ fsstack_copy_attr_atime(inode, h_inode);
10494+ fi_write_unlock(file);
9dbd164d 10495+ iput(h_inode);
1facf9fc 10496+
10497+ AuTraceErr(err);
10498+ return err;
10499+ }
10500+
4f0767ce 10501+out_unlock:
1facf9fc 10502+ di_read_unlock(dentry, AuLock_IR);
10503+ fi_write_unlock(file);
4f0767ce 10504+out:
1facf9fc 10505+ si_read_unlock(sb);
10506+ return err;
10507+}
10508+
10509+/* ---------------------------------------------------------------------- */
10510+
10511+#define AuTestEmpty_WHONLY 1
dece6358
AM
10512+#define AuTestEmpty_CALLED (1 << 1)
10513+#define AuTestEmpty_SHWH (1 << 2)
1facf9fc 10514+#define au_ftest_testempty(flags, name) ((flags) & AuTestEmpty_##name)
7f207e10
AM
10515+#define au_fset_testempty(flags, name) \
10516+ do { (flags) |= AuTestEmpty_##name; } while (0)
10517+#define au_fclr_testempty(flags, name) \
10518+ do { (flags) &= ~AuTestEmpty_##name; } while (0)
1facf9fc 10519+
dece6358
AM
10520+#ifndef CONFIG_AUFS_SHWH
10521+#undef AuTestEmpty_SHWH
10522+#define AuTestEmpty_SHWH 0
10523+#endif
10524+
1facf9fc 10525+struct test_empty_arg {
392086de 10526+ struct dir_context ctx;
1308ab2a 10527+ struct au_nhash *whlist;
1facf9fc 10528+ unsigned int flags;
10529+ int err;
10530+ aufs_bindex_t bindex;
10531+};
10532+
392086de
AM
10533+static int test_empty_cb(struct dir_context *ctx, const char *__name,
10534+ int namelen, loff_t offset __maybe_unused, u64 ino,
dece6358 10535+ unsigned int d_type)
1facf9fc 10536+{
392086de
AM
10537+ struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10538+ ctx);
1facf9fc 10539+ char *name = (void *)__name;
10540+
10541+ arg->err = 0;
10542+ au_fset_testempty(arg->flags, CALLED);
10543+ /* smp_mb(); */
10544+ if (name[0] == '.'
10545+ && (namelen == 1 || (name[1] == '.' && namelen == 2)))
10546+ goto out; /* success */
10547+
10548+ if (namelen <= AUFS_WH_PFX_LEN
10549+ || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10550+ if (au_ftest_testempty(arg->flags, WHONLY)
1308ab2a 10551+ && !au_nhash_test_known_wh(arg->whlist, name, namelen))
1facf9fc 10552+ arg->err = -ENOTEMPTY;
10553+ goto out;
10554+ }
10555+
10556+ name += AUFS_WH_PFX_LEN;
10557+ namelen -= AUFS_WH_PFX_LEN;
1308ab2a 10558+ if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
1facf9fc 10559+ arg->err = au_nhash_append_wh
1308ab2a 10560+ (arg->whlist, name, namelen, ino, d_type, arg->bindex,
dece6358 10561+ au_ftest_testempty(arg->flags, SHWH));
1facf9fc 10562+
4f0767ce 10563+out:
1facf9fc 10564+ /* smp_mb(); */
10565+ AuTraceErr(arg->err);
10566+ return arg->err;
10567+}
10568+
10569+static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10570+{
10571+ int err;
10572+ struct file *h_file;
acd2b654 10573+ struct au_branch *br;
1facf9fc 10574+
10575+ h_file = au_h_open(dentry, arg->bindex,
10576+ O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
392086de 10577+ /*file*/NULL, /*force_wr*/0);
1facf9fc 10578+ err = PTR_ERR(h_file);
10579+ if (IS_ERR(h_file))
10580+ goto out;
10581+
10582+ err = 0;
10583+ if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
c06a8ce3 10584+ && !file_inode(h_file)->i_nlink)
1facf9fc 10585+ goto out_put;
10586+
10587+ do {
10588+ arg->err = 0;
10589+ au_fclr_testempty(arg->flags, CALLED);
10590+ /* smp_mb(); */
392086de 10591+ err = vfsub_iterate_dir(h_file, &arg->ctx);
1facf9fc 10592+ if (err >= 0)
10593+ err = arg->err;
10594+ } while (!err && au_ftest_testempty(arg->flags, CALLED));
10595+
4f0767ce 10596+out_put:
1facf9fc 10597+ fput(h_file);
acd2b654
AM
10598+ br = au_sbr(dentry->d_sb, arg->bindex);
10599+ au_lcnt_dec(&br->br_nfiles);
4f0767ce 10600+out:
1facf9fc 10601+ return err;
10602+}
10603+
10604+struct do_test_empty_args {
10605+ int *errp;
10606+ struct dentry *dentry;
10607+ struct test_empty_arg *arg;
10608+};
10609+
10610+static void call_do_test_empty(void *args)
10611+{
10612+ struct do_test_empty_args *a = args;
10613+ *a->errp = do_test_empty(a->dentry, a->arg);
10614+}
10615+
10616+static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10617+{
10618+ int err, wkq_err;
10619+ struct dentry *h_dentry;
10620+ struct inode *h_inode;
0b2a12c6 10621+ struct user_namespace *h_userns;
1facf9fc 10622+
0b2a12c6 10623+ h_userns = au_sbr_userns(dentry->d_sb, arg->bindex);
1facf9fc 10624+ h_dentry = au_h_dptr(dentry, arg->bindex);
5527c038 10625+ h_inode = d_inode(h_dentry);
53392da6 10626+ /* todo: i_mode changes anytime? */
be118d29 10627+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
0b2a12c6 10628+ err = au_test_h_perm_sio(h_userns, h_inode, MAY_EXEC | MAY_READ);
3c1bdaff 10629+ inode_unlock_shared(h_inode);
1facf9fc 10630+ if (!err)
10631+ err = do_test_empty(dentry, arg);
10632+ else {
10633+ struct do_test_empty_args args = {
10634+ .errp = &err,
10635+ .dentry = dentry,
10636+ .arg = arg
10637+ };
10638+ unsigned int flags = arg->flags;
10639+
10640+ wkq_err = au_wkq_wait(call_do_test_empty, &args);
10641+ if (unlikely(wkq_err))
10642+ err = wkq_err;
10643+ arg->flags = flags;
10644+ }
10645+
10646+ return err;
10647+}
10648+
10649+int au_test_empty_lower(struct dentry *dentry)
10650+{
10651+ int err;
1308ab2a 10652+ unsigned int rdhash;
5afbbe0d 10653+ aufs_bindex_t bindex, btop, btail;
1308ab2a 10654+ struct au_nhash whlist;
392086de
AM
10655+ struct test_empty_arg arg = {
10656+ .ctx = {
2000de60 10657+ .actor = test_empty_cb
392086de
AM
10658+ }
10659+ };
076b876e 10660+ int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
1facf9fc 10661+
dece6358
AM
10662+ SiMustAnyLock(dentry->d_sb);
10663+
1308ab2a 10664+ rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10665+ if (!rdhash)
10666+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10667+ err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
dece6358 10668+ if (unlikely(err))
1facf9fc 10669+ goto out;
10670+
1facf9fc 10671+ arg.flags = 0;
1308ab2a 10672+ arg.whlist = &whlist;
5afbbe0d 10673+ btop = au_dbtop(dentry);
dece6358
AM
10674+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10675+ au_fset_testempty(arg.flags, SHWH);
076b876e
AM
10676+ test_empty = do_test_empty;
10677+ if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10678+ test_empty = sio_test_empty;
5afbbe0d 10679+ arg.bindex = btop;
076b876e 10680+ err = test_empty(dentry, &arg);
1facf9fc 10681+ if (unlikely(err))
10682+ goto out_whlist;
10683+
10684+ au_fset_testempty(arg.flags, WHONLY);
10685+ btail = au_dbtaildir(dentry);
5afbbe0d 10686+ for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
1facf9fc 10687+ struct dentry *h_dentry;
10688+
10689+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 10690+ if (h_dentry && d_is_positive(h_dentry)) {
1facf9fc 10691+ arg.bindex = bindex;
076b876e 10692+ err = test_empty(dentry, &arg);
1facf9fc 10693+ }
10694+ }
10695+
4f0767ce 10696+out_whlist:
1308ab2a 10697+ au_nhash_wh_free(&whlist);
4f0767ce 10698+out:
1facf9fc 10699+ return err;
10700+}
10701+
10702+int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10703+{
10704+ int err;
392086de
AM
10705+ struct test_empty_arg arg = {
10706+ .ctx = {
2000de60 10707+ .actor = test_empty_cb
392086de
AM
10708+ }
10709+ };
1facf9fc 10710+ aufs_bindex_t bindex, btail;
10711+
10712+ err = 0;
1308ab2a 10713+ arg.whlist = whlist;
1facf9fc 10714+ arg.flags = AuTestEmpty_WHONLY;
dece6358
AM
10715+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10716+ au_fset_testempty(arg.flags, SHWH);
1facf9fc 10717+ btail = au_dbtaildir(dentry);
5afbbe0d 10718+ for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
1facf9fc 10719+ struct dentry *h_dentry;
10720+
10721+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 10722+ if (h_dentry && d_is_positive(h_dentry)) {
1facf9fc 10723+ arg.bindex = bindex;
10724+ err = sio_test_empty(dentry, &arg);
10725+ }
10726+ }
10727+
10728+ return err;
10729+}
10730+
10731+/* ---------------------------------------------------------------------- */
10732+
10733+const struct file_operations aufs_dir_fop = {
4a4d8108 10734+ .owner = THIS_MODULE,
027c5e7a 10735+ .llseek = default_llseek,
1facf9fc 10736+ .read = generic_read_dir,
5afbbe0d 10737+ .iterate_shared = aufs_iterate_shared,
1facf9fc 10738+ .unlocked_ioctl = aufs_ioctl_dir,
b752ccd1
AM
10739+#ifdef CONFIG_COMPAT
10740+ .compat_ioctl = aufs_compat_ioctl_dir,
10741+#endif
1facf9fc 10742+ .open = aufs_open_dir,
10743+ .release = aufs_release_dir,
4a4d8108 10744+ .flush = aufs_flush_dir,
1facf9fc 10745+ .fsync = aufs_fsync_dir
10746+};
7f207e10 10747diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
eca34b5c 10748--- /usr/share/empty/fs/aufs/dir.h 1970-01-01 01:00:00.000000000 +0100
46016270 10749+++ linux/fs/aufs/dir.h 2021-02-24 13:33:42.741013619 +0100
9f237c51 10750@@ -0,0 +1,134 @@
062440b3 10751+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 10752+/*
d58c55f2 10753+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 10754+ *
10755+ * This program, aufs is free software; you can redistribute it and/or modify
10756+ * it under the terms of the GNU General Public License as published by
10757+ * the Free Software Foundation; either version 2 of the License, or
10758+ * (at your option) any later version.
dece6358
AM
10759+ *
10760+ * This program is distributed in the hope that it will be useful,
10761+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10762+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10763+ * GNU General Public License for more details.
10764+ *
10765+ * You should have received a copy of the GNU General Public License
523b37e3 10766+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 10767+ */
10768+
10769+/*
10770+ * directory operations
10771+ */
10772+
10773+#ifndef __AUFS_DIR_H__
10774+#define __AUFS_DIR_H__
10775+
10776+#ifdef __KERNEL__
10777+
10778+#include <linux/fs.h>
1facf9fc 10779+
10780+/* ---------------------------------------------------------------------- */
10781+
10782+/* need to be faster and smaller */
10783+
10784+struct au_nhash {
dece6358
AM
10785+ unsigned int nh_num;
10786+ struct hlist_head *nh_head;
1facf9fc 10787+};
10788+
10789+struct au_vdir_destr {
10790+ unsigned char len;
42b5c33a 10791+ unsigned char name[];
1facf9fc 10792+} __packed;
10793+
10794+struct au_vdir_dehstr {
10795+ struct hlist_node hash;
1c60b727 10796+ struct au_vdir_destr *str;
9f237c51 10797+ struct rcu_head rcu;
4a4d8108 10798+} ____cacheline_aligned_in_smp;
1facf9fc 10799+
10800+struct au_vdir_de {
10801+ ino_t de_ino;
10802+ unsigned char de_type;
10803+ /* caution: packed */
10804+ struct au_vdir_destr de_str;
10805+} __packed;
10806+
10807+struct au_vdir_wh {
10808+ struct hlist_node wh_hash;
dece6358
AM
10809+#ifdef CONFIG_AUFS_SHWH
10810+ ino_t wh_ino;
1facf9fc 10811+ aufs_bindex_t wh_bindex;
dece6358
AM
10812+ unsigned char wh_type;
10813+#else
10814+ aufs_bindex_t wh_bindex;
10815+#endif
10816+ /* caution: packed */
1facf9fc 10817+ struct au_vdir_destr wh_str;
10818+} __packed;
10819+
10820+union au_vdir_deblk_p {
10821+ unsigned char *deblk;
10822+ struct au_vdir_de *de;
10823+};
10824+
10825+struct au_vdir {
10826+ unsigned char **vd_deblk;
10827+ unsigned long vd_nblk;
1facf9fc 10828+ struct {
10829+ unsigned long ul;
10830+ union au_vdir_deblk_p p;
10831+ } vd_last;
10832+
be118d29 10833+ u64 vd_version;
dece6358 10834+ unsigned int vd_deblk_sz;
9f237c51
AM
10835+ unsigned long vd_jiffy;
10836+ struct rcu_head rcu;
4a4d8108 10837+} ____cacheline_aligned_in_smp;
1facf9fc 10838+
10839+/* ---------------------------------------------------------------------- */
10840+
10841+/* dir.c */
10842+extern const struct file_operations aufs_dir_fop;
10843+void au_add_nlink(struct inode *dir, struct inode *h_dir);
10844+void au_sub_nlink(struct inode *dir, struct inode *h_dir);
1308ab2a 10845+loff_t au_dir_size(struct file *file, struct dentry *dentry);
b912730e 10846+void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
1facf9fc 10847+int au_test_empty_lower(struct dentry *dentry);
10848+int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10849+
10850+/* vdir.c */
1308ab2a 10851+unsigned int au_rdhash_est(loff_t sz);
dece6358
AM
10852+int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
10853+void au_nhash_wh_free(struct au_nhash *whlist);
1facf9fc 10854+int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
10855+ int limit);
dece6358
AM
10856+int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
10857+int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
10858+ unsigned int d_type, aufs_bindex_t bindex,
10859+ unsigned char shwh);
1c60b727 10860+void au_vdir_free(struct au_vdir *vdir);
1facf9fc 10861+int au_vdir_init(struct file *file);
392086de 10862+int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
1facf9fc 10863+
10864+/* ioctl.c */
10865+long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
10866+
1308ab2a 10867+#ifdef CONFIG_AUFS_RDU
10868+/* rdu.c */
10869+long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
b752ccd1
AM
10870+#ifdef CONFIG_COMPAT
10871+long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
10872+ unsigned long arg);
10873+#endif
1308ab2a 10874+#else
c1595e42
JR
10875+AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
10876+ unsigned int cmd, unsigned long arg)
b752ccd1 10877+#ifdef CONFIG_COMPAT
c1595e42
JR
10878+AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
10879+ unsigned int cmd, unsigned long arg)
b752ccd1 10880+#endif
1308ab2a 10881+#endif
10882+
1facf9fc 10883+#endif /* __KERNEL__ */
10884+#endif /* __AUFS_DIR_H__ */
8b6a4947 10885diff -urN /usr/share/empty/fs/aufs/dirren.c linux/fs/aufs/dirren.c
eca34b5c 10886--- /usr/share/empty/fs/aufs/dirren.c 1970-01-01 01:00:00.000000000 +0100
46016270 10887+++ linux/fs/aufs/dirren.c 2021-06-30 21:35:11.393873211 +0200
062440b3 10888@@ -0,0 +1,1316 @@
cd7a4cd9 10889+// SPDX-License-Identifier: GPL-2.0
8b6a4947 10890+/*
d58c55f2 10891+ * Copyright (C) 2017-2020 Junjiro R. Okajima
8b6a4947
AM
10892+ *
10893+ * This program, aufs is free software; you can redistribute it and/or modify
10894+ * it under the terms of the GNU General Public License as published by
10895+ * the Free Software Foundation; either version 2 of the License, or
10896+ * (at your option) any later version.
10897+ *
10898+ * This program is distributed in the hope that it will be useful,
10899+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10900+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10901+ * GNU General Public License for more details.
10902+ *
10903+ * You should have received a copy of the GNU General Public License
10904+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
10905+ */
10906+
10907+/*
acd2b654 10908+ * special handling in renaming a directory
8b6a4947
AM
10909+ * in order to support looking-up the before-renamed name on the lower readonly
10910+ * branches
10911+ */
10912+
10913+#include <linux/byteorder/generic.h>
10914+#include "aufs.h"
10915+
10916+static void au_dr_hino_del(struct au_dr_br *dr, struct au_dr_hino *ent)
10917+{
10918+ int idx;
10919+
10920+ idx = au_dr_ihash(ent->dr_h_ino);
10921+ au_hbl_del(&ent->dr_hnode, dr->dr_h_ino + idx);
10922+}
10923+
10924+static int au_dr_hino_test_empty(struct au_dr_br *dr)
10925+{
10926+ int ret, i;
10927+ struct hlist_bl_head *hbl;
10928+
10929+ ret = 1;
10930+ for (i = 0; ret && i < AuDirren_NHASH; i++) {
10931+ hbl = dr->dr_h_ino + i;
10932+ hlist_bl_lock(hbl);
10933+ ret &= hlist_bl_empty(hbl);
10934+ hlist_bl_unlock(hbl);
10935+ }
10936+
10937+ return ret;
10938+}
10939+
10940+static struct au_dr_hino *au_dr_hino_find(struct au_dr_br *dr, ino_t ino)
10941+{
10942+ struct au_dr_hino *found, *ent;
10943+ struct hlist_bl_head *hbl;
10944+ struct hlist_bl_node *pos;
10945+ int idx;
10946+
10947+ found = NULL;
10948+ idx = au_dr_ihash(ino);
10949+ hbl = dr->dr_h_ino + idx;
10950+ hlist_bl_lock(hbl);
10951+ hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10952+ if (ent->dr_h_ino == ino) {
10953+ found = ent;
10954+ break;
10955+ }
10956+ hlist_bl_unlock(hbl);
10957+
10958+ return found;
10959+}
10960+
10961+int au_dr_hino_test_add(struct au_dr_br *dr, ino_t ino,
10962+ struct au_dr_hino *add_ent)
10963+{
10964+ int found, idx;
10965+ struct hlist_bl_head *hbl;
10966+ struct hlist_bl_node *pos;
10967+ struct au_dr_hino *ent;
10968+
10969+ found = 0;
10970+ idx = au_dr_ihash(ino);
10971+ hbl = dr->dr_h_ino + idx;
43982f53 10972+#if 0 /* debug print */
8b6a4947
AM
10973+ {
10974+ struct hlist_bl_node *tmp;
10975+
10976+ hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
10977+ AuDbg("hi%llu\n", (unsigned long long)ent->dr_h_ino);
10978+ }
10979+#endif
10980+ hlist_bl_lock(hbl);
10981+ hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10982+ if (ent->dr_h_ino == ino) {
10983+ found = 1;
10984+ break;
10985+ }
10986+ if (!found && add_ent)
10987+ hlist_bl_add_head(&add_ent->dr_hnode, hbl);
10988+ hlist_bl_unlock(hbl);
10989+
10990+ if (!found && add_ent)
10991+ AuDbg("i%llu added\n", (unsigned long long)add_ent->dr_h_ino);
10992+
10993+ return found;
10994+}
10995+
10996+void au_dr_hino_free(struct au_dr_br *dr)
10997+{
10998+ int i;
10999+ struct hlist_bl_head *hbl;
11000+ struct hlist_bl_node *pos, *tmp;
11001+ struct au_dr_hino *ent;
11002+
11003+ /* SiMustWriteLock(sb); */
11004+
11005+ for (i = 0; i < AuDirren_NHASH; i++) {
11006+ hbl = dr->dr_h_ino + i;
11007+ /* no spinlock since sbinfo must be write-locked */
11008+ hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
9f237c51 11009+ au_kfree_rcu(ent);
8b6a4947
AM
11010+ INIT_HLIST_BL_HEAD(hbl);
11011+ }
11012+}
11013+
11014+/* returns the number of inodes or an error */
11015+static int au_dr_hino_store(struct super_block *sb, struct au_branch *br,
11016+ struct file *hinofile)
11017+{
11018+ int err, i;
11019+ ssize_t ssz;
11020+ loff_t pos, oldsize;
11021+ __be64 u64;
11022+ struct inode *hinoinode;
11023+ struct hlist_bl_head *hbl;
11024+ struct hlist_bl_node *n1, *n2;
11025+ struct au_dr_hino *ent;
11026+
11027+ SiMustWriteLock(sb);
11028+ AuDebugOn(!au_br_writable(br->br_perm));
11029+
11030+ hinoinode = file_inode(hinofile);
11031+ oldsize = i_size_read(hinoinode);
11032+
11033+ err = 0;
11034+ pos = 0;
11035+ hbl = br->br_dirren.dr_h_ino;
11036+ for (i = 0; !err && i < AuDirren_NHASH; i++, hbl++) {
11037+ /* no bit-lock since sbinfo must be write-locked */
11038+ hlist_bl_for_each_entry_safe(ent, n1, n2, hbl, dr_hnode) {
11039+ AuDbg("hi%llu, %pD2\n",
11040+ (unsigned long long)ent->dr_h_ino, hinofile);
11041+ u64 = cpu_to_be64(ent->dr_h_ino);
11042+ ssz = vfsub_write_k(hinofile, &u64, sizeof(u64), &pos);
11043+ if (ssz == sizeof(u64))
11044+ continue;
11045+
11046+ /* write error */
11047+ pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11048+ err = -ENOSPC;
11049+ if (ssz < 0)
11050+ err = ssz;
11051+ break;
11052+ }
11053+ }
11054+ /* regardless the error */
11055+ if (pos < oldsize) {
11056+ err = vfsub_trunc(&hinofile->f_path, pos, /*attr*/0, hinofile);
11057+ AuTraceErr(err);
11058+ }
11059+
11060+ AuTraceErr(err);
11061+ return err;
11062+}
11063+
11064+static int au_dr_hino_load(struct au_dr_br *dr, struct file *hinofile)
11065+{
11066+ int err, hidx;
11067+ ssize_t ssz;
11068+ size_t sz, n;
11069+ loff_t pos;
11070+ uint64_t u64;
11071+ struct au_dr_hino *ent;
11072+ struct inode *hinoinode;
11073+ struct hlist_bl_head *hbl;
11074+
11075+ err = 0;
11076+ pos = 0;
11077+ hbl = dr->dr_h_ino;
11078+ hinoinode = file_inode(hinofile);
11079+ sz = i_size_read(hinoinode);
11080+ AuDebugOn(sz % sizeof(u64));
11081+ n = sz / sizeof(u64);
11082+ while (n--) {
11083+ ssz = vfsub_read_k(hinofile, &u64, sizeof(u64), &pos);
11084+ if (unlikely(ssz != sizeof(u64))) {
11085+ pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11086+ err = -EINVAL;
11087+ if (ssz < 0)
11088+ err = ssz;
11089+ goto out_free;
11090+ }
11091+
11092+ ent = kmalloc(sizeof(*ent), GFP_NOFS);
11093+ if (!ent) {
11094+ err = -ENOMEM;
11095+ AuTraceErr(err);
11096+ goto out_free;
11097+ }
11098+ ent->dr_h_ino = be64_to_cpu((__force __be64)u64);
11099+ AuDbg("hi%llu, %pD2\n",
11100+ (unsigned long long)ent->dr_h_ino, hinofile);
11101+ hidx = au_dr_ihash(ent->dr_h_ino);
11102+ au_hbl_add(&ent->dr_hnode, hbl + hidx);
11103+ }
11104+ goto out; /* success */
11105+
11106+out_free:
11107+ au_dr_hino_free(dr);
11108+out:
11109+ AuTraceErr(err);
11110+ return err;
11111+}
11112+
11113+/*
11114+ * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
11115+ * @path is a switch to distinguish load and store.
11116+ */
11117+static int au_dr_hino(struct super_block *sb, aufs_bindex_t bindex,
11118+ struct au_branch *br, const struct path *path)
11119+{
11120+ int err, flags;
11121+ unsigned char load, suspend;
11122+ struct file *hinofile;
11123+ struct au_hinode *hdir;
11124+ struct inode *dir, *delegated;
11125+ struct path hinopath;
11126+ struct qstr hinoname = QSTR_INIT(AUFS_WH_DR_BRHINO,
11127+ sizeof(AUFS_WH_DR_BRHINO) - 1);
11128+
11129+ AuDebugOn(bindex < 0 && !br);
11130+ AuDebugOn(bindex >= 0 && br);
11131+
11132+ err = -EINVAL;
11133+ suspend = !br;
11134+ if (suspend)
11135+ br = au_sbr(sb, bindex);
11136+ load = !!path;
11137+ if (!load) {
11138+ path = &br->br_path;
11139+ AuDebugOn(!au_br_writable(br->br_perm));
11140+ if (unlikely(!au_br_writable(br->br_perm)))
11141+ goto out;
11142+ }
11143+
11144+ hdir = NULL;
11145+ if (suspend) {
11146+ dir = d_inode(sb->s_root);
11147+ hdir = au_hinode(au_ii(dir), bindex);
11148+ dir = hdir->hi_inode;
11149+ au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11150+ } else {
11151+ dir = d_inode(path->dentry);
11152+ inode_lock_nested(dir, AuLsc_I_CHILD);
11153+ }
11154+ hinopath.dentry = vfsub_lkup_one(&hinoname, path->dentry);
11155+ err = PTR_ERR(hinopath.dentry);
11156+ if (IS_ERR(hinopath.dentry))
11157+ goto out_unlock;
0b2a12c6 11158+ hinopath.mnt = path->mnt;
8b6a4947
AM
11159+
11160+ err = 0;
11161+ flags = O_RDONLY;
11162+ if (load) {
11163+ if (d_is_negative(hinopath.dentry))
11164+ goto out_dput; /* success */
11165+ } else {
11166+ if (au_dr_hino_test_empty(&br->br_dirren)) {
11167+ if (d_is_positive(hinopath.dentry)) {
11168+ delegated = NULL;
11169+ err = vfsub_unlink(dir, &hinopath, &delegated,
11170+ /*force*/0);
11171+ AuTraceErr(err);
11172+ if (unlikely(err))
11173+ pr_err("ignored err %d, %pd2\n",
11174+ err, hinopath.dentry);
11175+ if (unlikely(err == -EWOULDBLOCK))
11176+ iput(delegated);
11177+ err = 0;
11178+ }
11179+ goto out_dput;
11180+ } else if (!d_is_positive(hinopath.dentry)) {
11181+ err = vfsub_create(dir, &hinopath, 0600,
11182+ /*want_excl*/false);
11183+ AuTraceErr(err);
11184+ if (unlikely(err))
11185+ goto out_dput;
11186+ }
11187+ flags = O_WRONLY;
11188+ }
8b6a4947
AM
11189+ hinofile = vfsub_dentry_open(&hinopath, flags);
11190+ if (suspend)
11191+ au_hn_inode_unlock(hdir);
11192+ else
11193+ inode_unlock(dir);
11194+ dput(hinopath.dentry);
11195+ AuTraceErrPtr(hinofile);
11196+ if (IS_ERR(hinofile)) {
11197+ err = PTR_ERR(hinofile);
11198+ goto out;
11199+ }
11200+
11201+ if (load)
11202+ err = au_dr_hino_load(&br->br_dirren, hinofile);
11203+ else
11204+ err = au_dr_hino_store(sb, br, hinofile);
11205+ fput(hinofile);
11206+ goto out;
11207+
11208+out_dput:
11209+ dput(hinopath.dentry);
11210+out_unlock:
11211+ if (suspend)
11212+ au_hn_inode_unlock(hdir);
11213+ else
11214+ inode_unlock(dir);
11215+out:
11216+ AuTraceErr(err);
11217+ return err;
11218+}
11219+
11220+/* ---------------------------------------------------------------------- */
11221+
11222+static int au_dr_brid_init(struct au_dr_brid *brid, const struct path *path)
11223+{
11224+ int err;
11225+ struct kstatfs kstfs;
11226+ dev_t dev;
11227+ struct dentry *dentry;
11228+ struct super_block *sb;
11229+
11230+ err = vfs_statfs((void *)path, &kstfs);
11231+ AuTraceErr(err);
11232+ if (unlikely(err))
11233+ goto out;
11234+
11235+ /* todo: support for UUID */
11236+
11237+ if (kstfs.f_fsid.val[0] || kstfs.f_fsid.val[1]) {
11238+ brid->type = AuBrid_FSID;
11239+ brid->fsid = kstfs.f_fsid;
11240+ } else {
11241+ dentry = path->dentry;
11242+ sb = dentry->d_sb;
11243+ dev = sb->s_dev;
11244+ if (dev) {
11245+ brid->type = AuBrid_DEV;
11246+ brid->dev = dev;
11247+ }
11248+ }
11249+
11250+out:
11251+ return err;
11252+}
11253+
11254+int au_dr_br_init(struct super_block *sb, struct au_branch *br,
11255+ const struct path *path)
11256+{
11257+ int err, i;
11258+ struct au_dr_br *dr;
11259+ struct hlist_bl_head *hbl;
11260+
11261+ dr = &br->br_dirren;
11262+ hbl = dr->dr_h_ino;
11263+ for (i = 0; i < AuDirren_NHASH; i++, hbl++)
11264+ INIT_HLIST_BL_HEAD(hbl);
11265+
11266+ err = au_dr_brid_init(&dr->dr_brid, path);
11267+ if (unlikely(err))
11268+ goto out;
11269+
11270+ if (au_opt_test(au_mntflags(sb), DIRREN))
11271+ err = au_dr_hino(sb, /*bindex*/-1, br, path);
11272+
11273+out:
11274+ AuTraceErr(err);
11275+ return err;
11276+}
11277+
11278+int au_dr_br_fin(struct super_block *sb, struct au_branch *br)
11279+{
11280+ int err;
11281+
11282+ err = 0;
11283+ if (au_br_writable(br->br_perm))
11284+ err = au_dr_hino(sb, /*bindex*/-1, br, /*path*/NULL);
11285+ if (!err)
11286+ au_dr_hino_free(&br->br_dirren);
11287+
11288+ return err;
11289+}
11290+
11291+/* ---------------------------------------------------------------------- */
11292+
11293+static int au_brid_str(struct au_dr_brid *brid, struct inode *h_inode,
11294+ char *buf, size_t sz)
11295+{
11296+ int err;
11297+ unsigned int major, minor;
11298+ char *p;
11299+
11300+ p = buf;
11301+ err = snprintf(p, sz, "%d_", brid->type);
11302+ AuDebugOn(err > sz);
11303+ p += err;
11304+ sz -= err;
11305+ switch (brid->type) {
11306+ case AuBrid_Unset:
11307+ return -EINVAL;
11308+ case AuBrid_UUID:
11309+ err = snprintf(p, sz, "%pU", brid->uuid.b);
11310+ break;
11311+ case AuBrid_FSID:
11312+ err = snprintf(p, sz, "%08x-%08x",
11313+ brid->fsid.val[0], brid->fsid.val[1]);
11314+ break;
11315+ case AuBrid_DEV:
11316+ major = MAJOR(brid->dev);
11317+ minor = MINOR(brid->dev);
11318+ if (major <= 0xff && minor <= 0xff)
11319+ err = snprintf(p, sz, "%02x%02x", major, minor);
11320+ else
11321+ err = snprintf(p, sz, "%03x:%05x", major, minor);
11322+ break;
11323+ }
11324+ AuDebugOn(err > sz);
11325+ p += err;
11326+ sz -= err;
11327+ err = snprintf(p, sz, "_%llu", (unsigned long long)h_inode->i_ino);
11328+ AuDebugOn(err > sz);
11329+ p += err;
11330+ sz -= err;
11331+
11332+ return p - buf;
11333+}
11334+
11335+static int au_drinfo_name(struct au_branch *br, char *name, int len)
11336+{
11337+ int rlen;
11338+ struct dentry *br_dentry;
11339+ struct inode *br_inode;
11340+
11341+ br_dentry = au_br_dentry(br);
11342+ br_inode = d_inode(br_dentry);
11343+ rlen = au_brid_str(&br->br_dirren.dr_brid, br_inode, name, len);
11344+ AuDebugOn(rlen >= AUFS_DIRREN_ENV_VAL_SZ);
11345+ AuDebugOn(rlen > len);
11346+
11347+ return rlen;
11348+}
11349+
11350+/* ---------------------------------------------------------------------- */
11351+
11352+/*
11353+ * from the given @h_dentry, construct drinfo at @*fdata.
11354+ * when the size of @*fdata is not enough, reallocate and return new @fdata and
11355+ * @allocated.
11356+ */
11357+static int au_drinfo_construct(struct au_drinfo_fdata **fdata,
11358+ struct dentry *h_dentry,
11359+ unsigned char *allocated)
11360+{
11361+ int err, v;
11362+ struct au_drinfo_fdata *f, *p;
11363+ struct au_drinfo *drinfo;
11364+ struct inode *h_inode;
11365+ struct qstr *qname;
11366+
11367+ err = 0;
11368+ f = *fdata;
11369+ h_inode = d_inode(h_dentry);
11370+ qname = &h_dentry->d_name;
11371+ drinfo = &f->drinfo;
11372+ drinfo->ino = (__force uint64_t)cpu_to_be64(h_inode->i_ino);
11373+ drinfo->oldnamelen = qname->len;
11374+ if (*allocated < sizeof(*f) + qname->len) {
11375+ v = roundup_pow_of_two(*allocated + qname->len);
11376+ p = au_krealloc(f, v, GFP_NOFS, /*may_shrink*/0);
11377+ if (unlikely(!p)) {
11378+ err = -ENOMEM;
11379+ AuTraceErr(err);
11380+ goto out;
11381+ }
11382+ f = p;
11383+ *fdata = f;
11384+ *allocated = v;
11385+ drinfo = &f->drinfo;
11386+ }
11387+ memcpy(drinfo->oldname, qname->name, qname->len);
11388+ AuDbg("i%llu, %.*s\n",
11389+ be64_to_cpu((__force __be64)drinfo->ino), drinfo->oldnamelen,
11390+ drinfo->oldname);
11391+
11392+out:
11393+ AuTraceErr(err);
11394+ return err;
11395+}
11396+
11397+/* callers have to free the return value */
11398+static struct au_drinfo *au_drinfo_read_k(struct file *file, ino_t h_ino)
11399+{
11400+ struct au_drinfo *ret, *drinfo;
11401+ struct au_drinfo_fdata fdata;
11402+ int len;
11403+ loff_t pos;
11404+ ssize_t ssz;
11405+
11406+ ret = ERR_PTR(-EIO);
11407+ pos = 0;
11408+ ssz = vfsub_read_k(file, &fdata, sizeof(fdata), &pos);
11409+ if (unlikely(ssz != sizeof(fdata))) {
11410+ AuIOErr("ssz %zd, %u, %pD2\n",
11411+ ssz, (unsigned int)sizeof(fdata), file);
11412+ goto out;
11413+ }
11414+
11415+ fdata.magic = ntohl((__force __be32)fdata.magic);
11416+ switch (fdata.magic) {
11417+ case AUFS_DRINFO_MAGIC_V1:
11418+ break;
11419+ default:
11420+ AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
11421+ fdata.magic, AUFS_DRINFO_MAGIC_V1, file);
11422+ goto out;
11423+ }
11424+
11425+ drinfo = &fdata.drinfo;
11426+ len = drinfo->oldnamelen;
11427+ if (!len) {
11428+ AuIOErr("broken drinfo %pD2\n", file);
11429+ goto out;
11430+ }
11431+
11432+ ret = NULL;
11433+ drinfo->ino = be64_to_cpu((__force __be64)drinfo->ino);
11434+ if (unlikely(h_ino && drinfo->ino != h_ino)) {
11435+ AuDbg("ignored i%llu, i%llu, %pD2\n",
11436+ (unsigned long long)drinfo->ino,
11437+ (unsigned long long)h_ino, file);
11438+ goto out; /* success */
11439+ }
11440+
11441+ ret = kmalloc(sizeof(*ret) + len, GFP_NOFS);
11442+ if (unlikely(!ret)) {
11443+ ret = ERR_PTR(-ENOMEM);
11444+ AuTraceErrPtr(ret);
11445+ goto out;
11446+ }
11447+
11448+ *ret = *drinfo;
11449+ ssz = vfsub_read_k(file, (void *)ret->oldname, len, &pos);
11450+ if (unlikely(ssz != len)) {
9f237c51 11451+ au_kfree_rcu(ret);
8b6a4947
AM
11452+ ret = ERR_PTR(-EIO);
11453+ AuIOErr("ssz %zd, %u, %pD2\n", ssz, len, file);
11454+ goto out;
11455+ }
11456+
11457+ AuDbg("oldname %.*s\n", ret->oldnamelen, ret->oldname);
11458+
11459+out:
11460+ return ret;
11461+}
11462+
11463+/* ---------------------------------------------------------------------- */
11464+
11465+/* in order to be revertible */
11466+struct au_drinfo_rev_elm {
11467+ int created;
11468+ struct dentry *info_dentry;
11469+ struct au_drinfo *info_last;
11470+};
11471+
11472+struct au_drinfo_rev {
11473+ unsigned char already;
11474+ aufs_bindex_t nelm;
42b5c33a 11475+ struct au_drinfo_rev_elm elm[];
8b6a4947
AM
11476+};
11477+
11478+/* todo: isn't it too large? */
11479+struct au_drinfo_store {
11480+ struct path h_ppath;
11481+ struct dentry *h_dentry;
11482+ struct au_drinfo_fdata *fdata;
11483+ char *infoname; /* inside of whname, just after PFX */
11484+ char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ];
11485+ aufs_bindex_t btgt, btail;
11486+ unsigned char no_sio,
11487+ allocated, /* current size of *fdata */
11488+ infonamelen, /* room size for p */
acd2b654 11489+ whnamelen, /* length of the generated name */
8b6a4947
AM
11490+ renameback; /* renamed back */
11491+};
11492+
11493+/* on rename(2) error, the caller should revert it using @elm */
11494+static int au_drinfo_do_store(struct au_drinfo_store *w,
11495+ struct au_drinfo_rev_elm *elm)
11496+{
11497+ int err, len;
11498+ ssize_t ssz;
11499+ loff_t pos;
11500+ struct path infopath = {
11501+ .mnt = w->h_ppath.mnt
11502+ };
11503+ struct inode *h_dir, *h_inode, *delegated;
11504+ struct file *infofile;
11505+ struct qstr *qname;
11506+
11507+ AuDebugOn(elm
11508+ && memcmp(elm, page_address(ZERO_PAGE(0)), sizeof(*elm)));
11509+
11510+ infopath.dentry = vfsub_lookup_one_len(w->whname, w->h_ppath.dentry,
11511+ w->whnamelen);
11512+ AuTraceErrPtr(infopath.dentry);
11513+ if (IS_ERR(infopath.dentry)) {
11514+ err = PTR_ERR(infopath.dentry);
11515+ goto out;
11516+ }
11517+
11518+ err = 0;
11519+ h_dir = d_inode(w->h_ppath.dentry);
11520+ if (elm && d_is_negative(infopath.dentry)) {
11521+ err = vfsub_create(h_dir, &infopath, 0600, /*want_excl*/true);
11522+ AuTraceErr(err);
11523+ if (unlikely(err))
11524+ goto out_dput;
11525+ elm->created = 1;
11526+ elm->info_dentry = dget(infopath.dentry);
11527+ }
11528+
11529+ infofile = vfsub_dentry_open(&infopath, O_RDWR);
11530+ AuTraceErrPtr(infofile);
11531+ if (IS_ERR(infofile)) {
11532+ err = PTR_ERR(infofile);
11533+ goto out_dput;
11534+ }
11535+
11536+ h_inode = d_inode(infopath.dentry);
11537+ if (elm && i_size_read(h_inode)) {
11538+ h_inode = d_inode(w->h_dentry);
11539+ elm->info_last = au_drinfo_read_k(infofile, h_inode->i_ino);
11540+ AuTraceErrPtr(elm->info_last);
11541+ if (IS_ERR(elm->info_last)) {
11542+ err = PTR_ERR(elm->info_last);
11543+ elm->info_last = NULL;
11544+ AuDebugOn(elm->info_dentry);
11545+ goto out_fput;
11546+ }
11547+ }
11548+
11549+ if (elm && w->renameback) {
11550+ delegated = NULL;
11551+ err = vfsub_unlink(h_dir, &infopath, &delegated, /*force*/0);
11552+ AuTraceErr(err);
11553+ if (unlikely(err == -EWOULDBLOCK))
11554+ iput(delegated);
11555+ goto out_fput;
11556+ }
11557+
11558+ pos = 0;
11559+ qname = &w->h_dentry->d_name;
11560+ len = sizeof(*w->fdata) + qname->len;
11561+ if (!elm)
11562+ len = sizeof(*w->fdata) + w->fdata->drinfo.oldnamelen;
11563+ ssz = vfsub_write_k(infofile, w->fdata, len, &pos);
11564+ if (ssz == len) {
11565+ AuDbg("hi%llu, %.*s\n", w->fdata->drinfo.ino,
11566+ w->fdata->drinfo.oldnamelen, w->fdata->drinfo.oldname);
11567+ goto out_fput; /* success */
11568+ } else {
11569+ err = -EIO;
11570+ if (ssz < 0)
11571+ err = ssz;
11572+ /* the caller should revert it using @elm */
11573+ }
11574+
11575+out_fput:
11576+ fput(infofile);
11577+out_dput:
11578+ dput(infopath.dentry);
11579+out:
11580+ AuTraceErr(err);
11581+ return err;
11582+}
11583+
11584+struct au_call_drinfo_do_store_args {
11585+ int *errp;
11586+ struct au_drinfo_store *w;
11587+ struct au_drinfo_rev_elm *elm;
11588+};
11589+
11590+static void au_call_drinfo_do_store(void *args)
11591+{
11592+ struct au_call_drinfo_do_store_args *a = args;
11593+
11594+ *a->errp = au_drinfo_do_store(a->w, a->elm);
11595+}
11596+
11597+static int au_drinfo_store_sio(struct au_drinfo_store *w,
11598+ struct au_drinfo_rev_elm *elm)
11599+{
11600+ int err, wkq_err;
11601+
11602+ if (w->no_sio)
11603+ err = au_drinfo_do_store(w, elm);
11604+ else {
11605+ struct au_call_drinfo_do_store_args a = {
11606+ .errp = &err,
11607+ .w = w,
11608+ .elm = elm
11609+ };
11610+ wkq_err = au_wkq_wait(au_call_drinfo_do_store, &a);
11611+ if (unlikely(wkq_err))
11612+ err = wkq_err;
11613+ }
11614+ AuTraceErr(err);
11615+
11616+ return err;
11617+}
11618+
11619+static int au_drinfo_store_work_init(struct au_drinfo_store *w,
11620+ aufs_bindex_t btgt)
11621+{
11622+ int err;
11623+
11624+ memset(w, 0, sizeof(*w));
11625+ w->allocated = roundup_pow_of_two(sizeof(*w->fdata) + 40);
11626+ strcpy(w->whname, AUFS_WH_DR_INFO_PFX);
11627+ w->infoname = w->whname + sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11628+ w->infonamelen = sizeof(w->whname) - sizeof(AUFS_WH_DR_INFO_PFX);
11629+ w->btgt = btgt;
11630+ w->no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
11631+
11632+ err = -ENOMEM;
11633+ w->fdata = kcalloc(1, w->allocated, GFP_NOFS);
11634+ if (unlikely(!w->fdata)) {
11635+ AuTraceErr(err);
11636+ goto out;
11637+ }
11638+ w->fdata->magic = (__force uint32_t)htonl(AUFS_DRINFO_MAGIC_V1);
11639+ err = 0;
11640+
11641+out:
11642+ return err;
11643+}
11644+
11645+static void au_drinfo_store_work_fin(struct au_drinfo_store *w)
11646+{
9f237c51 11647+ au_kfree_rcu(w->fdata);
8b6a4947
AM
11648+}
11649+
11650+static void au_drinfo_store_rev(struct au_drinfo_rev *rev,
11651+ struct au_drinfo_store *w)
11652+{
11653+ struct au_drinfo_rev_elm *elm;
11654+ struct inode *h_dir, *delegated;
11655+ int err, nelm;
11656+ struct path infopath = {
11657+ .mnt = w->h_ppath.mnt
11658+ };
11659+
11660+ h_dir = d_inode(w->h_ppath.dentry);
11661+ IMustLock(h_dir);
11662+
11663+ err = 0;
11664+ elm = rev->elm;
11665+ for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11666+ AuDebugOn(elm->created && elm->info_last);
11667+ if (elm->created) {
11668+ AuDbg("here\n");
11669+ delegated = NULL;
11670+ infopath.dentry = elm->info_dentry;
11671+ err = vfsub_unlink(h_dir, &infopath, &delegated,
11672+ !w->no_sio);
11673+ AuTraceErr(err);
11674+ if (unlikely(err == -EWOULDBLOCK))
11675+ iput(delegated);
11676+ dput(elm->info_dentry);
11677+ } else if (elm->info_last) {
11678+ AuDbg("here\n");
11679+ w->fdata->drinfo = *elm->info_last;
11680+ memcpy(w->fdata->drinfo.oldname,
11681+ elm->info_last->oldname,
11682+ elm->info_last->oldnamelen);
11683+ err = au_drinfo_store_sio(w, /*elm*/NULL);
9f237c51 11684+ au_kfree_rcu(elm->info_last);
8b6a4947
AM
11685+ }
11686+ if (unlikely(err))
11687+ AuIOErr("%d, %s\n", err, w->whname);
11688+ /* go on even if err */
11689+ }
11690+}
11691+
11692+/* caller has to call au_dr_rename_fin() later */
11693+static int au_drinfo_store(struct dentry *dentry, aufs_bindex_t btgt,
11694+ struct qstr *dst_name, void *_rev)
11695+{
11696+ int err, sz, nelm;
11697+ aufs_bindex_t bindex, btail;
11698+ struct au_drinfo_store work;
11699+ struct au_drinfo_rev *rev, **p;
11700+ struct au_drinfo_rev_elm *elm;
11701+ struct super_block *sb;
11702+ struct au_branch *br;
11703+ struct au_hinode *hdir;
11704+
11705+ err = au_drinfo_store_work_init(&work, btgt);
11706+ AuTraceErr(err);
11707+ if (unlikely(err))
11708+ goto out;
11709+
11710+ err = -ENOMEM;
11711+ btail = au_dbtaildir(dentry);
11712+ nelm = btail - btgt;
11713+ sz = sizeof(*rev) + sizeof(*elm) * nelm;
11714+ rev = kcalloc(1, sz, GFP_NOFS);
11715+ if (unlikely(!rev)) {
11716+ AuTraceErr(err);
11717+ goto out_args;
11718+ }
11719+ rev->nelm = nelm;
11720+ elm = rev->elm;
11721+ p = _rev;
11722+ *p = rev;
11723+
11724+ err = 0;
11725+ sb = dentry->d_sb;
11726+ work.h_ppath.dentry = au_h_dptr(dentry, btgt);
11727+ work.h_ppath.mnt = au_sbr_mnt(sb, btgt);
11728+ hdir = au_hi(d_inode(dentry), btgt);
11729+ au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11730+ for (bindex = btgt + 1; bindex <= btail; bindex++, elm++) {
11731+ work.h_dentry = au_h_dptr(dentry, bindex);
11732+ if (!work.h_dentry)
11733+ continue;
11734+
11735+ err = au_drinfo_construct(&work.fdata, work.h_dentry,
11736+ &work.allocated);
11737+ AuTraceErr(err);
11738+ if (unlikely(err))
11739+ break;
11740+
11741+ work.renameback = au_qstreq(&work.h_dentry->d_name, dst_name);
11742+ br = au_sbr(sb, bindex);
11743+ work.whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11744+ work.whnamelen += au_drinfo_name(br, work.infoname,
11745+ work.infonamelen);
11746+ AuDbg("whname %.*s, i%llu, %.*s\n",
11747+ work.whnamelen, work.whname,
11748+ be64_to_cpu((__force __be64)work.fdata->drinfo.ino),
11749+ work.fdata->drinfo.oldnamelen,
11750+ work.fdata->drinfo.oldname);
11751+
11752+ err = au_drinfo_store_sio(&work, elm);
11753+ AuTraceErr(err);
11754+ if (unlikely(err))
11755+ break;
11756+ }
11757+ if (unlikely(err)) {
11758+ /* revert all drinfo */
11759+ au_drinfo_store_rev(rev, &work);
9f237c51 11760+ au_kfree_try_rcu(rev);
8b6a4947
AM
11761+ *p = NULL;
11762+ }
11763+ au_hn_inode_unlock(hdir);
11764+
11765+out_args:
11766+ au_drinfo_store_work_fin(&work);
11767+out:
11768+ return err;
11769+}
11770+
11771+/* ---------------------------------------------------------------------- */
11772+
11773+int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
11774+ struct qstr *dst_name, void *_rev)
11775+{
11776+ int err, already;
11777+ ino_t ino;
11778+ struct super_block *sb;
11779+ struct au_branch *br;
11780+ struct au_dr_br *dr;
11781+ struct dentry *h_dentry;
11782+ struct inode *h_inode;
11783+ struct au_dr_hino *ent;
11784+ struct au_drinfo_rev *rev, **p;
11785+
11786+ AuDbg("bindex %d\n", bindex);
11787+
11788+ err = -ENOMEM;
11789+ ent = kmalloc(sizeof(*ent), GFP_NOFS);
11790+ if (unlikely(!ent))
11791+ goto out;
11792+
11793+ sb = src->d_sb;
11794+ br = au_sbr(sb, bindex);
11795+ dr = &br->br_dirren;
11796+ h_dentry = au_h_dptr(src, bindex);
11797+ h_inode = d_inode(h_dentry);
11798+ ino = h_inode->i_ino;
11799+ ent->dr_h_ino = ino;
11800+ already = au_dr_hino_test_add(dr, ino, ent);
11801+ AuDbg("b%d, hi%llu, already %d\n",
11802+ bindex, (unsigned long long)ino, already);
11803+
11804+ err = au_drinfo_store(src, bindex, dst_name, _rev);
11805+ AuTraceErr(err);
11806+ if (!err) {
11807+ p = _rev;
11808+ rev = *p;
11809+ rev->already = already;
11810+ goto out; /* success */
11811+ }
11812+
11813+ /* revert */
11814+ if (!already)
11815+ au_dr_hino_del(dr, ent);
9f237c51 11816+ au_kfree_rcu(ent);
8b6a4947
AM
11817+
11818+out:
11819+ AuTraceErr(err);
11820+ return err;
11821+}
11822+
11823+void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11824+{
11825+ struct au_drinfo_rev *rev;
11826+ struct au_drinfo_rev_elm *elm;
11827+ int nelm;
11828+
11829+ rev = _rev;
11830+ elm = rev->elm;
11831+ for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11832+ dput(elm->info_dentry);
9f237c51 11833+ au_kfree_rcu(elm->info_last);
8b6a4947 11834+ }
9f237c51 11835+ au_kfree_try_rcu(rev);
8b6a4947
AM
11836+}
11837+
11838+void au_dr_rename_rev(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11839+{
11840+ int err;
11841+ struct au_drinfo_store work;
11842+ struct au_drinfo_rev *rev = _rev;
11843+ struct super_block *sb;
11844+ struct au_branch *br;
11845+ struct inode *h_inode;
11846+ struct au_dr_br *dr;
11847+ struct au_dr_hino *ent;
11848+
11849+ err = au_drinfo_store_work_init(&work, btgt);
11850+ if (unlikely(err))
11851+ goto out;
11852+
11853+ sb = src->d_sb;
11854+ br = au_sbr(sb, btgt);
11855+ work.h_ppath.dentry = au_h_dptr(src, btgt);
11856+ work.h_ppath.mnt = au_br_mnt(br);
11857+ au_drinfo_store_rev(rev, &work);
11858+ au_drinfo_store_work_fin(&work);
11859+ if (rev->already)
11860+ goto out;
11861+
11862+ dr = &br->br_dirren;
11863+ h_inode = d_inode(work.h_ppath.dentry);
11864+ ent = au_dr_hino_find(dr, h_inode->i_ino);
11865+ BUG_ON(!ent);
11866+ au_dr_hino_del(dr, ent);
9f237c51 11867+ au_kfree_rcu(ent);
8b6a4947
AM
11868+
11869+out:
9f237c51 11870+ au_kfree_try_rcu(rev);
8b6a4947
AM
11871+ if (unlikely(err))
11872+ pr_err("failed to remove dirren info\n");
11873+}
11874+
11875+/* ---------------------------------------------------------------------- */
11876+
11877+static struct au_drinfo *au_drinfo_do_load(struct path *h_ppath,
11878+ char *whname, int whnamelen,
11879+ struct dentry **info_dentry)
11880+{
11881+ struct au_drinfo *drinfo;
11882+ struct file *f;
11883+ struct inode *h_dir;
11884+ struct path infopath;
11885+ int unlocked;
11886+
11887+ AuDbg("%pd/%.*s\n", h_ppath->dentry, whnamelen, whname);
11888+
11889+ *info_dentry = NULL;
11890+ drinfo = NULL;
11891+ unlocked = 0;
11892+ h_dir = d_inode(h_ppath->dentry);
be118d29 11893+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8b6a4947
AM
11894+ infopath.dentry = vfsub_lookup_one_len(whname, h_ppath->dentry,
11895+ whnamelen);
11896+ if (IS_ERR(infopath.dentry)) {
11897+ drinfo = (void *)infopath.dentry;
11898+ goto out;
11899+ }
11900+
11901+ if (d_is_negative(infopath.dentry))
11902+ goto out_dput; /* success */
11903+
11904+ infopath.mnt = h_ppath->mnt;
11905+ f = vfsub_dentry_open(&infopath, O_RDONLY);
11906+ inode_unlock_shared(h_dir);
11907+ unlocked = 1;
11908+ if (IS_ERR(f)) {
11909+ drinfo = (void *)f;
11910+ goto out_dput;
11911+ }
11912+
11913+ drinfo = au_drinfo_read_k(f, /*h_ino*/0);
11914+ if (IS_ERR_OR_NULL(drinfo))
11915+ goto out_fput;
11916+
11917+ AuDbg("oldname %.*s\n", drinfo->oldnamelen, drinfo->oldname);
11918+ *info_dentry = dget(infopath.dentry); /* keep it alive */
11919+
11920+out_fput:
11921+ fput(f);
11922+out_dput:
11923+ dput(infopath.dentry);
11924+out:
11925+ if (!unlocked)
11926+ inode_unlock_shared(h_dir);
11927+ AuTraceErrPtr(drinfo);
11928+ return drinfo;
11929+}
11930+
11931+struct au_drinfo_do_load_args {
11932+ struct au_drinfo **drinfop;
11933+ struct path *h_ppath;
11934+ char *whname;
11935+ int whnamelen;
11936+ struct dentry **info_dentry;
11937+};
11938+
11939+static void au_call_drinfo_do_load(void *args)
11940+{
11941+ struct au_drinfo_do_load_args *a = args;
11942+
11943+ *a->drinfop = au_drinfo_do_load(a->h_ppath, a->whname, a->whnamelen,
11944+ a->info_dentry);
11945+}
11946+
11947+struct au_drinfo_load {
11948+ struct path h_ppath;
11949+ struct qstr *qname;
11950+ unsigned char no_sio;
11951+
11952+ aufs_bindex_t ninfo;
11953+ struct au_drinfo **drinfo;
11954+};
11955+
11956+static int au_drinfo_load(struct au_drinfo_load *w, aufs_bindex_t bindex,
11957+ struct au_branch *br)
11958+{
11959+ int err, wkq_err, whnamelen, e;
11960+ char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ]
11961+ = AUFS_WH_DR_INFO_PFX;
11962+ struct au_drinfo *drinfo;
11963+ struct qstr oldname;
11964+ struct inode *h_dir, *delegated;
11965+ struct dentry *info_dentry;
11966+ struct path infopath;
11967+
11968+ whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11969+ whnamelen += au_drinfo_name(br, whname + whnamelen,
11970+ sizeof(whname) - whnamelen);
11971+ if (w->no_sio)
11972+ drinfo = au_drinfo_do_load(&w->h_ppath, whname, whnamelen,
11973+ &info_dentry);
11974+ else {
11975+ struct au_drinfo_do_load_args args = {
11976+ .drinfop = &drinfo,
11977+ .h_ppath = &w->h_ppath,
11978+ .whname = whname,
11979+ .whnamelen = whnamelen,
11980+ .info_dentry = &info_dentry
11981+ };
11982+ wkq_err = au_wkq_wait(au_call_drinfo_do_load, &args);
11983+ if (unlikely(wkq_err))
11984+ drinfo = ERR_PTR(wkq_err);
11985+ }
11986+ err = PTR_ERR(drinfo);
11987+ if (IS_ERR_OR_NULL(drinfo))
11988+ goto out;
11989+
11990+ err = 0;
11991+ oldname.len = drinfo->oldnamelen;
11992+ oldname.name = drinfo->oldname;
11993+ if (au_qstreq(w->qname, &oldname)) {
11994+ /* the name is renamed back */
9f237c51 11995+ au_kfree_rcu(drinfo);
8b6a4947
AM
11996+ drinfo = NULL;
11997+
11998+ infopath.dentry = info_dentry;
11999+ infopath.mnt = w->h_ppath.mnt;
12000+ h_dir = d_inode(w->h_ppath.dentry);
12001+ delegated = NULL;
12002+ inode_lock_nested(h_dir, AuLsc_I_PARENT);
12003+ e = vfsub_unlink(h_dir, &infopath, &delegated, !w->no_sio);
12004+ inode_unlock(h_dir);
12005+ if (unlikely(e))
12006+ AuIOErr("ignored %d, %pd2\n", e, &infopath.dentry);
12007+ if (unlikely(e == -EWOULDBLOCK))
12008+ iput(delegated);
12009+ }
9f237c51 12010+ au_kfree_rcu(w->drinfo[bindex]);
8b6a4947
AM
12011+ w->drinfo[bindex] = drinfo;
12012+ dput(info_dentry);
12013+
12014+out:
12015+ AuTraceErr(err);
12016+ return err;
12017+}
12018+
12019+/* ---------------------------------------------------------------------- */
12020+
12021+static void au_dr_lkup_free(struct au_drinfo **drinfo, int n)
12022+{
12023+ struct au_drinfo **p = drinfo;
12024+
12025+ while (n-- > 0)
9f237c51
AM
12026+ au_kfree_rcu(*drinfo++);
12027+ au_kfree_try_rcu(p);
8b6a4947
AM
12028+}
12029+
12030+int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12031+ aufs_bindex_t btgt)
12032+{
12033+ int err, ninfo;
12034+ struct au_drinfo_load w;
12035+ aufs_bindex_t bindex, bbot;
12036+ struct au_branch *br;
12037+ struct inode *h_dir;
12038+ struct au_dr_hino *ent;
12039+ struct super_block *sb;
12040+
12041+ AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
12042+ AuLNPair(&dentry->d_name), AuLNPair(&lkup->dirren.dr_name),
12043+ AuLNPair(&lkup->whname), btgt);
12044+
12045+ sb = dentry->d_sb;
12046+ bbot = au_sbbot(sb);
12047+ w.ninfo = bbot + 1;
12048+ if (!lkup->dirren.drinfo) {
12049+ lkup->dirren.drinfo = kcalloc(w.ninfo,
12050+ sizeof(*lkup->dirren.drinfo),
12051+ GFP_NOFS);
12052+ if (unlikely(!lkup->dirren.drinfo)) {
12053+ err = -ENOMEM;
12054+ goto out;
12055+ }
12056+ lkup->dirren.ninfo = w.ninfo;
12057+ }
12058+ w.drinfo = lkup->dirren.drinfo;
12059+ w.no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
12060+ w.h_ppath.dentry = au_h_dptr(dentry, btgt);
12061+ AuDebugOn(!w.h_ppath.dentry);
12062+ w.h_ppath.mnt = au_sbr_mnt(sb, btgt);
12063+ w.qname = &dentry->d_name;
12064+
12065+ ninfo = 0;
12066+ for (bindex = btgt + 1; bindex <= bbot; bindex++) {
12067+ br = au_sbr(sb, bindex);
12068+ err = au_drinfo_load(&w, bindex, br);
12069+ if (unlikely(err))
12070+ goto out_free;
12071+ if (w.drinfo[bindex])
12072+ ninfo++;
12073+ }
12074+ if (!ninfo) {
12075+ br = au_sbr(sb, btgt);
12076+ h_dir = d_inode(w.h_ppath.dentry);
12077+ ent = au_dr_hino_find(&br->br_dirren, h_dir->i_ino);
12078+ AuDebugOn(!ent);
12079+ au_dr_hino_del(&br->br_dirren, ent);
9f237c51 12080+ au_kfree_rcu(ent);
8b6a4947
AM
12081+ }
12082+ goto out; /* success */
12083+
12084+out_free:
12085+ au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12086+ lkup->dirren.ninfo = 0;
12087+ lkup->dirren.drinfo = NULL;
12088+out:
12089+ AuTraceErr(err);
12090+ return err;
12091+}
12092+
12093+void au_dr_lkup_fin(struct au_do_lookup_args *lkup)
12094+{
12095+ au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12096+}
12097+
12098+int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt)
12099+{
12100+ int err;
12101+ struct au_drinfo *drinfo;
12102+
12103+ err = 0;
12104+ if (!lkup->dirren.drinfo)
12105+ goto out;
43982f53
AM
12106+ AuDebugOn(lkup->dirren.ninfo <= btgt);
12107+ drinfo = lkup->dirren.drinfo[btgt];
8b6a4947
AM
12108+ if (!drinfo)
12109+ goto out;
12110+
9f237c51 12111+ au_kfree_try_rcu(lkup->whname.name);
8b6a4947
AM
12112+ lkup->whname.name = NULL;
12113+ lkup->dirren.dr_name.len = drinfo->oldnamelen;
12114+ lkup->dirren.dr_name.name = drinfo->oldname;
12115+ lkup->name = &lkup->dirren.dr_name;
12116+ err = au_wh_name_alloc(&lkup->whname, lkup->name);
12117+ if (!err)
12118+ AuDbg("name %.*s, whname %.*s, b%d\n",
12119+ AuLNPair(lkup->name), AuLNPair(&lkup->whname),
12120+ btgt);
12121+
12122+out:
12123+ AuTraceErr(err);
12124+ return err;
12125+}
12126+
12127+int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12128+ ino_t h_ino)
12129+{
12130+ int match;
12131+ struct au_drinfo *drinfo;
12132+
12133+ match = 1;
12134+ if (!lkup->dirren.drinfo)
12135+ goto out;
43982f53
AM
12136+ AuDebugOn(lkup->dirren.ninfo <= bindex);
12137+ drinfo = lkup->dirren.drinfo[bindex];
8b6a4947
AM
12138+ if (!drinfo)
12139+ goto out;
12140+
12141+ match = (drinfo->ino == h_ino);
12142+ AuDbg("match %d\n", match);
12143+
12144+out:
12145+ return match;
12146+}
12147+
12148+/* ---------------------------------------------------------------------- */
12149+
12150+int au_dr_opt_set(struct super_block *sb)
12151+{
12152+ int err;
12153+ aufs_bindex_t bindex, bbot;
12154+ struct au_branch *br;
12155+
12156+ err = 0;
12157+ bbot = au_sbbot(sb);
12158+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
12159+ br = au_sbr(sb, bindex);
12160+ err = au_dr_hino(sb, bindex, /*br*/NULL, &br->br_path);
12161+ }
12162+
12163+ return err;
12164+}
12165+
12166+int au_dr_opt_flush(struct super_block *sb)
12167+{
12168+ int err;
12169+ aufs_bindex_t bindex, bbot;
12170+ struct au_branch *br;
12171+
12172+ err = 0;
12173+ bbot = au_sbbot(sb);
12174+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
12175+ br = au_sbr(sb, bindex);
12176+ if (au_br_writable(br->br_perm))
12177+ err = au_dr_hino(sb, bindex, /*br*/NULL, /*path*/NULL);
12178+ }
12179+
12180+ return err;
12181+}
12182+
12183+int au_dr_opt_clr(struct super_block *sb, int no_flush)
12184+{
12185+ int err;
12186+ aufs_bindex_t bindex, bbot;
12187+ struct au_branch *br;
12188+
12189+ err = 0;
12190+ if (!no_flush) {
12191+ err = au_dr_opt_flush(sb);
12192+ if (unlikely(err))
12193+ goto out;
12194+ }
12195+
12196+ bbot = au_sbbot(sb);
12197+ for (bindex = 0; bindex <= bbot; bindex++) {
12198+ br = au_sbr(sb, bindex);
12199+ au_dr_hino_free(&br->br_dirren);
12200+ }
12201+
12202+out:
12203+ return err;
12204+}
12205diff -urN /usr/share/empty/fs/aufs/dirren.h linux/fs/aufs/dirren.h
eca34b5c 12206--- /usr/share/empty/fs/aufs/dirren.h 1970-01-01 01:00:00.000000000 +0100
46016270 12207+++ linux/fs/aufs/dirren.h 2021-02-24 13:33:42.741013619 +0100
062440b3
AM
12208@@ -0,0 +1,140 @@
12209+/* SPDX-License-Identifier: GPL-2.0 */
8b6a4947 12210+/*
d58c55f2 12211+ * Copyright (C) 2017-2020 Junjiro R. Okajima
8b6a4947
AM
12212+ *
12213+ * This program, aufs is free software; you can redistribute it and/or modify
12214+ * it under the terms of the GNU General Public License as published by
12215+ * the Free Software Foundation; either version 2 of the License, or
12216+ * (at your option) any later version.
12217+ *
12218+ * This program is distributed in the hope that it will be useful,
12219+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12220+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12221+ * GNU General Public License for more details.
12222+ *
12223+ * You should have received a copy of the GNU General Public License
12224+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
12225+ */
12226+
12227+/*
12228+ * renamed dir info
12229+ */
12230+
12231+#ifndef __AUFS_DIRREN_H__
12232+#define __AUFS_DIRREN_H__
12233+
12234+#ifdef __KERNEL__
12235+
12236+#include <linux/dcache.h>
12237+#include <linux/statfs.h>
12238+#include <linux/uuid.h>
12239+#include "hbl.h"
12240+
12241+#define AuDirren_NHASH 100
12242+
12243+#ifdef CONFIG_AUFS_DIRREN
12244+enum au_brid_type {
12245+ AuBrid_Unset,
12246+ AuBrid_UUID,
12247+ AuBrid_FSID,
12248+ AuBrid_DEV
12249+};
12250+
12251+struct au_dr_brid {
12252+ enum au_brid_type type;
12253+ union {
12254+ uuid_t uuid; /* unimplemented yet */
12255+ fsid_t fsid;
12256+ dev_t dev;
12257+ };
12258+};
12259+
12260+/* 20 is the max digits length of ulong 64 */
12261+/* brid-type "_" uuid "_" inum */
12262+#define AUFS_DIRREN_FNAME_SZ (1 + 1 + UUID_STRING_LEN + 20)
12263+#define AUFS_DIRREN_ENV_VAL_SZ (AUFS_DIRREN_FNAME_SZ + 1 + 20)
12264+
12265+struct au_dr_hino {
12266+ struct hlist_bl_node dr_hnode;
12267+ ino_t dr_h_ino;
12268+};
12269+
12270+struct au_dr_br {
12271+ struct hlist_bl_head dr_h_ino[AuDirren_NHASH];
12272+ struct au_dr_brid dr_brid;
12273+};
12274+
12275+struct au_dr_lookup {
12276+ /* dr_name is pointed by struct au_do_lookup_args.name */
12277+ struct qstr dr_name; /* subset of dr_info */
12278+ aufs_bindex_t ninfo;
12279+ struct au_drinfo **drinfo;
12280+};
12281+#else
12282+struct au_dr_hino;
12283+/* empty */
12284+struct au_dr_br { };
12285+struct au_dr_lookup { };
12286+#endif
12287+
12288+/* ---------------------------------------------------------------------- */
12289+
12290+struct au_branch;
12291+struct au_do_lookup_args;
12292+struct au_hinode;
12293+#ifdef CONFIG_AUFS_DIRREN
12294+int au_dr_hino_test_add(struct au_dr_br *dr, ino_t h_ino,
12295+ struct au_dr_hino *add_ent);
12296+void au_dr_hino_free(struct au_dr_br *dr);
12297+int au_dr_br_init(struct super_block *sb, struct au_branch *br,
12298+ const struct path *path);
12299+int au_dr_br_fin(struct super_block *sb, struct au_branch *br);
12300+int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
12301+ struct qstr *dst_name, void *_rev);
12302+void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *rev);
12303+void au_dr_rename_rev(struct dentry *src, aufs_bindex_t bindex, void *rev);
12304+int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12305+ aufs_bindex_t bindex);
12306+int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12307+int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12308+ ino_t h_ino);
12309+void au_dr_lkup_fin(struct au_do_lookup_args *lkup);
12310+int au_dr_opt_set(struct super_block *sb);
12311+int au_dr_opt_flush(struct super_block *sb);
12312+int au_dr_opt_clr(struct super_block *sb, int no_flush);
12313+#else
12314+AuStubInt0(au_dr_hino_test_add, struct au_dr_br *dr, ino_t h_ino,
12315+ struct au_dr_hino *add_ent);
12316+AuStubVoid(au_dr_hino_free, struct au_dr_br *dr);
12317+AuStubInt0(au_dr_br_init, struct super_block *sb, struct au_branch *br,
12318+ const struct path *path);
12319+AuStubInt0(au_dr_br_fin, struct super_block *sb, struct au_branch *br);
12320+AuStubInt0(au_dr_rename, struct dentry *src, aufs_bindex_t bindex,
12321+ struct qstr *dst_name, void *_rev);
12322+AuStubVoid(au_dr_rename_fin, struct dentry *src, aufs_bindex_t btgt, void *rev);
12323+AuStubVoid(au_dr_rename_rev, struct dentry *src, aufs_bindex_t bindex,
12324+ void *rev);
12325+AuStubInt0(au_dr_lkup, struct au_do_lookup_args *lkup, struct dentry *dentry,
12326+ aufs_bindex_t bindex);
12327+AuStubInt0(au_dr_lkup_name, struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12328+AuStubInt0(au_dr_lkup_h_ino, struct au_do_lookup_args *lkup,
12329+ aufs_bindex_t bindex, ino_t h_ino);
12330+AuStubVoid(au_dr_lkup_fin, struct au_do_lookup_args *lkup);
12331+AuStubInt0(au_dr_opt_set, struct super_block *sb);
12332+AuStubInt0(au_dr_opt_flush, struct super_block *sb);
12333+AuStubInt0(au_dr_opt_clr, struct super_block *sb, int no_flush);
12334+#endif
12335+
12336+/* ---------------------------------------------------------------------- */
12337+
12338+#ifdef CONFIG_AUFS_DIRREN
12339+static inline int au_dr_ihash(ino_t h_ino)
12340+{
12341+ return h_ino % AuDirren_NHASH;
12342+}
12343+#else
12344+AuStubInt0(au_dr_ihash, ino_t h_ino);
12345+#endif
12346+
12347+#endif /* __KERNEL__ */
12348+#endif /* __AUFS_DIRREN_H__ */
7f207e10 12349diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
eca34b5c 12350--- /usr/share/empty/fs/aufs/dynop.c 1970-01-01 01:00:00.000000000 +0100
46016270 12351+++ linux/fs/aufs/dynop.c 2021-02-24 13:33:42.744347058 +0100
e37dd06a 12352@@ -0,0 +1,368 @@
cd7a4cd9 12353+// SPDX-License-Identifier: GPL-2.0
1facf9fc 12354+/*
d58c55f2 12355+ * Copyright (C) 2010-2020 Junjiro R. Okajima
1facf9fc 12356+ *
12357+ * This program, aufs is free software; you can redistribute it and/or modify
12358+ * it under the terms of the GNU General Public License as published by
12359+ * the Free Software Foundation; either version 2 of the License, or
12360+ * (at your option) any later version.
dece6358
AM
12361+ *
12362+ * This program is distributed in the hope that it will be useful,
12363+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12364+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12365+ * GNU General Public License for more details.
12366+ *
12367+ * You should have received a copy of the GNU General Public License
523b37e3 12368+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 12369+ */
12370+
12371+/*
4a4d8108 12372+ * dynamically customizable operations for regular files
1facf9fc 12373+ */
12374+
1facf9fc 12375+#include "aufs.h"
12376+
4a4d8108 12377+#define DyPrSym(key) AuDbgSym(key->dk_op.dy_hop)
1facf9fc 12378+
4a4d8108
AM
12379+/*
12380+ * How large will these lists be?
12381+ * Usually just a few elements, 20-30 at most for each, I guess.
12382+ */
8b6a4947 12383+static struct hlist_bl_head dynop[AuDyLast];
4a4d8108 12384+
8b6a4947
AM
12385+static struct au_dykey *dy_gfind_get(struct hlist_bl_head *hbl,
12386+ const void *h_op)
1facf9fc 12387+{
4a4d8108 12388+ struct au_dykey *key, *tmp;
8b6a4947 12389+ struct hlist_bl_node *pos;
1facf9fc 12390+
4a4d8108 12391+ key = NULL;
8b6a4947
AM
12392+ hlist_bl_lock(hbl);
12393+ hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
4a4d8108 12394+ if (tmp->dk_op.dy_hop == h_op) {
83b672a5
AM
12395+ if (kref_get_unless_zero(&tmp->dk_kref))
12396+ key = tmp;
4a4d8108
AM
12397+ break;
12398+ }
8b6a4947 12399+ hlist_bl_unlock(hbl);
4a4d8108
AM
12400+
12401+ return key;
1facf9fc 12402+}
12403+
4a4d8108 12404+static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
1facf9fc 12405+{
4a4d8108
AM
12406+ struct au_dykey **k, *found;
12407+ const void *h_op = key->dk_op.dy_hop;
12408+ int i;
1facf9fc 12409+
4a4d8108
AM
12410+ found = NULL;
12411+ k = br->br_dykey;
12412+ for (i = 0; i < AuBrDynOp; i++)
12413+ if (k[i]) {
12414+ if (k[i]->dk_op.dy_hop == h_op) {
12415+ found = k[i];
12416+ break;
12417+ }
12418+ } else
12419+ break;
12420+ if (!found) {
12421+ spin_lock(&br->br_dykey_lock);
12422+ for (; i < AuBrDynOp; i++)
12423+ if (k[i]) {
12424+ if (k[i]->dk_op.dy_hop == h_op) {
12425+ found = k[i];
12426+ break;
12427+ }
12428+ } else {
12429+ k[i] = key;
12430+ break;
12431+ }
12432+ spin_unlock(&br->br_dykey_lock);
12433+ BUG_ON(i == AuBrDynOp); /* expand the array */
12434+ }
12435+
12436+ return found;
1facf9fc 12437+}
12438+
4a4d8108 12439+/* kref_get() if @key is already added */
8b6a4947 12440+static struct au_dykey *dy_gadd(struct hlist_bl_head *hbl, struct au_dykey *key)
4a4d8108
AM
12441+{
12442+ struct au_dykey *tmp, *found;
8b6a4947 12443+ struct hlist_bl_node *pos;
4a4d8108 12444+ const void *h_op = key->dk_op.dy_hop;
1facf9fc 12445+
4a4d8108 12446+ found = NULL;
8b6a4947
AM
12447+ hlist_bl_lock(hbl);
12448+ hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
4a4d8108 12449+ if (tmp->dk_op.dy_hop == h_op) {
83b672a5
AM
12450+ if (kref_get_unless_zero(&tmp->dk_kref))
12451+ found = tmp;
4a4d8108
AM
12452+ break;
12453+ }
12454+ if (!found)
8b6a4947
AM
12455+ hlist_bl_add_head(&key->dk_hnode, hbl);
12456+ hlist_bl_unlock(hbl);
1facf9fc 12457+
4a4d8108
AM
12458+ if (!found)
12459+ DyPrSym(key);
12460+ return found;
12461+}
12462+
12463+static void dy_free_rcu(struct rcu_head *rcu)
1facf9fc 12464+{
4a4d8108
AM
12465+ struct au_dykey *key;
12466+
12467+ key = container_of(rcu, struct au_dykey, dk_rcu);
12468+ DyPrSym(key);
83b672a5 12469+ kfree(key);
1facf9fc 12470+}
12471+
4a4d8108
AM
12472+static void dy_free(struct kref *kref)
12473+{
12474+ struct au_dykey *key;
8b6a4947 12475+ struct hlist_bl_head *hbl;
1facf9fc 12476+
4a4d8108 12477+ key = container_of(kref, struct au_dykey, dk_kref);
8b6a4947
AM
12478+ hbl = dynop + key->dk_op.dy_type;
12479+ au_hbl_del(&key->dk_hnode, hbl);
4a4d8108
AM
12480+ call_rcu(&key->dk_rcu, dy_free_rcu);
12481+}
12482+
12483+void au_dy_put(struct au_dykey *key)
1facf9fc 12484+{
4a4d8108
AM
12485+ kref_put(&key->dk_kref, dy_free);
12486+}
1facf9fc 12487+
4a4d8108
AM
12488+/* ---------------------------------------------------------------------- */
12489+
12490+#define DyDbgSize(cnt, op) AuDebugOn(cnt != sizeof(op)/sizeof(void *))
12491+
12492+#ifdef CONFIG_AUFS_DEBUG
12493+#define DyDbgDeclare(cnt) unsigned int cnt = 0
4f0767ce 12494+#define DyDbgInc(cnt) do { cnt++; } while (0)
4a4d8108
AM
12495+#else
12496+#define DyDbgDeclare(cnt) do {} while (0)
12497+#define DyDbgInc(cnt) do {} while (0)
12498+#endif
12499+
12500+#define DySet(func, dst, src, h_op, h_sb) do { \
12501+ DyDbgInc(cnt); \
12502+ if (h_op->func) { \
12503+ if (src.func) \
12504+ dst.func = src.func; \
12505+ else \
12506+ AuDbg("%s %s\n", au_sbtype(h_sb), #func); \
12507+ } \
12508+} while (0)
12509+
12510+#define DySetForce(func, dst, src) do { \
12511+ AuDebugOn(!src.func); \
12512+ DyDbgInc(cnt); \
12513+ dst.func = src.func; \
12514+} while (0)
12515+
12516+#define DySetAop(func) \
12517+ DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
12518+#define DySetAopForce(func) \
12519+ DySetForce(func, dyaop->da_op, aufs_aop)
12520+
12521+static void dy_aop(struct au_dykey *key, const void *h_op,
12522+ struct super_block *h_sb __maybe_unused)
12523+{
12524+ struct au_dyaop *dyaop = (void *)key;
12525+ const struct address_space_operations *h_aop = h_op;
12526+ DyDbgDeclare(cnt);
12527+
12528+ AuDbg("%s\n", au_sbtype(h_sb));
12529+
12530+ DySetAop(writepage);
12531+ DySetAopForce(readpage); /* force */
4a4d8108
AM
12532+ DySetAop(writepages);
12533+ DySetAop(set_page_dirty);
12534+ DySetAop(readpages);
e37dd06a 12535+ DySetAop(readahead);
4a4d8108
AM
12536+ DySetAop(write_begin);
12537+ DySetAop(write_end);
12538+ DySetAop(bmap);
12539+ DySetAop(invalidatepage);
12540+ DySetAop(releasepage);
027c5e7a 12541+ DySetAop(freepage);
7e9cd9fe 12542+ /* this one will be changed according to an aufs mount option */
4a4d8108 12543+ DySetAop(direct_IO);
4a4d8108 12544+ DySetAop(migratepage);
e2f27e51
AM
12545+ DySetAop(isolate_page);
12546+ DySetAop(putback_page);
4a4d8108
AM
12547+ DySetAop(launder_page);
12548+ DySetAop(is_partially_uptodate);
392086de 12549+ DySetAop(is_dirty_writeback);
4a4d8108 12550+ DySetAop(error_remove_page);
b4510431
AM
12551+ DySetAop(swap_activate);
12552+ DySetAop(swap_deactivate);
4a4d8108
AM
12553+
12554+ DyDbgSize(cnt, *h_aop);
4a4d8108
AM
12555+}
12556+
4a4d8108
AM
12557+/* ---------------------------------------------------------------------- */
12558+
12559+static void dy_bug(struct kref *kref)
12560+{
12561+ BUG();
12562+}
12563+
12564+static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
12565+{
12566+ struct au_dykey *key, *old;
8b6a4947 12567+ struct hlist_bl_head *hbl;
b752ccd1 12568+ struct op {
4a4d8108 12569+ unsigned int sz;
b752ccd1
AM
12570+ void (*set)(struct au_dykey *key, const void *h_op,
12571+ struct super_block *h_sb __maybe_unused);
12572+ };
12573+ static const struct op a[] = {
4a4d8108
AM
12574+ [AuDy_AOP] = {
12575+ .sz = sizeof(struct au_dyaop),
b752ccd1 12576+ .set = dy_aop
4a4d8108 12577+ }
b752ccd1
AM
12578+ };
12579+ const struct op *p;
4a4d8108 12580+
8b6a4947
AM
12581+ hbl = dynop + op->dy_type;
12582+ key = dy_gfind_get(hbl, op->dy_hop);
4a4d8108
AM
12583+ if (key)
12584+ goto out_add; /* success */
12585+
12586+ p = a + op->dy_type;
12587+ key = kzalloc(p->sz, GFP_NOFS);
12588+ if (unlikely(!key)) {
12589+ key = ERR_PTR(-ENOMEM);
12590+ goto out;
12591+ }
12592+
12593+ key->dk_op.dy_hop = op->dy_hop;
12594+ kref_init(&key->dk_kref);
86dc4139 12595+ p->set(key, op->dy_hop, au_br_sb(br));
8b6a4947 12596+ old = dy_gadd(hbl, key);
4a4d8108 12597+ if (old) {
9f237c51 12598+ au_kfree_rcu(key);
4a4d8108
AM
12599+ key = old;
12600+ }
12601+
12602+out_add:
12603+ old = dy_bradd(br, key);
12604+ if (old)
12605+ /* its ref-count should never be zero here */
12606+ kref_put(&key->dk_kref, dy_bug);
12607+out:
12608+ return key;
12609+}
12610+
12611+/* ---------------------------------------------------------------------- */
12612+/*
acd2b654 12613+ * Aufs prohibits O_DIRECT by default even if the branch supports it.
c1595e42 12614+ * This behaviour is necessary to return an error from open(O_DIRECT) instead
4a4d8108
AM
12615+ * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
12616+ * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
12617+ * See the aufs manual in detail.
4a4d8108
AM
12618+ */
12619+static void dy_adx(struct au_dyaop *dyaop, int do_dx)
12620+{
7e9cd9fe 12621+ if (!do_dx)
4a4d8108 12622+ dyaop->da_op.direct_IO = NULL;
7e9cd9fe 12623+ else
4a4d8108 12624+ dyaop->da_op.direct_IO = aufs_aop.direct_IO;
4a4d8108
AM
12625+}
12626+
12627+static struct au_dyaop *dy_aget(struct au_branch *br,
12628+ const struct address_space_operations *h_aop,
12629+ int do_dx)
12630+{
12631+ struct au_dyaop *dyaop;
12632+ struct au_dynop op;
12633+
12634+ op.dy_type = AuDy_AOP;
12635+ op.dy_haop = h_aop;
12636+ dyaop = (void *)dy_get(&op, br);
12637+ if (IS_ERR(dyaop))
12638+ goto out;
12639+ dy_adx(dyaop, do_dx);
12640+
12641+out:
12642+ return dyaop;
12643+}
12644+
12645+int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12646+ struct inode *h_inode)
12647+{
12648+ int err, do_dx;
12649+ struct super_block *sb;
12650+ struct au_branch *br;
12651+ struct au_dyaop *dyaop;
12652+
12653+ AuDebugOn(!S_ISREG(h_inode->i_mode));
12654+ IiMustWriteLock(inode);
12655+
12656+ sb = inode->i_sb;
12657+ br = au_sbr(sb, bindex);
12658+ do_dx = !!au_opt_test(au_mntflags(sb), DIO);
12659+ dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
12660+ err = PTR_ERR(dyaop);
12661+ if (IS_ERR(dyaop))
12662+ /* unnecessary to call dy_fput() */
12663+ goto out;
12664+
12665+ err = 0;
12666+ inode->i_mapping->a_ops = &dyaop->da_op;
12667+
12668+out:
12669+ return err;
12670+}
12671+
b752ccd1
AM
12672+/*
12673+ * Is it safe to replace a_ops during the inode/file is in operation?
12674+ * Yes, I hope so.
12675+ */
12676+int au_dy_irefresh(struct inode *inode)
12677+{
12678+ int err;
5afbbe0d 12679+ aufs_bindex_t btop;
b752ccd1
AM
12680+ struct inode *h_inode;
12681+
12682+ err = 0;
12683+ if (S_ISREG(inode->i_mode)) {
5afbbe0d
AM
12684+ btop = au_ibtop(inode);
12685+ h_inode = au_h_iptr(inode, btop);
12686+ err = au_dy_iaop(inode, btop, h_inode);
b752ccd1
AM
12687+ }
12688+ return err;
12689+}
12690+
4a4d8108
AM
12691+void au_dy_arefresh(int do_dx)
12692+{
8b6a4947
AM
12693+ struct hlist_bl_head *hbl;
12694+ struct hlist_bl_node *pos;
4a4d8108
AM
12695+ struct au_dykey *key;
12696+
8b6a4947
AM
12697+ hbl = dynop + AuDy_AOP;
12698+ hlist_bl_lock(hbl);
12699+ hlist_bl_for_each_entry(key, pos, hbl, dk_hnode)
4a4d8108 12700+ dy_adx((void *)key, do_dx);
8b6a4947 12701+ hlist_bl_unlock(hbl);
4a4d8108
AM
12702+}
12703+
4a4d8108
AM
12704+/* ---------------------------------------------------------------------- */
12705+
12706+void __init au_dy_init(void)
12707+{
12708+ int i;
12709+
4a4d8108 12710+ for (i = 0; i < AuDyLast; i++)
8b6a4947 12711+ INIT_HLIST_BL_HEAD(dynop + i);
4a4d8108
AM
12712+}
12713+
12714+void au_dy_fin(void)
12715+{
12716+ int i;
12717+
12718+ for (i = 0; i < AuDyLast; i++)
8b6a4947 12719+ WARN_ON(!hlist_bl_empty(dynop + i));
4a4d8108 12720+}
7f207e10 12721diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
eca34b5c 12722--- /usr/share/empty/fs/aufs/dynop.h 1970-01-01 01:00:00.000000000 +0100
46016270 12723+++ linux/fs/aufs/dynop.h 2021-02-24 13:33:42.744347058 +0100
fbc438ed 12724@@ -0,0 +1,77 @@
062440b3 12725+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 12726+/*
d58c55f2 12727+ * Copyright (C) 2010-2020 Junjiro R. Okajima
4a4d8108
AM
12728+ *
12729+ * This program, aufs is free software; you can redistribute it and/or modify
12730+ * it under the terms of the GNU General Public License as published by
12731+ * the Free Software Foundation; either version 2 of the License, or
12732+ * (at your option) any later version.
12733+ *
12734+ * This program is distributed in the hope that it will be useful,
12735+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12736+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12737+ * GNU General Public License for more details.
12738+ *
12739+ * You should have received a copy of the GNU General Public License
523b37e3 12740+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
12741+ */
12742+
12743+/*
12744+ * dynamically customizable operations (for regular files only)
12745+ */
12746+
12747+#ifndef __AUFS_DYNOP_H__
12748+#define __AUFS_DYNOP_H__
12749+
12750+#ifdef __KERNEL__
12751+
7e9cd9fe
AM
12752+#include <linux/fs.h>
12753+#include <linux/kref.h>
4a4d8108 12754+
2cbb1c4b 12755+enum {AuDy_AOP, AuDyLast};
4a4d8108
AM
12756+
12757+struct au_dynop {
12758+ int dy_type;
12759+ union {
12760+ const void *dy_hop;
12761+ const struct address_space_operations *dy_haop;
4a4d8108
AM
12762+ };
12763+};
12764+
12765+struct au_dykey {
12766+ union {
8b6a4947 12767+ struct hlist_bl_node dk_hnode;
4a4d8108
AM
12768+ struct rcu_head dk_rcu;
12769+ };
12770+ struct au_dynop dk_op;
12771+
12772+ /*
12773+ * during I am in the branch local array, kref is gotten. when the
12774+ * branch is removed, kref is put.
12775+ */
12776+ struct kref dk_kref;
12777+};
12778+
12779+/* stop unioning since their sizes are very different from each other */
12780+struct au_dyaop {
12781+ struct au_dykey da_key;
12782+ struct address_space_operations da_op; /* not const */
4a4d8108 12783+};
fbc438ed
JR
12784+/* make sure that 'struct au_dykey *' can be any type */
12785+static_assert(!offsetof(struct au_dyaop, da_key));
4a4d8108 12786+
4a4d8108
AM
12787+/* ---------------------------------------------------------------------- */
12788+
12789+/* dynop.c */
12790+struct au_branch;
12791+void au_dy_put(struct au_dykey *key);
12792+int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12793+ struct inode *h_inode);
b752ccd1 12794+int au_dy_irefresh(struct inode *inode);
4a4d8108 12795+void au_dy_arefresh(int do_dio);
4a4d8108
AM
12796+
12797+void __init au_dy_init(void);
12798+void au_dy_fin(void);
12799+
4a4d8108
AM
12800+#endif /* __KERNEL__ */
12801+#endif /* __AUFS_DYNOP_H__ */
7f207e10 12802diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
eca34b5c 12803--- /usr/share/empty/fs/aufs/export.c 1970-01-01 01:00:00.000000000 +0100
46016270 12804+++ linux/fs/aufs/export.c 2021-02-24 13:33:42.744347058 +0100
e37dd06a 12805@@ -0,0 +1,837 @@
cd7a4cd9 12806+// SPDX-License-Identifier: GPL-2.0
4a4d8108 12807+/*
d58c55f2 12808+ * Copyright (C) 2005-2020 Junjiro R. Okajima
4a4d8108
AM
12809+ *
12810+ * This program, aufs is free software; you can redistribute it and/or modify
12811+ * it under the terms of the GNU General Public License as published by
12812+ * the Free Software Foundation; either version 2 of the License, or
12813+ * (at your option) any later version.
12814+ *
12815+ * This program is distributed in the hope that it will be useful,
12816+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12817+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12818+ * GNU General Public License for more details.
12819+ *
12820+ * You should have received a copy of the GNU General Public License
523b37e3 12821+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
12822+ */
12823+
12824+/*
12825+ * export via nfs
12826+ */
12827+
12828+#include <linux/exportfs.h>
7eafdf33 12829+#include <linux/fs_struct.h>
4a4d8108
AM
12830+#include <linux/namei.h>
12831+#include <linux/nsproxy.h>
12832+#include <linux/random.h>
12833+#include <linux/writeback.h>
12834+#include "aufs.h"
12835+
12836+union conv {
12837+#ifdef CONFIG_AUFS_INO_T_64
12838+ __u32 a[2];
12839+#else
12840+ __u32 a[1];
12841+#endif
12842+ ino_t ino;
12843+};
12844+
12845+static ino_t decode_ino(__u32 *a)
12846+{
12847+ union conv u;
12848+
12849+ BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
12850+ u.a[0] = a[0];
12851+#ifdef CONFIG_AUFS_INO_T_64
12852+ u.a[1] = a[1];
12853+#endif
12854+ return u.ino;
12855+}
12856+
12857+static void encode_ino(__u32 *a, ino_t ino)
12858+{
12859+ union conv u;
12860+
12861+ u.ino = ino;
12862+ a[0] = u.a[0];
12863+#ifdef CONFIG_AUFS_INO_T_64
12864+ a[1] = u.a[1];
12865+#endif
12866+}
12867+
12868+/* NFS file handle */
12869+enum {
12870+ Fh_br_id,
12871+ Fh_sigen,
12872+#ifdef CONFIG_AUFS_INO_T_64
12873+ /* support 64bit inode number */
12874+ Fh_ino1,
12875+ Fh_ino2,
12876+ Fh_dir_ino1,
12877+ Fh_dir_ino2,
12878+#else
12879+ Fh_ino1,
12880+ Fh_dir_ino1,
12881+#endif
12882+ Fh_igen,
12883+ Fh_h_type,
12884+ Fh_tail,
12885+
12886+ Fh_ino = Fh_ino1,
12887+ Fh_dir_ino = Fh_dir_ino1
12888+};
12889+
12890+static int au_test_anon(struct dentry *dentry)
12891+{
027c5e7a 12892+ /* note: read d_flags without d_lock */
4a4d8108
AM
12893+ return !!(dentry->d_flags & DCACHE_DISCONNECTED);
12894+}
12895+
a2a7ad62
AM
12896+int au_test_nfsd(void)
12897+{
12898+ int ret;
12899+ struct task_struct *tsk = current;
12900+ char comm[sizeof(tsk->comm)];
12901+
12902+ ret = 0;
12903+ if (tsk->flags & PF_KTHREAD) {
12904+ get_task_comm(comm, tsk);
12905+ ret = !strcmp(comm, "nfsd");
12906+ }
12907+
12908+ return ret;
12909+}
12910+
4a4d8108
AM
12911+/* ---------------------------------------------------------------------- */
12912+/* inode generation external table */
12913+
b752ccd1 12914+void au_xigen_inc(struct inode *inode)
4a4d8108 12915+{
4a4d8108
AM
12916+ loff_t pos;
12917+ ssize_t sz;
12918+ __u32 igen;
12919+ struct super_block *sb;
12920+ struct au_sbinfo *sbinfo;
12921+
4a4d8108 12922+ sb = inode->i_sb;
b752ccd1 12923+ AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
1facf9fc 12924+
b752ccd1 12925+ sbinfo = au_sbi(sb);
1facf9fc 12926+ pos = inode->i_ino;
12927+ pos *= sizeof(igen);
12928+ igen = inode->i_generation + 1;
e37dd06a 12929+ sz = xino_fwrite(sbinfo->si_xigen, &igen, sizeof(igen), &pos);
1facf9fc 12930+ if (sz == sizeof(igen))
b752ccd1 12931+ return; /* success */
1facf9fc 12932+
b752ccd1 12933+ if (unlikely(sz >= 0))
1facf9fc 12934+ AuIOErr("xigen error (%zd)\n", sz);
1facf9fc 12935+}
12936+
12937+int au_xigen_new(struct inode *inode)
12938+{
12939+ int err;
12940+ loff_t pos;
12941+ ssize_t sz;
12942+ struct super_block *sb;
12943+ struct au_sbinfo *sbinfo;
12944+ struct file *file;
12945+
12946+ err = 0;
12947+ /* todo: dirty, at mount time */
12948+ if (inode->i_ino == AUFS_ROOT_INO)
12949+ goto out;
12950+ sb = inode->i_sb;
dece6358 12951+ SiMustAnyLock(sb);
1facf9fc 12952+ if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
12953+ goto out;
12954+
12955+ err = -EFBIG;
12956+ pos = inode->i_ino;
12957+ if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
12958+ AuIOErr1("too large i%lld\n", pos);
12959+ goto out;
12960+ }
12961+ pos *= sizeof(inode->i_generation);
12962+
12963+ err = 0;
12964+ sbinfo = au_sbi(sb);
12965+ file = sbinfo->si_xigen;
12966+ BUG_ON(!file);
12967+
c06a8ce3 12968+ if (vfsub_f_size_read(file)
1facf9fc 12969+ < pos + sizeof(inode->i_generation)) {
12970+ inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
e37dd06a 12971+ sz = xino_fwrite(file, &inode->i_generation,
1facf9fc 12972+ sizeof(inode->i_generation), &pos);
12973+ } else
e37dd06a 12974+ sz = xino_fread(file, &inode->i_generation,
1facf9fc 12975+ sizeof(inode->i_generation), &pos);
12976+ if (sz == sizeof(inode->i_generation))
12977+ goto out; /* success */
12978+
12979+ err = sz;
12980+ if (unlikely(sz >= 0)) {
12981+ err = -EIO;
12982+ AuIOErr("xigen error (%zd)\n", sz);
12983+ }
12984+
4f0767ce 12985+out:
1facf9fc 12986+ return err;
12987+}
12988+
062440b3 12989+int au_xigen_set(struct super_block *sb, struct path *path)
1facf9fc 12990+{
12991+ int err;
12992+ struct au_sbinfo *sbinfo;
12993+ struct file *file;
12994+
dece6358
AM
12995+ SiMustWriteLock(sb);
12996+
1facf9fc 12997+ sbinfo = au_sbi(sb);
062440b3 12998+ file = au_xino_create2(sb, path, sbinfo->si_xigen);
1facf9fc 12999+ err = PTR_ERR(file);
13000+ if (IS_ERR(file))
13001+ goto out;
13002+ err = 0;
13003+ if (sbinfo->si_xigen)
13004+ fput(sbinfo->si_xigen);
13005+ sbinfo->si_xigen = file;
13006+
4f0767ce 13007+out:
062440b3 13008+ AuTraceErr(err);
1facf9fc 13009+ return err;
13010+}
13011+
13012+void au_xigen_clr(struct super_block *sb)
13013+{
13014+ struct au_sbinfo *sbinfo;
13015+
dece6358
AM
13016+ SiMustWriteLock(sb);
13017+
1facf9fc 13018+ sbinfo = au_sbi(sb);
13019+ if (sbinfo->si_xigen) {
13020+ fput(sbinfo->si_xigen);
13021+ sbinfo->si_xigen = NULL;
13022+ }
13023+}
13024+
13025+/* ---------------------------------------------------------------------- */
13026+
13027+static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
13028+ ino_t dir_ino)
13029+{
13030+ struct dentry *dentry, *d;
13031+ struct inode *inode;
13032+ unsigned int sigen;
13033+
13034+ dentry = NULL;
13035+ inode = ilookup(sb, ino);
13036+ if (!inode)
13037+ goto out;
13038+
13039+ dentry = ERR_PTR(-ESTALE);
13040+ sigen = au_sigen(sb);
5afbbe0d 13041+ if (unlikely(au_is_bad_inode(inode)
1facf9fc 13042+ || IS_DEADDIR(inode)
537831f9 13043+ || sigen != au_iigen(inode, NULL)))
1facf9fc 13044+ goto out_iput;
13045+
13046+ dentry = NULL;
13047+ if (!dir_ino || S_ISDIR(inode->i_mode))
13048+ dentry = d_find_alias(inode);
13049+ else {
027c5e7a 13050+ spin_lock(&inode->i_lock);
c1595e42 13051+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
027c5e7a 13052+ spin_lock(&d->d_lock);
1facf9fc 13053+ if (!au_test_anon(d)
5527c038 13054+ && d_inode(d->d_parent)->i_ino == dir_ino) {
027c5e7a
AM
13055+ dentry = dget_dlock(d);
13056+ spin_unlock(&d->d_lock);
1facf9fc 13057+ break;
13058+ }
027c5e7a
AM
13059+ spin_unlock(&d->d_lock);
13060+ }
13061+ spin_unlock(&inode->i_lock);
1facf9fc 13062+ }
027c5e7a 13063+ if (unlikely(dentry && au_digen_test(dentry, sigen))) {
2cbb1c4b 13064+ /* need to refresh */
1facf9fc 13065+ dput(dentry);
2cbb1c4b 13066+ dentry = NULL;
1facf9fc 13067+ }
13068+
4f0767ce 13069+out_iput:
1facf9fc 13070+ iput(inode);
4f0767ce 13071+out:
2cbb1c4b 13072+ AuTraceErrPtr(dentry);
1facf9fc 13073+ return dentry;
13074+}
13075+
13076+/* ---------------------------------------------------------------------- */
13077+
13078+/* todo: dirty? */
13079+/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
4a4d8108
AM
13080+
13081+struct au_compare_mnt_args {
13082+ /* input */
13083+ struct super_block *sb;
13084+
13085+ /* output */
13086+ struct vfsmount *mnt;
13087+};
13088+
13089+static int au_compare_mnt(struct vfsmount *mnt, void *arg)
13090+{
13091+ struct au_compare_mnt_args *a = arg;
13092+
13093+ if (mnt->mnt_sb != a->sb)
13094+ return 0;
13095+ a->mnt = mntget(mnt);
13096+ return 1;
13097+}
13098+
1facf9fc 13099+static struct vfsmount *au_mnt_get(struct super_block *sb)
13100+{
4a4d8108 13101+ int err;
7eafdf33 13102+ struct path root;
4a4d8108
AM
13103+ struct au_compare_mnt_args args = {
13104+ .sb = sb
13105+ };
1facf9fc 13106+
7eafdf33 13107+ get_fs_root(current->fs, &root);
523b37e3 13108+ rcu_read_lock();
7eafdf33 13109+ err = iterate_mounts(au_compare_mnt, &args, root.mnt);
523b37e3 13110+ rcu_read_unlock();
7eafdf33 13111+ path_put(&root);
4a4d8108
AM
13112+ AuDebugOn(!err);
13113+ AuDebugOn(!args.mnt);
13114+ return args.mnt;
1facf9fc 13115+}
13116+
13117+struct au_nfsd_si_lock {
4a4d8108 13118+ unsigned int sigen;
027c5e7a 13119+ aufs_bindex_t bindex, br_id;
1facf9fc 13120+ unsigned char force_lock;
13121+};
13122+
027c5e7a
AM
13123+static int si_nfsd_read_lock(struct super_block *sb,
13124+ struct au_nfsd_si_lock *nsi_lock)
1facf9fc 13125+{
027c5e7a 13126+ int err;
1facf9fc 13127+ aufs_bindex_t bindex;
13128+
13129+ si_read_lock(sb, AuLock_FLUSH);
13130+
13131+ /* branch id may be wrapped around */
027c5e7a 13132+ err = 0;
1facf9fc 13133+ bindex = au_br_index(sb, nsi_lock->br_id);
13134+ if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
13135+ goto out; /* success */
13136+
027c5e7a
AM
13137+ err = -ESTALE;
13138+ bindex = -1;
1facf9fc 13139+ if (!nsi_lock->force_lock)
13140+ si_read_unlock(sb);
1facf9fc 13141+
4f0767ce 13142+out:
027c5e7a
AM
13143+ nsi_lock->bindex = bindex;
13144+ return err;
1facf9fc 13145+}
13146+
13147+struct find_name_by_ino {
392086de 13148+ struct dir_context ctx;
1facf9fc 13149+ int called, found;
13150+ ino_t ino;
13151+ char *name;
13152+ int namelen;
13153+};
13154+
13155+static int
392086de
AM
13156+find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
13157+ loff_t offset, u64 ino, unsigned int d_type)
1facf9fc 13158+{
392086de
AM
13159+ struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
13160+ ctx);
1facf9fc 13161+
13162+ a->called++;
13163+ if (a->ino != ino)
13164+ return 0;
13165+
13166+ memcpy(a->name, name, namelen);
13167+ a->namelen = namelen;
13168+ a->found = 1;
13169+ return 1;
13170+}
13171+
13172+static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
13173+ struct au_nfsd_si_lock *nsi_lock)
13174+{
13175+ struct dentry *dentry, *parent;
13176+ struct file *file;
13177+ struct inode *dir;
392086de
AM
13178+ struct find_name_by_ino arg = {
13179+ .ctx = {
2000de60 13180+ .actor = find_name_by_ino
392086de
AM
13181+ }
13182+ };
1facf9fc 13183+ int err;
13184+
13185+ parent = path->dentry;
13186+ if (nsi_lock)
13187+ si_read_unlock(parent->d_sb);
4a4d8108 13188+ file = vfsub_dentry_open(path, au_dir_roflags);
1facf9fc 13189+ dentry = (void *)file;
13190+ if (IS_ERR(file))
13191+ goto out;
13192+
13193+ dentry = ERR_PTR(-ENOMEM);
537831f9 13194+ arg.name = (void *)__get_free_page(GFP_NOFS);
1facf9fc 13195+ if (unlikely(!arg.name))
13196+ goto out_file;
13197+ arg.ino = ino;
13198+ arg.found = 0;
13199+ do {
13200+ arg.called = 0;
13201+ /* smp_mb(); */
392086de 13202+ err = vfsub_iterate_dir(file, &arg.ctx);
1facf9fc 13203+ } while (!err && !arg.found && arg.called);
13204+ dentry = ERR_PTR(err);
13205+ if (unlikely(err))
13206+ goto out_name;
1716fcea
AM
13207+ /* instead of ENOENT */
13208+ dentry = ERR_PTR(-ESTALE);
1facf9fc 13209+ if (!arg.found)
13210+ goto out_name;
13211+
b4510431 13212+ /* do not call vfsub_lkup_one() */
5527c038 13213+ dir = d_inode(parent);
febd17d6 13214+ dentry = vfsub_lookup_one_len_unlocked(arg.name, parent, arg.namelen);
1facf9fc 13215+ AuTraceErrPtr(dentry);
13216+ if (IS_ERR(dentry))
13217+ goto out_name;
13218+ AuDebugOn(au_test_anon(dentry));
5527c038 13219+ if (unlikely(d_really_is_negative(dentry))) {
1facf9fc 13220+ dput(dentry);
13221+ dentry = ERR_PTR(-ENOENT);
13222+ }
13223+
4f0767ce 13224+out_name:
1c60b727 13225+ free_page((unsigned long)arg.name);
4f0767ce 13226+out_file:
1facf9fc 13227+ fput(file);
4f0767ce 13228+out:
1facf9fc 13229+ if (unlikely(nsi_lock
13230+ && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
13231+ if (!IS_ERR(dentry)) {
13232+ dput(dentry);
13233+ dentry = ERR_PTR(-ESTALE);
13234+ }
13235+ AuTraceErrPtr(dentry);
13236+ return dentry;
13237+}
13238+
13239+static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
13240+ ino_t dir_ino,
13241+ struct au_nfsd_si_lock *nsi_lock)
13242+{
13243+ struct dentry *dentry;
13244+ struct path path;
13245+
13246+ if (dir_ino != AUFS_ROOT_INO) {
13247+ path.dentry = decode_by_ino(sb, dir_ino, 0);
13248+ dentry = path.dentry;
13249+ if (!path.dentry || IS_ERR(path.dentry))
13250+ goto out;
13251+ AuDebugOn(au_test_anon(path.dentry));
13252+ } else
13253+ path.dentry = dget(sb->s_root);
13254+
13255+ path.mnt = au_mnt_get(sb);
13256+ dentry = au_lkup_by_ino(&path, ino, nsi_lock);
13257+ path_put(&path);
13258+
4f0767ce 13259+out:
1facf9fc 13260+ AuTraceErrPtr(dentry);
13261+ return dentry;
13262+}
13263+
13264+/* ---------------------------------------------------------------------- */
13265+
13266+static int h_acceptable(void *expv, struct dentry *dentry)
13267+{
13268+ return 1;
13269+}
13270+
13271+static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
13272+ char *buf, int len, struct super_block *sb)
13273+{
13274+ char *p;
13275+ int n;
13276+ struct path path;
13277+
13278+ p = d_path(h_rootpath, buf, len);
13279+ if (IS_ERR(p))
13280+ goto out;
13281+ n = strlen(p);
13282+
13283+ path.mnt = h_rootpath->mnt;
13284+ path.dentry = h_parent;
13285+ p = d_path(&path, buf, len);
13286+ if (IS_ERR(p))
13287+ goto out;
13288+ if (n != 1)
13289+ p += n;
13290+
13291+ path.mnt = au_mnt_get(sb);
13292+ path.dentry = sb->s_root;
13293+ p = d_path(&path, buf, len - strlen(p));
13294+ mntput(path.mnt);
13295+ if (IS_ERR(p))
13296+ goto out;
13297+ if (n != 1)
13298+ p[strlen(p)] = '/';
13299+
4f0767ce 13300+out:
1facf9fc 13301+ AuTraceErrPtr(p);
13302+ return p;
13303+}
13304+
13305+static
027c5e7a
AM
13306+struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
13307+ int fh_len, struct au_nfsd_si_lock *nsi_lock)
1facf9fc 13308+{
13309+ struct dentry *dentry, *h_parent, *root;
13310+ struct super_block *h_sb;
13311+ char *pathname, *p;
13312+ struct vfsmount *h_mnt;
13313+ struct au_branch *br;
13314+ int err;
13315+ struct path path;
13316+
027c5e7a 13317+ br = au_sbr(sb, nsi_lock->bindex);
86dc4139 13318+ h_mnt = au_br_mnt(br);
1facf9fc 13319+ h_sb = h_mnt->mnt_sb;
13320+ /* todo: call lower fh_to_dentry()? fh_to_parent()? */
5afbbe0d 13321+ lockdep_off();
1facf9fc 13322+ h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
13323+ fh_len - Fh_tail, fh[Fh_h_type],
13324+ h_acceptable, /*context*/NULL);
5afbbe0d 13325+ lockdep_on();
1facf9fc 13326+ dentry = h_parent;
13327+ if (unlikely(!h_parent || IS_ERR(h_parent))) {
13328+ AuWarn1("%s decode_fh failed, %ld\n",
13329+ au_sbtype(h_sb), PTR_ERR(h_parent));
13330+ goto out;
13331+ }
13332+ dentry = NULL;
13333+ if (unlikely(au_test_anon(h_parent))) {
13334+ AuWarn1("%s decode_fh returned a disconnected dentry\n",
13335+ au_sbtype(h_sb));
13336+ goto out_h_parent;
13337+ }
13338+
13339+ dentry = ERR_PTR(-ENOMEM);
13340+ pathname = (void *)__get_free_page(GFP_NOFS);
13341+ if (unlikely(!pathname))
13342+ goto out_h_parent;
13343+
13344+ root = sb->s_root;
13345+ path.mnt = h_mnt;
13346+ di_read_lock_parent(root, !AuLock_IR);
027c5e7a 13347+ path.dentry = au_h_dptr(root, nsi_lock->bindex);
1facf9fc 13348+ di_read_unlock(root, !AuLock_IR);
13349+ p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
13350+ dentry = (void *)p;
13351+ if (IS_ERR(p))
13352+ goto out_pathname;
13353+
13354+ si_read_unlock(sb);
13355+ err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
13356+ dentry = ERR_PTR(err);
13357+ if (unlikely(err))
13358+ goto out_relock;
13359+
13360+ dentry = ERR_PTR(-ENOENT);
13361+ AuDebugOn(au_test_anon(path.dentry));
5527c038 13362+ if (unlikely(d_really_is_negative(path.dentry)))
1facf9fc 13363+ goto out_path;
13364+
5527c038 13365+ if (ino != d_inode(path.dentry)->i_ino)
1facf9fc 13366+ dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
13367+ else
13368+ dentry = dget(path.dentry);
13369+
4f0767ce 13370+out_path:
1facf9fc 13371+ path_put(&path);
4f0767ce 13372+out_relock:
1facf9fc 13373+ if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
13374+ if (!IS_ERR(dentry)) {
13375+ dput(dentry);
13376+ dentry = ERR_PTR(-ESTALE);
13377+ }
4f0767ce 13378+out_pathname:
1c60b727 13379+ free_page((unsigned long)pathname);
4f0767ce 13380+out_h_parent:
1facf9fc 13381+ dput(h_parent);
4f0767ce 13382+out:
1facf9fc 13383+ AuTraceErrPtr(dentry);
13384+ return dentry;
13385+}
13386+
13387+/* ---------------------------------------------------------------------- */
13388+
13389+static struct dentry *
13390+aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
13391+ int fh_type)
13392+{
13393+ struct dentry *dentry;
13394+ __u32 *fh = fid->raw;
027c5e7a 13395+ struct au_branch *br;
1facf9fc 13396+ ino_t ino, dir_ino;
1facf9fc 13397+ struct au_nfsd_si_lock nsi_lock = {
1facf9fc 13398+ .force_lock = 0
13399+ };
13400+
1facf9fc 13401+ dentry = ERR_PTR(-ESTALE);
4a4d8108
AM
13402+ /* it should never happen, but the file handle is unreliable */
13403+ if (unlikely(fh_len < Fh_tail))
13404+ goto out;
13405+ nsi_lock.sigen = fh[Fh_sigen];
13406+ nsi_lock.br_id = fh[Fh_br_id];
13407+
1facf9fc 13408+ /* branch id may be wrapped around */
027c5e7a
AM
13409+ br = NULL;
13410+ if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
1facf9fc 13411+ goto out;
13412+ nsi_lock.force_lock = 1;
13413+
13414+ /* is this inode still cached? */
13415+ ino = decode_ino(fh + Fh_ino);
4a4d8108
AM
13416+ /* it should never happen */
13417+ if (unlikely(ino == AUFS_ROOT_INO))
8cdd5066 13418+ goto out_unlock;
4a4d8108 13419+
1facf9fc 13420+ dir_ino = decode_ino(fh + Fh_dir_ino);
13421+ dentry = decode_by_ino(sb, ino, dir_ino);
13422+ if (IS_ERR(dentry))
13423+ goto out_unlock;
13424+ if (dentry)
13425+ goto accept;
13426+
13427+ /* is the parent dir cached? */
027c5e7a 13428+ br = au_sbr(sb, nsi_lock.bindex);
acd2b654 13429+ au_lcnt_inc(&br->br_nfiles);
1facf9fc 13430+ dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
13431+ if (IS_ERR(dentry))
13432+ goto out_unlock;
13433+ if (dentry)
13434+ goto accept;
13435+
13436+ /* lookup path */
027c5e7a 13437+ dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
1facf9fc 13438+ if (IS_ERR(dentry))
13439+ goto out_unlock;
13440+ if (unlikely(!dentry))
13441+ /* todo?: make it ESTALE */
13442+ goto out_unlock;
13443+
4f0767ce 13444+accept:
027c5e7a 13445+ if (!au_digen_test(dentry, au_sigen(sb))
5527c038 13446+ && d_inode(dentry)->i_generation == fh[Fh_igen])
1facf9fc 13447+ goto out_unlock; /* success */
13448+
13449+ dput(dentry);
13450+ dentry = ERR_PTR(-ESTALE);
4f0767ce 13451+out_unlock:
027c5e7a 13452+ if (br)
acd2b654 13453+ au_lcnt_dec(&br->br_nfiles);
1facf9fc 13454+ si_read_unlock(sb);
4f0767ce 13455+out:
1facf9fc 13456+ AuTraceErrPtr(dentry);
13457+ return dentry;
13458+}
13459+
13460+#if 0 /* reserved for future use */
13461+/* support subtreecheck option */
13462+static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
13463+ int fh_len, int fh_type)
13464+{
13465+ struct dentry *parent;
13466+ __u32 *fh = fid->raw;
13467+ ino_t dir_ino;
13468+
13469+ dir_ino = decode_ino(fh + Fh_dir_ino);
13470+ parent = decode_by_ino(sb, dir_ino, 0);
13471+ if (IS_ERR(parent))
13472+ goto out;
13473+ if (!parent)
13474+ parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
13475+ dir_ino, fh, fh_len);
13476+
4f0767ce 13477+out:
1facf9fc 13478+ AuTraceErrPtr(parent);
13479+ return parent;
13480+}
13481+#endif
13482+
13483+/* ---------------------------------------------------------------------- */
13484+
0c3ec466
AM
13485+static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
13486+ struct inode *dir)
1facf9fc 13487+{
13488+ int err;
0c3ec466 13489+ aufs_bindex_t bindex;
1facf9fc 13490+ struct super_block *sb, *h_sb;
0c3ec466
AM
13491+ struct dentry *dentry, *parent, *h_parent;
13492+ struct inode *h_dir;
1facf9fc 13493+ struct au_branch *br;
13494+
1facf9fc 13495+ err = -ENOSPC;
13496+ if (unlikely(*max_len <= Fh_tail)) {
13497+ AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
13498+ goto out;
13499+ }
13500+
13501+ err = FILEID_ROOT;
0c3ec466
AM
13502+ if (inode->i_ino == AUFS_ROOT_INO) {
13503+ AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
1facf9fc 13504+ goto out;
13505+ }
13506+
1facf9fc 13507+ h_parent = NULL;
0c3ec466
AM
13508+ sb = inode->i_sb;
13509+ err = si_read_lock(sb, AuLock_FLUSH);
027c5e7a
AM
13510+ if (unlikely(err))
13511+ goto out;
13512+
1facf9fc 13513+#ifdef CONFIG_AUFS_DEBUG
13514+ if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13515+ AuWarn1("NFS-exporting requires xino\n");
13516+#endif
027c5e7a 13517+ err = -EIO;
0c3ec466
AM
13518+ parent = NULL;
13519+ ii_read_lock_child(inode);
5afbbe0d 13520+ bindex = au_ibtop(inode);
0c3ec466 13521+ if (!dir) {
c1595e42 13522+ dentry = d_find_any_alias(inode);
0c3ec466
AM
13523+ if (unlikely(!dentry))
13524+ goto out_unlock;
13525+ AuDebugOn(au_test_anon(dentry));
13526+ parent = dget_parent(dentry);
13527+ dput(dentry);
13528+ if (unlikely(!parent))
13529+ goto out_unlock;
5527c038
JR
13530+ if (d_really_is_positive(parent))
13531+ dir = d_inode(parent);
1facf9fc 13532+ }
0c3ec466
AM
13533+
13534+ ii_read_lock_parent(dir);
13535+ h_dir = au_h_iptr(dir, bindex);
13536+ ii_read_unlock(dir);
13537+ if (unlikely(!h_dir))
13538+ goto out_parent;
c1595e42 13539+ h_parent = d_find_any_alias(h_dir);
1facf9fc 13540+ if (unlikely(!h_parent))
0c3ec466 13541+ goto out_hparent;
1facf9fc 13542+
13543+ err = -EPERM;
13544+ br = au_sbr(sb, bindex);
86dc4139 13545+ h_sb = au_br_sb(br);
1facf9fc 13546+ if (unlikely(!h_sb->s_export_op)) {
13547+ AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
0c3ec466 13548+ goto out_hparent;
1facf9fc 13549+ }
13550+
13551+ fh[Fh_br_id] = br->br_id;
13552+ fh[Fh_sigen] = au_sigen(sb);
13553+ encode_ino(fh + Fh_ino, inode->i_ino);
0c3ec466 13554+ encode_ino(fh + Fh_dir_ino, dir->i_ino);
1facf9fc 13555+ fh[Fh_igen] = inode->i_generation;
13556+
13557+ *max_len -= Fh_tail;
13558+ fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
13559+ max_len,
13560+ /*connectable or subtreecheck*/0);
13561+ err = fh[Fh_h_type];
13562+ *max_len += Fh_tail;
13563+ /* todo: macros? */
1716fcea 13564+ if (err != FILEID_INVALID)
1facf9fc 13565+ err = 99;
13566+ else
13567+ AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
13568+
0c3ec466 13569+out_hparent:
1facf9fc 13570+ dput(h_parent);
0c3ec466 13571+out_parent:
1facf9fc 13572+ dput(parent);
0c3ec466
AM
13573+out_unlock:
13574+ ii_read_unlock(inode);
13575+ si_read_unlock(sb);
4f0767ce 13576+out:
1facf9fc 13577+ if (unlikely(err < 0))
1716fcea 13578+ err = FILEID_INVALID;
1facf9fc 13579+ return err;
13580+}
13581+
13582+/* ---------------------------------------------------------------------- */
13583+
4a4d8108
AM
13584+static int aufs_commit_metadata(struct inode *inode)
13585+{
13586+ int err;
13587+ aufs_bindex_t bindex;
13588+ struct super_block *sb;
13589+ struct inode *h_inode;
13590+ int (*f)(struct inode *inode);
13591+
13592+ sb = inode->i_sb;
e49829fe 13593+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
4a4d8108 13594+ ii_write_lock_child(inode);
5afbbe0d 13595+ bindex = au_ibtop(inode);
4a4d8108
AM
13596+ AuDebugOn(bindex < 0);
13597+ h_inode = au_h_iptr(inode, bindex);
13598+
13599+ f = h_inode->i_sb->s_export_op->commit_metadata;
13600+ if (f)
13601+ err = f(h_inode);
13602+ else {
13603+ struct writeback_control wbc = {
13604+ .sync_mode = WB_SYNC_ALL,
13605+ .nr_to_write = 0 /* metadata only */
13606+ };
13607+
13608+ err = sync_inode(h_inode, &wbc);
13609+ }
13610+
13611+ au_cpup_attr_timesizes(inode);
13612+ ii_write_unlock(inode);
13613+ si_read_unlock(sb);
13614+ return err;
13615+}
13616+
13617+/* ---------------------------------------------------------------------- */
13618+
1facf9fc 13619+static struct export_operations aufs_export_op = {
4a4d8108 13620+ .fh_to_dentry = aufs_fh_to_dentry,
1facf9fc 13621+ /* .fh_to_parent = aufs_fh_to_parent, */
4a4d8108
AM
13622+ .encode_fh = aufs_encode_fh,
13623+ .commit_metadata = aufs_commit_metadata
1facf9fc 13624+};
13625+
13626+void au_export_init(struct super_block *sb)
13627+{
13628+ struct au_sbinfo *sbinfo;
13629+ __u32 u;
13630+
5afbbe0d
AM
13631+ BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
13632+ && IS_MODULE(CONFIG_EXPORTFS),
13633+ AUFS_NAME ": unsupported configuration "
13634+ "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
13635+
1facf9fc 13636+ sb->s_export_op = &aufs_export_op;
13637+ sbinfo = au_sbi(sb);
13638+ sbinfo->si_xigen = NULL;
13639+ get_random_bytes(&u, sizeof(u));
13640+ BUILD_BUG_ON(sizeof(u) != sizeof(int));
13641+ atomic_set(&sbinfo->si_xigen_next, u);
13642+}
076b876e 13643diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
eca34b5c 13644--- /usr/share/empty/fs/aufs/fhsm.c 1970-01-01 01:00:00.000000000 +0100
46016270 13645+++ linux/fs/aufs/fhsm.c 2021-02-24 13:33:42.744347058 +0100
062440b3 13646@@ -0,0 +1,427 @@
cd7a4cd9 13647+// SPDX-License-Identifier: GPL-2.0
076b876e 13648+/*
d58c55f2 13649+ * Copyright (C) 2011-2020 Junjiro R. Okajima
076b876e
AM
13650+ *
13651+ * This program, aufs is free software; you can redistribute it and/or modify
13652+ * it under the terms of the GNU General Public License as published by
13653+ * the Free Software Foundation; either version 2 of the License, or
13654+ * (at your option) any later version.
13655+ *
13656+ * This program is distributed in the hope that it will be useful,
13657+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13658+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13659+ * GNU General Public License for more details.
13660+ *
13661+ * You should have received a copy of the GNU General Public License
13662+ * along with this program; if not, write to the Free Software
13663+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
13664+ */
13665+
13666+/*
13667+ * File-based Hierarchy Storage Management
13668+ */
13669+
13670+#include <linux/anon_inodes.h>
13671+#include <linux/poll.h>
13672+#include <linux/seq_file.h>
13673+#include <linux/statfs.h>
13674+#include "aufs.h"
13675+
c1595e42
JR
13676+static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
13677+{
13678+ struct au_sbinfo *sbinfo;
13679+ struct au_fhsm *fhsm;
13680+
13681+ SiMustAnyLock(sb);
13682+
13683+ sbinfo = au_sbi(sb);
13684+ fhsm = &sbinfo->si_fhsm;
13685+ AuDebugOn(!fhsm);
13686+ return fhsm->fhsm_bottom;
13687+}
13688+
13689+void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
13690+{
13691+ struct au_sbinfo *sbinfo;
13692+ struct au_fhsm *fhsm;
13693+
13694+ SiMustWriteLock(sb);
13695+
13696+ sbinfo = au_sbi(sb);
13697+ fhsm = &sbinfo->si_fhsm;
13698+ AuDebugOn(!fhsm);
13699+ fhsm->fhsm_bottom = bindex;
13700+}
13701+
13702+/* ---------------------------------------------------------------------- */
13703+
076b876e
AM
13704+static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
13705+{
13706+ struct au_br_fhsm *bf;
13707+
13708+ bf = br->br_fhsm;
13709+ MtxMustLock(&bf->bf_lock);
13710+
13711+ return !bf->bf_readable
13712+ || time_after(jiffies,
13713+ bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
13714+}
13715+
13716+/* ---------------------------------------------------------------------- */
13717+
13718+static void au_fhsm_notify(struct super_block *sb, int val)
13719+{
13720+ struct au_sbinfo *sbinfo;
13721+ struct au_fhsm *fhsm;
13722+
13723+ SiMustAnyLock(sb);
13724+
13725+ sbinfo = au_sbi(sb);
13726+ fhsm = &sbinfo->si_fhsm;
13727+ if (au_fhsm_pid(fhsm)
13728+ && atomic_read(&fhsm->fhsm_readable) != -1) {
13729+ atomic_set(&fhsm->fhsm_readable, val);
13730+ if (val)
13731+ wake_up(&fhsm->fhsm_wqh);
13732+ }
13733+}
13734+
13735+static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
13736+ struct aufs_stfs *rstfs, int do_lock, int do_notify)
13737+{
13738+ int err;
13739+ struct au_branch *br;
13740+ struct au_br_fhsm *bf;
13741+
13742+ br = au_sbr(sb, bindex);
13743+ AuDebugOn(au_br_rdonly(br));
13744+ bf = br->br_fhsm;
13745+ AuDebugOn(!bf);
13746+
13747+ if (do_lock)
13748+ mutex_lock(&bf->bf_lock);
13749+ else
13750+ MtxMustLock(&bf->bf_lock);
13751+
13752+ /* sb->s_root for NFS is unreliable */
13753+ err = au_br_stfs(br, &bf->bf_stfs);
13754+ if (unlikely(err)) {
13755+ AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
13756+ goto out;
13757+ }
13758+
13759+ bf->bf_jiffy = jiffies;
13760+ bf->bf_readable = 1;
13761+ if (do_notify)
13762+ au_fhsm_notify(sb, /*val*/1);
13763+ if (rstfs)
13764+ *rstfs = bf->bf_stfs;
13765+
13766+out:
13767+ if (do_lock)
13768+ mutex_unlock(&bf->bf_lock);
13769+ au_fhsm_notify(sb, /*val*/1);
13770+
13771+ return err;
13772+}
13773+
13774+void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
13775+{
13776+ int err;
076b876e
AM
13777+ struct au_sbinfo *sbinfo;
13778+ struct au_fhsm *fhsm;
13779+ struct au_branch *br;
13780+ struct au_br_fhsm *bf;
13781+
13782+ AuDbg("b%d, force %d\n", bindex, force);
13783+ SiMustAnyLock(sb);
13784+
13785+ sbinfo = au_sbi(sb);
13786+ fhsm = &sbinfo->si_fhsm;
c1595e42
JR
13787+ if (!au_ftest_si(sbinfo, FHSM)
13788+ || fhsm->fhsm_bottom == bindex)
076b876e
AM
13789+ return;
13790+
13791+ br = au_sbr(sb, bindex);
13792+ bf = br->br_fhsm;
13793+ AuDebugOn(!bf);
13794+ mutex_lock(&bf->bf_lock);
13795+ if (force
13796+ || au_fhsm_pid(fhsm)
13797+ || au_fhsm_test_jiffy(sbinfo, br))
13798+ err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
13799+ /*do_notify*/1);
13800+ mutex_unlock(&bf->bf_lock);
13801+}
13802+
13803+void au_fhsm_wrote_all(struct super_block *sb, int force)
13804+{
5afbbe0d 13805+ aufs_bindex_t bindex, bbot;
076b876e
AM
13806+ struct au_branch *br;
13807+
13808+ /* exclude the bottom */
5afbbe0d
AM
13809+ bbot = au_fhsm_bottom(sb);
13810+ for (bindex = 0; bindex < bbot; bindex++) {
076b876e
AM
13811+ br = au_sbr(sb, bindex);
13812+ if (au_br_fhsm(br->br_perm))
13813+ au_fhsm_wrote(sb, bindex, force);
13814+ }
13815+}
13816+
13817+/* ---------------------------------------------------------------------- */
13818+
be118d29 13819+static __poll_t au_fhsm_poll(struct file *file, struct poll_table_struct *wait)
076b876e 13820+{
be118d29 13821+ __poll_t mask;
076b876e
AM
13822+ struct au_sbinfo *sbinfo;
13823+ struct au_fhsm *fhsm;
13824+
13825+ mask = 0;
13826+ sbinfo = file->private_data;
13827+ fhsm = &sbinfo->si_fhsm;
13828+ poll_wait(file, &fhsm->fhsm_wqh, wait);
13829+ if (atomic_read(&fhsm->fhsm_readable))
be118d29 13830+ mask = EPOLLIN /* | EPOLLRDNORM */;
076b876e 13831+
b00004a5
AM
13832+ if (!mask)
13833+ AuDbg("mask 0x%x\n", mask);
076b876e
AM
13834+ return mask;
13835+}
13836+
13837+static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
13838+ struct aufs_stfs *stfs, __s16 brid)
13839+{
13840+ int err;
13841+
13842+ err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
13843+ if (!err)
13844+ err = __put_user(brid, &stbr->brid);
13845+ if (unlikely(err))
13846+ err = -EFAULT;
13847+
13848+ return err;
13849+}
13850+
13851+static ssize_t au_fhsm_do_read(struct super_block *sb,
13852+ struct aufs_stbr __user *stbr, size_t count)
13853+{
13854+ ssize_t err;
13855+ int nstbr;
5afbbe0d 13856+ aufs_bindex_t bindex, bbot;
076b876e
AM
13857+ struct au_branch *br;
13858+ struct au_br_fhsm *bf;
13859+
13860+ /* except the bottom branch */
13861+ err = 0;
13862+ nstbr = 0;
5afbbe0d
AM
13863+ bbot = au_fhsm_bottom(sb);
13864+ for (bindex = 0; !err && bindex < bbot; bindex++) {
076b876e
AM
13865+ br = au_sbr(sb, bindex);
13866+ if (!au_br_fhsm(br->br_perm))
13867+ continue;
13868+
13869+ bf = br->br_fhsm;
13870+ mutex_lock(&bf->bf_lock);
13871+ if (bf->bf_readable) {
13872+ err = -EFAULT;
13873+ if (count >= sizeof(*stbr))
13874+ err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
13875+ br->br_id);
13876+ if (!err) {
13877+ bf->bf_readable = 0;
13878+ count -= sizeof(*stbr);
13879+ nstbr++;
13880+ }
13881+ }
13882+ mutex_unlock(&bf->bf_lock);
13883+ }
13884+ if (!err)
13885+ err = sizeof(*stbr) * nstbr;
13886+
13887+ return err;
13888+}
13889+
13890+static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
13891+ loff_t *pos)
13892+{
13893+ ssize_t err;
13894+ int readable;
5afbbe0d 13895+ aufs_bindex_t nfhsm, bindex, bbot;
076b876e
AM
13896+ struct au_sbinfo *sbinfo;
13897+ struct au_fhsm *fhsm;
13898+ struct au_branch *br;
13899+ struct super_block *sb;
13900+
13901+ err = 0;
13902+ sbinfo = file->private_data;
13903+ fhsm = &sbinfo->si_fhsm;
13904+need_data:
13905+ spin_lock_irq(&fhsm->fhsm_wqh.lock);
13906+ if (!atomic_read(&fhsm->fhsm_readable)) {
13907+ if (vfsub_file_flags(file) & O_NONBLOCK)
13908+ err = -EAGAIN;
13909+ else
13910+ err = wait_event_interruptible_locked_irq
13911+ (fhsm->fhsm_wqh,
13912+ atomic_read(&fhsm->fhsm_readable));
13913+ }
13914+ spin_unlock_irq(&fhsm->fhsm_wqh.lock);
13915+ if (unlikely(err))
13916+ goto out;
13917+
13918+ /* sb may already be dead */
13919+ au_rw_read_lock(&sbinfo->si_rwsem);
13920+ readable = atomic_read(&fhsm->fhsm_readable);
13921+ if (readable > 0) {
13922+ sb = sbinfo->si_sb;
13923+ AuDebugOn(!sb);
13924+ /* exclude the bottom branch */
13925+ nfhsm = 0;
5afbbe0d
AM
13926+ bbot = au_fhsm_bottom(sb);
13927+ for (bindex = 0; bindex < bbot; bindex++) {
076b876e
AM
13928+ br = au_sbr(sb, bindex);
13929+ if (au_br_fhsm(br->br_perm))
13930+ nfhsm++;
13931+ }
13932+ err = -EMSGSIZE;
13933+ if (nfhsm * sizeof(struct aufs_stbr) <= count) {
13934+ atomic_set(&fhsm->fhsm_readable, 0);
13935+ err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
13936+ count);
13937+ }
13938+ }
13939+ au_rw_read_unlock(&sbinfo->si_rwsem);
13940+ if (!readable)
13941+ goto need_data;
13942+
13943+out:
13944+ return err;
13945+}
13946+
13947+static int au_fhsm_release(struct inode *inode, struct file *file)
13948+{
13949+ struct au_sbinfo *sbinfo;
13950+ struct au_fhsm *fhsm;
13951+
13952+ /* sb may already be dead */
13953+ sbinfo = file->private_data;
13954+ fhsm = &sbinfo->si_fhsm;
13955+ spin_lock(&fhsm->fhsm_spin);
13956+ fhsm->fhsm_pid = 0;
13957+ spin_unlock(&fhsm->fhsm_spin);
13958+ kobject_put(&sbinfo->si_kobj);
13959+
13960+ return 0;
13961+}
13962+
13963+static const struct file_operations au_fhsm_fops = {
13964+ .owner = THIS_MODULE,
13965+ .llseek = noop_llseek,
13966+ .read = au_fhsm_read,
13967+ .poll = au_fhsm_poll,
13968+ .release = au_fhsm_release
13969+};
13970+
13971+int au_fhsm_fd(struct super_block *sb, int oflags)
13972+{
13973+ int err, fd;
13974+ struct au_sbinfo *sbinfo;
13975+ struct au_fhsm *fhsm;
13976+
13977+ err = -EPERM;
13978+ if (unlikely(!capable(CAP_SYS_ADMIN)))
13979+ goto out;
13980+
13981+ err = -EINVAL;
13982+ if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
13983+ goto out;
13984+
13985+ err = 0;
13986+ sbinfo = au_sbi(sb);
13987+ fhsm = &sbinfo->si_fhsm;
13988+ spin_lock(&fhsm->fhsm_spin);
13989+ if (!fhsm->fhsm_pid)
13990+ fhsm->fhsm_pid = current->pid;
13991+ else
13992+ err = -EBUSY;
13993+ spin_unlock(&fhsm->fhsm_spin);
13994+ if (unlikely(err))
13995+ goto out;
13996+
13997+ oflags |= O_RDONLY;
13998+ /* oflags |= FMODE_NONOTIFY; */
13999+ fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
14000+ err = fd;
14001+ if (unlikely(fd < 0))
14002+ goto out_pid;
14003+
acd2b654 14004+ /* succeed regardless 'fhsm' status */
076b876e
AM
14005+ kobject_get(&sbinfo->si_kobj);
14006+ si_noflush_read_lock(sb);
14007+ if (au_ftest_si(sbinfo, FHSM))
14008+ au_fhsm_wrote_all(sb, /*force*/0);
14009+ si_read_unlock(sb);
14010+ goto out; /* success */
14011+
14012+out_pid:
14013+ spin_lock(&fhsm->fhsm_spin);
14014+ fhsm->fhsm_pid = 0;
14015+ spin_unlock(&fhsm->fhsm_spin);
14016+out:
14017+ AuTraceErr(err);
14018+ return err;
14019+}
14020+
14021+/* ---------------------------------------------------------------------- */
14022+
14023+int au_fhsm_br_alloc(struct au_branch *br)
14024+{
14025+ int err;
14026+
14027+ err = 0;
14028+ br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
14029+ if (br->br_fhsm)
14030+ au_br_fhsm_init(br->br_fhsm);
14031+ else
14032+ err = -ENOMEM;
14033+
14034+ return err;
14035+}
14036+
14037+/* ---------------------------------------------------------------------- */
14038+
14039+void au_fhsm_fin(struct super_block *sb)
14040+{
14041+ au_fhsm_notify(sb, /*val*/-1);
14042+}
14043+
14044+void au_fhsm_init(struct au_sbinfo *sbinfo)
14045+{
14046+ struct au_fhsm *fhsm;
14047+
14048+ fhsm = &sbinfo->si_fhsm;
14049+ spin_lock_init(&fhsm->fhsm_spin);
14050+ init_waitqueue_head(&fhsm->fhsm_wqh);
14051+ atomic_set(&fhsm->fhsm_readable, 0);
14052+ fhsm->fhsm_expire
14053+ = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
c1595e42 14054+ fhsm->fhsm_bottom = -1;
076b876e
AM
14055+}
14056+
14057+void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
14058+{
14059+ sbinfo->si_fhsm.fhsm_expire
14060+ = msecs_to_jiffies(sec * MSEC_PER_SEC);
14061+}
14062+
14063+void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
14064+{
14065+ unsigned int u;
14066+
14067+ if (!au_ftest_si(sbinfo, FHSM))
14068+ return;
14069+
14070+ u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
14071+ if (u != AUFS_FHSM_CACHE_DEF_SEC)
14072+ seq_printf(seq, ",fhsm_sec=%u", u);
14073+}
7f207e10 14074diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
eca34b5c 14075--- /usr/share/empty/fs/aufs/file.c 1970-01-01 01:00:00.000000000 +0100
46016270 14076+++ linux/fs/aufs/file.c 2021-02-24 13:33:42.744347058 +0100
42a736d3 14077@@ -0,0 +1,863 @@
cd7a4cd9 14078+// SPDX-License-Identifier: GPL-2.0
1facf9fc 14079+/*
d58c55f2 14080+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 14081+ *
14082+ * This program, aufs is free software; you can redistribute it and/or modify
14083+ * it under the terms of the GNU General Public License as published by
14084+ * the Free Software Foundation; either version 2 of the License, or
14085+ * (at your option) any later version.
dece6358
AM
14086+ *
14087+ * This program is distributed in the hope that it will be useful,
14088+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14089+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14090+ * GNU General Public License for more details.
14091+ *
14092+ * You should have received a copy of the GNU General Public License
523b37e3 14093+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 14094+ */
14095+
14096+/*
4a4d8108 14097+ * handling file/dir, and address_space operation
1facf9fc 14098+ */
14099+
7eafdf33
AM
14100+#ifdef CONFIG_AUFS_DEBUG
14101+#include <linux/migrate.h>
14102+#endif
4a4d8108 14103+#include <linux/pagemap.h>
1facf9fc 14104+#include "aufs.h"
14105+
4a4d8108
AM
14106+/* drop flags for writing */
14107+unsigned int au_file_roflags(unsigned int flags)
14108+{
14109+ flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
14110+ flags |= O_RDONLY | O_NOATIME;
14111+ return flags;
14112+}
14113+
14114+/* common functions to regular file and dir */
14115+struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
392086de 14116+ struct file *file, int force_wr)
1facf9fc 14117+{
1308ab2a 14118+ struct file *h_file;
4a4d8108
AM
14119+ struct dentry *h_dentry;
14120+ struct inode *h_inode;
14121+ struct super_block *sb;
14122+ struct au_branch *br;
14123+ struct path h_path;
b912730e 14124+ int err;
1facf9fc 14125+
4a4d8108
AM
14126+ /* a race condition can happen between open and unlink/rmdir */
14127+ h_file = ERR_PTR(-ENOENT);
14128+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 14129+ if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
4a4d8108 14130+ goto out;
5527c038 14131+ h_inode = d_inode(h_dentry);
027c5e7a
AM
14132+ spin_lock(&h_dentry->d_lock);
14133+ err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
5527c038 14134+ /* || !d_inode(dentry)->i_nlink */
027c5e7a
AM
14135+ ;
14136+ spin_unlock(&h_dentry->d_lock);
14137+ if (unlikely(err))
4a4d8108 14138+ goto out;
1facf9fc 14139+
4a4d8108
AM
14140+ sb = dentry->d_sb;
14141+ br = au_sbr(sb, bindex);
b912730e
AM
14142+ err = au_br_test_oflag(flags, br);
14143+ h_file = ERR_PTR(err);
14144+ if (unlikely(err))
027c5e7a 14145+ goto out;
1facf9fc 14146+
4a4d8108 14147+ /* drop flags for writing */
5527c038 14148+ if (au_test_ro(sb, bindex, d_inode(dentry))) {
392086de
AM
14149+ if (force_wr && !(flags & O_WRONLY))
14150+ force_wr = 0;
4a4d8108 14151+ flags = au_file_roflags(flags);
392086de
AM
14152+ if (force_wr) {
14153+ h_file = ERR_PTR(-EROFS);
14154+ flags = au_file_roflags(flags);
14155+ if (unlikely(vfsub_native_ro(h_inode)
14156+ || IS_APPEND(h_inode)))
14157+ goto out;
14158+ flags &= ~O_ACCMODE;
14159+ flags |= O_WRONLY;
14160+ }
14161+ }
4a4d8108 14162+ flags &= ~O_CREAT;
acd2b654 14163+ au_lcnt_inc(&br->br_nfiles);
4a4d8108 14164+ h_path.dentry = h_dentry;
86dc4139 14165+ h_path.mnt = au_br_mnt(br);
38d290e6 14166+ h_file = vfsub_dentry_open(&h_path, flags);
4a4d8108
AM
14167+ if (IS_ERR(h_file))
14168+ goto out_br;
dece6358 14169+
b912730e 14170+ if (flags & __FMODE_EXEC) {
4a4d8108
AM
14171+ err = deny_write_access(h_file);
14172+ if (unlikely(err)) {
14173+ fput(h_file);
14174+ h_file = ERR_PTR(err);
14175+ goto out_br;
14176+ }
14177+ }
953406b4 14178+ fsnotify_open(h_file);
4a4d8108 14179+ goto out; /* success */
1facf9fc 14180+
4f0767ce 14181+out_br:
acd2b654 14182+ au_lcnt_dec(&br->br_nfiles);
4f0767ce 14183+out:
4a4d8108
AM
14184+ return h_file;
14185+}
1308ab2a 14186+
076b876e
AM
14187+static int au_cmoo(struct dentry *dentry)
14188+{
8b6a4947 14189+ int err, cmoo, matched;
076b876e
AM
14190+ unsigned int udba;
14191+ struct path h_path;
14192+ struct au_pin pin;
14193+ struct au_cp_generic cpg = {
14194+ .dentry = dentry,
14195+ .bdst = -1,
14196+ .bsrc = -1,
14197+ .len = -1,
14198+ .pin = &pin,
14199+ .flags = AuCpup_DTIME | AuCpup_HOPEN
14200+ };
7e9cd9fe 14201+ struct inode *delegated;
076b876e
AM
14202+ struct super_block *sb;
14203+ struct au_sbinfo *sbinfo;
14204+ struct au_fhsm *fhsm;
14205+ pid_t pid;
14206+ struct au_branch *br;
14207+ struct dentry *parent;
14208+ struct au_hinode *hdir;
14209+
14210+ DiMustWriteLock(dentry);
5527c038 14211+ IiMustWriteLock(d_inode(dentry));
076b876e
AM
14212+
14213+ err = 0;
14214+ if (IS_ROOT(dentry))
14215+ goto out;
5afbbe0d 14216+ cpg.bsrc = au_dbtop(dentry);
076b876e
AM
14217+ if (!cpg.bsrc)
14218+ goto out;
14219+
14220+ sb = dentry->d_sb;
14221+ sbinfo = au_sbi(sb);
14222+ fhsm = &sbinfo->si_fhsm;
14223+ pid = au_fhsm_pid(fhsm);
8b6a4947
AM
14224+ rcu_read_lock();
14225+ matched = (pid
14226+ && (current->pid == pid
14227+ || rcu_dereference(current->real_parent)->pid == pid));
14228+ rcu_read_unlock();
14229+ if (matched)
076b876e
AM
14230+ goto out;
14231+
14232+ br = au_sbr(sb, cpg.bsrc);
14233+ cmoo = au_br_cmoo(br->br_perm);
14234+ if (!cmoo)
14235+ goto out;
7e9cd9fe 14236+ if (!d_is_reg(dentry))
076b876e
AM
14237+ cmoo &= AuBrAttr_COO_ALL;
14238+ if (!cmoo)
14239+ goto out;
14240+
14241+ parent = dget_parent(dentry);
14242+ di_write_lock_parent(parent);
14243+ err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
14244+ cpg.bdst = err;
14245+ if (unlikely(err < 0)) {
14246+ err = 0; /* there is no upper writable branch */
14247+ goto out_dgrade;
14248+ }
14249+ AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
14250+
14251+ /* do not respect the coo attrib for the target branch */
14252+ err = au_cpup_dirs(dentry, cpg.bdst);
14253+ if (unlikely(err))
14254+ goto out_dgrade;
14255+
14256+ di_downgrade_lock(parent, AuLock_IR);
14257+ udba = au_opt_udba(sb);
14258+ err = au_pin(&pin, dentry, cpg.bdst, udba,
14259+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14260+ if (unlikely(err))
14261+ goto out_parent;
14262+
14263+ err = au_sio_cpup_simple(&cpg);
14264+ au_unpin(&pin);
14265+ if (unlikely(err))
14266+ goto out_parent;
14267+ if (!(cmoo & AuBrWAttr_MOO))
14268+ goto out_parent; /* success */
14269+
14270+ err = au_pin(&pin, dentry, cpg.bsrc, udba,
14271+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14272+ if (unlikely(err))
14273+ goto out_parent;
14274+
14275+ h_path.mnt = au_br_mnt(br);
14276+ h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
5527c038 14277+ hdir = au_hi(d_inode(parent), cpg.bsrc);
076b876e
AM
14278+ delegated = NULL;
14279+ err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
14280+ au_unpin(&pin);
14281+ /* todo: keep h_dentry or not? */
14282+ if (unlikely(err == -EWOULDBLOCK)) {
14283+ pr_warn("cannot retry for NFSv4 delegation"
14284+ " for an internal unlink\n");
14285+ iput(delegated);
14286+ }
14287+ if (unlikely(err)) {
14288+ pr_err("unlink %pd after coo failed (%d), ignored\n",
14289+ dentry, err);
14290+ err = 0;
14291+ }
14292+ goto out_parent; /* success */
14293+
14294+out_dgrade:
14295+ di_downgrade_lock(parent, AuLock_IR);
14296+out_parent:
14297+ di_read_unlock(parent, AuLock_IR);
14298+ dput(parent);
14299+out:
14300+ AuTraceErr(err);
14301+ return err;
14302+}
14303+
b912730e 14304+int au_do_open(struct file *file, struct au_do_open_args *args)
1facf9fc 14305+{
8b6a4947 14306+ int err, aopen = args->aopen;
1facf9fc 14307+ struct dentry *dentry;
076b876e 14308+ struct au_finfo *finfo;
1308ab2a 14309+
8b6a4947 14310+ if (!aopen)
b912730e
AM
14311+ err = au_finfo_init(file, args->fidir);
14312+ else {
14313+ lockdep_off();
14314+ err = au_finfo_init(file, args->fidir);
14315+ lockdep_on();
14316+ }
4a4d8108
AM
14317+ if (unlikely(err))
14318+ goto out;
1facf9fc 14319+
2000de60 14320+ dentry = file->f_path.dentry;
b912730e 14321+ AuDebugOn(IS_ERR_OR_NULL(dentry));
8b6a4947
AM
14322+ di_write_lock_child(dentry);
14323+ err = au_cmoo(dentry);
14324+ di_downgrade_lock(dentry, AuLock_IR);
14325+ if (!err) {
14326+ if (!aopen)
b912730e 14327+ err = args->open(file, vfsub_file_flags(file), NULL);
8b6a4947
AM
14328+ else {
14329+ lockdep_off();
acd2b654
AM
14330+ err = args->open(file, vfsub_file_flags(file),
14331+ args->h_file);
8b6a4947
AM
14332+ lockdep_on();
14333+ }
b912730e 14334+ }
8b6a4947 14335+ di_read_unlock(dentry, AuLock_IR);
1facf9fc 14336+
076b876e
AM
14337+ finfo = au_fi(file);
14338+ if (!err) {
14339+ finfo->fi_file = file;
8b6a4947
AM
14340+ au_hbl_add(&finfo->fi_hlist,
14341+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
076b876e 14342+ }
8b6a4947 14343+ if (!aopen)
b912730e
AM
14344+ fi_write_unlock(file);
14345+ else {
14346+ lockdep_off();
14347+ fi_write_unlock(file);
14348+ lockdep_on();
14349+ }
4a4d8108 14350+ if (unlikely(err)) {
076b876e 14351+ finfo->fi_hdir = NULL;
1c60b727 14352+ au_finfo_fin(file);
1308ab2a 14353+ }
4a4d8108 14354+
4f0767ce 14355+out:
8b6a4947 14356+ AuTraceErr(err);
1308ab2a 14357+ return err;
14358+}
dece6358 14359+
4a4d8108 14360+int au_reopen_nondir(struct file *file)
1308ab2a 14361+{
4a4d8108 14362+ int err;
5afbbe0d 14363+ aufs_bindex_t btop;
4a4d8108 14364+ struct dentry *dentry;
acd2b654 14365+ struct au_branch *br;
4a4d8108 14366+ struct file *h_file, *h_file_tmp;
1308ab2a 14367+
2000de60 14368+ dentry = file->f_path.dentry;
5afbbe0d 14369+ btop = au_dbtop(dentry);
acd2b654 14370+ br = au_sbr(dentry->d_sb, btop);
4a4d8108 14371+ h_file_tmp = NULL;
5afbbe0d 14372+ if (au_fbtop(file) == btop) {
4a4d8108
AM
14373+ h_file = au_hf_top(file);
14374+ if (file->f_mode == h_file->f_mode)
14375+ return 0; /* success */
14376+ h_file_tmp = h_file;
14377+ get_file(h_file_tmp);
acd2b654 14378+ au_lcnt_inc(&br->br_nfiles);
5afbbe0d 14379+ au_set_h_fptr(file, btop, NULL);
4a4d8108
AM
14380+ }
14381+ AuDebugOn(au_fi(file)->fi_hdir);
86dc4139
AM
14382+ /*
14383+ * it can happen
14384+ * file exists on both of rw and ro
5afbbe0d 14385+ * open --> dbtop and fbtop are both 0
86dc4139
AM
14386+ * prepend a branch as rw, "rw" become ro
14387+ * remove rw/file
14388+ * delete the top branch, "rw" becomes rw again
5afbbe0d
AM
14389+ * --> dbtop is 1, fbtop is still 0
14390+ * write --> fbtop is 0 but dbtop is 1
86dc4139 14391+ */
5afbbe0d 14392+ /* AuDebugOn(au_fbtop(file) < btop); */
1308ab2a 14393+
5afbbe0d 14394+ h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
392086de 14395+ file, /*force_wr*/0);
4a4d8108 14396+ err = PTR_ERR(h_file);
86dc4139
AM
14397+ if (IS_ERR(h_file)) {
14398+ if (h_file_tmp) {
acd2b654 14399+ /* revert */
5afbbe0d 14400+ au_set_h_fptr(file, btop, h_file_tmp);
86dc4139
AM
14401+ h_file_tmp = NULL;
14402+ }
4a4d8108 14403+ goto out; /* todo: close all? */
86dc4139 14404+ }
4a4d8108
AM
14405+
14406+ err = 0;
5afbbe0d
AM
14407+ au_set_fbtop(file, btop);
14408+ au_set_h_fptr(file, btop, h_file);
4a4d8108
AM
14409+ au_update_figen(file);
14410+ /* todo: necessary? */
14411+ /* file->f_ra = h_file->f_ra; */
14412+
4f0767ce 14413+out:
acd2b654 14414+ if (h_file_tmp) {
4a4d8108 14415+ fput(h_file_tmp);
acd2b654
AM
14416+ au_lcnt_dec(&br->br_nfiles);
14417+ }
4a4d8108 14418+ return err;
1facf9fc 14419+}
14420+
1308ab2a 14421+/* ---------------------------------------------------------------------- */
14422+
4a4d8108
AM
14423+static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
14424+ struct dentry *hi_wh)
1facf9fc 14425+{
4a4d8108 14426+ int err;
5afbbe0d 14427+ aufs_bindex_t btop;
4a4d8108
AM
14428+ struct au_dinfo *dinfo;
14429+ struct dentry *h_dentry;
14430+ struct au_hdentry *hdp;
1facf9fc 14431+
2000de60 14432+ dinfo = au_di(file->f_path.dentry);
4a4d8108 14433+ AuRwMustWriteLock(&dinfo->di_rwsem);
dece6358 14434+
5afbbe0d
AM
14435+ btop = dinfo->di_btop;
14436+ dinfo->di_btop = btgt;
14437+ hdp = au_hdentry(dinfo, btgt);
14438+ h_dentry = hdp->hd_dentry;
14439+ hdp->hd_dentry = hi_wh;
4a4d8108 14440+ err = au_reopen_nondir(file);
5afbbe0d
AM
14441+ hdp->hd_dentry = h_dentry;
14442+ dinfo->di_btop = btop;
1facf9fc 14443+
1facf9fc 14444+ return err;
14445+}
14446+
4a4d8108 14447+static int au_ready_to_write_wh(struct file *file, loff_t len,
86dc4139 14448+ aufs_bindex_t bcpup, struct au_pin *pin)
1facf9fc 14449+{
4a4d8108 14450+ int err;
027c5e7a 14451+ struct inode *inode, *h_inode;
c2b27bf2
AM
14452+ struct dentry *h_dentry, *hi_wh;
14453+ struct au_cp_generic cpg = {
2000de60 14454+ .dentry = file->f_path.dentry,
c2b27bf2
AM
14455+ .bdst = bcpup,
14456+ .bsrc = -1,
14457+ .len = len,
14458+ .pin = pin
14459+ };
1facf9fc 14460+
5afbbe0d 14461+ au_update_dbtop(cpg.dentry);
5527c038 14462+ inode = d_inode(cpg.dentry);
027c5e7a 14463+ h_inode = NULL;
5afbbe0d
AM
14464+ if (au_dbtop(cpg.dentry) <= bcpup
14465+ && au_dbbot(cpg.dentry) >= bcpup) {
c2b27bf2 14466+ h_dentry = au_h_dptr(cpg.dentry, bcpup);
5527c038
JR
14467+ if (h_dentry && d_is_positive(h_dentry))
14468+ h_inode = d_inode(h_dentry);
027c5e7a 14469+ }
4a4d8108 14470+ hi_wh = au_hi_wh(inode, bcpup);
027c5e7a 14471+ if (!hi_wh && !h_inode)
c2b27bf2 14472+ err = au_sio_cpup_wh(&cpg, file);
4a4d8108
AM
14473+ else
14474+ /* already copied-up after unlink */
14475+ err = au_reopen_wh(file, bcpup, hi_wh);
1facf9fc 14476+
4a4d8108 14477+ if (!err
38d290e6
JR
14478+ && (inode->i_nlink > 1
14479+ || (inode->i_state & I_LINKABLE))
c2b27bf2
AM
14480+ && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
14481+ au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
1308ab2a 14482+
dece6358 14483+ return err;
1facf9fc 14484+}
14485+
4a4d8108
AM
14486+/*
14487+ * prepare the @file for writing.
14488+ */
14489+int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
1facf9fc 14490+{
4a4d8108 14491+ int err;
5afbbe0d 14492+ aufs_bindex_t dbtop;
c1595e42 14493+ struct dentry *parent;
86dc4139 14494+ struct inode *inode;
1facf9fc 14495+ struct super_block *sb;
4a4d8108 14496+ struct file *h_file;
c2b27bf2 14497+ struct au_cp_generic cpg = {
2000de60 14498+ .dentry = file->f_path.dentry,
c2b27bf2
AM
14499+ .bdst = -1,
14500+ .bsrc = -1,
14501+ .len = len,
14502+ .pin = pin,
14503+ .flags = AuCpup_DTIME
14504+ };
1facf9fc 14505+
c2b27bf2 14506+ sb = cpg.dentry->d_sb;
5527c038 14507+ inode = d_inode(cpg.dentry);
5afbbe0d 14508+ cpg.bsrc = au_fbtop(file);
c2b27bf2 14509+ err = au_test_ro(sb, cpg.bsrc, inode);
4a4d8108 14510+ if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
c2b27bf2
AM
14511+ err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
14512+ /*flags*/0);
1facf9fc 14513+ goto out;
4a4d8108 14514+ }
1facf9fc 14515+
027c5e7a 14516+ /* need to cpup or reopen */
c2b27bf2 14517+ parent = dget_parent(cpg.dentry);
4a4d8108 14518+ di_write_lock_parent(parent);
c2b27bf2
AM
14519+ err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14520+ cpg.bdst = err;
4a4d8108
AM
14521+ if (unlikely(err < 0))
14522+ goto out_dgrade;
14523+ err = 0;
14524+
c2b27bf2
AM
14525+ if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
14526+ err = au_cpup_dirs(cpg.dentry, cpg.bdst);
1facf9fc 14527+ if (unlikely(err))
4a4d8108
AM
14528+ goto out_dgrade;
14529+ }
14530+
c2b27bf2 14531+ err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
4a4d8108
AM
14532+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14533+ if (unlikely(err))
14534+ goto out_dgrade;
14535+
5afbbe0d
AM
14536+ dbtop = au_dbtop(cpg.dentry);
14537+ if (dbtop <= cpg.bdst)
c2b27bf2 14538+ cpg.bsrc = cpg.bdst;
027c5e7a 14539+
5afbbe0d 14540+ if (dbtop <= cpg.bdst /* just reopen */
c2b27bf2 14541+ || !d_unhashed(cpg.dentry) /* copyup and reopen */
027c5e7a 14542+ ) {
392086de 14543+ h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
86dc4139 14544+ if (IS_ERR(h_file))
027c5e7a 14545+ err = PTR_ERR(h_file);
86dc4139 14546+ else {
027c5e7a 14547+ di_downgrade_lock(parent, AuLock_IR);
5afbbe0d 14548+ if (dbtop > cpg.bdst)
c2b27bf2 14549+ err = au_sio_cpup_simple(&cpg);
027c5e7a
AM
14550+ if (!err)
14551+ err = au_reopen_nondir(file);
c2b27bf2 14552+ au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
027c5e7a 14553+ }
027c5e7a
AM
14554+ } else { /* copyup as wh and reopen */
14555+ /*
14556+ * since writable hfsplus branch is not supported,
14557+ * h_open_pre/post() are unnecessary.
14558+ */
c2b27bf2 14559+ err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
4a4d8108 14560+ di_downgrade_lock(parent, AuLock_IR);
4a4d8108 14561+ }
4a4d8108
AM
14562+
14563+ if (!err) {
14564+ au_pin_set_parent_lflag(pin, /*lflag*/0);
14565+ goto out_dput; /* success */
14566+ }
14567+ au_unpin(pin);
14568+ goto out_unlock;
1facf9fc 14569+
4f0767ce 14570+out_dgrade:
4a4d8108 14571+ di_downgrade_lock(parent, AuLock_IR);
4f0767ce 14572+out_unlock:
4a4d8108 14573+ di_read_unlock(parent, AuLock_IR);
4f0767ce 14574+out_dput:
4a4d8108 14575+ dput(parent);
4f0767ce 14576+out:
1facf9fc 14577+ return err;
14578+}
14579+
4a4d8108
AM
14580+/* ---------------------------------------------------------------------- */
14581+
14582+int au_do_flush(struct file *file, fl_owner_t id,
14583+ int (*flush)(struct file *file, fl_owner_t id))
1facf9fc 14584+{
4a4d8108 14585+ int err;
1facf9fc 14586+ struct super_block *sb;
4a4d8108 14587+ struct inode *inode;
1facf9fc 14588+
c06a8ce3
AM
14589+ inode = file_inode(file);
14590+ sb = inode->i_sb;
4a4d8108
AM
14591+ si_noflush_read_lock(sb);
14592+ fi_read_lock(file);
b752ccd1 14593+ ii_read_lock_child(inode);
1facf9fc 14594+
4a4d8108
AM
14595+ err = flush(file, id);
14596+ au_cpup_attr_timesizes(inode);
1facf9fc 14597+
b752ccd1 14598+ ii_read_unlock(inode);
4a4d8108 14599+ fi_read_unlock(file);
1308ab2a 14600+ si_read_unlock(sb);
dece6358 14601+ return err;
1facf9fc 14602+}
14603+
4a4d8108
AM
14604+/* ---------------------------------------------------------------------- */
14605+
14606+static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
1facf9fc 14607+{
4a4d8108 14608+ int err;
4a4d8108
AM
14609+ struct au_pin pin;
14610+ struct au_finfo *finfo;
c2b27bf2 14611+ struct dentry *parent, *hi_wh;
4a4d8108 14612+ struct inode *inode;
1facf9fc 14613+ struct super_block *sb;
c2b27bf2 14614+ struct au_cp_generic cpg = {
2000de60 14615+ .dentry = file->f_path.dentry,
c2b27bf2
AM
14616+ .bdst = -1,
14617+ .bsrc = -1,
14618+ .len = -1,
14619+ .pin = &pin,
14620+ .flags = AuCpup_DTIME
14621+ };
1facf9fc 14622+
4a4d8108
AM
14623+ FiMustWriteLock(file);
14624+
14625+ err = 0;
14626+ finfo = au_fi(file);
c2b27bf2 14627+ sb = cpg.dentry->d_sb;
5527c038 14628+ inode = d_inode(cpg.dentry);
5afbbe0d 14629+ cpg.bdst = au_ibtop(inode);
c2b27bf2 14630+ if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
1308ab2a 14631+ goto out;
dece6358 14632+
c2b27bf2
AM
14633+ parent = dget_parent(cpg.dentry);
14634+ if (au_test_ro(sb, cpg.bdst, inode)) {
4a4d8108 14635+ di_read_lock_parent(parent, !AuLock_IR);
c2b27bf2
AM
14636+ err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14637+ cpg.bdst = err;
4a4d8108
AM
14638+ di_read_unlock(parent, !AuLock_IR);
14639+ if (unlikely(err < 0))
14640+ goto out_parent;
14641+ err = 0;
1facf9fc 14642+ }
1facf9fc 14643+
4a4d8108 14644+ di_read_lock_parent(parent, AuLock_IR);
c2b27bf2 14645+ hi_wh = au_hi_wh(inode, cpg.bdst);
7f207e10
AM
14646+ if (!S_ISDIR(inode->i_mode)
14647+ && au_opt_test(au_mntflags(sb), PLINK)
4a4d8108 14648+ && au_plink_test(inode)
c2b27bf2 14649+ && !d_unhashed(cpg.dentry)
5afbbe0d 14650+ && cpg.bdst < au_dbtop(cpg.dentry)) {
c2b27bf2 14651+ err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
4a4d8108
AM
14652+ if (unlikely(err))
14653+ goto out_unlock;
14654+
14655+ /* always superio. */
c2b27bf2 14656+ err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
4a4d8108 14657+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
367653fa 14658+ if (!err) {
c2b27bf2 14659+ err = au_sio_cpup_simple(&cpg);
367653fa
AM
14660+ au_unpin(&pin);
14661+ }
4a4d8108
AM
14662+ } else if (hi_wh) {
14663+ /* already copied-up after unlink */
c2b27bf2 14664+ err = au_reopen_wh(file, cpg.bdst, hi_wh);
4a4d8108
AM
14665+ *need_reopen = 0;
14666+ }
1facf9fc 14667+
4f0767ce 14668+out_unlock:
4a4d8108 14669+ di_read_unlock(parent, AuLock_IR);
4f0767ce 14670+out_parent:
4a4d8108 14671+ dput(parent);
4f0767ce 14672+out:
1308ab2a 14673+ return err;
dece6358 14674+}
1facf9fc 14675+
4a4d8108 14676+static void au_do_refresh_dir(struct file *file)
dece6358 14677+{
5afbbe0d 14678+ aufs_bindex_t bindex, bbot, new_bindex, brid;
4a4d8108
AM
14679+ struct au_hfile *p, tmp, *q;
14680+ struct au_finfo *finfo;
1308ab2a 14681+ struct super_block *sb;
4a4d8108 14682+ struct au_fidir *fidir;
1facf9fc 14683+
4a4d8108 14684+ FiMustWriteLock(file);
1facf9fc 14685+
2000de60 14686+ sb = file->f_path.dentry->d_sb;
4a4d8108
AM
14687+ finfo = au_fi(file);
14688+ fidir = finfo->fi_hdir;
14689+ AuDebugOn(!fidir);
14690+ p = fidir->fd_hfile + finfo->fi_btop;
14691+ brid = p->hf_br->br_id;
5afbbe0d
AM
14692+ bbot = fidir->fd_bbot;
14693+ for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
4a4d8108
AM
14694+ if (!p->hf_file)
14695+ continue;
1308ab2a 14696+
4a4d8108
AM
14697+ new_bindex = au_br_index(sb, p->hf_br->br_id);
14698+ if (new_bindex == bindex)
14699+ continue;
14700+ if (new_bindex < 0) {
14701+ au_set_h_fptr(file, bindex, NULL);
14702+ continue;
14703+ }
1308ab2a 14704+
4a4d8108
AM
14705+ /* swap two lower inode, and loop again */
14706+ q = fidir->fd_hfile + new_bindex;
14707+ tmp = *q;
14708+ *q = *p;
14709+ *p = tmp;
14710+ if (tmp.hf_file) {
14711+ bindex--;
14712+ p--;
14713+ }
14714+ }
1308ab2a 14715+
4a4d8108 14716+ p = fidir->fd_hfile;
2000de60 14717+ if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
5afbbe0d
AM
14718+ bbot = au_sbbot(sb);
14719+ for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
4a4d8108
AM
14720+ finfo->fi_btop++, p++)
14721+ if (p->hf_file) {
c06a8ce3 14722+ if (file_inode(p->hf_file))
4a4d8108 14723+ break;
1c60b727 14724+ au_hfput(p, /*execed*/0);
4a4d8108
AM
14725+ }
14726+ } else {
5afbbe0d
AM
14727+ bbot = au_br_index(sb, brid);
14728+ for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
4a4d8108
AM
14729+ finfo->fi_btop++, p++)
14730+ if (p->hf_file)
1c60b727 14731+ au_hfput(p, /*execed*/0);
5afbbe0d 14732+ bbot = au_sbbot(sb);
4a4d8108 14733+ }
1308ab2a 14734+
5afbbe0d
AM
14735+ p = fidir->fd_hfile + bbot;
14736+ for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
4a4d8108
AM
14737+ fidir->fd_bbot--, p--)
14738+ if (p->hf_file) {
c06a8ce3 14739+ if (file_inode(p->hf_file))
4a4d8108 14740+ break;
1c60b727 14741+ au_hfput(p, /*execed*/0);
4a4d8108
AM
14742+ }
14743+ AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
1308ab2a 14744+}
14745+
4a4d8108
AM
14746+/*
14747+ * after branch manipulating, refresh the file.
14748+ */
14749+static int refresh_file(struct file *file, int (*reopen)(struct file *file))
1facf9fc 14750+{
e2f27e51 14751+ int err, need_reopen, nbr;
5afbbe0d 14752+ aufs_bindex_t bbot, bindex;
4a4d8108 14753+ struct dentry *dentry;
e2f27e51 14754+ struct super_block *sb;
1308ab2a 14755+ struct au_finfo *finfo;
4a4d8108 14756+ struct au_hfile *hfile;
1facf9fc 14757+
2000de60 14758+ dentry = file->f_path.dentry;
e2f27e51
AM
14759+ sb = dentry->d_sb;
14760+ nbr = au_sbbot(sb) + 1;
1308ab2a 14761+ finfo = au_fi(file);
4a4d8108
AM
14762+ if (!finfo->fi_hdir) {
14763+ hfile = &finfo->fi_htop;
14764+ AuDebugOn(!hfile->hf_file);
e2f27e51 14765+ bindex = au_br_index(sb, hfile->hf_br->br_id);
4a4d8108
AM
14766+ AuDebugOn(bindex < 0);
14767+ if (bindex != finfo->fi_btop)
5afbbe0d 14768+ au_set_fbtop(file, bindex);
4a4d8108 14769+ } else {
e2f27e51 14770+ err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
4a4d8108
AM
14771+ if (unlikely(err))
14772+ goto out;
14773+ au_do_refresh_dir(file);
14774+ }
1facf9fc 14775+
4a4d8108
AM
14776+ err = 0;
14777+ need_reopen = 1;
14778+ if (!au_test_mmapped(file))
14779+ err = au_file_refresh_by_inode(file, &need_reopen);
e2f27e51
AM
14780+ if (finfo->fi_hdir)
14781+ /* harmless if err */
14782+ au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
027c5e7a 14783+ if (!err && need_reopen && !d_unlinked(dentry))
4a4d8108
AM
14784+ err = reopen(file);
14785+ if (!err) {
14786+ au_update_figen(file);
14787+ goto out; /* success */
14788+ }
14789+
14790+ /* error, close all lower files */
14791+ if (finfo->fi_hdir) {
5afbbe0d
AM
14792+ bbot = au_fbbot_dir(file);
14793+ for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
4a4d8108
AM
14794+ au_set_h_fptr(file, bindex, NULL);
14795+ }
1facf9fc 14796+
4f0767ce 14797+out:
1facf9fc 14798+ return err;
14799+}
14800+
4a4d8108
AM
14801+/* common function to regular file and dir */
14802+int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
521ced18 14803+ int wlock, unsigned int fi_lsc)
dece6358 14804+{
1308ab2a 14805+ int err;
4a4d8108 14806+ unsigned int sigen, figen;
5afbbe0d 14807+ aufs_bindex_t btop;
4a4d8108
AM
14808+ unsigned char pseudo_link;
14809+ struct dentry *dentry;
14810+ struct inode *inode;
1facf9fc 14811+
4a4d8108 14812+ err = 0;
2000de60 14813+ dentry = file->f_path.dentry;
5527c038 14814+ inode = d_inode(dentry);
4a4d8108 14815+ sigen = au_sigen(dentry->d_sb);
521ced18 14816+ fi_write_lock_nested(file, fi_lsc);
4a4d8108 14817+ figen = au_figen(file);
521ced18
JR
14818+ if (!fi_lsc)
14819+ di_write_lock_child(dentry);
14820+ else
14821+ di_write_lock_child2(dentry);
5afbbe0d
AM
14822+ btop = au_dbtop(dentry);
14823+ pseudo_link = (btop != au_ibtop(inode));
14824+ if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
4a4d8108
AM
14825+ if (!wlock) {
14826+ di_downgrade_lock(dentry, AuLock_IR);
14827+ fi_downgrade_lock(file);
14828+ }
14829+ goto out; /* success */
14830+ }
dece6358 14831+
4a4d8108 14832+ AuDbg("sigen %d, figen %d\n", sigen, figen);
027c5e7a 14833+ if (au_digen_test(dentry, sigen)) {
4a4d8108 14834+ err = au_reval_dpath(dentry, sigen);
027c5e7a 14835+ AuDebugOn(!err && au_digen_test(dentry, sigen));
4a4d8108 14836+ }
dece6358 14837+
027c5e7a
AM
14838+ if (!err)
14839+ err = refresh_file(file, reopen);
4a4d8108
AM
14840+ if (!err) {
14841+ if (!wlock) {
14842+ di_downgrade_lock(dentry, AuLock_IR);
14843+ fi_downgrade_lock(file);
14844+ }
14845+ } else {
14846+ di_write_unlock(dentry);
14847+ fi_write_unlock(file);
14848+ }
1facf9fc 14849+
4f0767ce 14850+out:
1308ab2a 14851+ return err;
14852+}
1facf9fc 14853+
4a4d8108
AM
14854+/* ---------------------------------------------------------------------- */
14855+
14856+/* cf. aufs_nopage() */
14857+/* for madvise(2) */
14858+static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
1308ab2a 14859+{
4a4d8108
AM
14860+ unlock_page(page);
14861+ return 0;
14862+}
1facf9fc 14863+
4a4d8108 14864+/* it will never be called, but necessary to support O_DIRECT */
5afbbe0d 14865+static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
4a4d8108 14866+{ BUG(); return 0; }
1facf9fc 14867+
4a4d8108
AM
14868+/* they will never be called. */
14869+#ifdef CONFIG_AUFS_DEBUG
14870+static int aufs_write_begin(struct file *file, struct address_space *mapping,
14871+ loff_t pos, unsigned len, unsigned flags,
14872+ struct page **pagep, void **fsdata)
14873+{ AuUnsupport(); return 0; }
14874+static int aufs_write_end(struct file *file, struct address_space *mapping,
14875+ loff_t pos, unsigned len, unsigned copied,
14876+ struct page *page, void *fsdata)
14877+{ AuUnsupport(); return 0; }
14878+static int aufs_writepage(struct page *page, struct writeback_control *wbc)
14879+{ AuUnsupport(); return 0; }
1308ab2a 14880+
4a4d8108
AM
14881+static int aufs_set_page_dirty(struct page *page)
14882+{ AuUnsupport(); return 0; }
392086de
AM
14883+static void aufs_invalidatepage(struct page *page, unsigned int offset,
14884+ unsigned int length)
4a4d8108
AM
14885+{ AuUnsupport(); }
14886+static int aufs_releasepage(struct page *page, gfp_t gfp)
14887+{ AuUnsupport(); return 0; }
79b8bda9 14888+#if 0 /* called by memory compaction regardless file */
4a4d8108 14889+static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
7eafdf33 14890+ struct page *page, enum migrate_mode mode)
4a4d8108 14891+{ AuUnsupport(); return 0; }
79b8bda9 14892+#endif
e2f27e51
AM
14893+static bool aufs_isolate_page(struct page *page, isolate_mode_t mode)
14894+{ AuUnsupport(); return true; }
14895+static void aufs_putback_page(struct page *page)
14896+{ AuUnsupport(); }
4a4d8108
AM
14897+static int aufs_launder_page(struct page *page)
14898+{ AuUnsupport(); return 0; }
14899+static int aufs_is_partially_uptodate(struct page *page,
38d290e6
JR
14900+ unsigned long from,
14901+ unsigned long count)
4a4d8108 14902+{ AuUnsupport(); return 0; }
392086de
AM
14903+static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
14904+ bool *writeback)
14905+{ AuUnsupport(); }
4a4d8108
AM
14906+static int aufs_error_remove_page(struct address_space *mapping,
14907+ struct page *page)
14908+{ AuUnsupport(); return 0; }
b4510431
AM
14909+static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
14910+ sector_t *span)
14911+{ AuUnsupport(); return 0; }
14912+static void aufs_swap_deactivate(struct file *file)
14913+{ AuUnsupport(); }
4a4d8108
AM
14914+#endif /* CONFIG_AUFS_DEBUG */
14915+
14916+const struct address_space_operations aufs_aop = {
14917+ .readpage = aufs_readpage,
14918+ .direct_IO = aufs_direct_IO,
4a4d8108
AM
14919+#ifdef CONFIG_AUFS_DEBUG
14920+ .writepage = aufs_writepage,
4a4d8108
AM
14921+ /* no writepages, because of writepage */
14922+ .set_page_dirty = aufs_set_page_dirty,
14923+ /* no readpages, because of readpage */
14924+ .write_begin = aufs_write_begin,
14925+ .write_end = aufs_write_end,
14926+ /* no bmap, no block device */
14927+ .invalidatepage = aufs_invalidatepage,
14928+ .releasepage = aufs_releasepage,
79b8bda9
AM
14929+ /* is fallback_migrate_page ok? */
14930+ /* .migratepage = aufs_migratepage, */
e2f27e51
AM
14931+ .isolate_page = aufs_isolate_page,
14932+ .putback_page = aufs_putback_page,
4a4d8108
AM
14933+ .launder_page = aufs_launder_page,
14934+ .is_partially_uptodate = aufs_is_partially_uptodate,
392086de 14935+ .is_dirty_writeback = aufs_is_dirty_writeback,
b4510431
AM
14936+ .error_remove_page = aufs_error_remove_page,
14937+ .swap_activate = aufs_swap_activate,
14938+ .swap_deactivate = aufs_swap_deactivate
4a4d8108 14939+#endif /* CONFIG_AUFS_DEBUG */
dece6358 14940+};
7f207e10 14941diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
eca34b5c 14942--- /usr/share/empty/fs/aufs/file.h 1970-01-01 01:00:00.000000000 +0100
46016270 14943+++ linux/fs/aufs/file.h 2021-02-24 13:33:42.744347058 +0100
9f237c51 14944@@ -0,0 +1,342 @@
062440b3 14945+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 14946+/*
d58c55f2 14947+ * Copyright (C) 2005-2020 Junjiro R. Okajima
4a4d8108
AM
14948+ *
14949+ * This program, aufs is free software; you can redistribute it and/or modify
14950+ * it under the terms of the GNU General Public License as published by
14951+ * the Free Software Foundation; either version 2 of the License, or
14952+ * (at your option) any later version.
14953+ *
14954+ * This program is distributed in the hope that it will be useful,
14955+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14956+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14957+ * GNU General Public License for more details.
14958+ *
14959+ * You should have received a copy of the GNU General Public License
523b37e3 14960+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 14961+ */
1facf9fc 14962+
4a4d8108
AM
14963+/*
14964+ * file operations
14965+ */
1facf9fc 14966+
4a4d8108
AM
14967+#ifndef __AUFS_FILE_H__
14968+#define __AUFS_FILE_H__
1facf9fc 14969+
4a4d8108 14970+#ifdef __KERNEL__
1facf9fc 14971+
2cbb1c4b 14972+#include <linux/file.h>
4a4d8108 14973+#include <linux/fs.h>
3c1bdaff 14974+#include <linux/mm_types.h>
4a4d8108 14975+#include <linux/poll.h>
4a4d8108 14976+#include "rwsem.h"
1facf9fc 14977+
4a4d8108
AM
14978+struct au_branch;
14979+struct au_hfile {
14980+ struct file *hf_file;
14981+ struct au_branch *hf_br;
14982+};
1facf9fc 14983+
4a4d8108
AM
14984+struct au_vdir;
14985+struct au_fidir {
14986+ aufs_bindex_t fd_bbot;
14987+ aufs_bindex_t fd_nent;
14988+ struct au_vdir *fd_vdir_cache;
14989+ struct au_hfile fd_hfile[];
14990+};
1facf9fc 14991+
4a4d8108 14992+static inline int au_fidir_sz(int nent)
dece6358 14993+{
4f0767ce
JR
14994+ AuDebugOn(nent < 0);
14995+ return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
4a4d8108 14996+}
1facf9fc 14997+
4a4d8108
AM
14998+struct au_finfo {
14999+ atomic_t fi_generation;
dece6358 15000+
4a4d8108
AM
15001+ struct au_rwsem fi_rwsem;
15002+ aufs_bindex_t fi_btop;
15003+
15004+ /* do not union them */
15005+ struct { /* for non-dir */
15006+ struct au_hfile fi_htop;
2cbb1c4b 15007+ atomic_t fi_mmapped;
4a4d8108
AM
15008+ };
15009+ struct au_fidir *fi_hdir; /* for dir only */
523b37e3 15010+
8b6a4947 15011+ struct hlist_bl_node fi_hlist;
1c60b727 15012+ struct file *fi_file; /* very ugly */
9f237c51 15013+ struct rcu_head rcu;
4a4d8108 15014+} ____cacheline_aligned_in_smp;
1facf9fc 15015+
4a4d8108 15016+/* ---------------------------------------------------------------------- */
1facf9fc 15017+
4a4d8108
AM
15018+/* file.c */
15019+extern const struct address_space_operations aufs_aop;
15020+unsigned int au_file_roflags(unsigned int flags);
15021+struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
392086de 15022+ struct file *file, int force_wr);
b912730e 15023+struct au_do_open_args {
8b6a4947 15024+ int aopen;
b912730e
AM
15025+ int (*open)(struct file *file, int flags,
15026+ struct file *h_file);
15027+ struct au_fidir *fidir;
15028+ struct file *h_file;
15029+};
15030+int au_do_open(struct file *file, struct au_do_open_args *args);
4a4d8108
AM
15031+int au_reopen_nondir(struct file *file);
15032+struct au_pin;
15033+int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
15034+int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
521ced18 15035+ int wlock, unsigned int fi_lsc);
4a4d8108
AM
15036+int au_do_flush(struct file *file, fl_owner_t id,
15037+ int (*flush)(struct file *file, fl_owner_t id));
1facf9fc 15038+
4a4d8108
AM
15039+/* poll.c */
15040+#ifdef CONFIG_AUFS_POLL
cd7a4cd9 15041+__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt);
4a4d8108 15042+#endif
1facf9fc 15043+
4a4d8108
AM
15044+#ifdef CONFIG_AUFS_BR_HFSPLUS
15045+/* hfsplus.c */
392086de
AM
15046+struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
15047+ int force_wr);
4a4d8108
AM
15048+void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
15049+ struct file *h_file);
15050+#else
c1595e42
JR
15051+AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
15052+ aufs_bindex_t bindex, int force_wr)
4a4d8108
AM
15053+AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
15054+ struct file *h_file);
15055+#endif
1facf9fc 15056+
4a4d8108
AM
15057+/* f_op.c */
15058+extern const struct file_operations aufs_file_fop;
b912730e 15059+int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
4a4d8108 15060+int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
521ced18 15061+struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
4a4d8108 15062+
4a4d8108 15063+/* finfo.c */
f0c0a007 15064+void au_hfput(struct au_hfile *hf, int execed);
4a4d8108
AM
15065+void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
15066+ struct file *h_file);
1facf9fc 15067+
4a4d8108 15068+void au_update_figen(struct file *file);
4a4d8108 15069+struct au_fidir *au_fidir_alloc(struct super_block *sb);
e2f27e51 15070+int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
1facf9fc 15071+
4a4d8108 15072+void au_fi_init_once(void *_fi);
1c60b727 15073+void au_finfo_fin(struct file *file);
4a4d8108 15074+int au_finfo_init(struct file *file, struct au_fidir *fidir);
1facf9fc 15075+
4a4d8108
AM
15076+/* ioctl.c */
15077+long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
b752ccd1
AM
15078+#ifdef CONFIG_COMPAT
15079+long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
15080+ unsigned long arg);
c2b27bf2
AM
15081+long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
15082+ unsigned long arg);
b752ccd1 15083+#endif
1facf9fc 15084+
4a4d8108 15085+/* ---------------------------------------------------------------------- */
1facf9fc 15086+
4a4d8108
AM
15087+static inline struct au_finfo *au_fi(struct file *file)
15088+{
38d290e6 15089+ return file->private_data;
4a4d8108 15090+}
1facf9fc 15091+
4a4d8108 15092+/* ---------------------------------------------------------------------- */
1facf9fc 15093+
8b6a4947
AM
15094+#define fi_read_lock(f) au_rw_read_lock(&au_fi(f)->fi_rwsem)
15095+#define fi_write_lock(f) au_rw_write_lock(&au_fi(f)->fi_rwsem)
15096+#define fi_read_trylock(f) au_rw_read_trylock(&au_fi(f)->fi_rwsem)
15097+#define fi_write_trylock(f) au_rw_write_trylock(&au_fi(f)->fi_rwsem)
4a4d8108 15098+/*
8b6a4947
AM
15099+#define fi_read_trylock_nested(f) \
15100+ au_rw_read_trylock_nested(&au_fi(f)->fi_rwsem)
15101+#define fi_write_trylock_nested(f) \
15102+ au_rw_write_trylock_nested(&au_fi(f)->fi_rwsem)
15103+*/
15104+
15105+#define fi_read_unlock(f) au_rw_read_unlock(&au_fi(f)->fi_rwsem)
15106+#define fi_write_unlock(f) au_rw_write_unlock(&au_fi(f)->fi_rwsem)
15107+#define fi_downgrade_lock(f) au_rw_dgrade_lock(&au_fi(f)->fi_rwsem)
1308ab2a 15108+
521ced18
JR
15109+/* lock subclass for finfo */
15110+enum {
15111+ AuLsc_FI_1,
15112+ AuLsc_FI_2
15113+};
15114+
15115+static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
15116+{
15117+ au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15118+}
15119+
15120+static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
15121+{
15122+ au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15123+}
15124+
15125+/*
15126+ * fi_read_lock_1, fi_write_lock_1,
15127+ * fi_read_lock_2, fi_write_lock_2
15128+ */
15129+#define AuReadLockFunc(name) \
15130+static inline void fi_read_lock_##name(struct file *f) \
15131+{ fi_read_lock_nested(f, AuLsc_FI_##name); }
15132+
15133+#define AuWriteLockFunc(name) \
15134+static inline void fi_write_lock_##name(struct file *f) \
15135+{ fi_write_lock_nested(f, AuLsc_FI_##name); }
15136+
15137+#define AuRWLockFuncs(name) \
15138+ AuReadLockFunc(name) \
15139+ AuWriteLockFunc(name)
15140+
15141+AuRWLockFuncs(1);
15142+AuRWLockFuncs(2);
15143+
15144+#undef AuReadLockFunc
15145+#undef AuWriteLockFunc
15146+#undef AuRWLockFuncs
15147+
4a4d8108
AM
15148+#define FiMustNoWaiters(f) AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
15149+#define FiMustAnyLock(f) AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
15150+#define FiMustWriteLock(f) AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
1facf9fc 15151+
1308ab2a 15152+/* ---------------------------------------------------------------------- */
15153+
4a4d8108 15154+/* todo: hard/soft set? */
5afbbe0d 15155+static inline aufs_bindex_t au_fbtop(struct file *file)
dece6358 15156+{
4a4d8108
AM
15157+ FiMustAnyLock(file);
15158+ return au_fi(file)->fi_btop;
15159+}
dece6358 15160+
5afbbe0d 15161+static inline aufs_bindex_t au_fbbot_dir(struct file *file)
4a4d8108
AM
15162+{
15163+ FiMustAnyLock(file);
15164+ AuDebugOn(!au_fi(file)->fi_hdir);
15165+ return au_fi(file)->fi_hdir->fd_bbot;
15166+}
1facf9fc 15167+
4a4d8108
AM
15168+static inline struct au_vdir *au_fvdir_cache(struct file *file)
15169+{
15170+ FiMustAnyLock(file);
15171+ AuDebugOn(!au_fi(file)->fi_hdir);
15172+ return au_fi(file)->fi_hdir->fd_vdir_cache;
15173+}
1facf9fc 15174+
5afbbe0d 15175+static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
4a4d8108
AM
15176+{
15177+ FiMustWriteLock(file);
15178+ au_fi(file)->fi_btop = bindex;
15179+}
1facf9fc 15180+
5afbbe0d 15181+static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
4a4d8108
AM
15182+{
15183+ FiMustWriteLock(file);
15184+ AuDebugOn(!au_fi(file)->fi_hdir);
15185+ au_fi(file)->fi_hdir->fd_bbot = bindex;
15186+}
1308ab2a 15187+
4a4d8108
AM
15188+static inline void au_set_fvdir_cache(struct file *file,
15189+ struct au_vdir *vdir_cache)
15190+{
15191+ FiMustWriteLock(file);
15192+ AuDebugOn(!au_fi(file)->fi_hdir);
15193+ au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
15194+}
dece6358 15195+
4a4d8108
AM
15196+static inline struct file *au_hf_top(struct file *file)
15197+{
15198+ FiMustAnyLock(file);
15199+ AuDebugOn(au_fi(file)->fi_hdir);
15200+ return au_fi(file)->fi_htop.hf_file;
15201+}
1facf9fc 15202+
4a4d8108
AM
15203+static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
15204+{
15205+ FiMustAnyLock(file);
15206+ AuDebugOn(!au_fi(file)->fi_hdir);
15207+ return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
dece6358
AM
15208+}
15209+
4a4d8108
AM
15210+/* todo: memory barrier? */
15211+static inline unsigned int au_figen(struct file *f)
dece6358 15212+{
4a4d8108
AM
15213+ return atomic_read(&au_fi(f)->fi_generation);
15214+}
dece6358 15215+
2cbb1c4b
JR
15216+static inline void au_set_mmapped(struct file *f)
15217+{
15218+ if (atomic_inc_return(&au_fi(f)->fi_mmapped))
15219+ return;
0c3ec466 15220+ pr_warn("fi_mmapped wrapped around\n");
2cbb1c4b
JR
15221+ while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
15222+ ;
15223+}
15224+
15225+static inline void au_unset_mmapped(struct file *f)
15226+{
15227+ atomic_dec(&au_fi(f)->fi_mmapped);
15228+}
15229+
4a4d8108
AM
15230+static inline int au_test_mmapped(struct file *f)
15231+{
2cbb1c4b
JR
15232+ return atomic_read(&au_fi(f)->fi_mmapped);
15233+}
15234+
15235+/* customize vma->vm_file */
15236+
15237+static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
15238+ struct file *file)
15239+{
53392da6
AM
15240+ struct file *f;
15241+
15242+ f = vma->vm_file;
2cbb1c4b
JR
15243+ get_file(file);
15244+ vma->vm_file = file;
53392da6 15245+ fput(f);
2cbb1c4b
JR
15246+}
15247+
15248+#ifdef CONFIG_MMU
15249+#define AuDbgVmRegion(file, vma) do {} while (0)
15250+
15251+static inline void au_vm_file_reset(struct vm_area_struct *vma,
15252+ struct file *file)
15253+{
15254+ au_do_vm_file_reset(vma, file);
15255+}
15256+#else
15257+#define AuDbgVmRegion(file, vma) \
15258+ AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
15259+
15260+static inline void au_vm_file_reset(struct vm_area_struct *vma,
15261+ struct file *file)
15262+{
53392da6
AM
15263+ struct file *f;
15264+
2cbb1c4b 15265+ au_do_vm_file_reset(vma, file);
53392da6 15266+ f = vma->vm_region->vm_file;
2cbb1c4b
JR
15267+ get_file(file);
15268+ vma->vm_region->vm_file = file;
53392da6 15269+ fput(f);
2cbb1c4b
JR
15270+}
15271+#endif /* CONFIG_MMU */
15272+
15273+/* handle vma->vm_prfile */
fb47a38f 15274+static inline void au_vm_prfile_set(struct vm_area_struct *vma,
2cbb1c4b
JR
15275+ struct file *file)
15276+{
2cbb1c4b
JR
15277+ get_file(file);
15278+ vma->vm_prfile = file;
15279+#ifndef CONFIG_MMU
15280+ get_file(file);
15281+ vma->vm_region->vm_prfile = file;
15282+#endif
fb47a38f 15283+}
1308ab2a 15284+
4a4d8108
AM
15285+#endif /* __KERNEL__ */
15286+#endif /* __AUFS_FILE_H__ */
7f207e10 15287diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
eca34b5c 15288--- /usr/share/empty/fs/aufs/finfo.c 1970-01-01 01:00:00.000000000 +0100
46016270 15289+++ linux/fs/aufs/finfo.c 2021-02-24 13:33:42.744347058 +0100
062440b3 15290@@ -0,0 +1,149 @@
cd7a4cd9 15291+// SPDX-License-Identifier: GPL-2.0
4a4d8108 15292+/*
d58c55f2 15293+ * Copyright (C) 2005-2020 Junjiro R. Okajima
4a4d8108
AM
15294+ *
15295+ * This program, aufs is free software; you can redistribute it and/or modify
15296+ * it under the terms of the GNU General Public License as published by
15297+ * the Free Software Foundation; either version 2 of the License, or
15298+ * (at your option) any later version.
15299+ *
15300+ * This program is distributed in the hope that it will be useful,
15301+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15302+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15303+ * GNU General Public License for more details.
15304+ *
15305+ * You should have received a copy of the GNU General Public License
523b37e3 15306+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 15307+ */
1308ab2a 15308+
4a4d8108
AM
15309+/*
15310+ * file private data
15311+ */
1facf9fc 15312+
4a4d8108 15313+#include "aufs.h"
1facf9fc 15314+
f0c0a007 15315+void au_hfput(struct au_hfile *hf, int execed)
4a4d8108 15316+{
f0c0a007 15317+ if (execed)
4a4d8108
AM
15318+ allow_write_access(hf->hf_file);
15319+ fput(hf->hf_file);
15320+ hf->hf_file = NULL;
acd2b654 15321+ au_lcnt_dec(&hf->hf_br->br_nfiles);
4a4d8108
AM
15322+ hf->hf_br = NULL;
15323+}
1facf9fc 15324+
4a4d8108
AM
15325+void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
15326+{
15327+ struct au_finfo *finfo = au_fi(file);
15328+ struct au_hfile *hf;
15329+ struct au_fidir *fidir;
15330+
15331+ fidir = finfo->fi_hdir;
15332+ if (!fidir) {
15333+ AuDebugOn(finfo->fi_btop != bindex);
15334+ hf = &finfo->fi_htop;
15335+ } else
15336+ hf = fidir->fd_hfile + bindex;
15337+
15338+ if (hf && hf->hf_file)
f0c0a007 15339+ au_hfput(hf, vfsub_file_execed(file));
4a4d8108
AM
15340+ if (val) {
15341+ FiMustWriteLock(file);
b912730e 15342+ AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
4a4d8108 15343+ hf->hf_file = val;
2000de60 15344+ hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
1308ab2a 15345+ }
4a4d8108 15346+}
1facf9fc 15347+
4a4d8108
AM
15348+void au_update_figen(struct file *file)
15349+{
2000de60 15350+ atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
4a4d8108 15351+ /* smp_mb(); */ /* atomic_set */
1facf9fc 15352+}
15353+
4a4d8108
AM
15354+/* ---------------------------------------------------------------------- */
15355+
4a4d8108
AM
15356+struct au_fidir *au_fidir_alloc(struct super_block *sb)
15357+{
15358+ struct au_fidir *fidir;
15359+ int nbr;
15360+
5afbbe0d 15361+ nbr = au_sbbot(sb) + 1;
4a4d8108
AM
15362+ if (nbr < 2)
15363+ nbr = 2; /* initial allocate for 2 branches */
15364+ fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
15365+ if (fidir) {
15366+ fidir->fd_bbot = -1;
15367+ fidir->fd_nent = nbr;
4a4d8108
AM
15368+ }
15369+
15370+ return fidir;
15371+}
15372+
e2f27e51 15373+int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
4a4d8108
AM
15374+{
15375+ int err;
15376+ struct au_fidir *fidir, *p;
15377+
15378+ AuRwMustWriteLock(&finfo->fi_rwsem);
15379+ fidir = finfo->fi_hdir;
15380+ AuDebugOn(!fidir);
15381+
15382+ err = -ENOMEM;
15383+ p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
e2f27e51 15384+ GFP_NOFS, may_shrink);
4a4d8108
AM
15385+ if (p) {
15386+ p->fd_nent = nbr;
15387+ finfo->fi_hdir = p;
15388+ err = 0;
15389+ }
1facf9fc 15390+
dece6358 15391+ return err;
1facf9fc 15392+}
1308ab2a 15393+
15394+/* ---------------------------------------------------------------------- */
15395+
1c60b727 15396+void au_finfo_fin(struct file *file)
1308ab2a 15397+{
4a4d8108
AM
15398+ struct au_finfo *finfo;
15399+
acd2b654 15400+ au_lcnt_dec(&au_sbi(file->f_path.dentry->d_sb)->si_nfiles);
7f207e10 15401+
4a4d8108
AM
15402+ finfo = au_fi(file);
15403+ AuDebugOn(finfo->fi_hdir);
15404+ AuRwDestroy(&finfo->fi_rwsem);
1c60b727 15405+ au_cache_free_finfo(finfo);
1308ab2a 15406+}
1308ab2a 15407+
e49829fe 15408+void au_fi_init_once(void *_finfo)
4a4d8108 15409+{
e49829fe 15410+ struct au_finfo *finfo = _finfo;
1308ab2a 15411+
e49829fe 15412+ au_rw_init(&finfo->fi_rwsem);
4a4d8108 15413+}
1308ab2a 15414+
4a4d8108
AM
15415+int au_finfo_init(struct file *file, struct au_fidir *fidir)
15416+{
1716fcea 15417+ int err;
4a4d8108
AM
15418+ struct au_finfo *finfo;
15419+ struct dentry *dentry;
15420+
15421+ err = -ENOMEM;
2000de60 15422+ dentry = file->f_path.dentry;
4a4d8108
AM
15423+ finfo = au_cache_alloc_finfo();
15424+ if (unlikely(!finfo))
15425+ goto out;
15426+
15427+ err = 0;
acd2b654 15428+ au_lcnt_inc(&au_sbi(dentry->d_sb)->si_nfiles);
4a4d8108
AM
15429+ au_rw_write_lock(&finfo->fi_rwsem);
15430+ finfo->fi_btop = -1;
15431+ finfo->fi_hdir = fidir;
15432+ atomic_set(&finfo->fi_generation, au_digen(dentry));
15433+ /* smp_mb(); */ /* atomic_set */
15434+
15435+ file->private_data = finfo;
15436+
15437+out:
15438+ return err;
15439+}
7f207e10 15440diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
eca34b5c 15441--- /usr/share/empty/fs/aufs/f_op.c 1970-01-01 01:00:00.000000000 +0100
46016270 15442+++ linux/fs/aufs/f_op.c 2021-06-30 21:35:11.393873211 +0200
0b2a12c6 15443@@ -0,0 +1,771 @@
cd7a4cd9 15444+// SPDX-License-Identifier: GPL-2.0
dece6358 15445+/*
d58c55f2 15446+ * Copyright (C) 2005-2020 Junjiro R. Okajima
dece6358
AM
15447+ *
15448+ * This program, aufs is free software; you can redistribute it and/or modify
15449+ * it under the terms of the GNU General Public License as published by
15450+ * the Free Software Foundation; either version 2 of the License, or
15451+ * (at your option) any later version.
15452+ *
15453+ * This program is distributed in the hope that it will be useful,
15454+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15455+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15456+ * GNU General Public License for more details.
15457+ *
15458+ * You should have received a copy of the GNU General Public License
523b37e3 15459+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358 15460+ */
1facf9fc 15461+
15462+/*
4a4d8108 15463+ * file and vm operations
1facf9fc 15464+ */
dece6358 15465+
86dc4139 15466+#include <linux/aio.h>
4a4d8108
AM
15467+#include <linux/fs_stack.h>
15468+#include <linux/mman.h>
4a4d8108 15469+#include <linux/security.h>
dece6358
AM
15470+#include "aufs.h"
15471+
b912730e 15472+int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
1facf9fc 15473+{
4a4d8108
AM
15474+ int err;
15475+ aufs_bindex_t bindex;
8cdd5066 15476+ struct dentry *dentry, *h_dentry;
4a4d8108 15477+ struct au_finfo *finfo;
38d290e6 15478+ struct inode *h_inode;
4a4d8108
AM
15479+
15480+ FiMustWriteLock(file);
15481+
523b37e3 15482+ err = 0;
2000de60 15483+ dentry = file->f_path.dentry;
b912730e 15484+ AuDebugOn(IS_ERR_OR_NULL(dentry));
4a4d8108
AM
15485+ finfo = au_fi(file);
15486+ memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
2cbb1c4b 15487+ atomic_set(&finfo->fi_mmapped, 0);
5afbbe0d 15488+ bindex = au_dbtop(dentry);
8cdd5066
JR
15489+ if (!h_file) {
15490+ h_dentry = au_h_dptr(dentry, bindex);
15491+ err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15492+ if (unlikely(err))
15493+ goto out;
b912730e 15494+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
acd2b654
AM
15495+ if (IS_ERR(h_file)) {
15496+ err = PTR_ERR(h_file);
15497+ goto out;
15498+ }
8cdd5066
JR
15499+ } else {
15500+ h_dentry = h_file->f_path.dentry;
15501+ err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15502+ if (unlikely(err))
15503+ goto out;
acd2b654 15504+ /* br ref is already inc-ed */
8cdd5066 15505+ }
acd2b654
AM
15506+
15507+ if ((flags & __O_TMPFILE)
15508+ && !(flags & O_EXCL)) {
15509+ h_inode = file_inode(h_file);
15510+ spin_lock(&h_inode->i_lock);
15511+ h_inode->i_state |= I_LINKABLE;
15512+ spin_unlock(&h_inode->i_lock);
4a4d8108 15513+ }
acd2b654
AM
15514+ au_set_fbtop(file, bindex);
15515+ au_set_h_fptr(file, bindex, h_file);
15516+ au_update_figen(file);
15517+ /* todo: necessary? */
15518+ /* file->f_ra = h_file->f_ra; */
027c5e7a 15519+
8cdd5066 15520+out:
4a4d8108 15521+ return err;
1facf9fc 15522+}
15523+
4a4d8108
AM
15524+static int aufs_open_nondir(struct inode *inode __maybe_unused,
15525+ struct file *file)
1facf9fc 15526+{
4a4d8108 15527+ int err;
1308ab2a 15528+ struct super_block *sb;
b912730e
AM
15529+ struct au_do_open_args args = {
15530+ .open = au_do_open_nondir
15531+ };
1facf9fc 15532+
523b37e3
AM
15533+ AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
15534+ file, vfsub_file_flags(file), file->f_mode);
1facf9fc 15535+
2000de60 15536+ sb = file->f_path.dentry->d_sb;
4a4d8108 15537+ si_read_lock(sb, AuLock_FLUSH);
b912730e 15538+ err = au_do_open(file, &args);
4a4d8108
AM
15539+ si_read_unlock(sb);
15540+ return err;
15541+}
1facf9fc 15542+
4a4d8108
AM
15543+int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
15544+{
15545+ struct au_finfo *finfo;
15546+ aufs_bindex_t bindex;
1facf9fc 15547+
4a4d8108 15548+ finfo = au_fi(file);
8b6a4947
AM
15549+ au_hbl_del(&finfo->fi_hlist,
15550+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
4a4d8108 15551+ bindex = finfo->fi_btop;
b4510431 15552+ if (bindex >= 0)
4a4d8108 15553+ au_set_h_fptr(file, bindex, NULL);
7f207e10 15554+
1c60b727 15555+ au_finfo_fin(file);
4a4d8108 15556+ return 0;
1facf9fc 15557+}
15558+
4a4d8108
AM
15559+/* ---------------------------------------------------------------------- */
15560+
15561+static int au_do_flush_nondir(struct file *file, fl_owner_t id)
dece6358 15562+{
1308ab2a 15563+ int err;
4a4d8108
AM
15564+ struct file *h_file;
15565+
15566+ err = 0;
15567+ h_file = au_hf_top(file);
15568+ if (h_file)
15569+ err = vfsub_flush(h_file, id);
15570+ return err;
15571+}
15572+
15573+static int aufs_flush_nondir(struct file *file, fl_owner_t id)
15574+{
15575+ return au_do_flush(file, id, au_do_flush_nondir);
15576+}
15577+
15578+/* ---------------------------------------------------------------------- */
9dbd164d
AM
15579+/*
15580+ * read and write functions acquire [fdi]_rwsem once, but release before
15581+ * mmap_sem. This is because to stop a race condition between mmap(2).
acd2b654 15582+ * Releasing these aufs-rwsem should be safe, no branch-management (by keeping
9dbd164d
AM
15583+ * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
15584+ * read functions after [fdi]_rwsem are released, but it should be harmless.
15585+ */
4a4d8108 15586+
b912730e 15587+/* Callers should call au_read_post() or fput() in the end */
521ced18 15588+struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
4a4d8108 15589+{
4a4d8108 15590+ struct file *h_file;
b912730e 15591+ int err;
1facf9fc 15592+
521ced18 15593+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
b912730e
AM
15594+ if (!err) {
15595+ di_read_unlock(file->f_path.dentry, AuLock_IR);
15596+ h_file = au_hf_top(file);
15597+ get_file(h_file);
15598+ if (!keep_fi)
15599+ fi_read_unlock(file);
15600+ } else
15601+ h_file = ERR_PTR(err);
15602+
15603+ return h_file;
15604+}
15605+
15606+static void au_read_post(struct inode *inode, struct file *h_file)
15607+{
15608+ /* update without lock, I don't think it a problem */
15609+ fsstack_copy_attr_atime(inode, file_inode(h_file));
15610+ fput(h_file);
15611+}
15612+
15613+struct au_write_pre {
521ced18
JR
15614+ /* input */
15615+ unsigned int lsc;
15616+
15617+ /* output */
b912730e 15618+ blkcnt_t blks;
5afbbe0d 15619+ aufs_bindex_t btop;
b912730e
AM
15620+};
15621+
15622+/*
15623+ * return with iinfo is write-locked
15624+ * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
15625+ * end
15626+ */
15627+static struct file *au_write_pre(struct file *file, int do_ready,
15628+ struct au_write_pre *wpre)
15629+{
15630+ struct file *h_file;
15631+ struct dentry *dentry;
15632+ int err;
521ced18 15633+ unsigned int lsc;
b912730e
AM
15634+ struct au_pin pin;
15635+
521ced18
JR
15636+ lsc = 0;
15637+ if (wpre)
15638+ lsc = wpre->lsc;
15639+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
b912730e 15640+ h_file = ERR_PTR(err);
dece6358
AM
15641+ if (unlikely(err))
15642+ goto out;
1facf9fc 15643+
b912730e
AM
15644+ dentry = file->f_path.dentry;
15645+ if (do_ready) {
15646+ err = au_ready_to_write(file, -1, &pin);
15647+ if (unlikely(err)) {
15648+ h_file = ERR_PTR(err);
15649+ di_write_unlock(dentry);
15650+ goto out_fi;
15651+ }
15652+ }
15653+
15654+ di_downgrade_lock(dentry, /*flags*/0);
15655+ if (wpre)
5afbbe0d 15656+ wpre->btop = au_fbtop(file);
4a4d8108 15657+ h_file = au_hf_top(file);
9dbd164d 15658+ get_file(h_file);
b912730e
AM
15659+ if (wpre)
15660+ wpre->blks = file_inode(h_file)->i_blocks;
15661+ if (do_ready)
15662+ au_unpin(&pin);
15663+ di_read_unlock(dentry, /*flags*/0);
15664+
15665+out_fi:
15666+ fi_write_unlock(file);
15667+out:
15668+ return h_file;
15669+}
15670+
15671+static void au_write_post(struct inode *inode, struct file *h_file,
15672+ struct au_write_pre *wpre, ssize_t written)
15673+{
15674+ struct inode *h_inode;
15675+
15676+ au_cpup_attr_timesizes(inode);
5afbbe0d 15677+ AuDebugOn(au_ibtop(inode) != wpre->btop);
b912730e
AM
15678+ h_inode = file_inode(h_file);
15679+ inode->i_mode = h_inode->i_mode;
15680+ ii_write_unlock(inode);
b912730e
AM
15681+ /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
15682+ if (written > 0)
5afbbe0d 15683+ au_fhsm_wrote(inode->i_sb, wpre->btop,
b912730e 15684+ /*force*/h_inode->i_blocks > wpre->blks);
1c60b727 15685+ fput(h_file);
b912730e
AM
15686+}
15687+
e49829fe
JR
15688+/*
15689+ * todo: very ugly
15690+ * it locks both of i_mutex and si_rwsem for read in safe.
15691+ * if the plink maintenance mode continues forever (that is the problem),
15692+ * may loop forever.
15693+ */
15694+static void au_mtx_and_read_lock(struct inode *inode)
15695+{
15696+ int err;
15697+ struct super_block *sb = inode->i_sb;
15698+
15699+ while (1) {
febd17d6 15700+ inode_lock(inode);
e49829fe
JR
15701+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
15702+ if (!err)
15703+ break;
febd17d6 15704+ inode_unlock(inode);
e49829fe
JR
15705+ si_read_lock(sb, AuLock_NOPLMW);
15706+ si_read_unlock(sb);
15707+ }
15708+}
15709+
076b876e
AM
15710+static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
15711+ struct iov_iter *iov_iter)
dece6358 15712+{
4a4d8108
AM
15713+ ssize_t err;
15714+ struct file *file;
076b876e 15715+ ssize_t (*iter)(struct kiocb *, struct iov_iter *);
1facf9fc 15716+
4a4d8108
AM
15717+ err = security_file_permission(h_file, rw);
15718+ if (unlikely(err))
15719+ goto out;
1facf9fc 15720+
43982f53 15721+ err = -ENOSYS; /* the branch doesn't have its ->(read|write)_iter() */
076b876e 15722+ iter = NULL;
5527c038 15723+ if (rw == MAY_READ)
076b876e 15724+ iter = h_file->f_op->read_iter;
5527c038 15725+ else if (rw == MAY_WRITE)
076b876e 15726+ iter = h_file->f_op->write_iter;
076b876e
AM
15727+
15728+ file = kio->ki_filp;
15729+ kio->ki_filp = h_file;
15730+ if (iter) {
2cbb1c4b 15731+ lockdep_off();
076b876e
AM
15732+ err = iter(kio, iov_iter);
15733+ lockdep_on();
4a4d8108
AM
15734+ } else
15735+ /* currently there is no such fs */
15736+ WARN_ON_ONCE(1);
076b876e 15737+ kio->ki_filp = file;
1facf9fc 15738+
4f0767ce 15739+out:
dece6358
AM
15740+ return err;
15741+}
1facf9fc 15742+
076b876e 15743+static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
1facf9fc 15744+{
4a4d8108
AM
15745+ ssize_t err;
15746+ struct file *file, *h_file;
b912730e 15747+ struct inode *inode;
dece6358 15748+ struct super_block *sb;
1facf9fc 15749+
4a4d8108 15750+ file = kio->ki_filp;
b912730e
AM
15751+ inode = file_inode(file);
15752+ sb = inode->i_sb;
e49829fe 15753+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
4a4d8108 15754+
521ced18 15755+ h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
b912730e
AM
15756+ err = PTR_ERR(h_file);
15757+ if (IS_ERR(h_file))
15758+ goto out;
9dbd164d 15759+
5afbbe0d
AM
15760+ if (au_test_loopback_kthread()) {
15761+ au_warn_loopback(h_file->f_path.dentry->d_sb);
15762+ if (file->f_mapping != h_file->f_mapping) {
15763+ file->f_mapping = h_file->f_mapping;
15764+ smp_mb(); /* unnecessary? */
15765+ }
15766+ }
15767+ fi_read_unlock(file);
15768+
076b876e 15769+ err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
4a4d8108
AM
15770+ /* todo: necessary? */
15771+ /* file->f_ra = h_file->f_ra; */
b912730e 15772+ au_read_post(inode, h_file);
1facf9fc 15773+
4f0767ce 15774+out:
4a4d8108 15775+ si_read_unlock(sb);
1308ab2a 15776+ return err;
15777+}
1facf9fc 15778+
076b876e 15779+static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
1308ab2a 15780+{
4a4d8108 15781+ ssize_t err;
b912730e
AM
15782+ struct au_write_pre wpre;
15783+ struct inode *inode;
4a4d8108 15784+ struct file *file, *h_file;
1308ab2a 15785+
4a4d8108 15786+ file = kio->ki_filp;
b912730e 15787+ inode = file_inode(file);
e49829fe
JR
15788+ au_mtx_and_read_lock(inode);
15789+
521ced18 15790+ wpre.lsc = 0;
b912730e
AM
15791+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15792+ err = PTR_ERR(h_file);
15793+ if (IS_ERR(h_file))
9dbd164d 15794+ goto out;
9dbd164d 15795+
076b876e 15796+ err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
b912730e 15797+ au_write_post(inode, h_file, &wpre, err);
1facf9fc 15798+
4f0767ce 15799+out:
b912730e 15800+ si_read_unlock(inode->i_sb);
febd17d6 15801+ inode_unlock(inode);
dece6358 15802+ return err;
1facf9fc 15803+}
15804+
0b2a12c6
JR
15805+/*
15806+ * We may be able to remove aufs_splice_{read,write}() since almost all FSes
15807+ * don't have their own .splice_{read,write} implimentations, and they use
15808+ * generic_file_splice_read() and iter_file_splice_write() who can act like the
15809+ * simple converters to f_op->iter_read() and ->iter_write().
15810+ * But we keep our own implementations because some non-mainlined FSes may have
15811+ * their own .splice_{read,write} implimentations and aufs doesn't want to take
15812+ * away an opportunity to co-work with aufs from them.
15813+ */
4a4d8108
AM
15814+static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
15815+ struct pipe_inode_info *pipe, size_t len,
15816+ unsigned int flags)
1facf9fc 15817+{
4a4d8108
AM
15818+ ssize_t err;
15819+ struct file *h_file;
b912730e 15820+ struct inode *inode;
dece6358 15821+ struct super_block *sb;
1facf9fc 15822+
b912730e
AM
15823+ inode = file_inode(file);
15824+ sb = inode->i_sb;
e49829fe 15825+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e 15826+
521ced18 15827+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
b912730e
AM
15828+ err = PTR_ERR(h_file);
15829+ if (IS_ERR(h_file))
dece6358 15830+ goto out;
1facf9fc 15831+
4a4d8108 15832+ err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
acd2b654 15833+ /* todo: necessary? */
4a4d8108 15834+ /* file->f_ra = h_file->f_ra; */
b912730e 15835+ au_read_post(inode, h_file);
1facf9fc 15836+
4f0767ce 15837+out:
4a4d8108 15838+ si_read_unlock(sb);
dece6358 15839+ return err;
1facf9fc 15840+}
15841+
4a4d8108
AM
15842+static ssize_t
15843+aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
15844+ size_t len, unsigned int flags)
1facf9fc 15845+{
4a4d8108 15846+ ssize_t err;
b912730e
AM
15847+ struct au_write_pre wpre;
15848+ struct inode *inode;
076b876e 15849+ struct file *h_file;
1facf9fc 15850+
b912730e 15851+ inode = file_inode(file);
e49829fe 15852+ au_mtx_and_read_lock(inode);
9dbd164d 15853+
521ced18 15854+ wpre.lsc = 0;
b912730e
AM
15855+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15856+ err = PTR_ERR(h_file);
15857+ if (IS_ERR(h_file))
9dbd164d 15858+ goto out;
9dbd164d 15859+
4a4d8108 15860+ err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
b912730e 15861+ au_write_post(inode, h_file, &wpre, err);
1facf9fc 15862+
4f0767ce 15863+out:
b912730e 15864+ si_read_unlock(inode->i_sb);
febd17d6 15865+ inode_unlock(inode);
4a4d8108
AM
15866+ return err;
15867+}
1facf9fc 15868+
38d290e6
JR
15869+static long aufs_fallocate(struct file *file, int mode, loff_t offset,
15870+ loff_t len)
15871+{
15872+ long err;
b912730e 15873+ struct au_write_pre wpre;
38d290e6
JR
15874+ struct inode *inode;
15875+ struct file *h_file;
15876+
b912730e 15877+ inode = file_inode(file);
38d290e6
JR
15878+ au_mtx_and_read_lock(inode);
15879+
521ced18 15880+ wpre.lsc = 0;
b912730e
AM
15881+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15882+ err = PTR_ERR(h_file);
15883+ if (IS_ERR(h_file))
38d290e6 15884+ goto out;
38d290e6
JR
15885+
15886+ lockdep_off();
03673fb0 15887+ err = vfs_fallocate(h_file, mode, offset, len);
38d290e6 15888+ lockdep_on();
b912730e 15889+ au_write_post(inode, h_file, &wpre, /*written*/1);
38d290e6
JR
15890+
15891+out:
b912730e 15892+ si_read_unlock(inode->i_sb);
febd17d6 15893+ inode_unlock(inode);
38d290e6
JR
15894+ return err;
15895+}
15896+
521ced18
JR
15897+static ssize_t aufs_copy_file_range(struct file *src, loff_t src_pos,
15898+ struct file *dst, loff_t dst_pos,
15899+ size_t len, unsigned int flags)
15900+{
15901+ ssize_t err;
15902+ struct au_write_pre wpre;
15903+ enum { SRC, DST };
15904+ struct {
15905+ struct inode *inode;
15906+ struct file *h_file;
15907+ struct super_block *h_sb;
15908+ } a[2];
15909+#define a_src a[SRC]
15910+#define a_dst a[DST]
15911+
15912+ err = -EINVAL;
15913+ a_src.inode = file_inode(src);
15914+ if (unlikely(!S_ISREG(a_src.inode->i_mode)))
15915+ goto out;
15916+ a_dst.inode = file_inode(dst);
15917+ if (unlikely(!S_ISREG(a_dst.inode->i_mode)))
15918+ goto out;
15919+
15920+ au_mtx_and_read_lock(a_dst.inode);
15921+ /*
15922+ * in order to match the order in di_write_lock2_{child,parent}(),
acd2b654 15923+ * use f_path.dentry for this comparison.
521ced18
JR
15924+ */
15925+ if (src->f_path.dentry < dst->f_path.dentry) {
15926+ a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_1);
15927+ err = PTR_ERR(a_src.h_file);
15928+ if (IS_ERR(a_src.h_file))
15929+ goto out_si;
15930+
15931+ wpre.lsc = AuLsc_FI_2;
15932+ a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
15933+ err = PTR_ERR(a_dst.h_file);
15934+ if (IS_ERR(a_dst.h_file)) {
15935+ au_read_post(a_src.inode, a_src.h_file);
15936+ goto out_si;
15937+ }
15938+ } else {
15939+ wpre.lsc = AuLsc_FI_1;
15940+ a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
15941+ err = PTR_ERR(a_dst.h_file);
15942+ if (IS_ERR(a_dst.h_file))
15943+ goto out_si;
15944+
15945+ a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_2);
15946+ err = PTR_ERR(a_src.h_file);
15947+ if (IS_ERR(a_src.h_file)) {
15948+ au_write_post(a_dst.inode, a_dst.h_file, &wpre,
15949+ /*written*/0);
15950+ goto out_si;
15951+ }
15952+ }
15953+
15954+ err = -EXDEV;
15955+ a_src.h_sb = file_inode(a_src.h_file)->i_sb;
15956+ a_dst.h_sb = file_inode(a_dst.h_file)->i_sb;
15957+ if (unlikely(a_src.h_sb != a_dst.h_sb)) {
15958+ AuDbgFile(src);
15959+ AuDbgFile(dst);
15960+ goto out_file;
15961+ }
15962+
15963+ err = vfsub_copy_file_range(a_src.h_file, src_pos, a_dst.h_file,
15964+ dst_pos, len, flags);
15965+
15966+out_file:
15967+ au_write_post(a_dst.inode, a_dst.h_file, &wpre, err);
15968+ fi_read_unlock(src);
15969+ au_read_post(a_src.inode, a_src.h_file);
15970+out_si:
15971+ si_read_unlock(a_dst.inode->i_sb);
15972+ inode_unlock(a_dst.inode);
15973+out:
15974+ return err;
15975+#undef a_src
15976+#undef a_dst
15977+}
15978+
4a4d8108
AM
15979+/* ---------------------------------------------------------------------- */
15980+
9dbd164d
AM
15981+/*
15982+ * The locking order around current->mmap_sem.
15983+ * - in most and regular cases
15984+ * file I/O syscall -- aufs_read() or something
15985+ * -- si_rwsem for read -- mmap_sem
15986+ * (Note that [fdi]i_rwsem are released before mmap_sem).
15987+ * - in mmap case
15988+ * mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
acd2b654
AM
15989+ * This AB-BA order is definitely bad, but is not a problem since "si_rwsem for
15990+ * read" allows multiple processes to acquire it and [fdi]i_rwsem are not held
15991+ * in file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
9dbd164d
AM
15992+ * It means that when aufs acquires si_rwsem for write, the process should never
15993+ * acquire mmap_sem.
15994+ *
392086de 15995+ * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
9dbd164d
AM
15996+ * problem either since any directory is not able to be mmap-ed.
15997+ * The similar scenario is applied to aufs_readlink() too.
15998+ */
15999+
38d290e6 16000+#if 0 /* stop calling security_file_mmap() */
2dfbb274
AM
16001+/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
16002+#define AuConv_VM_PROT(f, b) _calc_vm_trans(f, VM_##b, PROT_##b)
16003+
16004+static unsigned long au_arch_prot_conv(unsigned long flags)
16005+{
16006+ /* currently ppc64 only */
16007+#ifdef CONFIG_PPC64
16008+ /* cf. linux/arch/powerpc/include/asm/mman.h */
16009+ AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
16010+ return AuConv_VM_PROT(flags, SAO);
16011+#else
16012+ AuDebugOn(arch_calc_vm_prot_bits(-1));
16013+ return 0;
16014+#endif
16015+}
16016+
16017+static unsigned long au_prot_conv(unsigned long flags)
16018+{
16019+ return AuConv_VM_PROT(flags, READ)
16020+ | AuConv_VM_PROT(flags, WRITE)
16021+ | AuConv_VM_PROT(flags, EXEC)
16022+ | au_arch_prot_conv(flags);
16023+}
16024+
16025+/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
16026+#define AuConv_VM_MAP(f, b) _calc_vm_trans(f, VM_##b, MAP_##b)
16027+
16028+static unsigned long au_flag_conv(unsigned long flags)
16029+{
16030+ return AuConv_VM_MAP(flags, GROWSDOWN)
16031+ | AuConv_VM_MAP(flags, DENYWRITE)
2dfbb274
AM
16032+ | AuConv_VM_MAP(flags, LOCKED);
16033+}
38d290e6 16034+#endif
2dfbb274 16035+
9dbd164d 16036+static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
dece6358 16037+{
4a4d8108 16038+ int err;
4a4d8108 16039+ const unsigned char wlock
9dbd164d 16040+ = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
4a4d8108 16041+ struct super_block *sb;
9dbd164d 16042+ struct file *h_file;
b912730e 16043+ struct inode *inode;
9dbd164d
AM
16044+
16045+ AuDbgVmRegion(file, vma);
1308ab2a 16046+
b912730e
AM
16047+ inode = file_inode(file);
16048+ sb = inode->i_sb;
9dbd164d 16049+ lockdep_off();
e49829fe 16050+ si_read_lock(sb, AuLock_NOPLMW);
4a4d8108 16051+
b912730e 16052+ h_file = au_write_pre(file, wlock, /*wpre*/NULL);
9dbd164d 16053+ lockdep_on();
b912730e
AM
16054+ err = PTR_ERR(h_file);
16055+ if (IS_ERR(h_file))
16056+ goto out;
1308ab2a 16057+
b912730e
AM
16058+ err = 0;
16059+ au_set_mmapped(file);
9dbd164d 16060+ au_vm_file_reset(vma, h_file);
38d290e6
JR
16061+ /*
16062+ * we cannot call security_mmap_file() here since it may acquire
16063+ * mmap_sem or i_mutex.
16064+ *
16065+ * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
16066+ * au_flag_conv(vma->vm_flags));
16067+ */
9dbd164d 16068+ if (!err)
521ced18 16069+ err = call_mmap(h_file, vma);
b912730e
AM
16070+ if (!err) {
16071+ au_vm_prfile_set(vma, file);
16072+ fsstack_copy_attr_atime(inode, file_inode(h_file));
16073+ goto out_fput; /* success */
16074+ }
2cbb1c4b
JR
16075+ au_unset_mmapped(file);
16076+ au_vm_file_reset(vma, file);
b912730e 16077+
2cbb1c4b 16078+out_fput:
9dbd164d 16079+ lockdep_off();
b912730e
AM
16080+ ii_write_unlock(inode);
16081+ lockdep_on();
16082+ fput(h_file);
4f0767ce 16083+out:
b912730e 16084+ lockdep_off();
9dbd164d
AM
16085+ si_read_unlock(sb);
16086+ lockdep_on();
16087+ AuTraceErr(err);
4a4d8108
AM
16088+ return err;
16089+}
16090+
16091+/* ---------------------------------------------------------------------- */
16092+
1e00d052
AM
16093+static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
16094+ int datasync)
4a4d8108
AM
16095+{
16096+ int err;
b912730e 16097+ struct au_write_pre wpre;
4a4d8108
AM
16098+ struct inode *inode;
16099+ struct file *h_file;
4a4d8108
AM
16100+
16101+ err = 0; /* -EBADF; */ /* posix? */
16102+ if (unlikely(!(file->f_mode & FMODE_WRITE)))
b912730e 16103+ goto out;
4a4d8108 16104+
b912730e
AM
16105+ inode = file_inode(file);
16106+ au_mtx_and_read_lock(inode);
16107+
521ced18 16108+ wpre.lsc = 0;
b912730e
AM
16109+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16110+ err = PTR_ERR(h_file);
16111+ if (IS_ERR(h_file))
4a4d8108 16112+ goto out_unlock;
4a4d8108 16113+
53392da6 16114+ err = vfsub_fsync(h_file, &h_file->f_path, datasync);
b912730e 16115+ au_write_post(inode, h_file, &wpre, /*written*/0);
4a4d8108 16116+
4f0767ce 16117+out_unlock:
b912730e 16118+ si_read_unlock(inode->i_sb);
febd17d6 16119+ inode_unlock(inode);
b912730e 16120+out:
4a4d8108 16121+ return err;
dece6358
AM
16122+}
16123+
4a4d8108 16124+static int aufs_fasync(int fd, struct file *file, int flag)
dece6358 16125+{
4a4d8108
AM
16126+ int err;
16127+ struct file *h_file;
4a4d8108 16128+ struct super_block *sb;
1308ab2a 16129+
b912730e 16130+ sb = file->f_path.dentry->d_sb;
e49829fe 16131+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e 16132+
521ced18 16133+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
b912730e
AM
16134+ err = PTR_ERR(h_file);
16135+ if (IS_ERR(h_file))
4a4d8108
AM
16136+ goto out;
16137+
523b37e3 16138+ if (h_file->f_op->fasync)
4a4d8108 16139+ err = h_file->f_op->fasync(fd, h_file, flag);
b912730e 16140+ fput(h_file); /* instead of au_read_post() */
1308ab2a 16141+
4f0767ce 16142+out:
4a4d8108 16143+ si_read_unlock(sb);
1308ab2a 16144+ return err;
dece6358 16145+}
4a4d8108 16146+
febd17d6
JR
16147+static int aufs_setfl(struct file *file, unsigned long arg)
16148+{
16149+ int err;
16150+ struct file *h_file;
16151+ struct super_block *sb;
16152+
16153+ sb = file->f_path.dentry->d_sb;
16154+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16155+
521ced18 16156+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
febd17d6
JR
16157+ err = PTR_ERR(h_file);
16158+ if (IS_ERR(h_file))
16159+ goto out;
16160+
1c60b727
AM
16161+ /* stop calling h_file->fasync */
16162+ arg |= vfsub_file_flags(file) & FASYNC;
febd17d6
JR
16163+ err = setfl(/*unused fd*/-1, h_file, arg);
16164+ fput(h_file); /* instead of au_read_post() */
16165+
16166+out:
16167+ si_read_unlock(sb);
16168+ return err;
16169+}
16170+
4a4d8108
AM
16171+/* ---------------------------------------------------------------------- */
16172+
16173+/* no one supports this operation, currently */
43982f53 16174+#if 0 /* reserved for future use */
4a4d8108 16175+static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
2000de60 16176+ size_t len, loff_t *pos, int more)
4a4d8108
AM
16177+{
16178+}
16179+#endif
16180+
16181+/* ---------------------------------------------------------------------- */
16182+
16183+const struct file_operations aufs_file_fop = {
16184+ .owner = THIS_MODULE,
2cbb1c4b 16185+
027c5e7a 16186+ .llseek = default_llseek,
4a4d8108 16187+
076b876e
AM
16188+ .read_iter = aufs_read_iter,
16189+ .write_iter = aufs_write_iter,
16190+
4a4d8108
AM
16191+#ifdef CONFIG_AUFS_POLL
16192+ .poll = aufs_poll,
16193+#endif
16194+ .unlocked_ioctl = aufs_ioctl_nondir,
b752ccd1 16195+#ifdef CONFIG_COMPAT
c2b27bf2 16196+ .compat_ioctl = aufs_compat_ioctl_nondir,
b752ccd1 16197+#endif
4a4d8108
AM
16198+ .mmap = aufs_mmap,
16199+ .open = aufs_open_nondir,
16200+ .flush = aufs_flush_nondir,
16201+ .release = aufs_release_nondir,
16202+ .fsync = aufs_fsync_nondir,
4a4d8108
AM
16203+ .fasync = aufs_fasync,
16204+ /* .sendpage = aufs_sendpage, */
febd17d6 16205+ .setfl = aufs_setfl,
4a4d8108
AM
16206+ .splice_write = aufs_splice_write,
16207+ .splice_read = aufs_splice_read,
43982f53 16208+#if 0 /* reserved for future use */
4a4d8108 16209+ .aio_splice_write = aufs_aio_splice_write,
38d290e6 16210+ .aio_splice_read = aufs_aio_splice_read,
4a4d8108 16211+#endif
521ced18
JR
16212+ .fallocate = aufs_fallocate,
16213+ .copy_file_range = aufs_copy_file_range
4a4d8108 16214+};
7f207e10 16215diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
eca34b5c 16216--- /usr/share/empty/fs/aufs/fstype.h 1970-01-01 01:00:00.000000000 +0100
46016270 16217+++ linux/fs/aufs/fstype.h 2021-02-24 13:33:42.744347058 +0100
062440b3
AM
16218@@ -0,0 +1,401 @@
16219+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 16220+/*
d58c55f2 16221+ * Copyright (C) 2005-2020 Junjiro R. Okajima
4a4d8108
AM
16222+ *
16223+ * This program, aufs is free software; you can redistribute it and/or modify
16224+ * it under the terms of the GNU General Public License as published by
16225+ * the Free Software Foundation; either version 2 of the License, or
16226+ * (at your option) any later version.
16227+ *
16228+ * This program is distributed in the hope that it will be useful,
16229+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16230+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16231+ * GNU General Public License for more details.
16232+ *
16233+ * You should have received a copy of the GNU General Public License
523b37e3 16234+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
16235+ */
16236+
16237+/*
16238+ * judging filesystem type
16239+ */
16240+
16241+#ifndef __AUFS_FSTYPE_H__
16242+#define __AUFS_FSTYPE_H__
16243+
16244+#ifdef __KERNEL__
16245+
16246+#include <linux/fs.h>
16247+#include <linux/magic.h>
b912730e 16248+#include <linux/nfs_fs.h>
b95c5147 16249+#include <linux/romfs_fs.h>
4a4d8108
AM
16250+
16251+static inline int au_test_aufs(struct super_block *sb)
16252+{
16253+ return sb->s_magic == AUFS_SUPER_MAGIC;
16254+}
16255+
16256+static inline const char *au_sbtype(struct super_block *sb)
16257+{
16258+ return sb->s_type->name;
16259+}
1308ab2a 16260+
16261+static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
16262+{
f0c0a007 16263+#if IS_ENABLED(CONFIG_ISO9660_FS)
2000de60 16264+ return sb->s_magic == ISOFS_SUPER_MAGIC;
dece6358
AM
16265+#else
16266+ return 0;
16267+#endif
16268+}
16269+
1308ab2a 16270+static inline int au_test_romfs(struct super_block *sb __maybe_unused)
dece6358 16271+{
f0c0a007 16272+#if IS_ENABLED(CONFIG_ROMFS_FS)
2000de60 16273+ return sb->s_magic == ROMFS_MAGIC;
dece6358
AM
16274+#else
16275+ return 0;
16276+#endif
16277+}
16278+
1308ab2a 16279+static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
dece6358 16280+{
f0c0a007 16281+#if IS_ENABLED(CONFIG_CRAMFS)
1308ab2a 16282+ return sb->s_magic == CRAMFS_MAGIC;
16283+#endif
16284+ return 0;
16285+}
16286+
16287+static inline int au_test_nfs(struct super_block *sb __maybe_unused)
16288+{
f0c0a007 16289+#if IS_ENABLED(CONFIG_NFS_FS)
1308ab2a 16290+ return sb->s_magic == NFS_SUPER_MAGIC;
dece6358
AM
16291+#else
16292+ return 0;
16293+#endif
16294+}
16295+
1308ab2a 16296+static inline int au_test_fuse(struct super_block *sb __maybe_unused)
dece6358 16297+{
f0c0a007 16298+#if IS_ENABLED(CONFIG_FUSE_FS)
1308ab2a 16299+ return sb->s_magic == FUSE_SUPER_MAGIC;
dece6358
AM
16300+#else
16301+ return 0;
16302+#endif
16303+}
16304+
1308ab2a 16305+static inline int au_test_xfs(struct super_block *sb __maybe_unused)
dece6358 16306+{
f0c0a007 16307+#if IS_ENABLED(CONFIG_XFS_FS)
1308ab2a 16308+ return sb->s_magic == XFS_SB_MAGIC;
dece6358
AM
16309+#else
16310+ return 0;
16311+#endif
16312+}
16313+
1308ab2a 16314+static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
dece6358 16315+{
1308ab2a 16316+#ifdef CONFIG_TMPFS
16317+ return sb->s_magic == TMPFS_MAGIC;
16318+#else
16319+ return 0;
dece6358 16320+#endif
dece6358
AM
16321+}
16322+
1308ab2a 16323+static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
1facf9fc 16324+{
f0c0a007 16325+#if IS_ENABLED(CONFIG_ECRYPT_FS)
1308ab2a 16326+ return !strcmp(au_sbtype(sb), "ecryptfs");
16327+#else
16328+ return 0;
16329+#endif
1facf9fc 16330+}
16331+
1308ab2a 16332+static inline int au_test_ramfs(struct super_block *sb)
16333+{
16334+ return sb->s_magic == RAMFS_MAGIC;
16335+}
16336+
16337+static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
16338+{
f0c0a007 16339+#if IS_ENABLED(CONFIG_UBIFS_FS)
1308ab2a 16340+ return sb->s_magic == UBIFS_SUPER_MAGIC;
16341+#else
16342+ return 0;
16343+#endif
16344+}
16345+
16346+static inline int au_test_procfs(struct super_block *sb __maybe_unused)
16347+{
16348+#ifdef CONFIG_PROC_FS
16349+ return sb->s_magic == PROC_SUPER_MAGIC;
16350+#else
16351+ return 0;
16352+#endif
16353+}
16354+
16355+static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
16356+{
16357+#ifdef CONFIG_SYSFS
16358+ return sb->s_magic == SYSFS_MAGIC;
16359+#else
16360+ return 0;
16361+#endif
16362+}
16363+
16364+static inline int au_test_configfs(struct super_block *sb __maybe_unused)
16365+{
f0c0a007 16366+#if IS_ENABLED(CONFIG_CONFIGFS_FS)
1308ab2a 16367+ return sb->s_magic == CONFIGFS_MAGIC;
16368+#else
16369+ return 0;
16370+#endif
16371+}
16372+
16373+static inline int au_test_minix(struct super_block *sb __maybe_unused)
16374+{
f0c0a007 16375+#if IS_ENABLED(CONFIG_MINIX_FS)
1308ab2a 16376+ return sb->s_magic == MINIX3_SUPER_MAGIC
16377+ || sb->s_magic == MINIX2_SUPER_MAGIC
16378+ || sb->s_magic == MINIX2_SUPER_MAGIC2
16379+ || sb->s_magic == MINIX_SUPER_MAGIC
16380+ || sb->s_magic == MINIX_SUPER_MAGIC2;
16381+#else
16382+ return 0;
16383+#endif
16384+}
16385+
1308ab2a 16386+static inline int au_test_fat(struct super_block *sb __maybe_unused)
16387+{
f0c0a007 16388+#if IS_ENABLED(CONFIG_FAT_FS)
1308ab2a 16389+ return sb->s_magic == MSDOS_SUPER_MAGIC;
16390+#else
16391+ return 0;
16392+#endif
16393+}
16394+
16395+static inline int au_test_msdos(struct super_block *sb)
16396+{
16397+ return au_test_fat(sb);
16398+}
16399+
16400+static inline int au_test_vfat(struct super_block *sb)
16401+{
16402+ return au_test_fat(sb);
16403+}
16404+
16405+static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
16406+{
16407+#ifdef CONFIG_SECURITYFS
16408+ return sb->s_magic == SECURITYFS_MAGIC;
16409+#else
16410+ return 0;
16411+#endif
16412+}
16413+
16414+static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
16415+{
f0c0a007 16416+#if IS_ENABLED(CONFIG_SQUASHFS)
1308ab2a 16417+ return sb->s_magic == SQUASHFS_MAGIC;
16418+#else
16419+ return 0;
16420+#endif
16421+}
16422+
16423+static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
16424+{
f0c0a007 16425+#if IS_ENABLED(CONFIG_BTRFS_FS)
1308ab2a 16426+ return sb->s_magic == BTRFS_SUPER_MAGIC;
16427+#else
16428+ return 0;
16429+#endif
16430+}
16431+
16432+static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
16433+{
f0c0a007 16434+#if IS_ENABLED(CONFIG_XENFS)
1308ab2a 16435+ return sb->s_magic == XENFS_SUPER_MAGIC;
16436+#else
16437+ return 0;
16438+#endif
16439+}
16440+
16441+static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
16442+{
16443+#ifdef CONFIG_DEBUG_FS
16444+ return sb->s_magic == DEBUGFS_MAGIC;
16445+#else
16446+ return 0;
16447+#endif
16448+}
16449+
16450+static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
16451+{
f0c0a007 16452+#if IS_ENABLED(CONFIG_NILFS)
1308ab2a 16453+ return sb->s_magic == NILFS_SUPER_MAGIC;
16454+#else
16455+ return 0;
16456+#endif
16457+}
16458+
4a4d8108
AM
16459+static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
16460+{
f0c0a007 16461+#if IS_ENABLED(CONFIG_HFSPLUS_FS)
4a4d8108
AM
16462+ return sb->s_magic == HFSPLUS_SUPER_MAGIC;
16463+#else
16464+ return 0;
16465+#endif
16466+}
16467+
1308ab2a 16468+/* ---------------------------------------------------------------------- */
16469+/*
16470+ * they can't be an aufs branch.
16471+ */
16472+static inline int au_test_fs_unsuppoted(struct super_block *sb)
16473+{
16474+ return
16475+#ifndef CONFIG_AUFS_BR_RAMFS
16476+ au_test_ramfs(sb) ||
16477+#endif
16478+ au_test_procfs(sb)
16479+ || au_test_sysfs(sb)
16480+ || au_test_configfs(sb)
16481+ || au_test_debugfs(sb)
16482+ || au_test_securityfs(sb)
16483+ || au_test_xenfs(sb)
16484+ || au_test_ecryptfs(sb)
16485+ /* || !strcmp(au_sbtype(sb), "unionfs") */
16486+ || au_test_aufs(sb); /* will be supported in next version */
16487+}
16488+
1308ab2a 16489+static inline int au_test_fs_remote(struct super_block *sb)
16490+{
16491+ return !au_test_tmpfs(sb)
16492+#ifdef CONFIG_AUFS_BR_RAMFS
16493+ && !au_test_ramfs(sb)
16494+#endif
16495+ && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
16496+}
16497+
16498+/* ---------------------------------------------------------------------- */
16499+
16500+/*
16501+ * Note: these functions (below) are created after reading ->getattr() in all
16502+ * filesystems under linux/fs. it means we have to do so in every update...
16503+ */
16504+
16505+/*
16506+ * some filesystems require getattr to refresh the inode attributes before
16507+ * referencing.
16508+ * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
16509+ * and leave the work for d_revalidate()
16510+ */
16511+static inline int au_test_fs_refresh_iattr(struct super_block *sb)
16512+{
16513+ return au_test_nfs(sb)
16514+ || au_test_fuse(sb)
1308ab2a 16515+ /* || au_test_btrfs(sb) */ /* untested */
1308ab2a 16516+ ;
16517+}
16518+
16519+/*
16520+ * filesystems which don't maintain i_size or i_blocks.
16521+ */
16522+static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
16523+{
16524+ return au_test_xfs(sb)
4a4d8108
AM
16525+ || au_test_btrfs(sb)
16526+ || au_test_ubifs(sb)
16527+ || au_test_hfsplus(sb) /* maintained, but incorrect */
1308ab2a 16528+ /* || au_test_minix(sb) */ /* untested */
16529+ ;
16530+}
16531+
16532+/*
16533+ * filesystems which don't store the correct value in some of their inode
16534+ * attributes.
16535+ */
16536+static inline int au_test_fs_bad_iattr(struct super_block *sb)
16537+{
16538+ return au_test_fs_bad_iattr_size(sb)
1308ab2a 16539+ || au_test_fat(sb)
16540+ || au_test_msdos(sb)
16541+ || au_test_vfat(sb);
1facf9fc 16542+}
16543+
16544+/* they don't check i_nlink in link(2) */
16545+static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
16546+{
16547+ return au_test_tmpfs(sb)
16548+#ifdef CONFIG_AUFS_BR_RAMFS
16549+ || au_test_ramfs(sb)
16550+#endif
4a4d8108 16551+ || au_test_ubifs(sb)
4a4d8108 16552+ || au_test_hfsplus(sb);
1facf9fc 16553+}
16554+
16555+/*
16556+ * filesystems which sets S_NOATIME and S_NOCMTIME.
16557+ */
16558+static inline int au_test_fs_notime(struct super_block *sb)
16559+{
16560+ return au_test_nfs(sb)
16561+ || au_test_fuse(sb)
dece6358 16562+ || au_test_ubifs(sb)
1facf9fc 16563+ ;
16564+}
16565+
1facf9fc 16566+/* temporary support for i#1 in cramfs */
16567+static inline int au_test_fs_unique_ino(struct inode *inode)
16568+{
16569+ if (au_test_cramfs(inode->i_sb))
16570+ return inode->i_ino != 1;
16571+ return 1;
16572+}
16573+
16574+/* ---------------------------------------------------------------------- */
16575+
16576+/*
16577+ * the filesystem where the xino files placed must support i/o after unlink and
16578+ * maintain i_size and i_blocks.
16579+ */
16580+static inline int au_test_fs_bad_xino(struct super_block *sb)
16581+{
16582+ return au_test_fs_remote(sb)
16583+ || au_test_fs_bad_iattr_size(sb)
1facf9fc 16584+ /* don't want unnecessary work for xino */
16585+ || au_test_aufs(sb)
1308ab2a 16586+ || au_test_ecryptfs(sb)
16587+ || au_test_nilfs(sb);
1facf9fc 16588+}
16589+
16590+static inline int au_test_fs_trunc_xino(struct super_block *sb)
16591+{
16592+ return au_test_tmpfs(sb)
16593+ || au_test_ramfs(sb);
16594+}
16595+
16596+/*
16597+ * test if the @sb is real-readonly.
16598+ */
16599+static inline int au_test_fs_rr(struct super_block *sb)
16600+{
16601+ return au_test_squashfs(sb)
16602+ || au_test_iso9660(sb)
16603+ || au_test_cramfs(sb)
16604+ || au_test_romfs(sb);
16605+}
16606+
b912730e
AM
16607+/*
16608+ * test if the @inode is nfs with 'noacl' option
2121bcd9 16609+ * NFS always sets SB_POSIXACL regardless its mount option 'noacl.'
b912730e
AM
16610+ */
16611+static inline int au_test_nfs_noacl(struct inode *inode)
16612+{
16613+ return au_test_nfs(inode->i_sb)
16614+ /* && IS_POSIXACL(inode) */
16615+ && !nfs_server_capable(inode, NFS_CAP_ACLS);
16616+}
16617+
1facf9fc 16618+#endif /* __KERNEL__ */
16619+#endif /* __AUFS_FSTYPE_H__ */
8b6a4947 16620diff -urN /usr/share/empty/fs/aufs/hbl.h linux/fs/aufs/hbl.h
eca34b5c 16621--- /usr/share/empty/fs/aufs/hbl.h 1970-01-01 01:00:00.000000000 +0100
46016270 16622+++ linux/fs/aufs/hbl.h 2021-02-24 13:33:42.744347058 +0100
062440b3
AM
16623@@ -0,0 +1,65 @@
16624+/* SPDX-License-Identifier: GPL-2.0 */
8b6a4947 16625+/*
d58c55f2 16626+ * Copyright (C) 2017-2020 Junjiro R. Okajima
8b6a4947
AM
16627+ *
16628+ * This program, aufs is free software; you can redistribute it and/or modify
16629+ * it under the terms of the GNU General Public License as published by
16630+ * the Free Software Foundation; either version 2 of the License, or
16631+ * (at your option) any later version.
16632+ *
16633+ * This program is distributed in the hope that it will be useful,
16634+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16635+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16636+ * GNU General Public License for more details.
16637+ *
16638+ * You should have received a copy of the GNU General Public License
16639+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
16640+ */
16641+
16642+/*
16643+ * helpers for hlist_bl.h
16644+ */
16645+
16646+#ifndef __AUFS_HBL_H__
16647+#define __AUFS_HBL_H__
16648+
16649+#ifdef __KERNEL__
16650+
16651+#include <linux/list_bl.h>
16652+
16653+static inline void au_hbl_add(struct hlist_bl_node *node,
16654+ struct hlist_bl_head *hbl)
16655+{
16656+ hlist_bl_lock(hbl);
16657+ hlist_bl_add_head(node, hbl);
16658+ hlist_bl_unlock(hbl);
16659+}
16660+
16661+static inline void au_hbl_del(struct hlist_bl_node *node,
16662+ struct hlist_bl_head *hbl)
16663+{
16664+ hlist_bl_lock(hbl);
16665+ hlist_bl_del(node);
16666+ hlist_bl_unlock(hbl);
16667+}
16668+
16669+#define au_hbl_for_each(pos, head) \
16670+ for (pos = hlist_bl_first(head); \
16671+ pos; \
16672+ pos = pos->next)
16673+
16674+static inline unsigned long au_hbl_count(struct hlist_bl_head *hbl)
16675+{
16676+ unsigned long cnt;
16677+ struct hlist_bl_node *pos;
16678+
16679+ cnt = 0;
16680+ hlist_bl_lock(hbl);
16681+ au_hbl_for_each(pos, hbl)
16682+ cnt++;
16683+ hlist_bl_unlock(hbl);
16684+ return cnt;
16685+}
16686+
16687+#endif /* __KERNEL__ */
16688+#endif /* __AUFS_HBL_H__ */
7f207e10 16689diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
eca34b5c 16690--- /usr/share/empty/fs/aufs/hfsnotify.c 1970-01-01 01:00:00.000000000 +0100
46016270 16691+++ linux/fs/aufs/hfsnotify.c 2021-02-24 13:33:42.744347058 +0100
fbc438ed 16692@@ -0,0 +1,288 @@
cd7a4cd9 16693+// SPDX-License-Identifier: GPL-2.0
1facf9fc 16694+/*
d58c55f2 16695+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 16696+ *
16697+ * This program, aufs is free software; you can redistribute it and/or modify
16698+ * it under the terms of the GNU General Public License as published by
16699+ * the Free Software Foundation; either version 2 of the License, or
16700+ * (at your option) any later version.
dece6358
AM
16701+ *
16702+ * This program is distributed in the hope that it will be useful,
16703+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16704+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16705+ * GNU General Public License for more details.
16706+ *
16707+ * You should have received a copy of the GNU General Public License
523b37e3 16708+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 16709+ */
16710+
16711+/*
4a4d8108 16712+ * fsnotify for the lower directories
1facf9fc 16713+ */
16714+
16715+#include "aufs.h"
16716+
4a4d8108
AM
16717+/* FS_IN_IGNORED is unnecessary */
16718+static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
16719+ | FS_CREATE | FS_EVENT_ON_CHILD);
7f207e10 16720+static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
7eafdf33 16721+static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
1facf9fc 16722+
0c5527e5 16723+static void au_hfsn_free_mark(struct fsnotify_mark *mark)
1facf9fc 16724+{
0c5527e5
AM
16725+ struct au_hnotify *hn = container_of(mark, struct au_hnotify,
16726+ hn_mark);
5afbbe0d 16727+ /* AuDbg("here\n"); */
1c60b727 16728+ au_cache_free_hnotify(hn);
8b6a4947 16729+ smp_mb__before_atomic(); /* for atomic64_dec */
1716fcea
AM
16730+ if (atomic64_dec_and_test(&au_hfsn_ifree))
16731+ wake_up(&au_hfsn_wq);
4a4d8108 16732+}
1facf9fc 16733+
027c5e7a 16734+static int au_hfsn_alloc(struct au_hinode *hinode)
4a4d8108 16735+{
1716fcea 16736+ int err;
027c5e7a
AM
16737+ struct au_hnotify *hn;
16738+ struct super_block *sb;
16739+ struct au_branch *br;
0c5527e5 16740+ struct fsnotify_mark *mark;
027c5e7a 16741+ aufs_bindex_t bindex;
1facf9fc 16742+
027c5e7a
AM
16743+ hn = hinode->hi_notify;
16744+ sb = hn->hn_aufs_inode->i_sb;
16745+ bindex = au_br_index(sb, hinode->hi_id);
16746+ br = au_sbr(sb, bindex);
1716fcea
AM
16747+ AuDebugOn(!br->br_hfsn);
16748+
0c5527e5 16749+ mark = &hn->hn_mark;
ffa93bbd 16750+ fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
0c5527e5 16751+ mark->mask = AuHfsnMask;
7f207e10
AM
16752+ /*
16753+ * by udba rename or rmdir, aufs assign a new inode to the known
16754+ * h_inode, so specify 1 to allow dups.
16755+ */
c1595e42 16756+ lockdep_off();
acd2b654 16757+ err = fsnotify_add_inode_mark(mark, hinode->hi_inode, /*allow_dups*/1);
c1595e42 16758+ lockdep_on();
1716fcea
AM
16759+
16760+ return err;
1facf9fc 16761+}
16762+
7eafdf33 16763+static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
1facf9fc 16764+{
0c5527e5 16765+ struct fsnotify_mark *mark;
7eafdf33 16766+ unsigned long long ull;
1716fcea 16767+ struct fsnotify_group *group;
7eafdf33
AM
16768+
16769+ ull = atomic64_inc_return(&au_hfsn_ifree);
16770+ BUG_ON(!ull);
953406b4 16771+
0c5527e5 16772+ mark = &hn->hn_mark;
1716fcea
AM
16773+ spin_lock(&mark->lock);
16774+ group = mark->group;
16775+ fsnotify_get_group(group);
16776+ spin_unlock(&mark->lock);
c1595e42 16777+ lockdep_off();
1716fcea 16778+ fsnotify_destroy_mark(mark, group);
5afbbe0d 16779+ fsnotify_put_mark(mark);
1716fcea 16780+ fsnotify_put_group(group);
c1595e42 16781+ lockdep_on();
7f207e10 16782+
7eafdf33
AM
16783+ /* free hn by myself */
16784+ return 0;
1facf9fc 16785+}
16786+
16787+/* ---------------------------------------------------------------------- */
16788+
4a4d8108 16789+static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
1facf9fc 16790+{
0c5527e5 16791+ struct fsnotify_mark *mark;
1facf9fc 16792+
0c5527e5
AM
16793+ mark = &hinode->hi_notify->hn_mark;
16794+ spin_lock(&mark->lock);
1facf9fc 16795+ if (do_set) {
0c5527e5
AM
16796+ AuDebugOn(mark->mask & AuHfsnMask);
16797+ mark->mask |= AuHfsnMask;
1facf9fc 16798+ } else {
0c5527e5
AM
16799+ AuDebugOn(!(mark->mask & AuHfsnMask));
16800+ mark->mask &= ~AuHfsnMask;
1facf9fc 16801+ }
0c5527e5 16802+ spin_unlock(&mark->lock);
4a4d8108 16803+ /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
1facf9fc 16804+}
16805+
4a4d8108 16806+/* ---------------------------------------------------------------------- */
1facf9fc 16807+
4a4d8108
AM
16808+/* #define AuDbgHnotify */
16809+#ifdef AuDbgHnotify
16810+static char *au_hfsn_name(u32 mask)
16811+{
16812+#ifdef CONFIG_AUFS_DEBUG
c06a8ce3
AM
16813+#define test_ret(flag) \
16814+ do { \
16815+ if (mask & flag) \
16816+ return #flag; \
16817+ } while (0)
4a4d8108
AM
16818+ test_ret(FS_ACCESS);
16819+ test_ret(FS_MODIFY);
16820+ test_ret(FS_ATTRIB);
16821+ test_ret(FS_CLOSE_WRITE);
16822+ test_ret(FS_CLOSE_NOWRITE);
16823+ test_ret(FS_OPEN);
16824+ test_ret(FS_MOVED_FROM);
16825+ test_ret(FS_MOVED_TO);
16826+ test_ret(FS_CREATE);
16827+ test_ret(FS_DELETE);
16828+ test_ret(FS_DELETE_SELF);
16829+ test_ret(FS_MOVE_SELF);
16830+ test_ret(FS_UNMOUNT);
16831+ test_ret(FS_Q_OVERFLOW);
16832+ test_ret(FS_IN_IGNORED);
b912730e 16833+ test_ret(FS_ISDIR);
4a4d8108
AM
16834+ test_ret(FS_IN_ONESHOT);
16835+ test_ret(FS_EVENT_ON_CHILD);
16836+ return "";
16837+#undef test_ret
16838+#else
16839+ return "??";
16840+#endif
1facf9fc 16841+}
4a4d8108 16842+#endif
1facf9fc 16843+
16844+/* ---------------------------------------------------------------------- */
16845+
1716fcea
AM
16846+static void au_hfsn_free_group(struct fsnotify_group *group)
16847+{
16848+ struct au_br_hfsnotify *hfsn = group->private;
16849+
5afbbe0d 16850+ /* AuDbg("here\n"); */
9f237c51 16851+ au_kfree_try_rcu(hfsn);
1716fcea
AM
16852+}
16853+
4a4d8108 16854+static int au_hfsn_handle_event(struct fsnotify_group *group,
a2654f78 16855+ u32 mask, const void *data, int data_type,
e37dd06a 16856+ struct inode *dir,
83b672a5 16857+ const struct qstr *file_name, u32 cookie,
ffa93bbd 16858+ struct fsnotify_iter_info *iter_info)
1facf9fc 16859+{
16860+ int err;
4a4d8108
AM
16861+ struct au_hnotify *hnotify;
16862+ struct inode *h_dir, *h_inode;
cd7a4cd9 16863+ struct fsnotify_mark *inode_mark;
4a4d8108 16864+
fb47a38f 16865+ AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
1facf9fc 16866+
16867+ err = 0;
0c5527e5 16868+ /* if FS_UNMOUNT happens, there must be another bug */
4a4d8108 16869+ AuDebugOn(mask & FS_UNMOUNT);
0c5527e5 16870+ if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
1facf9fc 16871+ goto out;
1facf9fc 16872+
e37dd06a 16873+ h_dir = dir;
fb47a38f 16874+ h_inode = NULL;
4a4d8108 16875+#ifdef AuDbgHnotify
392086de 16876+ au_debug_on();
fbc438ed
JR
16877+ if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
16878+ || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
4a4d8108
AM
16879+ AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
16880+ h_dir->i_ino, mask, au_hfsn_name(mask),
fbc438ed 16881+ AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
4a4d8108 16882+ /* WARN_ON(1); */
1facf9fc 16883+ }
392086de 16884+ au_debug_off();
1facf9fc 16885+#endif
4a4d8108 16886+
cd7a4cd9 16887+ inode_mark = fsnotify_iter_inode_mark(iter_info);
0c5527e5
AM
16888+ AuDebugOn(!inode_mark);
16889+ hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
b62f1e65 16890+ err = au_hnotify(h_dir, hnotify, mask, file_name, h_inode);
1facf9fc 16891+
4a4d8108
AM
16892+out:
16893+ return err;
16894+}
1facf9fc 16895+
4a4d8108 16896+static struct fsnotify_ops au_hfsn_ops = {
1716fcea 16897+ .handle_event = au_hfsn_handle_event,
ffa93bbd
AM
16898+ .free_group_priv = au_hfsn_free_group,
16899+ .free_mark = au_hfsn_free_mark
4a4d8108
AM
16900+};
16901+
16902+/* ---------------------------------------------------------------------- */
16903+
027c5e7a
AM
16904+static void au_hfsn_fin_br(struct au_branch *br)
16905+{
1716fcea 16906+ struct au_br_hfsnotify *hfsn;
027c5e7a 16907+
1716fcea 16908+ hfsn = br->br_hfsn;
c1595e42
JR
16909+ if (hfsn) {
16910+ lockdep_off();
1716fcea 16911+ fsnotify_put_group(hfsn->hfsn_group);
c1595e42
JR
16912+ lockdep_on();
16913+ }
027c5e7a
AM
16914+}
16915+
1716fcea 16916+static int au_hfsn_init_br(struct au_branch *br, int perm)
4a4d8108
AM
16917+{
16918+ int err;
1716fcea
AM
16919+ struct fsnotify_group *group;
16920+ struct au_br_hfsnotify *hfsn;
1facf9fc 16921+
4a4d8108 16922+ err = 0;
1716fcea
AM
16923+ br->br_hfsn = NULL;
16924+ if (!au_br_hnotifyable(perm))
027c5e7a 16925+ goto out;
027c5e7a 16926+
1716fcea
AM
16927+ err = -ENOMEM;
16928+ hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
16929+ if (unlikely(!hfsn))
027c5e7a
AM
16930+ goto out;
16931+
1716fcea
AM
16932+ err = 0;
16933+ group = fsnotify_alloc_group(&au_hfsn_ops);
16934+ if (IS_ERR(group)) {
16935+ err = PTR_ERR(group);
0c5527e5 16936+ pr_err("fsnotify_alloc_group() failed, %d\n", err);
1716fcea 16937+ goto out_hfsn;
4a4d8108 16938+ }
1facf9fc 16939+
1716fcea
AM
16940+ group->private = hfsn;
16941+ hfsn->hfsn_group = group;
16942+ br->br_hfsn = hfsn;
16943+ goto out; /* success */
16944+
16945+out_hfsn:
9f237c51 16946+ au_kfree_try_rcu(hfsn);
027c5e7a 16947+out:
1716fcea
AM
16948+ return err;
16949+}
16950+
16951+static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
16952+{
16953+ int err;
16954+
16955+ err = 0;
16956+ if (!br->br_hfsn)
16957+ err = au_hfsn_init_br(br, perm);
16958+
1facf9fc 16959+ return err;
16960+}
16961+
7eafdf33
AM
16962+/* ---------------------------------------------------------------------- */
16963+
16964+static void au_hfsn_fin(void)
16965+{
16966+ AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
16967+ wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
16968+}
16969+
4a4d8108
AM
16970+const struct au_hnotify_op au_hnotify_op = {
16971+ .ctl = au_hfsn_ctl,
16972+ .alloc = au_hfsn_alloc,
16973+ .free = au_hfsn_free,
1facf9fc 16974+
7eafdf33
AM
16975+ .fin = au_hfsn_fin,
16976+
027c5e7a
AM
16977+ .reset_br = au_hfsn_reset_br,
16978+ .fin_br = au_hfsn_fin_br,
16979+ .init_br = au_hfsn_init_br
4a4d8108 16980+};
7f207e10 16981diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
eca34b5c 16982--- /usr/share/empty/fs/aufs/hfsplus.c 1970-01-01 01:00:00.000000000 +0100
46016270 16983+++ linux/fs/aufs/hfsplus.c 2021-02-24 13:33:42.744347058 +0100
acd2b654 16984@@ -0,0 +1,60 @@
cd7a4cd9 16985+// SPDX-License-Identifier: GPL-2.0
4a4d8108 16986+/*
d58c55f2 16987+ * Copyright (C) 2010-2020 Junjiro R. Okajima
4a4d8108
AM
16988+ *
16989+ * This program, aufs is free software; you can redistribute it and/or modify
16990+ * it under the terms of the GNU General Public License as published by
16991+ * the Free Software Foundation; either version 2 of the License, or
16992+ * (at your option) any later version.
16993+ *
16994+ * This program is distributed in the hope that it will be useful,
16995+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16996+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16997+ * GNU General Public License for more details.
16998+ *
16999+ * You should have received a copy of the GNU General Public License
523b37e3 17000+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 17001+ */
1facf9fc 17002+
4a4d8108 17003+/*
acd2b654 17004+ * special support for filesystems which acquires an inode mutex
4a4d8108
AM
17005+ * at final closing a file, eg, hfsplus.
17006+ *
17007+ * This trick is very simple and stupid, just to open the file before really
acd2b654 17008+ * necessary open to tell hfsplus that this is not the final closing.
4a4d8108
AM
17009+ * The caller should call au_h_open_pre() after acquiring the inode mutex,
17010+ * and au_h_open_post() after releasing it.
17011+ */
1facf9fc 17012+
4a4d8108 17013+#include "aufs.h"
1facf9fc 17014+
392086de
AM
17015+struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
17016+ int force_wr)
4a4d8108
AM
17017+{
17018+ struct file *h_file;
17019+ struct dentry *h_dentry;
1facf9fc 17020+
4a4d8108
AM
17021+ h_dentry = au_h_dptr(dentry, bindex);
17022+ AuDebugOn(!h_dentry);
5527c038 17023+ AuDebugOn(d_is_negative(h_dentry));
4a4d8108
AM
17024+
17025+ h_file = NULL;
17026+ if (au_test_hfsplus(h_dentry->d_sb)
7e9cd9fe 17027+ && d_is_reg(h_dentry))
4a4d8108
AM
17028+ h_file = au_h_open(dentry, bindex,
17029+ O_RDONLY | O_NOATIME | O_LARGEFILE,
392086de 17030+ /*file*/NULL, force_wr);
4a4d8108 17031+ return h_file;
1facf9fc 17032+}
17033+
4a4d8108
AM
17034+void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
17035+ struct file *h_file)
17036+{
acd2b654
AM
17037+ struct au_branch *br;
17038+
4a4d8108
AM
17039+ if (h_file) {
17040+ fput(h_file);
acd2b654
AM
17041+ br = au_sbr(dentry->d_sb, bindex);
17042+ au_lcnt_dec(&br->br_nfiles);
4a4d8108
AM
17043+ }
17044+}
7f207e10 17045diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
eca34b5c 17046--- /usr/share/empty/fs/aufs/hnotify.c 1970-01-01 01:00:00.000000000 +0100
46016270 17047+++ linux/fs/aufs/hnotify.c 2021-02-24 13:33:42.744347058 +0100
43982f53 17048@@ -0,0 +1,715 @@
cd7a4cd9 17049+// SPDX-License-Identifier: GPL-2.0
e49829fe 17050+/*
d58c55f2 17051+ * Copyright (C) 2005-2020 Junjiro R. Okajima
e49829fe
JR
17052+ *
17053+ * This program, aufs is free software; you can redistribute it and/or modify
17054+ * it under the terms of the GNU General Public License as published by
17055+ * the Free Software Foundation; either version 2 of the License, or
17056+ * (at your option) any later version.
17057+ *
17058+ * This program is distributed in the hope that it will be useful,
17059+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17060+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17061+ * GNU General Public License for more details.
17062+ *
17063+ * You should have received a copy of the GNU General Public License
523b37e3 17064+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
e49829fe
JR
17065+ */
17066+
17067+/*
7f207e10 17068+ * abstraction to notify the direct changes on lower directories
e49829fe
JR
17069+ */
17070+
eca801bf 17071+/* #include <linux/iversion.h> */
e49829fe
JR
17072+#include "aufs.h"
17073+
027c5e7a 17074+int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
e49829fe
JR
17075+{
17076+ int err;
7f207e10 17077+ struct au_hnotify *hn;
1facf9fc 17078+
4a4d8108
AM
17079+ err = -ENOMEM;
17080+ hn = au_cache_alloc_hnotify();
17081+ if (hn) {
17082+ hn->hn_aufs_inode = inode;
027c5e7a
AM
17083+ hinode->hi_notify = hn;
17084+ err = au_hnotify_op.alloc(hinode);
17085+ AuTraceErr(err);
17086+ if (unlikely(err)) {
17087+ hinode->hi_notify = NULL;
1c60b727 17088+ au_cache_free_hnotify(hn);
4a4d8108
AM
17089+ /*
17090+ * The upper dir was removed by udba, but the same named
acd2b654 17091+ * dir left. In this case, aufs assigns a new inode
4a4d8108 17092+ * number and set the monitor again.
acd2b654 17093+ * For the lower dir, the old monitor is still left.
4a4d8108
AM
17094+ */
17095+ if (err == -EEXIST)
17096+ err = 0;
17097+ }
1308ab2a 17098+ }
1308ab2a 17099+
027c5e7a 17100+ AuTraceErr(err);
1308ab2a 17101+ return err;
dece6358 17102+}
1facf9fc 17103+
4a4d8108 17104+void au_hn_free(struct au_hinode *hinode)
dece6358 17105+{
4a4d8108 17106+ struct au_hnotify *hn;
1facf9fc 17107+
4a4d8108
AM
17108+ hn = hinode->hi_notify;
17109+ if (hn) {
4a4d8108 17110+ hinode->hi_notify = NULL;
7eafdf33 17111+ if (au_hnotify_op.free(hinode, hn))
1c60b727 17112+ au_cache_free_hnotify(hn);
4a4d8108
AM
17113+ }
17114+}
dece6358 17115+
4a4d8108 17116+/* ---------------------------------------------------------------------- */
dece6358 17117+
4a4d8108
AM
17118+void au_hn_ctl(struct au_hinode *hinode, int do_set)
17119+{
17120+ if (hinode->hi_notify)
17121+ au_hnotify_op.ctl(hinode, do_set);
17122+}
17123+
17124+void au_hn_reset(struct inode *inode, unsigned int flags)
17125+{
5afbbe0d 17126+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
17127+ struct inode *hi;
17128+ struct dentry *iwhdentry;
1facf9fc 17129+
5afbbe0d
AM
17130+ bbot = au_ibbot(inode);
17131+ for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
4a4d8108
AM
17132+ hi = au_h_iptr(inode, bindex);
17133+ if (!hi)
17134+ continue;
1308ab2a 17135+
febd17d6 17136+ /* inode_lock_nested(hi, AuLsc_I_CHILD); */
4a4d8108
AM
17137+ iwhdentry = au_hi_wh(inode, bindex);
17138+ if (iwhdentry)
17139+ dget(iwhdentry);
17140+ au_igrab(hi);
17141+ au_set_h_iptr(inode, bindex, NULL, 0);
17142+ au_set_h_iptr(inode, bindex, au_igrab(hi),
17143+ flags & ~AuHi_XINO);
17144+ iput(hi);
17145+ dput(iwhdentry);
febd17d6 17146+ /* inode_unlock(hi); */
1facf9fc 17147+ }
1facf9fc 17148+}
17149+
1308ab2a 17150+/* ---------------------------------------------------------------------- */
1facf9fc 17151+
4a4d8108 17152+static int hn_xino(struct inode *inode, struct inode *h_inode)
1facf9fc 17153+{
4a4d8108 17154+ int err;
5afbbe0d 17155+ aufs_bindex_t bindex, bbot, bfound, btop;
4a4d8108 17156+ struct inode *h_i;
1facf9fc 17157+
4a4d8108
AM
17158+ err = 0;
17159+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 17160+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
17161+ goto out;
17162+ }
1facf9fc 17163+
4a4d8108 17164+ bfound = -1;
5afbbe0d
AM
17165+ bbot = au_ibbot(inode);
17166+ btop = au_ibtop(inode);
4a4d8108 17167+#if 0 /* reserved for future use */
5afbbe0d 17168+ if (bindex == bbot) {
4a4d8108
AM
17169+ /* keep this ino in rename case */
17170+ goto out;
17171+ }
17172+#endif
5afbbe0d 17173+ for (bindex = btop; bindex <= bbot; bindex++)
4a4d8108
AM
17174+ if (au_h_iptr(inode, bindex) == h_inode) {
17175+ bfound = bindex;
17176+ break;
17177+ }
17178+ if (bfound < 0)
1308ab2a 17179+ goto out;
1facf9fc 17180+
5afbbe0d 17181+ for (bindex = btop; bindex <= bbot; bindex++) {
4a4d8108
AM
17182+ h_i = au_h_iptr(inode, bindex);
17183+ if (!h_i)
17184+ continue;
1facf9fc 17185+
4a4d8108
AM
17186+ err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
17187+ /* ignore this error */
17188+ /* bad action? */
1facf9fc 17189+ }
1facf9fc 17190+
4a4d8108 17191+ /* children inode number will be broken */
1facf9fc 17192+
4f0767ce 17193+out:
4a4d8108
AM
17194+ AuTraceErr(err);
17195+ return err;
1facf9fc 17196+}
17197+
4a4d8108 17198+static int hn_gen_tree(struct dentry *dentry)
1facf9fc 17199+{
4a4d8108
AM
17200+ int err, i, j, ndentry;
17201+ struct au_dcsub_pages dpages;
17202+ struct au_dpage *dpage;
17203+ struct dentry **dentries;
1facf9fc 17204+
4a4d8108
AM
17205+ err = au_dpages_init(&dpages, GFP_NOFS);
17206+ if (unlikely(err))
17207+ goto out;
17208+ err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
17209+ if (unlikely(err))
17210+ goto out_dpages;
1facf9fc 17211+
4a4d8108
AM
17212+ for (i = 0; i < dpages.ndpage; i++) {
17213+ dpage = dpages.dpages + i;
17214+ dentries = dpage->dentries;
17215+ ndentry = dpage->ndentry;
17216+ for (j = 0; j < ndentry; j++) {
17217+ struct dentry *d;
17218+
17219+ d = dentries[j];
17220+ if (IS_ROOT(d))
17221+ continue;
17222+
4a4d8108 17223+ au_digen_dec(d);
5527c038 17224+ if (d_really_is_positive(d))
4a4d8108
AM
17225+ /* todo: reset children xino?
17226+ cached children only? */
5527c038 17227+ au_iigen_dec(d_inode(d));
1308ab2a 17228+ }
dece6358 17229+ }
1facf9fc 17230+
4f0767ce 17231+out_dpages:
4a4d8108 17232+ au_dpages_free(&dpages);
4f0767ce 17233+out:
dece6358
AM
17234+ return err;
17235+}
17236+
1308ab2a 17237+/*
4a4d8108 17238+ * return 0 if processed.
1308ab2a 17239+ */
4a4d8108
AM
17240+static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
17241+ const unsigned int isdir)
dece6358 17242+{
1308ab2a 17243+ int err;
4a4d8108
AM
17244+ struct dentry *d;
17245+ struct qstr *dname;
1facf9fc 17246+
4a4d8108
AM
17247+ err = 1;
17248+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 17249+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
17250+ err = 0;
17251+ goto out;
17252+ }
dece6358 17253+
4a4d8108
AM
17254+ if (!isdir) {
17255+ AuDebugOn(!name);
17256+ au_iigen_dec(inode);
027c5e7a 17257+ spin_lock(&inode->i_lock);
c1595e42 17258+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
027c5e7a 17259+ spin_lock(&d->d_lock);
4a4d8108
AM
17260+ dname = &d->d_name;
17261+ if (dname->len != nlen
027c5e7a
AM
17262+ && memcmp(dname->name, name, nlen)) {
17263+ spin_unlock(&d->d_lock);
4a4d8108 17264+ continue;
027c5e7a 17265+ }
4a4d8108 17266+ err = 0;
4a4d8108
AM
17267+ au_digen_dec(d);
17268+ spin_unlock(&d->d_lock);
17269+ break;
1facf9fc 17270+ }
027c5e7a 17271+ spin_unlock(&inode->i_lock);
1308ab2a 17272+ } else {
027c5e7a 17273+ au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
c1595e42 17274+ d = d_find_any_alias(inode);
4a4d8108
AM
17275+ if (!d) {
17276+ au_iigen_dec(inode);
17277+ goto out;
17278+ }
1facf9fc 17279+
027c5e7a 17280+ spin_lock(&d->d_lock);
4a4d8108 17281+ dname = &d->d_name;
027c5e7a
AM
17282+ if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
17283+ spin_unlock(&d->d_lock);
4a4d8108 17284+ err = hn_gen_tree(d);
027c5e7a
AM
17285+ spin_lock(&d->d_lock);
17286+ }
17287+ spin_unlock(&d->d_lock);
4a4d8108
AM
17288+ dput(d);
17289+ }
1facf9fc 17290+
4f0767ce 17291+out:
4a4d8108 17292+ AuTraceErr(err);
1308ab2a 17293+ return err;
17294+}
dece6358 17295+
4a4d8108 17296+static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
1facf9fc 17297+{
4a4d8108 17298+ int err;
1facf9fc 17299+
5527c038 17300+ if (IS_ROOT(dentry)) {
0c3ec466 17301+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
17302+ return 0;
17303+ }
1308ab2a 17304+
4a4d8108
AM
17305+ err = 0;
17306+ if (!isdir) {
4a4d8108 17307+ au_digen_dec(dentry);
5527c038
JR
17308+ if (d_really_is_positive(dentry))
17309+ au_iigen_dec(d_inode(dentry));
4a4d8108 17310+ } else {
027c5e7a 17311+ au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
5527c038 17312+ if (d_really_is_positive(dentry))
4a4d8108
AM
17313+ err = hn_gen_tree(dentry);
17314+ }
17315+
17316+ AuTraceErr(err);
17317+ return err;
1facf9fc 17318+}
17319+
4a4d8108 17320+/* ---------------------------------------------------------------------- */
1facf9fc 17321+
4a4d8108
AM
17322+/* hnotify job flags */
17323+#define AuHnJob_XINO0 1
17324+#define AuHnJob_GEN (1 << 1)
17325+#define AuHnJob_DIRENT (1 << 2)
17326+#define AuHnJob_ISDIR (1 << 3)
17327+#define AuHnJob_TRYXINO0 (1 << 4)
17328+#define AuHnJob_MNTPNT (1 << 5)
17329+#define au_ftest_hnjob(flags, name) ((flags) & AuHnJob_##name)
7f207e10
AM
17330+#define au_fset_hnjob(flags, name) \
17331+ do { (flags) |= AuHnJob_##name; } while (0)
17332+#define au_fclr_hnjob(flags, name) \
17333+ do { (flags) &= ~AuHnJob_##name; } while (0)
1facf9fc 17334+
4a4d8108
AM
17335+enum {
17336+ AuHn_CHILD,
17337+ AuHn_PARENT,
17338+ AuHnLast
17339+};
1facf9fc 17340+
4a4d8108
AM
17341+struct au_hnotify_args {
17342+ struct inode *h_dir, *dir, *h_child_inode;
17343+ u32 mask;
17344+ unsigned int flags[AuHnLast];
17345+ unsigned int h_child_nlen;
17346+ char h_child_name[];
17347+};
1facf9fc 17348+
4a4d8108
AM
17349+struct hn_job_args {
17350+ unsigned int flags;
17351+ struct inode *inode, *h_inode, *dir, *h_dir;
17352+ struct dentry *dentry;
17353+ char *h_name;
17354+ int h_nlen;
17355+};
1308ab2a 17356+
4a4d8108
AM
17357+static int hn_job(struct hn_job_args *a)
17358+{
17359+ const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
076b876e 17360+ int e;
1308ab2a 17361+
4a4d8108
AM
17362+ /* reset xino */
17363+ if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
17364+ hn_xino(a->inode, a->h_inode); /* ignore this error */
1308ab2a 17365+
4a4d8108
AM
17366+ if (au_ftest_hnjob(a->flags, TRYXINO0)
17367+ && a->inode
17368+ && a->h_inode) {
be118d29 17369+ inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
38d290e6
JR
17370+ if (!a->h_inode->i_nlink
17371+ && !(a->h_inode->i_state & I_LINKABLE))
4a4d8108 17372+ hn_xino(a->inode, a->h_inode); /* ignore this error */
3c1bdaff 17373+ inode_unlock_shared(a->h_inode);
1308ab2a 17374+ }
1facf9fc 17375+
4a4d8108
AM
17376+ /* make the generation obsolete */
17377+ if (au_ftest_hnjob(a->flags, GEN)) {
076b876e 17378+ e = -1;
4a4d8108 17379+ if (a->inode)
076b876e 17380+ e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
4a4d8108 17381+ isdir);
076b876e 17382+ if (e && a->dentry)
4a4d8108
AM
17383+ hn_gen_by_name(a->dentry, isdir);
17384+ /* ignore this error */
1facf9fc 17385+ }
1facf9fc 17386+
4a4d8108
AM
17387+ /* make dir entries obsolete */
17388+ if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
17389+ struct au_vdir *vdir;
1facf9fc 17390+
4a4d8108
AM
17391+ vdir = au_ivdir(a->inode);
17392+ if (vdir)
17393+ vdir->vd_jiffy = 0;
17394+ /* IMustLock(a->inode); */
be118d29 17395+ /* inode_inc_iversion(a->inode); */
4a4d8108 17396+ }
1facf9fc 17397+
4a4d8108
AM
17398+ /* can do nothing but warn */
17399+ if (au_ftest_hnjob(a->flags, MNTPNT)
17400+ && a->dentry
17401+ && d_mountpoint(a->dentry))
523b37e3 17402+ pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
1facf9fc 17403+
4a4d8108 17404+ return 0;
1308ab2a 17405+}
1facf9fc 17406+
1308ab2a 17407+/* ---------------------------------------------------------------------- */
1facf9fc 17408+
4a4d8108
AM
17409+static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
17410+ struct inode *dir)
1308ab2a 17411+{
4a4d8108
AM
17412+ struct dentry *dentry, *d, *parent;
17413+ struct qstr *dname;
1308ab2a 17414+
c1595e42 17415+ parent = d_find_any_alias(dir);
4a4d8108
AM
17416+ if (!parent)
17417+ return NULL;
1308ab2a 17418+
4a4d8108 17419+ dentry = NULL;
027c5e7a 17420+ spin_lock(&parent->d_lock);
c1595e42 17421+ list_for_each_entry(d, &parent->d_subdirs, d_child) {
523b37e3 17422+ /* AuDbg("%pd\n", d); */
027c5e7a 17423+ spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
4a4d8108
AM
17424+ dname = &d->d_name;
17425+ if (dname->len != nlen || memcmp(dname->name, name, nlen))
027c5e7a
AM
17426+ goto cont_unlock;
17427+ if (au_di(d))
17428+ au_digen_dec(d);
17429+ else
17430+ goto cont_unlock;
c1595e42 17431+ if (au_dcount(d) > 0) {
027c5e7a 17432+ dentry = dget_dlock(d);
4a4d8108 17433+ spin_unlock(&d->d_lock);
027c5e7a 17434+ break;
dece6358 17435+ }
1facf9fc 17436+
f6b6e03d 17437+cont_unlock:
027c5e7a 17438+ spin_unlock(&d->d_lock);
1308ab2a 17439+ }
027c5e7a 17440+ spin_unlock(&parent->d_lock);
4a4d8108 17441+ dput(parent);
1facf9fc 17442+
4a4d8108
AM
17443+ if (dentry)
17444+ di_write_lock_child(dentry);
1308ab2a 17445+
4a4d8108
AM
17446+ return dentry;
17447+}
dece6358 17448+
4a4d8108
AM
17449+static struct inode *lookup_wlock_by_ino(struct super_block *sb,
17450+ aufs_bindex_t bindex, ino_t h_ino)
17451+{
17452+ struct inode *inode;
17453+ ino_t ino;
17454+ int err;
17455+
17456+ inode = NULL;
17457+ err = au_xino_read(sb, bindex, h_ino, &ino);
17458+ if (!err && ino)
17459+ inode = ilookup(sb, ino);
17460+ if (!inode)
17461+ goto out;
17462+
17463+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 17464+ pr_warn("wrong root branch\n");
4a4d8108
AM
17465+ iput(inode);
17466+ inode = NULL;
17467+ goto out;
1308ab2a 17468+ }
17469+
4a4d8108 17470+ ii_write_lock_child(inode);
1308ab2a 17471+
4f0767ce 17472+out:
4a4d8108 17473+ return inode;
dece6358
AM
17474+}
17475+
4a4d8108 17476+static void au_hn_bh(void *_args)
1facf9fc 17477+{
4a4d8108
AM
17478+ struct au_hnotify_args *a = _args;
17479+ struct super_block *sb;
5afbbe0d 17480+ aufs_bindex_t bindex, bbot, bfound;
4a4d8108 17481+ unsigned char xino, try_iput;
1facf9fc 17482+ int err;
1308ab2a 17483+ struct inode *inode;
4a4d8108
AM
17484+ ino_t h_ino;
17485+ struct hn_job_args args;
17486+ struct dentry *dentry;
17487+ struct au_sbinfo *sbinfo;
1facf9fc 17488+
4a4d8108
AM
17489+ AuDebugOn(!_args);
17490+ AuDebugOn(!a->h_dir);
17491+ AuDebugOn(!a->dir);
17492+ AuDebugOn(!a->mask);
17493+ AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
17494+ a->mask, a->dir->i_ino, a->h_dir->i_ino,
17495+ a->h_child_inode ? a->h_child_inode->i_ino : 0);
1facf9fc 17496+
4a4d8108
AM
17497+ inode = NULL;
17498+ dentry = NULL;
17499+ /*
17500+ * do not lock a->dir->i_mutex here
17501+ * because of d_revalidate() may cause a deadlock.
17502+ */
17503+ sb = a->dir->i_sb;
17504+ AuDebugOn(!sb);
17505+ sbinfo = au_sbi(sb);
17506+ AuDebugOn(!sbinfo);
7f207e10 17507+ si_write_lock(sb, AuLock_NOPLMW);
1facf9fc 17508+
8b6a4947
AM
17509+ if (au_opt_test(sbinfo->si_mntflags, DIRREN))
17510+ switch (a->mask & FS_EVENTS_POSS_ON_CHILD) {
17511+ case FS_MOVED_FROM:
17512+ case FS_MOVED_TO:
17513+ AuWarn1("DIRREN with UDBA may not work correctly "
17514+ "for the direct rename(2)\n");
17515+ }
17516+
4a4d8108
AM
17517+ ii_read_lock_parent(a->dir);
17518+ bfound = -1;
5afbbe0d
AM
17519+ bbot = au_ibbot(a->dir);
17520+ for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
4a4d8108
AM
17521+ if (au_h_iptr(a->dir, bindex) == a->h_dir) {
17522+ bfound = bindex;
17523+ break;
17524+ }
17525+ ii_read_unlock(a->dir);
17526+ if (unlikely(bfound < 0))
17527+ goto out;
1facf9fc 17528+
4a4d8108
AM
17529+ xino = !!au_opt_test(au_mntflags(sb), XINO);
17530+ h_ino = 0;
17531+ if (a->h_child_inode)
17532+ h_ino = a->h_child_inode->i_ino;
1facf9fc 17533+
4a4d8108
AM
17534+ if (a->h_child_nlen
17535+ && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
17536+ || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
17537+ dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
17538+ a->dir);
17539+ try_iput = 0;
5527c038
JR
17540+ if (dentry && d_really_is_positive(dentry))
17541+ inode = d_inode(dentry);
4a4d8108
AM
17542+ if (xino && !inode && h_ino
17543+ && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
17544+ || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
17545+ || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
17546+ inode = lookup_wlock_by_ino(sb, bfound, h_ino);
17547+ try_iput = 1;
f0c0a007 17548+ }
1facf9fc 17549+
4a4d8108
AM
17550+ args.flags = a->flags[AuHn_CHILD];
17551+ args.dentry = dentry;
17552+ args.inode = inode;
17553+ args.h_inode = a->h_child_inode;
17554+ args.dir = a->dir;
17555+ args.h_dir = a->h_dir;
17556+ args.h_name = a->h_child_name;
17557+ args.h_nlen = a->h_child_nlen;
17558+ err = hn_job(&args);
17559+ if (dentry) {
027c5e7a 17560+ if (au_di(dentry))
4a4d8108
AM
17561+ di_write_unlock(dentry);
17562+ dput(dentry);
17563+ }
17564+ if (inode && try_iput) {
17565+ ii_write_unlock(inode);
17566+ iput(inode);
17567+ }
1facf9fc 17568+
4a4d8108
AM
17569+ ii_write_lock_parent(a->dir);
17570+ args.flags = a->flags[AuHn_PARENT];
17571+ args.dentry = NULL;
17572+ args.inode = a->dir;
17573+ args.h_inode = a->h_dir;
17574+ args.dir = NULL;
17575+ args.h_dir = NULL;
17576+ args.h_name = NULL;
17577+ args.h_nlen = 0;
17578+ err = hn_job(&args);
17579+ ii_write_unlock(a->dir);
1facf9fc 17580+
4f0767ce 17581+out:
4a4d8108
AM
17582+ iput(a->h_child_inode);
17583+ iput(a->h_dir);
17584+ iput(a->dir);
027c5e7a
AM
17585+ si_write_unlock(sb);
17586+ au_nwt_done(&sbinfo->si_nowait);
9f237c51 17587+ au_kfree_rcu(a);
dece6358 17588+}
1facf9fc 17589+
4a4d8108
AM
17590+/* ---------------------------------------------------------------------- */
17591+
17592+int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
b62f1e65 17593+ const struct qstr *h_child_qstr, struct inode *h_child_inode)
dece6358 17594+{
4a4d8108 17595+ int err, len;
53392da6 17596+ unsigned int flags[AuHnLast], f;
4a4d8108
AM
17597+ unsigned char isdir, isroot, wh;
17598+ struct inode *dir;
17599+ struct au_hnotify_args *args;
17600+ char *p, *h_child_name;
dece6358 17601+
1308ab2a 17602+ err = 0;
4a4d8108
AM
17603+ AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
17604+ dir = igrab(hnotify->hn_aufs_inode);
17605+ if (!dir)
17606+ goto out;
1facf9fc 17607+
4a4d8108
AM
17608+ isroot = (dir->i_ino == AUFS_ROOT_INO);
17609+ wh = 0;
17610+ h_child_name = (void *)h_child_qstr->name;
17611+ len = h_child_qstr->len;
17612+ if (h_child_name) {
17613+ if (len > AUFS_WH_PFX_LEN
17614+ && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
17615+ h_child_name += AUFS_WH_PFX_LEN;
17616+ len -= AUFS_WH_PFX_LEN;
17617+ wh = 1;
17618+ }
1facf9fc 17619+ }
dece6358 17620+
4a4d8108
AM
17621+ isdir = 0;
17622+ if (h_child_inode)
17623+ isdir = !!S_ISDIR(h_child_inode->i_mode);
17624+ flags[AuHn_PARENT] = AuHnJob_ISDIR;
17625+ flags[AuHn_CHILD] = 0;
17626+ if (isdir)
17627+ flags[AuHn_CHILD] = AuHnJob_ISDIR;
17628+ au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
17629+ au_fset_hnjob(flags[AuHn_CHILD], GEN);
fbc438ed 17630+ switch (mask & ALL_FSNOTIFY_DIRENT_EVENTS) {
4a4d8108
AM
17631+ case FS_MOVED_FROM:
17632+ case FS_MOVED_TO:
17633+ au_fset_hnjob(flags[AuHn_CHILD], XINO0);
17634+ au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
42a736d3 17635+ fallthrough;
4a4d8108 17636+ case FS_CREATE:
fb47a38f 17637+ AuDebugOn(!h_child_name);
4a4d8108 17638+ break;
1facf9fc 17639+
4a4d8108
AM
17640+ case FS_DELETE:
17641+ /*
17642+ * aufs never be able to get this child inode.
17643+ * revalidation should be in d_revalidate()
17644+ * by checking i_nlink, i_generation or d_unhashed().
17645+ */
17646+ AuDebugOn(!h_child_name);
17647+ au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
17648+ au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17649+ break;
dece6358 17650+
4a4d8108
AM
17651+ default:
17652+ AuDebugOn(1);
17653+ }
1308ab2a 17654+
4a4d8108
AM
17655+ if (wh)
17656+ h_child_inode = NULL;
1308ab2a 17657+
4a4d8108
AM
17658+ err = -ENOMEM;
17659+ /* iput() and kfree() will be called in au_hnotify() */
4a4d8108 17660+ args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
4a4d8108
AM
17661+ if (unlikely(!args)) {
17662+ AuErr1("no memory\n");
17663+ iput(dir);
17664+ goto out;
17665+ }
17666+ args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
17667+ args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
17668+ args->mask = mask;
17669+ args->dir = dir;
17670+ args->h_dir = igrab(h_dir);
17671+ if (h_child_inode)
17672+ h_child_inode = igrab(h_child_inode); /* can be NULL */
17673+ args->h_child_inode = h_child_inode;
17674+ args->h_child_nlen = len;
17675+ if (len) {
17676+ p = (void *)args;
17677+ p += sizeof(*args);
17678+ memcpy(p, h_child_name, len);
17679+ p[len] = 0;
1308ab2a 17680+ }
1308ab2a 17681+
38d290e6 17682+ /* NFS fires the event for silly-renamed one from kworker */
53392da6 17683+ f = 0;
38d290e6
JR
17684+ if (!dir->i_nlink
17685+ || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
53392da6
AM
17686+ f = AuWkq_NEST;
17687+ err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
4a4d8108
AM
17688+ if (unlikely(err)) {
17689+ pr_err("wkq %d\n", err);
17690+ iput(args->h_child_inode);
17691+ iput(args->h_dir);
17692+ iput(args->dir);
9f237c51 17693+ au_kfree_rcu(args);
1facf9fc 17694+ }
1facf9fc 17695+
4a4d8108 17696+out:
1facf9fc 17697+ return err;
17698+}
17699+
027c5e7a
AM
17700+/* ---------------------------------------------------------------------- */
17701+
17702+int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
17703+{
17704+ int err;
17705+
17706+ AuDebugOn(!(udba & AuOptMask_UDBA));
17707+
17708+ err = 0;
17709+ if (au_hnotify_op.reset_br)
17710+ err = au_hnotify_op.reset_br(udba, br, perm);
17711+
17712+ return err;
17713+}
17714+
17715+int au_hnotify_init_br(struct au_branch *br, int perm)
17716+{
17717+ int err;
17718+
17719+ err = 0;
17720+ if (au_hnotify_op.init_br)
17721+ err = au_hnotify_op.init_br(br, perm);
17722+
17723+ return err;
17724+}
17725+
17726+void au_hnotify_fin_br(struct au_branch *br)
17727+{
17728+ if (au_hnotify_op.fin_br)
17729+ au_hnotify_op.fin_br(br);
17730+}
17731+
4a4d8108
AM
17732+static void au_hn_destroy_cache(void)
17733+{
1c60b727
AM
17734+ kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
17735+ au_cache[AuCache_HNOTIFY] = NULL;
4a4d8108 17736+}
1308ab2a 17737+
4a4d8108 17738+int __init au_hnotify_init(void)
1facf9fc 17739+{
1308ab2a 17740+ int err;
1308ab2a 17741+
4a4d8108 17742+ err = -ENOMEM;
1c60b727
AM
17743+ au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
17744+ if (au_cache[AuCache_HNOTIFY]) {
027c5e7a
AM
17745+ err = 0;
17746+ if (au_hnotify_op.init)
17747+ err = au_hnotify_op.init();
4a4d8108
AM
17748+ if (unlikely(err))
17749+ au_hn_destroy_cache();
1308ab2a 17750+ }
1308ab2a 17751+ AuTraceErr(err);
4a4d8108 17752+ return err;
1308ab2a 17753+}
17754+
4a4d8108 17755+void au_hnotify_fin(void)
1308ab2a 17756+{
027c5e7a
AM
17757+ if (au_hnotify_op.fin)
17758+ au_hnotify_op.fin();
f0c0a007 17759+
4a4d8108 17760+ /* cf. au_cache_fin() */
1c60b727 17761+ if (au_cache[AuCache_HNOTIFY])
4a4d8108 17762+ au_hn_destroy_cache();
dece6358 17763+}
7f207e10 17764diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
eca34b5c 17765--- /usr/share/empty/fs/aufs/iinfo.c 1970-01-01 01:00:00.000000000 +0100
46016270 17766+++ linux/fs/aufs/iinfo.c 2021-02-24 13:33:42.744347058 +0100
062440b3 17767@@ -0,0 +1,286 @@
cd7a4cd9 17768+// SPDX-License-Identifier: GPL-2.0
dece6358 17769+/*
d58c55f2 17770+ * Copyright (C) 2005-2020 Junjiro R. Okajima
dece6358
AM
17771+ *
17772+ * This program, aufs is free software; you can redistribute it and/or modify
17773+ * it under the terms of the GNU General Public License as published by
17774+ * the Free Software Foundation; either version 2 of the License, or
17775+ * (at your option) any later version.
17776+ *
17777+ * This program is distributed in the hope that it will be useful,
17778+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17779+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17780+ * GNU General Public License for more details.
17781+ *
17782+ * You should have received a copy of the GNU General Public License
523b37e3 17783+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358 17784+ */
1facf9fc 17785+
dece6358 17786+/*
4a4d8108 17787+ * inode private data
dece6358 17788+ */
1facf9fc 17789+
1308ab2a 17790+#include "aufs.h"
1facf9fc 17791+
4a4d8108 17792+struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 17793+{
4a4d8108 17794+ struct inode *h_inode;
5afbbe0d 17795+ struct au_hinode *hinode;
1facf9fc 17796+
4a4d8108 17797+ IiMustAnyLock(inode);
1facf9fc 17798+
5afbbe0d
AM
17799+ hinode = au_hinode(au_ii(inode), bindex);
17800+ h_inode = hinode->hi_inode;
4a4d8108
AM
17801+ AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
17802+ return h_inode;
17803+}
1facf9fc 17804+
4a4d8108
AM
17805+/* todo: hard/soft set? */
17806+void au_hiput(struct au_hinode *hinode)
17807+{
17808+ au_hn_free(hinode);
17809+ dput(hinode->hi_whdentry);
17810+ iput(hinode->hi_inode);
17811+}
1facf9fc 17812+
4a4d8108
AM
17813+unsigned int au_hi_flags(struct inode *inode, int isdir)
17814+{
17815+ unsigned int flags;
17816+ const unsigned int mnt_flags = au_mntflags(inode->i_sb);
1facf9fc 17817+
4a4d8108
AM
17818+ flags = 0;
17819+ if (au_opt_test(mnt_flags, XINO))
17820+ au_fset_hi(flags, XINO);
17821+ if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
17822+ au_fset_hi(flags, HNOTIFY);
17823+ return flags;
1facf9fc 17824+}
17825+
4a4d8108
AM
17826+void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
17827+ struct inode *h_inode, unsigned int flags)
1308ab2a 17828+{
4a4d8108
AM
17829+ struct au_hinode *hinode;
17830+ struct inode *hi;
17831+ struct au_iinfo *iinfo = au_ii(inode);
1facf9fc 17832+
4a4d8108 17833+ IiMustWriteLock(inode);
dece6358 17834+
5afbbe0d 17835+ hinode = au_hinode(iinfo, bindex);
4a4d8108
AM
17836+ hi = hinode->hi_inode;
17837+ AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
17838+
17839+ if (hi)
17840+ au_hiput(hinode);
17841+ hinode->hi_inode = h_inode;
17842+ if (h_inode) {
17843+ int err;
17844+ struct super_block *sb = inode->i_sb;
17845+ struct au_branch *br;
17846+
027c5e7a
AM
17847+ AuDebugOn(inode->i_mode
17848+ && (h_inode->i_mode & S_IFMT)
17849+ != (inode->i_mode & S_IFMT));
5afbbe0d 17850+ if (bindex == iinfo->ii_btop)
4a4d8108
AM
17851+ au_cpup_igen(inode, h_inode);
17852+ br = au_sbr(sb, bindex);
17853+ hinode->hi_id = br->br_id;
17854+ if (au_ftest_hi(flags, XINO)) {
17855+ err = au_xino_write(sb, bindex, h_inode->i_ino,
17856+ inode->i_ino);
17857+ if (unlikely(err))
17858+ AuIOErr1("failed au_xino_write() %d\n", err);
17859+ }
17860+
17861+ if (au_ftest_hi(flags, HNOTIFY)
17862+ && au_br_hnotifyable(br->br_perm)) {
027c5e7a 17863+ err = au_hn_alloc(hinode, inode);
4a4d8108
AM
17864+ if (unlikely(err))
17865+ AuIOErr1("au_hn_alloc() %d\n", err);
1308ab2a 17866+ }
17867+ }
4a4d8108 17868+}
dece6358 17869+
4a4d8108
AM
17870+void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
17871+ struct dentry *h_wh)
17872+{
17873+ struct au_hinode *hinode;
dece6358 17874+
4a4d8108
AM
17875+ IiMustWriteLock(inode);
17876+
5afbbe0d 17877+ hinode = au_hinode(au_ii(inode), bindex);
4a4d8108
AM
17878+ AuDebugOn(hinode->hi_whdentry);
17879+ hinode->hi_whdentry = h_wh;
1facf9fc 17880+}
17881+
537831f9 17882+void au_update_iigen(struct inode *inode, int half)
1308ab2a 17883+{
537831f9
AM
17884+ struct au_iinfo *iinfo;
17885+ struct au_iigen *iigen;
17886+ unsigned int sigen;
17887+
17888+ sigen = au_sigen(inode->i_sb);
17889+ iinfo = au_ii(inode);
17890+ iigen = &iinfo->ii_generation;
be52b249 17891+ spin_lock(&iigen->ig_spin);
537831f9
AM
17892+ iigen->ig_generation = sigen;
17893+ if (half)
17894+ au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
17895+ else
17896+ au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
be52b249 17897+ spin_unlock(&iigen->ig_spin);
4a4d8108 17898+}
1facf9fc 17899+
4a4d8108
AM
17900+/* it may be called at remount time, too */
17901+void au_update_ibrange(struct inode *inode, int do_put_zero)
17902+{
17903+ struct au_iinfo *iinfo;
5afbbe0d 17904+ aufs_bindex_t bindex, bbot;
1facf9fc 17905+
5afbbe0d 17906+ AuDebugOn(au_is_bad_inode(inode));
4a4d8108 17907+ IiMustWriteLock(inode);
1facf9fc 17908+
5afbbe0d
AM
17909+ iinfo = au_ii(inode);
17910+ if (do_put_zero && iinfo->ii_btop >= 0) {
17911+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
4a4d8108
AM
17912+ bindex++) {
17913+ struct inode *h_i;
1facf9fc 17914+
5afbbe0d 17915+ h_i = au_hinode(iinfo, bindex)->hi_inode;
38d290e6
JR
17916+ if (h_i
17917+ && !h_i->i_nlink
17918+ && !(h_i->i_state & I_LINKABLE))
027c5e7a
AM
17919+ au_set_h_iptr(inode, bindex, NULL, 0);
17920+ }
4a4d8108
AM
17921+ }
17922+
5afbbe0d
AM
17923+ iinfo->ii_btop = -1;
17924+ iinfo->ii_bbot = -1;
17925+ bbot = au_sbbot(inode->i_sb);
17926+ for (bindex = 0; bindex <= bbot; bindex++)
17927+ if (au_hinode(iinfo, bindex)->hi_inode) {
17928+ iinfo->ii_btop = bindex;
4a4d8108 17929+ break;
027c5e7a 17930+ }
5afbbe0d
AM
17931+ if (iinfo->ii_btop >= 0)
17932+ for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
17933+ if (au_hinode(iinfo, bindex)->hi_inode) {
17934+ iinfo->ii_bbot = bindex;
027c5e7a
AM
17935+ break;
17936+ }
5afbbe0d 17937+ AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
1308ab2a 17938+}
1facf9fc 17939+
dece6358 17940+/* ---------------------------------------------------------------------- */
1facf9fc 17941+
4a4d8108 17942+void au_icntnr_init_once(void *_c)
dece6358 17943+{
4a4d8108
AM
17944+ struct au_icntnr *c = _c;
17945+ struct au_iinfo *iinfo = &c->iinfo;
1facf9fc 17946+
be52b249 17947+ spin_lock_init(&iinfo->ii_generation.ig_spin);
4a4d8108
AM
17948+ au_rw_init(&iinfo->ii_rwsem);
17949+ inode_init_once(&c->vfs_inode);
17950+}
1facf9fc 17951+
5afbbe0d
AM
17952+void au_hinode_init(struct au_hinode *hinode)
17953+{
17954+ hinode->hi_inode = NULL;
17955+ hinode->hi_id = -1;
17956+ au_hn_init(hinode);
17957+ hinode->hi_whdentry = NULL;
17958+}
17959+
4a4d8108
AM
17960+int au_iinfo_init(struct inode *inode)
17961+{
17962+ struct au_iinfo *iinfo;
17963+ struct super_block *sb;
5afbbe0d 17964+ struct au_hinode *hi;
4a4d8108 17965+ int nbr, i;
1facf9fc 17966+
4a4d8108
AM
17967+ sb = inode->i_sb;
17968+ iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
5afbbe0d 17969+ nbr = au_sbbot(sb) + 1;
4a4d8108
AM
17970+ if (unlikely(nbr <= 0))
17971+ nbr = 1;
5afbbe0d
AM
17972+ hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
17973+ if (hi) {
acd2b654 17974+ au_lcnt_inc(&au_sbi(sb)->si_ninodes);
5afbbe0d
AM
17975+
17976+ iinfo->ii_hinode = hi;
17977+ for (i = 0; i < nbr; i++, hi++)
17978+ au_hinode_init(hi);
1facf9fc 17979+
537831f9 17980+ iinfo->ii_generation.ig_generation = au_sigen(sb);
5afbbe0d
AM
17981+ iinfo->ii_btop = -1;
17982+ iinfo->ii_bbot = -1;
4a4d8108
AM
17983+ iinfo->ii_vdir = NULL;
17984+ return 0;
1308ab2a 17985+ }
4a4d8108
AM
17986+ return -ENOMEM;
17987+}
1facf9fc 17988+
e2f27e51 17989+int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
4a4d8108 17990+{
5afbbe0d 17991+ int err, i;
4a4d8108 17992+ struct au_hinode *hip;
1facf9fc 17993+
4a4d8108
AM
17994+ AuRwMustWriteLock(&iinfo->ii_rwsem);
17995+
17996+ err = -ENOMEM;
e2f27e51
AM
17997+ hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
17998+ may_shrink);
4a4d8108
AM
17999+ if (hip) {
18000+ iinfo->ii_hinode = hip;
5afbbe0d
AM
18001+ i = iinfo->ii_bbot + 1;
18002+ hip += i;
18003+ for (; i < nbr; i++, hip++)
18004+ au_hinode_init(hip);
4a4d8108 18005+ err = 0;
1308ab2a 18006+ }
4a4d8108 18007+
1308ab2a 18008+ return err;
1facf9fc 18009+}
18010+
4a4d8108 18011+void au_iinfo_fin(struct inode *inode)
1facf9fc 18012+{
4a4d8108
AM
18013+ struct au_iinfo *iinfo;
18014+ struct au_hinode *hi;
18015+ struct super_block *sb;
5afbbe0d 18016+ aufs_bindex_t bindex, bbot;
b752ccd1 18017+ const unsigned char unlinked = !inode->i_nlink;
1308ab2a 18018+
5afbbe0d 18019+ AuDebugOn(au_is_bad_inode(inode));
1308ab2a 18020+
b752ccd1 18021+ sb = inode->i_sb;
acd2b654 18022+ au_lcnt_dec(&au_sbi(sb)->si_ninodes);
b752ccd1
AM
18023+ if (si_pid_test(sb))
18024+ au_xino_delete_inode(inode, unlinked);
18025+ else {
18026+ /*
18027+ * it is safe to hide the dependency between sbinfo and
18028+ * sb->s_umount.
18029+ */
18030+ lockdep_off();
18031+ si_noflush_read_lock(sb);
18032+ au_xino_delete_inode(inode, unlinked);
18033+ si_read_unlock(sb);
18034+ lockdep_on();
18035+ }
18036+
5afbbe0d 18037+ iinfo = au_ii(inode);
4a4d8108 18038+ if (iinfo->ii_vdir)
1c60b727 18039+ au_vdir_free(iinfo->ii_vdir);
1308ab2a 18040+
5afbbe0d 18041+ bindex = iinfo->ii_btop;
b752ccd1 18042+ if (bindex >= 0) {
5afbbe0d
AM
18043+ hi = au_hinode(iinfo, bindex);
18044+ bbot = iinfo->ii_bbot;
18045+ while (bindex++ <= bbot) {
b752ccd1 18046+ if (hi->hi_inode)
4a4d8108 18047+ au_hiput(hi);
4a4d8108
AM
18048+ hi++;
18049+ }
18050+ }
9f237c51 18051+ au_kfree_rcu(iinfo->ii_hinode);
4a4d8108 18052+ AuRwDestroy(&iinfo->ii_rwsem);
dece6358 18053+}
7f207e10 18054diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
eca34b5c 18055--- /usr/share/empty/fs/aufs/inode.c 1970-01-01 01:00:00.000000000 +0100
46016270 18056+++ linux/fs/aufs/inode.c 2021-06-30 21:35:11.397206648 +0200
0b2a12c6 18057@@ -0,0 +1,531 @@
cd7a4cd9 18058+// SPDX-License-Identifier: GPL-2.0
4a4d8108 18059+/*
d58c55f2 18060+ * Copyright (C) 2005-2020 Junjiro R. Okajima
4a4d8108
AM
18061+ *
18062+ * This program, aufs is free software; you can redistribute it and/or modify
18063+ * it under the terms of the GNU General Public License as published by
18064+ * the Free Software Foundation; either version 2 of the License, or
18065+ * (at your option) any later version.
18066+ *
18067+ * This program is distributed in the hope that it will be useful,
18068+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18069+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18070+ * GNU General Public License for more details.
18071+ *
18072+ * You should have received a copy of the GNU General Public License
523b37e3 18073+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 18074+ */
1facf9fc 18075+
4a4d8108
AM
18076+/*
18077+ * inode functions
18078+ */
1facf9fc 18079+
eca801bf 18080+#include <linux/iversion.h>
4a4d8108 18081+#include "aufs.h"
1308ab2a 18082+
4a4d8108
AM
18083+struct inode *au_igrab(struct inode *inode)
18084+{
18085+ if (inode) {
18086+ AuDebugOn(!atomic_read(&inode->i_count));
027c5e7a 18087+ ihold(inode);
1facf9fc 18088+ }
4a4d8108
AM
18089+ return inode;
18090+}
1facf9fc 18091+
4a4d8108
AM
18092+static void au_refresh_hinode_attr(struct inode *inode, int do_version)
18093+{
18094+ au_cpup_attr_all(inode, /*force*/0);
537831f9 18095+ au_update_iigen(inode, /*half*/1);
4a4d8108 18096+ if (do_version)
be118d29 18097+ inode_inc_iversion(inode);
dece6358 18098+}
1facf9fc 18099+
027c5e7a 18100+static int au_ii_refresh(struct inode *inode, int *update)
dece6358 18101+{
e2f27e51 18102+ int err, e, nbr;
027c5e7a 18103+ umode_t type;
4a4d8108 18104+ aufs_bindex_t bindex, new_bindex;
1308ab2a 18105+ struct super_block *sb;
4a4d8108 18106+ struct au_iinfo *iinfo;
027c5e7a 18107+ struct au_hinode *p, *q, tmp;
1facf9fc 18108+
5afbbe0d 18109+ AuDebugOn(au_is_bad_inode(inode));
4a4d8108 18110+ IiMustWriteLock(inode);
1facf9fc 18111+
027c5e7a 18112+ *update = 0;
4a4d8108 18113+ sb = inode->i_sb;
e2f27e51 18114+ nbr = au_sbbot(sb) + 1;
027c5e7a 18115+ type = inode->i_mode & S_IFMT;
4a4d8108 18116+ iinfo = au_ii(inode);
e2f27e51 18117+ err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
4a4d8108 18118+ if (unlikely(err))
1308ab2a 18119+ goto out;
1facf9fc 18120+
5afbbe0d
AM
18121+ AuDebugOn(iinfo->ii_btop < 0);
18122+ p = au_hinode(iinfo, iinfo->ii_btop);
18123+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
4a4d8108
AM
18124+ bindex++, p++) {
18125+ if (!p->hi_inode)
18126+ continue;
1facf9fc 18127+
027c5e7a 18128+ AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
4a4d8108
AM
18129+ new_bindex = au_br_index(sb, p->hi_id);
18130+ if (new_bindex == bindex)
18131+ continue;
1facf9fc 18132+
4a4d8108 18133+ if (new_bindex < 0) {
027c5e7a 18134+ *update = 1;
4a4d8108
AM
18135+ au_hiput(p);
18136+ p->hi_inode = NULL;
18137+ continue;
1308ab2a 18138+ }
4a4d8108 18139+
5afbbe0d
AM
18140+ if (new_bindex < iinfo->ii_btop)
18141+ iinfo->ii_btop = new_bindex;
18142+ if (iinfo->ii_bbot < new_bindex)
18143+ iinfo->ii_bbot = new_bindex;
4a4d8108 18144+ /* swap two lower inode, and loop again */
5afbbe0d 18145+ q = au_hinode(iinfo, new_bindex);
4a4d8108
AM
18146+ tmp = *q;
18147+ *q = *p;
18148+ *p = tmp;
18149+ if (tmp.hi_inode) {
18150+ bindex--;
18151+ p--;
1308ab2a 18152+ }
18153+ }
4a4d8108 18154+ au_update_ibrange(inode, /*do_put_zero*/0);
e2f27e51 18155+ au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
4a4d8108
AM
18156+ e = au_dy_irefresh(inode);
18157+ if (unlikely(e && !err))
18158+ err = e;
1facf9fc 18159+
4f0767ce 18160+out:
027c5e7a
AM
18161+ AuTraceErr(err);
18162+ return err;
18163+}
18164+
b95c5147
AM
18165+void au_refresh_iop(struct inode *inode, int force_getattr)
18166+{
18167+ int type;
18168+ struct au_sbinfo *sbi = au_sbi(inode->i_sb);
18169+ const struct inode_operations *iop
18170+ = force_getattr ? aufs_iop : sbi->si_iop_array;
18171+
18172+ if (inode->i_op == iop)
18173+ return;
18174+
18175+ switch (inode->i_mode & S_IFMT) {
18176+ case S_IFDIR:
18177+ type = AuIop_DIR;
18178+ break;
18179+ case S_IFLNK:
18180+ type = AuIop_SYMLINK;
18181+ break;
18182+ default:
18183+ type = AuIop_OTHER;
18184+ break;
18185+ }
18186+
18187+ inode->i_op = iop + type;
18188+ /* unnecessary smp_wmb() */
18189+}
18190+
027c5e7a
AM
18191+int au_refresh_hinode_self(struct inode *inode)
18192+{
18193+ int err, update;
18194+
18195+ err = au_ii_refresh(inode, &update);
18196+ if (!err)
18197+ au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
18198+
18199+ AuTraceErr(err);
4a4d8108
AM
18200+ return err;
18201+}
1facf9fc 18202+
4a4d8108
AM
18203+int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
18204+{
027c5e7a 18205+ int err, e, update;
4a4d8108 18206+ unsigned int flags;
027c5e7a 18207+ umode_t mode;
5afbbe0d 18208+ aufs_bindex_t bindex, bbot;
027c5e7a 18209+ unsigned char isdir;
4a4d8108
AM
18210+ struct au_hinode *p;
18211+ struct au_iinfo *iinfo;
1facf9fc 18212+
027c5e7a 18213+ err = au_ii_refresh(inode, &update);
4a4d8108
AM
18214+ if (unlikely(err))
18215+ goto out;
18216+
18217+ update = 0;
18218+ iinfo = au_ii(inode);
5afbbe0d 18219+ p = au_hinode(iinfo, iinfo->ii_btop);
027c5e7a
AM
18220+ mode = (inode->i_mode & S_IFMT);
18221+ isdir = S_ISDIR(mode);
4a4d8108 18222+ flags = au_hi_flags(inode, isdir);
5afbbe0d
AM
18223+ bbot = au_dbbot(dentry);
18224+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
5527c038 18225+ struct inode *h_i, *h_inode;
4a4d8108
AM
18226+ struct dentry *h_d;
18227+
18228+ h_d = au_h_dptr(dentry, bindex);
5527c038 18229+ if (!h_d || d_is_negative(h_d))
4a4d8108
AM
18230+ continue;
18231+
5527c038
JR
18232+ h_inode = d_inode(h_d);
18233+ AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
5afbbe0d 18234+ if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
4a4d8108
AM
18235+ h_i = au_h_iptr(inode, bindex);
18236+ if (h_i) {
5527c038 18237+ if (h_i == h_inode)
4a4d8108
AM
18238+ continue;
18239+ err = -EIO;
18240+ break;
18241+ }
18242+ }
5afbbe0d
AM
18243+ if (bindex < iinfo->ii_btop)
18244+ iinfo->ii_btop = bindex;
18245+ if (iinfo->ii_bbot < bindex)
18246+ iinfo->ii_bbot = bindex;
5527c038 18247+ au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
4a4d8108 18248+ update = 1;
1308ab2a 18249+ }
4a4d8108
AM
18250+ au_update_ibrange(inode, /*do_put_zero*/0);
18251+ e = au_dy_irefresh(inode);
18252+ if (unlikely(e && !err))
18253+ err = e;
027c5e7a
AM
18254+ if (!err)
18255+ au_refresh_hinode_attr(inode, update && isdir);
4a4d8108 18256+
4f0767ce 18257+out:
4a4d8108 18258+ AuTraceErr(err);
1308ab2a 18259+ return err;
dece6358
AM
18260+}
18261+
4a4d8108 18262+static int set_inode(struct inode *inode, struct dentry *dentry)
dece6358 18263+{
4a4d8108
AM
18264+ int err;
18265+ unsigned int flags;
18266+ umode_t mode;
5afbbe0d 18267+ aufs_bindex_t bindex, btop, btail;
4a4d8108
AM
18268+ unsigned char isdir;
18269+ struct dentry *h_dentry;
18270+ struct inode *h_inode;
18271+ struct au_iinfo *iinfo;
43982f53 18272+ const struct inode_operations *iop;
dece6358 18273+
4a4d8108 18274+ IiMustWriteLock(inode);
dece6358 18275+
4a4d8108
AM
18276+ err = 0;
18277+ isdir = 0;
b95c5147 18278+ iop = au_sbi(inode->i_sb)->si_iop_array;
5afbbe0d
AM
18279+ btop = au_dbtop(dentry);
18280+ h_dentry = au_h_dptr(dentry, btop);
5527c038 18281+ h_inode = d_inode(h_dentry);
4a4d8108
AM
18282+ mode = h_inode->i_mode;
18283+ switch (mode & S_IFMT) {
18284+ case S_IFREG:
18285+ btail = au_dbtail(dentry);
b95c5147 18286+ inode->i_op = iop + AuIop_OTHER;
4a4d8108 18287+ inode->i_fop = &aufs_file_fop;
5afbbe0d 18288+ err = au_dy_iaop(inode, btop, h_inode);
4a4d8108
AM
18289+ if (unlikely(err))
18290+ goto out;
18291+ break;
18292+ case S_IFDIR:
18293+ isdir = 1;
18294+ btail = au_dbtaildir(dentry);
b95c5147 18295+ inode->i_op = iop + AuIop_DIR;
4a4d8108
AM
18296+ inode->i_fop = &aufs_dir_fop;
18297+ break;
18298+ case S_IFLNK:
18299+ btail = au_dbtail(dentry);
b95c5147 18300+ inode->i_op = iop + AuIop_SYMLINK;
4a4d8108
AM
18301+ break;
18302+ case S_IFBLK:
18303+ case S_IFCHR:
18304+ case S_IFIFO:
18305+ case S_IFSOCK:
18306+ btail = au_dbtail(dentry);
b95c5147 18307+ inode->i_op = iop + AuIop_OTHER;
38d290e6 18308+ init_special_inode(inode, mode, h_inode->i_rdev);
4a4d8108
AM
18309+ break;
18310+ default:
18311+ AuIOErr("Unknown file type 0%o\n", mode);
18312+ err = -EIO;
1308ab2a 18313+ goto out;
4a4d8108 18314+ }
dece6358 18315+
4a4d8108
AM
18316+ /* do not set hnotify for whiteouted dirs (SHWH mode) */
18317+ flags = au_hi_flags(inode, isdir);
18318+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
18319+ && au_ftest_hi(flags, HNOTIFY)
18320+ && dentry->d_name.len > AUFS_WH_PFX_LEN
18321+ && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
18322+ au_fclr_hi(flags, HNOTIFY);
18323+ iinfo = au_ii(inode);
5afbbe0d
AM
18324+ iinfo->ii_btop = btop;
18325+ iinfo->ii_bbot = btail;
18326+ for (bindex = btop; bindex <= btail; bindex++) {
4a4d8108
AM
18327+ h_dentry = au_h_dptr(dentry, bindex);
18328+ if (h_dentry)
18329+ au_set_h_iptr(inode, bindex,
5527c038 18330+ au_igrab(d_inode(h_dentry)), flags);
4a4d8108
AM
18331+ }
18332+ au_cpup_attr_all(inode, /*force*/1);
c1595e42
JR
18333+ /*
18334+ * to force calling aufs_get_acl() every time,
18335+ * do not call cache_no_acl() for aufs inode.
18336+ */
dece6358 18337+
4f0767ce 18338+out:
4a4d8108
AM
18339+ return err;
18340+}
dece6358 18341+
027c5e7a
AM
18342+/*
18343+ * successful returns with iinfo write_locked
18344+ * minus: errno
18345+ * zero: success, matched
18346+ * plus: no error, but unmatched
18347+ */
18348+static int reval_inode(struct inode *inode, struct dentry *dentry)
4a4d8108
AM
18349+{
18350+ int err;
cfc41e69 18351+ unsigned int gen, igflags;
5afbbe0d 18352+ aufs_bindex_t bindex, bbot;
4a4d8108 18353+ struct inode *h_inode, *h_dinode;
5527c038 18354+ struct dentry *h_dentry;
dece6358 18355+
4a4d8108
AM
18356+ /*
18357+ * before this function, if aufs got any iinfo lock, it must be only
18358+ * one, the parent dir.
18359+ * it can happen by UDBA and the obsoleted inode number.
18360+ */
18361+ err = -EIO;
18362+ if (unlikely(inode->i_ino == parent_ino(dentry)))
18363+ goto out;
18364+
027c5e7a 18365+ err = 1;
4a4d8108 18366+ ii_write_lock_new_child(inode);
5afbbe0d 18367+ h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
5527c038 18368+ h_dinode = d_inode(h_dentry);
5afbbe0d
AM
18369+ bbot = au_ibbot(inode);
18370+ for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
4a4d8108 18371+ h_inode = au_h_iptr(inode, bindex);
537831f9
AM
18372+ if (!h_inode || h_inode != h_dinode)
18373+ continue;
18374+
18375+ err = 0;
cfc41e69 18376+ gen = au_iigen(inode, &igflags);
537831f9 18377+ if (gen == au_digen(dentry)
cfc41e69 18378+ && !au_ig_ftest(igflags, HALF_REFRESHED))
4a4d8108 18379+ break;
537831f9
AM
18380+
18381+ /* fully refresh inode using dentry */
18382+ err = au_refresh_hinode(inode, dentry);
18383+ if (!err)
18384+ au_update_iigen(inode, /*half*/0);
18385+ break;
1facf9fc 18386+ }
dece6358 18387+
4a4d8108
AM
18388+ if (unlikely(err))
18389+ ii_write_unlock(inode);
4f0767ce 18390+out:
1facf9fc 18391+ return err;
18392+}
1facf9fc 18393+
4a4d8108
AM
18394+int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18395+ unsigned int d_type, ino_t *ino)
1facf9fc 18396+{
521ced18
JR
18397+ int err, idx;
18398+ const int isnondir = d_type != DT_DIR;
1facf9fc 18399+
b752ccd1 18400+ /* prevent hardlinked inode number from race condition */
521ced18
JR
18401+ if (isnondir) {
18402+ err = au_xinondir_enter(sb, bindex, h_ino, &idx);
18403+ if (unlikely(err))
18404+ goto out;
4a4d8108 18405+ }
521ced18 18406+
4a4d8108
AM
18407+ err = au_xino_read(sb, bindex, h_ino, ino);
18408+ if (unlikely(err))
521ced18 18409+ goto out_xinondir;
1308ab2a 18410+
4a4d8108
AM
18411+ if (!*ino) {
18412+ err = -EIO;
18413+ *ino = au_xino_new_ino(sb);
18414+ if (unlikely(!*ino))
521ced18 18415+ goto out_xinondir;
4a4d8108
AM
18416+ err = au_xino_write(sb, bindex, h_ino, *ino);
18417+ if (unlikely(err))
521ced18 18418+ goto out_xinondir;
1308ab2a 18419+ }
1facf9fc 18420+
521ced18
JR
18421+out_xinondir:
18422+ if (isnondir && idx >= 0)
18423+ au_xinondir_leave(sb, bindex, h_ino, idx);
4f0767ce 18424+out:
1facf9fc 18425+ return err;
18426+}
18427+
4a4d8108
AM
18428+/* successful returns with iinfo write_locked */
18429+/* todo: return with unlocked? */
18430+struct inode *au_new_inode(struct dentry *dentry, int must_new)
1facf9fc 18431+{
5527c038 18432+ struct inode *inode, *h_inode;
4a4d8108
AM
18433+ struct dentry *h_dentry;
18434+ struct super_block *sb;
18435+ ino_t h_ino, ino;
521ced18 18436+ int err, idx, hlinked;
5afbbe0d 18437+ aufs_bindex_t btop;
1facf9fc 18438+
4a4d8108 18439+ sb = dentry->d_sb;
5afbbe0d
AM
18440+ btop = au_dbtop(dentry);
18441+ h_dentry = au_h_dptr(dentry, btop);
5527c038
JR
18442+ h_inode = d_inode(h_dentry);
18443+ h_ino = h_inode->i_ino;
521ced18 18444+ hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
b752ccd1 18445+
521ced18 18446+new_ino:
b752ccd1
AM
18447+ /*
18448+ * stop 'race'-ing between hardlinks under different
18449+ * parents.
18450+ */
521ced18
JR
18451+ if (hlinked) {
18452+ err = au_xinondir_enter(sb, btop, h_ino, &idx);
18453+ inode = ERR_PTR(err);
18454+ if (unlikely(err))
18455+ goto out;
18456+ }
b752ccd1 18457+
5afbbe0d 18458+ err = au_xino_read(sb, btop, h_ino, &ino);
4a4d8108
AM
18459+ inode = ERR_PTR(err);
18460+ if (unlikely(err))
521ced18 18461+ goto out_xinondir;
b752ccd1 18462+
4a4d8108
AM
18463+ if (!ino) {
18464+ ino = au_xino_new_ino(sb);
18465+ if (unlikely(!ino)) {
18466+ inode = ERR_PTR(-EIO);
521ced18 18467+ goto out_xinondir;
dece6358
AM
18468+ }
18469+ }
1facf9fc 18470+
4a4d8108
AM
18471+ AuDbg("i%lu\n", (unsigned long)ino);
18472+ inode = au_iget_locked(sb, ino);
18473+ err = PTR_ERR(inode);
18474+ if (IS_ERR(inode))
521ced18 18475+ goto out_xinondir;
1facf9fc 18476+
4a4d8108
AM
18477+ AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
18478+ if (inode->i_state & I_NEW) {
18479+ ii_write_lock_new_child(inode);
18480+ err = set_inode(inode, dentry);
18481+ if (!err) {
18482+ unlock_new_inode(inode);
521ced18 18483+ goto out_xinondir; /* success */
4a4d8108 18484+ }
1308ab2a 18485+
027c5e7a
AM
18486+ /*
18487+ * iget_failed() calls iput(), but we need to call
18488+ * ii_write_unlock() after iget_failed(). so dirty hack for
18489+ * i_count.
18490+ */
18491+ atomic_inc(&inode->i_count);
4a4d8108 18492+ iget_failed(inode);
027c5e7a 18493+ ii_write_unlock(inode);
5afbbe0d 18494+ au_xino_write(sb, btop, h_ino, /*ino*/0);
027c5e7a
AM
18495+ /* ignore this error */
18496+ goto out_iput;
18497+ } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
b752ccd1
AM
18498+ /*
18499+ * horrible race condition between lookup, readdir and copyup
18500+ * (or something).
18501+ */
521ced18
JR
18502+ if (hlinked && idx >= 0)
18503+ au_xinondir_leave(sb, btop, h_ino, idx);
027c5e7a
AM
18504+ err = reval_inode(inode, dentry);
18505+ if (unlikely(err < 0)) {
521ced18 18506+ hlinked = 0;
027c5e7a
AM
18507+ goto out_iput;
18508+ }
521ced18 18509+ if (!err)
4a4d8108 18510+ goto out; /* success */
521ced18
JR
18511+ else if (hlinked && idx >= 0) {
18512+ err = au_xinondir_enter(sb, btop, h_ino, &idx);
18513+ if (unlikely(err)) {
18514+ iput(inode);
18515+ inode = ERR_PTR(err);
18516+ goto out;
18517+ }
18518+ }
4a4d8108
AM
18519+ }
18520+
5527c038 18521+ if (unlikely(au_test_fs_unique_ino(h_inode)))
4a4d8108 18522+ AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
523b37e3 18523+ " b%d, %s, %pd, hi%lu, i%lu.\n",
5afbbe0d 18524+ btop, au_sbtype(h_dentry->d_sb), dentry,
4a4d8108
AM
18525+ (unsigned long)h_ino, (unsigned long)ino);
18526+ ino = 0;
5afbbe0d 18527+ err = au_xino_write(sb, btop, h_ino, /*ino*/0);
4a4d8108
AM
18528+ if (!err) {
18529+ iput(inode);
521ced18
JR
18530+ if (hlinked && idx >= 0)
18531+ au_xinondir_leave(sb, btop, h_ino, idx);
4a4d8108
AM
18532+ goto new_ino;
18533+ }
1308ab2a 18534+
4f0767ce 18535+out_iput:
4a4d8108 18536+ iput(inode);
4a4d8108 18537+ inode = ERR_PTR(err);
521ced18
JR
18538+out_xinondir:
18539+ if (hlinked && idx >= 0)
18540+ au_xinondir_leave(sb, btop, h_ino, idx);
4f0767ce 18541+out:
4a4d8108 18542+ return inode;
1facf9fc 18543+}
18544+
4a4d8108 18545+/* ---------------------------------------------------------------------- */
1facf9fc 18546+
4a4d8108
AM
18547+int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18548+ struct inode *inode)
18549+{
18550+ int err;
076b876e 18551+ struct inode *hi;
1facf9fc 18552+
4a4d8108 18553+ err = au_br_rdonly(au_sbr(sb, bindex));
1facf9fc 18554+
4a4d8108
AM
18555+ /* pseudo-link after flushed may happen out of bounds */
18556+ if (!err
18557+ && inode
5afbbe0d
AM
18558+ && au_ibtop(inode) <= bindex
18559+ && bindex <= au_ibbot(inode)) {
4a4d8108
AM
18560+ /*
18561+ * permission check is unnecessary since vfsub routine
18562+ * will be called later
18563+ */
076b876e 18564+ hi = au_h_iptr(inode, bindex);
4a4d8108
AM
18565+ if (hi)
18566+ err = IS_IMMUTABLE(hi) ? -EROFS : 0;
1facf9fc 18567+ }
18568+
4a4d8108
AM
18569+ return err;
18570+}
dece6358 18571+
0b2a12c6
JR
18572+int au_test_h_perm(struct user_namespace *h_userns, struct inode *h_inode,
18573+ int mask)
4a4d8108 18574+{
2dfbb274 18575+ if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
4a4d8108 18576+ return 0;
0b2a12c6 18577+ return inode_permission(h_userns, h_inode, mask);
4a4d8108 18578+}
1facf9fc 18579+
0b2a12c6
JR
18580+int au_test_h_perm_sio(struct user_namespace *h_userns, struct inode *h_inode,
18581+ int mask)
4a4d8108
AM
18582+{
18583+ if (au_test_nfs(h_inode->i_sb)
18584+ && (mask & MAY_WRITE)
18585+ && S_ISDIR(h_inode->i_mode))
18586+ mask |= MAY_READ; /* force permission check */
0b2a12c6 18587+ return au_test_h_perm(h_userns, h_inode, mask);
1facf9fc 18588+}
7f207e10 18589diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
eca34b5c 18590--- /usr/share/empty/fs/aufs/inode.h 1970-01-01 01:00:00.000000000 +0100
46016270 18591+++ linux/fs/aufs/inode.h 2021-06-30 21:35:11.397206648 +0200
0b2a12c6 18592@@ -0,0 +1,705 @@
062440b3 18593+/* SPDX-License-Identifier: GPL-2.0 */
4a4d8108 18594+/*
d58c55f2 18595+ * Copyright (C) 2005-2020 Junjiro R. Okajima
4a4d8108
AM
18596+ *
18597+ * This program, aufs is free software; you can redistribute it and/or modify
18598+ * it under the terms of the GNU General Public License as published by
18599+ * the Free Software Foundation; either version 2 of the License, or
18600+ * (at your option) any later version.
18601+ *
18602+ * This program is distributed in the hope that it will be useful,
18603+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18604+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18605+ * GNU General Public License for more details.
18606+ *
18607+ * You should have received a copy of the GNU General Public License
523b37e3 18608+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 18609+ */
1facf9fc 18610+
1308ab2a 18611+/*
4a4d8108 18612+ * inode operations
1308ab2a 18613+ */
dece6358 18614+
4a4d8108
AM
18615+#ifndef __AUFS_INODE_H__
18616+#define __AUFS_INODE_H__
dece6358 18617+
4a4d8108 18618+#ifdef __KERNEL__
1308ab2a 18619+
4a4d8108 18620+#include <linux/fsnotify.h>
4a4d8108 18621+#include "rwsem.h"
1308ab2a 18622+
4a4d8108 18623+struct vfsmount;
1facf9fc 18624+
4a4d8108
AM
18625+struct au_hnotify {
18626+#ifdef CONFIG_AUFS_HNOTIFY
18627+#ifdef CONFIG_AUFS_HFSNOTIFY
7f207e10 18628+ /* never use fsnotify_add_vfsmount_mark() */
0c5527e5 18629+ struct fsnotify_mark hn_mark;
4a4d8108 18630+#endif
1c60b727 18631+ struct inode *hn_aufs_inode; /* no get/put */
9f237c51 18632+ struct rcu_head rcu;
4a4d8108
AM
18633+#endif
18634+} ____cacheline_aligned_in_smp;
1facf9fc 18635+
4a4d8108
AM
18636+struct au_hinode {
18637+ struct inode *hi_inode;
18638+ aufs_bindex_t hi_id;
18639+#ifdef CONFIG_AUFS_HNOTIFY
18640+ struct au_hnotify *hi_notify;
18641+#endif
dece6358 18642+
4a4d8108
AM
18643+ /* reference to the copied-up whiteout with get/put */
18644+ struct dentry *hi_whdentry;
18645+};
dece6358 18646+
537831f9
AM
18647+/* ig_flags */
18648+#define AuIG_HALF_REFRESHED 1
18649+#define au_ig_ftest(flags, name) ((flags) & AuIG_##name)
18650+#define au_ig_fset(flags, name) \
18651+ do { (flags) |= AuIG_##name; } while (0)
18652+#define au_ig_fclr(flags, name) \
18653+ do { (flags) &= ~AuIG_##name; } while (0)
18654+
18655+struct au_iigen {
be52b249 18656+ spinlock_t ig_spin;
537831f9
AM
18657+ __u32 ig_generation, ig_flags;
18658+};
18659+
4a4d8108
AM
18660+struct au_vdir;
18661+struct au_iinfo {
7a9e40b8 18662+ struct au_iigen ii_generation;
4a4d8108 18663+ struct super_block *ii_hsb1; /* no get/put */
1facf9fc 18664+
4a4d8108 18665+ struct au_rwsem ii_rwsem;
5afbbe0d 18666+ aufs_bindex_t ii_btop, ii_bbot;
4a4d8108
AM
18667+ __u32 ii_higen;
18668+ struct au_hinode *ii_hinode;
18669+ struct au_vdir *ii_vdir;
18670+};
1facf9fc 18671+
4a4d8108 18672+struct au_icntnr {
9f237c51
AM
18673+ struct au_iinfo iinfo;
18674+ struct inode vfs_inode;
18675+ struct hlist_bl_node plink;
18676+ struct rcu_head rcu;
4a4d8108 18677+} ____cacheline_aligned_in_smp;
1308ab2a 18678+
4a4d8108
AM
18679+/* au_pin flags */
18680+#define AuPin_DI_LOCKED 1
18681+#define AuPin_MNT_WRITE (1 << 1)
18682+#define au_ftest_pin(flags, name) ((flags) & AuPin_##name)
7f207e10
AM
18683+#define au_fset_pin(flags, name) \
18684+ do { (flags) |= AuPin_##name; } while (0)
18685+#define au_fclr_pin(flags, name) \
18686+ do { (flags) &= ~AuPin_##name; } while (0)
4a4d8108
AM
18687+
18688+struct au_pin {
18689+ /* input */
18690+ struct dentry *dentry;
18691+ unsigned int udba;
18692+ unsigned char lsc_di, lsc_hi, flags;
18693+ aufs_bindex_t bindex;
18694+
18695+ /* output */
18696+ struct dentry *parent;
18697+ struct au_hinode *hdir;
18698+ struct vfsmount *h_mnt;
86dc4139
AM
18699+
18700+ /* temporary unlock/relock for copyup */
18701+ struct dentry *h_dentry, *h_parent;
18702+ struct au_branch *br;
18703+ struct task_struct *task;
4a4d8108 18704+};
1facf9fc 18705+
86dc4139 18706+void au_pin_hdir_unlock(struct au_pin *p);
c1595e42 18707+int au_pin_hdir_lock(struct au_pin *p);
86dc4139 18708+int au_pin_hdir_relock(struct au_pin *p);
86dc4139
AM
18709+void au_pin_hdir_acquire_nest(struct au_pin *p);
18710+void au_pin_hdir_release(struct au_pin *p);
18711+
1308ab2a 18712+/* ---------------------------------------------------------------------- */
18713+
4a4d8108 18714+static inline struct au_iinfo *au_ii(struct inode *inode)
1facf9fc 18715+{
5afbbe0d
AM
18716+ BUG_ON(is_bad_inode(inode));
18717+ return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
4a4d8108 18718+}
1facf9fc 18719+
4a4d8108 18720+/* ---------------------------------------------------------------------- */
1facf9fc 18721+
4a4d8108
AM
18722+/* inode.c */
18723+struct inode *au_igrab(struct inode *inode);
b95c5147 18724+void au_refresh_iop(struct inode *inode, int force_getattr);
027c5e7a 18725+int au_refresh_hinode_self(struct inode *inode);
4a4d8108
AM
18726+int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
18727+int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18728+ unsigned int d_type, ino_t *ino);
18729+struct inode *au_new_inode(struct dentry *dentry, int must_new);
18730+int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18731+ struct inode *inode);
0b2a12c6
JR
18732+int au_test_h_perm(struct user_namespace *h_userns, struct inode *h_inode,
18733+ int mask);
18734+int au_test_h_perm_sio(struct user_namespace *h_userns, struct inode *h_inode,
18735+ int mask);
1facf9fc 18736+
4a4d8108
AM
18737+static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
18738+ ino_t h_ino, unsigned int d_type, ino_t *ino)
18739+{
18740+#ifdef CONFIG_AUFS_SHWH
18741+ return au_ino(sb, bindex, h_ino, d_type, ino);
18742+#else
18743+ return 0;
18744+#endif
18745+}
1facf9fc 18746+
4a4d8108 18747+/* i_op.c */
b95c5147
AM
18748+enum {
18749+ AuIop_SYMLINK,
18750+ AuIop_DIR,
18751+ AuIop_OTHER,
18752+ AuIop_Last
18753+};
43982f53 18754+extern struct inode_operations aufs_iop[AuIop_Last], /* not const */
b95c5147 18755+ aufs_iop_nogetattr[AuIop_Last];
1308ab2a 18756+
4a4d8108
AM
18757+/* au_wr_dir flags */
18758+#define AuWrDir_ADD_ENTRY 1
7e9cd9fe
AM
18759+#define AuWrDir_ISDIR (1 << 1)
18760+#define AuWrDir_TMPFILE (1 << 2)
4a4d8108 18761+#define au_ftest_wrdir(flags, name) ((flags) & AuWrDir_##name)
7f207e10
AM
18762+#define au_fset_wrdir(flags, name) \
18763+ do { (flags) |= AuWrDir_##name; } while (0)
18764+#define au_fclr_wrdir(flags, name) \
18765+ do { (flags) &= ~AuWrDir_##name; } while (0)
1facf9fc 18766+
4a4d8108
AM
18767+struct au_wr_dir_args {
18768+ aufs_bindex_t force_btgt;
18769+ unsigned char flags;
18770+};
18771+int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
18772+ struct au_wr_dir_args *args);
dece6358 18773+
4a4d8108
AM
18774+struct dentry *au_pinned_h_parent(struct au_pin *pin);
18775+void au_pin_init(struct au_pin *pin, struct dentry *dentry,
18776+ aufs_bindex_t bindex, int lsc_di, int lsc_hi,
18777+ unsigned int udba, unsigned char flags);
18778+int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
18779+ unsigned int udba, unsigned char flags) __must_check;
18780+int au_do_pin(struct au_pin *pin) __must_check;
18781+void au_unpin(struct au_pin *pin);
c1595e42
JR
18782+int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
18783+
18784+#define AuIcpup_DID_CPUP 1
18785+#define au_ftest_icpup(flags, name) ((flags) & AuIcpup_##name)
18786+#define au_fset_icpup(flags, name) \
18787+ do { (flags) |= AuIcpup_##name; } while (0)
18788+#define au_fclr_icpup(flags, name) \
18789+ do { (flags) &= ~AuIcpup_##name; } while (0)
18790+
18791+struct au_icpup_args {
18792+ unsigned char flags;
18793+ unsigned char pin_flags;
18794+ aufs_bindex_t btgt;
18795+ unsigned int udba;
18796+ struct au_pin pin;
18797+ struct path h_path;
18798+ struct inode *h_inode;
18799+};
18800+
18801+int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
18802+ struct au_icpup_args *a);
18803+
42b5c33a
AM
18804+int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
18805+ struct path *h_path, int locked);
1facf9fc 18806+
4a4d8108
AM
18807+/* i_op_add.c */
18808+int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
18809+ struct dentry *h_parent, int isdir);
0b2a12c6
JR
18810+int aufs_mknod(struct user_namespace *userns, struct inode *dir,
18811+ struct dentry *dentry, umode_t mode, dev_t dev);
18812+int aufs_symlink(struct user_namespace *userns, struct inode *dir,
18813+ struct dentry *dentry, const char *symname);
18814+int aufs_create(struct user_namespace *userns, struct inode *dir,
18815+ struct dentry *dentry, umode_t mode, bool want_excl);
b912730e
AM
18816+struct vfsub_aopen_args;
18817+int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
18818+ struct vfsub_aopen_args *args);
0b2a12c6
JR
18819+int aufs_tmpfile(struct user_namespace *userns, struct inode *dir,
18820+ struct dentry *dentry, umode_t mode);
4a4d8108
AM
18821+int aufs_link(struct dentry *src_dentry, struct inode *dir,
18822+ struct dentry *dentry);
0b2a12c6
JR
18823+int aufs_mkdir(struct user_namespace *userns, struct inode *dir,
18824+ struct dentry *dentry, umode_t mode);
1facf9fc 18825+
4a4d8108
AM
18826+/* i_op_del.c */
18827+int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
18828+int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
18829+ struct dentry *h_parent, int isdir);
18830+int aufs_unlink(struct inode *dir, struct dentry *dentry);
18831+int aufs_rmdir(struct inode *dir, struct dentry *dentry);
1308ab2a 18832+
4a4d8108
AM
18833+/* i_op_ren.c */
18834+int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
0b2a12c6
JR
18835+int aufs_rename(struct user_namespace *userns,
18836+ struct inode *_src_dir, struct dentry *_src_dentry,
18837+ struct inode *_dst_dir, struct dentry *_dst_dentry,
18838+ unsigned int _flags);
1facf9fc 18839+
4a4d8108
AM
18840+/* iinfo.c */
18841+struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
18842+void au_hiput(struct au_hinode *hinode);
18843+void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
18844+ struct dentry *h_wh);
18845+unsigned int au_hi_flags(struct inode *inode, int isdir);
1308ab2a 18846+
4a4d8108
AM
18847+/* hinode flags */
18848+#define AuHi_XINO 1
18849+#define AuHi_HNOTIFY (1 << 1)
18850+#define au_ftest_hi(flags, name) ((flags) & AuHi_##name)
7f207e10
AM
18851+#define au_fset_hi(flags, name) \
18852+ do { (flags) |= AuHi_##name; } while (0)
18853+#define au_fclr_hi(flags, name) \
18854+ do { (flags) &= ~AuHi_##name; } while (0)
1facf9fc 18855+
4a4d8108
AM
18856+#ifndef CONFIG_AUFS_HNOTIFY
18857+#undef AuHi_HNOTIFY
18858+#define AuHi_HNOTIFY 0
18859+#endif
1facf9fc 18860+
4a4d8108
AM
18861+void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
18862+ struct inode *h_inode, unsigned int flags);
1facf9fc 18863+
537831f9 18864+void au_update_iigen(struct inode *inode, int half);
4a4d8108 18865+void au_update_ibrange(struct inode *inode, int do_put_zero);
1facf9fc 18866+
4a4d8108 18867+void au_icntnr_init_once(void *_c);
5afbbe0d 18868+void au_hinode_init(struct au_hinode *hinode);
4a4d8108
AM
18869+int au_iinfo_init(struct inode *inode);
18870+void au_iinfo_fin(struct inode *inode);
e2f27e51 18871+int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
1308ab2a 18872+
e49829fe 18873+#ifdef CONFIG_PROC_FS
4a4d8108 18874+/* plink.c */
e49829fe 18875+int au_plink_maint(struct super_block *sb, int flags);
7e9cd9fe 18876+struct au_sbinfo;
e49829fe
JR
18877+void au_plink_maint_leave(struct au_sbinfo *sbinfo);
18878+int au_plink_maint_enter(struct super_block *sb);
4a4d8108
AM
18879+#ifdef CONFIG_AUFS_DEBUG
18880+void au_plink_list(struct super_block *sb);
18881+#else
18882+AuStubVoid(au_plink_list, struct super_block *sb)
18883+#endif
18884+int au_plink_test(struct inode *inode);
18885+struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
18886+void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
18887+ struct dentry *h_dentry);
e49829fe
JR
18888+void au_plink_put(struct super_block *sb, int verbose);
18889+void au_plink_clean(struct super_block *sb, int verbose);
4a4d8108 18890+void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
e49829fe
JR
18891+#else
18892+AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
18893+AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
18894+AuStubInt0(au_plink_maint_enter, struct super_block *sb);
18895+AuStubVoid(au_plink_list, struct super_block *sb);
18896+AuStubInt0(au_plink_test, struct inode *inode);
18897+AuStub(struct dentry *, au_plink_lkup, return NULL,
18898+ struct inode *inode, aufs_bindex_t bindex);
18899+AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
18900+ struct dentry *h_dentry);
18901+AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
18902+AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
18903+AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
18904+#endif /* CONFIG_PROC_FS */
1facf9fc 18905+
c1595e42
JR
18906+#ifdef CONFIG_AUFS_XATTR
18907+/* xattr.c */
0b2a12c6 18908+int au_cpup_xattr(struct path *h_dst, struct path *h_src, int ignore_flags,
7e9cd9fe 18909+ unsigned int verbose);
c1595e42 18910+ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
f2c43d5f 18911+void au_xattr_init(struct super_block *sb);
c1595e42 18912+#else
0b2a12c6 18913+AuStubInt0(au_cpup_xattr, struct path *h_dst, struct path *h_src,
7e9cd9fe 18914+ int ignore_flags, unsigned int verbose);
f2c43d5f 18915+AuStubVoid(au_xattr_init, struct super_block *sb);
c1595e42
JR
18916+#endif
18917+
18918+#ifdef CONFIG_FS_POSIX_ACL
18919+struct posix_acl *aufs_get_acl(struct inode *inode, int type);
0b2a12c6
JR
18920+int aufs_set_acl(struct user_namespace *userns, struct inode *inode,
18921+ struct posix_acl *acl, int type);
c1595e42
JR
18922+#endif
18923+
18924+#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
18925+enum {
18926+ AU_XATTR_SET,
c1595e42
JR
18927+ AU_ACL_SET
18928+};
18929+
f2c43d5f 18930+struct au_sxattr {
c1595e42
JR
18931+ int type;
18932+ union {
18933+ struct {
18934+ const char *name;
18935+ const void *value;
18936+ size_t size;
18937+ int flags;
18938+ } set;
18939+ struct {
c1595e42
JR
18940+ struct posix_acl *acl;
18941+ int type;
18942+ } acl_set;
18943+ } u;
18944+};
f2c43d5f
AM
18945+ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
18946+ struct au_sxattr *arg);
c1595e42
JR
18947+#endif
18948+
4a4d8108 18949+/* ---------------------------------------------------------------------- */
1308ab2a 18950+
4a4d8108
AM
18951+/* lock subclass for iinfo */
18952+enum {
18953+ AuLsc_II_CHILD, /* child first */
18954+ AuLsc_II_CHILD2, /* rename(2), link(2), and cpup at hnotify */
18955+ AuLsc_II_CHILD3, /* copyup dirs */
18956+ AuLsc_II_PARENT, /* see AuLsc_I_PARENT in vfsub.h */
18957+ AuLsc_II_PARENT2,
18958+ AuLsc_II_PARENT3, /* copyup dirs */
18959+ AuLsc_II_NEW_CHILD
18960+};
1308ab2a 18961+
1facf9fc 18962+/*
4a4d8108
AM
18963+ * ii_read_lock_child, ii_write_lock_child,
18964+ * ii_read_lock_child2, ii_write_lock_child2,
18965+ * ii_read_lock_child3, ii_write_lock_child3,
18966+ * ii_read_lock_parent, ii_write_lock_parent,
18967+ * ii_read_lock_parent2, ii_write_lock_parent2,
18968+ * ii_read_lock_parent3, ii_write_lock_parent3,
18969+ * ii_read_lock_new_child, ii_write_lock_new_child,
1facf9fc 18970+ */
4a4d8108
AM
18971+#define AuReadLockFunc(name, lsc) \
18972+static inline void ii_read_lock_##name(struct inode *i) \
18973+{ \
18974+ au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
18975+}
18976+
18977+#define AuWriteLockFunc(name, lsc) \
18978+static inline void ii_write_lock_##name(struct inode *i) \
18979+{ \
18980+ au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
18981+}
18982+
18983+#define AuRWLockFuncs(name, lsc) \
18984+ AuReadLockFunc(name, lsc) \
18985+ AuWriteLockFunc(name, lsc)
18986+
18987+AuRWLockFuncs(child, CHILD);
18988+AuRWLockFuncs(child2, CHILD2);
18989+AuRWLockFuncs(child3, CHILD3);
18990+AuRWLockFuncs(parent, PARENT);
18991+AuRWLockFuncs(parent2, PARENT2);
18992+AuRWLockFuncs(parent3, PARENT3);
18993+AuRWLockFuncs(new_child, NEW_CHILD);
18994+
18995+#undef AuReadLockFunc
18996+#undef AuWriteLockFunc
18997+#undef AuRWLockFuncs
1facf9fc 18998+
8b6a4947
AM
18999+#define ii_read_unlock(i) au_rw_read_unlock(&au_ii(i)->ii_rwsem)
19000+#define ii_write_unlock(i) au_rw_write_unlock(&au_ii(i)->ii_rwsem)
19001+#define ii_downgrade_lock(i) au_rw_dgrade_lock(&au_ii(i)->ii_rwsem)
1facf9fc 19002+
4a4d8108
AM
19003+#define IiMustNoWaiters(i) AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
19004+#define IiMustAnyLock(i) AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
19005+#define IiMustWriteLock(i) AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
1facf9fc 19006+
4a4d8108 19007+/* ---------------------------------------------------------------------- */
1308ab2a 19008+
027c5e7a
AM
19009+static inline void au_icntnr_init(struct au_icntnr *c)
19010+{
19011+#ifdef CONFIG_AUFS_DEBUG
19012+ c->vfs_inode.i_mode = 0;
19013+#endif
19014+}
19015+
cfc41e69 19016+static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
4a4d8108 19017+{
537831f9
AM
19018+ unsigned int gen;
19019+ struct au_iinfo *iinfo;
be52b249 19020+ struct au_iigen *iigen;
537831f9
AM
19021+
19022+ iinfo = au_ii(inode);
be52b249
AM
19023+ iigen = &iinfo->ii_generation;
19024+ spin_lock(&iigen->ig_spin);
cfc41e69
AM
19025+ if (igflags)
19026+ *igflags = iigen->ig_flags;
be52b249
AM
19027+ gen = iigen->ig_generation;
19028+ spin_unlock(&iigen->ig_spin);
537831f9
AM
19029+
19030+ return gen;
4a4d8108 19031+}
1308ab2a 19032+
4a4d8108
AM
19033+/* tiny test for inode number */
19034+/* tmpfs generation is too rough */
19035+static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
19036+{
19037+ struct au_iinfo *iinfo;
1308ab2a 19038+
4a4d8108
AM
19039+ iinfo = au_ii(inode);
19040+ AuRwMustAnyLock(&iinfo->ii_rwsem);
19041+ return !(iinfo->ii_hsb1 == h_inode->i_sb
19042+ && iinfo->ii_higen == h_inode->i_generation);
19043+}
1308ab2a 19044+
4a4d8108
AM
19045+static inline void au_iigen_dec(struct inode *inode)
19046+{
537831f9 19047+ struct au_iinfo *iinfo;
be52b249 19048+ struct au_iigen *iigen;
537831f9
AM
19049+
19050+ iinfo = au_ii(inode);
be52b249
AM
19051+ iigen = &iinfo->ii_generation;
19052+ spin_lock(&iigen->ig_spin);
19053+ iigen->ig_generation--;
19054+ spin_unlock(&iigen->ig_spin);
027c5e7a
AM
19055+}
19056+
19057+static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
19058+{
19059+ int err;
19060+
19061+ err = 0;
537831f9 19062+ if (unlikely(inode && au_iigen(inode, NULL) != sigen))
027c5e7a
AM
19063+ err = -EIO;
19064+
19065+ return err;
4a4d8108 19066+}
1308ab2a 19067+
4a4d8108 19068+/* ---------------------------------------------------------------------- */
1308ab2a 19069+
5afbbe0d
AM
19070+static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
19071+ aufs_bindex_t bindex)
19072+{
19073+ return iinfo->ii_hinode + bindex;
19074+}
19075+
19076+static inline int au_is_bad_inode(struct inode *inode)
19077+{
19078+ return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
19079+}
19080+
4a4d8108
AM
19081+static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
19082+ aufs_bindex_t bindex)
19083+{
19084+ IiMustAnyLock(inode);
5afbbe0d 19085+ return au_hinode(au_ii(inode), bindex)->hi_id;
4a4d8108 19086+}
1308ab2a 19087+
5afbbe0d 19088+static inline aufs_bindex_t au_ibtop(struct inode *inode)
4a4d8108
AM
19089+{
19090+ IiMustAnyLock(inode);
5afbbe0d 19091+ return au_ii(inode)->ii_btop;
4a4d8108 19092+}
1308ab2a 19093+
5afbbe0d 19094+static inline aufs_bindex_t au_ibbot(struct inode *inode)
4a4d8108
AM
19095+{
19096+ IiMustAnyLock(inode);
5afbbe0d 19097+ return au_ii(inode)->ii_bbot;
4a4d8108 19098+}
1308ab2a 19099+
4a4d8108
AM
19100+static inline struct au_vdir *au_ivdir(struct inode *inode)
19101+{
19102+ IiMustAnyLock(inode);
19103+ return au_ii(inode)->ii_vdir;
19104+}
1308ab2a 19105+
4a4d8108
AM
19106+static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
19107+{
19108+ IiMustAnyLock(inode);
5afbbe0d 19109+ return au_hinode(au_ii(inode), bindex)->hi_whdentry;
4a4d8108 19110+}
1308ab2a 19111+
5afbbe0d 19112+static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 19113+{
4a4d8108 19114+ IiMustWriteLock(inode);
5afbbe0d 19115+ au_ii(inode)->ii_btop = bindex;
4a4d8108 19116+}
1308ab2a 19117+
5afbbe0d 19118+static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
4a4d8108
AM
19119+{
19120+ IiMustWriteLock(inode);
5afbbe0d 19121+ au_ii(inode)->ii_bbot = bindex;
1308ab2a 19122+}
19123+
4a4d8108
AM
19124+static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
19125+{
19126+ IiMustWriteLock(inode);
19127+ au_ii(inode)->ii_vdir = vdir;
19128+}
1facf9fc 19129+
4a4d8108 19130+static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 19131+{
4a4d8108 19132+ IiMustAnyLock(inode);
5afbbe0d 19133+ return au_hinode(au_ii(inode), bindex);
4a4d8108 19134+}
dece6358 19135+
4a4d8108 19136+/* ---------------------------------------------------------------------- */
1facf9fc 19137+
4a4d8108
AM
19138+static inline struct dentry *au_pinned_parent(struct au_pin *pin)
19139+{
19140+ if (pin)
19141+ return pin->parent;
19142+ return NULL;
1facf9fc 19143+}
19144+
4a4d8108 19145+static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
1facf9fc 19146+{
4a4d8108
AM
19147+ if (pin && pin->hdir)
19148+ return pin->hdir->hi_inode;
19149+ return NULL;
1308ab2a 19150+}
1facf9fc 19151+
4a4d8108
AM
19152+static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
19153+{
19154+ if (pin)
19155+ return pin->hdir;
19156+ return NULL;
19157+}
1facf9fc 19158+
4a4d8108 19159+static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
1308ab2a 19160+{
4a4d8108
AM
19161+ if (pin)
19162+ pin->dentry = dentry;
19163+}
1308ab2a 19164+
4a4d8108
AM
19165+static inline void au_pin_set_parent_lflag(struct au_pin *pin,
19166+ unsigned char lflag)
19167+{
19168+ if (pin) {
7f207e10 19169+ if (lflag)
4a4d8108 19170+ au_fset_pin(pin->flags, DI_LOCKED);
7f207e10 19171+ else
4a4d8108 19172+ au_fclr_pin(pin->flags, DI_LOCKED);
1308ab2a 19173+ }
4a4d8108
AM
19174+}
19175+
7e9cd9fe 19176+#if 0 /* reserved */
4a4d8108
AM
19177+static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
19178+{
19179+ if (pin) {
19180+ dput(pin->parent);
19181+ pin->parent = dget(parent);
1facf9fc 19182+ }
4a4d8108 19183+}
7e9cd9fe 19184+#endif
1facf9fc 19185+
4a4d8108
AM
19186+/* ---------------------------------------------------------------------- */
19187+
027c5e7a 19188+struct au_branch;
4a4d8108
AM
19189+#ifdef CONFIG_AUFS_HNOTIFY
19190+struct au_hnotify_op {
19191+ void (*ctl)(struct au_hinode *hinode, int do_set);
027c5e7a 19192+ int (*alloc)(struct au_hinode *hinode);
7eafdf33
AM
19193+
19194+ /*
42a736d3 19195+ * if it returns true, the caller should free hinode->hi_notify,
7eafdf33
AM
19196+ * otherwise ->free() frees it.
19197+ */
19198+ int (*free)(struct au_hinode *hinode,
19199+ struct au_hnotify *hn) __must_check;
4a4d8108
AM
19200+
19201+ void (*fin)(void);
19202+ int (*init)(void);
027c5e7a
AM
19203+
19204+ int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
19205+ void (*fin_br)(struct au_branch *br);
19206+ int (*init_br)(struct au_branch *br, int perm);
4a4d8108
AM
19207+};
19208+
19209+/* hnotify.c */
027c5e7a 19210+int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
4a4d8108
AM
19211+void au_hn_free(struct au_hinode *hinode);
19212+void au_hn_ctl(struct au_hinode *hinode, int do_set);
19213+void au_hn_reset(struct inode *inode, unsigned int flags);
19214+int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
fbc438ed 19215+ const struct qstr *h_child_qstr, struct inode *h_child_inode);
027c5e7a
AM
19216+int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
19217+int au_hnotify_init_br(struct au_branch *br, int perm);
19218+void au_hnotify_fin_br(struct au_branch *br);
4a4d8108
AM
19219+int __init au_hnotify_init(void);
19220+void au_hnotify_fin(void);
19221+
7f207e10 19222+/* hfsnotify.c */
4a4d8108
AM
19223+extern const struct au_hnotify_op au_hnotify_op;
19224+
19225+static inline
19226+void au_hn_init(struct au_hinode *hinode)
19227+{
19228+ hinode->hi_notify = NULL;
1308ab2a 19229+}
19230+
53392da6
AM
19231+static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
19232+{
19233+ return hinode->hi_notify;
19234+}
19235+
4a4d8108 19236+#else
c1595e42
JR
19237+AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
19238+ struct au_hinode *hinode __maybe_unused,
19239+ struct inode *inode __maybe_unused)
19240+AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
4a4d8108
AM
19241+AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
19242+AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
19243+ int do_set __maybe_unused)
19244+AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
19245+ unsigned int flags __maybe_unused)
027c5e7a
AM
19246+AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
19247+ struct au_branch *br __maybe_unused,
19248+ int perm __maybe_unused)
19249+AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
19250+ int perm __maybe_unused)
19251+AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
4a4d8108
AM
19252+AuStubInt0(__init au_hnotify_init, void)
19253+AuStubVoid(au_hnotify_fin, void)
19254+AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
19255+#endif /* CONFIG_AUFS_HNOTIFY */
19256+
19257+static inline void au_hn_suspend(struct au_hinode *hdir)
19258+{
19259+ au_hn_ctl(hdir, /*do_set*/0);
1308ab2a 19260+}
19261+
4a4d8108 19262+static inline void au_hn_resume(struct au_hinode *hdir)
1308ab2a 19263+{
4a4d8108
AM
19264+ au_hn_ctl(hdir, /*do_set*/1);
19265+}
1308ab2a 19266+
5afbbe0d 19267+static inline void au_hn_inode_lock(struct au_hinode *hdir)
4a4d8108 19268+{
febd17d6 19269+ inode_lock(hdir->hi_inode);
4a4d8108
AM
19270+ au_hn_suspend(hdir);
19271+}
dece6358 19272+
5afbbe0d 19273+static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
4a4d8108
AM
19274+ unsigned int sc __maybe_unused)
19275+{
febd17d6 19276+ inode_lock_nested(hdir->hi_inode, sc);
4a4d8108 19277+ au_hn_suspend(hdir);
1facf9fc 19278+}
1facf9fc 19279+
8b6a4947
AM
19280+#if 0 /* unused */
19281+#include "vfsub.h"
3c1bdaff
AM
19282+static inline void au_hn_inode_lock_shared_nested(struct au_hinode *hdir,
19283+ unsigned int sc)
19284+{
be118d29 19285+ inode_lock_shared_nested(hdir->hi_inode, sc);
3c1bdaff
AM
19286+ au_hn_suspend(hdir);
19287+}
8b6a4947 19288+#endif
3c1bdaff 19289+
5afbbe0d 19290+static inline void au_hn_inode_unlock(struct au_hinode *hdir)
4a4d8108
AM
19291+{
19292+ au_hn_resume(hdir);
febd17d6 19293+ inode_unlock(hdir->hi_inode);
4a4d8108
AM
19294+}
19295+
19296+#endif /* __KERNEL__ */
19297+#endif /* __AUFS_INODE_H__ */
7f207e10 19298diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
eca34b5c 19299--- /usr/share/empty/fs/aufs/ioctl.c 1970-01-01 01:00:00.000000000 +0100
46016270 19300+++ linux/fs/aufs/ioctl.c 2021-02-24 13:33:42.744347058 +0100
062440b3 19301@@ -0,0 +1,220 @@
cd7a4cd9 19302+// SPDX-License-Identifier: GPL-2.0
4a4d8108 19303+/*
d58c55f2 19304+ * Copyright (C) 2005-2020 Junjiro R. Okajima
4a4d8108
AM
19305+ *
19306+ * This program, aufs is free software; you can redistribute it and/or modify
19307+ * it under the terms of the GNU General Public License as published by
19308+ * the Free Software Foundation; either version 2 of the License, or
19309+ * (at your option) any later version.
19310+ *
19311+ * This program is distributed in the hope that it will be useful,
19312+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19313+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19314+ * GNU General Public License for more details.
19315+ *
19316+ * You should have received a copy of the GNU General Public License
523b37e3 19317+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
19318+ */
19319+
19320+/*
19321+ * ioctl
19322+ * plink-management and readdir in userspace.
19323+ * assist the pathconf(3) wrapper library.
c2b27bf2 19324+ * move-down
076b876e 19325+ * File-based Hierarchical Storage Management.
4a4d8108
AM
19326+ */
19327+
c2b27bf2
AM
19328+#include <linux/compat.h>
19329+#include <linux/file.h>
4a4d8108
AM
19330+#include "aufs.h"
19331+
1e00d052 19332+static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
4a4d8108
AM
19333+{
19334+ int err, fd;
5afbbe0d 19335+ aufs_bindex_t wbi, bindex, bbot;
4a4d8108
AM
19336+ struct file *h_file;
19337+ struct super_block *sb;
19338+ struct dentry *root;
1e00d052
AM
19339+ struct au_branch *br;
19340+ struct aufs_wbr_fd wbrfd = {
19341+ .oflags = au_dir_roflags,
19342+ .brid = -1
19343+ };
19344+ const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
19345+ | O_NOATIME | O_CLOEXEC;
4a4d8108 19346+
1e00d052
AM
19347+ AuDebugOn(wbrfd.oflags & ~valid);
19348+
19349+ if (arg) {
19350+ err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
19351+ if (unlikely(err)) {
19352+ err = -EFAULT;
19353+ goto out;
19354+ }
19355+
19356+ err = -EINVAL;
19357+ AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
19358+ wbrfd.oflags |= au_dir_roflags;
19359+ AuDbg("0%o\n", wbrfd.oflags);
19360+ if (unlikely(wbrfd.oflags & ~valid))
19361+ goto out;
19362+ }
19363+
2000de60 19364+ fd = get_unused_fd_flags(0);
1e00d052
AM
19365+ err = fd;
19366+ if (unlikely(fd < 0))
4a4d8108 19367+ goto out;
4a4d8108 19368+
1e00d052 19369+ h_file = ERR_PTR(-EINVAL);
4a4d8108 19370+ wbi = 0;
1e00d052 19371+ br = NULL;
4a4d8108
AM
19372+ sb = path->dentry->d_sb;
19373+ root = sb->s_root;
19374+ aufs_read_lock(root, AuLock_IR);
5afbbe0d 19375+ bbot = au_sbbot(sb);
1e00d052
AM
19376+ if (wbrfd.brid >= 0) {
19377+ wbi = au_br_index(sb, wbrfd.brid);
5afbbe0d 19378+ if (unlikely(wbi < 0 || wbi > bbot))
1e00d052
AM
19379+ goto out_unlock;
19380+ }
19381+
19382+ h_file = ERR_PTR(-ENOENT);
19383+ br = au_sbr(sb, wbi);
19384+ if (!au_br_writable(br->br_perm)) {
19385+ if (arg)
19386+ goto out_unlock;
19387+
19388+ bindex = wbi + 1;
19389+ wbi = -1;
5afbbe0d 19390+ for (; bindex <= bbot; bindex++) {
1e00d052
AM
19391+ br = au_sbr(sb, bindex);
19392+ if (au_br_writable(br->br_perm)) {
4a4d8108 19393+ wbi = bindex;
1e00d052 19394+ br = au_sbr(sb, wbi);
4a4d8108
AM
19395+ break;
19396+ }
19397+ }
4a4d8108
AM
19398+ }
19399+ AuDbg("wbi %d\n", wbi);
1e00d052 19400+ if (wbi >= 0)
392086de
AM
19401+ h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
19402+ /*force_wr*/0);
1e00d052
AM
19403+
19404+out_unlock:
4a4d8108
AM
19405+ aufs_read_unlock(root, AuLock_IR);
19406+ err = PTR_ERR(h_file);
19407+ if (IS_ERR(h_file))
19408+ goto out_fd;
19409+
acd2b654 19410+ au_lcnt_dec(&br->br_nfiles); /* cf. au_h_open() */
4a4d8108
AM
19411+ fd_install(fd, h_file);
19412+ err = fd;
19413+ goto out; /* success */
19414+
4f0767ce 19415+out_fd:
4a4d8108 19416+ put_unused_fd(fd);
4f0767ce 19417+out:
1e00d052 19418+ AuTraceErr(err);
4a4d8108
AM
19419+ return err;
19420+}
19421+
19422+/* ---------------------------------------------------------------------- */
19423+
19424+long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
19425+{
19426+ long err;
c1595e42 19427+ struct dentry *dentry;
4a4d8108
AM
19428+
19429+ switch (cmd) {
4a4d8108
AM
19430+ case AUFS_CTL_RDU:
19431+ case AUFS_CTL_RDU_INO:
19432+ err = au_rdu_ioctl(file, cmd, arg);
19433+ break;
19434+
19435+ case AUFS_CTL_WBR_FD:
1e00d052 19436+ err = au_wbr_fd(&file->f_path, (void __user *)arg);
4a4d8108
AM
19437+ break;
19438+
027c5e7a
AM
19439+ case AUFS_CTL_IBUSY:
19440+ err = au_ibusy_ioctl(file, arg);
19441+ break;
19442+
076b876e
AM
19443+ case AUFS_CTL_BRINFO:
19444+ err = au_brinfo_ioctl(file, arg);
19445+ break;
19446+
19447+ case AUFS_CTL_FHSM_FD:
2000de60 19448+ dentry = file->f_path.dentry;
c1595e42
JR
19449+ if (IS_ROOT(dentry))
19450+ err = au_fhsm_fd(dentry->d_sb, arg);
19451+ else
19452+ err = -ENOTTY;
076b876e
AM
19453+ break;
19454+
4a4d8108
AM
19455+ default:
19456+ /* do not call the lower */
19457+ AuDbg("0x%x\n", cmd);
19458+ err = -ENOTTY;
19459+ }
19460+
19461+ AuTraceErr(err);
19462+ return err;
19463+}
19464+
19465+long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
19466+{
19467+ long err;
19468+
19469+ switch (cmd) {
c2b27bf2 19470+ case AUFS_CTL_MVDOWN:
2000de60 19471+ err = au_mvdown(file->f_path.dentry, (void __user *)arg);
c2b27bf2
AM
19472+ break;
19473+
4a4d8108 19474+ case AUFS_CTL_WBR_FD:
1e00d052 19475+ err = au_wbr_fd(&file->f_path, (void __user *)arg);
4a4d8108
AM
19476+ break;
19477+
19478+ default:
19479+ /* do not call the lower */
19480+ AuDbg("0x%x\n", cmd);
19481+ err = -ENOTTY;
19482+ }
19483+
19484+ AuTraceErr(err);
19485+ return err;
19486+}
b752ccd1
AM
19487+
19488+#ifdef CONFIG_COMPAT
19489+long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
19490+ unsigned long arg)
19491+{
19492+ long err;
19493+
19494+ switch (cmd) {
19495+ case AUFS_CTL_RDU:
19496+ case AUFS_CTL_RDU_INO:
19497+ err = au_rdu_compat_ioctl(file, cmd, arg);
19498+ break;
19499+
027c5e7a
AM
19500+ case AUFS_CTL_IBUSY:
19501+ err = au_ibusy_compat_ioctl(file, arg);
19502+ break;
19503+
076b876e
AM
19504+ case AUFS_CTL_BRINFO:
19505+ err = au_brinfo_compat_ioctl(file, arg);
19506+ break;
19507+
b752ccd1
AM
19508+ default:
19509+ err = aufs_ioctl_dir(file, cmd, arg);
19510+ }
19511+
19512+ AuTraceErr(err);
19513+ return err;
19514+}
19515+
b752ccd1
AM
19516+long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
19517+ unsigned long arg)
19518+{
19519+ return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
19520+}
19521+#endif
7f207e10 19522diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
eca34b5c 19523--- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
46016270 19524+++ linux/fs/aufs/i_op_add.c 2021-06-30 21:35:11.397206648 +0200
0b2a12c6 19525@@ -0,0 +1,941 @@
cd7a4cd9 19526+// SPDX-License-Identifier: GPL-2.0
4a4d8108 19527+/*
d58c55f2 19528+ * Copyright (C) 2005-2020 Junjiro R. Okajima
4a4d8108
AM
19529+ *
19530+ * This program, aufs is free software; you can redistribute it and/or modify
19531+ * it under the terms of the GNU General Public License as published by
19532+ * the Free Software Foundation; either version 2 of the License, or
19533+ * (at your option) any later version.
19534+ *
19535+ * This program is distributed in the hope that it will be useful,
19536+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19537+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19538+ * GNU General Public License for more details.
19539+ *
19540+ * You should have received a copy of the GNU General Public License
523b37e3 19541+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
19542+ */
19543+
19544+/*
19545+ * inode operations (add entry)
19546+ */
19547+
eca801bf 19548+#include <linux/iversion.h>
4a4d8108
AM
19549+#include "aufs.h"
19550+
19551+/*
19552+ * final procedure of adding a new entry, except link(2).
19553+ * remove whiteout, instantiate, copyup the parent dir's times and size
19554+ * and update version.
19555+ * if it failed, re-create the removed whiteout.
19556+ */
19557+static int epilog(struct inode *dir, aufs_bindex_t bindex,
19558+ struct dentry *wh_dentry, struct dentry *dentry)
19559+{
19560+ int err, rerr;
19561+ aufs_bindex_t bwh;
19562+ struct path h_path;
076b876e 19563+ struct super_block *sb;
4a4d8108
AM
19564+ struct inode *inode, *h_dir;
19565+ struct dentry *wh;
19566+
19567+ bwh = -1;
076b876e 19568+ sb = dir->i_sb;
4a4d8108 19569+ if (wh_dentry) {
5527c038 19570+ h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
4a4d8108
AM
19571+ IMustLock(h_dir);
19572+ AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
19573+ bwh = au_dbwh(dentry);
19574+ h_path.dentry = wh_dentry;
076b876e 19575+ h_path.mnt = au_sbr_mnt(sb, bindex);
4a4d8108
AM
19576+ err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
19577+ dentry);
19578+ if (unlikely(err))
19579+ goto out;
19580+ }
19581+
19582+ inode = au_new_inode(dentry, /*must_new*/1);
19583+ if (!IS_ERR(inode)) {
19584+ d_instantiate(dentry, inode);
5527c038 19585+ dir = d_inode(dentry->d_parent); /* dir inode is locked */
4a4d8108 19586+ IMustLock(dir);
b912730e 19587+ au_dir_ts(dir, bindex);
be118d29 19588+ inode_inc_iversion(dir);
076b876e 19589+ au_fhsm_wrote(sb, bindex, /*force*/0);
4a4d8108
AM
19590+ return 0; /* success */
19591+ }
19592+
19593+ err = PTR_ERR(inode);
19594+ if (!wh_dentry)
19595+ goto out;
19596+
19597+ /* revert */
19598+ /* dir inode is locked */
19599+ wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
19600+ rerr = PTR_ERR(wh);
19601+ if (IS_ERR(wh)) {
523b37e3
AM
19602+ AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
19603+ dentry, err, rerr);
4a4d8108
AM
19604+ err = -EIO;
19605+ } else
19606+ dput(wh);
19607+
4f0767ce 19608+out:
4a4d8108
AM
19609+ return err;
19610+}
19611+
027c5e7a
AM
19612+static int au_d_may_add(struct dentry *dentry)
19613+{
19614+ int err;
19615+
19616+ err = 0;
19617+ if (unlikely(d_unhashed(dentry)))
19618+ err = -ENOENT;
5527c038 19619+ if (unlikely(d_really_is_positive(dentry)))
027c5e7a
AM
19620+ err = -EEXIST;
19621+ return err;
19622+}
19623+
4a4d8108
AM
19624+/*
19625+ * simple tests for the adding inode operations.
19626+ * following the checks in vfs, plus the parent-child relationship.
19627+ */
19628+int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
19629+ struct dentry *h_parent, int isdir)
19630+{
19631+ int err;
19632+ umode_t h_mode;
19633+ struct dentry *h_dentry;
19634+ struct inode *h_inode;
19635+
19636+ err = -ENAMETOOLONG;
19637+ if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19638+ goto out;
19639+
19640+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 19641+ if (d_really_is_negative(dentry)) {
4a4d8108 19642+ err = -EEXIST;
5527c038 19643+ if (unlikely(d_is_positive(h_dentry)))
4a4d8108
AM
19644+ goto out;
19645+ } else {
19646+ /* rename(2) case */
19647+ err = -EIO;
5527c038
JR
19648+ if (unlikely(d_is_negative(h_dentry)))
19649+ goto out;
19650+ h_inode = d_inode(h_dentry);
19651+ if (unlikely(!h_inode->i_nlink))
4a4d8108
AM
19652+ goto out;
19653+
19654+ h_mode = h_inode->i_mode;
19655+ if (!isdir) {
19656+ err = -EISDIR;
19657+ if (unlikely(S_ISDIR(h_mode)))
19658+ goto out;
19659+ } else if (unlikely(!S_ISDIR(h_mode))) {
19660+ err = -ENOTDIR;
19661+ goto out;
19662+ }
19663+ }
19664+
19665+ err = 0;
19666+ /* expected parent dir is locked */
19667+ if (unlikely(h_parent != h_dentry->d_parent))
19668+ err = -EIO;
19669+
4f0767ce 19670+out:
4a4d8108
AM
19671+ AuTraceErr(err);
19672+ return err;
19673+}
19674+
19675+/*
19676+ * initial procedure of adding a new entry.
19677+ * prepare writable branch and the parent dir, lock it,
19678+ * and lookup whiteout for the new entry.
19679+ */
19680+static struct dentry*
19681+lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
19682+ struct dentry *src_dentry, struct au_pin *pin,
19683+ struct au_wr_dir_args *wr_dir_args)
19684+{
19685+ struct dentry *wh_dentry, *h_parent;
19686+ struct super_block *sb;
19687+ struct au_branch *br;
19688+ int err;
19689+ unsigned int udba;
19690+ aufs_bindex_t bcpup;
19691+
523b37e3 19692+ AuDbg("%pd\n", dentry);
4a4d8108
AM
19693+
19694+ err = au_wr_dir(dentry, src_dentry, wr_dir_args);
19695+ bcpup = err;
19696+ wh_dentry = ERR_PTR(err);
19697+ if (unlikely(err < 0))
19698+ goto out;
19699+
19700+ sb = dentry->d_sb;
19701+ udba = au_opt_udba(sb);
19702+ err = au_pin(pin, dentry, bcpup, udba,
19703+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
19704+ wh_dentry = ERR_PTR(err);
19705+ if (unlikely(err))
19706+ goto out;
19707+
19708+ h_parent = au_pinned_h_parent(pin);
19709+ if (udba != AuOpt_UDBA_NONE
5afbbe0d 19710+ && au_dbtop(dentry) == bcpup)
4a4d8108
AM
19711+ err = au_may_add(dentry, bcpup, h_parent,
19712+ au_ftest_wrdir(wr_dir_args->flags, ISDIR));
19713+ else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19714+ err = -ENAMETOOLONG;
19715+ wh_dentry = ERR_PTR(err);
19716+ if (unlikely(err))
19717+ goto out_unpin;
19718+
19719+ br = au_sbr(sb, bcpup);
19720+ if (dt) {
19721+ struct path tmp = {
19722+ .dentry = h_parent,
86dc4139 19723+ .mnt = au_br_mnt(br)
4a4d8108
AM
19724+ };
19725+ au_dtime_store(dt, au_pinned_parent(pin), &tmp);
19726+ }
19727+
19728+ wh_dentry = NULL;
19729+ if (bcpup != au_dbwh(dentry))
19730+ goto out; /* success */
19731+
2000de60
JR
19732+ /*
19733+ * ENAMETOOLONG here means that if we allowed create such name, then it
19734+ * would not be able to removed in the future. So we don't allow such
19735+ * name here and we don't handle ENAMETOOLONG differently here.
19736+ */
4a4d8108
AM
19737+ wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
19738+
4f0767ce 19739+out_unpin:
4a4d8108
AM
19740+ if (IS_ERR(wh_dentry))
19741+ au_unpin(pin);
4f0767ce 19742+out:
4a4d8108
AM
19743+ return wh_dentry;
19744+}
19745+
19746+/* ---------------------------------------------------------------------- */
19747+
19748+enum { Mknod, Symlink, Creat };
19749+struct simple_arg {
19750+ int type;
19751+ union {
19752+ struct {
b912730e
AM
19753+ umode_t mode;
19754+ bool want_excl;
19755+ bool try_aopen;
19756+ struct vfsub_aopen_args *aopen;
4a4d8108
AM
19757+ } c;
19758+ struct {
19759+ const char *symname;
19760+ } s;
19761+ struct {
7eafdf33 19762+ umode_t mode;
4a4d8108
AM
19763+ dev_t dev;
19764+ } m;
19765+ } u;
19766+};
19767+
19768+static int add_simple(struct inode *dir, struct dentry *dentry,
19769+ struct simple_arg *arg)
19770+{
076b876e 19771+ int err, rerr;
5afbbe0d 19772+ aufs_bindex_t btop;
4a4d8108 19773+ unsigned char created;
b912730e
AM
19774+ const unsigned char try_aopen
19775+ = (arg->type == Creat && arg->u.c.try_aopen);
acd2b654 19776+ struct vfsub_aopen_args *aopen = arg->u.c.aopen;
4a4d8108
AM
19777+ struct dentry *wh_dentry, *parent;
19778+ struct inode *h_dir;
b912730e
AM
19779+ struct super_block *sb;
19780+ struct au_branch *br;
acd2b654 19781+ /* to reduce stack size */
c2b27bf2
AM
19782+ struct {
19783+ struct au_dtime dt;
19784+ struct au_pin pin;
19785+ struct path h_path;
19786+ struct au_wr_dir_args wr_dir_args;
19787+ } *a;
4a4d8108 19788+
523b37e3 19789+ AuDbg("%pd\n", dentry);
4a4d8108
AM
19790+ IMustLock(dir);
19791+
c2b27bf2
AM
19792+ err = -ENOMEM;
19793+ a = kmalloc(sizeof(*a), GFP_NOFS);
19794+ if (unlikely(!a))
19795+ goto out;
19796+ a->wr_dir_args.force_btgt = -1;
19797+ a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
19798+
4a4d8108 19799+ parent = dentry->d_parent; /* dir inode is locked */
b912730e
AM
19800+ if (!try_aopen) {
19801+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
19802+ if (unlikely(err))
19803+ goto out_free;
19804+ }
027c5e7a
AM
19805+ err = au_d_may_add(dentry);
19806+ if (unlikely(err))
19807+ goto out_unlock;
b912730e
AM
19808+ if (!try_aopen)
19809+ di_write_lock_parent(parent);
c2b27bf2
AM
19810+ wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
19811+ &a->pin, &a->wr_dir_args);
4a4d8108
AM
19812+ err = PTR_ERR(wh_dentry);
19813+ if (IS_ERR(wh_dentry))
027c5e7a 19814+ goto out_parent;
4a4d8108 19815+
5afbbe0d 19816+ btop = au_dbtop(dentry);
b912730e 19817+ sb = dentry->d_sb;
5afbbe0d
AM
19818+ br = au_sbr(sb, btop);
19819+ a->h_path.dentry = au_h_dptr(dentry, btop);
b912730e 19820+ a->h_path.mnt = au_br_mnt(br);
c2b27bf2 19821+ h_dir = au_pinned_h_dir(&a->pin);
4a4d8108
AM
19822+ switch (arg->type) {
19823+ case Creat:
acd2b654 19824+ if (!try_aopen || !h_dir->i_op->atomic_open) {
b912730e
AM
19825+ err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
19826+ arg->u.c.want_excl);
acd2b654
AM
19827+ created = !err;
19828+ if (!err && try_aopen)
19829+ aopen->file->f_mode |= FMODE_CREATED;
19830+ } else {
19831+ aopen->br = br;
19832+ err = vfsub_atomic_open(h_dir, a->h_path.dentry, aopen);
19833+ AuDbg("err %d\n", err);
19834+ AuDbgFile(aopen->file);
19835+ created = err >= 0
19836+ && !!(aopen->file->f_mode & FMODE_CREATED);
19837+ }
4a4d8108
AM
19838+ break;
19839+ case Symlink:
c2b27bf2 19840+ err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
acd2b654 19841+ created = !err;
4a4d8108
AM
19842+ break;
19843+ case Mknod:
c2b27bf2
AM
19844+ err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
19845+ arg->u.m.dev);
acd2b654 19846+ created = !err;
4a4d8108
AM
19847+ break;
19848+ default:
19849+ BUG();
19850+ }
acd2b654
AM
19851+ if (unlikely(err < 0))
19852+ goto out_unpin;
19853+
19854+ err = epilog(dir, btop, wh_dentry, dentry);
4a4d8108 19855+ if (!err)
acd2b654 19856+ goto out_unpin; /* success */
4a4d8108
AM
19857+
19858+ /* revert */
acd2b654 19859+ if (created /* && d_is_positive(a->h_path.dentry) */) {
523b37e3
AM
19860+ /* no delegation since it is just created */
19861+ rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
19862+ /*force*/0);
4a4d8108 19863+ if (rerr) {
523b37e3
AM
19864+ AuIOErr("%pd revert failure(%d, %d)\n",
19865+ dentry, err, rerr);
4a4d8108
AM
19866+ err = -EIO;
19867+ }
c2b27bf2 19868+ au_dtime_revert(&a->dt);
4a4d8108 19869+ }
acd2b654
AM
19870+ if (try_aopen && h_dir->i_op->atomic_open
19871+ && (aopen->file->f_mode & FMODE_OPENED))
19872+ /* aopen->file is still opened */
19873+ au_lcnt_dec(&aopen->br->br_nfiles);
4a4d8108 19874+
acd2b654 19875+out_unpin:
c2b27bf2 19876+ au_unpin(&a->pin);
4a4d8108 19877+ dput(wh_dentry);
027c5e7a 19878+out_parent:
b912730e
AM
19879+ if (!try_aopen)
19880+ di_write_unlock(parent);
027c5e7a 19881+out_unlock:
4a4d8108 19882+ if (unlikely(err)) {
5afbbe0d 19883+ au_update_dbtop(dentry);
4a4d8108
AM
19884+ d_drop(dentry);
19885+ }
b912730e
AM
19886+ if (!try_aopen)
19887+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2 19888+out_free:
9f237c51 19889+ au_kfree_rcu(a);
027c5e7a 19890+out:
4a4d8108
AM
19891+ return err;
19892+}
19893+
0b2a12c6
JR
19894+int aufs_mknod(struct user_namespace *userns, struct inode *dir,
19895+ struct dentry *dentry, umode_t mode, dev_t dev)
4a4d8108
AM
19896+{
19897+ struct simple_arg arg = {
19898+ .type = Mknod,
19899+ .u.m = {
19900+ .mode = mode,
19901+ .dev = dev
19902+ }
19903+ };
19904+ return add_simple(dir, dentry, &arg);
19905+}
19906+
0b2a12c6
JR
19907+int aufs_symlink(struct user_namespace *userns, struct inode *dir,
19908+ struct dentry *dentry, const char *symname)
4a4d8108
AM
19909+{
19910+ struct simple_arg arg = {
19911+ .type = Symlink,
19912+ .u.s.symname = symname
19913+ };
19914+ return add_simple(dir, dentry, &arg);
19915+}
19916+
0b2a12c6
JR
19917+int aufs_create(struct user_namespace *userns, struct inode *dir,
19918+ struct dentry *dentry, umode_t mode, bool want_excl)
4a4d8108
AM
19919+{
19920+ struct simple_arg arg = {
19921+ .type = Creat,
19922+ .u.c = {
b4510431
AM
19923+ .mode = mode,
19924+ .want_excl = want_excl
4a4d8108
AM
19925+ }
19926+ };
19927+ return add_simple(dir, dentry, &arg);
19928+}
19929+
b912730e
AM
19930+int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
19931+ struct vfsub_aopen_args *aopen_args)
19932+{
19933+ struct simple_arg arg = {
19934+ .type = Creat,
19935+ .u.c = {
19936+ .mode = aopen_args->create_mode,
19937+ .want_excl = aopen_args->open_flag & O_EXCL,
19938+ .try_aopen = true,
19939+ .aopen = aopen_args
19940+ }
19941+ };
19942+ return add_simple(dir, dentry, &arg);
19943+}
19944+
0b2a12c6
JR
19945+int aufs_tmpfile(struct user_namespace *userns, struct inode *dir,
19946+ struct dentry *dentry, umode_t mode)
38d290e6
JR
19947+{
19948+ int err;
19949+ aufs_bindex_t bindex;
19950+ struct super_block *sb;
19951+ struct dentry *parent, *h_parent, *h_dentry;
19952+ struct inode *h_dir, *inode;
19953+ struct vfsmount *h_mnt;
0b2a12c6 19954+ struct user_namespace *h_userns;
38d290e6
JR
19955+ struct au_wr_dir_args wr_dir_args = {
19956+ .force_btgt = -1,
19957+ .flags = AuWrDir_TMPFILE
19958+ };
19959+
19960+ /* copy-up may happen */
febd17d6 19961+ inode_lock(dir);
38d290e6
JR
19962+
19963+ sb = dir->i_sb;
19964+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
19965+ if (unlikely(err))
19966+ goto out;
19967+
19968+ err = au_di_init(dentry);
19969+ if (unlikely(err))
19970+ goto out_si;
19971+
19972+ err = -EBUSY;
19973+ parent = d_find_any_alias(dir);
19974+ AuDebugOn(!parent);
19975+ di_write_lock_parent(parent);
5527c038 19976+ if (unlikely(d_inode(parent) != dir))
38d290e6
JR
19977+ goto out_parent;
19978+
19979+ err = au_digen_test(parent, au_sigen(sb));
19980+ if (unlikely(err))
19981+ goto out_parent;
19982+
5afbbe0d
AM
19983+ bindex = au_dbtop(parent);
19984+ au_set_dbtop(dentry, bindex);
19985+ au_set_dbbot(dentry, bindex);
38d290e6
JR
19986+ err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
19987+ bindex = err;
19988+ if (unlikely(err < 0))
19989+ goto out_parent;
19990+
19991+ err = -EOPNOTSUPP;
19992+ h_dir = au_h_iptr(dir, bindex);
19993+ if (unlikely(!h_dir->i_op->tmpfile))
19994+ goto out_parent;
19995+
19996+ h_mnt = au_sbr_mnt(sb, bindex);
19997+ err = vfsub_mnt_want_write(h_mnt);
19998+ if (unlikely(err))
19999+ goto out_parent;
20000+
0b2a12c6 20001+ h_userns = mnt_user_ns(h_mnt);
38d290e6 20002+ h_parent = au_h_dptr(parent, bindex);
0b2a12c6 20003+ h_dentry = vfs_tmpfile(h_userns, h_parent, mode, /*open_flag*/0);
521ced18
JR
20004+ if (IS_ERR(h_dentry)) {
20005+ err = PTR_ERR(h_dentry);
38d290e6 20006+ goto out_mnt;
521ced18 20007+ }
38d290e6 20008+
5afbbe0d
AM
20009+ au_set_dbtop(dentry, bindex);
20010+ au_set_dbbot(dentry, bindex);
38d290e6
JR
20011+ au_set_h_dptr(dentry, bindex, dget(h_dentry));
20012+ inode = au_new_inode(dentry, /*must_new*/1);
20013+ if (IS_ERR(inode)) {
20014+ err = PTR_ERR(inode);
20015+ au_set_h_dptr(dentry, bindex, NULL);
5afbbe0d
AM
20016+ au_set_dbtop(dentry, -1);
20017+ au_set_dbbot(dentry, -1);
38d290e6
JR
20018+ } else {
20019+ if (!inode->i_nlink)
20020+ set_nlink(inode, 1);
20021+ d_tmpfile(dentry, inode);
20022+ au_di(dentry)->di_tmpfile = 1;
20023+
20024+ /* update without i_mutex */
5afbbe0d 20025+ if (au_ibtop(dir) == au_dbtop(dentry))
38d290e6
JR
20026+ au_cpup_attr_timesizes(dir);
20027+ }
38d290e6 20028+ dput(h_dentry);
521ced18 20029+
38d290e6
JR
20030+out_mnt:
20031+ vfsub_mnt_drop_write(h_mnt);
20032+out_parent:
20033+ di_write_unlock(parent);
20034+ dput(parent);
20035+ di_write_unlock(dentry);
5afbbe0d 20036+ if (unlikely(err)) {
38d290e6
JR
20037+ au_di_fin(dentry);
20038+ dentry->d_fsdata = NULL;
20039+ }
20040+out_si:
20041+ si_read_unlock(sb);
20042+out:
febd17d6 20043+ inode_unlock(dir);
38d290e6
JR
20044+ return err;
20045+}
20046+
4a4d8108
AM
20047+/* ---------------------------------------------------------------------- */
20048+
20049+struct au_link_args {
20050+ aufs_bindex_t bdst, bsrc;
20051+ struct au_pin pin;
20052+ struct path h_path;
20053+ struct dentry *src_parent, *parent;
20054+};
20055+
20056+static int au_cpup_before_link(struct dentry *src_dentry,
20057+ struct au_link_args *a)
20058+{
20059+ int err;
20060+ struct dentry *h_src_dentry;
c2b27bf2
AM
20061+ struct au_cp_generic cpg = {
20062+ .dentry = src_dentry,
20063+ .bdst = a->bdst,
20064+ .bsrc = a->bsrc,
20065+ .len = -1,
20066+ .pin = &a->pin,
20067+ .flags = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
20068+ };
4a4d8108
AM
20069+
20070+ di_read_lock_parent(a->src_parent, AuLock_IR);
20071+ err = au_test_and_cpup_dirs(src_dentry, a->bdst);
20072+ if (unlikely(err))
20073+ goto out;
20074+
20075+ h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
4a4d8108
AM
20076+ err = au_pin(&a->pin, src_dentry, a->bdst,
20077+ au_opt_udba(src_dentry->d_sb),
20078+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20079+ if (unlikely(err))
20080+ goto out;
367653fa 20081+
c2b27bf2 20082+ err = au_sio_cpup_simple(&cpg);
4a4d8108
AM
20083+ au_unpin(&a->pin);
20084+
4f0767ce 20085+out:
4a4d8108
AM
20086+ di_read_unlock(a->src_parent, AuLock_IR);
20087+ return err;
20088+}
20089+
86dc4139
AM
20090+static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
20091+ struct au_link_args *a)
4a4d8108
AM
20092+{
20093+ int err;
20094+ unsigned char plink;
5afbbe0d 20095+ aufs_bindex_t bbot;
4a4d8108 20096+ struct dentry *h_src_dentry;
523b37e3 20097+ struct inode *h_inode, *inode, *delegated;
4a4d8108
AM
20098+ struct super_block *sb;
20099+ struct file *h_file;
20100+
20101+ plink = 0;
20102+ h_inode = NULL;
20103+ sb = src_dentry->d_sb;
5527c038 20104+ inode = d_inode(src_dentry);
5afbbe0d 20105+ if (au_ibtop(inode) <= a->bdst)
4a4d8108
AM
20106+ h_inode = au_h_iptr(inode, a->bdst);
20107+ if (!h_inode || !h_inode->i_nlink) {
20108+ /* copyup src_dentry as the name of dentry. */
5afbbe0d
AM
20109+ bbot = au_dbbot(dentry);
20110+ if (bbot < a->bsrc)
20111+ au_set_dbbot(dentry, a->bsrc);
86dc4139
AM
20112+ au_set_h_dptr(dentry, a->bsrc,
20113+ dget(au_h_dptr(src_dentry, a->bsrc)));
20114+ dget(a->h_path.dentry);
20115+ au_set_h_dptr(dentry, a->bdst, NULL);
c1595e42
JR
20116+ AuDbg("temporary d_inode...\n");
20117+ spin_lock(&dentry->d_lock);
5527c038 20118+ dentry->d_inode = d_inode(src_dentry); /* tmp */
c1595e42 20119+ spin_unlock(&dentry->d_lock);
392086de 20120+ h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
86dc4139 20121+ if (IS_ERR(h_file))
4a4d8108 20122+ err = PTR_ERR(h_file);
86dc4139 20123+ else {
c2b27bf2
AM
20124+ struct au_cp_generic cpg = {
20125+ .dentry = dentry,
20126+ .bdst = a->bdst,
20127+ .bsrc = -1,
20128+ .len = -1,
20129+ .pin = &a->pin,
20130+ .flags = AuCpup_KEEPLINO
20131+ };
20132+ err = au_sio_cpup_simple(&cpg);
86dc4139
AM
20133+ au_h_open_post(dentry, a->bsrc, h_file);
20134+ if (!err) {
20135+ dput(a->h_path.dentry);
20136+ a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20137+ } else
20138+ au_set_h_dptr(dentry, a->bdst,
20139+ a->h_path.dentry);
20140+ }
c1595e42 20141+ spin_lock(&dentry->d_lock);
86dc4139 20142+ dentry->d_inode = NULL; /* restore */
c1595e42
JR
20143+ spin_unlock(&dentry->d_lock);
20144+ AuDbg("temporary d_inode...done\n");
86dc4139 20145+ au_set_h_dptr(dentry, a->bsrc, NULL);
5afbbe0d 20146+ au_set_dbbot(dentry, bbot);
4a4d8108
AM
20147+ } else {
20148+ /* the inode of src_dentry already exists on a.bdst branch */
20149+ h_src_dentry = d_find_alias(h_inode);
20150+ if (!h_src_dentry && au_plink_test(inode)) {
20151+ plink = 1;
20152+ h_src_dentry = au_plink_lkup(inode, a->bdst);
20153+ err = PTR_ERR(h_src_dentry);
20154+ if (IS_ERR(h_src_dentry))
20155+ goto out;
20156+
5527c038 20157+ if (unlikely(d_is_negative(h_src_dentry))) {
4a4d8108
AM
20158+ dput(h_src_dentry);
20159+ h_src_dentry = NULL;
20160+ }
20161+
20162+ }
20163+ if (h_src_dentry) {
523b37e3 20164+ delegated = NULL;
4a4d8108 20165+ err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
523b37e3
AM
20166+ &a->h_path, &delegated);
20167+ if (unlikely(err == -EWOULDBLOCK)) {
20168+ pr_warn("cannot retry for NFSv4 delegation"
20169+ " for an internal link\n");
20170+ iput(delegated);
20171+ }
4a4d8108
AM
20172+ dput(h_src_dentry);
20173+ } else {
20174+ AuIOErr("no dentry found for hi%lu on b%d\n",
20175+ h_inode->i_ino, a->bdst);
20176+ err = -EIO;
20177+ }
20178+ }
20179+
20180+ if (!err && !plink)
20181+ au_plink_append(inode, a->bdst, a->h_path.dentry);
20182+
20183+out:
2cbb1c4b 20184+ AuTraceErr(err);
4a4d8108
AM
20185+ return err;
20186+}
20187+
20188+int aufs_link(struct dentry *src_dentry, struct inode *dir,
20189+ struct dentry *dentry)
20190+{
20191+ int err, rerr;
20192+ struct au_dtime dt;
20193+ struct au_link_args *a;
20194+ struct dentry *wh_dentry, *h_src_dentry;
523b37e3 20195+ struct inode *inode, *delegated;
4a4d8108
AM
20196+ struct super_block *sb;
20197+ struct au_wr_dir_args wr_dir_args = {
20198+ /* .force_btgt = -1, */
20199+ .flags = AuWrDir_ADD_ENTRY
20200+ };
20201+
20202+ IMustLock(dir);
5527c038 20203+ inode = d_inode(src_dentry);
4a4d8108
AM
20204+ IMustLock(inode);
20205+
4a4d8108
AM
20206+ err = -ENOMEM;
20207+ a = kzalloc(sizeof(*a), GFP_NOFS);
20208+ if (unlikely(!a))
20209+ goto out;
20210+
20211+ a->parent = dentry->d_parent; /* dir inode is locked */
027c5e7a
AM
20212+ err = aufs_read_and_write_lock2(dentry, src_dentry,
20213+ AuLock_NOPLM | AuLock_GEN);
e49829fe
JR
20214+ if (unlikely(err))
20215+ goto out_kfree;
38d290e6 20216+ err = au_d_linkable(src_dentry);
027c5e7a
AM
20217+ if (unlikely(err))
20218+ goto out_unlock;
20219+ err = au_d_may_add(dentry);
20220+ if (unlikely(err))
20221+ goto out_unlock;
e49829fe 20222+
4a4d8108 20223+ a->src_parent = dget_parent(src_dentry);
5afbbe0d 20224+ wr_dir_args.force_btgt = au_ibtop(inode);
4a4d8108
AM
20225+
20226+ di_write_lock_parent(a->parent);
20227+ wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
20228+ wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
20229+ &wr_dir_args);
20230+ err = PTR_ERR(wh_dentry);
20231+ if (IS_ERR(wh_dentry))
027c5e7a 20232+ goto out_parent;
4a4d8108
AM
20233+
20234+ err = 0;
20235+ sb = dentry->d_sb;
5afbbe0d 20236+ a->bdst = au_dbtop(dentry);
4a4d8108
AM
20237+ a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20238+ a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
5afbbe0d 20239+ a->bsrc = au_ibtop(inode);
2cbb1c4b 20240+ h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
38d290e6
JR
20241+ if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
20242+ h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
2cbb1c4b 20243+ if (!h_src_dentry) {
5afbbe0d 20244+ a->bsrc = au_dbtop(src_dentry);
2cbb1c4b
JR
20245+ h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
20246+ AuDebugOn(!h_src_dentry);
38d290e6
JR
20247+ } else if (IS_ERR(h_src_dentry)) {
20248+ err = PTR_ERR(h_src_dentry);
2cbb1c4b 20249+ goto out_parent;
38d290e6 20250+ }
2cbb1c4b 20251+
f2c43d5f
AM
20252+ /*
20253+ * aufs doesn't touch the credential so
acd2b654 20254+ * security_dentry_create_files_as() is unnecessary.
f2c43d5f 20255+ */
4a4d8108
AM
20256+ if (au_opt_test(au_mntflags(sb), PLINK)) {
20257+ if (a->bdst < a->bsrc
20258+ /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
86dc4139 20259+ err = au_cpup_or_link(src_dentry, dentry, a);
523b37e3
AM
20260+ else {
20261+ delegated = NULL;
4a4d8108 20262+ err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
523b37e3
AM
20263+ &a->h_path, &delegated);
20264+ if (unlikely(err == -EWOULDBLOCK)) {
20265+ pr_warn("cannot retry for NFSv4 delegation"
20266+ " for an internal link\n");
20267+ iput(delegated);
20268+ }
20269+ }
2cbb1c4b 20270+ dput(h_src_dentry);
4a4d8108
AM
20271+ } else {
20272+ /*
20273+ * copyup src_dentry to the branch we process,
20274+ * and then link(2) to it.
20275+ */
2cbb1c4b 20276+ dput(h_src_dentry);
4a4d8108
AM
20277+ if (a->bdst < a->bsrc
20278+ /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
20279+ au_unpin(&a->pin);
20280+ di_write_unlock(a->parent);
20281+ err = au_cpup_before_link(src_dentry, a);
20282+ di_write_lock_parent(a->parent);
20283+ if (!err)
20284+ err = au_pin(&a->pin, dentry, a->bdst,
20285+ au_opt_udba(sb),
20286+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20287+ if (unlikely(err))
20288+ goto out_wh;
20289+ }
20290+ if (!err) {
20291+ h_src_dentry = au_h_dptr(src_dentry, a->bdst);
20292+ err = -ENOENT;
5527c038 20293+ if (h_src_dentry && d_is_positive(h_src_dentry)) {
523b37e3 20294+ delegated = NULL;
4a4d8108
AM
20295+ err = vfsub_link(h_src_dentry,
20296+ au_pinned_h_dir(&a->pin),
523b37e3
AM
20297+ &a->h_path, &delegated);
20298+ if (unlikely(err == -EWOULDBLOCK)) {
20299+ pr_warn("cannot retry"
20300+ " for NFSv4 delegation"
20301+ " for an internal link\n");
20302+ iput(delegated);
20303+ }
20304+ }
4a4d8108
AM
20305+ }
20306+ }
20307+ if (unlikely(err))
20308+ goto out_unpin;
20309+
20310+ if (wh_dentry) {
20311+ a->h_path.dentry = wh_dentry;
20312+ err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
20313+ dentry);
20314+ if (unlikely(err))
20315+ goto out_revert;
20316+ }
20317+
b912730e 20318+ au_dir_ts(dir, a->bdst);
be118d29 20319+ inode_inc_iversion(dir);
4a4d8108
AM
20320+ inc_nlink(inode);
20321+ inode->i_ctime = dir->i_ctime;
027c5e7a
AM
20322+ d_instantiate(dentry, au_igrab(inode));
20323+ if (d_unhashed(a->h_path.dentry))
4a4d8108
AM
20324+ /* some filesystem calls d_drop() */
20325+ d_drop(dentry);
076b876e
AM
20326+ /* some filesystems consume an inode even hardlink */
20327+ au_fhsm_wrote(sb, a->bdst, /*force*/0);
4a4d8108
AM
20328+ goto out_unpin; /* success */
20329+
4f0767ce 20330+out_revert:
523b37e3
AM
20331+ /* no delegation since it is just created */
20332+ rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
20333+ /*delegated*/NULL, /*force*/0);
027c5e7a 20334+ if (unlikely(rerr)) {
523b37e3 20335+ AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
027c5e7a
AM
20336+ err = -EIO;
20337+ }
4a4d8108 20338+ au_dtime_revert(&dt);
4f0767ce 20339+out_unpin:
4a4d8108 20340+ au_unpin(&a->pin);
4f0767ce 20341+out_wh:
4a4d8108 20342+ dput(wh_dentry);
027c5e7a
AM
20343+out_parent:
20344+ di_write_unlock(a->parent);
20345+ dput(a->src_parent);
4f0767ce 20346+out_unlock:
4a4d8108 20347+ if (unlikely(err)) {
5afbbe0d 20348+ au_update_dbtop(dentry);
4a4d8108
AM
20349+ d_drop(dentry);
20350+ }
4a4d8108 20351+ aufs_read_and_write_unlock2(dentry, src_dentry);
e49829fe 20352+out_kfree:
9f237c51 20353+ au_kfree_rcu(a);
4f0767ce 20354+out:
86dc4139 20355+ AuTraceErr(err);
4a4d8108
AM
20356+ return err;
20357+}
20358+
0b2a12c6
JR
20359+int aufs_mkdir(struct user_namespace *userns, struct inode *dir,
20360+ struct dentry *dentry, umode_t mode)
4a4d8108
AM
20361+{
20362+ int err, rerr;
20363+ aufs_bindex_t bindex;
20364+ unsigned char diropq;
20365+ struct path h_path;
20366+ struct dentry *wh_dentry, *parent, *opq_dentry;
febd17d6 20367+ struct inode *h_inode;
4a4d8108
AM
20368+ struct super_block *sb;
20369+ struct {
20370+ struct au_pin pin;
20371+ struct au_dtime dt;
20372+ } *a; /* reduce the stack usage */
20373+ struct au_wr_dir_args wr_dir_args = {
20374+ .force_btgt = -1,
20375+ .flags = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
20376+ };
20377+
20378+ IMustLock(dir);
20379+
20380+ err = -ENOMEM;
20381+ a = kmalloc(sizeof(*a), GFP_NOFS);
20382+ if (unlikely(!a))
20383+ goto out;
20384+
027c5e7a
AM
20385+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
20386+ if (unlikely(err))
20387+ goto out_free;
20388+ err = au_d_may_add(dentry);
20389+ if (unlikely(err))
20390+ goto out_unlock;
20391+
4a4d8108
AM
20392+ parent = dentry->d_parent; /* dir inode is locked */
20393+ di_write_lock_parent(parent);
20394+ wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
20395+ &a->pin, &wr_dir_args);
20396+ err = PTR_ERR(wh_dentry);
20397+ if (IS_ERR(wh_dentry))
027c5e7a 20398+ goto out_parent;
4a4d8108
AM
20399+
20400+ sb = dentry->d_sb;
5afbbe0d 20401+ bindex = au_dbtop(dentry);
4a4d8108
AM
20402+ h_path.dentry = au_h_dptr(dentry, bindex);
20403+ h_path.mnt = au_sbr_mnt(sb, bindex);
20404+ err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
20405+ if (unlikely(err))
027c5e7a 20406+ goto out_unpin;
4a4d8108
AM
20407+
20408+ /* make the dir opaque */
20409+ diropq = 0;
febd17d6 20410+ h_inode = d_inode(h_path.dentry);
4a4d8108
AM
20411+ if (wh_dentry
20412+ || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
febd17d6 20413+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 20414+ opq_dentry = au_diropq_create(dentry, bindex);
febd17d6 20415+ inode_unlock(h_inode);
4a4d8108
AM
20416+ err = PTR_ERR(opq_dentry);
20417+ if (IS_ERR(opq_dentry))
20418+ goto out_dir;
20419+ dput(opq_dentry);
20420+ diropq = 1;
20421+ }
20422+
20423+ err = epilog(dir, bindex, wh_dentry, dentry);
20424+ if (!err) {
20425+ inc_nlink(dir);
027c5e7a 20426+ goto out_unpin; /* success */
4a4d8108
AM
20427+ }
20428+
20429+ /* revert */
20430+ if (diropq) {
20431+ AuLabel(revert opq);
febd17d6 20432+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 20433+ rerr = au_diropq_remove(dentry, bindex);
febd17d6 20434+ inode_unlock(h_inode);
4a4d8108 20435+ if (rerr) {
523b37e3
AM
20436+ AuIOErr("%pd reverting diropq failed(%d, %d)\n",
20437+ dentry, err, rerr);
4a4d8108
AM
20438+ err = -EIO;
20439+ }
20440+ }
20441+
4f0767ce 20442+out_dir:
4a4d8108
AM
20443+ AuLabel(revert dir);
20444+ rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
20445+ if (rerr) {
523b37e3
AM
20446+ AuIOErr("%pd reverting dir failed(%d, %d)\n",
20447+ dentry, err, rerr);
4a4d8108
AM
20448+ err = -EIO;
20449+ }
4a4d8108 20450+ au_dtime_revert(&a->dt);
027c5e7a 20451+out_unpin:
4a4d8108
AM
20452+ au_unpin(&a->pin);
20453+ dput(wh_dentry);
027c5e7a
AM
20454+out_parent:
20455+ di_write_unlock(parent);
20456+out_unlock:
4a4d8108 20457+ if (unlikely(err)) {
5afbbe0d 20458+ au_update_dbtop(dentry);
4a4d8108
AM
20459+ d_drop(dentry);
20460+ }
4a4d8108 20461+ aufs_read_unlock(dentry, AuLock_DW);
027c5e7a 20462+out_free:
9f237c51 20463+ au_kfree_rcu(a);
4f0767ce 20464+out:
4a4d8108
AM
20465+ return err;
20466+}
7f207e10 20467diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
eca34b5c 20468--- /usr/share/empty/fs/aufs/i_op.c 1970-01-01 01:00:00.000000000 +0100
46016270 20469+++ linux/fs/aufs/i_op.c 2021-06-30 21:35:11.397206648 +0200
0b2a12c6 20470@@ -0,0 +1,1513 @@
cd7a4cd9 20471+// SPDX-License-Identifier: GPL-2.0
4a4d8108 20472+/*
d58c55f2 20473+ * Copyright (C) 2005-2020 Junjiro R. Okajima
4a4d8108
AM
20474+ *
20475+ * This program, aufs is free software; you can redistribute it and/or modify
20476+ * it under the terms of the GNU General Public License as published by
20477+ * the Free Software Foundation; either version 2 of the License, or
20478+ * (at your option) any later version.
20479+ *
20480+ * This program is distributed in the hope that it will be useful,
20481+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20482+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20483+ * GNU General Public License for more details.
20484+ *
20485+ * You should have received a copy of the GNU General Public License
523b37e3 20486+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 20487+ */
1facf9fc 20488+
1308ab2a 20489+/*
4a4d8108 20490+ * inode operations (except add/del/rename)
1308ab2a 20491+ */
4a4d8108
AM
20492+
20493+#include <linux/device_cgroup.h>
20494+#include <linux/fs_stack.h>
eca801bf 20495+#include <linux/iversion.h>
4a4d8108
AM
20496+#include <linux/namei.h>
20497+#include <linux/security.h>
4a4d8108
AM
20498+#include "aufs.h"
20499+
1e00d052 20500+static int h_permission(struct inode *h_inode, int mask,
79b8bda9 20501+ struct path *h_path, int brperm)
1facf9fc 20502+{
1308ab2a 20503+ int err;
4a4d8108 20504+ const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
0b2a12c6 20505+ struct user_namespace *h_userns;
1facf9fc 20506+
e2f27e51
AM
20507+ err = -EPERM;
20508+ if (write_mask && IS_IMMUTABLE(h_inode))
20509+ goto out;
20510+
4a4d8108 20511+ err = -EACCES;
e2f27e51
AM
20512+ if (((mask & MAY_EXEC)
20513+ && S_ISREG(h_inode->i_mode)
20514+ && (path_noexec(h_path)
cd7a4cd9 20515+ || !(h_inode->i_mode & 0111))))
4a4d8108
AM
20516+ goto out;
20517+
20518+ /*
20519+ * - skip the lower fs test in the case of write to ro branch.
20520+ * - nfs dir permission write check is optimized, but a policy for
20521+ * link/rename requires a real check.
2121bcd9 20522+ * - nfs always sets SB_POSIXACL regardless its mount option 'noacl.'
b912730e 20523+ * in this case, generic_permission() returns -EOPNOTSUPP.
4a4d8108 20524+ */
0b2a12c6 20525+ h_userns = mnt_user_ns(h_path->mnt);
4a4d8108
AM
20526+ if ((write_mask && !au_br_writable(brperm))
20527+ || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
20528+ && write_mask && !(mask & MAY_READ))
20529+ || !h_inode->i_op->permission) {
20530+ /* AuLabel(generic_permission); */
062440b3 20531+ /* AuDbg("get_acl %ps\n", h_inode->i_op->get_acl); */
0b2a12c6 20532+ err = generic_permission(h_userns, h_inode, mask);
b912730e 20533+ if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
0b2a12c6
JR
20534+ err = h_inode->i_op->permission(h_userns, h_inode,
20535+ mask);
b912730e 20536+ AuTraceErr(err);
1308ab2a 20537+ } else {
4a4d8108 20538+ /* AuLabel(h_inode->permission); */
0b2a12c6 20539+ err = h_inode->i_op->permission(h_userns, h_inode, mask);
4a4d8108
AM
20540+ AuTraceErr(err);
20541+ }
1facf9fc 20542+
4a4d8108
AM
20543+ if (!err)
20544+ err = devcgroup_inode_permission(h_inode, mask);
7f207e10 20545+ if (!err)
4a4d8108 20546+ err = security_inode_permission(h_inode, mask);
4a4d8108 20547+
4f0767ce 20548+out:
1308ab2a 20549+ return err;
20550+}
dece6358 20551+
0b2a12c6
JR
20552+static int aufs_permission(struct user_namespace *userns, struct inode *inode,
20553+ int mask)
1308ab2a 20554+{
20555+ int err;
5afbbe0d 20556+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
20557+ const unsigned char isdir = !!S_ISDIR(inode->i_mode),
20558+ write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
20559+ struct inode *h_inode;
20560+ struct super_block *sb;
20561+ struct au_branch *br;
1facf9fc 20562+
027c5e7a 20563+ /* todo: support rcu-walk? */
1e00d052 20564+ if (mask & MAY_NOT_BLOCK)
027c5e7a
AM
20565+ return -ECHILD;
20566+
4a4d8108
AM
20567+ sb = inode->i_sb;
20568+ si_read_lock(sb, AuLock_FLUSH);
20569+ ii_read_lock_child(inode);
43982f53
AM
20570+#if 0 /* reserved for future use */
20571+ /*
20572+ * This test may be rather 'too much' since the test is essentially done
20573+ * in the aufs_lookup(). Theoretically it is possible that the inode
20574+ * generation doesn't match to the superblock's here. But it isn't a
20575+ * big deal I suppose.
20576+ */
027c5e7a
AM
20577+ err = au_iigen_test(inode, au_sigen(sb));
20578+ if (unlikely(err))
20579+ goto out;
20580+#endif
dece6358 20581+
076b876e
AM
20582+ if (!isdir
20583+ || write_mask
20584+ || au_opt_test(au_mntflags(sb), DIRPERM1)) {
4a4d8108 20585+ err = au_busy_or_stale();
5afbbe0d 20586+ h_inode = au_h_iptr(inode, au_ibtop(inode));
4a4d8108
AM
20587+ if (unlikely(!h_inode
20588+ || (h_inode->i_mode & S_IFMT)
20589+ != (inode->i_mode & S_IFMT)))
20590+ goto out;
1facf9fc 20591+
4a4d8108 20592+ err = 0;
5afbbe0d 20593+ bindex = au_ibtop(inode);
4a4d8108 20594+ br = au_sbr(sb, bindex);
79b8bda9 20595+ err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
4a4d8108
AM
20596+ if (write_mask
20597+ && !err
20598+ && !special_file(h_inode->i_mode)) {
20599+ /* test whether the upper writable branch exists */
20600+ err = -EROFS;
20601+ for (; bindex >= 0; bindex--)
20602+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
20603+ err = 0;
20604+ break;
20605+ }
20606+ }
20607+ goto out;
20608+ }
dece6358 20609+
4a4d8108 20610+ /* non-write to dir */
1308ab2a 20611+ err = 0;
5afbbe0d
AM
20612+ bbot = au_ibbot(inode);
20613+ for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
4a4d8108
AM
20614+ h_inode = au_h_iptr(inode, bindex);
20615+ if (h_inode) {
20616+ err = au_busy_or_stale();
20617+ if (unlikely(!S_ISDIR(h_inode->i_mode)))
20618+ break;
20619+
20620+ br = au_sbr(sb, bindex);
79b8bda9 20621+ err = h_permission(h_inode, mask, &br->br_path,
4a4d8108
AM
20622+ br->br_perm);
20623+ }
20624+ }
1308ab2a 20625+
4f0767ce 20626+out:
4a4d8108
AM
20627+ ii_read_unlock(inode);
20628+ si_read_unlock(sb);
1308ab2a 20629+ return err;
20630+}
20631+
4a4d8108 20632+/* ---------------------------------------------------------------------- */
1facf9fc 20633+
4a4d8108 20634+static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
b4510431 20635+ unsigned int flags)
4a4d8108
AM
20636+{
20637+ struct dentry *ret, *parent;
b752ccd1 20638+ struct inode *inode;
4a4d8108 20639+ struct super_block *sb;
1716fcea 20640+ int err, npositive;
dece6358 20641+
4a4d8108 20642+ IMustLock(dir);
1308ab2a 20643+
537831f9
AM
20644+ /* todo: support rcu-walk? */
20645+ ret = ERR_PTR(-ECHILD);
20646+ if (flags & LOOKUP_RCU)
20647+ goto out;
20648+
20649+ ret = ERR_PTR(-ENAMETOOLONG);
20650+ if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20651+ goto out;
20652+
4a4d8108 20653+ sb = dir->i_sb;
7f207e10
AM
20654+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
20655+ ret = ERR_PTR(err);
20656+ if (unlikely(err))
20657+ goto out;
20658+
4a4d8108
AM
20659+ err = au_di_init(dentry);
20660+ ret = ERR_PTR(err);
20661+ if (unlikely(err))
7f207e10 20662+ goto out_si;
1308ab2a 20663+
9dbd164d 20664+ inode = NULL;
027c5e7a 20665+ npositive = 0; /* suppress a warning */
4a4d8108
AM
20666+ parent = dentry->d_parent; /* dir inode is locked */
20667+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a
AM
20668+ err = au_alive_dir(parent);
20669+ if (!err)
20670+ err = au_digen_test(parent, au_sigen(sb));
20671+ if (!err) {
5afbbe0d
AM
20672+ /* regardless LOOKUP_CREATE, always ALLOW_NEG */
20673+ npositive = au_lkup_dentry(dentry, au_dbtop(parent),
20674+ AuLkup_ALLOW_NEG);
027c5e7a
AM
20675+ err = npositive;
20676+ }
4a4d8108 20677+ di_read_unlock(parent, AuLock_IR);
4a4d8108
AM
20678+ ret = ERR_PTR(err);
20679+ if (unlikely(err < 0))
20680+ goto out_unlock;
1308ab2a 20681+
4a4d8108 20682+ if (npositive) {
b752ccd1 20683+ inode = au_new_inode(dentry, /*must_new*/0);
c1595e42
JR
20684+ if (IS_ERR(inode)) {
20685+ ret = (void *)inode;
20686+ inode = NULL;
20687+ goto out_unlock;
20688+ }
9dbd164d 20689+ }
4a4d8108 20690+
c1595e42
JR
20691+ if (inode)
20692+ atomic_inc(&inode->i_count);
4a4d8108 20693+ ret = d_splice_alias(inode, dentry);
43982f53 20694+#if 0 /* reserved for future use */
537831f9
AM
20695+ if (unlikely(d_need_lookup(dentry))) {
20696+ spin_lock(&dentry->d_lock);
20697+ dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
20698+ spin_unlock(&dentry->d_lock);
20699+ } else
20700+#endif
c1595e42 20701+ if (inode) {
2000de60 20702+ if (!IS_ERR(ret)) {
c1595e42 20703+ iput(inode);
2000de60
JR
20704+ if (ret && ret != dentry)
20705+ ii_write_unlock(inode);
20706+ } else {
c1595e42
JR
20707+ ii_write_unlock(inode);
20708+ iput(inode);
20709+ inode = NULL;
20710+ }
7f207e10 20711+ }
1facf9fc 20712+
4f0767ce 20713+out_unlock:
4a4d8108 20714+ di_write_unlock(dentry);
7f207e10 20715+out_si:
4a4d8108 20716+ si_read_unlock(sb);
7f207e10 20717+out:
4a4d8108
AM
20718+ return ret;
20719+}
1facf9fc 20720+
4a4d8108 20721+/* ---------------------------------------------------------------------- */
1facf9fc 20722+
acd2b654
AM
20723+/*
20724+ * very dirty and complicated aufs ->atomic_open().
20725+ * aufs_atomic_open()
20726+ * + au_aopen_or_create()
20727+ * + add_simple()
20728+ * + vfsub_atomic_open()
20729+ * + branch fs ->atomic_open()
20730+ * may call the actual 'open' for h_file
20731+ * + inc br_nfiles only if opened
20732+ * + au_aopen_no_open() or au_aopen_do_open()
20733+ *
20734+ * au_aopen_do_open()
20735+ * + finish_open()
20736+ * + au_do_aopen()
20737+ * + au_do_open() the body of all 'open'
20738+ * + au_do_open_nondir()
20739+ * set the passed h_file
20740+ *
20741+ * au_aopen_no_open()
20742+ * + finish_no_open()
20743+ */
20744+
b912730e 20745+struct aopen_node {
8b6a4947 20746+ struct hlist_bl_node hblist;
b912730e
AM
20747+ struct file *file, *h_file;
20748+};
20749+
20750+static int au_do_aopen(struct inode *inode, struct file *file)
20751+{
8b6a4947
AM
20752+ struct hlist_bl_head *aopen;
20753+ struct hlist_bl_node *pos;
b912730e
AM
20754+ struct aopen_node *node;
20755+ struct au_do_open_args args = {
8b6a4947
AM
20756+ .aopen = 1,
20757+ .open = au_do_open_nondir
b912730e
AM
20758+ };
20759+
20760+ aopen = &au_sbi(inode->i_sb)->si_aopen;
8b6a4947
AM
20761+ hlist_bl_lock(aopen);
20762+ hlist_bl_for_each_entry(node, pos, aopen, hblist)
b912730e
AM
20763+ if (node->file == file) {
20764+ args.h_file = node->h_file;
20765+ break;
20766+ }
8b6a4947 20767+ hlist_bl_unlock(aopen);
b912730e
AM
20768+ /* AuDebugOn(!args.h_file); */
20769+
20770+ return au_do_open(file, &args);
20771+}
20772+
acd2b654
AM
20773+static int au_aopen_do_open(struct file *file, struct dentry *dentry,
20774+ struct aopen_node *aopen_node)
20775+{
20776+ int err;
20777+ struct hlist_bl_head *aopen;
20778+
20779+ AuLabel(here);
20780+ aopen = &au_sbi(dentry->d_sb)->si_aopen;
20781+ au_hbl_add(&aopen_node->hblist, aopen);
20782+ err = finish_open(file, dentry, au_do_aopen);
20783+ au_hbl_del(&aopen_node->hblist, aopen);
20784+ /* AuDbgFile(file); */
20785+ AuDbg("%pd%s%s\n", dentry,
20786+ (file->f_mode & FMODE_CREATED) ? " created" : "",
20787+ (file->f_mode & FMODE_OPENED) ? " opened" : "");
20788+
20789+ AuTraceErr(err);
20790+ return err;
20791+}
20792+
20793+static int au_aopen_no_open(struct file *file, struct dentry *dentry)
20794+{
20795+ int err;
20796+
20797+ AuLabel(here);
20798+ dget(dentry);
20799+ err = finish_no_open(file, dentry);
20800+
20801+ AuTraceErr(err);
20802+ return err;
20803+}
20804+
b912730e
AM
20805+static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
20806+ struct file *file, unsigned int open_flag,
acd2b654 20807+ umode_t create_mode)
b912730e 20808+{
acd2b654 20809+ int err, did_open;
5afbbe0d 20810+ unsigned int lkup_flags;
acd2b654
AM
20811+ aufs_bindex_t bindex;
20812+ struct super_block *sb;
f0c0a007 20813+ struct dentry *parent, *d;
b912730e
AM
20814+ struct vfsub_aopen_args args = {
20815+ .open_flag = open_flag,
acd2b654 20816+ .create_mode = create_mode
b912730e
AM
20817+ };
20818+ struct aopen_node aopen_node = {
20819+ .file = file
20820+ };
20821+
20822+ IMustLock(dir);
5afbbe0d 20823+ AuDbg("open_flag 0%o\n", open_flag);
b912730e
AM
20824+ AuDbgDentry(dentry);
20825+
20826+ err = 0;
20827+ if (!au_di(dentry)) {
5afbbe0d
AM
20828+ lkup_flags = LOOKUP_OPEN;
20829+ if (open_flag & O_CREAT)
20830+ lkup_flags |= LOOKUP_CREATE;
20831+ d = aufs_lookup(dir, dentry, lkup_flags);
b912730e
AM
20832+ if (IS_ERR(d)) {
20833+ err = PTR_ERR(d);
5afbbe0d 20834+ AuTraceErr(err);
b912730e
AM
20835+ goto out;
20836+ } else if (d) {
20837+ /*
20838+ * obsoleted dentry found.
20839+ * another error will be returned later.
20840+ */
20841+ d_drop(d);
b912730e 20842+ AuDbgDentry(d);
5afbbe0d 20843+ dput(d);
b912730e
AM
20844+ }
20845+ AuDbgDentry(dentry);
20846+ }
20847+
20848+ if (d_is_positive(dentry)
20849+ || d_unhashed(dentry)
20850+ || d_unlinked(dentry)
acd2b654
AM
20851+ || !(open_flag & O_CREAT)) {
20852+ err = au_aopen_no_open(file, dentry);
20853+ goto out; /* success */
20854+ }
b912730e
AM
20855+
20856+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
20857+ if (unlikely(err))
20858+ goto out;
20859+
acd2b654 20860+ sb = dentry->d_sb;
b912730e
AM
20861+ parent = dentry->d_parent; /* dir is locked */
20862+ di_write_lock_parent(parent);
5afbbe0d 20863+ err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
acd2b654
AM
20864+ if (unlikely(err < 0))
20865+ goto out_parent;
b912730e
AM
20866+
20867+ AuDbgDentry(dentry);
acd2b654
AM
20868+ if (d_is_positive(dentry)) {
20869+ err = au_aopen_no_open(file, dentry);
20870+ goto out_parent; /* success */
20871+ }
b912730e 20872+
acd2b654 20873+ args.file = alloc_empty_file(file->f_flags, current_cred());
b912730e
AM
20874+ err = PTR_ERR(args.file);
20875+ if (IS_ERR(args.file))
acd2b654 20876+ goto out_parent;
b912730e 20877+
acd2b654 20878+ bindex = au_dbtop(dentry);
b912730e
AM
20879+ err = au_aopen_or_create(dir, dentry, &args);
20880+ AuTraceErr(err);
20881+ AuDbgFile(args.file);
acd2b654
AM
20882+ file->f_mode = args.file->f_mode & ~FMODE_OPENED;
20883+ did_open = !!(args.file->f_mode & FMODE_OPENED);
20884+ if (!did_open) {
20885+ fput(args.file);
20886+ args.file = NULL;
b912730e 20887+ }
8b6a4947
AM
20888+ di_write_unlock(parent);
20889+ di_write_unlock(dentry);
acd2b654
AM
20890+ if (unlikely(err < 0)) {
20891+ if (args.file)
20892+ fput(args.file);
20893+ goto out_sb;
b912730e 20894+ }
b912730e 20895+
acd2b654
AM
20896+ if (!did_open)
20897+ err = au_aopen_no_open(file, dentry);
8b6a4947 20898+ else {
acd2b654
AM
20899+ aopen_node.h_file = args.file;
20900+ err = au_aopen_do_open(file, dentry, &aopen_node);
8b6a4947 20901+ }
acd2b654
AM
20902+ if (unlikely(err < 0)) {
20903+ if (args.file)
20904+ fput(args.file);
20905+ if (did_open)
20906+ au_lcnt_dec(&args.br->br_nfiles);
b912730e 20907+ }
acd2b654
AM
20908+ goto out_sb; /* success */
20909+
20910+out_parent:
20911+ di_write_unlock(parent);
20912+ di_write_unlock(dentry);
20913+out_sb:
20914+ si_read_unlock(sb);
b912730e 20915+out:
b912730e 20916+ AuTraceErr(err);
acd2b654 20917+ AuDbgFile(file);
b912730e
AM
20918+ return err;
20919+}
20920+
20921+
20922+/* ---------------------------------------------------------------------- */
20923+
4a4d8108
AM
20924+static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
20925+ const unsigned char add_entry, aufs_bindex_t bcpup,
5afbbe0d 20926+ aufs_bindex_t btop)
4a4d8108
AM
20927+{
20928+ int err;
20929+ struct dentry *h_parent;
20930+ struct inode *h_dir;
1facf9fc 20931+
027c5e7a 20932+ if (add_entry)
5527c038 20933+ IMustLock(d_inode(parent));
027c5e7a 20934+ else
4a4d8108
AM
20935+ di_write_lock_parent(parent);
20936+
20937+ err = 0;
20938+ if (!au_h_dptr(parent, bcpup)) {
5afbbe0d 20939+ if (btop > bcpup)
c2b27bf2 20940+ err = au_cpup_dirs(dentry, bcpup);
5afbbe0d 20941+ else if (btop < bcpup)
4a4d8108
AM
20942+ err = au_cpdown_dirs(dentry, bcpup);
20943+ else
c2b27bf2 20944+ BUG();
4a4d8108 20945+ }
38d290e6 20946+ if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
4a4d8108 20947+ h_parent = au_h_dptr(parent, bcpup);
5527c038 20948+ h_dir = d_inode(h_parent);
be118d29 20949+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
7e9cd9fe 20950+ err = au_lkup_neg(dentry, bcpup, /*wh*/0);
4a4d8108 20951+ /* todo: no unlock here */
3c1bdaff 20952+ inode_unlock_shared(h_dir);
027c5e7a
AM
20953+
20954+ AuDbg("bcpup %d\n", bcpup);
20955+ if (!err) {
5527c038 20956+ if (d_really_is_negative(dentry))
5afbbe0d 20957+ au_set_h_dptr(dentry, btop, NULL);
4a4d8108
AM
20958+ au_update_dbrange(dentry, /*do_put_zero*/0);
20959+ }
1308ab2a 20960+ }
1facf9fc 20961+
4a4d8108
AM
20962+ if (!add_entry)
20963+ di_write_unlock(parent);
20964+ if (!err)
20965+ err = bcpup; /* success */
1308ab2a 20966+
027c5e7a 20967+ AuTraceErr(err);
4a4d8108
AM
20968+ return err;
20969+}
1facf9fc 20970+
4a4d8108
AM
20971+/*
20972+ * decide the branch and the parent dir where we will create a new entry.
20973+ * returns new bindex or an error.
20974+ * copyup the parent dir if needed.
20975+ */
20976+int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
20977+ struct au_wr_dir_args *args)
20978+{
20979+ int err;
392086de 20980+ unsigned int flags;
5afbbe0d 20981+ aufs_bindex_t bcpup, btop, src_btop;
86dc4139
AM
20982+ const unsigned char add_entry
20983+ = au_ftest_wrdir(args->flags, ADD_ENTRY)
38d290e6 20984+ | au_ftest_wrdir(args->flags, TMPFILE);
4a4d8108
AM
20985+ struct super_block *sb;
20986+ struct dentry *parent;
20987+ struct au_sbinfo *sbinfo;
1facf9fc 20988+
4a4d8108
AM
20989+ sb = dentry->d_sb;
20990+ sbinfo = au_sbi(sb);
20991+ parent = dget_parent(dentry);
5afbbe0d
AM
20992+ btop = au_dbtop(dentry);
20993+ bcpup = btop;
4a4d8108
AM
20994+ if (args->force_btgt < 0) {
20995+ if (src_dentry) {
5afbbe0d
AM
20996+ src_btop = au_dbtop(src_dentry);
20997+ if (src_btop < btop)
20998+ bcpup = src_btop;
4a4d8108 20999+ } else if (add_entry) {
392086de
AM
21000+ flags = 0;
21001+ if (au_ftest_wrdir(args->flags, ISDIR))
21002+ au_fset_wbr(flags, DIR);
21003+ err = AuWbrCreate(sbinfo, dentry, flags);
4a4d8108
AM
21004+ bcpup = err;
21005+ }
1facf9fc 21006+
5527c038 21007+ if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
4a4d8108
AM
21008+ if (add_entry)
21009+ err = AuWbrCopyup(sbinfo, dentry);
21010+ else {
21011+ if (!IS_ROOT(dentry)) {
21012+ di_read_lock_parent(parent, !AuLock_IR);
21013+ err = AuWbrCopyup(sbinfo, dentry);
21014+ di_read_unlock(parent, !AuLock_IR);
21015+ } else
21016+ err = AuWbrCopyup(sbinfo, dentry);
21017+ }
21018+ bcpup = err;
21019+ if (unlikely(err < 0))
21020+ goto out;
21021+ }
21022+ } else {
21023+ bcpup = args->force_btgt;
5527c038 21024+ AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
1308ab2a 21025+ }
027c5e7a 21026+
5afbbe0d 21027+ AuDbg("btop %d, bcpup %d\n", btop, bcpup);
4a4d8108 21028+ err = bcpup;
5afbbe0d 21029+ if (bcpup == btop)
4a4d8108 21030+ goto out; /* success */
4a4d8108
AM
21031+
21032+ /* copyup the new parent into the branch we process */
5afbbe0d 21033+ err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
027c5e7a 21034+ if (err >= 0) {
5527c038 21035+ if (d_really_is_negative(dentry)) {
5afbbe0d
AM
21036+ au_set_h_dptr(dentry, btop, NULL);
21037+ au_set_dbtop(dentry, bcpup);
21038+ au_set_dbbot(dentry, bcpup);
027c5e7a 21039+ }
38d290e6
JR
21040+ AuDebugOn(add_entry
21041+ && !au_ftest_wrdir(args->flags, TMPFILE)
21042+ && !au_h_dptr(dentry, bcpup));
027c5e7a 21043+ }
86dc4139
AM
21044+
21045+out:
21046+ dput(parent);
21047+ return err;
21048+}
21049+
21050+/* ---------------------------------------------------------------------- */
21051+
21052+void au_pin_hdir_unlock(struct au_pin *p)
21053+{
21054+ if (p->hdir)
5afbbe0d 21055+ au_hn_inode_unlock(p->hdir);
86dc4139
AM
21056+}
21057+
c1595e42 21058+int au_pin_hdir_lock(struct au_pin *p)
86dc4139
AM
21059+{
21060+ int err;
21061+
21062+ err = 0;
21063+ if (!p->hdir)
21064+ goto out;
21065+
21066+ /* even if an error happens later, keep this lock */
5afbbe0d 21067+ au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
86dc4139
AM
21068+
21069+ err = -EBUSY;
5527c038 21070+ if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
86dc4139
AM
21071+ goto out;
21072+
21073+ err = 0;
21074+ if (p->h_dentry)
21075+ err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
21076+ p->h_parent, p->br);
21077+
21078+out:
21079+ return err;
21080+}
21081+
21082+int au_pin_hdir_relock(struct au_pin *p)
21083+{
21084+ int err, i;
21085+ struct inode *h_i;
21086+ struct dentry *h_d[] = {
21087+ p->h_dentry,
21088+ p->h_parent
21089+ };
21090+
21091+ err = au_pin_hdir_lock(p);
21092+ if (unlikely(err))
21093+ goto out;
21094+
21095+ for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
21096+ if (!h_d[i])
21097+ continue;
5527c038
JR
21098+ if (d_is_positive(h_d[i])) {
21099+ h_i = d_inode(h_d[i]);
86dc4139 21100+ err = !h_i->i_nlink;
5527c038 21101+ }
86dc4139
AM
21102+ }
21103+
21104+out:
21105+ return err;
21106+}
21107+
5afbbe0d 21108+static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
86dc4139 21109+{
eca34b5c 21110+ atomic_long_set(&p->hdir->hi_inode->i_rwsem.owner, (long)task);
86dc4139
AM
21111+}
21112+
21113+void au_pin_hdir_acquire_nest(struct au_pin *p)
21114+{
21115+ if (p->hdir) {
5afbbe0d 21116+ rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
86dc4139
AM
21117+ p->lsc_hi, 0, NULL, _RET_IP_);
21118+ au_pin_hdir_set_owner(p, current);
21119+ }
dece6358 21120+}
1facf9fc 21121+
86dc4139
AM
21122+void au_pin_hdir_release(struct au_pin *p)
21123+{
21124+ if (p->hdir) {
21125+ au_pin_hdir_set_owner(p, p->task);
d58c55f2 21126+ rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, _RET_IP_);
86dc4139
AM
21127+ }
21128+}
1308ab2a 21129+
4a4d8108 21130+struct dentry *au_pinned_h_parent(struct au_pin *pin)
1308ab2a 21131+{
4a4d8108
AM
21132+ if (pin && pin->parent)
21133+ return au_h_dptr(pin->parent, pin->bindex);
21134+ return NULL;
dece6358 21135+}
1facf9fc 21136+
4a4d8108 21137+void au_unpin(struct au_pin *p)
dece6358 21138+{
86dc4139
AM
21139+ if (p->hdir)
21140+ au_pin_hdir_unlock(p);
e49829fe 21141+ if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
b4510431 21142+ vfsub_mnt_drop_write(p->h_mnt);
4a4d8108
AM
21143+ if (!p->hdir)
21144+ return;
1facf9fc 21145+
4a4d8108
AM
21146+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21147+ di_read_unlock(p->parent, AuLock_IR);
21148+ iput(p->hdir->hi_inode);
21149+ dput(p->parent);
21150+ p->parent = NULL;
21151+ p->hdir = NULL;
21152+ p->h_mnt = NULL;
86dc4139 21153+ /* do not clear p->task */
4a4d8108 21154+}
1308ab2a 21155+
4a4d8108
AM
21156+int au_do_pin(struct au_pin *p)
21157+{
21158+ int err;
21159+ struct super_block *sb;
4a4d8108
AM
21160+ struct inode *h_dir;
21161+
21162+ err = 0;
21163+ sb = p->dentry->d_sb;
86dc4139 21164+ p->br = au_sbr(sb, p->bindex);
4a4d8108
AM
21165+ if (IS_ROOT(p->dentry)) {
21166+ if (au_ftest_pin(p->flags, MNT_WRITE)) {
86dc4139 21167+ p->h_mnt = au_br_mnt(p->br);
b4510431 21168+ err = vfsub_mnt_want_write(p->h_mnt);
4a4d8108
AM
21169+ if (unlikely(err)) {
21170+ au_fclr_pin(p->flags, MNT_WRITE);
21171+ goto out_err;
21172+ }
21173+ }
dece6358 21174+ goto out;
1facf9fc 21175+ }
21176+
86dc4139 21177+ p->h_dentry = NULL;
5afbbe0d 21178+ if (p->bindex <= au_dbbot(p->dentry))
86dc4139 21179+ p->h_dentry = au_h_dptr(p->dentry, p->bindex);
dece6358 21180+
4a4d8108
AM
21181+ p->parent = dget_parent(p->dentry);
21182+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21183+ di_read_lock(p->parent, AuLock_IR, p->lsc_di);
dece6358 21184+
4a4d8108 21185+ h_dir = NULL;
86dc4139 21186+ p->h_parent = au_h_dptr(p->parent, p->bindex);
5527c038 21187+ p->hdir = au_hi(d_inode(p->parent), p->bindex);
4a4d8108
AM
21188+ if (p->hdir)
21189+ h_dir = p->hdir->hi_inode;
dece6358 21190+
b752ccd1
AM
21191+ /*
21192+ * udba case, or
21193+ * if DI_LOCKED is not set, then p->parent may be different
21194+ * and h_parent can be NULL.
21195+ */
86dc4139 21196+ if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
e49829fe 21197+ err = -EBUSY;
4a4d8108
AM
21198+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21199+ di_read_unlock(p->parent, AuLock_IR);
21200+ dput(p->parent);
21201+ p->parent = NULL;
21202+ goto out_err;
21203+ }
1308ab2a 21204+
4a4d8108 21205+ if (au_ftest_pin(p->flags, MNT_WRITE)) {
86dc4139 21206+ p->h_mnt = au_br_mnt(p->br);
b4510431 21207+ err = vfsub_mnt_want_write(p->h_mnt);
dece6358 21208+ if (unlikely(err)) {
4a4d8108 21209+ au_fclr_pin(p->flags, MNT_WRITE);
86dc4139
AM
21210+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21211+ di_read_unlock(p->parent, AuLock_IR);
21212+ dput(p->parent);
21213+ p->parent = NULL;
21214+ goto out_err;
dece6358
AM
21215+ }
21216+ }
4a4d8108 21217+
86dc4139
AM
21218+ au_igrab(h_dir);
21219+ err = au_pin_hdir_lock(p);
21220+ if (!err)
21221+ goto out; /* success */
21222+
076b876e
AM
21223+ au_unpin(p);
21224+
4f0767ce 21225+out_err:
4a4d8108
AM
21226+ pr_err("err %d\n", err);
21227+ err = au_busy_or_stale();
4f0767ce 21228+out:
1facf9fc 21229+ return err;
21230+}
21231+
4a4d8108
AM
21232+void au_pin_init(struct au_pin *p, struct dentry *dentry,
21233+ aufs_bindex_t bindex, int lsc_di, int lsc_hi,
21234+ unsigned int udba, unsigned char flags)
21235+{
21236+ p->dentry = dentry;
21237+ p->udba = udba;
21238+ p->lsc_di = lsc_di;
21239+ p->lsc_hi = lsc_hi;
21240+ p->flags = flags;
21241+ p->bindex = bindex;
21242+
21243+ p->parent = NULL;
21244+ p->hdir = NULL;
21245+ p->h_mnt = NULL;
86dc4139
AM
21246+
21247+ p->h_dentry = NULL;
21248+ p->h_parent = NULL;
21249+ p->br = NULL;
21250+ p->task = current;
4a4d8108
AM
21251+}
21252+
21253+int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
21254+ unsigned int udba, unsigned char flags)
21255+{
21256+ au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
21257+ udba, flags);
21258+ return au_do_pin(pin);
21259+}
21260+
dece6358
AM
21261+/* ---------------------------------------------------------------------- */
21262+
1308ab2a 21263+/*
4a4d8108
AM
21264+ * ->setattr() and ->getattr() are called in various cases.
21265+ * chmod, stat: dentry is revalidated.
21266+ * fchmod, fstat: file and dentry are not revalidated, additionally they may be
21267+ * unhashed.
21268+ * for ->setattr(), ia->ia_file is passed from ftruncate only.
1308ab2a 21269+ */
027c5e7a 21270+/* todo: consolidate with do_refresh() and simple_reval_dpath() */
c1595e42 21271+int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
1facf9fc 21272+{
4a4d8108 21273+ int err;
4a4d8108 21274+ struct dentry *parent;
1facf9fc 21275+
1308ab2a 21276+ err = 0;
027c5e7a 21277+ if (au_digen_test(dentry, sigen)) {
4a4d8108
AM
21278+ parent = dget_parent(dentry);
21279+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 21280+ err = au_refresh_dentry(dentry, parent);
4a4d8108
AM
21281+ di_read_unlock(parent, AuLock_IR);
21282+ dput(parent);
dece6358 21283+ }
1facf9fc 21284+
4a4d8108 21285+ AuTraceErr(err);
1308ab2a 21286+ return err;
21287+}
dece6358 21288+
c1595e42
JR
21289+int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
21290+ struct au_icpup_args *a)
1308ab2a 21291+{
21292+ int err;
4a4d8108 21293+ loff_t sz;
5afbbe0d 21294+ aufs_bindex_t btop, ibtop;
4a4d8108
AM
21295+ struct dentry *hi_wh, *parent;
21296+ struct inode *inode;
4a4d8108
AM
21297+ struct au_wr_dir_args wr_dir_args = {
21298+ .force_btgt = -1,
21299+ .flags = 0
21300+ };
21301+
2000de60 21302+ if (d_is_dir(dentry))
4a4d8108
AM
21303+ au_fset_wrdir(wr_dir_args.flags, ISDIR);
21304+ /* plink or hi_wh() case */
5afbbe0d 21305+ btop = au_dbtop(dentry);
5527c038 21306+ inode = d_inode(dentry);
5afbbe0d
AM
21307+ ibtop = au_ibtop(inode);
21308+ if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
21309+ wr_dir_args.force_btgt = ibtop;
4a4d8108
AM
21310+ err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
21311+ if (unlikely(err < 0))
21312+ goto out;
21313+ a->btgt = err;
5afbbe0d 21314+ if (err != btop)
4a4d8108
AM
21315+ au_fset_icpup(a->flags, DID_CPUP);
21316+
21317+ err = 0;
21318+ a->pin_flags = AuPin_MNT_WRITE;
21319+ parent = NULL;
21320+ if (!IS_ROOT(dentry)) {
21321+ au_fset_pin(a->pin_flags, DI_LOCKED);
21322+ parent = dget_parent(dentry);
21323+ di_write_lock_parent(parent);
21324+ }
21325+
21326+ err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
21327+ if (unlikely(err))
21328+ goto out_parent;
21329+
4a4d8108 21330+ sz = -1;
5afbbe0d 21331+ a->h_path.dentry = au_h_dptr(dentry, btop);
5527c038 21332+ a->h_inode = d_inode(a->h_path.dentry);
c1595e42 21333+ if (ia && (ia->ia_valid & ATTR_SIZE)) {
be118d29 21334+ inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
c1595e42
JR
21335+ if (ia->ia_size < i_size_read(a->h_inode))
21336+ sz = ia->ia_size;
3c1bdaff 21337+ inode_unlock_shared(a->h_inode);
c1595e42 21338+ }
4a4d8108 21339+
4a4d8108 21340+ hi_wh = NULL;
027c5e7a 21341+ if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
4a4d8108
AM
21342+ hi_wh = au_hi_wh(inode, a->btgt);
21343+ if (!hi_wh) {
c2b27bf2
AM
21344+ struct au_cp_generic cpg = {
21345+ .dentry = dentry,
21346+ .bdst = a->btgt,
21347+ .bsrc = -1,
21348+ .len = sz,
21349+ .pin = &a->pin
21350+ };
21351+ err = au_sio_cpup_wh(&cpg, /*file*/NULL);
4a4d8108
AM
21352+ if (unlikely(err))
21353+ goto out_unlock;
21354+ hi_wh = au_hi_wh(inode, a->btgt);
21355+ /* todo: revalidate hi_wh? */
21356+ }
21357+ }
21358+
21359+ if (parent) {
21360+ au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
21361+ di_downgrade_lock(parent, AuLock_IR);
21362+ dput(parent);
21363+ parent = NULL;
21364+ }
21365+ if (!au_ftest_icpup(a->flags, DID_CPUP))
21366+ goto out; /* success */
21367+
21368+ if (!d_unhashed(dentry)) {
c2b27bf2
AM
21369+ struct au_cp_generic cpg = {
21370+ .dentry = dentry,
21371+ .bdst = a->btgt,
5afbbe0d 21372+ .bsrc = btop,
c2b27bf2
AM
21373+ .len = sz,
21374+ .pin = &a->pin,
21375+ .flags = AuCpup_DTIME | AuCpup_HOPEN
21376+ };
21377+ err = au_sio_cpup_simple(&cpg);
4a4d8108
AM
21378+ if (!err)
21379+ a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21380+ } else if (!hi_wh)
21381+ a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21382+ else
21383+ a->h_path.dentry = hi_wh; /* do not dget here */
1308ab2a 21384+
4f0767ce 21385+out_unlock:
5527c038 21386+ a->h_inode = d_inode(a->h_path.dentry);
86dc4139 21387+ if (!err)
dece6358 21388+ goto out; /* success */
4a4d8108 21389+ au_unpin(&a->pin);
4f0767ce 21390+out_parent:
4a4d8108
AM
21391+ if (parent) {
21392+ di_write_unlock(parent);
21393+ dput(parent);
21394+ }
4f0767ce 21395+out:
86dc4139 21396+ if (!err)
febd17d6 21397+ inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
1facf9fc 21398+ return err;
21399+}
21400+
0b2a12c6
JR
21401+static int aufs_setattr(struct user_namespace *userns, struct dentry *dentry,
21402+ struct iattr *ia)
1facf9fc 21403+{
4a4d8108 21404+ int err;
523b37e3 21405+ struct inode *inode, *delegated;
4a4d8108
AM
21406+ struct super_block *sb;
21407+ struct file *file;
21408+ struct au_icpup_args *a;
0b2a12c6 21409+ struct user_namespace *h_userns;
1facf9fc 21410+
5527c038 21411+ inode = d_inode(dentry);
4a4d8108 21412+ IMustLock(inode);
dece6358 21413+
0b2a12c6 21414+ err = setattr_prepare(userns, dentry, ia);
f2c43d5f
AM
21415+ if (unlikely(err))
21416+ goto out;
21417+
4a4d8108
AM
21418+ err = -ENOMEM;
21419+ a = kzalloc(sizeof(*a), GFP_NOFS);
21420+ if (unlikely(!a))
21421+ goto out;
1facf9fc 21422+
4a4d8108
AM
21423+ if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
21424+ ia->ia_valid &= ~ATTR_MODE;
dece6358 21425+
4a4d8108
AM
21426+ file = NULL;
21427+ sb = dentry->d_sb;
e49829fe
JR
21428+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21429+ if (unlikely(err))
21430+ goto out_kfree;
21431+
4a4d8108
AM
21432+ if (ia->ia_valid & ATTR_FILE) {
21433+ /* currently ftruncate(2) only */
7e9cd9fe 21434+ AuDebugOn(!d_is_reg(dentry));
4a4d8108 21435+ file = ia->ia_file;
521ced18
JR
21436+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
21437+ /*fi_lsc*/0);
4a4d8108
AM
21438+ if (unlikely(err))
21439+ goto out_si;
21440+ ia->ia_file = au_hf_top(file);
21441+ a->udba = AuOpt_UDBA_NONE;
21442+ } else {
21443+ /* fchmod() doesn't pass ia_file */
21444+ a->udba = au_opt_udba(sb);
027c5e7a
AM
21445+ di_write_lock_child(dentry);
21446+ /* no d_unlinked(), to set UDBA_NONE for root */
4a4d8108
AM
21447+ if (d_unhashed(dentry))
21448+ a->udba = AuOpt_UDBA_NONE;
4a4d8108
AM
21449+ if (a->udba != AuOpt_UDBA_NONE) {
21450+ AuDebugOn(IS_ROOT(dentry));
21451+ err = au_reval_for_attr(dentry, au_sigen(sb));
21452+ if (unlikely(err))
21453+ goto out_dentry;
21454+ }
dece6358 21455+ }
dece6358 21456+
4a4d8108
AM
21457+ err = au_pin_and_icpup(dentry, ia, a);
21458+ if (unlikely(err < 0))
21459+ goto out_dentry;
21460+ if (au_ftest_icpup(a->flags, DID_CPUP)) {
21461+ ia->ia_file = NULL;
21462+ ia->ia_valid &= ~ATTR_FILE;
1308ab2a 21463+ }
dece6358 21464+
4a4d8108
AM
21465+ a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
21466+ if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
21467+ == (ATTR_MODE | ATTR_CTIME)) {
7eafdf33 21468+ err = security_path_chmod(&a->h_path, ia->ia_mode);
4a4d8108
AM
21469+ if (unlikely(err))
21470+ goto out_unlock;
21471+ } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
21472+ && (ia->ia_valid & ATTR_CTIME)) {
86dc4139 21473+ err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
4a4d8108
AM
21474+ if (unlikely(err))
21475+ goto out_unlock;
21476+ }
dece6358 21477+
4a4d8108
AM
21478+ if (ia->ia_valid & ATTR_SIZE) {
21479+ struct file *f;
1308ab2a 21480+
953406b4 21481+ if (ia->ia_size < i_size_read(inode))
4a4d8108 21482+ /* unmap only */
953406b4 21483+ truncate_setsize(inode, ia->ia_size);
1308ab2a 21484+
4a4d8108
AM
21485+ f = NULL;
21486+ if (ia->ia_valid & ATTR_FILE)
21487+ f = ia->ia_file;
febd17d6 21488+ inode_unlock(a->h_inode);
4a4d8108 21489+ err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
febd17d6 21490+ inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
523b37e3
AM
21491+ } else {
21492+ delegated = NULL;
21493+ while (1) {
21494+ err = vfsub_notify_change(&a->h_path, ia, &delegated);
21495+ if (delegated) {
21496+ err = break_deleg_wait(&delegated);
21497+ if (!err)
21498+ continue;
21499+ }
21500+ break;
21501+ }
21502+ }
8cdd5066
JR
21503+ /*
21504+ * regardless aufs 'acl' option setting.
21505+ * why don't all acl-aware fs call this func from their ->setattr()?
21506+ */
0b2a12c6
JR
21507+ if (!err && (ia->ia_valid & ATTR_MODE)) {
21508+ h_userns = mnt_user_ns(a->h_path.mnt);
21509+ err = vfsub_acl_chmod(h_userns, a->h_inode, ia->ia_mode);
21510+ }
4a4d8108
AM
21511+ if (!err)
21512+ au_cpup_attr_changeable(inode);
1308ab2a 21513+
4f0767ce 21514+out_unlock:
febd17d6 21515+ inode_unlock(a->h_inode);
4a4d8108 21516+ au_unpin(&a->pin);
027c5e7a 21517+ if (unlikely(err))
5afbbe0d 21518+ au_update_dbtop(dentry);
4f0767ce 21519+out_dentry:
4a4d8108
AM
21520+ di_write_unlock(dentry);
21521+ if (file) {
21522+ fi_write_unlock(file);
21523+ ia->ia_file = file;
21524+ ia->ia_valid |= ATTR_FILE;
21525+ }
4f0767ce 21526+out_si:
4a4d8108 21527+ si_read_unlock(sb);
e49829fe 21528+out_kfree:
9f237c51 21529+ au_kfree_rcu(a);
4f0767ce 21530+out:
4a4d8108
AM
21531+ AuTraceErr(err);
21532+ return err;
1facf9fc 21533+}
21534+
c1595e42
JR
21535+#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
21536+static int au_h_path_to_set_attr(struct dentry *dentry,
21537+ struct au_icpup_args *a, struct path *h_path)
21538+{
21539+ int err;
21540+ struct super_block *sb;
21541+
21542+ sb = dentry->d_sb;
21543+ a->udba = au_opt_udba(sb);
21544+ /* no d_unlinked(), to set UDBA_NONE for root */
21545+ if (d_unhashed(dentry))
21546+ a->udba = AuOpt_UDBA_NONE;
21547+ if (a->udba != AuOpt_UDBA_NONE) {
21548+ AuDebugOn(IS_ROOT(dentry));
21549+ err = au_reval_for_attr(dentry, au_sigen(sb));
21550+ if (unlikely(err))
21551+ goto out;
21552+ }
21553+ err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
21554+ if (unlikely(err < 0))
21555+ goto out;
21556+
21557+ h_path->dentry = a->h_path.dentry;
21558+ h_path->mnt = au_sbr_mnt(sb, a->btgt);
21559+
21560+out:
21561+ return err;
21562+}
21563+
f2c43d5f
AM
21564+ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
21565+ struct au_sxattr *arg)
c1595e42
JR
21566+{
21567+ int err;
21568+ struct path h_path;
21569+ struct super_block *sb;
21570+ struct au_icpup_args *a;
5afbbe0d 21571+ struct inode *h_inode;
0b2a12c6 21572+ struct user_namespace *h_userns;
c1595e42 21573+
c1595e42
JR
21574+ IMustLock(inode);
21575+
21576+ err = -ENOMEM;
21577+ a = kzalloc(sizeof(*a), GFP_NOFS);
21578+ if (unlikely(!a))
21579+ goto out;
21580+
21581+ sb = dentry->d_sb;
21582+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21583+ if (unlikely(err))
21584+ goto out_kfree;
21585+
21586+ h_path.dentry = NULL; /* silence gcc */
21587+ di_write_lock_child(dentry);
21588+ err = au_h_path_to_set_attr(dentry, a, &h_path);
21589+ if (unlikely(err))
21590+ goto out_di;
0b2a12c6 21591+ h_userns = mnt_user_ns(h_path.mnt);
c1595e42 21592+
febd17d6 21593+ inode_unlock(a->h_inode);
c1595e42
JR
21594+ switch (arg->type) {
21595+ case AU_XATTR_SET:
5afbbe0d 21596+ AuDebugOn(d_is_negative(h_path.dentry));
0b2a12c6 21597+ err = vfsub_setxattr(h_userns, h_path.dentry,
c1595e42
JR
21598+ arg->u.set.name, arg->u.set.value,
21599+ arg->u.set.size, arg->u.set.flags);
21600+ break;
c1595e42
JR
21601+ case AU_ACL_SET:
21602+ err = -EOPNOTSUPP;
5527c038 21603+ h_inode = d_inode(h_path.dentry);
0b2a12c6 21604+ if (h_inode->i_op->set_acl) {
f2c43d5f 21605+ /* this will call posix_acl_update_mode */
0b2a12c6 21606+ err = h_inode->i_op->set_acl(h_userns, h_inode,
c1595e42
JR
21607+ arg->u.acl_set.acl,
21608+ arg->u.acl_set.type);
0b2a12c6 21609+ }
c1595e42
JR
21610+ break;
21611+ }
21612+ if (!err)
21613+ au_cpup_attr_timesizes(inode);
21614+
21615+ au_unpin(&a->pin);
21616+ if (unlikely(err))
5afbbe0d 21617+ au_update_dbtop(dentry);
c1595e42
JR
21618+
21619+out_di:
21620+ di_write_unlock(dentry);
21621+ si_read_unlock(sb);
21622+out_kfree:
9f237c51 21623+ au_kfree_rcu(a);
c1595e42
JR
21624+out:
21625+ AuTraceErr(err);
21626+ return err;
21627+}
21628+#endif
21629+
4a4d8108
AM
21630+static void au_refresh_iattr(struct inode *inode, struct kstat *st,
21631+ unsigned int nlink)
1facf9fc 21632+{
9dbd164d
AM
21633+ unsigned int n;
21634+
4a4d8108 21635+ inode->i_mode = st->mode;
86dc4139
AM
21636+ /* don't i_[ug]id_write() here */
21637+ inode->i_uid = st->uid;
21638+ inode->i_gid = st->gid;
4a4d8108
AM
21639+ inode->i_atime = st->atime;
21640+ inode->i_mtime = st->mtime;
21641+ inode->i_ctime = st->ctime;
1facf9fc 21642+
4a4d8108
AM
21643+ au_cpup_attr_nlink(inode, /*force*/0);
21644+ if (S_ISDIR(inode->i_mode)) {
9dbd164d
AM
21645+ n = inode->i_nlink;
21646+ n -= nlink;
21647+ n += st->nlink;
f6b6e03d 21648+ smp_mb(); /* for i_nlink */
7eafdf33 21649+ /* 0 can happen */
92d182d2 21650+ set_nlink(inode, n);
4a4d8108 21651+ }
1facf9fc 21652+
4a4d8108
AM
21653+ spin_lock(&inode->i_lock);
21654+ inode->i_blocks = st->blocks;
21655+ i_size_write(inode, st->size);
21656+ spin_unlock(&inode->i_lock);
1facf9fc 21657+}
21658+
c1595e42 21659+/*
f2c43d5f 21660+ * common routine for aufs_getattr() and au_getxattr().
c1595e42
JR
21661+ * returns zero or negative (an error).
21662+ * @dentry will be read-locked in success.
21663+ */
42b5c33a
AM
21664+int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
21665+ struct path *h_path, int locked)
1facf9fc 21666+{
4a4d8108 21667+ int err;
076b876e 21668+ unsigned int mnt_flags, sigen;
c1595e42 21669+ unsigned char udba_none;
4a4d8108 21670+ aufs_bindex_t bindex;
4a4d8108 21671+ struct super_block *sb, *h_sb;
1facf9fc 21672+
c1595e42
JR
21673+ h_path->mnt = NULL;
21674+ h_path->dentry = NULL;
21675+
21676+ err = 0;
4a4d8108 21677+ sb = dentry->d_sb;
4a4d8108
AM
21678+ mnt_flags = au_mntflags(sb);
21679+ udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
1facf9fc 21680+
a2654f78
AM
21681+ if (unlikely(locked))
21682+ goto body; /* skip locking dinfo */
21683+
4a4d8108 21684+ /* support fstat(2) */
027c5e7a 21685+ if (!d_unlinked(dentry) && !udba_none) {
076b876e 21686+ sigen = au_sigen(sb);
027c5e7a
AM
21687+ err = au_digen_test(dentry, sigen);
21688+ if (!err) {
4a4d8108 21689+ di_read_lock_child(dentry, AuLock_IR);
027c5e7a 21690+ err = au_dbrange_test(dentry);
c1595e42
JR
21691+ if (unlikely(err)) {
21692+ di_read_unlock(dentry, AuLock_IR);
21693+ goto out;
21694+ }
027c5e7a 21695+ } else {
4a4d8108
AM
21696+ AuDebugOn(IS_ROOT(dentry));
21697+ di_write_lock_child(dentry);
027c5e7a
AM
21698+ err = au_dbrange_test(dentry);
21699+ if (!err)
21700+ err = au_reval_for_attr(dentry, sigen);
c1595e42
JR
21701+ if (!err)
21702+ di_downgrade_lock(dentry, AuLock_IR);
21703+ else {
21704+ di_write_unlock(dentry);
21705+ goto out;
21706+ }
4a4d8108
AM
21707+ }
21708+ } else
21709+ di_read_lock_child(dentry, AuLock_IR);
1facf9fc 21710+
a2654f78 21711+body:
42b5c33a
AM
21712+ if (!inode) {
21713+ inode = d_inode(dentry);
21714+ if (unlikely(!inode))
21715+ goto out;
21716+ }
5afbbe0d 21717+ bindex = au_ibtop(inode);
c1595e42
JR
21718+ h_path->mnt = au_sbr_mnt(sb, bindex);
21719+ h_sb = h_path->mnt->mnt_sb;
21720+ if (!force
21721+ && !au_test_fs_bad_iattr(h_sb)
21722+ && udba_none)
21723+ goto out; /* success */
1facf9fc 21724+
5afbbe0d 21725+ if (au_dbtop(dentry) == bindex)
c1595e42 21726+ h_path->dentry = au_h_dptr(dentry, bindex);
4a4d8108 21727+ else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
c1595e42
JR
21728+ h_path->dentry = au_plink_lkup(inode, bindex);
21729+ if (IS_ERR(h_path->dentry))
21730+ /* pretending success */
21731+ h_path->dentry = NULL;
21732+ else
21733+ dput(h_path->dentry);
4a4d8108 21734+ }
c1595e42
JR
21735+
21736+out:
21737+ return err;
21738+}
21739+
0b2a12c6
JR
21740+static int aufs_getattr(struct user_namespace *userns, const struct path *path,
21741+ struct kstat *st, u32 request, unsigned int query)
c1595e42
JR
21742+{
21743+ int err;
21744+ unsigned char positive;
21745+ struct path h_path;
521ced18 21746+ struct dentry *dentry;
c1595e42
JR
21747+ struct inode *inode;
21748+ struct super_block *sb;
21749+
521ced18 21750+ dentry = path->dentry;
5527c038 21751+ inode = d_inode(dentry);
c1595e42
JR
21752+ sb = dentry->d_sb;
21753+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21754+ if (unlikely(err))
21755+ goto out;
42b5c33a
AM
21756+ err = au_h_path_getattr(dentry, /*inode*/NULL, /*force*/0, &h_path,
21757+ /*locked*/0);
c1595e42
JR
21758+ if (unlikely(err))
21759+ goto out_si;
c06a8ce3 21760+ if (unlikely(!h_path.dentry))
c1595e42 21761+ /* illegally overlapped or something */
4a4d8108
AM
21762+ goto out_fill; /* pretending success */
21763+
5527c038 21764+ positive = d_is_positive(h_path.dentry);
4a4d8108 21765+ if (positive)
521ced18
JR
21766+ /* no vfsub version */
21767+ err = vfs_getattr(&h_path, st, request, query);
4a4d8108
AM
21768+ if (!err) {
21769+ if (positive)
c06a8ce3 21770+ au_refresh_iattr(inode, st,
5527c038 21771+ d_inode(h_path.dentry)->i_nlink);
4a4d8108 21772+ goto out_fill; /* success */
1facf9fc 21773+ }
7f207e10 21774+ AuTraceErr(err);
c1595e42 21775+ goto out_di;
4a4d8108 21776+
4f0767ce 21777+out_fill:
0b2a12c6 21778+ generic_fillattr(userns, inode, st);
c1595e42 21779+out_di:
4a4d8108 21780+ di_read_unlock(dentry, AuLock_IR);
c1595e42 21781+out_si:
4a4d8108 21782+ si_read_unlock(sb);
7f207e10
AM
21783+out:
21784+ AuTraceErr(err);
4a4d8108 21785+ return err;
1facf9fc 21786+}
21787+
21788+/* ---------------------------------------------------------------------- */
21789+
febd17d6
JR
21790+static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
21791+ struct delayed_call *done)
4a4d8108 21792+{
c2c0f25c 21793+ const char *ret;
c2c0f25c 21794+ struct dentry *h_dentry;
febd17d6 21795+ struct inode *h_inode;
4a4d8108 21796+ int err;
c2c0f25c 21797+ aufs_bindex_t bindex;
1facf9fc 21798+
79b8bda9 21799+ ret = NULL; /* suppress a warning */
febd17d6
JR
21800+ err = -ECHILD;
21801+ if (!dentry)
21802+ goto out;
21803+
027c5e7a
AM
21804+ err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
21805+ if (unlikely(err))
c2c0f25c 21806+ goto out;
027c5e7a
AM
21807+
21808+ err = au_d_hashed_positive(dentry);
c2c0f25c
AM
21809+ if (unlikely(err))
21810+ goto out_unlock;
21811+
21812+ err = -EINVAL;
21813+ inode = d_inode(dentry);
5afbbe0d 21814+ bindex = au_ibtop(inode);
c2c0f25c 21815+ h_inode = au_h_iptr(inode, bindex);
febd17d6 21816+ if (unlikely(!h_inode->i_op->get_link))
c2c0f25c
AM
21817+ goto out_unlock;
21818+
21819+ err = -EBUSY;
21820+ h_dentry = NULL;
5afbbe0d 21821+ if (au_dbtop(dentry) <= bindex) {
c2c0f25c
AM
21822+ h_dentry = au_h_dptr(dentry, bindex);
21823+ if (h_dentry)
21824+ dget(h_dentry);
027c5e7a 21825+ }
c2c0f25c
AM
21826+ if (!h_dentry) {
21827+ h_dentry = d_find_any_alias(h_inode);
21828+ if (IS_ERR(h_dentry)) {
21829+ err = PTR_ERR(h_dentry);
febd17d6 21830+ goto out_unlock;
c2c0f25c
AM
21831+ }
21832+ }
21833+ if (unlikely(!h_dentry))
febd17d6 21834+ goto out_unlock;
1facf9fc 21835+
c2c0f25c 21836+ err = 0;
062440b3 21837+ AuDbg("%ps\n", h_inode->i_op->get_link);
c2c0f25c 21838+ AuDbgDentry(h_dentry);
f2c43d5f 21839+ ret = vfs_get_link(h_dentry, done);
c2c0f25c 21840+ dput(h_dentry);
febd17d6
JR
21841+ if (IS_ERR(ret))
21842+ err = PTR_ERR(ret);
c2c0f25c 21843+
c2c0f25c
AM
21844+out_unlock:
21845+ aufs_read_unlock(dentry, AuLock_IR);
4f0767ce 21846+out:
c2c0f25c
AM
21847+ if (unlikely(err))
21848+ ret = ERR_PTR(err);
21849+ AuTraceErrPtr(ret);
21850+ return ret;
4a4d8108 21851+}
1facf9fc 21852+
4a4d8108 21853+/* ---------------------------------------------------------------------- */
1facf9fc 21854+
e2f27e51
AM
21855+static int au_is_special(struct inode *inode)
21856+{
21857+ return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
21858+}
21859+
acd2b654
AM
21860+static int aufs_update_time(struct inode *inode, struct timespec64 *ts,
21861+ int flags)
4a4d8108 21862+{
0c3ec466 21863+ int err;
e2f27e51 21864+ aufs_bindex_t bindex;
0c3ec466
AM
21865+ struct super_block *sb;
21866+ struct inode *h_inode;
e2f27e51 21867+ struct vfsmount *h_mnt;
0c3ec466
AM
21868+
21869+ sb = inode->i_sb;
e2f27e51
AM
21870+ WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
21871+ "unexpected s_flags 0x%lx", sb->s_flags);
21872+
0c3ec466
AM
21873+ /* mmap_sem might be acquired already, cf. aufs_mmap() */
21874+ lockdep_off();
21875+ si_read_lock(sb, AuLock_FLUSH);
21876+ ii_write_lock_child(inode);
e2f27e51
AM
21877+
21878+ err = 0;
21879+ bindex = au_ibtop(inode);
21880+ h_inode = au_h_iptr(inode, bindex);
21881+ if (!au_test_ro(sb, bindex, inode)) {
21882+ h_mnt = au_sbr_mnt(sb, bindex);
21883+ err = vfsub_mnt_want_write(h_mnt);
21884+ if (!err) {
21885+ err = vfsub_update_time(h_inode, ts, flags);
21886+ vfsub_mnt_drop_write(h_mnt);
21887+ }
21888+ } else if (au_is_special(h_inode)) {
21889+ /*
21890+ * Never copy-up here.
21891+ * These special files may already be opened and used for
21892+ * communicating. If we copied it up, then the communication
21893+ * would be corrupted.
21894+ */
21895+ AuWarn1("timestamps for i%lu are ignored "
21896+ "since it is on readonly branch (hi%lu).\n",
21897+ inode->i_ino, h_inode->i_ino);
21898+ } else if (flags & ~S_ATIME) {
21899+ err = -EIO;
21900+ AuIOErr1("unexpected flags 0x%x\n", flags);
21901+ AuDebugOn(1);
21902+ }
21903+
38d290e6
JR
21904+ if (!err)
21905+ au_cpup_attr_timesizes(inode);
0c3ec466
AM
21906+ ii_write_unlock(inode);
21907+ si_read_unlock(sb);
21908+ lockdep_on();
38d290e6
JR
21909+
21910+ if (!err && (flags & S_VERSION))
21911+ inode_inc_iversion(inode);
21912+
0c3ec466 21913+ return err;
4a4d8108 21914+}
1facf9fc 21915+
4a4d8108 21916+/* ---------------------------------------------------------------------- */
1308ab2a 21917+
b95c5147
AM
21918+/* no getattr version will be set by module.c:aufs_init() */
21919+struct inode_operations aufs_iop_nogetattr[AuIop_Last],
21920+ aufs_iop[] = {
21921+ [AuIop_SYMLINK] = {
21922+ .permission = aufs_permission,
c1595e42 21923+#ifdef CONFIG_FS_POSIX_ACL
b95c5147
AM
21924+ .get_acl = aufs_get_acl,
21925+ .set_acl = aufs_set_acl, /* unsupport for symlink? */
c1595e42
JR
21926+#endif
21927+
b95c5147
AM
21928+ .setattr = aufs_setattr,
21929+ .getattr = aufs_getattr,
0c3ec466 21930+
c1595e42 21931+#ifdef CONFIG_AUFS_XATTR
b95c5147 21932+ .listxattr = aufs_listxattr,
c1595e42
JR
21933+#endif
21934+
febd17d6 21935+ .get_link = aufs_get_link,
0c3ec466 21936+
b95c5147
AM
21937+ /* .update_time = aufs_update_time */
21938+ },
21939+ [AuIop_DIR] = {
21940+ .create = aufs_create,
21941+ .lookup = aufs_lookup,
21942+ .link = aufs_link,
21943+ .unlink = aufs_unlink,
21944+ .symlink = aufs_symlink,
21945+ .mkdir = aufs_mkdir,
21946+ .rmdir = aufs_rmdir,
21947+ .mknod = aufs_mknod,
21948+ .rename = aufs_rename,
21949+
21950+ .permission = aufs_permission,
c1595e42 21951+#ifdef CONFIG_FS_POSIX_ACL
b95c5147
AM
21952+ .get_acl = aufs_get_acl,
21953+ .set_acl = aufs_set_acl,
c1595e42
JR
21954+#endif
21955+
b95c5147
AM
21956+ .setattr = aufs_setattr,
21957+ .getattr = aufs_getattr,
0c3ec466 21958+
c1595e42 21959+#ifdef CONFIG_AUFS_XATTR
b95c5147 21960+ .listxattr = aufs_listxattr,
c1595e42
JR
21961+#endif
21962+
b95c5147
AM
21963+ .update_time = aufs_update_time,
21964+ .atomic_open = aufs_atomic_open,
21965+ .tmpfile = aufs_tmpfile
21966+ },
21967+ [AuIop_OTHER] = {
21968+ .permission = aufs_permission,
c1595e42 21969+#ifdef CONFIG_FS_POSIX_ACL
b95c5147
AM
21970+ .get_acl = aufs_get_acl,
21971+ .set_acl = aufs_set_acl,
c1595e42
JR
21972+#endif
21973+
b95c5147
AM
21974+ .setattr = aufs_setattr,
21975+ .getattr = aufs_getattr,
0c3ec466 21976+
c1595e42 21977+#ifdef CONFIG_AUFS_XATTR
b95c5147 21978+ .listxattr = aufs_listxattr,
c1595e42
JR
21979+#endif
21980+
b95c5147
AM
21981+ .update_time = aufs_update_time
21982+ }
4a4d8108 21983+};
7f207e10 21984diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
eca34b5c 21985--- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
46016270 21986+++ linux/fs/aufs/i_op_del.c 2021-06-30 21:35:11.397206648 +0200
0b2a12c6 21987@@ -0,0 +1,515 @@
cd7a4cd9 21988+// SPDX-License-Identifier: GPL-2.0
1facf9fc 21989+/*
d58c55f2 21990+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 21991+ *
21992+ * This program, aufs is free software; you can redistribute it and/or modify
21993+ * it under the terms of the GNU General Public License as published by
21994+ * the Free Software Foundation; either version 2 of the License, or
21995+ * (at your option) any later version.
dece6358
AM
21996+ *
21997+ * This program is distributed in the hope that it will be useful,
21998+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
21999+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22000+ * GNU General Public License for more details.
22001+ *
22002+ * You should have received a copy of the GNU General Public License
523b37e3 22003+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 22004+ */
22005+
22006+/*
4a4d8108 22007+ * inode operations (del entry)
1308ab2a 22008+ */
dece6358 22009+
eca801bf 22010+#include <linux/iversion.h>
1308ab2a 22011+#include "aufs.h"
dece6358 22012+
4a4d8108
AM
22013+/*
22014+ * decide if a new whiteout for @dentry is necessary or not.
22015+ * when it is necessary, prepare the parent dir for the upper branch whose
22016+ * branch index is @bcpup for creation. the actual creation of the whiteout will
22017+ * be done by caller.
22018+ * return value:
22019+ * 0: wh is unnecessary
22020+ * plus: wh is necessary
22021+ * minus: error
22022+ */
22023+int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
1308ab2a 22024+{
4a4d8108 22025+ int need_wh, err;
5afbbe0d 22026+ aufs_bindex_t btop;
4a4d8108 22027+ struct super_block *sb;
dece6358 22028+
4a4d8108 22029+ sb = dentry->d_sb;
5afbbe0d 22030+ btop = au_dbtop(dentry);
4a4d8108 22031+ if (*bcpup < 0) {
5afbbe0d
AM
22032+ *bcpup = btop;
22033+ if (au_test_ro(sb, btop, d_inode(dentry))) {
4a4d8108
AM
22034+ err = AuWbrCopyup(au_sbi(sb), dentry);
22035+ *bcpup = err;
22036+ if (unlikely(err < 0))
22037+ goto out;
22038+ }
22039+ } else
5afbbe0d 22040+ AuDebugOn(btop < *bcpup
5527c038 22041+ || au_test_ro(sb, *bcpup, d_inode(dentry)));
5afbbe0d 22042+ AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
1308ab2a 22043+
5afbbe0d 22044+ if (*bcpup != btop) {
4a4d8108
AM
22045+ err = au_cpup_dirs(dentry, *bcpup);
22046+ if (unlikely(err))
22047+ goto out;
22048+ need_wh = 1;
22049+ } else {
027c5e7a 22050+ struct au_dinfo *dinfo, *tmp;
4a4d8108 22051+
027c5e7a
AM
22052+ need_wh = -ENOMEM;
22053+ dinfo = au_di(dentry);
22054+ tmp = au_di_alloc(sb, AuLsc_DI_TMP);
22055+ if (tmp) {
22056+ au_di_cp(tmp, dinfo);
22057+ au_di_swap(tmp, dinfo);
22058+ /* returns the number of positive dentries */
5afbbe0d
AM
22059+ need_wh = au_lkup_dentry(dentry, btop + 1,
22060+ /* AuLkup_IGNORE_PERM */ 0);
027c5e7a
AM
22061+ au_di_swap(tmp, dinfo);
22062+ au_rw_write_unlock(&tmp->di_rwsem);
22063+ au_di_free(tmp);
4a4d8108
AM
22064+ }
22065+ }
22066+ AuDbg("need_wh %d\n", need_wh);
22067+ err = need_wh;
22068+
4f0767ce 22069+out:
4a4d8108 22070+ return err;
1facf9fc 22071+}
22072+
4a4d8108
AM
22073+/*
22074+ * simple tests for the del-entry operations.
22075+ * following the checks in vfs, plus the parent-child relationship.
22076+ */
22077+int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
22078+ struct dentry *h_parent, int isdir)
1facf9fc 22079+{
4a4d8108
AM
22080+ int err;
22081+ umode_t h_mode;
22082+ struct dentry *h_dentry, *h_latest;
1308ab2a 22083+ struct inode *h_inode;
0b2a12c6 22084+ struct user_namespace *h_userns;
1facf9fc 22085+
4a4d8108 22086+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 22087+ if (d_really_is_positive(dentry)) {
4a4d8108 22088+ err = -ENOENT;
5527c038
JR
22089+ if (unlikely(d_is_negative(h_dentry)))
22090+ goto out;
22091+ h_inode = d_inode(h_dentry);
22092+ if (unlikely(!h_inode->i_nlink))
4a4d8108 22093+ goto out;
1facf9fc 22094+
4a4d8108
AM
22095+ h_mode = h_inode->i_mode;
22096+ if (!isdir) {
22097+ err = -EISDIR;
22098+ if (unlikely(S_ISDIR(h_mode)))
22099+ goto out;
22100+ } else if (unlikely(!S_ISDIR(h_mode))) {
22101+ err = -ENOTDIR;
22102+ goto out;
22103+ }
22104+ } else {
22105+ /* rename(2) case */
22106+ err = -EIO;
5527c038 22107+ if (unlikely(d_is_positive(h_dentry)))
4a4d8108
AM
22108+ goto out;
22109+ }
1facf9fc 22110+
4a4d8108
AM
22111+ err = -ENOENT;
22112+ /* expected parent dir is locked */
22113+ if (unlikely(h_parent != h_dentry->d_parent))
22114+ goto out;
22115+ err = 0;
22116+
22117+ /*
22118+ * rmdir a dir may break the consistency on some filesystem.
22119+ * let's try heavy test.
22120+ */
22121+ err = -EACCES;
0b2a12c6 22122+ h_userns = au_sbr_userns(dentry->d_sb, bindex);
076b876e 22123+ if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1)
0b2a12c6 22124+ && au_test_h_perm(h_userns, d_inode(h_parent),
076b876e 22125+ MAY_EXEC | MAY_WRITE)))
4a4d8108
AM
22126+ goto out;
22127+
0b2a12c6 22128+ h_latest = au_sio_lkup_one(h_userns, &dentry->d_name, h_parent);
4a4d8108
AM
22129+ err = -EIO;
22130+ if (IS_ERR(h_latest))
22131+ goto out;
22132+ if (h_latest == h_dentry)
22133+ err = 0;
22134+ dput(h_latest);
22135+
4f0767ce 22136+out:
4a4d8108 22137+ return err;
1308ab2a 22138+}
1facf9fc 22139+
4a4d8108
AM
22140+/*
22141+ * decide the branch where we operate for @dentry. the branch index will be set
acd2b654 22142+ * @rbcpup. after deciding it, 'pin' it and store the timestamps of the parent
4a4d8108
AM
22143+ * dir for reverting.
22144+ * when a new whiteout is necessary, create it.
22145+ */
22146+static struct dentry*
22147+lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
22148+ struct au_dtime *dt, struct au_pin *pin)
1308ab2a 22149+{
4a4d8108
AM
22150+ struct dentry *wh_dentry;
22151+ struct super_block *sb;
22152+ struct path h_path;
22153+ int err, need_wh;
22154+ unsigned int udba;
22155+ aufs_bindex_t bcpup;
dece6358 22156+
4a4d8108
AM
22157+ need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
22158+ wh_dentry = ERR_PTR(need_wh);
22159+ if (unlikely(need_wh < 0))
22160+ goto out;
22161+
22162+ sb = dentry->d_sb;
22163+ udba = au_opt_udba(sb);
22164+ bcpup = *rbcpup;
22165+ err = au_pin(pin, dentry, bcpup, udba,
22166+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
22167+ wh_dentry = ERR_PTR(err);
22168+ if (unlikely(err))
22169+ goto out;
22170+
22171+ h_path.dentry = au_pinned_h_parent(pin);
22172+ if (udba != AuOpt_UDBA_NONE
5afbbe0d 22173+ && au_dbtop(dentry) == bcpup) {
4a4d8108
AM
22174+ err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
22175+ wh_dentry = ERR_PTR(err);
22176+ if (unlikely(err))
22177+ goto out_unpin;
22178+ }
22179+
22180+ h_path.mnt = au_sbr_mnt(sb, bcpup);
22181+ au_dtime_store(dt, au_pinned_parent(pin), &h_path);
22182+ wh_dentry = NULL;
22183+ if (!need_wh)
22184+ goto out; /* success, no need to create whiteout */
22185+
22186+ wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
22187+ if (IS_ERR(wh_dentry))
22188+ goto out_unpin;
22189+
22190+ /* returns with the parent is locked and wh_dentry is dget-ed */
22191+ goto out; /* success */
22192+
4f0767ce 22193+out_unpin:
4a4d8108 22194+ au_unpin(pin);
4f0767ce 22195+out:
4a4d8108 22196+ return wh_dentry;
1facf9fc 22197+}
22198+
4a4d8108
AM
22199+/*
22200+ * when removing a dir, rename it to a unique temporary whiteout-ed name first
22201+ * in order to be revertible and save time for removing many child whiteouts
22202+ * under the dir.
22203+ * returns 1 when there are too many child whiteout and caller should remove
22204+ * them asynchronously. returns 0 when the number of children is enough small to
22205+ * remove now or the branch fs is a remote fs.
22206+ * otherwise return an error.
22207+ */
22208+static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
22209+ struct au_nhash *whlist, struct inode *dir)
1facf9fc 22210+{
4a4d8108
AM
22211+ int rmdir_later, err, dirwh;
22212+ struct dentry *h_dentry;
22213+ struct super_block *sb;
5527c038 22214+ struct inode *inode;
4a4d8108
AM
22215+
22216+ sb = dentry->d_sb;
22217+ SiMustAnyLock(sb);
22218+ h_dentry = au_h_dptr(dentry, bindex);
22219+ err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
22220+ if (unlikely(err))
22221+ goto out;
22222+
22223+ /* stop monitoring */
5527c038
JR
22224+ inode = d_inode(dentry);
22225+ au_hn_free(au_hi(inode, bindex));
4a4d8108
AM
22226+
22227+ if (!au_test_fs_remote(h_dentry->d_sb)) {
22228+ dirwh = au_sbi(sb)->si_dirwh;
22229+ rmdir_later = (dirwh <= 1);
22230+ if (!rmdir_later)
22231+ rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
22232+ dirwh);
22233+ if (rmdir_later)
22234+ return rmdir_later;
22235+ }
1facf9fc 22236+
4a4d8108
AM
22237+ err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
22238+ if (unlikely(err)) {
523b37e3
AM
22239+ AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
22240+ h_dentry, bindex, err);
4a4d8108
AM
22241+ err = 0;
22242+ }
dece6358 22243+
4f0767ce 22244+out:
4a4d8108
AM
22245+ AuTraceErr(err);
22246+ return err;
22247+}
1308ab2a 22248+
4a4d8108
AM
22249+/*
22250+ * final procedure for deleting a entry.
22251+ * maintain dentry and iattr.
22252+ */
22253+static void epilog(struct inode *dir, struct dentry *dentry,
22254+ aufs_bindex_t bindex)
22255+{
22256+ struct inode *inode;
1308ab2a 22257+
5527c038 22258+ inode = d_inode(dentry);
4a4d8108
AM
22259+ d_drop(dentry);
22260+ inode->i_ctime = dir->i_ctime;
1308ab2a 22261+
b912730e 22262+ au_dir_ts(dir, bindex);
be118d29 22263+ inode_inc_iversion(dir);
1facf9fc 22264+}
22265+
4a4d8108
AM
22266+/*
22267+ * when an error happened, remove the created whiteout and revert everything.
22268+ */
7f207e10
AM
22269+static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
22270+ aufs_bindex_t bwh, struct dentry *wh_dentry,
22271+ struct dentry *dentry, struct au_dtime *dt)
1facf9fc 22272+{
4a4d8108
AM
22273+ int rerr;
22274+ struct path h_path = {
22275+ .dentry = wh_dentry,
7f207e10 22276+ .mnt = au_sbr_mnt(dir->i_sb, bindex)
4a4d8108 22277+ };
dece6358 22278+
7f207e10 22279+ rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
4a4d8108
AM
22280+ if (!rerr) {
22281+ au_set_dbwh(dentry, bwh);
22282+ au_dtime_revert(dt);
22283+ return 0;
22284+ }
dece6358 22285+
523b37e3 22286+ AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
4a4d8108 22287+ return -EIO;
1facf9fc 22288+}
22289+
4a4d8108 22290+/* ---------------------------------------------------------------------- */
1facf9fc 22291+
4a4d8108 22292+int aufs_unlink(struct inode *dir, struct dentry *dentry)
1308ab2a 22293+{
4a4d8108 22294+ int err;
5afbbe0d 22295+ aufs_bindex_t bwh, bindex, btop;
523b37e3 22296+ struct inode *inode, *h_dir, *delegated;
4a4d8108 22297+ struct dentry *parent, *wh_dentry;
acd2b654 22298+ /* to reduce stack size */
c2b27bf2
AM
22299+ struct {
22300+ struct au_dtime dt;
22301+ struct au_pin pin;
22302+ struct path h_path;
22303+ } *a;
1facf9fc 22304+
4a4d8108 22305+ IMustLock(dir);
027c5e7a 22306+
c2b27bf2
AM
22307+ err = -ENOMEM;
22308+ a = kmalloc(sizeof(*a), GFP_NOFS);
22309+ if (unlikely(!a))
22310+ goto out;
22311+
027c5e7a
AM
22312+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
22313+ if (unlikely(err))
c2b27bf2 22314+ goto out_free;
027c5e7a
AM
22315+ err = au_d_hashed_positive(dentry);
22316+ if (unlikely(err))
22317+ goto out_unlock;
5527c038 22318+ inode = d_inode(dentry);
4a4d8108 22319+ IMustLock(inode);
027c5e7a 22320+ err = -EISDIR;
2000de60 22321+ if (unlikely(d_is_dir(dentry)))
027c5e7a 22322+ goto out_unlock; /* possible? */
1facf9fc 22323+
5afbbe0d 22324+ btop = au_dbtop(dentry);
4a4d8108
AM
22325+ bwh = au_dbwh(dentry);
22326+ bindex = -1;
027c5e7a
AM
22327+ parent = dentry->d_parent; /* dir inode is locked */
22328+ di_write_lock_parent(parent);
c2b27bf2
AM
22329+ wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
22330+ &a->pin);
4a4d8108
AM
22331+ err = PTR_ERR(wh_dentry);
22332+ if (IS_ERR(wh_dentry))
027c5e7a 22333+ goto out_parent;
1facf9fc 22334+
5afbbe0d
AM
22335+ a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
22336+ a->h_path.dentry = au_h_dptr(dentry, btop);
c2b27bf2 22337+ dget(a->h_path.dentry);
5afbbe0d 22338+ if (bindex == btop) {
c2b27bf2 22339+ h_dir = au_pinned_h_dir(&a->pin);
523b37e3
AM
22340+ delegated = NULL;
22341+ err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
22342+ if (unlikely(err == -EWOULDBLOCK)) {
22343+ pr_warn("cannot retry for NFSv4 delegation"
22344+ " for an internal unlink\n");
22345+ iput(delegated);
22346+ }
4a4d8108
AM
22347+ } else {
22348+ /* dir inode is locked */
5527c038 22349+ h_dir = d_inode(wh_dentry->d_parent);
4a4d8108
AM
22350+ IMustLock(h_dir);
22351+ err = 0;
22352+ }
dece6358 22353+
4a4d8108 22354+ if (!err) {
7f207e10 22355+ vfsub_drop_nlink(inode);
4a4d8108
AM
22356+ epilog(dir, dentry, bindex);
22357+
22358+ /* update target timestamps */
5afbbe0d 22359+ if (bindex == btop) {
c2b27bf2
AM
22360+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
22361+ /*ignore*/
5527c038 22362+ inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
4a4d8108
AM
22363+ } else
22364+ /* todo: this timestamp may be reverted later */
22365+ inode->i_ctime = h_dir->i_ctime;
027c5e7a 22366+ goto out_unpin; /* success */
1facf9fc 22367+ }
22368+
4a4d8108
AM
22369+ /* revert */
22370+ if (wh_dentry) {
22371+ int rerr;
22372+
c2b27bf2
AM
22373+ rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22374+ &a->dt);
4a4d8108
AM
22375+ if (rerr)
22376+ err = rerr;
dece6358 22377+ }
1facf9fc 22378+
027c5e7a 22379+out_unpin:
c2b27bf2 22380+ au_unpin(&a->pin);
4a4d8108 22381+ dput(wh_dentry);
c2b27bf2 22382+ dput(a->h_path.dentry);
027c5e7a 22383+out_parent:
4a4d8108 22384+ di_write_unlock(parent);
027c5e7a 22385+out_unlock:
4a4d8108 22386+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2 22387+out_free:
9f237c51 22388+ au_kfree_rcu(a);
027c5e7a 22389+out:
4a4d8108 22390+ return err;
dece6358
AM
22391+}
22392+
4a4d8108 22393+int aufs_rmdir(struct inode *dir, struct dentry *dentry)
1308ab2a 22394+{
4a4d8108 22395+ int err, rmdir_later;
5afbbe0d 22396+ aufs_bindex_t bwh, bindex, btop;
4a4d8108
AM
22397+ struct inode *inode;
22398+ struct dentry *parent, *wh_dentry, *h_dentry;
22399+ struct au_whtmp_rmdir *args;
acd2b654 22400+ /* to reduce stack size */
c2b27bf2
AM
22401+ struct {
22402+ struct au_dtime dt;
22403+ struct au_pin pin;
22404+ } *a;
1facf9fc 22405+
4a4d8108 22406+ IMustLock(dir);
027c5e7a 22407+
c2b27bf2
AM
22408+ err = -ENOMEM;
22409+ a = kmalloc(sizeof(*a), GFP_NOFS);
22410+ if (unlikely(!a))
22411+ goto out;
22412+
027c5e7a
AM
22413+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
22414+ if (unlikely(err))
c2b27bf2 22415+ goto out_free;
53392da6
AM
22416+ err = au_alive_dir(dentry);
22417+ if (unlikely(err))
027c5e7a 22418+ goto out_unlock;
5527c038 22419+ inode = d_inode(dentry);
4a4d8108 22420+ IMustLock(inode);
027c5e7a 22421+ err = -ENOTDIR;
2000de60 22422+ if (unlikely(!d_is_dir(dentry)))
027c5e7a 22423+ goto out_unlock; /* possible? */
dece6358 22424+
4a4d8108
AM
22425+ err = -ENOMEM;
22426+ args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
22427+ if (unlikely(!args))
22428+ goto out_unlock;
dece6358 22429+
4a4d8108
AM
22430+ parent = dentry->d_parent; /* dir inode is locked */
22431+ di_write_lock_parent(parent);
22432+ err = au_test_empty(dentry, &args->whlist);
22433+ if (unlikely(err))
027c5e7a 22434+ goto out_parent;
1facf9fc 22435+
5afbbe0d 22436+ btop = au_dbtop(dentry);
4a4d8108
AM
22437+ bwh = au_dbwh(dentry);
22438+ bindex = -1;
c2b27bf2
AM
22439+ wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
22440+ &a->pin);
4a4d8108
AM
22441+ err = PTR_ERR(wh_dentry);
22442+ if (IS_ERR(wh_dentry))
027c5e7a 22443+ goto out_parent;
1facf9fc 22444+
5afbbe0d 22445+ h_dentry = au_h_dptr(dentry, btop);
4a4d8108
AM
22446+ dget(h_dentry);
22447+ rmdir_later = 0;
5afbbe0d
AM
22448+ if (bindex == btop) {
22449+ err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
4a4d8108
AM
22450+ if (err > 0) {
22451+ rmdir_later = err;
22452+ err = 0;
22453+ }
22454+ } else {
22455+ /* stop monitoring */
5afbbe0d 22456+ au_hn_free(au_hi(inode, btop));
4a4d8108
AM
22457+
22458+ /* dir inode is locked */
5527c038 22459+ IMustLock(d_inode(wh_dentry->d_parent));
1facf9fc 22460+ err = 0;
22461+ }
22462+
4a4d8108 22463+ if (!err) {
027c5e7a 22464+ vfsub_dead_dir(inode);
4a4d8108
AM
22465+ au_set_dbdiropq(dentry, -1);
22466+ epilog(dir, dentry, bindex);
1308ab2a 22467+
4a4d8108 22468+ if (rmdir_later) {
5afbbe0d 22469+ au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
4a4d8108
AM
22470+ args = NULL;
22471+ }
1308ab2a 22472+
4a4d8108 22473+ goto out_unpin; /* success */
1facf9fc 22474+ }
22475+
4a4d8108
AM
22476+ /* revert */
22477+ AuLabel(revert);
22478+ if (wh_dentry) {
22479+ int rerr;
1308ab2a 22480+
c2b27bf2
AM
22481+ rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22482+ &a->dt);
4a4d8108
AM
22483+ if (rerr)
22484+ err = rerr;
1facf9fc 22485+ }
22486+
4f0767ce 22487+out_unpin:
c2b27bf2 22488+ au_unpin(&a->pin);
4a4d8108
AM
22489+ dput(wh_dentry);
22490+ dput(h_dentry);
027c5e7a 22491+out_parent:
4a4d8108
AM
22492+ di_write_unlock(parent);
22493+ if (args)
22494+ au_whtmp_rmdir_free(args);
4f0767ce 22495+out_unlock:
4a4d8108 22496+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2 22497+out_free:
9f237c51 22498+ au_kfree_rcu(a);
4f0767ce 22499+out:
4a4d8108
AM
22500+ AuTraceErr(err);
22501+ return err;
dece6358 22502+}
7f207e10 22503diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
eca34b5c 22504--- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
46016270 22505+++ linux/fs/aufs/i_op_ren.c 2021-06-30 21:35:11.397206648 +0200
0b2a12c6 22506@@ -0,0 +1,1251 @@
cd7a4cd9 22507+// SPDX-License-Identifier: GPL-2.0
1facf9fc 22508+/*
d58c55f2 22509+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 22510+ *
22511+ * This program, aufs is free software; you can redistribute it and/or modify
22512+ * it under the terms of the GNU General Public License as published by
22513+ * the Free Software Foundation; either version 2 of the License, or
22514+ * (at your option) any later version.
dece6358
AM
22515+ *
22516+ * This program is distributed in the hope that it will be useful,
22517+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
22518+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22519+ * GNU General Public License for more details.
22520+ *
22521+ * You should have received a copy of the GNU General Public License
523b37e3 22522+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 22523+ */
22524+
22525+/*
4a4d8108
AM
22526+ * inode operation (rename entry)
22527+ * todo: this is crazy monster
1facf9fc 22528+ */
22529+
eca801bf 22530+#include <linux/iversion.h>
1facf9fc 22531+#include "aufs.h"
22532+
4a4d8108
AM
22533+enum { AuSRC, AuDST, AuSrcDst };
22534+enum { AuPARENT, AuCHILD, AuParentChild };
1facf9fc 22535+
f2c43d5f
AM
22536+#define AuRen_ISDIR_SRC 1
22537+#define AuRen_ISDIR_DST (1 << 1)
22538+#define AuRen_ISSAMEDIR (1 << 2)
22539+#define AuRen_WHSRC (1 << 3)
22540+#define AuRen_WHDST (1 << 4)
22541+#define AuRen_MNT_WRITE (1 << 5)
22542+#define AuRen_DT_DSTDIR (1 << 6)
22543+#define AuRen_DIROPQ_SRC (1 << 7)
22544+#define AuRen_DIROPQ_DST (1 << 8)
8b6a4947
AM
22545+#define AuRen_DIRREN (1 << 9)
22546+#define AuRen_DROPPED_SRC (1 << 10)
22547+#define AuRen_DROPPED_DST (1 << 11)
4a4d8108 22548+#define au_ftest_ren(flags, name) ((flags) & AuRen_##name)
7f207e10
AM
22549+#define au_fset_ren(flags, name) \
22550+ do { (flags) |= AuRen_##name; } while (0)
22551+#define au_fclr_ren(flags, name) \
22552+ do { (flags) &= ~AuRen_##name; } while (0)
1facf9fc 22553+
8b6a4947
AM
22554+#ifndef CONFIG_AUFS_DIRREN
22555+#undef AuRen_DIRREN
22556+#define AuRen_DIRREN 0
22557+#endif
22558+
4a4d8108
AM
22559+struct au_ren_args {
22560+ struct {
22561+ struct dentry *dentry, *h_dentry, *parent, *h_parent,
22562+ *wh_dentry;
22563+ struct inode *dir, *inode;
f2c43d5f 22564+ struct au_hinode *hdir, *hinode;
4a4d8108 22565+ struct au_dtime dt[AuParentChild];
f2c43d5f 22566+ aufs_bindex_t btop, bdiropq;
4a4d8108 22567+ } sd[AuSrcDst];
1facf9fc 22568+
4a4d8108
AM
22569+#define src_dentry sd[AuSRC].dentry
22570+#define src_dir sd[AuSRC].dir
22571+#define src_inode sd[AuSRC].inode
22572+#define src_h_dentry sd[AuSRC].h_dentry
22573+#define src_parent sd[AuSRC].parent
22574+#define src_h_parent sd[AuSRC].h_parent
22575+#define src_wh_dentry sd[AuSRC].wh_dentry
22576+#define src_hdir sd[AuSRC].hdir
f2c43d5f 22577+#define src_hinode sd[AuSRC].hinode
4a4d8108
AM
22578+#define src_h_dir sd[AuSRC].hdir->hi_inode
22579+#define src_dt sd[AuSRC].dt
5afbbe0d 22580+#define src_btop sd[AuSRC].btop
f2c43d5f 22581+#define src_bdiropq sd[AuSRC].bdiropq
1facf9fc 22582+
4a4d8108
AM
22583+#define dst_dentry sd[AuDST].dentry
22584+#define dst_dir sd[AuDST].dir
22585+#define dst_inode sd[AuDST].inode
22586+#define dst_h_dentry sd[AuDST].h_dentry
22587+#define dst_parent sd[AuDST].parent
22588+#define dst_h_parent sd[AuDST].h_parent
22589+#define dst_wh_dentry sd[AuDST].wh_dentry
22590+#define dst_hdir sd[AuDST].hdir
f2c43d5f 22591+#define dst_hinode sd[AuDST].hinode
4a4d8108
AM
22592+#define dst_h_dir sd[AuDST].hdir->hi_inode
22593+#define dst_dt sd[AuDST].dt
5afbbe0d 22594+#define dst_btop sd[AuDST].btop
f2c43d5f 22595+#define dst_bdiropq sd[AuDST].bdiropq
4a4d8108
AM
22596+
22597+ struct dentry *h_trap;
22598+ struct au_branch *br;
4a4d8108
AM
22599+ struct path h_path;
22600+ struct au_nhash whlist;
f2c43d5f 22601+ aufs_bindex_t btgt, src_bwh;
1facf9fc 22602+
f2c43d5f
AM
22603+ struct {
22604+ unsigned short auren_flags;
22605+ unsigned char flags; /* syscall parameter */
22606+ unsigned char exchange;
22607+ } __packed;
1facf9fc 22608+
4a4d8108
AM
22609+ struct au_whtmp_rmdir *thargs;
22610+ struct dentry *h_dst;
8b6a4947 22611+ struct au_hinode *h_root;
4a4d8108 22612+};
1308ab2a 22613+
4a4d8108 22614+/* ---------------------------------------------------------------------- */
1308ab2a 22615+
4a4d8108
AM
22616+/*
22617+ * functions for reverting.
22618+ * when an error happened in a single rename systemcall, we should revert
79b8bda9 22619+ * everything as if nothing happened.
4a4d8108
AM
22620+ * we don't need to revert the copied-up/down the parent dir since they are
22621+ * harmless.
22622+ */
1facf9fc 22623+
4a4d8108
AM
22624+#define RevertFailure(fmt, ...) do { \
22625+ AuIOErr("revert failure: " fmt " (%d, %d)\n", \
22626+ ##__VA_ARGS__, err, rerr); \
22627+ err = -EIO; \
22628+} while (0)
1facf9fc 22629+
f2c43d5f 22630+static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
1facf9fc 22631+{
4a4d8108 22632+ int rerr;
f2c43d5f
AM
22633+ struct dentry *d;
22634+#define src_or_dst(member) a->sd[idx].member
1facf9fc 22635+
f2c43d5f
AM
22636+ d = src_or_dst(dentry); /* {src,dst}_dentry */
22637+ au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22638+ rerr = au_diropq_remove(d, a->btgt);
22639+ au_hn_inode_unlock(src_or_dst(hinode));
22640+ au_set_dbdiropq(d, src_or_dst(bdiropq));
4a4d8108 22641+ if (rerr)
f2c43d5f
AM
22642+ RevertFailure("remove diropq %pd", d);
22643+
22644+#undef src_or_dst_
22645+}
22646+
22647+static void au_ren_rev_diropq(int err, struct au_ren_args *a)
22648+{
22649+ if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
22650+ au_ren_do_rev_diropq(err, a, AuSRC);
22651+ if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
22652+ au_ren_do_rev_diropq(err, a, AuDST);
4a4d8108 22653+}
1facf9fc 22654+
4a4d8108
AM
22655+static void au_ren_rev_rename(int err, struct au_ren_args *a)
22656+{
22657+ int rerr;
523b37e3 22658+ struct inode *delegated;
1facf9fc 22659+
b4510431
AM
22660+ a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name,
22661+ a->src_h_parent);
4a4d8108
AM
22662+ rerr = PTR_ERR(a->h_path.dentry);
22663+ if (IS_ERR(a->h_path.dentry)) {
523b37e3 22664+ RevertFailure("lkup one %pd", a->src_dentry);
4a4d8108 22665+ return;
1facf9fc 22666+ }
22667+
523b37e3 22668+ delegated = NULL;
4a4d8108
AM
22669+ rerr = vfsub_rename(a->dst_h_dir,
22670+ au_h_dptr(a->src_dentry, a->btgt),
f2c43d5f 22671+ a->src_h_dir, &a->h_path, &delegated, a->flags);
523b37e3
AM
22672+ if (unlikely(rerr == -EWOULDBLOCK)) {
22673+ pr_warn("cannot retry for NFSv4 delegation"
22674+ " for an internal rename\n");
22675+ iput(delegated);
22676+ }
4a4d8108
AM
22677+ d_drop(a->h_path.dentry);
22678+ dput(a->h_path.dentry);
22679+ /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
22680+ if (rerr)
523b37e3 22681+ RevertFailure("rename %pd", a->src_dentry);
1facf9fc 22682+}
22683+
4a4d8108 22684+static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
1facf9fc 22685+{
4a4d8108 22686+ int rerr;
523b37e3 22687+ struct inode *delegated;
dece6358 22688+
b4510431
AM
22689+ a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name,
22690+ a->dst_h_parent);
4a4d8108
AM
22691+ rerr = PTR_ERR(a->h_path.dentry);
22692+ if (IS_ERR(a->h_path.dentry)) {
523b37e3 22693+ RevertFailure("lkup one %pd", a->dst_dentry);
4a4d8108
AM
22694+ return;
22695+ }
5527c038 22696+ if (d_is_positive(a->h_path.dentry)) {
4a4d8108
AM
22697+ d_drop(a->h_path.dentry);
22698+ dput(a->h_path.dentry);
22699+ return;
dece6358
AM
22700+ }
22701+
523b37e3
AM
22702+ delegated = NULL;
22703+ rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
f2c43d5f 22704+ &delegated, a->flags);
523b37e3
AM
22705+ if (unlikely(rerr == -EWOULDBLOCK)) {
22706+ pr_warn("cannot retry for NFSv4 delegation"
22707+ " for an internal rename\n");
22708+ iput(delegated);
22709+ }
4a4d8108
AM
22710+ d_drop(a->h_path.dentry);
22711+ dput(a->h_path.dentry);
22712+ if (!rerr)
22713+ au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
22714+ else
523b37e3 22715+ RevertFailure("rename %pd", a->h_dst);
4a4d8108 22716+}
1308ab2a 22717+
4a4d8108
AM
22718+static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
22719+{
22720+ int rerr;
1308ab2a 22721+
4a4d8108
AM
22722+ a->h_path.dentry = a->src_wh_dentry;
22723+ rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
027c5e7a 22724+ au_set_dbwh(a->src_dentry, a->src_bwh);
4a4d8108 22725+ if (rerr)
523b37e3 22726+ RevertFailure("unlink %pd", a->src_wh_dentry);
4a4d8108 22727+}
4a4d8108 22728+#undef RevertFailure
1facf9fc 22729+
1308ab2a 22730+/* ---------------------------------------------------------------------- */
22731+
4a4d8108
AM
22732+/*
22733+ * when we have to copyup the renaming entry, do it with the rename-target name
22734+ * in order to minimize the cost (the later actual rename is unnecessary).
22735+ * otherwise rename it on the target branch.
22736+ */
22737+static int au_ren_or_cpup(struct au_ren_args *a)
1facf9fc 22738+{
dece6358 22739+ int err;
4a4d8108 22740+ struct dentry *d;
523b37e3 22741+ struct inode *delegated;
1facf9fc 22742+
4a4d8108 22743+ d = a->src_dentry;
5afbbe0d 22744+ if (au_dbtop(d) == a->btgt) {
4a4d8108 22745+ a->h_path.dentry = a->dst_h_dentry;
5afbbe0d 22746+ AuDebugOn(au_dbtop(d) != a->btgt);
523b37e3 22747+ delegated = NULL;
4a4d8108 22748+ err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
f2c43d5f
AM
22749+ a->dst_h_dir, &a->h_path, &delegated,
22750+ a->flags);
523b37e3
AM
22751+ if (unlikely(err == -EWOULDBLOCK)) {
22752+ pr_warn("cannot retry for NFSv4 delegation"
22753+ " for an internal rename\n");
22754+ iput(delegated);
22755+ }
c2b27bf2 22756+ } else
86dc4139 22757+ BUG();
1308ab2a 22758+
027c5e7a
AM
22759+ if (!err && a->h_dst)
22760+ /* it will be set to dinfo later */
22761+ dget(a->h_dst);
1facf9fc 22762+
dece6358
AM
22763+ return err;
22764+}
1facf9fc 22765+
4a4d8108
AM
22766+/* cf. aufs_rmdir() */
22767+static int au_ren_del_whtmp(struct au_ren_args *a)
dece6358 22768+{
4a4d8108
AM
22769+ int err;
22770+ struct inode *dir;
1facf9fc 22771+
4a4d8108
AM
22772+ dir = a->dst_dir;
22773+ SiMustAnyLock(dir->i_sb);
22774+ if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
22775+ au_sbi(dir->i_sb)->si_dirwh)
22776+ || au_test_fs_remote(a->h_dst->d_sb)) {
22777+ err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
22778+ if (unlikely(err))
523b37e3
AM
22779+ pr_warn("failed removing whtmp dir %pd (%d), "
22780+ "ignored.\n", a->h_dst, err);
4a4d8108
AM
22781+ } else {
22782+ au_nhash_wh_free(&a->thargs->whlist);
22783+ a->thargs->whlist = a->whlist;
22784+ a->whlist.nh_num = 0;
22785+ au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
22786+ dput(a->h_dst);
22787+ a->thargs = NULL;
22788+ }
22789+
22790+ return 0;
1308ab2a 22791+}
1facf9fc 22792+
4a4d8108 22793+/* make it 'opaque' dir. */
f2c43d5f 22794+static int au_ren_do_diropq(struct au_ren_args *a, int idx)
4a4d8108
AM
22795+{
22796+ int err;
f2c43d5f
AM
22797+ struct dentry *d, *diropq;
22798+#define src_or_dst(member) a->sd[idx].member
1facf9fc 22799+
4a4d8108 22800+ err = 0;
f2c43d5f
AM
22801+ d = src_or_dst(dentry); /* {src,dst}_dentry */
22802+ src_or_dst(bdiropq) = au_dbdiropq(d);
22803+ src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
22804+ au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22805+ diropq = au_diropq_create(d, a->btgt);
22806+ au_hn_inode_unlock(src_or_dst(hinode));
4a4d8108
AM
22807+ if (IS_ERR(diropq))
22808+ err = PTR_ERR(diropq);
076b876e
AM
22809+ else
22810+ dput(diropq);
1facf9fc 22811+
f2c43d5f 22812+#undef src_or_dst_
4a4d8108
AM
22813+ return err;
22814+}
1facf9fc 22815+
f2c43d5f 22816+static int au_ren_diropq(struct au_ren_args *a)
4a4d8108
AM
22817+{
22818+ int err;
f2c43d5f
AM
22819+ unsigned char always;
22820+ struct dentry *d;
1facf9fc 22821+
f2c43d5f
AM
22822+ err = 0;
22823+ d = a->dst_dentry; /* already renamed on the branch */
22824+ always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
22825+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
8b6a4947 22826+ && !au_ftest_ren(a->auren_flags, DIRREN)
f2c43d5f
AM
22827+ && a->btgt != au_dbdiropq(a->src_dentry)
22828+ && (a->dst_wh_dentry
22829+ || a->btgt <= au_dbdiropq(d)
22830+ /* hide the lower to keep xino */
22831+ /* the lowers may not be a dir, but we hide them anyway */
22832+ || a->btgt < au_dbbot(d)
22833+ || always)) {
22834+ AuDbg("here\n");
22835+ err = au_ren_do_diropq(a, AuSRC);
22836+ if (unlikely(err))
4a4d8108 22837+ goto out;
f2c43d5f 22838+ au_fset_ren(a->auren_flags, DIROPQ_SRC);
4a4d8108 22839+ }
f2c43d5f
AM
22840+ if (!a->exchange)
22841+ goto out; /* success */
1facf9fc 22842+
f2c43d5f
AM
22843+ d = a->src_dentry; /* already renamed on the branch */
22844+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)
22845+ && a->btgt != au_dbdiropq(a->dst_dentry)
22846+ && (a->btgt < au_dbdiropq(d)
22847+ || a->btgt < au_dbbot(d)
22848+ || always)) {
22849+ AuDbgDentry(a->src_dentry);
22850+ AuDbgDentry(a->dst_dentry);
22851+ err = au_ren_do_diropq(a, AuDST);
4a4d8108 22852+ if (unlikely(err))
f2c43d5f
AM
22853+ goto out_rev_src;
22854+ au_fset_ren(a->auren_flags, DIROPQ_DST);
22855+ }
22856+ goto out; /* success */
dece6358 22857+
f2c43d5f
AM
22858+out_rev_src:
22859+ AuDbg("err %d, reverting src\n", err);
22860+ au_ren_rev_diropq(err, a);
22861+out:
22862+ return err;
22863+}
22864+
22865+static int do_rename(struct au_ren_args *a)
22866+{
22867+ int err;
22868+ struct dentry *d, *h_d;
22869+
22870+ if (!a->exchange) {
22871+ /* prepare workqueue args for asynchronous rmdir */
22872+ h_d = a->dst_h_dentry;
22873+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)
8b6a4947 22874+ /* && !au_ftest_ren(a->auren_flags, DIRREN) */
f2c43d5f
AM
22875+ && d_is_positive(h_d)) {
22876+ err = -ENOMEM;
22877+ a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
22878+ GFP_NOFS);
22879+ if (unlikely(!a->thargs))
22880+ goto out;
22881+ a->h_dst = dget(h_d);
22882+ }
22883+
22884+ /* create whiteout for src_dentry */
22885+ if (au_ftest_ren(a->auren_flags, WHSRC)) {
22886+ a->src_bwh = au_dbwh(a->src_dentry);
22887+ AuDebugOn(a->src_bwh >= 0);
22888+ a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
22889+ a->src_h_parent);
22890+ err = PTR_ERR(a->src_wh_dentry);
22891+ if (IS_ERR(a->src_wh_dentry))
22892+ goto out_thargs;
22893+ }
22894+
22895+ /* lookup whiteout for dentry */
22896+ if (au_ftest_ren(a->auren_flags, WHDST)) {
22897+ h_d = au_wh_lkup(a->dst_h_parent,
22898+ &a->dst_dentry->d_name, a->br);
22899+ err = PTR_ERR(h_d);
22900+ if (IS_ERR(h_d))
22901+ goto out_whsrc;
22902+ if (d_is_negative(h_d))
22903+ dput(h_d);
22904+ else
22905+ a->dst_wh_dentry = h_d;
22906+ }
22907+
22908+ /* rename dentry to tmpwh */
22909+ if (a->thargs) {
22910+ err = au_whtmp_ren(a->dst_h_dentry, a->br);
22911+ if (unlikely(err))
22912+ goto out_whdst;
22913+
22914+ d = a->dst_dentry;
22915+ au_set_h_dptr(d, a->btgt, NULL);
22916+ err = au_lkup_neg(d, a->btgt, /*wh*/0);
22917+ if (unlikely(err))
22918+ goto out_whtmp;
22919+ a->dst_h_dentry = au_h_dptr(d, a->btgt);
22920+ }
4a4d8108 22921+ }
1facf9fc 22922+
5afbbe0d 22923+ BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
43982f53 22924+#if 0 /* debugging */
8b6a4947
AM
22925+ BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN)
22926+ && d_is_positive(a->dst_h_dentry)
22927+ && a->src_btop != a->btgt);
22928+#endif
1facf9fc 22929+
4a4d8108 22930+ /* rename by vfs_rename or cpup */
4a4d8108
AM
22931+ err = au_ren_or_cpup(a);
22932+ if (unlikely(err))
22933+ /* leave the copied-up one */
22934+ goto out_whtmp;
1308ab2a 22935+
4a4d8108 22936+ /* make dir opaque */
f2c43d5f
AM
22937+ err = au_ren_diropq(a);
22938+ if (unlikely(err))
22939+ goto out_rename;
1308ab2a 22940+
4a4d8108 22941+ /* update target timestamps */
f2c43d5f
AM
22942+ if (a->exchange) {
22943+ AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
22944+ a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
22945+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
22946+ a->dst_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
22947+ }
5afbbe0d 22948+ AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
4a4d8108
AM
22949+ a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
22950+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
5527c038 22951+ a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
1facf9fc 22952+
f2c43d5f
AM
22953+ if (!a->exchange) {
22954+ /* remove whiteout for dentry */
22955+ if (a->dst_wh_dentry) {
22956+ a->h_path.dentry = a->dst_wh_dentry;
22957+ err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
22958+ a->dst_dentry);
22959+ if (unlikely(err))
22960+ goto out_diropq;
22961+ }
1facf9fc 22962+
f2c43d5f
AM
22963+ /* remove whtmp */
22964+ if (a->thargs)
22965+ au_ren_del_whtmp(a); /* ignore this error */
1308ab2a 22966+
f2c43d5f
AM
22967+ au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
22968+ }
4a4d8108
AM
22969+ err = 0;
22970+ goto out_success;
22971+
4f0767ce 22972+out_diropq:
f2c43d5f 22973+ au_ren_rev_diropq(err, a);
4f0767ce 22974+out_rename:
7e9cd9fe 22975+ au_ren_rev_rename(err, a);
027c5e7a 22976+ dput(a->h_dst);
4f0767ce 22977+out_whtmp:
4a4d8108
AM
22978+ if (a->thargs)
22979+ au_ren_rev_whtmp(err, a);
4f0767ce 22980+out_whdst:
4a4d8108
AM
22981+ dput(a->dst_wh_dentry);
22982+ a->dst_wh_dentry = NULL;
4f0767ce 22983+out_whsrc:
4a4d8108
AM
22984+ if (a->src_wh_dentry)
22985+ au_ren_rev_whsrc(err, a);
4f0767ce 22986+out_success:
4a4d8108
AM
22987+ dput(a->src_wh_dentry);
22988+ dput(a->dst_wh_dentry);
4f0767ce 22989+out_thargs:
4a4d8108
AM
22990+ if (a->thargs) {
22991+ dput(a->h_dst);
22992+ au_whtmp_rmdir_free(a->thargs);
22993+ a->thargs = NULL;
22994+ }
4f0767ce 22995+out:
4a4d8108 22996+ return err;
dece6358 22997+}
1facf9fc 22998+
1308ab2a 22999+/* ---------------------------------------------------------------------- */
1facf9fc 23000+
4a4d8108
AM
23001+/*
23002+ * test if @dentry dir can be rename destination or not.
23003+ * success means, it is a logically empty dir.
23004+ */
23005+static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
1308ab2a 23006+{
4a4d8108 23007+ return au_test_empty(dentry, whlist);
1308ab2a 23008+}
1facf9fc 23009+
4a4d8108 23010+/*
8b6a4947
AM
23011+ * test if @a->src_dentry dir can be rename source or not.
23012+ * if it can, return 0.
4a4d8108
AM
23013+ * success means,
23014+ * - it is a logically empty dir.
23015+ * - or, it exists on writable branch and has no children including whiteouts
8b6a4947 23016+ * on the lower branch unless DIRREN is on.
4a4d8108 23017+ */
8b6a4947 23018+static int may_rename_srcdir(struct au_ren_args *a)
4a4d8108
AM
23019+{
23020+ int err;
23021+ unsigned int rdhash;
8b6a4947
AM
23022+ aufs_bindex_t btop, btgt;
23023+ struct dentry *dentry;
23024+ struct super_block *sb;
23025+ struct au_sbinfo *sbinfo;
1facf9fc 23026+
8b6a4947
AM
23027+ dentry = a->src_dentry;
23028+ sb = dentry->d_sb;
23029+ sbinfo = au_sbi(sb);
23030+ if (au_opt_test(sbinfo->si_mntflags, DIRREN))
23031+ au_fset_ren(a->auren_flags, DIRREN);
23032+
23033+ btgt = a->btgt;
5afbbe0d
AM
23034+ btop = au_dbtop(dentry);
23035+ if (btop != btgt) {
4a4d8108 23036+ struct au_nhash whlist;
dece6358 23037+
8b6a4947
AM
23038+ SiMustAnyLock(sb);
23039+ rdhash = sbinfo->si_rdhash;
4a4d8108
AM
23040+ if (!rdhash)
23041+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
23042+ dentry));
23043+ err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
23044+ if (unlikely(err))
23045+ goto out;
23046+ err = au_test_empty(dentry, &whlist);
23047+ au_nhash_wh_free(&whlist);
23048+ goto out;
23049+ }
dece6358 23050+
5afbbe0d 23051+ if (btop == au_dbtaildir(dentry))
4a4d8108 23052+ return 0; /* success */
dece6358 23053+
4a4d8108 23054+ err = au_test_empty_lower(dentry);
1facf9fc 23055+
4f0767ce 23056+out:
4a4d8108 23057+ if (err == -ENOTEMPTY) {
8b6a4947
AM
23058+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
23059+ err = 0;
23060+ } else {
23061+ AuWarn1("renaming dir who has child(ren) on multiple "
23062+ "branches, is not supported\n");
23063+ err = -EXDEV;
23064+ }
4a4d8108
AM
23065+ }
23066+ return err;
23067+}
1308ab2a 23068+
4a4d8108
AM
23069+/* side effect: sets whlist and h_dentry */
23070+static int au_ren_may_dir(struct au_ren_args *a)
1308ab2a 23071+{
4a4d8108
AM
23072+ int err;
23073+ unsigned int rdhash;
23074+ struct dentry *d;
1facf9fc 23075+
4a4d8108
AM
23076+ d = a->dst_dentry;
23077+ SiMustAnyLock(d->d_sb);
1facf9fc 23078+
4a4d8108 23079+ err = 0;
f2c43d5f 23080+ if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
4a4d8108
AM
23081+ rdhash = au_sbi(d->d_sb)->si_rdhash;
23082+ if (!rdhash)
23083+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
23084+ err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
23085+ if (unlikely(err))
23086+ goto out;
1308ab2a 23087+
f2c43d5f
AM
23088+ if (!a->exchange) {
23089+ au_set_dbtop(d, a->dst_btop);
23090+ err = may_rename_dstdir(d, &a->whlist);
23091+ au_set_dbtop(d, a->btgt);
23092+ } else
8b6a4947 23093+ err = may_rename_srcdir(a);
4a4d8108 23094+ }
5afbbe0d 23095+ a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
4a4d8108
AM
23096+ if (unlikely(err))
23097+ goto out;
23098+
23099+ d = a->src_dentry;
5afbbe0d 23100+ a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
f2c43d5f 23101+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
8b6a4947 23102+ err = may_rename_srcdir(a);
4a4d8108
AM
23103+ if (unlikely(err)) {
23104+ au_nhash_wh_free(&a->whlist);
23105+ a->whlist.nh_num = 0;
23106+ }
23107+ }
4f0767ce 23108+out:
4a4d8108 23109+ return err;
1facf9fc 23110+}
23111+
4a4d8108 23112+/* ---------------------------------------------------------------------- */
1facf9fc 23113+
4a4d8108
AM
23114+/*
23115+ * simple tests for rename.
23116+ * following the checks in vfs, plus the parent-child relationship.
23117+ */
23118+static int au_may_ren(struct au_ren_args *a)
23119+{
23120+ int err, isdir;
23121+ struct inode *h_inode;
1facf9fc 23122+
5afbbe0d 23123+ if (a->src_btop == a->btgt) {
4a4d8108 23124+ err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
f2c43d5f 23125+ au_ftest_ren(a->auren_flags, ISDIR_SRC));
4a4d8108
AM
23126+ if (unlikely(err))
23127+ goto out;
23128+ err = -EINVAL;
23129+ if (unlikely(a->src_h_dentry == a->h_trap))
23130+ goto out;
23131+ }
1facf9fc 23132+
4a4d8108 23133+ err = 0;
5afbbe0d 23134+ if (a->dst_btop != a->btgt)
4a4d8108 23135+ goto out;
1facf9fc 23136+
027c5e7a
AM
23137+ err = -ENOTEMPTY;
23138+ if (unlikely(a->dst_h_dentry == a->h_trap))
23139+ goto out;
23140+
4a4d8108 23141+ err = -EIO;
f2c43d5f 23142+ isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
5527c038
JR
23143+ if (d_really_is_negative(a->dst_dentry)) {
23144+ if (d_is_negative(a->dst_h_dentry))
23145+ err = au_may_add(a->dst_dentry, a->btgt,
23146+ a->dst_h_parent, isdir);
4a4d8108 23147+ } else {
5527c038 23148+ if (unlikely(d_is_negative(a->dst_h_dentry)))
4a4d8108 23149+ goto out;
5527c038
JR
23150+ h_inode = d_inode(a->dst_h_dentry);
23151+ if (h_inode->i_nlink)
23152+ err = au_may_del(a->dst_dentry, a->btgt,
23153+ a->dst_h_parent, isdir);
4a4d8108 23154+ }
1facf9fc 23155+
4f0767ce 23156+out:
4a4d8108
AM
23157+ if (unlikely(err == -ENOENT || err == -EEXIST))
23158+ err = -EIO;
23159+ AuTraceErr(err);
23160+ return err;
23161+}
1facf9fc 23162+
1308ab2a 23163+/* ---------------------------------------------------------------------- */
1facf9fc 23164+
4a4d8108
AM
23165+/*
23166+ * locking order
23167+ * (VFS)
23168+ * - src_dir and dir by lock_rename()
acd2b654 23169+ * - inode if exists
4a4d8108
AM
23170+ * (aufs)
23171+ * - lock all
23172+ * + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
23173+ * + si_read_lock
23174+ * + di_write_lock2_child()
23175+ * + di_write_lock_child()
23176+ * + ii_write_lock_child()
23177+ * + di_write_lock_child2()
23178+ * + ii_write_lock_child2()
23179+ * + src_parent and parent
23180+ * + di_write_lock_parent()
23181+ * + ii_write_lock_parent()
23182+ * + di_write_lock_parent2()
23183+ * + ii_write_lock_parent2()
23184+ * + lower src_dir and dir by vfsub_lock_rename()
23185+ * + verify the every relationships between child and parent. if any
23186+ * of them failed, unlock all and return -EBUSY.
23187+ */
23188+static void au_ren_unlock(struct au_ren_args *a)
1308ab2a 23189+{
4a4d8108
AM
23190+ vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
23191+ a->dst_h_parent, a->dst_hdir);
8b6a4947
AM
23192+ if (au_ftest_ren(a->auren_flags, DIRREN)
23193+ && a->h_root)
23194+ au_hn_inode_unlock(a->h_root);
f2c43d5f 23195+ if (au_ftest_ren(a->auren_flags, MNT_WRITE))
86dc4139 23196+ vfsub_mnt_drop_write(au_br_mnt(a->br));
1308ab2a 23197+}
23198+
4a4d8108 23199+static int au_ren_lock(struct au_ren_args *a)
1308ab2a 23200+{
4a4d8108
AM
23201+ int err;
23202+ unsigned int udba;
1308ab2a 23203+
4a4d8108
AM
23204+ err = 0;
23205+ a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
23206+ a->src_hdir = au_hi(a->src_dir, a->btgt);
23207+ a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
23208+ a->dst_hdir = au_hi(a->dst_dir, a->btgt);
86dc4139
AM
23209+
23210+ err = vfsub_mnt_want_write(au_br_mnt(a->br));
23211+ if (unlikely(err))
23212+ goto out;
f2c43d5f 23213+ au_fset_ren(a->auren_flags, MNT_WRITE);
8b6a4947
AM
23214+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
23215+ struct dentry *root;
23216+ struct inode *dir;
23217+
23218+ /*
23219+ * sbinfo is already locked, so this ii_read_lock is
23220+ * unnecessary. but our debugging feature checks it.
23221+ */
23222+ root = a->src_inode->i_sb->s_root;
23223+ if (root != a->src_parent && root != a->dst_parent) {
23224+ dir = d_inode(root);
23225+ ii_read_lock_parent3(dir);
23226+ a->h_root = au_hi(dir, a->btgt);
23227+ ii_read_unlock(dir);
23228+ au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3);
23229+ }
23230+ }
4a4d8108
AM
23231+ a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
23232+ a->dst_h_parent, a->dst_hdir);
23233+ udba = au_opt_udba(a->src_dentry->d_sb);
5527c038
JR
23234+ if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
23235+ || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
4a4d8108 23236+ err = au_busy_or_stale();
5afbbe0d 23237+ if (!err && au_dbtop(a->src_dentry) == a->btgt)
4a4d8108 23238+ err = au_h_verify(a->src_h_dentry, udba,
5527c038 23239+ d_inode(a->src_h_parent), a->src_h_parent,
4a4d8108 23240+ a->br);
5afbbe0d 23241+ if (!err && au_dbtop(a->dst_dentry) == a->btgt)
4a4d8108 23242+ err = au_h_verify(a->dst_h_dentry, udba,
5527c038 23243+ d_inode(a->dst_h_parent), a->dst_h_parent,
4a4d8108 23244+ a->br);
86dc4139 23245+ if (!err)
4a4d8108 23246+ goto out; /* success */
4a4d8108
AM
23247+
23248+ err = au_busy_or_stale();
4a4d8108 23249+ au_ren_unlock(a);
86dc4139 23250+
4f0767ce 23251+out:
4a4d8108 23252+ return err;
1facf9fc 23253+}
23254+
23255+/* ---------------------------------------------------------------------- */
23256+
4a4d8108 23257+static void au_ren_refresh_dir(struct au_ren_args *a)
1facf9fc 23258+{
4a4d8108 23259+ struct inode *dir;
dece6358 23260+
4a4d8108 23261+ dir = a->dst_dir;
be118d29 23262+ inode_inc_iversion(dir);
f2c43d5f 23263+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
4a4d8108
AM
23264+ /* is this updating defined in POSIX? */
23265+ au_cpup_attr_timesizes(a->src_inode);
23266+ au_cpup_attr_nlink(dir, /*force*/1);
4a4d8108 23267+ }
b912730e 23268+ au_dir_ts(dir, a->btgt);
dece6358 23269+
f2c43d5f
AM
23270+ if (a->exchange) {
23271+ dir = a->src_dir;
be118d29 23272+ inode_inc_iversion(dir);
f2c43d5f
AM
23273+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
23274+ /* is this updating defined in POSIX? */
23275+ au_cpup_attr_timesizes(a->dst_inode);
23276+ au_cpup_attr_nlink(dir, /*force*/1);
23277+ }
23278+ au_dir_ts(dir, a->btgt);
23279+ }
23280+
23281+ if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
4a4d8108 23282+ return;
dece6358 23283+
4a4d8108 23284+ dir = a->src_dir;
be118d29 23285+ inode_inc_iversion(dir);
f2c43d5f 23286+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
4a4d8108 23287+ au_cpup_attr_nlink(dir, /*force*/1);
b912730e 23288+ au_dir_ts(dir, a->btgt);
1facf9fc 23289+}
23290+
4a4d8108 23291+static void au_ren_refresh(struct au_ren_args *a)
1facf9fc 23292+{
5afbbe0d 23293+ aufs_bindex_t bbot, bindex;
4a4d8108
AM
23294+ struct dentry *d, *h_d;
23295+ struct inode *i, *h_i;
23296+ struct super_block *sb;
dece6358 23297+
027c5e7a
AM
23298+ d = a->dst_dentry;
23299+ d_drop(d);
23300+ if (a->h_dst)
23301+ /* already dget-ed by au_ren_or_cpup() */
23302+ au_set_h_dptr(d, a->btgt, a->h_dst);
23303+
23304+ i = a->dst_inode;
23305+ if (i) {
f2c43d5f
AM
23306+ if (!a->exchange) {
23307+ if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
23308+ vfsub_drop_nlink(i);
23309+ else {
23310+ vfsub_dead_dir(i);
23311+ au_cpup_attr_timesizes(i);
23312+ }
23313+ au_update_dbrange(d, /*do_put_zero*/1);
23314+ } else
23315+ au_cpup_attr_nlink(i, /*force*/1);
027c5e7a 23316+ } else {
5afbbe0d
AM
23317+ bbot = a->btgt;
23318+ for (bindex = au_dbtop(d); bindex < bbot; bindex++)
027c5e7a 23319+ au_set_h_dptr(d, bindex, NULL);
5afbbe0d
AM
23320+ bbot = au_dbbot(d);
23321+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
027c5e7a
AM
23322+ au_set_h_dptr(d, bindex, NULL);
23323+ au_update_dbrange(d, /*do_put_zero*/0);
23324+ }
23325+
8b6a4947
AM
23326+ if (a->exchange
23327+ || au_ftest_ren(a->auren_flags, DIRREN)) {
23328+ d_drop(a->src_dentry);
23329+ if (au_ftest_ren(a->auren_flags, DIRREN))
23330+ au_set_dbwh(a->src_dentry, -1);
23331+ return;
23332+ }
23333+
4a4d8108 23334+ d = a->src_dentry;
8b6a4947
AM
23335+ au_set_dbwh(d, -1);
23336+ bbot = au_dbbot(d);
23337+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23338+ h_d = au_h_dptr(d, bindex);
23339+ if (h_d)
23340+ au_set_h_dptr(d, bindex, NULL);
23341+ }
23342+ au_set_dbbot(d, a->btgt);
4a4d8108 23343+
8b6a4947
AM
23344+ sb = d->d_sb;
23345+ i = a->src_inode;
23346+ if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
23347+ return; /* success */
4a4d8108 23348+
8b6a4947
AM
23349+ bbot = au_ibbot(i);
23350+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23351+ h_i = au_h_iptr(i, bindex);
23352+ if (h_i) {
23353+ au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
23354+ /* ignore this error */
23355+ au_set_h_iptr(i, bindex, NULL, 0);
4a4d8108
AM
23356+ }
23357+ }
8b6a4947 23358+ au_set_ibbot(i, a->btgt);
1308ab2a 23359+}
dece6358 23360+
4a4d8108
AM
23361+/* ---------------------------------------------------------------------- */
23362+
23363+/* mainly for link(2) and rename(2) */
23364+int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
1308ab2a 23365+{
4a4d8108
AM
23366+ aufs_bindex_t bdiropq, bwh;
23367+ struct dentry *parent;
23368+ struct au_branch *br;
23369+
23370+ parent = dentry->d_parent;
5527c038 23371+ IMustLock(d_inode(parent)); /* dir is locked */
4a4d8108
AM
23372+
23373+ bdiropq = au_dbdiropq(parent);
23374+ bwh = au_dbwh(dentry);
23375+ br = au_sbr(dentry->d_sb, btgt);
23376+ if (au_br_rdonly(br)
23377+ || (0 <= bdiropq && bdiropq < btgt)
23378+ || (0 <= bwh && bwh < btgt))
23379+ btgt = -1;
23380+
23381+ AuDbg("btgt %d\n", btgt);
23382+ return btgt;
1facf9fc 23383+}
23384+
5afbbe0d 23385+/* sets src_btop, dst_btop and btgt */
4a4d8108 23386+static int au_ren_wbr(struct au_ren_args *a)
1facf9fc 23387+{
4a4d8108
AM
23388+ int err;
23389+ struct au_wr_dir_args wr_dir_args = {
23390+ /* .force_btgt = -1, */
23391+ .flags = AuWrDir_ADD_ENTRY
23392+ };
dece6358 23393+
5afbbe0d
AM
23394+ a->src_btop = au_dbtop(a->src_dentry);
23395+ a->dst_btop = au_dbtop(a->dst_dentry);
f2c43d5f
AM
23396+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
23397+ || au_ftest_ren(a->auren_flags, ISDIR_DST))
4a4d8108 23398+ au_fset_wrdir(wr_dir_args.flags, ISDIR);
5afbbe0d
AM
23399+ wr_dir_args.force_btgt = a->src_btop;
23400+ if (a->dst_inode && a->dst_btop < a->src_btop)
23401+ wr_dir_args.force_btgt = a->dst_btop;
4a4d8108
AM
23402+ wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
23403+ err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
23404+ a->btgt = err;
f2c43d5f
AM
23405+ if (a->exchange)
23406+ au_update_dbtop(a->dst_dentry);
dece6358 23407+
4a4d8108 23408+ return err;
1facf9fc 23409+}
23410+
4a4d8108 23411+static void au_ren_dt(struct au_ren_args *a)
1facf9fc 23412+{
4a4d8108
AM
23413+ a->h_path.dentry = a->src_h_parent;
23414+ au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
f2c43d5f 23415+ if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
4a4d8108
AM
23416+ a->h_path.dentry = a->dst_h_parent;
23417+ au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
23418+ }
1facf9fc 23419+
f2c43d5f
AM
23420+ au_fclr_ren(a->auren_flags, DT_DSTDIR);
23421+ if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
23422+ && !a->exchange)
4a4d8108 23423+ return;
dece6358 23424+
4a4d8108
AM
23425+ a->h_path.dentry = a->src_h_dentry;
23426+ au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
5527c038 23427+ if (d_is_positive(a->dst_h_dentry)) {
f2c43d5f 23428+ au_fset_ren(a->auren_flags, DT_DSTDIR);
4a4d8108
AM
23429+ a->h_path.dentry = a->dst_h_dentry;
23430+ au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
23431+ }
1308ab2a 23432+}
dece6358 23433+
4a4d8108 23434+static void au_ren_rev_dt(int err, struct au_ren_args *a)
1308ab2a 23435+{
4a4d8108 23436+ struct dentry *h_d;
febd17d6 23437+ struct inode *h_inode;
4a4d8108
AM
23438+
23439+ au_dtime_revert(a->src_dt + AuPARENT);
f2c43d5f 23440+ if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
4a4d8108
AM
23441+ au_dtime_revert(a->dst_dt + AuPARENT);
23442+
f2c43d5f 23443+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
4a4d8108 23444+ h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
febd17d6
JR
23445+ h_inode = d_inode(h_d);
23446+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 23447+ au_dtime_revert(a->src_dt + AuCHILD);
febd17d6 23448+ inode_unlock(h_inode);
4a4d8108 23449+
f2c43d5f 23450+ if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
4a4d8108 23451+ h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
febd17d6
JR
23452+ h_inode = d_inode(h_d);
23453+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 23454+ au_dtime_revert(a->dst_dt + AuCHILD);
febd17d6 23455+ inode_unlock(h_inode);
1facf9fc 23456+ }
23457+ }
23458+}
23459+
4a4d8108
AM
23460+/* ---------------------------------------------------------------------- */
23461+
0b2a12c6
JR
23462+int aufs_rename(struct user_namespace *userns,
23463+ struct inode *_src_dir, struct dentry *_src_dentry,
f2c43d5f
AM
23464+ struct inode *_dst_dir, struct dentry *_dst_dentry,
23465+ unsigned int _flags)
1facf9fc 23466+{
f2c43d5f 23467+ int err, lock_flags;
8b6a4947 23468+ void *rev;
4a4d8108
AM
23469+ /* reduce stack space */
23470+ struct au_ren_args *a;
f2c43d5f 23471+ struct au_pin pin;
4a4d8108 23472+
f2c43d5f 23473+ AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
4a4d8108
AM
23474+ IMustLock(_src_dir);
23475+ IMustLock(_dst_dir);
23476+
f2c43d5f
AM
23477+ err = -EINVAL;
23478+ if (unlikely(_flags & RENAME_WHITEOUT))
23479+ goto out;
23480+
4a4d8108
AM
23481+ err = -ENOMEM;
23482+ BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
23483+ a = kzalloc(sizeof(*a), GFP_NOFS);
23484+ if (unlikely(!a))
23485+ goto out;
23486+
f2c43d5f 23487+ a->flags = _flags;
acd2b654
AM
23488+ BUILD_BUG_ON(sizeof(a->exchange) == sizeof(u8)
23489+ && RENAME_EXCHANGE > U8_MAX);
f2c43d5f 23490+ a->exchange = _flags & RENAME_EXCHANGE;
4a4d8108
AM
23491+ a->src_dir = _src_dir;
23492+ a->src_dentry = _src_dentry;
5527c038
JR
23493+ a->src_inode = NULL;
23494+ if (d_really_is_positive(a->src_dentry))
23495+ a->src_inode = d_inode(a->src_dentry);
4a4d8108
AM
23496+ a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
23497+ a->dst_dir = _dst_dir;
23498+ a->dst_dentry = _dst_dentry;
5527c038
JR
23499+ a->dst_inode = NULL;
23500+ if (d_really_is_positive(a->dst_dentry))
23501+ a->dst_inode = d_inode(a->dst_dentry);
4a4d8108
AM
23502+ a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
23503+ if (a->dst_inode) {
f2c43d5f
AM
23504+ /*
23505+ * if EXCHANGE && src is non-dir && dst is dir,
23506+ * dst is not locked.
23507+ */
23508+ /* IMustLock(a->dst_inode); */
4a4d8108 23509+ au_igrab(a->dst_inode);
1facf9fc 23510+ }
1facf9fc 23511+
4a4d8108 23512+ err = -ENOTDIR;
f2c43d5f 23513+ lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
2000de60 23514+ if (d_is_dir(a->src_dentry)) {
f2c43d5f
AM
23515+ au_fset_ren(a->auren_flags, ISDIR_SRC);
23516+ if (unlikely(!a->exchange
23517+ && d_really_is_positive(a->dst_dentry)
2000de60 23518+ && !d_is_dir(a->dst_dentry)))
4a4d8108 23519+ goto out_free;
f2c43d5f
AM
23520+ lock_flags |= AuLock_DIRS;
23521+ }
23522+ if (a->dst_inode && d_is_dir(a->dst_dentry)) {
23523+ au_fset_ren(a->auren_flags, ISDIR_DST);
23524+ if (unlikely(!a->exchange
23525+ && d_really_is_positive(a->src_dentry)
23526+ && !d_is_dir(a->src_dentry)))
23527+ goto out_free;
23528+ lock_flags |= AuLock_DIRS;
b95c5147 23529+ }
8b6a4947
AM
23530+ err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
23531+ lock_flags);
e49829fe
JR
23532+ if (unlikely(err))
23533+ goto out_free;
1facf9fc 23534+
027c5e7a
AM
23535+ err = au_d_hashed_positive(a->src_dentry);
23536+ if (unlikely(err))
23537+ goto out_unlock;
23538+ err = -ENOENT;
23539+ if (a->dst_inode) {
23540+ /*
f2c43d5f 23541+ * If it is a dir, VFS unhash it before this
027c5e7a
AM
23542+ * function. It means we cannot rely upon d_unhashed().
23543+ */
23544+ if (unlikely(!a->dst_inode->i_nlink))
23545+ goto out_unlock;
f2c43d5f 23546+ if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
027c5e7a 23547+ err = au_d_hashed_positive(a->dst_dentry);
f2c43d5f 23548+ if (unlikely(err && !a->exchange))
027c5e7a
AM
23549+ goto out_unlock;
23550+ } else if (unlikely(IS_DEADDIR(a->dst_inode)))
23551+ goto out_unlock;
23552+ } else if (unlikely(d_unhashed(a->dst_dentry)))
23553+ goto out_unlock;
23554+
7eafdf33
AM
23555+ /*
23556+ * is it possible?
79b8bda9 23557+ * yes, it happened (in linux-3.3-rcN) but I don't know why.
7eafdf33
AM
23558+ * there may exist a problem somewhere else.
23559+ */
23560+ err = -EINVAL;
5527c038 23561+ if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
7eafdf33
AM
23562+ goto out_unlock;
23563+
f2c43d5f 23564+ au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
4a4d8108 23565+ di_write_lock_parent(a->dst_parent);
1facf9fc 23566+
4a4d8108
AM
23567+ /* which branch we process */
23568+ err = au_ren_wbr(a);
23569+ if (unlikely(err < 0))
027c5e7a 23570+ goto out_parent;
4a4d8108 23571+ a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
86dc4139 23572+ a->h_path.mnt = au_br_mnt(a->br);
1facf9fc 23573+
4a4d8108
AM
23574+ /* are they available to be renamed */
23575+ err = au_ren_may_dir(a);
23576+ if (unlikely(err))
23577+ goto out_children;
1facf9fc 23578+
4a4d8108 23579+ /* prepare the writable parent dir on the same branch */
5afbbe0d 23580+ if (a->dst_btop == a->btgt) {
f2c43d5f 23581+ au_fset_ren(a->auren_flags, WHDST);
4a4d8108
AM
23582+ } else {
23583+ err = au_cpup_dirs(a->dst_dentry, a->btgt);
23584+ if (unlikely(err))
23585+ goto out_children;
23586+ }
1facf9fc 23587+
f2c43d5f
AM
23588+ err = 0;
23589+ if (!a->exchange) {
23590+ if (a->src_dir != a->dst_dir) {
23591+ /*
23592+ * this temporary unlock is safe,
23593+ * because both dir->i_mutex are locked.
23594+ */
23595+ di_write_unlock(a->dst_parent);
23596+ di_write_lock_parent(a->src_parent);
23597+ err = au_wr_dir_need_wh(a->src_dentry,
23598+ au_ftest_ren(a->auren_flags,
23599+ ISDIR_SRC),
23600+ &a->btgt);
23601+ di_write_unlock(a->src_parent);
23602+ di_write_lock2_parent(a->src_parent, a->dst_parent,
23603+ /*isdir*/1);
23604+ au_fclr_ren(a->auren_flags, ISSAMEDIR);
23605+ } else
23606+ err = au_wr_dir_need_wh(a->src_dentry,
23607+ au_ftest_ren(a->auren_flags,
23608+ ISDIR_SRC),
23609+ &a->btgt);
23610+ }
4a4d8108
AM
23611+ if (unlikely(err < 0))
23612+ goto out_children;
23613+ if (err)
f2c43d5f 23614+ au_fset_ren(a->auren_flags, WHSRC);
1facf9fc 23615+
86dc4139 23616+ /* cpup src */
5afbbe0d 23617+ if (a->src_btop != a->btgt) {
86dc4139
AM
23618+ err = au_pin(&pin, a->src_dentry, a->btgt,
23619+ au_opt_udba(a->src_dentry->d_sb),
23620+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
367653fa 23621+ if (!err) {
c2b27bf2
AM
23622+ struct au_cp_generic cpg = {
23623+ .dentry = a->src_dentry,
23624+ .bdst = a->btgt,
5afbbe0d 23625+ .bsrc = a->src_btop,
c2b27bf2
AM
23626+ .len = -1,
23627+ .pin = &pin,
23628+ .flags = AuCpup_DTIME | AuCpup_HOPEN
23629+ };
5afbbe0d 23630+ AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
c2b27bf2 23631+ err = au_sio_cpup_simple(&cpg);
367653fa 23632+ au_unpin(&pin);
86dc4139 23633+ }
86dc4139
AM
23634+ if (unlikely(err))
23635+ goto out_children;
5afbbe0d 23636+ a->src_btop = a->btgt;
86dc4139 23637+ a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
f2c43d5f
AM
23638+ if (!a->exchange)
23639+ au_fset_ren(a->auren_flags, WHSRC);
23640+ }
23641+
23642+ /* cpup dst */
23643+ if (a->exchange && a->dst_inode
23644+ && a->dst_btop != a->btgt) {
23645+ err = au_pin(&pin, a->dst_dentry, a->btgt,
23646+ au_opt_udba(a->dst_dentry->d_sb),
23647+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23648+ if (!err) {
23649+ struct au_cp_generic cpg = {
23650+ .dentry = a->dst_dentry,
23651+ .bdst = a->btgt,
23652+ .bsrc = a->dst_btop,
23653+ .len = -1,
23654+ .pin = &pin,
23655+ .flags = AuCpup_DTIME | AuCpup_HOPEN
23656+ };
23657+ err = au_sio_cpup_simple(&cpg);
23658+ au_unpin(&pin);
23659+ }
23660+ if (unlikely(err))
23661+ goto out_children;
23662+ a->dst_btop = a->btgt;
23663+ a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
86dc4139
AM
23664+ }
23665+
4a4d8108
AM
23666+ /* lock them all */
23667+ err = au_ren_lock(a);
23668+ if (unlikely(err))
86dc4139 23669+ /* leave the copied-up one */
4a4d8108 23670+ goto out_children;
1facf9fc 23671+
f2c43d5f
AM
23672+ if (!a->exchange) {
23673+ if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
23674+ err = au_may_ren(a);
23675+ else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
23676+ err = -ENAMETOOLONG;
23677+ if (unlikely(err))
23678+ goto out_hdir;
23679+ }
1facf9fc 23680+
4a4d8108
AM
23681+ /* store timestamps to be revertible */
23682+ au_ren_dt(a);
1facf9fc 23683+
8b6a4947
AM
23684+ /* store dirren info */
23685+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
23686+ err = au_dr_rename(a->src_dentry, a->btgt,
23687+ &a->dst_dentry->d_name, &rev);
23688+ AuTraceErr(err);
23689+ if (unlikely(err))
23690+ goto out_dt;
23691+ }
23692+
4a4d8108
AM
23693+ /* here we go */
23694+ err = do_rename(a);
23695+ if (unlikely(err))
8b6a4947
AM
23696+ goto out_dirren;
23697+
23698+ if (au_ftest_ren(a->auren_flags, DIRREN))
23699+ au_dr_rename_fin(a->src_dentry, a->btgt, rev);
4a4d8108
AM
23700+
23701+ /* update dir attributes */
23702+ au_ren_refresh_dir(a);
23703+
23704+ /* dput/iput all lower dentries */
23705+ au_ren_refresh(a);
23706+
23707+ goto out_hdir; /* success */
23708+
8b6a4947
AM
23709+out_dirren:
23710+ if (au_ftest_ren(a->auren_flags, DIRREN))
23711+ au_dr_rename_rev(a->src_dentry, a->btgt, rev);
4f0767ce 23712+out_dt:
4a4d8108 23713+ au_ren_rev_dt(err, a);
4f0767ce 23714+out_hdir:
4a4d8108 23715+ au_ren_unlock(a);
4f0767ce 23716+out_children:
4a4d8108 23717+ au_nhash_wh_free(&a->whlist);
5afbbe0d
AM
23718+ if (err && a->dst_inode && a->dst_btop != a->btgt) {
23719+ AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
027c5e7a 23720+ au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
5afbbe0d 23721+ au_set_dbtop(a->dst_dentry, a->dst_btop);
4a4d8108 23722+ }
027c5e7a 23723+out_parent:
f2c43d5f 23724+ if (!err) {
8b6a4947
AM
23725+ if (d_unhashed(a->src_dentry))
23726+ au_fset_ren(a->auren_flags, DROPPED_SRC);
23727+ if (d_unhashed(a->dst_dentry))
23728+ au_fset_ren(a->auren_flags, DROPPED_DST);
f2c43d5f
AM
23729+ if (!a->exchange)
23730+ d_move(a->src_dentry, a->dst_dentry);
8b6a4947 23731+ else {
f2c43d5f 23732+ d_exchange(a->src_dentry, a->dst_dentry);
8b6a4947
AM
23733+ if (au_ftest_ren(a->auren_flags, DROPPED_DST))
23734+ d_drop(a->dst_dentry);
23735+ }
23736+ if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
23737+ d_drop(a->src_dentry);
f2c43d5f 23738+ } else {
5afbbe0d 23739+ au_update_dbtop(a->dst_dentry);
027c5e7a
AM
23740+ if (!a->dst_inode)
23741+ d_drop(a->dst_dentry);
23742+ }
f2c43d5f 23743+ if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
4a4d8108
AM
23744+ di_write_unlock(a->dst_parent);
23745+ else
23746+ di_write_unlock2(a->src_parent, a->dst_parent);
027c5e7a 23747+out_unlock:
4a4d8108 23748+ aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
4f0767ce 23749+out_free:
4a4d8108
AM
23750+ iput(a->dst_inode);
23751+ if (a->thargs)
23752+ au_whtmp_rmdir_free(a->thargs);
9f237c51 23753+ au_kfree_rcu(a);
4f0767ce 23754+out:
4a4d8108
AM
23755+ AuTraceErr(err);
23756+ return err;
1308ab2a 23757+}
7f207e10 23758diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
eca34b5c 23759--- /usr/share/empty/fs/aufs/Kconfig 1970-01-01 01:00:00.000000000 +0100
46016270 23760+++ linux/fs/aufs/Kconfig 2021-02-24 13:33:42.741013619 +0100
2121bcd9
AM
23761@@ -0,0 +1,199 @@
23762+# SPDX-License-Identifier: GPL-2.0
4a4d8108
AM
23763+config AUFS_FS
23764+ tristate "Aufs (Advanced multi layered unification filesystem) support"
4a4d8108
AM
23765+ help
23766+ Aufs is a stackable unification filesystem such as Unionfs,
23767+ which unifies several directories and provides a merged single
23768+ directory.
23769+ In the early days, aufs was entirely re-designed and
23770+ re-implemented Unionfs Version 1.x series. Introducing many
23771+ original ideas, approaches and improvements, it becomes totally
23772+ different from Unionfs while keeping the basic features.
1facf9fc 23773+
4a4d8108
AM
23774+if AUFS_FS
23775+choice
23776+ prompt "Maximum number of branches"
23777+ default AUFS_BRANCH_MAX_127
23778+ help
23779+ Specifies the maximum number of branches (or member directories)
23780+ in a single aufs. The larger value consumes more system
23781+ resources and has a minor impact to performance.
23782+config AUFS_BRANCH_MAX_127
23783+ bool "127"
23784+ help
23785+ Specifies the maximum number of branches (or member directories)
23786+ in a single aufs. The larger value consumes more system
23787+ resources and has a minor impact to performance.
23788+config AUFS_BRANCH_MAX_511
23789+ bool "511"
23790+ help
23791+ Specifies the maximum number of branches (or member directories)
23792+ in a single aufs. The larger value consumes more system
23793+ resources and has a minor impact to performance.
23794+config AUFS_BRANCH_MAX_1023
23795+ bool "1023"
23796+ help
23797+ Specifies the maximum number of branches (or member directories)
23798+ in a single aufs. The larger value consumes more system
23799+ resources and has a minor impact to performance.
23800+config AUFS_BRANCH_MAX_32767
23801+ bool "32767"
23802+ help
23803+ Specifies the maximum number of branches (or member directories)
23804+ in a single aufs. The larger value consumes more system
23805+ resources and has a minor impact to performance.
23806+endchoice
1facf9fc 23807+
e49829fe
JR
23808+config AUFS_SBILIST
23809+ bool
23810+ depends on AUFS_MAGIC_SYSRQ || PROC_FS
23811+ default y
23812+ help
23813+ Automatic configuration for internal use.
23814+ When aufs supports Magic SysRq or /proc, enabled automatically.
23815+
4a4d8108
AM
23816+config AUFS_HNOTIFY
23817+ bool "Detect direct branch access (bypassing aufs)"
23818+ help
23819+ If you want to modify files on branches directly, eg. bypassing aufs,
23820+ and want aufs to detect the changes of them fully, then enable this
23821+ option and use 'udba=notify' mount option.
7f207e10 23822+ Currently there is only one available configuration, "fsnotify".
4a4d8108
AM
23823+ It will have a negative impact to the performance.
23824+ See detail in aufs.5.
dece6358 23825+
4a4d8108
AM
23826+choice
23827+ prompt "method" if AUFS_HNOTIFY
23828+ default AUFS_HFSNOTIFY
23829+config AUFS_HFSNOTIFY
23830+ bool "fsnotify"
23831+ select FSNOTIFY
4a4d8108 23832+endchoice
1facf9fc 23833+
4a4d8108
AM
23834+config AUFS_EXPORT
23835+ bool "NFS-exportable aufs"
2cbb1c4b 23836+ depends on EXPORTFS
4a4d8108
AM
23837+ help
23838+ If you want to export your mounted aufs via NFS, then enable this
23839+ option. There are several requirements for this configuration.
23840+ See detail in aufs.5.
1facf9fc 23841+
4a4d8108
AM
23842+config AUFS_INO_T_64
23843+ bool
23844+ depends on AUFS_EXPORT
23845+ depends on 64BIT && !(ALPHA || S390)
23846+ default y
23847+ help
23848+ Automatic configuration for internal use.
23849+ /* typedef unsigned long/int __kernel_ino_t */
23850+ /* alpha and s390x are int */
1facf9fc 23851+
c1595e42
JR
23852+config AUFS_XATTR
23853+ bool "support for XATTR/EA (including Security Labels)"
23854+ help
23855+ If your branch fs supports XATTR/EA and you want to make them
23856+ available in aufs too, then enable this opsion and specify the
23857+ branch attributes for EA.
23858+ See detail in aufs.5.
23859+
076b876e
AM
23860+config AUFS_FHSM
23861+ bool "File-based Hierarchical Storage Management"
23862+ help
23863+ Hierarchical Storage Management (or HSM) is a well-known feature
23864+ in the storage world. Aufs provides this feature as file-based.
23865+ with multiple branches.
23866+ These multiple branches are prioritized, ie. the topmost one
23867+ should be the fastest drive and be used heavily.
23868+
4a4d8108
AM
23869+config AUFS_RDU
23870+ bool "Readdir in userspace"
23871+ help
23872+ Aufs has two methods to provide a merged view for a directory,
23873+ by a user-space library and by kernel-space natively. The latter
23874+ is always enabled but sometimes large and slow.
23875+ If you enable this option, install the library in aufs2-util
23876+ package, and set some environment variables for your readdir(3),
23877+ then the work will be handled in user-space which generally
23878+ shows better performance in most cases.
23879+ See detail in aufs.5.
1facf9fc 23880+
8b6a4947
AM
23881+config AUFS_DIRREN
23882+ bool "Workaround for rename(2)-ing a directory"
23883+ help
23884+ By default, aufs returns EXDEV error in renameing a dir who has
23885+ his child on the lower branch, since it is a bad idea to issue
23886+ rename(2) internally for every lower branch. But user may not
23887+ accept this behaviour. So here is a workaround to allow such
23888+ rename(2) and store some extra infromation on the writable
23889+ branch. Obviously this costs high (and I don't like it).
23890+ To use this feature, you need to enable this configuration AND
23891+ to specify the mount option `dirren.'
23892+ See details in aufs.5 and the design documents.
23893+
4a4d8108
AM
23894+config AUFS_SHWH
23895+ bool "Show whiteouts"
23896+ help
23897+ If you want to make the whiteouts in aufs visible, then enable
23898+ this option and specify 'shwh' mount option. Although it may
23899+ sounds like philosophy or something, but in technically it
23900+ simply shows the name of whiteout with keeping its behaviour.
1facf9fc 23901+
4a4d8108
AM
23902+config AUFS_BR_RAMFS
23903+ bool "Ramfs (initramfs/rootfs) as an aufs branch"
23904+ help
23905+ If you want to use ramfs as an aufs branch fs, then enable this
23906+ option. Generally tmpfs is recommended.
23907+ Aufs prohibited them to be a branch fs by default, because
23908+ initramfs becomes unusable after switch_root or something
23909+ generally. If you sets initramfs as an aufs branch and boot your
23910+ system by switch_root, you will meet a problem easily since the
23911+ files in initramfs may be inaccessible.
23912+ Unless you are going to use ramfs as an aufs branch fs without
23913+ switch_root or something, leave it N.
1facf9fc 23914+
4a4d8108
AM
23915+config AUFS_BR_FUSE
23916+ bool "Fuse fs as an aufs branch"
23917+ depends on FUSE_FS
23918+ select AUFS_POLL
23919+ help
23920+ If you want to use fuse-based userspace filesystem as an aufs
23921+ branch fs, then enable this option.
23922+ It implements the internal poll(2) operation which is
23923+ implemented by fuse only (curretnly).
1facf9fc 23924+
4a4d8108
AM
23925+config AUFS_POLL
23926+ bool
23927+ help
23928+ Automatic configuration for internal use.
1facf9fc 23929+
4a4d8108
AM
23930+config AUFS_BR_HFSPLUS
23931+ bool "Hfsplus as an aufs branch"
23932+ depends on HFSPLUS_FS
23933+ default y
23934+ help
23935+ If you want to use hfsplus fs as an aufs branch fs, then enable
23936+ this option. This option introduces a small overhead at
23937+ copying-up a file on hfsplus.
1facf9fc 23938+
4a4d8108
AM
23939+config AUFS_BDEV_LOOP
23940+ bool
23941+ depends on BLK_DEV_LOOP
23942+ default y
23943+ help
23944+ Automatic configuration for internal use.
23945+ Convert =[ym] into =y.
1308ab2a 23946+
4a4d8108
AM
23947+config AUFS_DEBUG
23948+ bool "Debug aufs"
23949+ help
23950+ Enable this to compile aufs internal debug code.
23951+ It will have a negative impact to the performance.
23952+
23953+config AUFS_MAGIC_SYSRQ
23954+ bool
23955+ depends on AUFS_DEBUG && MAGIC_SYSRQ
23956+ default y
23957+ help
23958+ Automatic configuration for internal use.
23959+ When aufs supports Magic SysRq, enabled automatically.
23960+endif
acd2b654 23961diff -urN /usr/share/empty/fs/aufs/lcnt.h linux/fs/aufs/lcnt.h
eca34b5c 23962--- /usr/share/empty/fs/aufs/lcnt.h 1970-01-01 01:00:00.000000000 +0100
46016270 23963+++ linux/fs/aufs/lcnt.h 2021-02-24 13:33:42.747680497 +0100
acd2b654
AM
23964@@ -0,0 +1,186 @@
23965+/* SPDX-License-Identifier: GPL-2.0 */
23966+/*
d58c55f2 23967+ * Copyright (C) 2018-2020 Junjiro R. Okajima
acd2b654
AM
23968+ *
23969+ * This program, aufs is free software; you can redistribute it and/or modify
23970+ * it under the terms of the GNU General Public License as published by
23971+ * the Free Software Foundation; either version 2 of the License, or
23972+ * (at your option) any later version.
23973+ *
23974+ * This program is distributed in the hope that it will be useful,
23975+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
23976+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23977+ * GNU General Public License for more details.
23978+ *
23979+ * You should have received a copy of the GNU General Public License
23980+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
23981+ */
23982+
23983+/*
23984+ * simple long counter wrapper
23985+ */
23986+
23987+#ifndef __AUFS_LCNT_H__
23988+#define __AUFS_LCNT_H__
23989+
23990+#ifdef __KERNEL__
23991+
23992+#include "debug.h"
23993+
23994+#define AuLCntATOMIC 1
23995+#define AuLCntPCPUCNT 2
23996+/*
23997+ * why does percpu_refcount require extra synchronize_rcu()s in
23998+ * au_br_do_free()
23999+ */
24000+#define AuLCntPCPUREF 3
24001+
24002+/* #define AuLCntChosen AuLCntATOMIC */
24003+#define AuLCntChosen AuLCntPCPUCNT
24004+/* #define AuLCntChosen AuLCntPCPUREF */
24005+
24006+#if AuLCntChosen == AuLCntATOMIC
24007+#include <linux/atomic.h>
24008+
24009+typedef atomic_long_t au_lcnt_t;
24010+
24011+static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
24012+{
24013+ atomic_long_set(cnt, 0);
24014+ return 0;
24015+}
24016+
24017+static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24018+{
24019+ /* empty */
24020+}
24021+
24022+static inline void au_lcnt_fin(au_lcnt_t *cnt __maybe_unused,
24023+ int do_sync __maybe_unused)
24024+{
24025+ /* empty */
24026+}
24027+
24028+static inline void au_lcnt_inc(au_lcnt_t *cnt)
24029+{
24030+ atomic_long_inc(cnt);
24031+}
24032+
24033+static inline void au_lcnt_dec(au_lcnt_t *cnt)
24034+{
24035+ atomic_long_dec(cnt);
24036+}
24037+
24038+static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
24039+{
24040+ return atomic_long_read(cnt);
24041+}
24042+#endif
24043+
24044+#if AuLCntChosen == AuLCntPCPUCNT
24045+#include <linux/percpu_counter.h>
24046+
24047+typedef struct percpu_counter au_lcnt_t;
24048+
24049+static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
24050+{
24051+ return percpu_counter_init(cnt, 0, GFP_NOFS);
24052+}
24053+
24054+static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24055+{
24056+ /* empty */
24057+}
24058+
24059+static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync __maybe_unused)
24060+{
24061+ percpu_counter_destroy(cnt);
24062+}
24063+
24064+static inline void au_lcnt_inc(au_lcnt_t *cnt)
24065+{
24066+ percpu_counter_inc(cnt);
24067+}
24068+
24069+static inline void au_lcnt_dec(au_lcnt_t *cnt)
24070+{
24071+ percpu_counter_dec(cnt);
24072+}
24073+
24074+static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
24075+{
24076+ s64 n;
24077+
24078+ n = percpu_counter_sum(cnt);
24079+ BUG_ON(n < 0);
24080+ if (LONG_MAX != LLONG_MAX
24081+ && n > LONG_MAX)
24082+ AuWarn1("%s\n", "wrap-around");
24083+
24084+ return n;
24085+}
24086+#endif
24087+
24088+#if AuLCntChosen == AuLCntPCPUREF
24089+#include <linux/percpu-refcount.h>
24090+
24091+typedef struct percpu_ref au_lcnt_t;
24092+
24093+static inline int au_lcnt_init(au_lcnt_t *cnt, percpu_ref_func_t *release)
24094+{
24095+ if (!release)
24096+ release = percpu_ref_exit;
24097+ return percpu_ref_init(cnt, release, /*percpu mode*/0, GFP_NOFS);
24098+}
24099+
24100+static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24101+{
24102+ synchronize_rcu();
24103+}
24104+
24105+static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync)
24106+{
24107+ percpu_ref_kill(cnt);
24108+ if (do_sync)
24109+ au_lcnt_wait_for_fin(cnt);
24110+}
24111+
24112+static inline void au_lcnt_inc(au_lcnt_t *cnt)
24113+{
24114+ percpu_ref_get(cnt);
24115+}
24116+
24117+static inline void au_lcnt_dec(au_lcnt_t *cnt)
24118+{
24119+ percpu_ref_put(cnt);
24120+}
24121+
24122+/*
24123+ * avoid calling this func as possible.
24124+ */
24125+static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev)
24126+{
24127+ long l;
24128+
24129+ percpu_ref_switch_to_atomic_sync(cnt);
24130+ l = atomic_long_read(&cnt->count);
24131+ if (do_rev)
24132+ percpu_ref_switch_to_percpu(cnt);
24133+
24134+ /* percpu_ref is initialized by 1 instead of 0 */
24135+ return l - 1;
24136+}
24137+#endif
24138+
24139+#ifdef CONFIG_AUFS_DEBUG
24140+#define AuLCntZero(val) do { \
24141+ long l = val; \
24142+ if (l) \
24143+ AuDbg("%s = %ld\n", #val, l); \
24144+} while (0)
24145+#else
24146+#define AuLCntZero(val) do {} while (0)
24147+#endif
24148+
24149+#endif /* __KERNEL__ */
24150+#endif /* __AUFS_LCNT_H__ */
7f207e10 24151diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
eca34b5c 24152--- /usr/share/empty/fs/aufs/loop.c 1970-01-01 01:00:00.000000000 +0100
46016270 24153+++ linux/fs/aufs/loop.c 2021-02-24 13:33:42.747680497 +0100
062440b3 24154@@ -0,0 +1,148 @@
cd7a4cd9 24155+// SPDX-License-Identifier: GPL-2.0
1facf9fc 24156+/*
d58c55f2 24157+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 24158+ *
24159+ * This program, aufs is free software; you can redistribute it and/or modify
24160+ * it under the terms of the GNU General Public License as published by
24161+ * the Free Software Foundation; either version 2 of the License, or
24162+ * (at your option) any later version.
dece6358
AM
24163+ *
24164+ * This program is distributed in the hope that it will be useful,
24165+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24166+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24167+ * GNU General Public License for more details.
24168+ *
24169+ * You should have received a copy of the GNU General Public License
523b37e3 24170+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24171+ */
24172+
24173+/*
24174+ * support for loopback block device as a branch
24175+ */
24176+
1facf9fc 24177+#include "aufs.h"
24178+
392086de
AM
24179+/* added into drivers/block/loop.c */
24180+static struct file *(*backing_file_func)(struct super_block *sb);
24181+
1facf9fc 24182+/*
24183+ * test if two lower dentries have overlapping branches.
24184+ */
b752ccd1 24185+int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
1facf9fc 24186+{
b752ccd1 24187+ struct super_block *h_sb;
392086de
AM
24188+ struct file *backing_file;
24189+
24190+ if (unlikely(!backing_file_func)) {
24191+ /* don't load "loop" module here */
24192+ backing_file_func = symbol_get(loop_backing_file);
24193+ if (unlikely(!backing_file_func))
24194+ /* "loop" module is not loaded */
24195+ return 0;
24196+ }
1facf9fc 24197+
b752ccd1 24198+ h_sb = h_adding->d_sb;
392086de
AM
24199+ backing_file = backing_file_func(h_sb);
24200+ if (!backing_file)
1facf9fc 24201+ return 0;
24202+
2000de60 24203+ h_adding = backing_file->f_path.dentry;
b752ccd1
AM
24204+ /*
24205+ * h_adding can be local NFS.
24206+ * in this case aufs cannot detect the loop.
24207+ */
24208+ if (unlikely(h_adding->d_sb == sb))
1facf9fc 24209+ return 1;
b752ccd1 24210+ return !!au_test_subdir(h_adding, sb->s_root);
1facf9fc 24211+}
24212+
24213+/* true if a kernel thread named 'loop[0-9].*' accesses a file */
24214+int au_test_loopback_kthread(void)
24215+{
b752ccd1
AM
24216+ int ret;
24217+ struct task_struct *tsk = current;
a2a7ad62 24218+ char c, comm[sizeof(tsk->comm)];
b752ccd1
AM
24219+
24220+ ret = 0;
24221+ if (tsk->flags & PF_KTHREAD) {
a2a7ad62
AM
24222+ get_task_comm(comm, tsk);
24223+ c = comm[4];
b752ccd1 24224+ ret = ('0' <= c && c <= '9'
a2a7ad62 24225+ && !strncmp(comm, "loop", 4));
b752ccd1 24226+ }
1facf9fc 24227+
b752ccd1 24228+ return ret;
1facf9fc 24229+}
87a755f4
AM
24230+
24231+/* ---------------------------------------------------------------------- */
24232+
24233+#define au_warn_loopback_step 16
24234+static int au_warn_loopback_nelem = au_warn_loopback_step;
24235+static unsigned long *au_warn_loopback_array;
24236+
24237+void au_warn_loopback(struct super_block *h_sb)
24238+{
24239+ int i, new_nelem;
24240+ unsigned long *a, magic;
24241+ static DEFINE_SPINLOCK(spin);
24242+
24243+ magic = h_sb->s_magic;
24244+ spin_lock(&spin);
24245+ a = au_warn_loopback_array;
24246+ for (i = 0; i < au_warn_loopback_nelem && *a; i++)
24247+ if (a[i] == magic) {
24248+ spin_unlock(&spin);
24249+ return;
24250+ }
24251+
24252+ /* h_sb is new to us, print it */
24253+ if (i < au_warn_loopback_nelem) {
24254+ a[i] = magic;
24255+ goto pr;
24256+ }
24257+
24258+ /* expand the array */
24259+ new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
24260+ a = au_kzrealloc(au_warn_loopback_array,
24261+ au_warn_loopback_nelem * sizeof(unsigned long),
e2f27e51
AM
24262+ new_nelem * sizeof(unsigned long), GFP_ATOMIC,
24263+ /*may_shrink*/0);
87a755f4
AM
24264+ if (a) {
24265+ au_warn_loopback_nelem = new_nelem;
24266+ au_warn_loopback_array = a;
24267+ a[i] = magic;
24268+ goto pr;
24269+ }
24270+
24271+ spin_unlock(&spin);
24272+ AuWarn1("realloc failed, ignored\n");
24273+ return;
24274+
24275+pr:
24276+ spin_unlock(&spin);
0c3ec466
AM
24277+ pr_warn("you may want to try another patch for loopback file "
24278+ "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
87a755f4
AM
24279+}
24280+
24281+int au_loopback_init(void)
24282+{
24283+ int err;
24284+ struct super_block *sb __maybe_unused;
24285+
fbc438ed 24286+ BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(*au_warn_loopback_array));
87a755f4
AM
24287+
24288+ err = 0;
24289+ au_warn_loopback_array = kcalloc(au_warn_loopback_step,
24290+ sizeof(unsigned long), GFP_NOFS);
24291+ if (unlikely(!au_warn_loopback_array))
24292+ err = -ENOMEM;
24293+
24294+ return err;
24295+}
24296+
24297+void au_loopback_fin(void)
24298+{
79b8bda9
AM
24299+ if (backing_file_func)
24300+ symbol_put(loop_backing_file);
9f237c51 24301+ au_kfree_try_rcu(au_warn_loopback_array);
87a755f4 24302+}
7f207e10 24303diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
eca34b5c 24304--- /usr/share/empty/fs/aufs/loop.h 1970-01-01 01:00:00.000000000 +0100
46016270 24305+++ linux/fs/aufs/loop.h 2021-02-24 13:33:42.747680497 +0100
eca801bf 24306@@ -0,0 +1,55 @@
062440b3 24307+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 24308+/*
d58c55f2 24309+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 24310+ *
24311+ * This program, aufs is free software; you can redistribute it and/or modify
24312+ * it under the terms of the GNU General Public License as published by
24313+ * the Free Software Foundation; either version 2 of the License, or
24314+ * (at your option) any later version.
dece6358
AM
24315+ *
24316+ * This program is distributed in the hope that it will be useful,
24317+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24318+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24319+ * GNU General Public License for more details.
24320+ *
24321+ * You should have received a copy of the GNU General Public License
523b37e3 24322+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24323+ */
24324+
24325+/*
24326+ * support for loopback mount as a branch
24327+ */
24328+
24329+#ifndef __AUFS_LOOP_H__
24330+#define __AUFS_LOOP_H__
24331+
24332+#ifdef __KERNEL__
24333+
dece6358
AM
24334+struct dentry;
24335+struct super_block;
1facf9fc 24336+
24337+#ifdef CONFIG_AUFS_BDEV_LOOP
392086de
AM
24338+/* drivers/block/loop.c */
24339+struct file *loop_backing_file(struct super_block *sb);
24340+
1facf9fc 24341+/* loop.c */
b752ccd1 24342+int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
1facf9fc 24343+int au_test_loopback_kthread(void);
87a755f4
AM
24344+void au_warn_loopback(struct super_block *h_sb);
24345+
24346+int au_loopback_init(void);
24347+void au_loopback_fin(void);
1facf9fc 24348+#else
83b672a5 24349+AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
eca801bf 24350+
4a4d8108 24351+AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
b752ccd1 24352+ struct dentry *h_adding)
4a4d8108 24353+AuStubInt0(au_test_loopback_kthread, void)
87a755f4
AM
24354+AuStubVoid(au_warn_loopback, struct super_block *h_sb)
24355+
24356+AuStubInt0(au_loopback_init, void)
24357+AuStubVoid(au_loopback_fin, void)
1facf9fc 24358+#endif /* BLK_DEV_LOOP */
24359+
24360+#endif /* __KERNEL__ */
24361+#endif /* __AUFS_LOOP_H__ */
7f207e10 24362diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
eca34b5c 24363--- /usr/share/empty/fs/aufs/magic.mk 1970-01-01 01:00:00.000000000 +0100
46016270 24364+++ linux/fs/aufs/magic.mk 2021-02-24 13:33:42.747680497 +0100
2121bcd9
AM
24365@@ -0,0 +1,31 @@
24366+# SPDX-License-Identifier: GPL-2.0
1facf9fc 24367+
24368+# defined in ${srctree}/fs/fuse/inode.c
24369+# tristate
24370+ifdef CONFIG_FUSE_FS
24371+ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
24372+endif
24373+
1facf9fc 24374+# defined in ${srctree}/fs/xfs/xfs_sb.h
24375+# tristate
24376+ifdef CONFIG_XFS_FS
24377+ccflags-y += -DXFS_SB_MAGIC=0x58465342
24378+endif
24379+
24380+# defined in ${srctree}/fs/configfs/mount.c
24381+# tristate
24382+ifdef CONFIG_CONFIGFS_FS
24383+ccflags-y += -DCONFIGFS_MAGIC=0x62656570
24384+endif
24385+
1facf9fc 24386+# defined in ${srctree}/fs/ubifs/ubifs.h
24387+# tristate
24388+ifdef CONFIG_UBIFS_FS
24389+ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
24390+endif
4a4d8108
AM
24391+
24392+# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
24393+# tristate
24394+ifdef CONFIG_HFSPLUS_FS
24395+ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
24396+endif
7f207e10 24397diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
eca34b5c 24398--- /usr/share/empty/fs/aufs/Makefile 1970-01-01 01:00:00.000000000 +0100
46016270 24399+++ linux/fs/aufs/Makefile 2021-02-24 13:33:42.741013619 +0100
2121bcd9
AM
24400@@ -0,0 +1,46 @@
24401+# SPDX-License-Identifier: GPL-2.0
4a4d8108
AM
24402+
24403+include ${src}/magic.mk
24404+ifeq (${CONFIG_AUFS_FS},m)
24405+include ${src}/conf.mk
24406+endif
24407+-include ${src}/priv_def.mk
24408+
24409+# cf. include/linux/kernel.h
24410+# enable pr_debug
24411+ccflags-y += -DDEBUG
f6c5ef8b
AM
24412+# sparse requires the full pathname
24413+ifdef M
523b37e3 24414+ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
f6c5ef8b 24415+else
523b37e3 24416+ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
f6c5ef8b 24417+endif
4a4d8108
AM
24418+
24419+obj-$(CONFIG_AUFS_FS) += aufs.o
24420+aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
24421+ wkq.o vfsub.o dcsub.o \
e49829fe 24422+ cpup.o whout.o wbr_policy.o \
4a4d8108
AM
24423+ dinfo.o dentry.o \
24424+ dynop.o \
24425+ finfo.o file.o f_op.o \
24426+ dir.o vdir.o \
24427+ iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
c2b27bf2 24428+ mvdown.o ioctl.o
4a4d8108
AM
24429+
24430+# all are boolean
e49829fe 24431+aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
4a4d8108
AM
24432+aufs-$(CONFIG_SYSFS) += sysfs.o
24433+aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
24434+aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
24435+aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
24436+aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
4a4d8108 24437+aufs-$(CONFIG_AUFS_EXPORT) += export.o
c1595e42
JR
24438+aufs-$(CONFIG_AUFS_XATTR) += xattr.o
24439+aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
8b6a4947 24440+aufs-$(CONFIG_AUFS_DIRREN) += dirren.o
076b876e 24441+aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
4a4d8108
AM
24442+aufs-$(CONFIG_AUFS_POLL) += poll.o
24443+aufs-$(CONFIG_AUFS_RDU) += rdu.o
4a4d8108
AM
24444+aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
24445+aufs-$(CONFIG_AUFS_DEBUG) += debug.o
24446+aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
7f207e10 24447diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
eca34b5c 24448--- /usr/share/empty/fs/aufs/module.c 1970-01-01 01:00:00.000000000 +0100
46016270 24449+++ linux/fs/aufs/module.c 2021-02-24 13:33:42.747680497 +0100
062440b3 24450@@ -0,0 +1,273 @@
cd7a4cd9 24451+// SPDX-License-Identifier: GPL-2.0
1facf9fc 24452+/*
d58c55f2 24453+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 24454+ *
24455+ * This program, aufs is free software; you can redistribute it and/or modify
24456+ * it under the terms of the GNU General Public License as published by
24457+ * the Free Software Foundation; either version 2 of the License, or
24458+ * (at your option) any later version.
dece6358
AM
24459+ *
24460+ * This program is distributed in the hope that it will be useful,
24461+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24462+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24463+ * GNU General Public License for more details.
24464+ *
24465+ * You should have received a copy of the GNU General Public License
523b37e3 24466+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24467+ */
24468+
24469+/*
24470+ * module global variables and operations
24471+ */
24472+
24473+#include <linux/module.h>
24474+#include <linux/seq_file.h>
24475+#include "aufs.h"
24476+
e2f27e51
AM
24477+/* shrinkable realloc */
24478+void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
1facf9fc 24479+{
e2f27e51
AM
24480+ size_t sz;
24481+ int diff;
1facf9fc 24482+
e2f27e51
AM
24483+ sz = 0;
24484+ diff = -1;
24485+ if (p) {
24486+#if 0 /* unused */
24487+ if (!new_sz) {
9f237c51 24488+ au_kfree_rcu(p);
e2f27e51
AM
24489+ p = NULL;
24490+ goto out;
24491+ }
24492+#else
24493+ AuDebugOn(!new_sz);
24494+#endif
24495+ sz = ksize(p);
24496+ diff = au_kmidx_sub(sz, new_sz);
24497+ }
24498+ if (sz && !diff)
24499+ goto out;
24500+
24501+ if (sz < new_sz)
24502+ /* expand or SLOB */
24503+ p = krealloc(p, new_sz, gfp);
24504+ else if (new_sz < sz && may_shrink) {
24505+ /* shrink */
24506+ void *q;
24507+
24508+ q = kmalloc(new_sz, gfp);
24509+ if (q) {
24510+ if (p) {
24511+ memcpy(q, p, new_sz);
9f237c51 24512+ au_kfree_try_rcu(p);
e2f27e51
AM
24513+ }
24514+ p = q;
24515+ } else
24516+ p = NULL;
24517+ }
24518+
24519+out:
24520+ return p;
24521+}
24522+
24523+void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24524+ int may_shrink)
24525+{
24526+ p = au_krealloc(p, new_sz, gfp, may_shrink);
24527+ if (p && new_sz > nused)
1facf9fc 24528+ memset(p + nused, 0, new_sz - nused);
24529+ return p;
24530+}
24531+
24532+/* ---------------------------------------------------------------------- */
1facf9fc 24533+/*
24534+ * aufs caches
24535+ */
1c60b727 24536+struct kmem_cache *au_cache[AuCache_Last];
5afbbe0d
AM
24537+
24538+static void au_cache_fin(void)
24539+{
24540+ int i;
24541+
24542+ /*
24543+ * Make sure all delayed rcu free inodes are flushed before we
24544+ * destroy cache.
24545+ */
24546+ rcu_barrier();
24547+
24548+ /* excluding AuCache_HNOTIFY */
24549+ BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
24550+ for (i = 0; i < AuCache_HNOTIFY; i++) {
1c60b727
AM
24551+ kmem_cache_destroy(au_cache[i]);
24552+ au_cache[i] = NULL;
5afbbe0d
AM
24553+ }
24554+}
24555+
1facf9fc 24556+static int __init au_cache_init(void)
24557+{
1c60b727
AM
24558+ au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
24559+ if (au_cache[AuCache_DINFO])
027c5e7a 24560+ /* SLAB_DESTROY_BY_RCU */
1c60b727 24561+ au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
f0c0a007 24562+ au_icntnr_init_once);
1c60b727
AM
24563+ if (au_cache[AuCache_ICNTNR])
24564+ au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
f0c0a007 24565+ au_fi_init_once);
1c60b727
AM
24566+ if (au_cache[AuCache_FINFO])
24567+ au_cache[AuCache_VDIR] = AuCache(au_vdir);
24568+ if (au_cache[AuCache_VDIR])
24569+ au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
24570+ if (au_cache[AuCache_DEHSTR])
1facf9fc 24571+ return 0;
24572+
5afbbe0d 24573+ au_cache_fin();
1facf9fc 24574+ return -ENOMEM;
24575+}
24576+
1facf9fc 24577+/* ---------------------------------------------------------------------- */
24578+
24579+int au_dir_roflags;
24580+
e49829fe 24581+#ifdef CONFIG_AUFS_SBILIST
1e00d052
AM
24582+/*
24583+ * iterate_supers_type() doesn't protect us from
24584+ * remounting (branch management)
24585+ */
8b6a4947 24586+struct hlist_bl_head au_sbilist;
e49829fe
JR
24587+#endif
24588+
1facf9fc 24589+/*
24590+ * functions for module interface.
24591+ */
24592+MODULE_LICENSE("GPL");
24593+/* MODULE_LICENSE("GPL v2"); */
dece6358 24594+MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
1facf9fc 24595+MODULE_DESCRIPTION(AUFS_NAME
24596+ " -- Advanced multi layered unification filesystem");
24597+MODULE_VERSION(AUFS_VERSION);
c06a8ce3 24598+MODULE_ALIAS_FS(AUFS_NAME);
1facf9fc 24599+
1facf9fc 24600+/* this module parameter has no meaning when SYSFS is disabled */
24601+int sysaufs_brs = 1;
24602+MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
cd7a4cd9 24603+module_param_named(brs, sysaufs_brs, int, 0444);
1facf9fc 24604+
076b876e 24605+/* this module parameter has no meaning when USER_NS is disabled */
8cdd5066 24606+bool au_userns;
076b876e 24607+MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
cd7a4cd9 24608+module_param_named(allow_userns, au_userns, bool, 0444);
076b876e 24609+
1facf9fc 24610+/* ---------------------------------------------------------------------- */
24611+
24612+static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
24613+
24614+int au_seq_path(struct seq_file *seq, struct path *path)
24615+{
79b8bda9
AM
24616+ int err;
24617+
24618+ err = seq_path(seq, path, au_esc_chars);
1c60b727 24619+ if (err >= 0)
79b8bda9 24620+ err = 0;
1c60b727 24621+ else
79b8bda9
AM
24622+ err = -ENOMEM;
24623+
24624+ return err;
1facf9fc 24625+}
24626+
24627+/* ---------------------------------------------------------------------- */
24628+
24629+static int __init aufs_init(void)
24630+{
24631+ int err, i;
24632+ char *p;
24633+
24634+ p = au_esc_chars;
24635+ for (i = 1; i <= ' '; i++)
24636+ *p++ = i;
24637+ *p++ = '\\';
24638+ *p++ = '\x7f';
24639+ *p = 0;
24640+
24641+ au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
24642+
b95c5147
AM
24643+ memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
24644+ for (i = 0; i < AuIop_Last; i++)
24645+ aufs_iop_nogetattr[i].getattr = NULL;
24646+
1c60b727 24647+ memset(au_cache, 0, sizeof(au_cache)); /* including hnotify */
f0c0a007 24648+
e49829fe 24649+ au_sbilist_init();
1facf9fc 24650+ sysaufs_brs_init();
24651+ au_debug_init();
4a4d8108 24652+ au_dy_init();
1facf9fc 24653+ err = sysaufs_init();
24654+ if (unlikely(err))
24655+ goto out;
062440b3 24656+ err = dbgaufs_init();
4f0767ce 24657+ if (unlikely(err))
953406b4 24658+ goto out_sysaufs;
062440b3
AM
24659+ err = au_procfs_init();
24660+ if (unlikely(err))
24661+ goto out_dbgaufs;
e49829fe
JR
24662+ err = au_wkq_init();
24663+ if (unlikely(err))
24664+ goto out_procfs;
87a755f4 24665+ err = au_loopback_init();
1facf9fc 24666+ if (unlikely(err))
24667+ goto out_wkq;
87a755f4
AM
24668+ err = au_hnotify_init();
24669+ if (unlikely(err))
24670+ goto out_loopback;
1facf9fc 24671+ err = au_sysrq_init();
24672+ if (unlikely(err))
24673+ goto out_hin;
24674+ err = au_cache_init();
24675+ if (unlikely(err))
24676+ goto out_sysrq;
076b876e
AM
24677+
24678+ aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
1facf9fc 24679+ err = register_filesystem(&aufs_fs_type);
24680+ if (unlikely(err))
24681+ goto out_cache;
076b876e 24682+
4a4d8108
AM
24683+ /* since we define pr_fmt, call printk directly */
24684+ printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
1facf9fc 24685+ goto out; /* success */
24686+
4f0767ce 24687+out_cache:
1facf9fc 24688+ au_cache_fin();
4f0767ce 24689+out_sysrq:
1facf9fc 24690+ au_sysrq_fin();
4f0767ce 24691+out_hin:
4a4d8108 24692+ au_hnotify_fin();
87a755f4
AM
24693+out_loopback:
24694+ au_loopback_fin();
4f0767ce 24695+out_wkq:
1facf9fc 24696+ au_wkq_fin();
e49829fe
JR
24697+out_procfs:
24698+ au_procfs_fin();
062440b3
AM
24699+out_dbgaufs:
24700+ dbgaufs_fin();
4f0767ce 24701+out_sysaufs:
1facf9fc 24702+ sysaufs_fin();
4a4d8108 24703+ au_dy_fin();
4f0767ce 24704+out:
1facf9fc 24705+ return err;
24706+}
24707+
24708+static void __exit aufs_exit(void)
24709+{
24710+ unregister_filesystem(&aufs_fs_type);
24711+ au_cache_fin();
24712+ au_sysrq_fin();
4a4d8108 24713+ au_hnotify_fin();
87a755f4 24714+ au_loopback_fin();
1facf9fc 24715+ au_wkq_fin();
e49829fe 24716+ au_procfs_fin();
062440b3 24717+ dbgaufs_fin();
1facf9fc 24718+ sysaufs_fin();
4a4d8108 24719+ au_dy_fin();
1facf9fc 24720+}
24721+
24722+module_init(aufs_init);
24723+module_exit(aufs_exit);
7f207e10 24724diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
eca34b5c 24725--- /usr/share/empty/fs/aufs/module.h 1970-01-01 01:00:00.000000000 +0100
46016270 24726+++ linux/fs/aufs/module.h 2021-02-24 13:33:42.747680497 +0100
9f237c51 24727@@ -0,0 +1,166 @@
062440b3 24728+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 24729+/*
d58c55f2 24730+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 24731+ *
24732+ * This program, aufs is free software; you can redistribute it and/or modify
24733+ * it under the terms of the GNU General Public License as published by
24734+ * the Free Software Foundation; either version 2 of the License, or
24735+ * (at your option) any later version.
dece6358
AM
24736+ *
24737+ * This program is distributed in the hope that it will be useful,
24738+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24739+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24740+ * GNU General Public License for more details.
24741+ *
24742+ * You should have received a copy of the GNU General Public License
523b37e3 24743+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24744+ */
24745+
24746+/*
24747+ * module initialization and module-global
24748+ */
24749+
24750+#ifndef __AUFS_MODULE_H__
24751+#define __AUFS_MODULE_H__
24752+
24753+#ifdef __KERNEL__
24754+
24755+#include <linux/slab.h>
9f237c51
AM
24756+#include "debug.h"
24757+#include "dentry.h"
24758+#include "dir.h"
24759+#include "file.h"
24760+#include "inode.h"
1facf9fc 24761+
dece6358
AM
24762+struct path;
24763+struct seq_file;
24764+
1facf9fc 24765+/* module parameters */
1facf9fc 24766+extern int sysaufs_brs;
8cdd5066 24767+extern bool au_userns;
1facf9fc 24768+
24769+/* ---------------------------------------------------------------------- */
24770+
24771+extern int au_dir_roflags;
24772+
e2f27e51
AM
24773+void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
24774+void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24775+ int may_shrink);
24776+
9f237c51
AM
24777+/*
24778+ * Comparing the size of the object with sizeof(struct rcu_head)
24779+ * case 1: object is always larger
24780+ * --> au_kfree_rcu() or au_kfree_do_rcu()
24781+ * case 2: object is always smaller
24782+ * --> au_kfree_small()
24783+ * case 3: object can be any size
24784+ * --> au_kfree_try_rcu()
24785+ */
24786+
24787+static inline void au_kfree_do_rcu(const void *p)
24788+{
24789+ struct {
24790+ struct rcu_head rcu;
24791+ } *a = (void *)p;
24792+
24793+ kfree_rcu(a, rcu);
24794+}
24795+
24796+#define au_kfree_rcu(_p) do { \
24797+ typeof(_p) p = (_p); \
24798+ BUILD_BUG_ON(sizeof(*p) < sizeof(struct rcu_head)); \
24799+ if (p) \
24800+ au_kfree_do_rcu(p); \
24801+ } while (0)
24802+
24803+#define au_kfree_do_sz_test(sz) (sz >= sizeof(struct rcu_head))
24804+#define au_kfree_sz_test(p) (p && au_kfree_do_sz_test(ksize(p)))
24805+
24806+static inline void au_kfree_try_rcu(const void *p)
24807+{
24808+ if (!p)
24809+ return;
24810+ if (au_kfree_sz_test(p))
24811+ au_kfree_do_rcu(p);
24812+ else
24813+ kfree(p);
24814+}
24815+
24816+static inline void au_kfree_small(const void *p)
24817+{
24818+ if (!p)
24819+ return;
24820+ AuDebugOn(au_kfree_sz_test(p));
24821+ kfree(p);
24822+}
24823+
e2f27e51
AM
24824+static inline int au_kmidx_sub(size_t sz, size_t new_sz)
24825+{
24826+#ifndef CONFIG_SLOB
24827+ return kmalloc_index(sz) - kmalloc_index(new_sz);
24828+#else
24829+ return -1; /* SLOB is untested */
24830+#endif
24831+}
24832+
1facf9fc 24833+int au_seq_path(struct seq_file *seq, struct path *path);
24834+
e49829fe
JR
24835+#ifdef CONFIG_PROC_FS
24836+/* procfs.c */
24837+int __init au_procfs_init(void);
24838+void au_procfs_fin(void);
24839+#else
24840+AuStubInt0(au_procfs_init, void);
24841+AuStubVoid(au_procfs_fin, void);
24842+#endif
24843+
4f0767ce
JR
24844+/* ---------------------------------------------------------------------- */
24845+
1c60b727 24846+/* kmem cache */
1facf9fc 24847+enum {
24848+ AuCache_DINFO,
24849+ AuCache_ICNTNR,
24850+ AuCache_FINFO,
24851+ AuCache_VDIR,
24852+ AuCache_DEHSTR,
7eafdf33 24853+ AuCache_HNOTIFY, /* must be last */
1facf9fc 24854+ AuCache_Last
24855+};
24856+
1c60b727 24857+extern struct kmem_cache *au_cache[AuCache_Last];
f0c0a007 24858+
4a4d8108
AM
24859+#define AuCacheFlags (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
24860+#define AuCache(type) KMEM_CACHE(type, AuCacheFlags)
24861+#define AuCacheCtor(type, ctor) \
24862+ kmem_cache_create(#type, sizeof(struct type), \
24863+ __alignof__(struct type), AuCacheFlags, ctor)
1facf9fc 24864+
9f237c51
AM
24865+#define AuCacheFuncs(name, index) \
24866+ static inline struct au_##name *au_cache_alloc_##name(void) \
24867+ { return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); } \
24868+ static inline void au_cache_free_##name##_norcu(struct au_##name *p) \
24869+ { kmem_cache_free(au_cache[AuCache_##index], p); } \
24870+ \
24871+ static inline void au_cache_free_##name##_rcu_cb(struct rcu_head *rcu) \
24872+ { void *p = rcu; \
24873+ p -= offsetof(struct au_##name, rcu); \
24874+ kmem_cache_free(au_cache[AuCache_##index], p); } \
24875+ static inline void au_cache_free_##name##_rcu(struct au_##name *p) \
24876+ { BUILD_BUG_ON(sizeof(struct au_##name) < sizeof(struct rcu_head)); \
24877+ call_rcu(&p->rcu, au_cache_free_##name##_rcu_cb); } \
24878+ \
24879+ static inline void au_cache_free_##name(struct au_##name *p) \
24880+ { /* au_cache_free_##name##_norcu(p); */ \
24881+ au_cache_free_##name##_rcu(p); }
1facf9fc 24882+
24883+AuCacheFuncs(dinfo, DINFO);
24884+AuCacheFuncs(icntnr, ICNTNR);
24885+AuCacheFuncs(finfo, FINFO);
24886+AuCacheFuncs(vdir, VDIR);
4a4d8108
AM
24887+AuCacheFuncs(vdir_dehstr, DEHSTR);
24888+#ifdef CONFIG_AUFS_HNOTIFY
24889+AuCacheFuncs(hnotify, HNOTIFY);
24890+#endif
1facf9fc 24891+
4a4d8108
AM
24892+#endif /* __KERNEL__ */
24893+#endif /* __AUFS_MODULE_H__ */
c2b27bf2 24894diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
eca34b5c 24895--- /usr/share/empty/fs/aufs/mvdown.c 1970-01-01 01:00:00.000000000 +0100
46016270 24896+++ linux/fs/aufs/mvdown.c 2021-02-24 13:33:42.747680497 +0100
ba1aed25 24897@@ -0,0 +1,706 @@
cd7a4cd9 24898+// SPDX-License-Identifier: GPL-2.0
c2b27bf2 24899+/*
d58c55f2 24900+ * Copyright (C) 2011-2020 Junjiro R. Okajima
c2b27bf2
AM
24901+ *
24902+ * This program, aufs is free software; you can redistribute it and/or modify
24903+ * it under the terms of the GNU General Public License as published by
24904+ * the Free Software Foundation; either version 2 of the License, or
24905+ * (at your option) any later version.
24906+ *
24907+ * This program is distributed in the hope that it will be useful,
24908+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24909+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24910+ * GNU General Public License for more details.
24911+ *
24912+ * You should have received a copy of the GNU General Public License
523b37e3
AM
24913+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
24914+ */
24915+
24916+/*
24917+ * move-down, opposite of copy-up
c2b27bf2
AM
24918+ */
24919+
24920+#include "aufs.h"
24921+
c2b27bf2
AM
24922+struct au_mvd_args {
24923+ struct {
c2b27bf2
AM
24924+ struct super_block *h_sb;
24925+ struct dentry *h_parent;
24926+ struct au_hinode *hdir;
392086de 24927+ struct inode *h_dir, *h_inode;
c1595e42 24928+ struct au_pin pin;
c2b27bf2
AM
24929+ } info[AUFS_MVDOWN_NARRAY];
24930+
24931+ struct aufs_mvdown mvdown;
24932+ struct dentry *dentry, *parent;
24933+ struct inode *inode, *dir;
24934+ struct super_block *sb;
24935+ aufs_bindex_t bopq, bwh, bfound;
24936+ unsigned char rename_lock;
c2b27bf2
AM
24937+};
24938+
392086de 24939+#define mvd_errno mvdown.au_errno
076b876e
AM
24940+#define mvd_bsrc mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
24941+#define mvd_src_brid mvdown.stbr[AUFS_MVDOWN_UPPER].brid
24942+#define mvd_bdst mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
24943+#define mvd_dst_brid mvdown.stbr[AUFS_MVDOWN_LOWER].brid
c2b27bf2 24944+
392086de
AM
24945+#define mvd_h_src_sb info[AUFS_MVDOWN_UPPER].h_sb
24946+#define mvd_h_src_parent info[AUFS_MVDOWN_UPPER].h_parent
24947+#define mvd_hdir_src info[AUFS_MVDOWN_UPPER].hdir
24948+#define mvd_h_src_dir info[AUFS_MVDOWN_UPPER].h_dir
24949+#define mvd_h_src_inode info[AUFS_MVDOWN_UPPER].h_inode
c1595e42 24950+#define mvd_pin_src info[AUFS_MVDOWN_UPPER].pin
392086de
AM
24951+
24952+#define mvd_h_dst_sb info[AUFS_MVDOWN_LOWER].h_sb
24953+#define mvd_h_dst_parent info[AUFS_MVDOWN_LOWER].h_parent
24954+#define mvd_hdir_dst info[AUFS_MVDOWN_LOWER].hdir
24955+#define mvd_h_dst_dir info[AUFS_MVDOWN_LOWER].h_dir
24956+#define mvd_h_dst_inode info[AUFS_MVDOWN_LOWER].h_inode
c1595e42 24957+#define mvd_pin_dst info[AUFS_MVDOWN_LOWER].pin
c2b27bf2
AM
24958+
24959+#define AU_MVD_PR(flag, ...) do { \
24960+ if (flag) \
24961+ pr_err(__VA_ARGS__); \
24962+ } while (0)
24963+
076b876e
AM
24964+static int find_lower_writable(struct au_mvd_args *a)
24965+{
24966+ struct super_block *sb;
5afbbe0d 24967+ aufs_bindex_t bindex, bbot;
076b876e
AM
24968+ struct au_branch *br;
24969+
24970+ sb = a->sb;
24971+ bindex = a->mvd_bsrc;
5afbbe0d 24972+ bbot = au_sbbot(sb);
076b876e 24973+ if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
5afbbe0d 24974+ for (bindex++; bindex <= bbot; bindex++) {
076b876e
AM
24975+ br = au_sbr(sb, bindex);
24976+ if (au_br_fhsm(br->br_perm)
8b6a4947 24977+ && !sb_rdonly(au_br_sb(br)))
076b876e
AM
24978+ return bindex;
24979+ }
24980+ else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
5afbbe0d 24981+ for (bindex++; bindex <= bbot; bindex++) {
076b876e
AM
24982+ br = au_sbr(sb, bindex);
24983+ if (!au_br_rdonly(br))
24984+ return bindex;
24985+ }
24986+ else
5afbbe0d 24987+ for (bindex++; bindex <= bbot; bindex++) {
076b876e 24988+ br = au_sbr(sb, bindex);
8b6a4947 24989+ if (!sb_rdonly(au_br_sb(br))) {
076b876e
AM
24990+ if (au_br_rdonly(br))
24991+ a->mvdown.flags
24992+ |= AUFS_MVDOWN_ROLOWER_R;
24993+ return bindex;
24994+ }
24995+ }
24996+
24997+ return -1;
24998+}
24999+
c2b27bf2 25000+/* make the parent dir on bdst */
392086de 25001+static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25002+{
25003+ int err;
25004+
25005+ err = 0;
25006+ a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
25007+ a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
25008+ a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
25009+ a->mvd_h_dst_parent = NULL;
5afbbe0d 25010+ if (au_dbbot(a->parent) >= a->mvd_bdst)
c2b27bf2
AM
25011+ a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
25012+ if (!a->mvd_h_dst_parent) {
25013+ err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
25014+ if (unlikely(err)) {
392086de 25015+ AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
c2b27bf2
AM
25016+ goto out;
25017+ }
25018+ a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
25019+ }
25020+
25021+out:
25022+ AuTraceErr(err);
25023+ return err;
25024+}
25025+
25026+/* lock them all */
392086de 25027+static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25028+{
25029+ int err;
25030+ struct dentry *h_trap;
25031+
25032+ a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
25033+ a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
c1595e42
JR
25034+ err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
25035+ au_opt_udba(a->sb),
25036+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25037+ AuTraceErr(err);
25038+ if (unlikely(err)) {
25039+ AU_MVD_PR(dmsg, "pin_dst failed\n");
25040+ goto out;
25041+ }
25042+
c2b27bf2
AM
25043+ if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
25044+ a->rename_lock = 0;
c1595e42
JR
25045+ au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
25046+ AuLsc_DI_PARENT, AuLsc_I_PARENT3,
25047+ au_opt_udba(a->sb),
25048+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25049+ err = au_do_pin(&a->mvd_pin_src);
25050+ AuTraceErr(err);
5527c038 25051+ a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
c1595e42
JR
25052+ if (unlikely(err)) {
25053+ AU_MVD_PR(dmsg, "pin_src failed\n");
25054+ goto out_dst;
25055+ }
25056+ goto out; /* success */
c2b27bf2
AM
25057+ }
25058+
c2b27bf2 25059+ a->rename_lock = 1;
c1595e42
JR
25060+ au_pin_hdir_unlock(&a->mvd_pin_dst);
25061+ err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
25062+ au_opt_udba(a->sb),
25063+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25064+ AuTraceErr(err);
5527c038 25065+ a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
c1595e42
JR
25066+ if (unlikely(err)) {
25067+ AU_MVD_PR(dmsg, "pin_src failed\n");
25068+ au_pin_hdir_lock(&a->mvd_pin_dst);
25069+ goto out_dst;
25070+ }
25071+ au_pin_hdir_unlock(&a->mvd_pin_src);
c2b27bf2
AM
25072+ h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25073+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
25074+ if (h_trap) {
25075+ err = (h_trap != a->mvd_h_src_parent);
25076+ if (err)
25077+ err = (h_trap != a->mvd_h_dst_parent);
25078+ }
25079+ BUG_ON(err); /* it should never happen */
c1595e42
JR
25080+ if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
25081+ err = -EBUSY;
25082+ AuTraceErr(err);
25083+ vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25084+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
25085+ au_pin_hdir_lock(&a->mvd_pin_src);
25086+ au_unpin(&a->mvd_pin_src);
25087+ au_pin_hdir_lock(&a->mvd_pin_dst);
25088+ goto out_dst;
25089+ }
25090+ goto out; /* success */
c2b27bf2 25091+
c1595e42
JR
25092+out_dst:
25093+ au_unpin(&a->mvd_pin_dst);
c2b27bf2
AM
25094+out:
25095+ AuTraceErr(err);
25096+ return err;
25097+}
25098+
392086de 25099+static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2 25100+{
c1595e42
JR
25101+ if (!a->rename_lock)
25102+ au_unpin(&a->mvd_pin_src);
25103+ else {
c2b27bf2
AM
25104+ vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25105+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
c1595e42
JR
25106+ au_pin_hdir_lock(&a->mvd_pin_src);
25107+ au_unpin(&a->mvd_pin_src);
25108+ au_pin_hdir_lock(&a->mvd_pin_dst);
25109+ }
25110+ au_unpin(&a->mvd_pin_dst);
c2b27bf2
AM
25111+}
25112+
25113+/* copy-down the file */
392086de 25114+static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25115+{
25116+ int err;
25117+ struct au_cp_generic cpg = {
25118+ .dentry = a->dentry,
25119+ .bdst = a->mvd_bdst,
25120+ .bsrc = a->mvd_bsrc,
25121+ .len = -1,
c1595e42 25122+ .pin = &a->mvd_pin_dst,
c2b27bf2
AM
25123+ .flags = AuCpup_DTIME | AuCpup_HOPEN
25124+ };
25125+
25126+ AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
392086de
AM
25127+ if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25128+ au_fset_cpup(cpg.flags, OVERWRITE);
25129+ if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
25130+ au_fset_cpup(cpg.flags, RWDST);
c2b27bf2
AM
25131+ err = au_sio_cpdown_simple(&cpg);
25132+ if (unlikely(err))
392086de 25133+ AU_MVD_PR(dmsg, "cpdown failed\n");
c2b27bf2
AM
25134+
25135+ AuTraceErr(err);
25136+ return err;
25137+}
25138+
25139+/*
25140+ * unlink the whiteout on bdst if exist which may be created by UDBA while we
25141+ * were sleeping
25142+ */
392086de 25143+static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25144+{
25145+ int err;
25146+ struct path h_path;
25147+ struct au_branch *br;
523b37e3 25148+ struct inode *delegated;
c2b27bf2
AM
25149+
25150+ br = au_sbr(a->sb, a->mvd_bdst);
25151+ h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
25152+ err = PTR_ERR(h_path.dentry);
25153+ if (IS_ERR(h_path.dentry)) {
392086de 25154+ AU_MVD_PR(dmsg, "wh_lkup failed\n");
c2b27bf2
AM
25155+ goto out;
25156+ }
25157+
25158+ err = 0;
5527c038 25159+ if (d_is_positive(h_path.dentry)) {
c2b27bf2 25160+ h_path.mnt = au_br_mnt(br);
523b37e3 25161+ delegated = NULL;
5527c038 25162+ err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
523b37e3
AM
25163+ &delegated, /*force*/0);
25164+ if (unlikely(err == -EWOULDBLOCK)) {
25165+ pr_warn("cannot retry for NFSv4 delegation"
25166+ " for an internal unlink\n");
25167+ iput(delegated);
25168+ }
c2b27bf2 25169+ if (unlikely(err))
392086de 25170+ AU_MVD_PR(dmsg, "wh_unlink failed\n");
c2b27bf2
AM
25171+ }
25172+ dput(h_path.dentry);
25173+
25174+out:
25175+ AuTraceErr(err);
25176+ return err;
25177+}
25178+
25179+/*
25180+ * unlink the topmost h_dentry
c2b27bf2 25181+ */
392086de 25182+static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25183+{
25184+ int err;
25185+ struct path h_path;
523b37e3 25186+ struct inode *delegated;
c2b27bf2
AM
25187+
25188+ h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
25189+ h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
523b37e3
AM
25190+ delegated = NULL;
25191+ err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
25192+ if (unlikely(err == -EWOULDBLOCK)) {
25193+ pr_warn("cannot retry for NFSv4 delegation"
25194+ " for an internal unlink\n");
25195+ iput(delegated);
25196+ }
c2b27bf2 25197+ if (unlikely(err))
392086de 25198+ AU_MVD_PR(dmsg, "unlink failed\n");
c2b27bf2
AM
25199+
25200+ AuTraceErr(err);
25201+ return err;
25202+}
25203+
076b876e
AM
25204+/* Since mvdown succeeded, we ignore an error of this function */
25205+static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
25206+{
25207+ int err;
25208+ struct au_branch *br;
25209+
25210+ a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
25211+ br = au_sbr(a->sb, a->mvd_bsrc);
25212+ err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
25213+ if (!err) {
25214+ br = au_sbr(a->sb, a->mvd_bdst);
25215+ a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
25216+ err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
25217+ }
25218+ if (!err)
25219+ a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
25220+ else
25221+ AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
25222+}
25223+
c2b27bf2
AM
25224+/*
25225+ * copy-down the file and unlink the bsrc file.
25226+ * - unlink the bdst whout if exist
25227+ * - copy-down the file (with whtmp name and rename)
25228+ * - unlink the bsrc file
25229+ */
392086de 25230+static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25231+{
25232+ int err;
25233+
392086de 25234+ err = au_do_mkdir(dmsg, a);
c2b27bf2 25235+ if (!err)
392086de 25236+ err = au_do_lock(dmsg, a);
c2b27bf2
AM
25237+ if (unlikely(err))
25238+ goto out;
25239+
25240+ /*
25241+ * do not revert the activities we made on bdst since they should be
25242+ * harmless in aufs.
25243+ */
25244+
392086de 25245+ err = au_do_cpdown(dmsg, a);
c2b27bf2 25246+ if (!err)
392086de
AM
25247+ err = au_do_unlink_wh(dmsg, a);
25248+ if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
25249+ err = au_do_unlink(dmsg, a);
c2b27bf2
AM
25250+ if (unlikely(err))
25251+ goto out_unlock;
25252+
c1595e42
JR
25253+ AuDbg("%pd2, 0x%x, %d --> %d\n",
25254+ a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
076b876e
AM
25255+ if (find_lower_writable(a) < 0)
25256+ a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
25257+
25258+ if (a->mvdown.flags & AUFS_MVDOWN_STFS)
25259+ au_do_stfs(dmsg, a);
25260+
c2b27bf2 25261+ /* maintain internal array */
392086de
AM
25262+ if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
25263+ au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
5afbbe0d 25264+ au_set_dbtop(a->dentry, a->mvd_bdst);
392086de 25265+ au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
5afbbe0d 25266+ au_set_ibtop(a->inode, a->mvd_bdst);
79b8bda9
AM
25267+ } else {
25268+ /* hide the lower */
25269+ au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
5afbbe0d 25270+ au_set_dbbot(a->dentry, a->mvd_bsrc);
79b8bda9 25271+ au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
5afbbe0d 25272+ au_set_ibbot(a->inode, a->mvd_bsrc);
392086de 25273+ }
5afbbe0d
AM
25274+ if (au_dbbot(a->dentry) < a->mvd_bdst)
25275+ au_set_dbbot(a->dentry, a->mvd_bdst);
25276+ if (au_ibbot(a->inode) < a->mvd_bdst)
25277+ au_set_ibbot(a->inode, a->mvd_bdst);
c2b27bf2
AM
25278+
25279+out_unlock:
392086de 25280+ au_do_unlock(dmsg, a);
c2b27bf2
AM
25281+out:
25282+ AuTraceErr(err);
25283+ return err;
25284+}
25285+
25286+/* ---------------------------------------------------------------------- */
25287+
c2b27bf2 25288+/* make sure the file is idle */
392086de 25289+static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25290+{
25291+ int err, plinked;
c2b27bf2
AM
25292+
25293+ err = 0;
c2b27bf2 25294+ plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
5afbbe0d 25295+ if (au_dbtop(a->dentry) == a->mvd_bsrc
c1595e42 25296+ && au_dcount(a->dentry) == 1
c2b27bf2 25297+ && atomic_read(&a->inode->i_count) == 1
392086de 25298+ /* && a->mvd_h_src_inode->i_nlink == 1 */
c2b27bf2
AM
25299+ && (!plinked || !au_plink_test(a->inode))
25300+ && a->inode->i_nlink == 1)
25301+ goto out;
25302+
25303+ err = -EBUSY;
392086de 25304+ AU_MVD_PR(dmsg,
c1595e42 25305+ "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
5afbbe0d 25306+ a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
c2b27bf2 25307+ atomic_read(&a->inode->i_count), a->inode->i_nlink,
392086de 25308+ a->mvd_h_src_inode->i_nlink,
c2b27bf2
AM
25309+ plinked, plinked ? au_plink_test(a->inode) : 0);
25310+
25311+out:
25312+ AuTraceErr(err);
25313+ return err;
25314+}
25315+
25316+/* make sure the parent dir is fine */
392086de 25317+static int au_mvd_args_parent(const unsigned char dmsg,
c2b27bf2
AM
25318+ struct au_mvd_args *a)
25319+{
25320+ int err;
25321+ aufs_bindex_t bindex;
25322+
25323+ err = 0;
25324+ if (unlikely(au_alive_dir(a->parent))) {
25325+ err = -ENOENT;
392086de 25326+ AU_MVD_PR(dmsg, "parent dir is dead\n");
c2b27bf2
AM
25327+ goto out;
25328+ }
25329+
25330+ a->bopq = au_dbdiropq(a->parent);
25331+ bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
25332+ AuDbg("b%d\n", bindex);
25333+ if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
25334+ || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
25335+ err = -EINVAL;
392086de
AM
25336+ a->mvd_errno = EAU_MVDOWN_OPAQUE;
25337+ AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
c2b27bf2
AM
25338+ a->bopq, a->mvd_bdst);
25339+ }
25340+
25341+out:
25342+ AuTraceErr(err);
25343+ return err;
25344+}
25345+
392086de 25346+static int au_mvd_args_intermediate(const unsigned char dmsg,
c2b27bf2
AM
25347+ struct au_mvd_args *a)
25348+{
25349+ int err;
25350+ struct au_dinfo *dinfo, *tmp;
25351+
25352+ /* lookup the next lower positive entry */
25353+ err = -ENOMEM;
25354+ tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
25355+ if (unlikely(!tmp))
25356+ goto out;
25357+
25358+ a->bfound = -1;
25359+ a->bwh = -1;
25360+ dinfo = au_di(a->dentry);
25361+ au_di_cp(tmp, dinfo);
25362+ au_di_swap(tmp, dinfo);
25363+
25364+ /* returns the number of positive dentries */
5afbbe0d
AM
25365+ err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
25366+ /* AuLkup_IGNORE_PERM */ 0);
c2b27bf2
AM
25367+ if (!err)
25368+ a->bwh = au_dbwh(a->dentry);
25369+ else if (err > 0)
5afbbe0d 25370+ a->bfound = au_dbtop(a->dentry);
c2b27bf2
AM
25371+
25372+ au_di_swap(tmp, dinfo);
25373+ au_rw_write_unlock(&tmp->di_rwsem);
25374+ au_di_free(tmp);
25375+ if (unlikely(err < 0))
392086de 25376+ AU_MVD_PR(dmsg, "failed look-up lower\n");
c2b27bf2
AM
25377+
25378+ /*
25379+ * here, we have these cases.
25380+ * bfound == -1
25381+ * no positive dentry under bsrc. there are more sub-cases.
25382+ * bwh < 0
25383+ * there no whiteout, we can safely move-down.
25384+ * bwh <= bsrc
25385+ * impossible
25386+ * bsrc < bwh && bwh < bdst
25387+ * there is a whiteout on RO branch. cannot proceed.
25388+ * bwh == bdst
25389+ * there is a whiteout on the RW target branch. it should
25390+ * be removed.
25391+ * bdst < bwh
25392+ * there is a whiteout somewhere unrelated branch.
25393+ * -1 < bfound && bfound <= bsrc
25394+ * impossible.
25395+ * bfound < bdst
25396+ * found, but it is on RO branch between bsrc and bdst. cannot
25397+ * proceed.
25398+ * bfound == bdst
25399+ * found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
25400+ * error.
25401+ * bdst < bfound
25402+ * found, after we create the file on bdst, it will be hidden.
25403+ */
25404+
25405+ AuDebugOn(a->bfound == -1
25406+ && a->bwh != -1
25407+ && a->bwh <= a->mvd_bsrc);
25408+ AuDebugOn(-1 < a->bfound
25409+ && a->bfound <= a->mvd_bsrc);
25410+
25411+ err = -EINVAL;
25412+ if (a->bfound == -1
25413+ && a->mvd_bsrc < a->bwh
25414+ && a->bwh != -1
25415+ && a->bwh < a->mvd_bdst) {
392086de
AM
25416+ a->mvd_errno = EAU_MVDOWN_WHITEOUT;
25417+ AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
c2b27bf2
AM
25418+ a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
25419+ goto out;
25420+ } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
392086de
AM
25421+ a->mvd_errno = EAU_MVDOWN_UPPER;
25422+ AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
c2b27bf2
AM
25423+ a->mvd_bdst, a->bfound);
25424+ goto out;
25425+ }
25426+
25427+ err = 0; /* success */
25428+
25429+out:
25430+ AuTraceErr(err);
25431+ return err;
25432+}
25433+
392086de 25434+static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25435+{
25436+ int err;
25437+
392086de
AM
25438+ err = 0;
25439+ if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25440+ && a->bfound == a->mvd_bdst)
25441+ err = -EEXIST;
c2b27bf2
AM
25442+ AuTraceErr(err);
25443+ return err;
25444+}
25445+
392086de 25446+static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
25447+{
25448+ int err;
25449+ struct au_branch *br;
25450+
25451+ err = -EISDIR;
25452+ if (unlikely(S_ISDIR(a->inode->i_mode)))
25453+ goto out;
25454+
25455+ err = -EINVAL;
392086de 25456+ if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
5afbbe0d 25457+ a->mvd_bsrc = au_ibtop(a->inode);
392086de
AM
25458+ else {
25459+ a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
25460+ if (unlikely(a->mvd_bsrc < 0
5afbbe0d
AM
25461+ || (a->mvd_bsrc < au_dbtop(a->dentry)
25462+ || au_dbbot(a->dentry) < a->mvd_bsrc
392086de 25463+ || !au_h_dptr(a->dentry, a->mvd_bsrc))
5afbbe0d
AM
25464+ || (a->mvd_bsrc < au_ibtop(a->inode)
25465+ || au_ibbot(a->inode) < a->mvd_bsrc
392086de
AM
25466+ || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
25467+ a->mvd_errno = EAU_MVDOWN_NOUPPER;
25468+ AU_MVD_PR(dmsg, "no upper\n");
25469+ goto out;
25470+ }
25471+ }
5afbbe0d 25472+ if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
392086de
AM
25473+ a->mvd_errno = EAU_MVDOWN_BOTTOM;
25474+ AU_MVD_PR(dmsg, "on the bottom\n");
c2b27bf2
AM
25475+ goto out;
25476+ }
392086de 25477+ a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
c2b27bf2
AM
25478+ br = au_sbr(a->sb, a->mvd_bsrc);
25479+ err = au_br_rdonly(br);
392086de
AM
25480+ if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
25481+ if (unlikely(err))
25482+ goto out;
25483+ } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
25484+ || IS_APPEND(a->mvd_h_src_inode))) {
25485+ if (err)
25486+ a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
25487+ /* go on */
25488+ } else
c2b27bf2
AM
25489+ goto out;
25490+
25491+ err = -EINVAL;
392086de
AM
25492+ if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
25493+ a->mvd_bdst = find_lower_writable(a);
25494+ if (unlikely(a->mvd_bdst < 0)) {
25495+ a->mvd_errno = EAU_MVDOWN_BOTTOM;
25496+ AU_MVD_PR(dmsg, "no writable lower branch\n");
25497+ goto out;
25498+ }
25499+ } else {
25500+ a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
25501+ if (unlikely(a->mvd_bdst < 0
5afbbe0d 25502+ || au_sbbot(a->sb) < a->mvd_bdst)) {
392086de
AM
25503+ a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
25504+ AU_MVD_PR(dmsg, "no lower brid\n");
25505+ goto out;
25506+ }
c2b27bf2
AM
25507+ }
25508+
392086de 25509+ err = au_mvd_args_busy(dmsg, a);
c2b27bf2 25510+ if (!err)
392086de 25511+ err = au_mvd_args_parent(dmsg, a);
c2b27bf2 25512+ if (!err)
392086de 25513+ err = au_mvd_args_intermediate(dmsg, a);
c2b27bf2 25514+ if (!err)
392086de 25515+ err = au_mvd_args_exist(dmsg, a);
c2b27bf2
AM
25516+ if (!err)
25517+ AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
25518+
25519+out:
25520+ AuTraceErr(err);
25521+ return err;
25522+}
25523+
25524+int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
25525+{
392086de
AM
25526+ int err, e;
25527+ unsigned char dmsg;
25528+ struct au_mvd_args *args;
79b8bda9 25529+ struct inode *inode;
c2b27bf2 25530+
79b8bda9 25531+ inode = d_inode(dentry);
c2b27bf2
AM
25532+ err = -EPERM;
25533+ if (unlikely(!capable(CAP_SYS_ADMIN)))
25534+ goto out;
25535+
392086de
AM
25536+ err = -ENOMEM;
25537+ args = kmalloc(sizeof(*args), GFP_NOFS);
25538+ if (unlikely(!args))
25539+ goto out;
25540+
25541+ err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
25542+ if (!err)
ba1aed25
AM
25543+ /* VERIFY_WRITE */
25544+ err = !access_ok(uarg, sizeof(*uarg));
c2b27bf2
AM
25545+ if (unlikely(err)) {
25546+ err = -EFAULT;
392086de
AM
25547+ AuTraceErr(err);
25548+ goto out_free;
c2b27bf2 25549+ }
392086de
AM
25550+ AuDbg("flags 0x%x\n", args->mvdown.flags);
25551+ args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
25552+ args->mvdown.au_errno = 0;
25553+ args->dentry = dentry;
79b8bda9 25554+ args->inode = inode;
392086de 25555+ args->sb = dentry->d_sb;
c2b27bf2 25556+
392086de
AM
25557+ err = -ENOENT;
25558+ dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
25559+ args->parent = dget_parent(dentry);
5527c038 25560+ args->dir = d_inode(args->parent);
febd17d6 25561+ inode_lock_nested(args->dir, I_MUTEX_PARENT);
392086de
AM
25562+ dput(args->parent);
25563+ if (unlikely(args->parent != dentry->d_parent)) {
25564+ AU_MVD_PR(dmsg, "parent dir is moved\n");
c2b27bf2
AM
25565+ goto out_dir;
25566+ }
25567+
febd17d6 25568+ inode_lock_nested(inode, I_MUTEX_CHILD);
b95c5147 25569+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
c2b27bf2
AM
25570+ if (unlikely(err))
25571+ goto out_inode;
25572+
392086de
AM
25573+ di_write_lock_parent(args->parent);
25574+ err = au_mvd_args(dmsg, args);
c2b27bf2
AM
25575+ if (unlikely(err))
25576+ goto out_parent;
25577+
392086de 25578+ err = au_do_mvdown(dmsg, args);
c2b27bf2
AM
25579+ if (unlikely(err))
25580+ goto out_parent;
c2b27bf2 25581+
392086de 25582+ au_cpup_attr_timesizes(args->dir);
79b8bda9
AM
25583+ au_cpup_attr_timesizes(inode);
25584+ if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
25585+ au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
c2b27bf2
AM
25586+ /* au_digen_dec(dentry); */
25587+
25588+out_parent:
392086de 25589+ di_write_unlock(args->parent);
c2b27bf2
AM
25590+ aufs_read_unlock(dentry, AuLock_DW);
25591+out_inode:
febd17d6 25592+ inode_unlock(inode);
c2b27bf2 25593+out_dir:
febd17d6 25594+ inode_unlock(args->dir);
392086de
AM
25595+out_free:
25596+ e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
25597+ if (unlikely(e))
25598+ err = -EFAULT;
9f237c51 25599+ au_kfree_rcu(args);
c2b27bf2
AM
25600+out:
25601+ AuTraceErr(err);
25602+ return err;
25603+}
25604diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
eca34b5c 25605--- /usr/share/empty/fs/aufs/opts.c 1970-01-01 01:00:00.000000000 +0100
46016270 25606+++ linux/fs/aufs/opts.c 2021-02-24 13:33:42.747680497 +0100
eca34b5c 25607@@ -0,0 +1,1880 @@
cd7a4cd9 25608+// SPDX-License-Identifier: GPL-2.0
1facf9fc 25609+/*
d58c55f2 25610+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 25611+ *
25612+ * This program, aufs is free software; you can redistribute it and/or modify
25613+ * it under the terms of the GNU General Public License as published by
25614+ * the Free Software Foundation; either version 2 of the License, or
25615+ * (at your option) any later version.
dece6358
AM
25616+ *
25617+ * This program is distributed in the hope that it will be useful,
25618+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
25619+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25620+ * GNU General Public License for more details.
25621+ *
25622+ * You should have received a copy of the GNU General Public License
523b37e3 25623+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 25624+ */
25625+
25626+/*
25627+ * mount options/flags
25628+ */
25629+
dece6358 25630+#include <linux/namei.h>
1facf9fc 25631+#include <linux/types.h> /* a distribution requires */
25632+#include <linux/parser.h>
25633+#include "aufs.h"
25634+
25635+/* ---------------------------------------------------------------------- */
25636+
25637+enum {
25638+ Opt_br,
7e9cd9fe
AM
25639+ Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
25640+ Opt_idel, Opt_imod,
25641+ Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
dece6358 25642+ Opt_rdblk_def, Opt_rdhash_def,
7e9cd9fe 25643+ Opt_xino, Opt_noxino,
1facf9fc 25644+ Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
25645+ Opt_trunc_xino_path, Opt_itrunc_xino,
25646+ Opt_trunc_xib, Opt_notrunc_xib,
dece6358 25647+ Opt_shwh, Opt_noshwh,
1facf9fc 25648+ Opt_plink, Opt_noplink, Opt_list_plink,
25649+ Opt_udba,
4a4d8108 25650+ Opt_dio, Opt_nodio,
1facf9fc 25651+ Opt_diropq_a, Opt_diropq_w,
25652+ Opt_warn_perm, Opt_nowarn_perm,
25653+ Opt_wbr_copyup, Opt_wbr_create,
076b876e 25654+ Opt_fhsm_sec,
1facf9fc 25655+ Opt_verbose, Opt_noverbose,
25656+ Opt_sum, Opt_nosum, Opt_wsum,
076b876e 25657+ Opt_dirperm1, Opt_nodirperm1,
8b6a4947 25658+ Opt_dirren, Opt_nodirren,
c1595e42 25659+ Opt_acl, Opt_noacl,
1facf9fc 25660+ Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
25661+};
25662+
25663+static match_table_t options = {
25664+ {Opt_br, "br=%s"},
25665+ {Opt_br, "br:%s"},
25666+
25667+ {Opt_add, "add=%d:%s"},
25668+ {Opt_add, "add:%d:%s"},
25669+ {Opt_add, "ins=%d:%s"},
25670+ {Opt_add, "ins:%d:%s"},
25671+ {Opt_append, "append=%s"},
25672+ {Opt_append, "append:%s"},
25673+ {Opt_prepend, "prepend=%s"},
25674+ {Opt_prepend, "prepend:%s"},
25675+
25676+ {Opt_del, "del=%s"},
25677+ {Opt_del, "del:%s"},
25678+ /* {Opt_idel, "idel:%d"}, */
25679+ {Opt_mod, "mod=%s"},
25680+ {Opt_mod, "mod:%s"},
25681+ /* {Opt_imod, "imod:%d:%s"}, */
25682+
25683+ {Opt_dirwh, "dirwh=%d"},
25684+
25685+ {Opt_xino, "xino=%s"},
25686+ {Opt_noxino, "noxino"},
25687+ {Opt_trunc_xino, "trunc_xino"},
25688+ {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
25689+ {Opt_notrunc_xino, "notrunc_xino"},
25690+ {Opt_trunc_xino_path, "trunc_xino=%s"},
25691+ {Opt_itrunc_xino, "itrunc_xino=%d"},
25692+ /* {Opt_zxino, "zxino=%s"}, */
25693+ {Opt_trunc_xib, "trunc_xib"},
25694+ {Opt_notrunc_xib, "notrunc_xib"},
25695+
e49829fe 25696+#ifdef CONFIG_PROC_FS
1facf9fc 25697+ {Opt_plink, "plink"},
e49829fe
JR
25698+#else
25699+ {Opt_ignore_silent, "plink"},
25700+#endif
25701+
1facf9fc 25702+ {Opt_noplink, "noplink"},
e49829fe 25703+
1facf9fc 25704+#ifdef CONFIG_AUFS_DEBUG
25705+ {Opt_list_plink, "list_plink"},
25706+#endif
25707+
25708+ {Opt_udba, "udba=%s"},
25709+
4a4d8108
AM
25710+ {Opt_dio, "dio"},
25711+ {Opt_nodio, "nodio"},
25712+
8b6a4947
AM
25713+#ifdef CONFIG_AUFS_DIRREN
25714+ {Opt_dirren, "dirren"},
25715+ {Opt_nodirren, "nodirren"},
25716+#else
25717+ {Opt_ignore, "dirren"},
25718+ {Opt_ignore_silent, "nodirren"},
25719+#endif
25720+
076b876e
AM
25721+#ifdef CONFIG_AUFS_FHSM
25722+ {Opt_fhsm_sec, "fhsm_sec=%d"},
25723+#else
8b6a4947 25724+ {Opt_ignore, "fhsm_sec=%d"},
076b876e
AM
25725+#endif
25726+
1facf9fc 25727+ {Opt_diropq_a, "diropq=always"},
25728+ {Opt_diropq_a, "diropq=a"},
25729+ {Opt_diropq_w, "diropq=whiteouted"},
25730+ {Opt_diropq_w, "diropq=w"},
25731+
25732+ {Opt_warn_perm, "warn_perm"},
25733+ {Opt_nowarn_perm, "nowarn_perm"},
25734+
25735+ /* keep them temporary */
1facf9fc 25736+ {Opt_ignore_silent, "nodlgt"},
8b6a4947 25737+ {Opt_ignore, "clean_plink"},
1facf9fc 25738+
dece6358
AM
25739+#ifdef CONFIG_AUFS_SHWH
25740+ {Opt_shwh, "shwh"},
25741+#endif
25742+ {Opt_noshwh, "noshwh"},
25743+
076b876e
AM
25744+ {Opt_dirperm1, "dirperm1"},
25745+ {Opt_nodirperm1, "nodirperm1"},
25746+
1facf9fc 25747+ {Opt_verbose, "verbose"},
25748+ {Opt_verbose, "v"},
25749+ {Opt_noverbose, "noverbose"},
25750+ {Opt_noverbose, "quiet"},
25751+ {Opt_noverbose, "q"},
25752+ {Opt_noverbose, "silent"},
25753+
25754+ {Opt_sum, "sum"},
25755+ {Opt_nosum, "nosum"},
25756+ {Opt_wsum, "wsum"},
25757+
25758+ {Opt_rdcache, "rdcache=%d"},
25759+ {Opt_rdblk, "rdblk=%d"},
dece6358 25760+ {Opt_rdblk_def, "rdblk=def"},
1facf9fc 25761+ {Opt_rdhash, "rdhash=%d"},
dece6358 25762+ {Opt_rdhash_def, "rdhash=def"},
1facf9fc 25763+
25764+ {Opt_wbr_create, "create=%s"},
25765+ {Opt_wbr_create, "create_policy=%s"},
25766+ {Opt_wbr_copyup, "cpup=%s"},
25767+ {Opt_wbr_copyup, "copyup=%s"},
25768+ {Opt_wbr_copyup, "copyup_policy=%s"},
25769+
c1595e42
JR
25770+ /* generic VFS flag */
25771+#ifdef CONFIG_FS_POSIX_ACL
25772+ {Opt_acl, "acl"},
25773+ {Opt_noacl, "noacl"},
25774+#else
8b6a4947 25775+ {Opt_ignore, "acl"},
c1595e42
JR
25776+ {Opt_ignore_silent, "noacl"},
25777+#endif
25778+
1facf9fc 25779+ /* internal use for the scripts */
25780+ {Opt_ignore_silent, "si=%s"},
25781+
25782+ {Opt_br, "dirs=%s"},
25783+ {Opt_ignore, "debug=%d"},
25784+ {Opt_ignore, "delete=whiteout"},
25785+ {Opt_ignore, "delete=all"},
25786+ {Opt_ignore, "imap=%s"},
25787+
1308ab2a 25788+ /* temporary workaround, due to old mount(8)? */
25789+ {Opt_ignore_silent, "relatime"},
25790+
1facf9fc 25791+ {Opt_err, NULL}
25792+};
25793+
25794+/* ---------------------------------------------------------------------- */
25795+
076b876e 25796+static const char *au_parser_pattern(int val, match_table_t tbl)
1facf9fc 25797+{
076b876e
AM
25798+ struct match_token *p;
25799+
25800+ p = tbl;
25801+ while (p->pattern) {
25802+ if (p->token == val)
25803+ return p->pattern;
25804+ p++;
1facf9fc 25805+ }
25806+ BUG();
25807+ return "??";
25808+}
25809+
076b876e
AM
25810+static const char *au_optstr(int *val, match_table_t tbl)
25811+{
25812+ struct match_token *p;
25813+ int v;
25814+
25815+ v = *val;
2000de60
JR
25816+ if (!v)
25817+ goto out;
076b876e 25818+ p = tbl;
2000de60
JR
25819+ while (p->pattern) {
25820+ if (p->token
25821+ && (v & p->token) == p->token) {
076b876e
AM
25822+ *val &= ~p->token;
25823+ return p->pattern;
25824+ }
25825+ p++;
25826+ }
2000de60
JR
25827+
25828+out:
076b876e
AM
25829+ return NULL;
25830+}
25831+
1facf9fc 25832+/* ---------------------------------------------------------------------- */
25833+
1e00d052 25834+static match_table_t brperm = {
1facf9fc 25835+ {AuBrPerm_RO, AUFS_BRPERM_RO},
25836+ {AuBrPerm_RR, AUFS_BRPERM_RR},
25837+ {AuBrPerm_RW, AUFS_BRPERM_RW},
1e00d052
AM
25838+ {0, NULL}
25839+};
1facf9fc 25840+
86dc4139 25841+static match_table_t brattr = {
076b876e
AM
25842+ /* general */
25843+ {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
25844+ {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
c1595e42 25845+ /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
86dc4139 25846+ {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
2000de60 25847+#ifdef CONFIG_AUFS_FHSM
076b876e 25848+ {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
2000de60
JR
25849+#endif
25850+#ifdef CONFIG_AUFS_XATTR
c1595e42
JR
25851+ {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
25852+ {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
25853+ {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
25854+ {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
25855+ {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
25856+ {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
2000de60 25857+#endif
076b876e
AM
25858+
25859+ /* ro/rr branch */
1e00d052 25860+ {AuBrRAttr_WH, AUFS_BRRATTR_WH},
076b876e
AM
25861+
25862+ /* rw branch */
25863+ {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
1e00d052 25864+ {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
076b876e 25865+
1e00d052 25866+ {0, NULL}
1facf9fc 25867+};
25868+
1e00d052
AM
25869+static int br_attr_val(char *str, match_table_t table, substring_t args[])
25870+{
25871+ int attr, v;
25872+ char *p;
25873+
25874+ attr = 0;
25875+ do {
25876+ p = strchr(str, '+');
25877+ if (p)
25878+ *p = 0;
25879+ v = match_token(str, table, args);
076b876e
AM
25880+ if (v) {
25881+ if (v & AuBrAttr_CMOO_Mask)
25882+ attr &= ~AuBrAttr_CMOO_Mask;
1e00d052 25883+ attr |= v;
076b876e 25884+ } else {
1e00d052
AM
25885+ if (p)
25886+ *p = '+';
0c3ec466 25887+ pr_warn("ignored branch attribute %s\n", str);
1e00d052
AM
25888+ break;
25889+ }
25890+ if (p)
25891+ str = p + 1;
25892+ } while (p);
25893+
25894+ return attr;
25895+}
25896+
076b876e
AM
25897+static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
25898+{
25899+ int sz;
25900+ const char *p;
25901+ char *q;
25902+
076b876e
AM
25903+ q = str->a;
25904+ *q = 0;
25905+ p = au_optstr(&perm, brattr);
25906+ if (p) {
25907+ sz = strlen(p);
25908+ memcpy(q, p, sz + 1);
25909+ q += sz;
25910+ } else
25911+ goto out;
25912+
25913+ do {
25914+ p = au_optstr(&perm, brattr);
25915+ if (p) {
25916+ *q++ = '+';
25917+ sz = strlen(p);
25918+ memcpy(q, p, sz + 1);
25919+ q += sz;
25920+ }
25921+ } while (p);
25922+
25923+out:
c1595e42 25924+ return q - str->a;
076b876e
AM
25925+}
25926+
4a4d8108 25927+static int noinline_for_stack br_perm_val(char *perm)
1facf9fc 25928+{
076b876e
AM
25929+ int val, bad, sz;
25930+ char *p;
1facf9fc 25931+ substring_t args[MAX_OPT_ARGS];
076b876e 25932+ au_br_perm_str_t attr;
1facf9fc 25933+
1e00d052
AM
25934+ p = strchr(perm, '+');
25935+ if (p)
25936+ *p = 0;
25937+ val = match_token(perm, brperm, args);
25938+ if (!val) {
25939+ if (p)
25940+ *p = '+';
0c3ec466 25941+ pr_warn("ignored branch permission %s\n", perm);
1e00d052
AM
25942+ val = AuBrPerm_RO;
25943+ goto out;
25944+ }
25945+ if (!p)
25946+ goto out;
25947+
076b876e
AM
25948+ val |= br_attr_val(p + 1, brattr, args);
25949+
25950+ bad = 0;
86dc4139 25951+ switch (val & AuBrPerm_Mask) {
1e00d052
AM
25952+ case AuBrPerm_RO:
25953+ case AuBrPerm_RR:
076b876e
AM
25954+ bad = val & AuBrWAttr_Mask;
25955+ val &= ~AuBrWAttr_Mask;
1e00d052
AM
25956+ break;
25957+ case AuBrPerm_RW:
076b876e
AM
25958+ bad = val & AuBrRAttr_Mask;
25959+ val &= ~AuBrRAttr_Mask;
1e00d052
AM
25960+ break;
25961+ }
c1595e42
JR
25962+
25963+ /*
25964+ * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
25965+ * does not treat it as an error, just warning.
25966+ * this is a tiny guard for the user operation.
25967+ */
25968+ if (val & AuBrAttr_UNPIN) {
25969+ bad |= AuBrAttr_UNPIN;
25970+ val &= ~AuBrAttr_UNPIN;
25971+ }
25972+
076b876e
AM
25973+ if (unlikely(bad)) {
25974+ sz = au_do_optstr_br_attr(&attr, bad);
25975+ AuDebugOn(!sz);
25976+ pr_warn("ignored branch attribute %s\n", attr.a);
25977+ }
1e00d052
AM
25978+
25979+out:
1facf9fc 25980+ return val;
25981+}
25982+
076b876e 25983+void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
1facf9fc 25984+{
076b876e
AM
25985+ au_br_perm_str_t attr;
25986+ const char *p;
25987+ char *q;
1e00d052
AM
25988+ int sz;
25989+
076b876e
AM
25990+ q = str->a;
25991+ p = au_optstr(&perm, brperm);
25992+ AuDebugOn(!p || !*p);
25993+ sz = strlen(p);
25994+ memcpy(q, p, sz + 1);
25995+ q += sz;
1e00d052 25996+
076b876e
AM
25997+ sz = au_do_optstr_br_attr(&attr, perm);
25998+ if (sz) {
25999+ *q++ = '+';
26000+ memcpy(q, attr.a, sz + 1);
1e00d052
AM
26001+ }
26002+
076b876e 26003+ AuDebugOn(strlen(str->a) >= sizeof(str->a));
1facf9fc 26004+}
26005+
26006+/* ---------------------------------------------------------------------- */
26007+
26008+static match_table_t udbalevel = {
26009+ {AuOpt_UDBA_REVAL, "reval"},
26010+ {AuOpt_UDBA_NONE, "none"},
4a4d8108
AM
26011+#ifdef CONFIG_AUFS_HNOTIFY
26012+ {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
26013+#ifdef CONFIG_AUFS_HFSNOTIFY
26014+ {AuOpt_UDBA_HNOTIFY, "fsnotify"},
4a4d8108 26015+#endif
1facf9fc 26016+#endif
26017+ {-1, NULL}
26018+};
26019+
4a4d8108 26020+static int noinline_for_stack udba_val(char *str)
1facf9fc 26021+{
26022+ substring_t args[MAX_OPT_ARGS];
26023+
7f207e10 26024+ return match_token(str, udbalevel, args);
1facf9fc 26025+}
26026+
26027+const char *au_optstr_udba(int udba)
26028+{
076b876e 26029+ return au_parser_pattern(udba, udbalevel);
1facf9fc 26030+}
26031+
26032+/* ---------------------------------------------------------------------- */
26033+
26034+static match_table_t au_wbr_create_policy = {
26035+ {AuWbrCreate_TDP, "tdp"},
26036+ {AuWbrCreate_TDP, "top-down-parent"},
26037+ {AuWbrCreate_RR, "rr"},
26038+ {AuWbrCreate_RR, "round-robin"},
26039+ {AuWbrCreate_MFS, "mfs"},
26040+ {AuWbrCreate_MFS, "most-free-space"},
26041+ {AuWbrCreate_MFSV, "mfs:%d"},
26042+ {AuWbrCreate_MFSV, "most-free-space:%d"},
26043+
f2c43d5f
AM
26044+ /* top-down regardless the parent, and then mfs */
26045+ {AuWbrCreate_TDMFS, "tdmfs:%d"},
26046+ {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
26047+
1facf9fc 26048+ {AuWbrCreate_MFSRR, "mfsrr:%d"},
26049+ {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
26050+ {AuWbrCreate_PMFS, "pmfs"},
26051+ {AuWbrCreate_PMFSV, "pmfs:%d"},
392086de
AM
26052+ {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
26053+ {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
1facf9fc 26054+
26055+ {-1, NULL}
26056+};
26057+
1facf9fc 26058+static int au_wbr_mfs_wmark(substring_t *arg, char *str,
26059+ struct au_opt_wbr_create *create)
26060+{
26061+ int err;
26062+ unsigned long long ull;
26063+
26064+ err = 0;
a2654f78 26065+ if (!match_u64(arg, &ull))
1facf9fc 26066+ create->mfsrr_watermark = ull;
26067+ else {
4a4d8108 26068+ pr_err("bad integer in %s\n", str);
1facf9fc 26069+ err = -EINVAL;
26070+ }
26071+
26072+ return err;
26073+}
26074+
26075+static int au_wbr_mfs_sec(substring_t *arg, char *str,
26076+ struct au_opt_wbr_create *create)
26077+{
26078+ int n, err;
26079+
26080+ err = 0;
027c5e7a 26081+ if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
1facf9fc 26082+ create->mfs_second = n;
26083+ else {
4a4d8108 26084+ pr_err("bad integer in %s\n", str);
1facf9fc 26085+ err = -EINVAL;
26086+ }
26087+
26088+ return err;
26089+}
26090+
4a4d8108
AM
26091+static int noinline_for_stack
26092+au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
1facf9fc 26093+{
26094+ int err, e;
26095+ substring_t args[MAX_OPT_ARGS];
26096+
26097+ err = match_token(str, au_wbr_create_policy, args);
26098+ create->wbr_create = err;
26099+ switch (err) {
26100+ case AuWbrCreate_MFSRRV:
f2c43d5f 26101+ case AuWbrCreate_TDMFSV:
392086de 26102+ case AuWbrCreate_PMFSRRV:
1facf9fc 26103+ e = au_wbr_mfs_wmark(&args[0], str, create);
26104+ if (!e)
26105+ e = au_wbr_mfs_sec(&args[1], str, create);
26106+ if (unlikely(e))
26107+ err = e;
26108+ break;
26109+ case AuWbrCreate_MFSRR:
f2c43d5f 26110+ case AuWbrCreate_TDMFS:
392086de 26111+ case AuWbrCreate_PMFSRR:
1facf9fc 26112+ e = au_wbr_mfs_wmark(&args[0], str, create);
26113+ if (unlikely(e)) {
26114+ err = e;
26115+ break;
26116+ }
42a736d3 26117+ fallthrough;
1facf9fc 26118+ case AuWbrCreate_MFS:
26119+ case AuWbrCreate_PMFS:
027c5e7a 26120+ create->mfs_second = AUFS_MFS_DEF_SEC;
1facf9fc 26121+ break;
26122+ case AuWbrCreate_MFSV:
26123+ case AuWbrCreate_PMFSV:
26124+ e = au_wbr_mfs_sec(&args[0], str, create);
26125+ if (unlikely(e))
26126+ err = e;
26127+ break;
26128+ }
26129+
26130+ return err;
26131+}
26132+
26133+const char *au_optstr_wbr_create(int wbr_create)
26134+{
076b876e 26135+ return au_parser_pattern(wbr_create, au_wbr_create_policy);
1facf9fc 26136+}
26137+
26138+static match_table_t au_wbr_copyup_policy = {
26139+ {AuWbrCopyup_TDP, "tdp"},
26140+ {AuWbrCopyup_TDP, "top-down-parent"},
26141+ {AuWbrCopyup_BUP, "bup"},
26142+ {AuWbrCopyup_BUP, "bottom-up-parent"},
26143+ {AuWbrCopyup_BU, "bu"},
26144+ {AuWbrCopyup_BU, "bottom-up"},
26145+ {-1, NULL}
26146+};
26147+
4a4d8108 26148+static int noinline_for_stack au_wbr_copyup_val(char *str)
1facf9fc 26149+{
26150+ substring_t args[MAX_OPT_ARGS];
26151+
26152+ return match_token(str, au_wbr_copyup_policy, args);
26153+}
26154+
26155+const char *au_optstr_wbr_copyup(int wbr_copyup)
26156+{
076b876e 26157+ return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
1facf9fc 26158+}
26159+
26160+/* ---------------------------------------------------------------------- */
26161+
26162+static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
26163+
26164+static void dump_opts(struct au_opts *opts)
26165+{
26166+#ifdef CONFIG_AUFS_DEBUG
26167+ /* reduce stack space */
26168+ union {
26169+ struct au_opt_add *add;
26170+ struct au_opt_del *del;
26171+ struct au_opt_mod *mod;
26172+ struct au_opt_xino *xino;
26173+ struct au_opt_xino_itrunc *xino_itrunc;
26174+ struct au_opt_wbr_create *create;
26175+ } u;
26176+ struct au_opt *opt;
26177+
26178+ opt = opts->opt;
26179+ while (opt->type != Opt_tail) {
26180+ switch (opt->type) {
26181+ case Opt_add:
26182+ u.add = &opt->add;
26183+ AuDbg("add {b%d, %s, 0x%x, %p}\n",
26184+ u.add->bindex, u.add->pathname, u.add->perm,
26185+ u.add->path.dentry);
26186+ break;
26187+ case Opt_del:
26188+ case Opt_idel:
26189+ u.del = &opt->del;
26190+ AuDbg("del {%s, %p}\n",
26191+ u.del->pathname, u.del->h_path.dentry);
26192+ break;
26193+ case Opt_mod:
26194+ case Opt_imod:
26195+ u.mod = &opt->mod;
26196+ AuDbg("mod {%s, 0x%x, %p}\n",
26197+ u.mod->path, u.mod->perm, u.mod->h_root);
26198+ break;
26199+ case Opt_append:
26200+ u.add = &opt->add;
26201+ AuDbg("append {b%d, %s, 0x%x, %p}\n",
26202+ u.add->bindex, u.add->pathname, u.add->perm,
26203+ u.add->path.dentry);
26204+ break;
26205+ case Opt_prepend:
26206+ u.add = &opt->add;
26207+ AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
26208+ u.add->bindex, u.add->pathname, u.add->perm,
26209+ u.add->path.dentry);
26210+ break;
26211+ case Opt_dirwh:
26212+ AuDbg("dirwh %d\n", opt->dirwh);
26213+ break;
26214+ case Opt_rdcache:
26215+ AuDbg("rdcache %d\n", opt->rdcache);
26216+ break;
26217+ case Opt_rdblk:
26218+ AuDbg("rdblk %u\n", opt->rdblk);
26219+ break;
dece6358
AM
26220+ case Opt_rdblk_def:
26221+ AuDbg("rdblk_def\n");
26222+ break;
1facf9fc 26223+ case Opt_rdhash:
26224+ AuDbg("rdhash %u\n", opt->rdhash);
26225+ break;
dece6358
AM
26226+ case Opt_rdhash_def:
26227+ AuDbg("rdhash_def\n");
26228+ break;
1facf9fc 26229+ case Opt_xino:
26230+ u.xino = &opt->xino;
523b37e3 26231+ AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
1facf9fc 26232+ break;
26233+ case Opt_trunc_xino:
26234+ AuLabel(trunc_xino);
26235+ break;
26236+ case Opt_notrunc_xino:
26237+ AuLabel(notrunc_xino);
26238+ break;
26239+ case Opt_trunc_xino_path:
26240+ case Opt_itrunc_xino:
26241+ u.xino_itrunc = &opt->xino_itrunc;
26242+ AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
26243+ break;
1facf9fc 26244+ case Opt_noxino:
26245+ AuLabel(noxino);
26246+ break;
26247+ case Opt_trunc_xib:
26248+ AuLabel(trunc_xib);
26249+ break;
26250+ case Opt_notrunc_xib:
26251+ AuLabel(notrunc_xib);
26252+ break;
dece6358
AM
26253+ case Opt_shwh:
26254+ AuLabel(shwh);
26255+ break;
26256+ case Opt_noshwh:
26257+ AuLabel(noshwh);
26258+ break;
076b876e
AM
26259+ case Opt_dirperm1:
26260+ AuLabel(dirperm1);
26261+ break;
26262+ case Opt_nodirperm1:
26263+ AuLabel(nodirperm1);
26264+ break;
1facf9fc 26265+ case Opt_plink:
26266+ AuLabel(plink);
26267+ break;
26268+ case Opt_noplink:
26269+ AuLabel(noplink);
26270+ break;
26271+ case Opt_list_plink:
26272+ AuLabel(list_plink);
26273+ break;
26274+ case Opt_udba:
26275+ AuDbg("udba %d, %s\n",
26276+ opt->udba, au_optstr_udba(opt->udba));
26277+ break;
4a4d8108
AM
26278+ case Opt_dio:
26279+ AuLabel(dio);
26280+ break;
26281+ case Opt_nodio:
26282+ AuLabel(nodio);
26283+ break;
1facf9fc 26284+ case Opt_diropq_a:
26285+ AuLabel(diropq_a);
26286+ break;
26287+ case Opt_diropq_w:
26288+ AuLabel(diropq_w);
26289+ break;
26290+ case Opt_warn_perm:
26291+ AuLabel(warn_perm);
26292+ break;
26293+ case Opt_nowarn_perm:
26294+ AuLabel(nowarn_perm);
26295+ break;
1facf9fc 26296+ case Opt_verbose:
26297+ AuLabel(verbose);
26298+ break;
26299+ case Opt_noverbose:
26300+ AuLabel(noverbose);
26301+ break;
26302+ case Opt_sum:
26303+ AuLabel(sum);
26304+ break;
26305+ case Opt_nosum:
26306+ AuLabel(nosum);
26307+ break;
26308+ case Opt_wsum:
26309+ AuLabel(wsum);
26310+ break;
26311+ case Opt_wbr_create:
26312+ u.create = &opt->wbr_create;
26313+ AuDbg("create %d, %s\n", u.create->wbr_create,
26314+ au_optstr_wbr_create(u.create->wbr_create));
26315+ switch (u.create->wbr_create) {
26316+ case AuWbrCreate_MFSV:
26317+ case AuWbrCreate_PMFSV:
26318+ AuDbg("%d sec\n", u.create->mfs_second);
26319+ break;
26320+ case AuWbrCreate_MFSRR:
f2c43d5f 26321+ case AuWbrCreate_TDMFS:
1facf9fc 26322+ AuDbg("%llu watermark\n",
26323+ u.create->mfsrr_watermark);
26324+ break;
26325+ case AuWbrCreate_MFSRRV:
f2c43d5f 26326+ case AuWbrCreate_TDMFSV:
392086de 26327+ case AuWbrCreate_PMFSRRV:
1facf9fc 26328+ AuDbg("%llu watermark, %d sec\n",
26329+ u.create->mfsrr_watermark,
26330+ u.create->mfs_second);
26331+ break;
26332+ }
26333+ break;
26334+ case Opt_wbr_copyup:
26335+ AuDbg("copyup %d, %s\n", opt->wbr_copyup,
26336+ au_optstr_wbr_copyup(opt->wbr_copyup));
26337+ break;
076b876e
AM
26338+ case Opt_fhsm_sec:
26339+ AuDbg("fhsm_sec %u\n", opt->fhsm_second);
26340+ break;
8b6a4947
AM
26341+ case Opt_dirren:
26342+ AuLabel(dirren);
26343+ break;
26344+ case Opt_nodirren:
26345+ AuLabel(nodirren);
26346+ break;
c1595e42
JR
26347+ case Opt_acl:
26348+ AuLabel(acl);
26349+ break;
26350+ case Opt_noacl:
26351+ AuLabel(noacl);
26352+ break;
1facf9fc 26353+ default:
26354+ BUG();
26355+ }
26356+ opt++;
26357+ }
26358+#endif
26359+}
26360+
26361+void au_opts_free(struct au_opts *opts)
26362+{
26363+ struct au_opt *opt;
26364+
26365+ opt = opts->opt;
26366+ while (opt->type != Opt_tail) {
26367+ switch (opt->type) {
26368+ case Opt_add:
26369+ case Opt_append:
26370+ case Opt_prepend:
26371+ path_put(&opt->add.path);
26372+ break;
26373+ case Opt_del:
26374+ case Opt_idel:
26375+ path_put(&opt->del.h_path);
26376+ break;
26377+ case Opt_mod:
26378+ case Opt_imod:
26379+ dput(opt->mod.h_root);
26380+ break;
26381+ case Opt_xino:
26382+ fput(opt->xino.file);
26383+ break;
26384+ }
26385+ opt++;
26386+ }
26387+}
26388+
26389+static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
26390+ aufs_bindex_t bindex)
26391+{
26392+ int err;
26393+ struct au_opt_add *add = &opt->add;
26394+ char *p;
26395+
26396+ add->bindex = bindex;
1e00d052 26397+ add->perm = AuBrPerm_RO;
1facf9fc 26398+ add->pathname = opt_str;
26399+ p = strchr(opt_str, '=');
26400+ if (p) {
26401+ *p++ = 0;
26402+ if (*p)
26403+ add->perm = br_perm_val(p);
26404+ }
26405+
26406+ err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
26407+ if (!err) {
26408+ if (!p) {
26409+ add->perm = AuBrPerm_RO;
26410+ if (au_test_fs_rr(add->path.dentry->d_sb))
26411+ add->perm = AuBrPerm_RR;
2121bcd9 26412+ else if (!bindex && !(sb_flags & SB_RDONLY))
1facf9fc 26413+ add->perm = AuBrPerm_RW;
26414+ }
26415+ opt->type = Opt_add;
26416+ goto out;
26417+ }
4a4d8108 26418+ pr_err("lookup failed %s (%d)\n", add->pathname, err);
1facf9fc 26419+ err = -EINVAL;
26420+
4f0767ce 26421+out:
1facf9fc 26422+ return err;
26423+}
26424+
26425+static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
26426+{
26427+ int err;
26428+
26429+ del->pathname = args[0].from;
26430+ AuDbg("del path %s\n", del->pathname);
26431+
26432+ err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
26433+ if (unlikely(err))
4a4d8108 26434+ pr_err("lookup failed %s (%d)\n", del->pathname, err);
1facf9fc 26435+
26436+ return err;
26437+}
26438+
26439+#if 0 /* reserved for future use */
26440+static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
26441+ struct au_opt_del *del, substring_t args[])
26442+{
26443+ int err;
26444+ struct dentry *root;
26445+
26446+ err = -EINVAL;
26447+ root = sb->s_root;
26448+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d 26449+ if (bindex < 0 || au_sbbot(sb) < bindex) {
4a4d8108 26450+ pr_err("out of bounds, %d\n", bindex);
1facf9fc 26451+ goto out;
26452+ }
26453+
26454+ err = 0;
26455+ del->h_path.dentry = dget(au_h_dptr(root, bindex));
26456+ del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
26457+
4f0767ce 26458+out:
1facf9fc 26459+ aufs_read_unlock(root, !AuLock_IR);
26460+ return err;
26461+}
26462+#endif
26463+
4a4d8108
AM
26464+static int noinline_for_stack
26465+au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
1facf9fc 26466+{
26467+ int err;
26468+ struct path path;
26469+ char *p;
26470+
26471+ err = -EINVAL;
26472+ mod->path = args[0].from;
26473+ p = strchr(mod->path, '=');
26474+ if (unlikely(!p)) {
acd2b654 26475+ pr_err("no permission %s\n", args[0].from);
1facf9fc 26476+ goto out;
26477+ }
26478+
26479+ *p++ = 0;
26480+ err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
26481+ if (unlikely(err)) {
4a4d8108 26482+ pr_err("lookup failed %s (%d)\n", mod->path, err);
1facf9fc 26483+ goto out;
26484+ }
26485+
26486+ mod->perm = br_perm_val(p);
26487+ AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
26488+ mod->h_root = dget(path.dentry);
26489+ path_put(&path);
26490+
4f0767ce 26491+out:
1facf9fc 26492+ return err;
26493+}
26494+
26495+#if 0 /* reserved for future use */
26496+static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
26497+ struct au_opt_mod *mod, substring_t args[])
26498+{
26499+ int err;
26500+ struct dentry *root;
26501+
26502+ err = -EINVAL;
26503+ root = sb->s_root;
26504+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d 26505+ if (bindex < 0 || au_sbbot(sb) < bindex) {
4a4d8108 26506+ pr_err("out of bounds, %d\n", bindex);
1facf9fc 26507+ goto out;
26508+ }
26509+
26510+ err = 0;
26511+ mod->perm = br_perm_val(args[1].from);
26512+ AuDbg("mod path %s, perm 0x%x, %s\n",
26513+ mod->path, mod->perm, args[1].from);
26514+ mod->h_root = dget(au_h_dptr(root, bindex));
26515+
4f0767ce 26516+out:
1facf9fc 26517+ aufs_read_unlock(root, !AuLock_IR);
26518+ return err;
26519+}
26520+#endif
26521+
26522+static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
26523+ substring_t args[])
26524+{
26525+ int err;
26526+ struct file *file;
26527+
83b672a5 26528+ file = au_xino_create(sb, args[0].from, /*silent*/0, /*wbrtop*/0);
1facf9fc 26529+ err = PTR_ERR(file);
26530+ if (IS_ERR(file))
26531+ goto out;
26532+
26533+ err = -EINVAL;
2000de60 26534+ if (unlikely(file->f_path.dentry->d_sb == sb)) {
1facf9fc 26535+ fput(file);
4a4d8108 26536+ pr_err("%s must be outside\n", args[0].from);
1facf9fc 26537+ goto out;
26538+ }
26539+
26540+ err = 0;
26541+ xino->file = file;
26542+ xino->path = args[0].from;
26543+
4f0767ce 26544+out:
1facf9fc 26545+ return err;
26546+}
26547+
4a4d8108
AM
26548+static int noinline_for_stack
26549+au_opts_parse_xino_itrunc_path(struct super_block *sb,
26550+ struct au_opt_xino_itrunc *xino_itrunc,
26551+ substring_t args[])
1facf9fc 26552+{
26553+ int err;
5afbbe0d 26554+ aufs_bindex_t bbot, bindex;
1facf9fc 26555+ struct path path;
26556+ struct dentry *root;
26557+
26558+ err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
26559+ if (unlikely(err)) {
4a4d8108 26560+ pr_err("lookup failed %s (%d)\n", args[0].from, err);
1facf9fc 26561+ goto out;
26562+ }
26563+
26564+ xino_itrunc->bindex = -1;
26565+ root = sb->s_root;
26566+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d
AM
26567+ bbot = au_sbbot(sb);
26568+ for (bindex = 0; bindex <= bbot; bindex++) {
1facf9fc 26569+ if (au_h_dptr(root, bindex) == path.dentry) {
26570+ xino_itrunc->bindex = bindex;
26571+ break;
26572+ }
26573+ }
26574+ aufs_read_unlock(root, !AuLock_IR);
26575+ path_put(&path);
26576+
26577+ if (unlikely(xino_itrunc->bindex < 0)) {
4a4d8108 26578+ pr_err("no such branch %s\n", args[0].from);
1facf9fc 26579+ err = -EINVAL;
26580+ }
26581+
4f0767ce 26582+out:
1facf9fc 26583+ return err;
26584+}
26585+
26586+/* called without aufs lock */
26587+int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
26588+{
26589+ int err, n, token;
26590+ aufs_bindex_t bindex;
26591+ unsigned char skipped;
26592+ struct dentry *root;
26593+ struct au_opt *opt, *opt_tail;
26594+ char *opt_str;
26595+ /* reduce the stack space */
26596+ union {
26597+ struct au_opt_xino_itrunc *xino_itrunc;
26598+ struct au_opt_wbr_create *create;
26599+ } u;
26600+ struct {
26601+ substring_t args[MAX_OPT_ARGS];
26602+ } *a;
26603+
26604+ err = -ENOMEM;
26605+ a = kmalloc(sizeof(*a), GFP_NOFS);
26606+ if (unlikely(!a))
26607+ goto out;
26608+
26609+ root = sb->s_root;
26610+ err = 0;
26611+ bindex = 0;
26612+ opt = opts->opt;
26613+ opt_tail = opt + opts->max_opt - 1;
26614+ opt->type = Opt_tail;
26615+ while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
26616+ err = -EINVAL;
26617+ skipped = 0;
26618+ token = match_token(opt_str, options, a->args);
26619+ switch (token) {
26620+ case Opt_br:
26621+ err = 0;
26622+ while (!err && (opt_str = strsep(&a->args[0].from, ":"))
26623+ && *opt_str) {
26624+ err = opt_add(opt, opt_str, opts->sb_flags,
26625+ bindex++);
26626+ if (unlikely(!err && ++opt > opt_tail)) {
26627+ err = -E2BIG;
26628+ break;
26629+ }
26630+ opt->type = Opt_tail;
26631+ skipped = 1;
26632+ }
26633+ break;
26634+ case Opt_add:
26635+ if (unlikely(match_int(&a->args[0], &n))) {
4a4d8108 26636+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26637+ break;
26638+ }
26639+ bindex = n;
26640+ err = opt_add(opt, a->args[1].from, opts->sb_flags,
26641+ bindex);
26642+ if (!err)
26643+ opt->type = token;
26644+ break;
26645+ case Opt_append:
26646+ err = opt_add(opt, a->args[0].from, opts->sb_flags,
26647+ /*dummy bindex*/1);
26648+ if (!err)
26649+ opt->type = token;
26650+ break;
26651+ case Opt_prepend:
26652+ err = opt_add(opt, a->args[0].from, opts->sb_flags,
26653+ /*bindex*/0);
26654+ if (!err)
26655+ opt->type = token;
26656+ break;
26657+ case Opt_del:
26658+ err = au_opts_parse_del(&opt->del, a->args);
26659+ if (!err)
26660+ opt->type = token;
26661+ break;
26662+#if 0 /* reserved for future use */
26663+ case Opt_idel:
26664+ del->pathname = "(indexed)";
26665+ if (unlikely(match_int(&args[0], &n))) {
4a4d8108 26666+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26667+ break;
26668+ }
26669+ err = au_opts_parse_idel(sb, n, &opt->del, a->args);
26670+ if (!err)
26671+ opt->type = token;
26672+ break;
26673+#endif
26674+ case Opt_mod:
26675+ err = au_opts_parse_mod(&opt->mod, a->args);
26676+ if (!err)
26677+ opt->type = token;
26678+ break;
26679+#ifdef IMOD /* reserved for future use */
26680+ case Opt_imod:
26681+ u.mod->path = "(indexed)";
26682+ if (unlikely(match_int(&a->args[0], &n))) {
4a4d8108 26683+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26684+ break;
26685+ }
26686+ err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
26687+ if (!err)
26688+ opt->type = token;
26689+ break;
26690+#endif
26691+ case Opt_xino:
26692+ err = au_opts_parse_xino(sb, &opt->xino, a->args);
26693+ if (!err)
26694+ opt->type = token;
26695+ break;
26696+
26697+ case Opt_trunc_xino_path:
26698+ err = au_opts_parse_xino_itrunc_path
26699+ (sb, &opt->xino_itrunc, a->args);
26700+ if (!err)
26701+ opt->type = token;
26702+ break;
26703+
26704+ case Opt_itrunc_xino:
26705+ u.xino_itrunc = &opt->xino_itrunc;
26706+ if (unlikely(match_int(&a->args[0], &n))) {
4a4d8108 26707+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26708+ break;
26709+ }
26710+ u.xino_itrunc->bindex = n;
26711+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d 26712+ if (n < 0 || au_sbbot(sb) < n) {
4a4d8108 26713+ pr_err("out of bounds, %d\n", n);
1facf9fc 26714+ aufs_read_unlock(root, !AuLock_IR);
26715+ break;
26716+ }
26717+ aufs_read_unlock(root, !AuLock_IR);
26718+ err = 0;
26719+ opt->type = token;
26720+ break;
26721+
26722+ case Opt_dirwh:
26723+ if (unlikely(match_int(&a->args[0], &opt->dirwh)))
26724+ break;
26725+ err = 0;
26726+ opt->type = token;
26727+ break;
26728+
26729+ case Opt_rdcache:
027c5e7a
AM
26730+ if (unlikely(match_int(&a->args[0], &n))) {
26731+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26732+ break;
027c5e7a
AM
26733+ }
26734+ if (unlikely(n > AUFS_RDCACHE_MAX)) {
26735+ pr_err("rdcache must be smaller than %d\n",
26736+ AUFS_RDCACHE_MAX);
26737+ break;
26738+ }
26739+ opt->rdcache = n;
1facf9fc 26740+ err = 0;
26741+ opt->type = token;
26742+ break;
26743+ case Opt_rdblk:
26744+ if (unlikely(match_int(&a->args[0], &n)
1308ab2a 26745+ || n < 0
1facf9fc 26746+ || n > KMALLOC_MAX_SIZE)) {
4a4d8108 26747+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26748+ break;
26749+ }
1308ab2a 26750+ if (unlikely(n && n < NAME_MAX)) {
4a4d8108
AM
26751+ pr_err("rdblk must be larger than %d\n",
26752+ NAME_MAX);
1facf9fc 26753+ break;
26754+ }
26755+ opt->rdblk = n;
26756+ err = 0;
26757+ opt->type = token;
26758+ break;
26759+ case Opt_rdhash:
26760+ if (unlikely(match_int(&a->args[0], &n)
1308ab2a 26761+ || n < 0
1facf9fc 26762+ || n * sizeof(struct hlist_head)
26763+ > KMALLOC_MAX_SIZE)) {
4a4d8108 26764+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 26765+ break;
26766+ }
26767+ opt->rdhash = n;
26768+ err = 0;
26769+ opt->type = token;
26770+ break;
26771+
26772+ case Opt_trunc_xino:
26773+ case Opt_notrunc_xino:
26774+ case Opt_noxino:
26775+ case Opt_trunc_xib:
26776+ case Opt_notrunc_xib:
dece6358
AM
26777+ case Opt_shwh:
26778+ case Opt_noshwh:
076b876e
AM
26779+ case Opt_dirperm1:
26780+ case Opt_nodirperm1:
1facf9fc 26781+ case Opt_plink:
26782+ case Opt_noplink:
26783+ case Opt_list_plink:
4a4d8108
AM
26784+ case Opt_dio:
26785+ case Opt_nodio:
1facf9fc 26786+ case Opt_diropq_a:
26787+ case Opt_diropq_w:
26788+ case Opt_warn_perm:
26789+ case Opt_nowarn_perm:
1facf9fc 26790+ case Opt_verbose:
26791+ case Opt_noverbose:
26792+ case Opt_sum:
26793+ case Opt_nosum:
26794+ case Opt_wsum:
dece6358
AM
26795+ case Opt_rdblk_def:
26796+ case Opt_rdhash_def:
8b6a4947
AM
26797+ case Opt_dirren:
26798+ case Opt_nodirren:
c1595e42
JR
26799+ case Opt_acl:
26800+ case Opt_noacl:
1facf9fc 26801+ err = 0;
26802+ opt->type = token;
26803+ break;
26804+
26805+ case Opt_udba:
26806+ opt->udba = udba_val(a->args[0].from);
26807+ if (opt->udba >= 0) {
26808+ err = 0;
26809+ opt->type = token;
26810+ } else
4a4d8108 26811+ pr_err("wrong value, %s\n", opt_str);
1facf9fc 26812+ break;
26813+
26814+ case Opt_wbr_create:
26815+ u.create = &opt->wbr_create;
26816+ u.create->wbr_create
26817+ = au_wbr_create_val(a->args[0].from, u.create);
26818+ if (u.create->wbr_create >= 0) {
26819+ err = 0;
26820+ opt->type = token;
26821+ } else
4a4d8108 26822+ pr_err("wrong value, %s\n", opt_str);
1facf9fc 26823+ break;
26824+ case Opt_wbr_copyup:
26825+ opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
26826+ if (opt->wbr_copyup >= 0) {
26827+ err = 0;
26828+ opt->type = token;
26829+ } else
4a4d8108 26830+ pr_err("wrong value, %s\n", opt_str);
1facf9fc 26831+ break;
26832+
076b876e
AM
26833+ case Opt_fhsm_sec:
26834+ if (unlikely(match_int(&a->args[0], &n)
26835+ || n < 0)) {
26836+ pr_err("bad integer in %s\n", opt_str);
26837+ break;
26838+ }
26839+ if (sysaufs_brs) {
26840+ opt->fhsm_second = n;
26841+ opt->type = token;
26842+ } else
26843+ pr_warn("ignored %s\n", opt_str);
26844+ err = 0;
26845+ break;
26846+
1facf9fc 26847+ case Opt_ignore:
0c3ec466 26848+ pr_warn("ignored %s\n", opt_str);
42a736d3 26849+ fallthrough;
1facf9fc 26850+ case Opt_ignore_silent:
26851+ skipped = 1;
26852+ err = 0;
26853+ break;
26854+ case Opt_err:
4a4d8108 26855+ pr_err("unknown option %s\n", opt_str);
1facf9fc 26856+ break;
26857+ }
26858+
26859+ if (!err && !skipped) {
26860+ if (unlikely(++opt > opt_tail)) {
26861+ err = -E2BIG;
26862+ opt--;
26863+ opt->type = Opt_tail;
26864+ break;
26865+ }
26866+ opt->type = Opt_tail;
26867+ }
26868+ }
26869+
9f237c51 26870+ au_kfree_rcu(a);
1facf9fc 26871+ dump_opts(opts);
26872+ if (unlikely(err))
26873+ au_opts_free(opts);
26874+
4f0767ce 26875+out:
1facf9fc 26876+ return err;
26877+}
26878+
26879+static int au_opt_wbr_create(struct super_block *sb,
26880+ struct au_opt_wbr_create *create)
26881+{
26882+ int err;
26883+ struct au_sbinfo *sbinfo;
26884+
dece6358
AM
26885+ SiMustWriteLock(sb);
26886+
1facf9fc 26887+ err = 1; /* handled */
26888+ sbinfo = au_sbi(sb);
26889+ if (sbinfo->si_wbr_create_ops->fin) {
26890+ err = sbinfo->si_wbr_create_ops->fin(sb);
26891+ if (!err)
26892+ err = 1;
26893+ }
26894+
26895+ sbinfo->si_wbr_create = create->wbr_create;
26896+ sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
26897+ switch (create->wbr_create) {
26898+ case AuWbrCreate_MFSRRV:
26899+ case AuWbrCreate_MFSRR:
f2c43d5f
AM
26900+ case AuWbrCreate_TDMFS:
26901+ case AuWbrCreate_TDMFSV:
392086de
AM
26902+ case AuWbrCreate_PMFSRR:
26903+ case AuWbrCreate_PMFSRRV:
1facf9fc 26904+ sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
42a736d3 26905+ fallthrough;
1facf9fc 26906+ case AuWbrCreate_MFS:
26907+ case AuWbrCreate_MFSV:
26908+ case AuWbrCreate_PMFS:
26909+ case AuWbrCreate_PMFSV:
e49829fe
JR
26910+ sbinfo->si_wbr_mfs.mfs_expire
26911+ = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
1facf9fc 26912+ break;
26913+ }
26914+
26915+ if (sbinfo->si_wbr_create_ops->init)
26916+ sbinfo->si_wbr_create_ops->init(sb); /* ignore */
26917+
26918+ return err;
26919+}
26920+
26921+/*
26922+ * returns,
26923+ * plus: processed without an error
26924+ * zero: unprocessed
26925+ */
26926+static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
26927+ struct au_opts *opts)
26928+{
26929+ int err;
26930+ struct au_sbinfo *sbinfo;
26931+
dece6358
AM
26932+ SiMustWriteLock(sb);
26933+
1facf9fc 26934+ err = 1; /* handled */
26935+ sbinfo = au_sbi(sb);
26936+ switch (opt->type) {
26937+ case Opt_udba:
26938+ sbinfo->si_mntflags &= ~AuOptMask_UDBA;
26939+ sbinfo->si_mntflags |= opt->udba;
26940+ opts->given_udba |= opt->udba;
26941+ break;
26942+
26943+ case Opt_plink:
26944+ au_opt_set(sbinfo->si_mntflags, PLINK);
26945+ break;
26946+ case Opt_noplink:
26947+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
e49829fe 26948+ au_plink_put(sb, /*verbose*/1);
1facf9fc 26949+ au_opt_clr(sbinfo->si_mntflags, PLINK);
26950+ break;
26951+ case Opt_list_plink:
26952+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
26953+ au_plink_list(sb);
26954+ break;
26955+
4a4d8108
AM
26956+ case Opt_dio:
26957+ au_opt_set(sbinfo->si_mntflags, DIO);
26958+ au_fset_opts(opts->flags, REFRESH_DYAOP);
26959+ break;
26960+ case Opt_nodio:
26961+ au_opt_clr(sbinfo->si_mntflags, DIO);
26962+ au_fset_opts(opts->flags, REFRESH_DYAOP);
26963+ break;
26964+
076b876e
AM
26965+ case Opt_fhsm_sec:
26966+ au_fhsm_set(sbinfo, opt->fhsm_second);
26967+ break;
26968+
1facf9fc 26969+ case Opt_diropq_a:
26970+ au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
26971+ break;
26972+ case Opt_diropq_w:
26973+ au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
26974+ break;
26975+
26976+ case Opt_warn_perm:
26977+ au_opt_set(sbinfo->si_mntflags, WARN_PERM);
26978+ break;
26979+ case Opt_nowarn_perm:
26980+ au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
26981+ break;
26982+
1facf9fc 26983+ case Opt_verbose:
26984+ au_opt_set(sbinfo->si_mntflags, VERBOSE);
26985+ break;
26986+ case Opt_noverbose:
26987+ au_opt_clr(sbinfo->si_mntflags, VERBOSE);
26988+ break;
26989+
26990+ case Opt_sum:
26991+ au_opt_set(sbinfo->si_mntflags, SUM);
26992+ break;
26993+ case Opt_wsum:
26994+ au_opt_clr(sbinfo->si_mntflags, SUM);
26995+ au_opt_set(sbinfo->si_mntflags, SUM_W);
eca34b5c 26996+ break;
1facf9fc 26997+ case Opt_nosum:
26998+ au_opt_clr(sbinfo->si_mntflags, SUM);
26999+ au_opt_clr(sbinfo->si_mntflags, SUM_W);
27000+ break;
27001+
27002+ case Opt_wbr_create:
27003+ err = au_opt_wbr_create(sb, &opt->wbr_create);
27004+ break;
27005+ case Opt_wbr_copyup:
27006+ sbinfo->si_wbr_copyup = opt->wbr_copyup;
27007+ sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
27008+ break;
27009+
27010+ case Opt_dirwh:
27011+ sbinfo->si_dirwh = opt->dirwh;
27012+ break;
27013+
27014+ case Opt_rdcache:
e49829fe
JR
27015+ sbinfo->si_rdcache
27016+ = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
1facf9fc 27017+ break;
27018+ case Opt_rdblk:
27019+ sbinfo->si_rdblk = opt->rdblk;
27020+ break;
dece6358
AM
27021+ case Opt_rdblk_def:
27022+ sbinfo->si_rdblk = AUFS_RDBLK_DEF;
27023+ break;
1facf9fc 27024+ case Opt_rdhash:
27025+ sbinfo->si_rdhash = opt->rdhash;
27026+ break;
dece6358
AM
27027+ case Opt_rdhash_def:
27028+ sbinfo->si_rdhash = AUFS_RDHASH_DEF;
27029+ break;
27030+
27031+ case Opt_shwh:
27032+ au_opt_set(sbinfo->si_mntflags, SHWH);
27033+ break;
27034+ case Opt_noshwh:
27035+ au_opt_clr(sbinfo->si_mntflags, SHWH);
27036+ break;
1facf9fc 27037+
076b876e
AM
27038+ case Opt_dirperm1:
27039+ au_opt_set(sbinfo->si_mntflags, DIRPERM1);
27040+ break;
27041+ case Opt_nodirperm1:
27042+ au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
27043+ break;
27044+
1facf9fc 27045+ case Opt_trunc_xino:
27046+ au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
27047+ break;
27048+ case Opt_notrunc_xino:
27049+ au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
27050+ break;
27051+
27052+ case Opt_trunc_xino_path:
27053+ case Opt_itrunc_xino:
acd2b654
AM
27054+ err = au_xino_trunc(sb, opt->xino_itrunc.bindex,
27055+ /*idx_begin*/0);
1facf9fc 27056+ if (!err)
27057+ err = 1;
27058+ break;
27059+
27060+ case Opt_trunc_xib:
27061+ au_fset_opts(opts->flags, TRUNC_XIB);
27062+ break;
27063+ case Opt_notrunc_xib:
27064+ au_fclr_opts(opts->flags, TRUNC_XIB);
27065+ break;
27066+
8b6a4947
AM
27067+ case Opt_dirren:
27068+ err = 1;
27069+ if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27070+ err = au_dr_opt_set(sb);
27071+ if (!err)
27072+ err = 1;
27073+ }
27074+ if (err == 1)
27075+ au_opt_set(sbinfo->si_mntflags, DIRREN);
27076+ break;
27077+ case Opt_nodirren:
27078+ err = 1;
27079+ if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27080+ err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
27081+ DR_FLUSHED));
27082+ if (!err)
27083+ err = 1;
27084+ }
27085+ if (err == 1)
27086+ au_opt_clr(sbinfo->si_mntflags, DIRREN);
27087+ break;
27088+
c1595e42 27089+ case Opt_acl:
2121bcd9 27090+ sb->s_flags |= SB_POSIXACL;
c1595e42
JR
27091+ break;
27092+ case Opt_noacl:
2121bcd9 27093+ sb->s_flags &= ~SB_POSIXACL;
c1595e42
JR
27094+ break;
27095+
1facf9fc 27096+ default:
27097+ err = 0;
27098+ break;
27099+ }
27100+
27101+ return err;
27102+}
27103+
27104+/*
27105+ * returns tri-state.
27106+ * plus: processed without an error
27107+ * zero: unprocessed
27108+ * minus: error
27109+ */
27110+static int au_opt_br(struct super_block *sb, struct au_opt *opt,
27111+ struct au_opts *opts)
27112+{
27113+ int err, do_refresh;
27114+
27115+ err = 0;
27116+ switch (opt->type) {
27117+ case Opt_append:
5afbbe0d 27118+ opt->add.bindex = au_sbbot(sb) + 1;
1facf9fc 27119+ if (opt->add.bindex < 0)
27120+ opt->add.bindex = 0;
27121+ goto add;
eca34b5c 27122+ /* Always goto add, not fallthrough */
1facf9fc 27123+ case Opt_prepend:
27124+ opt->add.bindex = 0;
42a736d3 27125+ fallthrough;
f6b6e03d 27126+ add: /* indented label */
1facf9fc 27127+ case Opt_add:
27128+ err = au_br_add(sb, &opt->add,
27129+ au_ftest_opts(opts->flags, REMOUNT));
27130+ if (!err) {
27131+ err = 1;
027c5e7a 27132+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 27133+ }
27134+ break;
27135+
27136+ case Opt_del:
27137+ case Opt_idel:
27138+ err = au_br_del(sb, &opt->del,
27139+ au_ftest_opts(opts->flags, REMOUNT));
27140+ if (!err) {
27141+ err = 1;
27142+ au_fset_opts(opts->flags, TRUNC_XIB);
027c5e7a 27143+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 27144+ }
27145+ break;
27146+
27147+ case Opt_mod:
27148+ case Opt_imod:
27149+ err = au_br_mod(sb, &opt->mod,
27150+ au_ftest_opts(opts->flags, REMOUNT),
27151+ &do_refresh);
27152+ if (!err) {
27153+ err = 1;
027c5e7a
AM
27154+ if (do_refresh)
27155+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 27156+ }
27157+ break;
27158+ }
1facf9fc 27159+ return err;
27160+}
27161+
27162+static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
27163+ struct au_opt_xino **opt_xino,
27164+ struct au_opts *opts)
27165+{
27166+ int err;
1facf9fc 27167+
27168+ err = 0;
27169+ switch (opt->type) {
27170+ case Opt_xino:
062440b3
AM
27171+ err = au_xino_set(sb, &opt->xino,
27172+ !!au_ftest_opts(opts->flags, REMOUNT));
27173+ if (unlikely(err))
27174+ break;
27175+
27176+ *opt_xino = &opt->xino;
1facf9fc 27177+ break;
27178+
27179+ case Opt_noxino:
27180+ au_xino_clr(sb);
1facf9fc 27181+ *opt_xino = (void *)-1;
27182+ break;
27183+ }
27184+
27185+ return err;
27186+}
27187+
27188+int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27189+ unsigned int pending)
27190+{
076b876e 27191+ int err, fhsm;
5afbbe0d 27192+ aufs_bindex_t bindex, bbot;
79b8bda9 27193+ unsigned char do_plink, skip, do_free, can_no_dreval;
1facf9fc 27194+ struct au_branch *br;
27195+ struct au_wbr *wbr;
79b8bda9 27196+ struct dentry *root, *dentry;
1facf9fc 27197+ struct inode *dir, *h_dir;
27198+ struct au_sbinfo *sbinfo;
27199+ struct au_hinode *hdir;
27200+
dece6358
AM
27201+ SiMustAnyLock(sb);
27202+
1facf9fc 27203+ sbinfo = au_sbi(sb);
27204+ AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
27205+
2121bcd9 27206+ if (!(sb_flags & SB_RDONLY)) {
dece6358 27207+ if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
0c3ec466 27208+ pr_warn("first branch should be rw\n");
dece6358 27209+ if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
febd17d6 27210+ pr_warn_once("shwh should be used with ro\n");
dece6358 27211+ }
1facf9fc 27212+
4a4d8108 27213+ if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
1facf9fc 27214+ && !au_opt_test(sbinfo->si_mntflags, XINO))
febd17d6 27215+ pr_warn_once("udba=*notify requires xino\n");
1facf9fc 27216+
076b876e 27217+ if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
febd17d6
JR
27218+ pr_warn_once("dirperm1 breaks the protection"
27219+ " by the permission bits on the lower branch\n");
076b876e 27220+
1facf9fc 27221+ err = 0;
076b876e 27222+ fhsm = 0;
1facf9fc 27223+ root = sb->s_root;
5527c038 27224+ dir = d_inode(root);
1facf9fc 27225+ do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
79b8bda9
AM
27226+ can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
27227+ UDBA_NONE);
5afbbe0d
AM
27228+ bbot = au_sbbot(sb);
27229+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
1facf9fc 27230+ skip = 0;
27231+ h_dir = au_h_iptr(dir, bindex);
27232+ br = au_sbr(sb, bindex);
1facf9fc 27233+
c1595e42
JR
27234+ if ((br->br_perm & AuBrAttr_ICEX)
27235+ && !h_dir->i_op->listxattr)
27236+ br->br_perm &= ~AuBrAttr_ICEX;
43982f53 27237+#if 0 /* untested */
c1595e42 27238+ if ((br->br_perm & AuBrAttr_ICEX_SEC)
2121bcd9 27239+ && (au_br_sb(br)->s_flags & SB_NOSEC))
c1595e42
JR
27240+ br->br_perm &= ~AuBrAttr_ICEX_SEC;
27241+#endif
27242+
27243+ do_free = 0;
1facf9fc 27244+ wbr = br->br_wbr;
27245+ if (wbr)
27246+ wbr_wh_read_lock(wbr);
27247+
1e00d052 27248+ if (!au_br_writable(br->br_perm)) {
1facf9fc 27249+ do_free = !!wbr;
27250+ skip = (!wbr
27251+ || (!wbr->wbr_whbase
27252+ && !wbr->wbr_plink
27253+ && !wbr->wbr_orph));
1e00d052 27254+ } else if (!au_br_wh_linkable(br->br_perm)) {
1facf9fc 27255+ /* skip = (!br->br_whbase && !br->br_orph); */
27256+ skip = (!wbr || !wbr->wbr_whbase);
27257+ if (skip && wbr) {
27258+ if (do_plink)
27259+ skip = !!wbr->wbr_plink;
27260+ else
27261+ skip = !wbr->wbr_plink;
27262+ }
1e00d052 27263+ } else {
1facf9fc 27264+ /* skip = (br->br_whbase && br->br_ohph); */
27265+ skip = (wbr && wbr->wbr_whbase);
27266+ if (skip) {
27267+ if (do_plink)
27268+ skip = !!wbr->wbr_plink;
27269+ else
27270+ skip = !wbr->wbr_plink;
27271+ }
1facf9fc 27272+ }
27273+ if (wbr)
27274+ wbr_wh_read_unlock(wbr);
27275+
79b8bda9
AM
27276+ if (can_no_dreval) {
27277+ dentry = br->br_path.dentry;
27278+ spin_lock(&dentry->d_lock);
27279+ if (dentry->d_flags &
27280+ (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
27281+ can_no_dreval = 0;
27282+ spin_unlock(&dentry->d_lock);
27283+ }
27284+
076b876e
AM
27285+ if (au_br_fhsm(br->br_perm)) {
27286+ fhsm++;
27287+ AuDebugOn(!br->br_fhsm);
27288+ }
27289+
1facf9fc 27290+ if (skip)
27291+ continue;
27292+
27293+ hdir = au_hi(dir, bindex);
5afbbe0d 27294+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 27295+ if (wbr)
27296+ wbr_wh_write_lock(wbr);
86dc4139 27297+ err = au_wh_init(br, sb);
1facf9fc 27298+ if (wbr)
27299+ wbr_wh_write_unlock(wbr);
5afbbe0d 27300+ au_hn_inode_unlock(hdir);
1facf9fc 27301+
27302+ if (!err && do_free) {
9f237c51 27303+ au_kfree_rcu(wbr);
1facf9fc 27304+ br->br_wbr = NULL;
27305+ }
27306+ }
27307+
79b8bda9
AM
27308+ if (can_no_dreval)
27309+ au_fset_si(sbinfo, NO_DREVAL);
27310+ else
27311+ au_fclr_si(sbinfo, NO_DREVAL);
27312+
c1595e42 27313+ if (fhsm >= 2) {
076b876e 27314+ au_fset_si(sbinfo, FHSM);
5afbbe0d 27315+ for (bindex = bbot; bindex >= 0; bindex--) {
c1595e42
JR
27316+ br = au_sbr(sb, bindex);
27317+ if (au_br_fhsm(br->br_perm)) {
27318+ au_fhsm_set_bottom(sb, bindex);
27319+ break;
27320+ }
27321+ }
27322+ } else {
076b876e 27323+ au_fclr_si(sbinfo, FHSM);
c1595e42
JR
27324+ au_fhsm_set_bottom(sb, -1);
27325+ }
076b876e 27326+
1facf9fc 27327+ return err;
27328+}
27329+
27330+int au_opts_mount(struct super_block *sb, struct au_opts *opts)
27331+{
27332+ int err;
27333+ unsigned int tmp;
5afbbe0d 27334+ aufs_bindex_t bindex, bbot;
1facf9fc 27335+ struct au_opt *opt;
27336+ struct au_opt_xino *opt_xino, xino;
27337+ struct au_sbinfo *sbinfo;
027c5e7a 27338+ struct au_branch *br;
076b876e 27339+ struct inode *dir;
1facf9fc 27340+
dece6358
AM
27341+ SiMustWriteLock(sb);
27342+
1facf9fc 27343+ err = 0;
27344+ opt_xino = NULL;
27345+ opt = opts->opt;
27346+ while (err >= 0 && opt->type != Opt_tail)
27347+ err = au_opt_simple(sb, opt++, opts);
27348+ if (err > 0)
27349+ err = 0;
27350+ else if (unlikely(err < 0))
27351+ goto out;
27352+
27353+ /* disable xino and udba temporary */
27354+ sbinfo = au_sbi(sb);
27355+ tmp = sbinfo->si_mntflags;
27356+ au_opt_clr(sbinfo->si_mntflags, XINO);
27357+ au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
27358+
27359+ opt = opts->opt;
27360+ while (err >= 0 && opt->type != Opt_tail)
27361+ err = au_opt_br(sb, opt++, opts);
27362+ if (err > 0)
27363+ err = 0;
27364+ else if (unlikely(err < 0))
27365+ goto out;
27366+
5afbbe0d
AM
27367+ bbot = au_sbbot(sb);
27368+ if (unlikely(bbot < 0)) {
1facf9fc 27369+ err = -EINVAL;
4a4d8108 27370+ pr_err("no branches\n");
1facf9fc 27371+ goto out;
27372+ }
27373+
27374+ if (au_opt_test(tmp, XINO))
27375+ au_opt_set(sbinfo->si_mntflags, XINO);
27376+ opt = opts->opt;
27377+ while (!err && opt->type != Opt_tail)
27378+ err = au_opt_xino(sb, opt++, &opt_xino, opts);
27379+ if (unlikely(err))
27380+ goto out;
27381+
27382+ err = au_opts_verify(sb, sb->s_flags, tmp);
27383+ if (unlikely(err))
27384+ goto out;
27385+
27386+ /* restore xino */
27387+ if (au_opt_test(tmp, XINO) && !opt_xino) {
27388+ xino.file = au_xino_def(sb);
27389+ err = PTR_ERR(xino.file);
27390+ if (IS_ERR(xino.file))
27391+ goto out;
27392+
27393+ err = au_xino_set(sb, &xino, /*remount*/0);
27394+ fput(xino.file);
27395+ if (unlikely(err))
27396+ goto out;
27397+ }
27398+
27399+ /* restore udba */
027c5e7a 27400+ tmp &= AuOptMask_UDBA;
1facf9fc 27401+ sbinfo->si_mntflags &= ~AuOptMask_UDBA;
027c5e7a 27402+ sbinfo->si_mntflags |= tmp;
5afbbe0d
AM
27403+ bbot = au_sbbot(sb);
27404+ for (bindex = 0; bindex <= bbot; bindex++) {
027c5e7a
AM
27405+ br = au_sbr(sb, bindex);
27406+ err = au_hnotify_reset_br(tmp, br, br->br_perm);
27407+ if (unlikely(err))
27408+ AuIOErr("hnotify failed on br %d, %d, ignored\n",
27409+ bindex, err);
27410+ /* go on even if err */
27411+ }
4a4d8108 27412+ if (au_opt_test(tmp, UDBA_HNOTIFY)) {
5527c038 27413+ dir = d_inode(sb->s_root);
4a4d8108 27414+ au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
1facf9fc 27415+ }
27416+
4f0767ce 27417+out:
1facf9fc 27418+ return err;
27419+}
27420+
27421+int au_opts_remount(struct super_block *sb, struct au_opts *opts)
27422+{
27423+ int err, rerr;
79b8bda9 27424+ unsigned char no_dreval;
1facf9fc 27425+ struct inode *dir;
27426+ struct au_opt_xino *opt_xino;
27427+ struct au_opt *opt;
27428+ struct au_sbinfo *sbinfo;
27429+
dece6358
AM
27430+ SiMustWriteLock(sb);
27431+
8b6a4947
AM
27432+ err = au_dr_opt_flush(sb);
27433+ if (unlikely(err))
27434+ goto out;
27435+ au_fset_opts(opts->flags, DR_FLUSHED);
27436+
5527c038 27437+ dir = d_inode(sb->s_root);
1facf9fc 27438+ sbinfo = au_sbi(sb);
1facf9fc 27439+ opt_xino = NULL;
27440+ opt = opts->opt;
27441+ while (err >= 0 && opt->type != Opt_tail) {
27442+ err = au_opt_simple(sb, opt, opts);
27443+ if (!err)
27444+ err = au_opt_br(sb, opt, opts);
27445+ if (!err)
27446+ err = au_opt_xino(sb, opt, &opt_xino, opts);
27447+ opt++;
27448+ }
27449+ if (err > 0)
27450+ err = 0;
27451+ AuTraceErr(err);
27452+ /* go on even err */
27453+
79b8bda9 27454+ no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
1facf9fc 27455+ rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
27456+ if (unlikely(rerr && !err))
27457+ err = rerr;
27458+
79b8bda9 27459+ if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
b95c5147 27460+ au_fset_opts(opts->flags, REFRESH_IDOP);
79b8bda9 27461+
1facf9fc 27462+ if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
27463+ rerr = au_xib_trunc(sb);
27464+ if (unlikely(rerr && !err))
27465+ err = rerr;
27466+ }
27467+
27468+ /* will be handled by the caller */
027c5e7a 27469+ if (!au_ftest_opts(opts->flags, REFRESH)
79b8bda9
AM
27470+ && (opts->given_udba
27471+ || au_opt_test(sbinfo->si_mntflags, XINO)
b95c5147 27472+ || au_ftest_opts(opts->flags, REFRESH_IDOP)
79b8bda9 27473+ ))
027c5e7a 27474+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 27475+
27476+ AuDbg("status 0x%x\n", opts->flags);
8b6a4947
AM
27477+
27478+out:
1facf9fc 27479+ return err;
27480+}
27481+
27482+/* ---------------------------------------------------------------------- */
27483+
27484+unsigned int au_opt_udba(struct super_block *sb)
27485+{
27486+ return au_mntflags(sb) & AuOptMask_UDBA;
27487+}
7f207e10 27488diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
eca34b5c 27489--- /usr/share/empty/fs/aufs/opts.h 1970-01-01 01:00:00.000000000 +0100
46016270 27490+++ linux/fs/aufs/opts.h 2021-02-24 13:33:42.747680497 +0100
062440b3
AM
27491@@ -0,0 +1,225 @@
27492+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 27493+/*
d58c55f2 27494+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 27495+ *
27496+ * This program, aufs is free software; you can redistribute it and/or modify
27497+ * it under the terms of the GNU General Public License as published by
27498+ * the Free Software Foundation; either version 2 of the License, or
27499+ * (at your option) any later version.
dece6358
AM
27500+ *
27501+ * This program is distributed in the hope that it will be useful,
27502+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
27503+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27504+ * GNU General Public License for more details.
27505+ *
27506+ * You should have received a copy of the GNU General Public License
523b37e3 27507+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 27508+ */
27509+
27510+/*
27511+ * mount options/flags
27512+ */
27513+
27514+#ifndef __AUFS_OPTS_H__
27515+#define __AUFS_OPTS_H__
27516+
27517+#ifdef __KERNEL__
27518+
dece6358 27519+#include <linux/path.h>
1facf9fc 27520+
dece6358 27521+struct file;
dece6358 27522+
1facf9fc 27523+/* ---------------------------------------------------------------------- */
27524+
27525+/* mount flags */
27526+#define AuOpt_XINO 1 /* external inode number bitmap
27527+ and translation table */
27528+#define AuOpt_TRUNC_XINO (1 << 1) /* truncate xino files */
27529+#define AuOpt_UDBA_NONE (1 << 2) /* users direct branch access */
27530+#define AuOpt_UDBA_REVAL (1 << 3)
4a4d8108 27531+#define AuOpt_UDBA_HNOTIFY (1 << 4)
dece6358
AM
27532+#define AuOpt_SHWH (1 << 5) /* show whiteout */
27533+#define AuOpt_PLINK (1 << 6) /* pseudo-link */
076b876e
AM
27534+#define AuOpt_DIRPERM1 (1 << 7) /* ignore the lower dir's perm
27535+ bits */
dece6358
AM
27536+#define AuOpt_ALWAYS_DIROPQ (1 << 9) /* policy to creating diropq */
27537+#define AuOpt_SUM (1 << 10) /* summation for statfs(2) */
27538+#define AuOpt_SUM_W (1 << 11) /* unimplemented */
27539+#define AuOpt_WARN_PERM (1 << 12) /* warn when add-branch */
eca801bf 27540+#define AuOpt_VERBOSE (1 << 13) /* print the cause of error */
4a4d8108 27541+#define AuOpt_DIO (1 << 14) /* direct io */
8b6a4947 27542+#define AuOpt_DIRREN (1 << 15) /* directory rename */
1facf9fc 27543+
4a4d8108
AM
27544+#ifndef CONFIG_AUFS_HNOTIFY
27545+#undef AuOpt_UDBA_HNOTIFY
27546+#define AuOpt_UDBA_HNOTIFY 0
1facf9fc 27547+#endif
8b6a4947
AM
27548+#ifndef CONFIG_AUFS_DIRREN
27549+#undef AuOpt_DIRREN
27550+#define AuOpt_DIRREN 0
27551+#endif
dece6358
AM
27552+#ifndef CONFIG_AUFS_SHWH
27553+#undef AuOpt_SHWH
27554+#define AuOpt_SHWH 0
27555+#endif
1facf9fc 27556+
27557+#define AuOpt_Def (AuOpt_XINO \
27558+ | AuOpt_UDBA_REVAL \
27559+ | AuOpt_PLINK \
27560+ /* | AuOpt_DIRPERM1 */ \
27561+ | AuOpt_WARN_PERM)
27562+#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
27563+ | AuOpt_UDBA_REVAL \
4a4d8108 27564+ | AuOpt_UDBA_HNOTIFY)
1facf9fc 27565+
27566+#define au_opt_test(flags, name) (flags & AuOpt_##name)
27567+#define au_opt_set(flags, name) do { \
27568+ BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
27569+ ((flags) |= AuOpt_##name); \
27570+} while (0)
27571+#define au_opt_set_udba(flags, name) do { \
27572+ (flags) &= ~AuOptMask_UDBA; \
27573+ ((flags) |= AuOpt_##name); \
27574+} while (0)
7f207e10
AM
27575+#define au_opt_clr(flags, name) do { \
27576+ ((flags) &= ~AuOpt_##name); \
27577+} while (0)
1facf9fc 27578+
e49829fe
JR
27579+static inline unsigned int au_opts_plink(unsigned int mntflags)
27580+{
27581+#ifdef CONFIG_PROC_FS
27582+ return mntflags;
27583+#else
27584+ return mntflags & ~AuOpt_PLINK;
27585+#endif
27586+}
27587+
1facf9fc 27588+/* ---------------------------------------------------------------------- */
27589+
27590+/* policies to select one among multiple writable branches */
27591+enum {
27592+ AuWbrCreate_TDP, /* top down parent */
27593+ AuWbrCreate_RR, /* round robin */
27594+ AuWbrCreate_MFS, /* most free space */
27595+ AuWbrCreate_MFSV, /* mfs with seconds */
27596+ AuWbrCreate_MFSRR, /* mfs then rr */
27597+ AuWbrCreate_MFSRRV, /* mfs then rr with seconds */
f2c43d5f
AM
27598+ AuWbrCreate_TDMFS, /* top down regardless parent and mfs */
27599+ AuWbrCreate_TDMFSV, /* top down regardless parent and mfs */
1facf9fc 27600+ AuWbrCreate_PMFS, /* parent and mfs */
27601+ AuWbrCreate_PMFSV, /* parent and mfs with seconds */
392086de
AM
27602+ AuWbrCreate_PMFSRR, /* parent, mfs and round-robin */
27603+ AuWbrCreate_PMFSRRV, /* plus seconds */
1facf9fc 27604+
27605+ AuWbrCreate_Def = AuWbrCreate_TDP
27606+};
27607+
27608+enum {
27609+ AuWbrCopyup_TDP, /* top down parent */
27610+ AuWbrCopyup_BUP, /* bottom up parent */
27611+ AuWbrCopyup_BU, /* bottom up */
27612+
27613+ AuWbrCopyup_Def = AuWbrCopyup_TDP
27614+};
27615+
27616+/* ---------------------------------------------------------------------- */
27617+
27618+struct au_opt_add {
27619+ aufs_bindex_t bindex;
27620+ char *pathname;
27621+ int perm;
27622+ struct path path;
27623+};
27624+
27625+struct au_opt_del {
27626+ char *pathname;
27627+ struct path h_path;
27628+};
27629+
27630+struct au_opt_mod {
27631+ char *path;
27632+ int perm;
27633+ struct dentry *h_root;
27634+};
27635+
27636+struct au_opt_xino {
27637+ char *path;
27638+ struct file *file;
27639+};
27640+
27641+struct au_opt_xino_itrunc {
27642+ aufs_bindex_t bindex;
27643+};
27644+
27645+struct au_opt_wbr_create {
27646+ int wbr_create;
27647+ int mfs_second;
27648+ unsigned long long mfsrr_watermark;
27649+};
27650+
27651+struct au_opt {
27652+ int type;
27653+ union {
27654+ struct au_opt_xino xino;
27655+ struct au_opt_xino_itrunc xino_itrunc;
27656+ struct au_opt_add add;
27657+ struct au_opt_del del;
27658+ struct au_opt_mod mod;
27659+ int dirwh;
27660+ int rdcache;
27661+ unsigned int rdblk;
27662+ unsigned int rdhash;
27663+ int udba;
27664+ struct au_opt_wbr_create wbr_create;
27665+ int wbr_copyup;
076b876e 27666+ unsigned int fhsm_second;
1facf9fc 27667+ };
27668+};
27669+
27670+/* opts flags */
27671+#define AuOpts_REMOUNT 1
027c5e7a
AM
27672+#define AuOpts_REFRESH (1 << 1)
27673+#define AuOpts_TRUNC_XIB (1 << 2)
27674+#define AuOpts_REFRESH_DYAOP (1 << 3)
b95c5147 27675+#define AuOpts_REFRESH_IDOP (1 << 4)
8b6a4947 27676+#define AuOpts_DR_FLUSHED (1 << 5)
1facf9fc 27677+#define au_ftest_opts(flags, name) ((flags) & AuOpts_##name)
7f207e10
AM
27678+#define au_fset_opts(flags, name) \
27679+ do { (flags) |= AuOpts_##name; } while (0)
27680+#define au_fclr_opts(flags, name) \
27681+ do { (flags) &= ~AuOpts_##name; } while (0)
1facf9fc 27682+
8b6a4947
AM
27683+#ifndef CONFIG_AUFS_DIRREN
27684+#undef AuOpts_DR_FLUSHED
27685+#define AuOpts_DR_FLUSHED 0
27686+#endif
27687+
1facf9fc 27688+struct au_opts {
27689+ struct au_opt *opt;
27690+ int max_opt;
27691+
27692+ unsigned int given_udba;
27693+ unsigned int flags;
27694+ unsigned long sb_flags;
27695+};
27696+
27697+/* ---------------------------------------------------------------------- */
27698+
7e9cd9fe 27699+/* opts.c */
076b876e 27700+void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
1facf9fc 27701+const char *au_optstr_udba(int udba);
27702+const char *au_optstr_wbr_copyup(int wbr_copyup);
27703+const char *au_optstr_wbr_create(int wbr_create);
27704+
27705+void au_opts_free(struct au_opts *opts);
3c1bdaff 27706+struct super_block;
1facf9fc 27707+int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
27708+int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27709+ unsigned int pending);
27710+int au_opts_mount(struct super_block *sb, struct au_opts *opts);
27711+int au_opts_remount(struct super_block *sb, struct au_opts *opts);
27712+
27713+unsigned int au_opt_udba(struct super_block *sb);
27714+
1facf9fc 27715+#endif /* __KERNEL__ */
27716+#endif /* __AUFS_OPTS_H__ */
7f207e10 27717diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
eca34b5c 27718--- /usr/share/empty/fs/aufs/plink.c 1970-01-01 01:00:00.000000000 +0100
46016270 27719+++ linux/fs/aufs/plink.c 2021-02-24 13:33:42.747680497 +0100
062440b3 27720@@ -0,0 +1,516 @@
cd7a4cd9 27721+// SPDX-License-Identifier: GPL-2.0
1facf9fc 27722+/*
d58c55f2 27723+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 27724+ *
27725+ * This program, aufs is free software; you can redistribute it and/or modify
27726+ * it under the terms of the GNU General Public License as published by
27727+ * the Free Software Foundation; either version 2 of the License, or
27728+ * (at your option) any later version.
dece6358
AM
27729+ *
27730+ * This program is distributed in the hope that it will be useful,
27731+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
27732+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27733+ * GNU General Public License for more details.
27734+ *
27735+ * You should have received a copy of the GNU General Public License
523b37e3 27736+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 27737+ */
27738+
27739+/*
27740+ * pseudo-link
27741+ */
27742+
27743+#include "aufs.h"
27744+
27745+/*
e49829fe 27746+ * the pseudo-link maintenance mode.
1facf9fc 27747+ * during a user process maintains the pseudo-links,
27748+ * prohibit adding a new plink and branch manipulation.
e49829fe
JR
27749+ *
27750+ * Flags
27751+ * NOPLM:
27752+ * For entry functions which will handle plink, and i_mutex is already held
27753+ * in VFS.
27754+ * They cannot wait and should return an error at once.
27755+ * Callers has to check the error.
27756+ * NOPLMW:
27757+ * For entry functions which will handle plink, but i_mutex is not held
27758+ * in VFS.
27759+ * They can wait the plink maintenance mode to finish.
27760+ *
27761+ * They behave like F_SETLK and F_SETLKW.
27762+ * If the caller never handle plink, then both flags are unnecessary.
1facf9fc 27763+ */
e49829fe
JR
27764+
27765+int au_plink_maint(struct super_block *sb, int flags)
1facf9fc 27766+{
e49829fe
JR
27767+ int err;
27768+ pid_t pid, ppid;
f0c0a007 27769+ struct task_struct *parent, *prev;
e49829fe 27770+ struct au_sbinfo *sbi;
dece6358
AM
27771+
27772+ SiMustAnyLock(sb);
27773+
e49829fe
JR
27774+ err = 0;
27775+ if (!au_opt_test(au_mntflags(sb), PLINK))
27776+ goto out;
27777+
27778+ sbi = au_sbi(sb);
27779+ pid = sbi->si_plink_maint_pid;
27780+ if (!pid || pid == current->pid)
27781+ goto out;
27782+
27783+ /* todo: it highly depends upon /sbin/mount.aufs */
f0c0a007
AM
27784+ prev = NULL;
27785+ parent = current;
27786+ ppid = 0;
e49829fe 27787+ rcu_read_lock();
f0c0a007
AM
27788+ while (1) {
27789+ parent = rcu_dereference(parent->real_parent);
27790+ if (parent == prev)
27791+ break;
27792+ ppid = task_pid_vnr(parent);
27793+ if (pid == ppid) {
27794+ rcu_read_unlock();
27795+ goto out;
27796+ }
27797+ prev = parent;
27798+ }
e49829fe 27799+ rcu_read_unlock();
e49829fe
JR
27800+
27801+ if (au_ftest_lock(flags, NOPLMW)) {
027c5e7a
AM
27802+ /* if there is no i_mutex lock in VFS, we don't need to wait */
27803+ /* AuDebugOn(!lockdep_depth(current)); */
e49829fe
JR
27804+ while (sbi->si_plink_maint_pid) {
27805+ si_read_unlock(sb);
27806+ /* gave up wake_up_bit() */
27807+ wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
27808+
27809+ if (au_ftest_lock(flags, FLUSH))
27810+ au_nwt_flush(&sbi->si_nowait);
27811+ si_noflush_read_lock(sb);
27812+ }
27813+ } else if (au_ftest_lock(flags, NOPLM)) {
27814+ AuDbg("ppid %d, pid %d\n", ppid, pid);
27815+ err = -EAGAIN;
27816+ }
27817+
27818+out:
27819+ return err;
4a4d8108
AM
27820+}
27821+
e49829fe 27822+void au_plink_maint_leave(struct au_sbinfo *sbinfo)
4a4d8108 27823+{
4a4d8108 27824+ spin_lock(&sbinfo->si_plink_maint_lock);
027c5e7a 27825+ sbinfo->si_plink_maint_pid = 0;
4a4d8108 27826+ spin_unlock(&sbinfo->si_plink_maint_lock);
027c5e7a 27827+ wake_up_all(&sbinfo->si_plink_wq);
4a4d8108
AM
27828+}
27829+
e49829fe 27830+int au_plink_maint_enter(struct super_block *sb)
4a4d8108
AM
27831+{
27832+ int err;
4a4d8108
AM
27833+ struct au_sbinfo *sbinfo;
27834+
27835+ err = 0;
4a4d8108
AM
27836+ sbinfo = au_sbi(sb);
27837+ /* make sure i am the only one in this fs */
e49829fe
JR
27838+ si_write_lock(sb, AuLock_FLUSH);
27839+ if (au_opt_test(au_mntflags(sb), PLINK)) {
27840+ spin_lock(&sbinfo->si_plink_maint_lock);
27841+ if (!sbinfo->si_plink_maint_pid)
27842+ sbinfo->si_plink_maint_pid = current->pid;
27843+ else
27844+ err = -EBUSY;
27845+ spin_unlock(&sbinfo->si_plink_maint_lock);
27846+ }
4a4d8108
AM
27847+ si_write_unlock(sb);
27848+
27849+ return err;
1facf9fc 27850+}
27851+
27852+/* ---------------------------------------------------------------------- */
27853+
1facf9fc 27854+#ifdef CONFIG_AUFS_DEBUG
27855+void au_plink_list(struct super_block *sb)
27856+{
86dc4139 27857+ int i;
1facf9fc 27858+ struct au_sbinfo *sbinfo;
8b6a4947
AM
27859+ struct hlist_bl_head *hbl;
27860+ struct hlist_bl_node *pos;
5afbbe0d 27861+ struct au_icntnr *icntnr;
1facf9fc 27862+
dece6358
AM
27863+ SiMustAnyLock(sb);
27864+
1facf9fc 27865+ sbinfo = au_sbi(sb);
27866+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 27867+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 27868+
86dc4139 27869+ for (i = 0; i < AuPlink_NHASH; i++) {
8b6a4947
AM
27870+ hbl = sbinfo->si_plink + i;
27871+ hlist_bl_lock(hbl);
27872+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
5afbbe0d 27873+ AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
8b6a4947 27874+ hlist_bl_unlock(hbl);
86dc4139 27875+ }
1facf9fc 27876+}
27877+#endif
27878+
27879+/* is the inode pseudo-linked? */
27880+int au_plink_test(struct inode *inode)
27881+{
86dc4139 27882+ int found, i;
1facf9fc 27883+ struct au_sbinfo *sbinfo;
8b6a4947
AM
27884+ struct hlist_bl_head *hbl;
27885+ struct hlist_bl_node *pos;
5afbbe0d 27886+ struct au_icntnr *icntnr;
1facf9fc 27887+
27888+ sbinfo = au_sbi(inode->i_sb);
dece6358 27889+ AuRwMustAnyLock(&sbinfo->si_rwsem);
1facf9fc 27890+ AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
e49829fe 27891+ AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
1facf9fc 27892+
27893+ found = 0;
86dc4139 27894+ i = au_plink_hash(inode->i_ino);
8b6a4947
AM
27895+ hbl = sbinfo->si_plink + i;
27896+ hlist_bl_lock(hbl);
27897+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
5afbbe0d 27898+ if (&icntnr->vfs_inode == inode) {
1facf9fc 27899+ found = 1;
27900+ break;
27901+ }
8b6a4947 27902+ hlist_bl_unlock(hbl);
1facf9fc 27903+ return found;
27904+}
27905+
27906+/* ---------------------------------------------------------------------- */
27907+
27908+/*
27909+ * generate a name for plink.
27910+ * the file will be stored under AUFS_WH_PLINKDIR.
27911+ */
27912+/* 20 is max digits length of ulong 64 */
27913+#define PLINK_NAME_LEN ((20 + 1) * 2)
27914+
27915+static int plink_name(char *name, int len, struct inode *inode,
27916+ aufs_bindex_t bindex)
27917+{
27918+ int rlen;
27919+ struct inode *h_inode;
27920+
27921+ h_inode = au_h_iptr(inode, bindex);
27922+ rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
27923+ return rlen;
27924+}
27925+
7f207e10
AM
27926+struct au_do_plink_lkup_args {
27927+ struct dentry **errp;
27928+ struct qstr *tgtname;
27929+ struct dentry *h_parent;
27930+ struct au_branch *br;
27931+};
27932+
27933+static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
27934+ struct dentry *h_parent,
27935+ struct au_branch *br)
27936+{
27937+ struct dentry *h_dentry;
febd17d6 27938+ struct inode *h_inode;
7f207e10 27939+
febd17d6 27940+ h_inode = d_inode(h_parent);
be118d29 27941+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD2);
b4510431 27942+ h_dentry = vfsub_lkup_one(tgtname, h_parent);
3c1bdaff 27943+ inode_unlock_shared(h_inode);
7f207e10
AM
27944+ return h_dentry;
27945+}
27946+
27947+static void au_call_do_plink_lkup(void *args)
27948+{
27949+ struct au_do_plink_lkup_args *a = args;
27950+ *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
27951+}
27952+
1facf9fc 27953+/* lookup the plink-ed @inode under the branch at @bindex */
27954+struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
27955+{
27956+ struct dentry *h_dentry, *h_parent;
27957+ struct au_branch *br;
7f207e10 27958+ int wkq_err;
1facf9fc 27959+ char a[PLINK_NAME_LEN];
0c3ec466 27960+ struct qstr tgtname = QSTR_INIT(a, 0);
1facf9fc 27961+
e49829fe
JR
27962+ AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
27963+
1facf9fc 27964+ br = au_sbr(inode->i_sb, bindex);
27965+ h_parent = br->br_wbr->wbr_plink;
1facf9fc 27966+ tgtname.len = plink_name(a, sizeof(a), inode, bindex);
27967+
2dfbb274 27968+ if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
7f207e10
AM
27969+ struct au_do_plink_lkup_args args = {
27970+ .errp = &h_dentry,
27971+ .tgtname = &tgtname,
27972+ .h_parent = h_parent,
27973+ .br = br
27974+ };
27975+
27976+ wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
27977+ if (unlikely(wkq_err))
27978+ h_dentry = ERR_PTR(wkq_err);
27979+ } else
27980+ h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
27981+
1facf9fc 27982+ return h_dentry;
27983+}
27984+
27985+/* create a pseudo-link */
27986+static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
27987+ struct dentry *h_dentry, struct au_branch *br)
27988+{
27989+ int err;
27990+ struct path h_path = {
86dc4139 27991+ .mnt = au_br_mnt(br)
1facf9fc 27992+ };
523b37e3 27993+ struct inode *h_dir, *delegated;
1facf9fc 27994+
5527c038 27995+ h_dir = d_inode(h_parent);
febd17d6 27996+ inode_lock_nested(h_dir, AuLsc_I_CHILD2);
4f0767ce 27997+again:
b4510431 27998+ h_path.dentry = vfsub_lkup_one(tgt, h_parent);
1facf9fc 27999+ err = PTR_ERR(h_path.dentry);
28000+ if (IS_ERR(h_path.dentry))
28001+ goto out;
28002+
28003+ err = 0;
28004+ /* wh.plink dir is not monitored */
7f207e10 28005+ /* todo: is it really safe? */
5527c038
JR
28006+ if (d_is_positive(h_path.dentry)
28007+ && d_inode(h_path.dentry) != d_inode(h_dentry)) {
523b37e3
AM
28008+ delegated = NULL;
28009+ err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
28010+ if (unlikely(err == -EWOULDBLOCK)) {
28011+ pr_warn("cannot retry for NFSv4 delegation"
28012+ " for an internal unlink\n");
28013+ iput(delegated);
28014+ }
1facf9fc 28015+ dput(h_path.dentry);
28016+ h_path.dentry = NULL;
28017+ if (!err)
28018+ goto again;
28019+ }
5527c038 28020+ if (!err && d_is_negative(h_path.dentry)) {
523b37e3
AM
28021+ delegated = NULL;
28022+ err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
28023+ if (unlikely(err == -EWOULDBLOCK)) {
28024+ pr_warn("cannot retry for NFSv4 delegation"
28025+ " for an internal link\n");
28026+ iput(delegated);
28027+ }
28028+ }
1facf9fc 28029+ dput(h_path.dentry);
28030+
4f0767ce 28031+out:
febd17d6 28032+ inode_unlock(h_dir);
1facf9fc 28033+ return err;
28034+}
28035+
28036+struct do_whplink_args {
28037+ int *errp;
28038+ struct qstr *tgt;
28039+ struct dentry *h_parent;
28040+ struct dentry *h_dentry;
28041+ struct au_branch *br;
28042+};
28043+
28044+static void call_do_whplink(void *args)
28045+{
28046+ struct do_whplink_args *a = args;
28047+ *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
28048+}
28049+
28050+static int whplink(struct dentry *h_dentry, struct inode *inode,
28051+ aufs_bindex_t bindex, struct au_branch *br)
28052+{
28053+ int err, wkq_err;
28054+ struct au_wbr *wbr;
28055+ struct dentry *h_parent;
1facf9fc 28056+ char a[PLINK_NAME_LEN];
0c3ec466 28057+ struct qstr tgtname = QSTR_INIT(a, 0);
1facf9fc 28058+
28059+ wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
28060+ h_parent = wbr->wbr_plink;
1facf9fc 28061+ tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28062+
28063+ /* always superio. */
2dfbb274 28064+ if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
1facf9fc 28065+ struct do_whplink_args args = {
28066+ .errp = &err,
28067+ .tgt = &tgtname,
28068+ .h_parent = h_parent,
28069+ .h_dentry = h_dentry,
28070+ .br = br
28071+ };
28072+ wkq_err = au_wkq_wait(call_do_whplink, &args);
28073+ if (unlikely(wkq_err))
28074+ err = wkq_err;
28075+ } else
28076+ err = do_whplink(&tgtname, h_parent, h_dentry, br);
1facf9fc 28077+
28078+ return err;
28079+}
28080+
1facf9fc 28081+/*
28082+ * create a new pseudo-link for @h_dentry on @bindex.
28083+ * the linked inode is held in aufs @inode.
28084+ */
28085+void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
28086+ struct dentry *h_dentry)
28087+{
28088+ struct super_block *sb;
28089+ struct au_sbinfo *sbinfo;
8b6a4947
AM
28090+ struct hlist_bl_head *hbl;
28091+ struct hlist_bl_node *pos;
5afbbe0d 28092+ struct au_icntnr *icntnr;
86dc4139 28093+ int found, err, cnt, i;
1facf9fc 28094+
28095+ sb = inode->i_sb;
28096+ sbinfo = au_sbi(sb);
28097+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 28098+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 28099+
86dc4139 28100+ found = au_plink_test(inode);
4a4d8108 28101+ if (found)
1facf9fc 28102+ return;
4a4d8108 28103+
86dc4139 28104+ i = au_plink_hash(inode->i_ino);
8b6a4947 28105+ hbl = sbinfo->si_plink + i;
5afbbe0d 28106+ au_igrab(inode);
1facf9fc 28107+
8b6a4947
AM
28108+ hlist_bl_lock(hbl);
28109+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink) {
5afbbe0d 28110+ if (&icntnr->vfs_inode == inode) {
4a4d8108
AM
28111+ found = 1;
28112+ break;
28113+ }
1facf9fc 28114+ }
5afbbe0d
AM
28115+ if (!found) {
28116+ icntnr = container_of(inode, struct au_icntnr, vfs_inode);
8b6a4947 28117+ hlist_bl_add_head(&icntnr->plink, hbl);
5afbbe0d 28118+ }
8b6a4947 28119+ hlist_bl_unlock(hbl);
4a4d8108 28120+ if (!found) {
8b6a4947 28121+ cnt = au_hbl_count(hbl);
acd2b654 28122+#define msg "unexpectedly unbalanced or too many pseudo-links"
86dc4139
AM
28123+ if (cnt > AUFS_PLINK_WARN)
28124+ AuWarn1(msg ", %d\n", cnt);
28125+#undef msg
1facf9fc 28126+ err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
5afbbe0d
AM
28127+ if (unlikely(err)) {
28128+ pr_warn("err %d, damaged pseudo link.\n", err);
8b6a4947 28129+ au_hbl_del(&icntnr->plink, hbl);
5afbbe0d 28130+ iput(&icntnr->vfs_inode);
4a4d8108 28131+ }
5afbbe0d
AM
28132+ } else
28133+ iput(&icntnr->vfs_inode);
1facf9fc 28134+}
28135+
28136+/* free all plinks */
e49829fe 28137+void au_plink_put(struct super_block *sb, int verbose)
1facf9fc 28138+{
86dc4139 28139+ int i, warned;
1facf9fc 28140+ struct au_sbinfo *sbinfo;
8b6a4947
AM
28141+ struct hlist_bl_head *hbl;
28142+ struct hlist_bl_node *pos, *tmp;
5afbbe0d 28143+ struct au_icntnr *icntnr;
1facf9fc 28144+
dece6358
AM
28145+ SiMustWriteLock(sb);
28146+
1facf9fc 28147+ sbinfo = au_sbi(sb);
28148+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 28149+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 28150+
1facf9fc 28151+ /* no spin_lock since sbinfo is write-locked */
86dc4139
AM
28152+ warned = 0;
28153+ for (i = 0; i < AuPlink_NHASH; i++) {
8b6a4947
AM
28154+ hbl = sbinfo->si_plink + i;
28155+ if (!warned && verbose && !hlist_bl_empty(hbl)) {
86dc4139
AM
28156+ pr_warn("pseudo-link is not flushed");
28157+ warned = 1;
28158+ }
8b6a4947 28159+ hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink)
5afbbe0d 28160+ iput(&icntnr->vfs_inode);
8b6a4947 28161+ INIT_HLIST_BL_HEAD(hbl);
86dc4139 28162+ }
1facf9fc 28163+}
28164+
e49829fe
JR
28165+void au_plink_clean(struct super_block *sb, int verbose)
28166+{
28167+ struct dentry *root;
28168+
28169+ root = sb->s_root;
28170+ aufs_write_lock(root);
28171+ if (au_opt_test(au_mntflags(sb), PLINK))
28172+ au_plink_put(sb, verbose);
28173+ aufs_write_unlock(root);
28174+}
28175+
86dc4139
AM
28176+static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
28177+{
28178+ int do_put;
5afbbe0d 28179+ aufs_bindex_t btop, bbot, bindex;
86dc4139
AM
28180+
28181+ do_put = 0;
5afbbe0d
AM
28182+ btop = au_ibtop(inode);
28183+ bbot = au_ibbot(inode);
28184+ if (btop >= 0) {
28185+ for (bindex = btop; bindex <= bbot; bindex++) {
86dc4139
AM
28186+ if (!au_h_iptr(inode, bindex)
28187+ || au_ii_br_id(inode, bindex) != br_id)
28188+ continue;
28189+ au_set_h_iptr(inode, bindex, NULL, 0);
28190+ do_put = 1;
28191+ break;
28192+ }
28193+ if (do_put)
5afbbe0d 28194+ for (bindex = btop; bindex <= bbot; bindex++)
86dc4139
AM
28195+ if (au_h_iptr(inode, bindex)) {
28196+ do_put = 0;
28197+ break;
28198+ }
28199+ } else
28200+ do_put = 1;
28201+
28202+ return do_put;
28203+}
28204+
1facf9fc 28205+/* free the plinks on a branch specified by @br_id */
28206+void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
28207+{
28208+ struct au_sbinfo *sbinfo;
8b6a4947
AM
28209+ struct hlist_bl_head *hbl;
28210+ struct hlist_bl_node *pos, *tmp;
5afbbe0d 28211+ struct au_icntnr *icntnr;
1facf9fc 28212+ struct inode *inode;
86dc4139 28213+ int i, do_put;
1facf9fc 28214+
dece6358
AM
28215+ SiMustWriteLock(sb);
28216+
1facf9fc 28217+ sbinfo = au_sbi(sb);
28218+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 28219+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 28220+
8b6a4947 28221+ /* no bit_lock since sbinfo is write-locked */
86dc4139 28222+ for (i = 0; i < AuPlink_NHASH; i++) {
8b6a4947
AM
28223+ hbl = sbinfo->si_plink + i;
28224+ hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink) {
5afbbe0d 28225+ inode = au_igrab(&icntnr->vfs_inode);
86dc4139
AM
28226+ ii_write_lock_child(inode);
28227+ do_put = au_plink_do_half_refresh(inode, br_id);
5afbbe0d 28228+ if (do_put) {
8b6a4947 28229+ hlist_bl_del(&icntnr->plink);
5afbbe0d
AM
28230+ iput(inode);
28231+ }
86dc4139
AM
28232+ ii_write_unlock(inode);
28233+ iput(inode);
dece6358 28234+ }
dece6358
AM
28235+ }
28236+}
7f207e10 28237diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
eca34b5c 28238--- /usr/share/empty/fs/aufs/poll.c 1970-01-01 01:00:00.000000000 +0100
46016270 28239+++ linux/fs/aufs/poll.c 2021-02-24 13:33:42.747680497 +0100
cd7a4cd9
AM
28240@@ -0,0 +1,51 @@
28241+// SPDX-License-Identifier: GPL-2.0
dece6358 28242+/*
d58c55f2 28243+ * Copyright (C) 2005-2020 Junjiro R. Okajima
dece6358
AM
28244+ *
28245+ * This program, aufs is free software; you can redistribute it and/or modify
28246+ * it under the terms of the GNU General Public License as published by
28247+ * the Free Software Foundation; either version 2 of the License, or
28248+ * (at your option) any later version.
28249+ *
28250+ * This program is distributed in the hope that it will be useful,
28251+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28252+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28253+ * GNU General Public License for more details.
28254+ *
28255+ * You should have received a copy of the GNU General Public License
523b37e3 28256+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358
AM
28257+ */
28258+
1308ab2a 28259+/*
28260+ * poll operation
28261+ * There is only one filesystem which implements ->poll operation, currently.
28262+ */
28263+
28264+#include "aufs.h"
28265+
cd7a4cd9 28266+__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt)
1308ab2a 28267+{
be118d29 28268+ __poll_t mask;
1308ab2a 28269+ struct file *h_file;
1308ab2a 28270+ struct super_block *sb;
28271+
28272+ /* We should pretend an error happened. */
be118d29 28273+ mask = EPOLLERR /* | EPOLLIN | EPOLLOUT */;
b912730e 28274+ sb = file->f_path.dentry->d_sb;
e49829fe 28275+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e 28276+
521ced18 28277+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
062440b3
AM
28278+ if (IS_ERR(h_file)) {
28279+ AuDbg("h_file %ld\n", PTR_ERR(h_file));
1308ab2a 28280+ goto out;
062440b3 28281+ }
1308ab2a 28282+
cd7a4cd9 28283+ mask = vfs_poll(h_file, pt);
b912730e 28284+ fput(h_file); /* instead of au_read_post() */
1308ab2a 28285+
4f0767ce 28286+out:
1308ab2a 28287+ si_read_unlock(sb);
062440b3 28288+ if (mask & EPOLLERR)
b00004a5 28289+ AuDbg("mask 0x%x\n", mask);
1308ab2a 28290+ return mask;
28291+}
c1595e42 28292diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
eca34b5c 28293--- /usr/share/empty/fs/aufs/posix_acl.c 1970-01-01 01:00:00.000000000 +0100
46016270 28294+++ linux/fs/aufs/posix_acl.c 2021-06-30 21:35:11.397206648 +0200
0b2a12c6 28295@@ -0,0 +1,106 @@
cd7a4cd9 28296+// SPDX-License-Identifier: GPL-2.0
c1595e42 28297+/*
d58c55f2 28298+ * Copyright (C) 2014-2020 Junjiro R. Okajima
c1595e42
JR
28299+ *
28300+ * This program, aufs is free software; you can redistribute it and/or modify
28301+ * it under the terms of the GNU General Public License as published by
28302+ * the Free Software Foundation; either version 2 of the License, or
28303+ * (at your option) any later version.
28304+ *
28305+ * This program is distributed in the hope that it will be useful,
28306+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28307+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28308+ * GNU General Public License for more details.
28309+ *
28310+ * You should have received a copy of the GNU General Public License
28311+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
28312+ */
28313+
28314+/*
28315+ * posix acl operations
28316+ */
28317+
28318+#include <linux/fs.h>
c1595e42
JR
28319+#include "aufs.h"
28320+
28321+struct posix_acl *aufs_get_acl(struct inode *inode, int type)
28322+{
28323+ struct posix_acl *acl;
28324+ int err;
28325+ aufs_bindex_t bindex;
28326+ struct inode *h_inode;
28327+ struct super_block *sb;
28328+
28329+ acl = NULL;
28330+ sb = inode->i_sb;
28331+ si_read_lock(sb, AuLock_FLUSH);
28332+ ii_read_lock_child(inode);
2121bcd9 28333+ if (!(sb->s_flags & SB_POSIXACL))
c1595e42
JR
28334+ goto out;
28335+
5afbbe0d 28336+ bindex = au_ibtop(inode);
c1595e42
JR
28337+ h_inode = au_h_iptr(inode, bindex);
28338+ if (unlikely(!h_inode
28339+ || ((h_inode->i_mode & S_IFMT)
28340+ != (inode->i_mode & S_IFMT)))) {
28341+ err = au_busy_or_stale();
28342+ acl = ERR_PTR(err);
28343+ goto out;
28344+ }
28345+
28346+ /* always topmost only */
28347+ acl = get_acl(h_inode, type);
9f00928d
AM
28348+ if (IS_ERR(acl))
28349+ forget_cached_acl(inode, type);
28350+ else
a2654f78 28351+ set_cached_acl(inode, type, acl);
c1595e42
JR
28352+
28353+out:
28354+ ii_read_unlock(inode);
28355+ si_read_unlock(sb);
28356+
28357+ AuTraceErrPtr(acl);
28358+ return acl;
28359+}
28360+
0b2a12c6
JR
28361+int aufs_set_acl(struct user_namespace *userns, struct inode *inode,
28362+ struct posix_acl *acl, int type)
c1595e42
JR
28363+{
28364+ int err;
28365+ ssize_t ssz;
28366+ struct dentry *dentry;
f2c43d5f 28367+ struct au_sxattr arg = {
c1595e42
JR
28368+ .type = AU_ACL_SET,
28369+ .u.acl_set = {
28370+ .acl = acl,
28371+ .type = type
28372+ },
28373+ };
28374+
5afbbe0d
AM
28375+ IMustLock(inode);
28376+
c1595e42
JR
28377+ if (inode->i_ino == AUFS_ROOT_INO)
28378+ dentry = dget(inode->i_sb->s_root);
28379+ else {
28380+ dentry = d_find_alias(inode);
28381+ if (!dentry)
28382+ dentry = d_find_any_alias(inode);
28383+ if (!dentry) {
28384+ pr_warn("cannot handle this inode, "
28385+ "please report to aufs-users ML\n");
28386+ err = -ENOENT;
28387+ goto out;
28388+ }
28389+ }
28390+
f2c43d5f 28391+ ssz = au_sxattr(dentry, inode, &arg);
9f00928d
AM
28392+ /* forget even it if succeeds since the branch might set differently */
28393+ forget_cached_acl(inode, type);
c1595e42
JR
28394+ dput(dentry);
28395+ err = ssz;
9f00928d 28396+ if (ssz >= 0)
c1595e42
JR
28397+ err = 0;
28398+
28399+out:
c1595e42
JR
28400+ return err;
28401+}
7f207e10 28402diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
eca34b5c 28403--- /usr/share/empty/fs/aufs/procfs.c 1970-01-01 01:00:00.000000000 +0100
46016270 28404+++ linux/fs/aufs/procfs.c 2021-02-24 13:33:42.747680497 +0100
394e211a 28405@@ -0,0 +1,170 @@
cd7a4cd9 28406+// SPDX-License-Identifier: GPL-2.0
e49829fe 28407+/*
d58c55f2 28408+ * Copyright (C) 2010-2020 Junjiro R. Okajima
e49829fe
JR
28409+ *
28410+ * This program, aufs is free software; you can redistribute it and/or modify
28411+ * it under the terms of the GNU General Public License as published by
28412+ * the Free Software Foundation; either version 2 of the License, or
28413+ * (at your option) any later version.
28414+ *
28415+ * This program is distributed in the hope that it will be useful,
28416+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28417+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28418+ * GNU General Public License for more details.
28419+ *
28420+ * You should have received a copy of the GNU General Public License
523b37e3 28421+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
e49829fe
JR
28422+ */
28423+
28424+/*
28425+ * procfs interfaces
28426+ */
28427+
28428+#include <linux/proc_fs.h>
28429+#include "aufs.h"
28430+
28431+static int au_procfs_plm_release(struct inode *inode, struct file *file)
28432+{
28433+ struct au_sbinfo *sbinfo;
28434+
28435+ sbinfo = file->private_data;
28436+ if (sbinfo) {
28437+ au_plink_maint_leave(sbinfo);
28438+ kobject_put(&sbinfo->si_kobj);
28439+ }
28440+
28441+ return 0;
28442+}
28443+
28444+static void au_procfs_plm_write_clean(struct file *file)
28445+{
28446+ struct au_sbinfo *sbinfo;
28447+
28448+ sbinfo = file->private_data;
28449+ if (sbinfo)
28450+ au_plink_clean(sbinfo->si_sb, /*verbose*/0);
28451+}
28452+
28453+static int au_procfs_plm_write_si(struct file *file, unsigned long id)
28454+{
28455+ int err;
28456+ struct super_block *sb;
28457+ struct au_sbinfo *sbinfo;
8b6a4947 28458+ struct hlist_bl_node *pos;
e49829fe
JR
28459+
28460+ err = -EBUSY;
28461+ if (unlikely(file->private_data))
28462+ goto out;
28463+
28464+ sb = NULL;
53392da6 28465+ /* don't use au_sbilist_lock() here */
8b6a4947
AM
28466+ hlist_bl_lock(&au_sbilist);
28467+ hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
e49829fe 28468+ if (id == sysaufs_si_id(sbinfo)) {
83b672a5
AM
28469+ if (kobject_get_unless_zero(&sbinfo->si_kobj))
28470+ sb = sbinfo->si_sb;
e49829fe
JR
28471+ break;
28472+ }
8b6a4947 28473+ hlist_bl_unlock(&au_sbilist);
e49829fe
JR
28474+
28475+ err = -EINVAL;
28476+ if (unlikely(!sb))
28477+ goto out;
28478+
28479+ err = au_plink_maint_enter(sb);
28480+ if (!err)
28481+ /* keep kobject_get() */
28482+ file->private_data = sbinfo;
28483+ else
28484+ kobject_put(&sbinfo->si_kobj);
28485+out:
28486+ return err;
28487+}
28488+
28489+/*
28490+ * Accept a valid "si=xxxx" only.
28491+ * Once it is accepted successfully, accept "clean" too.
28492+ */
28493+static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
28494+ size_t count, loff_t *ppos)
28495+{
28496+ ssize_t err;
28497+ unsigned long id;
28498+ /* last newline is allowed */
28499+ char buf[3 + sizeof(unsigned long) * 2 + 1];
28500+
28501+ err = -EACCES;
28502+ if (unlikely(!capable(CAP_SYS_ADMIN)))
28503+ goto out;
28504+
28505+ err = -EINVAL;
28506+ if (unlikely(count > sizeof(buf)))
28507+ goto out;
28508+
28509+ err = copy_from_user(buf, ubuf, count);
28510+ if (unlikely(err)) {
28511+ err = -EFAULT;
28512+ goto out;
28513+ }
28514+ buf[count] = 0;
28515+
28516+ err = -EINVAL;
28517+ if (!strcmp("clean", buf)) {
28518+ au_procfs_plm_write_clean(file);
28519+ goto out_success;
28520+ } else if (unlikely(strncmp("si=", buf, 3)))
28521+ goto out;
28522+
9dbd164d 28523+ err = kstrtoul(buf + 3, 16, &id);
e49829fe
JR
28524+ if (unlikely(err))
28525+ goto out;
28526+
28527+ err = au_procfs_plm_write_si(file, id);
28528+ if (unlikely(err))
28529+ goto out;
28530+
28531+out_success:
28532+ err = count; /* success */
28533+out:
28534+ return err;
28535+}
28536+
394e211a
AM
28537+static const struct proc_ops au_procfs_plm_op = {
28538+ .proc_write = au_procfs_plm_write,
28539+ .proc_release = au_procfs_plm_release
e49829fe
JR
28540+};
28541+
28542+/* ---------------------------------------------------------------------- */
28543+
28544+static struct proc_dir_entry *au_procfs_dir;
28545+
28546+void au_procfs_fin(void)
28547+{
28548+ remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
28549+ remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28550+}
28551+
28552+int __init au_procfs_init(void)
28553+{
28554+ int err;
28555+ struct proc_dir_entry *entry;
28556+
28557+ err = -ENOMEM;
28558+ au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
28559+ if (unlikely(!au_procfs_dir))
28560+ goto out;
28561+
cd7a4cd9 28562+ entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | 0200,
394e211a 28563+ au_procfs_dir, &au_procfs_plm_op);
e49829fe
JR
28564+ if (unlikely(!entry))
28565+ goto out_dir;
28566+
28567+ err = 0;
28568+ goto out; /* success */
28569+
28570+
28571+out_dir:
28572+ remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28573+out:
28574+ return err;
28575+}
7f207e10 28576diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
eca34b5c 28577--- /usr/share/empty/fs/aufs/rdu.c 1970-01-01 01:00:00.000000000 +0100
46016270 28578+++ linux/fs/aufs/rdu.c 2021-02-24 13:33:42.747680497 +0100
ba1aed25 28579@@ -0,0 +1,384 @@
cd7a4cd9 28580+// SPDX-License-Identifier: GPL-2.0
1308ab2a 28581+/*
d58c55f2 28582+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1308ab2a 28583+ *
28584+ * This program, aufs is free software; you can redistribute it and/or modify
28585+ * it under the terms of the GNU General Public License as published by
28586+ * the Free Software Foundation; either version 2 of the License, or
28587+ * (at your option) any later version.
28588+ *
28589+ * This program is distributed in the hope that it will be useful,
28590+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28591+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28592+ * GNU General Public License for more details.
28593+ *
28594+ * You should have received a copy of the GNU General Public License
523b37e3 28595+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1308ab2a 28596+ */
28597+
28598+/*
28599+ * readdir in userspace.
28600+ */
28601+
b752ccd1 28602+#include <linux/compat.h>
4a4d8108 28603+#include <linux/fs_stack.h>
1308ab2a 28604+#include <linux/security.h>
1308ab2a 28605+#include "aufs.h"
28606+
28607+/* bits for struct aufs_rdu.flags */
28608+#define AuRdu_CALLED 1
28609+#define AuRdu_CONT (1 << 1)
28610+#define AuRdu_FULL (1 << 2)
28611+#define au_ftest_rdu(flags, name) ((flags) & AuRdu_##name)
7f207e10
AM
28612+#define au_fset_rdu(flags, name) \
28613+ do { (flags) |= AuRdu_##name; } while (0)
28614+#define au_fclr_rdu(flags, name) \
28615+ do { (flags) &= ~AuRdu_##name; } while (0)
1308ab2a 28616+
28617+struct au_rdu_arg {
392086de 28618+ struct dir_context ctx;
1308ab2a 28619+ struct aufs_rdu *rdu;
28620+ union au_rdu_ent_ul ent;
28621+ unsigned long end;
28622+
28623+ struct super_block *sb;
28624+ int err;
28625+};
28626+
392086de 28627+static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
1308ab2a 28628+ loff_t offset, u64 h_ino, unsigned int d_type)
28629+{
28630+ int err, len;
392086de 28631+ struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
1308ab2a 28632+ struct aufs_rdu *rdu = arg->rdu;
28633+ struct au_rdu_ent ent;
28634+
28635+ err = 0;
28636+ arg->err = 0;
28637+ au_fset_rdu(rdu->cookie.flags, CALLED);
28638+ len = au_rdu_len(nlen);
28639+ if (arg->ent.ul + len < arg->end) {
28640+ ent.ino = h_ino;
28641+ ent.bindex = rdu->cookie.bindex;
28642+ ent.type = d_type;
28643+ ent.nlen = nlen;
4a4d8108
AM
28644+ if (unlikely(nlen > AUFS_MAX_NAMELEN))
28645+ ent.type = DT_UNKNOWN;
1308ab2a 28646+
9dbd164d 28647+ /* unnecessary to support mmap_sem since this is a dir */
1308ab2a 28648+ err = -EFAULT;
28649+ if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
28650+ goto out;
28651+ if (copy_to_user(arg->ent.e->name, name, nlen))
28652+ goto out;
28653+ /* the terminating NULL */
28654+ if (__put_user(0, arg->ent.e->name + nlen))
28655+ goto out;
28656+ err = 0;
28657+ /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
28658+ arg->ent.ul += len;
28659+ rdu->rent++;
28660+ } else {
28661+ err = -EFAULT;
28662+ au_fset_rdu(rdu->cookie.flags, FULL);
28663+ rdu->full = 1;
28664+ rdu->tail = arg->ent;
28665+ }
28666+
4f0767ce 28667+out:
1308ab2a 28668+ /* AuTraceErr(err); */
28669+ return err;
28670+}
28671+
28672+static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
28673+{
28674+ int err;
28675+ loff_t offset;
28676+ struct au_rdu_cookie *cookie = &arg->rdu->cookie;
28677+
92d182d2 28678+ /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
1308ab2a 28679+ offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
28680+ err = offset;
28681+ if (unlikely(offset != cookie->h_pos))
28682+ goto out;
28683+
28684+ err = 0;
28685+ do {
28686+ arg->err = 0;
28687+ au_fclr_rdu(cookie->flags, CALLED);
28688+ /* smp_mb(); */
392086de 28689+ err = vfsub_iterate_dir(h_file, &arg->ctx);
1308ab2a 28690+ if (err >= 0)
28691+ err = arg->err;
28692+ } while (!err
28693+ && au_ftest_rdu(cookie->flags, CALLED)
28694+ && !au_ftest_rdu(cookie->flags, FULL));
28695+ cookie->h_pos = h_file->f_pos;
28696+
4f0767ce 28697+out:
1308ab2a 28698+ AuTraceErr(err);
28699+ return err;
28700+}
28701+
28702+static int au_rdu(struct file *file, struct aufs_rdu *rdu)
28703+{
28704+ int err;
5afbbe0d 28705+ aufs_bindex_t bbot;
392086de
AM
28706+ struct au_rdu_arg arg = {
28707+ .ctx = {
2000de60 28708+ .actor = au_rdu_fill
392086de
AM
28709+ }
28710+ };
1308ab2a 28711+ struct dentry *dentry;
28712+ struct inode *inode;
28713+ struct file *h_file;
28714+ struct au_rdu_cookie *cookie = &rdu->cookie;
28715+
ba1aed25
AM
28716+ /* VERIFY_WRITE */
28717+ err = !access_ok(rdu->ent.e, rdu->sz);
1308ab2a 28718+ if (unlikely(err)) {
28719+ err = -EFAULT;
28720+ AuTraceErr(err);
28721+ goto out;
28722+ }
28723+ rdu->rent = 0;
28724+ rdu->tail = rdu->ent;
28725+ rdu->full = 0;
28726+ arg.rdu = rdu;
28727+ arg.ent = rdu->ent;
28728+ arg.end = arg.ent.ul;
28729+ arg.end += rdu->sz;
28730+
28731+ err = -ENOTDIR;
5afbbe0d 28732+ if (unlikely(!file->f_op->iterate && !file->f_op->iterate_shared))
1308ab2a 28733+ goto out;
28734+
28735+ err = security_file_permission(file, MAY_READ);
28736+ AuTraceErr(err);
28737+ if (unlikely(err))
28738+ goto out;
28739+
2000de60 28740+ dentry = file->f_path.dentry;
5527c038 28741+ inode = d_inode(dentry);
5afbbe0d 28742+ inode_lock_shared(inode);
1308ab2a 28743+
28744+ arg.sb = inode->i_sb;
e49829fe
JR
28745+ err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
28746+ if (unlikely(err))
28747+ goto out_mtx;
027c5e7a
AM
28748+ err = au_alive_dir(dentry);
28749+ if (unlikely(err))
28750+ goto out_si;
e49829fe 28751+ /* todo: reval? */
1308ab2a 28752+ fi_read_lock(file);
28753+
28754+ err = -EAGAIN;
28755+ if (unlikely(au_ftest_rdu(cookie->flags, CONT)
28756+ && cookie->generation != au_figen(file)))
28757+ goto out_unlock;
28758+
28759+ err = 0;
28760+ if (!rdu->blk) {
28761+ rdu->blk = au_sbi(arg.sb)->si_rdblk;
28762+ if (!rdu->blk)
28763+ rdu->blk = au_dir_size(file, /*dentry*/NULL);
28764+ }
5afbbe0d
AM
28765+ bbot = au_fbtop(file);
28766+ if (cookie->bindex < bbot)
28767+ cookie->bindex = bbot;
28768+ bbot = au_fbbot_dir(file);
28769+ /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
28770+ for (; !err && cookie->bindex <= bbot;
1308ab2a 28771+ cookie->bindex++, cookie->h_pos = 0) {
4a4d8108 28772+ h_file = au_hf_dir(file, cookie->bindex);
1308ab2a 28773+ if (!h_file)
28774+ continue;
28775+
28776+ au_fclr_rdu(cookie->flags, FULL);
28777+ err = au_rdu_do(h_file, &arg);
28778+ AuTraceErr(err);
28779+ if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
28780+ break;
28781+ }
28782+ AuDbg("rent %llu\n", rdu->rent);
28783+
28784+ if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
28785+ rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
28786+ au_fset_rdu(cookie->flags, CONT);
28787+ cookie->generation = au_figen(file);
28788+ }
28789+
28790+ ii_read_lock_child(inode);
5afbbe0d 28791+ fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
1308ab2a 28792+ ii_read_unlock(inode);
28793+
4f0767ce 28794+out_unlock:
1308ab2a 28795+ fi_read_unlock(file);
027c5e7a 28796+out_si:
1308ab2a 28797+ si_read_unlock(arg.sb);
4f0767ce 28798+out_mtx:
5afbbe0d 28799+ inode_unlock_shared(inode);
4f0767ce 28800+out:
1308ab2a 28801+ AuTraceErr(err);
28802+ return err;
28803+}
28804+
28805+static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
28806+{
28807+ int err;
28808+ ino_t ino;
28809+ unsigned long long nent;
28810+ union au_rdu_ent_ul *u;
28811+ struct au_rdu_ent ent;
28812+ struct super_block *sb;
28813+
28814+ err = 0;
28815+ nent = rdu->nent;
28816+ u = &rdu->ent;
2000de60 28817+ sb = file->f_path.dentry->d_sb;
1308ab2a 28818+ si_read_lock(sb, AuLock_FLUSH);
28819+ while (nent-- > 0) {
9dbd164d 28820+ /* unnecessary to support mmap_sem since this is a dir */
1308ab2a 28821+ err = copy_from_user(&ent, u->e, sizeof(ent));
4a4d8108 28822+ if (!err)
ba1aed25
AM
28823+ /* VERIFY_WRITE */
28824+ err = !access_ok(&u->e->ino, sizeof(ino));
1308ab2a 28825+ if (unlikely(err)) {
28826+ err = -EFAULT;
28827+ AuTraceErr(err);
28828+ break;
28829+ }
28830+
28831+ /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
28832+ if (!ent.wh)
28833+ err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
28834+ else
28835+ err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
28836+ &ino);
28837+ if (unlikely(err)) {
28838+ AuTraceErr(err);
28839+ break;
28840+ }
28841+
28842+ err = __put_user(ino, &u->e->ino);
28843+ if (unlikely(err)) {
28844+ err = -EFAULT;
28845+ AuTraceErr(err);
28846+ break;
28847+ }
28848+ u->ul += au_rdu_len(ent.nlen);
28849+ }
28850+ si_read_unlock(sb);
28851+
28852+ return err;
28853+}
28854+
28855+/* ---------------------------------------------------------------------- */
28856+
28857+static int au_rdu_verify(struct aufs_rdu *rdu)
28858+{
b752ccd1 28859+ AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
1308ab2a 28860+ "%llu, b%d, 0x%x, g%u}\n",
b752ccd1 28861+ rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
1308ab2a 28862+ rdu->blk,
28863+ rdu->rent, rdu->shwh, rdu->full,
28864+ rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
28865+ rdu->cookie.generation);
dece6358 28866+
b752ccd1 28867+ if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
1308ab2a 28868+ return 0;
dece6358 28869+
b752ccd1
AM
28870+ AuDbg("%u:%u\n",
28871+ rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
1308ab2a 28872+ return -EINVAL;
28873+}
28874+
28875+long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
dece6358 28876+{
1308ab2a 28877+ long err, e;
28878+ struct aufs_rdu rdu;
28879+ void __user *p = (void __user *)arg;
dece6358 28880+
1308ab2a 28881+ err = copy_from_user(&rdu, p, sizeof(rdu));
28882+ if (unlikely(err)) {
28883+ err = -EFAULT;
28884+ AuTraceErr(err);
28885+ goto out;
28886+ }
28887+ err = au_rdu_verify(&rdu);
dece6358
AM
28888+ if (unlikely(err))
28889+ goto out;
28890+
1308ab2a 28891+ switch (cmd) {
28892+ case AUFS_CTL_RDU:
28893+ err = au_rdu(file, &rdu);
28894+ if (unlikely(err))
28895+ break;
dece6358 28896+
1308ab2a 28897+ e = copy_to_user(p, &rdu, sizeof(rdu));
28898+ if (unlikely(e)) {
28899+ err = -EFAULT;
28900+ AuTraceErr(err);
28901+ }
28902+ break;
28903+ case AUFS_CTL_RDU_INO:
28904+ err = au_rdu_ino(file, &rdu);
28905+ break;
28906+
28907+ default:
4a4d8108 28908+ /* err = -ENOTTY; */
1308ab2a 28909+ err = -EINVAL;
28910+ }
dece6358 28911+
4f0767ce 28912+out:
1308ab2a 28913+ AuTraceErr(err);
28914+ return err;
1facf9fc 28915+}
b752ccd1
AM
28916+
28917+#ifdef CONFIG_COMPAT
28918+long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
28919+{
28920+ long err, e;
28921+ struct aufs_rdu rdu;
28922+ void __user *p = compat_ptr(arg);
28923+
28924+ /* todo: get_user()? */
28925+ err = copy_from_user(&rdu, p, sizeof(rdu));
28926+ if (unlikely(err)) {
28927+ err = -EFAULT;
28928+ AuTraceErr(err);
28929+ goto out;
28930+ }
28931+ rdu.ent.e = compat_ptr(rdu.ent.ul);
28932+ err = au_rdu_verify(&rdu);
28933+ if (unlikely(err))
28934+ goto out;
28935+
28936+ switch (cmd) {
28937+ case AUFS_CTL_RDU:
28938+ err = au_rdu(file, &rdu);
28939+ if (unlikely(err))
28940+ break;
28941+
28942+ rdu.ent.ul = ptr_to_compat(rdu.ent.e);
28943+ rdu.tail.ul = ptr_to_compat(rdu.tail.e);
28944+ e = copy_to_user(p, &rdu, sizeof(rdu));
28945+ if (unlikely(e)) {
28946+ err = -EFAULT;
28947+ AuTraceErr(err);
28948+ }
28949+ break;
28950+ case AUFS_CTL_RDU_INO:
28951+ err = au_rdu_ino(file, &rdu);
28952+ break;
28953+
28954+ default:
28955+ /* err = -ENOTTY; */
28956+ err = -EINVAL;
28957+ }
28958+
4f0767ce 28959+out:
b752ccd1
AM
28960+ AuTraceErr(err);
28961+ return err;
28962+}
28963+#endif
7f207e10 28964diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
eca34b5c 28965--- /usr/share/empty/fs/aufs/rwsem.h 1970-01-01 01:00:00.000000000 +0100
46016270 28966+++ linux/fs/aufs/rwsem.h 2021-02-24 13:33:42.747680497 +0100
42a736d3 28967@@ -0,0 +1,85 @@
062440b3 28968+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 28969+/*
d58c55f2 28970+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 28971+ *
28972+ * This program, aufs is free software; you can redistribute it and/or modify
28973+ * it under the terms of the GNU General Public License as published by
28974+ * the Free Software Foundation; either version 2 of the License, or
28975+ * (at your option) any later version.
dece6358
AM
28976+ *
28977+ * This program is distributed in the hope that it will be useful,
28978+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28979+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28980+ * GNU General Public License for more details.
28981+ *
28982+ * You should have received a copy of the GNU General Public License
523b37e3 28983+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 28984+ */
28985+
28986+/*
28987+ * simple read-write semaphore wrappers
28988+ */
28989+
28990+#ifndef __AUFS_RWSEM_H__
28991+#define __AUFS_RWSEM_H__
28992+
28993+#ifdef __KERNEL__
28994+
4a4d8108 28995+#include "debug.h"
dece6358 28996+
acd2b654 28997+/* in the future, the name 'au_rwsem' will be totally gone */
8b6a4947 28998+#define au_rwsem rw_semaphore
dece6358
AM
28999+
29000+/* to debug easier, do not make them inlined functions */
8b6a4947 29001+#define AuRwMustNoWaiters(rw) AuDebugOn(rwsem_is_contended(rw))
42a736d3
AM
29002+
29003+#ifdef CONFIG_LOCKDEP
dece6358 29004+/* rwsem_is_locked() is unusable */
42a736d3
AM
29005+#define AuRwMustReadLock(rw) AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP) \
29006+ && !lockdep_recursing(current) \
29007+ && debug_locks \
8b6a4947 29008+ && !lockdep_is_held_type(rw, 1))
42a736d3
AM
29009+#define AuRwMustWriteLock(rw) AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP) \
29010+ && !lockdep_recursing(current) \
29011+ && debug_locks \
8b6a4947 29012+ && !lockdep_is_held_type(rw, 0))
42a736d3
AM
29013+#define AuRwMustAnyLock(rw) AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP) \
29014+ && !lockdep_recursing(current) \
29015+ && debug_locks \
8b6a4947 29016+ && !lockdep_is_held(rw))
42a736d3
AM
29017+#define AuRwDestroy(rw) AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP) \
29018+ && !lockdep_recursing(current) \
29019+ && debug_locks \
8b6a4947 29020+ && lockdep_is_held(rw))
42a736d3
AM
29021+#else
29022+#define AuRwMustReadLock(rw) do {} while (0)
29023+#define AuRwMustWriteLock(rw) do {} while (0)
29024+#define AuRwMustAnyLock(rw) do {} while (0)
29025+#define AuRwDestroy(rw) do {} while (0)
29026+#endif
8b6a4947
AM
29027+
29028+#define au_rw_init(rw) init_rwsem(rw)
dece6358 29029+
5afbbe0d
AM
29030+#define au_rw_init_wlock(rw) do { \
29031+ au_rw_init(rw); \
8b6a4947 29032+ down_write(rw); \
5afbbe0d 29033+ } while (0)
dece6358 29034+
8b6a4947
AM
29035+#define au_rw_init_wlock_nested(rw, lsc) do { \
29036+ au_rw_init(rw); \
29037+ down_write_nested(rw, lsc); \
5afbbe0d 29038+ } while (0)
dece6358 29039+
8b6a4947
AM
29040+#define au_rw_read_lock(rw) down_read(rw)
29041+#define au_rw_read_lock_nested(rw, lsc) down_read_nested(rw, lsc)
29042+#define au_rw_read_unlock(rw) up_read(rw)
29043+#define au_rw_dgrade_lock(rw) downgrade_write(rw)
29044+#define au_rw_write_lock(rw) down_write(rw)
29045+#define au_rw_write_lock_nested(rw, lsc) down_write_nested(rw, lsc)
29046+#define au_rw_write_unlock(rw) up_write(rw)
29047+/* why is not _nested version defined? */
29048+#define au_rw_read_trylock(rw) down_read_trylock(rw)
29049+#define au_rw_write_trylock(rw) down_write_trylock(rw)
1facf9fc 29050+
29051+#endif /* __KERNEL__ */
29052+#endif /* __AUFS_RWSEM_H__ */
7f207e10 29053diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
eca34b5c 29054--- /usr/share/empty/fs/aufs/sbinfo.c 1970-01-01 01:00:00.000000000 +0100
46016270 29055+++ linux/fs/aufs/sbinfo.c 2021-02-24 13:33:42.747680497 +0100
eca801bf 29056@@ -0,0 +1,314 @@
cd7a4cd9 29057+// SPDX-License-Identifier: GPL-2.0
1facf9fc 29058+/*
d58c55f2 29059+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 29060+ *
29061+ * This program, aufs is free software; you can redistribute it and/or modify
29062+ * it under the terms of the GNU General Public License as published by
29063+ * the Free Software Foundation; either version 2 of the License, or
29064+ * (at your option) any later version.
dece6358
AM
29065+ *
29066+ * This program is distributed in the hope that it will be useful,
29067+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
29068+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29069+ * GNU General Public License for more details.
29070+ *
29071+ * You should have received a copy of the GNU General Public License
523b37e3 29072+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 29073+ */
29074+
29075+/*
29076+ * superblock private data
29077+ */
29078+
eca801bf 29079+#include <linux/iversion.h>
1facf9fc 29080+#include "aufs.h"
29081+
29082+/*
29083+ * they are necessary regardless sysfs is disabled.
29084+ */
29085+void au_si_free(struct kobject *kobj)
29086+{
86dc4139 29087+ int i;
1facf9fc 29088+ struct au_sbinfo *sbinfo;
b752ccd1 29089+ char *locked __maybe_unused; /* debug only */
1facf9fc 29090+
29091+ sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
86dc4139 29092+ for (i = 0; i < AuPlink_NHASH; i++)
8b6a4947 29093+ AuDebugOn(!hlist_bl_empty(sbinfo->si_plink + i));
f0c0a007 29094+ AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
5afbbe0d 29095+
acd2b654
AM
29096+ AuLCntZero(au_lcnt_read(&sbinfo->si_ninodes, /*do_rev*/0));
29097+ au_lcnt_fin(&sbinfo->si_ninodes, /*do_sync*/0);
29098+ AuLCntZero(au_lcnt_read(&sbinfo->si_nfiles, /*do_rev*/0));
29099+ au_lcnt_fin(&sbinfo->si_nfiles, /*do_sync*/0);
1facf9fc 29100+
062440b3 29101+ dbgaufs_si_fin(sbinfo);
e49829fe 29102+ au_rw_write_lock(&sbinfo->si_rwsem);
1facf9fc 29103+ au_br_free(sbinfo);
e49829fe 29104+ au_rw_write_unlock(&sbinfo->si_rwsem);
b752ccd1 29105+
9f237c51 29106+ au_kfree_try_rcu(sbinfo->si_branch);
1facf9fc 29107+ mutex_destroy(&sbinfo->si_xib_mtx);
dece6358 29108+ AuRwDestroy(&sbinfo->si_rwsem);
1facf9fc 29109+
acd2b654
AM
29110+ au_lcnt_wait_for_fin(&sbinfo->si_ninodes);
29111+ /* si_nfiles is waited too */
9f237c51 29112+ au_kfree_rcu(sbinfo);
1facf9fc 29113+}
29114+
29115+int au_si_alloc(struct super_block *sb)
29116+{
86dc4139 29117+ int err, i;
1facf9fc 29118+ struct au_sbinfo *sbinfo;
29119+
29120+ err = -ENOMEM;
4a4d8108 29121+ sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
1facf9fc 29122+ if (unlikely(!sbinfo))
29123+ goto out;
29124+
29125+ /* will be reallocated separately */
29126+ sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
29127+ if (unlikely(!sbinfo->si_branch))
febd17d6 29128+ goto out_sbinfo;
1facf9fc 29129+
1facf9fc 29130+ err = sysaufs_si_init(sbinfo);
062440b3
AM
29131+ if (!err) {
29132+ dbgaufs_si_null(sbinfo);
29133+ err = dbgaufs_si_init(sbinfo);
29134+ if (unlikely(err))
29135+ kobject_put(&sbinfo->si_kobj);
29136+ }
1facf9fc 29137+ if (unlikely(err))
29138+ goto out_br;
29139+
29140+ au_nwt_init(&sbinfo->si_nowait);
dece6358 29141+ au_rw_init_wlock(&sbinfo->si_rwsem);
b752ccd1 29142+
acd2b654
AM
29143+ au_lcnt_init(&sbinfo->si_ninodes, /*release*/NULL);
29144+ au_lcnt_init(&sbinfo->si_nfiles, /*release*/NULL);
7f207e10 29145+
5afbbe0d 29146+ sbinfo->si_bbot = -1;
392086de 29147+ sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
1facf9fc 29148+
29149+ sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
29150+ sbinfo->si_wbr_create = AuWbrCreate_Def;
4a4d8108
AM
29151+ sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
29152+ sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
1facf9fc 29153+
076b876e
AM
29154+ au_fhsm_init(sbinfo);
29155+
e49829fe 29156+ sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
1facf9fc 29157+
392086de
AM
29158+ sbinfo->si_xino_jiffy = jiffies;
29159+ sbinfo->si_xino_expire
29160+ = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
1facf9fc 29161+ mutex_init(&sbinfo->si_xib_mtx);
1facf9fc 29162+ /* leave si_xib_last_pindex and si_xib_next_bit */
29163+
8b6a4947 29164+ INIT_HLIST_BL_HEAD(&sbinfo->si_aopen);
b912730e 29165+
e49829fe 29166+ sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
1facf9fc 29167+ sbinfo->si_rdblk = AUFS_RDBLK_DEF;
29168+ sbinfo->si_rdhash = AUFS_RDHASH_DEF;
29169+ sbinfo->si_dirwh = AUFS_DIRWH_DEF;
29170+
86dc4139 29171+ for (i = 0; i < AuPlink_NHASH; i++)
8b6a4947 29172+ INIT_HLIST_BL_HEAD(sbinfo->si_plink + i);
1facf9fc 29173+ init_waitqueue_head(&sbinfo->si_plink_wq);
4a4d8108 29174+ spin_lock_init(&sbinfo->si_plink_maint_lock);
1facf9fc 29175+
8b6a4947 29176+ INIT_HLIST_BL_HEAD(&sbinfo->si_files);
523b37e3 29177+
b95c5147
AM
29178+ /* with getattr by default */
29179+ sbinfo->si_iop_array = aufs_iop;
29180+
1facf9fc 29181+ /* leave other members for sysaufs and si_mnt. */
29182+ sbinfo->si_sb = sb;
29183+ sb->s_fs_info = sbinfo;
b752ccd1 29184+ si_pid_set(sb);
1facf9fc 29185+ return 0; /* success */
29186+
4f0767ce 29187+out_br:
9f237c51 29188+ au_kfree_try_rcu(sbinfo->si_branch);
4f0767ce 29189+out_sbinfo:
9f237c51 29190+ au_kfree_rcu(sbinfo);
4f0767ce 29191+out:
1facf9fc 29192+ return err;
29193+}
29194+
e2f27e51 29195+int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
1facf9fc 29196+{
29197+ int err, sz;
29198+ struct au_branch **brp;
29199+
dece6358
AM
29200+ AuRwMustWriteLock(&sbinfo->si_rwsem);
29201+
1facf9fc 29202+ err = -ENOMEM;
5afbbe0d 29203+ sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
1facf9fc 29204+ if (unlikely(!sz))
29205+ sz = sizeof(*brp);
e2f27e51
AM
29206+ brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
29207+ may_shrink);
1facf9fc 29208+ if (brp) {
29209+ sbinfo->si_branch = brp;
29210+ err = 0;
29211+ }
29212+
29213+ return err;
29214+}
29215+
29216+/* ---------------------------------------------------------------------- */
29217+
29218+unsigned int au_sigen_inc(struct super_block *sb)
29219+{
29220+ unsigned int gen;
5527c038 29221+ struct inode *inode;
1facf9fc 29222+
dece6358
AM
29223+ SiMustWriteLock(sb);
29224+
1facf9fc 29225+ gen = ++au_sbi(sb)->si_generation;
29226+ au_update_digen(sb->s_root);
5527c038
JR
29227+ inode = d_inode(sb->s_root);
29228+ au_update_iigen(inode, /*half*/0);
be118d29 29229+ inode_inc_iversion(inode);
1facf9fc 29230+ return gen;
29231+}
29232+
29233+aufs_bindex_t au_new_br_id(struct super_block *sb)
29234+{
29235+ aufs_bindex_t br_id;
29236+ int i;
29237+ struct au_sbinfo *sbinfo;
29238+
dece6358
AM
29239+ SiMustWriteLock(sb);
29240+
1facf9fc 29241+ sbinfo = au_sbi(sb);
29242+ for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
29243+ br_id = ++sbinfo->si_last_br_id;
7f207e10 29244+ AuDebugOn(br_id < 0);
1facf9fc 29245+ if (br_id && au_br_index(sb, br_id) < 0)
29246+ return br_id;
29247+ }
29248+
29249+ return -1;
29250+}
29251+
29252+/* ---------------------------------------------------------------------- */
29253+
e49829fe
JR
29254+/* it is ok that new 'nwt' tasks are appended while we are sleeping */
29255+int si_read_lock(struct super_block *sb, int flags)
29256+{
29257+ int err;
29258+
29259+ err = 0;
29260+ if (au_ftest_lock(flags, FLUSH))
29261+ au_nwt_flush(&au_sbi(sb)->si_nowait);
29262+
29263+ si_noflush_read_lock(sb);
29264+ err = au_plink_maint(sb, flags);
29265+ if (unlikely(err))
29266+ si_read_unlock(sb);
29267+
29268+ return err;
29269+}
29270+
29271+int si_write_lock(struct super_block *sb, int flags)
29272+{
29273+ int err;
29274+
29275+ if (au_ftest_lock(flags, FLUSH))
29276+ au_nwt_flush(&au_sbi(sb)->si_nowait);
29277+
29278+ si_noflush_write_lock(sb);
29279+ err = au_plink_maint(sb, flags);
29280+ if (unlikely(err))
29281+ si_write_unlock(sb);
29282+
29283+ return err;
29284+}
29285+
1facf9fc 29286+/* dentry and super_block lock. call at entry point */
e49829fe 29287+int aufs_read_lock(struct dentry *dentry, int flags)
1facf9fc 29288+{
e49829fe 29289+ int err;
027c5e7a 29290+ struct super_block *sb;
e49829fe 29291+
027c5e7a
AM
29292+ sb = dentry->d_sb;
29293+ err = si_read_lock(sb, flags);
29294+ if (unlikely(err))
29295+ goto out;
29296+
29297+ if (au_ftest_lock(flags, DW))
29298+ di_write_lock_child(dentry);
29299+ else
29300+ di_read_lock_child(dentry, flags);
29301+
29302+ if (au_ftest_lock(flags, GEN)) {
29303+ err = au_digen_test(dentry, au_sigen(sb));
79b8bda9
AM
29304+ if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
29305+ AuDebugOn(!err && au_dbrange_test(dentry));
29306+ else if (!err)
29307+ err = au_dbrange_test(dentry);
027c5e7a
AM
29308+ if (unlikely(err))
29309+ aufs_read_unlock(dentry, flags);
e49829fe
JR
29310+ }
29311+
027c5e7a 29312+out:
e49829fe 29313+ return err;
1facf9fc 29314+}
29315+
29316+void aufs_read_unlock(struct dentry *dentry, int flags)
29317+{
29318+ if (au_ftest_lock(flags, DW))
29319+ di_write_unlock(dentry);
29320+ else
29321+ di_read_unlock(dentry, flags);
29322+ si_read_unlock(dentry->d_sb);
29323+}
29324+
29325+void aufs_write_lock(struct dentry *dentry)
29326+{
e49829fe 29327+ si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
1facf9fc 29328+ di_write_lock_child(dentry);
29329+}
29330+
29331+void aufs_write_unlock(struct dentry *dentry)
29332+{
29333+ di_write_unlock(dentry);
29334+ si_write_unlock(dentry->d_sb);
29335+}
29336+
e49829fe 29337+int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
1facf9fc 29338+{
e49829fe 29339+ int err;
027c5e7a
AM
29340+ unsigned int sigen;
29341+ struct super_block *sb;
e49829fe 29342+
027c5e7a
AM
29343+ sb = d1->d_sb;
29344+ err = si_read_lock(sb, flags);
29345+ if (unlikely(err))
29346+ goto out;
29347+
b95c5147 29348+ di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
027c5e7a
AM
29349+
29350+ if (au_ftest_lock(flags, GEN)) {
29351+ sigen = au_sigen(sb);
29352+ err = au_digen_test(d1, sigen);
29353+ AuDebugOn(!err && au_dbrange_test(d1));
29354+ if (!err) {
29355+ err = au_digen_test(d2, sigen);
29356+ AuDebugOn(!err && au_dbrange_test(d2));
29357+ }
29358+ if (unlikely(err))
29359+ aufs_read_and_write_unlock2(d1, d2);
29360+ }
29361+
29362+out:
e49829fe 29363+ return err;
1facf9fc 29364+}
29365+
29366+void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
29367+{
29368+ di_write_unlock2(d1, d2);
29369+ si_read_unlock(d1->d_sb);
29370+}
7f207e10 29371diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
eca34b5c 29372--- /usr/share/empty/fs/aufs/super.c 1970-01-01 01:00:00.000000000 +0100
46016270 29373+++ linux/fs/aufs/super.c 2021-06-30 21:35:11.397206648 +0200
0b2a12c6 29374@@ -0,0 +1,1050 @@
cd7a4cd9 29375+// SPDX-License-Identifier: GPL-2.0
1facf9fc 29376+/*
d58c55f2 29377+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 29378+ *
29379+ * This program, aufs is free software; you can redistribute it and/or modify
29380+ * it under the terms of the GNU General Public License as published by
29381+ * the Free Software Foundation; either version 2 of the License, or
29382+ * (at your option) any later version.
dece6358
AM
29383+ *
29384+ * This program is distributed in the hope that it will be useful,
29385+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
29386+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29387+ * GNU General Public License for more details.
29388+ *
29389+ * You should have received a copy of the GNU General Public License
523b37e3 29390+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 29391+ */
29392+
29393+/*
29394+ * mount and super_block operations
29395+ */
29396+
eca801bf 29397+#include <linux/iversion.h>
f6c5ef8b 29398+#include <linux/mm.h>
1facf9fc 29399+#include <linux/seq_file.h>
29400+#include <linux/statfs.h>
7f207e10 29401+#include <linux/vmalloc.h>
1facf9fc 29402+#include "aufs.h"
29403+
29404+/*
29405+ * super_operations
29406+ */
29407+static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
29408+{
29409+ struct au_icntnr *c;
29410+
29411+ c = au_cache_alloc_icntnr();
29412+ if (c) {
027c5e7a 29413+ au_icntnr_init(c);
be118d29 29414+ inode_set_iversion(&c->vfs_inode, 1); /* sigen(sb); */
1facf9fc 29415+ c->iinfo.ii_hinode = NULL;
29416+ return &c->vfs_inode;
29417+ }
29418+ return NULL;
29419+}
29420+
29421+static void aufs_destroy_inode(struct inode *inode)
29422+{
5afbbe0d
AM
29423+ if (!au_is_bad_inode(inode))
29424+ au_iinfo_fin(inode);
fbc438ed
JR
29425+}
29426+
29427+static void aufs_free_inode(struct inode *inode)
29428+{
29429+ au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
1facf9fc 29430+}
29431+
29432+struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
29433+{
29434+ struct inode *inode;
29435+ int err;
29436+
29437+ inode = iget_locked(sb, ino);
29438+ if (unlikely(!inode)) {
29439+ inode = ERR_PTR(-ENOMEM);
29440+ goto out;
29441+ }
29442+ if (!(inode->i_state & I_NEW))
29443+ goto out;
29444+
29445+ err = au_xigen_new(inode);
29446+ if (!err)
29447+ err = au_iinfo_init(inode);
29448+ if (!err)
be118d29 29449+ inode_inc_iversion(inode);
1facf9fc 29450+ else {
29451+ iget_failed(inode);
29452+ inode = ERR_PTR(err);
29453+ }
29454+
4f0767ce 29455+out:
1facf9fc 29456+ /* never return NULL */
29457+ AuDebugOn(!inode);
29458+ AuTraceErrPtr(inode);
29459+ return inode;
29460+}
29461+
29462+/* lock free root dinfo */
29463+static int au_show_brs(struct seq_file *seq, struct super_block *sb)
29464+{
29465+ int err;
5afbbe0d 29466+ aufs_bindex_t bindex, bbot;
1facf9fc 29467+ struct path path;
4a4d8108 29468+ struct au_hdentry *hdp;
1facf9fc 29469+ struct au_branch *br;
076b876e 29470+ au_br_perm_str_t perm;
1facf9fc 29471+
29472+ err = 0;
5afbbe0d
AM
29473+ bbot = au_sbbot(sb);
29474+ bindex = 0;
29475+ hdp = au_hdentry(au_di(sb->s_root), bindex);
29476+ for (; !err && bindex <= bbot; bindex++, hdp++) {
1facf9fc 29477+ br = au_sbr(sb, bindex);
86dc4139 29478+ path.mnt = au_br_mnt(br);
5afbbe0d 29479+ path.dentry = hdp->hd_dentry;
1facf9fc 29480+ err = au_seq_path(seq, &path);
79b8bda9 29481+ if (!err) {
076b876e 29482+ au_optstr_br_perm(&perm, br->br_perm);
79b8bda9 29483+ seq_printf(seq, "=%s", perm.a);
5afbbe0d 29484+ if (bindex != bbot)
79b8bda9 29485+ seq_putc(seq, ':');
1e00d052 29486+ }
1facf9fc 29487+ }
79b8bda9
AM
29488+ if (unlikely(err || seq_has_overflowed(seq)))
29489+ err = -E2BIG;
1facf9fc 29490+
29491+ return err;
29492+}
29493+
f2c43d5f
AM
29494+static void au_gen_fmt(char *fmt, int len __maybe_unused, const char *pat,
29495+ const char *append)
29496+{
29497+ char *p;
29498+
29499+ p = fmt;
29500+ while (*pat != ':')
29501+ *p++ = *pat++;
29502+ *p++ = *pat++;
29503+ strcpy(p, append);
29504+ AuDebugOn(strlen(fmt) >= len);
29505+}
29506+
1facf9fc 29507+static void au_show_wbr_create(struct seq_file *m, int v,
29508+ struct au_sbinfo *sbinfo)
29509+{
29510+ const char *pat;
f2c43d5f
AM
29511+ char fmt[32];
29512+ struct au_wbr_mfs *mfs;
1facf9fc 29513+
dece6358
AM
29514+ AuRwMustAnyLock(&sbinfo->si_rwsem);
29515+
c2b27bf2 29516+ seq_puts(m, ",create=");
1facf9fc 29517+ pat = au_optstr_wbr_create(v);
f2c43d5f 29518+ mfs = &sbinfo->si_wbr_mfs;
1facf9fc 29519+ switch (v) {
29520+ case AuWbrCreate_TDP:
29521+ case AuWbrCreate_RR:
29522+ case AuWbrCreate_MFS:
29523+ case AuWbrCreate_PMFS:
c2b27bf2 29524+ seq_puts(m, pat);
1facf9fc 29525+ break;
f2c43d5f
AM
29526+ case AuWbrCreate_MFSRR:
29527+ case AuWbrCreate_TDMFS:
29528+ case AuWbrCreate_PMFSRR:
29529+ au_gen_fmt(fmt, sizeof(fmt), pat, "%llu");
29530+ seq_printf(m, fmt, mfs->mfsrr_watermark);
1facf9fc 29531+ break;
f2c43d5f 29532+ case AuWbrCreate_MFSV:
1facf9fc 29533+ case AuWbrCreate_PMFSV:
f2c43d5f
AM
29534+ au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
29535+ seq_printf(m, fmt,
29536+ jiffies_to_msecs(mfs->mfs_expire)
e49829fe 29537+ / MSEC_PER_SEC);
1facf9fc 29538+ break;
1facf9fc 29539+ case AuWbrCreate_MFSRRV:
f2c43d5f 29540+ case AuWbrCreate_TDMFSV:
392086de 29541+ case AuWbrCreate_PMFSRRV:
f2c43d5f
AM
29542+ au_gen_fmt(fmt, sizeof(fmt), pat, "%llu:%lu");
29543+ seq_printf(m, fmt, mfs->mfsrr_watermark,
29544+ jiffies_to_msecs(mfs->mfs_expire) / MSEC_PER_SEC);
392086de 29545+ break;
f2c43d5f
AM
29546+ default:
29547+ BUG();
1facf9fc 29548+ }
29549+}
29550+
7eafdf33 29551+static int au_show_xino(struct seq_file *seq, struct super_block *sb)
1facf9fc 29552+{
29553+#ifdef CONFIG_SYSFS
29554+ return 0;
29555+#else
29556+ int err;
29557+ const int len = sizeof(AUFS_XINO_FNAME) - 1;
29558+ aufs_bindex_t bindex, brid;
1facf9fc 29559+ struct qstr *name;
29560+ struct file *f;
29561+ struct dentry *d, *h_root;
acd2b654 29562+ struct au_branch *br;
1facf9fc 29563+
dece6358
AM
29564+ AuRwMustAnyLock(&sbinfo->si_rwsem);
29565+
1facf9fc 29566+ err = 0;
1facf9fc 29567+ f = au_sbi(sb)->si_xib;
29568+ if (!f)
29569+ goto out;
29570+
29571+ /* stop printing the default xino path on the first writable branch */
29572+ h_root = NULL;
acd2b654
AM
29573+ bindex = au_xi_root(sb, f->f_path.dentry);
29574+ if (bindex >= 0) {
29575+ br = au_sbr_sb(sb, bindex);
29576+ h_root = au_br_dentry(br);
1facf9fc 29577+ }
acd2b654 29578+
2000de60 29579+ d = f->f_path.dentry;
1facf9fc 29580+ name = &d->d_name;
29581+ /* safe ->d_parent because the file is unlinked */
29582+ if (d->d_parent == h_root
29583+ && name->len == len
29584+ && !memcmp(name->name, AUFS_XINO_FNAME, len))
29585+ goto out;
29586+
29587+ seq_puts(seq, ",xino=");
29588+ err = au_xino_path(seq, f);
29589+
4f0767ce 29590+out:
1facf9fc 29591+ return err;
29592+#endif
29593+}
29594+
29595+/* seq_file will re-call me in case of too long string */
7eafdf33 29596+static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
1facf9fc 29597+{
027c5e7a 29598+ int err;
1facf9fc 29599+ unsigned int mnt_flags, v;
29600+ struct super_block *sb;
29601+ struct au_sbinfo *sbinfo;
29602+
29603+#define AuBool(name, str) do { \
29604+ v = au_opt_test(mnt_flags, name); \
29605+ if (v != au_opt_test(AuOpt_Def, name)) \
29606+ seq_printf(m, ",%s" #str, v ? "" : "no"); \
29607+} while (0)
29608+
29609+#define AuStr(name, str) do { \
29610+ v = mnt_flags & AuOptMask_##name; \
29611+ if (v != (AuOpt_Def & AuOptMask_##name)) \
29612+ seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
29613+} while (0)
29614+
29615+#define AuUInt(name, str, val) do { \
29616+ if (val != AUFS_##name##_DEF) \
29617+ seq_printf(m, "," #str "=%u", val); \
29618+} while (0)
29619+
7eafdf33 29620+ sb = dentry->d_sb;
2121bcd9 29621+ if (sb->s_flags & SB_POSIXACL)
c1595e42 29622+ seq_puts(m, ",acl");
43982f53 29623+#if 0 /* reserved for future use */
be118d29
JR
29624+ if (sb->s_flags & SB_I_VERSION)
29625+ seq_puts(m, ",i_version");
29626+#endif
c1595e42
JR
29627+
29628+ /* lock free root dinfo */
1facf9fc 29629+ si_noflush_read_lock(sb);
29630+ sbinfo = au_sbi(sb);
29631+ seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
29632+
29633+ mnt_flags = au_mntflags(sb);
29634+ if (au_opt_test(mnt_flags, XINO)) {
7eafdf33 29635+ err = au_show_xino(m, sb);
1facf9fc 29636+ if (unlikely(err))
29637+ goto out;
29638+ } else
29639+ seq_puts(m, ",noxino");
29640+
29641+ AuBool(TRUNC_XINO, trunc_xino);
29642+ AuStr(UDBA, udba);
dece6358 29643+ AuBool(SHWH, shwh);
1facf9fc 29644+ AuBool(PLINK, plink);
4a4d8108 29645+ AuBool(DIO, dio);
076b876e 29646+ AuBool(DIRPERM1, dirperm1);
1facf9fc 29647+
29648+ v = sbinfo->si_wbr_create;
29649+ if (v != AuWbrCreate_Def)
29650+ au_show_wbr_create(m, v, sbinfo);
29651+
29652+ v = sbinfo->si_wbr_copyup;
29653+ if (v != AuWbrCopyup_Def)
29654+ seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
29655+
29656+ v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
29657+ if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
29658+ seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
29659+
29660+ AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
29661+
027c5e7a
AM
29662+ v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
29663+ AuUInt(RDCACHE, rdcache, v);
1facf9fc 29664+
29665+ AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
29666+ AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
29667+
076b876e
AM
29668+ au_fhsm_show(m, sbinfo);
29669+
8b6a4947 29670+ AuBool(DIRREN, dirren);
1facf9fc 29671+ AuBool(SUM, sum);
29672+ /* AuBool(SUM_W, wsum); */
29673+ AuBool(WARN_PERM, warn_perm);
29674+ AuBool(VERBOSE, verbose);
29675+
4f0767ce 29676+out:
1facf9fc 29677+ /* be sure to print "br:" last */
29678+ if (!sysaufs_brs) {
29679+ seq_puts(m, ",br:");
29680+ au_show_brs(m, sb);
29681+ }
29682+ si_read_unlock(sb);
29683+ return 0;
29684+
1facf9fc 29685+#undef AuBool
29686+#undef AuStr
4a4d8108 29687+#undef AuUInt
1facf9fc 29688+}
29689+
29690+/* ---------------------------------------------------------------------- */
29691+
29692+/* sum mode which returns the summation for statfs(2) */
29693+
29694+static u64 au_add_till_max(u64 a, u64 b)
29695+{
29696+ u64 old;
29697+
29698+ old = a;
29699+ a += b;
92d182d2
AM
29700+ if (old <= a)
29701+ return a;
29702+ return ULLONG_MAX;
29703+}
29704+
29705+static u64 au_mul_till_max(u64 a, long mul)
29706+{
29707+ u64 old;
29708+
29709+ old = a;
29710+ a *= mul;
29711+ if (old <= a)
1facf9fc 29712+ return a;
29713+ return ULLONG_MAX;
29714+}
29715+
29716+static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
29717+{
29718+ int err;
92d182d2 29719+ long bsize, factor;
1facf9fc 29720+ u64 blocks, bfree, bavail, files, ffree;
5afbbe0d 29721+ aufs_bindex_t bbot, bindex, i;
1facf9fc 29722+ unsigned char shared;
7f207e10 29723+ struct path h_path;
1facf9fc 29724+ struct super_block *h_sb;
29725+
92d182d2
AM
29726+ err = 0;
29727+ bsize = LONG_MAX;
29728+ files = 0;
29729+ ffree = 0;
1facf9fc 29730+ blocks = 0;
29731+ bfree = 0;
29732+ bavail = 0;
5afbbe0d
AM
29733+ bbot = au_sbbot(sb);
29734+ for (bindex = 0; bindex <= bbot; bindex++) {
7f207e10
AM
29735+ h_path.mnt = au_sbr_mnt(sb, bindex);
29736+ h_sb = h_path.mnt->mnt_sb;
1facf9fc 29737+ shared = 0;
92d182d2 29738+ for (i = 0; !shared && i < bindex; i++)
1facf9fc 29739+ shared = (au_sbr_sb(sb, i) == h_sb);
29740+ if (shared)
29741+ continue;
29742+
29743+ /* sb->s_root for NFS is unreliable */
7f207e10
AM
29744+ h_path.dentry = h_path.mnt->mnt_root;
29745+ err = vfs_statfs(&h_path, buf);
1facf9fc 29746+ if (unlikely(err))
29747+ goto out;
29748+
92d182d2
AM
29749+ if (bsize > buf->f_bsize) {
29750+ /*
29751+ * we will reduce bsize, so we have to expand blocks
29752+ * etc. to match them again
29753+ */
29754+ factor = (bsize / buf->f_bsize);
29755+ blocks = au_mul_till_max(blocks, factor);
29756+ bfree = au_mul_till_max(bfree, factor);
29757+ bavail = au_mul_till_max(bavail, factor);
29758+ bsize = buf->f_bsize;
29759+ }
29760+
29761+ factor = (buf->f_bsize / bsize);
29762+ blocks = au_add_till_max(blocks,
29763+ au_mul_till_max(buf->f_blocks, factor));
29764+ bfree = au_add_till_max(bfree,
29765+ au_mul_till_max(buf->f_bfree, factor));
29766+ bavail = au_add_till_max(bavail,
29767+ au_mul_till_max(buf->f_bavail, factor));
1facf9fc 29768+ files = au_add_till_max(files, buf->f_files);
29769+ ffree = au_add_till_max(ffree, buf->f_ffree);
29770+ }
29771+
92d182d2 29772+ buf->f_bsize = bsize;
1facf9fc 29773+ buf->f_blocks = blocks;
29774+ buf->f_bfree = bfree;
29775+ buf->f_bavail = bavail;
29776+ buf->f_files = files;
29777+ buf->f_ffree = ffree;
92d182d2 29778+ buf->f_frsize = 0;
1facf9fc 29779+
4f0767ce 29780+out:
1facf9fc 29781+ return err;
29782+}
29783+
29784+static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
29785+{
29786+ int err;
7f207e10 29787+ struct path h_path;
1facf9fc 29788+ struct super_block *sb;
29789+
29790+ /* lock free root dinfo */
29791+ sb = dentry->d_sb;
29792+ si_noflush_read_lock(sb);
7f207e10 29793+ if (!au_opt_test(au_mntflags(sb), SUM)) {
1facf9fc 29794+ /* sb->s_root for NFS is unreliable */
7f207e10
AM
29795+ h_path.mnt = au_sbr_mnt(sb, 0);
29796+ h_path.dentry = h_path.mnt->mnt_root;
29797+ err = vfs_statfs(&h_path, buf);
29798+ } else
1facf9fc 29799+ err = au_statfs_sum(sb, buf);
29800+ si_read_unlock(sb);
29801+
29802+ if (!err) {
29803+ buf->f_type = AUFS_SUPER_MAGIC;
4a4d8108 29804+ buf->f_namelen = AUFS_MAX_NAMELEN;
1facf9fc 29805+ memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
29806+ }
29807+ /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
29808+
29809+ return err;
29810+}
29811+
29812+/* ---------------------------------------------------------------------- */
29813+
537831f9
AM
29814+static int aufs_sync_fs(struct super_block *sb, int wait)
29815+{
29816+ int err, e;
5afbbe0d 29817+ aufs_bindex_t bbot, bindex;
537831f9
AM
29818+ struct au_branch *br;
29819+ struct super_block *h_sb;
29820+
29821+ err = 0;
29822+ si_noflush_read_lock(sb);
5afbbe0d
AM
29823+ bbot = au_sbbot(sb);
29824+ for (bindex = 0; bindex <= bbot; bindex++) {
537831f9
AM
29825+ br = au_sbr(sb, bindex);
29826+ if (!au_br_writable(br->br_perm))
29827+ continue;
29828+
29829+ h_sb = au_sbr_sb(sb, bindex);
a2654f78
AM
29830+ e = vfsub_sync_filesystem(h_sb, wait);
29831+ if (unlikely(e && !err))
29832+ err = e;
29833+ /* go on even if an error happens */
537831f9
AM
29834+ }
29835+ si_read_unlock(sb);
29836+
29837+ return err;
29838+}
29839+
29840+/* ---------------------------------------------------------------------- */
29841+
1facf9fc 29842+/* final actions when unmounting a file system */
29843+static void aufs_put_super(struct super_block *sb)
29844+{
29845+ struct au_sbinfo *sbinfo;
29846+
29847+ sbinfo = au_sbi(sb);
062440b3
AM
29848+ if (sbinfo)
29849+ kobject_put(&sbinfo->si_kobj);
1facf9fc 29850+}
29851+
29852+/* ---------------------------------------------------------------------- */
29853+
79b8bda9
AM
29854+void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
29855+ struct super_block *sb, void *arg)
7f207e10
AM
29856+{
29857+ void *array;
076b876e 29858+ unsigned long long n, sz;
7f207e10
AM
29859+
29860+ array = NULL;
29861+ n = 0;
29862+ if (!*hint)
29863+ goto out;
29864+
29865+ if (*hint > ULLONG_MAX / sizeof(array)) {
29866+ array = ERR_PTR(-EMFILE);
29867+ pr_err("hint %llu\n", *hint);
29868+ goto out;
29869+ }
29870+
076b876e
AM
29871+ sz = sizeof(array) * *hint;
29872+ array = kzalloc(sz, GFP_NOFS);
7f207e10 29873+ if (unlikely(!array))
076b876e 29874+ array = vzalloc(sz);
7f207e10
AM
29875+ if (unlikely(!array)) {
29876+ array = ERR_PTR(-ENOMEM);
29877+ goto out;
29878+ }
29879+
79b8bda9 29880+ n = cb(sb, array, *hint, arg);
7f207e10
AM
29881+ AuDebugOn(n > *hint);
29882+
29883+out:
29884+ *hint = n;
29885+ return array;
29886+}
29887+
79b8bda9 29888+static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
7f207e10
AM
29889+ unsigned long long max __maybe_unused,
29890+ void *arg)
29891+{
29892+ unsigned long long n;
29893+ struct inode **p, *inode;
29894+ struct list_head *head;
29895+
29896+ n = 0;
29897+ p = a;
29898+ head = arg;
79b8bda9 29899+ spin_lock(&sb->s_inode_list_lock);
7f207e10 29900+ list_for_each_entry(inode, head, i_sb_list) {
5afbbe0d
AM
29901+ if (!au_is_bad_inode(inode)
29902+ && au_ii(inode)->ii_btop >= 0) {
2cbb1c4b
JR
29903+ spin_lock(&inode->i_lock);
29904+ if (atomic_read(&inode->i_count)) {
29905+ au_igrab(inode);
29906+ *p++ = inode;
29907+ n++;
29908+ AuDebugOn(n > max);
29909+ }
29910+ spin_unlock(&inode->i_lock);
7f207e10
AM
29911+ }
29912+ }
79b8bda9 29913+ spin_unlock(&sb->s_inode_list_lock);
7f207e10
AM
29914+
29915+ return n;
29916+}
29917+
29918+struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
29919+{
acd2b654
AM
29920+ struct au_sbinfo *sbi;
29921+
29922+ sbi = au_sbi(sb);
29923+ *max = au_lcnt_read(&sbi->si_ninodes, /*do_rev*/1);
79b8bda9 29924+ return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
7f207e10
AM
29925+}
29926+
29927+void au_iarray_free(struct inode **a, unsigned long long max)
29928+{
29929+ unsigned long long ull;
29930+
29931+ for (ull = 0; ull < max; ull++)
29932+ iput(a[ull]);
be52b249 29933+ kvfree(a);
7f207e10
AM
29934+}
29935+
29936+/* ---------------------------------------------------------------------- */
29937+
1facf9fc 29938+/*
29939+ * refresh dentry and inode at remount time.
29940+ */
027c5e7a
AM
29941+/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
29942+static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
29943+ struct dentry *parent)
1facf9fc 29944+{
29945+ int err;
1facf9fc 29946+
29947+ di_write_lock_child(dentry);
1facf9fc 29948+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a
AM
29949+ err = au_refresh_dentry(dentry, parent);
29950+ if (!err && dir_flags)
5527c038 29951+ au_hn_reset(d_inode(dentry), dir_flags);
1facf9fc 29952+ di_read_unlock(parent, AuLock_IR);
1facf9fc 29953+ di_write_unlock(dentry);
29954+
29955+ return err;
29956+}
29957+
027c5e7a
AM
29958+static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
29959+ struct au_sbinfo *sbinfo,
b95c5147 29960+ const unsigned int dir_flags, unsigned int do_idop)
1facf9fc 29961+{
027c5e7a
AM
29962+ int err;
29963+ struct dentry *parent;
027c5e7a
AM
29964+
29965+ err = 0;
29966+ parent = dget_parent(dentry);
29967+ if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
5527c038
JR
29968+ if (d_really_is_positive(dentry)) {
29969+ if (!d_is_dir(dentry))
027c5e7a
AM
29970+ err = au_do_refresh(dentry, /*dir_flags*/0,
29971+ parent);
29972+ else {
29973+ err = au_do_refresh(dentry, dir_flags, parent);
29974+ if (unlikely(err))
29975+ au_fset_si(sbinfo, FAILED_REFRESH_DIR);
29976+ }
29977+ } else
29978+ err = au_do_refresh(dentry, /*dir_flags*/0, parent);
29979+ AuDbgDentry(dentry);
29980+ }
29981+ dput(parent);
29982+
79b8bda9 29983+ if (!err) {
b95c5147 29984+ if (do_idop)
79b8bda9
AM
29985+ au_refresh_dop(dentry, /*force_reval*/0);
29986+ } else
29987+ au_refresh_dop(dentry, /*force_reval*/1);
29988+
027c5e7a
AM
29989+ AuTraceErr(err);
29990+ return err;
1facf9fc 29991+}
29992+
b95c5147 29993+static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
1facf9fc 29994+{
29995+ int err, i, j, ndentry, e;
027c5e7a 29996+ unsigned int sigen;
1facf9fc 29997+ struct au_dcsub_pages dpages;
29998+ struct au_dpage *dpage;
027c5e7a
AM
29999+ struct dentry **dentries, *d;
30000+ struct au_sbinfo *sbinfo;
30001+ struct dentry *root = sb->s_root;
5527c038 30002+ const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
1facf9fc 30003+
b95c5147 30004+ if (do_idop)
79b8bda9
AM
30005+ au_refresh_dop(root, /*force_reval*/0);
30006+
027c5e7a
AM
30007+ err = au_dpages_init(&dpages, GFP_NOFS);
30008+ if (unlikely(err))
1facf9fc 30009+ goto out;
027c5e7a
AM
30010+ err = au_dcsub_pages(&dpages, root, NULL, NULL);
30011+ if (unlikely(err))
1facf9fc 30012+ goto out_dpages;
1facf9fc 30013+
027c5e7a
AM
30014+ sigen = au_sigen(sb);
30015+ sbinfo = au_sbi(sb);
30016+ for (i = 0; i < dpages.ndpage; i++) {
1facf9fc 30017+ dpage = dpages.dpages + i;
30018+ dentries = dpage->dentries;
30019+ ndentry = dpage->ndentry;
027c5e7a 30020+ for (j = 0; j < ndentry; j++) {
1facf9fc 30021+ d = dentries[j];
79b8bda9 30022+ e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
b95c5147 30023+ do_idop);
027c5e7a
AM
30024+ if (unlikely(e && !err))
30025+ err = e;
30026+ /* go on even err */
1facf9fc 30027+ }
30028+ }
30029+
4f0767ce 30030+out_dpages:
1facf9fc 30031+ au_dpages_free(&dpages);
4f0767ce 30032+out:
1facf9fc 30033+ return err;
30034+}
30035+
b95c5147 30036+static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
1facf9fc 30037+{
027c5e7a
AM
30038+ int err, e;
30039+ unsigned int sigen;
30040+ unsigned long long max, ull;
30041+ struct inode *inode, **array;
1facf9fc 30042+
027c5e7a
AM
30043+ array = au_iarray_alloc(sb, &max);
30044+ err = PTR_ERR(array);
30045+ if (IS_ERR(array))
30046+ goto out;
1facf9fc 30047+
30048+ err = 0;
027c5e7a
AM
30049+ sigen = au_sigen(sb);
30050+ for (ull = 0; ull < max; ull++) {
30051+ inode = array[ull];
076b876e
AM
30052+ if (unlikely(!inode))
30053+ break;
b95c5147
AM
30054+
30055+ e = 0;
30056+ ii_write_lock_child(inode);
537831f9 30057+ if (au_iigen(inode, NULL) != sigen) {
027c5e7a 30058+ e = au_refresh_hinode_self(inode);
1facf9fc 30059+ if (unlikely(e)) {
b95c5147 30060+ au_refresh_iop(inode, /*force_getattr*/1);
027c5e7a 30061+ pr_err("error %d, i%lu\n", e, inode->i_ino);
1facf9fc 30062+ if (!err)
30063+ err = e;
30064+ /* go on even if err */
30065+ }
30066+ }
b95c5147
AM
30067+ if (!e && do_idop)
30068+ au_refresh_iop(inode, /*force_getattr*/0);
30069+ ii_write_unlock(inode);
1facf9fc 30070+ }
30071+
027c5e7a 30072+ au_iarray_free(array, max);
1facf9fc 30073+
4f0767ce 30074+out:
1facf9fc 30075+ return err;
30076+}
30077+
b95c5147 30078+static void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
1facf9fc 30079+{
027c5e7a
AM
30080+ int err, e;
30081+ unsigned int udba;
5afbbe0d 30082+ aufs_bindex_t bindex, bbot;
1facf9fc 30083+ struct dentry *root;
30084+ struct inode *inode;
027c5e7a 30085+ struct au_branch *br;
79b8bda9 30086+ struct au_sbinfo *sbi;
1facf9fc 30087+
30088+ au_sigen_inc(sb);
79b8bda9
AM
30089+ sbi = au_sbi(sb);
30090+ au_fclr_si(sbi, FAILED_REFRESH_DIR);
1facf9fc 30091+
30092+ root = sb->s_root;
30093+ DiMustNoWaiters(root);
5527c038 30094+ inode = d_inode(root);
1facf9fc 30095+ IiMustNoWaiters(inode);
1facf9fc 30096+
027c5e7a 30097+ udba = au_opt_udba(sb);
5afbbe0d
AM
30098+ bbot = au_sbbot(sb);
30099+ for (bindex = 0; bindex <= bbot; bindex++) {
027c5e7a
AM
30100+ br = au_sbr(sb, bindex);
30101+ err = au_hnotify_reset_br(udba, br, br->br_perm);
1facf9fc 30102+ if (unlikely(err))
027c5e7a
AM
30103+ AuIOErr("hnotify failed on br %d, %d, ignored\n",
30104+ bindex, err);
30105+ /* go on even if err */
1facf9fc 30106+ }
027c5e7a 30107+ au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
1facf9fc 30108+
b95c5147 30109+ if (do_idop) {
79b8bda9
AM
30110+ if (au_ftest_si(sbi, NO_DREVAL)) {
30111+ AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
30112+ sb->s_d_op = &aufs_dop_noreval;
b95c5147
AM
30113+ AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
30114+ sbi->si_iop_array = aufs_iop_nogetattr;
79b8bda9
AM
30115+ } else {
30116+ AuDebugOn(sb->s_d_op == &aufs_dop);
30117+ sb->s_d_op = &aufs_dop;
b95c5147
AM
30118+ AuDebugOn(sbi->si_iop_array == aufs_iop);
30119+ sbi->si_iop_array = aufs_iop;
79b8bda9 30120+ }
062440b3 30121+ pr_info("reset to %ps and %ps\n",
b95c5147 30122+ sb->s_d_op, sbi->si_iop_array);
79b8bda9
AM
30123+ }
30124+
027c5e7a 30125+ di_write_unlock(root);
b95c5147
AM
30126+ err = au_refresh_d(sb, do_idop);
30127+ e = au_refresh_i(sb, do_idop);
027c5e7a
AM
30128+ if (unlikely(e && !err))
30129+ err = e;
1facf9fc 30130+ /* aufs_write_lock() calls ..._child() */
30131+ di_write_lock_child(root);
027c5e7a
AM
30132+
30133+ au_cpup_attr_all(inode, /*force*/1);
30134+
30135+ if (unlikely(err))
30136+ AuIOErr("refresh failed, ignored, %d\n", err);
1facf9fc 30137+}
30138+
30139+/* stop extra interpretation of errno in mount(8), and strange error messages */
30140+static int cvt_err(int err)
30141+{
30142+ AuTraceErr(err);
30143+
30144+ switch (err) {
30145+ case -ENOENT:
30146+ case -ENOTDIR:
30147+ case -EEXIST:
30148+ case -EIO:
30149+ err = -EINVAL;
30150+ }
30151+ return err;
30152+}
30153+
30154+static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
30155+{
4a4d8108
AM
30156+ int err, do_dx;
30157+ unsigned int mntflags;
be52b249
AM
30158+ struct au_opts opts = {
30159+ .opt = NULL
30160+ };
1facf9fc 30161+ struct dentry *root;
30162+ struct inode *inode;
30163+ struct au_sbinfo *sbinfo;
30164+
30165+ err = 0;
30166+ root = sb->s_root;
30167+ if (!data || !*data) {
e49829fe
JR
30168+ err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
30169+ if (!err) {
30170+ di_write_lock_child(root);
30171+ err = au_opts_verify(sb, *flags, /*pending*/0);
30172+ aufs_write_unlock(root);
30173+ }
1facf9fc 30174+ goto out;
30175+ }
30176+
30177+ err = -ENOMEM;
1facf9fc 30178+ opts.opt = (void *)__get_free_page(GFP_NOFS);
30179+ if (unlikely(!opts.opt))
30180+ goto out;
30181+ opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
30182+ opts.flags = AuOpts_REMOUNT;
30183+ opts.sb_flags = *flags;
30184+
30185+ /* parse it before aufs lock */
30186+ err = au_opts_parse(sb, data, &opts);
30187+ if (unlikely(err))
30188+ goto out_opts;
30189+
30190+ sbinfo = au_sbi(sb);
5527c038 30191+ inode = d_inode(root);
febd17d6 30192+ inode_lock(inode);
e49829fe
JR
30193+ err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
30194+ if (unlikely(err))
30195+ goto out_mtx;
30196+ di_write_lock_child(root);
1facf9fc 30197+
30198+ /* au_opts_remount() may return an error */
30199+ err = au_opts_remount(sb, &opts);
30200+ au_opts_free(&opts);
30201+
027c5e7a 30202+ if (au_ftest_opts(opts.flags, REFRESH))
b95c5147 30203+ au_remount_refresh(sb, au_ftest_opts(opts.flags, REFRESH_IDOP));
1facf9fc 30204+
4a4d8108
AM
30205+ if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
30206+ mntflags = au_mntflags(sb);
30207+ do_dx = !!au_opt_test(mntflags, DIO);
30208+ au_dy_arefresh(do_dx);
30209+ }
30210+
076b876e 30211+ au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
1facf9fc 30212+ aufs_write_unlock(root);
953406b4 30213+
e49829fe 30214+out_mtx:
febd17d6 30215+ inode_unlock(inode);
4f0767ce 30216+out_opts:
1c60b727 30217+ free_page((unsigned long)opts.opt);
4f0767ce 30218+out:
1facf9fc 30219+ err = cvt_err(err);
30220+ AuTraceErr(err);
30221+ return err;
30222+}
30223+
4a4d8108 30224+static const struct super_operations aufs_sop = {
1facf9fc 30225+ .alloc_inode = aufs_alloc_inode,
30226+ .destroy_inode = aufs_destroy_inode,
fbc438ed 30227+ .free_inode = aufs_free_inode,
b752ccd1 30228+ /* always deleting, no clearing */
1facf9fc 30229+ .drop_inode = generic_delete_inode,
30230+ .show_options = aufs_show_options,
30231+ .statfs = aufs_statfs,
30232+ .put_super = aufs_put_super,
537831f9 30233+ .sync_fs = aufs_sync_fs,
1facf9fc 30234+ .remount_fs = aufs_remount_fs
30235+};
30236+
30237+/* ---------------------------------------------------------------------- */
30238+
30239+static int alloc_root(struct super_block *sb)
30240+{
30241+ int err;
30242+ struct inode *inode;
30243+ struct dentry *root;
30244+
30245+ err = -ENOMEM;
30246+ inode = au_iget_locked(sb, AUFS_ROOT_INO);
30247+ err = PTR_ERR(inode);
30248+ if (IS_ERR(inode))
30249+ goto out;
30250+
b95c5147 30251+ inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
1facf9fc 30252+ inode->i_fop = &aufs_dir_fop;
30253+ inode->i_mode = S_IFDIR;
9dbd164d 30254+ set_nlink(inode, 2);
1facf9fc 30255+ unlock_new_inode(inode);
30256+
92d182d2 30257+ root = d_make_root(inode);
1facf9fc 30258+ if (unlikely(!root))
92d182d2 30259+ goto out;
1facf9fc 30260+ err = PTR_ERR(root);
30261+ if (IS_ERR(root))
92d182d2 30262+ goto out;
1facf9fc 30263+
4a4d8108 30264+ err = au_di_init(root);
1facf9fc 30265+ if (!err) {
30266+ sb->s_root = root;
30267+ return 0; /* success */
30268+ }
30269+ dput(root);
1facf9fc 30270+
4f0767ce 30271+out:
1facf9fc 30272+ return err;
1facf9fc 30273+}
30274+
30275+static int aufs_fill_super(struct super_block *sb, void *raw_data,
30276+ int silent __maybe_unused)
30277+{
30278+ int err;
be52b249
AM
30279+ struct au_opts opts = {
30280+ .opt = NULL
30281+ };
79b8bda9 30282+ struct au_sbinfo *sbinfo;
1facf9fc 30283+ struct dentry *root;
30284+ struct inode *inode;
30285+ char *arg = raw_data;
30286+
30287+ if (unlikely(!arg || !*arg)) {
30288+ err = -EINVAL;
4a4d8108 30289+ pr_err("no arg\n");
1facf9fc 30290+ goto out;
30291+ }
30292+
30293+ err = -ENOMEM;
1facf9fc 30294+ opts.opt = (void *)__get_free_page(GFP_NOFS);
30295+ if (unlikely(!opts.opt))
30296+ goto out;
30297+ opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
30298+ opts.sb_flags = sb->s_flags;
30299+
30300+ err = au_si_alloc(sb);
30301+ if (unlikely(err))
30302+ goto out_opts;
79b8bda9 30303+ sbinfo = au_sbi(sb);
1facf9fc 30304+
30305+ /* all timestamps always follow the ones on the branch */
2121bcd9 30306+ sb->s_flags |= SB_NOATIME | SB_NODIRATIME;
be118d29 30307+ sb->s_flags |= SB_I_VERSION; /* do we really need this? */
1facf9fc 30308+ sb->s_op = &aufs_sop;
027c5e7a 30309+ sb->s_d_op = &aufs_dop;
1facf9fc 30310+ sb->s_magic = AUFS_SUPER_MAGIC;
30311+ sb->s_maxbytes = 0;
c1595e42 30312+ sb->s_stack_depth = 1;
1facf9fc 30313+ au_export_init(sb);
f2c43d5f 30314+ au_xattr_init(sb);
1facf9fc 30315+
30316+ err = alloc_root(sb);
30317+ if (unlikely(err)) {
30318+ si_write_unlock(sb);
30319+ goto out_info;
30320+ }
30321+ root = sb->s_root;
5527c038 30322+ inode = d_inode(root);
1facf9fc 30323+
30324+ /*
30325+ * actually we can parse options regardless aufs lock here.
30326+ * but at remount time, parsing must be done before aufs lock.
30327+ * so we follow the same rule.
30328+ */
30329+ ii_write_lock_parent(inode);
30330+ aufs_write_unlock(root);
30331+ err = au_opts_parse(sb, arg, &opts);
30332+ if (unlikely(err))
30333+ goto out_root;
30334+
30335+ /* lock vfs_inode first, then aufs. */
febd17d6 30336+ inode_lock(inode);
1facf9fc 30337+ aufs_write_lock(root);
30338+ err = au_opts_mount(sb, &opts);
30339+ au_opts_free(&opts);
79b8bda9
AM
30340+ if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
30341+ sb->s_d_op = &aufs_dop_noreval;
062440b3 30342+ pr_info("%ps\n", sb->s_d_op);
79b8bda9 30343+ au_refresh_dop(root, /*force_reval*/0);
b95c5147
AM
30344+ sbinfo->si_iop_array = aufs_iop_nogetattr;
30345+ au_refresh_iop(inode, /*force_getattr*/0);
79b8bda9 30346+ }
1facf9fc 30347+ aufs_write_unlock(root);
febd17d6 30348+ inode_unlock(inode);
4a4d8108
AM
30349+ if (!err)
30350+ goto out_opts; /* success */
1facf9fc 30351+
4f0767ce 30352+out_root:
1facf9fc 30353+ dput(root);
30354+ sb->s_root = NULL;
4f0767ce 30355+out_info:
79b8bda9 30356+ kobject_put(&sbinfo->si_kobj);
1facf9fc 30357+ sb->s_fs_info = NULL;
4f0767ce 30358+out_opts:
1c60b727 30359+ free_page((unsigned long)opts.opt);
4f0767ce 30360+out:
1facf9fc 30361+ AuTraceErr(err);
30362+ err = cvt_err(err);
30363+ AuTraceErr(err);
30364+ return err;
30365+}
30366+
30367+/* ---------------------------------------------------------------------- */
30368+
027c5e7a
AM
30369+static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
30370+ const char *dev_name __maybe_unused,
30371+ void *raw_data)
1facf9fc 30372+{
027c5e7a 30373+ struct dentry *root;
1facf9fc 30374+
30375+ /* all timestamps always follow the ones on the branch */
30376+ /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
027c5e7a
AM
30377+ root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
30378+ if (IS_ERR(root))
30379+ goto out;
30380+
062440b3 30381+ au_sbilist_add(root->d_sb);
027c5e7a
AM
30382+
30383+out:
30384+ return root;
1facf9fc 30385+}
30386+
e49829fe
JR
30387+static void aufs_kill_sb(struct super_block *sb)
30388+{
30389+ struct au_sbinfo *sbinfo;
30390+
30391+ sbinfo = au_sbi(sb);
30392+ if (sbinfo) {
30393+ au_sbilist_del(sb);
30394+ aufs_write_lock(sb->s_root);
076b876e 30395+ au_fhsm_fin(sb);
e49829fe
JR
30396+ if (sbinfo->si_wbr_create_ops->fin)
30397+ sbinfo->si_wbr_create_ops->fin(sb);
30398+ if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
30399+ au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
b95c5147 30400+ au_remount_refresh(sb, /*do_idop*/0);
e49829fe
JR
30401+ }
30402+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
30403+ au_plink_put(sb, /*verbose*/1);
30404+ au_xino_clr(sb);
8b6a4947 30405+ au_dr_opt_flush(sb);
1e00d052 30406+ sbinfo->si_sb = NULL;
e49829fe 30407+ aufs_write_unlock(sb->s_root);
e49829fe
JR
30408+ au_nwt_flush(&sbinfo->si_nowait);
30409+ }
98d9a5b1 30410+ kill_anon_super(sb);
e49829fe
JR
30411+}
30412+
1facf9fc 30413+struct file_system_type aufs_fs_type = {
30414+ .name = AUFS_FSTYPE,
c06a8ce3 30415+ /* a race between rename and others */
0b2a12c6
JR
30416+ .fs_flags = FS_RENAME_DOES_D_MOVE
30417+ /* untested */
30418+ /*| FS_ALLOW_IDMAP*/
30419+ ,
027c5e7a 30420+ .mount = aufs_mount,
e49829fe 30421+ .kill_sb = aufs_kill_sb,
1facf9fc 30422+ /* no need to __module_get() and module_put(). */
30423+ .owner = THIS_MODULE,
30424+};
7f207e10 30425diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
eca34b5c 30426--- /usr/share/empty/fs/aufs/super.h 1970-01-01 01:00:00.000000000 +0100
46016270 30427+++ linux/fs/aufs/super.h 2021-02-24 13:33:42.747680497 +0100
e37dd06a 30428@@ -0,0 +1,587 @@
062440b3 30429+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 30430+/*
d58c55f2 30431+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 30432+ *
30433+ * This program, aufs is free software; you can redistribute it and/or modify
30434+ * it under the terms of the GNU General Public License as published by
30435+ * the Free Software Foundation; either version 2 of the License, or
30436+ * (at your option) any later version.
dece6358
AM
30437+ *
30438+ * This program is distributed in the hope that it will be useful,
30439+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
30440+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30441+ * GNU General Public License for more details.
30442+ *
30443+ * You should have received a copy of the GNU General Public License
523b37e3 30444+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 30445+ */
30446+
30447+/*
30448+ * super_block operations
30449+ */
30450+
30451+#ifndef __AUFS_SUPER_H__
30452+#define __AUFS_SUPER_H__
30453+
30454+#ifdef __KERNEL__
30455+
30456+#include <linux/fs.h>
5527c038 30457+#include <linux/kobject.h>
8b6a4947 30458+#include "hbl.h"
acd2b654 30459+#include "lcnt.h"
1facf9fc 30460+#include "rwsem.h"
1facf9fc 30461+#include "wkq.h"
30462+
1facf9fc 30463+/* policies to select one among multiple writable branches */
30464+struct au_wbr_copyup_operations {
30465+ int (*copyup)(struct dentry *dentry);
30466+};
30467+
392086de
AM
30468+#define AuWbr_DIR 1 /* target is a dir */
30469+#define AuWbr_PARENT (1 << 1) /* always require a parent */
30470+
30471+#define au_ftest_wbr(flags, name) ((flags) & AuWbr_##name)
30472+#define au_fset_wbr(flags, name) { (flags) |= AuWbr_##name; }
30473+#define au_fclr_wbr(flags, name) { (flags) &= ~AuWbr_##name; }
30474+
1facf9fc 30475+struct au_wbr_create_operations {
392086de 30476+ int (*create)(struct dentry *dentry, unsigned int flags);
1facf9fc 30477+ int (*init)(struct super_block *sb);
30478+ int (*fin)(struct super_block *sb);
30479+};
30480+
30481+struct au_wbr_mfs {
30482+ struct mutex mfs_lock; /* protect this structure */
30483+ unsigned long mfs_jiffy;
30484+ unsigned long mfs_expire;
30485+ aufs_bindex_t mfs_bindex;
30486+
30487+ unsigned long long mfsrr_bytes;
30488+ unsigned long long mfsrr_watermark;
30489+};
30490+
86dc4139
AM
30491+#define AuPlink_NHASH 100
30492+static inline int au_plink_hash(ino_t ino)
30493+{
30494+ return ino % AuPlink_NHASH;
30495+}
30496+
076b876e
AM
30497+/* File-based Hierarchical Storage Management */
30498+struct au_fhsm {
30499+#ifdef CONFIG_AUFS_FHSM
30500+ /* allow only one process who can receive the notification */
30501+ spinlock_t fhsm_spin;
30502+ pid_t fhsm_pid;
30503+ wait_queue_head_t fhsm_wqh;
30504+ atomic_t fhsm_readable;
30505+
c1595e42 30506+ /* these are protected by si_rwsem */
076b876e 30507+ unsigned long fhsm_expire;
c1595e42 30508+ aufs_bindex_t fhsm_bottom;
076b876e
AM
30509+#endif
30510+};
30511+
1facf9fc 30512+struct au_branch;
30513+struct au_sbinfo {
30514+ /* nowait tasks in the system-wide workqueue */
30515+ struct au_nowait_tasks si_nowait;
30516+
b752ccd1 30517+ /*
acd2b654 30518+ * tried sb->s_umount, but failed due to the dependency between i_mutex.
b752ccd1
AM
30519+ * rwsem for au_sbinfo is necessary.
30520+ */
dece6358 30521+ struct au_rwsem si_rwsem;
1facf9fc 30522+
7f207e10 30523+ /*
523b37e3
AM
30524+ * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
30525+ * remount.
7f207e10 30526+ */
acd2b654 30527+ au_lcnt_t si_ninodes, si_nfiles;
7f207e10 30528+
1facf9fc 30529+ /* branch management */
30530+ unsigned int si_generation;
30531+
2000de60 30532+ /* see AuSi_ flags */
1facf9fc 30533+ unsigned char au_si_status;
30534+
5afbbe0d 30535+ aufs_bindex_t si_bbot;
7f207e10
AM
30536+
30537+ /* dirty trick to keep br_id plus */
30538+ unsigned int si_last_br_id :
30539+ sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
1facf9fc 30540+ struct au_branch **si_branch;
30541+
30542+ /* policy to select a writable branch */
30543+ unsigned char si_wbr_copyup;
30544+ unsigned char si_wbr_create;
30545+ struct au_wbr_copyup_operations *si_wbr_copyup_ops;
30546+ struct au_wbr_create_operations *si_wbr_create_ops;
30547+
30548+ /* round robin */
30549+ atomic_t si_wbr_rr_next;
30550+
30551+ /* most free space */
30552+ struct au_wbr_mfs si_wbr_mfs;
30553+
076b876e
AM
30554+ /* File-based Hierarchical Storage Management */
30555+ struct au_fhsm si_fhsm;
30556+
1facf9fc 30557+ /* mount flags */
30558+ /* include/asm-ia64/siginfo.h defines a macro named si_flags */
30559+ unsigned int si_mntflags;
30560+
30561+ /* external inode number (bitmap and translation table) */
acd2b654
AM
30562+ loff_t si_ximaxent; /* max entries in a xino */
30563+
1facf9fc 30564+ struct file *si_xib;
30565+ struct mutex si_xib_mtx; /* protect xib members */
30566+ unsigned long *si_xib_buf;
30567+ unsigned long si_xib_last_pindex;
30568+ int si_xib_next_bit;
acd2b654 30569+
392086de
AM
30570+ unsigned long si_xino_jiffy;
30571+ unsigned long si_xino_expire;
1facf9fc 30572+ /* reserved for future use */
30573+ /* unsigned long long si_xib_limit; */ /* Max xib file size */
30574+
30575+#ifdef CONFIG_AUFS_EXPORT
30576+ /* i_generation */
acd2b654 30577+ /* todo: make xigen file an array to support many inode numbers */
1facf9fc 30578+ struct file *si_xigen;
30579+ atomic_t si_xigen_next;
30580+#endif
30581+
acd2b654 30582+ /* dirty trick to support atomic_open */
8b6a4947 30583+ struct hlist_bl_head si_aopen;
b912730e 30584+
1facf9fc 30585+ /* vdir parameters */
e49829fe 30586+ unsigned long si_rdcache; /* max cache time in jiffies */
1facf9fc 30587+ unsigned int si_rdblk; /* deblk size */
30588+ unsigned int si_rdhash; /* hash size */
30589+
30590+ /*
30591+ * If the number of whiteouts are larger than si_dirwh, leave all of
30592+ * them after au_whtmp_ren to reduce the cost of rmdir(2).
30593+ * future fsck.aufs or kernel thread will remove them later.
30594+ * Otherwise, remove all whiteouts and the dir in rmdir(2).
30595+ */
30596+ unsigned int si_dirwh;
30597+
1facf9fc 30598+ /* pseudo_link list */
8b6a4947 30599+ struct hlist_bl_head si_plink[AuPlink_NHASH];
1facf9fc 30600+ wait_queue_head_t si_plink_wq;
4a4d8108 30601+ spinlock_t si_plink_maint_lock;
e49829fe 30602+ pid_t si_plink_maint_pid;
1facf9fc 30603+
523b37e3 30604+ /* file list */
8b6a4947 30605+ struct hlist_bl_head si_files;
523b37e3 30606+
b95c5147 30607+ /* with/without getattr, brother of sb->s_d_op */
43982f53 30608+ const struct inode_operations *si_iop_array;
b95c5147 30609+
1facf9fc 30610+ /*
30611+ * sysfs and lifetime management.
30612+ * this is not a small structure and it may be a waste of memory in case
acd2b654 30613+ * of sysfs is disabled, particularly when many aufs-es are mounted.
1facf9fc 30614+ * but using sysfs is majority.
30615+ */
30616+ struct kobject si_kobj;
30617+#ifdef CONFIG_DEBUG_FS
86dc4139
AM
30618+ struct dentry *si_dbgaufs;
30619+ struct dentry *si_dbgaufs_plink;
30620+ struct dentry *si_dbgaufs_xib;
1facf9fc 30621+#ifdef CONFIG_AUFS_EXPORT
30622+ struct dentry *si_dbgaufs_xigen;
30623+#endif
30624+#endif
30625+
e49829fe 30626+#ifdef CONFIG_AUFS_SBILIST
8b6a4947 30627+ struct hlist_bl_node si_list;
e49829fe
JR
30628+#endif
30629+
1facf9fc 30630+ /* dirty, necessary for unmounting, sysfs and sysrq */
30631+ struct super_block *si_sb;
30632+};
30633+
dece6358
AM
30634+/* sbinfo status flags */
30635+/*
30636+ * set true when refresh_dirs() failed at remount time.
30637+ * then try refreshing dirs at access time again.
062440b3 30638+ * if it is false, refreshing dirs at access time is unnecessary
dece6358 30639+ */
027c5e7a 30640+#define AuSi_FAILED_REFRESH_DIR 1
076b876e 30641+#define AuSi_FHSM (1 << 1) /* fhsm is active now */
79b8bda9 30642+#define AuSi_NO_DREVAL (1 << 2) /* disable all d_revalidate */
076b876e
AM
30643+
30644+#ifndef CONFIG_AUFS_FHSM
30645+#undef AuSi_FHSM
30646+#define AuSi_FHSM 0
30647+#endif
30648+
dece6358
AM
30649+static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
30650+ unsigned int flag)
30651+{
30652+ AuRwMustAnyLock(&sbi->si_rwsem);
30653+ return sbi->au_si_status & flag;
30654+}
30655+#define au_ftest_si(sbinfo, name) au_do_ftest_si(sbinfo, AuSi_##name)
30656+#define au_fset_si(sbinfo, name) do { \
30657+ AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30658+ (sbinfo)->au_si_status |= AuSi_##name; \
30659+} while (0)
30660+#define au_fclr_si(sbinfo, name) do { \
30661+ AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30662+ (sbinfo)->au_si_status &= ~AuSi_##name; \
30663+} while (0)
30664+
1facf9fc 30665+/* ---------------------------------------------------------------------- */
30666+
30667+/* policy to select one among writable branches */
4a4d8108
AM
30668+#define AuWbrCopyup(sbinfo, ...) \
30669+ ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
30670+#define AuWbrCreate(sbinfo, ...) \
30671+ ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
1facf9fc 30672+
30673+/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
30674+#define AuLock_DW 1 /* write-lock dentry */
30675+#define AuLock_IR (1 << 1) /* read-lock inode */
30676+#define AuLock_IW (1 << 2) /* write-lock inode */
30677+#define AuLock_FLUSH (1 << 3) /* wait for 'nowait' tasks */
b95c5147 30678+#define AuLock_DIRS (1 << 4) /* target is a pair of dirs */
f2c43d5f 30679+ /* except RENAME_EXCHANGE */
e49829fe
JR
30680+#define AuLock_NOPLM (1 << 5) /* return err in plm mode */
30681+#define AuLock_NOPLMW (1 << 6) /* wait for plm mode ends */
027c5e7a 30682+#define AuLock_GEN (1 << 7) /* test digen/iigen */
1facf9fc 30683+#define au_ftest_lock(flags, name) ((flags) & AuLock_##name)
7f207e10
AM
30684+#define au_fset_lock(flags, name) \
30685+ do { (flags) |= AuLock_##name; } while (0)
30686+#define au_fclr_lock(flags, name) \
30687+ do { (flags) &= ~AuLock_##name; } while (0)
1facf9fc 30688+
30689+/* ---------------------------------------------------------------------- */
30690+
30691+/* super.c */
30692+extern struct file_system_type aufs_fs_type;
30693+struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
79b8bda9
AM
30694+typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
30695+ unsigned long long max, void *arg);
79b8bda9
AM
30696+void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30697+ struct super_block *sb, void *arg);
7f207e10
AM
30698+struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
30699+void au_iarray_free(struct inode **a, unsigned long long max);
1facf9fc 30700+
30701+/* sbinfo.c */
30702+void au_si_free(struct kobject *kobj);
30703+int au_si_alloc(struct super_block *sb);
e2f27e51 30704+int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
1facf9fc 30705+
30706+unsigned int au_sigen_inc(struct super_block *sb);
30707+aufs_bindex_t au_new_br_id(struct super_block *sb);
30708+
e49829fe
JR
30709+int si_read_lock(struct super_block *sb, int flags);
30710+int si_write_lock(struct super_block *sb, int flags);
30711+int aufs_read_lock(struct dentry *dentry, int flags);
1facf9fc 30712+void aufs_read_unlock(struct dentry *dentry, int flags);
30713+void aufs_write_lock(struct dentry *dentry);
30714+void aufs_write_unlock(struct dentry *dentry);
e49829fe 30715+int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
1facf9fc 30716+void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
30717+
30718+/* wbr_policy.c */
30719+extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
30720+extern struct au_wbr_create_operations au_wbr_create_ops[];
30721+int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
c2b27bf2 30722+int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
5afbbe0d 30723+int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
c2b27bf2
AM
30724+
30725+/* mvdown.c */
30726+int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
1facf9fc 30727+
076b876e
AM
30728+#ifdef CONFIG_AUFS_FHSM
30729+/* fhsm.c */
30730+
30731+static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
30732+{
30733+ pid_t pid;
30734+
30735+ spin_lock(&fhsm->fhsm_spin);
30736+ pid = fhsm->fhsm_pid;
30737+ spin_unlock(&fhsm->fhsm_spin);
30738+
30739+ return pid;
30740+}
30741+
30742+void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
30743+void au_fhsm_wrote_all(struct super_block *sb, int force);
30744+int au_fhsm_fd(struct super_block *sb, int oflags);
30745+int au_fhsm_br_alloc(struct au_branch *br);
c1595e42 30746+void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
076b876e
AM
30747+void au_fhsm_fin(struct super_block *sb);
30748+void au_fhsm_init(struct au_sbinfo *sbinfo);
30749+void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
30750+void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
30751+#else
30752+AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
30753+ int force)
30754+AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
30755+AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
c1595e42
JR
30756+AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
30757+AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
30758+AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
076b876e
AM
30759+AuStubVoid(au_fhsm_fin, struct super_block *sb)
30760+AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
30761+AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
30762+AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
30763+#endif
30764+
1facf9fc 30765+/* ---------------------------------------------------------------------- */
30766+
30767+static inline struct au_sbinfo *au_sbi(struct super_block *sb)
30768+{
30769+ return sb->s_fs_info;
30770+}
30771+
30772+/* ---------------------------------------------------------------------- */
30773+
30774+#ifdef CONFIG_AUFS_EXPORT
a2a7ad62 30775+int au_test_nfsd(void);
1facf9fc 30776+void au_export_init(struct super_block *sb);
b752ccd1 30777+void au_xigen_inc(struct inode *inode);
1facf9fc 30778+int au_xigen_new(struct inode *inode);
062440b3 30779+int au_xigen_set(struct super_block *sb, struct path *path);
1facf9fc 30780+void au_xigen_clr(struct super_block *sb);
30781+
30782+static inline int au_busy_or_stale(void)
30783+{
b752ccd1 30784+ if (!au_test_nfsd())
1facf9fc 30785+ return -EBUSY;
30786+ return -ESTALE;
30787+}
30788+#else
b752ccd1 30789+AuStubInt0(au_test_nfsd, void)
a2a7ad62 30790+AuStubVoid(au_export_init, struct super_block *sb)
b752ccd1 30791+AuStubVoid(au_xigen_inc, struct inode *inode)
4a4d8108 30792+AuStubInt0(au_xigen_new, struct inode *inode)
062440b3 30793+AuStubInt0(au_xigen_set, struct super_block *sb, struct path *path)
4a4d8108 30794+AuStubVoid(au_xigen_clr, struct super_block *sb)
c1595e42 30795+AuStub(int, au_busy_or_stale, return -EBUSY, void)
1facf9fc 30796+#endif /* CONFIG_AUFS_EXPORT */
30797+
30798+/* ---------------------------------------------------------------------- */
30799+
e49829fe
JR
30800+#ifdef CONFIG_AUFS_SBILIST
30801+/* module.c */
8b6a4947 30802+extern struct hlist_bl_head au_sbilist;
e49829fe
JR
30803+
30804+static inline void au_sbilist_init(void)
30805+{
8b6a4947 30806+ INIT_HLIST_BL_HEAD(&au_sbilist);
e49829fe
JR
30807+}
30808+
30809+static inline void au_sbilist_add(struct super_block *sb)
30810+{
8b6a4947 30811+ au_hbl_add(&au_sbi(sb)->si_list, &au_sbilist);
e49829fe
JR
30812+}
30813+
30814+static inline void au_sbilist_del(struct super_block *sb)
30815+{
8b6a4947 30816+ au_hbl_del(&au_sbi(sb)->si_list, &au_sbilist);
e49829fe 30817+}
53392da6
AM
30818+
30819+#ifdef CONFIG_AUFS_MAGIC_SYSRQ
30820+static inline void au_sbilist_lock(void)
30821+{
8b6a4947 30822+ hlist_bl_lock(&au_sbilist);
53392da6
AM
30823+}
30824+
30825+static inline void au_sbilist_unlock(void)
30826+{
8b6a4947 30827+ hlist_bl_unlock(&au_sbilist);
53392da6
AM
30828+}
30829+#define AuGFP_SBILIST GFP_ATOMIC
30830+#else
30831+AuStubVoid(au_sbilist_lock, void)
30832+AuStubVoid(au_sbilist_unlock, void)
30833+#define AuGFP_SBILIST GFP_NOFS
30834+#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
e49829fe
JR
30835+#else
30836+AuStubVoid(au_sbilist_init, void)
c1595e42
JR
30837+AuStubVoid(au_sbilist_add, struct super_block *sb)
30838+AuStubVoid(au_sbilist_del, struct super_block *sb)
53392da6
AM
30839+AuStubVoid(au_sbilist_lock, void)
30840+AuStubVoid(au_sbilist_unlock, void)
30841+#define AuGFP_SBILIST GFP_NOFS
e49829fe
JR
30842+#endif
30843+
30844+/* ---------------------------------------------------------------------- */
30845+
1facf9fc 30846+static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
30847+{
dece6358 30848+ /*
c1595e42 30849+ * This function is a dynamic '__init' function actually,
dece6358
AM
30850+ * so the tiny check for si_rwsem is unnecessary.
30851+ */
30852+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
1facf9fc 30853+#ifdef CONFIG_DEBUG_FS
30854+ sbinfo->si_dbgaufs = NULL;
86dc4139 30855+ sbinfo->si_dbgaufs_plink = NULL;
1facf9fc 30856+ sbinfo->si_dbgaufs_xib = NULL;
30857+#ifdef CONFIG_AUFS_EXPORT
30858+ sbinfo->si_dbgaufs_xigen = NULL;
30859+#endif
30860+#endif
30861+}
30862+
30863+/* ---------------------------------------------------------------------- */
30864+
a2654f78
AM
30865+/* current->atomic_flags */
30866+/* this value should never corrupt the ones defined in linux/sched.h */
fbc438ed 30867+#define PFA_AUFS 0x10
a2654f78
AM
30868+
30869+TASK_PFA_TEST(AUFS, test_aufs) /* task_test_aufs */
30870+TASK_PFA_SET(AUFS, aufs) /* task_set_aufs */
30871+TASK_PFA_CLEAR(AUFS, aufs) /* task_clear_aufs */
b752ccd1
AM
30872+
30873+static inline int si_pid_test(struct super_block *sb)
30874+{
a2654f78 30875+ return !!task_test_aufs(current);
b752ccd1
AM
30876+}
30877+
30878+static inline void si_pid_clr(struct super_block *sb)
30879+{
a2654f78
AM
30880+ AuDebugOn(!task_test_aufs(current));
30881+ task_clear_aufs(current);
b752ccd1
AM
30882+}
30883+
a2654f78
AM
30884+static inline void si_pid_set(struct super_block *sb)
30885+{
30886+ AuDebugOn(task_test_aufs(current));
30887+ task_set_aufs(current);
30888+}
febd17d6 30889+
b752ccd1
AM
30890+/* ---------------------------------------------------------------------- */
30891+
1facf9fc 30892+/* lock superblock. mainly for entry point functions */
8b6a4947
AM
30893+#define __si_read_lock(sb) au_rw_read_lock(&au_sbi(sb)->si_rwsem)
30894+#define __si_write_lock(sb) au_rw_write_lock(&au_sbi(sb)->si_rwsem)
30895+#define __si_read_trylock(sb) au_rw_read_trylock(&au_sbi(sb)->si_rwsem)
30896+#define __si_write_trylock(sb) au_rw_write_trylock(&au_sbi(sb)->si_rwsem)
30897+/*
30898+#define __si_read_trylock_nested(sb) \
30899+ au_rw_read_trylock_nested(&au_sbi(sb)->si_rwsem)
30900+#define __si_write_trylock_nested(sb) \
30901+ au_rw_write_trylock_nested(&au_sbi(sb)->si_rwsem)
30902+*/
30903+
30904+#define __si_read_unlock(sb) au_rw_read_unlock(&au_sbi(sb)->si_rwsem)
30905+#define __si_write_unlock(sb) au_rw_write_unlock(&au_sbi(sb)->si_rwsem)
30906+#define __si_downgrade_lock(sb) au_rw_dgrade_lock(&au_sbi(sb)->si_rwsem)
1facf9fc 30907+
dece6358
AM
30908+#define SiMustNoWaiters(sb) AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
30909+#define SiMustAnyLock(sb) AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
30910+#define SiMustWriteLock(sb) AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
30911+
b752ccd1
AM
30912+static inline void si_noflush_read_lock(struct super_block *sb)
30913+{
30914+ __si_read_lock(sb);
30915+ si_pid_set(sb);
30916+}
30917+
30918+static inline int si_noflush_read_trylock(struct super_block *sb)
30919+{
076b876e
AM
30920+ int locked;
30921+
30922+ locked = __si_read_trylock(sb);
b752ccd1
AM
30923+ if (locked)
30924+ si_pid_set(sb);
30925+ return locked;
30926+}
30927+
30928+static inline void si_noflush_write_lock(struct super_block *sb)
30929+{
30930+ __si_write_lock(sb);
30931+ si_pid_set(sb);
30932+}
30933+
30934+static inline int si_noflush_write_trylock(struct super_block *sb)
30935+{
076b876e
AM
30936+ int locked;
30937+
30938+ locked = __si_write_trylock(sb);
b752ccd1
AM
30939+ if (locked)
30940+ si_pid_set(sb);
30941+ return locked;
30942+}
30943+
7e9cd9fe 30944+#if 0 /* reserved */
1facf9fc 30945+static inline int si_read_trylock(struct super_block *sb, int flags)
30946+{
30947+ if (au_ftest_lock(flags, FLUSH))
30948+ au_nwt_flush(&au_sbi(sb)->si_nowait);
30949+ return si_noflush_read_trylock(sb);
30950+}
e49829fe 30951+#endif
1facf9fc 30952+
b752ccd1
AM
30953+static inline void si_read_unlock(struct super_block *sb)
30954+{
30955+ si_pid_clr(sb);
30956+ __si_read_unlock(sb);
30957+}
30958+
7e9cd9fe 30959+#if 0 /* reserved */
1facf9fc 30960+static inline int si_write_trylock(struct super_block *sb, int flags)
30961+{
30962+ if (au_ftest_lock(flags, FLUSH))
30963+ au_nwt_flush(&au_sbi(sb)->si_nowait);
30964+ return si_noflush_write_trylock(sb);
30965+}
b752ccd1
AM
30966+#endif
30967+
30968+static inline void si_write_unlock(struct super_block *sb)
30969+{
30970+ si_pid_clr(sb);
30971+ __si_write_unlock(sb);
30972+}
30973+
7e9cd9fe 30974+#if 0 /* reserved */
b752ccd1
AM
30975+static inline void si_downgrade_lock(struct super_block *sb)
30976+{
30977+ __si_downgrade_lock(sb);
30978+}
30979+#endif
1facf9fc 30980+
30981+/* ---------------------------------------------------------------------- */
30982+
5afbbe0d 30983+static inline aufs_bindex_t au_sbbot(struct super_block *sb)
1facf9fc 30984+{
dece6358 30985+ SiMustAnyLock(sb);
5afbbe0d 30986+ return au_sbi(sb)->si_bbot;
1facf9fc 30987+}
30988+
30989+static inline unsigned int au_mntflags(struct super_block *sb)
30990+{
dece6358 30991+ SiMustAnyLock(sb);
1facf9fc 30992+ return au_sbi(sb)->si_mntflags;
30993+}
30994+
30995+static inline unsigned int au_sigen(struct super_block *sb)
30996+{
dece6358 30997+ SiMustAnyLock(sb);
1facf9fc 30998+ return au_sbi(sb)->si_generation;
30999+}
31000+
31001+static inline struct au_branch *au_sbr(struct super_block *sb,
31002+ aufs_bindex_t bindex)
31003+{
dece6358 31004+ SiMustAnyLock(sb);
1facf9fc 31005+ return au_sbi(sb)->si_branch[0 + bindex];
31006+}
31007+
acd2b654 31008+static inline loff_t au_xi_maxent(struct super_block *sb)
1facf9fc 31009+{
dece6358 31010+ SiMustAnyLock(sb);
acd2b654 31011+ return au_sbi(sb)->si_ximaxent;
1facf9fc 31012+}
31013+
31014+#endif /* __KERNEL__ */
31015+#endif /* __AUFS_SUPER_H__ */
7f207e10 31016diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
eca34b5c 31017--- /usr/share/empty/fs/aufs/sysaufs.c 1970-01-01 01:00:00.000000000 +0100
46016270 31018+++ linux/fs/aufs/sysaufs.c 2021-02-24 13:33:42.747680497 +0100
062440b3 31019@@ -0,0 +1,93 @@
cd7a4cd9 31020+// SPDX-License-Identifier: GPL-2.0
1facf9fc 31021+/*
d58c55f2 31022+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 31023+ *
31024+ * This program, aufs is free software; you can redistribute it and/or modify
31025+ * it under the terms of the GNU General Public License as published by
31026+ * the Free Software Foundation; either version 2 of the License, or
31027+ * (at your option) any later version.
dece6358
AM
31028+ *
31029+ * This program is distributed in the hope that it will be useful,
31030+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31031+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31032+ * GNU General Public License for more details.
31033+ *
31034+ * You should have received a copy of the GNU General Public License
523b37e3 31035+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31036+ */
31037+
31038+/*
31039+ * sysfs interface and lifetime management
31040+ * they are necessary regardless sysfs is disabled.
31041+ */
31042+
1facf9fc 31043+#include <linux/random.h>
1facf9fc 31044+#include "aufs.h"
31045+
31046+unsigned long sysaufs_si_mask;
e49829fe 31047+struct kset *sysaufs_kset;
1facf9fc 31048+
31049+#define AuSiAttr(_name) { \
31050+ .attr = { .name = __stringify(_name), .mode = 0444 }, \
31051+ .show = sysaufs_si_##_name, \
31052+}
31053+
31054+static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
31055+struct attribute *sysaufs_si_attrs[] = {
31056+ &sysaufs_si_attr_xi_path.attr,
31057+ NULL,
31058+};
31059+
4a4d8108 31060+static const struct sysfs_ops au_sbi_ops = {
1facf9fc 31061+ .show = sysaufs_si_show
31062+};
31063+
31064+static struct kobj_type au_sbi_ktype = {
31065+ .release = au_si_free,
31066+ .sysfs_ops = &au_sbi_ops,
31067+ .default_attrs = sysaufs_si_attrs
31068+};
31069+
31070+/* ---------------------------------------------------------------------- */
31071+
31072+int sysaufs_si_init(struct au_sbinfo *sbinfo)
31073+{
31074+ int err;
31075+
e49829fe 31076+ sbinfo->si_kobj.kset = sysaufs_kset;
1facf9fc 31077+ /* cf. sysaufs_name() */
31078+ err = kobject_init_and_add
e49829fe 31079+ (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
1facf9fc 31080+ SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
31081+
1facf9fc 31082+ return err;
31083+}
31084+
31085+void sysaufs_fin(void)
31086+{
e49829fe
JR
31087+ sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31088+ kset_unregister(sysaufs_kset);
1facf9fc 31089+}
31090+
31091+int __init sysaufs_init(void)
31092+{
31093+ int err;
31094+
31095+ do {
31096+ get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
31097+ } while (!sysaufs_si_mask);
31098+
4a4d8108 31099+ err = -EINVAL;
e49829fe
JR
31100+ sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
31101+ if (unlikely(!sysaufs_kset))
4a4d8108 31102+ goto out;
e49829fe
JR
31103+ err = PTR_ERR(sysaufs_kset);
31104+ if (IS_ERR(sysaufs_kset))
1facf9fc 31105+ goto out;
e49829fe 31106+ err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
062440b3 31107+ if (unlikely(err))
e49829fe 31108+ kset_unregister(sysaufs_kset);
1facf9fc 31109+
4f0767ce 31110+out:
1facf9fc 31111+ return err;
31112+}
7f207e10 31113diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
eca34b5c 31114--- /usr/share/empty/fs/aufs/sysaufs.h 1970-01-01 01:00:00.000000000 +0100
46016270 31115+++ linux/fs/aufs/sysaufs.h 2021-02-24 13:33:42.747680497 +0100
062440b3
AM
31116@@ -0,0 +1,102 @@
31117+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 31118+/*
d58c55f2 31119+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 31120+ *
31121+ * This program, aufs is free software; you can redistribute it and/or modify
31122+ * it under the terms of the GNU General Public License as published by
31123+ * the Free Software Foundation; either version 2 of the License, or
31124+ * (at your option) any later version.
dece6358
AM
31125+ *
31126+ * This program is distributed in the hope that it will be useful,
31127+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31128+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31129+ * GNU General Public License for more details.
31130+ *
31131+ * You should have received a copy of the GNU General Public License
523b37e3 31132+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31133+ */
31134+
31135+/*
31136+ * sysfs interface and mount lifetime management
31137+ */
31138+
31139+#ifndef __SYSAUFS_H__
31140+#define __SYSAUFS_H__
31141+
31142+#ifdef __KERNEL__
31143+
1facf9fc 31144+#include <linux/sysfs.h>
1facf9fc 31145+#include "module.h"
31146+
dece6358
AM
31147+struct super_block;
31148+struct au_sbinfo;
31149+
1facf9fc 31150+struct sysaufs_si_attr {
31151+ struct attribute attr;
31152+ int (*show)(struct seq_file *seq, struct super_block *sb);
31153+};
31154+
31155+/* ---------------------------------------------------------------------- */
31156+
31157+/* sysaufs.c */
31158+extern unsigned long sysaufs_si_mask;
e49829fe 31159+extern struct kset *sysaufs_kset;
1facf9fc 31160+extern struct attribute *sysaufs_si_attrs[];
31161+int sysaufs_si_init(struct au_sbinfo *sbinfo);
31162+int __init sysaufs_init(void);
31163+void sysaufs_fin(void);
31164+
31165+/* ---------------------------------------------------------------------- */
31166+
31167+/* some people doesn't like to show a pointer in kernel */
31168+static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
31169+{
31170+ return sysaufs_si_mask ^ (unsigned long)sbinfo;
31171+}
31172+
31173+#define SysaufsSiNamePrefix "si_"
31174+#define SysaufsSiNameLen (sizeof(SysaufsSiNamePrefix) + 16)
31175+static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
31176+{
31177+ snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
31178+ sysaufs_si_id(sbinfo));
31179+}
31180+
31181+struct au_branch;
31182+#ifdef CONFIG_SYSFS
31183+/* sysfs.c */
31184+extern struct attribute_group *sysaufs_attr_group;
31185+
31186+int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
31187+ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31188+ char *buf);
076b876e
AM
31189+long au_brinfo_ioctl(struct file *file, unsigned long arg);
31190+#ifdef CONFIG_COMPAT
31191+long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
31192+#endif
1facf9fc 31193+
31194+void sysaufs_br_init(struct au_branch *br);
31195+void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
31196+void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
31197+
31198+#define sysaufs_brs_init() do {} while (0)
31199+
31200+#else
31201+#define sysaufs_attr_group NULL
31202+
4a4d8108 31203+AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
c1595e42
JR
31204+AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
31205+ struct attribute *attr, char *buf)
4a4d8108
AM
31206+AuStubVoid(sysaufs_br_init, struct au_branch *br)
31207+AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
31208+AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
1facf9fc 31209+
31210+static inline void sysaufs_brs_init(void)
31211+{
31212+ sysaufs_brs = 0;
31213+}
31214+
31215+#endif /* CONFIG_SYSFS */
31216+
31217+#endif /* __KERNEL__ */
31218+#endif /* __SYSAUFS_H__ */
7f207e10 31219diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
eca34b5c 31220--- /usr/share/empty/fs/aufs/sysfs.c 1970-01-01 01:00:00.000000000 +0100
46016270 31221+++ linux/fs/aufs/sysfs.c 2021-02-24 13:33:42.747680497 +0100
ba1aed25 31222@@ -0,0 +1,374 @@
cd7a4cd9 31223+// SPDX-License-Identifier: GPL-2.0
1facf9fc 31224+/*
d58c55f2 31225+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 31226+ *
31227+ * This program, aufs is free software; you can redistribute it and/or modify
31228+ * it under the terms of the GNU General Public License as published by
31229+ * the Free Software Foundation; either version 2 of the License, or
31230+ * (at your option) any later version.
dece6358
AM
31231+ *
31232+ * This program is distributed in the hope that it will be useful,
31233+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31234+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31235+ * GNU General Public License for more details.
31236+ *
31237+ * You should have received a copy of the GNU General Public License
523b37e3 31238+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31239+ */
31240+
31241+/*
31242+ * sysfs interface
31243+ */
31244+
076b876e 31245+#include <linux/compat.h>
1facf9fc 31246+#include <linux/seq_file.h>
1facf9fc 31247+#include "aufs.h"
31248+
4a4d8108
AM
31249+#ifdef CONFIG_AUFS_FS_MODULE
31250+/* this entry violates the "one line per file" policy of sysfs */
31251+static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
31252+ char *buf)
31253+{
31254+ ssize_t err;
31255+ static char *conf =
31256+/* this file is generated at compiling */
31257+#include "conf.str"
31258+ ;
31259+
31260+ err = snprintf(buf, PAGE_SIZE, conf);
31261+ if (unlikely(err >= PAGE_SIZE))
31262+ err = -EFBIG;
31263+ return err;
31264+}
31265+
31266+static struct kobj_attribute au_config_attr = __ATTR_RO(config);
31267+#endif
31268+
1facf9fc 31269+static struct attribute *au_attr[] = {
4a4d8108
AM
31270+#ifdef CONFIG_AUFS_FS_MODULE
31271+ &au_config_attr.attr,
31272+#endif
1facf9fc 31273+ NULL, /* need to NULL terminate the list of attributes */
31274+};
31275+
31276+static struct attribute_group sysaufs_attr_group_body = {
31277+ .attrs = au_attr
31278+};
31279+
31280+struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
31281+
31282+/* ---------------------------------------------------------------------- */
31283+
31284+int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
31285+{
31286+ int err;
31287+
dece6358
AM
31288+ SiMustAnyLock(sb);
31289+
1facf9fc 31290+ err = 0;
31291+ if (au_opt_test(au_mntflags(sb), XINO)) {
31292+ err = au_xino_path(seq, au_sbi(sb)->si_xib);
31293+ seq_putc(seq, '\n');
31294+ }
31295+ return err;
31296+}
31297+
31298+/*
31299+ * the lifetime of branch is independent from the entry under sysfs.
31300+ * sysfs handles the lifetime of the entry, and never call ->show() after it is
31301+ * unlinked.
31302+ */
31303+static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
392086de 31304+ aufs_bindex_t bindex, int idx)
1facf9fc 31305+{
1e00d052 31306+ int err;
1facf9fc 31307+ struct path path;
31308+ struct dentry *root;
31309+ struct au_branch *br;
076b876e 31310+ au_br_perm_str_t perm;
1facf9fc 31311+
31312+ AuDbg("b%d\n", bindex);
31313+
1e00d052 31314+ err = 0;
1facf9fc 31315+ root = sb->s_root;
31316+ di_read_lock_parent(root, !AuLock_IR);
31317+ br = au_sbr(sb, bindex);
392086de
AM
31318+
31319+ switch (idx) {
31320+ case AuBrSysfs_BR:
31321+ path.mnt = au_br_mnt(br);
31322+ path.dentry = au_h_dptr(root, bindex);
79b8bda9
AM
31323+ err = au_seq_path(seq, &path);
31324+ if (!err) {
31325+ au_optstr_br_perm(&perm, br->br_perm);
31326+ seq_printf(seq, "=%s\n", perm.a);
31327+ }
392086de
AM
31328+ break;
31329+ case AuBrSysfs_BRID:
79b8bda9 31330+ seq_printf(seq, "%d\n", br->br_id);
392086de
AM
31331+ break;
31332+ }
076b876e 31333+ di_read_unlock(root, !AuLock_IR);
79b8bda9 31334+ if (unlikely(err || seq_has_overflowed(seq)))
076b876e 31335+ err = -E2BIG;
392086de 31336+
1e00d052 31337+ return err;
1facf9fc 31338+}
31339+
31340+/* ---------------------------------------------------------------------- */
31341+
31342+static struct seq_file *au_seq(char *p, ssize_t len)
31343+{
31344+ struct seq_file *seq;
31345+
31346+ seq = kzalloc(sizeof(*seq), GFP_NOFS);
31347+ if (seq) {
31348+ /* mutex_init(&seq.lock); */
31349+ seq->buf = p;
31350+ seq->size = len;
31351+ return seq; /* success */
31352+ }
31353+
31354+ seq = ERR_PTR(-ENOMEM);
31355+ return seq;
31356+}
31357+
392086de
AM
31358+#define SysaufsBr_PREFIX "br"
31359+#define SysaufsBrid_PREFIX "brid"
1facf9fc 31360+
31361+/* todo: file size may exceed PAGE_SIZE */
31362+ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
1308ab2a 31363+ char *buf)
1facf9fc 31364+{
31365+ ssize_t err;
392086de 31366+ int idx;
1facf9fc 31367+ long l;
5afbbe0d 31368+ aufs_bindex_t bbot;
1facf9fc 31369+ struct au_sbinfo *sbinfo;
31370+ struct super_block *sb;
31371+ struct seq_file *seq;
31372+ char *name;
31373+ struct attribute **cattr;
31374+
31375+ sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
31376+ sb = sbinfo->si_sb;
1308ab2a 31377+
31378+ /*
31379+ * prevent a race condition between sysfs and aufs.
31380+ * for instance, sysfs_file_read() calls sysfs_get_active_two() which
31381+ * prohibits maintaining the sysfs entries.
31382+ * hew we acquire read lock after sysfs_get_active_two().
31383+ * on the other hand, the remount process may maintain the sysfs/aufs
31384+ * entries after acquiring write lock.
31385+ * it can cause a deadlock.
31386+ * simply we gave up processing read here.
31387+ */
31388+ err = -EBUSY;
31389+ if (unlikely(!si_noflush_read_trylock(sb)))
31390+ goto out;
1facf9fc 31391+
31392+ seq = au_seq(buf, PAGE_SIZE);
31393+ err = PTR_ERR(seq);
31394+ if (IS_ERR(seq))
1308ab2a 31395+ goto out_unlock;
1facf9fc 31396+
31397+ name = (void *)attr->name;
31398+ cattr = sysaufs_si_attrs;
31399+ while (*cattr) {
31400+ if (!strcmp(name, (*cattr)->name)) {
31401+ err = container_of(*cattr, struct sysaufs_si_attr, attr)
31402+ ->show(seq, sb);
31403+ goto out_seq;
31404+ }
31405+ cattr++;
31406+ }
31407+
392086de
AM
31408+ if (!strncmp(name, SysaufsBrid_PREFIX,
31409+ sizeof(SysaufsBrid_PREFIX) - 1)) {
31410+ idx = AuBrSysfs_BRID;
31411+ name += sizeof(SysaufsBrid_PREFIX) - 1;
31412+ } else if (!strncmp(name, SysaufsBr_PREFIX,
31413+ sizeof(SysaufsBr_PREFIX) - 1)) {
31414+ idx = AuBrSysfs_BR;
1facf9fc 31415+ name += sizeof(SysaufsBr_PREFIX) - 1;
392086de
AM
31416+ } else
31417+ BUG();
31418+
31419+ err = kstrtol(name, 10, &l);
31420+ if (!err) {
5afbbe0d
AM
31421+ bbot = au_sbbot(sb);
31422+ if (l <= bbot)
392086de
AM
31423+ err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
31424+ else
31425+ err = -ENOENT;
1facf9fc 31426+ }
1facf9fc 31427+
4f0767ce 31428+out_seq:
1facf9fc 31429+ if (!err) {
31430+ err = seq->count;
31431+ /* sysfs limit */
31432+ if (unlikely(err == PAGE_SIZE))
31433+ err = -EFBIG;
31434+ }
9f237c51 31435+ au_kfree_rcu(seq);
4f0767ce 31436+out_unlock:
1facf9fc 31437+ si_read_unlock(sb);
4f0767ce 31438+out:
1facf9fc 31439+ return err;
31440+}
31441+
31442+/* ---------------------------------------------------------------------- */
31443+
076b876e
AM
31444+static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
31445+{
31446+ int err;
31447+ int16_t brid;
5afbbe0d 31448+ aufs_bindex_t bindex, bbot;
076b876e
AM
31449+ size_t sz;
31450+ char *buf;
31451+ struct seq_file *seq;
31452+ struct au_branch *br;
31453+
31454+ si_read_lock(sb, AuLock_FLUSH);
5afbbe0d
AM
31455+ bbot = au_sbbot(sb);
31456+ err = bbot + 1;
076b876e
AM
31457+ if (!arg)
31458+ goto out;
31459+
31460+ err = -ENOMEM;
31461+ buf = (void *)__get_free_page(GFP_NOFS);
31462+ if (unlikely(!buf))
31463+ goto out;
31464+
31465+ seq = au_seq(buf, PAGE_SIZE);
31466+ err = PTR_ERR(seq);
31467+ if (IS_ERR(seq))
31468+ goto out_buf;
31469+
31470+ sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
5afbbe0d 31471+ for (bindex = 0; bindex <= bbot; bindex++, arg++) {
ba1aed25
AM
31472+ /* VERIFY_WRITE */
31473+ err = !access_ok(arg, sizeof(*arg));
076b876e
AM
31474+ if (unlikely(err))
31475+ break;
31476+
31477+ br = au_sbr(sb, bindex);
31478+ brid = br->br_id;
31479+ BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
31480+ err = __put_user(brid, &arg->id);
31481+ if (unlikely(err))
31482+ break;
31483+
31484+ BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
31485+ err = __put_user(br->br_perm, &arg->perm);
31486+ if (unlikely(err))
31487+ break;
31488+
79b8bda9
AM
31489+ err = au_seq_path(seq, &br->br_path);
31490+ if (unlikely(err))
31491+ break;
31492+ seq_putc(seq, '\0');
31493+ if (!seq_has_overflowed(seq)) {
076b876e
AM
31494+ err = copy_to_user(arg->path, seq->buf, seq->count);
31495+ seq->count = 0;
31496+ if (unlikely(err))
31497+ break;
31498+ } else {
31499+ err = -E2BIG;
31500+ goto out_seq;
31501+ }
31502+ }
31503+ if (unlikely(err))
31504+ err = -EFAULT;
31505+
31506+out_seq:
9f237c51 31507+ au_kfree_rcu(seq);
076b876e 31508+out_buf:
1c60b727 31509+ free_page((unsigned long)buf);
076b876e
AM
31510+out:
31511+ si_read_unlock(sb);
31512+ return err;
31513+}
31514+
31515+long au_brinfo_ioctl(struct file *file, unsigned long arg)
31516+{
2000de60 31517+ return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
076b876e
AM
31518+}
31519+
31520+#ifdef CONFIG_COMPAT
31521+long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
31522+{
2000de60 31523+ return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
076b876e
AM
31524+}
31525+#endif
31526+
31527+/* ---------------------------------------------------------------------- */
31528+
1facf9fc 31529+void sysaufs_br_init(struct au_branch *br)
31530+{
392086de
AM
31531+ int i;
31532+ struct au_brsysfs *br_sysfs;
31533+ struct attribute *attr;
4a4d8108 31534+
392086de
AM
31535+ br_sysfs = br->br_sysfs;
31536+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31537+ attr = &br_sysfs->attr;
31538+ sysfs_attr_init(attr);
31539+ attr->name = br_sysfs->name;
cd7a4cd9 31540+ attr->mode = 0444;
392086de
AM
31541+ br_sysfs++;
31542+ }
1facf9fc 31543+}
31544+
31545+void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
31546+{
31547+ struct au_branch *br;
31548+ struct kobject *kobj;
392086de
AM
31549+ struct au_brsysfs *br_sysfs;
31550+ int i;
5afbbe0d 31551+ aufs_bindex_t bbot;
1facf9fc 31552+
1facf9fc 31553+ if (!sysaufs_brs)
31554+ return;
31555+
31556+ kobj = &au_sbi(sb)->si_kobj;
5afbbe0d
AM
31557+ bbot = au_sbbot(sb);
31558+ for (; bindex <= bbot; bindex++) {
1facf9fc 31559+ br = au_sbr(sb, bindex);
392086de
AM
31560+ br_sysfs = br->br_sysfs;
31561+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31562+ sysfs_remove_file(kobj, &br_sysfs->attr);
31563+ br_sysfs++;
31564+ }
1facf9fc 31565+ }
31566+}
31567+
31568+void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
31569+{
392086de 31570+ int err, i;
5afbbe0d 31571+ aufs_bindex_t bbot;
1facf9fc 31572+ struct kobject *kobj;
31573+ struct au_branch *br;
392086de 31574+ struct au_brsysfs *br_sysfs;
1facf9fc 31575+
1facf9fc 31576+ if (!sysaufs_brs)
31577+ return;
31578+
31579+ kobj = &au_sbi(sb)->si_kobj;
5afbbe0d
AM
31580+ bbot = au_sbbot(sb);
31581+ for (; bindex <= bbot; bindex++) {
1facf9fc 31582+ br = au_sbr(sb, bindex);
392086de
AM
31583+ br_sysfs = br->br_sysfs;
31584+ snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
31585+ SysaufsBr_PREFIX "%d", bindex);
31586+ snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
31587+ SysaufsBrid_PREFIX "%d", bindex);
31588+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31589+ err = sysfs_create_file(kobj, &br_sysfs->attr);
31590+ if (unlikely(err))
31591+ pr_warn("failed %s under sysfs(%d)\n",
31592+ br_sysfs->name, err);
31593+ br_sysfs++;
31594+ }
1facf9fc 31595+ }
31596+}
7f207e10 31597diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
eca34b5c 31598--- /usr/share/empty/fs/aufs/sysrq.c 1970-01-01 01:00:00.000000000 +0100
46016270 31599+++ linux/fs/aufs/sysrq.c 2021-02-24 13:33:42.747680497 +0100
43982f53 31600@@ -0,0 +1,149 @@
cd7a4cd9 31601+// SPDX-License-Identifier: GPL-2.0
1facf9fc 31602+/*
d58c55f2 31603+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 31604+ *
31605+ * This program, aufs is free software; you can redistribute it and/or modify
31606+ * it under the terms of the GNU General Public License as published by
31607+ * the Free Software Foundation; either version 2 of the License, or
31608+ * (at your option) any later version.
dece6358
AM
31609+ *
31610+ * This program is distributed in the hope that it will be useful,
31611+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31612+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31613+ * GNU General Public License for more details.
31614+ *
31615+ * You should have received a copy of the GNU General Public License
523b37e3 31616+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31617+ */
31618+
31619+/*
acd2b654 31620+ * magic sysrq handler
1facf9fc 31621+ */
31622+
1facf9fc 31623+/* #include <linux/sysrq.h> */
027c5e7a 31624+#include <linux/writeback.h>
1facf9fc 31625+#include "aufs.h"
31626+
31627+/* ---------------------------------------------------------------------- */
31628+
31629+static void sysrq_sb(struct super_block *sb)
31630+{
31631+ char *plevel;
31632+ struct au_sbinfo *sbinfo;
31633+ struct file *file;
8b6a4947
AM
31634+ struct hlist_bl_head *files;
31635+ struct hlist_bl_node *pos;
523b37e3 31636+ struct au_finfo *finfo;
43982f53 31637+ struct inode *i;
1facf9fc 31638+
31639+ plevel = au_plevel;
31640+ au_plevel = KERN_WARNING;
1facf9fc 31641+
4a4d8108 31642+ /* since we define pr_fmt, call printk directly */
c06a8ce3
AM
31643+#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
31644+
31645+ sbinfo = au_sbi(sb);
4a4d8108 31646+ printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
c06a8ce3 31647+ pr("superblock\n");
1facf9fc 31648+ au_dpri_sb(sb);
027c5e7a 31649+
43982f53 31650+#if 0 /* reserved */
027c5e7a
AM
31651+ do {
31652+ int err, i, j, ndentry;
31653+ struct au_dcsub_pages dpages;
31654+ struct au_dpage *dpage;
31655+
31656+ err = au_dpages_init(&dpages, GFP_ATOMIC);
31657+ if (unlikely(err))
31658+ break;
31659+ err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
31660+ if (!err)
31661+ for (i = 0; i < dpages.ndpage; i++) {
31662+ dpage = dpages.dpages + i;
31663+ ndentry = dpage->ndentry;
31664+ for (j = 0; j < ndentry; j++)
31665+ au_dpri_dentry(dpage->dentries[j]);
31666+ }
31667+ au_dpages_free(&dpages);
31668+ } while (0);
31669+#endif
31670+
43982f53
AM
31671+ pr("isolated inode\n");
31672+ spin_lock(&sb->s_inode_list_lock);
31673+ list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
31674+ spin_lock(&i->i_lock);
31675+ if (hlist_empty(&i->i_dentry))
31676+ au_dpri_inode(i);
31677+ spin_unlock(&i->i_lock);
027c5e7a 31678+ }
43982f53
AM
31679+ spin_unlock(&sb->s_inode_list_lock);
31680+
c06a8ce3 31681+ pr("files\n");
523b37e3 31682+ files = &au_sbi(sb)->si_files;
8b6a4947
AM
31683+ hlist_bl_lock(files);
31684+ hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
4a4d8108 31685+ umode_t mode;
076b876e 31686+
523b37e3 31687+ file = finfo->fi_file;
c06a8ce3 31688+ mode = file_inode(file)->i_mode;
38d290e6 31689+ if (!special_file(mode))
1facf9fc 31690+ au_dpri_file(file);
523b37e3 31691+ }
8b6a4947 31692+ hlist_bl_unlock(files);
c06a8ce3 31693+ pr("done\n");
1facf9fc 31694+
c06a8ce3 31695+#undef pr
1facf9fc 31696+ au_plevel = plevel;
1facf9fc 31697+}
31698+
31699+/* ---------------------------------------------------------------------- */
31700+
31701+/* module parameter */
31702+static char *aufs_sysrq_key = "a";
cd7a4cd9 31703+module_param_named(sysrq, aufs_sysrq_key, charp, 0444);
1facf9fc 31704+MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
31705+
0c5527e5 31706+static void au_sysrq(int key __maybe_unused)
1facf9fc 31707+{
1facf9fc 31708+ struct au_sbinfo *sbinfo;
8b6a4947 31709+ struct hlist_bl_node *pos;
1facf9fc 31710+
027c5e7a 31711+ lockdep_off();
53392da6 31712+ au_sbilist_lock();
8b6a4947 31713+ hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
1facf9fc 31714+ sysrq_sb(sbinfo->si_sb);
53392da6 31715+ au_sbilist_unlock();
027c5e7a 31716+ lockdep_on();
1facf9fc 31717+}
31718+
31719+static struct sysrq_key_op au_sysrq_op = {
31720+ .handler = au_sysrq,
31721+ .help_msg = "Aufs",
31722+ .action_msg = "Aufs",
31723+ .enable_mask = SYSRQ_ENABLE_DUMP
31724+};
31725+
31726+/* ---------------------------------------------------------------------- */
31727+
31728+int __init au_sysrq_init(void)
31729+{
31730+ int err;
31731+ char key;
31732+
31733+ err = -1;
31734+ key = *aufs_sysrq_key;
31735+ if ('a' <= key && key <= 'z')
31736+ err = register_sysrq_key(key, &au_sysrq_op);
31737+ if (unlikely(err))
4a4d8108 31738+ pr_err("err %d, sysrq=%c\n", err, key);
1facf9fc 31739+ return err;
31740+}
31741+
31742+void au_sysrq_fin(void)
31743+{
31744+ int err;
076b876e 31745+
1facf9fc 31746+ err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
31747+ if (unlikely(err))
4a4d8108 31748+ pr_err("err %d (ignored)\n", err);
1facf9fc 31749+}
7f207e10 31750diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
eca34b5c 31751--- /usr/share/empty/fs/aufs/vdir.c 1970-01-01 01:00:00.000000000 +0100
46016270 31752+++ linux/fs/aufs/vdir.c 2021-02-24 13:33:42.747680497 +0100
eca801bf 31753@@ -0,0 +1,896 @@
cd7a4cd9 31754+// SPDX-License-Identifier: GPL-2.0
1facf9fc 31755+/*
d58c55f2 31756+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 31757+ *
31758+ * This program, aufs is free software; you can redistribute it and/or modify
31759+ * it under the terms of the GNU General Public License as published by
31760+ * the Free Software Foundation; either version 2 of the License, or
31761+ * (at your option) any later version.
dece6358
AM
31762+ *
31763+ * This program is distributed in the hope that it will be useful,
31764+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31765+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31766+ * GNU General Public License for more details.
31767+ *
31768+ * You should have received a copy of the GNU General Public License
523b37e3 31769+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31770+ */
31771+
31772+/*
31773+ * virtual or vertical directory
31774+ */
31775+
eca801bf 31776+#include <linux/iversion.h>
1facf9fc 31777+#include "aufs.h"
31778+
dece6358 31779+static unsigned int calc_size(int nlen)
1facf9fc 31780+{
dece6358 31781+ return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
1facf9fc 31782+}
31783+
31784+static int set_deblk_end(union au_vdir_deblk_p *p,
31785+ union au_vdir_deblk_p *deblk_end)
31786+{
31787+ if (calc_size(0) <= deblk_end->deblk - p->deblk) {
31788+ p->de->de_str.len = 0;
31789+ /* smp_mb(); */
31790+ return 0;
31791+ }
31792+ return -1; /* error */
31793+}
31794+
31795+/* returns true or false */
31796+static int is_deblk_end(union au_vdir_deblk_p *p,
31797+ union au_vdir_deblk_p *deblk_end)
31798+{
31799+ if (calc_size(0) <= deblk_end->deblk - p->deblk)
31800+ return !p->de->de_str.len;
31801+ return 1;
31802+}
31803+
31804+static unsigned char *last_deblk(struct au_vdir *vdir)
31805+{
31806+ return vdir->vd_deblk[vdir->vd_nblk - 1];
31807+}
31808+
31809+/* ---------------------------------------------------------------------- */
31810+
79b8bda9 31811+/* estimate the appropriate size for name hash table */
1308ab2a 31812+unsigned int au_rdhash_est(loff_t sz)
31813+{
31814+ unsigned int n;
31815+
31816+ n = UINT_MAX;
31817+ sz >>= 10;
31818+ if (sz < n)
31819+ n = sz;
31820+ if (sz < AUFS_RDHASH_DEF)
31821+ n = AUFS_RDHASH_DEF;
4a4d8108 31822+ /* pr_info("n %u\n", n); */
1308ab2a 31823+ return n;
31824+}
31825+
1facf9fc 31826+/*
31827+ * the allocated memory has to be freed by
dece6358 31828+ * au_nhash_wh_free() or au_nhash_de_free().
1facf9fc 31829+ */
dece6358 31830+int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
1facf9fc 31831+{
1facf9fc 31832+ struct hlist_head *head;
dece6358 31833+ unsigned int u;
076b876e 31834+ size_t sz;
1facf9fc 31835+
076b876e
AM
31836+ sz = sizeof(*nhash->nh_head) * num_hash;
31837+ head = kmalloc(sz, gfp);
dece6358
AM
31838+ if (head) {
31839+ nhash->nh_num = num_hash;
31840+ nhash->nh_head = head;
31841+ for (u = 0; u < num_hash; u++)
1facf9fc 31842+ INIT_HLIST_HEAD(head++);
dece6358 31843+ return 0; /* success */
1facf9fc 31844+ }
1facf9fc 31845+
dece6358 31846+ return -ENOMEM;
1facf9fc 31847+}
31848+
dece6358
AM
31849+static void nhash_count(struct hlist_head *head)
31850+{
43982f53 31851+#if 0 /* debugging */
dece6358
AM
31852+ unsigned long n;
31853+ struct hlist_node *pos;
31854+
31855+ n = 0;
31856+ hlist_for_each(pos, head)
31857+ n++;
4a4d8108 31858+ pr_info("%lu\n", n);
dece6358
AM
31859+#endif
31860+}
31861+
31862+static void au_nhash_wh_do_free(struct hlist_head *head)
1facf9fc 31863+{
c06a8ce3
AM
31864+ struct au_vdir_wh *pos;
31865+ struct hlist_node *node;
1facf9fc 31866+
c06a8ce3 31867+ hlist_for_each_entry_safe(pos, node, head, wh_hash)
9f237c51 31868+ au_kfree_rcu(pos);
1facf9fc 31869+}
31870+
dece6358 31871+static void au_nhash_de_do_free(struct hlist_head *head)
1facf9fc 31872+{
c06a8ce3
AM
31873+ struct au_vdir_dehstr *pos;
31874+ struct hlist_node *node;
1facf9fc 31875+
c06a8ce3 31876+ hlist_for_each_entry_safe(pos, node, head, hash)
1c60b727 31877+ au_cache_free_vdir_dehstr(pos);
1facf9fc 31878+}
31879+
dece6358
AM
31880+static void au_nhash_do_free(struct au_nhash *nhash,
31881+ void (*free)(struct hlist_head *head))
1facf9fc 31882+{
1308ab2a 31883+ unsigned int n;
1facf9fc 31884+ struct hlist_head *head;
1facf9fc 31885+
dece6358 31886+ n = nhash->nh_num;
1308ab2a 31887+ if (!n)
31888+ return;
31889+
dece6358 31890+ head = nhash->nh_head;
1308ab2a 31891+ while (n-- > 0) {
dece6358
AM
31892+ nhash_count(head);
31893+ free(head++);
1facf9fc 31894+ }
9f237c51 31895+ au_kfree_try_rcu(nhash->nh_head);
1facf9fc 31896+}
31897+
dece6358 31898+void au_nhash_wh_free(struct au_nhash *whlist)
1facf9fc 31899+{
dece6358
AM
31900+ au_nhash_do_free(whlist, au_nhash_wh_do_free);
31901+}
1facf9fc 31902+
dece6358
AM
31903+static void au_nhash_de_free(struct au_nhash *delist)
31904+{
31905+ au_nhash_do_free(delist, au_nhash_de_do_free);
1facf9fc 31906+}
31907+
31908+/* ---------------------------------------------------------------------- */
31909+
31910+int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
31911+ int limit)
31912+{
31913+ int num;
31914+ unsigned int u, n;
31915+ struct hlist_head *head;
c06a8ce3 31916+ struct au_vdir_wh *pos;
1facf9fc 31917+
31918+ num = 0;
31919+ n = whlist->nh_num;
31920+ head = whlist->nh_head;
1308ab2a 31921+ for (u = 0; u < n; u++, head++)
c06a8ce3
AM
31922+ hlist_for_each_entry(pos, head, wh_hash)
31923+ if (pos->wh_bindex == btgt && ++num > limit)
1facf9fc 31924+ return 1;
1facf9fc 31925+ return 0;
31926+}
31927+
31928+static struct hlist_head *au_name_hash(struct au_nhash *nhash,
dece6358 31929+ unsigned char *name,
1facf9fc 31930+ unsigned int len)
31931+{
dece6358
AM
31932+ unsigned int v;
31933+ /* const unsigned int magic_bit = 12; */
31934+
1308ab2a 31935+ AuDebugOn(!nhash->nh_num || !nhash->nh_head);
31936+
dece6358 31937+ v = 0;
f0c0a007
AM
31938+ if (len > 8)
31939+ len = 8;
dece6358
AM
31940+ while (len--)
31941+ v += *name++;
31942+ /* v = hash_long(v, magic_bit); */
31943+ v %= nhash->nh_num;
31944+ return nhash->nh_head + v;
31945+}
31946+
31947+static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
31948+ int nlen)
31949+{
31950+ return str->len == nlen && !memcmp(str->name, name, nlen);
1facf9fc 31951+}
31952+
31953+/* returns found or not */
dece6358 31954+int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
1facf9fc 31955+{
31956+ struct hlist_head *head;
c06a8ce3 31957+ struct au_vdir_wh *pos;
1facf9fc 31958+ struct au_vdir_destr *str;
31959+
dece6358 31960+ head = au_name_hash(whlist, name, nlen);
c06a8ce3
AM
31961+ hlist_for_each_entry(pos, head, wh_hash) {
31962+ str = &pos->wh_str;
1facf9fc 31963+ AuDbg("%.*s\n", str->len, str->name);
dece6358
AM
31964+ if (au_nhash_test_name(str, name, nlen))
31965+ return 1;
31966+ }
31967+ return 0;
31968+}
31969+
31970+/* returns found(true) or not */
31971+static int test_known(struct au_nhash *delist, char *name, int nlen)
31972+{
31973+ struct hlist_head *head;
c06a8ce3 31974+ struct au_vdir_dehstr *pos;
dece6358
AM
31975+ struct au_vdir_destr *str;
31976+
31977+ head = au_name_hash(delist, name, nlen);
c06a8ce3
AM
31978+ hlist_for_each_entry(pos, head, hash) {
31979+ str = pos->str;
dece6358
AM
31980+ AuDbg("%.*s\n", str->len, str->name);
31981+ if (au_nhash_test_name(str, name, nlen))
1facf9fc 31982+ return 1;
31983+ }
31984+ return 0;
31985+}
31986+
dece6358
AM
31987+static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
31988+ unsigned char d_type)
31989+{
31990+#ifdef CONFIG_AUFS_SHWH
31991+ wh->wh_ino = ino;
31992+ wh->wh_type = d_type;
31993+#endif
31994+}
31995+
31996+/* ---------------------------------------------------------------------- */
31997+
31998+int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
31999+ unsigned int d_type, aufs_bindex_t bindex,
32000+ unsigned char shwh)
1facf9fc 32001+{
32002+ int err;
32003+ struct au_vdir_destr *str;
32004+ struct au_vdir_wh *wh;
32005+
dece6358 32006+ AuDbg("%.*s\n", nlen, name);
1308ab2a 32007+ AuDebugOn(!whlist->nh_num || !whlist->nh_head);
32008+
1facf9fc 32009+ err = -ENOMEM;
dece6358 32010+ wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
1facf9fc 32011+ if (unlikely(!wh))
32012+ goto out;
32013+
32014+ err = 0;
32015+ wh->wh_bindex = bindex;
dece6358
AM
32016+ if (shwh)
32017+ au_shwh_init_wh(wh, ino, d_type);
1facf9fc 32018+ str = &wh->wh_str;
dece6358
AM
32019+ str->len = nlen;
32020+ memcpy(str->name, name, nlen);
32021+ hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
1facf9fc 32022+ /* smp_mb(); */
32023+
4f0767ce 32024+out:
1facf9fc 32025+ return err;
32026+}
32027+
1facf9fc 32028+static int append_deblk(struct au_vdir *vdir)
32029+{
32030+ int err;
dece6358 32031+ unsigned long ul;
1facf9fc 32032+ const unsigned int deblk_sz = vdir->vd_deblk_sz;
32033+ union au_vdir_deblk_p p, deblk_end;
32034+ unsigned char **o;
32035+
32036+ err = -ENOMEM;
e2f27e51
AM
32037+ o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
32038+ GFP_NOFS, /*may_shrink*/0);
1facf9fc 32039+ if (unlikely(!o))
32040+ goto out;
32041+
32042+ vdir->vd_deblk = o;
32043+ p.deblk = kmalloc(deblk_sz, GFP_NOFS);
32044+ if (p.deblk) {
32045+ ul = vdir->vd_nblk++;
32046+ vdir->vd_deblk[ul] = p.deblk;
32047+ vdir->vd_last.ul = ul;
32048+ vdir->vd_last.p.deblk = p.deblk;
32049+ deblk_end.deblk = p.deblk + deblk_sz;
32050+ err = set_deblk_end(&p, &deblk_end);
32051+ }
32052+
4f0767ce 32053+out:
1facf9fc 32054+ return err;
32055+}
32056+
dece6358
AM
32057+static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
32058+ unsigned int d_type, struct au_nhash *delist)
32059+{
32060+ int err;
32061+ unsigned int sz;
32062+ const unsigned int deblk_sz = vdir->vd_deblk_sz;
32063+ union au_vdir_deblk_p p, *room, deblk_end;
32064+ struct au_vdir_dehstr *dehstr;
32065+
32066+ p.deblk = last_deblk(vdir);
32067+ deblk_end.deblk = p.deblk + deblk_sz;
32068+ room = &vdir->vd_last.p;
32069+ AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
32070+ || !is_deblk_end(room, &deblk_end));
32071+
32072+ sz = calc_size(nlen);
32073+ if (unlikely(sz > deblk_end.deblk - room->deblk)) {
32074+ err = append_deblk(vdir);
32075+ if (unlikely(err))
32076+ goto out;
32077+
32078+ p.deblk = last_deblk(vdir);
32079+ deblk_end.deblk = p.deblk + deblk_sz;
32080+ /* smp_mb(); */
32081+ AuDebugOn(room->deblk != p.deblk);
32082+ }
32083+
32084+ err = -ENOMEM;
4a4d8108 32085+ dehstr = au_cache_alloc_vdir_dehstr();
dece6358
AM
32086+ if (unlikely(!dehstr))
32087+ goto out;
32088+
32089+ dehstr->str = &room->de->de_str;
32090+ hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
32091+ room->de->de_ino = ino;
32092+ room->de->de_type = d_type;
32093+ room->de->de_str.len = nlen;
32094+ memcpy(room->de->de_str.name, name, nlen);
32095+
32096+ err = 0;
32097+ room->deblk += sz;
32098+ if (unlikely(set_deblk_end(room, &deblk_end)))
32099+ err = append_deblk(vdir);
32100+ /* smp_mb(); */
32101+
4f0767ce 32102+out:
dece6358
AM
32103+ return err;
32104+}
32105+
32106+/* ---------------------------------------------------------------------- */
32107+
1c60b727 32108+void au_vdir_free(struct au_vdir *vdir)
dece6358
AM
32109+{
32110+ unsigned char **deblk;
32111+
32112+ deblk = vdir->vd_deblk;
1c60b727 32113+ while (vdir->vd_nblk--)
9f237c51
AM
32114+ au_kfree_try_rcu(*deblk++);
32115+ au_kfree_try_rcu(vdir->vd_deblk);
1c60b727 32116+ au_cache_free_vdir(vdir);
dece6358
AM
32117+}
32118+
1308ab2a 32119+static struct au_vdir *alloc_vdir(struct file *file)
1facf9fc 32120+{
32121+ struct au_vdir *vdir;
1308ab2a 32122+ struct super_block *sb;
1facf9fc 32123+ int err;
32124+
2000de60 32125+ sb = file->f_path.dentry->d_sb;
dece6358
AM
32126+ SiMustAnyLock(sb);
32127+
1facf9fc 32128+ err = -ENOMEM;
32129+ vdir = au_cache_alloc_vdir();
32130+ if (unlikely(!vdir))
32131+ goto out;
32132+
32133+ vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
32134+ if (unlikely(!vdir->vd_deblk))
32135+ goto out_free;
32136+
32137+ vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
1308ab2a 32138+ if (!vdir->vd_deblk_sz) {
79b8bda9 32139+ /* estimate the appropriate size for deblk */
1308ab2a 32140+ vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
4a4d8108 32141+ /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
1308ab2a 32142+ }
1facf9fc 32143+ vdir->vd_nblk = 0;
32144+ vdir->vd_version = 0;
32145+ vdir->vd_jiffy = 0;
32146+ err = append_deblk(vdir);
32147+ if (!err)
32148+ return vdir; /* success */
32149+
9f237c51 32150+ au_kfree_try_rcu(vdir->vd_deblk);
1facf9fc 32151+
4f0767ce 32152+out_free:
1c60b727 32153+ au_cache_free_vdir(vdir);
4f0767ce 32154+out:
1facf9fc 32155+ vdir = ERR_PTR(err);
32156+ return vdir;
32157+}
32158+
32159+static int reinit_vdir(struct au_vdir *vdir)
32160+{
32161+ int err;
32162+ union au_vdir_deblk_p p, deblk_end;
32163+
32164+ while (vdir->vd_nblk > 1) {
9f237c51 32165+ au_kfree_try_rcu(vdir->vd_deblk[vdir->vd_nblk - 1]);
1facf9fc 32166+ /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
32167+ vdir->vd_nblk--;
32168+ }
32169+ p.deblk = vdir->vd_deblk[0];
32170+ deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
32171+ err = set_deblk_end(&p, &deblk_end);
32172+ /* keep vd_dblk_sz */
32173+ vdir->vd_last.ul = 0;
32174+ vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32175+ vdir->vd_version = 0;
32176+ vdir->vd_jiffy = 0;
32177+ /* smp_mb(); */
32178+ return err;
32179+}
32180+
32181+/* ---------------------------------------------------------------------- */
32182+
1facf9fc 32183+#define AuFillVdir_CALLED 1
32184+#define AuFillVdir_WHABLE (1 << 1)
dece6358 32185+#define AuFillVdir_SHWH (1 << 2)
1facf9fc 32186+#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
7f207e10
AM
32187+#define au_fset_fillvdir(flags, name) \
32188+ do { (flags) |= AuFillVdir_##name; } while (0)
32189+#define au_fclr_fillvdir(flags, name) \
32190+ do { (flags) &= ~AuFillVdir_##name; } while (0)
1facf9fc 32191+
dece6358
AM
32192+#ifndef CONFIG_AUFS_SHWH
32193+#undef AuFillVdir_SHWH
32194+#define AuFillVdir_SHWH 0
32195+#endif
32196+
1facf9fc 32197+struct fillvdir_arg {
392086de 32198+ struct dir_context ctx;
1facf9fc 32199+ struct file *file;
32200+ struct au_vdir *vdir;
dece6358
AM
32201+ struct au_nhash delist;
32202+ struct au_nhash whlist;
1facf9fc 32203+ aufs_bindex_t bindex;
32204+ unsigned int flags;
32205+ int err;
32206+};
32207+
392086de 32208+static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
1facf9fc 32209+ loff_t offset __maybe_unused, u64 h_ino,
32210+ unsigned int d_type)
32211+{
392086de 32212+ struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
1facf9fc 32213+ char *name = (void *)__name;
32214+ struct super_block *sb;
1facf9fc 32215+ ino_t ino;
dece6358 32216+ const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
1facf9fc 32217+
1facf9fc 32218+ arg->err = 0;
2000de60 32219+ sb = arg->file->f_path.dentry->d_sb;
1facf9fc 32220+ au_fset_fillvdir(arg->flags, CALLED);
32221+ /* smp_mb(); */
dece6358 32222+ if (nlen <= AUFS_WH_PFX_LEN
1facf9fc 32223+ || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
dece6358
AM
32224+ if (test_known(&arg->delist, name, nlen)
32225+ || au_nhash_test_known_wh(&arg->whlist, name, nlen))
32226+ goto out; /* already exists or whiteouted */
1facf9fc 32227+
dece6358 32228+ arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
4a4d8108
AM
32229+ if (!arg->err) {
32230+ if (unlikely(nlen > AUFS_MAX_NAMELEN))
32231+ d_type = DT_UNKNOWN;
dece6358
AM
32232+ arg->err = append_de(arg->vdir, name, nlen, ino,
32233+ d_type, &arg->delist);
4a4d8108 32234+ }
1facf9fc 32235+ } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
32236+ name += AUFS_WH_PFX_LEN;
dece6358
AM
32237+ nlen -= AUFS_WH_PFX_LEN;
32238+ if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
32239+ goto out; /* already whiteouted */
1facf9fc 32240+
acd2b654 32241+ ino = 0; /* just to suppress a warning */
dece6358
AM
32242+ if (shwh)
32243+ arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
32244+ &ino);
4a4d8108
AM
32245+ if (!arg->err) {
32246+ if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
32247+ d_type = DT_UNKNOWN;
1facf9fc 32248+ arg->err = au_nhash_append_wh
dece6358
AM
32249+ (&arg->whlist, name, nlen, ino, d_type,
32250+ arg->bindex, shwh);
4a4d8108 32251+ }
1facf9fc 32252+ }
32253+
4f0767ce 32254+out:
1facf9fc 32255+ if (!arg->err)
32256+ arg->vdir->vd_jiffy = jiffies;
32257+ /* smp_mb(); */
32258+ AuTraceErr(arg->err);
32259+ return arg->err;
32260+}
32261+
dece6358
AM
32262+static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
32263+ struct au_nhash *whlist, struct au_nhash *delist)
32264+{
32265+#ifdef CONFIG_AUFS_SHWH
32266+ int err;
32267+ unsigned int nh, u;
32268+ struct hlist_head *head;
c06a8ce3
AM
32269+ struct au_vdir_wh *pos;
32270+ struct hlist_node *n;
dece6358
AM
32271+ char *p, *o;
32272+ struct au_vdir_destr *destr;
32273+
32274+ AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
32275+
32276+ err = -ENOMEM;
537831f9 32277+ o = p = (void *)__get_free_page(GFP_NOFS);
dece6358
AM
32278+ if (unlikely(!p))
32279+ goto out;
32280+
32281+ err = 0;
32282+ nh = whlist->nh_num;
32283+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32284+ p += AUFS_WH_PFX_LEN;
32285+ for (u = 0; u < nh; u++) {
32286+ head = whlist->nh_head + u;
c06a8ce3
AM
32287+ hlist_for_each_entry_safe(pos, n, head, wh_hash) {
32288+ destr = &pos->wh_str;
dece6358
AM
32289+ memcpy(p, destr->name, destr->len);
32290+ err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
c06a8ce3 32291+ pos->wh_ino, pos->wh_type, delist);
dece6358
AM
32292+ if (unlikely(err))
32293+ break;
32294+ }
32295+ }
32296+
1c60b727 32297+ free_page((unsigned long)o);
dece6358 32298+
4f0767ce 32299+out:
dece6358
AM
32300+ AuTraceErr(err);
32301+ return err;
32302+#else
32303+ return 0;
32304+#endif
32305+}
32306+
1facf9fc 32307+static int au_do_read_vdir(struct fillvdir_arg *arg)
32308+{
32309+ int err;
dece6358 32310+ unsigned int rdhash;
1facf9fc 32311+ loff_t offset;
5afbbe0d 32312+ aufs_bindex_t bbot, bindex, btop;
dece6358 32313+ unsigned char shwh;
1facf9fc 32314+ struct file *hf, *file;
32315+ struct super_block *sb;
32316+
1facf9fc 32317+ file = arg->file;
2000de60 32318+ sb = file->f_path.dentry->d_sb;
dece6358
AM
32319+ SiMustAnyLock(sb);
32320+
32321+ rdhash = au_sbi(sb)->si_rdhash;
1308ab2a 32322+ if (!rdhash)
32323+ rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
dece6358
AM
32324+ err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
32325+ if (unlikely(err))
1facf9fc 32326+ goto out;
dece6358
AM
32327+ err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
32328+ if (unlikely(err))
1facf9fc 32329+ goto out_delist;
32330+
32331+ err = 0;
32332+ arg->flags = 0;
dece6358
AM
32333+ shwh = 0;
32334+ if (au_opt_test(au_mntflags(sb), SHWH)) {
32335+ shwh = 1;
32336+ au_fset_fillvdir(arg->flags, SHWH);
32337+ }
5afbbe0d
AM
32338+ btop = au_fbtop(file);
32339+ bbot = au_fbbot_dir(file);
32340+ for (bindex = btop; !err && bindex <= bbot; bindex++) {
4a4d8108 32341+ hf = au_hf_dir(file, bindex);
1facf9fc 32342+ if (!hf)
32343+ continue;
32344+
32345+ offset = vfsub_llseek(hf, 0, SEEK_SET);
32346+ err = offset;
32347+ if (unlikely(offset))
32348+ break;
32349+
32350+ arg->bindex = bindex;
32351+ au_fclr_fillvdir(arg->flags, WHABLE);
dece6358 32352+ if (shwh
5afbbe0d 32353+ || (bindex != bbot
dece6358 32354+ && au_br_whable(au_sbr_perm(sb, bindex))))
1facf9fc 32355+ au_fset_fillvdir(arg->flags, WHABLE);
32356+ do {
32357+ arg->err = 0;
32358+ au_fclr_fillvdir(arg->flags, CALLED);
32359+ /* smp_mb(); */
392086de 32360+ err = vfsub_iterate_dir(hf, &arg->ctx);
1facf9fc 32361+ if (err >= 0)
32362+ err = arg->err;
32363+ } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
392086de
AM
32364+
32365+ /*
32366+ * dir_relax() may be good for concurrency, but aufs should not
32367+ * use it since it will cause a lockdep problem.
32368+ */
1facf9fc 32369+ }
dece6358
AM
32370+
32371+ if (!err && shwh)
32372+ err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
32373+
32374+ au_nhash_wh_free(&arg->whlist);
1facf9fc 32375+
4f0767ce 32376+out_delist:
dece6358 32377+ au_nhash_de_free(&arg->delist);
4f0767ce 32378+out:
1facf9fc 32379+ return err;
32380+}
32381+
32382+static int read_vdir(struct file *file, int may_read)
32383+{
32384+ int err;
32385+ unsigned long expire;
32386+ unsigned char do_read;
392086de
AM
32387+ struct fillvdir_arg arg = {
32388+ .ctx = {
2000de60 32389+ .actor = fillvdir
392086de
AM
32390+ }
32391+ };
1facf9fc 32392+ struct inode *inode;
32393+ struct au_vdir *vdir, *allocated;
32394+
32395+ err = 0;
c06a8ce3 32396+ inode = file_inode(file);
1facf9fc 32397+ IMustLock(inode);
5afbbe0d 32398+ IiMustWriteLock(inode);
dece6358
AM
32399+ SiMustAnyLock(inode->i_sb);
32400+
1facf9fc 32401+ allocated = NULL;
32402+ do_read = 0;
32403+ expire = au_sbi(inode->i_sb)->si_rdcache;
32404+ vdir = au_ivdir(inode);
32405+ if (!vdir) {
32406+ do_read = 1;
1308ab2a 32407+ vdir = alloc_vdir(file);
1facf9fc 32408+ err = PTR_ERR(vdir);
32409+ if (IS_ERR(vdir))
32410+ goto out;
32411+ err = 0;
32412+ allocated = vdir;
32413+ } else if (may_read
be118d29 32414+ && (!inode_eq_iversion(inode, vdir->vd_version)
1facf9fc 32415+ || time_after(jiffies, vdir->vd_jiffy + expire))) {
32416+ do_read = 1;
32417+ err = reinit_vdir(vdir);
32418+ if (unlikely(err))
32419+ goto out;
32420+ }
32421+
32422+ if (!do_read)
32423+ return 0; /* success */
32424+
32425+ arg.file = file;
32426+ arg.vdir = vdir;
32427+ err = au_do_read_vdir(&arg);
32428+ if (!err) {
392086de 32429+ /* file->f_pos = 0; */ /* todo: ctx->pos? */
be118d29 32430+ vdir->vd_version = inode_query_iversion(inode);
1facf9fc 32431+ vdir->vd_last.ul = 0;
32432+ vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32433+ if (allocated)
32434+ au_set_ivdir(inode, allocated);
32435+ } else if (allocated)
1c60b727 32436+ au_vdir_free(allocated);
1facf9fc 32437+
4f0767ce 32438+out:
1facf9fc 32439+ return err;
32440+}
32441+
32442+static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
32443+{
32444+ int err, rerr;
32445+ unsigned long ul, n;
32446+ const unsigned int deblk_sz = src->vd_deblk_sz;
32447+
32448+ AuDebugOn(tgt->vd_nblk != 1);
32449+
32450+ err = -ENOMEM;
32451+ if (tgt->vd_nblk < src->vd_nblk) {
32452+ unsigned char **p;
32453+
e2f27e51
AM
32454+ p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
32455+ GFP_NOFS, /*may_shrink*/0);
1facf9fc 32456+ if (unlikely(!p))
32457+ goto out;
32458+ tgt->vd_deblk = p;
32459+ }
32460+
1308ab2a 32461+ if (tgt->vd_deblk_sz != deblk_sz) {
32462+ unsigned char *p;
32463+
32464+ tgt->vd_deblk_sz = deblk_sz;
e2f27e51
AM
32465+ p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
32466+ /*may_shrink*/1);
1308ab2a 32467+ if (unlikely(!p))
32468+ goto out;
32469+ tgt->vd_deblk[0] = p;
32470+ }
1facf9fc 32471+ memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
1facf9fc 32472+ tgt->vd_version = src->vd_version;
32473+ tgt->vd_jiffy = src->vd_jiffy;
32474+
32475+ n = src->vd_nblk;
32476+ for (ul = 1; ul < n; ul++) {
dece6358
AM
32477+ tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
32478+ GFP_NOFS);
32479+ if (unlikely(!tgt->vd_deblk[ul]))
1facf9fc 32480+ goto out;
1308ab2a 32481+ tgt->vd_nblk++;
1facf9fc 32482+ }
1308ab2a 32483+ tgt->vd_nblk = n;
32484+ tgt->vd_last.ul = tgt->vd_last.ul;
32485+ tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
32486+ tgt->vd_last.p.deblk += src->vd_last.p.deblk
32487+ - src->vd_deblk[src->vd_last.ul];
1facf9fc 32488+ /* smp_mb(); */
32489+ return 0; /* success */
32490+
4f0767ce 32491+out:
1facf9fc 32492+ rerr = reinit_vdir(tgt);
32493+ BUG_ON(rerr);
32494+ return err;
32495+}
32496+
32497+int au_vdir_init(struct file *file)
32498+{
32499+ int err;
32500+ struct inode *inode;
32501+ struct au_vdir *vdir_cache, *allocated;
32502+
392086de 32503+ /* test file->f_pos here instead of ctx->pos */
1facf9fc 32504+ err = read_vdir(file, !file->f_pos);
32505+ if (unlikely(err))
32506+ goto out;
32507+
32508+ allocated = NULL;
32509+ vdir_cache = au_fvdir_cache(file);
32510+ if (!vdir_cache) {
1308ab2a 32511+ vdir_cache = alloc_vdir(file);
1facf9fc 32512+ err = PTR_ERR(vdir_cache);
32513+ if (IS_ERR(vdir_cache))
32514+ goto out;
32515+ allocated = vdir_cache;
32516+ } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
392086de 32517+ /* test file->f_pos here instead of ctx->pos */
1facf9fc 32518+ err = reinit_vdir(vdir_cache);
32519+ if (unlikely(err))
32520+ goto out;
32521+ } else
32522+ return 0; /* success */
32523+
c06a8ce3 32524+ inode = file_inode(file);
1facf9fc 32525+ err = copy_vdir(vdir_cache, au_ivdir(inode));
32526+ if (!err) {
be118d29 32527+ file->f_version = inode_query_iversion(inode);
1facf9fc 32528+ if (allocated)
32529+ au_set_fvdir_cache(file, allocated);
32530+ } else if (allocated)
1c60b727 32531+ au_vdir_free(allocated);
1facf9fc 32532+
4f0767ce 32533+out:
1facf9fc 32534+ return err;
32535+}
32536+
32537+static loff_t calc_offset(struct au_vdir *vdir)
32538+{
32539+ loff_t offset;
32540+ union au_vdir_deblk_p p;
32541+
32542+ p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
32543+ offset = vdir->vd_last.p.deblk - p.deblk;
32544+ offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
32545+ return offset;
32546+}
32547+
32548+/* returns true or false */
392086de 32549+static int seek_vdir(struct file *file, struct dir_context *ctx)
1facf9fc 32550+{
32551+ int valid;
32552+ unsigned int deblk_sz;
32553+ unsigned long ul, n;
32554+ loff_t offset;
32555+ union au_vdir_deblk_p p, deblk_end;
32556+ struct au_vdir *vdir_cache;
32557+
32558+ valid = 1;
32559+ vdir_cache = au_fvdir_cache(file);
32560+ offset = calc_offset(vdir_cache);
32561+ AuDbg("offset %lld\n", offset);
392086de 32562+ if (ctx->pos == offset)
1facf9fc 32563+ goto out;
32564+
32565+ vdir_cache->vd_last.ul = 0;
32566+ vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
392086de 32567+ if (!ctx->pos)
1facf9fc 32568+ goto out;
32569+
32570+ valid = 0;
32571+ deblk_sz = vdir_cache->vd_deblk_sz;
392086de 32572+ ul = div64_u64(ctx->pos, deblk_sz);
1facf9fc 32573+ AuDbg("ul %lu\n", ul);
32574+ if (ul >= vdir_cache->vd_nblk)
32575+ goto out;
32576+
32577+ n = vdir_cache->vd_nblk;
32578+ for (; ul < n; ul++) {
32579+ p.deblk = vdir_cache->vd_deblk[ul];
32580+ deblk_end.deblk = p.deblk + deblk_sz;
32581+ offset = ul;
32582+ offset *= deblk_sz;
392086de 32583+ while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
1facf9fc 32584+ unsigned int l;
32585+
32586+ l = calc_size(p.de->de_str.len);
32587+ offset += l;
32588+ p.deblk += l;
32589+ }
32590+ if (!is_deblk_end(&p, &deblk_end)) {
32591+ valid = 1;
32592+ vdir_cache->vd_last.ul = ul;
32593+ vdir_cache->vd_last.p = p;
32594+ break;
32595+ }
32596+ }
32597+
4f0767ce 32598+out:
1facf9fc 32599+ /* smp_mb(); */
b00004a5
AM
32600+ if (!valid)
32601+ AuDbg("valid %d\n", !valid);
1facf9fc 32602+ return valid;
32603+}
32604+
392086de 32605+int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
1facf9fc 32606+{
1facf9fc 32607+ unsigned int l, deblk_sz;
32608+ union au_vdir_deblk_p deblk_end;
32609+ struct au_vdir *vdir_cache;
32610+ struct au_vdir_de *de;
32611+
392086de 32612+ if (!seek_vdir(file, ctx))
1facf9fc 32613+ return 0;
32614+
acd2b654 32615+ vdir_cache = au_fvdir_cache(file);
1facf9fc 32616+ deblk_sz = vdir_cache->vd_deblk_sz;
32617+ while (1) {
32618+ deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32619+ deblk_end.deblk += deblk_sz;
32620+ while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
32621+ de = vdir_cache->vd_last.p.de;
32622+ AuDbg("%.*s, off%lld, i%lu, dt%d\n",
392086de 32623+ de->de_str.len, de->de_str.name, ctx->pos,
1facf9fc 32624+ (unsigned long)de->de_ino, de->de_type);
392086de
AM
32625+ if (unlikely(!dir_emit(ctx, de->de_str.name,
32626+ de->de_str.len, de->de_ino,
32627+ de->de_type))) {
1facf9fc 32628+ /* todo: ignore the error caused by udba? */
32629+ /* return err; */
32630+ return 0;
32631+ }
32632+
32633+ l = calc_size(de->de_str.len);
32634+ vdir_cache->vd_last.p.deblk += l;
392086de 32635+ ctx->pos += l;
1facf9fc 32636+ }
32637+ if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
32638+ vdir_cache->vd_last.ul++;
32639+ vdir_cache->vd_last.p.deblk
32640+ = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
392086de 32641+ ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
1facf9fc 32642+ continue;
32643+ }
32644+ break;
32645+ }
32646+
32647+ /* smp_mb(); */
32648+ return 0;
32649+}
7f207e10 32650diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
eca34b5c 32651--- /usr/share/empty/fs/aufs/vfsub.c 1970-01-01 01:00:00.000000000 +0100
46016270 32652+++ linux/fs/aufs/vfsub.c 2021-06-30 21:35:11.397206648 +0200
0b2a12c6 32653@@ -0,0 +1,916 @@
cd7a4cd9 32654+// SPDX-License-Identifier: GPL-2.0
1facf9fc 32655+/*
d58c55f2 32656+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 32657+ *
32658+ * This program, aufs is free software; you can redistribute it and/or modify
32659+ * it under the terms of the GNU General Public License as published by
32660+ * the Free Software Foundation; either version 2 of the License, or
32661+ * (at your option) any later version.
dece6358
AM
32662+ *
32663+ * This program is distributed in the hope that it will be useful,
32664+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
32665+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32666+ * GNU General Public License for more details.
32667+ *
32668+ * You should have received a copy of the GNU General Public License
523b37e3 32669+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 32670+ */
32671+
32672+/*
32673+ * sub-routines for VFS
32674+ */
32675+
8b6a4947 32676+#include <linux/mnt_namespace.h>
dece6358 32677+#include <linux/namei.h>
8cdd5066 32678+#include <linux/nsproxy.h>
dece6358
AM
32679+#include <linux/security.h>
32680+#include <linux/splice.h>
1facf9fc 32681+#include "aufs.h"
32682+
8cdd5066
JR
32683+#ifdef CONFIG_AUFS_BR_FUSE
32684+int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
32685+{
8cdd5066
JR
32686+ if (!au_test_fuse(h_sb) || !au_userns)
32687+ return 0;
32688+
8b6a4947 32689+ return is_current_mnt_ns(mnt) ? 0 : -EACCES;
8cdd5066
JR
32690+}
32691+#endif
32692+
a2654f78
AM
32693+int vfsub_sync_filesystem(struct super_block *h_sb, int wait)
32694+{
32695+ int err;
32696+
32697+ lockdep_off();
32698+ down_read(&h_sb->s_umount);
32699+ err = __sync_filesystem(h_sb, wait);
32700+ up_read(&h_sb->s_umount);
32701+ lockdep_on();
32702+
32703+ return err;
32704+}
32705+
8cdd5066
JR
32706+/* ---------------------------------------------------------------------- */
32707+
1facf9fc 32708+int vfsub_update_h_iattr(struct path *h_path, int *did)
32709+{
32710+ int err;
32711+ struct kstat st;
32712+ struct super_block *h_sb;
32713+
32714+ /* for remote fs, leave work for its getattr or d_revalidate */
32715+ /* for bad i_attr fs, handle them in aufs_getattr() */
32716+ /* still some fs may acquire i_mutex. we need to skip them */
32717+ err = 0;
32718+ if (!did)
32719+ did = &err;
32720+ h_sb = h_path->dentry->d_sb;
32721+ *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
32722+ if (*did)
521ced18 32723+ err = vfsub_getattr(h_path, &st);
1facf9fc 32724+
32725+ return err;
32726+}
32727+
32728+/* ---------------------------------------------------------------------- */
32729+
4a4d8108 32730+struct file *vfsub_dentry_open(struct path *path, int flags)
1308ab2a 32731+{
42b5c33a 32732+ return dentry_open(path, flags /* | __FMODE_NONOTIFY */,
7f207e10 32733+ current_cred());
1308ab2a 32734+}
32735+
1facf9fc 32736+struct file *vfsub_filp_open(const char *path, int oflags, int mode)
32737+{
32738+ struct file *file;
32739+
2cbb1c4b 32740+ lockdep_off();
7f207e10 32741+ file = filp_open(path,
2cbb1c4b 32742+ oflags /* | __FMODE_NONOTIFY */,
7f207e10 32743+ mode);
2cbb1c4b 32744+ lockdep_on();
1facf9fc 32745+ if (IS_ERR(file))
32746+ goto out;
32747+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
32748+
4f0767ce 32749+out:
1facf9fc 32750+ return file;
32751+}
32752+
b912730e
AM
32753+/*
32754+ * Ideally this function should call VFS:do_last() in order to keep all its
32755+ * checkings. But it is very hard for aufs to regenerate several VFS internal
32756+ * structure such as nameidata. This is a second (or third) best approach.
32757+ * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
32758+ */
32759+int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
acd2b654 32760+ struct vfsub_aopen_args *args)
b912730e
AM
32761+{
32762+ int err;
acd2b654 32763+ struct au_branch *br = args->br;
b912730e
AM
32764+ struct file *file = args->file;
32765+ /* copied from linux/fs/namei.c:atomic_open() */
32766+ struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
32767+
32768+ IMustLock(dir);
32769+ AuDebugOn(!dir->i_op->atomic_open);
32770+
32771+ err = au_br_test_oflag(args->open_flag, br);
32772+ if (unlikely(err))
32773+ goto out;
32774+
acd2b654
AM
32775+ au_lcnt_inc(&br->br_nfiles);
32776+ file->f_path.dentry = DENTRY_NOT_SET;
32777+ file->f_path.mnt = au_br_mnt(br);
32778+ AuDbg("%ps\n", dir->i_op->atomic_open);
b912730e 32779+ err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
acd2b654
AM
32780+ args->create_mode);
32781+ if (unlikely(err < 0)) {
32782+ au_lcnt_dec(&br->br_nfiles);
b912730e 32783+ goto out;
acd2b654 32784+ }
b912730e 32785+
acd2b654
AM
32786+ /* temporary workaround for nfsv4 branch */
32787+ if (au_test_nfs(dir->i_sb))
32788+ nfs_mark_for_revalidate(dir);
b912730e 32789+
acd2b654
AM
32790+ if (file->f_mode & FMODE_CREATED)
32791+ fsnotify_create(dir, dentry);
32792+ if (!(file->f_mode & FMODE_OPENED)) {
32793+ au_lcnt_dec(&br->br_nfiles);
32794+ goto out;
b912730e
AM
32795+ }
32796+
acd2b654
AM
32797+ /* todo: call VFS:may_open() here */
32798+ /* todo: ima_file_check() too? */
32799+ if (!err && (args->open_flag & __FMODE_EXEC))
32800+ err = deny_write_access(file);
32801+ if (!err)
32802+ fsnotify_open(file);
32803+ else
32804+ au_lcnt_dec(&br->br_nfiles);
32805+ /* note that the file is created and still opened */
b912730e
AM
32806+
32807+out:
32808+ return err;
32809+}
32810+
1facf9fc 32811+int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
32812+{
32813+ int err;
32814+
1facf9fc 32815+ err = kern_path(name, flags, path);
5527c038 32816+ if (!err && d_is_positive(path->dentry))
1facf9fc 32817+ vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
32818+ return err;
32819+}
32820+
febd17d6
JR
32821+struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
32822+ struct dentry *parent, int len)
32823+{
32824+ struct path path = {
32825+ .mnt = NULL
32826+ };
32827+
32828+ path.dentry = lookup_one_len_unlocked(name, parent, len);
32829+ if (IS_ERR(path.dentry))
32830+ goto out;
32831+ if (d_is_positive(path.dentry))
32832+ vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32833+
32834+out:
32835+ AuTraceErrPtr(path.dentry);
32836+ return path.dentry;
32837+}
32838+
1facf9fc 32839+struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
32840+ int len)
32841+{
32842+ struct path path = {
32843+ .mnt = NULL
32844+ };
32845+
1308ab2a 32846+ /* VFS checks it too, but by WARN_ON_ONCE() */
5527c038 32847+ IMustLock(d_inode(parent));
1facf9fc 32848+
32849+ path.dentry = lookup_one_len(name, parent, len);
32850+ if (IS_ERR(path.dentry))
32851+ goto out;
5527c038 32852+ if (d_is_positive(path.dentry))
1facf9fc 32853+ vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32854+
4f0767ce 32855+out:
4a4d8108 32856+ AuTraceErrPtr(path.dentry);
1facf9fc 32857+ return path.dentry;
32858+}
32859+
b4510431 32860+void vfsub_call_lkup_one(void *args)
2cbb1c4b 32861+{
b4510431
AM
32862+ struct vfsub_lkup_one_args *a = args;
32863+ *a->errp = vfsub_lkup_one(a->name, a->parent);
2cbb1c4b
JR
32864+}
32865+
1facf9fc 32866+/* ---------------------------------------------------------------------- */
32867+
32868+struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
32869+ struct dentry *d2, struct au_hinode *hdir2)
32870+{
32871+ struct dentry *d;
32872+
2cbb1c4b 32873+ lockdep_off();
1facf9fc 32874+ d = lock_rename(d1, d2);
2cbb1c4b 32875+ lockdep_on();
4a4d8108 32876+ au_hn_suspend(hdir1);
1facf9fc 32877+ if (hdir1 != hdir2)
4a4d8108 32878+ au_hn_suspend(hdir2);
1facf9fc 32879+
32880+ return d;
32881+}
32882+
32883+void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
32884+ struct dentry *d2, struct au_hinode *hdir2)
32885+{
4a4d8108 32886+ au_hn_resume(hdir1);
1facf9fc 32887+ if (hdir1 != hdir2)
4a4d8108 32888+ au_hn_resume(hdir2);
2cbb1c4b 32889+ lockdep_off();
1facf9fc 32890+ unlock_rename(d1, d2);
2cbb1c4b 32891+ lockdep_on();
1facf9fc 32892+}
32893+
32894+/* ---------------------------------------------------------------------- */
32895+
b4510431 32896+int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
1facf9fc 32897+{
32898+ int err;
32899+ struct dentry *d;
0b2a12c6 32900+ struct user_namespace *userns;
1facf9fc 32901+
32902+ IMustLock(dir);
32903+
32904+ d = path->dentry;
32905+ path->dentry = d->d_parent;
b752ccd1 32906+ err = security_path_mknod(path, d, mode, 0);
1facf9fc 32907+ path->dentry = d;
32908+ if (unlikely(err))
32909+ goto out;
0b2a12c6 32910+ userns = mnt_user_ns(path->mnt);
1facf9fc 32911+
c1595e42 32912+ lockdep_off();
0b2a12c6 32913+ err = vfs_create(userns, dir, path->dentry, mode, want_excl);
c1595e42 32914+ lockdep_on();
1facf9fc 32915+ if (!err) {
32916+ struct path tmp = *path;
32917+ int did;
32918+
32919+ vfsub_update_h_iattr(&tmp, &did);
32920+ if (did) {
32921+ tmp.dentry = path->dentry->d_parent;
32922+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32923+ }
32924+ /*ignore*/
32925+ }
32926+
4f0767ce 32927+out:
1facf9fc 32928+ return err;
32929+}
32930+
32931+int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
32932+{
32933+ int err;
32934+ struct dentry *d;
0b2a12c6 32935+ struct user_namespace *userns;
1facf9fc 32936+
32937+ IMustLock(dir);
32938+
32939+ d = path->dentry;
32940+ path->dentry = d->d_parent;
b752ccd1 32941+ err = security_path_symlink(path, d, symname);
1facf9fc 32942+ path->dentry = d;
32943+ if (unlikely(err))
32944+ goto out;
0b2a12c6 32945+ userns = mnt_user_ns(path->mnt);
1facf9fc 32946+
c1595e42 32947+ lockdep_off();
0b2a12c6 32948+ err = vfs_symlink(userns, dir, path->dentry, symname);
c1595e42 32949+ lockdep_on();
1facf9fc 32950+ if (!err) {
32951+ struct path tmp = *path;
32952+ int did;
32953+
32954+ vfsub_update_h_iattr(&tmp, &did);
32955+ if (did) {
32956+ tmp.dentry = path->dentry->d_parent;
32957+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32958+ }
32959+ /*ignore*/
32960+ }
32961+
4f0767ce 32962+out:
1facf9fc 32963+ return err;
32964+}
32965+
32966+int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
32967+{
32968+ int err;
32969+ struct dentry *d;
0b2a12c6 32970+ struct user_namespace *userns;
1facf9fc 32971+
32972+ IMustLock(dir);
32973+
32974+ d = path->dentry;
32975+ path->dentry = d->d_parent;
027c5e7a 32976+ err = security_path_mknod(path, d, mode, new_encode_dev(dev));
1facf9fc 32977+ path->dentry = d;
32978+ if (unlikely(err))
32979+ goto out;
0b2a12c6 32980+ userns = mnt_user_ns(path->mnt);
1facf9fc 32981+
c1595e42 32982+ lockdep_off();
0b2a12c6 32983+ err = vfs_mknod(userns, dir, path->dentry, mode, dev);
c1595e42 32984+ lockdep_on();
1facf9fc 32985+ if (!err) {
32986+ struct path tmp = *path;
32987+ int did;
32988+
32989+ vfsub_update_h_iattr(&tmp, &did);
32990+ if (did) {
32991+ tmp.dentry = path->dentry->d_parent;
32992+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32993+ }
32994+ /*ignore*/
32995+ }
32996+
4f0767ce 32997+out:
1facf9fc 32998+ return err;
32999+}
33000+
33001+static int au_test_nlink(struct inode *inode)
33002+{
33003+ const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
33004+
33005+ if (!au_test_fs_no_limit_nlink(inode->i_sb)
33006+ || inode->i_nlink < link_max)
33007+ return 0;
33008+ return -EMLINK;
33009+}
33010+
523b37e3
AM
33011+int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
33012+ struct inode **delegated_inode)
1facf9fc 33013+{
33014+ int err;
33015+ struct dentry *d;
0b2a12c6 33016+ struct user_namespace *userns;
1facf9fc 33017+
33018+ IMustLock(dir);
33019+
5527c038 33020+ err = au_test_nlink(d_inode(src_dentry));
1facf9fc 33021+ if (unlikely(err))
33022+ return err;
33023+
b4510431 33024+ /* we don't call may_linkat() */
1facf9fc 33025+ d = path->dentry;
33026+ path->dentry = d->d_parent;
b752ccd1 33027+ err = security_path_link(src_dentry, path, d);
1facf9fc 33028+ path->dentry = d;
33029+ if (unlikely(err))
33030+ goto out;
0b2a12c6 33031+ userns = mnt_user_ns(path->mnt);
1facf9fc 33032+
2cbb1c4b 33033+ lockdep_off();
0b2a12c6 33034+ err = vfs_link(src_dentry, userns, dir, path->dentry, delegated_inode);
2cbb1c4b 33035+ lockdep_on();
1facf9fc 33036+ if (!err) {
33037+ struct path tmp = *path;
33038+ int did;
33039+
33040+ /* fuse has different memory inode for the same inumber */
33041+ vfsub_update_h_iattr(&tmp, &did);
33042+ if (did) {
33043+ tmp.dentry = path->dentry->d_parent;
33044+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33045+ tmp.dentry = src_dentry;
33046+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33047+ }
33048+ /*ignore*/
33049+ }
33050+
4f0767ce 33051+out:
1facf9fc 33052+ return err;
33053+}
33054+
33055+int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
523b37e3 33056+ struct inode *dir, struct path *path,
f2c43d5f 33057+ struct inode **delegated_inode, unsigned int flags)
1facf9fc 33058+{
33059+ int err;
0b2a12c6 33060+ struct renamedata rd;
1facf9fc 33061+ struct path tmp = {
33062+ .mnt = path->mnt
33063+ };
33064+ struct dentry *d;
33065+
33066+ IMustLock(dir);
33067+ IMustLock(src_dir);
33068+
33069+ d = path->dentry;
33070+ path->dentry = d->d_parent;
33071+ tmp.dentry = src_dentry->d_parent;
38d290e6 33072+ err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
1facf9fc 33073+ path->dentry = d;
33074+ if (unlikely(err))
33075+ goto out;
33076+
0b2a12c6
JR
33077+ rd.old_mnt_userns = mnt_user_ns(path->mnt);
33078+ rd.old_dir = src_dir;
33079+ rd.old_dentry = src_dentry;
33080+ rd.new_mnt_userns = rd.old_mnt_userns;
33081+ rd.new_dir = dir;
33082+ rd.new_dentry = path->dentry;
33083+ rd.delegated_inode = delegated_inode;
33084+ rd.flags = flags;
2cbb1c4b 33085+ lockdep_off();
0b2a12c6 33086+ err = vfs_rename(&rd);
2cbb1c4b 33087+ lockdep_on();
1facf9fc 33088+ if (!err) {
33089+ int did;
33090+
33091+ tmp.dentry = d->d_parent;
33092+ vfsub_update_h_iattr(&tmp, &did);
33093+ if (did) {
33094+ tmp.dentry = src_dentry;
33095+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33096+ tmp.dentry = src_dentry->d_parent;
33097+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33098+ }
33099+ /*ignore*/
33100+ }
33101+
4f0767ce 33102+out:
1facf9fc 33103+ return err;
33104+}
33105+
33106+int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
33107+{
33108+ int err;
33109+ struct dentry *d;
0b2a12c6 33110+ struct user_namespace *userns;
1facf9fc 33111+
33112+ IMustLock(dir);
33113+
33114+ d = path->dentry;
33115+ path->dentry = d->d_parent;
b752ccd1 33116+ err = security_path_mkdir(path, d, mode);
1facf9fc 33117+ path->dentry = d;
33118+ if (unlikely(err))
33119+ goto out;
0b2a12c6 33120+ userns = mnt_user_ns(path->mnt);
1facf9fc 33121+
c1595e42 33122+ lockdep_off();
0b2a12c6 33123+ err = vfs_mkdir(userns, dir, path->dentry, mode);
c1595e42 33124+ lockdep_on();
1facf9fc 33125+ if (!err) {
33126+ struct path tmp = *path;
33127+ int did;
33128+
33129+ vfsub_update_h_iattr(&tmp, &did);
33130+ if (did) {
33131+ tmp.dentry = path->dentry->d_parent;
33132+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33133+ }
33134+ /*ignore*/
33135+ }
33136+
4f0767ce 33137+out:
1facf9fc 33138+ return err;
33139+}
33140+
33141+int vfsub_rmdir(struct inode *dir, struct path *path)
33142+{
33143+ int err;
33144+ struct dentry *d;
0b2a12c6 33145+ struct user_namespace *userns;
1facf9fc 33146+
33147+ IMustLock(dir);
33148+
33149+ d = path->dentry;
33150+ path->dentry = d->d_parent;
b752ccd1 33151+ err = security_path_rmdir(path, d);
1facf9fc 33152+ path->dentry = d;
33153+ if (unlikely(err))
33154+ goto out;
0b2a12c6 33155+ userns = mnt_user_ns(path->mnt);
1facf9fc 33156+
2cbb1c4b 33157+ lockdep_off();
0b2a12c6 33158+ err = vfs_rmdir(userns, dir, path->dentry);
2cbb1c4b 33159+ lockdep_on();
1facf9fc 33160+ if (!err) {
33161+ struct path tmp = {
33162+ .dentry = path->dentry->d_parent,
33163+ .mnt = path->mnt
33164+ };
33165+
33166+ vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33167+ }
33168+
4f0767ce 33169+out:
1facf9fc 33170+ return err;
33171+}
33172+
33173+/* ---------------------------------------------------------------------- */
33174+
9dbd164d 33175+/* todo: support mmap_sem? */
1facf9fc 33176+ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33177+ loff_t *ppos)
33178+{
33179+ ssize_t err;
33180+
2cbb1c4b 33181+ lockdep_off();
1facf9fc 33182+ err = vfs_read(file, ubuf, count, ppos);
2cbb1c4b 33183+ lockdep_on();
1facf9fc 33184+ if (err >= 0)
33185+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33186+ return err;
33187+}
33188+
1facf9fc 33189+ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33190+ loff_t *ppos)
33191+{
33192+ ssize_t err;
1facf9fc 33193+
e37dd06a
AM
33194+ lockdep_off();
33195+ err = kernel_read(file, kbuf, count, ppos);
33196+ lockdep_on();
33197+ AuTraceErr(err);
33198+ if (err >= 0)
33199+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
1facf9fc 33200+ return err;
33201+}
33202+
33203+ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33204+ loff_t *ppos)
33205+{
33206+ ssize_t err;
33207+
2cbb1c4b 33208+ lockdep_off();
1facf9fc 33209+ err = vfs_write(file, ubuf, count, ppos);
2cbb1c4b 33210+ lockdep_on();
1facf9fc 33211+ if (err >= 0)
33212+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33213+ return err;
33214+}
33215+
33216+ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
33217+{
33218+ ssize_t err;
1facf9fc 33219+
e37dd06a
AM
33220+ lockdep_off();
33221+ err = kernel_write(file, kbuf, count, ppos);
33222+ lockdep_on();
33223+ if (err >= 0)
33224+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
1facf9fc 33225+ return err;
33226+}
33227+
4a4d8108
AM
33228+int vfsub_flush(struct file *file, fl_owner_t id)
33229+{
33230+ int err;
33231+
33232+ err = 0;
523b37e3 33233+ if (file->f_op->flush) {
2000de60 33234+ if (!au_test_nfs(file->f_path.dentry->d_sb))
2cbb1c4b
JR
33235+ err = file->f_op->flush(file, id);
33236+ else {
33237+ lockdep_off();
33238+ err = file->f_op->flush(file, id);
33239+ lockdep_on();
33240+ }
4a4d8108
AM
33241+ if (!err)
33242+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
33243+ /*ignore*/
33244+ }
33245+ return err;
33246+}
33247+
392086de 33248+int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
1facf9fc 33249+{
33250+ int err;
33251+
062440b3 33252+ AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
392086de 33253+
2cbb1c4b 33254+ lockdep_off();
392086de 33255+ err = iterate_dir(file, ctx);
2cbb1c4b 33256+ lockdep_on();
1facf9fc 33257+ if (err >= 0)
33258+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
1c60b727 33259+
1facf9fc 33260+ return err;
33261+}
33262+
33263+long vfsub_splice_to(struct file *in, loff_t *ppos,
33264+ struct pipe_inode_info *pipe, size_t len,
33265+ unsigned int flags)
33266+{
33267+ long err;
33268+
2cbb1c4b 33269+ lockdep_off();
0fc653ad 33270+ err = do_splice_to(in, ppos, pipe, len, flags);
2cbb1c4b 33271+ lockdep_on();
4a4d8108 33272+ file_accessed(in);
1facf9fc 33273+ if (err >= 0)
33274+ vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
33275+ return err;
33276+}
33277+
33278+long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33279+ loff_t *ppos, size_t len, unsigned int flags)
33280+{
33281+ long err;
33282+
2cbb1c4b 33283+ lockdep_off();
0fc653ad 33284+ err = do_splice_from(pipe, out, ppos, len, flags);
2cbb1c4b 33285+ lockdep_on();
1facf9fc 33286+ if (err >= 0)
33287+ vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
33288+ return err;
33289+}
33290+
53392da6
AM
33291+int vfsub_fsync(struct file *file, struct path *path, int datasync)
33292+{
33293+ int err;
33294+
33295+ /* file can be NULL */
33296+ lockdep_off();
33297+ err = vfs_fsync(file, datasync);
33298+ lockdep_on();
33299+ if (!err) {
33300+ if (!path) {
33301+ AuDebugOn(!file);
33302+ path = &file->f_path;
33303+ }
33304+ vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33305+ }
33306+ return err;
33307+}
33308+
1facf9fc 33309+/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
33310+int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33311+ struct file *h_file)
33312+{
33313+ int err;
33314+ struct inode *h_inode;
c06a8ce3 33315+ struct super_block *h_sb;
0b2a12c6 33316+ struct user_namespace *h_userns;
1facf9fc 33317+
1facf9fc 33318+ if (!h_file) {
c06a8ce3
AM
33319+ err = vfsub_truncate(h_path, length);
33320+ goto out;
1facf9fc 33321+ }
33322+
5527c038 33323+ h_inode = d_inode(h_path->dentry);
c06a8ce3
AM
33324+ h_sb = h_inode->i_sb;
33325+ lockdep_off();
33326+ sb_start_write(h_sb);
33327+ lockdep_on();
1facf9fc 33328+ err = locks_verify_truncate(h_inode, h_file, length);
33329+ if (!err)
953406b4 33330+ err = security_path_truncate(h_path);
2cbb1c4b 33331+ if (!err) {
0b2a12c6 33332+ h_userns = mnt_user_ns(h_path->mnt);
2cbb1c4b 33333+ lockdep_off();
0b2a12c6
JR
33334+ err = do_truncate(h_userns, h_path->dentry, length, attr,
33335+ h_file);
2cbb1c4b
JR
33336+ lockdep_on();
33337+ }
c06a8ce3
AM
33338+ lockdep_off();
33339+ sb_end_write(h_sb);
33340+ lockdep_on();
1facf9fc 33341+
4f0767ce 33342+out:
1facf9fc 33343+ return err;
33344+}
33345+
33346+/* ---------------------------------------------------------------------- */
33347+
33348+struct au_vfsub_mkdir_args {
33349+ int *errp;
33350+ struct inode *dir;
33351+ struct path *path;
33352+ int mode;
33353+};
33354+
33355+static void au_call_vfsub_mkdir(void *args)
33356+{
33357+ struct au_vfsub_mkdir_args *a = args;
33358+ *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
33359+}
33360+
33361+int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
33362+{
33363+ int err, do_sio, wkq_err;
0b2a12c6 33364+ struct user_namespace *userns;
1facf9fc 33365+
0b2a12c6
JR
33366+ userns = mnt_user_ns(path->mnt);
33367+ do_sio = au_test_h_perm_sio(userns, dir, MAY_EXEC | MAY_WRITE);
c1595e42
JR
33368+ if (!do_sio) {
33369+ lockdep_off();
1facf9fc 33370+ err = vfsub_mkdir(dir, path, mode);
c1595e42
JR
33371+ lockdep_on();
33372+ } else {
1facf9fc 33373+ struct au_vfsub_mkdir_args args = {
33374+ .errp = &err,
33375+ .dir = dir,
33376+ .path = path,
33377+ .mode = mode
33378+ };
33379+ wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
33380+ if (unlikely(wkq_err))
33381+ err = wkq_err;
33382+ }
33383+
33384+ return err;
33385+}
33386+
33387+struct au_vfsub_rmdir_args {
33388+ int *errp;
33389+ struct inode *dir;
33390+ struct path *path;
33391+};
33392+
33393+static void au_call_vfsub_rmdir(void *args)
33394+{
33395+ struct au_vfsub_rmdir_args *a = args;
33396+ *a->errp = vfsub_rmdir(a->dir, a->path);
33397+}
33398+
33399+int vfsub_sio_rmdir(struct inode *dir, struct path *path)
33400+{
33401+ int err, do_sio, wkq_err;
0b2a12c6 33402+ struct user_namespace *userns;
1facf9fc 33403+
0b2a12c6
JR
33404+ userns = mnt_user_ns(path->mnt);
33405+ do_sio = au_test_h_perm_sio(userns, dir, MAY_EXEC | MAY_WRITE);
c1595e42
JR
33406+ if (!do_sio) {
33407+ lockdep_off();
1facf9fc 33408+ err = vfsub_rmdir(dir, path);
c1595e42
JR
33409+ lockdep_on();
33410+ } else {
1facf9fc 33411+ struct au_vfsub_rmdir_args args = {
33412+ .errp = &err,
33413+ .dir = dir,
33414+ .path = path
33415+ };
33416+ wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
33417+ if (unlikely(wkq_err))
33418+ err = wkq_err;
33419+ }
33420+
33421+ return err;
33422+}
33423+
33424+/* ---------------------------------------------------------------------- */
33425+
33426+struct notify_change_args {
33427+ int *errp;
33428+ struct path *path;
33429+ struct iattr *ia;
523b37e3 33430+ struct inode **delegated_inode;
1facf9fc 33431+};
33432+
33433+static void call_notify_change(void *args)
33434+{
33435+ struct notify_change_args *a = args;
33436+ struct inode *h_inode;
0b2a12c6 33437+ struct user_namespace *userns;
1facf9fc 33438+
5527c038 33439+ h_inode = d_inode(a->path->dentry);
1facf9fc 33440+ IMustLock(h_inode);
33441+
33442+ *a->errp = -EPERM;
33443+ if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
0b2a12c6 33444+ userns = mnt_user_ns(a->path->mnt);
c1595e42 33445+ lockdep_off();
0b2a12c6 33446+ *a->errp = notify_change(userns, a->path->dentry, a->ia,
523b37e3 33447+ a->delegated_inode);
c1595e42 33448+ lockdep_on();
1facf9fc 33449+ if (!*a->errp)
33450+ vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
33451+ }
33452+ AuTraceErr(*a->errp);
33453+}
33454+
523b37e3
AM
33455+int vfsub_notify_change(struct path *path, struct iattr *ia,
33456+ struct inode **delegated_inode)
1facf9fc 33457+{
33458+ int err;
33459+ struct notify_change_args args = {
523b37e3
AM
33460+ .errp = &err,
33461+ .path = path,
33462+ .ia = ia,
33463+ .delegated_inode = delegated_inode
1facf9fc 33464+ };
33465+
33466+ call_notify_change(&args);
33467+
33468+ return err;
33469+}
33470+
523b37e3
AM
33471+int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33472+ struct inode **delegated_inode)
1facf9fc 33473+{
33474+ int err, wkq_err;
33475+ struct notify_change_args args = {
523b37e3
AM
33476+ .errp = &err,
33477+ .path = path,
33478+ .ia = ia,
33479+ .delegated_inode = delegated_inode
1facf9fc 33480+ };
33481+
33482+ wkq_err = au_wkq_wait(call_notify_change, &args);
33483+ if (unlikely(wkq_err))
33484+ err = wkq_err;
33485+
33486+ return err;
33487+}
33488+
33489+/* ---------------------------------------------------------------------- */
33490+
33491+struct unlink_args {
33492+ int *errp;
33493+ struct inode *dir;
33494+ struct path *path;
523b37e3 33495+ struct inode **delegated_inode;
1facf9fc 33496+};
33497+
33498+static void call_unlink(void *args)
33499+{
33500+ struct unlink_args *a = args;
33501+ struct dentry *d = a->path->dentry;
33502+ struct inode *h_inode;
0b2a12c6 33503+ struct user_namespace *userns;
1facf9fc 33504+ const int stop_sillyrename = (au_test_nfs(d->d_sb)
c1595e42 33505+ && au_dcount(d) == 1);
1facf9fc 33506+
33507+ IMustLock(a->dir);
33508+
33509+ a->path->dentry = d->d_parent;
33510+ *a->errp = security_path_unlink(a->path, d);
33511+ a->path->dentry = d;
33512+ if (unlikely(*a->errp))
33513+ return;
33514+
33515+ if (!stop_sillyrename)
33516+ dget(d);
5527c038
JR
33517+ h_inode = NULL;
33518+ if (d_is_positive(d)) {
33519+ h_inode = d_inode(d);
027c5e7a 33520+ ihold(h_inode);
5527c038 33521+ }
1facf9fc 33522+
0b2a12c6 33523+ userns = mnt_user_ns(a->path->mnt);
2cbb1c4b 33524+ lockdep_off();
0b2a12c6 33525+ *a->errp = vfs_unlink(userns, a->dir, d, a->delegated_inode);
2cbb1c4b 33526+ lockdep_on();
1facf9fc 33527+ if (!*a->errp) {
33528+ struct path tmp = {
33529+ .dentry = d->d_parent,
33530+ .mnt = a->path->mnt
33531+ };
33532+ vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33533+ }
33534+
33535+ if (!stop_sillyrename)
33536+ dput(d);
33537+ if (h_inode)
33538+ iput(h_inode);
33539+
33540+ AuTraceErr(*a->errp);
33541+}
33542+
33543+/*
33544+ * @dir: must be locked.
33545+ * @dentry: target dentry.
33546+ */
523b37e3
AM
33547+int vfsub_unlink(struct inode *dir, struct path *path,
33548+ struct inode **delegated_inode, int force)
1facf9fc 33549+{
33550+ int err;
33551+ struct unlink_args args = {
523b37e3
AM
33552+ .errp = &err,
33553+ .dir = dir,
33554+ .path = path,
33555+ .delegated_inode = delegated_inode
1facf9fc 33556+ };
33557+
33558+ if (!force)
33559+ call_unlink(&args);
33560+ else {
33561+ int wkq_err;
33562+
33563+ wkq_err = au_wkq_wait(call_unlink, &args);
33564+ if (unlikely(wkq_err))
33565+ err = wkq_err;
33566+ }
33567+
33568+ return err;
33569+}
7f207e10 33570diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
eca34b5c 33571--- /usr/share/empty/fs/aufs/vfsub.h 1970-01-01 01:00:00.000000000 +0100
46016270 33572+++ linux/fs/aufs/vfsub.h 2021-06-30 21:35:11.397206648 +0200
0b2a12c6 33573@@ -0,0 +1,358 @@
062440b3 33574+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 33575+/*
d58c55f2 33576+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 33577+ *
33578+ * This program, aufs is free software; you can redistribute it and/or modify
33579+ * it under the terms of the GNU General Public License as published by
33580+ * the Free Software Foundation; either version 2 of the License, or
33581+ * (at your option) any later version.
dece6358
AM
33582+ *
33583+ * This program is distributed in the hope that it will be useful,
33584+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
33585+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33586+ * GNU General Public License for more details.
33587+ *
33588+ * You should have received a copy of the GNU General Public License
523b37e3 33589+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 33590+ */
33591+
33592+/*
33593+ * sub-routines for VFS
33594+ */
33595+
33596+#ifndef __AUFS_VFSUB_H__
33597+#define __AUFS_VFSUB_H__
33598+
33599+#ifdef __KERNEL__
33600+
33601+#include <linux/fs.h>
b4510431 33602+#include <linux/mount.h>
8cdd5066 33603+#include <linux/posix_acl.h>
c1595e42 33604+#include <linux/xattr.h>
7f207e10 33605+#include "debug.h"
1facf9fc 33606+
7f207e10 33607+/* copied from linux/fs/internal.h */
2cbb1c4b 33608+/* todo: BAD approach!! */
c06a8ce3 33609+extern void __mnt_drop_write(struct vfsmount *);
acd2b654 33610+extern struct file *alloc_empty_file(int, const struct cred *);
7f207e10
AM
33611+
33612+/* ---------------------------------------------------------------------- */
1facf9fc 33613+
33614+/* lock subclass for lower inode */
33615+/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
33616+/* reduce? gave up. */
33617+enum {
c1595e42 33618+ AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
1facf9fc 33619+ AuLsc_I_PARENT, /* lower inode, parent first */
33620+ AuLsc_I_PARENT2, /* copyup dirs */
dece6358 33621+ AuLsc_I_PARENT3, /* copyup wh */
1facf9fc 33622+ AuLsc_I_CHILD,
33623+ AuLsc_I_CHILD2,
33624+ AuLsc_I_End
33625+};
33626+
33627+/* to debug easier, do not make them inlined functions */
33628+#define MtxMustLock(mtx) AuDebugOn(!mutex_is_locked(mtx))
febd17d6 33629+#define IMustLock(i) AuDebugOn(!inode_is_locked(i))
1facf9fc 33630+
33631+/* ---------------------------------------------------------------------- */
33632+
7f207e10
AM
33633+static inline void vfsub_drop_nlink(struct inode *inode)
33634+{
33635+ AuDebugOn(!inode->i_nlink);
33636+ drop_nlink(inode);
33637+}
33638+
027c5e7a
AM
33639+static inline void vfsub_dead_dir(struct inode *inode)
33640+{
33641+ AuDebugOn(!S_ISDIR(inode->i_mode));
33642+ inode->i_flags |= S_DEAD;
33643+ clear_nlink(inode);
33644+}
33645+
392086de
AM
33646+static inline int vfsub_native_ro(struct inode *inode)
33647+{
8b6a4947 33648+ return sb_rdonly(inode->i_sb)
392086de
AM
33649+ || IS_RDONLY(inode)
33650+ /* || IS_APPEND(inode) */
33651+ || IS_IMMUTABLE(inode);
33652+}
33653+
8cdd5066
JR
33654+#ifdef CONFIG_AUFS_BR_FUSE
33655+int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
33656+#else
33657+AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
33658+#endif
33659+
a2654f78
AM
33660+int vfsub_sync_filesystem(struct super_block *h_sb, int wait);
33661+
7f207e10
AM
33662+/* ---------------------------------------------------------------------- */
33663+
33664+int vfsub_update_h_iattr(struct path *h_path, int *did);
33665+struct file *vfsub_dentry_open(struct path *path, int flags);
33666+struct file *vfsub_filp_open(const char *path, int oflags, int mode);
acd2b654 33667+struct au_branch;
b912730e 33668+struct vfsub_aopen_args {
acd2b654
AM
33669+ struct file *file;
33670+ unsigned int open_flag;
33671+ umode_t create_mode;
33672+ struct au_branch *br;
b912730e 33673+};
b912730e 33674+int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
acd2b654 33675+ struct vfsub_aopen_args *args);
1facf9fc 33676+int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
b4510431 33677+
febd17d6
JR
33678+struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33679+ struct dentry *parent, int len);
1facf9fc 33680+struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
33681+ int len);
b4510431
AM
33682+
33683+struct vfsub_lkup_one_args {
33684+ struct dentry **errp;
33685+ struct qstr *name;
33686+ struct dentry *parent;
33687+};
33688+
33689+static inline struct dentry *vfsub_lkup_one(struct qstr *name,
33690+ struct dentry *parent)
33691+{
33692+ return vfsub_lookup_one_len(name->name, parent, name->len);
33693+}
33694+
33695+void vfsub_call_lkup_one(void *args);
33696+
33697+/* ---------------------------------------------------------------------- */
33698+
33699+static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
33700+{
33701+ int err;
076b876e 33702+
b4510431
AM
33703+ lockdep_off();
33704+ err = mnt_want_write(mnt);
33705+ lockdep_on();
33706+ return err;
33707+}
33708+
33709+static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
33710+{
33711+ lockdep_off();
33712+ mnt_drop_write(mnt);
33713+ lockdep_on();
33714+}
1facf9fc 33715+
7e9cd9fe 33716+#if 0 /* reserved */
c06a8ce3
AM
33717+static inline void vfsub_mnt_drop_write_file(struct file *file)
33718+{
33719+ lockdep_off();
33720+ mnt_drop_write_file(file);
33721+ lockdep_on();
33722+}
7e9cd9fe 33723+#endif
c06a8ce3 33724+
1facf9fc 33725+/* ---------------------------------------------------------------------- */
33726+
33727+struct au_hinode;
33728+struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33729+ struct dentry *d2, struct au_hinode *hdir2);
33730+void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33731+ struct dentry *d2, struct au_hinode *hdir2);
33732+
537831f9
AM
33733+int vfsub_create(struct inode *dir, struct path *path, int mode,
33734+ bool want_excl);
1facf9fc 33735+int vfsub_symlink(struct inode *dir, struct path *path,
33736+ const char *symname);
33737+int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
33738+int vfsub_link(struct dentry *src_dentry, struct inode *dir,
523b37e3 33739+ struct path *path, struct inode **delegated_inode);
1facf9fc 33740+int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
523b37e3 33741+ struct inode *hdir, struct path *path,
f2c43d5f 33742+ struct inode **delegated_inode, unsigned int flags);
1facf9fc 33743+int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
33744+int vfsub_rmdir(struct inode *dir, struct path *path);
33745+
33746+/* ---------------------------------------------------------------------- */
33747+
33748+ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33749+ loff_t *ppos);
33750+ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33751+ loff_t *ppos);
33752+ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33753+ loff_t *ppos);
33754+ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
33755+ loff_t *ppos);
4a4d8108 33756+int vfsub_flush(struct file *file, fl_owner_t id);
392086de
AM
33757+int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
33758+
c06a8ce3
AM
33759+static inline loff_t vfsub_f_size_read(struct file *file)
33760+{
33761+ return i_size_read(file_inode(file));
33762+}
33763+
4a4d8108
AM
33764+static inline unsigned int vfsub_file_flags(struct file *file)
33765+{
33766+ unsigned int flags;
33767+
33768+ spin_lock(&file->f_lock);
33769+ flags = file->f_flags;
33770+ spin_unlock(&file->f_lock);
33771+
33772+ return flags;
33773+}
1308ab2a 33774+
f0c0a007
AM
33775+static inline int vfsub_file_execed(struct file *file)
33776+{
33777+ /* todo: direct access f_flags */
33778+ return !!(vfsub_file_flags(file) & __FMODE_EXEC);
33779+}
33780+
7e9cd9fe 33781+#if 0 /* reserved */
1facf9fc 33782+static inline void vfsub_file_accessed(struct file *h_file)
33783+{
33784+ file_accessed(h_file);
33785+ vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
33786+}
7e9cd9fe 33787+#endif
1facf9fc 33788+
79b8bda9 33789+#if 0 /* reserved */
1facf9fc 33790+static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
33791+ struct dentry *h_dentry)
33792+{
33793+ struct path h_path = {
33794+ .dentry = h_dentry,
33795+ .mnt = h_mnt
33796+ };
92d182d2 33797+ touch_atime(&h_path);
1facf9fc 33798+ vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
33799+}
79b8bda9 33800+#endif
1facf9fc 33801+
cd7a4cd9
AM
33802+static inline int vfsub_update_time(struct inode *h_inode,
33803+ struct timespec64 *ts, int flags)
0c3ec466 33804+{
5afbbe0d 33805+ return update_time(h_inode, ts, flags);
0c3ec466
AM
33806+ /* no vfsub_update_h_iattr() since we don't have struct path */
33807+}
33808+
8cdd5066 33809+#ifdef CONFIG_FS_POSIX_ACL
0b2a12c6
JR
33810+static inline int vfsub_acl_chmod(struct user_namespace *h_userns,
33811+ struct inode *h_inode, umode_t h_mode)
8cdd5066
JR
33812+{
33813+ int err;
33814+
0b2a12c6 33815+ err = posix_acl_chmod(h_userns, h_inode, h_mode);
8cdd5066
JR
33816+ if (err == -EOPNOTSUPP)
33817+ err = 0;
33818+ return err;
33819+}
33820+#else
0b2a12c6
JR
33821+AuStubInt0(vfsub_acl_chmod, struct user_namespace *h_userns,
33822+ struct inode *h_inode, umode_t h_mode);
8cdd5066
JR
33823+#endif
33824+
4a4d8108
AM
33825+long vfsub_splice_to(struct file *in, loff_t *ppos,
33826+ struct pipe_inode_info *pipe, size_t len,
33827+ unsigned int flags);
33828+long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33829+ loff_t *ppos, size_t len, unsigned int flags);
c06a8ce3
AM
33830+
33831+static inline long vfsub_truncate(struct path *path, loff_t length)
33832+{
33833+ long err;
076b876e 33834+
c06a8ce3
AM
33835+ lockdep_off();
33836+ err = vfs_truncate(path, length);
33837+ lockdep_on();
33838+ return err;
33839+}
33840+
4a4d8108
AM
33841+int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33842+ struct file *h_file);
53392da6 33843+int vfsub_fsync(struct file *file, struct path *path, int datasync);
4a4d8108 33844+
521ced18
JR
33845+/*
33846+ * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
33847+ * ioctl.
33848+ */
9f237c51
AM
33849+static inline loff_t vfsub_clone_file_range(struct file *src, struct file *dst,
33850+ loff_t len)
521ced18 33851+{
9f237c51 33852+ loff_t err;
521ced18
JR
33853+
33854+ lockdep_off();
9f237c51 33855+ err = vfs_clone_file_range(src, 0, dst, 0, len, /*remap_flags*/0);
521ced18
JR
33856+ lockdep_on();
33857+
33858+ return err;
33859+}
33860+
33861+/* copy_file_range(2) is a systemcall */
33862+static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
33863+ struct file *dst, loff_t dst_pos,
33864+ size_t len, unsigned int flags)
33865+{
33866+ ssize_t ssz;
33867+
33868+ lockdep_off();
33869+ ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
33870+ lockdep_on();
33871+
33872+ return ssz;
33873+}
33874+
1facf9fc 33875+/* ---------------------------------------------------------------------- */
33876+
33877+static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
33878+{
33879+ loff_t err;
33880+
2cbb1c4b 33881+ lockdep_off();
1facf9fc 33882+ err = vfs_llseek(file, offset, origin);
2cbb1c4b 33883+ lockdep_on();
1facf9fc 33884+ return err;
33885+}
33886+
33887+/* ---------------------------------------------------------------------- */
33888+
4a4d8108
AM
33889+int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
33890+int vfsub_sio_rmdir(struct inode *dir, struct path *path);
523b37e3
AM
33891+int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33892+ struct inode **delegated_inode);
33893+int vfsub_notify_change(struct path *path, struct iattr *ia,
33894+ struct inode **delegated_inode);
33895+int vfsub_unlink(struct inode *dir, struct path *path,
33896+ struct inode **delegated_inode, int force);
4a4d8108 33897+
521ced18
JR
33898+static inline int vfsub_getattr(const struct path *path, struct kstat *st)
33899+{
33900+ return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
33901+}
33902+
c1595e42
JR
33903+/* ---------------------------------------------------------------------- */
33904+
0b2a12c6
JR
33905+static inline int vfsub_setxattr(struct user_namespace *userns,
33906+ struct dentry *dentry, const char *name,
c1595e42
JR
33907+ const void *value, size_t size, int flags)
33908+{
33909+ int err;
33910+
33911+ lockdep_off();
0b2a12c6 33912+ err = vfs_setxattr(userns, dentry, name, value, size, flags);
c1595e42
JR
33913+ lockdep_on();
33914+
33915+ return err;
33916+}
33917+
0b2a12c6
JR
33918+static inline int vfsub_removexattr(struct user_namespace *userns,
33919+ struct dentry *dentry, const char *name)
c1595e42
JR
33920+{
33921+ int err;
33922+
33923+ lockdep_off();
0b2a12c6 33924+ err = vfs_removexattr(userns, dentry, name);
c1595e42
JR
33925+ lockdep_on();
33926+
33927+ return err;
33928+}
33929+
1facf9fc 33930+#endif /* __KERNEL__ */
33931+#endif /* __AUFS_VFSUB_H__ */
7f207e10 33932diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
eca34b5c 33933--- /usr/share/empty/fs/aufs/wbr_policy.c 1970-01-01 01:00:00.000000000 +0100
46016270 33934+++ linux/fs/aufs/wbr_policy.c 2021-02-24 13:33:42.747680497 +0100
cd7a4cd9
AM
33935@@ -0,0 +1,830 @@
33936+// SPDX-License-Identifier: GPL-2.0
1facf9fc 33937+/*
d58c55f2 33938+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 33939+ *
33940+ * This program, aufs is free software; you can redistribute it and/or modify
33941+ * it under the terms of the GNU General Public License as published by
33942+ * the Free Software Foundation; either version 2 of the License, or
33943+ * (at your option) any later version.
dece6358
AM
33944+ *
33945+ * This program is distributed in the hope that it will be useful,
33946+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
33947+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33948+ * GNU General Public License for more details.
33949+ *
33950+ * You should have received a copy of the GNU General Public License
523b37e3 33951+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 33952+ */
33953+
33954+/*
33955+ * policies for selecting one among multiple writable branches
33956+ */
33957+
33958+#include <linux/statfs.h>
33959+#include "aufs.h"
33960+
33961+/* subset of cpup_attr() */
33962+static noinline_for_stack
33963+int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
33964+{
33965+ int err, sbits;
33966+ struct iattr ia;
33967+ struct inode *h_isrc;
33968+
5527c038 33969+ h_isrc = d_inode(h_src);
1facf9fc 33970+ ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
33971+ ia.ia_mode = h_isrc->i_mode;
33972+ ia.ia_uid = h_isrc->i_uid;
33973+ ia.ia_gid = h_isrc->i_gid;
33974+ sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
5527c038 33975+ au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
523b37e3
AM
33976+ /* no delegation since it is just created */
33977+ err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
1facf9fc 33978+
33979+ /* is this nfs only? */
33980+ if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
33981+ ia.ia_valid = ATTR_FORCE | ATTR_MODE;
33982+ ia.ia_mode = h_isrc->i_mode;
523b37e3 33983+ err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
1facf9fc 33984+ }
33985+
33986+ return err;
33987+}
33988+
33989+#define AuCpdown_PARENT_OPQ 1
33990+#define AuCpdown_WHED (1 << 1)
33991+#define AuCpdown_MADE_DIR (1 << 2)
33992+#define AuCpdown_DIROPQ (1 << 3)
33993+#define au_ftest_cpdown(flags, name) ((flags) & AuCpdown_##name)
7f207e10
AM
33994+#define au_fset_cpdown(flags, name) \
33995+ do { (flags) |= AuCpdown_##name; } while (0)
33996+#define au_fclr_cpdown(flags, name) \
33997+ do { (flags) &= ~AuCpdown_##name; } while (0)
1facf9fc 33998+
1facf9fc 33999+static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
c2b27bf2 34000+ unsigned int *flags)
1facf9fc 34001+{
34002+ int err;
34003+ struct dentry *opq_dentry;
34004+
34005+ opq_dentry = au_diropq_create(dentry, bdst);
34006+ err = PTR_ERR(opq_dentry);
34007+ if (IS_ERR(opq_dentry))
34008+ goto out;
34009+ dput(opq_dentry);
c2b27bf2 34010+ au_fset_cpdown(*flags, DIROPQ);
1facf9fc 34011+
4f0767ce 34012+out:
1facf9fc 34013+ return err;
34014+}
34015+
34016+static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
34017+ struct inode *dir, aufs_bindex_t bdst)
34018+{
34019+ int err;
34020+ struct path h_path;
34021+ struct au_branch *br;
34022+
34023+ br = au_sbr(dentry->d_sb, bdst);
34024+ h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
34025+ err = PTR_ERR(h_path.dentry);
34026+ if (IS_ERR(h_path.dentry))
34027+ goto out;
34028+
34029+ err = 0;
5527c038 34030+ if (d_is_positive(h_path.dentry)) {
86dc4139 34031+ h_path.mnt = au_br_mnt(br);
1facf9fc 34032+ err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
34033+ dentry);
34034+ }
34035+ dput(h_path.dentry);
34036+
4f0767ce 34037+out:
1facf9fc 34038+ return err;
34039+}
34040+
34041+static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 34042+ struct au_pin *pin,
1facf9fc 34043+ struct dentry *h_parent, void *arg)
34044+{
34045+ int err, rerr;
5afbbe0d 34046+ aufs_bindex_t bopq, btop;
1facf9fc 34047+ struct path h_path;
34048+ struct dentry *parent;
34049+ struct inode *h_dir, *h_inode, *inode, *dir;
c2b27bf2 34050+ unsigned int *flags = arg;
1facf9fc 34051+
5afbbe0d 34052+ btop = au_dbtop(dentry);
1facf9fc 34053+ /* dentry is di-locked */
34054+ parent = dget_parent(dentry);
5527c038
JR
34055+ dir = d_inode(parent);
34056+ h_dir = d_inode(h_parent);
1facf9fc 34057+ AuDebugOn(h_dir != au_h_iptr(dir, bdst));
34058+ IMustLock(h_dir);
34059+
86dc4139 34060+ err = au_lkup_neg(dentry, bdst, /*wh*/0);
1facf9fc 34061+ if (unlikely(err < 0))
34062+ goto out;
34063+ h_path.dentry = au_h_dptr(dentry, bdst);
34064+ h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
cd7a4cd9 34065+ err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path, 0755);
1facf9fc 34066+ if (unlikely(err))
34067+ goto out_put;
c2b27bf2 34068+ au_fset_cpdown(*flags, MADE_DIR);
1facf9fc 34069+
1facf9fc 34070+ bopq = au_dbdiropq(dentry);
c2b27bf2
AM
34071+ au_fclr_cpdown(*flags, WHED);
34072+ au_fclr_cpdown(*flags, DIROPQ);
1facf9fc 34073+ if (au_dbwh(dentry) == bdst)
c2b27bf2
AM
34074+ au_fset_cpdown(*flags, WHED);
34075+ if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
34076+ au_fset_cpdown(*flags, PARENT_OPQ);
5527c038 34077+ h_inode = d_inode(h_path.dentry);
febd17d6 34078+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
c2b27bf2
AM
34079+ if (au_ftest_cpdown(*flags, WHED)) {
34080+ err = au_cpdown_dir_opq(dentry, bdst, flags);
1facf9fc 34081+ if (unlikely(err)) {
febd17d6 34082+ inode_unlock(h_inode);
1facf9fc 34083+ goto out_dir;
34084+ }
34085+ }
34086+
5afbbe0d 34087+ err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
febd17d6 34088+ inode_unlock(h_inode);
1facf9fc 34089+ if (unlikely(err))
34090+ goto out_opq;
34091+
c2b27bf2 34092+ if (au_ftest_cpdown(*flags, WHED)) {
1facf9fc 34093+ err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
34094+ if (unlikely(err))
34095+ goto out_opq;
34096+ }
34097+
5527c038 34098+ inode = d_inode(dentry);
5afbbe0d
AM
34099+ if (au_ibbot(inode) < bdst)
34100+ au_set_ibbot(inode, bdst);
1facf9fc 34101+ au_set_h_iptr(inode, bdst, au_igrab(h_inode),
34102+ au_hi_flags(inode, /*isdir*/1));
076b876e 34103+ au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
1facf9fc 34104+ goto out; /* success */
34105+
34106+ /* revert */
4f0767ce 34107+out_opq:
c2b27bf2 34108+ if (au_ftest_cpdown(*flags, DIROPQ)) {
febd17d6 34109+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
1facf9fc 34110+ rerr = au_diropq_remove(dentry, bdst);
febd17d6 34111+ inode_unlock(h_inode);
1facf9fc 34112+ if (unlikely(rerr)) {
523b37e3
AM
34113+ AuIOErr("failed removing diropq for %pd b%d (%d)\n",
34114+ dentry, bdst, rerr);
1facf9fc 34115+ err = -EIO;
34116+ goto out;
34117+ }
34118+ }
4f0767ce 34119+out_dir:
c2b27bf2 34120+ if (au_ftest_cpdown(*flags, MADE_DIR)) {
1facf9fc 34121+ rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
34122+ if (unlikely(rerr)) {
523b37e3
AM
34123+ AuIOErr("failed removing %pd b%d (%d)\n",
34124+ dentry, bdst, rerr);
1facf9fc 34125+ err = -EIO;
34126+ }
34127+ }
4f0767ce 34128+out_put:
1facf9fc 34129+ au_set_h_dptr(dentry, bdst, NULL);
5afbbe0d
AM
34130+ if (au_dbbot(dentry) == bdst)
34131+ au_update_dbbot(dentry);
4f0767ce 34132+out:
1facf9fc 34133+ dput(parent);
34134+ return err;
34135+}
34136+
34137+int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
34138+{
34139+ int err;
c2b27bf2 34140+ unsigned int flags;
1facf9fc 34141+
c2b27bf2
AM
34142+ flags = 0;
34143+ err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
1facf9fc 34144+
34145+ return err;
34146+}
34147+
34148+/* ---------------------------------------------------------------------- */
34149+
34150+/* policies for create */
34151+
c2b27bf2 34152+int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
4a4d8108
AM
34153+{
34154+ int err, i, j, ndentry;
34155+ aufs_bindex_t bopq;
34156+ struct au_dcsub_pages dpages;
34157+ struct au_dpage *dpage;
34158+ struct dentry **dentries, *parent, *d;
34159+
34160+ err = au_dpages_init(&dpages, GFP_NOFS);
34161+ if (unlikely(err))
34162+ goto out;
34163+ parent = dget_parent(dentry);
027c5e7a 34164+ err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
4a4d8108
AM
34165+ if (unlikely(err))
34166+ goto out_free;
34167+
34168+ err = bindex;
34169+ for (i = 0; i < dpages.ndpage; i++) {
34170+ dpage = dpages.dpages + i;
34171+ dentries = dpage->dentries;
34172+ ndentry = dpage->ndentry;
34173+ for (j = 0; j < ndentry; j++) {
34174+ d = dentries[j];
34175+ di_read_lock_parent2(d, !AuLock_IR);
34176+ bopq = au_dbdiropq(d);
34177+ di_read_unlock(d, !AuLock_IR);
34178+ if (bopq >= 0 && bopq < err)
34179+ err = bopq;
34180+ }
34181+ }
34182+
34183+out_free:
34184+ dput(parent);
34185+ au_dpages_free(&dpages);
34186+out:
34187+ return err;
34188+}
34189+
1facf9fc 34190+static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
34191+{
34192+ for (; bindex >= 0; bindex--)
34193+ if (!au_br_rdonly(au_sbr(sb, bindex)))
34194+ return bindex;
34195+ return -EROFS;
34196+}
34197+
34198+/* top down parent */
392086de
AM
34199+static int au_wbr_create_tdp(struct dentry *dentry,
34200+ unsigned int flags __maybe_unused)
1facf9fc 34201+{
34202+ int err;
5afbbe0d 34203+ aufs_bindex_t btop, bindex;
1facf9fc 34204+ struct super_block *sb;
34205+ struct dentry *parent, *h_parent;
34206+
34207+ sb = dentry->d_sb;
5afbbe0d
AM
34208+ btop = au_dbtop(dentry);
34209+ err = btop;
34210+ if (!au_br_rdonly(au_sbr(sb, btop)))
1facf9fc 34211+ goto out;
34212+
34213+ err = -EROFS;
34214+ parent = dget_parent(dentry);
5afbbe0d 34215+ for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
1facf9fc 34216+ h_parent = au_h_dptr(parent, bindex);
5527c038 34217+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 34218+ continue;
34219+
34220+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
34221+ err = bindex;
34222+ break;
34223+ }
34224+ }
34225+ dput(parent);
34226+
34227+ /* bottom up here */
4a4d8108 34228+ if (unlikely(err < 0)) {
5afbbe0d 34229+ err = au_wbr_bu(sb, btop - 1);
4a4d8108
AM
34230+ if (err >= 0)
34231+ err = au_wbr_nonopq(dentry, err);
34232+ }
1facf9fc 34233+
4f0767ce 34234+out:
1facf9fc 34235+ AuDbg("b%d\n", err);
34236+ return err;
34237+}
34238+
34239+/* ---------------------------------------------------------------------- */
34240+
34241+/* an exception for the policy other than tdp */
34242+static int au_wbr_create_exp(struct dentry *dentry)
34243+{
34244+ int err;
34245+ aufs_bindex_t bwh, bdiropq;
34246+ struct dentry *parent;
34247+
34248+ err = -1;
34249+ bwh = au_dbwh(dentry);
34250+ parent = dget_parent(dentry);
34251+ bdiropq = au_dbdiropq(parent);
34252+ if (bwh >= 0) {
34253+ if (bdiropq >= 0)
34254+ err = min(bdiropq, bwh);
34255+ else
34256+ err = bwh;
34257+ AuDbg("%d\n", err);
34258+ } else if (bdiropq >= 0) {
34259+ err = bdiropq;
34260+ AuDbg("%d\n", err);
34261+ }
34262+ dput(parent);
34263+
4a4d8108
AM
34264+ if (err >= 0)
34265+ err = au_wbr_nonopq(dentry, err);
34266+
1facf9fc 34267+ if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
34268+ err = -1;
34269+
34270+ AuDbg("%d\n", err);
34271+ return err;
34272+}
34273+
34274+/* ---------------------------------------------------------------------- */
34275+
34276+/* round robin */
34277+static int au_wbr_create_init_rr(struct super_block *sb)
34278+{
34279+ int err;
34280+
5afbbe0d 34281+ err = au_wbr_bu(sb, au_sbbot(sb));
1facf9fc 34282+ atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
dece6358 34283+ /* smp_mb(); */
1facf9fc 34284+
34285+ AuDbg("b%d\n", err);
34286+ return err;
34287+}
34288+
392086de 34289+static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
1facf9fc 34290+{
34291+ int err, nbr;
34292+ unsigned int u;
5afbbe0d 34293+ aufs_bindex_t bindex, bbot;
1facf9fc 34294+ struct super_block *sb;
34295+ atomic_t *next;
34296+
34297+ err = au_wbr_create_exp(dentry);
34298+ if (err >= 0)
34299+ goto out;
34300+
34301+ sb = dentry->d_sb;
34302+ next = &au_sbi(sb)->si_wbr_rr_next;
5afbbe0d
AM
34303+ bbot = au_sbbot(sb);
34304+ nbr = bbot + 1;
34305+ for (bindex = 0; bindex <= bbot; bindex++) {
392086de 34306+ if (!au_ftest_wbr(flags, DIR)) {
1facf9fc 34307+ err = atomic_dec_return(next) + 1;
34308+ /* modulo for 0 is meaningless */
34309+ if (unlikely(!err))
34310+ err = atomic_dec_return(next) + 1;
34311+ } else
34312+ err = atomic_read(next);
34313+ AuDbg("%d\n", err);
34314+ u = err;
34315+ err = u % nbr;
34316+ AuDbg("%d\n", err);
34317+ if (!au_br_rdonly(au_sbr(sb, err)))
34318+ break;
34319+ err = -EROFS;
34320+ }
34321+
4a4d8108
AM
34322+ if (err >= 0)
34323+ err = au_wbr_nonopq(dentry, err);
34324+
4f0767ce 34325+out:
1facf9fc 34326+ AuDbg("%d\n", err);
34327+ return err;
34328+}
34329+
34330+/* ---------------------------------------------------------------------- */
34331+
34332+/* most free space */
392086de 34333+static void au_mfs(struct dentry *dentry, struct dentry *parent)
1facf9fc 34334+{
34335+ struct super_block *sb;
34336+ struct au_branch *br;
34337+ struct au_wbr_mfs *mfs;
392086de 34338+ struct dentry *h_parent;
5afbbe0d 34339+ aufs_bindex_t bindex, bbot;
1facf9fc 34340+ int err;
34341+ unsigned long long b, bavail;
7f207e10 34342+ struct path h_path;
1facf9fc 34343+ /* reduce the stack usage */
34344+ struct kstatfs *st;
34345+
34346+ st = kmalloc(sizeof(*st), GFP_NOFS);
34347+ if (unlikely(!st)) {
34348+ AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
34349+ return;
34350+ }
34351+
34352+ bavail = 0;
34353+ sb = dentry->d_sb;
34354+ mfs = &au_sbi(sb)->si_wbr_mfs;
dece6358 34355+ MtxMustLock(&mfs->mfs_lock);
1facf9fc 34356+ mfs->mfs_bindex = -EROFS;
34357+ mfs->mfsrr_bytes = 0;
392086de
AM
34358+ if (!parent) {
34359+ bindex = 0;
5afbbe0d 34360+ bbot = au_sbbot(sb);
392086de 34361+ } else {
5afbbe0d
AM
34362+ bindex = au_dbtop(parent);
34363+ bbot = au_dbtaildir(parent);
392086de
AM
34364+ }
34365+
5afbbe0d 34366+ for (; bindex <= bbot; bindex++) {
392086de
AM
34367+ if (parent) {
34368+ h_parent = au_h_dptr(parent, bindex);
5527c038 34369+ if (!h_parent || d_is_negative(h_parent))
392086de
AM
34370+ continue;
34371+ }
1facf9fc 34372+ br = au_sbr(sb, bindex);
34373+ if (au_br_rdonly(br))
34374+ continue;
34375+
34376+ /* sb->s_root for NFS is unreliable */
86dc4139 34377+ h_path.mnt = au_br_mnt(br);
7f207e10
AM
34378+ h_path.dentry = h_path.mnt->mnt_root;
34379+ err = vfs_statfs(&h_path, st);
1facf9fc 34380+ if (unlikely(err)) {
34381+ AuWarn1("failed statfs, b%d, %d\n", bindex, err);
34382+ continue;
34383+ }
34384+
34385+ /* when the available size is equal, select the lower one */
34386+ BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
34387+ || sizeof(b) < sizeof(st->f_bsize));
34388+ b = st->f_bavail * st->f_bsize;
34389+ br->br_wbr->wbr_bytes = b;
34390+ if (b >= bavail) {
34391+ bavail = b;
34392+ mfs->mfs_bindex = bindex;
34393+ mfs->mfs_jiffy = jiffies;
34394+ }
34395+ }
34396+
34397+ mfs->mfsrr_bytes = bavail;
34398+ AuDbg("b%d\n", mfs->mfs_bindex);
9f237c51 34399+ au_kfree_rcu(st);
1facf9fc 34400+}
34401+
392086de 34402+static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
1facf9fc 34403+{
34404+ int err;
392086de 34405+ struct dentry *parent;
1facf9fc 34406+ struct super_block *sb;
34407+ struct au_wbr_mfs *mfs;
34408+
34409+ err = au_wbr_create_exp(dentry);
34410+ if (err >= 0)
34411+ goto out;
34412+
34413+ sb = dentry->d_sb;
392086de
AM
34414+ parent = NULL;
34415+ if (au_ftest_wbr(flags, PARENT))
34416+ parent = dget_parent(dentry);
1facf9fc 34417+ mfs = &au_sbi(sb)->si_wbr_mfs;
34418+ mutex_lock(&mfs->mfs_lock);
34419+ if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34420+ || mfs->mfs_bindex < 0
34421+ || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
392086de 34422+ au_mfs(dentry, parent);
1facf9fc 34423+ mutex_unlock(&mfs->mfs_lock);
34424+ err = mfs->mfs_bindex;
392086de 34425+ dput(parent);
1facf9fc 34426+
4a4d8108
AM
34427+ if (err >= 0)
34428+ err = au_wbr_nonopq(dentry, err);
34429+
4f0767ce 34430+out:
1facf9fc 34431+ AuDbg("b%d\n", err);
34432+ return err;
34433+}
34434+
34435+static int au_wbr_create_init_mfs(struct super_block *sb)
34436+{
34437+ struct au_wbr_mfs *mfs;
34438+
34439+ mfs = &au_sbi(sb)->si_wbr_mfs;
34440+ mutex_init(&mfs->mfs_lock);
34441+ mfs->mfs_jiffy = 0;
34442+ mfs->mfs_bindex = -EROFS;
34443+
34444+ return 0;
34445+}
34446+
34447+static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
34448+{
34449+ mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
34450+ return 0;
34451+}
34452+
34453+/* ---------------------------------------------------------------------- */
34454+
f2c43d5f
AM
34455+/* top down regardless parent, and then mfs */
34456+static int au_wbr_create_tdmfs(struct dentry *dentry,
34457+ unsigned int flags __maybe_unused)
34458+{
34459+ int err;
34460+ aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
34461+ unsigned long long watermark;
34462+ struct super_block *sb;
34463+ struct au_wbr_mfs *mfs;
34464+ struct au_branch *br;
34465+ struct dentry *parent;
34466+
34467+ sb = dentry->d_sb;
34468+ mfs = &au_sbi(sb)->si_wbr_mfs;
34469+ mutex_lock(&mfs->mfs_lock);
34470+ if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34471+ || mfs->mfs_bindex < 0)
34472+ au_mfs(dentry, /*parent*/NULL);
34473+ watermark = mfs->mfsrr_watermark;
34474+ bmfs = mfs->mfs_bindex;
34475+ mutex_unlock(&mfs->mfs_lock);
34476+
34477+ /* another style of au_wbr_create_exp() */
34478+ bwh = au_dbwh(dentry);
34479+ parent = dget_parent(dentry);
34480+ btail = au_dbtaildir(parent);
34481+ if (bwh >= 0 && bwh < btail)
34482+ btail = bwh;
34483+
34484+ err = au_wbr_nonopq(dentry, btail);
34485+ if (unlikely(err < 0))
34486+ goto out;
34487+ btail = err;
34488+ bfound = -1;
34489+ for (bindex = 0; bindex <= btail; bindex++) {
34490+ br = au_sbr(sb, bindex);
34491+ if (au_br_rdonly(br))
34492+ continue;
34493+ if (br->br_wbr->wbr_bytes > watermark) {
34494+ bfound = bindex;
34495+ break;
34496+ }
34497+ }
34498+ err = bfound;
34499+ if (err < 0)
34500+ err = bmfs;
34501+
34502+out:
34503+ dput(parent);
34504+ AuDbg("b%d\n", err);
34505+ return err;
34506+}
34507+
34508+/* ---------------------------------------------------------------------- */
34509+
1facf9fc 34510+/* most free space and then round robin */
392086de 34511+static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
1facf9fc 34512+{
34513+ int err;
34514+ struct au_wbr_mfs *mfs;
34515+
392086de 34516+ err = au_wbr_create_mfs(dentry, flags);
1facf9fc 34517+ if (err >= 0) {
34518+ mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
dece6358 34519+ mutex_lock(&mfs->mfs_lock);
1facf9fc 34520+ if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
392086de 34521+ err = au_wbr_create_rr(dentry, flags);
dece6358 34522+ mutex_unlock(&mfs->mfs_lock);
1facf9fc 34523+ }
34524+
34525+ AuDbg("b%d\n", err);
34526+ return err;
34527+}
34528+
34529+static int au_wbr_create_init_mfsrr(struct super_block *sb)
34530+{
34531+ int err;
34532+
34533+ au_wbr_create_init_mfs(sb); /* ignore */
34534+ err = au_wbr_create_init_rr(sb);
34535+
34536+ return err;
34537+}
34538+
34539+/* ---------------------------------------------------------------------- */
34540+
34541+/* top down parent and most free space */
392086de 34542+static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
1facf9fc 34543+{
34544+ int err, e2;
34545+ unsigned long long b;
5afbbe0d 34546+ aufs_bindex_t bindex, btop, bbot;
1facf9fc 34547+ struct super_block *sb;
34548+ struct dentry *parent, *h_parent;
34549+ struct au_branch *br;
34550+
392086de 34551+ err = au_wbr_create_tdp(dentry, flags);
1facf9fc 34552+ if (unlikely(err < 0))
34553+ goto out;
34554+ parent = dget_parent(dentry);
5afbbe0d
AM
34555+ btop = au_dbtop(parent);
34556+ bbot = au_dbtaildir(parent);
34557+ if (btop == bbot)
1facf9fc 34558+ goto out_parent; /* success */
34559+
392086de 34560+ e2 = au_wbr_create_mfs(dentry, flags);
1facf9fc 34561+ if (e2 < 0)
34562+ goto out_parent; /* success */
34563+
34564+ /* when the available size is equal, select upper one */
34565+ sb = dentry->d_sb;
34566+ br = au_sbr(sb, err);
34567+ b = br->br_wbr->wbr_bytes;
34568+ AuDbg("b%d, %llu\n", err, b);
34569+
5afbbe0d 34570+ for (bindex = btop; bindex <= bbot; bindex++) {
1facf9fc 34571+ h_parent = au_h_dptr(parent, bindex);
5527c038 34572+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 34573+ continue;
34574+
34575+ br = au_sbr(sb, bindex);
34576+ if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
34577+ b = br->br_wbr->wbr_bytes;
34578+ err = bindex;
34579+ AuDbg("b%d, %llu\n", err, b);
34580+ }
34581+ }
34582+
4a4d8108
AM
34583+ if (err >= 0)
34584+ err = au_wbr_nonopq(dentry, err);
34585+
4f0767ce 34586+out_parent:
1facf9fc 34587+ dput(parent);
4f0767ce 34588+out:
1facf9fc 34589+ AuDbg("b%d\n", err);
34590+ return err;
34591+}
34592+
34593+/* ---------------------------------------------------------------------- */
34594+
392086de
AM
34595+/*
34596+ * - top down parent
34597+ * - most free space with parent
34598+ * - most free space round-robin regardless parent
34599+ */
34600+static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
34601+{
34602+ int err;
34603+ unsigned long long watermark;
34604+ struct super_block *sb;
34605+ struct au_branch *br;
34606+ struct au_wbr_mfs *mfs;
34607+
34608+ err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
34609+ if (unlikely(err < 0))
34610+ goto out;
34611+
34612+ sb = dentry->d_sb;
34613+ br = au_sbr(sb, err);
34614+ mfs = &au_sbi(sb)->si_wbr_mfs;
34615+ mutex_lock(&mfs->mfs_lock);
34616+ watermark = mfs->mfsrr_watermark;
34617+ mutex_unlock(&mfs->mfs_lock);
34618+ if (br->br_wbr->wbr_bytes < watermark)
34619+ /* regardless the parent dir */
34620+ err = au_wbr_create_mfsrr(dentry, flags);
34621+
34622+out:
34623+ AuDbg("b%d\n", err);
34624+ return err;
34625+}
34626+
34627+/* ---------------------------------------------------------------------- */
34628+
1facf9fc 34629+/* policies for copyup */
34630+
34631+/* top down parent */
34632+static int au_wbr_copyup_tdp(struct dentry *dentry)
34633+{
392086de 34634+ return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
1facf9fc 34635+}
34636+
34637+/* bottom up parent */
34638+static int au_wbr_copyup_bup(struct dentry *dentry)
34639+{
34640+ int err;
5afbbe0d 34641+ aufs_bindex_t bindex, btop;
1facf9fc 34642+ struct dentry *parent, *h_parent;
34643+ struct super_block *sb;
34644+
34645+ err = -EROFS;
34646+ sb = dentry->d_sb;
34647+ parent = dget_parent(dentry);
5afbbe0d
AM
34648+ btop = au_dbtop(parent);
34649+ for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
1facf9fc 34650+ h_parent = au_h_dptr(parent, bindex);
5527c038 34651+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 34652+ continue;
34653+
34654+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
34655+ err = bindex;
34656+ break;
34657+ }
34658+ }
34659+ dput(parent);
34660+
34661+ /* bottom up here */
34662+ if (unlikely(err < 0))
5afbbe0d 34663+ err = au_wbr_bu(sb, btop - 1);
1facf9fc 34664+
34665+ AuDbg("b%d\n", err);
34666+ return err;
34667+}
34668+
34669+/* bottom up */
5afbbe0d 34670+int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
1facf9fc 34671+{
34672+ int err;
34673+
5afbbe0d 34674+ err = au_wbr_bu(dentry->d_sb, btop);
4a4d8108 34675+ AuDbg("b%d\n", err);
5afbbe0d 34676+ if (err > btop)
4a4d8108 34677+ err = au_wbr_nonopq(dentry, err);
1facf9fc 34678+
34679+ AuDbg("b%d\n", err);
34680+ return err;
34681+}
34682+
076b876e
AM
34683+static int au_wbr_copyup_bu(struct dentry *dentry)
34684+{
34685+ int err;
5afbbe0d 34686+ aufs_bindex_t btop;
076b876e 34687+
5afbbe0d
AM
34688+ btop = au_dbtop(dentry);
34689+ err = au_wbr_do_copyup_bu(dentry, btop);
076b876e
AM
34690+ return err;
34691+}
34692+
1facf9fc 34693+/* ---------------------------------------------------------------------- */
34694+
34695+struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
34696+ [AuWbrCopyup_TDP] = {
34697+ .copyup = au_wbr_copyup_tdp
34698+ },
34699+ [AuWbrCopyup_BUP] = {
34700+ .copyup = au_wbr_copyup_bup
34701+ },
34702+ [AuWbrCopyup_BU] = {
34703+ .copyup = au_wbr_copyup_bu
34704+ }
34705+};
34706+
34707+struct au_wbr_create_operations au_wbr_create_ops[] = {
34708+ [AuWbrCreate_TDP] = {
34709+ .create = au_wbr_create_tdp
34710+ },
34711+ [AuWbrCreate_RR] = {
34712+ .create = au_wbr_create_rr,
34713+ .init = au_wbr_create_init_rr
34714+ },
34715+ [AuWbrCreate_MFS] = {
34716+ .create = au_wbr_create_mfs,
34717+ .init = au_wbr_create_init_mfs,
34718+ .fin = au_wbr_create_fin_mfs
34719+ },
34720+ [AuWbrCreate_MFSV] = {
34721+ .create = au_wbr_create_mfs,
34722+ .init = au_wbr_create_init_mfs,
34723+ .fin = au_wbr_create_fin_mfs
34724+ },
34725+ [AuWbrCreate_MFSRR] = {
34726+ .create = au_wbr_create_mfsrr,
34727+ .init = au_wbr_create_init_mfsrr,
34728+ .fin = au_wbr_create_fin_mfs
34729+ },
34730+ [AuWbrCreate_MFSRRV] = {
34731+ .create = au_wbr_create_mfsrr,
34732+ .init = au_wbr_create_init_mfsrr,
34733+ .fin = au_wbr_create_fin_mfs
34734+ },
f2c43d5f
AM
34735+ [AuWbrCreate_TDMFS] = {
34736+ .create = au_wbr_create_tdmfs,
34737+ .init = au_wbr_create_init_mfs,
34738+ .fin = au_wbr_create_fin_mfs
34739+ },
34740+ [AuWbrCreate_TDMFSV] = {
34741+ .create = au_wbr_create_tdmfs,
34742+ .init = au_wbr_create_init_mfs,
34743+ .fin = au_wbr_create_fin_mfs
34744+ },
1facf9fc 34745+ [AuWbrCreate_PMFS] = {
34746+ .create = au_wbr_create_pmfs,
34747+ .init = au_wbr_create_init_mfs,
34748+ .fin = au_wbr_create_fin_mfs
34749+ },
34750+ [AuWbrCreate_PMFSV] = {
34751+ .create = au_wbr_create_pmfs,
34752+ .init = au_wbr_create_init_mfs,
34753+ .fin = au_wbr_create_fin_mfs
392086de
AM
34754+ },
34755+ [AuWbrCreate_PMFSRR] = {
34756+ .create = au_wbr_create_pmfsrr,
34757+ .init = au_wbr_create_init_mfsrr,
34758+ .fin = au_wbr_create_fin_mfs
34759+ },
34760+ [AuWbrCreate_PMFSRRV] = {
34761+ .create = au_wbr_create_pmfsrr,
34762+ .init = au_wbr_create_init_mfsrr,
34763+ .fin = au_wbr_create_fin_mfs
1facf9fc 34764+ }
34765+};
7f207e10 34766diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
eca34b5c 34767--- /usr/share/empty/fs/aufs/whout.c 1970-01-01 01:00:00.000000000 +0100
46016270 34768+++ linux/fs/aufs/whout.c 2021-06-30 21:35:11.397206648 +0200
0b2a12c6 34769@@ -0,0 +1,1070 @@
cd7a4cd9 34770+// SPDX-License-Identifier: GPL-2.0
1facf9fc 34771+/*
d58c55f2 34772+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 34773+ *
34774+ * This program, aufs is free software; you can redistribute it and/or modify
34775+ * it under the terms of the GNU General Public License as published by
34776+ * the Free Software Foundation; either version 2 of the License, or
34777+ * (at your option) any later version.
dece6358
AM
34778+ *
34779+ * This program is distributed in the hope that it will be useful,
34780+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
34781+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34782+ * GNU General Public License for more details.
34783+ *
34784+ * You should have received a copy of the GNU General Public License
523b37e3 34785+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 34786+ */
34787+
34788+/*
34789+ * whiteout for logical deletion and opaque directory
34790+ */
34791+
1facf9fc 34792+#include "aufs.h"
34793+
cd7a4cd9 34794+#define WH_MASK 0444
1facf9fc 34795+
34796+/*
34797+ * If a directory contains this file, then it is opaque. We start with the
34798+ * .wh. flag so that it is blocked by lookup.
34799+ */
0c3ec466
AM
34800+static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
34801+ sizeof(AUFS_WH_DIROPQ) - 1);
1facf9fc 34802+
34803+/*
34804+ * generate whiteout name, which is NOT terminated by NULL.
34805+ * @name: original d_name.name
34806+ * @len: original d_name.len
34807+ * @wh: whiteout qstr
34808+ * returns zero when succeeds, otherwise error.
34809+ * succeeded value as wh->name should be freed by kfree().
34810+ */
34811+int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
34812+{
34813+ char *p;
34814+
34815+ if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
34816+ return -ENAMETOOLONG;
34817+
34818+ wh->len = name->len + AUFS_WH_PFX_LEN;
34819+ p = kmalloc(wh->len, GFP_NOFS);
34820+ wh->name = p;
34821+ if (p) {
34822+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
34823+ memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
34824+ /* smp_mb(); */
34825+ return 0;
34826+ }
34827+ return -ENOMEM;
34828+}
34829+
34830+/* ---------------------------------------------------------------------- */
34831+
34832+/*
34833+ * test if the @wh_name exists under @h_parent.
34834+ * @try_sio specifies the necessary of super-io.
34835+ */
0b2a12c6
JR
34836+int au_wh_test(struct user_namespace *h_userns, struct dentry *h_parent,
34837+ struct qstr *wh_name, int try_sio)
1facf9fc 34838+{
34839+ int err;
34840+ struct dentry *wh_dentry;
1facf9fc 34841+
1facf9fc 34842+ if (!try_sio)
b4510431 34843+ wh_dentry = vfsub_lkup_one(wh_name, h_parent);
1facf9fc 34844+ else
0b2a12c6 34845+ wh_dentry = au_sio_lkup_one(h_userns, wh_name, h_parent);
1facf9fc 34846+ err = PTR_ERR(wh_dentry);
2000de60
JR
34847+ if (IS_ERR(wh_dentry)) {
34848+ if (err == -ENAMETOOLONG)
34849+ err = 0;
1facf9fc 34850+ goto out;
2000de60 34851+ }
1facf9fc 34852+
34853+ err = 0;
5527c038 34854+ if (d_is_negative(wh_dentry))
1facf9fc 34855+ goto out_wh; /* success */
34856+
34857+ err = 1;
7e9cd9fe 34858+ if (d_is_reg(wh_dentry))
1facf9fc 34859+ goto out_wh; /* success */
34860+
34861+ err = -EIO;
523b37e3 34862+ AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
5527c038 34863+ wh_dentry, d_inode(wh_dentry)->i_mode);
1facf9fc 34864+
4f0767ce 34865+out_wh:
1facf9fc 34866+ dput(wh_dentry);
4f0767ce 34867+out:
1facf9fc 34868+ return err;
34869+}
34870+
34871+/*
34872+ * test if the @h_dentry sets opaque or not.
34873+ */
0b2a12c6 34874+int au_diropq_test(struct user_namespace *h_userns, struct dentry *h_dentry)
1facf9fc 34875+{
34876+ int err;
34877+ struct inode *h_dir;
34878+
5527c038 34879+ h_dir = d_inode(h_dentry);
0b2a12c6
JR
34880+ err = au_wh_test(h_userns, h_dentry, &diropq_name,
34881+ au_test_h_perm_sio(h_userns, h_dir, MAY_EXEC));
1facf9fc 34882+ return err;
34883+}
34884+
34885+/*
34886+ * returns a negative dentry whose name is unique and temporary.
34887+ */
34888+struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
34889+ struct qstr *prefix)
34890+{
1facf9fc 34891+ struct dentry *dentry;
34892+ int i;
027c5e7a 34893+ char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
4a4d8108 34894+ *name, *p;
027c5e7a 34895+ /* strict atomic_t is unnecessary here */
1facf9fc 34896+ static unsigned short cnt;
34897+ struct qstr qs;
0b2a12c6 34898+ struct user_namespace *h_userns;
1facf9fc 34899+
4a4d8108
AM
34900+ BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
34901+
1facf9fc 34902+ name = defname;
027c5e7a
AM
34903+ qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
34904+ if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
1facf9fc 34905+ dentry = ERR_PTR(-ENAMETOOLONG);
4a4d8108 34906+ if (unlikely(qs.len > NAME_MAX))
1facf9fc 34907+ goto out;
34908+ dentry = ERR_PTR(-ENOMEM);
34909+ name = kmalloc(qs.len + 1, GFP_NOFS);
34910+ if (unlikely(!name))
34911+ goto out;
34912+ }
34913+
34914+ /* doubly whiteout-ed */
34915+ memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
34916+ p = name + AUFS_WH_PFX_LEN * 2;
34917+ memcpy(p, prefix->name, prefix->len);
34918+ p += prefix->len;
34919+ *p++ = '.';
4a4d8108 34920+ AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
1facf9fc 34921+
0b2a12c6 34922+ h_userns = au_br_userns(br);
1facf9fc 34923+ qs.name = name;
34924+ for (i = 0; i < 3; i++) {
b752ccd1 34925+ sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
0b2a12c6 34926+ dentry = au_sio_lkup_one(h_userns, &qs, h_parent);
5527c038 34927+ if (IS_ERR(dentry) || d_is_negative(dentry))
1facf9fc 34928+ goto out_name;
34929+ dput(dentry);
34930+ }
0c3ec466 34931+ /* pr_warn("could not get random name\n"); */
1facf9fc 34932+ dentry = ERR_PTR(-EEXIST);
34933+ AuDbg("%.*s\n", AuLNPair(&qs));
34934+ BUG();
34935+
4f0767ce 34936+out_name:
1facf9fc 34937+ if (name != defname)
9f237c51 34938+ au_kfree_try_rcu(name);
4f0767ce 34939+out:
4a4d8108 34940+ AuTraceErrPtr(dentry);
1facf9fc 34941+ return dentry;
1facf9fc 34942+}
34943+
34944+/*
34945+ * rename the @h_dentry on @br to the whiteouted temporary name.
34946+ */
34947+int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
34948+{
34949+ int err;
34950+ struct path h_path = {
86dc4139 34951+ .mnt = au_br_mnt(br)
1facf9fc 34952+ };
523b37e3 34953+ struct inode *h_dir, *delegated;
1facf9fc 34954+ struct dentry *h_parent;
34955+
34956+ h_parent = h_dentry->d_parent; /* dir inode is locked */
5527c038 34957+ h_dir = d_inode(h_parent);
1facf9fc 34958+ IMustLock(h_dir);
34959+
34960+ h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
34961+ err = PTR_ERR(h_path.dentry);
34962+ if (IS_ERR(h_path.dentry))
34963+ goto out;
34964+
34965+ /* under the same dir, no need to lock_rename() */
523b37e3 34966+ delegated = NULL;
f2c43d5f
AM
34967+ err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
34968+ /*flags*/0);
1facf9fc 34969+ AuTraceErr(err);
523b37e3
AM
34970+ if (unlikely(err == -EWOULDBLOCK)) {
34971+ pr_warn("cannot retry for NFSv4 delegation"
34972+ " for an internal rename\n");
34973+ iput(delegated);
34974+ }
1facf9fc 34975+ dput(h_path.dentry);
34976+
4f0767ce 34977+out:
4a4d8108 34978+ AuTraceErr(err);
1facf9fc 34979+ return err;
34980+}
34981+
34982+/* ---------------------------------------------------------------------- */
34983+/*
34984+ * functions for removing a whiteout
34985+ */
34986+
34987+static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
34988+{
523b37e3
AM
34989+ int err, force;
34990+ struct inode *delegated;
1facf9fc 34991+
34992+ /*
34993+ * forces superio when the dir has a sticky bit.
34994+ * this may be a violation of unix fs semantics.
34995+ */
34996+ force = (h_dir->i_mode & S_ISVTX)
5527c038 34997+ && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
523b37e3
AM
34998+ delegated = NULL;
34999+ err = vfsub_unlink(h_dir, h_path, &delegated, force);
35000+ if (unlikely(err == -EWOULDBLOCK)) {
35001+ pr_warn("cannot retry for NFSv4 delegation"
35002+ " for an internal unlink\n");
35003+ iput(delegated);
35004+ }
35005+ return err;
1facf9fc 35006+}
35007+
35008+int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35009+ struct dentry *dentry)
35010+{
35011+ int err;
35012+
35013+ err = do_unlink_wh(h_dir, h_path);
35014+ if (!err && dentry)
35015+ au_set_dbwh(dentry, -1);
35016+
35017+ return err;
35018+}
35019+
35020+static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
35021+ struct au_branch *br)
35022+{
35023+ int err;
35024+ struct path h_path = {
86dc4139 35025+ .mnt = au_br_mnt(br)
1facf9fc 35026+ };
35027+
35028+ err = 0;
b4510431 35029+ h_path.dentry = vfsub_lkup_one(wh, h_parent);
1facf9fc 35030+ if (IS_ERR(h_path.dentry))
35031+ err = PTR_ERR(h_path.dentry);
35032+ else {
5527c038
JR
35033+ if (d_is_reg(h_path.dentry))
35034+ err = do_unlink_wh(d_inode(h_parent), &h_path);
1facf9fc 35035+ dput(h_path.dentry);
35036+ }
35037+
35038+ return err;
35039+}
35040+
35041+/* ---------------------------------------------------------------------- */
35042+/*
35043+ * initialize/clean whiteout for a branch
35044+ */
35045+
35046+static void au_wh_clean(struct inode *h_dir, struct path *whpath,
35047+ const int isdir)
35048+{
35049+ int err;
523b37e3 35050+ struct inode *delegated;
1facf9fc 35051+
5527c038 35052+ if (d_is_negative(whpath->dentry))
1facf9fc 35053+ return;
35054+
86dc4139
AM
35055+ if (isdir)
35056+ err = vfsub_rmdir(h_dir, whpath);
523b37e3
AM
35057+ else {
35058+ delegated = NULL;
35059+ err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
35060+ if (unlikely(err == -EWOULDBLOCK)) {
35061+ pr_warn("cannot retry for NFSv4 delegation"
35062+ " for an internal unlink\n");
35063+ iput(delegated);
35064+ }
35065+ }
1facf9fc 35066+ if (unlikely(err))
523b37e3
AM
35067+ pr_warn("failed removing %pd (%d), ignored.\n",
35068+ whpath->dentry, err);
1facf9fc 35069+}
35070+
35071+static int test_linkable(struct dentry *h_root)
35072+{
5527c038 35073+ struct inode *h_dir = d_inode(h_root);
1facf9fc 35074+
35075+ if (h_dir->i_op->link)
35076+ return 0;
35077+
523b37e3
AM
35078+ pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
35079+ h_root, au_sbtype(h_root->d_sb));
43982f53 35080+ return -ENOSYS; /* the branch doesn't have its ->link() */
1facf9fc 35081+}
35082+
35083+/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
35084+static int au_whdir(struct inode *h_dir, struct path *path)
35085+{
35086+ int err;
35087+
35088+ err = -EEXIST;
5527c038 35089+ if (d_is_negative(path->dentry)) {
cd7a4cd9 35090+ int mode = 0700;
1facf9fc 35091+
35092+ if (au_test_nfs(path->dentry->d_sb))
cd7a4cd9 35093+ mode |= 0111;
86dc4139 35094+ err = vfsub_mkdir(h_dir, path, mode);
2000de60 35095+ } else if (d_is_dir(path->dentry))
1facf9fc 35096+ err = 0;
35097+ else
523b37e3 35098+ pr_err("unknown %pd exists\n", path->dentry);
1facf9fc 35099+
35100+ return err;
35101+}
35102+
35103+struct au_wh_base {
35104+ const struct qstr *name;
35105+ struct dentry *dentry;
35106+};
35107+
35108+static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
35109+ struct path *h_path)
35110+{
35111+ h_path->dentry = base[AuBrWh_BASE].dentry;
35112+ au_wh_clean(h_dir, h_path, /*isdir*/0);
35113+ h_path->dentry = base[AuBrWh_PLINK].dentry;
35114+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35115+ h_path->dentry = base[AuBrWh_ORPH].dentry;
35116+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35117+}
35118+
35119+/*
35120+ * returns tri-state,
c1595e42 35121+ * minus: error, caller should print the message
acd2b654 35122+ * zero: success
c1595e42 35123+ * plus: error, caller should NOT print the message
1facf9fc 35124+ */
35125+static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
35126+ int do_plink, struct au_wh_base base[],
35127+ struct path *h_path)
35128+{
35129+ int err;
35130+ struct inode *h_dir;
35131+
5527c038 35132+ h_dir = d_inode(h_root);
1facf9fc 35133+ h_path->dentry = base[AuBrWh_BASE].dentry;
35134+ au_wh_clean(h_dir, h_path, /*isdir*/0);
35135+ h_path->dentry = base[AuBrWh_PLINK].dentry;
35136+ if (do_plink) {
35137+ err = test_linkable(h_root);
35138+ if (unlikely(err)) {
35139+ err = 1;
35140+ goto out;
35141+ }
35142+
35143+ err = au_whdir(h_dir, h_path);
35144+ if (unlikely(err))
35145+ goto out;
35146+ wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35147+ } else
35148+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35149+ h_path->dentry = base[AuBrWh_ORPH].dentry;
35150+ err = au_whdir(h_dir, h_path);
35151+ if (unlikely(err))
35152+ goto out;
35153+ wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35154+
4f0767ce 35155+out:
1facf9fc 35156+ return err;
35157+}
35158+
35159+/*
35160+ * for the moment, aufs supports the branch filesystem which does not support
35161+ * link(2). testing on FAT which does not support i_op->setattr() fully either,
35162+ * copyup failed. finally, such filesystem will not be used as the writable
35163+ * branch.
35164+ *
35165+ * returns tri-state, see above.
35166+ */
35167+static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
35168+ int do_plink, struct au_wh_base base[],
35169+ struct path *h_path)
35170+{
35171+ int err;
35172+ struct inode *h_dir;
35173+
1308ab2a 35174+ WbrWhMustWriteLock(wbr);
35175+
1facf9fc 35176+ err = test_linkable(h_root);
35177+ if (unlikely(err)) {
35178+ err = 1;
35179+ goto out;
35180+ }
35181+
35182+ /*
35183+ * todo: should this create be done in /sbin/mount.aufs helper?
35184+ */
35185+ err = -EEXIST;
5527c038
JR
35186+ h_dir = d_inode(h_root);
35187+ if (d_is_negative(base[AuBrWh_BASE].dentry)) {
86dc4139
AM
35188+ h_path->dentry = base[AuBrWh_BASE].dentry;
35189+ err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
7e9cd9fe 35190+ } else if (d_is_reg(base[AuBrWh_BASE].dentry))
1facf9fc 35191+ err = 0;
35192+ else
523b37e3 35193+ pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
1facf9fc 35194+ if (unlikely(err))
35195+ goto out;
35196+
35197+ h_path->dentry = base[AuBrWh_PLINK].dentry;
35198+ if (do_plink) {
35199+ err = au_whdir(h_dir, h_path);
35200+ if (unlikely(err))
35201+ goto out;
35202+ wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35203+ } else
35204+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35205+ wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
35206+
35207+ h_path->dentry = base[AuBrWh_ORPH].dentry;
35208+ err = au_whdir(h_dir, h_path);
35209+ if (unlikely(err))
35210+ goto out;
35211+ wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35212+
4f0767ce 35213+out:
1facf9fc 35214+ return err;
35215+}
35216+
35217+/*
35218+ * initialize the whiteout base file/dir for @br.
35219+ */
86dc4139 35220+int au_wh_init(struct au_branch *br, struct super_block *sb)
1facf9fc 35221+{
35222+ int err, i;
35223+ const unsigned char do_plink
35224+ = !!au_opt_test(au_mntflags(sb), PLINK);
1facf9fc 35225+ struct inode *h_dir;
86dc4139
AM
35226+ struct path path = br->br_path;
35227+ struct dentry *h_root = path.dentry;
1facf9fc 35228+ struct au_wbr *wbr = br->br_wbr;
35229+ static const struct qstr base_name[] = {
0c3ec466
AM
35230+ [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
35231+ sizeof(AUFS_BASE_NAME) - 1),
35232+ [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
35233+ sizeof(AUFS_PLINKDIR_NAME) - 1),
35234+ [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
35235+ sizeof(AUFS_ORPHDIR_NAME) - 1)
1facf9fc 35236+ };
35237+ struct au_wh_base base[] = {
35238+ [AuBrWh_BASE] = {
35239+ .name = base_name + AuBrWh_BASE,
35240+ .dentry = NULL
35241+ },
35242+ [AuBrWh_PLINK] = {
35243+ .name = base_name + AuBrWh_PLINK,
35244+ .dentry = NULL
35245+ },
35246+ [AuBrWh_ORPH] = {
35247+ .name = base_name + AuBrWh_ORPH,
35248+ .dentry = NULL
35249+ }
35250+ };
35251+
1308ab2a 35252+ if (wbr)
35253+ WbrWhMustWriteLock(wbr);
1facf9fc 35254+
1facf9fc 35255+ for (i = 0; i < AuBrWh_Last; i++) {
35256+ /* doubly whiteouted */
35257+ struct dentry *d;
35258+
35259+ d = au_wh_lkup(h_root, (void *)base[i].name, br);
35260+ err = PTR_ERR(d);
35261+ if (IS_ERR(d))
35262+ goto out;
35263+
35264+ base[i].dentry = d;
35265+ AuDebugOn(wbr
35266+ && wbr->wbr_wh[i]
35267+ && wbr->wbr_wh[i] != base[i].dentry);
35268+ }
35269+
35270+ if (wbr)
35271+ for (i = 0; i < AuBrWh_Last; i++) {
35272+ dput(wbr->wbr_wh[i]);
35273+ wbr->wbr_wh[i] = NULL;
35274+ }
35275+
35276+ err = 0;
1e00d052 35277+ if (!au_br_writable(br->br_perm)) {
5527c038 35278+ h_dir = d_inode(h_root);
1facf9fc 35279+ au_wh_init_ro(h_dir, base, &path);
1e00d052 35280+ } else if (!au_br_wh_linkable(br->br_perm)) {
1facf9fc 35281+ err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
35282+ if (err > 0)
35283+ goto out;
35284+ else if (err)
35285+ goto out_err;
1e00d052 35286+ } else {
1facf9fc 35287+ err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
35288+ if (err > 0)
35289+ goto out;
35290+ else if (err)
35291+ goto out_err;
1facf9fc 35292+ }
35293+ goto out; /* success */
35294+
4f0767ce 35295+out_err:
523b37e3
AM
35296+ pr_err("an error(%d) on the writable branch %pd(%s)\n",
35297+ err, h_root, au_sbtype(h_root->d_sb));
4f0767ce 35298+out:
1facf9fc 35299+ for (i = 0; i < AuBrWh_Last; i++)
35300+ dput(base[i].dentry);
35301+ return err;
35302+}
35303+
35304+/* ---------------------------------------------------------------------- */
35305+/*
35306+ * whiteouts are all hard-linked usually.
35307+ * when its link count reaches a ceiling, we create a new whiteout base
35308+ * asynchronously.
35309+ */
35310+
35311+struct reinit_br_wh {
35312+ struct super_block *sb;
35313+ struct au_branch *br;
35314+};
35315+
35316+static void reinit_br_wh(void *arg)
35317+{
35318+ int err;
35319+ aufs_bindex_t bindex;
35320+ struct path h_path;
35321+ struct reinit_br_wh *a = arg;
35322+ struct au_wbr *wbr;
523b37e3 35323+ struct inode *dir, *delegated;
1facf9fc 35324+ struct dentry *h_root;
35325+ struct au_hinode *hdir;
35326+
35327+ err = 0;
35328+ wbr = a->br->br_wbr;
35329+ /* big aufs lock */
35330+ si_noflush_write_lock(a->sb);
35331+ if (!au_br_writable(a->br->br_perm))
35332+ goto out;
35333+ bindex = au_br_index(a->sb, a->br->br_id);
35334+ if (unlikely(bindex < 0))
35335+ goto out;
35336+
1308ab2a 35337+ di_read_lock_parent(a->sb->s_root, AuLock_IR);
5527c038 35338+ dir = d_inode(a->sb->s_root);
1facf9fc 35339+ hdir = au_hi(dir, bindex);
35340+ h_root = au_h_dptr(a->sb->s_root, bindex);
86dc4139 35341+ AuDebugOn(h_root != au_br_dentry(a->br));
1facf9fc 35342+
5afbbe0d 35343+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 35344+ wbr_wh_write_lock(wbr);
35345+ err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
35346+ h_root, a->br);
35347+ if (!err) {
86dc4139
AM
35348+ h_path.dentry = wbr->wbr_whbase;
35349+ h_path.mnt = au_br_mnt(a->br);
523b37e3
AM
35350+ delegated = NULL;
35351+ err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
35352+ /*force*/0);
35353+ if (unlikely(err == -EWOULDBLOCK)) {
35354+ pr_warn("cannot retry for NFSv4 delegation"
35355+ " for an internal unlink\n");
35356+ iput(delegated);
35357+ }
1facf9fc 35358+ } else {
523b37e3 35359+ pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
1facf9fc 35360+ err = 0;
35361+ }
35362+ dput(wbr->wbr_whbase);
35363+ wbr->wbr_whbase = NULL;
35364+ if (!err)
86dc4139 35365+ err = au_wh_init(a->br, a->sb);
1facf9fc 35366+ wbr_wh_write_unlock(wbr);
5afbbe0d 35367+ au_hn_inode_unlock(hdir);
1308ab2a 35368+ di_read_unlock(a->sb->s_root, AuLock_IR);
076b876e
AM
35369+ if (!err)
35370+ au_fhsm_wrote(a->sb, bindex, /*force*/0);
1facf9fc 35371+
4f0767ce 35372+out:
1facf9fc 35373+ if (wbr)
35374+ atomic_dec(&wbr->wbr_wh_running);
acd2b654 35375+ au_lcnt_dec(&a->br->br_count);
1facf9fc 35376+ si_write_unlock(a->sb);
027c5e7a 35377+ au_nwt_done(&au_sbi(a->sb)->si_nowait);
9f237c51 35378+ au_kfree_rcu(a);
1facf9fc 35379+ if (unlikely(err))
35380+ AuIOErr("err %d\n", err);
35381+}
35382+
35383+static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
35384+{
35385+ int do_dec, wkq_err;
35386+ struct reinit_br_wh *arg;
35387+
35388+ do_dec = 1;
35389+ if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
35390+ goto out;
35391+
35392+ /* ignore ENOMEM */
35393+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
35394+ if (arg) {
35395+ /*
35396+ * dec(wh_running), kfree(arg) and dec(br_count)
35397+ * in reinit function
35398+ */
35399+ arg->sb = sb;
35400+ arg->br = br;
acd2b654 35401+ au_lcnt_inc(&br->br_count);
53392da6 35402+ wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
1facf9fc 35403+ if (unlikely(wkq_err)) {
35404+ atomic_dec(&br->br_wbr->wbr_wh_running);
acd2b654 35405+ au_lcnt_dec(&br->br_count);
9f237c51 35406+ au_kfree_rcu(arg);
1facf9fc 35407+ }
35408+ do_dec = 0;
35409+ }
35410+
4f0767ce 35411+out:
1facf9fc 35412+ if (do_dec)
35413+ atomic_dec(&br->br_wbr->wbr_wh_running);
35414+}
35415+
35416+/* ---------------------------------------------------------------------- */
35417+
35418+/*
35419+ * create the whiteout @wh.
35420+ */
35421+static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
35422+ struct dentry *wh)
35423+{
35424+ int err;
35425+ struct path h_path = {
35426+ .dentry = wh
35427+ };
35428+ struct au_branch *br;
35429+ struct au_wbr *wbr;
35430+ struct dentry *h_parent;
523b37e3 35431+ struct inode *h_dir, *delegated;
1facf9fc 35432+
35433+ h_parent = wh->d_parent; /* dir inode is locked */
5527c038 35434+ h_dir = d_inode(h_parent);
1facf9fc 35435+ IMustLock(h_dir);
35436+
35437+ br = au_sbr(sb, bindex);
86dc4139 35438+ h_path.mnt = au_br_mnt(br);
1facf9fc 35439+ wbr = br->br_wbr;
35440+ wbr_wh_read_lock(wbr);
35441+ if (wbr->wbr_whbase) {
523b37e3
AM
35442+ delegated = NULL;
35443+ err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
35444+ if (unlikely(err == -EWOULDBLOCK)) {
35445+ pr_warn("cannot retry for NFSv4 delegation"
35446+ " for an internal link\n");
35447+ iput(delegated);
35448+ }
1facf9fc 35449+ if (!err || err != -EMLINK)
35450+ goto out;
35451+
35452+ /* link count full. re-initialize br_whbase. */
35453+ kick_reinit_br_wh(sb, br);
35454+ }
35455+
35456+ /* return this error in this context */
b4510431 35457+ err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
076b876e
AM
35458+ if (!err)
35459+ au_fhsm_wrote(sb, bindex, /*force*/0);
1facf9fc 35460+
4f0767ce 35461+out:
1facf9fc 35462+ wbr_wh_read_unlock(wbr);
35463+ return err;
35464+}
35465+
35466+/* ---------------------------------------------------------------------- */
35467+
35468+/*
35469+ * create or remove the diropq.
35470+ */
35471+static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
35472+ unsigned int flags)
35473+{
35474+ struct dentry *opq_dentry, *h_dentry;
35475+ struct super_block *sb;
35476+ struct au_branch *br;
35477+ int err;
35478+
35479+ sb = dentry->d_sb;
35480+ br = au_sbr(sb, bindex);
35481+ h_dentry = au_h_dptr(dentry, bindex);
b4510431 35482+ opq_dentry = vfsub_lkup_one(&diropq_name, h_dentry);
1facf9fc 35483+ if (IS_ERR(opq_dentry))
35484+ goto out;
35485+
35486+ if (au_ftest_diropq(flags, CREATE)) {
35487+ err = link_or_create_wh(sb, bindex, opq_dentry);
35488+ if (!err) {
35489+ au_set_dbdiropq(dentry, bindex);
35490+ goto out; /* success */
35491+ }
35492+ } else {
35493+ struct path tmp = {
35494+ .dentry = opq_dentry,
86dc4139 35495+ .mnt = au_br_mnt(br)
1facf9fc 35496+ };
5527c038 35497+ err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &tmp);
1facf9fc 35498+ if (!err)
35499+ au_set_dbdiropq(dentry, -1);
35500+ }
35501+ dput(opq_dentry);
35502+ opq_dentry = ERR_PTR(err);
35503+
4f0767ce 35504+out:
1facf9fc 35505+ return opq_dentry;
35506+}
35507+
35508+struct do_diropq_args {
35509+ struct dentry **errp;
35510+ struct dentry *dentry;
35511+ aufs_bindex_t bindex;
35512+ unsigned int flags;
35513+};
35514+
35515+static void call_do_diropq(void *args)
35516+{
35517+ struct do_diropq_args *a = args;
35518+ *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
35519+}
35520+
35521+struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35522+ unsigned int flags)
35523+{
35524+ struct dentry *diropq, *h_dentry;
0b2a12c6 35525+ struct user_namespace *h_userns;
1facf9fc 35526+
0b2a12c6 35527+ h_userns = au_sbr_userns(dentry->d_sb, bindex);
1facf9fc 35528+ h_dentry = au_h_dptr(dentry, bindex);
0b2a12c6
JR
35529+ if (!au_test_h_perm_sio(h_userns, d_inode(h_dentry),
35530+ MAY_EXEC | MAY_WRITE))
1facf9fc 35531+ diropq = do_diropq(dentry, bindex, flags);
35532+ else {
35533+ int wkq_err;
35534+ struct do_diropq_args args = {
35535+ .errp = &diropq,
35536+ .dentry = dentry,
35537+ .bindex = bindex,
35538+ .flags = flags
35539+ };
35540+
35541+ wkq_err = au_wkq_wait(call_do_diropq, &args);
35542+ if (unlikely(wkq_err))
35543+ diropq = ERR_PTR(wkq_err);
35544+ }
35545+
35546+ return diropq;
35547+}
35548+
35549+/* ---------------------------------------------------------------------- */
35550+
35551+/*
35552+ * lookup whiteout dentry.
35553+ * @h_parent: lower parent dentry which must exist and be locked
35554+ * @base_name: name of dentry which will be whiteouted
35555+ * returns dentry for whiteout.
35556+ */
35557+struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35558+ struct au_branch *br)
35559+{
35560+ int err;
35561+ struct qstr wh_name;
35562+ struct dentry *wh_dentry;
35563+
35564+ err = au_wh_name_alloc(&wh_name, base_name);
35565+ wh_dentry = ERR_PTR(err);
35566+ if (!err) {
b4510431 35567+ wh_dentry = vfsub_lkup_one(&wh_name, h_parent);
9f237c51 35568+ au_kfree_try_rcu(wh_name.name);
1facf9fc 35569+ }
35570+ return wh_dentry;
35571+}
35572+
35573+/*
35574+ * link/create a whiteout for @dentry on @bindex.
35575+ */
35576+struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35577+ struct dentry *h_parent)
35578+{
35579+ struct dentry *wh_dentry;
35580+ struct super_block *sb;
35581+ int err;
35582+
35583+ sb = dentry->d_sb;
35584+ wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
5527c038 35585+ if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
1facf9fc 35586+ err = link_or_create_wh(sb, bindex, wh_dentry);
076b876e 35587+ if (!err) {
1facf9fc 35588+ au_set_dbwh(dentry, bindex);
076b876e
AM
35589+ au_fhsm_wrote(sb, bindex, /*force*/0);
35590+ } else {
1facf9fc 35591+ dput(wh_dentry);
35592+ wh_dentry = ERR_PTR(err);
35593+ }
35594+ }
35595+
35596+ return wh_dentry;
35597+}
35598+
35599+/* ---------------------------------------------------------------------- */
35600+
35601+/* Delete all whiteouts in this directory on branch bindex. */
35602+static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
35603+ aufs_bindex_t bindex, struct au_branch *br)
35604+{
35605+ int err;
35606+ unsigned long ul, n;
35607+ struct qstr wh_name;
35608+ char *p;
35609+ struct hlist_head *head;
c06a8ce3 35610+ struct au_vdir_wh *pos;
1facf9fc 35611+ struct au_vdir_destr *str;
35612+
35613+ err = -ENOMEM;
537831f9 35614+ p = (void *)__get_free_page(GFP_NOFS);
1facf9fc 35615+ wh_name.name = p;
35616+ if (unlikely(!wh_name.name))
35617+ goto out;
35618+
35619+ err = 0;
35620+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35621+ p += AUFS_WH_PFX_LEN;
35622+ n = whlist->nh_num;
35623+ head = whlist->nh_head;
35624+ for (ul = 0; !err && ul < n; ul++, head++) {
c06a8ce3
AM
35625+ hlist_for_each_entry(pos, head, wh_hash) {
35626+ if (pos->wh_bindex != bindex)
1facf9fc 35627+ continue;
35628+
c06a8ce3 35629+ str = &pos->wh_str;
1facf9fc 35630+ if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
35631+ memcpy(p, str->name, str->len);
35632+ wh_name.len = AUFS_WH_PFX_LEN + str->len;
35633+ err = unlink_wh_name(h_dentry, &wh_name, br);
35634+ if (!err)
35635+ continue;
35636+ break;
35637+ }
35638+ AuIOErr("whiteout name too long %.*s\n",
35639+ str->len, str->name);
35640+ err = -EIO;
35641+ break;
35642+ }
35643+ }
1c60b727 35644+ free_page((unsigned long)wh_name.name);
1facf9fc 35645+
4f0767ce 35646+out:
1facf9fc 35647+ return err;
35648+}
35649+
35650+struct del_wh_children_args {
35651+ int *errp;
35652+ struct dentry *h_dentry;
1308ab2a 35653+ struct au_nhash *whlist;
1facf9fc 35654+ aufs_bindex_t bindex;
35655+ struct au_branch *br;
35656+};
35657+
35658+static void call_del_wh_children(void *args)
35659+{
35660+ struct del_wh_children_args *a = args;
1308ab2a 35661+ *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
1facf9fc 35662+}
35663+
35664+/* ---------------------------------------------------------------------- */
35665+
35666+struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
35667+{
35668+ struct au_whtmp_rmdir *whtmp;
dece6358 35669+ int err;
1308ab2a 35670+ unsigned int rdhash;
dece6358
AM
35671+
35672+ SiMustAnyLock(sb);
1facf9fc 35673+
be52b249 35674+ whtmp = kzalloc(sizeof(*whtmp), gfp);
dece6358
AM
35675+ if (unlikely(!whtmp)) {
35676+ whtmp = ERR_PTR(-ENOMEM);
1facf9fc 35677+ goto out;
dece6358 35678+ }
1facf9fc 35679+
1308ab2a 35680+ /* no estimation for dir size */
35681+ rdhash = au_sbi(sb)->si_rdhash;
35682+ if (!rdhash)
35683+ rdhash = AUFS_RDHASH_DEF;
35684+ err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
35685+ if (unlikely(err)) {
9f237c51 35686+ au_kfree_rcu(whtmp);
1308ab2a 35687+ whtmp = ERR_PTR(err);
35688+ }
dece6358 35689+
4f0767ce 35690+out:
dece6358 35691+ return whtmp;
1facf9fc 35692+}
35693+
35694+void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
35695+{
027c5e7a 35696+ if (whtmp->br)
acd2b654 35697+ au_lcnt_dec(&whtmp->br->br_count);
1facf9fc 35698+ dput(whtmp->wh_dentry);
35699+ iput(whtmp->dir);
dece6358 35700+ au_nhash_wh_free(&whtmp->whlist);
9f237c51 35701+ au_kfree_rcu(whtmp);
1facf9fc 35702+}
35703+
35704+/*
35705+ * rmdir the whiteouted temporary named dir @h_dentry.
35706+ * @whlist: whiteouted children.
35707+ */
35708+int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35709+ struct dentry *wh_dentry, struct au_nhash *whlist)
35710+{
35711+ int err;
2000de60 35712+ unsigned int h_nlink;
1facf9fc 35713+ struct path h_tmp;
35714+ struct inode *wh_inode, *h_dir;
35715+ struct au_branch *br;
0b2a12c6 35716+ struct user_namespace *h_userns;
1facf9fc 35717+
5527c038 35718+ h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
1facf9fc 35719+ IMustLock(h_dir);
35720+
35721+ br = au_sbr(dir->i_sb, bindex);
0b2a12c6 35722+ h_userns = au_br_userns(br);
5527c038 35723+ wh_inode = d_inode(wh_dentry);
febd17d6 35724+ inode_lock_nested(wh_inode, AuLsc_I_CHILD);
1facf9fc 35725+
35726+ /*
35727+ * someone else might change some whiteouts while we were sleeping.
35728+ * it means this whlist may have an obsoleted entry.
35729+ */
0b2a12c6 35730+ if (!au_test_h_perm_sio(h_userns, wh_inode, MAY_EXEC | MAY_WRITE))
1facf9fc 35731+ err = del_wh_children(wh_dentry, whlist, bindex, br);
35732+ else {
35733+ int wkq_err;
35734+ struct del_wh_children_args args = {
35735+ .errp = &err,
35736+ .h_dentry = wh_dentry,
1308ab2a 35737+ .whlist = whlist,
1facf9fc 35738+ .bindex = bindex,
35739+ .br = br
35740+ };
35741+
35742+ wkq_err = au_wkq_wait(call_del_wh_children, &args);
35743+ if (unlikely(wkq_err))
35744+ err = wkq_err;
35745+ }
febd17d6 35746+ inode_unlock(wh_inode);
1facf9fc 35747+
35748+ if (!err) {
35749+ h_tmp.dentry = wh_dentry;
86dc4139 35750+ h_tmp.mnt = au_br_mnt(br);
2000de60 35751+ h_nlink = h_dir->i_nlink;
1facf9fc 35752+ err = vfsub_rmdir(h_dir, &h_tmp);
2000de60
JR
35753+ /* some fs doesn't change the parent nlink in some cases */
35754+ h_nlink -= h_dir->i_nlink;
1facf9fc 35755+ }
35756+
35757+ if (!err) {
5afbbe0d 35758+ if (au_ibtop(dir) == bindex) {
7f207e10 35759+ /* todo: dir->i_mutex is necessary */
1facf9fc 35760+ au_cpup_attr_timesizes(dir);
2000de60
JR
35761+ if (h_nlink)
35762+ vfsub_drop_nlink(dir);
1facf9fc 35763+ }
35764+ return 0; /* success */
35765+ }
35766+
523b37e3 35767+ pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
1facf9fc 35768+ return err;
35769+}
35770+
35771+static void call_rmdir_whtmp(void *args)
35772+{
35773+ int err;
e49829fe 35774+ aufs_bindex_t bindex;
1facf9fc 35775+ struct au_whtmp_rmdir *a = args;
35776+ struct super_block *sb;
35777+ struct dentry *h_parent;
35778+ struct inode *h_dir;
1facf9fc 35779+ struct au_hinode *hdir;
35780+
35781+ /* rmdir by nfsd may cause deadlock with this i_mutex */
febd17d6 35782+ /* inode_lock(a->dir); */
e49829fe 35783+ err = -EROFS;
1facf9fc 35784+ sb = a->dir->i_sb;
e49829fe
JR
35785+ si_read_lock(sb, !AuLock_FLUSH);
35786+ if (!au_br_writable(a->br->br_perm))
35787+ goto out;
35788+ bindex = au_br_index(sb, a->br->br_id);
35789+ if (unlikely(bindex < 0))
1facf9fc 35790+ goto out;
35791+
35792+ err = -EIO;
1facf9fc 35793+ ii_write_lock_parent(a->dir);
35794+ h_parent = dget_parent(a->wh_dentry);
5527c038 35795+ h_dir = d_inode(h_parent);
e49829fe 35796+ hdir = au_hi(a->dir, bindex);
86dc4139
AM
35797+ err = vfsub_mnt_want_write(au_br_mnt(a->br));
35798+ if (unlikely(err))
35799+ goto out_mnt;
5afbbe0d 35800+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
e49829fe
JR
35801+ err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
35802+ a->br);
86dc4139
AM
35803+ if (!err)
35804+ err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
5afbbe0d 35805+ au_hn_inode_unlock(hdir);
86dc4139
AM
35806+ vfsub_mnt_drop_write(au_br_mnt(a->br));
35807+
35808+out_mnt:
1facf9fc 35809+ dput(h_parent);
35810+ ii_write_unlock(a->dir);
4f0767ce 35811+out:
febd17d6 35812+ /* inode_unlock(a->dir); */
1facf9fc 35813+ au_whtmp_rmdir_free(a);
027c5e7a
AM
35814+ si_read_unlock(sb);
35815+ au_nwt_done(&au_sbi(sb)->si_nowait);
1facf9fc 35816+ if (unlikely(err))
35817+ AuIOErr("err %d\n", err);
35818+}
35819+
35820+void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35821+ struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
35822+{
35823+ int wkq_err;
e49829fe 35824+ struct super_block *sb;
1facf9fc 35825+
35826+ IMustLock(dir);
35827+
35828+ /* all post-process will be done in do_rmdir_whtmp(). */
e49829fe 35829+ sb = dir->i_sb;
1facf9fc 35830+ args->dir = au_igrab(dir);
e49829fe 35831+ args->br = au_sbr(sb, bindex);
acd2b654 35832+ au_lcnt_inc(&args->br->br_count);
1facf9fc 35833+ args->wh_dentry = dget(wh_dentry);
53392da6 35834+ wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
1facf9fc 35835+ if (unlikely(wkq_err)) {
523b37e3 35836+ pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
1facf9fc 35837+ au_whtmp_rmdir_free(args);
35838+ }
35839+}
7f207e10 35840diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
eca34b5c 35841--- /usr/share/empty/fs/aufs/whout.h 1970-01-01 01:00:00.000000000 +0100
46016270 35842+++ linux/fs/aufs/whout.h 2021-06-30 21:35:11.397206648 +0200
0b2a12c6 35843@@ -0,0 +1,87 @@
062440b3 35844+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 35845+/*
d58c55f2 35846+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 35847+ *
35848+ * This program, aufs is free software; you can redistribute it and/or modify
35849+ * it under the terms of the GNU General Public License as published by
35850+ * the Free Software Foundation; either version 2 of the License, or
35851+ * (at your option) any later version.
dece6358
AM
35852+ *
35853+ * This program is distributed in the hope that it will be useful,
35854+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
35855+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35856+ * GNU General Public License for more details.
35857+ *
35858+ * You should have received a copy of the GNU General Public License
523b37e3 35859+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 35860+ */
35861+
35862+/*
35863+ * whiteout for logical deletion and opaque directory
35864+ */
35865+
35866+#ifndef __AUFS_WHOUT_H__
35867+#define __AUFS_WHOUT_H__
35868+
35869+#ifdef __KERNEL__
35870+
1facf9fc 35871+#include "dir.h"
35872+
35873+/* whout.c */
35874+int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
0b2a12c6
JR
35875+int au_wh_test(struct user_namespace *h_userns, struct dentry *h_parent,
35876+ struct qstr *wh_name, int try_sio);
35877+int au_diropq_test(struct user_namespace *h_userns, struct dentry *h_dentry);
3c1bdaff 35878+struct au_branch;
1facf9fc 35879+struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
35880+ struct qstr *prefix);
35881+int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
35882+int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35883+ struct dentry *dentry);
86dc4139 35884+int au_wh_init(struct au_branch *br, struct super_block *sb);
1facf9fc 35885+
35886+/* diropq flags */
35887+#define AuDiropq_CREATE 1
35888+#define au_ftest_diropq(flags, name) ((flags) & AuDiropq_##name)
7f207e10
AM
35889+#define au_fset_diropq(flags, name) \
35890+ do { (flags) |= AuDiropq_##name; } while (0)
35891+#define au_fclr_diropq(flags, name) \
35892+ do { (flags) &= ~AuDiropq_##name; } while (0)
1facf9fc 35893+
35894+struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35895+ unsigned int flags);
35896+struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35897+ struct au_branch *br);
35898+struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35899+ struct dentry *h_parent);
35900+
35901+/* real rmdir for the whiteout-ed dir */
35902+struct au_whtmp_rmdir {
35903+ struct inode *dir;
e49829fe 35904+ struct au_branch *br;
1facf9fc 35905+ struct dentry *wh_dentry;
dece6358 35906+ struct au_nhash whlist;
1facf9fc 35907+};
35908+
35909+struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
35910+void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
35911+int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35912+ struct dentry *wh_dentry, struct au_nhash *whlist);
35913+void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35914+ struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
35915+
35916+/* ---------------------------------------------------------------------- */
35917+
35918+static inline struct dentry *au_diropq_create(struct dentry *dentry,
35919+ aufs_bindex_t bindex)
35920+{
35921+ return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
35922+}
35923+
35924+static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
35925+{
35926+ return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
35927+}
35928+
35929+#endif /* __KERNEL__ */
35930+#endif /* __AUFS_WHOUT_H__ */
7f207e10 35931diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
eca34b5c 35932--- /usr/share/empty/fs/aufs/wkq.c 1970-01-01 01:00:00.000000000 +0100
46016270 35933+++ linux/fs/aufs/wkq.c 2021-02-24 13:33:42.751013936 +0100
43982f53 35934@@ -0,0 +1,372 @@
cd7a4cd9 35935+// SPDX-License-Identifier: GPL-2.0
1facf9fc 35936+/*
d58c55f2 35937+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 35938+ *
35939+ * This program, aufs is free software; you can redistribute it and/or modify
35940+ * it under the terms of the GNU General Public License as published by
35941+ * the Free Software Foundation; either version 2 of the License, or
35942+ * (at your option) any later version.
dece6358
AM
35943+ *
35944+ * This program is distributed in the hope that it will be useful,
35945+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
35946+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35947+ * GNU General Public License for more details.
35948+ *
35949+ * You should have received a copy of the GNU General Public License
523b37e3 35950+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 35951+ */
35952+
35953+/*
35954+ * workqueue for asynchronous/super-io operations
acd2b654 35955+ * todo: try new credential scheme
1facf9fc 35956+ */
35957+
dece6358 35958+#include <linux/module.h>
1facf9fc 35959+#include "aufs.h"
35960+
9dbd164d 35961+/* internal workqueue named AUFS_WKQ_NAME */
b752ccd1 35962+
9dbd164d 35963+static struct workqueue_struct *au_wkq;
1facf9fc 35964+
35965+struct au_wkinfo {
35966+ struct work_struct wk;
7f207e10 35967+ struct kobject *kobj;
1facf9fc 35968+
35969+ unsigned int flags; /* see wkq.h */
35970+
35971+ au_wkq_func_t func;
35972+ void *args;
35973+
8b6a4947
AM
35974+#ifdef CONFIG_LOCKDEP
35975+ int dont_check;
35976+ struct held_lock **hlock;
35977+#endif
35978+
1facf9fc 35979+ struct completion *comp;
35980+};
35981+
35982+/* ---------------------------------------------------------------------- */
8b6a4947
AM
35983+/*
35984+ * Aufs passes some operations to the workqueue such as the internal copyup.
35985+ * This scheme looks rather unnatural for LOCKDEP debugging feature, since the
35986+ * job run by workqueue depends upon the locks acquired in the other task.
35987+ * Delegating a small operation to the workqueue, aufs passes its lockdep
35988+ * information too. And the job in the workqueue restores the info in order to
35989+ * pretend as if it acquired those locks. This is just to make LOCKDEP work
35990+ * correctly and expectedly.
35991+ */
35992+
35993+#ifndef CONFIG_LOCKDEP
35994+AuStubInt0(au_wkq_lockdep_alloc, struct au_wkinfo *wkinfo);
35995+AuStubVoid(au_wkq_lockdep_free, struct au_wkinfo *wkinfo);
35996+AuStubVoid(au_wkq_lockdep_pre, struct au_wkinfo *wkinfo);
35997+AuStubVoid(au_wkq_lockdep_post, struct au_wkinfo *wkinfo);
35998+AuStubVoid(au_wkq_lockdep_init, struct au_wkinfo *wkinfo);
35999+#else
36000+static void au_wkq_lockdep_init(struct au_wkinfo *wkinfo)
36001+{
36002+ wkinfo->hlock = NULL;
36003+ wkinfo->dont_check = 0;
36004+}
36005+
36006+/*
36007+ * 1: matched
36008+ * 0: unmatched
36009+ */
36010+static int au_wkq_lockdep_test(struct lock_class_key *key, const char *name)
36011+{
36012+ static DEFINE_SPINLOCK(spin);
36013+ static struct {
36014+ char *name;
36015+ struct lock_class_key *key;
36016+ } a[] = {
36017+ { .name = "&sbinfo->si_rwsem" },
36018+ { .name = "&finfo->fi_rwsem" },
36019+ { .name = "&dinfo->di_rwsem" },
36020+ { .name = "&iinfo->ii_rwsem" }
36021+ };
36022+ static int set;
36023+ int i;
36024+
36025+ /* lockless read from 'set.' see below */
36026+ if (set == ARRAY_SIZE(a)) {
36027+ for (i = 0; i < ARRAY_SIZE(a); i++)
36028+ if (a[i].key == key)
36029+ goto match;
36030+ goto unmatch;
36031+ }
36032+
36033+ spin_lock(&spin);
36034+ if (set)
36035+ for (i = 0; i < ARRAY_SIZE(a); i++)
36036+ if (a[i].key == key) {
36037+ spin_unlock(&spin);
36038+ goto match;
36039+ }
36040+ for (i = 0; i < ARRAY_SIZE(a); i++) {
36041+ if (a[i].key) {
36042+ if (unlikely(a[i].key == key)) { /* rare but possible */
36043+ spin_unlock(&spin);
36044+ goto match;
36045+ } else
36046+ continue;
36047+ }
36048+ if (strstr(a[i].name, name)) {
36049+ /*
36050+ * the order of these three lines is important for the
36051+ * lockless read above.
36052+ */
36053+ a[i].key = key;
36054+ spin_unlock(&spin);
36055+ set++;
36056+ /* AuDbg("%d, %s\n", set, name); */
36057+ goto match;
36058+ }
36059+ }
36060+ spin_unlock(&spin);
36061+ goto unmatch;
36062+
36063+match:
36064+ return 1;
36065+unmatch:
36066+ return 0;
36067+}
36068+
36069+static int au_wkq_lockdep_alloc(struct au_wkinfo *wkinfo)
36070+{
36071+ int err, n;
36072+ struct task_struct *curr;
36073+ struct held_lock **hl, *held_locks, *p;
36074+
36075+ err = 0;
36076+ curr = current;
36077+ wkinfo->dont_check = lockdep_recursing(curr);
36078+ if (wkinfo->dont_check)
36079+ goto out;
36080+ n = curr->lockdep_depth;
36081+ if (!n)
36082+ goto out;
36083+
36084+ err = -ENOMEM;
36085+ wkinfo->hlock = kmalloc_array(n + 1, sizeof(*wkinfo->hlock), GFP_NOFS);
36086+ if (unlikely(!wkinfo->hlock))
36087+ goto out;
36088+
36089+ err = 0;
43982f53
AM
36090+#if 0 /* left for debugging */
36091+ if (0 && au_debug_test())
8b6a4947
AM
36092+ lockdep_print_held_locks(curr);
36093+#endif
36094+ held_locks = curr->held_locks;
36095+ hl = wkinfo->hlock;
36096+ while (n--) {
36097+ p = held_locks++;
36098+ if (au_wkq_lockdep_test(p->instance->key, p->instance->name))
36099+ *hl++ = p;
36100+ }
36101+ *hl = NULL;
36102+
36103+out:
36104+ return err;
36105+}
36106+
36107+static void au_wkq_lockdep_free(struct au_wkinfo *wkinfo)
36108+{
9f237c51 36109+ au_kfree_try_rcu(wkinfo->hlock);
8b6a4947
AM
36110+}
36111+
36112+static void au_wkq_lockdep_pre(struct au_wkinfo *wkinfo)
36113+{
36114+ struct held_lock *p, **hl = wkinfo->hlock;
36115+ int subclass;
36116+
36117+ if (wkinfo->dont_check)
36118+ lockdep_off();
36119+ if (!hl)
36120+ return;
36121+ while ((p = *hl++)) { /* assignment */
36122+ subclass = lockdep_hlock_class(p)->subclass;
36123+ /* AuDbg("%s, %d\n", p->instance->name, subclass); */
36124+ if (p->read)
36125+ rwsem_acquire_read(p->instance, subclass, 0,
36126+ /*p->acquire_ip*/_RET_IP_);
36127+ else
36128+ rwsem_acquire(p->instance, subclass, 0,
36129+ /*p->acquire_ip*/_RET_IP_);
36130+ }
36131+}
36132+
36133+static void au_wkq_lockdep_post(struct au_wkinfo *wkinfo)
36134+{
36135+ struct held_lock *p, **hl = wkinfo->hlock;
36136+
36137+ if (wkinfo->dont_check)
36138+ lockdep_on();
36139+ if (!hl)
36140+ return;
36141+ while ((p = *hl++)) /* assignment */
d58c55f2 36142+ rwsem_release(p->instance, /*p->acquire_ip*/_RET_IP_);
8b6a4947
AM
36143+}
36144+#endif
1facf9fc 36145+
1facf9fc 36146+static void wkq_func(struct work_struct *wk)
36147+{
36148+ struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
36149+
2dfbb274 36150+ AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
7f207e10
AM
36151+ AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
36152+
8b6a4947 36153+ au_wkq_lockdep_pre(wkinfo);
1facf9fc 36154+ wkinfo->func(wkinfo->args);
8b6a4947 36155+ au_wkq_lockdep_post(wkinfo);
1facf9fc 36156+ if (au_ftest_wkq(wkinfo->flags, WAIT))
36157+ complete(wkinfo->comp);
36158+ else {
7f207e10 36159+ kobject_put(wkinfo->kobj);
9dbd164d 36160+ module_put(THIS_MODULE); /* todo: ?? */
9f237c51 36161+ au_kfree_rcu(wkinfo);
1facf9fc 36162+ }
36163+}
36164+
36165+/*
36166+ * Since struct completion is large, try allocating it dynamically.
36167+ */
1facf9fc 36168+#define AuWkqCompDeclare(name) struct completion *comp = NULL
36169+
36170+static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
36171+{
36172+ *comp = kmalloc(sizeof(**comp), GFP_NOFS);
36173+ if (*comp) {
36174+ init_completion(*comp);
36175+ wkinfo->comp = *comp;
36176+ return 0;
36177+ }
36178+ return -ENOMEM;
36179+}
36180+
36181+static void au_wkq_comp_free(struct completion *comp)
36182+{
9f237c51 36183+ au_kfree_rcu(comp);
1facf9fc 36184+}
36185+
53392da6 36186+static void au_wkq_run(struct au_wkinfo *wkinfo)
1facf9fc 36187+{
53392da6
AM
36188+ if (au_ftest_wkq(wkinfo->flags, NEST)) {
36189+ if (au_wkq_test()) {
38d290e6 36190+ AuWarn1("wkq from wkq, unless silly-rename on NFS,"
acd2b654
AM
36191+ " due to a dead dir by UDBA,"
36192+ " or async xino write?\n");
53392da6
AM
36193+ AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
36194+ }
36195+ } else
36196+ au_dbg_verify_kthread();
36197+
36198+ if (au_ftest_wkq(wkinfo->flags, WAIT)) {
a1f66529 36199+ INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
9dbd164d 36200+ queue_work(au_wkq, &wkinfo->wk);
4a4d8108
AM
36201+ } else {
36202+ INIT_WORK(&wkinfo->wk, wkq_func);
36203+ schedule_work(&wkinfo->wk);
36204+ }
1facf9fc 36205+}
36206+
7f207e10
AM
36207+/*
36208+ * Be careful. It is easy to make deadlock happen.
36209+ * processA: lock, wkq and wait
36210+ * processB: wkq and wait, lock in wkq
36211+ * --> deadlock
36212+ */
b752ccd1 36213+int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
1facf9fc 36214+{
36215+ int err;
36216+ AuWkqCompDeclare(comp);
36217+ struct au_wkinfo wkinfo = {
b752ccd1 36218+ .flags = flags,
1facf9fc 36219+ .func = func,
36220+ .args = args
36221+ };
36222+
36223+ err = au_wkq_comp_alloc(&wkinfo, &comp);
8b6a4947
AM
36224+ if (unlikely(err))
36225+ goto out;
36226+ err = au_wkq_lockdep_alloc(&wkinfo);
36227+ if (unlikely(err))
36228+ goto out_comp;
1facf9fc 36229+ if (!err) {
53392da6 36230+ au_wkq_run(&wkinfo);
1facf9fc 36231+ /* no timeout, no interrupt */
36232+ wait_for_completion(wkinfo.comp);
1facf9fc 36233+ }
8b6a4947 36234+ au_wkq_lockdep_free(&wkinfo);
1facf9fc 36235+
8b6a4947
AM
36236+out_comp:
36237+ au_wkq_comp_free(comp);
36238+out:
36239+ destroy_work_on_stack(&wkinfo.wk);
1facf9fc 36240+ return err;
1facf9fc 36241+}
36242+
027c5e7a
AM
36243+/*
36244+ * Note: dget/dput() in func for aufs dentries are not supported. It will be a
36245+ * problem in a concurrent umounting.
36246+ */
53392da6
AM
36247+int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36248+ unsigned int flags)
1facf9fc 36249+{
36250+ int err;
36251+ struct au_wkinfo *wkinfo;
36252+
f0c0a007 36253+ atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
1facf9fc 36254+
36255+ /*
36256+ * wkq_func() must free this wkinfo.
36257+ * it highly depends upon the implementation of workqueue.
36258+ */
36259+ err = 0;
36260+ wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
36261+ if (wkinfo) {
7f207e10 36262+ wkinfo->kobj = &au_sbi(sb)->si_kobj;
53392da6 36263+ wkinfo->flags = flags & ~AuWkq_WAIT;
1facf9fc 36264+ wkinfo->func = func;
36265+ wkinfo->args = args;
36266+ wkinfo->comp = NULL;
8b6a4947 36267+ au_wkq_lockdep_init(wkinfo);
7f207e10 36268+ kobject_get(wkinfo->kobj);
9dbd164d 36269+ __module_get(THIS_MODULE); /* todo: ?? */
1facf9fc 36270+
53392da6 36271+ au_wkq_run(wkinfo);
1facf9fc 36272+ } else {
36273+ err = -ENOMEM;
e49829fe 36274+ au_nwt_done(&au_sbi(sb)->si_nowait);
1facf9fc 36275+ }
36276+
36277+ return err;
36278+}
36279+
36280+/* ---------------------------------------------------------------------- */
36281+
36282+void au_nwt_init(struct au_nowait_tasks *nwt)
36283+{
f0c0a007
AM
36284+ atomic_set(&nwt->nw_len, 0);
36285+ /* smp_mb(); */ /* atomic_set */
1facf9fc 36286+ init_waitqueue_head(&nwt->nw_wq);
36287+}
36288+
36289+void au_wkq_fin(void)
36290+{
9dbd164d 36291+ destroy_workqueue(au_wkq);
1facf9fc 36292+}
36293+
36294+int __init au_wkq_init(void)
36295+{
9dbd164d 36296+ int err;
b752ccd1
AM
36297+
36298+ err = 0;
86dc4139 36299+ au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
9dbd164d
AM
36300+ if (IS_ERR(au_wkq))
36301+ err = PTR_ERR(au_wkq);
36302+ else if (!au_wkq)
36303+ err = -ENOMEM;
b752ccd1
AM
36304+
36305+ return err;
1facf9fc 36306+}
7f207e10 36307diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
eca34b5c 36308--- /usr/share/empty/fs/aufs/wkq.h 1970-01-01 01:00:00.000000000 +0100
46016270 36309+++ linux/fs/aufs/wkq.h 2021-02-24 13:33:42.751013936 +0100
acd2b654 36310@@ -0,0 +1,89 @@
062440b3 36311+/* SPDX-License-Identifier: GPL-2.0 */
1facf9fc 36312+/*
d58c55f2 36313+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 36314+ *
36315+ * This program, aufs is free software; you can redistribute it and/or modify
36316+ * it under the terms of the GNU General Public License as published by
36317+ * the Free Software Foundation; either version 2 of the License, or
36318+ * (at your option) any later version.
dece6358
AM
36319+ *
36320+ * This program is distributed in the hope that it will be useful,
36321+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36322+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36323+ * GNU General Public License for more details.
36324+ *
36325+ * You should have received a copy of the GNU General Public License
523b37e3 36326+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 36327+ */
36328+
36329+/*
36330+ * workqueue for asynchronous/super-io operations
36331+ * todo: try new credentials management scheme
36332+ */
36333+
36334+#ifndef __AUFS_WKQ_H__
36335+#define __AUFS_WKQ_H__
36336+
36337+#ifdef __KERNEL__
36338+
8b6a4947 36339+#include <linux/wait.h>
5afbbe0d 36340+
dece6358
AM
36341+struct super_block;
36342+
1facf9fc 36343+/* ---------------------------------------------------------------------- */
36344+
36345+/*
36346+ * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
36347+ */
36348+struct au_nowait_tasks {
f0c0a007 36349+ atomic_t nw_len;
1facf9fc 36350+ wait_queue_head_t nw_wq;
36351+};
36352+
36353+/* ---------------------------------------------------------------------- */
36354+
36355+typedef void (*au_wkq_func_t)(void *args);
36356+
36357+/* wkq flags */
36358+#define AuWkq_WAIT 1
9dbd164d 36359+#define AuWkq_NEST (1 << 1)
1facf9fc 36360+#define au_ftest_wkq(flags, name) ((flags) & AuWkq_##name)
7f207e10
AM
36361+#define au_fset_wkq(flags, name) \
36362+ do { (flags) |= AuWkq_##name; } while (0)
36363+#define au_fclr_wkq(flags, name) \
36364+ do { (flags) &= ~AuWkq_##name; } while (0)
1facf9fc 36365+
36366+/* wkq.c */
b752ccd1 36367+int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
53392da6
AM
36368+int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36369+ unsigned int flags);
1facf9fc 36370+void au_nwt_init(struct au_nowait_tasks *nwt);
36371+int __init au_wkq_init(void);
36372+void au_wkq_fin(void);
36373+
36374+/* ---------------------------------------------------------------------- */
36375+
53392da6
AM
36376+static inline int au_wkq_test(void)
36377+{
36378+ return current->flags & PF_WQ_WORKER;
36379+}
36380+
b752ccd1 36381+static inline int au_wkq_wait(au_wkq_func_t func, void *args)
1facf9fc 36382+{
b752ccd1 36383+ return au_wkq_do_wait(AuWkq_WAIT, func, args);
1facf9fc 36384+}
36385+
36386+static inline void au_nwt_done(struct au_nowait_tasks *nwt)
36387+{
f0c0a007 36388+ if (atomic_dec_and_test(&nwt->nw_len))
1facf9fc 36389+ wake_up_all(&nwt->nw_wq);
36390+}
36391+
36392+static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
36393+{
f0c0a007 36394+ wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
1facf9fc 36395+ return 0;
36396+}
36397+
36398+#endif /* __KERNEL__ */
36399+#endif /* __AUFS_WKQ_H__ */
c1595e42 36400diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
eca34b5c 36401--- /usr/share/empty/fs/aufs/xattr.c 1970-01-01 01:00:00.000000000 +0100
46016270 36402+++ linux/fs/aufs/xattr.c 2021-06-30 21:35:11.397206648 +0200
0b2a12c6 36403@@ -0,0 +1,368 @@
cd7a4cd9 36404+// SPDX-License-Identifier: GPL-2.0
c1595e42 36405+/*
d58c55f2 36406+ * Copyright (C) 2014-2020 Junjiro R. Okajima
c1595e42
JR
36407+ *
36408+ * This program, aufs is free software; you can redistribute it and/or modify
36409+ * it under the terms of the GNU General Public License as published by
36410+ * the Free Software Foundation; either version 2 of the License, or
36411+ * (at your option) any later version.
36412+ *
36413+ * This program is distributed in the hope that it will be useful,
36414+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36415+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36416+ * GNU General Public License for more details.
36417+ *
36418+ * You should have received a copy of the GNU General Public License
36419+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
36420+ */
36421+
36422+/*
36423+ * handling xattr functions
36424+ */
36425+
a2654f78
AM
36426+#include <linux/fs.h>
36427+#include <linux/posix_acl_xattr.h>
c1595e42
JR
36428+#include <linux/xattr.h>
36429+#include "aufs.h"
36430+
36431+static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
36432+{
36433+ if (!ignore_flags)
36434+ goto out;
36435+ switch (err) {
36436+ case -ENOMEM:
36437+ case -EDQUOT:
36438+ goto out;
36439+ }
36440+
36441+ if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
36442+ err = 0;
36443+ goto out;
36444+ }
36445+
36446+#define cmp(brattr, prefix) do { \
36447+ if (!strncmp(name, XATTR_##prefix##_PREFIX, \
36448+ XATTR_##prefix##_PREFIX_LEN)) { \
36449+ if (ignore_flags & AuBrAttr_ICEX_##brattr) \
36450+ err = 0; \
36451+ goto out; \
36452+ } \
36453+ } while (0)
36454+
36455+ cmp(SEC, SECURITY);
36456+ cmp(SYS, SYSTEM);
36457+ cmp(TR, TRUSTED);
36458+ cmp(USR, USER);
36459+#undef cmp
36460+
36461+ if (ignore_flags & AuBrAttr_ICEX_OTH)
36462+ err = 0;
36463+
36464+out:
36465+ return err;
36466+}
36467+
36468+static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
36469+
0b2a12c6 36470+static int au_do_cpup_xattr(struct path *h_dst, struct path *h_src,
7e9cd9fe
AM
36471+ char *name, char **buf, unsigned int ignore_flags,
36472+ unsigned int verbose)
c1595e42
JR
36473+{
36474+ int err;
36475+ ssize_t ssz;
36476+ struct inode *h_idst;
0b2a12c6
JR
36477+ struct dentry *h_dst_dentry, *h_src_dentry;
36478+ struct user_namespace *h_dst_userns, *h_src_userns;
c1595e42 36479+
0b2a12c6
JR
36480+ h_src_userns = mnt_user_ns(h_src->mnt);
36481+ h_src_dentry = h_src->dentry;
36482+ ssz = vfs_getxattr_alloc(h_src_userns, h_src_dentry, name, buf, 0,
36483+ GFP_NOFS);
c1595e42
JR
36484+ err = ssz;
36485+ if (unlikely(err <= 0)) {
c1595e42
JR
36486+ if (err == -ENODATA
36487+ || (err == -EOPNOTSUPP
b912730e 36488+ && ((ignore_flags & au_xattr_out_of_list)
0b2a12c6 36489+ || (au_test_nfs_noacl(d_inode(h_src_dentry))
b912730e
AM
36490+ && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
36491+ || !strcmp(name,
36492+ XATTR_NAME_POSIX_ACL_DEFAULT))))
36493+ ))
c1595e42 36494+ err = 0;
b912730e
AM
36495+ if (err && (verbose || au_debug_test()))
36496+ pr_err("%s, err %d\n", name, err);
c1595e42
JR
36497+ goto out;
36498+ }
36499+
36500+ /* unlock it temporary */
0b2a12c6
JR
36501+ h_dst_userns = mnt_user_ns(h_dst->mnt);
36502+ h_dst_dentry = h_dst->dentry;
36503+ h_idst = d_inode(h_dst_dentry);
febd17d6 36504+ inode_unlock(h_idst);
0b2a12c6
JR
36505+ err = vfsub_setxattr(h_dst_userns, h_dst_dentry, name, *buf, ssz,
36506+ /*flags*/0);
febd17d6 36507+ inode_lock_nested(h_idst, AuLsc_I_CHILD2);
c1595e42 36508+ if (unlikely(err)) {
7e9cd9fe
AM
36509+ if (verbose || au_debug_test())
36510+ pr_err("%s, err %d\n", name, err);
c1595e42
JR
36511+ err = au_xattr_ignore(err, name, ignore_flags);
36512+ }
36513+
36514+out:
36515+ return err;
36516+}
36517+
0b2a12c6 36518+int au_cpup_xattr(struct path *h_dst, struct path *h_src, int ignore_flags,
7e9cd9fe 36519+ unsigned int verbose)
c1595e42
JR
36520+{
36521+ int err, unlocked, acl_access, acl_default;
36522+ ssize_t ssz;
0b2a12c6 36523+ struct dentry *h_dst_dentry, *h_src_dentry;
c1595e42
JR
36524+ struct inode *h_isrc, *h_idst;
36525+ char *value, *p, *o, *e;
36526+
36527+ /* try stopping to update the source inode while we are referencing */
7e9cd9fe 36528+ /* there should not be the parent-child relationship between them */
0b2a12c6
JR
36529+ h_dst_dentry = h_dst->dentry;
36530+ h_idst = d_inode(h_dst_dentry);
36531+ h_src_dentry = h_src->dentry;
36532+ h_isrc = d_inode(h_src_dentry);
febd17d6 36533+ inode_unlock(h_idst);
be118d29 36534+ inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
febd17d6 36535+ inode_lock_nested(h_idst, AuLsc_I_CHILD2);
c1595e42
JR
36536+ unlocked = 0;
36537+
36538+ /* some filesystems don't list POSIX ACL, for example tmpfs */
0b2a12c6 36539+ ssz = vfs_listxattr(h_src_dentry, NULL, 0);
c1595e42
JR
36540+ err = ssz;
36541+ if (unlikely(err < 0)) {
36542+ AuTraceErr(err);
36543+ if (err == -ENODATA
36544+ || err == -EOPNOTSUPP)
36545+ err = 0; /* ignore */
36546+ goto out;
36547+ }
36548+
36549+ err = 0;
36550+ p = NULL;
36551+ o = NULL;
36552+ if (ssz) {
36553+ err = -ENOMEM;
36554+ p = kmalloc(ssz, GFP_NOFS);
36555+ o = p;
36556+ if (unlikely(!p))
36557+ goto out;
0b2a12c6 36558+ err = vfs_listxattr(h_src_dentry, p, ssz);
c1595e42 36559+ }
3c1bdaff 36560+ inode_unlock_shared(h_isrc);
c1595e42
JR
36561+ unlocked = 1;
36562+ AuDbg("err %d, ssz %zd\n", err, ssz);
36563+ if (unlikely(err < 0))
36564+ goto out_free;
36565+
36566+ err = 0;
36567+ e = p + ssz;
36568+ value = NULL;
36569+ acl_access = 0;
36570+ acl_default = 0;
36571+ while (!err && p < e) {
36572+ acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
36573+ sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
36574+ acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
36575+ sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
36576+ - 1);
7e9cd9fe
AM
36577+ err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
36578+ verbose);
c1595e42
JR
36579+ p += strlen(p) + 1;
36580+ }
36581+ AuTraceErr(err);
36582+ ignore_flags |= au_xattr_out_of_list;
36583+ if (!err && !acl_access) {
36584+ err = au_do_cpup_xattr(h_dst, h_src,
36585+ XATTR_NAME_POSIX_ACL_ACCESS, &value,
7e9cd9fe 36586+ ignore_flags, verbose);
c1595e42
JR
36587+ AuTraceErr(err);
36588+ }
36589+ if (!err && !acl_default) {
36590+ err = au_do_cpup_xattr(h_dst, h_src,
36591+ XATTR_NAME_POSIX_ACL_DEFAULT, &value,
7e9cd9fe 36592+ ignore_flags, verbose);
c1595e42
JR
36593+ AuTraceErr(err);
36594+ }
36595+
9f237c51 36596+ au_kfree_try_rcu(value);
c1595e42
JR
36597+
36598+out_free:
9f237c51 36599+ au_kfree_try_rcu(o);
c1595e42
JR
36600+out:
36601+ if (!unlocked)
3c1bdaff 36602+ inode_unlock_shared(h_isrc);
c1595e42
JR
36603+ AuTraceErr(err);
36604+ return err;
36605+}
36606+
36607+/* ---------------------------------------------------------------------- */
36608+
a2654f78
AM
36609+static int au_smack_reentering(struct super_block *sb)
36610+{
42b5c33a 36611+#if IS_ENABLED(CONFIG_SECURITY_SMACK) || IS_ENABLED(CONFIG_SECURITY_SELINUX)
a2654f78
AM
36612+ /*
36613+ * as a part of lookup, smack_d_instantiate() is called, and it calls
36614+ * i_op->getxattr(). ouch.
36615+ */
36616+ return si_pid_test(sb);
36617+#else
36618+ return 0;
36619+#endif
36620+}
36621+
c1595e42
JR
36622+enum {
36623+ AU_XATTR_LIST,
36624+ AU_XATTR_GET
36625+};
36626+
36627+struct au_lgxattr {
36628+ int type;
36629+ union {
36630+ struct {
36631+ char *list;
36632+ size_t size;
36633+ } list;
36634+ struct {
36635+ const char *name;
36636+ void *value;
36637+ size_t size;
36638+ } get;
36639+ } u;
36640+};
36641+
42b5c33a
AM
36642+static ssize_t au_lgxattr(struct dentry *dentry, struct inode *inode,
36643+ struct au_lgxattr *arg)
c1595e42
JR
36644+{
36645+ ssize_t err;
a2654f78 36646+ int reenter;
c1595e42
JR
36647+ struct path h_path;
36648+ struct super_block *sb;
36649+
36650+ sb = dentry->d_sb;
a2654f78
AM
36651+ reenter = au_smack_reentering(sb);
36652+ if (!reenter) {
36653+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
36654+ if (unlikely(err))
36655+ goto out;
36656+ }
42b5c33a 36657+ err = au_h_path_getattr(dentry, inode, /*force*/1, &h_path, reenter);
c1595e42
JR
36658+ if (unlikely(err))
36659+ goto out_si;
36660+ if (unlikely(!h_path.dentry))
36661+ /* illegally overlapped or something */
36662+ goto out_di; /* pretending success */
36663+
36664+ /* always topmost entry only */
36665+ switch (arg->type) {
36666+ case AU_XATTR_LIST:
36667+ err = vfs_listxattr(h_path.dentry,
36668+ arg->u.list.list, arg->u.list.size);
36669+ break;
36670+ case AU_XATTR_GET:
5afbbe0d 36671+ AuDebugOn(d_is_negative(h_path.dentry));
0b2a12c6 36672+ err = vfs_getxattr(mnt_user_ns(h_path.mnt), h_path.dentry,
c1595e42
JR
36673+ arg->u.get.name, arg->u.get.value,
36674+ arg->u.get.size);
36675+ break;
36676+ }
36677+
36678+out_di:
a2654f78
AM
36679+ if (!reenter)
36680+ di_read_unlock(dentry, AuLock_IR);
c1595e42 36681+out_si:
a2654f78
AM
36682+ if (!reenter)
36683+ si_read_unlock(sb);
c1595e42
JR
36684+out:
36685+ AuTraceErr(err);
36686+ return err;
36687+}
36688+
36689+ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
36690+{
36691+ struct au_lgxattr arg = {
36692+ .type = AU_XATTR_LIST,
36693+ .u.list = {
36694+ .list = list,
36695+ .size = size
36696+ },
36697+ };
36698+
42b5c33a 36699+ return au_lgxattr(dentry, /*inode*/NULL, &arg);
c1595e42
JR
36700+}
36701+
42b5c33a 36702+static ssize_t au_getxattr(struct dentry *dentry, struct inode *inode,
f2c43d5f 36703+ const char *name, void *value, size_t size)
c1595e42
JR
36704+{
36705+ struct au_lgxattr arg = {
36706+ .type = AU_XATTR_GET,
36707+ .u.get = {
36708+ .name = name,
36709+ .value = value,
36710+ .size = size
36711+ },
36712+ };
36713+
42b5c33a 36714+ return au_lgxattr(dentry, inode, &arg);
c1595e42
JR
36715+}
36716+
f2c43d5f
AM
36717+static int au_setxattr(struct dentry *dentry, struct inode *inode,
36718+ const char *name, const void *value, size_t size,
36719+ int flags)
c1595e42 36720+{
f2c43d5f 36721+ struct au_sxattr arg = {
c1595e42
JR
36722+ .type = AU_XATTR_SET,
36723+ .u.set = {
36724+ .name = name,
36725+ .value = value,
36726+ .size = size,
36727+ .flags = flags
36728+ },
36729+ };
36730+
f2c43d5f 36731+ return au_sxattr(dentry, inode, &arg);
c1595e42
JR
36732+}
36733+
36734+/* ---------------------------------------------------------------------- */
36735+
f2c43d5f
AM
36736+static int au_xattr_get(const struct xattr_handler *handler,
36737+ struct dentry *dentry, struct inode *inode,
36738+ const char *name, void *buffer, size_t size)
c1595e42 36739+{
f2c43d5f 36740+ return au_getxattr(dentry, inode, name, buffer, size);
c1595e42
JR
36741+}
36742+
f2c43d5f 36743+static int au_xattr_set(const struct xattr_handler *handler,
0b2a12c6 36744+ struct user_namespace *userns,
f2c43d5f
AM
36745+ struct dentry *dentry, struct inode *inode,
36746+ const char *name, const void *value, size_t size,
36747+ int flags)
c1595e42 36748+{
f2c43d5f 36749+ return au_setxattr(dentry, inode, name, value, size, flags);
c1595e42
JR
36750+}
36751+
36752+static const struct xattr_handler au_xattr_handler = {
f2c43d5f
AM
36753+ .name = "",
36754+ .prefix = "",
c1595e42
JR
36755+ .get = au_xattr_get,
36756+ .set = au_xattr_set
c1595e42
JR
36757+};
36758+
36759+static const struct xattr_handler *au_xattr_handlers[] = {
a2654f78
AM
36760+#ifdef CONFIG_FS_POSIX_ACL
36761+ &posix_acl_access_xattr_handler,
36762+ &posix_acl_default_xattr_handler,
36763+#endif
36764+ &au_xattr_handler, /* must be last */
f2c43d5f 36765+ NULL
c1595e42
JR
36766+};
36767+
36768+void au_xattr_init(struct super_block *sb)
36769+{
f2c43d5f 36770+ sb->s_xattr = au_xattr_handlers;
c1595e42 36771+}
7f207e10 36772diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
eca34b5c 36773--- /usr/share/empty/fs/aufs/xino.c 1970-01-01 01:00:00.000000000 +0100
46016270 36774+++ linux/fs/aufs/xino.c 2021-06-30 21:35:11.397206648 +0200
e37dd06a 36775@@ -0,0 +1,1925 @@
cd7a4cd9 36776+// SPDX-License-Identifier: GPL-2.0
1facf9fc 36777+/*
d58c55f2 36778+ * Copyright (C) 2005-2020 Junjiro R. Okajima
1facf9fc 36779+ *
36780+ * This program, aufs is free software; you can redistribute it and/or modify
36781+ * it under the terms of the GNU General Public License as published by
36782+ * the Free Software Foundation; either version 2 of the License, or
36783+ * (at your option) any later version.
dece6358
AM
36784+ *
36785+ * This program is distributed in the hope that it will be useful,
36786+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36787+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36788+ * GNU General Public License for more details.
36789+ *
36790+ * You should have received a copy of the GNU General Public License
523b37e3 36791+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 36792+ */
36793+
36794+/*
36795+ * external inode number translation table and bitmap
062440b3
AM
36796+ *
36797+ * things to consider
36798+ * - the lifetime
36799+ * + au_xino object
36800+ * + XINO files (xino, xib, xigen)
36801+ * + dynamic debugfs entries (xiN)
36802+ * + static debugfs entries (xib, xigen)
36803+ * + static sysfs entry (xi_path)
36804+ * - several entry points to handle them.
36805+ * + mount(2) without xino option (default)
36806+ * + mount(2) with xino option
36807+ * + mount(2) with noxino option
36808+ * + umount(2)
36809+ * + remount with add/del branches
36810+ * + remount with xino/noxino options
1facf9fc 36811+ */
36812+
36813+#include <linux/seq_file.h>
392086de 36814+#include <linux/statfs.h>
1facf9fc 36815+#include "aufs.h"
36816+
062440b3
AM
36817+static aufs_bindex_t sbr_find_shared(struct super_block *sb, aufs_bindex_t btop,
36818+ aufs_bindex_t bbot,
36819+ struct super_block *h_sb)
1facf9fc 36820+{
062440b3
AM
36821+ /* todo: try binary-search if the branches are many */
36822+ for (; btop <= bbot; btop++)
36823+ if (h_sb == au_sbr_sb(sb, btop))
36824+ return btop;
36825+ return -1;
be52b249
AM
36826+}
36827+
062440b3
AM
36828+/*
36829+ * find another branch who is on the same filesystem of the specified
36830+ * branch{@btgt}. search until @bbot.
36831+ */
36832+static aufs_bindex_t is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
36833+ aufs_bindex_t bbot)
1facf9fc 36834+{
062440b3
AM
36835+ aufs_bindex_t bindex;
36836+ struct super_block *tgt_sb;
1facf9fc 36837+
062440b3
AM
36838+ tgt_sb = au_sbr_sb(sb, btgt);
36839+ bindex = sbr_find_shared(sb, /*btop*/0, btgt - 1, tgt_sb);
36840+ if (bindex < 0)
36841+ bindex = sbr_find_shared(sb, btgt + 1, bbot, tgt_sb);
1facf9fc 36842+
062440b3 36843+ return bindex;
1facf9fc 36844+}
36845+
36846+/* ---------------------------------------------------------------------- */
36847+
36848+/*
062440b3 36849+ * stop unnecessary notify events at creating xino files
1facf9fc 36850+ */
acd2b654
AM
36851+
36852+aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry)
36853+{
36854+ aufs_bindex_t bfound, bindex, bbot;
36855+ struct dentry *parent;
36856+ struct au_branch *br;
36857+
36858+ bfound = -1;
36859+ parent = dentry->d_parent; /* safe d_parent access */
36860+ bbot = au_sbbot(sb);
36861+ for (bindex = 0; bindex <= bbot; bindex++) {
36862+ br = au_sbr(sb, bindex);
36863+ if (au_br_dentry(br) == parent) {
36864+ bfound = bindex;
36865+ break;
36866+ }
36867+ }
36868+
36869+ AuDbg("bfound b%d\n", bfound);
36870+ return bfound;
36871+}
36872+
062440b3
AM
36873+struct au_xino_lock_dir {
36874+ struct au_hinode *hdir;
36875+ struct dentry *parent;
36876+ struct inode *dir;
36877+};
36878+
36879+static struct dentry *au_dget_parent_lock(struct dentry *dentry,
36880+ unsigned int lsc)
1facf9fc 36881+{
062440b3
AM
36882+ struct dentry *parent;
36883+ struct inode *dir;
1facf9fc 36884+
062440b3 36885+ parent = dget_parent(dentry);
5527c038 36886+ dir = d_inode(parent);
062440b3
AM
36887+ inode_lock_nested(dir, lsc);
36888+#if 0 /* it should not happen */
36889+ spin_lock(&dentry->d_lock);
36890+ if (unlikely(dentry->d_parent != parent)) {
36891+ spin_unlock(&dentry->d_lock);
36892+ inode_unlock(dir);
36893+ dput(parent);
36894+ parent = NULL;
1facf9fc 36895+ goto out;
36896+ }
062440b3 36897+ spin_unlock(&dentry->d_lock);
1facf9fc 36898+
4f0767ce 36899+out:
062440b3
AM
36900+#endif
36901+ return parent;
1facf9fc 36902+}
36903+
062440b3 36904+static void au_xino_lock_dir(struct super_block *sb, struct path *xipath,
1facf9fc 36905+ struct au_xino_lock_dir *ldir)
36906+{
acd2b654 36907+ aufs_bindex_t bindex;
1facf9fc 36908+
36909+ ldir->hdir = NULL;
acd2b654 36910+ bindex = au_xi_root(sb, xipath->dentry);
1facf9fc 36911+ if (bindex >= 0) {
062440b3 36912+ /* rw branch root */
5527c038 36913+ ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
5afbbe0d 36914+ au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
1facf9fc 36915+ } else {
062440b3
AM
36916+ /* other */
36917+ ldir->parent = au_dget_parent_lock(xipath->dentry,
36918+ AuLsc_I_PARENT);
febd17d6 36919+ ldir->dir = d_inode(ldir->parent);
1facf9fc 36920+ }
36921+}
36922+
36923+static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
36924+{
36925+ if (ldir->hdir)
5afbbe0d 36926+ au_hn_inode_unlock(ldir->hdir);
1facf9fc 36927+ else {
febd17d6 36928+ inode_unlock(ldir->dir);
1facf9fc 36929+ dput(ldir->parent);
36930+ }
36931+}
36932+
36933+/* ---------------------------------------------------------------------- */
36934+
062440b3
AM
36935+/*
36936+ * create and set a new xino file
36937+ */
83b672a5
AM
36938+struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
36939+ int wbrtop)
062440b3
AM
36940+{
36941+ struct file *file;
36942+ struct dentry *h_parent, *d;
36943+ struct inode *h_dir, *inode;
36944+ int err;
83b672a5 36945+ static DEFINE_MUTEX(mtx);
062440b3
AM
36946+
36947+ /*
36948+ * at mount-time, and the xino file is the default path,
36949+ * hnotify is disabled so we have no notify events to ignore.
36950+ * when a user specified the xino, we cannot get au_hdir to be ignored.
36951+ */
83b672a5
AM
36952+ if (!wbrtop)
36953+ mutex_lock(&mtx);
062440b3
AM
36954+ file = vfsub_filp_open(fpath, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
36955+ /* | __FMODE_NONOTIFY */,
cd7a4cd9 36956+ 0666);
062440b3 36957+ if (IS_ERR(file)) {
83b672a5
AM
36958+ if (!wbrtop)
36959+ mutex_unlock(&mtx);
062440b3
AM
36960+ if (!silent)
36961+ pr_err("open %s(%ld)\n", fpath, PTR_ERR(file));
36962+ return file;
36963+ }
36964+
36965+ /* keep file count */
36966+ err = 0;
36967+ d = file->f_path.dentry;
36968+ h_parent = au_dget_parent_lock(d, AuLsc_I_PARENT);
83b672a5
AM
36969+ if (!wbrtop)
36970+ mutex_unlock(&mtx);
062440b3
AM
36971+ /* mnt_want_write() is unnecessary here */
36972+ h_dir = d_inode(h_parent);
36973+ inode = file_inode(file);
36974+ /* no delegation since it is just created */
36975+ if (inode->i_nlink)
36976+ err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
36977+ /*force*/0);
36978+ inode_unlock(h_dir);
36979+ dput(h_parent);
36980+ if (unlikely(err)) {
36981+ if (!silent)
36982+ pr_err("unlink %s(%d)\n", fpath, err);
36983+ goto out;
36984+ }
36985+
36986+ err = -EINVAL;
36987+ if (unlikely(sb == d->d_sb)) {
36988+ if (!silent)
36989+ pr_err("%s must be outside\n", fpath);
36990+ goto out;
36991+ }
36992+ if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
36993+ if (!silent)
36994+ pr_err("xino doesn't support %s(%s)\n",
36995+ fpath, au_sbtype(d->d_sb));
36996+ goto out;
36997+ }
36998+ return file; /* success */
36999+
37000+out:
37001+ fput(file);
37002+ file = ERR_PTR(err);
37003+ return file;
37004+}
37005+
37006+/*
37007+ * create a new xinofile at the same place/path as @base.
37008+ */
37009+struct file *au_xino_create2(struct super_block *sb, struct path *base,
37010+ struct file *copy_src)
37011+{
37012+ struct file *file;
37013+ struct dentry *dentry, *parent;
37014+ struct inode *dir, *delegated;
37015+ struct qstr *name;
37016+ struct path path;
37017+ int err, do_unlock;
37018+ struct au_xino_lock_dir ldir;
37019+
37020+ do_unlock = 1;
37021+ au_xino_lock_dir(sb, base, &ldir);
37022+ dentry = base->dentry;
37023+ parent = dentry->d_parent; /* dir inode is locked */
37024+ dir = d_inode(parent);
37025+ IMustLock(dir);
37026+
37027+ name = &dentry->d_name;
37028+ path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
37029+ if (IS_ERR(path.dentry)) {
37030+ file = (void *)path.dentry;
37031+ pr_err("%pd lookup err %ld\n", dentry, PTR_ERR(path.dentry));
37032+ goto out;
37033+ }
37034+
37035+ /* no need to mnt_want_write() since we call dentry_open() later */
0b2a12c6 37036+ err = vfs_create(mnt_user_ns(base->mnt), dir, path.dentry, 0666, NULL);
062440b3
AM
37037+ if (unlikely(err)) {
37038+ file = ERR_PTR(err);
37039+ pr_err("%pd create err %d\n", dentry, err);
37040+ goto out_dput;
37041+ }
37042+
37043+ path.mnt = base->mnt;
37044+ file = vfsub_dentry_open(&path,
37045+ O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37046+ /* | __FMODE_NONOTIFY */);
37047+ if (IS_ERR(file)) {
37048+ pr_err("%pd open err %ld\n", dentry, PTR_ERR(file));
37049+ goto out_dput;
37050+ }
37051+
37052+ delegated = NULL;
37053+ err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
37054+ au_xino_unlock_dir(&ldir);
37055+ do_unlock = 0;
37056+ if (unlikely(err == -EWOULDBLOCK)) {
37057+ pr_warn("cannot retry for NFSv4 delegation"
37058+ " for an internal unlink\n");
37059+ iput(delegated);
37060+ }
37061+ if (unlikely(err)) {
37062+ pr_err("%pd unlink err %d\n", dentry, err);
37063+ goto out_fput;
37064+ }
37065+
37066+ if (copy_src) {
37067+ /* no one can touch copy_src xino */
37068+ err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
37069+ if (unlikely(err)) {
37070+ pr_err("%pd copy err %d\n", dentry, err);
37071+ goto out_fput;
37072+ }
37073+ }
37074+ goto out_dput; /* success */
1facf9fc 37075+
062440b3
AM
37076+out_fput:
37077+ fput(file);
37078+ file = ERR_PTR(err);
37079+out_dput:
37080+ dput(path.dentry);
37081+out:
37082+ if (do_unlock)
37083+ au_xino_unlock_dir(&ldir);
37084+ return file;
37085+}
37086+
acd2b654
AM
37087+struct file *au_xino_file1(struct au_xino *xi)
37088+{
37089+ struct file *file;
37090+ unsigned int u, nfile;
37091+
37092+ file = NULL;
37093+ nfile = xi->xi_nfile;
37094+ for (u = 0; u < nfile; u++) {
37095+ file = xi->xi_file[u];
37096+ if (file)
37097+ break;
37098+ }
37099+
37100+ return file;
37101+}
37102+
37103+static int au_xino_file_set(struct au_xino *xi, int idx, struct file *file)
37104+{
37105+ int err;
37106+ struct file *f;
37107+ void *p;
37108+
37109+ if (file)
37110+ get_file(file);
37111+
37112+ err = 0;
37113+ f = NULL;
37114+ if (idx < xi->xi_nfile) {
37115+ f = xi->xi_file[idx];
37116+ if (f)
37117+ fput(f);
37118+ } else {
37119+ p = au_kzrealloc(xi->xi_file,
37120+ sizeof(*xi->xi_file) * xi->xi_nfile,
37121+ sizeof(*xi->xi_file) * (idx + 1),
37122+ GFP_NOFS, /*may_shrink*/0);
37123+ if (p) {
37124+ MtxMustLock(&xi->xi_mtx);
37125+ xi->xi_file = p;
37126+ xi->xi_nfile = idx + 1;
37127+ } else {
37128+ err = -ENOMEM;
37129+ if (file)
37130+ fput(file);
37131+ goto out;
37132+ }
37133+ }
37134+ xi->xi_file[idx] = file;
37135+
37136+out:
37137+ return err;
37138+}
37139+
37140+/*
37141+ * if @xinew->xi is not set, then create new xigen file.
37142+ */
37143+struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew)
37144+{
37145+ struct file *file;
37146+ int err;
37147+
37148+ SiMustAnyLock(sb);
37149+
37150+ file = au_xino_create2(sb, xinew->base, xinew->copy_src);
37151+ if (IS_ERR(file)) {
37152+ err = PTR_ERR(file);
37153+ pr_err("%s[%d], err %d\n",
37154+ xinew->xi ? "xino" : "xigen",
37155+ xinew->idx, err);
37156+ goto out;
37157+ }
37158+
37159+ if (xinew->xi)
37160+ err = au_xino_file_set(xinew->xi, xinew->idx, file);
37161+ else {
37162+ BUG();
37163+ /* todo: make xigen file an array */
37164+ /* err = au_xigen_file_set(sb, xinew->idx, file); */
37165+ }
37166+ fput(file);
37167+ if (unlikely(err))
37168+ file = ERR_PTR(err);
37169+
37170+out:
37171+ return file;
37172+}
37173+
062440b3
AM
37174+/* ---------------------------------------------------------------------- */
37175+
37176+/*
37177+ * truncate xino files
37178+ */
acd2b654
AM
37179+static int au_xino_do_trunc(struct super_block *sb, aufs_bindex_t bindex,
37180+ int idx, struct kstatfs *st)
1facf9fc 37181+{
37182+ int err;
392086de 37183+ blkcnt_t blocks;
acd2b654
AM
37184+ struct file *file, *new_xino;
37185+ struct au_xi_new xinew = {
37186+ .idx = idx
37187+ };
37188+
37189+ err = 0;
37190+ xinew.xi = au_sbr(sb, bindex)->br_xino;
37191+ file = au_xino_file(xinew.xi, idx);
37192+ if (!file)
37193+ goto out;
37194+
37195+ xinew.base = &file->f_path;
37196+ err = vfs_statfs(xinew.base, st);
37197+ if (unlikely(err)) {
37198+ AuErr1("statfs err %d, ignored\n", err);
37199+ err = 0;
37200+ goto out;
37201+ }
37202+
37203+ blocks = file_inode(file)->i_blocks;
37204+ pr_info("begin truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37205+ bindex, idx, (u64)blocks, st->f_bfree, st->f_blocks);
37206+
37207+ xinew.copy_src = file;
37208+ new_xino = au_xi_new(sb, &xinew);
37209+ if (IS_ERR(new_xino)) {
37210+ err = PTR_ERR(new_xino);
37211+ pr_err("xino(b%d-%d), err %d, ignored\n", bindex, idx, err);
37212+ goto out;
37213+ }
37214+
37215+ err = vfs_statfs(&new_xino->f_path, st);
37216+ if (!err)
37217+ pr_info("end truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37218+ bindex, idx, (u64)file_inode(new_xino)->i_blocks,
37219+ st->f_bfree, st->f_blocks);
37220+ else {
37221+ AuErr1("statfs err %d, ignored\n", err);
37222+ err = 0;
37223+ }
37224+
37225+out:
37226+ return err;
37227+}
37228+
37229+int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin)
37230+{
37231+ int err, i;
37232+ unsigned long jiffy;
062440b3 37233+ aufs_bindex_t bbot;
392086de 37234+ struct kstatfs *st;
1facf9fc 37235+ struct au_branch *br;
acd2b654 37236+ struct au_xino *xi;
1facf9fc 37237+
392086de 37238+ err = -ENOMEM;
be52b249 37239+ st = kmalloc(sizeof(*st), GFP_NOFS);
392086de
AM
37240+ if (unlikely(!st))
37241+ goto out;
37242+
1facf9fc 37243+ err = -EINVAL;
5afbbe0d
AM
37244+ bbot = au_sbbot(sb);
37245+ if (unlikely(bindex < 0 || bbot < bindex))
392086de 37246+ goto out_st;
392086de 37247+
1facf9fc 37248+ err = 0;
acd2b654
AM
37249+ jiffy = jiffies;
37250+ br = au_sbr(sb, bindex);
37251+ xi = br->br_xino;
37252+ for (i = idx_begin; !err && i < xi->xi_nfile; i++)
37253+ err = au_xino_do_trunc(sb, bindex, i, st);
37254+ if (!err)
37255+ au_sbi(sb)->si_xino_jiffy = jiffy;
392086de
AM
37256+
37257+out_st:
9f237c51 37258+ au_kfree_rcu(st);
4f0767ce 37259+out:
1facf9fc 37260+ return err;
37261+}
37262+
37263+struct xino_do_trunc_args {
37264+ struct super_block *sb;
37265+ struct au_branch *br;
acd2b654 37266+ int idx;
1facf9fc 37267+};
37268+
37269+static void xino_do_trunc(void *_args)
37270+{
37271+ struct xino_do_trunc_args *args = _args;
37272+ struct super_block *sb;
37273+ struct au_branch *br;
37274+ struct inode *dir;
acd2b654 37275+ int err, idx;
1facf9fc 37276+ aufs_bindex_t bindex;
37277+
37278+ err = 0;
37279+ sb = args->sb;
5527c038 37280+ dir = d_inode(sb->s_root);
1facf9fc 37281+ br = args->br;
acd2b654 37282+ idx = args->idx;
1facf9fc 37283+
37284+ si_noflush_write_lock(sb);
37285+ ii_read_lock_parent(dir);
37286+ bindex = au_br_index(sb, br->br_id);
acd2b654 37287+ err = au_xino_trunc(sb, bindex, idx);
1facf9fc 37288+ ii_read_unlock(dir);
37289+ if (unlikely(err))
392086de 37290+ pr_warn("err b%d, (%d)\n", bindex, err);
062440b3 37291+ atomic_dec(&br->br_xino->xi_truncating);
acd2b654 37292+ au_lcnt_dec(&br->br_count);
1facf9fc 37293+ si_write_unlock(sb);
027c5e7a 37294+ au_nwt_done(&au_sbi(sb)->si_nowait);
9f237c51 37295+ au_kfree_rcu(args);
1facf9fc 37296+}
37297+
acd2b654
AM
37298+/*
37299+ * returns the index in the xi_file array whose corresponding file is necessary
37300+ * to truncate, or -1 which means no need to truncate.
37301+ */
392086de
AM
37302+static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
37303+{
37304+ int err;
acd2b654 37305+ unsigned int u;
392086de
AM
37306+ struct kstatfs st;
37307+ struct au_sbinfo *sbinfo;
acd2b654 37308+ struct au_xino *xi;
062440b3 37309+ struct file *file;
392086de
AM
37310+
37311+ /* todo: si_xino_expire and the ratio should be customizable */
37312+ sbinfo = au_sbi(sb);
37313+ if (time_before(jiffies,
37314+ sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
acd2b654 37315+ return -1;
392086de
AM
37316+
37317+ /* truncation border */
acd2b654
AM
37318+ xi = br->br_xino;
37319+ for (u = 0; u < xi->xi_nfile; u++) {
37320+ file = au_xino_file(xi, u);
37321+ if (!file)
37322+ continue;
37323+
37324+ err = vfs_statfs(&file->f_path, &st);
37325+ if (unlikely(err)) {
37326+ AuErr1("statfs err %d, ignored\n", err);
37327+ return -1;
37328+ }
37329+ if (div64_u64(st.f_bfree * 100, st.f_blocks)
37330+ >= AUFS_XINO_DEF_TRUNC)
37331+ return u;
392086de 37332+ }
392086de 37333+
acd2b654 37334+ return -1;
392086de
AM
37335+}
37336+
1facf9fc 37337+static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
37338+{
acd2b654 37339+ int idx;
1facf9fc 37340+ struct xino_do_trunc_args *args;
37341+ int wkq_err;
37342+
acd2b654
AM
37343+ idx = xino_trunc_test(sb, br);
37344+ if (idx < 0)
1facf9fc 37345+ return;
37346+
062440b3 37347+ if (atomic_inc_return(&br->br_xino->xi_truncating) > 1)
1facf9fc 37348+ goto out;
37349+
37350+ /* lock and kfree() will be called in trunc_xino() */
37351+ args = kmalloc(sizeof(*args), GFP_NOFS);
37352+ if (unlikely(!args)) {
37353+ AuErr1("no memory\n");
f0c0a007 37354+ goto out;
1facf9fc 37355+ }
37356+
acd2b654 37357+ au_lcnt_inc(&br->br_count);
1facf9fc 37358+ args->sb = sb;
37359+ args->br = br;
acd2b654 37360+ args->idx = idx;
53392da6 37361+ wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
1facf9fc 37362+ if (!wkq_err)
37363+ return; /* success */
37364+
4a4d8108 37365+ pr_err("wkq %d\n", wkq_err);
acd2b654 37366+ au_lcnt_dec(&br->br_count);
9f237c51 37367+ au_kfree_rcu(args);
1facf9fc 37368+
4f0767ce 37369+out:
062440b3 37370+ atomic_dec(&br->br_xino->xi_truncating);
1facf9fc 37371+}
37372+
37373+/* ---------------------------------------------------------------------- */
37374+
acd2b654
AM
37375+struct au_xi_calc {
37376+ int idx;
37377+ loff_t pos;
37378+};
37379+
37380+static void au_xi_calc(struct super_block *sb, ino_t h_ino,
37381+ struct au_xi_calc *calc)
37382+{
37383+ loff_t maxent;
37384+
37385+ maxent = au_xi_maxent(sb);
37386+ calc->idx = div64_u64_rem(h_ino, maxent, &calc->pos);
37387+ calc->pos *= sizeof(ino_t);
37388+}
37389+
37390+static int au_xino_do_new_async(struct super_block *sb, struct au_branch *br,
37391+ struct au_xi_calc *calc)
37392+{
37393+ int err;
37394+ struct file *file;
37395+ struct au_xino *xi = br->br_xino;
37396+ struct au_xi_new xinew = {
37397+ .xi = xi
37398+ };
37399+
37400+ SiMustAnyLock(sb);
37401+
37402+ err = 0;
37403+ if (!xi)
37404+ goto out;
37405+
37406+ mutex_lock(&xi->xi_mtx);
37407+ file = au_xino_file(xi, calc->idx);
37408+ if (file)
37409+ goto out_mtx;
37410+
37411+ file = au_xino_file(xi, /*idx*/-1);
37412+ AuDebugOn(!file);
37413+ xinew.idx = calc->idx;
37414+ xinew.base = &file->f_path;
37415+ /* xinew.copy_src = NULL; */
37416+ file = au_xi_new(sb, &xinew);
37417+ if (IS_ERR(file))
37418+ err = PTR_ERR(file);
37419+
37420+out_mtx:
37421+ mutex_unlock(&xi->xi_mtx);
37422+out:
37423+ return err;
37424+}
37425+
37426+struct au_xino_do_new_async_args {
37427+ struct super_block *sb;
37428+ struct au_branch *br;
37429+ struct au_xi_calc calc;
37430+ ino_t ino;
37431+};
37432+
9f237c51
AM
37433+struct au_xi_writing {
37434+ struct hlist_bl_node node;
37435+ ino_t h_ino, ino;
37436+};
37437+
e37dd06a
AM
37438+static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37439+ ino_t ino);
acd2b654
AM
37440+
37441+static void au_xino_call_do_new_async(void *args)
37442+{
37443+ struct au_xino_do_new_async_args *a = args;
37444+ struct au_branch *br;
37445+ struct super_block *sb;
37446+ struct au_sbinfo *sbi;
37447+ struct inode *root;
37448+ struct file *file;
9f237c51
AM
37449+ struct au_xi_writing *del, *p;
37450+ struct hlist_bl_head *hbl;
37451+ struct hlist_bl_node *pos;
acd2b654
AM
37452+ int err;
37453+
37454+ br = a->br;
37455+ sb = a->sb;
37456+ sbi = au_sbi(sb);
37457+ si_noflush_read_lock(sb);
37458+ root = d_inode(sb->s_root);
37459+ ii_read_lock_child(root);
37460+ err = au_xino_do_new_async(sb, br, &a->calc);
9f237c51
AM
37461+ if (unlikely(err)) {
37462+ AuIOErr("err %d\n", err);
37463+ goto out;
37464+ }
37465+
37466+ file = au_xino_file(br->br_xino, a->calc.idx);
37467+ AuDebugOn(!file);
e37dd06a 37468+ err = au_xino_do_write(file, &a->calc, a->ino);
9f237c51 37469+ if (unlikely(err)) {
acd2b654 37470+ AuIOErr("err %d\n", err);
9f237c51
AM
37471+ goto out;
37472+ }
37473+
37474+ del = NULL;
37475+ hbl = &br->br_xino->xi_writing;
37476+ hlist_bl_lock(hbl);
37477+ au_hbl_for_each(pos, hbl) {
37478+ p = container_of(pos, struct au_xi_writing, node);
37479+ if (p->ino == a->ino) {
37480+ del = p;
37481+ hlist_bl_del(&p->node);
37482+ break;
37483+ }
37484+ }
37485+ hlist_bl_unlock(hbl);
37486+ au_kfree_rcu(del);
37487+
37488+out:
acd2b654
AM
37489+ au_lcnt_dec(&br->br_count);
37490+ ii_read_unlock(root);
37491+ si_read_unlock(sb);
37492+ au_nwt_done(&sbi->si_nowait);
9f237c51 37493+ au_kfree_rcu(a);
acd2b654
AM
37494+}
37495+
37496+/*
37497+ * create a new xino file asynchronously
37498+ */
37499+static int au_xino_new_async(struct super_block *sb, struct au_branch *br,
37500+ struct au_xi_calc *calc, ino_t ino)
37501+{
37502+ int err;
37503+ struct au_xino_do_new_async_args *arg;
37504+
37505+ err = -ENOMEM;
37506+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
37507+ if (unlikely(!arg))
37508+ goto out;
37509+
37510+ arg->sb = sb;
37511+ arg->br = br;
37512+ arg->calc = *calc;
37513+ arg->ino = ino;
37514+ au_lcnt_inc(&br->br_count);
37515+ err = au_wkq_nowait(au_xino_call_do_new_async, arg, sb, AuWkq_NEST);
37516+ if (unlikely(err)) {
37517+ pr_err("wkq %d\n", err);
37518+ au_lcnt_dec(&br->br_count);
9f237c51 37519+ au_kfree_rcu(arg);
acd2b654
AM
37520+ }
37521+
37522+out:
37523+ return err;
37524+}
37525+
062440b3
AM
37526+/*
37527+ * read @ino from xinofile for the specified branch{@sb, @bindex}
37528+ * at the position of @h_ino.
37529+ */
37530+int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37531+ ino_t *ino)
37532+{
37533+ int err;
37534+ ssize_t sz;
acd2b654 37535+ struct au_xi_calc calc;
062440b3 37536+ struct au_sbinfo *sbinfo;
acd2b654 37537+ struct file *file;
9f237c51
AM
37538+ struct au_xino *xi;
37539+ struct hlist_bl_head *hbl;
37540+ struct hlist_bl_node *pos;
37541+ struct au_xi_writing *p;
062440b3
AM
37542+
37543+ *ino = 0;
37544+ if (!au_opt_test(au_mntflags(sb), XINO))
37545+ return 0; /* no xino */
37546+
37547+ err = 0;
acd2b654 37548+ au_xi_calc(sb, h_ino, &calc);
9f237c51
AM
37549+ xi = au_sbr(sb, bindex)->br_xino;
37550+ file = au_xino_file(xi, calc.idx);
37551+ if (!file) {
37552+ hbl = &xi->xi_writing;
37553+ hlist_bl_lock(hbl);
37554+ au_hbl_for_each(pos, hbl) {
37555+ p = container_of(pos, struct au_xi_writing, node);
37556+ if (p->h_ino == h_ino) {
37557+ AuDbg("hi%llu, i%llu, found\n",
37558+ (u64)p->h_ino, (u64)p->ino);
37559+ *ino = p->ino;
37560+ break;
37561+ }
37562+ }
37563+ hlist_bl_unlock(hbl);
37564+ return 0;
37565+ } else if (vfsub_f_size_read(file) < calc.pos + sizeof(*ino))
37566+ return 0; /* no xino */
062440b3 37567+
acd2b654 37568+ sbinfo = au_sbi(sb);
e37dd06a 37569+ sz = xino_fread(file, ino, sizeof(*ino), &calc.pos);
062440b3
AM
37570+ if (sz == sizeof(*ino))
37571+ return 0; /* success */
37572+
37573+ err = sz;
37574+ if (unlikely(sz >= 0)) {
37575+ err = -EIO;
37576+ AuIOErr("xino read error (%zd)\n", sz);
37577+ }
062440b3
AM
37578+ return err;
37579+}
37580+
e37dd06a
AM
37581+static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37582+ ino_t ino)
1facf9fc 37583+{
1facf9fc 37584+ ssize_t sz;
37585+
e37dd06a 37586+ sz = xino_fwrite(file, &ino, sizeof(ino), &calc->pos);
1facf9fc 37587+ if (sz == sizeof(ino))
37588+ return 0; /* success */
37589+
37590+ AuIOErr("write failed (%zd)\n", sz);
37591+ return -EIO;
37592+}
37593+
37594+/*
37595+ * write @ino to the xinofile for the specified branch{@sb, @bindex}
37596+ * at the position of @h_ino.
37597+ * even if @ino is zero, it is written to the xinofile and means no entry.
37598+ * if the size of the xino file on a specific filesystem exceeds the watermark,
37599+ * try truncating it.
37600+ */
37601+int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37602+ ino_t ino)
37603+{
37604+ int err;
37605+ unsigned int mnt_flags;
acd2b654
AM
37606+ struct au_xi_calc calc;
37607+ struct file *file;
1facf9fc 37608+ struct au_branch *br;
acd2b654 37609+ struct au_xino *xi;
9f237c51 37610+ struct au_xi_writing *p;
1facf9fc 37611+
dece6358 37612+ SiMustAnyLock(sb);
1facf9fc 37613+
37614+ mnt_flags = au_mntflags(sb);
37615+ if (!au_opt_test(mnt_flags, XINO))
37616+ return 0;
37617+
acd2b654 37618+ au_xi_calc(sb, h_ino, &calc);
1facf9fc 37619+ br = au_sbr(sb, bindex);
acd2b654
AM
37620+ xi = br->br_xino;
37621+ file = au_xino_file(xi, calc.idx);
37622+ if (!file) {
9f237c51
AM
37623+ /* store the inum pair into the list */
37624+ p = kmalloc(sizeof(*p), GFP_NOFS | __GFP_NOFAIL);
37625+ p->h_ino = h_ino;
37626+ p->ino = ino;
37627+ au_hbl_add(&p->node, &xi->xi_writing);
37628+
acd2b654
AM
37629+ /* create and write a new xino file asynchronously */
37630+ err = au_xino_new_async(sb, br, &calc, ino);
37631+ if (!err)
37632+ return 0; /* success */
37633+ goto out;
37634+ }
37635+
e37dd06a 37636+ err = au_xino_do_write(file, &calc, ino);
1facf9fc 37637+ if (!err) {
acd2b654 37638+ br = au_sbr(sb, bindex);
1facf9fc 37639+ if (au_opt_test(mnt_flags, TRUNC_XINO)
86dc4139 37640+ && au_test_fs_trunc_xino(au_br_sb(br)))
1facf9fc 37641+ xino_try_trunc(sb, br);
37642+ return 0; /* success */
37643+ }
37644+
acd2b654 37645+out:
1facf9fc 37646+ AuIOErr("write failed (%d)\n", err);
37647+ return -EIO;
37648+}
37649+
e37dd06a
AM
37650+static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
37651+ loff_t *pos);
1facf9fc 37652+
062440b3 37653+/* todo: unnecessary to support mmap_sem since kernel-space? */
e37dd06a 37654+ssize_t xino_fread(struct file *file, void *kbuf, size_t size, loff_t *pos)
062440b3
AM
37655+{
37656+ ssize_t err;
062440b3
AM
37657+ int i;
37658+ const int prevent_endless = 10;
1facf9fc 37659+
062440b3 37660+ i = 0;
062440b3 37661+ do {
e37dd06a 37662+ err = vfsub_read_k(file, kbuf, size, pos);
062440b3
AM
37663+ if (err == -EINTR
37664+ && !au_wkq_test()
37665+ && fatal_signal_pending(current)) {
e37dd06a 37666+ err = xino_fread_wkq(file, kbuf, size, pos);
062440b3 37667+ BUG_ON(err == -EINTR);
062440b3
AM
37668+ }
37669+ } while (i++ < prevent_endless
37670+ && (err == -EAGAIN || err == -EINTR));
062440b3
AM
37671+
37672+#if 0 /* reserved for future use */
37673+ if (err > 0)
37674+ fsnotify_access(file->f_path.dentry);
37675+#endif
37676+
37677+ return err;
37678+}
37679+
37680+struct xino_fread_args {
37681+ ssize_t *errp;
062440b3
AM
37682+ struct file *file;
37683+ void *buf;
37684+ size_t size;
37685+ loff_t *pos;
37686+};
37687+
37688+static void call_xino_fread(void *args)
37689+{
37690+ struct xino_fread_args *a = args;
e37dd06a 37691+ *a->errp = xino_fread(a->file, a->buf, a->size, a->pos);
062440b3
AM
37692+}
37693+
e37dd06a
AM
37694+static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
37695+ loff_t *pos)
062440b3
AM
37696+{
37697+ ssize_t err;
37698+ int wkq_err;
37699+ struct xino_fread_args args = {
37700+ .errp = &err,
062440b3
AM
37701+ .file = file,
37702+ .buf = buf,
37703+ .size = size,
37704+ .pos = pos
37705+ };
37706+
37707+ wkq_err = au_wkq_wait(call_xino_fread, &args);
37708+ if (unlikely(wkq_err))
37709+ err = wkq_err;
37710+
37711+ return err;
37712+}
37713+
e37dd06a
AM
37714+static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
37715+ loff_t *pos);
062440b3 37716+
e37dd06a
AM
37717+static ssize_t do_xino_fwrite(struct file *file, void *kbuf, size_t size,
37718+ loff_t *pos)
062440b3
AM
37719+{
37720+ ssize_t err;
062440b3
AM
37721+ int i;
37722+ const int prevent_endless = 10;
37723+
37724+ i = 0;
062440b3 37725+ do {
e37dd06a 37726+ err = vfsub_write_k(file, kbuf, size, pos);
062440b3
AM
37727+ if (err == -EINTR
37728+ && !au_wkq_test()
37729+ && fatal_signal_pending(current)) {
e37dd06a 37730+ err = xino_fwrite_wkq(file, kbuf, size, pos);
062440b3 37731+ BUG_ON(err == -EINTR);
062440b3
AM
37732+ }
37733+ } while (i++ < prevent_endless
37734+ && (err == -EAGAIN || err == -EINTR));
062440b3
AM
37735+
37736+#if 0 /* reserved for future use */
37737+ if (err > 0)
37738+ fsnotify_modify(file->f_path.dentry);
37739+#endif
37740+
37741+ return err;
37742+}
37743+
37744+struct do_xino_fwrite_args {
37745+ ssize_t *errp;
062440b3
AM
37746+ struct file *file;
37747+ void *buf;
37748+ size_t size;
37749+ loff_t *pos;
37750+};
37751+
37752+static void call_do_xino_fwrite(void *args)
37753+{
37754+ struct do_xino_fwrite_args *a = args;
e37dd06a 37755+ *a->errp = do_xino_fwrite(a->file, a->buf, a->size, a->pos);
062440b3
AM
37756+}
37757+
e37dd06a
AM
37758+static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
37759+ loff_t *pos)
062440b3
AM
37760+{
37761+ ssize_t err;
37762+ int wkq_err;
37763+ struct do_xino_fwrite_args args = {
37764+ .errp = &err,
062440b3
AM
37765+ .file = file,
37766+ .buf = buf,
37767+ .size = size,
37768+ .pos = pos
37769+ };
37770+
37771+ /*
37772+ * it breaks RLIMIT_FSIZE and normal user's limit,
37773+ * users should care about quota and real 'filesystem full.'
37774+ */
37775+ wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
37776+ if (unlikely(wkq_err))
37777+ err = wkq_err;
37778+
37779+ return err;
37780+}
37781+
e37dd06a 37782+ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos)
062440b3
AM
37783+{
37784+ ssize_t err;
37785+
37786+ if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
37787+ lockdep_off();
e37dd06a 37788+ err = do_xino_fwrite(file, buf, size, pos);
062440b3
AM
37789+ lockdep_on();
37790+ } else {
37791+ lockdep_off();
e37dd06a 37792+ err = xino_fwrite_wkq(file, buf, size, pos);
062440b3
AM
37793+ lockdep_on();
37794+ }
37795+
37796+ return err;
37797+}
37798+
37799+/* ---------------------------------------------------------------------- */
37800+
37801+/*
37802+ * inode number bitmap
37803+ */
1facf9fc 37804+static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
37805+static ino_t xib_calc_ino(unsigned long pindex, int bit)
37806+{
37807+ ino_t ino;
37808+
37809+ AuDebugOn(bit < 0 || page_bits <= bit);
37810+ ino = AUFS_FIRST_INO + pindex * page_bits + bit;
37811+ return ino;
37812+}
37813+
37814+static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
37815+{
37816+ AuDebugOn(ino < AUFS_FIRST_INO);
37817+ ino -= AUFS_FIRST_INO;
37818+ *pindex = ino / page_bits;
37819+ *bit = ino % page_bits;
37820+}
37821+
37822+static int xib_pindex(struct super_block *sb, unsigned long pindex)
37823+{
37824+ int err;
37825+ loff_t pos;
37826+ ssize_t sz;
37827+ struct au_sbinfo *sbinfo;
37828+ struct file *xib;
37829+ unsigned long *p;
37830+
37831+ sbinfo = au_sbi(sb);
37832+ MtxMustLock(&sbinfo->si_xib_mtx);
37833+ AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
37834+ || !au_opt_test(sbinfo->si_mntflags, XINO));
37835+
37836+ if (pindex == sbinfo->si_xib_last_pindex)
37837+ return 0;
37838+
37839+ xib = sbinfo->si_xib;
37840+ p = sbinfo->si_xib_buf;
37841+ pos = sbinfo->si_xib_last_pindex;
37842+ pos *= PAGE_SIZE;
e37dd06a 37843+ sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
1facf9fc 37844+ if (unlikely(sz != PAGE_SIZE))
37845+ goto out;
37846+
37847+ pos = pindex;
37848+ pos *= PAGE_SIZE;
c06a8ce3 37849+ if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
e37dd06a 37850+ sz = xino_fread(xib, p, PAGE_SIZE, &pos);
1facf9fc 37851+ else {
37852+ memset(p, 0, PAGE_SIZE);
e37dd06a 37853+ sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
1facf9fc 37854+ }
37855+ if (sz == PAGE_SIZE) {
37856+ sbinfo->si_xib_last_pindex = pindex;
37857+ return 0; /* success */
37858+ }
37859+
4f0767ce 37860+out:
b752ccd1
AM
37861+ AuIOErr1("write failed (%zd)\n", sz);
37862+ err = sz;
37863+ if (sz >= 0)
37864+ err = -EIO;
37865+ return err;
37866+}
37867+
b752ccd1
AM
37868+static void au_xib_clear_bit(struct inode *inode)
37869+{
37870+ int err, bit;
37871+ unsigned long pindex;
37872+ struct super_block *sb;
37873+ struct au_sbinfo *sbinfo;
37874+
37875+ AuDebugOn(inode->i_nlink);
37876+
37877+ sb = inode->i_sb;
37878+ xib_calc_bit(inode->i_ino, &pindex, &bit);
37879+ AuDebugOn(page_bits <= bit);
37880+ sbinfo = au_sbi(sb);
37881+ mutex_lock(&sbinfo->si_xib_mtx);
37882+ err = xib_pindex(sb, pindex);
37883+ if (!err) {
37884+ clear_bit(bit, sbinfo->si_xib_buf);
37885+ sbinfo->si_xib_next_bit = bit;
37886+ }
37887+ mutex_unlock(&sbinfo->si_xib_mtx);
37888+}
37889+
1facf9fc 37890+/* ---------------------------------------------------------------------- */
37891+
1facf9fc 37892+/*
062440b3 37893+ * truncate a xino bitmap file
1facf9fc 37894+ */
1facf9fc 37895+
37896+/* todo: slow */
37897+static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
37898+{
37899+ int err, bit;
37900+ ssize_t sz;
37901+ unsigned long pindex;
37902+ loff_t pos, pend;
37903+ struct au_sbinfo *sbinfo;
1facf9fc 37904+ ino_t *ino;
37905+ unsigned long *p;
37906+
37907+ err = 0;
37908+ sbinfo = au_sbi(sb);
dece6358 37909+ MtxMustLock(&sbinfo->si_xib_mtx);
1facf9fc 37910+ p = sbinfo->si_xib_buf;
c06a8ce3 37911+ pend = vfsub_f_size_read(file);
1facf9fc 37912+ pos = 0;
37913+ while (pos < pend) {
e37dd06a 37914+ sz = xino_fread(file, page, PAGE_SIZE, &pos);
1facf9fc 37915+ err = sz;
37916+ if (unlikely(sz <= 0))
37917+ goto out;
37918+
37919+ err = 0;
37920+ for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
37921+ if (unlikely(*ino < AUFS_FIRST_INO))
37922+ continue;
37923+
37924+ xib_calc_bit(*ino, &pindex, &bit);
37925+ AuDebugOn(page_bits <= bit);
37926+ err = xib_pindex(sb, pindex);
37927+ if (!err)
37928+ set_bit(bit, p);
37929+ else
37930+ goto out;
37931+ }
37932+ }
37933+
4f0767ce 37934+out:
1facf9fc 37935+ return err;
37936+}
37937+
37938+static int xib_restore(struct super_block *sb)
37939+{
acd2b654
AM
37940+ int err, i;
37941+ unsigned int nfile;
5afbbe0d 37942+ aufs_bindex_t bindex, bbot;
1facf9fc 37943+ void *page;
062440b3 37944+ struct au_branch *br;
acd2b654
AM
37945+ struct au_xino *xi;
37946+ struct file *file;
1facf9fc 37947+
37948+ err = -ENOMEM;
37949+ page = (void *)__get_free_page(GFP_NOFS);
37950+ if (unlikely(!page))
37951+ goto out;
37952+
37953+ err = 0;
5afbbe0d
AM
37954+ bbot = au_sbbot(sb);
37955+ for (bindex = 0; !err && bindex <= bbot; bindex++)
062440b3
AM
37956+ if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0) {
37957+ br = au_sbr(sb, bindex);
acd2b654
AM
37958+ xi = br->br_xino;
37959+ nfile = xi->xi_nfile;
37960+ for (i = 0; i < nfile; i++) {
37961+ file = au_xino_file(xi, i);
37962+ if (file)
37963+ err = do_xib_restore(sb, file, page);
37964+ }
062440b3
AM
37965+ } else
37966+ AuDbg("skip shared b%d\n", bindex);
1c60b727 37967+ free_page((unsigned long)page);
1facf9fc 37968+
4f0767ce 37969+out:
1facf9fc 37970+ return err;
37971+}
37972+
37973+int au_xib_trunc(struct super_block *sb)
37974+{
37975+ int err;
37976+ ssize_t sz;
37977+ loff_t pos;
1facf9fc 37978+ struct au_sbinfo *sbinfo;
37979+ unsigned long *p;
37980+ struct file *file;
37981+
dece6358
AM
37982+ SiMustWriteLock(sb);
37983+
1facf9fc 37984+ err = 0;
37985+ sbinfo = au_sbi(sb);
37986+ if (!au_opt_test(sbinfo->si_mntflags, XINO))
37987+ goto out;
37988+
37989+ file = sbinfo->si_xib;
c06a8ce3 37990+ if (vfsub_f_size_read(file) <= PAGE_SIZE)
1facf9fc 37991+ goto out;
37992+
062440b3 37993+ file = au_xino_create2(sb, &sbinfo->si_xib->f_path, NULL);
1facf9fc 37994+ err = PTR_ERR(file);
37995+ if (IS_ERR(file))
37996+ goto out;
37997+ fput(sbinfo->si_xib);
37998+ sbinfo->si_xib = file;
37999+
38000+ p = sbinfo->si_xib_buf;
38001+ memset(p, 0, PAGE_SIZE);
38002+ pos = 0;
e37dd06a 38003+ sz = xino_fwrite(sbinfo->si_xib, p, PAGE_SIZE, &pos);
1facf9fc 38004+ if (unlikely(sz != PAGE_SIZE)) {
38005+ err = sz;
38006+ AuIOErr("err %d\n", err);
38007+ if (sz >= 0)
38008+ err = -EIO;
38009+ goto out;
38010+ }
38011+
38012+ mutex_lock(&sbinfo->si_xib_mtx);
38013+ /* mnt_want_write() is unnecessary here */
38014+ err = xib_restore(sb);
38015+ mutex_unlock(&sbinfo->si_xib_mtx);
38016+
38017+out:
38018+ return err;
38019+}
38020+
38021+/* ---------------------------------------------------------------------- */
38022+
acd2b654 38023+struct au_xino *au_xino_alloc(unsigned int nfile)
062440b3
AM
38024+{
38025+ struct au_xino *xi;
38026+
38027+ xi = kzalloc(sizeof(*xi), GFP_NOFS);
38028+ if (unlikely(!xi))
38029+ goto out;
acd2b654
AM
38030+ xi->xi_nfile = nfile;
38031+ xi->xi_file = kcalloc(nfile, sizeof(*xi->xi_file), GFP_NOFS);
38032+ if (unlikely(!xi->xi_file))
38033+ goto out_free;
062440b3
AM
38034+
38035+ xi->xi_nondir.total = 8; /* initial size */
38036+ xi->xi_nondir.array = kcalloc(xi->xi_nondir.total, sizeof(ino_t),
38037+ GFP_NOFS);
38038+ if (unlikely(!xi->xi_nondir.array))
acd2b654 38039+ goto out_file;
062440b3
AM
38040+
38041+ spin_lock_init(&xi->xi_nondir.spin);
38042+ init_waitqueue_head(&xi->xi_nondir.wqh);
acd2b654 38043+ mutex_init(&xi->xi_mtx);
9f237c51 38044+ INIT_HLIST_BL_HEAD(&xi->xi_writing);
062440b3
AM
38045+ atomic_set(&xi->xi_truncating, 0);
38046+ kref_init(&xi->xi_kref);
38047+ goto out; /* success */
38048+
acd2b654 38049+out_file:
9f237c51 38050+ au_kfree_try_rcu(xi->xi_file);
062440b3 38051+out_free:
9f237c51 38052+ au_kfree_rcu(xi);
062440b3
AM
38053+ xi = NULL;
38054+out:
38055+ return xi;
38056+}
38057+
acd2b654 38058+static int au_xino_init(struct au_branch *br, int idx, struct file *file)
062440b3
AM
38059+{
38060+ int err;
38061+ struct au_xino *xi;
38062+
38063+ err = 0;
acd2b654 38064+ xi = au_xino_alloc(idx + 1);
062440b3
AM
38065+ if (unlikely(!xi)) {
38066+ err = -ENOMEM;
38067+ goto out;
38068+ }
38069+
acd2b654
AM
38070+ if (file)
38071+ get_file(file);
38072+ xi->xi_file[idx] = file;
062440b3
AM
38073+ AuDebugOn(br->br_xino);
38074+ br->br_xino = xi;
38075+
38076+out:
38077+ return err;
38078+}
38079+
38080+static void au_xino_release(struct kref *kref)
38081+{
38082+ struct au_xino *xi;
38083+ int i;
9f237c51
AM
38084+ unsigned long ul;
38085+ struct hlist_bl_head *hbl;
38086+ struct hlist_bl_node *pos, *n;
38087+ struct au_xi_writing *p;
062440b3
AM
38088+
38089+ xi = container_of(kref, struct au_xino, xi_kref);
acd2b654
AM
38090+ for (i = 0; i < xi->xi_nfile; i++)
38091+ if (xi->xi_file[i])
38092+ fput(xi->xi_file[i]);
062440b3
AM
38093+ for (i = xi->xi_nondir.total - 1; i >= 0; i--)
38094+ AuDebugOn(xi->xi_nondir.array[i]);
acd2b654 38095+ mutex_destroy(&xi->xi_mtx);
9f237c51
AM
38096+ hbl = &xi->xi_writing;
38097+ ul = au_hbl_count(hbl);
38098+ if (unlikely(ul)) {
38099+ pr_warn("xi_writing %lu\n", ul);
38100+ hlist_bl_lock(hbl);
43982f53 38101+ hlist_bl_for_each_entry_safe(p, pos, n, hbl, node) {
9f237c51 38102+ hlist_bl_del(&p->node);
394e211a
AM
38103+ /* kmemleak reported au_kfree_rcu() doesn't free it */
38104+ kfree(p);
9f237c51
AM
38105+ }
38106+ hlist_bl_unlock(hbl);
38107+ }
38108+ au_kfree_try_rcu(xi->xi_file);
38109+ au_kfree_try_rcu(xi->xi_nondir.array);
38110+ au_kfree_rcu(xi);
062440b3
AM
38111+}
38112+
38113+int au_xino_put(struct au_branch *br)
38114+{
38115+ int ret;
38116+ struct au_xino *xi;
38117+
38118+ ret = 0;
38119+ xi = br->br_xino;
38120+ if (xi) {
38121+ br->br_xino = NULL;
38122+ ret = kref_put(&xi->xi_kref, au_xino_release);
38123+ }
38124+
38125+ return ret;
38126+}
38127+
062440b3
AM
38128+/* ---------------------------------------------------------------------- */
38129+
1facf9fc 38130+/*
38131+ * xino mount option handlers
38132+ */
1facf9fc 38133+
38134+/* xino bitmap */
38135+static void xino_clear_xib(struct super_block *sb)
38136+{
38137+ struct au_sbinfo *sbinfo;
38138+
dece6358
AM
38139+ SiMustWriteLock(sb);
38140+
1facf9fc 38141+ sbinfo = au_sbi(sb);
1facf9fc 38142+ if (sbinfo->si_xib)
38143+ fput(sbinfo->si_xib);
38144+ sbinfo->si_xib = NULL;
f0c0a007 38145+ if (sbinfo->si_xib_buf)
1c60b727 38146+ free_page((unsigned long)sbinfo->si_xib_buf);
1facf9fc 38147+ sbinfo->si_xib_buf = NULL;
38148+}
38149+
062440b3 38150+static int au_xino_set_xib(struct super_block *sb, struct path *path)
1facf9fc 38151+{
38152+ int err;
38153+ loff_t pos;
38154+ struct au_sbinfo *sbinfo;
38155+ struct file *file;
acd2b654 38156+ struct super_block *xi_sb;
1facf9fc 38157+
dece6358
AM
38158+ SiMustWriteLock(sb);
38159+
1facf9fc 38160+ sbinfo = au_sbi(sb);
062440b3 38161+ file = au_xino_create2(sb, path, sbinfo->si_xib);
1facf9fc 38162+ err = PTR_ERR(file);
38163+ if (IS_ERR(file))
38164+ goto out;
38165+ if (sbinfo->si_xib)
38166+ fput(sbinfo->si_xib);
38167+ sbinfo->si_xib = file;
acd2b654
AM
38168+ xi_sb = file_inode(file)->i_sb;
38169+ sbinfo->si_ximaxent = xi_sb->s_maxbytes;
38170+ if (unlikely(sbinfo->si_ximaxent < PAGE_SIZE)) {
38171+ err = -EIO;
38172+ pr_err("s_maxbytes(%llu) on %s is too small\n",
38173+ (u64)sbinfo->si_ximaxent, au_sbtype(xi_sb));
38174+ goto out_unset;
38175+ }
38176+ sbinfo->si_ximaxent /= sizeof(ino_t);
1facf9fc 38177+
38178+ err = -ENOMEM;
38179+ if (!sbinfo->si_xib_buf)
38180+ sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
38181+ if (unlikely(!sbinfo->si_xib_buf))
38182+ goto out_unset;
38183+
38184+ sbinfo->si_xib_last_pindex = 0;
38185+ sbinfo->si_xib_next_bit = 0;
c06a8ce3 38186+ if (vfsub_f_size_read(file) < PAGE_SIZE) {
1facf9fc 38187+ pos = 0;
e37dd06a 38188+ err = xino_fwrite(file, sbinfo->si_xib_buf, PAGE_SIZE, &pos);
1facf9fc 38189+ if (unlikely(err != PAGE_SIZE))
38190+ goto out_free;
38191+ }
38192+ err = 0;
38193+ goto out; /* success */
38194+
4f0767ce 38195+out_free:
f0c0a007 38196+ if (sbinfo->si_xib_buf)
1c60b727 38197+ free_page((unsigned long)sbinfo->si_xib_buf);
b752ccd1
AM
38198+ sbinfo->si_xib_buf = NULL;
38199+ if (err >= 0)
38200+ err = -EIO;
4f0767ce 38201+out_unset:
b752ccd1
AM
38202+ fput(sbinfo->si_xib);
38203+ sbinfo->si_xib = NULL;
4f0767ce 38204+out:
062440b3 38205+ AuTraceErr(err);
b752ccd1 38206+ return err;
1facf9fc 38207+}
38208+
b752ccd1
AM
38209+/* xino for each branch */
38210+static void xino_clear_br(struct super_block *sb)
38211+{
5afbbe0d 38212+ aufs_bindex_t bindex, bbot;
b752ccd1 38213+ struct au_branch *br;
1facf9fc 38214+
5afbbe0d
AM
38215+ bbot = au_sbbot(sb);
38216+ for (bindex = 0; bindex <= bbot; bindex++) {
b752ccd1 38217+ br = au_sbr(sb, bindex);
062440b3
AM
38218+ AuDebugOn(!br);
38219+ au_xino_put(br);
38220+ }
38221+}
38222+
38223+static void au_xino_set_br_shared(struct super_block *sb, struct au_branch *br,
38224+ aufs_bindex_t bshared)
38225+{
38226+ struct au_branch *brshared;
b752ccd1 38227+
062440b3
AM
38228+ brshared = au_sbr(sb, bshared);
38229+ AuDebugOn(!brshared->br_xino);
38230+ AuDebugOn(!brshared->br_xino->xi_file);
38231+ if (br->br_xino != brshared->br_xino) {
38232+ au_xino_get(brshared);
38233+ au_xino_put(br);
38234+ br->br_xino = brshared->br_xino;
b752ccd1
AM
38235+ }
38236+}
38237+
062440b3 38238+struct au_xino_do_set_br {
062440b3
AM
38239+ struct au_branch *br;
38240+ ino_t h_ino;
38241+ aufs_bindex_t bshared;
38242+};
38243+
38244+static int au_xino_do_set_br(struct super_block *sb, struct path *path,
38245+ struct au_xino_do_set_br *args)
1facf9fc 38246+{
38247+ int err;
acd2b654 38248+ struct au_xi_calc calc;
062440b3 38249+ struct file *file;
acd2b654
AM
38250+ struct au_branch *br;
38251+ struct au_xi_new xinew = {
38252+ .base = path
38253+ };
062440b3 38254+
acd2b654
AM
38255+ br = args->br;
38256+ xinew.xi = br->br_xino;
38257+ au_xi_calc(sb, args->h_ino, &calc);
38258+ xinew.copy_src = au_xino_file(xinew.xi, calc.idx);
38259+ if (args->bshared >= 0)
062440b3 38260+ /* shared xino */
acd2b654
AM
38261+ au_xino_set_br_shared(sb, br, args->bshared);
38262+ else if (!xinew.xi) {
38263+ /* new xino */
38264+ err = au_xino_init(br, calc.idx, xinew.copy_src);
38265+ if (unlikely(err))
38266+ goto out;
062440b3
AM
38267+ }
38268+
acd2b654
AM
38269+ /* force re-creating */
38270+ xinew.xi = br->br_xino;
38271+ xinew.idx = calc.idx;
38272+ mutex_lock(&xinew.xi->xi_mtx);
38273+ file = au_xi_new(sb, &xinew);
38274+ mutex_unlock(&xinew.xi->xi_mtx);
062440b3
AM
38275+ err = PTR_ERR(file);
38276+ if (IS_ERR(file))
38277+ goto out;
acd2b654
AM
38278+ AuDebugOn(!file);
38279+
e37dd06a 38280+ err = au_xino_do_write(file, &calc, AUFS_ROOT_INO);
acd2b654
AM
38281+ if (unlikely(err))
38282+ au_xino_put(br);
062440b3 38283+
062440b3
AM
38284+out:
38285+ AuTraceErr(err);
38286+ return err;
38287+}
38288+
38289+static int au_xino_set_br(struct super_block *sb, struct path *path)
38290+{
38291+ int err;
38292+ aufs_bindex_t bindex, bbot;
38293+ struct au_xino_do_set_br args;
b752ccd1 38294+ struct inode *inode;
1facf9fc 38295+
b752ccd1
AM
38296+ SiMustWriteLock(sb);
38297+
5afbbe0d 38298+ bbot = au_sbbot(sb);
5527c038 38299+ inode = d_inode(sb->s_root);
062440b3
AM
38300+ for (bindex = 0; bindex <= bbot; bindex++) {
38301+ args.h_ino = au_h_iptr(inode, bindex)->i_ino;
38302+ args.br = au_sbr(sb, bindex);
38303+ args.bshared = is_sb_shared(sb, bindex, bindex - 1);
38304+ err = au_xino_do_set_br(sb, path, &args);
b752ccd1 38305+ if (unlikely(err))
062440b3 38306+ break;
b752ccd1 38307+ }
1facf9fc 38308+
062440b3 38309+ AuTraceErr(err);
1facf9fc 38310+ return err;
38311+}
b752ccd1
AM
38312+
38313+void au_xino_clr(struct super_block *sb)
38314+{
38315+ struct au_sbinfo *sbinfo;
38316+
38317+ au_xigen_clr(sb);
38318+ xino_clear_xib(sb);
38319+ xino_clear_br(sb);
062440b3 38320+ dbgaufs_brs_del(sb, 0);
b752ccd1
AM
38321+ sbinfo = au_sbi(sb);
38322+ /* lvalue, do not call au_mntflags() */
38323+ au_opt_clr(sbinfo->si_mntflags, XINO);
38324+}
38325+
062440b3 38326+int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount)
b752ccd1
AM
38327+{
38328+ int err, skip;
062440b3 38329+ struct dentry *dentry, *parent, *cur_dentry, *cur_parent;
b752ccd1
AM
38330+ struct qstr *dname, *cur_name;
38331+ struct file *cur_xino;
b752ccd1 38332+ struct au_sbinfo *sbinfo;
062440b3 38333+ struct path *path, *cur_path;
b752ccd1
AM
38334+
38335+ SiMustWriteLock(sb);
38336+
38337+ err = 0;
38338+ sbinfo = au_sbi(sb);
062440b3
AM
38339+ path = &xiopt->file->f_path;
38340+ dentry = path->dentry;
38341+ parent = dget_parent(dentry);
b752ccd1
AM
38342+ if (remount) {
38343+ skip = 0;
b752ccd1
AM
38344+ cur_xino = sbinfo->si_xib;
38345+ if (cur_xino) {
062440b3
AM
38346+ cur_path = &cur_xino->f_path;
38347+ cur_dentry = cur_path->dentry;
38348+ cur_parent = dget_parent(cur_dentry);
38349+ cur_name = &cur_dentry->d_name;
38350+ dname = &dentry->d_name;
b752ccd1 38351+ skip = (cur_parent == parent
38d290e6 38352+ && au_qstreq(dname, cur_name));
b752ccd1
AM
38353+ dput(cur_parent);
38354+ }
38355+ if (skip)
38356+ goto out;
38357+ }
38358+
38359+ au_opt_set(sbinfo->si_mntflags, XINO);
062440b3
AM
38360+ err = au_xino_set_xib(sb, path);
38361+ /* si_x{read,write} are set */
b752ccd1 38362+ if (!err)
062440b3 38363+ err = au_xigen_set(sb, path);
b752ccd1 38364+ if (!err)
062440b3
AM
38365+ err = au_xino_set_br(sb, path);
38366+ if (!err) {
38367+ dbgaufs_brs_add(sb, 0, /*topdown*/1);
b752ccd1 38368+ goto out; /* success */
062440b3 38369+ }
b752ccd1
AM
38370+
38371+ /* reset all */
062440b3
AM
38372+ AuIOErr("failed setting xino(%d).\n", err);
38373+ au_xino_clr(sb);
b752ccd1 38374+
4f0767ce 38375+out:
b752ccd1
AM
38376+ dput(parent);
38377+ return err;
38378+}
38379+
b752ccd1
AM
38380+/*
38381+ * create a xinofile at the default place/path.
38382+ */
38383+struct file *au_xino_def(struct super_block *sb)
38384+{
38385+ struct file *file;
38386+ char *page, *p;
38387+ struct au_branch *br;
38388+ struct super_block *h_sb;
38389+ struct path path;
5afbbe0d 38390+ aufs_bindex_t bbot, bindex, bwr;
b752ccd1
AM
38391+
38392+ br = NULL;
5afbbe0d 38393+ bbot = au_sbbot(sb);
b752ccd1 38394+ bwr = -1;
5afbbe0d 38395+ for (bindex = 0; bindex <= bbot; bindex++) {
b752ccd1
AM
38396+ br = au_sbr(sb, bindex);
38397+ if (au_br_writable(br->br_perm)
86dc4139 38398+ && !au_test_fs_bad_xino(au_br_sb(br))) {
b752ccd1
AM
38399+ bwr = bindex;
38400+ break;
38401+ }
38402+ }
38403+
7f207e10
AM
38404+ if (bwr >= 0) {
38405+ file = ERR_PTR(-ENOMEM);
537831f9 38406+ page = (void *)__get_free_page(GFP_NOFS);
7f207e10
AM
38407+ if (unlikely(!page))
38408+ goto out;
86dc4139 38409+ path.mnt = au_br_mnt(br);
7f207e10
AM
38410+ path.dentry = au_h_dptr(sb->s_root, bwr);
38411+ p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
38412+ file = (void *)p;
38413+ if (!IS_ERR(p)) {
38414+ strcat(p, "/" AUFS_XINO_FNAME);
38415+ AuDbg("%s\n", p);
83b672a5 38416+ file = au_xino_create(sb, p, /*silent*/0, /*wbrtop*/1);
7f207e10 38417+ }
1c60b727 38418+ free_page((unsigned long)page);
7f207e10 38419+ } else {
83b672a5
AM
38420+ file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0,
38421+ /*wbrtop*/0);
7f207e10
AM
38422+ if (IS_ERR(file))
38423+ goto out;
2000de60 38424+ h_sb = file->f_path.dentry->d_sb;
7f207e10
AM
38425+ if (unlikely(au_test_fs_bad_xino(h_sb))) {
38426+ pr_err("xino doesn't support %s(%s)\n",
38427+ AUFS_XINO_DEFPATH, au_sbtype(h_sb));
38428+ fput(file);
38429+ file = ERR_PTR(-EINVAL);
38430+ }
7f207e10 38431+ }
0c5527e5 38432+
7f207e10
AM
38433+out:
38434+ return file;
38435+}
38436+
38437+/* ---------------------------------------------------------------------- */
38438+
062440b3
AM
38439+/*
38440+ * initialize the xinofile for the specified branch @br
38441+ * at the place/path where @base_file indicates.
38442+ * test whether another branch is on the same filesystem or not,
38443+ * if found then share the xinofile with another branch.
38444+ */
38445+int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
38446+ struct path *base)
7f207e10
AM
38447+{
38448+ int err;
062440b3
AM
38449+ struct au_xino_do_set_br args = {
38450+ .h_ino = h_ino,
38451+ .br = br
38452+ };
7f207e10 38453+
062440b3
AM
38454+ args.bshared = sbr_find_shared(sb, /*btop*/0, au_sbbot(sb),
38455+ au_br_sb(br));
38456+ err = au_xino_do_set_br(sb, base, &args);
79b8bda9 38457+ if (unlikely(err))
062440b3 38458+ au_xino_put(br);
7f207e10 38459+
7f207e10
AM
38460+ return err;
38461+}
521ced18
JR
38462+
38463+/* ---------------------------------------------------------------------- */
38464+
062440b3
AM
38465+/*
38466+ * get an unused inode number from bitmap
38467+ */
38468+ino_t au_xino_new_ino(struct super_block *sb)
38469+{
38470+ ino_t ino;
38471+ unsigned long *p, pindex, ul, pend;
38472+ struct au_sbinfo *sbinfo;
38473+ struct file *file;
38474+ int free_bit, err;
38475+
38476+ if (!au_opt_test(au_mntflags(sb), XINO))
38477+ return iunique(sb, AUFS_FIRST_INO);
38478+
38479+ sbinfo = au_sbi(sb);
38480+ mutex_lock(&sbinfo->si_xib_mtx);
38481+ p = sbinfo->si_xib_buf;
38482+ free_bit = sbinfo->si_xib_next_bit;
38483+ if (free_bit < page_bits && !test_bit(free_bit, p))
38484+ goto out; /* success */
38485+ free_bit = find_first_zero_bit(p, page_bits);
38486+ if (free_bit < page_bits)
38487+ goto out; /* success */
38488+
38489+ pindex = sbinfo->si_xib_last_pindex;
38490+ for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
38491+ err = xib_pindex(sb, ul);
38492+ if (unlikely(err))
38493+ goto out_err;
38494+ free_bit = find_first_zero_bit(p, page_bits);
38495+ if (free_bit < page_bits)
38496+ goto out; /* success */
38497+ }
38498+
38499+ file = sbinfo->si_xib;
38500+ pend = vfsub_f_size_read(file) / PAGE_SIZE;
38501+ for (ul = pindex + 1; ul <= pend; ul++) {
38502+ err = xib_pindex(sb, ul);
38503+ if (unlikely(err))
38504+ goto out_err;
38505+ free_bit = find_first_zero_bit(p, page_bits);
38506+ if (free_bit < page_bits)
38507+ goto out; /* success */
38508+ }
38509+ BUG();
38510+
38511+out:
38512+ set_bit(free_bit, p);
38513+ sbinfo->si_xib_next_bit = free_bit + 1;
38514+ pindex = sbinfo->si_xib_last_pindex;
38515+ mutex_unlock(&sbinfo->si_xib_mtx);
38516+ ino = xib_calc_ino(pindex, free_bit);
38517+ AuDbg("i%lu\n", (unsigned long)ino);
38518+ return ino;
38519+out_err:
38520+ mutex_unlock(&sbinfo->si_xib_mtx);
38521+ AuDbg("i0\n");
38522+ return 0;
38523+}
38524+
38525+/* for s_op->delete_inode() */
38526+void au_xino_delete_inode(struct inode *inode, const int unlinked)
521ced18 38527+{
062440b3
AM
38528+ int err;
38529+ unsigned int mnt_flags;
38530+ aufs_bindex_t bindex, bbot, bi;
38531+ unsigned char try_trunc;
38532+ struct au_iinfo *iinfo;
38533+ struct super_block *sb;
38534+ struct au_hinode *hi;
38535+ struct inode *h_inode;
38536+ struct au_branch *br;
acd2b654
AM
38537+ struct au_xi_calc calc;
38538+ struct file *file;
521ced18 38539+
062440b3 38540+ AuDebugOn(au_is_bad_inode(inode));
521ced18 38541+
062440b3
AM
38542+ sb = inode->i_sb;
38543+ mnt_flags = au_mntflags(sb);
38544+ if (!au_opt_test(mnt_flags, XINO)
38545+ || inode->i_ino == AUFS_ROOT_INO)
38546+ return;
38547+
38548+ if (unlinked) {
38549+ au_xigen_inc(inode);
38550+ au_xib_clear_bit(inode);
38551+ }
38552+
38553+ iinfo = au_ii(inode);
38554+ bindex = iinfo->ii_btop;
38555+ if (bindex < 0)
38556+ return;
38557+
062440b3
AM
38558+ try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
38559+ hi = au_hinode(iinfo, bindex);
38560+ bbot = iinfo->ii_bbot;
38561+ for (; bindex <= bbot; bindex++, hi++) {
38562+ h_inode = hi->hi_inode;
38563+ if (!h_inode
38564+ || (!unlinked && h_inode->i_nlink))
38565+ continue;
38566+
38567+ /* inode may not be revalidated */
38568+ bi = au_br_index(sb, hi->hi_id);
38569+ if (bi < 0)
38570+ continue;
38571+
38572+ br = au_sbr(sb, bi);
acd2b654
AM
38573+ au_xi_calc(sb, h_inode->i_ino, &calc);
38574+ file = au_xino_file(br->br_xino, calc.idx);
38575+ if (IS_ERR_OR_NULL(file))
38576+ continue;
38577+
e37dd06a 38578+ err = au_xino_do_write(file, &calc, /*ino*/0);
062440b3
AM
38579+ if (!err && try_trunc
38580+ && au_test_fs_trunc_xino(au_br_sb(br)))
38581+ xino_try_trunc(sb, br);
38582+ }
521ced18
JR
38583+}
38584+
062440b3
AM
38585+/* ---------------------------------------------------------------------- */
38586+
38587+static int au_xinondir_find(struct au_xino *xi, ino_t h_ino)
521ced18
JR
38588+{
38589+ int found, total, i;
38590+
38591+ found = -1;
062440b3 38592+ total = xi->xi_nondir.total;
521ced18 38593+ for (i = 0; i < total; i++) {
062440b3 38594+ if (xi->xi_nondir.array[i] != h_ino)
521ced18
JR
38595+ continue;
38596+ found = i;
38597+ break;
38598+ }
38599+
38600+ return found;
38601+}
38602+
062440b3 38603+static int au_xinondir_expand(struct au_xino *xi)
521ced18
JR
38604+{
38605+ int err, sz;
38606+ ino_t *p;
38607+
38608+ BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
38609+
38610+ err = -ENOMEM;
062440b3 38611+ sz = xi->xi_nondir.total * sizeof(ino_t);
521ced18
JR
38612+ if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
38613+ goto out;
062440b3 38614+ p = au_kzrealloc(xi->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
521ced18
JR
38615+ /*may_shrink*/0);
38616+ if (p) {
062440b3
AM
38617+ xi->xi_nondir.array = p;
38618+ xi->xi_nondir.total <<= 1;
38619+ AuDbg("xi_nondir.total %d\n", xi->xi_nondir.total);
521ced18
JR
38620+ err = 0;
38621+ }
38622+
38623+out:
38624+ return err;
38625+}
38626+
062440b3
AM
38627+void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
38628+ ino_t h_ino, int idx)
38629+{
38630+ struct au_xino *xi;
38631+
38632+ AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
38633+ xi = au_sbr(sb, bindex)->br_xino;
38634+ AuDebugOn(idx < 0 || xi->xi_nondir.total <= idx);
38635+
38636+ spin_lock(&xi->xi_nondir.spin);
38637+ AuDebugOn(xi->xi_nondir.array[idx] != h_ino);
38638+ xi->xi_nondir.array[idx] = 0;
38639+ spin_unlock(&xi->xi_nondir.spin);
38640+ wake_up_all(&xi->xi_nondir.wqh);
38641+}
38642+
521ced18
JR
38643+int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
38644+ int *idx)
38645+{
38646+ int err, found, empty;
062440b3 38647+ struct au_xino *xi;
521ced18
JR
38648+
38649+ err = 0;
38650+ *idx = -1;
38651+ if (!au_opt_test(au_mntflags(sb), XINO))
38652+ goto out; /* no xino */
38653+
062440b3 38654+ xi = au_sbr(sb, bindex)->br_xino;
521ced18
JR
38655+
38656+again:
062440b3
AM
38657+ spin_lock(&xi->xi_nondir.spin);
38658+ found = au_xinondir_find(xi, h_ino);
521ced18 38659+ if (found == -1) {
062440b3 38660+ empty = au_xinondir_find(xi, /*h_ino*/0);
521ced18 38661+ if (empty == -1) {
062440b3
AM
38662+ empty = xi->xi_nondir.total;
38663+ err = au_xinondir_expand(xi);
521ced18
JR
38664+ if (unlikely(err))
38665+ goto out_unlock;
38666+ }
062440b3 38667+ xi->xi_nondir.array[empty] = h_ino;
521ced18
JR
38668+ *idx = empty;
38669+ } else {
062440b3
AM
38670+ spin_unlock(&xi->xi_nondir.spin);
38671+ wait_event(xi->xi_nondir.wqh,
38672+ xi->xi_nondir.array[found] != h_ino);
521ced18
JR
38673+ goto again;
38674+ }
38675+
38676+out_unlock:
062440b3
AM
38677+ spin_unlock(&xi->xi_nondir.spin);
38678+out:
38679+ return err;
38680+}
38681+
38682+/* ---------------------------------------------------------------------- */
38683+
38684+int au_xino_path(struct seq_file *seq, struct file *file)
38685+{
38686+ int err;
38687+
38688+ err = au_seq_path(seq, &file->f_path);
38689+ if (unlikely(err))
38690+ goto out;
38691+
38692+#define Deleted "\\040(deleted)"
38693+ seq->count -= sizeof(Deleted) - 1;
38694+ AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
38695+ sizeof(Deleted) - 1));
38696+#undef Deleted
38697+
521ced18
JR
38698+out:
38699+ return err;
38700+}
537831f9 38701diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
eca34b5c 38702--- /usr/share/empty/include/uapi/linux/aufs_type.h 1970-01-01 01:00:00.000000000 +0100
46016270 38703+++ linux/include/uapi/linux/aufs_type.h 2021-06-30 21:35:11.397206648 +0200
eca34b5c
AM
38704@@ -0,0 +1,452 @@
38705+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
7f207e10 38706+/*
d58c55f2 38707+ * Copyright (C) 2005-2020 Junjiro R. Okajima
7f207e10
AM
38708+ *
38709+ * This program, aufs is free software; you can redistribute it and/or modify
38710+ * it under the terms of the GNU General Public License as published by
38711+ * the Free Software Foundation; either version 2 of the License, or
38712+ * (at your option) any later version.
38713+ *
38714+ * This program is distributed in the hope that it will be useful,
38715+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
38716+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38717+ * GNU General Public License for more details.
38718+ *
38719+ * You should have received a copy of the GNU General Public License
523b37e3 38720+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7f207e10
AM
38721+ */
38722+
38723+#ifndef __AUFS_TYPE_H__
38724+#define __AUFS_TYPE_H__
38725+
f6c5ef8b
AM
38726+#define AUFS_NAME "aufs"
38727+
9dbd164d 38728+#ifdef __KERNEL__
f6c5ef8b
AM
38729+/*
38730+ * define it before including all other headers.
38731+ * sched.h may use pr_* macros before defining "current", so define the
38732+ * no-current version first, and re-define later.
38733+ */
38734+#define pr_fmt(fmt) AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
38735+#include <linux/sched.h>
38736+#undef pr_fmt
a2a7ad62
AM
38737+#define pr_fmt(fmt) \
38738+ AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
38739+ (int)sizeof(current->comm), current->comm, current->pid
43982f53 38740+#include <linux/limits.h>
9dbd164d
AM
38741+#else
38742+#include <stdint.h>
38743+#include <sys/types.h>
43982f53 38744+#include <limits.h>
f6c5ef8b 38745+#endif /* __KERNEL__ */
7f207e10 38746+
46016270 38747+#define AUFS_VERSION "5.x-rcN-20210517"
7f207e10
AM
38748+
38749+/* todo? move this to linux-2.6.19/include/magic.h */
38750+#define AUFS_SUPER_MAGIC ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
38751+
38752+/* ---------------------------------------------------------------------- */
38753+
43982f53 38754+#ifdef __KERNEL__
7f207e10 38755+#ifdef CONFIG_AUFS_BRANCH_MAX_127
9dbd164d 38756+typedef int8_t aufs_bindex_t;
7f207e10
AM
38757+#define AUFS_BRANCH_MAX 127
38758+#else
9dbd164d 38759+typedef int16_t aufs_bindex_t;
7f207e10
AM
38760+#ifdef CONFIG_AUFS_BRANCH_MAX_511
38761+#define AUFS_BRANCH_MAX 511
38762+#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
38763+#define AUFS_BRANCH_MAX 1023
38764+#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
38765+#define AUFS_BRANCH_MAX 32767
38766+#endif
38767+#endif
38768+
7f207e10
AM
38769+#ifndef AUFS_BRANCH_MAX
38770+#error unknown CONFIG_AUFS_BRANCH_MAX value
38771+#endif
38772+#endif /* __KERNEL__ */
38773+
38774+/* ---------------------------------------------------------------------- */
38775+
7f207e10
AM
38776+#define AUFS_FSTYPE AUFS_NAME
38777+
38778+#define AUFS_ROOT_INO 2
38779+#define AUFS_FIRST_INO 11
38780+
38781+#define AUFS_WH_PFX ".wh."
38782+#define AUFS_WH_PFX_LEN ((int)sizeof(AUFS_WH_PFX) - 1)
38783+#define AUFS_WH_TMP_LEN 4
86dc4139 38784+/* a limit for rmdir/rename a dir and copyup */
7f207e10
AM
38785+#define AUFS_MAX_NAMELEN (NAME_MAX \
38786+ - AUFS_WH_PFX_LEN * 2 /* doubly whiteouted */\
38787+ - 1 /* dot */\
38788+ - AUFS_WH_TMP_LEN) /* hex */
38789+#define AUFS_XINO_FNAME "." AUFS_NAME ".xino"
38790+#define AUFS_XINO_DEFPATH "/tmp/" AUFS_XINO_FNAME
392086de
AM
38791+#define AUFS_XINO_DEF_SEC 30 /* seconds */
38792+#define AUFS_XINO_DEF_TRUNC 45 /* percentage */
7f207e10
AM
38793+#define AUFS_DIRWH_DEF 3
38794+#define AUFS_RDCACHE_DEF 10 /* seconds */
027c5e7a 38795+#define AUFS_RDCACHE_MAX 3600 /* seconds */
7f207e10
AM
38796+#define AUFS_RDBLK_DEF 512 /* bytes */
38797+#define AUFS_RDHASH_DEF 32
38798+#define AUFS_WKQ_NAME AUFS_NAME "d"
027c5e7a
AM
38799+#define AUFS_MFS_DEF_SEC 30 /* seconds */
38800+#define AUFS_MFS_MAX_SEC 3600 /* seconds */
076b876e 38801+#define AUFS_FHSM_CACHE_DEF_SEC 30 /* seconds */
86dc4139 38802+#define AUFS_PLINK_WARN 50 /* number of plinks in a single bucket */
7f207e10
AM
38803+
38804+/* pseudo-link maintenace under /proc */
38805+#define AUFS_PLINK_MAINT_NAME "plink_maint"
38806+#define AUFS_PLINK_MAINT_DIR "fs/" AUFS_NAME
38807+#define AUFS_PLINK_MAINT_PATH AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
38808+
8b6a4947
AM
38809+/* dirren, renamed dir */
38810+#define AUFS_DR_INFO_PFX AUFS_WH_PFX ".dr."
38811+#define AUFS_DR_BRHINO_NAME AUFS_WH_PFX "hino"
38812+/* whiteouted doubly */
38813+#define AUFS_WH_DR_INFO_PFX AUFS_WH_PFX AUFS_DR_INFO_PFX
38814+#define AUFS_WH_DR_BRHINO AUFS_WH_PFX AUFS_DR_BRHINO_NAME
38815+
7f207e10
AM
38816+#define AUFS_DIROPQ_NAME AUFS_WH_PFX ".opq" /* whiteouted doubly */
38817+#define AUFS_WH_DIROPQ AUFS_WH_PFX AUFS_DIROPQ_NAME
38818+
38819+#define AUFS_BASE_NAME AUFS_WH_PFX AUFS_NAME
38820+#define AUFS_PLINKDIR_NAME AUFS_WH_PFX "plnk"
38821+#define AUFS_ORPHDIR_NAME AUFS_WH_PFX "orph"
38822+
38823+/* doubly whiteouted */
38824+#define AUFS_WH_BASE AUFS_WH_PFX AUFS_BASE_NAME
38825+#define AUFS_WH_PLINKDIR AUFS_WH_PFX AUFS_PLINKDIR_NAME
38826+#define AUFS_WH_ORPHDIR AUFS_WH_PFX AUFS_ORPHDIR_NAME
38827+
1e00d052 38828+/* branch permissions and attributes */
7f207e10
AM
38829+#define AUFS_BRPERM_RW "rw"
38830+#define AUFS_BRPERM_RO "ro"
38831+#define AUFS_BRPERM_RR "rr"
076b876e
AM
38832+#define AUFS_BRATTR_COO_REG "coo_reg"
38833+#define AUFS_BRATTR_COO_ALL "coo_all"
38834+#define AUFS_BRATTR_FHSM "fhsm"
38835+#define AUFS_BRATTR_UNPIN "unpin"
c1595e42
JR
38836+#define AUFS_BRATTR_ICEX "icex"
38837+#define AUFS_BRATTR_ICEX_SEC "icexsec"
38838+#define AUFS_BRATTR_ICEX_SYS "icexsys"
38839+#define AUFS_BRATTR_ICEX_TR "icextr"
38840+#define AUFS_BRATTR_ICEX_USR "icexusr"
38841+#define AUFS_BRATTR_ICEX_OTH "icexoth"
1e00d052
AM
38842+#define AUFS_BRRATTR_WH "wh"
38843+#define AUFS_BRWATTR_NLWH "nolwh"
076b876e
AM
38844+#define AUFS_BRWATTR_MOO "moo"
38845+
38846+#define AuBrPerm_RW 1 /* writable, hardlinkable wh */
38847+#define AuBrPerm_RO (1 << 1) /* readonly */
38848+#define AuBrPerm_RR (1 << 2) /* natively readonly */
38849+#define AuBrPerm_Mask (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
38850+
38851+#define AuBrAttr_COO_REG (1 << 3) /* copy-up on open */
38852+#define AuBrAttr_COO_ALL (1 << 4)
38853+#define AuBrAttr_COO_Mask (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
38854+
38855+#define AuBrAttr_FHSM (1 << 5) /* file-based hsm */
38856+#define AuBrAttr_UNPIN (1 << 6) /* rename-able top dir of
c1595e42
JR
38857+ branch. meaningless since
38858+ linux-3.18-rc1 */
38859+
38860+/* ignore error in copying XATTR */
38861+#define AuBrAttr_ICEX_SEC (1 << 7)
38862+#define AuBrAttr_ICEX_SYS (1 << 8)
38863+#define AuBrAttr_ICEX_TR (1 << 9)
38864+#define AuBrAttr_ICEX_USR (1 << 10)
38865+#define AuBrAttr_ICEX_OTH (1 << 11)
38866+#define AuBrAttr_ICEX (AuBrAttr_ICEX_SEC \
38867+ | AuBrAttr_ICEX_SYS \
38868+ | AuBrAttr_ICEX_TR \
38869+ | AuBrAttr_ICEX_USR \
38870+ | AuBrAttr_ICEX_OTH)
38871+
38872+#define AuBrRAttr_WH (1 << 12) /* whiteout-able */
076b876e
AM
38873+#define AuBrRAttr_Mask AuBrRAttr_WH
38874+
c1595e42
JR
38875+#define AuBrWAttr_NoLinkWH (1 << 13) /* un-hardlinkable whiteouts */
38876+#define AuBrWAttr_MOO (1 << 14) /* move-up on open */
076b876e
AM
38877+#define AuBrWAttr_Mask (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
38878+
38879+#define AuBrAttr_CMOO_Mask (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
38880+
c1595e42 38881+/* #warning test userspace */
076b876e
AM
38882+#ifdef __KERNEL__
38883+#ifndef CONFIG_AUFS_FHSM
38884+#undef AuBrAttr_FHSM
38885+#define AuBrAttr_FHSM 0
38886+#endif
c1595e42
JR
38887+#ifndef CONFIG_AUFS_XATTR
38888+#undef AuBrAttr_ICEX
38889+#define AuBrAttr_ICEX 0
38890+#undef AuBrAttr_ICEX_SEC
38891+#define AuBrAttr_ICEX_SEC 0
38892+#undef AuBrAttr_ICEX_SYS
38893+#define AuBrAttr_ICEX_SYS 0
38894+#undef AuBrAttr_ICEX_TR
38895+#define AuBrAttr_ICEX_TR 0
38896+#undef AuBrAttr_ICEX_USR
38897+#define AuBrAttr_ICEX_USR 0
38898+#undef AuBrAttr_ICEX_OTH
38899+#define AuBrAttr_ICEX_OTH 0
38900+#endif
076b876e
AM
38901+#endif
38902+
38903+/* the longest combination */
c1595e42
JR
38904+/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
38905+#define AuBrPermStrSz sizeof(AUFS_BRPERM_RW \
38906+ "+" AUFS_BRATTR_COO_REG \
38907+ "+" AUFS_BRATTR_FHSM \
38908+ "+" AUFS_BRATTR_UNPIN \
7e9cd9fe
AM
38909+ "+" AUFS_BRATTR_ICEX_SEC \
38910+ "+" AUFS_BRATTR_ICEX_SYS \
38911+ "+" AUFS_BRATTR_ICEX_USR \
38912+ "+" AUFS_BRATTR_ICEX_OTH \
076b876e
AM
38913+ "+" AUFS_BRWATTR_NLWH)
38914+
38915+typedef struct {
38916+ char a[AuBrPermStrSz];
38917+} au_br_perm_str_t;
38918+
38919+static inline int au_br_writable(int brperm)
38920+{
38921+ return brperm & AuBrPerm_RW;
38922+}
38923+
38924+static inline int au_br_whable(int brperm)
38925+{
38926+ return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
38927+}
38928+
38929+static inline int au_br_wh_linkable(int brperm)
38930+{
38931+ return !(brperm & AuBrWAttr_NoLinkWH);
38932+}
38933+
38934+static inline int au_br_cmoo(int brperm)
38935+{
38936+ return brperm & AuBrAttr_CMOO_Mask;
38937+}
38938+
38939+static inline int au_br_fhsm(int brperm)
38940+{
38941+ return brperm & AuBrAttr_FHSM;
38942+}
7f207e10
AM
38943+
38944+/* ---------------------------------------------------------------------- */
38945+
38946+/* ioctl */
38947+enum {
38948+ /* readdir in userspace */
38949+ AuCtl_RDU,
38950+ AuCtl_RDU_INO,
38951+
076b876e
AM
38952+ AuCtl_WBR_FD, /* pathconf wrapper */
38953+ AuCtl_IBUSY, /* busy inode */
38954+ AuCtl_MVDOWN, /* move-down */
38955+ AuCtl_BR, /* info about branches */
38956+ AuCtl_FHSM_FD /* connection for fhsm */
7f207e10
AM
38957+};
38958+
38959+/* borrowed from linux/include/linux/kernel.h */
38960+#ifndef ALIGN
eca34b5c 38961+#ifdef _GNU_SOURCE
7f207e10 38962+#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
eca34b5c
AM
38963+#else
38964+#define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
38965+#endif
7f207e10
AM
38966+#define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
38967+#endif
38968+
38969+/* borrowed from linux/include/linux/compiler-gcc3.h */
38970+#ifndef __aligned
38971+#define __aligned(x) __attribute__((aligned(x)))
53392da6
AM
38972+#endif
38973+
38974+#ifdef __KERNEL__
38975+#ifndef __packed
7f207e10
AM
38976+#define __packed __attribute__((packed))
38977+#endif
53392da6 38978+#endif
7f207e10
AM
38979+
38980+struct au_rdu_cookie {
9dbd164d
AM
38981+ uint64_t h_pos;
38982+ int16_t bindex;
38983+ uint8_t flags;
38984+ uint8_t pad;
38985+ uint32_t generation;
7f207e10
AM
38986+} __aligned(8);
38987+
38988+struct au_rdu_ent {
9dbd164d
AM
38989+ uint64_t ino;
38990+ int16_t bindex;
38991+ uint8_t type;
38992+ uint8_t nlen;
38993+ uint8_t wh;
42b5c33a 38994+ char name[];
7f207e10
AM
38995+} __aligned(8);
38996+
38997+static inline int au_rdu_len(int nlen)
38998+{
38999+ /* include the terminating NULL */
39000+ return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
9dbd164d 39001+ sizeof(uint64_t));
7f207e10
AM
39002+}
39003+
39004+union au_rdu_ent_ul {
39005+ struct au_rdu_ent __user *e;
9dbd164d 39006+ uint64_t ul;
7f207e10
AM
39007+};
39008+
39009+enum {
39010+ AufsCtlRduV_SZ,
39011+ AufsCtlRduV_End
39012+};
39013+
39014+struct aufs_rdu {
39015+ /* input */
39016+ union {
9dbd164d
AM
39017+ uint64_t sz; /* AuCtl_RDU */
39018+ uint64_t nent; /* AuCtl_RDU_INO */
7f207e10
AM
39019+ };
39020+ union au_rdu_ent_ul ent;
9dbd164d 39021+ uint16_t verify[AufsCtlRduV_End];
7f207e10
AM
39022+
39023+ /* input/output */
9dbd164d 39024+ uint32_t blk;
7f207e10
AM
39025+
39026+ /* output */
39027+ union au_rdu_ent_ul tail;
39028+ /* number of entries which were added in a single call */
9dbd164d
AM
39029+ uint64_t rent;
39030+ uint8_t full;
39031+ uint8_t shwh;
7f207e10
AM
39032+
39033+ struct au_rdu_cookie cookie;
39034+} __aligned(8);
39035+
1e00d052
AM
39036+/* ---------------------------------------------------------------------- */
39037+
8b6a4947
AM
39038+/* dirren. the branch is identified by the filename who contains this */
39039+struct au_drinfo {
39040+ uint64_t ino;
39041+ union {
39042+ uint8_t oldnamelen;
39043+ uint64_t _padding;
39044+ };
42b5c33a 39045+ uint8_t oldname[];
8b6a4947
AM
39046+} __aligned(8);
39047+
39048+struct au_drinfo_fdata {
39049+ uint32_t magic;
39050+ struct au_drinfo drinfo;
39051+} __aligned(8);
39052+
39053+#define AUFS_DRINFO_MAGIC_V1 ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x01)
39054+/* future */
39055+#define AUFS_DRINFO_MAGIC_V2 ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x02)
39056+
39057+/* ---------------------------------------------------------------------- */
39058+
1e00d052 39059+struct aufs_wbr_fd {
9dbd164d
AM
39060+ uint32_t oflags;
39061+ int16_t brid;
1e00d052
AM
39062+} __aligned(8);
39063+
39064+/* ---------------------------------------------------------------------- */
39065+
027c5e7a 39066+struct aufs_ibusy {
9dbd164d
AM
39067+ uint64_t ino, h_ino;
39068+ int16_t bindex;
027c5e7a
AM
39069+} __aligned(8);
39070+
1e00d052
AM
39071+/* ---------------------------------------------------------------------- */
39072+
392086de
AM
39073+/* error code for move-down */
39074+/* the actual message strings are implemented in aufs-util.git */
39075+enum {
39076+ EAU_MVDOWN_OPAQUE = 1,
39077+ EAU_MVDOWN_WHITEOUT,
39078+ EAU_MVDOWN_UPPER,
39079+ EAU_MVDOWN_BOTTOM,
39080+ EAU_MVDOWN_NOUPPER,
39081+ EAU_MVDOWN_NOLOWERBR,
39082+ EAU_Last
39083+};
39084+
c2b27bf2 39085+/* flags for move-down */
392086de
AM
39086+#define AUFS_MVDOWN_DMSG 1
39087+#define AUFS_MVDOWN_OWLOWER (1 << 1) /* overwrite lower */
39088+#define AUFS_MVDOWN_KUPPER (1 << 2) /* keep upper */
39089+#define AUFS_MVDOWN_ROLOWER (1 << 3) /* do even if lower is RO */
39090+#define AUFS_MVDOWN_ROLOWER_R (1 << 4) /* did on lower RO */
39091+#define AUFS_MVDOWN_ROUPPER (1 << 5) /* do even if upper is RO */
39092+#define AUFS_MVDOWN_ROUPPER_R (1 << 6) /* did on upper RO */
39093+#define AUFS_MVDOWN_BRID_UPPER (1 << 7) /* upper brid */
39094+#define AUFS_MVDOWN_BRID_LOWER (1 << 8) /* lower brid */
076b876e
AM
39095+#define AUFS_MVDOWN_FHSM_LOWER (1 << 9) /* find fhsm attr for lower */
39096+#define AUFS_MVDOWN_STFS (1 << 10) /* req. stfs */
39097+#define AUFS_MVDOWN_STFS_FAILED (1 << 11) /* output: stfs is unusable */
39098+#define AUFS_MVDOWN_BOTTOM (1 << 12) /* output: no more lowers */
c2b27bf2 39099+
076b876e 39100+/* index for move-down */
392086de
AM
39101+enum {
39102+ AUFS_MVDOWN_UPPER,
39103+ AUFS_MVDOWN_LOWER,
39104+ AUFS_MVDOWN_NARRAY
39105+};
39106+
076b876e
AM
39107+/*
39108+ * additional info of move-down
39109+ * number of free blocks and inodes.
39110+ * subset of struct kstatfs, but smaller and always 64bit.
39111+ */
39112+struct aufs_stfs {
39113+ uint64_t f_blocks;
39114+ uint64_t f_bavail;
39115+ uint64_t f_files;
39116+ uint64_t f_ffree;
39117+};
39118+
39119+struct aufs_stbr {
39120+ int16_t brid; /* optional input */
39121+ int16_t bindex; /* output */
39122+ struct aufs_stfs stfs; /* output when AUFS_MVDOWN_STFS set */
39123+} __aligned(8);
39124+
c2b27bf2 39125+struct aufs_mvdown {
076b876e
AM
39126+ uint32_t flags; /* input/output */
39127+ struct aufs_stbr stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
39128+ int8_t au_errno; /* output */
39129+} __aligned(8);
39130+
39131+/* ---------------------------------------------------------------------- */
39132+
39133+union aufs_brinfo {
39134+ /* PATH_MAX may differ between kernel-space and user-space */
39135+ char _spacer[4096];
392086de 39136+ struct {
076b876e
AM
39137+ int16_t id;
39138+ int perm;
42b5c33a 39139+ char path[];
076b876e 39140+ };
c2b27bf2
AM
39141+} __aligned(8);
39142+
39143+/* ---------------------------------------------------------------------- */
39144+
7f207e10
AM
39145+#define AuCtlType 'A'
39146+#define AUFS_CTL_RDU _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
39147+#define AUFS_CTL_RDU_INO _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
1e00d052
AM
39148+#define AUFS_CTL_WBR_FD _IOW(AuCtlType, AuCtl_WBR_FD, \
39149+ struct aufs_wbr_fd)
027c5e7a 39150+#define AUFS_CTL_IBUSY _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
392086de
AM
39151+#define AUFS_CTL_MVDOWN _IOWR(AuCtlType, AuCtl_MVDOWN, \
39152+ struct aufs_mvdown)
076b876e
AM
39153+#define AUFS_CTL_BRINFO _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
39154+#define AUFS_CTL_FHSM_FD _IOW(AuCtlType, AuCtl_FHSM_FD, int)
7f207e10
AM
39155+
39156+#endif /* __AUFS_TYPE_H__ */
2121bcd9 39157SPDX-License-Identifier: GPL-2.0
46016270 39158aufs5.x-rcN loopback patch
5527c038
JR
39159
39160diff --git a/drivers/block/loop.c b/drivers/block/loop.c
46016270 39161index 794a8a1341989..0e5664a6753e5 100644
5527c038
JR
39162--- a/drivers/block/loop.c
39163+++ b/drivers/block/loop.c
46016270 39164@@ -647,6 +647,15 @@ static inline void loop_update_dio(struct loop_device *lo)
42a736d3 39165 lo->use_dio);
5527c038
JR
39166 }
39167
5527c038
JR
39168+static struct file *loop_real_file(struct file *file)
39169+{
39170+ struct file *f = NULL;
39171+
39172+ if (file->f_path.dentry->d_sb->s_op->real_loop)
39173+ f = file->f_path.dentry->d_sb->s_op->real_loop(file);
39174+ return f;
8b6a4947
AM
39175+}
39176+
c2c0f25c 39177 static void loop_reread_partitions(struct loop_device *lo,
8b6a4947
AM
39178 struct block_device *bdev)
39179 {
46016270 39180@@ -702,6 +711,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
5527c038
JR
39181 unsigned int arg)
39182 {
e1106418 39183 struct file *file = NULL, *old_file;
5527c038 39184+ struct file *f, *virt_file = NULL, *old_virt_file;
5527c038 39185 int error;
ba1aed25 39186 bool partscan;
5527c038 39187
46016270 39188@@ -721,12 +731,19 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
cd7a4cd9
AM
39189 file = fget(arg);
39190 if (!file)
e1106418 39191 goto out_err;
5527c038
JR
39192+ f = loop_real_file(file);
39193+ if (f) {
39194+ virt_file = file;
39195+ file = f;
39196+ get_file(file);
39197+ }
39198
cd7a4cd9
AM
39199 error = loop_validate_file(file, bdev);
39200 if (error)
e1106418 39201 goto out_err;
cd7a4cd9 39202
5527c038
JR
39203 old_file = lo->lo_backing_file;
39204+ old_virt_file = lo->lo_backing_virt_file;
39205
39206 error = -EINVAL;
39207
46016270 39208@@ -738,6 +755,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
8b6a4947
AM
39209 blk_mq_freeze_queue(lo->lo_queue);
39210 mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
39211 lo->lo_backing_file = file;
39212+ lo->lo_backing_virt_file = virt_file;
39213 lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
39214 mapping_set_gfp_mask(file->f_mapping,
39215 lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
46016270 39216@@ -751,6 +769,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
e1106418
JR
39217 * dependency.
39218 */
5527c038
JR
39219 fput(old_file);
39220+ if (old_virt_file)
39221+ fput(old_virt_file);
e1106418 39222 if (partscan)
c2c0f25c 39223 loop_reread_partitions(lo, bdev);
5527c038 39224 return 0;
46016270 39225@@ -759,6 +779,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
13f235e5 39226 mutex_unlock(&lo->lo_mutex);
e1106418
JR
39227 if (file)
39228 fput(file);
5527c038
JR
39229+ if (virt_file)
39230+ fput(virt_file);
5527c038
JR
39231 return error;
39232 }
e1106418 39233
46016270 39234@@ -1085,7 +1107,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
42a736d3
AM
39235 struct block_device *bdev,
39236 const struct loop_config *config)
5527c038 39237 {
062440b3 39238- struct file *file;
5527c038
JR
39239+ struct file *file, *f, *virt_file = NULL;
39240 struct inode *inode;
39241 struct address_space *mapping;
42a736d3 39242 int error;
46016270 39243@@ -1100,6 +1122,12 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
42a736d3 39244 file = fget(config->fd);
5527c038
JR
39245 if (!file)
39246 goto out;
39247+ f = loop_real_file(file);
39248+ if (f) {
39249+ virt_file = file;
39250+ file = f;
39251+ get_file(file);
39252+ }
39253
fbc438ed
JR
39254 /*
39255 * If we don't hold exclusive handle for the device, upgrade to it
46016270 39256@@ -1154,6 +1182,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
f24b0ddd 39257 lo->use_dio = lo->lo_flags & LO_FLAGS_DIRECT_IO;
5527c038 39258 lo->lo_device = bdev;
5527c038
JR
39259 lo->lo_backing_file = file;
39260+ lo->lo_backing_virt_file = virt_file;
f24b0ddd
JR
39261 lo->old_gfp_mask = mapping_gfp_mask(mapping);
39262 mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39263
46016270 39264@@ -1204,6 +1233,8 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
42a736d3 39265 bd_abort_claiming(bdev, loop_configure);
e1106418 39266 out_putf:
5527c038
JR
39267 fput(file);
39268+ if (virt_file)
39269+ fput(virt_file);
e1106418 39270 out:
5527c038
JR
39271 /* This is safe: open() is still holding a reference. */
39272 module_put(THIS_MODULE);
46016270 39273@@ -1213,6 +1244,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
ba1aed25 39274 static int __loop_clr_fd(struct loop_device *lo, bool release)
5527c038 39275 {
e1106418 39276 struct file *filp = NULL;
5527c038
JR
39277+ struct file *virt_filp = lo->lo_backing_virt_file;
39278 gfp_t gfp = lo->old_gfp_mask;
39279 struct block_device *bdev = lo->lo_device;
ba1aed25 39280 int err = 0;
46016270 39281@@ -1239,6 +1271,7 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
5527c038 39282
5527c038 39283 spin_lock_irq(&lo->lo_lock);
5527c038
JR
39284 lo->lo_backing_file = NULL;
39285+ lo->lo_backing_virt_file = NULL;
39286 spin_unlock_irq(&lo->lo_lock);
39287
39288 loop_release_xfer(lo);
46016270 39289@@ -1320,6 +1353,8 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
5527c038 39290 */
e1106418
JR
39291 if (filp)
39292 fput(filp);
5527c038
JR
39293+ if (virt_filp)
39294+ fput(virt_filp);
e1106418 39295 return err;
5527c038
JR
39296 }
39297
39298diff --git a/drivers/block/loop.h b/drivers/block/loop.h
0b2a12c6 39299index a3c04f310672e..161c3c5d1c22b 100644
5527c038
JR
39300--- a/drivers/block/loop.h
39301+++ b/drivers/block/loop.h
39302@@ -46,7 +46,7 @@ struct loop_device {
39303 int (*ioctl)(struct loop_device *, int cmd,
39304 unsigned long arg);
39305
39306- struct file * lo_backing_file;
43982f53 39307+ struct file *lo_backing_file, *lo_backing_virt_file;
5527c038 39308 struct block_device *lo_device;
5527c038 39309 void *key_data;
8b6a4947 39310
5527c038 39311diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
0b2a12c6 39312index 9f58ba0cb769f..1c2267a5a2ae1 100644
5527c038
JR
39313--- a/fs/aufs/f_op.c
39314+++ b/fs/aufs/f_op.c
42a736d3 39315@@ -304,7 +304,7 @@ static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
5527c038
JR
39316 if (IS_ERR(h_file))
39317 goto out;
39318
39319- if (au_test_loopback_kthread()) {
39320+ if (0 && au_test_loopback_kthread()) {
39321 au_warn_loopback(h_file->f_path.dentry->d_sb);
39322 if (file->f_mapping != h_file->f_mapping) {
39323 file->f_mapping = h_file->f_mapping;
39324diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
42b5c33a 39325index a8b63acc62045..9d97c3af5686a 100644
5527c038
JR
39326--- a/fs/aufs/loop.c
39327+++ b/fs/aufs/loop.c
2121bcd9 39328@@ -133,3 +133,19 @@ void au_loopback_fin(void)
79b8bda9 39329 symbol_put(loop_backing_file);
9f237c51 39330 au_kfree_try_rcu(au_warn_loopback_array);
5527c038
JR
39331 }
39332+
39333+/* ---------------------------------------------------------------------- */
39334+
39335+/* support the loopback block device insude aufs */
39336+
39337+struct file *aufs_real_loop(struct file *file)
39338+{
39339+ struct file *f;
39340+
39341+ BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
39342+ fi_read_lock(file);
39343+ f = au_hf_top(file);
39344+ fi_read_unlock(file);
39345+ AuDebugOn(!f);
39346+ return f;
39347+}
39348diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
42b5c33a 39349index 94f4f80ae33bf..ca1194354aff4 100644
5527c038
JR
39350--- a/fs/aufs/loop.h
39351+++ b/fs/aufs/loop.h
eca801bf 39352@@ -26,6 +26,8 @@ void au_warn_loopback(struct super_block *h_sb);
5527c038
JR
39353
39354 int au_loopback_init(void);
39355 void au_loopback_fin(void);
39356+
39357+struct file *aufs_real_loop(struct file *file);
39358 #else
83b672a5 39359 AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
eca801bf
AM
39360
39361@@ -36,6 +38,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
5527c038
JR
39362
39363 AuStubInt0(au_loopback_init, void)
39364 AuStubVoid(au_loopback_fin, void)
39365+
39366+AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
39367 #endif /* BLK_DEV_LOOP */
39368
39369 #endif /* __KERNEL__ */
39370diff --git a/fs/aufs/super.c b/fs/aufs/super.c
0b2a12c6 39371index d252963a87b53..ecfc5fc96ad8c 100644
5527c038
JR
39372--- a/fs/aufs/super.c
39373+++ b/fs/aufs/super.c
fbc438ed 39374@@ -844,7 +844,10 @@ static const struct super_operations aufs_sop = {
5527c038
JR
39375 .statfs = aufs_statfs,
39376 .put_super = aufs_put_super,
39377 .sync_fs = aufs_sync_fs,
39378- .remount_fs = aufs_remount_fs
39379+ .remount_fs = aufs_remount_fs,
39380+#ifdef CONFIG_AUFS_BDEV_LOOP
39381+ .real_loop = aufs_real_loop
39382+#endif
39383 };
39384
39385 /* ---------------------------------------------------------------------- */
39386diff --git a/include/linux/fs.h b/include/linux/fs.h
46016270 39387index 93eb43e002d97..8b392f6b36a62 100644
5527c038
JR
39388--- a/include/linux/fs.h
39389+++ b/include/linux/fs.h
46016270 39390@@ -2178,6 +2178,10 @@ struct super_operations {
5527c038
JR
39391 struct shrink_control *);
39392 long (*free_cached_objects)(struct super_block *,
39393 struct shrink_control *);
43982f53 39394+#if IS_ENABLED(CONFIG_BLK_DEV_LOOP) || IS_ENABLED(CONFIG_BLK_DEV_LOOP_MODULE)
5527c038
JR
39395+ /* and aufs */
39396+ struct file *(*real_loop)(struct file *);
39397+#endif
39398 };
39399
39400 /*
This page took 7.782706 seconds and 4 git commands to generate.