]> git.pld-linux.org Git - packages/kernel.git/blame_incremental - kernel-aufs5.patch
Up to 5.15.28.
[packages/kernel.git] / kernel-aufs5.patch
... / ...
CommitLineData
1SPDX-License-Identifier: GPL-2.0
2aufs5.15.5 kbuild patch
3
4diff --git a/fs/Kconfig b/fs/Kconfig
5index a6313a969bc5f..aca4b89d41a14 100644
6--- a/fs/Kconfig
7+++ b/fs/Kconfig
8@@ -312,6 +312,7 @@ source "fs/sysv/Kconfig"
9 source "fs/ufs/Kconfig"
10 source "fs/erofs/Kconfig"
11 source "fs/vboxsf/Kconfig"
12+source "fs/aufs/Kconfig"
13
14 endif # MISC_FILESYSTEMS
15
16diff --git a/fs/Makefile b/fs/Makefile
17index 84c5e4cdfee5a..b4fcdad8412ed 100644
18--- a/fs/Makefile
19+++ b/fs/Makefile
20@@ -138,3 +138,4 @@ obj-$(CONFIG_EFIVAR_FS) += efivarfs/
21 obj-$(CONFIG_EROFS_FS) += erofs/
22 obj-$(CONFIG_VBOXSF_FS) += vboxsf/
23 obj-$(CONFIG_ZONEFS_FS) += zonefs/
24+obj-$(CONFIG_AUFS_FS) += aufs/
25SPDX-License-Identifier: GPL-2.0
26aufs5.15.5 base patch
27
28diff --git a/MAINTAINERS b/MAINTAINERS
29index 3b79fd441dde8..3fb0a57f61b05 100644
30--- a/MAINTAINERS
31+++ b/MAINTAINERS
32@@ -3122,6 +3122,19 @@ F: include/uapi/linux/audit.h
33 F: kernel/audit*
34 F: lib/*audit.c
35
36+AUFS (advanced multi layered unification filesystem) FILESYSTEM
37+M: "J. R. Okajima" <hooanon05g@gmail.com>
38+L: aufs-users@lists.sourceforge.net (members only)
39+L: linux-unionfs@vger.kernel.org
40+S: Supported
41+W: http://aufs.sourceforge.net
42+T: git://github.com/sfjro/aufs4-linux.git
43+F: Documentation/ABI/testing/debugfs-aufs
44+F: Documentation/ABI/testing/sysfs-aufs
45+F: Documentation/filesystems/aufs/
46+F: fs/aufs/
47+F: include/uapi/linux/aufs_type.h
48+
49 AUXILIARY DISPLAY DRIVERS
50 M: Miguel Ojeda <ojeda@kernel.org>
51 S: Maintained
52diff --git a/drivers/block/loop.c b/drivers/block/loop.c
53index dfc72a1f6500d..c9d1ef703673c 100644
54--- a/drivers/block/loop.c
55+++ b/drivers/block/loop.c
56@@ -798,6 +798,24 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
57 return error;
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+}
76+EXPORT_SYMBOL_GPL(loop_backing_file);
77+
78 /* loop sysfs attributes */
79
80 static ssize_t loop_attr_show(struct device *dev, char *page,
81diff --git a/fs/dcache.c b/fs/dcache.c
82index cf871a81f4fdc..bc5095b734f58 100644
83--- a/fs/dcache.c
84+++ b/fs/dcache.c
85@@ -1320,7 +1320,7 @@ enum d_walk_ret {
86 *
87 * The @enter() callbacks are called with d_lock held.
88 */
89-static void d_walk(struct dentry *parent, void *data,
90+void d_walk(struct dentry *parent, void *data,
91 enum d_walk_ret (*enter)(void *, struct dentry *))
92 {
93 struct dentry *this_parent;
94diff --git a/fs/fcntl.c b/fs/fcntl.c
95index 9c6c6a3e2de51..02382fa9bd341 100644
96--- a/fs/fcntl.c
97+++ b/fs/fcntl.c
98@@ -33,7 +33,7 @@
99
100 #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME)
101
102-static int setfl(int fd, struct file * filp, unsigned long arg)
103+int setfl(int fd, struct file *filp, unsigned long arg)
104 {
105 struct inode * inode = file_inode(filp);
106 int error = 0;
107@@ -64,6 +64,8 @@ static int setfl(int fd, struct file * filp, unsigned long arg)
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
116diff --git a/fs/namespace.c b/fs/namespace.c
117index 659a8f39c61af..1283670737826 100644
118--- a/fs/namespace.c
119+++ b/fs/namespace.c
120@@ -808,6 +808,12 @@ static inline int check_mnt(struct mount *mnt)
121 return mnt->mnt_ns == current->nsproxy->mnt_ns;
122 }
123
124+/* for aufs, CONFIG_AUFS_BR_FUSE */
125+int is_current_mnt_ns(struct vfsmount *mnt)
126+{
127+ return check_mnt(real_mount(mnt));
128+}
129+
130 /*
131 * vfsmount lock must be held for write
132 */
133diff --git a/fs/splice.c b/fs/splice.c
134index 5dbce4dcc1a7d..3e6ba363b7775 100644
135--- a/fs/splice.c
136+++ b/fs/splice.c
137@@ -759,8 +759,8 @@ static int warn_unsupported(struct file *file, const char *op)
138 /*
139 * Attempt to initiate a splice from pipe to file.
140 */
141-static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
142- loff_t *ppos, size_t len, unsigned int flags)
143+long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
144+ loff_t *ppos, size_t len, unsigned int flags)
145 {
146 if (unlikely(!out->f_op->splice_write))
147 return warn_unsupported(out, "write");
148@@ -770,9 +770,9 @@ static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
149 /*
150 * Attempt to initiate a splice from a file to a pipe.
151 */
152-static long do_splice_to(struct file *in, loff_t *ppos,
153- struct pipe_inode_info *pipe, size_t len,
154- unsigned int flags)
155+long do_splice_to(struct file *in, loff_t *ppos,
156+ struct pipe_inode_info *pipe, size_t len,
157+ unsigned int flags)
158 {
159 unsigned int p_space;
160 int ret;
161diff --git a/fs/sync.c b/fs/sync.c
162index 1373a610dc784..b7b5a0a0df6ff 100644
163--- a/fs/sync.c
164+++ b/fs/sync.c
165@@ -28,7 +28,7 @@
166 * wait == 1 case since in that case write_inode() functions do
167 * sync_dirty_buffer() and thus effectively write one block at a time.
168 */
169-static int __sync_filesystem(struct super_block *sb, int wait)
170+int __sync_filesystem(struct super_block *sb, int wait)
171 {
172 if (wait)
173 sync_inodes_sb(sb);
174diff --git a/include/linux/fs.h b/include/linux/fs.h
175index 56eba723477e3..e60d8ad854004 100644
176--- a/include/linux/fs.h
177+++ b/include/linux/fs.h
178@@ -1381,6 +1381,7 @@ extern void fasync_free(struct fasync_struct *);
179 /* can be called from interrupts */
180 extern void kill_fasync(struct fasync_struct **, int, int);
181
182+extern int setfl(int fd, struct file *filp, unsigned long arg);
183 extern void __f_setown(struct file *filp, struct pid *, enum pid_type, int force);
184 extern int f_setown(struct file *filp, unsigned long arg, int force);
185 extern void f_delown(struct file *filp);
186@@ -2092,6 +2093,7 @@ struct file_operations {
187 ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
188 unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
189 int (*check_flags)(int);
190+ int (*setfl)(struct file *, unsigned long);
191 int (*flock) (struct file *, int, struct file_lock *);
192 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
193 ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
194@@ -2615,6 +2617,7 @@ extern int current_umask(void);
195 extern void ihold(struct inode * inode);
196 extern void iput(struct inode *);
197 extern int generic_update_time(struct inode *, struct timespec64 *, int);
198+extern int update_time(struct inode *, struct timespec64 *, int);
199
200 /* /sys/fs */
201 extern struct kobject *fs_kobj;
202@@ -2778,6 +2781,7 @@ static inline bool sb_is_blkdev_sb(struct super_block *sb)
203 }
204
205 void emergency_thaw_all(void);
206+extern int __sync_filesystem(struct super_block *, int);
207 extern int sync_filesystem(struct super_block *);
208 extern const struct file_operations def_blk_fops;
209 extern const struct file_operations def_chr_fops;
210diff --git a/include/linux/lockdep.h b/include/linux/lockdep.h
211index 9fe165beb0f9e..e47f7e15eeaf7 100644
212--- a/include/linux/lockdep.h
213+++ b/include/linux/lockdep.h
214@@ -248,6 +248,8 @@ static inline int lockdep_match_key(struct lockdep_map *lock,
215 return lock->key == key;
216 }
217
218+struct lock_class *lockdep_hlock_class(struct held_lock *hlock);
219+
220 /*
221 * Acquire a lock.
222 *
223diff --git a/include/linux/mnt_namespace.h b/include/linux/mnt_namespace.h
224index 8f882f5881e87..6b9808f098435 100644
225--- a/include/linux/mnt_namespace.h
226+++ b/include/linux/mnt_namespace.h
227@@ -7,12 +7,15 @@ struct mnt_namespace;
228 struct fs_struct;
229 struct user_namespace;
230 struct ns_common;
231+struct vfsmount;
232
233 extern struct mnt_namespace *copy_mnt_ns(unsigned long, struct mnt_namespace *,
234 struct user_namespace *, struct fs_struct *);
235 extern void put_mnt_ns(struct mnt_namespace *ns);
236 extern struct ns_common *from_mnt_ns(struct mnt_namespace *);
237
238+extern int is_current_mnt_ns(struct vfsmount *mnt);
239+
240 extern const struct file_operations proc_mounts_operations;
241 extern const struct file_operations proc_mountinfo_operations;
242 extern const struct file_operations proc_mountstats_operations;
243diff --git a/include/linux/splice.h b/include/linux/splice.h
244index a55179fd60fc3..8e21c53cf8831 100644
245--- a/include/linux/splice.h
246+++ b/include/linux/splice.h
247@@ -93,4 +93,10 @@ extern void splice_shrink_spd(struct splice_pipe_desc *);
248
249 extern const struct pipe_buf_operations page_cache_pipe_buf_ops;
250 extern const struct pipe_buf_operations default_pipe_buf_ops;
251+
252+extern long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
253+ loff_t *ppos, size_t len, unsigned int flags);
254+extern long do_splice_to(struct file *in, loff_t *ppos,
255+ struct pipe_inode_info *pipe, size_t len,
256+ unsigned int flags);
257 #endif
258diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
259index d624231eab2bb..eff2ca26e6c58 100644
260--- a/kernel/locking/lockdep.c
261+++ b/kernel/locking/lockdep.c
262@@ -189,7 +189,7 @@ static
263 struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
264 static DECLARE_BITMAP(lock_classes_in_use, MAX_LOCKDEP_KEYS);
265
266-static inline struct lock_class *hlock_class(struct held_lock *hlock)
267+inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
268 {
269 unsigned int class_idx = hlock->class_idx;
270
271@@ -210,6 +210,7 @@ static inline struct lock_class *hlock_class(struct held_lock *hlock)
272 */
273 return lock_classes + class_idx;
274 }
275+#define hlock_class(hlock) lockdep_hlock_class(hlock)
276
277 #ifdef CONFIG_LOCK_STAT
278 static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
279SPDX-License-Identifier: GPL-2.0
280aufs5.15.5 mmap patch
281
282diff --git a/fs/proc/base.c b/fs/proc/base.c
283index 1f394095eb880..93f2479ef319e 100644
284--- a/fs/proc/base.c
285+++ b/fs/proc/base.c
286@@ -2189,7 +2189,7 @@ static int map_files_get_link(struct dentry *dentry, struct path *path)
287 rc = -ENOENT;
288 vma = find_exact_vma(mm, vm_start, vm_end);
289 if (vma && vma->vm_file) {
290- *path = vma->vm_file->f_path;
291+ *path = vma_pr_or_file(vma)->f_path;
292 path_get(path);
293 rc = 0;
294 }
295diff --git a/fs/proc/nommu.c b/fs/proc/nommu.c
296index 13452b32e2bd5..38acccfef9d49 100644
297--- a/fs/proc/nommu.c
298+++ b/fs/proc/nommu.c
299@@ -40,7 +40,10 @@ static int nommu_region_show(struct seq_file *m, struct vm_region *region)
300 file = region->vm_file;
301
302 if (file) {
303- struct inode *inode = file_inode(region->vm_file);
304+ struct inode *inode;
305+
306+ file = vmr_pr_or_file(region);
307+ inode = file_inode(file);
308 dev = inode->i_sb->s_dev;
309 ino = inode->i_ino;
310 }
311diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
312index cf25be3e03212..70af1c25ffa22 100644
313--- a/fs/proc/task_mmu.c
314+++ b/fs/proc/task_mmu.c
315@@ -280,7 +280,10 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
316 const char *name = NULL;
317
318 if (file) {
319- struct inode *inode = file_inode(vma->vm_file);
320+ struct inode *inode;
321+
322+ file = vma_pr_or_file(vma);
323+ inode = file_inode(file);
324 dev = inode->i_sb->s_dev;
325 ino = inode->i_ino;
326 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
327@@ -1865,7 +1868,7 @@ static int show_numa_map(struct seq_file *m, void *v)
328 struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
329 struct vm_area_struct *vma = v;
330 struct numa_maps *md = &numa_priv->md;
331- struct file *file = vma->vm_file;
332+ struct file *file = vma_pr_or_file(vma);
333 struct mm_struct *mm = vma->vm_mm;
334 struct mempolicy *pol;
335 char buffer[64];
336diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c
337index a6d21fc0033c6..02c2de31196e0 100644
338--- a/fs/proc/task_nommu.c
339+++ b/fs/proc/task_nommu.c
340@@ -155,7 +155,10 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma)
341 file = vma->vm_file;
342
343 if (file) {
344- struct inode *inode = file_inode(vma->vm_file);
345+ struct inode *inode;
346+
347+ file = vma_pr_or_file(vma);
348+ inode = file_inode(file);
349 dev = inode->i_sb->s_dev;
350 ino = inode->i_ino;
351 pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
352diff --git a/include/linux/mm.h b/include/linux/mm.h
353index 73a52aba448f9..5dd42acf0707b 100644
354--- a/include/linux/mm.h
355+++ b/include/linux/mm.h
356@@ -1806,6 +1806,28 @@ static inline void unmap_shared_mapping_range(struct address_space *mapping,
357 unmap_mapping_range(mapping, holebegin, holelen, 0);
358 }
359
360+extern void vma_do_file_update_time(struct vm_area_struct *, const char[], int);
361+extern struct file *vma_do_pr_or_file(struct vm_area_struct *, const char[],
362+ int);
363+extern void vma_do_get_file(struct vm_area_struct *, const char[], int);
364+extern void vma_do_fput(struct vm_area_struct *, const char[], int);
365+
366+#define vma_file_update_time(vma) vma_do_file_update_time(vma, __func__, \
367+ __LINE__)
368+#define vma_pr_or_file(vma) vma_do_pr_or_file(vma, __func__, \
369+ __LINE__)
370+#define vma_get_file(vma) vma_do_get_file(vma, __func__, __LINE__)
371+#define vma_fput(vma) vma_do_fput(vma, __func__, __LINE__)
372+
373+#ifndef CONFIG_MMU
374+extern struct file *vmr_do_pr_or_file(struct vm_region *, const char[], int);
375+extern void vmr_do_fput(struct vm_region *, const char[], int);
376+
377+#define vmr_pr_or_file(region) vmr_do_pr_or_file(region, __func__, \
378+ __LINE__)
379+#define vmr_fput(region) vmr_do_fput(region, __func__, __LINE__)
380+#endif /* !CONFIG_MMU */
381+
382 extern int access_process_vm(struct task_struct *tsk, unsigned long addr,
383 void *buf, int len, unsigned int gup_flags);
384 extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
385diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
386index 7f8ee09c711f4..3a9a798a4ae1f 100644
387--- a/include/linux/mm_types.h
388+++ b/include/linux/mm_types.h
389@@ -294,6 +294,7 @@ struct vm_region {
390 unsigned long vm_top; /* region allocated to here */
391 unsigned long vm_pgoff; /* the offset in vm_file corresponding to vm_start */
392 struct file *vm_file; /* the backing file or NULL */
393+ struct file *vm_prfile; /* the virtual backing file or NULL */
394
395 int vm_usage; /* region usage count (access under nommu_region_sem) */
396 bool vm_icache_flushed : 1; /* true if the icache has been flushed for
397@@ -373,6 +374,7 @@ struct vm_area_struct {
398 unsigned long vm_pgoff; /* Offset (within vm_file) in PAGE_SIZE
399 units */
400 struct file * vm_file; /* File we map to (can be NULL). */
401+ struct file *vm_prfile; /* shadow of vm_file */
402 void * vm_private_data; /* was vm_pte (shared mem) */
403
404 #ifdef CONFIG_SWAP
405diff --git a/kernel/fork.c b/kernel/fork.c
406index 10885c649ca42..f3a9cd12011b3 100644
407--- a/kernel/fork.c
408+++ b/kernel/fork.c
409@@ -573,7 +573,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
410 if (file) {
411 struct address_space *mapping = file->f_mapping;
412
413- get_file(file);
414+ vma_get_file(tmp);
415 i_mmap_lock_write(mapping);
416 if (tmp->vm_flags & VM_SHARED)
417 mapping_allow_writable(mapping);
418diff --git a/mm/Makefile b/mm/Makefile
419index fc60a40ce954b..c715b0138237b 100644
420--- a/mm/Makefile
421+++ b/mm/Makefile
422@@ -52,7 +52,7 @@ obj-y := filemap.o mempool.o oom_kill.o fadvise.o \
423 mm_init.o percpu.o slab_common.o \
424 compaction.o vmacache.o \
425 interval_tree.o list_lru.o workingset.o \
426- debug.o gup.o mmap_lock.o $(mmu-y)
427+ prfile.o debug.o gup.o mmap_lock.o $(mmu-y)
428
429 # Give 'page_alloc' its own module-parameter namespace
430 page-alloc-y := page_alloc.o
431diff --git a/mm/filemap.c b/mm/filemap.c
432index 82a17c35eb96b..348e22067abdc 100644
433--- a/mm/filemap.c
434+++ b/mm/filemap.c
435@@ -3349,7 +3349,7 @@ vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
436 vm_fault_t ret = VM_FAULT_LOCKED;
437
438 sb_start_pagefault(mapping->host->i_sb);
439- file_update_time(vmf->vma->vm_file);
440+ vma_file_update_time(vmf->vma);
441 lock_page(page);
442 if (page->mapping != mapping) {
443 unlock_page(page);
444diff --git a/mm/mmap.c b/mm/mmap.c
445index 88dcc5c252255..6c276614ca962 100644
446--- a/mm/mmap.c
447+++ b/mm/mmap.c
448@@ -183,7 +183,7 @@ static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
449 if (vma->vm_ops && vma->vm_ops->close)
450 vma->vm_ops->close(vma);
451 if (vma->vm_file)
452- fput(vma->vm_file);
453+ vma_fput(vma);
454 mpol_put(vma_policy(vma));
455 vm_area_free(vma);
456 return next;
457@@ -952,7 +952,7 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
458 if (remove_next) {
459 if (file) {
460 uprobe_munmap(next, next->vm_start, next->vm_end);
461- fput(file);
462+ vma_fput(vma);
463 }
464 if (next->anon_vma)
465 anon_vma_merge(vma, next);
466@@ -1873,7 +1873,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
467 return addr;
468
469 unmap_and_free_vma:
470- fput(vma->vm_file);
471+ vma_fput(vma);
472 vma->vm_file = NULL;
473
474 /* Undo any partial mapping done by a device driver. */
475@@ -2731,7 +2731,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
476 goto out_free_mpol;
477
478 if (new->vm_file)
479- get_file(new->vm_file);
480+ vma_get_file(new);
481
482 if (new->vm_ops && new->vm_ops->open)
483 new->vm_ops->open(new);
484@@ -2750,7 +2750,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
485 if (new->vm_ops && new->vm_ops->close)
486 new->vm_ops->close(new);
487 if (new->vm_file)
488- fput(new->vm_file);
489+ vma_fput(new);
490 unlink_anon_vmas(new);
491 out_free_mpol:
492 mpol_put(vma_policy(new));
493@@ -2945,7 +2945,7 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
494 struct vm_area_struct *vma;
495 unsigned long populate = 0;
496 unsigned long ret = -EINVAL;
497- struct file *file;
498+ struct file *file, *prfile;
499
500 pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/vm/remap_file_pages.rst.\n",
501 current->comm, current->pid);
502@@ -3001,10 +3001,27 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
503 if (vma->vm_flags & VM_LOCKED)
504 flags |= MAP_LOCKED;
505
506- file = get_file(vma->vm_file);
507+ vma_get_file(vma);
508+ file = vma->vm_file;
509+ prfile = vma->vm_prfile;
510 ret = do_mmap(vma->vm_file, start, size,
511 prot, flags, pgoff, &populate, NULL);
512+ if (!IS_ERR_VALUE(ret) && file && prfile) {
513+ struct vm_area_struct *new_vma;
514+
515+ new_vma = find_vma(mm, ret);
516+ if (!new_vma->vm_prfile)
517+ new_vma->vm_prfile = prfile;
518+ if (new_vma != vma)
519+ get_file(prfile);
520+ }
521+ /*
522+ * two fput()s instead of vma_fput(vma),
523+ * coz vma may not be available anymore.
524+ */
525 fput(file);
526+ if (prfile)
527+ fput(prfile);
528 out:
529 mmap_write_unlock(mm);
530 if (populate)
531@@ -3285,7 +3302,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
532 if (anon_vma_clone(new_vma, vma))
533 goto out_free_mempol;
534 if (new_vma->vm_file)
535- get_file(new_vma->vm_file);
536+ vma_get_file(new_vma);
537 if (new_vma->vm_ops && new_vma->vm_ops->open)
538 new_vma->vm_ops->open(new_vma);
539 vma_link(mm, new_vma, prev, rb_link, rb_parent);
540diff --git a/mm/nommu.c b/mm/nommu.c
541index 02d2427b8f9e8..a7419302ab4ed 100644
542--- a/mm/nommu.c
543+++ b/mm/nommu.c
544@@ -523,7 +523,7 @@ static void __put_nommu_region(struct vm_region *region)
545 up_write(&nommu_region_sem);
546
547 if (region->vm_file)
548- fput(region->vm_file);
549+ vmr_fput(region);
550
551 /* IO memory and memory shared directly out of the pagecache
552 * from ramfs/tmpfs mustn't be released here */
553@@ -655,7 +655,7 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
554 if (vma->vm_ops && vma->vm_ops->close)
555 vma->vm_ops->close(vma);
556 if (vma->vm_file)
557- fput(vma->vm_file);
558+ vma_fput(vma);
559 put_nommu_region(vma->vm_region);
560 vm_area_free(vma);
561 }
562@@ -1175,7 +1175,7 @@ unsigned long do_mmap(struct file *file,
563 goto error_just_free;
564 }
565 }
566- fput(region->vm_file);
567+ vmr_fput(region);
568 kmem_cache_free(vm_region_jar, region);
569 region = pregion;
570 result = start;
571@@ -1252,10 +1252,10 @@ unsigned long do_mmap(struct file *file,
572 up_write(&nommu_region_sem);
573 error:
574 if (region->vm_file)
575- fput(region->vm_file);
576+ vmr_fput(region);
577 kmem_cache_free(vm_region_jar, region);
578 if (vma->vm_file)
579- fput(vma->vm_file);
580+ vma_fput(vma);
581 vm_area_free(vma);
582 return ret;
583
584diff --git a/mm/prfile.c b/mm/prfile.c
585new file mode 100644
586index 0000000000000..511543ab1b418
587--- /dev/null
588+++ b/mm/prfile.c
589@@ -0,0 +1,86 @@
590+// SPDX-License-Identifier: GPL-2.0
591+/*
592+ * Mainly for aufs which mmap(2) different file and wants to print different
593+ * path in /proc/PID/maps.
594+ * Call these functions via macros defined in linux/mm.h.
595+ *
596+ * See Documentation/filesystems/aufs/design/06mmap.txt
597+ *
598+ * Copyright (c) 2014-2021 Junjro R. Okajima
599+ * Copyright (c) 2014 Ian Campbell
600+ */
601+
602+#include <linux/mm.h>
603+#include <linux/file.h>
604+#include <linux/fs.h>
605+
606+/* #define PRFILE_TRACE */
607+static inline void prfile_trace(struct file *f, struct file *pr,
608+ const char func[], int line, const char func2[])
609+{
610+#ifdef PRFILE_TRACE
611+ if (pr)
612+ pr_info("%s:%d: %s, %pD2\n", func, line, func2, f);
613+#endif
614+}
615+
616+void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
617+ int line)
618+{
619+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
620+
621+ prfile_trace(f, pr, func, line, __func__);
622+ file_update_time(f);
623+ if (f && pr)
624+ file_update_time(pr);
625+}
626+
627+struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
628+ int line)
629+{
630+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
631+
632+ prfile_trace(f, pr, func, line, __func__);
633+ return (f && pr) ? pr : f;
634+}
635+
636+void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
637+{
638+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
639+
640+ prfile_trace(f, pr, func, line, __func__);
641+ get_file(f);
642+ if (f && pr)
643+ get_file(pr);
644+}
645+
646+void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
647+{
648+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
649+
650+ prfile_trace(f, pr, func, line, __func__);
651+ fput(f);
652+ if (f && pr)
653+ fput(pr);
654+}
655+
656+#ifndef CONFIG_MMU
657+struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
658+ int line)
659+{
660+ struct file *f = region->vm_file, *pr = region->vm_prfile;
661+
662+ prfile_trace(f, pr, func, line, __func__);
663+ return (f && pr) ? pr : f;
664+}
665+
666+void vmr_do_fput(struct vm_region *region, const char func[], int line)
667+{
668+ struct file *f = region->vm_file, *pr = region->vm_prfile;
669+
670+ prfile_trace(f, pr, func, line, __func__);
671+ fput(f);
672+ if (f && pr)
673+ fput(pr);
674+}
675+#endif /* !CONFIG_MMU */
676SPDX-License-Identifier: GPL-2.0
677aufs5.15.5 standalone patch
678
679diff --git a/fs/dcache.c b/fs/dcache.c
680index bc5095b734f58..9508bd57a3bc0 100644
681--- a/fs/dcache.c
682+++ b/fs/dcache.c
683@@ -1425,6 +1425,7 @@ void d_walk(struct dentry *parent, void *data,
684 seq = 1;
685 goto again;
686 }
687+EXPORT_SYMBOL_GPL(d_walk);
688
689 struct check_mount {
690 struct vfsmount *mnt;
691@@ -2970,6 +2971,7 @@ void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
692
693 write_sequnlock(&rename_lock);
694 }
695+EXPORT_SYMBOL_GPL(d_exchange);
696
697 /**
698 * d_ancestor - search for an ancestor
699diff --git a/fs/exec.c b/fs/exec.c
700index ac7b51b51f38a..52a8be4ebc1e1 100644
701--- a/fs/exec.c
702+++ b/fs/exec.c
703@@ -111,6 +111,7 @@ bool path_noexec(const struct path *path)
704 return (path->mnt->mnt_flags & MNT_NOEXEC) ||
705 (path->mnt->mnt_sb->s_iflags & SB_I_NOEXEC);
706 }
707+EXPORT_SYMBOL_GPL(path_noexec);
708
709 #ifdef CONFIG_USELIB
710 /*
711diff --git a/fs/fcntl.c b/fs/fcntl.c
712index 02382fa9bd341..3418c60b90146 100644
713--- a/fs/fcntl.c
714+++ b/fs/fcntl.c
715@@ -86,6 +86,7 @@ int setfl(int fd, struct file *filp, unsigned long arg)
716 out:
717 return error;
718 }
719+EXPORT_SYMBOL_GPL(setfl);
720
721 static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
722 int force)
723diff --git a/fs/file_table.c b/fs/file_table.c
724index 45437f8e1003e..786af52904fcf 100644
725--- a/fs/file_table.c
726+++ b/fs/file_table.c
727@@ -161,6 +161,7 @@ struct file *alloc_empty_file(int flags, const struct cred *cred)
728 }
729 return ERR_PTR(-ENFILE);
730 }
731+EXPORT_SYMBOL_GPL(alloc_empty_file);
732
733 /*
734 * Variant of alloc_empty_file() that doesn't check and modify nr_files.
735@@ -375,6 +376,7 @@ void __fput_sync(struct file *file)
736 }
737
738 EXPORT_SYMBOL(fput);
739+EXPORT_SYMBOL_GPL(__fput_sync);
740
741 void __init files_init(void)
742 {
743diff --git a/fs/namespace.c b/fs/namespace.c
744index 1283670737826..db99365620118 100644
745--- a/fs/namespace.c
746+++ b/fs/namespace.c
747@@ -439,6 +439,7 @@ void __mnt_drop_write(struct vfsmount *mnt)
748 mnt_dec_writers(real_mount(mnt));
749 preempt_enable();
750 }
751+EXPORT_SYMBOL_GPL(__mnt_drop_write);
752
753 /**
754 * mnt_drop_write - give up write access to a mount
755@@ -813,6 +814,7 @@ int is_current_mnt_ns(struct vfsmount *mnt)
756 {
757 return check_mnt(real_mount(mnt));
758 }
759+EXPORT_SYMBOL_GPL(is_current_mnt_ns);
760
761 /*
762 * vfsmount lock must be held for write
763@@ -2011,6 +2013,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
764 }
765 return 0;
766 }
767+EXPORT_SYMBOL_GPL(iterate_mounts);
768
769 static void lock_mnt_tree(struct mount *mnt)
770 {
771diff --git a/fs/notify/group.c b/fs/notify/group.c
772index fb89c351295d6..460ad19c2570a 100644
773--- a/fs/notify/group.c
774+++ b/fs/notify/group.c
775@@ -100,6 +100,7 @@ void fsnotify_get_group(struct fsnotify_group *group)
776 {
777 refcount_inc(&group->refcnt);
778 }
779+EXPORT_SYMBOL_GPL(fsnotify_get_group);
780
781 /*
782 * Drop a reference to a group. Free it if it's through.
783diff --git a/fs/open.c b/fs/open.c
784index e0df1536eb69f..81b2d7c83addb 100644
785--- a/fs/open.c
786+++ b/fs/open.c
787@@ -65,6 +65,7 @@ int do_truncate(struct user_namespace *mnt_userns, struct dentry *dentry,
788 inode_unlock(dentry->d_inode);
789 return ret;
790 }
791+EXPORT_SYMBOL_GPL(do_truncate);
792
793 long vfs_truncate(const struct path *path, loff_t length)
794 {
795diff --git a/fs/read_write.c b/fs/read_write.c
796index af057c57bdc64..76017f8331fbf 100644
797--- a/fs/read_write.c
798+++ b/fs/read_write.c
799@@ -492,6 +492,7 @@ ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
800 inc_syscr(current);
801 return ret;
802 }
803+EXPORT_SYMBOL_GPL(vfs_read);
804
805 static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
806 {
807@@ -602,6 +603,7 @@ ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_
808 file_end_write(file);
809 return ret;
810 }
811+EXPORT_SYMBOL_GPL(vfs_write);
812
813 /* file_ppos returns &file->f_pos or NULL if file is stream */
814 static inline loff_t *file_ppos(struct file *file)
815diff --git a/fs/splice.c b/fs/splice.c
816index 3e6ba363b7775..7c1be373eb7cd 100644
817--- a/fs/splice.c
818+++ b/fs/splice.c
819@@ -766,6 +766,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
820 return warn_unsupported(out, "write");
821 return out->f_op->splice_write(pipe, out, ppos, len, flags);
822 }
823+EXPORT_SYMBOL_GPL(do_splice_from);
824
825 /*
826 * Attempt to initiate a splice from a file to a pipe.
827@@ -795,6 +796,7 @@ long do_splice_to(struct file *in, loff_t *ppos,
828 return warn_unsupported(in, "read");
829 return in->f_op->splice_read(in, ppos, pipe, len, flags);
830 }
831+EXPORT_SYMBOL_GPL(do_splice_to);
832
833 /**
834 * splice_direct_to_actor - splices data directly between two non-pipes
835diff --git a/fs/sync.c b/fs/sync.c
836index b7b5a0a0df6ff..fa5c7fba7f1ba 100644
837--- a/fs/sync.c
838+++ b/fs/sync.c
839@@ -39,6 +39,7 @@ int __sync_filesystem(struct super_block *sb, int wait)
840 sb->s_op->sync_fs(sb, wait);
841 return __sync_blockdev(sb->s_bdev, wait);
842 }
843+EXPORT_SYMBOL_GPL(__sync_filesystem);
844
845 /*
846 * Write out and wait upon all dirty data associated with this
847diff --git a/fs/xattr.c b/fs/xattr.c
848index 5c8c5175b385c..ff7e9ff774b73 100644
849--- a/fs/xattr.c
850+++ b/fs/xattr.c
851@@ -384,6 +384,7 @@ vfs_getxattr_alloc(struct user_namespace *mnt_userns, struct dentry *dentry,
852 *xattr_value = value;
853 return error;
854 }
855+EXPORT_SYMBOL_GPL(vfs_getxattr_alloc);
856
857 ssize_t
858 __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
859diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
860index eff2ca26e6c58..0627584f7872a 100644
861--- a/kernel/locking/lockdep.c
862+++ b/kernel/locking/lockdep.c
863@@ -210,6 +210,7 @@ inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
864 */
865 return lock_classes + class_idx;
866 }
867+EXPORT_SYMBOL_GPL(lockdep_hlock_class);
868 #define hlock_class(hlock) lockdep_hlock_class(hlock)
869
870 #ifdef CONFIG_LOCK_STAT
871diff --git a/kernel/task_work.c b/kernel/task_work.c
872index 1698fbe6f0e13..081b05acadf82 100644
873--- a/kernel/task_work.c
874+++ b/kernel/task_work.c
875@@ -167,3 +167,4 @@ void task_work_run(void)
876 } while (work);
877 }
878 }
879+EXPORT_SYMBOL_GPL(task_work_run);
880diff --git a/security/security.c b/security/security.c
881index 67264cb08fb31..0d78065d71fe1 100644
882--- a/security/security.c
883+++ b/security/security.c
884@@ -1147,6 +1147,7 @@ int security_path_rmdir(const struct path *dir, struct dentry *dentry)
885 return 0;
886 return call_int_hook(path_rmdir, 0, dir, dentry);
887 }
888+EXPORT_SYMBOL_GPL(security_path_rmdir);
889
890 int security_path_unlink(const struct path *dir, struct dentry *dentry)
891 {
892@@ -1163,6 +1164,7 @@ int security_path_symlink(const struct path *dir, struct dentry *dentry,
893 return 0;
894 return call_int_hook(path_symlink, 0, dir, dentry, old_name);
895 }
896+EXPORT_SYMBOL_GPL(security_path_symlink);
897
898 int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
899 struct dentry *new_dentry)
900@@ -1171,6 +1173,7 @@ int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
901 return 0;
902 return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
903 }
904+EXPORT_SYMBOL_GPL(security_path_link);
905
906 int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
907 const struct path *new_dir, struct dentry *new_dentry,
908@@ -1198,6 +1201,7 @@ int security_path_truncate(const struct path *path)
909 return 0;
910 return call_int_hook(path_truncate, 0, path);
911 }
912+EXPORT_SYMBOL_GPL(security_path_truncate);
913
914 int security_path_chmod(const struct path *path, umode_t mode)
915 {
916@@ -1205,6 +1209,7 @@ int security_path_chmod(const struct path *path, umode_t mode)
917 return 0;
918 return call_int_hook(path_chmod, 0, path, mode);
919 }
920+EXPORT_SYMBOL_GPL(security_path_chmod);
921
922 int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
923 {
924@@ -1212,6 +1217,7 @@ int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
925 return 0;
926 return call_int_hook(path_chown, 0, path, uid, gid);
927 }
928+EXPORT_SYMBOL_GPL(security_path_chown);
929
930 int security_path_chroot(const struct path *path)
931 {
932@@ -1312,6 +1318,7 @@ int security_inode_permission(struct inode *inode, int mask)
933 return 0;
934 return call_int_hook(inode_permission, 0, inode, mask);
935 }
936+EXPORT_SYMBOL_GPL(security_inode_permission);
937
938 int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
939 {
940@@ -1509,6 +1516,7 @@ int security_file_permission(struct file *file, int mask)
941
942 return fsnotify_perm(file, mask);
943 }
944+EXPORT_SYMBOL_GPL(security_file_permission);
945
946 int security_file_alloc(struct file *file)
947 {
948diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
949--- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs 1970-01-01 01:00:00.000000000 +0100
950+++ linux/Documentation/ABI/testing/debugfs-aufs 2021-12-03 15:38:59.926647310 +0100
951@@ -0,0 +1,55 @@
952+What: /debug/aufs/si_<id>/
953+Date: March 2009
954+Contact: J. R. Okajima <hooanon05g@gmail.com>
955+Description:
956+ Under /debug/aufs, a directory named si_<id> is created
957+ per aufs mount, where <id> is a unique id generated
958+ internally.
959+
960+What: /debug/aufs/si_<id>/plink
961+Date: Apr 2013
962+Contact: J. R. Okajima <hooanon05g@gmail.com>
963+Description:
964+ It has three lines and shows the information about the
965+ pseudo-link. The first line is a single number
966+ representing a number of buckets. The second line is a
967+ number of pseudo-links per buckets (separated by a
968+ blank). The last line is a single number representing a
969+ total number of psedo-links.
970+ When the aufs mount option 'noplink' is specified, it
971+ will show "1\n0\n0\n".
972+
973+What: /debug/aufs/si_<id>/xib
974+Date: March 2009
975+Contact: J. R. Okajima <hooanon05g@gmail.com>
976+Description:
977+ It shows the consumed blocks by xib (External Inode Number
978+ Bitmap), its block size and file size.
979+ When the aufs mount option 'noxino' is specified, it
980+ will be empty. About XINO files, see the aufs manual.
981+
982+What: /debug/aufs/si_<id>/xi<branch-index>
983+Date: March 2009
984+Contact: J. R. Okajima <hooanon05g@gmail.com>
985+Description:
986+ It shows the consumed blocks by xino (External Inode Number
987+ Translation Table), its link count, block size and file
988+ size.
989+ Due to the file size limit, there may exist multiple
990+ xino files per branch. In this case, "-N" is added to
991+ the filename and it corresponds to the index of the
992+ internal xino array. "-0" is omitted.
993+ When the aufs mount option 'noxino' is specified, Those
994+ entries won't exist. About XINO files, see the aufs
995+ manual.
996+
997+What: /debug/aufs/si_<id>/xigen
998+Date: March 2009
999+Contact: J. R. Okajima <hooanon05g@gmail.com>
1000+Description:
1001+ It shows the consumed blocks by xigen (External Inode
1002+ Generation Table), its block size and file size.
1003+ If CONFIG_AUFS_EXPORT is disabled, this entry will not
1004+ be created.
1005+ When the aufs mount option 'noxino' is specified, it
1006+ will be empty. About XINO files, see the aufs manual.
1007diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
1008--- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs 1970-01-01 01:00:00.000000000 +0100
1009+++ linux/Documentation/ABI/testing/sysfs-aufs 2021-12-03 15:38:59.926647310 +0100
1010@@ -0,0 +1,31 @@
1011+What: /sys/fs/aufs/si_<id>/
1012+Date: March 2009
1013+Contact: J. R. Okajima <hooanon05g@gmail.com>
1014+Description:
1015+ Under /sys/fs/aufs, a directory named si_<id> is created
1016+ per aufs mount, where <id> is a unique id generated
1017+ internally.
1018+
1019+What: /sys/fs/aufs/si_<id>/br<idx>
1020+Date: March 2009
1021+Contact: J. R. Okajima <hooanon05g@gmail.com>
1022+Description:
1023+ It shows the abolute path of a member directory (which
1024+ is called branch) in aufs, and its permission.
1025+
1026+What: /sys/fs/aufs/si_<id>/brid<idx>
1027+Date: July 2013
1028+Contact: J. R. Okajima <hooanon05g@gmail.com>
1029+Description:
1030+ It shows the id of a member directory (which is called
1031+ branch) in aufs.
1032+
1033+What: /sys/fs/aufs/si_<id>/xi_path
1034+Date: March 2009
1035+Contact: J. R. Okajima <hooanon05g@gmail.com>
1036+Description:
1037+ It shows the abolute path of XINO (External Inode Number
1038+ Bitmap, Translation Table and Generation Table) file
1039+ even if it is the default path.
1040+ When the aufs mount option 'noxino' is specified, it
1041+ will be empty. About XINO files, see the aufs manual.
1042diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
1043--- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt 1970-01-01 01:00:00.000000000 +0100
1044+++ linux/Documentation/filesystems/aufs/design/01intro.txt 2021-12-03 15:38:59.929980643 +0100
1045@@ -0,0 +1,171 @@
1046+
1047+# Copyright (C) 2005-2021 Junjiro R. Okajima
1048+#
1049+# This program is free software; you can redistribute it and/or modify
1050+# it under the terms of the GNU General Public License as published by
1051+# the Free Software Foundation; either version 2 of the License, or
1052+# (at your option) any later version.
1053+#
1054+# This program is distributed in the hope that it will be useful,
1055+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1056+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1057+# GNU General Public License for more details.
1058+#
1059+# You should have received a copy of the GNU General Public License
1060+# along with this program. If not, see <http://www.gnu.org/licenses/>.
1061+
1062+Introduction
1063+----------------------------------------
1064+
1065+aufs [ei ju: ef es] | /ey-yoo-ef-es/ | [a u f s]
1066+1. abbrev. for "advanced multi-layered unification filesystem".
1067+2. abbrev. for "another unionfs".
1068+3. abbrev. for "auf das" in German which means "on the" in English.
1069+ Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
1070+ But "Filesystem aufs Filesystem" is hard to understand.
1071+4. abbrev. for "African Urban Fashion Show".
1072+
1073+AUFS is a filesystem with features:
1074+- multi layered stackable unification filesystem, the member directory
1075+ is called as a branch.
1076+- branch permission and attribute, 'readonly', 'real-readonly',
1077+ 'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
1078+ combination.
1079+- internal "file copy-on-write".
1080+- logical deletion, whiteout.
1081+- dynamic branch manipulation, adding, deleting and changing permission.
1082+- allow bypassing aufs, user's direct branch access.
1083+- external inode number translation table and bitmap which maintains the
1084+ persistent aufs inode number.
1085+- seekable directory, including NFS readdir.
1086+- file mapping, mmap and sharing pages.
1087+- pseudo-link, hardlink over branches.
1088+- loopback mounted filesystem as a branch.
1089+- several policies to select one among multiple writable branches.
1090+- revert a single systemcall when an error occurs in aufs.
1091+- and more...
1092+
1093+
1094+Multi Layered Stackable Unification Filesystem
1095+----------------------------------------------------------------------
1096+Most people already knows what it is.
1097+It is a filesystem which unifies several directories and provides a
1098+merged single directory. When users access a file, the access will be
1099+passed/re-directed/converted (sorry, I am not sure which English word is
1100+correct) to the real file on the member filesystem. The member
1101+filesystem is called 'lower filesystem' or 'branch' and has a mode
1102+'readonly' and 'readwrite.' And the deletion for a file on the lower
1103+readonly branch is handled by creating 'whiteout' on the upper writable
1104+branch.
1105+
1106+On LKML, there have been discussions about UnionMount (Jan Blunck,
1107+Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
1108+different approaches to implement the merged-view.
1109+The former tries putting it into VFS, and the latter implements as a
1110+separate filesystem.
1111+(If I misunderstand about these implementations, please let me know and
1112+I shall correct it. Because it is a long time ago when I read their
1113+source files last time).
1114+
1115+UnionMount's approach will be able to small, but may be hard to share
1116+branches between several UnionMount since the whiteout in it is
1117+implemented in the inode on branch filesystem and always
1118+shared. According to Bharata's post, readdir does not seems to be
1119+finished yet.
1120+There are several missing features known in this implementations such as
1121+- for users, the inode number may change silently. eg. copy-up.
1122+- link(2) may break by copy-up.
1123+- read(2) may get an obsoleted filedata (fstat(2) too).
1124+- fcntl(F_SETLK) may be broken by copy-up.
1125+- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
1126+ open(O_RDWR).
1127+
1128+In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
1129+merged into mainline. This is another implementation of UnionMount as a
1130+separated filesystem. All the limitations and known problems which
1131+UnionMount are equally inherited to "overlay" filesystem.
1132+
1133+Unionfs has a longer history. When I started implementing a stackable
1134+filesystem (Aug 2005), it already existed. It has virtual super_block,
1135+inode, dentry and file objects and they have an array pointing lower
1136+same kind objects. After contributing many patches for Unionfs, I
1137+re-started my project AUFS (Jun 2006).
1138+
1139+In AUFS, the structure of filesystem resembles to Unionfs, but I
1140+implemented my own ideas, approaches and enhancements and it became
1141+totally different one.
1142+
1143+Comparing DM snapshot and fs based implementation
1144+- the number of bytes to be copied between devices is much smaller.
1145+- the type of filesystem must be one and only.
1146+- the fs must be writable, no readonly fs, even for the lower original
1147+ device. so the compression fs will not be usable. but if we use
1148+ loopback mount, we may address this issue.
1149+ for instance,
1150+ mount /cdrom/squashfs.img /sq
1151+ losetup /sq/ext2.img
1152+ losetup /somewhere/cow
1153+ dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1154+- it will be difficult (or needs more operations) to extract the
1155+ difference between the original device and COW.
1156+- DM snapshot-merge may help a lot when users try merging. in the
1157+ fs-layer union, users will use rsync(1).
1158+
1159+You may want to read my old paper "Filesystems in LiveCD"
1160+(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
1161+
1162+
1163+Several characters/aspects/persona of aufs
1164+----------------------------------------------------------------------
1165+
1166+Aufs has several characters, aspects or persona.
1167+1. a filesystem, callee of VFS helper
1168+2. sub-VFS, caller of VFS helper for branches
1169+3. a virtual filesystem which maintains persistent inode number
1170+4. reader/writer of files on branches such like an application
1171+
1172+1. Callee of VFS Helper
1173+As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1174+unlink(2) from an application reaches sys_unlink() kernel function and
1175+then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1176+calls filesystem specific unlink operation. Actually aufs implements the
1177+unlink operation but it behaves like a redirector.
1178+
1179+2. Caller of VFS Helper for Branches
1180+aufs_unlink() passes the unlink request to the branch filesystem as if
1181+it were called from VFS. So the called unlink operation of the branch
1182+filesystem acts as usual. As a caller of VFS helper, aufs should handle
1183+every necessary pre/post operation for the branch filesystem.
1184+- acquire the lock for the parent dir on a branch
1185+- lookup in a branch
1186+- revalidate dentry on a branch
1187+- mnt_want_write() for a branch
1188+- vfs_unlink() for a branch
1189+- mnt_drop_write() for a branch
1190+- release the lock on a branch
1191+
1192+3. Persistent Inode Number
1193+One of the most important issue for a filesystem is to maintain inode
1194+numbers. This is particularly important to support exporting a
1195+filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1196+backend block device for its own. But some storage is necessary to
1197+keep and maintain the inode numbers. It may be a large space and may not
1198+suit to keep in memory. Aufs rents some space from its first writable
1199+branch filesystem (by default) and creates file(s) on it. These files
1200+are created by aufs internally and removed soon (currently) keeping
1201+opened.
1202+Note: Because these files are removed, they are totally gone after
1203+ unmounting aufs. It means the inode numbers are not persistent
1204+ across unmount or reboot. I have a plan to make them really
1205+ persistent which will be important for aufs on NFS server.
1206+
1207+4. Read/Write Files Internally (copy-on-write)
1208+Because a branch can be readonly, when you write a file on it, aufs will
1209+"copy-up" it to the upper writable branch internally. And then write the
1210+originally requested thing to the file. Generally kernel doesn't
1211+open/read/write file actively. In aufs, even a single write may cause a
1212+internal "file copy". This behaviour is very similar to cp(1) command.
1213+
1214+Some people may think it is better to pass such work to user space
1215+helper, instead of doing in kernel space. Actually I am still thinking
1216+about it. But currently I have implemented it in kernel space.
1217diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
1218--- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
1219+++ linux/Documentation/filesystems/aufs/design/02struct.txt 2021-12-03 15:38:59.929980643 +0100
1220@@ -0,0 +1,258 @@
1221+
1222+# Copyright (C) 2005-2021 Junjiro R. Okajima
1223+#
1224+# This program is free software; you can redistribute it and/or modify
1225+# it under the terms of the GNU General Public License as published by
1226+# the Free Software Foundation; either version 2 of the License, or
1227+# (at your option) any later version.
1228+#
1229+# This program is distributed in the hope that it will be useful,
1230+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1231+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1232+# GNU General Public License for more details.
1233+#
1234+# You should have received a copy of the GNU General Public License
1235+# along with this program. If not, see <http://www.gnu.org/licenses/>.
1236+
1237+Basic Aufs Internal Structure
1238+
1239+Superblock/Inode/Dentry/File Objects
1240+----------------------------------------------------------------------
1241+As like an ordinary filesystem, aufs has its own
1242+superblock/inode/dentry/file objects. All these objects have a
1243+dynamically allocated array and store the same kind of pointers to the
1244+lower filesystem, branch.
1245+For example, when you build a union with one readwrite branch and one
1246+readonly, mounted /au, /rw and /ro respectively.
1247+- /au = /rw + /ro
1248+- /ro/fileA exists but /rw/fileA
1249+
1250+Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1251+pointers are stored in a aufs dentry. The array in aufs dentry will be,
1252+- [0] = NULL (because /rw/fileA doesn't exist)
1253+- [1] = /ro/fileA
1254+
1255+This style of an array is essentially same to the aufs
1256+superblock/inode/dentry/file objects.
1257+
1258+Because aufs supports manipulating branches, ie. add/delete/change
1259+branches dynamically, these objects has its own generation. When
1260+branches are changed, the generation in aufs superblock is
1261+incremented. And a generation in other object are compared when it is
1262+accessed. When a generation in other objects are obsoleted, aufs
1263+refreshes the internal array.
1264+
1265+
1266+Superblock
1267+----------------------------------------------------------------------
1268+Additionally aufs superblock has some data for policies to select one
1269+among multiple writable branches, XIB files, pseudo-links and kobject.
1270+See below in detail.
1271+About the policies which supports copy-down a directory, see
1272+wbr_policy.txt too.
1273+
1274+
1275+Branch and XINO(External Inode Number Translation Table)
1276+----------------------------------------------------------------------
1277+Every branch has its own xino (external inode number translation table)
1278+file. The xino file is created and unlinked by aufs internally. When two
1279+members of a union exist on the same filesystem, they share the single
1280+xino file.
1281+The struct of a xino file is simple, just a sequence of aufs inode
1282+numbers which is indexed by the lower inode number.
1283+In the above sample, assume the inode number of /ro/fileA is i111 and
1284+aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1285+4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1286+
1287+When the inode numbers are not contiguous, the xino file will be sparse
1288+which has a hole in it and doesn't consume as much disk space as it
1289+might appear. If your branch filesystem consumes disk space for such
1290+holes, then you should specify 'xino=' option at mounting aufs.
1291+
1292+Aufs has a mount option to free the disk blocks for such holes in XINO
1293+files on tmpfs or ramdisk. But it is not so effective actually. If you
1294+meet a problem of disk shortage due to XINO files, then you should try
1295+"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1296+The patch localizes the assignment inumbers per tmpfs-mount and avoid
1297+the holes in XINO files.
1298+
1299+Also a writable branch has three kinds of "whiteout bases". All these
1300+are existed when the branch is joined to aufs, and their names are
1301+whiteout-ed doubly, so that users will never see their names in aufs
1302+hierarchy.
1303+1. a regular file which will be hardlinked to all whiteouts.
1304+2. a directory to store a pseudo-link.
1305+3. a directory to store an "orphan"-ed file temporary.
1306+
1307+1. Whiteout Base
1308+ When you remove a file on a readonly branch, aufs handles it as a
1309+ logical deletion and creates a whiteout on the upper writable branch
1310+ as a hardlink of this file in order not to consume inode on the
1311+ writable branch.
1312+2. Pseudo-link Dir
1313+ See below, Pseudo-link.
1314+3. Step-Parent Dir
1315+ When "fileC" exists on the lower readonly branch only and it is
1316+ opened and removed with its parent dir, and then user writes
1317+ something into it, then aufs copies-up fileC to this
1318+ directory. Because there is no other dir to store fileC. After
1319+ creating a file under this dir, the file is unlinked.
1320+
1321+Because aufs supports manipulating branches, ie. add/delete/change
1322+dynamically, a branch has its own id. When the branch order changes,
1323+aufs finds the new index by searching the branch id.
1324+
1325+
1326+Pseudo-link
1327+----------------------------------------------------------------------
1328+Assume "fileA" exists on the lower readonly branch only and it is
1329+hardlinked to "fileB" on the branch. When you write something to fileA,
1330+aufs copies-up it to the upper writable branch. Additionally aufs
1331+creates a hardlink under the Pseudo-link Directory of the writable
1332+branch. The inode of a pseudo-link is kept in aufs super_block as a
1333+simple list. If fileB is read after unlinking fileA, aufs returns
1334+filedata from the pseudo-link instead of the lower readonly
1335+branch. Because the pseudo-link is based upon the inode, to keep the
1336+inode number by xino (see above) is essentially necessary.
1337+
1338+All the hardlinks under the Pseudo-link Directory of the writable branch
1339+should be restored in a proper location later. Aufs provides a utility
1340+to do this. The userspace helpers executed at remounting and unmounting
1341+aufs by default.
1342+During this utility is running, it puts aufs into the pseudo-link
1343+maintenance mode. In this mode, only the process which began the
1344+maintenance mode (and its child processes) is allowed to operate in
1345+aufs. Some other processes which are not related to the pseudo-link will
1346+be allowed to run too, but the rest have to return an error or wait
1347+until the maintenance mode ends. If a process already acquires an inode
1348+mutex (in VFS), it has to return an error.
1349+
1350+
1351+XIB(external inode number bitmap)
1352+----------------------------------------------------------------------
1353+Addition to the xino file per a branch, aufs has an external inode number
1354+bitmap in a superblock object. It is also an internal file such like a
1355+xino file.
1356+It is a simple bitmap to mark whether the aufs inode number is in-use or
1357+not.
1358+To reduce the file I/O, aufs prepares a single memory page to cache xib.
1359+
1360+As well as XINO files, aufs has a feature to truncate/refresh XIB to
1361+reduce the number of consumed disk blocks for these files.
1362+
1363+
1364+Virtual or Vertical Dir, and Readdir in Userspace
1365+----------------------------------------------------------------------
1366+In order to support multiple layers (branches), aufs readdir operation
1367+constructs a virtual dir block on memory. For readdir, aufs calls
1368+vfs_readdir() internally for each dir on branches, merges their entries
1369+with eliminating the whiteout-ed ones, and sets it to file (dir)
1370+object. So the file object has its entry list until it is closed. The
1371+entry list will be updated when the file position is zero and becomes
1372+obsoleted. This decision is made in aufs automatically.
1373+
1374+The dynamically allocated memory block for the name of entries has a
1375+unit of 512 bytes (by default) and stores the names contiguously (no
1376+padding). Another block for each entry is handled by kmem_cache too.
1377+During building dir blocks, aufs creates hash list and judging whether
1378+the entry is whiteouted by its upper branch or already listed.
1379+The merged result is cached in the corresponding inode object and
1380+maintained by a customizable life-time option.
1381+
1382+Some people may call it can be a security hole or invite DoS attack
1383+since the opened and once readdir-ed dir (file object) holds its entry
1384+list and becomes a pressure for system memory. But I'd say it is similar
1385+to files under /proc or /sys. The virtual files in them also holds a
1386+memory page (generally) while they are opened. When an idea to reduce
1387+memory for them is introduced, it will be applied to aufs too.
1388+For those who really hate this situation, I've developed readdir(3)
1389+library which operates this merging in userspace. You just need to set
1390+LD_PRELOAD environment variable, and aufs will not consume no memory in
1391+kernel space for readdir(3).
1392+
1393+
1394+Workqueue
1395+----------------------------------------------------------------------
1396+Aufs sometimes requires privilege access to a branch. For instance,
1397+in copy-up/down operation. When a user process is going to make changes
1398+to a file which exists in the lower readonly branch only, and the mode
1399+of one of ancestor directories may not be writable by a user
1400+process. Here aufs copy-up the file with its ancestors and they may
1401+require privilege to set its owner/group/mode/etc.
1402+This is a typical case of a application character of aufs (see
1403+Introduction).
1404+
1405+Aufs uses workqueue synchronously for this case. It creates its own
1406+workqueue. The workqueue is a kernel thread and has privilege. Aufs
1407+passes the request to call mkdir or write (for example), and wait for
1408+its completion. This approach solves a problem of a signal handler
1409+simply.
1410+If aufs didn't adopt the workqueue and changed the privilege of the
1411+process, then the process may receive the unexpected SIGXFSZ or other
1412+signals.
1413+
1414+Also aufs uses the system global workqueue ("events" kernel thread) too
1415+for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1416+whiteout base and etc. This is unrelated to a privilege.
1417+Most of aufs operation tries acquiring a rw_semaphore for aufs
1418+superblock at the beginning, at the same time waits for the completion
1419+of all queued asynchronous tasks.
1420+
1421+
1422+Whiteout
1423+----------------------------------------------------------------------
1424+The whiteout in aufs is very similar to Unionfs's. That is represented
1425+by its filename. UnionMount takes an approach of a file mode, but I am
1426+afraid several utilities (find(1) or something) will have to support it.
1427+
1428+Basically the whiteout represents "logical deletion" which stops aufs to
1429+lookup further, but also it represents "dir is opaque" which also stop
1430+further lookup.
1431+
1432+In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1433+In order to make several functions in a single systemcall to be
1434+revertible, aufs adopts an approach to rename a directory to a temporary
1435+unique whiteouted name.
1436+For example, in rename(2) dir where the target dir already existed, aufs
1437+renames the target dir to a temporary unique whiteouted name before the
1438+actual rename on a branch, and then handles other actions (make it opaque,
1439+update the attributes, etc). If an error happens in these actions, aufs
1440+simply renames the whiteouted name back and returns an error. If all are
1441+succeeded, aufs registers a function to remove the whiteouted unique
1442+temporary name completely and asynchronously to the system global
1443+workqueue.
1444+
1445+
1446+Copy-up
1447+----------------------------------------------------------------------
1448+It is a well-known feature or concept.
1449+When user modifies a file on a readonly branch, aufs operate "copy-up"
1450+internally and makes change to the new file on the upper writable branch.
1451+When the trigger systemcall does not update the timestamps of the parent
1452+dir, aufs reverts it after copy-up.
1453+
1454+
1455+Move-down (aufs3.9 and later)
1456+----------------------------------------------------------------------
1457+"Copy-up" is one of the essential feature in aufs. It copies a file from
1458+the lower readonly branch to the upper writable branch when a user
1459+changes something about the file.
1460+"Move-down" is an opposite action of copy-up. Basically this action is
1461+ran manually instead of automatically and internally.
1462+For desgin and implementation, aufs has to consider these issues.
1463+- whiteout for the file may exist on the lower branch.
1464+- ancestor directories may not exist on the lower branch.
1465+- diropq for the ancestor directories may exist on the upper branch.
1466+- free space on the lower branch will reduce.
1467+- another access to the file may happen during moving-down, including
1468+ UDBA (see "Revalidate Dentry and UDBA").
1469+- the file should not be hard-linked nor pseudo-linked. they should be
1470+ handled by auplink utility later.
1471+
1472+Sometimes users want to move-down a file from the upper writable branch
1473+to the lower readonly or writable branch. For instance,
1474+- the free space of the upper writable branch is going to run out.
1475+- create a new intermediate branch between the upper and lower branch.
1476+- etc.
1477+
1478+For this purpose, use "aumvdown" command in aufs-util.git.
1479diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt linux/Documentation/filesystems/aufs/design/03atomic_open.txt
1480--- /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt 1970-01-01 01:00:00.000000000 +0100
1481+++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt 2021-12-03 15:38:59.929980643 +0100
1482@@ -0,0 +1,85 @@
1483+
1484+# Copyright (C) 2015-2021 Junjiro R. Okajima
1485+#
1486+# This program is free software; you can redistribute it and/or modify
1487+# it under the terms of the GNU General Public License as published by
1488+# the Free Software Foundation; either version 2 of the License, or
1489+# (at your option) any later version.
1490+#
1491+# This program is distributed in the hope that it will be useful,
1492+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1493+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1494+# GNU General Public License for more details.
1495+#
1496+# You should have received a copy of the GNU General Public License
1497+# along with this program. If not, see <http://www.gnu.org/licenses/>.
1498+
1499+Support for a branch who has its ->atomic_open()
1500+----------------------------------------------------------------------
1501+The filesystems who implement its ->atomic_open() are not majority. For
1502+example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
1503+particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
1504+->atomic_open(), NFSv4 returns an error for this open(2). While I am not
1505+sure whether all filesystems who have ->atomic_open() behave like this,
1506+but NFSv4 surely returns the error.
1507+
1508+In order to support ->atomic_open() for aufs, there are a few
1509+approaches.
1510+
1511+A. Introduce aufs_atomic_open()
1512+ - calls one of VFS:do_last(), lookup_open() or atomic_open() for
1513+ branch fs.
1514+B. Introduce aufs_atomic_open() calling create, open and chmod. this is
1515+ an aufs user Pip Cet's approach
1516+ - calls aufs_create(), VFS finish_open() and notify_change().
1517+ - pass fake-mode to finish_open(), and then correct the mode by
1518+ notify_change().
1519+C. Extend aufs_open() to call branch fs's ->atomic_open()
1520+ - no aufs_atomic_open().
1521+ - aufs_lookup() registers the TID to an aufs internal object.
1522+ - aufs_create() does nothing when the matching TID is registered, but
1523+ registers the mode.
1524+ - aufs_open() calls branch fs's ->atomic_open() when the matching
1525+ TID is registered.
1526+D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
1527+ credential
1528+ - no aufs_atomic_open().
1529+ - aufs_create() registers the TID to an internal object. this info
1530+ represents "this process created this file just now."
1531+ - when aufs gets EACCES from branch fs's ->open(), then confirm the
1532+ registered TID and re-try open() with superuser's credential.
1533+
1534+Pros and cons for each approach.
1535+
1536+A.
1537+ - straightforward but highly depends upon VFS internal.
1538+ - the atomic behavaiour is kept.
1539+ - some of parameters such as nameidata are hard to reproduce for
1540+ branch fs.
1541+ - large overhead.
1542+B.
1543+ - easy to implement.
1544+ - the atomic behavaiour is lost.
1545+C.
1546+ - the atomic behavaiour is kept.
1547+ - dirty and tricky.
1548+ - VFS checks whether the file is created correctly after calling
1549+ ->create(), which means this approach doesn't work.
1550+D.
1551+ - easy to implement.
1552+ - the atomic behavaiour is lost.
1553+ - to open a file with superuser's credential and give it to a user
1554+ process is a bad idea, since the file object keeps the credential
1555+ in it. It may affect LSM or something. This approach doesn't work
1556+ either.
1557+
1558+The approach A is ideal, but it hard to implement. So here is a
1559+variation of A, which is to be implemented.
1560+
1561+A-1. Introduce aufs_atomic_open()
1562+ - calls branch fs ->atomic_open() if exists. otherwise calls
1563+ vfs_create() and finish_open().
1564+ - the demerit is that the several checks after branch fs
1565+ ->atomic_open() are lost. in the ordinary case, the checks are
1566+ done by VFS:do_last(), lookup_open() and atomic_open(). some can
1567+ be implemented in aufs, but not all I am afraid.
1568diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
1569--- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
1570+++ linux/Documentation/filesystems/aufs/design/03lookup.txt 2021-12-03 15:38:59.929980643 +0100
1571@@ -0,0 +1,113 @@
1572+
1573+# Copyright (C) 2005-2021 Junjiro R. Okajima
1574+#
1575+# This program is free software; you can redistribute it and/or modify
1576+# it under the terms of the GNU General Public License as published by
1577+# the Free Software Foundation; either version 2 of the License, or
1578+# (at your option) any later version.
1579+#
1580+# This program is distributed in the hope that it will be useful,
1581+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1582+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1583+# GNU General Public License for more details.
1584+#
1585+# You should have received a copy of the GNU General Public License
1586+# along with this program. If not, see <http://www.gnu.org/licenses/>.
1587+
1588+Lookup in a Branch
1589+----------------------------------------------------------------------
1590+Since aufs has a character of sub-VFS (see Introduction), it operates
1591+lookup for branches as VFS does. It may be a heavy work. But almost all
1592+lookup operation in aufs is the simplest case, ie. lookup only an entry
1593+directly connected to its parent. Digging down the directory hierarchy
1594+is unnecessary. VFS has a function lookup_one_len() for that use, and
1595+aufs calls it.
1596+
1597+When a branch is a remote filesystem, aufs basically relies upon its
1598+->d_revalidate(), also aufs forces the hardest revalidate tests for
1599+them.
1600+For d_revalidate, aufs implements three levels of revalidate tests. See
1601+"Revalidate Dentry and UDBA" in detail.
1602+
1603+
1604+Test Only the Highest One for the Directory Permission (dirperm1 option)
1605+----------------------------------------------------------------------
1606+Let's try case study.
1607+- aufs has two branches, upper readwrite and lower readonly.
1608+ /au = /rw + /ro
1609+- "dirA" exists under /ro, but /rw. and its mode is 0700.
1610+- user invoked "chmod a+rx /au/dirA"
1611+- the internal copy-up is activated and "/rw/dirA" is created and its
1612+ permission bits are set to world readable.
1613+- then "/au/dirA" becomes world readable?
1614+
1615+In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1616+or it may be a natively readonly filesystem. If aufs respects the lower
1617+branch, it should not respond readdir request from other users. But user
1618+allowed it by chmod. Should really aufs rejects showing the entries
1619+under /ro/dirA?
1620+
1621+To be honest, I don't have a good solution for this case. So aufs
1622+implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1623+users.
1624+When dirperm1 is specified, aufs checks only the highest one for the
1625+directory permission, and shows the entries. Otherwise, as usual, checks
1626+every dir existing on all branches and rejects the request.
1627+
1628+As a side effect, dirperm1 option improves the performance of aufs
1629+because the number of permission check is reduced when the number of
1630+branch is many.
1631+
1632+
1633+Revalidate Dentry and UDBA (User's Direct Branch Access)
1634+----------------------------------------------------------------------
1635+Generally VFS helpers re-validate a dentry as a part of lookup.
1636+0. digging down the directory hierarchy.
1637+1. lock the parent dir by its i_mutex.
1638+2. lookup the final (child) entry.
1639+3. revalidate it.
1640+4. call the actual operation (create, unlink, etc.)
1641+5. unlock the parent dir
1642+
1643+If the filesystem implements its ->d_revalidate() (step 3), then it is
1644+called. Actually aufs implements it and checks the dentry on a branch is
1645+still valid.
1646+But it is not enough. Because aufs has to release the lock for the
1647+parent dir on a branch at the end of ->lookup() (step 2) and
1648+->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1649+held by VFS.
1650+If the file on a branch is changed directly, eg. bypassing aufs, after
1651+aufs released the lock, then the subsequent operation may cause
1652+something unpleasant result.
1653+
1654+This situation is a result of VFS architecture, ->lookup() and
1655+->d_revalidate() is separated. But I never say it is wrong. It is a good
1656+design from VFS's point of view. It is just not suitable for sub-VFS
1657+character in aufs.
1658+
1659+Aufs supports such case by three level of revalidation which is
1660+selectable by user.
1661+1. Simple Revalidate
1662+ Addition to the native flow in VFS's, confirm the child-parent
1663+ relationship on the branch just after locking the parent dir on the
1664+ branch in the "actual operation" (step 4). When this validation
1665+ fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1666+ checks the validation of the dentry on branches.
1667+2. Monitor Changes Internally by Inotify/Fsnotify
1668+ Addition to above, in the "actual operation" (step 4) aufs re-lookup
1669+ the dentry on the branch, and returns EBUSY if it finds different
1670+ dentry.
1671+ Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1672+ during it is in cache. When the event is notified, aufs registers a
1673+ function to kernel 'events' thread by schedule_work(). And the
1674+ function sets some special status to the cached aufs dentry and inode
1675+ private data. If they are not cached, then aufs has nothing to
1676+ do. When the same file is accessed through aufs (step 0-3) later,
1677+ aufs will detect the status and refresh all necessary data.
1678+ In this mode, aufs has to ignore the event which is fired by aufs
1679+ itself.
1680+3. No Extra Validation
1681+ This is the simplest test and doesn't add any additional revalidation
1682+ test, and skip the revalidation in step 4. It is useful and improves
1683+ aufs performance when system surely hide the aufs branches from user,
1684+ by over-mounting something (or another method).
1685diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
1686--- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
1687+++ linux/Documentation/filesystems/aufs/design/04branch.txt 2021-12-03 15:38:59.929980643 +0100
1688@@ -0,0 +1,74 @@
1689+
1690+# Copyright (C) 2005-2021 Junjiro R. Okajima
1691+#
1692+# This program is free software; you can redistribute it and/or modify
1693+# it under the terms of the GNU General Public License as published by
1694+# the Free Software Foundation; either version 2 of the License, or
1695+# (at your option) any later version.
1696+#
1697+# This program is distributed in the hope that it will be useful,
1698+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1699+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1700+# GNU General Public License for more details.
1701+#
1702+# You should have received a copy of the GNU General Public License
1703+# along with this program. If not, see <http://www.gnu.org/licenses/>.
1704+
1705+Branch Manipulation
1706+
1707+Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1708+and changing its permission/attribute, there are a lot of works to do.
1709+
1710+
1711+Add a Branch
1712+----------------------------------------------------------------------
1713+o Confirm the adding dir exists outside of aufs, including loopback
1714+ mount, and its various attributes.
1715+o Initialize the xino file and whiteout bases if necessary.
1716+ See struct.txt.
1717+
1718+o Check the owner/group/mode of the directory
1719+ When the owner/group/mode of the adding directory differs from the
1720+ existing branch, aufs issues a warning because it may impose a
1721+ security risk.
1722+ For example, when a upper writable branch has a world writable empty
1723+ top directory, a malicious user can create any files on the writable
1724+ branch directly, like copy-up and modify manually. If something like
1725+ /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1726+ writable branch, and the writable branch is world-writable, then a
1727+ malicious guy may create /etc/passwd on the writable branch directly
1728+ and the infected file will be valid in aufs.
1729+ I am afraid it can be a security issue, but aufs can do nothing except
1730+ producing a warning.
1731+
1732+
1733+Delete a Branch
1734+----------------------------------------------------------------------
1735+o Confirm the deleting branch is not busy
1736+ To be general, there is one merit to adopt "remount" interface to
1737+ manipulate branches. It is to discard caches. At deleting a branch,
1738+ aufs checks the still cached (and connected) dentries and inodes. If
1739+ there are any, then they are all in-use. An inode without its
1740+ corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1741+
1742+ For the cached one, aufs checks whether the same named entry exists on
1743+ other branches.
1744+ If the cached one is a directory, because aufs provides a merged view
1745+ to users, as long as one dir is left on any branch aufs can show the
1746+ dir to users. In this case, the branch can be removed from aufs.
1747+ Otherwise aufs rejects deleting the branch.
1748+
1749+ If any file on the deleting branch is opened by aufs, then aufs
1750+ rejects deleting.
1751+
1752+
1753+Modify the Permission of a Branch
1754+----------------------------------------------------------------------
1755+o Re-initialize or remove the xino file and whiteout bases if necessary.
1756+ See struct.txt.
1757+
1758+o rw --> ro: Confirm the modifying branch is not busy
1759+ Aufs rejects the request if any of these conditions are true.
1760+ - a file on the branch is mmap-ed.
1761+ - a regular file on the branch is opened for write and there is no
1762+ same named entry on the upper branch.
1763diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
1764--- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt 1970-01-01 01:00:00.000000000 +0100
1765+++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt 2021-12-03 15:38:59.929980643 +0100
1766@@ -0,0 +1,64 @@
1767+
1768+# Copyright (C) 2005-2021 Junjiro R. Okajima
1769+#
1770+# This program is free software; you can redistribute it and/or modify
1771+# it under the terms of the GNU General Public License as published by
1772+# the Free Software Foundation; either version 2 of the License, or
1773+# (at your option) any later version.
1774+#
1775+# This program is distributed in the hope that it will be useful,
1776+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1777+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1778+# GNU General Public License for more details.
1779+#
1780+# You should have received a copy of the GNU General Public License
1781+# along with this program. If not, see <http://www.gnu.org/licenses/>.
1782+
1783+Policies to Select One among Multiple Writable Branches
1784+----------------------------------------------------------------------
1785+When the number of writable branch is more than one, aufs has to decide
1786+the target branch for file creation or copy-up. By default, the highest
1787+writable branch which has the parent (or ancestor) dir of the target
1788+file is chosen (top-down-parent policy).
1789+By user's request, aufs implements some other policies to select the
1790+writable branch, for file creation several policies, round-robin,
1791+most-free-space, and other policies. For copy-up, top-down-parent,
1792+bottom-up-parent, bottom-up and others.
1793+
1794+As expected, the round-robin policy selects the branch in circular. When
1795+you have two writable branches and creates 10 new files, 5 files will be
1796+created for each branch. mkdir(2) systemcall is an exception. When you
1797+create 10 new directories, all will be created on the same branch.
1798+And the most-free-space policy selects the one which has most free
1799+space among the writable branches. The amount of free space will be
1800+checked by aufs internally, and users can specify its time interval.
1801+
1802+The policies for copy-up is more simple,
1803+top-down-parent is equivalent to the same named on in create policy,
1804+bottom-up-parent selects the writable branch where the parent dir
1805+exists and the nearest upper one from the copyup-source,
1806+bottom-up selects the nearest upper writable branch from the
1807+copyup-source, regardless the existence of the parent dir.
1808+
1809+There are some rules or exceptions to apply these policies.
1810+- If there is a readonly branch above the policy-selected branch and
1811+ the parent dir is marked as opaque (a variation of whiteout), or the
1812+ target (creating) file is whiteout-ed on the upper readonly branch,
1813+ then the result of the policy is ignored and the target file will be
1814+ created on the nearest upper writable branch than the readonly branch.
1815+- If there is a writable branch above the policy-selected branch and
1816+ the parent dir is marked as opaque or the target file is whiteouted
1817+ on the branch, then the result of the policy is ignored and the target
1818+ file will be created on the highest one among the upper writable
1819+ branches who has diropq or whiteout. In case of whiteout, aufs removes
1820+ it as usual.
1821+- link(2) and rename(2) systemcalls are exceptions in every policy.
1822+ They try selecting the branch where the source exists as possible
1823+ since copyup a large file will take long time. If it can't be,
1824+ ie. the branch where the source exists is readonly, then they will
1825+ follow the copyup policy.
1826+- There is an exception for rename(2) when the target exists.
1827+ If the rename target exists, aufs compares the index of the branches
1828+ where the source and the target exists and selects the higher
1829+ one. If the selected branch is readonly, then aufs follows the
1830+ copyup policy.
1831diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot linux/Documentation/filesystems/aufs/design/06dirren.dot
1832--- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot 1970-01-01 01:00:00.000000000 +0100
1833+++ linux/Documentation/filesystems/aufs/design/06dirren.dot 2021-12-03 15:38:59.929980643 +0100
1834@@ -0,0 +1,31 @@
1835+
1836+// to view this graph, run dot(1) command in GRAPHVIZ.
1837+
1838+digraph G {
1839+node [shape=box];
1840+whinfo [label="detailed info file\n(lower_brid_root-hinum, h_inum, namelen, old name)"];
1841+
1842+node [shape=oval];
1843+
1844+aufs_rename -> whinfo [label="store/remove"];
1845+
1846+node [shape=oval];
1847+inode_list [label="h_inum list in branch\ncache"];
1848+
1849+node [shape=box];
1850+whinode [label="h_inum list file"];
1851+
1852+node [shape=oval];
1853+brmgmt [label="br_add/del/mod/umount"];
1854+
1855+brmgmt -> inode_list [label="create/remove"];
1856+brmgmt -> whinode [label="load/store"];
1857+
1858+inode_list -> whinode [style=dashed,dir=both];
1859+
1860+aufs_rename -> inode_list [label="add/del"];
1861+
1862+aufs_lookup -> inode_list [label="search"];
1863+
1864+aufs_lookup -> whinfo [label="load/remove"];
1865+}
1866diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt linux/Documentation/filesystems/aufs/design/06dirren.txt
1867--- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt 1970-01-01 01:00:00.000000000 +0100
1868+++ linux/Documentation/filesystems/aufs/design/06dirren.txt 2021-12-03 15:38:59.929980643 +0100
1869@@ -0,0 +1,102 @@
1870+
1871+# Copyright (C) 2017-2021 Junjiro R. Okajima
1872+#
1873+# This program is free software; you can redistribute it and/or modify
1874+# it under the terms of the GNU General Public License as published by
1875+# the Free Software Foundation; either version 2 of the License, or
1876+# (at your option) any later version.
1877+#
1878+# This program is distributed in the hope that it will be useful,
1879+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1880+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1881+# GNU General Public License for more details.
1882+#
1883+# You should have received a copy of the GNU General Public License
1884+# along with this program. If not, see <http://www.gnu.org/licenses/>.
1885+
1886+Special handling for renaming a directory (DIRREN)
1887+----------------------------------------------------------------------
1888+First, let's assume we have a simple usecase.
1889+
1890+- /u = /rw + /ro
1891+- /rw/dirA exists
1892+- /ro/dirA and /ro/dirA/file exist too
1893+- there is no dirB on both branches
1894+- a user issues rename("dirA", "dirB")
1895+
1896+Now, what should aufs behave against this rename(2)?
1897+There are a few possible cases.
1898+
1899+A. returns EROFS.
1900+ since dirA exists on a readonly branch which cannot be renamed.
1901+B. returns EXDEV.
1902+ it is possible to copy-up dirA (only the dir itself), but the child
1903+ entries ("file" in this case) should not be. it must be a bad
1904+ approach to copy-up recursively.
1905+C. returns a success.
1906+ even the branch /ro is readonly, aufs tries renaming it. Obviously it
1907+ is a violation of aufs' policy.
1908+D. construct an extra information which indicates that /ro/dirA should
1909+ be handled as the name of dirB.
1910+ overlayfs has a similar feature called REDIRECT.
1911+
1912+Until now, aufs implements the case B only which returns EXDEV, and
1913+expects the userspace application behaves like mv(1) which tries
1914+issueing rename(2) recursively.
1915+
1916+A new aufs feature called DIRREN is introduced which implements the case
1917+D. There are several "extra information" added.
1918+
1919+1. detailed info per renamed directory
1920+ path: /rw/dirB/$AUFS_WH_DR_INFO_PFX.<lower branch-id>
1921+2. the inode-number list of directories on a branch
1922+ path: /rw/dirB/$AUFS_WH_DR_BRHINO
1923+
1924+The filename of "detailed info per directory" represents the lower
1925+branch, and its format is
1926+- a type of the branch id
1927+ one of these.
1928+ + uuid (not implemented yet)
1929+ + fsid
1930+ + dev
1931+- the inode-number of the branch root dir
1932+
1933+And it contains these info in a single regular file.
1934+- magic number
1935+- branch's inode-number of the logically renamed dir
1936+- the name of the before-renamed dir
1937+
1938+The "detailed info per directory" file is created in aufs rename(2), and
1939+loaded in any lookup.
1940+The info is considered in lookup for the matching case only. Here
1941+"matching" means that the root of branch (in the info filename) is same
1942+to the current looking-up branch. After looking-up the before-renamed
1943+name, the inode-number is compared. And the matched dentry is used.
1944+
1945+The "inode-number list of directories" is a regular file which contains
1946+simply the inode-numbers on the branch. The file is created or updated
1947+in removing the branch, and loaded in adding the branch. Its lifetime is
1948+equal to the branch.
1949+The list is refered in lookup, and when the current target inode is
1950+found in the list, the aufs tries loading the "detailed info per
1951+directory" and get the changed and valid name of the dir.
1952+
1953+Theoretically these "extra informaiton" may be able to be put into XATTR
1954+in the dir inode. But aufs doesn't choose this way because
1955+1. XATTR may not be supported by the branch (or its configuration)
1956+2. XATTR may have its size limit.
1957+3. XATTR may be less easy to convert than a regular file, when the
1958+ format of the info is changed in the future.
1959+At the same time, I agree that the regular file approach is much slower
1960+than XATTR approach. So, in the future, aufs may take the XATTR or other
1961+better approach.
1962+
1963+This DIRREN feature is enabled by aufs configuration, and is activated
1964+by a new mount option.
1965+
1966+For the more complicated case, there is a work with UDBA option, which
1967+is to dected the direct access to the branches (by-passing aufs) and to
1968+maintain the cashes in aufs. Since a single cached aufs dentry may
1969+contains two names, before- and after-rename, the name comparision in
1970+UDBA handler may not work correctly. In this case, the behaviour will be
1971+equivalen to udba=reval case.
1972diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt linux/Documentation/filesystems/aufs/design/06fhsm.txt
1973--- /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt 1970-01-01 01:00:00.000000000 +0100
1974+++ linux/Documentation/filesystems/aufs/design/06fhsm.txt 2021-12-03 15:38:59.929980643 +0100
1975@@ -0,0 +1,120 @@
1976+
1977+# Copyright (C) 2011-2021 Junjiro R. Okajima
1978+#
1979+# This program is free software; you can redistribute it and/or modify
1980+# it under the terms of the GNU General Public License as published by
1981+# the Free Software Foundation; either version 2 of the License, or
1982+# (at your option) any later version.
1983+#
1984+# This program is distributed in the hope that it will be useful,
1985+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1986+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1987+# GNU General Public License for more details.
1988+#
1989+# You should have received a copy of the GNU General Public License
1990+# along with this program; if not, write to the Free Software
1991+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1992+
1993+
1994+File-based Hierarchical Storage Management (FHSM)
1995+----------------------------------------------------------------------
1996+Hierarchical Storage Management (or HSM) is a well-known feature in the
1997+storage world. Aufs provides this feature as file-based with multiple
1998+writable branches, based upon the principle of "Colder, the Lower".
1999+Here the word "colder" means that the less used files, and "lower" means
2000+that the position in the order of the stacked branches vertically.
2001+These multiple writable branches are prioritized, ie. the topmost one
2002+should be the fastest drive and be used heavily.
2003+
2004+o Characters in aufs FHSM story
2005+- aufs itself and a new branch attribute.
2006+- a new ioctl interface to move-down and to establish a connection with
2007+ the daemon ("move-down" is a converse of "copy-up").
2008+- userspace tool and daemon.
2009+
2010+The userspace daemon establishes a connection with aufs and waits for
2011+the notification. The notified information is very similar to struct
2012+statfs containing the number of consumed blocks and inodes.
2013+When the consumed blocks/inodes of a branch exceeds the user-specified
2014+upper watermark, the daemon activates its move-down process until the
2015+consumed blocks/inodes reaches the user-specified lower watermark.
2016+
2017+The actual move-down is done by aufs based upon the request from
2018+user-space since we need to maintain the inode number and the internal
2019+pointer arrays in aufs.
2020+
2021+Currently aufs FHSM handles the regular files only. Additionally they
2022+must not be hard-linked nor pseudo-linked.
2023+
2024+
2025+o Cowork of aufs and the user-space daemon
2026+ During the userspace daemon established the connection, aufs sends a
2027+ small notification to it whenever aufs writes something into the
2028+ writable branch. But it may cost high since aufs issues statfs(2)
2029+ internally. So user can specify a new option to cache the
2030+ info. Actually the notification is controlled by these factors.
2031+ + the specified cache time.
2032+ + classified as "force" by aufs internally.
2033+ Until the specified time expires, aufs doesn't send the info
2034+ except the forced cases. When aufs decide forcing, the info is always
2035+ notified to userspace.
2036+ For example, the number of free inodes is generally large enough and
2037+ the shortage of it happens rarely. So aufs doesn't force the
2038+ notification when creating a new file, directory and others. This is
2039+ the typical case which aufs doesn't force.
2040+ When aufs writes the actual filedata and the files consumes any of new
2041+ blocks, the aufs forces notifying.
2042+
2043+
2044+o Interfaces in aufs
2045+- New branch attribute.
2046+ + fhsm
2047+ Specifies that the branch is managed by FHSM feature. In other word,
2048+ participant in the FHSM.
2049+ When nofhsm is set to the branch, it will not be the source/target
2050+ branch of the move-down operation. This attribute is set
2051+ independently from coo and moo attributes, and if you want full
2052+ FHSM, you should specify them as well.
2053+- New mount option.
2054+ + fhsm_sec
2055+ Specifies a second to suppress many less important info to be
2056+ notified.
2057+- New ioctl.
2058+ + AUFS_CTL_FHSM_FD
2059+ create a new file descriptor which userspace can read the notification
2060+ (a subset of struct statfs) from aufs.
2061+- Module parameter 'brs'
2062+ It has to be set to 1. Otherwise the new mount option 'fhsm' will not
2063+ be set.
2064+- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
2065+ When there are two or more branches with fhsm attributes,
2066+ /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
2067+ terminates it. As a result of remounting and branch-manipulation, the
2068+ number of branches with fhsm attribute can be one. In this case,
2069+ /sbin/mount.aufs will terminate the user-space daemon.
2070+
2071+
2072+Finally the operation is done as these steps in kernel-space.
2073+- make sure that,
2074+ + no one else is using the file.
2075+ + the file is not hard-linked.
2076+ + the file is not pseudo-linked.
2077+ + the file is a regular file.
2078+ + the parent dir is not opaqued.
2079+- find the target writable branch.
2080+- make sure the file is not whiteout-ed by the upper (than the target)
2081+ branch.
2082+- make the parent dir on the target branch.
2083+- mutex lock the inode on the branch.
2084+- unlink the whiteout on the target branch (if exists).
2085+- lookup and create the whiteout-ed temporary name on the target branch.
2086+- copy the file as the whiteout-ed temporary name on the target branch.
2087+- rename the whiteout-ed temporary name to the original name.
2088+- unlink the file on the source branch.
2089+- maintain the internal pointer array and the external inode number
2090+ table (XINO).
2091+- maintain the timestamps and other attributes of the parent dir and the
2092+ file.
2093+
2094+And of course, in every step, an error may happen. So the operation
2095+should restore the original file state after an error happens.
2096diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
2097--- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt 1970-01-01 01:00:00.000000000 +0100
2098+++ linux/Documentation/filesystems/aufs/design/06mmap.txt 2021-12-03 15:38:59.929980643 +0100
2099@@ -0,0 +1,72 @@
2100+
2101+# Copyright (C) 2005-2021 Junjiro R. Okajima
2102+#
2103+# This program is free software; you can redistribute it and/or modify
2104+# it under the terms of the GNU General Public License as published by
2105+# the Free Software Foundation; either version 2 of the License, or
2106+# (at your option) any later version.
2107+#
2108+# This program is distributed in the hope that it will be useful,
2109+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2110+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2111+# GNU General Public License for more details.
2112+#
2113+# You should have received a copy of the GNU General Public License
2114+# along with this program. If not, see <http://www.gnu.org/licenses/>.
2115+
2116+mmap(2) -- File Memory Mapping
2117+----------------------------------------------------------------------
2118+In aufs, the file-mapped pages are handled by a branch fs directly, no
2119+interaction with aufs. It means aufs_mmap() calls the branch fs's
2120+->mmap().
2121+This approach is simple and good, but there is one problem.
2122+Under /proc, several entries show the mmapped files by its path (with
2123+device and inode number), and the printed path will be the path on the
2124+branch fs's instead of virtual aufs's.
2125+This is not a problem in most cases, but some utilities lsof(1) (and its
2126+user) may expect the path on aufs.
2127+
2128+To address this issue, aufs adds a new member called vm_prfile in struct
2129+vm_area_struct (and struct vm_region). The original vm_file points to
2130+the file on the branch fs in order to handle everything correctly as
2131+usual. The new vm_prfile points to a virtual file in aufs, and the
2132+show-functions in procfs refers to vm_prfile if it is set.
2133+Also we need to maintain several other places where touching vm_file
2134+such like
2135+- fork()/clone() copies vma and the reference count of vm_file is
2136+ incremented.
2137+- merging vma maintains the ref count too.
2138+
2139+This is not a good approach. It just fakes the printed path. But it
2140+leaves all behaviour around f_mapping unchanged. This is surely an
2141+advantage.
2142+Actually aufs had adopted another complicated approach which calls
2143+generic_file_mmap() and handles struct vm_operations_struct. In this
2144+approach, aufs met a hard problem and I could not solve it without
2145+switching the approach.
2146+
2147+There may be one more another approach which is
2148+- bind-mount the branch-root onto the aufs-root internally
2149+- grab the new vfsmount (ie. struct mount)
2150+- lazy-umount the branch-root internally
2151+- in open(2) the aufs-file, open the branch-file with the hidden
2152+ vfsmount (instead of the original branch's vfsmount)
2153+- ideally this "bind-mount and lazy-umount" should be done atomically,
2154+ but it may be possible from userspace by the mount helper.
2155+
2156+Adding the internal hidden vfsmount and using it in opening a file, the
2157+file path under /proc will be printed correctly. This approach looks
2158+smarter, but is not possible I am afraid.
2159+- aufs-root may be bind-mount later. when it happens, another hidden
2160+ vfsmount will be required.
2161+- it is hard to get the chance to bind-mount and lazy-umount
2162+ + in kernel-space, FS can have vfsmount in open(2) via
2163+ file->f_path, and aufs can know its vfsmount. But several locks are
2164+ already acquired, and if aufs tries to bind-mount and lazy-umount
2165+ here, then it may cause a deadlock.
2166+ + in user-space, bind-mount doesn't invoke the mount helper.
2167+- since /proc shows dev and ino, aufs has to give vma these info. it
2168+ means a new member vm_prinode will be necessary. this is essentially
2169+ equivalent to vm_prfile described above.
2170+
2171+I have to give up this "looks-smater" approach.
2172diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt linux/Documentation/filesystems/aufs/design/06xattr.txt
2173--- /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt 1970-01-01 01:00:00.000000000 +0100
2174+++ linux/Documentation/filesystems/aufs/design/06xattr.txt 2021-12-03 15:38:59.929980643 +0100
2175@@ -0,0 +1,96 @@
2176+
2177+# Copyright (C) 2014-2021 Junjiro R. Okajima
2178+#
2179+# This program is free software; you can redistribute it and/or modify
2180+# it under the terms of the GNU General Public License as published by
2181+# the Free Software Foundation; either version 2 of the License, or
2182+# (at your option) any later version.
2183+#
2184+# This program is distributed in the hope that it will be useful,
2185+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2186+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2187+# GNU General Public License for more details.
2188+#
2189+# You should have received a copy of the GNU General Public License
2190+# along with this program; if not, write to the Free Software
2191+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2192+
2193+
2194+Listing XATTR/EA and getting the value
2195+----------------------------------------------------------------------
2196+For the inode standard attributes (owner, group, timestamps, etc.), aufs
2197+shows the values from the topmost existing file. This behaviour is good
2198+for the non-dir entries since the bahaviour exactly matches the shown
2199+information. But for the directories, aufs considers all the same named
2200+entries on the lower branches. Which means, if one of the lower entry
2201+rejects readdir call, then aufs returns an error even if the topmost
2202+entry allows it. This behaviour is necessary to respect the branch fs's
2203+security, but can make users confused since the user-visible standard
2204+attributes don't match the behaviour.
2205+To address this issue, aufs has a mount option called dirperm1 which
2206+checks the permission for the topmost entry only, and ignores the lower
2207+entry's permission.
2208+
2209+A similar issue can happen around XATTR.
2210+getxattr(2) and listxattr(2) families behave as if dirperm1 option is
2211+always set. Otherwise these very unpleasant situation would happen.
2212+- listxattr(2) may return the duplicated entries.
2213+- users may not be able to remove or reset the XATTR forever,
2214+
2215+
2216+XATTR/EA support in the internal (copy,move)-(up,down)
2217+----------------------------------------------------------------------
2218+Generally the extended attributes of inode are categorized as these.
2219+- "security" for LSM and capability.
2220+- "system" for posix ACL, 'acl' mount option is required for the branch
2221+ fs generally.
2222+- "trusted" for userspace, CAP_SYS_ADMIN is required.
2223+- "user" for userspace, 'user_xattr' mount option is required for the
2224+ branch fs generally.
2225+
2226+Moreover there are some other categories. Aufs handles these rather
2227+unpopular categories as the ordinary ones, ie. there is no special
2228+condition nor exception.
2229+
2230+In copy-up, the support for XATTR on the dst branch may differ from the
2231+src branch. In this case, the copy-up operation will get an error and
2232+the original user operation which triggered the copy-up will fail. It
2233+can happen that even all copy-up will fail.
2234+When both of src and dst branches support XATTR and if an error occurs
2235+during copying XATTR, then the copy-up should fail obviously. That is a
2236+good reason and aufs should return an error to userspace. But when only
2237+the src branch support that XATTR, aufs should not return an error.
2238+For example, the src branch supports ACL but the dst branch doesn't
2239+because the dst branch may natively un-support it or temporary
2240+un-support it due to "noacl" mount option. Of course, the dst branch fs
2241+may NOT return an error even if the XATTR is not supported. It is
2242+totally up to the branch fs.
2243+
2244+Anyway when the aufs internal copy-up gets an error from the dst branch
2245+fs, then aufs tries removing the just copied entry and returns the error
2246+to the userspace. The worst case of this situation will be all copy-up
2247+will fail.
2248+
2249+For the copy-up operation, there two basic approaches.
2250+- copy the specified XATTR only (by category above), and return the
2251+ error unconditionally if it happens.
2252+- copy all XATTR, and ignore the error on the specified category only.
2253+
2254+In order to support XATTR and to implement the correct behaviour, aufs
2255+chooses the latter approach and introduces some new branch attributes,
2256+"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
2257+They correspond to the XATTR namespaces (see above). Additionally, to be
2258+convenient, "icex" is also provided which means all "icex*" attributes
2259+are set (here the word "icex" stands for "ignore copy-error on XATTR").
2260+
2261+The meaning of these attributes is to ignore the error from setting
2262+XATTR on that branch.
2263+Note that aufs tries copying all XATTR unconditionally, and ignores the
2264+error from the dst branch according to the specified attributes.
2265+
2266+Some XATTR may have its default value. The default value may come from
2267+the parent dir or the environment. If the default value is set at the
2268+file creating-time, it will be overwritten by copy-up.
2269+Some contradiction may happen I am afraid.
2270+Do we need another attribute to stop copying XATTR? I am unsure. For
2271+now, aufs implements the branch attributes to ignore the error.
2272diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
2273--- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
2274+++ linux/Documentation/filesystems/aufs/design/07export.txt 2021-12-03 15:38:59.929980643 +0100
2275@@ -0,0 +1,58 @@
2276+
2277+# Copyright (C) 2005-2021 Junjiro R. Okajima
2278+#
2279+# This program is free software; you can redistribute it and/or modify
2280+# it under the terms of the GNU General Public License as published by
2281+# the Free Software Foundation; either version 2 of the License, or
2282+# (at your option) any later version.
2283+#
2284+# This program is distributed in the hope that it will be useful,
2285+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2286+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2287+# GNU General Public License for more details.
2288+#
2289+# You should have received a copy of the GNU General Public License
2290+# along with this program. If not, see <http://www.gnu.org/licenses/>.
2291+
2292+Export Aufs via NFS
2293+----------------------------------------------------------------------
2294+Here is an approach.
2295+- like xino/xib, add a new file 'xigen' which stores aufs inode
2296+ generation.
2297+- iget_locked(): initialize aufs inode generation for a new inode, and
2298+ store it in xigen file.
2299+- destroy_inode(): increment aufs inode generation and store it in xigen
2300+ file. it is necessary even if it is not unlinked, because any data of
2301+ inode may be changed by UDBA.
2302+- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2303+ build file handle by
2304+ + branch id (4 bytes)
2305+ + superblock generation (4 bytes)
2306+ + inode number (4 or 8 bytes)
2307+ + parent dir inode number (4 or 8 bytes)
2308+ + inode generation (4 bytes))
2309+ + return value of exportfs_encode_fh() for the parent on a branch (4
2310+ bytes)
2311+ + file handle for a branch (by exportfs_encode_fh())
2312+- fh_to_dentry():
2313+ + find the index of a branch from its id in handle, and check it is
2314+ still exist in aufs.
2315+ + 1st level: get the inode number from handle and search it in cache.
2316+ + 2nd level: if not found in cache, get the parent inode number from
2317+ the handle and search it in cache. and then open the found parent
2318+ dir, find the matching inode number by vfs_readdir() and get its
2319+ name, and call lookup_one_len() for the target dentry.
2320+ + 3rd level: if the parent dir is not cached, call
2321+ exportfs_decode_fh() for a branch and get the parent on a branch,
2322+ build a pathname of it, convert it a pathname in aufs, call
2323+ path_lookup(). now aufs gets a parent dir dentry, then handle it as
2324+ the 2nd level.
2325+ + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2326+ for every branch, but not itself. to get this, (currently) aufs
2327+ searches in current->nsproxy->mnt_ns list. it may not be a good
2328+ idea, but I didn't get other approach.
2329+ + test the generation of the gotten inode.
2330+- every inode operation: they may get EBUSY due to UDBA. in this case,
2331+ convert it into ESTALE for NFSD.
2332+- readdir(): call lockdep_on/off() because filldir in NFSD calls
2333+ lookup_one_len(), vfs_getattr(), encode_fh() and others.
2334diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
2335--- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt 1970-01-01 01:00:00.000000000 +0100
2336+++ linux/Documentation/filesystems/aufs/design/08shwh.txt 2021-12-03 15:38:59.929980643 +0100
2337@@ -0,0 +1,52 @@
2338+
2339+# Copyright (C) 2005-2021 Junjiro R. Okajima
2340+#
2341+# This program is free software; you can redistribute it and/or modify
2342+# it under the terms of the GNU General Public License as published by
2343+# the Free Software Foundation; either version 2 of the License, or
2344+# (at your option) any later version.
2345+#
2346+# This program is distributed in the hope that it will be useful,
2347+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2348+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2349+# GNU General Public License for more details.
2350+#
2351+# You should have received a copy of the GNU General Public License
2352+# along with this program. If not, see <http://www.gnu.org/licenses/>.
2353+
2354+Show Whiteout Mode (shwh)
2355+----------------------------------------------------------------------
2356+Generally aufs hides the name of whiteouts. But in some cases, to show
2357+them is very useful for users. For instance, creating a new middle layer
2358+(branch) by merging existing layers.
2359+
2360+(borrowing aufs1 HOW-TO from a user, Michael Towers)
2361+When you have three branches,
2362+- Bottom: 'system', squashfs (underlying base system), read-only
2363+- Middle: 'mods', squashfs, read-only
2364+- Top: 'overlay', ram (tmpfs), read-write
2365+
2366+The top layer is loaded at boot time and saved at shutdown, to preserve
2367+the changes made to the system during the session.
2368+When larger changes have been made, or smaller changes have accumulated,
2369+the size of the saved top layer data grows. At this point, it would be
2370+nice to be able to merge the two overlay branches ('mods' and 'overlay')
2371+and rewrite the 'mods' squashfs, clearing the top layer and thus
2372+restoring save and load speed.
2373+
2374+This merging is simplified by the use of another aufs mount, of just the
2375+two overlay branches using the 'shwh' option.
2376+# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2377+ aufs /livesys/merge_union
2378+
2379+A merged view of these two branches is then available at
2380+/livesys/merge_union, and the new feature is that the whiteouts are
2381+visible!
2382+Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2383+writing to all branches. Also the default mode for all branches is 'ro'.
2384+It is now possible to save the combined contents of the two overlay
2385+branches to a new squashfs, e.g.:
2386+# mksquashfs /livesys/merge_union /path/to/newmods.squash
2387+
2388+This new squashfs archive can be stored on the boot device and the
2389+initramfs will use it to replace the old one at the next boot.
2390diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
2391--- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt 1970-01-01 01:00:00.000000000 +0100
2392+++ linux/Documentation/filesystems/aufs/design/10dynop.txt 2021-12-03 15:38:59.929980643 +0100
2393@@ -0,0 +1,47 @@
2394+
2395+# Copyright (C) 2010-2021 Junjiro R. Okajima
2396+#
2397+# This program is free software; you can redistribute it and/or modify
2398+# it under the terms of the GNU General Public License as published by
2399+# the Free Software Foundation; either version 2 of the License, or
2400+# (at your option) any later version.
2401+#
2402+# This program is distributed in the hope that it will be useful,
2403+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2404+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2405+# GNU General Public License for more details.
2406+#
2407+# You should have received a copy of the GNU General Public License
2408+# along with this program. If not, see <http://www.gnu.org/licenses/>.
2409+
2410+Dynamically customizable FS operations
2411+----------------------------------------------------------------------
2412+Generally FS operations (struct inode_operations, struct
2413+address_space_operations, struct file_operations, etc.) are defined as
2414+"static const", but it never means that FS have only one set of
2415+operation. Some FS have multiple sets of them. For instance, ext2 has
2416+three sets, one for XIP, for NOBH, and for normal.
2417+Since aufs overrides and redirects these operations, sometimes aufs has
2418+to change its behaviour according to the branch FS type. More importantly
2419+VFS acts differently if a function (member in the struct) is set or
2420+not. It means aufs should have several sets of operations and select one
2421+among them according to the branch FS definition.
2422+
2423+In order to solve this problem and not to affect the behaviour of VFS,
2424+aufs defines these operations dynamically. For instance, aufs defines
2425+dummy direct_IO function for struct address_space_operations, but it may
2426+not be set to the address_space_operations actually. When the branch FS
2427+doesn't have it, aufs doesn't set it to its address_space_operations
2428+while the function definition itself is still alive. So the behaviour
2429+itself will not change, and it will return an error when direct_IO is
2430+not set.
2431+
2432+The lifetime of these dynamically generated operation object is
2433+maintained by aufs branch object. When the branch is removed from aufs,
2434+the reference counter of the object is decremented. When it reaches
2435+zero, the dynamically generated operation object will be freed.
2436+
2437+This approach is designed to support AIO (io_submit), Direct I/O and
2438+XIP (DAX) mainly.
2439+Currently this approach is applied to address_space_operations for
2440+regular files only.
2441diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
2442--- /usr/share/empty/Documentation/filesystems/aufs/README 1970-01-01 01:00:00.000000000 +0100
2443+++ linux/Documentation/filesystems/aufs/README 2021-12-03 15:40:58.233313963 +0100
2444@@ -0,0 +1,396 @@
2445+
2446+Aufs5 -- advanced multi layered unification filesystem version 5.x
2447+http://aufs.sf.net
2448+Junjiro R. Okajima
2449+
2450+
2451+0. Introduction
2452+----------------------------------------
2453+In the early days, aufs was entirely re-designed and re-implemented
2454+Unionfs Version 1.x series. Adding many original ideas, approaches,
2455+improvements and implementations, it became totally different from
2456+Unionfs while keeping the basic features.
2457+Later, Unionfs Version 2.x series began taking some of the same
2458+approaches to aufs1's.
2459+Unionfs was being developed by Professor Erez Zadok at Stony Brook
2460+University and his team.
2461+
2462+Aufs5 supports linux-v5.0 and later, If you want older kernel version
2463+support,
2464+- for linux-v4.x series, try aufs4-linux.git or aufs4-standalone.git
2465+- for linux-v3.x series, try aufs3-linux.git or aufs3-standalone.git
2466+- for linux-v2.6.16 and later, try aufs2-2.6.git, aufs2-standalone.git
2467+ or aufs1 from CVS on SourceForge.
2468+
2469+Note: it becomes clear that "Aufs was rejected. Let's give it up."
2470+ According to Christoph Hellwig, linux rejects all union-type
2471+ filesystems but UnionMount.
2472+<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
2473+
2474+PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
2475+ UnionMount, and he pointed out an issue around a directory mutex
2476+ lock and aufs addressed it. But it is still unsure whether aufs will
2477+ be merged (or any other union solution).
2478+<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
2479+
2480+
2481+1. Features
2482+----------------------------------------
2483+- unite several directories into a single virtual filesystem. The member
2484+ directory is called as a branch.
2485+- you can specify the permission flags to the branch, which are 'readonly',
2486+ 'readwrite' and 'whiteout-able.'
2487+- by upper writable branch, internal copyup and whiteout, files/dirs on
2488+ readonly branch are modifiable logically.
2489+- dynamic branch manipulation, add, del.
2490+- etc...
2491+
2492+Also there are many enhancements in aufs, such as:
2493+- test only the highest one for the directory permission (dirperm1)
2494+- copyup on open (coo=)
2495+- 'move' policy for copy-up between two writable branches, after
2496+ checking free space.
2497+- xattr, acl
2498+- readdir(3) in userspace.
2499+- keep inode number by external inode number table
2500+- keep the timestamps of file/dir in internal copyup operation
2501+- seekable directory, supporting NFS readdir.
2502+- whiteout is hardlinked in order to reduce the consumption of inodes
2503+ on branch
2504+- do not copyup, nor create a whiteout when it is unnecessary
2505+- revert a single systemcall when an error occurs in aufs
2506+- remount interface instead of ioctl
2507+- maintain /etc/mtab by an external command, /sbin/mount.aufs.
2508+- loopback mounted filesystem as a branch
2509+- kernel thread for removing the dir who has a plenty of whiteouts
2510+- support copyup sparse file (a file which has a 'hole' in it)
2511+- default permission flags for branches
2512+- selectable permission flags for ro branch, whether whiteout can
2513+ exist or not
2514+- export via NFS.
2515+- support <sysfs>/fs/aufs and <debugfs>/aufs.
2516+- support multiple writable branches, some policies to select one
2517+ among multiple writable branches.
2518+- a new semantics for link(2) and rename(2) to support multiple
2519+ writable branches.
2520+- no glibc changes are required.
2521+- pseudo hardlink (hardlink over branches)
2522+- allow a direct access manually to a file on branch, e.g. bypassing aufs.
2523+ including NFS or remote filesystem branch.
2524+- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
2525+- and more...
2526+
2527+Currently these features are dropped temporary from aufs5.
2528+See design/08plan.txt in detail.
2529+- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
2530+ (robr)
2531+- statistics of aufs thread (/sys/fs/aufs/stat)
2532+
2533+Features or just an idea in the future (see also design/*.txt),
2534+- reorder the branch index without del/re-add.
2535+- permanent xino files for NFSD
2536+- an option for refreshing the opened files after add/del branches
2537+- light version, without branch manipulation. (unnecessary?)
2538+- copyup in userspace
2539+- inotify in userspace
2540+- readv/writev
2541+
2542+
2543+2. Download
2544+----------------------------------------
2545+There are three GIT trees for aufs5, aufs5-linux.git,
2546+aufs5-standalone.git, and aufs-util.git. Note that there is no "5" in
2547+"aufs-util.git."
2548+While the aufs-util is always necessary, you need either of aufs5-linux
2549+or aufs5-standalone.
2550+
2551+The aufs5-linux tree includes the whole linux mainline GIT tree,
2552+git://git.kernel.org/.../torvalds/linux.git.
2553+And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
2554+build aufs5 as an external kernel module.
2555+Several extra patches are not included in this tree. Only
2556+aufs5-standalone tree contains them. They are described in the later
2557+section "Configuration and Compilation."
2558+
2559+On the other hand, the aufs5-standalone tree has only aufs source files
2560+and necessary patches, and you can select CONFIG_AUFS_FS=m.
2561+But you need to apply all aufs patches manually.
2562+
2563+You will find GIT branches whose name is in form of "aufs5.x" where "x"
2564+represents the linux kernel version, "linux-5.x". For instance,
2565+"aufs5.0" is for linux-5.0. For latest "linux-5.x-rcN", use
2566+"aufs5.x-rcN" branch.
2567+
2568+o aufs5-linux tree
2569+$ git clone --reference /your/linux/git/tree \
2570+ git://github.com/sfjro/aufs5-linux.git aufs5-linux.git
2571+- if you don't have linux GIT tree, then remove "--reference ..."
2572+$ cd aufs5-linux.git
2573+$ git checkout origin/aufs5.0
2574+
2575+Or You may want to directly git-pull aufs into your linux GIT tree, and
2576+leave the patch-work to GIT.
2577+$ cd /your/linux/git/tree
2578+$ git remote add aufs5 git://github.com/sfjro/aufs5-linux.git
2579+$ git fetch aufs5
2580+$ git checkout -b my5.0 v5.0
2581+$ (add your local change...)
2582+$ git pull aufs5 aufs5.0
2583+- now you have v5.0 + your_changes + aufs5.0 in you my5.0 branch.
2584+- you may need to solve some conflicts between your_changes and
2585+ aufs5.0. in this case, git-rerere is recommended so that you can
2586+ solve the similar conflicts automatically when you upgrade to 5.1 or
2587+ later in the future.
2588+
2589+o aufs5-standalone tree
2590+$ git clone git://github.com/sfjro/aufs5-standalone.git aufs5-standalone.git
2591+$ cd aufs5-standalone.git
2592+$ git checkout origin/aufs5.0
2593+
2594+o aufs-util tree
2595+$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
2596+- note that the public aufs-util.git is on SourceForge instead of
2597+ GitHUB.
2598+$ cd aufs-util.git
2599+$ git checkout origin/aufs5.0
2600+
2601+Note: The 5.x-rcN branch is to be used with `rc' kernel versions ONLY.
2602+The minor version number, 'x' in '5.x', of aufs may not always
2603+follow the minor version number of the kernel.
2604+Because changes in the kernel that cause the use of a new
2605+minor version number do not always require changes to aufs-util.
2606+
2607+Since aufs-util has its own minor version number, you may not be
2608+able to find a GIT branch in aufs-util for your kernel's
2609+exact minor version number.
2610+In this case, you should git-checkout the branch for the
2611+nearest lower number.
2612+
2613+For (an unreleased) example:
2614+If you are using "linux-5.10" and the "aufs5.10" branch
2615+does not exist in aufs-util repository, then "aufs5.9", "aufs5.8"
2616+or something numerically smaller is the branch for your kernel.
2617+
2618+Also you can view all branches by
2619+ $ git branch -a
2620+
2621+
2622+3. Configuration and Compilation
2623+----------------------------------------
2624+Make sure you have git-checkout'ed the correct branch.
2625+
2626+For aufs5-linux tree,
2627+- enable CONFIG_AUFS_FS.
2628+- set other aufs configurations if necessary.
2629+- for aufs5.13 and later
2630+ Because aufs is not only an ordinary filesystem (callee of VFS), but
2631+ also a caller of VFS functions for branch filesystems, subclassing of
2632+ the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
2633+ feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
2634+ need to customize some LOCKDEP numbers. Here are what I use on my
2635+ test environment.
2636+ CONFIG_LOCKDEP_BITS=21
2637+ CONFIG_LOCKDEP_CHAINS_BITS=21
2638+ CONFIG_LOCKDEP_STACK_TRACE_BITS=24
2639+
2640+For aufs5-standalone tree,
2641+There are several ways to build.
2642+
2643+1.
2644+- apply ./aufs5-kbuild.patch to your kernel source files.
2645+- apply ./aufs5-base.patch too.
2646+- apply ./aufs5-mmap.patch too.
2647+- apply ./aufs5-standalone.patch too, if you have a plan to set
2648+ CONFIG_AUFS_FS=m. otherwise you don't need ./aufs5-standalone.patch.
2649+- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2650+ kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
2651+- enable CONFIG_AUFS_FS, you can select either
2652+ =m or =y.
2653+- and build your kernel as usual.
2654+- install the built kernel.
2655+- install the header files too by "make headers_install" to the
2656+ directory where you specify. By default, it is $PWD/usr.
2657+ "make help" shows a brief note for headers_install.
2658+- and reboot your system.
2659+
2660+2.
2661+- module only (CONFIG_AUFS_FS=m).
2662+- apply ./aufs5-base.patch to your kernel source files.
2663+- apply ./aufs5-mmap.patch too.
2664+- apply ./aufs5-standalone.patch too.
2665+- build your kernel, don't forget "make headers_install", and reboot.
2666+- edit ./config.mk and set other aufs configurations if necessary.
2667+ Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
2668+ every aufs configurations.
2669+- build the module by simple "make".
2670+- you can specify ${KDIR} make variable which points to your kernel
2671+ source tree.
2672+- install the files
2673+ + run "make install" to install the aufs module, or copy the built
2674+ $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2675+ + run "make install_headers" (instead of headers_install) to install
2676+ the modified aufs header file (you can specify DESTDIR which is
2677+ available in aufs standalone version's Makefile only), or copy
2678+ $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2679+ you like manually. By default, the target directory is $PWD/usr.
2680+- no need to apply aufs5-kbuild.patch, nor copying source files to your
2681+ kernel source tree.
2682+
2683+Note: The header file aufs_type.h is necessary to build aufs-util
2684+ as well as "make headers_install" in the kernel source tree.
2685+ headers_install is subject to be forgotten, but it is essentially
2686+ necessary, not only for building aufs-util.
2687+ You may not meet problems without headers_install in some older
2688+ version though.
2689+
2690+And then,
2691+- read README in aufs-util, build and install it
2692+- note that your distribution may contain an obsoleted version of
2693+ aufs_type.h in /usr/include/linux or something. When you build aufs
2694+ utilities, make sure that your compiler refers the correct aufs header
2695+ file which is built by "make headers_install."
2696+- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2697+ then run "make install_ulib" too. And refer to the aufs manual in
2698+ detail.
2699+
2700+There several other patches in aufs5-standalone.git. They are all
2701+optional. When you meet some problems, they will help you.
2702+- aufs5-loopback.patch
2703+ Supports a nested loopback mount in a branch-fs. This patch is
2704+ unnecessary until aufs produces a message like "you may want to try
2705+ another patch for loopback file".
2706+- vfs-ino.patch
2707+ Modifies a system global kernel internal function get_next_ino() in
2708+ order to stop assigning 0 for an inode-number. Not directly related to
2709+ aufs, but recommended generally.
2710+- tmpfs-idr.patch
2711+ Keeps the tmpfs inode number as the lowest value. Effective to reduce
2712+ the size of aufs XINO files for tmpfs branch. Also it prevents the
2713+ duplication of inode number, which is important for backup tools and
2714+ other utilities. When you find aufs XINO files for tmpfs branch
2715+ growing too much, try this patch.
2716+
2717+
2718+4. Usage
2719+----------------------------------------
2720+At first, make sure aufs-util are installed, and please read the aufs
2721+manual, aufs.5 in aufs-util.git tree.
2722+$ man -l aufs.5
2723+
2724+And then,
2725+$ mkdir /tmp/rw /tmp/aufs
2726+# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2727+
2728+Here is another example. The result is equivalent.
2729+# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2730+ Or
2731+# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2732+# mount -o remount,append:${HOME} /tmp/aufs
2733+
2734+Then, you can see whole tree of your home dir through /tmp/aufs. If
2735+you modify a file under /tmp/aufs, the one on your home directory is
2736+not affected, instead the same named file will be newly created under
2737+/tmp/rw. And all of your modification to a file will be applied to
2738+the one under /tmp/rw. This is called the file based Copy on Write
2739+(COW) method.
2740+Aufs mount options are described in aufs.5.
2741+If you run chroot or something and make your aufs as a root directory,
2742+then you need to customize the shutdown script. See the aufs manual in
2743+detail.
2744+
2745+Additionally, there are some sample usages of aufs which are a
2746+diskless system with network booting, and LiveCD over NFS.
2747+See sample dir in CVS tree on SourceForge.
2748+
2749+
2750+5. Contact
2751+----------------------------------------
2752+When you have any problems or strange behaviour in aufs, please let me
2753+know with:
2754+- /proc/mounts (instead of the output of mount(8))
2755+- /sys/module/aufs/*
2756+- /sys/fs/aufs/* (if you have them)
2757+- /debug/aufs/* (if you have them)
2758+- linux kernel version
2759+ if your kernel is not plain, for example modified by distributor,
2760+ the url where i can download its source is necessary too.
2761+- aufs version which was printed at loading the module or booting the
2762+ system, instead of the date you downloaded.
2763+- configuration (define/undefine CONFIG_AUFS_xxx)
2764+- kernel configuration or /proc/config.gz (if you have it)
2765+- LSM (linux security module, if you are using)
2766+- behaviour which you think to be incorrect
2767+- actual operation, reproducible one is better
2768+- mailto: aufs-users at lists.sourceforge.net
2769+
2770+Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
2771+and Feature Requests) on SourceForge. Please join and write to
2772+aufs-users ML.
2773+
2774+
2775+6. Acknowledgements
2776+----------------------------------------
2777+Thanks to everyone who have tried and are using aufs, whoever
2778+have reported a bug or any feedback.
2779+
2780+Especially donators:
2781+Tomas Matejicek(slax.org) made a donation (much more than once).
2782+ Since Apr 2010, Tomas M (the author of Slax and Linux Live
2783+ scripts) is making "doubling" donations.
2784+ Unfortunately I cannot list all of the donators, but I really
2785+ appreciate.
2786+ It ends Aug 2010, but the ordinary donation URL is still available.
2787+ <http://sourceforge.net/donate/index.php?group_id=167503>
2788+Dai Itasaka made a donation (2007/8).
2789+Chuck Smith made a donation (2008/4, 10 and 12).
2790+Henk Schoneveld made a donation (2008/9).
2791+Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
2792+Francois Dupoux made a donation (2008/11).
2793+Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
2794+ aufs2 GIT tree (2009/2).
2795+William Grant made a donation (2009/3).
2796+Patrick Lane made a donation (2009/4).
2797+The Mail Archive (mail-archive.com) made donations (2009/5).
2798+Nippy Networks (Ed Wildgoose) made a donation (2009/7).
2799+New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
2800+Pavel Pronskiy made a donation (2011/2).
2801+Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
2802+ Networks (Ed Wildgoose) made a donation for hardware (2011/3).
2803+Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
2804+11).
2805+Sam Liddicott made a donation (2011/9).
2806+Era Scarecrow made a donation (2013/4).
2807+Bor Ratajc made a donation (2013/4).
2808+Alessandro Gorreta made a donation (2013/4).
2809+POIRETTE Marc made a donation (2013/4).
2810+Alessandro Gorreta made a donation (2013/4).
2811+lauri kasvandik made a donation (2013/5).
2812+"pemasu from Finland" made a donation (2013/7).
2813+The Parted Magic Project made a donation (2013/9 and 11).
2814+Pavel Barta made a donation (2013/10).
2815+Nikolay Pertsev made a donation (2014/5).
2816+James B made a donation (2014/7 and 2015/7).
2817+Stefano Di Biase made a donation (2014/8).
2818+Daniel Epellei made a donation (2015/1).
2819+OmegaPhil made a donation (2016/1, 2018/4).
2820+Tomasz Szewczyk made a donation (2016/4).
2821+James Burry made a donation (2016/12).
2822+Carsten Rose made a donation (2018/9).
2823+Porteus Kiosk made a donation (2018/10).
2824+
2825+Thank you very much.
2826+Donations are always, including future donations, very important and
2827+helpful for me to keep on developing aufs.
2828+
2829+
2830+7.
2831+----------------------------------------
2832+If you are an experienced user, no explanation is needed. Aufs is
2833+just a linux filesystem.
2834+
2835+
2836+Enjoy!
2837+
2838+# Local variables: ;
2839+# mode: text;
2840+# End: ;
2841diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
2842--- /usr/share/empty/fs/aufs/aufs.h 1970-01-01 01:00:00.000000000 +0100
2843+++ linux/fs/aufs/aufs.h 2021-12-03 15:38:59.929980643 +0100
2844@@ -0,0 +1,62 @@
2845+/* SPDX-License-Identifier: GPL-2.0 */
2846+/*
2847+ * Copyright (C) 2005-2021 Junjiro R. Okajima
2848+ *
2849+ * This program, aufs is free software; you can redistribute it and/or modify
2850+ * it under the terms of the GNU General Public License as published by
2851+ * the Free Software Foundation; either version 2 of the License, or
2852+ * (at your option) any later version.
2853+ *
2854+ * This program is distributed in the hope that it will be useful,
2855+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
2856+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2857+ * GNU General Public License for more details.
2858+ *
2859+ * You should have received a copy of the GNU General Public License
2860+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
2861+ */
2862+
2863+/*
2864+ * all header files
2865+ */
2866+
2867+#ifndef __AUFS_H__
2868+#define __AUFS_H__
2869+
2870+#ifdef __KERNEL__
2871+
2872+#define AuStub(type, name, body, ...) \
2873+ static inline type name(__VA_ARGS__) { body; }
2874+
2875+#define AuStubVoid(name, ...) \
2876+ AuStub(void, name, , __VA_ARGS__)
2877+#define AuStubInt0(name, ...) \
2878+ AuStub(int, name, return 0, __VA_ARGS__)
2879+
2880+#include "debug.h"
2881+
2882+#include "branch.h"
2883+#include "cpup.h"
2884+#include "dcsub.h"
2885+#include "dbgaufs.h"
2886+#include "dentry.h"
2887+#include "dir.h"
2888+#include "dirren.h"
2889+#include "dynop.h"
2890+#include "file.h"
2891+#include "fstype.h"
2892+#include "hbl.h"
2893+#include "inode.h"
2894+#include "lcnt.h"
2895+#include "loop.h"
2896+#include "module.h"
2897+#include "opts.h"
2898+#include "rwsem.h"
2899+#include "super.h"
2900+#include "sysaufs.h"
2901+#include "vfsub.h"
2902+#include "whout.h"
2903+#include "wkq.h"
2904+
2905+#endif /* __KERNEL__ */
2906+#endif /* __AUFS_H__ */
2907diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
2908--- /usr/share/empty/fs/aufs/branch.c 1970-01-01 01:00:00.000000000 +0100
2909+++ linux/fs/aufs/branch.c 2021-12-03 15:38:59.933313976 +0100
2910@@ -0,0 +1,1427 @@
2911+// SPDX-License-Identifier: GPL-2.0
2912+/*
2913+ * Copyright (C) 2005-2021 Junjiro R. Okajima
2914+ *
2915+ * This program, aufs is free software; you can redistribute it and/or modify
2916+ * it under the terms of the GNU General Public License as published by
2917+ * the Free Software Foundation; either version 2 of the License, or
2918+ * (at your option) any later version.
2919+ *
2920+ * This program is distributed in the hope that it will be useful,
2921+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
2922+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2923+ * GNU General Public License for more details.
2924+ *
2925+ * You should have received a copy of the GNU General Public License
2926+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
2927+ */
2928+
2929+/*
2930+ * branch management
2931+ */
2932+
2933+#include <linux/compat.h>
2934+#include <linux/statfs.h>
2935+#include "aufs.h"
2936+
2937+/*
2938+ * free a single branch
2939+ */
2940+static void au_br_do_free(struct au_branch *br)
2941+{
2942+ int i;
2943+ struct au_wbr *wbr;
2944+ struct au_dykey **key;
2945+
2946+ au_hnotify_fin_br(br);
2947+ /* always, regardless the mount option */
2948+ au_dr_hino_free(&br->br_dirren);
2949+ au_xino_put(br);
2950+
2951+ AuLCntZero(au_lcnt_read(&br->br_nfiles, /*do_rev*/0));
2952+ au_lcnt_fin(&br->br_nfiles, /*do_sync*/0);
2953+ AuLCntZero(au_lcnt_read(&br->br_count, /*do_rev*/0));
2954+ au_lcnt_fin(&br->br_count, /*do_sync*/0);
2955+
2956+ wbr = br->br_wbr;
2957+ if (wbr) {
2958+ for (i = 0; i < AuBrWh_Last; i++)
2959+ dput(wbr->wbr_wh[i]);
2960+ AuDebugOn(atomic_read(&wbr->wbr_wh_running));
2961+ AuRwDestroy(&wbr->wbr_wh_rwsem);
2962+ }
2963+
2964+ if (br->br_fhsm) {
2965+ au_br_fhsm_fin(br->br_fhsm);
2966+ au_kfree_try_rcu(br->br_fhsm);
2967+ }
2968+
2969+ key = br->br_dykey;
2970+ for (i = 0; i < AuBrDynOp; i++, key++)
2971+ if (*key)
2972+ au_dy_put(*key);
2973+ else
2974+ break;
2975+
2976+ /* recursive lock, s_umount of branch's */
2977+ /* synchronize_rcu(); */ /* why? */
2978+ lockdep_off();
2979+ path_put(&br->br_path);
2980+ lockdep_on();
2981+ au_kfree_rcu(wbr);
2982+ au_lcnt_wait_for_fin(&br->br_nfiles);
2983+ au_lcnt_wait_for_fin(&br->br_count);
2984+ /* I don't know why, but percpu_refcount requires this */
2985+ /* synchronize_rcu(); */
2986+ au_kfree_rcu(br);
2987+}
2988+
2989+/*
2990+ * frees all branches
2991+ */
2992+void au_br_free(struct au_sbinfo *sbinfo)
2993+{
2994+ aufs_bindex_t bmax;
2995+ struct au_branch **br;
2996+
2997+ AuRwMustWriteLock(&sbinfo->si_rwsem);
2998+
2999+ bmax = sbinfo->si_bbot + 1;
3000+ br = sbinfo->si_branch;
3001+ while (bmax--)
3002+ au_br_do_free(*br++);
3003+}
3004+
3005+/*
3006+ * find the index of a branch which is specified by @br_id.
3007+ */
3008+int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
3009+{
3010+ aufs_bindex_t bindex, bbot;
3011+
3012+ bbot = au_sbbot(sb);
3013+ for (bindex = 0; bindex <= bbot; bindex++)
3014+ if (au_sbr_id(sb, bindex) == br_id)
3015+ return bindex;
3016+ return -1;
3017+}
3018+
3019+/* ---------------------------------------------------------------------- */
3020+
3021+/*
3022+ * add a branch
3023+ */
3024+
3025+static int test_overlap(struct super_block *sb, struct dentry *h_adding,
3026+ struct dentry *h_root)
3027+{
3028+ if (unlikely(h_adding == h_root
3029+ || au_test_loopback_overlap(sb, h_adding)))
3030+ return 1;
3031+ if (h_adding->d_sb != h_root->d_sb)
3032+ return 0;
3033+ return au_test_subdir(h_adding, h_root)
3034+ || au_test_subdir(h_root, h_adding);
3035+}
3036+
3037+/*
3038+ * returns a newly allocated branch. @new_nbranch is a number of branches
3039+ * after adding a branch.
3040+ */
3041+static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
3042+ int perm)
3043+{
3044+ struct au_branch *add_branch;
3045+ struct dentry *root;
3046+ struct inode *inode;
3047+ int err;
3048+
3049+ err = -ENOMEM;
3050+ add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
3051+ if (unlikely(!add_branch))
3052+ goto out;
3053+ add_branch->br_xino = au_xino_alloc(/*nfile*/1);
3054+ if (unlikely(!add_branch->br_xino))
3055+ goto out_br;
3056+ err = au_hnotify_init_br(add_branch, perm);
3057+ if (unlikely(err))
3058+ goto out_xino;
3059+
3060+ if (au_br_writable(perm)) {
3061+ /* may be freed separately at changing the branch permission */
3062+ add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
3063+ GFP_NOFS);
3064+ if (unlikely(!add_branch->br_wbr))
3065+ goto out_hnotify;
3066+ }
3067+
3068+ if (au_br_fhsm(perm)) {
3069+ err = au_fhsm_br_alloc(add_branch);
3070+ if (unlikely(err))
3071+ goto out_wbr;
3072+ }
3073+
3074+ root = sb->s_root;
3075+ err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
3076+ if (!err)
3077+ err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
3078+ if (!err) {
3079+ inode = d_inode(root);
3080+ err = au_hinode_realloc(au_ii(inode), new_nbranch,
3081+ /*may_shrink*/0);
3082+ }
3083+ if (!err)
3084+ return add_branch; /* success */
3085+
3086+out_wbr:
3087+ au_kfree_rcu(add_branch->br_wbr);
3088+out_hnotify:
3089+ au_hnotify_fin_br(add_branch);
3090+out_xino:
3091+ au_xino_put(add_branch);
3092+out_br:
3093+ au_kfree_rcu(add_branch);
3094+out:
3095+ return ERR_PTR(err);
3096+}
3097+
3098+/*
3099+ * test if the branch permission is legal or not.
3100+ */
3101+static int test_br(struct inode *inode, int brperm, char *path)
3102+{
3103+ int err;
3104+
3105+ err = (au_br_writable(brperm) && IS_RDONLY(inode));
3106+ if (!err)
3107+ goto out;
3108+
3109+ err = -EINVAL;
3110+ pr_err("write permission for readonly mount or inode, %s\n", path);
3111+
3112+out:
3113+ return err;
3114+}
3115+
3116+/*
3117+ * returns:
3118+ * 0: success, the caller will add it
3119+ * plus: success, it is already unified, the caller should ignore it
3120+ * minus: error
3121+ */
3122+static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3123+{
3124+ int err;
3125+ aufs_bindex_t bbot, bindex;
3126+ struct dentry *root, *h_dentry;
3127+ struct inode *inode, *h_inode;
3128+
3129+ root = sb->s_root;
3130+ bbot = au_sbbot(sb);
3131+ if (unlikely(bbot >= 0
3132+ && au_find_dbindex(root, add->path.dentry) >= 0)) {
3133+ err = 1;
3134+ if (!remount) {
3135+ err = -EINVAL;
3136+ pr_err("%s duplicated\n", add->pathname);
3137+ }
3138+ goto out;
3139+ }
3140+
3141+ err = -ENOSPC; /* -E2BIG; */
3142+ if (unlikely(AUFS_BRANCH_MAX <= add->bindex
3143+ || AUFS_BRANCH_MAX - 1 <= bbot)) {
3144+ pr_err("number of branches exceeded %s\n", add->pathname);
3145+ goto out;
3146+ }
3147+
3148+ err = -EDOM;
3149+ if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
3150+ pr_err("bad index %d\n", add->bindex);
3151+ goto out;
3152+ }
3153+
3154+ inode = d_inode(add->path.dentry);
3155+ err = -ENOENT;
3156+ if (unlikely(!inode->i_nlink)) {
3157+ pr_err("no existence %s\n", add->pathname);
3158+ goto out;
3159+ }
3160+
3161+ err = -EINVAL;
3162+ if (unlikely(inode->i_sb == sb)) {
3163+ pr_err("%s must be outside\n", add->pathname);
3164+ goto out;
3165+ }
3166+
3167+ if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
3168+ pr_err("unsupported filesystem, %s (%s)\n",
3169+ add->pathname, au_sbtype(inode->i_sb));
3170+ goto out;
3171+ }
3172+
3173+ if (unlikely(inode->i_sb->s_stack_depth)) {
3174+ pr_err("already stacked, %s (%s)\n",
3175+ add->pathname, au_sbtype(inode->i_sb));
3176+ goto out;
3177+ }
3178+
3179+ err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
3180+ if (unlikely(err))
3181+ goto out;
3182+
3183+ if (bbot < 0)
3184+ return 0; /* success */
3185+
3186+ err = -EINVAL;
3187+ for (bindex = 0; bindex <= bbot; bindex++)
3188+ if (unlikely(test_overlap(sb, add->path.dentry,
3189+ au_h_dptr(root, bindex)))) {
3190+ pr_err("%s is overlapped\n", add->pathname);
3191+ goto out;
3192+ }
3193+
3194+ err = 0;
3195+ if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
3196+ h_dentry = au_h_dptr(root, 0);
3197+ h_inode = d_inode(h_dentry);
3198+ if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
3199+ || !uid_eq(h_inode->i_uid, inode->i_uid)
3200+ || !gid_eq(h_inode->i_gid, inode->i_gid))
3201+ pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3202+ add->pathname,
3203+ i_uid_read(inode), i_gid_read(inode),
3204+ (inode->i_mode & S_IALLUGO),
3205+ i_uid_read(h_inode), i_gid_read(h_inode),
3206+ (h_inode->i_mode & S_IALLUGO));
3207+ }
3208+
3209+out:
3210+ return err;
3211+}
3212+
3213+/*
3214+ * initialize or clean the whiteouts for an adding branch
3215+ */
3216+static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
3217+ int new_perm)
3218+{
3219+ int err, old_perm;
3220+ aufs_bindex_t bindex;
3221+ struct inode *h_inode;
3222+ struct au_wbr *wbr;
3223+ struct au_hinode *hdir;
3224+ struct dentry *h_dentry;
3225+
3226+ err = vfsub_mnt_want_write(au_br_mnt(br));
3227+ if (unlikely(err))
3228+ goto out;
3229+
3230+ wbr = br->br_wbr;
3231+ old_perm = br->br_perm;
3232+ br->br_perm = new_perm;
3233+ hdir = NULL;
3234+ h_inode = NULL;
3235+ bindex = au_br_index(sb, br->br_id);
3236+ if (0 <= bindex) {
3237+ hdir = au_hi(d_inode(sb->s_root), bindex);
3238+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
3239+ } else {
3240+ h_dentry = au_br_dentry(br);
3241+ h_inode = d_inode(h_dentry);
3242+ inode_lock_nested(h_inode, AuLsc_I_PARENT);
3243+ }
3244+ if (!wbr)
3245+ err = au_wh_init(br, sb);
3246+ else {
3247+ wbr_wh_write_lock(wbr);
3248+ err = au_wh_init(br, sb);
3249+ wbr_wh_write_unlock(wbr);
3250+ }
3251+ if (hdir)
3252+ au_hn_inode_unlock(hdir);
3253+ else
3254+ inode_unlock(h_inode);
3255+ vfsub_mnt_drop_write(au_br_mnt(br));
3256+ br->br_perm = old_perm;
3257+
3258+ if (!err && wbr && !au_br_writable(new_perm)) {
3259+ au_kfree_rcu(wbr);
3260+ br->br_wbr = NULL;
3261+ }
3262+
3263+out:
3264+ return err;
3265+}
3266+
3267+static int au_wbr_init(struct au_branch *br, struct super_block *sb,
3268+ int perm)
3269+{
3270+ int err;
3271+ struct kstatfs kst;
3272+ struct au_wbr *wbr;
3273+
3274+ wbr = br->br_wbr;
3275+ au_rw_init(&wbr->wbr_wh_rwsem);
3276+ atomic_set(&wbr->wbr_wh_running, 0);
3277+
3278+ /*
3279+ * a limit for rmdir/rename a dir
3280+ * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
3281+ */
3282+ err = vfs_statfs(&br->br_path, &kst);
3283+ if (unlikely(err))
3284+ goto out;
3285+ err = -EINVAL;
3286+ if (kst.f_namelen >= NAME_MAX)
3287+ err = au_br_init_wh(sb, br, perm);
3288+ else
3289+ pr_err("%pd(%s), unsupported namelen %ld\n",
3290+ au_br_dentry(br),
3291+ au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
3292+
3293+out:
3294+ return err;
3295+}
3296+
3297+/* initialize a new branch */
3298+static int au_br_init(struct au_branch *br, struct super_block *sb,
3299+ struct au_opt_add *add)
3300+{
3301+ int err;
3302+ struct au_branch *brbase;
3303+ struct file *xf;
3304+ struct inode *h_inode;
3305+
3306+ err = 0;
3307+ br->br_perm = add->perm;
3308+ br->br_path = add->path; /* set first, path_get() later */
3309+ spin_lock_init(&br->br_dykey_lock);
3310+ au_lcnt_init(&br->br_nfiles, /*release*/NULL);
3311+ au_lcnt_init(&br->br_count, /*release*/NULL);
3312+ br->br_id = au_new_br_id(sb);
3313+ AuDebugOn(br->br_id < 0);
3314+
3315+ /* always, regardless the given option */
3316+ err = au_dr_br_init(sb, br, &add->path);
3317+ if (unlikely(err))
3318+ goto out_err;
3319+
3320+ if (au_br_writable(add->perm)) {
3321+ err = au_wbr_init(br, sb, add->perm);
3322+ if (unlikely(err))
3323+ goto out_err;
3324+ }
3325+
3326+ if (au_opt_test(au_mntflags(sb), XINO)) {
3327+ brbase = au_sbr(sb, 0);
3328+ xf = au_xino_file(brbase->br_xino, /*idx*/-1);
3329+ AuDebugOn(!xf);
3330+ h_inode = d_inode(add->path.dentry);
3331+ err = au_xino_init_br(sb, br, h_inode->i_ino, &xf->f_path);
3332+ if (unlikely(err)) {
3333+ AuDebugOn(au_xino_file(br->br_xino, /*idx*/-1));
3334+ goto out_err;
3335+ }
3336+ }
3337+
3338+ sysaufs_br_init(br);
3339+ path_get(&br->br_path);
3340+ goto out; /* success */
3341+
3342+out_err:
3343+ memset(&br->br_path, 0, sizeof(br->br_path));
3344+out:
3345+ return err;
3346+}
3347+
3348+static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
3349+ struct au_branch *br, aufs_bindex_t bbot,
3350+ aufs_bindex_t amount)
3351+{
3352+ struct au_branch **brp;
3353+
3354+ AuRwMustWriteLock(&sbinfo->si_rwsem);
3355+
3356+ brp = sbinfo->si_branch + bindex;
3357+ memmove(brp + 1, brp, sizeof(*brp) * amount);
3358+ *brp = br;
3359+ sbinfo->si_bbot++;
3360+ if (unlikely(bbot < 0))
3361+ sbinfo->si_bbot = 0;
3362+}
3363+
3364+static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
3365+ aufs_bindex_t bbot, aufs_bindex_t amount)
3366+{
3367+ struct au_hdentry *hdp;
3368+
3369+ AuRwMustWriteLock(&dinfo->di_rwsem);
3370+
3371+ hdp = au_hdentry(dinfo, bindex);
3372+ memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3373+ au_h_dentry_init(hdp);
3374+ dinfo->di_bbot++;
3375+ if (unlikely(bbot < 0))
3376+ dinfo->di_btop = 0;
3377+}
3378+
3379+static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
3380+ aufs_bindex_t bbot, aufs_bindex_t amount)
3381+{
3382+ struct au_hinode *hip;
3383+
3384+ AuRwMustWriteLock(&iinfo->ii_rwsem);
3385+
3386+ hip = au_hinode(iinfo, bindex);
3387+ memmove(hip + 1, hip, sizeof(*hip) * amount);
3388+ au_hinode_init(hip);
3389+ iinfo->ii_bbot++;
3390+ if (unlikely(bbot < 0))
3391+ iinfo->ii_btop = 0;
3392+}
3393+
3394+static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3395+ aufs_bindex_t bindex)
3396+{
3397+ struct dentry *root, *h_dentry;
3398+ struct inode *root_inode, *h_inode;
3399+ aufs_bindex_t bbot, amount;
3400+
3401+ root = sb->s_root;
3402+ root_inode = d_inode(root);
3403+ bbot = au_sbbot(sb);
3404+ amount = bbot + 1 - bindex;
3405+ h_dentry = au_br_dentry(br);
3406+ au_sbilist_lock();
3407+ au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
3408+ au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
3409+ au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
3410+ au_set_h_dptr(root, bindex, dget(h_dentry));
3411+ h_inode = d_inode(h_dentry);
3412+ au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
3413+ au_sbilist_unlock();
3414+}
3415+
3416+int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3417+{
3418+ int err;
3419+ aufs_bindex_t bbot, add_bindex;
3420+ struct dentry *root, *h_dentry;
3421+ struct inode *root_inode;
3422+ struct au_branch *add_branch;
3423+
3424+ root = sb->s_root;
3425+ root_inode = d_inode(root);
3426+ IMustLock(root_inode);
3427+ IiMustWriteLock(root_inode);
3428+ err = test_add(sb, add, remount);
3429+ if (unlikely(err < 0))
3430+ goto out;
3431+ if (err) {
3432+ err = 0;
3433+ goto out; /* success */
3434+ }
3435+
3436+ bbot = au_sbbot(sb);
3437+ add_branch = au_br_alloc(sb, bbot + 2, add->perm);
3438+ err = PTR_ERR(add_branch);
3439+ if (IS_ERR(add_branch))
3440+ goto out;
3441+
3442+ err = au_br_init(add_branch, sb, add);
3443+ if (unlikely(err)) {
3444+ au_br_do_free(add_branch);
3445+ goto out;
3446+ }
3447+
3448+ add_bindex = add->bindex;
3449+ sysaufs_brs_del(sb, add_bindex); /* remove successors */
3450+ au_br_do_add(sb, add_branch, add_bindex);
3451+ sysaufs_brs_add(sb, add_bindex); /* append successors */
3452+ dbgaufs_brs_add(sb, add_bindex, /*topdown*/0); /* rename successors */
3453+
3454+ h_dentry = add->path.dentry;
3455+ if (!add_bindex) {
3456+ au_cpup_attr_all(root_inode, /*force*/1);
3457+ sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3458+ } else
3459+ au_add_nlink(root_inode, d_inode(h_dentry));
3460+
3461+out:
3462+ return err;
3463+}
3464+
3465+/* ---------------------------------------------------------------------- */
3466+
3467+static unsigned long long au_farray_cb(struct super_block *sb, void *a,
3468+ unsigned long long max __maybe_unused,
3469+ void *arg)
3470+{
3471+ unsigned long long n;
3472+ struct file **p, *f;
3473+ struct hlist_bl_head *files;
3474+ struct hlist_bl_node *pos;
3475+ struct au_finfo *finfo;
3476+
3477+ n = 0;
3478+ p = a;
3479+ files = &au_sbi(sb)->si_files;
3480+ hlist_bl_lock(files);
3481+ hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
3482+ f = finfo->fi_file;
3483+ if (file_count(f)
3484+ && !special_file(file_inode(f)->i_mode)) {
3485+ get_file(f);
3486+ *p++ = f;
3487+ n++;
3488+ AuDebugOn(n > max);
3489+ }
3490+ }
3491+ hlist_bl_unlock(files);
3492+
3493+ return n;
3494+}
3495+
3496+static struct file **au_farray_alloc(struct super_block *sb,
3497+ unsigned long long *max)
3498+{
3499+ struct au_sbinfo *sbi;
3500+
3501+ sbi = au_sbi(sb);
3502+ *max = au_lcnt_read(&sbi->si_nfiles, /*do_rev*/1);
3503+ return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
3504+}
3505+
3506+static void au_farray_free(struct file **a, unsigned long long max)
3507+{
3508+ unsigned long long ull;
3509+
3510+ for (ull = 0; ull < max; ull++)
3511+ if (a[ull])
3512+ fput(a[ull]);
3513+ kvfree(a);
3514+}
3515+
3516+/* ---------------------------------------------------------------------- */
3517+
3518+/*
3519+ * delete a branch
3520+ */
3521+
3522+/* to show the line number, do not make it inlined function */
3523+#define AuVerbose(do_info, fmt, ...) do { \
3524+ if (do_info) \
3525+ pr_info(fmt, ##__VA_ARGS__); \
3526+} while (0)
3527+
3528+static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
3529+ aufs_bindex_t bbot)
3530+{
3531+ return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
3532+}
3533+
3534+static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
3535+ aufs_bindex_t bbot)
3536+{
3537+ return au_test_ibusy(d_inode(dentry), btop, bbot);
3538+}
3539+
3540+/*
3541+ * test if the branch is deletable or not.
3542+ */
3543+static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
3544+ unsigned int sigen, const unsigned int verbose)
3545+{
3546+ int err, i, j, ndentry;
3547+ aufs_bindex_t btop, bbot;
3548+ struct au_dcsub_pages dpages;
3549+ struct au_dpage *dpage;
3550+ struct dentry *d;
3551+
3552+ err = au_dpages_init(&dpages, GFP_NOFS);
3553+ if (unlikely(err))
3554+ goto out;
3555+ err = au_dcsub_pages(&dpages, root, NULL, NULL);
3556+ if (unlikely(err))
3557+ goto out_dpages;
3558+
3559+ for (i = 0; !err && i < dpages.ndpage; i++) {
3560+ dpage = dpages.dpages + i;
3561+ ndentry = dpage->ndentry;
3562+ for (j = 0; !err && j < ndentry; j++) {
3563+ d = dpage->dentries[j];
3564+ AuDebugOn(au_dcount(d) <= 0);
3565+ if (!au_digen_test(d, sigen)) {
3566+ di_read_lock_child(d, AuLock_IR);
3567+ if (unlikely(au_dbrange_test(d))) {
3568+ di_read_unlock(d, AuLock_IR);
3569+ continue;
3570+ }
3571+ } else {
3572+ di_write_lock_child(d);
3573+ if (unlikely(au_dbrange_test(d))) {
3574+ di_write_unlock(d);
3575+ continue;
3576+ }
3577+ err = au_reval_dpath(d, sigen);
3578+ if (!err)
3579+ di_downgrade_lock(d, AuLock_IR);
3580+ else {
3581+ di_write_unlock(d);
3582+ break;
3583+ }
3584+ }
3585+
3586+ /* AuDbgDentry(d); */
3587+ btop = au_dbtop(d);
3588+ bbot = au_dbbot(d);
3589+ if (btop <= bindex
3590+ && bindex <= bbot
3591+ && au_h_dptr(d, bindex)
3592+ && au_test_dbusy(d, btop, bbot)) {
3593+ err = -EBUSY;
3594+ AuVerbose(verbose, "busy %pd\n", d);
3595+ AuDbgDentry(d);
3596+ }
3597+ di_read_unlock(d, AuLock_IR);
3598+ }
3599+ }
3600+
3601+out_dpages:
3602+ au_dpages_free(&dpages);
3603+out:
3604+ return err;
3605+}
3606+
3607+static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
3608+ unsigned int sigen, const unsigned int verbose)
3609+{
3610+ int err;
3611+ unsigned long long max, ull;
3612+ struct inode *i, **array;
3613+ aufs_bindex_t btop, bbot;
3614+
3615+ array = au_iarray_alloc(sb, &max);
3616+ err = PTR_ERR(array);
3617+ if (IS_ERR(array))
3618+ goto out;
3619+
3620+ err = 0;
3621+ AuDbg("b%d\n", bindex);
3622+ for (ull = 0; !err && ull < max; ull++) {
3623+ i = array[ull];
3624+ if (unlikely(!i))
3625+ break;
3626+ if (i->i_ino == AUFS_ROOT_INO)
3627+ continue;
3628+
3629+ /* AuDbgInode(i); */
3630+ if (au_iigen(i, NULL) == sigen)
3631+ ii_read_lock_child(i);
3632+ else {
3633+ ii_write_lock_child(i);
3634+ err = au_refresh_hinode_self(i);
3635+ au_iigen_dec(i);
3636+ if (!err)
3637+ ii_downgrade_lock(i);
3638+ else {
3639+ ii_write_unlock(i);
3640+ break;
3641+ }
3642+ }
3643+
3644+ btop = au_ibtop(i);
3645+ bbot = au_ibbot(i);
3646+ if (btop <= bindex
3647+ && bindex <= bbot
3648+ && au_h_iptr(i, bindex)
3649+ && au_test_ibusy(i, btop, bbot)) {
3650+ err = -EBUSY;
3651+ AuVerbose(verbose, "busy i%lu\n", i->i_ino);
3652+ AuDbgInode(i);
3653+ }
3654+ ii_read_unlock(i);
3655+ }
3656+ au_iarray_free(array, max);
3657+
3658+out:
3659+ return err;
3660+}
3661+
3662+static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3663+ const unsigned int verbose)
3664+{
3665+ int err;
3666+ unsigned int sigen;
3667+
3668+ sigen = au_sigen(root->d_sb);
3669+ DiMustNoWaiters(root);
3670+ IiMustNoWaiters(d_inode(root));
3671+ di_write_unlock(root);
3672+ err = test_dentry_busy(root, bindex, sigen, verbose);
3673+ if (!err)
3674+ err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
3675+ di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3676+
3677+ return err;
3678+}
3679+
3680+static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3681+ struct file **to_free, int *idx)
3682+{
3683+ int err;
3684+ unsigned char matched, root;
3685+ aufs_bindex_t bindex, bbot;
3686+ struct au_fidir *fidir;
3687+ struct au_hfile *hfile;
3688+
3689+ err = 0;
3690+ root = IS_ROOT(file->f_path.dentry);
3691+ if (root) {
3692+ get_file(file);
3693+ to_free[*idx] = file;
3694+ (*idx)++;
3695+ goto out;
3696+ }
3697+
3698+ matched = 0;
3699+ fidir = au_fi(file)->fi_hdir;
3700+ AuDebugOn(!fidir);
3701+ bbot = au_fbbot_dir(file);
3702+ for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
3703+ hfile = fidir->fd_hfile + bindex;
3704+ if (!hfile->hf_file)
3705+ continue;
3706+
3707+ if (hfile->hf_br->br_id == br_id) {
3708+ matched = 1;
3709+ break;
3710+ }
3711+ }
3712+ if (matched)
3713+ err = -EBUSY;
3714+
3715+out:
3716+ return err;
3717+}
3718+
3719+static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3720+ struct file **to_free, int opened)
3721+{
3722+ int err, idx;
3723+ unsigned long long ull, max;
3724+ aufs_bindex_t btop;
3725+ struct file *file, **array;
3726+ struct dentry *root;
3727+ struct au_hfile *hfile;
3728+
3729+ array = au_farray_alloc(sb, &max);
3730+ err = PTR_ERR(array);
3731+ if (IS_ERR(array))
3732+ goto out;
3733+
3734+ err = 0;
3735+ idx = 0;
3736+ root = sb->s_root;
3737+ di_write_unlock(root);
3738+ for (ull = 0; ull < max; ull++) {
3739+ file = array[ull];
3740+ if (unlikely(!file))
3741+ break;
3742+
3743+ /* AuDbg("%pD\n", file); */
3744+ fi_read_lock(file);
3745+ btop = au_fbtop(file);
3746+ if (!d_is_dir(file->f_path.dentry)) {
3747+ hfile = &au_fi(file)->fi_htop;
3748+ if (hfile->hf_br->br_id == br_id)
3749+ err = -EBUSY;
3750+ } else
3751+ err = test_dir_busy(file, br_id, to_free, &idx);
3752+ fi_read_unlock(file);
3753+ if (unlikely(err))
3754+ break;
3755+ }
3756+ di_write_lock_child(root);
3757+ au_farray_free(array, max);
3758+ AuDebugOn(idx > opened);
3759+
3760+out:
3761+ return err;
3762+}
3763+
3764+static void br_del_file(struct file **to_free, unsigned long long opened,
3765+ aufs_bindex_t br_id)
3766+{
3767+ unsigned long long ull;
3768+ aufs_bindex_t bindex, btop, bbot, bfound;
3769+ struct file *file;
3770+ struct au_fidir *fidir;
3771+ struct au_hfile *hfile;
3772+
3773+ for (ull = 0; ull < opened; ull++) {
3774+ file = to_free[ull];
3775+ if (unlikely(!file))
3776+ break;
3777+
3778+ /* AuDbg("%pD\n", file); */
3779+ AuDebugOn(!d_is_dir(file->f_path.dentry));
3780+ bfound = -1;
3781+ fidir = au_fi(file)->fi_hdir;
3782+ AuDebugOn(!fidir);
3783+ fi_write_lock(file);
3784+ btop = au_fbtop(file);
3785+ bbot = au_fbbot_dir(file);
3786+ for (bindex = btop; bindex <= bbot; bindex++) {
3787+ hfile = fidir->fd_hfile + bindex;
3788+ if (!hfile->hf_file)
3789+ continue;
3790+
3791+ if (hfile->hf_br->br_id == br_id) {
3792+ bfound = bindex;
3793+ break;
3794+ }
3795+ }
3796+ AuDebugOn(bfound < 0);
3797+ au_set_h_fptr(file, bfound, NULL);
3798+ if (bfound == btop) {
3799+ for (btop++; btop <= bbot; btop++)
3800+ if (au_hf_dir(file, btop)) {
3801+ au_set_fbtop(file, btop);
3802+ break;
3803+ }
3804+ }
3805+ fi_write_unlock(file);
3806+ }
3807+}
3808+
3809+static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
3810+ const aufs_bindex_t bindex,
3811+ const aufs_bindex_t bbot)
3812+{
3813+ struct au_branch **brp, **p;
3814+
3815+ AuRwMustWriteLock(&sbinfo->si_rwsem);
3816+
3817+ brp = sbinfo->si_branch + bindex;
3818+ if (bindex < bbot)
3819+ memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
3820+ sbinfo->si_branch[0 + bbot] = NULL;
3821+ sbinfo->si_bbot--;
3822+
3823+ p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
3824+ /*may_shrink*/1);
3825+ if (p)
3826+ sbinfo->si_branch = p;
3827+ /* harmless error */
3828+}
3829+
3830+static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
3831+ const aufs_bindex_t bbot)
3832+{
3833+ struct au_hdentry *hdp, *p;
3834+
3835+ AuRwMustWriteLock(&dinfo->di_rwsem);
3836+
3837+ hdp = au_hdentry(dinfo, bindex);
3838+ if (bindex < bbot)
3839+ memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
3840+ /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
3841+ dinfo->di_bbot--;
3842+
3843+ p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
3844+ /*may_shrink*/1);
3845+ if (p)
3846+ dinfo->di_hdentry = p;
3847+ /* harmless error */
3848+}
3849+
3850+static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
3851+ const aufs_bindex_t bbot)
3852+{
3853+ struct au_hinode *hip, *p;
3854+
3855+ AuRwMustWriteLock(&iinfo->ii_rwsem);
3856+
3857+ hip = au_hinode(iinfo, bindex);
3858+ if (bindex < bbot)
3859+ memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
3860+ /* au_hinode_init(au_hinode(iinfo, bbot)); */
3861+ iinfo->ii_bbot--;
3862+
3863+ p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
3864+ /*may_shrink*/1);
3865+ if (p)
3866+ iinfo->ii_hinode = p;
3867+ /* harmless error */
3868+}
3869+
3870+static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
3871+ struct au_branch *br)
3872+{
3873+ aufs_bindex_t bbot;
3874+ struct au_sbinfo *sbinfo;
3875+ struct dentry *root, *h_root;
3876+ struct inode *inode, *h_inode;
3877+ struct au_hinode *hinode;
3878+
3879+ SiMustWriteLock(sb);
3880+
3881+ root = sb->s_root;
3882+ inode = d_inode(root);
3883+ sbinfo = au_sbi(sb);
3884+ bbot = sbinfo->si_bbot;
3885+
3886+ h_root = au_h_dptr(root, bindex);
3887+ hinode = au_hi(inode, bindex);
3888+ h_inode = au_igrab(hinode->hi_inode);
3889+ au_hiput(hinode);
3890+
3891+ au_sbilist_lock();
3892+ au_br_do_del_brp(sbinfo, bindex, bbot);
3893+ au_br_do_del_hdp(au_di(root), bindex, bbot);
3894+ au_br_do_del_hip(au_ii(inode), bindex, bbot);
3895+ au_sbilist_unlock();
3896+
3897+ /* ignore an error */
3898+ au_dr_br_fin(sb, br); /* always, regardless the mount option */
3899+
3900+ dput(h_root);
3901+ iput(h_inode);
3902+ au_br_do_free(br);
3903+}
3904+
3905+static unsigned long long empty_cb(struct super_block *sb, void *array,
3906+ unsigned long long max, void *arg)
3907+{
3908+ return max;
3909+}
3910+
3911+int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
3912+{
3913+ int err, rerr, i;
3914+ unsigned long long opened;
3915+ unsigned int mnt_flags;
3916+ aufs_bindex_t bindex, bbot, br_id;
3917+ unsigned char do_wh, verbose;
3918+ struct au_branch *br;
3919+ struct au_wbr *wbr;
3920+ struct dentry *root;
3921+ struct file **to_free;
3922+
3923+ err = 0;
3924+ opened = 0;
3925+ to_free = NULL;
3926+ root = sb->s_root;
3927+ bindex = au_find_dbindex(root, del->h_path.dentry);
3928+ if (bindex < 0) {
3929+ if (remount)
3930+ goto out; /* success */
3931+ err = -ENOENT;
3932+ pr_err("%s no such branch\n", del->pathname);
3933+ goto out;
3934+ }
3935+ AuDbg("bindex b%d\n", bindex);
3936+
3937+ err = -EBUSY;
3938+ mnt_flags = au_mntflags(sb);
3939+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
3940+ bbot = au_sbbot(sb);
3941+ if (unlikely(!bbot)) {
3942+ AuVerbose(verbose, "no more branches left\n");
3943+ goto out;
3944+ }
3945+
3946+ br = au_sbr(sb, bindex);
3947+ AuDebugOn(!path_equal(&br->br_path, &del->h_path));
3948+ if (unlikely(au_lcnt_read(&br->br_count, /*do_rev*/1))) {
3949+ AuVerbose(verbose, "br %pd2 is busy now\n", del->h_path.dentry);
3950+ goto out;
3951+ }
3952+
3953+ br_id = br->br_id;
3954+ opened = au_lcnt_read(&br->br_nfiles, /*do_rev*/1);
3955+ if (unlikely(opened)) {
3956+ to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
3957+ err = PTR_ERR(to_free);
3958+ if (IS_ERR(to_free))
3959+ goto out;
3960+
3961+ err = test_file_busy(sb, br_id, to_free, opened);
3962+ if (unlikely(err)) {
3963+ AuVerbose(verbose, "%llu file(s) opened\n", opened);
3964+ goto out;
3965+ }
3966+ }
3967+
3968+ wbr = br->br_wbr;
3969+ do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
3970+ if (do_wh) {
3971+ /* instead of WbrWhMustWriteLock(wbr) */
3972+ SiMustWriteLock(sb);
3973+ for (i = 0; i < AuBrWh_Last; i++) {
3974+ dput(wbr->wbr_wh[i]);
3975+ wbr->wbr_wh[i] = NULL;
3976+ }
3977+ }
3978+
3979+ err = test_children_busy(root, bindex, verbose);
3980+ if (unlikely(err)) {
3981+ if (do_wh)
3982+ goto out_wh;
3983+ goto out;
3984+ }
3985+
3986+ err = 0;
3987+ if (to_free) {
3988+ /*
3989+ * now we confirmed the branch is deletable.
3990+ * let's free the remaining opened dirs on the branch.
3991+ */
3992+ di_write_unlock(root);
3993+ br_del_file(to_free, opened, br_id);
3994+ di_write_lock_child(root);
3995+ }
3996+
3997+ sysaufs_brs_del(sb, bindex); /* remove successors */
3998+ dbgaufs_xino_del(br); /* remove one */
3999+ au_br_do_del(sb, bindex, br);
4000+ sysaufs_brs_add(sb, bindex); /* append successors */
4001+ dbgaufs_brs_add(sb, bindex, /*topdown*/1); /* rename successors */
4002+
4003+ if (!bindex) {
4004+ au_cpup_attr_all(d_inode(root), /*force*/1);
4005+ sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
4006+ } else
4007+ au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
4008+ if (au_opt_test(mnt_flags, PLINK))
4009+ au_plink_half_refresh(sb, br_id);
4010+
4011+ goto out; /* success */
4012+
4013+out_wh:
4014+ /* revert */
4015+ rerr = au_br_init_wh(sb, br, br->br_perm);
4016+ if (rerr)
4017+ pr_warn("failed re-creating base whiteout, %s. (%d)\n",
4018+ del->pathname, rerr);
4019+out:
4020+ if (to_free)
4021+ au_farray_free(to_free, opened);
4022+ return err;
4023+}
4024+
4025+/* ---------------------------------------------------------------------- */
4026+
4027+static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
4028+{
4029+ int err;
4030+ aufs_bindex_t btop, bbot;
4031+ struct aufs_ibusy ibusy;
4032+ struct inode *inode, *h_inode;
4033+
4034+ err = -EPERM;
4035+ if (unlikely(!capable(CAP_SYS_ADMIN)))
4036+ goto out;
4037+
4038+ err = copy_from_user(&ibusy, arg, sizeof(ibusy));
4039+ if (!err)
4040+ /* VERIFY_WRITE */
4041+ err = !access_ok(&arg->h_ino, sizeof(arg->h_ino));
4042+ if (unlikely(err)) {
4043+ err = -EFAULT;
4044+ AuTraceErr(err);
4045+ goto out;
4046+ }
4047+
4048+ err = -EINVAL;
4049+ si_read_lock(sb, AuLock_FLUSH);
4050+ if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
4051+ goto out_unlock;
4052+
4053+ err = 0;
4054+ ibusy.h_ino = 0; /* invalid */
4055+ inode = ilookup(sb, ibusy.ino);
4056+ if (!inode
4057+ || inode->i_ino == AUFS_ROOT_INO
4058+ || au_is_bad_inode(inode))
4059+ goto out_unlock;
4060+
4061+ ii_read_lock_child(inode);
4062+ btop = au_ibtop(inode);
4063+ bbot = au_ibbot(inode);
4064+ if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
4065+ h_inode = au_h_iptr(inode, ibusy.bindex);
4066+ if (h_inode && au_test_ibusy(inode, btop, bbot))
4067+ ibusy.h_ino = h_inode->i_ino;
4068+ }
4069+ ii_read_unlock(inode);
4070+ iput(inode);
4071+
4072+out_unlock:
4073+ si_read_unlock(sb);
4074+ if (!err) {
4075+ err = __put_user(ibusy.h_ino, &arg->h_ino);
4076+ if (unlikely(err)) {
4077+ err = -EFAULT;
4078+ AuTraceErr(err);
4079+ }
4080+ }
4081+out:
4082+ return err;
4083+}
4084+
4085+long au_ibusy_ioctl(struct file *file, unsigned long arg)
4086+{
4087+ return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
4088+}
4089+
4090+#ifdef CONFIG_COMPAT
4091+long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
4092+{
4093+ return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
4094+}
4095+#endif
4096+
4097+/* ---------------------------------------------------------------------- */
4098+
4099+/*
4100+ * change a branch permission
4101+ */
4102+
4103+static void au_warn_ima(void)
4104+{
4105+#ifdef CONFIG_IMA
4106+ /* since it doesn't support mark_files_ro() */
4107+ AuWarn1("RW -> RO makes IMA to produce wrong message\n");
4108+#endif
4109+}
4110+
4111+static int do_need_sigen_inc(int a, int b)
4112+{
4113+ return au_br_whable(a) && !au_br_whable(b);
4114+}
4115+
4116+static int need_sigen_inc(int old, int new)
4117+{
4118+ return do_need_sigen_inc(old, new)
4119+ || do_need_sigen_inc(new, old);
4120+}
4121+
4122+static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
4123+{
4124+ int err, do_warn;
4125+ unsigned int mnt_flags;
4126+ unsigned long long ull, max;
4127+ aufs_bindex_t br_id;
4128+ unsigned char verbose, writer;
4129+ struct file *file, *hf, **array;
4130+ struct au_hfile *hfile;
4131+ struct inode *h_inode;
4132+
4133+ mnt_flags = au_mntflags(sb);
4134+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
4135+
4136+ array = au_farray_alloc(sb, &max);
4137+ err = PTR_ERR(array);
4138+ if (IS_ERR(array))
4139+ goto out;
4140+
4141+ do_warn = 0;
4142+ br_id = au_sbr_id(sb, bindex);
4143+ for (ull = 0; ull < max; ull++) {
4144+ file = array[ull];
4145+ if (unlikely(!file))
4146+ break;
4147+
4148+ /* AuDbg("%pD\n", file); */
4149+ fi_read_lock(file);
4150+ if (unlikely(au_test_mmapped(file))) {
4151+ err = -EBUSY;
4152+ AuVerbose(verbose, "mmapped %pD\n", file);
4153+ AuDbgFile(file);
4154+ FiMustNoWaiters(file);
4155+ fi_read_unlock(file);
4156+ goto out_array;
4157+ }
4158+
4159+ hfile = &au_fi(file)->fi_htop;
4160+ hf = hfile->hf_file;
4161+ if (!d_is_reg(file->f_path.dentry)
4162+ || !(file->f_mode & FMODE_WRITE)
4163+ || hfile->hf_br->br_id != br_id
4164+ || !(hf->f_mode & FMODE_WRITE))
4165+ array[ull] = NULL;
4166+ else {
4167+ do_warn = 1;
4168+ get_file(file);
4169+ }
4170+
4171+ FiMustNoWaiters(file);
4172+ fi_read_unlock(file);
4173+ fput(file);
4174+ }
4175+
4176+ err = 0;
4177+ if (do_warn)
4178+ au_warn_ima();
4179+
4180+ for (ull = 0; ull < max; ull++) {
4181+ file = array[ull];
4182+ if (!file)
4183+ continue;
4184+
4185+ /* todo: already flushed? */
4186+ /*
4187+ * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4188+ * approach which resets f_mode and calls mnt_drop_write() and
4189+ * file_release_write() for each file, because the branch
4190+ * attribute in aufs world is totally different from the native
4191+ * fs rw/ro mode.
4192+ */
4193+ /* fi_read_lock(file); */
4194+ hfile = &au_fi(file)->fi_htop;
4195+ hf = hfile->hf_file;
4196+ /* fi_read_unlock(file); */
4197+ spin_lock(&hf->f_lock);
4198+ writer = !!(hf->f_mode & FMODE_WRITER);
4199+ hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
4200+ spin_unlock(&hf->f_lock);
4201+ if (writer) {
4202+ h_inode = file_inode(hf);
4203+ if (hf->f_mode & FMODE_READ)
4204+ i_readcount_inc(h_inode);
4205+ put_write_access(h_inode);
4206+ __mnt_drop_write(hf->f_path.mnt);
4207+ }
4208+ }
4209+
4210+out_array:
4211+ au_farray_free(array, max);
4212+out:
4213+ AuTraceErr(err);
4214+ return err;
4215+}
4216+
4217+int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4218+ int *do_refresh)
4219+{
4220+ int err, rerr;
4221+ aufs_bindex_t bindex;
4222+ struct dentry *root;
4223+ struct au_branch *br;
4224+ struct au_br_fhsm *bf;
4225+
4226+ root = sb->s_root;
4227+ bindex = au_find_dbindex(root, mod->h_root);
4228+ if (bindex < 0) {
4229+ if (remount)
4230+ return 0; /* success */
4231+ err = -ENOENT;
4232+ pr_err("%s no such branch\n", mod->path);
4233+ goto out;
4234+ }
4235+ AuDbg("bindex b%d\n", bindex);
4236+
4237+ err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
4238+ if (unlikely(err))
4239+ goto out;
4240+
4241+ br = au_sbr(sb, bindex);
4242+ AuDebugOn(mod->h_root != au_br_dentry(br));
4243+ if (br->br_perm == mod->perm)
4244+ return 0; /* success */
4245+
4246+ /* pre-allocate for non-fhsm --> fhsm */
4247+ bf = NULL;
4248+ if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4249+ err = au_fhsm_br_alloc(br);
4250+ if (unlikely(err))
4251+ goto out;
4252+ bf = br->br_fhsm;
4253+ br->br_fhsm = NULL;
4254+ }
4255+
4256+ if (au_br_writable(br->br_perm)) {
4257+ /* remove whiteout base */
4258+ err = au_br_init_wh(sb, br, mod->perm);
4259+ if (unlikely(err))
4260+ goto out_bf;
4261+
4262+ if (!au_br_writable(mod->perm)) {
4263+ /* rw --> ro, file might be mmapped */
4264+ DiMustNoWaiters(root);
4265+ IiMustNoWaiters(d_inode(root));
4266+ di_write_unlock(root);
4267+ err = au_br_mod_files_ro(sb, bindex);
4268+ /* aufs_write_lock() calls ..._child() */
4269+ di_write_lock_child(root);
4270+
4271+ if (unlikely(err)) {
4272+ rerr = -ENOMEM;
4273+ br->br_wbr = kzalloc(sizeof(*br->br_wbr),
4274+ GFP_NOFS);
4275+ if (br->br_wbr)
4276+ rerr = au_wbr_init(br, sb, br->br_perm);
4277+ if (unlikely(rerr)) {
4278+ AuIOErr("nested error %d (%d)\n",
4279+ rerr, err);
4280+ br->br_perm = mod->perm;
4281+ }
4282+ }
4283+ }
4284+ } else if (au_br_writable(mod->perm)) {
4285+ /* ro --> rw */
4286+ err = -ENOMEM;
4287+ br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
4288+ if (br->br_wbr) {
4289+ err = au_wbr_init(br, sb, mod->perm);
4290+ if (unlikely(err)) {
4291+ au_kfree_rcu(br->br_wbr);
4292+ br->br_wbr = NULL;
4293+ }
4294+ }
4295+ }
4296+ if (unlikely(err))
4297+ goto out_bf;
4298+
4299+ if (au_br_fhsm(br->br_perm)) {
4300+ if (!au_br_fhsm(mod->perm)) {
4301+ /* fhsm --> non-fhsm */
4302+ au_br_fhsm_fin(br->br_fhsm);
4303+ au_kfree_rcu(br->br_fhsm);
4304+ br->br_fhsm = NULL;
4305+ }
4306+ } else if (au_br_fhsm(mod->perm))
4307+ /* non-fhsm --> fhsm */
4308+ br->br_fhsm = bf;
4309+
4310+ *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4311+ br->br_perm = mod->perm;
4312+ goto out; /* success */
4313+
4314+out_bf:
4315+ au_kfree_try_rcu(bf);
4316+out:
4317+ AuTraceErr(err);
4318+ return err;
4319+}
4320+
4321+/* ---------------------------------------------------------------------- */
4322+
4323+int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4324+{
4325+ int err;
4326+ struct kstatfs kstfs;
4327+
4328+ err = vfs_statfs(&br->br_path, &kstfs);
4329+ if (!err) {
4330+ stfs->f_blocks = kstfs.f_blocks;
4331+ stfs->f_bavail = kstfs.f_bavail;
4332+ stfs->f_files = kstfs.f_files;
4333+ stfs->f_ffree = kstfs.f_ffree;
4334+ }
4335+
4336+ return err;
4337+}
4338diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
4339--- /usr/share/empty/fs/aufs/branch.h 1970-01-01 01:00:00.000000000 +0100
4340+++ linux/fs/aufs/branch.h 2021-12-03 15:40:58.233313963 +0100
4341@@ -0,0 +1,375 @@
4342+/* SPDX-License-Identifier: GPL-2.0 */
4343+/*
4344+ * Copyright (C) 2005-2021 Junjiro R. Okajima
4345+ *
4346+ * This program, aufs is free software; you can redistribute it and/or modify
4347+ * it under the terms of the GNU General Public License as published by
4348+ * the Free Software Foundation; either version 2 of the License, or
4349+ * (at your option) any later version.
4350+ *
4351+ * This program is distributed in the hope that it will be useful,
4352+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
4353+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4354+ * GNU General Public License for more details.
4355+ *
4356+ * You should have received a copy of the GNU General Public License
4357+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4358+ */
4359+
4360+/*
4361+ * branch filesystems and xino for them
4362+ */
4363+
4364+#ifndef __AUFS_BRANCH_H__
4365+#define __AUFS_BRANCH_H__
4366+
4367+#ifdef __KERNEL__
4368+
4369+#include <linux/mount.h>
4370+#include "dirren.h"
4371+#include "dynop.h"
4372+#include "lcnt.h"
4373+#include "rwsem.h"
4374+#include "super.h"
4375+
4376+/* ---------------------------------------------------------------------- */
4377+
4378+/* a xino file */
4379+struct au_xino {
4380+ struct file **xi_file;
4381+ unsigned int xi_nfile;
4382+
4383+ struct {
4384+ spinlock_t spin;
4385+ ino_t *array;
4386+ int total;
4387+ /* reserved for future use */
4388+ /* unsigned long *bitmap; */
4389+ wait_queue_head_t wqh;
4390+ } xi_nondir;
4391+
4392+ struct mutex xi_mtx; /* protects xi_file array */
4393+ struct hlist_bl_head xi_writing;
4394+
4395+ atomic_t xi_truncating;
4396+
4397+ struct kref xi_kref;
4398+};
4399+
4400+/* File-based Hierarchical Storage Management */
4401+struct au_br_fhsm {
4402+#ifdef CONFIG_AUFS_FHSM
4403+ struct mutex bf_lock;
4404+ unsigned long bf_jiffy;
4405+ struct aufs_stfs bf_stfs;
4406+ int bf_readable;
4407+#endif
4408+};
4409+
4410+/* members for writable branch only */
4411+enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4412+struct au_wbr {
4413+ struct au_rwsem wbr_wh_rwsem;
4414+ struct dentry *wbr_wh[AuBrWh_Last];
4415+ atomic_t wbr_wh_running;
4416+#define wbr_whbase wbr_wh[AuBrWh_BASE] /* whiteout base */
4417+#define wbr_plink wbr_wh[AuBrWh_PLINK] /* pseudo-link dir */
4418+#define wbr_orph wbr_wh[AuBrWh_ORPH] /* dir for orphans */
4419+
4420+ /* mfs mode */
4421+ unsigned long long wbr_bytes;
4422+};
4423+
4424+/* ext2 has 3 types of operations at least, ext3 has 4 */
4425+#define AuBrDynOp (AuDyLast * 4)
4426+
4427+#ifdef CONFIG_AUFS_HFSNOTIFY
4428+/* support for asynchronous destruction */
4429+struct au_br_hfsnotify {
4430+ struct fsnotify_group *hfsn_group;
4431+};
4432+#endif
4433+
4434+/* sysfs entries */
4435+struct au_brsysfs {
4436+ char name[16];
4437+ struct attribute attr;
4438+};
4439+
4440+enum {
4441+ AuBrSysfs_BR,
4442+ AuBrSysfs_BRID,
4443+ AuBrSysfs_Last
4444+};
4445+
4446+/* protected by superblock rwsem */
4447+struct au_branch {
4448+ struct au_xino *br_xino;
4449+
4450+ aufs_bindex_t br_id;
4451+
4452+ int br_perm;
4453+ struct path br_path;
4454+ spinlock_t br_dykey_lock;
4455+ struct au_dykey *br_dykey[AuBrDynOp];
4456+ au_lcnt_t br_nfiles; /* opened files */
4457+ au_lcnt_t br_count; /* in-use for other */
4458+
4459+ struct au_wbr *br_wbr;
4460+ struct au_br_fhsm *br_fhsm;
4461+
4462+#ifdef CONFIG_AUFS_HFSNOTIFY
4463+ struct au_br_hfsnotify *br_hfsn;
4464+#endif
4465+
4466+#ifdef CONFIG_SYSFS
4467+ /* entries under sysfs per mount-point */
4468+ struct au_brsysfs br_sysfs[AuBrSysfs_Last];
4469+#endif
4470+
4471+#ifdef CONFIG_DEBUG_FS
4472+ struct dentry *br_dbgaufs; /* xino */
4473+#endif
4474+
4475+ struct au_dr_br br_dirren;
4476+};
4477+
4478+/* ---------------------------------------------------------------------- */
4479+
4480+static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4481+{
4482+ return br->br_path.mnt;
4483+}
4484+
4485+static inline struct dentry *au_br_dentry(struct au_branch *br)
4486+{
4487+ return br->br_path.dentry;
4488+}
4489+
4490+static inline struct user_namespace *au_br_userns(struct au_branch *br)
4491+{
4492+ return mnt_user_ns(br->br_path.mnt);
4493+}
4494+
4495+static inline struct super_block *au_br_sb(struct au_branch *br)
4496+{
4497+ return au_br_mnt(br)->mnt_sb;
4498+}
4499+
4500+static inline int au_br_rdonly(struct au_branch *br)
4501+{
4502+ return (sb_rdonly(au_br_sb(br))
4503+ || !au_br_writable(br->br_perm))
4504+ ? -EROFS : 0;
4505+}
4506+
4507+static inline int au_br_hnotifyable(int brperm __maybe_unused)
4508+{
4509+#ifdef CONFIG_AUFS_HNOTIFY
4510+ return !(brperm & AuBrPerm_RR);
4511+#else
4512+ return 0;
4513+#endif
4514+}
4515+
4516+static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4517+{
4518+ int err, exec_flag;
4519+
4520+ err = 0;
4521+ exec_flag = oflag & __FMODE_EXEC;
4522+ if (unlikely(exec_flag && path_noexec(&br->br_path)))
4523+ err = -EACCES;
4524+
4525+ return err;
4526+}
4527+
4528+static inline void au_xino_get(struct au_branch *br)
4529+{
4530+ struct au_xino *xi;
4531+
4532+ xi = br->br_xino;
4533+ if (xi)
4534+ kref_get(&xi->xi_kref);
4535+}
4536+
4537+static inline int au_xino_count(struct au_branch *br)
4538+{
4539+ int v;
4540+ struct au_xino *xi;
4541+
4542+ v = 0;
4543+ xi = br->br_xino;
4544+ if (xi)
4545+ v = kref_read(&xi->xi_kref);
4546+
4547+ return v;
4548+}
4549+
4550+/* ---------------------------------------------------------------------- */
4551+
4552+/* branch.c */
4553+struct au_sbinfo;
4554+void au_br_free(struct au_sbinfo *sinfo);
4555+int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4556+struct au_opt_add;
4557+int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4558+struct au_opt_del;
4559+int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
4560+long au_ibusy_ioctl(struct file *file, unsigned long arg);
4561+#ifdef CONFIG_COMPAT
4562+long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4563+#endif
4564+struct au_opt_mod;
4565+int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4566+ int *do_refresh);
4567+struct aufs_stfs;
4568+int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
4569+
4570+/* xino.c */
4571+static const loff_t au_loff_max = LLONG_MAX;
4572+
4573+aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry);
4574+struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
4575+ int wbrtop);
4576+struct file *au_xino_create2(struct super_block *sb, struct path *base,
4577+ struct file *copy_src);
4578+struct au_xi_new {
4579+ struct au_xino *xi; /* switch between xino and xigen */
4580+ int idx;
4581+ struct path *base;
4582+ struct file *copy_src;
4583+};
4584+struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew);
4585+
4586+int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4587+ ino_t *ino);
4588+int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4589+ ino_t ino);
4590+ssize_t xino_fread(struct file *file, void *buf, size_t size, loff_t *pos);
4591+ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos);
4592+
4593+int au_xib_trunc(struct super_block *sb);
4594+int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin);
4595+
4596+struct au_xino *au_xino_alloc(unsigned int nfile);
4597+int au_xino_put(struct au_branch *br);
4598+struct file *au_xino_file1(struct au_xino *xi);
4599+
4600+struct au_opt_xino;
4601+void au_xino_clr(struct super_block *sb);
4602+int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount);
4603+struct file *au_xino_def(struct super_block *sb);
4604+int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4605+ struct path *base);
4606+
4607+ino_t au_xino_new_ino(struct super_block *sb);
4608+void au_xino_delete_inode(struct inode *inode, const int unlinked);
4609+
4610+void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
4611+ ino_t h_ino, int idx);
4612+int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4613+ int *idx);
4614+
4615+int au_xino_path(struct seq_file *seq, struct file *file);
4616+
4617+/* ---------------------------------------------------------------------- */
4618+
4619+/* @idx is signed to accept -1 meaning the first file */
4620+static inline struct file *au_xino_file(struct au_xino *xi, int idx)
4621+{
4622+ struct file *file;
4623+
4624+ file = NULL;
4625+ if (!xi)
4626+ goto out;
4627+
4628+ if (idx >= 0) {
4629+ if (idx < xi->xi_nfile)
4630+ file = xi->xi_file[idx];
4631+ } else
4632+ file = au_xino_file1(xi);
4633+
4634+out:
4635+ return file;
4636+}
4637+
4638+/* ---------------------------------------------------------------------- */
4639+
4640+/* Superblock to branch */
4641+static inline
4642+aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4643+{
4644+ return au_sbr(sb, bindex)->br_id;
4645+}
4646+
4647+static inline
4648+struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4649+{
4650+ return au_br_mnt(au_sbr(sb, bindex));
4651+}
4652+
4653+static inline
4654+struct user_namespace *au_sbr_userns(struct super_block *sb, aufs_bindex_t bindex)
4655+{
4656+ return au_br_userns(au_sbr(sb, bindex));
4657+}
4658+
4659+static inline
4660+struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4661+{
4662+ return au_br_sb(au_sbr(sb, bindex));
4663+}
4664+
4665+static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4666+{
4667+ return au_sbr(sb, bindex)->br_perm;
4668+}
4669+
4670+static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4671+{
4672+ return au_br_whable(au_sbr_perm(sb, bindex));
4673+}
4674+
4675+/* ---------------------------------------------------------------------- */
4676+
4677+#define wbr_wh_read_lock(wbr) au_rw_read_lock(&(wbr)->wbr_wh_rwsem)
4678+#define wbr_wh_write_lock(wbr) au_rw_write_lock(&(wbr)->wbr_wh_rwsem)
4679+#define wbr_wh_read_trylock(wbr) au_rw_read_trylock(&(wbr)->wbr_wh_rwsem)
4680+#define wbr_wh_write_trylock(wbr) au_rw_write_trylock(&(wbr)->wbr_wh_rwsem)
4681+/*
4682+#define wbr_wh_read_trylock_nested(wbr) \
4683+ au_rw_read_trylock_nested(&(wbr)->wbr_wh_rwsem)
4684+#define wbr_wh_write_trylock_nested(wbr) \
4685+ au_rw_write_trylock_nested(&(wbr)->wbr_wh_rwsem)
4686+*/
4687+
4688+#define wbr_wh_read_unlock(wbr) au_rw_read_unlock(&(wbr)->wbr_wh_rwsem)
4689+#define wbr_wh_write_unlock(wbr) au_rw_write_unlock(&(wbr)->wbr_wh_rwsem)
4690+#define wbr_wh_downgrade_lock(wbr) au_rw_dgrade_lock(&(wbr)->wbr_wh_rwsem)
4691+
4692+#define WbrWhMustNoWaiters(wbr) AuRwMustNoWaiters(&(wbr)->wbr_wh_rwsem)
4693+#define WbrWhMustAnyLock(wbr) AuRwMustAnyLock(&(wbr)->wbr_wh_rwsem)
4694+#define WbrWhMustWriteLock(wbr) AuRwMustWriteLock(&(wbr)->wbr_wh_rwsem)
4695+
4696+/* ---------------------------------------------------------------------- */
4697+
4698+#ifdef CONFIG_AUFS_FHSM
4699+static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4700+{
4701+ mutex_init(&brfhsm->bf_lock);
4702+ brfhsm->bf_jiffy = 0;
4703+ brfhsm->bf_readable = 0;
4704+}
4705+
4706+static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4707+{
4708+ mutex_destroy(&brfhsm->bf_lock);
4709+}
4710+#else
4711+AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4712+AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4713+#endif
4714+
4715+#endif /* __KERNEL__ */
4716+#endif /* __AUFS_BRANCH_H__ */
4717diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
4718--- /usr/share/empty/fs/aufs/conf.mk 1970-01-01 01:00:00.000000000 +0100
4719+++ linux/fs/aufs/conf.mk 2021-12-03 15:38:59.933313976 +0100
4720@@ -0,0 +1,40 @@
4721+# SPDX-License-Identifier: GPL-2.0
4722+
4723+AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4724+
4725+define AuConf
4726+ifdef ${1}
4727+AuConfStr += ${1}=${${1}}
4728+endif
4729+endef
4730+
4731+AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
4732+ SBILIST \
4733+ HNOTIFY HFSNOTIFY \
4734+ EXPORT INO_T_64 \
4735+ XATTR \
4736+ FHSM \
4737+ RDU \
4738+ DIRREN \
4739+ SHWH \
4740+ BR_RAMFS \
4741+ BR_FUSE POLL \
4742+ BR_HFSPLUS \
4743+ BDEV_LOOP \
4744+ DEBUG MAGIC_SYSRQ
4745+$(foreach i, ${AuConfAll}, \
4746+ $(eval $(call AuConf,CONFIG_AUFS_${i})))
4747+
4748+AuConfName = ${obj}/conf.str
4749+${AuConfName}.tmp: FORCE
4750+ @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4751+${AuConfName}: ${AuConfName}.tmp
4752+ @diff -q $< $@ > /dev/null 2>&1 || { \
4753+ echo ' GEN ' $@; \
4754+ cp -p $< $@; \
4755+ }
4756+FORCE:
4757+clean-files += ${AuConfName} ${AuConfName}.tmp
4758+${obj}/sysfs.o: ${AuConfName}
4759+
4760+-include ${srctree}/${src}/conf_priv.mk
4761diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
4762--- /usr/share/empty/fs/aufs/cpup.c 1970-01-01 01:00:00.000000000 +0100
4763+++ linux/fs/aufs/cpup.c 2021-12-03 15:40:58.233313963 +0100
4764@@ -0,0 +1,1459 @@
4765+// SPDX-License-Identifier: GPL-2.0
4766+/*
4767+ * Copyright (C) 2005-2021 Junjiro R. Okajima
4768+ *
4769+ * This program, aufs is free software; you can redistribute it and/or modify
4770+ * it under the terms of the GNU General Public License as published by
4771+ * the Free Software Foundation; either version 2 of the License, or
4772+ * (at your option) any later version.
4773+ *
4774+ * This program is distributed in the hope that it will be useful,
4775+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
4776+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4777+ * GNU General Public License for more details.
4778+ *
4779+ * You should have received a copy of the GNU General Public License
4780+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4781+ */
4782+
4783+/*
4784+ * copy-up functions, see wbr_policy.c for copy-down
4785+ */
4786+
4787+#include <linux/fs_stack.h>
4788+#include <linux/mm.h>
4789+#include <linux/task_work.h>
4790+#include "aufs.h"
4791+
4792+void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
4793+{
4794+ const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
4795+ | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
4796+
4797+ BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
4798+
4799+ dst->i_flags |= iflags & ~mask;
4800+ if (au_test_fs_notime(dst->i_sb))
4801+ dst->i_flags |= S_NOATIME | S_NOCMTIME;
4802+}
4803+
4804+void au_cpup_attr_timesizes(struct inode *inode)
4805+{
4806+ struct inode *h_inode;
4807+
4808+ h_inode = au_h_iptr(inode, au_ibtop(inode));
4809+ fsstack_copy_attr_times(inode, h_inode);
4810+ fsstack_copy_inode_size(inode, h_inode);
4811+}
4812+
4813+void au_cpup_attr_nlink(struct inode *inode, int force)
4814+{
4815+ struct inode *h_inode;
4816+ struct super_block *sb;
4817+ aufs_bindex_t bindex, bbot;
4818+
4819+ sb = inode->i_sb;
4820+ bindex = au_ibtop(inode);
4821+ h_inode = au_h_iptr(inode, bindex);
4822+ if (!force
4823+ && !S_ISDIR(h_inode->i_mode)
4824+ && au_opt_test(au_mntflags(sb), PLINK)
4825+ && au_plink_test(inode))
4826+ return;
4827+
4828+ /*
4829+ * 0 can happen in revalidating.
4830+ * h_inode->i_mutex may not be held here, but it is harmless since once
4831+ * i_nlink reaches 0, it will never become positive except O_TMPFILE
4832+ * case.
4833+ * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
4834+ * the incorrect link count.
4835+ */
4836+ set_nlink(inode, h_inode->i_nlink);
4837+
4838+ /*
4839+ * fewer nlink makes find(1) noisy, but larger nlink doesn't.
4840+ * it may includes whplink directory.
4841+ */
4842+ if (S_ISDIR(h_inode->i_mode)) {
4843+ bbot = au_ibbot(inode);
4844+ for (bindex++; bindex <= bbot; bindex++) {
4845+ h_inode = au_h_iptr(inode, bindex);
4846+ if (h_inode)
4847+ au_add_nlink(inode, h_inode);
4848+ }
4849+ }
4850+}
4851+
4852+void au_cpup_attr_changeable(struct inode *inode)
4853+{
4854+ struct inode *h_inode;
4855+
4856+ h_inode = au_h_iptr(inode, au_ibtop(inode));
4857+ inode->i_mode = h_inode->i_mode;
4858+ inode->i_uid = h_inode->i_uid;
4859+ inode->i_gid = h_inode->i_gid;
4860+ au_cpup_attr_timesizes(inode);
4861+ au_cpup_attr_flags(inode, h_inode->i_flags);
4862+}
4863+
4864+void au_cpup_igen(struct inode *inode, struct inode *h_inode)
4865+{
4866+ struct au_iinfo *iinfo = au_ii(inode);
4867+
4868+ IiMustWriteLock(inode);
4869+
4870+ iinfo->ii_higen = h_inode->i_generation;
4871+ iinfo->ii_hsb1 = h_inode->i_sb;
4872+}
4873+
4874+void au_cpup_attr_all(struct inode *inode, int force)
4875+{
4876+ struct inode *h_inode;
4877+
4878+ h_inode = au_h_iptr(inode, au_ibtop(inode));
4879+ au_cpup_attr_changeable(inode);
4880+ if (inode->i_nlink > 0)
4881+ au_cpup_attr_nlink(inode, force);
4882+ inode->i_rdev = h_inode->i_rdev;
4883+ inode->i_blkbits = h_inode->i_blkbits;
4884+ au_cpup_igen(inode, h_inode);
4885+}
4886+
4887+/* ---------------------------------------------------------------------- */
4888+
4889+/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
4890+
4891+/* keep the timestamps of the parent dir when cpup */
4892+void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
4893+ struct path *h_path)
4894+{
4895+ struct inode *h_inode;
4896+
4897+ dt->dt_dentry = dentry;
4898+ dt->dt_h_path = *h_path;
4899+ h_inode = d_inode(h_path->dentry);
4900+ dt->dt_atime = h_inode->i_atime;
4901+ dt->dt_mtime = h_inode->i_mtime;
4902+ /* smp_mb(); */
4903+}
4904+
4905+void au_dtime_revert(struct au_dtime *dt)
4906+{
4907+ struct iattr attr;
4908+ int err;
4909+
4910+ attr.ia_atime = dt->dt_atime;
4911+ attr.ia_mtime = dt->dt_mtime;
4912+ attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
4913+ | ATTR_ATIME | ATTR_ATIME_SET;
4914+
4915+ /* no delegation since this is a directory */
4916+ err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
4917+ if (unlikely(err))
4918+ pr_warn("restoring timestamps failed(%d). ignored\n", err);
4919+}
4920+
4921+/* ---------------------------------------------------------------------- */
4922+
4923+/* internal use only */
4924+struct au_cpup_reg_attr {
4925+ int valid;
4926+ struct kstat st;
4927+ unsigned int iflags; /* inode->i_flags */
4928+};
4929+
4930+static noinline_for_stack
4931+int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct path *h_src,
4932+ struct au_cpup_reg_attr *h_src_attr)
4933+{
4934+ int err, sbits, icex;
4935+ unsigned int mnt_flags;
4936+ unsigned char verbose;
4937+ struct iattr ia;
4938+ struct path h_path;
4939+ struct inode *h_isrc, *h_idst;
4940+ struct kstat *h_st;
4941+ struct au_branch *br;
4942+
4943+ br = au_sbr(dst->d_sb, bindex);
4944+ h_path.mnt = au_br_mnt(br);
4945+ h_path.dentry = au_h_dptr(dst, bindex);
4946+ h_idst = d_inode(h_path.dentry);
4947+ h_isrc = d_inode(h_src->dentry);
4948+ ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
4949+ | ATTR_ATIME | ATTR_MTIME
4950+ | ATTR_ATIME_SET | ATTR_MTIME_SET;
4951+ if (h_src_attr && h_src_attr->valid) {
4952+ h_st = &h_src_attr->st;
4953+ ia.ia_uid = h_st->uid;
4954+ ia.ia_gid = h_st->gid;
4955+ ia.ia_atime = h_st->atime;
4956+ ia.ia_mtime = h_st->mtime;
4957+ if (h_idst->i_mode != h_st->mode
4958+ && !S_ISLNK(h_idst->i_mode)) {
4959+ ia.ia_valid |= ATTR_MODE;
4960+ ia.ia_mode = h_st->mode;
4961+ }
4962+ sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
4963+ au_cpup_attr_flags(h_idst, h_src_attr->iflags);
4964+ } else {
4965+ ia.ia_uid = h_isrc->i_uid;
4966+ ia.ia_gid = h_isrc->i_gid;
4967+ ia.ia_atime = h_isrc->i_atime;
4968+ ia.ia_mtime = h_isrc->i_mtime;
4969+ if (h_idst->i_mode != h_isrc->i_mode
4970+ && !S_ISLNK(h_idst->i_mode)) {
4971+ ia.ia_valid |= ATTR_MODE;
4972+ ia.ia_mode = h_isrc->i_mode;
4973+ }
4974+ sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
4975+ au_cpup_attr_flags(h_idst, h_isrc->i_flags);
4976+ }
4977+ /* no delegation since it is just created */
4978+ err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4979+
4980+ /* is this nfs only? */
4981+ if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
4982+ ia.ia_valid = ATTR_FORCE | ATTR_MODE;
4983+ ia.ia_mode = h_isrc->i_mode;
4984+ err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4985+ }
4986+
4987+ icex = br->br_perm & AuBrAttr_ICEX;
4988+ if (!err) {
4989+ mnt_flags = au_mntflags(dst->d_sb);
4990+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
4991+ err = au_cpup_xattr(&h_path, h_src, icex, verbose);
4992+ }
4993+
4994+ return err;
4995+}
4996+
4997+/* ---------------------------------------------------------------------- */
4998+
4999+static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
5000+ char *buf, unsigned long blksize)
5001+{
5002+ int err;
5003+ size_t sz, rbytes, wbytes;
5004+ unsigned char all_zero;
5005+ char *p, *zp;
5006+ struct inode *h_inode;
5007+ /* reduce stack usage */
5008+ struct iattr *ia;
5009+
5010+ zp = page_address(ZERO_PAGE(0));
5011+ if (unlikely(!zp))
5012+ return -ENOMEM; /* possible? */
5013+
5014+ err = 0;
5015+ all_zero = 0;
5016+ while (len) {
5017+ AuDbg("len %lld\n", len);
5018+ sz = blksize;
5019+ if (len < blksize)
5020+ sz = len;
5021+
5022+ rbytes = 0;
5023+ /* todo: signal_pending? */
5024+ while (!rbytes || err == -EAGAIN || err == -EINTR) {
5025+ rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
5026+ err = rbytes;
5027+ }
5028+ if (unlikely(err < 0))
5029+ break;
5030+
5031+ all_zero = 0;
5032+ if (len >= rbytes && rbytes == blksize)
5033+ all_zero = !memcmp(buf, zp, rbytes);
5034+ if (!all_zero) {
5035+ wbytes = rbytes;
5036+ p = buf;
5037+ while (wbytes) {
5038+ size_t b;
5039+
5040+ b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
5041+ err = b;
5042+ /* todo: signal_pending? */
5043+ if (unlikely(err == -EAGAIN || err == -EINTR))
5044+ continue;
5045+ if (unlikely(err < 0))
5046+ break;
5047+ wbytes -= b;
5048+ p += b;
5049+ }
5050+ if (unlikely(err < 0))
5051+ break;
5052+ } else {
5053+ loff_t res;
5054+
5055+ AuLabel(hole);
5056+ res = vfsub_llseek(dst, rbytes, SEEK_CUR);
5057+ err = res;
5058+ if (unlikely(res < 0))
5059+ break;
5060+ }
5061+ len -= rbytes;
5062+ err = 0;
5063+ }
5064+
5065+ /* the last block may be a hole */
5066+ if (!err && all_zero) {
5067+ AuLabel(last hole);
5068+
5069+ err = 1;
5070+ if (au_test_nfs(dst->f_path.dentry->d_sb)) {
5071+ /* nfs requires this step to make last hole */
5072+ /* is this only nfs? */
5073+ do {
5074+ /* todo: signal_pending? */
5075+ err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
5076+ } while (err == -EAGAIN || err == -EINTR);
5077+ if (err == 1)
5078+ dst->f_pos--;
5079+ }
5080+
5081+ if (err == 1) {
5082+ ia = (void *)buf;
5083+ ia->ia_size = dst->f_pos;
5084+ ia->ia_valid = ATTR_SIZE | ATTR_FILE;
5085+ ia->ia_file = dst;
5086+ h_inode = file_inode(dst);
5087+ inode_lock_nested(h_inode, AuLsc_I_CHILD2);
5088+ /* no delegation since it is just created */
5089+ err = vfsub_notify_change(&dst->f_path, ia,
5090+ /*delegated*/NULL);
5091+ inode_unlock(h_inode);
5092+ }
5093+ }
5094+
5095+ return err;
5096+}
5097+
5098+int au_copy_file(struct file *dst, struct file *src, loff_t len)
5099+{
5100+ int err;
5101+ unsigned long blksize;
5102+ unsigned char do_kfree;
5103+ char *buf;
5104+ struct super_block *h_sb;
5105+
5106+ err = -ENOMEM;
5107+ h_sb = file_inode(dst)->i_sb;
5108+ blksize = h_sb->s_blocksize;
5109+ if (!blksize || PAGE_SIZE < blksize)
5110+ blksize = PAGE_SIZE;
5111+ AuDbg("blksize %lu\n", blksize);
5112+ do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
5113+ if (do_kfree)
5114+ buf = kmalloc(blksize, GFP_NOFS);
5115+ else
5116+ buf = (void *)__get_free_page(GFP_NOFS);
5117+ if (unlikely(!buf))
5118+ goto out;
5119+
5120+ if (len > (1 << 22))
5121+ AuDbg("copying a large file %lld\n", (long long)len);
5122+
5123+ src->f_pos = 0;
5124+ dst->f_pos = 0;
5125+ err = au_do_copy_file(dst, src, len, buf, blksize);
5126+ if (do_kfree) {
5127+ AuDebugOn(!au_kfree_do_sz_test(blksize));
5128+ au_kfree_do_rcu(buf);
5129+ } else
5130+ free_page((unsigned long)buf);
5131+
5132+out:
5133+ return err;
5134+}
5135+
5136+static int au_do_copy(struct file *dst, struct file *src, loff_t len)
5137+{
5138+ int err;
5139+ struct super_block *h_src_sb;
5140+ struct inode *h_src_inode;
5141+
5142+ h_src_inode = file_inode(src);
5143+ h_src_sb = h_src_inode->i_sb;
5144+
5145+ /* XFS acquires inode_lock */
5146+ if (!au_test_xfs(h_src_sb))
5147+ err = au_copy_file(dst, src, len);
5148+ else {
5149+ inode_unlock_shared(h_src_inode);
5150+ err = au_copy_file(dst, src, len);
5151+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5152+ }
5153+
5154+ return err;
5155+}
5156+
5157+static int au_clone_or_copy(struct file *dst, struct file *src, loff_t len)
5158+{
5159+ int err;
5160+ loff_t lo;
5161+ struct super_block *h_src_sb;
5162+ struct inode *h_src_inode;
5163+
5164+ h_src_inode = file_inode(src);
5165+ h_src_sb = h_src_inode->i_sb;
5166+ if (h_src_sb != file_inode(dst)->i_sb
5167+ || !dst->f_op->remap_file_range) {
5168+ err = au_do_copy(dst, src, len);
5169+ goto out;
5170+ }
5171+
5172+ if (!au_test_nfs(h_src_sb)) {
5173+ inode_unlock_shared(h_src_inode);
5174+ lo = vfsub_clone_file_range(src, dst, len);
5175+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5176+ } else
5177+ lo = vfsub_clone_file_range(src, dst, len);
5178+ if (lo == len) {
5179+ err = 0;
5180+ goto out; /* success */
5181+ } else if (lo >= 0)
5182+ /* todo: possible? */
5183+ /* paritially succeeded */
5184+ AuDbg("lo %lld, len %lld. Retrying.\n", lo, len);
5185+ else if (lo != -EOPNOTSUPP) {
5186+ /* older XFS has a condition in cloning */
5187+ err = lo;
5188+ goto out;
5189+ }
5190+
5191+ /* the backend fs on NFS may not support cloning */
5192+ err = au_do_copy(dst, src, len);
5193+
5194+out:
5195+ AuTraceErr(err);
5196+ return err;
5197+}
5198+
5199+/*
5200+ * to support a sparse file which is opened with O_APPEND,
5201+ * we need to close the file.
5202+ */
5203+static int au_cp_regular(struct au_cp_generic *cpg)
5204+{
5205+ int err, i;
5206+ enum { SRC, DST };
5207+ struct {
5208+ aufs_bindex_t bindex;
5209+ unsigned int flags;
5210+ struct dentry *dentry;
5211+ int force_wr;
5212+ struct file *file;
5213+ } *f, file[] = {
5214+ {
5215+ .bindex = cpg->bsrc,
5216+ .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
5217+ },
5218+ {
5219+ .bindex = cpg->bdst,
5220+ .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
5221+ .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
5222+ }
5223+ };
5224+ struct au_branch *br;
5225+ struct super_block *sb, *h_src_sb;
5226+ struct inode *h_src_inode;
5227+ struct task_struct *tsk = current;
5228+
5229+ /* bsrc branch can be ro/rw. */
5230+ sb = cpg->dentry->d_sb;
5231+ f = file;
5232+ for (i = 0; i < 2; i++, f++) {
5233+ f->dentry = au_h_dptr(cpg->dentry, f->bindex);
5234+ f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
5235+ /*file*/NULL, f->force_wr);
5236+ if (IS_ERR(f->file)) {
5237+ err = PTR_ERR(f->file);
5238+ if (i == SRC)
5239+ goto out;
5240+ else
5241+ goto out_src;
5242+ }
5243+ }
5244+
5245+ /* try stopping to update while we copyup */
5246+ h_src_inode = d_inode(file[SRC].dentry);
5247+ h_src_sb = h_src_inode->i_sb;
5248+ if (!au_test_nfs(h_src_sb))
5249+ IMustLock(h_src_inode);
5250+ err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
5251+
5252+ /* i wonder if we had O_NO_DELAY_FPUT flag */
5253+ if (tsk->flags & PF_KTHREAD)
5254+ __fput_sync(file[DST].file);
5255+ else {
5256+ /* it happened actually */
5257+ fput(file[DST].file);
5258+ /*
5259+ * too bad.
5260+ * we have to call both since we don't know which place the file
5261+ * was added to.
5262+ */
5263+ task_work_run();
5264+ flush_delayed_fput();
5265+ }
5266+ br = au_sbr(sb, file[DST].bindex);
5267+ au_lcnt_dec(&br->br_nfiles);
5268+
5269+out_src:
5270+ fput(file[SRC].file);
5271+ br = au_sbr(sb, file[SRC].bindex);
5272+ au_lcnt_dec(&br->br_nfiles);
5273+out:
5274+ return err;
5275+}
5276+
5277+static int au_do_cpup_regular(struct au_cp_generic *cpg,
5278+ struct au_cpup_reg_attr *h_src_attr)
5279+{
5280+ int err, rerr;
5281+ loff_t l;
5282+ struct path h_path;
5283+ struct inode *h_src_inode, *h_dst_inode;
5284+
5285+ err = 0;
5286+ h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
5287+ l = i_size_read(h_src_inode);
5288+ if (cpg->len == -1 || l < cpg->len)
5289+ cpg->len = l;
5290+ if (cpg->len) {
5291+ /* try stopping to update while we are referencing */
5292+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5293+ au_pin_hdir_unlock(cpg->pin);
5294+
5295+ h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5296+ h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
5297+ h_src_attr->iflags = h_src_inode->i_flags;
5298+ if (!au_test_nfs(h_src_inode->i_sb))
5299+ err = vfsub_getattr(&h_path, &h_src_attr->st);
5300+ else {
5301+ inode_unlock_shared(h_src_inode);
5302+ err = vfsub_getattr(&h_path, &h_src_attr->st);
5303+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5304+ }
5305+ if (unlikely(err)) {
5306+ inode_unlock_shared(h_src_inode);
5307+ goto out;
5308+ }
5309+ h_src_attr->valid = 1;
5310+ if (!au_test_nfs(h_src_inode->i_sb)) {
5311+ err = au_cp_regular(cpg);
5312+ inode_unlock_shared(h_src_inode);
5313+ } else {
5314+ inode_unlock_shared(h_src_inode);
5315+ err = au_cp_regular(cpg);
5316+ }
5317+ rerr = au_pin_hdir_relock(cpg->pin);
5318+ if (!err && rerr)
5319+ err = rerr;
5320+ }
5321+ if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5322+ h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5323+ h_dst_inode = d_inode(h_path.dentry);
5324+ spin_lock(&h_dst_inode->i_lock);
5325+ h_dst_inode->i_state |= I_LINKABLE;
5326+ spin_unlock(&h_dst_inode->i_lock);
5327+ }
5328+
5329+out:
5330+ return err;
5331+}
5332+
5333+static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5334+ struct inode *h_dir)
5335+{
5336+ int err;
5337+ DEFINE_DELAYED_CALL(done);
5338+ const char *sym;
5339+
5340+ sym = vfs_get_link(h_src, &done);
5341+ err = PTR_ERR(sym);
5342+ if (IS_ERR(sym))
5343+ goto out;
5344+
5345+ err = vfsub_symlink(h_dir, h_path, sym);
5346+
5347+out:
5348+ do_delayed_call(&done);
5349+ return err;
5350+}
5351+
5352+/*
5353+ * regardless 'acl' option, reset all ACL.
5354+ * All ACL will be copied up later from the original entry on the lower branch.
5355+ */
5356+static int au_reset_acl(struct inode *h_dir, struct path *h_path, umode_t mode)
5357+{
5358+ int err;
5359+ struct dentry *h_dentry;
5360+ struct inode *h_inode;
5361+ struct user_namespace *h_userns;
5362+
5363+ h_userns = mnt_user_ns(h_path->mnt);
5364+ h_dentry = h_path->dentry;
5365+ h_inode = d_inode(h_dentry);
5366+ /* forget_all_cached_acls(h_inode)); */
5367+ err = vfsub_removexattr(h_userns, h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
5368+ AuTraceErr(err);
5369+ if (err == -EOPNOTSUPP)
5370+ err = 0;
5371+ if (!err)
5372+ err = vfsub_acl_chmod(h_userns, h_inode, mode);
5373+
5374+ AuTraceErr(err);
5375+ return err;
5376+}
5377+
5378+static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5379+ struct inode *h_dir, struct path *h_path)
5380+{
5381+ int err;
5382+ struct inode *dir, *inode;
5383+ struct user_namespace *h_userns;
5384+
5385+ h_userns = mnt_user_ns(h_path->mnt);
5386+ err = vfsub_removexattr(h_userns, h_path->dentry,
5387+ XATTR_NAME_POSIX_ACL_DEFAULT);
5388+ AuTraceErr(err);
5389+ if (err == -EOPNOTSUPP)
5390+ err = 0;
5391+ if (unlikely(err))
5392+ goto out;
5393+
5394+ /*
5395+ * strange behaviour from the users view,
5396+ * particularly setattr case
5397+ */
5398+ dir = d_inode(dst_parent);
5399+ if (au_ibtop(dir) == cpg->bdst)
5400+ au_cpup_attr_nlink(dir, /*force*/1);
5401+ inode = d_inode(cpg->dentry);
5402+ au_cpup_attr_nlink(inode, /*force*/1);
5403+
5404+out:
5405+ return err;
5406+}
5407+
5408+static noinline_for_stack
5409+int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
5410+ struct au_cpup_reg_attr *h_src_attr)
5411+{
5412+ int err;
5413+ umode_t mode;
5414+ unsigned int mnt_flags;
5415+ unsigned char isdir, isreg, force;
5416+ const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
5417+ struct au_dtime dt;
5418+ struct path h_path;
5419+ struct dentry *h_src, *h_dst, *h_parent;
5420+ struct inode *h_inode, *h_dir;
5421+ struct super_block *sb;
5422+
5423+ /* bsrc branch can be ro/rw. */
5424+ h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5425+ h_inode = d_inode(h_src);
5426+ AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
5427+
5428+ /* try stopping to be referenced while we are creating */
5429+ h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5430+ if (au_ftest_cpup(cpg->flags, RENAME))
5431+ AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5432+ AUFS_WH_PFX_LEN));
5433+ h_parent = h_dst->d_parent; /* dir inode is locked */
5434+ h_dir = d_inode(h_parent);
5435+ IMustLock(h_dir);
5436+ AuDebugOn(h_parent != h_dst->d_parent);
5437+
5438+ sb = cpg->dentry->d_sb;
5439+ h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
5440+ if (do_dt) {
5441+ h_path.dentry = h_parent;
5442+ au_dtime_store(&dt, dst_parent, &h_path);
5443+ }
5444+ h_path.dentry = h_dst;
5445+
5446+ isreg = 0;
5447+ isdir = 0;
5448+ mode = h_inode->i_mode;
5449+ switch (mode & S_IFMT) {
5450+ case S_IFREG:
5451+ isreg = 1;
5452+ err = vfsub_create(h_dir, &h_path, 0600, /*want_excl*/true);
5453+ if (!err)
5454+ err = au_do_cpup_regular(cpg, h_src_attr);
5455+ break;
5456+ case S_IFDIR:
5457+ isdir = 1;
5458+ err = vfsub_mkdir(h_dir, &h_path, mode);
5459+ if (!err)
5460+ err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
5461+ break;
5462+ case S_IFLNK:
5463+ err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5464+ break;
5465+ case S_IFCHR:
5466+ case S_IFBLK:
5467+ AuDebugOn(!capable(CAP_MKNOD));
5468+ fallthrough;
5469+ case S_IFIFO:
5470+ case S_IFSOCK:
5471+ err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5472+ break;
5473+ default:
5474+ AuIOErr("Unknown inode type 0%o\n", mode);
5475+ err = -EIO;
5476+ }
5477+ if (!err)
5478+ err = au_reset_acl(h_dir, &h_path, mode);
5479+
5480+ mnt_flags = au_mntflags(sb);
5481+ if (!au_opt_test(mnt_flags, UDBA_NONE)
5482+ && !isdir
5483+ && au_opt_test(mnt_flags, XINO)
5484+ && (h_inode->i_nlink == 1
5485+ || (h_inode->i_state & I_LINKABLE))
5486+ /* todo: unnecessary? */
5487+ /* && d_inode(cpg->dentry)->i_nlink == 1 */
5488+ && cpg->bdst < cpg->bsrc
5489+ && !au_ftest_cpup(cpg->flags, KEEPLINO))
5490+ au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
5491+ /* ignore this error */
5492+
5493+ if (!err) {
5494+ force = 0;
5495+ if (isreg) {
5496+ force = !!cpg->len;
5497+ if (cpg->len == -1)
5498+ force = !!i_size_read(h_inode);
5499+ }
5500+ au_fhsm_wrote(sb, cpg->bdst, force);
5501+ }
5502+
5503+ if (do_dt)
5504+ au_dtime_revert(&dt);
5505+ return err;
5506+}
5507+
5508+static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
5509+{
5510+ int err;
5511+ struct dentry *dentry, *h_dentry, *h_parent, *parent;
5512+ struct path h_ppath;
5513+ struct inode *h_dir;
5514+ aufs_bindex_t bdst;
5515+
5516+ dentry = cpg->dentry;
5517+ bdst = cpg->bdst;
5518+ h_ppath.mnt = au_sbr_mnt(dentry->d_sb, bdst);
5519+ h_dentry = au_h_dptr(dentry, bdst);
5520+ if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5521+ dget(h_dentry);
5522+ au_set_h_dptr(dentry, bdst, NULL);
5523+ err = au_lkup_neg(dentry, bdst, /*wh*/0);
5524+ if (!err)
5525+ h_path->dentry = dget(au_h_dptr(dentry, bdst));
5526+ au_set_h_dptr(dentry, bdst, h_dentry);
5527+ } else {
5528+ err = 0;
5529+ parent = dget_parent(dentry);
5530+ h_ppath.dentry = au_h_dptr(parent, bdst);
5531+ dput(parent);
5532+ h_path->dentry = vfsub_lkup_one(&dentry->d_name, &h_ppath);
5533+ if (IS_ERR(h_path->dentry))
5534+ err = PTR_ERR(h_path->dentry);
5535+ }
5536+ if (unlikely(err))
5537+ goto out;
5538+
5539+ h_parent = h_dentry->d_parent; /* dir inode is locked */
5540+ h_dir = d_inode(h_parent);
5541+ IMustLock(h_dir);
5542+ AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5543+ /* no delegation since it is just created */
5544+ err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
5545+ /*flags*/0);
5546+ dput(h_path->dentry);
5547+
5548+out:
5549+ return err;
5550+}
5551+
5552+/*
5553+ * copyup the @dentry from @bsrc to @bdst.
5554+ * the caller must set the both of lower dentries.
5555+ * @len is for truncating when it is -1 copyup the entire file.
5556+ * in link/rename cases, @dst_parent may be different from the real one.
5557+ * basic->bsrc can be larger than basic->bdst.
5558+ * aufs doesn't touch the credential so
5559+ * security_inode_copy_up{,_xattr}() are unnecessary.
5560+ */
5561+static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5562+{
5563+ int err, rerr;
5564+ aufs_bindex_t old_ibtop;
5565+ unsigned char isdir, plink;
5566+ struct dentry *h_src, *h_dst, *h_parent;
5567+ struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
5568+ struct super_block *sb;
5569+ struct au_branch *br;
5570+ struct path h_src_path;
5571+ /* to reduce stack size */
5572+ struct {
5573+ struct au_dtime dt;
5574+ struct path h_path;
5575+ struct au_cpup_reg_attr h_src_attr;
5576+ } *a;
5577+
5578+ err = -ENOMEM;
5579+ a = kmalloc(sizeof(*a), GFP_NOFS);
5580+ if (unlikely(!a))
5581+ goto out;
5582+ a->h_src_attr.valid = 0;
5583+
5584+ sb = cpg->dentry->d_sb;
5585+ br = au_sbr(sb, cpg->bdst);
5586+ a->h_path.mnt = au_br_mnt(br);
5587+ h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5588+ h_parent = h_dst->d_parent; /* dir inode is locked */
5589+ h_dir = d_inode(h_parent);
5590+ IMustLock(h_dir);
5591+
5592+ h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5593+ inode = d_inode(cpg->dentry);
5594+
5595+ if (!dst_parent)
5596+ dst_parent = dget_parent(cpg->dentry);
5597+ else
5598+ dget(dst_parent);
5599+
5600+ plink = !!au_opt_test(au_mntflags(sb), PLINK);
5601+ dst_inode = au_h_iptr(inode, cpg->bdst);
5602+ if (dst_inode) {
5603+ if (unlikely(!plink)) {
5604+ err = -EIO;
5605+ AuIOErr("hi%lu(i%lu) exists on b%d "
5606+ "but plink is disabled\n",
5607+ dst_inode->i_ino, inode->i_ino, cpg->bdst);
5608+ goto out_parent;
5609+ }
5610+
5611+ if (dst_inode->i_nlink) {
5612+ const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
5613+
5614+ h_src = au_plink_lkup(inode, cpg->bdst);
5615+ err = PTR_ERR(h_src);
5616+ if (IS_ERR(h_src))
5617+ goto out_parent;
5618+ if (unlikely(d_is_negative(h_src))) {
5619+ err = -EIO;
5620+ AuIOErr("i%lu exists on b%d "
5621+ "but not pseudo-linked\n",
5622+ inode->i_ino, cpg->bdst);
5623+ dput(h_src);
5624+ goto out_parent;
5625+ }
5626+
5627+ if (do_dt) {
5628+ a->h_path.dentry = h_parent;
5629+ au_dtime_store(&a->dt, dst_parent, &a->h_path);
5630+ }
5631+
5632+ a->h_path.dentry = h_dst;
5633+ delegated = NULL;
5634+ err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
5635+ if (!err && au_ftest_cpup(cpg->flags, RENAME))
5636+ err = au_do_ren_after_cpup(cpg, &a->h_path);
5637+ if (do_dt)
5638+ au_dtime_revert(&a->dt);
5639+ if (unlikely(err == -EWOULDBLOCK)) {
5640+ pr_warn("cannot retry for NFSv4 delegation"
5641+ " for an internal link\n");
5642+ iput(delegated);
5643+ }
5644+ dput(h_src);
5645+ goto out_parent;
5646+ } else
5647+ /* todo: cpup_wh_file? */
5648+ /* udba work */
5649+ au_update_ibrange(inode, /*do_put_zero*/1);
5650+ }
5651+
5652+ isdir = S_ISDIR(inode->i_mode);
5653+ old_ibtop = au_ibtop(inode);
5654+ err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
5655+ if (unlikely(err))
5656+ goto out_rev;
5657+ dst_inode = d_inode(h_dst);
5658+ inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
5659+ /* todo: necessary? */
5660+ /* au_pin_hdir_unlock(cpg->pin); */
5661+
5662+ h_src_path.dentry = h_src;
5663+ h_src_path.mnt = au_sbr_mnt(sb, cpg->bsrc);
5664+ err = cpup_iattr(cpg->dentry, cpg->bdst, &h_src_path, &a->h_src_attr);
5665+ if (unlikely(err)) {
5666+ /* todo: necessary? */
5667+ /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
5668+ inode_unlock(dst_inode);
5669+ goto out_rev;
5670+ }
5671+
5672+ if (cpg->bdst < old_ibtop) {
5673+ if (S_ISREG(inode->i_mode)) {
5674+ err = au_dy_iaop(inode, cpg->bdst, dst_inode);
5675+ if (unlikely(err)) {
5676+ /* ignore an error */
5677+ /* au_pin_hdir_relock(cpg->pin); */
5678+ inode_unlock(dst_inode);
5679+ goto out_rev;
5680+ }
5681+ }
5682+ au_set_ibtop(inode, cpg->bdst);
5683+ } else
5684+ au_set_ibbot(inode, cpg->bdst);
5685+ au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
5686+ au_hi_flags(inode, isdir));
5687+
5688+ /* todo: necessary? */
5689+ /* err = au_pin_hdir_relock(cpg->pin); */
5690+ inode_unlock(dst_inode);
5691+ if (unlikely(err))
5692+ goto out_rev;
5693+
5694+ src_inode = d_inode(h_src);
5695+ if (!isdir
5696+ && (src_inode->i_nlink > 1
5697+ || src_inode->i_state & I_LINKABLE)
5698+ && plink)
5699+ au_plink_append(inode, cpg->bdst, h_dst);
5700+
5701+ if (au_ftest_cpup(cpg->flags, RENAME)) {
5702+ a->h_path.dentry = h_dst;
5703+ err = au_do_ren_after_cpup(cpg, &a->h_path);
5704+ }
5705+ if (!err)
5706+ goto out_parent; /* success */
5707+
5708+ /* revert */
5709+out_rev:
5710+ a->h_path.dentry = h_parent;
5711+ au_dtime_store(&a->dt, dst_parent, &a->h_path);
5712+ a->h_path.dentry = h_dst;
5713+ rerr = 0;
5714+ if (d_is_positive(h_dst)) {
5715+ if (!isdir) {
5716+ /* no delegation since it is just created */
5717+ rerr = vfsub_unlink(h_dir, &a->h_path,
5718+ /*delegated*/NULL, /*force*/0);
5719+ } else
5720+ rerr = vfsub_rmdir(h_dir, &a->h_path);
5721+ }
5722+ au_dtime_revert(&a->dt);
5723+ if (rerr) {
5724+ AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5725+ err = -EIO;
5726+ }
5727+out_parent:
5728+ dput(dst_parent);
5729+ au_kfree_rcu(a);
5730+out:
5731+ return err;
5732+}
5733+
5734+#if 0 /* reserved */
5735+struct au_cpup_single_args {
5736+ int *errp;
5737+ struct au_cp_generic *cpg;
5738+ struct dentry *dst_parent;
5739+};
5740+
5741+static void au_call_cpup_single(void *args)
5742+{
5743+ struct au_cpup_single_args *a = args;
5744+
5745+ au_pin_hdir_acquire_nest(a->cpg->pin);
5746+ *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5747+ au_pin_hdir_release(a->cpg->pin);
5748+}
5749+#endif
5750+
5751+/*
5752+ * prevent SIGXFSZ in copy-up.
5753+ * testing CAP_MKNOD is for generic fs,
5754+ * but CAP_FSETID is for xfs only, currently.
5755+ */
5756+static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
5757+{
5758+ int do_sio;
5759+ struct super_block *sb;
5760+ struct inode *h_dir;
5761+
5762+ do_sio = 0;
5763+ sb = au_pinned_parent(pin)->d_sb;
5764+ if (!au_wkq_test()
5765+ && (!au_sbi(sb)->si_plink_maint_pid
5766+ || au_plink_maint(sb, AuLock_NOPLM))) {
5767+ switch (mode & S_IFMT) {
5768+ case S_IFREG:
5769+ /* no condition about RLIMIT_FSIZE and the file size */
5770+ do_sio = 1;
5771+ break;
5772+ case S_IFCHR:
5773+ case S_IFBLK:
5774+ do_sio = !capable(CAP_MKNOD);
5775+ break;
5776+ }
5777+ if (!do_sio)
5778+ do_sio = ((mode & (S_ISUID | S_ISGID))
5779+ && !capable(CAP_FSETID));
5780+ /* this workaround may be removed in the future */
5781+ if (!do_sio) {
5782+ h_dir = au_pinned_h_dir(pin);
5783+ do_sio = h_dir->i_mode & S_ISVTX;
5784+ }
5785+ }
5786+
5787+ return do_sio;
5788+}
5789+
5790+#if 0 /* reserved */
5791+int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5792+{
5793+ int err, wkq_err;
5794+ struct dentry *h_dentry;
5795+
5796+ h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5797+ if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
5798+ err = au_cpup_single(cpg, dst_parent);
5799+ else {
5800+ struct au_cpup_single_args args = {
5801+ .errp = &err,
5802+ .cpg = cpg,
5803+ .dst_parent = dst_parent
5804+ };
5805+ wkq_err = au_wkq_wait(au_call_cpup_single, &args);
5806+ if (unlikely(wkq_err))
5807+ err = wkq_err;
5808+ }
5809+
5810+ return err;
5811+}
5812+#endif
5813+
5814+/*
5815+ * copyup the @dentry from the first active lower branch to @bdst,
5816+ * using au_cpup_single().
5817+ */
5818+static int au_cpup_simple(struct au_cp_generic *cpg)
5819+{
5820+ int err;
5821+ unsigned int flags_orig;
5822+ struct dentry *dentry;
5823+
5824+ AuDebugOn(cpg->bsrc < 0);
5825+
5826+ dentry = cpg->dentry;
5827+ DiMustWriteLock(dentry);
5828+
5829+ err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
5830+ if (!err) {
5831+ flags_orig = cpg->flags;
5832+ au_fset_cpup(cpg->flags, RENAME);
5833+ err = au_cpup_single(cpg, NULL);
5834+ cpg->flags = flags_orig;
5835+ if (!err)
5836+ return 0; /* success */
5837+
5838+ /* revert */
5839+ au_set_h_dptr(dentry, cpg->bdst, NULL);
5840+ au_set_dbtop(dentry, cpg->bsrc);
5841+ }
5842+
5843+ return err;
5844+}
5845+
5846+struct au_cpup_simple_args {
5847+ int *errp;
5848+ struct au_cp_generic *cpg;
5849+};
5850+
5851+static void au_call_cpup_simple(void *args)
5852+{
5853+ struct au_cpup_simple_args *a = args;
5854+
5855+ au_pin_hdir_acquire_nest(a->cpg->pin);
5856+ *a->errp = au_cpup_simple(a->cpg);
5857+ au_pin_hdir_release(a->cpg->pin);
5858+}
5859+
5860+static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
5861+{
5862+ int err, wkq_err;
5863+ struct dentry *dentry, *parent;
5864+ struct file *h_file;
5865+ struct inode *h_dir;
5866+ struct user_namespace *h_userns;
5867+
5868+ dentry = cpg->dentry;
5869+ h_file = NULL;
5870+ if (au_ftest_cpup(cpg->flags, HOPEN)) {
5871+ AuDebugOn(cpg->bsrc < 0);
5872+ h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
5873+ err = PTR_ERR(h_file);
5874+ if (IS_ERR(h_file))
5875+ goto out;
5876+ }
5877+
5878+ parent = dget_parent(dentry);
5879+ h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
5880+ h_userns = au_sbr_userns(dentry->d_sb, cpg->bdst);
5881+ if (!au_test_h_perm_sio(h_userns, h_dir, MAY_EXEC | MAY_WRITE)
5882+ && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
5883+ err = au_cpup_simple(cpg);
5884+ else {
5885+ struct au_cpup_simple_args args = {
5886+ .errp = &err,
5887+ .cpg = cpg
5888+ };
5889+ wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
5890+ if (unlikely(wkq_err))
5891+ err = wkq_err;
5892+ }
5893+
5894+ dput(parent);
5895+ if (h_file)
5896+ au_h_open_post(dentry, cpg->bsrc, h_file);
5897+
5898+out:
5899+ return err;
5900+}
5901+
5902+int au_sio_cpup_simple(struct au_cp_generic *cpg)
5903+{
5904+ aufs_bindex_t bsrc, bbot;
5905+ struct dentry *dentry, *h_dentry;
5906+
5907+ if (cpg->bsrc < 0) {
5908+ dentry = cpg->dentry;
5909+ bbot = au_dbbot(dentry);
5910+ for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
5911+ h_dentry = au_h_dptr(dentry, bsrc);
5912+ if (h_dentry) {
5913+ AuDebugOn(d_is_negative(h_dentry));
5914+ break;
5915+ }
5916+ }
5917+ AuDebugOn(bsrc > bbot);
5918+ cpg->bsrc = bsrc;
5919+ }
5920+ AuDebugOn(cpg->bsrc <= cpg->bdst);
5921+ return au_do_sio_cpup_simple(cpg);
5922+}
5923+
5924+int au_sio_cpdown_simple(struct au_cp_generic *cpg)
5925+{
5926+ AuDebugOn(cpg->bdst <= cpg->bsrc);
5927+ return au_do_sio_cpup_simple(cpg);
5928+}
5929+
5930+/* ---------------------------------------------------------------------- */
5931+
5932+/*
5933+ * copyup the deleted file for writing.
5934+ */
5935+static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
5936+ struct file *file)
5937+{
5938+ int err;
5939+ unsigned int flags_orig;
5940+ aufs_bindex_t bsrc_orig;
5941+ struct au_dinfo *dinfo;
5942+ struct {
5943+ struct au_hdentry *hd;
5944+ struct dentry *h_dentry;
5945+ } hdst, hsrc;
5946+
5947+ dinfo = au_di(cpg->dentry);
5948+ AuRwMustWriteLock(&dinfo->di_rwsem);
5949+
5950+ bsrc_orig = cpg->bsrc;
5951+ cpg->bsrc = dinfo->di_btop;
5952+ hdst.hd = au_hdentry(dinfo, cpg->bdst);
5953+ hdst.h_dentry = hdst.hd->hd_dentry;
5954+ hdst.hd->hd_dentry = wh_dentry;
5955+ dinfo->di_btop = cpg->bdst;
5956+
5957+ hsrc.h_dentry = NULL;
5958+ if (file) {
5959+ hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
5960+ hsrc.h_dentry = hsrc.hd->hd_dentry;
5961+ hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
5962+ }
5963+ flags_orig = cpg->flags;
5964+ cpg->flags = !AuCpup_DTIME;
5965+ err = au_cpup_single(cpg, /*h_parent*/NULL);
5966+ cpg->flags = flags_orig;
5967+ if (file) {
5968+ if (!err)
5969+ err = au_reopen_nondir(file);
5970+ hsrc.hd->hd_dentry = hsrc.h_dentry;
5971+ }
5972+ hdst.hd->hd_dentry = hdst.h_dentry;
5973+ dinfo->di_btop = cpg->bsrc;
5974+ cpg->bsrc = bsrc_orig;
5975+
5976+ return err;
5977+}
5978+
5979+static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
5980+{
5981+ int err;
5982+ aufs_bindex_t bdst;
5983+ struct au_dtime dt;
5984+ struct dentry *dentry, *parent, *h_parent, *wh_dentry;
5985+ struct au_branch *br;
5986+ struct path h_path;
5987+
5988+ dentry = cpg->dentry;
5989+ bdst = cpg->bdst;
5990+ br = au_sbr(dentry->d_sb, bdst);
5991+ parent = dget_parent(dentry);
5992+ h_parent = au_h_dptr(parent, bdst);
5993+ wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
5994+ err = PTR_ERR(wh_dentry);
5995+ if (IS_ERR(wh_dentry))
5996+ goto out;
5997+
5998+ h_path.dentry = h_parent;
5999+ h_path.mnt = au_br_mnt(br);
6000+ au_dtime_store(&dt, parent, &h_path);
6001+ err = au_do_cpup_wh(cpg, wh_dentry, file);
6002+ if (unlikely(err))
6003+ goto out_wh;
6004+
6005+ dget(wh_dentry);
6006+ h_path.dentry = wh_dentry;
6007+ if (!d_is_dir(wh_dentry)) {
6008+ /* no delegation since it is just created */
6009+ err = vfsub_unlink(d_inode(h_parent), &h_path,
6010+ /*delegated*/NULL, /*force*/0);
6011+ } else
6012+ err = vfsub_rmdir(d_inode(h_parent), &h_path);
6013+ if (unlikely(err)) {
6014+ AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
6015+ wh_dentry, err);
6016+ err = -EIO;
6017+ }
6018+ au_dtime_revert(&dt);
6019+ au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
6020+
6021+out_wh:
6022+ dput(wh_dentry);
6023+out:
6024+ dput(parent);
6025+ return err;
6026+}
6027+
6028+struct au_cpup_wh_args {
6029+ int *errp;
6030+ struct au_cp_generic *cpg;
6031+ struct file *file;
6032+};
6033+
6034+static void au_call_cpup_wh(void *args)
6035+{
6036+ struct au_cpup_wh_args *a = args;
6037+
6038+ au_pin_hdir_acquire_nest(a->cpg->pin);
6039+ *a->errp = au_cpup_wh(a->cpg, a->file);
6040+ au_pin_hdir_release(a->cpg->pin);
6041+}
6042+
6043+int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
6044+{
6045+ int err, wkq_err;
6046+ aufs_bindex_t bdst;
6047+ struct dentry *dentry, *parent, *h_orph, *h_parent;
6048+ struct inode *dir, *h_dir, *h_tmpdir;
6049+ struct au_wbr *wbr;
6050+ struct au_pin wh_pin, *pin_orig;
6051+ struct user_namespace *h_userns;
6052+
6053+ dentry = cpg->dentry;
6054+ bdst = cpg->bdst;
6055+ parent = dget_parent(dentry);
6056+ dir = d_inode(parent);
6057+ h_orph = NULL;
6058+ h_parent = NULL;
6059+ h_dir = au_igrab(au_h_iptr(dir, bdst));
6060+ h_tmpdir = h_dir;
6061+ pin_orig = NULL;
6062+ if (!h_dir->i_nlink) {
6063+ wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
6064+ h_orph = wbr->wbr_orph;
6065+
6066+ h_parent = dget(au_h_dptr(parent, bdst));
6067+ au_set_h_dptr(parent, bdst, dget(h_orph));
6068+ h_tmpdir = d_inode(h_orph);
6069+ au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
6070+
6071+ inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
6072+ /* todo: au_h_open_pre()? */
6073+
6074+ pin_orig = cpg->pin;
6075+ au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
6076+ AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
6077+ cpg->pin = &wh_pin;
6078+ }
6079+
6080+ h_userns = au_sbr_userns(dentry->d_sb, bdst);
6081+ if (!au_test_h_perm_sio(h_userns, h_tmpdir, MAY_EXEC | MAY_WRITE)
6082+ && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
6083+ err = au_cpup_wh(cpg, file);
6084+ else {
6085+ struct au_cpup_wh_args args = {
6086+ .errp = &err,
6087+ .cpg = cpg,
6088+ .file = file
6089+ };
6090+ wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
6091+ if (unlikely(wkq_err))
6092+ err = wkq_err;
6093+ }
6094+
6095+ if (h_orph) {
6096+ inode_unlock(h_tmpdir);
6097+ /* todo: au_h_open_post()? */
6098+ au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
6099+ au_set_h_dptr(parent, bdst, h_parent);
6100+ AuDebugOn(!pin_orig);
6101+ cpg->pin = pin_orig;
6102+ }
6103+ iput(h_dir);
6104+ dput(parent);
6105+
6106+ return err;
6107+}
6108+
6109+/* ---------------------------------------------------------------------- */
6110+
6111+/*
6112+ * generic routine for both of copy-up and copy-down.
6113+ */
6114+/* cf. revalidate function in file.c */
6115+int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6116+ int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6117+ struct au_pin *pin,
6118+ struct dentry *h_parent, void *arg),
6119+ void *arg)
6120+{
6121+ int err;
6122+ struct au_pin pin;
6123+ struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
6124+
6125+ err = 0;
6126+ parent = dget_parent(dentry);
6127+ if (IS_ROOT(parent))
6128+ goto out;
6129+
6130+ au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
6131+ au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
6132+
6133+ /* do not use au_dpage */
6134+ real_parent = parent;
6135+ while (1) {
6136+ dput(parent);
6137+ parent = dget_parent(dentry);
6138+ h_parent = au_h_dptr(parent, bdst);
6139+ if (h_parent)
6140+ goto out; /* success */
6141+
6142+ /* find top dir which is necessary to cpup */
6143+ do {
6144+ d = parent;
6145+ dput(parent);
6146+ parent = dget_parent(d);
6147+ di_read_lock_parent3(parent, !AuLock_IR);
6148+ h_parent = au_h_dptr(parent, bdst);
6149+ di_read_unlock(parent, !AuLock_IR);
6150+ } while (!h_parent);
6151+
6152+ if (d != real_parent)
6153+ di_write_lock_child3(d);
6154+
6155+ /* somebody else might create while we were sleeping */
6156+ h_dentry = au_h_dptr(d, bdst);
6157+ if (!h_dentry || d_is_negative(h_dentry)) {
6158+ if (h_dentry)
6159+ au_update_dbtop(d);
6160+
6161+ au_pin_set_dentry(&pin, d);
6162+ err = au_do_pin(&pin);
6163+ if (!err) {
6164+ err = cp(d, bdst, &pin, h_parent, arg);
6165+ au_unpin(&pin);
6166+ }
6167+ }
6168+
6169+ if (d != real_parent)
6170+ di_write_unlock(d);
6171+ if (unlikely(err))
6172+ break;
6173+ }
6174+
6175+out:
6176+ dput(parent);
6177+ return err;
6178+}
6179+
6180+static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
6181+ struct au_pin *pin,
6182+ struct dentry *h_parent __maybe_unused,
6183+ void *arg __maybe_unused)
6184+{
6185+ struct au_cp_generic cpg = {
6186+ .dentry = dentry,
6187+ .bdst = bdst,
6188+ .bsrc = -1,
6189+ .len = 0,
6190+ .pin = pin,
6191+ .flags = AuCpup_DTIME
6192+ };
6193+ return au_sio_cpup_simple(&cpg);
6194+}
6195+
6196+int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6197+{
6198+ return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
6199+}
6200+
6201+int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6202+{
6203+ int err;
6204+ struct dentry *parent;
6205+ struct inode *dir;
6206+
6207+ parent = dget_parent(dentry);
6208+ dir = d_inode(parent);
6209+ err = 0;
6210+ if (au_h_iptr(dir, bdst))
6211+ goto out;
6212+
6213+ di_read_unlock(parent, AuLock_IR);
6214+ di_write_lock_parent(parent);
6215+ /* someone else might change our inode while we were sleeping */
6216+ if (!au_h_iptr(dir, bdst))
6217+ err = au_cpup_dirs(dentry, bdst);
6218+ di_downgrade_lock(parent, AuLock_IR);
6219+
6220+out:
6221+ dput(parent);
6222+ return err;
6223+}
6224diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
6225--- /usr/share/empty/fs/aufs/cpup.h 1970-01-01 01:00:00.000000000 +0100
6226+++ linux/fs/aufs/cpup.h 2021-12-03 15:38:59.933313976 +0100
6227@@ -0,0 +1,100 @@
6228+/* SPDX-License-Identifier: GPL-2.0 */
6229+/*
6230+ * Copyright (C) 2005-2021 Junjiro R. Okajima
6231+ *
6232+ * This program, aufs is free software; you can redistribute it and/or modify
6233+ * it under the terms of the GNU General Public License as published by
6234+ * the Free Software Foundation; either version 2 of the License, or
6235+ * (at your option) any later version.
6236+ *
6237+ * This program is distributed in the hope that it will be useful,
6238+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6239+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6240+ * GNU General Public License for more details.
6241+ *
6242+ * You should have received a copy of the GNU General Public License
6243+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
6244+ */
6245+
6246+/*
6247+ * copy-up/down functions
6248+ */
6249+
6250+#ifndef __AUFS_CPUP_H__
6251+#define __AUFS_CPUP_H__
6252+
6253+#ifdef __KERNEL__
6254+
6255+#include <linux/path.h>
6256+
6257+struct inode;
6258+struct file;
6259+struct au_pin;
6260+
6261+void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
6262+void au_cpup_attr_timesizes(struct inode *inode);
6263+void au_cpup_attr_nlink(struct inode *inode, int force);
6264+void au_cpup_attr_changeable(struct inode *inode);
6265+void au_cpup_igen(struct inode *inode, struct inode *h_inode);
6266+void au_cpup_attr_all(struct inode *inode, int force);
6267+
6268+/* ---------------------------------------------------------------------- */
6269+
6270+struct au_cp_generic {
6271+ struct dentry *dentry;
6272+ aufs_bindex_t bdst, bsrc;
6273+ loff_t len;
6274+ struct au_pin *pin;
6275+ unsigned int flags;
6276+};
6277+
6278+/* cpup flags */
6279+#define AuCpup_DTIME 1 /* do dtime_store/revert */
6280+#define AuCpup_KEEPLINO (1 << 1) /* do not clear the lower xino,
6281+ for link(2) */
6282+#define AuCpup_RENAME (1 << 2) /* rename after cpup */
6283+#define AuCpup_HOPEN (1 << 3) /* call h_open_pre/post() in
6284+ cpup */
6285+#define AuCpup_OVERWRITE (1 << 4) /* allow overwriting the
6286+ existing entry */
6287+#define AuCpup_RWDST (1 << 5) /* force write target even if
6288+ the branch is marked as RO */
6289+
6290+#ifndef CONFIG_AUFS_BR_HFSPLUS
6291+#undef AuCpup_HOPEN
6292+#define AuCpup_HOPEN 0
6293+#endif
6294+
6295+#define au_ftest_cpup(flags, name) ((flags) & AuCpup_##name)
6296+#define au_fset_cpup(flags, name) \
6297+ do { (flags) |= AuCpup_##name; } while (0)
6298+#define au_fclr_cpup(flags, name) \
6299+ do { (flags) &= ~AuCpup_##name; } while (0)
6300+
6301+int au_copy_file(struct file *dst, struct file *src, loff_t len);
6302+int au_sio_cpup_simple(struct au_cp_generic *cpg);
6303+int au_sio_cpdown_simple(struct au_cp_generic *cpg);
6304+int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
6305+
6306+int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6307+ int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6308+ struct au_pin *pin,
6309+ struct dentry *h_parent, void *arg),
6310+ void *arg);
6311+int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6312+int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6313+
6314+/* ---------------------------------------------------------------------- */
6315+
6316+/* keep timestamps when copyup */
6317+struct au_dtime {
6318+ struct dentry *dt_dentry;
6319+ struct path dt_h_path;
6320+ struct timespec64 dt_atime, dt_mtime;
6321+};
6322+void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6323+ struct path *h_path);
6324+void au_dtime_revert(struct au_dtime *dt);
6325+
6326+#endif /* __KERNEL__ */
6327+#endif /* __AUFS_CPUP_H__ */
6328diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
6329--- /usr/share/empty/fs/aufs/dbgaufs.c 1970-01-01 01:00:00.000000000 +0100
6330+++ linux/fs/aufs/dbgaufs.c 2021-12-03 15:38:59.933313976 +0100
6331@@ -0,0 +1,526 @@
6332+// SPDX-License-Identifier: GPL-2.0
6333+/*
6334+ * Copyright (C) 2005-2021 Junjiro R. Okajima
6335+ *
6336+ * This program, aufs is free software; you can redistribute it and/or modify
6337+ * it under the terms of the GNU General Public License as published by
6338+ * the Free Software Foundation; either version 2 of the License, or
6339+ * (at your option) any later version.
6340+ *
6341+ * This program is distributed in the hope that it will be useful,
6342+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6343+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6344+ * GNU General Public License for more details.
6345+ *
6346+ * You should have received a copy of the GNU General Public License
6347+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
6348+ */
6349+
6350+/*
6351+ * debugfs interface
6352+ */
6353+
6354+#include <linux/debugfs.h>
6355+#include "aufs.h"
6356+
6357+#ifndef CONFIG_SYSFS
6358+#error DEBUG_FS depends upon SYSFS
6359+#endif
6360+
6361+static struct dentry *dbgaufs;
6362+static const mode_t dbgaufs_mode = 0444;
6363+
6364+/* 20 is max digits length of ulong 64 */
6365+struct dbgaufs_arg {
6366+ int n;
6367+ char a[20 * 4];
6368+};
6369+
6370+/*
6371+ * common function for all XINO files
6372+ */
6373+static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6374+ struct file *file)
6375+{
6376+ void *p;
6377+
6378+ p = file->private_data;
6379+ if (p) {
6380+ /* this is struct dbgaufs_arg */
6381+ AuDebugOn(!au_kfree_sz_test(p));
6382+ au_kfree_do_rcu(p);
6383+ }
6384+ return 0;
6385+}
6386+
6387+static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt,
6388+ int cnt)
6389+{
6390+ int err;
6391+ struct kstat st;
6392+ struct dbgaufs_arg *p;
6393+
6394+ err = -ENOMEM;
6395+ p = kmalloc(sizeof(*p), GFP_NOFS);
6396+ if (unlikely(!p))
6397+ goto out;
6398+
6399+ err = 0;
6400+ p->n = 0;
6401+ file->private_data = p;
6402+ if (!xf)
6403+ goto out;
6404+
6405+ err = vfsub_getattr(&xf->f_path, &st);
6406+ if (!err) {
6407+ if (do_fcnt)
6408+ p->n = snprintf
6409+ (p->a, sizeof(p->a), "%d, %llux%u %lld\n",
6410+ cnt, st.blocks, st.blksize,
6411+ (long long)st.size);
6412+ else
6413+ p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
6414+ st.blocks, st.blksize,
6415+ (long long)st.size);
6416+ AuDebugOn(p->n >= sizeof(p->a));
6417+ } else {
6418+ p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6419+ err = 0;
6420+ }
6421+
6422+out:
6423+ return err;
6424+}
6425+
6426+static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6427+ size_t count, loff_t *ppos)
6428+{
6429+ struct dbgaufs_arg *p;
6430+
6431+ p = file->private_data;
6432+ return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6433+}
6434+
6435+/* ---------------------------------------------------------------------- */
6436+
6437+struct dbgaufs_plink_arg {
6438+ int n;
6439+ char a[];
6440+};
6441+
6442+static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6443+ struct file *file)
6444+{
6445+ free_page((unsigned long)file->private_data);
6446+ return 0;
6447+}
6448+
6449+static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6450+{
6451+ int err, i, limit;
6452+ unsigned long n, sum;
6453+ struct dbgaufs_plink_arg *p;
6454+ struct au_sbinfo *sbinfo;
6455+ struct super_block *sb;
6456+ struct hlist_bl_head *hbl;
6457+
6458+ err = -ENOMEM;
6459+ p = (void *)get_zeroed_page(GFP_NOFS);
6460+ if (unlikely(!p))
6461+ goto out;
6462+
6463+ err = -EFBIG;
6464+ sbinfo = inode->i_private;
6465+ sb = sbinfo->si_sb;
6466+ si_noflush_read_lock(sb);
6467+ if (au_opt_test(au_mntflags(sb), PLINK)) {
6468+ limit = PAGE_SIZE - sizeof(p->n);
6469+
6470+ /* the number of buckets */
6471+ n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6472+ p->n += n;
6473+ limit -= n;
6474+
6475+ sum = 0;
6476+ for (i = 0, hbl = sbinfo->si_plink; i < AuPlink_NHASH;
6477+ i++, hbl++) {
6478+ n = au_hbl_count(hbl);
6479+ sum += n;
6480+
6481+ n = snprintf(p->a + p->n, limit, "%lu ", n);
6482+ p->n += n;
6483+ limit -= n;
6484+ if (unlikely(limit <= 0))
6485+ goto out_free;
6486+ }
6487+ p->a[p->n - 1] = '\n';
6488+
6489+ /* the sum of plinks */
6490+ n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6491+ p->n += n;
6492+ limit -= n;
6493+ if (unlikely(limit <= 0))
6494+ goto out_free;
6495+ } else {
6496+#define str "1\n0\n0\n"
6497+ p->n = sizeof(str) - 1;
6498+ strcpy(p->a, str);
6499+#undef str
6500+ }
6501+ si_read_unlock(sb);
6502+
6503+ err = 0;
6504+ file->private_data = p;
6505+ goto out; /* success */
6506+
6507+out_free:
6508+ free_page((unsigned long)p);
6509+out:
6510+ return err;
6511+}
6512+
6513+static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6514+ size_t count, loff_t *ppos)
6515+{
6516+ struct dbgaufs_plink_arg *p;
6517+
6518+ p = file->private_data;
6519+ return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6520+}
6521+
6522+static const struct file_operations dbgaufs_plink_fop = {
6523+ .owner = THIS_MODULE,
6524+ .open = dbgaufs_plink_open,
6525+ .release = dbgaufs_plink_release,
6526+ .read = dbgaufs_plink_read
6527+};
6528+
6529+/* ---------------------------------------------------------------------- */
6530+
6531+static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6532+{
6533+ int err;
6534+ struct au_sbinfo *sbinfo;
6535+ struct super_block *sb;
6536+
6537+ sbinfo = inode->i_private;
6538+ sb = sbinfo->si_sb;
6539+ si_noflush_read_lock(sb);
6540+ err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0, /*cnt*/0);
6541+ si_read_unlock(sb);
6542+ return err;
6543+}
6544+
6545+static const struct file_operations dbgaufs_xib_fop = {
6546+ .owner = THIS_MODULE,
6547+ .open = dbgaufs_xib_open,
6548+ .release = dbgaufs_xi_release,
6549+ .read = dbgaufs_xi_read
6550+};
6551+
6552+/* ---------------------------------------------------------------------- */
6553+
6554+#define DbgaufsXi_PREFIX "xi"
6555+
6556+static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6557+{
6558+ int err, idx;
6559+ long l;
6560+ aufs_bindex_t bindex;
6561+ char *p, a[sizeof(DbgaufsXi_PREFIX) + 8];
6562+ struct au_sbinfo *sbinfo;
6563+ struct super_block *sb;
6564+ struct au_xino *xi;
6565+ struct file *xf;
6566+ struct qstr *name;
6567+ struct au_branch *br;
6568+
6569+ err = -ENOENT;
6570+ name = &file->f_path.dentry->d_name;
6571+ if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6572+ || memcmp(name->name, DbgaufsXi_PREFIX,
6573+ sizeof(DbgaufsXi_PREFIX) - 1)))
6574+ goto out;
6575+
6576+ AuDebugOn(name->len >= sizeof(a));
6577+ memcpy(a, name->name, name->len);
6578+ a[name->len] = '\0';
6579+ p = strchr(a, '-');
6580+ if (p)
6581+ *p = '\0';
6582+ err = kstrtol(a + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
6583+ if (unlikely(err))
6584+ goto out;
6585+ bindex = l;
6586+ idx = 0;
6587+ if (p) {
6588+ err = kstrtol(p + 1, 10, &l);
6589+ if (unlikely(err))
6590+ goto out;
6591+ idx = l;
6592+ }
6593+
6594+ err = -ENOENT;
6595+ sbinfo = inode->i_private;
6596+ sb = sbinfo->si_sb;
6597+ si_noflush_read_lock(sb);
6598+ if (unlikely(bindex < 0 || bindex > au_sbbot(sb)))
6599+ goto out_si;
6600+ br = au_sbr(sb, bindex);
6601+ xi = br->br_xino;
6602+ if (unlikely(idx >= xi->xi_nfile))
6603+ goto out_si;
6604+ xf = au_xino_file(xi, idx);
6605+ if (xf)
6606+ err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1,
6607+ au_xino_count(br));
6608+
6609+out_si:
6610+ si_read_unlock(sb);
6611+out:
6612+ AuTraceErr(err);
6613+ return err;
6614+}
6615+
6616+static const struct file_operations dbgaufs_xino_fop = {
6617+ .owner = THIS_MODULE,
6618+ .open = dbgaufs_xino_open,
6619+ .release = dbgaufs_xi_release,
6620+ .read = dbgaufs_xi_read
6621+};
6622+
6623+void dbgaufs_xino_del(struct au_branch *br)
6624+{
6625+ struct dentry *dbgaufs;
6626+
6627+ dbgaufs = br->br_dbgaufs;
6628+ if (!dbgaufs)
6629+ return;
6630+
6631+ br->br_dbgaufs = NULL;
6632+ /* debugfs acquires the parent i_mutex */
6633+ lockdep_off();
6634+ debugfs_remove(dbgaufs);
6635+ lockdep_on();
6636+}
6637+
6638+void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6639+{
6640+ aufs_bindex_t bbot;
6641+ struct au_branch *br;
6642+
6643+ if (!au_sbi(sb)->si_dbgaufs)
6644+ return;
6645+
6646+ bbot = au_sbbot(sb);
6647+ for (; bindex <= bbot; bindex++) {
6648+ br = au_sbr(sb, bindex);
6649+ dbgaufs_xino_del(br);
6650+ }
6651+}
6652+
6653+static void dbgaufs_br_do_add(struct super_block *sb, aufs_bindex_t bindex,
6654+ unsigned int idx, struct dentry *parent,
6655+ struct au_sbinfo *sbinfo)
6656+{
6657+ struct au_branch *br;
6658+ struct dentry *d;
6659+ /* "xi" bindex(5) "-" idx(2) NULL */
6660+ char name[sizeof(DbgaufsXi_PREFIX) + 8];
6661+
6662+ if (!idx)
6663+ snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6664+ else
6665+ snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d-%u",
6666+ bindex, idx);
6667+ br = au_sbr(sb, bindex);
6668+ if (br->br_dbgaufs) {
6669+ struct qstr qstr = QSTR_INIT(name, strlen(name));
6670+
6671+ if (!au_qstreq(&br->br_dbgaufs->d_name, &qstr)) {
6672+ /* debugfs acquires the parent i_mutex */
6673+ lockdep_off();
6674+ d = debugfs_rename(parent, br->br_dbgaufs, parent,
6675+ name);
6676+ lockdep_on();
6677+ if (unlikely(!d))
6678+ pr_warn("failed renaming %pd/%s, ignored.\n",
6679+ parent, name);
6680+ }
6681+ } else {
6682+ lockdep_off();
6683+ br->br_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6684+ sbinfo, &dbgaufs_xino_fop);
6685+ lockdep_on();
6686+ if (unlikely(!br->br_dbgaufs))
6687+ pr_warn("failed creating %pd/%s, ignored.\n",
6688+ parent, name);
6689+ }
6690+}
6691+
6692+static void dbgaufs_br_add(struct super_block *sb, aufs_bindex_t bindex,
6693+ struct dentry *parent, struct au_sbinfo *sbinfo)
6694+{
6695+ struct au_branch *br;
6696+ struct au_xino *xi;
6697+ unsigned int u;
6698+
6699+ br = au_sbr(sb, bindex);
6700+ xi = br->br_xino;
6701+ for (u = 0; u < xi->xi_nfile; u++)
6702+ dbgaufs_br_do_add(sb, bindex, u, parent, sbinfo);
6703+}
6704+
6705+void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown)
6706+{
6707+ struct au_sbinfo *sbinfo;
6708+ struct dentry *parent;
6709+ aufs_bindex_t bbot;
6710+
6711+ if (!au_opt_test(au_mntflags(sb), XINO))
6712+ return;
6713+
6714+ sbinfo = au_sbi(sb);
6715+ parent = sbinfo->si_dbgaufs;
6716+ if (!parent)
6717+ return;
6718+
6719+ bbot = au_sbbot(sb);
6720+ if (topdown)
6721+ for (; bindex <= bbot; bindex++)
6722+ dbgaufs_br_add(sb, bindex, parent, sbinfo);
6723+ else
6724+ for (; bbot >= bindex; bbot--)
6725+ dbgaufs_br_add(sb, bbot, parent, sbinfo);
6726+}
6727+
6728+/* ---------------------------------------------------------------------- */
6729+
6730+#ifdef CONFIG_AUFS_EXPORT
6731+static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6732+{
6733+ int err;
6734+ struct au_sbinfo *sbinfo;
6735+ struct super_block *sb;
6736+
6737+ sbinfo = inode->i_private;
6738+ sb = sbinfo->si_sb;
6739+ si_noflush_read_lock(sb);
6740+ err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0, /*cnt*/0);
6741+ si_read_unlock(sb);
6742+ return err;
6743+}
6744+
6745+static const struct file_operations dbgaufs_xigen_fop = {
6746+ .owner = THIS_MODULE,
6747+ .open = dbgaufs_xigen_open,
6748+ .release = dbgaufs_xi_release,
6749+ .read = dbgaufs_xi_read
6750+};
6751+
6752+static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6753+{
6754+ int err;
6755+
6756+ /*
6757+ * This function is a dynamic '__init' function actually,
6758+ * so the tiny check for si_rwsem is unnecessary.
6759+ */
6760+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6761+
6762+ err = -EIO;
6763+ sbinfo->si_dbgaufs_xigen = debugfs_create_file
6764+ ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6765+ &dbgaufs_xigen_fop);
6766+ if (sbinfo->si_dbgaufs_xigen)
6767+ err = 0;
6768+
6769+ return err;
6770+}
6771+#else
6772+static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6773+{
6774+ return 0;
6775+}
6776+#endif /* CONFIG_AUFS_EXPORT */
6777+
6778+/* ---------------------------------------------------------------------- */
6779+
6780+void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6781+{
6782+ /*
6783+ * This function is a dynamic '__fin' function actually,
6784+ * so the tiny check for si_rwsem is unnecessary.
6785+ */
6786+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6787+
6788+ debugfs_remove_recursive(sbinfo->si_dbgaufs);
6789+ sbinfo->si_dbgaufs = NULL;
6790+}
6791+
6792+int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6793+{
6794+ int err;
6795+ char name[SysaufsSiNameLen];
6796+
6797+ /*
6798+ * This function is a dynamic '__init' function actually,
6799+ * so the tiny check for si_rwsem is unnecessary.
6800+ */
6801+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6802+
6803+ err = -ENOENT;
6804+ if (!dbgaufs) {
6805+ AuErr1("/debug/aufs is uninitialized\n");
6806+ goto out;
6807+ }
6808+
6809+ err = -EIO;
6810+ sysaufs_name(sbinfo, name);
6811+ sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6812+ if (unlikely(!sbinfo->si_dbgaufs))
6813+ goto out;
6814+
6815+ /* regardless plink/noplink option */
6816+ sbinfo->si_dbgaufs_plink = debugfs_create_file
6817+ ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6818+ &dbgaufs_plink_fop);
6819+ if (unlikely(!sbinfo->si_dbgaufs_plink))
6820+ goto out_dir;
6821+
6822+ /* regardless xino/noxino option */
6823+ sbinfo->si_dbgaufs_xib = debugfs_create_file
6824+ ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6825+ &dbgaufs_xib_fop);
6826+ if (unlikely(!sbinfo->si_dbgaufs_xib))
6827+ goto out_dir;
6828+
6829+ err = dbgaufs_xigen_init(sbinfo);
6830+ if (!err)
6831+ goto out; /* success */
6832+
6833+out_dir:
6834+ dbgaufs_si_fin(sbinfo);
6835+out:
6836+ if (unlikely(err))
6837+ pr_err("debugfs/aufs failed\n");
6838+ return err;
6839+}
6840+
6841+/* ---------------------------------------------------------------------- */
6842+
6843+void dbgaufs_fin(void)
6844+{
6845+ debugfs_remove(dbgaufs);
6846+}
6847+
6848+int __init dbgaufs_init(void)
6849+{
6850+ int err;
6851+
6852+ err = -EIO;
6853+ dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
6854+ if (dbgaufs)
6855+ err = 0;
6856+ return err;
6857+}
6858diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
6859--- /usr/share/empty/fs/aufs/dbgaufs.h 1970-01-01 01:00:00.000000000 +0100
6860+++ linux/fs/aufs/dbgaufs.h 2021-12-03 15:38:59.933313976 +0100
6861@@ -0,0 +1,53 @@
6862+/* SPDX-License-Identifier: GPL-2.0 */
6863+/*
6864+ * Copyright (C) 2005-2021 Junjiro R. Okajima
6865+ *
6866+ * This program, aufs is free software; you can redistribute it and/or modify
6867+ * it under the terms of the GNU General Public License as published by
6868+ * the Free Software Foundation; either version 2 of the License, or
6869+ * (at your option) any later version.
6870+ *
6871+ * This program is distributed in the hope that it will be useful,
6872+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6873+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6874+ * GNU General Public License for more details.
6875+ *
6876+ * You should have received a copy of the GNU General Public License
6877+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
6878+ */
6879+
6880+/*
6881+ * debugfs interface
6882+ */
6883+
6884+#ifndef __DBGAUFS_H__
6885+#define __DBGAUFS_H__
6886+
6887+#ifdef __KERNEL__
6888+
6889+struct super_block;
6890+struct au_sbinfo;
6891+struct au_branch;
6892+
6893+#ifdef CONFIG_DEBUG_FS
6894+/* dbgaufs.c */
6895+void dbgaufs_xino_del(struct au_branch *br);
6896+void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
6897+void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown);
6898+void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
6899+int dbgaufs_si_init(struct au_sbinfo *sbinfo);
6900+void dbgaufs_fin(void);
6901+int __init dbgaufs_init(void);
6902+#else
6903+AuStubVoid(dbgaufs_xino_del, struct au_branch *br)
6904+AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
6905+AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex,
6906+ int topdown)
6907+AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
6908+AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
6909+AuStubVoid(dbgaufs_fin, void)
6910+AuStubInt0(__init dbgaufs_init, void)
6911+#endif /* CONFIG_DEBUG_FS */
6912+
6913+#endif /* __KERNEL__ */
6914+#endif /* __DBGAUFS_H__ */
6915diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
6916--- /usr/share/empty/fs/aufs/dcsub.c 1970-01-01 01:00:00.000000000 +0100
6917+++ linux/fs/aufs/dcsub.c 2021-12-03 15:38:59.933313976 +0100
6918@@ -0,0 +1,225 @@
6919+// SPDX-License-Identifier: GPL-2.0
6920+/*
6921+ * Copyright (C) 2005-2021 Junjiro R. Okajima
6922+ *
6923+ * This program, aufs is free software; you can redistribute it and/or modify
6924+ * it under the terms of the GNU General Public License as published by
6925+ * the Free Software Foundation; either version 2 of the License, or
6926+ * (at your option) any later version.
6927+ *
6928+ * This program is distributed in the hope that it will be useful,
6929+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6930+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6931+ * GNU General Public License for more details.
6932+ *
6933+ * You should have received a copy of the GNU General Public License
6934+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
6935+ */
6936+
6937+/*
6938+ * sub-routines for dentry cache
6939+ */
6940+
6941+#include "aufs.h"
6942+
6943+static void au_dpage_free(struct au_dpage *dpage)
6944+{
6945+ int i;
6946+ struct dentry **p;
6947+
6948+ p = dpage->dentries;
6949+ for (i = 0; i < dpage->ndentry; i++)
6950+ dput(*p++);
6951+ free_page((unsigned long)dpage->dentries);
6952+}
6953+
6954+int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
6955+{
6956+ int err;
6957+ void *p;
6958+
6959+ err = -ENOMEM;
6960+ dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
6961+ if (unlikely(!dpages->dpages))
6962+ goto out;
6963+
6964+ p = (void *)__get_free_page(gfp);
6965+ if (unlikely(!p))
6966+ goto out_dpages;
6967+
6968+ dpages->dpages[0].ndentry = 0;
6969+ dpages->dpages[0].dentries = p;
6970+ dpages->ndpage = 1;
6971+ return 0; /* success */
6972+
6973+out_dpages:
6974+ au_kfree_try_rcu(dpages->dpages);
6975+out:
6976+ return err;
6977+}
6978+
6979+void au_dpages_free(struct au_dcsub_pages *dpages)
6980+{
6981+ int i;
6982+ struct au_dpage *p;
6983+
6984+ p = dpages->dpages;
6985+ for (i = 0; i < dpages->ndpage; i++)
6986+ au_dpage_free(p++);
6987+ au_kfree_try_rcu(dpages->dpages);
6988+}
6989+
6990+static int au_dpages_append(struct au_dcsub_pages *dpages,
6991+ struct dentry *dentry, gfp_t gfp)
6992+{
6993+ int err, sz;
6994+ struct au_dpage *dpage;
6995+ void *p;
6996+
6997+ dpage = dpages->dpages + dpages->ndpage - 1;
6998+ sz = PAGE_SIZE / sizeof(dentry);
6999+ if (unlikely(dpage->ndentry >= sz)) {
7000+ AuLabel(new dpage);
7001+ err = -ENOMEM;
7002+ sz = dpages->ndpage * sizeof(*dpages->dpages);
7003+ p = au_kzrealloc(dpages->dpages, sz,
7004+ sz + sizeof(*dpages->dpages), gfp,
7005+ /*may_shrink*/0);
7006+ if (unlikely(!p))
7007+ goto out;
7008+
7009+ dpages->dpages = p;
7010+ dpage = dpages->dpages + dpages->ndpage;
7011+ p = (void *)__get_free_page(gfp);
7012+ if (unlikely(!p))
7013+ goto out;
7014+
7015+ dpage->ndentry = 0;
7016+ dpage->dentries = p;
7017+ dpages->ndpage++;
7018+ }
7019+
7020+ AuDebugOn(au_dcount(dentry) <= 0);
7021+ dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
7022+ return 0; /* success */
7023+
7024+out:
7025+ return err;
7026+}
7027+
7028+/* todo: BAD approach */
7029+/* copied from linux/fs/dcache.c */
7030+enum d_walk_ret {
7031+ D_WALK_CONTINUE,
7032+ D_WALK_QUIT,
7033+ D_WALK_NORETRY,
7034+ D_WALK_SKIP,
7035+};
7036+
7037+extern void d_walk(struct dentry *parent, void *data,
7038+ enum d_walk_ret (*enter)(void *, struct dentry *));
7039+
7040+struct ac_dpages_arg {
7041+ int err;
7042+ struct au_dcsub_pages *dpages;
7043+ struct super_block *sb;
7044+ au_dpages_test test;
7045+ void *arg;
7046+};
7047+
7048+static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
7049+{
7050+ enum d_walk_ret ret;
7051+ struct ac_dpages_arg *arg = _arg;
7052+
7053+ ret = D_WALK_CONTINUE;
7054+ if (dentry->d_sb == arg->sb
7055+ && !IS_ROOT(dentry)
7056+ && au_dcount(dentry) > 0
7057+ && au_di(dentry)
7058+ && (!arg->test || arg->test(dentry, arg->arg))) {
7059+ arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
7060+ if (unlikely(arg->err))
7061+ ret = D_WALK_QUIT;
7062+ }
7063+
7064+ return ret;
7065+}
7066+
7067+int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7068+ au_dpages_test test, void *arg)
7069+{
7070+ struct ac_dpages_arg args = {
7071+ .err = 0,
7072+ .dpages = dpages,
7073+ .sb = root->d_sb,
7074+ .test = test,
7075+ .arg = arg
7076+ };
7077+
7078+ d_walk(root, &args, au_call_dpages_append);
7079+
7080+ return args.err;
7081+}
7082+
7083+int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7084+ int do_include, au_dpages_test test, void *arg)
7085+{
7086+ int err;
7087+
7088+ err = 0;
7089+ write_seqlock(&rename_lock);
7090+ spin_lock(&dentry->d_lock);
7091+ if (do_include
7092+ && au_dcount(dentry) > 0
7093+ && (!test || test(dentry, arg)))
7094+ err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7095+ spin_unlock(&dentry->d_lock);
7096+ if (unlikely(err))
7097+ goto out;
7098+
7099+ /*
7100+ * RCU for vfsmount is unnecessary since this is a traverse in a single
7101+ * mount
7102+ */
7103+ while (!IS_ROOT(dentry)) {
7104+ dentry = dentry->d_parent; /* rename_lock is locked */
7105+ spin_lock(&dentry->d_lock);
7106+ if (au_dcount(dentry) > 0
7107+ && (!test || test(dentry, arg)))
7108+ err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7109+ spin_unlock(&dentry->d_lock);
7110+ if (unlikely(err))
7111+ break;
7112+ }
7113+
7114+out:
7115+ write_sequnlock(&rename_lock);
7116+ return err;
7117+}
7118+
7119+static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
7120+{
7121+ return au_di(dentry) && dentry->d_sb == arg;
7122+}
7123+
7124+int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7125+ struct dentry *dentry, int do_include)
7126+{
7127+ return au_dcsub_pages_rev(dpages, dentry, do_include,
7128+ au_dcsub_dpages_aufs, dentry->d_sb);
7129+}
7130+
7131+int au_test_subdir(struct dentry *d1, struct dentry *d2)
7132+{
7133+ struct path path[2] = {
7134+ {
7135+ .dentry = d1
7136+ },
7137+ {
7138+ .dentry = d2
7139+ }
7140+ };
7141+
7142+ return path_is_under(path + 0, path + 1);
7143+}
7144diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
7145--- /usr/share/empty/fs/aufs/dcsub.h 1970-01-01 01:00:00.000000000 +0100
7146+++ linux/fs/aufs/dcsub.h 2021-12-03 15:38:59.933313976 +0100
7147@@ -0,0 +1,137 @@
7148+/* SPDX-License-Identifier: GPL-2.0 */
7149+/*
7150+ * Copyright (C) 2005-2021 Junjiro R. Okajima
7151+ *
7152+ * This program, aufs is free software; you can redistribute it and/or modify
7153+ * it under the terms of the GNU General Public License as published by
7154+ * the Free Software Foundation; either version 2 of the License, or
7155+ * (at your option) any later version.
7156+ *
7157+ * This program is distributed in the hope that it will be useful,
7158+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7159+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7160+ * GNU General Public License for more details.
7161+ *
7162+ * You should have received a copy of the GNU General Public License
7163+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7164+ */
7165+
7166+/*
7167+ * sub-routines for dentry cache
7168+ */
7169+
7170+#ifndef __AUFS_DCSUB_H__
7171+#define __AUFS_DCSUB_H__
7172+
7173+#ifdef __KERNEL__
7174+
7175+#include <linux/dcache.h>
7176+#include <linux/fs.h>
7177+
7178+struct au_dpage {
7179+ int ndentry;
7180+ struct dentry **dentries;
7181+};
7182+
7183+struct au_dcsub_pages {
7184+ int ndpage;
7185+ struct au_dpage *dpages;
7186+};
7187+
7188+/* ---------------------------------------------------------------------- */
7189+
7190+/* dcsub.c */
7191+int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
7192+void au_dpages_free(struct au_dcsub_pages *dpages);
7193+typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
7194+int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7195+ au_dpages_test test, void *arg);
7196+int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7197+ int do_include, au_dpages_test test, void *arg);
7198+int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7199+ struct dentry *dentry, int do_include);
7200+int au_test_subdir(struct dentry *d1, struct dentry *d2);
7201+
7202+/* ---------------------------------------------------------------------- */
7203+
7204+/*
7205+ * todo: in linux-3.13, several similar (but faster) helpers are added to
7206+ * include/linux/dcache.h. Try them (in the future).
7207+ */
7208+
7209+static inline int au_d_hashed_positive(struct dentry *d)
7210+{
7211+ int err;
7212+ struct inode *inode = d_inode(d);
7213+
7214+ err = 0;
7215+ if (unlikely(d_unhashed(d)
7216+ || d_is_negative(d)
7217+ || !inode->i_nlink))
7218+ err = -ENOENT;
7219+ return err;
7220+}
7221+
7222+static inline int au_d_linkable(struct dentry *d)
7223+{
7224+ int err;
7225+ struct inode *inode = d_inode(d);
7226+
7227+ err = au_d_hashed_positive(d);
7228+ if (err
7229+ && d_is_positive(d)
7230+ && (inode->i_state & I_LINKABLE))
7231+ err = 0;
7232+ return err;
7233+}
7234+
7235+static inline int au_d_alive(struct dentry *d)
7236+{
7237+ int err;
7238+ struct inode *inode;
7239+
7240+ err = 0;
7241+ if (!IS_ROOT(d))
7242+ err = au_d_hashed_positive(d);
7243+ else {
7244+ inode = d_inode(d);
7245+ if (unlikely(d_unlinked(d)
7246+ || d_is_negative(d)
7247+ || !inode->i_nlink))
7248+ err = -ENOENT;
7249+ }
7250+ return err;
7251+}
7252+
7253+static inline int au_alive_dir(struct dentry *d)
7254+{
7255+ int err;
7256+
7257+ err = au_d_alive(d);
7258+ if (unlikely(err || IS_DEADDIR(d_inode(d))))
7259+ err = -ENOENT;
7260+ return err;
7261+}
7262+
7263+static inline int au_qstreq(struct qstr *a, struct qstr *b)
7264+{
7265+ return a->len == b->len
7266+ && !memcmp(a->name, b->name, a->len);
7267+}
7268+
7269+/*
7270+ * by the commit
7271+ * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
7272+ * taking d_lock
7273+ * the type of d_lockref.count became int, but the inlined function d_count()
7274+ * still returns unsigned int.
7275+ * I don't know why. Maybe it is for every d_count() users?
7276+ * Anyway au_dcount() lives on.
7277+ */
7278+static inline int au_dcount(struct dentry *d)
7279+{
7280+ return (int)d_count(d);
7281+}
7282+
7283+#endif /* __KERNEL__ */
7284+#endif /* __AUFS_DCSUB_H__ */
7285diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
7286--- /usr/share/empty/fs/aufs/debug.c 1970-01-01 01:00:00.000000000 +0100
7287+++ linux/fs/aufs/debug.c 2021-12-03 15:38:59.933313976 +0100
7288@@ -0,0 +1,444 @@
7289+// SPDX-License-Identifier: GPL-2.0
7290+/*
7291+ * Copyright (C) 2005-2021 Junjiro R. Okajima
7292+ *
7293+ * This program, aufs is free software; you can redistribute it and/or modify
7294+ * it under the terms of the GNU General Public License as published by
7295+ * the Free Software Foundation; either version 2 of the License, or
7296+ * (at your option) any later version.
7297+ *
7298+ * This program is distributed in the hope that it will be useful,
7299+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7300+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7301+ * GNU General Public License for more details.
7302+ *
7303+ * You should have received a copy of the GNU General Public License
7304+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7305+ */
7306+
7307+/*
7308+ * debug print functions
7309+ */
7310+
7311+#include <linux/iversion.h>
7312+#include "aufs.h"
7313+
7314+/* Returns 0, or -errno. arg is in kp->arg. */
7315+static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
7316+{
7317+ int err, n;
7318+
7319+ err = kstrtoint(val, 0, &n);
7320+ if (!err) {
7321+ if (n > 0)
7322+ au_debug_on();
7323+ else
7324+ au_debug_off();
7325+ }
7326+ return err;
7327+}
7328+
7329+/* Returns length written or -errno. Buffer is 4k (ie. be short!) */
7330+static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
7331+{
7332+ atomic_t *a;
7333+
7334+ a = kp->arg;
7335+ return sprintf(buffer, "%d", atomic_read(a));
7336+}
7337+
7338+static struct kernel_param_ops param_ops_atomic_t = {
7339+ .set = param_atomic_t_set,
7340+ .get = param_atomic_t_get
7341+ /* void (*free)(void *arg) */
7342+};
7343+
7344+atomic_t aufs_debug = ATOMIC_INIT(0);
7345+MODULE_PARM_DESC(debug, "debug print");
7346+module_param_named(debug, aufs_debug, atomic_t, 0664);
7347+
7348+DEFINE_MUTEX(au_dbg_mtx); /* just to serialize the dbg msgs */
7349+char *au_plevel = KERN_DEBUG;
7350+#define dpri(fmt, ...) do { \
7351+ if ((au_plevel \
7352+ && strcmp(au_plevel, KERN_DEBUG)) \
7353+ || au_debug_test()) \
7354+ printk("%s" fmt, au_plevel, ##__VA_ARGS__); \
7355+} while (0)
7356+
7357+/* ---------------------------------------------------------------------- */
7358+
7359+void au_dpri_whlist(struct au_nhash *whlist)
7360+{
7361+ unsigned long ul, n;
7362+ struct hlist_head *head;
7363+ struct au_vdir_wh *pos;
7364+
7365+ n = whlist->nh_num;
7366+ head = whlist->nh_head;
7367+ for (ul = 0; ul < n; ul++) {
7368+ hlist_for_each_entry(pos, head, wh_hash)
7369+ dpri("b%d, %.*s, %d\n",
7370+ pos->wh_bindex,
7371+ pos->wh_str.len, pos->wh_str.name,
7372+ pos->wh_str.len);
7373+ head++;
7374+ }
7375+}
7376+
7377+void au_dpri_vdir(struct au_vdir *vdir)
7378+{
7379+ unsigned long ul;
7380+ union au_vdir_deblk_p p;
7381+ unsigned char *o;
7382+
7383+ if (!vdir || IS_ERR(vdir)) {
7384+ dpri("err %ld\n", PTR_ERR(vdir));
7385+ return;
7386+ }
7387+
7388+ dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %llu\n",
7389+ vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
7390+ vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
7391+ for (ul = 0; ul < vdir->vd_nblk; ul++) {
7392+ p.deblk = vdir->vd_deblk[ul];
7393+ o = p.deblk;
7394+ dpri("[%lu]: %p\n", ul, o);
7395+ }
7396+}
7397+
7398+static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
7399+ struct dentry *wh)
7400+{
7401+ char *n = NULL;
7402+ int l = 0;
7403+
7404+ if (!inode || IS_ERR(inode)) {
7405+ dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7406+ return -1;
7407+ }
7408+
7409+ /* the type of i_blocks depends upon CONFIG_LBDAF */
7410+ BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7411+ && sizeof(inode->i_blocks) != sizeof(u64));
7412+ if (wh) {
7413+ n = (void *)wh->d_name.name;
7414+ l = wh->d_name.len;
7415+ }
7416+
7417+ dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
7418+ " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7419+ bindex, inode,
7420+ inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7421+ atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7422+ i_size_read(inode), (unsigned long long)inode->i_blocks,
7423+ hn, (long long)timespec64_to_ns(&inode->i_ctime) & 0x0ffff,
7424+ inode->i_mapping ? inode->i_mapping->nrpages : 0,
7425+ inode->i_state, inode->i_flags, inode_peek_iversion(inode),
7426+ inode->i_generation,
7427+ l ? ", wh " : "", l, n);
7428+ return 0;
7429+}
7430+
7431+void au_dpri_inode(struct inode *inode)
7432+{
7433+ struct au_iinfo *iinfo;
7434+ struct au_hinode *hi;
7435+ aufs_bindex_t bindex;
7436+ int err, hn;
7437+
7438+ err = do_pri_inode(-1, inode, -1, NULL);
7439+ if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
7440+ return;
7441+
7442+ iinfo = au_ii(inode);
7443+ dpri("i-1: btop %d, bbot %d, gen %d\n",
7444+ iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
7445+ if (iinfo->ii_btop < 0)
7446+ return;
7447+ hn = 0;
7448+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
7449+ hi = au_hinode(iinfo, bindex);
7450+ hn = !!au_hn(hi);
7451+ do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
7452+ }
7453+}
7454+
7455+void au_dpri_dalias(struct inode *inode)
7456+{
7457+ struct dentry *d;
7458+
7459+ spin_lock(&inode->i_lock);
7460+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
7461+ au_dpri_dentry(d);
7462+ spin_unlock(&inode->i_lock);
7463+}
7464+
7465+static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7466+{
7467+ struct dentry *wh = NULL;
7468+ int hn;
7469+ struct inode *inode;
7470+ struct au_iinfo *iinfo;
7471+ struct au_hinode *hi;
7472+
7473+ if (!dentry || IS_ERR(dentry)) {
7474+ dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7475+ return -1;
7476+ }
7477+ /* do not call dget_parent() here */
7478+ /* note: access d_xxx without d_lock */
7479+ dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7480+ bindex, dentry, dentry,
7481+ dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
7482+ au_dcount(dentry), dentry->d_flags,
7483+ d_unhashed(dentry) ? "un" : "");
7484+ hn = -1;
7485+ inode = NULL;
7486+ if (d_is_positive(dentry))
7487+ inode = d_inode(dentry);
7488+ if (inode
7489+ && au_test_aufs(dentry->d_sb)
7490+ && bindex >= 0
7491+ && !au_is_bad_inode(inode)) {
7492+ iinfo = au_ii(inode);
7493+ hi = au_hinode(iinfo, bindex);
7494+ hn = !!au_hn(hi);
7495+ wh = hi->hi_whdentry;
7496+ }
7497+ do_pri_inode(bindex, inode, hn, wh);
7498+ return 0;
7499+}
7500+
7501+void au_dpri_dentry(struct dentry *dentry)
7502+{
7503+ struct au_dinfo *dinfo;
7504+ aufs_bindex_t bindex;
7505+ int err;
7506+
7507+ err = do_pri_dentry(-1, dentry);
7508+ if (err || !au_test_aufs(dentry->d_sb))
7509+ return;
7510+
7511+ dinfo = au_di(dentry);
7512+ if (!dinfo)
7513+ return;
7514+ dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7515+ dinfo->di_btop, dinfo->di_bbot,
7516+ dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7517+ dinfo->di_tmpfile);
7518+ if (dinfo->di_btop < 0)
7519+ return;
7520+ for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
7521+ do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
7522+}
7523+
7524+static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7525+{
7526+ char a[32];
7527+
7528+ if (!file || IS_ERR(file)) {
7529+ dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7530+ return -1;
7531+ }
7532+ a[0] = 0;
7533+ if (bindex < 0
7534+ && !IS_ERR_OR_NULL(file->f_path.dentry)
7535+ && au_test_aufs(file->f_path.dentry->d_sb)
7536+ && au_fi(file))
7537+ snprintf(a, sizeof(a), ", gen %d, mmapped %d",
7538+ au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
7539+ dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
7540+ bindex, file->f_mode, file->f_flags, (long)file_count(file),
7541+ file->f_version, file->f_pos, a);
7542+ if (!IS_ERR_OR_NULL(file->f_path.dentry))
7543+ do_pri_dentry(bindex, file->f_path.dentry);
7544+ return 0;
7545+}
7546+
7547+void au_dpri_file(struct file *file)
7548+{
7549+ struct au_finfo *finfo;
7550+ struct au_fidir *fidir;
7551+ struct au_hfile *hfile;
7552+ aufs_bindex_t bindex;
7553+ int err;
7554+
7555+ err = do_pri_file(-1, file);
7556+ if (err
7557+ || IS_ERR_OR_NULL(file->f_path.dentry)
7558+ || !au_test_aufs(file->f_path.dentry->d_sb))
7559+ return;
7560+
7561+ finfo = au_fi(file);
7562+ if (!finfo)
7563+ return;
7564+ if (finfo->fi_btop < 0)
7565+ return;
7566+ fidir = finfo->fi_hdir;
7567+ if (!fidir)
7568+ do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7569+ else
7570+ for (bindex = finfo->fi_btop;
7571+ bindex >= 0 && bindex <= fidir->fd_bbot;
7572+ bindex++) {
7573+ hfile = fidir->fd_hfile + bindex;
7574+ do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7575+ }
7576+}
7577+
7578+static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7579+{
7580+ struct vfsmount *mnt;
7581+ struct super_block *sb;
7582+
7583+ if (!br || IS_ERR(br))
7584+ goto out;
7585+ mnt = au_br_mnt(br);
7586+ if (!mnt || IS_ERR(mnt))
7587+ goto out;
7588+ sb = mnt->mnt_sb;
7589+ if (!sb || IS_ERR(sb))
7590+ goto out;
7591+
7592+ dpri("s%d: {perm 0x%x, id %d, wbr %p}, "
7593+ "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
7594+ "xino %d\n",
7595+ bindex, br->br_perm, br->br_id, br->br_wbr,
7596+ au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
7597+ sb->s_flags, sb->s_count,
7598+ atomic_read(&sb->s_active),
7599+ !!au_xino_file(br->br_xino, /*idx*/-1));
7600+ return 0;
7601+
7602+out:
7603+ dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7604+ return -1;
7605+}
7606+
7607+void au_dpri_sb(struct super_block *sb)
7608+{
7609+ struct au_sbinfo *sbinfo;
7610+ aufs_bindex_t bindex;
7611+ int err;
7612+ /* to reduce stack size */
7613+ struct {
7614+ struct vfsmount mnt;
7615+ struct au_branch fake;
7616+ } *a;
7617+
7618+ /* this function can be called from magic sysrq */
7619+ a = kzalloc(sizeof(*a), GFP_ATOMIC);
7620+ if (unlikely(!a)) {
7621+ dpri("no memory\n");
7622+ return;
7623+ }
7624+
7625+ a->mnt.mnt_sb = sb;
7626+ a->fake.br_path.mnt = &a->mnt;
7627+ err = do_pri_br(-1, &a->fake);
7628+ au_kfree_rcu(a);
7629+ dpri("dev 0x%x\n", sb->s_dev);
7630+ if (err || !au_test_aufs(sb))
7631+ return;
7632+
7633+ sbinfo = au_sbi(sb);
7634+ if (!sbinfo)
7635+ return;
7636+ dpri("nw %d, gen %u, kobj %d\n",
7637+ atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
7638+ kref_read(&sbinfo->si_kobj.kref));
7639+ for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
7640+ do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7641+}
7642+
7643+/* ---------------------------------------------------------------------- */
7644+
7645+void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7646+{
7647+ struct inode *h_inode, *inode = d_inode(dentry);
7648+ struct dentry *h_dentry;
7649+ aufs_bindex_t bindex, bbot, bi;
7650+
7651+ if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7652+ return;
7653+
7654+ bbot = au_dbbot(dentry);
7655+ bi = au_ibbot(inode);
7656+ if (bi < bbot)
7657+ bbot = bi;
7658+ bindex = au_dbtop(dentry);
7659+ bi = au_ibtop(inode);
7660+ if (bi > bindex)
7661+ bindex = bi;
7662+
7663+ for (; bindex <= bbot; bindex++) {
7664+ h_dentry = au_h_dptr(dentry, bindex);
7665+ if (!h_dentry)
7666+ continue;
7667+ h_inode = au_h_iptr(inode, bindex);
7668+ if (unlikely(h_inode != d_inode(h_dentry))) {
7669+ au_debug_on();
7670+ AuDbg("b%d, %s:%d\n", bindex, func, line);
7671+ AuDbgDentry(dentry);
7672+ AuDbgInode(inode);
7673+ au_debug_off();
7674+ if (au_test_fuse(h_inode->i_sb))
7675+ WARN_ON_ONCE(1);
7676+ else
7677+ BUG();
7678+ }
7679+ }
7680+}
7681+
7682+void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7683+{
7684+ int err, i, j;
7685+ struct au_dcsub_pages dpages;
7686+ struct au_dpage *dpage;
7687+ struct dentry **dentries;
7688+
7689+ err = au_dpages_init(&dpages, GFP_NOFS);
7690+ AuDebugOn(err);
7691+ err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
7692+ AuDebugOn(err);
7693+ for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7694+ dpage = dpages.dpages + i;
7695+ dentries = dpage->dentries;
7696+ for (j = dpage->ndentry - 1; !err && j >= 0; j--)
7697+ AuDebugOn(au_digen_test(dentries[j], sigen));
7698+ }
7699+ au_dpages_free(&dpages);
7700+}
7701+
7702+void au_dbg_verify_kthread(void)
7703+{
7704+ if (au_wkq_test()) {
7705+ au_dbg_blocked();
7706+ /*
7707+ * It may be recursive, but udba=notify between two aufs mounts,
7708+ * where a single ro branch is shared, is not a problem.
7709+ */
7710+ /* WARN_ON(1); */
7711+ }
7712+}
7713+
7714+/* ---------------------------------------------------------------------- */
7715+
7716+int __init au_debug_init(void)
7717+{
7718+ aufs_bindex_t bindex;
7719+ struct au_vdir_destr destr;
7720+
7721+ bindex = -1;
7722+ AuDebugOn(bindex >= 0);
7723+
7724+ destr.len = -1;
7725+ AuDebugOn(destr.len < NAME_MAX);
7726+
7727+#ifdef CONFIG_4KSTACKS
7728+ pr_warn("CONFIG_4KSTACKS is defined.\n");
7729+#endif
7730+
7731+ return 0;
7732+}
7733diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
7734--- /usr/share/empty/fs/aufs/debug.h 1970-01-01 01:00:00.000000000 +0100
7735+++ linux/fs/aufs/debug.h 2021-12-03 15:38:59.933313976 +0100
7736@@ -0,0 +1,226 @@
7737+/* SPDX-License-Identifier: GPL-2.0 */
7738+/*
7739+ * Copyright (C) 2005-2021 Junjiro R. Okajima
7740+ *
7741+ * This program, aufs is free software; you can redistribute it and/or modify
7742+ * it under the terms of the GNU General Public License as published by
7743+ * the Free Software Foundation; either version 2 of the License, or
7744+ * (at your option) any later version.
7745+ *
7746+ * This program is distributed in the hope that it will be useful,
7747+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7748+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7749+ * GNU General Public License for more details.
7750+ *
7751+ * You should have received a copy of the GNU General Public License
7752+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7753+ */
7754+
7755+/*
7756+ * debug print functions
7757+ */
7758+
7759+#ifndef __AUFS_DEBUG_H__
7760+#define __AUFS_DEBUG_H__
7761+
7762+#ifdef __KERNEL__
7763+
7764+#include <linux/atomic.h>
7765+#include <linux/module.h>
7766+#include <linux/kallsyms.h>
7767+#include <linux/sysrq.h>
7768+
7769+#ifdef CONFIG_AUFS_DEBUG
7770+#define AuDebugOn(a) BUG_ON(a)
7771+
7772+/* module parameter */
7773+extern atomic_t aufs_debug;
7774+static inline void au_debug_on(void)
7775+{
7776+ atomic_inc(&aufs_debug);
7777+}
7778+static inline void au_debug_off(void)
7779+{
7780+ atomic_dec_if_positive(&aufs_debug);
7781+}
7782+
7783+static inline int au_debug_test(void)
7784+{
7785+ return atomic_read(&aufs_debug) > 0;
7786+}
7787+#else
7788+#define AuDebugOn(a) do {} while (0)
7789+AuStubVoid(au_debug_on, void)
7790+AuStubVoid(au_debug_off, void)
7791+AuStubInt0(au_debug_test, void)
7792+#endif /* CONFIG_AUFS_DEBUG */
7793+
7794+#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7795+
7796+/* ---------------------------------------------------------------------- */
7797+
7798+/* debug print */
7799+
7800+#define AuDbg(fmt, ...) do { \
7801+ if (au_debug_test()) \
7802+ pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
7803+} while (0)
7804+#define AuLabel(l) AuDbg(#l "\n")
7805+#define AuIOErr(fmt, ...) pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7806+#define AuWarn1(fmt, ...) do { \
7807+ static unsigned char _c; \
7808+ if (!_c++) \
7809+ pr_warn(fmt, ##__VA_ARGS__); \
7810+} while (0)
7811+
7812+#define AuErr1(fmt, ...) do { \
7813+ static unsigned char _c; \
7814+ if (!_c++) \
7815+ pr_err(fmt, ##__VA_ARGS__); \
7816+} while (0)
7817+
7818+#define AuIOErr1(fmt, ...) do { \
7819+ static unsigned char _c; \
7820+ if (!_c++) \
7821+ AuIOErr(fmt, ##__VA_ARGS__); \
7822+} while (0)
7823+
7824+#define AuUnsupportMsg "This operation is not supported." \
7825+ " Please report this application to aufs-users ML."
7826+#define AuUnsupport(fmt, ...) do { \
7827+ pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
7828+ dump_stack(); \
7829+} while (0)
7830+
7831+#define AuTraceErr(e) do { \
7832+ if (unlikely((e) < 0)) \
7833+ AuDbg("err %d\n", (int)(e)); \
7834+} while (0)
7835+
7836+#define AuTraceErrPtr(p) do { \
7837+ if (IS_ERR(p)) \
7838+ AuDbg("err %ld\n", PTR_ERR(p)); \
7839+} while (0)
7840+
7841+/* dirty macros for debug print, use with "%.*s" and caution */
7842+#define AuLNPair(qstr) (qstr)->len, (qstr)->name
7843+
7844+/* ---------------------------------------------------------------------- */
7845+
7846+struct dentry;
7847+#ifdef CONFIG_AUFS_DEBUG
7848+extern struct mutex au_dbg_mtx;
7849+extern char *au_plevel;
7850+struct au_nhash;
7851+void au_dpri_whlist(struct au_nhash *whlist);
7852+struct au_vdir;
7853+void au_dpri_vdir(struct au_vdir *vdir);
7854+struct inode;
7855+void au_dpri_inode(struct inode *inode);
7856+void au_dpri_dalias(struct inode *inode);
7857+void au_dpri_dentry(struct dentry *dentry);
7858+struct file;
7859+void au_dpri_file(struct file *filp);
7860+struct super_block;
7861+void au_dpri_sb(struct super_block *sb);
7862+
7863+#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
7864+void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
7865+void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
7866+void au_dbg_verify_kthread(void);
7867+
7868+int __init au_debug_init(void);
7869+
7870+#define AuDbgWhlist(w) do { \
7871+ mutex_lock(&au_dbg_mtx); \
7872+ AuDbg(#w "\n"); \
7873+ au_dpri_whlist(w); \
7874+ mutex_unlock(&au_dbg_mtx); \
7875+} while (0)
7876+
7877+#define AuDbgVdir(v) do { \
7878+ mutex_lock(&au_dbg_mtx); \
7879+ AuDbg(#v "\n"); \
7880+ au_dpri_vdir(v); \
7881+ mutex_unlock(&au_dbg_mtx); \
7882+} while (0)
7883+
7884+#define AuDbgInode(i) do { \
7885+ mutex_lock(&au_dbg_mtx); \
7886+ AuDbg(#i "\n"); \
7887+ au_dpri_inode(i); \
7888+ mutex_unlock(&au_dbg_mtx); \
7889+} while (0)
7890+
7891+#define AuDbgDAlias(i) do { \
7892+ mutex_lock(&au_dbg_mtx); \
7893+ AuDbg(#i "\n"); \
7894+ au_dpri_dalias(i); \
7895+ mutex_unlock(&au_dbg_mtx); \
7896+} while (0)
7897+
7898+#define AuDbgDentry(d) do { \
7899+ mutex_lock(&au_dbg_mtx); \
7900+ AuDbg(#d "\n"); \
7901+ au_dpri_dentry(d); \
7902+ mutex_unlock(&au_dbg_mtx); \
7903+} while (0)
7904+
7905+#define AuDbgFile(f) do { \
7906+ mutex_lock(&au_dbg_mtx); \
7907+ AuDbg(#f "\n"); \
7908+ au_dpri_file(f); \
7909+ mutex_unlock(&au_dbg_mtx); \
7910+} while (0)
7911+
7912+#define AuDbgSb(sb) do { \
7913+ mutex_lock(&au_dbg_mtx); \
7914+ AuDbg(#sb "\n"); \
7915+ au_dpri_sb(sb); \
7916+ mutex_unlock(&au_dbg_mtx); \
7917+} while (0)
7918+
7919+#define AuDbgSym(addr) do { \
7920+ char sym[KSYM_SYMBOL_LEN]; \
7921+ sprint_symbol(sym, (unsigned long)addr); \
7922+ AuDbg("%s\n", sym); \
7923+} while (0)
7924+#else
7925+AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
7926+AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
7927+AuStubVoid(au_dbg_verify_kthread, void)
7928+AuStubInt0(__init au_debug_init, void)
7929+
7930+#define AuDbgWhlist(w) do {} while (0)
7931+#define AuDbgVdir(v) do {} while (0)
7932+#define AuDbgInode(i) do {} while (0)
7933+#define AuDbgDAlias(i) do {} while (0)
7934+#define AuDbgDentry(d) do {} while (0)
7935+#define AuDbgFile(f) do {} while (0)
7936+#define AuDbgSb(sb) do {} while (0)
7937+#define AuDbgSym(addr) do {} while (0)
7938+#endif /* CONFIG_AUFS_DEBUG */
7939+
7940+/* ---------------------------------------------------------------------- */
7941+
7942+#ifdef CONFIG_AUFS_MAGIC_SYSRQ
7943+int __init au_sysrq_init(void);
7944+void au_sysrq_fin(void);
7945+
7946+#ifdef CONFIG_HW_CONSOLE
7947+#define au_dbg_blocked() do { \
7948+ WARN_ON(1); \
7949+ handle_sysrq('w'); \
7950+} while (0)
7951+#else
7952+AuStubVoid(au_dbg_blocked, void)
7953+#endif
7954+
7955+#else
7956+AuStubInt0(__init au_sysrq_init, void)
7957+AuStubVoid(au_sysrq_fin, void)
7958+AuStubVoid(au_dbg_blocked, void)
7959+#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
7960+
7961+#endif /* __KERNEL__ */
7962+#endif /* __AUFS_DEBUG_H__ */
7963diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
7964--- /usr/share/empty/fs/aufs/dentry.c 1970-01-01 01:00:00.000000000 +0100
7965+++ linux/fs/aufs/dentry.c 2021-12-03 15:40:58.233313963 +0100
7966@@ -0,0 +1,1169 @@
7967+// SPDX-License-Identifier: GPL-2.0
7968+/*
7969+ * Copyright (C) 2005-2021 Junjiro R. Okajima
7970+ *
7971+ * This program, aufs is free software; you can redistribute it and/or modify
7972+ * it under the terms of the GNU General Public License as published by
7973+ * the Free Software Foundation; either version 2 of the License, or
7974+ * (at your option) any later version.
7975+ *
7976+ * This program is distributed in the hope that it will be useful,
7977+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7978+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7979+ * GNU General Public License for more details.
7980+ *
7981+ * You should have received a copy of the GNU General Public License
7982+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7983+ */
7984+
7985+/*
7986+ * lookup and dentry operations
7987+ */
7988+
7989+#include <linux/iversion.h>
7990+#include <linux/namei.h>
7991+#include "aufs.h"
7992+
7993+/*
7994+ * returns positive/negative dentry, NULL or an error.
7995+ * NULL means whiteout-ed or not-found.
7996+ */
7997+static struct dentry*
7998+au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
7999+ aufs_bindex_t bindex, struct au_do_lookup_args *args)
8000+{
8001+ struct dentry *h_dentry;
8002+ struct inode *h_inode;
8003+ struct au_branch *br;
8004+ struct user_namespace *h_userns;
8005+ struct path h_path;
8006+ int wh_found, opq;
8007+ unsigned char wh_able;
8008+ const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
8009+ const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
8010+ IGNORE_PERM);
8011+
8012+ wh_found = 0;
8013+ br = au_sbr(dentry->d_sb, bindex);
8014+ h_path.dentry = h_parent;
8015+ h_path.mnt = au_br_mnt(br);
8016+ h_userns = au_br_userns(br);
8017+ wh_able = !!au_br_whable(br->br_perm);
8018+ if (wh_able)
8019+ wh_found = au_wh_test(h_userns, &h_path, &args->whname,
8020+ ignore_perm);
8021+ h_dentry = ERR_PTR(wh_found);
8022+ if (!wh_found)
8023+ goto real_lookup;
8024+ if (unlikely(wh_found < 0))
8025+ goto out;
8026+
8027+ /* We found a whiteout */
8028+ /* au_set_dbbot(dentry, bindex); */
8029+ au_set_dbwh(dentry, bindex);
8030+ if (!allow_neg)
8031+ return NULL; /* success */
8032+
8033+real_lookup:
8034+ if (!ignore_perm)
8035+ h_dentry = vfsub_lkup_one(args->name, &h_path);
8036+ else
8037+ h_dentry = au_sio_lkup_one(h_userns, args->name, &h_path);
8038+ if (IS_ERR(h_dentry)) {
8039+ if (PTR_ERR(h_dentry) == -ENAMETOOLONG
8040+ && !allow_neg)
8041+ h_dentry = NULL;
8042+ goto out;
8043+ }
8044+
8045+ h_inode = d_inode(h_dentry);
8046+ if (d_is_negative(h_dentry)) {
8047+ if (!allow_neg)
8048+ goto out_neg;
8049+ } else if (wh_found
8050+ || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
8051+ goto out_neg;
8052+ else if (au_ftest_lkup(args->flags, DIRREN)
8053+ /* && h_inode */
8054+ && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
8055+ AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
8056+ (unsigned long long)h_inode->i_ino);
8057+ goto out_neg;
8058+ }
8059+
8060+ if (au_dbbot(dentry) <= bindex)
8061+ au_set_dbbot(dentry, bindex);
8062+ if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
8063+ au_set_dbtop(dentry, bindex);
8064+ au_set_h_dptr(dentry, bindex, h_dentry);
8065+
8066+ if (!d_is_dir(h_dentry)
8067+ || !wh_able
8068+ || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
8069+ goto out; /* success */
8070+
8071+ h_path.dentry = h_dentry;
8072+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
8073+ opq = au_diropq_test(h_userns, &h_path);
8074+ inode_unlock_shared(h_inode);
8075+ if (opq > 0)
8076+ au_set_dbdiropq(dentry, bindex);
8077+ else if (unlikely(opq < 0)) {
8078+ au_set_h_dptr(dentry, bindex, NULL);
8079+ h_dentry = ERR_PTR(opq);
8080+ }
8081+ goto out;
8082+
8083+out_neg:
8084+ dput(h_dentry);
8085+ h_dentry = NULL;
8086+out:
8087+ return h_dentry;
8088+}
8089+
8090+static int au_test_shwh(struct super_block *sb, const struct qstr *name)
8091+{
8092+ if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
8093+ && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
8094+ return -EPERM;
8095+ return 0;
8096+}
8097+
8098+/*
8099+ * returns the number of lower positive dentries,
8100+ * otherwise an error.
8101+ * can be called at unlinking with @type is zero.
8102+ */
8103+int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
8104+ unsigned int flags)
8105+{
8106+ int npositive, err;
8107+ aufs_bindex_t bindex, btail, bdiropq;
8108+ unsigned char isdir, dirperm1, dirren;
8109+ struct au_do_lookup_args args = {
8110+ .flags = flags,
8111+ .name = &dentry->d_name
8112+ };
8113+ struct dentry *parent;
8114+ struct super_block *sb;
8115+
8116+ sb = dentry->d_sb;
8117+ err = au_test_shwh(sb, args.name);
8118+ if (unlikely(err))
8119+ goto out;
8120+
8121+ err = au_wh_name_alloc(&args.whname, args.name);
8122+ if (unlikely(err))
8123+ goto out;
8124+
8125+ isdir = !!d_is_dir(dentry);
8126+ dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
8127+ dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
8128+ if (dirren)
8129+ au_fset_lkup(args.flags, DIRREN);
8130+
8131+ npositive = 0;
8132+ parent = dget_parent(dentry);
8133+ btail = au_dbtaildir(parent);
8134+ for (bindex = btop; bindex <= btail; bindex++) {
8135+ struct dentry *h_parent, *h_dentry;
8136+ struct inode *h_inode, *h_dir;
8137+ struct au_branch *br;
8138+
8139+ h_dentry = au_h_dptr(dentry, bindex);
8140+ if (h_dentry) {
8141+ if (d_is_positive(h_dentry))
8142+ npositive++;
8143+ break;
8144+ }
8145+ h_parent = au_h_dptr(parent, bindex);
8146+ if (!h_parent || !d_is_dir(h_parent))
8147+ continue;
8148+
8149+ if (dirren) {
8150+ /* if the inum matches, then use the prepared name */
8151+ err = au_dr_lkup_name(&args, bindex);
8152+ if (unlikely(err))
8153+ goto out_parent;
8154+ }
8155+
8156+ h_dir = d_inode(h_parent);
8157+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8158+ h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
8159+ inode_unlock_shared(h_dir);
8160+ err = PTR_ERR(h_dentry);
8161+ if (IS_ERR(h_dentry))
8162+ goto out_parent;
8163+ if (h_dentry)
8164+ au_fclr_lkup(args.flags, ALLOW_NEG);
8165+ if (dirperm1)
8166+ au_fset_lkup(args.flags, IGNORE_PERM);
8167+
8168+ if (au_dbwh(dentry) == bindex)
8169+ break;
8170+ if (!h_dentry)
8171+ continue;
8172+ if (d_is_negative(h_dentry))
8173+ continue;
8174+ h_inode = d_inode(h_dentry);
8175+ npositive++;
8176+ if (!args.type)
8177+ args.type = h_inode->i_mode & S_IFMT;
8178+ if (args.type != S_IFDIR)
8179+ break;
8180+ else if (isdir) {
8181+ /* the type of lower may be different */
8182+ bdiropq = au_dbdiropq(dentry);
8183+ if (bdiropq >= 0 && bdiropq <= bindex)
8184+ break;
8185+ }
8186+ br = au_sbr(sb, bindex);
8187+ if (dirren
8188+ && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
8189+ /*add_ent*/NULL)) {
8190+ /* prepare next name to lookup */
8191+ err = au_dr_lkup(&args, dentry, bindex);
8192+ if (unlikely(err))
8193+ goto out_parent;
8194+ }
8195+ }
8196+
8197+ if (npositive) {
8198+ AuLabel(positive);
8199+ au_update_dbtop(dentry);
8200+ }
8201+ err = npositive;
8202+ if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
8203+ && au_dbtop(dentry) < 0)) {
8204+ err = -EIO;
8205+ AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
8206+ dentry, err);
8207+ }
8208+
8209+out_parent:
8210+ dput(parent);
8211+ au_kfree_try_rcu(args.whname.name);
8212+ if (dirren)
8213+ au_dr_lkup_fin(&args);
8214+out:
8215+ return err;
8216+}
8217+
8218+struct dentry *au_sio_lkup_one(struct user_namespace *userns, struct qstr *name,
8219+ struct path *ppath)
8220+{
8221+ struct dentry *dentry;
8222+ int wkq_err;
8223+
8224+ if (!au_test_h_perm_sio(userns, d_inode(ppath->dentry), MAY_EXEC))
8225+ dentry = vfsub_lkup_one(name, ppath);
8226+ else {
8227+ struct vfsub_lkup_one_args args = {
8228+ .errp = &dentry,
8229+ .name = name,
8230+ .ppath = ppath
8231+ };
8232+
8233+ wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
8234+ if (unlikely(wkq_err))
8235+ dentry = ERR_PTR(wkq_err);
8236+ }
8237+
8238+ return dentry;
8239+}
8240+
8241+/*
8242+ * lookup @dentry on @bindex which should be negative.
8243+ */
8244+int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
8245+{
8246+ int err;
8247+ struct dentry *parent, *h_dentry;
8248+ struct au_branch *br;
8249+ struct user_namespace *h_userns;
8250+ struct path h_ppath;
8251+
8252+ parent = dget_parent(dentry);
8253+ br = au_sbr(dentry->d_sb, bindex);
8254+ h_ppath.dentry = au_h_dptr(parent, bindex);
8255+ h_ppath.mnt = au_br_mnt(br);
8256+ h_userns = au_br_userns(br);
8257+ if (wh)
8258+ h_dentry = au_whtmp_lkup(h_ppath.dentry, br, &dentry->d_name);
8259+ else
8260+ h_dentry = au_sio_lkup_one(h_userns, &dentry->d_name, &h_ppath);
8261+ err = PTR_ERR(h_dentry);
8262+ if (IS_ERR(h_dentry))
8263+ goto out;
8264+ if (unlikely(d_is_positive(h_dentry))) {
8265+ err = -EIO;
8266+ AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
8267+ dput(h_dentry);
8268+ goto out;
8269+ }
8270+
8271+ err = 0;
8272+ if (bindex < au_dbtop(dentry))
8273+ au_set_dbtop(dentry, bindex);
8274+ if (au_dbbot(dentry) < bindex)
8275+ au_set_dbbot(dentry, bindex);
8276+ au_set_h_dptr(dentry, bindex, h_dentry);
8277+
8278+out:
8279+ dput(parent);
8280+ return err;
8281+}
8282+
8283+/* ---------------------------------------------------------------------- */
8284+
8285+/* subset of struct inode */
8286+struct au_iattr {
8287+ unsigned long i_ino;
8288+ /* unsigned int i_nlink; */
8289+ kuid_t i_uid;
8290+ kgid_t i_gid;
8291+ u64 i_version;
8292+/*
8293+ loff_t i_size;
8294+ blkcnt_t i_blocks;
8295+*/
8296+ umode_t i_mode;
8297+};
8298+
8299+static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
8300+{
8301+ ia->i_ino = h_inode->i_ino;
8302+ /* ia->i_nlink = h_inode->i_nlink; */
8303+ ia->i_uid = h_inode->i_uid;
8304+ ia->i_gid = h_inode->i_gid;
8305+ ia->i_version = inode_query_iversion(h_inode);
8306+/*
8307+ ia->i_size = h_inode->i_size;
8308+ ia->i_blocks = h_inode->i_blocks;
8309+*/
8310+ ia->i_mode = (h_inode->i_mode & S_IFMT);
8311+}
8312+
8313+static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
8314+{
8315+ return ia->i_ino != h_inode->i_ino
8316+ /* || ia->i_nlink != h_inode->i_nlink */
8317+ || !uid_eq(ia->i_uid, h_inode->i_uid)
8318+ || !gid_eq(ia->i_gid, h_inode->i_gid)
8319+ || !inode_eq_iversion(h_inode, ia->i_version)
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_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
8328+ struct au_branch *br)
8329+{
8330+ int err;
8331+ struct au_iattr ia;
8332+ struct inode *h_inode;
8333+ struct dentry *h_d;
8334+ struct super_block *h_sb;
8335+ struct path h_ppath;
8336+
8337+ err = 0;
8338+ memset(&ia, -1, sizeof(ia));
8339+ h_sb = h_dentry->d_sb;
8340+ h_inode = NULL;
8341+ if (d_is_positive(h_dentry)) {
8342+ h_inode = d_inode(h_dentry);
8343+ au_iattr_save(&ia, h_inode);
8344+ } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
8345+ /* nfs d_revalidate may return 0 for negative dentry */
8346+ /* fuse d_revalidate always return 0 for negative dentry */
8347+ goto out;
8348+
8349+ /* main purpose is namei.c:cached_lookup() and d_revalidate */
8350+ h_ppath.dentry = h_parent;
8351+ h_ppath.mnt = au_br_mnt(br);
8352+ h_d = vfsub_lkup_one(&h_dentry->d_name, &h_ppath);
8353+ err = PTR_ERR(h_d);
8354+ if (IS_ERR(h_d))
8355+ goto out;
8356+
8357+ err = 0;
8358+ if (unlikely(h_d != h_dentry
8359+ || d_inode(h_d) != h_inode
8360+ || (h_inode && au_iattr_test(&ia, h_inode))))
8361+ err = au_busy_or_stale();
8362+ dput(h_d);
8363+
8364+out:
8365+ AuTraceErr(err);
8366+ return err;
8367+}
8368+
8369+int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8370+ struct dentry *h_parent, struct au_branch *br)
8371+{
8372+ int err;
8373+
8374+ err = 0;
8375+ if (udba == AuOpt_UDBA_REVAL
8376+ && !au_test_fs_remote(h_dentry->d_sb)) {
8377+ IMustLock(h_dir);
8378+ err = (d_inode(h_dentry->d_parent) != h_dir);
8379+ } else if (udba != AuOpt_UDBA_NONE)
8380+ err = au_h_verify_dentry(h_dentry, h_parent, br);
8381+
8382+ return err;
8383+}
8384+
8385+/* ---------------------------------------------------------------------- */
8386+
8387+static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
8388+{
8389+ int err;
8390+ aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
8391+ struct au_hdentry tmp, *p, *q;
8392+ struct au_dinfo *dinfo;
8393+ struct super_block *sb;
8394+
8395+ DiMustWriteLock(dentry);
8396+
8397+ sb = dentry->d_sb;
8398+ dinfo = au_di(dentry);
8399+ bbot = dinfo->di_bbot;
8400+ bwh = dinfo->di_bwh;
8401+ bdiropq = dinfo->di_bdiropq;
8402+ bindex = dinfo->di_btop;
8403+ p = au_hdentry(dinfo, bindex);
8404+ for (; bindex <= bbot; bindex++, p++) {
8405+ if (!p->hd_dentry)
8406+ continue;
8407+
8408+ new_bindex = au_br_index(sb, p->hd_id);
8409+ if (new_bindex == bindex)
8410+ continue;
8411+
8412+ if (dinfo->di_bwh == bindex)
8413+ bwh = new_bindex;
8414+ if (dinfo->di_bdiropq == bindex)
8415+ bdiropq = new_bindex;
8416+ if (new_bindex < 0) {
8417+ au_hdput(p);
8418+ p->hd_dentry = NULL;
8419+ continue;
8420+ }
8421+
8422+ /* swap two lower dentries, and loop again */
8423+ q = au_hdentry(dinfo, new_bindex);
8424+ tmp = *q;
8425+ *q = *p;
8426+ *p = tmp;
8427+ if (tmp.hd_dentry) {
8428+ bindex--;
8429+ p--;
8430+ }
8431+ }
8432+
8433+ dinfo->di_bwh = -1;
8434+ if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
8435+ dinfo->di_bwh = bwh;
8436+
8437+ dinfo->di_bdiropq = -1;
8438+ if (bdiropq >= 0
8439+ && bdiropq <= au_sbbot(sb)
8440+ && au_sbr_whable(sb, bdiropq))
8441+ dinfo->di_bdiropq = bdiropq;
8442+
8443+ err = -EIO;
8444+ dinfo->di_btop = -1;
8445+ dinfo->di_bbot = -1;
8446+ bbot = au_dbbot(parent);
8447+ bindex = 0;
8448+ p = au_hdentry(dinfo, bindex);
8449+ for (; bindex <= bbot; bindex++, p++)
8450+ if (p->hd_dentry) {
8451+ dinfo->di_btop = bindex;
8452+ break;
8453+ }
8454+
8455+ if (dinfo->di_btop >= 0) {
8456+ bindex = bbot;
8457+ p = au_hdentry(dinfo, bindex);
8458+ for (; bindex >= 0; bindex--, p--)
8459+ if (p->hd_dentry) {
8460+ dinfo->di_bbot = bindex;
8461+ err = 0;
8462+ break;
8463+ }
8464+ }
8465+
8466+ return err;
8467+}
8468+
8469+static void au_do_hide(struct dentry *dentry)
8470+{
8471+ struct inode *inode;
8472+
8473+ if (d_really_is_positive(dentry)) {
8474+ inode = d_inode(dentry);
8475+ if (!d_is_dir(dentry)) {
8476+ if (inode->i_nlink && !d_unhashed(dentry))
8477+ drop_nlink(inode);
8478+ } else {
8479+ clear_nlink(inode);
8480+ /* stop next lookup */
8481+ inode->i_flags |= S_DEAD;
8482+ }
8483+ smp_mb(); /* necessary? */
8484+ }
8485+ d_drop(dentry);
8486+}
8487+
8488+static int au_hide_children(struct dentry *parent)
8489+{
8490+ int err, i, j, ndentry;
8491+ struct au_dcsub_pages dpages;
8492+ struct au_dpage *dpage;
8493+ struct dentry *dentry;
8494+
8495+ err = au_dpages_init(&dpages, GFP_NOFS);
8496+ if (unlikely(err))
8497+ goto out;
8498+ err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8499+ if (unlikely(err))
8500+ goto out_dpages;
8501+
8502+ /* in reverse order */
8503+ for (i = dpages.ndpage - 1; i >= 0; i--) {
8504+ dpage = dpages.dpages + i;
8505+ ndentry = dpage->ndentry;
8506+ for (j = ndentry - 1; j >= 0; j--) {
8507+ dentry = dpage->dentries[j];
8508+ if (dentry != parent)
8509+ au_do_hide(dentry);
8510+ }
8511+ }
8512+
8513+out_dpages:
8514+ au_dpages_free(&dpages);
8515+out:
8516+ return err;
8517+}
8518+
8519+static void au_hide(struct dentry *dentry)
8520+{
8521+ int err;
8522+
8523+ AuDbgDentry(dentry);
8524+ if (d_is_dir(dentry)) {
8525+ /* shrink_dcache_parent(dentry); */
8526+ err = au_hide_children(dentry);
8527+ if (unlikely(err))
8528+ AuIOErr("%pd, failed hiding children, ignored %d\n",
8529+ dentry, err);
8530+ }
8531+ au_do_hide(dentry);
8532+}
8533+
8534+/*
8535+ * By adding a dirty branch, a cached dentry may be affected in various ways.
8536+ *
8537+ * a dirty branch is added
8538+ * - on the top of layers
8539+ * - in the middle of layers
8540+ * - to the bottom of layers
8541+ *
8542+ * on the added branch there exists
8543+ * - a whiteout
8544+ * - a diropq
8545+ * - a same named entry
8546+ * + exist
8547+ * * negative --> positive
8548+ * * positive --> positive
8549+ * - type is unchanged
8550+ * - type is changed
8551+ * + doesn't exist
8552+ * * negative --> negative
8553+ * * positive --> negative (rejected by au_br_del() for non-dir case)
8554+ * - none
8555+ */
8556+static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8557+ struct au_dinfo *tmp)
8558+{
8559+ int err;
8560+ aufs_bindex_t bindex, bbot;
8561+ struct {
8562+ struct dentry *dentry;
8563+ struct inode *inode;
8564+ mode_t mode;
8565+ } orig_h, tmp_h = {
8566+ .dentry = NULL
8567+ };
8568+ struct au_hdentry *hd;
8569+ struct inode *inode, *h_inode;
8570+ struct dentry *h_dentry;
8571+
8572+ err = 0;
8573+ AuDebugOn(dinfo->di_btop < 0);
8574+ orig_h.mode = 0;
8575+ orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
8576+ orig_h.inode = NULL;
8577+ if (d_is_positive(orig_h.dentry)) {
8578+ orig_h.inode = d_inode(orig_h.dentry);
8579+ orig_h.mode = orig_h.inode->i_mode & S_IFMT;
8580+ }
8581+ if (tmp->di_btop >= 0) {
8582+ tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
8583+ if (d_is_positive(tmp_h.dentry)) {
8584+ tmp_h.inode = d_inode(tmp_h.dentry);
8585+ tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
8586+ }
8587+ }
8588+
8589+ inode = NULL;
8590+ if (d_really_is_positive(dentry))
8591+ inode = d_inode(dentry);
8592+ if (!orig_h.inode) {
8593+ AuDbg("negative originally\n");
8594+ if (inode) {
8595+ au_hide(dentry);
8596+ goto out;
8597+ }
8598+ AuDebugOn(inode);
8599+ AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8600+ AuDebugOn(dinfo->di_bdiropq != -1);
8601+
8602+ if (!tmp_h.inode) {
8603+ AuDbg("negative --> negative\n");
8604+ /* should have only one negative lower */
8605+ if (tmp->di_btop >= 0
8606+ && tmp->di_btop < dinfo->di_btop) {
8607+ AuDebugOn(tmp->di_btop != tmp->di_bbot);
8608+ AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8609+ au_set_h_dptr(dentry, dinfo->di_btop, NULL);
8610+ au_di_cp(dinfo, tmp);
8611+ hd = au_hdentry(tmp, tmp->di_btop);
8612+ au_set_h_dptr(dentry, tmp->di_btop,
8613+ dget(hd->hd_dentry));
8614+ }
8615+ au_dbg_verify_dinode(dentry);
8616+ } else {
8617+ AuDbg("negative --> positive\n");
8618+ /*
8619+ * similar to the behaviour of creating with bypassing
8620+ * aufs.
8621+ * unhash it in order to force an error in the
8622+ * succeeding create operation.
8623+ * we should not set S_DEAD here.
8624+ */
8625+ d_drop(dentry);
8626+ /* au_di_swap(tmp, dinfo); */
8627+ au_dbg_verify_dinode(dentry);
8628+ }
8629+ } else {
8630+ AuDbg("positive originally\n");
8631+ /* inode may be NULL */
8632+ AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8633+ if (!tmp_h.inode) {
8634+ AuDbg("positive --> negative\n");
8635+ /* or bypassing aufs */
8636+ au_hide(dentry);
8637+ if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
8638+ dinfo->di_bwh = tmp->di_bwh;
8639+ if (inode)
8640+ err = au_refresh_hinode_self(inode);
8641+ au_dbg_verify_dinode(dentry);
8642+ } else if (orig_h.mode == tmp_h.mode) {
8643+ AuDbg("positive --> positive, same type\n");
8644+ if (!S_ISDIR(orig_h.mode)
8645+ && dinfo->di_btop > tmp->di_btop) {
8646+ /*
8647+ * similar to the behaviour of removing and
8648+ * creating.
8649+ */
8650+ au_hide(dentry);
8651+ if (inode)
8652+ err = au_refresh_hinode_self(inode);
8653+ au_dbg_verify_dinode(dentry);
8654+ } else {
8655+ /* fill empty slots */
8656+ if (dinfo->di_btop > tmp->di_btop)
8657+ dinfo->di_btop = tmp->di_btop;
8658+ if (dinfo->di_bbot < tmp->di_bbot)
8659+ dinfo->di_bbot = tmp->di_bbot;
8660+ dinfo->di_bwh = tmp->di_bwh;
8661+ dinfo->di_bdiropq = tmp->di_bdiropq;
8662+ bbot = dinfo->di_bbot;
8663+ bindex = tmp->di_btop;
8664+ hd = au_hdentry(tmp, bindex);
8665+ for (; bindex <= bbot; bindex++, hd++) {
8666+ if (au_h_dptr(dentry, bindex))
8667+ continue;
8668+ h_dentry = hd->hd_dentry;
8669+ if (!h_dentry)
8670+ continue;
8671+ AuDebugOn(d_is_negative(h_dentry));
8672+ h_inode = d_inode(h_dentry);
8673+ AuDebugOn(orig_h.mode
8674+ != (h_inode->i_mode
8675+ & S_IFMT));
8676+ au_set_h_dptr(dentry, bindex,
8677+ dget(h_dentry));
8678+ }
8679+ if (inode)
8680+ err = au_refresh_hinode(inode, dentry);
8681+ au_dbg_verify_dinode(dentry);
8682+ }
8683+ } else {
8684+ AuDbg("positive --> positive, different type\n");
8685+ /* similar to the behaviour of removing and creating */
8686+ au_hide(dentry);
8687+ if (inode)
8688+ err = au_refresh_hinode_self(inode);
8689+ au_dbg_verify_dinode(dentry);
8690+ }
8691+ }
8692+
8693+out:
8694+ return err;
8695+}
8696+
8697+void au_refresh_dop(struct dentry *dentry, int force_reval)
8698+{
8699+ const struct dentry_operations *dop
8700+ = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8701+ static const unsigned int mask
8702+ = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8703+
8704+ BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8705+
8706+ if (dentry->d_op == dop)
8707+ return;
8708+
8709+ AuDbg("%pd\n", dentry);
8710+ spin_lock(&dentry->d_lock);
8711+ if (dop == &aufs_dop)
8712+ dentry->d_flags |= mask;
8713+ else
8714+ dentry->d_flags &= ~mask;
8715+ dentry->d_op = dop;
8716+ spin_unlock(&dentry->d_lock);
8717+}
8718+
8719+int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8720+{
8721+ int err, ebrange, nbr;
8722+ unsigned int sigen;
8723+ struct au_dinfo *dinfo, *tmp;
8724+ struct super_block *sb;
8725+ struct inode *inode;
8726+
8727+ DiMustWriteLock(dentry);
8728+ AuDebugOn(IS_ROOT(dentry));
8729+ AuDebugOn(d_really_is_negative(parent));
8730+
8731+ sb = dentry->d_sb;
8732+ sigen = au_sigen(sb);
8733+ err = au_digen_test(parent, sigen);
8734+ if (unlikely(err))
8735+ goto out;
8736+
8737+ nbr = au_sbbot(sb) + 1;
8738+ dinfo = au_di(dentry);
8739+ err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
8740+ if (unlikely(err))
8741+ goto out;
8742+ ebrange = au_dbrange_test(dentry);
8743+ if (!ebrange)
8744+ ebrange = au_do_refresh_hdentry(dentry, parent);
8745+
8746+ if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
8747+ AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
8748+ if (d_really_is_positive(dentry)) {
8749+ inode = d_inode(dentry);
8750+ err = au_refresh_hinode_self(inode);
8751+ }
8752+ au_dbg_verify_dinode(dentry);
8753+ if (!err)
8754+ goto out_dgen; /* success */
8755+ goto out;
8756+ }
8757+
8758+ /* temporary dinfo */
8759+ AuDbgDentry(dentry);
8760+ err = -ENOMEM;
8761+ tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8762+ if (unlikely(!tmp))
8763+ goto out;
8764+ au_di_swap(tmp, dinfo);
8765+ /* returns the number of positive dentries */
8766+ /*
8767+ * if current working dir is removed, it returns an error.
8768+ * but the dentry is legal.
8769+ */
8770+ err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
8771+ AuDbgDentry(dentry);
8772+ au_di_swap(tmp, dinfo);
8773+ if (err == -ENOENT)
8774+ err = 0;
8775+ if (err >= 0) {
8776+ /* compare/refresh by dinfo */
8777+ AuDbgDentry(dentry);
8778+ err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8779+ au_dbg_verify_dinode(dentry);
8780+ AuTraceErr(err);
8781+ }
8782+ au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
8783+ au_rw_write_unlock(&tmp->di_rwsem);
8784+ au_di_free(tmp);
8785+ if (unlikely(err))
8786+ goto out;
8787+
8788+out_dgen:
8789+ au_update_digen(dentry);
8790+out:
8791+ if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
8792+ AuIOErr("failed refreshing %pd, %d\n", dentry, err);
8793+ AuDbgDentry(dentry);
8794+ }
8795+ AuTraceErr(err);
8796+ return err;
8797+}
8798+
8799+static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8800+ struct dentry *dentry, aufs_bindex_t bindex)
8801+{
8802+ int err, valid;
8803+
8804+ err = 0;
8805+ if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8806+ goto out;
8807+
8808+ AuDbg("b%d\n", bindex);
8809+ /*
8810+ * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8811+ * due to whiteout and branch permission.
8812+ */
8813+ flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8814+ | LOOKUP_FOLLOW | LOOKUP_EXCL);
8815+ /* it may return tri-state */
8816+ valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
8817+
8818+ if (unlikely(valid < 0))
8819+ err = valid;
8820+ else if (!valid)
8821+ err = -EINVAL;
8822+
8823+out:
8824+ AuTraceErr(err);
8825+ return err;
8826+}
8827+
8828+/* todo: remove this */
8829+static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8830+ unsigned int flags, int do_udba, int dirren)
8831+{
8832+ int err;
8833+ umode_t mode, h_mode;
8834+ aufs_bindex_t bindex, btail, btop, ibs, ibe;
8835+ unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
8836+ struct inode *h_inode, *h_cached_inode;
8837+ struct dentry *h_dentry;
8838+ struct qstr *name, *h_name;
8839+
8840+ err = 0;
8841+ plus = 0;
8842+ mode = 0;
8843+ ibs = -1;
8844+ ibe = -1;
8845+ unhashed = !!d_unhashed(dentry);
8846+ is_root = !!IS_ROOT(dentry);
8847+ name = &dentry->d_name;
8848+ tmpfile = au_di(dentry)->di_tmpfile;
8849+
8850+ /*
8851+ * Theoretically, REVAL test should be unnecessary in case of
8852+ * {FS,I}NOTIFY.
8853+ * But {fs,i}notify doesn't fire some necessary events,
8854+ * IN_ATTRIB for atime/nlink/pageio
8855+ * Let's do REVAL test too.
8856+ */
8857+ if (do_udba && inode) {
8858+ mode = (inode->i_mode & S_IFMT);
8859+ plus = (inode->i_nlink > 0);
8860+ ibs = au_ibtop(inode);
8861+ ibe = au_ibbot(inode);
8862+ }
8863+
8864+ btop = au_dbtop(dentry);
8865+ btail = btop;
8866+ if (inode && S_ISDIR(inode->i_mode))
8867+ btail = au_dbtaildir(dentry);
8868+ for (bindex = btop; bindex <= btail; bindex++) {
8869+ h_dentry = au_h_dptr(dentry, bindex);
8870+ if (!h_dentry)
8871+ continue;
8872+
8873+ AuDbg("b%d, %pd\n", bindex, h_dentry);
8874+ h_nfs = !!au_test_nfs(h_dentry->d_sb);
8875+ spin_lock(&h_dentry->d_lock);
8876+ h_name = &h_dentry->d_name;
8877+ if (unlikely(do_udba
8878+ && !is_root
8879+ && ((!h_nfs
8880+ && (unhashed != !!d_unhashed(h_dentry)
8881+ || (!tmpfile && !dirren
8882+ && !au_qstreq(name, h_name))
8883+ ))
8884+ || (h_nfs
8885+ && !(flags & LOOKUP_OPEN)
8886+ && (h_dentry->d_flags
8887+ & DCACHE_NFSFS_RENAMED)))
8888+ )) {
8889+ int h_unhashed;
8890+
8891+ h_unhashed = d_unhashed(h_dentry);
8892+ spin_unlock(&h_dentry->d_lock);
8893+ AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
8894+ unhashed, h_unhashed, dentry, h_dentry);
8895+ goto err;
8896+ }
8897+ spin_unlock(&h_dentry->d_lock);
8898+
8899+ err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
8900+ if (unlikely(err))
8901+ /* do not goto err, to keep the errno */
8902+ break;
8903+
8904+ /* todo: plink too? */
8905+ if (!do_udba)
8906+ continue;
8907+
8908+ /* UDBA tests */
8909+ if (unlikely(!!inode != d_is_positive(h_dentry)))
8910+ goto err;
8911+
8912+ h_inode = NULL;
8913+ if (d_is_positive(h_dentry))
8914+ h_inode = d_inode(h_dentry);
8915+ h_plus = plus;
8916+ h_mode = mode;
8917+ h_cached_inode = h_inode;
8918+ if (h_inode) {
8919+ h_mode = (h_inode->i_mode & S_IFMT);
8920+ h_plus = (h_inode->i_nlink > 0);
8921+ }
8922+ if (inode && ibs <= bindex && bindex <= ibe)
8923+ h_cached_inode = au_h_iptr(inode, bindex);
8924+
8925+ if (!h_nfs) {
8926+ if (unlikely(plus != h_plus && !tmpfile))
8927+ goto err;
8928+ } else {
8929+ if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
8930+ && !is_root
8931+ && !IS_ROOT(h_dentry)
8932+ && unhashed != d_unhashed(h_dentry)))
8933+ goto err;
8934+ }
8935+ if (unlikely(mode != h_mode
8936+ || h_cached_inode != h_inode))
8937+ goto err;
8938+ continue;
8939+
8940+err:
8941+ err = -EINVAL;
8942+ break;
8943+ }
8944+
8945+ AuTraceErr(err);
8946+ return err;
8947+}
8948+
8949+/* todo: consolidate with do_refresh() and au_reval_for_attr() */
8950+static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
8951+{
8952+ int err;
8953+ struct dentry *parent;
8954+
8955+ if (!au_digen_test(dentry, sigen))
8956+ return 0;
8957+
8958+ parent = dget_parent(dentry);
8959+ di_read_lock_parent(parent, AuLock_IR);
8960+ AuDebugOn(au_digen_test(parent, sigen));
8961+ au_dbg_verify_gen(parent, sigen);
8962+ err = au_refresh_dentry(dentry, parent);
8963+ di_read_unlock(parent, AuLock_IR);
8964+ dput(parent);
8965+ AuTraceErr(err);
8966+ return err;
8967+}
8968+
8969+int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
8970+{
8971+ int err;
8972+ struct dentry *d, *parent;
8973+
8974+ if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
8975+ return simple_reval_dpath(dentry, sigen);
8976+
8977+ /* slow loop, keep it simple and stupid */
8978+ /* cf: au_cpup_dirs() */
8979+ err = 0;
8980+ parent = NULL;
8981+ while (au_digen_test(dentry, sigen)) {
8982+ d = dentry;
8983+ while (1) {
8984+ dput(parent);
8985+ parent = dget_parent(d);
8986+ if (!au_digen_test(parent, sigen))
8987+ break;
8988+ d = parent;
8989+ }
8990+
8991+ if (d != dentry)
8992+ di_write_lock_child2(d);
8993+
8994+ /* someone might update our dentry while we were sleeping */
8995+ if (au_digen_test(d, sigen)) {
8996+ /*
8997+ * todo: consolidate with simple_reval_dpath(),
8998+ * do_refresh() and au_reval_for_attr().
8999+ */
9000+ di_read_lock_parent(parent, AuLock_IR);
9001+ err = au_refresh_dentry(d, parent);
9002+ di_read_unlock(parent, AuLock_IR);
9003+ }
9004+
9005+ if (d != dentry)
9006+ di_write_unlock(d);
9007+ dput(parent);
9008+ if (unlikely(err))
9009+ break;
9010+ }
9011+
9012+ return err;
9013+}
9014+
9015+/*
9016+ * if valid returns 1, otherwise 0.
9017+ */
9018+static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
9019+{
9020+ int valid, err;
9021+ unsigned int sigen;
9022+ unsigned char do_udba, dirren;
9023+ struct super_block *sb;
9024+ struct inode *inode;
9025+
9026+ /* todo: support rcu-walk? */
9027+ if (flags & LOOKUP_RCU)
9028+ return -ECHILD;
9029+
9030+ valid = 0;
9031+ if (unlikely(!au_di(dentry)))
9032+ goto out;
9033+
9034+ valid = 1;
9035+ sb = dentry->d_sb;
9036+ /*
9037+ * todo: very ugly
9038+ * i_mutex of parent dir may be held,
9039+ * but we should not return 'invalid' due to busy.
9040+ */
9041+ err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
9042+ if (unlikely(err)) {
9043+ valid = err;
9044+ AuTraceErr(err);
9045+ goto out;
9046+ }
9047+ inode = NULL;
9048+ if (d_really_is_positive(dentry))
9049+ inode = d_inode(dentry);
9050+ if (unlikely(inode && au_is_bad_inode(inode))) {
9051+ err = -EINVAL;
9052+ AuTraceErr(err);
9053+ goto out_dgrade;
9054+ }
9055+ if (unlikely(au_dbrange_test(dentry))) {
9056+ err = -EINVAL;
9057+ AuTraceErr(err);
9058+ goto out_dgrade;
9059+ }
9060+
9061+ sigen = au_sigen(sb);
9062+ if (au_digen_test(dentry, sigen)) {
9063+ AuDebugOn(IS_ROOT(dentry));
9064+ err = au_reval_dpath(dentry, sigen);
9065+ if (unlikely(err)) {
9066+ AuTraceErr(err);
9067+ goto out_dgrade;
9068+ }
9069+ }
9070+ di_downgrade_lock(dentry, AuLock_IR);
9071+
9072+ err = -EINVAL;
9073+ if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
9074+ && inode
9075+ && !(inode->i_state && I_LINKABLE)
9076+ && (IS_DEADDIR(inode) || !inode->i_nlink)) {
9077+ AuTraceErr(err);
9078+ goto out_inval;
9079+ }
9080+
9081+ do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
9082+ if (do_udba && inode) {
9083+ aufs_bindex_t btop = au_ibtop(inode);
9084+ struct inode *h_inode;
9085+
9086+ if (btop >= 0) {
9087+ h_inode = au_h_iptr(inode, btop);
9088+ if (h_inode && au_test_higen(inode, h_inode)) {
9089+ AuTraceErr(err);
9090+ goto out_inval;
9091+ }
9092+ }
9093+ }
9094+
9095+ dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
9096+ err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
9097+ if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
9098+ err = -EIO;
9099+ AuDbg("both of real entry and whiteout found, %p, err %d\n",
9100+ dentry, err);
9101+ }
9102+ goto out_inval;
9103+
9104+out_dgrade:
9105+ di_downgrade_lock(dentry, AuLock_IR);
9106+out_inval:
9107+ aufs_read_unlock(dentry, AuLock_IR);
9108+ AuTraceErr(err);
9109+ valid = !err;
9110+out:
9111+ if (!valid) {
9112+ AuDbg("%pd invalid, %d\n", dentry, valid);
9113+ d_drop(dentry);
9114+ }
9115+ return valid;
9116+}
9117+
9118+static void aufs_d_release(struct dentry *dentry)
9119+{
9120+ if (au_di(dentry)) {
9121+ au_di_fin(dentry);
9122+ au_hn_di_reinit(dentry);
9123+ }
9124+}
9125+
9126+const struct dentry_operations aufs_dop = {
9127+ .d_revalidate = aufs_d_revalidate,
9128+ .d_weak_revalidate = aufs_d_revalidate,
9129+ .d_release = aufs_d_release
9130+};
9131+
9132+/* aufs_dop without d_revalidate */
9133+const struct dentry_operations aufs_dop_noreval = {
9134+ .d_release = aufs_d_release
9135+};
9136diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
9137--- /usr/share/empty/fs/aufs/dentry.h 1970-01-01 01:00:00.000000000 +0100
9138+++ linux/fs/aufs/dentry.h 2021-12-03 15:40:58.233313963 +0100
9139@@ -0,0 +1,269 @@
9140+/* SPDX-License-Identifier: GPL-2.0 */
9141+/*
9142+ * Copyright (C) 2005-2021 Junjiro R. Okajima
9143+ *
9144+ * This program, aufs is free software; you can redistribute it and/or modify
9145+ * it under the terms of the GNU General Public License as published by
9146+ * the Free Software Foundation; either version 2 of the License, or
9147+ * (at your option) any later version.
9148+ *
9149+ * This program is distributed in the hope that it will be useful,
9150+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9151+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9152+ * GNU General Public License for more details.
9153+ *
9154+ * You should have received a copy of the GNU General Public License
9155+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
9156+ */
9157+
9158+/*
9159+ * lookup and dentry operations
9160+ */
9161+
9162+#ifndef __AUFS_DENTRY_H__
9163+#define __AUFS_DENTRY_H__
9164+
9165+#ifdef __KERNEL__
9166+
9167+#include <linux/dcache.h>
9168+#include "dirren.h"
9169+#include "rwsem.h"
9170+
9171+struct au_hdentry {
9172+ struct dentry *hd_dentry;
9173+ aufs_bindex_t hd_id;
9174+};
9175+
9176+struct au_dinfo {
9177+ atomic_t di_generation;
9178+
9179+ struct au_rwsem di_rwsem;
9180+ aufs_bindex_t di_btop, di_bbot, di_bwh, di_bdiropq;
9181+ unsigned char di_tmpfile; /* to allow the different name */
9182+ struct au_hdentry *di_hdentry;
9183+ struct rcu_head rcu;
9184+} ____cacheline_aligned_in_smp;
9185+
9186+/* ---------------------------------------------------------------------- */
9187+
9188+/* flags for au_lkup_dentry() */
9189+#define AuLkup_ALLOW_NEG 1
9190+#define AuLkup_IGNORE_PERM (1 << 1)
9191+#define AuLkup_DIRREN (1 << 2)
9192+#define au_ftest_lkup(flags, name) ((flags) & AuLkup_##name)
9193+#define au_fset_lkup(flags, name) \
9194+ do { (flags) |= AuLkup_##name; } while (0)
9195+#define au_fclr_lkup(flags, name) \
9196+ do { (flags) &= ~AuLkup_##name; } while (0)
9197+
9198+#ifndef CONFIG_AUFS_DIRREN
9199+#undef AuLkup_DIRREN
9200+#define AuLkup_DIRREN 0
9201+#endif
9202+
9203+struct au_do_lookup_args {
9204+ unsigned int flags;
9205+ mode_t type;
9206+ struct qstr whname, *name;
9207+ struct au_dr_lookup dirren;
9208+};
9209+
9210+/* ---------------------------------------------------------------------- */
9211+
9212+/* dentry.c */
9213+extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
9214+struct au_branch;
9215+struct dentry *au_sio_lkup_one(struct user_namespace *userns, struct qstr *name,
9216+ struct path *ppath);
9217+int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
9218+ struct dentry *h_parent, struct au_branch *br);
9219+
9220+int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
9221+ unsigned int flags);
9222+int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
9223+int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
9224+int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
9225+void au_refresh_dop(struct dentry *dentry, int force_reval);
9226+
9227+/* dinfo.c */
9228+void au_di_init_once(void *_di);
9229+struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
9230+void au_di_free(struct au_dinfo *dinfo);
9231+void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
9232+void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
9233+int au_di_init(struct dentry *dentry);
9234+void au_di_fin(struct dentry *dentry);
9235+int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
9236+
9237+void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
9238+void di_read_unlock(struct dentry *d, int flags);
9239+void di_downgrade_lock(struct dentry *d, int flags);
9240+void di_write_lock(struct dentry *d, unsigned int lsc);
9241+void di_write_unlock(struct dentry *d);
9242+void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
9243+void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
9244+void di_write_unlock2(struct dentry *d1, struct dentry *d2);
9245+
9246+struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
9247+struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
9248+aufs_bindex_t au_dbtail(struct dentry *dentry);
9249+aufs_bindex_t au_dbtaildir(struct dentry *dentry);
9250+
9251+void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9252+ struct dentry *h_dentry);
9253+int au_digen_test(struct dentry *dentry, unsigned int sigen);
9254+int au_dbrange_test(struct dentry *dentry);
9255+void au_update_digen(struct dentry *dentry);
9256+void au_update_dbrange(struct dentry *dentry, int do_put_zero);
9257+void au_update_dbtop(struct dentry *dentry);
9258+void au_update_dbbot(struct dentry *dentry);
9259+int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
9260+
9261+/* ---------------------------------------------------------------------- */
9262+
9263+static inline struct au_dinfo *au_di(struct dentry *dentry)
9264+{
9265+ return dentry->d_fsdata;
9266+}
9267+
9268+/* ---------------------------------------------------------------------- */
9269+
9270+/* lock subclass for dinfo */
9271+enum {
9272+ AuLsc_DI_CHILD, /* child first */
9273+ AuLsc_DI_CHILD2, /* rename(2), link(2), and cpup at hnotify */
9274+ AuLsc_DI_CHILD3, /* copyup dirs */
9275+ AuLsc_DI_PARENT,
9276+ AuLsc_DI_PARENT2,
9277+ AuLsc_DI_PARENT3,
9278+ AuLsc_DI_TMP /* temp for replacing dinfo */
9279+};
9280+
9281+/*
9282+ * di_read_lock_child, di_write_lock_child,
9283+ * di_read_lock_child2, di_write_lock_child2,
9284+ * di_read_lock_child3, di_write_lock_child3,
9285+ * di_read_lock_parent, di_write_lock_parent,
9286+ * di_read_lock_parent2, di_write_lock_parent2,
9287+ * di_read_lock_parent3, di_write_lock_parent3,
9288+ */
9289+#define AuReadLockFunc(name, lsc) \
9290+static inline void di_read_lock_##name(struct dentry *d, int flags) \
9291+{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
9292+
9293+#define AuWriteLockFunc(name, lsc) \
9294+static inline void di_write_lock_##name(struct dentry *d) \
9295+{ di_write_lock(d, AuLsc_DI_##lsc); }
9296+
9297+#define AuRWLockFuncs(name, lsc) \
9298+ AuReadLockFunc(name, lsc) \
9299+ AuWriteLockFunc(name, lsc)
9300+
9301+AuRWLockFuncs(child, CHILD);
9302+AuRWLockFuncs(child2, CHILD2);
9303+AuRWLockFuncs(child3, CHILD3);
9304+AuRWLockFuncs(parent, PARENT);
9305+AuRWLockFuncs(parent2, PARENT2);
9306+AuRWLockFuncs(parent3, PARENT3);
9307+
9308+#undef AuReadLockFunc
9309+#undef AuWriteLockFunc
9310+#undef AuRWLockFuncs
9311+
9312+#define DiMustNoWaiters(d) AuRwMustNoWaiters(&au_di(d)->di_rwsem)
9313+#define DiMustAnyLock(d) AuRwMustAnyLock(&au_di(d)->di_rwsem)
9314+#define DiMustWriteLock(d) AuRwMustWriteLock(&au_di(d)->di_rwsem)
9315+
9316+/* ---------------------------------------------------------------------- */
9317+
9318+/* todo: memory barrier? */
9319+static inline unsigned int au_digen(struct dentry *d)
9320+{
9321+ return atomic_read(&au_di(d)->di_generation);
9322+}
9323+
9324+static inline void au_h_dentry_init(struct au_hdentry *hdentry)
9325+{
9326+ hdentry->hd_dentry = NULL;
9327+}
9328+
9329+static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
9330+ aufs_bindex_t bindex)
9331+{
9332+ return di->di_hdentry + bindex;
9333+}
9334+
9335+static inline void au_hdput(struct au_hdentry *hd)
9336+{
9337+ if (hd)
9338+ dput(hd->hd_dentry);
9339+}
9340+
9341+static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
9342+{
9343+ DiMustAnyLock(dentry);
9344+ return au_di(dentry)->di_btop;
9345+}
9346+
9347+static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
9348+{
9349+ DiMustAnyLock(dentry);
9350+ return au_di(dentry)->di_bbot;
9351+}
9352+
9353+static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
9354+{
9355+ DiMustAnyLock(dentry);
9356+ return au_di(dentry)->di_bwh;
9357+}
9358+
9359+static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
9360+{
9361+ DiMustAnyLock(dentry);
9362+ return au_di(dentry)->di_bdiropq;
9363+}
9364+
9365+/* todo: hard/soft set? */
9366+static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
9367+{
9368+ DiMustWriteLock(dentry);
9369+ au_di(dentry)->di_btop = bindex;
9370+}
9371+
9372+static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
9373+{
9374+ DiMustWriteLock(dentry);
9375+ au_di(dentry)->di_bbot = bindex;
9376+}
9377+
9378+static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
9379+{
9380+ DiMustWriteLock(dentry);
9381+ /* dbwh can be outside of btop - bbot range */
9382+ au_di(dentry)->di_bwh = bindex;
9383+}
9384+
9385+static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
9386+{
9387+ DiMustWriteLock(dentry);
9388+ au_di(dentry)->di_bdiropq = bindex;
9389+}
9390+
9391+/* ---------------------------------------------------------------------- */
9392+
9393+#ifdef CONFIG_AUFS_HNOTIFY
9394+static inline void au_digen_dec(struct dentry *d)
9395+{
9396+ atomic_dec(&au_di(d)->di_generation);
9397+}
9398+
9399+static inline void au_hn_di_reinit(struct dentry *dentry)
9400+{
9401+ dentry->d_fsdata = NULL;
9402+}
9403+#else
9404+AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
9405+#endif /* CONFIG_AUFS_HNOTIFY */
9406+
9407+#endif /* __KERNEL__ */
9408+#endif /* __AUFS_DENTRY_H__ */
9409diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
9410--- /usr/share/empty/fs/aufs/dinfo.c 1970-01-01 01:00:00.000000000 +0100
9411+++ linux/fs/aufs/dinfo.c 2021-12-03 15:38:59.933313976 +0100
9412@@ -0,0 +1,554 @@
9413+// SPDX-License-Identifier: GPL-2.0
9414+/*
9415+ * Copyright (C) 2005-2021 Junjiro R. Okajima
9416+ *
9417+ * This program, aufs is free software; you can redistribute it and/or modify
9418+ * it under the terms of the GNU General Public License as published by
9419+ * the Free Software Foundation; either version 2 of the License, or
9420+ * (at your option) any later version.
9421+ *
9422+ * This program is distributed in the hope that it will be useful,
9423+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9424+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9425+ * GNU General Public License for more details.
9426+ *
9427+ * You should have received a copy of the GNU General Public License
9428+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
9429+ */
9430+
9431+/*
9432+ * dentry private data
9433+ */
9434+
9435+#include "aufs.h"
9436+
9437+void au_di_init_once(void *_dinfo)
9438+{
9439+ struct au_dinfo *dinfo = _dinfo;
9440+
9441+ au_rw_init(&dinfo->di_rwsem);
9442+}
9443+
9444+struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
9445+{
9446+ struct au_dinfo *dinfo;
9447+ int nbr, i;
9448+
9449+ dinfo = au_cache_alloc_dinfo();
9450+ if (unlikely(!dinfo))
9451+ goto out;
9452+
9453+ nbr = au_sbbot(sb) + 1;
9454+ if (nbr <= 0)
9455+ nbr = 1;
9456+ dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
9457+ if (dinfo->di_hdentry) {
9458+ au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
9459+ dinfo->di_btop = -1;
9460+ dinfo->di_bbot = -1;
9461+ dinfo->di_bwh = -1;
9462+ dinfo->di_bdiropq = -1;
9463+ dinfo->di_tmpfile = 0;
9464+ for (i = 0; i < nbr; i++)
9465+ dinfo->di_hdentry[i].hd_id = -1;
9466+ goto out;
9467+ }
9468+
9469+ au_cache_free_dinfo(dinfo);
9470+ dinfo = NULL;
9471+
9472+out:
9473+ return dinfo;
9474+}
9475+
9476+void au_di_free(struct au_dinfo *dinfo)
9477+{
9478+ struct au_hdentry *p;
9479+ aufs_bindex_t bbot, bindex;
9480+
9481+ /* dentry may not be revalidated */
9482+ bindex = dinfo->di_btop;
9483+ if (bindex >= 0) {
9484+ bbot = dinfo->di_bbot;
9485+ p = au_hdentry(dinfo, bindex);
9486+ while (bindex++ <= bbot)
9487+ au_hdput(p++);
9488+ }
9489+ au_kfree_try_rcu(dinfo->di_hdentry);
9490+ au_cache_free_dinfo(dinfo);
9491+}
9492+
9493+void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9494+{
9495+ struct au_hdentry *p;
9496+ aufs_bindex_t bi;
9497+
9498+ AuRwMustWriteLock(&a->di_rwsem);
9499+ AuRwMustWriteLock(&b->di_rwsem);
9500+
9501+#define DiSwap(v, name) \
9502+ do { \
9503+ v = a->di_##name; \
9504+ a->di_##name = b->di_##name; \
9505+ b->di_##name = v; \
9506+ } while (0)
9507+
9508+ DiSwap(p, hdentry);
9509+ DiSwap(bi, btop);
9510+ DiSwap(bi, bbot);
9511+ DiSwap(bi, bwh);
9512+ DiSwap(bi, bdiropq);
9513+ /* smp_mb(); */
9514+
9515+#undef DiSwap
9516+}
9517+
9518+void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9519+{
9520+ AuRwMustWriteLock(&dst->di_rwsem);
9521+ AuRwMustWriteLock(&src->di_rwsem);
9522+
9523+ dst->di_btop = src->di_btop;
9524+ dst->di_bbot = src->di_bbot;
9525+ dst->di_bwh = src->di_bwh;
9526+ dst->di_bdiropq = src->di_bdiropq;
9527+ /* smp_mb(); */
9528+}
9529+
9530+int au_di_init(struct dentry *dentry)
9531+{
9532+ int err;
9533+ struct super_block *sb;
9534+ struct au_dinfo *dinfo;
9535+
9536+ err = 0;
9537+ sb = dentry->d_sb;
9538+ dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9539+ if (dinfo) {
9540+ atomic_set(&dinfo->di_generation, au_sigen(sb));
9541+ /* smp_mb(); */ /* atomic_set */
9542+ dentry->d_fsdata = dinfo;
9543+ } else
9544+ err = -ENOMEM;
9545+
9546+ return err;
9547+}
9548+
9549+void au_di_fin(struct dentry *dentry)
9550+{
9551+ struct au_dinfo *dinfo;
9552+
9553+ dinfo = au_di(dentry);
9554+ AuRwDestroy(&dinfo->di_rwsem);
9555+ au_di_free(dinfo);
9556+}
9557+
9558+int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
9559+{
9560+ int err, sz;
9561+ struct au_hdentry *hdp;
9562+
9563+ AuRwMustWriteLock(&dinfo->di_rwsem);
9564+
9565+ err = -ENOMEM;
9566+ sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
9567+ if (!sz)
9568+ sz = sizeof(*hdp);
9569+ hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
9570+ may_shrink);
9571+ if (hdp) {
9572+ dinfo->di_hdentry = hdp;
9573+ err = 0;
9574+ }
9575+
9576+ return err;
9577+}
9578+
9579+/* ---------------------------------------------------------------------- */
9580+
9581+static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9582+{
9583+ switch (lsc) {
9584+ case AuLsc_DI_CHILD:
9585+ ii_write_lock_child(inode);
9586+ break;
9587+ case AuLsc_DI_CHILD2:
9588+ ii_write_lock_child2(inode);
9589+ break;
9590+ case AuLsc_DI_CHILD3:
9591+ ii_write_lock_child3(inode);
9592+ break;
9593+ case AuLsc_DI_PARENT:
9594+ ii_write_lock_parent(inode);
9595+ break;
9596+ case AuLsc_DI_PARENT2:
9597+ ii_write_lock_parent2(inode);
9598+ break;
9599+ case AuLsc_DI_PARENT3:
9600+ ii_write_lock_parent3(inode);
9601+ break;
9602+ default:
9603+ BUG();
9604+ }
9605+}
9606+
9607+static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9608+{
9609+ switch (lsc) {
9610+ case AuLsc_DI_CHILD:
9611+ ii_read_lock_child(inode);
9612+ break;
9613+ case AuLsc_DI_CHILD2:
9614+ ii_read_lock_child2(inode);
9615+ break;
9616+ case AuLsc_DI_CHILD3:
9617+ ii_read_lock_child3(inode);
9618+ break;
9619+ case AuLsc_DI_PARENT:
9620+ ii_read_lock_parent(inode);
9621+ break;
9622+ case AuLsc_DI_PARENT2:
9623+ ii_read_lock_parent2(inode);
9624+ break;
9625+ case AuLsc_DI_PARENT3:
9626+ ii_read_lock_parent3(inode);
9627+ break;
9628+ default:
9629+ BUG();
9630+ }
9631+}
9632+
9633+void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9634+{
9635+ struct inode *inode;
9636+
9637+ au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
9638+ if (d_really_is_positive(d)) {
9639+ inode = d_inode(d);
9640+ if (au_ftest_lock(flags, IW))
9641+ do_ii_write_lock(inode, lsc);
9642+ else if (au_ftest_lock(flags, IR))
9643+ do_ii_read_lock(inode, lsc);
9644+ }
9645+}
9646+
9647+void di_read_unlock(struct dentry *d, int flags)
9648+{
9649+ struct inode *inode;
9650+
9651+ if (d_really_is_positive(d)) {
9652+ inode = d_inode(d);
9653+ if (au_ftest_lock(flags, IW)) {
9654+ au_dbg_verify_dinode(d);
9655+ ii_write_unlock(inode);
9656+ } else if (au_ftest_lock(flags, IR)) {
9657+ au_dbg_verify_dinode(d);
9658+ ii_read_unlock(inode);
9659+ }
9660+ }
9661+ au_rw_read_unlock(&au_di(d)->di_rwsem);
9662+}
9663+
9664+void di_downgrade_lock(struct dentry *d, int flags)
9665+{
9666+ if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9667+ ii_downgrade_lock(d_inode(d));
9668+ au_rw_dgrade_lock(&au_di(d)->di_rwsem);
9669+}
9670+
9671+void di_write_lock(struct dentry *d, unsigned int lsc)
9672+{
9673+ au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
9674+ if (d_really_is_positive(d))
9675+ do_ii_write_lock(d_inode(d), lsc);
9676+}
9677+
9678+void di_write_unlock(struct dentry *d)
9679+{
9680+ au_dbg_verify_dinode(d);
9681+ if (d_really_is_positive(d))
9682+ ii_write_unlock(d_inode(d));
9683+ au_rw_write_unlock(&au_di(d)->di_rwsem);
9684+}
9685+
9686+void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9687+{
9688+ AuDebugOn(d1 == d2
9689+ || d_inode(d1) == d_inode(d2)
9690+ || d1->d_sb != d2->d_sb);
9691+
9692+ if ((isdir && au_test_subdir(d1, d2))
9693+ || d1 < d2) {
9694+ di_write_lock_child(d1);
9695+ di_write_lock_child2(d2);
9696+ } else {
9697+ di_write_lock_child(d2);
9698+ di_write_lock_child2(d1);
9699+ }
9700+}
9701+
9702+void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9703+{
9704+ AuDebugOn(d1 == d2
9705+ || d_inode(d1) == d_inode(d2)
9706+ || d1->d_sb != d2->d_sb);
9707+
9708+ if ((isdir && au_test_subdir(d1, d2))
9709+ || d1 < d2) {
9710+ di_write_lock_parent(d1);
9711+ di_write_lock_parent2(d2);
9712+ } else {
9713+ di_write_lock_parent(d2);
9714+ di_write_lock_parent2(d1);
9715+ }
9716+}
9717+
9718+void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9719+{
9720+ di_write_unlock(d1);
9721+ if (d_inode(d1) == d_inode(d2))
9722+ au_rw_write_unlock(&au_di(d2)->di_rwsem);
9723+ else
9724+ di_write_unlock(d2);
9725+}
9726+
9727+/* ---------------------------------------------------------------------- */
9728+
9729+struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9730+{
9731+ struct dentry *d;
9732+
9733+ DiMustAnyLock(dentry);
9734+
9735+ if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
9736+ return NULL;
9737+ AuDebugOn(bindex < 0);
9738+ d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
9739+ AuDebugOn(d && au_dcount(d) <= 0);
9740+ return d;
9741+}
9742+
9743+/*
9744+ * extended version of au_h_dptr().
9745+ * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9746+ * error.
9747+ */
9748+struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9749+{
9750+ struct dentry *h_dentry;
9751+ struct inode *inode, *h_inode;
9752+
9753+ AuDebugOn(d_really_is_negative(dentry));
9754+
9755+ h_dentry = NULL;
9756+ if (au_dbtop(dentry) <= bindex
9757+ && bindex <= au_dbbot(dentry))
9758+ h_dentry = au_h_dptr(dentry, bindex);
9759+ if (h_dentry && !au_d_linkable(h_dentry)) {
9760+ dget(h_dentry);
9761+ goto out; /* success */
9762+ }
9763+
9764+ inode = d_inode(dentry);
9765+ AuDebugOn(bindex < au_ibtop(inode));
9766+ AuDebugOn(au_ibbot(inode) < bindex);
9767+ h_inode = au_h_iptr(inode, bindex);
9768+ h_dentry = d_find_alias(h_inode);
9769+ if (h_dentry) {
9770+ if (!IS_ERR(h_dentry)) {
9771+ if (!au_d_linkable(h_dentry))
9772+ goto out; /* success */
9773+ dput(h_dentry);
9774+ } else
9775+ goto out;
9776+ }
9777+
9778+ if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9779+ h_dentry = au_plink_lkup(inode, bindex);
9780+ AuDebugOn(!h_dentry);
9781+ if (!IS_ERR(h_dentry)) {
9782+ if (!au_d_hashed_positive(h_dentry))
9783+ goto out; /* success */
9784+ dput(h_dentry);
9785+ h_dentry = NULL;
9786+ }
9787+ }
9788+
9789+out:
9790+ AuDbgDentry(h_dentry);
9791+ return h_dentry;
9792+}
9793+
9794+aufs_bindex_t au_dbtail(struct dentry *dentry)
9795+{
9796+ aufs_bindex_t bbot, bwh;
9797+
9798+ bbot = au_dbbot(dentry);
9799+ if (0 <= bbot) {
9800+ bwh = au_dbwh(dentry);
9801+ if (!bwh)
9802+ return bwh;
9803+ if (0 < bwh && bwh < bbot)
9804+ return bwh - 1;
9805+ }
9806+ return bbot;
9807+}
9808+
9809+aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9810+{
9811+ aufs_bindex_t bbot, bopq;
9812+
9813+ bbot = au_dbtail(dentry);
9814+ if (0 <= bbot) {
9815+ bopq = au_dbdiropq(dentry);
9816+ if (0 <= bopq && bopq < bbot)
9817+ bbot = bopq;
9818+ }
9819+ return bbot;
9820+}
9821+
9822+/* ---------------------------------------------------------------------- */
9823+
9824+void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9825+ struct dentry *h_dentry)
9826+{
9827+ struct au_dinfo *dinfo;
9828+ struct au_hdentry *hd;
9829+ struct au_branch *br;
9830+
9831+ DiMustWriteLock(dentry);
9832+
9833+ dinfo = au_di(dentry);
9834+ hd = au_hdentry(dinfo, bindex);
9835+ au_hdput(hd);
9836+ hd->hd_dentry = h_dentry;
9837+ if (h_dentry) {
9838+ br = au_sbr(dentry->d_sb, bindex);
9839+ hd->hd_id = br->br_id;
9840+ }
9841+}
9842+
9843+int au_dbrange_test(struct dentry *dentry)
9844+{
9845+ int err;
9846+ aufs_bindex_t btop, bbot;
9847+
9848+ err = 0;
9849+ btop = au_dbtop(dentry);
9850+ bbot = au_dbbot(dentry);
9851+ if (btop >= 0)
9852+ AuDebugOn(bbot < 0 && btop > bbot);
9853+ else {
9854+ err = -EIO;
9855+ AuDebugOn(bbot >= 0);
9856+ }
9857+
9858+ return err;
9859+}
9860+
9861+int au_digen_test(struct dentry *dentry, unsigned int sigen)
9862+{
9863+ int err;
9864+
9865+ err = 0;
9866+ if (unlikely(au_digen(dentry) != sigen
9867+ || au_iigen_test(d_inode(dentry), sigen)))
9868+ err = -EIO;
9869+
9870+ return err;
9871+}
9872+
9873+void au_update_digen(struct dentry *dentry)
9874+{
9875+ atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
9876+ /* smp_mb(); */ /* atomic_set */
9877+}
9878+
9879+void au_update_dbrange(struct dentry *dentry, int do_put_zero)
9880+{
9881+ struct au_dinfo *dinfo;
9882+ struct dentry *h_d;
9883+ struct au_hdentry *hdp;
9884+ aufs_bindex_t bindex, bbot;
9885+
9886+ DiMustWriteLock(dentry);
9887+
9888+ dinfo = au_di(dentry);
9889+ if (!dinfo || dinfo->di_btop < 0)
9890+ return;
9891+
9892+ if (do_put_zero) {
9893+ bbot = dinfo->di_bbot;
9894+ bindex = dinfo->di_btop;
9895+ hdp = au_hdentry(dinfo, bindex);
9896+ for (; bindex <= bbot; bindex++, hdp++) {
9897+ h_d = hdp->hd_dentry;
9898+ if (h_d && d_is_negative(h_d))
9899+ au_set_h_dptr(dentry, bindex, NULL);
9900+ }
9901+ }
9902+
9903+ dinfo->di_btop = 0;
9904+ hdp = au_hdentry(dinfo, dinfo->di_btop);
9905+ for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
9906+ if (hdp->hd_dentry)
9907+ break;
9908+ if (dinfo->di_btop > dinfo->di_bbot) {
9909+ dinfo->di_btop = -1;
9910+ dinfo->di_bbot = -1;
9911+ return;
9912+ }
9913+
9914+ hdp = au_hdentry(dinfo, dinfo->di_bbot);
9915+ for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
9916+ if (hdp->hd_dentry)
9917+ break;
9918+ AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
9919+}
9920+
9921+void au_update_dbtop(struct dentry *dentry)
9922+{
9923+ aufs_bindex_t bindex, bbot;
9924+ struct dentry *h_dentry;
9925+
9926+ bbot = au_dbbot(dentry);
9927+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
9928+ h_dentry = au_h_dptr(dentry, bindex);
9929+ if (!h_dentry)
9930+ continue;
9931+ if (d_is_positive(h_dentry)) {
9932+ au_set_dbtop(dentry, bindex);
9933+ return;
9934+ }
9935+ au_set_h_dptr(dentry, bindex, NULL);
9936+ }
9937+}
9938+
9939+void au_update_dbbot(struct dentry *dentry)
9940+{
9941+ aufs_bindex_t bindex, btop;
9942+ struct dentry *h_dentry;
9943+
9944+ btop = au_dbtop(dentry);
9945+ for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
9946+ h_dentry = au_h_dptr(dentry, bindex);
9947+ if (!h_dentry)
9948+ continue;
9949+ if (d_is_positive(h_dentry)) {
9950+ au_set_dbbot(dentry, bindex);
9951+ return;
9952+ }
9953+ au_set_h_dptr(dentry, bindex, NULL);
9954+ }
9955+}
9956+
9957+int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
9958+{
9959+ aufs_bindex_t bindex, bbot;
9960+
9961+ bbot = au_dbbot(dentry);
9962+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
9963+ if (au_h_dptr(dentry, bindex) == h_dentry)
9964+ return bindex;
9965+ return -1;
9966+}
9967diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
9968--- /usr/share/empty/fs/aufs/dir.c 1970-01-01 01:00:00.000000000 +0100
9969+++ linux/fs/aufs/dir.c 2021-12-03 15:40:58.233313963 +0100
9970@@ -0,0 +1,765 @@
9971+// SPDX-License-Identifier: GPL-2.0
9972+/*
9973+ * Copyright (C) 2005-2021 Junjiro R. Okajima
9974+ *
9975+ * This program, aufs is free software; you can redistribute it and/or modify
9976+ * it under the terms of the GNU General Public License as published by
9977+ * the Free Software Foundation; either version 2 of the License, or
9978+ * (at your option) any later version.
9979+ *
9980+ * This program is distributed in the hope that it will be useful,
9981+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9982+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9983+ * GNU General Public License for more details.
9984+ *
9985+ * You should have received a copy of the GNU General Public License
9986+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
9987+ */
9988+
9989+/*
9990+ * directory operations
9991+ */
9992+
9993+#include <linux/fs_stack.h>
9994+#include <linux/iversion.h>
9995+#include "aufs.h"
9996+
9997+void au_add_nlink(struct inode *dir, struct inode *h_dir)
9998+{
9999+ unsigned int nlink;
10000+
10001+ AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10002+
10003+ nlink = dir->i_nlink;
10004+ nlink += h_dir->i_nlink - 2;
10005+ if (h_dir->i_nlink < 2)
10006+ nlink += 2;
10007+ smp_mb(); /* for i_nlink */
10008+ /* 0 can happen in revaliding */
10009+ set_nlink(dir, nlink);
10010+}
10011+
10012+void au_sub_nlink(struct inode *dir, struct inode *h_dir)
10013+{
10014+ unsigned int nlink;
10015+
10016+ AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10017+
10018+ nlink = dir->i_nlink;
10019+ nlink -= h_dir->i_nlink - 2;
10020+ if (h_dir->i_nlink < 2)
10021+ nlink -= 2;
10022+ smp_mb(); /* for i_nlink */
10023+ /* nlink == 0 means the branch-fs is broken */
10024+ set_nlink(dir, nlink);
10025+}
10026+
10027+loff_t au_dir_size(struct file *file, struct dentry *dentry)
10028+{
10029+ loff_t sz;
10030+ aufs_bindex_t bindex, bbot;
10031+ struct file *h_file;
10032+ struct dentry *h_dentry;
10033+
10034+ sz = 0;
10035+ if (file) {
10036+ AuDebugOn(!d_is_dir(file->f_path.dentry));
10037+
10038+ bbot = au_fbbot_dir(file);
10039+ for (bindex = au_fbtop(file);
10040+ bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10041+ bindex++) {
10042+ h_file = au_hf_dir(file, bindex);
10043+ if (h_file && file_inode(h_file))
10044+ sz += vfsub_f_size_read(h_file);
10045+ }
10046+ } else {
10047+ AuDebugOn(!dentry);
10048+ AuDebugOn(!d_is_dir(dentry));
10049+
10050+ bbot = au_dbtaildir(dentry);
10051+ for (bindex = au_dbtop(dentry);
10052+ bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10053+ bindex++) {
10054+ h_dentry = au_h_dptr(dentry, bindex);
10055+ if (h_dentry && d_is_positive(h_dentry))
10056+ sz += i_size_read(d_inode(h_dentry));
10057+ }
10058+ }
10059+ if (sz < KMALLOC_MAX_SIZE)
10060+ sz = roundup_pow_of_two(sz);
10061+ if (sz > KMALLOC_MAX_SIZE)
10062+ sz = KMALLOC_MAX_SIZE;
10063+ else if (sz < NAME_MAX) {
10064+ BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
10065+ sz = AUFS_RDBLK_DEF;
10066+ }
10067+ return sz;
10068+}
10069+
10070+struct au_dir_ts_arg {
10071+ struct dentry *dentry;
10072+ aufs_bindex_t brid;
10073+};
10074+
10075+static void au_do_dir_ts(void *arg)
10076+{
10077+ struct au_dir_ts_arg *a = arg;
10078+ struct au_dtime dt;
10079+ struct path h_path;
10080+ struct inode *dir, *h_dir;
10081+ struct super_block *sb;
10082+ struct au_branch *br;
10083+ struct au_hinode *hdir;
10084+ int err;
10085+ aufs_bindex_t btop, bindex;
10086+
10087+ sb = a->dentry->d_sb;
10088+ if (d_really_is_negative(a->dentry))
10089+ goto out;
10090+ /* no dir->i_mutex lock */
10091+ aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
10092+
10093+ dir = d_inode(a->dentry);
10094+ btop = au_ibtop(dir);
10095+ bindex = au_br_index(sb, a->brid);
10096+ if (bindex < btop)
10097+ goto out_unlock;
10098+
10099+ br = au_sbr(sb, bindex);
10100+ h_path.dentry = au_h_dptr(a->dentry, bindex);
10101+ if (!h_path.dentry)
10102+ goto out_unlock;
10103+ h_path.mnt = au_br_mnt(br);
10104+ au_dtime_store(&dt, a->dentry, &h_path);
10105+
10106+ br = au_sbr(sb, btop);
10107+ if (!au_br_writable(br->br_perm))
10108+ goto out_unlock;
10109+ h_path.dentry = au_h_dptr(a->dentry, btop);
10110+ h_path.mnt = au_br_mnt(br);
10111+ err = vfsub_mnt_want_write(h_path.mnt);
10112+ if (err)
10113+ goto out_unlock;
10114+ hdir = au_hi(dir, btop);
10115+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
10116+ h_dir = au_h_iptr(dir, btop);
10117+ if (h_dir->i_nlink
10118+ && timespec64_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
10119+ dt.dt_h_path = h_path;
10120+ au_dtime_revert(&dt);
10121+ }
10122+ au_hn_inode_unlock(hdir);
10123+ vfsub_mnt_drop_write(h_path.mnt);
10124+ au_cpup_attr_timesizes(dir);
10125+
10126+out_unlock:
10127+ aufs_read_unlock(a->dentry, AuLock_DW);
10128+out:
10129+ dput(a->dentry);
10130+ au_nwt_done(&au_sbi(sb)->si_nowait);
10131+ au_kfree_try_rcu(arg);
10132+}
10133+
10134+void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
10135+{
10136+ int perm, wkq_err;
10137+ aufs_bindex_t btop;
10138+ struct au_dir_ts_arg *arg;
10139+ struct dentry *dentry;
10140+ struct super_block *sb;
10141+
10142+ IMustLock(dir);
10143+
10144+ dentry = d_find_any_alias(dir);
10145+ AuDebugOn(!dentry);
10146+ sb = dentry->d_sb;
10147+ btop = au_ibtop(dir);
10148+ if (btop == bindex) {
10149+ au_cpup_attr_timesizes(dir);
10150+ goto out;
10151+ }
10152+
10153+ perm = au_sbr_perm(sb, btop);
10154+ if (!au_br_writable(perm))
10155+ goto out;
10156+
10157+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
10158+ if (!arg)
10159+ goto out;
10160+
10161+ arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
10162+ arg->brid = au_sbr_id(sb, bindex);
10163+ wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
10164+ if (unlikely(wkq_err)) {
10165+ pr_err("wkq %d\n", wkq_err);
10166+ dput(dentry);
10167+ au_kfree_try_rcu(arg);
10168+ }
10169+
10170+out:
10171+ dput(dentry);
10172+}
10173+
10174+/* ---------------------------------------------------------------------- */
10175+
10176+static int reopen_dir(struct file *file)
10177+{
10178+ int err;
10179+ unsigned int flags;
10180+ aufs_bindex_t bindex, btail, btop;
10181+ struct dentry *dentry, *h_dentry;
10182+ struct file *h_file;
10183+
10184+ /* open all lower dirs */
10185+ dentry = file->f_path.dentry;
10186+ btop = au_dbtop(dentry);
10187+ for (bindex = au_fbtop(file); bindex < btop; bindex++)
10188+ au_set_h_fptr(file, bindex, NULL);
10189+ au_set_fbtop(file, btop);
10190+
10191+ btail = au_dbtaildir(dentry);
10192+ for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
10193+ au_set_h_fptr(file, bindex, NULL);
10194+ au_set_fbbot_dir(file, btail);
10195+
10196+ flags = vfsub_file_flags(file);
10197+ for (bindex = btop; bindex <= btail; bindex++) {
10198+ h_dentry = au_h_dptr(dentry, bindex);
10199+ if (!h_dentry)
10200+ continue;
10201+ h_file = au_hf_dir(file, bindex);
10202+ if (h_file)
10203+ continue;
10204+
10205+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10206+ err = PTR_ERR(h_file);
10207+ if (IS_ERR(h_file))
10208+ goto out; /* close all? */
10209+ au_set_h_fptr(file, bindex, h_file);
10210+ }
10211+ au_update_figen(file);
10212+ /* todo: necessary? */
10213+ /* file->f_ra = h_file->f_ra; */
10214+ err = 0;
10215+
10216+out:
10217+ return err;
10218+}
10219+
10220+static int do_open_dir(struct file *file, int flags, struct file *h_file)
10221+{
10222+ int err;
10223+ aufs_bindex_t bindex, btail;
10224+ struct dentry *dentry, *h_dentry;
10225+ struct vfsmount *mnt;
10226+
10227+ FiMustWriteLock(file);
10228+ AuDebugOn(h_file);
10229+
10230+ err = 0;
10231+ mnt = file->f_path.mnt;
10232+ dentry = file->f_path.dentry;
10233+ file->f_version = inode_query_iversion(d_inode(dentry));
10234+ bindex = au_dbtop(dentry);
10235+ au_set_fbtop(file, bindex);
10236+ btail = au_dbtaildir(dentry);
10237+ au_set_fbbot_dir(file, btail);
10238+ for (; !err && bindex <= btail; bindex++) {
10239+ h_dentry = au_h_dptr(dentry, bindex);
10240+ if (!h_dentry)
10241+ continue;
10242+
10243+ err = vfsub_test_mntns(mnt, h_dentry->d_sb);
10244+ if (unlikely(err))
10245+ break;
10246+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10247+ if (IS_ERR(h_file)) {
10248+ err = PTR_ERR(h_file);
10249+ break;
10250+ }
10251+ au_set_h_fptr(file, bindex, h_file);
10252+ }
10253+ au_update_figen(file);
10254+ /* todo: necessary? */
10255+ /* file->f_ra = h_file->f_ra; */
10256+ if (!err)
10257+ return 0; /* success */
10258+
10259+ /* close all */
10260+ for (bindex = au_fbtop(file); bindex <= btail; bindex++)
10261+ au_set_h_fptr(file, bindex, NULL);
10262+ au_set_fbtop(file, -1);
10263+ au_set_fbbot_dir(file, -1);
10264+
10265+ return err;
10266+}
10267+
10268+static int aufs_open_dir(struct inode *inode __maybe_unused,
10269+ struct file *file)
10270+{
10271+ int err;
10272+ struct super_block *sb;
10273+ struct au_fidir *fidir;
10274+
10275+ err = -ENOMEM;
10276+ sb = file->f_path.dentry->d_sb;
10277+ si_read_lock(sb, AuLock_FLUSH);
10278+ fidir = au_fidir_alloc(sb);
10279+ if (fidir) {
10280+ struct au_do_open_args args = {
10281+ .open = do_open_dir,
10282+ .fidir = fidir
10283+ };
10284+ err = au_do_open(file, &args);
10285+ if (unlikely(err))
10286+ au_kfree_rcu(fidir);
10287+ }
10288+ si_read_unlock(sb);
10289+ return err;
10290+}
10291+
10292+static int aufs_release_dir(struct inode *inode __maybe_unused,
10293+ struct file *file)
10294+{
10295+ struct au_vdir *vdir_cache;
10296+ struct au_finfo *finfo;
10297+ struct au_fidir *fidir;
10298+ struct au_hfile *hf;
10299+ aufs_bindex_t bindex, bbot;
10300+
10301+ finfo = au_fi(file);
10302+ fidir = finfo->fi_hdir;
10303+ if (fidir) {
10304+ au_hbl_del(&finfo->fi_hlist,
10305+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
10306+ vdir_cache = fidir->fd_vdir_cache; /* lock-free */
10307+ if (vdir_cache)
10308+ au_vdir_free(vdir_cache);
10309+
10310+ bindex = finfo->fi_btop;
10311+ if (bindex >= 0) {
10312+ hf = fidir->fd_hfile + bindex;
10313+ /*
10314+ * calls fput() instead of filp_close(),
10315+ * since no dnotify or lock for the lower file.
10316+ */
10317+ bbot = fidir->fd_bbot;
10318+ for (; bindex <= bbot; bindex++, hf++)
10319+ if (hf->hf_file)
10320+ au_hfput(hf, /*execed*/0);
10321+ }
10322+ au_kfree_rcu(fidir);
10323+ finfo->fi_hdir = NULL;
10324+ }
10325+ au_finfo_fin(file);
10326+ return 0;
10327+}
10328+
10329+/* ---------------------------------------------------------------------- */
10330+
10331+static int au_do_flush_dir(struct file *file, fl_owner_t id)
10332+{
10333+ int err;
10334+ aufs_bindex_t bindex, bbot;
10335+ struct file *h_file;
10336+
10337+ err = 0;
10338+ bbot = au_fbbot_dir(file);
10339+ for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10340+ h_file = au_hf_dir(file, bindex);
10341+ if (h_file)
10342+ err = vfsub_flush(h_file, id);
10343+ }
10344+ return err;
10345+}
10346+
10347+static int aufs_flush_dir(struct file *file, fl_owner_t id)
10348+{
10349+ return au_do_flush(file, id, au_do_flush_dir);
10350+}
10351+
10352+/* ---------------------------------------------------------------------- */
10353+
10354+static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
10355+{
10356+ int err;
10357+ aufs_bindex_t bbot, bindex;
10358+ struct inode *inode;
10359+ struct super_block *sb;
10360+
10361+ err = 0;
10362+ sb = dentry->d_sb;
10363+ inode = d_inode(dentry);
10364+ IMustLock(inode);
10365+ bbot = au_dbbot(dentry);
10366+ for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
10367+ struct path h_path;
10368+
10369+ if (au_test_ro(sb, bindex, inode))
10370+ continue;
10371+ h_path.dentry = au_h_dptr(dentry, bindex);
10372+ if (!h_path.dentry)
10373+ continue;
10374+
10375+ h_path.mnt = au_sbr_mnt(sb, bindex);
10376+ err = vfsub_fsync(NULL, &h_path, datasync);
10377+ }
10378+
10379+ return err;
10380+}
10381+
10382+static int au_do_fsync_dir(struct file *file, int datasync)
10383+{
10384+ int err;
10385+ aufs_bindex_t bbot, bindex;
10386+ struct file *h_file;
10387+ struct super_block *sb;
10388+ struct inode *inode;
10389+
10390+ err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10391+ if (unlikely(err))
10392+ goto out;
10393+
10394+ inode = file_inode(file);
10395+ sb = inode->i_sb;
10396+ bbot = au_fbbot_dir(file);
10397+ for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10398+ h_file = au_hf_dir(file, bindex);
10399+ if (!h_file || au_test_ro(sb, bindex, inode))
10400+ continue;
10401+
10402+ err = vfsub_fsync(h_file, &h_file->f_path, datasync);
10403+ }
10404+
10405+out:
10406+ return err;
10407+}
10408+
10409+/*
10410+ * @file may be NULL
10411+ */
10412+static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
10413+ int datasync)
10414+{
10415+ int err;
10416+ struct dentry *dentry;
10417+ struct inode *inode;
10418+ struct super_block *sb;
10419+
10420+ err = 0;
10421+ dentry = file->f_path.dentry;
10422+ inode = d_inode(dentry);
10423+ inode_lock(inode);
10424+ sb = dentry->d_sb;
10425+ si_noflush_read_lock(sb);
10426+ if (file)
10427+ err = au_do_fsync_dir(file, datasync);
10428+ else {
10429+ di_write_lock_child(dentry);
10430+ err = au_do_fsync_dir_no_file(dentry, datasync);
10431+ }
10432+ au_cpup_attr_timesizes(inode);
10433+ di_write_unlock(dentry);
10434+ if (file)
10435+ fi_write_unlock(file);
10436+
10437+ si_read_unlock(sb);
10438+ inode_unlock(inode);
10439+ return err;
10440+}
10441+
10442+/* ---------------------------------------------------------------------- */
10443+
10444+static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
10445+{
10446+ int err;
10447+ struct dentry *dentry;
10448+ struct inode *inode, *h_inode;
10449+ struct super_block *sb;
10450+
10451+ AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
10452+
10453+ dentry = file->f_path.dentry;
10454+ inode = d_inode(dentry);
10455+ IMustLock(inode);
10456+
10457+ sb = dentry->d_sb;
10458+ si_read_lock(sb, AuLock_FLUSH);
10459+ err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10460+ if (unlikely(err))
10461+ goto out;
10462+ err = au_alive_dir(dentry);
10463+ if (!err)
10464+ err = au_vdir_init(file);
10465+ di_downgrade_lock(dentry, AuLock_IR);
10466+ if (unlikely(err))
10467+ goto out_unlock;
10468+
10469+ h_inode = au_h_iptr(inode, au_ibtop(inode));
10470+ if (!au_test_nfsd()) {
10471+ err = au_vdir_fill_de(file, ctx);
10472+ fsstack_copy_attr_atime(inode, h_inode);
10473+ } else {
10474+ /*
10475+ * nfsd filldir may call lookup_one_len(), vfs_getattr(),
10476+ * encode_fh() and others.
10477+ */
10478+ atomic_inc(&h_inode->i_count);
10479+ di_read_unlock(dentry, AuLock_IR);
10480+ si_read_unlock(sb);
10481+ err = au_vdir_fill_de(file, ctx);
10482+ fsstack_copy_attr_atime(inode, h_inode);
10483+ fi_write_unlock(file);
10484+ iput(h_inode);
10485+
10486+ AuTraceErr(err);
10487+ return err;
10488+ }
10489+
10490+out_unlock:
10491+ di_read_unlock(dentry, AuLock_IR);
10492+ fi_write_unlock(file);
10493+out:
10494+ si_read_unlock(sb);
10495+ return err;
10496+}
10497+
10498+/* ---------------------------------------------------------------------- */
10499+
10500+#define AuTestEmpty_WHONLY 1
10501+#define AuTestEmpty_CALLED (1 << 1)
10502+#define AuTestEmpty_SHWH (1 << 2)
10503+#define au_ftest_testempty(flags, name) ((flags) & AuTestEmpty_##name)
10504+#define au_fset_testempty(flags, name) \
10505+ do { (flags) |= AuTestEmpty_##name; } while (0)
10506+#define au_fclr_testempty(flags, name) \
10507+ do { (flags) &= ~AuTestEmpty_##name; } while (0)
10508+
10509+#ifndef CONFIG_AUFS_SHWH
10510+#undef AuTestEmpty_SHWH
10511+#define AuTestEmpty_SHWH 0
10512+#endif
10513+
10514+struct test_empty_arg {
10515+ struct dir_context ctx;
10516+ struct au_nhash *whlist;
10517+ unsigned int flags;
10518+ int err;
10519+ aufs_bindex_t bindex;
10520+};
10521+
10522+static int test_empty_cb(struct dir_context *ctx, const char *__name,
10523+ int namelen, loff_t offset __maybe_unused, u64 ino,
10524+ unsigned int d_type)
10525+{
10526+ struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10527+ ctx);
10528+ char *name = (void *)__name;
10529+
10530+ arg->err = 0;
10531+ au_fset_testempty(arg->flags, CALLED);
10532+ /* smp_mb(); */
10533+ if (name[0] == '.'
10534+ && (namelen == 1 || (name[1] == '.' && namelen == 2)))
10535+ goto out; /* success */
10536+
10537+ if (namelen <= AUFS_WH_PFX_LEN
10538+ || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10539+ if (au_ftest_testempty(arg->flags, WHONLY)
10540+ && !au_nhash_test_known_wh(arg->whlist, name, namelen))
10541+ arg->err = -ENOTEMPTY;
10542+ goto out;
10543+ }
10544+
10545+ name += AUFS_WH_PFX_LEN;
10546+ namelen -= AUFS_WH_PFX_LEN;
10547+ if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
10548+ arg->err = au_nhash_append_wh
10549+ (arg->whlist, name, namelen, ino, d_type, arg->bindex,
10550+ au_ftest_testempty(arg->flags, SHWH));
10551+
10552+out:
10553+ /* smp_mb(); */
10554+ AuTraceErr(arg->err);
10555+ return arg->err;
10556+}
10557+
10558+static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10559+{
10560+ int err;
10561+ struct file *h_file;
10562+ struct au_branch *br;
10563+
10564+ h_file = au_h_open(dentry, arg->bindex,
10565+ O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
10566+ /*file*/NULL, /*force_wr*/0);
10567+ err = PTR_ERR(h_file);
10568+ if (IS_ERR(h_file))
10569+ goto out;
10570+
10571+ err = 0;
10572+ if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
10573+ && !file_inode(h_file)->i_nlink)
10574+ goto out_put;
10575+
10576+ do {
10577+ arg->err = 0;
10578+ au_fclr_testempty(arg->flags, CALLED);
10579+ /* smp_mb(); */
10580+ err = vfsub_iterate_dir(h_file, &arg->ctx);
10581+ if (err >= 0)
10582+ err = arg->err;
10583+ } while (!err && au_ftest_testempty(arg->flags, CALLED));
10584+
10585+out_put:
10586+ fput(h_file);
10587+ br = au_sbr(dentry->d_sb, arg->bindex);
10588+ au_lcnt_dec(&br->br_nfiles);
10589+out:
10590+ return err;
10591+}
10592+
10593+struct do_test_empty_args {
10594+ int *errp;
10595+ struct dentry *dentry;
10596+ struct test_empty_arg *arg;
10597+};
10598+
10599+static void call_do_test_empty(void *args)
10600+{
10601+ struct do_test_empty_args *a = args;
10602+ *a->errp = do_test_empty(a->dentry, a->arg);
10603+}
10604+
10605+static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10606+{
10607+ int err, wkq_err;
10608+ struct dentry *h_dentry;
10609+ struct inode *h_inode;
10610+ struct user_namespace *h_userns;
10611+
10612+ h_userns = au_sbr_userns(dentry->d_sb, arg->bindex);
10613+ h_dentry = au_h_dptr(dentry, arg->bindex);
10614+ h_inode = d_inode(h_dentry);
10615+ /* todo: i_mode changes anytime? */
10616+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
10617+ err = au_test_h_perm_sio(h_userns, h_inode, MAY_EXEC | MAY_READ);
10618+ inode_unlock_shared(h_inode);
10619+ if (!err)
10620+ err = do_test_empty(dentry, arg);
10621+ else {
10622+ struct do_test_empty_args args = {
10623+ .errp = &err,
10624+ .dentry = dentry,
10625+ .arg = arg
10626+ };
10627+ unsigned int flags = arg->flags;
10628+
10629+ wkq_err = au_wkq_wait(call_do_test_empty, &args);
10630+ if (unlikely(wkq_err))
10631+ err = wkq_err;
10632+ arg->flags = flags;
10633+ }
10634+
10635+ return err;
10636+}
10637+
10638+int au_test_empty_lower(struct dentry *dentry)
10639+{
10640+ int err;
10641+ unsigned int rdhash;
10642+ aufs_bindex_t bindex, btop, btail;
10643+ struct au_nhash whlist;
10644+ struct test_empty_arg arg = {
10645+ .ctx = {
10646+ .actor = test_empty_cb
10647+ }
10648+ };
10649+ int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
10650+
10651+ SiMustAnyLock(dentry->d_sb);
10652+
10653+ rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10654+ if (!rdhash)
10655+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10656+ err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
10657+ if (unlikely(err))
10658+ goto out;
10659+
10660+ arg.flags = 0;
10661+ arg.whlist = &whlist;
10662+ btop = au_dbtop(dentry);
10663+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10664+ au_fset_testempty(arg.flags, SHWH);
10665+ test_empty = do_test_empty;
10666+ if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10667+ test_empty = sio_test_empty;
10668+ arg.bindex = btop;
10669+ err = test_empty(dentry, &arg);
10670+ if (unlikely(err))
10671+ goto out_whlist;
10672+
10673+ au_fset_testempty(arg.flags, WHONLY);
10674+ btail = au_dbtaildir(dentry);
10675+ for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
10676+ struct dentry *h_dentry;
10677+
10678+ h_dentry = au_h_dptr(dentry, bindex);
10679+ if (h_dentry && d_is_positive(h_dentry)) {
10680+ arg.bindex = bindex;
10681+ err = test_empty(dentry, &arg);
10682+ }
10683+ }
10684+
10685+out_whlist:
10686+ au_nhash_wh_free(&whlist);
10687+out:
10688+ return err;
10689+}
10690+
10691+int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10692+{
10693+ int err;
10694+ struct test_empty_arg arg = {
10695+ .ctx = {
10696+ .actor = test_empty_cb
10697+ }
10698+ };
10699+ aufs_bindex_t bindex, btail;
10700+
10701+ err = 0;
10702+ arg.whlist = whlist;
10703+ arg.flags = AuTestEmpty_WHONLY;
10704+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10705+ au_fset_testempty(arg.flags, SHWH);
10706+ btail = au_dbtaildir(dentry);
10707+ for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
10708+ struct dentry *h_dentry;
10709+
10710+ h_dentry = au_h_dptr(dentry, bindex);
10711+ if (h_dentry && d_is_positive(h_dentry)) {
10712+ arg.bindex = bindex;
10713+ err = sio_test_empty(dentry, &arg);
10714+ }
10715+ }
10716+
10717+ return err;
10718+}
10719+
10720+/* ---------------------------------------------------------------------- */
10721+
10722+const struct file_operations aufs_dir_fop = {
10723+ .owner = THIS_MODULE,
10724+ .llseek = default_llseek,
10725+ .read = generic_read_dir,
10726+ .iterate_shared = aufs_iterate_shared,
10727+ .unlocked_ioctl = aufs_ioctl_dir,
10728+#ifdef CONFIG_COMPAT
10729+ .compat_ioctl = aufs_compat_ioctl_dir,
10730+#endif
10731+ .open = aufs_open_dir,
10732+ .release = aufs_release_dir,
10733+ .flush = aufs_flush_dir,
10734+ .fsync = aufs_fsync_dir
10735+};
10736diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
10737--- /usr/share/empty/fs/aufs/dir.h 1970-01-01 01:00:00.000000000 +0100
10738+++ linux/fs/aufs/dir.h 2021-12-03 15:38:59.933313976 +0100
10739@@ -0,0 +1,134 @@
10740+/* SPDX-License-Identifier: GPL-2.0 */
10741+/*
10742+ * Copyright (C) 2005-2021 Junjiro R. Okajima
10743+ *
10744+ * This program, aufs is free software; you can redistribute it and/or modify
10745+ * it under the terms of the GNU General Public License as published by
10746+ * the Free Software Foundation; either version 2 of the License, or
10747+ * (at your option) any later version.
10748+ *
10749+ * This program is distributed in the hope that it will be useful,
10750+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10751+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10752+ * GNU General Public License for more details.
10753+ *
10754+ * You should have received a copy of the GNU General Public License
10755+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
10756+ */
10757+
10758+/*
10759+ * directory operations
10760+ */
10761+
10762+#ifndef __AUFS_DIR_H__
10763+#define __AUFS_DIR_H__
10764+
10765+#ifdef __KERNEL__
10766+
10767+#include <linux/fs.h>
10768+
10769+/* ---------------------------------------------------------------------- */
10770+
10771+/* need to be faster and smaller */
10772+
10773+struct au_nhash {
10774+ unsigned int nh_num;
10775+ struct hlist_head *nh_head;
10776+};
10777+
10778+struct au_vdir_destr {
10779+ unsigned char len;
10780+ unsigned char name[];
10781+} __packed;
10782+
10783+struct au_vdir_dehstr {
10784+ struct hlist_node hash;
10785+ struct au_vdir_destr *str;
10786+ struct rcu_head rcu;
10787+} ____cacheline_aligned_in_smp;
10788+
10789+struct au_vdir_de {
10790+ ino_t de_ino;
10791+ unsigned char de_type;
10792+ /* caution: packed */
10793+ struct au_vdir_destr de_str;
10794+} __packed;
10795+
10796+struct au_vdir_wh {
10797+ struct hlist_node wh_hash;
10798+#ifdef CONFIG_AUFS_SHWH
10799+ ino_t wh_ino;
10800+ aufs_bindex_t wh_bindex;
10801+ unsigned char wh_type;
10802+#else
10803+ aufs_bindex_t wh_bindex;
10804+#endif
10805+ /* caution: packed */
10806+ struct au_vdir_destr wh_str;
10807+} __packed;
10808+
10809+union au_vdir_deblk_p {
10810+ unsigned char *deblk;
10811+ struct au_vdir_de *de;
10812+};
10813+
10814+struct au_vdir {
10815+ unsigned char **vd_deblk;
10816+ unsigned long vd_nblk;
10817+ struct {
10818+ unsigned long ul;
10819+ union au_vdir_deblk_p p;
10820+ } vd_last;
10821+
10822+ u64 vd_version;
10823+ unsigned int vd_deblk_sz;
10824+ unsigned long vd_jiffy;
10825+ struct rcu_head rcu;
10826+} ____cacheline_aligned_in_smp;
10827+
10828+/* ---------------------------------------------------------------------- */
10829+
10830+/* dir.c */
10831+extern const struct file_operations aufs_dir_fop;
10832+void au_add_nlink(struct inode *dir, struct inode *h_dir);
10833+void au_sub_nlink(struct inode *dir, struct inode *h_dir);
10834+loff_t au_dir_size(struct file *file, struct dentry *dentry);
10835+void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
10836+int au_test_empty_lower(struct dentry *dentry);
10837+int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10838+
10839+/* vdir.c */
10840+unsigned int au_rdhash_est(loff_t sz);
10841+int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
10842+void au_nhash_wh_free(struct au_nhash *whlist);
10843+int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
10844+ int limit);
10845+int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
10846+int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
10847+ unsigned int d_type, aufs_bindex_t bindex,
10848+ unsigned char shwh);
10849+void au_vdir_free(struct au_vdir *vdir);
10850+int au_vdir_init(struct file *file);
10851+int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
10852+
10853+/* ioctl.c */
10854+long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
10855+
10856+#ifdef CONFIG_AUFS_RDU
10857+/* rdu.c */
10858+long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
10859+#ifdef CONFIG_COMPAT
10860+long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
10861+ unsigned long arg);
10862+#endif
10863+#else
10864+AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
10865+ unsigned int cmd, unsigned long arg)
10866+#ifdef CONFIG_COMPAT
10867+AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
10868+ unsigned int cmd, unsigned long arg)
10869+#endif
10870+#endif
10871+
10872+#endif /* __KERNEL__ */
10873+#endif /* __AUFS_DIR_H__ */
10874diff -urN /usr/share/empty/fs/aufs/dirren.c linux/fs/aufs/dirren.c
10875--- /usr/share/empty/fs/aufs/dirren.c 1970-01-01 01:00:00.000000000 +0100
10876+++ linux/fs/aufs/dirren.c 2021-12-03 15:38:59.933313976 +0100
10877@@ -0,0 +1,1315 @@
10878+// SPDX-License-Identifier: GPL-2.0
10879+/*
10880+ * Copyright (C) 2017-2021 Junjiro R. Okajima
10881+ *
10882+ * This program, aufs is free software; you can redistribute it and/or modify
10883+ * it under the terms of the GNU General Public License as published by
10884+ * the Free Software Foundation; either version 2 of the License, or
10885+ * (at your option) any later version.
10886+ *
10887+ * This program is distributed in the hope that it will be useful,
10888+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10889+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10890+ * GNU General Public License for more details.
10891+ *
10892+ * You should have received a copy of the GNU General Public License
10893+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
10894+ */
10895+
10896+/*
10897+ * special handling in renaming a directory
10898+ * in order to support looking-up the before-renamed name on the lower readonly
10899+ * branches
10900+ */
10901+
10902+#include <linux/byteorder/generic.h>
10903+#include "aufs.h"
10904+
10905+static void au_dr_hino_del(struct au_dr_br *dr, struct au_dr_hino *ent)
10906+{
10907+ int idx;
10908+
10909+ idx = au_dr_ihash(ent->dr_h_ino);
10910+ au_hbl_del(&ent->dr_hnode, dr->dr_h_ino + idx);
10911+}
10912+
10913+static int au_dr_hino_test_empty(struct au_dr_br *dr)
10914+{
10915+ int ret, i;
10916+ struct hlist_bl_head *hbl;
10917+
10918+ ret = 1;
10919+ for (i = 0; ret && i < AuDirren_NHASH; i++) {
10920+ hbl = dr->dr_h_ino + i;
10921+ hlist_bl_lock(hbl);
10922+ ret &= hlist_bl_empty(hbl);
10923+ hlist_bl_unlock(hbl);
10924+ }
10925+
10926+ return ret;
10927+}
10928+
10929+static struct au_dr_hino *au_dr_hino_find(struct au_dr_br *dr, ino_t ino)
10930+{
10931+ struct au_dr_hino *found, *ent;
10932+ struct hlist_bl_head *hbl;
10933+ struct hlist_bl_node *pos;
10934+ int idx;
10935+
10936+ found = NULL;
10937+ idx = au_dr_ihash(ino);
10938+ hbl = dr->dr_h_ino + idx;
10939+ hlist_bl_lock(hbl);
10940+ hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10941+ if (ent->dr_h_ino == ino) {
10942+ found = ent;
10943+ break;
10944+ }
10945+ hlist_bl_unlock(hbl);
10946+
10947+ return found;
10948+}
10949+
10950+int au_dr_hino_test_add(struct au_dr_br *dr, ino_t ino,
10951+ struct au_dr_hino *add_ent)
10952+{
10953+ int found, idx;
10954+ struct hlist_bl_head *hbl;
10955+ struct hlist_bl_node *pos;
10956+ struct au_dr_hino *ent;
10957+
10958+ found = 0;
10959+ idx = au_dr_ihash(ino);
10960+ hbl = dr->dr_h_ino + idx;
10961+#if 0 /* debug print */
10962+ {
10963+ struct hlist_bl_node *tmp;
10964+
10965+ hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
10966+ AuDbg("hi%llu\n", (unsigned long long)ent->dr_h_ino);
10967+ }
10968+#endif
10969+ hlist_bl_lock(hbl);
10970+ hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10971+ if (ent->dr_h_ino == ino) {
10972+ found = 1;
10973+ break;
10974+ }
10975+ if (!found && add_ent)
10976+ hlist_bl_add_head(&add_ent->dr_hnode, hbl);
10977+ hlist_bl_unlock(hbl);
10978+
10979+ if (!found && add_ent)
10980+ AuDbg("i%llu added\n", (unsigned long long)add_ent->dr_h_ino);
10981+
10982+ return found;
10983+}
10984+
10985+void au_dr_hino_free(struct au_dr_br *dr)
10986+{
10987+ int i;
10988+ struct hlist_bl_head *hbl;
10989+ struct hlist_bl_node *pos, *tmp;
10990+ struct au_dr_hino *ent;
10991+
10992+ /* SiMustWriteLock(sb); */
10993+
10994+ for (i = 0; i < AuDirren_NHASH; i++) {
10995+ hbl = dr->dr_h_ino + i;
10996+ /* no spinlock since sbinfo must be write-locked */
10997+ hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
10998+ au_kfree_rcu(ent);
10999+ INIT_HLIST_BL_HEAD(hbl);
11000+ }
11001+}
11002+
11003+/* returns the number of inodes or an error */
11004+static int au_dr_hino_store(struct super_block *sb, struct au_branch *br,
11005+ struct file *hinofile)
11006+{
11007+ int err, i;
11008+ ssize_t ssz;
11009+ loff_t pos, oldsize;
11010+ __be64 u64;
11011+ struct inode *hinoinode;
11012+ struct hlist_bl_head *hbl;
11013+ struct hlist_bl_node *n1, *n2;
11014+ struct au_dr_hino *ent;
11015+
11016+ SiMustWriteLock(sb);
11017+ AuDebugOn(!au_br_writable(br->br_perm));
11018+
11019+ hinoinode = file_inode(hinofile);
11020+ oldsize = i_size_read(hinoinode);
11021+
11022+ err = 0;
11023+ pos = 0;
11024+ hbl = br->br_dirren.dr_h_ino;
11025+ for (i = 0; !err && i < AuDirren_NHASH; i++, hbl++) {
11026+ /* no bit-lock since sbinfo must be write-locked */
11027+ hlist_bl_for_each_entry_safe(ent, n1, n2, hbl, dr_hnode) {
11028+ AuDbg("hi%llu, %pD2\n",
11029+ (unsigned long long)ent->dr_h_ino, hinofile);
11030+ u64 = cpu_to_be64(ent->dr_h_ino);
11031+ ssz = vfsub_write_k(hinofile, &u64, sizeof(u64), &pos);
11032+ if (ssz == sizeof(u64))
11033+ continue;
11034+
11035+ /* write error */
11036+ pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11037+ err = -ENOSPC;
11038+ if (ssz < 0)
11039+ err = ssz;
11040+ break;
11041+ }
11042+ }
11043+ /* regardless the error */
11044+ if (pos < oldsize) {
11045+ err = vfsub_trunc(&hinofile->f_path, pos, /*attr*/0, hinofile);
11046+ AuTraceErr(err);
11047+ }
11048+
11049+ AuTraceErr(err);
11050+ return err;
11051+}
11052+
11053+static int au_dr_hino_load(struct au_dr_br *dr, struct file *hinofile)
11054+{
11055+ int err, hidx;
11056+ ssize_t ssz;
11057+ size_t sz, n;
11058+ loff_t pos;
11059+ uint64_t u64;
11060+ struct au_dr_hino *ent;
11061+ struct inode *hinoinode;
11062+ struct hlist_bl_head *hbl;
11063+
11064+ err = 0;
11065+ pos = 0;
11066+ hbl = dr->dr_h_ino;
11067+ hinoinode = file_inode(hinofile);
11068+ sz = i_size_read(hinoinode);
11069+ AuDebugOn(sz % sizeof(u64));
11070+ n = sz / sizeof(u64);
11071+ while (n--) {
11072+ ssz = vfsub_read_k(hinofile, &u64, sizeof(u64), &pos);
11073+ if (unlikely(ssz != sizeof(u64))) {
11074+ pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11075+ err = -EINVAL;
11076+ if (ssz < 0)
11077+ err = ssz;
11078+ goto out_free;
11079+ }
11080+
11081+ ent = kmalloc(sizeof(*ent), GFP_NOFS);
11082+ if (!ent) {
11083+ err = -ENOMEM;
11084+ AuTraceErr(err);
11085+ goto out_free;
11086+ }
11087+ ent->dr_h_ino = be64_to_cpu((__force __be64)u64);
11088+ AuDbg("hi%llu, %pD2\n",
11089+ (unsigned long long)ent->dr_h_ino, hinofile);
11090+ hidx = au_dr_ihash(ent->dr_h_ino);
11091+ au_hbl_add(&ent->dr_hnode, hbl + hidx);
11092+ }
11093+ goto out; /* success */
11094+
11095+out_free:
11096+ au_dr_hino_free(dr);
11097+out:
11098+ AuTraceErr(err);
11099+ return err;
11100+}
11101+
11102+/*
11103+ * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
11104+ * @path is a switch to distinguish load and store.
11105+ */
11106+static int au_dr_hino(struct super_block *sb, aufs_bindex_t bindex,
11107+ struct au_branch *br, const struct path *path)
11108+{
11109+ int err, flags;
11110+ unsigned char load, suspend;
11111+ struct file *hinofile;
11112+ struct au_hinode *hdir;
11113+ struct inode *dir, *delegated;
11114+ struct path hinopath;
11115+ struct qstr hinoname = QSTR_INIT(AUFS_WH_DR_BRHINO,
11116+ sizeof(AUFS_WH_DR_BRHINO) - 1);
11117+
11118+ AuDebugOn(bindex < 0 && !br);
11119+ AuDebugOn(bindex >= 0 && br);
11120+
11121+ err = -EINVAL;
11122+ suspend = !br;
11123+ if (suspend)
11124+ br = au_sbr(sb, bindex);
11125+ load = !!path;
11126+ if (!load) {
11127+ path = &br->br_path;
11128+ AuDebugOn(!au_br_writable(br->br_perm));
11129+ if (unlikely(!au_br_writable(br->br_perm)))
11130+ goto out;
11131+ }
11132+
11133+ hdir = NULL;
11134+ if (suspend) {
11135+ dir = d_inode(sb->s_root);
11136+ hdir = au_hinode(au_ii(dir), bindex);
11137+ dir = hdir->hi_inode;
11138+ au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11139+ } else {
11140+ dir = d_inode(path->dentry);
11141+ inode_lock_nested(dir, AuLsc_I_CHILD);
11142+ }
11143+ hinopath.mnt = path->mnt;
11144+ hinopath.dentry = vfsub_lkup_one(&hinoname, (struct path *)path);
11145+ err = PTR_ERR(hinopath.dentry);
11146+ if (IS_ERR(hinopath.dentry))
11147+ goto out_unlock;
11148+
11149+ err = 0;
11150+ flags = O_RDONLY;
11151+ if (load) {
11152+ if (d_is_negative(hinopath.dentry))
11153+ goto out_dput; /* success */
11154+ } else {
11155+ if (au_dr_hino_test_empty(&br->br_dirren)) {
11156+ if (d_is_positive(hinopath.dentry)) {
11157+ delegated = NULL;
11158+ err = vfsub_unlink(dir, &hinopath, &delegated,
11159+ /*force*/0);
11160+ AuTraceErr(err);
11161+ if (unlikely(err))
11162+ pr_err("ignored err %d, %pd2\n",
11163+ err, hinopath.dentry);
11164+ if (unlikely(err == -EWOULDBLOCK))
11165+ iput(delegated);
11166+ err = 0;
11167+ }
11168+ goto out_dput;
11169+ } else if (!d_is_positive(hinopath.dentry)) {
11170+ err = vfsub_create(dir, &hinopath, 0600,
11171+ /*want_excl*/false);
11172+ AuTraceErr(err);
11173+ if (unlikely(err))
11174+ goto out_dput;
11175+ }
11176+ flags = O_WRONLY;
11177+ }
11178+ hinofile = vfsub_dentry_open(&hinopath, flags);
11179+ if (suspend)
11180+ au_hn_inode_unlock(hdir);
11181+ else
11182+ inode_unlock(dir);
11183+ dput(hinopath.dentry);
11184+ AuTraceErrPtr(hinofile);
11185+ if (IS_ERR(hinofile)) {
11186+ err = PTR_ERR(hinofile);
11187+ goto out;
11188+ }
11189+
11190+ if (load)
11191+ err = au_dr_hino_load(&br->br_dirren, hinofile);
11192+ else
11193+ err = au_dr_hino_store(sb, br, hinofile);
11194+ fput(hinofile);
11195+ goto out;
11196+
11197+out_dput:
11198+ dput(hinopath.dentry);
11199+out_unlock:
11200+ if (suspend)
11201+ au_hn_inode_unlock(hdir);
11202+ else
11203+ inode_unlock(dir);
11204+out:
11205+ AuTraceErr(err);
11206+ return err;
11207+}
11208+
11209+/* ---------------------------------------------------------------------- */
11210+
11211+static int au_dr_brid_init(struct au_dr_brid *brid, const struct path *path)
11212+{
11213+ int err;
11214+ struct kstatfs kstfs;
11215+ dev_t dev;
11216+ struct dentry *dentry;
11217+ struct super_block *sb;
11218+
11219+ err = vfs_statfs((void *)path, &kstfs);
11220+ AuTraceErr(err);
11221+ if (unlikely(err))
11222+ goto out;
11223+
11224+ /* todo: support for UUID */
11225+
11226+ if (kstfs.f_fsid.val[0] || kstfs.f_fsid.val[1]) {
11227+ brid->type = AuBrid_FSID;
11228+ brid->fsid = kstfs.f_fsid;
11229+ } else {
11230+ dentry = path->dentry;
11231+ sb = dentry->d_sb;
11232+ dev = sb->s_dev;
11233+ if (dev) {
11234+ brid->type = AuBrid_DEV;
11235+ brid->dev = dev;
11236+ }
11237+ }
11238+
11239+out:
11240+ return err;
11241+}
11242+
11243+int au_dr_br_init(struct super_block *sb, struct au_branch *br,
11244+ const struct path *path)
11245+{
11246+ int err, i;
11247+ struct au_dr_br *dr;
11248+ struct hlist_bl_head *hbl;
11249+
11250+ dr = &br->br_dirren;
11251+ hbl = dr->dr_h_ino;
11252+ for (i = 0; i < AuDirren_NHASH; i++, hbl++)
11253+ INIT_HLIST_BL_HEAD(hbl);
11254+
11255+ err = au_dr_brid_init(&dr->dr_brid, path);
11256+ if (unlikely(err))
11257+ goto out;
11258+
11259+ if (au_opt_test(au_mntflags(sb), DIRREN))
11260+ err = au_dr_hino(sb, /*bindex*/-1, br, path);
11261+
11262+out:
11263+ AuTraceErr(err);
11264+ return err;
11265+}
11266+
11267+int au_dr_br_fin(struct super_block *sb, struct au_branch *br)
11268+{
11269+ int err;
11270+
11271+ err = 0;
11272+ if (au_br_writable(br->br_perm))
11273+ err = au_dr_hino(sb, /*bindex*/-1, br, /*path*/NULL);
11274+ if (!err)
11275+ au_dr_hino_free(&br->br_dirren);
11276+
11277+ return err;
11278+}
11279+
11280+/* ---------------------------------------------------------------------- */
11281+
11282+static int au_brid_str(struct au_dr_brid *brid, struct inode *h_inode,
11283+ char *buf, size_t sz)
11284+{
11285+ int err;
11286+ unsigned int major, minor;
11287+ char *p;
11288+
11289+ p = buf;
11290+ err = snprintf(p, sz, "%d_", brid->type);
11291+ AuDebugOn(err > sz);
11292+ p += err;
11293+ sz -= err;
11294+ switch (brid->type) {
11295+ case AuBrid_Unset:
11296+ return -EINVAL;
11297+ case AuBrid_UUID:
11298+ err = snprintf(p, sz, "%pU", brid->uuid.b);
11299+ break;
11300+ case AuBrid_FSID:
11301+ err = snprintf(p, sz, "%08x-%08x",
11302+ brid->fsid.val[0], brid->fsid.val[1]);
11303+ break;
11304+ case AuBrid_DEV:
11305+ major = MAJOR(brid->dev);
11306+ minor = MINOR(brid->dev);
11307+ if (major <= 0xff && minor <= 0xff)
11308+ err = snprintf(p, sz, "%02x%02x", major, minor);
11309+ else
11310+ err = snprintf(p, sz, "%03x:%05x", major, minor);
11311+ break;
11312+ }
11313+ AuDebugOn(err > sz);
11314+ p += err;
11315+ sz -= err;
11316+ err = snprintf(p, sz, "_%llu", (unsigned long long)h_inode->i_ino);
11317+ AuDebugOn(err > sz);
11318+ p += err;
11319+ sz -= err;
11320+
11321+ return p - buf;
11322+}
11323+
11324+static int au_drinfo_name(struct au_branch *br, char *name, int len)
11325+{
11326+ int rlen;
11327+ struct dentry *br_dentry;
11328+ struct inode *br_inode;
11329+
11330+ br_dentry = au_br_dentry(br);
11331+ br_inode = d_inode(br_dentry);
11332+ rlen = au_brid_str(&br->br_dirren.dr_brid, br_inode, name, len);
11333+ AuDebugOn(rlen >= AUFS_DIRREN_ENV_VAL_SZ);
11334+ AuDebugOn(rlen > len);
11335+
11336+ return rlen;
11337+}
11338+
11339+/* ---------------------------------------------------------------------- */
11340+
11341+/*
11342+ * from the given @h_dentry, construct drinfo at @*fdata.
11343+ * when the size of @*fdata is not enough, reallocate and return new @fdata and
11344+ * @allocated.
11345+ */
11346+static int au_drinfo_construct(struct au_drinfo_fdata **fdata,
11347+ struct dentry *h_dentry,
11348+ unsigned char *allocated)
11349+{
11350+ int err, v;
11351+ struct au_drinfo_fdata *f, *p;
11352+ struct au_drinfo *drinfo;
11353+ struct inode *h_inode;
11354+ struct qstr *qname;
11355+
11356+ err = 0;
11357+ f = *fdata;
11358+ h_inode = d_inode(h_dentry);
11359+ qname = &h_dentry->d_name;
11360+ drinfo = &f->drinfo;
11361+ drinfo->ino = (__force uint64_t)cpu_to_be64(h_inode->i_ino);
11362+ drinfo->oldnamelen = qname->len;
11363+ if (*allocated < sizeof(*f) + qname->len) {
11364+ v = roundup_pow_of_two(*allocated + qname->len);
11365+ p = au_krealloc(f, v, GFP_NOFS, /*may_shrink*/0);
11366+ if (unlikely(!p)) {
11367+ err = -ENOMEM;
11368+ AuTraceErr(err);
11369+ goto out;
11370+ }
11371+ f = p;
11372+ *fdata = f;
11373+ *allocated = v;
11374+ drinfo = &f->drinfo;
11375+ }
11376+ memcpy(drinfo->oldname, qname->name, qname->len);
11377+ AuDbg("i%llu, %.*s\n",
11378+ be64_to_cpu((__force __be64)drinfo->ino), drinfo->oldnamelen,
11379+ drinfo->oldname);
11380+
11381+out:
11382+ AuTraceErr(err);
11383+ return err;
11384+}
11385+
11386+/* callers have to free the return value */
11387+static struct au_drinfo *au_drinfo_read_k(struct file *file, ino_t h_ino)
11388+{
11389+ struct au_drinfo *ret, *drinfo;
11390+ struct au_drinfo_fdata fdata;
11391+ int len;
11392+ loff_t pos;
11393+ ssize_t ssz;
11394+
11395+ ret = ERR_PTR(-EIO);
11396+ pos = 0;
11397+ ssz = vfsub_read_k(file, &fdata, sizeof(fdata), &pos);
11398+ if (unlikely(ssz != sizeof(fdata))) {
11399+ AuIOErr("ssz %zd, %u, %pD2\n",
11400+ ssz, (unsigned int)sizeof(fdata), file);
11401+ goto out;
11402+ }
11403+
11404+ fdata.magic = ntohl((__force __be32)fdata.magic);
11405+ switch (fdata.magic) {
11406+ case AUFS_DRINFO_MAGIC_V1:
11407+ break;
11408+ default:
11409+ AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
11410+ fdata.magic, AUFS_DRINFO_MAGIC_V1, file);
11411+ goto out;
11412+ }
11413+
11414+ drinfo = &fdata.drinfo;
11415+ len = drinfo->oldnamelen;
11416+ if (!len) {
11417+ AuIOErr("broken drinfo %pD2\n", file);
11418+ goto out;
11419+ }
11420+
11421+ ret = NULL;
11422+ drinfo->ino = be64_to_cpu((__force __be64)drinfo->ino);
11423+ if (unlikely(h_ino && drinfo->ino != h_ino)) {
11424+ AuDbg("ignored i%llu, i%llu, %pD2\n",
11425+ (unsigned long long)drinfo->ino,
11426+ (unsigned long long)h_ino, file);
11427+ goto out; /* success */
11428+ }
11429+
11430+ ret = kmalloc(sizeof(*ret) + len, GFP_NOFS);
11431+ if (unlikely(!ret)) {
11432+ ret = ERR_PTR(-ENOMEM);
11433+ AuTraceErrPtr(ret);
11434+ goto out;
11435+ }
11436+
11437+ *ret = *drinfo;
11438+ ssz = vfsub_read_k(file, (void *)ret->oldname, len, &pos);
11439+ if (unlikely(ssz != len)) {
11440+ au_kfree_rcu(ret);
11441+ ret = ERR_PTR(-EIO);
11442+ AuIOErr("ssz %zd, %u, %pD2\n", ssz, len, file);
11443+ goto out;
11444+ }
11445+
11446+ AuDbg("oldname %.*s\n", ret->oldnamelen, ret->oldname);
11447+
11448+out:
11449+ return ret;
11450+}
11451+
11452+/* ---------------------------------------------------------------------- */
11453+
11454+/* in order to be revertible */
11455+struct au_drinfo_rev_elm {
11456+ int created;
11457+ struct dentry *info_dentry;
11458+ struct au_drinfo *info_last;
11459+};
11460+
11461+struct au_drinfo_rev {
11462+ unsigned char already;
11463+ aufs_bindex_t nelm;
11464+ struct au_drinfo_rev_elm elm[];
11465+};
11466+
11467+/* todo: isn't it too large? */
11468+struct au_drinfo_store {
11469+ struct path h_ppath;
11470+ struct dentry *h_dentry;
11471+ struct au_drinfo_fdata *fdata;
11472+ char *infoname; /* inside of whname, just after PFX */
11473+ char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ];
11474+ aufs_bindex_t btgt, btail;
11475+ unsigned char no_sio,
11476+ allocated, /* current size of *fdata */
11477+ infonamelen, /* room size for p */
11478+ whnamelen, /* length of the generated name */
11479+ renameback; /* renamed back */
11480+};
11481+
11482+/* on rename(2) error, the caller should revert it using @elm */
11483+static int au_drinfo_do_store(struct au_drinfo_store *w,
11484+ struct au_drinfo_rev_elm *elm)
11485+{
11486+ int err, len;
11487+ ssize_t ssz;
11488+ loff_t pos;
11489+ struct path infopath = {
11490+ .mnt = w->h_ppath.mnt
11491+ };
11492+ struct inode *h_dir, *h_inode, *delegated;
11493+ struct file *infofile;
11494+ struct qstr *qname;
11495+
11496+ AuDebugOn(elm
11497+ && memcmp(elm, page_address(ZERO_PAGE(0)), sizeof(*elm)));
11498+
11499+ infopath.dentry = vfsub_lookup_one_len(w->whname, &w->h_ppath,
11500+ w->whnamelen);
11501+ AuTraceErrPtr(infopath.dentry);
11502+ if (IS_ERR(infopath.dentry)) {
11503+ err = PTR_ERR(infopath.dentry);
11504+ goto out;
11505+ }
11506+
11507+ err = 0;
11508+ h_dir = d_inode(w->h_ppath.dentry);
11509+ if (elm && d_is_negative(infopath.dentry)) {
11510+ err = vfsub_create(h_dir, &infopath, 0600, /*want_excl*/true);
11511+ AuTraceErr(err);
11512+ if (unlikely(err))
11513+ goto out_dput;
11514+ elm->created = 1;
11515+ elm->info_dentry = dget(infopath.dentry);
11516+ }
11517+
11518+ infofile = vfsub_dentry_open(&infopath, O_RDWR);
11519+ AuTraceErrPtr(infofile);
11520+ if (IS_ERR(infofile)) {
11521+ err = PTR_ERR(infofile);
11522+ goto out_dput;
11523+ }
11524+
11525+ h_inode = d_inode(infopath.dentry);
11526+ if (elm && i_size_read(h_inode)) {
11527+ h_inode = d_inode(w->h_dentry);
11528+ elm->info_last = au_drinfo_read_k(infofile, h_inode->i_ino);
11529+ AuTraceErrPtr(elm->info_last);
11530+ if (IS_ERR(elm->info_last)) {
11531+ err = PTR_ERR(elm->info_last);
11532+ elm->info_last = NULL;
11533+ AuDebugOn(elm->info_dentry);
11534+ goto out_fput;
11535+ }
11536+ }
11537+
11538+ if (elm && w->renameback) {
11539+ delegated = NULL;
11540+ err = vfsub_unlink(h_dir, &infopath, &delegated, /*force*/0);
11541+ AuTraceErr(err);
11542+ if (unlikely(err == -EWOULDBLOCK))
11543+ iput(delegated);
11544+ goto out_fput;
11545+ }
11546+
11547+ pos = 0;
11548+ qname = &w->h_dentry->d_name;
11549+ len = sizeof(*w->fdata) + qname->len;
11550+ if (!elm)
11551+ len = sizeof(*w->fdata) + w->fdata->drinfo.oldnamelen;
11552+ ssz = vfsub_write_k(infofile, w->fdata, len, &pos);
11553+ if (ssz == len) {
11554+ AuDbg("hi%llu, %.*s\n", w->fdata->drinfo.ino,
11555+ w->fdata->drinfo.oldnamelen, w->fdata->drinfo.oldname);
11556+ goto out_fput; /* success */
11557+ } else {
11558+ err = -EIO;
11559+ if (ssz < 0)
11560+ err = ssz;
11561+ /* the caller should revert it using @elm */
11562+ }
11563+
11564+out_fput:
11565+ fput(infofile);
11566+out_dput:
11567+ dput(infopath.dentry);
11568+out:
11569+ AuTraceErr(err);
11570+ return err;
11571+}
11572+
11573+struct au_call_drinfo_do_store_args {
11574+ int *errp;
11575+ struct au_drinfo_store *w;
11576+ struct au_drinfo_rev_elm *elm;
11577+};
11578+
11579+static void au_call_drinfo_do_store(void *args)
11580+{
11581+ struct au_call_drinfo_do_store_args *a = args;
11582+
11583+ *a->errp = au_drinfo_do_store(a->w, a->elm);
11584+}
11585+
11586+static int au_drinfo_store_sio(struct au_drinfo_store *w,
11587+ struct au_drinfo_rev_elm *elm)
11588+{
11589+ int err, wkq_err;
11590+
11591+ if (w->no_sio)
11592+ err = au_drinfo_do_store(w, elm);
11593+ else {
11594+ struct au_call_drinfo_do_store_args a = {
11595+ .errp = &err,
11596+ .w = w,
11597+ .elm = elm
11598+ };
11599+ wkq_err = au_wkq_wait(au_call_drinfo_do_store, &a);
11600+ if (unlikely(wkq_err))
11601+ err = wkq_err;
11602+ }
11603+ AuTraceErr(err);
11604+
11605+ return err;
11606+}
11607+
11608+static int au_drinfo_store_work_init(struct au_drinfo_store *w,
11609+ aufs_bindex_t btgt)
11610+{
11611+ int err;
11612+
11613+ memset(w, 0, sizeof(*w));
11614+ w->allocated = roundup_pow_of_two(sizeof(*w->fdata) + 40);
11615+ strcpy(w->whname, AUFS_WH_DR_INFO_PFX);
11616+ w->infoname = w->whname + sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11617+ w->infonamelen = sizeof(w->whname) - sizeof(AUFS_WH_DR_INFO_PFX);
11618+ w->btgt = btgt;
11619+ w->no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
11620+
11621+ err = -ENOMEM;
11622+ w->fdata = kcalloc(1, w->allocated, GFP_NOFS);
11623+ if (unlikely(!w->fdata)) {
11624+ AuTraceErr(err);
11625+ goto out;
11626+ }
11627+ w->fdata->magic = (__force uint32_t)htonl(AUFS_DRINFO_MAGIC_V1);
11628+ err = 0;
11629+
11630+out:
11631+ return err;
11632+}
11633+
11634+static void au_drinfo_store_work_fin(struct au_drinfo_store *w)
11635+{
11636+ au_kfree_rcu(w->fdata);
11637+}
11638+
11639+static void au_drinfo_store_rev(struct au_drinfo_rev *rev,
11640+ struct au_drinfo_store *w)
11641+{
11642+ struct au_drinfo_rev_elm *elm;
11643+ struct inode *h_dir, *delegated;
11644+ int err, nelm;
11645+ struct path infopath = {
11646+ .mnt = w->h_ppath.mnt
11647+ };
11648+
11649+ h_dir = d_inode(w->h_ppath.dentry);
11650+ IMustLock(h_dir);
11651+
11652+ err = 0;
11653+ elm = rev->elm;
11654+ for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11655+ AuDebugOn(elm->created && elm->info_last);
11656+ if (elm->created) {
11657+ AuDbg("here\n");
11658+ delegated = NULL;
11659+ infopath.dentry = elm->info_dentry;
11660+ err = vfsub_unlink(h_dir, &infopath, &delegated,
11661+ !w->no_sio);
11662+ AuTraceErr(err);
11663+ if (unlikely(err == -EWOULDBLOCK))
11664+ iput(delegated);
11665+ dput(elm->info_dentry);
11666+ } else if (elm->info_last) {
11667+ AuDbg("here\n");
11668+ w->fdata->drinfo = *elm->info_last;
11669+ memcpy(w->fdata->drinfo.oldname,
11670+ elm->info_last->oldname,
11671+ elm->info_last->oldnamelen);
11672+ err = au_drinfo_store_sio(w, /*elm*/NULL);
11673+ au_kfree_rcu(elm->info_last);
11674+ }
11675+ if (unlikely(err))
11676+ AuIOErr("%d, %s\n", err, w->whname);
11677+ /* go on even if err */
11678+ }
11679+}
11680+
11681+/* caller has to call au_dr_rename_fin() later */
11682+static int au_drinfo_store(struct dentry *dentry, aufs_bindex_t btgt,
11683+ struct qstr *dst_name, void *_rev)
11684+{
11685+ int err, sz, nelm;
11686+ aufs_bindex_t bindex, btail;
11687+ struct au_drinfo_store work;
11688+ struct au_drinfo_rev *rev, **p;
11689+ struct au_drinfo_rev_elm *elm;
11690+ struct super_block *sb;
11691+ struct au_branch *br;
11692+ struct au_hinode *hdir;
11693+
11694+ err = au_drinfo_store_work_init(&work, btgt);
11695+ AuTraceErr(err);
11696+ if (unlikely(err))
11697+ goto out;
11698+
11699+ err = -ENOMEM;
11700+ btail = au_dbtaildir(dentry);
11701+ nelm = btail - btgt;
11702+ sz = sizeof(*rev) + sizeof(*elm) * nelm;
11703+ rev = kcalloc(1, sz, GFP_NOFS);
11704+ if (unlikely(!rev)) {
11705+ AuTraceErr(err);
11706+ goto out_args;
11707+ }
11708+ rev->nelm = nelm;
11709+ elm = rev->elm;
11710+ p = _rev;
11711+ *p = rev;
11712+
11713+ err = 0;
11714+ sb = dentry->d_sb;
11715+ work.h_ppath.dentry = au_h_dptr(dentry, btgt);
11716+ work.h_ppath.mnt = au_sbr_mnt(sb, btgt);
11717+ hdir = au_hi(d_inode(dentry), btgt);
11718+ au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11719+ for (bindex = btgt + 1; bindex <= btail; bindex++, elm++) {
11720+ work.h_dentry = au_h_dptr(dentry, bindex);
11721+ if (!work.h_dentry)
11722+ continue;
11723+
11724+ err = au_drinfo_construct(&work.fdata, work.h_dentry,
11725+ &work.allocated);
11726+ AuTraceErr(err);
11727+ if (unlikely(err))
11728+ break;
11729+
11730+ work.renameback = au_qstreq(&work.h_dentry->d_name, dst_name);
11731+ br = au_sbr(sb, bindex);
11732+ work.whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11733+ work.whnamelen += au_drinfo_name(br, work.infoname,
11734+ work.infonamelen);
11735+ AuDbg("whname %.*s, i%llu, %.*s\n",
11736+ work.whnamelen, work.whname,
11737+ be64_to_cpu((__force __be64)work.fdata->drinfo.ino),
11738+ work.fdata->drinfo.oldnamelen,
11739+ work.fdata->drinfo.oldname);
11740+
11741+ err = au_drinfo_store_sio(&work, elm);
11742+ AuTraceErr(err);
11743+ if (unlikely(err))
11744+ break;
11745+ }
11746+ if (unlikely(err)) {
11747+ /* revert all drinfo */
11748+ au_drinfo_store_rev(rev, &work);
11749+ au_kfree_try_rcu(rev);
11750+ *p = NULL;
11751+ }
11752+ au_hn_inode_unlock(hdir);
11753+
11754+out_args:
11755+ au_drinfo_store_work_fin(&work);
11756+out:
11757+ return err;
11758+}
11759+
11760+/* ---------------------------------------------------------------------- */
11761+
11762+int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
11763+ struct qstr *dst_name, void *_rev)
11764+{
11765+ int err, already;
11766+ ino_t ino;
11767+ struct super_block *sb;
11768+ struct au_branch *br;
11769+ struct au_dr_br *dr;
11770+ struct dentry *h_dentry;
11771+ struct inode *h_inode;
11772+ struct au_dr_hino *ent;
11773+ struct au_drinfo_rev *rev, **p;
11774+
11775+ AuDbg("bindex %d\n", bindex);
11776+
11777+ err = -ENOMEM;
11778+ ent = kmalloc(sizeof(*ent), GFP_NOFS);
11779+ if (unlikely(!ent))
11780+ goto out;
11781+
11782+ sb = src->d_sb;
11783+ br = au_sbr(sb, bindex);
11784+ dr = &br->br_dirren;
11785+ h_dentry = au_h_dptr(src, bindex);
11786+ h_inode = d_inode(h_dentry);
11787+ ino = h_inode->i_ino;
11788+ ent->dr_h_ino = ino;
11789+ already = au_dr_hino_test_add(dr, ino, ent);
11790+ AuDbg("b%d, hi%llu, already %d\n",
11791+ bindex, (unsigned long long)ino, already);
11792+
11793+ err = au_drinfo_store(src, bindex, dst_name, _rev);
11794+ AuTraceErr(err);
11795+ if (!err) {
11796+ p = _rev;
11797+ rev = *p;
11798+ rev->already = already;
11799+ goto out; /* success */
11800+ }
11801+
11802+ /* revert */
11803+ if (!already)
11804+ au_dr_hino_del(dr, ent);
11805+ au_kfree_rcu(ent);
11806+
11807+out:
11808+ AuTraceErr(err);
11809+ return err;
11810+}
11811+
11812+void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11813+{
11814+ struct au_drinfo_rev *rev;
11815+ struct au_drinfo_rev_elm *elm;
11816+ int nelm;
11817+
11818+ rev = _rev;
11819+ elm = rev->elm;
11820+ for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11821+ dput(elm->info_dentry);
11822+ au_kfree_rcu(elm->info_last);
11823+ }
11824+ au_kfree_try_rcu(rev);
11825+}
11826+
11827+void au_dr_rename_rev(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11828+{
11829+ int err;
11830+ struct au_drinfo_store work;
11831+ struct au_drinfo_rev *rev = _rev;
11832+ struct super_block *sb;
11833+ struct au_branch *br;
11834+ struct inode *h_inode;
11835+ struct au_dr_br *dr;
11836+ struct au_dr_hino *ent;
11837+
11838+ err = au_drinfo_store_work_init(&work, btgt);
11839+ if (unlikely(err))
11840+ goto out;
11841+
11842+ sb = src->d_sb;
11843+ br = au_sbr(sb, btgt);
11844+ work.h_ppath.dentry = au_h_dptr(src, btgt);
11845+ work.h_ppath.mnt = au_br_mnt(br);
11846+ au_drinfo_store_rev(rev, &work);
11847+ au_drinfo_store_work_fin(&work);
11848+ if (rev->already)
11849+ goto out;
11850+
11851+ dr = &br->br_dirren;
11852+ h_inode = d_inode(work.h_ppath.dentry);
11853+ ent = au_dr_hino_find(dr, h_inode->i_ino);
11854+ BUG_ON(!ent);
11855+ au_dr_hino_del(dr, ent);
11856+ au_kfree_rcu(ent);
11857+
11858+out:
11859+ au_kfree_try_rcu(rev);
11860+ if (unlikely(err))
11861+ pr_err("failed to remove dirren info\n");
11862+}
11863+
11864+/* ---------------------------------------------------------------------- */
11865+
11866+static struct au_drinfo *au_drinfo_do_load(struct path *h_ppath,
11867+ char *whname, int whnamelen,
11868+ struct dentry **info_dentry)
11869+{
11870+ struct au_drinfo *drinfo;
11871+ struct file *f;
11872+ struct inode *h_dir;
11873+ struct path infopath;
11874+ int unlocked;
11875+
11876+ AuDbg("%pd/%.*s\n", h_ppath->dentry, whnamelen, whname);
11877+
11878+ *info_dentry = NULL;
11879+ drinfo = NULL;
11880+ unlocked = 0;
11881+ h_dir = d_inode(h_ppath->dentry);
11882+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
11883+ infopath.dentry = vfsub_lookup_one_len(whname, h_ppath, whnamelen);
11884+ if (IS_ERR(infopath.dentry)) {
11885+ drinfo = (void *)infopath.dentry;
11886+ goto out;
11887+ }
11888+
11889+ if (d_is_negative(infopath.dentry))
11890+ goto out_dput; /* success */
11891+
11892+ infopath.mnt = h_ppath->mnt;
11893+ f = vfsub_dentry_open(&infopath, O_RDONLY);
11894+ inode_unlock_shared(h_dir);
11895+ unlocked = 1;
11896+ if (IS_ERR(f)) {
11897+ drinfo = (void *)f;
11898+ goto out_dput;
11899+ }
11900+
11901+ drinfo = au_drinfo_read_k(f, /*h_ino*/0);
11902+ if (IS_ERR_OR_NULL(drinfo))
11903+ goto out_fput;
11904+
11905+ AuDbg("oldname %.*s\n", drinfo->oldnamelen, drinfo->oldname);
11906+ *info_dentry = dget(infopath.dentry); /* keep it alive */
11907+
11908+out_fput:
11909+ fput(f);
11910+out_dput:
11911+ dput(infopath.dentry);
11912+out:
11913+ if (!unlocked)
11914+ inode_unlock_shared(h_dir);
11915+ AuTraceErrPtr(drinfo);
11916+ return drinfo;
11917+}
11918+
11919+struct au_drinfo_do_load_args {
11920+ struct au_drinfo **drinfop;
11921+ struct path *h_ppath;
11922+ char *whname;
11923+ int whnamelen;
11924+ struct dentry **info_dentry;
11925+};
11926+
11927+static void au_call_drinfo_do_load(void *args)
11928+{
11929+ struct au_drinfo_do_load_args *a = args;
11930+
11931+ *a->drinfop = au_drinfo_do_load(a->h_ppath, a->whname, a->whnamelen,
11932+ a->info_dentry);
11933+}
11934+
11935+struct au_drinfo_load {
11936+ struct path h_ppath;
11937+ struct qstr *qname;
11938+ unsigned char no_sio;
11939+
11940+ aufs_bindex_t ninfo;
11941+ struct au_drinfo **drinfo;
11942+};
11943+
11944+static int au_drinfo_load(struct au_drinfo_load *w, aufs_bindex_t bindex,
11945+ struct au_branch *br)
11946+{
11947+ int err, wkq_err, whnamelen, e;
11948+ char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ]
11949+ = AUFS_WH_DR_INFO_PFX;
11950+ struct au_drinfo *drinfo;
11951+ struct qstr oldname;
11952+ struct inode *h_dir, *delegated;
11953+ struct dentry *info_dentry;
11954+ struct path infopath;
11955+
11956+ whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11957+ whnamelen += au_drinfo_name(br, whname + whnamelen,
11958+ sizeof(whname) - whnamelen);
11959+ if (w->no_sio)
11960+ drinfo = au_drinfo_do_load(&w->h_ppath, whname, whnamelen,
11961+ &info_dentry);
11962+ else {
11963+ struct au_drinfo_do_load_args args = {
11964+ .drinfop = &drinfo,
11965+ .h_ppath = &w->h_ppath,
11966+ .whname = whname,
11967+ .whnamelen = whnamelen,
11968+ .info_dentry = &info_dentry
11969+ };
11970+ wkq_err = au_wkq_wait(au_call_drinfo_do_load, &args);
11971+ if (unlikely(wkq_err))
11972+ drinfo = ERR_PTR(wkq_err);
11973+ }
11974+ err = PTR_ERR(drinfo);
11975+ if (IS_ERR_OR_NULL(drinfo))
11976+ goto out;
11977+
11978+ err = 0;
11979+ oldname.len = drinfo->oldnamelen;
11980+ oldname.name = drinfo->oldname;
11981+ if (au_qstreq(w->qname, &oldname)) {
11982+ /* the name is renamed back */
11983+ au_kfree_rcu(drinfo);
11984+ drinfo = NULL;
11985+
11986+ infopath.dentry = info_dentry;
11987+ infopath.mnt = w->h_ppath.mnt;
11988+ h_dir = d_inode(w->h_ppath.dentry);
11989+ delegated = NULL;
11990+ inode_lock_nested(h_dir, AuLsc_I_PARENT);
11991+ e = vfsub_unlink(h_dir, &infopath, &delegated, !w->no_sio);
11992+ inode_unlock(h_dir);
11993+ if (unlikely(e))
11994+ AuIOErr("ignored %d, %pd2\n", e, &infopath.dentry);
11995+ if (unlikely(e == -EWOULDBLOCK))
11996+ iput(delegated);
11997+ }
11998+ au_kfree_rcu(w->drinfo[bindex]);
11999+ w->drinfo[bindex] = drinfo;
12000+ dput(info_dentry);
12001+
12002+out:
12003+ AuTraceErr(err);
12004+ return err;
12005+}
12006+
12007+/* ---------------------------------------------------------------------- */
12008+
12009+static void au_dr_lkup_free(struct au_drinfo **drinfo, int n)
12010+{
12011+ struct au_drinfo **p = drinfo;
12012+
12013+ while (n-- > 0)
12014+ au_kfree_rcu(*drinfo++);
12015+ au_kfree_try_rcu(p);
12016+}
12017+
12018+int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12019+ aufs_bindex_t btgt)
12020+{
12021+ int err, ninfo;
12022+ struct au_drinfo_load w;
12023+ aufs_bindex_t bindex, bbot;
12024+ struct au_branch *br;
12025+ struct inode *h_dir;
12026+ struct au_dr_hino *ent;
12027+ struct super_block *sb;
12028+
12029+ AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
12030+ AuLNPair(&dentry->d_name), AuLNPair(&lkup->dirren.dr_name),
12031+ AuLNPair(&lkup->whname), btgt);
12032+
12033+ sb = dentry->d_sb;
12034+ bbot = au_sbbot(sb);
12035+ w.ninfo = bbot + 1;
12036+ if (!lkup->dirren.drinfo) {
12037+ lkup->dirren.drinfo = kcalloc(w.ninfo,
12038+ sizeof(*lkup->dirren.drinfo),
12039+ GFP_NOFS);
12040+ if (unlikely(!lkup->dirren.drinfo)) {
12041+ err = -ENOMEM;
12042+ goto out;
12043+ }
12044+ lkup->dirren.ninfo = w.ninfo;
12045+ }
12046+ w.drinfo = lkup->dirren.drinfo;
12047+ w.no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
12048+ w.h_ppath.dentry = au_h_dptr(dentry, btgt);
12049+ AuDebugOn(!w.h_ppath.dentry);
12050+ w.h_ppath.mnt = au_sbr_mnt(sb, btgt);
12051+ w.qname = &dentry->d_name;
12052+
12053+ ninfo = 0;
12054+ for (bindex = btgt + 1; bindex <= bbot; bindex++) {
12055+ br = au_sbr(sb, bindex);
12056+ err = au_drinfo_load(&w, bindex, br);
12057+ if (unlikely(err))
12058+ goto out_free;
12059+ if (w.drinfo[bindex])
12060+ ninfo++;
12061+ }
12062+ if (!ninfo) {
12063+ br = au_sbr(sb, btgt);
12064+ h_dir = d_inode(w.h_ppath.dentry);
12065+ ent = au_dr_hino_find(&br->br_dirren, h_dir->i_ino);
12066+ AuDebugOn(!ent);
12067+ au_dr_hino_del(&br->br_dirren, ent);
12068+ au_kfree_rcu(ent);
12069+ }
12070+ goto out; /* success */
12071+
12072+out_free:
12073+ au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12074+ lkup->dirren.ninfo = 0;
12075+ lkup->dirren.drinfo = NULL;
12076+out:
12077+ AuTraceErr(err);
12078+ return err;
12079+}
12080+
12081+void au_dr_lkup_fin(struct au_do_lookup_args *lkup)
12082+{
12083+ au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12084+}
12085+
12086+int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt)
12087+{
12088+ int err;
12089+ struct au_drinfo *drinfo;
12090+
12091+ err = 0;
12092+ if (!lkup->dirren.drinfo)
12093+ goto out;
12094+ AuDebugOn(lkup->dirren.ninfo <= btgt);
12095+ drinfo = lkup->dirren.drinfo[btgt];
12096+ if (!drinfo)
12097+ goto out;
12098+
12099+ au_kfree_try_rcu(lkup->whname.name);
12100+ lkup->whname.name = NULL;
12101+ lkup->dirren.dr_name.len = drinfo->oldnamelen;
12102+ lkup->dirren.dr_name.name = drinfo->oldname;
12103+ lkup->name = &lkup->dirren.dr_name;
12104+ err = au_wh_name_alloc(&lkup->whname, lkup->name);
12105+ if (!err)
12106+ AuDbg("name %.*s, whname %.*s, b%d\n",
12107+ AuLNPair(lkup->name), AuLNPair(&lkup->whname),
12108+ btgt);
12109+
12110+out:
12111+ AuTraceErr(err);
12112+ return err;
12113+}
12114+
12115+int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12116+ ino_t h_ino)
12117+{
12118+ int match;
12119+ struct au_drinfo *drinfo;
12120+
12121+ match = 1;
12122+ if (!lkup->dirren.drinfo)
12123+ goto out;
12124+ AuDebugOn(lkup->dirren.ninfo <= bindex);
12125+ drinfo = lkup->dirren.drinfo[bindex];
12126+ if (!drinfo)
12127+ goto out;
12128+
12129+ match = (drinfo->ino == h_ino);
12130+ AuDbg("match %d\n", match);
12131+
12132+out:
12133+ return match;
12134+}
12135+
12136+/* ---------------------------------------------------------------------- */
12137+
12138+int au_dr_opt_set(struct super_block *sb)
12139+{
12140+ int err;
12141+ aufs_bindex_t bindex, bbot;
12142+ struct au_branch *br;
12143+
12144+ err = 0;
12145+ bbot = au_sbbot(sb);
12146+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
12147+ br = au_sbr(sb, bindex);
12148+ err = au_dr_hino(sb, bindex, /*br*/NULL, &br->br_path);
12149+ }
12150+
12151+ return err;
12152+}
12153+
12154+int au_dr_opt_flush(struct super_block *sb)
12155+{
12156+ int err;
12157+ aufs_bindex_t bindex, bbot;
12158+ struct au_branch *br;
12159+
12160+ err = 0;
12161+ bbot = au_sbbot(sb);
12162+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
12163+ br = au_sbr(sb, bindex);
12164+ if (au_br_writable(br->br_perm))
12165+ err = au_dr_hino(sb, bindex, /*br*/NULL, /*path*/NULL);
12166+ }
12167+
12168+ return err;
12169+}
12170+
12171+int au_dr_opt_clr(struct super_block *sb, int no_flush)
12172+{
12173+ int err;
12174+ aufs_bindex_t bindex, bbot;
12175+ struct au_branch *br;
12176+
12177+ err = 0;
12178+ if (!no_flush) {
12179+ err = au_dr_opt_flush(sb);
12180+ if (unlikely(err))
12181+ goto out;
12182+ }
12183+
12184+ bbot = au_sbbot(sb);
12185+ for (bindex = 0; bindex <= bbot; bindex++) {
12186+ br = au_sbr(sb, bindex);
12187+ au_dr_hino_free(&br->br_dirren);
12188+ }
12189+
12190+out:
12191+ return err;
12192+}
12193diff -urN /usr/share/empty/fs/aufs/dirren.h linux/fs/aufs/dirren.h
12194--- /usr/share/empty/fs/aufs/dirren.h 1970-01-01 01:00:00.000000000 +0100
12195+++ linux/fs/aufs/dirren.h 2021-12-03 15:38:59.933313976 +0100
12196@@ -0,0 +1,140 @@
12197+/* SPDX-License-Identifier: GPL-2.0 */
12198+/*
12199+ * Copyright (C) 2017-2021 Junjiro R. Okajima
12200+ *
12201+ * This program, aufs is free software; you can redistribute it and/or modify
12202+ * it under the terms of the GNU General Public License as published by
12203+ * the Free Software Foundation; either version 2 of the License, or
12204+ * (at your option) any later version.
12205+ *
12206+ * This program is distributed in the hope that it will be useful,
12207+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12208+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12209+ * GNU General Public License for more details.
12210+ *
12211+ * You should have received a copy of the GNU General Public License
12212+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
12213+ */
12214+
12215+/*
12216+ * renamed dir info
12217+ */
12218+
12219+#ifndef __AUFS_DIRREN_H__
12220+#define __AUFS_DIRREN_H__
12221+
12222+#ifdef __KERNEL__
12223+
12224+#include <linux/dcache.h>
12225+#include <linux/statfs.h>
12226+#include <linux/uuid.h>
12227+#include "hbl.h"
12228+
12229+#define AuDirren_NHASH 100
12230+
12231+#ifdef CONFIG_AUFS_DIRREN
12232+enum au_brid_type {
12233+ AuBrid_Unset,
12234+ AuBrid_UUID,
12235+ AuBrid_FSID,
12236+ AuBrid_DEV
12237+};
12238+
12239+struct au_dr_brid {
12240+ enum au_brid_type type;
12241+ union {
12242+ uuid_t uuid; /* unimplemented yet */
12243+ fsid_t fsid;
12244+ dev_t dev;
12245+ };
12246+};
12247+
12248+/* 20 is the max digits length of ulong 64 */
12249+/* brid-type "_" uuid "_" inum */
12250+#define AUFS_DIRREN_FNAME_SZ (1 + 1 + UUID_STRING_LEN + 20)
12251+#define AUFS_DIRREN_ENV_VAL_SZ (AUFS_DIRREN_FNAME_SZ + 1 + 20)
12252+
12253+struct au_dr_hino {
12254+ struct hlist_bl_node dr_hnode;
12255+ ino_t dr_h_ino;
12256+};
12257+
12258+struct au_dr_br {
12259+ struct hlist_bl_head dr_h_ino[AuDirren_NHASH];
12260+ struct au_dr_brid dr_brid;
12261+};
12262+
12263+struct au_dr_lookup {
12264+ /* dr_name is pointed by struct au_do_lookup_args.name */
12265+ struct qstr dr_name; /* subset of dr_info */
12266+ aufs_bindex_t ninfo;
12267+ struct au_drinfo **drinfo;
12268+};
12269+#else
12270+struct au_dr_hino;
12271+/* empty */
12272+struct au_dr_br { };
12273+struct au_dr_lookup { };
12274+#endif
12275+
12276+/* ---------------------------------------------------------------------- */
12277+
12278+struct au_branch;
12279+struct au_do_lookup_args;
12280+struct au_hinode;
12281+#ifdef CONFIG_AUFS_DIRREN
12282+int au_dr_hino_test_add(struct au_dr_br *dr, ino_t h_ino,
12283+ struct au_dr_hino *add_ent);
12284+void au_dr_hino_free(struct au_dr_br *dr);
12285+int au_dr_br_init(struct super_block *sb, struct au_branch *br,
12286+ const struct path *path);
12287+int au_dr_br_fin(struct super_block *sb, struct au_branch *br);
12288+int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
12289+ struct qstr *dst_name, void *_rev);
12290+void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *rev);
12291+void au_dr_rename_rev(struct dentry *src, aufs_bindex_t bindex, void *rev);
12292+int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12293+ aufs_bindex_t bindex);
12294+int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12295+int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12296+ ino_t h_ino);
12297+void au_dr_lkup_fin(struct au_do_lookup_args *lkup);
12298+int au_dr_opt_set(struct super_block *sb);
12299+int au_dr_opt_flush(struct super_block *sb);
12300+int au_dr_opt_clr(struct super_block *sb, int no_flush);
12301+#else
12302+AuStubInt0(au_dr_hino_test_add, struct au_dr_br *dr, ino_t h_ino,
12303+ struct au_dr_hino *add_ent);
12304+AuStubVoid(au_dr_hino_free, struct au_dr_br *dr);
12305+AuStubInt0(au_dr_br_init, struct super_block *sb, struct au_branch *br,
12306+ const struct path *path);
12307+AuStubInt0(au_dr_br_fin, struct super_block *sb, struct au_branch *br);
12308+AuStubInt0(au_dr_rename, struct dentry *src, aufs_bindex_t bindex,
12309+ struct qstr *dst_name, void *_rev);
12310+AuStubVoid(au_dr_rename_fin, struct dentry *src, aufs_bindex_t btgt, void *rev);
12311+AuStubVoid(au_dr_rename_rev, struct dentry *src, aufs_bindex_t bindex,
12312+ void *rev);
12313+AuStubInt0(au_dr_lkup, struct au_do_lookup_args *lkup, struct dentry *dentry,
12314+ aufs_bindex_t bindex);
12315+AuStubInt0(au_dr_lkup_name, struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12316+AuStubInt0(au_dr_lkup_h_ino, struct au_do_lookup_args *lkup,
12317+ aufs_bindex_t bindex, ino_t h_ino);
12318+AuStubVoid(au_dr_lkup_fin, struct au_do_lookup_args *lkup);
12319+AuStubInt0(au_dr_opt_set, struct super_block *sb);
12320+AuStubInt0(au_dr_opt_flush, struct super_block *sb);
12321+AuStubInt0(au_dr_opt_clr, struct super_block *sb, int no_flush);
12322+#endif
12323+
12324+/* ---------------------------------------------------------------------- */
12325+
12326+#ifdef CONFIG_AUFS_DIRREN
12327+static inline int au_dr_ihash(ino_t h_ino)
12328+{
12329+ return h_ino % AuDirren_NHASH;
12330+}
12331+#else
12332+AuStubInt0(au_dr_ihash, ino_t h_ino);
12333+#endif
12334+
12335+#endif /* __KERNEL__ */
12336+#endif /* __AUFS_DIRREN_H__ */
12337diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
12338--- /usr/share/empty/fs/aufs/dynop.c 1970-01-01 01:00:00.000000000 +0100
12339+++ linux/fs/aufs/dynop.c 2021-12-03 15:38:59.933313976 +0100
12340@@ -0,0 +1,368 @@
12341+// SPDX-License-Identifier: GPL-2.0
12342+/*
12343+ * Copyright (C) 2010-2021 Junjiro R. Okajima
12344+ *
12345+ * This program, aufs is free software; you can redistribute it and/or modify
12346+ * it under the terms of the GNU General Public License as published by
12347+ * the Free Software Foundation; either version 2 of the License, or
12348+ * (at your option) any later version.
12349+ *
12350+ * This program is distributed in the hope that it will be useful,
12351+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12352+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12353+ * GNU General Public License for more details.
12354+ *
12355+ * You should have received a copy of the GNU General Public License
12356+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
12357+ */
12358+
12359+/*
12360+ * dynamically customizable operations for regular files
12361+ */
12362+
12363+#include "aufs.h"
12364+
12365+#define DyPrSym(key) AuDbgSym(key->dk_op.dy_hop)
12366+
12367+/*
12368+ * How large will these lists be?
12369+ * Usually just a few elements, 20-30 at most for each, I guess.
12370+ */
12371+static struct hlist_bl_head dynop[AuDyLast];
12372+
12373+static struct au_dykey *dy_gfind_get(struct hlist_bl_head *hbl,
12374+ const void *h_op)
12375+{
12376+ struct au_dykey *key, *tmp;
12377+ struct hlist_bl_node *pos;
12378+
12379+ key = NULL;
12380+ hlist_bl_lock(hbl);
12381+ hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12382+ if (tmp->dk_op.dy_hop == h_op) {
12383+ if (kref_get_unless_zero(&tmp->dk_kref))
12384+ key = tmp;
12385+ break;
12386+ }
12387+ hlist_bl_unlock(hbl);
12388+
12389+ return key;
12390+}
12391+
12392+static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
12393+{
12394+ struct au_dykey **k, *found;
12395+ const void *h_op = key->dk_op.dy_hop;
12396+ int i;
12397+
12398+ found = NULL;
12399+ k = br->br_dykey;
12400+ for (i = 0; i < AuBrDynOp; i++)
12401+ if (k[i]) {
12402+ if (k[i]->dk_op.dy_hop == h_op) {
12403+ found = k[i];
12404+ break;
12405+ }
12406+ } else
12407+ break;
12408+ if (!found) {
12409+ spin_lock(&br->br_dykey_lock);
12410+ for (; i < AuBrDynOp; i++)
12411+ if (k[i]) {
12412+ if (k[i]->dk_op.dy_hop == h_op) {
12413+ found = k[i];
12414+ break;
12415+ }
12416+ } else {
12417+ k[i] = key;
12418+ break;
12419+ }
12420+ spin_unlock(&br->br_dykey_lock);
12421+ BUG_ON(i == AuBrDynOp); /* expand the array */
12422+ }
12423+
12424+ return found;
12425+}
12426+
12427+/* kref_get() if @key is already added */
12428+static struct au_dykey *dy_gadd(struct hlist_bl_head *hbl, struct au_dykey *key)
12429+{
12430+ struct au_dykey *tmp, *found;
12431+ struct hlist_bl_node *pos;
12432+ const void *h_op = key->dk_op.dy_hop;
12433+
12434+ found = NULL;
12435+ hlist_bl_lock(hbl);
12436+ hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12437+ if (tmp->dk_op.dy_hop == h_op) {
12438+ if (kref_get_unless_zero(&tmp->dk_kref))
12439+ found = tmp;
12440+ break;
12441+ }
12442+ if (!found)
12443+ hlist_bl_add_head(&key->dk_hnode, hbl);
12444+ hlist_bl_unlock(hbl);
12445+
12446+ if (!found)
12447+ DyPrSym(key);
12448+ return found;
12449+}
12450+
12451+static void dy_free_rcu(struct rcu_head *rcu)
12452+{
12453+ struct au_dykey *key;
12454+
12455+ key = container_of(rcu, struct au_dykey, dk_rcu);
12456+ DyPrSym(key);
12457+ kfree(key);
12458+}
12459+
12460+static void dy_free(struct kref *kref)
12461+{
12462+ struct au_dykey *key;
12463+ struct hlist_bl_head *hbl;
12464+
12465+ key = container_of(kref, struct au_dykey, dk_kref);
12466+ hbl = dynop + key->dk_op.dy_type;
12467+ au_hbl_del(&key->dk_hnode, hbl);
12468+ call_rcu(&key->dk_rcu, dy_free_rcu);
12469+}
12470+
12471+void au_dy_put(struct au_dykey *key)
12472+{
12473+ kref_put(&key->dk_kref, dy_free);
12474+}
12475+
12476+/* ---------------------------------------------------------------------- */
12477+
12478+#define DyDbgSize(cnt, op) AuDebugOn(cnt != sizeof(op)/sizeof(void *))
12479+
12480+#ifdef CONFIG_AUFS_DEBUG
12481+#define DyDbgDeclare(cnt) unsigned int cnt = 0
12482+#define DyDbgInc(cnt) do { cnt++; } while (0)
12483+#else
12484+#define DyDbgDeclare(cnt) do {} while (0)
12485+#define DyDbgInc(cnt) do {} while (0)
12486+#endif
12487+
12488+#define DySet(func, dst, src, h_op, h_sb) do { \
12489+ DyDbgInc(cnt); \
12490+ if (h_op->func) { \
12491+ if (src.func) \
12492+ dst.func = src.func; \
12493+ else \
12494+ AuDbg("%s %s\n", au_sbtype(h_sb), #func); \
12495+ } \
12496+} while (0)
12497+
12498+#define DySetForce(func, dst, src) do { \
12499+ AuDebugOn(!src.func); \
12500+ DyDbgInc(cnt); \
12501+ dst.func = src.func; \
12502+} while (0)
12503+
12504+#define DySetAop(func) \
12505+ DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
12506+#define DySetAopForce(func) \
12507+ DySetForce(func, dyaop->da_op, aufs_aop)
12508+
12509+static void dy_aop(struct au_dykey *key, const void *h_op,
12510+ struct super_block *h_sb __maybe_unused)
12511+{
12512+ struct au_dyaop *dyaop = (void *)key;
12513+ const struct address_space_operations *h_aop = h_op;
12514+ DyDbgDeclare(cnt);
12515+
12516+ AuDbg("%s\n", au_sbtype(h_sb));
12517+
12518+ DySetAop(writepage);
12519+ DySetAopForce(readpage); /* force */
12520+ DySetAop(writepages);
12521+ DySetAop(set_page_dirty);
12522+ DySetAop(readpages);
12523+ DySetAop(readahead);
12524+ DySetAop(write_begin);
12525+ DySetAop(write_end);
12526+ DySetAop(bmap);
12527+ DySetAop(invalidatepage);
12528+ DySetAop(releasepage);
12529+ DySetAop(freepage);
12530+ /* this one will be changed according to an aufs mount option */
12531+ DySetAop(direct_IO);
12532+ DySetAop(migratepage);
12533+ DySetAop(isolate_page);
12534+ DySetAop(putback_page);
12535+ DySetAop(launder_page);
12536+ DySetAop(is_partially_uptodate);
12537+ DySetAop(is_dirty_writeback);
12538+ DySetAop(error_remove_page);
12539+ DySetAop(swap_activate);
12540+ DySetAop(swap_deactivate);
12541+
12542+ DyDbgSize(cnt, *h_aop);
12543+}
12544+
12545+/* ---------------------------------------------------------------------- */
12546+
12547+static void dy_bug(struct kref *kref)
12548+{
12549+ BUG();
12550+}
12551+
12552+static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
12553+{
12554+ struct au_dykey *key, *old;
12555+ struct hlist_bl_head *hbl;
12556+ struct op {
12557+ unsigned int sz;
12558+ void (*set)(struct au_dykey *key, const void *h_op,
12559+ struct super_block *h_sb __maybe_unused);
12560+ };
12561+ static const struct op a[] = {
12562+ [AuDy_AOP] = {
12563+ .sz = sizeof(struct au_dyaop),
12564+ .set = dy_aop
12565+ }
12566+ };
12567+ const struct op *p;
12568+
12569+ hbl = dynop + op->dy_type;
12570+ key = dy_gfind_get(hbl, op->dy_hop);
12571+ if (key)
12572+ goto out_add; /* success */
12573+
12574+ p = a + op->dy_type;
12575+ key = kzalloc(p->sz, GFP_NOFS);
12576+ if (unlikely(!key)) {
12577+ key = ERR_PTR(-ENOMEM);
12578+ goto out;
12579+ }
12580+
12581+ key->dk_op.dy_hop = op->dy_hop;
12582+ kref_init(&key->dk_kref);
12583+ p->set(key, op->dy_hop, au_br_sb(br));
12584+ old = dy_gadd(hbl, key);
12585+ if (old) {
12586+ au_kfree_rcu(key);
12587+ key = old;
12588+ }
12589+
12590+out_add:
12591+ old = dy_bradd(br, key);
12592+ if (old)
12593+ /* its ref-count should never be zero here */
12594+ kref_put(&key->dk_kref, dy_bug);
12595+out:
12596+ return key;
12597+}
12598+
12599+/* ---------------------------------------------------------------------- */
12600+/*
12601+ * Aufs prohibits O_DIRECT by default even if the branch supports it.
12602+ * This behaviour is necessary to return an error from open(O_DIRECT) instead
12603+ * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
12604+ * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
12605+ * See the aufs manual in detail.
12606+ */
12607+static void dy_adx(struct au_dyaop *dyaop, int do_dx)
12608+{
12609+ if (!do_dx)
12610+ dyaop->da_op.direct_IO = NULL;
12611+ else
12612+ dyaop->da_op.direct_IO = aufs_aop.direct_IO;
12613+}
12614+
12615+static struct au_dyaop *dy_aget(struct au_branch *br,
12616+ const struct address_space_operations *h_aop,
12617+ int do_dx)
12618+{
12619+ struct au_dyaop *dyaop;
12620+ struct au_dynop op;
12621+
12622+ op.dy_type = AuDy_AOP;
12623+ op.dy_haop = h_aop;
12624+ dyaop = (void *)dy_get(&op, br);
12625+ if (IS_ERR(dyaop))
12626+ goto out;
12627+ dy_adx(dyaop, do_dx);
12628+
12629+out:
12630+ return dyaop;
12631+}
12632+
12633+int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12634+ struct inode *h_inode)
12635+{
12636+ int err, do_dx;
12637+ struct super_block *sb;
12638+ struct au_branch *br;
12639+ struct au_dyaop *dyaop;
12640+
12641+ AuDebugOn(!S_ISREG(h_inode->i_mode));
12642+ IiMustWriteLock(inode);
12643+
12644+ sb = inode->i_sb;
12645+ br = au_sbr(sb, bindex);
12646+ do_dx = !!au_opt_test(au_mntflags(sb), DIO);
12647+ dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
12648+ err = PTR_ERR(dyaop);
12649+ if (IS_ERR(dyaop))
12650+ /* unnecessary to call dy_fput() */
12651+ goto out;
12652+
12653+ err = 0;
12654+ inode->i_mapping->a_ops = &dyaop->da_op;
12655+
12656+out:
12657+ return err;
12658+}
12659+
12660+/*
12661+ * Is it safe to replace a_ops during the inode/file is in operation?
12662+ * Yes, I hope so.
12663+ */
12664+int au_dy_irefresh(struct inode *inode)
12665+{
12666+ int err;
12667+ aufs_bindex_t btop;
12668+ struct inode *h_inode;
12669+
12670+ err = 0;
12671+ if (S_ISREG(inode->i_mode)) {
12672+ btop = au_ibtop(inode);
12673+ h_inode = au_h_iptr(inode, btop);
12674+ err = au_dy_iaop(inode, btop, h_inode);
12675+ }
12676+ return err;
12677+}
12678+
12679+void au_dy_arefresh(int do_dx)
12680+{
12681+ struct hlist_bl_head *hbl;
12682+ struct hlist_bl_node *pos;
12683+ struct au_dykey *key;
12684+
12685+ hbl = dynop + AuDy_AOP;
12686+ hlist_bl_lock(hbl);
12687+ hlist_bl_for_each_entry(key, pos, hbl, dk_hnode)
12688+ dy_adx((void *)key, do_dx);
12689+ hlist_bl_unlock(hbl);
12690+}
12691+
12692+/* ---------------------------------------------------------------------- */
12693+
12694+void __init au_dy_init(void)
12695+{
12696+ int i;
12697+
12698+ for (i = 0; i < AuDyLast; i++)
12699+ INIT_HLIST_BL_HEAD(dynop + i);
12700+}
12701+
12702+void au_dy_fin(void)
12703+{
12704+ int i;
12705+
12706+ for (i = 0; i < AuDyLast; i++)
12707+ WARN_ON(!hlist_bl_empty(dynop + i));
12708+}
12709diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
12710--- /usr/share/empty/fs/aufs/dynop.h 1970-01-01 01:00:00.000000000 +0100
12711+++ linux/fs/aufs/dynop.h 2021-12-03 15:38:59.933313976 +0100
12712@@ -0,0 +1,77 @@
12713+/* SPDX-License-Identifier: GPL-2.0 */
12714+/*
12715+ * Copyright (C) 2010-2021 Junjiro R. Okajima
12716+ *
12717+ * This program, aufs is free software; you can redistribute it and/or modify
12718+ * it under the terms of the GNU General Public License as published by
12719+ * the Free Software Foundation; either version 2 of the License, or
12720+ * (at your option) any later version.
12721+ *
12722+ * This program is distributed in the hope that it will be useful,
12723+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12724+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12725+ * GNU General Public License for more details.
12726+ *
12727+ * You should have received a copy of the GNU General Public License
12728+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
12729+ */
12730+
12731+/*
12732+ * dynamically customizable operations (for regular files only)
12733+ */
12734+
12735+#ifndef __AUFS_DYNOP_H__
12736+#define __AUFS_DYNOP_H__
12737+
12738+#ifdef __KERNEL__
12739+
12740+#include <linux/fs.h>
12741+#include <linux/kref.h>
12742+
12743+enum {AuDy_AOP, AuDyLast};
12744+
12745+struct au_dynop {
12746+ int dy_type;
12747+ union {
12748+ const void *dy_hop;
12749+ const struct address_space_operations *dy_haop;
12750+ };
12751+};
12752+
12753+struct au_dykey {
12754+ union {
12755+ struct hlist_bl_node dk_hnode;
12756+ struct rcu_head dk_rcu;
12757+ };
12758+ struct au_dynop dk_op;
12759+
12760+ /*
12761+ * during I am in the branch local array, kref is gotten. when the
12762+ * branch is removed, kref is put.
12763+ */
12764+ struct kref dk_kref;
12765+};
12766+
12767+/* stop unioning since their sizes are very different from each other */
12768+struct au_dyaop {
12769+ struct au_dykey da_key;
12770+ struct address_space_operations da_op; /* not const */
12771+};
12772+/* make sure that 'struct au_dykey *' can be any type */
12773+static_assert(!offsetof(struct au_dyaop, da_key));
12774+
12775+/* ---------------------------------------------------------------------- */
12776+
12777+/* dynop.c */
12778+struct au_branch;
12779+void au_dy_put(struct au_dykey *key);
12780+int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12781+ struct inode *h_inode);
12782+int au_dy_irefresh(struct inode *inode);
12783+void au_dy_arefresh(int do_dio);
12784+
12785+void __init au_dy_init(void);
12786+void au_dy_fin(void);
12787+
12788+#endif /* __KERNEL__ */
12789+#endif /* __AUFS_DYNOP_H__ */
12790diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
12791--- /usr/share/empty/fs/aufs/export.c 1970-01-01 01:00:00.000000000 +0100
12792+++ linux/fs/aufs/export.c 2021-12-03 15:40:58.233313963 +0100
12793@@ -0,0 +1,831 @@
12794+// SPDX-License-Identifier: GPL-2.0
12795+/*
12796+ * Copyright (C) 2005-2021 Junjiro R. Okajima
12797+ *
12798+ * This program, aufs is free software; you can redistribute it and/or modify
12799+ * it under the terms of the GNU General Public License as published by
12800+ * the Free Software Foundation; either version 2 of the License, or
12801+ * (at your option) any later version.
12802+ *
12803+ * This program is distributed in the hope that it will be useful,
12804+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12805+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12806+ * GNU General Public License for more details.
12807+ *
12808+ * You should have received a copy of the GNU General Public License
12809+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
12810+ */
12811+
12812+/*
12813+ * export via nfs
12814+ */
12815+
12816+#include <linux/exportfs.h>
12817+#include <linux/fs_struct.h>
12818+#include <linux/namei.h>
12819+#include <linux/nsproxy.h>
12820+#include <linux/random.h>
12821+#include <linux/writeback.h>
12822+#include "aufs.h"
12823+
12824+union conv {
12825+#ifdef CONFIG_AUFS_INO_T_64
12826+ __u32 a[2];
12827+#else
12828+ __u32 a[1];
12829+#endif
12830+ ino_t ino;
12831+};
12832+
12833+static ino_t decode_ino(__u32 *a)
12834+{
12835+ union conv u;
12836+
12837+ BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
12838+ u.a[0] = a[0];
12839+#ifdef CONFIG_AUFS_INO_T_64
12840+ u.a[1] = a[1];
12841+#endif
12842+ return u.ino;
12843+}
12844+
12845+static void encode_ino(__u32 *a, ino_t ino)
12846+{
12847+ union conv u;
12848+
12849+ u.ino = ino;
12850+ a[0] = u.a[0];
12851+#ifdef CONFIG_AUFS_INO_T_64
12852+ a[1] = u.a[1];
12853+#endif
12854+}
12855+
12856+/* NFS file handle */
12857+enum {
12858+ Fh_br_id,
12859+ Fh_sigen,
12860+#ifdef CONFIG_AUFS_INO_T_64
12861+ /* support 64bit inode number */
12862+ Fh_ino1,
12863+ Fh_ino2,
12864+ Fh_dir_ino1,
12865+ Fh_dir_ino2,
12866+#else
12867+ Fh_ino1,
12868+ Fh_dir_ino1,
12869+#endif
12870+ Fh_igen,
12871+ Fh_h_type,
12872+ Fh_tail,
12873+
12874+ Fh_ino = Fh_ino1,
12875+ Fh_dir_ino = Fh_dir_ino1
12876+};
12877+
12878+static int au_test_anon(struct dentry *dentry)
12879+{
12880+ /* note: read d_flags without d_lock */
12881+ return !!(dentry->d_flags & DCACHE_DISCONNECTED);
12882+}
12883+
12884+int au_test_nfsd(void)
12885+{
12886+ int ret;
12887+ struct task_struct *tsk = current;
12888+ char comm[sizeof(tsk->comm)];
12889+
12890+ ret = 0;
12891+ if (tsk->flags & PF_KTHREAD) {
12892+ get_task_comm(comm, tsk);
12893+ ret = !strcmp(comm, "nfsd");
12894+ }
12895+
12896+ return ret;
12897+}
12898+
12899+/* ---------------------------------------------------------------------- */
12900+/* inode generation external table */
12901+
12902+void au_xigen_inc(struct inode *inode)
12903+{
12904+ loff_t pos;
12905+ ssize_t sz;
12906+ __u32 igen;
12907+ struct super_block *sb;
12908+ struct au_sbinfo *sbinfo;
12909+
12910+ sb = inode->i_sb;
12911+ AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
12912+
12913+ sbinfo = au_sbi(sb);
12914+ pos = inode->i_ino;
12915+ pos *= sizeof(igen);
12916+ igen = inode->i_generation + 1;
12917+ sz = xino_fwrite(sbinfo->si_xigen, &igen, sizeof(igen), &pos);
12918+ if (sz == sizeof(igen))
12919+ return; /* success */
12920+
12921+ if (unlikely(sz >= 0))
12922+ AuIOErr("xigen error (%zd)\n", sz);
12923+}
12924+
12925+int au_xigen_new(struct inode *inode)
12926+{
12927+ int err;
12928+ loff_t pos;
12929+ ssize_t sz;
12930+ struct super_block *sb;
12931+ struct au_sbinfo *sbinfo;
12932+ struct file *file;
12933+
12934+ err = 0;
12935+ /* todo: dirty, at mount time */
12936+ if (inode->i_ino == AUFS_ROOT_INO)
12937+ goto out;
12938+ sb = inode->i_sb;
12939+ SiMustAnyLock(sb);
12940+ if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
12941+ goto out;
12942+
12943+ err = -EFBIG;
12944+ pos = inode->i_ino;
12945+ if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
12946+ AuIOErr1("too large i%lld\n", pos);
12947+ goto out;
12948+ }
12949+ pos *= sizeof(inode->i_generation);
12950+
12951+ err = 0;
12952+ sbinfo = au_sbi(sb);
12953+ file = sbinfo->si_xigen;
12954+ BUG_ON(!file);
12955+
12956+ if (vfsub_f_size_read(file)
12957+ < pos + sizeof(inode->i_generation)) {
12958+ inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
12959+ sz = xino_fwrite(file, &inode->i_generation,
12960+ sizeof(inode->i_generation), &pos);
12961+ } else
12962+ sz = xino_fread(file, &inode->i_generation,
12963+ sizeof(inode->i_generation), &pos);
12964+ if (sz == sizeof(inode->i_generation))
12965+ goto out; /* success */
12966+
12967+ err = sz;
12968+ if (unlikely(sz >= 0)) {
12969+ err = -EIO;
12970+ AuIOErr("xigen error (%zd)\n", sz);
12971+ }
12972+
12973+out:
12974+ return err;
12975+}
12976+
12977+int au_xigen_set(struct super_block *sb, struct path *path)
12978+{
12979+ int err;
12980+ struct au_sbinfo *sbinfo;
12981+ struct file *file;
12982+
12983+ SiMustWriteLock(sb);
12984+
12985+ sbinfo = au_sbi(sb);
12986+ file = au_xino_create2(sb, path, sbinfo->si_xigen);
12987+ err = PTR_ERR(file);
12988+ if (IS_ERR(file))
12989+ goto out;
12990+ err = 0;
12991+ if (sbinfo->si_xigen)
12992+ fput(sbinfo->si_xigen);
12993+ sbinfo->si_xigen = file;
12994+
12995+out:
12996+ AuTraceErr(err);
12997+ return err;
12998+}
12999+
13000+void au_xigen_clr(struct super_block *sb)
13001+{
13002+ struct au_sbinfo *sbinfo;
13003+
13004+ SiMustWriteLock(sb);
13005+
13006+ sbinfo = au_sbi(sb);
13007+ if (sbinfo->si_xigen) {
13008+ fput(sbinfo->si_xigen);
13009+ sbinfo->si_xigen = NULL;
13010+ }
13011+}
13012+
13013+/* ---------------------------------------------------------------------- */
13014+
13015+static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
13016+ ino_t dir_ino)
13017+{
13018+ struct dentry *dentry, *d;
13019+ struct inode *inode;
13020+ unsigned int sigen;
13021+
13022+ dentry = NULL;
13023+ inode = ilookup(sb, ino);
13024+ if (!inode)
13025+ goto out;
13026+
13027+ dentry = ERR_PTR(-ESTALE);
13028+ sigen = au_sigen(sb);
13029+ if (unlikely(au_is_bad_inode(inode)
13030+ || IS_DEADDIR(inode)
13031+ || sigen != au_iigen(inode, NULL)))
13032+ goto out_iput;
13033+
13034+ dentry = NULL;
13035+ if (!dir_ino || S_ISDIR(inode->i_mode))
13036+ dentry = d_find_alias(inode);
13037+ else {
13038+ spin_lock(&inode->i_lock);
13039+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
13040+ spin_lock(&d->d_lock);
13041+ if (!au_test_anon(d)
13042+ && d_inode(d->d_parent)->i_ino == dir_ino) {
13043+ dentry = dget_dlock(d);
13044+ spin_unlock(&d->d_lock);
13045+ break;
13046+ }
13047+ spin_unlock(&d->d_lock);
13048+ }
13049+ spin_unlock(&inode->i_lock);
13050+ }
13051+ if (unlikely(dentry && au_digen_test(dentry, sigen))) {
13052+ /* need to refresh */
13053+ dput(dentry);
13054+ dentry = NULL;
13055+ }
13056+
13057+out_iput:
13058+ iput(inode);
13059+out:
13060+ AuTraceErrPtr(dentry);
13061+ return dentry;
13062+}
13063+
13064+/* ---------------------------------------------------------------------- */
13065+
13066+/* todo: dirty? */
13067+/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
13068+
13069+struct au_compare_mnt_args {
13070+ /* input */
13071+ struct super_block *sb;
13072+
13073+ /* output */
13074+ struct vfsmount *mnt;
13075+};
13076+
13077+static int au_compare_mnt(struct vfsmount *mnt, void *arg)
13078+{
13079+ struct au_compare_mnt_args *a = arg;
13080+
13081+ if (mnt->mnt_sb != a->sb)
13082+ return 0;
13083+ a->mnt = mntget(mnt);
13084+ return 1;
13085+}
13086+
13087+static struct vfsmount *au_mnt_get(struct super_block *sb)
13088+{
13089+ int err;
13090+ struct path root;
13091+ struct au_compare_mnt_args args = {
13092+ .sb = sb
13093+ };
13094+
13095+ get_fs_root(current->fs, &root);
13096+ rcu_read_lock();
13097+ err = iterate_mounts(au_compare_mnt, &args, root.mnt);
13098+ rcu_read_unlock();
13099+ path_put(&root);
13100+ AuDebugOn(!err);
13101+ AuDebugOn(!args.mnt);
13102+ return args.mnt;
13103+}
13104+
13105+struct au_nfsd_si_lock {
13106+ unsigned int sigen;
13107+ aufs_bindex_t bindex, br_id;
13108+ unsigned char force_lock;
13109+};
13110+
13111+static int si_nfsd_read_lock(struct super_block *sb,
13112+ struct au_nfsd_si_lock *nsi_lock)
13113+{
13114+ int err;
13115+ aufs_bindex_t bindex;
13116+
13117+ si_read_lock(sb, AuLock_FLUSH);
13118+
13119+ /* branch id may be wrapped around */
13120+ err = 0;
13121+ bindex = au_br_index(sb, nsi_lock->br_id);
13122+ if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
13123+ goto out; /* success */
13124+
13125+ err = -ESTALE;
13126+ bindex = -1;
13127+ if (!nsi_lock->force_lock)
13128+ si_read_unlock(sb);
13129+
13130+out:
13131+ nsi_lock->bindex = bindex;
13132+ return err;
13133+}
13134+
13135+struct find_name_by_ino {
13136+ struct dir_context ctx;
13137+ int called, found;
13138+ ino_t ino;
13139+ char *name;
13140+ int namelen;
13141+};
13142+
13143+static int
13144+find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
13145+ loff_t offset, u64 ino, unsigned int d_type)
13146+{
13147+ struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
13148+ ctx);
13149+
13150+ a->called++;
13151+ if (a->ino != ino)
13152+ return 0;
13153+
13154+ memcpy(a->name, name, namelen);
13155+ a->namelen = namelen;
13156+ a->found = 1;
13157+ return 1;
13158+}
13159+
13160+static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
13161+ struct au_nfsd_si_lock *nsi_lock)
13162+{
13163+ struct dentry *dentry, *parent;
13164+ struct file *file;
13165+ struct inode *dir;
13166+ struct find_name_by_ino arg = {
13167+ .ctx = {
13168+ .actor = find_name_by_ino
13169+ }
13170+ };
13171+ int err;
13172+
13173+ parent = path->dentry;
13174+ if (nsi_lock)
13175+ si_read_unlock(parent->d_sb);
13176+ file = vfsub_dentry_open(path, au_dir_roflags);
13177+ dentry = (void *)file;
13178+ if (IS_ERR(file))
13179+ goto out;
13180+
13181+ dentry = ERR_PTR(-ENOMEM);
13182+ arg.name = (void *)__get_free_page(GFP_NOFS);
13183+ if (unlikely(!arg.name))
13184+ goto out_file;
13185+ arg.ino = ino;
13186+ arg.found = 0;
13187+ do {
13188+ arg.called = 0;
13189+ /* smp_mb(); */
13190+ err = vfsub_iterate_dir(file, &arg.ctx);
13191+ } while (!err && !arg.found && arg.called);
13192+ dentry = ERR_PTR(err);
13193+ if (unlikely(err))
13194+ goto out_name;
13195+ /* instead of ENOENT */
13196+ dentry = ERR_PTR(-ESTALE);
13197+ if (!arg.found)
13198+ goto out_name;
13199+
13200+ /* do not call vfsub_lkup_one() */
13201+ dir = d_inode(parent);
13202+ dentry = vfsub_lookup_one_len_unlocked(arg.name, path, arg.namelen);
13203+ AuTraceErrPtr(dentry);
13204+ if (IS_ERR(dentry))
13205+ goto out_name;
13206+ AuDebugOn(au_test_anon(dentry));
13207+ if (unlikely(d_really_is_negative(dentry))) {
13208+ dput(dentry);
13209+ dentry = ERR_PTR(-ENOENT);
13210+ }
13211+
13212+out_name:
13213+ free_page((unsigned long)arg.name);
13214+out_file:
13215+ fput(file);
13216+out:
13217+ if (unlikely(nsi_lock
13218+ && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
13219+ if (!IS_ERR(dentry)) {
13220+ dput(dentry);
13221+ dentry = ERR_PTR(-ESTALE);
13222+ }
13223+ AuTraceErrPtr(dentry);
13224+ return dentry;
13225+}
13226+
13227+static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
13228+ ino_t dir_ino,
13229+ struct au_nfsd_si_lock *nsi_lock)
13230+{
13231+ struct dentry *dentry;
13232+ struct path path;
13233+
13234+ if (dir_ino != AUFS_ROOT_INO) {
13235+ path.dentry = decode_by_ino(sb, dir_ino, 0);
13236+ dentry = path.dentry;
13237+ if (!path.dentry || IS_ERR(path.dentry))
13238+ goto out;
13239+ AuDebugOn(au_test_anon(path.dentry));
13240+ } else
13241+ path.dentry = dget(sb->s_root);
13242+
13243+ path.mnt = au_mnt_get(sb);
13244+ dentry = au_lkup_by_ino(&path, ino, nsi_lock);
13245+ path_put(&path);
13246+
13247+out:
13248+ AuTraceErrPtr(dentry);
13249+ return dentry;
13250+}
13251+
13252+/* ---------------------------------------------------------------------- */
13253+
13254+static int h_acceptable(void *expv, struct dentry *dentry)
13255+{
13256+ return 1;
13257+}
13258+
13259+static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
13260+ char *buf, int len, struct super_block *sb)
13261+{
13262+ char *p;
13263+ int n;
13264+ struct path path;
13265+
13266+ p = d_path(h_rootpath, buf, len);
13267+ if (IS_ERR(p))
13268+ goto out;
13269+ n = strlen(p);
13270+
13271+ path.mnt = h_rootpath->mnt;
13272+ path.dentry = h_parent;
13273+ p = d_path(&path, buf, len);
13274+ if (IS_ERR(p))
13275+ goto out;
13276+ if (n != 1)
13277+ p += n;
13278+
13279+ path.mnt = au_mnt_get(sb);
13280+ path.dentry = sb->s_root;
13281+ p = d_path(&path, buf, len - strlen(p));
13282+ mntput(path.mnt);
13283+ if (IS_ERR(p))
13284+ goto out;
13285+ if (n != 1)
13286+ p[strlen(p)] = '/';
13287+
13288+out:
13289+ AuTraceErrPtr(p);
13290+ return p;
13291+}
13292+
13293+static
13294+struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
13295+ int fh_len, struct au_nfsd_si_lock *nsi_lock)
13296+{
13297+ struct dentry *dentry, *h_parent, *root;
13298+ struct super_block *h_sb;
13299+ char *pathname, *p;
13300+ struct vfsmount *h_mnt;
13301+ struct au_branch *br;
13302+ int err;
13303+ struct path path;
13304+
13305+ br = au_sbr(sb, nsi_lock->bindex);
13306+ h_mnt = au_br_mnt(br);
13307+ h_sb = h_mnt->mnt_sb;
13308+ /* todo: call lower fh_to_dentry()? fh_to_parent()? */
13309+ lockdep_off();
13310+ h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
13311+ fh_len - Fh_tail, fh[Fh_h_type],
13312+ h_acceptable, /*context*/NULL);
13313+ lockdep_on();
13314+ dentry = h_parent;
13315+ if (unlikely(!h_parent || IS_ERR(h_parent))) {
13316+ AuWarn1("%s decode_fh failed, %ld\n",
13317+ au_sbtype(h_sb), PTR_ERR(h_parent));
13318+ goto out;
13319+ }
13320+ dentry = NULL;
13321+ if (unlikely(au_test_anon(h_parent))) {
13322+ AuWarn1("%s decode_fh returned a disconnected dentry\n",
13323+ au_sbtype(h_sb));
13324+ goto out_h_parent;
13325+ }
13326+
13327+ dentry = ERR_PTR(-ENOMEM);
13328+ pathname = (void *)__get_free_page(GFP_NOFS);
13329+ if (unlikely(!pathname))
13330+ goto out_h_parent;
13331+
13332+ root = sb->s_root;
13333+ path.mnt = h_mnt;
13334+ di_read_lock_parent(root, !AuLock_IR);
13335+ path.dentry = au_h_dptr(root, nsi_lock->bindex);
13336+ di_read_unlock(root, !AuLock_IR);
13337+ p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
13338+ dentry = (void *)p;
13339+ if (IS_ERR(p))
13340+ goto out_pathname;
13341+
13342+ si_read_unlock(sb);
13343+ err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
13344+ dentry = ERR_PTR(err);
13345+ if (unlikely(err))
13346+ goto out_relock;
13347+
13348+ dentry = ERR_PTR(-ENOENT);
13349+ AuDebugOn(au_test_anon(path.dentry));
13350+ if (unlikely(d_really_is_negative(path.dentry)))
13351+ goto out_path;
13352+
13353+ if (ino != d_inode(path.dentry)->i_ino)
13354+ dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
13355+ else
13356+ dentry = dget(path.dentry);
13357+
13358+out_path:
13359+ path_put(&path);
13360+out_relock:
13361+ if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
13362+ if (!IS_ERR(dentry)) {
13363+ dput(dentry);
13364+ dentry = ERR_PTR(-ESTALE);
13365+ }
13366+out_pathname:
13367+ free_page((unsigned long)pathname);
13368+out_h_parent:
13369+ dput(h_parent);
13370+out:
13371+ AuTraceErrPtr(dentry);
13372+ return dentry;
13373+}
13374+
13375+/* ---------------------------------------------------------------------- */
13376+
13377+static struct dentry *
13378+aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
13379+ int fh_type)
13380+{
13381+ struct dentry *dentry;
13382+ __u32 *fh = fid->raw;
13383+ struct au_branch *br;
13384+ ino_t ino, dir_ino;
13385+ struct au_nfsd_si_lock nsi_lock = {
13386+ .force_lock = 0
13387+ };
13388+
13389+ dentry = ERR_PTR(-ESTALE);
13390+ /* it should never happen, but the file handle is unreliable */
13391+ if (unlikely(fh_len < Fh_tail))
13392+ goto out;
13393+ nsi_lock.sigen = fh[Fh_sigen];
13394+ nsi_lock.br_id = fh[Fh_br_id];
13395+
13396+ /* branch id may be wrapped around */
13397+ br = NULL;
13398+ if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
13399+ goto out;
13400+ nsi_lock.force_lock = 1;
13401+
13402+ /* is this inode still cached? */
13403+ ino = decode_ino(fh + Fh_ino);
13404+ /* it should never happen */
13405+ if (unlikely(ino == AUFS_ROOT_INO))
13406+ goto out_unlock;
13407+
13408+ dir_ino = decode_ino(fh + Fh_dir_ino);
13409+ dentry = decode_by_ino(sb, ino, dir_ino);
13410+ if (IS_ERR(dentry))
13411+ goto out_unlock;
13412+ if (dentry)
13413+ goto accept;
13414+
13415+ /* is the parent dir cached? */
13416+ br = au_sbr(sb, nsi_lock.bindex);
13417+ au_lcnt_inc(&br->br_nfiles);
13418+ dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
13419+ if (IS_ERR(dentry))
13420+ goto out_unlock;
13421+ if (dentry)
13422+ goto accept;
13423+
13424+ /* lookup path */
13425+ dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
13426+ if (IS_ERR(dentry))
13427+ goto out_unlock;
13428+ if (unlikely(!dentry))
13429+ /* todo?: make it ESTALE */
13430+ goto out_unlock;
13431+
13432+accept:
13433+ if (!au_digen_test(dentry, au_sigen(sb))
13434+ && d_inode(dentry)->i_generation == fh[Fh_igen])
13435+ goto out_unlock; /* success */
13436+
13437+ dput(dentry);
13438+ dentry = ERR_PTR(-ESTALE);
13439+out_unlock:
13440+ if (br)
13441+ au_lcnt_dec(&br->br_nfiles);
13442+ si_read_unlock(sb);
13443+out:
13444+ AuTraceErrPtr(dentry);
13445+ return dentry;
13446+}
13447+
13448+#if 0 /* reserved for future use */
13449+/* support subtreecheck option */
13450+static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
13451+ int fh_len, int fh_type)
13452+{
13453+ struct dentry *parent;
13454+ __u32 *fh = fid->raw;
13455+ ino_t dir_ino;
13456+
13457+ dir_ino = decode_ino(fh + Fh_dir_ino);
13458+ parent = decode_by_ino(sb, dir_ino, 0);
13459+ if (IS_ERR(parent))
13460+ goto out;
13461+ if (!parent)
13462+ parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
13463+ dir_ino, fh, fh_len);
13464+
13465+out:
13466+ AuTraceErrPtr(parent);
13467+ return parent;
13468+}
13469+#endif
13470+
13471+/* ---------------------------------------------------------------------- */
13472+
13473+static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
13474+ struct inode *dir)
13475+{
13476+ int err;
13477+ aufs_bindex_t bindex;
13478+ struct super_block *sb, *h_sb;
13479+ struct dentry *dentry, *parent, *h_parent;
13480+ struct inode *h_dir;
13481+ struct au_branch *br;
13482+
13483+ err = -ENOSPC;
13484+ if (unlikely(*max_len <= Fh_tail)) {
13485+ AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
13486+ goto out;
13487+ }
13488+
13489+ err = FILEID_ROOT;
13490+ if (inode->i_ino == AUFS_ROOT_INO) {
13491+ AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
13492+ goto out;
13493+ }
13494+
13495+ h_parent = NULL;
13496+ sb = inode->i_sb;
13497+ err = si_read_lock(sb, AuLock_FLUSH);
13498+ if (unlikely(err))
13499+ goto out;
13500+
13501+#ifdef CONFIG_AUFS_DEBUG
13502+ if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13503+ AuWarn1("NFS-exporting requires xino\n");
13504+#endif
13505+ err = -EIO;
13506+ parent = NULL;
13507+ ii_read_lock_child(inode);
13508+ bindex = au_ibtop(inode);
13509+ if (!dir) {
13510+ dentry = d_find_any_alias(inode);
13511+ if (unlikely(!dentry))
13512+ goto out_unlock;
13513+ AuDebugOn(au_test_anon(dentry));
13514+ parent = dget_parent(dentry);
13515+ dput(dentry);
13516+ if (unlikely(!parent))
13517+ goto out_unlock;
13518+ if (d_really_is_positive(parent))
13519+ dir = d_inode(parent);
13520+ }
13521+
13522+ ii_read_lock_parent(dir);
13523+ h_dir = au_h_iptr(dir, bindex);
13524+ ii_read_unlock(dir);
13525+ if (unlikely(!h_dir))
13526+ goto out_parent;
13527+ h_parent = d_find_any_alias(h_dir);
13528+ if (unlikely(!h_parent))
13529+ goto out_hparent;
13530+
13531+ err = -EPERM;
13532+ br = au_sbr(sb, bindex);
13533+ h_sb = au_br_sb(br);
13534+ if (unlikely(!h_sb->s_export_op)) {
13535+ AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
13536+ goto out_hparent;
13537+ }
13538+
13539+ fh[Fh_br_id] = br->br_id;
13540+ fh[Fh_sigen] = au_sigen(sb);
13541+ encode_ino(fh + Fh_ino, inode->i_ino);
13542+ encode_ino(fh + Fh_dir_ino, dir->i_ino);
13543+ fh[Fh_igen] = inode->i_generation;
13544+
13545+ *max_len -= Fh_tail;
13546+ fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
13547+ max_len,
13548+ /*connectable or subtreecheck*/0);
13549+ err = fh[Fh_h_type];
13550+ *max_len += Fh_tail;
13551+ /* todo: macros? */
13552+ if (err != FILEID_INVALID)
13553+ err = 99;
13554+ else
13555+ AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
13556+
13557+out_hparent:
13558+ dput(h_parent);
13559+out_parent:
13560+ dput(parent);
13561+out_unlock:
13562+ ii_read_unlock(inode);
13563+ si_read_unlock(sb);
13564+out:
13565+ if (unlikely(err < 0))
13566+ err = FILEID_INVALID;
13567+ return err;
13568+}
13569+
13570+/* ---------------------------------------------------------------------- */
13571+
13572+static int aufs_commit_metadata(struct inode *inode)
13573+{
13574+ int err;
13575+ aufs_bindex_t bindex;
13576+ struct super_block *sb;
13577+ struct inode *h_inode;
13578+ int (*f)(struct inode *inode);
13579+
13580+ sb = inode->i_sb;
13581+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13582+ ii_write_lock_child(inode);
13583+ bindex = au_ibtop(inode);
13584+ AuDebugOn(bindex < 0);
13585+ h_inode = au_h_iptr(inode, bindex);
13586+
13587+ f = h_inode->i_sb->s_export_op->commit_metadata;
13588+ if (f)
13589+ err = f(h_inode);
13590+ else
13591+ err = sync_inode_metadata(h_inode, /*wait*/1);
13592+
13593+ au_cpup_attr_timesizes(inode);
13594+ ii_write_unlock(inode);
13595+ si_read_unlock(sb);
13596+ return err;
13597+}
13598+
13599+/* ---------------------------------------------------------------------- */
13600+
13601+static struct export_operations aufs_export_op = {
13602+ .fh_to_dentry = aufs_fh_to_dentry,
13603+ /* .fh_to_parent = aufs_fh_to_parent, */
13604+ .encode_fh = aufs_encode_fh,
13605+ .commit_metadata = aufs_commit_metadata
13606+};
13607+
13608+void au_export_init(struct super_block *sb)
13609+{
13610+ struct au_sbinfo *sbinfo;
13611+ __u32 u;
13612+
13613+ BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
13614+ && IS_MODULE(CONFIG_EXPORTFS),
13615+ AUFS_NAME ": unsupported configuration "
13616+ "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
13617+
13618+ sb->s_export_op = &aufs_export_op;
13619+ sbinfo = au_sbi(sb);
13620+ sbinfo->si_xigen = NULL;
13621+ get_random_bytes(&u, sizeof(u));
13622+ BUILD_BUG_ON(sizeof(u) != sizeof(int));
13623+ atomic_set(&sbinfo->si_xigen_next, u);
13624+}
13625diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
13626--- /usr/share/empty/fs/aufs/fhsm.c 1970-01-01 01:00:00.000000000 +0100
13627+++ linux/fs/aufs/fhsm.c 2021-12-03 15:38:59.933313976 +0100
13628@@ -0,0 +1,427 @@
13629+// SPDX-License-Identifier: GPL-2.0
13630+/*
13631+ * Copyright (C) 2011-2021 Junjiro R. Okajima
13632+ *
13633+ * This program, aufs is free software; you can redistribute it and/or modify
13634+ * it under the terms of the GNU General Public License as published by
13635+ * the Free Software Foundation; either version 2 of the License, or
13636+ * (at your option) any later version.
13637+ *
13638+ * This program is distributed in the hope that it will be useful,
13639+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13640+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13641+ * GNU General Public License for more details.
13642+ *
13643+ * You should have received a copy of the GNU General Public License
13644+ * along with this program; if not, write to the Free Software
13645+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
13646+ */
13647+
13648+/*
13649+ * File-based Hierarchy Storage Management
13650+ */
13651+
13652+#include <linux/anon_inodes.h>
13653+#include <linux/poll.h>
13654+#include <linux/seq_file.h>
13655+#include <linux/statfs.h>
13656+#include "aufs.h"
13657+
13658+static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
13659+{
13660+ struct au_sbinfo *sbinfo;
13661+ struct au_fhsm *fhsm;
13662+
13663+ SiMustAnyLock(sb);
13664+
13665+ sbinfo = au_sbi(sb);
13666+ fhsm = &sbinfo->si_fhsm;
13667+ AuDebugOn(!fhsm);
13668+ return fhsm->fhsm_bottom;
13669+}
13670+
13671+void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
13672+{
13673+ struct au_sbinfo *sbinfo;
13674+ struct au_fhsm *fhsm;
13675+
13676+ SiMustWriteLock(sb);
13677+
13678+ sbinfo = au_sbi(sb);
13679+ fhsm = &sbinfo->si_fhsm;
13680+ AuDebugOn(!fhsm);
13681+ fhsm->fhsm_bottom = bindex;
13682+}
13683+
13684+/* ---------------------------------------------------------------------- */
13685+
13686+static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
13687+{
13688+ struct au_br_fhsm *bf;
13689+
13690+ bf = br->br_fhsm;
13691+ MtxMustLock(&bf->bf_lock);
13692+
13693+ return !bf->bf_readable
13694+ || time_after(jiffies,
13695+ bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
13696+}
13697+
13698+/* ---------------------------------------------------------------------- */
13699+
13700+static void au_fhsm_notify(struct super_block *sb, int val)
13701+{
13702+ struct au_sbinfo *sbinfo;
13703+ struct au_fhsm *fhsm;
13704+
13705+ SiMustAnyLock(sb);
13706+
13707+ sbinfo = au_sbi(sb);
13708+ fhsm = &sbinfo->si_fhsm;
13709+ if (au_fhsm_pid(fhsm)
13710+ && atomic_read(&fhsm->fhsm_readable) != -1) {
13711+ atomic_set(&fhsm->fhsm_readable, val);
13712+ if (val)
13713+ wake_up(&fhsm->fhsm_wqh);
13714+ }
13715+}
13716+
13717+static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
13718+ struct aufs_stfs *rstfs, int do_lock, int do_notify)
13719+{
13720+ int err;
13721+ struct au_branch *br;
13722+ struct au_br_fhsm *bf;
13723+
13724+ br = au_sbr(sb, bindex);
13725+ AuDebugOn(au_br_rdonly(br));
13726+ bf = br->br_fhsm;
13727+ AuDebugOn(!bf);
13728+
13729+ if (do_lock)
13730+ mutex_lock(&bf->bf_lock);
13731+ else
13732+ MtxMustLock(&bf->bf_lock);
13733+
13734+ /* sb->s_root for NFS is unreliable */
13735+ err = au_br_stfs(br, &bf->bf_stfs);
13736+ if (unlikely(err)) {
13737+ AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
13738+ goto out;
13739+ }
13740+
13741+ bf->bf_jiffy = jiffies;
13742+ bf->bf_readable = 1;
13743+ if (do_notify)
13744+ au_fhsm_notify(sb, /*val*/1);
13745+ if (rstfs)
13746+ *rstfs = bf->bf_stfs;
13747+
13748+out:
13749+ if (do_lock)
13750+ mutex_unlock(&bf->bf_lock);
13751+ au_fhsm_notify(sb, /*val*/1);
13752+
13753+ return err;
13754+}
13755+
13756+void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
13757+{
13758+ int err;
13759+ struct au_sbinfo *sbinfo;
13760+ struct au_fhsm *fhsm;
13761+ struct au_branch *br;
13762+ struct au_br_fhsm *bf;
13763+
13764+ AuDbg("b%d, force %d\n", bindex, force);
13765+ SiMustAnyLock(sb);
13766+
13767+ sbinfo = au_sbi(sb);
13768+ fhsm = &sbinfo->si_fhsm;
13769+ if (!au_ftest_si(sbinfo, FHSM)
13770+ || fhsm->fhsm_bottom == bindex)
13771+ return;
13772+
13773+ br = au_sbr(sb, bindex);
13774+ bf = br->br_fhsm;
13775+ AuDebugOn(!bf);
13776+ mutex_lock(&bf->bf_lock);
13777+ if (force
13778+ || au_fhsm_pid(fhsm)
13779+ || au_fhsm_test_jiffy(sbinfo, br))
13780+ err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
13781+ /*do_notify*/1);
13782+ mutex_unlock(&bf->bf_lock);
13783+}
13784+
13785+void au_fhsm_wrote_all(struct super_block *sb, int force)
13786+{
13787+ aufs_bindex_t bindex, bbot;
13788+ struct au_branch *br;
13789+
13790+ /* exclude the bottom */
13791+ bbot = au_fhsm_bottom(sb);
13792+ for (bindex = 0; bindex < bbot; bindex++) {
13793+ br = au_sbr(sb, bindex);
13794+ if (au_br_fhsm(br->br_perm))
13795+ au_fhsm_wrote(sb, bindex, force);
13796+ }
13797+}
13798+
13799+/* ---------------------------------------------------------------------- */
13800+
13801+static __poll_t au_fhsm_poll(struct file *file, struct poll_table_struct *wait)
13802+{
13803+ __poll_t mask;
13804+ struct au_sbinfo *sbinfo;
13805+ struct au_fhsm *fhsm;
13806+
13807+ mask = 0;
13808+ sbinfo = file->private_data;
13809+ fhsm = &sbinfo->si_fhsm;
13810+ poll_wait(file, &fhsm->fhsm_wqh, wait);
13811+ if (atomic_read(&fhsm->fhsm_readable))
13812+ mask = EPOLLIN /* | EPOLLRDNORM */;
13813+
13814+ if (!mask)
13815+ AuDbg("mask 0x%x\n", mask);
13816+ return mask;
13817+}
13818+
13819+static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
13820+ struct aufs_stfs *stfs, __s16 brid)
13821+{
13822+ int err;
13823+
13824+ err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
13825+ if (!err)
13826+ err = __put_user(brid, &stbr->brid);
13827+ if (unlikely(err))
13828+ err = -EFAULT;
13829+
13830+ return err;
13831+}
13832+
13833+static ssize_t au_fhsm_do_read(struct super_block *sb,
13834+ struct aufs_stbr __user *stbr, size_t count)
13835+{
13836+ ssize_t err;
13837+ int nstbr;
13838+ aufs_bindex_t bindex, bbot;
13839+ struct au_branch *br;
13840+ struct au_br_fhsm *bf;
13841+
13842+ /* except the bottom branch */
13843+ err = 0;
13844+ nstbr = 0;
13845+ bbot = au_fhsm_bottom(sb);
13846+ for (bindex = 0; !err && bindex < bbot; bindex++) {
13847+ br = au_sbr(sb, bindex);
13848+ if (!au_br_fhsm(br->br_perm))
13849+ continue;
13850+
13851+ bf = br->br_fhsm;
13852+ mutex_lock(&bf->bf_lock);
13853+ if (bf->bf_readable) {
13854+ err = -EFAULT;
13855+ if (count >= sizeof(*stbr))
13856+ err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
13857+ br->br_id);
13858+ if (!err) {
13859+ bf->bf_readable = 0;
13860+ count -= sizeof(*stbr);
13861+ nstbr++;
13862+ }
13863+ }
13864+ mutex_unlock(&bf->bf_lock);
13865+ }
13866+ if (!err)
13867+ err = sizeof(*stbr) * nstbr;
13868+
13869+ return err;
13870+}
13871+
13872+static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
13873+ loff_t *pos)
13874+{
13875+ ssize_t err;
13876+ int readable;
13877+ aufs_bindex_t nfhsm, bindex, bbot;
13878+ struct au_sbinfo *sbinfo;
13879+ struct au_fhsm *fhsm;
13880+ struct au_branch *br;
13881+ struct super_block *sb;
13882+
13883+ err = 0;
13884+ sbinfo = file->private_data;
13885+ fhsm = &sbinfo->si_fhsm;
13886+need_data:
13887+ spin_lock_irq(&fhsm->fhsm_wqh.lock);
13888+ if (!atomic_read(&fhsm->fhsm_readable)) {
13889+ if (vfsub_file_flags(file) & O_NONBLOCK)
13890+ err = -EAGAIN;
13891+ else
13892+ err = wait_event_interruptible_locked_irq
13893+ (fhsm->fhsm_wqh,
13894+ atomic_read(&fhsm->fhsm_readable));
13895+ }
13896+ spin_unlock_irq(&fhsm->fhsm_wqh.lock);
13897+ if (unlikely(err))
13898+ goto out;
13899+
13900+ /* sb may already be dead */
13901+ au_rw_read_lock(&sbinfo->si_rwsem);
13902+ readable = atomic_read(&fhsm->fhsm_readable);
13903+ if (readable > 0) {
13904+ sb = sbinfo->si_sb;
13905+ AuDebugOn(!sb);
13906+ /* exclude the bottom branch */
13907+ nfhsm = 0;
13908+ bbot = au_fhsm_bottom(sb);
13909+ for (bindex = 0; bindex < bbot; bindex++) {
13910+ br = au_sbr(sb, bindex);
13911+ if (au_br_fhsm(br->br_perm))
13912+ nfhsm++;
13913+ }
13914+ err = -EMSGSIZE;
13915+ if (nfhsm * sizeof(struct aufs_stbr) <= count) {
13916+ atomic_set(&fhsm->fhsm_readable, 0);
13917+ err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
13918+ count);
13919+ }
13920+ }
13921+ au_rw_read_unlock(&sbinfo->si_rwsem);
13922+ if (!readable)
13923+ goto need_data;
13924+
13925+out:
13926+ return err;
13927+}
13928+
13929+static int au_fhsm_release(struct inode *inode, struct file *file)
13930+{
13931+ struct au_sbinfo *sbinfo;
13932+ struct au_fhsm *fhsm;
13933+
13934+ /* sb may already be dead */
13935+ sbinfo = file->private_data;
13936+ fhsm = &sbinfo->si_fhsm;
13937+ spin_lock(&fhsm->fhsm_spin);
13938+ fhsm->fhsm_pid = 0;
13939+ spin_unlock(&fhsm->fhsm_spin);
13940+ kobject_put(&sbinfo->si_kobj);
13941+
13942+ return 0;
13943+}
13944+
13945+static const struct file_operations au_fhsm_fops = {
13946+ .owner = THIS_MODULE,
13947+ .llseek = noop_llseek,
13948+ .read = au_fhsm_read,
13949+ .poll = au_fhsm_poll,
13950+ .release = au_fhsm_release
13951+};
13952+
13953+int au_fhsm_fd(struct super_block *sb, int oflags)
13954+{
13955+ int err, fd;
13956+ struct au_sbinfo *sbinfo;
13957+ struct au_fhsm *fhsm;
13958+
13959+ err = -EPERM;
13960+ if (unlikely(!capable(CAP_SYS_ADMIN)))
13961+ goto out;
13962+
13963+ err = -EINVAL;
13964+ if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
13965+ goto out;
13966+
13967+ err = 0;
13968+ sbinfo = au_sbi(sb);
13969+ fhsm = &sbinfo->si_fhsm;
13970+ spin_lock(&fhsm->fhsm_spin);
13971+ if (!fhsm->fhsm_pid)
13972+ fhsm->fhsm_pid = current->pid;
13973+ else
13974+ err = -EBUSY;
13975+ spin_unlock(&fhsm->fhsm_spin);
13976+ if (unlikely(err))
13977+ goto out;
13978+
13979+ oflags |= O_RDONLY;
13980+ /* oflags |= FMODE_NONOTIFY; */
13981+ fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
13982+ err = fd;
13983+ if (unlikely(fd < 0))
13984+ goto out_pid;
13985+
13986+ /* succeed regardless 'fhsm' status */
13987+ kobject_get(&sbinfo->si_kobj);
13988+ si_noflush_read_lock(sb);
13989+ if (au_ftest_si(sbinfo, FHSM))
13990+ au_fhsm_wrote_all(sb, /*force*/0);
13991+ si_read_unlock(sb);
13992+ goto out; /* success */
13993+
13994+out_pid:
13995+ spin_lock(&fhsm->fhsm_spin);
13996+ fhsm->fhsm_pid = 0;
13997+ spin_unlock(&fhsm->fhsm_spin);
13998+out:
13999+ AuTraceErr(err);
14000+ return err;
14001+}
14002+
14003+/* ---------------------------------------------------------------------- */
14004+
14005+int au_fhsm_br_alloc(struct au_branch *br)
14006+{
14007+ int err;
14008+
14009+ err = 0;
14010+ br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
14011+ if (br->br_fhsm)
14012+ au_br_fhsm_init(br->br_fhsm);
14013+ else
14014+ err = -ENOMEM;
14015+
14016+ return err;
14017+}
14018+
14019+/* ---------------------------------------------------------------------- */
14020+
14021+void au_fhsm_fin(struct super_block *sb)
14022+{
14023+ au_fhsm_notify(sb, /*val*/-1);
14024+}
14025+
14026+void au_fhsm_init(struct au_sbinfo *sbinfo)
14027+{
14028+ struct au_fhsm *fhsm;
14029+
14030+ fhsm = &sbinfo->si_fhsm;
14031+ spin_lock_init(&fhsm->fhsm_spin);
14032+ init_waitqueue_head(&fhsm->fhsm_wqh);
14033+ atomic_set(&fhsm->fhsm_readable, 0);
14034+ fhsm->fhsm_expire
14035+ = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
14036+ fhsm->fhsm_bottom = -1;
14037+}
14038+
14039+void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
14040+{
14041+ sbinfo->si_fhsm.fhsm_expire
14042+ = msecs_to_jiffies(sec * MSEC_PER_SEC);
14043+}
14044+
14045+void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
14046+{
14047+ unsigned int u;
14048+
14049+ if (!au_ftest_si(sbinfo, FHSM))
14050+ return;
14051+
14052+ u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
14053+ if (u != AUFS_FHSM_CACHE_DEF_SEC)
14054+ seq_printf(seq, ",fhsm_sec=%u", u);
14055+}
14056diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
14057--- /usr/share/empty/fs/aufs/file.c 1970-01-01 01:00:00.000000000 +0100
14058+++ linux/fs/aufs/file.c 2021-12-03 15:38:59.936647310 +0100
14059@@ -0,0 +1,863 @@
14060+// SPDX-License-Identifier: GPL-2.0
14061+/*
14062+ * Copyright (C) 2005-2021 Junjiro R. Okajima
14063+ *
14064+ * This program, aufs is free software; you can redistribute it and/or modify
14065+ * it under the terms of the GNU General Public License as published by
14066+ * the Free Software Foundation; either version 2 of the License, or
14067+ * (at your option) any later version.
14068+ *
14069+ * This program is distributed in the hope that it will be useful,
14070+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14071+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14072+ * GNU General Public License for more details.
14073+ *
14074+ * You should have received a copy of the GNU General Public License
14075+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
14076+ */
14077+
14078+/*
14079+ * handling file/dir, and address_space operation
14080+ */
14081+
14082+#ifdef CONFIG_AUFS_DEBUG
14083+#include <linux/migrate.h>
14084+#endif
14085+#include <linux/pagemap.h>
14086+#include "aufs.h"
14087+
14088+/* drop flags for writing */
14089+unsigned int au_file_roflags(unsigned int flags)
14090+{
14091+ flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
14092+ flags |= O_RDONLY | O_NOATIME;
14093+ return flags;
14094+}
14095+
14096+/* common functions to regular file and dir */
14097+struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
14098+ struct file *file, int force_wr)
14099+{
14100+ struct file *h_file;
14101+ struct dentry *h_dentry;
14102+ struct inode *h_inode;
14103+ struct super_block *sb;
14104+ struct au_branch *br;
14105+ struct path h_path;
14106+ int err;
14107+
14108+ /* a race condition can happen between open and unlink/rmdir */
14109+ h_file = ERR_PTR(-ENOENT);
14110+ h_dentry = au_h_dptr(dentry, bindex);
14111+ if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
14112+ goto out;
14113+ h_inode = d_inode(h_dentry);
14114+ spin_lock(&h_dentry->d_lock);
14115+ err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
14116+ /* || !d_inode(dentry)->i_nlink */
14117+ ;
14118+ spin_unlock(&h_dentry->d_lock);
14119+ if (unlikely(err))
14120+ goto out;
14121+
14122+ sb = dentry->d_sb;
14123+ br = au_sbr(sb, bindex);
14124+ err = au_br_test_oflag(flags, br);
14125+ h_file = ERR_PTR(err);
14126+ if (unlikely(err))
14127+ goto out;
14128+
14129+ /* drop flags for writing */
14130+ if (au_test_ro(sb, bindex, d_inode(dentry))) {
14131+ if (force_wr && !(flags & O_WRONLY))
14132+ force_wr = 0;
14133+ flags = au_file_roflags(flags);
14134+ if (force_wr) {
14135+ h_file = ERR_PTR(-EROFS);
14136+ flags = au_file_roflags(flags);
14137+ if (unlikely(vfsub_native_ro(h_inode)
14138+ || IS_APPEND(h_inode)))
14139+ goto out;
14140+ flags &= ~O_ACCMODE;
14141+ flags |= O_WRONLY;
14142+ }
14143+ }
14144+ flags &= ~O_CREAT;
14145+ au_lcnt_inc(&br->br_nfiles);
14146+ h_path.dentry = h_dentry;
14147+ h_path.mnt = au_br_mnt(br);
14148+ h_file = vfsub_dentry_open(&h_path, flags);
14149+ if (IS_ERR(h_file))
14150+ goto out_br;
14151+
14152+ if (flags & __FMODE_EXEC) {
14153+ err = deny_write_access(h_file);
14154+ if (unlikely(err)) {
14155+ fput(h_file);
14156+ h_file = ERR_PTR(err);
14157+ goto out_br;
14158+ }
14159+ }
14160+ fsnotify_open(h_file);
14161+ goto out; /* success */
14162+
14163+out_br:
14164+ au_lcnt_dec(&br->br_nfiles);
14165+out:
14166+ return h_file;
14167+}
14168+
14169+static int au_cmoo(struct dentry *dentry)
14170+{
14171+ int err, cmoo, matched;
14172+ unsigned int udba;
14173+ struct path h_path;
14174+ struct au_pin pin;
14175+ struct au_cp_generic cpg = {
14176+ .dentry = dentry,
14177+ .bdst = -1,
14178+ .bsrc = -1,
14179+ .len = -1,
14180+ .pin = &pin,
14181+ .flags = AuCpup_DTIME | AuCpup_HOPEN
14182+ };
14183+ struct inode *delegated;
14184+ struct super_block *sb;
14185+ struct au_sbinfo *sbinfo;
14186+ struct au_fhsm *fhsm;
14187+ pid_t pid;
14188+ struct au_branch *br;
14189+ struct dentry *parent;
14190+ struct au_hinode *hdir;
14191+
14192+ DiMustWriteLock(dentry);
14193+ IiMustWriteLock(d_inode(dentry));
14194+
14195+ err = 0;
14196+ if (IS_ROOT(dentry))
14197+ goto out;
14198+ cpg.bsrc = au_dbtop(dentry);
14199+ if (!cpg.bsrc)
14200+ goto out;
14201+
14202+ sb = dentry->d_sb;
14203+ sbinfo = au_sbi(sb);
14204+ fhsm = &sbinfo->si_fhsm;
14205+ pid = au_fhsm_pid(fhsm);
14206+ rcu_read_lock();
14207+ matched = (pid
14208+ && (current->pid == pid
14209+ || rcu_dereference(current->real_parent)->pid == pid));
14210+ rcu_read_unlock();
14211+ if (matched)
14212+ goto out;
14213+
14214+ br = au_sbr(sb, cpg.bsrc);
14215+ cmoo = au_br_cmoo(br->br_perm);
14216+ if (!cmoo)
14217+ goto out;
14218+ if (!d_is_reg(dentry))
14219+ cmoo &= AuBrAttr_COO_ALL;
14220+ if (!cmoo)
14221+ goto out;
14222+
14223+ parent = dget_parent(dentry);
14224+ di_write_lock_parent(parent);
14225+ err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
14226+ cpg.bdst = err;
14227+ if (unlikely(err < 0)) {
14228+ err = 0; /* there is no upper writable branch */
14229+ goto out_dgrade;
14230+ }
14231+ AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
14232+
14233+ /* do not respect the coo attrib for the target branch */
14234+ err = au_cpup_dirs(dentry, cpg.bdst);
14235+ if (unlikely(err))
14236+ goto out_dgrade;
14237+
14238+ di_downgrade_lock(parent, AuLock_IR);
14239+ udba = au_opt_udba(sb);
14240+ err = au_pin(&pin, dentry, cpg.bdst, udba,
14241+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14242+ if (unlikely(err))
14243+ goto out_parent;
14244+
14245+ err = au_sio_cpup_simple(&cpg);
14246+ au_unpin(&pin);
14247+ if (unlikely(err))
14248+ goto out_parent;
14249+ if (!(cmoo & AuBrWAttr_MOO))
14250+ goto out_parent; /* success */
14251+
14252+ err = au_pin(&pin, dentry, cpg.bsrc, udba,
14253+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14254+ if (unlikely(err))
14255+ goto out_parent;
14256+
14257+ h_path.mnt = au_br_mnt(br);
14258+ h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
14259+ hdir = au_hi(d_inode(parent), cpg.bsrc);
14260+ delegated = NULL;
14261+ err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
14262+ au_unpin(&pin);
14263+ /* todo: keep h_dentry or not? */
14264+ if (unlikely(err == -EWOULDBLOCK)) {
14265+ pr_warn("cannot retry for NFSv4 delegation"
14266+ " for an internal unlink\n");
14267+ iput(delegated);
14268+ }
14269+ if (unlikely(err)) {
14270+ pr_err("unlink %pd after coo failed (%d), ignored\n",
14271+ dentry, err);
14272+ err = 0;
14273+ }
14274+ goto out_parent; /* success */
14275+
14276+out_dgrade:
14277+ di_downgrade_lock(parent, AuLock_IR);
14278+out_parent:
14279+ di_read_unlock(parent, AuLock_IR);
14280+ dput(parent);
14281+out:
14282+ AuTraceErr(err);
14283+ return err;
14284+}
14285+
14286+int au_do_open(struct file *file, struct au_do_open_args *args)
14287+{
14288+ int err, aopen = args->aopen;
14289+ struct dentry *dentry;
14290+ struct au_finfo *finfo;
14291+
14292+ if (!aopen)
14293+ err = au_finfo_init(file, args->fidir);
14294+ else {
14295+ lockdep_off();
14296+ err = au_finfo_init(file, args->fidir);
14297+ lockdep_on();
14298+ }
14299+ if (unlikely(err))
14300+ goto out;
14301+
14302+ dentry = file->f_path.dentry;
14303+ AuDebugOn(IS_ERR_OR_NULL(dentry));
14304+ di_write_lock_child(dentry);
14305+ err = au_cmoo(dentry);
14306+ di_downgrade_lock(dentry, AuLock_IR);
14307+ if (!err) {
14308+ if (!aopen)
14309+ err = args->open(file, vfsub_file_flags(file), NULL);
14310+ else {
14311+ lockdep_off();
14312+ err = args->open(file, vfsub_file_flags(file),
14313+ args->h_file);
14314+ lockdep_on();
14315+ }
14316+ }
14317+ di_read_unlock(dentry, AuLock_IR);
14318+
14319+ finfo = au_fi(file);
14320+ if (!err) {
14321+ finfo->fi_file = file;
14322+ au_hbl_add(&finfo->fi_hlist,
14323+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
14324+ }
14325+ if (!aopen)
14326+ fi_write_unlock(file);
14327+ else {
14328+ lockdep_off();
14329+ fi_write_unlock(file);
14330+ lockdep_on();
14331+ }
14332+ if (unlikely(err)) {
14333+ finfo->fi_hdir = NULL;
14334+ au_finfo_fin(file);
14335+ }
14336+
14337+out:
14338+ AuTraceErr(err);
14339+ return err;
14340+}
14341+
14342+int au_reopen_nondir(struct file *file)
14343+{
14344+ int err;
14345+ aufs_bindex_t btop;
14346+ struct dentry *dentry;
14347+ struct au_branch *br;
14348+ struct file *h_file, *h_file_tmp;
14349+
14350+ dentry = file->f_path.dentry;
14351+ btop = au_dbtop(dentry);
14352+ br = au_sbr(dentry->d_sb, btop);
14353+ h_file_tmp = NULL;
14354+ if (au_fbtop(file) == btop) {
14355+ h_file = au_hf_top(file);
14356+ if (file->f_mode == h_file->f_mode)
14357+ return 0; /* success */
14358+ h_file_tmp = h_file;
14359+ get_file(h_file_tmp);
14360+ au_lcnt_inc(&br->br_nfiles);
14361+ au_set_h_fptr(file, btop, NULL);
14362+ }
14363+ AuDebugOn(au_fi(file)->fi_hdir);
14364+ /*
14365+ * it can happen
14366+ * file exists on both of rw and ro
14367+ * open --> dbtop and fbtop are both 0
14368+ * prepend a branch as rw, "rw" become ro
14369+ * remove rw/file
14370+ * delete the top branch, "rw" becomes rw again
14371+ * --> dbtop is 1, fbtop is still 0
14372+ * write --> fbtop is 0 but dbtop is 1
14373+ */
14374+ /* AuDebugOn(au_fbtop(file) < btop); */
14375+
14376+ h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
14377+ file, /*force_wr*/0);
14378+ err = PTR_ERR(h_file);
14379+ if (IS_ERR(h_file)) {
14380+ if (h_file_tmp) {
14381+ /* revert */
14382+ au_set_h_fptr(file, btop, h_file_tmp);
14383+ h_file_tmp = NULL;
14384+ }
14385+ goto out; /* todo: close all? */
14386+ }
14387+
14388+ err = 0;
14389+ au_set_fbtop(file, btop);
14390+ au_set_h_fptr(file, btop, h_file);
14391+ au_update_figen(file);
14392+ /* todo: necessary? */
14393+ /* file->f_ra = h_file->f_ra; */
14394+
14395+out:
14396+ if (h_file_tmp) {
14397+ fput(h_file_tmp);
14398+ au_lcnt_dec(&br->br_nfiles);
14399+ }
14400+ return err;
14401+}
14402+
14403+/* ---------------------------------------------------------------------- */
14404+
14405+static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
14406+ struct dentry *hi_wh)
14407+{
14408+ int err;
14409+ aufs_bindex_t btop;
14410+ struct au_dinfo *dinfo;
14411+ struct dentry *h_dentry;
14412+ struct au_hdentry *hdp;
14413+
14414+ dinfo = au_di(file->f_path.dentry);
14415+ AuRwMustWriteLock(&dinfo->di_rwsem);
14416+
14417+ btop = dinfo->di_btop;
14418+ dinfo->di_btop = btgt;
14419+ hdp = au_hdentry(dinfo, btgt);
14420+ h_dentry = hdp->hd_dentry;
14421+ hdp->hd_dentry = hi_wh;
14422+ err = au_reopen_nondir(file);
14423+ hdp->hd_dentry = h_dentry;
14424+ dinfo->di_btop = btop;
14425+
14426+ return err;
14427+}
14428+
14429+static int au_ready_to_write_wh(struct file *file, loff_t len,
14430+ aufs_bindex_t bcpup, struct au_pin *pin)
14431+{
14432+ int err;
14433+ struct inode *inode, *h_inode;
14434+ struct dentry *h_dentry, *hi_wh;
14435+ struct au_cp_generic cpg = {
14436+ .dentry = file->f_path.dentry,
14437+ .bdst = bcpup,
14438+ .bsrc = -1,
14439+ .len = len,
14440+ .pin = pin
14441+ };
14442+
14443+ au_update_dbtop(cpg.dentry);
14444+ inode = d_inode(cpg.dentry);
14445+ h_inode = NULL;
14446+ if (au_dbtop(cpg.dentry) <= bcpup
14447+ && au_dbbot(cpg.dentry) >= bcpup) {
14448+ h_dentry = au_h_dptr(cpg.dentry, bcpup);
14449+ if (h_dentry && d_is_positive(h_dentry))
14450+ h_inode = d_inode(h_dentry);
14451+ }
14452+ hi_wh = au_hi_wh(inode, bcpup);
14453+ if (!hi_wh && !h_inode)
14454+ err = au_sio_cpup_wh(&cpg, file);
14455+ else
14456+ /* already copied-up after unlink */
14457+ err = au_reopen_wh(file, bcpup, hi_wh);
14458+
14459+ if (!err
14460+ && (inode->i_nlink > 1
14461+ || (inode->i_state & I_LINKABLE))
14462+ && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
14463+ au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
14464+
14465+ return err;
14466+}
14467+
14468+/*
14469+ * prepare the @file for writing.
14470+ */
14471+int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
14472+{
14473+ int err;
14474+ aufs_bindex_t dbtop;
14475+ struct dentry *parent;
14476+ struct inode *inode;
14477+ struct super_block *sb;
14478+ struct file *h_file;
14479+ struct au_cp_generic cpg = {
14480+ .dentry = file->f_path.dentry,
14481+ .bdst = -1,
14482+ .bsrc = -1,
14483+ .len = len,
14484+ .pin = pin,
14485+ .flags = AuCpup_DTIME
14486+ };
14487+
14488+ sb = cpg.dentry->d_sb;
14489+ inode = d_inode(cpg.dentry);
14490+ cpg.bsrc = au_fbtop(file);
14491+ err = au_test_ro(sb, cpg.bsrc, inode);
14492+ if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
14493+ err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
14494+ /*flags*/0);
14495+ goto out;
14496+ }
14497+
14498+ /* need to cpup or reopen */
14499+ parent = dget_parent(cpg.dentry);
14500+ di_write_lock_parent(parent);
14501+ err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14502+ cpg.bdst = err;
14503+ if (unlikely(err < 0))
14504+ goto out_dgrade;
14505+ err = 0;
14506+
14507+ if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
14508+ err = au_cpup_dirs(cpg.dentry, cpg.bdst);
14509+ if (unlikely(err))
14510+ goto out_dgrade;
14511+ }
14512+
14513+ err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14514+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14515+ if (unlikely(err))
14516+ goto out_dgrade;
14517+
14518+ dbtop = au_dbtop(cpg.dentry);
14519+ if (dbtop <= cpg.bdst)
14520+ cpg.bsrc = cpg.bdst;
14521+
14522+ if (dbtop <= cpg.bdst /* just reopen */
14523+ || !d_unhashed(cpg.dentry) /* copyup and reopen */
14524+ ) {
14525+ h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
14526+ if (IS_ERR(h_file))
14527+ err = PTR_ERR(h_file);
14528+ else {
14529+ di_downgrade_lock(parent, AuLock_IR);
14530+ if (dbtop > cpg.bdst)
14531+ err = au_sio_cpup_simple(&cpg);
14532+ if (!err)
14533+ err = au_reopen_nondir(file);
14534+ au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
14535+ }
14536+ } else { /* copyup as wh and reopen */
14537+ /*
14538+ * since writable hfsplus branch is not supported,
14539+ * h_open_pre/post() are unnecessary.
14540+ */
14541+ err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
14542+ di_downgrade_lock(parent, AuLock_IR);
14543+ }
14544+
14545+ if (!err) {
14546+ au_pin_set_parent_lflag(pin, /*lflag*/0);
14547+ goto out_dput; /* success */
14548+ }
14549+ au_unpin(pin);
14550+ goto out_unlock;
14551+
14552+out_dgrade:
14553+ di_downgrade_lock(parent, AuLock_IR);
14554+out_unlock:
14555+ di_read_unlock(parent, AuLock_IR);
14556+out_dput:
14557+ dput(parent);
14558+out:
14559+ return err;
14560+}
14561+
14562+/* ---------------------------------------------------------------------- */
14563+
14564+int au_do_flush(struct file *file, fl_owner_t id,
14565+ int (*flush)(struct file *file, fl_owner_t id))
14566+{
14567+ int err;
14568+ struct super_block *sb;
14569+ struct inode *inode;
14570+
14571+ inode = file_inode(file);
14572+ sb = inode->i_sb;
14573+ si_noflush_read_lock(sb);
14574+ fi_read_lock(file);
14575+ ii_read_lock_child(inode);
14576+
14577+ err = flush(file, id);
14578+ au_cpup_attr_timesizes(inode);
14579+
14580+ ii_read_unlock(inode);
14581+ fi_read_unlock(file);
14582+ si_read_unlock(sb);
14583+ return err;
14584+}
14585+
14586+/* ---------------------------------------------------------------------- */
14587+
14588+static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
14589+{
14590+ int err;
14591+ struct au_pin pin;
14592+ struct au_finfo *finfo;
14593+ struct dentry *parent, *hi_wh;
14594+ struct inode *inode;
14595+ struct super_block *sb;
14596+ struct au_cp_generic cpg = {
14597+ .dentry = file->f_path.dentry,
14598+ .bdst = -1,
14599+ .bsrc = -1,
14600+ .len = -1,
14601+ .pin = &pin,
14602+ .flags = AuCpup_DTIME
14603+ };
14604+
14605+ FiMustWriteLock(file);
14606+
14607+ err = 0;
14608+ finfo = au_fi(file);
14609+ sb = cpg.dentry->d_sb;
14610+ inode = d_inode(cpg.dentry);
14611+ cpg.bdst = au_ibtop(inode);
14612+ if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
14613+ goto out;
14614+
14615+ parent = dget_parent(cpg.dentry);
14616+ if (au_test_ro(sb, cpg.bdst, inode)) {
14617+ di_read_lock_parent(parent, !AuLock_IR);
14618+ err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14619+ cpg.bdst = err;
14620+ di_read_unlock(parent, !AuLock_IR);
14621+ if (unlikely(err < 0))
14622+ goto out_parent;
14623+ err = 0;
14624+ }
14625+
14626+ di_read_lock_parent(parent, AuLock_IR);
14627+ hi_wh = au_hi_wh(inode, cpg.bdst);
14628+ if (!S_ISDIR(inode->i_mode)
14629+ && au_opt_test(au_mntflags(sb), PLINK)
14630+ && au_plink_test(inode)
14631+ && !d_unhashed(cpg.dentry)
14632+ && cpg.bdst < au_dbtop(cpg.dentry)) {
14633+ err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
14634+ if (unlikely(err))
14635+ goto out_unlock;
14636+
14637+ /* always superio. */
14638+ err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14639+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14640+ if (!err) {
14641+ err = au_sio_cpup_simple(&cpg);
14642+ au_unpin(&pin);
14643+ }
14644+ } else if (hi_wh) {
14645+ /* already copied-up after unlink */
14646+ err = au_reopen_wh(file, cpg.bdst, hi_wh);
14647+ *need_reopen = 0;
14648+ }
14649+
14650+out_unlock:
14651+ di_read_unlock(parent, AuLock_IR);
14652+out_parent:
14653+ dput(parent);
14654+out:
14655+ return err;
14656+}
14657+
14658+static void au_do_refresh_dir(struct file *file)
14659+{
14660+ aufs_bindex_t bindex, bbot, new_bindex, brid;
14661+ struct au_hfile *p, tmp, *q;
14662+ struct au_finfo *finfo;
14663+ struct super_block *sb;
14664+ struct au_fidir *fidir;
14665+
14666+ FiMustWriteLock(file);
14667+
14668+ sb = file->f_path.dentry->d_sb;
14669+ finfo = au_fi(file);
14670+ fidir = finfo->fi_hdir;
14671+ AuDebugOn(!fidir);
14672+ p = fidir->fd_hfile + finfo->fi_btop;
14673+ brid = p->hf_br->br_id;
14674+ bbot = fidir->fd_bbot;
14675+ for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
14676+ if (!p->hf_file)
14677+ continue;
14678+
14679+ new_bindex = au_br_index(sb, p->hf_br->br_id);
14680+ if (new_bindex == bindex)
14681+ continue;
14682+ if (new_bindex < 0) {
14683+ au_set_h_fptr(file, bindex, NULL);
14684+ continue;
14685+ }
14686+
14687+ /* swap two lower inode, and loop again */
14688+ q = fidir->fd_hfile + new_bindex;
14689+ tmp = *q;
14690+ *q = *p;
14691+ *p = tmp;
14692+ if (tmp.hf_file) {
14693+ bindex--;
14694+ p--;
14695+ }
14696+ }
14697+
14698+ p = fidir->fd_hfile;
14699+ if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
14700+ bbot = au_sbbot(sb);
14701+ for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
14702+ finfo->fi_btop++, p++)
14703+ if (p->hf_file) {
14704+ if (file_inode(p->hf_file))
14705+ break;
14706+ au_hfput(p, /*execed*/0);
14707+ }
14708+ } else {
14709+ bbot = au_br_index(sb, brid);
14710+ for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
14711+ finfo->fi_btop++, p++)
14712+ if (p->hf_file)
14713+ au_hfput(p, /*execed*/0);
14714+ bbot = au_sbbot(sb);
14715+ }
14716+
14717+ p = fidir->fd_hfile + bbot;
14718+ for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
14719+ fidir->fd_bbot--, p--)
14720+ if (p->hf_file) {
14721+ if (file_inode(p->hf_file))
14722+ break;
14723+ au_hfput(p, /*execed*/0);
14724+ }
14725+ AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
14726+}
14727+
14728+/*
14729+ * after branch manipulating, refresh the file.
14730+ */
14731+static int refresh_file(struct file *file, int (*reopen)(struct file *file))
14732+{
14733+ int err, need_reopen, nbr;
14734+ aufs_bindex_t bbot, bindex;
14735+ struct dentry *dentry;
14736+ struct super_block *sb;
14737+ struct au_finfo *finfo;
14738+ struct au_hfile *hfile;
14739+
14740+ dentry = file->f_path.dentry;
14741+ sb = dentry->d_sb;
14742+ nbr = au_sbbot(sb) + 1;
14743+ finfo = au_fi(file);
14744+ if (!finfo->fi_hdir) {
14745+ hfile = &finfo->fi_htop;
14746+ AuDebugOn(!hfile->hf_file);
14747+ bindex = au_br_index(sb, hfile->hf_br->br_id);
14748+ AuDebugOn(bindex < 0);
14749+ if (bindex != finfo->fi_btop)
14750+ au_set_fbtop(file, bindex);
14751+ } else {
14752+ err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
14753+ if (unlikely(err))
14754+ goto out;
14755+ au_do_refresh_dir(file);
14756+ }
14757+
14758+ err = 0;
14759+ need_reopen = 1;
14760+ if (!au_test_mmapped(file))
14761+ err = au_file_refresh_by_inode(file, &need_reopen);
14762+ if (finfo->fi_hdir)
14763+ /* harmless if err */
14764+ au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
14765+ if (!err && need_reopen && !d_unlinked(dentry))
14766+ err = reopen(file);
14767+ if (!err) {
14768+ au_update_figen(file);
14769+ goto out; /* success */
14770+ }
14771+
14772+ /* error, close all lower files */
14773+ if (finfo->fi_hdir) {
14774+ bbot = au_fbbot_dir(file);
14775+ for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
14776+ au_set_h_fptr(file, bindex, NULL);
14777+ }
14778+
14779+out:
14780+ return err;
14781+}
14782+
14783+/* common function to regular file and dir */
14784+int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
14785+ int wlock, unsigned int fi_lsc)
14786+{
14787+ int err;
14788+ unsigned int sigen, figen;
14789+ aufs_bindex_t btop;
14790+ unsigned char pseudo_link;
14791+ struct dentry *dentry;
14792+ struct inode *inode;
14793+
14794+ err = 0;
14795+ dentry = file->f_path.dentry;
14796+ inode = d_inode(dentry);
14797+ sigen = au_sigen(dentry->d_sb);
14798+ fi_write_lock_nested(file, fi_lsc);
14799+ figen = au_figen(file);
14800+ if (!fi_lsc)
14801+ di_write_lock_child(dentry);
14802+ else
14803+ di_write_lock_child2(dentry);
14804+ btop = au_dbtop(dentry);
14805+ pseudo_link = (btop != au_ibtop(inode));
14806+ if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
14807+ if (!wlock) {
14808+ di_downgrade_lock(dentry, AuLock_IR);
14809+ fi_downgrade_lock(file);
14810+ }
14811+ goto out; /* success */
14812+ }
14813+
14814+ AuDbg("sigen %d, figen %d\n", sigen, figen);
14815+ if (au_digen_test(dentry, sigen)) {
14816+ err = au_reval_dpath(dentry, sigen);
14817+ AuDebugOn(!err && au_digen_test(dentry, sigen));
14818+ }
14819+
14820+ if (!err)
14821+ err = refresh_file(file, reopen);
14822+ if (!err) {
14823+ if (!wlock) {
14824+ di_downgrade_lock(dentry, AuLock_IR);
14825+ fi_downgrade_lock(file);
14826+ }
14827+ } else {
14828+ di_write_unlock(dentry);
14829+ fi_write_unlock(file);
14830+ }
14831+
14832+out:
14833+ return err;
14834+}
14835+
14836+/* ---------------------------------------------------------------------- */
14837+
14838+/* cf. aufs_nopage() */
14839+/* for madvise(2) */
14840+static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
14841+{
14842+ unlock_page(page);
14843+ return 0;
14844+}
14845+
14846+/* it will never be called, but necessary to support O_DIRECT */
14847+static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
14848+{ BUG(); return 0; }
14849+
14850+/* they will never be called. */
14851+#ifdef CONFIG_AUFS_DEBUG
14852+static int aufs_write_begin(struct file *file, struct address_space *mapping,
14853+ loff_t pos, unsigned len, unsigned flags,
14854+ struct page **pagep, void **fsdata)
14855+{ AuUnsupport(); return 0; }
14856+static int aufs_write_end(struct file *file, struct address_space *mapping,
14857+ loff_t pos, unsigned len, unsigned copied,
14858+ struct page *page, void *fsdata)
14859+{ AuUnsupport(); return 0; }
14860+static int aufs_writepage(struct page *page, struct writeback_control *wbc)
14861+{ AuUnsupport(); return 0; }
14862+
14863+static int aufs_set_page_dirty(struct page *page)
14864+{ AuUnsupport(); return 0; }
14865+static void aufs_invalidatepage(struct page *page, unsigned int offset,
14866+ unsigned int length)
14867+{ AuUnsupport(); }
14868+static int aufs_releasepage(struct page *page, gfp_t gfp)
14869+{ AuUnsupport(); return 0; }
14870+#if 0 /* called by memory compaction regardless file */
14871+static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
14872+ struct page *page, enum migrate_mode mode)
14873+{ AuUnsupport(); return 0; }
14874+#endif
14875+static bool aufs_isolate_page(struct page *page, isolate_mode_t mode)
14876+{ AuUnsupport(); return true; }
14877+static void aufs_putback_page(struct page *page)
14878+{ AuUnsupport(); }
14879+static int aufs_launder_page(struct page *page)
14880+{ AuUnsupport(); return 0; }
14881+static int aufs_is_partially_uptodate(struct page *page,
14882+ unsigned long from,
14883+ unsigned long count)
14884+{ AuUnsupport(); return 0; }
14885+static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
14886+ bool *writeback)
14887+{ AuUnsupport(); }
14888+static int aufs_error_remove_page(struct address_space *mapping,
14889+ struct page *page)
14890+{ AuUnsupport(); return 0; }
14891+static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
14892+ sector_t *span)
14893+{ AuUnsupport(); return 0; }
14894+static void aufs_swap_deactivate(struct file *file)
14895+{ AuUnsupport(); }
14896+#endif /* CONFIG_AUFS_DEBUG */
14897+
14898+const struct address_space_operations aufs_aop = {
14899+ .readpage = aufs_readpage,
14900+ .direct_IO = aufs_direct_IO,
14901+#ifdef CONFIG_AUFS_DEBUG
14902+ .writepage = aufs_writepage,
14903+ /* no writepages, because of writepage */
14904+ .set_page_dirty = aufs_set_page_dirty,
14905+ /* no readpages, because of readpage */
14906+ .write_begin = aufs_write_begin,
14907+ .write_end = aufs_write_end,
14908+ /* no bmap, no block device */
14909+ .invalidatepage = aufs_invalidatepage,
14910+ .releasepage = aufs_releasepage,
14911+ /* is fallback_migrate_page ok? */
14912+ /* .migratepage = aufs_migratepage, */
14913+ .isolate_page = aufs_isolate_page,
14914+ .putback_page = aufs_putback_page,
14915+ .launder_page = aufs_launder_page,
14916+ .is_partially_uptodate = aufs_is_partially_uptodate,
14917+ .is_dirty_writeback = aufs_is_dirty_writeback,
14918+ .error_remove_page = aufs_error_remove_page,
14919+ .swap_activate = aufs_swap_activate,
14920+ .swap_deactivate = aufs_swap_deactivate
14921+#endif /* CONFIG_AUFS_DEBUG */
14922+};
14923diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
14924--- /usr/share/empty/fs/aufs/file.h 1970-01-01 01:00:00.000000000 +0100
14925+++ linux/fs/aufs/file.h 2021-12-03 15:38:59.936647310 +0100
14926@@ -0,0 +1,342 @@
14927+/* SPDX-License-Identifier: GPL-2.0 */
14928+/*
14929+ * Copyright (C) 2005-2021 Junjiro R. Okajima
14930+ *
14931+ * This program, aufs is free software; you can redistribute it and/or modify
14932+ * it under the terms of the GNU General Public License as published by
14933+ * the Free Software Foundation; either version 2 of the License, or
14934+ * (at your option) any later version.
14935+ *
14936+ * This program is distributed in the hope that it will be useful,
14937+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14938+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14939+ * GNU General Public License for more details.
14940+ *
14941+ * You should have received a copy of the GNU General Public License
14942+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
14943+ */
14944+
14945+/*
14946+ * file operations
14947+ */
14948+
14949+#ifndef __AUFS_FILE_H__
14950+#define __AUFS_FILE_H__
14951+
14952+#ifdef __KERNEL__
14953+
14954+#include <linux/file.h>
14955+#include <linux/fs.h>
14956+#include <linux/mm_types.h>
14957+#include <linux/poll.h>
14958+#include "rwsem.h"
14959+
14960+struct au_branch;
14961+struct au_hfile {
14962+ struct file *hf_file;
14963+ struct au_branch *hf_br;
14964+};
14965+
14966+struct au_vdir;
14967+struct au_fidir {
14968+ aufs_bindex_t fd_bbot;
14969+ aufs_bindex_t fd_nent;
14970+ struct au_vdir *fd_vdir_cache;
14971+ struct au_hfile fd_hfile[];
14972+};
14973+
14974+static inline int au_fidir_sz(int nent)
14975+{
14976+ AuDebugOn(nent < 0);
14977+ return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
14978+}
14979+
14980+struct au_finfo {
14981+ atomic_t fi_generation;
14982+
14983+ struct au_rwsem fi_rwsem;
14984+ aufs_bindex_t fi_btop;
14985+
14986+ /* do not union them */
14987+ struct { /* for non-dir */
14988+ struct au_hfile fi_htop;
14989+ atomic_t fi_mmapped;
14990+ };
14991+ struct au_fidir *fi_hdir; /* for dir only */
14992+
14993+ struct hlist_bl_node fi_hlist;
14994+ struct file *fi_file; /* very ugly */
14995+ struct rcu_head rcu;
14996+} ____cacheline_aligned_in_smp;
14997+
14998+/* ---------------------------------------------------------------------- */
14999+
15000+/* file.c */
15001+extern const struct address_space_operations aufs_aop;
15002+unsigned int au_file_roflags(unsigned int flags);
15003+struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
15004+ struct file *file, int force_wr);
15005+struct au_do_open_args {
15006+ int aopen;
15007+ int (*open)(struct file *file, int flags,
15008+ struct file *h_file);
15009+ struct au_fidir *fidir;
15010+ struct file *h_file;
15011+};
15012+int au_do_open(struct file *file, struct au_do_open_args *args);
15013+int au_reopen_nondir(struct file *file);
15014+struct au_pin;
15015+int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
15016+int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
15017+ int wlock, unsigned int fi_lsc);
15018+int au_do_flush(struct file *file, fl_owner_t id,
15019+ int (*flush)(struct file *file, fl_owner_t id));
15020+
15021+/* poll.c */
15022+#ifdef CONFIG_AUFS_POLL
15023+__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt);
15024+#endif
15025+
15026+#ifdef CONFIG_AUFS_BR_HFSPLUS
15027+/* hfsplus.c */
15028+struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
15029+ int force_wr);
15030+void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
15031+ struct file *h_file);
15032+#else
15033+AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
15034+ aufs_bindex_t bindex, int force_wr)
15035+AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
15036+ struct file *h_file);
15037+#endif
15038+
15039+/* f_op.c */
15040+extern const struct file_operations aufs_file_fop;
15041+int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
15042+int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
15043+struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
15044+
15045+/* finfo.c */
15046+void au_hfput(struct au_hfile *hf, int execed);
15047+void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
15048+ struct file *h_file);
15049+
15050+void au_update_figen(struct file *file);
15051+struct au_fidir *au_fidir_alloc(struct super_block *sb);
15052+int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
15053+
15054+void au_fi_init_once(void *_fi);
15055+void au_finfo_fin(struct file *file);
15056+int au_finfo_init(struct file *file, struct au_fidir *fidir);
15057+
15058+/* ioctl.c */
15059+long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
15060+#ifdef CONFIG_COMPAT
15061+long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
15062+ unsigned long arg);
15063+long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
15064+ unsigned long arg);
15065+#endif
15066+
15067+/* ---------------------------------------------------------------------- */
15068+
15069+static inline struct au_finfo *au_fi(struct file *file)
15070+{
15071+ return file->private_data;
15072+}
15073+
15074+/* ---------------------------------------------------------------------- */
15075+
15076+#define fi_read_lock(f) au_rw_read_lock(&au_fi(f)->fi_rwsem)
15077+#define fi_write_lock(f) au_rw_write_lock(&au_fi(f)->fi_rwsem)
15078+#define fi_read_trylock(f) au_rw_read_trylock(&au_fi(f)->fi_rwsem)
15079+#define fi_write_trylock(f) au_rw_write_trylock(&au_fi(f)->fi_rwsem)
15080+/*
15081+#define fi_read_trylock_nested(f) \
15082+ au_rw_read_trylock_nested(&au_fi(f)->fi_rwsem)
15083+#define fi_write_trylock_nested(f) \
15084+ au_rw_write_trylock_nested(&au_fi(f)->fi_rwsem)
15085+*/
15086+
15087+#define fi_read_unlock(f) au_rw_read_unlock(&au_fi(f)->fi_rwsem)
15088+#define fi_write_unlock(f) au_rw_write_unlock(&au_fi(f)->fi_rwsem)
15089+#define fi_downgrade_lock(f) au_rw_dgrade_lock(&au_fi(f)->fi_rwsem)
15090+
15091+/* lock subclass for finfo */
15092+enum {
15093+ AuLsc_FI_1,
15094+ AuLsc_FI_2
15095+};
15096+
15097+static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
15098+{
15099+ au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15100+}
15101+
15102+static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
15103+{
15104+ au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15105+}
15106+
15107+/*
15108+ * fi_read_lock_1, fi_write_lock_1,
15109+ * fi_read_lock_2, fi_write_lock_2
15110+ */
15111+#define AuReadLockFunc(name) \
15112+static inline void fi_read_lock_##name(struct file *f) \
15113+{ fi_read_lock_nested(f, AuLsc_FI_##name); }
15114+
15115+#define AuWriteLockFunc(name) \
15116+static inline void fi_write_lock_##name(struct file *f) \
15117+{ fi_write_lock_nested(f, AuLsc_FI_##name); }
15118+
15119+#define AuRWLockFuncs(name) \
15120+ AuReadLockFunc(name) \
15121+ AuWriteLockFunc(name)
15122+
15123+AuRWLockFuncs(1);
15124+AuRWLockFuncs(2);
15125+
15126+#undef AuReadLockFunc
15127+#undef AuWriteLockFunc
15128+#undef AuRWLockFuncs
15129+
15130+#define FiMustNoWaiters(f) AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
15131+#define FiMustAnyLock(f) AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
15132+#define FiMustWriteLock(f) AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
15133+
15134+/* ---------------------------------------------------------------------- */
15135+
15136+/* todo: hard/soft set? */
15137+static inline aufs_bindex_t au_fbtop(struct file *file)
15138+{
15139+ FiMustAnyLock(file);
15140+ return au_fi(file)->fi_btop;
15141+}
15142+
15143+static inline aufs_bindex_t au_fbbot_dir(struct file *file)
15144+{
15145+ FiMustAnyLock(file);
15146+ AuDebugOn(!au_fi(file)->fi_hdir);
15147+ return au_fi(file)->fi_hdir->fd_bbot;
15148+}
15149+
15150+static inline struct au_vdir *au_fvdir_cache(struct file *file)
15151+{
15152+ FiMustAnyLock(file);
15153+ AuDebugOn(!au_fi(file)->fi_hdir);
15154+ return au_fi(file)->fi_hdir->fd_vdir_cache;
15155+}
15156+
15157+static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
15158+{
15159+ FiMustWriteLock(file);
15160+ au_fi(file)->fi_btop = bindex;
15161+}
15162+
15163+static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
15164+{
15165+ FiMustWriteLock(file);
15166+ AuDebugOn(!au_fi(file)->fi_hdir);
15167+ au_fi(file)->fi_hdir->fd_bbot = bindex;
15168+}
15169+
15170+static inline void au_set_fvdir_cache(struct file *file,
15171+ struct au_vdir *vdir_cache)
15172+{
15173+ FiMustWriteLock(file);
15174+ AuDebugOn(!au_fi(file)->fi_hdir);
15175+ au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
15176+}
15177+
15178+static inline struct file *au_hf_top(struct file *file)
15179+{
15180+ FiMustAnyLock(file);
15181+ AuDebugOn(au_fi(file)->fi_hdir);
15182+ return au_fi(file)->fi_htop.hf_file;
15183+}
15184+
15185+static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
15186+{
15187+ FiMustAnyLock(file);
15188+ AuDebugOn(!au_fi(file)->fi_hdir);
15189+ return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
15190+}
15191+
15192+/* todo: memory barrier? */
15193+static inline unsigned int au_figen(struct file *f)
15194+{
15195+ return atomic_read(&au_fi(f)->fi_generation);
15196+}
15197+
15198+static inline void au_set_mmapped(struct file *f)
15199+{
15200+ if (atomic_inc_return(&au_fi(f)->fi_mmapped))
15201+ return;
15202+ pr_warn("fi_mmapped wrapped around\n");
15203+ while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
15204+ ;
15205+}
15206+
15207+static inline void au_unset_mmapped(struct file *f)
15208+{
15209+ atomic_dec(&au_fi(f)->fi_mmapped);
15210+}
15211+
15212+static inline int au_test_mmapped(struct file *f)
15213+{
15214+ return atomic_read(&au_fi(f)->fi_mmapped);
15215+}
15216+
15217+/* customize vma->vm_file */
15218+
15219+static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
15220+ struct file *file)
15221+{
15222+ struct file *f;
15223+
15224+ f = vma->vm_file;
15225+ get_file(file);
15226+ vma->vm_file = file;
15227+ fput(f);
15228+}
15229+
15230+#ifdef CONFIG_MMU
15231+#define AuDbgVmRegion(file, vma) do {} while (0)
15232+
15233+static inline void au_vm_file_reset(struct vm_area_struct *vma,
15234+ struct file *file)
15235+{
15236+ au_do_vm_file_reset(vma, file);
15237+}
15238+#else
15239+#define AuDbgVmRegion(file, vma) \
15240+ AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
15241+
15242+static inline void au_vm_file_reset(struct vm_area_struct *vma,
15243+ struct file *file)
15244+{
15245+ struct file *f;
15246+
15247+ au_do_vm_file_reset(vma, file);
15248+ f = vma->vm_region->vm_file;
15249+ get_file(file);
15250+ vma->vm_region->vm_file = file;
15251+ fput(f);
15252+}
15253+#endif /* CONFIG_MMU */
15254+
15255+/* handle vma->vm_prfile */
15256+static inline void au_vm_prfile_set(struct vm_area_struct *vma,
15257+ struct file *file)
15258+{
15259+ get_file(file);
15260+ vma->vm_prfile = file;
15261+#ifndef CONFIG_MMU
15262+ get_file(file);
15263+ vma->vm_region->vm_prfile = file;
15264+#endif
15265+}
15266+
15267+#endif /* __KERNEL__ */
15268+#endif /* __AUFS_FILE_H__ */
15269diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
15270--- /usr/share/empty/fs/aufs/finfo.c 1970-01-01 01:00:00.000000000 +0100
15271+++ linux/fs/aufs/finfo.c 2021-12-03 15:38:59.936647310 +0100
15272@@ -0,0 +1,149 @@
15273+// SPDX-License-Identifier: GPL-2.0
15274+/*
15275+ * Copyright (C) 2005-2021 Junjiro R. Okajima
15276+ *
15277+ * This program, aufs is free software; you can redistribute it and/or modify
15278+ * it under the terms of the GNU General Public License as published by
15279+ * the Free Software Foundation; either version 2 of the License, or
15280+ * (at your option) any later version.
15281+ *
15282+ * This program is distributed in the hope that it will be useful,
15283+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15284+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15285+ * GNU General Public License for more details.
15286+ *
15287+ * You should have received a copy of the GNU General Public License
15288+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15289+ */
15290+
15291+/*
15292+ * file private data
15293+ */
15294+
15295+#include "aufs.h"
15296+
15297+void au_hfput(struct au_hfile *hf, int execed)
15298+{
15299+ if (execed)
15300+ allow_write_access(hf->hf_file);
15301+ fput(hf->hf_file);
15302+ hf->hf_file = NULL;
15303+ au_lcnt_dec(&hf->hf_br->br_nfiles);
15304+ hf->hf_br = NULL;
15305+}
15306+
15307+void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
15308+{
15309+ struct au_finfo *finfo = au_fi(file);
15310+ struct au_hfile *hf;
15311+ struct au_fidir *fidir;
15312+
15313+ fidir = finfo->fi_hdir;
15314+ if (!fidir) {
15315+ AuDebugOn(finfo->fi_btop != bindex);
15316+ hf = &finfo->fi_htop;
15317+ } else
15318+ hf = fidir->fd_hfile + bindex;
15319+
15320+ if (hf && hf->hf_file)
15321+ au_hfput(hf, vfsub_file_execed(file));
15322+ if (val) {
15323+ FiMustWriteLock(file);
15324+ AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
15325+ hf->hf_file = val;
15326+ hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
15327+ }
15328+}
15329+
15330+void au_update_figen(struct file *file)
15331+{
15332+ atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
15333+ /* smp_mb(); */ /* atomic_set */
15334+}
15335+
15336+/* ---------------------------------------------------------------------- */
15337+
15338+struct au_fidir *au_fidir_alloc(struct super_block *sb)
15339+{
15340+ struct au_fidir *fidir;
15341+ int nbr;
15342+
15343+ nbr = au_sbbot(sb) + 1;
15344+ if (nbr < 2)
15345+ nbr = 2; /* initial allocate for 2 branches */
15346+ fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
15347+ if (fidir) {
15348+ fidir->fd_bbot = -1;
15349+ fidir->fd_nent = nbr;
15350+ }
15351+
15352+ return fidir;
15353+}
15354+
15355+int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
15356+{
15357+ int err;
15358+ struct au_fidir *fidir, *p;
15359+
15360+ AuRwMustWriteLock(&finfo->fi_rwsem);
15361+ fidir = finfo->fi_hdir;
15362+ AuDebugOn(!fidir);
15363+
15364+ err = -ENOMEM;
15365+ p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
15366+ GFP_NOFS, may_shrink);
15367+ if (p) {
15368+ p->fd_nent = nbr;
15369+ finfo->fi_hdir = p;
15370+ err = 0;
15371+ }
15372+
15373+ return err;
15374+}
15375+
15376+/* ---------------------------------------------------------------------- */
15377+
15378+void au_finfo_fin(struct file *file)
15379+{
15380+ struct au_finfo *finfo;
15381+
15382+ au_lcnt_dec(&au_sbi(file->f_path.dentry->d_sb)->si_nfiles);
15383+
15384+ finfo = au_fi(file);
15385+ AuDebugOn(finfo->fi_hdir);
15386+ AuRwDestroy(&finfo->fi_rwsem);
15387+ au_cache_free_finfo(finfo);
15388+}
15389+
15390+void au_fi_init_once(void *_finfo)
15391+{
15392+ struct au_finfo *finfo = _finfo;
15393+
15394+ au_rw_init(&finfo->fi_rwsem);
15395+}
15396+
15397+int au_finfo_init(struct file *file, struct au_fidir *fidir)
15398+{
15399+ int err;
15400+ struct au_finfo *finfo;
15401+ struct dentry *dentry;
15402+
15403+ err = -ENOMEM;
15404+ dentry = file->f_path.dentry;
15405+ finfo = au_cache_alloc_finfo();
15406+ if (unlikely(!finfo))
15407+ goto out;
15408+
15409+ err = 0;
15410+ au_lcnt_inc(&au_sbi(dentry->d_sb)->si_nfiles);
15411+ au_rw_write_lock(&finfo->fi_rwsem);
15412+ finfo->fi_btop = -1;
15413+ finfo->fi_hdir = fidir;
15414+ atomic_set(&finfo->fi_generation, au_digen(dentry));
15415+ /* smp_mb(); */ /* atomic_set */
15416+
15417+ file->private_data = finfo;
15418+
15419+out:
15420+ return err;
15421+}
15422diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
15423--- /usr/share/empty/fs/aufs/f_op.c 1970-01-01 01:00:00.000000000 +0100
15424+++ linux/fs/aufs/f_op.c 2021-12-03 15:38:59.933313976 +0100
15425@@ -0,0 +1,771 @@
15426+// SPDX-License-Identifier: GPL-2.0
15427+/*
15428+ * Copyright (C) 2005-2021 Junjiro R. Okajima
15429+ *
15430+ * This program, aufs is free software; you can redistribute it and/or modify
15431+ * it under the terms of the GNU General Public License as published by
15432+ * the Free Software Foundation; either version 2 of the License, or
15433+ * (at your option) any later version.
15434+ *
15435+ * This program is distributed in the hope that it will be useful,
15436+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15437+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15438+ * GNU General Public License for more details.
15439+ *
15440+ * You should have received a copy of the GNU General Public License
15441+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15442+ */
15443+
15444+/*
15445+ * file and vm operations
15446+ */
15447+
15448+#include <linux/aio.h>
15449+#include <linux/fs_stack.h>
15450+#include <linux/mman.h>
15451+#include <linux/security.h>
15452+#include "aufs.h"
15453+
15454+int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
15455+{
15456+ int err;
15457+ aufs_bindex_t bindex;
15458+ struct dentry *dentry, *h_dentry;
15459+ struct au_finfo *finfo;
15460+ struct inode *h_inode;
15461+
15462+ FiMustWriteLock(file);
15463+
15464+ err = 0;
15465+ dentry = file->f_path.dentry;
15466+ AuDebugOn(IS_ERR_OR_NULL(dentry));
15467+ finfo = au_fi(file);
15468+ memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
15469+ atomic_set(&finfo->fi_mmapped, 0);
15470+ bindex = au_dbtop(dentry);
15471+ if (!h_file) {
15472+ h_dentry = au_h_dptr(dentry, bindex);
15473+ err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15474+ if (unlikely(err))
15475+ goto out;
15476+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
15477+ if (IS_ERR(h_file)) {
15478+ err = PTR_ERR(h_file);
15479+ goto out;
15480+ }
15481+ } else {
15482+ h_dentry = h_file->f_path.dentry;
15483+ err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15484+ if (unlikely(err))
15485+ goto out;
15486+ /* br ref is already inc-ed */
15487+ }
15488+
15489+ if ((flags & __O_TMPFILE)
15490+ && !(flags & O_EXCL)) {
15491+ h_inode = file_inode(h_file);
15492+ spin_lock(&h_inode->i_lock);
15493+ h_inode->i_state |= I_LINKABLE;
15494+ spin_unlock(&h_inode->i_lock);
15495+ }
15496+ au_set_fbtop(file, bindex);
15497+ au_set_h_fptr(file, bindex, h_file);
15498+ au_update_figen(file);
15499+ /* todo: necessary? */
15500+ /* file->f_ra = h_file->f_ra; */
15501+
15502+out:
15503+ return err;
15504+}
15505+
15506+static int aufs_open_nondir(struct inode *inode __maybe_unused,
15507+ struct file *file)
15508+{
15509+ int err;
15510+ struct super_block *sb;
15511+ struct au_do_open_args args = {
15512+ .open = au_do_open_nondir
15513+ };
15514+
15515+ AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
15516+ file, vfsub_file_flags(file), file->f_mode);
15517+
15518+ sb = file->f_path.dentry->d_sb;
15519+ si_read_lock(sb, AuLock_FLUSH);
15520+ err = au_do_open(file, &args);
15521+ si_read_unlock(sb);
15522+ return err;
15523+}
15524+
15525+int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
15526+{
15527+ struct au_finfo *finfo;
15528+ aufs_bindex_t bindex;
15529+
15530+ finfo = au_fi(file);
15531+ au_hbl_del(&finfo->fi_hlist,
15532+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
15533+ bindex = finfo->fi_btop;
15534+ if (bindex >= 0)
15535+ au_set_h_fptr(file, bindex, NULL);
15536+
15537+ au_finfo_fin(file);
15538+ return 0;
15539+}
15540+
15541+/* ---------------------------------------------------------------------- */
15542+
15543+static int au_do_flush_nondir(struct file *file, fl_owner_t id)
15544+{
15545+ int err;
15546+ struct file *h_file;
15547+
15548+ err = 0;
15549+ h_file = au_hf_top(file);
15550+ if (h_file)
15551+ err = vfsub_flush(h_file, id);
15552+ return err;
15553+}
15554+
15555+static int aufs_flush_nondir(struct file *file, fl_owner_t id)
15556+{
15557+ return au_do_flush(file, id, au_do_flush_nondir);
15558+}
15559+
15560+/* ---------------------------------------------------------------------- */
15561+/*
15562+ * read and write functions acquire [fdi]_rwsem once, but release before
15563+ * mmap_sem. This is because to stop a race condition between mmap(2).
15564+ * Releasing these aufs-rwsem should be safe, no branch-management (by keeping
15565+ * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
15566+ * read functions after [fdi]_rwsem are released, but it should be harmless.
15567+ */
15568+
15569+/* Callers should call au_read_post() or fput() in the end */
15570+struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
15571+{
15572+ struct file *h_file;
15573+ int err;
15574+
15575+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
15576+ if (!err) {
15577+ di_read_unlock(file->f_path.dentry, AuLock_IR);
15578+ h_file = au_hf_top(file);
15579+ get_file(h_file);
15580+ if (!keep_fi)
15581+ fi_read_unlock(file);
15582+ } else
15583+ h_file = ERR_PTR(err);
15584+
15585+ return h_file;
15586+}
15587+
15588+static void au_read_post(struct inode *inode, struct file *h_file)
15589+{
15590+ /* update without lock, I don't think it a problem */
15591+ fsstack_copy_attr_atime(inode, file_inode(h_file));
15592+ fput(h_file);
15593+}
15594+
15595+struct au_write_pre {
15596+ /* input */
15597+ unsigned int lsc;
15598+
15599+ /* output */
15600+ blkcnt_t blks;
15601+ aufs_bindex_t btop;
15602+};
15603+
15604+/*
15605+ * return with iinfo is write-locked
15606+ * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
15607+ * end
15608+ */
15609+static struct file *au_write_pre(struct file *file, int do_ready,
15610+ struct au_write_pre *wpre)
15611+{
15612+ struct file *h_file;
15613+ struct dentry *dentry;
15614+ int err;
15615+ unsigned int lsc;
15616+ struct au_pin pin;
15617+
15618+ lsc = 0;
15619+ if (wpre)
15620+ lsc = wpre->lsc;
15621+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
15622+ h_file = ERR_PTR(err);
15623+ if (unlikely(err))
15624+ goto out;
15625+
15626+ dentry = file->f_path.dentry;
15627+ if (do_ready) {
15628+ err = au_ready_to_write(file, -1, &pin);
15629+ if (unlikely(err)) {
15630+ h_file = ERR_PTR(err);
15631+ di_write_unlock(dentry);
15632+ goto out_fi;
15633+ }
15634+ }
15635+
15636+ di_downgrade_lock(dentry, /*flags*/0);
15637+ if (wpre)
15638+ wpre->btop = au_fbtop(file);
15639+ h_file = au_hf_top(file);
15640+ get_file(h_file);
15641+ if (wpre)
15642+ wpre->blks = file_inode(h_file)->i_blocks;
15643+ if (do_ready)
15644+ au_unpin(&pin);
15645+ di_read_unlock(dentry, /*flags*/0);
15646+
15647+out_fi:
15648+ fi_write_unlock(file);
15649+out:
15650+ return h_file;
15651+}
15652+
15653+static void au_write_post(struct inode *inode, struct file *h_file,
15654+ struct au_write_pre *wpre, ssize_t written)
15655+{
15656+ struct inode *h_inode;
15657+
15658+ au_cpup_attr_timesizes(inode);
15659+ AuDebugOn(au_ibtop(inode) != wpre->btop);
15660+ h_inode = file_inode(h_file);
15661+ inode->i_mode = h_inode->i_mode;
15662+ ii_write_unlock(inode);
15663+ /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
15664+ if (written > 0)
15665+ au_fhsm_wrote(inode->i_sb, wpre->btop,
15666+ /*force*/h_inode->i_blocks > wpre->blks);
15667+ fput(h_file);
15668+}
15669+
15670+/*
15671+ * todo: very ugly
15672+ * it locks both of i_mutex and si_rwsem for read in safe.
15673+ * if the plink maintenance mode continues forever (that is the problem),
15674+ * may loop forever.
15675+ */
15676+static void au_mtx_and_read_lock(struct inode *inode)
15677+{
15678+ int err;
15679+ struct super_block *sb = inode->i_sb;
15680+
15681+ while (1) {
15682+ inode_lock(inode);
15683+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
15684+ if (!err)
15685+ break;
15686+ inode_unlock(inode);
15687+ si_read_lock(sb, AuLock_NOPLMW);
15688+ si_read_unlock(sb);
15689+ }
15690+}
15691+
15692+static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
15693+ struct iov_iter *iov_iter)
15694+{
15695+ ssize_t err;
15696+ struct file *file;
15697+ ssize_t (*iter)(struct kiocb *, struct iov_iter *);
15698+
15699+ err = security_file_permission(h_file, rw);
15700+ if (unlikely(err))
15701+ goto out;
15702+
15703+ err = -ENOSYS; /* the branch doesn't have its ->(read|write)_iter() */
15704+ iter = NULL;
15705+ if (rw == MAY_READ)
15706+ iter = h_file->f_op->read_iter;
15707+ else if (rw == MAY_WRITE)
15708+ iter = h_file->f_op->write_iter;
15709+
15710+ file = kio->ki_filp;
15711+ kio->ki_filp = h_file;
15712+ if (iter) {
15713+ lockdep_off();
15714+ err = iter(kio, iov_iter);
15715+ lockdep_on();
15716+ } else
15717+ /* currently there is no such fs */
15718+ WARN_ON_ONCE(1);
15719+ kio->ki_filp = file;
15720+
15721+out:
15722+ return err;
15723+}
15724+
15725+static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15726+{
15727+ ssize_t err;
15728+ struct file *file, *h_file;
15729+ struct inode *inode;
15730+ struct super_block *sb;
15731+
15732+ file = kio->ki_filp;
15733+ inode = file_inode(file);
15734+ sb = inode->i_sb;
15735+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15736+
15737+ h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
15738+ err = PTR_ERR(h_file);
15739+ if (IS_ERR(h_file))
15740+ goto out;
15741+
15742+ if (au_test_loopback_kthread()) {
15743+ au_warn_loopback(h_file->f_path.dentry->d_sb);
15744+ if (file->f_mapping != h_file->f_mapping) {
15745+ file->f_mapping = h_file->f_mapping;
15746+ smp_mb(); /* unnecessary? */
15747+ }
15748+ }
15749+ fi_read_unlock(file);
15750+
15751+ err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
15752+ /* todo: necessary? */
15753+ /* file->f_ra = h_file->f_ra; */
15754+ au_read_post(inode, h_file);
15755+
15756+out:
15757+ si_read_unlock(sb);
15758+ return err;
15759+}
15760+
15761+static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15762+{
15763+ ssize_t err;
15764+ struct au_write_pre wpre;
15765+ struct inode *inode;
15766+ struct file *file, *h_file;
15767+
15768+ file = kio->ki_filp;
15769+ inode = file_inode(file);
15770+ au_mtx_and_read_lock(inode);
15771+
15772+ wpre.lsc = 0;
15773+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15774+ err = PTR_ERR(h_file);
15775+ if (IS_ERR(h_file))
15776+ goto out;
15777+
15778+ err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
15779+ au_write_post(inode, h_file, &wpre, err);
15780+
15781+out:
15782+ si_read_unlock(inode->i_sb);
15783+ inode_unlock(inode);
15784+ return err;
15785+}
15786+
15787+/*
15788+ * We may be able to remove aufs_splice_{read,write}() since almost all FSes
15789+ * don't have their own .splice_{read,write} implimentations, and they use
15790+ * generic_file_splice_read() and iter_file_splice_write() who can act like the
15791+ * simple converters to f_op->iter_read() and ->iter_write().
15792+ * But we keep our own implementations because some non-mainlined FSes may have
15793+ * their own .splice_{read,write} implimentations and aufs doesn't want to take
15794+ * away an opportunity to co-work with aufs from them.
15795+ */
15796+static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
15797+ struct pipe_inode_info *pipe, size_t len,
15798+ unsigned int flags)
15799+{
15800+ ssize_t err;
15801+ struct file *h_file;
15802+ struct inode *inode;
15803+ struct super_block *sb;
15804+
15805+ inode = file_inode(file);
15806+ sb = inode->i_sb;
15807+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15808+
15809+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
15810+ err = PTR_ERR(h_file);
15811+ if (IS_ERR(h_file))
15812+ goto out;
15813+
15814+ err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
15815+ /* todo: necessary? */
15816+ /* file->f_ra = h_file->f_ra; */
15817+ au_read_post(inode, h_file);
15818+
15819+out:
15820+ si_read_unlock(sb);
15821+ return err;
15822+}
15823+
15824+static ssize_t
15825+aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
15826+ size_t len, unsigned int flags)
15827+{
15828+ ssize_t err;
15829+ struct au_write_pre wpre;
15830+ struct inode *inode;
15831+ struct file *h_file;
15832+
15833+ inode = file_inode(file);
15834+ au_mtx_and_read_lock(inode);
15835+
15836+ wpre.lsc = 0;
15837+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15838+ err = PTR_ERR(h_file);
15839+ if (IS_ERR(h_file))
15840+ goto out;
15841+
15842+ err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
15843+ au_write_post(inode, h_file, &wpre, err);
15844+
15845+out:
15846+ si_read_unlock(inode->i_sb);
15847+ inode_unlock(inode);
15848+ return err;
15849+}
15850+
15851+static long aufs_fallocate(struct file *file, int mode, loff_t offset,
15852+ loff_t len)
15853+{
15854+ long err;
15855+ struct au_write_pre wpre;
15856+ struct inode *inode;
15857+ struct file *h_file;
15858+
15859+ inode = file_inode(file);
15860+ au_mtx_and_read_lock(inode);
15861+
15862+ wpre.lsc = 0;
15863+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15864+ err = PTR_ERR(h_file);
15865+ if (IS_ERR(h_file))
15866+ goto out;
15867+
15868+ lockdep_off();
15869+ err = vfs_fallocate(h_file, mode, offset, len);
15870+ lockdep_on();
15871+ au_write_post(inode, h_file, &wpre, /*written*/1);
15872+
15873+out:
15874+ si_read_unlock(inode->i_sb);
15875+ inode_unlock(inode);
15876+ return err;
15877+}
15878+
15879+static ssize_t aufs_copy_file_range(struct file *src, loff_t src_pos,
15880+ struct file *dst, loff_t dst_pos,
15881+ size_t len, unsigned int flags)
15882+{
15883+ ssize_t err;
15884+ struct au_write_pre wpre;
15885+ enum { SRC, DST };
15886+ struct {
15887+ struct inode *inode;
15888+ struct file *h_file;
15889+ struct super_block *h_sb;
15890+ } a[2];
15891+#define a_src a[SRC]
15892+#define a_dst a[DST]
15893+
15894+ err = -EINVAL;
15895+ a_src.inode = file_inode(src);
15896+ if (unlikely(!S_ISREG(a_src.inode->i_mode)))
15897+ goto out;
15898+ a_dst.inode = file_inode(dst);
15899+ if (unlikely(!S_ISREG(a_dst.inode->i_mode)))
15900+ goto out;
15901+
15902+ au_mtx_and_read_lock(a_dst.inode);
15903+ /*
15904+ * in order to match the order in di_write_lock2_{child,parent}(),
15905+ * use f_path.dentry for this comparison.
15906+ */
15907+ if (src->f_path.dentry < dst->f_path.dentry) {
15908+ a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_1);
15909+ err = PTR_ERR(a_src.h_file);
15910+ if (IS_ERR(a_src.h_file))
15911+ goto out_si;
15912+
15913+ wpre.lsc = AuLsc_FI_2;
15914+ a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
15915+ err = PTR_ERR(a_dst.h_file);
15916+ if (IS_ERR(a_dst.h_file)) {
15917+ au_read_post(a_src.inode, a_src.h_file);
15918+ goto out_si;
15919+ }
15920+ } else {
15921+ wpre.lsc = AuLsc_FI_1;
15922+ a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
15923+ err = PTR_ERR(a_dst.h_file);
15924+ if (IS_ERR(a_dst.h_file))
15925+ goto out_si;
15926+
15927+ a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_2);
15928+ err = PTR_ERR(a_src.h_file);
15929+ if (IS_ERR(a_src.h_file)) {
15930+ au_write_post(a_dst.inode, a_dst.h_file, &wpre,
15931+ /*written*/0);
15932+ goto out_si;
15933+ }
15934+ }
15935+
15936+ err = -EXDEV;
15937+ a_src.h_sb = file_inode(a_src.h_file)->i_sb;
15938+ a_dst.h_sb = file_inode(a_dst.h_file)->i_sb;
15939+ if (unlikely(a_src.h_sb != a_dst.h_sb)) {
15940+ AuDbgFile(src);
15941+ AuDbgFile(dst);
15942+ goto out_file;
15943+ }
15944+
15945+ err = vfsub_copy_file_range(a_src.h_file, src_pos, a_dst.h_file,
15946+ dst_pos, len, flags);
15947+
15948+out_file:
15949+ au_write_post(a_dst.inode, a_dst.h_file, &wpre, err);
15950+ fi_read_unlock(src);
15951+ au_read_post(a_src.inode, a_src.h_file);
15952+out_si:
15953+ si_read_unlock(a_dst.inode->i_sb);
15954+ inode_unlock(a_dst.inode);
15955+out:
15956+ return err;
15957+#undef a_src
15958+#undef a_dst
15959+}
15960+
15961+/* ---------------------------------------------------------------------- */
15962+
15963+/*
15964+ * The locking order around current->mmap_sem.
15965+ * - in most and regular cases
15966+ * file I/O syscall -- aufs_read() or something
15967+ * -- si_rwsem for read -- mmap_sem
15968+ * (Note that [fdi]i_rwsem are released before mmap_sem).
15969+ * - in mmap case
15970+ * mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
15971+ * This AB-BA order is definitely bad, but is not a problem since "si_rwsem for
15972+ * read" allows multiple processes to acquire it and [fdi]i_rwsem are not held
15973+ * in file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
15974+ * It means that when aufs acquires si_rwsem for write, the process should never
15975+ * acquire mmap_sem.
15976+ *
15977+ * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
15978+ * problem either since any directory is not able to be mmap-ed.
15979+ * The similar scenario is applied to aufs_readlink() too.
15980+ */
15981+
15982+#if 0 /* stop calling security_file_mmap() */
15983+/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
15984+#define AuConv_VM_PROT(f, b) _calc_vm_trans(f, VM_##b, PROT_##b)
15985+
15986+static unsigned long au_arch_prot_conv(unsigned long flags)
15987+{
15988+ /* currently ppc64 only */
15989+#ifdef CONFIG_PPC64
15990+ /* cf. linux/arch/powerpc/include/asm/mman.h */
15991+ AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
15992+ return AuConv_VM_PROT(flags, SAO);
15993+#else
15994+ AuDebugOn(arch_calc_vm_prot_bits(-1));
15995+ return 0;
15996+#endif
15997+}
15998+
15999+static unsigned long au_prot_conv(unsigned long flags)
16000+{
16001+ return AuConv_VM_PROT(flags, READ)
16002+ | AuConv_VM_PROT(flags, WRITE)
16003+ | AuConv_VM_PROT(flags, EXEC)
16004+ | au_arch_prot_conv(flags);
16005+}
16006+
16007+/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
16008+#define AuConv_VM_MAP(f, b) _calc_vm_trans(f, VM_##b, MAP_##b)
16009+
16010+static unsigned long au_flag_conv(unsigned long flags)
16011+{
16012+ return AuConv_VM_MAP(flags, GROWSDOWN)
16013+ | AuConv_VM_MAP(flags, DENYWRITE)
16014+ | AuConv_VM_MAP(flags, LOCKED);
16015+}
16016+#endif
16017+
16018+static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
16019+{
16020+ int err;
16021+ const unsigned char wlock
16022+ = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
16023+ struct super_block *sb;
16024+ struct file *h_file;
16025+ struct inode *inode;
16026+
16027+ AuDbgVmRegion(file, vma);
16028+
16029+ inode = file_inode(file);
16030+ sb = inode->i_sb;
16031+ lockdep_off();
16032+ si_read_lock(sb, AuLock_NOPLMW);
16033+
16034+ h_file = au_write_pre(file, wlock, /*wpre*/NULL);
16035+ lockdep_on();
16036+ err = PTR_ERR(h_file);
16037+ if (IS_ERR(h_file))
16038+ goto out;
16039+
16040+ err = 0;
16041+ au_set_mmapped(file);
16042+ au_vm_file_reset(vma, h_file);
16043+ /*
16044+ * we cannot call security_mmap_file() here since it may acquire
16045+ * mmap_sem or i_mutex.
16046+ *
16047+ * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
16048+ * au_flag_conv(vma->vm_flags));
16049+ */
16050+ if (!err)
16051+ err = call_mmap(h_file, vma);
16052+ if (!err) {
16053+ au_vm_prfile_set(vma, file);
16054+ fsstack_copy_attr_atime(inode, file_inode(h_file));
16055+ goto out_fput; /* success */
16056+ }
16057+ au_unset_mmapped(file);
16058+ au_vm_file_reset(vma, file);
16059+
16060+out_fput:
16061+ lockdep_off();
16062+ ii_write_unlock(inode);
16063+ lockdep_on();
16064+ fput(h_file);
16065+out:
16066+ lockdep_off();
16067+ si_read_unlock(sb);
16068+ lockdep_on();
16069+ AuTraceErr(err);
16070+ return err;
16071+}
16072+
16073+/* ---------------------------------------------------------------------- */
16074+
16075+static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
16076+ int datasync)
16077+{
16078+ int err;
16079+ struct au_write_pre wpre;
16080+ struct inode *inode;
16081+ struct file *h_file;
16082+
16083+ err = 0; /* -EBADF; */ /* posix? */
16084+ if (unlikely(!(file->f_mode & FMODE_WRITE)))
16085+ goto out;
16086+
16087+ inode = file_inode(file);
16088+ au_mtx_and_read_lock(inode);
16089+
16090+ wpre.lsc = 0;
16091+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16092+ err = PTR_ERR(h_file);
16093+ if (IS_ERR(h_file))
16094+ goto out_unlock;
16095+
16096+ err = vfsub_fsync(h_file, &h_file->f_path, datasync);
16097+ au_write_post(inode, h_file, &wpre, /*written*/0);
16098+
16099+out_unlock:
16100+ si_read_unlock(inode->i_sb);
16101+ inode_unlock(inode);
16102+out:
16103+ return err;
16104+}
16105+
16106+static int aufs_fasync(int fd, struct file *file, int flag)
16107+{
16108+ int err;
16109+ struct file *h_file;
16110+ struct super_block *sb;
16111+
16112+ sb = file->f_path.dentry->d_sb;
16113+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16114+
16115+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16116+ err = PTR_ERR(h_file);
16117+ if (IS_ERR(h_file))
16118+ goto out;
16119+
16120+ if (h_file->f_op->fasync)
16121+ err = h_file->f_op->fasync(fd, h_file, flag);
16122+ fput(h_file); /* instead of au_read_post() */
16123+
16124+out:
16125+ si_read_unlock(sb);
16126+ return err;
16127+}
16128+
16129+static int aufs_setfl(struct file *file, unsigned long arg)
16130+{
16131+ int err;
16132+ struct file *h_file;
16133+ struct super_block *sb;
16134+
16135+ sb = file->f_path.dentry->d_sb;
16136+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16137+
16138+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16139+ err = PTR_ERR(h_file);
16140+ if (IS_ERR(h_file))
16141+ goto out;
16142+
16143+ /* stop calling h_file->fasync */
16144+ arg |= vfsub_file_flags(file) & FASYNC;
16145+ err = setfl(/*unused fd*/-1, h_file, arg);
16146+ fput(h_file); /* instead of au_read_post() */
16147+
16148+out:
16149+ si_read_unlock(sb);
16150+ return err;
16151+}
16152+
16153+/* ---------------------------------------------------------------------- */
16154+
16155+/* no one supports this operation, currently */
16156+#if 0 /* reserved for future use */
16157+static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
16158+ size_t len, loff_t *pos, int more)
16159+{
16160+}
16161+#endif
16162+
16163+/* ---------------------------------------------------------------------- */
16164+
16165+const struct file_operations aufs_file_fop = {
16166+ .owner = THIS_MODULE,
16167+
16168+ .llseek = default_llseek,
16169+
16170+ .read_iter = aufs_read_iter,
16171+ .write_iter = aufs_write_iter,
16172+
16173+#ifdef CONFIG_AUFS_POLL
16174+ .poll = aufs_poll,
16175+#endif
16176+ .unlocked_ioctl = aufs_ioctl_nondir,
16177+#ifdef CONFIG_COMPAT
16178+ .compat_ioctl = aufs_compat_ioctl_nondir,
16179+#endif
16180+ .mmap = aufs_mmap,
16181+ .open = aufs_open_nondir,
16182+ .flush = aufs_flush_nondir,
16183+ .release = aufs_release_nondir,
16184+ .fsync = aufs_fsync_nondir,
16185+ .fasync = aufs_fasync,
16186+ /* .sendpage = aufs_sendpage, */
16187+ .setfl = aufs_setfl,
16188+ .splice_write = aufs_splice_write,
16189+ .splice_read = aufs_splice_read,
16190+#if 0 /* reserved for future use */
16191+ .aio_splice_write = aufs_aio_splice_write,
16192+ .aio_splice_read = aufs_aio_splice_read,
16193+#endif
16194+ .fallocate = aufs_fallocate,
16195+ .copy_file_range = aufs_copy_file_range
16196+};
16197diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
16198--- /usr/share/empty/fs/aufs/fstype.h 1970-01-01 01:00:00.000000000 +0100
16199+++ linux/fs/aufs/fstype.h 2021-12-03 15:38:59.936647310 +0100
16200@@ -0,0 +1,401 @@
16201+/* SPDX-License-Identifier: GPL-2.0 */
16202+/*
16203+ * Copyright (C) 2005-2021 Junjiro R. Okajima
16204+ *
16205+ * This program, aufs is free software; you can redistribute it and/or modify
16206+ * it under the terms of the GNU General Public License as published by
16207+ * the Free Software Foundation; either version 2 of the License, or
16208+ * (at your option) any later version.
16209+ *
16210+ * This program is distributed in the hope that it will be useful,
16211+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16212+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16213+ * GNU General Public License for more details.
16214+ *
16215+ * You should have received a copy of the GNU General Public License
16216+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
16217+ */
16218+
16219+/*
16220+ * judging filesystem type
16221+ */
16222+
16223+#ifndef __AUFS_FSTYPE_H__
16224+#define __AUFS_FSTYPE_H__
16225+
16226+#ifdef __KERNEL__
16227+
16228+#include <linux/fs.h>
16229+#include <linux/magic.h>
16230+#include <linux/nfs_fs.h>
16231+#include <linux/romfs_fs.h>
16232+
16233+static inline int au_test_aufs(struct super_block *sb)
16234+{
16235+ return sb->s_magic == AUFS_SUPER_MAGIC;
16236+}
16237+
16238+static inline const char *au_sbtype(struct super_block *sb)
16239+{
16240+ return sb->s_type->name;
16241+}
16242+
16243+static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
16244+{
16245+#if IS_ENABLED(CONFIG_ISO9660_FS)
16246+ return sb->s_magic == ISOFS_SUPER_MAGIC;
16247+#else
16248+ return 0;
16249+#endif
16250+}
16251+
16252+static inline int au_test_romfs(struct super_block *sb __maybe_unused)
16253+{
16254+#if IS_ENABLED(CONFIG_ROMFS_FS)
16255+ return sb->s_magic == ROMFS_MAGIC;
16256+#else
16257+ return 0;
16258+#endif
16259+}
16260+
16261+static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
16262+{
16263+#if IS_ENABLED(CONFIG_CRAMFS)
16264+ return sb->s_magic == CRAMFS_MAGIC;
16265+#endif
16266+ return 0;
16267+}
16268+
16269+static inline int au_test_nfs(struct super_block *sb __maybe_unused)
16270+{
16271+#if IS_ENABLED(CONFIG_NFS_FS)
16272+ return sb->s_magic == NFS_SUPER_MAGIC;
16273+#else
16274+ return 0;
16275+#endif
16276+}
16277+
16278+static inline int au_test_fuse(struct super_block *sb __maybe_unused)
16279+{
16280+#if IS_ENABLED(CONFIG_FUSE_FS)
16281+ return sb->s_magic == FUSE_SUPER_MAGIC;
16282+#else
16283+ return 0;
16284+#endif
16285+}
16286+
16287+static inline int au_test_xfs(struct super_block *sb __maybe_unused)
16288+{
16289+#if IS_ENABLED(CONFIG_XFS_FS)
16290+ return sb->s_magic == XFS_SB_MAGIC;
16291+#else
16292+ return 0;
16293+#endif
16294+}
16295+
16296+static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
16297+{
16298+#ifdef CONFIG_TMPFS
16299+ return sb->s_magic == TMPFS_MAGIC;
16300+#else
16301+ return 0;
16302+#endif
16303+}
16304+
16305+static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
16306+{
16307+#if IS_ENABLED(CONFIG_ECRYPT_FS)
16308+ return !strcmp(au_sbtype(sb), "ecryptfs");
16309+#else
16310+ return 0;
16311+#endif
16312+}
16313+
16314+static inline int au_test_ramfs(struct super_block *sb)
16315+{
16316+ return sb->s_magic == RAMFS_MAGIC;
16317+}
16318+
16319+static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
16320+{
16321+#if IS_ENABLED(CONFIG_UBIFS_FS)
16322+ return sb->s_magic == UBIFS_SUPER_MAGIC;
16323+#else
16324+ return 0;
16325+#endif
16326+}
16327+
16328+static inline int au_test_procfs(struct super_block *sb __maybe_unused)
16329+{
16330+#ifdef CONFIG_PROC_FS
16331+ return sb->s_magic == PROC_SUPER_MAGIC;
16332+#else
16333+ return 0;
16334+#endif
16335+}
16336+
16337+static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
16338+{
16339+#ifdef CONFIG_SYSFS
16340+ return sb->s_magic == SYSFS_MAGIC;
16341+#else
16342+ return 0;
16343+#endif
16344+}
16345+
16346+static inline int au_test_configfs(struct super_block *sb __maybe_unused)
16347+{
16348+#if IS_ENABLED(CONFIG_CONFIGFS_FS)
16349+ return sb->s_magic == CONFIGFS_MAGIC;
16350+#else
16351+ return 0;
16352+#endif
16353+}
16354+
16355+static inline int au_test_minix(struct super_block *sb __maybe_unused)
16356+{
16357+#if IS_ENABLED(CONFIG_MINIX_FS)
16358+ return sb->s_magic == MINIX3_SUPER_MAGIC
16359+ || sb->s_magic == MINIX2_SUPER_MAGIC
16360+ || sb->s_magic == MINIX2_SUPER_MAGIC2
16361+ || sb->s_magic == MINIX_SUPER_MAGIC
16362+ || sb->s_magic == MINIX_SUPER_MAGIC2;
16363+#else
16364+ return 0;
16365+#endif
16366+}
16367+
16368+static inline int au_test_fat(struct super_block *sb __maybe_unused)
16369+{
16370+#if IS_ENABLED(CONFIG_FAT_FS)
16371+ return sb->s_magic == MSDOS_SUPER_MAGIC;
16372+#else
16373+ return 0;
16374+#endif
16375+}
16376+
16377+static inline int au_test_msdos(struct super_block *sb)
16378+{
16379+ return au_test_fat(sb);
16380+}
16381+
16382+static inline int au_test_vfat(struct super_block *sb)
16383+{
16384+ return au_test_fat(sb);
16385+}
16386+
16387+static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
16388+{
16389+#ifdef CONFIG_SECURITYFS
16390+ return sb->s_magic == SECURITYFS_MAGIC;
16391+#else
16392+ return 0;
16393+#endif
16394+}
16395+
16396+static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
16397+{
16398+#if IS_ENABLED(CONFIG_SQUASHFS)
16399+ return sb->s_magic == SQUASHFS_MAGIC;
16400+#else
16401+ return 0;
16402+#endif
16403+}
16404+
16405+static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
16406+{
16407+#if IS_ENABLED(CONFIG_BTRFS_FS)
16408+ return sb->s_magic == BTRFS_SUPER_MAGIC;
16409+#else
16410+ return 0;
16411+#endif
16412+}
16413+
16414+static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
16415+{
16416+#if IS_ENABLED(CONFIG_XENFS)
16417+ return sb->s_magic == XENFS_SUPER_MAGIC;
16418+#else
16419+ return 0;
16420+#endif
16421+}
16422+
16423+static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
16424+{
16425+#ifdef CONFIG_DEBUG_FS
16426+ return sb->s_magic == DEBUGFS_MAGIC;
16427+#else
16428+ return 0;
16429+#endif
16430+}
16431+
16432+static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
16433+{
16434+#if IS_ENABLED(CONFIG_NILFS)
16435+ return sb->s_magic == NILFS_SUPER_MAGIC;
16436+#else
16437+ return 0;
16438+#endif
16439+}
16440+
16441+static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
16442+{
16443+#if IS_ENABLED(CONFIG_HFSPLUS_FS)
16444+ return sb->s_magic == HFSPLUS_SUPER_MAGIC;
16445+#else
16446+ return 0;
16447+#endif
16448+}
16449+
16450+/* ---------------------------------------------------------------------- */
16451+/*
16452+ * they can't be an aufs branch.
16453+ */
16454+static inline int au_test_fs_unsuppoted(struct super_block *sb)
16455+{
16456+ return
16457+#ifndef CONFIG_AUFS_BR_RAMFS
16458+ au_test_ramfs(sb) ||
16459+#endif
16460+ au_test_procfs(sb)
16461+ || au_test_sysfs(sb)
16462+ || au_test_configfs(sb)
16463+ || au_test_debugfs(sb)
16464+ || au_test_securityfs(sb)
16465+ || au_test_xenfs(sb)
16466+ || au_test_ecryptfs(sb)
16467+ /* || !strcmp(au_sbtype(sb), "unionfs") */
16468+ || au_test_aufs(sb); /* will be supported in next version */
16469+}
16470+
16471+static inline int au_test_fs_remote(struct super_block *sb)
16472+{
16473+ return !au_test_tmpfs(sb)
16474+#ifdef CONFIG_AUFS_BR_RAMFS
16475+ && !au_test_ramfs(sb)
16476+#endif
16477+ && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
16478+}
16479+
16480+/* ---------------------------------------------------------------------- */
16481+
16482+/*
16483+ * Note: these functions (below) are created after reading ->getattr() in all
16484+ * filesystems under linux/fs. it means we have to do so in every update...
16485+ */
16486+
16487+/*
16488+ * some filesystems require getattr to refresh the inode attributes before
16489+ * referencing.
16490+ * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
16491+ * and leave the work for d_revalidate()
16492+ */
16493+static inline int au_test_fs_refresh_iattr(struct super_block *sb)
16494+{
16495+ return au_test_nfs(sb)
16496+ || au_test_fuse(sb)
16497+ /* || au_test_btrfs(sb) */ /* untested */
16498+ ;
16499+}
16500+
16501+/*
16502+ * filesystems which don't maintain i_size or i_blocks.
16503+ */
16504+static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
16505+{
16506+ return au_test_xfs(sb)
16507+ || au_test_btrfs(sb)
16508+ || au_test_ubifs(sb)
16509+ || au_test_hfsplus(sb) /* maintained, but incorrect */
16510+ /* || au_test_minix(sb) */ /* untested */
16511+ ;
16512+}
16513+
16514+/*
16515+ * filesystems which don't store the correct value in some of their inode
16516+ * attributes.
16517+ */
16518+static inline int au_test_fs_bad_iattr(struct super_block *sb)
16519+{
16520+ return au_test_fs_bad_iattr_size(sb)
16521+ || au_test_fat(sb)
16522+ || au_test_msdos(sb)
16523+ || au_test_vfat(sb);
16524+}
16525+
16526+/* they don't check i_nlink in link(2) */
16527+static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
16528+{
16529+ return au_test_tmpfs(sb)
16530+#ifdef CONFIG_AUFS_BR_RAMFS
16531+ || au_test_ramfs(sb)
16532+#endif
16533+ || au_test_ubifs(sb)
16534+ || au_test_hfsplus(sb);
16535+}
16536+
16537+/*
16538+ * filesystems which sets S_NOATIME and S_NOCMTIME.
16539+ */
16540+static inline int au_test_fs_notime(struct super_block *sb)
16541+{
16542+ return au_test_nfs(sb)
16543+ || au_test_fuse(sb)
16544+ || au_test_ubifs(sb)
16545+ ;
16546+}
16547+
16548+/* temporary support for i#1 in cramfs */
16549+static inline int au_test_fs_unique_ino(struct inode *inode)
16550+{
16551+ if (au_test_cramfs(inode->i_sb))
16552+ return inode->i_ino != 1;
16553+ return 1;
16554+}
16555+
16556+/* ---------------------------------------------------------------------- */
16557+
16558+/*
16559+ * the filesystem where the xino files placed must support i/o after unlink and
16560+ * maintain i_size and i_blocks.
16561+ */
16562+static inline int au_test_fs_bad_xino(struct super_block *sb)
16563+{
16564+ return au_test_fs_remote(sb)
16565+ || au_test_fs_bad_iattr_size(sb)
16566+ /* don't want unnecessary work for xino */
16567+ || au_test_aufs(sb)
16568+ || au_test_ecryptfs(sb)
16569+ || au_test_nilfs(sb);
16570+}
16571+
16572+static inline int au_test_fs_trunc_xino(struct super_block *sb)
16573+{
16574+ return au_test_tmpfs(sb)
16575+ || au_test_ramfs(sb);
16576+}
16577+
16578+/*
16579+ * test if the @sb is real-readonly.
16580+ */
16581+static inline int au_test_fs_rr(struct super_block *sb)
16582+{
16583+ return au_test_squashfs(sb)
16584+ || au_test_iso9660(sb)
16585+ || au_test_cramfs(sb)
16586+ || au_test_romfs(sb);
16587+}
16588+
16589+/*
16590+ * test if the @inode is nfs with 'noacl' option
16591+ * NFS always sets SB_POSIXACL regardless its mount option 'noacl.'
16592+ */
16593+static inline int au_test_nfs_noacl(struct inode *inode)
16594+{
16595+ return au_test_nfs(inode->i_sb)
16596+ /* && IS_POSIXACL(inode) */
16597+ && !nfs_server_capable(inode, NFS_CAP_ACLS);
16598+}
16599+
16600+#endif /* __KERNEL__ */
16601+#endif /* __AUFS_FSTYPE_H__ */
16602diff -urN /usr/share/empty/fs/aufs/hbl.h linux/fs/aufs/hbl.h
16603--- /usr/share/empty/fs/aufs/hbl.h 1970-01-01 01:00:00.000000000 +0100
16604+++ linux/fs/aufs/hbl.h 2021-12-03 15:38:59.936647310 +0100
16605@@ -0,0 +1,65 @@
16606+/* SPDX-License-Identifier: GPL-2.0 */
16607+/*
16608+ * Copyright (C) 2017-2021 Junjiro R. Okajima
16609+ *
16610+ * This program, aufs is free software; you can redistribute it and/or modify
16611+ * it under the terms of the GNU General Public License as published by
16612+ * the Free Software Foundation; either version 2 of the License, or
16613+ * (at your option) any later version.
16614+ *
16615+ * This program is distributed in the hope that it will be useful,
16616+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16617+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16618+ * GNU General Public License for more details.
16619+ *
16620+ * You should have received a copy of the GNU General Public License
16621+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
16622+ */
16623+
16624+/*
16625+ * helpers for hlist_bl.h
16626+ */
16627+
16628+#ifndef __AUFS_HBL_H__
16629+#define __AUFS_HBL_H__
16630+
16631+#ifdef __KERNEL__
16632+
16633+#include <linux/list_bl.h>
16634+
16635+static inline void au_hbl_add(struct hlist_bl_node *node,
16636+ struct hlist_bl_head *hbl)
16637+{
16638+ hlist_bl_lock(hbl);
16639+ hlist_bl_add_head(node, hbl);
16640+ hlist_bl_unlock(hbl);
16641+}
16642+
16643+static inline void au_hbl_del(struct hlist_bl_node *node,
16644+ struct hlist_bl_head *hbl)
16645+{
16646+ hlist_bl_lock(hbl);
16647+ hlist_bl_del(node);
16648+ hlist_bl_unlock(hbl);
16649+}
16650+
16651+#define au_hbl_for_each(pos, head) \
16652+ for (pos = hlist_bl_first(head); \
16653+ pos; \
16654+ pos = pos->next)
16655+
16656+static inline unsigned long au_hbl_count(struct hlist_bl_head *hbl)
16657+{
16658+ unsigned long cnt;
16659+ struct hlist_bl_node *pos;
16660+
16661+ cnt = 0;
16662+ hlist_bl_lock(hbl);
16663+ au_hbl_for_each(pos, hbl)
16664+ cnt++;
16665+ hlist_bl_unlock(hbl);
16666+ return cnt;
16667+}
16668+
16669+#endif /* __KERNEL__ */
16670+#endif /* __AUFS_HBL_H__ */
16671diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
16672--- /usr/share/empty/fs/aufs/hfsnotify.c 1970-01-01 01:00:00.000000000 +0100
16673+++ linux/fs/aufs/hfsnotify.c 2021-12-03 15:38:59.936647310 +0100
16674@@ -0,0 +1,288 @@
16675+// SPDX-License-Identifier: GPL-2.0
16676+/*
16677+ * Copyright (C) 2005-2021 Junjiro R. Okajima
16678+ *
16679+ * This program, aufs is free software; you can redistribute it and/or modify
16680+ * it under the terms of the GNU General Public License as published by
16681+ * the Free Software Foundation; either version 2 of the License, or
16682+ * (at your option) any later version.
16683+ *
16684+ * This program is distributed in the hope that it will be useful,
16685+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16686+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16687+ * GNU General Public License for more details.
16688+ *
16689+ * You should have received a copy of the GNU General Public License
16690+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
16691+ */
16692+
16693+/*
16694+ * fsnotify for the lower directories
16695+ */
16696+
16697+#include "aufs.h"
16698+
16699+/* FS_IN_IGNORED is unnecessary */
16700+static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
16701+ | FS_CREATE | FS_EVENT_ON_CHILD);
16702+static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
16703+static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
16704+
16705+static void au_hfsn_free_mark(struct fsnotify_mark *mark)
16706+{
16707+ struct au_hnotify *hn = container_of(mark, struct au_hnotify,
16708+ hn_mark);
16709+ /* AuDbg("here\n"); */
16710+ au_cache_free_hnotify(hn);
16711+ smp_mb__before_atomic(); /* for atomic64_dec */
16712+ if (atomic64_dec_and_test(&au_hfsn_ifree))
16713+ wake_up(&au_hfsn_wq);
16714+}
16715+
16716+static int au_hfsn_alloc(struct au_hinode *hinode)
16717+{
16718+ int err;
16719+ struct au_hnotify *hn;
16720+ struct super_block *sb;
16721+ struct au_branch *br;
16722+ struct fsnotify_mark *mark;
16723+ aufs_bindex_t bindex;
16724+
16725+ hn = hinode->hi_notify;
16726+ sb = hn->hn_aufs_inode->i_sb;
16727+ bindex = au_br_index(sb, hinode->hi_id);
16728+ br = au_sbr(sb, bindex);
16729+ AuDebugOn(!br->br_hfsn);
16730+
16731+ mark = &hn->hn_mark;
16732+ fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
16733+ mark->mask = AuHfsnMask;
16734+ /*
16735+ * by udba rename or rmdir, aufs assign a new inode to the known
16736+ * h_inode, so specify 1 to allow dups.
16737+ */
16738+ lockdep_off();
16739+ err = fsnotify_add_inode_mark(mark, hinode->hi_inode, /*allow_dups*/1);
16740+ lockdep_on();
16741+
16742+ return err;
16743+}
16744+
16745+static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
16746+{
16747+ struct fsnotify_mark *mark;
16748+ unsigned long long ull;
16749+ struct fsnotify_group *group;
16750+
16751+ ull = atomic64_inc_return(&au_hfsn_ifree);
16752+ BUG_ON(!ull);
16753+
16754+ mark = &hn->hn_mark;
16755+ spin_lock(&mark->lock);
16756+ group = mark->group;
16757+ fsnotify_get_group(group);
16758+ spin_unlock(&mark->lock);
16759+ lockdep_off();
16760+ fsnotify_destroy_mark(mark, group);
16761+ fsnotify_put_mark(mark);
16762+ fsnotify_put_group(group);
16763+ lockdep_on();
16764+
16765+ /* free hn by myself */
16766+ return 0;
16767+}
16768+
16769+/* ---------------------------------------------------------------------- */
16770+
16771+static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
16772+{
16773+ struct fsnotify_mark *mark;
16774+
16775+ mark = &hinode->hi_notify->hn_mark;
16776+ spin_lock(&mark->lock);
16777+ if (do_set) {
16778+ AuDebugOn(mark->mask & AuHfsnMask);
16779+ mark->mask |= AuHfsnMask;
16780+ } else {
16781+ AuDebugOn(!(mark->mask & AuHfsnMask));
16782+ mark->mask &= ~AuHfsnMask;
16783+ }
16784+ spin_unlock(&mark->lock);
16785+ /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
16786+}
16787+
16788+/* ---------------------------------------------------------------------- */
16789+
16790+/* #define AuDbgHnotify */
16791+#ifdef AuDbgHnotify
16792+static char *au_hfsn_name(u32 mask)
16793+{
16794+#ifdef CONFIG_AUFS_DEBUG
16795+#define test_ret(flag) \
16796+ do { \
16797+ if (mask & flag) \
16798+ return #flag; \
16799+ } while (0)
16800+ test_ret(FS_ACCESS);
16801+ test_ret(FS_MODIFY);
16802+ test_ret(FS_ATTRIB);
16803+ test_ret(FS_CLOSE_WRITE);
16804+ test_ret(FS_CLOSE_NOWRITE);
16805+ test_ret(FS_OPEN);
16806+ test_ret(FS_MOVED_FROM);
16807+ test_ret(FS_MOVED_TO);
16808+ test_ret(FS_CREATE);
16809+ test_ret(FS_DELETE);
16810+ test_ret(FS_DELETE_SELF);
16811+ test_ret(FS_MOVE_SELF);
16812+ test_ret(FS_UNMOUNT);
16813+ test_ret(FS_Q_OVERFLOW);
16814+ test_ret(FS_IN_IGNORED);
16815+ test_ret(FS_ISDIR);
16816+ test_ret(FS_IN_ONESHOT);
16817+ test_ret(FS_EVENT_ON_CHILD);
16818+ return "";
16819+#undef test_ret
16820+#else
16821+ return "??";
16822+#endif
16823+}
16824+#endif
16825+
16826+/* ---------------------------------------------------------------------- */
16827+
16828+static void au_hfsn_free_group(struct fsnotify_group *group)
16829+{
16830+ struct au_br_hfsnotify *hfsn = group->private;
16831+
16832+ /* AuDbg("here\n"); */
16833+ au_kfree_try_rcu(hfsn);
16834+}
16835+
16836+static int au_hfsn_handle_event(struct fsnotify_group *group,
16837+ u32 mask, const void *data, int data_type,
16838+ struct inode *dir,
16839+ const struct qstr *file_name, u32 cookie,
16840+ struct fsnotify_iter_info *iter_info)
16841+{
16842+ int err;
16843+ struct au_hnotify *hnotify;
16844+ struct inode *h_dir, *h_inode;
16845+ struct fsnotify_mark *inode_mark;
16846+
16847+ AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
16848+
16849+ err = 0;
16850+ /* if FS_UNMOUNT happens, there must be another bug */
16851+ AuDebugOn(mask & FS_UNMOUNT);
16852+ if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
16853+ goto out;
16854+
16855+ h_dir = dir;
16856+ h_inode = NULL;
16857+#ifdef AuDbgHnotify
16858+ au_debug_on();
16859+ if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
16860+ || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
16861+ AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
16862+ h_dir->i_ino, mask, au_hfsn_name(mask),
16863+ AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
16864+ /* WARN_ON(1); */
16865+ }
16866+ au_debug_off();
16867+#endif
16868+
16869+ inode_mark = fsnotify_iter_inode_mark(iter_info);
16870+ AuDebugOn(!inode_mark);
16871+ hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
16872+ err = au_hnotify(h_dir, hnotify, mask, file_name, h_inode);
16873+
16874+out:
16875+ return err;
16876+}
16877+
16878+static struct fsnotify_ops au_hfsn_ops = {
16879+ .handle_event = au_hfsn_handle_event,
16880+ .free_group_priv = au_hfsn_free_group,
16881+ .free_mark = au_hfsn_free_mark
16882+};
16883+
16884+/* ---------------------------------------------------------------------- */
16885+
16886+static void au_hfsn_fin_br(struct au_branch *br)
16887+{
16888+ struct au_br_hfsnotify *hfsn;
16889+
16890+ hfsn = br->br_hfsn;
16891+ if (hfsn) {
16892+ lockdep_off();
16893+ fsnotify_put_group(hfsn->hfsn_group);
16894+ lockdep_on();
16895+ }
16896+}
16897+
16898+static int au_hfsn_init_br(struct au_branch *br, int perm)
16899+{
16900+ int err;
16901+ struct fsnotify_group *group;
16902+ struct au_br_hfsnotify *hfsn;
16903+
16904+ err = 0;
16905+ br->br_hfsn = NULL;
16906+ if (!au_br_hnotifyable(perm))
16907+ goto out;
16908+
16909+ err = -ENOMEM;
16910+ hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
16911+ if (unlikely(!hfsn))
16912+ goto out;
16913+
16914+ err = 0;
16915+ group = fsnotify_alloc_group(&au_hfsn_ops);
16916+ if (IS_ERR(group)) {
16917+ err = PTR_ERR(group);
16918+ pr_err("fsnotify_alloc_group() failed, %d\n", err);
16919+ goto out_hfsn;
16920+ }
16921+
16922+ group->private = hfsn;
16923+ hfsn->hfsn_group = group;
16924+ br->br_hfsn = hfsn;
16925+ goto out; /* success */
16926+
16927+out_hfsn:
16928+ au_kfree_try_rcu(hfsn);
16929+out:
16930+ return err;
16931+}
16932+
16933+static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
16934+{
16935+ int err;
16936+
16937+ err = 0;
16938+ if (!br->br_hfsn)
16939+ err = au_hfsn_init_br(br, perm);
16940+
16941+ return err;
16942+}
16943+
16944+/* ---------------------------------------------------------------------- */
16945+
16946+static void au_hfsn_fin(void)
16947+{
16948+ AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
16949+ wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
16950+}
16951+
16952+const struct au_hnotify_op au_hnotify_op = {
16953+ .ctl = au_hfsn_ctl,
16954+ .alloc = au_hfsn_alloc,
16955+ .free = au_hfsn_free,
16956+
16957+ .fin = au_hfsn_fin,
16958+
16959+ .reset_br = au_hfsn_reset_br,
16960+ .fin_br = au_hfsn_fin_br,
16961+ .init_br = au_hfsn_init_br
16962+};
16963diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
16964--- /usr/share/empty/fs/aufs/hfsplus.c 1970-01-01 01:00:00.000000000 +0100
16965+++ linux/fs/aufs/hfsplus.c 2021-12-03 15:38:59.936647310 +0100
16966@@ -0,0 +1,60 @@
16967+// SPDX-License-Identifier: GPL-2.0
16968+/*
16969+ * Copyright (C) 2010-2021 Junjiro R. Okajima
16970+ *
16971+ * This program, aufs is free software; you can redistribute it and/or modify
16972+ * it under the terms of the GNU General Public License as published by
16973+ * the Free Software Foundation; either version 2 of the License, or
16974+ * (at your option) any later version.
16975+ *
16976+ * This program is distributed in the hope that it will be useful,
16977+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16978+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16979+ * GNU General Public License for more details.
16980+ *
16981+ * You should have received a copy of the GNU General Public License
16982+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
16983+ */
16984+
16985+/*
16986+ * special support for filesystems which acquires an inode mutex
16987+ * at final closing a file, eg, hfsplus.
16988+ *
16989+ * This trick is very simple and stupid, just to open the file before really
16990+ * necessary open to tell hfsplus that this is not the final closing.
16991+ * The caller should call au_h_open_pre() after acquiring the inode mutex,
16992+ * and au_h_open_post() after releasing it.
16993+ */
16994+
16995+#include "aufs.h"
16996+
16997+struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
16998+ int force_wr)
16999+{
17000+ struct file *h_file;
17001+ struct dentry *h_dentry;
17002+
17003+ h_dentry = au_h_dptr(dentry, bindex);
17004+ AuDebugOn(!h_dentry);
17005+ AuDebugOn(d_is_negative(h_dentry));
17006+
17007+ h_file = NULL;
17008+ if (au_test_hfsplus(h_dentry->d_sb)
17009+ && d_is_reg(h_dentry))
17010+ h_file = au_h_open(dentry, bindex,
17011+ O_RDONLY | O_NOATIME | O_LARGEFILE,
17012+ /*file*/NULL, force_wr);
17013+ return h_file;
17014+}
17015+
17016+void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
17017+ struct file *h_file)
17018+{
17019+ struct au_branch *br;
17020+
17021+ if (h_file) {
17022+ fput(h_file);
17023+ br = au_sbr(dentry->d_sb, bindex);
17024+ au_lcnt_dec(&br->br_nfiles);
17025+ }
17026+}
17027diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
17028--- /usr/share/empty/fs/aufs/hnotify.c 1970-01-01 01:00:00.000000000 +0100
17029+++ linux/fs/aufs/hnotify.c 2021-12-03 15:38:59.936647310 +0100
17030@@ -0,0 +1,715 @@
17031+// SPDX-License-Identifier: GPL-2.0
17032+/*
17033+ * Copyright (C) 2005-2021 Junjiro R. Okajima
17034+ *
17035+ * This program, aufs is free software; you can redistribute it and/or modify
17036+ * it under the terms of the GNU General Public License as published by
17037+ * the Free Software Foundation; either version 2 of the License, or
17038+ * (at your option) any later version.
17039+ *
17040+ * This program is distributed in the hope that it will be useful,
17041+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17042+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17043+ * GNU General Public License for more details.
17044+ *
17045+ * You should have received a copy of the GNU General Public License
17046+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
17047+ */
17048+
17049+/*
17050+ * abstraction to notify the direct changes on lower directories
17051+ */
17052+
17053+/* #include <linux/iversion.h> */
17054+#include "aufs.h"
17055+
17056+int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
17057+{
17058+ int err;
17059+ struct au_hnotify *hn;
17060+
17061+ err = -ENOMEM;
17062+ hn = au_cache_alloc_hnotify();
17063+ if (hn) {
17064+ hn->hn_aufs_inode = inode;
17065+ hinode->hi_notify = hn;
17066+ err = au_hnotify_op.alloc(hinode);
17067+ AuTraceErr(err);
17068+ if (unlikely(err)) {
17069+ hinode->hi_notify = NULL;
17070+ au_cache_free_hnotify(hn);
17071+ /*
17072+ * The upper dir was removed by udba, but the same named
17073+ * dir left. In this case, aufs assigns a new inode
17074+ * number and set the monitor again.
17075+ * For the lower dir, the old monitor is still left.
17076+ */
17077+ if (err == -EEXIST)
17078+ err = 0;
17079+ }
17080+ }
17081+
17082+ AuTraceErr(err);
17083+ return err;
17084+}
17085+
17086+void au_hn_free(struct au_hinode *hinode)
17087+{
17088+ struct au_hnotify *hn;
17089+
17090+ hn = hinode->hi_notify;
17091+ if (hn) {
17092+ hinode->hi_notify = NULL;
17093+ if (au_hnotify_op.free(hinode, hn))
17094+ au_cache_free_hnotify(hn);
17095+ }
17096+}
17097+
17098+/* ---------------------------------------------------------------------- */
17099+
17100+void au_hn_ctl(struct au_hinode *hinode, int do_set)
17101+{
17102+ if (hinode->hi_notify)
17103+ au_hnotify_op.ctl(hinode, do_set);
17104+}
17105+
17106+void au_hn_reset(struct inode *inode, unsigned int flags)
17107+{
17108+ aufs_bindex_t bindex, bbot;
17109+ struct inode *hi;
17110+ struct dentry *iwhdentry;
17111+
17112+ bbot = au_ibbot(inode);
17113+ for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
17114+ hi = au_h_iptr(inode, bindex);
17115+ if (!hi)
17116+ continue;
17117+
17118+ /* inode_lock_nested(hi, AuLsc_I_CHILD); */
17119+ iwhdentry = au_hi_wh(inode, bindex);
17120+ if (iwhdentry)
17121+ dget(iwhdentry);
17122+ au_igrab(hi);
17123+ au_set_h_iptr(inode, bindex, NULL, 0);
17124+ au_set_h_iptr(inode, bindex, au_igrab(hi),
17125+ flags & ~AuHi_XINO);
17126+ iput(hi);
17127+ dput(iwhdentry);
17128+ /* inode_unlock(hi); */
17129+ }
17130+}
17131+
17132+/* ---------------------------------------------------------------------- */
17133+
17134+static int hn_xino(struct inode *inode, struct inode *h_inode)
17135+{
17136+ int err;
17137+ aufs_bindex_t bindex, bbot, bfound, btop;
17138+ struct inode *h_i;
17139+
17140+ err = 0;
17141+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
17142+ pr_warn("branch root dir was changed\n");
17143+ goto out;
17144+ }
17145+
17146+ bfound = -1;
17147+ bbot = au_ibbot(inode);
17148+ btop = au_ibtop(inode);
17149+#if 0 /* reserved for future use */
17150+ if (bindex == bbot) {
17151+ /* keep this ino in rename case */
17152+ goto out;
17153+ }
17154+#endif
17155+ for (bindex = btop; bindex <= bbot; bindex++)
17156+ if (au_h_iptr(inode, bindex) == h_inode) {
17157+ bfound = bindex;
17158+ break;
17159+ }
17160+ if (bfound < 0)
17161+ goto out;
17162+
17163+ for (bindex = btop; bindex <= bbot; bindex++) {
17164+ h_i = au_h_iptr(inode, bindex);
17165+ if (!h_i)
17166+ continue;
17167+
17168+ err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
17169+ /* ignore this error */
17170+ /* bad action? */
17171+ }
17172+
17173+ /* children inode number will be broken */
17174+
17175+out:
17176+ AuTraceErr(err);
17177+ return err;
17178+}
17179+
17180+static int hn_gen_tree(struct dentry *dentry)
17181+{
17182+ int err, i, j, ndentry;
17183+ struct au_dcsub_pages dpages;
17184+ struct au_dpage *dpage;
17185+ struct dentry **dentries;
17186+
17187+ err = au_dpages_init(&dpages, GFP_NOFS);
17188+ if (unlikely(err))
17189+ goto out;
17190+ err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
17191+ if (unlikely(err))
17192+ goto out_dpages;
17193+
17194+ for (i = 0; i < dpages.ndpage; i++) {
17195+ dpage = dpages.dpages + i;
17196+ dentries = dpage->dentries;
17197+ ndentry = dpage->ndentry;
17198+ for (j = 0; j < ndentry; j++) {
17199+ struct dentry *d;
17200+
17201+ d = dentries[j];
17202+ if (IS_ROOT(d))
17203+ continue;
17204+
17205+ au_digen_dec(d);
17206+ if (d_really_is_positive(d))
17207+ /* todo: reset children xino?
17208+ cached children only? */
17209+ au_iigen_dec(d_inode(d));
17210+ }
17211+ }
17212+
17213+out_dpages:
17214+ au_dpages_free(&dpages);
17215+out:
17216+ return err;
17217+}
17218+
17219+/*
17220+ * return 0 if processed.
17221+ */
17222+static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
17223+ const unsigned int isdir)
17224+{
17225+ int err;
17226+ struct dentry *d;
17227+ struct qstr *dname;
17228+
17229+ err = 1;
17230+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
17231+ pr_warn("branch root dir was changed\n");
17232+ err = 0;
17233+ goto out;
17234+ }
17235+
17236+ if (!isdir) {
17237+ AuDebugOn(!name);
17238+ au_iigen_dec(inode);
17239+ spin_lock(&inode->i_lock);
17240+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
17241+ spin_lock(&d->d_lock);
17242+ dname = &d->d_name;
17243+ if (dname->len != nlen
17244+ && memcmp(dname->name, name, nlen)) {
17245+ spin_unlock(&d->d_lock);
17246+ continue;
17247+ }
17248+ err = 0;
17249+ au_digen_dec(d);
17250+ spin_unlock(&d->d_lock);
17251+ break;
17252+ }
17253+ spin_unlock(&inode->i_lock);
17254+ } else {
17255+ au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
17256+ d = d_find_any_alias(inode);
17257+ if (!d) {
17258+ au_iigen_dec(inode);
17259+ goto out;
17260+ }
17261+
17262+ spin_lock(&d->d_lock);
17263+ dname = &d->d_name;
17264+ if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
17265+ spin_unlock(&d->d_lock);
17266+ err = hn_gen_tree(d);
17267+ spin_lock(&d->d_lock);
17268+ }
17269+ spin_unlock(&d->d_lock);
17270+ dput(d);
17271+ }
17272+
17273+out:
17274+ AuTraceErr(err);
17275+ return err;
17276+}
17277+
17278+static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
17279+{
17280+ int err;
17281+
17282+ if (IS_ROOT(dentry)) {
17283+ pr_warn("branch root dir was changed\n");
17284+ return 0;
17285+ }
17286+
17287+ err = 0;
17288+ if (!isdir) {
17289+ au_digen_dec(dentry);
17290+ if (d_really_is_positive(dentry))
17291+ au_iigen_dec(d_inode(dentry));
17292+ } else {
17293+ au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
17294+ if (d_really_is_positive(dentry))
17295+ err = hn_gen_tree(dentry);
17296+ }
17297+
17298+ AuTraceErr(err);
17299+ return err;
17300+}
17301+
17302+/* ---------------------------------------------------------------------- */
17303+
17304+/* hnotify job flags */
17305+#define AuHnJob_XINO0 1
17306+#define AuHnJob_GEN (1 << 1)
17307+#define AuHnJob_DIRENT (1 << 2)
17308+#define AuHnJob_ISDIR (1 << 3)
17309+#define AuHnJob_TRYXINO0 (1 << 4)
17310+#define AuHnJob_MNTPNT (1 << 5)
17311+#define au_ftest_hnjob(flags, name) ((flags) & AuHnJob_##name)
17312+#define au_fset_hnjob(flags, name) \
17313+ do { (flags) |= AuHnJob_##name; } while (0)
17314+#define au_fclr_hnjob(flags, name) \
17315+ do { (flags) &= ~AuHnJob_##name; } while (0)
17316+
17317+enum {
17318+ AuHn_CHILD,
17319+ AuHn_PARENT,
17320+ AuHnLast
17321+};
17322+
17323+struct au_hnotify_args {
17324+ struct inode *h_dir, *dir, *h_child_inode;
17325+ u32 mask;
17326+ unsigned int flags[AuHnLast];
17327+ unsigned int h_child_nlen;
17328+ char h_child_name[];
17329+};
17330+
17331+struct hn_job_args {
17332+ unsigned int flags;
17333+ struct inode *inode, *h_inode, *dir, *h_dir;
17334+ struct dentry *dentry;
17335+ char *h_name;
17336+ int h_nlen;
17337+};
17338+
17339+static int hn_job(struct hn_job_args *a)
17340+{
17341+ const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
17342+ int e;
17343+
17344+ /* reset xino */
17345+ if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
17346+ hn_xino(a->inode, a->h_inode); /* ignore this error */
17347+
17348+ if (au_ftest_hnjob(a->flags, TRYXINO0)
17349+ && a->inode
17350+ && a->h_inode) {
17351+ inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
17352+ if (!a->h_inode->i_nlink
17353+ && !(a->h_inode->i_state & I_LINKABLE))
17354+ hn_xino(a->inode, a->h_inode); /* ignore this error */
17355+ inode_unlock_shared(a->h_inode);
17356+ }
17357+
17358+ /* make the generation obsolete */
17359+ if (au_ftest_hnjob(a->flags, GEN)) {
17360+ e = -1;
17361+ if (a->inode)
17362+ e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
17363+ isdir);
17364+ if (e && a->dentry)
17365+ hn_gen_by_name(a->dentry, isdir);
17366+ /* ignore this error */
17367+ }
17368+
17369+ /* make dir entries obsolete */
17370+ if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
17371+ struct au_vdir *vdir;
17372+
17373+ vdir = au_ivdir(a->inode);
17374+ if (vdir)
17375+ vdir->vd_jiffy = 0;
17376+ /* IMustLock(a->inode); */
17377+ /* inode_inc_iversion(a->inode); */
17378+ }
17379+
17380+ /* can do nothing but warn */
17381+ if (au_ftest_hnjob(a->flags, MNTPNT)
17382+ && a->dentry
17383+ && d_mountpoint(a->dentry))
17384+ pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
17385+
17386+ return 0;
17387+}
17388+
17389+/* ---------------------------------------------------------------------- */
17390+
17391+static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
17392+ struct inode *dir)
17393+{
17394+ struct dentry *dentry, *d, *parent;
17395+ struct qstr *dname;
17396+
17397+ parent = d_find_any_alias(dir);
17398+ if (!parent)
17399+ return NULL;
17400+
17401+ dentry = NULL;
17402+ spin_lock(&parent->d_lock);
17403+ list_for_each_entry(d, &parent->d_subdirs, d_child) {
17404+ /* AuDbg("%pd\n", d); */
17405+ spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
17406+ dname = &d->d_name;
17407+ if (dname->len != nlen || memcmp(dname->name, name, nlen))
17408+ goto cont_unlock;
17409+ if (au_di(d))
17410+ au_digen_dec(d);
17411+ else
17412+ goto cont_unlock;
17413+ if (au_dcount(d) > 0) {
17414+ dentry = dget_dlock(d);
17415+ spin_unlock(&d->d_lock);
17416+ break;
17417+ }
17418+
17419+cont_unlock:
17420+ spin_unlock(&d->d_lock);
17421+ }
17422+ spin_unlock(&parent->d_lock);
17423+ dput(parent);
17424+
17425+ if (dentry)
17426+ di_write_lock_child(dentry);
17427+
17428+ return dentry;
17429+}
17430+
17431+static struct inode *lookup_wlock_by_ino(struct super_block *sb,
17432+ aufs_bindex_t bindex, ino_t h_ino)
17433+{
17434+ struct inode *inode;
17435+ ino_t ino;
17436+ int err;
17437+
17438+ inode = NULL;
17439+ err = au_xino_read(sb, bindex, h_ino, &ino);
17440+ if (!err && ino)
17441+ inode = ilookup(sb, ino);
17442+ if (!inode)
17443+ goto out;
17444+
17445+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
17446+ pr_warn("wrong root branch\n");
17447+ iput(inode);
17448+ inode = NULL;
17449+ goto out;
17450+ }
17451+
17452+ ii_write_lock_child(inode);
17453+
17454+out:
17455+ return inode;
17456+}
17457+
17458+static void au_hn_bh(void *_args)
17459+{
17460+ struct au_hnotify_args *a = _args;
17461+ struct super_block *sb;
17462+ aufs_bindex_t bindex, bbot, bfound;
17463+ unsigned char xino, try_iput;
17464+ int err;
17465+ struct inode *inode;
17466+ ino_t h_ino;
17467+ struct hn_job_args args;
17468+ struct dentry *dentry;
17469+ struct au_sbinfo *sbinfo;
17470+
17471+ AuDebugOn(!_args);
17472+ AuDebugOn(!a->h_dir);
17473+ AuDebugOn(!a->dir);
17474+ AuDebugOn(!a->mask);
17475+ AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
17476+ a->mask, a->dir->i_ino, a->h_dir->i_ino,
17477+ a->h_child_inode ? a->h_child_inode->i_ino : 0);
17478+
17479+ inode = NULL;
17480+ dentry = NULL;
17481+ /*
17482+ * do not lock a->dir->i_mutex here
17483+ * because of d_revalidate() may cause a deadlock.
17484+ */
17485+ sb = a->dir->i_sb;
17486+ AuDebugOn(!sb);
17487+ sbinfo = au_sbi(sb);
17488+ AuDebugOn(!sbinfo);
17489+ si_write_lock(sb, AuLock_NOPLMW);
17490+
17491+ if (au_opt_test(sbinfo->si_mntflags, DIRREN))
17492+ switch (a->mask & FS_EVENTS_POSS_ON_CHILD) {
17493+ case FS_MOVED_FROM:
17494+ case FS_MOVED_TO:
17495+ AuWarn1("DIRREN with UDBA may not work correctly "
17496+ "for the direct rename(2)\n");
17497+ }
17498+
17499+ ii_read_lock_parent(a->dir);
17500+ bfound = -1;
17501+ bbot = au_ibbot(a->dir);
17502+ for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
17503+ if (au_h_iptr(a->dir, bindex) == a->h_dir) {
17504+ bfound = bindex;
17505+ break;
17506+ }
17507+ ii_read_unlock(a->dir);
17508+ if (unlikely(bfound < 0))
17509+ goto out;
17510+
17511+ xino = !!au_opt_test(au_mntflags(sb), XINO);
17512+ h_ino = 0;
17513+ if (a->h_child_inode)
17514+ h_ino = a->h_child_inode->i_ino;
17515+
17516+ if (a->h_child_nlen
17517+ && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
17518+ || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
17519+ dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
17520+ a->dir);
17521+ try_iput = 0;
17522+ if (dentry && d_really_is_positive(dentry))
17523+ inode = d_inode(dentry);
17524+ if (xino && !inode && h_ino
17525+ && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
17526+ || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
17527+ || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
17528+ inode = lookup_wlock_by_ino(sb, bfound, h_ino);
17529+ try_iput = 1;
17530+ }
17531+
17532+ args.flags = a->flags[AuHn_CHILD];
17533+ args.dentry = dentry;
17534+ args.inode = inode;
17535+ args.h_inode = a->h_child_inode;
17536+ args.dir = a->dir;
17537+ args.h_dir = a->h_dir;
17538+ args.h_name = a->h_child_name;
17539+ args.h_nlen = a->h_child_nlen;
17540+ err = hn_job(&args);
17541+ if (dentry) {
17542+ if (au_di(dentry))
17543+ di_write_unlock(dentry);
17544+ dput(dentry);
17545+ }
17546+ if (inode && try_iput) {
17547+ ii_write_unlock(inode);
17548+ iput(inode);
17549+ }
17550+
17551+ ii_write_lock_parent(a->dir);
17552+ args.flags = a->flags[AuHn_PARENT];
17553+ args.dentry = NULL;
17554+ args.inode = a->dir;
17555+ args.h_inode = a->h_dir;
17556+ args.dir = NULL;
17557+ args.h_dir = NULL;
17558+ args.h_name = NULL;
17559+ args.h_nlen = 0;
17560+ err = hn_job(&args);
17561+ ii_write_unlock(a->dir);
17562+
17563+out:
17564+ iput(a->h_child_inode);
17565+ iput(a->h_dir);
17566+ iput(a->dir);
17567+ si_write_unlock(sb);
17568+ au_nwt_done(&sbinfo->si_nowait);
17569+ au_kfree_rcu(a);
17570+}
17571+
17572+/* ---------------------------------------------------------------------- */
17573+
17574+int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
17575+ const struct qstr *h_child_qstr, struct inode *h_child_inode)
17576+{
17577+ int err, len;
17578+ unsigned int flags[AuHnLast], f;
17579+ unsigned char isdir, isroot, wh;
17580+ struct inode *dir;
17581+ struct au_hnotify_args *args;
17582+ char *p, *h_child_name;
17583+
17584+ err = 0;
17585+ AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
17586+ dir = igrab(hnotify->hn_aufs_inode);
17587+ if (!dir)
17588+ goto out;
17589+
17590+ isroot = (dir->i_ino == AUFS_ROOT_INO);
17591+ wh = 0;
17592+ h_child_name = (void *)h_child_qstr->name;
17593+ len = h_child_qstr->len;
17594+ if (h_child_name) {
17595+ if (len > AUFS_WH_PFX_LEN
17596+ && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
17597+ h_child_name += AUFS_WH_PFX_LEN;
17598+ len -= AUFS_WH_PFX_LEN;
17599+ wh = 1;
17600+ }
17601+ }
17602+
17603+ isdir = 0;
17604+ if (h_child_inode)
17605+ isdir = !!S_ISDIR(h_child_inode->i_mode);
17606+ flags[AuHn_PARENT] = AuHnJob_ISDIR;
17607+ flags[AuHn_CHILD] = 0;
17608+ if (isdir)
17609+ flags[AuHn_CHILD] = AuHnJob_ISDIR;
17610+ au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
17611+ au_fset_hnjob(flags[AuHn_CHILD], GEN);
17612+ switch (mask & ALL_FSNOTIFY_DIRENT_EVENTS) {
17613+ case FS_MOVED_FROM:
17614+ case FS_MOVED_TO:
17615+ au_fset_hnjob(flags[AuHn_CHILD], XINO0);
17616+ au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17617+ fallthrough;
17618+ case FS_CREATE:
17619+ AuDebugOn(!h_child_name);
17620+ break;
17621+
17622+ case FS_DELETE:
17623+ /*
17624+ * aufs never be able to get this child inode.
17625+ * revalidation should be in d_revalidate()
17626+ * by checking i_nlink, i_generation or d_unhashed().
17627+ */
17628+ AuDebugOn(!h_child_name);
17629+ au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
17630+ au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17631+ break;
17632+
17633+ default:
17634+ AuDebugOn(1);
17635+ }
17636+
17637+ if (wh)
17638+ h_child_inode = NULL;
17639+
17640+ err = -ENOMEM;
17641+ /* iput() and kfree() will be called in au_hnotify() */
17642+ args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
17643+ if (unlikely(!args)) {
17644+ AuErr1("no memory\n");
17645+ iput(dir);
17646+ goto out;
17647+ }
17648+ args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
17649+ args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
17650+ args->mask = mask;
17651+ args->dir = dir;
17652+ args->h_dir = igrab(h_dir);
17653+ if (h_child_inode)
17654+ h_child_inode = igrab(h_child_inode); /* can be NULL */
17655+ args->h_child_inode = h_child_inode;
17656+ args->h_child_nlen = len;
17657+ if (len) {
17658+ p = (void *)args;
17659+ p += sizeof(*args);
17660+ memcpy(p, h_child_name, len);
17661+ p[len] = 0;
17662+ }
17663+
17664+ /* NFS fires the event for silly-renamed one from kworker */
17665+ f = 0;
17666+ if (!dir->i_nlink
17667+ || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
17668+ f = AuWkq_NEST;
17669+ err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
17670+ if (unlikely(err)) {
17671+ pr_err("wkq %d\n", err);
17672+ iput(args->h_child_inode);
17673+ iput(args->h_dir);
17674+ iput(args->dir);
17675+ au_kfree_rcu(args);
17676+ }
17677+
17678+out:
17679+ return err;
17680+}
17681+
17682+/* ---------------------------------------------------------------------- */
17683+
17684+int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
17685+{
17686+ int err;
17687+
17688+ AuDebugOn(!(udba & AuOptMask_UDBA));
17689+
17690+ err = 0;
17691+ if (au_hnotify_op.reset_br)
17692+ err = au_hnotify_op.reset_br(udba, br, perm);
17693+
17694+ return err;
17695+}
17696+
17697+int au_hnotify_init_br(struct au_branch *br, int perm)
17698+{
17699+ int err;
17700+
17701+ err = 0;
17702+ if (au_hnotify_op.init_br)
17703+ err = au_hnotify_op.init_br(br, perm);
17704+
17705+ return err;
17706+}
17707+
17708+void au_hnotify_fin_br(struct au_branch *br)
17709+{
17710+ if (au_hnotify_op.fin_br)
17711+ au_hnotify_op.fin_br(br);
17712+}
17713+
17714+static void au_hn_destroy_cache(void)
17715+{
17716+ kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
17717+ au_cache[AuCache_HNOTIFY] = NULL;
17718+}
17719+
17720+int __init au_hnotify_init(void)
17721+{
17722+ int err;
17723+
17724+ err = -ENOMEM;
17725+ au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
17726+ if (au_cache[AuCache_HNOTIFY]) {
17727+ err = 0;
17728+ if (au_hnotify_op.init)
17729+ err = au_hnotify_op.init();
17730+ if (unlikely(err))
17731+ au_hn_destroy_cache();
17732+ }
17733+ AuTraceErr(err);
17734+ return err;
17735+}
17736+
17737+void au_hnotify_fin(void)
17738+{
17739+ if (au_hnotify_op.fin)
17740+ au_hnotify_op.fin();
17741+
17742+ /* cf. au_cache_fin() */
17743+ if (au_cache[AuCache_HNOTIFY])
17744+ au_hn_destroy_cache();
17745+}
17746diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
17747--- /usr/share/empty/fs/aufs/iinfo.c 1970-01-01 01:00:00.000000000 +0100
17748+++ linux/fs/aufs/iinfo.c 2021-12-03 15:38:59.936647310 +0100
17749@@ -0,0 +1,286 @@
17750+// SPDX-License-Identifier: GPL-2.0
17751+/*
17752+ * Copyright (C) 2005-2021 Junjiro R. Okajima
17753+ *
17754+ * This program, aufs is free software; you can redistribute it and/or modify
17755+ * it under the terms of the GNU General Public License as published by
17756+ * the Free Software Foundation; either version 2 of the License, or
17757+ * (at your option) any later version.
17758+ *
17759+ * This program is distributed in the hope that it will be useful,
17760+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17761+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17762+ * GNU General Public License for more details.
17763+ *
17764+ * You should have received a copy of the GNU General Public License
17765+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
17766+ */
17767+
17768+/*
17769+ * inode private data
17770+ */
17771+
17772+#include "aufs.h"
17773+
17774+struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
17775+{
17776+ struct inode *h_inode;
17777+ struct au_hinode *hinode;
17778+
17779+ IiMustAnyLock(inode);
17780+
17781+ hinode = au_hinode(au_ii(inode), bindex);
17782+ h_inode = hinode->hi_inode;
17783+ AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
17784+ return h_inode;
17785+}
17786+
17787+/* todo: hard/soft set? */
17788+void au_hiput(struct au_hinode *hinode)
17789+{
17790+ au_hn_free(hinode);
17791+ dput(hinode->hi_whdentry);
17792+ iput(hinode->hi_inode);
17793+}
17794+
17795+unsigned int au_hi_flags(struct inode *inode, int isdir)
17796+{
17797+ unsigned int flags;
17798+ const unsigned int mnt_flags = au_mntflags(inode->i_sb);
17799+
17800+ flags = 0;
17801+ if (au_opt_test(mnt_flags, XINO))
17802+ au_fset_hi(flags, XINO);
17803+ if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
17804+ au_fset_hi(flags, HNOTIFY);
17805+ return flags;
17806+}
17807+
17808+void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
17809+ struct inode *h_inode, unsigned int flags)
17810+{
17811+ struct au_hinode *hinode;
17812+ struct inode *hi;
17813+ struct au_iinfo *iinfo = au_ii(inode);
17814+
17815+ IiMustWriteLock(inode);
17816+
17817+ hinode = au_hinode(iinfo, bindex);
17818+ hi = hinode->hi_inode;
17819+ AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
17820+
17821+ if (hi)
17822+ au_hiput(hinode);
17823+ hinode->hi_inode = h_inode;
17824+ if (h_inode) {
17825+ int err;
17826+ struct super_block *sb = inode->i_sb;
17827+ struct au_branch *br;
17828+
17829+ AuDebugOn(inode->i_mode
17830+ && (h_inode->i_mode & S_IFMT)
17831+ != (inode->i_mode & S_IFMT));
17832+ if (bindex == iinfo->ii_btop)
17833+ au_cpup_igen(inode, h_inode);
17834+ br = au_sbr(sb, bindex);
17835+ hinode->hi_id = br->br_id;
17836+ if (au_ftest_hi(flags, XINO)) {
17837+ err = au_xino_write(sb, bindex, h_inode->i_ino,
17838+ inode->i_ino);
17839+ if (unlikely(err))
17840+ AuIOErr1("failed au_xino_write() %d\n", err);
17841+ }
17842+
17843+ if (au_ftest_hi(flags, HNOTIFY)
17844+ && au_br_hnotifyable(br->br_perm)) {
17845+ err = au_hn_alloc(hinode, inode);
17846+ if (unlikely(err))
17847+ AuIOErr1("au_hn_alloc() %d\n", err);
17848+ }
17849+ }
17850+}
17851+
17852+void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
17853+ struct dentry *h_wh)
17854+{
17855+ struct au_hinode *hinode;
17856+
17857+ IiMustWriteLock(inode);
17858+
17859+ hinode = au_hinode(au_ii(inode), bindex);
17860+ AuDebugOn(hinode->hi_whdentry);
17861+ hinode->hi_whdentry = h_wh;
17862+}
17863+
17864+void au_update_iigen(struct inode *inode, int half)
17865+{
17866+ struct au_iinfo *iinfo;
17867+ struct au_iigen *iigen;
17868+ unsigned int sigen;
17869+
17870+ sigen = au_sigen(inode->i_sb);
17871+ iinfo = au_ii(inode);
17872+ iigen = &iinfo->ii_generation;
17873+ spin_lock(&iigen->ig_spin);
17874+ iigen->ig_generation = sigen;
17875+ if (half)
17876+ au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
17877+ else
17878+ au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
17879+ spin_unlock(&iigen->ig_spin);
17880+}
17881+
17882+/* it may be called at remount time, too */
17883+void au_update_ibrange(struct inode *inode, int do_put_zero)
17884+{
17885+ struct au_iinfo *iinfo;
17886+ aufs_bindex_t bindex, bbot;
17887+
17888+ AuDebugOn(au_is_bad_inode(inode));
17889+ IiMustWriteLock(inode);
17890+
17891+ iinfo = au_ii(inode);
17892+ if (do_put_zero && iinfo->ii_btop >= 0) {
17893+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
17894+ bindex++) {
17895+ struct inode *h_i;
17896+
17897+ h_i = au_hinode(iinfo, bindex)->hi_inode;
17898+ if (h_i
17899+ && !h_i->i_nlink
17900+ && !(h_i->i_state & I_LINKABLE))
17901+ au_set_h_iptr(inode, bindex, NULL, 0);
17902+ }
17903+ }
17904+
17905+ iinfo->ii_btop = -1;
17906+ iinfo->ii_bbot = -1;
17907+ bbot = au_sbbot(inode->i_sb);
17908+ for (bindex = 0; bindex <= bbot; bindex++)
17909+ if (au_hinode(iinfo, bindex)->hi_inode) {
17910+ iinfo->ii_btop = bindex;
17911+ break;
17912+ }
17913+ if (iinfo->ii_btop >= 0)
17914+ for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
17915+ if (au_hinode(iinfo, bindex)->hi_inode) {
17916+ iinfo->ii_bbot = bindex;
17917+ break;
17918+ }
17919+ AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
17920+}
17921+
17922+/* ---------------------------------------------------------------------- */
17923+
17924+void au_icntnr_init_once(void *_c)
17925+{
17926+ struct au_icntnr *c = _c;
17927+ struct au_iinfo *iinfo = &c->iinfo;
17928+
17929+ spin_lock_init(&iinfo->ii_generation.ig_spin);
17930+ au_rw_init(&iinfo->ii_rwsem);
17931+ inode_init_once(&c->vfs_inode);
17932+}
17933+
17934+void au_hinode_init(struct au_hinode *hinode)
17935+{
17936+ hinode->hi_inode = NULL;
17937+ hinode->hi_id = -1;
17938+ au_hn_init(hinode);
17939+ hinode->hi_whdentry = NULL;
17940+}
17941+
17942+int au_iinfo_init(struct inode *inode)
17943+{
17944+ struct au_iinfo *iinfo;
17945+ struct super_block *sb;
17946+ struct au_hinode *hi;
17947+ int nbr, i;
17948+
17949+ sb = inode->i_sb;
17950+ iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
17951+ nbr = au_sbbot(sb) + 1;
17952+ if (unlikely(nbr <= 0))
17953+ nbr = 1;
17954+ hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
17955+ if (hi) {
17956+ au_lcnt_inc(&au_sbi(sb)->si_ninodes);
17957+
17958+ iinfo->ii_hinode = hi;
17959+ for (i = 0; i < nbr; i++, hi++)
17960+ au_hinode_init(hi);
17961+
17962+ iinfo->ii_generation.ig_generation = au_sigen(sb);
17963+ iinfo->ii_btop = -1;
17964+ iinfo->ii_bbot = -1;
17965+ iinfo->ii_vdir = NULL;
17966+ return 0;
17967+ }
17968+ return -ENOMEM;
17969+}
17970+
17971+int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
17972+{
17973+ int err, i;
17974+ struct au_hinode *hip;
17975+
17976+ AuRwMustWriteLock(&iinfo->ii_rwsem);
17977+
17978+ err = -ENOMEM;
17979+ hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
17980+ may_shrink);
17981+ if (hip) {
17982+ iinfo->ii_hinode = hip;
17983+ i = iinfo->ii_bbot + 1;
17984+ hip += i;
17985+ for (; i < nbr; i++, hip++)
17986+ au_hinode_init(hip);
17987+ err = 0;
17988+ }
17989+
17990+ return err;
17991+}
17992+
17993+void au_iinfo_fin(struct inode *inode)
17994+{
17995+ struct au_iinfo *iinfo;
17996+ struct au_hinode *hi;
17997+ struct super_block *sb;
17998+ aufs_bindex_t bindex, bbot;
17999+ const unsigned char unlinked = !inode->i_nlink;
18000+
18001+ AuDebugOn(au_is_bad_inode(inode));
18002+
18003+ sb = inode->i_sb;
18004+ au_lcnt_dec(&au_sbi(sb)->si_ninodes);
18005+ if (si_pid_test(sb))
18006+ au_xino_delete_inode(inode, unlinked);
18007+ else {
18008+ /*
18009+ * it is safe to hide the dependency between sbinfo and
18010+ * sb->s_umount.
18011+ */
18012+ lockdep_off();
18013+ si_noflush_read_lock(sb);
18014+ au_xino_delete_inode(inode, unlinked);
18015+ si_read_unlock(sb);
18016+ lockdep_on();
18017+ }
18018+
18019+ iinfo = au_ii(inode);
18020+ if (iinfo->ii_vdir)
18021+ au_vdir_free(iinfo->ii_vdir);
18022+
18023+ bindex = iinfo->ii_btop;
18024+ if (bindex >= 0) {
18025+ hi = au_hinode(iinfo, bindex);
18026+ bbot = iinfo->ii_bbot;
18027+ while (bindex++ <= bbot) {
18028+ if (hi->hi_inode)
18029+ au_hiput(hi);
18030+ hi++;
18031+ }
18032+ }
18033+ au_kfree_rcu(iinfo->ii_hinode);
18034+ AuRwDestroy(&iinfo->ii_rwsem);
18035+}
18036diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
18037--- /usr/share/empty/fs/aufs/inode.c 1970-01-01 01:00:00.000000000 +0100
18038+++ linux/fs/aufs/inode.c 2021-12-03 15:40:58.236647297 +0100
18039@@ -0,0 +1,531 @@
18040+// SPDX-License-Identifier: GPL-2.0
18041+/*
18042+ * Copyright (C) 2005-2021 Junjiro R. Okajima
18043+ *
18044+ * This program, aufs is free software; you can redistribute it and/or modify
18045+ * it under the terms of the GNU General Public License as published by
18046+ * the Free Software Foundation; either version 2 of the License, or
18047+ * (at your option) any later version.
18048+ *
18049+ * This program is distributed in the hope that it will be useful,
18050+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18051+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18052+ * GNU General Public License for more details.
18053+ *
18054+ * You should have received a copy of the GNU General Public License
18055+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
18056+ */
18057+
18058+/*
18059+ * inode functions
18060+ */
18061+
18062+#include <linux/iversion.h>
18063+#include "aufs.h"
18064+
18065+struct inode *au_igrab(struct inode *inode)
18066+{
18067+ if (inode) {
18068+ AuDebugOn(!atomic_read(&inode->i_count));
18069+ ihold(inode);
18070+ }
18071+ return inode;
18072+}
18073+
18074+static void au_refresh_hinode_attr(struct inode *inode, int do_version)
18075+{
18076+ au_cpup_attr_all(inode, /*force*/0);
18077+ au_update_iigen(inode, /*half*/1);
18078+ if (do_version)
18079+ inode_inc_iversion(inode);
18080+}
18081+
18082+static int au_ii_refresh(struct inode *inode, int *update)
18083+{
18084+ int err, e, nbr;
18085+ umode_t type;
18086+ aufs_bindex_t bindex, new_bindex;
18087+ struct super_block *sb;
18088+ struct au_iinfo *iinfo;
18089+ struct au_hinode *p, *q, tmp;
18090+
18091+ AuDebugOn(au_is_bad_inode(inode));
18092+ IiMustWriteLock(inode);
18093+
18094+ *update = 0;
18095+ sb = inode->i_sb;
18096+ nbr = au_sbbot(sb) + 1;
18097+ type = inode->i_mode & S_IFMT;
18098+ iinfo = au_ii(inode);
18099+ err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
18100+ if (unlikely(err))
18101+ goto out;
18102+
18103+ AuDebugOn(iinfo->ii_btop < 0);
18104+ p = au_hinode(iinfo, iinfo->ii_btop);
18105+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
18106+ bindex++, p++) {
18107+ if (!p->hi_inode)
18108+ continue;
18109+
18110+ AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
18111+ new_bindex = au_br_index(sb, p->hi_id);
18112+ if (new_bindex == bindex)
18113+ continue;
18114+
18115+ if (new_bindex < 0) {
18116+ *update = 1;
18117+ au_hiput(p);
18118+ p->hi_inode = NULL;
18119+ continue;
18120+ }
18121+
18122+ if (new_bindex < iinfo->ii_btop)
18123+ iinfo->ii_btop = new_bindex;
18124+ if (iinfo->ii_bbot < new_bindex)
18125+ iinfo->ii_bbot = new_bindex;
18126+ /* swap two lower inode, and loop again */
18127+ q = au_hinode(iinfo, new_bindex);
18128+ tmp = *q;
18129+ *q = *p;
18130+ *p = tmp;
18131+ if (tmp.hi_inode) {
18132+ bindex--;
18133+ p--;
18134+ }
18135+ }
18136+ au_update_ibrange(inode, /*do_put_zero*/0);
18137+ au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
18138+ e = au_dy_irefresh(inode);
18139+ if (unlikely(e && !err))
18140+ err = e;
18141+
18142+out:
18143+ AuTraceErr(err);
18144+ return err;
18145+}
18146+
18147+void au_refresh_iop(struct inode *inode, int force_getattr)
18148+{
18149+ int type;
18150+ struct au_sbinfo *sbi = au_sbi(inode->i_sb);
18151+ const struct inode_operations *iop
18152+ = force_getattr ? aufs_iop : sbi->si_iop_array;
18153+
18154+ if (inode->i_op == iop)
18155+ return;
18156+
18157+ switch (inode->i_mode & S_IFMT) {
18158+ case S_IFDIR:
18159+ type = AuIop_DIR;
18160+ break;
18161+ case S_IFLNK:
18162+ type = AuIop_SYMLINK;
18163+ break;
18164+ default:
18165+ type = AuIop_OTHER;
18166+ break;
18167+ }
18168+
18169+ inode->i_op = iop + type;
18170+ /* unnecessary smp_wmb() */
18171+}
18172+
18173+int au_refresh_hinode_self(struct inode *inode)
18174+{
18175+ int err, update;
18176+
18177+ err = au_ii_refresh(inode, &update);
18178+ if (!err)
18179+ au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
18180+
18181+ AuTraceErr(err);
18182+ return err;
18183+}
18184+
18185+int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
18186+{
18187+ int err, e, update;
18188+ unsigned int flags;
18189+ umode_t mode;
18190+ aufs_bindex_t bindex, bbot;
18191+ unsigned char isdir;
18192+ struct au_hinode *p;
18193+ struct au_iinfo *iinfo;
18194+
18195+ err = au_ii_refresh(inode, &update);
18196+ if (unlikely(err))
18197+ goto out;
18198+
18199+ update = 0;
18200+ iinfo = au_ii(inode);
18201+ p = au_hinode(iinfo, iinfo->ii_btop);
18202+ mode = (inode->i_mode & S_IFMT);
18203+ isdir = S_ISDIR(mode);
18204+ flags = au_hi_flags(inode, isdir);
18205+ bbot = au_dbbot(dentry);
18206+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
18207+ struct inode *h_i, *h_inode;
18208+ struct dentry *h_d;
18209+
18210+ h_d = au_h_dptr(dentry, bindex);
18211+ if (!h_d || d_is_negative(h_d))
18212+ continue;
18213+
18214+ h_inode = d_inode(h_d);
18215+ AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
18216+ if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
18217+ h_i = au_h_iptr(inode, bindex);
18218+ if (h_i) {
18219+ if (h_i == h_inode)
18220+ continue;
18221+ err = -EIO;
18222+ break;
18223+ }
18224+ }
18225+ if (bindex < iinfo->ii_btop)
18226+ iinfo->ii_btop = bindex;
18227+ if (iinfo->ii_bbot < bindex)
18228+ iinfo->ii_bbot = bindex;
18229+ au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
18230+ update = 1;
18231+ }
18232+ au_update_ibrange(inode, /*do_put_zero*/0);
18233+ e = au_dy_irefresh(inode);
18234+ if (unlikely(e && !err))
18235+ err = e;
18236+ if (!err)
18237+ au_refresh_hinode_attr(inode, update && isdir);
18238+
18239+out:
18240+ AuTraceErr(err);
18241+ return err;
18242+}
18243+
18244+static int set_inode(struct inode *inode, struct dentry *dentry)
18245+{
18246+ int err;
18247+ unsigned int flags;
18248+ umode_t mode;
18249+ aufs_bindex_t bindex, btop, btail;
18250+ unsigned char isdir;
18251+ struct dentry *h_dentry;
18252+ struct inode *h_inode;
18253+ struct au_iinfo *iinfo;
18254+ const struct inode_operations *iop;
18255+
18256+ IiMustWriteLock(inode);
18257+
18258+ err = 0;
18259+ isdir = 0;
18260+ iop = au_sbi(inode->i_sb)->si_iop_array;
18261+ btop = au_dbtop(dentry);
18262+ h_dentry = au_h_dptr(dentry, btop);
18263+ h_inode = d_inode(h_dentry);
18264+ mode = h_inode->i_mode;
18265+ switch (mode & S_IFMT) {
18266+ case S_IFREG:
18267+ btail = au_dbtail(dentry);
18268+ inode->i_op = iop + AuIop_OTHER;
18269+ inode->i_fop = &aufs_file_fop;
18270+ err = au_dy_iaop(inode, btop, h_inode);
18271+ if (unlikely(err))
18272+ goto out;
18273+ break;
18274+ case S_IFDIR:
18275+ isdir = 1;
18276+ btail = au_dbtaildir(dentry);
18277+ inode->i_op = iop + AuIop_DIR;
18278+ inode->i_fop = &aufs_dir_fop;
18279+ break;
18280+ case S_IFLNK:
18281+ btail = au_dbtail(dentry);
18282+ inode->i_op = iop + AuIop_SYMLINK;
18283+ break;
18284+ case S_IFBLK:
18285+ case S_IFCHR:
18286+ case S_IFIFO:
18287+ case S_IFSOCK:
18288+ btail = au_dbtail(dentry);
18289+ inode->i_op = iop + AuIop_OTHER;
18290+ init_special_inode(inode, mode, h_inode->i_rdev);
18291+ break;
18292+ default:
18293+ AuIOErr("Unknown file type 0%o\n", mode);
18294+ err = -EIO;
18295+ goto out;
18296+ }
18297+
18298+ /* do not set hnotify for whiteouted dirs (SHWH mode) */
18299+ flags = au_hi_flags(inode, isdir);
18300+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
18301+ && au_ftest_hi(flags, HNOTIFY)
18302+ && dentry->d_name.len > AUFS_WH_PFX_LEN
18303+ && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
18304+ au_fclr_hi(flags, HNOTIFY);
18305+ iinfo = au_ii(inode);
18306+ iinfo->ii_btop = btop;
18307+ iinfo->ii_bbot = btail;
18308+ for (bindex = btop; bindex <= btail; bindex++) {
18309+ h_dentry = au_h_dptr(dentry, bindex);
18310+ if (h_dentry)
18311+ au_set_h_iptr(inode, bindex,
18312+ au_igrab(d_inode(h_dentry)), flags);
18313+ }
18314+ au_cpup_attr_all(inode, /*force*/1);
18315+ /*
18316+ * to force calling aufs_get_acl() every time,
18317+ * do not call cache_no_acl() for aufs inode.
18318+ */
18319+
18320+out:
18321+ return err;
18322+}
18323+
18324+/*
18325+ * successful returns with iinfo write_locked
18326+ * minus: errno
18327+ * zero: success, matched
18328+ * plus: no error, but unmatched
18329+ */
18330+static int reval_inode(struct inode *inode, struct dentry *dentry)
18331+{
18332+ int err;
18333+ unsigned int gen, igflags;
18334+ aufs_bindex_t bindex, bbot;
18335+ struct inode *h_inode, *h_dinode;
18336+ struct dentry *h_dentry;
18337+
18338+ /*
18339+ * before this function, if aufs got any iinfo lock, it must be only
18340+ * one, the parent dir.
18341+ * it can happen by UDBA and the obsoleted inode number.
18342+ */
18343+ err = -EIO;
18344+ if (unlikely(inode->i_ino == parent_ino(dentry)))
18345+ goto out;
18346+
18347+ err = 1;
18348+ ii_write_lock_new_child(inode);
18349+ h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
18350+ h_dinode = d_inode(h_dentry);
18351+ bbot = au_ibbot(inode);
18352+ for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
18353+ h_inode = au_h_iptr(inode, bindex);
18354+ if (!h_inode || h_inode != h_dinode)
18355+ continue;
18356+
18357+ err = 0;
18358+ gen = au_iigen(inode, &igflags);
18359+ if (gen == au_digen(dentry)
18360+ && !au_ig_ftest(igflags, HALF_REFRESHED))
18361+ break;
18362+
18363+ /* fully refresh inode using dentry */
18364+ err = au_refresh_hinode(inode, dentry);
18365+ if (!err)
18366+ au_update_iigen(inode, /*half*/0);
18367+ break;
18368+ }
18369+
18370+ if (unlikely(err))
18371+ ii_write_unlock(inode);
18372+out:
18373+ return err;
18374+}
18375+
18376+int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18377+ unsigned int d_type, ino_t *ino)
18378+{
18379+ int err, idx;
18380+ const int isnondir = d_type != DT_DIR;
18381+
18382+ /* prevent hardlinked inode number from race condition */
18383+ if (isnondir) {
18384+ err = au_xinondir_enter(sb, bindex, h_ino, &idx);
18385+ if (unlikely(err))
18386+ goto out;
18387+ }
18388+
18389+ err = au_xino_read(sb, bindex, h_ino, ino);
18390+ if (unlikely(err))
18391+ goto out_xinondir;
18392+
18393+ if (!*ino) {
18394+ err = -EIO;
18395+ *ino = au_xino_new_ino(sb);
18396+ if (unlikely(!*ino))
18397+ goto out_xinondir;
18398+ err = au_xino_write(sb, bindex, h_ino, *ino);
18399+ if (unlikely(err))
18400+ goto out_xinondir;
18401+ }
18402+
18403+out_xinondir:
18404+ if (isnondir && idx >= 0)
18405+ au_xinondir_leave(sb, bindex, h_ino, idx);
18406+out:
18407+ return err;
18408+}
18409+
18410+/* successful returns with iinfo write_locked */
18411+/* todo: return with unlocked? */
18412+struct inode *au_new_inode(struct dentry *dentry, int must_new)
18413+{
18414+ struct inode *inode, *h_inode;
18415+ struct dentry *h_dentry;
18416+ struct super_block *sb;
18417+ ino_t h_ino, ino;
18418+ int err, idx, hlinked;
18419+ aufs_bindex_t btop;
18420+
18421+ sb = dentry->d_sb;
18422+ btop = au_dbtop(dentry);
18423+ h_dentry = au_h_dptr(dentry, btop);
18424+ h_inode = d_inode(h_dentry);
18425+ h_ino = h_inode->i_ino;
18426+ hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
18427+
18428+new_ino:
18429+ /*
18430+ * stop 'race'-ing between hardlinks under different
18431+ * parents.
18432+ */
18433+ if (hlinked) {
18434+ err = au_xinondir_enter(sb, btop, h_ino, &idx);
18435+ inode = ERR_PTR(err);
18436+ if (unlikely(err))
18437+ goto out;
18438+ }
18439+
18440+ err = au_xino_read(sb, btop, h_ino, &ino);
18441+ inode = ERR_PTR(err);
18442+ if (unlikely(err))
18443+ goto out_xinondir;
18444+
18445+ if (!ino) {
18446+ ino = au_xino_new_ino(sb);
18447+ if (unlikely(!ino)) {
18448+ inode = ERR_PTR(-EIO);
18449+ goto out_xinondir;
18450+ }
18451+ }
18452+
18453+ AuDbg("i%lu\n", (unsigned long)ino);
18454+ inode = au_iget_locked(sb, ino);
18455+ err = PTR_ERR(inode);
18456+ if (IS_ERR(inode))
18457+ goto out_xinondir;
18458+
18459+ AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
18460+ if (inode->i_state & I_NEW) {
18461+ ii_write_lock_new_child(inode);
18462+ err = set_inode(inode, dentry);
18463+ if (!err) {
18464+ unlock_new_inode(inode);
18465+ goto out_xinondir; /* success */
18466+ }
18467+
18468+ /*
18469+ * iget_failed() calls iput(), but we need to call
18470+ * ii_write_unlock() after iget_failed(). so dirty hack for
18471+ * i_count.
18472+ */
18473+ atomic_inc(&inode->i_count);
18474+ iget_failed(inode);
18475+ ii_write_unlock(inode);
18476+ au_xino_write(sb, btop, h_ino, /*ino*/0);
18477+ /* ignore this error */
18478+ goto out_iput;
18479+ } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
18480+ /*
18481+ * horrible race condition between lookup, readdir and copyup
18482+ * (or something).
18483+ */
18484+ if (hlinked && idx >= 0)
18485+ au_xinondir_leave(sb, btop, h_ino, idx);
18486+ err = reval_inode(inode, dentry);
18487+ if (unlikely(err < 0)) {
18488+ hlinked = 0;
18489+ goto out_iput;
18490+ }
18491+ if (!err)
18492+ goto out; /* success */
18493+ else if (hlinked && idx >= 0) {
18494+ err = au_xinondir_enter(sb, btop, h_ino, &idx);
18495+ if (unlikely(err)) {
18496+ iput(inode);
18497+ inode = ERR_PTR(err);
18498+ goto out;
18499+ }
18500+ }
18501+ }
18502+
18503+ if (unlikely(au_test_fs_unique_ino(h_inode)))
18504+ AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
18505+ " b%d, %s, %pd, hi%lu, i%lu.\n",
18506+ btop, au_sbtype(h_dentry->d_sb), dentry,
18507+ (unsigned long)h_ino, (unsigned long)ino);
18508+ ino = 0;
18509+ err = au_xino_write(sb, btop, h_ino, /*ino*/0);
18510+ if (!err) {
18511+ iput(inode);
18512+ if (hlinked && idx >= 0)
18513+ au_xinondir_leave(sb, btop, h_ino, idx);
18514+ goto new_ino;
18515+ }
18516+
18517+out_iput:
18518+ iput(inode);
18519+ inode = ERR_PTR(err);
18520+out_xinondir:
18521+ if (hlinked && idx >= 0)
18522+ au_xinondir_leave(sb, btop, h_ino, idx);
18523+out:
18524+ return inode;
18525+}
18526+
18527+/* ---------------------------------------------------------------------- */
18528+
18529+int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18530+ struct inode *inode)
18531+{
18532+ int err;
18533+ struct inode *hi;
18534+
18535+ err = au_br_rdonly(au_sbr(sb, bindex));
18536+
18537+ /* pseudo-link after flushed may happen out of bounds */
18538+ if (!err
18539+ && inode
18540+ && au_ibtop(inode) <= bindex
18541+ && bindex <= au_ibbot(inode)) {
18542+ /*
18543+ * permission check is unnecessary since vfsub routine
18544+ * will be called later
18545+ */
18546+ hi = au_h_iptr(inode, bindex);
18547+ if (hi)
18548+ err = IS_IMMUTABLE(hi) ? -EROFS : 0;
18549+ }
18550+
18551+ return err;
18552+}
18553+
18554+int au_test_h_perm(struct user_namespace *h_userns, struct inode *h_inode,
18555+ int mask)
18556+{
18557+ if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
18558+ return 0;
18559+ return inode_permission(h_userns, h_inode, mask);
18560+}
18561+
18562+int au_test_h_perm_sio(struct user_namespace *h_userns, struct inode *h_inode,
18563+ int mask)
18564+{
18565+ if (au_test_nfs(h_inode->i_sb)
18566+ && (mask & MAY_WRITE)
18567+ && S_ISDIR(h_inode->i_mode))
18568+ mask |= MAY_READ; /* force permission check */
18569+ return au_test_h_perm(h_userns, h_inode, mask);
18570+}
18571diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
18572--- /usr/share/empty/fs/aufs/inode.h 1970-01-01 01:00:00.000000000 +0100
18573+++ linux/fs/aufs/inode.h 2021-12-03 15:40:58.236647297 +0100
18574@@ -0,0 +1,705 @@
18575+/* SPDX-License-Identifier: GPL-2.0 */
18576+/*
18577+ * Copyright (C) 2005-2021 Junjiro R. Okajima
18578+ *
18579+ * This program, aufs is free software; you can redistribute it and/or modify
18580+ * it under the terms of the GNU General Public License as published by
18581+ * the Free Software Foundation; either version 2 of the License, or
18582+ * (at your option) any later version.
18583+ *
18584+ * This program is distributed in the hope that it will be useful,
18585+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18586+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18587+ * GNU General Public License for more details.
18588+ *
18589+ * You should have received a copy of the GNU General Public License
18590+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
18591+ */
18592+
18593+/*
18594+ * inode operations
18595+ */
18596+
18597+#ifndef __AUFS_INODE_H__
18598+#define __AUFS_INODE_H__
18599+
18600+#ifdef __KERNEL__
18601+
18602+#include <linux/fsnotify.h>
18603+#include "rwsem.h"
18604+
18605+struct vfsmount;
18606+
18607+struct au_hnotify {
18608+#ifdef CONFIG_AUFS_HNOTIFY
18609+#ifdef CONFIG_AUFS_HFSNOTIFY
18610+ /* never use fsnotify_add_vfsmount_mark() */
18611+ struct fsnotify_mark hn_mark;
18612+#endif
18613+ struct inode *hn_aufs_inode; /* no get/put */
18614+ struct rcu_head rcu;
18615+#endif
18616+} ____cacheline_aligned_in_smp;
18617+
18618+struct au_hinode {
18619+ struct inode *hi_inode;
18620+ aufs_bindex_t hi_id;
18621+#ifdef CONFIG_AUFS_HNOTIFY
18622+ struct au_hnotify *hi_notify;
18623+#endif
18624+
18625+ /* reference to the copied-up whiteout with get/put */
18626+ struct dentry *hi_whdentry;
18627+};
18628+
18629+/* ig_flags */
18630+#define AuIG_HALF_REFRESHED 1
18631+#define au_ig_ftest(flags, name) ((flags) & AuIG_##name)
18632+#define au_ig_fset(flags, name) \
18633+ do { (flags) |= AuIG_##name; } while (0)
18634+#define au_ig_fclr(flags, name) \
18635+ do { (flags) &= ~AuIG_##name; } while (0)
18636+
18637+struct au_iigen {
18638+ spinlock_t ig_spin;
18639+ __u32 ig_generation, ig_flags;
18640+};
18641+
18642+struct au_vdir;
18643+struct au_iinfo {
18644+ struct au_iigen ii_generation;
18645+ struct super_block *ii_hsb1; /* no get/put */
18646+
18647+ struct au_rwsem ii_rwsem;
18648+ aufs_bindex_t ii_btop, ii_bbot;
18649+ __u32 ii_higen;
18650+ struct au_hinode *ii_hinode;
18651+ struct au_vdir *ii_vdir;
18652+};
18653+
18654+struct au_icntnr {
18655+ struct au_iinfo iinfo;
18656+ struct inode vfs_inode;
18657+ struct hlist_bl_node plink;
18658+ struct rcu_head rcu;
18659+} ____cacheline_aligned_in_smp;
18660+
18661+/* au_pin flags */
18662+#define AuPin_DI_LOCKED 1
18663+#define AuPin_MNT_WRITE (1 << 1)
18664+#define au_ftest_pin(flags, name) ((flags) & AuPin_##name)
18665+#define au_fset_pin(flags, name) \
18666+ do { (flags) |= AuPin_##name; } while (0)
18667+#define au_fclr_pin(flags, name) \
18668+ do { (flags) &= ~AuPin_##name; } while (0)
18669+
18670+struct au_pin {
18671+ /* input */
18672+ struct dentry *dentry;
18673+ unsigned int udba;
18674+ unsigned char lsc_di, lsc_hi, flags;
18675+ aufs_bindex_t bindex;
18676+
18677+ /* output */
18678+ struct dentry *parent;
18679+ struct au_hinode *hdir;
18680+ struct vfsmount *h_mnt;
18681+
18682+ /* temporary unlock/relock for copyup */
18683+ struct dentry *h_dentry, *h_parent;
18684+ struct au_branch *br;
18685+ struct task_struct *task;
18686+};
18687+
18688+void au_pin_hdir_unlock(struct au_pin *p);
18689+int au_pin_hdir_lock(struct au_pin *p);
18690+int au_pin_hdir_relock(struct au_pin *p);
18691+void au_pin_hdir_acquire_nest(struct au_pin *p);
18692+void au_pin_hdir_release(struct au_pin *p);
18693+
18694+/* ---------------------------------------------------------------------- */
18695+
18696+static inline struct au_iinfo *au_ii(struct inode *inode)
18697+{
18698+ BUG_ON(is_bad_inode(inode));
18699+ return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
18700+}
18701+
18702+/* ---------------------------------------------------------------------- */
18703+
18704+/* inode.c */
18705+struct inode *au_igrab(struct inode *inode);
18706+void au_refresh_iop(struct inode *inode, int force_getattr);
18707+int au_refresh_hinode_self(struct inode *inode);
18708+int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
18709+int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18710+ unsigned int d_type, ino_t *ino);
18711+struct inode *au_new_inode(struct dentry *dentry, int must_new);
18712+int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18713+ struct inode *inode);
18714+int au_test_h_perm(struct user_namespace *h_userns, struct inode *h_inode,
18715+ int mask);
18716+int au_test_h_perm_sio(struct user_namespace *h_userns, struct inode *h_inode,
18717+ int mask);
18718+
18719+static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
18720+ ino_t h_ino, unsigned int d_type, ino_t *ino)
18721+{
18722+#ifdef CONFIG_AUFS_SHWH
18723+ return au_ino(sb, bindex, h_ino, d_type, ino);
18724+#else
18725+ return 0;
18726+#endif
18727+}
18728+
18729+/* i_op.c */
18730+enum {
18731+ AuIop_SYMLINK,
18732+ AuIop_DIR,
18733+ AuIop_OTHER,
18734+ AuIop_Last
18735+};
18736+extern struct inode_operations aufs_iop[AuIop_Last], /* not const */
18737+ aufs_iop_nogetattr[AuIop_Last];
18738+
18739+/* au_wr_dir flags */
18740+#define AuWrDir_ADD_ENTRY 1
18741+#define AuWrDir_ISDIR (1 << 1)
18742+#define AuWrDir_TMPFILE (1 << 2)
18743+#define au_ftest_wrdir(flags, name) ((flags) & AuWrDir_##name)
18744+#define au_fset_wrdir(flags, name) \
18745+ do { (flags) |= AuWrDir_##name; } while (0)
18746+#define au_fclr_wrdir(flags, name) \
18747+ do { (flags) &= ~AuWrDir_##name; } while (0)
18748+
18749+struct au_wr_dir_args {
18750+ aufs_bindex_t force_btgt;
18751+ unsigned char flags;
18752+};
18753+int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
18754+ struct au_wr_dir_args *args);
18755+
18756+struct dentry *au_pinned_h_parent(struct au_pin *pin);
18757+void au_pin_init(struct au_pin *pin, struct dentry *dentry,
18758+ aufs_bindex_t bindex, int lsc_di, int lsc_hi,
18759+ unsigned int udba, unsigned char flags);
18760+int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
18761+ unsigned int udba, unsigned char flags) __must_check;
18762+int au_do_pin(struct au_pin *pin) __must_check;
18763+void au_unpin(struct au_pin *pin);
18764+int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
18765+
18766+#define AuIcpup_DID_CPUP 1
18767+#define au_ftest_icpup(flags, name) ((flags) & AuIcpup_##name)
18768+#define au_fset_icpup(flags, name) \
18769+ do { (flags) |= AuIcpup_##name; } while (0)
18770+#define au_fclr_icpup(flags, name) \
18771+ do { (flags) &= ~AuIcpup_##name; } while (0)
18772+
18773+struct au_icpup_args {
18774+ unsigned char flags;
18775+ unsigned char pin_flags;
18776+ aufs_bindex_t btgt;
18777+ unsigned int udba;
18778+ struct au_pin pin;
18779+ struct path h_path;
18780+ struct inode *h_inode;
18781+};
18782+
18783+int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
18784+ struct au_icpup_args *a);
18785+
18786+int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
18787+ struct path *h_path, int locked);
18788+
18789+/* i_op_add.c */
18790+int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
18791+ struct dentry *h_parent, int isdir);
18792+int aufs_mknod(struct user_namespace *userns, struct inode *dir,
18793+ struct dentry *dentry, umode_t mode, dev_t dev);
18794+int aufs_symlink(struct user_namespace *userns, struct inode *dir,
18795+ struct dentry *dentry, const char *symname);
18796+int aufs_create(struct user_namespace *userns, struct inode *dir,
18797+ struct dentry *dentry, umode_t mode, bool want_excl);
18798+struct vfsub_aopen_args;
18799+int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
18800+ struct vfsub_aopen_args *args);
18801+int aufs_tmpfile(struct user_namespace *userns, struct inode *dir,
18802+ struct dentry *dentry, umode_t mode);
18803+int aufs_link(struct dentry *src_dentry, struct inode *dir,
18804+ struct dentry *dentry);
18805+int aufs_mkdir(struct user_namespace *userns, struct inode *dir,
18806+ struct dentry *dentry, umode_t mode);
18807+
18808+/* i_op_del.c */
18809+int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
18810+int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
18811+ struct dentry *h_parent, int isdir);
18812+int aufs_unlink(struct inode *dir, struct dentry *dentry);
18813+int aufs_rmdir(struct inode *dir, struct dentry *dentry);
18814+
18815+/* i_op_ren.c */
18816+int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
18817+int aufs_rename(struct user_namespace *userns,
18818+ struct inode *_src_dir, struct dentry *_src_dentry,
18819+ struct inode *_dst_dir, struct dentry *_dst_dentry,
18820+ unsigned int _flags);
18821+
18822+/* iinfo.c */
18823+struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
18824+void au_hiput(struct au_hinode *hinode);
18825+void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
18826+ struct dentry *h_wh);
18827+unsigned int au_hi_flags(struct inode *inode, int isdir);
18828+
18829+/* hinode flags */
18830+#define AuHi_XINO 1
18831+#define AuHi_HNOTIFY (1 << 1)
18832+#define au_ftest_hi(flags, name) ((flags) & AuHi_##name)
18833+#define au_fset_hi(flags, name) \
18834+ do { (flags) |= AuHi_##name; } while (0)
18835+#define au_fclr_hi(flags, name) \
18836+ do { (flags) &= ~AuHi_##name; } while (0)
18837+
18838+#ifndef CONFIG_AUFS_HNOTIFY
18839+#undef AuHi_HNOTIFY
18840+#define AuHi_HNOTIFY 0
18841+#endif
18842+
18843+void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
18844+ struct inode *h_inode, unsigned int flags);
18845+
18846+void au_update_iigen(struct inode *inode, int half);
18847+void au_update_ibrange(struct inode *inode, int do_put_zero);
18848+
18849+void au_icntnr_init_once(void *_c);
18850+void au_hinode_init(struct au_hinode *hinode);
18851+int au_iinfo_init(struct inode *inode);
18852+void au_iinfo_fin(struct inode *inode);
18853+int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
18854+
18855+#ifdef CONFIG_PROC_FS
18856+/* plink.c */
18857+int au_plink_maint(struct super_block *sb, int flags);
18858+struct au_sbinfo;
18859+void au_plink_maint_leave(struct au_sbinfo *sbinfo);
18860+int au_plink_maint_enter(struct super_block *sb);
18861+#ifdef CONFIG_AUFS_DEBUG
18862+void au_plink_list(struct super_block *sb);
18863+#else
18864+AuStubVoid(au_plink_list, struct super_block *sb)
18865+#endif
18866+int au_plink_test(struct inode *inode);
18867+struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
18868+void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
18869+ struct dentry *h_dentry);
18870+void au_plink_put(struct super_block *sb, int verbose);
18871+void au_plink_clean(struct super_block *sb, int verbose);
18872+void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
18873+#else
18874+AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
18875+AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
18876+AuStubInt0(au_plink_maint_enter, struct super_block *sb);
18877+AuStubVoid(au_plink_list, struct super_block *sb);
18878+AuStubInt0(au_plink_test, struct inode *inode);
18879+AuStub(struct dentry *, au_plink_lkup, return NULL,
18880+ struct inode *inode, aufs_bindex_t bindex);
18881+AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
18882+ struct dentry *h_dentry);
18883+AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
18884+AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
18885+AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
18886+#endif /* CONFIG_PROC_FS */
18887+
18888+#ifdef CONFIG_AUFS_XATTR
18889+/* xattr.c */
18890+int au_cpup_xattr(struct path *h_dst, struct path *h_src, int ignore_flags,
18891+ unsigned int verbose);
18892+ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
18893+void au_xattr_init(struct super_block *sb);
18894+#else
18895+AuStubInt0(au_cpup_xattr, struct path *h_dst, struct path *h_src,
18896+ int ignore_flags, unsigned int verbose);
18897+AuStubVoid(au_xattr_init, struct super_block *sb);
18898+#endif
18899+
18900+#ifdef CONFIG_FS_POSIX_ACL
18901+struct posix_acl *aufs_get_acl(struct inode *inode, int type, bool rcu);
18902+int aufs_set_acl(struct user_namespace *userns, struct inode *inode,
18903+ struct posix_acl *acl, int type);
18904+#endif
18905+
18906+#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
18907+enum {
18908+ AU_XATTR_SET,
18909+ AU_ACL_SET
18910+};
18911+
18912+struct au_sxattr {
18913+ int type;
18914+ union {
18915+ struct {
18916+ const char *name;
18917+ const void *value;
18918+ size_t size;
18919+ int flags;
18920+ } set;
18921+ struct {
18922+ struct posix_acl *acl;
18923+ int type;
18924+ } acl_set;
18925+ } u;
18926+};
18927+ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
18928+ struct au_sxattr *arg);
18929+#endif
18930+
18931+/* ---------------------------------------------------------------------- */
18932+
18933+/* lock subclass for iinfo */
18934+enum {
18935+ AuLsc_II_CHILD, /* child first */
18936+ AuLsc_II_CHILD2, /* rename(2), link(2), and cpup at hnotify */
18937+ AuLsc_II_CHILD3, /* copyup dirs */
18938+ AuLsc_II_PARENT, /* see AuLsc_I_PARENT in vfsub.h */
18939+ AuLsc_II_PARENT2,
18940+ AuLsc_II_PARENT3, /* copyup dirs */
18941+ AuLsc_II_NEW_CHILD
18942+};
18943+
18944+/*
18945+ * ii_read_lock_child, ii_write_lock_child,
18946+ * ii_read_lock_child2, ii_write_lock_child2,
18947+ * ii_read_lock_child3, ii_write_lock_child3,
18948+ * ii_read_lock_parent, ii_write_lock_parent,
18949+ * ii_read_lock_parent2, ii_write_lock_parent2,
18950+ * ii_read_lock_parent3, ii_write_lock_parent3,
18951+ * ii_read_lock_new_child, ii_write_lock_new_child,
18952+ */
18953+#define AuReadLockFunc(name, lsc) \
18954+static inline void ii_read_lock_##name(struct inode *i) \
18955+{ \
18956+ au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
18957+}
18958+
18959+#define AuWriteLockFunc(name, lsc) \
18960+static inline void ii_write_lock_##name(struct inode *i) \
18961+{ \
18962+ au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
18963+}
18964+
18965+#define AuRWLockFuncs(name, lsc) \
18966+ AuReadLockFunc(name, lsc) \
18967+ AuWriteLockFunc(name, lsc)
18968+
18969+AuRWLockFuncs(child, CHILD);
18970+AuRWLockFuncs(child2, CHILD2);
18971+AuRWLockFuncs(child3, CHILD3);
18972+AuRWLockFuncs(parent, PARENT);
18973+AuRWLockFuncs(parent2, PARENT2);
18974+AuRWLockFuncs(parent3, PARENT3);
18975+AuRWLockFuncs(new_child, NEW_CHILD);
18976+
18977+#undef AuReadLockFunc
18978+#undef AuWriteLockFunc
18979+#undef AuRWLockFuncs
18980+
18981+#define ii_read_unlock(i) au_rw_read_unlock(&au_ii(i)->ii_rwsem)
18982+#define ii_write_unlock(i) au_rw_write_unlock(&au_ii(i)->ii_rwsem)
18983+#define ii_downgrade_lock(i) au_rw_dgrade_lock(&au_ii(i)->ii_rwsem)
18984+
18985+#define IiMustNoWaiters(i) AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
18986+#define IiMustAnyLock(i) AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
18987+#define IiMustWriteLock(i) AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
18988+
18989+/* ---------------------------------------------------------------------- */
18990+
18991+static inline void au_icntnr_init(struct au_icntnr *c)
18992+{
18993+#ifdef CONFIG_AUFS_DEBUG
18994+ c->vfs_inode.i_mode = 0;
18995+#endif
18996+}
18997+
18998+static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
18999+{
19000+ unsigned int gen;
19001+ struct au_iinfo *iinfo;
19002+ struct au_iigen *iigen;
19003+
19004+ iinfo = au_ii(inode);
19005+ iigen = &iinfo->ii_generation;
19006+ spin_lock(&iigen->ig_spin);
19007+ if (igflags)
19008+ *igflags = iigen->ig_flags;
19009+ gen = iigen->ig_generation;
19010+ spin_unlock(&iigen->ig_spin);
19011+
19012+ return gen;
19013+}
19014+
19015+/* tiny test for inode number */
19016+/* tmpfs generation is too rough */
19017+static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
19018+{
19019+ struct au_iinfo *iinfo;
19020+
19021+ iinfo = au_ii(inode);
19022+ AuRwMustAnyLock(&iinfo->ii_rwsem);
19023+ return !(iinfo->ii_hsb1 == h_inode->i_sb
19024+ && iinfo->ii_higen == h_inode->i_generation);
19025+}
19026+
19027+static inline void au_iigen_dec(struct inode *inode)
19028+{
19029+ struct au_iinfo *iinfo;
19030+ struct au_iigen *iigen;
19031+
19032+ iinfo = au_ii(inode);
19033+ iigen = &iinfo->ii_generation;
19034+ spin_lock(&iigen->ig_spin);
19035+ iigen->ig_generation--;
19036+ spin_unlock(&iigen->ig_spin);
19037+}
19038+
19039+static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
19040+{
19041+ int err;
19042+
19043+ err = 0;
19044+ if (unlikely(inode && au_iigen(inode, NULL) != sigen))
19045+ err = -EIO;
19046+
19047+ return err;
19048+}
19049+
19050+/* ---------------------------------------------------------------------- */
19051+
19052+static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
19053+ aufs_bindex_t bindex)
19054+{
19055+ return iinfo->ii_hinode + bindex;
19056+}
19057+
19058+static inline int au_is_bad_inode(struct inode *inode)
19059+{
19060+ return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
19061+}
19062+
19063+static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
19064+ aufs_bindex_t bindex)
19065+{
19066+ IiMustAnyLock(inode);
19067+ return au_hinode(au_ii(inode), bindex)->hi_id;
19068+}
19069+
19070+static inline aufs_bindex_t au_ibtop(struct inode *inode)
19071+{
19072+ IiMustAnyLock(inode);
19073+ return au_ii(inode)->ii_btop;
19074+}
19075+
19076+static inline aufs_bindex_t au_ibbot(struct inode *inode)
19077+{
19078+ IiMustAnyLock(inode);
19079+ return au_ii(inode)->ii_bbot;
19080+}
19081+
19082+static inline struct au_vdir *au_ivdir(struct inode *inode)
19083+{
19084+ IiMustAnyLock(inode);
19085+ return au_ii(inode)->ii_vdir;
19086+}
19087+
19088+static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
19089+{
19090+ IiMustAnyLock(inode);
19091+ return au_hinode(au_ii(inode), bindex)->hi_whdentry;
19092+}
19093+
19094+static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
19095+{
19096+ IiMustWriteLock(inode);
19097+ au_ii(inode)->ii_btop = bindex;
19098+}
19099+
19100+static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
19101+{
19102+ IiMustWriteLock(inode);
19103+ au_ii(inode)->ii_bbot = bindex;
19104+}
19105+
19106+static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
19107+{
19108+ IiMustWriteLock(inode);
19109+ au_ii(inode)->ii_vdir = vdir;
19110+}
19111+
19112+static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
19113+{
19114+ IiMustAnyLock(inode);
19115+ return au_hinode(au_ii(inode), bindex);
19116+}
19117+
19118+/* ---------------------------------------------------------------------- */
19119+
19120+static inline struct dentry *au_pinned_parent(struct au_pin *pin)
19121+{
19122+ if (pin)
19123+ return pin->parent;
19124+ return NULL;
19125+}
19126+
19127+static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
19128+{
19129+ if (pin && pin->hdir)
19130+ return pin->hdir->hi_inode;
19131+ return NULL;
19132+}
19133+
19134+static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
19135+{
19136+ if (pin)
19137+ return pin->hdir;
19138+ return NULL;
19139+}
19140+
19141+static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
19142+{
19143+ if (pin)
19144+ pin->dentry = dentry;
19145+}
19146+
19147+static inline void au_pin_set_parent_lflag(struct au_pin *pin,
19148+ unsigned char lflag)
19149+{
19150+ if (pin) {
19151+ if (lflag)
19152+ au_fset_pin(pin->flags, DI_LOCKED);
19153+ else
19154+ au_fclr_pin(pin->flags, DI_LOCKED);
19155+ }
19156+}
19157+
19158+#if 0 /* reserved */
19159+static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
19160+{
19161+ if (pin) {
19162+ dput(pin->parent);
19163+ pin->parent = dget(parent);
19164+ }
19165+}
19166+#endif
19167+
19168+/* ---------------------------------------------------------------------- */
19169+
19170+struct au_branch;
19171+#ifdef CONFIG_AUFS_HNOTIFY
19172+struct au_hnotify_op {
19173+ void (*ctl)(struct au_hinode *hinode, int do_set);
19174+ int (*alloc)(struct au_hinode *hinode);
19175+
19176+ /*
19177+ * if it returns true, the caller should free hinode->hi_notify,
19178+ * otherwise ->free() frees it.
19179+ */
19180+ int (*free)(struct au_hinode *hinode,
19181+ struct au_hnotify *hn) __must_check;
19182+
19183+ void (*fin)(void);
19184+ int (*init)(void);
19185+
19186+ int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
19187+ void (*fin_br)(struct au_branch *br);
19188+ int (*init_br)(struct au_branch *br, int perm);
19189+};
19190+
19191+/* hnotify.c */
19192+int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
19193+void au_hn_free(struct au_hinode *hinode);
19194+void au_hn_ctl(struct au_hinode *hinode, int do_set);
19195+void au_hn_reset(struct inode *inode, unsigned int flags);
19196+int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
19197+ const struct qstr *h_child_qstr, struct inode *h_child_inode);
19198+int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
19199+int au_hnotify_init_br(struct au_branch *br, int perm);
19200+void au_hnotify_fin_br(struct au_branch *br);
19201+int __init au_hnotify_init(void);
19202+void au_hnotify_fin(void);
19203+
19204+/* hfsnotify.c */
19205+extern const struct au_hnotify_op au_hnotify_op;
19206+
19207+static inline
19208+void au_hn_init(struct au_hinode *hinode)
19209+{
19210+ hinode->hi_notify = NULL;
19211+}
19212+
19213+static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
19214+{
19215+ return hinode->hi_notify;
19216+}
19217+
19218+#else
19219+AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
19220+ struct au_hinode *hinode __maybe_unused,
19221+ struct inode *inode __maybe_unused)
19222+AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
19223+AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
19224+AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
19225+ int do_set __maybe_unused)
19226+AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
19227+ unsigned int flags __maybe_unused)
19228+AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
19229+ struct au_branch *br __maybe_unused,
19230+ int perm __maybe_unused)
19231+AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
19232+ int perm __maybe_unused)
19233+AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
19234+AuStubInt0(__init au_hnotify_init, void)
19235+AuStubVoid(au_hnotify_fin, void)
19236+AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
19237+#endif /* CONFIG_AUFS_HNOTIFY */
19238+
19239+static inline void au_hn_suspend(struct au_hinode *hdir)
19240+{
19241+ au_hn_ctl(hdir, /*do_set*/0);
19242+}
19243+
19244+static inline void au_hn_resume(struct au_hinode *hdir)
19245+{
19246+ au_hn_ctl(hdir, /*do_set*/1);
19247+}
19248+
19249+static inline void au_hn_inode_lock(struct au_hinode *hdir)
19250+{
19251+ inode_lock(hdir->hi_inode);
19252+ au_hn_suspend(hdir);
19253+}
19254+
19255+static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
19256+ unsigned int sc __maybe_unused)
19257+{
19258+ inode_lock_nested(hdir->hi_inode, sc);
19259+ au_hn_suspend(hdir);
19260+}
19261+
19262+#if 0 /* unused */
19263+#include "vfsub.h"
19264+static inline void au_hn_inode_lock_shared_nested(struct au_hinode *hdir,
19265+ unsigned int sc)
19266+{
19267+ inode_lock_shared_nested(hdir->hi_inode, sc);
19268+ au_hn_suspend(hdir);
19269+}
19270+#endif
19271+
19272+static inline void au_hn_inode_unlock(struct au_hinode *hdir)
19273+{
19274+ au_hn_resume(hdir);
19275+ inode_unlock(hdir->hi_inode);
19276+}
19277+
19278+#endif /* __KERNEL__ */
19279+#endif /* __AUFS_INODE_H__ */
19280diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
19281--- /usr/share/empty/fs/aufs/ioctl.c 1970-01-01 01:00:00.000000000 +0100
19282+++ linux/fs/aufs/ioctl.c 2021-12-03 15:38:59.936647310 +0100
19283@@ -0,0 +1,220 @@
19284+// SPDX-License-Identifier: GPL-2.0
19285+/*
19286+ * Copyright (C) 2005-2021 Junjiro R. Okajima
19287+ *
19288+ * This program, aufs is free software; you can redistribute it and/or modify
19289+ * it under the terms of the GNU General Public License as published by
19290+ * the Free Software Foundation; either version 2 of the License, or
19291+ * (at your option) any later version.
19292+ *
19293+ * This program is distributed in the hope that it will be useful,
19294+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19295+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19296+ * GNU General Public License for more details.
19297+ *
19298+ * You should have received a copy of the GNU General Public License
19299+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
19300+ */
19301+
19302+/*
19303+ * ioctl
19304+ * plink-management and readdir in userspace.
19305+ * assist the pathconf(3) wrapper library.
19306+ * move-down
19307+ * File-based Hierarchical Storage Management.
19308+ */
19309+
19310+#include <linux/compat.h>
19311+#include <linux/file.h>
19312+#include "aufs.h"
19313+
19314+static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
19315+{
19316+ int err, fd;
19317+ aufs_bindex_t wbi, bindex, bbot;
19318+ struct file *h_file;
19319+ struct super_block *sb;
19320+ struct dentry *root;
19321+ struct au_branch *br;
19322+ struct aufs_wbr_fd wbrfd = {
19323+ .oflags = au_dir_roflags,
19324+ .brid = -1
19325+ };
19326+ const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
19327+ | O_NOATIME | O_CLOEXEC;
19328+
19329+ AuDebugOn(wbrfd.oflags & ~valid);
19330+
19331+ if (arg) {
19332+ err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
19333+ if (unlikely(err)) {
19334+ err = -EFAULT;
19335+ goto out;
19336+ }
19337+
19338+ err = -EINVAL;
19339+ AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
19340+ wbrfd.oflags |= au_dir_roflags;
19341+ AuDbg("0%o\n", wbrfd.oflags);
19342+ if (unlikely(wbrfd.oflags & ~valid))
19343+ goto out;
19344+ }
19345+
19346+ fd = get_unused_fd_flags(0);
19347+ err = fd;
19348+ if (unlikely(fd < 0))
19349+ goto out;
19350+
19351+ h_file = ERR_PTR(-EINVAL);
19352+ wbi = 0;
19353+ br = NULL;
19354+ sb = path->dentry->d_sb;
19355+ root = sb->s_root;
19356+ aufs_read_lock(root, AuLock_IR);
19357+ bbot = au_sbbot(sb);
19358+ if (wbrfd.brid >= 0) {
19359+ wbi = au_br_index(sb, wbrfd.brid);
19360+ if (unlikely(wbi < 0 || wbi > bbot))
19361+ goto out_unlock;
19362+ }
19363+
19364+ h_file = ERR_PTR(-ENOENT);
19365+ br = au_sbr(sb, wbi);
19366+ if (!au_br_writable(br->br_perm)) {
19367+ if (arg)
19368+ goto out_unlock;
19369+
19370+ bindex = wbi + 1;
19371+ wbi = -1;
19372+ for (; bindex <= bbot; bindex++) {
19373+ br = au_sbr(sb, bindex);
19374+ if (au_br_writable(br->br_perm)) {
19375+ wbi = bindex;
19376+ br = au_sbr(sb, wbi);
19377+ break;
19378+ }
19379+ }
19380+ }
19381+ AuDbg("wbi %d\n", wbi);
19382+ if (wbi >= 0)
19383+ h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
19384+ /*force_wr*/0);
19385+
19386+out_unlock:
19387+ aufs_read_unlock(root, AuLock_IR);
19388+ err = PTR_ERR(h_file);
19389+ if (IS_ERR(h_file))
19390+ goto out_fd;
19391+
19392+ au_lcnt_dec(&br->br_nfiles); /* cf. au_h_open() */
19393+ fd_install(fd, h_file);
19394+ err = fd;
19395+ goto out; /* success */
19396+
19397+out_fd:
19398+ put_unused_fd(fd);
19399+out:
19400+ AuTraceErr(err);
19401+ return err;
19402+}
19403+
19404+/* ---------------------------------------------------------------------- */
19405+
19406+long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
19407+{
19408+ long err;
19409+ struct dentry *dentry;
19410+
19411+ switch (cmd) {
19412+ case AUFS_CTL_RDU:
19413+ case AUFS_CTL_RDU_INO:
19414+ err = au_rdu_ioctl(file, cmd, arg);
19415+ break;
19416+
19417+ case AUFS_CTL_WBR_FD:
19418+ err = au_wbr_fd(&file->f_path, (void __user *)arg);
19419+ break;
19420+
19421+ case AUFS_CTL_IBUSY:
19422+ err = au_ibusy_ioctl(file, arg);
19423+ break;
19424+
19425+ case AUFS_CTL_BRINFO:
19426+ err = au_brinfo_ioctl(file, arg);
19427+ break;
19428+
19429+ case AUFS_CTL_FHSM_FD:
19430+ dentry = file->f_path.dentry;
19431+ if (IS_ROOT(dentry))
19432+ err = au_fhsm_fd(dentry->d_sb, arg);
19433+ else
19434+ err = -ENOTTY;
19435+ break;
19436+
19437+ default:
19438+ /* do not call the lower */
19439+ AuDbg("0x%x\n", cmd);
19440+ err = -ENOTTY;
19441+ }
19442+
19443+ AuTraceErr(err);
19444+ return err;
19445+}
19446+
19447+long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
19448+{
19449+ long err;
19450+
19451+ switch (cmd) {
19452+ case AUFS_CTL_MVDOWN:
19453+ err = au_mvdown(file->f_path.dentry, (void __user *)arg);
19454+ break;
19455+
19456+ case AUFS_CTL_WBR_FD:
19457+ err = au_wbr_fd(&file->f_path, (void __user *)arg);
19458+ break;
19459+
19460+ default:
19461+ /* do not call the lower */
19462+ AuDbg("0x%x\n", cmd);
19463+ err = -ENOTTY;
19464+ }
19465+
19466+ AuTraceErr(err);
19467+ return err;
19468+}
19469+
19470+#ifdef CONFIG_COMPAT
19471+long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
19472+ unsigned long arg)
19473+{
19474+ long err;
19475+
19476+ switch (cmd) {
19477+ case AUFS_CTL_RDU:
19478+ case AUFS_CTL_RDU_INO:
19479+ err = au_rdu_compat_ioctl(file, cmd, arg);
19480+ break;
19481+
19482+ case AUFS_CTL_IBUSY:
19483+ err = au_ibusy_compat_ioctl(file, arg);
19484+ break;
19485+
19486+ case AUFS_CTL_BRINFO:
19487+ err = au_brinfo_compat_ioctl(file, arg);
19488+ break;
19489+
19490+ default:
19491+ err = aufs_ioctl_dir(file, cmd, arg);
19492+ }
19493+
19494+ AuTraceErr(err);
19495+ return err;
19496+}
19497+
19498+long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
19499+ unsigned long arg)
19500+{
19501+ return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
19502+}
19503+#endif
19504diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
19505--- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
19506+++ linux/fs/aufs/i_op_add.c 2021-12-03 15:40:58.236647297 +0100
19507@@ -0,0 +1,941 @@
19508+// SPDX-License-Identifier: GPL-2.0
19509+/*
19510+ * Copyright (C) 2005-2021 Junjiro R. Okajima
19511+ *
19512+ * This program, aufs is free software; you can redistribute it and/or modify
19513+ * it under the terms of the GNU General Public License as published by
19514+ * the Free Software Foundation; either version 2 of the License, or
19515+ * (at your option) any later version.
19516+ *
19517+ * This program is distributed in the hope that it will be useful,
19518+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19519+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19520+ * GNU General Public License for more details.
19521+ *
19522+ * You should have received a copy of the GNU General Public License
19523+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
19524+ */
19525+
19526+/*
19527+ * inode operations (add entry)
19528+ */
19529+
19530+#include <linux/iversion.h>
19531+#include "aufs.h"
19532+
19533+/*
19534+ * final procedure of adding a new entry, except link(2).
19535+ * remove whiteout, instantiate, copyup the parent dir's times and size
19536+ * and update version.
19537+ * if it failed, re-create the removed whiteout.
19538+ */
19539+static int epilog(struct inode *dir, aufs_bindex_t bindex,
19540+ struct dentry *wh_dentry, struct dentry *dentry)
19541+{
19542+ int err, rerr;
19543+ aufs_bindex_t bwh;
19544+ struct path h_path;
19545+ struct super_block *sb;
19546+ struct inode *inode, *h_dir;
19547+ struct dentry *wh;
19548+
19549+ bwh = -1;
19550+ sb = dir->i_sb;
19551+ if (wh_dentry) {
19552+ h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
19553+ IMustLock(h_dir);
19554+ AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
19555+ bwh = au_dbwh(dentry);
19556+ h_path.dentry = wh_dentry;
19557+ h_path.mnt = au_sbr_mnt(sb, bindex);
19558+ err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
19559+ dentry);
19560+ if (unlikely(err))
19561+ goto out;
19562+ }
19563+
19564+ inode = au_new_inode(dentry, /*must_new*/1);
19565+ if (!IS_ERR(inode)) {
19566+ d_instantiate(dentry, inode);
19567+ dir = d_inode(dentry->d_parent); /* dir inode is locked */
19568+ IMustLock(dir);
19569+ au_dir_ts(dir, bindex);
19570+ inode_inc_iversion(dir);
19571+ au_fhsm_wrote(sb, bindex, /*force*/0);
19572+ return 0; /* success */
19573+ }
19574+
19575+ err = PTR_ERR(inode);
19576+ if (!wh_dentry)
19577+ goto out;
19578+
19579+ /* revert */
19580+ /* dir inode is locked */
19581+ wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
19582+ rerr = PTR_ERR(wh);
19583+ if (IS_ERR(wh)) {
19584+ AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
19585+ dentry, err, rerr);
19586+ err = -EIO;
19587+ } else
19588+ dput(wh);
19589+
19590+out:
19591+ return err;
19592+}
19593+
19594+static int au_d_may_add(struct dentry *dentry)
19595+{
19596+ int err;
19597+
19598+ err = 0;
19599+ if (unlikely(d_unhashed(dentry)))
19600+ err = -ENOENT;
19601+ if (unlikely(d_really_is_positive(dentry)))
19602+ err = -EEXIST;
19603+ return err;
19604+}
19605+
19606+/*
19607+ * simple tests for the adding inode operations.
19608+ * following the checks in vfs, plus the parent-child relationship.
19609+ */
19610+int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
19611+ struct dentry *h_parent, int isdir)
19612+{
19613+ int err;
19614+ umode_t h_mode;
19615+ struct dentry *h_dentry;
19616+ struct inode *h_inode;
19617+
19618+ err = -ENAMETOOLONG;
19619+ if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19620+ goto out;
19621+
19622+ h_dentry = au_h_dptr(dentry, bindex);
19623+ if (d_really_is_negative(dentry)) {
19624+ err = -EEXIST;
19625+ if (unlikely(d_is_positive(h_dentry)))
19626+ goto out;
19627+ } else {
19628+ /* rename(2) case */
19629+ err = -EIO;
19630+ if (unlikely(d_is_negative(h_dentry)))
19631+ goto out;
19632+ h_inode = d_inode(h_dentry);
19633+ if (unlikely(!h_inode->i_nlink))
19634+ goto out;
19635+
19636+ h_mode = h_inode->i_mode;
19637+ if (!isdir) {
19638+ err = -EISDIR;
19639+ if (unlikely(S_ISDIR(h_mode)))
19640+ goto out;
19641+ } else if (unlikely(!S_ISDIR(h_mode))) {
19642+ err = -ENOTDIR;
19643+ goto out;
19644+ }
19645+ }
19646+
19647+ err = 0;
19648+ /* expected parent dir is locked */
19649+ if (unlikely(h_parent != h_dentry->d_parent))
19650+ err = -EIO;
19651+
19652+out:
19653+ AuTraceErr(err);
19654+ return err;
19655+}
19656+
19657+/*
19658+ * initial procedure of adding a new entry.
19659+ * prepare writable branch and the parent dir, lock it,
19660+ * and lookup whiteout for the new entry.
19661+ */
19662+static struct dentry*
19663+lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
19664+ struct dentry *src_dentry, struct au_pin *pin,
19665+ struct au_wr_dir_args *wr_dir_args)
19666+{
19667+ struct dentry *wh_dentry, *h_parent;
19668+ struct super_block *sb;
19669+ struct au_branch *br;
19670+ int err;
19671+ unsigned int udba;
19672+ aufs_bindex_t bcpup;
19673+
19674+ AuDbg("%pd\n", dentry);
19675+
19676+ err = au_wr_dir(dentry, src_dentry, wr_dir_args);
19677+ bcpup = err;
19678+ wh_dentry = ERR_PTR(err);
19679+ if (unlikely(err < 0))
19680+ goto out;
19681+
19682+ sb = dentry->d_sb;
19683+ udba = au_opt_udba(sb);
19684+ err = au_pin(pin, dentry, bcpup, udba,
19685+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
19686+ wh_dentry = ERR_PTR(err);
19687+ if (unlikely(err))
19688+ goto out;
19689+
19690+ h_parent = au_pinned_h_parent(pin);
19691+ if (udba != AuOpt_UDBA_NONE
19692+ && au_dbtop(dentry) == bcpup)
19693+ err = au_may_add(dentry, bcpup, h_parent,
19694+ au_ftest_wrdir(wr_dir_args->flags, ISDIR));
19695+ else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19696+ err = -ENAMETOOLONG;
19697+ wh_dentry = ERR_PTR(err);
19698+ if (unlikely(err))
19699+ goto out_unpin;
19700+
19701+ br = au_sbr(sb, bcpup);
19702+ if (dt) {
19703+ struct path tmp = {
19704+ .dentry = h_parent,
19705+ .mnt = au_br_mnt(br)
19706+ };
19707+ au_dtime_store(dt, au_pinned_parent(pin), &tmp);
19708+ }
19709+
19710+ wh_dentry = NULL;
19711+ if (bcpup != au_dbwh(dentry))
19712+ goto out; /* success */
19713+
19714+ /*
19715+ * ENAMETOOLONG here means that if we allowed create such name, then it
19716+ * would not be able to removed in the future. So we don't allow such
19717+ * name here and we don't handle ENAMETOOLONG differently here.
19718+ */
19719+ wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
19720+
19721+out_unpin:
19722+ if (IS_ERR(wh_dentry))
19723+ au_unpin(pin);
19724+out:
19725+ return wh_dentry;
19726+}
19727+
19728+/* ---------------------------------------------------------------------- */
19729+
19730+enum { Mknod, Symlink, Creat };
19731+struct simple_arg {
19732+ int type;
19733+ union {
19734+ struct {
19735+ umode_t mode;
19736+ bool want_excl;
19737+ bool try_aopen;
19738+ struct vfsub_aopen_args *aopen;
19739+ } c;
19740+ struct {
19741+ const char *symname;
19742+ } s;
19743+ struct {
19744+ umode_t mode;
19745+ dev_t dev;
19746+ } m;
19747+ } u;
19748+};
19749+
19750+static int add_simple(struct inode *dir, struct dentry *dentry,
19751+ struct simple_arg *arg)
19752+{
19753+ int err, rerr;
19754+ aufs_bindex_t btop;
19755+ unsigned char created;
19756+ const unsigned char try_aopen
19757+ = (arg->type == Creat && arg->u.c.try_aopen);
19758+ struct vfsub_aopen_args *aopen = arg->u.c.aopen;
19759+ struct dentry *wh_dentry, *parent;
19760+ struct inode *h_dir;
19761+ struct super_block *sb;
19762+ struct au_branch *br;
19763+ /* to reduce stack size */
19764+ struct {
19765+ struct au_dtime dt;
19766+ struct au_pin pin;
19767+ struct path h_path;
19768+ struct au_wr_dir_args wr_dir_args;
19769+ } *a;
19770+
19771+ AuDbg("%pd\n", dentry);
19772+ IMustLock(dir);
19773+
19774+ err = -ENOMEM;
19775+ a = kmalloc(sizeof(*a), GFP_NOFS);
19776+ if (unlikely(!a))
19777+ goto out;
19778+ a->wr_dir_args.force_btgt = -1;
19779+ a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
19780+
19781+ parent = dentry->d_parent; /* dir inode is locked */
19782+ if (!try_aopen) {
19783+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
19784+ if (unlikely(err))
19785+ goto out_free;
19786+ }
19787+ err = au_d_may_add(dentry);
19788+ if (unlikely(err))
19789+ goto out_unlock;
19790+ if (!try_aopen)
19791+ di_write_lock_parent(parent);
19792+ wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
19793+ &a->pin, &a->wr_dir_args);
19794+ err = PTR_ERR(wh_dentry);
19795+ if (IS_ERR(wh_dentry))
19796+ goto out_parent;
19797+
19798+ btop = au_dbtop(dentry);
19799+ sb = dentry->d_sb;
19800+ br = au_sbr(sb, btop);
19801+ a->h_path.dentry = au_h_dptr(dentry, btop);
19802+ a->h_path.mnt = au_br_mnt(br);
19803+ h_dir = au_pinned_h_dir(&a->pin);
19804+ switch (arg->type) {
19805+ case Creat:
19806+ if (!try_aopen || !h_dir->i_op->atomic_open) {
19807+ err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
19808+ arg->u.c.want_excl);
19809+ created = !err;
19810+ if (!err && try_aopen)
19811+ aopen->file->f_mode |= FMODE_CREATED;
19812+ } else {
19813+ aopen->br = br;
19814+ err = vfsub_atomic_open(h_dir, a->h_path.dentry, aopen);
19815+ AuDbg("err %d\n", err);
19816+ AuDbgFile(aopen->file);
19817+ created = err >= 0
19818+ && !!(aopen->file->f_mode & FMODE_CREATED);
19819+ }
19820+ break;
19821+ case Symlink:
19822+ err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
19823+ created = !err;
19824+ break;
19825+ case Mknod:
19826+ err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
19827+ arg->u.m.dev);
19828+ created = !err;
19829+ break;
19830+ default:
19831+ BUG();
19832+ }
19833+ if (unlikely(err < 0))
19834+ goto out_unpin;
19835+
19836+ err = epilog(dir, btop, wh_dentry, dentry);
19837+ if (!err)
19838+ goto out_unpin; /* success */
19839+
19840+ /* revert */
19841+ if (created /* && d_is_positive(a->h_path.dentry) */) {
19842+ /* no delegation since it is just created */
19843+ rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
19844+ /*force*/0);
19845+ if (rerr) {
19846+ AuIOErr("%pd revert failure(%d, %d)\n",
19847+ dentry, err, rerr);
19848+ err = -EIO;
19849+ }
19850+ au_dtime_revert(&a->dt);
19851+ }
19852+ if (try_aopen && h_dir->i_op->atomic_open
19853+ && (aopen->file->f_mode & FMODE_OPENED))
19854+ /* aopen->file is still opened */
19855+ au_lcnt_dec(&aopen->br->br_nfiles);
19856+
19857+out_unpin:
19858+ au_unpin(&a->pin);
19859+ dput(wh_dentry);
19860+out_parent:
19861+ if (!try_aopen)
19862+ di_write_unlock(parent);
19863+out_unlock:
19864+ if (unlikely(err)) {
19865+ au_update_dbtop(dentry);
19866+ d_drop(dentry);
19867+ }
19868+ if (!try_aopen)
19869+ aufs_read_unlock(dentry, AuLock_DW);
19870+out_free:
19871+ au_kfree_rcu(a);
19872+out:
19873+ return err;
19874+}
19875+
19876+int aufs_mknod(struct user_namespace *userns, struct inode *dir,
19877+ struct dentry *dentry, umode_t mode, dev_t dev)
19878+{
19879+ struct simple_arg arg = {
19880+ .type = Mknod,
19881+ .u.m = {
19882+ .mode = mode,
19883+ .dev = dev
19884+ }
19885+ };
19886+ return add_simple(dir, dentry, &arg);
19887+}
19888+
19889+int aufs_symlink(struct user_namespace *userns, struct inode *dir,
19890+ struct dentry *dentry, const char *symname)
19891+{
19892+ struct simple_arg arg = {
19893+ .type = Symlink,
19894+ .u.s.symname = symname
19895+ };
19896+ return add_simple(dir, dentry, &arg);
19897+}
19898+
19899+int aufs_create(struct user_namespace *userns, struct inode *dir,
19900+ struct dentry *dentry, umode_t mode, bool want_excl)
19901+{
19902+ struct simple_arg arg = {
19903+ .type = Creat,
19904+ .u.c = {
19905+ .mode = mode,
19906+ .want_excl = want_excl
19907+ }
19908+ };
19909+ return add_simple(dir, dentry, &arg);
19910+}
19911+
19912+int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
19913+ struct vfsub_aopen_args *aopen_args)
19914+{
19915+ struct simple_arg arg = {
19916+ .type = Creat,
19917+ .u.c = {
19918+ .mode = aopen_args->create_mode,
19919+ .want_excl = aopen_args->open_flag & O_EXCL,
19920+ .try_aopen = true,
19921+ .aopen = aopen_args
19922+ }
19923+ };
19924+ return add_simple(dir, dentry, &arg);
19925+}
19926+
19927+int aufs_tmpfile(struct user_namespace *userns, struct inode *dir,
19928+ struct dentry *dentry, umode_t mode)
19929+{
19930+ int err;
19931+ aufs_bindex_t bindex;
19932+ struct super_block *sb;
19933+ struct dentry *parent, *h_parent, *h_dentry;
19934+ struct inode *h_dir, *inode;
19935+ struct vfsmount *h_mnt;
19936+ struct user_namespace *h_userns;
19937+ struct au_wr_dir_args wr_dir_args = {
19938+ .force_btgt = -1,
19939+ .flags = AuWrDir_TMPFILE
19940+ };
19941+
19942+ /* copy-up may happen */
19943+ inode_lock(dir);
19944+
19945+ sb = dir->i_sb;
19946+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
19947+ if (unlikely(err))
19948+ goto out;
19949+
19950+ err = au_di_init(dentry);
19951+ if (unlikely(err))
19952+ goto out_si;
19953+
19954+ err = -EBUSY;
19955+ parent = d_find_any_alias(dir);
19956+ AuDebugOn(!parent);
19957+ di_write_lock_parent(parent);
19958+ if (unlikely(d_inode(parent) != dir))
19959+ goto out_parent;
19960+
19961+ err = au_digen_test(parent, au_sigen(sb));
19962+ if (unlikely(err))
19963+ goto out_parent;
19964+
19965+ bindex = au_dbtop(parent);
19966+ au_set_dbtop(dentry, bindex);
19967+ au_set_dbbot(dentry, bindex);
19968+ err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
19969+ bindex = err;
19970+ if (unlikely(err < 0))
19971+ goto out_parent;
19972+
19973+ err = -EOPNOTSUPP;
19974+ h_dir = au_h_iptr(dir, bindex);
19975+ if (unlikely(!h_dir->i_op->tmpfile))
19976+ goto out_parent;
19977+
19978+ h_mnt = au_sbr_mnt(sb, bindex);
19979+ err = vfsub_mnt_want_write(h_mnt);
19980+ if (unlikely(err))
19981+ goto out_parent;
19982+
19983+ h_userns = mnt_user_ns(h_mnt);
19984+ h_parent = au_h_dptr(parent, bindex);
19985+ h_dentry = vfs_tmpfile(h_userns, h_parent, mode, /*open_flag*/0);
19986+ if (IS_ERR(h_dentry)) {
19987+ err = PTR_ERR(h_dentry);
19988+ goto out_mnt;
19989+ }
19990+
19991+ au_set_dbtop(dentry, bindex);
19992+ au_set_dbbot(dentry, bindex);
19993+ au_set_h_dptr(dentry, bindex, dget(h_dentry));
19994+ inode = au_new_inode(dentry, /*must_new*/1);
19995+ if (IS_ERR(inode)) {
19996+ err = PTR_ERR(inode);
19997+ au_set_h_dptr(dentry, bindex, NULL);
19998+ au_set_dbtop(dentry, -1);
19999+ au_set_dbbot(dentry, -1);
20000+ } else {
20001+ if (!inode->i_nlink)
20002+ set_nlink(inode, 1);
20003+ d_tmpfile(dentry, inode);
20004+ au_di(dentry)->di_tmpfile = 1;
20005+
20006+ /* update without i_mutex */
20007+ if (au_ibtop(dir) == au_dbtop(dentry))
20008+ au_cpup_attr_timesizes(dir);
20009+ }
20010+ dput(h_dentry);
20011+
20012+out_mnt:
20013+ vfsub_mnt_drop_write(h_mnt);
20014+out_parent:
20015+ di_write_unlock(parent);
20016+ dput(parent);
20017+ di_write_unlock(dentry);
20018+ if (unlikely(err)) {
20019+ au_di_fin(dentry);
20020+ dentry->d_fsdata = NULL;
20021+ }
20022+out_si:
20023+ si_read_unlock(sb);
20024+out:
20025+ inode_unlock(dir);
20026+ return err;
20027+}
20028+
20029+/* ---------------------------------------------------------------------- */
20030+
20031+struct au_link_args {
20032+ aufs_bindex_t bdst, bsrc;
20033+ struct au_pin pin;
20034+ struct path h_path;
20035+ struct dentry *src_parent, *parent;
20036+};
20037+
20038+static int au_cpup_before_link(struct dentry *src_dentry,
20039+ struct au_link_args *a)
20040+{
20041+ int err;
20042+ struct dentry *h_src_dentry;
20043+ struct au_cp_generic cpg = {
20044+ .dentry = src_dentry,
20045+ .bdst = a->bdst,
20046+ .bsrc = a->bsrc,
20047+ .len = -1,
20048+ .pin = &a->pin,
20049+ .flags = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
20050+ };
20051+
20052+ di_read_lock_parent(a->src_parent, AuLock_IR);
20053+ err = au_test_and_cpup_dirs(src_dentry, a->bdst);
20054+ if (unlikely(err))
20055+ goto out;
20056+
20057+ h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
20058+ err = au_pin(&a->pin, src_dentry, a->bdst,
20059+ au_opt_udba(src_dentry->d_sb),
20060+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20061+ if (unlikely(err))
20062+ goto out;
20063+
20064+ err = au_sio_cpup_simple(&cpg);
20065+ au_unpin(&a->pin);
20066+
20067+out:
20068+ di_read_unlock(a->src_parent, AuLock_IR);
20069+ return err;
20070+}
20071+
20072+static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
20073+ struct au_link_args *a)
20074+{
20075+ int err;
20076+ unsigned char plink;
20077+ aufs_bindex_t bbot;
20078+ struct dentry *h_src_dentry;
20079+ struct inode *h_inode, *inode, *delegated;
20080+ struct super_block *sb;
20081+ struct file *h_file;
20082+
20083+ plink = 0;
20084+ h_inode = NULL;
20085+ sb = src_dentry->d_sb;
20086+ inode = d_inode(src_dentry);
20087+ if (au_ibtop(inode) <= a->bdst)
20088+ h_inode = au_h_iptr(inode, a->bdst);
20089+ if (!h_inode || !h_inode->i_nlink) {
20090+ /* copyup src_dentry as the name of dentry. */
20091+ bbot = au_dbbot(dentry);
20092+ if (bbot < a->bsrc)
20093+ au_set_dbbot(dentry, a->bsrc);
20094+ au_set_h_dptr(dentry, a->bsrc,
20095+ dget(au_h_dptr(src_dentry, a->bsrc)));
20096+ dget(a->h_path.dentry);
20097+ au_set_h_dptr(dentry, a->bdst, NULL);
20098+ AuDbg("temporary d_inode...\n");
20099+ spin_lock(&dentry->d_lock);
20100+ dentry->d_inode = d_inode(src_dentry); /* tmp */
20101+ spin_unlock(&dentry->d_lock);
20102+ h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
20103+ if (IS_ERR(h_file))
20104+ err = PTR_ERR(h_file);
20105+ else {
20106+ struct au_cp_generic cpg = {
20107+ .dentry = dentry,
20108+ .bdst = a->bdst,
20109+ .bsrc = -1,
20110+ .len = -1,
20111+ .pin = &a->pin,
20112+ .flags = AuCpup_KEEPLINO
20113+ };
20114+ err = au_sio_cpup_simple(&cpg);
20115+ au_h_open_post(dentry, a->bsrc, h_file);
20116+ if (!err) {
20117+ dput(a->h_path.dentry);
20118+ a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20119+ } else
20120+ au_set_h_dptr(dentry, a->bdst,
20121+ a->h_path.dentry);
20122+ }
20123+ spin_lock(&dentry->d_lock);
20124+ dentry->d_inode = NULL; /* restore */
20125+ spin_unlock(&dentry->d_lock);
20126+ AuDbg("temporary d_inode...done\n");
20127+ au_set_h_dptr(dentry, a->bsrc, NULL);
20128+ au_set_dbbot(dentry, bbot);
20129+ } else {
20130+ /* the inode of src_dentry already exists on a.bdst branch */
20131+ h_src_dentry = d_find_alias(h_inode);
20132+ if (!h_src_dentry && au_plink_test(inode)) {
20133+ plink = 1;
20134+ h_src_dentry = au_plink_lkup(inode, a->bdst);
20135+ err = PTR_ERR(h_src_dentry);
20136+ if (IS_ERR(h_src_dentry))
20137+ goto out;
20138+
20139+ if (unlikely(d_is_negative(h_src_dentry))) {
20140+ dput(h_src_dentry);
20141+ h_src_dentry = NULL;
20142+ }
20143+
20144+ }
20145+ if (h_src_dentry) {
20146+ delegated = NULL;
20147+ err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
20148+ &a->h_path, &delegated);
20149+ if (unlikely(err == -EWOULDBLOCK)) {
20150+ pr_warn("cannot retry for NFSv4 delegation"
20151+ " for an internal link\n");
20152+ iput(delegated);
20153+ }
20154+ dput(h_src_dentry);
20155+ } else {
20156+ AuIOErr("no dentry found for hi%lu on b%d\n",
20157+ h_inode->i_ino, a->bdst);
20158+ err = -EIO;
20159+ }
20160+ }
20161+
20162+ if (!err && !plink)
20163+ au_plink_append(inode, a->bdst, a->h_path.dentry);
20164+
20165+out:
20166+ AuTraceErr(err);
20167+ return err;
20168+}
20169+
20170+int aufs_link(struct dentry *src_dentry, struct inode *dir,
20171+ struct dentry *dentry)
20172+{
20173+ int err, rerr;
20174+ struct au_dtime dt;
20175+ struct au_link_args *a;
20176+ struct dentry *wh_dentry, *h_src_dentry;
20177+ struct inode *inode, *delegated;
20178+ struct super_block *sb;
20179+ struct au_wr_dir_args wr_dir_args = {
20180+ /* .force_btgt = -1, */
20181+ .flags = AuWrDir_ADD_ENTRY
20182+ };
20183+
20184+ IMustLock(dir);
20185+ inode = d_inode(src_dentry);
20186+ IMustLock(inode);
20187+
20188+ err = -ENOMEM;
20189+ a = kzalloc(sizeof(*a), GFP_NOFS);
20190+ if (unlikely(!a))
20191+ goto out;
20192+
20193+ a->parent = dentry->d_parent; /* dir inode is locked */
20194+ err = aufs_read_and_write_lock2(dentry, src_dentry,
20195+ AuLock_NOPLM | AuLock_GEN);
20196+ if (unlikely(err))
20197+ goto out_kfree;
20198+ err = au_d_linkable(src_dentry);
20199+ if (unlikely(err))
20200+ goto out_unlock;
20201+ err = au_d_may_add(dentry);
20202+ if (unlikely(err))
20203+ goto out_unlock;
20204+
20205+ a->src_parent = dget_parent(src_dentry);
20206+ wr_dir_args.force_btgt = au_ibtop(inode);
20207+
20208+ di_write_lock_parent(a->parent);
20209+ wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
20210+ wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
20211+ &wr_dir_args);
20212+ err = PTR_ERR(wh_dentry);
20213+ if (IS_ERR(wh_dentry))
20214+ goto out_parent;
20215+
20216+ err = 0;
20217+ sb = dentry->d_sb;
20218+ a->bdst = au_dbtop(dentry);
20219+ a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20220+ a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
20221+ a->bsrc = au_ibtop(inode);
20222+ h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
20223+ if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
20224+ h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
20225+ if (!h_src_dentry) {
20226+ a->bsrc = au_dbtop(src_dentry);
20227+ h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
20228+ AuDebugOn(!h_src_dentry);
20229+ } else if (IS_ERR(h_src_dentry)) {
20230+ err = PTR_ERR(h_src_dentry);
20231+ goto out_parent;
20232+ }
20233+
20234+ /*
20235+ * aufs doesn't touch the credential so
20236+ * security_dentry_create_files_as() is unnecessary.
20237+ */
20238+ if (au_opt_test(au_mntflags(sb), PLINK)) {
20239+ if (a->bdst < a->bsrc
20240+ /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
20241+ err = au_cpup_or_link(src_dentry, dentry, a);
20242+ else {
20243+ delegated = NULL;
20244+ err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
20245+ &a->h_path, &delegated);
20246+ if (unlikely(err == -EWOULDBLOCK)) {
20247+ pr_warn("cannot retry for NFSv4 delegation"
20248+ " for an internal link\n");
20249+ iput(delegated);
20250+ }
20251+ }
20252+ dput(h_src_dentry);
20253+ } else {
20254+ /*
20255+ * copyup src_dentry to the branch we process,
20256+ * and then link(2) to it.
20257+ */
20258+ dput(h_src_dentry);
20259+ if (a->bdst < a->bsrc
20260+ /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
20261+ au_unpin(&a->pin);
20262+ di_write_unlock(a->parent);
20263+ err = au_cpup_before_link(src_dentry, a);
20264+ di_write_lock_parent(a->parent);
20265+ if (!err)
20266+ err = au_pin(&a->pin, dentry, a->bdst,
20267+ au_opt_udba(sb),
20268+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20269+ if (unlikely(err))
20270+ goto out_wh;
20271+ }
20272+ if (!err) {
20273+ h_src_dentry = au_h_dptr(src_dentry, a->bdst);
20274+ err = -ENOENT;
20275+ if (h_src_dentry && d_is_positive(h_src_dentry)) {
20276+ delegated = NULL;
20277+ err = vfsub_link(h_src_dentry,
20278+ au_pinned_h_dir(&a->pin),
20279+ &a->h_path, &delegated);
20280+ if (unlikely(err == -EWOULDBLOCK)) {
20281+ pr_warn("cannot retry"
20282+ " for NFSv4 delegation"
20283+ " for an internal link\n");
20284+ iput(delegated);
20285+ }
20286+ }
20287+ }
20288+ }
20289+ if (unlikely(err))
20290+ goto out_unpin;
20291+
20292+ if (wh_dentry) {
20293+ a->h_path.dentry = wh_dentry;
20294+ err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
20295+ dentry);
20296+ if (unlikely(err))
20297+ goto out_revert;
20298+ }
20299+
20300+ au_dir_ts(dir, a->bdst);
20301+ inode_inc_iversion(dir);
20302+ inc_nlink(inode);
20303+ inode->i_ctime = dir->i_ctime;
20304+ d_instantiate(dentry, au_igrab(inode));
20305+ if (d_unhashed(a->h_path.dentry))
20306+ /* some filesystem calls d_drop() */
20307+ d_drop(dentry);
20308+ /* some filesystems consume an inode even hardlink */
20309+ au_fhsm_wrote(sb, a->bdst, /*force*/0);
20310+ goto out_unpin; /* success */
20311+
20312+out_revert:
20313+ /* no delegation since it is just created */
20314+ rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
20315+ /*delegated*/NULL, /*force*/0);
20316+ if (unlikely(rerr)) {
20317+ AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
20318+ err = -EIO;
20319+ }
20320+ au_dtime_revert(&dt);
20321+out_unpin:
20322+ au_unpin(&a->pin);
20323+out_wh:
20324+ dput(wh_dentry);
20325+out_parent:
20326+ di_write_unlock(a->parent);
20327+ dput(a->src_parent);
20328+out_unlock:
20329+ if (unlikely(err)) {
20330+ au_update_dbtop(dentry);
20331+ d_drop(dentry);
20332+ }
20333+ aufs_read_and_write_unlock2(dentry, src_dentry);
20334+out_kfree:
20335+ au_kfree_rcu(a);
20336+out:
20337+ AuTraceErr(err);
20338+ return err;
20339+}
20340+
20341+int aufs_mkdir(struct user_namespace *userns, struct inode *dir,
20342+ struct dentry *dentry, umode_t mode)
20343+{
20344+ int err, rerr;
20345+ aufs_bindex_t bindex;
20346+ unsigned char diropq;
20347+ struct path h_path;
20348+ struct dentry *wh_dentry, *parent, *opq_dentry;
20349+ struct inode *h_inode;
20350+ struct super_block *sb;
20351+ struct {
20352+ struct au_pin pin;
20353+ struct au_dtime dt;
20354+ } *a; /* reduce the stack usage */
20355+ struct au_wr_dir_args wr_dir_args = {
20356+ .force_btgt = -1,
20357+ .flags = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
20358+ };
20359+
20360+ IMustLock(dir);
20361+
20362+ err = -ENOMEM;
20363+ a = kmalloc(sizeof(*a), GFP_NOFS);
20364+ if (unlikely(!a))
20365+ goto out;
20366+
20367+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
20368+ if (unlikely(err))
20369+ goto out_free;
20370+ err = au_d_may_add(dentry);
20371+ if (unlikely(err))
20372+ goto out_unlock;
20373+
20374+ parent = dentry->d_parent; /* dir inode is locked */
20375+ di_write_lock_parent(parent);
20376+ wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
20377+ &a->pin, &wr_dir_args);
20378+ err = PTR_ERR(wh_dentry);
20379+ if (IS_ERR(wh_dentry))
20380+ goto out_parent;
20381+
20382+ sb = dentry->d_sb;
20383+ bindex = au_dbtop(dentry);
20384+ h_path.dentry = au_h_dptr(dentry, bindex);
20385+ h_path.mnt = au_sbr_mnt(sb, bindex);
20386+ err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
20387+ if (unlikely(err))
20388+ goto out_unpin;
20389+
20390+ /* make the dir opaque */
20391+ diropq = 0;
20392+ h_inode = d_inode(h_path.dentry);
20393+ if (wh_dentry
20394+ || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
20395+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
20396+ opq_dentry = au_diropq_create(dentry, bindex);
20397+ inode_unlock(h_inode);
20398+ err = PTR_ERR(opq_dentry);
20399+ if (IS_ERR(opq_dentry))
20400+ goto out_dir;
20401+ dput(opq_dentry);
20402+ diropq = 1;
20403+ }
20404+
20405+ err = epilog(dir, bindex, wh_dentry, dentry);
20406+ if (!err) {
20407+ inc_nlink(dir);
20408+ goto out_unpin; /* success */
20409+ }
20410+
20411+ /* revert */
20412+ if (diropq) {
20413+ AuLabel(revert opq);
20414+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
20415+ rerr = au_diropq_remove(dentry, bindex);
20416+ inode_unlock(h_inode);
20417+ if (rerr) {
20418+ AuIOErr("%pd reverting diropq failed(%d, %d)\n",
20419+ dentry, err, rerr);
20420+ err = -EIO;
20421+ }
20422+ }
20423+
20424+out_dir:
20425+ AuLabel(revert dir);
20426+ rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
20427+ if (rerr) {
20428+ AuIOErr("%pd reverting dir failed(%d, %d)\n",
20429+ dentry, err, rerr);
20430+ err = -EIO;
20431+ }
20432+ au_dtime_revert(&a->dt);
20433+out_unpin:
20434+ au_unpin(&a->pin);
20435+ dput(wh_dentry);
20436+out_parent:
20437+ di_write_unlock(parent);
20438+out_unlock:
20439+ if (unlikely(err)) {
20440+ au_update_dbtop(dentry);
20441+ d_drop(dentry);
20442+ }
20443+ aufs_read_unlock(dentry, AuLock_DW);
20444+out_free:
20445+ au_kfree_rcu(a);
20446+out:
20447+ return err;
20448+}
20449diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
20450--- /usr/share/empty/fs/aufs/i_op.c 1970-01-01 01:00:00.000000000 +0100
20451+++ linux/fs/aufs/i_op.c 2021-12-03 15:40:58.236647297 +0100
20452@@ -0,0 +1,1513 @@
20453+// SPDX-License-Identifier: GPL-2.0
20454+/*
20455+ * Copyright (C) 2005-2021 Junjiro R. Okajima
20456+ *
20457+ * This program, aufs is free software; you can redistribute it and/or modify
20458+ * it under the terms of the GNU General Public License as published by
20459+ * the Free Software Foundation; either version 2 of the License, or
20460+ * (at your option) any later version.
20461+ *
20462+ * This program is distributed in the hope that it will be useful,
20463+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20464+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20465+ * GNU General Public License for more details.
20466+ *
20467+ * You should have received a copy of the GNU General Public License
20468+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
20469+ */
20470+
20471+/*
20472+ * inode operations (except add/del/rename)
20473+ */
20474+
20475+#include <linux/device_cgroup.h>
20476+#include <linux/fs_stack.h>
20477+#include <linux/iversion.h>
20478+#include <linux/namei.h>
20479+#include <linux/security.h>
20480+#include "aufs.h"
20481+
20482+static int h_permission(struct inode *h_inode, int mask,
20483+ struct path *h_path, int brperm)
20484+{
20485+ int err;
20486+ const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
20487+ struct user_namespace *h_userns;
20488+
20489+ err = -EPERM;
20490+ if (write_mask && IS_IMMUTABLE(h_inode))
20491+ goto out;
20492+
20493+ err = -EACCES;
20494+ if (((mask & MAY_EXEC)
20495+ && S_ISREG(h_inode->i_mode)
20496+ && (path_noexec(h_path)
20497+ || !(h_inode->i_mode & 0111))))
20498+ goto out;
20499+
20500+ /*
20501+ * - skip the lower fs test in the case of write to ro branch.
20502+ * - nfs dir permission write check is optimized, but a policy for
20503+ * link/rename requires a real check.
20504+ * - nfs always sets SB_POSIXACL regardless its mount option 'noacl.'
20505+ * in this case, generic_permission() returns -EOPNOTSUPP.
20506+ */
20507+ h_userns = mnt_user_ns(h_path->mnt);
20508+ if ((write_mask && !au_br_writable(brperm))
20509+ || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
20510+ && write_mask && !(mask & MAY_READ))
20511+ || !h_inode->i_op->permission) {
20512+ /* AuLabel(generic_permission); */
20513+ /* AuDbg("get_acl %ps\n", h_inode->i_op->get_acl); */
20514+ err = generic_permission(h_userns, h_inode, mask);
20515+ if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
20516+ err = h_inode->i_op->permission(h_userns, h_inode,
20517+ mask);
20518+ AuTraceErr(err);
20519+ } else {
20520+ /* AuLabel(h_inode->permission); */
20521+ err = h_inode->i_op->permission(h_userns, h_inode, mask);
20522+ AuTraceErr(err);
20523+ }
20524+
20525+ if (!err)
20526+ err = devcgroup_inode_permission(h_inode, mask);
20527+ if (!err)
20528+ err = security_inode_permission(h_inode, mask);
20529+
20530+out:
20531+ return err;
20532+}
20533+
20534+static int aufs_permission(struct user_namespace *userns, struct inode *inode,
20535+ int mask)
20536+{
20537+ int err;
20538+ aufs_bindex_t bindex, bbot;
20539+ const unsigned char isdir = !!S_ISDIR(inode->i_mode),
20540+ write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
20541+ struct inode *h_inode;
20542+ struct super_block *sb;
20543+ struct au_branch *br;
20544+
20545+ /* todo: support rcu-walk? */
20546+ if (mask & MAY_NOT_BLOCK)
20547+ return -ECHILD;
20548+
20549+ sb = inode->i_sb;
20550+ si_read_lock(sb, AuLock_FLUSH);
20551+ ii_read_lock_child(inode);
20552+#if 0 /* reserved for future use */
20553+ /*
20554+ * This test may be rather 'too much' since the test is essentially done
20555+ * in the aufs_lookup(). Theoretically it is possible that the inode
20556+ * generation doesn't match to the superblock's here. But it isn't a
20557+ * big deal I suppose.
20558+ */
20559+ err = au_iigen_test(inode, au_sigen(sb));
20560+ if (unlikely(err))
20561+ goto out;
20562+#endif
20563+
20564+ if (!isdir
20565+ || write_mask
20566+ || au_opt_test(au_mntflags(sb), DIRPERM1)) {
20567+ err = au_busy_or_stale();
20568+ h_inode = au_h_iptr(inode, au_ibtop(inode));
20569+ if (unlikely(!h_inode
20570+ || (h_inode->i_mode & S_IFMT)
20571+ != (inode->i_mode & S_IFMT)))
20572+ goto out;
20573+
20574+ err = 0;
20575+ bindex = au_ibtop(inode);
20576+ br = au_sbr(sb, bindex);
20577+ err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
20578+ if (write_mask
20579+ && !err
20580+ && !special_file(h_inode->i_mode)) {
20581+ /* test whether the upper writable branch exists */
20582+ err = -EROFS;
20583+ for (; bindex >= 0; bindex--)
20584+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
20585+ err = 0;
20586+ break;
20587+ }
20588+ }
20589+ goto out;
20590+ }
20591+
20592+ /* non-write to dir */
20593+ err = 0;
20594+ bbot = au_ibbot(inode);
20595+ for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
20596+ h_inode = au_h_iptr(inode, bindex);
20597+ if (h_inode) {
20598+ err = au_busy_or_stale();
20599+ if (unlikely(!S_ISDIR(h_inode->i_mode)))
20600+ break;
20601+
20602+ br = au_sbr(sb, bindex);
20603+ err = h_permission(h_inode, mask, &br->br_path,
20604+ br->br_perm);
20605+ }
20606+ }
20607+
20608+out:
20609+ ii_read_unlock(inode);
20610+ si_read_unlock(sb);
20611+ return err;
20612+}
20613+
20614+/* ---------------------------------------------------------------------- */
20615+
20616+static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
20617+ unsigned int flags)
20618+{
20619+ struct dentry *ret, *parent;
20620+ struct inode *inode;
20621+ struct super_block *sb;
20622+ int err, npositive;
20623+
20624+ IMustLock(dir);
20625+
20626+ /* todo: support rcu-walk? */
20627+ ret = ERR_PTR(-ECHILD);
20628+ if (flags & LOOKUP_RCU)
20629+ goto out;
20630+
20631+ ret = ERR_PTR(-ENAMETOOLONG);
20632+ if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20633+ goto out;
20634+
20635+ sb = dir->i_sb;
20636+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
20637+ ret = ERR_PTR(err);
20638+ if (unlikely(err))
20639+ goto out;
20640+
20641+ err = au_di_init(dentry);
20642+ ret = ERR_PTR(err);
20643+ if (unlikely(err))
20644+ goto out_si;
20645+
20646+ inode = NULL;
20647+ npositive = 0; /* suppress a warning */
20648+ parent = dentry->d_parent; /* dir inode is locked */
20649+ di_read_lock_parent(parent, AuLock_IR);
20650+ err = au_alive_dir(parent);
20651+ if (!err)
20652+ err = au_digen_test(parent, au_sigen(sb));
20653+ if (!err) {
20654+ /* regardless LOOKUP_CREATE, always ALLOW_NEG */
20655+ npositive = au_lkup_dentry(dentry, au_dbtop(parent),
20656+ AuLkup_ALLOW_NEG);
20657+ err = npositive;
20658+ }
20659+ di_read_unlock(parent, AuLock_IR);
20660+ ret = ERR_PTR(err);
20661+ if (unlikely(err < 0))
20662+ goto out_unlock;
20663+
20664+ if (npositive) {
20665+ inode = au_new_inode(dentry, /*must_new*/0);
20666+ if (IS_ERR(inode)) {
20667+ ret = (void *)inode;
20668+ inode = NULL;
20669+ goto out_unlock;
20670+ }
20671+ }
20672+
20673+ if (inode)
20674+ atomic_inc(&inode->i_count);
20675+ ret = d_splice_alias(inode, dentry);
20676+#if 0 /* reserved for future use */
20677+ if (unlikely(d_need_lookup(dentry))) {
20678+ spin_lock(&dentry->d_lock);
20679+ dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
20680+ spin_unlock(&dentry->d_lock);
20681+ } else
20682+#endif
20683+ if (inode) {
20684+ if (!IS_ERR(ret)) {
20685+ iput(inode);
20686+ if (ret && ret != dentry)
20687+ ii_write_unlock(inode);
20688+ } else {
20689+ ii_write_unlock(inode);
20690+ iput(inode);
20691+ inode = NULL;
20692+ }
20693+ }
20694+
20695+out_unlock:
20696+ di_write_unlock(dentry);
20697+out_si:
20698+ si_read_unlock(sb);
20699+out:
20700+ return ret;
20701+}
20702+
20703+/* ---------------------------------------------------------------------- */
20704+
20705+/*
20706+ * very dirty and complicated aufs ->atomic_open().
20707+ * aufs_atomic_open()
20708+ * + au_aopen_or_create()
20709+ * + add_simple()
20710+ * + vfsub_atomic_open()
20711+ * + branch fs ->atomic_open()
20712+ * may call the actual 'open' for h_file
20713+ * + inc br_nfiles only if opened
20714+ * + au_aopen_no_open() or au_aopen_do_open()
20715+ *
20716+ * au_aopen_do_open()
20717+ * + finish_open()
20718+ * + au_do_aopen()
20719+ * + au_do_open() the body of all 'open'
20720+ * + au_do_open_nondir()
20721+ * set the passed h_file
20722+ *
20723+ * au_aopen_no_open()
20724+ * + finish_no_open()
20725+ */
20726+
20727+struct aopen_node {
20728+ struct hlist_bl_node hblist;
20729+ struct file *file, *h_file;
20730+};
20731+
20732+static int au_do_aopen(struct inode *inode, struct file *file)
20733+{
20734+ struct hlist_bl_head *aopen;
20735+ struct hlist_bl_node *pos;
20736+ struct aopen_node *node;
20737+ struct au_do_open_args args = {
20738+ .aopen = 1,
20739+ .open = au_do_open_nondir
20740+ };
20741+
20742+ aopen = &au_sbi(inode->i_sb)->si_aopen;
20743+ hlist_bl_lock(aopen);
20744+ hlist_bl_for_each_entry(node, pos, aopen, hblist)
20745+ if (node->file == file) {
20746+ args.h_file = node->h_file;
20747+ break;
20748+ }
20749+ hlist_bl_unlock(aopen);
20750+ /* AuDebugOn(!args.h_file); */
20751+
20752+ return au_do_open(file, &args);
20753+}
20754+
20755+static int au_aopen_do_open(struct file *file, struct dentry *dentry,
20756+ struct aopen_node *aopen_node)
20757+{
20758+ int err;
20759+ struct hlist_bl_head *aopen;
20760+
20761+ AuLabel(here);
20762+ aopen = &au_sbi(dentry->d_sb)->si_aopen;
20763+ au_hbl_add(&aopen_node->hblist, aopen);
20764+ err = finish_open(file, dentry, au_do_aopen);
20765+ au_hbl_del(&aopen_node->hblist, aopen);
20766+ /* AuDbgFile(file); */
20767+ AuDbg("%pd%s%s\n", dentry,
20768+ (file->f_mode & FMODE_CREATED) ? " created" : "",
20769+ (file->f_mode & FMODE_OPENED) ? " opened" : "");
20770+
20771+ AuTraceErr(err);
20772+ return err;
20773+}
20774+
20775+static int au_aopen_no_open(struct file *file, struct dentry *dentry)
20776+{
20777+ int err;
20778+
20779+ AuLabel(here);
20780+ dget(dentry);
20781+ err = finish_no_open(file, dentry);
20782+
20783+ AuTraceErr(err);
20784+ return err;
20785+}
20786+
20787+static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
20788+ struct file *file, unsigned int open_flag,
20789+ umode_t create_mode)
20790+{
20791+ int err, did_open;
20792+ unsigned int lkup_flags;
20793+ aufs_bindex_t bindex;
20794+ struct super_block *sb;
20795+ struct dentry *parent, *d;
20796+ struct vfsub_aopen_args args = {
20797+ .open_flag = open_flag,
20798+ .create_mode = create_mode
20799+ };
20800+ struct aopen_node aopen_node = {
20801+ .file = file
20802+ };
20803+
20804+ IMustLock(dir);
20805+ AuDbg("open_flag 0%o\n", open_flag);
20806+ AuDbgDentry(dentry);
20807+
20808+ err = 0;
20809+ if (!au_di(dentry)) {
20810+ lkup_flags = LOOKUP_OPEN;
20811+ if (open_flag & O_CREAT)
20812+ lkup_flags |= LOOKUP_CREATE;
20813+ d = aufs_lookup(dir, dentry, lkup_flags);
20814+ if (IS_ERR(d)) {
20815+ err = PTR_ERR(d);
20816+ AuTraceErr(err);
20817+ goto out;
20818+ } else if (d) {
20819+ /*
20820+ * obsoleted dentry found.
20821+ * another error will be returned later.
20822+ */
20823+ d_drop(d);
20824+ AuDbgDentry(d);
20825+ dput(d);
20826+ }
20827+ AuDbgDentry(dentry);
20828+ }
20829+
20830+ if (d_is_positive(dentry)
20831+ || d_unhashed(dentry)
20832+ || d_unlinked(dentry)
20833+ || !(open_flag & O_CREAT)) {
20834+ err = au_aopen_no_open(file, dentry);
20835+ goto out; /* success */
20836+ }
20837+
20838+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
20839+ if (unlikely(err))
20840+ goto out;
20841+
20842+ sb = dentry->d_sb;
20843+ parent = dentry->d_parent; /* dir is locked */
20844+ di_write_lock_parent(parent);
20845+ err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
20846+ if (unlikely(err < 0))
20847+ goto out_parent;
20848+
20849+ AuDbgDentry(dentry);
20850+ if (d_is_positive(dentry)) {
20851+ err = au_aopen_no_open(file, dentry);
20852+ goto out_parent; /* success */
20853+ }
20854+
20855+ args.file = alloc_empty_file(file->f_flags, current_cred());
20856+ err = PTR_ERR(args.file);
20857+ if (IS_ERR(args.file))
20858+ goto out_parent;
20859+
20860+ bindex = au_dbtop(dentry);
20861+ err = au_aopen_or_create(dir, dentry, &args);
20862+ AuTraceErr(err);
20863+ AuDbgFile(args.file);
20864+ file->f_mode = args.file->f_mode & ~FMODE_OPENED;
20865+ did_open = !!(args.file->f_mode & FMODE_OPENED);
20866+ if (!did_open) {
20867+ fput(args.file);
20868+ args.file = NULL;
20869+ }
20870+ di_write_unlock(parent);
20871+ di_write_unlock(dentry);
20872+ if (unlikely(err < 0)) {
20873+ if (args.file)
20874+ fput(args.file);
20875+ goto out_sb;
20876+ }
20877+
20878+ if (!did_open)
20879+ err = au_aopen_no_open(file, dentry);
20880+ else {
20881+ aopen_node.h_file = args.file;
20882+ err = au_aopen_do_open(file, dentry, &aopen_node);
20883+ }
20884+ if (unlikely(err < 0)) {
20885+ if (args.file)
20886+ fput(args.file);
20887+ if (did_open)
20888+ au_lcnt_dec(&args.br->br_nfiles);
20889+ }
20890+ goto out_sb; /* success */
20891+
20892+out_parent:
20893+ di_write_unlock(parent);
20894+ di_write_unlock(dentry);
20895+out_sb:
20896+ si_read_unlock(sb);
20897+out:
20898+ AuTraceErr(err);
20899+ AuDbgFile(file);
20900+ return err;
20901+}
20902+
20903+
20904+/* ---------------------------------------------------------------------- */
20905+
20906+static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
20907+ const unsigned char add_entry, aufs_bindex_t bcpup,
20908+ aufs_bindex_t btop)
20909+{
20910+ int err;
20911+ struct dentry *h_parent;
20912+ struct inode *h_dir;
20913+
20914+ if (add_entry)
20915+ IMustLock(d_inode(parent));
20916+ else
20917+ di_write_lock_parent(parent);
20918+
20919+ err = 0;
20920+ if (!au_h_dptr(parent, bcpup)) {
20921+ if (btop > bcpup)
20922+ err = au_cpup_dirs(dentry, bcpup);
20923+ else if (btop < bcpup)
20924+ err = au_cpdown_dirs(dentry, bcpup);
20925+ else
20926+ BUG();
20927+ }
20928+ if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
20929+ h_parent = au_h_dptr(parent, bcpup);
20930+ h_dir = d_inode(h_parent);
20931+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
20932+ err = au_lkup_neg(dentry, bcpup, /*wh*/0);
20933+ /* todo: no unlock here */
20934+ inode_unlock_shared(h_dir);
20935+
20936+ AuDbg("bcpup %d\n", bcpup);
20937+ if (!err) {
20938+ if (d_really_is_negative(dentry))
20939+ au_set_h_dptr(dentry, btop, NULL);
20940+ au_update_dbrange(dentry, /*do_put_zero*/0);
20941+ }
20942+ }
20943+
20944+ if (!add_entry)
20945+ di_write_unlock(parent);
20946+ if (!err)
20947+ err = bcpup; /* success */
20948+
20949+ AuTraceErr(err);
20950+ return err;
20951+}
20952+
20953+/*
20954+ * decide the branch and the parent dir where we will create a new entry.
20955+ * returns new bindex or an error.
20956+ * copyup the parent dir if needed.
20957+ */
20958+int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
20959+ struct au_wr_dir_args *args)
20960+{
20961+ int err;
20962+ unsigned int flags;
20963+ aufs_bindex_t bcpup, btop, src_btop;
20964+ const unsigned char add_entry
20965+ = au_ftest_wrdir(args->flags, ADD_ENTRY)
20966+ | au_ftest_wrdir(args->flags, TMPFILE);
20967+ struct super_block *sb;
20968+ struct dentry *parent;
20969+ struct au_sbinfo *sbinfo;
20970+
20971+ sb = dentry->d_sb;
20972+ sbinfo = au_sbi(sb);
20973+ parent = dget_parent(dentry);
20974+ btop = au_dbtop(dentry);
20975+ bcpup = btop;
20976+ if (args->force_btgt < 0) {
20977+ if (src_dentry) {
20978+ src_btop = au_dbtop(src_dentry);
20979+ if (src_btop < btop)
20980+ bcpup = src_btop;
20981+ } else if (add_entry) {
20982+ flags = 0;
20983+ if (au_ftest_wrdir(args->flags, ISDIR))
20984+ au_fset_wbr(flags, DIR);
20985+ err = AuWbrCreate(sbinfo, dentry, flags);
20986+ bcpup = err;
20987+ }
20988+
20989+ if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
20990+ if (add_entry)
20991+ err = AuWbrCopyup(sbinfo, dentry);
20992+ else {
20993+ if (!IS_ROOT(dentry)) {
20994+ di_read_lock_parent(parent, !AuLock_IR);
20995+ err = AuWbrCopyup(sbinfo, dentry);
20996+ di_read_unlock(parent, !AuLock_IR);
20997+ } else
20998+ err = AuWbrCopyup(sbinfo, dentry);
20999+ }
21000+ bcpup = err;
21001+ if (unlikely(err < 0))
21002+ goto out;
21003+ }
21004+ } else {
21005+ bcpup = args->force_btgt;
21006+ AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
21007+ }
21008+
21009+ AuDbg("btop %d, bcpup %d\n", btop, bcpup);
21010+ err = bcpup;
21011+ if (bcpup == btop)
21012+ goto out; /* success */
21013+
21014+ /* copyup the new parent into the branch we process */
21015+ err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
21016+ if (err >= 0) {
21017+ if (d_really_is_negative(dentry)) {
21018+ au_set_h_dptr(dentry, btop, NULL);
21019+ au_set_dbtop(dentry, bcpup);
21020+ au_set_dbbot(dentry, bcpup);
21021+ }
21022+ AuDebugOn(add_entry
21023+ && !au_ftest_wrdir(args->flags, TMPFILE)
21024+ && !au_h_dptr(dentry, bcpup));
21025+ }
21026+
21027+out:
21028+ dput(parent);
21029+ return err;
21030+}
21031+
21032+/* ---------------------------------------------------------------------- */
21033+
21034+void au_pin_hdir_unlock(struct au_pin *p)
21035+{
21036+ if (p->hdir)
21037+ au_hn_inode_unlock(p->hdir);
21038+}
21039+
21040+int au_pin_hdir_lock(struct au_pin *p)
21041+{
21042+ int err;
21043+
21044+ err = 0;
21045+ if (!p->hdir)
21046+ goto out;
21047+
21048+ /* even if an error happens later, keep this lock */
21049+ au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
21050+
21051+ err = -EBUSY;
21052+ if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
21053+ goto out;
21054+
21055+ err = 0;
21056+ if (p->h_dentry)
21057+ err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
21058+ p->h_parent, p->br);
21059+
21060+out:
21061+ return err;
21062+}
21063+
21064+int au_pin_hdir_relock(struct au_pin *p)
21065+{
21066+ int err, i;
21067+ struct inode *h_i;
21068+ struct dentry *h_d[] = {
21069+ p->h_dentry,
21070+ p->h_parent
21071+ };
21072+
21073+ err = au_pin_hdir_lock(p);
21074+ if (unlikely(err))
21075+ goto out;
21076+
21077+ for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
21078+ if (!h_d[i])
21079+ continue;
21080+ if (d_is_positive(h_d[i])) {
21081+ h_i = d_inode(h_d[i]);
21082+ err = !h_i->i_nlink;
21083+ }
21084+ }
21085+
21086+out:
21087+ return err;
21088+}
21089+
21090+static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
21091+{
21092+ atomic_long_set(&p->hdir->hi_inode->i_rwsem.owner, (long)task);
21093+}
21094+
21095+void au_pin_hdir_acquire_nest(struct au_pin *p)
21096+{
21097+ if (p->hdir) {
21098+ rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
21099+ p->lsc_hi, 0, NULL, _RET_IP_);
21100+ au_pin_hdir_set_owner(p, current);
21101+ }
21102+}
21103+
21104+void au_pin_hdir_release(struct au_pin *p)
21105+{
21106+ if (p->hdir) {
21107+ au_pin_hdir_set_owner(p, p->task);
21108+ rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, _RET_IP_);
21109+ }
21110+}
21111+
21112+struct dentry *au_pinned_h_parent(struct au_pin *pin)
21113+{
21114+ if (pin && pin->parent)
21115+ return au_h_dptr(pin->parent, pin->bindex);
21116+ return NULL;
21117+}
21118+
21119+void au_unpin(struct au_pin *p)
21120+{
21121+ if (p->hdir)
21122+ au_pin_hdir_unlock(p);
21123+ if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
21124+ vfsub_mnt_drop_write(p->h_mnt);
21125+ if (!p->hdir)
21126+ return;
21127+
21128+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21129+ di_read_unlock(p->parent, AuLock_IR);
21130+ iput(p->hdir->hi_inode);
21131+ dput(p->parent);
21132+ p->parent = NULL;
21133+ p->hdir = NULL;
21134+ p->h_mnt = NULL;
21135+ /* do not clear p->task */
21136+}
21137+
21138+int au_do_pin(struct au_pin *p)
21139+{
21140+ int err;
21141+ struct super_block *sb;
21142+ struct inode *h_dir;
21143+
21144+ err = 0;
21145+ sb = p->dentry->d_sb;
21146+ p->br = au_sbr(sb, p->bindex);
21147+ if (IS_ROOT(p->dentry)) {
21148+ if (au_ftest_pin(p->flags, MNT_WRITE)) {
21149+ p->h_mnt = au_br_mnt(p->br);
21150+ err = vfsub_mnt_want_write(p->h_mnt);
21151+ if (unlikely(err)) {
21152+ au_fclr_pin(p->flags, MNT_WRITE);
21153+ goto out_err;
21154+ }
21155+ }
21156+ goto out;
21157+ }
21158+
21159+ p->h_dentry = NULL;
21160+ if (p->bindex <= au_dbbot(p->dentry))
21161+ p->h_dentry = au_h_dptr(p->dentry, p->bindex);
21162+
21163+ p->parent = dget_parent(p->dentry);
21164+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21165+ di_read_lock(p->parent, AuLock_IR, p->lsc_di);
21166+
21167+ h_dir = NULL;
21168+ p->h_parent = au_h_dptr(p->parent, p->bindex);
21169+ p->hdir = au_hi(d_inode(p->parent), p->bindex);
21170+ if (p->hdir)
21171+ h_dir = p->hdir->hi_inode;
21172+
21173+ /*
21174+ * udba case, or
21175+ * if DI_LOCKED is not set, then p->parent may be different
21176+ * and h_parent can be NULL.
21177+ */
21178+ if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
21179+ err = -EBUSY;
21180+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21181+ di_read_unlock(p->parent, AuLock_IR);
21182+ dput(p->parent);
21183+ p->parent = NULL;
21184+ goto out_err;
21185+ }
21186+
21187+ if (au_ftest_pin(p->flags, MNT_WRITE)) {
21188+ p->h_mnt = au_br_mnt(p->br);
21189+ err = vfsub_mnt_want_write(p->h_mnt);
21190+ if (unlikely(err)) {
21191+ au_fclr_pin(p->flags, MNT_WRITE);
21192+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21193+ di_read_unlock(p->parent, AuLock_IR);
21194+ dput(p->parent);
21195+ p->parent = NULL;
21196+ goto out_err;
21197+ }
21198+ }
21199+
21200+ au_igrab(h_dir);
21201+ err = au_pin_hdir_lock(p);
21202+ if (!err)
21203+ goto out; /* success */
21204+
21205+ au_unpin(p);
21206+
21207+out_err:
21208+ pr_err("err %d\n", err);
21209+ err = au_busy_or_stale();
21210+out:
21211+ return err;
21212+}
21213+
21214+void au_pin_init(struct au_pin *p, struct dentry *dentry,
21215+ aufs_bindex_t bindex, int lsc_di, int lsc_hi,
21216+ unsigned int udba, unsigned char flags)
21217+{
21218+ p->dentry = dentry;
21219+ p->udba = udba;
21220+ p->lsc_di = lsc_di;
21221+ p->lsc_hi = lsc_hi;
21222+ p->flags = flags;
21223+ p->bindex = bindex;
21224+
21225+ p->parent = NULL;
21226+ p->hdir = NULL;
21227+ p->h_mnt = NULL;
21228+
21229+ p->h_dentry = NULL;
21230+ p->h_parent = NULL;
21231+ p->br = NULL;
21232+ p->task = current;
21233+}
21234+
21235+int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
21236+ unsigned int udba, unsigned char flags)
21237+{
21238+ au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
21239+ udba, flags);
21240+ return au_do_pin(pin);
21241+}
21242+
21243+/* ---------------------------------------------------------------------- */
21244+
21245+/*
21246+ * ->setattr() and ->getattr() are called in various cases.
21247+ * chmod, stat: dentry is revalidated.
21248+ * fchmod, fstat: file and dentry are not revalidated, additionally they may be
21249+ * unhashed.
21250+ * for ->setattr(), ia->ia_file is passed from ftruncate only.
21251+ */
21252+/* todo: consolidate with do_refresh() and simple_reval_dpath() */
21253+int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
21254+{
21255+ int err;
21256+ struct dentry *parent;
21257+
21258+ err = 0;
21259+ if (au_digen_test(dentry, sigen)) {
21260+ parent = dget_parent(dentry);
21261+ di_read_lock_parent(parent, AuLock_IR);
21262+ err = au_refresh_dentry(dentry, parent);
21263+ di_read_unlock(parent, AuLock_IR);
21264+ dput(parent);
21265+ }
21266+
21267+ AuTraceErr(err);
21268+ return err;
21269+}
21270+
21271+int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
21272+ struct au_icpup_args *a)
21273+{
21274+ int err;
21275+ loff_t sz;
21276+ aufs_bindex_t btop, ibtop;
21277+ struct dentry *hi_wh, *parent;
21278+ struct inode *inode;
21279+ struct au_wr_dir_args wr_dir_args = {
21280+ .force_btgt = -1,
21281+ .flags = 0
21282+ };
21283+
21284+ if (d_is_dir(dentry))
21285+ au_fset_wrdir(wr_dir_args.flags, ISDIR);
21286+ /* plink or hi_wh() case */
21287+ btop = au_dbtop(dentry);
21288+ inode = d_inode(dentry);
21289+ ibtop = au_ibtop(inode);
21290+ if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
21291+ wr_dir_args.force_btgt = ibtop;
21292+ err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
21293+ if (unlikely(err < 0))
21294+ goto out;
21295+ a->btgt = err;
21296+ if (err != btop)
21297+ au_fset_icpup(a->flags, DID_CPUP);
21298+
21299+ err = 0;
21300+ a->pin_flags = AuPin_MNT_WRITE;
21301+ parent = NULL;
21302+ if (!IS_ROOT(dentry)) {
21303+ au_fset_pin(a->pin_flags, DI_LOCKED);
21304+ parent = dget_parent(dentry);
21305+ di_write_lock_parent(parent);
21306+ }
21307+
21308+ err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
21309+ if (unlikely(err))
21310+ goto out_parent;
21311+
21312+ sz = -1;
21313+ a->h_path.dentry = au_h_dptr(dentry, btop);
21314+ a->h_inode = d_inode(a->h_path.dentry);
21315+ if (ia && (ia->ia_valid & ATTR_SIZE)) {
21316+ inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
21317+ if (ia->ia_size < i_size_read(a->h_inode))
21318+ sz = ia->ia_size;
21319+ inode_unlock_shared(a->h_inode);
21320+ }
21321+
21322+ hi_wh = NULL;
21323+ if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
21324+ hi_wh = au_hi_wh(inode, a->btgt);
21325+ if (!hi_wh) {
21326+ struct au_cp_generic cpg = {
21327+ .dentry = dentry,
21328+ .bdst = a->btgt,
21329+ .bsrc = -1,
21330+ .len = sz,
21331+ .pin = &a->pin
21332+ };
21333+ err = au_sio_cpup_wh(&cpg, /*file*/NULL);
21334+ if (unlikely(err))
21335+ goto out_unlock;
21336+ hi_wh = au_hi_wh(inode, a->btgt);
21337+ /* todo: revalidate hi_wh? */
21338+ }
21339+ }
21340+
21341+ if (parent) {
21342+ au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
21343+ di_downgrade_lock(parent, AuLock_IR);
21344+ dput(parent);
21345+ parent = NULL;
21346+ }
21347+ if (!au_ftest_icpup(a->flags, DID_CPUP))
21348+ goto out; /* success */
21349+
21350+ if (!d_unhashed(dentry)) {
21351+ struct au_cp_generic cpg = {
21352+ .dentry = dentry,
21353+ .bdst = a->btgt,
21354+ .bsrc = btop,
21355+ .len = sz,
21356+ .pin = &a->pin,
21357+ .flags = AuCpup_DTIME | AuCpup_HOPEN
21358+ };
21359+ err = au_sio_cpup_simple(&cpg);
21360+ if (!err)
21361+ a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21362+ } else if (!hi_wh)
21363+ a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21364+ else
21365+ a->h_path.dentry = hi_wh; /* do not dget here */
21366+
21367+out_unlock:
21368+ a->h_inode = d_inode(a->h_path.dentry);
21369+ if (!err)
21370+ goto out; /* success */
21371+ au_unpin(&a->pin);
21372+out_parent:
21373+ if (parent) {
21374+ di_write_unlock(parent);
21375+ dput(parent);
21376+ }
21377+out:
21378+ if (!err)
21379+ inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
21380+ return err;
21381+}
21382+
21383+static int aufs_setattr(struct user_namespace *userns, struct dentry *dentry,
21384+ struct iattr *ia)
21385+{
21386+ int err;
21387+ struct inode *inode, *delegated;
21388+ struct super_block *sb;
21389+ struct file *file;
21390+ struct au_icpup_args *a;
21391+ struct user_namespace *h_userns;
21392+
21393+ inode = d_inode(dentry);
21394+ IMustLock(inode);
21395+
21396+ err = setattr_prepare(userns, dentry, ia);
21397+ if (unlikely(err))
21398+ goto out;
21399+
21400+ err = -ENOMEM;
21401+ a = kzalloc(sizeof(*a), GFP_NOFS);
21402+ if (unlikely(!a))
21403+ goto out;
21404+
21405+ if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
21406+ ia->ia_valid &= ~ATTR_MODE;
21407+
21408+ file = NULL;
21409+ sb = dentry->d_sb;
21410+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21411+ if (unlikely(err))
21412+ goto out_kfree;
21413+
21414+ if (ia->ia_valid & ATTR_FILE) {
21415+ /* currently ftruncate(2) only */
21416+ AuDebugOn(!d_is_reg(dentry));
21417+ file = ia->ia_file;
21418+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
21419+ /*fi_lsc*/0);
21420+ if (unlikely(err))
21421+ goto out_si;
21422+ ia->ia_file = au_hf_top(file);
21423+ a->udba = AuOpt_UDBA_NONE;
21424+ } else {
21425+ /* fchmod() doesn't pass ia_file */
21426+ a->udba = au_opt_udba(sb);
21427+ di_write_lock_child(dentry);
21428+ /* no d_unlinked(), to set UDBA_NONE for root */
21429+ if (d_unhashed(dentry))
21430+ a->udba = AuOpt_UDBA_NONE;
21431+ if (a->udba != AuOpt_UDBA_NONE) {
21432+ AuDebugOn(IS_ROOT(dentry));
21433+ err = au_reval_for_attr(dentry, au_sigen(sb));
21434+ if (unlikely(err))
21435+ goto out_dentry;
21436+ }
21437+ }
21438+
21439+ err = au_pin_and_icpup(dentry, ia, a);
21440+ if (unlikely(err < 0))
21441+ goto out_dentry;
21442+ if (au_ftest_icpup(a->flags, DID_CPUP)) {
21443+ ia->ia_file = NULL;
21444+ ia->ia_valid &= ~ATTR_FILE;
21445+ }
21446+
21447+ a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
21448+ if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
21449+ == (ATTR_MODE | ATTR_CTIME)) {
21450+ err = security_path_chmod(&a->h_path, ia->ia_mode);
21451+ if (unlikely(err))
21452+ goto out_unlock;
21453+ } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
21454+ && (ia->ia_valid & ATTR_CTIME)) {
21455+ err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
21456+ if (unlikely(err))
21457+ goto out_unlock;
21458+ }
21459+
21460+ if (ia->ia_valid & ATTR_SIZE) {
21461+ struct file *f;
21462+
21463+ if (ia->ia_size < i_size_read(inode))
21464+ /* unmap only */
21465+ truncate_setsize(inode, ia->ia_size);
21466+
21467+ f = NULL;
21468+ if (ia->ia_valid & ATTR_FILE)
21469+ f = ia->ia_file;
21470+ inode_unlock(a->h_inode);
21471+ err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
21472+ inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
21473+ } else {
21474+ delegated = NULL;
21475+ while (1) {
21476+ err = vfsub_notify_change(&a->h_path, ia, &delegated);
21477+ if (delegated) {
21478+ err = break_deleg_wait(&delegated);
21479+ if (!err)
21480+ continue;
21481+ }
21482+ break;
21483+ }
21484+ }
21485+ /*
21486+ * regardless aufs 'acl' option setting.
21487+ * why don't all acl-aware fs call this func from their ->setattr()?
21488+ */
21489+ if (!err && (ia->ia_valid & ATTR_MODE)) {
21490+ h_userns = mnt_user_ns(a->h_path.mnt);
21491+ err = vfsub_acl_chmod(h_userns, a->h_inode, ia->ia_mode);
21492+ }
21493+ if (!err)
21494+ au_cpup_attr_changeable(inode);
21495+
21496+out_unlock:
21497+ inode_unlock(a->h_inode);
21498+ au_unpin(&a->pin);
21499+ if (unlikely(err))
21500+ au_update_dbtop(dentry);
21501+out_dentry:
21502+ di_write_unlock(dentry);
21503+ if (file) {
21504+ fi_write_unlock(file);
21505+ ia->ia_file = file;
21506+ ia->ia_valid |= ATTR_FILE;
21507+ }
21508+out_si:
21509+ si_read_unlock(sb);
21510+out_kfree:
21511+ au_kfree_rcu(a);
21512+out:
21513+ AuTraceErr(err);
21514+ return err;
21515+}
21516+
21517+#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
21518+static int au_h_path_to_set_attr(struct dentry *dentry,
21519+ struct au_icpup_args *a, struct path *h_path)
21520+{
21521+ int err;
21522+ struct super_block *sb;
21523+
21524+ sb = dentry->d_sb;
21525+ a->udba = au_opt_udba(sb);
21526+ /* no d_unlinked(), to set UDBA_NONE for root */
21527+ if (d_unhashed(dentry))
21528+ a->udba = AuOpt_UDBA_NONE;
21529+ if (a->udba != AuOpt_UDBA_NONE) {
21530+ AuDebugOn(IS_ROOT(dentry));
21531+ err = au_reval_for_attr(dentry, au_sigen(sb));
21532+ if (unlikely(err))
21533+ goto out;
21534+ }
21535+ err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
21536+ if (unlikely(err < 0))
21537+ goto out;
21538+
21539+ h_path->dentry = a->h_path.dentry;
21540+ h_path->mnt = au_sbr_mnt(sb, a->btgt);
21541+
21542+out:
21543+ return err;
21544+}
21545+
21546+ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
21547+ struct au_sxattr *arg)
21548+{
21549+ int err;
21550+ struct path h_path;
21551+ struct super_block *sb;
21552+ struct au_icpup_args *a;
21553+ struct inode *h_inode;
21554+ struct user_namespace *h_userns;
21555+
21556+ IMustLock(inode);
21557+
21558+ err = -ENOMEM;
21559+ a = kzalloc(sizeof(*a), GFP_NOFS);
21560+ if (unlikely(!a))
21561+ goto out;
21562+
21563+ sb = dentry->d_sb;
21564+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21565+ if (unlikely(err))
21566+ goto out_kfree;
21567+
21568+ h_path.dentry = NULL; /* silence gcc */
21569+ di_write_lock_child(dentry);
21570+ err = au_h_path_to_set_attr(dentry, a, &h_path);
21571+ if (unlikely(err))
21572+ goto out_di;
21573+ h_userns = mnt_user_ns(h_path.mnt);
21574+
21575+ inode_unlock(a->h_inode);
21576+ switch (arg->type) {
21577+ case AU_XATTR_SET:
21578+ AuDebugOn(d_is_negative(h_path.dentry));
21579+ err = vfsub_setxattr(h_userns, h_path.dentry,
21580+ arg->u.set.name, arg->u.set.value,
21581+ arg->u.set.size, arg->u.set.flags);
21582+ break;
21583+ case AU_ACL_SET:
21584+ err = -EOPNOTSUPP;
21585+ h_inode = d_inode(h_path.dentry);
21586+ if (h_inode->i_op->set_acl) {
21587+ /* this will call posix_acl_update_mode */
21588+ err = h_inode->i_op->set_acl(h_userns, h_inode,
21589+ arg->u.acl_set.acl,
21590+ arg->u.acl_set.type);
21591+ }
21592+ break;
21593+ }
21594+ if (!err)
21595+ au_cpup_attr_timesizes(inode);
21596+
21597+ au_unpin(&a->pin);
21598+ if (unlikely(err))
21599+ au_update_dbtop(dentry);
21600+
21601+out_di:
21602+ di_write_unlock(dentry);
21603+ si_read_unlock(sb);
21604+out_kfree:
21605+ au_kfree_rcu(a);
21606+out:
21607+ AuTraceErr(err);
21608+ return err;
21609+}
21610+#endif
21611+
21612+static void au_refresh_iattr(struct inode *inode, struct kstat *st,
21613+ unsigned int nlink)
21614+{
21615+ unsigned int n;
21616+
21617+ inode->i_mode = st->mode;
21618+ /* don't i_[ug]id_write() here */
21619+ inode->i_uid = st->uid;
21620+ inode->i_gid = st->gid;
21621+ inode->i_atime = st->atime;
21622+ inode->i_mtime = st->mtime;
21623+ inode->i_ctime = st->ctime;
21624+
21625+ au_cpup_attr_nlink(inode, /*force*/0);
21626+ if (S_ISDIR(inode->i_mode)) {
21627+ n = inode->i_nlink;
21628+ n -= nlink;
21629+ n += st->nlink;
21630+ smp_mb(); /* for i_nlink */
21631+ /* 0 can happen */
21632+ set_nlink(inode, n);
21633+ }
21634+
21635+ spin_lock(&inode->i_lock);
21636+ inode->i_blocks = st->blocks;
21637+ i_size_write(inode, st->size);
21638+ spin_unlock(&inode->i_lock);
21639+}
21640+
21641+/*
21642+ * common routine for aufs_getattr() and au_getxattr().
21643+ * returns zero or negative (an error).
21644+ * @dentry will be read-locked in success.
21645+ */
21646+int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
21647+ struct path *h_path, int locked)
21648+{
21649+ int err;
21650+ unsigned int mnt_flags, sigen;
21651+ unsigned char udba_none;
21652+ aufs_bindex_t bindex;
21653+ struct super_block *sb, *h_sb;
21654+
21655+ h_path->mnt = NULL;
21656+ h_path->dentry = NULL;
21657+
21658+ err = 0;
21659+ sb = dentry->d_sb;
21660+ mnt_flags = au_mntflags(sb);
21661+ udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
21662+
21663+ if (unlikely(locked))
21664+ goto body; /* skip locking dinfo */
21665+
21666+ /* support fstat(2) */
21667+ if (!d_unlinked(dentry) && !udba_none) {
21668+ sigen = au_sigen(sb);
21669+ err = au_digen_test(dentry, sigen);
21670+ if (!err) {
21671+ di_read_lock_child(dentry, AuLock_IR);
21672+ err = au_dbrange_test(dentry);
21673+ if (unlikely(err)) {
21674+ di_read_unlock(dentry, AuLock_IR);
21675+ goto out;
21676+ }
21677+ } else {
21678+ AuDebugOn(IS_ROOT(dentry));
21679+ di_write_lock_child(dentry);
21680+ err = au_dbrange_test(dentry);
21681+ if (!err)
21682+ err = au_reval_for_attr(dentry, sigen);
21683+ if (!err)
21684+ di_downgrade_lock(dentry, AuLock_IR);
21685+ else {
21686+ di_write_unlock(dentry);
21687+ goto out;
21688+ }
21689+ }
21690+ } else
21691+ di_read_lock_child(dentry, AuLock_IR);
21692+
21693+body:
21694+ if (!inode) {
21695+ inode = d_inode(dentry);
21696+ if (unlikely(!inode))
21697+ goto out;
21698+ }
21699+ bindex = au_ibtop(inode);
21700+ h_path->mnt = au_sbr_mnt(sb, bindex);
21701+ h_sb = h_path->mnt->mnt_sb;
21702+ if (!force
21703+ && !au_test_fs_bad_iattr(h_sb)
21704+ && udba_none)
21705+ goto out; /* success */
21706+
21707+ if (au_dbtop(dentry) == bindex)
21708+ h_path->dentry = au_h_dptr(dentry, bindex);
21709+ else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
21710+ h_path->dentry = au_plink_lkup(inode, bindex);
21711+ if (IS_ERR(h_path->dentry))
21712+ /* pretending success */
21713+ h_path->dentry = NULL;
21714+ else
21715+ dput(h_path->dentry);
21716+ }
21717+
21718+out:
21719+ return err;
21720+}
21721+
21722+static int aufs_getattr(struct user_namespace *userns, const struct path *path,
21723+ struct kstat *st, u32 request, unsigned int query)
21724+{
21725+ int err;
21726+ unsigned char positive;
21727+ struct path h_path;
21728+ struct dentry *dentry;
21729+ struct inode *inode;
21730+ struct super_block *sb;
21731+
21732+ dentry = path->dentry;
21733+ inode = d_inode(dentry);
21734+ sb = dentry->d_sb;
21735+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21736+ if (unlikely(err))
21737+ goto out;
21738+ err = au_h_path_getattr(dentry, /*inode*/NULL, /*force*/0, &h_path,
21739+ /*locked*/0);
21740+ if (unlikely(err))
21741+ goto out_si;
21742+ if (unlikely(!h_path.dentry))
21743+ /* illegally overlapped or something */
21744+ goto out_fill; /* pretending success */
21745+
21746+ positive = d_is_positive(h_path.dentry);
21747+ if (positive)
21748+ /* no vfsub version */
21749+ err = vfs_getattr(&h_path, st, request, query);
21750+ if (!err) {
21751+ if (positive)
21752+ au_refresh_iattr(inode, st,
21753+ d_inode(h_path.dentry)->i_nlink);
21754+ goto out_fill; /* success */
21755+ }
21756+ AuTraceErr(err);
21757+ goto out_di;
21758+
21759+out_fill:
21760+ generic_fillattr(userns, inode, st);
21761+out_di:
21762+ di_read_unlock(dentry, AuLock_IR);
21763+out_si:
21764+ si_read_unlock(sb);
21765+out:
21766+ AuTraceErr(err);
21767+ return err;
21768+}
21769+
21770+/* ---------------------------------------------------------------------- */
21771+
21772+static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
21773+ struct delayed_call *done)
21774+{
21775+ const char *ret;
21776+ struct dentry *h_dentry;
21777+ struct inode *h_inode;
21778+ int err;
21779+ aufs_bindex_t bindex;
21780+
21781+ ret = NULL; /* suppress a warning */
21782+ err = -ECHILD;
21783+ if (!dentry)
21784+ goto out;
21785+
21786+ err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
21787+ if (unlikely(err))
21788+ goto out;
21789+
21790+ err = au_d_hashed_positive(dentry);
21791+ if (unlikely(err))
21792+ goto out_unlock;
21793+
21794+ err = -EINVAL;
21795+ inode = d_inode(dentry);
21796+ bindex = au_ibtop(inode);
21797+ h_inode = au_h_iptr(inode, bindex);
21798+ if (unlikely(!h_inode->i_op->get_link))
21799+ goto out_unlock;
21800+
21801+ err = -EBUSY;
21802+ h_dentry = NULL;
21803+ if (au_dbtop(dentry) <= bindex) {
21804+ h_dentry = au_h_dptr(dentry, bindex);
21805+ if (h_dentry)
21806+ dget(h_dentry);
21807+ }
21808+ if (!h_dentry) {
21809+ h_dentry = d_find_any_alias(h_inode);
21810+ if (IS_ERR(h_dentry)) {
21811+ err = PTR_ERR(h_dentry);
21812+ goto out_unlock;
21813+ }
21814+ }
21815+ if (unlikely(!h_dentry))
21816+ goto out_unlock;
21817+
21818+ err = 0;
21819+ AuDbg("%ps\n", h_inode->i_op->get_link);
21820+ AuDbgDentry(h_dentry);
21821+ ret = vfs_get_link(h_dentry, done);
21822+ dput(h_dentry);
21823+ if (IS_ERR(ret))
21824+ err = PTR_ERR(ret);
21825+
21826+out_unlock:
21827+ aufs_read_unlock(dentry, AuLock_IR);
21828+out:
21829+ if (unlikely(err))
21830+ ret = ERR_PTR(err);
21831+ AuTraceErrPtr(ret);
21832+ return ret;
21833+}
21834+
21835+/* ---------------------------------------------------------------------- */
21836+
21837+static int au_is_special(struct inode *inode)
21838+{
21839+ return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
21840+}
21841+
21842+static int aufs_update_time(struct inode *inode, struct timespec64 *ts,
21843+ int flags)
21844+{
21845+ int err;
21846+ aufs_bindex_t bindex;
21847+ struct super_block *sb;
21848+ struct inode *h_inode;
21849+ struct vfsmount *h_mnt;
21850+
21851+ sb = inode->i_sb;
21852+ WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
21853+ "unexpected s_flags 0x%lx", sb->s_flags);
21854+
21855+ /* mmap_sem might be acquired already, cf. aufs_mmap() */
21856+ lockdep_off();
21857+ si_read_lock(sb, AuLock_FLUSH);
21858+ ii_write_lock_child(inode);
21859+
21860+ err = 0;
21861+ bindex = au_ibtop(inode);
21862+ h_inode = au_h_iptr(inode, bindex);
21863+ if (!au_test_ro(sb, bindex, inode)) {
21864+ h_mnt = au_sbr_mnt(sb, bindex);
21865+ err = vfsub_mnt_want_write(h_mnt);
21866+ if (!err) {
21867+ err = vfsub_update_time(h_inode, ts, flags);
21868+ vfsub_mnt_drop_write(h_mnt);
21869+ }
21870+ } else if (au_is_special(h_inode)) {
21871+ /*
21872+ * Never copy-up here.
21873+ * These special files may already be opened and used for
21874+ * communicating. If we copied it up, then the communication
21875+ * would be corrupted.
21876+ */
21877+ AuWarn1("timestamps for i%lu are ignored "
21878+ "since it is on readonly branch (hi%lu).\n",
21879+ inode->i_ino, h_inode->i_ino);
21880+ } else if (flags & ~S_ATIME) {
21881+ err = -EIO;
21882+ AuIOErr1("unexpected flags 0x%x\n", flags);
21883+ AuDebugOn(1);
21884+ }
21885+
21886+ if (!err)
21887+ au_cpup_attr_timesizes(inode);
21888+ ii_write_unlock(inode);
21889+ si_read_unlock(sb);
21890+ lockdep_on();
21891+
21892+ if (!err && (flags & S_VERSION))
21893+ inode_inc_iversion(inode);
21894+
21895+ return err;
21896+}
21897+
21898+/* ---------------------------------------------------------------------- */
21899+
21900+/* no getattr version will be set by module.c:aufs_init() */
21901+struct inode_operations aufs_iop_nogetattr[AuIop_Last],
21902+ aufs_iop[] = {
21903+ [AuIop_SYMLINK] = {
21904+ .permission = aufs_permission,
21905+#ifdef CONFIG_FS_POSIX_ACL
21906+ .get_acl = aufs_get_acl,
21907+ .set_acl = aufs_set_acl, /* unsupport for symlink? */
21908+#endif
21909+
21910+ .setattr = aufs_setattr,
21911+ .getattr = aufs_getattr,
21912+
21913+#ifdef CONFIG_AUFS_XATTR
21914+ .listxattr = aufs_listxattr,
21915+#endif
21916+
21917+ .get_link = aufs_get_link,
21918+
21919+ /* .update_time = aufs_update_time */
21920+ },
21921+ [AuIop_DIR] = {
21922+ .create = aufs_create,
21923+ .lookup = aufs_lookup,
21924+ .link = aufs_link,
21925+ .unlink = aufs_unlink,
21926+ .symlink = aufs_symlink,
21927+ .mkdir = aufs_mkdir,
21928+ .rmdir = aufs_rmdir,
21929+ .mknod = aufs_mknod,
21930+ .rename = aufs_rename,
21931+
21932+ .permission = aufs_permission,
21933+#ifdef CONFIG_FS_POSIX_ACL
21934+ .get_acl = aufs_get_acl,
21935+ .set_acl = aufs_set_acl,
21936+#endif
21937+
21938+ .setattr = aufs_setattr,
21939+ .getattr = aufs_getattr,
21940+
21941+#ifdef CONFIG_AUFS_XATTR
21942+ .listxattr = aufs_listxattr,
21943+#endif
21944+
21945+ .update_time = aufs_update_time,
21946+ .atomic_open = aufs_atomic_open,
21947+ .tmpfile = aufs_tmpfile
21948+ },
21949+ [AuIop_OTHER] = {
21950+ .permission = aufs_permission,
21951+#ifdef CONFIG_FS_POSIX_ACL
21952+ .get_acl = aufs_get_acl,
21953+ .set_acl = aufs_set_acl,
21954+#endif
21955+
21956+ .setattr = aufs_setattr,
21957+ .getattr = aufs_getattr,
21958+
21959+#ifdef CONFIG_AUFS_XATTR
21960+ .listxattr = aufs_listxattr,
21961+#endif
21962+
21963+ .update_time = aufs_update_time
21964+ }
21965+};
21966diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
21967--- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
21968+++ linux/fs/aufs/i_op_del.c 2021-12-03 15:40:58.236647297 +0100
21969@@ -0,0 +1,522 @@
21970+// SPDX-License-Identifier: GPL-2.0
21971+/*
21972+ * Copyright (C) 2005-2021 Junjiro R. Okajima
21973+ *
21974+ * This program, aufs is free software; you can redistribute it and/or modify
21975+ * it under the terms of the GNU General Public License as published by
21976+ * the Free Software Foundation; either version 2 of the License, or
21977+ * (at your option) any later version.
21978+ *
21979+ * This program is distributed in the hope that it will be useful,
21980+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
21981+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21982+ * GNU General Public License for more details.
21983+ *
21984+ * You should have received a copy of the GNU General Public License
21985+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
21986+ */
21987+
21988+/*
21989+ * inode operations (del entry)
21990+ */
21991+
21992+#include <linux/iversion.h>
21993+#include "aufs.h"
21994+
21995+/*
21996+ * decide if a new whiteout for @dentry is necessary or not.
21997+ * when it is necessary, prepare the parent dir for the upper branch whose
21998+ * branch index is @bcpup for creation. the actual creation of the whiteout will
21999+ * be done by caller.
22000+ * return value:
22001+ * 0: wh is unnecessary
22002+ * plus: wh is necessary
22003+ * minus: error
22004+ */
22005+int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
22006+{
22007+ int need_wh, err;
22008+ aufs_bindex_t btop;
22009+ struct super_block *sb;
22010+
22011+ sb = dentry->d_sb;
22012+ btop = au_dbtop(dentry);
22013+ if (*bcpup < 0) {
22014+ *bcpup = btop;
22015+ if (au_test_ro(sb, btop, d_inode(dentry))) {
22016+ err = AuWbrCopyup(au_sbi(sb), dentry);
22017+ *bcpup = err;
22018+ if (unlikely(err < 0))
22019+ goto out;
22020+ }
22021+ } else
22022+ AuDebugOn(btop < *bcpup
22023+ || au_test_ro(sb, *bcpup, d_inode(dentry)));
22024+ AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
22025+
22026+ if (*bcpup != btop) {
22027+ err = au_cpup_dirs(dentry, *bcpup);
22028+ if (unlikely(err))
22029+ goto out;
22030+ need_wh = 1;
22031+ } else {
22032+ struct au_dinfo *dinfo, *tmp;
22033+
22034+ need_wh = -ENOMEM;
22035+ dinfo = au_di(dentry);
22036+ tmp = au_di_alloc(sb, AuLsc_DI_TMP);
22037+ if (tmp) {
22038+ au_di_cp(tmp, dinfo);
22039+ au_di_swap(tmp, dinfo);
22040+ /* returns the number of positive dentries */
22041+ need_wh = au_lkup_dentry(dentry, btop + 1,
22042+ /* AuLkup_IGNORE_PERM */ 0);
22043+ au_di_swap(tmp, dinfo);
22044+ au_rw_write_unlock(&tmp->di_rwsem);
22045+ au_di_free(tmp);
22046+ }
22047+ }
22048+ AuDbg("need_wh %d\n", need_wh);
22049+ err = need_wh;
22050+
22051+out:
22052+ return err;
22053+}
22054+
22055+/*
22056+ * simple tests for the del-entry operations.
22057+ * following the checks in vfs, plus the parent-child relationship.
22058+ */
22059+int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
22060+ struct dentry *h_parent, int isdir)
22061+{
22062+ int err;
22063+ umode_t h_mode;
22064+ struct dentry *h_dentry, *h_latest;
22065+ struct inode *h_inode;
22066+ struct path h_ppath;
22067+ struct super_block *sb;
22068+ struct au_branch *br;
22069+ struct user_namespace *h_userns;
22070+
22071+ h_dentry = au_h_dptr(dentry, bindex);
22072+ if (d_really_is_positive(dentry)) {
22073+ err = -ENOENT;
22074+ if (unlikely(d_is_negative(h_dentry)))
22075+ goto out;
22076+ h_inode = d_inode(h_dentry);
22077+ if (unlikely(!h_inode->i_nlink))
22078+ goto out;
22079+
22080+ h_mode = h_inode->i_mode;
22081+ if (!isdir) {
22082+ err = -EISDIR;
22083+ if (unlikely(S_ISDIR(h_mode)))
22084+ goto out;
22085+ } else if (unlikely(!S_ISDIR(h_mode))) {
22086+ err = -ENOTDIR;
22087+ goto out;
22088+ }
22089+ } else {
22090+ /* rename(2) case */
22091+ err = -EIO;
22092+ if (unlikely(d_is_positive(h_dentry)))
22093+ goto out;
22094+ }
22095+
22096+ err = -ENOENT;
22097+ /* expected parent dir is locked */
22098+ if (unlikely(h_parent != h_dentry->d_parent))
22099+ goto out;
22100+ err = 0;
22101+
22102+ /*
22103+ * rmdir a dir may break the consistency on some filesystem.
22104+ * let's try heavy test.
22105+ */
22106+ err = -EACCES;
22107+ sb = dentry->d_sb;
22108+ br = au_sbr(sb, bindex);
22109+ h_userns = au_br_userns(br);
22110+ if (unlikely(!au_opt_test(au_mntflags(sb), DIRPERM1)
22111+ && au_test_h_perm(h_userns, d_inode(h_parent),
22112+ MAY_EXEC | MAY_WRITE)))
22113+ goto out;
22114+
22115+ h_ppath.dentry = h_parent;
22116+ h_ppath.mnt = au_br_mnt(br);
22117+ h_latest = au_sio_lkup_one(h_userns, &dentry->d_name, &h_ppath);
22118+ err = -EIO;
22119+ if (IS_ERR(h_latest))
22120+ goto out;
22121+ if (h_latest == h_dentry)
22122+ err = 0;
22123+ dput(h_latest);
22124+
22125+out:
22126+ return err;
22127+}
22128+
22129+/*
22130+ * decide the branch where we operate for @dentry. the branch index will be set
22131+ * @rbcpup. after deciding it, 'pin' it and store the timestamps of the parent
22132+ * dir for reverting.
22133+ * when a new whiteout is necessary, create it.
22134+ */
22135+static struct dentry*
22136+lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
22137+ struct au_dtime *dt, struct au_pin *pin)
22138+{
22139+ struct dentry *wh_dentry;
22140+ struct super_block *sb;
22141+ struct path h_path;
22142+ int err, need_wh;
22143+ unsigned int udba;
22144+ aufs_bindex_t bcpup;
22145+
22146+ need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
22147+ wh_dentry = ERR_PTR(need_wh);
22148+ if (unlikely(need_wh < 0))
22149+ goto out;
22150+
22151+ sb = dentry->d_sb;
22152+ udba = au_opt_udba(sb);
22153+ bcpup = *rbcpup;
22154+ err = au_pin(pin, dentry, bcpup, udba,
22155+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
22156+ wh_dentry = ERR_PTR(err);
22157+ if (unlikely(err))
22158+ goto out;
22159+
22160+ h_path.dentry = au_pinned_h_parent(pin);
22161+ if (udba != AuOpt_UDBA_NONE
22162+ && au_dbtop(dentry) == bcpup) {
22163+ err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
22164+ wh_dentry = ERR_PTR(err);
22165+ if (unlikely(err))
22166+ goto out_unpin;
22167+ }
22168+
22169+ h_path.mnt = au_sbr_mnt(sb, bcpup);
22170+ au_dtime_store(dt, au_pinned_parent(pin), &h_path);
22171+ wh_dentry = NULL;
22172+ if (!need_wh)
22173+ goto out; /* success, no need to create whiteout */
22174+
22175+ wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
22176+ if (IS_ERR(wh_dentry))
22177+ goto out_unpin;
22178+
22179+ /* returns with the parent is locked and wh_dentry is dget-ed */
22180+ goto out; /* success */
22181+
22182+out_unpin:
22183+ au_unpin(pin);
22184+out:
22185+ return wh_dentry;
22186+}
22187+
22188+/*
22189+ * when removing a dir, rename it to a unique temporary whiteout-ed name first
22190+ * in order to be revertible and save time for removing many child whiteouts
22191+ * under the dir.
22192+ * returns 1 when there are too many child whiteout and caller should remove
22193+ * them asynchronously. returns 0 when the number of children is enough small to
22194+ * remove now or the branch fs is a remote fs.
22195+ * otherwise return an error.
22196+ */
22197+static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
22198+ struct au_nhash *whlist, struct inode *dir)
22199+{
22200+ int rmdir_later, err, dirwh;
22201+ struct dentry *h_dentry;
22202+ struct super_block *sb;
22203+ struct inode *inode;
22204+
22205+ sb = dentry->d_sb;
22206+ SiMustAnyLock(sb);
22207+ h_dentry = au_h_dptr(dentry, bindex);
22208+ err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
22209+ if (unlikely(err))
22210+ goto out;
22211+
22212+ /* stop monitoring */
22213+ inode = d_inode(dentry);
22214+ au_hn_free(au_hi(inode, bindex));
22215+
22216+ if (!au_test_fs_remote(h_dentry->d_sb)) {
22217+ dirwh = au_sbi(sb)->si_dirwh;
22218+ rmdir_later = (dirwh <= 1);
22219+ if (!rmdir_later)
22220+ rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
22221+ dirwh);
22222+ if (rmdir_later)
22223+ return rmdir_later;
22224+ }
22225+
22226+ err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
22227+ if (unlikely(err)) {
22228+ AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
22229+ h_dentry, bindex, err);
22230+ err = 0;
22231+ }
22232+
22233+out:
22234+ AuTraceErr(err);
22235+ return err;
22236+}
22237+
22238+/*
22239+ * final procedure for deleting a entry.
22240+ * maintain dentry and iattr.
22241+ */
22242+static void epilog(struct inode *dir, struct dentry *dentry,
22243+ aufs_bindex_t bindex)
22244+{
22245+ struct inode *inode;
22246+
22247+ inode = d_inode(dentry);
22248+ d_drop(dentry);
22249+ inode->i_ctime = dir->i_ctime;
22250+
22251+ au_dir_ts(dir, bindex);
22252+ inode_inc_iversion(dir);
22253+}
22254+
22255+/*
22256+ * when an error happened, remove the created whiteout and revert everything.
22257+ */
22258+static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
22259+ aufs_bindex_t bwh, struct dentry *wh_dentry,
22260+ struct dentry *dentry, struct au_dtime *dt)
22261+{
22262+ int rerr;
22263+ struct path h_path = {
22264+ .dentry = wh_dentry,
22265+ .mnt = au_sbr_mnt(dir->i_sb, bindex)
22266+ };
22267+
22268+ rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
22269+ if (!rerr) {
22270+ au_set_dbwh(dentry, bwh);
22271+ au_dtime_revert(dt);
22272+ return 0;
22273+ }
22274+
22275+ AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
22276+ return -EIO;
22277+}
22278+
22279+/* ---------------------------------------------------------------------- */
22280+
22281+int aufs_unlink(struct inode *dir, struct dentry *dentry)
22282+{
22283+ int err;
22284+ aufs_bindex_t bwh, bindex, btop;
22285+ struct inode *inode, *h_dir, *delegated;
22286+ struct dentry *parent, *wh_dentry;
22287+ /* to reduce stack size */
22288+ struct {
22289+ struct au_dtime dt;
22290+ struct au_pin pin;
22291+ struct path h_path;
22292+ } *a;
22293+
22294+ IMustLock(dir);
22295+
22296+ err = -ENOMEM;
22297+ a = kmalloc(sizeof(*a), GFP_NOFS);
22298+ if (unlikely(!a))
22299+ goto out;
22300+
22301+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
22302+ if (unlikely(err))
22303+ goto out_free;
22304+ err = au_d_hashed_positive(dentry);
22305+ if (unlikely(err))
22306+ goto out_unlock;
22307+ inode = d_inode(dentry);
22308+ IMustLock(inode);
22309+ err = -EISDIR;
22310+ if (unlikely(d_is_dir(dentry)))
22311+ goto out_unlock; /* possible? */
22312+
22313+ btop = au_dbtop(dentry);
22314+ bwh = au_dbwh(dentry);
22315+ bindex = -1;
22316+ parent = dentry->d_parent; /* dir inode is locked */
22317+ di_write_lock_parent(parent);
22318+ wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
22319+ &a->pin);
22320+ err = PTR_ERR(wh_dentry);
22321+ if (IS_ERR(wh_dentry))
22322+ goto out_parent;
22323+
22324+ a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
22325+ a->h_path.dentry = au_h_dptr(dentry, btop);
22326+ dget(a->h_path.dentry);
22327+ if (bindex == btop) {
22328+ h_dir = au_pinned_h_dir(&a->pin);
22329+ delegated = NULL;
22330+ err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
22331+ if (unlikely(err == -EWOULDBLOCK)) {
22332+ pr_warn("cannot retry for NFSv4 delegation"
22333+ " for an internal unlink\n");
22334+ iput(delegated);
22335+ }
22336+ } else {
22337+ /* dir inode is locked */
22338+ h_dir = d_inode(wh_dentry->d_parent);
22339+ IMustLock(h_dir);
22340+ err = 0;
22341+ }
22342+
22343+ if (!err) {
22344+ vfsub_drop_nlink(inode);
22345+ epilog(dir, dentry, bindex);
22346+
22347+ /* update target timestamps */
22348+ if (bindex == btop) {
22349+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
22350+ /*ignore*/
22351+ inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
22352+ } else
22353+ /* todo: this timestamp may be reverted later */
22354+ inode->i_ctime = h_dir->i_ctime;
22355+ goto out_unpin; /* success */
22356+ }
22357+
22358+ /* revert */
22359+ if (wh_dentry) {
22360+ int rerr;
22361+
22362+ rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22363+ &a->dt);
22364+ if (rerr)
22365+ err = rerr;
22366+ }
22367+
22368+out_unpin:
22369+ au_unpin(&a->pin);
22370+ dput(wh_dentry);
22371+ dput(a->h_path.dentry);
22372+out_parent:
22373+ di_write_unlock(parent);
22374+out_unlock:
22375+ aufs_read_unlock(dentry, AuLock_DW);
22376+out_free:
22377+ au_kfree_rcu(a);
22378+out:
22379+ return err;
22380+}
22381+
22382+int aufs_rmdir(struct inode *dir, struct dentry *dentry)
22383+{
22384+ int err, rmdir_later;
22385+ aufs_bindex_t bwh, bindex, btop;
22386+ struct inode *inode;
22387+ struct dentry *parent, *wh_dentry, *h_dentry;
22388+ struct au_whtmp_rmdir *args;
22389+ /* to reduce stack size */
22390+ struct {
22391+ struct au_dtime dt;
22392+ struct au_pin pin;
22393+ } *a;
22394+
22395+ IMustLock(dir);
22396+
22397+ err = -ENOMEM;
22398+ a = kmalloc(sizeof(*a), GFP_NOFS);
22399+ if (unlikely(!a))
22400+ goto out;
22401+
22402+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
22403+ if (unlikely(err))
22404+ goto out_free;
22405+ err = au_alive_dir(dentry);
22406+ if (unlikely(err))
22407+ goto out_unlock;
22408+ inode = d_inode(dentry);
22409+ IMustLock(inode);
22410+ err = -ENOTDIR;
22411+ if (unlikely(!d_is_dir(dentry)))
22412+ goto out_unlock; /* possible? */
22413+
22414+ err = -ENOMEM;
22415+ args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
22416+ if (unlikely(!args))
22417+ goto out_unlock;
22418+
22419+ parent = dentry->d_parent; /* dir inode is locked */
22420+ di_write_lock_parent(parent);
22421+ err = au_test_empty(dentry, &args->whlist);
22422+ if (unlikely(err))
22423+ goto out_parent;
22424+
22425+ btop = au_dbtop(dentry);
22426+ bwh = au_dbwh(dentry);
22427+ bindex = -1;
22428+ wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
22429+ &a->pin);
22430+ err = PTR_ERR(wh_dentry);
22431+ if (IS_ERR(wh_dentry))
22432+ goto out_parent;
22433+
22434+ h_dentry = au_h_dptr(dentry, btop);
22435+ dget(h_dentry);
22436+ rmdir_later = 0;
22437+ if (bindex == btop) {
22438+ err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
22439+ if (err > 0) {
22440+ rmdir_later = err;
22441+ err = 0;
22442+ }
22443+ } else {
22444+ /* stop monitoring */
22445+ au_hn_free(au_hi(inode, btop));
22446+
22447+ /* dir inode is locked */
22448+ IMustLock(d_inode(wh_dentry->d_parent));
22449+ err = 0;
22450+ }
22451+
22452+ if (!err) {
22453+ vfsub_dead_dir(inode);
22454+ au_set_dbdiropq(dentry, -1);
22455+ epilog(dir, dentry, bindex);
22456+
22457+ if (rmdir_later) {
22458+ au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
22459+ args = NULL;
22460+ }
22461+
22462+ goto out_unpin; /* success */
22463+ }
22464+
22465+ /* revert */
22466+ AuLabel(revert);
22467+ if (wh_dentry) {
22468+ int rerr;
22469+
22470+ rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22471+ &a->dt);
22472+ if (rerr)
22473+ err = rerr;
22474+ }
22475+
22476+out_unpin:
22477+ au_unpin(&a->pin);
22478+ dput(wh_dentry);
22479+ dput(h_dentry);
22480+out_parent:
22481+ di_write_unlock(parent);
22482+ if (args)
22483+ au_whtmp_rmdir_free(args);
22484+out_unlock:
22485+ aufs_read_unlock(dentry, AuLock_DW);
22486+out_free:
22487+ au_kfree_rcu(a);
22488+out:
22489+ AuTraceErr(err);
22490+ return err;
22491+}
22492diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
22493--- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
22494+++ linux/fs/aufs/i_op_ren.c 2021-12-03 15:40:58.236647297 +0100
22495@@ -0,0 +1,1257 @@
22496+// SPDX-License-Identifier: GPL-2.0
22497+/*
22498+ * Copyright (C) 2005-2021 Junjiro R. Okajima
22499+ *
22500+ * This program, aufs is free software; you can redistribute it and/or modify
22501+ * it under the terms of the GNU General Public License as published by
22502+ * the Free Software Foundation; either version 2 of the License, or
22503+ * (at your option) any later version.
22504+ *
22505+ * This program is distributed in the hope that it will be useful,
22506+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
22507+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22508+ * GNU General Public License for more details.
22509+ *
22510+ * You should have received a copy of the GNU General Public License
22511+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
22512+ */
22513+
22514+/*
22515+ * inode operation (rename entry)
22516+ * todo: this is crazy monster
22517+ */
22518+
22519+#include <linux/iversion.h>
22520+#include "aufs.h"
22521+
22522+enum { AuSRC, AuDST, AuSrcDst };
22523+enum { AuPARENT, AuCHILD, AuParentChild };
22524+
22525+#define AuRen_ISDIR_SRC 1
22526+#define AuRen_ISDIR_DST (1 << 1)
22527+#define AuRen_ISSAMEDIR (1 << 2)
22528+#define AuRen_WHSRC (1 << 3)
22529+#define AuRen_WHDST (1 << 4)
22530+#define AuRen_MNT_WRITE (1 << 5)
22531+#define AuRen_DT_DSTDIR (1 << 6)
22532+#define AuRen_DIROPQ_SRC (1 << 7)
22533+#define AuRen_DIROPQ_DST (1 << 8)
22534+#define AuRen_DIRREN (1 << 9)
22535+#define AuRen_DROPPED_SRC (1 << 10)
22536+#define AuRen_DROPPED_DST (1 << 11)
22537+#define au_ftest_ren(flags, name) ((flags) & AuRen_##name)
22538+#define au_fset_ren(flags, name) \
22539+ do { (flags) |= AuRen_##name; } while (0)
22540+#define au_fclr_ren(flags, name) \
22541+ do { (flags) &= ~AuRen_##name; } while (0)
22542+
22543+#ifndef CONFIG_AUFS_DIRREN
22544+#undef AuRen_DIRREN
22545+#define AuRen_DIRREN 0
22546+#endif
22547+
22548+struct au_ren_args {
22549+ struct {
22550+ struct dentry *dentry, *h_dentry, *parent, *h_parent,
22551+ *wh_dentry;
22552+ struct inode *dir, *inode;
22553+ struct au_hinode *hdir, *hinode;
22554+ struct au_dtime dt[AuParentChild];
22555+ aufs_bindex_t btop, bdiropq;
22556+ } sd[AuSrcDst];
22557+
22558+#define src_dentry sd[AuSRC].dentry
22559+#define src_dir sd[AuSRC].dir
22560+#define src_inode sd[AuSRC].inode
22561+#define src_h_dentry sd[AuSRC].h_dentry
22562+#define src_parent sd[AuSRC].parent
22563+#define src_h_parent sd[AuSRC].h_parent
22564+#define src_wh_dentry sd[AuSRC].wh_dentry
22565+#define src_hdir sd[AuSRC].hdir
22566+#define src_hinode sd[AuSRC].hinode
22567+#define src_h_dir sd[AuSRC].hdir->hi_inode
22568+#define src_dt sd[AuSRC].dt
22569+#define src_btop sd[AuSRC].btop
22570+#define src_bdiropq sd[AuSRC].bdiropq
22571+
22572+#define dst_dentry sd[AuDST].dentry
22573+#define dst_dir sd[AuDST].dir
22574+#define dst_inode sd[AuDST].inode
22575+#define dst_h_dentry sd[AuDST].h_dentry
22576+#define dst_parent sd[AuDST].parent
22577+#define dst_h_parent sd[AuDST].h_parent
22578+#define dst_wh_dentry sd[AuDST].wh_dentry
22579+#define dst_hdir sd[AuDST].hdir
22580+#define dst_hinode sd[AuDST].hinode
22581+#define dst_h_dir sd[AuDST].hdir->hi_inode
22582+#define dst_dt sd[AuDST].dt
22583+#define dst_btop sd[AuDST].btop
22584+#define dst_bdiropq sd[AuDST].bdiropq
22585+
22586+ struct dentry *h_trap;
22587+ struct au_branch *br;
22588+ struct path h_path;
22589+ struct au_nhash whlist;
22590+ aufs_bindex_t btgt, src_bwh;
22591+
22592+ struct {
22593+ unsigned short auren_flags;
22594+ unsigned char flags; /* syscall parameter */
22595+ unsigned char exchange;
22596+ } __packed;
22597+
22598+ struct au_whtmp_rmdir *thargs;
22599+ struct dentry *h_dst;
22600+ struct au_hinode *h_root;
22601+};
22602+
22603+/* ---------------------------------------------------------------------- */
22604+
22605+/*
22606+ * functions for reverting.
22607+ * when an error happened in a single rename systemcall, we should revert
22608+ * everything as if nothing happened.
22609+ * we don't need to revert the copied-up/down the parent dir since they are
22610+ * harmless.
22611+ */
22612+
22613+#define RevertFailure(fmt, ...) do { \
22614+ AuIOErr("revert failure: " fmt " (%d, %d)\n", \
22615+ ##__VA_ARGS__, err, rerr); \
22616+ err = -EIO; \
22617+} while (0)
22618+
22619+static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
22620+{
22621+ int rerr;
22622+ struct dentry *d;
22623+#define src_or_dst(member) a->sd[idx].member
22624+
22625+ d = src_or_dst(dentry); /* {src,dst}_dentry */
22626+ au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22627+ rerr = au_diropq_remove(d, a->btgt);
22628+ au_hn_inode_unlock(src_or_dst(hinode));
22629+ au_set_dbdiropq(d, src_or_dst(bdiropq));
22630+ if (rerr)
22631+ RevertFailure("remove diropq %pd", d);
22632+
22633+#undef src_or_dst_
22634+}
22635+
22636+static void au_ren_rev_diropq(int err, struct au_ren_args *a)
22637+{
22638+ if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
22639+ au_ren_do_rev_diropq(err, a, AuSRC);
22640+ if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
22641+ au_ren_do_rev_diropq(err, a, AuDST);
22642+}
22643+
22644+static void au_ren_rev_rename(int err, struct au_ren_args *a)
22645+{
22646+ int rerr;
22647+ struct inode *delegated;
22648+ struct path h_ppath = {
22649+ .dentry = a->src_h_parent,
22650+ .mnt = a->h_path.mnt
22651+ };
22652+
22653+ a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name, &h_ppath);
22654+ rerr = PTR_ERR(a->h_path.dentry);
22655+ if (IS_ERR(a->h_path.dentry)) {
22656+ RevertFailure("lkup one %pd", a->src_dentry);
22657+ return;
22658+ }
22659+
22660+ delegated = NULL;
22661+ rerr = vfsub_rename(a->dst_h_dir,
22662+ au_h_dptr(a->src_dentry, a->btgt),
22663+ a->src_h_dir, &a->h_path, &delegated, a->flags);
22664+ if (unlikely(rerr == -EWOULDBLOCK)) {
22665+ pr_warn("cannot retry for NFSv4 delegation"
22666+ " for an internal rename\n");
22667+ iput(delegated);
22668+ }
22669+ d_drop(a->h_path.dentry);
22670+ dput(a->h_path.dentry);
22671+ /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
22672+ if (rerr)
22673+ RevertFailure("rename %pd", a->src_dentry);
22674+}
22675+
22676+static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
22677+{
22678+ int rerr;
22679+ struct inode *delegated;
22680+ struct path h_ppath = {
22681+ .dentry = a->dst_h_parent,
22682+ .mnt = a->h_path.mnt
22683+ };
22684+
22685+ a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name, &h_ppath);
22686+ rerr = PTR_ERR(a->h_path.dentry);
22687+ if (IS_ERR(a->h_path.dentry)) {
22688+ RevertFailure("lkup one %pd", a->dst_dentry);
22689+ return;
22690+ }
22691+ if (d_is_positive(a->h_path.dentry)) {
22692+ d_drop(a->h_path.dentry);
22693+ dput(a->h_path.dentry);
22694+ return;
22695+ }
22696+
22697+ delegated = NULL;
22698+ rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
22699+ &delegated, a->flags);
22700+ if (unlikely(rerr == -EWOULDBLOCK)) {
22701+ pr_warn("cannot retry for NFSv4 delegation"
22702+ " for an internal rename\n");
22703+ iput(delegated);
22704+ }
22705+ d_drop(a->h_path.dentry);
22706+ dput(a->h_path.dentry);
22707+ if (!rerr)
22708+ au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
22709+ else
22710+ RevertFailure("rename %pd", a->h_dst);
22711+}
22712+
22713+static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
22714+{
22715+ int rerr;
22716+
22717+ a->h_path.dentry = a->src_wh_dentry;
22718+ rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
22719+ au_set_dbwh(a->src_dentry, a->src_bwh);
22720+ if (rerr)
22721+ RevertFailure("unlink %pd", a->src_wh_dentry);
22722+}
22723+#undef RevertFailure
22724+
22725+/* ---------------------------------------------------------------------- */
22726+
22727+/*
22728+ * when we have to copyup the renaming entry, do it with the rename-target name
22729+ * in order to minimize the cost (the later actual rename is unnecessary).
22730+ * otherwise rename it on the target branch.
22731+ */
22732+static int au_ren_or_cpup(struct au_ren_args *a)
22733+{
22734+ int err;
22735+ struct dentry *d;
22736+ struct inode *delegated;
22737+
22738+ d = a->src_dentry;
22739+ if (au_dbtop(d) == a->btgt) {
22740+ a->h_path.dentry = a->dst_h_dentry;
22741+ AuDebugOn(au_dbtop(d) != a->btgt);
22742+ delegated = NULL;
22743+ err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
22744+ a->dst_h_dir, &a->h_path, &delegated,
22745+ a->flags);
22746+ if (unlikely(err == -EWOULDBLOCK)) {
22747+ pr_warn("cannot retry for NFSv4 delegation"
22748+ " for an internal rename\n");
22749+ iput(delegated);
22750+ }
22751+ } else
22752+ BUG();
22753+
22754+ if (!err && a->h_dst)
22755+ /* it will be set to dinfo later */
22756+ dget(a->h_dst);
22757+
22758+ return err;
22759+}
22760+
22761+/* cf. aufs_rmdir() */
22762+static int au_ren_del_whtmp(struct au_ren_args *a)
22763+{
22764+ int err;
22765+ struct inode *dir;
22766+
22767+ dir = a->dst_dir;
22768+ SiMustAnyLock(dir->i_sb);
22769+ if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
22770+ au_sbi(dir->i_sb)->si_dirwh)
22771+ || au_test_fs_remote(a->h_dst->d_sb)) {
22772+ err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
22773+ if (unlikely(err))
22774+ pr_warn("failed removing whtmp dir %pd (%d), "
22775+ "ignored.\n", a->h_dst, err);
22776+ } else {
22777+ au_nhash_wh_free(&a->thargs->whlist);
22778+ a->thargs->whlist = a->whlist;
22779+ a->whlist.nh_num = 0;
22780+ au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
22781+ dput(a->h_dst);
22782+ a->thargs = NULL;
22783+ }
22784+
22785+ return 0;
22786+}
22787+
22788+/* make it 'opaque' dir. */
22789+static int au_ren_do_diropq(struct au_ren_args *a, int idx)
22790+{
22791+ int err;
22792+ struct dentry *d, *diropq;
22793+#define src_or_dst(member) a->sd[idx].member
22794+
22795+ err = 0;
22796+ d = src_or_dst(dentry); /* {src,dst}_dentry */
22797+ src_or_dst(bdiropq) = au_dbdiropq(d);
22798+ src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
22799+ au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22800+ diropq = au_diropq_create(d, a->btgt);
22801+ au_hn_inode_unlock(src_or_dst(hinode));
22802+ if (IS_ERR(diropq))
22803+ err = PTR_ERR(diropq);
22804+ else
22805+ dput(diropq);
22806+
22807+#undef src_or_dst_
22808+ return err;
22809+}
22810+
22811+static int au_ren_diropq(struct au_ren_args *a)
22812+{
22813+ int err;
22814+ unsigned char always;
22815+ struct dentry *d;
22816+
22817+ err = 0;
22818+ d = a->dst_dentry; /* already renamed on the branch */
22819+ always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
22820+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
22821+ && !au_ftest_ren(a->auren_flags, DIRREN)
22822+ && a->btgt != au_dbdiropq(a->src_dentry)
22823+ && (a->dst_wh_dentry
22824+ || a->btgt <= au_dbdiropq(d)
22825+ /* hide the lower to keep xino */
22826+ /* the lowers may not be a dir, but we hide them anyway */
22827+ || a->btgt < au_dbbot(d)
22828+ || always)) {
22829+ AuDbg("here\n");
22830+ err = au_ren_do_diropq(a, AuSRC);
22831+ if (unlikely(err))
22832+ goto out;
22833+ au_fset_ren(a->auren_flags, DIROPQ_SRC);
22834+ }
22835+ if (!a->exchange)
22836+ goto out; /* success */
22837+
22838+ d = a->src_dentry; /* already renamed on the branch */
22839+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)
22840+ && a->btgt != au_dbdiropq(a->dst_dentry)
22841+ && (a->btgt < au_dbdiropq(d)
22842+ || a->btgt < au_dbbot(d)
22843+ || always)) {
22844+ AuDbgDentry(a->src_dentry);
22845+ AuDbgDentry(a->dst_dentry);
22846+ err = au_ren_do_diropq(a, AuDST);
22847+ if (unlikely(err))
22848+ goto out_rev_src;
22849+ au_fset_ren(a->auren_flags, DIROPQ_DST);
22850+ }
22851+ goto out; /* success */
22852+
22853+out_rev_src:
22854+ AuDbg("err %d, reverting src\n", err);
22855+ au_ren_rev_diropq(err, a);
22856+out:
22857+ return err;
22858+}
22859+
22860+static int do_rename(struct au_ren_args *a)
22861+{
22862+ int err;
22863+ struct dentry *d, *h_d;
22864+
22865+ if (!a->exchange) {
22866+ /* prepare workqueue args for asynchronous rmdir */
22867+ h_d = a->dst_h_dentry;
22868+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)
22869+ /* && !au_ftest_ren(a->auren_flags, DIRREN) */
22870+ && d_is_positive(h_d)) {
22871+ err = -ENOMEM;
22872+ a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
22873+ GFP_NOFS);
22874+ if (unlikely(!a->thargs))
22875+ goto out;
22876+ a->h_dst = dget(h_d);
22877+ }
22878+
22879+ /* create whiteout for src_dentry */
22880+ if (au_ftest_ren(a->auren_flags, WHSRC)) {
22881+ a->src_bwh = au_dbwh(a->src_dentry);
22882+ AuDebugOn(a->src_bwh >= 0);
22883+ a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
22884+ a->src_h_parent);
22885+ err = PTR_ERR(a->src_wh_dentry);
22886+ if (IS_ERR(a->src_wh_dentry))
22887+ goto out_thargs;
22888+ }
22889+
22890+ /* lookup whiteout for dentry */
22891+ if (au_ftest_ren(a->auren_flags, WHDST)) {
22892+ h_d = au_wh_lkup(a->dst_h_parent,
22893+ &a->dst_dentry->d_name, a->br);
22894+ err = PTR_ERR(h_d);
22895+ if (IS_ERR(h_d))
22896+ goto out_whsrc;
22897+ if (d_is_negative(h_d))
22898+ dput(h_d);
22899+ else
22900+ a->dst_wh_dentry = h_d;
22901+ }
22902+
22903+ /* rename dentry to tmpwh */
22904+ if (a->thargs) {
22905+ err = au_whtmp_ren(a->dst_h_dentry, a->br);
22906+ if (unlikely(err))
22907+ goto out_whdst;
22908+
22909+ d = a->dst_dentry;
22910+ au_set_h_dptr(d, a->btgt, NULL);
22911+ err = au_lkup_neg(d, a->btgt, /*wh*/0);
22912+ if (unlikely(err))
22913+ goto out_whtmp;
22914+ a->dst_h_dentry = au_h_dptr(d, a->btgt);
22915+ }
22916+ }
22917+
22918+ BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
22919+#if 0 /* debugging */
22920+ BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN)
22921+ && d_is_positive(a->dst_h_dentry)
22922+ && a->src_btop != a->btgt);
22923+#endif
22924+
22925+ /* rename by vfs_rename or cpup */
22926+ err = au_ren_or_cpup(a);
22927+ if (unlikely(err))
22928+ /* leave the copied-up one */
22929+ goto out_whtmp;
22930+
22931+ /* make dir opaque */
22932+ err = au_ren_diropq(a);
22933+ if (unlikely(err))
22934+ goto out_rename;
22935+
22936+ /* update target timestamps */
22937+ if (a->exchange) {
22938+ AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
22939+ a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
22940+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
22941+ a->dst_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
22942+ }
22943+ AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
22944+ a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
22945+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
22946+ a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
22947+
22948+ if (!a->exchange) {
22949+ /* remove whiteout for dentry */
22950+ if (a->dst_wh_dentry) {
22951+ a->h_path.dentry = a->dst_wh_dentry;
22952+ err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
22953+ a->dst_dentry);
22954+ if (unlikely(err))
22955+ goto out_diropq;
22956+ }
22957+
22958+ /* remove whtmp */
22959+ if (a->thargs)
22960+ au_ren_del_whtmp(a); /* ignore this error */
22961+
22962+ au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
22963+ }
22964+ err = 0;
22965+ goto out_success;
22966+
22967+out_diropq:
22968+ au_ren_rev_diropq(err, a);
22969+out_rename:
22970+ au_ren_rev_rename(err, a);
22971+ dput(a->h_dst);
22972+out_whtmp:
22973+ if (a->thargs)
22974+ au_ren_rev_whtmp(err, a);
22975+out_whdst:
22976+ dput(a->dst_wh_dentry);
22977+ a->dst_wh_dentry = NULL;
22978+out_whsrc:
22979+ if (a->src_wh_dentry)
22980+ au_ren_rev_whsrc(err, a);
22981+out_success:
22982+ dput(a->src_wh_dentry);
22983+ dput(a->dst_wh_dentry);
22984+out_thargs:
22985+ if (a->thargs) {
22986+ dput(a->h_dst);
22987+ au_whtmp_rmdir_free(a->thargs);
22988+ a->thargs = NULL;
22989+ }
22990+out:
22991+ return err;
22992+}
22993+
22994+/* ---------------------------------------------------------------------- */
22995+
22996+/*
22997+ * test if @dentry dir can be rename destination or not.
22998+ * success means, it is a logically empty dir.
22999+ */
23000+static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
23001+{
23002+ return au_test_empty(dentry, whlist);
23003+}
23004+
23005+/*
23006+ * test if @a->src_dentry dir can be rename source or not.
23007+ * if it can, return 0.
23008+ * success means,
23009+ * - it is a logically empty dir.
23010+ * - or, it exists on writable branch and has no children including whiteouts
23011+ * on the lower branch unless DIRREN is on.
23012+ */
23013+static int may_rename_srcdir(struct au_ren_args *a)
23014+{
23015+ int err;
23016+ unsigned int rdhash;
23017+ aufs_bindex_t btop, btgt;
23018+ struct dentry *dentry;
23019+ struct super_block *sb;
23020+ struct au_sbinfo *sbinfo;
23021+
23022+ dentry = a->src_dentry;
23023+ sb = dentry->d_sb;
23024+ sbinfo = au_sbi(sb);
23025+ if (au_opt_test(sbinfo->si_mntflags, DIRREN))
23026+ au_fset_ren(a->auren_flags, DIRREN);
23027+
23028+ btgt = a->btgt;
23029+ btop = au_dbtop(dentry);
23030+ if (btop != btgt) {
23031+ struct au_nhash whlist;
23032+
23033+ SiMustAnyLock(sb);
23034+ rdhash = sbinfo->si_rdhash;
23035+ if (!rdhash)
23036+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
23037+ dentry));
23038+ err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
23039+ if (unlikely(err))
23040+ goto out;
23041+ err = au_test_empty(dentry, &whlist);
23042+ au_nhash_wh_free(&whlist);
23043+ goto out;
23044+ }
23045+
23046+ if (btop == au_dbtaildir(dentry))
23047+ return 0; /* success */
23048+
23049+ err = au_test_empty_lower(dentry);
23050+
23051+out:
23052+ if (err == -ENOTEMPTY) {
23053+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
23054+ err = 0;
23055+ } else {
23056+ AuWarn1("renaming dir who has child(ren) on multiple "
23057+ "branches, is not supported\n");
23058+ err = -EXDEV;
23059+ }
23060+ }
23061+ return err;
23062+}
23063+
23064+/* side effect: sets whlist and h_dentry */
23065+static int au_ren_may_dir(struct au_ren_args *a)
23066+{
23067+ int err;
23068+ unsigned int rdhash;
23069+ struct dentry *d;
23070+
23071+ d = a->dst_dentry;
23072+ SiMustAnyLock(d->d_sb);
23073+
23074+ err = 0;
23075+ if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
23076+ rdhash = au_sbi(d->d_sb)->si_rdhash;
23077+ if (!rdhash)
23078+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
23079+ err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
23080+ if (unlikely(err))
23081+ goto out;
23082+
23083+ if (!a->exchange) {
23084+ au_set_dbtop(d, a->dst_btop);
23085+ err = may_rename_dstdir(d, &a->whlist);
23086+ au_set_dbtop(d, a->btgt);
23087+ } else
23088+ err = may_rename_srcdir(a);
23089+ }
23090+ a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
23091+ if (unlikely(err))
23092+ goto out;
23093+
23094+ d = a->src_dentry;
23095+ a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
23096+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
23097+ err = may_rename_srcdir(a);
23098+ if (unlikely(err)) {
23099+ au_nhash_wh_free(&a->whlist);
23100+ a->whlist.nh_num = 0;
23101+ }
23102+ }
23103+out:
23104+ return err;
23105+}
23106+
23107+/* ---------------------------------------------------------------------- */
23108+
23109+/*
23110+ * simple tests for rename.
23111+ * following the checks in vfs, plus the parent-child relationship.
23112+ */
23113+static int au_may_ren(struct au_ren_args *a)
23114+{
23115+ int err, isdir;
23116+ struct inode *h_inode;
23117+
23118+ if (a->src_btop == a->btgt) {
23119+ err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
23120+ au_ftest_ren(a->auren_flags, ISDIR_SRC));
23121+ if (unlikely(err))
23122+ goto out;
23123+ err = -EINVAL;
23124+ if (unlikely(a->src_h_dentry == a->h_trap))
23125+ goto out;
23126+ }
23127+
23128+ err = 0;
23129+ if (a->dst_btop != a->btgt)
23130+ goto out;
23131+
23132+ err = -ENOTEMPTY;
23133+ if (unlikely(a->dst_h_dentry == a->h_trap))
23134+ goto out;
23135+
23136+ err = -EIO;
23137+ isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
23138+ if (d_really_is_negative(a->dst_dentry)) {
23139+ if (d_is_negative(a->dst_h_dentry))
23140+ err = au_may_add(a->dst_dentry, a->btgt,
23141+ a->dst_h_parent, isdir);
23142+ } else {
23143+ if (unlikely(d_is_negative(a->dst_h_dentry)))
23144+ goto out;
23145+ h_inode = d_inode(a->dst_h_dentry);
23146+ if (h_inode->i_nlink)
23147+ err = au_may_del(a->dst_dentry, a->btgt,
23148+ a->dst_h_parent, isdir);
23149+ }
23150+
23151+out:
23152+ if (unlikely(err == -ENOENT || err == -EEXIST))
23153+ err = -EIO;
23154+ AuTraceErr(err);
23155+ return err;
23156+}
23157+
23158+/* ---------------------------------------------------------------------- */
23159+
23160+/*
23161+ * locking order
23162+ * (VFS)
23163+ * - src_dir and dir by lock_rename()
23164+ * - inode if exists
23165+ * (aufs)
23166+ * - lock all
23167+ * + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
23168+ * + si_read_lock
23169+ * + di_write_lock2_child()
23170+ * + di_write_lock_child()
23171+ * + ii_write_lock_child()
23172+ * + di_write_lock_child2()
23173+ * + ii_write_lock_child2()
23174+ * + src_parent and parent
23175+ * + di_write_lock_parent()
23176+ * + ii_write_lock_parent()
23177+ * + di_write_lock_parent2()
23178+ * + ii_write_lock_parent2()
23179+ * + lower src_dir and dir by vfsub_lock_rename()
23180+ * + verify the every relationships between child and parent. if any
23181+ * of them failed, unlock all and return -EBUSY.
23182+ */
23183+static void au_ren_unlock(struct au_ren_args *a)
23184+{
23185+ vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
23186+ a->dst_h_parent, a->dst_hdir);
23187+ if (au_ftest_ren(a->auren_flags, DIRREN)
23188+ && a->h_root)
23189+ au_hn_inode_unlock(a->h_root);
23190+ if (au_ftest_ren(a->auren_flags, MNT_WRITE))
23191+ vfsub_mnt_drop_write(au_br_mnt(a->br));
23192+}
23193+
23194+static int au_ren_lock(struct au_ren_args *a)
23195+{
23196+ int err;
23197+ unsigned int udba;
23198+
23199+ err = 0;
23200+ a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
23201+ a->src_hdir = au_hi(a->src_dir, a->btgt);
23202+ a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
23203+ a->dst_hdir = au_hi(a->dst_dir, a->btgt);
23204+
23205+ err = vfsub_mnt_want_write(au_br_mnt(a->br));
23206+ if (unlikely(err))
23207+ goto out;
23208+ au_fset_ren(a->auren_flags, MNT_WRITE);
23209+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
23210+ struct dentry *root;
23211+ struct inode *dir;
23212+
23213+ /*
23214+ * sbinfo is already locked, so this ii_read_lock is
23215+ * unnecessary. but our debugging feature checks it.
23216+ */
23217+ root = a->src_inode->i_sb->s_root;
23218+ if (root != a->src_parent && root != a->dst_parent) {
23219+ dir = d_inode(root);
23220+ ii_read_lock_parent3(dir);
23221+ a->h_root = au_hi(dir, a->btgt);
23222+ ii_read_unlock(dir);
23223+ au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3);
23224+ }
23225+ }
23226+ a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
23227+ a->dst_h_parent, a->dst_hdir);
23228+ udba = au_opt_udba(a->src_dentry->d_sb);
23229+ if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
23230+ || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
23231+ err = au_busy_or_stale();
23232+ if (!err && au_dbtop(a->src_dentry) == a->btgt)
23233+ err = au_h_verify(a->src_h_dentry, udba,
23234+ d_inode(a->src_h_parent), a->src_h_parent,
23235+ a->br);
23236+ if (!err && au_dbtop(a->dst_dentry) == a->btgt)
23237+ err = au_h_verify(a->dst_h_dentry, udba,
23238+ d_inode(a->dst_h_parent), a->dst_h_parent,
23239+ a->br);
23240+ if (!err)
23241+ goto out; /* success */
23242+
23243+ err = au_busy_or_stale();
23244+ au_ren_unlock(a);
23245+
23246+out:
23247+ return err;
23248+}
23249+
23250+/* ---------------------------------------------------------------------- */
23251+
23252+static void au_ren_refresh_dir(struct au_ren_args *a)
23253+{
23254+ struct inode *dir;
23255+
23256+ dir = a->dst_dir;
23257+ inode_inc_iversion(dir);
23258+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
23259+ /* is this updating defined in POSIX? */
23260+ au_cpup_attr_timesizes(a->src_inode);
23261+ au_cpup_attr_nlink(dir, /*force*/1);
23262+ }
23263+ au_dir_ts(dir, a->btgt);
23264+
23265+ if (a->exchange) {
23266+ dir = a->src_dir;
23267+ inode_inc_iversion(dir);
23268+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
23269+ /* is this updating defined in POSIX? */
23270+ au_cpup_attr_timesizes(a->dst_inode);
23271+ au_cpup_attr_nlink(dir, /*force*/1);
23272+ }
23273+ au_dir_ts(dir, a->btgt);
23274+ }
23275+
23276+ if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
23277+ return;
23278+
23279+ dir = a->src_dir;
23280+ inode_inc_iversion(dir);
23281+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
23282+ au_cpup_attr_nlink(dir, /*force*/1);
23283+ au_dir_ts(dir, a->btgt);
23284+}
23285+
23286+static void au_ren_refresh(struct au_ren_args *a)
23287+{
23288+ aufs_bindex_t bbot, bindex;
23289+ struct dentry *d, *h_d;
23290+ struct inode *i, *h_i;
23291+ struct super_block *sb;
23292+
23293+ d = a->dst_dentry;
23294+ d_drop(d);
23295+ if (a->h_dst)
23296+ /* already dget-ed by au_ren_or_cpup() */
23297+ au_set_h_dptr(d, a->btgt, a->h_dst);
23298+
23299+ i = a->dst_inode;
23300+ if (i) {
23301+ if (!a->exchange) {
23302+ if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
23303+ vfsub_drop_nlink(i);
23304+ else {
23305+ vfsub_dead_dir(i);
23306+ au_cpup_attr_timesizes(i);
23307+ }
23308+ au_update_dbrange(d, /*do_put_zero*/1);
23309+ } else
23310+ au_cpup_attr_nlink(i, /*force*/1);
23311+ } else {
23312+ bbot = a->btgt;
23313+ for (bindex = au_dbtop(d); bindex < bbot; bindex++)
23314+ au_set_h_dptr(d, bindex, NULL);
23315+ bbot = au_dbbot(d);
23316+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
23317+ au_set_h_dptr(d, bindex, NULL);
23318+ au_update_dbrange(d, /*do_put_zero*/0);
23319+ }
23320+
23321+ if (a->exchange
23322+ || au_ftest_ren(a->auren_flags, DIRREN)) {
23323+ d_drop(a->src_dentry);
23324+ if (au_ftest_ren(a->auren_flags, DIRREN))
23325+ au_set_dbwh(a->src_dentry, -1);
23326+ return;
23327+ }
23328+
23329+ d = a->src_dentry;
23330+ au_set_dbwh(d, -1);
23331+ bbot = au_dbbot(d);
23332+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23333+ h_d = au_h_dptr(d, bindex);
23334+ if (h_d)
23335+ au_set_h_dptr(d, bindex, NULL);
23336+ }
23337+ au_set_dbbot(d, a->btgt);
23338+
23339+ sb = d->d_sb;
23340+ i = a->src_inode;
23341+ if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
23342+ return; /* success */
23343+
23344+ bbot = au_ibbot(i);
23345+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23346+ h_i = au_h_iptr(i, bindex);
23347+ if (h_i) {
23348+ au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
23349+ /* ignore this error */
23350+ au_set_h_iptr(i, bindex, NULL, 0);
23351+ }
23352+ }
23353+ au_set_ibbot(i, a->btgt);
23354+}
23355+
23356+/* ---------------------------------------------------------------------- */
23357+
23358+/* mainly for link(2) and rename(2) */
23359+int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
23360+{
23361+ aufs_bindex_t bdiropq, bwh;
23362+ struct dentry *parent;
23363+ struct au_branch *br;
23364+
23365+ parent = dentry->d_parent;
23366+ IMustLock(d_inode(parent)); /* dir is locked */
23367+
23368+ bdiropq = au_dbdiropq(parent);
23369+ bwh = au_dbwh(dentry);
23370+ br = au_sbr(dentry->d_sb, btgt);
23371+ if (au_br_rdonly(br)
23372+ || (0 <= bdiropq && bdiropq < btgt)
23373+ || (0 <= bwh && bwh < btgt))
23374+ btgt = -1;
23375+
23376+ AuDbg("btgt %d\n", btgt);
23377+ return btgt;
23378+}
23379+
23380+/* sets src_btop, dst_btop and btgt */
23381+static int au_ren_wbr(struct au_ren_args *a)
23382+{
23383+ int err;
23384+ struct au_wr_dir_args wr_dir_args = {
23385+ /* .force_btgt = -1, */
23386+ .flags = AuWrDir_ADD_ENTRY
23387+ };
23388+
23389+ a->src_btop = au_dbtop(a->src_dentry);
23390+ a->dst_btop = au_dbtop(a->dst_dentry);
23391+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
23392+ || au_ftest_ren(a->auren_flags, ISDIR_DST))
23393+ au_fset_wrdir(wr_dir_args.flags, ISDIR);
23394+ wr_dir_args.force_btgt = a->src_btop;
23395+ if (a->dst_inode && a->dst_btop < a->src_btop)
23396+ wr_dir_args.force_btgt = a->dst_btop;
23397+ wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
23398+ err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
23399+ a->btgt = err;
23400+ if (a->exchange)
23401+ au_update_dbtop(a->dst_dentry);
23402+
23403+ return err;
23404+}
23405+
23406+static void au_ren_dt(struct au_ren_args *a)
23407+{
23408+ a->h_path.dentry = a->src_h_parent;
23409+ au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
23410+ if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
23411+ a->h_path.dentry = a->dst_h_parent;
23412+ au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
23413+ }
23414+
23415+ au_fclr_ren(a->auren_flags, DT_DSTDIR);
23416+ if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
23417+ && !a->exchange)
23418+ return;
23419+
23420+ a->h_path.dentry = a->src_h_dentry;
23421+ au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
23422+ if (d_is_positive(a->dst_h_dentry)) {
23423+ au_fset_ren(a->auren_flags, DT_DSTDIR);
23424+ a->h_path.dentry = a->dst_h_dentry;
23425+ au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
23426+ }
23427+}
23428+
23429+static void au_ren_rev_dt(int err, struct au_ren_args *a)
23430+{
23431+ struct dentry *h_d;
23432+ struct inode *h_inode;
23433+
23434+ au_dtime_revert(a->src_dt + AuPARENT);
23435+ if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
23436+ au_dtime_revert(a->dst_dt + AuPARENT);
23437+
23438+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
23439+ h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
23440+ h_inode = d_inode(h_d);
23441+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
23442+ au_dtime_revert(a->src_dt + AuCHILD);
23443+ inode_unlock(h_inode);
23444+
23445+ if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
23446+ h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
23447+ h_inode = d_inode(h_d);
23448+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
23449+ au_dtime_revert(a->dst_dt + AuCHILD);
23450+ inode_unlock(h_inode);
23451+ }
23452+ }
23453+}
23454+
23455+/* ---------------------------------------------------------------------- */
23456+
23457+int aufs_rename(struct user_namespace *userns,
23458+ struct inode *_src_dir, struct dentry *_src_dentry,
23459+ struct inode *_dst_dir, struct dentry *_dst_dentry,
23460+ unsigned int _flags)
23461+{
23462+ int err, lock_flags;
23463+ void *rev;
23464+ /* reduce stack space */
23465+ struct au_ren_args *a;
23466+ struct au_pin pin;
23467+
23468+ AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
23469+ IMustLock(_src_dir);
23470+ IMustLock(_dst_dir);
23471+
23472+ err = -EINVAL;
23473+ if (unlikely(_flags & RENAME_WHITEOUT))
23474+ goto out;
23475+
23476+ err = -ENOMEM;
23477+ BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
23478+ a = kzalloc(sizeof(*a), GFP_NOFS);
23479+ if (unlikely(!a))
23480+ goto out;
23481+
23482+ a->flags = _flags;
23483+ BUILD_BUG_ON(sizeof(a->exchange) == sizeof(u8)
23484+ && RENAME_EXCHANGE > U8_MAX);
23485+ a->exchange = _flags & RENAME_EXCHANGE;
23486+ a->src_dir = _src_dir;
23487+ a->src_dentry = _src_dentry;
23488+ a->src_inode = NULL;
23489+ if (d_really_is_positive(a->src_dentry))
23490+ a->src_inode = d_inode(a->src_dentry);
23491+ a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
23492+ a->dst_dir = _dst_dir;
23493+ a->dst_dentry = _dst_dentry;
23494+ a->dst_inode = NULL;
23495+ if (d_really_is_positive(a->dst_dentry))
23496+ a->dst_inode = d_inode(a->dst_dentry);
23497+ a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
23498+ if (a->dst_inode) {
23499+ /*
23500+ * if EXCHANGE && src is non-dir && dst is dir,
23501+ * dst is not locked.
23502+ */
23503+ /* IMustLock(a->dst_inode); */
23504+ au_igrab(a->dst_inode);
23505+ }
23506+
23507+ err = -ENOTDIR;
23508+ lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
23509+ if (d_is_dir(a->src_dentry)) {
23510+ au_fset_ren(a->auren_flags, ISDIR_SRC);
23511+ if (unlikely(!a->exchange
23512+ && d_really_is_positive(a->dst_dentry)
23513+ && !d_is_dir(a->dst_dentry)))
23514+ goto out_free;
23515+ lock_flags |= AuLock_DIRS;
23516+ }
23517+ if (a->dst_inode && d_is_dir(a->dst_dentry)) {
23518+ au_fset_ren(a->auren_flags, ISDIR_DST);
23519+ if (unlikely(!a->exchange
23520+ && d_really_is_positive(a->src_dentry)
23521+ && !d_is_dir(a->src_dentry)))
23522+ goto out_free;
23523+ lock_flags |= AuLock_DIRS;
23524+ }
23525+ err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
23526+ lock_flags);
23527+ if (unlikely(err))
23528+ goto out_free;
23529+
23530+ err = au_d_hashed_positive(a->src_dentry);
23531+ if (unlikely(err))
23532+ goto out_unlock;
23533+ err = -ENOENT;
23534+ if (a->dst_inode) {
23535+ /*
23536+ * If it is a dir, VFS unhash it before this
23537+ * function. It means we cannot rely upon d_unhashed().
23538+ */
23539+ if (unlikely(!a->dst_inode->i_nlink))
23540+ goto out_unlock;
23541+ if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
23542+ err = au_d_hashed_positive(a->dst_dentry);
23543+ if (unlikely(err && !a->exchange))
23544+ goto out_unlock;
23545+ } else if (unlikely(IS_DEADDIR(a->dst_inode)))
23546+ goto out_unlock;
23547+ } else if (unlikely(d_unhashed(a->dst_dentry)))
23548+ goto out_unlock;
23549+
23550+ /*
23551+ * is it possible?
23552+ * yes, it happened (in linux-3.3-rcN) but I don't know why.
23553+ * there may exist a problem somewhere else.
23554+ */
23555+ err = -EINVAL;
23556+ if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
23557+ goto out_unlock;
23558+
23559+ au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
23560+ di_write_lock_parent(a->dst_parent);
23561+
23562+ /* which branch we process */
23563+ err = au_ren_wbr(a);
23564+ if (unlikely(err < 0))
23565+ goto out_parent;
23566+ a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
23567+ a->h_path.mnt = au_br_mnt(a->br);
23568+
23569+ /* are they available to be renamed */
23570+ err = au_ren_may_dir(a);
23571+ if (unlikely(err))
23572+ goto out_children;
23573+
23574+ /* prepare the writable parent dir on the same branch */
23575+ if (a->dst_btop == a->btgt) {
23576+ au_fset_ren(a->auren_flags, WHDST);
23577+ } else {
23578+ err = au_cpup_dirs(a->dst_dentry, a->btgt);
23579+ if (unlikely(err))
23580+ goto out_children;
23581+ }
23582+
23583+ err = 0;
23584+ if (!a->exchange) {
23585+ if (a->src_dir != a->dst_dir) {
23586+ /*
23587+ * this temporary unlock is safe,
23588+ * because both dir->i_mutex are locked.
23589+ */
23590+ di_write_unlock(a->dst_parent);
23591+ di_write_lock_parent(a->src_parent);
23592+ err = au_wr_dir_need_wh(a->src_dentry,
23593+ au_ftest_ren(a->auren_flags,
23594+ ISDIR_SRC),
23595+ &a->btgt);
23596+ di_write_unlock(a->src_parent);
23597+ di_write_lock2_parent(a->src_parent, a->dst_parent,
23598+ /*isdir*/1);
23599+ au_fclr_ren(a->auren_flags, ISSAMEDIR);
23600+ } else
23601+ err = au_wr_dir_need_wh(a->src_dentry,
23602+ au_ftest_ren(a->auren_flags,
23603+ ISDIR_SRC),
23604+ &a->btgt);
23605+ }
23606+ if (unlikely(err < 0))
23607+ goto out_children;
23608+ if (err)
23609+ au_fset_ren(a->auren_flags, WHSRC);
23610+
23611+ /* cpup src */
23612+ if (a->src_btop != a->btgt) {
23613+ err = au_pin(&pin, a->src_dentry, a->btgt,
23614+ au_opt_udba(a->src_dentry->d_sb),
23615+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23616+ if (!err) {
23617+ struct au_cp_generic cpg = {
23618+ .dentry = a->src_dentry,
23619+ .bdst = a->btgt,
23620+ .bsrc = a->src_btop,
23621+ .len = -1,
23622+ .pin = &pin,
23623+ .flags = AuCpup_DTIME | AuCpup_HOPEN
23624+ };
23625+ AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
23626+ err = au_sio_cpup_simple(&cpg);
23627+ au_unpin(&pin);
23628+ }
23629+ if (unlikely(err))
23630+ goto out_children;
23631+ a->src_btop = a->btgt;
23632+ a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
23633+ if (!a->exchange)
23634+ au_fset_ren(a->auren_flags, WHSRC);
23635+ }
23636+
23637+ /* cpup dst */
23638+ if (a->exchange && a->dst_inode
23639+ && a->dst_btop != a->btgt) {
23640+ err = au_pin(&pin, a->dst_dentry, a->btgt,
23641+ au_opt_udba(a->dst_dentry->d_sb),
23642+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23643+ if (!err) {
23644+ struct au_cp_generic cpg = {
23645+ .dentry = a->dst_dentry,
23646+ .bdst = a->btgt,
23647+ .bsrc = a->dst_btop,
23648+ .len = -1,
23649+ .pin = &pin,
23650+ .flags = AuCpup_DTIME | AuCpup_HOPEN
23651+ };
23652+ err = au_sio_cpup_simple(&cpg);
23653+ au_unpin(&pin);
23654+ }
23655+ if (unlikely(err))
23656+ goto out_children;
23657+ a->dst_btop = a->btgt;
23658+ a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
23659+ }
23660+
23661+ /* lock them all */
23662+ err = au_ren_lock(a);
23663+ if (unlikely(err))
23664+ /* leave the copied-up one */
23665+ goto out_children;
23666+
23667+ if (!a->exchange) {
23668+ if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
23669+ err = au_may_ren(a);
23670+ else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
23671+ err = -ENAMETOOLONG;
23672+ if (unlikely(err))
23673+ goto out_hdir;
23674+ }
23675+
23676+ /* store timestamps to be revertible */
23677+ au_ren_dt(a);
23678+
23679+ /* store dirren info */
23680+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
23681+ err = au_dr_rename(a->src_dentry, a->btgt,
23682+ &a->dst_dentry->d_name, &rev);
23683+ AuTraceErr(err);
23684+ if (unlikely(err))
23685+ goto out_dt;
23686+ }
23687+
23688+ /* here we go */
23689+ err = do_rename(a);
23690+ if (unlikely(err))
23691+ goto out_dirren;
23692+
23693+ if (au_ftest_ren(a->auren_flags, DIRREN))
23694+ au_dr_rename_fin(a->src_dentry, a->btgt, rev);
23695+
23696+ /* update dir attributes */
23697+ au_ren_refresh_dir(a);
23698+
23699+ /* dput/iput all lower dentries */
23700+ au_ren_refresh(a);
23701+
23702+ goto out_hdir; /* success */
23703+
23704+out_dirren:
23705+ if (au_ftest_ren(a->auren_flags, DIRREN))
23706+ au_dr_rename_rev(a->src_dentry, a->btgt, rev);
23707+out_dt:
23708+ au_ren_rev_dt(err, a);
23709+out_hdir:
23710+ au_ren_unlock(a);
23711+out_children:
23712+ au_nhash_wh_free(&a->whlist);
23713+ if (err && a->dst_inode && a->dst_btop != a->btgt) {
23714+ AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
23715+ au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
23716+ au_set_dbtop(a->dst_dentry, a->dst_btop);
23717+ }
23718+out_parent:
23719+ if (!err) {
23720+ if (d_unhashed(a->src_dentry))
23721+ au_fset_ren(a->auren_flags, DROPPED_SRC);
23722+ if (d_unhashed(a->dst_dentry))
23723+ au_fset_ren(a->auren_flags, DROPPED_DST);
23724+ if (!a->exchange)
23725+ d_move(a->src_dentry, a->dst_dentry);
23726+ else {
23727+ d_exchange(a->src_dentry, a->dst_dentry);
23728+ if (au_ftest_ren(a->auren_flags, DROPPED_DST))
23729+ d_drop(a->dst_dentry);
23730+ }
23731+ if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
23732+ d_drop(a->src_dentry);
23733+ } else {
23734+ au_update_dbtop(a->dst_dentry);
23735+ if (!a->dst_inode)
23736+ d_drop(a->dst_dentry);
23737+ }
23738+ if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
23739+ di_write_unlock(a->dst_parent);
23740+ else
23741+ di_write_unlock2(a->src_parent, a->dst_parent);
23742+out_unlock:
23743+ aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
23744+out_free:
23745+ iput(a->dst_inode);
23746+ if (a->thargs)
23747+ au_whtmp_rmdir_free(a->thargs);
23748+ au_kfree_rcu(a);
23749+out:
23750+ AuTraceErr(err);
23751+ return err;
23752+}
23753diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
23754--- /usr/share/empty/fs/aufs/Kconfig 1970-01-01 01:00:00.000000000 +0100
23755+++ linux/fs/aufs/Kconfig 2021-12-03 15:38:59.929980643 +0100
23756@@ -0,0 +1,199 @@
23757+# SPDX-License-Identifier: GPL-2.0
23758+config AUFS_FS
23759+ tristate "Aufs (Advanced multi layered unification filesystem) support"
23760+ help
23761+ Aufs is a stackable unification filesystem such as Unionfs,
23762+ which unifies several directories and provides a merged single
23763+ directory.
23764+ In the early days, aufs was entirely re-designed and
23765+ re-implemented Unionfs Version 1.x series. Introducing many
23766+ original ideas, approaches and improvements, it becomes totally
23767+ different from Unionfs while keeping the basic features.
23768+
23769+if AUFS_FS
23770+choice
23771+ prompt "Maximum number of branches"
23772+ default AUFS_BRANCH_MAX_127
23773+ help
23774+ Specifies the maximum number of branches (or member directories)
23775+ in a single aufs. The larger value consumes more system
23776+ resources and has a minor impact to performance.
23777+config AUFS_BRANCH_MAX_127
23778+ bool "127"
23779+ help
23780+ Specifies the maximum number of branches (or member directories)
23781+ in a single aufs. The larger value consumes more system
23782+ resources and has a minor impact to performance.
23783+config AUFS_BRANCH_MAX_511
23784+ bool "511"
23785+ help
23786+ Specifies the maximum number of branches (or member directories)
23787+ in a single aufs. The larger value consumes more system
23788+ resources and has a minor impact to performance.
23789+config AUFS_BRANCH_MAX_1023
23790+ bool "1023"
23791+ help
23792+ Specifies the maximum number of branches (or member directories)
23793+ in a single aufs. The larger value consumes more system
23794+ resources and has a minor impact to performance.
23795+config AUFS_BRANCH_MAX_32767
23796+ bool "32767"
23797+ help
23798+ Specifies the maximum number of branches (or member directories)
23799+ in a single aufs. The larger value consumes more system
23800+ resources and has a minor impact to performance.
23801+endchoice
23802+
23803+config AUFS_SBILIST
23804+ bool
23805+ depends on AUFS_MAGIC_SYSRQ || PROC_FS
23806+ default y
23807+ help
23808+ Automatic configuration for internal use.
23809+ When aufs supports Magic SysRq or /proc, enabled automatically.
23810+
23811+config AUFS_HNOTIFY
23812+ bool "Detect direct branch access (bypassing aufs)"
23813+ help
23814+ If you want to modify files on branches directly, eg. bypassing aufs,
23815+ and want aufs to detect the changes of them fully, then enable this
23816+ option and use 'udba=notify' mount option.
23817+ Currently there is only one available configuration, "fsnotify".
23818+ It will have a negative impact to the performance.
23819+ See detail in aufs.5.
23820+
23821+choice
23822+ prompt "method" if AUFS_HNOTIFY
23823+ default AUFS_HFSNOTIFY
23824+config AUFS_HFSNOTIFY
23825+ bool "fsnotify"
23826+ select FSNOTIFY
23827+endchoice
23828+
23829+config AUFS_EXPORT
23830+ bool "NFS-exportable aufs"
23831+ depends on EXPORTFS
23832+ help
23833+ If you want to export your mounted aufs via NFS, then enable this
23834+ option. There are several requirements for this configuration.
23835+ See detail in aufs.5.
23836+
23837+config AUFS_INO_T_64
23838+ bool
23839+ depends on AUFS_EXPORT
23840+ depends on 64BIT && !(ALPHA || S390)
23841+ default y
23842+ help
23843+ Automatic configuration for internal use.
23844+ /* typedef unsigned long/int __kernel_ino_t */
23845+ /* alpha and s390x are int */
23846+
23847+config AUFS_XATTR
23848+ bool "support for XATTR/EA (including Security Labels)"
23849+ help
23850+ If your branch fs supports XATTR/EA and you want to make them
23851+ available in aufs too, then enable this opsion and specify the
23852+ branch attributes for EA.
23853+ See detail in aufs.5.
23854+
23855+config AUFS_FHSM
23856+ bool "File-based Hierarchical Storage Management"
23857+ help
23858+ Hierarchical Storage Management (or HSM) is a well-known feature
23859+ in the storage world. Aufs provides this feature as file-based.
23860+ with multiple branches.
23861+ These multiple branches are prioritized, ie. the topmost one
23862+ should be the fastest drive and be used heavily.
23863+
23864+config AUFS_RDU
23865+ bool "Readdir in userspace"
23866+ help
23867+ Aufs has two methods to provide a merged view for a directory,
23868+ by a user-space library and by kernel-space natively. The latter
23869+ is always enabled but sometimes large and slow.
23870+ If you enable this option, install the library in aufs2-util
23871+ package, and set some environment variables for your readdir(3),
23872+ then the work will be handled in user-space which generally
23873+ shows better performance in most cases.
23874+ See detail in aufs.5.
23875+
23876+config AUFS_DIRREN
23877+ bool "Workaround for rename(2)-ing a directory"
23878+ help
23879+ By default, aufs returns EXDEV error in renameing a dir who has
23880+ his child on the lower branch, since it is a bad idea to issue
23881+ rename(2) internally for every lower branch. But user may not
23882+ accept this behaviour. So here is a workaround to allow such
23883+ rename(2) and store some extra infromation on the writable
23884+ branch. Obviously this costs high (and I don't like it).
23885+ To use this feature, you need to enable this configuration AND
23886+ to specify the mount option `dirren.'
23887+ See details in aufs.5 and the design documents.
23888+
23889+config AUFS_SHWH
23890+ bool "Show whiteouts"
23891+ help
23892+ If you want to make the whiteouts in aufs visible, then enable
23893+ this option and specify 'shwh' mount option. Although it may
23894+ sounds like philosophy or something, but in technically it
23895+ simply shows the name of whiteout with keeping its behaviour.
23896+
23897+config AUFS_BR_RAMFS
23898+ bool "Ramfs (initramfs/rootfs) as an aufs branch"
23899+ help
23900+ If you want to use ramfs as an aufs branch fs, then enable this
23901+ option. Generally tmpfs is recommended.
23902+ Aufs prohibited them to be a branch fs by default, because
23903+ initramfs becomes unusable after switch_root or something
23904+ generally. If you sets initramfs as an aufs branch and boot your
23905+ system by switch_root, you will meet a problem easily since the
23906+ files in initramfs may be inaccessible.
23907+ Unless you are going to use ramfs as an aufs branch fs without
23908+ switch_root or something, leave it N.
23909+
23910+config AUFS_BR_FUSE
23911+ bool "Fuse fs as an aufs branch"
23912+ depends on FUSE_FS
23913+ select AUFS_POLL
23914+ help
23915+ If you want to use fuse-based userspace filesystem as an aufs
23916+ branch fs, then enable this option.
23917+ It implements the internal poll(2) operation which is
23918+ implemented by fuse only (curretnly).
23919+
23920+config AUFS_POLL
23921+ bool
23922+ help
23923+ Automatic configuration for internal use.
23924+
23925+config AUFS_BR_HFSPLUS
23926+ bool "Hfsplus as an aufs branch"
23927+ depends on HFSPLUS_FS
23928+ default y
23929+ help
23930+ If you want to use hfsplus fs as an aufs branch fs, then enable
23931+ this option. This option introduces a small overhead at
23932+ copying-up a file on hfsplus.
23933+
23934+config AUFS_BDEV_LOOP
23935+ bool
23936+ depends on BLK_DEV_LOOP
23937+ default y
23938+ help
23939+ Automatic configuration for internal use.
23940+ Convert =[ym] into =y.
23941+
23942+config AUFS_DEBUG
23943+ bool "Debug aufs"
23944+ help
23945+ Enable this to compile aufs internal debug code.
23946+ It will have a negative impact to the performance.
23947+
23948+config AUFS_MAGIC_SYSRQ
23949+ bool
23950+ depends on AUFS_DEBUG && MAGIC_SYSRQ
23951+ default y
23952+ help
23953+ Automatic configuration for internal use.
23954+ When aufs supports Magic SysRq, enabled automatically.
23955+endif
23956diff -urN /usr/share/empty/fs/aufs/lcnt.h linux/fs/aufs/lcnt.h
23957--- /usr/share/empty/fs/aufs/lcnt.h 1970-01-01 01:00:00.000000000 +0100
23958+++ linux/fs/aufs/lcnt.h 2021-12-03 15:38:59.936647310 +0100
23959@@ -0,0 +1,186 @@
23960+/* SPDX-License-Identifier: GPL-2.0 */
23961+/*
23962+ * Copyright (C) 2018-2021 Junjiro R. Okajima
23963+ *
23964+ * This program, aufs is free software; you can redistribute it and/or modify
23965+ * it under the terms of the GNU General Public License as published by
23966+ * the Free Software Foundation; either version 2 of the License, or
23967+ * (at your option) any later version.
23968+ *
23969+ * This program is distributed in the hope that it will be useful,
23970+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
23971+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23972+ * GNU General Public License for more details.
23973+ *
23974+ * You should have received a copy of the GNU General Public License
23975+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
23976+ */
23977+
23978+/*
23979+ * simple long counter wrapper
23980+ */
23981+
23982+#ifndef __AUFS_LCNT_H__
23983+#define __AUFS_LCNT_H__
23984+
23985+#ifdef __KERNEL__
23986+
23987+#include "debug.h"
23988+
23989+#define AuLCntATOMIC 1
23990+#define AuLCntPCPUCNT 2
23991+/*
23992+ * why does percpu_refcount require extra synchronize_rcu()s in
23993+ * au_br_do_free()
23994+ */
23995+#define AuLCntPCPUREF 3
23996+
23997+/* #define AuLCntChosen AuLCntATOMIC */
23998+#define AuLCntChosen AuLCntPCPUCNT
23999+/* #define AuLCntChosen AuLCntPCPUREF */
24000+
24001+#if AuLCntChosen == AuLCntATOMIC
24002+#include <linux/atomic.h>
24003+
24004+typedef atomic_long_t au_lcnt_t;
24005+
24006+static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
24007+{
24008+ atomic_long_set(cnt, 0);
24009+ return 0;
24010+}
24011+
24012+static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24013+{
24014+ /* empty */
24015+}
24016+
24017+static inline void au_lcnt_fin(au_lcnt_t *cnt __maybe_unused,
24018+ int do_sync __maybe_unused)
24019+{
24020+ /* empty */
24021+}
24022+
24023+static inline void au_lcnt_inc(au_lcnt_t *cnt)
24024+{
24025+ atomic_long_inc(cnt);
24026+}
24027+
24028+static inline void au_lcnt_dec(au_lcnt_t *cnt)
24029+{
24030+ atomic_long_dec(cnt);
24031+}
24032+
24033+static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
24034+{
24035+ return atomic_long_read(cnt);
24036+}
24037+#endif
24038+
24039+#if AuLCntChosen == AuLCntPCPUCNT
24040+#include <linux/percpu_counter.h>
24041+
24042+typedef struct percpu_counter au_lcnt_t;
24043+
24044+static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
24045+{
24046+ return percpu_counter_init(cnt, 0, GFP_NOFS);
24047+}
24048+
24049+static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24050+{
24051+ /* empty */
24052+}
24053+
24054+static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync __maybe_unused)
24055+{
24056+ percpu_counter_destroy(cnt);
24057+}
24058+
24059+static inline void au_lcnt_inc(au_lcnt_t *cnt)
24060+{
24061+ percpu_counter_inc(cnt);
24062+}
24063+
24064+static inline void au_lcnt_dec(au_lcnt_t *cnt)
24065+{
24066+ percpu_counter_dec(cnt);
24067+}
24068+
24069+static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
24070+{
24071+ s64 n;
24072+
24073+ n = percpu_counter_sum(cnt);
24074+ BUG_ON(n < 0);
24075+ if (LONG_MAX != LLONG_MAX
24076+ && n > LONG_MAX)
24077+ AuWarn1("%s\n", "wrap-around");
24078+
24079+ return n;
24080+}
24081+#endif
24082+
24083+#if AuLCntChosen == AuLCntPCPUREF
24084+#include <linux/percpu-refcount.h>
24085+
24086+typedef struct percpu_ref au_lcnt_t;
24087+
24088+static inline int au_lcnt_init(au_lcnt_t *cnt, percpu_ref_func_t *release)
24089+{
24090+ if (!release)
24091+ release = percpu_ref_exit;
24092+ return percpu_ref_init(cnt, release, /*percpu mode*/0, GFP_NOFS);
24093+}
24094+
24095+static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24096+{
24097+ synchronize_rcu();
24098+}
24099+
24100+static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync)
24101+{
24102+ percpu_ref_kill(cnt);
24103+ if (do_sync)
24104+ au_lcnt_wait_for_fin(cnt);
24105+}
24106+
24107+static inline void au_lcnt_inc(au_lcnt_t *cnt)
24108+{
24109+ percpu_ref_get(cnt);
24110+}
24111+
24112+static inline void au_lcnt_dec(au_lcnt_t *cnt)
24113+{
24114+ percpu_ref_put(cnt);
24115+}
24116+
24117+/*
24118+ * avoid calling this func as possible.
24119+ */
24120+static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev)
24121+{
24122+ long l;
24123+
24124+ percpu_ref_switch_to_atomic_sync(cnt);
24125+ l = atomic_long_read(&cnt->count);
24126+ if (do_rev)
24127+ percpu_ref_switch_to_percpu(cnt);
24128+
24129+ /* percpu_ref is initialized by 1 instead of 0 */
24130+ return l - 1;
24131+}
24132+#endif
24133+
24134+#ifdef CONFIG_AUFS_DEBUG
24135+#define AuLCntZero(val) do { \
24136+ long l = val; \
24137+ if (l) \
24138+ AuDbg("%s = %ld\n", #val, l); \
24139+} while (0)
24140+#else
24141+#define AuLCntZero(val) do {} while (0)
24142+#endif
24143+
24144+#endif /* __KERNEL__ */
24145+#endif /* __AUFS_LCNT_H__ */
24146diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
24147--- /usr/share/empty/fs/aufs/loop.c 1970-01-01 01:00:00.000000000 +0100
24148+++ linux/fs/aufs/loop.c 2021-12-03 15:38:59.936647310 +0100
24149@@ -0,0 +1,148 @@
24150+// SPDX-License-Identifier: GPL-2.0
24151+/*
24152+ * Copyright (C) 2005-2021 Junjiro R. Okajima
24153+ *
24154+ * This program, aufs is free software; you can redistribute it and/or modify
24155+ * it under the terms of the GNU General Public License as published by
24156+ * the Free Software Foundation; either version 2 of the License, or
24157+ * (at your option) any later version.
24158+ *
24159+ * This program is distributed in the hope that it will be useful,
24160+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24161+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24162+ * GNU General Public License for more details.
24163+ *
24164+ * You should have received a copy of the GNU General Public License
24165+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
24166+ */
24167+
24168+/*
24169+ * support for loopback block device as a branch
24170+ */
24171+
24172+#include "aufs.h"
24173+
24174+/* added into drivers/block/loop.c */
24175+static struct file *(*backing_file_func)(struct super_block *sb);
24176+
24177+/*
24178+ * test if two lower dentries have overlapping branches.
24179+ */
24180+int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
24181+{
24182+ struct super_block *h_sb;
24183+ struct file *backing_file;
24184+
24185+ if (unlikely(!backing_file_func)) {
24186+ /* don't load "loop" module here */
24187+ backing_file_func = symbol_get(loop_backing_file);
24188+ if (unlikely(!backing_file_func))
24189+ /* "loop" module is not loaded */
24190+ return 0;
24191+ }
24192+
24193+ h_sb = h_adding->d_sb;
24194+ backing_file = backing_file_func(h_sb);
24195+ if (!backing_file)
24196+ return 0;
24197+
24198+ h_adding = backing_file->f_path.dentry;
24199+ /*
24200+ * h_adding can be local NFS.
24201+ * in this case aufs cannot detect the loop.
24202+ */
24203+ if (unlikely(h_adding->d_sb == sb))
24204+ return 1;
24205+ return !!au_test_subdir(h_adding, sb->s_root);
24206+}
24207+
24208+/* true if a kernel thread named 'loop[0-9].*' accesses a file */
24209+int au_test_loopback_kthread(void)
24210+{
24211+ int ret;
24212+ struct task_struct *tsk = current;
24213+ char c, comm[sizeof(tsk->comm)];
24214+
24215+ ret = 0;
24216+ if (tsk->flags & PF_KTHREAD) {
24217+ get_task_comm(comm, tsk);
24218+ c = comm[4];
24219+ ret = ('0' <= c && c <= '9'
24220+ && !strncmp(comm, "loop", 4));
24221+ }
24222+
24223+ return ret;
24224+}
24225+
24226+/* ---------------------------------------------------------------------- */
24227+
24228+#define au_warn_loopback_step 16
24229+static int au_warn_loopback_nelem = au_warn_loopback_step;
24230+static unsigned long *au_warn_loopback_array;
24231+
24232+void au_warn_loopback(struct super_block *h_sb)
24233+{
24234+ int i, new_nelem;
24235+ unsigned long *a, magic;
24236+ static DEFINE_SPINLOCK(spin);
24237+
24238+ magic = h_sb->s_magic;
24239+ spin_lock(&spin);
24240+ a = au_warn_loopback_array;
24241+ for (i = 0; i < au_warn_loopback_nelem && *a; i++)
24242+ if (a[i] == magic) {
24243+ spin_unlock(&spin);
24244+ return;
24245+ }
24246+
24247+ /* h_sb is new to us, print it */
24248+ if (i < au_warn_loopback_nelem) {
24249+ a[i] = magic;
24250+ goto pr;
24251+ }
24252+
24253+ /* expand the array */
24254+ new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
24255+ a = au_kzrealloc(au_warn_loopback_array,
24256+ au_warn_loopback_nelem * sizeof(unsigned long),
24257+ new_nelem * sizeof(unsigned long), GFP_ATOMIC,
24258+ /*may_shrink*/0);
24259+ if (a) {
24260+ au_warn_loopback_nelem = new_nelem;
24261+ au_warn_loopback_array = a;
24262+ a[i] = magic;
24263+ goto pr;
24264+ }
24265+
24266+ spin_unlock(&spin);
24267+ AuWarn1("realloc failed, ignored\n");
24268+ return;
24269+
24270+pr:
24271+ spin_unlock(&spin);
24272+ pr_warn("you may want to try another patch for loopback file "
24273+ "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
24274+}
24275+
24276+int au_loopback_init(void)
24277+{
24278+ int err;
24279+ struct super_block *sb __maybe_unused;
24280+
24281+ BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(*au_warn_loopback_array));
24282+
24283+ err = 0;
24284+ au_warn_loopback_array = kcalloc(au_warn_loopback_step,
24285+ sizeof(unsigned long), GFP_NOFS);
24286+ if (unlikely(!au_warn_loopback_array))
24287+ err = -ENOMEM;
24288+
24289+ return err;
24290+}
24291+
24292+void au_loopback_fin(void)
24293+{
24294+ if (backing_file_func)
24295+ symbol_put(loop_backing_file);
24296+ au_kfree_try_rcu(au_warn_loopback_array);
24297+}
24298diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
24299--- /usr/share/empty/fs/aufs/loop.h 1970-01-01 01:00:00.000000000 +0100
24300+++ linux/fs/aufs/loop.h 2021-12-03 15:38:59.936647310 +0100
24301@@ -0,0 +1,55 @@
24302+/* SPDX-License-Identifier: GPL-2.0 */
24303+/*
24304+ * Copyright (C) 2005-2021 Junjiro R. Okajima
24305+ *
24306+ * This program, aufs is free software; you can redistribute it and/or modify
24307+ * it under the terms of the GNU General Public License as published by
24308+ * the Free Software Foundation; either version 2 of the License, or
24309+ * (at your option) any later version.
24310+ *
24311+ * This program is distributed in the hope that it will be useful,
24312+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24313+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24314+ * GNU General Public License for more details.
24315+ *
24316+ * You should have received a copy of the GNU General Public License
24317+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
24318+ */
24319+
24320+/*
24321+ * support for loopback mount as a branch
24322+ */
24323+
24324+#ifndef __AUFS_LOOP_H__
24325+#define __AUFS_LOOP_H__
24326+
24327+#ifdef __KERNEL__
24328+
24329+struct dentry;
24330+struct super_block;
24331+
24332+#ifdef CONFIG_AUFS_BDEV_LOOP
24333+/* drivers/block/loop.c */
24334+struct file *loop_backing_file(struct super_block *sb);
24335+
24336+/* loop.c */
24337+int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
24338+int au_test_loopback_kthread(void);
24339+void au_warn_loopback(struct super_block *h_sb);
24340+
24341+int au_loopback_init(void);
24342+void au_loopback_fin(void);
24343+#else
24344+AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
24345+
24346+AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
24347+ struct dentry *h_adding)
24348+AuStubInt0(au_test_loopback_kthread, void)
24349+AuStubVoid(au_warn_loopback, struct super_block *h_sb)
24350+
24351+AuStubInt0(au_loopback_init, void)
24352+AuStubVoid(au_loopback_fin, void)
24353+#endif /* BLK_DEV_LOOP */
24354+
24355+#endif /* __KERNEL__ */
24356+#endif /* __AUFS_LOOP_H__ */
24357diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
24358--- /usr/share/empty/fs/aufs/magic.mk 1970-01-01 01:00:00.000000000 +0100
24359+++ linux/fs/aufs/magic.mk 2021-12-03 15:38:59.936647310 +0100
24360@@ -0,0 +1,31 @@
24361+# SPDX-License-Identifier: GPL-2.0
24362+
24363+# defined in ${srctree}/fs/fuse/inode.c
24364+# tristate
24365+ifdef CONFIG_FUSE_FS
24366+ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
24367+endif
24368+
24369+# defined in ${srctree}/fs/xfs/xfs_sb.h
24370+# tristate
24371+ifdef CONFIG_XFS_FS
24372+ccflags-y += -DXFS_SB_MAGIC=0x58465342
24373+endif
24374+
24375+# defined in ${srctree}/fs/configfs/mount.c
24376+# tristate
24377+ifdef CONFIG_CONFIGFS_FS
24378+ccflags-y += -DCONFIGFS_MAGIC=0x62656570
24379+endif
24380+
24381+# defined in ${srctree}/fs/ubifs/ubifs.h
24382+# tristate
24383+ifdef CONFIG_UBIFS_FS
24384+ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
24385+endif
24386+
24387+# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
24388+# tristate
24389+ifdef CONFIG_HFSPLUS_FS
24390+ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
24391+endif
24392diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
24393--- /usr/share/empty/fs/aufs/Makefile 1970-01-01 01:00:00.000000000 +0100
24394+++ linux/fs/aufs/Makefile 2021-12-03 15:38:59.929980643 +0100
24395@@ -0,0 +1,46 @@
24396+# SPDX-License-Identifier: GPL-2.0
24397+
24398+include ${src}/magic.mk
24399+ifeq (${CONFIG_AUFS_FS},m)
24400+include ${src}/conf.mk
24401+endif
24402+-include ${src}/priv_def.mk
24403+
24404+# cf. include/linux/kernel.h
24405+# enable pr_debug
24406+ccflags-y += -DDEBUG
24407+# sparse requires the full pathname
24408+ifdef M
24409+ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
24410+else
24411+ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
24412+endif
24413+
24414+obj-$(CONFIG_AUFS_FS) += aufs.o
24415+aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
24416+ wkq.o vfsub.o dcsub.o \
24417+ cpup.o whout.o wbr_policy.o \
24418+ dinfo.o dentry.o \
24419+ dynop.o \
24420+ finfo.o file.o f_op.o \
24421+ dir.o vdir.o \
24422+ iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
24423+ mvdown.o ioctl.o
24424+
24425+# all are boolean
24426+aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
24427+aufs-$(CONFIG_SYSFS) += sysfs.o
24428+aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
24429+aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
24430+aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
24431+aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
24432+aufs-$(CONFIG_AUFS_EXPORT) += export.o
24433+aufs-$(CONFIG_AUFS_XATTR) += xattr.o
24434+aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
24435+aufs-$(CONFIG_AUFS_DIRREN) += dirren.o
24436+aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
24437+aufs-$(CONFIG_AUFS_POLL) += poll.o
24438+aufs-$(CONFIG_AUFS_RDU) += rdu.o
24439+aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
24440+aufs-$(CONFIG_AUFS_DEBUG) += debug.o
24441+aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
24442diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
24443--- /usr/share/empty/fs/aufs/module.c 1970-01-01 01:00:00.000000000 +0100
24444+++ linux/fs/aufs/module.c 2021-12-03 15:38:59.936647310 +0100
24445@@ -0,0 +1,273 @@
24446+// SPDX-License-Identifier: GPL-2.0
24447+/*
24448+ * Copyright (C) 2005-2021 Junjiro R. Okajima
24449+ *
24450+ * This program, aufs is free software; you can redistribute it and/or modify
24451+ * it under the terms of the GNU General Public License as published by
24452+ * the Free Software Foundation; either version 2 of the License, or
24453+ * (at your option) any later version.
24454+ *
24455+ * This program is distributed in the hope that it will be useful,
24456+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24457+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24458+ * GNU General Public License for more details.
24459+ *
24460+ * You should have received a copy of the GNU General Public License
24461+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
24462+ */
24463+
24464+/*
24465+ * module global variables and operations
24466+ */
24467+
24468+#include <linux/module.h>
24469+#include <linux/seq_file.h>
24470+#include "aufs.h"
24471+
24472+/* shrinkable realloc */
24473+void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
24474+{
24475+ size_t sz;
24476+ int diff;
24477+
24478+ sz = 0;
24479+ diff = -1;
24480+ if (p) {
24481+#if 0 /* unused */
24482+ if (!new_sz) {
24483+ au_kfree_rcu(p);
24484+ p = NULL;
24485+ goto out;
24486+ }
24487+#else
24488+ AuDebugOn(!new_sz);
24489+#endif
24490+ sz = ksize(p);
24491+ diff = au_kmidx_sub(sz, new_sz);
24492+ }
24493+ if (sz && !diff)
24494+ goto out;
24495+
24496+ if (sz < new_sz)
24497+ /* expand or SLOB */
24498+ p = krealloc(p, new_sz, gfp);
24499+ else if (new_sz < sz && may_shrink) {
24500+ /* shrink */
24501+ void *q;
24502+
24503+ q = kmalloc(new_sz, gfp);
24504+ if (q) {
24505+ if (p) {
24506+ memcpy(q, p, new_sz);
24507+ au_kfree_try_rcu(p);
24508+ }
24509+ p = q;
24510+ } else
24511+ p = NULL;
24512+ }
24513+
24514+out:
24515+ return p;
24516+}
24517+
24518+void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24519+ int may_shrink)
24520+{
24521+ p = au_krealloc(p, new_sz, gfp, may_shrink);
24522+ if (p && new_sz > nused)
24523+ memset(p + nused, 0, new_sz - nused);
24524+ return p;
24525+}
24526+
24527+/* ---------------------------------------------------------------------- */
24528+/*
24529+ * aufs caches
24530+ */
24531+struct kmem_cache *au_cache[AuCache_Last];
24532+
24533+static void au_cache_fin(void)
24534+{
24535+ int i;
24536+
24537+ /*
24538+ * Make sure all delayed rcu free inodes are flushed before we
24539+ * destroy cache.
24540+ */
24541+ rcu_barrier();
24542+
24543+ /* excluding AuCache_HNOTIFY */
24544+ BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
24545+ for (i = 0; i < AuCache_HNOTIFY; i++) {
24546+ kmem_cache_destroy(au_cache[i]);
24547+ au_cache[i] = NULL;
24548+ }
24549+}
24550+
24551+static int __init au_cache_init(void)
24552+{
24553+ au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
24554+ if (au_cache[AuCache_DINFO])
24555+ /* SLAB_DESTROY_BY_RCU */
24556+ au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
24557+ au_icntnr_init_once);
24558+ if (au_cache[AuCache_ICNTNR])
24559+ au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
24560+ au_fi_init_once);
24561+ if (au_cache[AuCache_FINFO])
24562+ au_cache[AuCache_VDIR] = AuCache(au_vdir);
24563+ if (au_cache[AuCache_VDIR])
24564+ au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
24565+ if (au_cache[AuCache_DEHSTR])
24566+ return 0;
24567+
24568+ au_cache_fin();
24569+ return -ENOMEM;
24570+}
24571+
24572+/* ---------------------------------------------------------------------- */
24573+
24574+int au_dir_roflags;
24575+
24576+#ifdef CONFIG_AUFS_SBILIST
24577+/*
24578+ * iterate_supers_type() doesn't protect us from
24579+ * remounting (branch management)
24580+ */
24581+struct hlist_bl_head au_sbilist;
24582+#endif
24583+
24584+/*
24585+ * functions for module interface.
24586+ */
24587+MODULE_LICENSE("GPL");
24588+/* MODULE_LICENSE("GPL v2"); */
24589+MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
24590+MODULE_DESCRIPTION(AUFS_NAME
24591+ " -- Advanced multi layered unification filesystem");
24592+MODULE_VERSION(AUFS_VERSION);
24593+MODULE_ALIAS_FS(AUFS_NAME);
24594+
24595+/* this module parameter has no meaning when SYSFS is disabled */
24596+int sysaufs_brs = 1;
24597+MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
24598+module_param_named(brs, sysaufs_brs, int, 0444);
24599+
24600+/* this module parameter has no meaning when USER_NS is disabled */
24601+bool au_userns;
24602+MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
24603+module_param_named(allow_userns, au_userns, bool, 0444);
24604+
24605+/* ---------------------------------------------------------------------- */
24606+
24607+static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
24608+
24609+int au_seq_path(struct seq_file *seq, struct path *path)
24610+{
24611+ int err;
24612+
24613+ err = seq_path(seq, path, au_esc_chars);
24614+ if (err >= 0)
24615+ err = 0;
24616+ else
24617+ err = -ENOMEM;
24618+
24619+ return err;
24620+}
24621+
24622+/* ---------------------------------------------------------------------- */
24623+
24624+static int __init aufs_init(void)
24625+{
24626+ int err, i;
24627+ char *p;
24628+
24629+ p = au_esc_chars;
24630+ for (i = 1; i <= ' '; i++)
24631+ *p++ = i;
24632+ *p++ = '\\';
24633+ *p++ = '\x7f';
24634+ *p = 0;
24635+
24636+ au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
24637+
24638+ memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
24639+ for (i = 0; i < AuIop_Last; i++)
24640+ aufs_iop_nogetattr[i].getattr = NULL;
24641+
24642+ memset(au_cache, 0, sizeof(au_cache)); /* including hnotify */
24643+
24644+ au_sbilist_init();
24645+ sysaufs_brs_init();
24646+ au_debug_init();
24647+ au_dy_init();
24648+ err = sysaufs_init();
24649+ if (unlikely(err))
24650+ goto out;
24651+ err = dbgaufs_init();
24652+ if (unlikely(err))
24653+ goto out_sysaufs;
24654+ err = au_procfs_init();
24655+ if (unlikely(err))
24656+ goto out_dbgaufs;
24657+ err = au_wkq_init();
24658+ if (unlikely(err))
24659+ goto out_procfs;
24660+ err = au_loopback_init();
24661+ if (unlikely(err))
24662+ goto out_wkq;
24663+ err = au_hnotify_init();
24664+ if (unlikely(err))
24665+ goto out_loopback;
24666+ err = au_sysrq_init();
24667+ if (unlikely(err))
24668+ goto out_hin;
24669+ err = au_cache_init();
24670+ if (unlikely(err))
24671+ goto out_sysrq;
24672+
24673+ aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
24674+ err = register_filesystem(&aufs_fs_type);
24675+ if (unlikely(err))
24676+ goto out_cache;
24677+
24678+ /* since we define pr_fmt, call printk directly */
24679+ printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
24680+ goto out; /* success */
24681+
24682+out_cache:
24683+ au_cache_fin();
24684+out_sysrq:
24685+ au_sysrq_fin();
24686+out_hin:
24687+ au_hnotify_fin();
24688+out_loopback:
24689+ au_loopback_fin();
24690+out_wkq:
24691+ au_wkq_fin();
24692+out_procfs:
24693+ au_procfs_fin();
24694+out_dbgaufs:
24695+ dbgaufs_fin();
24696+out_sysaufs:
24697+ sysaufs_fin();
24698+ au_dy_fin();
24699+out:
24700+ return err;
24701+}
24702+
24703+static void __exit aufs_exit(void)
24704+{
24705+ unregister_filesystem(&aufs_fs_type);
24706+ au_cache_fin();
24707+ au_sysrq_fin();
24708+ au_hnotify_fin();
24709+ au_loopback_fin();
24710+ au_wkq_fin();
24711+ au_procfs_fin();
24712+ dbgaufs_fin();
24713+ sysaufs_fin();
24714+ au_dy_fin();
24715+}
24716+
24717+module_init(aufs_init);
24718+module_exit(aufs_exit);
24719diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
24720--- /usr/share/empty/fs/aufs/module.h 1970-01-01 01:00:00.000000000 +0100
24721+++ linux/fs/aufs/module.h 2021-12-03 15:40:58.236647297 +0100
24722@@ -0,0 +1,166 @@
24723+/* SPDX-License-Identifier: GPL-2.0 */
24724+/*
24725+ * Copyright (C) 2005-2021 Junjiro R. Okajima
24726+ *
24727+ * This program, aufs is free software; you can redistribute it and/or modify
24728+ * it under the terms of the GNU General Public License as published by
24729+ * the Free Software Foundation; either version 2 of the License, or
24730+ * (at your option) any later version.
24731+ *
24732+ * This program is distributed in the hope that it will be useful,
24733+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24734+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24735+ * GNU General Public License for more details.
24736+ *
24737+ * You should have received a copy of the GNU General Public License
24738+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
24739+ */
24740+
24741+/*
24742+ * module initialization and module-global
24743+ */
24744+
24745+#ifndef __AUFS_MODULE_H__
24746+#define __AUFS_MODULE_H__
24747+
24748+#ifdef __KERNEL__
24749+
24750+#include <linux/slab.h>
24751+#include "debug.h"
24752+#include "dentry.h"
24753+#include "dir.h"
24754+#include "file.h"
24755+#include "inode.h"
24756+
24757+struct path;
24758+struct seq_file;
24759+
24760+/* module parameters */
24761+extern int sysaufs_brs;
24762+extern bool au_userns;
24763+
24764+/* ---------------------------------------------------------------------- */
24765+
24766+extern int au_dir_roflags;
24767+
24768+void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
24769+void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24770+ int may_shrink);
24771+
24772+/*
24773+ * Comparing the size of the object with sizeof(struct rcu_head)
24774+ * case 1: object is always larger
24775+ * --> au_kfree_rcu() or au_kfree_do_rcu()
24776+ * case 2: object is always smaller
24777+ * --> au_kfree_small()
24778+ * case 3: object can be any size
24779+ * --> au_kfree_try_rcu()
24780+ */
24781+
24782+static inline void au_kfree_do_rcu(const void *p)
24783+{
24784+ struct {
24785+ struct rcu_head rcu;
24786+ } *a = (void *)p;
24787+
24788+ kfree_rcu(a, rcu);
24789+}
24790+
24791+#define au_kfree_rcu(_p) do { \
24792+ typeof(_p) p = (_p); \
24793+ BUILD_BUG_ON(sizeof(*p) < sizeof(struct rcu_head)); \
24794+ if (p) \
24795+ au_kfree_do_rcu(p); \
24796+ } while (0)
24797+
24798+#define au_kfree_do_sz_test(sz) (sz >= sizeof(struct rcu_head))
24799+#define au_kfree_sz_test(p) (p && au_kfree_do_sz_test(ksize(p)))
24800+
24801+static inline void au_kfree_try_rcu(const void *p)
24802+{
24803+ if (!p)
24804+ return;
24805+ if (au_kfree_sz_test(p))
24806+ au_kfree_do_rcu(p);
24807+ else
24808+ kfree(p);
24809+}
24810+
24811+static inline void au_kfree_small(const void *p)
24812+{
24813+ if (!p)
24814+ return;
24815+ AuDebugOn(au_kfree_sz_test(p));
24816+ kfree(p);
24817+}
24818+
24819+static inline int au_kmidx_sub(size_t sz, size_t new_sz)
24820+{
24821+#ifndef CONFIG_SLOB
24822+ return __kmalloc_index(sz, false) - __kmalloc_index(new_sz, false);
24823+#else
24824+ return -1; /* SLOB is untested */
24825+#endif
24826+}
24827+
24828+int au_seq_path(struct seq_file *seq, struct path *path);
24829+
24830+#ifdef CONFIG_PROC_FS
24831+/* procfs.c */
24832+int __init au_procfs_init(void);
24833+void au_procfs_fin(void);
24834+#else
24835+AuStubInt0(au_procfs_init, void);
24836+AuStubVoid(au_procfs_fin, void);
24837+#endif
24838+
24839+/* ---------------------------------------------------------------------- */
24840+
24841+/* kmem cache */
24842+enum {
24843+ AuCache_DINFO,
24844+ AuCache_ICNTNR,
24845+ AuCache_FINFO,
24846+ AuCache_VDIR,
24847+ AuCache_DEHSTR,
24848+ AuCache_HNOTIFY, /* must be last */
24849+ AuCache_Last
24850+};
24851+
24852+extern struct kmem_cache *au_cache[AuCache_Last];
24853+
24854+#define AuCacheFlags (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
24855+#define AuCache(type) KMEM_CACHE(type, AuCacheFlags)
24856+#define AuCacheCtor(type, ctor) \
24857+ kmem_cache_create(#type, sizeof(struct type), \
24858+ __alignof__(struct type), AuCacheFlags, ctor)
24859+
24860+#define AuCacheFuncs(name, index) \
24861+ static inline struct au_##name *au_cache_alloc_##name(void) \
24862+ { return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); } \
24863+ static inline void au_cache_free_##name##_norcu(struct au_##name *p) \
24864+ { kmem_cache_free(au_cache[AuCache_##index], p); } \
24865+ \
24866+ static inline void au_cache_free_##name##_rcu_cb(struct rcu_head *rcu) \
24867+ { void *p = rcu; \
24868+ p -= offsetof(struct au_##name, rcu); \
24869+ kmem_cache_free(au_cache[AuCache_##index], p); } \
24870+ static inline void au_cache_free_##name##_rcu(struct au_##name *p) \
24871+ { BUILD_BUG_ON(sizeof(struct au_##name) < sizeof(struct rcu_head)); \
24872+ call_rcu(&p->rcu, au_cache_free_##name##_rcu_cb); } \
24873+ \
24874+ static inline void au_cache_free_##name(struct au_##name *p) \
24875+ { /* au_cache_free_##name##_norcu(p); */ \
24876+ au_cache_free_##name##_rcu(p); }
24877+
24878+AuCacheFuncs(dinfo, DINFO);
24879+AuCacheFuncs(icntnr, ICNTNR);
24880+AuCacheFuncs(finfo, FINFO);
24881+AuCacheFuncs(vdir, VDIR);
24882+AuCacheFuncs(vdir_dehstr, DEHSTR);
24883+#ifdef CONFIG_AUFS_HNOTIFY
24884+AuCacheFuncs(hnotify, HNOTIFY);
24885+#endif
24886+
24887+#endif /* __KERNEL__ */
24888+#endif /* __AUFS_MODULE_H__ */
24889diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
24890--- /usr/share/empty/fs/aufs/mvdown.c 1970-01-01 01:00:00.000000000 +0100
24891+++ linux/fs/aufs/mvdown.c 2021-12-03 15:38:59.939980643 +0100
24892@@ -0,0 +1,706 @@
24893+// SPDX-License-Identifier: GPL-2.0
24894+/*
24895+ * Copyright (C) 2011-2021 Junjiro R. Okajima
24896+ *
24897+ * This program, aufs is free software; you can redistribute it and/or modify
24898+ * it under the terms of the GNU General Public License as published by
24899+ * the Free Software Foundation; either version 2 of the License, or
24900+ * (at your option) any later version.
24901+ *
24902+ * This program is distributed in the hope that it will be useful,
24903+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24904+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24905+ * GNU General Public License for more details.
24906+ *
24907+ * You should have received a copy of the GNU General Public License
24908+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
24909+ */
24910+
24911+/*
24912+ * move-down, opposite of copy-up
24913+ */
24914+
24915+#include "aufs.h"
24916+
24917+struct au_mvd_args {
24918+ struct {
24919+ struct super_block *h_sb;
24920+ struct dentry *h_parent;
24921+ struct au_hinode *hdir;
24922+ struct inode *h_dir, *h_inode;
24923+ struct au_pin pin;
24924+ } info[AUFS_MVDOWN_NARRAY];
24925+
24926+ struct aufs_mvdown mvdown;
24927+ struct dentry *dentry, *parent;
24928+ struct inode *inode, *dir;
24929+ struct super_block *sb;
24930+ aufs_bindex_t bopq, bwh, bfound;
24931+ unsigned char rename_lock;
24932+};
24933+
24934+#define mvd_errno mvdown.au_errno
24935+#define mvd_bsrc mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
24936+#define mvd_src_brid mvdown.stbr[AUFS_MVDOWN_UPPER].brid
24937+#define mvd_bdst mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
24938+#define mvd_dst_brid mvdown.stbr[AUFS_MVDOWN_LOWER].brid
24939+
24940+#define mvd_h_src_sb info[AUFS_MVDOWN_UPPER].h_sb
24941+#define mvd_h_src_parent info[AUFS_MVDOWN_UPPER].h_parent
24942+#define mvd_hdir_src info[AUFS_MVDOWN_UPPER].hdir
24943+#define mvd_h_src_dir info[AUFS_MVDOWN_UPPER].h_dir
24944+#define mvd_h_src_inode info[AUFS_MVDOWN_UPPER].h_inode
24945+#define mvd_pin_src info[AUFS_MVDOWN_UPPER].pin
24946+
24947+#define mvd_h_dst_sb info[AUFS_MVDOWN_LOWER].h_sb
24948+#define mvd_h_dst_parent info[AUFS_MVDOWN_LOWER].h_parent
24949+#define mvd_hdir_dst info[AUFS_MVDOWN_LOWER].hdir
24950+#define mvd_h_dst_dir info[AUFS_MVDOWN_LOWER].h_dir
24951+#define mvd_h_dst_inode info[AUFS_MVDOWN_LOWER].h_inode
24952+#define mvd_pin_dst info[AUFS_MVDOWN_LOWER].pin
24953+
24954+#define AU_MVD_PR(flag, ...) do { \
24955+ if (flag) \
24956+ pr_err(__VA_ARGS__); \
24957+ } while (0)
24958+
24959+static int find_lower_writable(struct au_mvd_args *a)
24960+{
24961+ struct super_block *sb;
24962+ aufs_bindex_t bindex, bbot;
24963+ struct au_branch *br;
24964+
24965+ sb = a->sb;
24966+ bindex = a->mvd_bsrc;
24967+ bbot = au_sbbot(sb);
24968+ if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
24969+ for (bindex++; bindex <= bbot; bindex++) {
24970+ br = au_sbr(sb, bindex);
24971+ if (au_br_fhsm(br->br_perm)
24972+ && !sb_rdonly(au_br_sb(br)))
24973+ return bindex;
24974+ }
24975+ else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
24976+ for (bindex++; bindex <= bbot; bindex++) {
24977+ br = au_sbr(sb, bindex);
24978+ if (!au_br_rdonly(br))
24979+ return bindex;
24980+ }
24981+ else
24982+ for (bindex++; bindex <= bbot; bindex++) {
24983+ br = au_sbr(sb, bindex);
24984+ if (!sb_rdonly(au_br_sb(br))) {
24985+ if (au_br_rdonly(br))
24986+ a->mvdown.flags
24987+ |= AUFS_MVDOWN_ROLOWER_R;
24988+ return bindex;
24989+ }
24990+ }
24991+
24992+ return -1;
24993+}
24994+
24995+/* make the parent dir on bdst */
24996+static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
24997+{
24998+ int err;
24999+
25000+ err = 0;
25001+ a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
25002+ a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
25003+ a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
25004+ a->mvd_h_dst_parent = NULL;
25005+ if (au_dbbot(a->parent) >= a->mvd_bdst)
25006+ a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
25007+ if (!a->mvd_h_dst_parent) {
25008+ err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
25009+ if (unlikely(err)) {
25010+ AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
25011+ goto out;
25012+ }
25013+ a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
25014+ }
25015+
25016+out:
25017+ AuTraceErr(err);
25018+ return err;
25019+}
25020+
25021+/* lock them all */
25022+static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
25023+{
25024+ int err;
25025+ struct dentry *h_trap;
25026+
25027+ a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
25028+ a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
25029+ err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
25030+ au_opt_udba(a->sb),
25031+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25032+ AuTraceErr(err);
25033+ if (unlikely(err)) {
25034+ AU_MVD_PR(dmsg, "pin_dst failed\n");
25035+ goto out;
25036+ }
25037+
25038+ if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
25039+ a->rename_lock = 0;
25040+ au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
25041+ AuLsc_DI_PARENT, AuLsc_I_PARENT3,
25042+ au_opt_udba(a->sb),
25043+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25044+ err = au_do_pin(&a->mvd_pin_src);
25045+ AuTraceErr(err);
25046+ a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
25047+ if (unlikely(err)) {
25048+ AU_MVD_PR(dmsg, "pin_src failed\n");
25049+ goto out_dst;
25050+ }
25051+ goto out; /* success */
25052+ }
25053+
25054+ a->rename_lock = 1;
25055+ au_pin_hdir_unlock(&a->mvd_pin_dst);
25056+ err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
25057+ au_opt_udba(a->sb),
25058+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25059+ AuTraceErr(err);
25060+ a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
25061+ if (unlikely(err)) {
25062+ AU_MVD_PR(dmsg, "pin_src failed\n");
25063+ au_pin_hdir_lock(&a->mvd_pin_dst);
25064+ goto out_dst;
25065+ }
25066+ au_pin_hdir_unlock(&a->mvd_pin_src);
25067+ h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25068+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
25069+ if (h_trap) {
25070+ err = (h_trap != a->mvd_h_src_parent);
25071+ if (err)
25072+ err = (h_trap != a->mvd_h_dst_parent);
25073+ }
25074+ BUG_ON(err); /* it should never happen */
25075+ if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
25076+ err = -EBUSY;
25077+ AuTraceErr(err);
25078+ vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25079+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
25080+ au_pin_hdir_lock(&a->mvd_pin_src);
25081+ au_unpin(&a->mvd_pin_src);
25082+ au_pin_hdir_lock(&a->mvd_pin_dst);
25083+ goto out_dst;
25084+ }
25085+ goto out; /* success */
25086+
25087+out_dst:
25088+ au_unpin(&a->mvd_pin_dst);
25089+out:
25090+ AuTraceErr(err);
25091+ return err;
25092+}
25093+
25094+static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
25095+{
25096+ if (!a->rename_lock)
25097+ au_unpin(&a->mvd_pin_src);
25098+ else {
25099+ vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25100+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
25101+ au_pin_hdir_lock(&a->mvd_pin_src);
25102+ au_unpin(&a->mvd_pin_src);
25103+ au_pin_hdir_lock(&a->mvd_pin_dst);
25104+ }
25105+ au_unpin(&a->mvd_pin_dst);
25106+}
25107+
25108+/* copy-down the file */
25109+static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
25110+{
25111+ int err;
25112+ struct au_cp_generic cpg = {
25113+ .dentry = a->dentry,
25114+ .bdst = a->mvd_bdst,
25115+ .bsrc = a->mvd_bsrc,
25116+ .len = -1,
25117+ .pin = &a->mvd_pin_dst,
25118+ .flags = AuCpup_DTIME | AuCpup_HOPEN
25119+ };
25120+
25121+ AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
25122+ if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25123+ au_fset_cpup(cpg.flags, OVERWRITE);
25124+ if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
25125+ au_fset_cpup(cpg.flags, RWDST);
25126+ err = au_sio_cpdown_simple(&cpg);
25127+ if (unlikely(err))
25128+ AU_MVD_PR(dmsg, "cpdown failed\n");
25129+
25130+ AuTraceErr(err);
25131+ return err;
25132+}
25133+
25134+/*
25135+ * unlink the whiteout on bdst if exist which may be created by UDBA while we
25136+ * were sleeping
25137+ */
25138+static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
25139+{
25140+ int err;
25141+ struct path h_path;
25142+ struct au_branch *br;
25143+ struct inode *delegated;
25144+
25145+ br = au_sbr(a->sb, a->mvd_bdst);
25146+ h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
25147+ err = PTR_ERR(h_path.dentry);
25148+ if (IS_ERR(h_path.dentry)) {
25149+ AU_MVD_PR(dmsg, "wh_lkup failed\n");
25150+ goto out;
25151+ }
25152+
25153+ err = 0;
25154+ if (d_is_positive(h_path.dentry)) {
25155+ h_path.mnt = au_br_mnt(br);
25156+ delegated = NULL;
25157+ err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
25158+ &delegated, /*force*/0);
25159+ if (unlikely(err == -EWOULDBLOCK)) {
25160+ pr_warn("cannot retry for NFSv4 delegation"
25161+ " for an internal unlink\n");
25162+ iput(delegated);
25163+ }
25164+ if (unlikely(err))
25165+ AU_MVD_PR(dmsg, "wh_unlink failed\n");
25166+ }
25167+ dput(h_path.dentry);
25168+
25169+out:
25170+ AuTraceErr(err);
25171+ return err;
25172+}
25173+
25174+/*
25175+ * unlink the topmost h_dentry
25176+ */
25177+static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
25178+{
25179+ int err;
25180+ struct path h_path;
25181+ struct inode *delegated;
25182+
25183+ h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
25184+ h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
25185+ delegated = NULL;
25186+ err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
25187+ if (unlikely(err == -EWOULDBLOCK)) {
25188+ pr_warn("cannot retry for NFSv4 delegation"
25189+ " for an internal unlink\n");
25190+ iput(delegated);
25191+ }
25192+ if (unlikely(err))
25193+ AU_MVD_PR(dmsg, "unlink failed\n");
25194+
25195+ AuTraceErr(err);
25196+ return err;
25197+}
25198+
25199+/* Since mvdown succeeded, we ignore an error of this function */
25200+static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
25201+{
25202+ int err;
25203+ struct au_branch *br;
25204+
25205+ a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
25206+ br = au_sbr(a->sb, a->mvd_bsrc);
25207+ err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
25208+ if (!err) {
25209+ br = au_sbr(a->sb, a->mvd_bdst);
25210+ a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
25211+ err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
25212+ }
25213+ if (!err)
25214+ a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
25215+ else
25216+ AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
25217+}
25218+
25219+/*
25220+ * copy-down the file and unlink the bsrc file.
25221+ * - unlink the bdst whout if exist
25222+ * - copy-down the file (with whtmp name and rename)
25223+ * - unlink the bsrc file
25224+ */
25225+static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
25226+{
25227+ int err;
25228+
25229+ err = au_do_mkdir(dmsg, a);
25230+ if (!err)
25231+ err = au_do_lock(dmsg, a);
25232+ if (unlikely(err))
25233+ goto out;
25234+
25235+ /*
25236+ * do not revert the activities we made on bdst since they should be
25237+ * harmless in aufs.
25238+ */
25239+
25240+ err = au_do_cpdown(dmsg, a);
25241+ if (!err)
25242+ err = au_do_unlink_wh(dmsg, a);
25243+ if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
25244+ err = au_do_unlink(dmsg, a);
25245+ if (unlikely(err))
25246+ goto out_unlock;
25247+
25248+ AuDbg("%pd2, 0x%x, %d --> %d\n",
25249+ a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
25250+ if (find_lower_writable(a) < 0)
25251+ a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
25252+
25253+ if (a->mvdown.flags & AUFS_MVDOWN_STFS)
25254+ au_do_stfs(dmsg, a);
25255+
25256+ /* maintain internal array */
25257+ if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
25258+ au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
25259+ au_set_dbtop(a->dentry, a->mvd_bdst);
25260+ au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
25261+ au_set_ibtop(a->inode, a->mvd_bdst);
25262+ } else {
25263+ /* hide the lower */
25264+ au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
25265+ au_set_dbbot(a->dentry, a->mvd_bsrc);
25266+ au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
25267+ au_set_ibbot(a->inode, a->mvd_bsrc);
25268+ }
25269+ if (au_dbbot(a->dentry) < a->mvd_bdst)
25270+ au_set_dbbot(a->dentry, a->mvd_bdst);
25271+ if (au_ibbot(a->inode) < a->mvd_bdst)
25272+ au_set_ibbot(a->inode, a->mvd_bdst);
25273+
25274+out_unlock:
25275+ au_do_unlock(dmsg, a);
25276+out:
25277+ AuTraceErr(err);
25278+ return err;
25279+}
25280+
25281+/* ---------------------------------------------------------------------- */
25282+
25283+/* make sure the file is idle */
25284+static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
25285+{
25286+ int err, plinked;
25287+
25288+ err = 0;
25289+ plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
25290+ if (au_dbtop(a->dentry) == a->mvd_bsrc
25291+ && au_dcount(a->dentry) == 1
25292+ && atomic_read(&a->inode->i_count) == 1
25293+ /* && a->mvd_h_src_inode->i_nlink == 1 */
25294+ && (!plinked || !au_plink_test(a->inode))
25295+ && a->inode->i_nlink == 1)
25296+ goto out;
25297+
25298+ err = -EBUSY;
25299+ AU_MVD_PR(dmsg,
25300+ "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
25301+ a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
25302+ atomic_read(&a->inode->i_count), a->inode->i_nlink,
25303+ a->mvd_h_src_inode->i_nlink,
25304+ plinked, plinked ? au_plink_test(a->inode) : 0);
25305+
25306+out:
25307+ AuTraceErr(err);
25308+ return err;
25309+}
25310+
25311+/* make sure the parent dir is fine */
25312+static int au_mvd_args_parent(const unsigned char dmsg,
25313+ struct au_mvd_args *a)
25314+{
25315+ int err;
25316+ aufs_bindex_t bindex;
25317+
25318+ err = 0;
25319+ if (unlikely(au_alive_dir(a->parent))) {
25320+ err = -ENOENT;
25321+ AU_MVD_PR(dmsg, "parent dir is dead\n");
25322+ goto out;
25323+ }
25324+
25325+ a->bopq = au_dbdiropq(a->parent);
25326+ bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
25327+ AuDbg("b%d\n", bindex);
25328+ if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
25329+ || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
25330+ err = -EINVAL;
25331+ a->mvd_errno = EAU_MVDOWN_OPAQUE;
25332+ AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
25333+ a->bopq, a->mvd_bdst);
25334+ }
25335+
25336+out:
25337+ AuTraceErr(err);
25338+ return err;
25339+}
25340+
25341+static int au_mvd_args_intermediate(const unsigned char dmsg,
25342+ struct au_mvd_args *a)
25343+{
25344+ int err;
25345+ struct au_dinfo *dinfo, *tmp;
25346+
25347+ /* lookup the next lower positive entry */
25348+ err = -ENOMEM;
25349+ tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
25350+ if (unlikely(!tmp))
25351+ goto out;
25352+
25353+ a->bfound = -1;
25354+ a->bwh = -1;
25355+ dinfo = au_di(a->dentry);
25356+ au_di_cp(tmp, dinfo);
25357+ au_di_swap(tmp, dinfo);
25358+
25359+ /* returns the number of positive dentries */
25360+ err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
25361+ /* AuLkup_IGNORE_PERM */ 0);
25362+ if (!err)
25363+ a->bwh = au_dbwh(a->dentry);
25364+ else if (err > 0)
25365+ a->bfound = au_dbtop(a->dentry);
25366+
25367+ au_di_swap(tmp, dinfo);
25368+ au_rw_write_unlock(&tmp->di_rwsem);
25369+ au_di_free(tmp);
25370+ if (unlikely(err < 0))
25371+ AU_MVD_PR(dmsg, "failed look-up lower\n");
25372+
25373+ /*
25374+ * here, we have these cases.
25375+ * bfound == -1
25376+ * no positive dentry under bsrc. there are more sub-cases.
25377+ * bwh < 0
25378+ * there no whiteout, we can safely move-down.
25379+ * bwh <= bsrc
25380+ * impossible
25381+ * bsrc < bwh && bwh < bdst
25382+ * there is a whiteout on RO branch. cannot proceed.
25383+ * bwh == bdst
25384+ * there is a whiteout on the RW target branch. it should
25385+ * be removed.
25386+ * bdst < bwh
25387+ * there is a whiteout somewhere unrelated branch.
25388+ * -1 < bfound && bfound <= bsrc
25389+ * impossible.
25390+ * bfound < bdst
25391+ * found, but it is on RO branch between bsrc and bdst. cannot
25392+ * proceed.
25393+ * bfound == bdst
25394+ * found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
25395+ * error.
25396+ * bdst < bfound
25397+ * found, after we create the file on bdst, it will be hidden.
25398+ */
25399+
25400+ AuDebugOn(a->bfound == -1
25401+ && a->bwh != -1
25402+ && a->bwh <= a->mvd_bsrc);
25403+ AuDebugOn(-1 < a->bfound
25404+ && a->bfound <= a->mvd_bsrc);
25405+
25406+ err = -EINVAL;
25407+ if (a->bfound == -1
25408+ && a->mvd_bsrc < a->bwh
25409+ && a->bwh != -1
25410+ && a->bwh < a->mvd_bdst) {
25411+ a->mvd_errno = EAU_MVDOWN_WHITEOUT;
25412+ AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
25413+ a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
25414+ goto out;
25415+ } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
25416+ a->mvd_errno = EAU_MVDOWN_UPPER;
25417+ AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
25418+ a->mvd_bdst, a->bfound);
25419+ goto out;
25420+ }
25421+
25422+ err = 0; /* success */
25423+
25424+out:
25425+ AuTraceErr(err);
25426+ return err;
25427+}
25428+
25429+static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
25430+{
25431+ int err;
25432+
25433+ err = 0;
25434+ if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25435+ && a->bfound == a->mvd_bdst)
25436+ err = -EEXIST;
25437+ AuTraceErr(err);
25438+ return err;
25439+}
25440+
25441+static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
25442+{
25443+ int err;
25444+ struct au_branch *br;
25445+
25446+ err = -EISDIR;
25447+ if (unlikely(S_ISDIR(a->inode->i_mode)))
25448+ goto out;
25449+
25450+ err = -EINVAL;
25451+ if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
25452+ a->mvd_bsrc = au_ibtop(a->inode);
25453+ else {
25454+ a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
25455+ if (unlikely(a->mvd_bsrc < 0
25456+ || (a->mvd_bsrc < au_dbtop(a->dentry)
25457+ || au_dbbot(a->dentry) < a->mvd_bsrc
25458+ || !au_h_dptr(a->dentry, a->mvd_bsrc))
25459+ || (a->mvd_bsrc < au_ibtop(a->inode)
25460+ || au_ibbot(a->inode) < a->mvd_bsrc
25461+ || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
25462+ a->mvd_errno = EAU_MVDOWN_NOUPPER;
25463+ AU_MVD_PR(dmsg, "no upper\n");
25464+ goto out;
25465+ }
25466+ }
25467+ if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
25468+ a->mvd_errno = EAU_MVDOWN_BOTTOM;
25469+ AU_MVD_PR(dmsg, "on the bottom\n");
25470+ goto out;
25471+ }
25472+ a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
25473+ br = au_sbr(a->sb, a->mvd_bsrc);
25474+ err = au_br_rdonly(br);
25475+ if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
25476+ if (unlikely(err))
25477+ goto out;
25478+ } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
25479+ || IS_APPEND(a->mvd_h_src_inode))) {
25480+ if (err)
25481+ a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
25482+ /* go on */
25483+ } else
25484+ goto out;
25485+
25486+ err = -EINVAL;
25487+ if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
25488+ a->mvd_bdst = find_lower_writable(a);
25489+ if (unlikely(a->mvd_bdst < 0)) {
25490+ a->mvd_errno = EAU_MVDOWN_BOTTOM;
25491+ AU_MVD_PR(dmsg, "no writable lower branch\n");
25492+ goto out;
25493+ }
25494+ } else {
25495+ a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
25496+ if (unlikely(a->mvd_bdst < 0
25497+ || au_sbbot(a->sb) < a->mvd_bdst)) {
25498+ a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
25499+ AU_MVD_PR(dmsg, "no lower brid\n");
25500+ goto out;
25501+ }
25502+ }
25503+
25504+ err = au_mvd_args_busy(dmsg, a);
25505+ if (!err)
25506+ err = au_mvd_args_parent(dmsg, a);
25507+ if (!err)
25508+ err = au_mvd_args_intermediate(dmsg, a);
25509+ if (!err)
25510+ err = au_mvd_args_exist(dmsg, a);
25511+ if (!err)
25512+ AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
25513+
25514+out:
25515+ AuTraceErr(err);
25516+ return err;
25517+}
25518+
25519+int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
25520+{
25521+ int err, e;
25522+ unsigned char dmsg;
25523+ struct au_mvd_args *args;
25524+ struct inode *inode;
25525+
25526+ inode = d_inode(dentry);
25527+ err = -EPERM;
25528+ if (unlikely(!capable(CAP_SYS_ADMIN)))
25529+ goto out;
25530+
25531+ err = -ENOMEM;
25532+ args = kmalloc(sizeof(*args), GFP_NOFS);
25533+ if (unlikely(!args))
25534+ goto out;
25535+
25536+ err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
25537+ if (!err)
25538+ /* VERIFY_WRITE */
25539+ err = !access_ok(uarg, sizeof(*uarg));
25540+ if (unlikely(err)) {
25541+ err = -EFAULT;
25542+ AuTraceErr(err);
25543+ goto out_free;
25544+ }
25545+ AuDbg("flags 0x%x\n", args->mvdown.flags);
25546+ args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
25547+ args->mvdown.au_errno = 0;
25548+ args->dentry = dentry;
25549+ args->inode = inode;
25550+ args->sb = dentry->d_sb;
25551+
25552+ err = -ENOENT;
25553+ dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
25554+ args->parent = dget_parent(dentry);
25555+ args->dir = d_inode(args->parent);
25556+ inode_lock_nested(args->dir, I_MUTEX_PARENT);
25557+ dput(args->parent);
25558+ if (unlikely(args->parent != dentry->d_parent)) {
25559+ AU_MVD_PR(dmsg, "parent dir is moved\n");
25560+ goto out_dir;
25561+ }
25562+
25563+ inode_lock_nested(inode, I_MUTEX_CHILD);
25564+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
25565+ if (unlikely(err))
25566+ goto out_inode;
25567+
25568+ di_write_lock_parent(args->parent);
25569+ err = au_mvd_args(dmsg, args);
25570+ if (unlikely(err))
25571+ goto out_parent;
25572+
25573+ err = au_do_mvdown(dmsg, args);
25574+ if (unlikely(err))
25575+ goto out_parent;
25576+
25577+ au_cpup_attr_timesizes(args->dir);
25578+ au_cpup_attr_timesizes(inode);
25579+ if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
25580+ au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
25581+ /* au_digen_dec(dentry); */
25582+
25583+out_parent:
25584+ di_write_unlock(args->parent);
25585+ aufs_read_unlock(dentry, AuLock_DW);
25586+out_inode:
25587+ inode_unlock(inode);
25588+out_dir:
25589+ inode_unlock(args->dir);
25590+out_free:
25591+ e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
25592+ if (unlikely(e))
25593+ err = -EFAULT;
25594+ au_kfree_rcu(args);
25595+out:
25596+ AuTraceErr(err);
25597+ return err;
25598+}
25599diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
25600--- /usr/share/empty/fs/aufs/opts.c 1970-01-01 01:00:00.000000000 +0100
25601+++ linux/fs/aufs/opts.c 2021-12-03 15:38:59.939980643 +0100
25602@@ -0,0 +1,1880 @@
25603+// SPDX-License-Identifier: GPL-2.0
25604+/*
25605+ * Copyright (C) 2005-2021 Junjiro R. Okajima
25606+ *
25607+ * This program, aufs is free software; you can redistribute it and/or modify
25608+ * it under the terms of the GNU General Public License as published by
25609+ * the Free Software Foundation; either version 2 of the License, or
25610+ * (at your option) any later version.
25611+ *
25612+ * This program is distributed in the hope that it will be useful,
25613+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
25614+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25615+ * GNU General Public License for more details.
25616+ *
25617+ * You should have received a copy of the GNU General Public License
25618+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
25619+ */
25620+
25621+/*
25622+ * mount options/flags
25623+ */
25624+
25625+#include <linux/namei.h>
25626+#include <linux/types.h> /* a distribution requires */
25627+#include <linux/parser.h>
25628+#include "aufs.h"
25629+
25630+/* ---------------------------------------------------------------------- */
25631+
25632+enum {
25633+ Opt_br,
25634+ Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
25635+ Opt_idel, Opt_imod,
25636+ Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
25637+ Opt_rdblk_def, Opt_rdhash_def,
25638+ Opt_xino, Opt_noxino,
25639+ Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
25640+ Opt_trunc_xino_path, Opt_itrunc_xino,
25641+ Opt_trunc_xib, Opt_notrunc_xib,
25642+ Opt_shwh, Opt_noshwh,
25643+ Opt_plink, Opt_noplink, Opt_list_plink,
25644+ Opt_udba,
25645+ Opt_dio, Opt_nodio,
25646+ Opt_diropq_a, Opt_diropq_w,
25647+ Opt_warn_perm, Opt_nowarn_perm,
25648+ Opt_wbr_copyup, Opt_wbr_create,
25649+ Opt_fhsm_sec,
25650+ Opt_verbose, Opt_noverbose,
25651+ Opt_sum, Opt_nosum, Opt_wsum,
25652+ Opt_dirperm1, Opt_nodirperm1,
25653+ Opt_dirren, Opt_nodirren,
25654+ Opt_acl, Opt_noacl,
25655+ Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
25656+};
25657+
25658+static match_table_t options = {
25659+ {Opt_br, "br=%s"},
25660+ {Opt_br, "br:%s"},
25661+
25662+ {Opt_add, "add=%d:%s"},
25663+ {Opt_add, "add:%d:%s"},
25664+ {Opt_add, "ins=%d:%s"},
25665+ {Opt_add, "ins:%d:%s"},
25666+ {Opt_append, "append=%s"},
25667+ {Opt_append, "append:%s"},
25668+ {Opt_prepend, "prepend=%s"},
25669+ {Opt_prepend, "prepend:%s"},
25670+
25671+ {Opt_del, "del=%s"},
25672+ {Opt_del, "del:%s"},
25673+ /* {Opt_idel, "idel:%d"}, */
25674+ {Opt_mod, "mod=%s"},
25675+ {Opt_mod, "mod:%s"},
25676+ /* {Opt_imod, "imod:%d:%s"}, */
25677+
25678+ {Opt_dirwh, "dirwh=%d"},
25679+
25680+ {Opt_xino, "xino=%s"},
25681+ {Opt_noxino, "noxino"},
25682+ {Opt_trunc_xino, "trunc_xino"},
25683+ {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
25684+ {Opt_notrunc_xino, "notrunc_xino"},
25685+ {Opt_trunc_xino_path, "trunc_xino=%s"},
25686+ {Opt_itrunc_xino, "itrunc_xino=%d"},
25687+ /* {Opt_zxino, "zxino=%s"}, */
25688+ {Opt_trunc_xib, "trunc_xib"},
25689+ {Opt_notrunc_xib, "notrunc_xib"},
25690+
25691+#ifdef CONFIG_PROC_FS
25692+ {Opt_plink, "plink"},
25693+#else
25694+ {Opt_ignore_silent, "plink"},
25695+#endif
25696+
25697+ {Opt_noplink, "noplink"},
25698+
25699+#ifdef CONFIG_AUFS_DEBUG
25700+ {Opt_list_plink, "list_plink"},
25701+#endif
25702+
25703+ {Opt_udba, "udba=%s"},
25704+
25705+ {Opt_dio, "dio"},
25706+ {Opt_nodio, "nodio"},
25707+
25708+#ifdef CONFIG_AUFS_DIRREN
25709+ {Opt_dirren, "dirren"},
25710+ {Opt_nodirren, "nodirren"},
25711+#else
25712+ {Opt_ignore, "dirren"},
25713+ {Opt_ignore_silent, "nodirren"},
25714+#endif
25715+
25716+#ifdef CONFIG_AUFS_FHSM
25717+ {Opt_fhsm_sec, "fhsm_sec=%d"},
25718+#else
25719+ {Opt_ignore, "fhsm_sec=%d"},
25720+#endif
25721+
25722+ {Opt_diropq_a, "diropq=always"},
25723+ {Opt_diropq_a, "diropq=a"},
25724+ {Opt_diropq_w, "diropq=whiteouted"},
25725+ {Opt_diropq_w, "diropq=w"},
25726+
25727+ {Opt_warn_perm, "warn_perm"},
25728+ {Opt_nowarn_perm, "nowarn_perm"},
25729+
25730+ /* keep them temporary */
25731+ {Opt_ignore_silent, "nodlgt"},
25732+ {Opt_ignore, "clean_plink"},
25733+
25734+#ifdef CONFIG_AUFS_SHWH
25735+ {Opt_shwh, "shwh"},
25736+#endif
25737+ {Opt_noshwh, "noshwh"},
25738+
25739+ {Opt_dirperm1, "dirperm1"},
25740+ {Opt_nodirperm1, "nodirperm1"},
25741+
25742+ {Opt_verbose, "verbose"},
25743+ {Opt_verbose, "v"},
25744+ {Opt_noverbose, "noverbose"},
25745+ {Opt_noverbose, "quiet"},
25746+ {Opt_noverbose, "q"},
25747+ {Opt_noverbose, "silent"},
25748+
25749+ {Opt_sum, "sum"},
25750+ {Opt_nosum, "nosum"},
25751+ {Opt_wsum, "wsum"},
25752+
25753+ {Opt_rdcache, "rdcache=%d"},
25754+ {Opt_rdblk, "rdblk=%d"},
25755+ {Opt_rdblk_def, "rdblk=def"},
25756+ {Opt_rdhash, "rdhash=%d"},
25757+ {Opt_rdhash_def, "rdhash=def"},
25758+
25759+ {Opt_wbr_create, "create=%s"},
25760+ {Opt_wbr_create, "create_policy=%s"},
25761+ {Opt_wbr_copyup, "cpup=%s"},
25762+ {Opt_wbr_copyup, "copyup=%s"},
25763+ {Opt_wbr_copyup, "copyup_policy=%s"},
25764+
25765+ /* generic VFS flag */
25766+#ifdef CONFIG_FS_POSIX_ACL
25767+ {Opt_acl, "acl"},
25768+ {Opt_noacl, "noacl"},
25769+#else
25770+ {Opt_ignore, "acl"},
25771+ {Opt_ignore_silent, "noacl"},
25772+#endif
25773+
25774+ /* internal use for the scripts */
25775+ {Opt_ignore_silent, "si=%s"},
25776+
25777+ {Opt_br, "dirs=%s"},
25778+ {Opt_ignore, "debug=%d"},
25779+ {Opt_ignore, "delete=whiteout"},
25780+ {Opt_ignore, "delete=all"},
25781+ {Opt_ignore, "imap=%s"},
25782+
25783+ /* temporary workaround, due to old mount(8)? */
25784+ {Opt_ignore_silent, "relatime"},
25785+
25786+ {Opt_err, NULL}
25787+};
25788+
25789+/* ---------------------------------------------------------------------- */
25790+
25791+static const char *au_parser_pattern(int val, match_table_t tbl)
25792+{
25793+ struct match_token *p;
25794+
25795+ p = tbl;
25796+ while (p->pattern) {
25797+ if (p->token == val)
25798+ return p->pattern;
25799+ p++;
25800+ }
25801+ BUG();
25802+ return "??";
25803+}
25804+
25805+static const char *au_optstr(int *val, match_table_t tbl)
25806+{
25807+ struct match_token *p;
25808+ int v;
25809+
25810+ v = *val;
25811+ if (!v)
25812+ goto out;
25813+ p = tbl;
25814+ while (p->pattern) {
25815+ if (p->token
25816+ && (v & p->token) == p->token) {
25817+ *val &= ~p->token;
25818+ return p->pattern;
25819+ }
25820+ p++;
25821+ }
25822+
25823+out:
25824+ return NULL;
25825+}
25826+
25827+/* ---------------------------------------------------------------------- */
25828+
25829+static match_table_t brperm = {
25830+ {AuBrPerm_RO, AUFS_BRPERM_RO},
25831+ {AuBrPerm_RR, AUFS_BRPERM_RR},
25832+ {AuBrPerm_RW, AUFS_BRPERM_RW},
25833+ {0, NULL}
25834+};
25835+
25836+static match_table_t brattr = {
25837+ /* general */
25838+ {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
25839+ {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
25840+ /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
25841+ {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
25842+#ifdef CONFIG_AUFS_FHSM
25843+ {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
25844+#endif
25845+#ifdef CONFIG_AUFS_XATTR
25846+ {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
25847+ {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
25848+ {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
25849+ {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
25850+ {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
25851+ {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
25852+#endif
25853+
25854+ /* ro/rr branch */
25855+ {AuBrRAttr_WH, AUFS_BRRATTR_WH},
25856+
25857+ /* rw branch */
25858+ {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
25859+ {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
25860+
25861+ {0, NULL}
25862+};
25863+
25864+static int br_attr_val(char *str, match_table_t table, substring_t args[])
25865+{
25866+ int attr, v;
25867+ char *p;
25868+
25869+ attr = 0;
25870+ do {
25871+ p = strchr(str, '+');
25872+ if (p)
25873+ *p = 0;
25874+ v = match_token(str, table, args);
25875+ if (v) {
25876+ if (v & AuBrAttr_CMOO_Mask)
25877+ attr &= ~AuBrAttr_CMOO_Mask;
25878+ attr |= v;
25879+ } else {
25880+ if (p)
25881+ *p = '+';
25882+ pr_warn("ignored branch attribute %s\n", str);
25883+ break;
25884+ }
25885+ if (p)
25886+ str = p + 1;
25887+ } while (p);
25888+
25889+ return attr;
25890+}
25891+
25892+static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
25893+{
25894+ int sz;
25895+ const char *p;
25896+ char *q;
25897+
25898+ q = str->a;
25899+ *q = 0;
25900+ p = au_optstr(&perm, brattr);
25901+ if (p) {
25902+ sz = strlen(p);
25903+ memcpy(q, p, sz + 1);
25904+ q += sz;
25905+ } else
25906+ goto out;
25907+
25908+ do {
25909+ p = au_optstr(&perm, brattr);
25910+ if (p) {
25911+ *q++ = '+';
25912+ sz = strlen(p);
25913+ memcpy(q, p, sz + 1);
25914+ q += sz;
25915+ }
25916+ } while (p);
25917+
25918+out:
25919+ return q - str->a;
25920+}
25921+
25922+static int noinline_for_stack br_perm_val(char *perm)
25923+{
25924+ int val, bad, sz;
25925+ char *p;
25926+ substring_t args[MAX_OPT_ARGS];
25927+ au_br_perm_str_t attr;
25928+
25929+ p = strchr(perm, '+');
25930+ if (p)
25931+ *p = 0;
25932+ val = match_token(perm, brperm, args);
25933+ if (!val) {
25934+ if (p)
25935+ *p = '+';
25936+ pr_warn("ignored branch permission %s\n", perm);
25937+ val = AuBrPerm_RO;
25938+ goto out;
25939+ }
25940+ if (!p)
25941+ goto out;
25942+
25943+ val |= br_attr_val(p + 1, brattr, args);
25944+
25945+ bad = 0;
25946+ switch (val & AuBrPerm_Mask) {
25947+ case AuBrPerm_RO:
25948+ case AuBrPerm_RR:
25949+ bad = val & AuBrWAttr_Mask;
25950+ val &= ~AuBrWAttr_Mask;
25951+ break;
25952+ case AuBrPerm_RW:
25953+ bad = val & AuBrRAttr_Mask;
25954+ val &= ~AuBrRAttr_Mask;
25955+ break;
25956+ }
25957+
25958+ /*
25959+ * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
25960+ * does not treat it as an error, just warning.
25961+ * this is a tiny guard for the user operation.
25962+ */
25963+ if (val & AuBrAttr_UNPIN) {
25964+ bad |= AuBrAttr_UNPIN;
25965+ val &= ~AuBrAttr_UNPIN;
25966+ }
25967+
25968+ if (unlikely(bad)) {
25969+ sz = au_do_optstr_br_attr(&attr, bad);
25970+ AuDebugOn(!sz);
25971+ pr_warn("ignored branch attribute %s\n", attr.a);
25972+ }
25973+
25974+out:
25975+ return val;
25976+}
25977+
25978+void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
25979+{
25980+ au_br_perm_str_t attr;
25981+ const char *p;
25982+ char *q;
25983+ int sz;
25984+
25985+ q = str->a;
25986+ p = au_optstr(&perm, brperm);
25987+ AuDebugOn(!p || !*p);
25988+ sz = strlen(p);
25989+ memcpy(q, p, sz + 1);
25990+ q += sz;
25991+
25992+ sz = au_do_optstr_br_attr(&attr, perm);
25993+ if (sz) {
25994+ *q++ = '+';
25995+ memcpy(q, attr.a, sz + 1);
25996+ }
25997+
25998+ AuDebugOn(strlen(str->a) >= sizeof(str->a));
25999+}
26000+
26001+/* ---------------------------------------------------------------------- */
26002+
26003+static match_table_t udbalevel = {
26004+ {AuOpt_UDBA_REVAL, "reval"},
26005+ {AuOpt_UDBA_NONE, "none"},
26006+#ifdef CONFIG_AUFS_HNOTIFY
26007+ {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
26008+#ifdef CONFIG_AUFS_HFSNOTIFY
26009+ {AuOpt_UDBA_HNOTIFY, "fsnotify"},
26010+#endif
26011+#endif
26012+ {-1, NULL}
26013+};
26014+
26015+static int noinline_for_stack udba_val(char *str)
26016+{
26017+ substring_t args[MAX_OPT_ARGS];
26018+
26019+ return match_token(str, udbalevel, args);
26020+}
26021+
26022+const char *au_optstr_udba(int udba)
26023+{
26024+ return au_parser_pattern(udba, udbalevel);
26025+}
26026+
26027+/* ---------------------------------------------------------------------- */
26028+
26029+static match_table_t au_wbr_create_policy = {
26030+ {AuWbrCreate_TDP, "tdp"},
26031+ {AuWbrCreate_TDP, "top-down-parent"},
26032+ {AuWbrCreate_RR, "rr"},
26033+ {AuWbrCreate_RR, "round-robin"},
26034+ {AuWbrCreate_MFS, "mfs"},
26035+ {AuWbrCreate_MFS, "most-free-space"},
26036+ {AuWbrCreate_MFSV, "mfs:%d"},
26037+ {AuWbrCreate_MFSV, "most-free-space:%d"},
26038+
26039+ /* top-down regardless the parent, and then mfs */
26040+ {AuWbrCreate_TDMFS, "tdmfs:%d"},
26041+ {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
26042+
26043+ {AuWbrCreate_MFSRR, "mfsrr:%d"},
26044+ {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
26045+ {AuWbrCreate_PMFS, "pmfs"},
26046+ {AuWbrCreate_PMFSV, "pmfs:%d"},
26047+ {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
26048+ {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
26049+
26050+ {-1, NULL}
26051+};
26052+
26053+static int au_wbr_mfs_wmark(substring_t *arg, char *str,
26054+ struct au_opt_wbr_create *create)
26055+{
26056+ int err;
26057+ unsigned long long ull;
26058+
26059+ err = 0;
26060+ if (!match_u64(arg, &ull))
26061+ create->mfsrr_watermark = ull;
26062+ else {
26063+ pr_err("bad integer in %s\n", str);
26064+ err = -EINVAL;
26065+ }
26066+
26067+ return err;
26068+}
26069+
26070+static int au_wbr_mfs_sec(substring_t *arg, char *str,
26071+ struct au_opt_wbr_create *create)
26072+{
26073+ int n, err;
26074+
26075+ err = 0;
26076+ if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
26077+ create->mfs_second = n;
26078+ else {
26079+ pr_err("bad integer in %s\n", str);
26080+ err = -EINVAL;
26081+ }
26082+
26083+ return err;
26084+}
26085+
26086+static int noinline_for_stack
26087+au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
26088+{
26089+ int err, e;
26090+ substring_t args[MAX_OPT_ARGS];
26091+
26092+ err = match_token(str, au_wbr_create_policy, args);
26093+ create->wbr_create = err;
26094+ switch (err) {
26095+ case AuWbrCreate_MFSRRV:
26096+ case AuWbrCreate_TDMFSV:
26097+ case AuWbrCreate_PMFSRRV:
26098+ e = au_wbr_mfs_wmark(&args[0], str, create);
26099+ if (!e)
26100+ e = au_wbr_mfs_sec(&args[1], str, create);
26101+ if (unlikely(e))
26102+ err = e;
26103+ break;
26104+ case AuWbrCreate_MFSRR:
26105+ case AuWbrCreate_TDMFS:
26106+ case AuWbrCreate_PMFSRR:
26107+ e = au_wbr_mfs_wmark(&args[0], str, create);
26108+ if (unlikely(e)) {
26109+ err = e;
26110+ break;
26111+ }
26112+ fallthrough;
26113+ case AuWbrCreate_MFS:
26114+ case AuWbrCreate_PMFS:
26115+ create->mfs_second = AUFS_MFS_DEF_SEC;
26116+ break;
26117+ case AuWbrCreate_MFSV:
26118+ case AuWbrCreate_PMFSV:
26119+ e = au_wbr_mfs_sec(&args[0], str, create);
26120+ if (unlikely(e))
26121+ err = e;
26122+ break;
26123+ }
26124+
26125+ return err;
26126+}
26127+
26128+const char *au_optstr_wbr_create(int wbr_create)
26129+{
26130+ return au_parser_pattern(wbr_create, au_wbr_create_policy);
26131+}
26132+
26133+static match_table_t au_wbr_copyup_policy = {
26134+ {AuWbrCopyup_TDP, "tdp"},
26135+ {AuWbrCopyup_TDP, "top-down-parent"},
26136+ {AuWbrCopyup_BUP, "bup"},
26137+ {AuWbrCopyup_BUP, "bottom-up-parent"},
26138+ {AuWbrCopyup_BU, "bu"},
26139+ {AuWbrCopyup_BU, "bottom-up"},
26140+ {-1, NULL}
26141+};
26142+
26143+static int noinline_for_stack au_wbr_copyup_val(char *str)
26144+{
26145+ substring_t args[MAX_OPT_ARGS];
26146+
26147+ return match_token(str, au_wbr_copyup_policy, args);
26148+}
26149+
26150+const char *au_optstr_wbr_copyup(int wbr_copyup)
26151+{
26152+ return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
26153+}
26154+
26155+/* ---------------------------------------------------------------------- */
26156+
26157+static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
26158+
26159+static void dump_opts(struct au_opts *opts)
26160+{
26161+#ifdef CONFIG_AUFS_DEBUG
26162+ /* reduce stack space */
26163+ union {
26164+ struct au_opt_add *add;
26165+ struct au_opt_del *del;
26166+ struct au_opt_mod *mod;
26167+ struct au_opt_xino *xino;
26168+ struct au_opt_xino_itrunc *xino_itrunc;
26169+ struct au_opt_wbr_create *create;
26170+ } u;
26171+ struct au_opt *opt;
26172+
26173+ opt = opts->opt;
26174+ while (opt->type != Opt_tail) {
26175+ switch (opt->type) {
26176+ case Opt_add:
26177+ u.add = &opt->add;
26178+ AuDbg("add {b%d, %s, 0x%x, %p}\n",
26179+ u.add->bindex, u.add->pathname, u.add->perm,
26180+ u.add->path.dentry);
26181+ break;
26182+ case Opt_del:
26183+ case Opt_idel:
26184+ u.del = &opt->del;
26185+ AuDbg("del {%s, %p}\n",
26186+ u.del->pathname, u.del->h_path.dentry);
26187+ break;
26188+ case Opt_mod:
26189+ case Opt_imod:
26190+ u.mod = &opt->mod;
26191+ AuDbg("mod {%s, 0x%x, %p}\n",
26192+ u.mod->path, u.mod->perm, u.mod->h_root);
26193+ break;
26194+ case Opt_append:
26195+ u.add = &opt->add;
26196+ AuDbg("append {b%d, %s, 0x%x, %p}\n",
26197+ u.add->bindex, u.add->pathname, u.add->perm,
26198+ u.add->path.dentry);
26199+ break;
26200+ case Opt_prepend:
26201+ u.add = &opt->add;
26202+ AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
26203+ u.add->bindex, u.add->pathname, u.add->perm,
26204+ u.add->path.dentry);
26205+ break;
26206+ case Opt_dirwh:
26207+ AuDbg("dirwh %d\n", opt->dirwh);
26208+ break;
26209+ case Opt_rdcache:
26210+ AuDbg("rdcache %d\n", opt->rdcache);
26211+ break;
26212+ case Opt_rdblk:
26213+ AuDbg("rdblk %u\n", opt->rdblk);
26214+ break;
26215+ case Opt_rdblk_def:
26216+ AuDbg("rdblk_def\n");
26217+ break;
26218+ case Opt_rdhash:
26219+ AuDbg("rdhash %u\n", opt->rdhash);
26220+ break;
26221+ case Opt_rdhash_def:
26222+ AuDbg("rdhash_def\n");
26223+ break;
26224+ case Opt_xino:
26225+ u.xino = &opt->xino;
26226+ AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
26227+ break;
26228+ case Opt_trunc_xino:
26229+ AuLabel(trunc_xino);
26230+ break;
26231+ case Opt_notrunc_xino:
26232+ AuLabel(notrunc_xino);
26233+ break;
26234+ case Opt_trunc_xino_path:
26235+ case Opt_itrunc_xino:
26236+ u.xino_itrunc = &opt->xino_itrunc;
26237+ AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
26238+ break;
26239+ case Opt_noxino:
26240+ AuLabel(noxino);
26241+ break;
26242+ case Opt_trunc_xib:
26243+ AuLabel(trunc_xib);
26244+ break;
26245+ case Opt_notrunc_xib:
26246+ AuLabel(notrunc_xib);
26247+ break;
26248+ case Opt_shwh:
26249+ AuLabel(shwh);
26250+ break;
26251+ case Opt_noshwh:
26252+ AuLabel(noshwh);
26253+ break;
26254+ case Opt_dirperm1:
26255+ AuLabel(dirperm1);
26256+ break;
26257+ case Opt_nodirperm1:
26258+ AuLabel(nodirperm1);
26259+ break;
26260+ case Opt_plink:
26261+ AuLabel(plink);
26262+ break;
26263+ case Opt_noplink:
26264+ AuLabel(noplink);
26265+ break;
26266+ case Opt_list_plink:
26267+ AuLabel(list_plink);
26268+ break;
26269+ case Opt_udba:
26270+ AuDbg("udba %d, %s\n",
26271+ opt->udba, au_optstr_udba(opt->udba));
26272+ break;
26273+ case Opt_dio:
26274+ AuLabel(dio);
26275+ break;
26276+ case Opt_nodio:
26277+ AuLabel(nodio);
26278+ break;
26279+ case Opt_diropq_a:
26280+ AuLabel(diropq_a);
26281+ break;
26282+ case Opt_diropq_w:
26283+ AuLabel(diropq_w);
26284+ break;
26285+ case Opt_warn_perm:
26286+ AuLabel(warn_perm);
26287+ break;
26288+ case Opt_nowarn_perm:
26289+ AuLabel(nowarn_perm);
26290+ break;
26291+ case Opt_verbose:
26292+ AuLabel(verbose);
26293+ break;
26294+ case Opt_noverbose:
26295+ AuLabel(noverbose);
26296+ break;
26297+ case Opt_sum:
26298+ AuLabel(sum);
26299+ break;
26300+ case Opt_nosum:
26301+ AuLabel(nosum);
26302+ break;
26303+ case Opt_wsum:
26304+ AuLabel(wsum);
26305+ break;
26306+ case Opt_wbr_create:
26307+ u.create = &opt->wbr_create;
26308+ AuDbg("create %d, %s\n", u.create->wbr_create,
26309+ au_optstr_wbr_create(u.create->wbr_create));
26310+ switch (u.create->wbr_create) {
26311+ case AuWbrCreate_MFSV:
26312+ case AuWbrCreate_PMFSV:
26313+ AuDbg("%d sec\n", u.create->mfs_second);
26314+ break;
26315+ case AuWbrCreate_MFSRR:
26316+ case AuWbrCreate_TDMFS:
26317+ AuDbg("%llu watermark\n",
26318+ u.create->mfsrr_watermark);
26319+ break;
26320+ case AuWbrCreate_MFSRRV:
26321+ case AuWbrCreate_TDMFSV:
26322+ case AuWbrCreate_PMFSRRV:
26323+ AuDbg("%llu watermark, %d sec\n",
26324+ u.create->mfsrr_watermark,
26325+ u.create->mfs_second);
26326+ break;
26327+ }
26328+ break;
26329+ case Opt_wbr_copyup:
26330+ AuDbg("copyup %d, %s\n", opt->wbr_copyup,
26331+ au_optstr_wbr_copyup(opt->wbr_copyup));
26332+ break;
26333+ case Opt_fhsm_sec:
26334+ AuDbg("fhsm_sec %u\n", opt->fhsm_second);
26335+ break;
26336+ case Opt_dirren:
26337+ AuLabel(dirren);
26338+ break;
26339+ case Opt_nodirren:
26340+ AuLabel(nodirren);
26341+ break;
26342+ case Opt_acl:
26343+ AuLabel(acl);
26344+ break;
26345+ case Opt_noacl:
26346+ AuLabel(noacl);
26347+ break;
26348+ default:
26349+ BUG();
26350+ }
26351+ opt++;
26352+ }
26353+#endif
26354+}
26355+
26356+void au_opts_free(struct au_opts *opts)
26357+{
26358+ struct au_opt *opt;
26359+
26360+ opt = opts->opt;
26361+ while (opt->type != Opt_tail) {
26362+ switch (opt->type) {
26363+ case Opt_add:
26364+ case Opt_append:
26365+ case Opt_prepend:
26366+ path_put(&opt->add.path);
26367+ break;
26368+ case Opt_del:
26369+ case Opt_idel:
26370+ path_put(&opt->del.h_path);
26371+ break;
26372+ case Opt_mod:
26373+ case Opt_imod:
26374+ dput(opt->mod.h_root);
26375+ break;
26376+ case Opt_xino:
26377+ fput(opt->xino.file);
26378+ break;
26379+ }
26380+ opt++;
26381+ }
26382+}
26383+
26384+static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
26385+ aufs_bindex_t bindex)
26386+{
26387+ int err;
26388+ struct au_opt_add *add = &opt->add;
26389+ char *p;
26390+
26391+ add->bindex = bindex;
26392+ add->perm = AuBrPerm_RO;
26393+ add->pathname = opt_str;
26394+ p = strchr(opt_str, '=');
26395+ if (p) {
26396+ *p++ = 0;
26397+ if (*p)
26398+ add->perm = br_perm_val(p);
26399+ }
26400+
26401+ err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
26402+ if (!err) {
26403+ if (!p) {
26404+ add->perm = AuBrPerm_RO;
26405+ if (au_test_fs_rr(add->path.dentry->d_sb))
26406+ add->perm = AuBrPerm_RR;
26407+ else if (!bindex && !(sb_flags & SB_RDONLY))
26408+ add->perm = AuBrPerm_RW;
26409+ }
26410+ opt->type = Opt_add;
26411+ goto out;
26412+ }
26413+ pr_err("lookup failed %s (%d)\n", add->pathname, err);
26414+ err = -EINVAL;
26415+
26416+out:
26417+ return err;
26418+}
26419+
26420+static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
26421+{
26422+ int err;
26423+
26424+ del->pathname = args[0].from;
26425+ AuDbg("del path %s\n", del->pathname);
26426+
26427+ err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
26428+ if (unlikely(err))
26429+ pr_err("lookup failed %s (%d)\n", del->pathname, err);
26430+
26431+ return err;
26432+}
26433+
26434+#if 0 /* reserved for future use */
26435+static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
26436+ struct au_opt_del *del, substring_t args[])
26437+{
26438+ int err;
26439+ struct dentry *root;
26440+
26441+ err = -EINVAL;
26442+ root = sb->s_root;
26443+ aufs_read_lock(root, AuLock_FLUSH);
26444+ if (bindex < 0 || au_sbbot(sb) < bindex) {
26445+ pr_err("out of bounds, %d\n", bindex);
26446+ goto out;
26447+ }
26448+
26449+ err = 0;
26450+ del->h_path.dentry = dget(au_h_dptr(root, bindex));
26451+ del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
26452+
26453+out:
26454+ aufs_read_unlock(root, !AuLock_IR);
26455+ return err;
26456+}
26457+#endif
26458+
26459+static int noinline_for_stack
26460+au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
26461+{
26462+ int err;
26463+ struct path path;
26464+ char *p;
26465+
26466+ err = -EINVAL;
26467+ mod->path = args[0].from;
26468+ p = strchr(mod->path, '=');
26469+ if (unlikely(!p)) {
26470+ pr_err("no permission %s\n", args[0].from);
26471+ goto out;
26472+ }
26473+
26474+ *p++ = 0;
26475+ err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
26476+ if (unlikely(err)) {
26477+ pr_err("lookup failed %s (%d)\n", mod->path, err);
26478+ goto out;
26479+ }
26480+
26481+ mod->perm = br_perm_val(p);
26482+ AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
26483+ mod->h_root = dget(path.dentry);
26484+ path_put(&path);
26485+
26486+out:
26487+ return err;
26488+}
26489+
26490+#if 0 /* reserved for future use */
26491+static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
26492+ struct au_opt_mod *mod, substring_t args[])
26493+{
26494+ int err;
26495+ struct dentry *root;
26496+
26497+ err = -EINVAL;
26498+ root = sb->s_root;
26499+ aufs_read_lock(root, AuLock_FLUSH);
26500+ if (bindex < 0 || au_sbbot(sb) < bindex) {
26501+ pr_err("out of bounds, %d\n", bindex);
26502+ goto out;
26503+ }
26504+
26505+ err = 0;
26506+ mod->perm = br_perm_val(args[1].from);
26507+ AuDbg("mod path %s, perm 0x%x, %s\n",
26508+ mod->path, mod->perm, args[1].from);
26509+ mod->h_root = dget(au_h_dptr(root, bindex));
26510+
26511+out:
26512+ aufs_read_unlock(root, !AuLock_IR);
26513+ return err;
26514+}
26515+#endif
26516+
26517+static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
26518+ substring_t args[])
26519+{
26520+ int err;
26521+ struct file *file;
26522+
26523+ file = au_xino_create(sb, args[0].from, /*silent*/0, /*wbrtop*/0);
26524+ err = PTR_ERR(file);
26525+ if (IS_ERR(file))
26526+ goto out;
26527+
26528+ err = -EINVAL;
26529+ if (unlikely(file->f_path.dentry->d_sb == sb)) {
26530+ fput(file);
26531+ pr_err("%s must be outside\n", args[0].from);
26532+ goto out;
26533+ }
26534+
26535+ err = 0;
26536+ xino->file = file;
26537+ xino->path = args[0].from;
26538+
26539+out:
26540+ return err;
26541+}
26542+
26543+static int noinline_for_stack
26544+au_opts_parse_xino_itrunc_path(struct super_block *sb,
26545+ struct au_opt_xino_itrunc *xino_itrunc,
26546+ substring_t args[])
26547+{
26548+ int err;
26549+ aufs_bindex_t bbot, bindex;
26550+ struct path path;
26551+ struct dentry *root;
26552+
26553+ err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
26554+ if (unlikely(err)) {
26555+ pr_err("lookup failed %s (%d)\n", args[0].from, err);
26556+ goto out;
26557+ }
26558+
26559+ xino_itrunc->bindex = -1;
26560+ root = sb->s_root;
26561+ aufs_read_lock(root, AuLock_FLUSH);
26562+ bbot = au_sbbot(sb);
26563+ for (bindex = 0; bindex <= bbot; bindex++) {
26564+ if (au_h_dptr(root, bindex) == path.dentry) {
26565+ xino_itrunc->bindex = bindex;
26566+ break;
26567+ }
26568+ }
26569+ aufs_read_unlock(root, !AuLock_IR);
26570+ path_put(&path);
26571+
26572+ if (unlikely(xino_itrunc->bindex < 0)) {
26573+ pr_err("no such branch %s\n", args[0].from);
26574+ err = -EINVAL;
26575+ }
26576+
26577+out:
26578+ return err;
26579+}
26580+
26581+/* called without aufs lock */
26582+int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
26583+{
26584+ int err, n, token;
26585+ aufs_bindex_t bindex;
26586+ unsigned char skipped;
26587+ struct dentry *root;
26588+ struct au_opt *opt, *opt_tail;
26589+ char *opt_str;
26590+ /* reduce the stack space */
26591+ union {
26592+ struct au_opt_xino_itrunc *xino_itrunc;
26593+ struct au_opt_wbr_create *create;
26594+ } u;
26595+ struct {
26596+ substring_t args[MAX_OPT_ARGS];
26597+ } *a;
26598+
26599+ err = -ENOMEM;
26600+ a = kmalloc(sizeof(*a), GFP_NOFS);
26601+ if (unlikely(!a))
26602+ goto out;
26603+
26604+ root = sb->s_root;
26605+ err = 0;
26606+ bindex = 0;
26607+ opt = opts->opt;
26608+ opt_tail = opt + opts->max_opt - 1;
26609+ opt->type = Opt_tail;
26610+ while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
26611+ err = -EINVAL;
26612+ skipped = 0;
26613+ token = match_token(opt_str, options, a->args);
26614+ switch (token) {
26615+ case Opt_br:
26616+ err = 0;
26617+ while (!err && (opt_str = strsep(&a->args[0].from, ":"))
26618+ && *opt_str) {
26619+ err = opt_add(opt, opt_str, opts->sb_flags,
26620+ bindex++);
26621+ if (unlikely(!err && ++opt > opt_tail)) {
26622+ err = -E2BIG;
26623+ break;
26624+ }
26625+ opt->type = Opt_tail;
26626+ skipped = 1;
26627+ }
26628+ break;
26629+ case Opt_add:
26630+ if (unlikely(match_int(&a->args[0], &n))) {
26631+ pr_err("bad integer in %s\n", opt_str);
26632+ break;
26633+ }
26634+ bindex = n;
26635+ err = opt_add(opt, a->args[1].from, opts->sb_flags,
26636+ bindex);
26637+ if (!err)
26638+ opt->type = token;
26639+ break;
26640+ case Opt_append:
26641+ err = opt_add(opt, a->args[0].from, opts->sb_flags,
26642+ /*dummy bindex*/1);
26643+ if (!err)
26644+ opt->type = token;
26645+ break;
26646+ case Opt_prepend:
26647+ err = opt_add(opt, a->args[0].from, opts->sb_flags,
26648+ /*bindex*/0);
26649+ if (!err)
26650+ opt->type = token;
26651+ break;
26652+ case Opt_del:
26653+ err = au_opts_parse_del(&opt->del, a->args);
26654+ if (!err)
26655+ opt->type = token;
26656+ break;
26657+#if 0 /* reserved for future use */
26658+ case Opt_idel:
26659+ del->pathname = "(indexed)";
26660+ if (unlikely(match_int(&args[0], &n))) {
26661+ pr_err("bad integer in %s\n", opt_str);
26662+ break;
26663+ }
26664+ err = au_opts_parse_idel(sb, n, &opt->del, a->args);
26665+ if (!err)
26666+ opt->type = token;
26667+ break;
26668+#endif
26669+ case Opt_mod:
26670+ err = au_opts_parse_mod(&opt->mod, a->args);
26671+ if (!err)
26672+ opt->type = token;
26673+ break;
26674+#ifdef IMOD /* reserved for future use */
26675+ case Opt_imod:
26676+ u.mod->path = "(indexed)";
26677+ if (unlikely(match_int(&a->args[0], &n))) {
26678+ pr_err("bad integer in %s\n", opt_str);
26679+ break;
26680+ }
26681+ err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
26682+ if (!err)
26683+ opt->type = token;
26684+ break;
26685+#endif
26686+ case Opt_xino:
26687+ err = au_opts_parse_xino(sb, &opt->xino, a->args);
26688+ if (!err)
26689+ opt->type = token;
26690+ break;
26691+
26692+ case Opt_trunc_xino_path:
26693+ err = au_opts_parse_xino_itrunc_path
26694+ (sb, &opt->xino_itrunc, a->args);
26695+ if (!err)
26696+ opt->type = token;
26697+ break;
26698+
26699+ case Opt_itrunc_xino:
26700+ u.xino_itrunc = &opt->xino_itrunc;
26701+ if (unlikely(match_int(&a->args[0], &n))) {
26702+ pr_err("bad integer in %s\n", opt_str);
26703+ break;
26704+ }
26705+ u.xino_itrunc->bindex = n;
26706+ aufs_read_lock(root, AuLock_FLUSH);
26707+ if (n < 0 || au_sbbot(sb) < n) {
26708+ pr_err("out of bounds, %d\n", n);
26709+ aufs_read_unlock(root, !AuLock_IR);
26710+ break;
26711+ }
26712+ aufs_read_unlock(root, !AuLock_IR);
26713+ err = 0;
26714+ opt->type = token;
26715+ break;
26716+
26717+ case Opt_dirwh:
26718+ if (unlikely(match_int(&a->args[0], &opt->dirwh)))
26719+ break;
26720+ err = 0;
26721+ opt->type = token;
26722+ break;
26723+
26724+ case Opt_rdcache:
26725+ if (unlikely(match_int(&a->args[0], &n))) {
26726+ pr_err("bad integer in %s\n", opt_str);
26727+ break;
26728+ }
26729+ if (unlikely(n > AUFS_RDCACHE_MAX)) {
26730+ pr_err("rdcache must be smaller than %d\n",
26731+ AUFS_RDCACHE_MAX);
26732+ break;
26733+ }
26734+ opt->rdcache = n;
26735+ err = 0;
26736+ opt->type = token;
26737+ break;
26738+ case Opt_rdblk:
26739+ if (unlikely(match_int(&a->args[0], &n)
26740+ || n < 0
26741+ || n > KMALLOC_MAX_SIZE)) {
26742+ pr_err("bad integer in %s\n", opt_str);
26743+ break;
26744+ }
26745+ if (unlikely(n && n < NAME_MAX)) {
26746+ pr_err("rdblk must be larger than %d\n",
26747+ NAME_MAX);
26748+ break;
26749+ }
26750+ opt->rdblk = n;
26751+ err = 0;
26752+ opt->type = token;
26753+ break;
26754+ case Opt_rdhash:
26755+ if (unlikely(match_int(&a->args[0], &n)
26756+ || n < 0
26757+ || n * sizeof(struct hlist_head)
26758+ > KMALLOC_MAX_SIZE)) {
26759+ pr_err("bad integer in %s\n", opt_str);
26760+ break;
26761+ }
26762+ opt->rdhash = n;
26763+ err = 0;
26764+ opt->type = token;
26765+ break;
26766+
26767+ case Opt_trunc_xino:
26768+ case Opt_notrunc_xino:
26769+ case Opt_noxino:
26770+ case Opt_trunc_xib:
26771+ case Opt_notrunc_xib:
26772+ case Opt_shwh:
26773+ case Opt_noshwh:
26774+ case Opt_dirperm1:
26775+ case Opt_nodirperm1:
26776+ case Opt_plink:
26777+ case Opt_noplink:
26778+ case Opt_list_plink:
26779+ case Opt_dio:
26780+ case Opt_nodio:
26781+ case Opt_diropq_a:
26782+ case Opt_diropq_w:
26783+ case Opt_warn_perm:
26784+ case Opt_nowarn_perm:
26785+ case Opt_verbose:
26786+ case Opt_noverbose:
26787+ case Opt_sum:
26788+ case Opt_nosum:
26789+ case Opt_wsum:
26790+ case Opt_rdblk_def:
26791+ case Opt_rdhash_def:
26792+ case Opt_dirren:
26793+ case Opt_nodirren:
26794+ case Opt_acl:
26795+ case Opt_noacl:
26796+ err = 0;
26797+ opt->type = token;
26798+ break;
26799+
26800+ case Opt_udba:
26801+ opt->udba = udba_val(a->args[0].from);
26802+ if (opt->udba >= 0) {
26803+ err = 0;
26804+ opt->type = token;
26805+ } else
26806+ pr_err("wrong value, %s\n", opt_str);
26807+ break;
26808+
26809+ case Opt_wbr_create:
26810+ u.create = &opt->wbr_create;
26811+ u.create->wbr_create
26812+ = au_wbr_create_val(a->args[0].from, u.create);
26813+ if (u.create->wbr_create >= 0) {
26814+ err = 0;
26815+ opt->type = token;
26816+ } else
26817+ pr_err("wrong value, %s\n", opt_str);
26818+ break;
26819+ case Opt_wbr_copyup:
26820+ opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
26821+ if (opt->wbr_copyup >= 0) {
26822+ err = 0;
26823+ opt->type = token;
26824+ } else
26825+ pr_err("wrong value, %s\n", opt_str);
26826+ break;
26827+
26828+ case Opt_fhsm_sec:
26829+ if (unlikely(match_int(&a->args[0], &n)
26830+ || n < 0)) {
26831+ pr_err("bad integer in %s\n", opt_str);
26832+ break;
26833+ }
26834+ if (sysaufs_brs) {
26835+ opt->fhsm_second = n;
26836+ opt->type = token;
26837+ } else
26838+ pr_warn("ignored %s\n", opt_str);
26839+ err = 0;
26840+ break;
26841+
26842+ case Opt_ignore:
26843+ pr_warn("ignored %s\n", opt_str);
26844+ fallthrough;
26845+ case Opt_ignore_silent:
26846+ skipped = 1;
26847+ err = 0;
26848+ break;
26849+ case Opt_err:
26850+ pr_err("unknown option %s\n", opt_str);
26851+ break;
26852+ }
26853+
26854+ if (!err && !skipped) {
26855+ if (unlikely(++opt > opt_tail)) {
26856+ err = -E2BIG;
26857+ opt--;
26858+ opt->type = Opt_tail;
26859+ break;
26860+ }
26861+ opt->type = Opt_tail;
26862+ }
26863+ }
26864+
26865+ au_kfree_rcu(a);
26866+ dump_opts(opts);
26867+ if (unlikely(err))
26868+ au_opts_free(opts);
26869+
26870+out:
26871+ return err;
26872+}
26873+
26874+static int au_opt_wbr_create(struct super_block *sb,
26875+ struct au_opt_wbr_create *create)
26876+{
26877+ int err;
26878+ struct au_sbinfo *sbinfo;
26879+
26880+ SiMustWriteLock(sb);
26881+
26882+ err = 1; /* handled */
26883+ sbinfo = au_sbi(sb);
26884+ if (sbinfo->si_wbr_create_ops->fin) {
26885+ err = sbinfo->si_wbr_create_ops->fin(sb);
26886+ if (!err)
26887+ err = 1;
26888+ }
26889+
26890+ sbinfo->si_wbr_create = create->wbr_create;
26891+ sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
26892+ switch (create->wbr_create) {
26893+ case AuWbrCreate_MFSRRV:
26894+ case AuWbrCreate_MFSRR:
26895+ case AuWbrCreate_TDMFS:
26896+ case AuWbrCreate_TDMFSV:
26897+ case AuWbrCreate_PMFSRR:
26898+ case AuWbrCreate_PMFSRRV:
26899+ sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
26900+ fallthrough;
26901+ case AuWbrCreate_MFS:
26902+ case AuWbrCreate_MFSV:
26903+ case AuWbrCreate_PMFS:
26904+ case AuWbrCreate_PMFSV:
26905+ sbinfo->si_wbr_mfs.mfs_expire
26906+ = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
26907+ break;
26908+ }
26909+
26910+ if (sbinfo->si_wbr_create_ops->init)
26911+ sbinfo->si_wbr_create_ops->init(sb); /* ignore */
26912+
26913+ return err;
26914+}
26915+
26916+/*
26917+ * returns,
26918+ * plus: processed without an error
26919+ * zero: unprocessed
26920+ */
26921+static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
26922+ struct au_opts *opts)
26923+{
26924+ int err;
26925+ struct au_sbinfo *sbinfo;
26926+
26927+ SiMustWriteLock(sb);
26928+
26929+ err = 1; /* handled */
26930+ sbinfo = au_sbi(sb);
26931+ switch (opt->type) {
26932+ case Opt_udba:
26933+ sbinfo->si_mntflags &= ~AuOptMask_UDBA;
26934+ sbinfo->si_mntflags |= opt->udba;
26935+ opts->given_udba |= opt->udba;
26936+ break;
26937+
26938+ case Opt_plink:
26939+ au_opt_set(sbinfo->si_mntflags, PLINK);
26940+ break;
26941+ case Opt_noplink:
26942+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
26943+ au_plink_put(sb, /*verbose*/1);
26944+ au_opt_clr(sbinfo->si_mntflags, PLINK);
26945+ break;
26946+ case Opt_list_plink:
26947+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
26948+ au_plink_list(sb);
26949+ break;
26950+
26951+ case Opt_dio:
26952+ au_opt_set(sbinfo->si_mntflags, DIO);
26953+ au_fset_opts(opts->flags, REFRESH_DYAOP);
26954+ break;
26955+ case Opt_nodio:
26956+ au_opt_clr(sbinfo->si_mntflags, DIO);
26957+ au_fset_opts(opts->flags, REFRESH_DYAOP);
26958+ break;
26959+
26960+ case Opt_fhsm_sec:
26961+ au_fhsm_set(sbinfo, opt->fhsm_second);
26962+ break;
26963+
26964+ case Opt_diropq_a:
26965+ au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
26966+ break;
26967+ case Opt_diropq_w:
26968+ au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
26969+ break;
26970+
26971+ case Opt_warn_perm:
26972+ au_opt_set(sbinfo->si_mntflags, WARN_PERM);
26973+ break;
26974+ case Opt_nowarn_perm:
26975+ au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
26976+ break;
26977+
26978+ case Opt_verbose:
26979+ au_opt_set(sbinfo->si_mntflags, VERBOSE);
26980+ break;
26981+ case Opt_noverbose:
26982+ au_opt_clr(sbinfo->si_mntflags, VERBOSE);
26983+ break;
26984+
26985+ case Opt_sum:
26986+ au_opt_set(sbinfo->si_mntflags, SUM);
26987+ break;
26988+ case Opt_wsum:
26989+ au_opt_clr(sbinfo->si_mntflags, SUM);
26990+ au_opt_set(sbinfo->si_mntflags, SUM_W);
26991+ break;
26992+ case Opt_nosum:
26993+ au_opt_clr(sbinfo->si_mntflags, SUM);
26994+ au_opt_clr(sbinfo->si_mntflags, SUM_W);
26995+ break;
26996+
26997+ case Opt_wbr_create:
26998+ err = au_opt_wbr_create(sb, &opt->wbr_create);
26999+ break;
27000+ case Opt_wbr_copyup:
27001+ sbinfo->si_wbr_copyup = opt->wbr_copyup;
27002+ sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
27003+ break;
27004+
27005+ case Opt_dirwh:
27006+ sbinfo->si_dirwh = opt->dirwh;
27007+ break;
27008+
27009+ case Opt_rdcache:
27010+ sbinfo->si_rdcache
27011+ = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
27012+ break;
27013+ case Opt_rdblk:
27014+ sbinfo->si_rdblk = opt->rdblk;
27015+ break;
27016+ case Opt_rdblk_def:
27017+ sbinfo->si_rdblk = AUFS_RDBLK_DEF;
27018+ break;
27019+ case Opt_rdhash:
27020+ sbinfo->si_rdhash = opt->rdhash;
27021+ break;
27022+ case Opt_rdhash_def:
27023+ sbinfo->si_rdhash = AUFS_RDHASH_DEF;
27024+ break;
27025+
27026+ case Opt_shwh:
27027+ au_opt_set(sbinfo->si_mntflags, SHWH);
27028+ break;
27029+ case Opt_noshwh:
27030+ au_opt_clr(sbinfo->si_mntflags, SHWH);
27031+ break;
27032+
27033+ case Opt_dirperm1:
27034+ au_opt_set(sbinfo->si_mntflags, DIRPERM1);
27035+ break;
27036+ case Opt_nodirperm1:
27037+ au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
27038+ break;
27039+
27040+ case Opt_trunc_xino:
27041+ au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
27042+ break;
27043+ case Opt_notrunc_xino:
27044+ au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
27045+ break;
27046+
27047+ case Opt_trunc_xino_path:
27048+ case Opt_itrunc_xino:
27049+ err = au_xino_trunc(sb, opt->xino_itrunc.bindex,
27050+ /*idx_begin*/0);
27051+ if (!err)
27052+ err = 1;
27053+ break;
27054+
27055+ case Opt_trunc_xib:
27056+ au_fset_opts(opts->flags, TRUNC_XIB);
27057+ break;
27058+ case Opt_notrunc_xib:
27059+ au_fclr_opts(opts->flags, TRUNC_XIB);
27060+ break;
27061+
27062+ case Opt_dirren:
27063+ err = 1;
27064+ if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27065+ err = au_dr_opt_set(sb);
27066+ if (!err)
27067+ err = 1;
27068+ }
27069+ if (err == 1)
27070+ au_opt_set(sbinfo->si_mntflags, DIRREN);
27071+ break;
27072+ case Opt_nodirren:
27073+ err = 1;
27074+ if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27075+ err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
27076+ DR_FLUSHED));
27077+ if (!err)
27078+ err = 1;
27079+ }
27080+ if (err == 1)
27081+ au_opt_clr(sbinfo->si_mntflags, DIRREN);
27082+ break;
27083+
27084+ case Opt_acl:
27085+ sb->s_flags |= SB_POSIXACL;
27086+ break;
27087+ case Opt_noacl:
27088+ sb->s_flags &= ~SB_POSIXACL;
27089+ break;
27090+
27091+ default:
27092+ err = 0;
27093+ break;
27094+ }
27095+
27096+ return err;
27097+}
27098+
27099+/*
27100+ * returns tri-state.
27101+ * plus: processed without an error
27102+ * zero: unprocessed
27103+ * minus: error
27104+ */
27105+static int au_opt_br(struct super_block *sb, struct au_opt *opt,
27106+ struct au_opts *opts)
27107+{
27108+ int err, do_refresh;
27109+
27110+ err = 0;
27111+ switch (opt->type) {
27112+ case Opt_append:
27113+ opt->add.bindex = au_sbbot(sb) + 1;
27114+ if (opt->add.bindex < 0)
27115+ opt->add.bindex = 0;
27116+ goto add;
27117+ /* Always goto add, not fallthrough */
27118+ case Opt_prepend:
27119+ opt->add.bindex = 0;
27120+ fallthrough;
27121+ add: /* indented label */
27122+ case Opt_add:
27123+ err = au_br_add(sb, &opt->add,
27124+ au_ftest_opts(opts->flags, REMOUNT));
27125+ if (!err) {
27126+ err = 1;
27127+ au_fset_opts(opts->flags, REFRESH);
27128+ }
27129+ break;
27130+
27131+ case Opt_del:
27132+ case Opt_idel:
27133+ err = au_br_del(sb, &opt->del,
27134+ au_ftest_opts(opts->flags, REMOUNT));
27135+ if (!err) {
27136+ err = 1;
27137+ au_fset_opts(opts->flags, TRUNC_XIB);
27138+ au_fset_opts(opts->flags, REFRESH);
27139+ }
27140+ break;
27141+
27142+ case Opt_mod:
27143+ case Opt_imod:
27144+ err = au_br_mod(sb, &opt->mod,
27145+ au_ftest_opts(opts->flags, REMOUNT),
27146+ &do_refresh);
27147+ if (!err) {
27148+ err = 1;
27149+ if (do_refresh)
27150+ au_fset_opts(opts->flags, REFRESH);
27151+ }
27152+ break;
27153+ }
27154+ return err;
27155+}
27156+
27157+static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
27158+ struct au_opt_xino **opt_xino,
27159+ struct au_opts *opts)
27160+{
27161+ int err;
27162+
27163+ err = 0;
27164+ switch (opt->type) {
27165+ case Opt_xino:
27166+ err = au_xino_set(sb, &opt->xino,
27167+ !!au_ftest_opts(opts->flags, REMOUNT));
27168+ if (unlikely(err))
27169+ break;
27170+
27171+ *opt_xino = &opt->xino;
27172+ break;
27173+
27174+ case Opt_noxino:
27175+ au_xino_clr(sb);
27176+ *opt_xino = (void *)-1;
27177+ break;
27178+ }
27179+
27180+ return err;
27181+}
27182+
27183+int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27184+ unsigned int pending)
27185+{
27186+ int err, fhsm;
27187+ aufs_bindex_t bindex, bbot;
27188+ unsigned char do_plink, skip, do_free, can_no_dreval;
27189+ struct au_branch *br;
27190+ struct au_wbr *wbr;
27191+ struct dentry *root, *dentry;
27192+ struct inode *dir, *h_dir;
27193+ struct au_sbinfo *sbinfo;
27194+ struct au_hinode *hdir;
27195+
27196+ SiMustAnyLock(sb);
27197+
27198+ sbinfo = au_sbi(sb);
27199+ AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
27200+
27201+ if (!(sb_flags & SB_RDONLY)) {
27202+ if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
27203+ pr_warn("first branch should be rw\n");
27204+ if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
27205+ pr_warn_once("shwh should be used with ro\n");
27206+ }
27207+
27208+ if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
27209+ && !au_opt_test(sbinfo->si_mntflags, XINO))
27210+ pr_warn_once("udba=*notify requires xino\n");
27211+
27212+ if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
27213+ pr_warn_once("dirperm1 breaks the protection"
27214+ " by the permission bits on the lower branch\n");
27215+
27216+ err = 0;
27217+ fhsm = 0;
27218+ root = sb->s_root;
27219+ dir = d_inode(root);
27220+ do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
27221+ can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
27222+ UDBA_NONE);
27223+ bbot = au_sbbot(sb);
27224+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
27225+ skip = 0;
27226+ h_dir = au_h_iptr(dir, bindex);
27227+ br = au_sbr(sb, bindex);
27228+
27229+ if ((br->br_perm & AuBrAttr_ICEX)
27230+ && !h_dir->i_op->listxattr)
27231+ br->br_perm &= ~AuBrAttr_ICEX;
27232+#if 0 /* untested */
27233+ if ((br->br_perm & AuBrAttr_ICEX_SEC)
27234+ && (au_br_sb(br)->s_flags & SB_NOSEC))
27235+ br->br_perm &= ~AuBrAttr_ICEX_SEC;
27236+#endif
27237+
27238+ do_free = 0;
27239+ wbr = br->br_wbr;
27240+ if (wbr)
27241+ wbr_wh_read_lock(wbr);
27242+
27243+ if (!au_br_writable(br->br_perm)) {
27244+ do_free = !!wbr;
27245+ skip = (!wbr
27246+ || (!wbr->wbr_whbase
27247+ && !wbr->wbr_plink
27248+ && !wbr->wbr_orph));
27249+ } else if (!au_br_wh_linkable(br->br_perm)) {
27250+ /* skip = (!br->br_whbase && !br->br_orph); */
27251+ skip = (!wbr || !wbr->wbr_whbase);
27252+ if (skip && wbr) {
27253+ if (do_plink)
27254+ skip = !!wbr->wbr_plink;
27255+ else
27256+ skip = !wbr->wbr_plink;
27257+ }
27258+ } else {
27259+ /* skip = (br->br_whbase && br->br_ohph); */
27260+ skip = (wbr && wbr->wbr_whbase);
27261+ if (skip) {
27262+ if (do_plink)
27263+ skip = !!wbr->wbr_plink;
27264+ else
27265+ skip = !wbr->wbr_plink;
27266+ }
27267+ }
27268+ if (wbr)
27269+ wbr_wh_read_unlock(wbr);
27270+
27271+ if (can_no_dreval) {
27272+ dentry = br->br_path.dentry;
27273+ spin_lock(&dentry->d_lock);
27274+ if (dentry->d_flags &
27275+ (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
27276+ can_no_dreval = 0;
27277+ spin_unlock(&dentry->d_lock);
27278+ }
27279+
27280+ if (au_br_fhsm(br->br_perm)) {
27281+ fhsm++;
27282+ AuDebugOn(!br->br_fhsm);
27283+ }
27284+
27285+ if (skip)
27286+ continue;
27287+
27288+ hdir = au_hi(dir, bindex);
27289+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
27290+ if (wbr)
27291+ wbr_wh_write_lock(wbr);
27292+ err = au_wh_init(br, sb);
27293+ if (wbr)
27294+ wbr_wh_write_unlock(wbr);
27295+ au_hn_inode_unlock(hdir);
27296+
27297+ if (!err && do_free) {
27298+ au_kfree_rcu(wbr);
27299+ br->br_wbr = NULL;
27300+ }
27301+ }
27302+
27303+ if (can_no_dreval)
27304+ au_fset_si(sbinfo, NO_DREVAL);
27305+ else
27306+ au_fclr_si(sbinfo, NO_DREVAL);
27307+
27308+ if (fhsm >= 2) {
27309+ au_fset_si(sbinfo, FHSM);
27310+ for (bindex = bbot; bindex >= 0; bindex--) {
27311+ br = au_sbr(sb, bindex);
27312+ if (au_br_fhsm(br->br_perm)) {
27313+ au_fhsm_set_bottom(sb, bindex);
27314+ break;
27315+ }
27316+ }
27317+ } else {
27318+ au_fclr_si(sbinfo, FHSM);
27319+ au_fhsm_set_bottom(sb, -1);
27320+ }
27321+
27322+ return err;
27323+}
27324+
27325+int au_opts_mount(struct super_block *sb, struct au_opts *opts)
27326+{
27327+ int err;
27328+ unsigned int tmp;
27329+ aufs_bindex_t bindex, bbot;
27330+ struct au_opt *opt;
27331+ struct au_opt_xino *opt_xino, xino;
27332+ struct au_sbinfo *sbinfo;
27333+ struct au_branch *br;
27334+ struct inode *dir;
27335+
27336+ SiMustWriteLock(sb);
27337+
27338+ err = 0;
27339+ opt_xino = NULL;
27340+ opt = opts->opt;
27341+ while (err >= 0 && opt->type != Opt_tail)
27342+ err = au_opt_simple(sb, opt++, opts);
27343+ if (err > 0)
27344+ err = 0;
27345+ else if (unlikely(err < 0))
27346+ goto out;
27347+
27348+ /* disable xino and udba temporary */
27349+ sbinfo = au_sbi(sb);
27350+ tmp = sbinfo->si_mntflags;
27351+ au_opt_clr(sbinfo->si_mntflags, XINO);
27352+ au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
27353+
27354+ opt = opts->opt;
27355+ while (err >= 0 && opt->type != Opt_tail)
27356+ err = au_opt_br(sb, opt++, opts);
27357+ if (err > 0)
27358+ err = 0;
27359+ else if (unlikely(err < 0))
27360+ goto out;
27361+
27362+ bbot = au_sbbot(sb);
27363+ if (unlikely(bbot < 0)) {
27364+ err = -EINVAL;
27365+ pr_err("no branches\n");
27366+ goto out;
27367+ }
27368+
27369+ if (au_opt_test(tmp, XINO))
27370+ au_opt_set(sbinfo->si_mntflags, XINO);
27371+ opt = opts->opt;
27372+ while (!err && opt->type != Opt_tail)
27373+ err = au_opt_xino(sb, opt++, &opt_xino, opts);
27374+ if (unlikely(err))
27375+ goto out;
27376+
27377+ err = au_opts_verify(sb, sb->s_flags, tmp);
27378+ if (unlikely(err))
27379+ goto out;
27380+
27381+ /* restore xino */
27382+ if (au_opt_test(tmp, XINO) && !opt_xino) {
27383+ xino.file = au_xino_def(sb);
27384+ err = PTR_ERR(xino.file);
27385+ if (IS_ERR(xino.file))
27386+ goto out;
27387+
27388+ err = au_xino_set(sb, &xino, /*remount*/0);
27389+ fput(xino.file);
27390+ if (unlikely(err))
27391+ goto out;
27392+ }
27393+
27394+ /* restore udba */
27395+ tmp &= AuOptMask_UDBA;
27396+ sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27397+ sbinfo->si_mntflags |= tmp;
27398+ bbot = au_sbbot(sb);
27399+ for (bindex = 0; bindex <= bbot; bindex++) {
27400+ br = au_sbr(sb, bindex);
27401+ err = au_hnotify_reset_br(tmp, br, br->br_perm);
27402+ if (unlikely(err))
27403+ AuIOErr("hnotify failed on br %d, %d, ignored\n",
27404+ bindex, err);
27405+ /* go on even if err */
27406+ }
27407+ if (au_opt_test(tmp, UDBA_HNOTIFY)) {
27408+ dir = d_inode(sb->s_root);
27409+ au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
27410+ }
27411+
27412+out:
27413+ return err;
27414+}
27415+
27416+int au_opts_remount(struct super_block *sb, struct au_opts *opts)
27417+{
27418+ int err, rerr;
27419+ unsigned char no_dreval;
27420+ struct inode *dir;
27421+ struct au_opt_xino *opt_xino;
27422+ struct au_opt *opt;
27423+ struct au_sbinfo *sbinfo;
27424+
27425+ SiMustWriteLock(sb);
27426+
27427+ err = au_dr_opt_flush(sb);
27428+ if (unlikely(err))
27429+ goto out;
27430+ au_fset_opts(opts->flags, DR_FLUSHED);
27431+
27432+ dir = d_inode(sb->s_root);
27433+ sbinfo = au_sbi(sb);
27434+ opt_xino = NULL;
27435+ opt = opts->opt;
27436+ while (err >= 0 && opt->type != Opt_tail) {
27437+ err = au_opt_simple(sb, opt, opts);
27438+ if (!err)
27439+ err = au_opt_br(sb, opt, opts);
27440+ if (!err)
27441+ err = au_opt_xino(sb, opt, &opt_xino, opts);
27442+ opt++;
27443+ }
27444+ if (err > 0)
27445+ err = 0;
27446+ AuTraceErr(err);
27447+ /* go on even err */
27448+
27449+ no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
27450+ rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
27451+ if (unlikely(rerr && !err))
27452+ err = rerr;
27453+
27454+ if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
27455+ au_fset_opts(opts->flags, REFRESH_IDOP);
27456+
27457+ if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
27458+ rerr = au_xib_trunc(sb);
27459+ if (unlikely(rerr && !err))
27460+ err = rerr;
27461+ }
27462+
27463+ /* will be handled by the caller */
27464+ if (!au_ftest_opts(opts->flags, REFRESH)
27465+ && (opts->given_udba
27466+ || au_opt_test(sbinfo->si_mntflags, XINO)
27467+ || au_ftest_opts(opts->flags, REFRESH_IDOP)
27468+ ))
27469+ au_fset_opts(opts->flags, REFRESH);
27470+
27471+ AuDbg("status 0x%x\n", opts->flags);
27472+
27473+out:
27474+ return err;
27475+}
27476+
27477+/* ---------------------------------------------------------------------- */
27478+
27479+unsigned int au_opt_udba(struct super_block *sb)
27480+{
27481+ return au_mntflags(sb) & AuOptMask_UDBA;
27482+}
27483diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
27484--- /usr/share/empty/fs/aufs/opts.h 1970-01-01 01:00:00.000000000 +0100
27485+++ linux/fs/aufs/opts.h 2021-12-03 15:38:59.939980643 +0100
27486@@ -0,0 +1,225 @@
27487+/* SPDX-License-Identifier: GPL-2.0 */
27488+/*
27489+ * Copyright (C) 2005-2021 Junjiro R. Okajima
27490+ *
27491+ * This program, aufs is free software; you can redistribute it and/or modify
27492+ * it under the terms of the GNU General Public License as published by
27493+ * the Free Software Foundation; either version 2 of the License, or
27494+ * (at your option) any later version.
27495+ *
27496+ * This program is distributed in the hope that it will be useful,
27497+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
27498+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27499+ * GNU General Public License for more details.
27500+ *
27501+ * You should have received a copy of the GNU General Public License
27502+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
27503+ */
27504+
27505+/*
27506+ * mount options/flags
27507+ */
27508+
27509+#ifndef __AUFS_OPTS_H__
27510+#define __AUFS_OPTS_H__
27511+
27512+#ifdef __KERNEL__
27513+
27514+#include <linux/path.h>
27515+
27516+struct file;
27517+
27518+/* ---------------------------------------------------------------------- */
27519+
27520+/* mount flags */
27521+#define AuOpt_XINO 1 /* external inode number bitmap
27522+ and translation table */
27523+#define AuOpt_TRUNC_XINO (1 << 1) /* truncate xino files */
27524+#define AuOpt_UDBA_NONE (1 << 2) /* users direct branch access */
27525+#define AuOpt_UDBA_REVAL (1 << 3)
27526+#define AuOpt_UDBA_HNOTIFY (1 << 4)
27527+#define AuOpt_SHWH (1 << 5) /* show whiteout */
27528+#define AuOpt_PLINK (1 << 6) /* pseudo-link */
27529+#define AuOpt_DIRPERM1 (1 << 7) /* ignore the lower dir's perm
27530+ bits */
27531+#define AuOpt_ALWAYS_DIROPQ (1 << 9) /* policy to creating diropq */
27532+#define AuOpt_SUM (1 << 10) /* summation for statfs(2) */
27533+#define AuOpt_SUM_W (1 << 11) /* unimplemented */
27534+#define AuOpt_WARN_PERM (1 << 12) /* warn when add-branch */
27535+#define AuOpt_VERBOSE (1 << 13) /* print the cause of error */
27536+#define AuOpt_DIO (1 << 14) /* direct io */
27537+#define AuOpt_DIRREN (1 << 15) /* directory rename */
27538+
27539+#ifndef CONFIG_AUFS_HNOTIFY
27540+#undef AuOpt_UDBA_HNOTIFY
27541+#define AuOpt_UDBA_HNOTIFY 0
27542+#endif
27543+#ifndef CONFIG_AUFS_DIRREN
27544+#undef AuOpt_DIRREN
27545+#define AuOpt_DIRREN 0
27546+#endif
27547+#ifndef CONFIG_AUFS_SHWH
27548+#undef AuOpt_SHWH
27549+#define AuOpt_SHWH 0
27550+#endif
27551+
27552+#define AuOpt_Def (AuOpt_XINO \
27553+ | AuOpt_UDBA_REVAL \
27554+ | AuOpt_PLINK \
27555+ /* | AuOpt_DIRPERM1 */ \
27556+ | AuOpt_WARN_PERM)
27557+#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
27558+ | AuOpt_UDBA_REVAL \
27559+ | AuOpt_UDBA_HNOTIFY)
27560+
27561+#define au_opt_test(flags, name) (flags & AuOpt_##name)
27562+#define au_opt_set(flags, name) do { \
27563+ BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
27564+ ((flags) |= AuOpt_##name); \
27565+} while (0)
27566+#define au_opt_set_udba(flags, name) do { \
27567+ (flags) &= ~AuOptMask_UDBA; \
27568+ ((flags) |= AuOpt_##name); \
27569+} while (0)
27570+#define au_opt_clr(flags, name) do { \
27571+ ((flags) &= ~AuOpt_##name); \
27572+} while (0)
27573+
27574+static inline unsigned int au_opts_plink(unsigned int mntflags)
27575+{
27576+#ifdef CONFIG_PROC_FS
27577+ return mntflags;
27578+#else
27579+ return mntflags & ~AuOpt_PLINK;
27580+#endif
27581+}
27582+
27583+/* ---------------------------------------------------------------------- */
27584+
27585+/* policies to select one among multiple writable branches */
27586+enum {
27587+ AuWbrCreate_TDP, /* top down parent */
27588+ AuWbrCreate_RR, /* round robin */
27589+ AuWbrCreate_MFS, /* most free space */
27590+ AuWbrCreate_MFSV, /* mfs with seconds */
27591+ AuWbrCreate_MFSRR, /* mfs then rr */
27592+ AuWbrCreate_MFSRRV, /* mfs then rr with seconds */
27593+ AuWbrCreate_TDMFS, /* top down regardless parent and mfs */
27594+ AuWbrCreate_TDMFSV, /* top down regardless parent and mfs */
27595+ AuWbrCreate_PMFS, /* parent and mfs */
27596+ AuWbrCreate_PMFSV, /* parent and mfs with seconds */
27597+ AuWbrCreate_PMFSRR, /* parent, mfs and round-robin */
27598+ AuWbrCreate_PMFSRRV, /* plus seconds */
27599+
27600+ AuWbrCreate_Def = AuWbrCreate_TDP
27601+};
27602+
27603+enum {
27604+ AuWbrCopyup_TDP, /* top down parent */
27605+ AuWbrCopyup_BUP, /* bottom up parent */
27606+ AuWbrCopyup_BU, /* bottom up */
27607+
27608+ AuWbrCopyup_Def = AuWbrCopyup_TDP
27609+};
27610+
27611+/* ---------------------------------------------------------------------- */
27612+
27613+struct au_opt_add {
27614+ aufs_bindex_t bindex;
27615+ char *pathname;
27616+ int perm;
27617+ struct path path;
27618+};
27619+
27620+struct au_opt_del {
27621+ char *pathname;
27622+ struct path h_path;
27623+};
27624+
27625+struct au_opt_mod {
27626+ char *path;
27627+ int perm;
27628+ struct dentry *h_root;
27629+};
27630+
27631+struct au_opt_xino {
27632+ char *path;
27633+ struct file *file;
27634+};
27635+
27636+struct au_opt_xino_itrunc {
27637+ aufs_bindex_t bindex;
27638+};
27639+
27640+struct au_opt_wbr_create {
27641+ int wbr_create;
27642+ int mfs_second;
27643+ unsigned long long mfsrr_watermark;
27644+};
27645+
27646+struct au_opt {
27647+ int type;
27648+ union {
27649+ struct au_opt_xino xino;
27650+ struct au_opt_xino_itrunc xino_itrunc;
27651+ struct au_opt_add add;
27652+ struct au_opt_del del;
27653+ struct au_opt_mod mod;
27654+ int dirwh;
27655+ int rdcache;
27656+ unsigned int rdblk;
27657+ unsigned int rdhash;
27658+ int udba;
27659+ struct au_opt_wbr_create wbr_create;
27660+ int wbr_copyup;
27661+ unsigned int fhsm_second;
27662+ };
27663+};
27664+
27665+/* opts flags */
27666+#define AuOpts_REMOUNT 1
27667+#define AuOpts_REFRESH (1 << 1)
27668+#define AuOpts_TRUNC_XIB (1 << 2)
27669+#define AuOpts_REFRESH_DYAOP (1 << 3)
27670+#define AuOpts_REFRESH_IDOP (1 << 4)
27671+#define AuOpts_DR_FLUSHED (1 << 5)
27672+#define au_ftest_opts(flags, name) ((flags) & AuOpts_##name)
27673+#define au_fset_opts(flags, name) \
27674+ do { (flags) |= AuOpts_##name; } while (0)
27675+#define au_fclr_opts(flags, name) \
27676+ do { (flags) &= ~AuOpts_##name; } while (0)
27677+
27678+#ifndef CONFIG_AUFS_DIRREN
27679+#undef AuOpts_DR_FLUSHED
27680+#define AuOpts_DR_FLUSHED 0
27681+#endif
27682+
27683+struct au_opts {
27684+ struct au_opt *opt;
27685+ int max_opt;
27686+
27687+ unsigned int given_udba;
27688+ unsigned int flags;
27689+ unsigned long sb_flags;
27690+};
27691+
27692+/* ---------------------------------------------------------------------- */
27693+
27694+/* opts.c */
27695+void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
27696+const char *au_optstr_udba(int udba);
27697+const char *au_optstr_wbr_copyup(int wbr_copyup);
27698+const char *au_optstr_wbr_create(int wbr_create);
27699+
27700+void au_opts_free(struct au_opts *opts);
27701+struct super_block;
27702+int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
27703+int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27704+ unsigned int pending);
27705+int au_opts_mount(struct super_block *sb, struct au_opts *opts);
27706+int au_opts_remount(struct super_block *sb, struct au_opts *opts);
27707+
27708+unsigned int au_opt_udba(struct super_block *sb);
27709+
27710+#endif /* __KERNEL__ */
27711+#endif /* __AUFS_OPTS_H__ */
27712diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
27713--- /usr/share/empty/fs/aufs/plink.c 1970-01-01 01:00:00.000000000 +0100
27714+++ linux/fs/aufs/plink.c 2021-12-03 15:38:59.939980643 +0100
27715@@ -0,0 +1,516 @@
27716+// SPDX-License-Identifier: GPL-2.0
27717+/*
27718+ * Copyright (C) 2005-2021 Junjiro R. Okajima
27719+ *
27720+ * This program, aufs is free software; you can redistribute it and/or modify
27721+ * it under the terms of the GNU General Public License as published by
27722+ * the Free Software Foundation; either version 2 of the License, or
27723+ * (at your option) any later version.
27724+ *
27725+ * This program is distributed in the hope that it will be useful,
27726+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
27727+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27728+ * GNU General Public License for more details.
27729+ *
27730+ * You should have received a copy of the GNU General Public License
27731+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
27732+ */
27733+
27734+/*
27735+ * pseudo-link
27736+ */
27737+
27738+#include "aufs.h"
27739+
27740+/*
27741+ * the pseudo-link maintenance mode.
27742+ * during a user process maintains the pseudo-links,
27743+ * prohibit adding a new plink and branch manipulation.
27744+ *
27745+ * Flags
27746+ * NOPLM:
27747+ * For entry functions which will handle plink, and i_mutex is already held
27748+ * in VFS.
27749+ * They cannot wait and should return an error at once.
27750+ * Callers has to check the error.
27751+ * NOPLMW:
27752+ * For entry functions which will handle plink, but i_mutex is not held
27753+ * in VFS.
27754+ * They can wait the plink maintenance mode to finish.
27755+ *
27756+ * They behave like F_SETLK and F_SETLKW.
27757+ * If the caller never handle plink, then both flags are unnecessary.
27758+ */
27759+
27760+int au_plink_maint(struct super_block *sb, int flags)
27761+{
27762+ int err;
27763+ pid_t pid, ppid;
27764+ struct task_struct *parent, *prev;
27765+ struct au_sbinfo *sbi;
27766+
27767+ SiMustAnyLock(sb);
27768+
27769+ err = 0;
27770+ if (!au_opt_test(au_mntflags(sb), PLINK))
27771+ goto out;
27772+
27773+ sbi = au_sbi(sb);
27774+ pid = sbi->si_plink_maint_pid;
27775+ if (!pid || pid == current->pid)
27776+ goto out;
27777+
27778+ /* todo: it highly depends upon /sbin/mount.aufs */
27779+ prev = NULL;
27780+ parent = current;
27781+ ppid = 0;
27782+ rcu_read_lock();
27783+ while (1) {
27784+ parent = rcu_dereference(parent->real_parent);
27785+ if (parent == prev)
27786+ break;
27787+ ppid = task_pid_vnr(parent);
27788+ if (pid == ppid) {
27789+ rcu_read_unlock();
27790+ goto out;
27791+ }
27792+ prev = parent;
27793+ }
27794+ rcu_read_unlock();
27795+
27796+ if (au_ftest_lock(flags, NOPLMW)) {
27797+ /* if there is no i_mutex lock in VFS, we don't need to wait */
27798+ /* AuDebugOn(!lockdep_depth(current)); */
27799+ while (sbi->si_plink_maint_pid) {
27800+ si_read_unlock(sb);
27801+ /* gave up wake_up_bit() */
27802+ wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
27803+
27804+ if (au_ftest_lock(flags, FLUSH))
27805+ au_nwt_flush(&sbi->si_nowait);
27806+ si_noflush_read_lock(sb);
27807+ }
27808+ } else if (au_ftest_lock(flags, NOPLM)) {
27809+ AuDbg("ppid %d, pid %d\n", ppid, pid);
27810+ err = -EAGAIN;
27811+ }
27812+
27813+out:
27814+ return err;
27815+}
27816+
27817+void au_plink_maint_leave(struct au_sbinfo *sbinfo)
27818+{
27819+ spin_lock(&sbinfo->si_plink_maint_lock);
27820+ sbinfo->si_plink_maint_pid = 0;
27821+ spin_unlock(&sbinfo->si_plink_maint_lock);
27822+ wake_up_all(&sbinfo->si_plink_wq);
27823+}
27824+
27825+int au_plink_maint_enter(struct super_block *sb)
27826+{
27827+ int err;
27828+ struct au_sbinfo *sbinfo;
27829+
27830+ err = 0;
27831+ sbinfo = au_sbi(sb);
27832+ /* make sure i am the only one in this fs */
27833+ si_write_lock(sb, AuLock_FLUSH);
27834+ if (au_opt_test(au_mntflags(sb), PLINK)) {
27835+ spin_lock(&sbinfo->si_plink_maint_lock);
27836+ if (!sbinfo->si_plink_maint_pid)
27837+ sbinfo->si_plink_maint_pid = current->pid;
27838+ else
27839+ err = -EBUSY;
27840+ spin_unlock(&sbinfo->si_plink_maint_lock);
27841+ }
27842+ si_write_unlock(sb);
27843+
27844+ return err;
27845+}
27846+
27847+/* ---------------------------------------------------------------------- */
27848+
27849+#ifdef CONFIG_AUFS_DEBUG
27850+void au_plink_list(struct super_block *sb)
27851+{
27852+ int i;
27853+ struct au_sbinfo *sbinfo;
27854+ struct hlist_bl_head *hbl;
27855+ struct hlist_bl_node *pos;
27856+ struct au_icntnr *icntnr;
27857+
27858+ SiMustAnyLock(sb);
27859+
27860+ sbinfo = au_sbi(sb);
27861+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
27862+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
27863+
27864+ for (i = 0; i < AuPlink_NHASH; i++) {
27865+ hbl = sbinfo->si_plink + i;
27866+ hlist_bl_lock(hbl);
27867+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
27868+ AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
27869+ hlist_bl_unlock(hbl);
27870+ }
27871+}
27872+#endif
27873+
27874+/* is the inode pseudo-linked? */
27875+int au_plink_test(struct inode *inode)
27876+{
27877+ int found, i;
27878+ struct au_sbinfo *sbinfo;
27879+ struct hlist_bl_head *hbl;
27880+ struct hlist_bl_node *pos;
27881+ struct au_icntnr *icntnr;
27882+
27883+ sbinfo = au_sbi(inode->i_sb);
27884+ AuRwMustAnyLock(&sbinfo->si_rwsem);
27885+ AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
27886+ AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
27887+
27888+ found = 0;
27889+ i = au_plink_hash(inode->i_ino);
27890+ hbl = sbinfo->si_plink + i;
27891+ hlist_bl_lock(hbl);
27892+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
27893+ if (&icntnr->vfs_inode == inode) {
27894+ found = 1;
27895+ break;
27896+ }
27897+ hlist_bl_unlock(hbl);
27898+ return found;
27899+}
27900+
27901+/* ---------------------------------------------------------------------- */
27902+
27903+/*
27904+ * generate a name for plink.
27905+ * the file will be stored under AUFS_WH_PLINKDIR.
27906+ */
27907+/* 20 is max digits length of ulong 64 */
27908+#define PLINK_NAME_LEN ((20 + 1) * 2)
27909+
27910+static int plink_name(char *name, int len, struct inode *inode,
27911+ aufs_bindex_t bindex)
27912+{
27913+ int rlen;
27914+ struct inode *h_inode;
27915+
27916+ h_inode = au_h_iptr(inode, bindex);
27917+ rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
27918+ return rlen;
27919+}
27920+
27921+struct au_do_plink_lkup_args {
27922+ struct dentry **errp;
27923+ struct qstr *tgtname;
27924+ struct path *h_ppath;
27925+};
27926+
27927+static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
27928+ struct path *h_ppath)
27929+{
27930+ struct dentry *h_dentry;
27931+ struct inode *h_inode;
27932+
27933+ h_inode = d_inode(h_ppath->dentry);
27934+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD2);
27935+ h_dentry = vfsub_lkup_one(tgtname, h_ppath);
27936+ inode_unlock_shared(h_inode);
27937+
27938+ return h_dentry;
27939+}
27940+
27941+static void au_call_do_plink_lkup(void *args)
27942+{
27943+ struct au_do_plink_lkup_args *a = args;
27944+ *a->errp = au_do_plink_lkup(a->tgtname, a->h_ppath);
27945+}
27946+
27947+/* lookup the plink-ed @inode under the branch at @bindex */
27948+struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
27949+{
27950+ struct dentry *h_dentry;
27951+ struct au_branch *br;
27952+ struct path h_ppath;
27953+ int wkq_err;
27954+ char a[PLINK_NAME_LEN];
27955+ struct qstr tgtname = QSTR_INIT(a, 0);
27956+
27957+ AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
27958+
27959+ br = au_sbr(inode->i_sb, bindex);
27960+ h_ppath.dentry = br->br_wbr->wbr_plink;
27961+ h_ppath.mnt = au_br_mnt(br);
27962+ tgtname.len = plink_name(a, sizeof(a), inode, bindex);
27963+
27964+ if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
27965+ struct au_do_plink_lkup_args args = {
27966+ .errp = &h_dentry,
27967+ .tgtname = &tgtname,
27968+ .h_ppath = &h_ppath
27969+ };
27970+
27971+ wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
27972+ if (unlikely(wkq_err))
27973+ h_dentry = ERR_PTR(wkq_err);
27974+ } else
27975+ h_dentry = au_do_plink_lkup(&tgtname, &h_ppath);
27976+
27977+ return h_dentry;
27978+}
27979+
27980+/* create a pseudo-link */
27981+static int do_whplink(struct qstr *tgt, struct path *h_ppath,
27982+ struct dentry *h_dentry)
27983+{
27984+ int err;
27985+ struct path h_path;
27986+ struct inode *h_dir, *delegated;
27987+
27988+ h_dir = d_inode(h_ppath->dentry);
27989+ inode_lock_nested(h_dir, AuLsc_I_CHILD2);
27990+ h_path.mnt = h_ppath->mnt;
27991+again:
27992+ h_path.dentry = vfsub_lkup_one(tgt, h_ppath);
27993+ err = PTR_ERR(h_path.dentry);
27994+ if (IS_ERR(h_path.dentry))
27995+ goto out;
27996+
27997+ err = 0;
27998+ /* wh.plink dir is not monitored */
27999+ /* todo: is it really safe? */
28000+ if (d_is_positive(h_path.dentry)
28001+ && d_inode(h_path.dentry) != d_inode(h_dentry)) {
28002+ delegated = NULL;
28003+ err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
28004+ if (unlikely(err == -EWOULDBLOCK)) {
28005+ pr_warn("cannot retry for NFSv4 delegation"
28006+ " for an internal unlink\n");
28007+ iput(delegated);
28008+ }
28009+ dput(h_path.dentry);
28010+ h_path.dentry = NULL;
28011+ if (!err)
28012+ goto again;
28013+ }
28014+ if (!err && d_is_negative(h_path.dentry)) {
28015+ delegated = NULL;
28016+ err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
28017+ if (unlikely(err == -EWOULDBLOCK)) {
28018+ pr_warn("cannot retry for NFSv4 delegation"
28019+ " for an internal link\n");
28020+ iput(delegated);
28021+ }
28022+ }
28023+ dput(h_path.dentry);
28024+
28025+out:
28026+ inode_unlock(h_dir);
28027+ return err;
28028+}
28029+
28030+struct do_whplink_args {
28031+ int *errp;
28032+ struct qstr *tgt;
28033+ struct path *h_ppath;
28034+ struct dentry *h_dentry;
28035+};
28036+
28037+static void call_do_whplink(void *args)
28038+{
28039+ struct do_whplink_args *a = args;
28040+ *a->errp = do_whplink(a->tgt, a->h_ppath, a->h_dentry);
28041+}
28042+
28043+static int whplink(struct dentry *h_dentry, struct inode *inode,
28044+ aufs_bindex_t bindex)
28045+{
28046+ int err, wkq_err;
28047+ struct au_branch *br;
28048+ struct au_wbr *wbr;
28049+ struct path h_ppath;
28050+ char a[PLINK_NAME_LEN];
28051+ struct qstr tgtname = QSTR_INIT(a, 0);
28052+
28053+ br = au_sbr(inode->i_sb, bindex);
28054+ wbr = br->br_wbr;
28055+ h_ppath.dentry = wbr->wbr_plink;
28056+ h_ppath.mnt = au_br_mnt(br);
28057+ tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28058+
28059+ /* always superio. */
28060+ if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
28061+ struct do_whplink_args args = {
28062+ .errp = &err,
28063+ .tgt = &tgtname,
28064+ .h_ppath = &h_ppath,
28065+ .h_dentry = h_dentry
28066+ };
28067+ wkq_err = au_wkq_wait(call_do_whplink, &args);
28068+ if (unlikely(wkq_err))
28069+ err = wkq_err;
28070+ } else
28071+ err = do_whplink(&tgtname, &h_ppath, h_dentry);
28072+
28073+ return err;
28074+}
28075+
28076+/*
28077+ * create a new pseudo-link for @h_dentry on @bindex.
28078+ * the linked inode is held in aufs @inode.
28079+ */
28080+void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
28081+ struct dentry *h_dentry)
28082+{
28083+ struct super_block *sb;
28084+ struct au_sbinfo *sbinfo;
28085+ struct hlist_bl_head *hbl;
28086+ struct hlist_bl_node *pos;
28087+ struct au_icntnr *icntnr;
28088+ int found, err, cnt, i;
28089+
28090+ sb = inode->i_sb;
28091+ sbinfo = au_sbi(sb);
28092+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28093+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28094+
28095+ found = au_plink_test(inode);
28096+ if (found)
28097+ return;
28098+
28099+ i = au_plink_hash(inode->i_ino);
28100+ hbl = sbinfo->si_plink + i;
28101+ au_igrab(inode);
28102+
28103+ hlist_bl_lock(hbl);
28104+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink) {
28105+ if (&icntnr->vfs_inode == inode) {
28106+ found = 1;
28107+ break;
28108+ }
28109+ }
28110+ if (!found) {
28111+ icntnr = container_of(inode, struct au_icntnr, vfs_inode);
28112+ hlist_bl_add_head(&icntnr->plink, hbl);
28113+ }
28114+ hlist_bl_unlock(hbl);
28115+ if (!found) {
28116+ cnt = au_hbl_count(hbl);
28117+#define msg "unexpectedly unbalanced or too many pseudo-links"
28118+ if (cnt > AUFS_PLINK_WARN)
28119+ AuWarn1(msg ", %d\n", cnt);
28120+#undef msg
28121+ err = whplink(h_dentry, inode, bindex);
28122+ if (unlikely(err)) {
28123+ pr_warn("err %d, damaged pseudo link.\n", err);
28124+ au_hbl_del(&icntnr->plink, hbl);
28125+ iput(&icntnr->vfs_inode);
28126+ }
28127+ } else
28128+ iput(&icntnr->vfs_inode);
28129+}
28130+
28131+/* free all plinks */
28132+void au_plink_put(struct super_block *sb, int verbose)
28133+{
28134+ int i, warned;
28135+ struct au_sbinfo *sbinfo;
28136+ struct hlist_bl_head *hbl;
28137+ struct hlist_bl_node *pos, *tmp;
28138+ struct au_icntnr *icntnr;
28139+
28140+ SiMustWriteLock(sb);
28141+
28142+ sbinfo = au_sbi(sb);
28143+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28144+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28145+
28146+ /* no spin_lock since sbinfo is write-locked */
28147+ warned = 0;
28148+ for (i = 0; i < AuPlink_NHASH; i++) {
28149+ hbl = sbinfo->si_plink + i;
28150+ if (!warned && verbose && !hlist_bl_empty(hbl)) {
28151+ pr_warn("pseudo-link is not flushed");
28152+ warned = 1;
28153+ }
28154+ hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink)
28155+ iput(&icntnr->vfs_inode);
28156+ INIT_HLIST_BL_HEAD(hbl);
28157+ }
28158+}
28159+
28160+void au_plink_clean(struct super_block *sb, int verbose)
28161+{
28162+ struct dentry *root;
28163+
28164+ root = sb->s_root;
28165+ aufs_write_lock(root);
28166+ if (au_opt_test(au_mntflags(sb), PLINK))
28167+ au_plink_put(sb, verbose);
28168+ aufs_write_unlock(root);
28169+}
28170+
28171+static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
28172+{
28173+ int do_put;
28174+ aufs_bindex_t btop, bbot, bindex;
28175+
28176+ do_put = 0;
28177+ btop = au_ibtop(inode);
28178+ bbot = au_ibbot(inode);
28179+ if (btop >= 0) {
28180+ for (bindex = btop; bindex <= bbot; bindex++) {
28181+ if (!au_h_iptr(inode, bindex)
28182+ || au_ii_br_id(inode, bindex) != br_id)
28183+ continue;
28184+ au_set_h_iptr(inode, bindex, NULL, 0);
28185+ do_put = 1;
28186+ break;
28187+ }
28188+ if (do_put)
28189+ for (bindex = btop; bindex <= bbot; bindex++)
28190+ if (au_h_iptr(inode, bindex)) {
28191+ do_put = 0;
28192+ break;
28193+ }
28194+ } else
28195+ do_put = 1;
28196+
28197+ return do_put;
28198+}
28199+
28200+/* free the plinks on a branch specified by @br_id */
28201+void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
28202+{
28203+ struct au_sbinfo *sbinfo;
28204+ struct hlist_bl_head *hbl;
28205+ struct hlist_bl_node *pos, *tmp;
28206+ struct au_icntnr *icntnr;
28207+ struct inode *inode;
28208+ int i, do_put;
28209+
28210+ SiMustWriteLock(sb);
28211+
28212+ sbinfo = au_sbi(sb);
28213+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28214+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28215+
28216+ /* no bit_lock since sbinfo is write-locked */
28217+ for (i = 0; i < AuPlink_NHASH; i++) {
28218+ hbl = sbinfo->si_plink + i;
28219+ hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink) {
28220+ inode = au_igrab(&icntnr->vfs_inode);
28221+ ii_write_lock_child(inode);
28222+ do_put = au_plink_do_half_refresh(inode, br_id);
28223+ if (do_put) {
28224+ hlist_bl_del(&icntnr->plink);
28225+ iput(inode);
28226+ }
28227+ ii_write_unlock(inode);
28228+ iput(inode);
28229+ }
28230+ }
28231+}
28232diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
28233--- /usr/share/empty/fs/aufs/poll.c 1970-01-01 01:00:00.000000000 +0100
28234+++ linux/fs/aufs/poll.c 2021-12-03 15:38:59.939980643 +0100
28235@@ -0,0 +1,51 @@
28236+// SPDX-License-Identifier: GPL-2.0
28237+/*
28238+ * Copyright (C) 2005-2021 Junjiro R. Okajima
28239+ *
28240+ * This program, aufs is free software; you can redistribute it and/or modify
28241+ * it under the terms of the GNU General Public License as published by
28242+ * the Free Software Foundation; either version 2 of the License, or
28243+ * (at your option) any later version.
28244+ *
28245+ * This program is distributed in the hope that it will be useful,
28246+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28247+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28248+ * GNU General Public License for more details.
28249+ *
28250+ * You should have received a copy of the GNU General Public License
28251+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
28252+ */
28253+
28254+/*
28255+ * poll operation
28256+ * There is only one filesystem which implements ->poll operation, currently.
28257+ */
28258+
28259+#include "aufs.h"
28260+
28261+__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt)
28262+{
28263+ __poll_t mask;
28264+ struct file *h_file;
28265+ struct super_block *sb;
28266+
28267+ /* We should pretend an error happened. */
28268+ mask = EPOLLERR /* | EPOLLIN | EPOLLOUT */;
28269+ sb = file->f_path.dentry->d_sb;
28270+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
28271+
28272+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
28273+ if (IS_ERR(h_file)) {
28274+ AuDbg("h_file %ld\n", PTR_ERR(h_file));
28275+ goto out;
28276+ }
28277+
28278+ mask = vfs_poll(h_file, pt);
28279+ fput(h_file); /* instead of au_read_post() */
28280+
28281+out:
28282+ si_read_unlock(sb);
28283+ if (mask & EPOLLERR)
28284+ AuDbg("mask 0x%x\n", mask);
28285+ return mask;
28286+}
28287diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
28288--- /usr/share/empty/fs/aufs/posix_acl.c 1970-01-01 01:00:00.000000000 +0100
28289+++ linux/fs/aufs/posix_acl.c 2021-12-03 15:40:58.236647297 +0100
28290@@ -0,0 +1,111 @@
28291+// SPDX-License-Identifier: GPL-2.0
28292+/*
28293+ * Copyright (C) 2014-2021 Junjiro R. Okajima
28294+ *
28295+ * This program, aufs is free software; you can redistribute it and/or modify
28296+ * it under the terms of the GNU General Public License as published by
28297+ * the Free Software Foundation; either version 2 of the License, or
28298+ * (at your option) any later version.
28299+ *
28300+ * This program is distributed in the hope that it will be useful,
28301+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28302+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28303+ * GNU General Public License for more details.
28304+ *
28305+ * You should have received a copy of the GNU General Public License
28306+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
28307+ */
28308+
28309+/*
28310+ * posix acl operations
28311+ */
28312+
28313+#include <linux/fs.h>
28314+#include "aufs.h"
28315+
28316+struct posix_acl *aufs_get_acl(struct inode *inode, int type, bool rcu)
28317+{
28318+ struct posix_acl *acl;
28319+ int err;
28320+ aufs_bindex_t bindex;
28321+ struct inode *h_inode;
28322+ struct super_block *sb;
28323+
28324+ acl = ERR_PTR(-ECHILD);
28325+ if (rcu)
28326+ goto out;
28327+
28328+ acl = NULL;
28329+ sb = inode->i_sb;
28330+ si_read_lock(sb, AuLock_FLUSH);
28331+ ii_read_lock_child(inode);
28332+ if (!(sb->s_flags & SB_POSIXACL))
28333+ goto unlock;
28334+
28335+ bindex = au_ibtop(inode);
28336+ h_inode = au_h_iptr(inode, bindex);
28337+ if (unlikely(!h_inode
28338+ || ((h_inode->i_mode & S_IFMT)
28339+ != (inode->i_mode & S_IFMT)))) {
28340+ err = au_busy_or_stale();
28341+ acl = ERR_PTR(err);
28342+ goto unlock;
28343+ }
28344+
28345+ /* always topmost only */
28346+ acl = get_acl(h_inode, type);
28347+ if (IS_ERR(acl))
28348+ forget_cached_acl(inode, type);
28349+ else
28350+ set_cached_acl(inode, type, acl);
28351+
28352+unlock:
28353+ ii_read_unlock(inode);
28354+ si_read_unlock(sb);
28355+
28356+out:
28357+ AuTraceErrPtr(acl);
28358+ return acl;
28359+}
28360+
28361+int aufs_set_acl(struct user_namespace *userns, struct inode *inode,
28362+ struct posix_acl *acl, int type)
28363+{
28364+ int err;
28365+ ssize_t ssz;
28366+ struct dentry *dentry;
28367+ struct au_sxattr arg = {
28368+ .type = AU_ACL_SET,
28369+ .u.acl_set = {
28370+ .acl = acl,
28371+ .type = type
28372+ },
28373+ };
28374+
28375+ IMustLock(inode);
28376+
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+
28391+ ssz = au_sxattr(dentry, inode, &arg);
28392+ /* forget even it if succeeds since the branch might set differently */
28393+ forget_cached_acl(inode, type);
28394+ dput(dentry);
28395+ err = ssz;
28396+ if (ssz >= 0)
28397+ err = 0;
28398+
28399+out:
28400+ return err;
28401+}
28402diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
28403--- /usr/share/empty/fs/aufs/procfs.c 1970-01-01 01:00:00.000000000 +0100
28404+++ linux/fs/aufs/procfs.c 2021-12-03 15:38:59.939980643 +0100
28405@@ -0,0 +1,170 @@
28406+// SPDX-License-Identifier: GPL-2.0
28407+/*
28408+ * Copyright (C) 2010-2021 Junjiro R. Okajima
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
28421+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
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;
28458+ struct hlist_bl_node *pos;
28459+
28460+ err = -EBUSY;
28461+ if (unlikely(file->private_data))
28462+ goto out;
28463+
28464+ sb = NULL;
28465+ /* don't use au_sbilist_lock() here */
28466+ hlist_bl_lock(&au_sbilist);
28467+ hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
28468+ if (id == sysaufs_si_id(sbinfo)) {
28469+ if (kobject_get_unless_zero(&sbinfo->si_kobj))
28470+ sb = sbinfo->si_sb;
28471+ break;
28472+ }
28473+ hlist_bl_unlock(&au_sbilist);
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+
28523+ err = kstrtoul(buf + 3, 16, &id);
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+
28537+static const struct proc_ops au_procfs_plm_op = {
28538+ .proc_write = au_procfs_plm_write,
28539+ .proc_release = au_procfs_plm_release
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+
28562+ entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | 0200,
28563+ au_procfs_dir, &au_procfs_plm_op);
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+}
28576diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
28577--- /usr/share/empty/fs/aufs/rdu.c 1970-01-01 01:00:00.000000000 +0100
28578+++ linux/fs/aufs/rdu.c 2021-12-03 15:38:59.939980643 +0100
28579@@ -0,0 +1,384 @@
28580+// SPDX-License-Identifier: GPL-2.0
28581+/*
28582+ * Copyright (C) 2005-2021 Junjiro R. Okajima
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
28595+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
28596+ */
28597+
28598+/*
28599+ * readdir in userspace.
28600+ */
28601+
28602+#include <linux/compat.h>
28603+#include <linux/fs_stack.h>
28604+#include <linux/security.h>
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)
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)
28616+
28617+struct au_rdu_arg {
28618+ struct dir_context ctx;
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+
28627+static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
28628+ loff_t offset, u64 h_ino, unsigned int d_type)
28629+{
28630+ int err, len;
28631+ struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
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;
28644+ if (unlikely(nlen > AUFS_MAX_NAMELEN))
28645+ ent.type = DT_UNKNOWN;
28646+
28647+ /* unnecessary to support mmap_sem since this is a dir */
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+
28667+out:
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+
28678+ /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
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(); */
28689+ err = vfsub_iterate_dir(h_file, &arg->ctx);
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+
28697+out:
28698+ AuTraceErr(err);
28699+ return err;
28700+}
28701+
28702+static int au_rdu(struct file *file, struct aufs_rdu *rdu)
28703+{
28704+ int err;
28705+ aufs_bindex_t bbot;
28706+ struct au_rdu_arg arg = {
28707+ .ctx = {
28708+ .actor = au_rdu_fill
28709+ }
28710+ };
28711+ struct dentry *dentry;
28712+ struct inode *inode;
28713+ struct file *h_file;
28714+ struct au_rdu_cookie *cookie = &rdu->cookie;
28715+
28716+ /* VERIFY_WRITE */
28717+ err = !access_ok(rdu->ent.e, rdu->sz);
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;
28732+ if (unlikely(!file->f_op->iterate && !file->f_op->iterate_shared))
28733+ goto out;
28734+
28735+ err = security_file_permission(file, MAY_READ);
28736+ AuTraceErr(err);
28737+ if (unlikely(err))
28738+ goto out;
28739+
28740+ dentry = file->f_path.dentry;
28741+ inode = d_inode(dentry);
28742+ inode_lock_shared(inode);
28743+
28744+ arg.sb = inode->i_sb;
28745+ err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
28746+ if (unlikely(err))
28747+ goto out_mtx;
28748+ err = au_alive_dir(dentry);
28749+ if (unlikely(err))
28750+ goto out_si;
28751+ /* todo: reval? */
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+ }
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;
28771+ cookie->bindex++, cookie->h_pos = 0) {
28772+ h_file = au_hf_dir(file, cookie->bindex);
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);
28791+ fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
28792+ ii_read_unlock(inode);
28793+
28794+out_unlock:
28795+ fi_read_unlock(file);
28796+out_si:
28797+ si_read_unlock(arg.sb);
28798+out_mtx:
28799+ inode_unlock_shared(inode);
28800+out:
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;
28817+ sb = file->f_path.dentry->d_sb;
28818+ si_read_lock(sb, AuLock_FLUSH);
28819+ while (nent-- > 0) {
28820+ /* unnecessary to support mmap_sem since this is a dir */
28821+ err = copy_from_user(&ent, u->e, sizeof(ent));
28822+ if (!err)
28823+ /* VERIFY_WRITE */
28824+ err = !access_ok(&u->e->ino, sizeof(ino));
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+{
28859+ AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
28860+ "%llu, b%d, 0x%x, g%u}\n",
28861+ rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
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);
28866+
28867+ if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
28868+ return 0;
28869+
28870+ AuDbg("%u:%u\n",
28871+ rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
28872+ return -EINVAL;
28873+}
28874+
28875+long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
28876+{
28877+ long err, e;
28878+ struct aufs_rdu rdu;
28879+ void __user *p = (void __user *)arg;
28880+
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);
28888+ if (unlikely(err))
28889+ goto out;
28890+
28891+ switch (cmd) {
28892+ case AUFS_CTL_RDU:
28893+ err = au_rdu(file, &rdu);
28894+ if (unlikely(err))
28895+ break;
28896+
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:
28908+ /* err = -ENOTTY; */
28909+ err = -EINVAL;
28910+ }
28911+
28912+out:
28913+ AuTraceErr(err);
28914+ return err;
28915+}
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+
28959+out:
28960+ AuTraceErr(err);
28961+ return err;
28962+}
28963+#endif
28964diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
28965--- /usr/share/empty/fs/aufs/rwsem.h 1970-01-01 01:00:00.000000000 +0100
28966+++ linux/fs/aufs/rwsem.h 2021-12-03 15:40:58.236647297 +0100
28967@@ -0,0 +1,85 @@
28968+/* SPDX-License-Identifier: GPL-2.0 */
28969+/*
28970+ * Copyright (C) 2005-2021 Junjiro R. Okajima
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.
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
28983+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
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+
28995+#include "debug.h"
28996+
28997+/* in the future, the name 'au_rwsem' will be totally gone */
28998+#define au_rwsem rw_semaphore
28999+
29000+/* to debug easier, do not make them inlined functions */
29001+#define AuRwMustNoWaiters(rw) AuDebugOn(rwsem_is_contended(rw))
29002+
29003+#ifdef CONFIG_LOCKDEP
29004+/* rwsem_is_locked() is unusable */
29005+#define AuRwMustReadLock(rw) AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP) \
29006+ && !lockdep_recursing(current) \
29007+ && debug_locks \
29008+ && !lockdep_is_held_type(rw, 1))
29009+#define AuRwMustWriteLock(rw) AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP) \
29010+ && !lockdep_recursing(current) \
29011+ && debug_locks \
29012+ && !lockdep_is_held_type(rw, 0))
29013+#define AuRwMustAnyLock(rw) AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP) \
29014+ && !lockdep_recursing(current) \
29015+ && debug_locks \
29016+ && !lockdep_is_held(rw))
29017+#define AuRwDestroy(rw) AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP) \
29018+ && !lockdep_recursing(current) \
29019+ && debug_locks \
29020+ && lockdep_is_held(rw))
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
29027+
29028+#define au_rw_init(rw) init_rwsem(rw)
29029+
29030+#define au_rw_init_wlock(rw) do { \
29031+ au_rw_init(rw); \
29032+ down_write(rw); \
29033+ } while (0)
29034+
29035+#define au_rw_init_wlock_nested(rw, lsc) do { \
29036+ au_rw_init(rw); \
29037+ down_write_nested(rw, lsc); \
29038+ } while (0)
29039+
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)
29050+
29051+#endif /* __KERNEL__ */
29052+#endif /* __AUFS_RWSEM_H__ */
29053diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
29054--- /usr/share/empty/fs/aufs/sbinfo.c 1970-01-01 01:00:00.000000000 +0100
29055+++ linux/fs/aufs/sbinfo.c 2021-12-03 15:38:59.939980643 +0100
29056@@ -0,0 +1,314 @@
29057+// SPDX-License-Identifier: GPL-2.0
29058+/*
29059+ * Copyright (C) 2005-2021 Junjiro R. Okajima
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.
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
29072+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
29073+ */
29074+
29075+/*
29076+ * superblock private data
29077+ */
29078+
29079+#include <linux/iversion.h>
29080+#include "aufs.h"
29081+
29082+/*
29083+ * they are necessary regardless sysfs is disabled.
29084+ */
29085+void au_si_free(struct kobject *kobj)
29086+{
29087+ int i;
29088+ struct au_sbinfo *sbinfo;
29089+ char *locked __maybe_unused; /* debug only */
29090+
29091+ sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
29092+ for (i = 0; i < AuPlink_NHASH; i++)
29093+ AuDebugOn(!hlist_bl_empty(sbinfo->si_plink + i));
29094+ AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
29095+
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);
29100+
29101+ dbgaufs_si_fin(sbinfo);
29102+ au_rw_write_lock(&sbinfo->si_rwsem);
29103+ au_br_free(sbinfo);
29104+ au_rw_write_unlock(&sbinfo->si_rwsem);
29105+
29106+ au_kfree_try_rcu(sbinfo->si_branch);
29107+ mutex_destroy(&sbinfo->si_xib_mtx);
29108+ AuRwDestroy(&sbinfo->si_rwsem);
29109+
29110+ au_lcnt_wait_for_fin(&sbinfo->si_ninodes);
29111+ /* si_nfiles is waited too */
29112+ au_kfree_rcu(sbinfo);
29113+}
29114+
29115+int au_si_alloc(struct super_block *sb)
29116+{
29117+ int err, i;
29118+ struct au_sbinfo *sbinfo;
29119+
29120+ err = -ENOMEM;
29121+ sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
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))
29128+ goto out_sbinfo;
29129+
29130+ err = sysaufs_si_init(sbinfo);
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+ }
29137+ if (unlikely(err))
29138+ goto out_br;
29139+
29140+ au_nwt_init(&sbinfo->si_nowait);
29141+ au_rw_init_wlock(&sbinfo->si_rwsem);
29142+
29143+ au_lcnt_init(&sbinfo->si_ninodes, /*release*/NULL);
29144+ au_lcnt_init(&sbinfo->si_nfiles, /*release*/NULL);
29145+
29146+ sbinfo->si_bbot = -1;
29147+ sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
29148+
29149+ sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
29150+ sbinfo->si_wbr_create = AuWbrCreate_Def;
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;
29153+
29154+ au_fhsm_init(sbinfo);
29155+
29156+ sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
29157+
29158+ sbinfo->si_xino_jiffy = jiffies;
29159+ sbinfo->si_xino_expire
29160+ = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
29161+ mutex_init(&sbinfo->si_xib_mtx);
29162+ /* leave si_xib_last_pindex and si_xib_next_bit */
29163+
29164+ INIT_HLIST_BL_HEAD(&sbinfo->si_aopen);
29165+
29166+ sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
29167+ sbinfo->si_rdblk = AUFS_RDBLK_DEF;
29168+ sbinfo->si_rdhash = AUFS_RDHASH_DEF;
29169+ sbinfo->si_dirwh = AUFS_DIRWH_DEF;
29170+
29171+ for (i = 0; i < AuPlink_NHASH; i++)
29172+ INIT_HLIST_BL_HEAD(sbinfo->si_plink + i);
29173+ init_waitqueue_head(&sbinfo->si_plink_wq);
29174+ spin_lock_init(&sbinfo->si_plink_maint_lock);
29175+
29176+ INIT_HLIST_BL_HEAD(&sbinfo->si_files);
29177+
29178+ /* with getattr by default */
29179+ sbinfo->si_iop_array = aufs_iop;
29180+
29181+ /* leave other members for sysaufs and si_mnt. */
29182+ sbinfo->si_sb = sb;
29183+ sb->s_fs_info = sbinfo;
29184+ si_pid_set(sb);
29185+ return 0; /* success */
29186+
29187+out_br:
29188+ au_kfree_try_rcu(sbinfo->si_branch);
29189+out_sbinfo:
29190+ au_kfree_rcu(sbinfo);
29191+out:
29192+ return err;
29193+}
29194+
29195+int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
29196+{
29197+ int err, sz;
29198+ struct au_branch **brp;
29199+
29200+ AuRwMustWriteLock(&sbinfo->si_rwsem);
29201+
29202+ err = -ENOMEM;
29203+ sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
29204+ if (unlikely(!sz))
29205+ sz = sizeof(*brp);
29206+ brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
29207+ may_shrink);
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;
29221+ struct inode *inode;
29222+
29223+ SiMustWriteLock(sb);
29224+
29225+ gen = ++au_sbi(sb)->si_generation;
29226+ au_update_digen(sb->s_root);
29227+ inode = d_inode(sb->s_root);
29228+ au_update_iigen(inode, /*half*/0);
29229+ inode_inc_iversion(inode);
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+
29239+ SiMustWriteLock(sb);
29240+
29241+ sbinfo = au_sbi(sb);
29242+ for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
29243+ br_id = ++sbinfo->si_last_br_id;
29244+ AuDebugOn(br_id < 0);
29245+ if (br_id && au_br_index(sb, br_id) < 0)
29246+ return br_id;
29247+ }
29248+
29249+ return -1;
29250+}
29251+
29252+/* ---------------------------------------------------------------------- */
29253+
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+
29286+/* dentry and super_block lock. call at entry point */
29287+int aufs_read_lock(struct dentry *dentry, int flags)
29288+{
29289+ int err;
29290+ struct super_block *sb;
29291+
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));
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);
29308+ if (unlikely(err))
29309+ aufs_read_unlock(dentry, flags);
29310+ }
29311+
29312+out:
29313+ return err;
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+{
29327+ si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
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+
29337+int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
29338+{
29339+ int err;
29340+ unsigned int sigen;
29341+ struct super_block *sb;
29342+
29343+ sb = d1->d_sb;
29344+ err = si_read_lock(sb, flags);
29345+ if (unlikely(err))
29346+ goto out;
29347+
29348+ di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
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:
29363+ return err;
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+}
29371diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
29372--- /usr/share/empty/fs/aufs/super.c 1970-01-01 01:00:00.000000000 +0100
29373+++ linux/fs/aufs/super.c 2021-12-03 15:40:58.236647297 +0100
29374@@ -0,0 +1,1050 @@
29375+// SPDX-License-Identifier: GPL-2.0
29376+/*
29377+ * Copyright (C) 2005-2021 Junjiro R. Okajima
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.
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
29390+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
29391+ */
29392+
29393+/*
29394+ * mount and super_block operations
29395+ */
29396+
29397+#include <linux/iversion.h>
29398+#include <linux/mm.h>
29399+#include <linux/seq_file.h>
29400+#include <linux/statfs.h>
29401+#include <linux/vmalloc.h>
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) {
29413+ au_icntnr_init(c);
29414+ inode_set_iversion(&c->vfs_inode, 1); /* sigen(sb); */
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+{
29423+ if (!au_is_bad_inode(inode))
29424+ au_iinfo_fin(inode);
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));
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)
29449+ inode_inc_iversion(inode);
29450+ else {
29451+ iget_failed(inode);
29452+ inode = ERR_PTR(err);
29453+ }
29454+
29455+out:
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;
29466+ aufs_bindex_t bindex, bbot;
29467+ struct path path;
29468+ struct au_hdentry *hdp;
29469+ struct au_branch *br;
29470+ au_br_perm_str_t perm;
29471+
29472+ err = 0;
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++) {
29477+ br = au_sbr(sb, bindex);
29478+ path.mnt = au_br_mnt(br);
29479+ path.dentry = hdp->hd_dentry;
29480+ err = au_seq_path(seq, &path);
29481+ if (!err) {
29482+ au_optstr_br_perm(&perm, br->br_perm);
29483+ seq_printf(seq, "=%s", perm.a);
29484+ if (bindex != bbot)
29485+ seq_putc(seq, ':');
29486+ }
29487+ }
29488+ if (unlikely(err || seq_has_overflowed(seq)))
29489+ err = -E2BIG;
29490+
29491+ return err;
29492+}
29493+
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+
29507+static void au_show_wbr_create(struct seq_file *m, int v,
29508+ struct au_sbinfo *sbinfo)
29509+{
29510+ const char *pat;
29511+ char fmt[32];
29512+ struct au_wbr_mfs *mfs;
29513+
29514+ AuRwMustAnyLock(&sbinfo->si_rwsem);
29515+
29516+ seq_puts(m, ",create=");
29517+ pat = au_optstr_wbr_create(v);
29518+ mfs = &sbinfo->si_wbr_mfs;
29519+ switch (v) {
29520+ case AuWbrCreate_TDP:
29521+ case AuWbrCreate_RR:
29522+ case AuWbrCreate_MFS:
29523+ case AuWbrCreate_PMFS:
29524+ seq_puts(m, pat);
29525+ break;
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);
29531+ break;
29532+ case AuWbrCreate_MFSV:
29533+ case AuWbrCreate_PMFSV:
29534+ au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
29535+ seq_printf(m, fmt,
29536+ jiffies_to_msecs(mfs->mfs_expire)
29537+ / MSEC_PER_SEC);
29538+ break;
29539+ case AuWbrCreate_MFSRRV:
29540+ case AuWbrCreate_TDMFSV:
29541+ case AuWbrCreate_PMFSRRV:
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);
29545+ break;
29546+ default:
29547+ BUG();
29548+ }
29549+}
29550+
29551+static int au_show_xino(struct seq_file *seq, struct super_block *sb)
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;
29559+ struct qstr *name;
29560+ struct file *f;
29561+ struct dentry *d, *h_root;
29562+ struct au_branch *br;
29563+
29564+ AuRwMustAnyLock(&sbinfo->si_rwsem);
29565+
29566+ err = 0;
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;
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);
29577+ }
29578+
29579+ d = f->f_path.dentry;
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+
29590+out:
29591+ return err;
29592+#endif
29593+}
29594+
29595+/* seq_file will re-call me in case of too long string */
29596+static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
29597+{
29598+ int err;
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+
29620+ sb = dentry->d_sb;
29621+ if (sb->s_flags & SB_POSIXACL)
29622+ seq_puts(m, ",acl");
29623+#if 0 /* reserved for future use */
29624+ if (sb->s_flags & SB_I_VERSION)
29625+ seq_puts(m, ",i_version");
29626+#endif
29627+
29628+ /* lock free root dinfo */
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)) {
29635+ err = au_show_xino(m, sb);
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);
29643+ AuBool(SHWH, shwh);
29644+ AuBool(PLINK, plink);
29645+ AuBool(DIO, dio);
29646+ AuBool(DIRPERM1, dirperm1);
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+
29662+ v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
29663+ AuUInt(RDCACHE, rdcache, v);
29664+
29665+ AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
29666+ AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
29667+
29668+ au_fhsm_show(m, sbinfo);
29669+
29670+ AuBool(DIRREN, dirren);
29671+ AuBool(SUM, sum);
29672+ /* AuBool(SUM_W, wsum); */
29673+ AuBool(WARN_PERM, warn_perm);
29674+ AuBool(VERBOSE, verbose);
29675+
29676+out:
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+
29685+#undef AuBool
29686+#undef AuStr
29687+#undef AuUInt
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;
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)
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;
29719+ long bsize, factor;
29720+ u64 blocks, bfree, bavail, files, ffree;
29721+ aufs_bindex_t bbot, bindex, i;
29722+ unsigned char shared;
29723+ struct path h_path;
29724+ struct super_block *h_sb;
29725+
29726+ err = 0;
29727+ bsize = LONG_MAX;
29728+ files = 0;
29729+ ffree = 0;
29730+ blocks = 0;
29731+ bfree = 0;
29732+ bavail = 0;
29733+ bbot = au_sbbot(sb);
29734+ for (bindex = 0; bindex <= bbot; bindex++) {
29735+ h_path.mnt = au_sbr_mnt(sb, bindex);
29736+ h_sb = h_path.mnt->mnt_sb;
29737+ shared = 0;
29738+ for (i = 0; !shared && i < bindex; i++)
29739+ shared = (au_sbr_sb(sb, i) == h_sb);
29740+ if (shared)
29741+ continue;
29742+
29743+ /* sb->s_root for NFS is unreliable */
29744+ h_path.dentry = h_path.mnt->mnt_root;
29745+ err = vfs_statfs(&h_path, buf);
29746+ if (unlikely(err))
29747+ goto out;
29748+
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));
29768+ files = au_add_till_max(files, buf->f_files);
29769+ ffree = au_add_till_max(ffree, buf->f_ffree);
29770+ }
29771+
29772+ buf->f_bsize = bsize;
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;
29778+ buf->f_frsize = 0;
29779+
29780+out:
29781+ return err;
29782+}
29783+
29784+static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
29785+{
29786+ int err;
29787+ struct path h_path;
29788+ struct super_block *sb;
29789+
29790+ /* lock free root dinfo */
29791+ sb = dentry->d_sb;
29792+ si_noflush_read_lock(sb);
29793+ if (!au_opt_test(au_mntflags(sb), SUM)) {
29794+ /* sb->s_root for NFS is unreliable */
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
29799+ err = au_statfs_sum(sb, buf);
29800+ si_read_unlock(sb);
29801+
29802+ if (!err) {
29803+ buf->f_type = AUFS_SUPER_MAGIC;
29804+ buf->f_namelen = AUFS_MAX_NAMELEN;
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+
29814+static int aufs_sync_fs(struct super_block *sb, int wait)
29815+{
29816+ int err, e;
29817+ aufs_bindex_t bbot, bindex;
29818+ struct au_branch *br;
29819+ struct super_block *h_sb;
29820+
29821+ err = 0;
29822+ si_noflush_read_lock(sb);
29823+ bbot = au_sbbot(sb);
29824+ for (bindex = 0; bindex <= bbot; bindex++) {
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);
29830+ e = vfsub_sync_filesystem(h_sb, wait);
29831+ if (unlikely(e && !err))
29832+ err = e;
29833+ /* go on even if an error happens */
29834+ }
29835+ si_read_unlock(sb);
29836+
29837+ return err;
29838+}
29839+
29840+/* ---------------------------------------------------------------------- */
29841+
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);
29848+ if (sbinfo)
29849+ kobject_put(&sbinfo->si_kobj);
29850+}
29851+
29852+/* ---------------------------------------------------------------------- */
29853+
29854+void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
29855+ struct super_block *sb, void *arg)
29856+{
29857+ void *array;
29858+ unsigned long long n, sz;
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+
29871+ sz = sizeof(array) * *hint;
29872+ array = kzalloc(sz, GFP_NOFS);
29873+ if (unlikely(!array))
29874+ array = vzalloc(sz);
29875+ if (unlikely(!array)) {
29876+ array = ERR_PTR(-ENOMEM);
29877+ goto out;
29878+ }
29879+
29880+ n = cb(sb, array, *hint, arg);
29881+ AuDebugOn(n > *hint);
29882+
29883+out:
29884+ *hint = n;
29885+ return array;
29886+}
29887+
29888+static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
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;
29899+ spin_lock(&sb->s_inode_list_lock);
29900+ list_for_each_entry(inode, head, i_sb_list) {
29901+ if (!au_is_bad_inode(inode)
29902+ && au_ii(inode)->ii_btop >= 0) {
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);
29911+ }
29912+ }
29913+ spin_unlock(&sb->s_inode_list_lock);
29914+
29915+ return n;
29916+}
29917+
29918+struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
29919+{
29920+ struct au_sbinfo *sbi;
29921+
29922+ sbi = au_sbi(sb);
29923+ *max = au_lcnt_read(&sbi->si_ninodes, /*do_rev*/1);
29924+ return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
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]);
29933+ kvfree(a);
29934+}
29935+
29936+/* ---------------------------------------------------------------------- */
29937+
29938+/*
29939+ * refresh dentry and inode at remount time.
29940+ */
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)
29944+{
29945+ int err;
29946+
29947+ di_write_lock_child(dentry);
29948+ di_read_lock_parent(parent, AuLock_IR);
29949+ err = au_refresh_dentry(dentry, parent);
29950+ if (!err && dir_flags)
29951+ au_hn_reset(d_inode(dentry), dir_flags);
29952+ di_read_unlock(parent, AuLock_IR);
29953+ di_write_unlock(dentry);
29954+
29955+ return err;
29956+}
29957+
29958+static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
29959+ struct au_sbinfo *sbinfo,
29960+ const unsigned int dir_flags, unsigned int do_idop)
29961+{
29962+ int err;
29963+ struct dentry *parent;
29964+
29965+ err = 0;
29966+ parent = dget_parent(dentry);
29967+ if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
29968+ if (d_really_is_positive(dentry)) {
29969+ if (!d_is_dir(dentry))
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+
29983+ if (!err) {
29984+ if (do_idop)
29985+ au_refresh_dop(dentry, /*force_reval*/0);
29986+ } else
29987+ au_refresh_dop(dentry, /*force_reval*/1);
29988+
29989+ AuTraceErr(err);
29990+ return err;
29991+}
29992+
29993+static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
29994+{
29995+ int err, i, j, ndentry, e;
29996+ unsigned int sigen;
29997+ struct au_dcsub_pages dpages;
29998+ struct au_dpage *dpage;
29999+ struct dentry **dentries, *d;
30000+ struct au_sbinfo *sbinfo;
30001+ struct dentry *root = sb->s_root;
30002+ const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
30003+
30004+ if (do_idop)
30005+ au_refresh_dop(root, /*force_reval*/0);
30006+
30007+ err = au_dpages_init(&dpages, GFP_NOFS);
30008+ if (unlikely(err))
30009+ goto out;
30010+ err = au_dcsub_pages(&dpages, root, NULL, NULL);
30011+ if (unlikely(err))
30012+ goto out_dpages;
30013+
30014+ sigen = au_sigen(sb);
30015+ sbinfo = au_sbi(sb);
30016+ for (i = 0; i < dpages.ndpage; i++) {
30017+ dpage = dpages.dpages + i;
30018+ dentries = dpage->dentries;
30019+ ndentry = dpage->ndentry;
30020+ for (j = 0; j < ndentry; j++) {
30021+ d = dentries[j];
30022+ e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
30023+ do_idop);
30024+ if (unlikely(e && !err))
30025+ err = e;
30026+ /* go on even err */
30027+ }
30028+ }
30029+
30030+out_dpages:
30031+ au_dpages_free(&dpages);
30032+out:
30033+ return err;
30034+}
30035+
30036+static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
30037+{
30038+ int err, e;
30039+ unsigned int sigen;
30040+ unsigned long long max, ull;
30041+ struct inode *inode, **array;
30042+
30043+ array = au_iarray_alloc(sb, &max);
30044+ err = PTR_ERR(array);
30045+ if (IS_ERR(array))
30046+ goto out;
30047+
30048+ err = 0;
30049+ sigen = au_sigen(sb);
30050+ for (ull = 0; ull < max; ull++) {
30051+ inode = array[ull];
30052+ if (unlikely(!inode))
30053+ break;
30054+
30055+ e = 0;
30056+ ii_write_lock_child(inode);
30057+ if (au_iigen(inode, NULL) != sigen) {
30058+ e = au_refresh_hinode_self(inode);
30059+ if (unlikely(e)) {
30060+ au_refresh_iop(inode, /*force_getattr*/1);
30061+ pr_err("error %d, i%lu\n", e, inode->i_ino);
30062+ if (!err)
30063+ err = e;
30064+ /* go on even if err */
30065+ }
30066+ }
30067+ if (!e && do_idop)
30068+ au_refresh_iop(inode, /*force_getattr*/0);
30069+ ii_write_unlock(inode);
30070+ }
30071+
30072+ au_iarray_free(array, max);
30073+
30074+out:
30075+ return err;
30076+}
30077+
30078+static void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
30079+{
30080+ int err, e;
30081+ unsigned int udba;
30082+ aufs_bindex_t bindex, bbot;
30083+ struct dentry *root;
30084+ struct inode *inode;
30085+ struct au_branch *br;
30086+ struct au_sbinfo *sbi;
30087+
30088+ au_sigen_inc(sb);
30089+ sbi = au_sbi(sb);
30090+ au_fclr_si(sbi, FAILED_REFRESH_DIR);
30091+
30092+ root = sb->s_root;
30093+ DiMustNoWaiters(root);
30094+ inode = d_inode(root);
30095+ IiMustNoWaiters(inode);
30096+
30097+ udba = au_opt_udba(sb);
30098+ bbot = au_sbbot(sb);
30099+ for (bindex = 0; bindex <= bbot; bindex++) {
30100+ br = au_sbr(sb, bindex);
30101+ err = au_hnotify_reset_br(udba, br, br->br_perm);
30102+ if (unlikely(err))
30103+ AuIOErr("hnotify failed on br %d, %d, ignored\n",
30104+ bindex, err);
30105+ /* go on even if err */
30106+ }
30107+ au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
30108+
30109+ if (do_idop) {
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;
30113+ AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
30114+ sbi->si_iop_array = aufs_iop_nogetattr;
30115+ } else {
30116+ AuDebugOn(sb->s_d_op == &aufs_dop);
30117+ sb->s_d_op = &aufs_dop;
30118+ AuDebugOn(sbi->si_iop_array == aufs_iop);
30119+ sbi->si_iop_array = aufs_iop;
30120+ }
30121+ pr_info("reset to %ps and %ps\n",
30122+ sb->s_d_op, sbi->si_iop_array);
30123+ }
30124+
30125+ di_write_unlock(root);
30126+ err = au_refresh_d(sb, do_idop);
30127+ e = au_refresh_i(sb, do_idop);
30128+ if (unlikely(e && !err))
30129+ err = e;
30130+ /* aufs_write_lock() calls ..._child() */
30131+ di_write_lock_child(root);
30132+
30133+ au_cpup_attr_all(inode, /*force*/1);
30134+
30135+ if (unlikely(err))
30136+ AuIOErr("refresh failed, ignored, %d\n", err);
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+{
30156+ int err, do_dx;
30157+ unsigned int mntflags;
30158+ struct au_opts opts = {
30159+ .opt = NULL
30160+ };
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) {
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+ }
30174+ goto out;
30175+ }
30176+
30177+ err = -ENOMEM;
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);
30191+ inode = d_inode(root);
30192+ inode_lock(inode);
30193+ err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
30194+ if (unlikely(err))
30195+ goto out_mtx;
30196+ di_write_lock_child(root);
30197+
30198+ /* au_opts_remount() may return an error */
30199+ err = au_opts_remount(sb, &opts);
30200+ au_opts_free(&opts);
30201+
30202+ if (au_ftest_opts(opts.flags, REFRESH))
30203+ au_remount_refresh(sb, au_ftest_opts(opts.flags, REFRESH_IDOP));
30204+
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+
30211+ au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
30212+ aufs_write_unlock(root);
30213+
30214+out_mtx:
30215+ inode_unlock(inode);
30216+out_opts:
30217+ free_page((unsigned long)opts.opt);
30218+out:
30219+ err = cvt_err(err);
30220+ AuTraceErr(err);
30221+ return err;
30222+}
30223+
30224+static const struct super_operations aufs_sop = {
30225+ .alloc_inode = aufs_alloc_inode,
30226+ .destroy_inode = aufs_destroy_inode,
30227+ .free_inode = aufs_free_inode,
30228+ /* always deleting, no clearing */
30229+ .drop_inode = generic_delete_inode,
30230+ .show_options = aufs_show_options,
30231+ .statfs = aufs_statfs,
30232+ .put_super = aufs_put_super,
30233+ .sync_fs = aufs_sync_fs,
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+
30251+ inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
30252+ inode->i_fop = &aufs_dir_fop;
30253+ inode->i_mode = S_IFDIR;
30254+ set_nlink(inode, 2);
30255+ unlock_new_inode(inode);
30256+
30257+ root = d_make_root(inode);
30258+ if (unlikely(!root))
30259+ goto out;
30260+ err = PTR_ERR(root);
30261+ if (IS_ERR(root))
30262+ goto out;
30263+
30264+ err = au_di_init(root);
30265+ if (!err) {
30266+ sb->s_root = root;
30267+ return 0; /* success */
30268+ }
30269+ dput(root);
30270+
30271+out:
30272+ return err;
30273+}
30274+
30275+static int aufs_fill_super(struct super_block *sb, void *raw_data,
30276+ int silent __maybe_unused)
30277+{
30278+ int err;
30279+ struct au_opts opts = {
30280+ .opt = NULL
30281+ };
30282+ struct au_sbinfo *sbinfo;
30283+ struct dentry *root;
30284+ struct inode *inode;
30285+ char *arg = raw_data;
30286+
30287+ if (unlikely(!arg || !*arg)) {
30288+ err = -EINVAL;
30289+ pr_err("no arg\n");
30290+ goto out;
30291+ }
30292+
30293+ err = -ENOMEM;
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;
30303+ sbinfo = au_sbi(sb);
30304+
30305+ /* all timestamps always follow the ones on the branch */
30306+ sb->s_flags |= SB_NOATIME | SB_NODIRATIME;
30307+ sb->s_flags |= SB_I_VERSION; /* do we really need this? */
30308+ sb->s_op = &aufs_sop;
30309+ sb->s_d_op = &aufs_dop;
30310+ sb->s_magic = AUFS_SUPER_MAGIC;
30311+ sb->s_maxbytes = 0;
30312+ sb->s_stack_depth = 1;
30313+ au_export_init(sb);
30314+ au_xattr_init(sb);
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;
30322+ inode = d_inode(root);
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. */
30336+ inode_lock(inode);
30337+ aufs_write_lock(root);
30338+ err = au_opts_mount(sb, &opts);
30339+ au_opts_free(&opts);
30340+ if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
30341+ sb->s_d_op = &aufs_dop_noreval;
30342+ pr_info("%ps\n", sb->s_d_op);
30343+ au_refresh_dop(root, /*force_reval*/0);
30344+ sbinfo->si_iop_array = aufs_iop_nogetattr;
30345+ au_refresh_iop(inode, /*force_getattr*/0);
30346+ }
30347+ aufs_write_unlock(root);
30348+ inode_unlock(inode);
30349+ if (!err)
30350+ goto out_opts; /* success */
30351+
30352+out_root:
30353+ dput(root);
30354+ sb->s_root = NULL;
30355+out_info:
30356+ kobject_put(&sbinfo->si_kobj);
30357+ sb->s_fs_info = NULL;
30358+out_opts:
30359+ free_page((unsigned long)opts.opt);
30360+out:
30361+ AuTraceErr(err);
30362+ err = cvt_err(err);
30363+ AuTraceErr(err);
30364+ return err;
30365+}
30366+
30367+/* ---------------------------------------------------------------------- */
30368+
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)
30372+{
30373+ struct dentry *root;
30374+
30375+ /* all timestamps always follow the ones on the branch */
30376+ /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
30377+ root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
30378+ if (IS_ERR(root))
30379+ goto out;
30380+
30381+ au_sbilist_add(root->d_sb);
30382+
30383+out:
30384+ return root;
30385+}
30386+
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);
30395+ au_fhsm_fin(sb);
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);
30400+ au_remount_refresh(sb, /*do_idop*/0);
30401+ }
30402+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
30403+ au_plink_put(sb, /*verbose*/1);
30404+ au_xino_clr(sb);
30405+ au_dr_opt_flush(sb);
30406+ sbinfo->si_sb = NULL;
30407+ aufs_write_unlock(sb->s_root);
30408+ au_nwt_flush(&sbinfo->si_nowait);
30409+ }
30410+ kill_anon_super(sb);
30411+}
30412+
30413+struct file_system_type aufs_fs_type = {
30414+ .name = AUFS_FSTYPE,
30415+ /* a race between rename and others */
30416+ .fs_flags = FS_RENAME_DOES_D_MOVE
30417+ /* untested */
30418+ /*| FS_ALLOW_IDMAP*/
30419+ ,
30420+ .mount = aufs_mount,
30421+ .kill_sb = aufs_kill_sb,
30422+ /* no need to __module_get() and module_put(). */
30423+ .owner = THIS_MODULE,
30424+};
30425diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
30426--- /usr/share/empty/fs/aufs/super.h 1970-01-01 01:00:00.000000000 +0100
30427+++ linux/fs/aufs/super.h 2021-12-03 15:38:59.939980643 +0100
30428@@ -0,0 +1,587 @@
30429+/* SPDX-License-Identifier: GPL-2.0 */
30430+/*
30431+ * Copyright (C) 2005-2021 Junjiro R. Okajima
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.
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
30444+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
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>
30457+#include <linux/kobject.h>
30458+#include "hbl.h"
30459+#include "lcnt.h"
30460+#include "rwsem.h"
30461+#include "wkq.h"
30462+
30463+/* policies to select one among multiple writable branches */
30464+struct au_wbr_copyup_operations {
30465+ int (*copyup)(struct dentry *dentry);
30466+};
30467+
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+
30475+struct au_wbr_create_operations {
30476+ int (*create)(struct dentry *dentry, unsigned int flags);
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+
30491+#define AuPlink_NHASH 100
30492+static inline int au_plink_hash(ino_t ino)
30493+{
30494+ return ino % AuPlink_NHASH;
30495+}
30496+
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+
30506+ /* these are protected by si_rwsem */
30507+ unsigned long fhsm_expire;
30508+ aufs_bindex_t fhsm_bottom;
30509+#endif
30510+};
30511+
30512+struct au_branch;
30513+struct au_sbinfo {
30514+ /* nowait tasks in the system-wide workqueue */
30515+ struct au_nowait_tasks si_nowait;
30516+
30517+ /*
30518+ * tried sb->s_umount, but failed due to the dependency between i_mutex.
30519+ * rwsem for au_sbinfo is necessary.
30520+ */
30521+ struct au_rwsem si_rwsem;
30522+
30523+ /*
30524+ * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
30525+ * remount.
30526+ */
30527+ au_lcnt_t si_ninodes, si_nfiles;
30528+
30529+ /* branch management */
30530+ unsigned int si_generation;
30531+
30532+ /* see AuSi_ flags */
30533+ unsigned char au_si_status;
30534+
30535+ aufs_bindex_t si_bbot;
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;
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+
30554+ /* File-based Hierarchical Storage Management */
30555+ struct au_fhsm si_fhsm;
30556+
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) */
30562+ loff_t si_ximaxent; /* max entries in a xino */
30563+
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;
30569+
30570+ unsigned long si_xino_jiffy;
30571+ unsigned long si_xino_expire;
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 */
30577+ /* todo: make xigen file an array to support many inode numbers */
30578+ struct file *si_xigen;
30579+ atomic_t si_xigen_next;
30580+#endif
30581+
30582+ /* dirty trick to support atomic_open */
30583+ struct hlist_bl_head si_aopen;
30584+
30585+ /* vdir parameters */
30586+ unsigned long si_rdcache; /* max cache time in jiffies */
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+
30598+ /* pseudo_link list */
30599+ struct hlist_bl_head si_plink[AuPlink_NHASH];
30600+ wait_queue_head_t si_plink_wq;
30601+ spinlock_t si_plink_maint_lock;
30602+ pid_t si_plink_maint_pid;
30603+
30604+ /* file list */
30605+ struct hlist_bl_head si_files;
30606+
30607+ /* with/without getattr, brother of sb->s_d_op */
30608+ const struct inode_operations *si_iop_array;
30609+
30610+ /*
30611+ * sysfs and lifetime management.
30612+ * this is not a small structure and it may be a waste of memory in case
30613+ * of sysfs is disabled, particularly when many aufs-es are mounted.
30614+ * but using sysfs is majority.
30615+ */
30616+ struct kobject si_kobj;
30617+#ifdef CONFIG_DEBUG_FS
30618+ struct dentry *si_dbgaufs;
30619+ struct dentry *si_dbgaufs_plink;
30620+ struct dentry *si_dbgaufs_xib;
30621+#ifdef CONFIG_AUFS_EXPORT
30622+ struct dentry *si_dbgaufs_xigen;
30623+#endif
30624+#endif
30625+
30626+#ifdef CONFIG_AUFS_SBILIST
30627+ struct hlist_bl_node si_list;
30628+#endif
30629+
30630+ /* dirty, necessary for unmounting, sysfs and sysrq */
30631+ struct super_block *si_sb;
30632+};
30633+
30634+/* sbinfo status flags */
30635+/*
30636+ * set true when refresh_dirs() failed at remount time.
30637+ * then try refreshing dirs at access time again.
30638+ * if it is false, refreshing dirs at access time is unnecessary
30639+ */
30640+#define AuSi_FAILED_REFRESH_DIR 1
30641+#define AuSi_FHSM (1 << 1) /* fhsm is active now */
30642+#define AuSi_NO_DREVAL (1 << 2) /* disable all d_revalidate */
30643+
30644+#ifndef CONFIG_AUFS_FHSM
30645+#undef AuSi_FHSM
30646+#define AuSi_FHSM 0
30647+#endif
30648+
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+
30665+/* ---------------------------------------------------------------------- */
30666+
30667+/* policy to select one among writable branches */
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__))
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 */
30678+#define AuLock_DIRS (1 << 4) /* target is a pair of dirs */
30679+ /* except RENAME_EXCHANGE */
30680+#define AuLock_NOPLM (1 << 5) /* return err in plm mode */
30681+#define AuLock_NOPLMW (1 << 6) /* wait for plm mode ends */
30682+#define AuLock_GEN (1 << 7) /* test digen/iigen */
30683+#define au_ftest_lock(flags, name) ((flags) & AuLock_##name)
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)
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);
30694+typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
30695+ unsigned long long max, void *arg);
30696+void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30697+ struct super_block *sb, void *arg);
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);
30700+
30701+/* sbinfo.c */
30702+void au_si_free(struct kobject *kobj);
30703+int au_si_alloc(struct super_block *sb);
30704+int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
30705+
30706+unsigned int au_sigen_inc(struct super_block *sb);
30707+aufs_bindex_t au_new_br_id(struct super_block *sb);
30708+
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);
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);
30715+int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
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);
30722+int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
30723+int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
30724+
30725+/* mvdown.c */
30726+int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
30727+
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);
30746+void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
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)
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)
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+
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
30775+int au_test_nfsd(void);
30776+void au_export_init(struct super_block *sb);
30777+void au_xigen_inc(struct inode *inode);
30778+int au_xigen_new(struct inode *inode);
30779+int au_xigen_set(struct super_block *sb, struct path *path);
30780+void au_xigen_clr(struct super_block *sb);
30781+
30782+static inline int au_busy_or_stale(void)
30783+{
30784+ if (!au_test_nfsd())
30785+ return -EBUSY;
30786+ return -ESTALE;
30787+}
30788+#else
30789+AuStubInt0(au_test_nfsd, void)
30790+AuStubVoid(au_export_init, struct super_block *sb)
30791+AuStubVoid(au_xigen_inc, struct inode *inode)
30792+AuStubInt0(au_xigen_new, struct inode *inode)
30793+AuStubInt0(au_xigen_set, struct super_block *sb, struct path *path)
30794+AuStubVoid(au_xigen_clr, struct super_block *sb)
30795+AuStub(int, au_busy_or_stale, return -EBUSY, void)
30796+#endif /* CONFIG_AUFS_EXPORT */
30797+
30798+/* ---------------------------------------------------------------------- */
30799+
30800+#ifdef CONFIG_AUFS_SBILIST
30801+/* module.c */
30802+extern struct hlist_bl_head au_sbilist;
30803+
30804+static inline void au_sbilist_init(void)
30805+{
30806+ INIT_HLIST_BL_HEAD(&au_sbilist);
30807+}
30808+
30809+static inline void au_sbilist_add(struct super_block *sb)
30810+{
30811+ au_hbl_add(&au_sbi(sb)->si_list, &au_sbilist);
30812+}
30813+
30814+static inline void au_sbilist_del(struct super_block *sb)
30815+{
30816+ au_hbl_del(&au_sbi(sb)->si_list, &au_sbilist);
30817+}
30818+
30819+#ifdef CONFIG_AUFS_MAGIC_SYSRQ
30820+static inline void au_sbilist_lock(void)
30821+{
30822+ hlist_bl_lock(&au_sbilist);
30823+}
30824+
30825+static inline void au_sbilist_unlock(void)
30826+{
30827+ hlist_bl_unlock(&au_sbilist);
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 */
30835+#else
30836+AuStubVoid(au_sbilist_init, void)
30837+AuStubVoid(au_sbilist_add, struct super_block *sb)
30838+AuStubVoid(au_sbilist_del, struct super_block *sb)
30839+AuStubVoid(au_sbilist_lock, void)
30840+AuStubVoid(au_sbilist_unlock, void)
30841+#define AuGFP_SBILIST GFP_NOFS
30842+#endif
30843+
30844+/* ---------------------------------------------------------------------- */
30845+
30846+static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
30847+{
30848+ /*
30849+ * This function is a dynamic '__init' function actually,
30850+ * so the tiny check for si_rwsem is unnecessary.
30851+ */
30852+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
30853+#ifdef CONFIG_DEBUG_FS
30854+ sbinfo->si_dbgaufs = NULL;
30855+ sbinfo->si_dbgaufs_plink = NULL;
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+
30865+/* current->atomic_flags */
30866+/* this value should never corrupt the ones defined in linux/sched.h */
30867+#define PFA_AUFS 0x10
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 */
30872+
30873+static inline int si_pid_test(struct super_block *sb)
30874+{
30875+ return !!task_test_aufs(current);
30876+}
30877+
30878+static inline void si_pid_clr(struct super_block *sb)
30879+{
30880+ AuDebugOn(!task_test_aufs(current));
30881+ task_clear_aufs(current);
30882+}
30883+
30884+static inline void si_pid_set(struct super_block *sb)
30885+{
30886+ AuDebugOn(task_test_aufs(current));
30887+ task_set_aufs(current);
30888+}
30889+
30890+/* ---------------------------------------------------------------------- */
30891+
30892+/* lock superblock. mainly for entry point functions */
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)
30907+
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+
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+{
30920+ int locked;
30921+
30922+ locked = __si_read_trylock(sb);
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+{
30936+ int locked;
30937+
30938+ locked = __si_write_trylock(sb);
30939+ if (locked)
30940+ si_pid_set(sb);
30941+ return locked;
30942+}
30943+
30944+#if 0 /* reserved */
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+}
30951+#endif
30952+
30953+static inline void si_read_unlock(struct super_block *sb)
30954+{
30955+ si_pid_clr(sb);
30956+ __si_read_unlock(sb);
30957+}
30958+
30959+#if 0 /* reserved */
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+}
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+
30974+#if 0 /* reserved */
30975+static inline void si_downgrade_lock(struct super_block *sb)
30976+{
30977+ __si_downgrade_lock(sb);
30978+}
30979+#endif
30980+
30981+/* ---------------------------------------------------------------------- */
30982+
30983+static inline aufs_bindex_t au_sbbot(struct super_block *sb)
30984+{
30985+ SiMustAnyLock(sb);
30986+ return au_sbi(sb)->si_bbot;
30987+}
30988+
30989+static inline unsigned int au_mntflags(struct super_block *sb)
30990+{
30991+ SiMustAnyLock(sb);
30992+ return au_sbi(sb)->si_mntflags;
30993+}
30994+
30995+static inline unsigned int au_sigen(struct super_block *sb)
30996+{
30997+ SiMustAnyLock(sb);
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+{
31004+ SiMustAnyLock(sb);
31005+ return au_sbi(sb)->si_branch[0 + bindex];
31006+}
31007+
31008+static inline loff_t au_xi_maxent(struct super_block *sb)
31009+{
31010+ SiMustAnyLock(sb);
31011+ return au_sbi(sb)->si_ximaxent;
31012+}
31013+
31014+#endif /* __KERNEL__ */
31015+#endif /* __AUFS_SUPER_H__ */
31016diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
31017--- /usr/share/empty/fs/aufs/sysaufs.c 1970-01-01 01:00:00.000000000 +0100
31018+++ linux/fs/aufs/sysaufs.c 2021-12-03 15:38:59.939980643 +0100
31019@@ -0,0 +1,93 @@
31020+// SPDX-License-Identifier: GPL-2.0
31021+/*
31022+ * Copyright (C) 2005-2021 Junjiro R. Okajima
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.
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
31035+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
31036+ */
31037+
31038+/*
31039+ * sysfs interface and lifetime management
31040+ * they are necessary regardless sysfs is disabled.
31041+ */
31042+
31043+#include <linux/random.h>
31044+#include "aufs.h"
31045+
31046+unsigned long sysaufs_si_mask;
31047+struct kset *sysaufs_kset;
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+
31060+static const struct sysfs_ops au_sbi_ops = {
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+
31076+ sbinfo->si_kobj.kset = sysaufs_kset;
31077+ /* cf. sysaufs_name() */
31078+ err = kobject_init_and_add
31079+ (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
31080+ SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
31081+
31082+ return err;
31083+}
31084+
31085+void sysaufs_fin(void)
31086+{
31087+ sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31088+ kset_unregister(sysaufs_kset);
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+
31099+ err = -EINVAL;
31100+ sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
31101+ if (unlikely(!sysaufs_kset))
31102+ goto out;
31103+ err = PTR_ERR(sysaufs_kset);
31104+ if (IS_ERR(sysaufs_kset))
31105+ goto out;
31106+ err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31107+ if (unlikely(err))
31108+ kset_unregister(sysaufs_kset);
31109+
31110+out:
31111+ return err;
31112+}
31113diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
31114--- /usr/share/empty/fs/aufs/sysaufs.h 1970-01-01 01:00:00.000000000 +0100
31115+++ linux/fs/aufs/sysaufs.h 2021-12-03 15:38:59.939980643 +0100
31116@@ -0,0 +1,102 @@
31117+/* SPDX-License-Identifier: GPL-2.0 */
31118+/*
31119+ * Copyright (C) 2005-2021 Junjiro R. Okajima
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.
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
31132+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
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+
31144+#include <linux/sysfs.h>
31145+#include "module.h"
31146+
31147+struct super_block;
31148+struct au_sbinfo;
31149+
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;
31159+extern struct kset *sysaufs_kset;
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);
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
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+
31203+AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
31204+AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
31205+ struct attribute *attr, char *buf)
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)
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__ */
31219diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
31220--- /usr/share/empty/fs/aufs/sysfs.c 1970-01-01 01:00:00.000000000 +0100
31221+++ linux/fs/aufs/sysfs.c 2021-12-03 15:38:59.939980643 +0100
31222@@ -0,0 +1,374 @@
31223+// SPDX-License-Identifier: GPL-2.0
31224+/*
31225+ * Copyright (C) 2005-2021 Junjiro R. Okajima
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.
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
31238+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
31239+ */
31240+
31241+/*
31242+ * sysfs interface
31243+ */
31244+
31245+#include <linux/compat.h>
31246+#include <linux/seq_file.h>
31247+#include "aufs.h"
31248+
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+
31269+static struct attribute *au_attr[] = {
31270+#ifdef CONFIG_AUFS_FS_MODULE
31271+ &au_config_attr.attr,
31272+#endif
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+
31288+ SiMustAnyLock(sb);
31289+
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,
31304+ aufs_bindex_t bindex, int idx)
31305+{
31306+ int err;
31307+ struct path path;
31308+ struct dentry *root;
31309+ struct au_branch *br;
31310+ au_br_perm_str_t perm;
31311+
31312+ AuDbg("b%d\n", bindex);
31313+
31314+ err = 0;
31315+ root = sb->s_root;
31316+ di_read_lock_parent(root, !AuLock_IR);
31317+ br = au_sbr(sb, bindex);
31318+
31319+ switch (idx) {
31320+ case AuBrSysfs_BR:
31321+ path.mnt = au_br_mnt(br);
31322+ path.dentry = au_h_dptr(root, bindex);
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+ }
31328+ break;
31329+ case AuBrSysfs_BRID:
31330+ seq_printf(seq, "%d\n", br->br_id);
31331+ break;
31332+ }
31333+ di_read_unlock(root, !AuLock_IR);
31334+ if (unlikely(err || seq_has_overflowed(seq)))
31335+ err = -E2BIG;
31336+
31337+ return err;
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+
31358+#define SysaufsBr_PREFIX "br"
31359+#define SysaufsBrid_PREFIX "brid"
31360+
31361+/* todo: file size may exceed PAGE_SIZE */
31362+ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31363+ char *buf)
31364+{
31365+ ssize_t err;
31366+ int idx;
31367+ long l;
31368+ aufs_bindex_t bbot;
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;
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;
31391+
31392+ seq = au_seq(buf, PAGE_SIZE);
31393+ err = PTR_ERR(seq);
31394+ if (IS_ERR(seq))
31395+ goto out_unlock;
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+
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;
31415+ name += sizeof(SysaufsBr_PREFIX) - 1;
31416+ } else
31417+ BUG();
31418+
31419+ err = kstrtol(name, 10, &l);
31420+ if (!err) {
31421+ bbot = au_sbbot(sb);
31422+ if (l <= bbot)
31423+ err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
31424+ else
31425+ err = -ENOENT;
31426+ }
31427+
31428+out_seq:
31429+ if (!err) {
31430+ err = seq->count;
31431+ /* sysfs limit */
31432+ if (unlikely(err == PAGE_SIZE))
31433+ err = -EFBIG;
31434+ }
31435+ au_kfree_rcu(seq);
31436+out_unlock:
31437+ si_read_unlock(sb);
31438+out:
31439+ return err;
31440+}
31441+
31442+/* ---------------------------------------------------------------------- */
31443+
31444+static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
31445+{
31446+ int err;
31447+ int16_t brid;
31448+ aufs_bindex_t bindex, bbot;
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);
31455+ bbot = au_sbbot(sb);
31456+ err = bbot + 1;
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);
31471+ for (bindex = 0; bindex <= bbot; bindex++, arg++) {
31472+ /* VERIFY_WRITE */
31473+ err = !access_ok(arg, sizeof(*arg));
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+
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)) {
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:
31507+ au_kfree_rcu(seq);
31508+out_buf:
31509+ free_page((unsigned long)buf);
31510+out:
31511+ si_read_unlock(sb);
31512+ return err;
31513+}
31514+
31515+long au_brinfo_ioctl(struct file *file, unsigned long arg)
31516+{
31517+ return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
31518+}
31519+
31520+#ifdef CONFIG_COMPAT
31521+long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
31522+{
31523+ return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
31524+}
31525+#endif
31526+
31527+/* ---------------------------------------------------------------------- */
31528+
31529+void sysaufs_br_init(struct au_branch *br)
31530+{
31531+ int i;
31532+ struct au_brsysfs *br_sysfs;
31533+ struct attribute *attr;
31534+
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;
31540+ attr->mode = 0444;
31541+ br_sysfs++;
31542+ }
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;
31549+ struct au_brsysfs *br_sysfs;
31550+ int i;
31551+ aufs_bindex_t bbot;
31552+
31553+ if (!sysaufs_brs)
31554+ return;
31555+
31556+ kobj = &au_sbi(sb)->si_kobj;
31557+ bbot = au_sbbot(sb);
31558+ for (; bindex <= bbot; bindex++) {
31559+ br = au_sbr(sb, bindex);
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+ }
31565+ }
31566+}
31567+
31568+void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
31569+{
31570+ int err, i;
31571+ aufs_bindex_t bbot;
31572+ struct kobject *kobj;
31573+ struct au_branch *br;
31574+ struct au_brsysfs *br_sysfs;
31575+
31576+ if (!sysaufs_brs)
31577+ return;
31578+
31579+ kobj = &au_sbi(sb)->si_kobj;
31580+ bbot = au_sbbot(sb);
31581+ for (; bindex <= bbot; bindex++) {
31582+ br = au_sbr(sb, bindex);
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+ }
31595+ }
31596+}
31597diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
31598--- /usr/share/empty/fs/aufs/sysrq.c 1970-01-01 01:00:00.000000000 +0100
31599+++ linux/fs/aufs/sysrq.c 2021-12-03 15:38:59.939980643 +0100
31600@@ -0,0 +1,149 @@
31601+// SPDX-License-Identifier: GPL-2.0
31602+/*
31603+ * Copyright (C) 2005-2021 Junjiro R. Okajima
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.
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
31616+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
31617+ */
31618+
31619+/*
31620+ * magic sysrq handler
31621+ */
31622+
31623+/* #include <linux/sysrq.h> */
31624+#include <linux/writeback.h>
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;
31634+ struct hlist_bl_head *files;
31635+ struct hlist_bl_node *pos;
31636+ struct au_finfo *finfo;
31637+ struct inode *i;
31638+
31639+ plevel = au_plevel;
31640+ au_plevel = KERN_WARNING;
31641+
31642+ /* since we define pr_fmt, call printk directly */
31643+#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
31644+
31645+ sbinfo = au_sbi(sb);
31646+ printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
31647+ pr("superblock\n");
31648+ au_dpri_sb(sb);
31649+
31650+#if 0 /* reserved */
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+
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);
31678+ }
31679+ spin_unlock(&sb->s_inode_list_lock);
31680+
31681+ pr("files\n");
31682+ files = &au_sbi(sb)->si_files;
31683+ hlist_bl_lock(files);
31684+ hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
31685+ umode_t mode;
31686+
31687+ file = finfo->fi_file;
31688+ mode = file_inode(file)->i_mode;
31689+ if (!special_file(mode))
31690+ au_dpri_file(file);
31691+ }
31692+ hlist_bl_unlock(files);
31693+ pr("done\n");
31694+
31695+#undef pr
31696+ au_plevel = plevel;
31697+}
31698+
31699+/* ---------------------------------------------------------------------- */
31700+
31701+/* module parameter */
31702+static char *aufs_sysrq_key = "a";
31703+module_param_named(sysrq, aufs_sysrq_key, charp, 0444);
31704+MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
31705+
31706+static void au_sysrq(int key __maybe_unused)
31707+{
31708+ struct au_sbinfo *sbinfo;
31709+ struct hlist_bl_node *pos;
31710+
31711+ lockdep_off();
31712+ au_sbilist_lock();
31713+ hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
31714+ sysrq_sb(sbinfo->si_sb);
31715+ au_sbilist_unlock();
31716+ lockdep_on();
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))
31738+ pr_err("err %d, sysrq=%c\n", err, key);
31739+ return err;
31740+}
31741+
31742+void au_sysrq_fin(void)
31743+{
31744+ int err;
31745+
31746+ err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
31747+ if (unlikely(err))
31748+ pr_err("err %d (ignored)\n", err);
31749+}
31750diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
31751--- /usr/share/empty/fs/aufs/vdir.c 1970-01-01 01:00:00.000000000 +0100
31752+++ linux/fs/aufs/vdir.c 2021-12-03 15:38:59.939980643 +0100
31753@@ -0,0 +1,896 @@
31754+// SPDX-License-Identifier: GPL-2.0
31755+/*
31756+ * Copyright (C) 2005-2021 Junjiro R. Okajima
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.
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
31769+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
31770+ */
31771+
31772+/*
31773+ * virtual or vertical directory
31774+ */
31775+
31776+#include <linux/iversion.h>
31777+#include "aufs.h"
31778+
31779+static unsigned int calc_size(int nlen)
31780+{
31781+ return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
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+
31811+/* estimate the appropriate size for name hash table */
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;
31822+ /* pr_info("n %u\n", n); */
31823+ return n;
31824+}
31825+
31826+/*
31827+ * the allocated memory has to be freed by
31828+ * au_nhash_wh_free() or au_nhash_de_free().
31829+ */
31830+int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
31831+{
31832+ struct hlist_head *head;
31833+ unsigned int u;
31834+ size_t sz;
31835+
31836+ sz = sizeof(*nhash->nh_head) * num_hash;
31837+ head = kmalloc(sz, gfp);
31838+ if (head) {
31839+ nhash->nh_num = num_hash;
31840+ nhash->nh_head = head;
31841+ for (u = 0; u < num_hash; u++)
31842+ INIT_HLIST_HEAD(head++);
31843+ return 0; /* success */
31844+ }
31845+
31846+ return -ENOMEM;
31847+}
31848+
31849+static void nhash_count(struct hlist_head *head)
31850+{
31851+#if 0 /* debugging */
31852+ unsigned long n;
31853+ struct hlist_node *pos;
31854+
31855+ n = 0;
31856+ hlist_for_each(pos, head)
31857+ n++;
31858+ pr_info("%lu\n", n);
31859+#endif
31860+}
31861+
31862+static void au_nhash_wh_do_free(struct hlist_head *head)
31863+{
31864+ struct au_vdir_wh *pos;
31865+ struct hlist_node *node;
31866+
31867+ hlist_for_each_entry_safe(pos, node, head, wh_hash)
31868+ au_kfree_rcu(pos);
31869+}
31870+
31871+static void au_nhash_de_do_free(struct hlist_head *head)
31872+{
31873+ struct au_vdir_dehstr *pos;
31874+ struct hlist_node *node;
31875+
31876+ hlist_for_each_entry_safe(pos, node, head, hash)
31877+ au_cache_free_vdir_dehstr(pos);
31878+}
31879+
31880+static void au_nhash_do_free(struct au_nhash *nhash,
31881+ void (*free)(struct hlist_head *head))
31882+{
31883+ unsigned int n;
31884+ struct hlist_head *head;
31885+
31886+ n = nhash->nh_num;
31887+ if (!n)
31888+ return;
31889+
31890+ head = nhash->nh_head;
31891+ while (n-- > 0) {
31892+ nhash_count(head);
31893+ free(head++);
31894+ }
31895+ au_kfree_try_rcu(nhash->nh_head);
31896+}
31897+
31898+void au_nhash_wh_free(struct au_nhash *whlist)
31899+{
31900+ au_nhash_do_free(whlist, au_nhash_wh_do_free);
31901+}
31902+
31903+static void au_nhash_de_free(struct au_nhash *delist)
31904+{
31905+ au_nhash_do_free(delist, au_nhash_de_do_free);
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;
31916+ struct au_vdir_wh *pos;
31917+
31918+ num = 0;
31919+ n = whlist->nh_num;
31920+ head = whlist->nh_head;
31921+ for (u = 0; u < n; u++, head++)
31922+ hlist_for_each_entry(pos, head, wh_hash)
31923+ if (pos->wh_bindex == btgt && ++num > limit)
31924+ return 1;
31925+ return 0;
31926+}
31927+
31928+static struct hlist_head *au_name_hash(struct au_nhash *nhash,
31929+ unsigned char *name,
31930+ unsigned int len)
31931+{
31932+ unsigned int v;
31933+ /* const unsigned int magic_bit = 12; */
31934+
31935+ AuDebugOn(!nhash->nh_num || !nhash->nh_head);
31936+
31937+ v = 0;
31938+ if (len > 8)
31939+ len = 8;
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);
31951+}
31952+
31953+/* returns found or not */
31954+int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
31955+{
31956+ struct hlist_head *head;
31957+ struct au_vdir_wh *pos;
31958+ struct au_vdir_destr *str;
31959+
31960+ head = au_name_hash(whlist, name, nlen);
31961+ hlist_for_each_entry(pos, head, wh_hash) {
31962+ str = &pos->wh_str;
31963+ AuDbg("%.*s\n", str->len, str->name);
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;
31974+ struct au_vdir_dehstr *pos;
31975+ struct au_vdir_destr *str;
31976+
31977+ head = au_name_hash(delist, name, nlen);
31978+ hlist_for_each_entry(pos, head, hash) {
31979+ str = pos->str;
31980+ AuDbg("%.*s\n", str->len, str->name);
31981+ if (au_nhash_test_name(str, name, nlen))
31982+ return 1;
31983+ }
31984+ return 0;
31985+}
31986+
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)
32001+{
32002+ int err;
32003+ struct au_vdir_destr *str;
32004+ struct au_vdir_wh *wh;
32005+
32006+ AuDbg("%.*s\n", nlen, name);
32007+ AuDebugOn(!whlist->nh_num || !whlist->nh_head);
32008+
32009+ err = -ENOMEM;
32010+ wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
32011+ if (unlikely(!wh))
32012+ goto out;
32013+
32014+ err = 0;
32015+ wh->wh_bindex = bindex;
32016+ if (shwh)
32017+ au_shwh_init_wh(wh, ino, d_type);
32018+ str = &wh->wh_str;
32019+ str->len = nlen;
32020+ memcpy(str->name, name, nlen);
32021+ hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
32022+ /* smp_mb(); */
32023+
32024+out:
32025+ return err;
32026+}
32027+
32028+static int append_deblk(struct au_vdir *vdir)
32029+{
32030+ int err;
32031+ unsigned long ul;
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;
32037+ o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
32038+ GFP_NOFS, /*may_shrink*/0);
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+
32053+out:
32054+ return err;
32055+}
32056+
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;
32085+ dehstr = au_cache_alloc_vdir_dehstr();
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+
32102+out:
32103+ return err;
32104+}
32105+
32106+/* ---------------------------------------------------------------------- */
32107+
32108+void au_vdir_free(struct au_vdir *vdir)
32109+{
32110+ unsigned char **deblk;
32111+
32112+ deblk = vdir->vd_deblk;
32113+ while (vdir->vd_nblk--)
32114+ au_kfree_try_rcu(*deblk++);
32115+ au_kfree_try_rcu(vdir->vd_deblk);
32116+ au_cache_free_vdir(vdir);
32117+}
32118+
32119+static struct au_vdir *alloc_vdir(struct file *file)
32120+{
32121+ struct au_vdir *vdir;
32122+ struct super_block *sb;
32123+ int err;
32124+
32125+ sb = file->f_path.dentry->d_sb;
32126+ SiMustAnyLock(sb);
32127+
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;
32138+ if (!vdir->vd_deblk_sz) {
32139+ /* estimate the appropriate size for deblk */
32140+ vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
32141+ /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
32142+ }
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+
32150+ au_kfree_try_rcu(vdir->vd_deblk);
32151+
32152+out_free:
32153+ au_cache_free_vdir(vdir);
32154+out:
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) {
32165+ au_kfree_try_rcu(vdir->vd_deblk[vdir->vd_nblk - 1]);
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+
32183+#define AuFillVdir_CALLED 1
32184+#define AuFillVdir_WHABLE (1 << 1)
32185+#define AuFillVdir_SHWH (1 << 2)
32186+#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
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)
32191+
32192+#ifndef CONFIG_AUFS_SHWH
32193+#undef AuFillVdir_SHWH
32194+#define AuFillVdir_SHWH 0
32195+#endif
32196+
32197+struct fillvdir_arg {
32198+ struct dir_context ctx;
32199+ struct file *file;
32200+ struct au_vdir *vdir;
32201+ struct au_nhash delist;
32202+ struct au_nhash whlist;
32203+ aufs_bindex_t bindex;
32204+ unsigned int flags;
32205+ int err;
32206+};
32207+
32208+static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
32209+ loff_t offset __maybe_unused, u64 h_ino,
32210+ unsigned int d_type)
32211+{
32212+ struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
32213+ char *name = (void *)__name;
32214+ struct super_block *sb;
32215+ ino_t ino;
32216+ const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
32217+
32218+ arg->err = 0;
32219+ sb = arg->file->f_path.dentry->d_sb;
32220+ au_fset_fillvdir(arg->flags, CALLED);
32221+ /* smp_mb(); */
32222+ if (nlen <= AUFS_WH_PFX_LEN
32223+ || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
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 */
32227+
32228+ arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
32229+ if (!arg->err) {
32230+ if (unlikely(nlen > AUFS_MAX_NAMELEN))
32231+ d_type = DT_UNKNOWN;
32232+ arg->err = append_de(arg->vdir, name, nlen, ino,
32233+ d_type, &arg->delist);
32234+ }
32235+ } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
32236+ name += AUFS_WH_PFX_LEN;
32237+ nlen -= AUFS_WH_PFX_LEN;
32238+ if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
32239+ goto out; /* already whiteouted */
32240+
32241+ ino = 0; /* just to suppress a warning */
32242+ if (shwh)
32243+ arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
32244+ &ino);
32245+ if (!arg->err) {
32246+ if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
32247+ d_type = DT_UNKNOWN;
32248+ arg->err = au_nhash_append_wh
32249+ (&arg->whlist, name, nlen, ino, d_type,
32250+ arg->bindex, shwh);
32251+ }
32252+ }
32253+
32254+out:
32255+ if (!arg->err)
32256+ arg->vdir->vd_jiffy = jiffies;
32257+ /* smp_mb(); */
32258+ AuTraceErr(arg->err);
32259+ return arg->err;
32260+}
32261+
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;
32269+ struct au_vdir_wh *pos;
32270+ struct hlist_node *n;
32271+ char *p, *o;
32272+ struct au_vdir_destr *destr;
32273+
32274+ AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
32275+
32276+ err = -ENOMEM;
32277+ o = p = (void *)__get_free_page(GFP_NOFS);
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;
32287+ hlist_for_each_entry_safe(pos, n, head, wh_hash) {
32288+ destr = &pos->wh_str;
32289+ memcpy(p, destr->name, destr->len);
32290+ err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
32291+ pos->wh_ino, pos->wh_type, delist);
32292+ if (unlikely(err))
32293+ break;
32294+ }
32295+ }
32296+
32297+ free_page((unsigned long)o);
32298+
32299+out:
32300+ AuTraceErr(err);
32301+ return err;
32302+#else
32303+ return 0;
32304+#endif
32305+}
32306+
32307+static int au_do_read_vdir(struct fillvdir_arg *arg)
32308+{
32309+ int err;
32310+ unsigned int rdhash;
32311+ loff_t offset;
32312+ aufs_bindex_t bbot, bindex, btop;
32313+ unsigned char shwh;
32314+ struct file *hf, *file;
32315+ struct super_block *sb;
32316+
32317+ file = arg->file;
32318+ sb = file->f_path.dentry->d_sb;
32319+ SiMustAnyLock(sb);
32320+
32321+ rdhash = au_sbi(sb)->si_rdhash;
32322+ if (!rdhash)
32323+ rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
32324+ err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
32325+ if (unlikely(err))
32326+ goto out;
32327+ err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
32328+ if (unlikely(err))
32329+ goto out_delist;
32330+
32331+ err = 0;
32332+ arg->flags = 0;
32333+ shwh = 0;
32334+ if (au_opt_test(au_mntflags(sb), SHWH)) {
32335+ shwh = 1;
32336+ au_fset_fillvdir(arg->flags, SHWH);
32337+ }
32338+ btop = au_fbtop(file);
32339+ bbot = au_fbbot_dir(file);
32340+ for (bindex = btop; !err && bindex <= bbot; bindex++) {
32341+ hf = au_hf_dir(file, bindex);
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);
32352+ if (shwh
32353+ || (bindex != bbot
32354+ && au_br_whable(au_sbr_perm(sb, bindex))))
32355+ au_fset_fillvdir(arg->flags, WHABLE);
32356+ do {
32357+ arg->err = 0;
32358+ au_fclr_fillvdir(arg->flags, CALLED);
32359+ /* smp_mb(); */
32360+ err = vfsub_iterate_dir(hf, &arg->ctx);
32361+ if (err >= 0)
32362+ err = arg->err;
32363+ } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
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+ */
32369+ }
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);
32375+
32376+out_delist:
32377+ au_nhash_de_free(&arg->delist);
32378+out:
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;
32387+ struct fillvdir_arg arg = {
32388+ .ctx = {
32389+ .actor = fillvdir
32390+ }
32391+ };
32392+ struct inode *inode;
32393+ struct au_vdir *vdir, *allocated;
32394+
32395+ err = 0;
32396+ inode = file_inode(file);
32397+ IMustLock(inode);
32398+ IiMustWriteLock(inode);
32399+ SiMustAnyLock(inode->i_sb);
32400+
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;
32407+ vdir = alloc_vdir(file);
32408+ err = PTR_ERR(vdir);
32409+ if (IS_ERR(vdir))
32410+ goto out;
32411+ err = 0;
32412+ allocated = vdir;
32413+ } else if (may_read
32414+ && (!inode_eq_iversion(inode, vdir->vd_version)
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) {
32429+ /* file->f_pos = 0; */ /* todo: ctx->pos? */
32430+ vdir->vd_version = inode_query_iversion(inode);
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)
32436+ au_vdir_free(allocated);
32437+
32438+out:
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+
32454+ p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
32455+ GFP_NOFS, /*may_shrink*/0);
32456+ if (unlikely(!p))
32457+ goto out;
32458+ tgt->vd_deblk = p;
32459+ }
32460+
32461+ if (tgt->vd_deblk_sz != deblk_sz) {
32462+ unsigned char *p;
32463+
32464+ tgt->vd_deblk_sz = deblk_sz;
32465+ p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
32466+ /*may_shrink*/1);
32467+ if (unlikely(!p))
32468+ goto out;
32469+ tgt->vd_deblk[0] = p;
32470+ }
32471+ memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
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++) {
32477+ tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
32478+ GFP_NOFS);
32479+ if (unlikely(!tgt->vd_deblk[ul]))
32480+ goto out;
32481+ tgt->vd_nblk++;
32482+ }
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];
32488+ /* smp_mb(); */
32489+ return 0; /* success */
32490+
32491+out:
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+
32503+ /* test file->f_pos here instead of ctx->pos */
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) {
32511+ vdir_cache = alloc_vdir(file);
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) {
32517+ /* test file->f_pos here instead of ctx->pos */
32518+ err = reinit_vdir(vdir_cache);
32519+ if (unlikely(err))
32520+ goto out;
32521+ } else
32522+ return 0; /* success */
32523+
32524+ inode = file_inode(file);
32525+ err = copy_vdir(vdir_cache, au_ivdir(inode));
32526+ if (!err) {
32527+ file->f_version = inode_query_iversion(inode);
32528+ if (allocated)
32529+ au_set_fvdir_cache(file, allocated);
32530+ } else if (allocated)
32531+ au_vdir_free(allocated);
32532+
32533+out:
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 */
32549+static int seek_vdir(struct file *file, struct dir_context *ctx)
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);
32562+ if (ctx->pos == offset)
32563+ goto out;
32564+
32565+ vdir_cache->vd_last.ul = 0;
32566+ vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
32567+ if (!ctx->pos)
32568+ goto out;
32569+
32570+ valid = 0;
32571+ deblk_sz = vdir_cache->vd_deblk_sz;
32572+ ul = div64_u64(ctx->pos, deblk_sz);
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;
32583+ while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
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+
32598+out:
32599+ /* smp_mb(); */
32600+ if (!valid)
32601+ AuDbg("valid %d\n", !valid);
32602+ return valid;
32603+}
32604+
32605+int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
32606+{
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+
32612+ if (!seek_vdir(file, ctx))
32613+ return 0;
32614+
32615+ vdir_cache = au_fvdir_cache(file);
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",
32623+ de->de_str.len, de->de_str.name, ctx->pos,
32624+ (unsigned long)de->de_ino, de->de_type);
32625+ if (unlikely(!dir_emit(ctx, de->de_str.name,
32626+ de->de_str.len, de->de_ino,
32627+ de->de_type))) {
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;
32635+ ctx->pos += l;
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];
32641+ ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
32642+ continue;
32643+ }
32644+ break;
32645+ }
32646+
32647+ /* smp_mb(); */
32648+ return 0;
32649+}
32650diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
32651--- /usr/share/empty/fs/aufs/vfsub.c 1970-01-01 01:00:00.000000000 +0100
32652+++ linux/fs/aufs/vfsub.c 2021-12-03 15:40:58.236647297 +0100
32653@@ -0,0 +1,919 @@
32654+// SPDX-License-Identifier: GPL-2.0
32655+/*
32656+ * Copyright (C) 2005-2021 Junjiro R. Okajima
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.
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
32669+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
32670+ */
32671+
32672+/*
32673+ * sub-routines for VFS
32674+ */
32675+
32676+#include <linux/mnt_namespace.h>
32677+#include <linux/namei.h>
32678+#include <linux/nsproxy.h>
32679+#include <linux/security.h>
32680+#include <linux/splice.h>
32681+#include "aufs.h"
32682+
32683+#ifdef CONFIG_AUFS_BR_FUSE
32684+int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
32685+{
32686+ if (!au_test_fuse(h_sb) || !au_userns)
32687+ return 0;
32688+
32689+ return is_current_mnt_ns(mnt) ? 0 : -EACCES;
32690+}
32691+#endif
32692+
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+
32706+/* ---------------------------------------------------------------------- */
32707+
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+ /*
32715+ * Always needs h_path->mnt for LSM or FUSE branch.
32716+ */
32717+ AuDebugOn(!h_path->mnt);
32718+
32719+ /* for remote fs, leave work for its getattr or d_revalidate */
32720+ /* for bad i_attr fs, handle them in aufs_getattr() */
32721+ /* still some fs may acquire i_mutex. we need to skip them */
32722+ err = 0;
32723+ if (!did)
32724+ did = &err;
32725+ h_sb = h_path->dentry->d_sb;
32726+ *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
32727+ if (*did)
32728+ err = vfsub_getattr(h_path, &st);
32729+
32730+ return err;
32731+}
32732+
32733+/* ---------------------------------------------------------------------- */
32734+
32735+struct file *vfsub_dentry_open(struct path *path, int flags)
32736+{
32737+ return dentry_open(path, flags /* | __FMODE_NONOTIFY */,
32738+ current_cred());
32739+}
32740+
32741+struct file *vfsub_filp_open(const char *path, int oflags, int mode)
32742+{
32743+ struct file *file;
32744+
32745+ lockdep_off();
32746+ file = filp_open(path,
32747+ oflags /* | __FMODE_NONOTIFY */,
32748+ mode);
32749+ lockdep_on();
32750+ if (IS_ERR(file))
32751+ goto out;
32752+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
32753+
32754+out:
32755+ return file;
32756+}
32757+
32758+/*
32759+ * Ideally this function should call VFS:do_last() in order to keep all its
32760+ * checkings. But it is very hard for aufs to regenerate several VFS internal
32761+ * structure such as nameidata. This is a second (or third) best approach.
32762+ * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
32763+ */
32764+int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
32765+ struct vfsub_aopen_args *args)
32766+{
32767+ int err;
32768+ struct au_branch *br = args->br;
32769+ struct file *file = args->file;
32770+ /* copied from linux/fs/namei.c:atomic_open() */
32771+ struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
32772+
32773+ IMustLock(dir);
32774+ AuDebugOn(!dir->i_op->atomic_open);
32775+
32776+ err = au_br_test_oflag(args->open_flag, br);
32777+ if (unlikely(err))
32778+ goto out;
32779+
32780+ au_lcnt_inc(&br->br_nfiles);
32781+ file->f_path.dentry = DENTRY_NOT_SET;
32782+ file->f_path.mnt = au_br_mnt(br);
32783+ AuDbg("%ps\n", dir->i_op->atomic_open);
32784+ err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
32785+ args->create_mode);
32786+ if (unlikely(err < 0)) {
32787+ au_lcnt_dec(&br->br_nfiles);
32788+ goto out;
32789+ }
32790+
32791+ /* temporary workaround for nfsv4 branch */
32792+ if (au_test_nfs(dir->i_sb))
32793+ nfs_mark_for_revalidate(dir);
32794+
32795+ if (file->f_mode & FMODE_CREATED)
32796+ fsnotify_create(dir, dentry);
32797+ if (!(file->f_mode & FMODE_OPENED)) {
32798+ au_lcnt_dec(&br->br_nfiles);
32799+ goto out;
32800+ }
32801+
32802+ /* todo: call VFS:may_open() here */
32803+ /* todo: ima_file_check() too? */
32804+ if (!err && (args->open_flag & __FMODE_EXEC))
32805+ err = deny_write_access(file);
32806+ if (!err)
32807+ fsnotify_open(file);
32808+ else
32809+ au_lcnt_dec(&br->br_nfiles);
32810+ /* note that the file is created and still opened */
32811+
32812+out:
32813+ return err;
32814+}
32815+
32816+int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
32817+{
32818+ int err;
32819+
32820+ err = kern_path(name, flags, path);
32821+ if (!err && d_is_positive(path->dentry))
32822+ vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
32823+ return err;
32824+}
32825+
32826+struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
32827+ struct path *ppath, int len)
32828+{
32829+ struct path path;
32830+
32831+ path.dentry = lookup_one_len_unlocked(name, ppath->dentry, len);
32832+ if (IS_ERR(path.dentry))
32833+ goto out;
32834+ if (d_is_positive(path.dentry)) {
32835+ path.mnt = ppath->mnt;
32836+ vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32837+ }
32838+
32839+out:
32840+ AuTraceErrPtr(path.dentry);
32841+ return path.dentry;
32842+}
32843+
32844+struct dentry *vfsub_lookup_one_len(const char *name, struct path *ppath,
32845+ int len)
32846+{
32847+ struct path path;
32848+
32849+ /* VFS checks it too, but by WARN_ON_ONCE() */
32850+ IMustLock(d_inode(ppath->dentry));
32851+
32852+ path.dentry = lookup_one_len(name, ppath->dentry, len);
32853+ if (IS_ERR(path.dentry))
32854+ goto out;
32855+ if (d_is_positive(path.dentry)) {
32856+ path.mnt = ppath->mnt;
32857+ vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32858+ }
32859+
32860+out:
32861+ AuTraceErrPtr(path.dentry);
32862+ return path.dentry;
32863+}
32864+
32865+void vfsub_call_lkup_one(void *args)
32866+{
32867+ struct vfsub_lkup_one_args *a = args;
32868+ *a->errp = vfsub_lkup_one(a->name, a->ppath);
32869+}
32870+
32871+/* ---------------------------------------------------------------------- */
32872+
32873+struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
32874+ struct dentry *d2, struct au_hinode *hdir2)
32875+{
32876+ struct dentry *d;
32877+
32878+ lockdep_off();
32879+ d = lock_rename(d1, d2);
32880+ lockdep_on();
32881+ au_hn_suspend(hdir1);
32882+ if (hdir1 != hdir2)
32883+ au_hn_suspend(hdir2);
32884+
32885+ return d;
32886+}
32887+
32888+void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
32889+ struct dentry *d2, struct au_hinode *hdir2)
32890+{
32891+ au_hn_resume(hdir1);
32892+ if (hdir1 != hdir2)
32893+ au_hn_resume(hdir2);
32894+ lockdep_off();
32895+ unlock_rename(d1, d2);
32896+ lockdep_on();
32897+}
32898+
32899+/* ---------------------------------------------------------------------- */
32900+
32901+int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
32902+{
32903+ int err;
32904+ struct dentry *d;
32905+ struct user_namespace *userns;
32906+
32907+ IMustLock(dir);
32908+
32909+ d = path->dentry;
32910+ path->dentry = d->d_parent;
32911+ err = security_path_mknod(path, d, mode, 0);
32912+ path->dentry = d;
32913+ if (unlikely(err))
32914+ goto out;
32915+ userns = mnt_user_ns(path->mnt);
32916+
32917+ lockdep_off();
32918+ err = vfs_create(userns, dir, path->dentry, mode, want_excl);
32919+ lockdep_on();
32920+ if (!err) {
32921+ struct path tmp = *path;
32922+ int did;
32923+
32924+ vfsub_update_h_iattr(&tmp, &did);
32925+ if (did) {
32926+ tmp.dentry = path->dentry->d_parent;
32927+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32928+ }
32929+ /*ignore*/
32930+ }
32931+
32932+out:
32933+ return err;
32934+}
32935+
32936+int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
32937+{
32938+ int err;
32939+ struct dentry *d;
32940+ struct user_namespace *userns;
32941+
32942+ IMustLock(dir);
32943+
32944+ d = path->dentry;
32945+ path->dentry = d->d_parent;
32946+ err = security_path_symlink(path, d, symname);
32947+ path->dentry = d;
32948+ if (unlikely(err))
32949+ goto out;
32950+ userns = mnt_user_ns(path->mnt);
32951+
32952+ lockdep_off();
32953+ err = vfs_symlink(userns, dir, path->dentry, symname);
32954+ lockdep_on();
32955+ if (!err) {
32956+ struct path tmp = *path;
32957+ int did;
32958+
32959+ vfsub_update_h_iattr(&tmp, &did);
32960+ if (did) {
32961+ tmp.dentry = path->dentry->d_parent;
32962+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32963+ }
32964+ /*ignore*/
32965+ }
32966+
32967+out:
32968+ return err;
32969+}
32970+
32971+int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
32972+{
32973+ int err;
32974+ struct dentry *d;
32975+ struct user_namespace *userns;
32976+
32977+ IMustLock(dir);
32978+
32979+ d = path->dentry;
32980+ path->dentry = d->d_parent;
32981+ err = security_path_mknod(path, d, mode, new_encode_dev(dev));
32982+ path->dentry = d;
32983+ if (unlikely(err))
32984+ goto out;
32985+ userns = mnt_user_ns(path->mnt);
32986+
32987+ lockdep_off();
32988+ err = vfs_mknod(userns, dir, path->dentry, mode, dev);
32989+ lockdep_on();
32990+ if (!err) {
32991+ struct path tmp = *path;
32992+ int did;
32993+
32994+ vfsub_update_h_iattr(&tmp, &did);
32995+ if (did) {
32996+ tmp.dentry = path->dentry->d_parent;
32997+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32998+ }
32999+ /*ignore*/
33000+ }
33001+
33002+out:
33003+ return err;
33004+}
33005+
33006+static int au_test_nlink(struct inode *inode)
33007+{
33008+ const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
33009+
33010+ if (!au_test_fs_no_limit_nlink(inode->i_sb)
33011+ || inode->i_nlink < link_max)
33012+ return 0;
33013+ return -EMLINK;
33014+}
33015+
33016+int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
33017+ struct inode **delegated_inode)
33018+{
33019+ int err;
33020+ struct dentry *d;
33021+ struct user_namespace *userns;
33022+
33023+ IMustLock(dir);
33024+
33025+ err = au_test_nlink(d_inode(src_dentry));
33026+ if (unlikely(err))
33027+ return err;
33028+
33029+ /* we don't call may_linkat() */
33030+ d = path->dentry;
33031+ path->dentry = d->d_parent;
33032+ err = security_path_link(src_dentry, path, d);
33033+ path->dentry = d;
33034+ if (unlikely(err))
33035+ goto out;
33036+ userns = mnt_user_ns(path->mnt);
33037+
33038+ lockdep_off();
33039+ err = vfs_link(src_dentry, userns, dir, path->dentry, delegated_inode);
33040+ lockdep_on();
33041+ if (!err) {
33042+ struct path tmp = *path;
33043+ int did;
33044+
33045+ /* fuse has different memory inode for the same inumber */
33046+ vfsub_update_h_iattr(&tmp, &did);
33047+ if (did) {
33048+ tmp.dentry = path->dentry->d_parent;
33049+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33050+ tmp.dentry = src_dentry;
33051+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33052+ }
33053+ /*ignore*/
33054+ }
33055+
33056+out:
33057+ return err;
33058+}
33059+
33060+int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
33061+ struct inode *dir, struct path *path,
33062+ struct inode **delegated_inode, unsigned int flags)
33063+{
33064+ int err;
33065+ struct renamedata rd;
33066+ struct path tmp = {
33067+ .mnt = path->mnt
33068+ };
33069+ struct dentry *d;
33070+
33071+ IMustLock(dir);
33072+ IMustLock(src_dir);
33073+
33074+ d = path->dentry;
33075+ path->dentry = d->d_parent;
33076+ tmp.dentry = src_dentry->d_parent;
33077+ err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
33078+ path->dentry = d;
33079+ if (unlikely(err))
33080+ goto out;
33081+
33082+ rd.old_mnt_userns = mnt_user_ns(path->mnt);
33083+ rd.old_dir = src_dir;
33084+ rd.old_dentry = src_dentry;
33085+ rd.new_mnt_userns = rd.old_mnt_userns;
33086+ rd.new_dir = dir;
33087+ rd.new_dentry = path->dentry;
33088+ rd.delegated_inode = delegated_inode;
33089+ rd.flags = flags;
33090+ lockdep_off();
33091+ err = vfs_rename(&rd);
33092+ lockdep_on();
33093+ if (!err) {
33094+ int did;
33095+
33096+ tmp.dentry = d->d_parent;
33097+ vfsub_update_h_iattr(&tmp, &did);
33098+ if (did) {
33099+ tmp.dentry = src_dentry;
33100+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33101+ tmp.dentry = src_dentry->d_parent;
33102+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33103+ }
33104+ /*ignore*/
33105+ }
33106+
33107+out:
33108+ return err;
33109+}
33110+
33111+int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
33112+{
33113+ int err;
33114+ struct dentry *d;
33115+ struct user_namespace *userns;
33116+
33117+ IMustLock(dir);
33118+
33119+ d = path->dentry;
33120+ path->dentry = d->d_parent;
33121+ err = security_path_mkdir(path, d, mode);
33122+ path->dentry = d;
33123+ if (unlikely(err))
33124+ goto out;
33125+ userns = mnt_user_ns(path->mnt);
33126+
33127+ lockdep_off();
33128+ err = vfs_mkdir(userns, dir, path->dentry, mode);
33129+ lockdep_on();
33130+ if (!err) {
33131+ struct path tmp = *path;
33132+ int did;
33133+
33134+ vfsub_update_h_iattr(&tmp, &did);
33135+ if (did) {
33136+ tmp.dentry = path->dentry->d_parent;
33137+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33138+ }
33139+ /*ignore*/
33140+ }
33141+
33142+out:
33143+ return err;
33144+}
33145+
33146+int vfsub_rmdir(struct inode *dir, struct path *path)
33147+{
33148+ int err;
33149+ struct dentry *d;
33150+ struct user_namespace *userns;
33151+
33152+ IMustLock(dir);
33153+
33154+ d = path->dentry;
33155+ path->dentry = d->d_parent;
33156+ err = security_path_rmdir(path, d);
33157+ path->dentry = d;
33158+ if (unlikely(err))
33159+ goto out;
33160+ userns = mnt_user_ns(path->mnt);
33161+
33162+ lockdep_off();
33163+ err = vfs_rmdir(userns, dir, path->dentry);
33164+ lockdep_on();
33165+ if (!err) {
33166+ struct path tmp = {
33167+ .dentry = path->dentry->d_parent,
33168+ .mnt = path->mnt
33169+ };
33170+
33171+ vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33172+ }
33173+
33174+out:
33175+ return err;
33176+}
33177+
33178+/* ---------------------------------------------------------------------- */
33179+
33180+/* todo: support mmap_sem? */
33181+ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33182+ loff_t *ppos)
33183+{
33184+ ssize_t err;
33185+
33186+ lockdep_off();
33187+ err = vfs_read(file, ubuf, count, ppos);
33188+ lockdep_on();
33189+ if (err >= 0)
33190+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33191+ return err;
33192+}
33193+
33194+ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33195+ loff_t *ppos)
33196+{
33197+ ssize_t err;
33198+
33199+ lockdep_off();
33200+ err = kernel_read(file, kbuf, count, ppos);
33201+ lockdep_on();
33202+ AuTraceErr(err);
33203+ if (err >= 0)
33204+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33205+ return err;
33206+}
33207+
33208+ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33209+ loff_t *ppos)
33210+{
33211+ ssize_t err;
33212+
33213+ lockdep_off();
33214+ err = vfs_write(file, ubuf, count, ppos);
33215+ lockdep_on();
33216+ if (err >= 0)
33217+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33218+ return err;
33219+}
33220+
33221+ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
33222+{
33223+ ssize_t err;
33224+
33225+ lockdep_off();
33226+ err = kernel_write(file, kbuf, count, ppos);
33227+ lockdep_on();
33228+ if (err >= 0)
33229+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33230+ return err;
33231+}
33232+
33233+int vfsub_flush(struct file *file, fl_owner_t id)
33234+{
33235+ int err;
33236+
33237+ err = 0;
33238+ if (file->f_op->flush) {
33239+ if (!au_test_nfs(file->f_path.dentry->d_sb))
33240+ err = file->f_op->flush(file, id);
33241+ else {
33242+ lockdep_off();
33243+ err = file->f_op->flush(file, id);
33244+ lockdep_on();
33245+ }
33246+ if (!err)
33247+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
33248+ /*ignore*/
33249+ }
33250+ return err;
33251+}
33252+
33253+int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
33254+{
33255+ int err;
33256+
33257+ AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
33258+
33259+ lockdep_off();
33260+ err = iterate_dir(file, ctx);
33261+ lockdep_on();
33262+ if (err >= 0)
33263+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33264+
33265+ return err;
33266+}
33267+
33268+long vfsub_splice_to(struct file *in, loff_t *ppos,
33269+ struct pipe_inode_info *pipe, size_t len,
33270+ unsigned int flags)
33271+{
33272+ long err;
33273+
33274+ lockdep_off();
33275+ err = do_splice_to(in, ppos, pipe, len, flags);
33276+ lockdep_on();
33277+ file_accessed(in);
33278+ if (err >= 0)
33279+ vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
33280+ return err;
33281+}
33282+
33283+long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33284+ loff_t *ppos, size_t len, unsigned int flags)
33285+{
33286+ long err;
33287+
33288+ lockdep_off();
33289+ err = do_splice_from(pipe, out, ppos, len, flags);
33290+ lockdep_on();
33291+ if (err >= 0)
33292+ vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
33293+ return err;
33294+}
33295+
33296+int vfsub_fsync(struct file *file, struct path *path, int datasync)
33297+{
33298+ int err;
33299+
33300+ /* file can be NULL */
33301+ lockdep_off();
33302+ err = vfs_fsync(file, datasync);
33303+ lockdep_on();
33304+ if (!err) {
33305+ if (!path) {
33306+ AuDebugOn(!file);
33307+ path = &file->f_path;
33308+ }
33309+ vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33310+ }
33311+ return err;
33312+}
33313+
33314+/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
33315+int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33316+ struct file *h_file)
33317+{
33318+ int err;
33319+ struct inode *h_inode;
33320+ struct super_block *h_sb;
33321+ struct user_namespace *h_userns;
33322+
33323+ if (!h_file) {
33324+ err = vfsub_truncate(h_path, length);
33325+ goto out;
33326+ }
33327+
33328+ h_inode = d_inode(h_path->dentry);
33329+ h_sb = h_inode->i_sb;
33330+ lockdep_off();
33331+ sb_start_write(h_sb);
33332+ lockdep_on();
33333+ err = security_path_truncate(h_path);
33334+ if (!err) {
33335+ h_userns = mnt_user_ns(h_path->mnt);
33336+ lockdep_off();
33337+ err = do_truncate(h_userns, h_path->dentry, length, attr,
33338+ h_file);
33339+ lockdep_on();
33340+ }
33341+ lockdep_off();
33342+ sb_end_write(h_sb);
33343+ lockdep_on();
33344+
33345+out:
33346+ return err;
33347+}
33348+
33349+/* ---------------------------------------------------------------------- */
33350+
33351+struct au_vfsub_mkdir_args {
33352+ int *errp;
33353+ struct inode *dir;
33354+ struct path *path;
33355+ int mode;
33356+};
33357+
33358+static void au_call_vfsub_mkdir(void *args)
33359+{
33360+ struct au_vfsub_mkdir_args *a = args;
33361+ *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
33362+}
33363+
33364+int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
33365+{
33366+ int err, do_sio, wkq_err;
33367+ struct user_namespace *userns;
33368+
33369+ userns = mnt_user_ns(path->mnt);
33370+ do_sio = au_test_h_perm_sio(userns, dir, MAY_EXEC | MAY_WRITE);
33371+ if (!do_sio) {
33372+ lockdep_off();
33373+ err = vfsub_mkdir(dir, path, mode);
33374+ lockdep_on();
33375+ } else {
33376+ struct au_vfsub_mkdir_args args = {
33377+ .errp = &err,
33378+ .dir = dir,
33379+ .path = path,
33380+ .mode = mode
33381+ };
33382+ wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
33383+ if (unlikely(wkq_err))
33384+ err = wkq_err;
33385+ }
33386+
33387+ return err;
33388+}
33389+
33390+struct au_vfsub_rmdir_args {
33391+ int *errp;
33392+ struct inode *dir;
33393+ struct path *path;
33394+};
33395+
33396+static void au_call_vfsub_rmdir(void *args)
33397+{
33398+ struct au_vfsub_rmdir_args *a = args;
33399+ *a->errp = vfsub_rmdir(a->dir, a->path);
33400+}
33401+
33402+int vfsub_sio_rmdir(struct inode *dir, struct path *path)
33403+{
33404+ int err, do_sio, wkq_err;
33405+ struct user_namespace *userns;
33406+
33407+ userns = mnt_user_ns(path->mnt);
33408+ do_sio = au_test_h_perm_sio(userns, dir, MAY_EXEC | MAY_WRITE);
33409+ if (!do_sio) {
33410+ lockdep_off();
33411+ err = vfsub_rmdir(dir, path);
33412+ lockdep_on();
33413+ } else {
33414+ struct au_vfsub_rmdir_args args = {
33415+ .errp = &err,
33416+ .dir = dir,
33417+ .path = path
33418+ };
33419+ wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
33420+ if (unlikely(wkq_err))
33421+ err = wkq_err;
33422+ }
33423+
33424+ return err;
33425+}
33426+
33427+/* ---------------------------------------------------------------------- */
33428+
33429+struct notify_change_args {
33430+ int *errp;
33431+ struct path *path;
33432+ struct iattr *ia;
33433+ struct inode **delegated_inode;
33434+};
33435+
33436+static void call_notify_change(void *args)
33437+{
33438+ struct notify_change_args *a = args;
33439+ struct inode *h_inode;
33440+ struct user_namespace *userns;
33441+
33442+ h_inode = d_inode(a->path->dentry);
33443+ IMustLock(h_inode);
33444+
33445+ *a->errp = -EPERM;
33446+ if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
33447+ userns = mnt_user_ns(a->path->mnt);
33448+ lockdep_off();
33449+ *a->errp = notify_change(userns, a->path->dentry, a->ia,
33450+ a->delegated_inode);
33451+ lockdep_on();
33452+ if (!*a->errp)
33453+ vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
33454+ }
33455+ AuTraceErr(*a->errp);
33456+}
33457+
33458+int vfsub_notify_change(struct path *path, struct iattr *ia,
33459+ struct inode **delegated_inode)
33460+{
33461+ int err;
33462+ struct notify_change_args args = {
33463+ .errp = &err,
33464+ .path = path,
33465+ .ia = ia,
33466+ .delegated_inode = delegated_inode
33467+ };
33468+
33469+ call_notify_change(&args);
33470+
33471+ return err;
33472+}
33473+
33474+int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33475+ struct inode **delegated_inode)
33476+{
33477+ int err, wkq_err;
33478+ struct notify_change_args args = {
33479+ .errp = &err,
33480+ .path = path,
33481+ .ia = ia,
33482+ .delegated_inode = delegated_inode
33483+ };
33484+
33485+ wkq_err = au_wkq_wait(call_notify_change, &args);
33486+ if (unlikely(wkq_err))
33487+ err = wkq_err;
33488+
33489+ return err;
33490+}
33491+
33492+/* ---------------------------------------------------------------------- */
33493+
33494+struct unlink_args {
33495+ int *errp;
33496+ struct inode *dir;
33497+ struct path *path;
33498+ struct inode **delegated_inode;
33499+};
33500+
33501+static void call_unlink(void *args)
33502+{
33503+ struct unlink_args *a = args;
33504+ struct dentry *d = a->path->dentry;
33505+ struct inode *h_inode;
33506+ struct user_namespace *userns;
33507+ const int stop_sillyrename = (au_test_nfs(d->d_sb)
33508+ && au_dcount(d) == 1);
33509+
33510+ IMustLock(a->dir);
33511+
33512+ a->path->dentry = d->d_parent;
33513+ *a->errp = security_path_unlink(a->path, d);
33514+ a->path->dentry = d;
33515+ if (unlikely(*a->errp))
33516+ return;
33517+
33518+ if (!stop_sillyrename)
33519+ dget(d);
33520+ h_inode = NULL;
33521+ if (d_is_positive(d)) {
33522+ h_inode = d_inode(d);
33523+ ihold(h_inode);
33524+ }
33525+
33526+ userns = mnt_user_ns(a->path->mnt);
33527+ lockdep_off();
33528+ *a->errp = vfs_unlink(userns, a->dir, d, a->delegated_inode);
33529+ lockdep_on();
33530+ if (!*a->errp) {
33531+ struct path tmp = {
33532+ .dentry = d->d_parent,
33533+ .mnt = a->path->mnt
33534+ };
33535+ vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33536+ }
33537+
33538+ if (!stop_sillyrename)
33539+ dput(d);
33540+ if (h_inode)
33541+ iput(h_inode);
33542+
33543+ AuTraceErr(*a->errp);
33544+}
33545+
33546+/*
33547+ * @dir: must be locked.
33548+ * @dentry: target dentry.
33549+ */
33550+int vfsub_unlink(struct inode *dir, struct path *path,
33551+ struct inode **delegated_inode, int force)
33552+{
33553+ int err;
33554+ struct unlink_args args = {
33555+ .errp = &err,
33556+ .dir = dir,
33557+ .path = path,
33558+ .delegated_inode = delegated_inode
33559+ };
33560+
33561+ if (!force)
33562+ call_unlink(&args);
33563+ else {
33564+ int wkq_err;
33565+
33566+ wkq_err = au_wkq_wait(call_unlink, &args);
33567+ if (unlikely(wkq_err))
33568+ err = wkq_err;
33569+ }
33570+
33571+ return err;
33572+}
33573diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
33574--- /usr/share/empty/fs/aufs/vfsub.h 1970-01-01 01:00:00.000000000 +0100
33575+++ linux/fs/aufs/vfsub.h 2021-12-03 15:40:58.236647297 +0100
33576@@ -0,0 +1,358 @@
33577+/* SPDX-License-Identifier: GPL-2.0 */
33578+/*
33579+ * Copyright (C) 2005-2021 Junjiro R. Okajima
33580+ *
33581+ * This program, aufs is free software; you can redistribute it and/or modify
33582+ * it under the terms of the GNU General Public License as published by
33583+ * the Free Software Foundation; either version 2 of the License, or
33584+ * (at your option) any later version.
33585+ *
33586+ * This program is distributed in the hope that it will be useful,
33587+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
33588+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33589+ * GNU General Public License for more details.
33590+ *
33591+ * You should have received a copy of the GNU General Public License
33592+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
33593+ */
33594+
33595+/*
33596+ * sub-routines for VFS
33597+ */
33598+
33599+#ifndef __AUFS_VFSUB_H__
33600+#define __AUFS_VFSUB_H__
33601+
33602+#ifdef __KERNEL__
33603+
33604+#include <linux/fs.h>
33605+#include <linux/mount.h>
33606+#include <linux/posix_acl.h>
33607+#include <linux/xattr.h>
33608+#include "debug.h"
33609+
33610+/* copied from linux/fs/internal.h */
33611+/* todo: BAD approach!! */
33612+extern void __mnt_drop_write(struct vfsmount *);
33613+extern struct file *alloc_empty_file(int, const struct cred *);
33614+
33615+/* ---------------------------------------------------------------------- */
33616+
33617+/* lock subclass for lower inode */
33618+/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
33619+/* reduce? gave up. */
33620+enum {
33621+ AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
33622+ AuLsc_I_PARENT, /* lower inode, parent first */
33623+ AuLsc_I_PARENT2, /* copyup dirs */
33624+ AuLsc_I_PARENT3, /* copyup wh */
33625+ AuLsc_I_CHILD,
33626+ AuLsc_I_CHILD2,
33627+ AuLsc_I_End
33628+};
33629+
33630+/* to debug easier, do not make them inlined functions */
33631+#define MtxMustLock(mtx) AuDebugOn(!mutex_is_locked(mtx))
33632+#define IMustLock(i) AuDebugOn(!inode_is_locked(i))
33633+
33634+/* ---------------------------------------------------------------------- */
33635+
33636+static inline void vfsub_drop_nlink(struct inode *inode)
33637+{
33638+ AuDebugOn(!inode->i_nlink);
33639+ drop_nlink(inode);
33640+}
33641+
33642+static inline void vfsub_dead_dir(struct inode *inode)
33643+{
33644+ AuDebugOn(!S_ISDIR(inode->i_mode));
33645+ inode->i_flags |= S_DEAD;
33646+ clear_nlink(inode);
33647+}
33648+
33649+static inline int vfsub_native_ro(struct inode *inode)
33650+{
33651+ return sb_rdonly(inode->i_sb)
33652+ || IS_RDONLY(inode)
33653+ /* || IS_APPEND(inode) */
33654+ || IS_IMMUTABLE(inode);
33655+}
33656+
33657+#ifdef CONFIG_AUFS_BR_FUSE
33658+int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
33659+#else
33660+AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
33661+#endif
33662+
33663+int vfsub_sync_filesystem(struct super_block *h_sb, int wait);
33664+
33665+/* ---------------------------------------------------------------------- */
33666+
33667+int vfsub_update_h_iattr(struct path *h_path, int *did);
33668+struct file *vfsub_dentry_open(struct path *path, int flags);
33669+struct file *vfsub_filp_open(const char *path, int oflags, int mode);
33670+struct au_branch;
33671+struct vfsub_aopen_args {
33672+ struct file *file;
33673+ unsigned int open_flag;
33674+ umode_t create_mode;
33675+ struct au_branch *br;
33676+};
33677+int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
33678+ struct vfsub_aopen_args *args);
33679+int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
33680+
33681+struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33682+ struct path *ppath, int len);
33683+struct dentry *vfsub_lookup_one_len(const char *name, struct path *ppath,
33684+ int len);
33685+
33686+struct vfsub_lkup_one_args {
33687+ struct dentry **errp;
33688+ struct qstr *name;
33689+ struct path *ppath;
33690+};
33691+
33692+static inline struct dentry *vfsub_lkup_one(struct qstr *name,
33693+ struct path *ppath)
33694+{
33695+ return vfsub_lookup_one_len(name->name, ppath, name->len);
33696+}
33697+
33698+void vfsub_call_lkup_one(void *args);
33699+
33700+/* ---------------------------------------------------------------------- */
33701+
33702+static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
33703+{
33704+ int err;
33705+
33706+ lockdep_off();
33707+ err = mnt_want_write(mnt);
33708+ lockdep_on();
33709+ return err;
33710+}
33711+
33712+static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
33713+{
33714+ lockdep_off();
33715+ mnt_drop_write(mnt);
33716+ lockdep_on();
33717+}
33718+
33719+#if 0 /* reserved */
33720+static inline void vfsub_mnt_drop_write_file(struct file *file)
33721+{
33722+ lockdep_off();
33723+ mnt_drop_write_file(file);
33724+ lockdep_on();
33725+}
33726+#endif
33727+
33728+/* ---------------------------------------------------------------------- */
33729+
33730+struct au_hinode;
33731+struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33732+ struct dentry *d2, struct au_hinode *hdir2);
33733+void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33734+ struct dentry *d2, struct au_hinode *hdir2);
33735+
33736+int vfsub_create(struct inode *dir, struct path *path, int mode,
33737+ bool want_excl);
33738+int vfsub_symlink(struct inode *dir, struct path *path,
33739+ const char *symname);
33740+int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
33741+int vfsub_link(struct dentry *src_dentry, struct inode *dir,
33742+ struct path *path, struct inode **delegated_inode);
33743+int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
33744+ struct inode *hdir, struct path *path,
33745+ struct inode **delegated_inode, unsigned int flags);
33746+int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
33747+int vfsub_rmdir(struct inode *dir, struct path *path);
33748+
33749+/* ---------------------------------------------------------------------- */
33750+
33751+ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33752+ loff_t *ppos);
33753+ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33754+ loff_t *ppos);
33755+ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33756+ loff_t *ppos);
33757+ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
33758+ loff_t *ppos);
33759+int vfsub_flush(struct file *file, fl_owner_t id);
33760+int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
33761+
33762+static inline loff_t vfsub_f_size_read(struct file *file)
33763+{
33764+ return i_size_read(file_inode(file));
33765+}
33766+
33767+static inline unsigned int vfsub_file_flags(struct file *file)
33768+{
33769+ unsigned int flags;
33770+
33771+ spin_lock(&file->f_lock);
33772+ flags = file->f_flags;
33773+ spin_unlock(&file->f_lock);
33774+
33775+ return flags;
33776+}
33777+
33778+static inline int vfsub_file_execed(struct file *file)
33779+{
33780+ /* todo: direct access f_flags */
33781+ return !!(vfsub_file_flags(file) & __FMODE_EXEC);
33782+}
33783+
33784+#if 0 /* reserved */
33785+static inline void vfsub_file_accessed(struct file *h_file)
33786+{
33787+ file_accessed(h_file);
33788+ vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
33789+}
33790+#endif
33791+
33792+#if 0 /* reserved */
33793+static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
33794+ struct dentry *h_dentry)
33795+{
33796+ struct path h_path = {
33797+ .dentry = h_dentry,
33798+ .mnt = h_mnt
33799+ };
33800+ touch_atime(&h_path);
33801+ vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
33802+}
33803+#endif
33804+
33805+static inline int vfsub_update_time(struct inode *h_inode,
33806+ struct timespec64 *ts, int flags)
33807+{
33808+ return inode_update_time(h_inode, ts, flags);
33809+ /* no vfsub_update_h_iattr() since we don't have struct path */
33810+}
33811+
33812+#ifdef CONFIG_FS_POSIX_ACL
33813+static inline int vfsub_acl_chmod(struct user_namespace *h_userns,
33814+ struct inode *h_inode, umode_t h_mode)
33815+{
33816+ int err;
33817+
33818+ err = posix_acl_chmod(h_userns, h_inode, h_mode);
33819+ if (err == -EOPNOTSUPP)
33820+ err = 0;
33821+ return err;
33822+}
33823+#else
33824+AuStubInt0(vfsub_acl_chmod, struct user_namespace *h_userns,
33825+ struct inode *h_inode, umode_t h_mode);
33826+#endif
33827+
33828+long vfsub_splice_to(struct file *in, loff_t *ppos,
33829+ struct pipe_inode_info *pipe, size_t len,
33830+ unsigned int flags);
33831+long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33832+ loff_t *ppos, size_t len, unsigned int flags);
33833+
33834+static inline long vfsub_truncate(struct path *path, loff_t length)
33835+{
33836+ long err;
33837+
33838+ lockdep_off();
33839+ err = vfs_truncate(path, length);
33840+ lockdep_on();
33841+ return err;
33842+}
33843+
33844+int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33845+ struct file *h_file);
33846+int vfsub_fsync(struct file *file, struct path *path, int datasync);
33847+
33848+/*
33849+ * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
33850+ * ioctl.
33851+ */
33852+static inline loff_t vfsub_clone_file_range(struct file *src, struct file *dst,
33853+ loff_t len)
33854+{
33855+ loff_t err;
33856+
33857+ lockdep_off();
33858+ err = vfs_clone_file_range(src, 0, dst, 0, len, /*remap_flags*/0);
33859+ lockdep_on();
33860+
33861+ return err;
33862+}
33863+
33864+/* copy_file_range(2) is a systemcall */
33865+static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
33866+ struct file *dst, loff_t dst_pos,
33867+ size_t len, unsigned int flags)
33868+{
33869+ ssize_t ssz;
33870+
33871+ lockdep_off();
33872+ ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
33873+ lockdep_on();
33874+
33875+ return ssz;
33876+}
33877+
33878+/* ---------------------------------------------------------------------- */
33879+
33880+static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
33881+{
33882+ loff_t err;
33883+
33884+ lockdep_off();
33885+ err = vfs_llseek(file, offset, origin);
33886+ lockdep_on();
33887+ return err;
33888+}
33889+
33890+/* ---------------------------------------------------------------------- */
33891+
33892+int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
33893+int vfsub_sio_rmdir(struct inode *dir, struct path *path);
33894+int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33895+ struct inode **delegated_inode);
33896+int vfsub_notify_change(struct path *path, struct iattr *ia,
33897+ struct inode **delegated_inode);
33898+int vfsub_unlink(struct inode *dir, struct path *path,
33899+ struct inode **delegated_inode, int force);
33900+
33901+static inline int vfsub_getattr(const struct path *path, struct kstat *st)
33902+{
33903+ return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
33904+}
33905+
33906+/* ---------------------------------------------------------------------- */
33907+
33908+static inline int vfsub_setxattr(struct user_namespace *userns,
33909+ struct dentry *dentry, const char *name,
33910+ const void *value, size_t size, int flags)
33911+{
33912+ int err;
33913+
33914+ lockdep_off();
33915+ err = vfs_setxattr(userns, dentry, name, value, size, flags);
33916+ lockdep_on();
33917+
33918+ return err;
33919+}
33920+
33921+static inline int vfsub_removexattr(struct user_namespace *userns,
33922+ struct dentry *dentry, const char *name)
33923+{
33924+ int err;
33925+
33926+ lockdep_off();
33927+ err = vfs_removexattr(userns, dentry, name);
33928+ lockdep_on();
33929+
33930+ return err;
33931+}
33932+
33933+#endif /* __KERNEL__ */
33934+#endif /* __AUFS_VFSUB_H__ */
33935diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
33936--- /usr/share/empty/fs/aufs/wbr_policy.c 1970-01-01 01:00:00.000000000 +0100
33937+++ linux/fs/aufs/wbr_policy.c 2021-12-03 15:38:59.939980643 +0100
33938@@ -0,0 +1,830 @@
33939+// SPDX-License-Identifier: GPL-2.0
33940+/*
33941+ * Copyright (C) 2005-2021 Junjiro R. Okajima
33942+ *
33943+ * This program, aufs is free software; you can redistribute it and/or modify
33944+ * it under the terms of the GNU General Public License as published by
33945+ * the Free Software Foundation; either version 2 of the License, or
33946+ * (at your option) any later version.
33947+ *
33948+ * This program is distributed in the hope that it will be useful,
33949+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
33950+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33951+ * GNU General Public License for more details.
33952+ *
33953+ * You should have received a copy of the GNU General Public License
33954+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
33955+ */
33956+
33957+/*
33958+ * policies for selecting one among multiple writable branches
33959+ */
33960+
33961+#include <linux/statfs.h>
33962+#include "aufs.h"
33963+
33964+/* subset of cpup_attr() */
33965+static noinline_for_stack
33966+int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
33967+{
33968+ int err, sbits;
33969+ struct iattr ia;
33970+ struct inode *h_isrc;
33971+
33972+ h_isrc = d_inode(h_src);
33973+ ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
33974+ ia.ia_mode = h_isrc->i_mode;
33975+ ia.ia_uid = h_isrc->i_uid;
33976+ ia.ia_gid = h_isrc->i_gid;
33977+ sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
33978+ au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
33979+ /* no delegation since it is just created */
33980+ err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
33981+
33982+ /* is this nfs only? */
33983+ if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
33984+ ia.ia_valid = ATTR_FORCE | ATTR_MODE;
33985+ ia.ia_mode = h_isrc->i_mode;
33986+ err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
33987+ }
33988+
33989+ return err;
33990+}
33991+
33992+#define AuCpdown_PARENT_OPQ 1
33993+#define AuCpdown_WHED (1 << 1)
33994+#define AuCpdown_MADE_DIR (1 << 2)
33995+#define AuCpdown_DIROPQ (1 << 3)
33996+#define au_ftest_cpdown(flags, name) ((flags) & AuCpdown_##name)
33997+#define au_fset_cpdown(flags, name) \
33998+ do { (flags) |= AuCpdown_##name; } while (0)
33999+#define au_fclr_cpdown(flags, name) \
34000+ do { (flags) &= ~AuCpdown_##name; } while (0)
34001+
34002+static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
34003+ unsigned int *flags)
34004+{
34005+ int err;
34006+ struct dentry *opq_dentry;
34007+
34008+ opq_dentry = au_diropq_create(dentry, bdst);
34009+ err = PTR_ERR(opq_dentry);
34010+ if (IS_ERR(opq_dentry))
34011+ goto out;
34012+ dput(opq_dentry);
34013+ au_fset_cpdown(*flags, DIROPQ);
34014+
34015+out:
34016+ return err;
34017+}
34018+
34019+static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
34020+ struct inode *dir, aufs_bindex_t bdst)
34021+{
34022+ int err;
34023+ struct path h_path;
34024+ struct au_branch *br;
34025+
34026+ br = au_sbr(dentry->d_sb, bdst);
34027+ h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
34028+ err = PTR_ERR(h_path.dentry);
34029+ if (IS_ERR(h_path.dentry))
34030+ goto out;
34031+
34032+ err = 0;
34033+ if (d_is_positive(h_path.dentry)) {
34034+ h_path.mnt = au_br_mnt(br);
34035+ err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
34036+ dentry);
34037+ }
34038+ dput(h_path.dentry);
34039+
34040+out:
34041+ return err;
34042+}
34043+
34044+static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
34045+ struct au_pin *pin,
34046+ struct dentry *h_parent, void *arg)
34047+{
34048+ int err, rerr;
34049+ aufs_bindex_t bopq, btop;
34050+ struct path h_path;
34051+ struct dentry *parent;
34052+ struct inode *h_dir, *h_inode, *inode, *dir;
34053+ unsigned int *flags = arg;
34054+
34055+ btop = au_dbtop(dentry);
34056+ /* dentry is di-locked */
34057+ parent = dget_parent(dentry);
34058+ dir = d_inode(parent);
34059+ h_dir = d_inode(h_parent);
34060+ AuDebugOn(h_dir != au_h_iptr(dir, bdst));
34061+ IMustLock(h_dir);
34062+
34063+ err = au_lkup_neg(dentry, bdst, /*wh*/0);
34064+ if (unlikely(err < 0))
34065+ goto out;
34066+ h_path.dentry = au_h_dptr(dentry, bdst);
34067+ h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
34068+ err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path, 0755);
34069+ if (unlikely(err))
34070+ goto out_put;
34071+ au_fset_cpdown(*flags, MADE_DIR);
34072+
34073+ bopq = au_dbdiropq(dentry);
34074+ au_fclr_cpdown(*flags, WHED);
34075+ au_fclr_cpdown(*flags, DIROPQ);
34076+ if (au_dbwh(dentry) == bdst)
34077+ au_fset_cpdown(*flags, WHED);
34078+ if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
34079+ au_fset_cpdown(*flags, PARENT_OPQ);
34080+ h_inode = d_inode(h_path.dentry);
34081+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
34082+ if (au_ftest_cpdown(*flags, WHED)) {
34083+ err = au_cpdown_dir_opq(dentry, bdst, flags);
34084+ if (unlikely(err)) {
34085+ inode_unlock(h_inode);
34086+ goto out_dir;
34087+ }
34088+ }
34089+
34090+ err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
34091+ inode_unlock(h_inode);
34092+ if (unlikely(err))
34093+ goto out_opq;
34094+
34095+ if (au_ftest_cpdown(*flags, WHED)) {
34096+ err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
34097+ if (unlikely(err))
34098+ goto out_opq;
34099+ }
34100+
34101+ inode = d_inode(dentry);
34102+ if (au_ibbot(inode) < bdst)
34103+ au_set_ibbot(inode, bdst);
34104+ au_set_h_iptr(inode, bdst, au_igrab(h_inode),
34105+ au_hi_flags(inode, /*isdir*/1));
34106+ au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
34107+ goto out; /* success */
34108+
34109+ /* revert */
34110+out_opq:
34111+ if (au_ftest_cpdown(*flags, DIROPQ)) {
34112+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
34113+ rerr = au_diropq_remove(dentry, bdst);
34114+ inode_unlock(h_inode);
34115+ if (unlikely(rerr)) {
34116+ AuIOErr("failed removing diropq for %pd b%d (%d)\n",
34117+ dentry, bdst, rerr);
34118+ err = -EIO;
34119+ goto out;
34120+ }
34121+ }
34122+out_dir:
34123+ if (au_ftest_cpdown(*flags, MADE_DIR)) {
34124+ rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
34125+ if (unlikely(rerr)) {
34126+ AuIOErr("failed removing %pd b%d (%d)\n",
34127+ dentry, bdst, rerr);
34128+ err = -EIO;
34129+ }
34130+ }
34131+out_put:
34132+ au_set_h_dptr(dentry, bdst, NULL);
34133+ if (au_dbbot(dentry) == bdst)
34134+ au_update_dbbot(dentry);
34135+out:
34136+ dput(parent);
34137+ return err;
34138+}
34139+
34140+int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
34141+{
34142+ int err;
34143+ unsigned int flags;
34144+
34145+ flags = 0;
34146+ err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
34147+
34148+ return err;
34149+}
34150+
34151+/* ---------------------------------------------------------------------- */
34152+
34153+/* policies for create */
34154+
34155+int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
34156+{
34157+ int err, i, j, ndentry;
34158+ aufs_bindex_t bopq;
34159+ struct au_dcsub_pages dpages;
34160+ struct au_dpage *dpage;
34161+ struct dentry **dentries, *parent, *d;
34162+
34163+ err = au_dpages_init(&dpages, GFP_NOFS);
34164+ if (unlikely(err))
34165+ goto out;
34166+ parent = dget_parent(dentry);
34167+ err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
34168+ if (unlikely(err))
34169+ goto out_free;
34170+
34171+ err = bindex;
34172+ for (i = 0; i < dpages.ndpage; i++) {
34173+ dpage = dpages.dpages + i;
34174+ dentries = dpage->dentries;
34175+ ndentry = dpage->ndentry;
34176+ for (j = 0; j < ndentry; j++) {
34177+ d = dentries[j];
34178+ di_read_lock_parent2(d, !AuLock_IR);
34179+ bopq = au_dbdiropq(d);
34180+ di_read_unlock(d, !AuLock_IR);
34181+ if (bopq >= 0 && bopq < err)
34182+ err = bopq;
34183+ }
34184+ }
34185+
34186+out_free:
34187+ dput(parent);
34188+ au_dpages_free(&dpages);
34189+out:
34190+ return err;
34191+}
34192+
34193+static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
34194+{
34195+ for (; bindex >= 0; bindex--)
34196+ if (!au_br_rdonly(au_sbr(sb, bindex)))
34197+ return bindex;
34198+ return -EROFS;
34199+}
34200+
34201+/* top down parent */
34202+static int au_wbr_create_tdp(struct dentry *dentry,
34203+ unsigned int flags __maybe_unused)
34204+{
34205+ int err;
34206+ aufs_bindex_t btop, bindex;
34207+ struct super_block *sb;
34208+ struct dentry *parent, *h_parent;
34209+
34210+ sb = dentry->d_sb;
34211+ btop = au_dbtop(dentry);
34212+ err = btop;
34213+ if (!au_br_rdonly(au_sbr(sb, btop)))
34214+ goto out;
34215+
34216+ err = -EROFS;
34217+ parent = dget_parent(dentry);
34218+ for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
34219+ h_parent = au_h_dptr(parent, bindex);
34220+ if (!h_parent || d_is_negative(h_parent))
34221+ continue;
34222+
34223+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
34224+ err = bindex;
34225+ break;
34226+ }
34227+ }
34228+ dput(parent);
34229+
34230+ /* bottom up here */
34231+ if (unlikely(err < 0)) {
34232+ err = au_wbr_bu(sb, btop - 1);
34233+ if (err >= 0)
34234+ err = au_wbr_nonopq(dentry, err);
34235+ }
34236+
34237+out:
34238+ AuDbg("b%d\n", err);
34239+ return err;
34240+}
34241+
34242+/* ---------------------------------------------------------------------- */
34243+
34244+/* an exception for the policy other than tdp */
34245+static int au_wbr_create_exp(struct dentry *dentry)
34246+{
34247+ int err;
34248+ aufs_bindex_t bwh, bdiropq;
34249+ struct dentry *parent;
34250+
34251+ err = -1;
34252+ bwh = au_dbwh(dentry);
34253+ parent = dget_parent(dentry);
34254+ bdiropq = au_dbdiropq(parent);
34255+ if (bwh >= 0) {
34256+ if (bdiropq >= 0)
34257+ err = min(bdiropq, bwh);
34258+ else
34259+ err = bwh;
34260+ AuDbg("%d\n", err);
34261+ } else if (bdiropq >= 0) {
34262+ err = bdiropq;
34263+ AuDbg("%d\n", err);
34264+ }
34265+ dput(parent);
34266+
34267+ if (err >= 0)
34268+ err = au_wbr_nonopq(dentry, err);
34269+
34270+ if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
34271+ err = -1;
34272+
34273+ AuDbg("%d\n", err);
34274+ return err;
34275+}
34276+
34277+/* ---------------------------------------------------------------------- */
34278+
34279+/* round robin */
34280+static int au_wbr_create_init_rr(struct super_block *sb)
34281+{
34282+ int err;
34283+
34284+ err = au_wbr_bu(sb, au_sbbot(sb));
34285+ atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
34286+ /* smp_mb(); */
34287+
34288+ AuDbg("b%d\n", err);
34289+ return err;
34290+}
34291+
34292+static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
34293+{
34294+ int err, nbr;
34295+ unsigned int u;
34296+ aufs_bindex_t bindex, bbot;
34297+ struct super_block *sb;
34298+ atomic_t *next;
34299+
34300+ err = au_wbr_create_exp(dentry);
34301+ if (err >= 0)
34302+ goto out;
34303+
34304+ sb = dentry->d_sb;
34305+ next = &au_sbi(sb)->si_wbr_rr_next;
34306+ bbot = au_sbbot(sb);
34307+ nbr = bbot + 1;
34308+ for (bindex = 0; bindex <= bbot; bindex++) {
34309+ if (!au_ftest_wbr(flags, DIR)) {
34310+ err = atomic_dec_return(next) + 1;
34311+ /* modulo for 0 is meaningless */
34312+ if (unlikely(!err))
34313+ err = atomic_dec_return(next) + 1;
34314+ } else
34315+ err = atomic_read(next);
34316+ AuDbg("%d\n", err);
34317+ u = err;
34318+ err = u % nbr;
34319+ AuDbg("%d\n", err);
34320+ if (!au_br_rdonly(au_sbr(sb, err)))
34321+ break;
34322+ err = -EROFS;
34323+ }
34324+
34325+ if (err >= 0)
34326+ err = au_wbr_nonopq(dentry, err);
34327+
34328+out:
34329+ AuDbg("%d\n", err);
34330+ return err;
34331+}
34332+
34333+/* ---------------------------------------------------------------------- */
34334+
34335+/* most free space */
34336+static void au_mfs(struct dentry *dentry, struct dentry *parent)
34337+{
34338+ struct super_block *sb;
34339+ struct au_branch *br;
34340+ struct au_wbr_mfs *mfs;
34341+ struct dentry *h_parent;
34342+ aufs_bindex_t bindex, bbot;
34343+ int err;
34344+ unsigned long long b, bavail;
34345+ struct path h_path;
34346+ /* reduce the stack usage */
34347+ struct kstatfs *st;
34348+
34349+ st = kmalloc(sizeof(*st), GFP_NOFS);
34350+ if (unlikely(!st)) {
34351+ AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
34352+ return;
34353+ }
34354+
34355+ bavail = 0;
34356+ sb = dentry->d_sb;
34357+ mfs = &au_sbi(sb)->si_wbr_mfs;
34358+ MtxMustLock(&mfs->mfs_lock);
34359+ mfs->mfs_bindex = -EROFS;
34360+ mfs->mfsrr_bytes = 0;
34361+ if (!parent) {
34362+ bindex = 0;
34363+ bbot = au_sbbot(sb);
34364+ } else {
34365+ bindex = au_dbtop(parent);
34366+ bbot = au_dbtaildir(parent);
34367+ }
34368+
34369+ for (; bindex <= bbot; bindex++) {
34370+ if (parent) {
34371+ h_parent = au_h_dptr(parent, bindex);
34372+ if (!h_parent || d_is_negative(h_parent))
34373+ continue;
34374+ }
34375+ br = au_sbr(sb, bindex);
34376+ if (au_br_rdonly(br))
34377+ continue;
34378+
34379+ /* sb->s_root for NFS is unreliable */
34380+ h_path.mnt = au_br_mnt(br);
34381+ h_path.dentry = h_path.mnt->mnt_root;
34382+ err = vfs_statfs(&h_path, st);
34383+ if (unlikely(err)) {
34384+ AuWarn1("failed statfs, b%d, %d\n", bindex, err);
34385+ continue;
34386+ }
34387+
34388+ /* when the available size is equal, select the lower one */
34389+ BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
34390+ || sizeof(b) < sizeof(st->f_bsize));
34391+ b = st->f_bavail * st->f_bsize;
34392+ br->br_wbr->wbr_bytes = b;
34393+ if (b >= bavail) {
34394+ bavail = b;
34395+ mfs->mfs_bindex = bindex;
34396+ mfs->mfs_jiffy = jiffies;
34397+ }
34398+ }
34399+
34400+ mfs->mfsrr_bytes = bavail;
34401+ AuDbg("b%d\n", mfs->mfs_bindex);
34402+ au_kfree_rcu(st);
34403+}
34404+
34405+static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
34406+{
34407+ int err;
34408+ struct dentry *parent;
34409+ struct super_block *sb;
34410+ struct au_wbr_mfs *mfs;
34411+
34412+ err = au_wbr_create_exp(dentry);
34413+ if (err >= 0)
34414+ goto out;
34415+
34416+ sb = dentry->d_sb;
34417+ parent = NULL;
34418+ if (au_ftest_wbr(flags, PARENT))
34419+ parent = dget_parent(dentry);
34420+ mfs = &au_sbi(sb)->si_wbr_mfs;
34421+ mutex_lock(&mfs->mfs_lock);
34422+ if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34423+ || mfs->mfs_bindex < 0
34424+ || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
34425+ au_mfs(dentry, parent);
34426+ mutex_unlock(&mfs->mfs_lock);
34427+ err = mfs->mfs_bindex;
34428+ dput(parent);
34429+
34430+ if (err >= 0)
34431+ err = au_wbr_nonopq(dentry, err);
34432+
34433+out:
34434+ AuDbg("b%d\n", err);
34435+ return err;
34436+}
34437+
34438+static int au_wbr_create_init_mfs(struct super_block *sb)
34439+{
34440+ struct au_wbr_mfs *mfs;
34441+
34442+ mfs = &au_sbi(sb)->si_wbr_mfs;
34443+ mutex_init(&mfs->mfs_lock);
34444+ mfs->mfs_jiffy = 0;
34445+ mfs->mfs_bindex = -EROFS;
34446+
34447+ return 0;
34448+}
34449+
34450+static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
34451+{
34452+ mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
34453+ return 0;
34454+}
34455+
34456+/* ---------------------------------------------------------------------- */
34457+
34458+/* top down regardless parent, and then mfs */
34459+static int au_wbr_create_tdmfs(struct dentry *dentry,
34460+ unsigned int flags __maybe_unused)
34461+{
34462+ int err;
34463+ aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
34464+ unsigned long long watermark;
34465+ struct super_block *sb;
34466+ struct au_wbr_mfs *mfs;
34467+ struct au_branch *br;
34468+ struct dentry *parent;
34469+
34470+ sb = dentry->d_sb;
34471+ mfs = &au_sbi(sb)->si_wbr_mfs;
34472+ mutex_lock(&mfs->mfs_lock);
34473+ if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34474+ || mfs->mfs_bindex < 0)
34475+ au_mfs(dentry, /*parent*/NULL);
34476+ watermark = mfs->mfsrr_watermark;
34477+ bmfs = mfs->mfs_bindex;
34478+ mutex_unlock(&mfs->mfs_lock);
34479+
34480+ /* another style of au_wbr_create_exp() */
34481+ bwh = au_dbwh(dentry);
34482+ parent = dget_parent(dentry);
34483+ btail = au_dbtaildir(parent);
34484+ if (bwh >= 0 && bwh < btail)
34485+ btail = bwh;
34486+
34487+ err = au_wbr_nonopq(dentry, btail);
34488+ if (unlikely(err < 0))
34489+ goto out;
34490+ btail = err;
34491+ bfound = -1;
34492+ for (bindex = 0; bindex <= btail; bindex++) {
34493+ br = au_sbr(sb, bindex);
34494+ if (au_br_rdonly(br))
34495+ continue;
34496+ if (br->br_wbr->wbr_bytes > watermark) {
34497+ bfound = bindex;
34498+ break;
34499+ }
34500+ }
34501+ err = bfound;
34502+ if (err < 0)
34503+ err = bmfs;
34504+
34505+out:
34506+ dput(parent);
34507+ AuDbg("b%d\n", err);
34508+ return err;
34509+}
34510+
34511+/* ---------------------------------------------------------------------- */
34512+
34513+/* most free space and then round robin */
34514+static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
34515+{
34516+ int err;
34517+ struct au_wbr_mfs *mfs;
34518+
34519+ err = au_wbr_create_mfs(dentry, flags);
34520+ if (err >= 0) {
34521+ mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
34522+ mutex_lock(&mfs->mfs_lock);
34523+ if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
34524+ err = au_wbr_create_rr(dentry, flags);
34525+ mutex_unlock(&mfs->mfs_lock);
34526+ }
34527+
34528+ AuDbg("b%d\n", err);
34529+ return err;
34530+}
34531+
34532+static int au_wbr_create_init_mfsrr(struct super_block *sb)
34533+{
34534+ int err;
34535+
34536+ au_wbr_create_init_mfs(sb); /* ignore */
34537+ err = au_wbr_create_init_rr(sb);
34538+
34539+ return err;
34540+}
34541+
34542+/* ---------------------------------------------------------------------- */
34543+
34544+/* top down parent and most free space */
34545+static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
34546+{
34547+ int err, e2;
34548+ unsigned long long b;
34549+ aufs_bindex_t bindex, btop, bbot;
34550+ struct super_block *sb;
34551+ struct dentry *parent, *h_parent;
34552+ struct au_branch *br;
34553+
34554+ err = au_wbr_create_tdp(dentry, flags);
34555+ if (unlikely(err < 0))
34556+ goto out;
34557+ parent = dget_parent(dentry);
34558+ btop = au_dbtop(parent);
34559+ bbot = au_dbtaildir(parent);
34560+ if (btop == bbot)
34561+ goto out_parent; /* success */
34562+
34563+ e2 = au_wbr_create_mfs(dentry, flags);
34564+ if (e2 < 0)
34565+ goto out_parent; /* success */
34566+
34567+ /* when the available size is equal, select upper one */
34568+ sb = dentry->d_sb;
34569+ br = au_sbr(sb, err);
34570+ b = br->br_wbr->wbr_bytes;
34571+ AuDbg("b%d, %llu\n", err, b);
34572+
34573+ for (bindex = btop; bindex <= bbot; bindex++) {
34574+ h_parent = au_h_dptr(parent, bindex);
34575+ if (!h_parent || d_is_negative(h_parent))
34576+ continue;
34577+
34578+ br = au_sbr(sb, bindex);
34579+ if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
34580+ b = br->br_wbr->wbr_bytes;
34581+ err = bindex;
34582+ AuDbg("b%d, %llu\n", err, b);
34583+ }
34584+ }
34585+
34586+ if (err >= 0)
34587+ err = au_wbr_nonopq(dentry, err);
34588+
34589+out_parent:
34590+ dput(parent);
34591+out:
34592+ AuDbg("b%d\n", err);
34593+ return err;
34594+}
34595+
34596+/* ---------------------------------------------------------------------- */
34597+
34598+/*
34599+ * - top down parent
34600+ * - most free space with parent
34601+ * - most free space round-robin regardless parent
34602+ */
34603+static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
34604+{
34605+ int err;
34606+ unsigned long long watermark;
34607+ struct super_block *sb;
34608+ struct au_branch *br;
34609+ struct au_wbr_mfs *mfs;
34610+
34611+ err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
34612+ if (unlikely(err < 0))
34613+ goto out;
34614+
34615+ sb = dentry->d_sb;
34616+ br = au_sbr(sb, err);
34617+ mfs = &au_sbi(sb)->si_wbr_mfs;
34618+ mutex_lock(&mfs->mfs_lock);
34619+ watermark = mfs->mfsrr_watermark;
34620+ mutex_unlock(&mfs->mfs_lock);
34621+ if (br->br_wbr->wbr_bytes < watermark)
34622+ /* regardless the parent dir */
34623+ err = au_wbr_create_mfsrr(dentry, flags);
34624+
34625+out:
34626+ AuDbg("b%d\n", err);
34627+ return err;
34628+}
34629+
34630+/* ---------------------------------------------------------------------- */
34631+
34632+/* policies for copyup */
34633+
34634+/* top down parent */
34635+static int au_wbr_copyup_tdp(struct dentry *dentry)
34636+{
34637+ return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
34638+}
34639+
34640+/* bottom up parent */
34641+static int au_wbr_copyup_bup(struct dentry *dentry)
34642+{
34643+ int err;
34644+ aufs_bindex_t bindex, btop;
34645+ struct dentry *parent, *h_parent;
34646+ struct super_block *sb;
34647+
34648+ err = -EROFS;
34649+ sb = dentry->d_sb;
34650+ parent = dget_parent(dentry);
34651+ btop = au_dbtop(parent);
34652+ for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
34653+ h_parent = au_h_dptr(parent, bindex);
34654+ if (!h_parent || d_is_negative(h_parent))
34655+ continue;
34656+
34657+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
34658+ err = bindex;
34659+ break;
34660+ }
34661+ }
34662+ dput(parent);
34663+
34664+ /* bottom up here */
34665+ if (unlikely(err < 0))
34666+ err = au_wbr_bu(sb, btop - 1);
34667+
34668+ AuDbg("b%d\n", err);
34669+ return err;
34670+}
34671+
34672+/* bottom up */
34673+int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
34674+{
34675+ int err;
34676+
34677+ err = au_wbr_bu(dentry->d_sb, btop);
34678+ AuDbg("b%d\n", err);
34679+ if (err > btop)
34680+ err = au_wbr_nonopq(dentry, err);
34681+
34682+ AuDbg("b%d\n", err);
34683+ return err;
34684+}
34685+
34686+static int au_wbr_copyup_bu(struct dentry *dentry)
34687+{
34688+ int err;
34689+ aufs_bindex_t btop;
34690+
34691+ btop = au_dbtop(dentry);
34692+ err = au_wbr_do_copyup_bu(dentry, btop);
34693+ return err;
34694+}
34695+
34696+/* ---------------------------------------------------------------------- */
34697+
34698+struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
34699+ [AuWbrCopyup_TDP] = {
34700+ .copyup = au_wbr_copyup_tdp
34701+ },
34702+ [AuWbrCopyup_BUP] = {
34703+ .copyup = au_wbr_copyup_bup
34704+ },
34705+ [AuWbrCopyup_BU] = {
34706+ .copyup = au_wbr_copyup_bu
34707+ }
34708+};
34709+
34710+struct au_wbr_create_operations au_wbr_create_ops[] = {
34711+ [AuWbrCreate_TDP] = {
34712+ .create = au_wbr_create_tdp
34713+ },
34714+ [AuWbrCreate_RR] = {
34715+ .create = au_wbr_create_rr,
34716+ .init = au_wbr_create_init_rr
34717+ },
34718+ [AuWbrCreate_MFS] = {
34719+ .create = au_wbr_create_mfs,
34720+ .init = au_wbr_create_init_mfs,
34721+ .fin = au_wbr_create_fin_mfs
34722+ },
34723+ [AuWbrCreate_MFSV] = {
34724+ .create = au_wbr_create_mfs,
34725+ .init = au_wbr_create_init_mfs,
34726+ .fin = au_wbr_create_fin_mfs
34727+ },
34728+ [AuWbrCreate_MFSRR] = {
34729+ .create = au_wbr_create_mfsrr,
34730+ .init = au_wbr_create_init_mfsrr,
34731+ .fin = au_wbr_create_fin_mfs
34732+ },
34733+ [AuWbrCreate_MFSRRV] = {
34734+ .create = au_wbr_create_mfsrr,
34735+ .init = au_wbr_create_init_mfsrr,
34736+ .fin = au_wbr_create_fin_mfs
34737+ },
34738+ [AuWbrCreate_TDMFS] = {
34739+ .create = au_wbr_create_tdmfs,
34740+ .init = au_wbr_create_init_mfs,
34741+ .fin = au_wbr_create_fin_mfs
34742+ },
34743+ [AuWbrCreate_TDMFSV] = {
34744+ .create = au_wbr_create_tdmfs,
34745+ .init = au_wbr_create_init_mfs,
34746+ .fin = au_wbr_create_fin_mfs
34747+ },
34748+ [AuWbrCreate_PMFS] = {
34749+ .create = au_wbr_create_pmfs,
34750+ .init = au_wbr_create_init_mfs,
34751+ .fin = au_wbr_create_fin_mfs
34752+ },
34753+ [AuWbrCreate_PMFSV] = {
34754+ .create = au_wbr_create_pmfs,
34755+ .init = au_wbr_create_init_mfs,
34756+ .fin = au_wbr_create_fin_mfs
34757+ },
34758+ [AuWbrCreate_PMFSRR] = {
34759+ .create = au_wbr_create_pmfsrr,
34760+ .init = au_wbr_create_init_mfsrr,
34761+ .fin = au_wbr_create_fin_mfs
34762+ },
34763+ [AuWbrCreate_PMFSRRV] = {
34764+ .create = au_wbr_create_pmfsrr,
34765+ .init = au_wbr_create_init_mfsrr,
34766+ .fin = au_wbr_create_fin_mfs
34767+ }
34768+};
34769diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
34770--- /usr/share/empty/fs/aufs/whout.c 1970-01-01 01:00:00.000000000 +0100
34771+++ linux/fs/aufs/whout.c 2021-12-03 15:40:58.236647297 +0100
34772@@ -0,0 +1,1072 @@
34773+// SPDX-License-Identifier: GPL-2.0
34774+/*
34775+ * Copyright (C) 2005-2021 Junjiro R. Okajima
34776+ *
34777+ * This program, aufs is free software; you can redistribute it and/or modify
34778+ * it under the terms of the GNU General Public License as published by
34779+ * the Free Software Foundation; either version 2 of the License, or
34780+ * (at your option) any later version.
34781+ *
34782+ * This program is distributed in the hope that it will be useful,
34783+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
34784+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34785+ * GNU General Public License for more details.
34786+ *
34787+ * You should have received a copy of the GNU General Public License
34788+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
34789+ */
34790+
34791+/*
34792+ * whiteout for logical deletion and opaque directory
34793+ */
34794+
34795+#include "aufs.h"
34796+
34797+#define WH_MASK 0444
34798+
34799+/*
34800+ * If a directory contains this file, then it is opaque. We start with the
34801+ * .wh. flag so that it is blocked by lookup.
34802+ */
34803+static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
34804+ sizeof(AUFS_WH_DIROPQ) - 1);
34805+
34806+/*
34807+ * generate whiteout name, which is NOT terminated by NULL.
34808+ * @name: original d_name.name
34809+ * @len: original d_name.len
34810+ * @wh: whiteout qstr
34811+ * returns zero when succeeds, otherwise error.
34812+ * succeeded value as wh->name should be freed by kfree().
34813+ */
34814+int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
34815+{
34816+ char *p;
34817+
34818+ if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
34819+ return -ENAMETOOLONG;
34820+
34821+ wh->len = name->len + AUFS_WH_PFX_LEN;
34822+ p = kmalloc(wh->len, GFP_NOFS);
34823+ wh->name = p;
34824+ if (p) {
34825+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
34826+ memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
34827+ /* smp_mb(); */
34828+ return 0;
34829+ }
34830+ return -ENOMEM;
34831+}
34832+
34833+/* ---------------------------------------------------------------------- */
34834+
34835+/*
34836+ * test if the @wh_name exists under @h_ppath.
34837+ * @try_sio specifies the necessary of super-io.
34838+ */
34839+int au_wh_test(struct user_namespace *h_userns, struct path *h_ppath,
34840+ struct qstr *wh_name, int try_sio)
34841+{
34842+ int err;
34843+ struct dentry *wh_dentry;
34844+
34845+ if (!try_sio)
34846+ wh_dentry = vfsub_lkup_one(wh_name, h_ppath);
34847+ else
34848+ wh_dentry = au_sio_lkup_one(h_userns, wh_name, h_ppath);
34849+ err = PTR_ERR(wh_dentry);
34850+ if (IS_ERR(wh_dentry)) {
34851+ if (err == -ENAMETOOLONG)
34852+ err = 0;
34853+ goto out;
34854+ }
34855+
34856+ err = 0;
34857+ if (d_is_negative(wh_dentry))
34858+ goto out_wh; /* success */
34859+
34860+ err = 1;
34861+ if (d_is_reg(wh_dentry))
34862+ goto out_wh; /* success */
34863+
34864+ err = -EIO;
34865+ AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
34866+ wh_dentry, d_inode(wh_dentry)->i_mode);
34867+
34868+out_wh:
34869+ dput(wh_dentry);
34870+out:
34871+ return err;
34872+}
34873+
34874+/*
34875+ * test if the @h_path->dentry sets opaque or not.
34876+ */
34877+int au_diropq_test(struct user_namespace *h_userns, struct path *h_path)
34878+{
34879+ int err;
34880+ struct inode *h_dir;
34881+
34882+ h_dir = d_inode(h_path->dentry);
34883+ err = au_wh_test(h_userns, h_path, &diropq_name,
34884+ au_test_h_perm_sio(h_userns, h_dir, MAY_EXEC));
34885+ return err;
34886+}
34887+
34888+/*
34889+ * returns a negative dentry whose name is unique and temporary.
34890+ */
34891+struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
34892+ struct qstr *prefix)
34893+{
34894+ struct dentry *dentry;
34895+ int i;
34896+ char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
34897+ *name, *p;
34898+ /* strict atomic_t is unnecessary here */
34899+ static unsigned short cnt;
34900+ struct qstr qs;
34901+ struct path h_ppath;
34902+ struct user_namespace *h_userns;
34903+
34904+ BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
34905+
34906+ name = defname;
34907+ qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
34908+ if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
34909+ dentry = ERR_PTR(-ENAMETOOLONG);
34910+ if (unlikely(qs.len > NAME_MAX))
34911+ goto out;
34912+ dentry = ERR_PTR(-ENOMEM);
34913+ name = kmalloc(qs.len + 1, GFP_NOFS);
34914+ if (unlikely(!name))
34915+ goto out;
34916+ }
34917+
34918+ /* doubly whiteout-ed */
34919+ memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
34920+ p = name + AUFS_WH_PFX_LEN * 2;
34921+ memcpy(p, prefix->name, prefix->len);
34922+ p += prefix->len;
34923+ *p++ = '.';
34924+ AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
34925+
34926+ h_ppath.dentry = h_parent;
34927+ h_ppath.mnt = au_br_mnt(br);
34928+ h_userns = au_br_userns(br);
34929+ qs.name = name;
34930+ for (i = 0; i < 3; i++) {
34931+ sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
34932+ dentry = au_sio_lkup_one(h_userns, &qs, &h_ppath);
34933+ if (IS_ERR(dentry) || d_is_negative(dentry))
34934+ goto out_name;
34935+ dput(dentry);
34936+ }
34937+ /* pr_warn("could not get random name\n"); */
34938+ dentry = ERR_PTR(-EEXIST);
34939+ AuDbg("%.*s\n", AuLNPair(&qs));
34940+ BUG();
34941+
34942+out_name:
34943+ if (name != defname)
34944+ au_kfree_try_rcu(name);
34945+out:
34946+ AuTraceErrPtr(dentry);
34947+ return dentry;
34948+}
34949+
34950+/*
34951+ * rename the @h_dentry on @br to the whiteouted temporary name.
34952+ */
34953+int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
34954+{
34955+ int err;
34956+ struct path h_path = {
34957+ .mnt = au_br_mnt(br)
34958+ };
34959+ struct inode *h_dir, *delegated;
34960+ struct dentry *h_parent;
34961+
34962+ h_parent = h_dentry->d_parent; /* dir inode is locked */
34963+ h_dir = d_inode(h_parent);
34964+ IMustLock(h_dir);
34965+
34966+ h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
34967+ err = PTR_ERR(h_path.dentry);
34968+ if (IS_ERR(h_path.dentry))
34969+ goto out;
34970+
34971+ /* under the same dir, no need to lock_rename() */
34972+ delegated = NULL;
34973+ err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
34974+ /*flags*/0);
34975+ AuTraceErr(err);
34976+ if (unlikely(err == -EWOULDBLOCK)) {
34977+ pr_warn("cannot retry for NFSv4 delegation"
34978+ " for an internal rename\n");
34979+ iput(delegated);
34980+ }
34981+ dput(h_path.dentry);
34982+
34983+out:
34984+ AuTraceErr(err);
34985+ return err;
34986+}
34987+
34988+/* ---------------------------------------------------------------------- */
34989+/*
34990+ * functions for removing a whiteout
34991+ */
34992+
34993+static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
34994+{
34995+ int err, force;
34996+ struct inode *delegated;
34997+
34998+ /*
34999+ * forces superio when the dir has a sticky bit.
35000+ * this may be a violation of unix fs semantics.
35001+ */
35002+ force = (h_dir->i_mode & S_ISVTX)
35003+ && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
35004+ delegated = NULL;
35005+ err = vfsub_unlink(h_dir, h_path, &delegated, force);
35006+ if (unlikely(err == -EWOULDBLOCK)) {
35007+ pr_warn("cannot retry for NFSv4 delegation"
35008+ " for an internal unlink\n");
35009+ iput(delegated);
35010+ }
35011+ return err;
35012+}
35013+
35014+int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35015+ struct dentry *dentry)
35016+{
35017+ int err;
35018+
35019+ err = do_unlink_wh(h_dir, h_path);
35020+ if (!err && dentry)
35021+ au_set_dbwh(dentry, -1);
35022+
35023+ return err;
35024+}
35025+
35026+static int unlink_wh_name(struct path *h_ppath, struct qstr *wh)
35027+{
35028+ int err;
35029+ struct path h_path;
35030+
35031+ err = 0;
35032+ h_path.dentry = vfsub_lkup_one(wh, h_ppath);
35033+ if (IS_ERR(h_path.dentry))
35034+ err = PTR_ERR(h_path.dentry);
35035+ else {
35036+ if (d_is_reg(h_path.dentry)) {
35037+ h_path.mnt = h_ppath->mnt;
35038+ err = do_unlink_wh(d_inode(h_ppath->dentry), &h_path);
35039+ }
35040+ dput(h_path.dentry);
35041+ }
35042+
35043+ return err;
35044+}
35045+
35046+/* ---------------------------------------------------------------------- */
35047+/*
35048+ * initialize/clean whiteout for a branch
35049+ */
35050+
35051+static void au_wh_clean(struct inode *h_dir, struct path *whpath,
35052+ const int isdir)
35053+{
35054+ int err;
35055+ struct inode *delegated;
35056+
35057+ if (d_is_negative(whpath->dentry))
35058+ return;
35059+
35060+ if (isdir)
35061+ err = vfsub_rmdir(h_dir, whpath);
35062+ else {
35063+ delegated = NULL;
35064+ err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
35065+ if (unlikely(err == -EWOULDBLOCK)) {
35066+ pr_warn("cannot retry for NFSv4 delegation"
35067+ " for an internal unlink\n");
35068+ iput(delegated);
35069+ }
35070+ }
35071+ if (unlikely(err))
35072+ pr_warn("failed removing %pd (%d), ignored.\n",
35073+ whpath->dentry, err);
35074+}
35075+
35076+static int test_linkable(struct dentry *h_root)
35077+{
35078+ struct inode *h_dir = d_inode(h_root);
35079+
35080+ if (h_dir->i_op->link)
35081+ return 0;
35082+
35083+ pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
35084+ h_root, au_sbtype(h_root->d_sb));
35085+ return -ENOSYS; /* the branch doesn't have its ->link() */
35086+}
35087+
35088+/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
35089+static int au_whdir(struct inode *h_dir, struct path *path)
35090+{
35091+ int err;
35092+
35093+ err = -EEXIST;
35094+ if (d_is_negative(path->dentry)) {
35095+ int mode = 0700;
35096+
35097+ if (au_test_nfs(path->dentry->d_sb))
35098+ mode |= 0111;
35099+ err = vfsub_mkdir(h_dir, path, mode);
35100+ } else if (d_is_dir(path->dentry))
35101+ err = 0;
35102+ else
35103+ pr_err("unknown %pd exists\n", path->dentry);
35104+
35105+ return err;
35106+}
35107+
35108+struct au_wh_base {
35109+ const struct qstr *name;
35110+ struct dentry *dentry;
35111+};
35112+
35113+static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
35114+ struct path *h_path)
35115+{
35116+ h_path->dentry = base[AuBrWh_BASE].dentry;
35117+ au_wh_clean(h_dir, h_path, /*isdir*/0);
35118+ h_path->dentry = base[AuBrWh_PLINK].dentry;
35119+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35120+ h_path->dentry = base[AuBrWh_ORPH].dentry;
35121+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35122+}
35123+
35124+/*
35125+ * returns tri-state,
35126+ * minus: error, caller should print the message
35127+ * zero: success
35128+ * plus: error, caller should NOT print the message
35129+ */
35130+static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
35131+ int do_plink, struct au_wh_base base[],
35132+ struct path *h_path)
35133+{
35134+ int err;
35135+ struct inode *h_dir;
35136+
35137+ h_dir = d_inode(h_root);
35138+ h_path->dentry = base[AuBrWh_BASE].dentry;
35139+ au_wh_clean(h_dir, h_path, /*isdir*/0);
35140+ h_path->dentry = base[AuBrWh_PLINK].dentry;
35141+ if (do_plink) {
35142+ err = test_linkable(h_root);
35143+ if (unlikely(err)) {
35144+ err = 1;
35145+ goto out;
35146+ }
35147+
35148+ err = au_whdir(h_dir, h_path);
35149+ if (unlikely(err))
35150+ goto out;
35151+ wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35152+ } else
35153+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35154+ h_path->dentry = base[AuBrWh_ORPH].dentry;
35155+ err = au_whdir(h_dir, h_path);
35156+ if (unlikely(err))
35157+ goto out;
35158+ wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35159+
35160+out:
35161+ return err;
35162+}
35163+
35164+/*
35165+ * for the moment, aufs supports the branch filesystem which does not support
35166+ * link(2). testing on FAT which does not support i_op->setattr() fully either,
35167+ * copyup failed. finally, such filesystem will not be used as the writable
35168+ * branch.
35169+ *
35170+ * returns tri-state, see above.
35171+ */
35172+static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
35173+ int do_plink, struct au_wh_base base[],
35174+ struct path *h_path)
35175+{
35176+ int err;
35177+ struct inode *h_dir;
35178+
35179+ WbrWhMustWriteLock(wbr);
35180+
35181+ err = test_linkable(h_root);
35182+ if (unlikely(err)) {
35183+ err = 1;
35184+ goto out;
35185+ }
35186+
35187+ /*
35188+ * todo: should this create be done in /sbin/mount.aufs helper?
35189+ */
35190+ err = -EEXIST;
35191+ h_dir = d_inode(h_root);
35192+ if (d_is_negative(base[AuBrWh_BASE].dentry)) {
35193+ h_path->dentry = base[AuBrWh_BASE].dentry;
35194+ err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
35195+ } else if (d_is_reg(base[AuBrWh_BASE].dentry))
35196+ err = 0;
35197+ else
35198+ pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
35199+ if (unlikely(err))
35200+ goto out;
35201+
35202+ h_path->dentry = base[AuBrWh_PLINK].dentry;
35203+ if (do_plink) {
35204+ err = au_whdir(h_dir, h_path);
35205+ if (unlikely(err))
35206+ goto out;
35207+ wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35208+ } else
35209+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35210+ wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
35211+
35212+ h_path->dentry = base[AuBrWh_ORPH].dentry;
35213+ err = au_whdir(h_dir, h_path);
35214+ if (unlikely(err))
35215+ goto out;
35216+ wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35217+
35218+out:
35219+ return err;
35220+}
35221+
35222+/*
35223+ * initialize the whiteout base file/dir for @br.
35224+ */
35225+int au_wh_init(struct au_branch *br, struct super_block *sb)
35226+{
35227+ int err, i;
35228+ const unsigned char do_plink
35229+ = !!au_opt_test(au_mntflags(sb), PLINK);
35230+ struct inode *h_dir;
35231+ struct path path = br->br_path;
35232+ struct dentry *h_root = path.dentry;
35233+ struct au_wbr *wbr = br->br_wbr;
35234+ static const struct qstr base_name[] = {
35235+ [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
35236+ sizeof(AUFS_BASE_NAME) - 1),
35237+ [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
35238+ sizeof(AUFS_PLINKDIR_NAME) - 1),
35239+ [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
35240+ sizeof(AUFS_ORPHDIR_NAME) - 1)
35241+ };
35242+ struct au_wh_base base[] = {
35243+ [AuBrWh_BASE] = {
35244+ .name = base_name + AuBrWh_BASE,
35245+ .dentry = NULL
35246+ },
35247+ [AuBrWh_PLINK] = {
35248+ .name = base_name + AuBrWh_PLINK,
35249+ .dentry = NULL
35250+ },
35251+ [AuBrWh_ORPH] = {
35252+ .name = base_name + AuBrWh_ORPH,
35253+ .dentry = NULL
35254+ }
35255+ };
35256+
35257+ if (wbr)
35258+ WbrWhMustWriteLock(wbr);
35259+
35260+ for (i = 0; i < AuBrWh_Last; i++) {
35261+ /* doubly whiteouted */
35262+ struct dentry *d;
35263+
35264+ d = au_wh_lkup(h_root, (void *)base[i].name, br);
35265+ err = PTR_ERR(d);
35266+ if (IS_ERR(d))
35267+ goto out;
35268+
35269+ base[i].dentry = d;
35270+ AuDebugOn(wbr
35271+ && wbr->wbr_wh[i]
35272+ && wbr->wbr_wh[i] != base[i].dentry);
35273+ }
35274+
35275+ if (wbr)
35276+ for (i = 0; i < AuBrWh_Last; i++) {
35277+ dput(wbr->wbr_wh[i]);
35278+ wbr->wbr_wh[i] = NULL;
35279+ }
35280+
35281+ err = 0;
35282+ if (!au_br_writable(br->br_perm)) {
35283+ h_dir = d_inode(h_root);
35284+ au_wh_init_ro(h_dir, base, &path);
35285+ } else if (!au_br_wh_linkable(br->br_perm)) {
35286+ err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
35287+ if (err > 0)
35288+ goto out;
35289+ else if (err)
35290+ goto out_err;
35291+ } else {
35292+ err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
35293+ if (err > 0)
35294+ goto out;
35295+ else if (err)
35296+ goto out_err;
35297+ }
35298+ goto out; /* success */
35299+
35300+out_err:
35301+ pr_err("an error(%d) on the writable branch %pd(%s)\n",
35302+ err, h_root, au_sbtype(h_root->d_sb));
35303+out:
35304+ for (i = 0; i < AuBrWh_Last; i++)
35305+ dput(base[i].dentry);
35306+ return err;
35307+}
35308+
35309+/* ---------------------------------------------------------------------- */
35310+/*
35311+ * whiteouts are all hard-linked usually.
35312+ * when its link count reaches a ceiling, we create a new whiteout base
35313+ * asynchronously.
35314+ */
35315+
35316+struct reinit_br_wh {
35317+ struct super_block *sb;
35318+ struct au_branch *br;
35319+};
35320+
35321+static void reinit_br_wh(void *arg)
35322+{
35323+ int err;
35324+ aufs_bindex_t bindex;
35325+ struct path h_path;
35326+ struct reinit_br_wh *a = arg;
35327+ struct au_wbr *wbr;
35328+ struct inode *dir, *delegated;
35329+ struct dentry *h_root;
35330+ struct au_hinode *hdir;
35331+
35332+ err = 0;
35333+ wbr = a->br->br_wbr;
35334+ /* big aufs lock */
35335+ si_noflush_write_lock(a->sb);
35336+ if (!au_br_writable(a->br->br_perm))
35337+ goto out;
35338+ bindex = au_br_index(a->sb, a->br->br_id);
35339+ if (unlikely(bindex < 0))
35340+ goto out;
35341+
35342+ di_read_lock_parent(a->sb->s_root, AuLock_IR);
35343+ dir = d_inode(a->sb->s_root);
35344+ hdir = au_hi(dir, bindex);
35345+ h_root = au_h_dptr(a->sb->s_root, bindex);
35346+ AuDebugOn(h_root != au_br_dentry(a->br));
35347+
35348+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
35349+ wbr_wh_write_lock(wbr);
35350+ err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
35351+ h_root, a->br);
35352+ if (!err) {
35353+ h_path.dentry = wbr->wbr_whbase;
35354+ h_path.mnt = au_br_mnt(a->br);
35355+ delegated = NULL;
35356+ err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
35357+ /*force*/0);
35358+ if (unlikely(err == -EWOULDBLOCK)) {
35359+ pr_warn("cannot retry for NFSv4 delegation"
35360+ " for an internal unlink\n");
35361+ iput(delegated);
35362+ }
35363+ } else {
35364+ pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
35365+ err = 0;
35366+ }
35367+ dput(wbr->wbr_whbase);
35368+ wbr->wbr_whbase = NULL;
35369+ if (!err)
35370+ err = au_wh_init(a->br, a->sb);
35371+ wbr_wh_write_unlock(wbr);
35372+ au_hn_inode_unlock(hdir);
35373+ di_read_unlock(a->sb->s_root, AuLock_IR);
35374+ if (!err)
35375+ au_fhsm_wrote(a->sb, bindex, /*force*/0);
35376+
35377+out:
35378+ if (wbr)
35379+ atomic_dec(&wbr->wbr_wh_running);
35380+ au_lcnt_dec(&a->br->br_count);
35381+ si_write_unlock(a->sb);
35382+ au_nwt_done(&au_sbi(a->sb)->si_nowait);
35383+ au_kfree_rcu(a);
35384+ if (unlikely(err))
35385+ AuIOErr("err %d\n", err);
35386+}
35387+
35388+static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
35389+{
35390+ int do_dec, wkq_err;
35391+ struct reinit_br_wh *arg;
35392+
35393+ do_dec = 1;
35394+ if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
35395+ goto out;
35396+
35397+ /* ignore ENOMEM */
35398+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
35399+ if (arg) {
35400+ /*
35401+ * dec(wh_running), kfree(arg) and dec(br_count)
35402+ * in reinit function
35403+ */
35404+ arg->sb = sb;
35405+ arg->br = br;
35406+ au_lcnt_inc(&br->br_count);
35407+ wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
35408+ if (unlikely(wkq_err)) {
35409+ atomic_dec(&br->br_wbr->wbr_wh_running);
35410+ au_lcnt_dec(&br->br_count);
35411+ au_kfree_rcu(arg);
35412+ }
35413+ do_dec = 0;
35414+ }
35415+
35416+out:
35417+ if (do_dec)
35418+ atomic_dec(&br->br_wbr->wbr_wh_running);
35419+}
35420+
35421+/* ---------------------------------------------------------------------- */
35422+
35423+/*
35424+ * create the whiteout @wh.
35425+ */
35426+static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
35427+ struct dentry *wh)
35428+{
35429+ int err;
35430+ struct path h_path = {
35431+ .dentry = wh
35432+ };
35433+ struct au_branch *br;
35434+ struct au_wbr *wbr;
35435+ struct dentry *h_parent;
35436+ struct inode *h_dir, *delegated;
35437+
35438+ h_parent = wh->d_parent; /* dir inode is locked */
35439+ h_dir = d_inode(h_parent);
35440+ IMustLock(h_dir);
35441+
35442+ br = au_sbr(sb, bindex);
35443+ h_path.mnt = au_br_mnt(br);
35444+ wbr = br->br_wbr;
35445+ wbr_wh_read_lock(wbr);
35446+ if (wbr->wbr_whbase) {
35447+ delegated = NULL;
35448+ err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
35449+ if (unlikely(err == -EWOULDBLOCK)) {
35450+ pr_warn("cannot retry for NFSv4 delegation"
35451+ " for an internal link\n");
35452+ iput(delegated);
35453+ }
35454+ if (!err || err != -EMLINK)
35455+ goto out;
35456+
35457+ /* link count full. re-initialize br_whbase. */
35458+ kick_reinit_br_wh(sb, br);
35459+ }
35460+
35461+ /* return this error in this context */
35462+ err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
35463+ if (!err)
35464+ au_fhsm_wrote(sb, bindex, /*force*/0);
35465+
35466+out:
35467+ wbr_wh_read_unlock(wbr);
35468+ return err;
35469+}
35470+
35471+/* ---------------------------------------------------------------------- */
35472+
35473+/*
35474+ * create or remove the diropq.
35475+ */
35476+static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
35477+ unsigned int flags)
35478+{
35479+ struct dentry *opq_dentry;
35480+ struct super_block *sb;
35481+ struct au_branch *br;
35482+ struct path h_path;
35483+ int err;
35484+
35485+ sb = dentry->d_sb;
35486+ br = au_sbr(sb, bindex);
35487+ h_path.dentry = au_h_dptr(dentry, bindex);
35488+ h_path.mnt = au_br_mnt(br);
35489+ opq_dentry = vfsub_lkup_one(&diropq_name, &h_path);
35490+ if (IS_ERR(opq_dentry))
35491+ goto out;
35492+
35493+ if (au_ftest_diropq(flags, CREATE)) {
35494+ err = link_or_create_wh(sb, bindex, opq_dentry);
35495+ if (!err) {
35496+ au_set_dbdiropq(dentry, bindex);
35497+ goto out; /* success */
35498+ }
35499+ } else {
35500+ h_path.dentry = opq_dentry;
35501+ err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &h_path);
35502+ if (!err)
35503+ au_set_dbdiropq(dentry, -1);
35504+ }
35505+ dput(opq_dentry);
35506+ opq_dentry = ERR_PTR(err);
35507+
35508+out:
35509+ return opq_dentry;
35510+}
35511+
35512+struct do_diropq_args {
35513+ struct dentry **errp;
35514+ struct dentry *dentry;
35515+ aufs_bindex_t bindex;
35516+ unsigned int flags;
35517+};
35518+
35519+static void call_do_diropq(void *args)
35520+{
35521+ struct do_diropq_args *a = args;
35522+ *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
35523+}
35524+
35525+struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35526+ unsigned int flags)
35527+{
35528+ struct dentry *diropq, *h_dentry;
35529+ struct user_namespace *h_userns;
35530+
35531+ h_userns = au_sbr_userns(dentry->d_sb, bindex);
35532+ h_dentry = au_h_dptr(dentry, bindex);
35533+ if (!au_test_h_perm_sio(h_userns, d_inode(h_dentry),
35534+ MAY_EXEC | MAY_WRITE))
35535+ diropq = do_diropq(dentry, bindex, flags);
35536+ else {
35537+ int wkq_err;
35538+ struct do_diropq_args args = {
35539+ .errp = &diropq,
35540+ .dentry = dentry,
35541+ .bindex = bindex,
35542+ .flags = flags
35543+ };
35544+
35545+ wkq_err = au_wkq_wait(call_do_diropq, &args);
35546+ if (unlikely(wkq_err))
35547+ diropq = ERR_PTR(wkq_err);
35548+ }
35549+
35550+ return diropq;
35551+}
35552+
35553+/* ---------------------------------------------------------------------- */
35554+
35555+/*
35556+ * lookup whiteout dentry.
35557+ * @h_parent: lower parent dentry which must exist and be locked
35558+ * @base_name: name of dentry which will be whiteouted
35559+ * returns dentry for whiteout.
35560+ */
35561+struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35562+ struct au_branch *br)
35563+{
35564+ int err;
35565+ struct qstr wh_name;
35566+ struct dentry *wh_dentry;
35567+ struct path h_path;
35568+
35569+ err = au_wh_name_alloc(&wh_name, base_name);
35570+ wh_dentry = ERR_PTR(err);
35571+ if (!err) {
35572+ h_path.dentry = h_parent;
35573+ h_path.mnt = au_br_mnt(br);
35574+ wh_dentry = vfsub_lkup_one(&wh_name, &h_path);
35575+ au_kfree_try_rcu(wh_name.name);
35576+ }
35577+ return wh_dentry;
35578+}
35579+
35580+/*
35581+ * link/create a whiteout for @dentry on @bindex.
35582+ */
35583+struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35584+ struct dentry *h_parent)
35585+{
35586+ struct dentry *wh_dentry;
35587+ struct super_block *sb;
35588+ int err;
35589+
35590+ sb = dentry->d_sb;
35591+ wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
35592+ if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
35593+ err = link_or_create_wh(sb, bindex, wh_dentry);
35594+ if (!err) {
35595+ au_set_dbwh(dentry, bindex);
35596+ au_fhsm_wrote(sb, bindex, /*force*/0);
35597+ } else {
35598+ dput(wh_dentry);
35599+ wh_dentry = ERR_PTR(err);
35600+ }
35601+ }
35602+
35603+ return wh_dentry;
35604+}
35605+
35606+/* ---------------------------------------------------------------------- */
35607+
35608+/* Delete all whiteouts in this directory on branch bindex. */
35609+static int del_wh_children(struct path *h_path, struct au_nhash *whlist,
35610+ aufs_bindex_t bindex)
35611+{
35612+ int err;
35613+ unsigned long ul, n;
35614+ struct qstr wh_name;
35615+ char *p;
35616+ struct hlist_head *head;
35617+ struct au_vdir_wh *pos;
35618+ struct au_vdir_destr *str;
35619+
35620+ err = -ENOMEM;
35621+ p = (void *)__get_free_page(GFP_NOFS);
35622+ wh_name.name = p;
35623+ if (unlikely(!wh_name.name))
35624+ goto out;
35625+
35626+ err = 0;
35627+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35628+ p += AUFS_WH_PFX_LEN;
35629+ n = whlist->nh_num;
35630+ head = whlist->nh_head;
35631+ for (ul = 0; !err && ul < n; ul++, head++) {
35632+ hlist_for_each_entry(pos, head, wh_hash) {
35633+ if (pos->wh_bindex != bindex)
35634+ continue;
35635+
35636+ str = &pos->wh_str;
35637+ if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
35638+ memcpy(p, str->name, str->len);
35639+ wh_name.len = AUFS_WH_PFX_LEN + str->len;
35640+ err = unlink_wh_name(h_path, &wh_name);
35641+ if (!err)
35642+ continue;
35643+ break;
35644+ }
35645+ AuIOErr("whiteout name too long %.*s\n",
35646+ str->len, str->name);
35647+ err = -EIO;
35648+ break;
35649+ }
35650+ }
35651+ free_page((unsigned long)wh_name.name);
35652+
35653+out:
35654+ return err;
35655+}
35656+
35657+struct del_wh_children_args {
35658+ int *errp;
35659+ struct path *h_path;
35660+ struct au_nhash *whlist;
35661+ aufs_bindex_t bindex;
35662+};
35663+
35664+static void call_del_wh_children(void *args)
35665+{
35666+ struct del_wh_children_args *a = args;
35667+ *a->errp = del_wh_children(a->h_path, a->whlist, a->bindex);
35668+}
35669+
35670+/* ---------------------------------------------------------------------- */
35671+
35672+struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
35673+{
35674+ struct au_whtmp_rmdir *whtmp;
35675+ int err;
35676+ unsigned int rdhash;
35677+
35678+ SiMustAnyLock(sb);
35679+
35680+ whtmp = kzalloc(sizeof(*whtmp), gfp);
35681+ if (unlikely(!whtmp)) {
35682+ whtmp = ERR_PTR(-ENOMEM);
35683+ goto out;
35684+ }
35685+
35686+ /* no estimation for dir size */
35687+ rdhash = au_sbi(sb)->si_rdhash;
35688+ if (!rdhash)
35689+ rdhash = AUFS_RDHASH_DEF;
35690+ err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
35691+ if (unlikely(err)) {
35692+ au_kfree_rcu(whtmp);
35693+ whtmp = ERR_PTR(err);
35694+ }
35695+
35696+out:
35697+ return whtmp;
35698+}
35699+
35700+void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
35701+{
35702+ if (whtmp->br)
35703+ au_lcnt_dec(&whtmp->br->br_count);
35704+ dput(whtmp->wh_dentry);
35705+ iput(whtmp->dir);
35706+ au_nhash_wh_free(&whtmp->whlist);
35707+ au_kfree_rcu(whtmp);
35708+}
35709+
35710+/*
35711+ * rmdir the whiteouted temporary named dir @h_dentry.
35712+ * @whlist: whiteouted children.
35713+ */
35714+int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35715+ struct dentry *wh_dentry, struct au_nhash *whlist)
35716+{
35717+ int err;
35718+ unsigned int h_nlink;
35719+ struct path wh_path;
35720+ struct inode *wh_inode, *h_dir;
35721+ struct au_branch *br;
35722+ struct user_namespace *h_userns;
35723+
35724+ h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
35725+ IMustLock(h_dir);
35726+
35727+ br = au_sbr(dir->i_sb, bindex);
35728+ wh_path.dentry = wh_dentry;
35729+ wh_path.mnt = au_br_mnt(br);
35730+ h_userns = au_br_userns(br);
35731+ wh_inode = d_inode(wh_dentry);
35732+ inode_lock_nested(wh_inode, AuLsc_I_CHILD);
35733+
35734+ /*
35735+ * someone else might change some whiteouts while we were sleeping.
35736+ * it means this whlist may have an obsoleted entry.
35737+ */
35738+ if (!au_test_h_perm_sio(h_userns, wh_inode, MAY_EXEC | MAY_WRITE))
35739+ err = del_wh_children(&wh_path, whlist, bindex);
35740+ else {
35741+ int wkq_err;
35742+ struct del_wh_children_args args = {
35743+ .errp = &err,
35744+ .h_path = &wh_path,
35745+ .whlist = whlist,
35746+ .bindex = bindex
35747+ };
35748+
35749+ wkq_err = au_wkq_wait(call_del_wh_children, &args);
35750+ if (unlikely(wkq_err))
35751+ err = wkq_err;
35752+ }
35753+ inode_unlock(wh_inode);
35754+
35755+ if (!err) {
35756+ h_nlink = h_dir->i_nlink;
35757+ err = vfsub_rmdir(h_dir, &wh_path);
35758+ /* some fs doesn't change the parent nlink in some cases */
35759+ h_nlink -= h_dir->i_nlink;
35760+ }
35761+
35762+ if (!err) {
35763+ if (au_ibtop(dir) == bindex) {
35764+ /* todo: dir->i_mutex is necessary */
35765+ au_cpup_attr_timesizes(dir);
35766+ if (h_nlink)
35767+ vfsub_drop_nlink(dir);
35768+ }
35769+ return 0; /* success */
35770+ }
35771+
35772+ pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
35773+ return err;
35774+}
35775+
35776+static void call_rmdir_whtmp(void *args)
35777+{
35778+ int err;
35779+ aufs_bindex_t bindex;
35780+ struct au_whtmp_rmdir *a = args;
35781+ struct super_block *sb;
35782+ struct dentry *h_parent;
35783+ struct inode *h_dir;
35784+ struct au_hinode *hdir;
35785+
35786+ /* rmdir by nfsd may cause deadlock with this i_mutex */
35787+ /* inode_lock(a->dir); */
35788+ err = -EROFS;
35789+ sb = a->dir->i_sb;
35790+ si_read_lock(sb, !AuLock_FLUSH);
35791+ if (!au_br_writable(a->br->br_perm))
35792+ goto out;
35793+ bindex = au_br_index(sb, a->br->br_id);
35794+ if (unlikely(bindex < 0))
35795+ goto out;
35796+
35797+ err = -EIO;
35798+ ii_write_lock_parent(a->dir);
35799+ h_parent = dget_parent(a->wh_dentry);
35800+ h_dir = d_inode(h_parent);
35801+ hdir = au_hi(a->dir, bindex);
35802+ err = vfsub_mnt_want_write(au_br_mnt(a->br));
35803+ if (unlikely(err))
35804+ goto out_mnt;
35805+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
35806+ err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
35807+ a->br);
35808+ if (!err)
35809+ err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
35810+ au_hn_inode_unlock(hdir);
35811+ vfsub_mnt_drop_write(au_br_mnt(a->br));
35812+
35813+out_mnt:
35814+ dput(h_parent);
35815+ ii_write_unlock(a->dir);
35816+out:
35817+ /* inode_unlock(a->dir); */
35818+ au_whtmp_rmdir_free(a);
35819+ si_read_unlock(sb);
35820+ au_nwt_done(&au_sbi(sb)->si_nowait);
35821+ if (unlikely(err))
35822+ AuIOErr("err %d\n", err);
35823+}
35824+
35825+void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35826+ struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
35827+{
35828+ int wkq_err;
35829+ struct super_block *sb;
35830+
35831+ IMustLock(dir);
35832+
35833+ /* all post-process will be done in do_rmdir_whtmp(). */
35834+ sb = dir->i_sb;
35835+ args->dir = au_igrab(dir);
35836+ args->br = au_sbr(sb, bindex);
35837+ au_lcnt_inc(&args->br->br_count);
35838+ args->wh_dentry = dget(wh_dentry);
35839+ wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
35840+ if (unlikely(wkq_err)) {
35841+ pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
35842+ au_whtmp_rmdir_free(args);
35843+ }
35844+}
35845diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
35846--- /usr/share/empty/fs/aufs/whout.h 1970-01-01 01:00:00.000000000 +0100
35847+++ linux/fs/aufs/whout.h 2021-12-03 15:40:58.236647297 +0100
35848@@ -0,0 +1,87 @@
35849+/* SPDX-License-Identifier: GPL-2.0 */
35850+/*
35851+ * Copyright (C) 2005-2021 Junjiro R. Okajima
35852+ *
35853+ * This program, aufs is free software; you can redistribute it and/or modify
35854+ * it under the terms of the GNU General Public License as published by
35855+ * the Free Software Foundation; either version 2 of the License, or
35856+ * (at your option) any later version.
35857+ *
35858+ * This program is distributed in the hope that it will be useful,
35859+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
35860+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35861+ * GNU General Public License for more details.
35862+ *
35863+ * You should have received a copy of the GNU General Public License
35864+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
35865+ */
35866+
35867+/*
35868+ * whiteout for logical deletion and opaque directory
35869+ */
35870+
35871+#ifndef __AUFS_WHOUT_H__
35872+#define __AUFS_WHOUT_H__
35873+
35874+#ifdef __KERNEL__
35875+
35876+#include "dir.h"
35877+
35878+/* whout.c */
35879+int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
35880+int au_wh_test(struct user_namespace *h_userns, struct path *h_ppath,
35881+ struct qstr *wh_name, int try_sio);
35882+int au_diropq_test(struct user_namespace *h_userns, struct path *h_path);
35883+struct au_branch;
35884+struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
35885+ struct qstr *prefix);
35886+int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
35887+int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35888+ struct dentry *dentry);
35889+int au_wh_init(struct au_branch *br, struct super_block *sb);
35890+
35891+/* diropq flags */
35892+#define AuDiropq_CREATE 1
35893+#define au_ftest_diropq(flags, name) ((flags) & AuDiropq_##name)
35894+#define au_fset_diropq(flags, name) \
35895+ do { (flags) |= AuDiropq_##name; } while (0)
35896+#define au_fclr_diropq(flags, name) \
35897+ do { (flags) &= ~AuDiropq_##name; } while (0)
35898+
35899+struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35900+ unsigned int flags);
35901+struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35902+ struct au_branch *br);
35903+struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35904+ struct dentry *h_parent);
35905+
35906+/* real rmdir for the whiteout-ed dir */
35907+struct au_whtmp_rmdir {
35908+ struct inode *dir;
35909+ struct au_branch *br;
35910+ struct dentry *wh_dentry;
35911+ struct au_nhash whlist;
35912+};
35913+
35914+struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
35915+void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
35916+int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35917+ struct dentry *wh_dentry, struct au_nhash *whlist);
35918+void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35919+ struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
35920+
35921+/* ---------------------------------------------------------------------- */
35922+
35923+static inline struct dentry *au_diropq_create(struct dentry *dentry,
35924+ aufs_bindex_t bindex)
35925+{
35926+ return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
35927+}
35928+
35929+static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
35930+{
35931+ return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
35932+}
35933+
35934+#endif /* __KERNEL__ */
35935+#endif /* __AUFS_WHOUT_H__ */
35936diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
35937--- /usr/share/empty/fs/aufs/wkq.c 1970-01-01 01:00:00.000000000 +0100
35938+++ linux/fs/aufs/wkq.c 2021-12-03 15:38:59.943313977 +0100
35939@@ -0,0 +1,372 @@
35940+// SPDX-License-Identifier: GPL-2.0
35941+/*
35942+ * Copyright (C) 2005-2021 Junjiro R. Okajima
35943+ *
35944+ * This program, aufs is free software; you can redistribute it and/or modify
35945+ * it under the terms of the GNU General Public License as published by
35946+ * the Free Software Foundation; either version 2 of the License, or
35947+ * (at your option) any later version.
35948+ *
35949+ * This program is distributed in the hope that it will be useful,
35950+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
35951+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35952+ * GNU General Public License for more details.
35953+ *
35954+ * You should have received a copy of the GNU General Public License
35955+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
35956+ */
35957+
35958+/*
35959+ * workqueue for asynchronous/super-io operations
35960+ * todo: try new credential scheme
35961+ */
35962+
35963+#include <linux/module.h>
35964+#include "aufs.h"
35965+
35966+/* internal workqueue named AUFS_WKQ_NAME */
35967+
35968+static struct workqueue_struct *au_wkq;
35969+
35970+struct au_wkinfo {
35971+ struct work_struct wk;
35972+ struct kobject *kobj;
35973+
35974+ unsigned int flags; /* see wkq.h */
35975+
35976+ au_wkq_func_t func;
35977+ void *args;
35978+
35979+#ifdef CONFIG_LOCKDEP
35980+ int dont_check;
35981+ struct held_lock **hlock;
35982+#endif
35983+
35984+ struct completion *comp;
35985+};
35986+
35987+/* ---------------------------------------------------------------------- */
35988+/*
35989+ * Aufs passes some operations to the workqueue such as the internal copyup.
35990+ * This scheme looks rather unnatural for LOCKDEP debugging feature, since the
35991+ * job run by workqueue depends upon the locks acquired in the other task.
35992+ * Delegating a small operation to the workqueue, aufs passes its lockdep
35993+ * information too. And the job in the workqueue restores the info in order to
35994+ * pretend as if it acquired those locks. This is just to make LOCKDEP work
35995+ * correctly and expectedly.
35996+ */
35997+
35998+#ifndef CONFIG_LOCKDEP
35999+AuStubInt0(au_wkq_lockdep_alloc, struct au_wkinfo *wkinfo);
36000+AuStubVoid(au_wkq_lockdep_free, struct au_wkinfo *wkinfo);
36001+AuStubVoid(au_wkq_lockdep_pre, struct au_wkinfo *wkinfo);
36002+AuStubVoid(au_wkq_lockdep_post, struct au_wkinfo *wkinfo);
36003+AuStubVoid(au_wkq_lockdep_init, struct au_wkinfo *wkinfo);
36004+#else
36005+static void au_wkq_lockdep_init(struct au_wkinfo *wkinfo)
36006+{
36007+ wkinfo->hlock = NULL;
36008+ wkinfo->dont_check = 0;
36009+}
36010+
36011+/*
36012+ * 1: matched
36013+ * 0: unmatched
36014+ */
36015+static int au_wkq_lockdep_test(struct lock_class_key *key, const char *name)
36016+{
36017+ static DEFINE_SPINLOCK(spin);
36018+ static struct {
36019+ char *name;
36020+ struct lock_class_key *key;
36021+ } a[] = {
36022+ { .name = "&sbinfo->si_rwsem" },
36023+ { .name = "&finfo->fi_rwsem" },
36024+ { .name = "&dinfo->di_rwsem" },
36025+ { .name = "&iinfo->ii_rwsem" }
36026+ };
36027+ static int set;
36028+ int i;
36029+
36030+ /* lockless read from 'set.' see below */
36031+ if (set == ARRAY_SIZE(a)) {
36032+ for (i = 0; i < ARRAY_SIZE(a); i++)
36033+ if (a[i].key == key)
36034+ goto match;
36035+ goto unmatch;
36036+ }
36037+
36038+ spin_lock(&spin);
36039+ if (set)
36040+ for (i = 0; i < ARRAY_SIZE(a); i++)
36041+ if (a[i].key == key) {
36042+ spin_unlock(&spin);
36043+ goto match;
36044+ }
36045+ for (i = 0; i < ARRAY_SIZE(a); i++) {
36046+ if (a[i].key) {
36047+ if (unlikely(a[i].key == key)) { /* rare but possible */
36048+ spin_unlock(&spin);
36049+ goto match;
36050+ } else
36051+ continue;
36052+ }
36053+ if (strstr(a[i].name, name)) {
36054+ /*
36055+ * the order of these three lines is important for the
36056+ * lockless read above.
36057+ */
36058+ a[i].key = key;
36059+ spin_unlock(&spin);
36060+ set++;
36061+ /* AuDbg("%d, %s\n", set, name); */
36062+ goto match;
36063+ }
36064+ }
36065+ spin_unlock(&spin);
36066+ goto unmatch;
36067+
36068+match:
36069+ return 1;
36070+unmatch:
36071+ return 0;
36072+}
36073+
36074+static int au_wkq_lockdep_alloc(struct au_wkinfo *wkinfo)
36075+{
36076+ int err, n;
36077+ struct task_struct *curr;
36078+ struct held_lock **hl, *held_locks, *p;
36079+
36080+ err = 0;
36081+ curr = current;
36082+ wkinfo->dont_check = lockdep_recursing(curr);
36083+ if (wkinfo->dont_check)
36084+ goto out;
36085+ n = curr->lockdep_depth;
36086+ if (!n)
36087+ goto out;
36088+
36089+ err = -ENOMEM;
36090+ wkinfo->hlock = kmalloc_array(n + 1, sizeof(*wkinfo->hlock), GFP_NOFS);
36091+ if (unlikely(!wkinfo->hlock))
36092+ goto out;
36093+
36094+ err = 0;
36095+#if 0 /* left for debugging */
36096+ if (0 && au_debug_test())
36097+ lockdep_print_held_locks(curr);
36098+#endif
36099+ held_locks = curr->held_locks;
36100+ hl = wkinfo->hlock;
36101+ while (n--) {
36102+ p = held_locks++;
36103+ if (au_wkq_lockdep_test(p->instance->key, p->instance->name))
36104+ *hl++ = p;
36105+ }
36106+ *hl = NULL;
36107+
36108+out:
36109+ return err;
36110+}
36111+
36112+static void au_wkq_lockdep_free(struct au_wkinfo *wkinfo)
36113+{
36114+ au_kfree_try_rcu(wkinfo->hlock);
36115+}
36116+
36117+static void au_wkq_lockdep_pre(struct au_wkinfo *wkinfo)
36118+{
36119+ struct held_lock *p, **hl = wkinfo->hlock;
36120+ int subclass;
36121+
36122+ if (wkinfo->dont_check)
36123+ lockdep_off();
36124+ if (!hl)
36125+ return;
36126+ while ((p = *hl++)) { /* assignment */
36127+ subclass = lockdep_hlock_class(p)->subclass;
36128+ /* AuDbg("%s, %d\n", p->instance->name, subclass); */
36129+ if (p->read)
36130+ rwsem_acquire_read(p->instance, subclass, 0,
36131+ /*p->acquire_ip*/_RET_IP_);
36132+ else
36133+ rwsem_acquire(p->instance, subclass, 0,
36134+ /*p->acquire_ip*/_RET_IP_);
36135+ }
36136+}
36137+
36138+static void au_wkq_lockdep_post(struct au_wkinfo *wkinfo)
36139+{
36140+ struct held_lock *p, **hl = wkinfo->hlock;
36141+
36142+ if (wkinfo->dont_check)
36143+ lockdep_on();
36144+ if (!hl)
36145+ return;
36146+ while ((p = *hl++)) /* assignment */
36147+ rwsem_release(p->instance, /*p->acquire_ip*/_RET_IP_);
36148+}
36149+#endif
36150+
36151+static void wkq_func(struct work_struct *wk)
36152+{
36153+ struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
36154+
36155+ AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
36156+ AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
36157+
36158+ au_wkq_lockdep_pre(wkinfo);
36159+ wkinfo->func(wkinfo->args);
36160+ au_wkq_lockdep_post(wkinfo);
36161+ if (au_ftest_wkq(wkinfo->flags, WAIT))
36162+ complete(wkinfo->comp);
36163+ else {
36164+ kobject_put(wkinfo->kobj);
36165+ module_put(THIS_MODULE); /* todo: ?? */
36166+ au_kfree_rcu(wkinfo);
36167+ }
36168+}
36169+
36170+/*
36171+ * Since struct completion is large, try allocating it dynamically.
36172+ */
36173+#define AuWkqCompDeclare(name) struct completion *comp = NULL
36174+
36175+static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
36176+{
36177+ *comp = kmalloc(sizeof(**comp), GFP_NOFS);
36178+ if (*comp) {
36179+ init_completion(*comp);
36180+ wkinfo->comp = *comp;
36181+ return 0;
36182+ }
36183+ return -ENOMEM;
36184+}
36185+
36186+static void au_wkq_comp_free(struct completion *comp)
36187+{
36188+ au_kfree_rcu(comp);
36189+}
36190+
36191+static void au_wkq_run(struct au_wkinfo *wkinfo)
36192+{
36193+ if (au_ftest_wkq(wkinfo->flags, NEST)) {
36194+ if (au_wkq_test()) {
36195+ AuWarn1("wkq from wkq, unless silly-rename on NFS,"
36196+ " due to a dead dir by UDBA,"
36197+ " or async xino write?\n");
36198+ AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
36199+ }
36200+ } else
36201+ au_dbg_verify_kthread();
36202+
36203+ if (au_ftest_wkq(wkinfo->flags, WAIT)) {
36204+ INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
36205+ queue_work(au_wkq, &wkinfo->wk);
36206+ } else {
36207+ INIT_WORK(&wkinfo->wk, wkq_func);
36208+ schedule_work(&wkinfo->wk);
36209+ }
36210+}
36211+
36212+/*
36213+ * Be careful. It is easy to make deadlock happen.
36214+ * processA: lock, wkq and wait
36215+ * processB: wkq and wait, lock in wkq
36216+ * --> deadlock
36217+ */
36218+int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
36219+{
36220+ int err;
36221+ AuWkqCompDeclare(comp);
36222+ struct au_wkinfo wkinfo = {
36223+ .flags = flags,
36224+ .func = func,
36225+ .args = args
36226+ };
36227+
36228+ err = au_wkq_comp_alloc(&wkinfo, &comp);
36229+ if (unlikely(err))
36230+ goto out;
36231+ err = au_wkq_lockdep_alloc(&wkinfo);
36232+ if (unlikely(err))
36233+ goto out_comp;
36234+ if (!err) {
36235+ au_wkq_run(&wkinfo);
36236+ /* no timeout, no interrupt */
36237+ wait_for_completion(wkinfo.comp);
36238+ }
36239+ au_wkq_lockdep_free(&wkinfo);
36240+
36241+out_comp:
36242+ au_wkq_comp_free(comp);
36243+out:
36244+ destroy_work_on_stack(&wkinfo.wk);
36245+ return err;
36246+}
36247+
36248+/*
36249+ * Note: dget/dput() in func for aufs dentries are not supported. It will be a
36250+ * problem in a concurrent umounting.
36251+ */
36252+int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36253+ unsigned int flags)
36254+{
36255+ int err;
36256+ struct au_wkinfo *wkinfo;
36257+
36258+ atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
36259+
36260+ /*
36261+ * wkq_func() must free this wkinfo.
36262+ * it highly depends upon the implementation of workqueue.
36263+ */
36264+ err = 0;
36265+ wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
36266+ if (wkinfo) {
36267+ wkinfo->kobj = &au_sbi(sb)->si_kobj;
36268+ wkinfo->flags = flags & ~AuWkq_WAIT;
36269+ wkinfo->func = func;
36270+ wkinfo->args = args;
36271+ wkinfo->comp = NULL;
36272+ au_wkq_lockdep_init(wkinfo);
36273+ kobject_get(wkinfo->kobj);
36274+ __module_get(THIS_MODULE); /* todo: ?? */
36275+
36276+ au_wkq_run(wkinfo);
36277+ } else {
36278+ err = -ENOMEM;
36279+ au_nwt_done(&au_sbi(sb)->si_nowait);
36280+ }
36281+
36282+ return err;
36283+}
36284+
36285+/* ---------------------------------------------------------------------- */
36286+
36287+void au_nwt_init(struct au_nowait_tasks *nwt)
36288+{
36289+ atomic_set(&nwt->nw_len, 0);
36290+ /* smp_mb(); */ /* atomic_set */
36291+ init_waitqueue_head(&nwt->nw_wq);
36292+}
36293+
36294+void au_wkq_fin(void)
36295+{
36296+ destroy_workqueue(au_wkq);
36297+}
36298+
36299+int __init au_wkq_init(void)
36300+{
36301+ int err;
36302+
36303+ err = 0;
36304+ au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
36305+ if (IS_ERR(au_wkq))
36306+ err = PTR_ERR(au_wkq);
36307+ else if (!au_wkq)
36308+ err = -ENOMEM;
36309+
36310+ return err;
36311+}
36312diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
36313--- /usr/share/empty/fs/aufs/wkq.h 1970-01-01 01:00:00.000000000 +0100
36314+++ linux/fs/aufs/wkq.h 2021-12-03 15:38:59.943313977 +0100
36315@@ -0,0 +1,89 @@
36316+/* SPDX-License-Identifier: GPL-2.0 */
36317+/*
36318+ * Copyright (C) 2005-2021 Junjiro R. Okajima
36319+ *
36320+ * This program, aufs is free software; you can redistribute it and/or modify
36321+ * it under the terms of the GNU General Public License as published by
36322+ * the Free Software Foundation; either version 2 of the License, or
36323+ * (at your option) any later version.
36324+ *
36325+ * This program is distributed in the hope that it will be useful,
36326+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36327+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36328+ * GNU General Public License for more details.
36329+ *
36330+ * You should have received a copy of the GNU General Public License
36331+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
36332+ */
36333+
36334+/*
36335+ * workqueue for asynchronous/super-io operations
36336+ * todo: try new credentials management scheme
36337+ */
36338+
36339+#ifndef __AUFS_WKQ_H__
36340+#define __AUFS_WKQ_H__
36341+
36342+#ifdef __KERNEL__
36343+
36344+#include <linux/wait.h>
36345+
36346+struct super_block;
36347+
36348+/* ---------------------------------------------------------------------- */
36349+
36350+/*
36351+ * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
36352+ */
36353+struct au_nowait_tasks {
36354+ atomic_t nw_len;
36355+ wait_queue_head_t nw_wq;
36356+};
36357+
36358+/* ---------------------------------------------------------------------- */
36359+
36360+typedef void (*au_wkq_func_t)(void *args);
36361+
36362+/* wkq flags */
36363+#define AuWkq_WAIT 1
36364+#define AuWkq_NEST (1 << 1)
36365+#define au_ftest_wkq(flags, name) ((flags) & AuWkq_##name)
36366+#define au_fset_wkq(flags, name) \
36367+ do { (flags) |= AuWkq_##name; } while (0)
36368+#define au_fclr_wkq(flags, name) \
36369+ do { (flags) &= ~AuWkq_##name; } while (0)
36370+
36371+/* wkq.c */
36372+int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
36373+int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36374+ unsigned int flags);
36375+void au_nwt_init(struct au_nowait_tasks *nwt);
36376+int __init au_wkq_init(void);
36377+void au_wkq_fin(void);
36378+
36379+/* ---------------------------------------------------------------------- */
36380+
36381+static inline int au_wkq_test(void)
36382+{
36383+ return current->flags & PF_WQ_WORKER;
36384+}
36385+
36386+static inline int au_wkq_wait(au_wkq_func_t func, void *args)
36387+{
36388+ return au_wkq_do_wait(AuWkq_WAIT, func, args);
36389+}
36390+
36391+static inline void au_nwt_done(struct au_nowait_tasks *nwt)
36392+{
36393+ if (atomic_dec_and_test(&nwt->nw_len))
36394+ wake_up_all(&nwt->nw_wq);
36395+}
36396+
36397+static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
36398+{
36399+ wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
36400+ return 0;
36401+}
36402+
36403+#endif /* __KERNEL__ */
36404+#endif /* __AUFS_WKQ_H__ */
36405diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
36406--- /usr/share/empty/fs/aufs/xattr.c 1970-01-01 01:00:00.000000000 +0100
36407+++ linux/fs/aufs/xattr.c 2021-12-03 15:40:58.236647297 +0100
36408@@ -0,0 +1,368 @@
36409+// SPDX-License-Identifier: GPL-2.0
36410+/*
36411+ * Copyright (C) 2014-2021 Junjiro R. Okajima
36412+ *
36413+ * This program, aufs is free software; you can redistribute it and/or modify
36414+ * it under the terms of the GNU General Public License as published by
36415+ * the Free Software Foundation; either version 2 of the License, or
36416+ * (at your option) any later version.
36417+ *
36418+ * This program is distributed in the hope that it will be useful,
36419+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36420+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36421+ * GNU General Public License for more details.
36422+ *
36423+ * You should have received a copy of the GNU General Public License
36424+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
36425+ */
36426+
36427+/*
36428+ * handling xattr functions
36429+ */
36430+
36431+#include <linux/fs.h>
36432+#include <linux/posix_acl_xattr.h>
36433+#include <linux/xattr.h>
36434+#include "aufs.h"
36435+
36436+static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
36437+{
36438+ if (!ignore_flags)
36439+ goto out;
36440+ switch (err) {
36441+ case -ENOMEM:
36442+ case -EDQUOT:
36443+ goto out;
36444+ }
36445+
36446+ if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
36447+ err = 0;
36448+ goto out;
36449+ }
36450+
36451+#define cmp(brattr, prefix) do { \
36452+ if (!strncmp(name, XATTR_##prefix##_PREFIX, \
36453+ XATTR_##prefix##_PREFIX_LEN)) { \
36454+ if (ignore_flags & AuBrAttr_ICEX_##brattr) \
36455+ err = 0; \
36456+ goto out; \
36457+ } \
36458+ } while (0)
36459+
36460+ cmp(SEC, SECURITY);
36461+ cmp(SYS, SYSTEM);
36462+ cmp(TR, TRUSTED);
36463+ cmp(USR, USER);
36464+#undef cmp
36465+
36466+ if (ignore_flags & AuBrAttr_ICEX_OTH)
36467+ err = 0;
36468+
36469+out:
36470+ return err;
36471+}
36472+
36473+static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
36474+
36475+static int au_do_cpup_xattr(struct path *h_dst, struct path *h_src,
36476+ char *name, char **buf, unsigned int ignore_flags,
36477+ unsigned int verbose)
36478+{
36479+ int err;
36480+ ssize_t ssz;
36481+ struct inode *h_idst;
36482+ struct dentry *h_dst_dentry, *h_src_dentry;
36483+ struct user_namespace *h_dst_userns, *h_src_userns;
36484+
36485+ h_src_userns = mnt_user_ns(h_src->mnt);
36486+ h_src_dentry = h_src->dentry;
36487+ ssz = vfs_getxattr_alloc(h_src_userns, h_src_dentry, name, buf, 0,
36488+ GFP_NOFS);
36489+ err = ssz;
36490+ if (unlikely(err <= 0)) {
36491+ if (err == -ENODATA
36492+ || (err == -EOPNOTSUPP
36493+ && ((ignore_flags & au_xattr_out_of_list)
36494+ || (au_test_nfs_noacl(d_inode(h_src_dentry))
36495+ && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
36496+ || !strcmp(name,
36497+ XATTR_NAME_POSIX_ACL_DEFAULT))))
36498+ ))
36499+ err = 0;
36500+ if (err && (verbose || au_debug_test()))
36501+ pr_err("%s, err %d\n", name, err);
36502+ goto out;
36503+ }
36504+
36505+ /* unlock it temporary */
36506+ h_dst_userns = mnt_user_ns(h_dst->mnt);
36507+ h_dst_dentry = h_dst->dentry;
36508+ h_idst = d_inode(h_dst_dentry);
36509+ inode_unlock(h_idst);
36510+ err = vfsub_setxattr(h_dst_userns, h_dst_dentry, name, *buf, ssz,
36511+ /*flags*/0);
36512+ inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36513+ if (unlikely(err)) {
36514+ if (verbose || au_debug_test())
36515+ pr_err("%s, err %d\n", name, err);
36516+ err = au_xattr_ignore(err, name, ignore_flags);
36517+ }
36518+
36519+out:
36520+ return err;
36521+}
36522+
36523+int au_cpup_xattr(struct path *h_dst, struct path *h_src, int ignore_flags,
36524+ unsigned int verbose)
36525+{
36526+ int err, unlocked, acl_access, acl_default;
36527+ ssize_t ssz;
36528+ struct dentry *h_dst_dentry, *h_src_dentry;
36529+ struct inode *h_isrc, *h_idst;
36530+ char *value, *p, *o, *e;
36531+
36532+ /* try stopping to update the source inode while we are referencing */
36533+ /* there should not be the parent-child relationship between them */
36534+ h_dst_dentry = h_dst->dentry;
36535+ h_idst = d_inode(h_dst_dentry);
36536+ h_src_dentry = h_src->dentry;
36537+ h_isrc = d_inode(h_src_dentry);
36538+ inode_unlock(h_idst);
36539+ inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
36540+ inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36541+ unlocked = 0;
36542+
36543+ /* some filesystems don't list POSIX ACL, for example tmpfs */
36544+ ssz = vfs_listxattr(h_src_dentry, NULL, 0);
36545+ err = ssz;
36546+ if (unlikely(err < 0)) {
36547+ AuTraceErr(err);
36548+ if (err == -ENODATA
36549+ || err == -EOPNOTSUPP)
36550+ err = 0; /* ignore */
36551+ goto out;
36552+ }
36553+
36554+ err = 0;
36555+ p = NULL;
36556+ o = NULL;
36557+ if (ssz) {
36558+ err = -ENOMEM;
36559+ p = kmalloc(ssz, GFP_NOFS);
36560+ o = p;
36561+ if (unlikely(!p))
36562+ goto out;
36563+ err = vfs_listxattr(h_src_dentry, p, ssz);
36564+ }
36565+ inode_unlock_shared(h_isrc);
36566+ unlocked = 1;
36567+ AuDbg("err %d, ssz %zd\n", err, ssz);
36568+ if (unlikely(err < 0))
36569+ goto out_free;
36570+
36571+ err = 0;
36572+ e = p + ssz;
36573+ value = NULL;
36574+ acl_access = 0;
36575+ acl_default = 0;
36576+ while (!err && p < e) {
36577+ acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
36578+ sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
36579+ acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
36580+ sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
36581+ - 1);
36582+ err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
36583+ verbose);
36584+ p += strlen(p) + 1;
36585+ }
36586+ AuTraceErr(err);
36587+ ignore_flags |= au_xattr_out_of_list;
36588+ if (!err && !acl_access) {
36589+ err = au_do_cpup_xattr(h_dst, h_src,
36590+ XATTR_NAME_POSIX_ACL_ACCESS, &value,
36591+ ignore_flags, verbose);
36592+ AuTraceErr(err);
36593+ }
36594+ if (!err && !acl_default) {
36595+ err = au_do_cpup_xattr(h_dst, h_src,
36596+ XATTR_NAME_POSIX_ACL_DEFAULT, &value,
36597+ ignore_flags, verbose);
36598+ AuTraceErr(err);
36599+ }
36600+
36601+ au_kfree_try_rcu(value);
36602+
36603+out_free:
36604+ au_kfree_try_rcu(o);
36605+out:
36606+ if (!unlocked)
36607+ inode_unlock_shared(h_isrc);
36608+ AuTraceErr(err);
36609+ return err;
36610+}
36611+
36612+/* ---------------------------------------------------------------------- */
36613+
36614+static int au_smack_reentering(struct super_block *sb)
36615+{
36616+#if IS_ENABLED(CONFIG_SECURITY_SMACK) || IS_ENABLED(CONFIG_SECURITY_SELINUX)
36617+ /*
36618+ * as a part of lookup, smack_d_instantiate() is called, and it calls
36619+ * i_op->getxattr(). ouch.
36620+ */
36621+ return si_pid_test(sb);
36622+#else
36623+ return 0;
36624+#endif
36625+}
36626+
36627+enum {
36628+ AU_XATTR_LIST,
36629+ AU_XATTR_GET
36630+};
36631+
36632+struct au_lgxattr {
36633+ int type;
36634+ union {
36635+ struct {
36636+ char *list;
36637+ size_t size;
36638+ } list;
36639+ struct {
36640+ const char *name;
36641+ void *value;
36642+ size_t size;
36643+ } get;
36644+ } u;
36645+};
36646+
36647+static ssize_t au_lgxattr(struct dentry *dentry, struct inode *inode,
36648+ struct au_lgxattr *arg)
36649+{
36650+ ssize_t err;
36651+ int reenter;
36652+ struct path h_path;
36653+ struct super_block *sb;
36654+
36655+ sb = dentry->d_sb;
36656+ reenter = au_smack_reentering(sb);
36657+ if (!reenter) {
36658+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
36659+ if (unlikely(err))
36660+ goto out;
36661+ }
36662+ err = au_h_path_getattr(dentry, inode, /*force*/1, &h_path, reenter);
36663+ if (unlikely(err))
36664+ goto out_si;
36665+ if (unlikely(!h_path.dentry))
36666+ /* illegally overlapped or something */
36667+ goto out_di; /* pretending success */
36668+
36669+ /* always topmost entry only */
36670+ switch (arg->type) {
36671+ case AU_XATTR_LIST:
36672+ err = vfs_listxattr(h_path.dentry,
36673+ arg->u.list.list, arg->u.list.size);
36674+ break;
36675+ case AU_XATTR_GET:
36676+ AuDebugOn(d_is_negative(h_path.dentry));
36677+ err = vfs_getxattr(mnt_user_ns(h_path.mnt), h_path.dentry,
36678+ arg->u.get.name, arg->u.get.value,
36679+ arg->u.get.size);
36680+ break;
36681+ }
36682+
36683+out_di:
36684+ if (!reenter)
36685+ di_read_unlock(dentry, AuLock_IR);
36686+out_si:
36687+ if (!reenter)
36688+ si_read_unlock(sb);
36689+out:
36690+ AuTraceErr(err);
36691+ return err;
36692+}
36693+
36694+ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
36695+{
36696+ struct au_lgxattr arg = {
36697+ .type = AU_XATTR_LIST,
36698+ .u.list = {
36699+ .list = list,
36700+ .size = size
36701+ },
36702+ };
36703+
36704+ return au_lgxattr(dentry, /*inode*/NULL, &arg);
36705+}
36706+
36707+static ssize_t au_getxattr(struct dentry *dentry, struct inode *inode,
36708+ const char *name, void *value, size_t size)
36709+{
36710+ struct au_lgxattr arg = {
36711+ .type = AU_XATTR_GET,
36712+ .u.get = {
36713+ .name = name,
36714+ .value = value,
36715+ .size = size
36716+ },
36717+ };
36718+
36719+ return au_lgxattr(dentry, inode, &arg);
36720+}
36721+
36722+static int au_setxattr(struct dentry *dentry, struct inode *inode,
36723+ const char *name, const void *value, size_t size,
36724+ int flags)
36725+{
36726+ struct au_sxattr arg = {
36727+ .type = AU_XATTR_SET,
36728+ .u.set = {
36729+ .name = name,
36730+ .value = value,
36731+ .size = size,
36732+ .flags = flags
36733+ },
36734+ };
36735+
36736+ return au_sxattr(dentry, inode, &arg);
36737+}
36738+
36739+/* ---------------------------------------------------------------------- */
36740+
36741+static int au_xattr_get(const struct xattr_handler *handler,
36742+ struct dentry *dentry, struct inode *inode,
36743+ const char *name, void *buffer, size_t size)
36744+{
36745+ return au_getxattr(dentry, inode, name, buffer, size);
36746+}
36747+
36748+static int au_xattr_set(const struct xattr_handler *handler,
36749+ struct user_namespace *userns,
36750+ struct dentry *dentry, struct inode *inode,
36751+ const char *name, const void *value, size_t size,
36752+ int flags)
36753+{
36754+ return au_setxattr(dentry, inode, name, value, size, flags);
36755+}
36756+
36757+static const struct xattr_handler au_xattr_handler = {
36758+ .name = "",
36759+ .prefix = "",
36760+ .get = au_xattr_get,
36761+ .set = au_xattr_set
36762+};
36763+
36764+static const struct xattr_handler *au_xattr_handlers[] = {
36765+#ifdef CONFIG_FS_POSIX_ACL
36766+ &posix_acl_access_xattr_handler,
36767+ &posix_acl_default_xattr_handler,
36768+#endif
36769+ &au_xattr_handler, /* must be last */
36770+ NULL
36771+};
36772+
36773+void au_xattr_init(struct super_block *sb)
36774+{
36775+ sb->s_xattr = au_xattr_handlers;
36776+}
36777diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
36778--- /usr/share/empty/fs/aufs/xino.c 1970-01-01 01:00:00.000000000 +0100
36779+++ linux/fs/aufs/xino.c 2021-12-03 15:40:58.236647297 +0100
36780@@ -0,0 +1,1926 @@
36781+// SPDX-License-Identifier: GPL-2.0
36782+/*
36783+ * Copyright (C) 2005-2021 Junjiro R. Okajima
36784+ *
36785+ * This program, aufs is free software; you can redistribute it and/or modify
36786+ * it under the terms of the GNU General Public License as published by
36787+ * the Free Software Foundation; either version 2 of the License, or
36788+ * (at your option) any later version.
36789+ *
36790+ * This program is distributed in the hope that it will be useful,
36791+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36792+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36793+ * GNU General Public License for more details.
36794+ *
36795+ * You should have received a copy of the GNU General Public License
36796+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
36797+ */
36798+
36799+/*
36800+ * external inode number translation table and bitmap
36801+ *
36802+ * things to consider
36803+ * - the lifetime
36804+ * + au_xino object
36805+ * + XINO files (xino, xib, xigen)
36806+ * + dynamic debugfs entries (xiN)
36807+ * + static debugfs entries (xib, xigen)
36808+ * + static sysfs entry (xi_path)
36809+ * - several entry points to handle them.
36810+ * + mount(2) without xino option (default)
36811+ * + mount(2) with xino option
36812+ * + mount(2) with noxino option
36813+ * + umount(2)
36814+ * + remount with add/del branches
36815+ * + remount with xino/noxino options
36816+ */
36817+
36818+#include <linux/seq_file.h>
36819+#include <linux/statfs.h>
36820+#include "aufs.h"
36821+
36822+static aufs_bindex_t sbr_find_shared(struct super_block *sb, aufs_bindex_t btop,
36823+ aufs_bindex_t bbot,
36824+ struct super_block *h_sb)
36825+{
36826+ /* todo: try binary-search if the branches are many */
36827+ for (; btop <= bbot; btop++)
36828+ if (h_sb == au_sbr_sb(sb, btop))
36829+ return btop;
36830+ return -1;
36831+}
36832+
36833+/*
36834+ * find another branch who is on the same filesystem of the specified
36835+ * branch{@btgt}. search until @bbot.
36836+ */
36837+static aufs_bindex_t is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
36838+ aufs_bindex_t bbot)
36839+{
36840+ aufs_bindex_t bindex;
36841+ struct super_block *tgt_sb;
36842+
36843+ tgt_sb = au_sbr_sb(sb, btgt);
36844+ bindex = sbr_find_shared(sb, /*btop*/0, btgt - 1, tgt_sb);
36845+ if (bindex < 0)
36846+ bindex = sbr_find_shared(sb, btgt + 1, bbot, tgt_sb);
36847+
36848+ return bindex;
36849+}
36850+
36851+/* ---------------------------------------------------------------------- */
36852+
36853+/*
36854+ * stop unnecessary notify events at creating xino files
36855+ */
36856+
36857+aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry)
36858+{
36859+ aufs_bindex_t bfound, bindex, bbot;
36860+ struct dentry *parent;
36861+ struct au_branch *br;
36862+
36863+ bfound = -1;
36864+ parent = dentry->d_parent; /* safe d_parent access */
36865+ bbot = au_sbbot(sb);
36866+ for (bindex = 0; bindex <= bbot; bindex++) {
36867+ br = au_sbr(sb, bindex);
36868+ if (au_br_dentry(br) == parent) {
36869+ bfound = bindex;
36870+ break;
36871+ }
36872+ }
36873+
36874+ AuDbg("bfound b%d\n", bfound);
36875+ return bfound;
36876+}
36877+
36878+struct au_xino_lock_dir {
36879+ struct au_hinode *hdir;
36880+ struct dentry *parent;
36881+ struct inode *dir;
36882+};
36883+
36884+static struct dentry *au_dget_parent_lock(struct dentry *dentry,
36885+ unsigned int lsc)
36886+{
36887+ struct dentry *parent;
36888+ struct inode *dir;
36889+
36890+ parent = dget_parent(dentry);
36891+ dir = d_inode(parent);
36892+ inode_lock_nested(dir, lsc);
36893+#if 0 /* it should not happen */
36894+ spin_lock(&dentry->d_lock);
36895+ if (unlikely(dentry->d_parent != parent)) {
36896+ spin_unlock(&dentry->d_lock);
36897+ inode_unlock(dir);
36898+ dput(parent);
36899+ parent = NULL;
36900+ goto out;
36901+ }
36902+ spin_unlock(&dentry->d_lock);
36903+
36904+out:
36905+#endif
36906+ return parent;
36907+}
36908+
36909+static void au_xino_lock_dir(struct super_block *sb, struct path *xipath,
36910+ struct au_xino_lock_dir *ldir)
36911+{
36912+ aufs_bindex_t bindex;
36913+
36914+ ldir->hdir = NULL;
36915+ bindex = au_xi_root(sb, xipath->dentry);
36916+ if (bindex >= 0) {
36917+ /* rw branch root */
36918+ ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
36919+ au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
36920+ } else {
36921+ /* other */
36922+ ldir->parent = au_dget_parent_lock(xipath->dentry,
36923+ AuLsc_I_PARENT);
36924+ ldir->dir = d_inode(ldir->parent);
36925+ }
36926+}
36927+
36928+static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
36929+{
36930+ if (ldir->hdir)
36931+ au_hn_inode_unlock(ldir->hdir);
36932+ else {
36933+ inode_unlock(ldir->dir);
36934+ dput(ldir->parent);
36935+ }
36936+}
36937+
36938+/* ---------------------------------------------------------------------- */
36939+
36940+/*
36941+ * create and set a new xino file
36942+ */
36943+struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
36944+ int wbrtop)
36945+{
36946+ struct file *file;
36947+ struct dentry *h_parent, *d;
36948+ struct inode *h_dir, *inode;
36949+ int err;
36950+ static DEFINE_MUTEX(mtx);
36951+
36952+ /*
36953+ * at mount-time, and the xino file is the default path,
36954+ * hnotify is disabled so we have no notify events to ignore.
36955+ * when a user specified the xino, we cannot get au_hdir to be ignored.
36956+ */
36957+ if (!wbrtop)
36958+ mutex_lock(&mtx);
36959+ file = vfsub_filp_open(fpath, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
36960+ /* | __FMODE_NONOTIFY */,
36961+ 0666);
36962+ if (IS_ERR(file)) {
36963+ if (!wbrtop)
36964+ mutex_unlock(&mtx);
36965+ if (!silent)
36966+ pr_err("open %s(%ld)\n", fpath, PTR_ERR(file));
36967+ return file;
36968+ }
36969+
36970+ /* keep file count */
36971+ err = 0;
36972+ d = file->f_path.dentry;
36973+ h_parent = au_dget_parent_lock(d, AuLsc_I_PARENT);
36974+ if (!wbrtop)
36975+ mutex_unlock(&mtx);
36976+ /* mnt_want_write() is unnecessary here */
36977+ h_dir = d_inode(h_parent);
36978+ inode = file_inode(file);
36979+ /* no delegation since it is just created */
36980+ if (inode->i_nlink)
36981+ err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
36982+ /*force*/0);
36983+ inode_unlock(h_dir);
36984+ dput(h_parent);
36985+ if (unlikely(err)) {
36986+ if (!silent)
36987+ pr_err("unlink %s(%d)\n", fpath, err);
36988+ goto out;
36989+ }
36990+
36991+ err = -EINVAL;
36992+ if (unlikely(sb == d->d_sb)) {
36993+ if (!silent)
36994+ pr_err("%s must be outside\n", fpath);
36995+ goto out;
36996+ }
36997+ if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
36998+ if (!silent)
36999+ pr_err("xino doesn't support %s(%s)\n",
37000+ fpath, au_sbtype(d->d_sb));
37001+ goto out;
37002+ }
37003+ return file; /* success */
37004+
37005+out:
37006+ fput(file);
37007+ file = ERR_PTR(err);
37008+ return file;
37009+}
37010+
37011+/*
37012+ * create a new xinofile at the same place/path as @base.
37013+ */
37014+struct file *au_xino_create2(struct super_block *sb, struct path *base,
37015+ struct file *copy_src)
37016+{
37017+ struct file *file;
37018+ struct dentry *dentry;
37019+ struct inode *dir, *delegated;
37020+ struct qstr *name;
37021+ struct path ppath, path;
37022+ int err, do_unlock;
37023+ struct au_xino_lock_dir ldir;
37024+
37025+ do_unlock = 1;
37026+ au_xino_lock_dir(sb, base, &ldir);
37027+ dentry = base->dentry;
37028+ ppath.dentry = dentry->d_parent; /* dir inode is locked */
37029+ ppath.mnt = base->mnt;
37030+ dir = d_inode(ppath.dentry);
37031+ IMustLock(dir);
37032+
37033+ name = &dentry->d_name;
37034+ path.dentry = vfsub_lookup_one_len(name->name, &ppath, name->len);
37035+ if (IS_ERR(path.dentry)) {
37036+ file = (void *)path.dentry;
37037+ pr_err("%pd lookup err %ld\n", dentry, PTR_ERR(path.dentry));
37038+ goto out;
37039+ }
37040+
37041+ /* no need to mnt_want_write() since we call dentry_open() later */
37042+ err = vfs_create(mnt_user_ns(base->mnt), dir, path.dentry, 0666, NULL);
37043+ if (unlikely(err)) {
37044+ file = ERR_PTR(err);
37045+ pr_err("%pd create err %d\n", dentry, err);
37046+ goto out_dput;
37047+ }
37048+
37049+ path.mnt = base->mnt;
37050+ file = vfsub_dentry_open(&path,
37051+ O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37052+ /* | __FMODE_NONOTIFY */);
37053+ if (IS_ERR(file)) {
37054+ pr_err("%pd open err %ld\n", dentry, PTR_ERR(file));
37055+ goto out_dput;
37056+ }
37057+
37058+ delegated = NULL;
37059+ err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
37060+ au_xino_unlock_dir(&ldir);
37061+ do_unlock = 0;
37062+ if (unlikely(err == -EWOULDBLOCK)) {
37063+ pr_warn("cannot retry for NFSv4 delegation"
37064+ " for an internal unlink\n");
37065+ iput(delegated);
37066+ }
37067+ if (unlikely(err)) {
37068+ pr_err("%pd unlink err %d\n", dentry, err);
37069+ goto out_fput;
37070+ }
37071+
37072+ if (copy_src) {
37073+ /* no one can touch copy_src xino */
37074+ err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
37075+ if (unlikely(err)) {
37076+ pr_err("%pd copy err %d\n", dentry, err);
37077+ goto out_fput;
37078+ }
37079+ }
37080+ goto out_dput; /* success */
37081+
37082+out_fput:
37083+ fput(file);
37084+ file = ERR_PTR(err);
37085+out_dput:
37086+ dput(path.dentry);
37087+out:
37088+ if (do_unlock)
37089+ au_xino_unlock_dir(&ldir);
37090+ return file;
37091+}
37092+
37093+struct file *au_xino_file1(struct au_xino *xi)
37094+{
37095+ struct file *file;
37096+ unsigned int u, nfile;
37097+
37098+ file = NULL;
37099+ nfile = xi->xi_nfile;
37100+ for (u = 0; u < nfile; u++) {
37101+ file = xi->xi_file[u];
37102+ if (file)
37103+ break;
37104+ }
37105+
37106+ return file;
37107+}
37108+
37109+static int au_xino_file_set(struct au_xino *xi, int idx, struct file *file)
37110+{
37111+ int err;
37112+ struct file *f;
37113+ void *p;
37114+
37115+ if (file)
37116+ get_file(file);
37117+
37118+ err = 0;
37119+ f = NULL;
37120+ if (idx < xi->xi_nfile) {
37121+ f = xi->xi_file[idx];
37122+ if (f)
37123+ fput(f);
37124+ } else {
37125+ p = au_kzrealloc(xi->xi_file,
37126+ sizeof(*xi->xi_file) * xi->xi_nfile,
37127+ sizeof(*xi->xi_file) * (idx + 1),
37128+ GFP_NOFS, /*may_shrink*/0);
37129+ if (p) {
37130+ MtxMustLock(&xi->xi_mtx);
37131+ xi->xi_file = p;
37132+ xi->xi_nfile = idx + 1;
37133+ } else {
37134+ err = -ENOMEM;
37135+ if (file)
37136+ fput(file);
37137+ goto out;
37138+ }
37139+ }
37140+ xi->xi_file[idx] = file;
37141+
37142+out:
37143+ return err;
37144+}
37145+
37146+/*
37147+ * if @xinew->xi is not set, then create new xigen file.
37148+ */
37149+struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew)
37150+{
37151+ struct file *file;
37152+ int err;
37153+
37154+ SiMustAnyLock(sb);
37155+
37156+ file = au_xino_create2(sb, xinew->base, xinew->copy_src);
37157+ if (IS_ERR(file)) {
37158+ err = PTR_ERR(file);
37159+ pr_err("%s[%d], err %d\n",
37160+ xinew->xi ? "xino" : "xigen",
37161+ xinew->idx, err);
37162+ goto out;
37163+ }
37164+
37165+ if (xinew->xi)
37166+ err = au_xino_file_set(xinew->xi, xinew->idx, file);
37167+ else {
37168+ BUG();
37169+ /* todo: make xigen file an array */
37170+ /* err = au_xigen_file_set(sb, xinew->idx, file); */
37171+ }
37172+ fput(file);
37173+ if (unlikely(err))
37174+ file = ERR_PTR(err);
37175+
37176+out:
37177+ return file;
37178+}
37179+
37180+/* ---------------------------------------------------------------------- */
37181+
37182+/*
37183+ * truncate xino files
37184+ */
37185+static int au_xino_do_trunc(struct super_block *sb, aufs_bindex_t bindex,
37186+ int idx, struct kstatfs *st)
37187+{
37188+ int err;
37189+ blkcnt_t blocks;
37190+ struct file *file, *new_xino;
37191+ struct au_xi_new xinew = {
37192+ .idx = idx
37193+ };
37194+
37195+ err = 0;
37196+ xinew.xi = au_sbr(sb, bindex)->br_xino;
37197+ file = au_xino_file(xinew.xi, idx);
37198+ if (!file)
37199+ goto out;
37200+
37201+ xinew.base = &file->f_path;
37202+ err = vfs_statfs(xinew.base, st);
37203+ if (unlikely(err)) {
37204+ AuErr1("statfs err %d, ignored\n", err);
37205+ err = 0;
37206+ goto out;
37207+ }
37208+
37209+ blocks = file_inode(file)->i_blocks;
37210+ pr_info("begin truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37211+ bindex, idx, (u64)blocks, st->f_bfree, st->f_blocks);
37212+
37213+ xinew.copy_src = file;
37214+ new_xino = au_xi_new(sb, &xinew);
37215+ if (IS_ERR(new_xino)) {
37216+ err = PTR_ERR(new_xino);
37217+ pr_err("xino(b%d-%d), err %d, ignored\n", bindex, idx, err);
37218+ goto out;
37219+ }
37220+
37221+ err = vfs_statfs(&new_xino->f_path, st);
37222+ if (!err)
37223+ pr_info("end truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37224+ bindex, idx, (u64)file_inode(new_xino)->i_blocks,
37225+ st->f_bfree, st->f_blocks);
37226+ else {
37227+ AuErr1("statfs err %d, ignored\n", err);
37228+ err = 0;
37229+ }
37230+
37231+out:
37232+ return err;
37233+}
37234+
37235+int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin)
37236+{
37237+ int err, i;
37238+ unsigned long jiffy;
37239+ aufs_bindex_t bbot;
37240+ struct kstatfs *st;
37241+ struct au_branch *br;
37242+ struct au_xino *xi;
37243+
37244+ err = -ENOMEM;
37245+ st = kmalloc(sizeof(*st), GFP_NOFS);
37246+ if (unlikely(!st))
37247+ goto out;
37248+
37249+ err = -EINVAL;
37250+ bbot = au_sbbot(sb);
37251+ if (unlikely(bindex < 0 || bbot < bindex))
37252+ goto out_st;
37253+
37254+ err = 0;
37255+ jiffy = jiffies;
37256+ br = au_sbr(sb, bindex);
37257+ xi = br->br_xino;
37258+ for (i = idx_begin; !err && i < xi->xi_nfile; i++)
37259+ err = au_xino_do_trunc(sb, bindex, i, st);
37260+ if (!err)
37261+ au_sbi(sb)->si_xino_jiffy = jiffy;
37262+
37263+out_st:
37264+ au_kfree_rcu(st);
37265+out:
37266+ return err;
37267+}
37268+
37269+struct xino_do_trunc_args {
37270+ struct super_block *sb;
37271+ struct au_branch *br;
37272+ int idx;
37273+};
37274+
37275+static void xino_do_trunc(void *_args)
37276+{
37277+ struct xino_do_trunc_args *args = _args;
37278+ struct super_block *sb;
37279+ struct au_branch *br;
37280+ struct inode *dir;
37281+ int err, idx;
37282+ aufs_bindex_t bindex;
37283+
37284+ err = 0;
37285+ sb = args->sb;
37286+ dir = d_inode(sb->s_root);
37287+ br = args->br;
37288+ idx = args->idx;
37289+
37290+ si_noflush_write_lock(sb);
37291+ ii_read_lock_parent(dir);
37292+ bindex = au_br_index(sb, br->br_id);
37293+ err = au_xino_trunc(sb, bindex, idx);
37294+ ii_read_unlock(dir);
37295+ if (unlikely(err))
37296+ pr_warn("err b%d, (%d)\n", bindex, err);
37297+ atomic_dec(&br->br_xino->xi_truncating);
37298+ au_lcnt_dec(&br->br_count);
37299+ si_write_unlock(sb);
37300+ au_nwt_done(&au_sbi(sb)->si_nowait);
37301+ au_kfree_rcu(args);
37302+}
37303+
37304+/*
37305+ * returns the index in the xi_file array whose corresponding file is necessary
37306+ * to truncate, or -1 which means no need to truncate.
37307+ */
37308+static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
37309+{
37310+ int err;
37311+ unsigned int u;
37312+ struct kstatfs st;
37313+ struct au_sbinfo *sbinfo;
37314+ struct au_xino *xi;
37315+ struct file *file;
37316+
37317+ /* todo: si_xino_expire and the ratio should be customizable */
37318+ sbinfo = au_sbi(sb);
37319+ if (time_before(jiffies,
37320+ sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
37321+ return -1;
37322+
37323+ /* truncation border */
37324+ xi = br->br_xino;
37325+ for (u = 0; u < xi->xi_nfile; u++) {
37326+ file = au_xino_file(xi, u);
37327+ if (!file)
37328+ continue;
37329+
37330+ err = vfs_statfs(&file->f_path, &st);
37331+ if (unlikely(err)) {
37332+ AuErr1("statfs err %d, ignored\n", err);
37333+ return -1;
37334+ }
37335+ if (div64_u64(st.f_bfree * 100, st.f_blocks)
37336+ >= AUFS_XINO_DEF_TRUNC)
37337+ return u;
37338+ }
37339+
37340+ return -1;
37341+}
37342+
37343+static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
37344+{
37345+ int idx;
37346+ struct xino_do_trunc_args *args;
37347+ int wkq_err;
37348+
37349+ idx = xino_trunc_test(sb, br);
37350+ if (idx < 0)
37351+ return;
37352+
37353+ if (atomic_inc_return(&br->br_xino->xi_truncating) > 1)
37354+ goto out;
37355+
37356+ /* lock and kfree() will be called in trunc_xino() */
37357+ args = kmalloc(sizeof(*args), GFP_NOFS);
37358+ if (unlikely(!args)) {
37359+ AuErr1("no memory\n");
37360+ goto out;
37361+ }
37362+
37363+ au_lcnt_inc(&br->br_count);
37364+ args->sb = sb;
37365+ args->br = br;
37366+ args->idx = idx;
37367+ wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
37368+ if (!wkq_err)
37369+ return; /* success */
37370+
37371+ pr_err("wkq %d\n", wkq_err);
37372+ au_lcnt_dec(&br->br_count);
37373+ au_kfree_rcu(args);
37374+
37375+out:
37376+ atomic_dec(&br->br_xino->xi_truncating);
37377+}
37378+
37379+/* ---------------------------------------------------------------------- */
37380+
37381+struct au_xi_calc {
37382+ int idx;
37383+ loff_t pos;
37384+};
37385+
37386+static void au_xi_calc(struct super_block *sb, ino_t h_ino,
37387+ struct au_xi_calc *calc)
37388+{
37389+ loff_t maxent;
37390+
37391+ maxent = au_xi_maxent(sb);
37392+ calc->idx = div64_u64_rem(h_ino, maxent, &calc->pos);
37393+ calc->pos *= sizeof(ino_t);
37394+}
37395+
37396+static int au_xino_do_new_async(struct super_block *sb, struct au_branch *br,
37397+ struct au_xi_calc *calc)
37398+{
37399+ int err;
37400+ struct file *file;
37401+ struct au_xino *xi = br->br_xino;
37402+ struct au_xi_new xinew = {
37403+ .xi = xi
37404+ };
37405+
37406+ SiMustAnyLock(sb);
37407+
37408+ err = 0;
37409+ if (!xi)
37410+ goto out;
37411+
37412+ mutex_lock(&xi->xi_mtx);
37413+ file = au_xino_file(xi, calc->idx);
37414+ if (file)
37415+ goto out_mtx;
37416+
37417+ file = au_xino_file(xi, /*idx*/-1);
37418+ AuDebugOn(!file);
37419+ xinew.idx = calc->idx;
37420+ xinew.base = &file->f_path;
37421+ /* xinew.copy_src = NULL; */
37422+ file = au_xi_new(sb, &xinew);
37423+ if (IS_ERR(file))
37424+ err = PTR_ERR(file);
37425+
37426+out_mtx:
37427+ mutex_unlock(&xi->xi_mtx);
37428+out:
37429+ return err;
37430+}
37431+
37432+struct au_xino_do_new_async_args {
37433+ struct super_block *sb;
37434+ struct au_branch *br;
37435+ struct au_xi_calc calc;
37436+ ino_t ino;
37437+};
37438+
37439+struct au_xi_writing {
37440+ struct hlist_bl_node node;
37441+ ino_t h_ino, ino;
37442+};
37443+
37444+static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37445+ ino_t ino);
37446+
37447+static void au_xino_call_do_new_async(void *args)
37448+{
37449+ struct au_xino_do_new_async_args *a = args;
37450+ struct au_branch *br;
37451+ struct super_block *sb;
37452+ struct au_sbinfo *sbi;
37453+ struct inode *root;
37454+ struct file *file;
37455+ struct au_xi_writing *del, *p;
37456+ struct hlist_bl_head *hbl;
37457+ struct hlist_bl_node *pos;
37458+ int err;
37459+
37460+ br = a->br;
37461+ sb = a->sb;
37462+ sbi = au_sbi(sb);
37463+ si_noflush_read_lock(sb);
37464+ root = d_inode(sb->s_root);
37465+ ii_read_lock_child(root);
37466+ err = au_xino_do_new_async(sb, br, &a->calc);
37467+ if (unlikely(err)) {
37468+ AuIOErr("err %d\n", err);
37469+ goto out;
37470+ }
37471+
37472+ file = au_xino_file(br->br_xino, a->calc.idx);
37473+ AuDebugOn(!file);
37474+ err = au_xino_do_write(file, &a->calc, a->ino);
37475+ if (unlikely(err)) {
37476+ AuIOErr("err %d\n", err);
37477+ goto out;
37478+ }
37479+
37480+ del = NULL;
37481+ hbl = &br->br_xino->xi_writing;
37482+ hlist_bl_lock(hbl);
37483+ au_hbl_for_each(pos, hbl) {
37484+ p = container_of(pos, struct au_xi_writing, node);
37485+ if (p->ino == a->ino) {
37486+ del = p;
37487+ hlist_bl_del(&p->node);
37488+ break;
37489+ }
37490+ }
37491+ hlist_bl_unlock(hbl);
37492+ au_kfree_rcu(del);
37493+
37494+out:
37495+ au_lcnt_dec(&br->br_count);
37496+ ii_read_unlock(root);
37497+ si_read_unlock(sb);
37498+ au_nwt_done(&sbi->si_nowait);
37499+ au_kfree_rcu(a);
37500+}
37501+
37502+/*
37503+ * create a new xino file asynchronously
37504+ */
37505+static int au_xino_new_async(struct super_block *sb, struct au_branch *br,
37506+ struct au_xi_calc *calc, ino_t ino)
37507+{
37508+ int err;
37509+ struct au_xino_do_new_async_args *arg;
37510+
37511+ err = -ENOMEM;
37512+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
37513+ if (unlikely(!arg))
37514+ goto out;
37515+
37516+ arg->sb = sb;
37517+ arg->br = br;
37518+ arg->calc = *calc;
37519+ arg->ino = ino;
37520+ au_lcnt_inc(&br->br_count);
37521+ err = au_wkq_nowait(au_xino_call_do_new_async, arg, sb, AuWkq_NEST);
37522+ if (unlikely(err)) {
37523+ pr_err("wkq %d\n", err);
37524+ au_lcnt_dec(&br->br_count);
37525+ au_kfree_rcu(arg);
37526+ }
37527+
37528+out:
37529+ return err;
37530+}
37531+
37532+/*
37533+ * read @ino from xinofile for the specified branch{@sb, @bindex}
37534+ * at the position of @h_ino.
37535+ */
37536+int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37537+ ino_t *ino)
37538+{
37539+ int err;
37540+ ssize_t sz;
37541+ struct au_xi_calc calc;
37542+ struct au_sbinfo *sbinfo;
37543+ struct file *file;
37544+ struct au_xino *xi;
37545+ struct hlist_bl_head *hbl;
37546+ struct hlist_bl_node *pos;
37547+ struct au_xi_writing *p;
37548+
37549+ *ino = 0;
37550+ if (!au_opt_test(au_mntflags(sb), XINO))
37551+ return 0; /* no xino */
37552+
37553+ err = 0;
37554+ au_xi_calc(sb, h_ino, &calc);
37555+ xi = au_sbr(sb, bindex)->br_xino;
37556+ file = au_xino_file(xi, calc.idx);
37557+ if (!file) {
37558+ hbl = &xi->xi_writing;
37559+ hlist_bl_lock(hbl);
37560+ au_hbl_for_each(pos, hbl) {
37561+ p = container_of(pos, struct au_xi_writing, node);
37562+ if (p->h_ino == h_ino) {
37563+ AuDbg("hi%llu, i%llu, found\n",
37564+ (u64)p->h_ino, (u64)p->ino);
37565+ *ino = p->ino;
37566+ break;
37567+ }
37568+ }
37569+ hlist_bl_unlock(hbl);
37570+ return 0;
37571+ } else if (vfsub_f_size_read(file) < calc.pos + sizeof(*ino))
37572+ return 0; /* no xino */
37573+
37574+ sbinfo = au_sbi(sb);
37575+ sz = xino_fread(file, ino, sizeof(*ino), &calc.pos);
37576+ if (sz == sizeof(*ino))
37577+ return 0; /* success */
37578+
37579+ err = sz;
37580+ if (unlikely(sz >= 0)) {
37581+ err = -EIO;
37582+ AuIOErr("xino read error (%zd)\n", sz);
37583+ }
37584+ return err;
37585+}
37586+
37587+static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37588+ ino_t ino)
37589+{
37590+ ssize_t sz;
37591+
37592+ sz = xino_fwrite(file, &ino, sizeof(ino), &calc->pos);
37593+ if (sz == sizeof(ino))
37594+ return 0; /* success */
37595+
37596+ AuIOErr("write failed (%zd)\n", sz);
37597+ return -EIO;
37598+}
37599+
37600+/*
37601+ * write @ino to the xinofile for the specified branch{@sb, @bindex}
37602+ * at the position of @h_ino.
37603+ * even if @ino is zero, it is written to the xinofile and means no entry.
37604+ * if the size of the xino file on a specific filesystem exceeds the watermark,
37605+ * try truncating it.
37606+ */
37607+int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37608+ ino_t ino)
37609+{
37610+ int err;
37611+ unsigned int mnt_flags;
37612+ struct au_xi_calc calc;
37613+ struct file *file;
37614+ struct au_branch *br;
37615+ struct au_xino *xi;
37616+ struct au_xi_writing *p;
37617+
37618+ SiMustAnyLock(sb);
37619+
37620+ mnt_flags = au_mntflags(sb);
37621+ if (!au_opt_test(mnt_flags, XINO))
37622+ return 0;
37623+
37624+ au_xi_calc(sb, h_ino, &calc);
37625+ br = au_sbr(sb, bindex);
37626+ xi = br->br_xino;
37627+ file = au_xino_file(xi, calc.idx);
37628+ if (!file) {
37629+ /* store the inum pair into the list */
37630+ p = kmalloc(sizeof(*p), GFP_NOFS | __GFP_NOFAIL);
37631+ p->h_ino = h_ino;
37632+ p->ino = ino;
37633+ au_hbl_add(&p->node, &xi->xi_writing);
37634+
37635+ /* create and write a new xino file asynchronously */
37636+ err = au_xino_new_async(sb, br, &calc, ino);
37637+ if (!err)
37638+ return 0; /* success */
37639+ goto out;
37640+ }
37641+
37642+ err = au_xino_do_write(file, &calc, ino);
37643+ if (!err) {
37644+ br = au_sbr(sb, bindex);
37645+ if (au_opt_test(mnt_flags, TRUNC_XINO)
37646+ && au_test_fs_trunc_xino(au_br_sb(br)))
37647+ xino_try_trunc(sb, br);
37648+ return 0; /* success */
37649+ }
37650+
37651+out:
37652+ AuIOErr("write failed (%d)\n", err);
37653+ return -EIO;
37654+}
37655+
37656+static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
37657+ loff_t *pos);
37658+
37659+/* todo: unnecessary to support mmap_sem since kernel-space? */
37660+ssize_t xino_fread(struct file *file, void *kbuf, size_t size, loff_t *pos)
37661+{
37662+ ssize_t err;
37663+ int i;
37664+ const int prevent_endless = 10;
37665+
37666+ i = 0;
37667+ do {
37668+ err = vfsub_read_k(file, kbuf, size, pos);
37669+ if (err == -EINTR
37670+ && !au_wkq_test()
37671+ && fatal_signal_pending(current)) {
37672+ err = xino_fread_wkq(file, kbuf, size, pos);
37673+ BUG_ON(err == -EINTR);
37674+ }
37675+ } while (i++ < prevent_endless
37676+ && (err == -EAGAIN || err == -EINTR));
37677+
37678+#if 0 /* reserved for future use */
37679+ if (err > 0)
37680+ fsnotify_access(file->f_path.dentry);
37681+#endif
37682+
37683+ return err;
37684+}
37685+
37686+struct xino_fread_args {
37687+ ssize_t *errp;
37688+ struct file *file;
37689+ void *buf;
37690+ size_t size;
37691+ loff_t *pos;
37692+};
37693+
37694+static void call_xino_fread(void *args)
37695+{
37696+ struct xino_fread_args *a = args;
37697+ *a->errp = xino_fread(a->file, a->buf, a->size, a->pos);
37698+}
37699+
37700+static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
37701+ loff_t *pos)
37702+{
37703+ ssize_t err;
37704+ int wkq_err;
37705+ struct xino_fread_args args = {
37706+ .errp = &err,
37707+ .file = file,
37708+ .buf = buf,
37709+ .size = size,
37710+ .pos = pos
37711+ };
37712+
37713+ wkq_err = au_wkq_wait(call_xino_fread, &args);
37714+ if (unlikely(wkq_err))
37715+ err = wkq_err;
37716+
37717+ return err;
37718+}
37719+
37720+static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
37721+ loff_t *pos);
37722+
37723+static ssize_t do_xino_fwrite(struct file *file, void *kbuf, size_t size,
37724+ loff_t *pos)
37725+{
37726+ ssize_t err;
37727+ int i;
37728+ const int prevent_endless = 10;
37729+
37730+ i = 0;
37731+ do {
37732+ err = vfsub_write_k(file, kbuf, size, pos);
37733+ if (err == -EINTR
37734+ && !au_wkq_test()
37735+ && fatal_signal_pending(current)) {
37736+ err = xino_fwrite_wkq(file, kbuf, size, pos);
37737+ BUG_ON(err == -EINTR);
37738+ }
37739+ } while (i++ < prevent_endless
37740+ && (err == -EAGAIN || err == -EINTR));
37741+
37742+#if 0 /* reserved for future use */
37743+ if (err > 0)
37744+ fsnotify_modify(file->f_path.dentry);
37745+#endif
37746+
37747+ return err;
37748+}
37749+
37750+struct do_xino_fwrite_args {
37751+ ssize_t *errp;
37752+ struct file *file;
37753+ void *buf;
37754+ size_t size;
37755+ loff_t *pos;
37756+};
37757+
37758+static void call_do_xino_fwrite(void *args)
37759+{
37760+ struct do_xino_fwrite_args *a = args;
37761+ *a->errp = do_xino_fwrite(a->file, a->buf, a->size, a->pos);
37762+}
37763+
37764+static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
37765+ loff_t *pos)
37766+{
37767+ ssize_t err;
37768+ int wkq_err;
37769+ struct do_xino_fwrite_args args = {
37770+ .errp = &err,
37771+ .file = file,
37772+ .buf = buf,
37773+ .size = size,
37774+ .pos = pos
37775+ };
37776+
37777+ /*
37778+ * it breaks RLIMIT_FSIZE and normal user's limit,
37779+ * users should care about quota and real 'filesystem full.'
37780+ */
37781+ wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
37782+ if (unlikely(wkq_err))
37783+ err = wkq_err;
37784+
37785+ return err;
37786+}
37787+
37788+ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos)
37789+{
37790+ ssize_t err;
37791+
37792+ if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
37793+ lockdep_off();
37794+ err = do_xino_fwrite(file, buf, size, pos);
37795+ lockdep_on();
37796+ } else {
37797+ lockdep_off();
37798+ err = xino_fwrite_wkq(file, buf, size, pos);
37799+ lockdep_on();
37800+ }
37801+
37802+ return err;
37803+}
37804+
37805+/* ---------------------------------------------------------------------- */
37806+
37807+/*
37808+ * inode number bitmap
37809+ */
37810+static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
37811+static ino_t xib_calc_ino(unsigned long pindex, int bit)
37812+{
37813+ ino_t ino;
37814+
37815+ AuDebugOn(bit < 0 || page_bits <= bit);
37816+ ino = AUFS_FIRST_INO + pindex * page_bits + bit;
37817+ return ino;
37818+}
37819+
37820+static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
37821+{
37822+ AuDebugOn(ino < AUFS_FIRST_INO);
37823+ ino -= AUFS_FIRST_INO;
37824+ *pindex = ino / page_bits;
37825+ *bit = ino % page_bits;
37826+}
37827+
37828+static int xib_pindex(struct super_block *sb, unsigned long pindex)
37829+{
37830+ int err;
37831+ loff_t pos;
37832+ ssize_t sz;
37833+ struct au_sbinfo *sbinfo;
37834+ struct file *xib;
37835+ unsigned long *p;
37836+
37837+ sbinfo = au_sbi(sb);
37838+ MtxMustLock(&sbinfo->si_xib_mtx);
37839+ AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
37840+ || !au_opt_test(sbinfo->si_mntflags, XINO));
37841+
37842+ if (pindex == sbinfo->si_xib_last_pindex)
37843+ return 0;
37844+
37845+ xib = sbinfo->si_xib;
37846+ p = sbinfo->si_xib_buf;
37847+ pos = sbinfo->si_xib_last_pindex;
37848+ pos *= PAGE_SIZE;
37849+ sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
37850+ if (unlikely(sz != PAGE_SIZE))
37851+ goto out;
37852+
37853+ pos = pindex;
37854+ pos *= PAGE_SIZE;
37855+ if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
37856+ sz = xino_fread(xib, p, PAGE_SIZE, &pos);
37857+ else {
37858+ memset(p, 0, PAGE_SIZE);
37859+ sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
37860+ }
37861+ if (sz == PAGE_SIZE) {
37862+ sbinfo->si_xib_last_pindex = pindex;
37863+ return 0; /* success */
37864+ }
37865+
37866+out:
37867+ AuIOErr1("write failed (%zd)\n", sz);
37868+ err = sz;
37869+ if (sz >= 0)
37870+ err = -EIO;
37871+ return err;
37872+}
37873+
37874+static void au_xib_clear_bit(struct inode *inode)
37875+{
37876+ int err, bit;
37877+ unsigned long pindex;
37878+ struct super_block *sb;
37879+ struct au_sbinfo *sbinfo;
37880+
37881+ AuDebugOn(inode->i_nlink);
37882+
37883+ sb = inode->i_sb;
37884+ xib_calc_bit(inode->i_ino, &pindex, &bit);
37885+ AuDebugOn(page_bits <= bit);
37886+ sbinfo = au_sbi(sb);
37887+ mutex_lock(&sbinfo->si_xib_mtx);
37888+ err = xib_pindex(sb, pindex);
37889+ if (!err) {
37890+ clear_bit(bit, sbinfo->si_xib_buf);
37891+ sbinfo->si_xib_next_bit = bit;
37892+ }
37893+ mutex_unlock(&sbinfo->si_xib_mtx);
37894+}
37895+
37896+/* ---------------------------------------------------------------------- */
37897+
37898+/*
37899+ * truncate a xino bitmap file
37900+ */
37901+
37902+/* todo: slow */
37903+static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
37904+{
37905+ int err, bit;
37906+ ssize_t sz;
37907+ unsigned long pindex;
37908+ loff_t pos, pend;
37909+ struct au_sbinfo *sbinfo;
37910+ ino_t *ino;
37911+ unsigned long *p;
37912+
37913+ err = 0;
37914+ sbinfo = au_sbi(sb);
37915+ MtxMustLock(&sbinfo->si_xib_mtx);
37916+ p = sbinfo->si_xib_buf;
37917+ pend = vfsub_f_size_read(file);
37918+ pos = 0;
37919+ while (pos < pend) {
37920+ sz = xino_fread(file, page, PAGE_SIZE, &pos);
37921+ err = sz;
37922+ if (unlikely(sz <= 0))
37923+ goto out;
37924+
37925+ err = 0;
37926+ for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
37927+ if (unlikely(*ino < AUFS_FIRST_INO))
37928+ continue;
37929+
37930+ xib_calc_bit(*ino, &pindex, &bit);
37931+ AuDebugOn(page_bits <= bit);
37932+ err = xib_pindex(sb, pindex);
37933+ if (!err)
37934+ set_bit(bit, p);
37935+ else
37936+ goto out;
37937+ }
37938+ }
37939+
37940+out:
37941+ return err;
37942+}
37943+
37944+static int xib_restore(struct super_block *sb)
37945+{
37946+ int err, i;
37947+ unsigned int nfile;
37948+ aufs_bindex_t bindex, bbot;
37949+ void *page;
37950+ struct au_branch *br;
37951+ struct au_xino *xi;
37952+ struct file *file;
37953+
37954+ err = -ENOMEM;
37955+ page = (void *)__get_free_page(GFP_NOFS);
37956+ if (unlikely(!page))
37957+ goto out;
37958+
37959+ err = 0;
37960+ bbot = au_sbbot(sb);
37961+ for (bindex = 0; !err && bindex <= bbot; bindex++)
37962+ if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0) {
37963+ br = au_sbr(sb, bindex);
37964+ xi = br->br_xino;
37965+ nfile = xi->xi_nfile;
37966+ for (i = 0; i < nfile; i++) {
37967+ file = au_xino_file(xi, i);
37968+ if (file)
37969+ err = do_xib_restore(sb, file, page);
37970+ }
37971+ } else
37972+ AuDbg("skip shared b%d\n", bindex);
37973+ free_page((unsigned long)page);
37974+
37975+out:
37976+ return err;
37977+}
37978+
37979+int au_xib_trunc(struct super_block *sb)
37980+{
37981+ int err;
37982+ ssize_t sz;
37983+ loff_t pos;
37984+ struct au_sbinfo *sbinfo;
37985+ unsigned long *p;
37986+ struct file *file;
37987+
37988+ SiMustWriteLock(sb);
37989+
37990+ err = 0;
37991+ sbinfo = au_sbi(sb);
37992+ if (!au_opt_test(sbinfo->si_mntflags, XINO))
37993+ goto out;
37994+
37995+ file = sbinfo->si_xib;
37996+ if (vfsub_f_size_read(file) <= PAGE_SIZE)
37997+ goto out;
37998+
37999+ file = au_xino_create2(sb, &sbinfo->si_xib->f_path, NULL);
38000+ err = PTR_ERR(file);
38001+ if (IS_ERR(file))
38002+ goto out;
38003+ fput(sbinfo->si_xib);
38004+ sbinfo->si_xib = file;
38005+
38006+ p = sbinfo->si_xib_buf;
38007+ memset(p, 0, PAGE_SIZE);
38008+ pos = 0;
38009+ sz = xino_fwrite(sbinfo->si_xib, p, PAGE_SIZE, &pos);
38010+ if (unlikely(sz != PAGE_SIZE)) {
38011+ err = sz;
38012+ AuIOErr("err %d\n", err);
38013+ if (sz >= 0)
38014+ err = -EIO;
38015+ goto out;
38016+ }
38017+
38018+ mutex_lock(&sbinfo->si_xib_mtx);
38019+ /* mnt_want_write() is unnecessary here */
38020+ err = xib_restore(sb);
38021+ mutex_unlock(&sbinfo->si_xib_mtx);
38022+
38023+out:
38024+ return err;
38025+}
38026+
38027+/* ---------------------------------------------------------------------- */
38028+
38029+struct au_xino *au_xino_alloc(unsigned int nfile)
38030+{
38031+ struct au_xino *xi;
38032+
38033+ xi = kzalloc(sizeof(*xi), GFP_NOFS);
38034+ if (unlikely(!xi))
38035+ goto out;
38036+ xi->xi_nfile = nfile;
38037+ xi->xi_file = kcalloc(nfile, sizeof(*xi->xi_file), GFP_NOFS);
38038+ if (unlikely(!xi->xi_file))
38039+ goto out_free;
38040+
38041+ xi->xi_nondir.total = 8; /* initial size */
38042+ xi->xi_nondir.array = kcalloc(xi->xi_nondir.total, sizeof(ino_t),
38043+ GFP_NOFS);
38044+ if (unlikely(!xi->xi_nondir.array))
38045+ goto out_file;
38046+
38047+ spin_lock_init(&xi->xi_nondir.spin);
38048+ init_waitqueue_head(&xi->xi_nondir.wqh);
38049+ mutex_init(&xi->xi_mtx);
38050+ INIT_HLIST_BL_HEAD(&xi->xi_writing);
38051+ atomic_set(&xi->xi_truncating, 0);
38052+ kref_init(&xi->xi_kref);
38053+ goto out; /* success */
38054+
38055+out_file:
38056+ au_kfree_try_rcu(xi->xi_file);
38057+out_free:
38058+ au_kfree_rcu(xi);
38059+ xi = NULL;
38060+out:
38061+ return xi;
38062+}
38063+
38064+static int au_xino_init(struct au_branch *br, int idx, struct file *file)
38065+{
38066+ int err;
38067+ struct au_xino *xi;
38068+
38069+ err = 0;
38070+ xi = au_xino_alloc(idx + 1);
38071+ if (unlikely(!xi)) {
38072+ err = -ENOMEM;
38073+ goto out;
38074+ }
38075+
38076+ if (file)
38077+ get_file(file);
38078+ xi->xi_file[idx] = file;
38079+ AuDebugOn(br->br_xino);
38080+ br->br_xino = xi;
38081+
38082+out:
38083+ return err;
38084+}
38085+
38086+static void au_xino_release(struct kref *kref)
38087+{
38088+ struct au_xino *xi;
38089+ int i;
38090+ unsigned long ul;
38091+ struct hlist_bl_head *hbl;
38092+ struct hlist_bl_node *pos, *n;
38093+ struct au_xi_writing *p;
38094+
38095+ xi = container_of(kref, struct au_xino, xi_kref);
38096+ for (i = 0; i < xi->xi_nfile; i++)
38097+ if (xi->xi_file[i])
38098+ fput(xi->xi_file[i]);
38099+ for (i = xi->xi_nondir.total - 1; i >= 0; i--)
38100+ AuDebugOn(xi->xi_nondir.array[i]);
38101+ mutex_destroy(&xi->xi_mtx);
38102+ hbl = &xi->xi_writing;
38103+ ul = au_hbl_count(hbl);
38104+ if (unlikely(ul)) {
38105+ pr_warn("xi_writing %lu\n", ul);
38106+ hlist_bl_lock(hbl);
38107+ hlist_bl_for_each_entry_safe(p, pos, n, hbl, node) {
38108+ hlist_bl_del(&p->node);
38109+ /* kmemleak reported au_kfree_rcu() doesn't free it */
38110+ kfree(p);
38111+ }
38112+ hlist_bl_unlock(hbl);
38113+ }
38114+ au_kfree_try_rcu(xi->xi_file);
38115+ au_kfree_try_rcu(xi->xi_nondir.array);
38116+ au_kfree_rcu(xi);
38117+}
38118+
38119+int au_xino_put(struct au_branch *br)
38120+{
38121+ int ret;
38122+ struct au_xino *xi;
38123+
38124+ ret = 0;
38125+ xi = br->br_xino;
38126+ if (xi) {
38127+ br->br_xino = NULL;
38128+ ret = kref_put(&xi->xi_kref, au_xino_release);
38129+ }
38130+
38131+ return ret;
38132+}
38133+
38134+/* ---------------------------------------------------------------------- */
38135+
38136+/*
38137+ * xino mount option handlers
38138+ */
38139+
38140+/* xino bitmap */
38141+static void xino_clear_xib(struct super_block *sb)
38142+{
38143+ struct au_sbinfo *sbinfo;
38144+
38145+ SiMustWriteLock(sb);
38146+
38147+ sbinfo = au_sbi(sb);
38148+ if (sbinfo->si_xib)
38149+ fput(sbinfo->si_xib);
38150+ sbinfo->si_xib = NULL;
38151+ if (sbinfo->si_xib_buf)
38152+ free_page((unsigned long)sbinfo->si_xib_buf);
38153+ sbinfo->si_xib_buf = NULL;
38154+}
38155+
38156+static int au_xino_set_xib(struct super_block *sb, struct path *path)
38157+{
38158+ int err;
38159+ loff_t pos;
38160+ struct au_sbinfo *sbinfo;
38161+ struct file *file;
38162+ struct super_block *xi_sb;
38163+
38164+ SiMustWriteLock(sb);
38165+
38166+ sbinfo = au_sbi(sb);
38167+ file = au_xino_create2(sb, path, sbinfo->si_xib);
38168+ err = PTR_ERR(file);
38169+ if (IS_ERR(file))
38170+ goto out;
38171+ if (sbinfo->si_xib)
38172+ fput(sbinfo->si_xib);
38173+ sbinfo->si_xib = file;
38174+ xi_sb = file_inode(file)->i_sb;
38175+ sbinfo->si_ximaxent = xi_sb->s_maxbytes;
38176+ if (unlikely(sbinfo->si_ximaxent < PAGE_SIZE)) {
38177+ err = -EIO;
38178+ pr_err("s_maxbytes(%llu) on %s is too small\n",
38179+ (u64)sbinfo->si_ximaxent, au_sbtype(xi_sb));
38180+ goto out_unset;
38181+ }
38182+ sbinfo->si_ximaxent /= sizeof(ino_t);
38183+
38184+ err = -ENOMEM;
38185+ if (!sbinfo->si_xib_buf)
38186+ sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
38187+ if (unlikely(!sbinfo->si_xib_buf))
38188+ goto out_unset;
38189+
38190+ sbinfo->si_xib_last_pindex = 0;
38191+ sbinfo->si_xib_next_bit = 0;
38192+ if (vfsub_f_size_read(file) < PAGE_SIZE) {
38193+ pos = 0;
38194+ err = xino_fwrite(file, sbinfo->si_xib_buf, PAGE_SIZE, &pos);
38195+ if (unlikely(err != PAGE_SIZE))
38196+ goto out_free;
38197+ }
38198+ err = 0;
38199+ goto out; /* success */
38200+
38201+out_free:
38202+ if (sbinfo->si_xib_buf)
38203+ free_page((unsigned long)sbinfo->si_xib_buf);
38204+ sbinfo->si_xib_buf = NULL;
38205+ if (err >= 0)
38206+ err = -EIO;
38207+out_unset:
38208+ fput(sbinfo->si_xib);
38209+ sbinfo->si_xib = NULL;
38210+out:
38211+ AuTraceErr(err);
38212+ return err;
38213+}
38214+
38215+/* xino for each branch */
38216+static void xino_clear_br(struct super_block *sb)
38217+{
38218+ aufs_bindex_t bindex, bbot;
38219+ struct au_branch *br;
38220+
38221+ bbot = au_sbbot(sb);
38222+ for (bindex = 0; bindex <= bbot; bindex++) {
38223+ br = au_sbr(sb, bindex);
38224+ AuDebugOn(!br);
38225+ au_xino_put(br);
38226+ }
38227+}
38228+
38229+static void au_xino_set_br_shared(struct super_block *sb, struct au_branch *br,
38230+ aufs_bindex_t bshared)
38231+{
38232+ struct au_branch *brshared;
38233+
38234+ brshared = au_sbr(sb, bshared);
38235+ AuDebugOn(!brshared->br_xino);
38236+ AuDebugOn(!brshared->br_xino->xi_file);
38237+ if (br->br_xino != brshared->br_xino) {
38238+ au_xino_get(brshared);
38239+ au_xino_put(br);
38240+ br->br_xino = brshared->br_xino;
38241+ }
38242+}
38243+
38244+struct au_xino_do_set_br {
38245+ struct au_branch *br;
38246+ ino_t h_ino;
38247+ aufs_bindex_t bshared;
38248+};
38249+
38250+static int au_xino_do_set_br(struct super_block *sb, struct path *path,
38251+ struct au_xino_do_set_br *args)
38252+{
38253+ int err;
38254+ struct au_xi_calc calc;
38255+ struct file *file;
38256+ struct au_branch *br;
38257+ struct au_xi_new xinew = {
38258+ .base = path
38259+ };
38260+
38261+ br = args->br;
38262+ xinew.xi = br->br_xino;
38263+ au_xi_calc(sb, args->h_ino, &calc);
38264+ xinew.copy_src = au_xino_file(xinew.xi, calc.idx);
38265+ if (args->bshared >= 0)
38266+ /* shared xino */
38267+ au_xino_set_br_shared(sb, br, args->bshared);
38268+ else if (!xinew.xi) {
38269+ /* new xino */
38270+ err = au_xino_init(br, calc.idx, xinew.copy_src);
38271+ if (unlikely(err))
38272+ goto out;
38273+ }
38274+
38275+ /* force re-creating */
38276+ xinew.xi = br->br_xino;
38277+ xinew.idx = calc.idx;
38278+ mutex_lock(&xinew.xi->xi_mtx);
38279+ file = au_xi_new(sb, &xinew);
38280+ mutex_unlock(&xinew.xi->xi_mtx);
38281+ err = PTR_ERR(file);
38282+ if (IS_ERR(file))
38283+ goto out;
38284+ AuDebugOn(!file);
38285+
38286+ err = au_xino_do_write(file, &calc, AUFS_ROOT_INO);
38287+ if (unlikely(err))
38288+ au_xino_put(br);
38289+
38290+out:
38291+ AuTraceErr(err);
38292+ return err;
38293+}
38294+
38295+static int au_xino_set_br(struct super_block *sb, struct path *path)
38296+{
38297+ int err;
38298+ aufs_bindex_t bindex, bbot;
38299+ struct au_xino_do_set_br args;
38300+ struct inode *inode;
38301+
38302+ SiMustWriteLock(sb);
38303+
38304+ bbot = au_sbbot(sb);
38305+ inode = d_inode(sb->s_root);
38306+ for (bindex = 0; bindex <= bbot; bindex++) {
38307+ args.h_ino = au_h_iptr(inode, bindex)->i_ino;
38308+ args.br = au_sbr(sb, bindex);
38309+ args.bshared = is_sb_shared(sb, bindex, bindex - 1);
38310+ err = au_xino_do_set_br(sb, path, &args);
38311+ if (unlikely(err))
38312+ break;
38313+ }
38314+
38315+ AuTraceErr(err);
38316+ return err;
38317+}
38318+
38319+void au_xino_clr(struct super_block *sb)
38320+{
38321+ struct au_sbinfo *sbinfo;
38322+
38323+ au_xigen_clr(sb);
38324+ xino_clear_xib(sb);
38325+ xino_clear_br(sb);
38326+ dbgaufs_brs_del(sb, 0);
38327+ sbinfo = au_sbi(sb);
38328+ /* lvalue, do not call au_mntflags() */
38329+ au_opt_clr(sbinfo->si_mntflags, XINO);
38330+}
38331+
38332+int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount)
38333+{
38334+ int err, skip;
38335+ struct dentry *dentry, *parent, *cur_dentry, *cur_parent;
38336+ struct qstr *dname, *cur_name;
38337+ struct file *cur_xino;
38338+ struct au_sbinfo *sbinfo;
38339+ struct path *path, *cur_path;
38340+
38341+ SiMustWriteLock(sb);
38342+
38343+ err = 0;
38344+ sbinfo = au_sbi(sb);
38345+ path = &xiopt->file->f_path;
38346+ dentry = path->dentry;
38347+ parent = dget_parent(dentry);
38348+ if (remount) {
38349+ skip = 0;
38350+ cur_xino = sbinfo->si_xib;
38351+ if (cur_xino) {
38352+ cur_path = &cur_xino->f_path;
38353+ cur_dentry = cur_path->dentry;
38354+ cur_parent = dget_parent(cur_dentry);
38355+ cur_name = &cur_dentry->d_name;
38356+ dname = &dentry->d_name;
38357+ skip = (cur_parent == parent
38358+ && au_qstreq(dname, cur_name));
38359+ dput(cur_parent);
38360+ }
38361+ if (skip)
38362+ goto out;
38363+ }
38364+
38365+ au_opt_set(sbinfo->si_mntflags, XINO);
38366+ err = au_xino_set_xib(sb, path);
38367+ /* si_x{read,write} are set */
38368+ if (!err)
38369+ err = au_xigen_set(sb, path);
38370+ if (!err)
38371+ err = au_xino_set_br(sb, path);
38372+ if (!err) {
38373+ dbgaufs_brs_add(sb, 0, /*topdown*/1);
38374+ goto out; /* success */
38375+ }
38376+
38377+ /* reset all */
38378+ AuIOErr("failed setting xino(%d).\n", err);
38379+ au_xino_clr(sb);
38380+
38381+out:
38382+ dput(parent);
38383+ return err;
38384+}
38385+
38386+/*
38387+ * create a xinofile at the default place/path.
38388+ */
38389+struct file *au_xino_def(struct super_block *sb)
38390+{
38391+ struct file *file;
38392+ char *page, *p;
38393+ struct au_branch *br;
38394+ struct super_block *h_sb;
38395+ struct path path;
38396+ aufs_bindex_t bbot, bindex, bwr;
38397+
38398+ br = NULL;
38399+ bbot = au_sbbot(sb);
38400+ bwr = -1;
38401+ for (bindex = 0; bindex <= bbot; bindex++) {
38402+ br = au_sbr(sb, bindex);
38403+ if (au_br_writable(br->br_perm)
38404+ && !au_test_fs_bad_xino(au_br_sb(br))) {
38405+ bwr = bindex;
38406+ break;
38407+ }
38408+ }
38409+
38410+ if (bwr >= 0) {
38411+ file = ERR_PTR(-ENOMEM);
38412+ page = (void *)__get_free_page(GFP_NOFS);
38413+ if (unlikely(!page))
38414+ goto out;
38415+ path.mnt = au_br_mnt(br);
38416+ path.dentry = au_h_dptr(sb->s_root, bwr);
38417+ p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
38418+ file = (void *)p;
38419+ if (!IS_ERR(p)) {
38420+ strcat(p, "/" AUFS_XINO_FNAME);
38421+ AuDbg("%s\n", p);
38422+ file = au_xino_create(sb, p, /*silent*/0, /*wbrtop*/1);
38423+ }
38424+ free_page((unsigned long)page);
38425+ } else {
38426+ file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0,
38427+ /*wbrtop*/0);
38428+ if (IS_ERR(file))
38429+ goto out;
38430+ h_sb = file->f_path.dentry->d_sb;
38431+ if (unlikely(au_test_fs_bad_xino(h_sb))) {
38432+ pr_err("xino doesn't support %s(%s)\n",
38433+ AUFS_XINO_DEFPATH, au_sbtype(h_sb));
38434+ fput(file);
38435+ file = ERR_PTR(-EINVAL);
38436+ }
38437+ }
38438+
38439+out:
38440+ return file;
38441+}
38442+
38443+/* ---------------------------------------------------------------------- */
38444+
38445+/*
38446+ * initialize the xinofile for the specified branch @br
38447+ * at the place/path where @base_file indicates.
38448+ * test whether another branch is on the same filesystem or not,
38449+ * if found then share the xinofile with another branch.
38450+ */
38451+int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
38452+ struct path *base)
38453+{
38454+ int err;
38455+ struct au_xino_do_set_br args = {
38456+ .h_ino = h_ino,
38457+ .br = br
38458+ };
38459+
38460+ args.bshared = sbr_find_shared(sb, /*btop*/0, au_sbbot(sb),
38461+ au_br_sb(br));
38462+ err = au_xino_do_set_br(sb, base, &args);
38463+ if (unlikely(err))
38464+ au_xino_put(br);
38465+
38466+ return err;
38467+}
38468+
38469+/* ---------------------------------------------------------------------- */
38470+
38471+/*
38472+ * get an unused inode number from bitmap
38473+ */
38474+ino_t au_xino_new_ino(struct super_block *sb)
38475+{
38476+ ino_t ino;
38477+ unsigned long *p, pindex, ul, pend;
38478+ struct au_sbinfo *sbinfo;
38479+ struct file *file;
38480+ int free_bit, err;
38481+
38482+ if (!au_opt_test(au_mntflags(sb), XINO))
38483+ return iunique(sb, AUFS_FIRST_INO);
38484+
38485+ sbinfo = au_sbi(sb);
38486+ mutex_lock(&sbinfo->si_xib_mtx);
38487+ p = sbinfo->si_xib_buf;
38488+ free_bit = sbinfo->si_xib_next_bit;
38489+ if (free_bit < page_bits && !test_bit(free_bit, p))
38490+ goto out; /* success */
38491+ free_bit = find_first_zero_bit(p, page_bits);
38492+ if (free_bit < page_bits)
38493+ goto out; /* success */
38494+
38495+ pindex = sbinfo->si_xib_last_pindex;
38496+ for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
38497+ err = xib_pindex(sb, ul);
38498+ if (unlikely(err))
38499+ goto out_err;
38500+ free_bit = find_first_zero_bit(p, page_bits);
38501+ if (free_bit < page_bits)
38502+ goto out; /* success */
38503+ }
38504+
38505+ file = sbinfo->si_xib;
38506+ pend = vfsub_f_size_read(file) / PAGE_SIZE;
38507+ for (ul = pindex + 1; ul <= pend; ul++) {
38508+ err = xib_pindex(sb, ul);
38509+ if (unlikely(err))
38510+ goto out_err;
38511+ free_bit = find_first_zero_bit(p, page_bits);
38512+ if (free_bit < page_bits)
38513+ goto out; /* success */
38514+ }
38515+ BUG();
38516+
38517+out:
38518+ set_bit(free_bit, p);
38519+ sbinfo->si_xib_next_bit = free_bit + 1;
38520+ pindex = sbinfo->si_xib_last_pindex;
38521+ mutex_unlock(&sbinfo->si_xib_mtx);
38522+ ino = xib_calc_ino(pindex, free_bit);
38523+ AuDbg("i%lu\n", (unsigned long)ino);
38524+ return ino;
38525+out_err:
38526+ mutex_unlock(&sbinfo->si_xib_mtx);
38527+ AuDbg("i0\n");
38528+ return 0;
38529+}
38530+
38531+/* for s_op->delete_inode() */
38532+void au_xino_delete_inode(struct inode *inode, const int unlinked)
38533+{
38534+ int err;
38535+ unsigned int mnt_flags;
38536+ aufs_bindex_t bindex, bbot, bi;
38537+ unsigned char try_trunc;
38538+ struct au_iinfo *iinfo;
38539+ struct super_block *sb;
38540+ struct au_hinode *hi;
38541+ struct inode *h_inode;
38542+ struct au_branch *br;
38543+ struct au_xi_calc calc;
38544+ struct file *file;
38545+
38546+ AuDebugOn(au_is_bad_inode(inode));
38547+
38548+ sb = inode->i_sb;
38549+ mnt_flags = au_mntflags(sb);
38550+ if (!au_opt_test(mnt_flags, XINO)
38551+ || inode->i_ino == AUFS_ROOT_INO)
38552+ return;
38553+
38554+ if (unlinked) {
38555+ au_xigen_inc(inode);
38556+ au_xib_clear_bit(inode);
38557+ }
38558+
38559+ iinfo = au_ii(inode);
38560+ bindex = iinfo->ii_btop;
38561+ if (bindex < 0)
38562+ return;
38563+
38564+ try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
38565+ hi = au_hinode(iinfo, bindex);
38566+ bbot = iinfo->ii_bbot;
38567+ for (; bindex <= bbot; bindex++, hi++) {
38568+ h_inode = hi->hi_inode;
38569+ if (!h_inode
38570+ || (!unlinked && h_inode->i_nlink))
38571+ continue;
38572+
38573+ /* inode may not be revalidated */
38574+ bi = au_br_index(sb, hi->hi_id);
38575+ if (bi < 0)
38576+ continue;
38577+
38578+ br = au_sbr(sb, bi);
38579+ au_xi_calc(sb, h_inode->i_ino, &calc);
38580+ file = au_xino_file(br->br_xino, calc.idx);
38581+ if (IS_ERR_OR_NULL(file))
38582+ continue;
38583+
38584+ err = au_xino_do_write(file, &calc, /*ino*/0);
38585+ if (!err && try_trunc
38586+ && au_test_fs_trunc_xino(au_br_sb(br)))
38587+ xino_try_trunc(sb, br);
38588+ }
38589+}
38590+
38591+/* ---------------------------------------------------------------------- */
38592+
38593+static int au_xinondir_find(struct au_xino *xi, ino_t h_ino)
38594+{
38595+ int found, total, i;
38596+
38597+ found = -1;
38598+ total = xi->xi_nondir.total;
38599+ for (i = 0; i < total; i++) {
38600+ if (xi->xi_nondir.array[i] != h_ino)
38601+ continue;
38602+ found = i;
38603+ break;
38604+ }
38605+
38606+ return found;
38607+}
38608+
38609+static int au_xinondir_expand(struct au_xino *xi)
38610+{
38611+ int err, sz;
38612+ ino_t *p;
38613+
38614+ BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
38615+
38616+ err = -ENOMEM;
38617+ sz = xi->xi_nondir.total * sizeof(ino_t);
38618+ if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
38619+ goto out;
38620+ p = au_kzrealloc(xi->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
38621+ /*may_shrink*/0);
38622+ if (p) {
38623+ xi->xi_nondir.array = p;
38624+ xi->xi_nondir.total <<= 1;
38625+ AuDbg("xi_nondir.total %d\n", xi->xi_nondir.total);
38626+ err = 0;
38627+ }
38628+
38629+out:
38630+ return err;
38631+}
38632+
38633+void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
38634+ ino_t h_ino, int idx)
38635+{
38636+ struct au_xino *xi;
38637+
38638+ AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
38639+ xi = au_sbr(sb, bindex)->br_xino;
38640+ AuDebugOn(idx < 0 || xi->xi_nondir.total <= idx);
38641+
38642+ spin_lock(&xi->xi_nondir.spin);
38643+ AuDebugOn(xi->xi_nondir.array[idx] != h_ino);
38644+ xi->xi_nondir.array[idx] = 0;
38645+ spin_unlock(&xi->xi_nondir.spin);
38646+ wake_up_all(&xi->xi_nondir.wqh);
38647+}
38648+
38649+int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
38650+ int *idx)
38651+{
38652+ int err, found, empty;
38653+ struct au_xino *xi;
38654+
38655+ err = 0;
38656+ *idx = -1;
38657+ if (!au_opt_test(au_mntflags(sb), XINO))
38658+ goto out; /* no xino */
38659+
38660+ xi = au_sbr(sb, bindex)->br_xino;
38661+
38662+again:
38663+ spin_lock(&xi->xi_nondir.spin);
38664+ found = au_xinondir_find(xi, h_ino);
38665+ if (found == -1) {
38666+ empty = au_xinondir_find(xi, /*h_ino*/0);
38667+ if (empty == -1) {
38668+ empty = xi->xi_nondir.total;
38669+ err = au_xinondir_expand(xi);
38670+ if (unlikely(err))
38671+ goto out_unlock;
38672+ }
38673+ xi->xi_nondir.array[empty] = h_ino;
38674+ *idx = empty;
38675+ } else {
38676+ spin_unlock(&xi->xi_nondir.spin);
38677+ wait_event(xi->xi_nondir.wqh,
38678+ xi->xi_nondir.array[found] != h_ino);
38679+ goto again;
38680+ }
38681+
38682+out_unlock:
38683+ spin_unlock(&xi->xi_nondir.spin);
38684+out:
38685+ return err;
38686+}
38687+
38688+/* ---------------------------------------------------------------------- */
38689+
38690+int au_xino_path(struct seq_file *seq, struct file *file)
38691+{
38692+ int err;
38693+
38694+ err = au_seq_path(seq, &file->f_path);
38695+ if (unlikely(err))
38696+ goto out;
38697+
38698+#define Deleted "\\040(deleted)"
38699+ seq->count -= sizeof(Deleted) - 1;
38700+ AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
38701+ sizeof(Deleted) - 1));
38702+#undef Deleted
38703+
38704+out:
38705+ return err;
38706+}
38707diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
38708--- /usr/share/empty/include/uapi/linux/aufs_type.h 1970-01-01 01:00:00.000000000 +0100
38709+++ linux/include/uapi/linux/aufs_type.h 2021-12-03 15:40:58.239980630 +0100
38710@@ -0,0 +1,452 @@
38711+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
38712+/*
38713+ * Copyright (C) 2005-2021 Junjiro R. Okajima
38714+ *
38715+ * This program, aufs is free software; you can redistribute it and/or modify
38716+ * it under the terms of the GNU General Public License as published by
38717+ * the Free Software Foundation; either version 2 of the License, or
38718+ * (at your option) any later version.
38719+ *
38720+ * This program is distributed in the hope that it will be useful,
38721+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
38722+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38723+ * GNU General Public License for more details.
38724+ *
38725+ * You should have received a copy of the GNU General Public License
38726+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
38727+ */
38728+
38729+#ifndef __AUFS_TYPE_H__
38730+#define __AUFS_TYPE_H__
38731+
38732+#define AUFS_NAME "aufs"
38733+
38734+#ifdef __KERNEL__
38735+/*
38736+ * define it before including all other headers.
38737+ * sched.h may use pr_* macros before defining "current", so define the
38738+ * no-current version first, and re-define later.
38739+ */
38740+#define pr_fmt(fmt) AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
38741+#include <linux/sched.h>
38742+#undef pr_fmt
38743+#define pr_fmt(fmt) \
38744+ AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
38745+ (int)sizeof(current->comm), current->comm, current->pid
38746+#include <linux/limits.h>
38747+#else
38748+#include <stdint.h>
38749+#include <sys/types.h>
38750+#include <limits.h>
38751+#endif /* __KERNEL__ */
38752+
38753+#define AUFS_VERSION "5.15.5-20211129"
38754+
38755+/* todo? move this to linux-2.6.19/include/magic.h */
38756+#define AUFS_SUPER_MAGIC ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
38757+
38758+/* ---------------------------------------------------------------------- */
38759+
38760+#ifdef __KERNEL__
38761+#ifdef CONFIG_AUFS_BRANCH_MAX_127
38762+typedef int8_t aufs_bindex_t;
38763+#define AUFS_BRANCH_MAX 127
38764+#else
38765+typedef int16_t aufs_bindex_t;
38766+#ifdef CONFIG_AUFS_BRANCH_MAX_511
38767+#define AUFS_BRANCH_MAX 511
38768+#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
38769+#define AUFS_BRANCH_MAX 1023
38770+#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
38771+#define AUFS_BRANCH_MAX 32767
38772+#endif
38773+#endif
38774+
38775+#ifndef AUFS_BRANCH_MAX
38776+#error unknown CONFIG_AUFS_BRANCH_MAX value
38777+#endif
38778+#endif /* __KERNEL__ */
38779+
38780+/* ---------------------------------------------------------------------- */
38781+
38782+#define AUFS_FSTYPE AUFS_NAME
38783+
38784+#define AUFS_ROOT_INO 2
38785+#define AUFS_FIRST_INO 11
38786+
38787+#define AUFS_WH_PFX ".wh."
38788+#define AUFS_WH_PFX_LEN ((int)sizeof(AUFS_WH_PFX) - 1)
38789+#define AUFS_WH_TMP_LEN 4
38790+/* a limit for rmdir/rename a dir and copyup */
38791+#define AUFS_MAX_NAMELEN (NAME_MAX \
38792+ - AUFS_WH_PFX_LEN * 2 /* doubly whiteouted */\
38793+ - 1 /* dot */\
38794+ - AUFS_WH_TMP_LEN) /* hex */
38795+#define AUFS_XINO_FNAME "." AUFS_NAME ".xino"
38796+#define AUFS_XINO_DEFPATH "/tmp/" AUFS_XINO_FNAME
38797+#define AUFS_XINO_DEF_SEC 30 /* seconds */
38798+#define AUFS_XINO_DEF_TRUNC 45 /* percentage */
38799+#define AUFS_DIRWH_DEF 3
38800+#define AUFS_RDCACHE_DEF 10 /* seconds */
38801+#define AUFS_RDCACHE_MAX 3600 /* seconds */
38802+#define AUFS_RDBLK_DEF 512 /* bytes */
38803+#define AUFS_RDHASH_DEF 32
38804+#define AUFS_WKQ_NAME AUFS_NAME "d"
38805+#define AUFS_MFS_DEF_SEC 30 /* seconds */
38806+#define AUFS_MFS_MAX_SEC 3600 /* seconds */
38807+#define AUFS_FHSM_CACHE_DEF_SEC 30 /* seconds */
38808+#define AUFS_PLINK_WARN 50 /* number of plinks in a single bucket */
38809+
38810+/* pseudo-link maintenace under /proc */
38811+#define AUFS_PLINK_MAINT_NAME "plink_maint"
38812+#define AUFS_PLINK_MAINT_DIR "fs/" AUFS_NAME
38813+#define AUFS_PLINK_MAINT_PATH AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
38814+
38815+/* dirren, renamed dir */
38816+#define AUFS_DR_INFO_PFX AUFS_WH_PFX ".dr."
38817+#define AUFS_DR_BRHINO_NAME AUFS_WH_PFX "hino"
38818+/* whiteouted doubly */
38819+#define AUFS_WH_DR_INFO_PFX AUFS_WH_PFX AUFS_DR_INFO_PFX
38820+#define AUFS_WH_DR_BRHINO AUFS_WH_PFX AUFS_DR_BRHINO_NAME
38821+
38822+#define AUFS_DIROPQ_NAME AUFS_WH_PFX ".opq" /* whiteouted doubly */
38823+#define AUFS_WH_DIROPQ AUFS_WH_PFX AUFS_DIROPQ_NAME
38824+
38825+#define AUFS_BASE_NAME AUFS_WH_PFX AUFS_NAME
38826+#define AUFS_PLINKDIR_NAME AUFS_WH_PFX "plnk"
38827+#define AUFS_ORPHDIR_NAME AUFS_WH_PFX "orph"
38828+
38829+/* doubly whiteouted */
38830+#define AUFS_WH_BASE AUFS_WH_PFX AUFS_BASE_NAME
38831+#define AUFS_WH_PLINKDIR AUFS_WH_PFX AUFS_PLINKDIR_NAME
38832+#define AUFS_WH_ORPHDIR AUFS_WH_PFX AUFS_ORPHDIR_NAME
38833+
38834+/* branch permissions and attributes */
38835+#define AUFS_BRPERM_RW "rw"
38836+#define AUFS_BRPERM_RO "ro"
38837+#define AUFS_BRPERM_RR "rr"
38838+#define AUFS_BRATTR_COO_REG "coo_reg"
38839+#define AUFS_BRATTR_COO_ALL "coo_all"
38840+#define AUFS_BRATTR_FHSM "fhsm"
38841+#define AUFS_BRATTR_UNPIN "unpin"
38842+#define AUFS_BRATTR_ICEX "icex"
38843+#define AUFS_BRATTR_ICEX_SEC "icexsec"
38844+#define AUFS_BRATTR_ICEX_SYS "icexsys"
38845+#define AUFS_BRATTR_ICEX_TR "icextr"
38846+#define AUFS_BRATTR_ICEX_USR "icexusr"
38847+#define AUFS_BRATTR_ICEX_OTH "icexoth"
38848+#define AUFS_BRRATTR_WH "wh"
38849+#define AUFS_BRWATTR_NLWH "nolwh"
38850+#define AUFS_BRWATTR_MOO "moo"
38851+
38852+#define AuBrPerm_RW 1 /* writable, hardlinkable wh */
38853+#define AuBrPerm_RO (1 << 1) /* readonly */
38854+#define AuBrPerm_RR (1 << 2) /* natively readonly */
38855+#define AuBrPerm_Mask (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
38856+
38857+#define AuBrAttr_COO_REG (1 << 3) /* copy-up on open */
38858+#define AuBrAttr_COO_ALL (1 << 4)
38859+#define AuBrAttr_COO_Mask (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
38860+
38861+#define AuBrAttr_FHSM (1 << 5) /* file-based hsm */
38862+#define AuBrAttr_UNPIN (1 << 6) /* rename-able top dir of
38863+ branch. meaningless since
38864+ linux-3.18-rc1 */
38865+
38866+/* ignore error in copying XATTR */
38867+#define AuBrAttr_ICEX_SEC (1 << 7)
38868+#define AuBrAttr_ICEX_SYS (1 << 8)
38869+#define AuBrAttr_ICEX_TR (1 << 9)
38870+#define AuBrAttr_ICEX_USR (1 << 10)
38871+#define AuBrAttr_ICEX_OTH (1 << 11)
38872+#define AuBrAttr_ICEX (AuBrAttr_ICEX_SEC \
38873+ | AuBrAttr_ICEX_SYS \
38874+ | AuBrAttr_ICEX_TR \
38875+ | AuBrAttr_ICEX_USR \
38876+ | AuBrAttr_ICEX_OTH)
38877+
38878+#define AuBrRAttr_WH (1 << 12) /* whiteout-able */
38879+#define AuBrRAttr_Mask AuBrRAttr_WH
38880+
38881+#define AuBrWAttr_NoLinkWH (1 << 13) /* un-hardlinkable whiteouts */
38882+#define AuBrWAttr_MOO (1 << 14) /* move-up on open */
38883+#define AuBrWAttr_Mask (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
38884+
38885+#define AuBrAttr_CMOO_Mask (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
38886+
38887+/* #warning test userspace */
38888+#ifdef __KERNEL__
38889+#ifndef CONFIG_AUFS_FHSM
38890+#undef AuBrAttr_FHSM
38891+#define AuBrAttr_FHSM 0
38892+#endif
38893+#ifndef CONFIG_AUFS_XATTR
38894+#undef AuBrAttr_ICEX
38895+#define AuBrAttr_ICEX 0
38896+#undef AuBrAttr_ICEX_SEC
38897+#define AuBrAttr_ICEX_SEC 0
38898+#undef AuBrAttr_ICEX_SYS
38899+#define AuBrAttr_ICEX_SYS 0
38900+#undef AuBrAttr_ICEX_TR
38901+#define AuBrAttr_ICEX_TR 0
38902+#undef AuBrAttr_ICEX_USR
38903+#define AuBrAttr_ICEX_USR 0
38904+#undef AuBrAttr_ICEX_OTH
38905+#define AuBrAttr_ICEX_OTH 0
38906+#endif
38907+#endif
38908+
38909+/* the longest combination */
38910+/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
38911+#define AuBrPermStrSz sizeof(AUFS_BRPERM_RW \
38912+ "+" AUFS_BRATTR_COO_REG \
38913+ "+" AUFS_BRATTR_FHSM \
38914+ "+" AUFS_BRATTR_UNPIN \
38915+ "+" AUFS_BRATTR_ICEX_SEC \
38916+ "+" AUFS_BRATTR_ICEX_SYS \
38917+ "+" AUFS_BRATTR_ICEX_USR \
38918+ "+" AUFS_BRATTR_ICEX_OTH \
38919+ "+" AUFS_BRWATTR_NLWH)
38920+
38921+typedef struct {
38922+ char a[AuBrPermStrSz];
38923+} au_br_perm_str_t;
38924+
38925+static inline int au_br_writable(int brperm)
38926+{
38927+ return brperm & AuBrPerm_RW;
38928+}
38929+
38930+static inline int au_br_whable(int brperm)
38931+{
38932+ return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
38933+}
38934+
38935+static inline int au_br_wh_linkable(int brperm)
38936+{
38937+ return !(brperm & AuBrWAttr_NoLinkWH);
38938+}
38939+
38940+static inline int au_br_cmoo(int brperm)
38941+{
38942+ return brperm & AuBrAttr_CMOO_Mask;
38943+}
38944+
38945+static inline int au_br_fhsm(int brperm)
38946+{
38947+ return brperm & AuBrAttr_FHSM;
38948+}
38949+
38950+/* ---------------------------------------------------------------------- */
38951+
38952+/* ioctl */
38953+enum {
38954+ /* readdir in userspace */
38955+ AuCtl_RDU,
38956+ AuCtl_RDU_INO,
38957+
38958+ AuCtl_WBR_FD, /* pathconf wrapper */
38959+ AuCtl_IBUSY, /* busy inode */
38960+ AuCtl_MVDOWN, /* move-down */
38961+ AuCtl_BR, /* info about branches */
38962+ AuCtl_FHSM_FD /* connection for fhsm */
38963+};
38964+
38965+/* borrowed from linux/include/linux/kernel.h */
38966+#ifndef ALIGN
38967+#ifdef _GNU_SOURCE
38968+#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
38969+#else
38970+#define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
38971+#endif
38972+#define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
38973+#endif
38974+
38975+/* borrowed from linux/include/linux/compiler-gcc3.h */
38976+#ifndef __aligned
38977+#define __aligned(x) __attribute__((aligned(x)))
38978+#endif
38979+
38980+#ifdef __KERNEL__
38981+#ifndef __packed
38982+#define __packed __attribute__((packed))
38983+#endif
38984+#endif
38985+
38986+struct au_rdu_cookie {
38987+ uint64_t h_pos;
38988+ int16_t bindex;
38989+ uint8_t flags;
38990+ uint8_t pad;
38991+ uint32_t generation;
38992+} __aligned(8);
38993+
38994+struct au_rdu_ent {
38995+ uint64_t ino;
38996+ int16_t bindex;
38997+ uint8_t type;
38998+ uint8_t nlen;
38999+ uint8_t wh;
39000+ char name[];
39001+} __aligned(8);
39002+
39003+static inline int au_rdu_len(int nlen)
39004+{
39005+ /* include the terminating NULL */
39006+ return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
39007+ sizeof(uint64_t));
39008+}
39009+
39010+union au_rdu_ent_ul {
39011+ struct au_rdu_ent __user *e;
39012+ uint64_t ul;
39013+};
39014+
39015+enum {
39016+ AufsCtlRduV_SZ,
39017+ AufsCtlRduV_End
39018+};
39019+
39020+struct aufs_rdu {
39021+ /* input */
39022+ union {
39023+ uint64_t sz; /* AuCtl_RDU */
39024+ uint64_t nent; /* AuCtl_RDU_INO */
39025+ };
39026+ union au_rdu_ent_ul ent;
39027+ uint16_t verify[AufsCtlRduV_End];
39028+
39029+ /* input/output */
39030+ uint32_t blk;
39031+
39032+ /* output */
39033+ union au_rdu_ent_ul tail;
39034+ /* number of entries which were added in a single call */
39035+ uint64_t rent;
39036+ uint8_t full;
39037+ uint8_t shwh;
39038+
39039+ struct au_rdu_cookie cookie;
39040+} __aligned(8);
39041+
39042+/* ---------------------------------------------------------------------- */
39043+
39044+/* dirren. the branch is identified by the filename who contains this */
39045+struct au_drinfo {
39046+ uint64_t ino;
39047+ union {
39048+ uint8_t oldnamelen;
39049+ uint64_t _padding;
39050+ };
39051+ uint8_t oldname[];
39052+} __aligned(8);
39053+
39054+struct au_drinfo_fdata {
39055+ uint32_t magic;
39056+ struct au_drinfo drinfo;
39057+} __aligned(8);
39058+
39059+#define AUFS_DRINFO_MAGIC_V1 ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x01)
39060+/* future */
39061+#define AUFS_DRINFO_MAGIC_V2 ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x02)
39062+
39063+/* ---------------------------------------------------------------------- */
39064+
39065+struct aufs_wbr_fd {
39066+ uint32_t oflags;
39067+ int16_t brid;
39068+} __aligned(8);
39069+
39070+/* ---------------------------------------------------------------------- */
39071+
39072+struct aufs_ibusy {
39073+ uint64_t ino, h_ino;
39074+ int16_t bindex;
39075+} __aligned(8);
39076+
39077+/* ---------------------------------------------------------------------- */
39078+
39079+/* error code for move-down */
39080+/* the actual message strings are implemented in aufs-util.git */
39081+enum {
39082+ EAU_MVDOWN_OPAQUE = 1,
39083+ EAU_MVDOWN_WHITEOUT,
39084+ EAU_MVDOWN_UPPER,
39085+ EAU_MVDOWN_BOTTOM,
39086+ EAU_MVDOWN_NOUPPER,
39087+ EAU_MVDOWN_NOLOWERBR,
39088+ EAU_Last
39089+};
39090+
39091+/* flags for move-down */
39092+#define AUFS_MVDOWN_DMSG 1
39093+#define AUFS_MVDOWN_OWLOWER (1 << 1) /* overwrite lower */
39094+#define AUFS_MVDOWN_KUPPER (1 << 2) /* keep upper */
39095+#define AUFS_MVDOWN_ROLOWER (1 << 3) /* do even if lower is RO */
39096+#define AUFS_MVDOWN_ROLOWER_R (1 << 4) /* did on lower RO */
39097+#define AUFS_MVDOWN_ROUPPER (1 << 5) /* do even if upper is RO */
39098+#define AUFS_MVDOWN_ROUPPER_R (1 << 6) /* did on upper RO */
39099+#define AUFS_MVDOWN_BRID_UPPER (1 << 7) /* upper brid */
39100+#define AUFS_MVDOWN_BRID_LOWER (1 << 8) /* lower brid */
39101+#define AUFS_MVDOWN_FHSM_LOWER (1 << 9) /* find fhsm attr for lower */
39102+#define AUFS_MVDOWN_STFS (1 << 10) /* req. stfs */
39103+#define AUFS_MVDOWN_STFS_FAILED (1 << 11) /* output: stfs is unusable */
39104+#define AUFS_MVDOWN_BOTTOM (1 << 12) /* output: no more lowers */
39105+
39106+/* index for move-down */
39107+enum {
39108+ AUFS_MVDOWN_UPPER,
39109+ AUFS_MVDOWN_LOWER,
39110+ AUFS_MVDOWN_NARRAY
39111+};
39112+
39113+/*
39114+ * additional info of move-down
39115+ * number of free blocks and inodes.
39116+ * subset of struct kstatfs, but smaller and always 64bit.
39117+ */
39118+struct aufs_stfs {
39119+ uint64_t f_blocks;
39120+ uint64_t f_bavail;
39121+ uint64_t f_files;
39122+ uint64_t f_ffree;
39123+};
39124+
39125+struct aufs_stbr {
39126+ int16_t brid; /* optional input */
39127+ int16_t bindex; /* output */
39128+ struct aufs_stfs stfs; /* output when AUFS_MVDOWN_STFS set */
39129+} __aligned(8);
39130+
39131+struct aufs_mvdown {
39132+ uint32_t flags; /* input/output */
39133+ struct aufs_stbr stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
39134+ int8_t au_errno; /* output */
39135+} __aligned(8);
39136+
39137+/* ---------------------------------------------------------------------- */
39138+
39139+union aufs_brinfo {
39140+ /* PATH_MAX may differ between kernel-space and user-space */
39141+ char _spacer[4096];
39142+ struct {
39143+ int16_t id;
39144+ int perm;
39145+ char path[];
39146+ };
39147+} __aligned(8);
39148+
39149+/* ---------------------------------------------------------------------- */
39150+
39151+#define AuCtlType 'A'
39152+#define AUFS_CTL_RDU _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
39153+#define AUFS_CTL_RDU_INO _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
39154+#define AUFS_CTL_WBR_FD _IOW(AuCtlType, AuCtl_WBR_FD, \
39155+ struct aufs_wbr_fd)
39156+#define AUFS_CTL_IBUSY _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
39157+#define AUFS_CTL_MVDOWN _IOWR(AuCtlType, AuCtl_MVDOWN, \
39158+ struct aufs_mvdown)
39159+#define AUFS_CTL_BRINFO _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
39160+#define AUFS_CTL_FHSM_FD _IOW(AuCtlType, AuCtl_FHSM_FD, int)
39161+
39162+#endif /* __AUFS_TYPE_H__ */
39163SPDX-License-Identifier: GPL-2.0
39164aufs5.15.5 loopback patch
39165
39166diff --git a/drivers/block/loop.c b/drivers/block/loop.c
39167index c9d1ef703673c..ca8b0b1146a60 100644
39168--- a/drivers/block/loop.c
39169+++ b/drivers/block/loop.c
39170@@ -672,6 +672,15 @@ static inline void loop_update_dio(struct loop_device *lo)
39171 lo->use_dio);
39172 }
39173
39174+static struct file *loop_real_file(struct file *file)
39175+{
39176+ struct file *f = NULL;
39177+
39178+ if (file->f_path.dentry->d_sb->s_op->real_loop)
39179+ f = file->f_path.dentry->d_sb->s_op->real_loop(file);
39180+ return f;
39181+}
39182+
39183 static void loop_reread_partitions(struct loop_device *lo)
39184 {
39185 int rc;
39186@@ -729,6 +738,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39187 {
39188 struct file *file = fget(arg);
39189 struct file *old_file;
39190+ struct file *f, *virt_file = NULL, *old_virt_file;
39191 int error;
39192 bool partscan;
39193 bool is_loop;
39194@@ -748,11 +758,19 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39195 if (!(lo->lo_flags & LO_FLAGS_READ_ONLY))
39196 goto out_err;
39197
39198+ f = loop_real_file(file);
39199+ if (f) {
39200+ virt_file = file;
39201+ file = f;
39202+ get_file(file);
39203+ }
39204+
39205 error = loop_validate_file(file, bdev);
39206 if (error)
39207 goto out_err;
39208
39209 old_file = lo->lo_backing_file;
39210+ old_virt_file = lo->lo_backing_virt_file;
39211
39212 error = -EINVAL;
39213
39214@@ -765,6 +783,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39215 blk_mq_freeze_queue(lo->lo_queue);
39216 mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
39217 lo->lo_backing_file = file;
39218+ lo->lo_backing_virt_file = virt_file;
39219 lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
39220 mapping_set_gfp_mask(file->f_mapping,
39221 lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39222@@ -787,6 +806,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39223 * dependency.
39224 */
39225 fput(old_file);
39226+ if (old_virt_file)
39227+ fput(old_virt_file);
39228 if (partscan)
39229 loop_reread_partitions(lo);
39230 return 0;
39231@@ -795,6 +816,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39232 loop_global_unlock(lo, is_loop);
39233 out_putf:
39234 fput(file);
39235+ if (virt_file)
39236+ fput(virt_file);
39237 return error;
39238 }
39239
39240@@ -1195,6 +1218,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39241 const struct loop_config *config)
39242 {
39243 struct file *file = fget(config->fd);
39244+ struct file *f, *virt_file = NULL;
39245 struct inode *inode;
39246 struct address_space *mapping;
39247 int error;
39248@@ -1210,6 +1234,13 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39249 /* This is safe, since we have a reference from open(). */
39250 __module_get(THIS_MODULE);
39251
39252+ f = loop_real_file(file);
39253+ if (f) {
39254+ virt_file = file;
39255+ file = f;
39256+ get_file(file);
39257+ }
39258+
39259 /*
39260 * If we don't hold exclusive handle for the device, upgrade to it
39261 * here to avoid changing device under exclusive owner.
39262@@ -1275,6 +1306,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39263 lo->use_dio = lo->lo_flags & LO_FLAGS_DIRECT_IO;
39264 lo->lo_device = bdev;
39265 lo->lo_backing_file = file;
39266+ lo->lo_backing_virt_file = virt_file;
39267 lo->old_gfp_mask = mapping_gfp_mask(mapping);
39268 mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39269
39270@@ -1325,6 +1357,8 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39271 bd_abort_claiming(bdev, loop_configure);
39272 out_putf:
39273 fput(file);
39274+ if (virt_file)
39275+ fput(virt_file);
39276 /* This is safe: open() is still holding a reference. */
39277 module_put(THIS_MODULE);
39278 return error;
39279@@ -1333,6 +1367,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39280 static int __loop_clr_fd(struct loop_device *lo, bool release)
39281 {
39282 struct file *filp = NULL;
39283+ struct file *virt_filp = lo->lo_backing_virt_file;
39284 gfp_t gfp = lo->old_gfp_mask;
39285 struct block_device *bdev = lo->lo_device;
39286 int err = 0;
39287@@ -1384,6 +1419,7 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
39288
39289 spin_lock_irq(&lo->lo_lock);
39290 lo->lo_backing_file = NULL;
39291+ lo->lo_backing_virt_file = NULL;
39292 spin_unlock_irq(&lo->lo_lock);
39293
39294 loop_release_xfer(lo);
39295@@ -1464,6 +1500,8 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
39296 */
39297 if (filp)
39298 fput(filp);
39299+ if (virt_filp)
39300+ fput(virt_filp);
39301 return err;
39302 }
39303
39304diff --git a/drivers/block/loop.h b/drivers/block/loop.h
39305index 04c88dd6eabd6..0ff3ba22ee170 100644
39306--- a/drivers/block/loop.h
39307+++ b/drivers/block/loop.h
39308@@ -46,7 +46,7 @@ struct loop_device {
39309 int (*ioctl)(struct loop_device *, int cmd,
39310 unsigned long arg);
39311
39312- struct file * lo_backing_file;
39313+ struct file *lo_backing_file, *lo_backing_virt_file;
39314 struct block_device *lo_device;
39315 void *key_data;
39316
39317diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
39318index 99ceca1440449..e49dfe8550329 100644
39319--- a/fs/aufs/f_op.c
39320+++ b/fs/aufs/f_op.c
39321@@ -304,7 +304,7 @@ static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
39322 if (IS_ERR(h_file))
39323 goto out;
39324
39325- if (au_test_loopback_kthread()) {
39326+ if (0 && au_test_loopback_kthread()) {
39327 au_warn_loopback(h_file->f_path.dentry->d_sb);
39328 if (file->f_mapping != h_file->f_mapping) {
39329 file->f_mapping = h_file->f_mapping;
39330diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
39331index 74347bd75b380..5ef888a1d53f4 100644
39332--- a/fs/aufs/loop.c
39333+++ b/fs/aufs/loop.c
39334@@ -133,3 +133,19 @@ void au_loopback_fin(void)
39335 symbol_put(loop_backing_file);
39336 au_kfree_try_rcu(au_warn_loopback_array);
39337 }
39338+
39339+/* ---------------------------------------------------------------------- */
39340+
39341+/* support the loopback block device insude aufs */
39342+
39343+struct file *aufs_real_loop(struct file *file)
39344+{
39345+ struct file *f;
39346+
39347+ BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
39348+ fi_read_lock(file);
39349+ f = au_hf_top(file);
39350+ fi_read_unlock(file);
39351+ AuDebugOn(!f);
39352+ return f;
39353+}
39354diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
39355index 7293bee427f96..3345c098d0d47 100644
39356--- a/fs/aufs/loop.h
39357+++ b/fs/aufs/loop.h
39358@@ -26,6 +26,8 @@ void au_warn_loopback(struct super_block *h_sb);
39359
39360 int au_loopback_init(void);
39361 void au_loopback_fin(void);
39362+
39363+struct file *aufs_real_loop(struct file *file);
39364 #else
39365 AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
39366
39367@@ -36,6 +38,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
39368
39369 AuStubInt0(au_loopback_init, void)
39370 AuStubVoid(au_loopback_fin, void)
39371+
39372+AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
39373 #endif /* BLK_DEV_LOOP */
39374
39375 #endif /* __KERNEL__ */
39376diff --git a/fs/aufs/super.c b/fs/aufs/super.c
39377index ae38857541533..af32f0960e595 100644
39378--- a/fs/aufs/super.c
39379+++ b/fs/aufs/super.c
39380@@ -844,7 +844,10 @@ static const struct super_operations aufs_sop = {
39381 .statfs = aufs_statfs,
39382 .put_super = aufs_put_super,
39383 .sync_fs = aufs_sync_fs,
39384- .remount_fs = aufs_remount_fs
39385+ .remount_fs = aufs_remount_fs,
39386+#ifdef CONFIG_AUFS_BDEV_LOOP
39387+ .real_loop = aufs_real_loop
39388+#endif
39389 };
39390
39391 /* ---------------------------------------------------------------------- */
39392diff --git a/include/linux/fs.h b/include/linux/fs.h
39393index e60d8ad854004..2ac5317f9b795 100644
39394--- a/include/linux/fs.h
39395+++ b/include/linux/fs.h
39396@@ -2226,6 +2226,10 @@ struct super_operations {
39397 struct shrink_control *);
39398 long (*free_cached_objects)(struct super_block *,
39399 struct shrink_control *);
39400+#if IS_ENABLED(CONFIG_BLK_DEV_LOOP) || IS_ENABLED(CONFIG_BLK_DEV_LOOP_MODULE)
39401+ /* and aufs */
39402+ struct file *(*real_loop)(struct file *);
39403+#endif
39404 };
39405
39406 /*
This page took 0.559984 seconds and 4 git commands to generate.