]> git.pld-linux.org Git - packages/kernel.git/blame_incremental - kernel-aufs5.patch
- parametrize aufs upstream branch
[packages/kernel.git] / kernel-aufs5.patch
... / ...
CommitLineData
1SPDX-License-Identifier: GPL-2.0
2aufs5.x-rcN kbuild patch
3
4diff --git a/fs/Kconfig b/fs/Kconfig
5index 141a856c50e71..6741d695b1df8 100644
6--- a/fs/Kconfig
7+++ b/fs/Kconfig
8@@ -292,6 +292,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 9c708e1fbe8fb..fde6be84a0830 100644
18--- a/fs/Makefile
19+++ b/fs/Makefile
20@@ -136,3 +136,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.x-rcN base patch
27
28diff --git a/MAINTAINERS b/MAINTAINERS
29index bd7aff0c120f2..7c4bc08596e0b 100644
30--- a/MAINTAINERS
31+++ b/MAINTAINERS
32@@ -3040,6 +3040,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 d58d68f3c7cd0..794a8a1341989 100644
54--- a/drivers/block/loop.c
55+++ b/drivers/block/loop.c
56@@ -762,6 +762,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 dfc72f15be7fc..d8a12eb63961d 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/inode.c b/fs/inode.c
117index c93500d84264d..f24d21236ad01 100644
118--- a/fs/inode.c
119+++ b/fs/inode.c
120@@ -1778,7 +1778,7 @@ EXPORT_SYMBOL(generic_update_time);
121 * This does the actual work of updating an inodes time or version. Must have
122 * had called mnt_want_write() before calling this.
123 */
124-static int update_time(struct inode *inode, struct timespec64 *time, int flags)
125+int update_time(struct inode *inode, struct timespec64 *time, int flags)
126 {
127 if (inode->i_op->update_time)
128 return inode->i_op->update_time(inode, time, flags);
129diff --git a/fs/namespace.c b/fs/namespace.c
130index f63337828e1c4..099e89c81af7c 100644
131--- a/fs/namespace.c
132+++ b/fs/namespace.c
133@@ -807,6 +807,12 @@ static inline int check_mnt(struct mount *mnt)
134 return mnt->mnt_ns == current->nsproxy->mnt_ns;
135 }
136
137+/* for aufs, CONFIG_AUFS_BR_FUSE */
138+int is_current_mnt_ns(struct vfsmount *mnt)
139+{
140+ return check_mnt(real_mount(mnt));
141+}
142+
143 /*
144 * vfsmount lock must be held for write
145 */
146diff --git a/fs/splice.c b/fs/splice.c
147index 5dbce4dcc1a7d..3e6ba363b7775 100644
148--- a/fs/splice.c
149+++ b/fs/splice.c
150@@ -759,8 +759,8 @@ static int warn_unsupported(struct file *file, const char *op)
151 /*
152 * Attempt to initiate a splice from pipe to file.
153 */
154-static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
155- loff_t *ppos, size_t len, unsigned int flags)
156+long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
157+ loff_t *ppos, size_t len, unsigned int flags)
158 {
159 if (unlikely(!out->f_op->splice_write))
160 return warn_unsupported(out, "write");
161@@ -770,9 +770,9 @@ static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
162 /*
163 * Attempt to initiate a splice from a file to a pipe.
164 */
165-static long do_splice_to(struct file *in, loff_t *ppos,
166- struct pipe_inode_info *pipe, size_t len,
167- unsigned int flags)
168+long do_splice_to(struct file *in, loff_t *ppos,
169+ struct pipe_inode_info *pipe, size_t len,
170+ unsigned int flags)
171 {
172 unsigned int p_space;
173 int ret;
174diff --git a/fs/sync.c b/fs/sync.c
175index 1373a610dc784..b7b5a0a0df6ff 100644
176--- a/fs/sync.c
177+++ b/fs/sync.c
178@@ -28,7 +28,7 @@
179 * wait == 1 case since in that case write_inode() functions do
180 * sync_dirty_buffer() and thus effectively write one block at a time.
181 */
182-static int __sync_filesystem(struct super_block *sb, int wait)
183+int __sync_filesystem(struct super_block *sb, int wait)
184 {
185 if (wait)
186 sync_inodes_sb(sb);
187diff --git a/include/linux/fs.h b/include/linux/fs.h
188index c3c88fdb9b2a5..93eb43e002d97 100644
189--- a/include/linux/fs.h
190+++ b/include/linux/fs.h
191@@ -1335,6 +1335,7 @@ extern void fasync_free(struct fasync_struct *);
192 /* can be called from interrupts */
193 extern void kill_fasync(struct fasync_struct **, int, int);
194
195+extern int setfl(int fd, struct file *filp, unsigned long arg);
196 extern void __f_setown(struct file *filp, struct pid *, enum pid_type, int force);
197 extern int f_setown(struct file *filp, unsigned long arg, int force);
198 extern void f_delown(struct file *filp);
199@@ -2043,6 +2044,7 @@ struct file_operations {
200 ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
201 unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
202 int (*check_flags)(int);
203+ int (*setfl)(struct file *, unsigned long);
204 int (*flock) (struct file *, int, struct file_lock *);
205 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
206 ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
207@@ -2565,6 +2567,7 @@ extern int current_umask(void);
208 extern void ihold(struct inode * inode);
209 extern void iput(struct inode *);
210 extern int generic_update_time(struct inode *, struct timespec64 *, int);
211+extern int update_time(struct inode *, struct timespec64 *, int);
212
213 /* /sys/fs */
214 extern struct kobject *fs_kobj;
215@@ -2805,6 +2808,7 @@ static inline bool sb_is_blkdev_sb(struct super_block *sb)
216 }
217
218 void emergency_thaw_all(void);
219+extern int __sync_filesystem(struct super_block *, int);
220 extern int sync_filesystem(struct super_block *);
221 extern const struct file_operations def_blk_fops;
222 extern const struct file_operations def_chr_fops;
223diff --git a/include/linux/lockdep.h b/include/linux/lockdep.h
224index 5cf3878137549..59b65e1a9e85c 100644
225--- a/include/linux/lockdep.h
226+++ b/include/linux/lockdep.h
227@@ -248,6 +248,8 @@ static inline int lockdep_match_key(struct lockdep_map *lock,
228 return lock->key == key;
229 }
230
231+struct lock_class *lockdep_hlock_class(struct held_lock *hlock);
232+
233 /*
234 * Acquire a lock.
235 *
236diff --git a/include/linux/mnt_namespace.h b/include/linux/mnt_namespace.h
237index 8f882f5881e87..6b9808f098435 100644
238--- a/include/linux/mnt_namespace.h
239+++ b/include/linux/mnt_namespace.h
240@@ -7,12 +7,15 @@ struct mnt_namespace;
241 struct fs_struct;
242 struct user_namespace;
243 struct ns_common;
244+struct vfsmount;
245
246 extern struct mnt_namespace *copy_mnt_ns(unsigned long, struct mnt_namespace *,
247 struct user_namespace *, struct fs_struct *);
248 extern void put_mnt_ns(struct mnt_namespace *ns);
249 extern struct ns_common *from_mnt_ns(struct mnt_namespace *);
250
251+extern int is_current_mnt_ns(struct vfsmount *mnt);
252+
253 extern const struct file_operations proc_mounts_operations;
254 extern const struct file_operations proc_mountinfo_operations;
255 extern const struct file_operations proc_mountstats_operations;
256diff --git a/include/linux/splice.h b/include/linux/splice.h
257index a55179fd60fc3..8e21c53cf8831 100644
258--- a/include/linux/splice.h
259+++ b/include/linux/splice.h
260@@ -93,4 +93,10 @@ extern void splice_shrink_spd(struct splice_pipe_desc *);
261
262 extern const struct pipe_buf_operations page_cache_pipe_buf_ops;
263 extern const struct pipe_buf_operations default_pipe_buf_ops;
264+
265+extern long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
266+ loff_t *ppos, size_t len, unsigned int flags);
267+extern long do_splice_to(struct file *in, loff_t *ppos,
268+ struct pipe_inode_info *pipe, size_t len,
269+ unsigned int flags);
270 #endif
271diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
272index 48d736aa03b24..dce278159546c 100644
273--- a/kernel/locking/lockdep.c
274+++ b/kernel/locking/lockdep.c
275@@ -189,7 +189,7 @@ static
276 struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
277 static DECLARE_BITMAP(lock_classes_in_use, MAX_LOCKDEP_KEYS);
278
279-static inline struct lock_class *hlock_class(struct held_lock *hlock)
280+inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
281 {
282 unsigned int class_idx = hlock->class_idx;
283
284@@ -210,6 +210,7 @@ static inline struct lock_class *hlock_class(struct held_lock *hlock)
285 */
286 return lock_classes + class_idx;
287 }
288+#define hlock_class(hlock) lockdep_hlock_class(hlock)
289
290 #ifdef CONFIG_LOCK_STAT
291 static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
292SPDX-License-Identifier: GPL-2.0
293aufs5.x-rcN mmap patch
294
295diff --git a/fs/proc/base.c b/fs/proc/base.c
296index 3851bfcdba56e..d632ddd5f5ee8 100644
297--- a/fs/proc/base.c
298+++ b/fs/proc/base.c
299@@ -2183,7 +2183,7 @@ static int map_files_get_link(struct dentry *dentry, struct path *path)
300 rc = -ENOENT;
301 vma = find_exact_vma(mm, vm_start, vm_end);
302 if (vma && vma->vm_file) {
303- *path = vma->vm_file->f_path;
304+ *path = vma_pr_or_file(vma)->f_path;
305 path_get(path);
306 rc = 0;
307 }
308diff --git a/fs/proc/nommu.c b/fs/proc/nommu.c
309index 13452b32e2bd5..38acccfef9d49 100644
310--- a/fs/proc/nommu.c
311+++ b/fs/proc/nommu.c
312@@ -40,7 +40,10 @@ static int nommu_region_show(struct seq_file *m, struct vm_region *region)
313 file = region->vm_file;
314
315 if (file) {
316- struct inode *inode = file_inode(region->vm_file);
317+ struct inode *inode;
318+
319+ file = vmr_pr_or_file(region);
320+ inode = file_inode(file);
321 dev = inode->i_sb->s_dev;
322 ino = inode->i_ino;
323 }
324diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
325index fc9784544b241..84ba06f5158e9 100644
326--- a/fs/proc/task_mmu.c
327+++ b/fs/proc/task_mmu.c
328@@ -280,7 +280,10 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
329 const char *name = NULL;
330
331 if (file) {
332- struct inode *inode = file_inode(vma->vm_file);
333+ struct inode *inode;
334+
335+ file = vma_pr_or_file(vma);
336+ inode = file_inode(file);
337 dev = inode->i_sb->s_dev;
338 ino = inode->i_ino;
339 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
340@@ -1864,7 +1867,7 @@ static int show_numa_map(struct seq_file *m, void *v)
341 struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
342 struct vm_area_struct *vma = v;
343 struct numa_maps *md = &numa_priv->md;
344- struct file *file = vma->vm_file;
345+ struct file *file = vma_pr_or_file(vma);
346 struct mm_struct *mm = vma->vm_mm;
347 struct mempolicy *pol;
348 char buffer[64];
349diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c
350index a6d21fc0033c6..02c2de31196e0 100644
351--- a/fs/proc/task_nommu.c
352+++ b/fs/proc/task_nommu.c
353@@ -155,7 +155,10 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma)
354 file = vma->vm_file;
355
356 if (file) {
357- struct inode *inode = file_inode(vma->vm_file);
358+ struct inode *inode;
359+
360+ file = vma_pr_or_file(vma);
361+ inode = file_inode(file);
362 dev = inode->i_sb->s_dev;
363 ino = inode->i_ino;
364 pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
365diff --git a/include/linux/mm.h b/include/linux/mm.h
366index 322ec61d0da79..ae6f0584e4c98 100644
367--- a/include/linux/mm.h
368+++ b/include/linux/mm.h
369@@ -1798,6 +1798,28 @@ static inline void unmap_shared_mapping_range(struct address_space *mapping,
370 unmap_mapping_range(mapping, holebegin, holelen, 0);
371 }
372
373+extern void vma_do_file_update_time(struct vm_area_struct *, const char[], int);
374+extern struct file *vma_do_pr_or_file(struct vm_area_struct *, const char[],
375+ int);
376+extern void vma_do_get_file(struct vm_area_struct *, const char[], int);
377+extern void vma_do_fput(struct vm_area_struct *, const char[], int);
378+
379+#define vma_file_update_time(vma) vma_do_file_update_time(vma, __func__, \
380+ __LINE__)
381+#define vma_pr_or_file(vma) vma_do_pr_or_file(vma, __func__, \
382+ __LINE__)
383+#define vma_get_file(vma) vma_do_get_file(vma, __func__, __LINE__)
384+#define vma_fput(vma) vma_do_fput(vma, __func__, __LINE__)
385+
386+#ifndef CONFIG_MMU
387+extern struct file *vmr_do_pr_or_file(struct vm_region *, const char[], int);
388+extern void vmr_do_fput(struct vm_region *, const char[], int);
389+
390+#define vmr_pr_or_file(region) vmr_do_pr_or_file(region, __func__, \
391+ __LINE__)
392+#define vmr_fput(region) vmr_do_fput(region, __func__, __LINE__)
393+#endif /* !CONFIG_MMU */
394+
395 extern int access_process_vm(struct task_struct *tsk, unsigned long addr,
396 void *buf, int len, unsigned int gup_flags);
397 extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
398diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
399index 6613b26a88946..e94df45b5483a 100644
400--- a/include/linux/mm_types.h
401+++ b/include/linux/mm_types.h
402@@ -279,6 +279,7 @@ struct vm_region {
403 unsigned long vm_top; /* region allocated to here */
404 unsigned long vm_pgoff; /* the offset in vm_file corresponding to vm_start */
405 struct file *vm_file; /* the backing file or NULL */
406+ struct file *vm_prfile; /* the virtual backing file or NULL */
407
408 int vm_usage; /* region usage count (access under nommu_region_sem) */
409 bool vm_icache_flushed : 1; /* true if the icache has been flushed for
410@@ -358,6 +359,7 @@ struct vm_area_struct {
411 unsigned long vm_pgoff; /* Offset (within vm_file) in PAGE_SIZE
412 units */
413 struct file * vm_file; /* File we map to (can be NULL). */
414+ struct file *vm_prfile; /* shadow of vm_file */
415 void * vm_private_data; /* was vm_pte (shared mem) */
416
417 #ifdef CONFIG_SWAP
418diff --git a/kernel/fork.c b/kernel/fork.c
419index dc06afd725cbd..66f1486e63e35 100644
420--- a/kernel/fork.c
421+++ b/kernel/fork.c
422@@ -559,7 +559,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
423 struct inode *inode = file_inode(file);
424 struct address_space *mapping = file->f_mapping;
425
426- get_file(file);
427+ vma_get_file(tmp);
428 if (tmp->vm_flags & VM_DENYWRITE)
429 put_write_access(inode);
430 i_mmap_lock_write(mapping);
431diff --git a/mm/Makefile b/mm/Makefile
432index bf71e295e9f69..bd223b81c564c 100644
433--- a/mm/Makefile
434+++ b/mm/Makefile
435@@ -52,7 +52,7 @@ obj-y := filemap.o mempool.o oom_kill.o fadvise.o \
436 mm_init.o percpu.o slab_common.o \
437 compaction.o vmacache.o \
438 interval_tree.o list_lru.o workingset.o \
439- debug.o gup.o mmap_lock.o $(mmu-y)
440+ prfile.o debug.o gup.o mmap_lock.o $(mmu-y)
441
442 # Give 'page_alloc' its own module-parameter namespace
443 page-alloc-y := page_alloc.o
444diff --git a/mm/filemap.c b/mm/filemap.c
445index 66f7e9fdfbc4f..f9a8ff48e697a 100644
446--- a/mm/filemap.c
447+++ b/mm/filemap.c
448@@ -3240,7 +3240,7 @@ vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
449 vm_fault_t ret = VM_FAULT_LOCKED;
450
451 sb_start_pagefault(mapping->host->i_sb);
452- file_update_time(vmf->vma->vm_file);
453+ vma_file_update_time(vmf->vma);
454 lock_page(page);
455 if (page->mapping != mapping) {
456 unlock_page(page);
457diff --git a/mm/mmap.c b/mm/mmap.c
458index 0584e540246e1..8b2f082002a9c 100644
459--- a/mm/mmap.c
460+++ b/mm/mmap.c
461@@ -185,7 +185,7 @@ static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
462 if (vma->vm_ops && vma->vm_ops->close)
463 vma->vm_ops->close(vma);
464 if (vma->vm_file)
465- fput(vma->vm_file);
466+ vma_fput(vma);
467 mpol_put(vma_policy(vma));
468 vm_area_free(vma);
469 return next;
470@@ -955,7 +955,7 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
471 if (remove_next) {
472 if (file) {
473 uprobe_munmap(next, next->vm_start, next->vm_end);
474- fput(file);
475+ vma_fput(vma);
476 }
477 if (next->anon_vma)
478 anon_vma_merge(vma, next);
479@@ -1901,7 +1901,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
480 return addr;
481
482 unmap_and_free_vma:
483- fput(vma->vm_file);
484+ vma_fput(vma);
485 vma->vm_file = NULL;
486
487 /* Undo any partial mapping done by a device driver. */
488@@ -2761,7 +2761,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
489 goto out_free_mpol;
490
491 if (new->vm_file)
492- get_file(new->vm_file);
493+ vma_get_file(new);
494
495 if (new->vm_ops && new->vm_ops->open)
496 new->vm_ops->open(new);
497@@ -2780,7 +2780,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
498 if (new->vm_ops && new->vm_ops->close)
499 new->vm_ops->close(new);
500 if (new->vm_file)
501- fput(new->vm_file);
502+ vma_fput(new);
503 unlink_anon_vmas(new);
504 out_free_mpol:
505 mpol_put(vma_policy(new));
506@@ -2973,7 +2973,7 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
507 struct vm_area_struct *vma;
508 unsigned long populate = 0;
509 unsigned long ret = -EINVAL;
510- struct file *file;
511+ struct file *file, *prfile;
512
513 pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/vm/remap_file_pages.rst.\n",
514 current->comm, current->pid);
515@@ -3032,10 +3032,27 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
516 if (vma->vm_flags & VM_LOCKED)
517 flags |= MAP_LOCKED;
518
519- file = get_file(vma->vm_file);
520+ vma_get_file(vma);
521+ file = vma->vm_file;
522+ prfile = vma->vm_prfile;
523 ret = do_mmap(vma->vm_file, start, size,
524 prot, flags, pgoff, &populate, NULL);
525+ if (!IS_ERR_VALUE(ret) && file && prfile) {
526+ struct vm_area_struct *new_vma;
527+
528+ new_vma = find_vma(mm, ret);
529+ if (!new_vma->vm_prfile)
530+ new_vma->vm_prfile = prfile;
531+ if (new_vma != vma)
532+ get_file(prfile);
533+ }
534+ /*
535+ * two fput()s instead of vma_fput(vma),
536+ * coz vma may not be available anymore.
537+ */
538 fput(file);
539+ if (prfile)
540+ fput(prfile);
541 out:
542 mmap_write_unlock(mm);
543 if (populate)
544@@ -3322,7 +3339,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
545 if (anon_vma_clone(new_vma, vma))
546 goto out_free_mempol;
547 if (new_vma->vm_file)
548- get_file(new_vma->vm_file);
549+ vma_get_file(new_vma);
550 if (new_vma->vm_ops && new_vma->vm_ops->open)
551 new_vma->vm_ops->open(new_vma);
552 vma_link(mm, new_vma, prev, rb_link, rb_parent);
553diff --git a/mm/nommu.c b/mm/nommu.c
554index 85a3a68dffb68..a2bee44172ccc 100644
555--- a/mm/nommu.c
556+++ b/mm/nommu.c
557@@ -523,7 +523,7 @@ static void __put_nommu_region(struct vm_region *region)
558 up_write(&nommu_region_sem);
559
560 if (region->vm_file)
561- fput(region->vm_file);
562+ vmr_fput(region);
563
564 /* IO memory and memory shared directly out of the pagecache
565 * from ramfs/tmpfs mustn't be released here */
566@@ -655,7 +655,7 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
567 if (vma->vm_ops && vma->vm_ops->close)
568 vma->vm_ops->close(vma);
569 if (vma->vm_file)
570- fput(vma->vm_file);
571+ vma_fput(vma);
572 put_nommu_region(vma->vm_region);
573 vm_area_free(vma);
574 }
575@@ -1178,7 +1178,7 @@ unsigned long do_mmap(struct file *file,
576 goto error_just_free;
577 }
578 }
579- fput(region->vm_file);
580+ vmr_fput(region);
581 kmem_cache_free(vm_region_jar, region);
582 region = pregion;
583 result = start;
584@@ -1255,10 +1255,10 @@ unsigned long do_mmap(struct file *file,
585 up_write(&nommu_region_sem);
586 error:
587 if (region->vm_file)
588- fput(region->vm_file);
589+ vmr_fput(region);
590 kmem_cache_free(vm_region_jar, region);
591 if (vma->vm_file)
592- fput(vma->vm_file);
593+ vma_fput(vma);
594 vm_area_free(vma);
595 return ret;
596
597diff --git a/mm/prfile.c b/mm/prfile.c
598new file mode 100644
599index 0000000000000..00d51187c3250
600--- /dev/null
601+++ b/mm/prfile.c
602@@ -0,0 +1,86 @@
603+// SPDX-License-Identifier: GPL-2.0
604+/*
605+ * Mainly for aufs which mmap(2) different file and wants to print different
606+ * path in /proc/PID/maps.
607+ * Call these functions via macros defined in linux/mm.h.
608+ *
609+ * See Documentation/filesystems/aufs/design/06mmap.txt
610+ *
611+ * Copyright (c) 2014-2020 Junjro R. Okajima
612+ * Copyright (c) 2014 Ian Campbell
613+ */
614+
615+#include <linux/mm.h>
616+#include <linux/file.h>
617+#include <linux/fs.h>
618+
619+/* #define PRFILE_TRACE */
620+static inline void prfile_trace(struct file *f, struct file *pr,
621+ const char func[], int line, const char func2[])
622+{
623+#ifdef PRFILE_TRACE
624+ if (pr)
625+ pr_info("%s:%d: %s, %pD2\n", func, line, func2, f);
626+#endif
627+}
628+
629+void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
630+ int line)
631+{
632+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
633+
634+ prfile_trace(f, pr, func, line, __func__);
635+ file_update_time(f);
636+ if (f && pr)
637+ file_update_time(pr);
638+}
639+
640+struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
641+ int line)
642+{
643+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
644+
645+ prfile_trace(f, pr, func, line, __func__);
646+ return (f && pr) ? pr : f;
647+}
648+
649+void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
650+{
651+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
652+
653+ prfile_trace(f, pr, func, line, __func__);
654+ get_file(f);
655+ if (f && pr)
656+ get_file(pr);
657+}
658+
659+void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
660+{
661+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
662+
663+ prfile_trace(f, pr, func, line, __func__);
664+ fput(f);
665+ if (f && pr)
666+ fput(pr);
667+}
668+
669+#ifndef CONFIG_MMU
670+struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
671+ int line)
672+{
673+ struct file *f = region->vm_file, *pr = region->vm_prfile;
674+
675+ prfile_trace(f, pr, func, line, __func__);
676+ return (f && pr) ? pr : f;
677+}
678+
679+void vmr_do_fput(struct vm_region *region, const char func[], int line)
680+{
681+ struct file *f = region->vm_file, *pr = region->vm_prfile;
682+
683+ prfile_trace(f, pr, func, line, __func__);
684+ fput(f);
685+ if (f && pr)
686+ fput(pr);
687+}
688+#endif /* !CONFIG_MMU */
689SPDX-License-Identifier: GPL-2.0
690aufs5.x-rcN standalone patch
691
692diff --git a/fs/dcache.c b/fs/dcache.c
693index bc5095b734f58..9508bd57a3bc0 100644
694--- a/fs/dcache.c
695+++ b/fs/dcache.c
696@@ -1425,6 +1425,7 @@ void d_walk(struct dentry *parent, void *data,
697 seq = 1;
698 goto again;
699 }
700+EXPORT_SYMBOL_GPL(d_walk);
701
702 struct check_mount {
703 struct vfsmount *mnt;
704@@ -2970,6 +2971,7 @@ void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
705
706 write_sequnlock(&rename_lock);
707 }
708+EXPORT_SYMBOL_GPL(d_exchange);
709
710 /**
711 * d_ancestor - search for an ancestor
712diff --git a/fs/exec.c b/fs/exec.c
713index 18594f11c31fe..ac38c0424d2a3 100644
714--- a/fs/exec.c
715+++ b/fs/exec.c
716@@ -114,6 +114,7 @@ bool path_noexec(const struct path *path)
717 return (path->mnt->mnt_flags & MNT_NOEXEC) ||
718 (path->mnt->mnt_sb->s_iflags & SB_I_NOEXEC);
719 }
720+EXPORT_SYMBOL_GPL(path_noexec);
721
722 #ifdef CONFIG_USELIB
723 /*
724diff --git a/fs/fcntl.c b/fs/fcntl.c
725index d8a12eb63961d..0532996186311 100644
726--- a/fs/fcntl.c
727+++ b/fs/fcntl.c
728@@ -86,6 +86,7 @@ int setfl(int fd, struct file *filp, unsigned long arg)
729 out:
730 return error;
731 }
732+EXPORT_SYMBOL_GPL(setfl);
733
734 static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
735 int force)
736diff --git a/fs/file_table.c b/fs/file_table.c
737index 45437f8e1003e..786af52904fcf 100644
738--- a/fs/file_table.c
739+++ b/fs/file_table.c
740@@ -161,6 +161,7 @@ struct file *alloc_empty_file(int flags, const struct cred *cred)
741 }
742 return ERR_PTR(-ENFILE);
743 }
744+EXPORT_SYMBOL_GPL(alloc_empty_file);
745
746 /*
747 * Variant of alloc_empty_file() that doesn't check and modify nr_files.
748@@ -375,6 +376,7 @@ void __fput_sync(struct file *file)
749 }
750
751 EXPORT_SYMBOL(fput);
752+EXPORT_SYMBOL_GPL(__fput_sync);
753
754 void __init files_init(void)
755 {
756diff --git a/fs/inode.c b/fs/inode.c
757index f24d21236ad01..78de5b5dc0840 100644
758--- a/fs/inode.c
759+++ b/fs/inode.c
760@@ -1784,6 +1784,7 @@ int update_time(struct inode *inode, struct timespec64 *time, int flags)
761 return inode->i_op->update_time(inode, time, flags);
762 return generic_update_time(inode, time, flags);
763 }
764+EXPORT_SYMBOL_GPL(update_time);
765
766 /**
767 * atime_needs_update - update the access time
768diff --git a/fs/namespace.c b/fs/namespace.c
769index 099e89c81af7c..3e46b53795b1e 100644
770--- a/fs/namespace.c
771+++ b/fs/namespace.c
772@@ -438,6 +438,7 @@ void __mnt_drop_write(struct vfsmount *mnt)
773 mnt_dec_writers(real_mount(mnt));
774 preempt_enable();
775 }
776+EXPORT_SYMBOL_GPL(__mnt_drop_write);
777
778 /**
779 * mnt_drop_write - give up write access to a mount
780@@ -812,6 +813,7 @@ int is_current_mnt_ns(struct vfsmount *mnt)
781 {
782 return check_mnt(real_mount(mnt));
783 }
784+EXPORT_SYMBOL_GPL(is_current_mnt_ns);
785
786 /*
787 * vfsmount lock must be held for write
788@@ -1987,6 +1989,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
789 }
790 return 0;
791 }
792+EXPORT_SYMBOL_GPL(iterate_mounts);
793
794 static void lock_mnt_tree(struct mount *mnt)
795 {
796diff --git a/fs/notify/group.c b/fs/notify/group.c
797index fb89c351295d6..460ad19c2570a 100644
798--- a/fs/notify/group.c
799+++ b/fs/notify/group.c
800@@ -100,6 +100,7 @@ void fsnotify_get_group(struct fsnotify_group *group)
801 {
802 refcount_inc(&group->refcnt);
803 }
804+EXPORT_SYMBOL_GPL(fsnotify_get_group);
805
806 /*
807 * Drop a reference to a group. Free it if it's through.
808diff --git a/fs/open.c b/fs/open.c
809index e53af13b5835f..f37da065e6544 100644
810--- a/fs/open.c
811+++ b/fs/open.c
812@@ -65,6 +65,7 @@ int do_truncate(struct user_namespace *mnt_userns, struct dentry *dentry,
813 inode_unlock(dentry->d_inode);
814 return ret;
815 }
816+EXPORT_SYMBOL_GPL(do_truncate);
817
818 long vfs_truncate(const struct path *path, loff_t length)
819 {
820diff --git a/fs/read_write.c b/fs/read_write.c
821index 9db7adf160d20..8dc93a57a933f 100644
822--- a/fs/read_write.c
823+++ b/fs/read_write.c
824@@ -503,6 +503,7 @@ ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
825 inc_syscr(current);
826 return ret;
827 }
828+EXPORT_SYMBOL_GPL(vfs_read);
829
830 static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
831 {
832@@ -613,6 +614,7 @@ ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_
833 file_end_write(file);
834 return ret;
835 }
836+EXPORT_SYMBOL_GPL(vfs_write);
837
838 /* file_ppos returns &file->f_pos or NULL if file is stream */
839 static inline loff_t *file_ppos(struct file *file)
840diff --git a/fs/splice.c b/fs/splice.c
841index 3e6ba363b7775..7c1be373eb7cd 100644
842--- a/fs/splice.c
843+++ b/fs/splice.c
844@@ -766,6 +766,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
845 return warn_unsupported(out, "write");
846 return out->f_op->splice_write(pipe, out, ppos, len, flags);
847 }
848+EXPORT_SYMBOL_GPL(do_splice_from);
849
850 /*
851 * Attempt to initiate a splice from a file to a pipe.
852@@ -795,6 +796,7 @@ long do_splice_to(struct file *in, loff_t *ppos,
853 return warn_unsupported(in, "read");
854 return in->f_op->splice_read(in, ppos, pipe, len, flags);
855 }
856+EXPORT_SYMBOL_GPL(do_splice_to);
857
858 /**
859 * splice_direct_to_actor - splices data directly between two non-pipes
860diff --git a/fs/sync.c b/fs/sync.c
861index b7b5a0a0df6ff..fa5c7fba7f1ba 100644
862--- a/fs/sync.c
863+++ b/fs/sync.c
864@@ -39,6 +39,7 @@ int __sync_filesystem(struct super_block *sb, int wait)
865 sb->s_op->sync_fs(sb, wait);
866 return __sync_blockdev(sb->s_bdev, wait);
867 }
868+EXPORT_SYMBOL_GPL(__sync_filesystem);
869
870 /*
871 * Write out and wait upon all dirty data associated with this
872diff --git a/fs/xattr.c b/fs/xattr.c
873index 5c8c5175b385c..ff7e9ff774b73 100644
874--- a/fs/xattr.c
875+++ b/fs/xattr.c
876@@ -384,6 +384,7 @@ vfs_getxattr_alloc(struct user_namespace *mnt_userns, struct dentry *dentry,
877 *xattr_value = value;
878 return error;
879 }
880+EXPORT_SYMBOL_GPL(vfs_getxattr_alloc);
881
882 ssize_t
883 __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
884diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
885index dce278159546c..5b0c571dbca2f 100644
886--- a/kernel/locking/lockdep.c
887+++ b/kernel/locking/lockdep.c
888@@ -210,6 +210,7 @@ inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
889 */
890 return lock_classes + class_idx;
891 }
892+EXPORT_SYMBOL_GPL(lockdep_hlock_class);
893 #define hlock_class(hlock) lockdep_hlock_class(hlock)
894
895 #ifdef CONFIG_LOCK_STAT
896diff --git a/kernel/task_work.c b/kernel/task_work.c
897index 1698fbe6f0e13..081b05acadf82 100644
898--- a/kernel/task_work.c
899+++ b/kernel/task_work.c
900@@ -167,3 +167,4 @@ void task_work_run(void)
901 } while (work);
902 }
903 }
904+EXPORT_SYMBOL_GPL(task_work_run);
905diff --git a/security/security.c b/security/security.c
906index b38155b2de83f..386c2741886ce 100644
907--- a/security/security.c
908+++ b/security/security.c
909@@ -1146,6 +1146,7 @@ int security_path_rmdir(const struct path *dir, struct dentry *dentry)
910 return 0;
911 return call_int_hook(path_rmdir, 0, dir, dentry);
912 }
913+EXPORT_SYMBOL_GPL(security_path_rmdir);
914
915 int security_path_unlink(const struct path *dir, struct dentry *dentry)
916 {
917@@ -1162,6 +1163,7 @@ int security_path_symlink(const struct path *dir, struct dentry *dentry,
918 return 0;
919 return call_int_hook(path_symlink, 0, dir, dentry, old_name);
920 }
921+EXPORT_SYMBOL_GPL(security_path_symlink);
922
923 int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
924 struct dentry *new_dentry)
925@@ -1170,6 +1172,7 @@ int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
926 return 0;
927 return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
928 }
929+EXPORT_SYMBOL_GPL(security_path_link);
930
931 int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
932 const struct path *new_dir, struct dentry *new_dentry,
933@@ -1197,6 +1200,7 @@ int security_path_truncate(const struct path *path)
934 return 0;
935 return call_int_hook(path_truncate, 0, path);
936 }
937+EXPORT_SYMBOL_GPL(security_path_truncate);
938
939 int security_path_chmod(const struct path *path, umode_t mode)
940 {
941@@ -1204,6 +1208,7 @@ int security_path_chmod(const struct path *path, umode_t mode)
942 return 0;
943 return call_int_hook(path_chmod, 0, path, mode);
944 }
945+EXPORT_SYMBOL_GPL(security_path_chmod);
946
947 int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
948 {
949@@ -1211,6 +1216,7 @@ int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
950 return 0;
951 return call_int_hook(path_chown, 0, path, uid, gid);
952 }
953+EXPORT_SYMBOL_GPL(security_path_chown);
954
955 int security_path_chroot(const struct path *path)
956 {
957@@ -1311,6 +1317,7 @@ int security_inode_permission(struct inode *inode, int mask)
958 return 0;
959 return call_int_hook(inode_permission, 0, inode, mask);
960 }
961+EXPORT_SYMBOL_GPL(security_inode_permission);
962
963 int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
964 {
965@@ -1508,6 +1515,7 @@ int security_file_permission(struct file *file, int mask)
966
967 return fsnotify_perm(file, mask);
968 }
969+EXPORT_SYMBOL_GPL(security_file_permission);
970
971 int security_file_alloc(struct file *file)
972 {
973diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
974--- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs 1970-01-01 01:00:00.000000000 +0100
975+++ linux/Documentation/ABI/testing/debugfs-aufs 2021-06-30 21:35:11.393873211 +0200
976@@ -0,0 +1,55 @@
977+What: /debug/aufs/si_<id>/
978+Date: March 2009
979+Contact: J. R. Okajima <hooanon05g@gmail.com>
980+Description:
981+ Under /debug/aufs, a directory named si_<id> is created
982+ per aufs mount, where <id> is a unique id generated
983+ internally.
984+
985+What: /debug/aufs/si_<id>/plink
986+Date: Apr 2013
987+Contact: J. R. Okajima <hooanon05g@gmail.com>
988+Description:
989+ It has three lines and shows the information about the
990+ pseudo-link. The first line is a single number
991+ representing a number of buckets. The second line is a
992+ number of pseudo-links per buckets (separated by a
993+ blank). The last line is a single number representing a
994+ total number of psedo-links.
995+ When the aufs mount option 'noplink' is specified, it
996+ will show "1\n0\n0\n".
997+
998+What: /debug/aufs/si_<id>/xib
999+Date: March 2009
1000+Contact: J. R. Okajima <hooanon05g@gmail.com>
1001+Description:
1002+ It shows the consumed blocks by xib (External Inode Number
1003+ Bitmap), its block size and file size.
1004+ When the aufs mount option 'noxino' is specified, it
1005+ will be empty. About XINO files, see the aufs manual.
1006+
1007+What: /debug/aufs/si_<id>/xi<branch-index>
1008+Date: March 2009
1009+Contact: J. R. Okajima <hooanon05g@gmail.com>
1010+Description:
1011+ It shows the consumed blocks by xino (External Inode Number
1012+ Translation Table), its link count, block size and file
1013+ size.
1014+ Due to the file size limit, there may exist multiple
1015+ xino files per branch. In this case, "-N" is added to
1016+ the filename and it corresponds to the index of the
1017+ internal xino array. "-0" is omitted.
1018+ When the aufs mount option 'noxino' is specified, Those
1019+ entries won't exist. About XINO files, see the aufs
1020+ manual.
1021+
1022+What: /debug/aufs/si_<id>/xigen
1023+Date: March 2009
1024+Contact: J. R. Okajima <hooanon05g@gmail.com>
1025+Description:
1026+ It shows the consumed blocks by xigen (External Inode
1027+ Generation Table), its block size and file size.
1028+ If CONFIG_AUFS_EXPORT is disabled, this entry will not
1029+ be created.
1030+ When the aufs mount option 'noxino' is specified, it
1031+ will be empty. About XINO files, see the aufs manual.
1032diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
1033--- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs 1970-01-01 01:00:00.000000000 +0100
1034+++ linux/Documentation/ABI/testing/sysfs-aufs 2021-06-30 21:35:11.393873211 +0200
1035@@ -0,0 +1,31 @@
1036+What: /sys/fs/aufs/si_<id>/
1037+Date: March 2009
1038+Contact: J. R. Okajima <hooanon05g@gmail.com>
1039+Description:
1040+ Under /sys/fs/aufs, a directory named si_<id> is created
1041+ per aufs mount, where <id> is a unique id generated
1042+ internally.
1043+
1044+What: /sys/fs/aufs/si_<id>/br<idx>
1045+Date: March 2009
1046+Contact: J. R. Okajima <hooanon05g@gmail.com>
1047+Description:
1048+ It shows the abolute path of a member directory (which
1049+ is called branch) in aufs, and its permission.
1050+
1051+What: /sys/fs/aufs/si_<id>/brid<idx>
1052+Date: July 2013
1053+Contact: J. R. Okajima <hooanon05g@gmail.com>
1054+Description:
1055+ It shows the id of a member directory (which is called
1056+ branch) in aufs.
1057+
1058+What: /sys/fs/aufs/si_<id>/xi_path
1059+Date: March 2009
1060+Contact: J. R. Okajima <hooanon05g@gmail.com>
1061+Description:
1062+ It shows the abolute path of XINO (External Inode Number
1063+ Bitmap, Translation Table and Generation Table) file
1064+ even if it is the default path.
1065+ When the aufs mount option 'noxino' is specified, it
1066+ will be empty. About XINO files, see the aufs manual.
1067diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
1068--- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt 1970-01-01 01:00:00.000000000 +0100
1069+++ linux/Documentation/filesystems/aufs/design/01intro.txt 2021-02-24 13:33:42.737680181 +0100
1070@@ -0,0 +1,171 @@
1071+
1072+# Copyright (C) 2005-2020 Junjiro R. Okajima
1073+#
1074+# This program is free software; you can redistribute it and/or modify
1075+# it under the terms of the GNU General Public License as published by
1076+# the Free Software Foundation; either version 2 of the License, or
1077+# (at your option) any later version.
1078+#
1079+# This program is distributed in the hope that it will be useful,
1080+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1081+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1082+# GNU General Public License for more details.
1083+#
1084+# You should have received a copy of the GNU General Public License
1085+# along with this program. If not, see <http://www.gnu.org/licenses/>.
1086+
1087+Introduction
1088+----------------------------------------
1089+
1090+aufs [ei ju: ef es] | /ey-yoo-ef-es/ | [a u f s]
1091+1. abbrev. for "advanced multi-layered unification filesystem".
1092+2. abbrev. for "another unionfs".
1093+3. abbrev. for "auf das" in German which means "on the" in English.
1094+ Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
1095+ But "Filesystem aufs Filesystem" is hard to understand.
1096+4. abbrev. for "African Urban Fashion Show".
1097+
1098+AUFS is a filesystem with features:
1099+- multi layered stackable unification filesystem, the member directory
1100+ is called as a branch.
1101+- branch permission and attribute, 'readonly', 'real-readonly',
1102+ 'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
1103+ combination.
1104+- internal "file copy-on-write".
1105+- logical deletion, whiteout.
1106+- dynamic branch manipulation, adding, deleting and changing permission.
1107+- allow bypassing aufs, user's direct branch access.
1108+- external inode number translation table and bitmap which maintains the
1109+ persistent aufs inode number.
1110+- seekable directory, including NFS readdir.
1111+- file mapping, mmap and sharing pages.
1112+- pseudo-link, hardlink over branches.
1113+- loopback mounted filesystem as a branch.
1114+- several policies to select one among multiple writable branches.
1115+- revert a single systemcall when an error occurs in aufs.
1116+- and more...
1117+
1118+
1119+Multi Layered Stackable Unification Filesystem
1120+----------------------------------------------------------------------
1121+Most people already knows what it is.
1122+It is a filesystem which unifies several directories and provides a
1123+merged single directory. When users access a file, the access will be
1124+passed/re-directed/converted (sorry, I am not sure which English word is
1125+correct) to the real file on the member filesystem. The member
1126+filesystem is called 'lower filesystem' or 'branch' and has a mode
1127+'readonly' and 'readwrite.' And the deletion for a file on the lower
1128+readonly branch is handled by creating 'whiteout' on the upper writable
1129+branch.
1130+
1131+On LKML, there have been discussions about UnionMount (Jan Blunck,
1132+Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
1133+different approaches to implement the merged-view.
1134+The former tries putting it into VFS, and the latter implements as a
1135+separate filesystem.
1136+(If I misunderstand about these implementations, please let me know and
1137+I shall correct it. Because it is a long time ago when I read their
1138+source files last time).
1139+
1140+UnionMount's approach will be able to small, but may be hard to share
1141+branches between several UnionMount since the whiteout in it is
1142+implemented in the inode on branch filesystem and always
1143+shared. According to Bharata's post, readdir does not seems to be
1144+finished yet.
1145+There are several missing features known in this implementations such as
1146+- for users, the inode number may change silently. eg. copy-up.
1147+- link(2) may break by copy-up.
1148+- read(2) may get an obsoleted filedata (fstat(2) too).
1149+- fcntl(F_SETLK) may be broken by copy-up.
1150+- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
1151+ open(O_RDWR).
1152+
1153+In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
1154+merged into mainline. This is another implementation of UnionMount as a
1155+separated filesystem. All the limitations and known problems which
1156+UnionMount are equally inherited to "overlay" filesystem.
1157+
1158+Unionfs has a longer history. When I started implementing a stackable
1159+filesystem (Aug 2005), it already existed. It has virtual super_block,
1160+inode, dentry and file objects and they have an array pointing lower
1161+same kind objects. After contributing many patches for Unionfs, I
1162+re-started my project AUFS (Jun 2006).
1163+
1164+In AUFS, the structure of filesystem resembles to Unionfs, but I
1165+implemented my own ideas, approaches and enhancements and it became
1166+totally different one.
1167+
1168+Comparing DM snapshot and fs based implementation
1169+- the number of bytes to be copied between devices is much smaller.
1170+- the type of filesystem must be one and only.
1171+- the fs must be writable, no readonly fs, even for the lower original
1172+ device. so the compression fs will not be usable. but if we use
1173+ loopback mount, we may address this issue.
1174+ for instance,
1175+ mount /cdrom/squashfs.img /sq
1176+ losetup /sq/ext2.img
1177+ losetup /somewhere/cow
1178+ dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1179+- it will be difficult (or needs more operations) to extract the
1180+ difference between the original device and COW.
1181+- DM snapshot-merge may help a lot when users try merging. in the
1182+ fs-layer union, users will use rsync(1).
1183+
1184+You may want to read my old paper "Filesystems in LiveCD"
1185+(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
1186+
1187+
1188+Several characters/aspects/persona of aufs
1189+----------------------------------------------------------------------
1190+
1191+Aufs has several characters, aspects or persona.
1192+1. a filesystem, callee of VFS helper
1193+2. sub-VFS, caller of VFS helper for branches
1194+3. a virtual filesystem which maintains persistent inode number
1195+4. reader/writer of files on branches such like an application
1196+
1197+1. Callee of VFS Helper
1198+As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1199+unlink(2) from an application reaches sys_unlink() kernel function and
1200+then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1201+calls filesystem specific unlink operation. Actually aufs implements the
1202+unlink operation but it behaves like a redirector.
1203+
1204+2. Caller of VFS Helper for Branches
1205+aufs_unlink() passes the unlink request to the branch filesystem as if
1206+it were called from VFS. So the called unlink operation of the branch
1207+filesystem acts as usual. As a caller of VFS helper, aufs should handle
1208+every necessary pre/post operation for the branch filesystem.
1209+- acquire the lock for the parent dir on a branch
1210+- lookup in a branch
1211+- revalidate dentry on a branch
1212+- mnt_want_write() for a branch
1213+- vfs_unlink() for a branch
1214+- mnt_drop_write() for a branch
1215+- release the lock on a branch
1216+
1217+3. Persistent Inode Number
1218+One of the most important issue for a filesystem is to maintain inode
1219+numbers. This is particularly important to support exporting a
1220+filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1221+backend block device for its own. But some storage is necessary to
1222+keep and maintain the inode numbers. It may be a large space and may not
1223+suit to keep in memory. Aufs rents some space from its first writable
1224+branch filesystem (by default) and creates file(s) on it. These files
1225+are created by aufs internally and removed soon (currently) keeping
1226+opened.
1227+Note: Because these files are removed, they are totally gone after
1228+ unmounting aufs. It means the inode numbers are not persistent
1229+ across unmount or reboot. I have a plan to make them really
1230+ persistent which will be important for aufs on NFS server.
1231+
1232+4. Read/Write Files Internally (copy-on-write)
1233+Because a branch can be readonly, when you write a file on it, aufs will
1234+"copy-up" it to the upper writable branch internally. And then write the
1235+originally requested thing to the file. Generally kernel doesn't
1236+open/read/write file actively. In aufs, even a single write may cause a
1237+internal "file copy". This behaviour is very similar to cp(1) command.
1238+
1239+Some people may think it is better to pass such work to user space
1240+helper, instead of doing in kernel space. Actually I am still thinking
1241+about it. But currently I have implemented it in kernel space.
1242diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
1243--- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
1244+++ linux/Documentation/filesystems/aufs/design/02struct.txt 2021-02-24 13:33:42.737680181 +0100
1245@@ -0,0 +1,258 @@
1246+
1247+# Copyright (C) 2005-2020 Junjiro R. Okajima
1248+#
1249+# This program is free software; you can redistribute it and/or modify
1250+# it under the terms of the GNU General Public License as published by
1251+# the Free Software Foundation; either version 2 of the License, or
1252+# (at your option) any later version.
1253+#
1254+# This program is distributed in the hope that it will be useful,
1255+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1256+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1257+# GNU General Public License for more details.
1258+#
1259+# You should have received a copy of the GNU General Public License
1260+# along with this program. If not, see <http://www.gnu.org/licenses/>.
1261+
1262+Basic Aufs Internal Structure
1263+
1264+Superblock/Inode/Dentry/File Objects
1265+----------------------------------------------------------------------
1266+As like an ordinary filesystem, aufs has its own
1267+superblock/inode/dentry/file objects. All these objects have a
1268+dynamically allocated array and store the same kind of pointers to the
1269+lower filesystem, branch.
1270+For example, when you build a union with one readwrite branch and one
1271+readonly, mounted /au, /rw and /ro respectively.
1272+- /au = /rw + /ro
1273+- /ro/fileA exists but /rw/fileA
1274+
1275+Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1276+pointers are stored in a aufs dentry. The array in aufs dentry will be,
1277+- [0] = NULL (because /rw/fileA doesn't exist)
1278+- [1] = /ro/fileA
1279+
1280+This style of an array is essentially same to the aufs
1281+superblock/inode/dentry/file objects.
1282+
1283+Because aufs supports manipulating branches, ie. add/delete/change
1284+branches dynamically, these objects has its own generation. When
1285+branches are changed, the generation in aufs superblock is
1286+incremented. And a generation in other object are compared when it is
1287+accessed. When a generation in other objects are obsoleted, aufs
1288+refreshes the internal array.
1289+
1290+
1291+Superblock
1292+----------------------------------------------------------------------
1293+Additionally aufs superblock has some data for policies to select one
1294+among multiple writable branches, XIB files, pseudo-links and kobject.
1295+See below in detail.
1296+About the policies which supports copy-down a directory, see
1297+wbr_policy.txt too.
1298+
1299+
1300+Branch and XINO(External Inode Number Translation Table)
1301+----------------------------------------------------------------------
1302+Every branch has its own xino (external inode number translation table)
1303+file. The xino file is created and unlinked by aufs internally. When two
1304+members of a union exist on the same filesystem, they share the single
1305+xino file.
1306+The struct of a xino file is simple, just a sequence of aufs inode
1307+numbers which is indexed by the lower inode number.
1308+In the above sample, assume the inode number of /ro/fileA is i111 and
1309+aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1310+4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1311+
1312+When the inode numbers are not contiguous, the xino file will be sparse
1313+which has a hole in it and doesn't consume as much disk space as it
1314+might appear. If your branch filesystem consumes disk space for such
1315+holes, then you should specify 'xino=' option at mounting aufs.
1316+
1317+Aufs has a mount option to free the disk blocks for such holes in XINO
1318+files on tmpfs or ramdisk. But it is not so effective actually. If you
1319+meet a problem of disk shortage due to XINO files, then you should try
1320+"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1321+The patch localizes the assignment inumbers per tmpfs-mount and avoid
1322+the holes in XINO files.
1323+
1324+Also a writable branch has three kinds of "whiteout bases". All these
1325+are existed when the branch is joined to aufs, and their names are
1326+whiteout-ed doubly, so that users will never see their names in aufs
1327+hierarchy.
1328+1. a regular file which will be hardlinked to all whiteouts.
1329+2. a directory to store a pseudo-link.
1330+3. a directory to store an "orphan"-ed file temporary.
1331+
1332+1. Whiteout Base
1333+ When you remove a file on a readonly branch, aufs handles it as a
1334+ logical deletion and creates a whiteout on the upper writable branch
1335+ as a hardlink of this file in order not to consume inode on the
1336+ writable branch.
1337+2. Pseudo-link Dir
1338+ See below, Pseudo-link.
1339+3. Step-Parent Dir
1340+ When "fileC" exists on the lower readonly branch only and it is
1341+ opened and removed with its parent dir, and then user writes
1342+ something into it, then aufs copies-up fileC to this
1343+ directory. Because there is no other dir to store fileC. After
1344+ creating a file under this dir, the file is unlinked.
1345+
1346+Because aufs supports manipulating branches, ie. add/delete/change
1347+dynamically, a branch has its own id. When the branch order changes,
1348+aufs finds the new index by searching the branch id.
1349+
1350+
1351+Pseudo-link
1352+----------------------------------------------------------------------
1353+Assume "fileA" exists on the lower readonly branch only and it is
1354+hardlinked to "fileB" on the branch. When you write something to fileA,
1355+aufs copies-up it to the upper writable branch. Additionally aufs
1356+creates a hardlink under the Pseudo-link Directory of the writable
1357+branch. The inode of a pseudo-link is kept in aufs super_block as a
1358+simple list. If fileB is read after unlinking fileA, aufs returns
1359+filedata from the pseudo-link instead of the lower readonly
1360+branch. Because the pseudo-link is based upon the inode, to keep the
1361+inode number by xino (see above) is essentially necessary.
1362+
1363+All the hardlinks under the Pseudo-link Directory of the writable branch
1364+should be restored in a proper location later. Aufs provides a utility
1365+to do this. The userspace helpers executed at remounting and unmounting
1366+aufs by default.
1367+During this utility is running, it puts aufs into the pseudo-link
1368+maintenance mode. In this mode, only the process which began the
1369+maintenance mode (and its child processes) is allowed to operate in
1370+aufs. Some other processes which are not related to the pseudo-link will
1371+be allowed to run too, but the rest have to return an error or wait
1372+until the maintenance mode ends. If a process already acquires an inode
1373+mutex (in VFS), it has to return an error.
1374+
1375+
1376+XIB(external inode number bitmap)
1377+----------------------------------------------------------------------
1378+Addition to the xino file per a branch, aufs has an external inode number
1379+bitmap in a superblock object. It is also an internal file such like a
1380+xino file.
1381+It is a simple bitmap to mark whether the aufs inode number is in-use or
1382+not.
1383+To reduce the file I/O, aufs prepares a single memory page to cache xib.
1384+
1385+As well as XINO files, aufs has a feature to truncate/refresh XIB to
1386+reduce the number of consumed disk blocks for these files.
1387+
1388+
1389+Virtual or Vertical Dir, and Readdir in Userspace
1390+----------------------------------------------------------------------
1391+In order to support multiple layers (branches), aufs readdir operation
1392+constructs a virtual dir block on memory. For readdir, aufs calls
1393+vfs_readdir() internally for each dir on branches, merges their entries
1394+with eliminating the whiteout-ed ones, and sets it to file (dir)
1395+object. So the file object has its entry list until it is closed. The
1396+entry list will be updated when the file position is zero and becomes
1397+obsoleted. This decision is made in aufs automatically.
1398+
1399+The dynamically allocated memory block for the name of entries has a
1400+unit of 512 bytes (by default) and stores the names contiguously (no
1401+padding). Another block for each entry is handled by kmem_cache too.
1402+During building dir blocks, aufs creates hash list and judging whether
1403+the entry is whiteouted by its upper branch or already listed.
1404+The merged result is cached in the corresponding inode object and
1405+maintained by a customizable life-time option.
1406+
1407+Some people may call it can be a security hole or invite DoS attack
1408+since the opened and once readdir-ed dir (file object) holds its entry
1409+list and becomes a pressure for system memory. But I'd say it is similar
1410+to files under /proc or /sys. The virtual files in them also holds a
1411+memory page (generally) while they are opened. When an idea to reduce
1412+memory for them is introduced, it will be applied to aufs too.
1413+For those who really hate this situation, I've developed readdir(3)
1414+library which operates this merging in userspace. You just need to set
1415+LD_PRELOAD environment variable, and aufs will not consume no memory in
1416+kernel space for readdir(3).
1417+
1418+
1419+Workqueue
1420+----------------------------------------------------------------------
1421+Aufs sometimes requires privilege access to a branch. For instance,
1422+in copy-up/down operation. When a user process is going to make changes
1423+to a file which exists in the lower readonly branch only, and the mode
1424+of one of ancestor directories may not be writable by a user
1425+process. Here aufs copy-up the file with its ancestors and they may
1426+require privilege to set its owner/group/mode/etc.
1427+This is a typical case of a application character of aufs (see
1428+Introduction).
1429+
1430+Aufs uses workqueue synchronously for this case. It creates its own
1431+workqueue. The workqueue is a kernel thread and has privilege. Aufs
1432+passes the request to call mkdir or write (for example), and wait for
1433+its completion. This approach solves a problem of a signal handler
1434+simply.
1435+If aufs didn't adopt the workqueue and changed the privilege of the
1436+process, then the process may receive the unexpected SIGXFSZ or other
1437+signals.
1438+
1439+Also aufs uses the system global workqueue ("events" kernel thread) too
1440+for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1441+whiteout base and etc. This is unrelated to a privilege.
1442+Most of aufs operation tries acquiring a rw_semaphore for aufs
1443+superblock at the beginning, at the same time waits for the completion
1444+of all queued asynchronous tasks.
1445+
1446+
1447+Whiteout
1448+----------------------------------------------------------------------
1449+The whiteout in aufs is very similar to Unionfs's. That is represented
1450+by its filename. UnionMount takes an approach of a file mode, but I am
1451+afraid several utilities (find(1) or something) will have to support it.
1452+
1453+Basically the whiteout represents "logical deletion" which stops aufs to
1454+lookup further, but also it represents "dir is opaque" which also stop
1455+further lookup.
1456+
1457+In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1458+In order to make several functions in a single systemcall to be
1459+revertible, aufs adopts an approach to rename a directory to a temporary
1460+unique whiteouted name.
1461+For example, in rename(2) dir where the target dir already existed, aufs
1462+renames the target dir to a temporary unique whiteouted name before the
1463+actual rename on a branch, and then handles other actions (make it opaque,
1464+update the attributes, etc). If an error happens in these actions, aufs
1465+simply renames the whiteouted name back and returns an error. If all are
1466+succeeded, aufs registers a function to remove the whiteouted unique
1467+temporary name completely and asynchronously to the system global
1468+workqueue.
1469+
1470+
1471+Copy-up
1472+----------------------------------------------------------------------
1473+It is a well-known feature or concept.
1474+When user modifies a file on a readonly branch, aufs operate "copy-up"
1475+internally and makes change to the new file on the upper writable branch.
1476+When the trigger systemcall does not update the timestamps of the parent
1477+dir, aufs reverts it after copy-up.
1478+
1479+
1480+Move-down (aufs3.9 and later)
1481+----------------------------------------------------------------------
1482+"Copy-up" is one of the essential feature in aufs. It copies a file from
1483+the lower readonly branch to the upper writable branch when a user
1484+changes something about the file.
1485+"Move-down" is an opposite action of copy-up. Basically this action is
1486+ran manually instead of automatically and internally.
1487+For desgin and implementation, aufs has to consider these issues.
1488+- whiteout for the file may exist on the lower branch.
1489+- ancestor directories may not exist on the lower branch.
1490+- diropq for the ancestor directories may exist on the upper branch.
1491+- free space on the lower branch will reduce.
1492+- another access to the file may happen during moving-down, including
1493+ UDBA (see "Revalidate Dentry and UDBA").
1494+- the file should not be hard-linked nor pseudo-linked. they should be
1495+ handled by auplink utility later.
1496+
1497+Sometimes users want to move-down a file from the upper writable branch
1498+to the lower readonly or writable branch. For instance,
1499+- the free space of the upper writable branch is going to run out.
1500+- create a new intermediate branch between the upper and lower branch.
1501+- etc.
1502+
1503+For this purpose, use "aumvdown" command in aufs-util.git.
1504diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt linux/Documentation/filesystems/aufs/design/03atomic_open.txt
1505--- /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt 1970-01-01 01:00:00.000000000 +0100
1506+++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt 2021-02-24 13:33:42.737680181 +0100
1507@@ -0,0 +1,85 @@
1508+
1509+# Copyright (C) 2015-2020 Junjiro R. Okajima
1510+#
1511+# This program is free software; you can redistribute it and/or modify
1512+# it under the terms of the GNU General Public License as published by
1513+# the Free Software Foundation; either version 2 of the License, or
1514+# (at your option) any later version.
1515+#
1516+# This program is distributed in the hope that it will be useful,
1517+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1518+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1519+# GNU General Public License for more details.
1520+#
1521+# You should have received a copy of the GNU General Public License
1522+# along with this program. If not, see <http://www.gnu.org/licenses/>.
1523+
1524+Support for a branch who has its ->atomic_open()
1525+----------------------------------------------------------------------
1526+The filesystems who implement its ->atomic_open() are not majority. For
1527+example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
1528+particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
1529+->atomic_open(), NFSv4 returns an error for this open(2). While I am not
1530+sure whether all filesystems who have ->atomic_open() behave like this,
1531+but NFSv4 surely returns the error.
1532+
1533+In order to support ->atomic_open() for aufs, there are a few
1534+approaches.
1535+
1536+A. Introduce aufs_atomic_open()
1537+ - calls one of VFS:do_last(), lookup_open() or atomic_open() for
1538+ branch fs.
1539+B. Introduce aufs_atomic_open() calling create, open and chmod. this is
1540+ an aufs user Pip Cet's approach
1541+ - calls aufs_create(), VFS finish_open() and notify_change().
1542+ - pass fake-mode to finish_open(), and then correct the mode by
1543+ notify_change().
1544+C. Extend aufs_open() to call branch fs's ->atomic_open()
1545+ - no aufs_atomic_open().
1546+ - aufs_lookup() registers the TID to an aufs internal object.
1547+ - aufs_create() does nothing when the matching TID is registered, but
1548+ registers the mode.
1549+ - aufs_open() calls branch fs's ->atomic_open() when the matching
1550+ TID is registered.
1551+D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
1552+ credential
1553+ - no aufs_atomic_open().
1554+ - aufs_create() registers the TID to an internal object. this info
1555+ represents "this process created this file just now."
1556+ - when aufs gets EACCES from branch fs's ->open(), then confirm the
1557+ registered TID and re-try open() with superuser's credential.
1558+
1559+Pros and cons for each approach.
1560+
1561+A.
1562+ - straightforward but highly depends upon VFS internal.
1563+ - the atomic behavaiour is kept.
1564+ - some of parameters such as nameidata are hard to reproduce for
1565+ branch fs.
1566+ - large overhead.
1567+B.
1568+ - easy to implement.
1569+ - the atomic behavaiour is lost.
1570+C.
1571+ - the atomic behavaiour is kept.
1572+ - dirty and tricky.
1573+ - VFS checks whether the file is created correctly after calling
1574+ ->create(), which means this approach doesn't work.
1575+D.
1576+ - easy to implement.
1577+ - the atomic behavaiour is lost.
1578+ - to open a file with superuser's credential and give it to a user
1579+ process is a bad idea, since the file object keeps the credential
1580+ in it. It may affect LSM or something. This approach doesn't work
1581+ either.
1582+
1583+The approach A is ideal, but it hard to implement. So here is a
1584+variation of A, which is to be implemented.
1585+
1586+A-1. Introduce aufs_atomic_open()
1587+ - calls branch fs ->atomic_open() if exists. otherwise calls
1588+ vfs_create() and finish_open().
1589+ - the demerit is that the several checks after branch fs
1590+ ->atomic_open() are lost. in the ordinary case, the checks are
1591+ done by VFS:do_last(), lookup_open() and atomic_open(). some can
1592+ be implemented in aufs, but not all I am afraid.
1593diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
1594--- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
1595+++ linux/Documentation/filesystems/aufs/design/03lookup.txt 2021-02-24 13:33:42.737680181 +0100
1596@@ -0,0 +1,113 @@
1597+
1598+# Copyright (C) 2005-2020 Junjiro R. Okajima
1599+#
1600+# This program is free software; you can redistribute it and/or modify
1601+# it under the terms of the GNU General Public License as published by
1602+# the Free Software Foundation; either version 2 of the License, or
1603+# (at your option) any later version.
1604+#
1605+# This program is distributed in the hope that it will be useful,
1606+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1607+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1608+# GNU General Public License for more details.
1609+#
1610+# You should have received a copy of the GNU General Public License
1611+# along with this program. If not, see <http://www.gnu.org/licenses/>.
1612+
1613+Lookup in a Branch
1614+----------------------------------------------------------------------
1615+Since aufs has a character of sub-VFS (see Introduction), it operates
1616+lookup for branches as VFS does. It may be a heavy work. But almost all
1617+lookup operation in aufs is the simplest case, ie. lookup only an entry
1618+directly connected to its parent. Digging down the directory hierarchy
1619+is unnecessary. VFS has a function lookup_one_len() for that use, and
1620+aufs calls it.
1621+
1622+When a branch is a remote filesystem, aufs basically relies upon its
1623+->d_revalidate(), also aufs forces the hardest revalidate tests for
1624+them.
1625+For d_revalidate, aufs implements three levels of revalidate tests. See
1626+"Revalidate Dentry and UDBA" in detail.
1627+
1628+
1629+Test Only the Highest One for the Directory Permission (dirperm1 option)
1630+----------------------------------------------------------------------
1631+Let's try case study.
1632+- aufs has two branches, upper readwrite and lower readonly.
1633+ /au = /rw + /ro
1634+- "dirA" exists under /ro, but /rw. and its mode is 0700.
1635+- user invoked "chmod a+rx /au/dirA"
1636+- the internal copy-up is activated and "/rw/dirA" is created and its
1637+ permission bits are set to world readable.
1638+- then "/au/dirA" becomes world readable?
1639+
1640+In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1641+or it may be a natively readonly filesystem. If aufs respects the lower
1642+branch, it should not respond readdir request from other users. But user
1643+allowed it by chmod. Should really aufs rejects showing the entries
1644+under /ro/dirA?
1645+
1646+To be honest, I don't have a good solution for this case. So aufs
1647+implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1648+users.
1649+When dirperm1 is specified, aufs checks only the highest one for the
1650+directory permission, and shows the entries. Otherwise, as usual, checks
1651+every dir existing on all branches and rejects the request.
1652+
1653+As a side effect, dirperm1 option improves the performance of aufs
1654+because the number of permission check is reduced when the number of
1655+branch is many.
1656+
1657+
1658+Revalidate Dentry and UDBA (User's Direct Branch Access)
1659+----------------------------------------------------------------------
1660+Generally VFS helpers re-validate a dentry as a part of lookup.
1661+0. digging down the directory hierarchy.
1662+1. lock the parent dir by its i_mutex.
1663+2. lookup the final (child) entry.
1664+3. revalidate it.
1665+4. call the actual operation (create, unlink, etc.)
1666+5. unlock the parent dir
1667+
1668+If the filesystem implements its ->d_revalidate() (step 3), then it is
1669+called. Actually aufs implements it and checks the dentry on a branch is
1670+still valid.
1671+But it is not enough. Because aufs has to release the lock for the
1672+parent dir on a branch at the end of ->lookup() (step 2) and
1673+->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1674+held by VFS.
1675+If the file on a branch is changed directly, eg. bypassing aufs, after
1676+aufs released the lock, then the subsequent operation may cause
1677+something unpleasant result.
1678+
1679+This situation is a result of VFS architecture, ->lookup() and
1680+->d_revalidate() is separated. But I never say it is wrong. It is a good
1681+design from VFS's point of view. It is just not suitable for sub-VFS
1682+character in aufs.
1683+
1684+Aufs supports such case by three level of revalidation which is
1685+selectable by user.
1686+1. Simple Revalidate
1687+ Addition to the native flow in VFS's, confirm the child-parent
1688+ relationship on the branch just after locking the parent dir on the
1689+ branch in the "actual operation" (step 4). When this validation
1690+ fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1691+ checks the validation of the dentry on branches.
1692+2. Monitor Changes Internally by Inotify/Fsnotify
1693+ Addition to above, in the "actual operation" (step 4) aufs re-lookup
1694+ the dentry on the branch, and returns EBUSY if it finds different
1695+ dentry.
1696+ Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1697+ during it is in cache. When the event is notified, aufs registers a
1698+ function to kernel 'events' thread by schedule_work(). And the
1699+ function sets some special status to the cached aufs dentry and inode
1700+ private data. If they are not cached, then aufs has nothing to
1701+ do. When the same file is accessed through aufs (step 0-3) later,
1702+ aufs will detect the status and refresh all necessary data.
1703+ In this mode, aufs has to ignore the event which is fired by aufs
1704+ itself.
1705+3. No Extra Validation
1706+ This is the simplest test and doesn't add any additional revalidation
1707+ test, and skip the revalidation in step 4. It is useful and improves
1708+ aufs performance when system surely hide the aufs branches from user,
1709+ by over-mounting something (or another method).
1710diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
1711--- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
1712+++ linux/Documentation/filesystems/aufs/design/04branch.txt 2021-02-24 13:33:42.737680181 +0100
1713@@ -0,0 +1,74 @@
1714+
1715+# Copyright (C) 2005-2020 Junjiro R. Okajima
1716+#
1717+# This program is free software; you can redistribute it and/or modify
1718+# it under the terms of the GNU General Public License as published by
1719+# the Free Software Foundation; either version 2 of the License, or
1720+# (at your option) any later version.
1721+#
1722+# This program is distributed in the hope that it will be useful,
1723+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1724+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1725+# GNU General Public License for more details.
1726+#
1727+# You should have received a copy of the GNU General Public License
1728+# along with this program. If not, see <http://www.gnu.org/licenses/>.
1729+
1730+Branch Manipulation
1731+
1732+Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1733+and changing its permission/attribute, there are a lot of works to do.
1734+
1735+
1736+Add a Branch
1737+----------------------------------------------------------------------
1738+o Confirm the adding dir exists outside of aufs, including loopback
1739+ mount, and its various attributes.
1740+o Initialize the xino file and whiteout bases if necessary.
1741+ See struct.txt.
1742+
1743+o Check the owner/group/mode of the directory
1744+ When the owner/group/mode of the adding directory differs from the
1745+ existing branch, aufs issues a warning because it may impose a
1746+ security risk.
1747+ For example, when a upper writable branch has a world writable empty
1748+ top directory, a malicious user can create any files on the writable
1749+ branch directly, like copy-up and modify manually. If something like
1750+ /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1751+ writable branch, and the writable branch is world-writable, then a
1752+ malicious guy may create /etc/passwd on the writable branch directly
1753+ and the infected file will be valid in aufs.
1754+ I am afraid it can be a security issue, but aufs can do nothing except
1755+ producing a warning.
1756+
1757+
1758+Delete a Branch
1759+----------------------------------------------------------------------
1760+o Confirm the deleting branch is not busy
1761+ To be general, there is one merit to adopt "remount" interface to
1762+ manipulate branches. It is to discard caches. At deleting a branch,
1763+ aufs checks the still cached (and connected) dentries and inodes. If
1764+ there are any, then they are all in-use. An inode without its
1765+ corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1766+
1767+ For the cached one, aufs checks whether the same named entry exists on
1768+ other branches.
1769+ If the cached one is a directory, because aufs provides a merged view
1770+ to users, as long as one dir is left on any branch aufs can show the
1771+ dir to users. In this case, the branch can be removed from aufs.
1772+ Otherwise aufs rejects deleting the branch.
1773+
1774+ If any file on the deleting branch is opened by aufs, then aufs
1775+ rejects deleting.
1776+
1777+
1778+Modify the Permission of a Branch
1779+----------------------------------------------------------------------
1780+o Re-initialize or remove the xino file and whiteout bases if necessary.
1781+ See struct.txt.
1782+
1783+o rw --> ro: Confirm the modifying branch is not busy
1784+ Aufs rejects the request if any of these conditions are true.
1785+ - a file on the branch is mmap-ed.
1786+ - a regular file on the branch is opened for write and there is no
1787+ same named entry on the upper branch.
1788diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
1789--- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt 1970-01-01 01:00:00.000000000 +0100
1790+++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt 2021-02-24 13:33:42.737680181 +0100
1791@@ -0,0 +1,64 @@
1792+
1793+# Copyright (C) 2005-2020 Junjiro R. Okajima
1794+#
1795+# This program is free software; you can redistribute it and/or modify
1796+# it under the terms of the GNU General Public License as published by
1797+# the Free Software Foundation; either version 2 of the License, or
1798+# (at your option) any later version.
1799+#
1800+# This program is distributed in the hope that it will be useful,
1801+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1802+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1803+# GNU General Public License for more details.
1804+#
1805+# You should have received a copy of the GNU General Public License
1806+# along with this program. If not, see <http://www.gnu.org/licenses/>.
1807+
1808+Policies to Select One among Multiple Writable Branches
1809+----------------------------------------------------------------------
1810+When the number of writable branch is more than one, aufs has to decide
1811+the target branch for file creation or copy-up. By default, the highest
1812+writable branch which has the parent (or ancestor) dir of the target
1813+file is chosen (top-down-parent policy).
1814+By user's request, aufs implements some other policies to select the
1815+writable branch, for file creation several policies, round-robin,
1816+most-free-space, and other policies. For copy-up, top-down-parent,
1817+bottom-up-parent, bottom-up and others.
1818+
1819+As expected, the round-robin policy selects the branch in circular. When
1820+you have two writable branches and creates 10 new files, 5 files will be
1821+created for each branch. mkdir(2) systemcall is an exception. When you
1822+create 10 new directories, all will be created on the same branch.
1823+And the most-free-space policy selects the one which has most free
1824+space among the writable branches. The amount of free space will be
1825+checked by aufs internally, and users can specify its time interval.
1826+
1827+The policies for copy-up is more simple,
1828+top-down-parent is equivalent to the same named on in create policy,
1829+bottom-up-parent selects the writable branch where the parent dir
1830+exists and the nearest upper one from the copyup-source,
1831+bottom-up selects the nearest upper writable branch from the
1832+copyup-source, regardless the existence of the parent dir.
1833+
1834+There are some rules or exceptions to apply these policies.
1835+- If there is a readonly branch above the policy-selected branch and
1836+ the parent dir is marked as opaque (a variation of whiteout), or the
1837+ target (creating) file is whiteout-ed on the upper readonly branch,
1838+ then the result of the policy is ignored and the target file will be
1839+ created on the nearest upper writable branch than the readonly branch.
1840+- If there is a writable branch above the policy-selected branch and
1841+ the parent dir is marked as opaque or the target file is whiteouted
1842+ on the branch, then the result of the policy is ignored and the target
1843+ file will be created on the highest one among the upper writable
1844+ branches who has diropq or whiteout. In case of whiteout, aufs removes
1845+ it as usual.
1846+- link(2) and rename(2) systemcalls are exceptions in every policy.
1847+ They try selecting the branch where the source exists as possible
1848+ since copyup a large file will take long time. If it can't be,
1849+ ie. the branch where the source exists is readonly, then they will
1850+ follow the copyup policy.
1851+- There is an exception for rename(2) when the target exists.
1852+ If the rename target exists, aufs compares the index of the branches
1853+ where the source and the target exists and selects the higher
1854+ one. If the selected branch is readonly, then aufs follows the
1855+ copyup policy.
1856diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot linux/Documentation/filesystems/aufs/design/06dirren.dot
1857--- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot 1970-01-01 01:00:00.000000000 +0100
1858+++ linux/Documentation/filesystems/aufs/design/06dirren.dot 2021-02-24 13:33:42.737680181 +0100
1859@@ -0,0 +1,31 @@
1860+
1861+// to view this graph, run dot(1) command in GRAPHVIZ.
1862+
1863+digraph G {
1864+node [shape=box];
1865+whinfo [label="detailed info file\n(lower_brid_root-hinum, h_inum, namelen, old name)"];
1866+
1867+node [shape=oval];
1868+
1869+aufs_rename -> whinfo [label="store/remove"];
1870+
1871+node [shape=oval];
1872+inode_list [label="h_inum list in branch\ncache"];
1873+
1874+node [shape=box];
1875+whinode [label="h_inum list file"];
1876+
1877+node [shape=oval];
1878+brmgmt [label="br_add/del/mod/umount"];
1879+
1880+brmgmt -> inode_list [label="create/remove"];
1881+brmgmt -> whinode [label="load/store"];
1882+
1883+inode_list -> whinode [style=dashed,dir=both];
1884+
1885+aufs_rename -> inode_list [label="add/del"];
1886+
1887+aufs_lookup -> inode_list [label="search"];
1888+
1889+aufs_lookup -> whinfo [label="load/remove"];
1890+}
1891diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt linux/Documentation/filesystems/aufs/design/06dirren.txt
1892--- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt 1970-01-01 01:00:00.000000000 +0100
1893+++ linux/Documentation/filesystems/aufs/design/06dirren.txt 2021-02-24 13:33:42.737680181 +0100
1894@@ -0,0 +1,102 @@
1895+
1896+# Copyright (C) 2017-2020 Junjiro R. Okajima
1897+#
1898+# This program is free software; you can redistribute it and/or modify
1899+# it under the terms of the GNU General Public License as published by
1900+# the Free Software Foundation; either version 2 of the License, or
1901+# (at your option) any later version.
1902+#
1903+# This program is distributed in the hope that it will be useful,
1904+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1905+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1906+# GNU General Public License for more details.
1907+#
1908+# You should have received a copy of the GNU General Public License
1909+# along with this program. If not, see <http://www.gnu.org/licenses/>.
1910+
1911+Special handling for renaming a directory (DIRREN)
1912+----------------------------------------------------------------------
1913+First, let's assume we have a simple usecase.
1914+
1915+- /u = /rw + /ro
1916+- /rw/dirA exists
1917+- /ro/dirA and /ro/dirA/file exist too
1918+- there is no dirB on both branches
1919+- a user issues rename("dirA", "dirB")
1920+
1921+Now, what should aufs behave against this rename(2)?
1922+There are a few possible cases.
1923+
1924+A. returns EROFS.
1925+ since dirA exists on a readonly branch which cannot be renamed.
1926+B. returns EXDEV.
1927+ it is possible to copy-up dirA (only the dir itself), but the child
1928+ entries ("file" in this case) should not be. it must be a bad
1929+ approach to copy-up recursively.
1930+C. returns a success.
1931+ even the branch /ro is readonly, aufs tries renaming it. Obviously it
1932+ is a violation of aufs' policy.
1933+D. construct an extra information which indicates that /ro/dirA should
1934+ be handled as the name of dirB.
1935+ overlayfs has a similar feature called REDIRECT.
1936+
1937+Until now, aufs implements the case B only which returns EXDEV, and
1938+expects the userspace application behaves like mv(1) which tries
1939+issueing rename(2) recursively.
1940+
1941+A new aufs feature called DIRREN is introduced which implements the case
1942+D. There are several "extra information" added.
1943+
1944+1. detailed info per renamed directory
1945+ path: /rw/dirB/$AUFS_WH_DR_INFO_PFX.<lower branch-id>
1946+2. the inode-number list of directories on a branch
1947+ path: /rw/dirB/$AUFS_WH_DR_BRHINO
1948+
1949+The filename of "detailed info per directory" represents the lower
1950+branch, and its format is
1951+- a type of the branch id
1952+ one of these.
1953+ + uuid (not implemented yet)
1954+ + fsid
1955+ + dev
1956+- the inode-number of the branch root dir
1957+
1958+And it contains these info in a single regular file.
1959+- magic number
1960+- branch's inode-number of the logically renamed dir
1961+- the name of the before-renamed dir
1962+
1963+The "detailed info per directory" file is created in aufs rename(2), and
1964+loaded in any lookup.
1965+The info is considered in lookup for the matching case only. Here
1966+"matching" means that the root of branch (in the info filename) is same
1967+to the current looking-up branch. After looking-up the before-renamed
1968+name, the inode-number is compared. And the matched dentry is used.
1969+
1970+The "inode-number list of directories" is a regular file which contains
1971+simply the inode-numbers on the branch. The file is created or updated
1972+in removing the branch, and loaded in adding the branch. Its lifetime is
1973+equal to the branch.
1974+The list is refered in lookup, and when the current target inode is
1975+found in the list, the aufs tries loading the "detailed info per
1976+directory" and get the changed and valid name of the dir.
1977+
1978+Theoretically these "extra informaiton" may be able to be put into XATTR
1979+in the dir inode. But aufs doesn't choose this way because
1980+1. XATTR may not be supported by the branch (or its configuration)
1981+2. XATTR may have its size limit.
1982+3. XATTR may be less easy to convert than a regular file, when the
1983+ format of the info is changed in the future.
1984+At the same time, I agree that the regular file approach is much slower
1985+than XATTR approach. So, in the future, aufs may take the XATTR or other
1986+better approach.
1987+
1988+This DIRREN feature is enabled by aufs configuration, and is activated
1989+by a new mount option.
1990+
1991+For the more complicated case, there is a work with UDBA option, which
1992+is to dected the direct access to the branches (by-passing aufs) and to
1993+maintain the cashes in aufs. Since a single cached aufs dentry may
1994+contains two names, before- and after-rename, the name comparision in
1995+UDBA handler may not work correctly. In this case, the behaviour will be
1996+equivalen to udba=reval case.
1997diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt linux/Documentation/filesystems/aufs/design/06fhsm.txt
1998--- /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt 1970-01-01 01:00:00.000000000 +0100
1999+++ linux/Documentation/filesystems/aufs/design/06fhsm.txt 2021-02-24 13:33:42.737680181 +0100
2000@@ -0,0 +1,120 @@
2001+
2002+# Copyright (C) 2011-2020 Junjiro R. Okajima
2003+#
2004+# This program is free software; you can redistribute it and/or modify
2005+# it under the terms of the GNU General Public License as published by
2006+# the Free Software Foundation; either version 2 of the License, or
2007+# (at your option) any later version.
2008+#
2009+# This program is distributed in the hope that it will be useful,
2010+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2011+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2012+# GNU General Public License for more details.
2013+#
2014+# You should have received a copy of the GNU General Public License
2015+# along with this program; if not, write to the Free Software
2016+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2017+
2018+
2019+File-based Hierarchical Storage Management (FHSM)
2020+----------------------------------------------------------------------
2021+Hierarchical Storage Management (or HSM) is a well-known feature in the
2022+storage world. Aufs provides this feature as file-based with multiple
2023+writable branches, based upon the principle of "Colder, the Lower".
2024+Here the word "colder" means that the less used files, and "lower" means
2025+that the position in the order of the stacked branches vertically.
2026+These multiple writable branches are prioritized, ie. the topmost one
2027+should be the fastest drive and be used heavily.
2028+
2029+o Characters in aufs FHSM story
2030+- aufs itself and a new branch attribute.
2031+- a new ioctl interface to move-down and to establish a connection with
2032+ the daemon ("move-down" is a converse of "copy-up").
2033+- userspace tool and daemon.
2034+
2035+The userspace daemon establishes a connection with aufs and waits for
2036+the notification. The notified information is very similar to struct
2037+statfs containing the number of consumed blocks and inodes.
2038+When the consumed blocks/inodes of a branch exceeds the user-specified
2039+upper watermark, the daemon activates its move-down process until the
2040+consumed blocks/inodes reaches the user-specified lower watermark.
2041+
2042+The actual move-down is done by aufs based upon the request from
2043+user-space since we need to maintain the inode number and the internal
2044+pointer arrays in aufs.
2045+
2046+Currently aufs FHSM handles the regular files only. Additionally they
2047+must not be hard-linked nor pseudo-linked.
2048+
2049+
2050+o Cowork of aufs and the user-space daemon
2051+ During the userspace daemon established the connection, aufs sends a
2052+ small notification to it whenever aufs writes something into the
2053+ writable branch. But it may cost high since aufs issues statfs(2)
2054+ internally. So user can specify a new option to cache the
2055+ info. Actually the notification is controlled by these factors.
2056+ + the specified cache time.
2057+ + classified as "force" by aufs internally.
2058+ Until the specified time expires, aufs doesn't send the info
2059+ except the forced cases. When aufs decide forcing, the info is always
2060+ notified to userspace.
2061+ For example, the number of free inodes is generally large enough and
2062+ the shortage of it happens rarely. So aufs doesn't force the
2063+ notification when creating a new file, directory and others. This is
2064+ the typical case which aufs doesn't force.
2065+ When aufs writes the actual filedata and the files consumes any of new
2066+ blocks, the aufs forces notifying.
2067+
2068+
2069+o Interfaces in aufs
2070+- New branch attribute.
2071+ + fhsm
2072+ Specifies that the branch is managed by FHSM feature. In other word,
2073+ participant in the FHSM.
2074+ When nofhsm is set to the branch, it will not be the source/target
2075+ branch of the move-down operation. This attribute is set
2076+ independently from coo and moo attributes, and if you want full
2077+ FHSM, you should specify them as well.
2078+- New mount option.
2079+ + fhsm_sec
2080+ Specifies a second to suppress many less important info to be
2081+ notified.
2082+- New ioctl.
2083+ + AUFS_CTL_FHSM_FD
2084+ create a new file descriptor which userspace can read the notification
2085+ (a subset of struct statfs) from aufs.
2086+- Module parameter 'brs'
2087+ It has to be set to 1. Otherwise the new mount option 'fhsm' will not
2088+ be set.
2089+- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
2090+ When there are two or more branches with fhsm attributes,
2091+ /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
2092+ terminates it. As a result of remounting and branch-manipulation, the
2093+ number of branches with fhsm attribute can be one. In this case,
2094+ /sbin/mount.aufs will terminate the user-space daemon.
2095+
2096+
2097+Finally the operation is done as these steps in kernel-space.
2098+- make sure that,
2099+ + no one else is using the file.
2100+ + the file is not hard-linked.
2101+ + the file is not pseudo-linked.
2102+ + the file is a regular file.
2103+ + the parent dir is not opaqued.
2104+- find the target writable branch.
2105+- make sure the file is not whiteout-ed by the upper (than the target)
2106+ branch.
2107+- make the parent dir on the target branch.
2108+- mutex lock the inode on the branch.
2109+- unlink the whiteout on the target branch (if exists).
2110+- lookup and create the whiteout-ed temporary name on the target branch.
2111+- copy the file as the whiteout-ed temporary name on the target branch.
2112+- rename the whiteout-ed temporary name to the original name.
2113+- unlink the file on the source branch.
2114+- maintain the internal pointer array and the external inode number
2115+ table (XINO).
2116+- maintain the timestamps and other attributes of the parent dir and the
2117+ file.
2118+
2119+And of course, in every step, an error may happen. So the operation
2120+should restore the original file state after an error happens.
2121diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
2122--- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt 1970-01-01 01:00:00.000000000 +0100
2123+++ linux/Documentation/filesystems/aufs/design/06mmap.txt 2021-02-24 13:33:42.737680181 +0100
2124@@ -0,0 +1,72 @@
2125+
2126+# Copyright (C) 2005-2020 Junjiro R. Okajima
2127+#
2128+# This program is free software; you can redistribute it and/or modify
2129+# it under the terms of the GNU General Public License as published by
2130+# the Free Software Foundation; either version 2 of the License, or
2131+# (at your option) any later version.
2132+#
2133+# This program is distributed in the hope that it will be useful,
2134+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2135+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2136+# GNU General Public License for more details.
2137+#
2138+# You should have received a copy of the GNU General Public License
2139+# along with this program. If not, see <http://www.gnu.org/licenses/>.
2140+
2141+mmap(2) -- File Memory Mapping
2142+----------------------------------------------------------------------
2143+In aufs, the file-mapped pages are handled by a branch fs directly, no
2144+interaction with aufs. It means aufs_mmap() calls the branch fs's
2145+->mmap().
2146+This approach is simple and good, but there is one problem.
2147+Under /proc, several entries show the mmapped files by its path (with
2148+device and inode number), and the printed path will be the path on the
2149+branch fs's instead of virtual aufs's.
2150+This is not a problem in most cases, but some utilities lsof(1) (and its
2151+user) may expect the path on aufs.
2152+
2153+To address this issue, aufs adds a new member called vm_prfile in struct
2154+vm_area_struct (and struct vm_region). The original vm_file points to
2155+the file on the branch fs in order to handle everything correctly as
2156+usual. The new vm_prfile points to a virtual file in aufs, and the
2157+show-functions in procfs refers to vm_prfile if it is set.
2158+Also we need to maintain several other places where touching vm_file
2159+such like
2160+- fork()/clone() copies vma and the reference count of vm_file is
2161+ incremented.
2162+- merging vma maintains the ref count too.
2163+
2164+This is not a good approach. It just fakes the printed path. But it
2165+leaves all behaviour around f_mapping unchanged. This is surely an
2166+advantage.
2167+Actually aufs had adopted another complicated approach which calls
2168+generic_file_mmap() and handles struct vm_operations_struct. In this
2169+approach, aufs met a hard problem and I could not solve it without
2170+switching the approach.
2171+
2172+There may be one more another approach which is
2173+- bind-mount the branch-root onto the aufs-root internally
2174+- grab the new vfsmount (ie. struct mount)
2175+- lazy-umount the branch-root internally
2176+- in open(2) the aufs-file, open the branch-file with the hidden
2177+ vfsmount (instead of the original branch's vfsmount)
2178+- ideally this "bind-mount and lazy-umount" should be done atomically,
2179+ but it may be possible from userspace by the mount helper.
2180+
2181+Adding the internal hidden vfsmount and using it in opening a file, the
2182+file path under /proc will be printed correctly. This approach looks
2183+smarter, but is not possible I am afraid.
2184+- aufs-root may be bind-mount later. when it happens, another hidden
2185+ vfsmount will be required.
2186+- it is hard to get the chance to bind-mount and lazy-umount
2187+ + in kernel-space, FS can have vfsmount in open(2) via
2188+ file->f_path, and aufs can know its vfsmount. But several locks are
2189+ already acquired, and if aufs tries to bind-mount and lazy-umount
2190+ here, then it may cause a deadlock.
2191+ + in user-space, bind-mount doesn't invoke the mount helper.
2192+- since /proc shows dev and ino, aufs has to give vma these info. it
2193+ means a new member vm_prinode will be necessary. this is essentially
2194+ equivalent to vm_prfile described above.
2195+
2196+I have to give up this "looks-smater" approach.
2197diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt linux/Documentation/filesystems/aufs/design/06xattr.txt
2198--- /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt 1970-01-01 01:00:00.000000000 +0100
2199+++ linux/Documentation/filesystems/aufs/design/06xattr.txt 2021-02-24 13:33:42.737680181 +0100
2200@@ -0,0 +1,96 @@
2201+
2202+# Copyright (C) 2014-2020 Junjiro R. Okajima
2203+#
2204+# This program is free software; you can redistribute it and/or modify
2205+# it under the terms of the GNU General Public License as published by
2206+# the Free Software Foundation; either version 2 of the License, or
2207+# (at your option) any later version.
2208+#
2209+# This program is distributed in the hope that it will be useful,
2210+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2211+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2212+# GNU General Public License for more details.
2213+#
2214+# You should have received a copy of the GNU General Public License
2215+# along with this program; if not, write to the Free Software
2216+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2217+
2218+
2219+Listing XATTR/EA and getting the value
2220+----------------------------------------------------------------------
2221+For the inode standard attributes (owner, group, timestamps, etc.), aufs
2222+shows the values from the topmost existing file. This behaviour is good
2223+for the non-dir entries since the bahaviour exactly matches the shown
2224+information. But for the directories, aufs considers all the same named
2225+entries on the lower branches. Which means, if one of the lower entry
2226+rejects readdir call, then aufs returns an error even if the topmost
2227+entry allows it. This behaviour is necessary to respect the branch fs's
2228+security, but can make users confused since the user-visible standard
2229+attributes don't match the behaviour.
2230+To address this issue, aufs has a mount option called dirperm1 which
2231+checks the permission for the topmost entry only, and ignores the lower
2232+entry's permission.
2233+
2234+A similar issue can happen around XATTR.
2235+getxattr(2) and listxattr(2) families behave as if dirperm1 option is
2236+always set. Otherwise these very unpleasant situation would happen.
2237+- listxattr(2) may return the duplicated entries.
2238+- users may not be able to remove or reset the XATTR forever,
2239+
2240+
2241+XATTR/EA support in the internal (copy,move)-(up,down)
2242+----------------------------------------------------------------------
2243+Generally the extended attributes of inode are categorized as these.
2244+- "security" for LSM and capability.
2245+- "system" for posix ACL, 'acl' mount option is required for the branch
2246+ fs generally.
2247+- "trusted" for userspace, CAP_SYS_ADMIN is required.
2248+- "user" for userspace, 'user_xattr' mount option is required for the
2249+ branch fs generally.
2250+
2251+Moreover there are some other categories. Aufs handles these rather
2252+unpopular categories as the ordinary ones, ie. there is no special
2253+condition nor exception.
2254+
2255+In copy-up, the support for XATTR on the dst branch may differ from the
2256+src branch. In this case, the copy-up operation will get an error and
2257+the original user operation which triggered the copy-up will fail. It
2258+can happen that even all copy-up will fail.
2259+When both of src and dst branches support XATTR and if an error occurs
2260+during copying XATTR, then the copy-up should fail obviously. That is a
2261+good reason and aufs should return an error to userspace. But when only
2262+the src branch support that XATTR, aufs should not return an error.
2263+For example, the src branch supports ACL but the dst branch doesn't
2264+because the dst branch may natively un-support it or temporary
2265+un-support it due to "noacl" mount option. Of course, the dst branch fs
2266+may NOT return an error even if the XATTR is not supported. It is
2267+totally up to the branch fs.
2268+
2269+Anyway when the aufs internal copy-up gets an error from the dst branch
2270+fs, then aufs tries removing the just copied entry and returns the error
2271+to the userspace. The worst case of this situation will be all copy-up
2272+will fail.
2273+
2274+For the copy-up operation, there two basic approaches.
2275+- copy the specified XATTR only (by category above), and return the
2276+ error unconditionally if it happens.
2277+- copy all XATTR, and ignore the error on the specified category only.
2278+
2279+In order to support XATTR and to implement the correct behaviour, aufs
2280+chooses the latter approach and introduces some new branch attributes,
2281+"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
2282+They correspond to the XATTR namespaces (see above). Additionally, to be
2283+convenient, "icex" is also provided which means all "icex*" attributes
2284+are set (here the word "icex" stands for "ignore copy-error on XATTR").
2285+
2286+The meaning of these attributes is to ignore the error from setting
2287+XATTR on that branch.
2288+Note that aufs tries copying all XATTR unconditionally, and ignores the
2289+error from the dst branch according to the specified attributes.
2290+
2291+Some XATTR may have its default value. The default value may come from
2292+the parent dir or the environment. If the default value is set at the
2293+file creating-time, it will be overwritten by copy-up.
2294+Some contradiction may happen I am afraid.
2295+Do we need another attribute to stop copying XATTR? I am unsure. For
2296+now, aufs implements the branch attributes to ignore the error.
2297diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
2298--- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
2299+++ linux/Documentation/filesystems/aufs/design/07export.txt 2021-02-24 13:33:42.737680181 +0100
2300@@ -0,0 +1,58 @@
2301+
2302+# Copyright (C) 2005-2020 Junjiro R. Okajima
2303+#
2304+# This program is free software; you can redistribute it and/or modify
2305+# it under the terms of the GNU General Public License as published by
2306+# the Free Software Foundation; either version 2 of the License, or
2307+# (at your option) any later version.
2308+#
2309+# This program is distributed in the hope that it will be useful,
2310+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2311+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2312+# GNU General Public License for more details.
2313+#
2314+# You should have received a copy of the GNU General Public License
2315+# along with this program. If not, see <http://www.gnu.org/licenses/>.
2316+
2317+Export Aufs via NFS
2318+----------------------------------------------------------------------
2319+Here is an approach.
2320+- like xino/xib, add a new file 'xigen' which stores aufs inode
2321+ generation.
2322+- iget_locked(): initialize aufs inode generation for a new inode, and
2323+ store it in xigen file.
2324+- destroy_inode(): increment aufs inode generation and store it in xigen
2325+ file. it is necessary even if it is not unlinked, because any data of
2326+ inode may be changed by UDBA.
2327+- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2328+ build file handle by
2329+ + branch id (4 bytes)
2330+ + superblock generation (4 bytes)
2331+ + inode number (4 or 8 bytes)
2332+ + parent dir inode number (4 or 8 bytes)
2333+ + inode generation (4 bytes))
2334+ + return value of exportfs_encode_fh() for the parent on a branch (4
2335+ bytes)
2336+ + file handle for a branch (by exportfs_encode_fh())
2337+- fh_to_dentry():
2338+ + find the index of a branch from its id in handle, and check it is
2339+ still exist in aufs.
2340+ + 1st level: get the inode number from handle and search it in cache.
2341+ + 2nd level: if not found in cache, get the parent inode number from
2342+ the handle and search it in cache. and then open the found parent
2343+ dir, find the matching inode number by vfs_readdir() and get its
2344+ name, and call lookup_one_len() for the target dentry.
2345+ + 3rd level: if the parent dir is not cached, call
2346+ exportfs_decode_fh() for a branch and get the parent on a branch,
2347+ build a pathname of it, convert it a pathname in aufs, call
2348+ path_lookup(). now aufs gets a parent dir dentry, then handle it as
2349+ the 2nd level.
2350+ + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2351+ for every branch, but not itself. to get this, (currently) aufs
2352+ searches in current->nsproxy->mnt_ns list. it may not be a good
2353+ idea, but I didn't get other approach.
2354+ + test the generation of the gotten inode.
2355+- every inode operation: they may get EBUSY due to UDBA. in this case,
2356+ convert it into ESTALE for NFSD.
2357+- readdir(): call lockdep_on/off() because filldir in NFSD calls
2358+ lookup_one_len(), vfs_getattr(), encode_fh() and others.
2359diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
2360--- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt 1970-01-01 01:00:00.000000000 +0100
2361+++ linux/Documentation/filesystems/aufs/design/08shwh.txt 2021-02-24 13:33:42.737680181 +0100
2362@@ -0,0 +1,52 @@
2363+
2364+# Copyright (C) 2005-2020 Junjiro R. Okajima
2365+#
2366+# This program is free software; you can redistribute it and/or modify
2367+# it under the terms of the GNU General Public License as published by
2368+# the Free Software Foundation; either version 2 of the License, or
2369+# (at your option) any later version.
2370+#
2371+# This program is distributed in the hope that it will be useful,
2372+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2373+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2374+# GNU General Public License for more details.
2375+#
2376+# You should have received a copy of the GNU General Public License
2377+# along with this program. If not, see <http://www.gnu.org/licenses/>.
2378+
2379+Show Whiteout Mode (shwh)
2380+----------------------------------------------------------------------
2381+Generally aufs hides the name of whiteouts. But in some cases, to show
2382+them is very useful for users. For instance, creating a new middle layer
2383+(branch) by merging existing layers.
2384+
2385+(borrowing aufs1 HOW-TO from a user, Michael Towers)
2386+When you have three branches,
2387+- Bottom: 'system', squashfs (underlying base system), read-only
2388+- Middle: 'mods', squashfs, read-only
2389+- Top: 'overlay', ram (tmpfs), read-write
2390+
2391+The top layer is loaded at boot time and saved at shutdown, to preserve
2392+the changes made to the system during the session.
2393+When larger changes have been made, or smaller changes have accumulated,
2394+the size of the saved top layer data grows. At this point, it would be
2395+nice to be able to merge the two overlay branches ('mods' and 'overlay')
2396+and rewrite the 'mods' squashfs, clearing the top layer and thus
2397+restoring save and load speed.
2398+
2399+This merging is simplified by the use of another aufs mount, of just the
2400+two overlay branches using the 'shwh' option.
2401+# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2402+ aufs /livesys/merge_union
2403+
2404+A merged view of these two branches is then available at
2405+/livesys/merge_union, and the new feature is that the whiteouts are
2406+visible!
2407+Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2408+writing to all branches. Also the default mode for all branches is 'ro'.
2409+It is now possible to save the combined contents of the two overlay
2410+branches to a new squashfs, e.g.:
2411+# mksquashfs /livesys/merge_union /path/to/newmods.squash
2412+
2413+This new squashfs archive can be stored on the boot device and the
2414+initramfs will use it to replace the old one at the next boot.
2415diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
2416--- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt 1970-01-01 01:00:00.000000000 +0100
2417+++ linux/Documentation/filesystems/aufs/design/10dynop.txt 2021-02-24 13:33:42.737680181 +0100
2418@@ -0,0 +1,47 @@
2419+
2420+# Copyright (C) 2010-2020 Junjiro R. Okajima
2421+#
2422+# This program is free software; you can redistribute it and/or modify
2423+# it under the terms of the GNU General Public License as published by
2424+# the Free Software Foundation; either version 2 of the License, or
2425+# (at your option) any later version.
2426+#
2427+# This program is distributed in the hope that it will be useful,
2428+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2429+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2430+# GNU General Public License for more details.
2431+#
2432+# You should have received a copy of the GNU General Public License
2433+# along with this program. If not, see <http://www.gnu.org/licenses/>.
2434+
2435+Dynamically customizable FS operations
2436+----------------------------------------------------------------------
2437+Generally FS operations (struct inode_operations, struct
2438+address_space_operations, struct file_operations, etc.) are defined as
2439+"static const", but it never means that FS have only one set of
2440+operation. Some FS have multiple sets of them. For instance, ext2 has
2441+three sets, one for XIP, for NOBH, and for normal.
2442+Since aufs overrides and redirects these operations, sometimes aufs has
2443+to change its behaviour according to the branch FS type. More importantly
2444+VFS acts differently if a function (member in the struct) is set or
2445+not. It means aufs should have several sets of operations and select one
2446+among them according to the branch FS definition.
2447+
2448+In order to solve this problem and not to affect the behaviour of VFS,
2449+aufs defines these operations dynamically. For instance, aufs defines
2450+dummy direct_IO function for struct address_space_operations, but it may
2451+not be set to the address_space_operations actually. When the branch FS
2452+doesn't have it, aufs doesn't set it to its address_space_operations
2453+while the function definition itself is still alive. So the behaviour
2454+itself will not change, and it will return an error when direct_IO is
2455+not set.
2456+
2457+The lifetime of these dynamically generated operation object is
2458+maintained by aufs branch object. When the branch is removed from aufs,
2459+the reference counter of the object is decremented. When it reaches
2460+zero, the dynamically generated operation object will be freed.
2461+
2462+This approach is designed to support AIO (io_submit), Direct I/O and
2463+XIP (DAX) mainly.
2464+Currently this approach is applied to address_space_operations for
2465+regular files only.
2466diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
2467--- /usr/share/empty/Documentation/filesystems/aufs/README 1970-01-01 01:00:00.000000000 +0100
2468+++ linux/Documentation/filesystems/aufs/README 2021-06-30 21:35:11.393873211 +0200
2469@@ -0,0 +1,396 @@
2470+
2471+Aufs5 -- advanced multi layered unification filesystem version 5.x
2472+http://aufs.sf.net
2473+Junjiro R. Okajima
2474+
2475+
2476+0. Introduction
2477+----------------------------------------
2478+In the early days, aufs was entirely re-designed and re-implemented
2479+Unionfs Version 1.x series. Adding many original ideas, approaches,
2480+improvements and implementations, it became totally different from
2481+Unionfs while keeping the basic features.
2482+Later, Unionfs Version 2.x series began taking some of the same
2483+approaches to aufs1's.
2484+Unionfs was being developed by Professor Erez Zadok at Stony Brook
2485+University and his team.
2486+
2487+Aufs5 supports linux-v5.0 and later, If you want older kernel version
2488+support,
2489+- for linux-v4.x series, try aufs4-linux.git or aufs4-standalone.git
2490+- for linux-v3.x series, try aufs3-linux.git or aufs3-standalone.git
2491+- for linux-v2.6.16 and later, try aufs2-2.6.git, aufs2-standalone.git
2492+ or aufs1 from CVS on SourceForge.
2493+
2494+Note: it becomes clear that "Aufs was rejected. Let's give it up."
2495+ According to Christoph Hellwig, linux rejects all union-type
2496+ filesystems but UnionMount.
2497+<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
2498+
2499+PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
2500+ UnionMount, and he pointed out an issue around a directory mutex
2501+ lock and aufs addressed it. But it is still unsure whether aufs will
2502+ be merged (or any other union solution).
2503+<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
2504+
2505+
2506+1. Features
2507+----------------------------------------
2508+- unite several directories into a single virtual filesystem. The member
2509+ directory is called as a branch.
2510+- you can specify the permission flags to the branch, which are 'readonly',
2511+ 'readwrite' and 'whiteout-able.'
2512+- by upper writable branch, internal copyup and whiteout, files/dirs on
2513+ readonly branch are modifiable logically.
2514+- dynamic branch manipulation, add, del.
2515+- etc...
2516+
2517+Also there are many enhancements in aufs, such as:
2518+- test only the highest one for the directory permission (dirperm1)
2519+- copyup on open (coo=)
2520+- 'move' policy for copy-up between two writable branches, after
2521+ checking free space.
2522+- xattr, acl
2523+- readdir(3) in userspace.
2524+- keep inode number by external inode number table
2525+- keep the timestamps of file/dir in internal copyup operation
2526+- seekable directory, supporting NFS readdir.
2527+- whiteout is hardlinked in order to reduce the consumption of inodes
2528+ on branch
2529+- do not copyup, nor create a whiteout when it is unnecessary
2530+- revert a single systemcall when an error occurs in aufs
2531+- remount interface instead of ioctl
2532+- maintain /etc/mtab by an external command, /sbin/mount.aufs.
2533+- loopback mounted filesystem as a branch
2534+- kernel thread for removing the dir who has a plenty of whiteouts
2535+- support copyup sparse file (a file which has a 'hole' in it)
2536+- default permission flags for branches
2537+- selectable permission flags for ro branch, whether whiteout can
2538+ exist or not
2539+- export via NFS.
2540+- support <sysfs>/fs/aufs and <debugfs>/aufs.
2541+- support multiple writable branches, some policies to select one
2542+ among multiple writable branches.
2543+- a new semantics for link(2) and rename(2) to support multiple
2544+ writable branches.
2545+- no glibc changes are required.
2546+- pseudo hardlink (hardlink over branches)
2547+- allow a direct access manually to a file on branch, e.g. bypassing aufs.
2548+ including NFS or remote filesystem branch.
2549+- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
2550+- and more...
2551+
2552+Currently these features are dropped temporary from aufs5.
2553+See design/08plan.txt in detail.
2554+- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
2555+ (robr)
2556+- statistics of aufs thread (/sys/fs/aufs/stat)
2557+
2558+Features or just an idea in the future (see also design/*.txt),
2559+- reorder the branch index without del/re-add.
2560+- permanent xino files for NFSD
2561+- an option for refreshing the opened files after add/del branches
2562+- light version, without branch manipulation. (unnecessary?)
2563+- copyup in userspace
2564+- inotify in userspace
2565+- readv/writev
2566+
2567+
2568+2. Download
2569+----------------------------------------
2570+There are three GIT trees for aufs5, aufs5-linux.git,
2571+aufs5-standalone.git, and aufs-util.git. Note that there is no "5" in
2572+"aufs-util.git."
2573+While the aufs-util is always necessary, you need either of aufs5-linux
2574+or aufs5-standalone.
2575+
2576+The aufs5-linux tree includes the whole linux mainline GIT tree,
2577+git://git.kernel.org/.../torvalds/linux.git.
2578+And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
2579+build aufs5 as an external kernel module.
2580+Several extra patches are not included in this tree. Only
2581+aufs5-standalone tree contains them. They are described in the later
2582+section "Configuration and Compilation."
2583+
2584+On the other hand, the aufs5-standalone tree has only aufs source files
2585+and necessary patches, and you can select CONFIG_AUFS_FS=m.
2586+But you need to apply all aufs patches manually.
2587+
2588+You will find GIT branches whose name is in form of "aufs5.x" where "x"
2589+represents the linux kernel version, "linux-5.x". For instance,
2590+"aufs5.0" is for linux-5.0. For latest "linux-5.x-rcN", use
2591+"aufs5.x-rcN" branch.
2592+
2593+o aufs5-linux tree
2594+$ git clone --reference /your/linux/git/tree \
2595+ git://github.com/sfjro/aufs5-linux.git aufs5-linux.git
2596+- if you don't have linux GIT tree, then remove "--reference ..."
2597+$ cd aufs5-linux.git
2598+$ git checkout origin/aufs5.0
2599+
2600+Or You may want to directly git-pull aufs into your linux GIT tree, and
2601+leave the patch-work to GIT.
2602+$ cd /your/linux/git/tree
2603+$ git remote add aufs5 git://github.com/sfjro/aufs5-linux.git
2604+$ git fetch aufs5
2605+$ git checkout -b my5.0 v5.0
2606+$ (add your local change...)
2607+$ git pull aufs5 aufs5.0
2608+- now you have v5.0 + your_changes + aufs5.0 in you my5.0 branch.
2609+- you may need to solve some conflicts between your_changes and
2610+ aufs5.0. in this case, git-rerere is recommended so that you can
2611+ solve the similar conflicts automatically when you upgrade to 5.1 or
2612+ later in the future.
2613+
2614+o aufs5-standalone tree
2615+$ git clone git://github.com/sfjro/aufs5-standalone.git aufs5-standalone.git
2616+$ cd aufs5-standalone.git
2617+$ git checkout origin/aufs5.0
2618+
2619+o aufs-util tree
2620+$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
2621+- note that the public aufs-util.git is on SourceForge instead of
2622+ GitHUB.
2623+$ cd aufs-util.git
2624+$ git checkout origin/aufs5.0
2625+
2626+Note: The 5.x-rcN branch is to be used with `rc' kernel versions ONLY.
2627+The minor version number, 'x' in '5.x', of aufs may not always
2628+follow the minor version number of the kernel.
2629+Because changes in the kernel that cause the use of a new
2630+minor version number do not always require changes to aufs-util.
2631+
2632+Since aufs-util has its own minor version number, you may not be
2633+able to find a GIT branch in aufs-util for your kernel's
2634+exact minor version number.
2635+In this case, you should git-checkout the branch for the
2636+nearest lower number.
2637+
2638+For (an unreleased) example:
2639+If you are using "linux-5.10" and the "aufs5.10" branch
2640+does not exist in aufs-util repository, then "aufs5.9", "aufs5.8"
2641+or something numerically smaller is the branch for your kernel.
2642+
2643+Also you can view all branches by
2644+ $ git branch -a
2645+
2646+
2647+3. Configuration and Compilation
2648+----------------------------------------
2649+Make sure you have git-checkout'ed the correct branch.
2650+
2651+For aufs5-linux tree,
2652+- enable CONFIG_AUFS_FS.
2653+- set other aufs configurations if necessary.
2654+- for aufs5.13 and later
2655+ Because aufs is not only an ordinary filesystem (callee of VFS), but
2656+ also a caller of VFS functions for branch filesystems, subclassing of
2657+ the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
2658+ feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
2659+ need to customize some LOCKDEP numbers. Here are what I use on my
2660+ test environment.
2661+ CONFIG_LOCKDEP_BITS=21
2662+ CONFIG_LOCKDEP_CHAINS_BITS=21
2663+ CONFIG_LOCKDEP_STACK_TRACE_BITS=24
2664+
2665+For aufs5-standalone tree,
2666+There are several ways to build.
2667+
2668+1.
2669+- apply ./aufs5-kbuild.patch to your kernel source files.
2670+- apply ./aufs5-base.patch too.
2671+- apply ./aufs5-mmap.patch too.
2672+- apply ./aufs5-standalone.patch too, if you have a plan to set
2673+ CONFIG_AUFS_FS=m. otherwise you don't need ./aufs5-standalone.patch.
2674+- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2675+ kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
2676+- enable CONFIG_AUFS_FS, you can select either
2677+ =m or =y.
2678+- and build your kernel as usual.
2679+- install the built kernel.
2680+- install the header files too by "make headers_install" to the
2681+ directory where you specify. By default, it is $PWD/usr.
2682+ "make help" shows a brief note for headers_install.
2683+- and reboot your system.
2684+
2685+2.
2686+- module only (CONFIG_AUFS_FS=m).
2687+- apply ./aufs5-base.patch to your kernel source files.
2688+- apply ./aufs5-mmap.patch too.
2689+- apply ./aufs5-standalone.patch too.
2690+- build your kernel, don't forget "make headers_install", and reboot.
2691+- edit ./config.mk and set other aufs configurations if necessary.
2692+ Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
2693+ every aufs configurations.
2694+- build the module by simple "make".
2695+- you can specify ${KDIR} make variable which points to your kernel
2696+ source tree.
2697+- install the files
2698+ + run "make install" to install the aufs module, or copy the built
2699+ $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2700+ + run "make install_headers" (instead of headers_install) to install
2701+ the modified aufs header file (you can specify DESTDIR which is
2702+ available in aufs standalone version's Makefile only), or copy
2703+ $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2704+ you like manually. By default, the target directory is $PWD/usr.
2705+- no need to apply aufs5-kbuild.patch, nor copying source files to your
2706+ kernel source tree.
2707+
2708+Note: The header file aufs_type.h is necessary to build aufs-util
2709+ as well as "make headers_install" in the kernel source tree.
2710+ headers_install is subject to be forgotten, but it is essentially
2711+ necessary, not only for building aufs-util.
2712+ You may not meet problems without headers_install in some older
2713+ version though.
2714+
2715+And then,
2716+- read README in aufs-util, build and install it
2717+- note that your distribution may contain an obsoleted version of
2718+ aufs_type.h in /usr/include/linux or something. When you build aufs
2719+ utilities, make sure that your compiler refers the correct aufs header
2720+ file which is built by "make headers_install."
2721+- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2722+ then run "make install_ulib" too. And refer to the aufs manual in
2723+ detail.
2724+
2725+There several other patches in aufs5-standalone.git. They are all
2726+optional. When you meet some problems, they will help you.
2727+- aufs5-loopback.patch
2728+ Supports a nested loopback mount in a branch-fs. This patch is
2729+ unnecessary until aufs produces a message like "you may want to try
2730+ another patch for loopback file".
2731+- vfs-ino.patch
2732+ Modifies a system global kernel internal function get_next_ino() in
2733+ order to stop assigning 0 for an inode-number. Not directly related to
2734+ aufs, but recommended generally.
2735+- tmpfs-idr.patch
2736+ Keeps the tmpfs inode number as the lowest value. Effective to reduce
2737+ the size of aufs XINO files for tmpfs branch. Also it prevents the
2738+ duplication of inode number, which is important for backup tools and
2739+ other utilities. When you find aufs XINO files for tmpfs branch
2740+ growing too much, try this patch.
2741+
2742+
2743+4. Usage
2744+----------------------------------------
2745+At first, make sure aufs-util are installed, and please read the aufs
2746+manual, aufs.5 in aufs-util.git tree.
2747+$ man -l aufs.5
2748+
2749+And then,
2750+$ mkdir /tmp/rw /tmp/aufs
2751+# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2752+
2753+Here is another example. The result is equivalent.
2754+# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2755+ Or
2756+# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2757+# mount -o remount,append:${HOME} /tmp/aufs
2758+
2759+Then, you can see whole tree of your home dir through /tmp/aufs. If
2760+you modify a file under /tmp/aufs, the one on your home directory is
2761+not affected, instead the same named file will be newly created under
2762+/tmp/rw. And all of your modification to a file will be applied to
2763+the one under /tmp/rw. This is called the file based Copy on Write
2764+(COW) method.
2765+Aufs mount options are described in aufs.5.
2766+If you run chroot or something and make your aufs as a root directory,
2767+then you need to customize the shutdown script. See the aufs manual in
2768+detail.
2769+
2770+Additionally, there are some sample usages of aufs which are a
2771+diskless system with network booting, and LiveCD over NFS.
2772+See sample dir in CVS tree on SourceForge.
2773+
2774+
2775+5. Contact
2776+----------------------------------------
2777+When you have any problems or strange behaviour in aufs, please let me
2778+know with:
2779+- /proc/mounts (instead of the output of mount(8))
2780+- /sys/module/aufs/*
2781+- /sys/fs/aufs/* (if you have them)
2782+- /debug/aufs/* (if you have them)
2783+- linux kernel version
2784+ if your kernel is not plain, for example modified by distributor,
2785+ the url where i can download its source is necessary too.
2786+- aufs version which was printed at loading the module or booting the
2787+ system, instead of the date you downloaded.
2788+- configuration (define/undefine CONFIG_AUFS_xxx)
2789+- kernel configuration or /proc/config.gz (if you have it)
2790+- LSM (linux security module, if you are using)
2791+- behaviour which you think to be incorrect
2792+- actual operation, reproducible one is better
2793+- mailto: aufs-users at lists.sourceforge.net
2794+
2795+Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
2796+and Feature Requests) on SourceForge. Please join and write to
2797+aufs-users ML.
2798+
2799+
2800+6. Acknowledgements
2801+----------------------------------------
2802+Thanks to everyone who have tried and are using aufs, whoever
2803+have reported a bug or any feedback.
2804+
2805+Especially donators:
2806+Tomas Matejicek(slax.org) made a donation (much more than once).
2807+ Since Apr 2010, Tomas M (the author of Slax and Linux Live
2808+ scripts) is making "doubling" donations.
2809+ Unfortunately I cannot list all of the donators, but I really
2810+ appreciate.
2811+ It ends Aug 2010, but the ordinary donation URL is still available.
2812+ <http://sourceforge.net/donate/index.php?group_id=167503>
2813+Dai Itasaka made a donation (2007/8).
2814+Chuck Smith made a donation (2008/4, 10 and 12).
2815+Henk Schoneveld made a donation (2008/9).
2816+Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
2817+Francois Dupoux made a donation (2008/11).
2818+Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
2819+ aufs2 GIT tree (2009/2).
2820+William Grant made a donation (2009/3).
2821+Patrick Lane made a donation (2009/4).
2822+The Mail Archive (mail-archive.com) made donations (2009/5).
2823+Nippy Networks (Ed Wildgoose) made a donation (2009/7).
2824+New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
2825+Pavel Pronskiy made a donation (2011/2).
2826+Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
2827+ Networks (Ed Wildgoose) made a donation for hardware (2011/3).
2828+Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
2829+11).
2830+Sam Liddicott made a donation (2011/9).
2831+Era Scarecrow made a donation (2013/4).
2832+Bor Ratajc made a donation (2013/4).
2833+Alessandro Gorreta made a donation (2013/4).
2834+POIRETTE Marc made a donation (2013/4).
2835+Alessandro Gorreta made a donation (2013/4).
2836+lauri kasvandik made a donation (2013/5).
2837+"pemasu from Finland" made a donation (2013/7).
2838+The Parted Magic Project made a donation (2013/9 and 11).
2839+Pavel Barta made a donation (2013/10).
2840+Nikolay Pertsev made a donation (2014/5).
2841+James B made a donation (2014/7 and 2015/7).
2842+Stefano Di Biase made a donation (2014/8).
2843+Daniel Epellei made a donation (2015/1).
2844+OmegaPhil made a donation (2016/1, 2018/4).
2845+Tomasz Szewczyk made a donation (2016/4).
2846+James Burry made a donation (2016/12).
2847+Carsten Rose made a donation (2018/9).
2848+Porteus Kiosk made a donation (2018/10).
2849+
2850+Thank you very much.
2851+Donations are always, including future donations, very important and
2852+helpful for me to keep on developing aufs.
2853+
2854+
2855+7.
2856+----------------------------------------
2857+If you are an experienced user, no explanation is needed. Aufs is
2858+just a linux filesystem.
2859+
2860+
2861+Enjoy!
2862+
2863+# Local variables: ;
2864+# mode: text;
2865+# End: ;
2866diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
2867--- /usr/share/empty/fs/aufs/aufs.h 1970-01-01 01:00:00.000000000 +0100
2868+++ linux/fs/aufs/aufs.h 2021-02-24 13:33:42.741013619 +0100
2869@@ -0,0 +1,62 @@
2870+/* SPDX-License-Identifier: GPL-2.0 */
2871+/*
2872+ * Copyright (C) 2005-2020 Junjiro R. Okajima
2873+ *
2874+ * This program, aufs is free software; you can redistribute it and/or modify
2875+ * it under the terms of the GNU General Public License as published by
2876+ * the Free Software Foundation; either version 2 of the License, or
2877+ * (at your option) any later version.
2878+ *
2879+ * This program is distributed in the hope that it will be useful,
2880+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
2881+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2882+ * GNU General Public License for more details.
2883+ *
2884+ * You should have received a copy of the GNU General Public License
2885+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
2886+ */
2887+
2888+/*
2889+ * all header files
2890+ */
2891+
2892+#ifndef __AUFS_H__
2893+#define __AUFS_H__
2894+
2895+#ifdef __KERNEL__
2896+
2897+#define AuStub(type, name, body, ...) \
2898+ static inline type name(__VA_ARGS__) { body; }
2899+
2900+#define AuStubVoid(name, ...) \
2901+ AuStub(void, name, , __VA_ARGS__)
2902+#define AuStubInt0(name, ...) \
2903+ AuStub(int, name, return 0, __VA_ARGS__)
2904+
2905+#include "debug.h"
2906+
2907+#include "branch.h"
2908+#include "cpup.h"
2909+#include "dcsub.h"
2910+#include "dbgaufs.h"
2911+#include "dentry.h"
2912+#include "dir.h"
2913+#include "dirren.h"
2914+#include "dynop.h"
2915+#include "file.h"
2916+#include "fstype.h"
2917+#include "hbl.h"
2918+#include "inode.h"
2919+#include "lcnt.h"
2920+#include "loop.h"
2921+#include "module.h"
2922+#include "opts.h"
2923+#include "rwsem.h"
2924+#include "super.h"
2925+#include "sysaufs.h"
2926+#include "vfsub.h"
2927+#include "whout.h"
2928+#include "wkq.h"
2929+
2930+#endif /* __KERNEL__ */
2931+#endif /* __AUFS_H__ */
2932diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
2933--- /usr/share/empty/fs/aufs/branch.c 1970-01-01 01:00:00.000000000 +0100
2934+++ linux/fs/aufs/branch.c 2021-02-24 13:33:42.741013619 +0100
2935@@ -0,0 +1,1427 @@
2936+// SPDX-License-Identifier: GPL-2.0
2937+/*
2938+ * Copyright (C) 2005-2020 Junjiro R. Okajima
2939+ *
2940+ * This program, aufs is free software; you can redistribute it and/or modify
2941+ * it under the terms of the GNU General Public License as published by
2942+ * the Free Software Foundation; either version 2 of the License, or
2943+ * (at your option) any later version.
2944+ *
2945+ * This program is distributed in the hope that it will be useful,
2946+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
2947+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2948+ * GNU General Public License for more details.
2949+ *
2950+ * You should have received a copy of the GNU General Public License
2951+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
2952+ */
2953+
2954+/*
2955+ * branch management
2956+ */
2957+
2958+#include <linux/compat.h>
2959+#include <linux/statfs.h>
2960+#include "aufs.h"
2961+
2962+/*
2963+ * free a single branch
2964+ */
2965+static void au_br_do_free(struct au_branch *br)
2966+{
2967+ int i;
2968+ struct au_wbr *wbr;
2969+ struct au_dykey **key;
2970+
2971+ au_hnotify_fin_br(br);
2972+ /* always, regardless the mount option */
2973+ au_dr_hino_free(&br->br_dirren);
2974+ au_xino_put(br);
2975+
2976+ AuLCntZero(au_lcnt_read(&br->br_nfiles, /*do_rev*/0));
2977+ au_lcnt_fin(&br->br_nfiles, /*do_sync*/0);
2978+ AuLCntZero(au_lcnt_read(&br->br_count, /*do_rev*/0));
2979+ au_lcnt_fin(&br->br_count, /*do_sync*/0);
2980+
2981+ wbr = br->br_wbr;
2982+ if (wbr) {
2983+ for (i = 0; i < AuBrWh_Last; i++)
2984+ dput(wbr->wbr_wh[i]);
2985+ AuDebugOn(atomic_read(&wbr->wbr_wh_running));
2986+ AuRwDestroy(&wbr->wbr_wh_rwsem);
2987+ }
2988+
2989+ if (br->br_fhsm) {
2990+ au_br_fhsm_fin(br->br_fhsm);
2991+ au_kfree_try_rcu(br->br_fhsm);
2992+ }
2993+
2994+ key = br->br_dykey;
2995+ for (i = 0; i < AuBrDynOp; i++, key++)
2996+ if (*key)
2997+ au_dy_put(*key);
2998+ else
2999+ break;
3000+
3001+ /* recursive lock, s_umount of branch's */
3002+ /* synchronize_rcu(); */ /* why? */
3003+ lockdep_off();
3004+ path_put(&br->br_path);
3005+ lockdep_on();
3006+ au_kfree_rcu(wbr);
3007+ au_lcnt_wait_for_fin(&br->br_nfiles);
3008+ au_lcnt_wait_for_fin(&br->br_count);
3009+ /* I don't know why, but percpu_refcount requires this */
3010+ /* synchronize_rcu(); */
3011+ au_kfree_rcu(br);
3012+}
3013+
3014+/*
3015+ * frees all branches
3016+ */
3017+void au_br_free(struct au_sbinfo *sbinfo)
3018+{
3019+ aufs_bindex_t bmax;
3020+ struct au_branch **br;
3021+
3022+ AuRwMustWriteLock(&sbinfo->si_rwsem);
3023+
3024+ bmax = sbinfo->si_bbot + 1;
3025+ br = sbinfo->si_branch;
3026+ while (bmax--)
3027+ au_br_do_free(*br++);
3028+}
3029+
3030+/*
3031+ * find the index of a branch which is specified by @br_id.
3032+ */
3033+int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
3034+{
3035+ aufs_bindex_t bindex, bbot;
3036+
3037+ bbot = au_sbbot(sb);
3038+ for (bindex = 0; bindex <= bbot; bindex++)
3039+ if (au_sbr_id(sb, bindex) == br_id)
3040+ return bindex;
3041+ return -1;
3042+}
3043+
3044+/* ---------------------------------------------------------------------- */
3045+
3046+/*
3047+ * add a branch
3048+ */
3049+
3050+static int test_overlap(struct super_block *sb, struct dentry *h_adding,
3051+ struct dentry *h_root)
3052+{
3053+ if (unlikely(h_adding == h_root
3054+ || au_test_loopback_overlap(sb, h_adding)))
3055+ return 1;
3056+ if (h_adding->d_sb != h_root->d_sb)
3057+ return 0;
3058+ return au_test_subdir(h_adding, h_root)
3059+ || au_test_subdir(h_root, h_adding);
3060+}
3061+
3062+/*
3063+ * returns a newly allocated branch. @new_nbranch is a number of branches
3064+ * after adding a branch.
3065+ */
3066+static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
3067+ int perm)
3068+{
3069+ struct au_branch *add_branch;
3070+ struct dentry *root;
3071+ struct inode *inode;
3072+ int err;
3073+
3074+ err = -ENOMEM;
3075+ add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
3076+ if (unlikely(!add_branch))
3077+ goto out;
3078+ add_branch->br_xino = au_xino_alloc(/*nfile*/1);
3079+ if (unlikely(!add_branch->br_xino))
3080+ goto out_br;
3081+ err = au_hnotify_init_br(add_branch, perm);
3082+ if (unlikely(err))
3083+ goto out_xino;
3084+
3085+ if (au_br_writable(perm)) {
3086+ /* may be freed separately at changing the branch permission */
3087+ add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
3088+ GFP_NOFS);
3089+ if (unlikely(!add_branch->br_wbr))
3090+ goto out_hnotify;
3091+ }
3092+
3093+ if (au_br_fhsm(perm)) {
3094+ err = au_fhsm_br_alloc(add_branch);
3095+ if (unlikely(err))
3096+ goto out_wbr;
3097+ }
3098+
3099+ root = sb->s_root;
3100+ err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
3101+ if (!err)
3102+ err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
3103+ if (!err) {
3104+ inode = d_inode(root);
3105+ err = au_hinode_realloc(au_ii(inode), new_nbranch,
3106+ /*may_shrink*/0);
3107+ }
3108+ if (!err)
3109+ return add_branch; /* success */
3110+
3111+out_wbr:
3112+ au_kfree_rcu(add_branch->br_wbr);
3113+out_hnotify:
3114+ au_hnotify_fin_br(add_branch);
3115+out_xino:
3116+ au_xino_put(add_branch);
3117+out_br:
3118+ au_kfree_rcu(add_branch);
3119+out:
3120+ return ERR_PTR(err);
3121+}
3122+
3123+/*
3124+ * test if the branch permission is legal or not.
3125+ */
3126+static int test_br(struct inode *inode, int brperm, char *path)
3127+{
3128+ int err;
3129+
3130+ err = (au_br_writable(brperm) && IS_RDONLY(inode));
3131+ if (!err)
3132+ goto out;
3133+
3134+ err = -EINVAL;
3135+ pr_err("write permission for readonly mount or inode, %s\n", path);
3136+
3137+out:
3138+ return err;
3139+}
3140+
3141+/*
3142+ * returns:
3143+ * 0: success, the caller will add it
3144+ * plus: success, it is already unified, the caller should ignore it
3145+ * minus: error
3146+ */
3147+static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3148+{
3149+ int err;
3150+ aufs_bindex_t bbot, bindex;
3151+ struct dentry *root, *h_dentry;
3152+ struct inode *inode, *h_inode;
3153+
3154+ root = sb->s_root;
3155+ bbot = au_sbbot(sb);
3156+ if (unlikely(bbot >= 0
3157+ && au_find_dbindex(root, add->path.dentry) >= 0)) {
3158+ err = 1;
3159+ if (!remount) {
3160+ err = -EINVAL;
3161+ pr_err("%s duplicated\n", add->pathname);
3162+ }
3163+ goto out;
3164+ }
3165+
3166+ err = -ENOSPC; /* -E2BIG; */
3167+ if (unlikely(AUFS_BRANCH_MAX <= add->bindex
3168+ || AUFS_BRANCH_MAX - 1 <= bbot)) {
3169+ pr_err("number of branches exceeded %s\n", add->pathname);
3170+ goto out;
3171+ }
3172+
3173+ err = -EDOM;
3174+ if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
3175+ pr_err("bad index %d\n", add->bindex);
3176+ goto out;
3177+ }
3178+
3179+ inode = d_inode(add->path.dentry);
3180+ err = -ENOENT;
3181+ if (unlikely(!inode->i_nlink)) {
3182+ pr_err("no existence %s\n", add->pathname);
3183+ goto out;
3184+ }
3185+
3186+ err = -EINVAL;
3187+ if (unlikely(inode->i_sb == sb)) {
3188+ pr_err("%s must be outside\n", add->pathname);
3189+ goto out;
3190+ }
3191+
3192+ if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
3193+ pr_err("unsupported filesystem, %s (%s)\n",
3194+ add->pathname, au_sbtype(inode->i_sb));
3195+ goto out;
3196+ }
3197+
3198+ if (unlikely(inode->i_sb->s_stack_depth)) {
3199+ pr_err("already stacked, %s (%s)\n",
3200+ add->pathname, au_sbtype(inode->i_sb));
3201+ goto out;
3202+ }
3203+
3204+ err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
3205+ if (unlikely(err))
3206+ goto out;
3207+
3208+ if (bbot < 0)
3209+ return 0; /* success */
3210+
3211+ err = -EINVAL;
3212+ for (bindex = 0; bindex <= bbot; bindex++)
3213+ if (unlikely(test_overlap(sb, add->path.dentry,
3214+ au_h_dptr(root, bindex)))) {
3215+ pr_err("%s is overlapped\n", add->pathname);
3216+ goto out;
3217+ }
3218+
3219+ err = 0;
3220+ if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
3221+ h_dentry = au_h_dptr(root, 0);
3222+ h_inode = d_inode(h_dentry);
3223+ if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
3224+ || !uid_eq(h_inode->i_uid, inode->i_uid)
3225+ || !gid_eq(h_inode->i_gid, inode->i_gid))
3226+ pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3227+ add->pathname,
3228+ i_uid_read(inode), i_gid_read(inode),
3229+ (inode->i_mode & S_IALLUGO),
3230+ i_uid_read(h_inode), i_gid_read(h_inode),
3231+ (h_inode->i_mode & S_IALLUGO));
3232+ }
3233+
3234+out:
3235+ return err;
3236+}
3237+
3238+/*
3239+ * initialize or clean the whiteouts for an adding branch
3240+ */
3241+static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
3242+ int new_perm)
3243+{
3244+ int err, old_perm;
3245+ aufs_bindex_t bindex;
3246+ struct inode *h_inode;
3247+ struct au_wbr *wbr;
3248+ struct au_hinode *hdir;
3249+ struct dentry *h_dentry;
3250+
3251+ err = vfsub_mnt_want_write(au_br_mnt(br));
3252+ if (unlikely(err))
3253+ goto out;
3254+
3255+ wbr = br->br_wbr;
3256+ old_perm = br->br_perm;
3257+ br->br_perm = new_perm;
3258+ hdir = NULL;
3259+ h_inode = NULL;
3260+ bindex = au_br_index(sb, br->br_id);
3261+ if (0 <= bindex) {
3262+ hdir = au_hi(d_inode(sb->s_root), bindex);
3263+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
3264+ } else {
3265+ h_dentry = au_br_dentry(br);
3266+ h_inode = d_inode(h_dentry);
3267+ inode_lock_nested(h_inode, AuLsc_I_PARENT);
3268+ }
3269+ if (!wbr)
3270+ err = au_wh_init(br, sb);
3271+ else {
3272+ wbr_wh_write_lock(wbr);
3273+ err = au_wh_init(br, sb);
3274+ wbr_wh_write_unlock(wbr);
3275+ }
3276+ if (hdir)
3277+ au_hn_inode_unlock(hdir);
3278+ else
3279+ inode_unlock(h_inode);
3280+ vfsub_mnt_drop_write(au_br_mnt(br));
3281+ br->br_perm = old_perm;
3282+
3283+ if (!err && wbr && !au_br_writable(new_perm)) {
3284+ au_kfree_rcu(wbr);
3285+ br->br_wbr = NULL;
3286+ }
3287+
3288+out:
3289+ return err;
3290+}
3291+
3292+static int au_wbr_init(struct au_branch *br, struct super_block *sb,
3293+ int perm)
3294+{
3295+ int err;
3296+ struct kstatfs kst;
3297+ struct au_wbr *wbr;
3298+
3299+ wbr = br->br_wbr;
3300+ au_rw_init(&wbr->wbr_wh_rwsem);
3301+ atomic_set(&wbr->wbr_wh_running, 0);
3302+
3303+ /*
3304+ * a limit for rmdir/rename a dir
3305+ * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
3306+ */
3307+ err = vfs_statfs(&br->br_path, &kst);
3308+ if (unlikely(err))
3309+ goto out;
3310+ err = -EINVAL;
3311+ if (kst.f_namelen >= NAME_MAX)
3312+ err = au_br_init_wh(sb, br, perm);
3313+ else
3314+ pr_err("%pd(%s), unsupported namelen %ld\n",
3315+ au_br_dentry(br),
3316+ au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
3317+
3318+out:
3319+ return err;
3320+}
3321+
3322+/* initialize a new branch */
3323+static int au_br_init(struct au_branch *br, struct super_block *sb,
3324+ struct au_opt_add *add)
3325+{
3326+ int err;
3327+ struct au_branch *brbase;
3328+ struct file *xf;
3329+ struct inode *h_inode;
3330+
3331+ err = 0;
3332+ br->br_perm = add->perm;
3333+ br->br_path = add->path; /* set first, path_get() later */
3334+ spin_lock_init(&br->br_dykey_lock);
3335+ au_lcnt_init(&br->br_nfiles, /*release*/NULL);
3336+ au_lcnt_init(&br->br_count, /*release*/NULL);
3337+ br->br_id = au_new_br_id(sb);
3338+ AuDebugOn(br->br_id < 0);
3339+
3340+ /* always, regardless the given option */
3341+ err = au_dr_br_init(sb, br, &add->path);
3342+ if (unlikely(err))
3343+ goto out_err;
3344+
3345+ if (au_br_writable(add->perm)) {
3346+ err = au_wbr_init(br, sb, add->perm);
3347+ if (unlikely(err))
3348+ goto out_err;
3349+ }
3350+
3351+ if (au_opt_test(au_mntflags(sb), XINO)) {
3352+ brbase = au_sbr(sb, 0);
3353+ xf = au_xino_file(brbase->br_xino, /*idx*/-1);
3354+ AuDebugOn(!xf);
3355+ h_inode = d_inode(add->path.dentry);
3356+ err = au_xino_init_br(sb, br, h_inode->i_ino, &xf->f_path);
3357+ if (unlikely(err)) {
3358+ AuDebugOn(au_xino_file(br->br_xino, /*idx*/-1));
3359+ goto out_err;
3360+ }
3361+ }
3362+
3363+ sysaufs_br_init(br);
3364+ path_get(&br->br_path);
3365+ goto out; /* success */
3366+
3367+out_err:
3368+ memset(&br->br_path, 0, sizeof(br->br_path));
3369+out:
3370+ return err;
3371+}
3372+
3373+static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
3374+ struct au_branch *br, aufs_bindex_t bbot,
3375+ aufs_bindex_t amount)
3376+{
3377+ struct au_branch **brp;
3378+
3379+ AuRwMustWriteLock(&sbinfo->si_rwsem);
3380+
3381+ brp = sbinfo->si_branch + bindex;
3382+ memmove(brp + 1, brp, sizeof(*brp) * amount);
3383+ *brp = br;
3384+ sbinfo->si_bbot++;
3385+ if (unlikely(bbot < 0))
3386+ sbinfo->si_bbot = 0;
3387+}
3388+
3389+static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
3390+ aufs_bindex_t bbot, aufs_bindex_t amount)
3391+{
3392+ struct au_hdentry *hdp;
3393+
3394+ AuRwMustWriteLock(&dinfo->di_rwsem);
3395+
3396+ hdp = au_hdentry(dinfo, bindex);
3397+ memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3398+ au_h_dentry_init(hdp);
3399+ dinfo->di_bbot++;
3400+ if (unlikely(bbot < 0))
3401+ dinfo->di_btop = 0;
3402+}
3403+
3404+static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
3405+ aufs_bindex_t bbot, aufs_bindex_t amount)
3406+{
3407+ struct au_hinode *hip;
3408+
3409+ AuRwMustWriteLock(&iinfo->ii_rwsem);
3410+
3411+ hip = au_hinode(iinfo, bindex);
3412+ memmove(hip + 1, hip, sizeof(*hip) * amount);
3413+ au_hinode_init(hip);
3414+ iinfo->ii_bbot++;
3415+ if (unlikely(bbot < 0))
3416+ iinfo->ii_btop = 0;
3417+}
3418+
3419+static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3420+ aufs_bindex_t bindex)
3421+{
3422+ struct dentry *root, *h_dentry;
3423+ struct inode *root_inode, *h_inode;
3424+ aufs_bindex_t bbot, amount;
3425+
3426+ root = sb->s_root;
3427+ root_inode = d_inode(root);
3428+ bbot = au_sbbot(sb);
3429+ amount = bbot + 1 - bindex;
3430+ h_dentry = au_br_dentry(br);
3431+ au_sbilist_lock();
3432+ au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
3433+ au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
3434+ au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
3435+ au_set_h_dptr(root, bindex, dget(h_dentry));
3436+ h_inode = d_inode(h_dentry);
3437+ au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
3438+ au_sbilist_unlock();
3439+}
3440+
3441+int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3442+{
3443+ int err;
3444+ aufs_bindex_t bbot, add_bindex;
3445+ struct dentry *root, *h_dentry;
3446+ struct inode *root_inode;
3447+ struct au_branch *add_branch;
3448+
3449+ root = sb->s_root;
3450+ root_inode = d_inode(root);
3451+ IMustLock(root_inode);
3452+ IiMustWriteLock(root_inode);
3453+ err = test_add(sb, add, remount);
3454+ if (unlikely(err < 0))
3455+ goto out;
3456+ if (err) {
3457+ err = 0;
3458+ goto out; /* success */
3459+ }
3460+
3461+ bbot = au_sbbot(sb);
3462+ add_branch = au_br_alloc(sb, bbot + 2, add->perm);
3463+ err = PTR_ERR(add_branch);
3464+ if (IS_ERR(add_branch))
3465+ goto out;
3466+
3467+ err = au_br_init(add_branch, sb, add);
3468+ if (unlikely(err)) {
3469+ au_br_do_free(add_branch);
3470+ goto out;
3471+ }
3472+
3473+ add_bindex = add->bindex;
3474+ sysaufs_brs_del(sb, add_bindex); /* remove successors */
3475+ au_br_do_add(sb, add_branch, add_bindex);
3476+ sysaufs_brs_add(sb, add_bindex); /* append successors */
3477+ dbgaufs_brs_add(sb, add_bindex, /*topdown*/0); /* rename successors */
3478+
3479+ h_dentry = add->path.dentry;
3480+ if (!add_bindex) {
3481+ au_cpup_attr_all(root_inode, /*force*/1);
3482+ sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3483+ } else
3484+ au_add_nlink(root_inode, d_inode(h_dentry));
3485+
3486+out:
3487+ return err;
3488+}
3489+
3490+/* ---------------------------------------------------------------------- */
3491+
3492+static unsigned long long au_farray_cb(struct super_block *sb, void *a,
3493+ unsigned long long max __maybe_unused,
3494+ void *arg)
3495+{
3496+ unsigned long long n;
3497+ struct file **p, *f;
3498+ struct hlist_bl_head *files;
3499+ struct hlist_bl_node *pos;
3500+ struct au_finfo *finfo;
3501+
3502+ n = 0;
3503+ p = a;
3504+ files = &au_sbi(sb)->si_files;
3505+ hlist_bl_lock(files);
3506+ hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
3507+ f = finfo->fi_file;
3508+ if (file_count(f)
3509+ && !special_file(file_inode(f)->i_mode)) {
3510+ get_file(f);
3511+ *p++ = f;
3512+ n++;
3513+ AuDebugOn(n > max);
3514+ }
3515+ }
3516+ hlist_bl_unlock(files);
3517+
3518+ return n;
3519+}
3520+
3521+static struct file **au_farray_alloc(struct super_block *sb,
3522+ unsigned long long *max)
3523+{
3524+ struct au_sbinfo *sbi;
3525+
3526+ sbi = au_sbi(sb);
3527+ *max = au_lcnt_read(&sbi->si_nfiles, /*do_rev*/1);
3528+ return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
3529+}
3530+
3531+static void au_farray_free(struct file **a, unsigned long long max)
3532+{
3533+ unsigned long long ull;
3534+
3535+ for (ull = 0; ull < max; ull++)
3536+ if (a[ull])
3537+ fput(a[ull]);
3538+ kvfree(a);
3539+}
3540+
3541+/* ---------------------------------------------------------------------- */
3542+
3543+/*
3544+ * delete a branch
3545+ */
3546+
3547+/* to show the line number, do not make it inlined function */
3548+#define AuVerbose(do_info, fmt, ...) do { \
3549+ if (do_info) \
3550+ pr_info(fmt, ##__VA_ARGS__); \
3551+} while (0)
3552+
3553+static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
3554+ aufs_bindex_t bbot)
3555+{
3556+ return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
3557+}
3558+
3559+static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
3560+ aufs_bindex_t bbot)
3561+{
3562+ return au_test_ibusy(d_inode(dentry), btop, bbot);
3563+}
3564+
3565+/*
3566+ * test if the branch is deletable or not.
3567+ */
3568+static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
3569+ unsigned int sigen, const unsigned int verbose)
3570+{
3571+ int err, i, j, ndentry;
3572+ aufs_bindex_t btop, bbot;
3573+ struct au_dcsub_pages dpages;
3574+ struct au_dpage *dpage;
3575+ struct dentry *d;
3576+
3577+ err = au_dpages_init(&dpages, GFP_NOFS);
3578+ if (unlikely(err))
3579+ goto out;
3580+ err = au_dcsub_pages(&dpages, root, NULL, NULL);
3581+ if (unlikely(err))
3582+ goto out_dpages;
3583+
3584+ for (i = 0; !err && i < dpages.ndpage; i++) {
3585+ dpage = dpages.dpages + i;
3586+ ndentry = dpage->ndentry;
3587+ for (j = 0; !err && j < ndentry; j++) {
3588+ d = dpage->dentries[j];
3589+ AuDebugOn(au_dcount(d) <= 0);
3590+ if (!au_digen_test(d, sigen)) {
3591+ di_read_lock_child(d, AuLock_IR);
3592+ if (unlikely(au_dbrange_test(d))) {
3593+ di_read_unlock(d, AuLock_IR);
3594+ continue;
3595+ }
3596+ } else {
3597+ di_write_lock_child(d);
3598+ if (unlikely(au_dbrange_test(d))) {
3599+ di_write_unlock(d);
3600+ continue;
3601+ }
3602+ err = au_reval_dpath(d, sigen);
3603+ if (!err)
3604+ di_downgrade_lock(d, AuLock_IR);
3605+ else {
3606+ di_write_unlock(d);
3607+ break;
3608+ }
3609+ }
3610+
3611+ /* AuDbgDentry(d); */
3612+ btop = au_dbtop(d);
3613+ bbot = au_dbbot(d);
3614+ if (btop <= bindex
3615+ && bindex <= bbot
3616+ && au_h_dptr(d, bindex)
3617+ && au_test_dbusy(d, btop, bbot)) {
3618+ err = -EBUSY;
3619+ AuVerbose(verbose, "busy %pd\n", d);
3620+ AuDbgDentry(d);
3621+ }
3622+ di_read_unlock(d, AuLock_IR);
3623+ }
3624+ }
3625+
3626+out_dpages:
3627+ au_dpages_free(&dpages);
3628+out:
3629+ return err;
3630+}
3631+
3632+static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
3633+ unsigned int sigen, const unsigned int verbose)
3634+{
3635+ int err;
3636+ unsigned long long max, ull;
3637+ struct inode *i, **array;
3638+ aufs_bindex_t btop, bbot;
3639+
3640+ array = au_iarray_alloc(sb, &max);
3641+ err = PTR_ERR(array);
3642+ if (IS_ERR(array))
3643+ goto out;
3644+
3645+ err = 0;
3646+ AuDbg("b%d\n", bindex);
3647+ for (ull = 0; !err && ull < max; ull++) {
3648+ i = array[ull];
3649+ if (unlikely(!i))
3650+ break;
3651+ if (i->i_ino == AUFS_ROOT_INO)
3652+ continue;
3653+
3654+ /* AuDbgInode(i); */
3655+ if (au_iigen(i, NULL) == sigen)
3656+ ii_read_lock_child(i);
3657+ else {
3658+ ii_write_lock_child(i);
3659+ err = au_refresh_hinode_self(i);
3660+ au_iigen_dec(i);
3661+ if (!err)
3662+ ii_downgrade_lock(i);
3663+ else {
3664+ ii_write_unlock(i);
3665+ break;
3666+ }
3667+ }
3668+
3669+ btop = au_ibtop(i);
3670+ bbot = au_ibbot(i);
3671+ if (btop <= bindex
3672+ && bindex <= bbot
3673+ && au_h_iptr(i, bindex)
3674+ && au_test_ibusy(i, btop, bbot)) {
3675+ err = -EBUSY;
3676+ AuVerbose(verbose, "busy i%lu\n", i->i_ino);
3677+ AuDbgInode(i);
3678+ }
3679+ ii_read_unlock(i);
3680+ }
3681+ au_iarray_free(array, max);
3682+
3683+out:
3684+ return err;
3685+}
3686+
3687+static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3688+ const unsigned int verbose)
3689+{
3690+ int err;
3691+ unsigned int sigen;
3692+
3693+ sigen = au_sigen(root->d_sb);
3694+ DiMustNoWaiters(root);
3695+ IiMustNoWaiters(d_inode(root));
3696+ di_write_unlock(root);
3697+ err = test_dentry_busy(root, bindex, sigen, verbose);
3698+ if (!err)
3699+ err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
3700+ di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3701+
3702+ return err;
3703+}
3704+
3705+static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3706+ struct file **to_free, int *idx)
3707+{
3708+ int err;
3709+ unsigned char matched, root;
3710+ aufs_bindex_t bindex, bbot;
3711+ struct au_fidir *fidir;
3712+ struct au_hfile *hfile;
3713+
3714+ err = 0;
3715+ root = IS_ROOT(file->f_path.dentry);
3716+ if (root) {
3717+ get_file(file);
3718+ to_free[*idx] = file;
3719+ (*idx)++;
3720+ goto out;
3721+ }
3722+
3723+ matched = 0;
3724+ fidir = au_fi(file)->fi_hdir;
3725+ AuDebugOn(!fidir);
3726+ bbot = au_fbbot_dir(file);
3727+ for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
3728+ hfile = fidir->fd_hfile + bindex;
3729+ if (!hfile->hf_file)
3730+ continue;
3731+
3732+ if (hfile->hf_br->br_id == br_id) {
3733+ matched = 1;
3734+ break;
3735+ }
3736+ }
3737+ if (matched)
3738+ err = -EBUSY;
3739+
3740+out:
3741+ return err;
3742+}
3743+
3744+static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3745+ struct file **to_free, int opened)
3746+{
3747+ int err, idx;
3748+ unsigned long long ull, max;
3749+ aufs_bindex_t btop;
3750+ struct file *file, **array;
3751+ struct dentry *root;
3752+ struct au_hfile *hfile;
3753+
3754+ array = au_farray_alloc(sb, &max);
3755+ err = PTR_ERR(array);
3756+ if (IS_ERR(array))
3757+ goto out;
3758+
3759+ err = 0;
3760+ idx = 0;
3761+ root = sb->s_root;
3762+ di_write_unlock(root);
3763+ for (ull = 0; ull < max; ull++) {
3764+ file = array[ull];
3765+ if (unlikely(!file))
3766+ break;
3767+
3768+ /* AuDbg("%pD\n", file); */
3769+ fi_read_lock(file);
3770+ btop = au_fbtop(file);
3771+ if (!d_is_dir(file->f_path.dentry)) {
3772+ hfile = &au_fi(file)->fi_htop;
3773+ if (hfile->hf_br->br_id == br_id)
3774+ err = -EBUSY;
3775+ } else
3776+ err = test_dir_busy(file, br_id, to_free, &idx);
3777+ fi_read_unlock(file);
3778+ if (unlikely(err))
3779+ break;
3780+ }
3781+ di_write_lock_child(root);
3782+ au_farray_free(array, max);
3783+ AuDebugOn(idx > opened);
3784+
3785+out:
3786+ return err;
3787+}
3788+
3789+static void br_del_file(struct file **to_free, unsigned long long opened,
3790+ aufs_bindex_t br_id)
3791+{
3792+ unsigned long long ull;
3793+ aufs_bindex_t bindex, btop, bbot, bfound;
3794+ struct file *file;
3795+ struct au_fidir *fidir;
3796+ struct au_hfile *hfile;
3797+
3798+ for (ull = 0; ull < opened; ull++) {
3799+ file = to_free[ull];
3800+ if (unlikely(!file))
3801+ break;
3802+
3803+ /* AuDbg("%pD\n", file); */
3804+ AuDebugOn(!d_is_dir(file->f_path.dentry));
3805+ bfound = -1;
3806+ fidir = au_fi(file)->fi_hdir;
3807+ AuDebugOn(!fidir);
3808+ fi_write_lock(file);
3809+ btop = au_fbtop(file);
3810+ bbot = au_fbbot_dir(file);
3811+ for (bindex = btop; bindex <= bbot; bindex++) {
3812+ hfile = fidir->fd_hfile + bindex;
3813+ if (!hfile->hf_file)
3814+ continue;
3815+
3816+ if (hfile->hf_br->br_id == br_id) {
3817+ bfound = bindex;
3818+ break;
3819+ }
3820+ }
3821+ AuDebugOn(bfound < 0);
3822+ au_set_h_fptr(file, bfound, NULL);
3823+ if (bfound == btop) {
3824+ for (btop++; btop <= bbot; btop++)
3825+ if (au_hf_dir(file, btop)) {
3826+ au_set_fbtop(file, btop);
3827+ break;
3828+ }
3829+ }
3830+ fi_write_unlock(file);
3831+ }
3832+}
3833+
3834+static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
3835+ const aufs_bindex_t bindex,
3836+ const aufs_bindex_t bbot)
3837+{
3838+ struct au_branch **brp, **p;
3839+
3840+ AuRwMustWriteLock(&sbinfo->si_rwsem);
3841+
3842+ brp = sbinfo->si_branch + bindex;
3843+ if (bindex < bbot)
3844+ memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
3845+ sbinfo->si_branch[0 + bbot] = NULL;
3846+ sbinfo->si_bbot--;
3847+
3848+ p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
3849+ /*may_shrink*/1);
3850+ if (p)
3851+ sbinfo->si_branch = p;
3852+ /* harmless error */
3853+}
3854+
3855+static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
3856+ const aufs_bindex_t bbot)
3857+{
3858+ struct au_hdentry *hdp, *p;
3859+
3860+ AuRwMustWriteLock(&dinfo->di_rwsem);
3861+
3862+ hdp = au_hdentry(dinfo, bindex);
3863+ if (bindex < bbot)
3864+ memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
3865+ /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
3866+ dinfo->di_bbot--;
3867+
3868+ p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
3869+ /*may_shrink*/1);
3870+ if (p)
3871+ dinfo->di_hdentry = p;
3872+ /* harmless error */
3873+}
3874+
3875+static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
3876+ const aufs_bindex_t bbot)
3877+{
3878+ struct au_hinode *hip, *p;
3879+
3880+ AuRwMustWriteLock(&iinfo->ii_rwsem);
3881+
3882+ hip = au_hinode(iinfo, bindex);
3883+ if (bindex < bbot)
3884+ memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
3885+ /* au_hinode_init(au_hinode(iinfo, bbot)); */
3886+ iinfo->ii_bbot--;
3887+
3888+ p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
3889+ /*may_shrink*/1);
3890+ if (p)
3891+ iinfo->ii_hinode = p;
3892+ /* harmless error */
3893+}
3894+
3895+static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
3896+ struct au_branch *br)
3897+{
3898+ aufs_bindex_t bbot;
3899+ struct au_sbinfo *sbinfo;
3900+ struct dentry *root, *h_root;
3901+ struct inode *inode, *h_inode;
3902+ struct au_hinode *hinode;
3903+
3904+ SiMustWriteLock(sb);
3905+
3906+ root = sb->s_root;
3907+ inode = d_inode(root);
3908+ sbinfo = au_sbi(sb);
3909+ bbot = sbinfo->si_bbot;
3910+
3911+ h_root = au_h_dptr(root, bindex);
3912+ hinode = au_hi(inode, bindex);
3913+ h_inode = au_igrab(hinode->hi_inode);
3914+ au_hiput(hinode);
3915+
3916+ au_sbilist_lock();
3917+ au_br_do_del_brp(sbinfo, bindex, bbot);
3918+ au_br_do_del_hdp(au_di(root), bindex, bbot);
3919+ au_br_do_del_hip(au_ii(inode), bindex, bbot);
3920+ au_sbilist_unlock();
3921+
3922+ /* ignore an error */
3923+ au_dr_br_fin(sb, br); /* always, regardless the mount option */
3924+
3925+ dput(h_root);
3926+ iput(h_inode);
3927+ au_br_do_free(br);
3928+}
3929+
3930+static unsigned long long empty_cb(struct super_block *sb, void *array,
3931+ unsigned long long max, void *arg)
3932+{
3933+ return max;
3934+}
3935+
3936+int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
3937+{
3938+ int err, rerr, i;
3939+ unsigned long long opened;
3940+ unsigned int mnt_flags;
3941+ aufs_bindex_t bindex, bbot, br_id;
3942+ unsigned char do_wh, verbose;
3943+ struct au_branch *br;
3944+ struct au_wbr *wbr;
3945+ struct dentry *root;
3946+ struct file **to_free;
3947+
3948+ err = 0;
3949+ opened = 0;
3950+ to_free = NULL;
3951+ root = sb->s_root;
3952+ bindex = au_find_dbindex(root, del->h_path.dentry);
3953+ if (bindex < 0) {
3954+ if (remount)
3955+ goto out; /* success */
3956+ err = -ENOENT;
3957+ pr_err("%s no such branch\n", del->pathname);
3958+ goto out;
3959+ }
3960+ AuDbg("bindex b%d\n", bindex);
3961+
3962+ err = -EBUSY;
3963+ mnt_flags = au_mntflags(sb);
3964+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
3965+ bbot = au_sbbot(sb);
3966+ if (unlikely(!bbot)) {
3967+ AuVerbose(verbose, "no more branches left\n");
3968+ goto out;
3969+ }
3970+
3971+ br = au_sbr(sb, bindex);
3972+ AuDebugOn(!path_equal(&br->br_path, &del->h_path));
3973+ if (unlikely(au_lcnt_read(&br->br_count, /*do_rev*/1))) {
3974+ AuVerbose(verbose, "br %pd2 is busy now\n", del->h_path.dentry);
3975+ goto out;
3976+ }
3977+
3978+ br_id = br->br_id;
3979+ opened = au_lcnt_read(&br->br_nfiles, /*do_rev*/1);
3980+ if (unlikely(opened)) {
3981+ to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
3982+ err = PTR_ERR(to_free);
3983+ if (IS_ERR(to_free))
3984+ goto out;
3985+
3986+ err = test_file_busy(sb, br_id, to_free, opened);
3987+ if (unlikely(err)) {
3988+ AuVerbose(verbose, "%llu file(s) opened\n", opened);
3989+ goto out;
3990+ }
3991+ }
3992+
3993+ wbr = br->br_wbr;
3994+ do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
3995+ if (do_wh) {
3996+ /* instead of WbrWhMustWriteLock(wbr) */
3997+ SiMustWriteLock(sb);
3998+ for (i = 0; i < AuBrWh_Last; i++) {
3999+ dput(wbr->wbr_wh[i]);
4000+ wbr->wbr_wh[i] = NULL;
4001+ }
4002+ }
4003+
4004+ err = test_children_busy(root, bindex, verbose);
4005+ if (unlikely(err)) {
4006+ if (do_wh)
4007+ goto out_wh;
4008+ goto out;
4009+ }
4010+
4011+ err = 0;
4012+ if (to_free) {
4013+ /*
4014+ * now we confirmed the branch is deletable.
4015+ * let's free the remaining opened dirs on the branch.
4016+ */
4017+ di_write_unlock(root);
4018+ br_del_file(to_free, opened, br_id);
4019+ di_write_lock_child(root);
4020+ }
4021+
4022+ sysaufs_brs_del(sb, bindex); /* remove successors */
4023+ dbgaufs_xino_del(br); /* remove one */
4024+ au_br_do_del(sb, bindex, br);
4025+ sysaufs_brs_add(sb, bindex); /* append successors */
4026+ dbgaufs_brs_add(sb, bindex, /*topdown*/1); /* rename successors */
4027+
4028+ if (!bindex) {
4029+ au_cpup_attr_all(d_inode(root), /*force*/1);
4030+ sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
4031+ } else
4032+ au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
4033+ if (au_opt_test(mnt_flags, PLINK))
4034+ au_plink_half_refresh(sb, br_id);
4035+
4036+ goto out; /* success */
4037+
4038+out_wh:
4039+ /* revert */
4040+ rerr = au_br_init_wh(sb, br, br->br_perm);
4041+ if (rerr)
4042+ pr_warn("failed re-creating base whiteout, %s. (%d)\n",
4043+ del->pathname, rerr);
4044+out:
4045+ if (to_free)
4046+ au_farray_free(to_free, opened);
4047+ return err;
4048+}
4049+
4050+/* ---------------------------------------------------------------------- */
4051+
4052+static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
4053+{
4054+ int err;
4055+ aufs_bindex_t btop, bbot;
4056+ struct aufs_ibusy ibusy;
4057+ struct inode *inode, *h_inode;
4058+
4059+ err = -EPERM;
4060+ if (unlikely(!capable(CAP_SYS_ADMIN)))
4061+ goto out;
4062+
4063+ err = copy_from_user(&ibusy, arg, sizeof(ibusy));
4064+ if (!err)
4065+ /* VERIFY_WRITE */
4066+ err = !access_ok(&arg->h_ino, sizeof(arg->h_ino));
4067+ if (unlikely(err)) {
4068+ err = -EFAULT;
4069+ AuTraceErr(err);
4070+ goto out;
4071+ }
4072+
4073+ err = -EINVAL;
4074+ si_read_lock(sb, AuLock_FLUSH);
4075+ if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
4076+ goto out_unlock;
4077+
4078+ err = 0;
4079+ ibusy.h_ino = 0; /* invalid */
4080+ inode = ilookup(sb, ibusy.ino);
4081+ if (!inode
4082+ || inode->i_ino == AUFS_ROOT_INO
4083+ || au_is_bad_inode(inode))
4084+ goto out_unlock;
4085+
4086+ ii_read_lock_child(inode);
4087+ btop = au_ibtop(inode);
4088+ bbot = au_ibbot(inode);
4089+ if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
4090+ h_inode = au_h_iptr(inode, ibusy.bindex);
4091+ if (h_inode && au_test_ibusy(inode, btop, bbot))
4092+ ibusy.h_ino = h_inode->i_ino;
4093+ }
4094+ ii_read_unlock(inode);
4095+ iput(inode);
4096+
4097+out_unlock:
4098+ si_read_unlock(sb);
4099+ if (!err) {
4100+ err = __put_user(ibusy.h_ino, &arg->h_ino);
4101+ if (unlikely(err)) {
4102+ err = -EFAULT;
4103+ AuTraceErr(err);
4104+ }
4105+ }
4106+out:
4107+ return err;
4108+}
4109+
4110+long au_ibusy_ioctl(struct file *file, unsigned long arg)
4111+{
4112+ return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
4113+}
4114+
4115+#ifdef CONFIG_COMPAT
4116+long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
4117+{
4118+ return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
4119+}
4120+#endif
4121+
4122+/* ---------------------------------------------------------------------- */
4123+
4124+/*
4125+ * change a branch permission
4126+ */
4127+
4128+static void au_warn_ima(void)
4129+{
4130+#ifdef CONFIG_IMA
4131+ /* since it doesn't support mark_files_ro() */
4132+ AuWarn1("RW -> RO makes IMA to produce wrong message\n");
4133+#endif
4134+}
4135+
4136+static int do_need_sigen_inc(int a, int b)
4137+{
4138+ return au_br_whable(a) && !au_br_whable(b);
4139+}
4140+
4141+static int need_sigen_inc(int old, int new)
4142+{
4143+ return do_need_sigen_inc(old, new)
4144+ || do_need_sigen_inc(new, old);
4145+}
4146+
4147+static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
4148+{
4149+ int err, do_warn;
4150+ unsigned int mnt_flags;
4151+ unsigned long long ull, max;
4152+ aufs_bindex_t br_id;
4153+ unsigned char verbose, writer;
4154+ struct file *file, *hf, **array;
4155+ struct au_hfile *hfile;
4156+ struct inode *h_inode;
4157+
4158+ mnt_flags = au_mntflags(sb);
4159+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
4160+
4161+ array = au_farray_alloc(sb, &max);
4162+ err = PTR_ERR(array);
4163+ if (IS_ERR(array))
4164+ goto out;
4165+
4166+ do_warn = 0;
4167+ br_id = au_sbr_id(sb, bindex);
4168+ for (ull = 0; ull < max; ull++) {
4169+ file = array[ull];
4170+ if (unlikely(!file))
4171+ break;
4172+
4173+ /* AuDbg("%pD\n", file); */
4174+ fi_read_lock(file);
4175+ if (unlikely(au_test_mmapped(file))) {
4176+ err = -EBUSY;
4177+ AuVerbose(verbose, "mmapped %pD\n", file);
4178+ AuDbgFile(file);
4179+ FiMustNoWaiters(file);
4180+ fi_read_unlock(file);
4181+ goto out_array;
4182+ }
4183+
4184+ hfile = &au_fi(file)->fi_htop;
4185+ hf = hfile->hf_file;
4186+ if (!d_is_reg(file->f_path.dentry)
4187+ || !(file->f_mode & FMODE_WRITE)
4188+ || hfile->hf_br->br_id != br_id
4189+ || !(hf->f_mode & FMODE_WRITE))
4190+ array[ull] = NULL;
4191+ else {
4192+ do_warn = 1;
4193+ get_file(file);
4194+ }
4195+
4196+ FiMustNoWaiters(file);
4197+ fi_read_unlock(file);
4198+ fput(file);
4199+ }
4200+
4201+ err = 0;
4202+ if (do_warn)
4203+ au_warn_ima();
4204+
4205+ for (ull = 0; ull < max; ull++) {
4206+ file = array[ull];
4207+ if (!file)
4208+ continue;
4209+
4210+ /* todo: already flushed? */
4211+ /*
4212+ * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4213+ * approach which resets f_mode and calls mnt_drop_write() and
4214+ * file_release_write() for each file, because the branch
4215+ * attribute in aufs world is totally different from the native
4216+ * fs rw/ro mode.
4217+ */
4218+ /* fi_read_lock(file); */
4219+ hfile = &au_fi(file)->fi_htop;
4220+ hf = hfile->hf_file;
4221+ /* fi_read_unlock(file); */
4222+ spin_lock(&hf->f_lock);
4223+ writer = !!(hf->f_mode & FMODE_WRITER);
4224+ hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
4225+ spin_unlock(&hf->f_lock);
4226+ if (writer) {
4227+ h_inode = file_inode(hf);
4228+ if (hf->f_mode & FMODE_READ)
4229+ i_readcount_inc(h_inode);
4230+ put_write_access(h_inode);
4231+ __mnt_drop_write(hf->f_path.mnt);
4232+ }
4233+ }
4234+
4235+out_array:
4236+ au_farray_free(array, max);
4237+out:
4238+ AuTraceErr(err);
4239+ return err;
4240+}
4241+
4242+int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4243+ int *do_refresh)
4244+{
4245+ int err, rerr;
4246+ aufs_bindex_t bindex;
4247+ struct dentry *root;
4248+ struct au_branch *br;
4249+ struct au_br_fhsm *bf;
4250+
4251+ root = sb->s_root;
4252+ bindex = au_find_dbindex(root, mod->h_root);
4253+ if (bindex < 0) {
4254+ if (remount)
4255+ return 0; /* success */
4256+ err = -ENOENT;
4257+ pr_err("%s no such branch\n", mod->path);
4258+ goto out;
4259+ }
4260+ AuDbg("bindex b%d\n", bindex);
4261+
4262+ err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
4263+ if (unlikely(err))
4264+ goto out;
4265+
4266+ br = au_sbr(sb, bindex);
4267+ AuDebugOn(mod->h_root != au_br_dentry(br));
4268+ if (br->br_perm == mod->perm)
4269+ return 0; /* success */
4270+
4271+ /* pre-allocate for non-fhsm --> fhsm */
4272+ bf = NULL;
4273+ if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4274+ err = au_fhsm_br_alloc(br);
4275+ if (unlikely(err))
4276+ goto out;
4277+ bf = br->br_fhsm;
4278+ br->br_fhsm = NULL;
4279+ }
4280+
4281+ if (au_br_writable(br->br_perm)) {
4282+ /* remove whiteout base */
4283+ err = au_br_init_wh(sb, br, mod->perm);
4284+ if (unlikely(err))
4285+ goto out_bf;
4286+
4287+ if (!au_br_writable(mod->perm)) {
4288+ /* rw --> ro, file might be mmapped */
4289+ DiMustNoWaiters(root);
4290+ IiMustNoWaiters(d_inode(root));
4291+ di_write_unlock(root);
4292+ err = au_br_mod_files_ro(sb, bindex);
4293+ /* aufs_write_lock() calls ..._child() */
4294+ di_write_lock_child(root);
4295+
4296+ if (unlikely(err)) {
4297+ rerr = -ENOMEM;
4298+ br->br_wbr = kzalloc(sizeof(*br->br_wbr),
4299+ GFP_NOFS);
4300+ if (br->br_wbr)
4301+ rerr = au_wbr_init(br, sb, br->br_perm);
4302+ if (unlikely(rerr)) {
4303+ AuIOErr("nested error %d (%d)\n",
4304+ rerr, err);
4305+ br->br_perm = mod->perm;
4306+ }
4307+ }
4308+ }
4309+ } else if (au_br_writable(mod->perm)) {
4310+ /* ro --> rw */
4311+ err = -ENOMEM;
4312+ br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
4313+ if (br->br_wbr) {
4314+ err = au_wbr_init(br, sb, mod->perm);
4315+ if (unlikely(err)) {
4316+ au_kfree_rcu(br->br_wbr);
4317+ br->br_wbr = NULL;
4318+ }
4319+ }
4320+ }
4321+ if (unlikely(err))
4322+ goto out_bf;
4323+
4324+ if (au_br_fhsm(br->br_perm)) {
4325+ if (!au_br_fhsm(mod->perm)) {
4326+ /* fhsm --> non-fhsm */
4327+ au_br_fhsm_fin(br->br_fhsm);
4328+ au_kfree_rcu(br->br_fhsm);
4329+ br->br_fhsm = NULL;
4330+ }
4331+ } else if (au_br_fhsm(mod->perm))
4332+ /* non-fhsm --> fhsm */
4333+ br->br_fhsm = bf;
4334+
4335+ *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4336+ br->br_perm = mod->perm;
4337+ goto out; /* success */
4338+
4339+out_bf:
4340+ au_kfree_try_rcu(bf);
4341+out:
4342+ AuTraceErr(err);
4343+ return err;
4344+}
4345+
4346+/* ---------------------------------------------------------------------- */
4347+
4348+int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4349+{
4350+ int err;
4351+ struct kstatfs kstfs;
4352+
4353+ err = vfs_statfs(&br->br_path, &kstfs);
4354+ if (!err) {
4355+ stfs->f_blocks = kstfs.f_blocks;
4356+ stfs->f_bavail = kstfs.f_bavail;
4357+ stfs->f_files = kstfs.f_files;
4358+ stfs->f_ffree = kstfs.f_ffree;
4359+ }
4360+
4361+ return err;
4362+}
4363diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
4364--- /usr/share/empty/fs/aufs/branch.h 1970-01-01 01:00:00.000000000 +0100
4365+++ linux/fs/aufs/branch.h 2021-06-30 21:35:11.393873211 +0200
4366@@ -0,0 +1,375 @@
4367+/* SPDX-License-Identifier: GPL-2.0 */
4368+/*
4369+ * Copyright (C) 2005-2020 Junjiro R. Okajima
4370+ *
4371+ * This program, aufs is free software; you can redistribute it and/or modify
4372+ * it under the terms of the GNU General Public License as published by
4373+ * the Free Software Foundation; either version 2 of the License, or
4374+ * (at your option) any later version.
4375+ *
4376+ * This program is distributed in the hope that it will be useful,
4377+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
4378+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4379+ * GNU General Public License for more details.
4380+ *
4381+ * You should have received a copy of the GNU General Public License
4382+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4383+ */
4384+
4385+/*
4386+ * branch filesystems and xino for them
4387+ */
4388+
4389+#ifndef __AUFS_BRANCH_H__
4390+#define __AUFS_BRANCH_H__
4391+
4392+#ifdef __KERNEL__
4393+
4394+#include <linux/mount.h>
4395+#include "dirren.h"
4396+#include "dynop.h"
4397+#include "lcnt.h"
4398+#include "rwsem.h"
4399+#include "super.h"
4400+
4401+/* ---------------------------------------------------------------------- */
4402+
4403+/* a xino file */
4404+struct au_xino {
4405+ struct file **xi_file;
4406+ unsigned int xi_nfile;
4407+
4408+ struct {
4409+ spinlock_t spin;
4410+ ino_t *array;
4411+ int total;
4412+ /* reserved for future use */
4413+ /* unsigned long *bitmap; */
4414+ wait_queue_head_t wqh;
4415+ } xi_nondir;
4416+
4417+ struct mutex xi_mtx; /* protects xi_file array */
4418+ struct hlist_bl_head xi_writing;
4419+
4420+ atomic_t xi_truncating;
4421+
4422+ struct kref xi_kref;
4423+};
4424+
4425+/* File-based Hierarchical Storage Management */
4426+struct au_br_fhsm {
4427+#ifdef CONFIG_AUFS_FHSM
4428+ struct mutex bf_lock;
4429+ unsigned long bf_jiffy;
4430+ struct aufs_stfs bf_stfs;
4431+ int bf_readable;
4432+#endif
4433+};
4434+
4435+/* members for writable branch only */
4436+enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4437+struct au_wbr {
4438+ struct au_rwsem wbr_wh_rwsem;
4439+ struct dentry *wbr_wh[AuBrWh_Last];
4440+ atomic_t wbr_wh_running;
4441+#define wbr_whbase wbr_wh[AuBrWh_BASE] /* whiteout base */
4442+#define wbr_plink wbr_wh[AuBrWh_PLINK] /* pseudo-link dir */
4443+#define wbr_orph wbr_wh[AuBrWh_ORPH] /* dir for orphans */
4444+
4445+ /* mfs mode */
4446+ unsigned long long wbr_bytes;
4447+};
4448+
4449+/* ext2 has 3 types of operations at least, ext3 has 4 */
4450+#define AuBrDynOp (AuDyLast * 4)
4451+
4452+#ifdef CONFIG_AUFS_HFSNOTIFY
4453+/* support for asynchronous destruction */
4454+struct au_br_hfsnotify {
4455+ struct fsnotify_group *hfsn_group;
4456+};
4457+#endif
4458+
4459+/* sysfs entries */
4460+struct au_brsysfs {
4461+ char name[16];
4462+ struct attribute attr;
4463+};
4464+
4465+enum {
4466+ AuBrSysfs_BR,
4467+ AuBrSysfs_BRID,
4468+ AuBrSysfs_Last
4469+};
4470+
4471+/* protected by superblock rwsem */
4472+struct au_branch {
4473+ struct au_xino *br_xino;
4474+
4475+ aufs_bindex_t br_id;
4476+
4477+ int br_perm;
4478+ struct path br_path;
4479+ spinlock_t br_dykey_lock;
4480+ struct au_dykey *br_dykey[AuBrDynOp];
4481+ au_lcnt_t br_nfiles; /* opened files */
4482+ au_lcnt_t br_count; /* in-use for other */
4483+
4484+ struct au_wbr *br_wbr;
4485+ struct au_br_fhsm *br_fhsm;
4486+
4487+#ifdef CONFIG_AUFS_HFSNOTIFY
4488+ struct au_br_hfsnotify *br_hfsn;
4489+#endif
4490+
4491+#ifdef CONFIG_SYSFS
4492+ /* entries under sysfs per mount-point */
4493+ struct au_brsysfs br_sysfs[AuBrSysfs_Last];
4494+#endif
4495+
4496+#ifdef CONFIG_DEBUG_FS
4497+ struct dentry *br_dbgaufs; /* xino */
4498+#endif
4499+
4500+ struct au_dr_br br_dirren;
4501+};
4502+
4503+/* ---------------------------------------------------------------------- */
4504+
4505+static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4506+{
4507+ return br->br_path.mnt;
4508+}
4509+
4510+static inline struct dentry *au_br_dentry(struct au_branch *br)
4511+{
4512+ return br->br_path.dentry;
4513+}
4514+
4515+static inline struct user_namespace *au_br_userns(struct au_branch *br)
4516+{
4517+ return mnt_user_ns(br->br_path.mnt);
4518+}
4519+
4520+static inline struct super_block *au_br_sb(struct au_branch *br)
4521+{
4522+ return au_br_mnt(br)->mnt_sb;
4523+}
4524+
4525+static inline int au_br_rdonly(struct au_branch *br)
4526+{
4527+ return (sb_rdonly(au_br_sb(br))
4528+ || !au_br_writable(br->br_perm))
4529+ ? -EROFS : 0;
4530+}
4531+
4532+static inline int au_br_hnotifyable(int brperm __maybe_unused)
4533+{
4534+#ifdef CONFIG_AUFS_HNOTIFY
4535+ return !(brperm & AuBrPerm_RR);
4536+#else
4537+ return 0;
4538+#endif
4539+}
4540+
4541+static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4542+{
4543+ int err, exec_flag;
4544+
4545+ err = 0;
4546+ exec_flag = oflag & __FMODE_EXEC;
4547+ if (unlikely(exec_flag && path_noexec(&br->br_path)))
4548+ err = -EACCES;
4549+
4550+ return err;
4551+}
4552+
4553+static inline void au_xino_get(struct au_branch *br)
4554+{
4555+ struct au_xino *xi;
4556+
4557+ xi = br->br_xino;
4558+ if (xi)
4559+ kref_get(&xi->xi_kref);
4560+}
4561+
4562+static inline int au_xino_count(struct au_branch *br)
4563+{
4564+ int v;
4565+ struct au_xino *xi;
4566+
4567+ v = 0;
4568+ xi = br->br_xino;
4569+ if (xi)
4570+ v = kref_read(&xi->xi_kref);
4571+
4572+ return v;
4573+}
4574+
4575+/* ---------------------------------------------------------------------- */
4576+
4577+/* branch.c */
4578+struct au_sbinfo;
4579+void au_br_free(struct au_sbinfo *sinfo);
4580+int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4581+struct au_opt_add;
4582+int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4583+struct au_opt_del;
4584+int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
4585+long au_ibusy_ioctl(struct file *file, unsigned long arg);
4586+#ifdef CONFIG_COMPAT
4587+long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4588+#endif
4589+struct au_opt_mod;
4590+int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4591+ int *do_refresh);
4592+struct aufs_stfs;
4593+int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
4594+
4595+/* xino.c */
4596+static const loff_t au_loff_max = LLONG_MAX;
4597+
4598+aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry);
4599+struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
4600+ int wbrtop);
4601+struct file *au_xino_create2(struct super_block *sb, struct path *base,
4602+ struct file *copy_src);
4603+struct au_xi_new {
4604+ struct au_xino *xi; /* switch between xino and xigen */
4605+ int idx;
4606+ struct path *base;
4607+ struct file *copy_src;
4608+};
4609+struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew);
4610+
4611+int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4612+ ino_t *ino);
4613+int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4614+ ino_t ino);
4615+ssize_t xino_fread(struct file *file, void *buf, size_t size, loff_t *pos);
4616+ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos);
4617+
4618+int au_xib_trunc(struct super_block *sb);
4619+int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin);
4620+
4621+struct au_xino *au_xino_alloc(unsigned int nfile);
4622+int au_xino_put(struct au_branch *br);
4623+struct file *au_xino_file1(struct au_xino *xi);
4624+
4625+struct au_opt_xino;
4626+void au_xino_clr(struct super_block *sb);
4627+int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount);
4628+struct file *au_xino_def(struct super_block *sb);
4629+int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4630+ struct path *base);
4631+
4632+ino_t au_xino_new_ino(struct super_block *sb);
4633+void au_xino_delete_inode(struct inode *inode, const int unlinked);
4634+
4635+void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
4636+ ino_t h_ino, int idx);
4637+int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4638+ int *idx);
4639+
4640+int au_xino_path(struct seq_file *seq, struct file *file);
4641+
4642+/* ---------------------------------------------------------------------- */
4643+
4644+/* @idx is signed to accept -1 meaning the first file */
4645+static inline struct file *au_xino_file(struct au_xino *xi, int idx)
4646+{
4647+ struct file *file;
4648+
4649+ file = NULL;
4650+ if (!xi)
4651+ goto out;
4652+
4653+ if (idx >= 0) {
4654+ if (idx < xi->xi_nfile)
4655+ file = xi->xi_file[idx];
4656+ } else
4657+ file = au_xino_file1(xi);
4658+
4659+out:
4660+ return file;
4661+}
4662+
4663+/* ---------------------------------------------------------------------- */
4664+
4665+/* Superblock to branch */
4666+static inline
4667+aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4668+{
4669+ return au_sbr(sb, bindex)->br_id;
4670+}
4671+
4672+static inline
4673+struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4674+{
4675+ return au_br_mnt(au_sbr(sb, bindex));
4676+}
4677+
4678+static inline
4679+struct user_namespace *au_sbr_userns(struct super_block *sb, aufs_bindex_t bindex)
4680+{
4681+ return au_br_userns(au_sbr(sb, bindex));
4682+}
4683+
4684+static inline
4685+struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4686+{
4687+ return au_br_sb(au_sbr(sb, bindex));
4688+}
4689+
4690+static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4691+{
4692+ return au_sbr(sb, bindex)->br_perm;
4693+}
4694+
4695+static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4696+{
4697+ return au_br_whable(au_sbr_perm(sb, bindex));
4698+}
4699+
4700+/* ---------------------------------------------------------------------- */
4701+
4702+#define wbr_wh_read_lock(wbr) au_rw_read_lock(&(wbr)->wbr_wh_rwsem)
4703+#define wbr_wh_write_lock(wbr) au_rw_write_lock(&(wbr)->wbr_wh_rwsem)
4704+#define wbr_wh_read_trylock(wbr) au_rw_read_trylock(&(wbr)->wbr_wh_rwsem)
4705+#define wbr_wh_write_trylock(wbr) au_rw_write_trylock(&(wbr)->wbr_wh_rwsem)
4706+/*
4707+#define wbr_wh_read_trylock_nested(wbr) \
4708+ au_rw_read_trylock_nested(&(wbr)->wbr_wh_rwsem)
4709+#define wbr_wh_write_trylock_nested(wbr) \
4710+ au_rw_write_trylock_nested(&(wbr)->wbr_wh_rwsem)
4711+*/
4712+
4713+#define wbr_wh_read_unlock(wbr) au_rw_read_unlock(&(wbr)->wbr_wh_rwsem)
4714+#define wbr_wh_write_unlock(wbr) au_rw_write_unlock(&(wbr)->wbr_wh_rwsem)
4715+#define wbr_wh_downgrade_lock(wbr) au_rw_dgrade_lock(&(wbr)->wbr_wh_rwsem)
4716+
4717+#define WbrWhMustNoWaiters(wbr) AuRwMustNoWaiters(&(wbr)->wbr_wh_rwsem)
4718+#define WbrWhMustAnyLock(wbr) AuRwMustAnyLock(&(wbr)->wbr_wh_rwsem)
4719+#define WbrWhMustWriteLock(wbr) AuRwMustWriteLock(&(wbr)->wbr_wh_rwsem)
4720+
4721+/* ---------------------------------------------------------------------- */
4722+
4723+#ifdef CONFIG_AUFS_FHSM
4724+static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4725+{
4726+ mutex_init(&brfhsm->bf_lock);
4727+ brfhsm->bf_jiffy = 0;
4728+ brfhsm->bf_readable = 0;
4729+}
4730+
4731+static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4732+{
4733+ mutex_destroy(&brfhsm->bf_lock);
4734+}
4735+#else
4736+AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4737+AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4738+#endif
4739+
4740+#endif /* __KERNEL__ */
4741+#endif /* __AUFS_BRANCH_H__ */
4742diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
4743--- /usr/share/empty/fs/aufs/conf.mk 1970-01-01 01:00:00.000000000 +0100
4744+++ linux/fs/aufs/conf.mk 2021-02-24 13:33:42.741013619 +0100
4745@@ -0,0 +1,40 @@
4746+# SPDX-License-Identifier: GPL-2.0
4747+
4748+AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4749+
4750+define AuConf
4751+ifdef ${1}
4752+AuConfStr += ${1}=${${1}}
4753+endif
4754+endef
4755+
4756+AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
4757+ SBILIST \
4758+ HNOTIFY HFSNOTIFY \
4759+ EXPORT INO_T_64 \
4760+ XATTR \
4761+ FHSM \
4762+ RDU \
4763+ DIRREN \
4764+ SHWH \
4765+ BR_RAMFS \
4766+ BR_FUSE POLL \
4767+ BR_HFSPLUS \
4768+ BDEV_LOOP \
4769+ DEBUG MAGIC_SYSRQ
4770+$(foreach i, ${AuConfAll}, \
4771+ $(eval $(call AuConf,CONFIG_AUFS_${i})))
4772+
4773+AuConfName = ${obj}/conf.str
4774+${AuConfName}.tmp: FORCE
4775+ @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4776+${AuConfName}: ${AuConfName}.tmp
4777+ @diff -q $< $@ > /dev/null 2>&1 || { \
4778+ echo ' GEN ' $@; \
4779+ cp -p $< $@; \
4780+ }
4781+FORCE:
4782+clean-files += ${AuConfName} ${AuConfName}.tmp
4783+${obj}/sysfs.o: ${AuConfName}
4784+
4785+-include ${srctree}/${src}/conf_priv.mk
4786diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
4787--- /usr/share/empty/fs/aufs/cpup.c 1970-01-01 01:00:00.000000000 +0100
4788+++ linux/fs/aufs/cpup.c 2021-06-30 21:35:11.393873211 +0200
4789@@ -0,0 +1,1457 @@
4790+// SPDX-License-Identifier: GPL-2.0
4791+/*
4792+ * Copyright (C) 2005-2020 Junjiro R. Okajima
4793+ *
4794+ * This program, aufs is free software; you can redistribute it and/or modify
4795+ * it under the terms of the GNU General Public License as published by
4796+ * the Free Software Foundation; either version 2 of the License, or
4797+ * (at your option) any later version.
4798+ *
4799+ * This program is distributed in the hope that it will be useful,
4800+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
4801+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4802+ * GNU General Public License for more details.
4803+ *
4804+ * You should have received a copy of the GNU General Public License
4805+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4806+ */
4807+
4808+/*
4809+ * copy-up functions, see wbr_policy.c for copy-down
4810+ */
4811+
4812+#include <linux/fs_stack.h>
4813+#include <linux/mm.h>
4814+#include <linux/task_work.h>
4815+#include "aufs.h"
4816+
4817+void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
4818+{
4819+ const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
4820+ | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
4821+
4822+ BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
4823+
4824+ dst->i_flags |= iflags & ~mask;
4825+ if (au_test_fs_notime(dst->i_sb))
4826+ dst->i_flags |= S_NOATIME | S_NOCMTIME;
4827+}
4828+
4829+void au_cpup_attr_timesizes(struct inode *inode)
4830+{
4831+ struct inode *h_inode;
4832+
4833+ h_inode = au_h_iptr(inode, au_ibtop(inode));
4834+ fsstack_copy_attr_times(inode, h_inode);
4835+ fsstack_copy_inode_size(inode, h_inode);
4836+}
4837+
4838+void au_cpup_attr_nlink(struct inode *inode, int force)
4839+{
4840+ struct inode *h_inode;
4841+ struct super_block *sb;
4842+ aufs_bindex_t bindex, bbot;
4843+
4844+ sb = inode->i_sb;
4845+ bindex = au_ibtop(inode);
4846+ h_inode = au_h_iptr(inode, bindex);
4847+ if (!force
4848+ && !S_ISDIR(h_inode->i_mode)
4849+ && au_opt_test(au_mntflags(sb), PLINK)
4850+ && au_plink_test(inode))
4851+ return;
4852+
4853+ /*
4854+ * 0 can happen in revalidating.
4855+ * h_inode->i_mutex may not be held here, but it is harmless since once
4856+ * i_nlink reaches 0, it will never become positive except O_TMPFILE
4857+ * case.
4858+ * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
4859+ * the incorrect link count.
4860+ */
4861+ set_nlink(inode, h_inode->i_nlink);
4862+
4863+ /*
4864+ * fewer nlink makes find(1) noisy, but larger nlink doesn't.
4865+ * it may includes whplink directory.
4866+ */
4867+ if (S_ISDIR(h_inode->i_mode)) {
4868+ bbot = au_ibbot(inode);
4869+ for (bindex++; bindex <= bbot; bindex++) {
4870+ h_inode = au_h_iptr(inode, bindex);
4871+ if (h_inode)
4872+ au_add_nlink(inode, h_inode);
4873+ }
4874+ }
4875+}
4876+
4877+void au_cpup_attr_changeable(struct inode *inode)
4878+{
4879+ struct inode *h_inode;
4880+
4881+ h_inode = au_h_iptr(inode, au_ibtop(inode));
4882+ inode->i_mode = h_inode->i_mode;
4883+ inode->i_uid = h_inode->i_uid;
4884+ inode->i_gid = h_inode->i_gid;
4885+ au_cpup_attr_timesizes(inode);
4886+ au_cpup_attr_flags(inode, h_inode->i_flags);
4887+}
4888+
4889+void au_cpup_igen(struct inode *inode, struct inode *h_inode)
4890+{
4891+ struct au_iinfo *iinfo = au_ii(inode);
4892+
4893+ IiMustWriteLock(inode);
4894+
4895+ iinfo->ii_higen = h_inode->i_generation;
4896+ iinfo->ii_hsb1 = h_inode->i_sb;
4897+}
4898+
4899+void au_cpup_attr_all(struct inode *inode, int force)
4900+{
4901+ struct inode *h_inode;
4902+
4903+ h_inode = au_h_iptr(inode, au_ibtop(inode));
4904+ au_cpup_attr_changeable(inode);
4905+ if (inode->i_nlink > 0)
4906+ au_cpup_attr_nlink(inode, force);
4907+ inode->i_rdev = h_inode->i_rdev;
4908+ inode->i_blkbits = h_inode->i_blkbits;
4909+ au_cpup_igen(inode, h_inode);
4910+}
4911+
4912+/* ---------------------------------------------------------------------- */
4913+
4914+/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
4915+
4916+/* keep the timestamps of the parent dir when cpup */
4917+void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
4918+ struct path *h_path)
4919+{
4920+ struct inode *h_inode;
4921+
4922+ dt->dt_dentry = dentry;
4923+ dt->dt_h_path = *h_path;
4924+ h_inode = d_inode(h_path->dentry);
4925+ dt->dt_atime = h_inode->i_atime;
4926+ dt->dt_mtime = h_inode->i_mtime;
4927+ /* smp_mb(); */
4928+}
4929+
4930+void au_dtime_revert(struct au_dtime *dt)
4931+{
4932+ struct iattr attr;
4933+ int err;
4934+
4935+ attr.ia_atime = dt->dt_atime;
4936+ attr.ia_mtime = dt->dt_mtime;
4937+ attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
4938+ | ATTR_ATIME | ATTR_ATIME_SET;
4939+
4940+ /* no delegation since this is a directory */
4941+ err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
4942+ if (unlikely(err))
4943+ pr_warn("restoring timestamps failed(%d). ignored\n", err);
4944+}
4945+
4946+/* ---------------------------------------------------------------------- */
4947+
4948+/* internal use only */
4949+struct au_cpup_reg_attr {
4950+ int valid;
4951+ struct kstat st;
4952+ unsigned int iflags; /* inode->i_flags */
4953+};
4954+
4955+static noinline_for_stack
4956+int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct path *h_src,
4957+ struct au_cpup_reg_attr *h_src_attr)
4958+{
4959+ int err, sbits, icex;
4960+ unsigned int mnt_flags;
4961+ unsigned char verbose;
4962+ struct iattr ia;
4963+ struct path h_path;
4964+ struct inode *h_isrc, *h_idst;
4965+ struct kstat *h_st;
4966+ struct au_branch *br;
4967+
4968+ br = au_sbr(dst->d_sb, bindex);
4969+ h_path.mnt = au_br_mnt(br);
4970+ h_path.dentry = au_h_dptr(dst, bindex);
4971+ h_idst = d_inode(h_path.dentry);
4972+ h_isrc = d_inode(h_src->dentry);
4973+ ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
4974+ | ATTR_ATIME | ATTR_MTIME
4975+ | ATTR_ATIME_SET | ATTR_MTIME_SET;
4976+ if (h_src_attr && h_src_attr->valid) {
4977+ h_st = &h_src_attr->st;
4978+ ia.ia_uid = h_st->uid;
4979+ ia.ia_gid = h_st->gid;
4980+ ia.ia_atime = h_st->atime;
4981+ ia.ia_mtime = h_st->mtime;
4982+ if (h_idst->i_mode != h_st->mode
4983+ && !S_ISLNK(h_idst->i_mode)) {
4984+ ia.ia_valid |= ATTR_MODE;
4985+ ia.ia_mode = h_st->mode;
4986+ }
4987+ sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
4988+ au_cpup_attr_flags(h_idst, h_src_attr->iflags);
4989+ } else {
4990+ ia.ia_uid = h_isrc->i_uid;
4991+ ia.ia_gid = h_isrc->i_gid;
4992+ ia.ia_atime = h_isrc->i_atime;
4993+ ia.ia_mtime = h_isrc->i_mtime;
4994+ if (h_idst->i_mode != h_isrc->i_mode
4995+ && !S_ISLNK(h_idst->i_mode)) {
4996+ ia.ia_valid |= ATTR_MODE;
4997+ ia.ia_mode = h_isrc->i_mode;
4998+ }
4999+ sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
5000+ au_cpup_attr_flags(h_idst, h_isrc->i_flags);
5001+ }
5002+ /* no delegation since it is just created */
5003+ err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
5004+
5005+ /* is this nfs only? */
5006+ if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
5007+ ia.ia_valid = ATTR_FORCE | ATTR_MODE;
5008+ ia.ia_mode = h_isrc->i_mode;
5009+ err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
5010+ }
5011+
5012+ icex = br->br_perm & AuBrAttr_ICEX;
5013+ if (!err) {
5014+ mnt_flags = au_mntflags(dst->d_sb);
5015+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
5016+ err = au_cpup_xattr(&h_path, h_src, icex, verbose);
5017+ }
5018+
5019+ return err;
5020+}
5021+
5022+/* ---------------------------------------------------------------------- */
5023+
5024+static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
5025+ char *buf, unsigned long blksize)
5026+{
5027+ int err;
5028+ size_t sz, rbytes, wbytes;
5029+ unsigned char all_zero;
5030+ char *p, *zp;
5031+ struct inode *h_inode;
5032+ /* reduce stack usage */
5033+ struct iattr *ia;
5034+
5035+ zp = page_address(ZERO_PAGE(0));
5036+ if (unlikely(!zp))
5037+ return -ENOMEM; /* possible? */
5038+
5039+ err = 0;
5040+ all_zero = 0;
5041+ while (len) {
5042+ AuDbg("len %lld\n", len);
5043+ sz = blksize;
5044+ if (len < blksize)
5045+ sz = len;
5046+
5047+ rbytes = 0;
5048+ /* todo: signal_pending? */
5049+ while (!rbytes || err == -EAGAIN || err == -EINTR) {
5050+ rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
5051+ err = rbytes;
5052+ }
5053+ if (unlikely(err < 0))
5054+ break;
5055+
5056+ all_zero = 0;
5057+ if (len >= rbytes && rbytes == blksize)
5058+ all_zero = !memcmp(buf, zp, rbytes);
5059+ if (!all_zero) {
5060+ wbytes = rbytes;
5061+ p = buf;
5062+ while (wbytes) {
5063+ size_t b;
5064+
5065+ b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
5066+ err = b;
5067+ /* todo: signal_pending? */
5068+ if (unlikely(err == -EAGAIN || err == -EINTR))
5069+ continue;
5070+ if (unlikely(err < 0))
5071+ break;
5072+ wbytes -= b;
5073+ p += b;
5074+ }
5075+ if (unlikely(err < 0))
5076+ break;
5077+ } else {
5078+ loff_t res;
5079+
5080+ AuLabel(hole);
5081+ res = vfsub_llseek(dst, rbytes, SEEK_CUR);
5082+ err = res;
5083+ if (unlikely(res < 0))
5084+ break;
5085+ }
5086+ len -= rbytes;
5087+ err = 0;
5088+ }
5089+
5090+ /* the last block may be a hole */
5091+ if (!err && all_zero) {
5092+ AuLabel(last hole);
5093+
5094+ err = 1;
5095+ if (au_test_nfs(dst->f_path.dentry->d_sb)) {
5096+ /* nfs requires this step to make last hole */
5097+ /* is this only nfs? */
5098+ do {
5099+ /* todo: signal_pending? */
5100+ err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
5101+ } while (err == -EAGAIN || err == -EINTR);
5102+ if (err == 1)
5103+ dst->f_pos--;
5104+ }
5105+
5106+ if (err == 1) {
5107+ ia = (void *)buf;
5108+ ia->ia_size = dst->f_pos;
5109+ ia->ia_valid = ATTR_SIZE | ATTR_FILE;
5110+ ia->ia_file = dst;
5111+ h_inode = file_inode(dst);
5112+ inode_lock_nested(h_inode, AuLsc_I_CHILD2);
5113+ /* no delegation since it is just created */
5114+ err = vfsub_notify_change(&dst->f_path, ia,
5115+ /*delegated*/NULL);
5116+ inode_unlock(h_inode);
5117+ }
5118+ }
5119+
5120+ return err;
5121+}
5122+
5123+int au_copy_file(struct file *dst, struct file *src, loff_t len)
5124+{
5125+ int err;
5126+ unsigned long blksize;
5127+ unsigned char do_kfree;
5128+ char *buf;
5129+ struct super_block *h_sb;
5130+
5131+ err = -ENOMEM;
5132+ h_sb = file_inode(dst)->i_sb;
5133+ blksize = h_sb->s_blocksize;
5134+ if (!blksize || PAGE_SIZE < blksize)
5135+ blksize = PAGE_SIZE;
5136+ AuDbg("blksize %lu\n", blksize);
5137+ do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
5138+ if (do_kfree)
5139+ buf = kmalloc(blksize, GFP_NOFS);
5140+ else
5141+ buf = (void *)__get_free_page(GFP_NOFS);
5142+ if (unlikely(!buf))
5143+ goto out;
5144+
5145+ if (len > (1 << 22))
5146+ AuDbg("copying a large file %lld\n", (long long)len);
5147+
5148+ src->f_pos = 0;
5149+ dst->f_pos = 0;
5150+ err = au_do_copy_file(dst, src, len, buf, blksize);
5151+ if (do_kfree) {
5152+ AuDebugOn(!au_kfree_do_sz_test(blksize));
5153+ au_kfree_do_rcu(buf);
5154+ } else
5155+ free_page((unsigned long)buf);
5156+
5157+out:
5158+ return err;
5159+}
5160+
5161+static int au_do_copy(struct file *dst, struct file *src, loff_t len)
5162+{
5163+ int err;
5164+ struct super_block *h_src_sb;
5165+ struct inode *h_src_inode;
5166+
5167+ h_src_inode = file_inode(src);
5168+ h_src_sb = h_src_inode->i_sb;
5169+
5170+ /* XFS acquires inode_lock */
5171+ if (!au_test_xfs(h_src_sb))
5172+ err = au_copy_file(dst, src, len);
5173+ else {
5174+ inode_unlock_shared(h_src_inode);
5175+ err = au_copy_file(dst, src, len);
5176+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5177+ }
5178+
5179+ return err;
5180+}
5181+
5182+static int au_clone_or_copy(struct file *dst, struct file *src, loff_t len)
5183+{
5184+ int err;
5185+ loff_t lo;
5186+ struct super_block *h_src_sb;
5187+ struct inode *h_src_inode;
5188+
5189+ h_src_inode = file_inode(src);
5190+ h_src_sb = h_src_inode->i_sb;
5191+ if (h_src_sb != file_inode(dst)->i_sb
5192+ || !dst->f_op->remap_file_range) {
5193+ err = au_do_copy(dst, src, len);
5194+ goto out;
5195+ }
5196+
5197+ if (!au_test_nfs(h_src_sb)) {
5198+ inode_unlock_shared(h_src_inode);
5199+ lo = vfsub_clone_file_range(src, dst, len);
5200+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5201+ } else
5202+ lo = vfsub_clone_file_range(src, dst, len);
5203+ if (lo == len) {
5204+ err = 0;
5205+ goto out; /* success */
5206+ } else if (lo >= 0)
5207+ /* todo: possible? */
5208+ /* paritially succeeded */
5209+ AuDbg("lo %lld, len %lld. Retrying.\n", lo, len);
5210+ else if (lo != -EOPNOTSUPP) {
5211+ /* older XFS has a condition in cloning */
5212+ err = lo;
5213+ goto out;
5214+ }
5215+
5216+ /* the backend fs on NFS may not support cloning */
5217+ err = au_do_copy(dst, src, len);
5218+
5219+out:
5220+ AuTraceErr(err);
5221+ return err;
5222+}
5223+
5224+/*
5225+ * to support a sparse file which is opened with O_APPEND,
5226+ * we need to close the file.
5227+ */
5228+static int au_cp_regular(struct au_cp_generic *cpg)
5229+{
5230+ int err, i;
5231+ enum { SRC, DST };
5232+ struct {
5233+ aufs_bindex_t bindex;
5234+ unsigned int flags;
5235+ struct dentry *dentry;
5236+ int force_wr;
5237+ struct file *file;
5238+ } *f, file[] = {
5239+ {
5240+ .bindex = cpg->bsrc,
5241+ .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
5242+ },
5243+ {
5244+ .bindex = cpg->bdst,
5245+ .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
5246+ .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
5247+ }
5248+ };
5249+ struct au_branch *br;
5250+ struct super_block *sb, *h_src_sb;
5251+ struct inode *h_src_inode;
5252+ struct task_struct *tsk = current;
5253+
5254+ /* bsrc branch can be ro/rw. */
5255+ sb = cpg->dentry->d_sb;
5256+ f = file;
5257+ for (i = 0; i < 2; i++, f++) {
5258+ f->dentry = au_h_dptr(cpg->dentry, f->bindex);
5259+ f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
5260+ /*file*/NULL, f->force_wr);
5261+ if (IS_ERR(f->file)) {
5262+ err = PTR_ERR(f->file);
5263+ if (i == SRC)
5264+ goto out;
5265+ else
5266+ goto out_src;
5267+ }
5268+ }
5269+
5270+ /* try stopping to update while we copyup */
5271+ h_src_inode = d_inode(file[SRC].dentry);
5272+ h_src_sb = h_src_inode->i_sb;
5273+ if (!au_test_nfs(h_src_sb))
5274+ IMustLock(h_src_inode);
5275+ err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
5276+
5277+ /* i wonder if we had O_NO_DELAY_FPUT flag */
5278+ if (tsk->flags & PF_KTHREAD)
5279+ __fput_sync(file[DST].file);
5280+ else {
5281+ /* it happened actually */
5282+ fput(file[DST].file);
5283+ /*
5284+ * too bad.
5285+ * we have to call both since we don't know which place the file
5286+ * was added to.
5287+ */
5288+ task_work_run();
5289+ flush_delayed_fput();
5290+ }
5291+ br = au_sbr(sb, file[DST].bindex);
5292+ au_lcnt_dec(&br->br_nfiles);
5293+
5294+out_src:
5295+ fput(file[SRC].file);
5296+ br = au_sbr(sb, file[SRC].bindex);
5297+ au_lcnt_dec(&br->br_nfiles);
5298+out:
5299+ return err;
5300+}
5301+
5302+static int au_do_cpup_regular(struct au_cp_generic *cpg,
5303+ struct au_cpup_reg_attr *h_src_attr)
5304+{
5305+ int err, rerr;
5306+ loff_t l;
5307+ struct path h_path;
5308+ struct inode *h_src_inode, *h_dst_inode;
5309+
5310+ err = 0;
5311+ h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
5312+ l = i_size_read(h_src_inode);
5313+ if (cpg->len == -1 || l < cpg->len)
5314+ cpg->len = l;
5315+ if (cpg->len) {
5316+ /* try stopping to update while we are referencing */
5317+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5318+ au_pin_hdir_unlock(cpg->pin);
5319+
5320+ h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5321+ h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
5322+ h_src_attr->iflags = h_src_inode->i_flags;
5323+ if (!au_test_nfs(h_src_inode->i_sb))
5324+ err = vfsub_getattr(&h_path, &h_src_attr->st);
5325+ else {
5326+ inode_unlock_shared(h_src_inode);
5327+ err = vfsub_getattr(&h_path, &h_src_attr->st);
5328+ inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5329+ }
5330+ if (unlikely(err)) {
5331+ inode_unlock_shared(h_src_inode);
5332+ goto out;
5333+ }
5334+ h_src_attr->valid = 1;
5335+ if (!au_test_nfs(h_src_inode->i_sb)) {
5336+ err = au_cp_regular(cpg);
5337+ inode_unlock_shared(h_src_inode);
5338+ } else {
5339+ inode_unlock_shared(h_src_inode);
5340+ err = au_cp_regular(cpg);
5341+ }
5342+ rerr = au_pin_hdir_relock(cpg->pin);
5343+ if (!err && rerr)
5344+ err = rerr;
5345+ }
5346+ if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5347+ h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5348+ h_dst_inode = d_inode(h_path.dentry);
5349+ spin_lock(&h_dst_inode->i_lock);
5350+ h_dst_inode->i_state |= I_LINKABLE;
5351+ spin_unlock(&h_dst_inode->i_lock);
5352+ }
5353+
5354+out:
5355+ return err;
5356+}
5357+
5358+static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5359+ struct inode *h_dir)
5360+{
5361+ int err;
5362+ DEFINE_DELAYED_CALL(done);
5363+ const char *sym;
5364+
5365+ sym = vfs_get_link(h_src, &done);
5366+ err = PTR_ERR(sym);
5367+ if (IS_ERR(sym))
5368+ goto out;
5369+
5370+ err = vfsub_symlink(h_dir, h_path, sym);
5371+
5372+out:
5373+ do_delayed_call(&done);
5374+ return err;
5375+}
5376+
5377+/*
5378+ * regardless 'acl' option, reset all ACL.
5379+ * All ACL will be copied up later from the original entry on the lower branch.
5380+ */
5381+static int au_reset_acl(struct inode *h_dir, struct path *h_path, umode_t mode)
5382+{
5383+ int err;
5384+ struct dentry *h_dentry;
5385+ struct inode *h_inode;
5386+ struct user_namespace *h_userns;
5387+
5388+ h_userns = mnt_user_ns(h_path->mnt);
5389+ h_dentry = h_path->dentry;
5390+ h_inode = d_inode(h_dentry);
5391+ /* forget_all_cached_acls(h_inode)); */
5392+ err = vfsub_removexattr(h_userns, h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
5393+ AuTraceErr(err);
5394+ if (err == -EOPNOTSUPP)
5395+ err = 0;
5396+ if (!err)
5397+ err = vfsub_acl_chmod(h_userns, h_inode, mode);
5398+
5399+ AuTraceErr(err);
5400+ return err;
5401+}
5402+
5403+static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5404+ struct inode *h_dir, struct path *h_path)
5405+{
5406+ int err;
5407+ struct inode *dir, *inode;
5408+ struct user_namespace *h_userns;
5409+
5410+ h_userns = mnt_user_ns(h_path->mnt);
5411+ err = vfsub_removexattr(h_userns, h_path->dentry,
5412+ XATTR_NAME_POSIX_ACL_DEFAULT);
5413+ AuTraceErr(err);
5414+ if (err == -EOPNOTSUPP)
5415+ err = 0;
5416+ if (unlikely(err))
5417+ goto out;
5418+
5419+ /*
5420+ * strange behaviour from the users view,
5421+ * particularly setattr case
5422+ */
5423+ dir = d_inode(dst_parent);
5424+ if (au_ibtop(dir) == cpg->bdst)
5425+ au_cpup_attr_nlink(dir, /*force*/1);
5426+ inode = d_inode(cpg->dentry);
5427+ au_cpup_attr_nlink(inode, /*force*/1);
5428+
5429+out:
5430+ return err;
5431+}
5432+
5433+static noinline_for_stack
5434+int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
5435+ struct au_cpup_reg_attr *h_src_attr)
5436+{
5437+ int err;
5438+ umode_t mode;
5439+ unsigned int mnt_flags;
5440+ unsigned char isdir, isreg, force;
5441+ const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
5442+ struct au_dtime dt;
5443+ struct path h_path;
5444+ struct dentry *h_src, *h_dst, *h_parent;
5445+ struct inode *h_inode, *h_dir;
5446+ struct super_block *sb;
5447+
5448+ /* bsrc branch can be ro/rw. */
5449+ h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5450+ h_inode = d_inode(h_src);
5451+ AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
5452+
5453+ /* try stopping to be referenced while we are creating */
5454+ h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5455+ if (au_ftest_cpup(cpg->flags, RENAME))
5456+ AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5457+ AUFS_WH_PFX_LEN));
5458+ h_parent = h_dst->d_parent; /* dir inode is locked */
5459+ h_dir = d_inode(h_parent);
5460+ IMustLock(h_dir);
5461+ AuDebugOn(h_parent != h_dst->d_parent);
5462+
5463+ sb = cpg->dentry->d_sb;
5464+ h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
5465+ if (do_dt) {
5466+ h_path.dentry = h_parent;
5467+ au_dtime_store(&dt, dst_parent, &h_path);
5468+ }
5469+ h_path.dentry = h_dst;
5470+
5471+ isreg = 0;
5472+ isdir = 0;
5473+ mode = h_inode->i_mode;
5474+ switch (mode & S_IFMT) {
5475+ case S_IFREG:
5476+ isreg = 1;
5477+ err = vfsub_create(h_dir, &h_path, 0600, /*want_excl*/true);
5478+ if (!err)
5479+ err = au_do_cpup_regular(cpg, h_src_attr);
5480+ break;
5481+ case S_IFDIR:
5482+ isdir = 1;
5483+ err = vfsub_mkdir(h_dir, &h_path, mode);
5484+ if (!err)
5485+ err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
5486+ break;
5487+ case S_IFLNK:
5488+ err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5489+ break;
5490+ case S_IFCHR:
5491+ case S_IFBLK:
5492+ AuDebugOn(!capable(CAP_MKNOD));
5493+ fallthrough;
5494+ case S_IFIFO:
5495+ case S_IFSOCK:
5496+ err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5497+ break;
5498+ default:
5499+ AuIOErr("Unknown inode type 0%o\n", mode);
5500+ err = -EIO;
5501+ }
5502+ if (!err)
5503+ err = au_reset_acl(h_dir, &h_path, mode);
5504+
5505+ mnt_flags = au_mntflags(sb);
5506+ if (!au_opt_test(mnt_flags, UDBA_NONE)
5507+ && !isdir
5508+ && au_opt_test(mnt_flags, XINO)
5509+ && (h_inode->i_nlink == 1
5510+ || (h_inode->i_state & I_LINKABLE))
5511+ /* todo: unnecessary? */
5512+ /* && d_inode(cpg->dentry)->i_nlink == 1 */
5513+ && cpg->bdst < cpg->bsrc
5514+ && !au_ftest_cpup(cpg->flags, KEEPLINO))
5515+ au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
5516+ /* ignore this error */
5517+
5518+ if (!err) {
5519+ force = 0;
5520+ if (isreg) {
5521+ force = !!cpg->len;
5522+ if (cpg->len == -1)
5523+ force = !!i_size_read(h_inode);
5524+ }
5525+ au_fhsm_wrote(sb, cpg->bdst, force);
5526+ }
5527+
5528+ if (do_dt)
5529+ au_dtime_revert(&dt);
5530+ return err;
5531+}
5532+
5533+static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
5534+{
5535+ int err;
5536+ struct dentry *dentry, *h_dentry, *h_parent, *parent;
5537+ struct inode *h_dir;
5538+ aufs_bindex_t bdst;
5539+
5540+ dentry = cpg->dentry;
5541+ bdst = cpg->bdst;
5542+ h_dentry = au_h_dptr(dentry, bdst);
5543+ if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5544+ dget(h_dentry);
5545+ au_set_h_dptr(dentry, bdst, NULL);
5546+ err = au_lkup_neg(dentry, bdst, /*wh*/0);
5547+ if (!err)
5548+ h_path->dentry = dget(au_h_dptr(dentry, bdst));
5549+ au_set_h_dptr(dentry, bdst, h_dentry);
5550+ } else {
5551+ err = 0;
5552+ parent = dget_parent(dentry);
5553+ h_parent = au_h_dptr(parent, bdst);
5554+ dput(parent);
5555+ h_path->dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
5556+ if (IS_ERR(h_path->dentry))
5557+ err = PTR_ERR(h_path->dentry);
5558+ }
5559+ if (unlikely(err))
5560+ goto out;
5561+
5562+ h_parent = h_dentry->d_parent; /* dir inode is locked */
5563+ h_dir = d_inode(h_parent);
5564+ IMustLock(h_dir);
5565+ AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5566+ /* no delegation since it is just created */
5567+ err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
5568+ /*flags*/0);
5569+ dput(h_path->dentry);
5570+
5571+out:
5572+ return err;
5573+}
5574+
5575+/*
5576+ * copyup the @dentry from @bsrc to @bdst.
5577+ * the caller must set the both of lower dentries.
5578+ * @len is for truncating when it is -1 copyup the entire file.
5579+ * in link/rename cases, @dst_parent may be different from the real one.
5580+ * basic->bsrc can be larger than basic->bdst.
5581+ * aufs doesn't touch the credential so
5582+ * security_inode_copy_up{,_xattr}() are unnecessary.
5583+ */
5584+static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5585+{
5586+ int err, rerr;
5587+ aufs_bindex_t old_ibtop;
5588+ unsigned char isdir, plink;
5589+ struct dentry *h_src, *h_dst, *h_parent;
5590+ struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
5591+ struct super_block *sb;
5592+ struct au_branch *br;
5593+ struct path h_src_path;
5594+ /* to reduce stack size */
5595+ struct {
5596+ struct au_dtime dt;
5597+ struct path h_path;
5598+ struct au_cpup_reg_attr h_src_attr;
5599+ } *a;
5600+
5601+ err = -ENOMEM;
5602+ a = kmalloc(sizeof(*a), GFP_NOFS);
5603+ if (unlikely(!a))
5604+ goto out;
5605+ a->h_src_attr.valid = 0;
5606+
5607+ sb = cpg->dentry->d_sb;
5608+ br = au_sbr(sb, cpg->bdst);
5609+ a->h_path.mnt = au_br_mnt(br);
5610+ h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5611+ h_parent = h_dst->d_parent; /* dir inode is locked */
5612+ h_dir = d_inode(h_parent);
5613+ IMustLock(h_dir);
5614+
5615+ h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5616+ inode = d_inode(cpg->dentry);
5617+
5618+ if (!dst_parent)
5619+ dst_parent = dget_parent(cpg->dentry);
5620+ else
5621+ dget(dst_parent);
5622+
5623+ plink = !!au_opt_test(au_mntflags(sb), PLINK);
5624+ dst_inode = au_h_iptr(inode, cpg->bdst);
5625+ if (dst_inode) {
5626+ if (unlikely(!plink)) {
5627+ err = -EIO;
5628+ AuIOErr("hi%lu(i%lu) exists on b%d "
5629+ "but plink is disabled\n",
5630+ dst_inode->i_ino, inode->i_ino, cpg->bdst);
5631+ goto out_parent;
5632+ }
5633+
5634+ if (dst_inode->i_nlink) {
5635+ const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
5636+
5637+ h_src = au_plink_lkup(inode, cpg->bdst);
5638+ err = PTR_ERR(h_src);
5639+ if (IS_ERR(h_src))
5640+ goto out_parent;
5641+ if (unlikely(d_is_negative(h_src))) {
5642+ err = -EIO;
5643+ AuIOErr("i%lu exists on b%d "
5644+ "but not pseudo-linked\n",
5645+ inode->i_ino, cpg->bdst);
5646+ dput(h_src);
5647+ goto out_parent;
5648+ }
5649+
5650+ if (do_dt) {
5651+ a->h_path.dentry = h_parent;
5652+ au_dtime_store(&a->dt, dst_parent, &a->h_path);
5653+ }
5654+
5655+ a->h_path.dentry = h_dst;
5656+ delegated = NULL;
5657+ err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
5658+ if (!err && au_ftest_cpup(cpg->flags, RENAME))
5659+ err = au_do_ren_after_cpup(cpg, &a->h_path);
5660+ if (do_dt)
5661+ au_dtime_revert(&a->dt);
5662+ if (unlikely(err == -EWOULDBLOCK)) {
5663+ pr_warn("cannot retry for NFSv4 delegation"
5664+ " for an internal link\n");
5665+ iput(delegated);
5666+ }
5667+ dput(h_src);
5668+ goto out_parent;
5669+ } else
5670+ /* todo: cpup_wh_file? */
5671+ /* udba work */
5672+ au_update_ibrange(inode, /*do_put_zero*/1);
5673+ }
5674+
5675+ isdir = S_ISDIR(inode->i_mode);
5676+ old_ibtop = au_ibtop(inode);
5677+ err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
5678+ if (unlikely(err))
5679+ goto out_rev;
5680+ dst_inode = d_inode(h_dst);
5681+ inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
5682+ /* todo: necessary? */
5683+ /* au_pin_hdir_unlock(cpg->pin); */
5684+
5685+ h_src_path.dentry = h_src;
5686+ h_src_path.mnt = au_sbr_mnt(sb, cpg->bsrc);
5687+ err = cpup_iattr(cpg->dentry, cpg->bdst, &h_src_path, &a->h_src_attr);
5688+ if (unlikely(err)) {
5689+ /* todo: necessary? */
5690+ /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
5691+ inode_unlock(dst_inode);
5692+ goto out_rev;
5693+ }
5694+
5695+ if (cpg->bdst < old_ibtop) {
5696+ if (S_ISREG(inode->i_mode)) {
5697+ err = au_dy_iaop(inode, cpg->bdst, dst_inode);
5698+ if (unlikely(err)) {
5699+ /* ignore an error */
5700+ /* au_pin_hdir_relock(cpg->pin); */
5701+ inode_unlock(dst_inode);
5702+ goto out_rev;
5703+ }
5704+ }
5705+ au_set_ibtop(inode, cpg->bdst);
5706+ } else
5707+ au_set_ibbot(inode, cpg->bdst);
5708+ au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
5709+ au_hi_flags(inode, isdir));
5710+
5711+ /* todo: necessary? */
5712+ /* err = au_pin_hdir_relock(cpg->pin); */
5713+ inode_unlock(dst_inode);
5714+ if (unlikely(err))
5715+ goto out_rev;
5716+
5717+ src_inode = d_inode(h_src);
5718+ if (!isdir
5719+ && (src_inode->i_nlink > 1
5720+ || src_inode->i_state & I_LINKABLE)
5721+ && plink)
5722+ au_plink_append(inode, cpg->bdst, h_dst);
5723+
5724+ if (au_ftest_cpup(cpg->flags, RENAME)) {
5725+ a->h_path.dentry = h_dst;
5726+ err = au_do_ren_after_cpup(cpg, &a->h_path);
5727+ }
5728+ if (!err)
5729+ goto out_parent; /* success */
5730+
5731+ /* revert */
5732+out_rev:
5733+ a->h_path.dentry = h_parent;
5734+ au_dtime_store(&a->dt, dst_parent, &a->h_path);
5735+ a->h_path.dentry = h_dst;
5736+ rerr = 0;
5737+ if (d_is_positive(h_dst)) {
5738+ if (!isdir) {
5739+ /* no delegation since it is just created */
5740+ rerr = vfsub_unlink(h_dir, &a->h_path,
5741+ /*delegated*/NULL, /*force*/0);
5742+ } else
5743+ rerr = vfsub_rmdir(h_dir, &a->h_path);
5744+ }
5745+ au_dtime_revert(&a->dt);
5746+ if (rerr) {
5747+ AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5748+ err = -EIO;
5749+ }
5750+out_parent:
5751+ dput(dst_parent);
5752+ au_kfree_rcu(a);
5753+out:
5754+ return err;
5755+}
5756+
5757+#if 0 /* reserved */
5758+struct au_cpup_single_args {
5759+ int *errp;
5760+ struct au_cp_generic *cpg;
5761+ struct dentry *dst_parent;
5762+};
5763+
5764+static void au_call_cpup_single(void *args)
5765+{
5766+ struct au_cpup_single_args *a = args;
5767+
5768+ au_pin_hdir_acquire_nest(a->cpg->pin);
5769+ *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5770+ au_pin_hdir_release(a->cpg->pin);
5771+}
5772+#endif
5773+
5774+/*
5775+ * prevent SIGXFSZ in copy-up.
5776+ * testing CAP_MKNOD is for generic fs,
5777+ * but CAP_FSETID is for xfs only, currently.
5778+ */
5779+static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
5780+{
5781+ int do_sio;
5782+ struct super_block *sb;
5783+ struct inode *h_dir;
5784+
5785+ do_sio = 0;
5786+ sb = au_pinned_parent(pin)->d_sb;
5787+ if (!au_wkq_test()
5788+ && (!au_sbi(sb)->si_plink_maint_pid
5789+ || au_plink_maint(sb, AuLock_NOPLM))) {
5790+ switch (mode & S_IFMT) {
5791+ case S_IFREG:
5792+ /* no condition about RLIMIT_FSIZE and the file size */
5793+ do_sio = 1;
5794+ break;
5795+ case S_IFCHR:
5796+ case S_IFBLK:
5797+ do_sio = !capable(CAP_MKNOD);
5798+ break;
5799+ }
5800+ if (!do_sio)
5801+ do_sio = ((mode & (S_ISUID | S_ISGID))
5802+ && !capable(CAP_FSETID));
5803+ /* this workaround may be removed in the future */
5804+ if (!do_sio) {
5805+ h_dir = au_pinned_h_dir(pin);
5806+ do_sio = h_dir->i_mode & S_ISVTX;
5807+ }
5808+ }
5809+
5810+ return do_sio;
5811+}
5812+
5813+#if 0 /* reserved */
5814+int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5815+{
5816+ int err, wkq_err;
5817+ struct dentry *h_dentry;
5818+
5819+ h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5820+ if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
5821+ err = au_cpup_single(cpg, dst_parent);
5822+ else {
5823+ struct au_cpup_single_args args = {
5824+ .errp = &err,
5825+ .cpg = cpg,
5826+ .dst_parent = dst_parent
5827+ };
5828+ wkq_err = au_wkq_wait(au_call_cpup_single, &args);
5829+ if (unlikely(wkq_err))
5830+ err = wkq_err;
5831+ }
5832+
5833+ return err;
5834+}
5835+#endif
5836+
5837+/*
5838+ * copyup the @dentry from the first active lower branch to @bdst,
5839+ * using au_cpup_single().
5840+ */
5841+static int au_cpup_simple(struct au_cp_generic *cpg)
5842+{
5843+ int err;
5844+ unsigned int flags_orig;
5845+ struct dentry *dentry;
5846+
5847+ AuDebugOn(cpg->bsrc < 0);
5848+
5849+ dentry = cpg->dentry;
5850+ DiMustWriteLock(dentry);
5851+
5852+ err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
5853+ if (!err) {
5854+ flags_orig = cpg->flags;
5855+ au_fset_cpup(cpg->flags, RENAME);
5856+ err = au_cpup_single(cpg, NULL);
5857+ cpg->flags = flags_orig;
5858+ if (!err)
5859+ return 0; /* success */
5860+
5861+ /* revert */
5862+ au_set_h_dptr(dentry, cpg->bdst, NULL);
5863+ au_set_dbtop(dentry, cpg->bsrc);
5864+ }
5865+
5866+ return err;
5867+}
5868+
5869+struct au_cpup_simple_args {
5870+ int *errp;
5871+ struct au_cp_generic *cpg;
5872+};
5873+
5874+static void au_call_cpup_simple(void *args)
5875+{
5876+ struct au_cpup_simple_args *a = args;
5877+
5878+ au_pin_hdir_acquire_nest(a->cpg->pin);
5879+ *a->errp = au_cpup_simple(a->cpg);
5880+ au_pin_hdir_release(a->cpg->pin);
5881+}
5882+
5883+static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
5884+{
5885+ int err, wkq_err;
5886+ struct dentry *dentry, *parent;
5887+ struct file *h_file;
5888+ struct inode *h_dir;
5889+ struct user_namespace *h_userns;
5890+
5891+ dentry = cpg->dentry;
5892+ h_file = NULL;
5893+ if (au_ftest_cpup(cpg->flags, HOPEN)) {
5894+ AuDebugOn(cpg->bsrc < 0);
5895+ h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
5896+ err = PTR_ERR(h_file);
5897+ if (IS_ERR(h_file))
5898+ goto out;
5899+ }
5900+
5901+ parent = dget_parent(dentry);
5902+ h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
5903+ h_userns = au_sbr_userns(dentry->d_sb, cpg->bdst);
5904+ if (!au_test_h_perm_sio(h_userns, h_dir, MAY_EXEC | MAY_WRITE)
5905+ && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
5906+ err = au_cpup_simple(cpg);
5907+ else {
5908+ struct au_cpup_simple_args args = {
5909+ .errp = &err,
5910+ .cpg = cpg
5911+ };
5912+ wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
5913+ if (unlikely(wkq_err))
5914+ err = wkq_err;
5915+ }
5916+
5917+ dput(parent);
5918+ if (h_file)
5919+ au_h_open_post(dentry, cpg->bsrc, h_file);
5920+
5921+out:
5922+ return err;
5923+}
5924+
5925+int au_sio_cpup_simple(struct au_cp_generic *cpg)
5926+{
5927+ aufs_bindex_t bsrc, bbot;
5928+ struct dentry *dentry, *h_dentry;
5929+
5930+ if (cpg->bsrc < 0) {
5931+ dentry = cpg->dentry;
5932+ bbot = au_dbbot(dentry);
5933+ for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
5934+ h_dentry = au_h_dptr(dentry, bsrc);
5935+ if (h_dentry) {
5936+ AuDebugOn(d_is_negative(h_dentry));
5937+ break;
5938+ }
5939+ }
5940+ AuDebugOn(bsrc > bbot);
5941+ cpg->bsrc = bsrc;
5942+ }
5943+ AuDebugOn(cpg->bsrc <= cpg->bdst);
5944+ return au_do_sio_cpup_simple(cpg);
5945+}
5946+
5947+int au_sio_cpdown_simple(struct au_cp_generic *cpg)
5948+{
5949+ AuDebugOn(cpg->bdst <= cpg->bsrc);
5950+ return au_do_sio_cpup_simple(cpg);
5951+}
5952+
5953+/* ---------------------------------------------------------------------- */
5954+
5955+/*
5956+ * copyup the deleted file for writing.
5957+ */
5958+static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
5959+ struct file *file)
5960+{
5961+ int err;
5962+ unsigned int flags_orig;
5963+ aufs_bindex_t bsrc_orig;
5964+ struct au_dinfo *dinfo;
5965+ struct {
5966+ struct au_hdentry *hd;
5967+ struct dentry *h_dentry;
5968+ } hdst, hsrc;
5969+
5970+ dinfo = au_di(cpg->dentry);
5971+ AuRwMustWriteLock(&dinfo->di_rwsem);
5972+
5973+ bsrc_orig = cpg->bsrc;
5974+ cpg->bsrc = dinfo->di_btop;
5975+ hdst.hd = au_hdentry(dinfo, cpg->bdst);
5976+ hdst.h_dentry = hdst.hd->hd_dentry;
5977+ hdst.hd->hd_dentry = wh_dentry;
5978+ dinfo->di_btop = cpg->bdst;
5979+
5980+ hsrc.h_dentry = NULL;
5981+ if (file) {
5982+ hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
5983+ hsrc.h_dentry = hsrc.hd->hd_dentry;
5984+ hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
5985+ }
5986+ flags_orig = cpg->flags;
5987+ cpg->flags = !AuCpup_DTIME;
5988+ err = au_cpup_single(cpg, /*h_parent*/NULL);
5989+ cpg->flags = flags_orig;
5990+ if (file) {
5991+ if (!err)
5992+ err = au_reopen_nondir(file);
5993+ hsrc.hd->hd_dentry = hsrc.h_dentry;
5994+ }
5995+ hdst.hd->hd_dentry = hdst.h_dentry;
5996+ dinfo->di_btop = cpg->bsrc;
5997+ cpg->bsrc = bsrc_orig;
5998+
5999+ return err;
6000+}
6001+
6002+static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
6003+{
6004+ int err;
6005+ aufs_bindex_t bdst;
6006+ struct au_dtime dt;
6007+ struct dentry *dentry, *parent, *h_parent, *wh_dentry;
6008+ struct au_branch *br;
6009+ struct path h_path;
6010+
6011+ dentry = cpg->dentry;
6012+ bdst = cpg->bdst;
6013+ br = au_sbr(dentry->d_sb, bdst);
6014+ parent = dget_parent(dentry);
6015+ h_parent = au_h_dptr(parent, bdst);
6016+ wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
6017+ err = PTR_ERR(wh_dentry);
6018+ if (IS_ERR(wh_dentry))
6019+ goto out;
6020+
6021+ h_path.dentry = h_parent;
6022+ h_path.mnt = au_br_mnt(br);
6023+ au_dtime_store(&dt, parent, &h_path);
6024+ err = au_do_cpup_wh(cpg, wh_dentry, file);
6025+ if (unlikely(err))
6026+ goto out_wh;
6027+
6028+ dget(wh_dentry);
6029+ h_path.dentry = wh_dentry;
6030+ if (!d_is_dir(wh_dentry)) {
6031+ /* no delegation since it is just created */
6032+ err = vfsub_unlink(d_inode(h_parent), &h_path,
6033+ /*delegated*/NULL, /*force*/0);
6034+ } else
6035+ err = vfsub_rmdir(d_inode(h_parent), &h_path);
6036+ if (unlikely(err)) {
6037+ AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
6038+ wh_dentry, err);
6039+ err = -EIO;
6040+ }
6041+ au_dtime_revert(&dt);
6042+ au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
6043+
6044+out_wh:
6045+ dput(wh_dentry);
6046+out:
6047+ dput(parent);
6048+ return err;
6049+}
6050+
6051+struct au_cpup_wh_args {
6052+ int *errp;
6053+ struct au_cp_generic *cpg;
6054+ struct file *file;
6055+};
6056+
6057+static void au_call_cpup_wh(void *args)
6058+{
6059+ struct au_cpup_wh_args *a = args;
6060+
6061+ au_pin_hdir_acquire_nest(a->cpg->pin);
6062+ *a->errp = au_cpup_wh(a->cpg, a->file);
6063+ au_pin_hdir_release(a->cpg->pin);
6064+}
6065+
6066+int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
6067+{
6068+ int err, wkq_err;
6069+ aufs_bindex_t bdst;
6070+ struct dentry *dentry, *parent, *h_orph, *h_parent;
6071+ struct inode *dir, *h_dir, *h_tmpdir;
6072+ struct au_wbr *wbr;
6073+ struct au_pin wh_pin, *pin_orig;
6074+ struct user_namespace *h_userns;
6075+
6076+ dentry = cpg->dentry;
6077+ bdst = cpg->bdst;
6078+ parent = dget_parent(dentry);
6079+ dir = d_inode(parent);
6080+ h_orph = NULL;
6081+ h_parent = NULL;
6082+ h_dir = au_igrab(au_h_iptr(dir, bdst));
6083+ h_tmpdir = h_dir;
6084+ pin_orig = NULL;
6085+ if (!h_dir->i_nlink) {
6086+ wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
6087+ h_orph = wbr->wbr_orph;
6088+
6089+ h_parent = dget(au_h_dptr(parent, bdst));
6090+ au_set_h_dptr(parent, bdst, dget(h_orph));
6091+ h_tmpdir = d_inode(h_orph);
6092+ au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
6093+
6094+ inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
6095+ /* todo: au_h_open_pre()? */
6096+
6097+ pin_orig = cpg->pin;
6098+ au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
6099+ AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
6100+ cpg->pin = &wh_pin;
6101+ }
6102+
6103+ h_userns = au_sbr_userns(dentry->d_sb, bdst);
6104+ if (!au_test_h_perm_sio(h_userns, h_tmpdir, MAY_EXEC | MAY_WRITE)
6105+ && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
6106+ err = au_cpup_wh(cpg, file);
6107+ else {
6108+ struct au_cpup_wh_args args = {
6109+ .errp = &err,
6110+ .cpg = cpg,
6111+ .file = file
6112+ };
6113+ wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
6114+ if (unlikely(wkq_err))
6115+ err = wkq_err;
6116+ }
6117+
6118+ if (h_orph) {
6119+ inode_unlock(h_tmpdir);
6120+ /* todo: au_h_open_post()? */
6121+ au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
6122+ au_set_h_dptr(parent, bdst, h_parent);
6123+ AuDebugOn(!pin_orig);
6124+ cpg->pin = pin_orig;
6125+ }
6126+ iput(h_dir);
6127+ dput(parent);
6128+
6129+ return err;
6130+}
6131+
6132+/* ---------------------------------------------------------------------- */
6133+
6134+/*
6135+ * generic routine for both of copy-up and copy-down.
6136+ */
6137+/* cf. revalidate function in file.c */
6138+int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6139+ int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6140+ struct au_pin *pin,
6141+ struct dentry *h_parent, void *arg),
6142+ void *arg)
6143+{
6144+ int err;
6145+ struct au_pin pin;
6146+ struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
6147+
6148+ err = 0;
6149+ parent = dget_parent(dentry);
6150+ if (IS_ROOT(parent))
6151+ goto out;
6152+
6153+ au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
6154+ au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
6155+
6156+ /* do not use au_dpage */
6157+ real_parent = parent;
6158+ while (1) {
6159+ dput(parent);
6160+ parent = dget_parent(dentry);
6161+ h_parent = au_h_dptr(parent, bdst);
6162+ if (h_parent)
6163+ goto out; /* success */
6164+
6165+ /* find top dir which is necessary to cpup */
6166+ do {
6167+ d = parent;
6168+ dput(parent);
6169+ parent = dget_parent(d);
6170+ di_read_lock_parent3(parent, !AuLock_IR);
6171+ h_parent = au_h_dptr(parent, bdst);
6172+ di_read_unlock(parent, !AuLock_IR);
6173+ } while (!h_parent);
6174+
6175+ if (d != real_parent)
6176+ di_write_lock_child3(d);
6177+
6178+ /* somebody else might create while we were sleeping */
6179+ h_dentry = au_h_dptr(d, bdst);
6180+ if (!h_dentry || d_is_negative(h_dentry)) {
6181+ if (h_dentry)
6182+ au_update_dbtop(d);
6183+
6184+ au_pin_set_dentry(&pin, d);
6185+ err = au_do_pin(&pin);
6186+ if (!err) {
6187+ err = cp(d, bdst, &pin, h_parent, arg);
6188+ au_unpin(&pin);
6189+ }
6190+ }
6191+
6192+ if (d != real_parent)
6193+ di_write_unlock(d);
6194+ if (unlikely(err))
6195+ break;
6196+ }
6197+
6198+out:
6199+ dput(parent);
6200+ return err;
6201+}
6202+
6203+static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
6204+ struct au_pin *pin,
6205+ struct dentry *h_parent __maybe_unused,
6206+ void *arg __maybe_unused)
6207+{
6208+ struct au_cp_generic cpg = {
6209+ .dentry = dentry,
6210+ .bdst = bdst,
6211+ .bsrc = -1,
6212+ .len = 0,
6213+ .pin = pin,
6214+ .flags = AuCpup_DTIME
6215+ };
6216+ return au_sio_cpup_simple(&cpg);
6217+}
6218+
6219+int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6220+{
6221+ return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
6222+}
6223+
6224+int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6225+{
6226+ int err;
6227+ struct dentry *parent;
6228+ struct inode *dir;
6229+
6230+ parent = dget_parent(dentry);
6231+ dir = d_inode(parent);
6232+ err = 0;
6233+ if (au_h_iptr(dir, bdst))
6234+ goto out;
6235+
6236+ di_read_unlock(parent, AuLock_IR);
6237+ di_write_lock_parent(parent);
6238+ /* someone else might change our inode while we were sleeping */
6239+ if (!au_h_iptr(dir, bdst))
6240+ err = au_cpup_dirs(dentry, bdst);
6241+ di_downgrade_lock(parent, AuLock_IR);
6242+
6243+out:
6244+ dput(parent);
6245+ return err;
6246+}
6247diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
6248--- /usr/share/empty/fs/aufs/cpup.h 1970-01-01 01:00:00.000000000 +0100
6249+++ linux/fs/aufs/cpup.h 2021-02-24 13:33:42.741013619 +0100
6250@@ -0,0 +1,100 @@
6251+/* SPDX-License-Identifier: GPL-2.0 */
6252+/*
6253+ * Copyright (C) 2005-2020 Junjiro R. Okajima
6254+ *
6255+ * This program, aufs is free software; you can redistribute it and/or modify
6256+ * it under the terms of the GNU General Public License as published by
6257+ * the Free Software Foundation; either version 2 of the License, or
6258+ * (at your option) any later version.
6259+ *
6260+ * This program is distributed in the hope that it will be useful,
6261+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6262+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6263+ * GNU General Public License for more details.
6264+ *
6265+ * You should have received a copy of the GNU General Public License
6266+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
6267+ */
6268+
6269+/*
6270+ * copy-up/down functions
6271+ */
6272+
6273+#ifndef __AUFS_CPUP_H__
6274+#define __AUFS_CPUP_H__
6275+
6276+#ifdef __KERNEL__
6277+
6278+#include <linux/path.h>
6279+
6280+struct inode;
6281+struct file;
6282+struct au_pin;
6283+
6284+void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
6285+void au_cpup_attr_timesizes(struct inode *inode);
6286+void au_cpup_attr_nlink(struct inode *inode, int force);
6287+void au_cpup_attr_changeable(struct inode *inode);
6288+void au_cpup_igen(struct inode *inode, struct inode *h_inode);
6289+void au_cpup_attr_all(struct inode *inode, int force);
6290+
6291+/* ---------------------------------------------------------------------- */
6292+
6293+struct au_cp_generic {
6294+ struct dentry *dentry;
6295+ aufs_bindex_t bdst, bsrc;
6296+ loff_t len;
6297+ struct au_pin *pin;
6298+ unsigned int flags;
6299+};
6300+
6301+/* cpup flags */
6302+#define AuCpup_DTIME 1 /* do dtime_store/revert */
6303+#define AuCpup_KEEPLINO (1 << 1) /* do not clear the lower xino,
6304+ for link(2) */
6305+#define AuCpup_RENAME (1 << 2) /* rename after cpup */
6306+#define AuCpup_HOPEN (1 << 3) /* call h_open_pre/post() in
6307+ cpup */
6308+#define AuCpup_OVERWRITE (1 << 4) /* allow overwriting the
6309+ existing entry */
6310+#define AuCpup_RWDST (1 << 5) /* force write target even if
6311+ the branch is marked as RO */
6312+
6313+#ifndef CONFIG_AUFS_BR_HFSPLUS
6314+#undef AuCpup_HOPEN
6315+#define AuCpup_HOPEN 0
6316+#endif
6317+
6318+#define au_ftest_cpup(flags, name) ((flags) & AuCpup_##name)
6319+#define au_fset_cpup(flags, name) \
6320+ do { (flags) |= AuCpup_##name; } while (0)
6321+#define au_fclr_cpup(flags, name) \
6322+ do { (flags) &= ~AuCpup_##name; } while (0)
6323+
6324+int au_copy_file(struct file *dst, struct file *src, loff_t len);
6325+int au_sio_cpup_simple(struct au_cp_generic *cpg);
6326+int au_sio_cpdown_simple(struct au_cp_generic *cpg);
6327+int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
6328+
6329+int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6330+ int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6331+ struct au_pin *pin,
6332+ struct dentry *h_parent, void *arg),
6333+ void *arg);
6334+int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6335+int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6336+
6337+/* ---------------------------------------------------------------------- */
6338+
6339+/* keep timestamps when copyup */
6340+struct au_dtime {
6341+ struct dentry *dt_dentry;
6342+ struct path dt_h_path;
6343+ struct timespec64 dt_atime, dt_mtime;
6344+};
6345+void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6346+ struct path *h_path);
6347+void au_dtime_revert(struct au_dtime *dt);
6348+
6349+#endif /* __KERNEL__ */
6350+#endif /* __AUFS_CPUP_H__ */
6351diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
6352--- /usr/share/empty/fs/aufs/dbgaufs.c 1970-01-01 01:00:00.000000000 +0100
6353+++ linux/fs/aufs/dbgaufs.c 2021-02-24 13:33:42.741013619 +0100
6354@@ -0,0 +1,526 @@
6355+// SPDX-License-Identifier: GPL-2.0
6356+/*
6357+ * Copyright (C) 2005-2020 Junjiro R. Okajima
6358+ *
6359+ * This program, aufs is free software; you can redistribute it and/or modify
6360+ * it under the terms of the GNU General Public License as published by
6361+ * the Free Software Foundation; either version 2 of the License, or
6362+ * (at your option) any later version.
6363+ *
6364+ * This program is distributed in the hope that it will be useful,
6365+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6366+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6367+ * GNU General Public License for more details.
6368+ *
6369+ * You should have received a copy of the GNU General Public License
6370+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
6371+ */
6372+
6373+/*
6374+ * debugfs interface
6375+ */
6376+
6377+#include <linux/debugfs.h>
6378+#include "aufs.h"
6379+
6380+#ifndef CONFIG_SYSFS
6381+#error DEBUG_FS depends upon SYSFS
6382+#endif
6383+
6384+static struct dentry *dbgaufs;
6385+static const mode_t dbgaufs_mode = 0444;
6386+
6387+/* 20 is max digits length of ulong 64 */
6388+struct dbgaufs_arg {
6389+ int n;
6390+ char a[20 * 4];
6391+};
6392+
6393+/*
6394+ * common function for all XINO files
6395+ */
6396+static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6397+ struct file *file)
6398+{
6399+ void *p;
6400+
6401+ p = file->private_data;
6402+ if (p) {
6403+ /* this is struct dbgaufs_arg */
6404+ AuDebugOn(!au_kfree_sz_test(p));
6405+ au_kfree_do_rcu(p);
6406+ }
6407+ return 0;
6408+}
6409+
6410+static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt,
6411+ int cnt)
6412+{
6413+ int err;
6414+ struct kstat st;
6415+ struct dbgaufs_arg *p;
6416+
6417+ err = -ENOMEM;
6418+ p = kmalloc(sizeof(*p), GFP_NOFS);
6419+ if (unlikely(!p))
6420+ goto out;
6421+
6422+ err = 0;
6423+ p->n = 0;
6424+ file->private_data = p;
6425+ if (!xf)
6426+ goto out;
6427+
6428+ err = vfsub_getattr(&xf->f_path, &st);
6429+ if (!err) {
6430+ if (do_fcnt)
6431+ p->n = snprintf
6432+ (p->a, sizeof(p->a), "%d, %llux%u %lld\n",
6433+ cnt, st.blocks, st.blksize,
6434+ (long long)st.size);
6435+ else
6436+ p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
6437+ st.blocks, st.blksize,
6438+ (long long)st.size);
6439+ AuDebugOn(p->n >= sizeof(p->a));
6440+ } else {
6441+ p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6442+ err = 0;
6443+ }
6444+
6445+out:
6446+ return err;
6447+}
6448+
6449+static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6450+ size_t count, loff_t *ppos)
6451+{
6452+ struct dbgaufs_arg *p;
6453+
6454+ p = file->private_data;
6455+ return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6456+}
6457+
6458+/* ---------------------------------------------------------------------- */
6459+
6460+struct dbgaufs_plink_arg {
6461+ int n;
6462+ char a[];
6463+};
6464+
6465+static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6466+ struct file *file)
6467+{
6468+ free_page((unsigned long)file->private_data);
6469+ return 0;
6470+}
6471+
6472+static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6473+{
6474+ int err, i, limit;
6475+ unsigned long n, sum;
6476+ struct dbgaufs_plink_arg *p;
6477+ struct au_sbinfo *sbinfo;
6478+ struct super_block *sb;
6479+ struct hlist_bl_head *hbl;
6480+
6481+ err = -ENOMEM;
6482+ p = (void *)get_zeroed_page(GFP_NOFS);
6483+ if (unlikely(!p))
6484+ goto out;
6485+
6486+ err = -EFBIG;
6487+ sbinfo = inode->i_private;
6488+ sb = sbinfo->si_sb;
6489+ si_noflush_read_lock(sb);
6490+ if (au_opt_test(au_mntflags(sb), PLINK)) {
6491+ limit = PAGE_SIZE - sizeof(p->n);
6492+
6493+ /* the number of buckets */
6494+ n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6495+ p->n += n;
6496+ limit -= n;
6497+
6498+ sum = 0;
6499+ for (i = 0, hbl = sbinfo->si_plink; i < AuPlink_NHASH;
6500+ i++, hbl++) {
6501+ n = au_hbl_count(hbl);
6502+ sum += n;
6503+
6504+ n = snprintf(p->a + p->n, limit, "%lu ", n);
6505+ p->n += n;
6506+ limit -= n;
6507+ if (unlikely(limit <= 0))
6508+ goto out_free;
6509+ }
6510+ p->a[p->n - 1] = '\n';
6511+
6512+ /* the sum of plinks */
6513+ n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6514+ p->n += n;
6515+ limit -= n;
6516+ if (unlikely(limit <= 0))
6517+ goto out_free;
6518+ } else {
6519+#define str "1\n0\n0\n"
6520+ p->n = sizeof(str) - 1;
6521+ strcpy(p->a, str);
6522+#undef str
6523+ }
6524+ si_read_unlock(sb);
6525+
6526+ err = 0;
6527+ file->private_data = p;
6528+ goto out; /* success */
6529+
6530+out_free:
6531+ free_page((unsigned long)p);
6532+out:
6533+ return err;
6534+}
6535+
6536+static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6537+ size_t count, loff_t *ppos)
6538+{
6539+ struct dbgaufs_plink_arg *p;
6540+
6541+ p = file->private_data;
6542+ return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6543+}
6544+
6545+static const struct file_operations dbgaufs_plink_fop = {
6546+ .owner = THIS_MODULE,
6547+ .open = dbgaufs_plink_open,
6548+ .release = dbgaufs_plink_release,
6549+ .read = dbgaufs_plink_read
6550+};
6551+
6552+/* ---------------------------------------------------------------------- */
6553+
6554+static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6555+{
6556+ int err;
6557+ struct au_sbinfo *sbinfo;
6558+ struct super_block *sb;
6559+
6560+ sbinfo = inode->i_private;
6561+ sb = sbinfo->si_sb;
6562+ si_noflush_read_lock(sb);
6563+ err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0, /*cnt*/0);
6564+ si_read_unlock(sb);
6565+ return err;
6566+}
6567+
6568+static const struct file_operations dbgaufs_xib_fop = {
6569+ .owner = THIS_MODULE,
6570+ .open = dbgaufs_xib_open,
6571+ .release = dbgaufs_xi_release,
6572+ .read = dbgaufs_xi_read
6573+};
6574+
6575+/* ---------------------------------------------------------------------- */
6576+
6577+#define DbgaufsXi_PREFIX "xi"
6578+
6579+static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6580+{
6581+ int err, idx;
6582+ long l;
6583+ aufs_bindex_t bindex;
6584+ char *p, a[sizeof(DbgaufsXi_PREFIX) + 8];
6585+ struct au_sbinfo *sbinfo;
6586+ struct super_block *sb;
6587+ struct au_xino *xi;
6588+ struct file *xf;
6589+ struct qstr *name;
6590+ struct au_branch *br;
6591+
6592+ err = -ENOENT;
6593+ name = &file->f_path.dentry->d_name;
6594+ if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6595+ || memcmp(name->name, DbgaufsXi_PREFIX,
6596+ sizeof(DbgaufsXi_PREFIX) - 1)))
6597+ goto out;
6598+
6599+ AuDebugOn(name->len >= sizeof(a));
6600+ memcpy(a, name->name, name->len);
6601+ a[name->len] = '\0';
6602+ p = strchr(a, '-');
6603+ if (p)
6604+ *p = '\0';
6605+ err = kstrtol(a + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
6606+ if (unlikely(err))
6607+ goto out;
6608+ bindex = l;
6609+ idx = 0;
6610+ if (p) {
6611+ err = kstrtol(p + 1, 10, &l);
6612+ if (unlikely(err))
6613+ goto out;
6614+ idx = l;
6615+ }
6616+
6617+ err = -ENOENT;
6618+ sbinfo = inode->i_private;
6619+ sb = sbinfo->si_sb;
6620+ si_noflush_read_lock(sb);
6621+ if (unlikely(bindex < 0 || bindex > au_sbbot(sb)))
6622+ goto out_si;
6623+ br = au_sbr(sb, bindex);
6624+ xi = br->br_xino;
6625+ if (unlikely(idx >= xi->xi_nfile))
6626+ goto out_si;
6627+ xf = au_xino_file(xi, idx);
6628+ if (xf)
6629+ err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1,
6630+ au_xino_count(br));
6631+
6632+out_si:
6633+ si_read_unlock(sb);
6634+out:
6635+ AuTraceErr(err);
6636+ return err;
6637+}
6638+
6639+static const struct file_operations dbgaufs_xino_fop = {
6640+ .owner = THIS_MODULE,
6641+ .open = dbgaufs_xino_open,
6642+ .release = dbgaufs_xi_release,
6643+ .read = dbgaufs_xi_read
6644+};
6645+
6646+void dbgaufs_xino_del(struct au_branch *br)
6647+{
6648+ struct dentry *dbgaufs;
6649+
6650+ dbgaufs = br->br_dbgaufs;
6651+ if (!dbgaufs)
6652+ return;
6653+
6654+ br->br_dbgaufs = NULL;
6655+ /* debugfs acquires the parent i_mutex */
6656+ lockdep_off();
6657+ debugfs_remove(dbgaufs);
6658+ lockdep_on();
6659+}
6660+
6661+void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6662+{
6663+ aufs_bindex_t bbot;
6664+ struct au_branch *br;
6665+
6666+ if (!au_sbi(sb)->si_dbgaufs)
6667+ return;
6668+
6669+ bbot = au_sbbot(sb);
6670+ for (; bindex <= bbot; bindex++) {
6671+ br = au_sbr(sb, bindex);
6672+ dbgaufs_xino_del(br);
6673+ }
6674+}
6675+
6676+static void dbgaufs_br_do_add(struct super_block *sb, aufs_bindex_t bindex,
6677+ unsigned int idx, struct dentry *parent,
6678+ struct au_sbinfo *sbinfo)
6679+{
6680+ struct au_branch *br;
6681+ struct dentry *d;
6682+ /* "xi" bindex(5) "-" idx(2) NULL */
6683+ char name[sizeof(DbgaufsXi_PREFIX) + 8];
6684+
6685+ if (!idx)
6686+ snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6687+ else
6688+ snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d-%u",
6689+ bindex, idx);
6690+ br = au_sbr(sb, bindex);
6691+ if (br->br_dbgaufs) {
6692+ struct qstr qstr = QSTR_INIT(name, strlen(name));
6693+
6694+ if (!au_qstreq(&br->br_dbgaufs->d_name, &qstr)) {
6695+ /* debugfs acquires the parent i_mutex */
6696+ lockdep_off();
6697+ d = debugfs_rename(parent, br->br_dbgaufs, parent,
6698+ name);
6699+ lockdep_on();
6700+ if (unlikely(!d))
6701+ pr_warn("failed renaming %pd/%s, ignored.\n",
6702+ parent, name);
6703+ }
6704+ } else {
6705+ lockdep_off();
6706+ br->br_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6707+ sbinfo, &dbgaufs_xino_fop);
6708+ lockdep_on();
6709+ if (unlikely(!br->br_dbgaufs))
6710+ pr_warn("failed creating %pd/%s, ignored.\n",
6711+ parent, name);
6712+ }
6713+}
6714+
6715+static void dbgaufs_br_add(struct super_block *sb, aufs_bindex_t bindex,
6716+ struct dentry *parent, struct au_sbinfo *sbinfo)
6717+{
6718+ struct au_branch *br;
6719+ struct au_xino *xi;
6720+ unsigned int u;
6721+
6722+ br = au_sbr(sb, bindex);
6723+ xi = br->br_xino;
6724+ for (u = 0; u < xi->xi_nfile; u++)
6725+ dbgaufs_br_do_add(sb, bindex, u, parent, sbinfo);
6726+}
6727+
6728+void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown)
6729+{
6730+ struct au_sbinfo *sbinfo;
6731+ struct dentry *parent;
6732+ aufs_bindex_t bbot;
6733+
6734+ if (!au_opt_test(au_mntflags(sb), XINO))
6735+ return;
6736+
6737+ sbinfo = au_sbi(sb);
6738+ parent = sbinfo->si_dbgaufs;
6739+ if (!parent)
6740+ return;
6741+
6742+ bbot = au_sbbot(sb);
6743+ if (topdown)
6744+ for (; bindex <= bbot; bindex++)
6745+ dbgaufs_br_add(sb, bindex, parent, sbinfo);
6746+ else
6747+ for (; bbot >= bindex; bbot--)
6748+ dbgaufs_br_add(sb, bbot, parent, sbinfo);
6749+}
6750+
6751+/* ---------------------------------------------------------------------- */
6752+
6753+#ifdef CONFIG_AUFS_EXPORT
6754+static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6755+{
6756+ int err;
6757+ struct au_sbinfo *sbinfo;
6758+ struct super_block *sb;
6759+
6760+ sbinfo = inode->i_private;
6761+ sb = sbinfo->si_sb;
6762+ si_noflush_read_lock(sb);
6763+ err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0, /*cnt*/0);
6764+ si_read_unlock(sb);
6765+ return err;
6766+}
6767+
6768+static const struct file_operations dbgaufs_xigen_fop = {
6769+ .owner = THIS_MODULE,
6770+ .open = dbgaufs_xigen_open,
6771+ .release = dbgaufs_xi_release,
6772+ .read = dbgaufs_xi_read
6773+};
6774+
6775+static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6776+{
6777+ int err;
6778+
6779+ /*
6780+ * This function is a dynamic '__init' function actually,
6781+ * so the tiny check for si_rwsem is unnecessary.
6782+ */
6783+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6784+
6785+ err = -EIO;
6786+ sbinfo->si_dbgaufs_xigen = debugfs_create_file
6787+ ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6788+ &dbgaufs_xigen_fop);
6789+ if (sbinfo->si_dbgaufs_xigen)
6790+ err = 0;
6791+
6792+ return err;
6793+}
6794+#else
6795+static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6796+{
6797+ return 0;
6798+}
6799+#endif /* CONFIG_AUFS_EXPORT */
6800+
6801+/* ---------------------------------------------------------------------- */
6802+
6803+void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6804+{
6805+ /*
6806+ * This function is a dynamic '__fin' function actually,
6807+ * so the tiny check for si_rwsem is unnecessary.
6808+ */
6809+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6810+
6811+ debugfs_remove_recursive(sbinfo->si_dbgaufs);
6812+ sbinfo->si_dbgaufs = NULL;
6813+}
6814+
6815+int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6816+{
6817+ int err;
6818+ char name[SysaufsSiNameLen];
6819+
6820+ /*
6821+ * This function is a dynamic '__init' function actually,
6822+ * so the tiny check for si_rwsem is unnecessary.
6823+ */
6824+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6825+
6826+ err = -ENOENT;
6827+ if (!dbgaufs) {
6828+ AuErr1("/debug/aufs is uninitialized\n");
6829+ goto out;
6830+ }
6831+
6832+ err = -EIO;
6833+ sysaufs_name(sbinfo, name);
6834+ sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6835+ if (unlikely(!sbinfo->si_dbgaufs))
6836+ goto out;
6837+
6838+ /* regardless plink/noplink option */
6839+ sbinfo->si_dbgaufs_plink = debugfs_create_file
6840+ ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6841+ &dbgaufs_plink_fop);
6842+ if (unlikely(!sbinfo->si_dbgaufs_plink))
6843+ goto out_dir;
6844+
6845+ /* regardless xino/noxino option */
6846+ sbinfo->si_dbgaufs_xib = debugfs_create_file
6847+ ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6848+ &dbgaufs_xib_fop);
6849+ if (unlikely(!sbinfo->si_dbgaufs_xib))
6850+ goto out_dir;
6851+
6852+ err = dbgaufs_xigen_init(sbinfo);
6853+ if (!err)
6854+ goto out; /* success */
6855+
6856+out_dir:
6857+ dbgaufs_si_fin(sbinfo);
6858+out:
6859+ if (unlikely(err))
6860+ pr_err("debugfs/aufs failed\n");
6861+ return err;
6862+}
6863+
6864+/* ---------------------------------------------------------------------- */
6865+
6866+void dbgaufs_fin(void)
6867+{
6868+ debugfs_remove(dbgaufs);
6869+}
6870+
6871+int __init dbgaufs_init(void)
6872+{
6873+ int err;
6874+
6875+ err = -EIO;
6876+ dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
6877+ if (dbgaufs)
6878+ err = 0;
6879+ return err;
6880+}
6881diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
6882--- /usr/share/empty/fs/aufs/dbgaufs.h 1970-01-01 01:00:00.000000000 +0100
6883+++ linux/fs/aufs/dbgaufs.h 2021-02-24 13:33:42.741013619 +0100
6884@@ -0,0 +1,53 @@
6885+/* SPDX-License-Identifier: GPL-2.0 */
6886+/*
6887+ * Copyright (C) 2005-2020 Junjiro R. Okajima
6888+ *
6889+ * This program, aufs is free software; you can redistribute it and/or modify
6890+ * it under the terms of the GNU General Public License as published by
6891+ * the Free Software Foundation; either version 2 of the License, or
6892+ * (at your option) any later version.
6893+ *
6894+ * This program is distributed in the hope that it will be useful,
6895+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6896+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6897+ * GNU General Public License for more details.
6898+ *
6899+ * You should have received a copy of the GNU General Public License
6900+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
6901+ */
6902+
6903+/*
6904+ * debugfs interface
6905+ */
6906+
6907+#ifndef __DBGAUFS_H__
6908+#define __DBGAUFS_H__
6909+
6910+#ifdef __KERNEL__
6911+
6912+struct super_block;
6913+struct au_sbinfo;
6914+struct au_branch;
6915+
6916+#ifdef CONFIG_DEBUG_FS
6917+/* dbgaufs.c */
6918+void dbgaufs_xino_del(struct au_branch *br);
6919+void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
6920+void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown);
6921+void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
6922+int dbgaufs_si_init(struct au_sbinfo *sbinfo);
6923+void dbgaufs_fin(void);
6924+int __init dbgaufs_init(void);
6925+#else
6926+AuStubVoid(dbgaufs_xino_del, struct au_branch *br)
6927+AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
6928+AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex,
6929+ int topdown)
6930+AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
6931+AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
6932+AuStubVoid(dbgaufs_fin, void)
6933+AuStubInt0(__init dbgaufs_init, void)
6934+#endif /* CONFIG_DEBUG_FS */
6935+
6936+#endif /* __KERNEL__ */
6937+#endif /* __DBGAUFS_H__ */
6938diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
6939--- /usr/share/empty/fs/aufs/dcsub.c 1970-01-01 01:00:00.000000000 +0100
6940+++ linux/fs/aufs/dcsub.c 2021-02-24 13:33:42.741013619 +0100
6941@@ -0,0 +1,225 @@
6942+// SPDX-License-Identifier: GPL-2.0
6943+/*
6944+ * Copyright (C) 2005-2020 Junjiro R. Okajima
6945+ *
6946+ * This program, aufs is free software; you can redistribute it and/or modify
6947+ * it under the terms of the GNU General Public License as published by
6948+ * the Free Software Foundation; either version 2 of the License, or
6949+ * (at your option) any later version.
6950+ *
6951+ * This program is distributed in the hope that it will be useful,
6952+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6953+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6954+ * GNU General Public License for more details.
6955+ *
6956+ * You should have received a copy of the GNU General Public License
6957+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
6958+ */
6959+
6960+/*
6961+ * sub-routines for dentry cache
6962+ */
6963+
6964+#include "aufs.h"
6965+
6966+static void au_dpage_free(struct au_dpage *dpage)
6967+{
6968+ int i;
6969+ struct dentry **p;
6970+
6971+ p = dpage->dentries;
6972+ for (i = 0; i < dpage->ndentry; i++)
6973+ dput(*p++);
6974+ free_page((unsigned long)dpage->dentries);
6975+}
6976+
6977+int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
6978+{
6979+ int err;
6980+ void *p;
6981+
6982+ err = -ENOMEM;
6983+ dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
6984+ if (unlikely(!dpages->dpages))
6985+ goto out;
6986+
6987+ p = (void *)__get_free_page(gfp);
6988+ if (unlikely(!p))
6989+ goto out_dpages;
6990+
6991+ dpages->dpages[0].ndentry = 0;
6992+ dpages->dpages[0].dentries = p;
6993+ dpages->ndpage = 1;
6994+ return 0; /* success */
6995+
6996+out_dpages:
6997+ au_kfree_try_rcu(dpages->dpages);
6998+out:
6999+ return err;
7000+}
7001+
7002+void au_dpages_free(struct au_dcsub_pages *dpages)
7003+{
7004+ int i;
7005+ struct au_dpage *p;
7006+
7007+ p = dpages->dpages;
7008+ for (i = 0; i < dpages->ndpage; i++)
7009+ au_dpage_free(p++);
7010+ au_kfree_try_rcu(dpages->dpages);
7011+}
7012+
7013+static int au_dpages_append(struct au_dcsub_pages *dpages,
7014+ struct dentry *dentry, gfp_t gfp)
7015+{
7016+ int err, sz;
7017+ struct au_dpage *dpage;
7018+ void *p;
7019+
7020+ dpage = dpages->dpages + dpages->ndpage - 1;
7021+ sz = PAGE_SIZE / sizeof(dentry);
7022+ if (unlikely(dpage->ndentry >= sz)) {
7023+ AuLabel(new dpage);
7024+ err = -ENOMEM;
7025+ sz = dpages->ndpage * sizeof(*dpages->dpages);
7026+ p = au_kzrealloc(dpages->dpages, sz,
7027+ sz + sizeof(*dpages->dpages), gfp,
7028+ /*may_shrink*/0);
7029+ if (unlikely(!p))
7030+ goto out;
7031+
7032+ dpages->dpages = p;
7033+ dpage = dpages->dpages + dpages->ndpage;
7034+ p = (void *)__get_free_page(gfp);
7035+ if (unlikely(!p))
7036+ goto out;
7037+
7038+ dpage->ndentry = 0;
7039+ dpage->dentries = p;
7040+ dpages->ndpage++;
7041+ }
7042+
7043+ AuDebugOn(au_dcount(dentry) <= 0);
7044+ dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
7045+ return 0; /* success */
7046+
7047+out:
7048+ return err;
7049+}
7050+
7051+/* todo: BAD approach */
7052+/* copied from linux/fs/dcache.c */
7053+enum d_walk_ret {
7054+ D_WALK_CONTINUE,
7055+ D_WALK_QUIT,
7056+ D_WALK_NORETRY,
7057+ D_WALK_SKIP,
7058+};
7059+
7060+extern void d_walk(struct dentry *parent, void *data,
7061+ enum d_walk_ret (*enter)(void *, struct dentry *));
7062+
7063+struct ac_dpages_arg {
7064+ int err;
7065+ struct au_dcsub_pages *dpages;
7066+ struct super_block *sb;
7067+ au_dpages_test test;
7068+ void *arg;
7069+};
7070+
7071+static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
7072+{
7073+ enum d_walk_ret ret;
7074+ struct ac_dpages_arg *arg = _arg;
7075+
7076+ ret = D_WALK_CONTINUE;
7077+ if (dentry->d_sb == arg->sb
7078+ && !IS_ROOT(dentry)
7079+ && au_dcount(dentry) > 0
7080+ && au_di(dentry)
7081+ && (!arg->test || arg->test(dentry, arg->arg))) {
7082+ arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
7083+ if (unlikely(arg->err))
7084+ ret = D_WALK_QUIT;
7085+ }
7086+
7087+ return ret;
7088+}
7089+
7090+int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7091+ au_dpages_test test, void *arg)
7092+{
7093+ struct ac_dpages_arg args = {
7094+ .err = 0,
7095+ .dpages = dpages,
7096+ .sb = root->d_sb,
7097+ .test = test,
7098+ .arg = arg
7099+ };
7100+
7101+ d_walk(root, &args, au_call_dpages_append);
7102+
7103+ return args.err;
7104+}
7105+
7106+int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7107+ int do_include, au_dpages_test test, void *arg)
7108+{
7109+ int err;
7110+
7111+ err = 0;
7112+ write_seqlock(&rename_lock);
7113+ spin_lock(&dentry->d_lock);
7114+ if (do_include
7115+ && au_dcount(dentry) > 0
7116+ && (!test || test(dentry, arg)))
7117+ err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7118+ spin_unlock(&dentry->d_lock);
7119+ if (unlikely(err))
7120+ goto out;
7121+
7122+ /*
7123+ * RCU for vfsmount is unnecessary since this is a traverse in a single
7124+ * mount
7125+ */
7126+ while (!IS_ROOT(dentry)) {
7127+ dentry = dentry->d_parent; /* rename_lock is locked */
7128+ spin_lock(&dentry->d_lock);
7129+ if (au_dcount(dentry) > 0
7130+ && (!test || test(dentry, arg)))
7131+ err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7132+ spin_unlock(&dentry->d_lock);
7133+ if (unlikely(err))
7134+ break;
7135+ }
7136+
7137+out:
7138+ write_sequnlock(&rename_lock);
7139+ return err;
7140+}
7141+
7142+static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
7143+{
7144+ return au_di(dentry) && dentry->d_sb == arg;
7145+}
7146+
7147+int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7148+ struct dentry *dentry, int do_include)
7149+{
7150+ return au_dcsub_pages_rev(dpages, dentry, do_include,
7151+ au_dcsub_dpages_aufs, dentry->d_sb);
7152+}
7153+
7154+int au_test_subdir(struct dentry *d1, struct dentry *d2)
7155+{
7156+ struct path path[2] = {
7157+ {
7158+ .dentry = d1
7159+ },
7160+ {
7161+ .dentry = d2
7162+ }
7163+ };
7164+
7165+ return path_is_under(path + 0, path + 1);
7166+}
7167diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
7168--- /usr/share/empty/fs/aufs/dcsub.h 1970-01-01 01:00:00.000000000 +0100
7169+++ linux/fs/aufs/dcsub.h 2021-02-24 13:33:42.741013619 +0100
7170@@ -0,0 +1,137 @@
7171+/* SPDX-License-Identifier: GPL-2.0 */
7172+/*
7173+ * Copyright (C) 2005-2020 Junjiro R. Okajima
7174+ *
7175+ * This program, aufs is free software; you can redistribute it and/or modify
7176+ * it under the terms of the GNU General Public License as published by
7177+ * the Free Software Foundation; either version 2 of the License, or
7178+ * (at your option) any later version.
7179+ *
7180+ * This program is distributed in the hope that it will be useful,
7181+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7182+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7183+ * GNU General Public License for more details.
7184+ *
7185+ * You should have received a copy of the GNU General Public License
7186+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7187+ */
7188+
7189+/*
7190+ * sub-routines for dentry cache
7191+ */
7192+
7193+#ifndef __AUFS_DCSUB_H__
7194+#define __AUFS_DCSUB_H__
7195+
7196+#ifdef __KERNEL__
7197+
7198+#include <linux/dcache.h>
7199+#include <linux/fs.h>
7200+
7201+struct au_dpage {
7202+ int ndentry;
7203+ struct dentry **dentries;
7204+};
7205+
7206+struct au_dcsub_pages {
7207+ int ndpage;
7208+ struct au_dpage *dpages;
7209+};
7210+
7211+/* ---------------------------------------------------------------------- */
7212+
7213+/* dcsub.c */
7214+int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
7215+void au_dpages_free(struct au_dcsub_pages *dpages);
7216+typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
7217+int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7218+ au_dpages_test test, void *arg);
7219+int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7220+ int do_include, au_dpages_test test, void *arg);
7221+int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7222+ struct dentry *dentry, int do_include);
7223+int au_test_subdir(struct dentry *d1, struct dentry *d2);
7224+
7225+/* ---------------------------------------------------------------------- */
7226+
7227+/*
7228+ * todo: in linux-3.13, several similar (but faster) helpers are added to
7229+ * include/linux/dcache.h. Try them (in the future).
7230+ */
7231+
7232+static inline int au_d_hashed_positive(struct dentry *d)
7233+{
7234+ int err;
7235+ struct inode *inode = d_inode(d);
7236+
7237+ err = 0;
7238+ if (unlikely(d_unhashed(d)
7239+ || d_is_negative(d)
7240+ || !inode->i_nlink))
7241+ err = -ENOENT;
7242+ return err;
7243+}
7244+
7245+static inline int au_d_linkable(struct dentry *d)
7246+{
7247+ int err;
7248+ struct inode *inode = d_inode(d);
7249+
7250+ err = au_d_hashed_positive(d);
7251+ if (err
7252+ && d_is_positive(d)
7253+ && (inode->i_state & I_LINKABLE))
7254+ err = 0;
7255+ return err;
7256+}
7257+
7258+static inline int au_d_alive(struct dentry *d)
7259+{
7260+ int err;
7261+ struct inode *inode;
7262+
7263+ err = 0;
7264+ if (!IS_ROOT(d))
7265+ err = au_d_hashed_positive(d);
7266+ else {
7267+ inode = d_inode(d);
7268+ if (unlikely(d_unlinked(d)
7269+ || d_is_negative(d)
7270+ || !inode->i_nlink))
7271+ err = -ENOENT;
7272+ }
7273+ return err;
7274+}
7275+
7276+static inline int au_alive_dir(struct dentry *d)
7277+{
7278+ int err;
7279+
7280+ err = au_d_alive(d);
7281+ if (unlikely(err || IS_DEADDIR(d_inode(d))))
7282+ err = -ENOENT;
7283+ return err;
7284+}
7285+
7286+static inline int au_qstreq(struct qstr *a, struct qstr *b)
7287+{
7288+ return a->len == b->len
7289+ && !memcmp(a->name, b->name, a->len);
7290+}
7291+
7292+/*
7293+ * by the commit
7294+ * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
7295+ * taking d_lock
7296+ * the type of d_lockref.count became int, but the inlined function d_count()
7297+ * still returns unsigned int.
7298+ * I don't know why. Maybe it is for every d_count() users?
7299+ * Anyway au_dcount() lives on.
7300+ */
7301+static inline int au_dcount(struct dentry *d)
7302+{
7303+ return (int)d_count(d);
7304+}
7305+
7306+#endif /* __KERNEL__ */
7307+#endif /* __AUFS_DCSUB_H__ */
7308diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
7309--- /usr/share/empty/fs/aufs/debug.c 1970-01-01 01:00:00.000000000 +0100
7310+++ linux/fs/aufs/debug.c 2021-02-24 13:33:42.741013619 +0100
7311@@ -0,0 +1,441 @@
7312+// SPDX-License-Identifier: GPL-2.0
7313+/*
7314+ * Copyright (C) 2005-2020 Junjiro R. Okajima
7315+ *
7316+ * This program, aufs is free software; you can redistribute it and/or modify
7317+ * it under the terms of the GNU General Public License as published by
7318+ * the Free Software Foundation; either version 2 of the License, or
7319+ * (at your option) any later version.
7320+ *
7321+ * This program is distributed in the hope that it will be useful,
7322+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7323+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7324+ * GNU General Public License for more details.
7325+ *
7326+ * You should have received a copy of the GNU General Public License
7327+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7328+ */
7329+
7330+/*
7331+ * debug print functions
7332+ */
7333+
7334+#include <linux/iversion.h>
7335+#include "aufs.h"
7336+
7337+/* Returns 0, or -errno. arg is in kp->arg. */
7338+static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
7339+{
7340+ int err, n;
7341+
7342+ err = kstrtoint(val, 0, &n);
7343+ if (!err) {
7344+ if (n > 0)
7345+ au_debug_on();
7346+ else
7347+ au_debug_off();
7348+ }
7349+ return err;
7350+}
7351+
7352+/* Returns length written or -errno. Buffer is 4k (ie. be short!) */
7353+static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
7354+{
7355+ atomic_t *a;
7356+
7357+ a = kp->arg;
7358+ return sprintf(buffer, "%d", atomic_read(a));
7359+}
7360+
7361+static struct kernel_param_ops param_ops_atomic_t = {
7362+ .set = param_atomic_t_set,
7363+ .get = param_atomic_t_get
7364+ /* void (*free)(void *arg) */
7365+};
7366+
7367+atomic_t aufs_debug = ATOMIC_INIT(0);
7368+MODULE_PARM_DESC(debug, "debug print");
7369+module_param_named(debug, aufs_debug, atomic_t, 0664);
7370+
7371+DEFINE_MUTEX(au_dbg_mtx); /* just to serialize the dbg msgs */
7372+char *au_plevel = KERN_DEBUG;
7373+#define dpri(fmt, ...) do { \
7374+ if ((au_plevel \
7375+ && strcmp(au_plevel, KERN_DEBUG)) \
7376+ || au_debug_test()) \
7377+ printk("%s" fmt, au_plevel, ##__VA_ARGS__); \
7378+} while (0)
7379+
7380+/* ---------------------------------------------------------------------- */
7381+
7382+void au_dpri_whlist(struct au_nhash *whlist)
7383+{
7384+ unsigned long ul, n;
7385+ struct hlist_head *head;
7386+ struct au_vdir_wh *pos;
7387+
7388+ n = whlist->nh_num;
7389+ head = whlist->nh_head;
7390+ for (ul = 0; ul < n; ul++) {
7391+ hlist_for_each_entry(pos, head, wh_hash)
7392+ dpri("b%d, %.*s, %d\n",
7393+ pos->wh_bindex,
7394+ pos->wh_str.len, pos->wh_str.name,
7395+ pos->wh_str.len);
7396+ head++;
7397+ }
7398+}
7399+
7400+void au_dpri_vdir(struct au_vdir *vdir)
7401+{
7402+ unsigned long ul;
7403+ union au_vdir_deblk_p p;
7404+ unsigned char *o;
7405+
7406+ if (!vdir || IS_ERR(vdir)) {
7407+ dpri("err %ld\n", PTR_ERR(vdir));
7408+ return;
7409+ }
7410+
7411+ dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %llu\n",
7412+ vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
7413+ vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
7414+ for (ul = 0; ul < vdir->vd_nblk; ul++) {
7415+ p.deblk = vdir->vd_deblk[ul];
7416+ o = p.deblk;
7417+ dpri("[%lu]: %p\n", ul, o);
7418+ }
7419+}
7420+
7421+static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
7422+ struct dentry *wh)
7423+{
7424+ char *n = NULL;
7425+ int l = 0;
7426+
7427+ if (!inode || IS_ERR(inode)) {
7428+ dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7429+ return -1;
7430+ }
7431+
7432+ /* the type of i_blocks depends upon CONFIG_LBDAF */
7433+ BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7434+ && sizeof(inode->i_blocks) != sizeof(u64));
7435+ if (wh) {
7436+ n = (void *)wh->d_name.name;
7437+ l = wh->d_name.len;
7438+ }
7439+
7440+ dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
7441+ " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7442+ bindex, inode,
7443+ inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7444+ atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7445+ i_size_read(inode), (unsigned long long)inode->i_blocks,
7446+ hn, (long long)timespec64_to_ns(&inode->i_ctime) & 0x0ffff,
7447+ inode->i_mapping ? inode->i_mapping->nrpages : 0,
7448+ inode->i_state, inode->i_flags, inode_peek_iversion(inode),
7449+ inode->i_generation,
7450+ l ? ", wh " : "", l, n);
7451+ return 0;
7452+}
7453+
7454+void au_dpri_inode(struct inode *inode)
7455+{
7456+ struct au_iinfo *iinfo;
7457+ struct au_hinode *hi;
7458+ aufs_bindex_t bindex;
7459+ int err, hn;
7460+
7461+ err = do_pri_inode(-1, inode, -1, NULL);
7462+ if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
7463+ return;
7464+
7465+ iinfo = au_ii(inode);
7466+ dpri("i-1: btop %d, bbot %d, gen %d\n",
7467+ iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
7468+ if (iinfo->ii_btop < 0)
7469+ return;
7470+ hn = 0;
7471+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
7472+ hi = au_hinode(iinfo, bindex);
7473+ hn = !!au_hn(hi);
7474+ do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
7475+ }
7476+}
7477+
7478+void au_dpri_dalias(struct inode *inode)
7479+{
7480+ struct dentry *d;
7481+
7482+ spin_lock(&inode->i_lock);
7483+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
7484+ au_dpri_dentry(d);
7485+ spin_unlock(&inode->i_lock);
7486+}
7487+
7488+static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7489+{
7490+ struct dentry *wh = NULL;
7491+ int hn;
7492+ struct inode *inode;
7493+ struct au_iinfo *iinfo;
7494+ struct au_hinode *hi;
7495+
7496+ if (!dentry || IS_ERR(dentry)) {
7497+ dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7498+ return -1;
7499+ }
7500+ /* do not call dget_parent() here */
7501+ /* note: access d_xxx without d_lock */
7502+ dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7503+ bindex, dentry, dentry,
7504+ dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
7505+ au_dcount(dentry), dentry->d_flags,
7506+ d_unhashed(dentry) ? "un" : "");
7507+ hn = -1;
7508+ inode = NULL;
7509+ if (d_is_positive(dentry))
7510+ inode = d_inode(dentry);
7511+ if (inode
7512+ && au_test_aufs(dentry->d_sb)
7513+ && bindex >= 0
7514+ && !au_is_bad_inode(inode)) {
7515+ iinfo = au_ii(inode);
7516+ hi = au_hinode(iinfo, bindex);
7517+ hn = !!au_hn(hi);
7518+ wh = hi->hi_whdentry;
7519+ }
7520+ do_pri_inode(bindex, inode, hn, wh);
7521+ return 0;
7522+}
7523+
7524+void au_dpri_dentry(struct dentry *dentry)
7525+{
7526+ struct au_dinfo *dinfo;
7527+ aufs_bindex_t bindex;
7528+ int err;
7529+
7530+ err = do_pri_dentry(-1, dentry);
7531+ if (err || !au_test_aufs(dentry->d_sb))
7532+ return;
7533+
7534+ dinfo = au_di(dentry);
7535+ if (!dinfo)
7536+ return;
7537+ dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7538+ dinfo->di_btop, dinfo->di_bbot,
7539+ dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7540+ dinfo->di_tmpfile);
7541+ if (dinfo->di_btop < 0)
7542+ return;
7543+ for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
7544+ do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
7545+}
7546+
7547+static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7548+{
7549+ char a[32];
7550+
7551+ if (!file || IS_ERR(file)) {
7552+ dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7553+ return -1;
7554+ }
7555+ a[0] = 0;
7556+ if (bindex < 0
7557+ && !IS_ERR_OR_NULL(file->f_path.dentry)
7558+ && au_test_aufs(file->f_path.dentry->d_sb)
7559+ && au_fi(file))
7560+ snprintf(a, sizeof(a), ", gen %d, mmapped %d",
7561+ au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
7562+ dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
7563+ bindex, file->f_mode, file->f_flags, (long)file_count(file),
7564+ file->f_version, file->f_pos, a);
7565+ if (!IS_ERR_OR_NULL(file->f_path.dentry))
7566+ do_pri_dentry(bindex, file->f_path.dentry);
7567+ return 0;
7568+}
7569+
7570+void au_dpri_file(struct file *file)
7571+{
7572+ struct au_finfo *finfo;
7573+ struct au_fidir *fidir;
7574+ struct au_hfile *hfile;
7575+ aufs_bindex_t bindex;
7576+ int err;
7577+
7578+ err = do_pri_file(-1, file);
7579+ if (err
7580+ || IS_ERR_OR_NULL(file->f_path.dentry)
7581+ || !au_test_aufs(file->f_path.dentry->d_sb))
7582+ return;
7583+
7584+ finfo = au_fi(file);
7585+ if (!finfo)
7586+ return;
7587+ if (finfo->fi_btop < 0)
7588+ return;
7589+ fidir = finfo->fi_hdir;
7590+ if (!fidir)
7591+ do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7592+ else
7593+ for (bindex = finfo->fi_btop;
7594+ bindex >= 0 && bindex <= fidir->fd_bbot;
7595+ bindex++) {
7596+ hfile = fidir->fd_hfile + bindex;
7597+ do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7598+ }
7599+}
7600+
7601+static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7602+{
7603+ struct vfsmount *mnt;
7604+ struct super_block *sb;
7605+
7606+ if (!br || IS_ERR(br))
7607+ goto out;
7608+ mnt = au_br_mnt(br);
7609+ if (!mnt || IS_ERR(mnt))
7610+ goto out;
7611+ sb = mnt->mnt_sb;
7612+ if (!sb || IS_ERR(sb))
7613+ goto out;
7614+
7615+ dpri("s%d: {perm 0x%x, id %d, wbr %p}, "
7616+ "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
7617+ "xino %d\n",
7618+ bindex, br->br_perm, br->br_id, br->br_wbr,
7619+ au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
7620+ sb->s_flags, sb->s_count,
7621+ atomic_read(&sb->s_active),
7622+ !!au_xino_file(br->br_xino, /*idx*/-1));
7623+ return 0;
7624+
7625+out:
7626+ dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7627+ return -1;
7628+}
7629+
7630+void au_dpri_sb(struct super_block *sb)
7631+{
7632+ struct au_sbinfo *sbinfo;
7633+ aufs_bindex_t bindex;
7634+ int err;
7635+ /* to reduce stack size */
7636+ struct {
7637+ struct vfsmount mnt;
7638+ struct au_branch fake;
7639+ } *a;
7640+
7641+ /* this function can be called from magic sysrq */
7642+ a = kzalloc(sizeof(*a), GFP_ATOMIC);
7643+ if (unlikely(!a)) {
7644+ dpri("no memory\n");
7645+ return;
7646+ }
7647+
7648+ a->mnt.mnt_sb = sb;
7649+ a->fake.br_path.mnt = &a->mnt;
7650+ err = do_pri_br(-1, &a->fake);
7651+ au_kfree_rcu(a);
7652+ dpri("dev 0x%x\n", sb->s_dev);
7653+ if (err || !au_test_aufs(sb))
7654+ return;
7655+
7656+ sbinfo = au_sbi(sb);
7657+ if (!sbinfo)
7658+ return;
7659+ dpri("nw %d, gen %u, kobj %d\n",
7660+ atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
7661+ kref_read(&sbinfo->si_kobj.kref));
7662+ for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
7663+ do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7664+}
7665+
7666+/* ---------------------------------------------------------------------- */
7667+
7668+void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7669+{
7670+ struct inode *h_inode, *inode = d_inode(dentry);
7671+ struct dentry *h_dentry;
7672+ aufs_bindex_t bindex, bbot, bi;
7673+
7674+ if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7675+ return;
7676+
7677+ bbot = au_dbbot(dentry);
7678+ bi = au_ibbot(inode);
7679+ if (bi < bbot)
7680+ bbot = bi;
7681+ bindex = au_dbtop(dentry);
7682+ bi = au_ibtop(inode);
7683+ if (bi > bindex)
7684+ bindex = bi;
7685+
7686+ for (; bindex <= bbot; bindex++) {
7687+ h_dentry = au_h_dptr(dentry, bindex);
7688+ if (!h_dentry)
7689+ continue;
7690+ h_inode = au_h_iptr(inode, bindex);
7691+ if (unlikely(h_inode != d_inode(h_dentry))) {
7692+ au_debug_on();
7693+ AuDbg("b%d, %s:%d\n", bindex, func, line);
7694+ AuDbgDentry(dentry);
7695+ AuDbgInode(inode);
7696+ au_debug_off();
7697+ BUG();
7698+ }
7699+ }
7700+}
7701+
7702+void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7703+{
7704+ int err, i, j;
7705+ struct au_dcsub_pages dpages;
7706+ struct au_dpage *dpage;
7707+ struct dentry **dentries;
7708+
7709+ err = au_dpages_init(&dpages, GFP_NOFS);
7710+ AuDebugOn(err);
7711+ err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
7712+ AuDebugOn(err);
7713+ for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7714+ dpage = dpages.dpages + i;
7715+ dentries = dpage->dentries;
7716+ for (j = dpage->ndentry - 1; !err && j >= 0; j--)
7717+ AuDebugOn(au_digen_test(dentries[j], sigen));
7718+ }
7719+ au_dpages_free(&dpages);
7720+}
7721+
7722+void au_dbg_verify_kthread(void)
7723+{
7724+ if (au_wkq_test()) {
7725+ au_dbg_blocked();
7726+ /*
7727+ * It may be recursive, but udba=notify between two aufs mounts,
7728+ * where a single ro branch is shared, is not a problem.
7729+ */
7730+ /* WARN_ON(1); */
7731+ }
7732+}
7733+
7734+/* ---------------------------------------------------------------------- */
7735+
7736+int __init au_debug_init(void)
7737+{
7738+ aufs_bindex_t bindex;
7739+ struct au_vdir_destr destr;
7740+
7741+ bindex = -1;
7742+ AuDebugOn(bindex >= 0);
7743+
7744+ destr.len = -1;
7745+ AuDebugOn(destr.len < NAME_MAX);
7746+
7747+#ifdef CONFIG_4KSTACKS
7748+ pr_warn("CONFIG_4KSTACKS is defined.\n");
7749+#endif
7750+
7751+ return 0;
7752+}
7753diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
7754--- /usr/share/empty/fs/aufs/debug.h 1970-01-01 01:00:00.000000000 +0100
7755+++ linux/fs/aufs/debug.h 2021-02-24 13:33:42.741013619 +0100
7756@@ -0,0 +1,226 @@
7757+/* SPDX-License-Identifier: GPL-2.0 */
7758+/*
7759+ * Copyright (C) 2005-2020 Junjiro R. Okajima
7760+ *
7761+ * This program, aufs is free software; you can redistribute it and/or modify
7762+ * it under the terms of the GNU General Public License as published by
7763+ * the Free Software Foundation; either version 2 of the License, or
7764+ * (at your option) any later version.
7765+ *
7766+ * This program is distributed in the hope that it will be useful,
7767+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7768+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7769+ * GNU General Public License for more details.
7770+ *
7771+ * You should have received a copy of the GNU General Public License
7772+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7773+ */
7774+
7775+/*
7776+ * debug print functions
7777+ */
7778+
7779+#ifndef __AUFS_DEBUG_H__
7780+#define __AUFS_DEBUG_H__
7781+
7782+#ifdef __KERNEL__
7783+
7784+#include <linux/atomic.h>
7785+#include <linux/module.h>
7786+#include <linux/kallsyms.h>
7787+#include <linux/sysrq.h>
7788+
7789+#ifdef CONFIG_AUFS_DEBUG
7790+#define AuDebugOn(a) BUG_ON(a)
7791+
7792+/* module parameter */
7793+extern atomic_t aufs_debug;
7794+static inline void au_debug_on(void)
7795+{
7796+ atomic_inc(&aufs_debug);
7797+}
7798+static inline void au_debug_off(void)
7799+{
7800+ atomic_dec_if_positive(&aufs_debug);
7801+}
7802+
7803+static inline int au_debug_test(void)
7804+{
7805+ return atomic_read(&aufs_debug) > 0;
7806+}
7807+#else
7808+#define AuDebugOn(a) do {} while (0)
7809+AuStubVoid(au_debug_on, void)
7810+AuStubVoid(au_debug_off, void)
7811+AuStubInt0(au_debug_test, void)
7812+#endif /* CONFIG_AUFS_DEBUG */
7813+
7814+#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7815+
7816+/* ---------------------------------------------------------------------- */
7817+
7818+/* debug print */
7819+
7820+#define AuDbg(fmt, ...) do { \
7821+ if (au_debug_test()) \
7822+ pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
7823+} while (0)
7824+#define AuLabel(l) AuDbg(#l "\n")
7825+#define AuIOErr(fmt, ...) pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7826+#define AuWarn1(fmt, ...) do { \
7827+ static unsigned char _c; \
7828+ if (!_c++) \
7829+ pr_warn(fmt, ##__VA_ARGS__); \
7830+} while (0)
7831+
7832+#define AuErr1(fmt, ...) do { \
7833+ static unsigned char _c; \
7834+ if (!_c++) \
7835+ pr_err(fmt, ##__VA_ARGS__); \
7836+} while (0)
7837+
7838+#define AuIOErr1(fmt, ...) do { \
7839+ static unsigned char _c; \
7840+ if (!_c++) \
7841+ AuIOErr(fmt, ##__VA_ARGS__); \
7842+} while (0)
7843+
7844+#define AuUnsupportMsg "This operation is not supported." \
7845+ " Please report this application to aufs-users ML."
7846+#define AuUnsupport(fmt, ...) do { \
7847+ pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
7848+ dump_stack(); \
7849+} while (0)
7850+
7851+#define AuTraceErr(e) do { \
7852+ if (unlikely((e) < 0)) \
7853+ AuDbg("err %d\n", (int)(e)); \
7854+} while (0)
7855+
7856+#define AuTraceErrPtr(p) do { \
7857+ if (IS_ERR(p)) \
7858+ AuDbg("err %ld\n", PTR_ERR(p)); \
7859+} while (0)
7860+
7861+/* dirty macros for debug print, use with "%.*s" and caution */
7862+#define AuLNPair(qstr) (qstr)->len, (qstr)->name
7863+
7864+/* ---------------------------------------------------------------------- */
7865+
7866+struct dentry;
7867+#ifdef CONFIG_AUFS_DEBUG
7868+extern struct mutex au_dbg_mtx;
7869+extern char *au_plevel;
7870+struct au_nhash;
7871+void au_dpri_whlist(struct au_nhash *whlist);
7872+struct au_vdir;
7873+void au_dpri_vdir(struct au_vdir *vdir);
7874+struct inode;
7875+void au_dpri_inode(struct inode *inode);
7876+void au_dpri_dalias(struct inode *inode);
7877+void au_dpri_dentry(struct dentry *dentry);
7878+struct file;
7879+void au_dpri_file(struct file *filp);
7880+struct super_block;
7881+void au_dpri_sb(struct super_block *sb);
7882+
7883+#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
7884+void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
7885+void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
7886+void au_dbg_verify_kthread(void);
7887+
7888+int __init au_debug_init(void);
7889+
7890+#define AuDbgWhlist(w) do { \
7891+ mutex_lock(&au_dbg_mtx); \
7892+ AuDbg(#w "\n"); \
7893+ au_dpri_whlist(w); \
7894+ mutex_unlock(&au_dbg_mtx); \
7895+} while (0)
7896+
7897+#define AuDbgVdir(v) do { \
7898+ mutex_lock(&au_dbg_mtx); \
7899+ AuDbg(#v "\n"); \
7900+ au_dpri_vdir(v); \
7901+ mutex_unlock(&au_dbg_mtx); \
7902+} while (0)
7903+
7904+#define AuDbgInode(i) do { \
7905+ mutex_lock(&au_dbg_mtx); \
7906+ AuDbg(#i "\n"); \
7907+ au_dpri_inode(i); \
7908+ mutex_unlock(&au_dbg_mtx); \
7909+} while (0)
7910+
7911+#define AuDbgDAlias(i) do { \
7912+ mutex_lock(&au_dbg_mtx); \
7913+ AuDbg(#i "\n"); \
7914+ au_dpri_dalias(i); \
7915+ mutex_unlock(&au_dbg_mtx); \
7916+} while (0)
7917+
7918+#define AuDbgDentry(d) do { \
7919+ mutex_lock(&au_dbg_mtx); \
7920+ AuDbg(#d "\n"); \
7921+ au_dpri_dentry(d); \
7922+ mutex_unlock(&au_dbg_mtx); \
7923+} while (0)
7924+
7925+#define AuDbgFile(f) do { \
7926+ mutex_lock(&au_dbg_mtx); \
7927+ AuDbg(#f "\n"); \
7928+ au_dpri_file(f); \
7929+ mutex_unlock(&au_dbg_mtx); \
7930+} while (0)
7931+
7932+#define AuDbgSb(sb) do { \
7933+ mutex_lock(&au_dbg_mtx); \
7934+ AuDbg(#sb "\n"); \
7935+ au_dpri_sb(sb); \
7936+ mutex_unlock(&au_dbg_mtx); \
7937+} while (0)
7938+
7939+#define AuDbgSym(addr) do { \
7940+ char sym[KSYM_SYMBOL_LEN]; \
7941+ sprint_symbol(sym, (unsigned long)addr); \
7942+ AuDbg("%s\n", sym); \
7943+} while (0)
7944+#else
7945+AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
7946+AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
7947+AuStubVoid(au_dbg_verify_kthread, void)
7948+AuStubInt0(__init au_debug_init, void)
7949+
7950+#define AuDbgWhlist(w) do {} while (0)
7951+#define AuDbgVdir(v) do {} while (0)
7952+#define AuDbgInode(i) do {} while (0)
7953+#define AuDbgDAlias(i) do {} while (0)
7954+#define AuDbgDentry(d) do {} while (0)
7955+#define AuDbgFile(f) do {} while (0)
7956+#define AuDbgSb(sb) do {} while (0)
7957+#define AuDbgSym(addr) do {} while (0)
7958+#endif /* CONFIG_AUFS_DEBUG */
7959+
7960+/* ---------------------------------------------------------------------- */
7961+
7962+#ifdef CONFIG_AUFS_MAGIC_SYSRQ
7963+int __init au_sysrq_init(void);
7964+void au_sysrq_fin(void);
7965+
7966+#ifdef CONFIG_HW_CONSOLE
7967+#define au_dbg_blocked() do { \
7968+ WARN_ON(1); \
7969+ handle_sysrq('w'); \
7970+} while (0)
7971+#else
7972+AuStubVoid(au_dbg_blocked, void)
7973+#endif
7974+
7975+#else
7976+AuStubInt0(__init au_sysrq_init, void)
7977+AuStubVoid(au_sysrq_fin, void)
7978+AuStubVoid(au_dbg_blocked, void)
7979+#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
7980+
7981+#endif /* __KERNEL__ */
7982+#endif /* __AUFS_DEBUG_H__ */
7983diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
7984--- /usr/share/empty/fs/aufs/dentry.c 1970-01-01 01:00:00.000000000 +0100
7985+++ linux/fs/aufs/dentry.c 2021-06-30 21:35:11.393873211 +0200
7986@@ -0,0 +1,1160 @@
7987+// SPDX-License-Identifier: GPL-2.0
7988+/*
7989+ * Copyright (C) 2005-2020 Junjiro R. Okajima
7990+ *
7991+ * This program, aufs is free software; you can redistribute it and/or modify
7992+ * it under the terms of the GNU General Public License as published by
7993+ * the Free Software Foundation; either version 2 of the License, or
7994+ * (at your option) any later version.
7995+ *
7996+ * This program is distributed in the hope that it will be useful,
7997+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7998+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7999+ * GNU General Public License for more details.
8000+ *
8001+ * You should have received a copy of the GNU General Public License
8002+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
8003+ */
8004+
8005+/*
8006+ * lookup and dentry operations
8007+ */
8008+
8009+#include <linux/iversion.h>
8010+#include <linux/namei.h>
8011+#include "aufs.h"
8012+
8013+/*
8014+ * returns positive/negative dentry, NULL or an error.
8015+ * NULL means whiteout-ed or not-found.
8016+ */
8017+static struct dentry*
8018+au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
8019+ aufs_bindex_t bindex, struct au_do_lookup_args *args)
8020+{
8021+ struct dentry *h_dentry;
8022+ struct inode *h_inode;
8023+ struct au_branch *br;
8024+ struct user_namespace *h_userns;
8025+ int wh_found, opq;
8026+ unsigned char wh_able;
8027+ const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
8028+ const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
8029+ IGNORE_PERM);
8030+
8031+ wh_found = 0;
8032+ br = au_sbr(dentry->d_sb, bindex);
8033+ h_userns = au_br_userns(br);
8034+ wh_able = !!au_br_whable(br->br_perm);
8035+ if (wh_able)
8036+ wh_found = au_wh_test(h_userns, h_parent, &args->whname,
8037+ ignore_perm);
8038+ h_dentry = ERR_PTR(wh_found);
8039+ if (!wh_found)
8040+ goto real_lookup;
8041+ if (unlikely(wh_found < 0))
8042+ goto out;
8043+
8044+ /* We found a whiteout */
8045+ /* au_set_dbbot(dentry, bindex); */
8046+ au_set_dbwh(dentry, bindex);
8047+ if (!allow_neg)
8048+ return NULL; /* success */
8049+
8050+real_lookup:
8051+ if (!ignore_perm)
8052+ h_dentry = vfsub_lkup_one(args->name, h_parent);
8053+ else
8054+ h_dentry = au_sio_lkup_one(h_userns, args->name, h_parent);
8055+ if (IS_ERR(h_dentry)) {
8056+ if (PTR_ERR(h_dentry) == -ENAMETOOLONG
8057+ && !allow_neg)
8058+ h_dentry = NULL;
8059+ goto out;
8060+ }
8061+
8062+ h_inode = d_inode(h_dentry);
8063+ if (d_is_negative(h_dentry)) {
8064+ if (!allow_neg)
8065+ goto out_neg;
8066+ } else if (wh_found
8067+ || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
8068+ goto out_neg;
8069+ else if (au_ftest_lkup(args->flags, DIRREN)
8070+ /* && h_inode */
8071+ && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
8072+ AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
8073+ (unsigned long long)h_inode->i_ino);
8074+ goto out_neg;
8075+ }
8076+
8077+ if (au_dbbot(dentry) <= bindex)
8078+ au_set_dbbot(dentry, bindex);
8079+ if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
8080+ au_set_dbtop(dentry, bindex);
8081+ au_set_h_dptr(dentry, bindex, h_dentry);
8082+
8083+ if (!d_is_dir(h_dentry)
8084+ || !wh_able
8085+ || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
8086+ goto out; /* success */
8087+
8088+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
8089+ opq = au_diropq_test(h_userns, h_dentry);
8090+ inode_unlock_shared(h_inode);
8091+ if (opq > 0)
8092+ au_set_dbdiropq(dentry, bindex);
8093+ else if (unlikely(opq < 0)) {
8094+ au_set_h_dptr(dentry, bindex, NULL);
8095+ h_dentry = ERR_PTR(opq);
8096+ }
8097+ goto out;
8098+
8099+out_neg:
8100+ dput(h_dentry);
8101+ h_dentry = NULL;
8102+out:
8103+ return h_dentry;
8104+}
8105+
8106+static int au_test_shwh(struct super_block *sb, const struct qstr *name)
8107+{
8108+ if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
8109+ && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
8110+ return -EPERM;
8111+ return 0;
8112+}
8113+
8114+/*
8115+ * returns the number of lower positive dentries,
8116+ * otherwise an error.
8117+ * can be called at unlinking with @type is zero.
8118+ */
8119+int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
8120+ unsigned int flags)
8121+{
8122+ int npositive, err;
8123+ aufs_bindex_t bindex, btail, bdiropq;
8124+ unsigned char isdir, dirperm1, dirren;
8125+ struct au_do_lookup_args args = {
8126+ .flags = flags,
8127+ .name = &dentry->d_name
8128+ };
8129+ struct dentry *parent;
8130+ struct super_block *sb;
8131+
8132+ sb = dentry->d_sb;
8133+ err = au_test_shwh(sb, args.name);
8134+ if (unlikely(err))
8135+ goto out;
8136+
8137+ err = au_wh_name_alloc(&args.whname, args.name);
8138+ if (unlikely(err))
8139+ goto out;
8140+
8141+ isdir = !!d_is_dir(dentry);
8142+ dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
8143+ dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
8144+ if (dirren)
8145+ au_fset_lkup(args.flags, DIRREN);
8146+
8147+ npositive = 0;
8148+ parent = dget_parent(dentry);
8149+ btail = au_dbtaildir(parent);
8150+ for (bindex = btop; bindex <= btail; bindex++) {
8151+ struct dentry *h_parent, *h_dentry;
8152+ struct inode *h_inode, *h_dir;
8153+ struct au_branch *br;
8154+
8155+ h_dentry = au_h_dptr(dentry, bindex);
8156+ if (h_dentry) {
8157+ if (d_is_positive(h_dentry))
8158+ npositive++;
8159+ break;
8160+ }
8161+ h_parent = au_h_dptr(parent, bindex);
8162+ if (!h_parent || !d_is_dir(h_parent))
8163+ continue;
8164+
8165+ if (dirren) {
8166+ /* if the inum matches, then use the prepared name */
8167+ err = au_dr_lkup_name(&args, bindex);
8168+ if (unlikely(err))
8169+ goto out_parent;
8170+ }
8171+
8172+ h_dir = d_inode(h_parent);
8173+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8174+ h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
8175+ inode_unlock_shared(h_dir);
8176+ err = PTR_ERR(h_dentry);
8177+ if (IS_ERR(h_dentry))
8178+ goto out_parent;
8179+ if (h_dentry)
8180+ au_fclr_lkup(args.flags, ALLOW_NEG);
8181+ if (dirperm1)
8182+ au_fset_lkup(args.flags, IGNORE_PERM);
8183+
8184+ if (au_dbwh(dentry) == bindex)
8185+ break;
8186+ if (!h_dentry)
8187+ continue;
8188+ if (d_is_negative(h_dentry))
8189+ continue;
8190+ h_inode = d_inode(h_dentry);
8191+ npositive++;
8192+ if (!args.type)
8193+ args.type = h_inode->i_mode & S_IFMT;
8194+ if (args.type != S_IFDIR)
8195+ break;
8196+ else if (isdir) {
8197+ /* the type of lower may be different */
8198+ bdiropq = au_dbdiropq(dentry);
8199+ if (bdiropq >= 0 && bdiropq <= bindex)
8200+ break;
8201+ }
8202+ br = au_sbr(sb, bindex);
8203+ if (dirren
8204+ && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
8205+ /*add_ent*/NULL)) {
8206+ /* prepare next name to lookup */
8207+ err = au_dr_lkup(&args, dentry, bindex);
8208+ if (unlikely(err))
8209+ goto out_parent;
8210+ }
8211+ }
8212+
8213+ if (npositive) {
8214+ AuLabel(positive);
8215+ au_update_dbtop(dentry);
8216+ }
8217+ err = npositive;
8218+ if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
8219+ && au_dbtop(dentry) < 0)) {
8220+ err = -EIO;
8221+ AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
8222+ dentry, err);
8223+ }
8224+
8225+out_parent:
8226+ dput(parent);
8227+ au_kfree_try_rcu(args.whname.name);
8228+ if (dirren)
8229+ au_dr_lkup_fin(&args);
8230+out:
8231+ return err;
8232+}
8233+
8234+struct dentry *au_sio_lkup_one(struct user_namespace *userns, struct qstr *name,
8235+ struct dentry *parent)
8236+{
8237+ struct dentry *dentry;
8238+ int wkq_err;
8239+
8240+ if (!au_test_h_perm_sio(userns, d_inode(parent), MAY_EXEC))
8241+ dentry = vfsub_lkup_one(name, parent);
8242+ else {
8243+ struct vfsub_lkup_one_args args = {
8244+ .errp = &dentry,
8245+ .name = name,
8246+ .parent = parent
8247+ };
8248+
8249+ wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
8250+ if (unlikely(wkq_err))
8251+ dentry = ERR_PTR(wkq_err);
8252+ }
8253+
8254+ return dentry;
8255+}
8256+
8257+/*
8258+ * lookup @dentry on @bindex which should be negative.
8259+ */
8260+int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
8261+{
8262+ int err;
8263+ struct dentry *parent, *h_parent, *h_dentry;
8264+ struct au_branch *br;
8265+ struct user_namespace *h_userns;
8266+
8267+ parent = dget_parent(dentry);
8268+ h_parent = au_h_dptr(parent, bindex);
8269+ br = au_sbr(dentry->d_sb, bindex);
8270+ h_userns = au_br_userns(br);
8271+ if (wh)
8272+ h_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
8273+ else
8274+ h_dentry = au_sio_lkup_one(h_userns, &dentry->d_name, h_parent);
8275+ err = PTR_ERR(h_dentry);
8276+ if (IS_ERR(h_dentry))
8277+ goto out;
8278+ if (unlikely(d_is_positive(h_dentry))) {
8279+ err = -EIO;
8280+ AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
8281+ dput(h_dentry);
8282+ goto out;
8283+ }
8284+
8285+ err = 0;
8286+ if (bindex < au_dbtop(dentry))
8287+ au_set_dbtop(dentry, bindex);
8288+ if (au_dbbot(dentry) < bindex)
8289+ au_set_dbbot(dentry, bindex);
8290+ au_set_h_dptr(dentry, bindex, h_dentry);
8291+
8292+out:
8293+ dput(parent);
8294+ return err;
8295+}
8296+
8297+/* ---------------------------------------------------------------------- */
8298+
8299+/* subset of struct inode */
8300+struct au_iattr {
8301+ unsigned long i_ino;
8302+ /* unsigned int i_nlink; */
8303+ kuid_t i_uid;
8304+ kgid_t i_gid;
8305+ u64 i_version;
8306+/*
8307+ loff_t i_size;
8308+ blkcnt_t i_blocks;
8309+*/
8310+ umode_t i_mode;
8311+};
8312+
8313+static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
8314+{
8315+ ia->i_ino = h_inode->i_ino;
8316+ /* ia->i_nlink = h_inode->i_nlink; */
8317+ ia->i_uid = h_inode->i_uid;
8318+ ia->i_gid = h_inode->i_gid;
8319+ ia->i_version = inode_query_iversion(h_inode);
8320+/*
8321+ ia->i_size = h_inode->i_size;
8322+ ia->i_blocks = h_inode->i_blocks;
8323+*/
8324+ ia->i_mode = (h_inode->i_mode & S_IFMT);
8325+}
8326+
8327+static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
8328+{
8329+ return ia->i_ino != h_inode->i_ino
8330+ /* || ia->i_nlink != h_inode->i_nlink */
8331+ || !uid_eq(ia->i_uid, h_inode->i_uid)
8332+ || !gid_eq(ia->i_gid, h_inode->i_gid)
8333+ || !inode_eq_iversion(h_inode, ia->i_version)
8334+/*
8335+ || ia->i_size != h_inode->i_size
8336+ || ia->i_blocks != h_inode->i_blocks
8337+*/
8338+ || ia->i_mode != (h_inode->i_mode & S_IFMT);
8339+}
8340+
8341+static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
8342+ struct au_branch *br)
8343+{
8344+ int err;
8345+ struct au_iattr ia;
8346+ struct inode *h_inode;
8347+ struct dentry *h_d;
8348+ struct super_block *h_sb;
8349+
8350+ err = 0;
8351+ memset(&ia, -1, sizeof(ia));
8352+ h_sb = h_dentry->d_sb;
8353+ h_inode = NULL;
8354+ if (d_is_positive(h_dentry)) {
8355+ h_inode = d_inode(h_dentry);
8356+ au_iattr_save(&ia, h_inode);
8357+ } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
8358+ /* nfs d_revalidate may return 0 for negative dentry */
8359+ /* fuse d_revalidate always return 0 for negative dentry */
8360+ goto out;
8361+
8362+ /* main purpose is namei.c:cached_lookup() and d_revalidate */
8363+ h_d = vfsub_lkup_one(&h_dentry->d_name, h_parent);
8364+ err = PTR_ERR(h_d);
8365+ if (IS_ERR(h_d))
8366+ goto out;
8367+
8368+ err = 0;
8369+ if (unlikely(h_d != h_dentry
8370+ || d_inode(h_d) != h_inode
8371+ || (h_inode && au_iattr_test(&ia, h_inode))))
8372+ err = au_busy_or_stale();
8373+ dput(h_d);
8374+
8375+out:
8376+ AuTraceErr(err);
8377+ return err;
8378+}
8379+
8380+int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8381+ struct dentry *h_parent, struct au_branch *br)
8382+{
8383+ int err;
8384+
8385+ err = 0;
8386+ if (udba == AuOpt_UDBA_REVAL
8387+ && !au_test_fs_remote(h_dentry->d_sb)) {
8388+ IMustLock(h_dir);
8389+ err = (d_inode(h_dentry->d_parent) != h_dir);
8390+ } else if (udba != AuOpt_UDBA_NONE)
8391+ err = au_h_verify_dentry(h_dentry, h_parent, br);
8392+
8393+ return err;
8394+}
8395+
8396+/* ---------------------------------------------------------------------- */
8397+
8398+static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
8399+{
8400+ int err;
8401+ aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
8402+ struct au_hdentry tmp, *p, *q;
8403+ struct au_dinfo *dinfo;
8404+ struct super_block *sb;
8405+
8406+ DiMustWriteLock(dentry);
8407+
8408+ sb = dentry->d_sb;
8409+ dinfo = au_di(dentry);
8410+ bbot = dinfo->di_bbot;
8411+ bwh = dinfo->di_bwh;
8412+ bdiropq = dinfo->di_bdiropq;
8413+ bindex = dinfo->di_btop;
8414+ p = au_hdentry(dinfo, bindex);
8415+ for (; bindex <= bbot; bindex++, p++) {
8416+ if (!p->hd_dentry)
8417+ continue;
8418+
8419+ new_bindex = au_br_index(sb, p->hd_id);
8420+ if (new_bindex == bindex)
8421+ continue;
8422+
8423+ if (dinfo->di_bwh == bindex)
8424+ bwh = new_bindex;
8425+ if (dinfo->di_bdiropq == bindex)
8426+ bdiropq = new_bindex;
8427+ if (new_bindex < 0) {
8428+ au_hdput(p);
8429+ p->hd_dentry = NULL;
8430+ continue;
8431+ }
8432+
8433+ /* swap two lower dentries, and loop again */
8434+ q = au_hdentry(dinfo, new_bindex);
8435+ tmp = *q;
8436+ *q = *p;
8437+ *p = tmp;
8438+ if (tmp.hd_dentry) {
8439+ bindex--;
8440+ p--;
8441+ }
8442+ }
8443+
8444+ dinfo->di_bwh = -1;
8445+ if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
8446+ dinfo->di_bwh = bwh;
8447+
8448+ dinfo->di_bdiropq = -1;
8449+ if (bdiropq >= 0
8450+ && bdiropq <= au_sbbot(sb)
8451+ && au_sbr_whable(sb, bdiropq))
8452+ dinfo->di_bdiropq = bdiropq;
8453+
8454+ err = -EIO;
8455+ dinfo->di_btop = -1;
8456+ dinfo->di_bbot = -1;
8457+ bbot = au_dbbot(parent);
8458+ bindex = 0;
8459+ p = au_hdentry(dinfo, bindex);
8460+ for (; bindex <= bbot; bindex++, p++)
8461+ if (p->hd_dentry) {
8462+ dinfo->di_btop = bindex;
8463+ break;
8464+ }
8465+
8466+ if (dinfo->di_btop >= 0) {
8467+ bindex = bbot;
8468+ p = au_hdentry(dinfo, bindex);
8469+ for (; bindex >= 0; bindex--, p--)
8470+ if (p->hd_dentry) {
8471+ dinfo->di_bbot = bindex;
8472+ err = 0;
8473+ break;
8474+ }
8475+ }
8476+
8477+ return err;
8478+}
8479+
8480+static void au_do_hide(struct dentry *dentry)
8481+{
8482+ struct inode *inode;
8483+
8484+ if (d_really_is_positive(dentry)) {
8485+ inode = d_inode(dentry);
8486+ if (!d_is_dir(dentry)) {
8487+ if (inode->i_nlink && !d_unhashed(dentry))
8488+ drop_nlink(inode);
8489+ } else {
8490+ clear_nlink(inode);
8491+ /* stop next lookup */
8492+ inode->i_flags |= S_DEAD;
8493+ }
8494+ smp_mb(); /* necessary? */
8495+ }
8496+ d_drop(dentry);
8497+}
8498+
8499+static int au_hide_children(struct dentry *parent)
8500+{
8501+ int err, i, j, ndentry;
8502+ struct au_dcsub_pages dpages;
8503+ struct au_dpage *dpage;
8504+ struct dentry *dentry;
8505+
8506+ err = au_dpages_init(&dpages, GFP_NOFS);
8507+ if (unlikely(err))
8508+ goto out;
8509+ err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8510+ if (unlikely(err))
8511+ goto out_dpages;
8512+
8513+ /* in reverse order */
8514+ for (i = dpages.ndpage - 1; i >= 0; i--) {
8515+ dpage = dpages.dpages + i;
8516+ ndentry = dpage->ndentry;
8517+ for (j = ndentry - 1; j >= 0; j--) {
8518+ dentry = dpage->dentries[j];
8519+ if (dentry != parent)
8520+ au_do_hide(dentry);
8521+ }
8522+ }
8523+
8524+out_dpages:
8525+ au_dpages_free(&dpages);
8526+out:
8527+ return err;
8528+}
8529+
8530+static void au_hide(struct dentry *dentry)
8531+{
8532+ int err;
8533+
8534+ AuDbgDentry(dentry);
8535+ if (d_is_dir(dentry)) {
8536+ /* shrink_dcache_parent(dentry); */
8537+ err = au_hide_children(dentry);
8538+ if (unlikely(err))
8539+ AuIOErr("%pd, failed hiding children, ignored %d\n",
8540+ dentry, err);
8541+ }
8542+ au_do_hide(dentry);
8543+}
8544+
8545+/*
8546+ * By adding a dirty branch, a cached dentry may be affected in various ways.
8547+ *
8548+ * a dirty branch is added
8549+ * - on the top of layers
8550+ * - in the middle of layers
8551+ * - to the bottom of layers
8552+ *
8553+ * on the added branch there exists
8554+ * - a whiteout
8555+ * - a diropq
8556+ * - a same named entry
8557+ * + exist
8558+ * * negative --> positive
8559+ * * positive --> positive
8560+ * - type is unchanged
8561+ * - type is changed
8562+ * + doesn't exist
8563+ * * negative --> negative
8564+ * * positive --> negative (rejected by au_br_del() for non-dir case)
8565+ * - none
8566+ */
8567+static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8568+ struct au_dinfo *tmp)
8569+{
8570+ int err;
8571+ aufs_bindex_t bindex, bbot;
8572+ struct {
8573+ struct dentry *dentry;
8574+ struct inode *inode;
8575+ mode_t mode;
8576+ } orig_h, tmp_h = {
8577+ .dentry = NULL
8578+ };
8579+ struct au_hdentry *hd;
8580+ struct inode *inode, *h_inode;
8581+ struct dentry *h_dentry;
8582+
8583+ err = 0;
8584+ AuDebugOn(dinfo->di_btop < 0);
8585+ orig_h.mode = 0;
8586+ orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
8587+ orig_h.inode = NULL;
8588+ if (d_is_positive(orig_h.dentry)) {
8589+ orig_h.inode = d_inode(orig_h.dentry);
8590+ orig_h.mode = orig_h.inode->i_mode & S_IFMT;
8591+ }
8592+ if (tmp->di_btop >= 0) {
8593+ tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
8594+ if (d_is_positive(tmp_h.dentry)) {
8595+ tmp_h.inode = d_inode(tmp_h.dentry);
8596+ tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
8597+ }
8598+ }
8599+
8600+ inode = NULL;
8601+ if (d_really_is_positive(dentry))
8602+ inode = d_inode(dentry);
8603+ if (!orig_h.inode) {
8604+ AuDbg("negative originally\n");
8605+ if (inode) {
8606+ au_hide(dentry);
8607+ goto out;
8608+ }
8609+ AuDebugOn(inode);
8610+ AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8611+ AuDebugOn(dinfo->di_bdiropq != -1);
8612+
8613+ if (!tmp_h.inode) {
8614+ AuDbg("negative --> negative\n");
8615+ /* should have only one negative lower */
8616+ if (tmp->di_btop >= 0
8617+ && tmp->di_btop < dinfo->di_btop) {
8618+ AuDebugOn(tmp->di_btop != tmp->di_bbot);
8619+ AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8620+ au_set_h_dptr(dentry, dinfo->di_btop, NULL);
8621+ au_di_cp(dinfo, tmp);
8622+ hd = au_hdentry(tmp, tmp->di_btop);
8623+ au_set_h_dptr(dentry, tmp->di_btop,
8624+ dget(hd->hd_dentry));
8625+ }
8626+ au_dbg_verify_dinode(dentry);
8627+ } else {
8628+ AuDbg("negative --> positive\n");
8629+ /*
8630+ * similar to the behaviour of creating with bypassing
8631+ * aufs.
8632+ * unhash it in order to force an error in the
8633+ * succeeding create operation.
8634+ * we should not set S_DEAD here.
8635+ */
8636+ d_drop(dentry);
8637+ /* au_di_swap(tmp, dinfo); */
8638+ au_dbg_verify_dinode(dentry);
8639+ }
8640+ } else {
8641+ AuDbg("positive originally\n");
8642+ /* inode may be NULL */
8643+ AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8644+ if (!tmp_h.inode) {
8645+ AuDbg("positive --> negative\n");
8646+ /* or bypassing aufs */
8647+ au_hide(dentry);
8648+ if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
8649+ dinfo->di_bwh = tmp->di_bwh;
8650+ if (inode)
8651+ err = au_refresh_hinode_self(inode);
8652+ au_dbg_verify_dinode(dentry);
8653+ } else if (orig_h.mode == tmp_h.mode) {
8654+ AuDbg("positive --> positive, same type\n");
8655+ if (!S_ISDIR(orig_h.mode)
8656+ && dinfo->di_btop > tmp->di_btop) {
8657+ /*
8658+ * similar to the behaviour of removing and
8659+ * creating.
8660+ */
8661+ au_hide(dentry);
8662+ if (inode)
8663+ err = au_refresh_hinode_self(inode);
8664+ au_dbg_verify_dinode(dentry);
8665+ } else {
8666+ /* fill empty slots */
8667+ if (dinfo->di_btop > tmp->di_btop)
8668+ dinfo->di_btop = tmp->di_btop;
8669+ if (dinfo->di_bbot < tmp->di_bbot)
8670+ dinfo->di_bbot = tmp->di_bbot;
8671+ dinfo->di_bwh = tmp->di_bwh;
8672+ dinfo->di_bdiropq = tmp->di_bdiropq;
8673+ bbot = dinfo->di_bbot;
8674+ bindex = tmp->di_btop;
8675+ hd = au_hdentry(tmp, bindex);
8676+ for (; bindex <= bbot; bindex++, hd++) {
8677+ if (au_h_dptr(dentry, bindex))
8678+ continue;
8679+ h_dentry = hd->hd_dentry;
8680+ if (!h_dentry)
8681+ continue;
8682+ AuDebugOn(d_is_negative(h_dentry));
8683+ h_inode = d_inode(h_dentry);
8684+ AuDebugOn(orig_h.mode
8685+ != (h_inode->i_mode
8686+ & S_IFMT));
8687+ au_set_h_dptr(dentry, bindex,
8688+ dget(h_dentry));
8689+ }
8690+ if (inode)
8691+ err = au_refresh_hinode(inode, dentry);
8692+ au_dbg_verify_dinode(dentry);
8693+ }
8694+ } else {
8695+ AuDbg("positive --> positive, different type\n");
8696+ /* similar to the behaviour of removing and creating */
8697+ au_hide(dentry);
8698+ if (inode)
8699+ err = au_refresh_hinode_self(inode);
8700+ au_dbg_verify_dinode(dentry);
8701+ }
8702+ }
8703+
8704+out:
8705+ return err;
8706+}
8707+
8708+void au_refresh_dop(struct dentry *dentry, int force_reval)
8709+{
8710+ const struct dentry_operations *dop
8711+ = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8712+ static const unsigned int mask
8713+ = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8714+
8715+ BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8716+
8717+ if (dentry->d_op == dop)
8718+ return;
8719+
8720+ AuDbg("%pd\n", dentry);
8721+ spin_lock(&dentry->d_lock);
8722+ if (dop == &aufs_dop)
8723+ dentry->d_flags |= mask;
8724+ else
8725+ dentry->d_flags &= ~mask;
8726+ dentry->d_op = dop;
8727+ spin_unlock(&dentry->d_lock);
8728+}
8729+
8730+int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8731+{
8732+ int err, ebrange, nbr;
8733+ unsigned int sigen;
8734+ struct au_dinfo *dinfo, *tmp;
8735+ struct super_block *sb;
8736+ struct inode *inode;
8737+
8738+ DiMustWriteLock(dentry);
8739+ AuDebugOn(IS_ROOT(dentry));
8740+ AuDebugOn(d_really_is_negative(parent));
8741+
8742+ sb = dentry->d_sb;
8743+ sigen = au_sigen(sb);
8744+ err = au_digen_test(parent, sigen);
8745+ if (unlikely(err))
8746+ goto out;
8747+
8748+ nbr = au_sbbot(sb) + 1;
8749+ dinfo = au_di(dentry);
8750+ err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
8751+ if (unlikely(err))
8752+ goto out;
8753+ ebrange = au_dbrange_test(dentry);
8754+ if (!ebrange)
8755+ ebrange = au_do_refresh_hdentry(dentry, parent);
8756+
8757+ if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
8758+ AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
8759+ if (d_really_is_positive(dentry)) {
8760+ inode = d_inode(dentry);
8761+ err = au_refresh_hinode_self(inode);
8762+ }
8763+ au_dbg_verify_dinode(dentry);
8764+ if (!err)
8765+ goto out_dgen; /* success */
8766+ goto out;
8767+ }
8768+
8769+ /* temporary dinfo */
8770+ AuDbgDentry(dentry);
8771+ err = -ENOMEM;
8772+ tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8773+ if (unlikely(!tmp))
8774+ goto out;
8775+ au_di_swap(tmp, dinfo);
8776+ /* returns the number of positive dentries */
8777+ /*
8778+ * if current working dir is removed, it returns an error.
8779+ * but the dentry is legal.
8780+ */
8781+ err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
8782+ AuDbgDentry(dentry);
8783+ au_di_swap(tmp, dinfo);
8784+ if (err == -ENOENT)
8785+ err = 0;
8786+ if (err >= 0) {
8787+ /* compare/refresh by dinfo */
8788+ AuDbgDentry(dentry);
8789+ err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8790+ au_dbg_verify_dinode(dentry);
8791+ AuTraceErr(err);
8792+ }
8793+ au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
8794+ au_rw_write_unlock(&tmp->di_rwsem);
8795+ au_di_free(tmp);
8796+ if (unlikely(err))
8797+ goto out;
8798+
8799+out_dgen:
8800+ au_update_digen(dentry);
8801+out:
8802+ if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
8803+ AuIOErr("failed refreshing %pd, %d\n", dentry, err);
8804+ AuDbgDentry(dentry);
8805+ }
8806+ AuTraceErr(err);
8807+ return err;
8808+}
8809+
8810+static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8811+ struct dentry *dentry, aufs_bindex_t bindex)
8812+{
8813+ int err, valid;
8814+
8815+ err = 0;
8816+ if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8817+ goto out;
8818+
8819+ AuDbg("b%d\n", bindex);
8820+ /*
8821+ * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8822+ * due to whiteout and branch permission.
8823+ */
8824+ flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8825+ | LOOKUP_FOLLOW | LOOKUP_EXCL);
8826+ /* it may return tri-state */
8827+ valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
8828+
8829+ if (unlikely(valid < 0))
8830+ err = valid;
8831+ else if (!valid)
8832+ err = -EINVAL;
8833+
8834+out:
8835+ AuTraceErr(err);
8836+ return err;
8837+}
8838+
8839+/* todo: remove this */
8840+static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8841+ unsigned int flags, int do_udba, int dirren)
8842+{
8843+ int err;
8844+ umode_t mode, h_mode;
8845+ aufs_bindex_t bindex, btail, btop, ibs, ibe;
8846+ unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
8847+ struct inode *h_inode, *h_cached_inode;
8848+ struct dentry *h_dentry;
8849+ struct qstr *name, *h_name;
8850+
8851+ err = 0;
8852+ plus = 0;
8853+ mode = 0;
8854+ ibs = -1;
8855+ ibe = -1;
8856+ unhashed = !!d_unhashed(dentry);
8857+ is_root = !!IS_ROOT(dentry);
8858+ name = &dentry->d_name;
8859+ tmpfile = au_di(dentry)->di_tmpfile;
8860+
8861+ /*
8862+ * Theoretically, REVAL test should be unnecessary in case of
8863+ * {FS,I}NOTIFY.
8864+ * But {fs,i}notify doesn't fire some necessary events,
8865+ * IN_ATTRIB for atime/nlink/pageio
8866+ * Let's do REVAL test too.
8867+ */
8868+ if (do_udba && inode) {
8869+ mode = (inode->i_mode & S_IFMT);
8870+ plus = (inode->i_nlink > 0);
8871+ ibs = au_ibtop(inode);
8872+ ibe = au_ibbot(inode);
8873+ }
8874+
8875+ btop = au_dbtop(dentry);
8876+ btail = btop;
8877+ if (inode && S_ISDIR(inode->i_mode))
8878+ btail = au_dbtaildir(dentry);
8879+ for (bindex = btop; bindex <= btail; bindex++) {
8880+ h_dentry = au_h_dptr(dentry, bindex);
8881+ if (!h_dentry)
8882+ continue;
8883+
8884+ AuDbg("b%d, %pd\n", bindex, h_dentry);
8885+ h_nfs = !!au_test_nfs(h_dentry->d_sb);
8886+ spin_lock(&h_dentry->d_lock);
8887+ h_name = &h_dentry->d_name;
8888+ if (unlikely(do_udba
8889+ && !is_root
8890+ && ((!h_nfs
8891+ && (unhashed != !!d_unhashed(h_dentry)
8892+ || (!tmpfile && !dirren
8893+ && !au_qstreq(name, h_name))
8894+ ))
8895+ || (h_nfs
8896+ && !(flags & LOOKUP_OPEN)
8897+ && (h_dentry->d_flags
8898+ & DCACHE_NFSFS_RENAMED)))
8899+ )) {
8900+ int h_unhashed;
8901+
8902+ h_unhashed = d_unhashed(h_dentry);
8903+ spin_unlock(&h_dentry->d_lock);
8904+ AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
8905+ unhashed, h_unhashed, dentry, h_dentry);
8906+ goto err;
8907+ }
8908+ spin_unlock(&h_dentry->d_lock);
8909+
8910+ err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
8911+ if (unlikely(err))
8912+ /* do not goto err, to keep the errno */
8913+ break;
8914+
8915+ /* todo: plink too? */
8916+ if (!do_udba)
8917+ continue;
8918+
8919+ /* UDBA tests */
8920+ if (unlikely(!!inode != d_is_positive(h_dentry)))
8921+ goto err;
8922+
8923+ h_inode = NULL;
8924+ if (d_is_positive(h_dentry))
8925+ h_inode = d_inode(h_dentry);
8926+ h_plus = plus;
8927+ h_mode = mode;
8928+ h_cached_inode = h_inode;
8929+ if (h_inode) {
8930+ h_mode = (h_inode->i_mode & S_IFMT);
8931+ h_plus = (h_inode->i_nlink > 0);
8932+ }
8933+ if (inode && ibs <= bindex && bindex <= ibe)
8934+ h_cached_inode = au_h_iptr(inode, bindex);
8935+
8936+ if (!h_nfs) {
8937+ if (unlikely(plus != h_plus && !tmpfile))
8938+ goto err;
8939+ } else {
8940+ if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
8941+ && !is_root
8942+ && !IS_ROOT(h_dentry)
8943+ && unhashed != d_unhashed(h_dentry)))
8944+ goto err;
8945+ }
8946+ if (unlikely(mode != h_mode
8947+ || h_cached_inode != h_inode))
8948+ goto err;
8949+ continue;
8950+
8951+err:
8952+ err = -EINVAL;
8953+ break;
8954+ }
8955+
8956+ AuTraceErr(err);
8957+ return err;
8958+}
8959+
8960+/* todo: consolidate with do_refresh() and au_reval_for_attr() */
8961+static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
8962+{
8963+ int err;
8964+ struct dentry *parent;
8965+
8966+ if (!au_digen_test(dentry, sigen))
8967+ return 0;
8968+
8969+ parent = dget_parent(dentry);
8970+ di_read_lock_parent(parent, AuLock_IR);
8971+ AuDebugOn(au_digen_test(parent, sigen));
8972+ au_dbg_verify_gen(parent, sigen);
8973+ err = au_refresh_dentry(dentry, parent);
8974+ di_read_unlock(parent, AuLock_IR);
8975+ dput(parent);
8976+ AuTraceErr(err);
8977+ return err;
8978+}
8979+
8980+int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
8981+{
8982+ int err;
8983+ struct dentry *d, *parent;
8984+
8985+ if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
8986+ return simple_reval_dpath(dentry, sigen);
8987+
8988+ /* slow loop, keep it simple and stupid */
8989+ /* cf: au_cpup_dirs() */
8990+ err = 0;
8991+ parent = NULL;
8992+ while (au_digen_test(dentry, sigen)) {
8993+ d = dentry;
8994+ while (1) {
8995+ dput(parent);
8996+ parent = dget_parent(d);
8997+ if (!au_digen_test(parent, sigen))
8998+ break;
8999+ d = parent;
9000+ }
9001+
9002+ if (d != dentry)
9003+ di_write_lock_child2(d);
9004+
9005+ /* someone might update our dentry while we were sleeping */
9006+ if (au_digen_test(d, sigen)) {
9007+ /*
9008+ * todo: consolidate with simple_reval_dpath(),
9009+ * do_refresh() and au_reval_for_attr().
9010+ */
9011+ di_read_lock_parent(parent, AuLock_IR);
9012+ err = au_refresh_dentry(d, parent);
9013+ di_read_unlock(parent, AuLock_IR);
9014+ }
9015+
9016+ if (d != dentry)
9017+ di_write_unlock(d);
9018+ dput(parent);
9019+ if (unlikely(err))
9020+ break;
9021+ }
9022+
9023+ return err;
9024+}
9025+
9026+/*
9027+ * if valid returns 1, otherwise 0.
9028+ */
9029+static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
9030+{
9031+ int valid, err;
9032+ unsigned int sigen;
9033+ unsigned char do_udba, dirren;
9034+ struct super_block *sb;
9035+ struct inode *inode;
9036+
9037+ /* todo: support rcu-walk? */
9038+ if (flags & LOOKUP_RCU)
9039+ return -ECHILD;
9040+
9041+ valid = 0;
9042+ if (unlikely(!au_di(dentry)))
9043+ goto out;
9044+
9045+ valid = 1;
9046+ sb = dentry->d_sb;
9047+ /*
9048+ * todo: very ugly
9049+ * i_mutex of parent dir may be held,
9050+ * but we should not return 'invalid' due to busy.
9051+ */
9052+ err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
9053+ if (unlikely(err)) {
9054+ valid = err;
9055+ AuTraceErr(err);
9056+ goto out;
9057+ }
9058+ inode = NULL;
9059+ if (d_really_is_positive(dentry))
9060+ inode = d_inode(dentry);
9061+ if (unlikely(inode && au_is_bad_inode(inode))) {
9062+ err = -EINVAL;
9063+ AuTraceErr(err);
9064+ goto out_dgrade;
9065+ }
9066+ if (unlikely(au_dbrange_test(dentry))) {
9067+ err = -EINVAL;
9068+ AuTraceErr(err);
9069+ goto out_dgrade;
9070+ }
9071+
9072+ sigen = au_sigen(sb);
9073+ if (au_digen_test(dentry, sigen)) {
9074+ AuDebugOn(IS_ROOT(dentry));
9075+ err = au_reval_dpath(dentry, sigen);
9076+ if (unlikely(err)) {
9077+ AuTraceErr(err);
9078+ goto out_dgrade;
9079+ }
9080+ }
9081+ di_downgrade_lock(dentry, AuLock_IR);
9082+
9083+ err = -EINVAL;
9084+ if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
9085+ && inode
9086+ && !(inode->i_state && I_LINKABLE)
9087+ && (IS_DEADDIR(inode) || !inode->i_nlink)) {
9088+ AuTraceErr(err);
9089+ goto out_inval;
9090+ }
9091+
9092+ do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
9093+ if (do_udba && inode) {
9094+ aufs_bindex_t btop = au_ibtop(inode);
9095+ struct inode *h_inode;
9096+
9097+ if (btop >= 0) {
9098+ h_inode = au_h_iptr(inode, btop);
9099+ if (h_inode && au_test_higen(inode, h_inode)) {
9100+ AuTraceErr(err);
9101+ goto out_inval;
9102+ }
9103+ }
9104+ }
9105+
9106+ dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
9107+ err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
9108+ if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
9109+ err = -EIO;
9110+ AuDbg("both of real entry and whiteout found, %p, err %d\n",
9111+ dentry, err);
9112+ }
9113+ goto out_inval;
9114+
9115+out_dgrade:
9116+ di_downgrade_lock(dentry, AuLock_IR);
9117+out_inval:
9118+ aufs_read_unlock(dentry, AuLock_IR);
9119+ AuTraceErr(err);
9120+ valid = !err;
9121+out:
9122+ if (!valid) {
9123+ AuDbg("%pd invalid, %d\n", dentry, valid);
9124+ d_drop(dentry);
9125+ }
9126+ return valid;
9127+}
9128+
9129+static void aufs_d_release(struct dentry *dentry)
9130+{
9131+ if (au_di(dentry)) {
9132+ au_di_fin(dentry);
9133+ au_hn_di_reinit(dentry);
9134+ }
9135+}
9136+
9137+const struct dentry_operations aufs_dop = {
9138+ .d_revalidate = aufs_d_revalidate,
9139+ .d_weak_revalidate = aufs_d_revalidate,
9140+ .d_release = aufs_d_release
9141+};
9142+
9143+/* aufs_dop without d_revalidate */
9144+const struct dentry_operations aufs_dop_noreval = {
9145+ .d_release = aufs_d_release
9146+};
9147diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
9148--- /usr/share/empty/fs/aufs/dentry.h 1970-01-01 01:00:00.000000000 +0100
9149+++ linux/fs/aufs/dentry.h 2021-06-30 21:35:11.393873211 +0200
9150@@ -0,0 +1,269 @@
9151+/* SPDX-License-Identifier: GPL-2.0 */
9152+/*
9153+ * Copyright (C) 2005-2020 Junjiro R. Okajima
9154+ *
9155+ * This program, aufs is free software; you can redistribute it and/or modify
9156+ * it under the terms of the GNU General Public License as published by
9157+ * the Free Software Foundation; either version 2 of the License, or
9158+ * (at your option) any later version.
9159+ *
9160+ * This program is distributed in the hope that it will be useful,
9161+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9162+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9163+ * GNU General Public License for more details.
9164+ *
9165+ * You should have received a copy of the GNU General Public License
9166+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
9167+ */
9168+
9169+/*
9170+ * lookup and dentry operations
9171+ */
9172+
9173+#ifndef __AUFS_DENTRY_H__
9174+#define __AUFS_DENTRY_H__
9175+
9176+#ifdef __KERNEL__
9177+
9178+#include <linux/dcache.h>
9179+#include "dirren.h"
9180+#include "rwsem.h"
9181+
9182+struct au_hdentry {
9183+ struct dentry *hd_dentry;
9184+ aufs_bindex_t hd_id;
9185+};
9186+
9187+struct au_dinfo {
9188+ atomic_t di_generation;
9189+
9190+ struct au_rwsem di_rwsem;
9191+ aufs_bindex_t di_btop, di_bbot, di_bwh, di_bdiropq;
9192+ unsigned char di_tmpfile; /* to allow the different name */
9193+ struct au_hdentry *di_hdentry;
9194+ struct rcu_head rcu;
9195+} ____cacheline_aligned_in_smp;
9196+
9197+/* ---------------------------------------------------------------------- */
9198+
9199+/* flags for au_lkup_dentry() */
9200+#define AuLkup_ALLOW_NEG 1
9201+#define AuLkup_IGNORE_PERM (1 << 1)
9202+#define AuLkup_DIRREN (1 << 2)
9203+#define au_ftest_lkup(flags, name) ((flags) & AuLkup_##name)
9204+#define au_fset_lkup(flags, name) \
9205+ do { (flags) |= AuLkup_##name; } while (0)
9206+#define au_fclr_lkup(flags, name) \
9207+ do { (flags) &= ~AuLkup_##name; } while (0)
9208+
9209+#ifndef CONFIG_AUFS_DIRREN
9210+#undef AuLkup_DIRREN
9211+#define AuLkup_DIRREN 0
9212+#endif
9213+
9214+struct au_do_lookup_args {
9215+ unsigned int flags;
9216+ mode_t type;
9217+ struct qstr whname, *name;
9218+ struct au_dr_lookup dirren;
9219+};
9220+
9221+/* ---------------------------------------------------------------------- */
9222+
9223+/* dentry.c */
9224+extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
9225+struct au_branch;
9226+struct dentry *au_sio_lkup_one(struct user_namespace *userns, struct qstr *name,
9227+ struct dentry *parent);
9228+int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
9229+ struct dentry *h_parent, struct au_branch *br);
9230+
9231+int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
9232+ unsigned int flags);
9233+int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
9234+int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
9235+int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
9236+void au_refresh_dop(struct dentry *dentry, int force_reval);
9237+
9238+/* dinfo.c */
9239+void au_di_init_once(void *_di);
9240+struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
9241+void au_di_free(struct au_dinfo *dinfo);
9242+void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
9243+void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
9244+int au_di_init(struct dentry *dentry);
9245+void au_di_fin(struct dentry *dentry);
9246+int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
9247+
9248+void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
9249+void di_read_unlock(struct dentry *d, int flags);
9250+void di_downgrade_lock(struct dentry *d, int flags);
9251+void di_write_lock(struct dentry *d, unsigned int lsc);
9252+void di_write_unlock(struct dentry *d);
9253+void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
9254+void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
9255+void di_write_unlock2(struct dentry *d1, struct dentry *d2);
9256+
9257+struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
9258+struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
9259+aufs_bindex_t au_dbtail(struct dentry *dentry);
9260+aufs_bindex_t au_dbtaildir(struct dentry *dentry);
9261+
9262+void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9263+ struct dentry *h_dentry);
9264+int au_digen_test(struct dentry *dentry, unsigned int sigen);
9265+int au_dbrange_test(struct dentry *dentry);
9266+void au_update_digen(struct dentry *dentry);
9267+void au_update_dbrange(struct dentry *dentry, int do_put_zero);
9268+void au_update_dbtop(struct dentry *dentry);
9269+void au_update_dbbot(struct dentry *dentry);
9270+int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
9271+
9272+/* ---------------------------------------------------------------------- */
9273+
9274+static inline struct au_dinfo *au_di(struct dentry *dentry)
9275+{
9276+ return dentry->d_fsdata;
9277+}
9278+
9279+/* ---------------------------------------------------------------------- */
9280+
9281+/* lock subclass for dinfo */
9282+enum {
9283+ AuLsc_DI_CHILD, /* child first */
9284+ AuLsc_DI_CHILD2, /* rename(2), link(2), and cpup at hnotify */
9285+ AuLsc_DI_CHILD3, /* copyup dirs */
9286+ AuLsc_DI_PARENT,
9287+ AuLsc_DI_PARENT2,
9288+ AuLsc_DI_PARENT3,
9289+ AuLsc_DI_TMP /* temp for replacing dinfo */
9290+};
9291+
9292+/*
9293+ * di_read_lock_child, di_write_lock_child,
9294+ * di_read_lock_child2, di_write_lock_child2,
9295+ * di_read_lock_child3, di_write_lock_child3,
9296+ * di_read_lock_parent, di_write_lock_parent,
9297+ * di_read_lock_parent2, di_write_lock_parent2,
9298+ * di_read_lock_parent3, di_write_lock_parent3,
9299+ */
9300+#define AuReadLockFunc(name, lsc) \
9301+static inline void di_read_lock_##name(struct dentry *d, int flags) \
9302+{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
9303+
9304+#define AuWriteLockFunc(name, lsc) \
9305+static inline void di_write_lock_##name(struct dentry *d) \
9306+{ di_write_lock(d, AuLsc_DI_##lsc); }
9307+
9308+#define AuRWLockFuncs(name, lsc) \
9309+ AuReadLockFunc(name, lsc) \
9310+ AuWriteLockFunc(name, lsc)
9311+
9312+AuRWLockFuncs(child, CHILD);
9313+AuRWLockFuncs(child2, CHILD2);
9314+AuRWLockFuncs(child3, CHILD3);
9315+AuRWLockFuncs(parent, PARENT);
9316+AuRWLockFuncs(parent2, PARENT2);
9317+AuRWLockFuncs(parent3, PARENT3);
9318+
9319+#undef AuReadLockFunc
9320+#undef AuWriteLockFunc
9321+#undef AuRWLockFuncs
9322+
9323+#define DiMustNoWaiters(d) AuRwMustNoWaiters(&au_di(d)->di_rwsem)
9324+#define DiMustAnyLock(d) AuRwMustAnyLock(&au_di(d)->di_rwsem)
9325+#define DiMustWriteLock(d) AuRwMustWriteLock(&au_di(d)->di_rwsem)
9326+
9327+/* ---------------------------------------------------------------------- */
9328+
9329+/* todo: memory barrier? */
9330+static inline unsigned int au_digen(struct dentry *d)
9331+{
9332+ return atomic_read(&au_di(d)->di_generation);
9333+}
9334+
9335+static inline void au_h_dentry_init(struct au_hdentry *hdentry)
9336+{
9337+ hdentry->hd_dentry = NULL;
9338+}
9339+
9340+static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
9341+ aufs_bindex_t bindex)
9342+{
9343+ return di->di_hdentry + bindex;
9344+}
9345+
9346+static inline void au_hdput(struct au_hdentry *hd)
9347+{
9348+ if (hd)
9349+ dput(hd->hd_dentry);
9350+}
9351+
9352+static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
9353+{
9354+ DiMustAnyLock(dentry);
9355+ return au_di(dentry)->di_btop;
9356+}
9357+
9358+static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
9359+{
9360+ DiMustAnyLock(dentry);
9361+ return au_di(dentry)->di_bbot;
9362+}
9363+
9364+static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
9365+{
9366+ DiMustAnyLock(dentry);
9367+ return au_di(dentry)->di_bwh;
9368+}
9369+
9370+static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
9371+{
9372+ DiMustAnyLock(dentry);
9373+ return au_di(dentry)->di_bdiropq;
9374+}
9375+
9376+/* todo: hard/soft set? */
9377+static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
9378+{
9379+ DiMustWriteLock(dentry);
9380+ au_di(dentry)->di_btop = bindex;
9381+}
9382+
9383+static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
9384+{
9385+ DiMustWriteLock(dentry);
9386+ au_di(dentry)->di_bbot = bindex;
9387+}
9388+
9389+static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
9390+{
9391+ DiMustWriteLock(dentry);
9392+ /* dbwh can be outside of btop - bbot range */
9393+ au_di(dentry)->di_bwh = bindex;
9394+}
9395+
9396+static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
9397+{
9398+ DiMustWriteLock(dentry);
9399+ au_di(dentry)->di_bdiropq = bindex;
9400+}
9401+
9402+/* ---------------------------------------------------------------------- */
9403+
9404+#ifdef CONFIG_AUFS_HNOTIFY
9405+static inline void au_digen_dec(struct dentry *d)
9406+{
9407+ atomic_dec(&au_di(d)->di_generation);
9408+}
9409+
9410+static inline void au_hn_di_reinit(struct dentry *dentry)
9411+{
9412+ dentry->d_fsdata = NULL;
9413+}
9414+#else
9415+AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
9416+#endif /* CONFIG_AUFS_HNOTIFY */
9417+
9418+#endif /* __KERNEL__ */
9419+#endif /* __AUFS_DENTRY_H__ */
9420diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
9421--- /usr/share/empty/fs/aufs/dinfo.c 1970-01-01 01:00:00.000000000 +0100
9422+++ linux/fs/aufs/dinfo.c 2021-02-24 13:33:42.741013619 +0100
9423@@ -0,0 +1,554 @@
9424+// SPDX-License-Identifier: GPL-2.0
9425+/*
9426+ * Copyright (C) 2005-2020 Junjiro R. Okajima
9427+ *
9428+ * This program, aufs is free software; you can redistribute it and/or modify
9429+ * it under the terms of the GNU General Public License as published by
9430+ * the Free Software Foundation; either version 2 of the License, or
9431+ * (at your option) any later version.
9432+ *
9433+ * This program is distributed in the hope that it will be useful,
9434+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9435+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9436+ * GNU General Public License for more details.
9437+ *
9438+ * You should have received a copy of the GNU General Public License
9439+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
9440+ */
9441+
9442+/*
9443+ * dentry private data
9444+ */
9445+
9446+#include "aufs.h"
9447+
9448+void au_di_init_once(void *_dinfo)
9449+{
9450+ struct au_dinfo *dinfo = _dinfo;
9451+
9452+ au_rw_init(&dinfo->di_rwsem);
9453+}
9454+
9455+struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
9456+{
9457+ struct au_dinfo *dinfo;
9458+ int nbr, i;
9459+
9460+ dinfo = au_cache_alloc_dinfo();
9461+ if (unlikely(!dinfo))
9462+ goto out;
9463+
9464+ nbr = au_sbbot(sb) + 1;
9465+ if (nbr <= 0)
9466+ nbr = 1;
9467+ dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
9468+ if (dinfo->di_hdentry) {
9469+ au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
9470+ dinfo->di_btop = -1;
9471+ dinfo->di_bbot = -1;
9472+ dinfo->di_bwh = -1;
9473+ dinfo->di_bdiropq = -1;
9474+ dinfo->di_tmpfile = 0;
9475+ for (i = 0; i < nbr; i++)
9476+ dinfo->di_hdentry[i].hd_id = -1;
9477+ goto out;
9478+ }
9479+
9480+ au_cache_free_dinfo(dinfo);
9481+ dinfo = NULL;
9482+
9483+out:
9484+ return dinfo;
9485+}
9486+
9487+void au_di_free(struct au_dinfo *dinfo)
9488+{
9489+ struct au_hdentry *p;
9490+ aufs_bindex_t bbot, bindex;
9491+
9492+ /* dentry may not be revalidated */
9493+ bindex = dinfo->di_btop;
9494+ if (bindex >= 0) {
9495+ bbot = dinfo->di_bbot;
9496+ p = au_hdentry(dinfo, bindex);
9497+ while (bindex++ <= bbot)
9498+ au_hdput(p++);
9499+ }
9500+ au_kfree_try_rcu(dinfo->di_hdentry);
9501+ au_cache_free_dinfo(dinfo);
9502+}
9503+
9504+void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9505+{
9506+ struct au_hdentry *p;
9507+ aufs_bindex_t bi;
9508+
9509+ AuRwMustWriteLock(&a->di_rwsem);
9510+ AuRwMustWriteLock(&b->di_rwsem);
9511+
9512+#define DiSwap(v, name) \
9513+ do { \
9514+ v = a->di_##name; \
9515+ a->di_##name = b->di_##name; \
9516+ b->di_##name = v; \
9517+ } while (0)
9518+
9519+ DiSwap(p, hdentry);
9520+ DiSwap(bi, btop);
9521+ DiSwap(bi, bbot);
9522+ DiSwap(bi, bwh);
9523+ DiSwap(bi, bdiropq);
9524+ /* smp_mb(); */
9525+
9526+#undef DiSwap
9527+}
9528+
9529+void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9530+{
9531+ AuRwMustWriteLock(&dst->di_rwsem);
9532+ AuRwMustWriteLock(&src->di_rwsem);
9533+
9534+ dst->di_btop = src->di_btop;
9535+ dst->di_bbot = src->di_bbot;
9536+ dst->di_bwh = src->di_bwh;
9537+ dst->di_bdiropq = src->di_bdiropq;
9538+ /* smp_mb(); */
9539+}
9540+
9541+int au_di_init(struct dentry *dentry)
9542+{
9543+ int err;
9544+ struct super_block *sb;
9545+ struct au_dinfo *dinfo;
9546+
9547+ err = 0;
9548+ sb = dentry->d_sb;
9549+ dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9550+ if (dinfo) {
9551+ atomic_set(&dinfo->di_generation, au_sigen(sb));
9552+ /* smp_mb(); */ /* atomic_set */
9553+ dentry->d_fsdata = dinfo;
9554+ } else
9555+ err = -ENOMEM;
9556+
9557+ return err;
9558+}
9559+
9560+void au_di_fin(struct dentry *dentry)
9561+{
9562+ struct au_dinfo *dinfo;
9563+
9564+ dinfo = au_di(dentry);
9565+ AuRwDestroy(&dinfo->di_rwsem);
9566+ au_di_free(dinfo);
9567+}
9568+
9569+int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
9570+{
9571+ int err, sz;
9572+ struct au_hdentry *hdp;
9573+
9574+ AuRwMustWriteLock(&dinfo->di_rwsem);
9575+
9576+ err = -ENOMEM;
9577+ sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
9578+ if (!sz)
9579+ sz = sizeof(*hdp);
9580+ hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
9581+ may_shrink);
9582+ if (hdp) {
9583+ dinfo->di_hdentry = hdp;
9584+ err = 0;
9585+ }
9586+
9587+ return err;
9588+}
9589+
9590+/* ---------------------------------------------------------------------- */
9591+
9592+static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9593+{
9594+ switch (lsc) {
9595+ case AuLsc_DI_CHILD:
9596+ ii_write_lock_child(inode);
9597+ break;
9598+ case AuLsc_DI_CHILD2:
9599+ ii_write_lock_child2(inode);
9600+ break;
9601+ case AuLsc_DI_CHILD3:
9602+ ii_write_lock_child3(inode);
9603+ break;
9604+ case AuLsc_DI_PARENT:
9605+ ii_write_lock_parent(inode);
9606+ break;
9607+ case AuLsc_DI_PARENT2:
9608+ ii_write_lock_parent2(inode);
9609+ break;
9610+ case AuLsc_DI_PARENT3:
9611+ ii_write_lock_parent3(inode);
9612+ break;
9613+ default:
9614+ BUG();
9615+ }
9616+}
9617+
9618+static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9619+{
9620+ switch (lsc) {
9621+ case AuLsc_DI_CHILD:
9622+ ii_read_lock_child(inode);
9623+ break;
9624+ case AuLsc_DI_CHILD2:
9625+ ii_read_lock_child2(inode);
9626+ break;
9627+ case AuLsc_DI_CHILD3:
9628+ ii_read_lock_child3(inode);
9629+ break;
9630+ case AuLsc_DI_PARENT:
9631+ ii_read_lock_parent(inode);
9632+ break;
9633+ case AuLsc_DI_PARENT2:
9634+ ii_read_lock_parent2(inode);
9635+ break;
9636+ case AuLsc_DI_PARENT3:
9637+ ii_read_lock_parent3(inode);
9638+ break;
9639+ default:
9640+ BUG();
9641+ }
9642+}
9643+
9644+void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9645+{
9646+ struct inode *inode;
9647+
9648+ au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
9649+ if (d_really_is_positive(d)) {
9650+ inode = d_inode(d);
9651+ if (au_ftest_lock(flags, IW))
9652+ do_ii_write_lock(inode, lsc);
9653+ else if (au_ftest_lock(flags, IR))
9654+ do_ii_read_lock(inode, lsc);
9655+ }
9656+}
9657+
9658+void di_read_unlock(struct dentry *d, int flags)
9659+{
9660+ struct inode *inode;
9661+
9662+ if (d_really_is_positive(d)) {
9663+ inode = d_inode(d);
9664+ if (au_ftest_lock(flags, IW)) {
9665+ au_dbg_verify_dinode(d);
9666+ ii_write_unlock(inode);
9667+ } else if (au_ftest_lock(flags, IR)) {
9668+ au_dbg_verify_dinode(d);
9669+ ii_read_unlock(inode);
9670+ }
9671+ }
9672+ au_rw_read_unlock(&au_di(d)->di_rwsem);
9673+}
9674+
9675+void di_downgrade_lock(struct dentry *d, int flags)
9676+{
9677+ if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9678+ ii_downgrade_lock(d_inode(d));
9679+ au_rw_dgrade_lock(&au_di(d)->di_rwsem);
9680+}
9681+
9682+void di_write_lock(struct dentry *d, unsigned int lsc)
9683+{
9684+ au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
9685+ if (d_really_is_positive(d))
9686+ do_ii_write_lock(d_inode(d), lsc);
9687+}
9688+
9689+void di_write_unlock(struct dentry *d)
9690+{
9691+ au_dbg_verify_dinode(d);
9692+ if (d_really_is_positive(d))
9693+ ii_write_unlock(d_inode(d));
9694+ au_rw_write_unlock(&au_di(d)->di_rwsem);
9695+}
9696+
9697+void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9698+{
9699+ AuDebugOn(d1 == d2
9700+ || d_inode(d1) == d_inode(d2)
9701+ || d1->d_sb != d2->d_sb);
9702+
9703+ if ((isdir && au_test_subdir(d1, d2))
9704+ || d1 < d2) {
9705+ di_write_lock_child(d1);
9706+ di_write_lock_child2(d2);
9707+ } else {
9708+ di_write_lock_child(d2);
9709+ di_write_lock_child2(d1);
9710+ }
9711+}
9712+
9713+void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9714+{
9715+ AuDebugOn(d1 == d2
9716+ || d_inode(d1) == d_inode(d2)
9717+ || d1->d_sb != d2->d_sb);
9718+
9719+ if ((isdir && au_test_subdir(d1, d2))
9720+ || d1 < d2) {
9721+ di_write_lock_parent(d1);
9722+ di_write_lock_parent2(d2);
9723+ } else {
9724+ di_write_lock_parent(d2);
9725+ di_write_lock_parent2(d1);
9726+ }
9727+}
9728+
9729+void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9730+{
9731+ di_write_unlock(d1);
9732+ if (d_inode(d1) == d_inode(d2))
9733+ au_rw_write_unlock(&au_di(d2)->di_rwsem);
9734+ else
9735+ di_write_unlock(d2);
9736+}
9737+
9738+/* ---------------------------------------------------------------------- */
9739+
9740+struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9741+{
9742+ struct dentry *d;
9743+
9744+ DiMustAnyLock(dentry);
9745+
9746+ if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
9747+ return NULL;
9748+ AuDebugOn(bindex < 0);
9749+ d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
9750+ AuDebugOn(d && au_dcount(d) <= 0);
9751+ return d;
9752+}
9753+
9754+/*
9755+ * extended version of au_h_dptr().
9756+ * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9757+ * error.
9758+ */
9759+struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9760+{
9761+ struct dentry *h_dentry;
9762+ struct inode *inode, *h_inode;
9763+
9764+ AuDebugOn(d_really_is_negative(dentry));
9765+
9766+ h_dentry = NULL;
9767+ if (au_dbtop(dentry) <= bindex
9768+ && bindex <= au_dbbot(dentry))
9769+ h_dentry = au_h_dptr(dentry, bindex);
9770+ if (h_dentry && !au_d_linkable(h_dentry)) {
9771+ dget(h_dentry);
9772+ goto out; /* success */
9773+ }
9774+
9775+ inode = d_inode(dentry);
9776+ AuDebugOn(bindex < au_ibtop(inode));
9777+ AuDebugOn(au_ibbot(inode) < bindex);
9778+ h_inode = au_h_iptr(inode, bindex);
9779+ h_dentry = d_find_alias(h_inode);
9780+ if (h_dentry) {
9781+ if (!IS_ERR(h_dentry)) {
9782+ if (!au_d_linkable(h_dentry))
9783+ goto out; /* success */
9784+ dput(h_dentry);
9785+ } else
9786+ goto out;
9787+ }
9788+
9789+ if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9790+ h_dentry = au_plink_lkup(inode, bindex);
9791+ AuDebugOn(!h_dentry);
9792+ if (!IS_ERR(h_dentry)) {
9793+ if (!au_d_hashed_positive(h_dentry))
9794+ goto out; /* success */
9795+ dput(h_dentry);
9796+ h_dentry = NULL;
9797+ }
9798+ }
9799+
9800+out:
9801+ AuDbgDentry(h_dentry);
9802+ return h_dentry;
9803+}
9804+
9805+aufs_bindex_t au_dbtail(struct dentry *dentry)
9806+{
9807+ aufs_bindex_t bbot, bwh;
9808+
9809+ bbot = au_dbbot(dentry);
9810+ if (0 <= bbot) {
9811+ bwh = au_dbwh(dentry);
9812+ if (!bwh)
9813+ return bwh;
9814+ if (0 < bwh && bwh < bbot)
9815+ return bwh - 1;
9816+ }
9817+ return bbot;
9818+}
9819+
9820+aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9821+{
9822+ aufs_bindex_t bbot, bopq;
9823+
9824+ bbot = au_dbtail(dentry);
9825+ if (0 <= bbot) {
9826+ bopq = au_dbdiropq(dentry);
9827+ if (0 <= bopq && bopq < bbot)
9828+ bbot = bopq;
9829+ }
9830+ return bbot;
9831+}
9832+
9833+/* ---------------------------------------------------------------------- */
9834+
9835+void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9836+ struct dentry *h_dentry)
9837+{
9838+ struct au_dinfo *dinfo;
9839+ struct au_hdentry *hd;
9840+ struct au_branch *br;
9841+
9842+ DiMustWriteLock(dentry);
9843+
9844+ dinfo = au_di(dentry);
9845+ hd = au_hdentry(dinfo, bindex);
9846+ au_hdput(hd);
9847+ hd->hd_dentry = h_dentry;
9848+ if (h_dentry) {
9849+ br = au_sbr(dentry->d_sb, bindex);
9850+ hd->hd_id = br->br_id;
9851+ }
9852+}
9853+
9854+int au_dbrange_test(struct dentry *dentry)
9855+{
9856+ int err;
9857+ aufs_bindex_t btop, bbot;
9858+
9859+ err = 0;
9860+ btop = au_dbtop(dentry);
9861+ bbot = au_dbbot(dentry);
9862+ if (btop >= 0)
9863+ AuDebugOn(bbot < 0 && btop > bbot);
9864+ else {
9865+ err = -EIO;
9866+ AuDebugOn(bbot >= 0);
9867+ }
9868+
9869+ return err;
9870+}
9871+
9872+int au_digen_test(struct dentry *dentry, unsigned int sigen)
9873+{
9874+ int err;
9875+
9876+ err = 0;
9877+ if (unlikely(au_digen(dentry) != sigen
9878+ || au_iigen_test(d_inode(dentry), sigen)))
9879+ err = -EIO;
9880+
9881+ return err;
9882+}
9883+
9884+void au_update_digen(struct dentry *dentry)
9885+{
9886+ atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
9887+ /* smp_mb(); */ /* atomic_set */
9888+}
9889+
9890+void au_update_dbrange(struct dentry *dentry, int do_put_zero)
9891+{
9892+ struct au_dinfo *dinfo;
9893+ struct dentry *h_d;
9894+ struct au_hdentry *hdp;
9895+ aufs_bindex_t bindex, bbot;
9896+
9897+ DiMustWriteLock(dentry);
9898+
9899+ dinfo = au_di(dentry);
9900+ if (!dinfo || dinfo->di_btop < 0)
9901+ return;
9902+
9903+ if (do_put_zero) {
9904+ bbot = dinfo->di_bbot;
9905+ bindex = dinfo->di_btop;
9906+ hdp = au_hdentry(dinfo, bindex);
9907+ for (; bindex <= bbot; bindex++, hdp++) {
9908+ h_d = hdp->hd_dentry;
9909+ if (h_d && d_is_negative(h_d))
9910+ au_set_h_dptr(dentry, bindex, NULL);
9911+ }
9912+ }
9913+
9914+ dinfo->di_btop = 0;
9915+ hdp = au_hdentry(dinfo, dinfo->di_btop);
9916+ for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
9917+ if (hdp->hd_dentry)
9918+ break;
9919+ if (dinfo->di_btop > dinfo->di_bbot) {
9920+ dinfo->di_btop = -1;
9921+ dinfo->di_bbot = -1;
9922+ return;
9923+ }
9924+
9925+ hdp = au_hdentry(dinfo, dinfo->di_bbot);
9926+ for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
9927+ if (hdp->hd_dentry)
9928+ break;
9929+ AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
9930+}
9931+
9932+void au_update_dbtop(struct dentry *dentry)
9933+{
9934+ aufs_bindex_t bindex, bbot;
9935+ struct dentry *h_dentry;
9936+
9937+ bbot = au_dbbot(dentry);
9938+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
9939+ h_dentry = au_h_dptr(dentry, bindex);
9940+ if (!h_dentry)
9941+ continue;
9942+ if (d_is_positive(h_dentry)) {
9943+ au_set_dbtop(dentry, bindex);
9944+ return;
9945+ }
9946+ au_set_h_dptr(dentry, bindex, NULL);
9947+ }
9948+}
9949+
9950+void au_update_dbbot(struct dentry *dentry)
9951+{
9952+ aufs_bindex_t bindex, btop;
9953+ struct dentry *h_dentry;
9954+
9955+ btop = au_dbtop(dentry);
9956+ for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
9957+ h_dentry = au_h_dptr(dentry, bindex);
9958+ if (!h_dentry)
9959+ continue;
9960+ if (d_is_positive(h_dentry)) {
9961+ au_set_dbbot(dentry, bindex);
9962+ return;
9963+ }
9964+ au_set_h_dptr(dentry, bindex, NULL);
9965+ }
9966+}
9967+
9968+int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
9969+{
9970+ aufs_bindex_t bindex, bbot;
9971+
9972+ bbot = au_dbbot(dentry);
9973+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
9974+ if (au_h_dptr(dentry, bindex) == h_dentry)
9975+ return bindex;
9976+ return -1;
9977+}
9978diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
9979--- /usr/share/empty/fs/aufs/dir.c 1970-01-01 01:00:00.000000000 +0100
9980+++ linux/fs/aufs/dir.c 2021-06-30 21:35:11.393873211 +0200
9981@@ -0,0 +1,765 @@
9982+// SPDX-License-Identifier: GPL-2.0
9983+/*
9984+ * Copyright (C) 2005-2020 Junjiro R. Okajima
9985+ *
9986+ * This program, aufs is free software; you can redistribute it and/or modify
9987+ * it under the terms of the GNU General Public License as published by
9988+ * the Free Software Foundation; either version 2 of the License, or
9989+ * (at your option) any later version.
9990+ *
9991+ * This program is distributed in the hope that it will be useful,
9992+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9993+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9994+ * GNU General Public License for more details.
9995+ *
9996+ * You should have received a copy of the GNU General Public License
9997+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
9998+ */
9999+
10000+/*
10001+ * directory operations
10002+ */
10003+
10004+#include <linux/fs_stack.h>
10005+#include <linux/iversion.h>
10006+#include "aufs.h"
10007+
10008+void au_add_nlink(struct inode *dir, struct inode *h_dir)
10009+{
10010+ unsigned int nlink;
10011+
10012+ AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10013+
10014+ nlink = dir->i_nlink;
10015+ nlink += h_dir->i_nlink - 2;
10016+ if (h_dir->i_nlink < 2)
10017+ nlink += 2;
10018+ smp_mb(); /* for i_nlink */
10019+ /* 0 can happen in revaliding */
10020+ set_nlink(dir, nlink);
10021+}
10022+
10023+void au_sub_nlink(struct inode *dir, struct inode *h_dir)
10024+{
10025+ unsigned int nlink;
10026+
10027+ AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10028+
10029+ nlink = dir->i_nlink;
10030+ nlink -= h_dir->i_nlink - 2;
10031+ if (h_dir->i_nlink < 2)
10032+ nlink -= 2;
10033+ smp_mb(); /* for i_nlink */
10034+ /* nlink == 0 means the branch-fs is broken */
10035+ set_nlink(dir, nlink);
10036+}
10037+
10038+loff_t au_dir_size(struct file *file, struct dentry *dentry)
10039+{
10040+ loff_t sz;
10041+ aufs_bindex_t bindex, bbot;
10042+ struct file *h_file;
10043+ struct dentry *h_dentry;
10044+
10045+ sz = 0;
10046+ if (file) {
10047+ AuDebugOn(!d_is_dir(file->f_path.dentry));
10048+
10049+ bbot = au_fbbot_dir(file);
10050+ for (bindex = au_fbtop(file);
10051+ bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10052+ bindex++) {
10053+ h_file = au_hf_dir(file, bindex);
10054+ if (h_file && file_inode(h_file))
10055+ sz += vfsub_f_size_read(h_file);
10056+ }
10057+ } else {
10058+ AuDebugOn(!dentry);
10059+ AuDebugOn(!d_is_dir(dentry));
10060+
10061+ bbot = au_dbtaildir(dentry);
10062+ for (bindex = au_dbtop(dentry);
10063+ bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10064+ bindex++) {
10065+ h_dentry = au_h_dptr(dentry, bindex);
10066+ if (h_dentry && d_is_positive(h_dentry))
10067+ sz += i_size_read(d_inode(h_dentry));
10068+ }
10069+ }
10070+ if (sz < KMALLOC_MAX_SIZE)
10071+ sz = roundup_pow_of_two(sz);
10072+ if (sz > KMALLOC_MAX_SIZE)
10073+ sz = KMALLOC_MAX_SIZE;
10074+ else if (sz < NAME_MAX) {
10075+ BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
10076+ sz = AUFS_RDBLK_DEF;
10077+ }
10078+ return sz;
10079+}
10080+
10081+struct au_dir_ts_arg {
10082+ struct dentry *dentry;
10083+ aufs_bindex_t brid;
10084+};
10085+
10086+static void au_do_dir_ts(void *arg)
10087+{
10088+ struct au_dir_ts_arg *a = arg;
10089+ struct au_dtime dt;
10090+ struct path h_path;
10091+ struct inode *dir, *h_dir;
10092+ struct super_block *sb;
10093+ struct au_branch *br;
10094+ struct au_hinode *hdir;
10095+ int err;
10096+ aufs_bindex_t btop, bindex;
10097+
10098+ sb = a->dentry->d_sb;
10099+ if (d_really_is_negative(a->dentry))
10100+ goto out;
10101+ /* no dir->i_mutex lock */
10102+ aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
10103+
10104+ dir = d_inode(a->dentry);
10105+ btop = au_ibtop(dir);
10106+ bindex = au_br_index(sb, a->brid);
10107+ if (bindex < btop)
10108+ goto out_unlock;
10109+
10110+ br = au_sbr(sb, bindex);
10111+ h_path.dentry = au_h_dptr(a->dentry, bindex);
10112+ if (!h_path.dentry)
10113+ goto out_unlock;
10114+ h_path.mnt = au_br_mnt(br);
10115+ au_dtime_store(&dt, a->dentry, &h_path);
10116+
10117+ br = au_sbr(sb, btop);
10118+ if (!au_br_writable(br->br_perm))
10119+ goto out_unlock;
10120+ h_path.dentry = au_h_dptr(a->dentry, btop);
10121+ h_path.mnt = au_br_mnt(br);
10122+ err = vfsub_mnt_want_write(h_path.mnt);
10123+ if (err)
10124+ goto out_unlock;
10125+ hdir = au_hi(dir, btop);
10126+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
10127+ h_dir = au_h_iptr(dir, btop);
10128+ if (h_dir->i_nlink
10129+ && timespec64_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
10130+ dt.dt_h_path = h_path;
10131+ au_dtime_revert(&dt);
10132+ }
10133+ au_hn_inode_unlock(hdir);
10134+ vfsub_mnt_drop_write(h_path.mnt);
10135+ au_cpup_attr_timesizes(dir);
10136+
10137+out_unlock:
10138+ aufs_read_unlock(a->dentry, AuLock_DW);
10139+out:
10140+ dput(a->dentry);
10141+ au_nwt_done(&au_sbi(sb)->si_nowait);
10142+ au_kfree_try_rcu(arg);
10143+}
10144+
10145+void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
10146+{
10147+ int perm, wkq_err;
10148+ aufs_bindex_t btop;
10149+ struct au_dir_ts_arg *arg;
10150+ struct dentry *dentry;
10151+ struct super_block *sb;
10152+
10153+ IMustLock(dir);
10154+
10155+ dentry = d_find_any_alias(dir);
10156+ AuDebugOn(!dentry);
10157+ sb = dentry->d_sb;
10158+ btop = au_ibtop(dir);
10159+ if (btop == bindex) {
10160+ au_cpup_attr_timesizes(dir);
10161+ goto out;
10162+ }
10163+
10164+ perm = au_sbr_perm(sb, btop);
10165+ if (!au_br_writable(perm))
10166+ goto out;
10167+
10168+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
10169+ if (!arg)
10170+ goto out;
10171+
10172+ arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
10173+ arg->brid = au_sbr_id(sb, bindex);
10174+ wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
10175+ if (unlikely(wkq_err)) {
10176+ pr_err("wkq %d\n", wkq_err);
10177+ dput(dentry);
10178+ au_kfree_try_rcu(arg);
10179+ }
10180+
10181+out:
10182+ dput(dentry);
10183+}
10184+
10185+/* ---------------------------------------------------------------------- */
10186+
10187+static int reopen_dir(struct file *file)
10188+{
10189+ int err;
10190+ unsigned int flags;
10191+ aufs_bindex_t bindex, btail, btop;
10192+ struct dentry *dentry, *h_dentry;
10193+ struct file *h_file;
10194+
10195+ /* open all lower dirs */
10196+ dentry = file->f_path.dentry;
10197+ btop = au_dbtop(dentry);
10198+ for (bindex = au_fbtop(file); bindex < btop; bindex++)
10199+ au_set_h_fptr(file, bindex, NULL);
10200+ au_set_fbtop(file, btop);
10201+
10202+ btail = au_dbtaildir(dentry);
10203+ for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
10204+ au_set_h_fptr(file, bindex, NULL);
10205+ au_set_fbbot_dir(file, btail);
10206+
10207+ flags = vfsub_file_flags(file);
10208+ for (bindex = btop; bindex <= btail; bindex++) {
10209+ h_dentry = au_h_dptr(dentry, bindex);
10210+ if (!h_dentry)
10211+ continue;
10212+ h_file = au_hf_dir(file, bindex);
10213+ if (h_file)
10214+ continue;
10215+
10216+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10217+ err = PTR_ERR(h_file);
10218+ if (IS_ERR(h_file))
10219+ goto out; /* close all? */
10220+ au_set_h_fptr(file, bindex, h_file);
10221+ }
10222+ au_update_figen(file);
10223+ /* todo: necessary? */
10224+ /* file->f_ra = h_file->f_ra; */
10225+ err = 0;
10226+
10227+out:
10228+ return err;
10229+}
10230+
10231+static int do_open_dir(struct file *file, int flags, struct file *h_file)
10232+{
10233+ int err;
10234+ aufs_bindex_t bindex, btail;
10235+ struct dentry *dentry, *h_dentry;
10236+ struct vfsmount *mnt;
10237+
10238+ FiMustWriteLock(file);
10239+ AuDebugOn(h_file);
10240+
10241+ err = 0;
10242+ mnt = file->f_path.mnt;
10243+ dentry = file->f_path.dentry;
10244+ file->f_version = inode_query_iversion(d_inode(dentry));
10245+ bindex = au_dbtop(dentry);
10246+ au_set_fbtop(file, bindex);
10247+ btail = au_dbtaildir(dentry);
10248+ au_set_fbbot_dir(file, btail);
10249+ for (; !err && bindex <= btail; bindex++) {
10250+ h_dentry = au_h_dptr(dentry, bindex);
10251+ if (!h_dentry)
10252+ continue;
10253+
10254+ err = vfsub_test_mntns(mnt, h_dentry->d_sb);
10255+ if (unlikely(err))
10256+ break;
10257+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10258+ if (IS_ERR(h_file)) {
10259+ err = PTR_ERR(h_file);
10260+ break;
10261+ }
10262+ au_set_h_fptr(file, bindex, h_file);
10263+ }
10264+ au_update_figen(file);
10265+ /* todo: necessary? */
10266+ /* file->f_ra = h_file->f_ra; */
10267+ if (!err)
10268+ return 0; /* success */
10269+
10270+ /* close all */
10271+ for (bindex = au_fbtop(file); bindex <= btail; bindex++)
10272+ au_set_h_fptr(file, bindex, NULL);
10273+ au_set_fbtop(file, -1);
10274+ au_set_fbbot_dir(file, -1);
10275+
10276+ return err;
10277+}
10278+
10279+static int aufs_open_dir(struct inode *inode __maybe_unused,
10280+ struct file *file)
10281+{
10282+ int err;
10283+ struct super_block *sb;
10284+ struct au_fidir *fidir;
10285+
10286+ err = -ENOMEM;
10287+ sb = file->f_path.dentry->d_sb;
10288+ si_read_lock(sb, AuLock_FLUSH);
10289+ fidir = au_fidir_alloc(sb);
10290+ if (fidir) {
10291+ struct au_do_open_args args = {
10292+ .open = do_open_dir,
10293+ .fidir = fidir
10294+ };
10295+ err = au_do_open(file, &args);
10296+ if (unlikely(err))
10297+ au_kfree_rcu(fidir);
10298+ }
10299+ si_read_unlock(sb);
10300+ return err;
10301+}
10302+
10303+static int aufs_release_dir(struct inode *inode __maybe_unused,
10304+ struct file *file)
10305+{
10306+ struct au_vdir *vdir_cache;
10307+ struct au_finfo *finfo;
10308+ struct au_fidir *fidir;
10309+ struct au_hfile *hf;
10310+ aufs_bindex_t bindex, bbot;
10311+
10312+ finfo = au_fi(file);
10313+ fidir = finfo->fi_hdir;
10314+ if (fidir) {
10315+ au_hbl_del(&finfo->fi_hlist,
10316+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
10317+ vdir_cache = fidir->fd_vdir_cache; /* lock-free */
10318+ if (vdir_cache)
10319+ au_vdir_free(vdir_cache);
10320+
10321+ bindex = finfo->fi_btop;
10322+ if (bindex >= 0) {
10323+ hf = fidir->fd_hfile + bindex;
10324+ /*
10325+ * calls fput() instead of filp_close(),
10326+ * since no dnotify or lock for the lower file.
10327+ */
10328+ bbot = fidir->fd_bbot;
10329+ for (; bindex <= bbot; bindex++, hf++)
10330+ if (hf->hf_file)
10331+ au_hfput(hf, /*execed*/0);
10332+ }
10333+ au_kfree_rcu(fidir);
10334+ finfo->fi_hdir = NULL;
10335+ }
10336+ au_finfo_fin(file);
10337+ return 0;
10338+}
10339+
10340+/* ---------------------------------------------------------------------- */
10341+
10342+static int au_do_flush_dir(struct file *file, fl_owner_t id)
10343+{
10344+ int err;
10345+ aufs_bindex_t bindex, bbot;
10346+ struct file *h_file;
10347+
10348+ err = 0;
10349+ bbot = au_fbbot_dir(file);
10350+ for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10351+ h_file = au_hf_dir(file, bindex);
10352+ if (h_file)
10353+ err = vfsub_flush(h_file, id);
10354+ }
10355+ return err;
10356+}
10357+
10358+static int aufs_flush_dir(struct file *file, fl_owner_t id)
10359+{
10360+ return au_do_flush(file, id, au_do_flush_dir);
10361+}
10362+
10363+/* ---------------------------------------------------------------------- */
10364+
10365+static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
10366+{
10367+ int err;
10368+ aufs_bindex_t bbot, bindex;
10369+ struct inode *inode;
10370+ struct super_block *sb;
10371+
10372+ err = 0;
10373+ sb = dentry->d_sb;
10374+ inode = d_inode(dentry);
10375+ IMustLock(inode);
10376+ bbot = au_dbbot(dentry);
10377+ for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
10378+ struct path h_path;
10379+
10380+ if (au_test_ro(sb, bindex, inode))
10381+ continue;
10382+ h_path.dentry = au_h_dptr(dentry, bindex);
10383+ if (!h_path.dentry)
10384+ continue;
10385+
10386+ h_path.mnt = au_sbr_mnt(sb, bindex);
10387+ err = vfsub_fsync(NULL, &h_path, datasync);
10388+ }
10389+
10390+ return err;
10391+}
10392+
10393+static int au_do_fsync_dir(struct file *file, int datasync)
10394+{
10395+ int err;
10396+ aufs_bindex_t bbot, bindex;
10397+ struct file *h_file;
10398+ struct super_block *sb;
10399+ struct inode *inode;
10400+
10401+ err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10402+ if (unlikely(err))
10403+ goto out;
10404+
10405+ inode = file_inode(file);
10406+ sb = inode->i_sb;
10407+ bbot = au_fbbot_dir(file);
10408+ for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10409+ h_file = au_hf_dir(file, bindex);
10410+ if (!h_file || au_test_ro(sb, bindex, inode))
10411+ continue;
10412+
10413+ err = vfsub_fsync(h_file, &h_file->f_path, datasync);
10414+ }
10415+
10416+out:
10417+ return err;
10418+}
10419+
10420+/*
10421+ * @file may be NULL
10422+ */
10423+static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
10424+ int datasync)
10425+{
10426+ int err;
10427+ struct dentry *dentry;
10428+ struct inode *inode;
10429+ struct super_block *sb;
10430+
10431+ err = 0;
10432+ dentry = file->f_path.dentry;
10433+ inode = d_inode(dentry);
10434+ inode_lock(inode);
10435+ sb = dentry->d_sb;
10436+ si_noflush_read_lock(sb);
10437+ if (file)
10438+ err = au_do_fsync_dir(file, datasync);
10439+ else {
10440+ di_write_lock_child(dentry);
10441+ err = au_do_fsync_dir_no_file(dentry, datasync);
10442+ }
10443+ au_cpup_attr_timesizes(inode);
10444+ di_write_unlock(dentry);
10445+ if (file)
10446+ fi_write_unlock(file);
10447+
10448+ si_read_unlock(sb);
10449+ inode_unlock(inode);
10450+ return err;
10451+}
10452+
10453+/* ---------------------------------------------------------------------- */
10454+
10455+static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
10456+{
10457+ int err;
10458+ struct dentry *dentry;
10459+ struct inode *inode, *h_inode;
10460+ struct super_block *sb;
10461+
10462+ AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
10463+
10464+ dentry = file->f_path.dentry;
10465+ inode = d_inode(dentry);
10466+ IMustLock(inode);
10467+
10468+ sb = dentry->d_sb;
10469+ si_read_lock(sb, AuLock_FLUSH);
10470+ err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10471+ if (unlikely(err))
10472+ goto out;
10473+ err = au_alive_dir(dentry);
10474+ if (!err)
10475+ err = au_vdir_init(file);
10476+ di_downgrade_lock(dentry, AuLock_IR);
10477+ if (unlikely(err))
10478+ goto out_unlock;
10479+
10480+ h_inode = au_h_iptr(inode, au_ibtop(inode));
10481+ if (!au_test_nfsd()) {
10482+ err = au_vdir_fill_de(file, ctx);
10483+ fsstack_copy_attr_atime(inode, h_inode);
10484+ } else {
10485+ /*
10486+ * nfsd filldir may call lookup_one_len(), vfs_getattr(),
10487+ * encode_fh() and others.
10488+ */
10489+ atomic_inc(&h_inode->i_count);
10490+ di_read_unlock(dentry, AuLock_IR);
10491+ si_read_unlock(sb);
10492+ err = au_vdir_fill_de(file, ctx);
10493+ fsstack_copy_attr_atime(inode, h_inode);
10494+ fi_write_unlock(file);
10495+ iput(h_inode);
10496+
10497+ AuTraceErr(err);
10498+ return err;
10499+ }
10500+
10501+out_unlock:
10502+ di_read_unlock(dentry, AuLock_IR);
10503+ fi_write_unlock(file);
10504+out:
10505+ si_read_unlock(sb);
10506+ return err;
10507+}
10508+
10509+/* ---------------------------------------------------------------------- */
10510+
10511+#define AuTestEmpty_WHONLY 1
10512+#define AuTestEmpty_CALLED (1 << 1)
10513+#define AuTestEmpty_SHWH (1 << 2)
10514+#define au_ftest_testempty(flags, name) ((flags) & AuTestEmpty_##name)
10515+#define au_fset_testempty(flags, name) \
10516+ do { (flags) |= AuTestEmpty_##name; } while (0)
10517+#define au_fclr_testempty(flags, name) \
10518+ do { (flags) &= ~AuTestEmpty_##name; } while (0)
10519+
10520+#ifndef CONFIG_AUFS_SHWH
10521+#undef AuTestEmpty_SHWH
10522+#define AuTestEmpty_SHWH 0
10523+#endif
10524+
10525+struct test_empty_arg {
10526+ struct dir_context ctx;
10527+ struct au_nhash *whlist;
10528+ unsigned int flags;
10529+ int err;
10530+ aufs_bindex_t bindex;
10531+};
10532+
10533+static int test_empty_cb(struct dir_context *ctx, const char *__name,
10534+ int namelen, loff_t offset __maybe_unused, u64 ino,
10535+ unsigned int d_type)
10536+{
10537+ struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10538+ ctx);
10539+ char *name = (void *)__name;
10540+
10541+ arg->err = 0;
10542+ au_fset_testempty(arg->flags, CALLED);
10543+ /* smp_mb(); */
10544+ if (name[0] == '.'
10545+ && (namelen == 1 || (name[1] == '.' && namelen == 2)))
10546+ goto out; /* success */
10547+
10548+ if (namelen <= AUFS_WH_PFX_LEN
10549+ || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10550+ if (au_ftest_testempty(arg->flags, WHONLY)
10551+ && !au_nhash_test_known_wh(arg->whlist, name, namelen))
10552+ arg->err = -ENOTEMPTY;
10553+ goto out;
10554+ }
10555+
10556+ name += AUFS_WH_PFX_LEN;
10557+ namelen -= AUFS_WH_PFX_LEN;
10558+ if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
10559+ arg->err = au_nhash_append_wh
10560+ (arg->whlist, name, namelen, ino, d_type, arg->bindex,
10561+ au_ftest_testempty(arg->flags, SHWH));
10562+
10563+out:
10564+ /* smp_mb(); */
10565+ AuTraceErr(arg->err);
10566+ return arg->err;
10567+}
10568+
10569+static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10570+{
10571+ int err;
10572+ struct file *h_file;
10573+ struct au_branch *br;
10574+
10575+ h_file = au_h_open(dentry, arg->bindex,
10576+ O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
10577+ /*file*/NULL, /*force_wr*/0);
10578+ err = PTR_ERR(h_file);
10579+ if (IS_ERR(h_file))
10580+ goto out;
10581+
10582+ err = 0;
10583+ if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
10584+ && !file_inode(h_file)->i_nlink)
10585+ goto out_put;
10586+
10587+ do {
10588+ arg->err = 0;
10589+ au_fclr_testempty(arg->flags, CALLED);
10590+ /* smp_mb(); */
10591+ err = vfsub_iterate_dir(h_file, &arg->ctx);
10592+ if (err >= 0)
10593+ err = arg->err;
10594+ } while (!err && au_ftest_testempty(arg->flags, CALLED));
10595+
10596+out_put:
10597+ fput(h_file);
10598+ br = au_sbr(dentry->d_sb, arg->bindex);
10599+ au_lcnt_dec(&br->br_nfiles);
10600+out:
10601+ return err;
10602+}
10603+
10604+struct do_test_empty_args {
10605+ int *errp;
10606+ struct dentry *dentry;
10607+ struct test_empty_arg *arg;
10608+};
10609+
10610+static void call_do_test_empty(void *args)
10611+{
10612+ struct do_test_empty_args *a = args;
10613+ *a->errp = do_test_empty(a->dentry, a->arg);
10614+}
10615+
10616+static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10617+{
10618+ int err, wkq_err;
10619+ struct dentry *h_dentry;
10620+ struct inode *h_inode;
10621+ struct user_namespace *h_userns;
10622+
10623+ h_userns = au_sbr_userns(dentry->d_sb, arg->bindex);
10624+ h_dentry = au_h_dptr(dentry, arg->bindex);
10625+ h_inode = d_inode(h_dentry);
10626+ /* todo: i_mode changes anytime? */
10627+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
10628+ err = au_test_h_perm_sio(h_userns, h_inode, MAY_EXEC | MAY_READ);
10629+ inode_unlock_shared(h_inode);
10630+ if (!err)
10631+ err = do_test_empty(dentry, arg);
10632+ else {
10633+ struct do_test_empty_args args = {
10634+ .errp = &err,
10635+ .dentry = dentry,
10636+ .arg = arg
10637+ };
10638+ unsigned int flags = arg->flags;
10639+
10640+ wkq_err = au_wkq_wait(call_do_test_empty, &args);
10641+ if (unlikely(wkq_err))
10642+ err = wkq_err;
10643+ arg->flags = flags;
10644+ }
10645+
10646+ return err;
10647+}
10648+
10649+int au_test_empty_lower(struct dentry *dentry)
10650+{
10651+ int err;
10652+ unsigned int rdhash;
10653+ aufs_bindex_t bindex, btop, btail;
10654+ struct au_nhash whlist;
10655+ struct test_empty_arg arg = {
10656+ .ctx = {
10657+ .actor = test_empty_cb
10658+ }
10659+ };
10660+ int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
10661+
10662+ SiMustAnyLock(dentry->d_sb);
10663+
10664+ rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10665+ if (!rdhash)
10666+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10667+ err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
10668+ if (unlikely(err))
10669+ goto out;
10670+
10671+ arg.flags = 0;
10672+ arg.whlist = &whlist;
10673+ btop = au_dbtop(dentry);
10674+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10675+ au_fset_testempty(arg.flags, SHWH);
10676+ test_empty = do_test_empty;
10677+ if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10678+ test_empty = sio_test_empty;
10679+ arg.bindex = btop;
10680+ err = test_empty(dentry, &arg);
10681+ if (unlikely(err))
10682+ goto out_whlist;
10683+
10684+ au_fset_testempty(arg.flags, WHONLY);
10685+ btail = au_dbtaildir(dentry);
10686+ for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
10687+ struct dentry *h_dentry;
10688+
10689+ h_dentry = au_h_dptr(dentry, bindex);
10690+ if (h_dentry && d_is_positive(h_dentry)) {
10691+ arg.bindex = bindex;
10692+ err = test_empty(dentry, &arg);
10693+ }
10694+ }
10695+
10696+out_whlist:
10697+ au_nhash_wh_free(&whlist);
10698+out:
10699+ return err;
10700+}
10701+
10702+int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10703+{
10704+ int err;
10705+ struct test_empty_arg arg = {
10706+ .ctx = {
10707+ .actor = test_empty_cb
10708+ }
10709+ };
10710+ aufs_bindex_t bindex, btail;
10711+
10712+ err = 0;
10713+ arg.whlist = whlist;
10714+ arg.flags = AuTestEmpty_WHONLY;
10715+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10716+ au_fset_testempty(arg.flags, SHWH);
10717+ btail = au_dbtaildir(dentry);
10718+ for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
10719+ struct dentry *h_dentry;
10720+
10721+ h_dentry = au_h_dptr(dentry, bindex);
10722+ if (h_dentry && d_is_positive(h_dentry)) {
10723+ arg.bindex = bindex;
10724+ err = sio_test_empty(dentry, &arg);
10725+ }
10726+ }
10727+
10728+ return err;
10729+}
10730+
10731+/* ---------------------------------------------------------------------- */
10732+
10733+const struct file_operations aufs_dir_fop = {
10734+ .owner = THIS_MODULE,
10735+ .llseek = default_llseek,
10736+ .read = generic_read_dir,
10737+ .iterate_shared = aufs_iterate_shared,
10738+ .unlocked_ioctl = aufs_ioctl_dir,
10739+#ifdef CONFIG_COMPAT
10740+ .compat_ioctl = aufs_compat_ioctl_dir,
10741+#endif
10742+ .open = aufs_open_dir,
10743+ .release = aufs_release_dir,
10744+ .flush = aufs_flush_dir,
10745+ .fsync = aufs_fsync_dir
10746+};
10747diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
10748--- /usr/share/empty/fs/aufs/dir.h 1970-01-01 01:00:00.000000000 +0100
10749+++ linux/fs/aufs/dir.h 2021-02-24 13:33:42.741013619 +0100
10750@@ -0,0 +1,134 @@
10751+/* SPDX-License-Identifier: GPL-2.0 */
10752+/*
10753+ * Copyright (C) 2005-2020 Junjiro R. Okajima
10754+ *
10755+ * This program, aufs is free software; you can redistribute it and/or modify
10756+ * it under the terms of the GNU General Public License as published by
10757+ * the Free Software Foundation; either version 2 of the License, or
10758+ * (at your option) any later version.
10759+ *
10760+ * This program is distributed in the hope that it will be useful,
10761+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10762+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10763+ * GNU General Public License for more details.
10764+ *
10765+ * You should have received a copy of the GNU General Public License
10766+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
10767+ */
10768+
10769+/*
10770+ * directory operations
10771+ */
10772+
10773+#ifndef __AUFS_DIR_H__
10774+#define __AUFS_DIR_H__
10775+
10776+#ifdef __KERNEL__
10777+
10778+#include <linux/fs.h>
10779+
10780+/* ---------------------------------------------------------------------- */
10781+
10782+/* need to be faster and smaller */
10783+
10784+struct au_nhash {
10785+ unsigned int nh_num;
10786+ struct hlist_head *nh_head;
10787+};
10788+
10789+struct au_vdir_destr {
10790+ unsigned char len;
10791+ unsigned char name[];
10792+} __packed;
10793+
10794+struct au_vdir_dehstr {
10795+ struct hlist_node hash;
10796+ struct au_vdir_destr *str;
10797+ struct rcu_head rcu;
10798+} ____cacheline_aligned_in_smp;
10799+
10800+struct au_vdir_de {
10801+ ino_t de_ino;
10802+ unsigned char de_type;
10803+ /* caution: packed */
10804+ struct au_vdir_destr de_str;
10805+} __packed;
10806+
10807+struct au_vdir_wh {
10808+ struct hlist_node wh_hash;
10809+#ifdef CONFIG_AUFS_SHWH
10810+ ino_t wh_ino;
10811+ aufs_bindex_t wh_bindex;
10812+ unsigned char wh_type;
10813+#else
10814+ aufs_bindex_t wh_bindex;
10815+#endif
10816+ /* caution: packed */
10817+ struct au_vdir_destr wh_str;
10818+} __packed;
10819+
10820+union au_vdir_deblk_p {
10821+ unsigned char *deblk;
10822+ struct au_vdir_de *de;
10823+};
10824+
10825+struct au_vdir {
10826+ unsigned char **vd_deblk;
10827+ unsigned long vd_nblk;
10828+ struct {
10829+ unsigned long ul;
10830+ union au_vdir_deblk_p p;
10831+ } vd_last;
10832+
10833+ u64 vd_version;
10834+ unsigned int vd_deblk_sz;
10835+ unsigned long vd_jiffy;
10836+ struct rcu_head rcu;
10837+} ____cacheline_aligned_in_smp;
10838+
10839+/* ---------------------------------------------------------------------- */
10840+
10841+/* dir.c */
10842+extern const struct file_operations aufs_dir_fop;
10843+void au_add_nlink(struct inode *dir, struct inode *h_dir);
10844+void au_sub_nlink(struct inode *dir, struct inode *h_dir);
10845+loff_t au_dir_size(struct file *file, struct dentry *dentry);
10846+void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
10847+int au_test_empty_lower(struct dentry *dentry);
10848+int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10849+
10850+/* vdir.c */
10851+unsigned int au_rdhash_est(loff_t sz);
10852+int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
10853+void au_nhash_wh_free(struct au_nhash *whlist);
10854+int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
10855+ int limit);
10856+int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
10857+int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
10858+ unsigned int d_type, aufs_bindex_t bindex,
10859+ unsigned char shwh);
10860+void au_vdir_free(struct au_vdir *vdir);
10861+int au_vdir_init(struct file *file);
10862+int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
10863+
10864+/* ioctl.c */
10865+long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
10866+
10867+#ifdef CONFIG_AUFS_RDU
10868+/* rdu.c */
10869+long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
10870+#ifdef CONFIG_COMPAT
10871+long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
10872+ unsigned long arg);
10873+#endif
10874+#else
10875+AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
10876+ unsigned int cmd, unsigned long arg)
10877+#ifdef CONFIG_COMPAT
10878+AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
10879+ unsigned int cmd, unsigned long arg)
10880+#endif
10881+#endif
10882+
10883+#endif /* __KERNEL__ */
10884+#endif /* __AUFS_DIR_H__ */
10885diff -urN /usr/share/empty/fs/aufs/dirren.c linux/fs/aufs/dirren.c
10886--- /usr/share/empty/fs/aufs/dirren.c 1970-01-01 01:00:00.000000000 +0100
10887+++ linux/fs/aufs/dirren.c 2021-06-30 21:35:11.393873211 +0200
10888@@ -0,0 +1,1316 @@
10889+// SPDX-License-Identifier: GPL-2.0
10890+/*
10891+ * Copyright (C) 2017-2020 Junjiro R. Okajima
10892+ *
10893+ * This program, aufs is free software; you can redistribute it and/or modify
10894+ * it under the terms of the GNU General Public License as published by
10895+ * the Free Software Foundation; either version 2 of the License, or
10896+ * (at your option) any later version.
10897+ *
10898+ * This program is distributed in the hope that it will be useful,
10899+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10900+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10901+ * GNU General Public License for more details.
10902+ *
10903+ * You should have received a copy of the GNU General Public License
10904+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
10905+ */
10906+
10907+/*
10908+ * special handling in renaming a directory
10909+ * in order to support looking-up the before-renamed name on the lower readonly
10910+ * branches
10911+ */
10912+
10913+#include <linux/byteorder/generic.h>
10914+#include "aufs.h"
10915+
10916+static void au_dr_hino_del(struct au_dr_br *dr, struct au_dr_hino *ent)
10917+{
10918+ int idx;
10919+
10920+ idx = au_dr_ihash(ent->dr_h_ino);
10921+ au_hbl_del(&ent->dr_hnode, dr->dr_h_ino + idx);
10922+}
10923+
10924+static int au_dr_hino_test_empty(struct au_dr_br *dr)
10925+{
10926+ int ret, i;
10927+ struct hlist_bl_head *hbl;
10928+
10929+ ret = 1;
10930+ for (i = 0; ret && i < AuDirren_NHASH; i++) {
10931+ hbl = dr->dr_h_ino + i;
10932+ hlist_bl_lock(hbl);
10933+ ret &= hlist_bl_empty(hbl);
10934+ hlist_bl_unlock(hbl);
10935+ }
10936+
10937+ return ret;
10938+}
10939+
10940+static struct au_dr_hino *au_dr_hino_find(struct au_dr_br *dr, ino_t ino)
10941+{
10942+ struct au_dr_hino *found, *ent;
10943+ struct hlist_bl_head *hbl;
10944+ struct hlist_bl_node *pos;
10945+ int idx;
10946+
10947+ found = NULL;
10948+ idx = au_dr_ihash(ino);
10949+ hbl = dr->dr_h_ino + idx;
10950+ hlist_bl_lock(hbl);
10951+ hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10952+ if (ent->dr_h_ino == ino) {
10953+ found = ent;
10954+ break;
10955+ }
10956+ hlist_bl_unlock(hbl);
10957+
10958+ return found;
10959+}
10960+
10961+int au_dr_hino_test_add(struct au_dr_br *dr, ino_t ino,
10962+ struct au_dr_hino *add_ent)
10963+{
10964+ int found, idx;
10965+ struct hlist_bl_head *hbl;
10966+ struct hlist_bl_node *pos;
10967+ struct au_dr_hino *ent;
10968+
10969+ found = 0;
10970+ idx = au_dr_ihash(ino);
10971+ hbl = dr->dr_h_ino + idx;
10972+#if 0 /* debug print */
10973+ {
10974+ struct hlist_bl_node *tmp;
10975+
10976+ hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
10977+ AuDbg("hi%llu\n", (unsigned long long)ent->dr_h_ino);
10978+ }
10979+#endif
10980+ hlist_bl_lock(hbl);
10981+ hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10982+ if (ent->dr_h_ino == ino) {
10983+ found = 1;
10984+ break;
10985+ }
10986+ if (!found && add_ent)
10987+ hlist_bl_add_head(&add_ent->dr_hnode, hbl);
10988+ hlist_bl_unlock(hbl);
10989+
10990+ if (!found && add_ent)
10991+ AuDbg("i%llu added\n", (unsigned long long)add_ent->dr_h_ino);
10992+
10993+ return found;
10994+}
10995+
10996+void au_dr_hino_free(struct au_dr_br *dr)
10997+{
10998+ int i;
10999+ struct hlist_bl_head *hbl;
11000+ struct hlist_bl_node *pos, *tmp;
11001+ struct au_dr_hino *ent;
11002+
11003+ /* SiMustWriteLock(sb); */
11004+
11005+ for (i = 0; i < AuDirren_NHASH; i++) {
11006+ hbl = dr->dr_h_ino + i;
11007+ /* no spinlock since sbinfo must be write-locked */
11008+ hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
11009+ au_kfree_rcu(ent);
11010+ INIT_HLIST_BL_HEAD(hbl);
11011+ }
11012+}
11013+
11014+/* returns the number of inodes or an error */
11015+static int au_dr_hino_store(struct super_block *sb, struct au_branch *br,
11016+ struct file *hinofile)
11017+{
11018+ int err, i;
11019+ ssize_t ssz;
11020+ loff_t pos, oldsize;
11021+ __be64 u64;
11022+ struct inode *hinoinode;
11023+ struct hlist_bl_head *hbl;
11024+ struct hlist_bl_node *n1, *n2;
11025+ struct au_dr_hino *ent;
11026+
11027+ SiMustWriteLock(sb);
11028+ AuDebugOn(!au_br_writable(br->br_perm));
11029+
11030+ hinoinode = file_inode(hinofile);
11031+ oldsize = i_size_read(hinoinode);
11032+
11033+ err = 0;
11034+ pos = 0;
11035+ hbl = br->br_dirren.dr_h_ino;
11036+ for (i = 0; !err && i < AuDirren_NHASH; i++, hbl++) {
11037+ /* no bit-lock since sbinfo must be write-locked */
11038+ hlist_bl_for_each_entry_safe(ent, n1, n2, hbl, dr_hnode) {
11039+ AuDbg("hi%llu, %pD2\n",
11040+ (unsigned long long)ent->dr_h_ino, hinofile);
11041+ u64 = cpu_to_be64(ent->dr_h_ino);
11042+ ssz = vfsub_write_k(hinofile, &u64, sizeof(u64), &pos);
11043+ if (ssz == sizeof(u64))
11044+ continue;
11045+
11046+ /* write error */
11047+ pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11048+ err = -ENOSPC;
11049+ if (ssz < 0)
11050+ err = ssz;
11051+ break;
11052+ }
11053+ }
11054+ /* regardless the error */
11055+ if (pos < oldsize) {
11056+ err = vfsub_trunc(&hinofile->f_path, pos, /*attr*/0, hinofile);
11057+ AuTraceErr(err);
11058+ }
11059+
11060+ AuTraceErr(err);
11061+ return err;
11062+}
11063+
11064+static int au_dr_hino_load(struct au_dr_br *dr, struct file *hinofile)
11065+{
11066+ int err, hidx;
11067+ ssize_t ssz;
11068+ size_t sz, n;
11069+ loff_t pos;
11070+ uint64_t u64;
11071+ struct au_dr_hino *ent;
11072+ struct inode *hinoinode;
11073+ struct hlist_bl_head *hbl;
11074+
11075+ err = 0;
11076+ pos = 0;
11077+ hbl = dr->dr_h_ino;
11078+ hinoinode = file_inode(hinofile);
11079+ sz = i_size_read(hinoinode);
11080+ AuDebugOn(sz % sizeof(u64));
11081+ n = sz / sizeof(u64);
11082+ while (n--) {
11083+ ssz = vfsub_read_k(hinofile, &u64, sizeof(u64), &pos);
11084+ if (unlikely(ssz != sizeof(u64))) {
11085+ pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11086+ err = -EINVAL;
11087+ if (ssz < 0)
11088+ err = ssz;
11089+ goto out_free;
11090+ }
11091+
11092+ ent = kmalloc(sizeof(*ent), GFP_NOFS);
11093+ if (!ent) {
11094+ err = -ENOMEM;
11095+ AuTraceErr(err);
11096+ goto out_free;
11097+ }
11098+ ent->dr_h_ino = be64_to_cpu((__force __be64)u64);
11099+ AuDbg("hi%llu, %pD2\n",
11100+ (unsigned long long)ent->dr_h_ino, hinofile);
11101+ hidx = au_dr_ihash(ent->dr_h_ino);
11102+ au_hbl_add(&ent->dr_hnode, hbl + hidx);
11103+ }
11104+ goto out; /* success */
11105+
11106+out_free:
11107+ au_dr_hino_free(dr);
11108+out:
11109+ AuTraceErr(err);
11110+ return err;
11111+}
11112+
11113+/*
11114+ * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
11115+ * @path is a switch to distinguish load and store.
11116+ */
11117+static int au_dr_hino(struct super_block *sb, aufs_bindex_t bindex,
11118+ struct au_branch *br, const struct path *path)
11119+{
11120+ int err, flags;
11121+ unsigned char load, suspend;
11122+ struct file *hinofile;
11123+ struct au_hinode *hdir;
11124+ struct inode *dir, *delegated;
11125+ struct path hinopath;
11126+ struct qstr hinoname = QSTR_INIT(AUFS_WH_DR_BRHINO,
11127+ sizeof(AUFS_WH_DR_BRHINO) - 1);
11128+
11129+ AuDebugOn(bindex < 0 && !br);
11130+ AuDebugOn(bindex >= 0 && br);
11131+
11132+ err = -EINVAL;
11133+ suspend = !br;
11134+ if (suspend)
11135+ br = au_sbr(sb, bindex);
11136+ load = !!path;
11137+ if (!load) {
11138+ path = &br->br_path;
11139+ AuDebugOn(!au_br_writable(br->br_perm));
11140+ if (unlikely(!au_br_writable(br->br_perm)))
11141+ goto out;
11142+ }
11143+
11144+ hdir = NULL;
11145+ if (suspend) {
11146+ dir = d_inode(sb->s_root);
11147+ hdir = au_hinode(au_ii(dir), bindex);
11148+ dir = hdir->hi_inode;
11149+ au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11150+ } else {
11151+ dir = d_inode(path->dentry);
11152+ inode_lock_nested(dir, AuLsc_I_CHILD);
11153+ }
11154+ hinopath.dentry = vfsub_lkup_one(&hinoname, path->dentry);
11155+ err = PTR_ERR(hinopath.dentry);
11156+ if (IS_ERR(hinopath.dentry))
11157+ goto out_unlock;
11158+ hinopath.mnt = path->mnt;
11159+
11160+ err = 0;
11161+ flags = O_RDONLY;
11162+ if (load) {
11163+ if (d_is_negative(hinopath.dentry))
11164+ goto out_dput; /* success */
11165+ } else {
11166+ if (au_dr_hino_test_empty(&br->br_dirren)) {
11167+ if (d_is_positive(hinopath.dentry)) {
11168+ delegated = NULL;
11169+ err = vfsub_unlink(dir, &hinopath, &delegated,
11170+ /*force*/0);
11171+ AuTraceErr(err);
11172+ if (unlikely(err))
11173+ pr_err("ignored err %d, %pd2\n",
11174+ err, hinopath.dentry);
11175+ if (unlikely(err == -EWOULDBLOCK))
11176+ iput(delegated);
11177+ err = 0;
11178+ }
11179+ goto out_dput;
11180+ } else if (!d_is_positive(hinopath.dentry)) {
11181+ err = vfsub_create(dir, &hinopath, 0600,
11182+ /*want_excl*/false);
11183+ AuTraceErr(err);
11184+ if (unlikely(err))
11185+ goto out_dput;
11186+ }
11187+ flags = O_WRONLY;
11188+ }
11189+ hinofile = vfsub_dentry_open(&hinopath, flags);
11190+ if (suspend)
11191+ au_hn_inode_unlock(hdir);
11192+ else
11193+ inode_unlock(dir);
11194+ dput(hinopath.dentry);
11195+ AuTraceErrPtr(hinofile);
11196+ if (IS_ERR(hinofile)) {
11197+ err = PTR_ERR(hinofile);
11198+ goto out;
11199+ }
11200+
11201+ if (load)
11202+ err = au_dr_hino_load(&br->br_dirren, hinofile);
11203+ else
11204+ err = au_dr_hino_store(sb, br, hinofile);
11205+ fput(hinofile);
11206+ goto out;
11207+
11208+out_dput:
11209+ dput(hinopath.dentry);
11210+out_unlock:
11211+ if (suspend)
11212+ au_hn_inode_unlock(hdir);
11213+ else
11214+ inode_unlock(dir);
11215+out:
11216+ AuTraceErr(err);
11217+ return err;
11218+}
11219+
11220+/* ---------------------------------------------------------------------- */
11221+
11222+static int au_dr_brid_init(struct au_dr_brid *brid, const struct path *path)
11223+{
11224+ int err;
11225+ struct kstatfs kstfs;
11226+ dev_t dev;
11227+ struct dentry *dentry;
11228+ struct super_block *sb;
11229+
11230+ err = vfs_statfs((void *)path, &kstfs);
11231+ AuTraceErr(err);
11232+ if (unlikely(err))
11233+ goto out;
11234+
11235+ /* todo: support for UUID */
11236+
11237+ if (kstfs.f_fsid.val[0] || kstfs.f_fsid.val[1]) {
11238+ brid->type = AuBrid_FSID;
11239+ brid->fsid = kstfs.f_fsid;
11240+ } else {
11241+ dentry = path->dentry;
11242+ sb = dentry->d_sb;
11243+ dev = sb->s_dev;
11244+ if (dev) {
11245+ brid->type = AuBrid_DEV;
11246+ brid->dev = dev;
11247+ }
11248+ }
11249+
11250+out:
11251+ return err;
11252+}
11253+
11254+int au_dr_br_init(struct super_block *sb, struct au_branch *br,
11255+ const struct path *path)
11256+{
11257+ int err, i;
11258+ struct au_dr_br *dr;
11259+ struct hlist_bl_head *hbl;
11260+
11261+ dr = &br->br_dirren;
11262+ hbl = dr->dr_h_ino;
11263+ for (i = 0; i < AuDirren_NHASH; i++, hbl++)
11264+ INIT_HLIST_BL_HEAD(hbl);
11265+
11266+ err = au_dr_brid_init(&dr->dr_brid, path);
11267+ if (unlikely(err))
11268+ goto out;
11269+
11270+ if (au_opt_test(au_mntflags(sb), DIRREN))
11271+ err = au_dr_hino(sb, /*bindex*/-1, br, path);
11272+
11273+out:
11274+ AuTraceErr(err);
11275+ return err;
11276+}
11277+
11278+int au_dr_br_fin(struct super_block *sb, struct au_branch *br)
11279+{
11280+ int err;
11281+
11282+ err = 0;
11283+ if (au_br_writable(br->br_perm))
11284+ err = au_dr_hino(sb, /*bindex*/-1, br, /*path*/NULL);
11285+ if (!err)
11286+ au_dr_hino_free(&br->br_dirren);
11287+
11288+ return err;
11289+}
11290+
11291+/* ---------------------------------------------------------------------- */
11292+
11293+static int au_brid_str(struct au_dr_brid *brid, struct inode *h_inode,
11294+ char *buf, size_t sz)
11295+{
11296+ int err;
11297+ unsigned int major, minor;
11298+ char *p;
11299+
11300+ p = buf;
11301+ err = snprintf(p, sz, "%d_", brid->type);
11302+ AuDebugOn(err > sz);
11303+ p += err;
11304+ sz -= err;
11305+ switch (brid->type) {
11306+ case AuBrid_Unset:
11307+ return -EINVAL;
11308+ case AuBrid_UUID:
11309+ err = snprintf(p, sz, "%pU", brid->uuid.b);
11310+ break;
11311+ case AuBrid_FSID:
11312+ err = snprintf(p, sz, "%08x-%08x",
11313+ brid->fsid.val[0], brid->fsid.val[1]);
11314+ break;
11315+ case AuBrid_DEV:
11316+ major = MAJOR(brid->dev);
11317+ minor = MINOR(brid->dev);
11318+ if (major <= 0xff && minor <= 0xff)
11319+ err = snprintf(p, sz, "%02x%02x", major, minor);
11320+ else
11321+ err = snprintf(p, sz, "%03x:%05x", major, minor);
11322+ break;
11323+ }
11324+ AuDebugOn(err > sz);
11325+ p += err;
11326+ sz -= err;
11327+ err = snprintf(p, sz, "_%llu", (unsigned long long)h_inode->i_ino);
11328+ AuDebugOn(err > sz);
11329+ p += err;
11330+ sz -= err;
11331+
11332+ return p - buf;
11333+}
11334+
11335+static int au_drinfo_name(struct au_branch *br, char *name, int len)
11336+{
11337+ int rlen;
11338+ struct dentry *br_dentry;
11339+ struct inode *br_inode;
11340+
11341+ br_dentry = au_br_dentry(br);
11342+ br_inode = d_inode(br_dentry);
11343+ rlen = au_brid_str(&br->br_dirren.dr_brid, br_inode, name, len);
11344+ AuDebugOn(rlen >= AUFS_DIRREN_ENV_VAL_SZ);
11345+ AuDebugOn(rlen > len);
11346+
11347+ return rlen;
11348+}
11349+
11350+/* ---------------------------------------------------------------------- */
11351+
11352+/*
11353+ * from the given @h_dentry, construct drinfo at @*fdata.
11354+ * when the size of @*fdata is not enough, reallocate and return new @fdata and
11355+ * @allocated.
11356+ */
11357+static int au_drinfo_construct(struct au_drinfo_fdata **fdata,
11358+ struct dentry *h_dentry,
11359+ unsigned char *allocated)
11360+{
11361+ int err, v;
11362+ struct au_drinfo_fdata *f, *p;
11363+ struct au_drinfo *drinfo;
11364+ struct inode *h_inode;
11365+ struct qstr *qname;
11366+
11367+ err = 0;
11368+ f = *fdata;
11369+ h_inode = d_inode(h_dentry);
11370+ qname = &h_dentry->d_name;
11371+ drinfo = &f->drinfo;
11372+ drinfo->ino = (__force uint64_t)cpu_to_be64(h_inode->i_ino);
11373+ drinfo->oldnamelen = qname->len;
11374+ if (*allocated < sizeof(*f) + qname->len) {
11375+ v = roundup_pow_of_two(*allocated + qname->len);
11376+ p = au_krealloc(f, v, GFP_NOFS, /*may_shrink*/0);
11377+ if (unlikely(!p)) {
11378+ err = -ENOMEM;
11379+ AuTraceErr(err);
11380+ goto out;
11381+ }
11382+ f = p;
11383+ *fdata = f;
11384+ *allocated = v;
11385+ drinfo = &f->drinfo;
11386+ }
11387+ memcpy(drinfo->oldname, qname->name, qname->len);
11388+ AuDbg("i%llu, %.*s\n",
11389+ be64_to_cpu((__force __be64)drinfo->ino), drinfo->oldnamelen,
11390+ drinfo->oldname);
11391+
11392+out:
11393+ AuTraceErr(err);
11394+ return err;
11395+}
11396+
11397+/* callers have to free the return value */
11398+static struct au_drinfo *au_drinfo_read_k(struct file *file, ino_t h_ino)
11399+{
11400+ struct au_drinfo *ret, *drinfo;
11401+ struct au_drinfo_fdata fdata;
11402+ int len;
11403+ loff_t pos;
11404+ ssize_t ssz;
11405+
11406+ ret = ERR_PTR(-EIO);
11407+ pos = 0;
11408+ ssz = vfsub_read_k(file, &fdata, sizeof(fdata), &pos);
11409+ if (unlikely(ssz != sizeof(fdata))) {
11410+ AuIOErr("ssz %zd, %u, %pD2\n",
11411+ ssz, (unsigned int)sizeof(fdata), file);
11412+ goto out;
11413+ }
11414+
11415+ fdata.magic = ntohl((__force __be32)fdata.magic);
11416+ switch (fdata.magic) {
11417+ case AUFS_DRINFO_MAGIC_V1:
11418+ break;
11419+ default:
11420+ AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
11421+ fdata.magic, AUFS_DRINFO_MAGIC_V1, file);
11422+ goto out;
11423+ }
11424+
11425+ drinfo = &fdata.drinfo;
11426+ len = drinfo->oldnamelen;
11427+ if (!len) {
11428+ AuIOErr("broken drinfo %pD2\n", file);
11429+ goto out;
11430+ }
11431+
11432+ ret = NULL;
11433+ drinfo->ino = be64_to_cpu((__force __be64)drinfo->ino);
11434+ if (unlikely(h_ino && drinfo->ino != h_ino)) {
11435+ AuDbg("ignored i%llu, i%llu, %pD2\n",
11436+ (unsigned long long)drinfo->ino,
11437+ (unsigned long long)h_ino, file);
11438+ goto out; /* success */
11439+ }
11440+
11441+ ret = kmalloc(sizeof(*ret) + len, GFP_NOFS);
11442+ if (unlikely(!ret)) {
11443+ ret = ERR_PTR(-ENOMEM);
11444+ AuTraceErrPtr(ret);
11445+ goto out;
11446+ }
11447+
11448+ *ret = *drinfo;
11449+ ssz = vfsub_read_k(file, (void *)ret->oldname, len, &pos);
11450+ if (unlikely(ssz != len)) {
11451+ au_kfree_rcu(ret);
11452+ ret = ERR_PTR(-EIO);
11453+ AuIOErr("ssz %zd, %u, %pD2\n", ssz, len, file);
11454+ goto out;
11455+ }
11456+
11457+ AuDbg("oldname %.*s\n", ret->oldnamelen, ret->oldname);
11458+
11459+out:
11460+ return ret;
11461+}
11462+
11463+/* ---------------------------------------------------------------------- */
11464+
11465+/* in order to be revertible */
11466+struct au_drinfo_rev_elm {
11467+ int created;
11468+ struct dentry *info_dentry;
11469+ struct au_drinfo *info_last;
11470+};
11471+
11472+struct au_drinfo_rev {
11473+ unsigned char already;
11474+ aufs_bindex_t nelm;
11475+ struct au_drinfo_rev_elm elm[];
11476+};
11477+
11478+/* todo: isn't it too large? */
11479+struct au_drinfo_store {
11480+ struct path h_ppath;
11481+ struct dentry *h_dentry;
11482+ struct au_drinfo_fdata *fdata;
11483+ char *infoname; /* inside of whname, just after PFX */
11484+ char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ];
11485+ aufs_bindex_t btgt, btail;
11486+ unsigned char no_sio,
11487+ allocated, /* current size of *fdata */
11488+ infonamelen, /* room size for p */
11489+ whnamelen, /* length of the generated name */
11490+ renameback; /* renamed back */
11491+};
11492+
11493+/* on rename(2) error, the caller should revert it using @elm */
11494+static int au_drinfo_do_store(struct au_drinfo_store *w,
11495+ struct au_drinfo_rev_elm *elm)
11496+{
11497+ int err, len;
11498+ ssize_t ssz;
11499+ loff_t pos;
11500+ struct path infopath = {
11501+ .mnt = w->h_ppath.mnt
11502+ };
11503+ struct inode *h_dir, *h_inode, *delegated;
11504+ struct file *infofile;
11505+ struct qstr *qname;
11506+
11507+ AuDebugOn(elm
11508+ && memcmp(elm, page_address(ZERO_PAGE(0)), sizeof(*elm)));
11509+
11510+ infopath.dentry = vfsub_lookup_one_len(w->whname, w->h_ppath.dentry,
11511+ w->whnamelen);
11512+ AuTraceErrPtr(infopath.dentry);
11513+ if (IS_ERR(infopath.dentry)) {
11514+ err = PTR_ERR(infopath.dentry);
11515+ goto out;
11516+ }
11517+
11518+ err = 0;
11519+ h_dir = d_inode(w->h_ppath.dentry);
11520+ if (elm && d_is_negative(infopath.dentry)) {
11521+ err = vfsub_create(h_dir, &infopath, 0600, /*want_excl*/true);
11522+ AuTraceErr(err);
11523+ if (unlikely(err))
11524+ goto out_dput;
11525+ elm->created = 1;
11526+ elm->info_dentry = dget(infopath.dentry);
11527+ }
11528+
11529+ infofile = vfsub_dentry_open(&infopath, O_RDWR);
11530+ AuTraceErrPtr(infofile);
11531+ if (IS_ERR(infofile)) {
11532+ err = PTR_ERR(infofile);
11533+ goto out_dput;
11534+ }
11535+
11536+ h_inode = d_inode(infopath.dentry);
11537+ if (elm && i_size_read(h_inode)) {
11538+ h_inode = d_inode(w->h_dentry);
11539+ elm->info_last = au_drinfo_read_k(infofile, h_inode->i_ino);
11540+ AuTraceErrPtr(elm->info_last);
11541+ if (IS_ERR(elm->info_last)) {
11542+ err = PTR_ERR(elm->info_last);
11543+ elm->info_last = NULL;
11544+ AuDebugOn(elm->info_dentry);
11545+ goto out_fput;
11546+ }
11547+ }
11548+
11549+ if (elm && w->renameback) {
11550+ delegated = NULL;
11551+ err = vfsub_unlink(h_dir, &infopath, &delegated, /*force*/0);
11552+ AuTraceErr(err);
11553+ if (unlikely(err == -EWOULDBLOCK))
11554+ iput(delegated);
11555+ goto out_fput;
11556+ }
11557+
11558+ pos = 0;
11559+ qname = &w->h_dentry->d_name;
11560+ len = sizeof(*w->fdata) + qname->len;
11561+ if (!elm)
11562+ len = sizeof(*w->fdata) + w->fdata->drinfo.oldnamelen;
11563+ ssz = vfsub_write_k(infofile, w->fdata, len, &pos);
11564+ if (ssz == len) {
11565+ AuDbg("hi%llu, %.*s\n", w->fdata->drinfo.ino,
11566+ w->fdata->drinfo.oldnamelen, w->fdata->drinfo.oldname);
11567+ goto out_fput; /* success */
11568+ } else {
11569+ err = -EIO;
11570+ if (ssz < 0)
11571+ err = ssz;
11572+ /* the caller should revert it using @elm */
11573+ }
11574+
11575+out_fput:
11576+ fput(infofile);
11577+out_dput:
11578+ dput(infopath.dentry);
11579+out:
11580+ AuTraceErr(err);
11581+ return err;
11582+}
11583+
11584+struct au_call_drinfo_do_store_args {
11585+ int *errp;
11586+ struct au_drinfo_store *w;
11587+ struct au_drinfo_rev_elm *elm;
11588+};
11589+
11590+static void au_call_drinfo_do_store(void *args)
11591+{
11592+ struct au_call_drinfo_do_store_args *a = args;
11593+
11594+ *a->errp = au_drinfo_do_store(a->w, a->elm);
11595+}
11596+
11597+static int au_drinfo_store_sio(struct au_drinfo_store *w,
11598+ struct au_drinfo_rev_elm *elm)
11599+{
11600+ int err, wkq_err;
11601+
11602+ if (w->no_sio)
11603+ err = au_drinfo_do_store(w, elm);
11604+ else {
11605+ struct au_call_drinfo_do_store_args a = {
11606+ .errp = &err,
11607+ .w = w,
11608+ .elm = elm
11609+ };
11610+ wkq_err = au_wkq_wait(au_call_drinfo_do_store, &a);
11611+ if (unlikely(wkq_err))
11612+ err = wkq_err;
11613+ }
11614+ AuTraceErr(err);
11615+
11616+ return err;
11617+}
11618+
11619+static int au_drinfo_store_work_init(struct au_drinfo_store *w,
11620+ aufs_bindex_t btgt)
11621+{
11622+ int err;
11623+
11624+ memset(w, 0, sizeof(*w));
11625+ w->allocated = roundup_pow_of_two(sizeof(*w->fdata) + 40);
11626+ strcpy(w->whname, AUFS_WH_DR_INFO_PFX);
11627+ w->infoname = w->whname + sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11628+ w->infonamelen = sizeof(w->whname) - sizeof(AUFS_WH_DR_INFO_PFX);
11629+ w->btgt = btgt;
11630+ w->no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
11631+
11632+ err = -ENOMEM;
11633+ w->fdata = kcalloc(1, w->allocated, GFP_NOFS);
11634+ if (unlikely(!w->fdata)) {
11635+ AuTraceErr(err);
11636+ goto out;
11637+ }
11638+ w->fdata->magic = (__force uint32_t)htonl(AUFS_DRINFO_MAGIC_V1);
11639+ err = 0;
11640+
11641+out:
11642+ return err;
11643+}
11644+
11645+static void au_drinfo_store_work_fin(struct au_drinfo_store *w)
11646+{
11647+ au_kfree_rcu(w->fdata);
11648+}
11649+
11650+static void au_drinfo_store_rev(struct au_drinfo_rev *rev,
11651+ struct au_drinfo_store *w)
11652+{
11653+ struct au_drinfo_rev_elm *elm;
11654+ struct inode *h_dir, *delegated;
11655+ int err, nelm;
11656+ struct path infopath = {
11657+ .mnt = w->h_ppath.mnt
11658+ };
11659+
11660+ h_dir = d_inode(w->h_ppath.dentry);
11661+ IMustLock(h_dir);
11662+
11663+ err = 0;
11664+ elm = rev->elm;
11665+ for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11666+ AuDebugOn(elm->created && elm->info_last);
11667+ if (elm->created) {
11668+ AuDbg("here\n");
11669+ delegated = NULL;
11670+ infopath.dentry = elm->info_dentry;
11671+ err = vfsub_unlink(h_dir, &infopath, &delegated,
11672+ !w->no_sio);
11673+ AuTraceErr(err);
11674+ if (unlikely(err == -EWOULDBLOCK))
11675+ iput(delegated);
11676+ dput(elm->info_dentry);
11677+ } else if (elm->info_last) {
11678+ AuDbg("here\n");
11679+ w->fdata->drinfo = *elm->info_last;
11680+ memcpy(w->fdata->drinfo.oldname,
11681+ elm->info_last->oldname,
11682+ elm->info_last->oldnamelen);
11683+ err = au_drinfo_store_sio(w, /*elm*/NULL);
11684+ au_kfree_rcu(elm->info_last);
11685+ }
11686+ if (unlikely(err))
11687+ AuIOErr("%d, %s\n", err, w->whname);
11688+ /* go on even if err */
11689+ }
11690+}
11691+
11692+/* caller has to call au_dr_rename_fin() later */
11693+static int au_drinfo_store(struct dentry *dentry, aufs_bindex_t btgt,
11694+ struct qstr *dst_name, void *_rev)
11695+{
11696+ int err, sz, nelm;
11697+ aufs_bindex_t bindex, btail;
11698+ struct au_drinfo_store work;
11699+ struct au_drinfo_rev *rev, **p;
11700+ struct au_drinfo_rev_elm *elm;
11701+ struct super_block *sb;
11702+ struct au_branch *br;
11703+ struct au_hinode *hdir;
11704+
11705+ err = au_drinfo_store_work_init(&work, btgt);
11706+ AuTraceErr(err);
11707+ if (unlikely(err))
11708+ goto out;
11709+
11710+ err = -ENOMEM;
11711+ btail = au_dbtaildir(dentry);
11712+ nelm = btail - btgt;
11713+ sz = sizeof(*rev) + sizeof(*elm) * nelm;
11714+ rev = kcalloc(1, sz, GFP_NOFS);
11715+ if (unlikely(!rev)) {
11716+ AuTraceErr(err);
11717+ goto out_args;
11718+ }
11719+ rev->nelm = nelm;
11720+ elm = rev->elm;
11721+ p = _rev;
11722+ *p = rev;
11723+
11724+ err = 0;
11725+ sb = dentry->d_sb;
11726+ work.h_ppath.dentry = au_h_dptr(dentry, btgt);
11727+ work.h_ppath.mnt = au_sbr_mnt(sb, btgt);
11728+ hdir = au_hi(d_inode(dentry), btgt);
11729+ au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11730+ for (bindex = btgt + 1; bindex <= btail; bindex++, elm++) {
11731+ work.h_dentry = au_h_dptr(dentry, bindex);
11732+ if (!work.h_dentry)
11733+ continue;
11734+
11735+ err = au_drinfo_construct(&work.fdata, work.h_dentry,
11736+ &work.allocated);
11737+ AuTraceErr(err);
11738+ if (unlikely(err))
11739+ break;
11740+
11741+ work.renameback = au_qstreq(&work.h_dentry->d_name, dst_name);
11742+ br = au_sbr(sb, bindex);
11743+ work.whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11744+ work.whnamelen += au_drinfo_name(br, work.infoname,
11745+ work.infonamelen);
11746+ AuDbg("whname %.*s, i%llu, %.*s\n",
11747+ work.whnamelen, work.whname,
11748+ be64_to_cpu((__force __be64)work.fdata->drinfo.ino),
11749+ work.fdata->drinfo.oldnamelen,
11750+ work.fdata->drinfo.oldname);
11751+
11752+ err = au_drinfo_store_sio(&work, elm);
11753+ AuTraceErr(err);
11754+ if (unlikely(err))
11755+ break;
11756+ }
11757+ if (unlikely(err)) {
11758+ /* revert all drinfo */
11759+ au_drinfo_store_rev(rev, &work);
11760+ au_kfree_try_rcu(rev);
11761+ *p = NULL;
11762+ }
11763+ au_hn_inode_unlock(hdir);
11764+
11765+out_args:
11766+ au_drinfo_store_work_fin(&work);
11767+out:
11768+ return err;
11769+}
11770+
11771+/* ---------------------------------------------------------------------- */
11772+
11773+int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
11774+ struct qstr *dst_name, void *_rev)
11775+{
11776+ int err, already;
11777+ ino_t ino;
11778+ struct super_block *sb;
11779+ struct au_branch *br;
11780+ struct au_dr_br *dr;
11781+ struct dentry *h_dentry;
11782+ struct inode *h_inode;
11783+ struct au_dr_hino *ent;
11784+ struct au_drinfo_rev *rev, **p;
11785+
11786+ AuDbg("bindex %d\n", bindex);
11787+
11788+ err = -ENOMEM;
11789+ ent = kmalloc(sizeof(*ent), GFP_NOFS);
11790+ if (unlikely(!ent))
11791+ goto out;
11792+
11793+ sb = src->d_sb;
11794+ br = au_sbr(sb, bindex);
11795+ dr = &br->br_dirren;
11796+ h_dentry = au_h_dptr(src, bindex);
11797+ h_inode = d_inode(h_dentry);
11798+ ino = h_inode->i_ino;
11799+ ent->dr_h_ino = ino;
11800+ already = au_dr_hino_test_add(dr, ino, ent);
11801+ AuDbg("b%d, hi%llu, already %d\n",
11802+ bindex, (unsigned long long)ino, already);
11803+
11804+ err = au_drinfo_store(src, bindex, dst_name, _rev);
11805+ AuTraceErr(err);
11806+ if (!err) {
11807+ p = _rev;
11808+ rev = *p;
11809+ rev->already = already;
11810+ goto out; /* success */
11811+ }
11812+
11813+ /* revert */
11814+ if (!already)
11815+ au_dr_hino_del(dr, ent);
11816+ au_kfree_rcu(ent);
11817+
11818+out:
11819+ AuTraceErr(err);
11820+ return err;
11821+}
11822+
11823+void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11824+{
11825+ struct au_drinfo_rev *rev;
11826+ struct au_drinfo_rev_elm *elm;
11827+ int nelm;
11828+
11829+ rev = _rev;
11830+ elm = rev->elm;
11831+ for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11832+ dput(elm->info_dentry);
11833+ au_kfree_rcu(elm->info_last);
11834+ }
11835+ au_kfree_try_rcu(rev);
11836+}
11837+
11838+void au_dr_rename_rev(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11839+{
11840+ int err;
11841+ struct au_drinfo_store work;
11842+ struct au_drinfo_rev *rev = _rev;
11843+ struct super_block *sb;
11844+ struct au_branch *br;
11845+ struct inode *h_inode;
11846+ struct au_dr_br *dr;
11847+ struct au_dr_hino *ent;
11848+
11849+ err = au_drinfo_store_work_init(&work, btgt);
11850+ if (unlikely(err))
11851+ goto out;
11852+
11853+ sb = src->d_sb;
11854+ br = au_sbr(sb, btgt);
11855+ work.h_ppath.dentry = au_h_dptr(src, btgt);
11856+ work.h_ppath.mnt = au_br_mnt(br);
11857+ au_drinfo_store_rev(rev, &work);
11858+ au_drinfo_store_work_fin(&work);
11859+ if (rev->already)
11860+ goto out;
11861+
11862+ dr = &br->br_dirren;
11863+ h_inode = d_inode(work.h_ppath.dentry);
11864+ ent = au_dr_hino_find(dr, h_inode->i_ino);
11865+ BUG_ON(!ent);
11866+ au_dr_hino_del(dr, ent);
11867+ au_kfree_rcu(ent);
11868+
11869+out:
11870+ au_kfree_try_rcu(rev);
11871+ if (unlikely(err))
11872+ pr_err("failed to remove dirren info\n");
11873+}
11874+
11875+/* ---------------------------------------------------------------------- */
11876+
11877+static struct au_drinfo *au_drinfo_do_load(struct path *h_ppath,
11878+ char *whname, int whnamelen,
11879+ struct dentry **info_dentry)
11880+{
11881+ struct au_drinfo *drinfo;
11882+ struct file *f;
11883+ struct inode *h_dir;
11884+ struct path infopath;
11885+ int unlocked;
11886+
11887+ AuDbg("%pd/%.*s\n", h_ppath->dentry, whnamelen, whname);
11888+
11889+ *info_dentry = NULL;
11890+ drinfo = NULL;
11891+ unlocked = 0;
11892+ h_dir = d_inode(h_ppath->dentry);
11893+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
11894+ infopath.dentry = vfsub_lookup_one_len(whname, h_ppath->dentry,
11895+ whnamelen);
11896+ if (IS_ERR(infopath.dentry)) {
11897+ drinfo = (void *)infopath.dentry;
11898+ goto out;
11899+ }
11900+
11901+ if (d_is_negative(infopath.dentry))
11902+ goto out_dput; /* success */
11903+
11904+ infopath.mnt = h_ppath->mnt;
11905+ f = vfsub_dentry_open(&infopath, O_RDONLY);
11906+ inode_unlock_shared(h_dir);
11907+ unlocked = 1;
11908+ if (IS_ERR(f)) {
11909+ drinfo = (void *)f;
11910+ goto out_dput;
11911+ }
11912+
11913+ drinfo = au_drinfo_read_k(f, /*h_ino*/0);
11914+ if (IS_ERR_OR_NULL(drinfo))
11915+ goto out_fput;
11916+
11917+ AuDbg("oldname %.*s\n", drinfo->oldnamelen, drinfo->oldname);
11918+ *info_dentry = dget(infopath.dentry); /* keep it alive */
11919+
11920+out_fput:
11921+ fput(f);
11922+out_dput:
11923+ dput(infopath.dentry);
11924+out:
11925+ if (!unlocked)
11926+ inode_unlock_shared(h_dir);
11927+ AuTraceErrPtr(drinfo);
11928+ return drinfo;
11929+}
11930+
11931+struct au_drinfo_do_load_args {
11932+ struct au_drinfo **drinfop;
11933+ struct path *h_ppath;
11934+ char *whname;
11935+ int whnamelen;
11936+ struct dentry **info_dentry;
11937+};
11938+
11939+static void au_call_drinfo_do_load(void *args)
11940+{
11941+ struct au_drinfo_do_load_args *a = args;
11942+
11943+ *a->drinfop = au_drinfo_do_load(a->h_ppath, a->whname, a->whnamelen,
11944+ a->info_dentry);
11945+}
11946+
11947+struct au_drinfo_load {
11948+ struct path h_ppath;
11949+ struct qstr *qname;
11950+ unsigned char no_sio;
11951+
11952+ aufs_bindex_t ninfo;
11953+ struct au_drinfo **drinfo;
11954+};
11955+
11956+static int au_drinfo_load(struct au_drinfo_load *w, aufs_bindex_t bindex,
11957+ struct au_branch *br)
11958+{
11959+ int err, wkq_err, whnamelen, e;
11960+ char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ]
11961+ = AUFS_WH_DR_INFO_PFX;
11962+ struct au_drinfo *drinfo;
11963+ struct qstr oldname;
11964+ struct inode *h_dir, *delegated;
11965+ struct dentry *info_dentry;
11966+ struct path infopath;
11967+
11968+ whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11969+ whnamelen += au_drinfo_name(br, whname + whnamelen,
11970+ sizeof(whname) - whnamelen);
11971+ if (w->no_sio)
11972+ drinfo = au_drinfo_do_load(&w->h_ppath, whname, whnamelen,
11973+ &info_dentry);
11974+ else {
11975+ struct au_drinfo_do_load_args args = {
11976+ .drinfop = &drinfo,
11977+ .h_ppath = &w->h_ppath,
11978+ .whname = whname,
11979+ .whnamelen = whnamelen,
11980+ .info_dentry = &info_dentry
11981+ };
11982+ wkq_err = au_wkq_wait(au_call_drinfo_do_load, &args);
11983+ if (unlikely(wkq_err))
11984+ drinfo = ERR_PTR(wkq_err);
11985+ }
11986+ err = PTR_ERR(drinfo);
11987+ if (IS_ERR_OR_NULL(drinfo))
11988+ goto out;
11989+
11990+ err = 0;
11991+ oldname.len = drinfo->oldnamelen;
11992+ oldname.name = drinfo->oldname;
11993+ if (au_qstreq(w->qname, &oldname)) {
11994+ /* the name is renamed back */
11995+ au_kfree_rcu(drinfo);
11996+ drinfo = NULL;
11997+
11998+ infopath.dentry = info_dentry;
11999+ infopath.mnt = w->h_ppath.mnt;
12000+ h_dir = d_inode(w->h_ppath.dentry);
12001+ delegated = NULL;
12002+ inode_lock_nested(h_dir, AuLsc_I_PARENT);
12003+ e = vfsub_unlink(h_dir, &infopath, &delegated, !w->no_sio);
12004+ inode_unlock(h_dir);
12005+ if (unlikely(e))
12006+ AuIOErr("ignored %d, %pd2\n", e, &infopath.dentry);
12007+ if (unlikely(e == -EWOULDBLOCK))
12008+ iput(delegated);
12009+ }
12010+ au_kfree_rcu(w->drinfo[bindex]);
12011+ w->drinfo[bindex] = drinfo;
12012+ dput(info_dentry);
12013+
12014+out:
12015+ AuTraceErr(err);
12016+ return err;
12017+}
12018+
12019+/* ---------------------------------------------------------------------- */
12020+
12021+static void au_dr_lkup_free(struct au_drinfo **drinfo, int n)
12022+{
12023+ struct au_drinfo **p = drinfo;
12024+
12025+ while (n-- > 0)
12026+ au_kfree_rcu(*drinfo++);
12027+ au_kfree_try_rcu(p);
12028+}
12029+
12030+int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12031+ aufs_bindex_t btgt)
12032+{
12033+ int err, ninfo;
12034+ struct au_drinfo_load w;
12035+ aufs_bindex_t bindex, bbot;
12036+ struct au_branch *br;
12037+ struct inode *h_dir;
12038+ struct au_dr_hino *ent;
12039+ struct super_block *sb;
12040+
12041+ AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
12042+ AuLNPair(&dentry->d_name), AuLNPair(&lkup->dirren.dr_name),
12043+ AuLNPair(&lkup->whname), btgt);
12044+
12045+ sb = dentry->d_sb;
12046+ bbot = au_sbbot(sb);
12047+ w.ninfo = bbot + 1;
12048+ if (!lkup->dirren.drinfo) {
12049+ lkup->dirren.drinfo = kcalloc(w.ninfo,
12050+ sizeof(*lkup->dirren.drinfo),
12051+ GFP_NOFS);
12052+ if (unlikely(!lkup->dirren.drinfo)) {
12053+ err = -ENOMEM;
12054+ goto out;
12055+ }
12056+ lkup->dirren.ninfo = w.ninfo;
12057+ }
12058+ w.drinfo = lkup->dirren.drinfo;
12059+ w.no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
12060+ w.h_ppath.dentry = au_h_dptr(dentry, btgt);
12061+ AuDebugOn(!w.h_ppath.dentry);
12062+ w.h_ppath.mnt = au_sbr_mnt(sb, btgt);
12063+ w.qname = &dentry->d_name;
12064+
12065+ ninfo = 0;
12066+ for (bindex = btgt + 1; bindex <= bbot; bindex++) {
12067+ br = au_sbr(sb, bindex);
12068+ err = au_drinfo_load(&w, bindex, br);
12069+ if (unlikely(err))
12070+ goto out_free;
12071+ if (w.drinfo[bindex])
12072+ ninfo++;
12073+ }
12074+ if (!ninfo) {
12075+ br = au_sbr(sb, btgt);
12076+ h_dir = d_inode(w.h_ppath.dentry);
12077+ ent = au_dr_hino_find(&br->br_dirren, h_dir->i_ino);
12078+ AuDebugOn(!ent);
12079+ au_dr_hino_del(&br->br_dirren, ent);
12080+ au_kfree_rcu(ent);
12081+ }
12082+ goto out; /* success */
12083+
12084+out_free:
12085+ au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12086+ lkup->dirren.ninfo = 0;
12087+ lkup->dirren.drinfo = NULL;
12088+out:
12089+ AuTraceErr(err);
12090+ return err;
12091+}
12092+
12093+void au_dr_lkup_fin(struct au_do_lookup_args *lkup)
12094+{
12095+ au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12096+}
12097+
12098+int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt)
12099+{
12100+ int err;
12101+ struct au_drinfo *drinfo;
12102+
12103+ err = 0;
12104+ if (!lkup->dirren.drinfo)
12105+ goto out;
12106+ AuDebugOn(lkup->dirren.ninfo <= btgt);
12107+ drinfo = lkup->dirren.drinfo[btgt];
12108+ if (!drinfo)
12109+ goto out;
12110+
12111+ au_kfree_try_rcu(lkup->whname.name);
12112+ lkup->whname.name = NULL;
12113+ lkup->dirren.dr_name.len = drinfo->oldnamelen;
12114+ lkup->dirren.dr_name.name = drinfo->oldname;
12115+ lkup->name = &lkup->dirren.dr_name;
12116+ err = au_wh_name_alloc(&lkup->whname, lkup->name);
12117+ if (!err)
12118+ AuDbg("name %.*s, whname %.*s, b%d\n",
12119+ AuLNPair(lkup->name), AuLNPair(&lkup->whname),
12120+ btgt);
12121+
12122+out:
12123+ AuTraceErr(err);
12124+ return err;
12125+}
12126+
12127+int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12128+ ino_t h_ino)
12129+{
12130+ int match;
12131+ struct au_drinfo *drinfo;
12132+
12133+ match = 1;
12134+ if (!lkup->dirren.drinfo)
12135+ goto out;
12136+ AuDebugOn(lkup->dirren.ninfo <= bindex);
12137+ drinfo = lkup->dirren.drinfo[bindex];
12138+ if (!drinfo)
12139+ goto out;
12140+
12141+ match = (drinfo->ino == h_ino);
12142+ AuDbg("match %d\n", match);
12143+
12144+out:
12145+ return match;
12146+}
12147+
12148+/* ---------------------------------------------------------------------- */
12149+
12150+int au_dr_opt_set(struct super_block *sb)
12151+{
12152+ int err;
12153+ aufs_bindex_t bindex, bbot;
12154+ struct au_branch *br;
12155+
12156+ err = 0;
12157+ bbot = au_sbbot(sb);
12158+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
12159+ br = au_sbr(sb, bindex);
12160+ err = au_dr_hino(sb, bindex, /*br*/NULL, &br->br_path);
12161+ }
12162+
12163+ return err;
12164+}
12165+
12166+int au_dr_opt_flush(struct super_block *sb)
12167+{
12168+ int err;
12169+ aufs_bindex_t bindex, bbot;
12170+ struct au_branch *br;
12171+
12172+ err = 0;
12173+ bbot = au_sbbot(sb);
12174+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
12175+ br = au_sbr(sb, bindex);
12176+ if (au_br_writable(br->br_perm))
12177+ err = au_dr_hino(sb, bindex, /*br*/NULL, /*path*/NULL);
12178+ }
12179+
12180+ return err;
12181+}
12182+
12183+int au_dr_opt_clr(struct super_block *sb, int no_flush)
12184+{
12185+ int err;
12186+ aufs_bindex_t bindex, bbot;
12187+ struct au_branch *br;
12188+
12189+ err = 0;
12190+ if (!no_flush) {
12191+ err = au_dr_opt_flush(sb);
12192+ if (unlikely(err))
12193+ goto out;
12194+ }
12195+
12196+ bbot = au_sbbot(sb);
12197+ for (bindex = 0; bindex <= bbot; bindex++) {
12198+ br = au_sbr(sb, bindex);
12199+ au_dr_hino_free(&br->br_dirren);
12200+ }
12201+
12202+out:
12203+ return err;
12204+}
12205diff -urN /usr/share/empty/fs/aufs/dirren.h linux/fs/aufs/dirren.h
12206--- /usr/share/empty/fs/aufs/dirren.h 1970-01-01 01:00:00.000000000 +0100
12207+++ linux/fs/aufs/dirren.h 2021-02-24 13:33:42.741013619 +0100
12208@@ -0,0 +1,140 @@
12209+/* SPDX-License-Identifier: GPL-2.0 */
12210+/*
12211+ * Copyright (C) 2017-2020 Junjiro R. Okajima
12212+ *
12213+ * This program, aufs is free software; you can redistribute it and/or modify
12214+ * it under the terms of the GNU General Public License as published by
12215+ * the Free Software Foundation; either version 2 of the License, or
12216+ * (at your option) any later version.
12217+ *
12218+ * This program is distributed in the hope that it will be useful,
12219+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12220+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12221+ * GNU General Public License for more details.
12222+ *
12223+ * You should have received a copy of the GNU General Public License
12224+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
12225+ */
12226+
12227+/*
12228+ * renamed dir info
12229+ */
12230+
12231+#ifndef __AUFS_DIRREN_H__
12232+#define __AUFS_DIRREN_H__
12233+
12234+#ifdef __KERNEL__
12235+
12236+#include <linux/dcache.h>
12237+#include <linux/statfs.h>
12238+#include <linux/uuid.h>
12239+#include "hbl.h"
12240+
12241+#define AuDirren_NHASH 100
12242+
12243+#ifdef CONFIG_AUFS_DIRREN
12244+enum au_brid_type {
12245+ AuBrid_Unset,
12246+ AuBrid_UUID,
12247+ AuBrid_FSID,
12248+ AuBrid_DEV
12249+};
12250+
12251+struct au_dr_brid {
12252+ enum au_brid_type type;
12253+ union {
12254+ uuid_t uuid; /* unimplemented yet */
12255+ fsid_t fsid;
12256+ dev_t dev;
12257+ };
12258+};
12259+
12260+/* 20 is the max digits length of ulong 64 */
12261+/* brid-type "_" uuid "_" inum */
12262+#define AUFS_DIRREN_FNAME_SZ (1 + 1 + UUID_STRING_LEN + 20)
12263+#define AUFS_DIRREN_ENV_VAL_SZ (AUFS_DIRREN_FNAME_SZ + 1 + 20)
12264+
12265+struct au_dr_hino {
12266+ struct hlist_bl_node dr_hnode;
12267+ ino_t dr_h_ino;
12268+};
12269+
12270+struct au_dr_br {
12271+ struct hlist_bl_head dr_h_ino[AuDirren_NHASH];
12272+ struct au_dr_brid dr_brid;
12273+};
12274+
12275+struct au_dr_lookup {
12276+ /* dr_name is pointed by struct au_do_lookup_args.name */
12277+ struct qstr dr_name; /* subset of dr_info */
12278+ aufs_bindex_t ninfo;
12279+ struct au_drinfo **drinfo;
12280+};
12281+#else
12282+struct au_dr_hino;
12283+/* empty */
12284+struct au_dr_br { };
12285+struct au_dr_lookup { };
12286+#endif
12287+
12288+/* ---------------------------------------------------------------------- */
12289+
12290+struct au_branch;
12291+struct au_do_lookup_args;
12292+struct au_hinode;
12293+#ifdef CONFIG_AUFS_DIRREN
12294+int au_dr_hino_test_add(struct au_dr_br *dr, ino_t h_ino,
12295+ struct au_dr_hino *add_ent);
12296+void au_dr_hino_free(struct au_dr_br *dr);
12297+int au_dr_br_init(struct super_block *sb, struct au_branch *br,
12298+ const struct path *path);
12299+int au_dr_br_fin(struct super_block *sb, struct au_branch *br);
12300+int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
12301+ struct qstr *dst_name, void *_rev);
12302+void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *rev);
12303+void au_dr_rename_rev(struct dentry *src, aufs_bindex_t bindex, void *rev);
12304+int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12305+ aufs_bindex_t bindex);
12306+int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12307+int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12308+ ino_t h_ino);
12309+void au_dr_lkup_fin(struct au_do_lookup_args *lkup);
12310+int au_dr_opt_set(struct super_block *sb);
12311+int au_dr_opt_flush(struct super_block *sb);
12312+int au_dr_opt_clr(struct super_block *sb, int no_flush);
12313+#else
12314+AuStubInt0(au_dr_hino_test_add, struct au_dr_br *dr, ino_t h_ino,
12315+ struct au_dr_hino *add_ent);
12316+AuStubVoid(au_dr_hino_free, struct au_dr_br *dr);
12317+AuStubInt0(au_dr_br_init, struct super_block *sb, struct au_branch *br,
12318+ const struct path *path);
12319+AuStubInt0(au_dr_br_fin, struct super_block *sb, struct au_branch *br);
12320+AuStubInt0(au_dr_rename, struct dentry *src, aufs_bindex_t bindex,
12321+ struct qstr *dst_name, void *_rev);
12322+AuStubVoid(au_dr_rename_fin, struct dentry *src, aufs_bindex_t btgt, void *rev);
12323+AuStubVoid(au_dr_rename_rev, struct dentry *src, aufs_bindex_t bindex,
12324+ void *rev);
12325+AuStubInt0(au_dr_lkup, struct au_do_lookup_args *lkup, struct dentry *dentry,
12326+ aufs_bindex_t bindex);
12327+AuStubInt0(au_dr_lkup_name, struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12328+AuStubInt0(au_dr_lkup_h_ino, struct au_do_lookup_args *lkup,
12329+ aufs_bindex_t bindex, ino_t h_ino);
12330+AuStubVoid(au_dr_lkup_fin, struct au_do_lookup_args *lkup);
12331+AuStubInt0(au_dr_opt_set, struct super_block *sb);
12332+AuStubInt0(au_dr_opt_flush, struct super_block *sb);
12333+AuStubInt0(au_dr_opt_clr, struct super_block *sb, int no_flush);
12334+#endif
12335+
12336+/* ---------------------------------------------------------------------- */
12337+
12338+#ifdef CONFIG_AUFS_DIRREN
12339+static inline int au_dr_ihash(ino_t h_ino)
12340+{
12341+ return h_ino % AuDirren_NHASH;
12342+}
12343+#else
12344+AuStubInt0(au_dr_ihash, ino_t h_ino);
12345+#endif
12346+
12347+#endif /* __KERNEL__ */
12348+#endif /* __AUFS_DIRREN_H__ */
12349diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
12350--- /usr/share/empty/fs/aufs/dynop.c 1970-01-01 01:00:00.000000000 +0100
12351+++ linux/fs/aufs/dynop.c 2021-02-24 13:33:42.744347058 +0100
12352@@ -0,0 +1,368 @@
12353+// SPDX-License-Identifier: GPL-2.0
12354+/*
12355+ * Copyright (C) 2010-2020 Junjiro R. Okajima
12356+ *
12357+ * This program, aufs is free software; you can redistribute it and/or modify
12358+ * it under the terms of the GNU General Public License as published by
12359+ * the Free Software Foundation; either version 2 of the License, or
12360+ * (at your option) any later version.
12361+ *
12362+ * This program is distributed in the hope that it will be useful,
12363+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12364+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12365+ * GNU General Public License for more details.
12366+ *
12367+ * You should have received a copy of the GNU General Public License
12368+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
12369+ */
12370+
12371+/*
12372+ * dynamically customizable operations for regular files
12373+ */
12374+
12375+#include "aufs.h"
12376+
12377+#define DyPrSym(key) AuDbgSym(key->dk_op.dy_hop)
12378+
12379+/*
12380+ * How large will these lists be?
12381+ * Usually just a few elements, 20-30 at most for each, I guess.
12382+ */
12383+static struct hlist_bl_head dynop[AuDyLast];
12384+
12385+static struct au_dykey *dy_gfind_get(struct hlist_bl_head *hbl,
12386+ const void *h_op)
12387+{
12388+ struct au_dykey *key, *tmp;
12389+ struct hlist_bl_node *pos;
12390+
12391+ key = NULL;
12392+ hlist_bl_lock(hbl);
12393+ hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12394+ if (tmp->dk_op.dy_hop == h_op) {
12395+ if (kref_get_unless_zero(&tmp->dk_kref))
12396+ key = tmp;
12397+ break;
12398+ }
12399+ hlist_bl_unlock(hbl);
12400+
12401+ return key;
12402+}
12403+
12404+static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
12405+{
12406+ struct au_dykey **k, *found;
12407+ const void *h_op = key->dk_op.dy_hop;
12408+ int i;
12409+
12410+ found = NULL;
12411+ k = br->br_dykey;
12412+ for (i = 0; i < AuBrDynOp; i++)
12413+ if (k[i]) {
12414+ if (k[i]->dk_op.dy_hop == h_op) {
12415+ found = k[i];
12416+ break;
12417+ }
12418+ } else
12419+ break;
12420+ if (!found) {
12421+ spin_lock(&br->br_dykey_lock);
12422+ for (; i < AuBrDynOp; i++)
12423+ if (k[i]) {
12424+ if (k[i]->dk_op.dy_hop == h_op) {
12425+ found = k[i];
12426+ break;
12427+ }
12428+ } else {
12429+ k[i] = key;
12430+ break;
12431+ }
12432+ spin_unlock(&br->br_dykey_lock);
12433+ BUG_ON(i == AuBrDynOp); /* expand the array */
12434+ }
12435+
12436+ return found;
12437+}
12438+
12439+/* kref_get() if @key is already added */
12440+static struct au_dykey *dy_gadd(struct hlist_bl_head *hbl, struct au_dykey *key)
12441+{
12442+ struct au_dykey *tmp, *found;
12443+ struct hlist_bl_node *pos;
12444+ const void *h_op = key->dk_op.dy_hop;
12445+
12446+ found = NULL;
12447+ hlist_bl_lock(hbl);
12448+ hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12449+ if (tmp->dk_op.dy_hop == h_op) {
12450+ if (kref_get_unless_zero(&tmp->dk_kref))
12451+ found = tmp;
12452+ break;
12453+ }
12454+ if (!found)
12455+ hlist_bl_add_head(&key->dk_hnode, hbl);
12456+ hlist_bl_unlock(hbl);
12457+
12458+ if (!found)
12459+ DyPrSym(key);
12460+ return found;
12461+}
12462+
12463+static void dy_free_rcu(struct rcu_head *rcu)
12464+{
12465+ struct au_dykey *key;
12466+
12467+ key = container_of(rcu, struct au_dykey, dk_rcu);
12468+ DyPrSym(key);
12469+ kfree(key);
12470+}
12471+
12472+static void dy_free(struct kref *kref)
12473+{
12474+ struct au_dykey *key;
12475+ struct hlist_bl_head *hbl;
12476+
12477+ key = container_of(kref, struct au_dykey, dk_kref);
12478+ hbl = dynop + key->dk_op.dy_type;
12479+ au_hbl_del(&key->dk_hnode, hbl);
12480+ call_rcu(&key->dk_rcu, dy_free_rcu);
12481+}
12482+
12483+void au_dy_put(struct au_dykey *key)
12484+{
12485+ kref_put(&key->dk_kref, dy_free);
12486+}
12487+
12488+/* ---------------------------------------------------------------------- */
12489+
12490+#define DyDbgSize(cnt, op) AuDebugOn(cnt != sizeof(op)/sizeof(void *))
12491+
12492+#ifdef CONFIG_AUFS_DEBUG
12493+#define DyDbgDeclare(cnt) unsigned int cnt = 0
12494+#define DyDbgInc(cnt) do { cnt++; } while (0)
12495+#else
12496+#define DyDbgDeclare(cnt) do {} while (0)
12497+#define DyDbgInc(cnt) do {} while (0)
12498+#endif
12499+
12500+#define DySet(func, dst, src, h_op, h_sb) do { \
12501+ DyDbgInc(cnt); \
12502+ if (h_op->func) { \
12503+ if (src.func) \
12504+ dst.func = src.func; \
12505+ else \
12506+ AuDbg("%s %s\n", au_sbtype(h_sb), #func); \
12507+ } \
12508+} while (0)
12509+
12510+#define DySetForce(func, dst, src) do { \
12511+ AuDebugOn(!src.func); \
12512+ DyDbgInc(cnt); \
12513+ dst.func = src.func; \
12514+} while (0)
12515+
12516+#define DySetAop(func) \
12517+ DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
12518+#define DySetAopForce(func) \
12519+ DySetForce(func, dyaop->da_op, aufs_aop)
12520+
12521+static void dy_aop(struct au_dykey *key, const void *h_op,
12522+ struct super_block *h_sb __maybe_unused)
12523+{
12524+ struct au_dyaop *dyaop = (void *)key;
12525+ const struct address_space_operations *h_aop = h_op;
12526+ DyDbgDeclare(cnt);
12527+
12528+ AuDbg("%s\n", au_sbtype(h_sb));
12529+
12530+ DySetAop(writepage);
12531+ DySetAopForce(readpage); /* force */
12532+ DySetAop(writepages);
12533+ DySetAop(set_page_dirty);
12534+ DySetAop(readpages);
12535+ DySetAop(readahead);
12536+ DySetAop(write_begin);
12537+ DySetAop(write_end);
12538+ DySetAop(bmap);
12539+ DySetAop(invalidatepage);
12540+ DySetAop(releasepage);
12541+ DySetAop(freepage);
12542+ /* this one will be changed according to an aufs mount option */
12543+ DySetAop(direct_IO);
12544+ DySetAop(migratepage);
12545+ DySetAop(isolate_page);
12546+ DySetAop(putback_page);
12547+ DySetAop(launder_page);
12548+ DySetAop(is_partially_uptodate);
12549+ DySetAop(is_dirty_writeback);
12550+ DySetAop(error_remove_page);
12551+ DySetAop(swap_activate);
12552+ DySetAop(swap_deactivate);
12553+
12554+ DyDbgSize(cnt, *h_aop);
12555+}
12556+
12557+/* ---------------------------------------------------------------------- */
12558+
12559+static void dy_bug(struct kref *kref)
12560+{
12561+ BUG();
12562+}
12563+
12564+static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
12565+{
12566+ struct au_dykey *key, *old;
12567+ struct hlist_bl_head *hbl;
12568+ struct op {
12569+ unsigned int sz;
12570+ void (*set)(struct au_dykey *key, const void *h_op,
12571+ struct super_block *h_sb __maybe_unused);
12572+ };
12573+ static const struct op a[] = {
12574+ [AuDy_AOP] = {
12575+ .sz = sizeof(struct au_dyaop),
12576+ .set = dy_aop
12577+ }
12578+ };
12579+ const struct op *p;
12580+
12581+ hbl = dynop + op->dy_type;
12582+ key = dy_gfind_get(hbl, op->dy_hop);
12583+ if (key)
12584+ goto out_add; /* success */
12585+
12586+ p = a + op->dy_type;
12587+ key = kzalloc(p->sz, GFP_NOFS);
12588+ if (unlikely(!key)) {
12589+ key = ERR_PTR(-ENOMEM);
12590+ goto out;
12591+ }
12592+
12593+ key->dk_op.dy_hop = op->dy_hop;
12594+ kref_init(&key->dk_kref);
12595+ p->set(key, op->dy_hop, au_br_sb(br));
12596+ old = dy_gadd(hbl, key);
12597+ if (old) {
12598+ au_kfree_rcu(key);
12599+ key = old;
12600+ }
12601+
12602+out_add:
12603+ old = dy_bradd(br, key);
12604+ if (old)
12605+ /* its ref-count should never be zero here */
12606+ kref_put(&key->dk_kref, dy_bug);
12607+out:
12608+ return key;
12609+}
12610+
12611+/* ---------------------------------------------------------------------- */
12612+/*
12613+ * Aufs prohibits O_DIRECT by default even if the branch supports it.
12614+ * This behaviour is necessary to return an error from open(O_DIRECT) instead
12615+ * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
12616+ * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
12617+ * See the aufs manual in detail.
12618+ */
12619+static void dy_adx(struct au_dyaop *dyaop, int do_dx)
12620+{
12621+ if (!do_dx)
12622+ dyaop->da_op.direct_IO = NULL;
12623+ else
12624+ dyaop->da_op.direct_IO = aufs_aop.direct_IO;
12625+}
12626+
12627+static struct au_dyaop *dy_aget(struct au_branch *br,
12628+ const struct address_space_operations *h_aop,
12629+ int do_dx)
12630+{
12631+ struct au_dyaop *dyaop;
12632+ struct au_dynop op;
12633+
12634+ op.dy_type = AuDy_AOP;
12635+ op.dy_haop = h_aop;
12636+ dyaop = (void *)dy_get(&op, br);
12637+ if (IS_ERR(dyaop))
12638+ goto out;
12639+ dy_adx(dyaop, do_dx);
12640+
12641+out:
12642+ return dyaop;
12643+}
12644+
12645+int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12646+ struct inode *h_inode)
12647+{
12648+ int err, do_dx;
12649+ struct super_block *sb;
12650+ struct au_branch *br;
12651+ struct au_dyaop *dyaop;
12652+
12653+ AuDebugOn(!S_ISREG(h_inode->i_mode));
12654+ IiMustWriteLock(inode);
12655+
12656+ sb = inode->i_sb;
12657+ br = au_sbr(sb, bindex);
12658+ do_dx = !!au_opt_test(au_mntflags(sb), DIO);
12659+ dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
12660+ err = PTR_ERR(dyaop);
12661+ if (IS_ERR(dyaop))
12662+ /* unnecessary to call dy_fput() */
12663+ goto out;
12664+
12665+ err = 0;
12666+ inode->i_mapping->a_ops = &dyaop->da_op;
12667+
12668+out:
12669+ return err;
12670+}
12671+
12672+/*
12673+ * Is it safe to replace a_ops during the inode/file is in operation?
12674+ * Yes, I hope so.
12675+ */
12676+int au_dy_irefresh(struct inode *inode)
12677+{
12678+ int err;
12679+ aufs_bindex_t btop;
12680+ struct inode *h_inode;
12681+
12682+ err = 0;
12683+ if (S_ISREG(inode->i_mode)) {
12684+ btop = au_ibtop(inode);
12685+ h_inode = au_h_iptr(inode, btop);
12686+ err = au_dy_iaop(inode, btop, h_inode);
12687+ }
12688+ return err;
12689+}
12690+
12691+void au_dy_arefresh(int do_dx)
12692+{
12693+ struct hlist_bl_head *hbl;
12694+ struct hlist_bl_node *pos;
12695+ struct au_dykey *key;
12696+
12697+ hbl = dynop + AuDy_AOP;
12698+ hlist_bl_lock(hbl);
12699+ hlist_bl_for_each_entry(key, pos, hbl, dk_hnode)
12700+ dy_adx((void *)key, do_dx);
12701+ hlist_bl_unlock(hbl);
12702+}
12703+
12704+/* ---------------------------------------------------------------------- */
12705+
12706+void __init au_dy_init(void)
12707+{
12708+ int i;
12709+
12710+ for (i = 0; i < AuDyLast; i++)
12711+ INIT_HLIST_BL_HEAD(dynop + i);
12712+}
12713+
12714+void au_dy_fin(void)
12715+{
12716+ int i;
12717+
12718+ for (i = 0; i < AuDyLast; i++)
12719+ WARN_ON(!hlist_bl_empty(dynop + i));
12720+}
12721diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
12722--- /usr/share/empty/fs/aufs/dynop.h 1970-01-01 01:00:00.000000000 +0100
12723+++ linux/fs/aufs/dynop.h 2021-02-24 13:33:42.744347058 +0100
12724@@ -0,0 +1,77 @@
12725+/* SPDX-License-Identifier: GPL-2.0 */
12726+/*
12727+ * Copyright (C) 2010-2020 Junjiro R. Okajima
12728+ *
12729+ * This program, aufs is free software; you can redistribute it and/or modify
12730+ * it under the terms of the GNU General Public License as published by
12731+ * the Free Software Foundation; either version 2 of the License, or
12732+ * (at your option) any later version.
12733+ *
12734+ * This program is distributed in the hope that it will be useful,
12735+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12736+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12737+ * GNU General Public License for more details.
12738+ *
12739+ * You should have received a copy of the GNU General Public License
12740+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
12741+ */
12742+
12743+/*
12744+ * dynamically customizable operations (for regular files only)
12745+ */
12746+
12747+#ifndef __AUFS_DYNOP_H__
12748+#define __AUFS_DYNOP_H__
12749+
12750+#ifdef __KERNEL__
12751+
12752+#include <linux/fs.h>
12753+#include <linux/kref.h>
12754+
12755+enum {AuDy_AOP, AuDyLast};
12756+
12757+struct au_dynop {
12758+ int dy_type;
12759+ union {
12760+ const void *dy_hop;
12761+ const struct address_space_operations *dy_haop;
12762+ };
12763+};
12764+
12765+struct au_dykey {
12766+ union {
12767+ struct hlist_bl_node dk_hnode;
12768+ struct rcu_head dk_rcu;
12769+ };
12770+ struct au_dynop dk_op;
12771+
12772+ /*
12773+ * during I am in the branch local array, kref is gotten. when the
12774+ * branch is removed, kref is put.
12775+ */
12776+ struct kref dk_kref;
12777+};
12778+
12779+/* stop unioning since their sizes are very different from each other */
12780+struct au_dyaop {
12781+ struct au_dykey da_key;
12782+ struct address_space_operations da_op; /* not const */
12783+};
12784+/* make sure that 'struct au_dykey *' can be any type */
12785+static_assert(!offsetof(struct au_dyaop, da_key));
12786+
12787+/* ---------------------------------------------------------------------- */
12788+
12789+/* dynop.c */
12790+struct au_branch;
12791+void au_dy_put(struct au_dykey *key);
12792+int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12793+ struct inode *h_inode);
12794+int au_dy_irefresh(struct inode *inode);
12795+void au_dy_arefresh(int do_dio);
12796+
12797+void __init au_dy_init(void);
12798+void au_dy_fin(void);
12799+
12800+#endif /* __KERNEL__ */
12801+#endif /* __AUFS_DYNOP_H__ */
12802diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
12803--- /usr/share/empty/fs/aufs/export.c 1970-01-01 01:00:00.000000000 +0100
12804+++ linux/fs/aufs/export.c 2021-02-24 13:33:42.744347058 +0100
12805@@ -0,0 +1,837 @@
12806+// SPDX-License-Identifier: GPL-2.0
12807+/*
12808+ * Copyright (C) 2005-2020 Junjiro R. Okajima
12809+ *
12810+ * This program, aufs is free software; you can redistribute it and/or modify
12811+ * it under the terms of the GNU General Public License as published by
12812+ * the Free Software Foundation; either version 2 of the License, or
12813+ * (at your option) any later version.
12814+ *
12815+ * This program is distributed in the hope that it will be useful,
12816+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12817+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12818+ * GNU General Public License for more details.
12819+ *
12820+ * You should have received a copy of the GNU General Public License
12821+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
12822+ */
12823+
12824+/*
12825+ * export via nfs
12826+ */
12827+
12828+#include <linux/exportfs.h>
12829+#include <linux/fs_struct.h>
12830+#include <linux/namei.h>
12831+#include <linux/nsproxy.h>
12832+#include <linux/random.h>
12833+#include <linux/writeback.h>
12834+#include "aufs.h"
12835+
12836+union conv {
12837+#ifdef CONFIG_AUFS_INO_T_64
12838+ __u32 a[2];
12839+#else
12840+ __u32 a[1];
12841+#endif
12842+ ino_t ino;
12843+};
12844+
12845+static ino_t decode_ino(__u32 *a)
12846+{
12847+ union conv u;
12848+
12849+ BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
12850+ u.a[0] = a[0];
12851+#ifdef CONFIG_AUFS_INO_T_64
12852+ u.a[1] = a[1];
12853+#endif
12854+ return u.ino;
12855+}
12856+
12857+static void encode_ino(__u32 *a, ino_t ino)
12858+{
12859+ union conv u;
12860+
12861+ u.ino = ino;
12862+ a[0] = u.a[0];
12863+#ifdef CONFIG_AUFS_INO_T_64
12864+ a[1] = u.a[1];
12865+#endif
12866+}
12867+
12868+/* NFS file handle */
12869+enum {
12870+ Fh_br_id,
12871+ Fh_sigen,
12872+#ifdef CONFIG_AUFS_INO_T_64
12873+ /* support 64bit inode number */
12874+ Fh_ino1,
12875+ Fh_ino2,
12876+ Fh_dir_ino1,
12877+ Fh_dir_ino2,
12878+#else
12879+ Fh_ino1,
12880+ Fh_dir_ino1,
12881+#endif
12882+ Fh_igen,
12883+ Fh_h_type,
12884+ Fh_tail,
12885+
12886+ Fh_ino = Fh_ino1,
12887+ Fh_dir_ino = Fh_dir_ino1
12888+};
12889+
12890+static int au_test_anon(struct dentry *dentry)
12891+{
12892+ /* note: read d_flags without d_lock */
12893+ return !!(dentry->d_flags & DCACHE_DISCONNECTED);
12894+}
12895+
12896+int au_test_nfsd(void)
12897+{
12898+ int ret;
12899+ struct task_struct *tsk = current;
12900+ char comm[sizeof(tsk->comm)];
12901+
12902+ ret = 0;
12903+ if (tsk->flags & PF_KTHREAD) {
12904+ get_task_comm(comm, tsk);
12905+ ret = !strcmp(comm, "nfsd");
12906+ }
12907+
12908+ return ret;
12909+}
12910+
12911+/* ---------------------------------------------------------------------- */
12912+/* inode generation external table */
12913+
12914+void au_xigen_inc(struct inode *inode)
12915+{
12916+ loff_t pos;
12917+ ssize_t sz;
12918+ __u32 igen;
12919+ struct super_block *sb;
12920+ struct au_sbinfo *sbinfo;
12921+
12922+ sb = inode->i_sb;
12923+ AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
12924+
12925+ sbinfo = au_sbi(sb);
12926+ pos = inode->i_ino;
12927+ pos *= sizeof(igen);
12928+ igen = inode->i_generation + 1;
12929+ sz = xino_fwrite(sbinfo->si_xigen, &igen, sizeof(igen), &pos);
12930+ if (sz == sizeof(igen))
12931+ return; /* success */
12932+
12933+ if (unlikely(sz >= 0))
12934+ AuIOErr("xigen error (%zd)\n", sz);
12935+}
12936+
12937+int au_xigen_new(struct inode *inode)
12938+{
12939+ int err;
12940+ loff_t pos;
12941+ ssize_t sz;
12942+ struct super_block *sb;
12943+ struct au_sbinfo *sbinfo;
12944+ struct file *file;
12945+
12946+ err = 0;
12947+ /* todo: dirty, at mount time */
12948+ if (inode->i_ino == AUFS_ROOT_INO)
12949+ goto out;
12950+ sb = inode->i_sb;
12951+ SiMustAnyLock(sb);
12952+ if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
12953+ goto out;
12954+
12955+ err = -EFBIG;
12956+ pos = inode->i_ino;
12957+ if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
12958+ AuIOErr1("too large i%lld\n", pos);
12959+ goto out;
12960+ }
12961+ pos *= sizeof(inode->i_generation);
12962+
12963+ err = 0;
12964+ sbinfo = au_sbi(sb);
12965+ file = sbinfo->si_xigen;
12966+ BUG_ON(!file);
12967+
12968+ if (vfsub_f_size_read(file)
12969+ < pos + sizeof(inode->i_generation)) {
12970+ inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
12971+ sz = xino_fwrite(file, &inode->i_generation,
12972+ sizeof(inode->i_generation), &pos);
12973+ } else
12974+ sz = xino_fread(file, &inode->i_generation,
12975+ sizeof(inode->i_generation), &pos);
12976+ if (sz == sizeof(inode->i_generation))
12977+ goto out; /* success */
12978+
12979+ err = sz;
12980+ if (unlikely(sz >= 0)) {
12981+ err = -EIO;
12982+ AuIOErr("xigen error (%zd)\n", sz);
12983+ }
12984+
12985+out:
12986+ return err;
12987+}
12988+
12989+int au_xigen_set(struct super_block *sb, struct path *path)
12990+{
12991+ int err;
12992+ struct au_sbinfo *sbinfo;
12993+ struct file *file;
12994+
12995+ SiMustWriteLock(sb);
12996+
12997+ sbinfo = au_sbi(sb);
12998+ file = au_xino_create2(sb, path, sbinfo->si_xigen);
12999+ err = PTR_ERR(file);
13000+ if (IS_ERR(file))
13001+ goto out;
13002+ err = 0;
13003+ if (sbinfo->si_xigen)
13004+ fput(sbinfo->si_xigen);
13005+ sbinfo->si_xigen = file;
13006+
13007+out:
13008+ AuTraceErr(err);
13009+ return err;
13010+}
13011+
13012+void au_xigen_clr(struct super_block *sb)
13013+{
13014+ struct au_sbinfo *sbinfo;
13015+
13016+ SiMustWriteLock(sb);
13017+
13018+ sbinfo = au_sbi(sb);
13019+ if (sbinfo->si_xigen) {
13020+ fput(sbinfo->si_xigen);
13021+ sbinfo->si_xigen = NULL;
13022+ }
13023+}
13024+
13025+/* ---------------------------------------------------------------------- */
13026+
13027+static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
13028+ ino_t dir_ino)
13029+{
13030+ struct dentry *dentry, *d;
13031+ struct inode *inode;
13032+ unsigned int sigen;
13033+
13034+ dentry = NULL;
13035+ inode = ilookup(sb, ino);
13036+ if (!inode)
13037+ goto out;
13038+
13039+ dentry = ERR_PTR(-ESTALE);
13040+ sigen = au_sigen(sb);
13041+ if (unlikely(au_is_bad_inode(inode)
13042+ || IS_DEADDIR(inode)
13043+ || sigen != au_iigen(inode, NULL)))
13044+ goto out_iput;
13045+
13046+ dentry = NULL;
13047+ if (!dir_ino || S_ISDIR(inode->i_mode))
13048+ dentry = d_find_alias(inode);
13049+ else {
13050+ spin_lock(&inode->i_lock);
13051+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
13052+ spin_lock(&d->d_lock);
13053+ if (!au_test_anon(d)
13054+ && d_inode(d->d_parent)->i_ino == dir_ino) {
13055+ dentry = dget_dlock(d);
13056+ spin_unlock(&d->d_lock);
13057+ break;
13058+ }
13059+ spin_unlock(&d->d_lock);
13060+ }
13061+ spin_unlock(&inode->i_lock);
13062+ }
13063+ if (unlikely(dentry && au_digen_test(dentry, sigen))) {
13064+ /* need to refresh */
13065+ dput(dentry);
13066+ dentry = NULL;
13067+ }
13068+
13069+out_iput:
13070+ iput(inode);
13071+out:
13072+ AuTraceErrPtr(dentry);
13073+ return dentry;
13074+}
13075+
13076+/* ---------------------------------------------------------------------- */
13077+
13078+/* todo: dirty? */
13079+/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
13080+
13081+struct au_compare_mnt_args {
13082+ /* input */
13083+ struct super_block *sb;
13084+
13085+ /* output */
13086+ struct vfsmount *mnt;
13087+};
13088+
13089+static int au_compare_mnt(struct vfsmount *mnt, void *arg)
13090+{
13091+ struct au_compare_mnt_args *a = arg;
13092+
13093+ if (mnt->mnt_sb != a->sb)
13094+ return 0;
13095+ a->mnt = mntget(mnt);
13096+ return 1;
13097+}
13098+
13099+static struct vfsmount *au_mnt_get(struct super_block *sb)
13100+{
13101+ int err;
13102+ struct path root;
13103+ struct au_compare_mnt_args args = {
13104+ .sb = sb
13105+ };
13106+
13107+ get_fs_root(current->fs, &root);
13108+ rcu_read_lock();
13109+ err = iterate_mounts(au_compare_mnt, &args, root.mnt);
13110+ rcu_read_unlock();
13111+ path_put(&root);
13112+ AuDebugOn(!err);
13113+ AuDebugOn(!args.mnt);
13114+ return args.mnt;
13115+}
13116+
13117+struct au_nfsd_si_lock {
13118+ unsigned int sigen;
13119+ aufs_bindex_t bindex, br_id;
13120+ unsigned char force_lock;
13121+};
13122+
13123+static int si_nfsd_read_lock(struct super_block *sb,
13124+ struct au_nfsd_si_lock *nsi_lock)
13125+{
13126+ int err;
13127+ aufs_bindex_t bindex;
13128+
13129+ si_read_lock(sb, AuLock_FLUSH);
13130+
13131+ /* branch id may be wrapped around */
13132+ err = 0;
13133+ bindex = au_br_index(sb, nsi_lock->br_id);
13134+ if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
13135+ goto out; /* success */
13136+
13137+ err = -ESTALE;
13138+ bindex = -1;
13139+ if (!nsi_lock->force_lock)
13140+ si_read_unlock(sb);
13141+
13142+out:
13143+ nsi_lock->bindex = bindex;
13144+ return err;
13145+}
13146+
13147+struct find_name_by_ino {
13148+ struct dir_context ctx;
13149+ int called, found;
13150+ ino_t ino;
13151+ char *name;
13152+ int namelen;
13153+};
13154+
13155+static int
13156+find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
13157+ loff_t offset, u64 ino, unsigned int d_type)
13158+{
13159+ struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
13160+ ctx);
13161+
13162+ a->called++;
13163+ if (a->ino != ino)
13164+ return 0;
13165+
13166+ memcpy(a->name, name, namelen);
13167+ a->namelen = namelen;
13168+ a->found = 1;
13169+ return 1;
13170+}
13171+
13172+static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
13173+ struct au_nfsd_si_lock *nsi_lock)
13174+{
13175+ struct dentry *dentry, *parent;
13176+ struct file *file;
13177+ struct inode *dir;
13178+ struct find_name_by_ino arg = {
13179+ .ctx = {
13180+ .actor = find_name_by_ino
13181+ }
13182+ };
13183+ int err;
13184+
13185+ parent = path->dentry;
13186+ if (nsi_lock)
13187+ si_read_unlock(parent->d_sb);
13188+ file = vfsub_dentry_open(path, au_dir_roflags);
13189+ dentry = (void *)file;
13190+ if (IS_ERR(file))
13191+ goto out;
13192+
13193+ dentry = ERR_PTR(-ENOMEM);
13194+ arg.name = (void *)__get_free_page(GFP_NOFS);
13195+ if (unlikely(!arg.name))
13196+ goto out_file;
13197+ arg.ino = ino;
13198+ arg.found = 0;
13199+ do {
13200+ arg.called = 0;
13201+ /* smp_mb(); */
13202+ err = vfsub_iterate_dir(file, &arg.ctx);
13203+ } while (!err && !arg.found && arg.called);
13204+ dentry = ERR_PTR(err);
13205+ if (unlikely(err))
13206+ goto out_name;
13207+ /* instead of ENOENT */
13208+ dentry = ERR_PTR(-ESTALE);
13209+ if (!arg.found)
13210+ goto out_name;
13211+
13212+ /* do not call vfsub_lkup_one() */
13213+ dir = d_inode(parent);
13214+ dentry = vfsub_lookup_one_len_unlocked(arg.name, parent, arg.namelen);
13215+ AuTraceErrPtr(dentry);
13216+ if (IS_ERR(dentry))
13217+ goto out_name;
13218+ AuDebugOn(au_test_anon(dentry));
13219+ if (unlikely(d_really_is_negative(dentry))) {
13220+ dput(dentry);
13221+ dentry = ERR_PTR(-ENOENT);
13222+ }
13223+
13224+out_name:
13225+ free_page((unsigned long)arg.name);
13226+out_file:
13227+ fput(file);
13228+out:
13229+ if (unlikely(nsi_lock
13230+ && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
13231+ if (!IS_ERR(dentry)) {
13232+ dput(dentry);
13233+ dentry = ERR_PTR(-ESTALE);
13234+ }
13235+ AuTraceErrPtr(dentry);
13236+ return dentry;
13237+}
13238+
13239+static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
13240+ ino_t dir_ino,
13241+ struct au_nfsd_si_lock *nsi_lock)
13242+{
13243+ struct dentry *dentry;
13244+ struct path path;
13245+
13246+ if (dir_ino != AUFS_ROOT_INO) {
13247+ path.dentry = decode_by_ino(sb, dir_ino, 0);
13248+ dentry = path.dentry;
13249+ if (!path.dentry || IS_ERR(path.dentry))
13250+ goto out;
13251+ AuDebugOn(au_test_anon(path.dentry));
13252+ } else
13253+ path.dentry = dget(sb->s_root);
13254+
13255+ path.mnt = au_mnt_get(sb);
13256+ dentry = au_lkup_by_ino(&path, ino, nsi_lock);
13257+ path_put(&path);
13258+
13259+out:
13260+ AuTraceErrPtr(dentry);
13261+ return dentry;
13262+}
13263+
13264+/* ---------------------------------------------------------------------- */
13265+
13266+static int h_acceptable(void *expv, struct dentry *dentry)
13267+{
13268+ return 1;
13269+}
13270+
13271+static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
13272+ char *buf, int len, struct super_block *sb)
13273+{
13274+ char *p;
13275+ int n;
13276+ struct path path;
13277+
13278+ p = d_path(h_rootpath, buf, len);
13279+ if (IS_ERR(p))
13280+ goto out;
13281+ n = strlen(p);
13282+
13283+ path.mnt = h_rootpath->mnt;
13284+ path.dentry = h_parent;
13285+ p = d_path(&path, buf, len);
13286+ if (IS_ERR(p))
13287+ goto out;
13288+ if (n != 1)
13289+ p += n;
13290+
13291+ path.mnt = au_mnt_get(sb);
13292+ path.dentry = sb->s_root;
13293+ p = d_path(&path, buf, len - strlen(p));
13294+ mntput(path.mnt);
13295+ if (IS_ERR(p))
13296+ goto out;
13297+ if (n != 1)
13298+ p[strlen(p)] = '/';
13299+
13300+out:
13301+ AuTraceErrPtr(p);
13302+ return p;
13303+}
13304+
13305+static
13306+struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
13307+ int fh_len, struct au_nfsd_si_lock *nsi_lock)
13308+{
13309+ struct dentry *dentry, *h_parent, *root;
13310+ struct super_block *h_sb;
13311+ char *pathname, *p;
13312+ struct vfsmount *h_mnt;
13313+ struct au_branch *br;
13314+ int err;
13315+ struct path path;
13316+
13317+ br = au_sbr(sb, nsi_lock->bindex);
13318+ h_mnt = au_br_mnt(br);
13319+ h_sb = h_mnt->mnt_sb;
13320+ /* todo: call lower fh_to_dentry()? fh_to_parent()? */
13321+ lockdep_off();
13322+ h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
13323+ fh_len - Fh_tail, fh[Fh_h_type],
13324+ h_acceptable, /*context*/NULL);
13325+ lockdep_on();
13326+ dentry = h_parent;
13327+ if (unlikely(!h_parent || IS_ERR(h_parent))) {
13328+ AuWarn1("%s decode_fh failed, %ld\n",
13329+ au_sbtype(h_sb), PTR_ERR(h_parent));
13330+ goto out;
13331+ }
13332+ dentry = NULL;
13333+ if (unlikely(au_test_anon(h_parent))) {
13334+ AuWarn1("%s decode_fh returned a disconnected dentry\n",
13335+ au_sbtype(h_sb));
13336+ goto out_h_parent;
13337+ }
13338+
13339+ dentry = ERR_PTR(-ENOMEM);
13340+ pathname = (void *)__get_free_page(GFP_NOFS);
13341+ if (unlikely(!pathname))
13342+ goto out_h_parent;
13343+
13344+ root = sb->s_root;
13345+ path.mnt = h_mnt;
13346+ di_read_lock_parent(root, !AuLock_IR);
13347+ path.dentry = au_h_dptr(root, nsi_lock->bindex);
13348+ di_read_unlock(root, !AuLock_IR);
13349+ p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
13350+ dentry = (void *)p;
13351+ if (IS_ERR(p))
13352+ goto out_pathname;
13353+
13354+ si_read_unlock(sb);
13355+ err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
13356+ dentry = ERR_PTR(err);
13357+ if (unlikely(err))
13358+ goto out_relock;
13359+
13360+ dentry = ERR_PTR(-ENOENT);
13361+ AuDebugOn(au_test_anon(path.dentry));
13362+ if (unlikely(d_really_is_negative(path.dentry)))
13363+ goto out_path;
13364+
13365+ if (ino != d_inode(path.dentry)->i_ino)
13366+ dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
13367+ else
13368+ dentry = dget(path.dentry);
13369+
13370+out_path:
13371+ path_put(&path);
13372+out_relock:
13373+ if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
13374+ if (!IS_ERR(dentry)) {
13375+ dput(dentry);
13376+ dentry = ERR_PTR(-ESTALE);
13377+ }
13378+out_pathname:
13379+ free_page((unsigned long)pathname);
13380+out_h_parent:
13381+ dput(h_parent);
13382+out:
13383+ AuTraceErrPtr(dentry);
13384+ return dentry;
13385+}
13386+
13387+/* ---------------------------------------------------------------------- */
13388+
13389+static struct dentry *
13390+aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
13391+ int fh_type)
13392+{
13393+ struct dentry *dentry;
13394+ __u32 *fh = fid->raw;
13395+ struct au_branch *br;
13396+ ino_t ino, dir_ino;
13397+ struct au_nfsd_si_lock nsi_lock = {
13398+ .force_lock = 0
13399+ };
13400+
13401+ dentry = ERR_PTR(-ESTALE);
13402+ /* it should never happen, but the file handle is unreliable */
13403+ if (unlikely(fh_len < Fh_tail))
13404+ goto out;
13405+ nsi_lock.sigen = fh[Fh_sigen];
13406+ nsi_lock.br_id = fh[Fh_br_id];
13407+
13408+ /* branch id may be wrapped around */
13409+ br = NULL;
13410+ if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
13411+ goto out;
13412+ nsi_lock.force_lock = 1;
13413+
13414+ /* is this inode still cached? */
13415+ ino = decode_ino(fh + Fh_ino);
13416+ /* it should never happen */
13417+ if (unlikely(ino == AUFS_ROOT_INO))
13418+ goto out_unlock;
13419+
13420+ dir_ino = decode_ino(fh + Fh_dir_ino);
13421+ dentry = decode_by_ino(sb, ino, dir_ino);
13422+ if (IS_ERR(dentry))
13423+ goto out_unlock;
13424+ if (dentry)
13425+ goto accept;
13426+
13427+ /* is the parent dir cached? */
13428+ br = au_sbr(sb, nsi_lock.bindex);
13429+ au_lcnt_inc(&br->br_nfiles);
13430+ dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
13431+ if (IS_ERR(dentry))
13432+ goto out_unlock;
13433+ if (dentry)
13434+ goto accept;
13435+
13436+ /* lookup path */
13437+ dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
13438+ if (IS_ERR(dentry))
13439+ goto out_unlock;
13440+ if (unlikely(!dentry))
13441+ /* todo?: make it ESTALE */
13442+ goto out_unlock;
13443+
13444+accept:
13445+ if (!au_digen_test(dentry, au_sigen(sb))
13446+ && d_inode(dentry)->i_generation == fh[Fh_igen])
13447+ goto out_unlock; /* success */
13448+
13449+ dput(dentry);
13450+ dentry = ERR_PTR(-ESTALE);
13451+out_unlock:
13452+ if (br)
13453+ au_lcnt_dec(&br->br_nfiles);
13454+ si_read_unlock(sb);
13455+out:
13456+ AuTraceErrPtr(dentry);
13457+ return dentry;
13458+}
13459+
13460+#if 0 /* reserved for future use */
13461+/* support subtreecheck option */
13462+static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
13463+ int fh_len, int fh_type)
13464+{
13465+ struct dentry *parent;
13466+ __u32 *fh = fid->raw;
13467+ ino_t dir_ino;
13468+
13469+ dir_ino = decode_ino(fh + Fh_dir_ino);
13470+ parent = decode_by_ino(sb, dir_ino, 0);
13471+ if (IS_ERR(parent))
13472+ goto out;
13473+ if (!parent)
13474+ parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
13475+ dir_ino, fh, fh_len);
13476+
13477+out:
13478+ AuTraceErrPtr(parent);
13479+ return parent;
13480+}
13481+#endif
13482+
13483+/* ---------------------------------------------------------------------- */
13484+
13485+static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
13486+ struct inode *dir)
13487+{
13488+ int err;
13489+ aufs_bindex_t bindex;
13490+ struct super_block *sb, *h_sb;
13491+ struct dentry *dentry, *parent, *h_parent;
13492+ struct inode *h_dir;
13493+ struct au_branch *br;
13494+
13495+ err = -ENOSPC;
13496+ if (unlikely(*max_len <= Fh_tail)) {
13497+ AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
13498+ goto out;
13499+ }
13500+
13501+ err = FILEID_ROOT;
13502+ if (inode->i_ino == AUFS_ROOT_INO) {
13503+ AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
13504+ goto out;
13505+ }
13506+
13507+ h_parent = NULL;
13508+ sb = inode->i_sb;
13509+ err = si_read_lock(sb, AuLock_FLUSH);
13510+ if (unlikely(err))
13511+ goto out;
13512+
13513+#ifdef CONFIG_AUFS_DEBUG
13514+ if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13515+ AuWarn1("NFS-exporting requires xino\n");
13516+#endif
13517+ err = -EIO;
13518+ parent = NULL;
13519+ ii_read_lock_child(inode);
13520+ bindex = au_ibtop(inode);
13521+ if (!dir) {
13522+ dentry = d_find_any_alias(inode);
13523+ if (unlikely(!dentry))
13524+ goto out_unlock;
13525+ AuDebugOn(au_test_anon(dentry));
13526+ parent = dget_parent(dentry);
13527+ dput(dentry);
13528+ if (unlikely(!parent))
13529+ goto out_unlock;
13530+ if (d_really_is_positive(parent))
13531+ dir = d_inode(parent);
13532+ }
13533+
13534+ ii_read_lock_parent(dir);
13535+ h_dir = au_h_iptr(dir, bindex);
13536+ ii_read_unlock(dir);
13537+ if (unlikely(!h_dir))
13538+ goto out_parent;
13539+ h_parent = d_find_any_alias(h_dir);
13540+ if (unlikely(!h_parent))
13541+ goto out_hparent;
13542+
13543+ err = -EPERM;
13544+ br = au_sbr(sb, bindex);
13545+ h_sb = au_br_sb(br);
13546+ if (unlikely(!h_sb->s_export_op)) {
13547+ AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
13548+ goto out_hparent;
13549+ }
13550+
13551+ fh[Fh_br_id] = br->br_id;
13552+ fh[Fh_sigen] = au_sigen(sb);
13553+ encode_ino(fh + Fh_ino, inode->i_ino);
13554+ encode_ino(fh + Fh_dir_ino, dir->i_ino);
13555+ fh[Fh_igen] = inode->i_generation;
13556+
13557+ *max_len -= Fh_tail;
13558+ fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
13559+ max_len,
13560+ /*connectable or subtreecheck*/0);
13561+ err = fh[Fh_h_type];
13562+ *max_len += Fh_tail;
13563+ /* todo: macros? */
13564+ if (err != FILEID_INVALID)
13565+ err = 99;
13566+ else
13567+ AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
13568+
13569+out_hparent:
13570+ dput(h_parent);
13571+out_parent:
13572+ dput(parent);
13573+out_unlock:
13574+ ii_read_unlock(inode);
13575+ si_read_unlock(sb);
13576+out:
13577+ if (unlikely(err < 0))
13578+ err = FILEID_INVALID;
13579+ return err;
13580+}
13581+
13582+/* ---------------------------------------------------------------------- */
13583+
13584+static int aufs_commit_metadata(struct inode *inode)
13585+{
13586+ int err;
13587+ aufs_bindex_t bindex;
13588+ struct super_block *sb;
13589+ struct inode *h_inode;
13590+ int (*f)(struct inode *inode);
13591+
13592+ sb = inode->i_sb;
13593+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13594+ ii_write_lock_child(inode);
13595+ bindex = au_ibtop(inode);
13596+ AuDebugOn(bindex < 0);
13597+ h_inode = au_h_iptr(inode, bindex);
13598+
13599+ f = h_inode->i_sb->s_export_op->commit_metadata;
13600+ if (f)
13601+ err = f(h_inode);
13602+ else {
13603+ struct writeback_control wbc = {
13604+ .sync_mode = WB_SYNC_ALL,
13605+ .nr_to_write = 0 /* metadata only */
13606+ };
13607+
13608+ err = sync_inode(h_inode, &wbc);
13609+ }
13610+
13611+ au_cpup_attr_timesizes(inode);
13612+ ii_write_unlock(inode);
13613+ si_read_unlock(sb);
13614+ return err;
13615+}
13616+
13617+/* ---------------------------------------------------------------------- */
13618+
13619+static struct export_operations aufs_export_op = {
13620+ .fh_to_dentry = aufs_fh_to_dentry,
13621+ /* .fh_to_parent = aufs_fh_to_parent, */
13622+ .encode_fh = aufs_encode_fh,
13623+ .commit_metadata = aufs_commit_metadata
13624+};
13625+
13626+void au_export_init(struct super_block *sb)
13627+{
13628+ struct au_sbinfo *sbinfo;
13629+ __u32 u;
13630+
13631+ BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
13632+ && IS_MODULE(CONFIG_EXPORTFS),
13633+ AUFS_NAME ": unsupported configuration "
13634+ "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
13635+
13636+ sb->s_export_op = &aufs_export_op;
13637+ sbinfo = au_sbi(sb);
13638+ sbinfo->si_xigen = NULL;
13639+ get_random_bytes(&u, sizeof(u));
13640+ BUILD_BUG_ON(sizeof(u) != sizeof(int));
13641+ atomic_set(&sbinfo->si_xigen_next, u);
13642+}
13643diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
13644--- /usr/share/empty/fs/aufs/fhsm.c 1970-01-01 01:00:00.000000000 +0100
13645+++ linux/fs/aufs/fhsm.c 2021-02-24 13:33:42.744347058 +0100
13646@@ -0,0 +1,427 @@
13647+// SPDX-License-Identifier: GPL-2.0
13648+/*
13649+ * Copyright (C) 2011-2020 Junjiro R. Okajima
13650+ *
13651+ * This program, aufs is free software; you can redistribute it and/or modify
13652+ * it under the terms of the GNU General Public License as published by
13653+ * the Free Software Foundation; either version 2 of the License, or
13654+ * (at your option) any later version.
13655+ *
13656+ * This program is distributed in the hope that it will be useful,
13657+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13658+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13659+ * GNU General Public License for more details.
13660+ *
13661+ * You should have received a copy of the GNU General Public License
13662+ * along with this program; if not, write to the Free Software
13663+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
13664+ */
13665+
13666+/*
13667+ * File-based Hierarchy Storage Management
13668+ */
13669+
13670+#include <linux/anon_inodes.h>
13671+#include <linux/poll.h>
13672+#include <linux/seq_file.h>
13673+#include <linux/statfs.h>
13674+#include "aufs.h"
13675+
13676+static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
13677+{
13678+ struct au_sbinfo *sbinfo;
13679+ struct au_fhsm *fhsm;
13680+
13681+ SiMustAnyLock(sb);
13682+
13683+ sbinfo = au_sbi(sb);
13684+ fhsm = &sbinfo->si_fhsm;
13685+ AuDebugOn(!fhsm);
13686+ return fhsm->fhsm_bottom;
13687+}
13688+
13689+void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
13690+{
13691+ struct au_sbinfo *sbinfo;
13692+ struct au_fhsm *fhsm;
13693+
13694+ SiMustWriteLock(sb);
13695+
13696+ sbinfo = au_sbi(sb);
13697+ fhsm = &sbinfo->si_fhsm;
13698+ AuDebugOn(!fhsm);
13699+ fhsm->fhsm_bottom = bindex;
13700+}
13701+
13702+/* ---------------------------------------------------------------------- */
13703+
13704+static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
13705+{
13706+ struct au_br_fhsm *bf;
13707+
13708+ bf = br->br_fhsm;
13709+ MtxMustLock(&bf->bf_lock);
13710+
13711+ return !bf->bf_readable
13712+ || time_after(jiffies,
13713+ bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
13714+}
13715+
13716+/* ---------------------------------------------------------------------- */
13717+
13718+static void au_fhsm_notify(struct super_block *sb, int val)
13719+{
13720+ struct au_sbinfo *sbinfo;
13721+ struct au_fhsm *fhsm;
13722+
13723+ SiMustAnyLock(sb);
13724+
13725+ sbinfo = au_sbi(sb);
13726+ fhsm = &sbinfo->si_fhsm;
13727+ if (au_fhsm_pid(fhsm)
13728+ && atomic_read(&fhsm->fhsm_readable) != -1) {
13729+ atomic_set(&fhsm->fhsm_readable, val);
13730+ if (val)
13731+ wake_up(&fhsm->fhsm_wqh);
13732+ }
13733+}
13734+
13735+static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
13736+ struct aufs_stfs *rstfs, int do_lock, int do_notify)
13737+{
13738+ int err;
13739+ struct au_branch *br;
13740+ struct au_br_fhsm *bf;
13741+
13742+ br = au_sbr(sb, bindex);
13743+ AuDebugOn(au_br_rdonly(br));
13744+ bf = br->br_fhsm;
13745+ AuDebugOn(!bf);
13746+
13747+ if (do_lock)
13748+ mutex_lock(&bf->bf_lock);
13749+ else
13750+ MtxMustLock(&bf->bf_lock);
13751+
13752+ /* sb->s_root for NFS is unreliable */
13753+ err = au_br_stfs(br, &bf->bf_stfs);
13754+ if (unlikely(err)) {
13755+ AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
13756+ goto out;
13757+ }
13758+
13759+ bf->bf_jiffy = jiffies;
13760+ bf->bf_readable = 1;
13761+ if (do_notify)
13762+ au_fhsm_notify(sb, /*val*/1);
13763+ if (rstfs)
13764+ *rstfs = bf->bf_stfs;
13765+
13766+out:
13767+ if (do_lock)
13768+ mutex_unlock(&bf->bf_lock);
13769+ au_fhsm_notify(sb, /*val*/1);
13770+
13771+ return err;
13772+}
13773+
13774+void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
13775+{
13776+ int err;
13777+ struct au_sbinfo *sbinfo;
13778+ struct au_fhsm *fhsm;
13779+ struct au_branch *br;
13780+ struct au_br_fhsm *bf;
13781+
13782+ AuDbg("b%d, force %d\n", bindex, force);
13783+ SiMustAnyLock(sb);
13784+
13785+ sbinfo = au_sbi(sb);
13786+ fhsm = &sbinfo->si_fhsm;
13787+ if (!au_ftest_si(sbinfo, FHSM)
13788+ || fhsm->fhsm_bottom == bindex)
13789+ return;
13790+
13791+ br = au_sbr(sb, bindex);
13792+ bf = br->br_fhsm;
13793+ AuDebugOn(!bf);
13794+ mutex_lock(&bf->bf_lock);
13795+ if (force
13796+ || au_fhsm_pid(fhsm)
13797+ || au_fhsm_test_jiffy(sbinfo, br))
13798+ err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
13799+ /*do_notify*/1);
13800+ mutex_unlock(&bf->bf_lock);
13801+}
13802+
13803+void au_fhsm_wrote_all(struct super_block *sb, int force)
13804+{
13805+ aufs_bindex_t bindex, bbot;
13806+ struct au_branch *br;
13807+
13808+ /* exclude the bottom */
13809+ bbot = au_fhsm_bottom(sb);
13810+ for (bindex = 0; bindex < bbot; bindex++) {
13811+ br = au_sbr(sb, bindex);
13812+ if (au_br_fhsm(br->br_perm))
13813+ au_fhsm_wrote(sb, bindex, force);
13814+ }
13815+}
13816+
13817+/* ---------------------------------------------------------------------- */
13818+
13819+static __poll_t au_fhsm_poll(struct file *file, struct poll_table_struct *wait)
13820+{
13821+ __poll_t mask;
13822+ struct au_sbinfo *sbinfo;
13823+ struct au_fhsm *fhsm;
13824+
13825+ mask = 0;
13826+ sbinfo = file->private_data;
13827+ fhsm = &sbinfo->si_fhsm;
13828+ poll_wait(file, &fhsm->fhsm_wqh, wait);
13829+ if (atomic_read(&fhsm->fhsm_readable))
13830+ mask = EPOLLIN /* | EPOLLRDNORM */;
13831+
13832+ if (!mask)
13833+ AuDbg("mask 0x%x\n", mask);
13834+ return mask;
13835+}
13836+
13837+static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
13838+ struct aufs_stfs *stfs, __s16 brid)
13839+{
13840+ int err;
13841+
13842+ err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
13843+ if (!err)
13844+ err = __put_user(brid, &stbr->brid);
13845+ if (unlikely(err))
13846+ err = -EFAULT;
13847+
13848+ return err;
13849+}
13850+
13851+static ssize_t au_fhsm_do_read(struct super_block *sb,
13852+ struct aufs_stbr __user *stbr, size_t count)
13853+{
13854+ ssize_t err;
13855+ int nstbr;
13856+ aufs_bindex_t bindex, bbot;
13857+ struct au_branch *br;
13858+ struct au_br_fhsm *bf;
13859+
13860+ /* except the bottom branch */
13861+ err = 0;
13862+ nstbr = 0;
13863+ bbot = au_fhsm_bottom(sb);
13864+ for (bindex = 0; !err && bindex < bbot; bindex++) {
13865+ br = au_sbr(sb, bindex);
13866+ if (!au_br_fhsm(br->br_perm))
13867+ continue;
13868+
13869+ bf = br->br_fhsm;
13870+ mutex_lock(&bf->bf_lock);
13871+ if (bf->bf_readable) {
13872+ err = -EFAULT;
13873+ if (count >= sizeof(*stbr))
13874+ err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
13875+ br->br_id);
13876+ if (!err) {
13877+ bf->bf_readable = 0;
13878+ count -= sizeof(*stbr);
13879+ nstbr++;
13880+ }
13881+ }
13882+ mutex_unlock(&bf->bf_lock);
13883+ }
13884+ if (!err)
13885+ err = sizeof(*stbr) * nstbr;
13886+
13887+ return err;
13888+}
13889+
13890+static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
13891+ loff_t *pos)
13892+{
13893+ ssize_t err;
13894+ int readable;
13895+ aufs_bindex_t nfhsm, bindex, bbot;
13896+ struct au_sbinfo *sbinfo;
13897+ struct au_fhsm *fhsm;
13898+ struct au_branch *br;
13899+ struct super_block *sb;
13900+
13901+ err = 0;
13902+ sbinfo = file->private_data;
13903+ fhsm = &sbinfo->si_fhsm;
13904+need_data:
13905+ spin_lock_irq(&fhsm->fhsm_wqh.lock);
13906+ if (!atomic_read(&fhsm->fhsm_readable)) {
13907+ if (vfsub_file_flags(file) & O_NONBLOCK)
13908+ err = -EAGAIN;
13909+ else
13910+ err = wait_event_interruptible_locked_irq
13911+ (fhsm->fhsm_wqh,
13912+ atomic_read(&fhsm->fhsm_readable));
13913+ }
13914+ spin_unlock_irq(&fhsm->fhsm_wqh.lock);
13915+ if (unlikely(err))
13916+ goto out;
13917+
13918+ /* sb may already be dead */
13919+ au_rw_read_lock(&sbinfo->si_rwsem);
13920+ readable = atomic_read(&fhsm->fhsm_readable);
13921+ if (readable > 0) {
13922+ sb = sbinfo->si_sb;
13923+ AuDebugOn(!sb);
13924+ /* exclude the bottom branch */
13925+ nfhsm = 0;
13926+ bbot = au_fhsm_bottom(sb);
13927+ for (bindex = 0; bindex < bbot; bindex++) {
13928+ br = au_sbr(sb, bindex);
13929+ if (au_br_fhsm(br->br_perm))
13930+ nfhsm++;
13931+ }
13932+ err = -EMSGSIZE;
13933+ if (nfhsm * sizeof(struct aufs_stbr) <= count) {
13934+ atomic_set(&fhsm->fhsm_readable, 0);
13935+ err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
13936+ count);
13937+ }
13938+ }
13939+ au_rw_read_unlock(&sbinfo->si_rwsem);
13940+ if (!readable)
13941+ goto need_data;
13942+
13943+out:
13944+ return err;
13945+}
13946+
13947+static int au_fhsm_release(struct inode *inode, struct file *file)
13948+{
13949+ struct au_sbinfo *sbinfo;
13950+ struct au_fhsm *fhsm;
13951+
13952+ /* sb may already be dead */
13953+ sbinfo = file->private_data;
13954+ fhsm = &sbinfo->si_fhsm;
13955+ spin_lock(&fhsm->fhsm_spin);
13956+ fhsm->fhsm_pid = 0;
13957+ spin_unlock(&fhsm->fhsm_spin);
13958+ kobject_put(&sbinfo->si_kobj);
13959+
13960+ return 0;
13961+}
13962+
13963+static const struct file_operations au_fhsm_fops = {
13964+ .owner = THIS_MODULE,
13965+ .llseek = noop_llseek,
13966+ .read = au_fhsm_read,
13967+ .poll = au_fhsm_poll,
13968+ .release = au_fhsm_release
13969+};
13970+
13971+int au_fhsm_fd(struct super_block *sb, int oflags)
13972+{
13973+ int err, fd;
13974+ struct au_sbinfo *sbinfo;
13975+ struct au_fhsm *fhsm;
13976+
13977+ err = -EPERM;
13978+ if (unlikely(!capable(CAP_SYS_ADMIN)))
13979+ goto out;
13980+
13981+ err = -EINVAL;
13982+ if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
13983+ goto out;
13984+
13985+ err = 0;
13986+ sbinfo = au_sbi(sb);
13987+ fhsm = &sbinfo->si_fhsm;
13988+ spin_lock(&fhsm->fhsm_spin);
13989+ if (!fhsm->fhsm_pid)
13990+ fhsm->fhsm_pid = current->pid;
13991+ else
13992+ err = -EBUSY;
13993+ spin_unlock(&fhsm->fhsm_spin);
13994+ if (unlikely(err))
13995+ goto out;
13996+
13997+ oflags |= O_RDONLY;
13998+ /* oflags |= FMODE_NONOTIFY; */
13999+ fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
14000+ err = fd;
14001+ if (unlikely(fd < 0))
14002+ goto out_pid;
14003+
14004+ /* succeed regardless 'fhsm' status */
14005+ kobject_get(&sbinfo->si_kobj);
14006+ si_noflush_read_lock(sb);
14007+ if (au_ftest_si(sbinfo, FHSM))
14008+ au_fhsm_wrote_all(sb, /*force*/0);
14009+ si_read_unlock(sb);
14010+ goto out; /* success */
14011+
14012+out_pid:
14013+ spin_lock(&fhsm->fhsm_spin);
14014+ fhsm->fhsm_pid = 0;
14015+ spin_unlock(&fhsm->fhsm_spin);
14016+out:
14017+ AuTraceErr(err);
14018+ return err;
14019+}
14020+
14021+/* ---------------------------------------------------------------------- */
14022+
14023+int au_fhsm_br_alloc(struct au_branch *br)
14024+{
14025+ int err;
14026+
14027+ err = 0;
14028+ br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
14029+ if (br->br_fhsm)
14030+ au_br_fhsm_init(br->br_fhsm);
14031+ else
14032+ err = -ENOMEM;
14033+
14034+ return err;
14035+}
14036+
14037+/* ---------------------------------------------------------------------- */
14038+
14039+void au_fhsm_fin(struct super_block *sb)
14040+{
14041+ au_fhsm_notify(sb, /*val*/-1);
14042+}
14043+
14044+void au_fhsm_init(struct au_sbinfo *sbinfo)
14045+{
14046+ struct au_fhsm *fhsm;
14047+
14048+ fhsm = &sbinfo->si_fhsm;
14049+ spin_lock_init(&fhsm->fhsm_spin);
14050+ init_waitqueue_head(&fhsm->fhsm_wqh);
14051+ atomic_set(&fhsm->fhsm_readable, 0);
14052+ fhsm->fhsm_expire
14053+ = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
14054+ fhsm->fhsm_bottom = -1;
14055+}
14056+
14057+void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
14058+{
14059+ sbinfo->si_fhsm.fhsm_expire
14060+ = msecs_to_jiffies(sec * MSEC_PER_SEC);
14061+}
14062+
14063+void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
14064+{
14065+ unsigned int u;
14066+
14067+ if (!au_ftest_si(sbinfo, FHSM))
14068+ return;
14069+
14070+ u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
14071+ if (u != AUFS_FHSM_CACHE_DEF_SEC)
14072+ seq_printf(seq, ",fhsm_sec=%u", u);
14073+}
14074diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
14075--- /usr/share/empty/fs/aufs/file.c 1970-01-01 01:00:00.000000000 +0100
14076+++ linux/fs/aufs/file.c 2021-02-24 13:33:42.744347058 +0100
14077@@ -0,0 +1,863 @@
14078+// SPDX-License-Identifier: GPL-2.0
14079+/*
14080+ * Copyright (C) 2005-2020 Junjiro R. Okajima
14081+ *
14082+ * This program, aufs is free software; you can redistribute it and/or modify
14083+ * it under the terms of the GNU General Public License as published by
14084+ * the Free Software Foundation; either version 2 of the License, or
14085+ * (at your option) any later version.
14086+ *
14087+ * This program is distributed in the hope that it will be useful,
14088+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14089+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14090+ * GNU General Public License for more details.
14091+ *
14092+ * You should have received a copy of the GNU General Public License
14093+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
14094+ */
14095+
14096+/*
14097+ * handling file/dir, and address_space operation
14098+ */
14099+
14100+#ifdef CONFIG_AUFS_DEBUG
14101+#include <linux/migrate.h>
14102+#endif
14103+#include <linux/pagemap.h>
14104+#include "aufs.h"
14105+
14106+/* drop flags for writing */
14107+unsigned int au_file_roflags(unsigned int flags)
14108+{
14109+ flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
14110+ flags |= O_RDONLY | O_NOATIME;
14111+ return flags;
14112+}
14113+
14114+/* common functions to regular file and dir */
14115+struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
14116+ struct file *file, int force_wr)
14117+{
14118+ struct file *h_file;
14119+ struct dentry *h_dentry;
14120+ struct inode *h_inode;
14121+ struct super_block *sb;
14122+ struct au_branch *br;
14123+ struct path h_path;
14124+ int err;
14125+
14126+ /* a race condition can happen between open and unlink/rmdir */
14127+ h_file = ERR_PTR(-ENOENT);
14128+ h_dentry = au_h_dptr(dentry, bindex);
14129+ if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
14130+ goto out;
14131+ h_inode = d_inode(h_dentry);
14132+ spin_lock(&h_dentry->d_lock);
14133+ err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
14134+ /* || !d_inode(dentry)->i_nlink */
14135+ ;
14136+ spin_unlock(&h_dentry->d_lock);
14137+ if (unlikely(err))
14138+ goto out;
14139+
14140+ sb = dentry->d_sb;
14141+ br = au_sbr(sb, bindex);
14142+ err = au_br_test_oflag(flags, br);
14143+ h_file = ERR_PTR(err);
14144+ if (unlikely(err))
14145+ goto out;
14146+
14147+ /* drop flags for writing */
14148+ if (au_test_ro(sb, bindex, d_inode(dentry))) {
14149+ if (force_wr && !(flags & O_WRONLY))
14150+ force_wr = 0;
14151+ flags = au_file_roflags(flags);
14152+ if (force_wr) {
14153+ h_file = ERR_PTR(-EROFS);
14154+ flags = au_file_roflags(flags);
14155+ if (unlikely(vfsub_native_ro(h_inode)
14156+ || IS_APPEND(h_inode)))
14157+ goto out;
14158+ flags &= ~O_ACCMODE;
14159+ flags |= O_WRONLY;
14160+ }
14161+ }
14162+ flags &= ~O_CREAT;
14163+ au_lcnt_inc(&br->br_nfiles);
14164+ h_path.dentry = h_dentry;
14165+ h_path.mnt = au_br_mnt(br);
14166+ h_file = vfsub_dentry_open(&h_path, flags);
14167+ if (IS_ERR(h_file))
14168+ goto out_br;
14169+
14170+ if (flags & __FMODE_EXEC) {
14171+ err = deny_write_access(h_file);
14172+ if (unlikely(err)) {
14173+ fput(h_file);
14174+ h_file = ERR_PTR(err);
14175+ goto out_br;
14176+ }
14177+ }
14178+ fsnotify_open(h_file);
14179+ goto out; /* success */
14180+
14181+out_br:
14182+ au_lcnt_dec(&br->br_nfiles);
14183+out:
14184+ return h_file;
14185+}
14186+
14187+static int au_cmoo(struct dentry *dentry)
14188+{
14189+ int err, cmoo, matched;
14190+ unsigned int udba;
14191+ struct path h_path;
14192+ struct au_pin pin;
14193+ struct au_cp_generic cpg = {
14194+ .dentry = dentry,
14195+ .bdst = -1,
14196+ .bsrc = -1,
14197+ .len = -1,
14198+ .pin = &pin,
14199+ .flags = AuCpup_DTIME | AuCpup_HOPEN
14200+ };
14201+ struct inode *delegated;
14202+ struct super_block *sb;
14203+ struct au_sbinfo *sbinfo;
14204+ struct au_fhsm *fhsm;
14205+ pid_t pid;
14206+ struct au_branch *br;
14207+ struct dentry *parent;
14208+ struct au_hinode *hdir;
14209+
14210+ DiMustWriteLock(dentry);
14211+ IiMustWriteLock(d_inode(dentry));
14212+
14213+ err = 0;
14214+ if (IS_ROOT(dentry))
14215+ goto out;
14216+ cpg.bsrc = au_dbtop(dentry);
14217+ if (!cpg.bsrc)
14218+ goto out;
14219+
14220+ sb = dentry->d_sb;
14221+ sbinfo = au_sbi(sb);
14222+ fhsm = &sbinfo->si_fhsm;
14223+ pid = au_fhsm_pid(fhsm);
14224+ rcu_read_lock();
14225+ matched = (pid
14226+ && (current->pid == pid
14227+ || rcu_dereference(current->real_parent)->pid == pid));
14228+ rcu_read_unlock();
14229+ if (matched)
14230+ goto out;
14231+
14232+ br = au_sbr(sb, cpg.bsrc);
14233+ cmoo = au_br_cmoo(br->br_perm);
14234+ if (!cmoo)
14235+ goto out;
14236+ if (!d_is_reg(dentry))
14237+ cmoo &= AuBrAttr_COO_ALL;
14238+ if (!cmoo)
14239+ goto out;
14240+
14241+ parent = dget_parent(dentry);
14242+ di_write_lock_parent(parent);
14243+ err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
14244+ cpg.bdst = err;
14245+ if (unlikely(err < 0)) {
14246+ err = 0; /* there is no upper writable branch */
14247+ goto out_dgrade;
14248+ }
14249+ AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
14250+
14251+ /* do not respect the coo attrib for the target branch */
14252+ err = au_cpup_dirs(dentry, cpg.bdst);
14253+ if (unlikely(err))
14254+ goto out_dgrade;
14255+
14256+ di_downgrade_lock(parent, AuLock_IR);
14257+ udba = au_opt_udba(sb);
14258+ err = au_pin(&pin, dentry, cpg.bdst, udba,
14259+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14260+ if (unlikely(err))
14261+ goto out_parent;
14262+
14263+ err = au_sio_cpup_simple(&cpg);
14264+ au_unpin(&pin);
14265+ if (unlikely(err))
14266+ goto out_parent;
14267+ if (!(cmoo & AuBrWAttr_MOO))
14268+ goto out_parent; /* success */
14269+
14270+ err = au_pin(&pin, dentry, cpg.bsrc, udba,
14271+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14272+ if (unlikely(err))
14273+ goto out_parent;
14274+
14275+ h_path.mnt = au_br_mnt(br);
14276+ h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
14277+ hdir = au_hi(d_inode(parent), cpg.bsrc);
14278+ delegated = NULL;
14279+ err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
14280+ au_unpin(&pin);
14281+ /* todo: keep h_dentry or not? */
14282+ if (unlikely(err == -EWOULDBLOCK)) {
14283+ pr_warn("cannot retry for NFSv4 delegation"
14284+ " for an internal unlink\n");
14285+ iput(delegated);
14286+ }
14287+ if (unlikely(err)) {
14288+ pr_err("unlink %pd after coo failed (%d), ignored\n",
14289+ dentry, err);
14290+ err = 0;
14291+ }
14292+ goto out_parent; /* success */
14293+
14294+out_dgrade:
14295+ di_downgrade_lock(parent, AuLock_IR);
14296+out_parent:
14297+ di_read_unlock(parent, AuLock_IR);
14298+ dput(parent);
14299+out:
14300+ AuTraceErr(err);
14301+ return err;
14302+}
14303+
14304+int au_do_open(struct file *file, struct au_do_open_args *args)
14305+{
14306+ int err, aopen = args->aopen;
14307+ struct dentry *dentry;
14308+ struct au_finfo *finfo;
14309+
14310+ if (!aopen)
14311+ err = au_finfo_init(file, args->fidir);
14312+ else {
14313+ lockdep_off();
14314+ err = au_finfo_init(file, args->fidir);
14315+ lockdep_on();
14316+ }
14317+ if (unlikely(err))
14318+ goto out;
14319+
14320+ dentry = file->f_path.dentry;
14321+ AuDebugOn(IS_ERR_OR_NULL(dentry));
14322+ di_write_lock_child(dentry);
14323+ err = au_cmoo(dentry);
14324+ di_downgrade_lock(dentry, AuLock_IR);
14325+ if (!err) {
14326+ if (!aopen)
14327+ err = args->open(file, vfsub_file_flags(file), NULL);
14328+ else {
14329+ lockdep_off();
14330+ err = args->open(file, vfsub_file_flags(file),
14331+ args->h_file);
14332+ lockdep_on();
14333+ }
14334+ }
14335+ di_read_unlock(dentry, AuLock_IR);
14336+
14337+ finfo = au_fi(file);
14338+ if (!err) {
14339+ finfo->fi_file = file;
14340+ au_hbl_add(&finfo->fi_hlist,
14341+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
14342+ }
14343+ if (!aopen)
14344+ fi_write_unlock(file);
14345+ else {
14346+ lockdep_off();
14347+ fi_write_unlock(file);
14348+ lockdep_on();
14349+ }
14350+ if (unlikely(err)) {
14351+ finfo->fi_hdir = NULL;
14352+ au_finfo_fin(file);
14353+ }
14354+
14355+out:
14356+ AuTraceErr(err);
14357+ return err;
14358+}
14359+
14360+int au_reopen_nondir(struct file *file)
14361+{
14362+ int err;
14363+ aufs_bindex_t btop;
14364+ struct dentry *dentry;
14365+ struct au_branch *br;
14366+ struct file *h_file, *h_file_tmp;
14367+
14368+ dentry = file->f_path.dentry;
14369+ btop = au_dbtop(dentry);
14370+ br = au_sbr(dentry->d_sb, btop);
14371+ h_file_tmp = NULL;
14372+ if (au_fbtop(file) == btop) {
14373+ h_file = au_hf_top(file);
14374+ if (file->f_mode == h_file->f_mode)
14375+ return 0; /* success */
14376+ h_file_tmp = h_file;
14377+ get_file(h_file_tmp);
14378+ au_lcnt_inc(&br->br_nfiles);
14379+ au_set_h_fptr(file, btop, NULL);
14380+ }
14381+ AuDebugOn(au_fi(file)->fi_hdir);
14382+ /*
14383+ * it can happen
14384+ * file exists on both of rw and ro
14385+ * open --> dbtop and fbtop are both 0
14386+ * prepend a branch as rw, "rw" become ro
14387+ * remove rw/file
14388+ * delete the top branch, "rw" becomes rw again
14389+ * --> dbtop is 1, fbtop is still 0
14390+ * write --> fbtop is 0 but dbtop is 1
14391+ */
14392+ /* AuDebugOn(au_fbtop(file) < btop); */
14393+
14394+ h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
14395+ file, /*force_wr*/0);
14396+ err = PTR_ERR(h_file);
14397+ if (IS_ERR(h_file)) {
14398+ if (h_file_tmp) {
14399+ /* revert */
14400+ au_set_h_fptr(file, btop, h_file_tmp);
14401+ h_file_tmp = NULL;
14402+ }
14403+ goto out; /* todo: close all? */
14404+ }
14405+
14406+ err = 0;
14407+ au_set_fbtop(file, btop);
14408+ au_set_h_fptr(file, btop, h_file);
14409+ au_update_figen(file);
14410+ /* todo: necessary? */
14411+ /* file->f_ra = h_file->f_ra; */
14412+
14413+out:
14414+ if (h_file_tmp) {
14415+ fput(h_file_tmp);
14416+ au_lcnt_dec(&br->br_nfiles);
14417+ }
14418+ return err;
14419+}
14420+
14421+/* ---------------------------------------------------------------------- */
14422+
14423+static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
14424+ struct dentry *hi_wh)
14425+{
14426+ int err;
14427+ aufs_bindex_t btop;
14428+ struct au_dinfo *dinfo;
14429+ struct dentry *h_dentry;
14430+ struct au_hdentry *hdp;
14431+
14432+ dinfo = au_di(file->f_path.dentry);
14433+ AuRwMustWriteLock(&dinfo->di_rwsem);
14434+
14435+ btop = dinfo->di_btop;
14436+ dinfo->di_btop = btgt;
14437+ hdp = au_hdentry(dinfo, btgt);
14438+ h_dentry = hdp->hd_dentry;
14439+ hdp->hd_dentry = hi_wh;
14440+ err = au_reopen_nondir(file);
14441+ hdp->hd_dentry = h_dentry;
14442+ dinfo->di_btop = btop;
14443+
14444+ return err;
14445+}
14446+
14447+static int au_ready_to_write_wh(struct file *file, loff_t len,
14448+ aufs_bindex_t bcpup, struct au_pin *pin)
14449+{
14450+ int err;
14451+ struct inode *inode, *h_inode;
14452+ struct dentry *h_dentry, *hi_wh;
14453+ struct au_cp_generic cpg = {
14454+ .dentry = file->f_path.dentry,
14455+ .bdst = bcpup,
14456+ .bsrc = -1,
14457+ .len = len,
14458+ .pin = pin
14459+ };
14460+
14461+ au_update_dbtop(cpg.dentry);
14462+ inode = d_inode(cpg.dentry);
14463+ h_inode = NULL;
14464+ if (au_dbtop(cpg.dentry) <= bcpup
14465+ && au_dbbot(cpg.dentry) >= bcpup) {
14466+ h_dentry = au_h_dptr(cpg.dentry, bcpup);
14467+ if (h_dentry && d_is_positive(h_dentry))
14468+ h_inode = d_inode(h_dentry);
14469+ }
14470+ hi_wh = au_hi_wh(inode, bcpup);
14471+ if (!hi_wh && !h_inode)
14472+ err = au_sio_cpup_wh(&cpg, file);
14473+ else
14474+ /* already copied-up after unlink */
14475+ err = au_reopen_wh(file, bcpup, hi_wh);
14476+
14477+ if (!err
14478+ && (inode->i_nlink > 1
14479+ || (inode->i_state & I_LINKABLE))
14480+ && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
14481+ au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
14482+
14483+ return err;
14484+}
14485+
14486+/*
14487+ * prepare the @file for writing.
14488+ */
14489+int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
14490+{
14491+ int err;
14492+ aufs_bindex_t dbtop;
14493+ struct dentry *parent;
14494+ struct inode *inode;
14495+ struct super_block *sb;
14496+ struct file *h_file;
14497+ struct au_cp_generic cpg = {
14498+ .dentry = file->f_path.dentry,
14499+ .bdst = -1,
14500+ .bsrc = -1,
14501+ .len = len,
14502+ .pin = pin,
14503+ .flags = AuCpup_DTIME
14504+ };
14505+
14506+ sb = cpg.dentry->d_sb;
14507+ inode = d_inode(cpg.dentry);
14508+ cpg.bsrc = au_fbtop(file);
14509+ err = au_test_ro(sb, cpg.bsrc, inode);
14510+ if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
14511+ err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
14512+ /*flags*/0);
14513+ goto out;
14514+ }
14515+
14516+ /* need to cpup or reopen */
14517+ parent = dget_parent(cpg.dentry);
14518+ di_write_lock_parent(parent);
14519+ err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14520+ cpg.bdst = err;
14521+ if (unlikely(err < 0))
14522+ goto out_dgrade;
14523+ err = 0;
14524+
14525+ if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
14526+ err = au_cpup_dirs(cpg.dentry, cpg.bdst);
14527+ if (unlikely(err))
14528+ goto out_dgrade;
14529+ }
14530+
14531+ err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14532+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14533+ if (unlikely(err))
14534+ goto out_dgrade;
14535+
14536+ dbtop = au_dbtop(cpg.dentry);
14537+ if (dbtop <= cpg.bdst)
14538+ cpg.bsrc = cpg.bdst;
14539+
14540+ if (dbtop <= cpg.bdst /* just reopen */
14541+ || !d_unhashed(cpg.dentry) /* copyup and reopen */
14542+ ) {
14543+ h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
14544+ if (IS_ERR(h_file))
14545+ err = PTR_ERR(h_file);
14546+ else {
14547+ di_downgrade_lock(parent, AuLock_IR);
14548+ if (dbtop > cpg.bdst)
14549+ err = au_sio_cpup_simple(&cpg);
14550+ if (!err)
14551+ err = au_reopen_nondir(file);
14552+ au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
14553+ }
14554+ } else { /* copyup as wh and reopen */
14555+ /*
14556+ * since writable hfsplus branch is not supported,
14557+ * h_open_pre/post() are unnecessary.
14558+ */
14559+ err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
14560+ di_downgrade_lock(parent, AuLock_IR);
14561+ }
14562+
14563+ if (!err) {
14564+ au_pin_set_parent_lflag(pin, /*lflag*/0);
14565+ goto out_dput; /* success */
14566+ }
14567+ au_unpin(pin);
14568+ goto out_unlock;
14569+
14570+out_dgrade:
14571+ di_downgrade_lock(parent, AuLock_IR);
14572+out_unlock:
14573+ di_read_unlock(parent, AuLock_IR);
14574+out_dput:
14575+ dput(parent);
14576+out:
14577+ return err;
14578+}
14579+
14580+/* ---------------------------------------------------------------------- */
14581+
14582+int au_do_flush(struct file *file, fl_owner_t id,
14583+ int (*flush)(struct file *file, fl_owner_t id))
14584+{
14585+ int err;
14586+ struct super_block *sb;
14587+ struct inode *inode;
14588+
14589+ inode = file_inode(file);
14590+ sb = inode->i_sb;
14591+ si_noflush_read_lock(sb);
14592+ fi_read_lock(file);
14593+ ii_read_lock_child(inode);
14594+
14595+ err = flush(file, id);
14596+ au_cpup_attr_timesizes(inode);
14597+
14598+ ii_read_unlock(inode);
14599+ fi_read_unlock(file);
14600+ si_read_unlock(sb);
14601+ return err;
14602+}
14603+
14604+/* ---------------------------------------------------------------------- */
14605+
14606+static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
14607+{
14608+ int err;
14609+ struct au_pin pin;
14610+ struct au_finfo *finfo;
14611+ struct dentry *parent, *hi_wh;
14612+ struct inode *inode;
14613+ struct super_block *sb;
14614+ struct au_cp_generic cpg = {
14615+ .dentry = file->f_path.dentry,
14616+ .bdst = -1,
14617+ .bsrc = -1,
14618+ .len = -1,
14619+ .pin = &pin,
14620+ .flags = AuCpup_DTIME
14621+ };
14622+
14623+ FiMustWriteLock(file);
14624+
14625+ err = 0;
14626+ finfo = au_fi(file);
14627+ sb = cpg.dentry->d_sb;
14628+ inode = d_inode(cpg.dentry);
14629+ cpg.bdst = au_ibtop(inode);
14630+ if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
14631+ goto out;
14632+
14633+ parent = dget_parent(cpg.dentry);
14634+ if (au_test_ro(sb, cpg.bdst, inode)) {
14635+ di_read_lock_parent(parent, !AuLock_IR);
14636+ err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14637+ cpg.bdst = err;
14638+ di_read_unlock(parent, !AuLock_IR);
14639+ if (unlikely(err < 0))
14640+ goto out_parent;
14641+ err = 0;
14642+ }
14643+
14644+ di_read_lock_parent(parent, AuLock_IR);
14645+ hi_wh = au_hi_wh(inode, cpg.bdst);
14646+ if (!S_ISDIR(inode->i_mode)
14647+ && au_opt_test(au_mntflags(sb), PLINK)
14648+ && au_plink_test(inode)
14649+ && !d_unhashed(cpg.dentry)
14650+ && cpg.bdst < au_dbtop(cpg.dentry)) {
14651+ err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
14652+ if (unlikely(err))
14653+ goto out_unlock;
14654+
14655+ /* always superio. */
14656+ err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14657+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14658+ if (!err) {
14659+ err = au_sio_cpup_simple(&cpg);
14660+ au_unpin(&pin);
14661+ }
14662+ } else if (hi_wh) {
14663+ /* already copied-up after unlink */
14664+ err = au_reopen_wh(file, cpg.bdst, hi_wh);
14665+ *need_reopen = 0;
14666+ }
14667+
14668+out_unlock:
14669+ di_read_unlock(parent, AuLock_IR);
14670+out_parent:
14671+ dput(parent);
14672+out:
14673+ return err;
14674+}
14675+
14676+static void au_do_refresh_dir(struct file *file)
14677+{
14678+ aufs_bindex_t bindex, bbot, new_bindex, brid;
14679+ struct au_hfile *p, tmp, *q;
14680+ struct au_finfo *finfo;
14681+ struct super_block *sb;
14682+ struct au_fidir *fidir;
14683+
14684+ FiMustWriteLock(file);
14685+
14686+ sb = file->f_path.dentry->d_sb;
14687+ finfo = au_fi(file);
14688+ fidir = finfo->fi_hdir;
14689+ AuDebugOn(!fidir);
14690+ p = fidir->fd_hfile + finfo->fi_btop;
14691+ brid = p->hf_br->br_id;
14692+ bbot = fidir->fd_bbot;
14693+ for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
14694+ if (!p->hf_file)
14695+ continue;
14696+
14697+ new_bindex = au_br_index(sb, p->hf_br->br_id);
14698+ if (new_bindex == bindex)
14699+ continue;
14700+ if (new_bindex < 0) {
14701+ au_set_h_fptr(file, bindex, NULL);
14702+ continue;
14703+ }
14704+
14705+ /* swap two lower inode, and loop again */
14706+ q = fidir->fd_hfile + new_bindex;
14707+ tmp = *q;
14708+ *q = *p;
14709+ *p = tmp;
14710+ if (tmp.hf_file) {
14711+ bindex--;
14712+ p--;
14713+ }
14714+ }
14715+
14716+ p = fidir->fd_hfile;
14717+ if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
14718+ bbot = au_sbbot(sb);
14719+ for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
14720+ finfo->fi_btop++, p++)
14721+ if (p->hf_file) {
14722+ if (file_inode(p->hf_file))
14723+ break;
14724+ au_hfput(p, /*execed*/0);
14725+ }
14726+ } else {
14727+ bbot = au_br_index(sb, brid);
14728+ for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
14729+ finfo->fi_btop++, p++)
14730+ if (p->hf_file)
14731+ au_hfput(p, /*execed*/0);
14732+ bbot = au_sbbot(sb);
14733+ }
14734+
14735+ p = fidir->fd_hfile + bbot;
14736+ for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
14737+ fidir->fd_bbot--, p--)
14738+ if (p->hf_file) {
14739+ if (file_inode(p->hf_file))
14740+ break;
14741+ au_hfput(p, /*execed*/0);
14742+ }
14743+ AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
14744+}
14745+
14746+/*
14747+ * after branch manipulating, refresh the file.
14748+ */
14749+static int refresh_file(struct file *file, int (*reopen)(struct file *file))
14750+{
14751+ int err, need_reopen, nbr;
14752+ aufs_bindex_t bbot, bindex;
14753+ struct dentry *dentry;
14754+ struct super_block *sb;
14755+ struct au_finfo *finfo;
14756+ struct au_hfile *hfile;
14757+
14758+ dentry = file->f_path.dentry;
14759+ sb = dentry->d_sb;
14760+ nbr = au_sbbot(sb) + 1;
14761+ finfo = au_fi(file);
14762+ if (!finfo->fi_hdir) {
14763+ hfile = &finfo->fi_htop;
14764+ AuDebugOn(!hfile->hf_file);
14765+ bindex = au_br_index(sb, hfile->hf_br->br_id);
14766+ AuDebugOn(bindex < 0);
14767+ if (bindex != finfo->fi_btop)
14768+ au_set_fbtop(file, bindex);
14769+ } else {
14770+ err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
14771+ if (unlikely(err))
14772+ goto out;
14773+ au_do_refresh_dir(file);
14774+ }
14775+
14776+ err = 0;
14777+ need_reopen = 1;
14778+ if (!au_test_mmapped(file))
14779+ err = au_file_refresh_by_inode(file, &need_reopen);
14780+ if (finfo->fi_hdir)
14781+ /* harmless if err */
14782+ au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
14783+ if (!err && need_reopen && !d_unlinked(dentry))
14784+ err = reopen(file);
14785+ if (!err) {
14786+ au_update_figen(file);
14787+ goto out; /* success */
14788+ }
14789+
14790+ /* error, close all lower files */
14791+ if (finfo->fi_hdir) {
14792+ bbot = au_fbbot_dir(file);
14793+ for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
14794+ au_set_h_fptr(file, bindex, NULL);
14795+ }
14796+
14797+out:
14798+ return err;
14799+}
14800+
14801+/* common function to regular file and dir */
14802+int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
14803+ int wlock, unsigned int fi_lsc)
14804+{
14805+ int err;
14806+ unsigned int sigen, figen;
14807+ aufs_bindex_t btop;
14808+ unsigned char pseudo_link;
14809+ struct dentry *dentry;
14810+ struct inode *inode;
14811+
14812+ err = 0;
14813+ dentry = file->f_path.dentry;
14814+ inode = d_inode(dentry);
14815+ sigen = au_sigen(dentry->d_sb);
14816+ fi_write_lock_nested(file, fi_lsc);
14817+ figen = au_figen(file);
14818+ if (!fi_lsc)
14819+ di_write_lock_child(dentry);
14820+ else
14821+ di_write_lock_child2(dentry);
14822+ btop = au_dbtop(dentry);
14823+ pseudo_link = (btop != au_ibtop(inode));
14824+ if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
14825+ if (!wlock) {
14826+ di_downgrade_lock(dentry, AuLock_IR);
14827+ fi_downgrade_lock(file);
14828+ }
14829+ goto out; /* success */
14830+ }
14831+
14832+ AuDbg("sigen %d, figen %d\n", sigen, figen);
14833+ if (au_digen_test(dentry, sigen)) {
14834+ err = au_reval_dpath(dentry, sigen);
14835+ AuDebugOn(!err && au_digen_test(dentry, sigen));
14836+ }
14837+
14838+ if (!err)
14839+ err = refresh_file(file, reopen);
14840+ if (!err) {
14841+ if (!wlock) {
14842+ di_downgrade_lock(dentry, AuLock_IR);
14843+ fi_downgrade_lock(file);
14844+ }
14845+ } else {
14846+ di_write_unlock(dentry);
14847+ fi_write_unlock(file);
14848+ }
14849+
14850+out:
14851+ return err;
14852+}
14853+
14854+/* ---------------------------------------------------------------------- */
14855+
14856+/* cf. aufs_nopage() */
14857+/* for madvise(2) */
14858+static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
14859+{
14860+ unlock_page(page);
14861+ return 0;
14862+}
14863+
14864+/* it will never be called, but necessary to support O_DIRECT */
14865+static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
14866+{ BUG(); return 0; }
14867+
14868+/* they will never be called. */
14869+#ifdef CONFIG_AUFS_DEBUG
14870+static int aufs_write_begin(struct file *file, struct address_space *mapping,
14871+ loff_t pos, unsigned len, unsigned flags,
14872+ struct page **pagep, void **fsdata)
14873+{ AuUnsupport(); return 0; }
14874+static int aufs_write_end(struct file *file, struct address_space *mapping,
14875+ loff_t pos, unsigned len, unsigned copied,
14876+ struct page *page, void *fsdata)
14877+{ AuUnsupport(); return 0; }
14878+static int aufs_writepage(struct page *page, struct writeback_control *wbc)
14879+{ AuUnsupport(); return 0; }
14880+
14881+static int aufs_set_page_dirty(struct page *page)
14882+{ AuUnsupport(); return 0; }
14883+static void aufs_invalidatepage(struct page *page, unsigned int offset,
14884+ unsigned int length)
14885+{ AuUnsupport(); }
14886+static int aufs_releasepage(struct page *page, gfp_t gfp)
14887+{ AuUnsupport(); return 0; }
14888+#if 0 /* called by memory compaction regardless file */
14889+static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
14890+ struct page *page, enum migrate_mode mode)
14891+{ AuUnsupport(); return 0; }
14892+#endif
14893+static bool aufs_isolate_page(struct page *page, isolate_mode_t mode)
14894+{ AuUnsupport(); return true; }
14895+static void aufs_putback_page(struct page *page)
14896+{ AuUnsupport(); }
14897+static int aufs_launder_page(struct page *page)
14898+{ AuUnsupport(); return 0; }
14899+static int aufs_is_partially_uptodate(struct page *page,
14900+ unsigned long from,
14901+ unsigned long count)
14902+{ AuUnsupport(); return 0; }
14903+static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
14904+ bool *writeback)
14905+{ AuUnsupport(); }
14906+static int aufs_error_remove_page(struct address_space *mapping,
14907+ struct page *page)
14908+{ AuUnsupport(); return 0; }
14909+static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
14910+ sector_t *span)
14911+{ AuUnsupport(); return 0; }
14912+static void aufs_swap_deactivate(struct file *file)
14913+{ AuUnsupport(); }
14914+#endif /* CONFIG_AUFS_DEBUG */
14915+
14916+const struct address_space_operations aufs_aop = {
14917+ .readpage = aufs_readpage,
14918+ .direct_IO = aufs_direct_IO,
14919+#ifdef CONFIG_AUFS_DEBUG
14920+ .writepage = aufs_writepage,
14921+ /* no writepages, because of writepage */
14922+ .set_page_dirty = aufs_set_page_dirty,
14923+ /* no readpages, because of readpage */
14924+ .write_begin = aufs_write_begin,
14925+ .write_end = aufs_write_end,
14926+ /* no bmap, no block device */
14927+ .invalidatepage = aufs_invalidatepage,
14928+ .releasepage = aufs_releasepage,
14929+ /* is fallback_migrate_page ok? */
14930+ /* .migratepage = aufs_migratepage, */
14931+ .isolate_page = aufs_isolate_page,
14932+ .putback_page = aufs_putback_page,
14933+ .launder_page = aufs_launder_page,
14934+ .is_partially_uptodate = aufs_is_partially_uptodate,
14935+ .is_dirty_writeback = aufs_is_dirty_writeback,
14936+ .error_remove_page = aufs_error_remove_page,
14937+ .swap_activate = aufs_swap_activate,
14938+ .swap_deactivate = aufs_swap_deactivate
14939+#endif /* CONFIG_AUFS_DEBUG */
14940+};
14941diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
14942--- /usr/share/empty/fs/aufs/file.h 1970-01-01 01:00:00.000000000 +0100
14943+++ linux/fs/aufs/file.h 2021-02-24 13:33:42.744347058 +0100
14944@@ -0,0 +1,342 @@
14945+/* SPDX-License-Identifier: GPL-2.0 */
14946+/*
14947+ * Copyright (C) 2005-2020 Junjiro R. Okajima
14948+ *
14949+ * This program, aufs is free software; you can redistribute it and/or modify
14950+ * it under the terms of the GNU General Public License as published by
14951+ * the Free Software Foundation; either version 2 of the License, or
14952+ * (at your option) any later version.
14953+ *
14954+ * This program is distributed in the hope that it will be useful,
14955+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14956+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14957+ * GNU General Public License for more details.
14958+ *
14959+ * You should have received a copy of the GNU General Public License
14960+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
14961+ */
14962+
14963+/*
14964+ * file operations
14965+ */
14966+
14967+#ifndef __AUFS_FILE_H__
14968+#define __AUFS_FILE_H__
14969+
14970+#ifdef __KERNEL__
14971+
14972+#include <linux/file.h>
14973+#include <linux/fs.h>
14974+#include <linux/mm_types.h>
14975+#include <linux/poll.h>
14976+#include "rwsem.h"
14977+
14978+struct au_branch;
14979+struct au_hfile {
14980+ struct file *hf_file;
14981+ struct au_branch *hf_br;
14982+};
14983+
14984+struct au_vdir;
14985+struct au_fidir {
14986+ aufs_bindex_t fd_bbot;
14987+ aufs_bindex_t fd_nent;
14988+ struct au_vdir *fd_vdir_cache;
14989+ struct au_hfile fd_hfile[];
14990+};
14991+
14992+static inline int au_fidir_sz(int nent)
14993+{
14994+ AuDebugOn(nent < 0);
14995+ return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
14996+}
14997+
14998+struct au_finfo {
14999+ atomic_t fi_generation;
15000+
15001+ struct au_rwsem fi_rwsem;
15002+ aufs_bindex_t fi_btop;
15003+
15004+ /* do not union them */
15005+ struct { /* for non-dir */
15006+ struct au_hfile fi_htop;
15007+ atomic_t fi_mmapped;
15008+ };
15009+ struct au_fidir *fi_hdir; /* for dir only */
15010+
15011+ struct hlist_bl_node fi_hlist;
15012+ struct file *fi_file; /* very ugly */
15013+ struct rcu_head rcu;
15014+} ____cacheline_aligned_in_smp;
15015+
15016+/* ---------------------------------------------------------------------- */
15017+
15018+/* file.c */
15019+extern const struct address_space_operations aufs_aop;
15020+unsigned int au_file_roflags(unsigned int flags);
15021+struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
15022+ struct file *file, int force_wr);
15023+struct au_do_open_args {
15024+ int aopen;
15025+ int (*open)(struct file *file, int flags,
15026+ struct file *h_file);
15027+ struct au_fidir *fidir;
15028+ struct file *h_file;
15029+};
15030+int au_do_open(struct file *file, struct au_do_open_args *args);
15031+int au_reopen_nondir(struct file *file);
15032+struct au_pin;
15033+int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
15034+int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
15035+ int wlock, unsigned int fi_lsc);
15036+int au_do_flush(struct file *file, fl_owner_t id,
15037+ int (*flush)(struct file *file, fl_owner_t id));
15038+
15039+/* poll.c */
15040+#ifdef CONFIG_AUFS_POLL
15041+__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt);
15042+#endif
15043+
15044+#ifdef CONFIG_AUFS_BR_HFSPLUS
15045+/* hfsplus.c */
15046+struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
15047+ int force_wr);
15048+void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
15049+ struct file *h_file);
15050+#else
15051+AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
15052+ aufs_bindex_t bindex, int force_wr)
15053+AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
15054+ struct file *h_file);
15055+#endif
15056+
15057+/* f_op.c */
15058+extern const struct file_operations aufs_file_fop;
15059+int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
15060+int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
15061+struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
15062+
15063+/* finfo.c */
15064+void au_hfput(struct au_hfile *hf, int execed);
15065+void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
15066+ struct file *h_file);
15067+
15068+void au_update_figen(struct file *file);
15069+struct au_fidir *au_fidir_alloc(struct super_block *sb);
15070+int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
15071+
15072+void au_fi_init_once(void *_fi);
15073+void au_finfo_fin(struct file *file);
15074+int au_finfo_init(struct file *file, struct au_fidir *fidir);
15075+
15076+/* ioctl.c */
15077+long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
15078+#ifdef CONFIG_COMPAT
15079+long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
15080+ unsigned long arg);
15081+long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
15082+ unsigned long arg);
15083+#endif
15084+
15085+/* ---------------------------------------------------------------------- */
15086+
15087+static inline struct au_finfo *au_fi(struct file *file)
15088+{
15089+ return file->private_data;
15090+}
15091+
15092+/* ---------------------------------------------------------------------- */
15093+
15094+#define fi_read_lock(f) au_rw_read_lock(&au_fi(f)->fi_rwsem)
15095+#define fi_write_lock(f) au_rw_write_lock(&au_fi(f)->fi_rwsem)
15096+#define fi_read_trylock(f) au_rw_read_trylock(&au_fi(f)->fi_rwsem)
15097+#define fi_write_trylock(f) au_rw_write_trylock(&au_fi(f)->fi_rwsem)
15098+/*
15099+#define fi_read_trylock_nested(f) \
15100+ au_rw_read_trylock_nested(&au_fi(f)->fi_rwsem)
15101+#define fi_write_trylock_nested(f) \
15102+ au_rw_write_trylock_nested(&au_fi(f)->fi_rwsem)
15103+*/
15104+
15105+#define fi_read_unlock(f) au_rw_read_unlock(&au_fi(f)->fi_rwsem)
15106+#define fi_write_unlock(f) au_rw_write_unlock(&au_fi(f)->fi_rwsem)
15107+#define fi_downgrade_lock(f) au_rw_dgrade_lock(&au_fi(f)->fi_rwsem)
15108+
15109+/* lock subclass for finfo */
15110+enum {
15111+ AuLsc_FI_1,
15112+ AuLsc_FI_2
15113+};
15114+
15115+static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
15116+{
15117+ au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15118+}
15119+
15120+static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
15121+{
15122+ au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15123+}
15124+
15125+/*
15126+ * fi_read_lock_1, fi_write_lock_1,
15127+ * fi_read_lock_2, fi_write_lock_2
15128+ */
15129+#define AuReadLockFunc(name) \
15130+static inline void fi_read_lock_##name(struct file *f) \
15131+{ fi_read_lock_nested(f, AuLsc_FI_##name); }
15132+
15133+#define AuWriteLockFunc(name) \
15134+static inline void fi_write_lock_##name(struct file *f) \
15135+{ fi_write_lock_nested(f, AuLsc_FI_##name); }
15136+
15137+#define AuRWLockFuncs(name) \
15138+ AuReadLockFunc(name) \
15139+ AuWriteLockFunc(name)
15140+
15141+AuRWLockFuncs(1);
15142+AuRWLockFuncs(2);
15143+
15144+#undef AuReadLockFunc
15145+#undef AuWriteLockFunc
15146+#undef AuRWLockFuncs
15147+
15148+#define FiMustNoWaiters(f) AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
15149+#define FiMustAnyLock(f) AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
15150+#define FiMustWriteLock(f) AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
15151+
15152+/* ---------------------------------------------------------------------- */
15153+
15154+/* todo: hard/soft set? */
15155+static inline aufs_bindex_t au_fbtop(struct file *file)
15156+{
15157+ FiMustAnyLock(file);
15158+ return au_fi(file)->fi_btop;
15159+}
15160+
15161+static inline aufs_bindex_t au_fbbot_dir(struct file *file)
15162+{
15163+ FiMustAnyLock(file);
15164+ AuDebugOn(!au_fi(file)->fi_hdir);
15165+ return au_fi(file)->fi_hdir->fd_bbot;
15166+}
15167+
15168+static inline struct au_vdir *au_fvdir_cache(struct file *file)
15169+{
15170+ FiMustAnyLock(file);
15171+ AuDebugOn(!au_fi(file)->fi_hdir);
15172+ return au_fi(file)->fi_hdir->fd_vdir_cache;
15173+}
15174+
15175+static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
15176+{
15177+ FiMustWriteLock(file);
15178+ au_fi(file)->fi_btop = bindex;
15179+}
15180+
15181+static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
15182+{
15183+ FiMustWriteLock(file);
15184+ AuDebugOn(!au_fi(file)->fi_hdir);
15185+ au_fi(file)->fi_hdir->fd_bbot = bindex;
15186+}
15187+
15188+static inline void au_set_fvdir_cache(struct file *file,
15189+ struct au_vdir *vdir_cache)
15190+{
15191+ FiMustWriteLock(file);
15192+ AuDebugOn(!au_fi(file)->fi_hdir);
15193+ au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
15194+}
15195+
15196+static inline struct file *au_hf_top(struct file *file)
15197+{
15198+ FiMustAnyLock(file);
15199+ AuDebugOn(au_fi(file)->fi_hdir);
15200+ return au_fi(file)->fi_htop.hf_file;
15201+}
15202+
15203+static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
15204+{
15205+ FiMustAnyLock(file);
15206+ AuDebugOn(!au_fi(file)->fi_hdir);
15207+ return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
15208+}
15209+
15210+/* todo: memory barrier? */
15211+static inline unsigned int au_figen(struct file *f)
15212+{
15213+ return atomic_read(&au_fi(f)->fi_generation);
15214+}
15215+
15216+static inline void au_set_mmapped(struct file *f)
15217+{
15218+ if (atomic_inc_return(&au_fi(f)->fi_mmapped))
15219+ return;
15220+ pr_warn("fi_mmapped wrapped around\n");
15221+ while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
15222+ ;
15223+}
15224+
15225+static inline void au_unset_mmapped(struct file *f)
15226+{
15227+ atomic_dec(&au_fi(f)->fi_mmapped);
15228+}
15229+
15230+static inline int au_test_mmapped(struct file *f)
15231+{
15232+ return atomic_read(&au_fi(f)->fi_mmapped);
15233+}
15234+
15235+/* customize vma->vm_file */
15236+
15237+static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
15238+ struct file *file)
15239+{
15240+ struct file *f;
15241+
15242+ f = vma->vm_file;
15243+ get_file(file);
15244+ vma->vm_file = file;
15245+ fput(f);
15246+}
15247+
15248+#ifdef CONFIG_MMU
15249+#define AuDbgVmRegion(file, vma) do {} while (0)
15250+
15251+static inline void au_vm_file_reset(struct vm_area_struct *vma,
15252+ struct file *file)
15253+{
15254+ au_do_vm_file_reset(vma, file);
15255+}
15256+#else
15257+#define AuDbgVmRegion(file, vma) \
15258+ AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
15259+
15260+static inline void au_vm_file_reset(struct vm_area_struct *vma,
15261+ struct file *file)
15262+{
15263+ struct file *f;
15264+
15265+ au_do_vm_file_reset(vma, file);
15266+ f = vma->vm_region->vm_file;
15267+ get_file(file);
15268+ vma->vm_region->vm_file = file;
15269+ fput(f);
15270+}
15271+#endif /* CONFIG_MMU */
15272+
15273+/* handle vma->vm_prfile */
15274+static inline void au_vm_prfile_set(struct vm_area_struct *vma,
15275+ struct file *file)
15276+{
15277+ get_file(file);
15278+ vma->vm_prfile = file;
15279+#ifndef CONFIG_MMU
15280+ get_file(file);
15281+ vma->vm_region->vm_prfile = file;
15282+#endif
15283+}
15284+
15285+#endif /* __KERNEL__ */
15286+#endif /* __AUFS_FILE_H__ */
15287diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
15288--- /usr/share/empty/fs/aufs/finfo.c 1970-01-01 01:00:00.000000000 +0100
15289+++ linux/fs/aufs/finfo.c 2021-02-24 13:33:42.744347058 +0100
15290@@ -0,0 +1,149 @@
15291+// SPDX-License-Identifier: GPL-2.0
15292+/*
15293+ * Copyright (C) 2005-2020 Junjiro R. Okajima
15294+ *
15295+ * This program, aufs is free software; you can redistribute it and/or modify
15296+ * it under the terms of the GNU General Public License as published by
15297+ * the Free Software Foundation; either version 2 of the License, or
15298+ * (at your option) any later version.
15299+ *
15300+ * This program is distributed in the hope that it will be useful,
15301+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15302+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15303+ * GNU General Public License for more details.
15304+ *
15305+ * You should have received a copy of the GNU General Public License
15306+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15307+ */
15308+
15309+/*
15310+ * file private data
15311+ */
15312+
15313+#include "aufs.h"
15314+
15315+void au_hfput(struct au_hfile *hf, int execed)
15316+{
15317+ if (execed)
15318+ allow_write_access(hf->hf_file);
15319+ fput(hf->hf_file);
15320+ hf->hf_file = NULL;
15321+ au_lcnt_dec(&hf->hf_br->br_nfiles);
15322+ hf->hf_br = NULL;
15323+}
15324+
15325+void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
15326+{
15327+ struct au_finfo *finfo = au_fi(file);
15328+ struct au_hfile *hf;
15329+ struct au_fidir *fidir;
15330+
15331+ fidir = finfo->fi_hdir;
15332+ if (!fidir) {
15333+ AuDebugOn(finfo->fi_btop != bindex);
15334+ hf = &finfo->fi_htop;
15335+ } else
15336+ hf = fidir->fd_hfile + bindex;
15337+
15338+ if (hf && hf->hf_file)
15339+ au_hfput(hf, vfsub_file_execed(file));
15340+ if (val) {
15341+ FiMustWriteLock(file);
15342+ AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
15343+ hf->hf_file = val;
15344+ hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
15345+ }
15346+}
15347+
15348+void au_update_figen(struct file *file)
15349+{
15350+ atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
15351+ /* smp_mb(); */ /* atomic_set */
15352+}
15353+
15354+/* ---------------------------------------------------------------------- */
15355+
15356+struct au_fidir *au_fidir_alloc(struct super_block *sb)
15357+{
15358+ struct au_fidir *fidir;
15359+ int nbr;
15360+
15361+ nbr = au_sbbot(sb) + 1;
15362+ if (nbr < 2)
15363+ nbr = 2; /* initial allocate for 2 branches */
15364+ fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
15365+ if (fidir) {
15366+ fidir->fd_bbot = -1;
15367+ fidir->fd_nent = nbr;
15368+ }
15369+
15370+ return fidir;
15371+}
15372+
15373+int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
15374+{
15375+ int err;
15376+ struct au_fidir *fidir, *p;
15377+
15378+ AuRwMustWriteLock(&finfo->fi_rwsem);
15379+ fidir = finfo->fi_hdir;
15380+ AuDebugOn(!fidir);
15381+
15382+ err = -ENOMEM;
15383+ p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
15384+ GFP_NOFS, may_shrink);
15385+ if (p) {
15386+ p->fd_nent = nbr;
15387+ finfo->fi_hdir = p;
15388+ err = 0;
15389+ }
15390+
15391+ return err;
15392+}
15393+
15394+/* ---------------------------------------------------------------------- */
15395+
15396+void au_finfo_fin(struct file *file)
15397+{
15398+ struct au_finfo *finfo;
15399+
15400+ au_lcnt_dec(&au_sbi(file->f_path.dentry->d_sb)->si_nfiles);
15401+
15402+ finfo = au_fi(file);
15403+ AuDebugOn(finfo->fi_hdir);
15404+ AuRwDestroy(&finfo->fi_rwsem);
15405+ au_cache_free_finfo(finfo);
15406+}
15407+
15408+void au_fi_init_once(void *_finfo)
15409+{
15410+ struct au_finfo *finfo = _finfo;
15411+
15412+ au_rw_init(&finfo->fi_rwsem);
15413+}
15414+
15415+int au_finfo_init(struct file *file, struct au_fidir *fidir)
15416+{
15417+ int err;
15418+ struct au_finfo *finfo;
15419+ struct dentry *dentry;
15420+
15421+ err = -ENOMEM;
15422+ dentry = file->f_path.dentry;
15423+ finfo = au_cache_alloc_finfo();
15424+ if (unlikely(!finfo))
15425+ goto out;
15426+
15427+ err = 0;
15428+ au_lcnt_inc(&au_sbi(dentry->d_sb)->si_nfiles);
15429+ au_rw_write_lock(&finfo->fi_rwsem);
15430+ finfo->fi_btop = -1;
15431+ finfo->fi_hdir = fidir;
15432+ atomic_set(&finfo->fi_generation, au_digen(dentry));
15433+ /* smp_mb(); */ /* atomic_set */
15434+
15435+ file->private_data = finfo;
15436+
15437+out:
15438+ return err;
15439+}
15440diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
15441--- /usr/share/empty/fs/aufs/f_op.c 1970-01-01 01:00:00.000000000 +0100
15442+++ linux/fs/aufs/f_op.c 2021-06-30 21:35:11.393873211 +0200
15443@@ -0,0 +1,771 @@
15444+// SPDX-License-Identifier: GPL-2.0
15445+/*
15446+ * Copyright (C) 2005-2020 Junjiro R. Okajima
15447+ *
15448+ * This program, aufs is free software; you can redistribute it and/or modify
15449+ * it under the terms of the GNU General Public License as published by
15450+ * the Free Software Foundation; either version 2 of the License, or
15451+ * (at your option) any later version.
15452+ *
15453+ * This program is distributed in the hope that it will be useful,
15454+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15455+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15456+ * GNU General Public License for more details.
15457+ *
15458+ * You should have received a copy of the GNU General Public License
15459+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15460+ */
15461+
15462+/*
15463+ * file and vm operations
15464+ */
15465+
15466+#include <linux/aio.h>
15467+#include <linux/fs_stack.h>
15468+#include <linux/mman.h>
15469+#include <linux/security.h>
15470+#include "aufs.h"
15471+
15472+int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
15473+{
15474+ int err;
15475+ aufs_bindex_t bindex;
15476+ struct dentry *dentry, *h_dentry;
15477+ struct au_finfo *finfo;
15478+ struct inode *h_inode;
15479+
15480+ FiMustWriteLock(file);
15481+
15482+ err = 0;
15483+ dentry = file->f_path.dentry;
15484+ AuDebugOn(IS_ERR_OR_NULL(dentry));
15485+ finfo = au_fi(file);
15486+ memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
15487+ atomic_set(&finfo->fi_mmapped, 0);
15488+ bindex = au_dbtop(dentry);
15489+ if (!h_file) {
15490+ h_dentry = au_h_dptr(dentry, bindex);
15491+ err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15492+ if (unlikely(err))
15493+ goto out;
15494+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
15495+ if (IS_ERR(h_file)) {
15496+ err = PTR_ERR(h_file);
15497+ goto out;
15498+ }
15499+ } else {
15500+ h_dentry = h_file->f_path.dentry;
15501+ err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15502+ if (unlikely(err))
15503+ goto out;
15504+ /* br ref is already inc-ed */
15505+ }
15506+
15507+ if ((flags & __O_TMPFILE)
15508+ && !(flags & O_EXCL)) {
15509+ h_inode = file_inode(h_file);
15510+ spin_lock(&h_inode->i_lock);
15511+ h_inode->i_state |= I_LINKABLE;
15512+ spin_unlock(&h_inode->i_lock);
15513+ }
15514+ au_set_fbtop(file, bindex);
15515+ au_set_h_fptr(file, bindex, h_file);
15516+ au_update_figen(file);
15517+ /* todo: necessary? */
15518+ /* file->f_ra = h_file->f_ra; */
15519+
15520+out:
15521+ return err;
15522+}
15523+
15524+static int aufs_open_nondir(struct inode *inode __maybe_unused,
15525+ struct file *file)
15526+{
15527+ int err;
15528+ struct super_block *sb;
15529+ struct au_do_open_args args = {
15530+ .open = au_do_open_nondir
15531+ };
15532+
15533+ AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
15534+ file, vfsub_file_flags(file), file->f_mode);
15535+
15536+ sb = file->f_path.dentry->d_sb;
15537+ si_read_lock(sb, AuLock_FLUSH);
15538+ err = au_do_open(file, &args);
15539+ si_read_unlock(sb);
15540+ return err;
15541+}
15542+
15543+int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
15544+{
15545+ struct au_finfo *finfo;
15546+ aufs_bindex_t bindex;
15547+
15548+ finfo = au_fi(file);
15549+ au_hbl_del(&finfo->fi_hlist,
15550+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
15551+ bindex = finfo->fi_btop;
15552+ if (bindex >= 0)
15553+ au_set_h_fptr(file, bindex, NULL);
15554+
15555+ au_finfo_fin(file);
15556+ return 0;
15557+}
15558+
15559+/* ---------------------------------------------------------------------- */
15560+
15561+static int au_do_flush_nondir(struct file *file, fl_owner_t id)
15562+{
15563+ int err;
15564+ struct file *h_file;
15565+
15566+ err = 0;
15567+ h_file = au_hf_top(file);
15568+ if (h_file)
15569+ err = vfsub_flush(h_file, id);
15570+ return err;
15571+}
15572+
15573+static int aufs_flush_nondir(struct file *file, fl_owner_t id)
15574+{
15575+ return au_do_flush(file, id, au_do_flush_nondir);
15576+}
15577+
15578+/* ---------------------------------------------------------------------- */
15579+/*
15580+ * read and write functions acquire [fdi]_rwsem once, but release before
15581+ * mmap_sem. This is because to stop a race condition between mmap(2).
15582+ * Releasing these aufs-rwsem should be safe, no branch-management (by keeping
15583+ * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
15584+ * read functions after [fdi]_rwsem are released, but it should be harmless.
15585+ */
15586+
15587+/* Callers should call au_read_post() or fput() in the end */
15588+struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
15589+{
15590+ struct file *h_file;
15591+ int err;
15592+
15593+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
15594+ if (!err) {
15595+ di_read_unlock(file->f_path.dentry, AuLock_IR);
15596+ h_file = au_hf_top(file);
15597+ get_file(h_file);
15598+ if (!keep_fi)
15599+ fi_read_unlock(file);
15600+ } else
15601+ h_file = ERR_PTR(err);
15602+
15603+ return h_file;
15604+}
15605+
15606+static void au_read_post(struct inode *inode, struct file *h_file)
15607+{
15608+ /* update without lock, I don't think it a problem */
15609+ fsstack_copy_attr_atime(inode, file_inode(h_file));
15610+ fput(h_file);
15611+}
15612+
15613+struct au_write_pre {
15614+ /* input */
15615+ unsigned int lsc;
15616+
15617+ /* output */
15618+ blkcnt_t blks;
15619+ aufs_bindex_t btop;
15620+};
15621+
15622+/*
15623+ * return with iinfo is write-locked
15624+ * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
15625+ * end
15626+ */
15627+static struct file *au_write_pre(struct file *file, int do_ready,
15628+ struct au_write_pre *wpre)
15629+{
15630+ struct file *h_file;
15631+ struct dentry *dentry;
15632+ int err;
15633+ unsigned int lsc;
15634+ struct au_pin pin;
15635+
15636+ lsc = 0;
15637+ if (wpre)
15638+ lsc = wpre->lsc;
15639+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
15640+ h_file = ERR_PTR(err);
15641+ if (unlikely(err))
15642+ goto out;
15643+
15644+ dentry = file->f_path.dentry;
15645+ if (do_ready) {
15646+ err = au_ready_to_write(file, -1, &pin);
15647+ if (unlikely(err)) {
15648+ h_file = ERR_PTR(err);
15649+ di_write_unlock(dentry);
15650+ goto out_fi;
15651+ }
15652+ }
15653+
15654+ di_downgrade_lock(dentry, /*flags*/0);
15655+ if (wpre)
15656+ wpre->btop = au_fbtop(file);
15657+ h_file = au_hf_top(file);
15658+ get_file(h_file);
15659+ if (wpre)
15660+ wpre->blks = file_inode(h_file)->i_blocks;
15661+ if (do_ready)
15662+ au_unpin(&pin);
15663+ di_read_unlock(dentry, /*flags*/0);
15664+
15665+out_fi:
15666+ fi_write_unlock(file);
15667+out:
15668+ return h_file;
15669+}
15670+
15671+static void au_write_post(struct inode *inode, struct file *h_file,
15672+ struct au_write_pre *wpre, ssize_t written)
15673+{
15674+ struct inode *h_inode;
15675+
15676+ au_cpup_attr_timesizes(inode);
15677+ AuDebugOn(au_ibtop(inode) != wpre->btop);
15678+ h_inode = file_inode(h_file);
15679+ inode->i_mode = h_inode->i_mode;
15680+ ii_write_unlock(inode);
15681+ /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
15682+ if (written > 0)
15683+ au_fhsm_wrote(inode->i_sb, wpre->btop,
15684+ /*force*/h_inode->i_blocks > wpre->blks);
15685+ fput(h_file);
15686+}
15687+
15688+/*
15689+ * todo: very ugly
15690+ * it locks both of i_mutex and si_rwsem for read in safe.
15691+ * if the plink maintenance mode continues forever (that is the problem),
15692+ * may loop forever.
15693+ */
15694+static void au_mtx_and_read_lock(struct inode *inode)
15695+{
15696+ int err;
15697+ struct super_block *sb = inode->i_sb;
15698+
15699+ while (1) {
15700+ inode_lock(inode);
15701+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
15702+ if (!err)
15703+ break;
15704+ inode_unlock(inode);
15705+ si_read_lock(sb, AuLock_NOPLMW);
15706+ si_read_unlock(sb);
15707+ }
15708+}
15709+
15710+static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
15711+ struct iov_iter *iov_iter)
15712+{
15713+ ssize_t err;
15714+ struct file *file;
15715+ ssize_t (*iter)(struct kiocb *, struct iov_iter *);
15716+
15717+ err = security_file_permission(h_file, rw);
15718+ if (unlikely(err))
15719+ goto out;
15720+
15721+ err = -ENOSYS; /* the branch doesn't have its ->(read|write)_iter() */
15722+ iter = NULL;
15723+ if (rw == MAY_READ)
15724+ iter = h_file->f_op->read_iter;
15725+ else if (rw == MAY_WRITE)
15726+ iter = h_file->f_op->write_iter;
15727+
15728+ file = kio->ki_filp;
15729+ kio->ki_filp = h_file;
15730+ if (iter) {
15731+ lockdep_off();
15732+ err = iter(kio, iov_iter);
15733+ lockdep_on();
15734+ } else
15735+ /* currently there is no such fs */
15736+ WARN_ON_ONCE(1);
15737+ kio->ki_filp = file;
15738+
15739+out:
15740+ return err;
15741+}
15742+
15743+static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15744+{
15745+ ssize_t err;
15746+ struct file *file, *h_file;
15747+ struct inode *inode;
15748+ struct super_block *sb;
15749+
15750+ file = kio->ki_filp;
15751+ inode = file_inode(file);
15752+ sb = inode->i_sb;
15753+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15754+
15755+ h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
15756+ err = PTR_ERR(h_file);
15757+ if (IS_ERR(h_file))
15758+ goto out;
15759+
15760+ if (au_test_loopback_kthread()) {
15761+ au_warn_loopback(h_file->f_path.dentry->d_sb);
15762+ if (file->f_mapping != h_file->f_mapping) {
15763+ file->f_mapping = h_file->f_mapping;
15764+ smp_mb(); /* unnecessary? */
15765+ }
15766+ }
15767+ fi_read_unlock(file);
15768+
15769+ err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
15770+ /* todo: necessary? */
15771+ /* file->f_ra = h_file->f_ra; */
15772+ au_read_post(inode, h_file);
15773+
15774+out:
15775+ si_read_unlock(sb);
15776+ return err;
15777+}
15778+
15779+static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15780+{
15781+ ssize_t err;
15782+ struct au_write_pre wpre;
15783+ struct inode *inode;
15784+ struct file *file, *h_file;
15785+
15786+ file = kio->ki_filp;
15787+ inode = file_inode(file);
15788+ au_mtx_and_read_lock(inode);
15789+
15790+ wpre.lsc = 0;
15791+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15792+ err = PTR_ERR(h_file);
15793+ if (IS_ERR(h_file))
15794+ goto out;
15795+
15796+ err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
15797+ au_write_post(inode, h_file, &wpre, err);
15798+
15799+out:
15800+ si_read_unlock(inode->i_sb);
15801+ inode_unlock(inode);
15802+ return err;
15803+}
15804+
15805+/*
15806+ * We may be able to remove aufs_splice_{read,write}() since almost all FSes
15807+ * don't have their own .splice_{read,write} implimentations, and they use
15808+ * generic_file_splice_read() and iter_file_splice_write() who can act like the
15809+ * simple converters to f_op->iter_read() and ->iter_write().
15810+ * But we keep our own implementations because some non-mainlined FSes may have
15811+ * their own .splice_{read,write} implimentations and aufs doesn't want to take
15812+ * away an opportunity to co-work with aufs from them.
15813+ */
15814+static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
15815+ struct pipe_inode_info *pipe, size_t len,
15816+ unsigned int flags)
15817+{
15818+ ssize_t err;
15819+ struct file *h_file;
15820+ struct inode *inode;
15821+ struct super_block *sb;
15822+
15823+ inode = file_inode(file);
15824+ sb = inode->i_sb;
15825+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15826+
15827+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
15828+ err = PTR_ERR(h_file);
15829+ if (IS_ERR(h_file))
15830+ goto out;
15831+
15832+ err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
15833+ /* todo: necessary? */
15834+ /* file->f_ra = h_file->f_ra; */
15835+ au_read_post(inode, h_file);
15836+
15837+out:
15838+ si_read_unlock(sb);
15839+ return err;
15840+}
15841+
15842+static ssize_t
15843+aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
15844+ size_t len, unsigned int flags)
15845+{
15846+ ssize_t err;
15847+ struct au_write_pre wpre;
15848+ struct inode *inode;
15849+ struct file *h_file;
15850+
15851+ inode = file_inode(file);
15852+ au_mtx_and_read_lock(inode);
15853+
15854+ wpre.lsc = 0;
15855+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15856+ err = PTR_ERR(h_file);
15857+ if (IS_ERR(h_file))
15858+ goto out;
15859+
15860+ err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
15861+ au_write_post(inode, h_file, &wpre, err);
15862+
15863+out:
15864+ si_read_unlock(inode->i_sb);
15865+ inode_unlock(inode);
15866+ return err;
15867+}
15868+
15869+static long aufs_fallocate(struct file *file, int mode, loff_t offset,
15870+ loff_t len)
15871+{
15872+ long err;
15873+ struct au_write_pre wpre;
15874+ struct inode *inode;
15875+ struct file *h_file;
15876+
15877+ inode = file_inode(file);
15878+ au_mtx_and_read_lock(inode);
15879+
15880+ wpre.lsc = 0;
15881+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15882+ err = PTR_ERR(h_file);
15883+ if (IS_ERR(h_file))
15884+ goto out;
15885+
15886+ lockdep_off();
15887+ err = vfs_fallocate(h_file, mode, offset, len);
15888+ lockdep_on();
15889+ au_write_post(inode, h_file, &wpre, /*written*/1);
15890+
15891+out:
15892+ si_read_unlock(inode->i_sb);
15893+ inode_unlock(inode);
15894+ return err;
15895+}
15896+
15897+static ssize_t aufs_copy_file_range(struct file *src, loff_t src_pos,
15898+ struct file *dst, loff_t dst_pos,
15899+ size_t len, unsigned int flags)
15900+{
15901+ ssize_t err;
15902+ struct au_write_pre wpre;
15903+ enum { SRC, DST };
15904+ struct {
15905+ struct inode *inode;
15906+ struct file *h_file;
15907+ struct super_block *h_sb;
15908+ } a[2];
15909+#define a_src a[SRC]
15910+#define a_dst a[DST]
15911+
15912+ err = -EINVAL;
15913+ a_src.inode = file_inode(src);
15914+ if (unlikely(!S_ISREG(a_src.inode->i_mode)))
15915+ goto out;
15916+ a_dst.inode = file_inode(dst);
15917+ if (unlikely(!S_ISREG(a_dst.inode->i_mode)))
15918+ goto out;
15919+
15920+ au_mtx_and_read_lock(a_dst.inode);
15921+ /*
15922+ * in order to match the order in di_write_lock2_{child,parent}(),
15923+ * use f_path.dentry for this comparison.
15924+ */
15925+ if (src->f_path.dentry < dst->f_path.dentry) {
15926+ a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_1);
15927+ err = PTR_ERR(a_src.h_file);
15928+ if (IS_ERR(a_src.h_file))
15929+ goto out_si;
15930+
15931+ wpre.lsc = AuLsc_FI_2;
15932+ a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
15933+ err = PTR_ERR(a_dst.h_file);
15934+ if (IS_ERR(a_dst.h_file)) {
15935+ au_read_post(a_src.inode, a_src.h_file);
15936+ goto out_si;
15937+ }
15938+ } else {
15939+ wpre.lsc = AuLsc_FI_1;
15940+ a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
15941+ err = PTR_ERR(a_dst.h_file);
15942+ if (IS_ERR(a_dst.h_file))
15943+ goto out_si;
15944+
15945+ a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_2);
15946+ err = PTR_ERR(a_src.h_file);
15947+ if (IS_ERR(a_src.h_file)) {
15948+ au_write_post(a_dst.inode, a_dst.h_file, &wpre,
15949+ /*written*/0);
15950+ goto out_si;
15951+ }
15952+ }
15953+
15954+ err = -EXDEV;
15955+ a_src.h_sb = file_inode(a_src.h_file)->i_sb;
15956+ a_dst.h_sb = file_inode(a_dst.h_file)->i_sb;
15957+ if (unlikely(a_src.h_sb != a_dst.h_sb)) {
15958+ AuDbgFile(src);
15959+ AuDbgFile(dst);
15960+ goto out_file;
15961+ }
15962+
15963+ err = vfsub_copy_file_range(a_src.h_file, src_pos, a_dst.h_file,
15964+ dst_pos, len, flags);
15965+
15966+out_file:
15967+ au_write_post(a_dst.inode, a_dst.h_file, &wpre, err);
15968+ fi_read_unlock(src);
15969+ au_read_post(a_src.inode, a_src.h_file);
15970+out_si:
15971+ si_read_unlock(a_dst.inode->i_sb);
15972+ inode_unlock(a_dst.inode);
15973+out:
15974+ return err;
15975+#undef a_src
15976+#undef a_dst
15977+}
15978+
15979+/* ---------------------------------------------------------------------- */
15980+
15981+/*
15982+ * The locking order around current->mmap_sem.
15983+ * - in most and regular cases
15984+ * file I/O syscall -- aufs_read() or something
15985+ * -- si_rwsem for read -- mmap_sem
15986+ * (Note that [fdi]i_rwsem are released before mmap_sem).
15987+ * - in mmap case
15988+ * mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
15989+ * This AB-BA order is definitely bad, but is not a problem since "si_rwsem for
15990+ * read" allows multiple processes to acquire it and [fdi]i_rwsem are not held
15991+ * in file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
15992+ * It means that when aufs acquires si_rwsem for write, the process should never
15993+ * acquire mmap_sem.
15994+ *
15995+ * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
15996+ * problem either since any directory is not able to be mmap-ed.
15997+ * The similar scenario is applied to aufs_readlink() too.
15998+ */
15999+
16000+#if 0 /* stop calling security_file_mmap() */
16001+/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
16002+#define AuConv_VM_PROT(f, b) _calc_vm_trans(f, VM_##b, PROT_##b)
16003+
16004+static unsigned long au_arch_prot_conv(unsigned long flags)
16005+{
16006+ /* currently ppc64 only */
16007+#ifdef CONFIG_PPC64
16008+ /* cf. linux/arch/powerpc/include/asm/mman.h */
16009+ AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
16010+ return AuConv_VM_PROT(flags, SAO);
16011+#else
16012+ AuDebugOn(arch_calc_vm_prot_bits(-1));
16013+ return 0;
16014+#endif
16015+}
16016+
16017+static unsigned long au_prot_conv(unsigned long flags)
16018+{
16019+ return AuConv_VM_PROT(flags, READ)
16020+ | AuConv_VM_PROT(flags, WRITE)
16021+ | AuConv_VM_PROT(flags, EXEC)
16022+ | au_arch_prot_conv(flags);
16023+}
16024+
16025+/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
16026+#define AuConv_VM_MAP(f, b) _calc_vm_trans(f, VM_##b, MAP_##b)
16027+
16028+static unsigned long au_flag_conv(unsigned long flags)
16029+{
16030+ return AuConv_VM_MAP(flags, GROWSDOWN)
16031+ | AuConv_VM_MAP(flags, DENYWRITE)
16032+ | AuConv_VM_MAP(flags, LOCKED);
16033+}
16034+#endif
16035+
16036+static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
16037+{
16038+ int err;
16039+ const unsigned char wlock
16040+ = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
16041+ struct super_block *sb;
16042+ struct file *h_file;
16043+ struct inode *inode;
16044+
16045+ AuDbgVmRegion(file, vma);
16046+
16047+ inode = file_inode(file);
16048+ sb = inode->i_sb;
16049+ lockdep_off();
16050+ si_read_lock(sb, AuLock_NOPLMW);
16051+
16052+ h_file = au_write_pre(file, wlock, /*wpre*/NULL);
16053+ lockdep_on();
16054+ err = PTR_ERR(h_file);
16055+ if (IS_ERR(h_file))
16056+ goto out;
16057+
16058+ err = 0;
16059+ au_set_mmapped(file);
16060+ au_vm_file_reset(vma, h_file);
16061+ /*
16062+ * we cannot call security_mmap_file() here since it may acquire
16063+ * mmap_sem or i_mutex.
16064+ *
16065+ * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
16066+ * au_flag_conv(vma->vm_flags));
16067+ */
16068+ if (!err)
16069+ err = call_mmap(h_file, vma);
16070+ if (!err) {
16071+ au_vm_prfile_set(vma, file);
16072+ fsstack_copy_attr_atime(inode, file_inode(h_file));
16073+ goto out_fput; /* success */
16074+ }
16075+ au_unset_mmapped(file);
16076+ au_vm_file_reset(vma, file);
16077+
16078+out_fput:
16079+ lockdep_off();
16080+ ii_write_unlock(inode);
16081+ lockdep_on();
16082+ fput(h_file);
16083+out:
16084+ lockdep_off();
16085+ si_read_unlock(sb);
16086+ lockdep_on();
16087+ AuTraceErr(err);
16088+ return err;
16089+}
16090+
16091+/* ---------------------------------------------------------------------- */
16092+
16093+static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
16094+ int datasync)
16095+{
16096+ int err;
16097+ struct au_write_pre wpre;
16098+ struct inode *inode;
16099+ struct file *h_file;
16100+
16101+ err = 0; /* -EBADF; */ /* posix? */
16102+ if (unlikely(!(file->f_mode & FMODE_WRITE)))
16103+ goto out;
16104+
16105+ inode = file_inode(file);
16106+ au_mtx_and_read_lock(inode);
16107+
16108+ wpre.lsc = 0;
16109+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16110+ err = PTR_ERR(h_file);
16111+ if (IS_ERR(h_file))
16112+ goto out_unlock;
16113+
16114+ err = vfsub_fsync(h_file, &h_file->f_path, datasync);
16115+ au_write_post(inode, h_file, &wpre, /*written*/0);
16116+
16117+out_unlock:
16118+ si_read_unlock(inode->i_sb);
16119+ inode_unlock(inode);
16120+out:
16121+ return err;
16122+}
16123+
16124+static int aufs_fasync(int fd, struct file *file, int flag)
16125+{
16126+ int err;
16127+ struct file *h_file;
16128+ struct super_block *sb;
16129+
16130+ sb = file->f_path.dentry->d_sb;
16131+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16132+
16133+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16134+ err = PTR_ERR(h_file);
16135+ if (IS_ERR(h_file))
16136+ goto out;
16137+
16138+ if (h_file->f_op->fasync)
16139+ err = h_file->f_op->fasync(fd, h_file, flag);
16140+ fput(h_file); /* instead of au_read_post() */
16141+
16142+out:
16143+ si_read_unlock(sb);
16144+ return err;
16145+}
16146+
16147+static int aufs_setfl(struct file *file, unsigned long arg)
16148+{
16149+ int err;
16150+ struct file *h_file;
16151+ struct super_block *sb;
16152+
16153+ sb = file->f_path.dentry->d_sb;
16154+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16155+
16156+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16157+ err = PTR_ERR(h_file);
16158+ if (IS_ERR(h_file))
16159+ goto out;
16160+
16161+ /* stop calling h_file->fasync */
16162+ arg |= vfsub_file_flags(file) & FASYNC;
16163+ err = setfl(/*unused fd*/-1, h_file, arg);
16164+ fput(h_file); /* instead of au_read_post() */
16165+
16166+out:
16167+ si_read_unlock(sb);
16168+ return err;
16169+}
16170+
16171+/* ---------------------------------------------------------------------- */
16172+
16173+/* no one supports this operation, currently */
16174+#if 0 /* reserved for future use */
16175+static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
16176+ size_t len, loff_t *pos, int more)
16177+{
16178+}
16179+#endif
16180+
16181+/* ---------------------------------------------------------------------- */
16182+
16183+const struct file_operations aufs_file_fop = {
16184+ .owner = THIS_MODULE,
16185+
16186+ .llseek = default_llseek,
16187+
16188+ .read_iter = aufs_read_iter,
16189+ .write_iter = aufs_write_iter,
16190+
16191+#ifdef CONFIG_AUFS_POLL
16192+ .poll = aufs_poll,
16193+#endif
16194+ .unlocked_ioctl = aufs_ioctl_nondir,
16195+#ifdef CONFIG_COMPAT
16196+ .compat_ioctl = aufs_compat_ioctl_nondir,
16197+#endif
16198+ .mmap = aufs_mmap,
16199+ .open = aufs_open_nondir,
16200+ .flush = aufs_flush_nondir,
16201+ .release = aufs_release_nondir,
16202+ .fsync = aufs_fsync_nondir,
16203+ .fasync = aufs_fasync,
16204+ /* .sendpage = aufs_sendpage, */
16205+ .setfl = aufs_setfl,
16206+ .splice_write = aufs_splice_write,
16207+ .splice_read = aufs_splice_read,
16208+#if 0 /* reserved for future use */
16209+ .aio_splice_write = aufs_aio_splice_write,
16210+ .aio_splice_read = aufs_aio_splice_read,
16211+#endif
16212+ .fallocate = aufs_fallocate,
16213+ .copy_file_range = aufs_copy_file_range
16214+};
16215diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
16216--- /usr/share/empty/fs/aufs/fstype.h 1970-01-01 01:00:00.000000000 +0100
16217+++ linux/fs/aufs/fstype.h 2021-02-24 13:33:42.744347058 +0100
16218@@ -0,0 +1,401 @@
16219+/* SPDX-License-Identifier: GPL-2.0 */
16220+/*
16221+ * Copyright (C) 2005-2020 Junjiro R. Okajima
16222+ *
16223+ * This program, aufs is free software; you can redistribute it and/or modify
16224+ * it under the terms of the GNU General Public License as published by
16225+ * the Free Software Foundation; either version 2 of the License, or
16226+ * (at your option) any later version.
16227+ *
16228+ * This program is distributed in the hope that it will be useful,
16229+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16230+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16231+ * GNU General Public License for more details.
16232+ *
16233+ * You should have received a copy of the GNU General Public License
16234+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
16235+ */
16236+
16237+/*
16238+ * judging filesystem type
16239+ */
16240+
16241+#ifndef __AUFS_FSTYPE_H__
16242+#define __AUFS_FSTYPE_H__
16243+
16244+#ifdef __KERNEL__
16245+
16246+#include <linux/fs.h>
16247+#include <linux/magic.h>
16248+#include <linux/nfs_fs.h>
16249+#include <linux/romfs_fs.h>
16250+
16251+static inline int au_test_aufs(struct super_block *sb)
16252+{
16253+ return sb->s_magic == AUFS_SUPER_MAGIC;
16254+}
16255+
16256+static inline const char *au_sbtype(struct super_block *sb)
16257+{
16258+ return sb->s_type->name;
16259+}
16260+
16261+static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
16262+{
16263+#if IS_ENABLED(CONFIG_ISO9660_FS)
16264+ return sb->s_magic == ISOFS_SUPER_MAGIC;
16265+#else
16266+ return 0;
16267+#endif
16268+}
16269+
16270+static inline int au_test_romfs(struct super_block *sb __maybe_unused)
16271+{
16272+#if IS_ENABLED(CONFIG_ROMFS_FS)
16273+ return sb->s_magic == ROMFS_MAGIC;
16274+#else
16275+ return 0;
16276+#endif
16277+}
16278+
16279+static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
16280+{
16281+#if IS_ENABLED(CONFIG_CRAMFS)
16282+ return sb->s_magic == CRAMFS_MAGIC;
16283+#endif
16284+ return 0;
16285+}
16286+
16287+static inline int au_test_nfs(struct super_block *sb __maybe_unused)
16288+{
16289+#if IS_ENABLED(CONFIG_NFS_FS)
16290+ return sb->s_magic == NFS_SUPER_MAGIC;
16291+#else
16292+ return 0;
16293+#endif
16294+}
16295+
16296+static inline int au_test_fuse(struct super_block *sb __maybe_unused)
16297+{
16298+#if IS_ENABLED(CONFIG_FUSE_FS)
16299+ return sb->s_magic == FUSE_SUPER_MAGIC;
16300+#else
16301+ return 0;
16302+#endif
16303+}
16304+
16305+static inline int au_test_xfs(struct super_block *sb __maybe_unused)
16306+{
16307+#if IS_ENABLED(CONFIG_XFS_FS)
16308+ return sb->s_magic == XFS_SB_MAGIC;
16309+#else
16310+ return 0;
16311+#endif
16312+}
16313+
16314+static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
16315+{
16316+#ifdef CONFIG_TMPFS
16317+ return sb->s_magic == TMPFS_MAGIC;
16318+#else
16319+ return 0;
16320+#endif
16321+}
16322+
16323+static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
16324+{
16325+#if IS_ENABLED(CONFIG_ECRYPT_FS)
16326+ return !strcmp(au_sbtype(sb), "ecryptfs");
16327+#else
16328+ return 0;
16329+#endif
16330+}
16331+
16332+static inline int au_test_ramfs(struct super_block *sb)
16333+{
16334+ return sb->s_magic == RAMFS_MAGIC;
16335+}
16336+
16337+static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
16338+{
16339+#if IS_ENABLED(CONFIG_UBIFS_FS)
16340+ return sb->s_magic == UBIFS_SUPER_MAGIC;
16341+#else
16342+ return 0;
16343+#endif
16344+}
16345+
16346+static inline int au_test_procfs(struct super_block *sb __maybe_unused)
16347+{
16348+#ifdef CONFIG_PROC_FS
16349+ return sb->s_magic == PROC_SUPER_MAGIC;
16350+#else
16351+ return 0;
16352+#endif
16353+}
16354+
16355+static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
16356+{
16357+#ifdef CONFIG_SYSFS
16358+ return sb->s_magic == SYSFS_MAGIC;
16359+#else
16360+ return 0;
16361+#endif
16362+}
16363+
16364+static inline int au_test_configfs(struct super_block *sb __maybe_unused)
16365+{
16366+#if IS_ENABLED(CONFIG_CONFIGFS_FS)
16367+ return sb->s_magic == CONFIGFS_MAGIC;
16368+#else
16369+ return 0;
16370+#endif
16371+}
16372+
16373+static inline int au_test_minix(struct super_block *sb __maybe_unused)
16374+{
16375+#if IS_ENABLED(CONFIG_MINIX_FS)
16376+ return sb->s_magic == MINIX3_SUPER_MAGIC
16377+ || sb->s_magic == MINIX2_SUPER_MAGIC
16378+ || sb->s_magic == MINIX2_SUPER_MAGIC2
16379+ || sb->s_magic == MINIX_SUPER_MAGIC
16380+ || sb->s_magic == MINIX_SUPER_MAGIC2;
16381+#else
16382+ return 0;
16383+#endif
16384+}
16385+
16386+static inline int au_test_fat(struct super_block *sb __maybe_unused)
16387+{
16388+#if IS_ENABLED(CONFIG_FAT_FS)
16389+ return sb->s_magic == MSDOS_SUPER_MAGIC;
16390+#else
16391+ return 0;
16392+#endif
16393+}
16394+
16395+static inline int au_test_msdos(struct super_block *sb)
16396+{
16397+ return au_test_fat(sb);
16398+}
16399+
16400+static inline int au_test_vfat(struct super_block *sb)
16401+{
16402+ return au_test_fat(sb);
16403+}
16404+
16405+static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
16406+{
16407+#ifdef CONFIG_SECURITYFS
16408+ return sb->s_magic == SECURITYFS_MAGIC;
16409+#else
16410+ return 0;
16411+#endif
16412+}
16413+
16414+static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
16415+{
16416+#if IS_ENABLED(CONFIG_SQUASHFS)
16417+ return sb->s_magic == SQUASHFS_MAGIC;
16418+#else
16419+ return 0;
16420+#endif
16421+}
16422+
16423+static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
16424+{
16425+#if IS_ENABLED(CONFIG_BTRFS_FS)
16426+ return sb->s_magic == BTRFS_SUPER_MAGIC;
16427+#else
16428+ return 0;
16429+#endif
16430+}
16431+
16432+static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
16433+{
16434+#if IS_ENABLED(CONFIG_XENFS)
16435+ return sb->s_magic == XENFS_SUPER_MAGIC;
16436+#else
16437+ return 0;
16438+#endif
16439+}
16440+
16441+static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
16442+{
16443+#ifdef CONFIG_DEBUG_FS
16444+ return sb->s_magic == DEBUGFS_MAGIC;
16445+#else
16446+ return 0;
16447+#endif
16448+}
16449+
16450+static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
16451+{
16452+#if IS_ENABLED(CONFIG_NILFS)
16453+ return sb->s_magic == NILFS_SUPER_MAGIC;
16454+#else
16455+ return 0;
16456+#endif
16457+}
16458+
16459+static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
16460+{
16461+#if IS_ENABLED(CONFIG_HFSPLUS_FS)
16462+ return sb->s_magic == HFSPLUS_SUPER_MAGIC;
16463+#else
16464+ return 0;
16465+#endif
16466+}
16467+
16468+/* ---------------------------------------------------------------------- */
16469+/*
16470+ * they can't be an aufs branch.
16471+ */
16472+static inline int au_test_fs_unsuppoted(struct super_block *sb)
16473+{
16474+ return
16475+#ifndef CONFIG_AUFS_BR_RAMFS
16476+ au_test_ramfs(sb) ||
16477+#endif
16478+ au_test_procfs(sb)
16479+ || au_test_sysfs(sb)
16480+ || au_test_configfs(sb)
16481+ || au_test_debugfs(sb)
16482+ || au_test_securityfs(sb)
16483+ || au_test_xenfs(sb)
16484+ || au_test_ecryptfs(sb)
16485+ /* || !strcmp(au_sbtype(sb), "unionfs") */
16486+ || au_test_aufs(sb); /* will be supported in next version */
16487+}
16488+
16489+static inline int au_test_fs_remote(struct super_block *sb)
16490+{
16491+ return !au_test_tmpfs(sb)
16492+#ifdef CONFIG_AUFS_BR_RAMFS
16493+ && !au_test_ramfs(sb)
16494+#endif
16495+ && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
16496+}
16497+
16498+/* ---------------------------------------------------------------------- */
16499+
16500+/*
16501+ * Note: these functions (below) are created after reading ->getattr() in all
16502+ * filesystems under linux/fs. it means we have to do so in every update...
16503+ */
16504+
16505+/*
16506+ * some filesystems require getattr to refresh the inode attributes before
16507+ * referencing.
16508+ * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
16509+ * and leave the work for d_revalidate()
16510+ */
16511+static inline int au_test_fs_refresh_iattr(struct super_block *sb)
16512+{
16513+ return au_test_nfs(sb)
16514+ || au_test_fuse(sb)
16515+ /* || au_test_btrfs(sb) */ /* untested */
16516+ ;
16517+}
16518+
16519+/*
16520+ * filesystems which don't maintain i_size or i_blocks.
16521+ */
16522+static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
16523+{
16524+ return au_test_xfs(sb)
16525+ || au_test_btrfs(sb)
16526+ || au_test_ubifs(sb)
16527+ || au_test_hfsplus(sb) /* maintained, but incorrect */
16528+ /* || au_test_minix(sb) */ /* untested */
16529+ ;
16530+}
16531+
16532+/*
16533+ * filesystems which don't store the correct value in some of their inode
16534+ * attributes.
16535+ */
16536+static inline int au_test_fs_bad_iattr(struct super_block *sb)
16537+{
16538+ return au_test_fs_bad_iattr_size(sb)
16539+ || au_test_fat(sb)
16540+ || au_test_msdos(sb)
16541+ || au_test_vfat(sb);
16542+}
16543+
16544+/* they don't check i_nlink in link(2) */
16545+static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
16546+{
16547+ return au_test_tmpfs(sb)
16548+#ifdef CONFIG_AUFS_BR_RAMFS
16549+ || au_test_ramfs(sb)
16550+#endif
16551+ || au_test_ubifs(sb)
16552+ || au_test_hfsplus(sb);
16553+}
16554+
16555+/*
16556+ * filesystems which sets S_NOATIME and S_NOCMTIME.
16557+ */
16558+static inline int au_test_fs_notime(struct super_block *sb)
16559+{
16560+ return au_test_nfs(sb)
16561+ || au_test_fuse(sb)
16562+ || au_test_ubifs(sb)
16563+ ;
16564+}
16565+
16566+/* temporary support for i#1 in cramfs */
16567+static inline int au_test_fs_unique_ino(struct inode *inode)
16568+{
16569+ if (au_test_cramfs(inode->i_sb))
16570+ return inode->i_ino != 1;
16571+ return 1;
16572+}
16573+
16574+/* ---------------------------------------------------------------------- */
16575+
16576+/*
16577+ * the filesystem where the xino files placed must support i/o after unlink and
16578+ * maintain i_size and i_blocks.
16579+ */
16580+static inline int au_test_fs_bad_xino(struct super_block *sb)
16581+{
16582+ return au_test_fs_remote(sb)
16583+ || au_test_fs_bad_iattr_size(sb)
16584+ /* don't want unnecessary work for xino */
16585+ || au_test_aufs(sb)
16586+ || au_test_ecryptfs(sb)
16587+ || au_test_nilfs(sb);
16588+}
16589+
16590+static inline int au_test_fs_trunc_xino(struct super_block *sb)
16591+{
16592+ return au_test_tmpfs(sb)
16593+ || au_test_ramfs(sb);
16594+}
16595+
16596+/*
16597+ * test if the @sb is real-readonly.
16598+ */
16599+static inline int au_test_fs_rr(struct super_block *sb)
16600+{
16601+ return au_test_squashfs(sb)
16602+ || au_test_iso9660(sb)
16603+ || au_test_cramfs(sb)
16604+ || au_test_romfs(sb);
16605+}
16606+
16607+/*
16608+ * test if the @inode is nfs with 'noacl' option
16609+ * NFS always sets SB_POSIXACL regardless its mount option 'noacl.'
16610+ */
16611+static inline int au_test_nfs_noacl(struct inode *inode)
16612+{
16613+ return au_test_nfs(inode->i_sb)
16614+ /* && IS_POSIXACL(inode) */
16615+ && !nfs_server_capable(inode, NFS_CAP_ACLS);
16616+}
16617+
16618+#endif /* __KERNEL__ */
16619+#endif /* __AUFS_FSTYPE_H__ */
16620diff -urN /usr/share/empty/fs/aufs/hbl.h linux/fs/aufs/hbl.h
16621--- /usr/share/empty/fs/aufs/hbl.h 1970-01-01 01:00:00.000000000 +0100
16622+++ linux/fs/aufs/hbl.h 2021-02-24 13:33:42.744347058 +0100
16623@@ -0,0 +1,65 @@
16624+/* SPDX-License-Identifier: GPL-2.0 */
16625+/*
16626+ * Copyright (C) 2017-2020 Junjiro R. Okajima
16627+ *
16628+ * This program, aufs is free software; you can redistribute it and/or modify
16629+ * it under the terms of the GNU General Public License as published by
16630+ * the Free Software Foundation; either version 2 of the License, or
16631+ * (at your option) any later version.
16632+ *
16633+ * This program is distributed in the hope that it will be useful,
16634+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16635+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16636+ * GNU General Public License for more details.
16637+ *
16638+ * You should have received a copy of the GNU General Public License
16639+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
16640+ */
16641+
16642+/*
16643+ * helpers for hlist_bl.h
16644+ */
16645+
16646+#ifndef __AUFS_HBL_H__
16647+#define __AUFS_HBL_H__
16648+
16649+#ifdef __KERNEL__
16650+
16651+#include <linux/list_bl.h>
16652+
16653+static inline void au_hbl_add(struct hlist_bl_node *node,
16654+ struct hlist_bl_head *hbl)
16655+{
16656+ hlist_bl_lock(hbl);
16657+ hlist_bl_add_head(node, hbl);
16658+ hlist_bl_unlock(hbl);
16659+}
16660+
16661+static inline void au_hbl_del(struct hlist_bl_node *node,
16662+ struct hlist_bl_head *hbl)
16663+{
16664+ hlist_bl_lock(hbl);
16665+ hlist_bl_del(node);
16666+ hlist_bl_unlock(hbl);
16667+}
16668+
16669+#define au_hbl_for_each(pos, head) \
16670+ for (pos = hlist_bl_first(head); \
16671+ pos; \
16672+ pos = pos->next)
16673+
16674+static inline unsigned long au_hbl_count(struct hlist_bl_head *hbl)
16675+{
16676+ unsigned long cnt;
16677+ struct hlist_bl_node *pos;
16678+
16679+ cnt = 0;
16680+ hlist_bl_lock(hbl);
16681+ au_hbl_for_each(pos, hbl)
16682+ cnt++;
16683+ hlist_bl_unlock(hbl);
16684+ return cnt;
16685+}
16686+
16687+#endif /* __KERNEL__ */
16688+#endif /* __AUFS_HBL_H__ */
16689diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
16690--- /usr/share/empty/fs/aufs/hfsnotify.c 1970-01-01 01:00:00.000000000 +0100
16691+++ linux/fs/aufs/hfsnotify.c 2021-02-24 13:33:42.744347058 +0100
16692@@ -0,0 +1,288 @@
16693+// SPDX-License-Identifier: GPL-2.0
16694+/*
16695+ * Copyright (C) 2005-2020 Junjiro R. Okajima
16696+ *
16697+ * This program, aufs is free software; you can redistribute it and/or modify
16698+ * it under the terms of the GNU General Public License as published by
16699+ * the Free Software Foundation; either version 2 of the License, or
16700+ * (at your option) any later version.
16701+ *
16702+ * This program is distributed in the hope that it will be useful,
16703+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16704+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16705+ * GNU General Public License for more details.
16706+ *
16707+ * You should have received a copy of the GNU General Public License
16708+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
16709+ */
16710+
16711+/*
16712+ * fsnotify for the lower directories
16713+ */
16714+
16715+#include "aufs.h"
16716+
16717+/* FS_IN_IGNORED is unnecessary */
16718+static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
16719+ | FS_CREATE | FS_EVENT_ON_CHILD);
16720+static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
16721+static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
16722+
16723+static void au_hfsn_free_mark(struct fsnotify_mark *mark)
16724+{
16725+ struct au_hnotify *hn = container_of(mark, struct au_hnotify,
16726+ hn_mark);
16727+ /* AuDbg("here\n"); */
16728+ au_cache_free_hnotify(hn);
16729+ smp_mb__before_atomic(); /* for atomic64_dec */
16730+ if (atomic64_dec_and_test(&au_hfsn_ifree))
16731+ wake_up(&au_hfsn_wq);
16732+}
16733+
16734+static int au_hfsn_alloc(struct au_hinode *hinode)
16735+{
16736+ int err;
16737+ struct au_hnotify *hn;
16738+ struct super_block *sb;
16739+ struct au_branch *br;
16740+ struct fsnotify_mark *mark;
16741+ aufs_bindex_t bindex;
16742+
16743+ hn = hinode->hi_notify;
16744+ sb = hn->hn_aufs_inode->i_sb;
16745+ bindex = au_br_index(sb, hinode->hi_id);
16746+ br = au_sbr(sb, bindex);
16747+ AuDebugOn(!br->br_hfsn);
16748+
16749+ mark = &hn->hn_mark;
16750+ fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
16751+ mark->mask = AuHfsnMask;
16752+ /*
16753+ * by udba rename or rmdir, aufs assign a new inode to the known
16754+ * h_inode, so specify 1 to allow dups.
16755+ */
16756+ lockdep_off();
16757+ err = fsnotify_add_inode_mark(mark, hinode->hi_inode, /*allow_dups*/1);
16758+ lockdep_on();
16759+
16760+ return err;
16761+}
16762+
16763+static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
16764+{
16765+ struct fsnotify_mark *mark;
16766+ unsigned long long ull;
16767+ struct fsnotify_group *group;
16768+
16769+ ull = atomic64_inc_return(&au_hfsn_ifree);
16770+ BUG_ON(!ull);
16771+
16772+ mark = &hn->hn_mark;
16773+ spin_lock(&mark->lock);
16774+ group = mark->group;
16775+ fsnotify_get_group(group);
16776+ spin_unlock(&mark->lock);
16777+ lockdep_off();
16778+ fsnotify_destroy_mark(mark, group);
16779+ fsnotify_put_mark(mark);
16780+ fsnotify_put_group(group);
16781+ lockdep_on();
16782+
16783+ /* free hn by myself */
16784+ return 0;
16785+}
16786+
16787+/* ---------------------------------------------------------------------- */
16788+
16789+static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
16790+{
16791+ struct fsnotify_mark *mark;
16792+
16793+ mark = &hinode->hi_notify->hn_mark;
16794+ spin_lock(&mark->lock);
16795+ if (do_set) {
16796+ AuDebugOn(mark->mask & AuHfsnMask);
16797+ mark->mask |= AuHfsnMask;
16798+ } else {
16799+ AuDebugOn(!(mark->mask & AuHfsnMask));
16800+ mark->mask &= ~AuHfsnMask;
16801+ }
16802+ spin_unlock(&mark->lock);
16803+ /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
16804+}
16805+
16806+/* ---------------------------------------------------------------------- */
16807+
16808+/* #define AuDbgHnotify */
16809+#ifdef AuDbgHnotify
16810+static char *au_hfsn_name(u32 mask)
16811+{
16812+#ifdef CONFIG_AUFS_DEBUG
16813+#define test_ret(flag) \
16814+ do { \
16815+ if (mask & flag) \
16816+ return #flag; \
16817+ } while (0)
16818+ test_ret(FS_ACCESS);
16819+ test_ret(FS_MODIFY);
16820+ test_ret(FS_ATTRIB);
16821+ test_ret(FS_CLOSE_WRITE);
16822+ test_ret(FS_CLOSE_NOWRITE);
16823+ test_ret(FS_OPEN);
16824+ test_ret(FS_MOVED_FROM);
16825+ test_ret(FS_MOVED_TO);
16826+ test_ret(FS_CREATE);
16827+ test_ret(FS_DELETE);
16828+ test_ret(FS_DELETE_SELF);
16829+ test_ret(FS_MOVE_SELF);
16830+ test_ret(FS_UNMOUNT);
16831+ test_ret(FS_Q_OVERFLOW);
16832+ test_ret(FS_IN_IGNORED);
16833+ test_ret(FS_ISDIR);
16834+ test_ret(FS_IN_ONESHOT);
16835+ test_ret(FS_EVENT_ON_CHILD);
16836+ return "";
16837+#undef test_ret
16838+#else
16839+ return "??";
16840+#endif
16841+}
16842+#endif
16843+
16844+/* ---------------------------------------------------------------------- */
16845+
16846+static void au_hfsn_free_group(struct fsnotify_group *group)
16847+{
16848+ struct au_br_hfsnotify *hfsn = group->private;
16849+
16850+ /* AuDbg("here\n"); */
16851+ au_kfree_try_rcu(hfsn);
16852+}
16853+
16854+static int au_hfsn_handle_event(struct fsnotify_group *group,
16855+ u32 mask, const void *data, int data_type,
16856+ struct inode *dir,
16857+ const struct qstr *file_name, u32 cookie,
16858+ struct fsnotify_iter_info *iter_info)
16859+{
16860+ int err;
16861+ struct au_hnotify *hnotify;
16862+ struct inode *h_dir, *h_inode;
16863+ struct fsnotify_mark *inode_mark;
16864+
16865+ AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
16866+
16867+ err = 0;
16868+ /* if FS_UNMOUNT happens, there must be another bug */
16869+ AuDebugOn(mask & FS_UNMOUNT);
16870+ if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
16871+ goto out;
16872+
16873+ h_dir = dir;
16874+ h_inode = NULL;
16875+#ifdef AuDbgHnotify
16876+ au_debug_on();
16877+ if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
16878+ || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
16879+ AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
16880+ h_dir->i_ino, mask, au_hfsn_name(mask),
16881+ AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
16882+ /* WARN_ON(1); */
16883+ }
16884+ au_debug_off();
16885+#endif
16886+
16887+ inode_mark = fsnotify_iter_inode_mark(iter_info);
16888+ AuDebugOn(!inode_mark);
16889+ hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
16890+ err = au_hnotify(h_dir, hnotify, mask, file_name, h_inode);
16891+
16892+out:
16893+ return err;
16894+}
16895+
16896+static struct fsnotify_ops au_hfsn_ops = {
16897+ .handle_event = au_hfsn_handle_event,
16898+ .free_group_priv = au_hfsn_free_group,
16899+ .free_mark = au_hfsn_free_mark
16900+};
16901+
16902+/* ---------------------------------------------------------------------- */
16903+
16904+static void au_hfsn_fin_br(struct au_branch *br)
16905+{
16906+ struct au_br_hfsnotify *hfsn;
16907+
16908+ hfsn = br->br_hfsn;
16909+ if (hfsn) {
16910+ lockdep_off();
16911+ fsnotify_put_group(hfsn->hfsn_group);
16912+ lockdep_on();
16913+ }
16914+}
16915+
16916+static int au_hfsn_init_br(struct au_branch *br, int perm)
16917+{
16918+ int err;
16919+ struct fsnotify_group *group;
16920+ struct au_br_hfsnotify *hfsn;
16921+
16922+ err = 0;
16923+ br->br_hfsn = NULL;
16924+ if (!au_br_hnotifyable(perm))
16925+ goto out;
16926+
16927+ err = -ENOMEM;
16928+ hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
16929+ if (unlikely(!hfsn))
16930+ goto out;
16931+
16932+ err = 0;
16933+ group = fsnotify_alloc_group(&au_hfsn_ops);
16934+ if (IS_ERR(group)) {
16935+ err = PTR_ERR(group);
16936+ pr_err("fsnotify_alloc_group() failed, %d\n", err);
16937+ goto out_hfsn;
16938+ }
16939+
16940+ group->private = hfsn;
16941+ hfsn->hfsn_group = group;
16942+ br->br_hfsn = hfsn;
16943+ goto out; /* success */
16944+
16945+out_hfsn:
16946+ au_kfree_try_rcu(hfsn);
16947+out:
16948+ return err;
16949+}
16950+
16951+static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
16952+{
16953+ int err;
16954+
16955+ err = 0;
16956+ if (!br->br_hfsn)
16957+ err = au_hfsn_init_br(br, perm);
16958+
16959+ return err;
16960+}
16961+
16962+/* ---------------------------------------------------------------------- */
16963+
16964+static void au_hfsn_fin(void)
16965+{
16966+ AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
16967+ wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
16968+}
16969+
16970+const struct au_hnotify_op au_hnotify_op = {
16971+ .ctl = au_hfsn_ctl,
16972+ .alloc = au_hfsn_alloc,
16973+ .free = au_hfsn_free,
16974+
16975+ .fin = au_hfsn_fin,
16976+
16977+ .reset_br = au_hfsn_reset_br,
16978+ .fin_br = au_hfsn_fin_br,
16979+ .init_br = au_hfsn_init_br
16980+};
16981diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
16982--- /usr/share/empty/fs/aufs/hfsplus.c 1970-01-01 01:00:00.000000000 +0100
16983+++ linux/fs/aufs/hfsplus.c 2021-02-24 13:33:42.744347058 +0100
16984@@ -0,0 +1,60 @@
16985+// SPDX-License-Identifier: GPL-2.0
16986+/*
16987+ * Copyright (C) 2010-2020 Junjiro R. Okajima
16988+ *
16989+ * This program, aufs is free software; you can redistribute it and/or modify
16990+ * it under the terms of the GNU General Public License as published by
16991+ * the Free Software Foundation; either version 2 of the License, or
16992+ * (at your option) any later version.
16993+ *
16994+ * This program is distributed in the hope that it will be useful,
16995+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16996+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16997+ * GNU General Public License for more details.
16998+ *
16999+ * You should have received a copy of the GNU General Public License
17000+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
17001+ */
17002+
17003+/*
17004+ * special support for filesystems which acquires an inode mutex
17005+ * at final closing a file, eg, hfsplus.
17006+ *
17007+ * This trick is very simple and stupid, just to open the file before really
17008+ * necessary open to tell hfsplus that this is not the final closing.
17009+ * The caller should call au_h_open_pre() after acquiring the inode mutex,
17010+ * and au_h_open_post() after releasing it.
17011+ */
17012+
17013+#include "aufs.h"
17014+
17015+struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
17016+ int force_wr)
17017+{
17018+ struct file *h_file;
17019+ struct dentry *h_dentry;
17020+
17021+ h_dentry = au_h_dptr(dentry, bindex);
17022+ AuDebugOn(!h_dentry);
17023+ AuDebugOn(d_is_negative(h_dentry));
17024+
17025+ h_file = NULL;
17026+ if (au_test_hfsplus(h_dentry->d_sb)
17027+ && d_is_reg(h_dentry))
17028+ h_file = au_h_open(dentry, bindex,
17029+ O_RDONLY | O_NOATIME | O_LARGEFILE,
17030+ /*file*/NULL, force_wr);
17031+ return h_file;
17032+}
17033+
17034+void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
17035+ struct file *h_file)
17036+{
17037+ struct au_branch *br;
17038+
17039+ if (h_file) {
17040+ fput(h_file);
17041+ br = au_sbr(dentry->d_sb, bindex);
17042+ au_lcnt_dec(&br->br_nfiles);
17043+ }
17044+}
17045diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
17046--- /usr/share/empty/fs/aufs/hnotify.c 1970-01-01 01:00:00.000000000 +0100
17047+++ linux/fs/aufs/hnotify.c 2021-02-24 13:33:42.744347058 +0100
17048@@ -0,0 +1,715 @@
17049+// SPDX-License-Identifier: GPL-2.0
17050+/*
17051+ * Copyright (C) 2005-2020 Junjiro R. Okajima
17052+ *
17053+ * This program, aufs is free software; you can redistribute it and/or modify
17054+ * it under the terms of the GNU General Public License as published by
17055+ * the Free Software Foundation; either version 2 of the License, or
17056+ * (at your option) any later version.
17057+ *
17058+ * This program is distributed in the hope that it will be useful,
17059+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17060+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17061+ * GNU General Public License for more details.
17062+ *
17063+ * You should have received a copy of the GNU General Public License
17064+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
17065+ */
17066+
17067+/*
17068+ * abstraction to notify the direct changes on lower directories
17069+ */
17070+
17071+/* #include <linux/iversion.h> */
17072+#include "aufs.h"
17073+
17074+int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
17075+{
17076+ int err;
17077+ struct au_hnotify *hn;
17078+
17079+ err = -ENOMEM;
17080+ hn = au_cache_alloc_hnotify();
17081+ if (hn) {
17082+ hn->hn_aufs_inode = inode;
17083+ hinode->hi_notify = hn;
17084+ err = au_hnotify_op.alloc(hinode);
17085+ AuTraceErr(err);
17086+ if (unlikely(err)) {
17087+ hinode->hi_notify = NULL;
17088+ au_cache_free_hnotify(hn);
17089+ /*
17090+ * The upper dir was removed by udba, but the same named
17091+ * dir left. In this case, aufs assigns a new inode
17092+ * number and set the monitor again.
17093+ * For the lower dir, the old monitor is still left.
17094+ */
17095+ if (err == -EEXIST)
17096+ err = 0;
17097+ }
17098+ }
17099+
17100+ AuTraceErr(err);
17101+ return err;
17102+}
17103+
17104+void au_hn_free(struct au_hinode *hinode)
17105+{
17106+ struct au_hnotify *hn;
17107+
17108+ hn = hinode->hi_notify;
17109+ if (hn) {
17110+ hinode->hi_notify = NULL;
17111+ if (au_hnotify_op.free(hinode, hn))
17112+ au_cache_free_hnotify(hn);
17113+ }
17114+}
17115+
17116+/* ---------------------------------------------------------------------- */
17117+
17118+void au_hn_ctl(struct au_hinode *hinode, int do_set)
17119+{
17120+ if (hinode->hi_notify)
17121+ au_hnotify_op.ctl(hinode, do_set);
17122+}
17123+
17124+void au_hn_reset(struct inode *inode, unsigned int flags)
17125+{
17126+ aufs_bindex_t bindex, bbot;
17127+ struct inode *hi;
17128+ struct dentry *iwhdentry;
17129+
17130+ bbot = au_ibbot(inode);
17131+ for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
17132+ hi = au_h_iptr(inode, bindex);
17133+ if (!hi)
17134+ continue;
17135+
17136+ /* inode_lock_nested(hi, AuLsc_I_CHILD); */
17137+ iwhdentry = au_hi_wh(inode, bindex);
17138+ if (iwhdentry)
17139+ dget(iwhdentry);
17140+ au_igrab(hi);
17141+ au_set_h_iptr(inode, bindex, NULL, 0);
17142+ au_set_h_iptr(inode, bindex, au_igrab(hi),
17143+ flags & ~AuHi_XINO);
17144+ iput(hi);
17145+ dput(iwhdentry);
17146+ /* inode_unlock(hi); */
17147+ }
17148+}
17149+
17150+/* ---------------------------------------------------------------------- */
17151+
17152+static int hn_xino(struct inode *inode, struct inode *h_inode)
17153+{
17154+ int err;
17155+ aufs_bindex_t bindex, bbot, bfound, btop;
17156+ struct inode *h_i;
17157+
17158+ err = 0;
17159+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
17160+ pr_warn("branch root dir was changed\n");
17161+ goto out;
17162+ }
17163+
17164+ bfound = -1;
17165+ bbot = au_ibbot(inode);
17166+ btop = au_ibtop(inode);
17167+#if 0 /* reserved for future use */
17168+ if (bindex == bbot) {
17169+ /* keep this ino in rename case */
17170+ goto out;
17171+ }
17172+#endif
17173+ for (bindex = btop; bindex <= bbot; bindex++)
17174+ if (au_h_iptr(inode, bindex) == h_inode) {
17175+ bfound = bindex;
17176+ break;
17177+ }
17178+ if (bfound < 0)
17179+ goto out;
17180+
17181+ for (bindex = btop; bindex <= bbot; bindex++) {
17182+ h_i = au_h_iptr(inode, bindex);
17183+ if (!h_i)
17184+ continue;
17185+
17186+ err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
17187+ /* ignore this error */
17188+ /* bad action? */
17189+ }
17190+
17191+ /* children inode number will be broken */
17192+
17193+out:
17194+ AuTraceErr(err);
17195+ return err;
17196+}
17197+
17198+static int hn_gen_tree(struct dentry *dentry)
17199+{
17200+ int err, i, j, ndentry;
17201+ struct au_dcsub_pages dpages;
17202+ struct au_dpage *dpage;
17203+ struct dentry **dentries;
17204+
17205+ err = au_dpages_init(&dpages, GFP_NOFS);
17206+ if (unlikely(err))
17207+ goto out;
17208+ err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
17209+ if (unlikely(err))
17210+ goto out_dpages;
17211+
17212+ for (i = 0; i < dpages.ndpage; i++) {
17213+ dpage = dpages.dpages + i;
17214+ dentries = dpage->dentries;
17215+ ndentry = dpage->ndentry;
17216+ for (j = 0; j < ndentry; j++) {
17217+ struct dentry *d;
17218+
17219+ d = dentries[j];
17220+ if (IS_ROOT(d))
17221+ continue;
17222+
17223+ au_digen_dec(d);
17224+ if (d_really_is_positive(d))
17225+ /* todo: reset children xino?
17226+ cached children only? */
17227+ au_iigen_dec(d_inode(d));
17228+ }
17229+ }
17230+
17231+out_dpages:
17232+ au_dpages_free(&dpages);
17233+out:
17234+ return err;
17235+}
17236+
17237+/*
17238+ * return 0 if processed.
17239+ */
17240+static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
17241+ const unsigned int isdir)
17242+{
17243+ int err;
17244+ struct dentry *d;
17245+ struct qstr *dname;
17246+
17247+ err = 1;
17248+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
17249+ pr_warn("branch root dir was changed\n");
17250+ err = 0;
17251+ goto out;
17252+ }
17253+
17254+ if (!isdir) {
17255+ AuDebugOn(!name);
17256+ au_iigen_dec(inode);
17257+ spin_lock(&inode->i_lock);
17258+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
17259+ spin_lock(&d->d_lock);
17260+ dname = &d->d_name;
17261+ if (dname->len != nlen
17262+ && memcmp(dname->name, name, nlen)) {
17263+ spin_unlock(&d->d_lock);
17264+ continue;
17265+ }
17266+ err = 0;
17267+ au_digen_dec(d);
17268+ spin_unlock(&d->d_lock);
17269+ break;
17270+ }
17271+ spin_unlock(&inode->i_lock);
17272+ } else {
17273+ au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
17274+ d = d_find_any_alias(inode);
17275+ if (!d) {
17276+ au_iigen_dec(inode);
17277+ goto out;
17278+ }
17279+
17280+ spin_lock(&d->d_lock);
17281+ dname = &d->d_name;
17282+ if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
17283+ spin_unlock(&d->d_lock);
17284+ err = hn_gen_tree(d);
17285+ spin_lock(&d->d_lock);
17286+ }
17287+ spin_unlock(&d->d_lock);
17288+ dput(d);
17289+ }
17290+
17291+out:
17292+ AuTraceErr(err);
17293+ return err;
17294+}
17295+
17296+static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
17297+{
17298+ int err;
17299+
17300+ if (IS_ROOT(dentry)) {
17301+ pr_warn("branch root dir was changed\n");
17302+ return 0;
17303+ }
17304+
17305+ err = 0;
17306+ if (!isdir) {
17307+ au_digen_dec(dentry);
17308+ if (d_really_is_positive(dentry))
17309+ au_iigen_dec(d_inode(dentry));
17310+ } else {
17311+ au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
17312+ if (d_really_is_positive(dentry))
17313+ err = hn_gen_tree(dentry);
17314+ }
17315+
17316+ AuTraceErr(err);
17317+ return err;
17318+}
17319+
17320+/* ---------------------------------------------------------------------- */
17321+
17322+/* hnotify job flags */
17323+#define AuHnJob_XINO0 1
17324+#define AuHnJob_GEN (1 << 1)
17325+#define AuHnJob_DIRENT (1 << 2)
17326+#define AuHnJob_ISDIR (1 << 3)
17327+#define AuHnJob_TRYXINO0 (1 << 4)
17328+#define AuHnJob_MNTPNT (1 << 5)
17329+#define au_ftest_hnjob(flags, name) ((flags) & AuHnJob_##name)
17330+#define au_fset_hnjob(flags, name) \
17331+ do { (flags) |= AuHnJob_##name; } while (0)
17332+#define au_fclr_hnjob(flags, name) \
17333+ do { (flags) &= ~AuHnJob_##name; } while (0)
17334+
17335+enum {
17336+ AuHn_CHILD,
17337+ AuHn_PARENT,
17338+ AuHnLast
17339+};
17340+
17341+struct au_hnotify_args {
17342+ struct inode *h_dir, *dir, *h_child_inode;
17343+ u32 mask;
17344+ unsigned int flags[AuHnLast];
17345+ unsigned int h_child_nlen;
17346+ char h_child_name[];
17347+};
17348+
17349+struct hn_job_args {
17350+ unsigned int flags;
17351+ struct inode *inode, *h_inode, *dir, *h_dir;
17352+ struct dentry *dentry;
17353+ char *h_name;
17354+ int h_nlen;
17355+};
17356+
17357+static int hn_job(struct hn_job_args *a)
17358+{
17359+ const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
17360+ int e;
17361+
17362+ /* reset xino */
17363+ if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
17364+ hn_xino(a->inode, a->h_inode); /* ignore this error */
17365+
17366+ if (au_ftest_hnjob(a->flags, TRYXINO0)
17367+ && a->inode
17368+ && a->h_inode) {
17369+ inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
17370+ if (!a->h_inode->i_nlink
17371+ && !(a->h_inode->i_state & I_LINKABLE))
17372+ hn_xino(a->inode, a->h_inode); /* ignore this error */
17373+ inode_unlock_shared(a->h_inode);
17374+ }
17375+
17376+ /* make the generation obsolete */
17377+ if (au_ftest_hnjob(a->flags, GEN)) {
17378+ e = -1;
17379+ if (a->inode)
17380+ e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
17381+ isdir);
17382+ if (e && a->dentry)
17383+ hn_gen_by_name(a->dentry, isdir);
17384+ /* ignore this error */
17385+ }
17386+
17387+ /* make dir entries obsolete */
17388+ if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
17389+ struct au_vdir *vdir;
17390+
17391+ vdir = au_ivdir(a->inode);
17392+ if (vdir)
17393+ vdir->vd_jiffy = 0;
17394+ /* IMustLock(a->inode); */
17395+ /* inode_inc_iversion(a->inode); */
17396+ }
17397+
17398+ /* can do nothing but warn */
17399+ if (au_ftest_hnjob(a->flags, MNTPNT)
17400+ && a->dentry
17401+ && d_mountpoint(a->dentry))
17402+ pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
17403+
17404+ return 0;
17405+}
17406+
17407+/* ---------------------------------------------------------------------- */
17408+
17409+static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
17410+ struct inode *dir)
17411+{
17412+ struct dentry *dentry, *d, *parent;
17413+ struct qstr *dname;
17414+
17415+ parent = d_find_any_alias(dir);
17416+ if (!parent)
17417+ return NULL;
17418+
17419+ dentry = NULL;
17420+ spin_lock(&parent->d_lock);
17421+ list_for_each_entry(d, &parent->d_subdirs, d_child) {
17422+ /* AuDbg("%pd\n", d); */
17423+ spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
17424+ dname = &d->d_name;
17425+ if (dname->len != nlen || memcmp(dname->name, name, nlen))
17426+ goto cont_unlock;
17427+ if (au_di(d))
17428+ au_digen_dec(d);
17429+ else
17430+ goto cont_unlock;
17431+ if (au_dcount(d) > 0) {
17432+ dentry = dget_dlock(d);
17433+ spin_unlock(&d->d_lock);
17434+ break;
17435+ }
17436+
17437+cont_unlock:
17438+ spin_unlock(&d->d_lock);
17439+ }
17440+ spin_unlock(&parent->d_lock);
17441+ dput(parent);
17442+
17443+ if (dentry)
17444+ di_write_lock_child(dentry);
17445+
17446+ return dentry;
17447+}
17448+
17449+static struct inode *lookup_wlock_by_ino(struct super_block *sb,
17450+ aufs_bindex_t bindex, ino_t h_ino)
17451+{
17452+ struct inode *inode;
17453+ ino_t ino;
17454+ int err;
17455+
17456+ inode = NULL;
17457+ err = au_xino_read(sb, bindex, h_ino, &ino);
17458+ if (!err && ino)
17459+ inode = ilookup(sb, ino);
17460+ if (!inode)
17461+ goto out;
17462+
17463+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
17464+ pr_warn("wrong root branch\n");
17465+ iput(inode);
17466+ inode = NULL;
17467+ goto out;
17468+ }
17469+
17470+ ii_write_lock_child(inode);
17471+
17472+out:
17473+ return inode;
17474+}
17475+
17476+static void au_hn_bh(void *_args)
17477+{
17478+ struct au_hnotify_args *a = _args;
17479+ struct super_block *sb;
17480+ aufs_bindex_t bindex, bbot, bfound;
17481+ unsigned char xino, try_iput;
17482+ int err;
17483+ struct inode *inode;
17484+ ino_t h_ino;
17485+ struct hn_job_args args;
17486+ struct dentry *dentry;
17487+ struct au_sbinfo *sbinfo;
17488+
17489+ AuDebugOn(!_args);
17490+ AuDebugOn(!a->h_dir);
17491+ AuDebugOn(!a->dir);
17492+ AuDebugOn(!a->mask);
17493+ AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
17494+ a->mask, a->dir->i_ino, a->h_dir->i_ino,
17495+ a->h_child_inode ? a->h_child_inode->i_ino : 0);
17496+
17497+ inode = NULL;
17498+ dentry = NULL;
17499+ /*
17500+ * do not lock a->dir->i_mutex here
17501+ * because of d_revalidate() may cause a deadlock.
17502+ */
17503+ sb = a->dir->i_sb;
17504+ AuDebugOn(!sb);
17505+ sbinfo = au_sbi(sb);
17506+ AuDebugOn(!sbinfo);
17507+ si_write_lock(sb, AuLock_NOPLMW);
17508+
17509+ if (au_opt_test(sbinfo->si_mntflags, DIRREN))
17510+ switch (a->mask & FS_EVENTS_POSS_ON_CHILD) {
17511+ case FS_MOVED_FROM:
17512+ case FS_MOVED_TO:
17513+ AuWarn1("DIRREN with UDBA may not work correctly "
17514+ "for the direct rename(2)\n");
17515+ }
17516+
17517+ ii_read_lock_parent(a->dir);
17518+ bfound = -1;
17519+ bbot = au_ibbot(a->dir);
17520+ for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
17521+ if (au_h_iptr(a->dir, bindex) == a->h_dir) {
17522+ bfound = bindex;
17523+ break;
17524+ }
17525+ ii_read_unlock(a->dir);
17526+ if (unlikely(bfound < 0))
17527+ goto out;
17528+
17529+ xino = !!au_opt_test(au_mntflags(sb), XINO);
17530+ h_ino = 0;
17531+ if (a->h_child_inode)
17532+ h_ino = a->h_child_inode->i_ino;
17533+
17534+ if (a->h_child_nlen
17535+ && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
17536+ || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
17537+ dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
17538+ a->dir);
17539+ try_iput = 0;
17540+ if (dentry && d_really_is_positive(dentry))
17541+ inode = d_inode(dentry);
17542+ if (xino && !inode && h_ino
17543+ && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
17544+ || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
17545+ || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
17546+ inode = lookup_wlock_by_ino(sb, bfound, h_ino);
17547+ try_iput = 1;
17548+ }
17549+
17550+ args.flags = a->flags[AuHn_CHILD];
17551+ args.dentry = dentry;
17552+ args.inode = inode;
17553+ args.h_inode = a->h_child_inode;
17554+ args.dir = a->dir;
17555+ args.h_dir = a->h_dir;
17556+ args.h_name = a->h_child_name;
17557+ args.h_nlen = a->h_child_nlen;
17558+ err = hn_job(&args);
17559+ if (dentry) {
17560+ if (au_di(dentry))
17561+ di_write_unlock(dentry);
17562+ dput(dentry);
17563+ }
17564+ if (inode && try_iput) {
17565+ ii_write_unlock(inode);
17566+ iput(inode);
17567+ }
17568+
17569+ ii_write_lock_parent(a->dir);
17570+ args.flags = a->flags[AuHn_PARENT];
17571+ args.dentry = NULL;
17572+ args.inode = a->dir;
17573+ args.h_inode = a->h_dir;
17574+ args.dir = NULL;
17575+ args.h_dir = NULL;
17576+ args.h_name = NULL;
17577+ args.h_nlen = 0;
17578+ err = hn_job(&args);
17579+ ii_write_unlock(a->dir);
17580+
17581+out:
17582+ iput(a->h_child_inode);
17583+ iput(a->h_dir);
17584+ iput(a->dir);
17585+ si_write_unlock(sb);
17586+ au_nwt_done(&sbinfo->si_nowait);
17587+ au_kfree_rcu(a);
17588+}
17589+
17590+/* ---------------------------------------------------------------------- */
17591+
17592+int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
17593+ const struct qstr *h_child_qstr, struct inode *h_child_inode)
17594+{
17595+ int err, len;
17596+ unsigned int flags[AuHnLast], f;
17597+ unsigned char isdir, isroot, wh;
17598+ struct inode *dir;
17599+ struct au_hnotify_args *args;
17600+ char *p, *h_child_name;
17601+
17602+ err = 0;
17603+ AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
17604+ dir = igrab(hnotify->hn_aufs_inode);
17605+ if (!dir)
17606+ goto out;
17607+
17608+ isroot = (dir->i_ino == AUFS_ROOT_INO);
17609+ wh = 0;
17610+ h_child_name = (void *)h_child_qstr->name;
17611+ len = h_child_qstr->len;
17612+ if (h_child_name) {
17613+ if (len > AUFS_WH_PFX_LEN
17614+ && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
17615+ h_child_name += AUFS_WH_PFX_LEN;
17616+ len -= AUFS_WH_PFX_LEN;
17617+ wh = 1;
17618+ }
17619+ }
17620+
17621+ isdir = 0;
17622+ if (h_child_inode)
17623+ isdir = !!S_ISDIR(h_child_inode->i_mode);
17624+ flags[AuHn_PARENT] = AuHnJob_ISDIR;
17625+ flags[AuHn_CHILD] = 0;
17626+ if (isdir)
17627+ flags[AuHn_CHILD] = AuHnJob_ISDIR;
17628+ au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
17629+ au_fset_hnjob(flags[AuHn_CHILD], GEN);
17630+ switch (mask & ALL_FSNOTIFY_DIRENT_EVENTS) {
17631+ case FS_MOVED_FROM:
17632+ case FS_MOVED_TO:
17633+ au_fset_hnjob(flags[AuHn_CHILD], XINO0);
17634+ au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17635+ fallthrough;
17636+ case FS_CREATE:
17637+ AuDebugOn(!h_child_name);
17638+ break;
17639+
17640+ case FS_DELETE:
17641+ /*
17642+ * aufs never be able to get this child inode.
17643+ * revalidation should be in d_revalidate()
17644+ * by checking i_nlink, i_generation or d_unhashed().
17645+ */
17646+ AuDebugOn(!h_child_name);
17647+ au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
17648+ au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17649+ break;
17650+
17651+ default:
17652+ AuDebugOn(1);
17653+ }
17654+
17655+ if (wh)
17656+ h_child_inode = NULL;
17657+
17658+ err = -ENOMEM;
17659+ /* iput() and kfree() will be called in au_hnotify() */
17660+ args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
17661+ if (unlikely(!args)) {
17662+ AuErr1("no memory\n");
17663+ iput(dir);
17664+ goto out;
17665+ }
17666+ args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
17667+ args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
17668+ args->mask = mask;
17669+ args->dir = dir;
17670+ args->h_dir = igrab(h_dir);
17671+ if (h_child_inode)
17672+ h_child_inode = igrab(h_child_inode); /* can be NULL */
17673+ args->h_child_inode = h_child_inode;
17674+ args->h_child_nlen = len;
17675+ if (len) {
17676+ p = (void *)args;
17677+ p += sizeof(*args);
17678+ memcpy(p, h_child_name, len);
17679+ p[len] = 0;
17680+ }
17681+
17682+ /* NFS fires the event for silly-renamed one from kworker */
17683+ f = 0;
17684+ if (!dir->i_nlink
17685+ || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
17686+ f = AuWkq_NEST;
17687+ err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
17688+ if (unlikely(err)) {
17689+ pr_err("wkq %d\n", err);
17690+ iput(args->h_child_inode);
17691+ iput(args->h_dir);
17692+ iput(args->dir);
17693+ au_kfree_rcu(args);
17694+ }
17695+
17696+out:
17697+ return err;
17698+}
17699+
17700+/* ---------------------------------------------------------------------- */
17701+
17702+int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
17703+{
17704+ int err;
17705+
17706+ AuDebugOn(!(udba & AuOptMask_UDBA));
17707+
17708+ err = 0;
17709+ if (au_hnotify_op.reset_br)
17710+ err = au_hnotify_op.reset_br(udba, br, perm);
17711+
17712+ return err;
17713+}
17714+
17715+int au_hnotify_init_br(struct au_branch *br, int perm)
17716+{
17717+ int err;
17718+
17719+ err = 0;
17720+ if (au_hnotify_op.init_br)
17721+ err = au_hnotify_op.init_br(br, perm);
17722+
17723+ return err;
17724+}
17725+
17726+void au_hnotify_fin_br(struct au_branch *br)
17727+{
17728+ if (au_hnotify_op.fin_br)
17729+ au_hnotify_op.fin_br(br);
17730+}
17731+
17732+static void au_hn_destroy_cache(void)
17733+{
17734+ kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
17735+ au_cache[AuCache_HNOTIFY] = NULL;
17736+}
17737+
17738+int __init au_hnotify_init(void)
17739+{
17740+ int err;
17741+
17742+ err = -ENOMEM;
17743+ au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
17744+ if (au_cache[AuCache_HNOTIFY]) {
17745+ err = 0;
17746+ if (au_hnotify_op.init)
17747+ err = au_hnotify_op.init();
17748+ if (unlikely(err))
17749+ au_hn_destroy_cache();
17750+ }
17751+ AuTraceErr(err);
17752+ return err;
17753+}
17754+
17755+void au_hnotify_fin(void)
17756+{
17757+ if (au_hnotify_op.fin)
17758+ au_hnotify_op.fin();
17759+
17760+ /* cf. au_cache_fin() */
17761+ if (au_cache[AuCache_HNOTIFY])
17762+ au_hn_destroy_cache();
17763+}
17764diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
17765--- /usr/share/empty/fs/aufs/iinfo.c 1970-01-01 01:00:00.000000000 +0100
17766+++ linux/fs/aufs/iinfo.c 2021-02-24 13:33:42.744347058 +0100
17767@@ -0,0 +1,286 @@
17768+// SPDX-License-Identifier: GPL-2.0
17769+/*
17770+ * Copyright (C) 2005-2020 Junjiro R. Okajima
17771+ *
17772+ * This program, aufs is free software; you can redistribute it and/or modify
17773+ * it under the terms of the GNU General Public License as published by
17774+ * the Free Software Foundation; either version 2 of the License, or
17775+ * (at your option) any later version.
17776+ *
17777+ * This program is distributed in the hope that it will be useful,
17778+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17779+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17780+ * GNU General Public License for more details.
17781+ *
17782+ * You should have received a copy of the GNU General Public License
17783+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
17784+ */
17785+
17786+/*
17787+ * inode private data
17788+ */
17789+
17790+#include "aufs.h"
17791+
17792+struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
17793+{
17794+ struct inode *h_inode;
17795+ struct au_hinode *hinode;
17796+
17797+ IiMustAnyLock(inode);
17798+
17799+ hinode = au_hinode(au_ii(inode), bindex);
17800+ h_inode = hinode->hi_inode;
17801+ AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
17802+ return h_inode;
17803+}
17804+
17805+/* todo: hard/soft set? */
17806+void au_hiput(struct au_hinode *hinode)
17807+{
17808+ au_hn_free(hinode);
17809+ dput(hinode->hi_whdentry);
17810+ iput(hinode->hi_inode);
17811+}
17812+
17813+unsigned int au_hi_flags(struct inode *inode, int isdir)
17814+{
17815+ unsigned int flags;
17816+ const unsigned int mnt_flags = au_mntflags(inode->i_sb);
17817+
17818+ flags = 0;
17819+ if (au_opt_test(mnt_flags, XINO))
17820+ au_fset_hi(flags, XINO);
17821+ if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
17822+ au_fset_hi(flags, HNOTIFY);
17823+ return flags;
17824+}
17825+
17826+void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
17827+ struct inode *h_inode, unsigned int flags)
17828+{
17829+ struct au_hinode *hinode;
17830+ struct inode *hi;
17831+ struct au_iinfo *iinfo = au_ii(inode);
17832+
17833+ IiMustWriteLock(inode);
17834+
17835+ hinode = au_hinode(iinfo, bindex);
17836+ hi = hinode->hi_inode;
17837+ AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
17838+
17839+ if (hi)
17840+ au_hiput(hinode);
17841+ hinode->hi_inode = h_inode;
17842+ if (h_inode) {
17843+ int err;
17844+ struct super_block *sb = inode->i_sb;
17845+ struct au_branch *br;
17846+
17847+ AuDebugOn(inode->i_mode
17848+ && (h_inode->i_mode & S_IFMT)
17849+ != (inode->i_mode & S_IFMT));
17850+ if (bindex == iinfo->ii_btop)
17851+ au_cpup_igen(inode, h_inode);
17852+ br = au_sbr(sb, bindex);
17853+ hinode->hi_id = br->br_id;
17854+ if (au_ftest_hi(flags, XINO)) {
17855+ err = au_xino_write(sb, bindex, h_inode->i_ino,
17856+ inode->i_ino);
17857+ if (unlikely(err))
17858+ AuIOErr1("failed au_xino_write() %d\n", err);
17859+ }
17860+
17861+ if (au_ftest_hi(flags, HNOTIFY)
17862+ && au_br_hnotifyable(br->br_perm)) {
17863+ err = au_hn_alloc(hinode, inode);
17864+ if (unlikely(err))
17865+ AuIOErr1("au_hn_alloc() %d\n", err);
17866+ }
17867+ }
17868+}
17869+
17870+void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
17871+ struct dentry *h_wh)
17872+{
17873+ struct au_hinode *hinode;
17874+
17875+ IiMustWriteLock(inode);
17876+
17877+ hinode = au_hinode(au_ii(inode), bindex);
17878+ AuDebugOn(hinode->hi_whdentry);
17879+ hinode->hi_whdentry = h_wh;
17880+}
17881+
17882+void au_update_iigen(struct inode *inode, int half)
17883+{
17884+ struct au_iinfo *iinfo;
17885+ struct au_iigen *iigen;
17886+ unsigned int sigen;
17887+
17888+ sigen = au_sigen(inode->i_sb);
17889+ iinfo = au_ii(inode);
17890+ iigen = &iinfo->ii_generation;
17891+ spin_lock(&iigen->ig_spin);
17892+ iigen->ig_generation = sigen;
17893+ if (half)
17894+ au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
17895+ else
17896+ au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
17897+ spin_unlock(&iigen->ig_spin);
17898+}
17899+
17900+/* it may be called at remount time, too */
17901+void au_update_ibrange(struct inode *inode, int do_put_zero)
17902+{
17903+ struct au_iinfo *iinfo;
17904+ aufs_bindex_t bindex, bbot;
17905+
17906+ AuDebugOn(au_is_bad_inode(inode));
17907+ IiMustWriteLock(inode);
17908+
17909+ iinfo = au_ii(inode);
17910+ if (do_put_zero && iinfo->ii_btop >= 0) {
17911+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
17912+ bindex++) {
17913+ struct inode *h_i;
17914+
17915+ h_i = au_hinode(iinfo, bindex)->hi_inode;
17916+ if (h_i
17917+ && !h_i->i_nlink
17918+ && !(h_i->i_state & I_LINKABLE))
17919+ au_set_h_iptr(inode, bindex, NULL, 0);
17920+ }
17921+ }
17922+
17923+ iinfo->ii_btop = -1;
17924+ iinfo->ii_bbot = -1;
17925+ bbot = au_sbbot(inode->i_sb);
17926+ for (bindex = 0; bindex <= bbot; bindex++)
17927+ if (au_hinode(iinfo, bindex)->hi_inode) {
17928+ iinfo->ii_btop = bindex;
17929+ break;
17930+ }
17931+ if (iinfo->ii_btop >= 0)
17932+ for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
17933+ if (au_hinode(iinfo, bindex)->hi_inode) {
17934+ iinfo->ii_bbot = bindex;
17935+ break;
17936+ }
17937+ AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
17938+}
17939+
17940+/* ---------------------------------------------------------------------- */
17941+
17942+void au_icntnr_init_once(void *_c)
17943+{
17944+ struct au_icntnr *c = _c;
17945+ struct au_iinfo *iinfo = &c->iinfo;
17946+
17947+ spin_lock_init(&iinfo->ii_generation.ig_spin);
17948+ au_rw_init(&iinfo->ii_rwsem);
17949+ inode_init_once(&c->vfs_inode);
17950+}
17951+
17952+void au_hinode_init(struct au_hinode *hinode)
17953+{
17954+ hinode->hi_inode = NULL;
17955+ hinode->hi_id = -1;
17956+ au_hn_init(hinode);
17957+ hinode->hi_whdentry = NULL;
17958+}
17959+
17960+int au_iinfo_init(struct inode *inode)
17961+{
17962+ struct au_iinfo *iinfo;
17963+ struct super_block *sb;
17964+ struct au_hinode *hi;
17965+ int nbr, i;
17966+
17967+ sb = inode->i_sb;
17968+ iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
17969+ nbr = au_sbbot(sb) + 1;
17970+ if (unlikely(nbr <= 0))
17971+ nbr = 1;
17972+ hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
17973+ if (hi) {
17974+ au_lcnt_inc(&au_sbi(sb)->si_ninodes);
17975+
17976+ iinfo->ii_hinode = hi;
17977+ for (i = 0; i < nbr; i++, hi++)
17978+ au_hinode_init(hi);
17979+
17980+ iinfo->ii_generation.ig_generation = au_sigen(sb);
17981+ iinfo->ii_btop = -1;
17982+ iinfo->ii_bbot = -1;
17983+ iinfo->ii_vdir = NULL;
17984+ return 0;
17985+ }
17986+ return -ENOMEM;
17987+}
17988+
17989+int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
17990+{
17991+ int err, i;
17992+ struct au_hinode *hip;
17993+
17994+ AuRwMustWriteLock(&iinfo->ii_rwsem);
17995+
17996+ err = -ENOMEM;
17997+ hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
17998+ may_shrink);
17999+ if (hip) {
18000+ iinfo->ii_hinode = hip;
18001+ i = iinfo->ii_bbot + 1;
18002+ hip += i;
18003+ for (; i < nbr; i++, hip++)
18004+ au_hinode_init(hip);
18005+ err = 0;
18006+ }
18007+
18008+ return err;
18009+}
18010+
18011+void au_iinfo_fin(struct inode *inode)
18012+{
18013+ struct au_iinfo *iinfo;
18014+ struct au_hinode *hi;
18015+ struct super_block *sb;
18016+ aufs_bindex_t bindex, bbot;
18017+ const unsigned char unlinked = !inode->i_nlink;
18018+
18019+ AuDebugOn(au_is_bad_inode(inode));
18020+
18021+ sb = inode->i_sb;
18022+ au_lcnt_dec(&au_sbi(sb)->si_ninodes);
18023+ if (si_pid_test(sb))
18024+ au_xino_delete_inode(inode, unlinked);
18025+ else {
18026+ /*
18027+ * it is safe to hide the dependency between sbinfo and
18028+ * sb->s_umount.
18029+ */
18030+ lockdep_off();
18031+ si_noflush_read_lock(sb);
18032+ au_xino_delete_inode(inode, unlinked);
18033+ si_read_unlock(sb);
18034+ lockdep_on();
18035+ }
18036+
18037+ iinfo = au_ii(inode);
18038+ if (iinfo->ii_vdir)
18039+ au_vdir_free(iinfo->ii_vdir);
18040+
18041+ bindex = iinfo->ii_btop;
18042+ if (bindex >= 0) {
18043+ hi = au_hinode(iinfo, bindex);
18044+ bbot = iinfo->ii_bbot;
18045+ while (bindex++ <= bbot) {
18046+ if (hi->hi_inode)
18047+ au_hiput(hi);
18048+ hi++;
18049+ }
18050+ }
18051+ au_kfree_rcu(iinfo->ii_hinode);
18052+ AuRwDestroy(&iinfo->ii_rwsem);
18053+}
18054diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
18055--- /usr/share/empty/fs/aufs/inode.c 1970-01-01 01:00:00.000000000 +0100
18056+++ linux/fs/aufs/inode.c 2021-06-30 21:35:11.397206648 +0200
18057@@ -0,0 +1,531 @@
18058+// SPDX-License-Identifier: GPL-2.0
18059+/*
18060+ * Copyright (C) 2005-2020 Junjiro R. Okajima
18061+ *
18062+ * This program, aufs is free software; you can redistribute it and/or modify
18063+ * it under the terms of the GNU General Public License as published by
18064+ * the Free Software Foundation; either version 2 of the License, or
18065+ * (at your option) any later version.
18066+ *
18067+ * This program is distributed in the hope that it will be useful,
18068+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18069+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18070+ * GNU General Public License for more details.
18071+ *
18072+ * You should have received a copy of the GNU General Public License
18073+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
18074+ */
18075+
18076+/*
18077+ * inode functions
18078+ */
18079+
18080+#include <linux/iversion.h>
18081+#include "aufs.h"
18082+
18083+struct inode *au_igrab(struct inode *inode)
18084+{
18085+ if (inode) {
18086+ AuDebugOn(!atomic_read(&inode->i_count));
18087+ ihold(inode);
18088+ }
18089+ return inode;
18090+}
18091+
18092+static void au_refresh_hinode_attr(struct inode *inode, int do_version)
18093+{
18094+ au_cpup_attr_all(inode, /*force*/0);
18095+ au_update_iigen(inode, /*half*/1);
18096+ if (do_version)
18097+ inode_inc_iversion(inode);
18098+}
18099+
18100+static int au_ii_refresh(struct inode *inode, int *update)
18101+{
18102+ int err, e, nbr;
18103+ umode_t type;
18104+ aufs_bindex_t bindex, new_bindex;
18105+ struct super_block *sb;
18106+ struct au_iinfo *iinfo;
18107+ struct au_hinode *p, *q, tmp;
18108+
18109+ AuDebugOn(au_is_bad_inode(inode));
18110+ IiMustWriteLock(inode);
18111+
18112+ *update = 0;
18113+ sb = inode->i_sb;
18114+ nbr = au_sbbot(sb) + 1;
18115+ type = inode->i_mode & S_IFMT;
18116+ iinfo = au_ii(inode);
18117+ err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
18118+ if (unlikely(err))
18119+ goto out;
18120+
18121+ AuDebugOn(iinfo->ii_btop < 0);
18122+ p = au_hinode(iinfo, iinfo->ii_btop);
18123+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
18124+ bindex++, p++) {
18125+ if (!p->hi_inode)
18126+ continue;
18127+
18128+ AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
18129+ new_bindex = au_br_index(sb, p->hi_id);
18130+ if (new_bindex == bindex)
18131+ continue;
18132+
18133+ if (new_bindex < 0) {
18134+ *update = 1;
18135+ au_hiput(p);
18136+ p->hi_inode = NULL;
18137+ continue;
18138+ }
18139+
18140+ if (new_bindex < iinfo->ii_btop)
18141+ iinfo->ii_btop = new_bindex;
18142+ if (iinfo->ii_bbot < new_bindex)
18143+ iinfo->ii_bbot = new_bindex;
18144+ /* swap two lower inode, and loop again */
18145+ q = au_hinode(iinfo, new_bindex);
18146+ tmp = *q;
18147+ *q = *p;
18148+ *p = tmp;
18149+ if (tmp.hi_inode) {
18150+ bindex--;
18151+ p--;
18152+ }
18153+ }
18154+ au_update_ibrange(inode, /*do_put_zero*/0);
18155+ au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
18156+ e = au_dy_irefresh(inode);
18157+ if (unlikely(e && !err))
18158+ err = e;
18159+
18160+out:
18161+ AuTraceErr(err);
18162+ return err;
18163+}
18164+
18165+void au_refresh_iop(struct inode *inode, int force_getattr)
18166+{
18167+ int type;
18168+ struct au_sbinfo *sbi = au_sbi(inode->i_sb);
18169+ const struct inode_operations *iop
18170+ = force_getattr ? aufs_iop : sbi->si_iop_array;
18171+
18172+ if (inode->i_op == iop)
18173+ return;
18174+
18175+ switch (inode->i_mode & S_IFMT) {
18176+ case S_IFDIR:
18177+ type = AuIop_DIR;
18178+ break;
18179+ case S_IFLNK:
18180+ type = AuIop_SYMLINK;
18181+ break;
18182+ default:
18183+ type = AuIop_OTHER;
18184+ break;
18185+ }
18186+
18187+ inode->i_op = iop + type;
18188+ /* unnecessary smp_wmb() */
18189+}
18190+
18191+int au_refresh_hinode_self(struct inode *inode)
18192+{
18193+ int err, update;
18194+
18195+ err = au_ii_refresh(inode, &update);
18196+ if (!err)
18197+ au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
18198+
18199+ AuTraceErr(err);
18200+ return err;
18201+}
18202+
18203+int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
18204+{
18205+ int err, e, update;
18206+ unsigned int flags;
18207+ umode_t mode;
18208+ aufs_bindex_t bindex, bbot;
18209+ unsigned char isdir;
18210+ struct au_hinode *p;
18211+ struct au_iinfo *iinfo;
18212+
18213+ err = au_ii_refresh(inode, &update);
18214+ if (unlikely(err))
18215+ goto out;
18216+
18217+ update = 0;
18218+ iinfo = au_ii(inode);
18219+ p = au_hinode(iinfo, iinfo->ii_btop);
18220+ mode = (inode->i_mode & S_IFMT);
18221+ isdir = S_ISDIR(mode);
18222+ flags = au_hi_flags(inode, isdir);
18223+ bbot = au_dbbot(dentry);
18224+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
18225+ struct inode *h_i, *h_inode;
18226+ struct dentry *h_d;
18227+
18228+ h_d = au_h_dptr(dentry, bindex);
18229+ if (!h_d || d_is_negative(h_d))
18230+ continue;
18231+
18232+ h_inode = d_inode(h_d);
18233+ AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
18234+ if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
18235+ h_i = au_h_iptr(inode, bindex);
18236+ if (h_i) {
18237+ if (h_i == h_inode)
18238+ continue;
18239+ err = -EIO;
18240+ break;
18241+ }
18242+ }
18243+ if (bindex < iinfo->ii_btop)
18244+ iinfo->ii_btop = bindex;
18245+ if (iinfo->ii_bbot < bindex)
18246+ iinfo->ii_bbot = bindex;
18247+ au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
18248+ update = 1;
18249+ }
18250+ au_update_ibrange(inode, /*do_put_zero*/0);
18251+ e = au_dy_irefresh(inode);
18252+ if (unlikely(e && !err))
18253+ err = e;
18254+ if (!err)
18255+ au_refresh_hinode_attr(inode, update && isdir);
18256+
18257+out:
18258+ AuTraceErr(err);
18259+ return err;
18260+}
18261+
18262+static int set_inode(struct inode *inode, struct dentry *dentry)
18263+{
18264+ int err;
18265+ unsigned int flags;
18266+ umode_t mode;
18267+ aufs_bindex_t bindex, btop, btail;
18268+ unsigned char isdir;
18269+ struct dentry *h_dentry;
18270+ struct inode *h_inode;
18271+ struct au_iinfo *iinfo;
18272+ const struct inode_operations *iop;
18273+
18274+ IiMustWriteLock(inode);
18275+
18276+ err = 0;
18277+ isdir = 0;
18278+ iop = au_sbi(inode->i_sb)->si_iop_array;
18279+ btop = au_dbtop(dentry);
18280+ h_dentry = au_h_dptr(dentry, btop);
18281+ h_inode = d_inode(h_dentry);
18282+ mode = h_inode->i_mode;
18283+ switch (mode & S_IFMT) {
18284+ case S_IFREG:
18285+ btail = au_dbtail(dentry);
18286+ inode->i_op = iop + AuIop_OTHER;
18287+ inode->i_fop = &aufs_file_fop;
18288+ err = au_dy_iaop(inode, btop, h_inode);
18289+ if (unlikely(err))
18290+ goto out;
18291+ break;
18292+ case S_IFDIR:
18293+ isdir = 1;
18294+ btail = au_dbtaildir(dentry);
18295+ inode->i_op = iop + AuIop_DIR;
18296+ inode->i_fop = &aufs_dir_fop;
18297+ break;
18298+ case S_IFLNK:
18299+ btail = au_dbtail(dentry);
18300+ inode->i_op = iop + AuIop_SYMLINK;
18301+ break;
18302+ case S_IFBLK:
18303+ case S_IFCHR:
18304+ case S_IFIFO:
18305+ case S_IFSOCK:
18306+ btail = au_dbtail(dentry);
18307+ inode->i_op = iop + AuIop_OTHER;
18308+ init_special_inode(inode, mode, h_inode->i_rdev);
18309+ break;
18310+ default:
18311+ AuIOErr("Unknown file type 0%o\n", mode);
18312+ err = -EIO;
18313+ goto out;
18314+ }
18315+
18316+ /* do not set hnotify for whiteouted dirs (SHWH mode) */
18317+ flags = au_hi_flags(inode, isdir);
18318+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
18319+ && au_ftest_hi(flags, HNOTIFY)
18320+ && dentry->d_name.len > AUFS_WH_PFX_LEN
18321+ && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
18322+ au_fclr_hi(flags, HNOTIFY);
18323+ iinfo = au_ii(inode);
18324+ iinfo->ii_btop = btop;
18325+ iinfo->ii_bbot = btail;
18326+ for (bindex = btop; bindex <= btail; bindex++) {
18327+ h_dentry = au_h_dptr(dentry, bindex);
18328+ if (h_dentry)
18329+ au_set_h_iptr(inode, bindex,
18330+ au_igrab(d_inode(h_dentry)), flags);
18331+ }
18332+ au_cpup_attr_all(inode, /*force*/1);
18333+ /*
18334+ * to force calling aufs_get_acl() every time,
18335+ * do not call cache_no_acl() for aufs inode.
18336+ */
18337+
18338+out:
18339+ return err;
18340+}
18341+
18342+/*
18343+ * successful returns with iinfo write_locked
18344+ * minus: errno
18345+ * zero: success, matched
18346+ * plus: no error, but unmatched
18347+ */
18348+static int reval_inode(struct inode *inode, struct dentry *dentry)
18349+{
18350+ int err;
18351+ unsigned int gen, igflags;
18352+ aufs_bindex_t bindex, bbot;
18353+ struct inode *h_inode, *h_dinode;
18354+ struct dentry *h_dentry;
18355+
18356+ /*
18357+ * before this function, if aufs got any iinfo lock, it must be only
18358+ * one, the parent dir.
18359+ * it can happen by UDBA and the obsoleted inode number.
18360+ */
18361+ err = -EIO;
18362+ if (unlikely(inode->i_ino == parent_ino(dentry)))
18363+ goto out;
18364+
18365+ err = 1;
18366+ ii_write_lock_new_child(inode);
18367+ h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
18368+ h_dinode = d_inode(h_dentry);
18369+ bbot = au_ibbot(inode);
18370+ for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
18371+ h_inode = au_h_iptr(inode, bindex);
18372+ if (!h_inode || h_inode != h_dinode)
18373+ continue;
18374+
18375+ err = 0;
18376+ gen = au_iigen(inode, &igflags);
18377+ if (gen == au_digen(dentry)
18378+ && !au_ig_ftest(igflags, HALF_REFRESHED))
18379+ break;
18380+
18381+ /* fully refresh inode using dentry */
18382+ err = au_refresh_hinode(inode, dentry);
18383+ if (!err)
18384+ au_update_iigen(inode, /*half*/0);
18385+ break;
18386+ }
18387+
18388+ if (unlikely(err))
18389+ ii_write_unlock(inode);
18390+out:
18391+ return err;
18392+}
18393+
18394+int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18395+ unsigned int d_type, ino_t *ino)
18396+{
18397+ int err, idx;
18398+ const int isnondir = d_type != DT_DIR;
18399+
18400+ /* prevent hardlinked inode number from race condition */
18401+ if (isnondir) {
18402+ err = au_xinondir_enter(sb, bindex, h_ino, &idx);
18403+ if (unlikely(err))
18404+ goto out;
18405+ }
18406+
18407+ err = au_xino_read(sb, bindex, h_ino, ino);
18408+ if (unlikely(err))
18409+ goto out_xinondir;
18410+
18411+ if (!*ino) {
18412+ err = -EIO;
18413+ *ino = au_xino_new_ino(sb);
18414+ if (unlikely(!*ino))
18415+ goto out_xinondir;
18416+ err = au_xino_write(sb, bindex, h_ino, *ino);
18417+ if (unlikely(err))
18418+ goto out_xinondir;
18419+ }
18420+
18421+out_xinondir:
18422+ if (isnondir && idx >= 0)
18423+ au_xinondir_leave(sb, bindex, h_ino, idx);
18424+out:
18425+ return err;
18426+}
18427+
18428+/* successful returns with iinfo write_locked */
18429+/* todo: return with unlocked? */
18430+struct inode *au_new_inode(struct dentry *dentry, int must_new)
18431+{
18432+ struct inode *inode, *h_inode;
18433+ struct dentry *h_dentry;
18434+ struct super_block *sb;
18435+ ino_t h_ino, ino;
18436+ int err, idx, hlinked;
18437+ aufs_bindex_t btop;
18438+
18439+ sb = dentry->d_sb;
18440+ btop = au_dbtop(dentry);
18441+ h_dentry = au_h_dptr(dentry, btop);
18442+ h_inode = d_inode(h_dentry);
18443+ h_ino = h_inode->i_ino;
18444+ hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
18445+
18446+new_ino:
18447+ /*
18448+ * stop 'race'-ing between hardlinks under different
18449+ * parents.
18450+ */
18451+ if (hlinked) {
18452+ err = au_xinondir_enter(sb, btop, h_ino, &idx);
18453+ inode = ERR_PTR(err);
18454+ if (unlikely(err))
18455+ goto out;
18456+ }
18457+
18458+ err = au_xino_read(sb, btop, h_ino, &ino);
18459+ inode = ERR_PTR(err);
18460+ if (unlikely(err))
18461+ goto out_xinondir;
18462+
18463+ if (!ino) {
18464+ ino = au_xino_new_ino(sb);
18465+ if (unlikely(!ino)) {
18466+ inode = ERR_PTR(-EIO);
18467+ goto out_xinondir;
18468+ }
18469+ }
18470+
18471+ AuDbg("i%lu\n", (unsigned long)ino);
18472+ inode = au_iget_locked(sb, ino);
18473+ err = PTR_ERR(inode);
18474+ if (IS_ERR(inode))
18475+ goto out_xinondir;
18476+
18477+ AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
18478+ if (inode->i_state & I_NEW) {
18479+ ii_write_lock_new_child(inode);
18480+ err = set_inode(inode, dentry);
18481+ if (!err) {
18482+ unlock_new_inode(inode);
18483+ goto out_xinondir; /* success */
18484+ }
18485+
18486+ /*
18487+ * iget_failed() calls iput(), but we need to call
18488+ * ii_write_unlock() after iget_failed(). so dirty hack for
18489+ * i_count.
18490+ */
18491+ atomic_inc(&inode->i_count);
18492+ iget_failed(inode);
18493+ ii_write_unlock(inode);
18494+ au_xino_write(sb, btop, h_ino, /*ino*/0);
18495+ /* ignore this error */
18496+ goto out_iput;
18497+ } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
18498+ /*
18499+ * horrible race condition between lookup, readdir and copyup
18500+ * (or something).
18501+ */
18502+ if (hlinked && idx >= 0)
18503+ au_xinondir_leave(sb, btop, h_ino, idx);
18504+ err = reval_inode(inode, dentry);
18505+ if (unlikely(err < 0)) {
18506+ hlinked = 0;
18507+ goto out_iput;
18508+ }
18509+ if (!err)
18510+ goto out; /* success */
18511+ else if (hlinked && idx >= 0) {
18512+ err = au_xinondir_enter(sb, btop, h_ino, &idx);
18513+ if (unlikely(err)) {
18514+ iput(inode);
18515+ inode = ERR_PTR(err);
18516+ goto out;
18517+ }
18518+ }
18519+ }
18520+
18521+ if (unlikely(au_test_fs_unique_ino(h_inode)))
18522+ AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
18523+ " b%d, %s, %pd, hi%lu, i%lu.\n",
18524+ btop, au_sbtype(h_dentry->d_sb), dentry,
18525+ (unsigned long)h_ino, (unsigned long)ino);
18526+ ino = 0;
18527+ err = au_xino_write(sb, btop, h_ino, /*ino*/0);
18528+ if (!err) {
18529+ iput(inode);
18530+ if (hlinked && idx >= 0)
18531+ au_xinondir_leave(sb, btop, h_ino, idx);
18532+ goto new_ino;
18533+ }
18534+
18535+out_iput:
18536+ iput(inode);
18537+ inode = ERR_PTR(err);
18538+out_xinondir:
18539+ if (hlinked && idx >= 0)
18540+ au_xinondir_leave(sb, btop, h_ino, idx);
18541+out:
18542+ return inode;
18543+}
18544+
18545+/* ---------------------------------------------------------------------- */
18546+
18547+int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18548+ struct inode *inode)
18549+{
18550+ int err;
18551+ struct inode *hi;
18552+
18553+ err = au_br_rdonly(au_sbr(sb, bindex));
18554+
18555+ /* pseudo-link after flushed may happen out of bounds */
18556+ if (!err
18557+ && inode
18558+ && au_ibtop(inode) <= bindex
18559+ && bindex <= au_ibbot(inode)) {
18560+ /*
18561+ * permission check is unnecessary since vfsub routine
18562+ * will be called later
18563+ */
18564+ hi = au_h_iptr(inode, bindex);
18565+ if (hi)
18566+ err = IS_IMMUTABLE(hi) ? -EROFS : 0;
18567+ }
18568+
18569+ return err;
18570+}
18571+
18572+int au_test_h_perm(struct user_namespace *h_userns, struct inode *h_inode,
18573+ int mask)
18574+{
18575+ if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
18576+ return 0;
18577+ return inode_permission(h_userns, h_inode, mask);
18578+}
18579+
18580+int au_test_h_perm_sio(struct user_namespace *h_userns, struct inode *h_inode,
18581+ int mask)
18582+{
18583+ if (au_test_nfs(h_inode->i_sb)
18584+ && (mask & MAY_WRITE)
18585+ && S_ISDIR(h_inode->i_mode))
18586+ mask |= MAY_READ; /* force permission check */
18587+ return au_test_h_perm(h_userns, h_inode, mask);
18588+}
18589diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
18590--- /usr/share/empty/fs/aufs/inode.h 1970-01-01 01:00:00.000000000 +0100
18591+++ linux/fs/aufs/inode.h 2021-06-30 21:35:11.397206648 +0200
18592@@ -0,0 +1,705 @@
18593+/* SPDX-License-Identifier: GPL-2.0 */
18594+/*
18595+ * Copyright (C) 2005-2020 Junjiro R. Okajima
18596+ *
18597+ * This program, aufs is free software; you can redistribute it and/or modify
18598+ * it under the terms of the GNU General Public License as published by
18599+ * the Free Software Foundation; either version 2 of the License, or
18600+ * (at your option) any later version.
18601+ *
18602+ * This program is distributed in the hope that it will be useful,
18603+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18604+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18605+ * GNU General Public License for more details.
18606+ *
18607+ * You should have received a copy of the GNU General Public License
18608+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
18609+ */
18610+
18611+/*
18612+ * inode operations
18613+ */
18614+
18615+#ifndef __AUFS_INODE_H__
18616+#define __AUFS_INODE_H__
18617+
18618+#ifdef __KERNEL__
18619+
18620+#include <linux/fsnotify.h>
18621+#include "rwsem.h"
18622+
18623+struct vfsmount;
18624+
18625+struct au_hnotify {
18626+#ifdef CONFIG_AUFS_HNOTIFY
18627+#ifdef CONFIG_AUFS_HFSNOTIFY
18628+ /* never use fsnotify_add_vfsmount_mark() */
18629+ struct fsnotify_mark hn_mark;
18630+#endif
18631+ struct inode *hn_aufs_inode; /* no get/put */
18632+ struct rcu_head rcu;
18633+#endif
18634+} ____cacheline_aligned_in_smp;
18635+
18636+struct au_hinode {
18637+ struct inode *hi_inode;
18638+ aufs_bindex_t hi_id;
18639+#ifdef CONFIG_AUFS_HNOTIFY
18640+ struct au_hnotify *hi_notify;
18641+#endif
18642+
18643+ /* reference to the copied-up whiteout with get/put */
18644+ struct dentry *hi_whdentry;
18645+};
18646+
18647+/* ig_flags */
18648+#define AuIG_HALF_REFRESHED 1
18649+#define au_ig_ftest(flags, name) ((flags) & AuIG_##name)
18650+#define au_ig_fset(flags, name) \
18651+ do { (flags) |= AuIG_##name; } while (0)
18652+#define au_ig_fclr(flags, name) \
18653+ do { (flags) &= ~AuIG_##name; } while (0)
18654+
18655+struct au_iigen {
18656+ spinlock_t ig_spin;
18657+ __u32 ig_generation, ig_flags;
18658+};
18659+
18660+struct au_vdir;
18661+struct au_iinfo {
18662+ struct au_iigen ii_generation;
18663+ struct super_block *ii_hsb1; /* no get/put */
18664+
18665+ struct au_rwsem ii_rwsem;
18666+ aufs_bindex_t ii_btop, ii_bbot;
18667+ __u32 ii_higen;
18668+ struct au_hinode *ii_hinode;
18669+ struct au_vdir *ii_vdir;
18670+};
18671+
18672+struct au_icntnr {
18673+ struct au_iinfo iinfo;
18674+ struct inode vfs_inode;
18675+ struct hlist_bl_node plink;
18676+ struct rcu_head rcu;
18677+} ____cacheline_aligned_in_smp;
18678+
18679+/* au_pin flags */
18680+#define AuPin_DI_LOCKED 1
18681+#define AuPin_MNT_WRITE (1 << 1)
18682+#define au_ftest_pin(flags, name) ((flags) & AuPin_##name)
18683+#define au_fset_pin(flags, name) \
18684+ do { (flags) |= AuPin_##name; } while (0)
18685+#define au_fclr_pin(flags, name) \
18686+ do { (flags) &= ~AuPin_##name; } while (0)
18687+
18688+struct au_pin {
18689+ /* input */
18690+ struct dentry *dentry;
18691+ unsigned int udba;
18692+ unsigned char lsc_di, lsc_hi, flags;
18693+ aufs_bindex_t bindex;
18694+
18695+ /* output */
18696+ struct dentry *parent;
18697+ struct au_hinode *hdir;
18698+ struct vfsmount *h_mnt;
18699+
18700+ /* temporary unlock/relock for copyup */
18701+ struct dentry *h_dentry, *h_parent;
18702+ struct au_branch *br;
18703+ struct task_struct *task;
18704+};
18705+
18706+void au_pin_hdir_unlock(struct au_pin *p);
18707+int au_pin_hdir_lock(struct au_pin *p);
18708+int au_pin_hdir_relock(struct au_pin *p);
18709+void au_pin_hdir_acquire_nest(struct au_pin *p);
18710+void au_pin_hdir_release(struct au_pin *p);
18711+
18712+/* ---------------------------------------------------------------------- */
18713+
18714+static inline struct au_iinfo *au_ii(struct inode *inode)
18715+{
18716+ BUG_ON(is_bad_inode(inode));
18717+ return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
18718+}
18719+
18720+/* ---------------------------------------------------------------------- */
18721+
18722+/* inode.c */
18723+struct inode *au_igrab(struct inode *inode);
18724+void au_refresh_iop(struct inode *inode, int force_getattr);
18725+int au_refresh_hinode_self(struct inode *inode);
18726+int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
18727+int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18728+ unsigned int d_type, ino_t *ino);
18729+struct inode *au_new_inode(struct dentry *dentry, int must_new);
18730+int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18731+ struct inode *inode);
18732+int au_test_h_perm(struct user_namespace *h_userns, struct inode *h_inode,
18733+ int mask);
18734+int au_test_h_perm_sio(struct user_namespace *h_userns, struct inode *h_inode,
18735+ int mask);
18736+
18737+static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
18738+ ino_t h_ino, unsigned int d_type, ino_t *ino)
18739+{
18740+#ifdef CONFIG_AUFS_SHWH
18741+ return au_ino(sb, bindex, h_ino, d_type, ino);
18742+#else
18743+ return 0;
18744+#endif
18745+}
18746+
18747+/* i_op.c */
18748+enum {
18749+ AuIop_SYMLINK,
18750+ AuIop_DIR,
18751+ AuIop_OTHER,
18752+ AuIop_Last
18753+};
18754+extern struct inode_operations aufs_iop[AuIop_Last], /* not const */
18755+ aufs_iop_nogetattr[AuIop_Last];
18756+
18757+/* au_wr_dir flags */
18758+#define AuWrDir_ADD_ENTRY 1
18759+#define AuWrDir_ISDIR (1 << 1)
18760+#define AuWrDir_TMPFILE (1 << 2)
18761+#define au_ftest_wrdir(flags, name) ((flags) & AuWrDir_##name)
18762+#define au_fset_wrdir(flags, name) \
18763+ do { (flags) |= AuWrDir_##name; } while (0)
18764+#define au_fclr_wrdir(flags, name) \
18765+ do { (flags) &= ~AuWrDir_##name; } while (0)
18766+
18767+struct au_wr_dir_args {
18768+ aufs_bindex_t force_btgt;
18769+ unsigned char flags;
18770+};
18771+int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
18772+ struct au_wr_dir_args *args);
18773+
18774+struct dentry *au_pinned_h_parent(struct au_pin *pin);
18775+void au_pin_init(struct au_pin *pin, struct dentry *dentry,
18776+ aufs_bindex_t bindex, int lsc_di, int lsc_hi,
18777+ unsigned int udba, unsigned char flags);
18778+int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
18779+ unsigned int udba, unsigned char flags) __must_check;
18780+int au_do_pin(struct au_pin *pin) __must_check;
18781+void au_unpin(struct au_pin *pin);
18782+int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
18783+
18784+#define AuIcpup_DID_CPUP 1
18785+#define au_ftest_icpup(flags, name) ((flags) & AuIcpup_##name)
18786+#define au_fset_icpup(flags, name) \
18787+ do { (flags) |= AuIcpup_##name; } while (0)
18788+#define au_fclr_icpup(flags, name) \
18789+ do { (flags) &= ~AuIcpup_##name; } while (0)
18790+
18791+struct au_icpup_args {
18792+ unsigned char flags;
18793+ unsigned char pin_flags;
18794+ aufs_bindex_t btgt;
18795+ unsigned int udba;
18796+ struct au_pin pin;
18797+ struct path h_path;
18798+ struct inode *h_inode;
18799+};
18800+
18801+int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
18802+ struct au_icpup_args *a);
18803+
18804+int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
18805+ struct path *h_path, int locked);
18806+
18807+/* i_op_add.c */
18808+int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
18809+ struct dentry *h_parent, int isdir);
18810+int aufs_mknod(struct user_namespace *userns, struct inode *dir,
18811+ struct dentry *dentry, umode_t mode, dev_t dev);
18812+int aufs_symlink(struct user_namespace *userns, struct inode *dir,
18813+ struct dentry *dentry, const char *symname);
18814+int aufs_create(struct user_namespace *userns, struct inode *dir,
18815+ struct dentry *dentry, umode_t mode, bool want_excl);
18816+struct vfsub_aopen_args;
18817+int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
18818+ struct vfsub_aopen_args *args);
18819+int aufs_tmpfile(struct user_namespace *userns, struct inode *dir,
18820+ struct dentry *dentry, umode_t mode);
18821+int aufs_link(struct dentry *src_dentry, struct inode *dir,
18822+ struct dentry *dentry);
18823+int aufs_mkdir(struct user_namespace *userns, struct inode *dir,
18824+ struct dentry *dentry, umode_t mode);
18825+
18826+/* i_op_del.c */
18827+int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
18828+int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
18829+ struct dentry *h_parent, int isdir);
18830+int aufs_unlink(struct inode *dir, struct dentry *dentry);
18831+int aufs_rmdir(struct inode *dir, struct dentry *dentry);
18832+
18833+/* i_op_ren.c */
18834+int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
18835+int aufs_rename(struct user_namespace *userns,
18836+ struct inode *_src_dir, struct dentry *_src_dentry,
18837+ struct inode *_dst_dir, struct dentry *_dst_dentry,
18838+ unsigned int _flags);
18839+
18840+/* iinfo.c */
18841+struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
18842+void au_hiput(struct au_hinode *hinode);
18843+void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
18844+ struct dentry *h_wh);
18845+unsigned int au_hi_flags(struct inode *inode, int isdir);
18846+
18847+/* hinode flags */
18848+#define AuHi_XINO 1
18849+#define AuHi_HNOTIFY (1 << 1)
18850+#define au_ftest_hi(flags, name) ((flags) & AuHi_##name)
18851+#define au_fset_hi(flags, name) \
18852+ do { (flags) |= AuHi_##name; } while (0)
18853+#define au_fclr_hi(flags, name) \
18854+ do { (flags) &= ~AuHi_##name; } while (0)
18855+
18856+#ifndef CONFIG_AUFS_HNOTIFY
18857+#undef AuHi_HNOTIFY
18858+#define AuHi_HNOTIFY 0
18859+#endif
18860+
18861+void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
18862+ struct inode *h_inode, unsigned int flags);
18863+
18864+void au_update_iigen(struct inode *inode, int half);
18865+void au_update_ibrange(struct inode *inode, int do_put_zero);
18866+
18867+void au_icntnr_init_once(void *_c);
18868+void au_hinode_init(struct au_hinode *hinode);
18869+int au_iinfo_init(struct inode *inode);
18870+void au_iinfo_fin(struct inode *inode);
18871+int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
18872+
18873+#ifdef CONFIG_PROC_FS
18874+/* plink.c */
18875+int au_plink_maint(struct super_block *sb, int flags);
18876+struct au_sbinfo;
18877+void au_plink_maint_leave(struct au_sbinfo *sbinfo);
18878+int au_plink_maint_enter(struct super_block *sb);
18879+#ifdef CONFIG_AUFS_DEBUG
18880+void au_plink_list(struct super_block *sb);
18881+#else
18882+AuStubVoid(au_plink_list, struct super_block *sb)
18883+#endif
18884+int au_plink_test(struct inode *inode);
18885+struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
18886+void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
18887+ struct dentry *h_dentry);
18888+void au_plink_put(struct super_block *sb, int verbose);
18889+void au_plink_clean(struct super_block *sb, int verbose);
18890+void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
18891+#else
18892+AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
18893+AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
18894+AuStubInt0(au_plink_maint_enter, struct super_block *sb);
18895+AuStubVoid(au_plink_list, struct super_block *sb);
18896+AuStubInt0(au_plink_test, struct inode *inode);
18897+AuStub(struct dentry *, au_plink_lkup, return NULL,
18898+ struct inode *inode, aufs_bindex_t bindex);
18899+AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
18900+ struct dentry *h_dentry);
18901+AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
18902+AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
18903+AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
18904+#endif /* CONFIG_PROC_FS */
18905+
18906+#ifdef CONFIG_AUFS_XATTR
18907+/* xattr.c */
18908+int au_cpup_xattr(struct path *h_dst, struct path *h_src, int ignore_flags,
18909+ unsigned int verbose);
18910+ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
18911+void au_xattr_init(struct super_block *sb);
18912+#else
18913+AuStubInt0(au_cpup_xattr, struct path *h_dst, struct path *h_src,
18914+ int ignore_flags, unsigned int verbose);
18915+AuStubVoid(au_xattr_init, struct super_block *sb);
18916+#endif
18917+
18918+#ifdef CONFIG_FS_POSIX_ACL
18919+struct posix_acl *aufs_get_acl(struct inode *inode, int type);
18920+int aufs_set_acl(struct user_namespace *userns, struct inode *inode,
18921+ struct posix_acl *acl, int type);
18922+#endif
18923+
18924+#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
18925+enum {
18926+ AU_XATTR_SET,
18927+ AU_ACL_SET
18928+};
18929+
18930+struct au_sxattr {
18931+ int type;
18932+ union {
18933+ struct {
18934+ const char *name;
18935+ const void *value;
18936+ size_t size;
18937+ int flags;
18938+ } set;
18939+ struct {
18940+ struct posix_acl *acl;
18941+ int type;
18942+ } acl_set;
18943+ } u;
18944+};
18945+ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
18946+ struct au_sxattr *arg);
18947+#endif
18948+
18949+/* ---------------------------------------------------------------------- */
18950+
18951+/* lock subclass for iinfo */
18952+enum {
18953+ AuLsc_II_CHILD, /* child first */
18954+ AuLsc_II_CHILD2, /* rename(2), link(2), and cpup at hnotify */
18955+ AuLsc_II_CHILD3, /* copyup dirs */
18956+ AuLsc_II_PARENT, /* see AuLsc_I_PARENT in vfsub.h */
18957+ AuLsc_II_PARENT2,
18958+ AuLsc_II_PARENT3, /* copyup dirs */
18959+ AuLsc_II_NEW_CHILD
18960+};
18961+
18962+/*
18963+ * ii_read_lock_child, ii_write_lock_child,
18964+ * ii_read_lock_child2, ii_write_lock_child2,
18965+ * ii_read_lock_child3, ii_write_lock_child3,
18966+ * ii_read_lock_parent, ii_write_lock_parent,
18967+ * ii_read_lock_parent2, ii_write_lock_parent2,
18968+ * ii_read_lock_parent3, ii_write_lock_parent3,
18969+ * ii_read_lock_new_child, ii_write_lock_new_child,
18970+ */
18971+#define AuReadLockFunc(name, lsc) \
18972+static inline void ii_read_lock_##name(struct inode *i) \
18973+{ \
18974+ au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
18975+}
18976+
18977+#define AuWriteLockFunc(name, lsc) \
18978+static inline void ii_write_lock_##name(struct inode *i) \
18979+{ \
18980+ au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
18981+}
18982+
18983+#define AuRWLockFuncs(name, lsc) \
18984+ AuReadLockFunc(name, lsc) \
18985+ AuWriteLockFunc(name, lsc)
18986+
18987+AuRWLockFuncs(child, CHILD);
18988+AuRWLockFuncs(child2, CHILD2);
18989+AuRWLockFuncs(child3, CHILD3);
18990+AuRWLockFuncs(parent, PARENT);
18991+AuRWLockFuncs(parent2, PARENT2);
18992+AuRWLockFuncs(parent3, PARENT3);
18993+AuRWLockFuncs(new_child, NEW_CHILD);
18994+
18995+#undef AuReadLockFunc
18996+#undef AuWriteLockFunc
18997+#undef AuRWLockFuncs
18998+
18999+#define ii_read_unlock(i) au_rw_read_unlock(&au_ii(i)->ii_rwsem)
19000+#define ii_write_unlock(i) au_rw_write_unlock(&au_ii(i)->ii_rwsem)
19001+#define ii_downgrade_lock(i) au_rw_dgrade_lock(&au_ii(i)->ii_rwsem)
19002+
19003+#define IiMustNoWaiters(i) AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
19004+#define IiMustAnyLock(i) AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
19005+#define IiMustWriteLock(i) AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
19006+
19007+/* ---------------------------------------------------------------------- */
19008+
19009+static inline void au_icntnr_init(struct au_icntnr *c)
19010+{
19011+#ifdef CONFIG_AUFS_DEBUG
19012+ c->vfs_inode.i_mode = 0;
19013+#endif
19014+}
19015+
19016+static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
19017+{
19018+ unsigned int gen;
19019+ struct au_iinfo *iinfo;
19020+ struct au_iigen *iigen;
19021+
19022+ iinfo = au_ii(inode);
19023+ iigen = &iinfo->ii_generation;
19024+ spin_lock(&iigen->ig_spin);
19025+ if (igflags)
19026+ *igflags = iigen->ig_flags;
19027+ gen = iigen->ig_generation;
19028+ spin_unlock(&iigen->ig_spin);
19029+
19030+ return gen;
19031+}
19032+
19033+/* tiny test for inode number */
19034+/* tmpfs generation is too rough */
19035+static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
19036+{
19037+ struct au_iinfo *iinfo;
19038+
19039+ iinfo = au_ii(inode);
19040+ AuRwMustAnyLock(&iinfo->ii_rwsem);
19041+ return !(iinfo->ii_hsb1 == h_inode->i_sb
19042+ && iinfo->ii_higen == h_inode->i_generation);
19043+}
19044+
19045+static inline void au_iigen_dec(struct inode *inode)
19046+{
19047+ struct au_iinfo *iinfo;
19048+ struct au_iigen *iigen;
19049+
19050+ iinfo = au_ii(inode);
19051+ iigen = &iinfo->ii_generation;
19052+ spin_lock(&iigen->ig_spin);
19053+ iigen->ig_generation--;
19054+ spin_unlock(&iigen->ig_spin);
19055+}
19056+
19057+static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
19058+{
19059+ int err;
19060+
19061+ err = 0;
19062+ if (unlikely(inode && au_iigen(inode, NULL) != sigen))
19063+ err = -EIO;
19064+
19065+ return err;
19066+}
19067+
19068+/* ---------------------------------------------------------------------- */
19069+
19070+static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
19071+ aufs_bindex_t bindex)
19072+{
19073+ return iinfo->ii_hinode + bindex;
19074+}
19075+
19076+static inline int au_is_bad_inode(struct inode *inode)
19077+{
19078+ return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
19079+}
19080+
19081+static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
19082+ aufs_bindex_t bindex)
19083+{
19084+ IiMustAnyLock(inode);
19085+ return au_hinode(au_ii(inode), bindex)->hi_id;
19086+}
19087+
19088+static inline aufs_bindex_t au_ibtop(struct inode *inode)
19089+{
19090+ IiMustAnyLock(inode);
19091+ return au_ii(inode)->ii_btop;
19092+}
19093+
19094+static inline aufs_bindex_t au_ibbot(struct inode *inode)
19095+{
19096+ IiMustAnyLock(inode);
19097+ return au_ii(inode)->ii_bbot;
19098+}
19099+
19100+static inline struct au_vdir *au_ivdir(struct inode *inode)
19101+{
19102+ IiMustAnyLock(inode);
19103+ return au_ii(inode)->ii_vdir;
19104+}
19105+
19106+static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
19107+{
19108+ IiMustAnyLock(inode);
19109+ return au_hinode(au_ii(inode), bindex)->hi_whdentry;
19110+}
19111+
19112+static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
19113+{
19114+ IiMustWriteLock(inode);
19115+ au_ii(inode)->ii_btop = bindex;
19116+}
19117+
19118+static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
19119+{
19120+ IiMustWriteLock(inode);
19121+ au_ii(inode)->ii_bbot = bindex;
19122+}
19123+
19124+static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
19125+{
19126+ IiMustWriteLock(inode);
19127+ au_ii(inode)->ii_vdir = vdir;
19128+}
19129+
19130+static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
19131+{
19132+ IiMustAnyLock(inode);
19133+ return au_hinode(au_ii(inode), bindex);
19134+}
19135+
19136+/* ---------------------------------------------------------------------- */
19137+
19138+static inline struct dentry *au_pinned_parent(struct au_pin *pin)
19139+{
19140+ if (pin)
19141+ return pin->parent;
19142+ return NULL;
19143+}
19144+
19145+static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
19146+{
19147+ if (pin && pin->hdir)
19148+ return pin->hdir->hi_inode;
19149+ return NULL;
19150+}
19151+
19152+static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
19153+{
19154+ if (pin)
19155+ return pin->hdir;
19156+ return NULL;
19157+}
19158+
19159+static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
19160+{
19161+ if (pin)
19162+ pin->dentry = dentry;
19163+}
19164+
19165+static inline void au_pin_set_parent_lflag(struct au_pin *pin,
19166+ unsigned char lflag)
19167+{
19168+ if (pin) {
19169+ if (lflag)
19170+ au_fset_pin(pin->flags, DI_LOCKED);
19171+ else
19172+ au_fclr_pin(pin->flags, DI_LOCKED);
19173+ }
19174+}
19175+
19176+#if 0 /* reserved */
19177+static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
19178+{
19179+ if (pin) {
19180+ dput(pin->parent);
19181+ pin->parent = dget(parent);
19182+ }
19183+}
19184+#endif
19185+
19186+/* ---------------------------------------------------------------------- */
19187+
19188+struct au_branch;
19189+#ifdef CONFIG_AUFS_HNOTIFY
19190+struct au_hnotify_op {
19191+ void (*ctl)(struct au_hinode *hinode, int do_set);
19192+ int (*alloc)(struct au_hinode *hinode);
19193+
19194+ /*
19195+ * if it returns true, the caller should free hinode->hi_notify,
19196+ * otherwise ->free() frees it.
19197+ */
19198+ int (*free)(struct au_hinode *hinode,
19199+ struct au_hnotify *hn) __must_check;
19200+
19201+ void (*fin)(void);
19202+ int (*init)(void);
19203+
19204+ int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
19205+ void (*fin_br)(struct au_branch *br);
19206+ int (*init_br)(struct au_branch *br, int perm);
19207+};
19208+
19209+/* hnotify.c */
19210+int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
19211+void au_hn_free(struct au_hinode *hinode);
19212+void au_hn_ctl(struct au_hinode *hinode, int do_set);
19213+void au_hn_reset(struct inode *inode, unsigned int flags);
19214+int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
19215+ const struct qstr *h_child_qstr, struct inode *h_child_inode);
19216+int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
19217+int au_hnotify_init_br(struct au_branch *br, int perm);
19218+void au_hnotify_fin_br(struct au_branch *br);
19219+int __init au_hnotify_init(void);
19220+void au_hnotify_fin(void);
19221+
19222+/* hfsnotify.c */
19223+extern const struct au_hnotify_op au_hnotify_op;
19224+
19225+static inline
19226+void au_hn_init(struct au_hinode *hinode)
19227+{
19228+ hinode->hi_notify = NULL;
19229+}
19230+
19231+static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
19232+{
19233+ return hinode->hi_notify;
19234+}
19235+
19236+#else
19237+AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
19238+ struct au_hinode *hinode __maybe_unused,
19239+ struct inode *inode __maybe_unused)
19240+AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
19241+AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
19242+AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
19243+ int do_set __maybe_unused)
19244+AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
19245+ unsigned int flags __maybe_unused)
19246+AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
19247+ struct au_branch *br __maybe_unused,
19248+ int perm __maybe_unused)
19249+AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
19250+ int perm __maybe_unused)
19251+AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
19252+AuStubInt0(__init au_hnotify_init, void)
19253+AuStubVoid(au_hnotify_fin, void)
19254+AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
19255+#endif /* CONFIG_AUFS_HNOTIFY */
19256+
19257+static inline void au_hn_suspend(struct au_hinode *hdir)
19258+{
19259+ au_hn_ctl(hdir, /*do_set*/0);
19260+}
19261+
19262+static inline void au_hn_resume(struct au_hinode *hdir)
19263+{
19264+ au_hn_ctl(hdir, /*do_set*/1);
19265+}
19266+
19267+static inline void au_hn_inode_lock(struct au_hinode *hdir)
19268+{
19269+ inode_lock(hdir->hi_inode);
19270+ au_hn_suspend(hdir);
19271+}
19272+
19273+static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
19274+ unsigned int sc __maybe_unused)
19275+{
19276+ inode_lock_nested(hdir->hi_inode, sc);
19277+ au_hn_suspend(hdir);
19278+}
19279+
19280+#if 0 /* unused */
19281+#include "vfsub.h"
19282+static inline void au_hn_inode_lock_shared_nested(struct au_hinode *hdir,
19283+ unsigned int sc)
19284+{
19285+ inode_lock_shared_nested(hdir->hi_inode, sc);
19286+ au_hn_suspend(hdir);
19287+}
19288+#endif
19289+
19290+static inline void au_hn_inode_unlock(struct au_hinode *hdir)
19291+{
19292+ au_hn_resume(hdir);
19293+ inode_unlock(hdir->hi_inode);
19294+}
19295+
19296+#endif /* __KERNEL__ */
19297+#endif /* __AUFS_INODE_H__ */
19298diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
19299--- /usr/share/empty/fs/aufs/ioctl.c 1970-01-01 01:00:00.000000000 +0100
19300+++ linux/fs/aufs/ioctl.c 2021-02-24 13:33:42.744347058 +0100
19301@@ -0,0 +1,220 @@
19302+// SPDX-License-Identifier: GPL-2.0
19303+/*
19304+ * Copyright (C) 2005-2020 Junjiro R. Okajima
19305+ *
19306+ * This program, aufs is free software; you can redistribute it and/or modify
19307+ * it under the terms of the GNU General Public License as published by
19308+ * the Free Software Foundation; either version 2 of the License, or
19309+ * (at your option) any later version.
19310+ *
19311+ * This program is distributed in the hope that it will be useful,
19312+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19313+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19314+ * GNU General Public License for more details.
19315+ *
19316+ * You should have received a copy of the GNU General Public License
19317+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
19318+ */
19319+
19320+/*
19321+ * ioctl
19322+ * plink-management and readdir in userspace.
19323+ * assist the pathconf(3) wrapper library.
19324+ * move-down
19325+ * File-based Hierarchical Storage Management.
19326+ */
19327+
19328+#include <linux/compat.h>
19329+#include <linux/file.h>
19330+#include "aufs.h"
19331+
19332+static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
19333+{
19334+ int err, fd;
19335+ aufs_bindex_t wbi, bindex, bbot;
19336+ struct file *h_file;
19337+ struct super_block *sb;
19338+ struct dentry *root;
19339+ struct au_branch *br;
19340+ struct aufs_wbr_fd wbrfd = {
19341+ .oflags = au_dir_roflags,
19342+ .brid = -1
19343+ };
19344+ const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
19345+ | O_NOATIME | O_CLOEXEC;
19346+
19347+ AuDebugOn(wbrfd.oflags & ~valid);
19348+
19349+ if (arg) {
19350+ err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
19351+ if (unlikely(err)) {
19352+ err = -EFAULT;
19353+ goto out;
19354+ }
19355+
19356+ err = -EINVAL;
19357+ AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
19358+ wbrfd.oflags |= au_dir_roflags;
19359+ AuDbg("0%o\n", wbrfd.oflags);
19360+ if (unlikely(wbrfd.oflags & ~valid))
19361+ goto out;
19362+ }
19363+
19364+ fd = get_unused_fd_flags(0);
19365+ err = fd;
19366+ if (unlikely(fd < 0))
19367+ goto out;
19368+
19369+ h_file = ERR_PTR(-EINVAL);
19370+ wbi = 0;
19371+ br = NULL;
19372+ sb = path->dentry->d_sb;
19373+ root = sb->s_root;
19374+ aufs_read_lock(root, AuLock_IR);
19375+ bbot = au_sbbot(sb);
19376+ if (wbrfd.brid >= 0) {
19377+ wbi = au_br_index(sb, wbrfd.brid);
19378+ if (unlikely(wbi < 0 || wbi > bbot))
19379+ goto out_unlock;
19380+ }
19381+
19382+ h_file = ERR_PTR(-ENOENT);
19383+ br = au_sbr(sb, wbi);
19384+ if (!au_br_writable(br->br_perm)) {
19385+ if (arg)
19386+ goto out_unlock;
19387+
19388+ bindex = wbi + 1;
19389+ wbi = -1;
19390+ for (; bindex <= bbot; bindex++) {
19391+ br = au_sbr(sb, bindex);
19392+ if (au_br_writable(br->br_perm)) {
19393+ wbi = bindex;
19394+ br = au_sbr(sb, wbi);
19395+ break;
19396+ }
19397+ }
19398+ }
19399+ AuDbg("wbi %d\n", wbi);
19400+ if (wbi >= 0)
19401+ h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
19402+ /*force_wr*/0);
19403+
19404+out_unlock:
19405+ aufs_read_unlock(root, AuLock_IR);
19406+ err = PTR_ERR(h_file);
19407+ if (IS_ERR(h_file))
19408+ goto out_fd;
19409+
19410+ au_lcnt_dec(&br->br_nfiles); /* cf. au_h_open() */
19411+ fd_install(fd, h_file);
19412+ err = fd;
19413+ goto out; /* success */
19414+
19415+out_fd:
19416+ put_unused_fd(fd);
19417+out:
19418+ AuTraceErr(err);
19419+ return err;
19420+}
19421+
19422+/* ---------------------------------------------------------------------- */
19423+
19424+long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
19425+{
19426+ long err;
19427+ struct dentry *dentry;
19428+
19429+ switch (cmd) {
19430+ case AUFS_CTL_RDU:
19431+ case AUFS_CTL_RDU_INO:
19432+ err = au_rdu_ioctl(file, cmd, arg);
19433+ break;
19434+
19435+ case AUFS_CTL_WBR_FD:
19436+ err = au_wbr_fd(&file->f_path, (void __user *)arg);
19437+ break;
19438+
19439+ case AUFS_CTL_IBUSY:
19440+ err = au_ibusy_ioctl(file, arg);
19441+ break;
19442+
19443+ case AUFS_CTL_BRINFO:
19444+ err = au_brinfo_ioctl(file, arg);
19445+ break;
19446+
19447+ case AUFS_CTL_FHSM_FD:
19448+ dentry = file->f_path.dentry;
19449+ if (IS_ROOT(dentry))
19450+ err = au_fhsm_fd(dentry->d_sb, arg);
19451+ else
19452+ err = -ENOTTY;
19453+ break;
19454+
19455+ default:
19456+ /* do not call the lower */
19457+ AuDbg("0x%x\n", cmd);
19458+ err = -ENOTTY;
19459+ }
19460+
19461+ AuTraceErr(err);
19462+ return err;
19463+}
19464+
19465+long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
19466+{
19467+ long err;
19468+
19469+ switch (cmd) {
19470+ case AUFS_CTL_MVDOWN:
19471+ err = au_mvdown(file->f_path.dentry, (void __user *)arg);
19472+ break;
19473+
19474+ case AUFS_CTL_WBR_FD:
19475+ err = au_wbr_fd(&file->f_path, (void __user *)arg);
19476+ break;
19477+
19478+ default:
19479+ /* do not call the lower */
19480+ AuDbg("0x%x\n", cmd);
19481+ err = -ENOTTY;
19482+ }
19483+
19484+ AuTraceErr(err);
19485+ return err;
19486+}
19487+
19488+#ifdef CONFIG_COMPAT
19489+long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
19490+ unsigned long arg)
19491+{
19492+ long err;
19493+
19494+ switch (cmd) {
19495+ case AUFS_CTL_RDU:
19496+ case AUFS_CTL_RDU_INO:
19497+ err = au_rdu_compat_ioctl(file, cmd, arg);
19498+ break;
19499+
19500+ case AUFS_CTL_IBUSY:
19501+ err = au_ibusy_compat_ioctl(file, arg);
19502+ break;
19503+
19504+ case AUFS_CTL_BRINFO:
19505+ err = au_brinfo_compat_ioctl(file, arg);
19506+ break;
19507+
19508+ default:
19509+ err = aufs_ioctl_dir(file, cmd, arg);
19510+ }
19511+
19512+ AuTraceErr(err);
19513+ return err;
19514+}
19515+
19516+long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
19517+ unsigned long arg)
19518+{
19519+ return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
19520+}
19521+#endif
19522diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
19523--- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
19524+++ linux/fs/aufs/i_op_add.c 2021-06-30 21:35:11.397206648 +0200
19525@@ -0,0 +1,941 @@
19526+// SPDX-License-Identifier: GPL-2.0
19527+/*
19528+ * Copyright (C) 2005-2020 Junjiro R. Okajima
19529+ *
19530+ * This program, aufs is free software; you can redistribute it and/or modify
19531+ * it under the terms of the GNU General Public License as published by
19532+ * the Free Software Foundation; either version 2 of the License, or
19533+ * (at your option) any later version.
19534+ *
19535+ * This program is distributed in the hope that it will be useful,
19536+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19537+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19538+ * GNU General Public License for more details.
19539+ *
19540+ * You should have received a copy of the GNU General Public License
19541+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
19542+ */
19543+
19544+/*
19545+ * inode operations (add entry)
19546+ */
19547+
19548+#include <linux/iversion.h>
19549+#include "aufs.h"
19550+
19551+/*
19552+ * final procedure of adding a new entry, except link(2).
19553+ * remove whiteout, instantiate, copyup the parent dir's times and size
19554+ * and update version.
19555+ * if it failed, re-create the removed whiteout.
19556+ */
19557+static int epilog(struct inode *dir, aufs_bindex_t bindex,
19558+ struct dentry *wh_dentry, struct dentry *dentry)
19559+{
19560+ int err, rerr;
19561+ aufs_bindex_t bwh;
19562+ struct path h_path;
19563+ struct super_block *sb;
19564+ struct inode *inode, *h_dir;
19565+ struct dentry *wh;
19566+
19567+ bwh = -1;
19568+ sb = dir->i_sb;
19569+ if (wh_dentry) {
19570+ h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
19571+ IMustLock(h_dir);
19572+ AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
19573+ bwh = au_dbwh(dentry);
19574+ h_path.dentry = wh_dentry;
19575+ h_path.mnt = au_sbr_mnt(sb, bindex);
19576+ err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
19577+ dentry);
19578+ if (unlikely(err))
19579+ goto out;
19580+ }
19581+
19582+ inode = au_new_inode(dentry, /*must_new*/1);
19583+ if (!IS_ERR(inode)) {
19584+ d_instantiate(dentry, inode);
19585+ dir = d_inode(dentry->d_parent); /* dir inode is locked */
19586+ IMustLock(dir);
19587+ au_dir_ts(dir, bindex);
19588+ inode_inc_iversion(dir);
19589+ au_fhsm_wrote(sb, bindex, /*force*/0);
19590+ return 0; /* success */
19591+ }
19592+
19593+ err = PTR_ERR(inode);
19594+ if (!wh_dentry)
19595+ goto out;
19596+
19597+ /* revert */
19598+ /* dir inode is locked */
19599+ wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
19600+ rerr = PTR_ERR(wh);
19601+ if (IS_ERR(wh)) {
19602+ AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
19603+ dentry, err, rerr);
19604+ err = -EIO;
19605+ } else
19606+ dput(wh);
19607+
19608+out:
19609+ return err;
19610+}
19611+
19612+static int au_d_may_add(struct dentry *dentry)
19613+{
19614+ int err;
19615+
19616+ err = 0;
19617+ if (unlikely(d_unhashed(dentry)))
19618+ err = -ENOENT;
19619+ if (unlikely(d_really_is_positive(dentry)))
19620+ err = -EEXIST;
19621+ return err;
19622+}
19623+
19624+/*
19625+ * simple tests for the adding inode operations.
19626+ * following the checks in vfs, plus the parent-child relationship.
19627+ */
19628+int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
19629+ struct dentry *h_parent, int isdir)
19630+{
19631+ int err;
19632+ umode_t h_mode;
19633+ struct dentry *h_dentry;
19634+ struct inode *h_inode;
19635+
19636+ err = -ENAMETOOLONG;
19637+ if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19638+ goto out;
19639+
19640+ h_dentry = au_h_dptr(dentry, bindex);
19641+ if (d_really_is_negative(dentry)) {
19642+ err = -EEXIST;
19643+ if (unlikely(d_is_positive(h_dentry)))
19644+ goto out;
19645+ } else {
19646+ /* rename(2) case */
19647+ err = -EIO;
19648+ if (unlikely(d_is_negative(h_dentry)))
19649+ goto out;
19650+ h_inode = d_inode(h_dentry);
19651+ if (unlikely(!h_inode->i_nlink))
19652+ goto out;
19653+
19654+ h_mode = h_inode->i_mode;
19655+ if (!isdir) {
19656+ err = -EISDIR;
19657+ if (unlikely(S_ISDIR(h_mode)))
19658+ goto out;
19659+ } else if (unlikely(!S_ISDIR(h_mode))) {
19660+ err = -ENOTDIR;
19661+ goto out;
19662+ }
19663+ }
19664+
19665+ err = 0;
19666+ /* expected parent dir is locked */
19667+ if (unlikely(h_parent != h_dentry->d_parent))
19668+ err = -EIO;
19669+
19670+out:
19671+ AuTraceErr(err);
19672+ return err;
19673+}
19674+
19675+/*
19676+ * initial procedure of adding a new entry.
19677+ * prepare writable branch and the parent dir, lock it,
19678+ * and lookup whiteout for the new entry.
19679+ */
19680+static struct dentry*
19681+lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
19682+ struct dentry *src_dentry, struct au_pin *pin,
19683+ struct au_wr_dir_args *wr_dir_args)
19684+{
19685+ struct dentry *wh_dentry, *h_parent;
19686+ struct super_block *sb;
19687+ struct au_branch *br;
19688+ int err;
19689+ unsigned int udba;
19690+ aufs_bindex_t bcpup;
19691+
19692+ AuDbg("%pd\n", dentry);
19693+
19694+ err = au_wr_dir(dentry, src_dentry, wr_dir_args);
19695+ bcpup = err;
19696+ wh_dentry = ERR_PTR(err);
19697+ if (unlikely(err < 0))
19698+ goto out;
19699+
19700+ sb = dentry->d_sb;
19701+ udba = au_opt_udba(sb);
19702+ err = au_pin(pin, dentry, bcpup, udba,
19703+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
19704+ wh_dentry = ERR_PTR(err);
19705+ if (unlikely(err))
19706+ goto out;
19707+
19708+ h_parent = au_pinned_h_parent(pin);
19709+ if (udba != AuOpt_UDBA_NONE
19710+ && au_dbtop(dentry) == bcpup)
19711+ err = au_may_add(dentry, bcpup, h_parent,
19712+ au_ftest_wrdir(wr_dir_args->flags, ISDIR));
19713+ else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19714+ err = -ENAMETOOLONG;
19715+ wh_dentry = ERR_PTR(err);
19716+ if (unlikely(err))
19717+ goto out_unpin;
19718+
19719+ br = au_sbr(sb, bcpup);
19720+ if (dt) {
19721+ struct path tmp = {
19722+ .dentry = h_parent,
19723+ .mnt = au_br_mnt(br)
19724+ };
19725+ au_dtime_store(dt, au_pinned_parent(pin), &tmp);
19726+ }
19727+
19728+ wh_dentry = NULL;
19729+ if (bcpup != au_dbwh(dentry))
19730+ goto out; /* success */
19731+
19732+ /*
19733+ * ENAMETOOLONG here means that if we allowed create such name, then it
19734+ * would not be able to removed in the future. So we don't allow such
19735+ * name here and we don't handle ENAMETOOLONG differently here.
19736+ */
19737+ wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
19738+
19739+out_unpin:
19740+ if (IS_ERR(wh_dentry))
19741+ au_unpin(pin);
19742+out:
19743+ return wh_dentry;
19744+}
19745+
19746+/* ---------------------------------------------------------------------- */
19747+
19748+enum { Mknod, Symlink, Creat };
19749+struct simple_arg {
19750+ int type;
19751+ union {
19752+ struct {
19753+ umode_t mode;
19754+ bool want_excl;
19755+ bool try_aopen;
19756+ struct vfsub_aopen_args *aopen;
19757+ } c;
19758+ struct {
19759+ const char *symname;
19760+ } s;
19761+ struct {
19762+ umode_t mode;
19763+ dev_t dev;
19764+ } m;
19765+ } u;
19766+};
19767+
19768+static int add_simple(struct inode *dir, struct dentry *dentry,
19769+ struct simple_arg *arg)
19770+{
19771+ int err, rerr;
19772+ aufs_bindex_t btop;
19773+ unsigned char created;
19774+ const unsigned char try_aopen
19775+ = (arg->type == Creat && arg->u.c.try_aopen);
19776+ struct vfsub_aopen_args *aopen = arg->u.c.aopen;
19777+ struct dentry *wh_dentry, *parent;
19778+ struct inode *h_dir;
19779+ struct super_block *sb;
19780+ struct au_branch *br;
19781+ /* to reduce stack size */
19782+ struct {
19783+ struct au_dtime dt;
19784+ struct au_pin pin;
19785+ struct path h_path;
19786+ struct au_wr_dir_args wr_dir_args;
19787+ } *a;
19788+
19789+ AuDbg("%pd\n", dentry);
19790+ IMustLock(dir);
19791+
19792+ err = -ENOMEM;
19793+ a = kmalloc(sizeof(*a), GFP_NOFS);
19794+ if (unlikely(!a))
19795+ goto out;
19796+ a->wr_dir_args.force_btgt = -1;
19797+ a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
19798+
19799+ parent = dentry->d_parent; /* dir inode is locked */
19800+ if (!try_aopen) {
19801+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
19802+ if (unlikely(err))
19803+ goto out_free;
19804+ }
19805+ err = au_d_may_add(dentry);
19806+ if (unlikely(err))
19807+ goto out_unlock;
19808+ if (!try_aopen)
19809+ di_write_lock_parent(parent);
19810+ wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
19811+ &a->pin, &a->wr_dir_args);
19812+ err = PTR_ERR(wh_dentry);
19813+ if (IS_ERR(wh_dentry))
19814+ goto out_parent;
19815+
19816+ btop = au_dbtop(dentry);
19817+ sb = dentry->d_sb;
19818+ br = au_sbr(sb, btop);
19819+ a->h_path.dentry = au_h_dptr(dentry, btop);
19820+ a->h_path.mnt = au_br_mnt(br);
19821+ h_dir = au_pinned_h_dir(&a->pin);
19822+ switch (arg->type) {
19823+ case Creat:
19824+ if (!try_aopen || !h_dir->i_op->atomic_open) {
19825+ err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
19826+ arg->u.c.want_excl);
19827+ created = !err;
19828+ if (!err && try_aopen)
19829+ aopen->file->f_mode |= FMODE_CREATED;
19830+ } else {
19831+ aopen->br = br;
19832+ err = vfsub_atomic_open(h_dir, a->h_path.dentry, aopen);
19833+ AuDbg("err %d\n", err);
19834+ AuDbgFile(aopen->file);
19835+ created = err >= 0
19836+ && !!(aopen->file->f_mode & FMODE_CREATED);
19837+ }
19838+ break;
19839+ case Symlink:
19840+ err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
19841+ created = !err;
19842+ break;
19843+ case Mknod:
19844+ err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
19845+ arg->u.m.dev);
19846+ created = !err;
19847+ break;
19848+ default:
19849+ BUG();
19850+ }
19851+ if (unlikely(err < 0))
19852+ goto out_unpin;
19853+
19854+ err = epilog(dir, btop, wh_dentry, dentry);
19855+ if (!err)
19856+ goto out_unpin; /* success */
19857+
19858+ /* revert */
19859+ if (created /* && d_is_positive(a->h_path.dentry) */) {
19860+ /* no delegation since it is just created */
19861+ rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
19862+ /*force*/0);
19863+ if (rerr) {
19864+ AuIOErr("%pd revert failure(%d, %d)\n",
19865+ dentry, err, rerr);
19866+ err = -EIO;
19867+ }
19868+ au_dtime_revert(&a->dt);
19869+ }
19870+ if (try_aopen && h_dir->i_op->atomic_open
19871+ && (aopen->file->f_mode & FMODE_OPENED))
19872+ /* aopen->file is still opened */
19873+ au_lcnt_dec(&aopen->br->br_nfiles);
19874+
19875+out_unpin:
19876+ au_unpin(&a->pin);
19877+ dput(wh_dentry);
19878+out_parent:
19879+ if (!try_aopen)
19880+ di_write_unlock(parent);
19881+out_unlock:
19882+ if (unlikely(err)) {
19883+ au_update_dbtop(dentry);
19884+ d_drop(dentry);
19885+ }
19886+ if (!try_aopen)
19887+ aufs_read_unlock(dentry, AuLock_DW);
19888+out_free:
19889+ au_kfree_rcu(a);
19890+out:
19891+ return err;
19892+}
19893+
19894+int aufs_mknod(struct user_namespace *userns, struct inode *dir,
19895+ struct dentry *dentry, umode_t mode, dev_t dev)
19896+{
19897+ struct simple_arg arg = {
19898+ .type = Mknod,
19899+ .u.m = {
19900+ .mode = mode,
19901+ .dev = dev
19902+ }
19903+ };
19904+ return add_simple(dir, dentry, &arg);
19905+}
19906+
19907+int aufs_symlink(struct user_namespace *userns, struct inode *dir,
19908+ struct dentry *dentry, const char *symname)
19909+{
19910+ struct simple_arg arg = {
19911+ .type = Symlink,
19912+ .u.s.symname = symname
19913+ };
19914+ return add_simple(dir, dentry, &arg);
19915+}
19916+
19917+int aufs_create(struct user_namespace *userns, struct inode *dir,
19918+ struct dentry *dentry, umode_t mode, bool want_excl)
19919+{
19920+ struct simple_arg arg = {
19921+ .type = Creat,
19922+ .u.c = {
19923+ .mode = mode,
19924+ .want_excl = want_excl
19925+ }
19926+ };
19927+ return add_simple(dir, dentry, &arg);
19928+}
19929+
19930+int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
19931+ struct vfsub_aopen_args *aopen_args)
19932+{
19933+ struct simple_arg arg = {
19934+ .type = Creat,
19935+ .u.c = {
19936+ .mode = aopen_args->create_mode,
19937+ .want_excl = aopen_args->open_flag & O_EXCL,
19938+ .try_aopen = true,
19939+ .aopen = aopen_args
19940+ }
19941+ };
19942+ return add_simple(dir, dentry, &arg);
19943+}
19944+
19945+int aufs_tmpfile(struct user_namespace *userns, struct inode *dir,
19946+ struct dentry *dentry, umode_t mode)
19947+{
19948+ int err;
19949+ aufs_bindex_t bindex;
19950+ struct super_block *sb;
19951+ struct dentry *parent, *h_parent, *h_dentry;
19952+ struct inode *h_dir, *inode;
19953+ struct vfsmount *h_mnt;
19954+ struct user_namespace *h_userns;
19955+ struct au_wr_dir_args wr_dir_args = {
19956+ .force_btgt = -1,
19957+ .flags = AuWrDir_TMPFILE
19958+ };
19959+
19960+ /* copy-up may happen */
19961+ inode_lock(dir);
19962+
19963+ sb = dir->i_sb;
19964+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
19965+ if (unlikely(err))
19966+ goto out;
19967+
19968+ err = au_di_init(dentry);
19969+ if (unlikely(err))
19970+ goto out_si;
19971+
19972+ err = -EBUSY;
19973+ parent = d_find_any_alias(dir);
19974+ AuDebugOn(!parent);
19975+ di_write_lock_parent(parent);
19976+ if (unlikely(d_inode(parent) != dir))
19977+ goto out_parent;
19978+
19979+ err = au_digen_test(parent, au_sigen(sb));
19980+ if (unlikely(err))
19981+ goto out_parent;
19982+
19983+ bindex = au_dbtop(parent);
19984+ au_set_dbtop(dentry, bindex);
19985+ au_set_dbbot(dentry, bindex);
19986+ err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
19987+ bindex = err;
19988+ if (unlikely(err < 0))
19989+ goto out_parent;
19990+
19991+ err = -EOPNOTSUPP;
19992+ h_dir = au_h_iptr(dir, bindex);
19993+ if (unlikely(!h_dir->i_op->tmpfile))
19994+ goto out_parent;
19995+
19996+ h_mnt = au_sbr_mnt(sb, bindex);
19997+ err = vfsub_mnt_want_write(h_mnt);
19998+ if (unlikely(err))
19999+ goto out_parent;
20000+
20001+ h_userns = mnt_user_ns(h_mnt);
20002+ h_parent = au_h_dptr(parent, bindex);
20003+ h_dentry = vfs_tmpfile(h_userns, h_parent, mode, /*open_flag*/0);
20004+ if (IS_ERR(h_dentry)) {
20005+ err = PTR_ERR(h_dentry);
20006+ goto out_mnt;
20007+ }
20008+
20009+ au_set_dbtop(dentry, bindex);
20010+ au_set_dbbot(dentry, bindex);
20011+ au_set_h_dptr(dentry, bindex, dget(h_dentry));
20012+ inode = au_new_inode(dentry, /*must_new*/1);
20013+ if (IS_ERR(inode)) {
20014+ err = PTR_ERR(inode);
20015+ au_set_h_dptr(dentry, bindex, NULL);
20016+ au_set_dbtop(dentry, -1);
20017+ au_set_dbbot(dentry, -1);
20018+ } else {
20019+ if (!inode->i_nlink)
20020+ set_nlink(inode, 1);
20021+ d_tmpfile(dentry, inode);
20022+ au_di(dentry)->di_tmpfile = 1;
20023+
20024+ /* update without i_mutex */
20025+ if (au_ibtop(dir) == au_dbtop(dentry))
20026+ au_cpup_attr_timesizes(dir);
20027+ }
20028+ dput(h_dentry);
20029+
20030+out_mnt:
20031+ vfsub_mnt_drop_write(h_mnt);
20032+out_parent:
20033+ di_write_unlock(parent);
20034+ dput(parent);
20035+ di_write_unlock(dentry);
20036+ if (unlikely(err)) {
20037+ au_di_fin(dentry);
20038+ dentry->d_fsdata = NULL;
20039+ }
20040+out_si:
20041+ si_read_unlock(sb);
20042+out:
20043+ inode_unlock(dir);
20044+ return err;
20045+}
20046+
20047+/* ---------------------------------------------------------------------- */
20048+
20049+struct au_link_args {
20050+ aufs_bindex_t bdst, bsrc;
20051+ struct au_pin pin;
20052+ struct path h_path;
20053+ struct dentry *src_parent, *parent;
20054+};
20055+
20056+static int au_cpup_before_link(struct dentry *src_dentry,
20057+ struct au_link_args *a)
20058+{
20059+ int err;
20060+ struct dentry *h_src_dentry;
20061+ struct au_cp_generic cpg = {
20062+ .dentry = src_dentry,
20063+ .bdst = a->bdst,
20064+ .bsrc = a->bsrc,
20065+ .len = -1,
20066+ .pin = &a->pin,
20067+ .flags = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
20068+ };
20069+
20070+ di_read_lock_parent(a->src_parent, AuLock_IR);
20071+ err = au_test_and_cpup_dirs(src_dentry, a->bdst);
20072+ if (unlikely(err))
20073+ goto out;
20074+
20075+ h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
20076+ err = au_pin(&a->pin, src_dentry, a->bdst,
20077+ au_opt_udba(src_dentry->d_sb),
20078+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20079+ if (unlikely(err))
20080+ goto out;
20081+
20082+ err = au_sio_cpup_simple(&cpg);
20083+ au_unpin(&a->pin);
20084+
20085+out:
20086+ di_read_unlock(a->src_parent, AuLock_IR);
20087+ return err;
20088+}
20089+
20090+static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
20091+ struct au_link_args *a)
20092+{
20093+ int err;
20094+ unsigned char plink;
20095+ aufs_bindex_t bbot;
20096+ struct dentry *h_src_dentry;
20097+ struct inode *h_inode, *inode, *delegated;
20098+ struct super_block *sb;
20099+ struct file *h_file;
20100+
20101+ plink = 0;
20102+ h_inode = NULL;
20103+ sb = src_dentry->d_sb;
20104+ inode = d_inode(src_dentry);
20105+ if (au_ibtop(inode) <= a->bdst)
20106+ h_inode = au_h_iptr(inode, a->bdst);
20107+ if (!h_inode || !h_inode->i_nlink) {
20108+ /* copyup src_dentry as the name of dentry. */
20109+ bbot = au_dbbot(dentry);
20110+ if (bbot < a->bsrc)
20111+ au_set_dbbot(dentry, a->bsrc);
20112+ au_set_h_dptr(dentry, a->bsrc,
20113+ dget(au_h_dptr(src_dentry, a->bsrc)));
20114+ dget(a->h_path.dentry);
20115+ au_set_h_dptr(dentry, a->bdst, NULL);
20116+ AuDbg("temporary d_inode...\n");
20117+ spin_lock(&dentry->d_lock);
20118+ dentry->d_inode = d_inode(src_dentry); /* tmp */
20119+ spin_unlock(&dentry->d_lock);
20120+ h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
20121+ if (IS_ERR(h_file))
20122+ err = PTR_ERR(h_file);
20123+ else {
20124+ struct au_cp_generic cpg = {
20125+ .dentry = dentry,
20126+ .bdst = a->bdst,
20127+ .bsrc = -1,
20128+ .len = -1,
20129+ .pin = &a->pin,
20130+ .flags = AuCpup_KEEPLINO
20131+ };
20132+ err = au_sio_cpup_simple(&cpg);
20133+ au_h_open_post(dentry, a->bsrc, h_file);
20134+ if (!err) {
20135+ dput(a->h_path.dentry);
20136+ a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20137+ } else
20138+ au_set_h_dptr(dentry, a->bdst,
20139+ a->h_path.dentry);
20140+ }
20141+ spin_lock(&dentry->d_lock);
20142+ dentry->d_inode = NULL; /* restore */
20143+ spin_unlock(&dentry->d_lock);
20144+ AuDbg("temporary d_inode...done\n");
20145+ au_set_h_dptr(dentry, a->bsrc, NULL);
20146+ au_set_dbbot(dentry, bbot);
20147+ } else {
20148+ /* the inode of src_dentry already exists on a.bdst branch */
20149+ h_src_dentry = d_find_alias(h_inode);
20150+ if (!h_src_dentry && au_plink_test(inode)) {
20151+ plink = 1;
20152+ h_src_dentry = au_plink_lkup(inode, a->bdst);
20153+ err = PTR_ERR(h_src_dentry);
20154+ if (IS_ERR(h_src_dentry))
20155+ goto out;
20156+
20157+ if (unlikely(d_is_negative(h_src_dentry))) {
20158+ dput(h_src_dentry);
20159+ h_src_dentry = NULL;
20160+ }
20161+
20162+ }
20163+ if (h_src_dentry) {
20164+ delegated = NULL;
20165+ err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
20166+ &a->h_path, &delegated);
20167+ if (unlikely(err == -EWOULDBLOCK)) {
20168+ pr_warn("cannot retry for NFSv4 delegation"
20169+ " for an internal link\n");
20170+ iput(delegated);
20171+ }
20172+ dput(h_src_dentry);
20173+ } else {
20174+ AuIOErr("no dentry found for hi%lu on b%d\n",
20175+ h_inode->i_ino, a->bdst);
20176+ err = -EIO;
20177+ }
20178+ }
20179+
20180+ if (!err && !plink)
20181+ au_plink_append(inode, a->bdst, a->h_path.dentry);
20182+
20183+out:
20184+ AuTraceErr(err);
20185+ return err;
20186+}
20187+
20188+int aufs_link(struct dentry *src_dentry, struct inode *dir,
20189+ struct dentry *dentry)
20190+{
20191+ int err, rerr;
20192+ struct au_dtime dt;
20193+ struct au_link_args *a;
20194+ struct dentry *wh_dentry, *h_src_dentry;
20195+ struct inode *inode, *delegated;
20196+ struct super_block *sb;
20197+ struct au_wr_dir_args wr_dir_args = {
20198+ /* .force_btgt = -1, */
20199+ .flags = AuWrDir_ADD_ENTRY
20200+ };
20201+
20202+ IMustLock(dir);
20203+ inode = d_inode(src_dentry);
20204+ IMustLock(inode);
20205+
20206+ err = -ENOMEM;
20207+ a = kzalloc(sizeof(*a), GFP_NOFS);
20208+ if (unlikely(!a))
20209+ goto out;
20210+
20211+ a->parent = dentry->d_parent; /* dir inode is locked */
20212+ err = aufs_read_and_write_lock2(dentry, src_dentry,
20213+ AuLock_NOPLM | AuLock_GEN);
20214+ if (unlikely(err))
20215+ goto out_kfree;
20216+ err = au_d_linkable(src_dentry);
20217+ if (unlikely(err))
20218+ goto out_unlock;
20219+ err = au_d_may_add(dentry);
20220+ if (unlikely(err))
20221+ goto out_unlock;
20222+
20223+ a->src_parent = dget_parent(src_dentry);
20224+ wr_dir_args.force_btgt = au_ibtop(inode);
20225+
20226+ di_write_lock_parent(a->parent);
20227+ wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
20228+ wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
20229+ &wr_dir_args);
20230+ err = PTR_ERR(wh_dentry);
20231+ if (IS_ERR(wh_dentry))
20232+ goto out_parent;
20233+
20234+ err = 0;
20235+ sb = dentry->d_sb;
20236+ a->bdst = au_dbtop(dentry);
20237+ a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20238+ a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
20239+ a->bsrc = au_ibtop(inode);
20240+ h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
20241+ if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
20242+ h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
20243+ if (!h_src_dentry) {
20244+ a->bsrc = au_dbtop(src_dentry);
20245+ h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
20246+ AuDebugOn(!h_src_dentry);
20247+ } else if (IS_ERR(h_src_dentry)) {
20248+ err = PTR_ERR(h_src_dentry);
20249+ goto out_parent;
20250+ }
20251+
20252+ /*
20253+ * aufs doesn't touch the credential so
20254+ * security_dentry_create_files_as() is unnecessary.
20255+ */
20256+ if (au_opt_test(au_mntflags(sb), PLINK)) {
20257+ if (a->bdst < a->bsrc
20258+ /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
20259+ err = au_cpup_or_link(src_dentry, dentry, a);
20260+ else {
20261+ delegated = NULL;
20262+ err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
20263+ &a->h_path, &delegated);
20264+ if (unlikely(err == -EWOULDBLOCK)) {
20265+ pr_warn("cannot retry for NFSv4 delegation"
20266+ " for an internal link\n");
20267+ iput(delegated);
20268+ }
20269+ }
20270+ dput(h_src_dentry);
20271+ } else {
20272+ /*
20273+ * copyup src_dentry to the branch we process,
20274+ * and then link(2) to it.
20275+ */
20276+ dput(h_src_dentry);
20277+ if (a->bdst < a->bsrc
20278+ /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
20279+ au_unpin(&a->pin);
20280+ di_write_unlock(a->parent);
20281+ err = au_cpup_before_link(src_dentry, a);
20282+ di_write_lock_parent(a->parent);
20283+ if (!err)
20284+ err = au_pin(&a->pin, dentry, a->bdst,
20285+ au_opt_udba(sb),
20286+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20287+ if (unlikely(err))
20288+ goto out_wh;
20289+ }
20290+ if (!err) {
20291+ h_src_dentry = au_h_dptr(src_dentry, a->bdst);
20292+ err = -ENOENT;
20293+ if (h_src_dentry && d_is_positive(h_src_dentry)) {
20294+ delegated = NULL;
20295+ err = vfsub_link(h_src_dentry,
20296+ au_pinned_h_dir(&a->pin),
20297+ &a->h_path, &delegated);
20298+ if (unlikely(err == -EWOULDBLOCK)) {
20299+ pr_warn("cannot retry"
20300+ " for NFSv4 delegation"
20301+ " for an internal link\n");
20302+ iput(delegated);
20303+ }
20304+ }
20305+ }
20306+ }
20307+ if (unlikely(err))
20308+ goto out_unpin;
20309+
20310+ if (wh_dentry) {
20311+ a->h_path.dentry = wh_dentry;
20312+ err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
20313+ dentry);
20314+ if (unlikely(err))
20315+ goto out_revert;
20316+ }
20317+
20318+ au_dir_ts(dir, a->bdst);
20319+ inode_inc_iversion(dir);
20320+ inc_nlink(inode);
20321+ inode->i_ctime = dir->i_ctime;
20322+ d_instantiate(dentry, au_igrab(inode));
20323+ if (d_unhashed(a->h_path.dentry))
20324+ /* some filesystem calls d_drop() */
20325+ d_drop(dentry);
20326+ /* some filesystems consume an inode even hardlink */
20327+ au_fhsm_wrote(sb, a->bdst, /*force*/0);
20328+ goto out_unpin; /* success */
20329+
20330+out_revert:
20331+ /* no delegation since it is just created */
20332+ rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
20333+ /*delegated*/NULL, /*force*/0);
20334+ if (unlikely(rerr)) {
20335+ AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
20336+ err = -EIO;
20337+ }
20338+ au_dtime_revert(&dt);
20339+out_unpin:
20340+ au_unpin(&a->pin);
20341+out_wh:
20342+ dput(wh_dentry);
20343+out_parent:
20344+ di_write_unlock(a->parent);
20345+ dput(a->src_parent);
20346+out_unlock:
20347+ if (unlikely(err)) {
20348+ au_update_dbtop(dentry);
20349+ d_drop(dentry);
20350+ }
20351+ aufs_read_and_write_unlock2(dentry, src_dentry);
20352+out_kfree:
20353+ au_kfree_rcu(a);
20354+out:
20355+ AuTraceErr(err);
20356+ return err;
20357+}
20358+
20359+int aufs_mkdir(struct user_namespace *userns, struct inode *dir,
20360+ struct dentry *dentry, umode_t mode)
20361+{
20362+ int err, rerr;
20363+ aufs_bindex_t bindex;
20364+ unsigned char diropq;
20365+ struct path h_path;
20366+ struct dentry *wh_dentry, *parent, *opq_dentry;
20367+ struct inode *h_inode;
20368+ struct super_block *sb;
20369+ struct {
20370+ struct au_pin pin;
20371+ struct au_dtime dt;
20372+ } *a; /* reduce the stack usage */
20373+ struct au_wr_dir_args wr_dir_args = {
20374+ .force_btgt = -1,
20375+ .flags = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
20376+ };
20377+
20378+ IMustLock(dir);
20379+
20380+ err = -ENOMEM;
20381+ a = kmalloc(sizeof(*a), GFP_NOFS);
20382+ if (unlikely(!a))
20383+ goto out;
20384+
20385+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
20386+ if (unlikely(err))
20387+ goto out_free;
20388+ err = au_d_may_add(dentry);
20389+ if (unlikely(err))
20390+ goto out_unlock;
20391+
20392+ parent = dentry->d_parent; /* dir inode is locked */
20393+ di_write_lock_parent(parent);
20394+ wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
20395+ &a->pin, &wr_dir_args);
20396+ err = PTR_ERR(wh_dentry);
20397+ if (IS_ERR(wh_dentry))
20398+ goto out_parent;
20399+
20400+ sb = dentry->d_sb;
20401+ bindex = au_dbtop(dentry);
20402+ h_path.dentry = au_h_dptr(dentry, bindex);
20403+ h_path.mnt = au_sbr_mnt(sb, bindex);
20404+ err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
20405+ if (unlikely(err))
20406+ goto out_unpin;
20407+
20408+ /* make the dir opaque */
20409+ diropq = 0;
20410+ h_inode = d_inode(h_path.dentry);
20411+ if (wh_dentry
20412+ || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
20413+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
20414+ opq_dentry = au_diropq_create(dentry, bindex);
20415+ inode_unlock(h_inode);
20416+ err = PTR_ERR(opq_dentry);
20417+ if (IS_ERR(opq_dentry))
20418+ goto out_dir;
20419+ dput(opq_dentry);
20420+ diropq = 1;
20421+ }
20422+
20423+ err = epilog(dir, bindex, wh_dentry, dentry);
20424+ if (!err) {
20425+ inc_nlink(dir);
20426+ goto out_unpin; /* success */
20427+ }
20428+
20429+ /* revert */
20430+ if (diropq) {
20431+ AuLabel(revert opq);
20432+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
20433+ rerr = au_diropq_remove(dentry, bindex);
20434+ inode_unlock(h_inode);
20435+ if (rerr) {
20436+ AuIOErr("%pd reverting diropq failed(%d, %d)\n",
20437+ dentry, err, rerr);
20438+ err = -EIO;
20439+ }
20440+ }
20441+
20442+out_dir:
20443+ AuLabel(revert dir);
20444+ rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
20445+ if (rerr) {
20446+ AuIOErr("%pd reverting dir failed(%d, %d)\n",
20447+ dentry, err, rerr);
20448+ err = -EIO;
20449+ }
20450+ au_dtime_revert(&a->dt);
20451+out_unpin:
20452+ au_unpin(&a->pin);
20453+ dput(wh_dentry);
20454+out_parent:
20455+ di_write_unlock(parent);
20456+out_unlock:
20457+ if (unlikely(err)) {
20458+ au_update_dbtop(dentry);
20459+ d_drop(dentry);
20460+ }
20461+ aufs_read_unlock(dentry, AuLock_DW);
20462+out_free:
20463+ au_kfree_rcu(a);
20464+out:
20465+ return err;
20466+}
20467diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
20468--- /usr/share/empty/fs/aufs/i_op.c 1970-01-01 01:00:00.000000000 +0100
20469+++ linux/fs/aufs/i_op.c 2021-06-30 21:35:11.397206648 +0200
20470@@ -0,0 +1,1513 @@
20471+// SPDX-License-Identifier: GPL-2.0
20472+/*
20473+ * Copyright (C) 2005-2020 Junjiro R. Okajima
20474+ *
20475+ * This program, aufs is free software; you can redistribute it and/or modify
20476+ * it under the terms of the GNU General Public License as published by
20477+ * the Free Software Foundation; either version 2 of the License, or
20478+ * (at your option) any later version.
20479+ *
20480+ * This program is distributed in the hope that it will be useful,
20481+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20482+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20483+ * GNU General Public License for more details.
20484+ *
20485+ * You should have received a copy of the GNU General Public License
20486+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
20487+ */
20488+
20489+/*
20490+ * inode operations (except add/del/rename)
20491+ */
20492+
20493+#include <linux/device_cgroup.h>
20494+#include <linux/fs_stack.h>
20495+#include <linux/iversion.h>
20496+#include <linux/namei.h>
20497+#include <linux/security.h>
20498+#include "aufs.h"
20499+
20500+static int h_permission(struct inode *h_inode, int mask,
20501+ struct path *h_path, int brperm)
20502+{
20503+ int err;
20504+ const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
20505+ struct user_namespace *h_userns;
20506+
20507+ err = -EPERM;
20508+ if (write_mask && IS_IMMUTABLE(h_inode))
20509+ goto out;
20510+
20511+ err = -EACCES;
20512+ if (((mask & MAY_EXEC)
20513+ && S_ISREG(h_inode->i_mode)
20514+ && (path_noexec(h_path)
20515+ || !(h_inode->i_mode & 0111))))
20516+ goto out;
20517+
20518+ /*
20519+ * - skip the lower fs test in the case of write to ro branch.
20520+ * - nfs dir permission write check is optimized, but a policy for
20521+ * link/rename requires a real check.
20522+ * - nfs always sets SB_POSIXACL regardless its mount option 'noacl.'
20523+ * in this case, generic_permission() returns -EOPNOTSUPP.
20524+ */
20525+ h_userns = mnt_user_ns(h_path->mnt);
20526+ if ((write_mask && !au_br_writable(brperm))
20527+ || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
20528+ && write_mask && !(mask & MAY_READ))
20529+ || !h_inode->i_op->permission) {
20530+ /* AuLabel(generic_permission); */
20531+ /* AuDbg("get_acl %ps\n", h_inode->i_op->get_acl); */
20532+ err = generic_permission(h_userns, h_inode, mask);
20533+ if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
20534+ err = h_inode->i_op->permission(h_userns, h_inode,
20535+ mask);
20536+ AuTraceErr(err);
20537+ } else {
20538+ /* AuLabel(h_inode->permission); */
20539+ err = h_inode->i_op->permission(h_userns, h_inode, mask);
20540+ AuTraceErr(err);
20541+ }
20542+
20543+ if (!err)
20544+ err = devcgroup_inode_permission(h_inode, mask);
20545+ if (!err)
20546+ err = security_inode_permission(h_inode, mask);
20547+
20548+out:
20549+ return err;
20550+}
20551+
20552+static int aufs_permission(struct user_namespace *userns, struct inode *inode,
20553+ int mask)
20554+{
20555+ int err;
20556+ aufs_bindex_t bindex, bbot;
20557+ const unsigned char isdir = !!S_ISDIR(inode->i_mode),
20558+ write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
20559+ struct inode *h_inode;
20560+ struct super_block *sb;
20561+ struct au_branch *br;
20562+
20563+ /* todo: support rcu-walk? */
20564+ if (mask & MAY_NOT_BLOCK)
20565+ return -ECHILD;
20566+
20567+ sb = inode->i_sb;
20568+ si_read_lock(sb, AuLock_FLUSH);
20569+ ii_read_lock_child(inode);
20570+#if 0 /* reserved for future use */
20571+ /*
20572+ * This test may be rather 'too much' since the test is essentially done
20573+ * in the aufs_lookup(). Theoretically it is possible that the inode
20574+ * generation doesn't match to the superblock's here. But it isn't a
20575+ * big deal I suppose.
20576+ */
20577+ err = au_iigen_test(inode, au_sigen(sb));
20578+ if (unlikely(err))
20579+ goto out;
20580+#endif
20581+
20582+ if (!isdir
20583+ || write_mask
20584+ || au_opt_test(au_mntflags(sb), DIRPERM1)) {
20585+ err = au_busy_or_stale();
20586+ h_inode = au_h_iptr(inode, au_ibtop(inode));
20587+ if (unlikely(!h_inode
20588+ || (h_inode->i_mode & S_IFMT)
20589+ != (inode->i_mode & S_IFMT)))
20590+ goto out;
20591+
20592+ err = 0;
20593+ bindex = au_ibtop(inode);
20594+ br = au_sbr(sb, bindex);
20595+ err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
20596+ if (write_mask
20597+ && !err
20598+ && !special_file(h_inode->i_mode)) {
20599+ /* test whether the upper writable branch exists */
20600+ err = -EROFS;
20601+ for (; bindex >= 0; bindex--)
20602+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
20603+ err = 0;
20604+ break;
20605+ }
20606+ }
20607+ goto out;
20608+ }
20609+
20610+ /* non-write to dir */
20611+ err = 0;
20612+ bbot = au_ibbot(inode);
20613+ for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
20614+ h_inode = au_h_iptr(inode, bindex);
20615+ if (h_inode) {
20616+ err = au_busy_or_stale();
20617+ if (unlikely(!S_ISDIR(h_inode->i_mode)))
20618+ break;
20619+
20620+ br = au_sbr(sb, bindex);
20621+ err = h_permission(h_inode, mask, &br->br_path,
20622+ br->br_perm);
20623+ }
20624+ }
20625+
20626+out:
20627+ ii_read_unlock(inode);
20628+ si_read_unlock(sb);
20629+ return err;
20630+}
20631+
20632+/* ---------------------------------------------------------------------- */
20633+
20634+static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
20635+ unsigned int flags)
20636+{
20637+ struct dentry *ret, *parent;
20638+ struct inode *inode;
20639+ struct super_block *sb;
20640+ int err, npositive;
20641+
20642+ IMustLock(dir);
20643+
20644+ /* todo: support rcu-walk? */
20645+ ret = ERR_PTR(-ECHILD);
20646+ if (flags & LOOKUP_RCU)
20647+ goto out;
20648+
20649+ ret = ERR_PTR(-ENAMETOOLONG);
20650+ if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20651+ goto out;
20652+
20653+ sb = dir->i_sb;
20654+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
20655+ ret = ERR_PTR(err);
20656+ if (unlikely(err))
20657+ goto out;
20658+
20659+ err = au_di_init(dentry);
20660+ ret = ERR_PTR(err);
20661+ if (unlikely(err))
20662+ goto out_si;
20663+
20664+ inode = NULL;
20665+ npositive = 0; /* suppress a warning */
20666+ parent = dentry->d_parent; /* dir inode is locked */
20667+ di_read_lock_parent(parent, AuLock_IR);
20668+ err = au_alive_dir(parent);
20669+ if (!err)
20670+ err = au_digen_test(parent, au_sigen(sb));
20671+ if (!err) {
20672+ /* regardless LOOKUP_CREATE, always ALLOW_NEG */
20673+ npositive = au_lkup_dentry(dentry, au_dbtop(parent),
20674+ AuLkup_ALLOW_NEG);
20675+ err = npositive;
20676+ }
20677+ di_read_unlock(parent, AuLock_IR);
20678+ ret = ERR_PTR(err);
20679+ if (unlikely(err < 0))
20680+ goto out_unlock;
20681+
20682+ if (npositive) {
20683+ inode = au_new_inode(dentry, /*must_new*/0);
20684+ if (IS_ERR(inode)) {
20685+ ret = (void *)inode;
20686+ inode = NULL;
20687+ goto out_unlock;
20688+ }
20689+ }
20690+
20691+ if (inode)
20692+ atomic_inc(&inode->i_count);
20693+ ret = d_splice_alias(inode, dentry);
20694+#if 0 /* reserved for future use */
20695+ if (unlikely(d_need_lookup(dentry))) {
20696+ spin_lock(&dentry->d_lock);
20697+ dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
20698+ spin_unlock(&dentry->d_lock);
20699+ } else
20700+#endif
20701+ if (inode) {
20702+ if (!IS_ERR(ret)) {
20703+ iput(inode);
20704+ if (ret && ret != dentry)
20705+ ii_write_unlock(inode);
20706+ } else {
20707+ ii_write_unlock(inode);
20708+ iput(inode);
20709+ inode = NULL;
20710+ }
20711+ }
20712+
20713+out_unlock:
20714+ di_write_unlock(dentry);
20715+out_si:
20716+ si_read_unlock(sb);
20717+out:
20718+ return ret;
20719+}
20720+
20721+/* ---------------------------------------------------------------------- */
20722+
20723+/*
20724+ * very dirty and complicated aufs ->atomic_open().
20725+ * aufs_atomic_open()
20726+ * + au_aopen_or_create()
20727+ * + add_simple()
20728+ * + vfsub_atomic_open()
20729+ * + branch fs ->atomic_open()
20730+ * may call the actual 'open' for h_file
20731+ * + inc br_nfiles only if opened
20732+ * + au_aopen_no_open() or au_aopen_do_open()
20733+ *
20734+ * au_aopen_do_open()
20735+ * + finish_open()
20736+ * + au_do_aopen()
20737+ * + au_do_open() the body of all 'open'
20738+ * + au_do_open_nondir()
20739+ * set the passed h_file
20740+ *
20741+ * au_aopen_no_open()
20742+ * + finish_no_open()
20743+ */
20744+
20745+struct aopen_node {
20746+ struct hlist_bl_node hblist;
20747+ struct file *file, *h_file;
20748+};
20749+
20750+static int au_do_aopen(struct inode *inode, struct file *file)
20751+{
20752+ struct hlist_bl_head *aopen;
20753+ struct hlist_bl_node *pos;
20754+ struct aopen_node *node;
20755+ struct au_do_open_args args = {
20756+ .aopen = 1,
20757+ .open = au_do_open_nondir
20758+ };
20759+
20760+ aopen = &au_sbi(inode->i_sb)->si_aopen;
20761+ hlist_bl_lock(aopen);
20762+ hlist_bl_for_each_entry(node, pos, aopen, hblist)
20763+ if (node->file == file) {
20764+ args.h_file = node->h_file;
20765+ break;
20766+ }
20767+ hlist_bl_unlock(aopen);
20768+ /* AuDebugOn(!args.h_file); */
20769+
20770+ return au_do_open(file, &args);
20771+}
20772+
20773+static int au_aopen_do_open(struct file *file, struct dentry *dentry,
20774+ struct aopen_node *aopen_node)
20775+{
20776+ int err;
20777+ struct hlist_bl_head *aopen;
20778+
20779+ AuLabel(here);
20780+ aopen = &au_sbi(dentry->d_sb)->si_aopen;
20781+ au_hbl_add(&aopen_node->hblist, aopen);
20782+ err = finish_open(file, dentry, au_do_aopen);
20783+ au_hbl_del(&aopen_node->hblist, aopen);
20784+ /* AuDbgFile(file); */
20785+ AuDbg("%pd%s%s\n", dentry,
20786+ (file->f_mode & FMODE_CREATED) ? " created" : "",
20787+ (file->f_mode & FMODE_OPENED) ? " opened" : "");
20788+
20789+ AuTraceErr(err);
20790+ return err;
20791+}
20792+
20793+static int au_aopen_no_open(struct file *file, struct dentry *dentry)
20794+{
20795+ int err;
20796+
20797+ AuLabel(here);
20798+ dget(dentry);
20799+ err = finish_no_open(file, dentry);
20800+
20801+ AuTraceErr(err);
20802+ return err;
20803+}
20804+
20805+static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
20806+ struct file *file, unsigned int open_flag,
20807+ umode_t create_mode)
20808+{
20809+ int err, did_open;
20810+ unsigned int lkup_flags;
20811+ aufs_bindex_t bindex;
20812+ struct super_block *sb;
20813+ struct dentry *parent, *d;
20814+ struct vfsub_aopen_args args = {
20815+ .open_flag = open_flag,
20816+ .create_mode = create_mode
20817+ };
20818+ struct aopen_node aopen_node = {
20819+ .file = file
20820+ };
20821+
20822+ IMustLock(dir);
20823+ AuDbg("open_flag 0%o\n", open_flag);
20824+ AuDbgDentry(dentry);
20825+
20826+ err = 0;
20827+ if (!au_di(dentry)) {
20828+ lkup_flags = LOOKUP_OPEN;
20829+ if (open_flag & O_CREAT)
20830+ lkup_flags |= LOOKUP_CREATE;
20831+ d = aufs_lookup(dir, dentry, lkup_flags);
20832+ if (IS_ERR(d)) {
20833+ err = PTR_ERR(d);
20834+ AuTraceErr(err);
20835+ goto out;
20836+ } else if (d) {
20837+ /*
20838+ * obsoleted dentry found.
20839+ * another error will be returned later.
20840+ */
20841+ d_drop(d);
20842+ AuDbgDentry(d);
20843+ dput(d);
20844+ }
20845+ AuDbgDentry(dentry);
20846+ }
20847+
20848+ if (d_is_positive(dentry)
20849+ || d_unhashed(dentry)
20850+ || d_unlinked(dentry)
20851+ || !(open_flag & O_CREAT)) {
20852+ err = au_aopen_no_open(file, dentry);
20853+ goto out; /* success */
20854+ }
20855+
20856+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
20857+ if (unlikely(err))
20858+ goto out;
20859+
20860+ sb = dentry->d_sb;
20861+ parent = dentry->d_parent; /* dir is locked */
20862+ di_write_lock_parent(parent);
20863+ err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
20864+ if (unlikely(err < 0))
20865+ goto out_parent;
20866+
20867+ AuDbgDentry(dentry);
20868+ if (d_is_positive(dentry)) {
20869+ err = au_aopen_no_open(file, dentry);
20870+ goto out_parent; /* success */
20871+ }
20872+
20873+ args.file = alloc_empty_file(file->f_flags, current_cred());
20874+ err = PTR_ERR(args.file);
20875+ if (IS_ERR(args.file))
20876+ goto out_parent;
20877+
20878+ bindex = au_dbtop(dentry);
20879+ err = au_aopen_or_create(dir, dentry, &args);
20880+ AuTraceErr(err);
20881+ AuDbgFile(args.file);
20882+ file->f_mode = args.file->f_mode & ~FMODE_OPENED;
20883+ did_open = !!(args.file->f_mode & FMODE_OPENED);
20884+ if (!did_open) {
20885+ fput(args.file);
20886+ args.file = NULL;
20887+ }
20888+ di_write_unlock(parent);
20889+ di_write_unlock(dentry);
20890+ if (unlikely(err < 0)) {
20891+ if (args.file)
20892+ fput(args.file);
20893+ goto out_sb;
20894+ }
20895+
20896+ if (!did_open)
20897+ err = au_aopen_no_open(file, dentry);
20898+ else {
20899+ aopen_node.h_file = args.file;
20900+ err = au_aopen_do_open(file, dentry, &aopen_node);
20901+ }
20902+ if (unlikely(err < 0)) {
20903+ if (args.file)
20904+ fput(args.file);
20905+ if (did_open)
20906+ au_lcnt_dec(&args.br->br_nfiles);
20907+ }
20908+ goto out_sb; /* success */
20909+
20910+out_parent:
20911+ di_write_unlock(parent);
20912+ di_write_unlock(dentry);
20913+out_sb:
20914+ si_read_unlock(sb);
20915+out:
20916+ AuTraceErr(err);
20917+ AuDbgFile(file);
20918+ return err;
20919+}
20920+
20921+
20922+/* ---------------------------------------------------------------------- */
20923+
20924+static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
20925+ const unsigned char add_entry, aufs_bindex_t bcpup,
20926+ aufs_bindex_t btop)
20927+{
20928+ int err;
20929+ struct dentry *h_parent;
20930+ struct inode *h_dir;
20931+
20932+ if (add_entry)
20933+ IMustLock(d_inode(parent));
20934+ else
20935+ di_write_lock_parent(parent);
20936+
20937+ err = 0;
20938+ if (!au_h_dptr(parent, bcpup)) {
20939+ if (btop > bcpup)
20940+ err = au_cpup_dirs(dentry, bcpup);
20941+ else if (btop < bcpup)
20942+ err = au_cpdown_dirs(dentry, bcpup);
20943+ else
20944+ BUG();
20945+ }
20946+ if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
20947+ h_parent = au_h_dptr(parent, bcpup);
20948+ h_dir = d_inode(h_parent);
20949+ inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
20950+ err = au_lkup_neg(dentry, bcpup, /*wh*/0);
20951+ /* todo: no unlock here */
20952+ inode_unlock_shared(h_dir);
20953+
20954+ AuDbg("bcpup %d\n", bcpup);
20955+ if (!err) {
20956+ if (d_really_is_negative(dentry))
20957+ au_set_h_dptr(dentry, btop, NULL);
20958+ au_update_dbrange(dentry, /*do_put_zero*/0);
20959+ }
20960+ }
20961+
20962+ if (!add_entry)
20963+ di_write_unlock(parent);
20964+ if (!err)
20965+ err = bcpup; /* success */
20966+
20967+ AuTraceErr(err);
20968+ return err;
20969+}
20970+
20971+/*
20972+ * decide the branch and the parent dir where we will create a new entry.
20973+ * returns new bindex or an error.
20974+ * copyup the parent dir if needed.
20975+ */
20976+int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
20977+ struct au_wr_dir_args *args)
20978+{
20979+ int err;
20980+ unsigned int flags;
20981+ aufs_bindex_t bcpup, btop, src_btop;
20982+ const unsigned char add_entry
20983+ = au_ftest_wrdir(args->flags, ADD_ENTRY)
20984+ | au_ftest_wrdir(args->flags, TMPFILE);
20985+ struct super_block *sb;
20986+ struct dentry *parent;
20987+ struct au_sbinfo *sbinfo;
20988+
20989+ sb = dentry->d_sb;
20990+ sbinfo = au_sbi(sb);
20991+ parent = dget_parent(dentry);
20992+ btop = au_dbtop(dentry);
20993+ bcpup = btop;
20994+ if (args->force_btgt < 0) {
20995+ if (src_dentry) {
20996+ src_btop = au_dbtop(src_dentry);
20997+ if (src_btop < btop)
20998+ bcpup = src_btop;
20999+ } else if (add_entry) {
21000+ flags = 0;
21001+ if (au_ftest_wrdir(args->flags, ISDIR))
21002+ au_fset_wbr(flags, DIR);
21003+ err = AuWbrCreate(sbinfo, dentry, flags);
21004+ bcpup = err;
21005+ }
21006+
21007+ if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
21008+ if (add_entry)
21009+ err = AuWbrCopyup(sbinfo, dentry);
21010+ else {
21011+ if (!IS_ROOT(dentry)) {
21012+ di_read_lock_parent(parent, !AuLock_IR);
21013+ err = AuWbrCopyup(sbinfo, dentry);
21014+ di_read_unlock(parent, !AuLock_IR);
21015+ } else
21016+ err = AuWbrCopyup(sbinfo, dentry);
21017+ }
21018+ bcpup = err;
21019+ if (unlikely(err < 0))
21020+ goto out;
21021+ }
21022+ } else {
21023+ bcpup = args->force_btgt;
21024+ AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
21025+ }
21026+
21027+ AuDbg("btop %d, bcpup %d\n", btop, bcpup);
21028+ err = bcpup;
21029+ if (bcpup == btop)
21030+ goto out; /* success */
21031+
21032+ /* copyup the new parent into the branch we process */
21033+ err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
21034+ if (err >= 0) {
21035+ if (d_really_is_negative(dentry)) {
21036+ au_set_h_dptr(dentry, btop, NULL);
21037+ au_set_dbtop(dentry, bcpup);
21038+ au_set_dbbot(dentry, bcpup);
21039+ }
21040+ AuDebugOn(add_entry
21041+ && !au_ftest_wrdir(args->flags, TMPFILE)
21042+ && !au_h_dptr(dentry, bcpup));
21043+ }
21044+
21045+out:
21046+ dput(parent);
21047+ return err;
21048+}
21049+
21050+/* ---------------------------------------------------------------------- */
21051+
21052+void au_pin_hdir_unlock(struct au_pin *p)
21053+{
21054+ if (p->hdir)
21055+ au_hn_inode_unlock(p->hdir);
21056+}
21057+
21058+int au_pin_hdir_lock(struct au_pin *p)
21059+{
21060+ int err;
21061+
21062+ err = 0;
21063+ if (!p->hdir)
21064+ goto out;
21065+
21066+ /* even if an error happens later, keep this lock */
21067+ au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
21068+
21069+ err = -EBUSY;
21070+ if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
21071+ goto out;
21072+
21073+ err = 0;
21074+ if (p->h_dentry)
21075+ err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
21076+ p->h_parent, p->br);
21077+
21078+out:
21079+ return err;
21080+}
21081+
21082+int au_pin_hdir_relock(struct au_pin *p)
21083+{
21084+ int err, i;
21085+ struct inode *h_i;
21086+ struct dentry *h_d[] = {
21087+ p->h_dentry,
21088+ p->h_parent
21089+ };
21090+
21091+ err = au_pin_hdir_lock(p);
21092+ if (unlikely(err))
21093+ goto out;
21094+
21095+ for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
21096+ if (!h_d[i])
21097+ continue;
21098+ if (d_is_positive(h_d[i])) {
21099+ h_i = d_inode(h_d[i]);
21100+ err = !h_i->i_nlink;
21101+ }
21102+ }
21103+
21104+out:
21105+ return err;
21106+}
21107+
21108+static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
21109+{
21110+ atomic_long_set(&p->hdir->hi_inode->i_rwsem.owner, (long)task);
21111+}
21112+
21113+void au_pin_hdir_acquire_nest(struct au_pin *p)
21114+{
21115+ if (p->hdir) {
21116+ rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
21117+ p->lsc_hi, 0, NULL, _RET_IP_);
21118+ au_pin_hdir_set_owner(p, current);
21119+ }
21120+}
21121+
21122+void au_pin_hdir_release(struct au_pin *p)
21123+{
21124+ if (p->hdir) {
21125+ au_pin_hdir_set_owner(p, p->task);
21126+ rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, _RET_IP_);
21127+ }
21128+}
21129+
21130+struct dentry *au_pinned_h_parent(struct au_pin *pin)
21131+{
21132+ if (pin && pin->parent)
21133+ return au_h_dptr(pin->parent, pin->bindex);
21134+ return NULL;
21135+}
21136+
21137+void au_unpin(struct au_pin *p)
21138+{
21139+ if (p->hdir)
21140+ au_pin_hdir_unlock(p);
21141+ if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
21142+ vfsub_mnt_drop_write(p->h_mnt);
21143+ if (!p->hdir)
21144+ return;
21145+
21146+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21147+ di_read_unlock(p->parent, AuLock_IR);
21148+ iput(p->hdir->hi_inode);
21149+ dput(p->parent);
21150+ p->parent = NULL;
21151+ p->hdir = NULL;
21152+ p->h_mnt = NULL;
21153+ /* do not clear p->task */
21154+}
21155+
21156+int au_do_pin(struct au_pin *p)
21157+{
21158+ int err;
21159+ struct super_block *sb;
21160+ struct inode *h_dir;
21161+
21162+ err = 0;
21163+ sb = p->dentry->d_sb;
21164+ p->br = au_sbr(sb, p->bindex);
21165+ if (IS_ROOT(p->dentry)) {
21166+ if (au_ftest_pin(p->flags, MNT_WRITE)) {
21167+ p->h_mnt = au_br_mnt(p->br);
21168+ err = vfsub_mnt_want_write(p->h_mnt);
21169+ if (unlikely(err)) {
21170+ au_fclr_pin(p->flags, MNT_WRITE);
21171+ goto out_err;
21172+ }
21173+ }
21174+ goto out;
21175+ }
21176+
21177+ p->h_dentry = NULL;
21178+ if (p->bindex <= au_dbbot(p->dentry))
21179+ p->h_dentry = au_h_dptr(p->dentry, p->bindex);
21180+
21181+ p->parent = dget_parent(p->dentry);
21182+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21183+ di_read_lock(p->parent, AuLock_IR, p->lsc_di);
21184+
21185+ h_dir = NULL;
21186+ p->h_parent = au_h_dptr(p->parent, p->bindex);
21187+ p->hdir = au_hi(d_inode(p->parent), p->bindex);
21188+ if (p->hdir)
21189+ h_dir = p->hdir->hi_inode;
21190+
21191+ /*
21192+ * udba case, or
21193+ * if DI_LOCKED is not set, then p->parent may be different
21194+ * and h_parent can be NULL.
21195+ */
21196+ if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
21197+ err = -EBUSY;
21198+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21199+ di_read_unlock(p->parent, AuLock_IR);
21200+ dput(p->parent);
21201+ p->parent = NULL;
21202+ goto out_err;
21203+ }
21204+
21205+ if (au_ftest_pin(p->flags, MNT_WRITE)) {
21206+ p->h_mnt = au_br_mnt(p->br);
21207+ err = vfsub_mnt_want_write(p->h_mnt);
21208+ if (unlikely(err)) {
21209+ au_fclr_pin(p->flags, MNT_WRITE);
21210+ if (!au_ftest_pin(p->flags, DI_LOCKED))
21211+ di_read_unlock(p->parent, AuLock_IR);
21212+ dput(p->parent);
21213+ p->parent = NULL;
21214+ goto out_err;
21215+ }
21216+ }
21217+
21218+ au_igrab(h_dir);
21219+ err = au_pin_hdir_lock(p);
21220+ if (!err)
21221+ goto out; /* success */
21222+
21223+ au_unpin(p);
21224+
21225+out_err:
21226+ pr_err("err %d\n", err);
21227+ err = au_busy_or_stale();
21228+out:
21229+ return err;
21230+}
21231+
21232+void au_pin_init(struct au_pin *p, struct dentry *dentry,
21233+ aufs_bindex_t bindex, int lsc_di, int lsc_hi,
21234+ unsigned int udba, unsigned char flags)
21235+{
21236+ p->dentry = dentry;
21237+ p->udba = udba;
21238+ p->lsc_di = lsc_di;
21239+ p->lsc_hi = lsc_hi;
21240+ p->flags = flags;
21241+ p->bindex = bindex;
21242+
21243+ p->parent = NULL;
21244+ p->hdir = NULL;
21245+ p->h_mnt = NULL;
21246+
21247+ p->h_dentry = NULL;
21248+ p->h_parent = NULL;
21249+ p->br = NULL;
21250+ p->task = current;
21251+}
21252+
21253+int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
21254+ unsigned int udba, unsigned char flags)
21255+{
21256+ au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
21257+ udba, flags);
21258+ return au_do_pin(pin);
21259+}
21260+
21261+/* ---------------------------------------------------------------------- */
21262+
21263+/*
21264+ * ->setattr() and ->getattr() are called in various cases.
21265+ * chmod, stat: dentry is revalidated.
21266+ * fchmod, fstat: file and dentry are not revalidated, additionally they may be
21267+ * unhashed.
21268+ * for ->setattr(), ia->ia_file is passed from ftruncate only.
21269+ */
21270+/* todo: consolidate with do_refresh() and simple_reval_dpath() */
21271+int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
21272+{
21273+ int err;
21274+ struct dentry *parent;
21275+
21276+ err = 0;
21277+ if (au_digen_test(dentry, sigen)) {
21278+ parent = dget_parent(dentry);
21279+ di_read_lock_parent(parent, AuLock_IR);
21280+ err = au_refresh_dentry(dentry, parent);
21281+ di_read_unlock(parent, AuLock_IR);
21282+ dput(parent);
21283+ }
21284+
21285+ AuTraceErr(err);
21286+ return err;
21287+}
21288+
21289+int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
21290+ struct au_icpup_args *a)
21291+{
21292+ int err;
21293+ loff_t sz;
21294+ aufs_bindex_t btop, ibtop;
21295+ struct dentry *hi_wh, *parent;
21296+ struct inode *inode;
21297+ struct au_wr_dir_args wr_dir_args = {
21298+ .force_btgt = -1,
21299+ .flags = 0
21300+ };
21301+
21302+ if (d_is_dir(dentry))
21303+ au_fset_wrdir(wr_dir_args.flags, ISDIR);
21304+ /* plink or hi_wh() case */
21305+ btop = au_dbtop(dentry);
21306+ inode = d_inode(dentry);
21307+ ibtop = au_ibtop(inode);
21308+ if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
21309+ wr_dir_args.force_btgt = ibtop;
21310+ err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
21311+ if (unlikely(err < 0))
21312+ goto out;
21313+ a->btgt = err;
21314+ if (err != btop)
21315+ au_fset_icpup(a->flags, DID_CPUP);
21316+
21317+ err = 0;
21318+ a->pin_flags = AuPin_MNT_WRITE;
21319+ parent = NULL;
21320+ if (!IS_ROOT(dentry)) {
21321+ au_fset_pin(a->pin_flags, DI_LOCKED);
21322+ parent = dget_parent(dentry);
21323+ di_write_lock_parent(parent);
21324+ }
21325+
21326+ err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
21327+ if (unlikely(err))
21328+ goto out_parent;
21329+
21330+ sz = -1;
21331+ a->h_path.dentry = au_h_dptr(dentry, btop);
21332+ a->h_inode = d_inode(a->h_path.dentry);
21333+ if (ia && (ia->ia_valid & ATTR_SIZE)) {
21334+ inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
21335+ if (ia->ia_size < i_size_read(a->h_inode))
21336+ sz = ia->ia_size;
21337+ inode_unlock_shared(a->h_inode);
21338+ }
21339+
21340+ hi_wh = NULL;
21341+ if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
21342+ hi_wh = au_hi_wh(inode, a->btgt);
21343+ if (!hi_wh) {
21344+ struct au_cp_generic cpg = {
21345+ .dentry = dentry,
21346+ .bdst = a->btgt,
21347+ .bsrc = -1,
21348+ .len = sz,
21349+ .pin = &a->pin
21350+ };
21351+ err = au_sio_cpup_wh(&cpg, /*file*/NULL);
21352+ if (unlikely(err))
21353+ goto out_unlock;
21354+ hi_wh = au_hi_wh(inode, a->btgt);
21355+ /* todo: revalidate hi_wh? */
21356+ }
21357+ }
21358+
21359+ if (parent) {
21360+ au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
21361+ di_downgrade_lock(parent, AuLock_IR);
21362+ dput(parent);
21363+ parent = NULL;
21364+ }
21365+ if (!au_ftest_icpup(a->flags, DID_CPUP))
21366+ goto out; /* success */
21367+
21368+ if (!d_unhashed(dentry)) {
21369+ struct au_cp_generic cpg = {
21370+ .dentry = dentry,
21371+ .bdst = a->btgt,
21372+ .bsrc = btop,
21373+ .len = sz,
21374+ .pin = &a->pin,
21375+ .flags = AuCpup_DTIME | AuCpup_HOPEN
21376+ };
21377+ err = au_sio_cpup_simple(&cpg);
21378+ if (!err)
21379+ a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21380+ } else if (!hi_wh)
21381+ a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21382+ else
21383+ a->h_path.dentry = hi_wh; /* do not dget here */
21384+
21385+out_unlock:
21386+ a->h_inode = d_inode(a->h_path.dentry);
21387+ if (!err)
21388+ goto out; /* success */
21389+ au_unpin(&a->pin);
21390+out_parent:
21391+ if (parent) {
21392+ di_write_unlock(parent);
21393+ dput(parent);
21394+ }
21395+out:
21396+ if (!err)
21397+ inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
21398+ return err;
21399+}
21400+
21401+static int aufs_setattr(struct user_namespace *userns, struct dentry *dentry,
21402+ struct iattr *ia)
21403+{
21404+ int err;
21405+ struct inode *inode, *delegated;
21406+ struct super_block *sb;
21407+ struct file *file;
21408+ struct au_icpup_args *a;
21409+ struct user_namespace *h_userns;
21410+
21411+ inode = d_inode(dentry);
21412+ IMustLock(inode);
21413+
21414+ err = setattr_prepare(userns, dentry, ia);
21415+ if (unlikely(err))
21416+ goto out;
21417+
21418+ err = -ENOMEM;
21419+ a = kzalloc(sizeof(*a), GFP_NOFS);
21420+ if (unlikely(!a))
21421+ goto out;
21422+
21423+ if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
21424+ ia->ia_valid &= ~ATTR_MODE;
21425+
21426+ file = NULL;
21427+ sb = dentry->d_sb;
21428+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21429+ if (unlikely(err))
21430+ goto out_kfree;
21431+
21432+ if (ia->ia_valid & ATTR_FILE) {
21433+ /* currently ftruncate(2) only */
21434+ AuDebugOn(!d_is_reg(dentry));
21435+ file = ia->ia_file;
21436+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
21437+ /*fi_lsc*/0);
21438+ if (unlikely(err))
21439+ goto out_si;
21440+ ia->ia_file = au_hf_top(file);
21441+ a->udba = AuOpt_UDBA_NONE;
21442+ } else {
21443+ /* fchmod() doesn't pass ia_file */
21444+ a->udba = au_opt_udba(sb);
21445+ di_write_lock_child(dentry);
21446+ /* no d_unlinked(), to set UDBA_NONE for root */
21447+ if (d_unhashed(dentry))
21448+ a->udba = AuOpt_UDBA_NONE;
21449+ if (a->udba != AuOpt_UDBA_NONE) {
21450+ AuDebugOn(IS_ROOT(dentry));
21451+ err = au_reval_for_attr(dentry, au_sigen(sb));
21452+ if (unlikely(err))
21453+ goto out_dentry;
21454+ }
21455+ }
21456+
21457+ err = au_pin_and_icpup(dentry, ia, a);
21458+ if (unlikely(err < 0))
21459+ goto out_dentry;
21460+ if (au_ftest_icpup(a->flags, DID_CPUP)) {
21461+ ia->ia_file = NULL;
21462+ ia->ia_valid &= ~ATTR_FILE;
21463+ }
21464+
21465+ a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
21466+ if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
21467+ == (ATTR_MODE | ATTR_CTIME)) {
21468+ err = security_path_chmod(&a->h_path, ia->ia_mode);
21469+ if (unlikely(err))
21470+ goto out_unlock;
21471+ } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
21472+ && (ia->ia_valid & ATTR_CTIME)) {
21473+ err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
21474+ if (unlikely(err))
21475+ goto out_unlock;
21476+ }
21477+
21478+ if (ia->ia_valid & ATTR_SIZE) {
21479+ struct file *f;
21480+
21481+ if (ia->ia_size < i_size_read(inode))
21482+ /* unmap only */
21483+ truncate_setsize(inode, ia->ia_size);
21484+
21485+ f = NULL;
21486+ if (ia->ia_valid & ATTR_FILE)
21487+ f = ia->ia_file;
21488+ inode_unlock(a->h_inode);
21489+ err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
21490+ inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
21491+ } else {
21492+ delegated = NULL;
21493+ while (1) {
21494+ err = vfsub_notify_change(&a->h_path, ia, &delegated);
21495+ if (delegated) {
21496+ err = break_deleg_wait(&delegated);
21497+ if (!err)
21498+ continue;
21499+ }
21500+ break;
21501+ }
21502+ }
21503+ /*
21504+ * regardless aufs 'acl' option setting.
21505+ * why don't all acl-aware fs call this func from their ->setattr()?
21506+ */
21507+ if (!err && (ia->ia_valid & ATTR_MODE)) {
21508+ h_userns = mnt_user_ns(a->h_path.mnt);
21509+ err = vfsub_acl_chmod(h_userns, a->h_inode, ia->ia_mode);
21510+ }
21511+ if (!err)
21512+ au_cpup_attr_changeable(inode);
21513+
21514+out_unlock:
21515+ inode_unlock(a->h_inode);
21516+ au_unpin(&a->pin);
21517+ if (unlikely(err))
21518+ au_update_dbtop(dentry);
21519+out_dentry:
21520+ di_write_unlock(dentry);
21521+ if (file) {
21522+ fi_write_unlock(file);
21523+ ia->ia_file = file;
21524+ ia->ia_valid |= ATTR_FILE;
21525+ }
21526+out_si:
21527+ si_read_unlock(sb);
21528+out_kfree:
21529+ au_kfree_rcu(a);
21530+out:
21531+ AuTraceErr(err);
21532+ return err;
21533+}
21534+
21535+#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
21536+static int au_h_path_to_set_attr(struct dentry *dentry,
21537+ struct au_icpup_args *a, struct path *h_path)
21538+{
21539+ int err;
21540+ struct super_block *sb;
21541+
21542+ sb = dentry->d_sb;
21543+ a->udba = au_opt_udba(sb);
21544+ /* no d_unlinked(), to set UDBA_NONE for root */
21545+ if (d_unhashed(dentry))
21546+ a->udba = AuOpt_UDBA_NONE;
21547+ if (a->udba != AuOpt_UDBA_NONE) {
21548+ AuDebugOn(IS_ROOT(dentry));
21549+ err = au_reval_for_attr(dentry, au_sigen(sb));
21550+ if (unlikely(err))
21551+ goto out;
21552+ }
21553+ err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
21554+ if (unlikely(err < 0))
21555+ goto out;
21556+
21557+ h_path->dentry = a->h_path.dentry;
21558+ h_path->mnt = au_sbr_mnt(sb, a->btgt);
21559+
21560+out:
21561+ return err;
21562+}
21563+
21564+ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
21565+ struct au_sxattr *arg)
21566+{
21567+ int err;
21568+ struct path h_path;
21569+ struct super_block *sb;
21570+ struct au_icpup_args *a;
21571+ struct inode *h_inode;
21572+ struct user_namespace *h_userns;
21573+
21574+ IMustLock(inode);
21575+
21576+ err = -ENOMEM;
21577+ a = kzalloc(sizeof(*a), GFP_NOFS);
21578+ if (unlikely(!a))
21579+ goto out;
21580+
21581+ sb = dentry->d_sb;
21582+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21583+ if (unlikely(err))
21584+ goto out_kfree;
21585+
21586+ h_path.dentry = NULL; /* silence gcc */
21587+ di_write_lock_child(dentry);
21588+ err = au_h_path_to_set_attr(dentry, a, &h_path);
21589+ if (unlikely(err))
21590+ goto out_di;
21591+ h_userns = mnt_user_ns(h_path.mnt);
21592+
21593+ inode_unlock(a->h_inode);
21594+ switch (arg->type) {
21595+ case AU_XATTR_SET:
21596+ AuDebugOn(d_is_negative(h_path.dentry));
21597+ err = vfsub_setxattr(h_userns, h_path.dentry,
21598+ arg->u.set.name, arg->u.set.value,
21599+ arg->u.set.size, arg->u.set.flags);
21600+ break;
21601+ case AU_ACL_SET:
21602+ err = -EOPNOTSUPP;
21603+ h_inode = d_inode(h_path.dentry);
21604+ if (h_inode->i_op->set_acl) {
21605+ /* this will call posix_acl_update_mode */
21606+ err = h_inode->i_op->set_acl(h_userns, h_inode,
21607+ arg->u.acl_set.acl,
21608+ arg->u.acl_set.type);
21609+ }
21610+ break;
21611+ }
21612+ if (!err)
21613+ au_cpup_attr_timesizes(inode);
21614+
21615+ au_unpin(&a->pin);
21616+ if (unlikely(err))
21617+ au_update_dbtop(dentry);
21618+
21619+out_di:
21620+ di_write_unlock(dentry);
21621+ si_read_unlock(sb);
21622+out_kfree:
21623+ au_kfree_rcu(a);
21624+out:
21625+ AuTraceErr(err);
21626+ return err;
21627+}
21628+#endif
21629+
21630+static void au_refresh_iattr(struct inode *inode, struct kstat *st,
21631+ unsigned int nlink)
21632+{
21633+ unsigned int n;
21634+
21635+ inode->i_mode = st->mode;
21636+ /* don't i_[ug]id_write() here */
21637+ inode->i_uid = st->uid;
21638+ inode->i_gid = st->gid;
21639+ inode->i_atime = st->atime;
21640+ inode->i_mtime = st->mtime;
21641+ inode->i_ctime = st->ctime;
21642+
21643+ au_cpup_attr_nlink(inode, /*force*/0);
21644+ if (S_ISDIR(inode->i_mode)) {
21645+ n = inode->i_nlink;
21646+ n -= nlink;
21647+ n += st->nlink;
21648+ smp_mb(); /* for i_nlink */
21649+ /* 0 can happen */
21650+ set_nlink(inode, n);
21651+ }
21652+
21653+ spin_lock(&inode->i_lock);
21654+ inode->i_blocks = st->blocks;
21655+ i_size_write(inode, st->size);
21656+ spin_unlock(&inode->i_lock);
21657+}
21658+
21659+/*
21660+ * common routine for aufs_getattr() and au_getxattr().
21661+ * returns zero or negative (an error).
21662+ * @dentry will be read-locked in success.
21663+ */
21664+int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
21665+ struct path *h_path, int locked)
21666+{
21667+ int err;
21668+ unsigned int mnt_flags, sigen;
21669+ unsigned char udba_none;
21670+ aufs_bindex_t bindex;
21671+ struct super_block *sb, *h_sb;
21672+
21673+ h_path->mnt = NULL;
21674+ h_path->dentry = NULL;
21675+
21676+ err = 0;
21677+ sb = dentry->d_sb;
21678+ mnt_flags = au_mntflags(sb);
21679+ udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
21680+
21681+ if (unlikely(locked))
21682+ goto body; /* skip locking dinfo */
21683+
21684+ /* support fstat(2) */
21685+ if (!d_unlinked(dentry) && !udba_none) {
21686+ sigen = au_sigen(sb);
21687+ err = au_digen_test(dentry, sigen);
21688+ if (!err) {
21689+ di_read_lock_child(dentry, AuLock_IR);
21690+ err = au_dbrange_test(dentry);
21691+ if (unlikely(err)) {
21692+ di_read_unlock(dentry, AuLock_IR);
21693+ goto out;
21694+ }
21695+ } else {
21696+ AuDebugOn(IS_ROOT(dentry));
21697+ di_write_lock_child(dentry);
21698+ err = au_dbrange_test(dentry);
21699+ if (!err)
21700+ err = au_reval_for_attr(dentry, sigen);
21701+ if (!err)
21702+ di_downgrade_lock(dentry, AuLock_IR);
21703+ else {
21704+ di_write_unlock(dentry);
21705+ goto out;
21706+ }
21707+ }
21708+ } else
21709+ di_read_lock_child(dentry, AuLock_IR);
21710+
21711+body:
21712+ if (!inode) {
21713+ inode = d_inode(dentry);
21714+ if (unlikely(!inode))
21715+ goto out;
21716+ }
21717+ bindex = au_ibtop(inode);
21718+ h_path->mnt = au_sbr_mnt(sb, bindex);
21719+ h_sb = h_path->mnt->mnt_sb;
21720+ if (!force
21721+ && !au_test_fs_bad_iattr(h_sb)
21722+ && udba_none)
21723+ goto out; /* success */
21724+
21725+ if (au_dbtop(dentry) == bindex)
21726+ h_path->dentry = au_h_dptr(dentry, bindex);
21727+ else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
21728+ h_path->dentry = au_plink_lkup(inode, bindex);
21729+ if (IS_ERR(h_path->dentry))
21730+ /* pretending success */
21731+ h_path->dentry = NULL;
21732+ else
21733+ dput(h_path->dentry);
21734+ }
21735+
21736+out:
21737+ return err;
21738+}
21739+
21740+static int aufs_getattr(struct user_namespace *userns, const struct path *path,
21741+ struct kstat *st, u32 request, unsigned int query)
21742+{
21743+ int err;
21744+ unsigned char positive;
21745+ struct path h_path;
21746+ struct dentry *dentry;
21747+ struct inode *inode;
21748+ struct super_block *sb;
21749+
21750+ dentry = path->dentry;
21751+ inode = d_inode(dentry);
21752+ sb = dentry->d_sb;
21753+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21754+ if (unlikely(err))
21755+ goto out;
21756+ err = au_h_path_getattr(dentry, /*inode*/NULL, /*force*/0, &h_path,
21757+ /*locked*/0);
21758+ if (unlikely(err))
21759+ goto out_si;
21760+ if (unlikely(!h_path.dentry))
21761+ /* illegally overlapped or something */
21762+ goto out_fill; /* pretending success */
21763+
21764+ positive = d_is_positive(h_path.dentry);
21765+ if (positive)
21766+ /* no vfsub version */
21767+ err = vfs_getattr(&h_path, st, request, query);
21768+ if (!err) {
21769+ if (positive)
21770+ au_refresh_iattr(inode, st,
21771+ d_inode(h_path.dentry)->i_nlink);
21772+ goto out_fill; /* success */
21773+ }
21774+ AuTraceErr(err);
21775+ goto out_di;
21776+
21777+out_fill:
21778+ generic_fillattr(userns, inode, st);
21779+out_di:
21780+ di_read_unlock(dentry, AuLock_IR);
21781+out_si:
21782+ si_read_unlock(sb);
21783+out:
21784+ AuTraceErr(err);
21785+ return err;
21786+}
21787+
21788+/* ---------------------------------------------------------------------- */
21789+
21790+static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
21791+ struct delayed_call *done)
21792+{
21793+ const char *ret;
21794+ struct dentry *h_dentry;
21795+ struct inode *h_inode;
21796+ int err;
21797+ aufs_bindex_t bindex;
21798+
21799+ ret = NULL; /* suppress a warning */
21800+ err = -ECHILD;
21801+ if (!dentry)
21802+ goto out;
21803+
21804+ err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
21805+ if (unlikely(err))
21806+ goto out;
21807+
21808+ err = au_d_hashed_positive(dentry);
21809+ if (unlikely(err))
21810+ goto out_unlock;
21811+
21812+ err = -EINVAL;
21813+ inode = d_inode(dentry);
21814+ bindex = au_ibtop(inode);
21815+ h_inode = au_h_iptr(inode, bindex);
21816+ if (unlikely(!h_inode->i_op->get_link))
21817+ goto out_unlock;
21818+
21819+ err = -EBUSY;
21820+ h_dentry = NULL;
21821+ if (au_dbtop(dentry) <= bindex) {
21822+ h_dentry = au_h_dptr(dentry, bindex);
21823+ if (h_dentry)
21824+ dget(h_dentry);
21825+ }
21826+ if (!h_dentry) {
21827+ h_dentry = d_find_any_alias(h_inode);
21828+ if (IS_ERR(h_dentry)) {
21829+ err = PTR_ERR(h_dentry);
21830+ goto out_unlock;
21831+ }
21832+ }
21833+ if (unlikely(!h_dentry))
21834+ goto out_unlock;
21835+
21836+ err = 0;
21837+ AuDbg("%ps\n", h_inode->i_op->get_link);
21838+ AuDbgDentry(h_dentry);
21839+ ret = vfs_get_link(h_dentry, done);
21840+ dput(h_dentry);
21841+ if (IS_ERR(ret))
21842+ err = PTR_ERR(ret);
21843+
21844+out_unlock:
21845+ aufs_read_unlock(dentry, AuLock_IR);
21846+out:
21847+ if (unlikely(err))
21848+ ret = ERR_PTR(err);
21849+ AuTraceErrPtr(ret);
21850+ return ret;
21851+}
21852+
21853+/* ---------------------------------------------------------------------- */
21854+
21855+static int au_is_special(struct inode *inode)
21856+{
21857+ return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
21858+}
21859+
21860+static int aufs_update_time(struct inode *inode, struct timespec64 *ts,
21861+ int flags)
21862+{
21863+ int err;
21864+ aufs_bindex_t bindex;
21865+ struct super_block *sb;
21866+ struct inode *h_inode;
21867+ struct vfsmount *h_mnt;
21868+
21869+ sb = inode->i_sb;
21870+ WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
21871+ "unexpected s_flags 0x%lx", sb->s_flags);
21872+
21873+ /* mmap_sem might be acquired already, cf. aufs_mmap() */
21874+ lockdep_off();
21875+ si_read_lock(sb, AuLock_FLUSH);
21876+ ii_write_lock_child(inode);
21877+
21878+ err = 0;
21879+ bindex = au_ibtop(inode);
21880+ h_inode = au_h_iptr(inode, bindex);
21881+ if (!au_test_ro(sb, bindex, inode)) {
21882+ h_mnt = au_sbr_mnt(sb, bindex);
21883+ err = vfsub_mnt_want_write(h_mnt);
21884+ if (!err) {
21885+ err = vfsub_update_time(h_inode, ts, flags);
21886+ vfsub_mnt_drop_write(h_mnt);
21887+ }
21888+ } else if (au_is_special(h_inode)) {
21889+ /*
21890+ * Never copy-up here.
21891+ * These special files may already be opened and used for
21892+ * communicating. If we copied it up, then the communication
21893+ * would be corrupted.
21894+ */
21895+ AuWarn1("timestamps for i%lu are ignored "
21896+ "since it is on readonly branch (hi%lu).\n",
21897+ inode->i_ino, h_inode->i_ino);
21898+ } else if (flags & ~S_ATIME) {
21899+ err = -EIO;
21900+ AuIOErr1("unexpected flags 0x%x\n", flags);
21901+ AuDebugOn(1);
21902+ }
21903+
21904+ if (!err)
21905+ au_cpup_attr_timesizes(inode);
21906+ ii_write_unlock(inode);
21907+ si_read_unlock(sb);
21908+ lockdep_on();
21909+
21910+ if (!err && (flags & S_VERSION))
21911+ inode_inc_iversion(inode);
21912+
21913+ return err;
21914+}
21915+
21916+/* ---------------------------------------------------------------------- */
21917+
21918+/* no getattr version will be set by module.c:aufs_init() */
21919+struct inode_operations aufs_iop_nogetattr[AuIop_Last],
21920+ aufs_iop[] = {
21921+ [AuIop_SYMLINK] = {
21922+ .permission = aufs_permission,
21923+#ifdef CONFIG_FS_POSIX_ACL
21924+ .get_acl = aufs_get_acl,
21925+ .set_acl = aufs_set_acl, /* unsupport for symlink? */
21926+#endif
21927+
21928+ .setattr = aufs_setattr,
21929+ .getattr = aufs_getattr,
21930+
21931+#ifdef CONFIG_AUFS_XATTR
21932+ .listxattr = aufs_listxattr,
21933+#endif
21934+
21935+ .get_link = aufs_get_link,
21936+
21937+ /* .update_time = aufs_update_time */
21938+ },
21939+ [AuIop_DIR] = {
21940+ .create = aufs_create,
21941+ .lookup = aufs_lookup,
21942+ .link = aufs_link,
21943+ .unlink = aufs_unlink,
21944+ .symlink = aufs_symlink,
21945+ .mkdir = aufs_mkdir,
21946+ .rmdir = aufs_rmdir,
21947+ .mknod = aufs_mknod,
21948+ .rename = aufs_rename,
21949+
21950+ .permission = aufs_permission,
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+ .atomic_open = aufs_atomic_open,
21965+ .tmpfile = aufs_tmpfile
21966+ },
21967+ [AuIop_OTHER] = {
21968+ .permission = aufs_permission,
21969+#ifdef CONFIG_FS_POSIX_ACL
21970+ .get_acl = aufs_get_acl,
21971+ .set_acl = aufs_set_acl,
21972+#endif
21973+
21974+ .setattr = aufs_setattr,
21975+ .getattr = aufs_getattr,
21976+
21977+#ifdef CONFIG_AUFS_XATTR
21978+ .listxattr = aufs_listxattr,
21979+#endif
21980+
21981+ .update_time = aufs_update_time
21982+ }
21983+};
21984diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
21985--- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
21986+++ linux/fs/aufs/i_op_del.c 2021-06-30 21:35:11.397206648 +0200
21987@@ -0,0 +1,515 @@
21988+// SPDX-License-Identifier: GPL-2.0
21989+/*
21990+ * Copyright (C) 2005-2020 Junjiro R. Okajima
21991+ *
21992+ * This program, aufs is free software; you can redistribute it and/or modify
21993+ * it under the terms of the GNU General Public License as published by
21994+ * the Free Software Foundation; either version 2 of the License, or
21995+ * (at your option) any later version.
21996+ *
21997+ * This program is distributed in the hope that it will be useful,
21998+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
21999+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22000+ * GNU General Public License for more details.
22001+ *
22002+ * You should have received a copy of the GNU General Public License
22003+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
22004+ */
22005+
22006+/*
22007+ * inode operations (del entry)
22008+ */
22009+
22010+#include <linux/iversion.h>
22011+#include "aufs.h"
22012+
22013+/*
22014+ * decide if a new whiteout for @dentry is necessary or not.
22015+ * when it is necessary, prepare the parent dir for the upper branch whose
22016+ * branch index is @bcpup for creation. the actual creation of the whiteout will
22017+ * be done by caller.
22018+ * return value:
22019+ * 0: wh is unnecessary
22020+ * plus: wh is necessary
22021+ * minus: error
22022+ */
22023+int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
22024+{
22025+ int need_wh, err;
22026+ aufs_bindex_t btop;
22027+ struct super_block *sb;
22028+
22029+ sb = dentry->d_sb;
22030+ btop = au_dbtop(dentry);
22031+ if (*bcpup < 0) {
22032+ *bcpup = btop;
22033+ if (au_test_ro(sb, btop, d_inode(dentry))) {
22034+ err = AuWbrCopyup(au_sbi(sb), dentry);
22035+ *bcpup = err;
22036+ if (unlikely(err < 0))
22037+ goto out;
22038+ }
22039+ } else
22040+ AuDebugOn(btop < *bcpup
22041+ || au_test_ro(sb, *bcpup, d_inode(dentry)));
22042+ AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
22043+
22044+ if (*bcpup != btop) {
22045+ err = au_cpup_dirs(dentry, *bcpup);
22046+ if (unlikely(err))
22047+ goto out;
22048+ need_wh = 1;
22049+ } else {
22050+ struct au_dinfo *dinfo, *tmp;
22051+
22052+ need_wh = -ENOMEM;
22053+ dinfo = au_di(dentry);
22054+ tmp = au_di_alloc(sb, AuLsc_DI_TMP);
22055+ if (tmp) {
22056+ au_di_cp(tmp, dinfo);
22057+ au_di_swap(tmp, dinfo);
22058+ /* returns the number of positive dentries */
22059+ need_wh = au_lkup_dentry(dentry, btop + 1,
22060+ /* AuLkup_IGNORE_PERM */ 0);
22061+ au_di_swap(tmp, dinfo);
22062+ au_rw_write_unlock(&tmp->di_rwsem);
22063+ au_di_free(tmp);
22064+ }
22065+ }
22066+ AuDbg("need_wh %d\n", need_wh);
22067+ err = need_wh;
22068+
22069+out:
22070+ return err;
22071+}
22072+
22073+/*
22074+ * simple tests for the del-entry operations.
22075+ * following the checks in vfs, plus the parent-child relationship.
22076+ */
22077+int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
22078+ struct dentry *h_parent, int isdir)
22079+{
22080+ int err;
22081+ umode_t h_mode;
22082+ struct dentry *h_dentry, *h_latest;
22083+ struct inode *h_inode;
22084+ struct user_namespace *h_userns;
22085+
22086+ h_dentry = au_h_dptr(dentry, bindex);
22087+ if (d_really_is_positive(dentry)) {
22088+ err = -ENOENT;
22089+ if (unlikely(d_is_negative(h_dentry)))
22090+ goto out;
22091+ h_inode = d_inode(h_dentry);
22092+ if (unlikely(!h_inode->i_nlink))
22093+ goto out;
22094+
22095+ h_mode = h_inode->i_mode;
22096+ if (!isdir) {
22097+ err = -EISDIR;
22098+ if (unlikely(S_ISDIR(h_mode)))
22099+ goto out;
22100+ } else if (unlikely(!S_ISDIR(h_mode))) {
22101+ err = -ENOTDIR;
22102+ goto out;
22103+ }
22104+ } else {
22105+ /* rename(2) case */
22106+ err = -EIO;
22107+ if (unlikely(d_is_positive(h_dentry)))
22108+ goto out;
22109+ }
22110+
22111+ err = -ENOENT;
22112+ /* expected parent dir is locked */
22113+ if (unlikely(h_parent != h_dentry->d_parent))
22114+ goto out;
22115+ err = 0;
22116+
22117+ /*
22118+ * rmdir a dir may break the consistency on some filesystem.
22119+ * let's try heavy test.
22120+ */
22121+ err = -EACCES;
22122+ h_userns = au_sbr_userns(dentry->d_sb, bindex);
22123+ if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1)
22124+ && au_test_h_perm(h_userns, d_inode(h_parent),
22125+ MAY_EXEC | MAY_WRITE)))
22126+ goto out;
22127+
22128+ h_latest = au_sio_lkup_one(h_userns, &dentry->d_name, h_parent);
22129+ err = -EIO;
22130+ if (IS_ERR(h_latest))
22131+ goto out;
22132+ if (h_latest == h_dentry)
22133+ err = 0;
22134+ dput(h_latest);
22135+
22136+out:
22137+ return err;
22138+}
22139+
22140+/*
22141+ * decide the branch where we operate for @dentry. the branch index will be set
22142+ * @rbcpup. after deciding it, 'pin' it and store the timestamps of the parent
22143+ * dir for reverting.
22144+ * when a new whiteout is necessary, create it.
22145+ */
22146+static struct dentry*
22147+lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
22148+ struct au_dtime *dt, struct au_pin *pin)
22149+{
22150+ struct dentry *wh_dentry;
22151+ struct super_block *sb;
22152+ struct path h_path;
22153+ int err, need_wh;
22154+ unsigned int udba;
22155+ aufs_bindex_t bcpup;
22156+
22157+ need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
22158+ wh_dentry = ERR_PTR(need_wh);
22159+ if (unlikely(need_wh < 0))
22160+ goto out;
22161+
22162+ sb = dentry->d_sb;
22163+ udba = au_opt_udba(sb);
22164+ bcpup = *rbcpup;
22165+ err = au_pin(pin, dentry, bcpup, udba,
22166+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
22167+ wh_dentry = ERR_PTR(err);
22168+ if (unlikely(err))
22169+ goto out;
22170+
22171+ h_path.dentry = au_pinned_h_parent(pin);
22172+ if (udba != AuOpt_UDBA_NONE
22173+ && au_dbtop(dentry) == bcpup) {
22174+ err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
22175+ wh_dentry = ERR_PTR(err);
22176+ if (unlikely(err))
22177+ goto out_unpin;
22178+ }
22179+
22180+ h_path.mnt = au_sbr_mnt(sb, bcpup);
22181+ au_dtime_store(dt, au_pinned_parent(pin), &h_path);
22182+ wh_dentry = NULL;
22183+ if (!need_wh)
22184+ goto out; /* success, no need to create whiteout */
22185+
22186+ wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
22187+ if (IS_ERR(wh_dentry))
22188+ goto out_unpin;
22189+
22190+ /* returns with the parent is locked and wh_dentry is dget-ed */
22191+ goto out; /* success */
22192+
22193+out_unpin:
22194+ au_unpin(pin);
22195+out:
22196+ return wh_dentry;
22197+}
22198+
22199+/*
22200+ * when removing a dir, rename it to a unique temporary whiteout-ed name first
22201+ * in order to be revertible and save time for removing many child whiteouts
22202+ * under the dir.
22203+ * returns 1 when there are too many child whiteout and caller should remove
22204+ * them asynchronously. returns 0 when the number of children is enough small to
22205+ * remove now or the branch fs is a remote fs.
22206+ * otherwise return an error.
22207+ */
22208+static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
22209+ struct au_nhash *whlist, struct inode *dir)
22210+{
22211+ int rmdir_later, err, dirwh;
22212+ struct dentry *h_dentry;
22213+ struct super_block *sb;
22214+ struct inode *inode;
22215+
22216+ sb = dentry->d_sb;
22217+ SiMustAnyLock(sb);
22218+ h_dentry = au_h_dptr(dentry, bindex);
22219+ err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
22220+ if (unlikely(err))
22221+ goto out;
22222+
22223+ /* stop monitoring */
22224+ inode = d_inode(dentry);
22225+ au_hn_free(au_hi(inode, bindex));
22226+
22227+ if (!au_test_fs_remote(h_dentry->d_sb)) {
22228+ dirwh = au_sbi(sb)->si_dirwh;
22229+ rmdir_later = (dirwh <= 1);
22230+ if (!rmdir_later)
22231+ rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
22232+ dirwh);
22233+ if (rmdir_later)
22234+ return rmdir_later;
22235+ }
22236+
22237+ err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
22238+ if (unlikely(err)) {
22239+ AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
22240+ h_dentry, bindex, err);
22241+ err = 0;
22242+ }
22243+
22244+out:
22245+ AuTraceErr(err);
22246+ return err;
22247+}
22248+
22249+/*
22250+ * final procedure for deleting a entry.
22251+ * maintain dentry and iattr.
22252+ */
22253+static void epilog(struct inode *dir, struct dentry *dentry,
22254+ aufs_bindex_t bindex)
22255+{
22256+ struct inode *inode;
22257+
22258+ inode = d_inode(dentry);
22259+ d_drop(dentry);
22260+ inode->i_ctime = dir->i_ctime;
22261+
22262+ au_dir_ts(dir, bindex);
22263+ inode_inc_iversion(dir);
22264+}
22265+
22266+/*
22267+ * when an error happened, remove the created whiteout and revert everything.
22268+ */
22269+static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
22270+ aufs_bindex_t bwh, struct dentry *wh_dentry,
22271+ struct dentry *dentry, struct au_dtime *dt)
22272+{
22273+ int rerr;
22274+ struct path h_path = {
22275+ .dentry = wh_dentry,
22276+ .mnt = au_sbr_mnt(dir->i_sb, bindex)
22277+ };
22278+
22279+ rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
22280+ if (!rerr) {
22281+ au_set_dbwh(dentry, bwh);
22282+ au_dtime_revert(dt);
22283+ return 0;
22284+ }
22285+
22286+ AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
22287+ return -EIO;
22288+}
22289+
22290+/* ---------------------------------------------------------------------- */
22291+
22292+int aufs_unlink(struct inode *dir, struct dentry *dentry)
22293+{
22294+ int err;
22295+ aufs_bindex_t bwh, bindex, btop;
22296+ struct inode *inode, *h_dir, *delegated;
22297+ struct dentry *parent, *wh_dentry;
22298+ /* to reduce stack size */
22299+ struct {
22300+ struct au_dtime dt;
22301+ struct au_pin pin;
22302+ struct path h_path;
22303+ } *a;
22304+
22305+ IMustLock(dir);
22306+
22307+ err = -ENOMEM;
22308+ a = kmalloc(sizeof(*a), GFP_NOFS);
22309+ if (unlikely(!a))
22310+ goto out;
22311+
22312+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
22313+ if (unlikely(err))
22314+ goto out_free;
22315+ err = au_d_hashed_positive(dentry);
22316+ if (unlikely(err))
22317+ goto out_unlock;
22318+ inode = d_inode(dentry);
22319+ IMustLock(inode);
22320+ err = -EISDIR;
22321+ if (unlikely(d_is_dir(dentry)))
22322+ goto out_unlock; /* possible? */
22323+
22324+ btop = au_dbtop(dentry);
22325+ bwh = au_dbwh(dentry);
22326+ bindex = -1;
22327+ parent = dentry->d_parent; /* dir inode is locked */
22328+ di_write_lock_parent(parent);
22329+ wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
22330+ &a->pin);
22331+ err = PTR_ERR(wh_dentry);
22332+ if (IS_ERR(wh_dentry))
22333+ goto out_parent;
22334+
22335+ a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
22336+ a->h_path.dentry = au_h_dptr(dentry, btop);
22337+ dget(a->h_path.dentry);
22338+ if (bindex == btop) {
22339+ h_dir = au_pinned_h_dir(&a->pin);
22340+ delegated = NULL;
22341+ err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
22342+ if (unlikely(err == -EWOULDBLOCK)) {
22343+ pr_warn("cannot retry for NFSv4 delegation"
22344+ " for an internal unlink\n");
22345+ iput(delegated);
22346+ }
22347+ } else {
22348+ /* dir inode is locked */
22349+ h_dir = d_inode(wh_dentry->d_parent);
22350+ IMustLock(h_dir);
22351+ err = 0;
22352+ }
22353+
22354+ if (!err) {
22355+ vfsub_drop_nlink(inode);
22356+ epilog(dir, dentry, bindex);
22357+
22358+ /* update target timestamps */
22359+ if (bindex == btop) {
22360+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
22361+ /*ignore*/
22362+ inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
22363+ } else
22364+ /* todo: this timestamp may be reverted later */
22365+ inode->i_ctime = h_dir->i_ctime;
22366+ goto out_unpin; /* success */
22367+ }
22368+
22369+ /* revert */
22370+ if (wh_dentry) {
22371+ int rerr;
22372+
22373+ rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22374+ &a->dt);
22375+ if (rerr)
22376+ err = rerr;
22377+ }
22378+
22379+out_unpin:
22380+ au_unpin(&a->pin);
22381+ dput(wh_dentry);
22382+ dput(a->h_path.dentry);
22383+out_parent:
22384+ di_write_unlock(parent);
22385+out_unlock:
22386+ aufs_read_unlock(dentry, AuLock_DW);
22387+out_free:
22388+ au_kfree_rcu(a);
22389+out:
22390+ return err;
22391+}
22392+
22393+int aufs_rmdir(struct inode *dir, struct dentry *dentry)
22394+{
22395+ int err, rmdir_later;
22396+ aufs_bindex_t bwh, bindex, btop;
22397+ struct inode *inode;
22398+ struct dentry *parent, *wh_dentry, *h_dentry;
22399+ struct au_whtmp_rmdir *args;
22400+ /* to reduce stack size */
22401+ struct {
22402+ struct au_dtime dt;
22403+ struct au_pin pin;
22404+ } *a;
22405+
22406+ IMustLock(dir);
22407+
22408+ err = -ENOMEM;
22409+ a = kmalloc(sizeof(*a), GFP_NOFS);
22410+ if (unlikely(!a))
22411+ goto out;
22412+
22413+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
22414+ if (unlikely(err))
22415+ goto out_free;
22416+ err = au_alive_dir(dentry);
22417+ if (unlikely(err))
22418+ goto out_unlock;
22419+ inode = d_inode(dentry);
22420+ IMustLock(inode);
22421+ err = -ENOTDIR;
22422+ if (unlikely(!d_is_dir(dentry)))
22423+ goto out_unlock; /* possible? */
22424+
22425+ err = -ENOMEM;
22426+ args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
22427+ if (unlikely(!args))
22428+ goto out_unlock;
22429+
22430+ parent = dentry->d_parent; /* dir inode is locked */
22431+ di_write_lock_parent(parent);
22432+ err = au_test_empty(dentry, &args->whlist);
22433+ if (unlikely(err))
22434+ goto out_parent;
22435+
22436+ btop = au_dbtop(dentry);
22437+ bwh = au_dbwh(dentry);
22438+ bindex = -1;
22439+ wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
22440+ &a->pin);
22441+ err = PTR_ERR(wh_dentry);
22442+ if (IS_ERR(wh_dentry))
22443+ goto out_parent;
22444+
22445+ h_dentry = au_h_dptr(dentry, btop);
22446+ dget(h_dentry);
22447+ rmdir_later = 0;
22448+ if (bindex == btop) {
22449+ err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
22450+ if (err > 0) {
22451+ rmdir_later = err;
22452+ err = 0;
22453+ }
22454+ } else {
22455+ /* stop monitoring */
22456+ au_hn_free(au_hi(inode, btop));
22457+
22458+ /* dir inode is locked */
22459+ IMustLock(d_inode(wh_dentry->d_parent));
22460+ err = 0;
22461+ }
22462+
22463+ if (!err) {
22464+ vfsub_dead_dir(inode);
22465+ au_set_dbdiropq(dentry, -1);
22466+ epilog(dir, dentry, bindex);
22467+
22468+ if (rmdir_later) {
22469+ au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
22470+ args = NULL;
22471+ }
22472+
22473+ goto out_unpin; /* success */
22474+ }
22475+
22476+ /* revert */
22477+ AuLabel(revert);
22478+ if (wh_dentry) {
22479+ int rerr;
22480+
22481+ rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22482+ &a->dt);
22483+ if (rerr)
22484+ err = rerr;
22485+ }
22486+
22487+out_unpin:
22488+ au_unpin(&a->pin);
22489+ dput(wh_dentry);
22490+ dput(h_dentry);
22491+out_parent:
22492+ di_write_unlock(parent);
22493+ if (args)
22494+ au_whtmp_rmdir_free(args);
22495+out_unlock:
22496+ aufs_read_unlock(dentry, AuLock_DW);
22497+out_free:
22498+ au_kfree_rcu(a);
22499+out:
22500+ AuTraceErr(err);
22501+ return err;
22502+}
22503diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
22504--- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
22505+++ linux/fs/aufs/i_op_ren.c 2021-06-30 21:35:11.397206648 +0200
22506@@ -0,0 +1,1251 @@
22507+// SPDX-License-Identifier: GPL-2.0
22508+/*
22509+ * Copyright (C) 2005-2020 Junjiro R. Okajima
22510+ *
22511+ * This program, aufs is free software; you can redistribute it and/or modify
22512+ * it under the terms of the GNU General Public License as published by
22513+ * the Free Software Foundation; either version 2 of the License, or
22514+ * (at your option) any later version.
22515+ *
22516+ * This program is distributed in the hope that it will be useful,
22517+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
22518+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22519+ * GNU General Public License for more details.
22520+ *
22521+ * You should have received a copy of the GNU General Public License
22522+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
22523+ */
22524+
22525+/*
22526+ * inode operation (rename entry)
22527+ * todo: this is crazy monster
22528+ */
22529+
22530+#include <linux/iversion.h>
22531+#include "aufs.h"
22532+
22533+enum { AuSRC, AuDST, AuSrcDst };
22534+enum { AuPARENT, AuCHILD, AuParentChild };
22535+
22536+#define AuRen_ISDIR_SRC 1
22537+#define AuRen_ISDIR_DST (1 << 1)
22538+#define AuRen_ISSAMEDIR (1 << 2)
22539+#define AuRen_WHSRC (1 << 3)
22540+#define AuRen_WHDST (1 << 4)
22541+#define AuRen_MNT_WRITE (1 << 5)
22542+#define AuRen_DT_DSTDIR (1 << 6)
22543+#define AuRen_DIROPQ_SRC (1 << 7)
22544+#define AuRen_DIROPQ_DST (1 << 8)
22545+#define AuRen_DIRREN (1 << 9)
22546+#define AuRen_DROPPED_SRC (1 << 10)
22547+#define AuRen_DROPPED_DST (1 << 11)
22548+#define au_ftest_ren(flags, name) ((flags) & AuRen_##name)
22549+#define au_fset_ren(flags, name) \
22550+ do { (flags) |= AuRen_##name; } while (0)
22551+#define au_fclr_ren(flags, name) \
22552+ do { (flags) &= ~AuRen_##name; } while (0)
22553+
22554+#ifndef CONFIG_AUFS_DIRREN
22555+#undef AuRen_DIRREN
22556+#define AuRen_DIRREN 0
22557+#endif
22558+
22559+struct au_ren_args {
22560+ struct {
22561+ struct dentry *dentry, *h_dentry, *parent, *h_parent,
22562+ *wh_dentry;
22563+ struct inode *dir, *inode;
22564+ struct au_hinode *hdir, *hinode;
22565+ struct au_dtime dt[AuParentChild];
22566+ aufs_bindex_t btop, bdiropq;
22567+ } sd[AuSrcDst];
22568+
22569+#define src_dentry sd[AuSRC].dentry
22570+#define src_dir sd[AuSRC].dir
22571+#define src_inode sd[AuSRC].inode
22572+#define src_h_dentry sd[AuSRC].h_dentry
22573+#define src_parent sd[AuSRC].parent
22574+#define src_h_parent sd[AuSRC].h_parent
22575+#define src_wh_dentry sd[AuSRC].wh_dentry
22576+#define src_hdir sd[AuSRC].hdir
22577+#define src_hinode sd[AuSRC].hinode
22578+#define src_h_dir sd[AuSRC].hdir->hi_inode
22579+#define src_dt sd[AuSRC].dt
22580+#define src_btop sd[AuSRC].btop
22581+#define src_bdiropq sd[AuSRC].bdiropq
22582+
22583+#define dst_dentry sd[AuDST].dentry
22584+#define dst_dir sd[AuDST].dir
22585+#define dst_inode sd[AuDST].inode
22586+#define dst_h_dentry sd[AuDST].h_dentry
22587+#define dst_parent sd[AuDST].parent
22588+#define dst_h_parent sd[AuDST].h_parent
22589+#define dst_wh_dentry sd[AuDST].wh_dentry
22590+#define dst_hdir sd[AuDST].hdir
22591+#define dst_hinode sd[AuDST].hinode
22592+#define dst_h_dir sd[AuDST].hdir->hi_inode
22593+#define dst_dt sd[AuDST].dt
22594+#define dst_btop sd[AuDST].btop
22595+#define dst_bdiropq sd[AuDST].bdiropq
22596+
22597+ struct dentry *h_trap;
22598+ struct au_branch *br;
22599+ struct path h_path;
22600+ struct au_nhash whlist;
22601+ aufs_bindex_t btgt, src_bwh;
22602+
22603+ struct {
22604+ unsigned short auren_flags;
22605+ unsigned char flags; /* syscall parameter */
22606+ unsigned char exchange;
22607+ } __packed;
22608+
22609+ struct au_whtmp_rmdir *thargs;
22610+ struct dentry *h_dst;
22611+ struct au_hinode *h_root;
22612+};
22613+
22614+/* ---------------------------------------------------------------------- */
22615+
22616+/*
22617+ * functions for reverting.
22618+ * when an error happened in a single rename systemcall, we should revert
22619+ * everything as if nothing happened.
22620+ * we don't need to revert the copied-up/down the parent dir since they are
22621+ * harmless.
22622+ */
22623+
22624+#define RevertFailure(fmt, ...) do { \
22625+ AuIOErr("revert failure: " fmt " (%d, %d)\n", \
22626+ ##__VA_ARGS__, err, rerr); \
22627+ err = -EIO; \
22628+} while (0)
22629+
22630+static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
22631+{
22632+ int rerr;
22633+ struct dentry *d;
22634+#define src_or_dst(member) a->sd[idx].member
22635+
22636+ d = src_or_dst(dentry); /* {src,dst}_dentry */
22637+ au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22638+ rerr = au_diropq_remove(d, a->btgt);
22639+ au_hn_inode_unlock(src_or_dst(hinode));
22640+ au_set_dbdiropq(d, src_or_dst(bdiropq));
22641+ if (rerr)
22642+ RevertFailure("remove diropq %pd", d);
22643+
22644+#undef src_or_dst_
22645+}
22646+
22647+static void au_ren_rev_diropq(int err, struct au_ren_args *a)
22648+{
22649+ if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
22650+ au_ren_do_rev_diropq(err, a, AuSRC);
22651+ if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
22652+ au_ren_do_rev_diropq(err, a, AuDST);
22653+}
22654+
22655+static void au_ren_rev_rename(int err, struct au_ren_args *a)
22656+{
22657+ int rerr;
22658+ struct inode *delegated;
22659+
22660+ a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name,
22661+ a->src_h_parent);
22662+ rerr = PTR_ERR(a->h_path.dentry);
22663+ if (IS_ERR(a->h_path.dentry)) {
22664+ RevertFailure("lkup one %pd", a->src_dentry);
22665+ return;
22666+ }
22667+
22668+ delegated = NULL;
22669+ rerr = vfsub_rename(a->dst_h_dir,
22670+ au_h_dptr(a->src_dentry, a->btgt),
22671+ a->src_h_dir, &a->h_path, &delegated, a->flags);
22672+ if (unlikely(rerr == -EWOULDBLOCK)) {
22673+ pr_warn("cannot retry for NFSv4 delegation"
22674+ " for an internal rename\n");
22675+ iput(delegated);
22676+ }
22677+ d_drop(a->h_path.dentry);
22678+ dput(a->h_path.dentry);
22679+ /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
22680+ if (rerr)
22681+ RevertFailure("rename %pd", a->src_dentry);
22682+}
22683+
22684+static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
22685+{
22686+ int rerr;
22687+ struct inode *delegated;
22688+
22689+ a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name,
22690+ a->dst_h_parent);
22691+ rerr = PTR_ERR(a->h_path.dentry);
22692+ if (IS_ERR(a->h_path.dentry)) {
22693+ RevertFailure("lkup one %pd", a->dst_dentry);
22694+ return;
22695+ }
22696+ if (d_is_positive(a->h_path.dentry)) {
22697+ d_drop(a->h_path.dentry);
22698+ dput(a->h_path.dentry);
22699+ return;
22700+ }
22701+
22702+ delegated = NULL;
22703+ rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
22704+ &delegated, a->flags);
22705+ if (unlikely(rerr == -EWOULDBLOCK)) {
22706+ pr_warn("cannot retry for NFSv4 delegation"
22707+ " for an internal rename\n");
22708+ iput(delegated);
22709+ }
22710+ d_drop(a->h_path.dentry);
22711+ dput(a->h_path.dentry);
22712+ if (!rerr)
22713+ au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
22714+ else
22715+ RevertFailure("rename %pd", a->h_dst);
22716+}
22717+
22718+static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
22719+{
22720+ int rerr;
22721+
22722+ a->h_path.dentry = a->src_wh_dentry;
22723+ rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
22724+ au_set_dbwh(a->src_dentry, a->src_bwh);
22725+ if (rerr)
22726+ RevertFailure("unlink %pd", a->src_wh_dentry);
22727+}
22728+#undef RevertFailure
22729+
22730+/* ---------------------------------------------------------------------- */
22731+
22732+/*
22733+ * when we have to copyup the renaming entry, do it with the rename-target name
22734+ * in order to minimize the cost (the later actual rename is unnecessary).
22735+ * otherwise rename it on the target branch.
22736+ */
22737+static int au_ren_or_cpup(struct au_ren_args *a)
22738+{
22739+ int err;
22740+ struct dentry *d;
22741+ struct inode *delegated;
22742+
22743+ d = a->src_dentry;
22744+ if (au_dbtop(d) == a->btgt) {
22745+ a->h_path.dentry = a->dst_h_dentry;
22746+ AuDebugOn(au_dbtop(d) != a->btgt);
22747+ delegated = NULL;
22748+ err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
22749+ a->dst_h_dir, &a->h_path, &delegated,
22750+ a->flags);
22751+ if (unlikely(err == -EWOULDBLOCK)) {
22752+ pr_warn("cannot retry for NFSv4 delegation"
22753+ " for an internal rename\n");
22754+ iput(delegated);
22755+ }
22756+ } else
22757+ BUG();
22758+
22759+ if (!err && a->h_dst)
22760+ /* it will be set to dinfo later */
22761+ dget(a->h_dst);
22762+
22763+ return err;
22764+}
22765+
22766+/* cf. aufs_rmdir() */
22767+static int au_ren_del_whtmp(struct au_ren_args *a)
22768+{
22769+ int err;
22770+ struct inode *dir;
22771+
22772+ dir = a->dst_dir;
22773+ SiMustAnyLock(dir->i_sb);
22774+ if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
22775+ au_sbi(dir->i_sb)->si_dirwh)
22776+ || au_test_fs_remote(a->h_dst->d_sb)) {
22777+ err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
22778+ if (unlikely(err))
22779+ pr_warn("failed removing whtmp dir %pd (%d), "
22780+ "ignored.\n", a->h_dst, err);
22781+ } else {
22782+ au_nhash_wh_free(&a->thargs->whlist);
22783+ a->thargs->whlist = a->whlist;
22784+ a->whlist.nh_num = 0;
22785+ au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
22786+ dput(a->h_dst);
22787+ a->thargs = NULL;
22788+ }
22789+
22790+ return 0;
22791+}
22792+
22793+/* make it 'opaque' dir. */
22794+static int au_ren_do_diropq(struct au_ren_args *a, int idx)
22795+{
22796+ int err;
22797+ struct dentry *d, *diropq;
22798+#define src_or_dst(member) a->sd[idx].member
22799+
22800+ err = 0;
22801+ d = src_or_dst(dentry); /* {src,dst}_dentry */
22802+ src_or_dst(bdiropq) = au_dbdiropq(d);
22803+ src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
22804+ au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22805+ diropq = au_diropq_create(d, a->btgt);
22806+ au_hn_inode_unlock(src_or_dst(hinode));
22807+ if (IS_ERR(diropq))
22808+ err = PTR_ERR(diropq);
22809+ else
22810+ dput(diropq);
22811+
22812+#undef src_or_dst_
22813+ return err;
22814+}
22815+
22816+static int au_ren_diropq(struct au_ren_args *a)
22817+{
22818+ int err;
22819+ unsigned char always;
22820+ struct dentry *d;
22821+
22822+ err = 0;
22823+ d = a->dst_dentry; /* already renamed on the branch */
22824+ always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
22825+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
22826+ && !au_ftest_ren(a->auren_flags, DIRREN)
22827+ && a->btgt != au_dbdiropq(a->src_dentry)
22828+ && (a->dst_wh_dentry
22829+ || a->btgt <= au_dbdiropq(d)
22830+ /* hide the lower to keep xino */
22831+ /* the lowers may not be a dir, but we hide them anyway */
22832+ || a->btgt < au_dbbot(d)
22833+ || always)) {
22834+ AuDbg("here\n");
22835+ err = au_ren_do_diropq(a, AuSRC);
22836+ if (unlikely(err))
22837+ goto out;
22838+ au_fset_ren(a->auren_flags, DIROPQ_SRC);
22839+ }
22840+ if (!a->exchange)
22841+ goto out; /* success */
22842+
22843+ d = a->src_dentry; /* already renamed on the branch */
22844+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)
22845+ && a->btgt != au_dbdiropq(a->dst_dentry)
22846+ && (a->btgt < au_dbdiropq(d)
22847+ || a->btgt < au_dbbot(d)
22848+ || always)) {
22849+ AuDbgDentry(a->src_dentry);
22850+ AuDbgDentry(a->dst_dentry);
22851+ err = au_ren_do_diropq(a, AuDST);
22852+ if (unlikely(err))
22853+ goto out_rev_src;
22854+ au_fset_ren(a->auren_flags, DIROPQ_DST);
22855+ }
22856+ goto out; /* success */
22857+
22858+out_rev_src:
22859+ AuDbg("err %d, reverting src\n", err);
22860+ au_ren_rev_diropq(err, a);
22861+out:
22862+ return err;
22863+}
22864+
22865+static int do_rename(struct au_ren_args *a)
22866+{
22867+ int err;
22868+ struct dentry *d, *h_d;
22869+
22870+ if (!a->exchange) {
22871+ /* prepare workqueue args for asynchronous rmdir */
22872+ h_d = a->dst_h_dentry;
22873+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)
22874+ /* && !au_ftest_ren(a->auren_flags, DIRREN) */
22875+ && d_is_positive(h_d)) {
22876+ err = -ENOMEM;
22877+ a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
22878+ GFP_NOFS);
22879+ if (unlikely(!a->thargs))
22880+ goto out;
22881+ a->h_dst = dget(h_d);
22882+ }
22883+
22884+ /* create whiteout for src_dentry */
22885+ if (au_ftest_ren(a->auren_flags, WHSRC)) {
22886+ a->src_bwh = au_dbwh(a->src_dentry);
22887+ AuDebugOn(a->src_bwh >= 0);
22888+ a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
22889+ a->src_h_parent);
22890+ err = PTR_ERR(a->src_wh_dentry);
22891+ if (IS_ERR(a->src_wh_dentry))
22892+ goto out_thargs;
22893+ }
22894+
22895+ /* lookup whiteout for dentry */
22896+ if (au_ftest_ren(a->auren_flags, WHDST)) {
22897+ h_d = au_wh_lkup(a->dst_h_parent,
22898+ &a->dst_dentry->d_name, a->br);
22899+ err = PTR_ERR(h_d);
22900+ if (IS_ERR(h_d))
22901+ goto out_whsrc;
22902+ if (d_is_negative(h_d))
22903+ dput(h_d);
22904+ else
22905+ a->dst_wh_dentry = h_d;
22906+ }
22907+
22908+ /* rename dentry to tmpwh */
22909+ if (a->thargs) {
22910+ err = au_whtmp_ren(a->dst_h_dentry, a->br);
22911+ if (unlikely(err))
22912+ goto out_whdst;
22913+
22914+ d = a->dst_dentry;
22915+ au_set_h_dptr(d, a->btgt, NULL);
22916+ err = au_lkup_neg(d, a->btgt, /*wh*/0);
22917+ if (unlikely(err))
22918+ goto out_whtmp;
22919+ a->dst_h_dentry = au_h_dptr(d, a->btgt);
22920+ }
22921+ }
22922+
22923+ BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
22924+#if 0 /* debugging */
22925+ BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN)
22926+ && d_is_positive(a->dst_h_dentry)
22927+ && a->src_btop != a->btgt);
22928+#endif
22929+
22930+ /* rename by vfs_rename or cpup */
22931+ err = au_ren_or_cpup(a);
22932+ if (unlikely(err))
22933+ /* leave the copied-up one */
22934+ goto out_whtmp;
22935+
22936+ /* make dir opaque */
22937+ err = au_ren_diropq(a);
22938+ if (unlikely(err))
22939+ goto out_rename;
22940+
22941+ /* update target timestamps */
22942+ if (a->exchange) {
22943+ AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
22944+ a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
22945+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
22946+ a->dst_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
22947+ }
22948+ AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
22949+ a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
22950+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
22951+ a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
22952+
22953+ if (!a->exchange) {
22954+ /* remove whiteout for dentry */
22955+ if (a->dst_wh_dentry) {
22956+ a->h_path.dentry = a->dst_wh_dentry;
22957+ err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
22958+ a->dst_dentry);
22959+ if (unlikely(err))
22960+ goto out_diropq;
22961+ }
22962+
22963+ /* remove whtmp */
22964+ if (a->thargs)
22965+ au_ren_del_whtmp(a); /* ignore this error */
22966+
22967+ au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
22968+ }
22969+ err = 0;
22970+ goto out_success;
22971+
22972+out_diropq:
22973+ au_ren_rev_diropq(err, a);
22974+out_rename:
22975+ au_ren_rev_rename(err, a);
22976+ dput(a->h_dst);
22977+out_whtmp:
22978+ if (a->thargs)
22979+ au_ren_rev_whtmp(err, a);
22980+out_whdst:
22981+ dput(a->dst_wh_dentry);
22982+ a->dst_wh_dentry = NULL;
22983+out_whsrc:
22984+ if (a->src_wh_dentry)
22985+ au_ren_rev_whsrc(err, a);
22986+out_success:
22987+ dput(a->src_wh_dentry);
22988+ dput(a->dst_wh_dentry);
22989+out_thargs:
22990+ if (a->thargs) {
22991+ dput(a->h_dst);
22992+ au_whtmp_rmdir_free(a->thargs);
22993+ a->thargs = NULL;
22994+ }
22995+out:
22996+ return err;
22997+}
22998+
22999+/* ---------------------------------------------------------------------- */
23000+
23001+/*
23002+ * test if @dentry dir can be rename destination or not.
23003+ * success means, it is a logically empty dir.
23004+ */
23005+static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
23006+{
23007+ return au_test_empty(dentry, whlist);
23008+}
23009+
23010+/*
23011+ * test if @a->src_dentry dir can be rename source or not.
23012+ * if it can, return 0.
23013+ * success means,
23014+ * - it is a logically empty dir.
23015+ * - or, it exists on writable branch and has no children including whiteouts
23016+ * on the lower branch unless DIRREN is on.
23017+ */
23018+static int may_rename_srcdir(struct au_ren_args *a)
23019+{
23020+ int err;
23021+ unsigned int rdhash;
23022+ aufs_bindex_t btop, btgt;
23023+ struct dentry *dentry;
23024+ struct super_block *sb;
23025+ struct au_sbinfo *sbinfo;
23026+
23027+ dentry = a->src_dentry;
23028+ sb = dentry->d_sb;
23029+ sbinfo = au_sbi(sb);
23030+ if (au_opt_test(sbinfo->si_mntflags, DIRREN))
23031+ au_fset_ren(a->auren_flags, DIRREN);
23032+
23033+ btgt = a->btgt;
23034+ btop = au_dbtop(dentry);
23035+ if (btop != btgt) {
23036+ struct au_nhash whlist;
23037+
23038+ SiMustAnyLock(sb);
23039+ rdhash = sbinfo->si_rdhash;
23040+ if (!rdhash)
23041+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
23042+ dentry));
23043+ err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
23044+ if (unlikely(err))
23045+ goto out;
23046+ err = au_test_empty(dentry, &whlist);
23047+ au_nhash_wh_free(&whlist);
23048+ goto out;
23049+ }
23050+
23051+ if (btop == au_dbtaildir(dentry))
23052+ return 0; /* success */
23053+
23054+ err = au_test_empty_lower(dentry);
23055+
23056+out:
23057+ if (err == -ENOTEMPTY) {
23058+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
23059+ err = 0;
23060+ } else {
23061+ AuWarn1("renaming dir who has child(ren) on multiple "
23062+ "branches, is not supported\n");
23063+ err = -EXDEV;
23064+ }
23065+ }
23066+ return err;
23067+}
23068+
23069+/* side effect: sets whlist and h_dentry */
23070+static int au_ren_may_dir(struct au_ren_args *a)
23071+{
23072+ int err;
23073+ unsigned int rdhash;
23074+ struct dentry *d;
23075+
23076+ d = a->dst_dentry;
23077+ SiMustAnyLock(d->d_sb);
23078+
23079+ err = 0;
23080+ if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
23081+ rdhash = au_sbi(d->d_sb)->si_rdhash;
23082+ if (!rdhash)
23083+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
23084+ err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
23085+ if (unlikely(err))
23086+ goto out;
23087+
23088+ if (!a->exchange) {
23089+ au_set_dbtop(d, a->dst_btop);
23090+ err = may_rename_dstdir(d, &a->whlist);
23091+ au_set_dbtop(d, a->btgt);
23092+ } else
23093+ err = may_rename_srcdir(a);
23094+ }
23095+ a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
23096+ if (unlikely(err))
23097+ goto out;
23098+
23099+ d = a->src_dentry;
23100+ a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
23101+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
23102+ err = may_rename_srcdir(a);
23103+ if (unlikely(err)) {
23104+ au_nhash_wh_free(&a->whlist);
23105+ a->whlist.nh_num = 0;
23106+ }
23107+ }
23108+out:
23109+ return err;
23110+}
23111+
23112+/* ---------------------------------------------------------------------- */
23113+
23114+/*
23115+ * simple tests for rename.
23116+ * following the checks in vfs, plus the parent-child relationship.
23117+ */
23118+static int au_may_ren(struct au_ren_args *a)
23119+{
23120+ int err, isdir;
23121+ struct inode *h_inode;
23122+
23123+ if (a->src_btop == a->btgt) {
23124+ err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
23125+ au_ftest_ren(a->auren_flags, ISDIR_SRC));
23126+ if (unlikely(err))
23127+ goto out;
23128+ err = -EINVAL;
23129+ if (unlikely(a->src_h_dentry == a->h_trap))
23130+ goto out;
23131+ }
23132+
23133+ err = 0;
23134+ if (a->dst_btop != a->btgt)
23135+ goto out;
23136+
23137+ err = -ENOTEMPTY;
23138+ if (unlikely(a->dst_h_dentry == a->h_trap))
23139+ goto out;
23140+
23141+ err = -EIO;
23142+ isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
23143+ if (d_really_is_negative(a->dst_dentry)) {
23144+ if (d_is_negative(a->dst_h_dentry))
23145+ err = au_may_add(a->dst_dentry, a->btgt,
23146+ a->dst_h_parent, isdir);
23147+ } else {
23148+ if (unlikely(d_is_negative(a->dst_h_dentry)))
23149+ goto out;
23150+ h_inode = d_inode(a->dst_h_dentry);
23151+ if (h_inode->i_nlink)
23152+ err = au_may_del(a->dst_dentry, a->btgt,
23153+ a->dst_h_parent, isdir);
23154+ }
23155+
23156+out:
23157+ if (unlikely(err == -ENOENT || err == -EEXIST))
23158+ err = -EIO;
23159+ AuTraceErr(err);
23160+ return err;
23161+}
23162+
23163+/* ---------------------------------------------------------------------- */
23164+
23165+/*
23166+ * locking order
23167+ * (VFS)
23168+ * - src_dir and dir by lock_rename()
23169+ * - inode if exists
23170+ * (aufs)
23171+ * - lock all
23172+ * + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
23173+ * + si_read_lock
23174+ * + di_write_lock2_child()
23175+ * + di_write_lock_child()
23176+ * + ii_write_lock_child()
23177+ * + di_write_lock_child2()
23178+ * + ii_write_lock_child2()
23179+ * + src_parent and parent
23180+ * + di_write_lock_parent()
23181+ * + ii_write_lock_parent()
23182+ * + di_write_lock_parent2()
23183+ * + ii_write_lock_parent2()
23184+ * + lower src_dir and dir by vfsub_lock_rename()
23185+ * + verify the every relationships between child and parent. if any
23186+ * of them failed, unlock all and return -EBUSY.
23187+ */
23188+static void au_ren_unlock(struct au_ren_args *a)
23189+{
23190+ vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
23191+ a->dst_h_parent, a->dst_hdir);
23192+ if (au_ftest_ren(a->auren_flags, DIRREN)
23193+ && a->h_root)
23194+ au_hn_inode_unlock(a->h_root);
23195+ if (au_ftest_ren(a->auren_flags, MNT_WRITE))
23196+ vfsub_mnt_drop_write(au_br_mnt(a->br));
23197+}
23198+
23199+static int au_ren_lock(struct au_ren_args *a)
23200+{
23201+ int err;
23202+ unsigned int udba;
23203+
23204+ err = 0;
23205+ a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
23206+ a->src_hdir = au_hi(a->src_dir, a->btgt);
23207+ a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
23208+ a->dst_hdir = au_hi(a->dst_dir, a->btgt);
23209+
23210+ err = vfsub_mnt_want_write(au_br_mnt(a->br));
23211+ if (unlikely(err))
23212+ goto out;
23213+ au_fset_ren(a->auren_flags, MNT_WRITE);
23214+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
23215+ struct dentry *root;
23216+ struct inode *dir;
23217+
23218+ /*
23219+ * sbinfo is already locked, so this ii_read_lock is
23220+ * unnecessary. but our debugging feature checks it.
23221+ */
23222+ root = a->src_inode->i_sb->s_root;
23223+ if (root != a->src_parent && root != a->dst_parent) {
23224+ dir = d_inode(root);
23225+ ii_read_lock_parent3(dir);
23226+ a->h_root = au_hi(dir, a->btgt);
23227+ ii_read_unlock(dir);
23228+ au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3);
23229+ }
23230+ }
23231+ a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
23232+ a->dst_h_parent, a->dst_hdir);
23233+ udba = au_opt_udba(a->src_dentry->d_sb);
23234+ if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
23235+ || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
23236+ err = au_busy_or_stale();
23237+ if (!err && au_dbtop(a->src_dentry) == a->btgt)
23238+ err = au_h_verify(a->src_h_dentry, udba,
23239+ d_inode(a->src_h_parent), a->src_h_parent,
23240+ a->br);
23241+ if (!err && au_dbtop(a->dst_dentry) == a->btgt)
23242+ err = au_h_verify(a->dst_h_dentry, udba,
23243+ d_inode(a->dst_h_parent), a->dst_h_parent,
23244+ a->br);
23245+ if (!err)
23246+ goto out; /* success */
23247+
23248+ err = au_busy_or_stale();
23249+ au_ren_unlock(a);
23250+
23251+out:
23252+ return err;
23253+}
23254+
23255+/* ---------------------------------------------------------------------- */
23256+
23257+static void au_ren_refresh_dir(struct au_ren_args *a)
23258+{
23259+ struct inode *dir;
23260+
23261+ dir = a->dst_dir;
23262+ inode_inc_iversion(dir);
23263+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
23264+ /* is this updating defined in POSIX? */
23265+ au_cpup_attr_timesizes(a->src_inode);
23266+ au_cpup_attr_nlink(dir, /*force*/1);
23267+ }
23268+ au_dir_ts(dir, a->btgt);
23269+
23270+ if (a->exchange) {
23271+ dir = a->src_dir;
23272+ inode_inc_iversion(dir);
23273+ if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
23274+ /* is this updating defined in POSIX? */
23275+ au_cpup_attr_timesizes(a->dst_inode);
23276+ au_cpup_attr_nlink(dir, /*force*/1);
23277+ }
23278+ au_dir_ts(dir, a->btgt);
23279+ }
23280+
23281+ if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
23282+ return;
23283+
23284+ dir = a->src_dir;
23285+ inode_inc_iversion(dir);
23286+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
23287+ au_cpup_attr_nlink(dir, /*force*/1);
23288+ au_dir_ts(dir, a->btgt);
23289+}
23290+
23291+static void au_ren_refresh(struct au_ren_args *a)
23292+{
23293+ aufs_bindex_t bbot, bindex;
23294+ struct dentry *d, *h_d;
23295+ struct inode *i, *h_i;
23296+ struct super_block *sb;
23297+
23298+ d = a->dst_dentry;
23299+ d_drop(d);
23300+ if (a->h_dst)
23301+ /* already dget-ed by au_ren_or_cpup() */
23302+ au_set_h_dptr(d, a->btgt, a->h_dst);
23303+
23304+ i = a->dst_inode;
23305+ if (i) {
23306+ if (!a->exchange) {
23307+ if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
23308+ vfsub_drop_nlink(i);
23309+ else {
23310+ vfsub_dead_dir(i);
23311+ au_cpup_attr_timesizes(i);
23312+ }
23313+ au_update_dbrange(d, /*do_put_zero*/1);
23314+ } else
23315+ au_cpup_attr_nlink(i, /*force*/1);
23316+ } else {
23317+ bbot = a->btgt;
23318+ for (bindex = au_dbtop(d); bindex < bbot; bindex++)
23319+ au_set_h_dptr(d, bindex, NULL);
23320+ bbot = au_dbbot(d);
23321+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
23322+ au_set_h_dptr(d, bindex, NULL);
23323+ au_update_dbrange(d, /*do_put_zero*/0);
23324+ }
23325+
23326+ if (a->exchange
23327+ || au_ftest_ren(a->auren_flags, DIRREN)) {
23328+ d_drop(a->src_dentry);
23329+ if (au_ftest_ren(a->auren_flags, DIRREN))
23330+ au_set_dbwh(a->src_dentry, -1);
23331+ return;
23332+ }
23333+
23334+ d = a->src_dentry;
23335+ au_set_dbwh(d, -1);
23336+ bbot = au_dbbot(d);
23337+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23338+ h_d = au_h_dptr(d, bindex);
23339+ if (h_d)
23340+ au_set_h_dptr(d, bindex, NULL);
23341+ }
23342+ au_set_dbbot(d, a->btgt);
23343+
23344+ sb = d->d_sb;
23345+ i = a->src_inode;
23346+ if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
23347+ return; /* success */
23348+
23349+ bbot = au_ibbot(i);
23350+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23351+ h_i = au_h_iptr(i, bindex);
23352+ if (h_i) {
23353+ au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
23354+ /* ignore this error */
23355+ au_set_h_iptr(i, bindex, NULL, 0);
23356+ }
23357+ }
23358+ au_set_ibbot(i, a->btgt);
23359+}
23360+
23361+/* ---------------------------------------------------------------------- */
23362+
23363+/* mainly for link(2) and rename(2) */
23364+int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
23365+{
23366+ aufs_bindex_t bdiropq, bwh;
23367+ struct dentry *parent;
23368+ struct au_branch *br;
23369+
23370+ parent = dentry->d_parent;
23371+ IMustLock(d_inode(parent)); /* dir is locked */
23372+
23373+ bdiropq = au_dbdiropq(parent);
23374+ bwh = au_dbwh(dentry);
23375+ br = au_sbr(dentry->d_sb, btgt);
23376+ if (au_br_rdonly(br)
23377+ || (0 <= bdiropq && bdiropq < btgt)
23378+ || (0 <= bwh && bwh < btgt))
23379+ btgt = -1;
23380+
23381+ AuDbg("btgt %d\n", btgt);
23382+ return btgt;
23383+}
23384+
23385+/* sets src_btop, dst_btop and btgt */
23386+static int au_ren_wbr(struct au_ren_args *a)
23387+{
23388+ int err;
23389+ struct au_wr_dir_args wr_dir_args = {
23390+ /* .force_btgt = -1, */
23391+ .flags = AuWrDir_ADD_ENTRY
23392+ };
23393+
23394+ a->src_btop = au_dbtop(a->src_dentry);
23395+ a->dst_btop = au_dbtop(a->dst_dentry);
23396+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
23397+ || au_ftest_ren(a->auren_flags, ISDIR_DST))
23398+ au_fset_wrdir(wr_dir_args.flags, ISDIR);
23399+ wr_dir_args.force_btgt = a->src_btop;
23400+ if (a->dst_inode && a->dst_btop < a->src_btop)
23401+ wr_dir_args.force_btgt = a->dst_btop;
23402+ wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
23403+ err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
23404+ a->btgt = err;
23405+ if (a->exchange)
23406+ au_update_dbtop(a->dst_dentry);
23407+
23408+ return err;
23409+}
23410+
23411+static void au_ren_dt(struct au_ren_args *a)
23412+{
23413+ a->h_path.dentry = a->src_h_parent;
23414+ au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
23415+ if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
23416+ a->h_path.dentry = a->dst_h_parent;
23417+ au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
23418+ }
23419+
23420+ au_fclr_ren(a->auren_flags, DT_DSTDIR);
23421+ if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
23422+ && !a->exchange)
23423+ return;
23424+
23425+ a->h_path.dentry = a->src_h_dentry;
23426+ au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
23427+ if (d_is_positive(a->dst_h_dentry)) {
23428+ au_fset_ren(a->auren_flags, DT_DSTDIR);
23429+ a->h_path.dentry = a->dst_h_dentry;
23430+ au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
23431+ }
23432+}
23433+
23434+static void au_ren_rev_dt(int err, struct au_ren_args *a)
23435+{
23436+ struct dentry *h_d;
23437+ struct inode *h_inode;
23438+
23439+ au_dtime_revert(a->src_dt + AuPARENT);
23440+ if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
23441+ au_dtime_revert(a->dst_dt + AuPARENT);
23442+
23443+ if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
23444+ h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
23445+ h_inode = d_inode(h_d);
23446+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
23447+ au_dtime_revert(a->src_dt + AuCHILD);
23448+ inode_unlock(h_inode);
23449+
23450+ if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
23451+ h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
23452+ h_inode = d_inode(h_d);
23453+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
23454+ au_dtime_revert(a->dst_dt + AuCHILD);
23455+ inode_unlock(h_inode);
23456+ }
23457+ }
23458+}
23459+
23460+/* ---------------------------------------------------------------------- */
23461+
23462+int aufs_rename(struct user_namespace *userns,
23463+ struct inode *_src_dir, struct dentry *_src_dentry,
23464+ struct inode *_dst_dir, struct dentry *_dst_dentry,
23465+ unsigned int _flags)
23466+{
23467+ int err, lock_flags;
23468+ void *rev;
23469+ /* reduce stack space */
23470+ struct au_ren_args *a;
23471+ struct au_pin pin;
23472+
23473+ AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
23474+ IMustLock(_src_dir);
23475+ IMustLock(_dst_dir);
23476+
23477+ err = -EINVAL;
23478+ if (unlikely(_flags & RENAME_WHITEOUT))
23479+ goto out;
23480+
23481+ err = -ENOMEM;
23482+ BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
23483+ a = kzalloc(sizeof(*a), GFP_NOFS);
23484+ if (unlikely(!a))
23485+ goto out;
23486+
23487+ a->flags = _flags;
23488+ BUILD_BUG_ON(sizeof(a->exchange) == sizeof(u8)
23489+ && RENAME_EXCHANGE > U8_MAX);
23490+ a->exchange = _flags & RENAME_EXCHANGE;
23491+ a->src_dir = _src_dir;
23492+ a->src_dentry = _src_dentry;
23493+ a->src_inode = NULL;
23494+ if (d_really_is_positive(a->src_dentry))
23495+ a->src_inode = d_inode(a->src_dentry);
23496+ a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
23497+ a->dst_dir = _dst_dir;
23498+ a->dst_dentry = _dst_dentry;
23499+ a->dst_inode = NULL;
23500+ if (d_really_is_positive(a->dst_dentry))
23501+ a->dst_inode = d_inode(a->dst_dentry);
23502+ a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
23503+ if (a->dst_inode) {
23504+ /*
23505+ * if EXCHANGE && src is non-dir && dst is dir,
23506+ * dst is not locked.
23507+ */
23508+ /* IMustLock(a->dst_inode); */
23509+ au_igrab(a->dst_inode);
23510+ }
23511+
23512+ err = -ENOTDIR;
23513+ lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
23514+ if (d_is_dir(a->src_dentry)) {
23515+ au_fset_ren(a->auren_flags, ISDIR_SRC);
23516+ if (unlikely(!a->exchange
23517+ && d_really_is_positive(a->dst_dentry)
23518+ && !d_is_dir(a->dst_dentry)))
23519+ goto out_free;
23520+ lock_flags |= AuLock_DIRS;
23521+ }
23522+ if (a->dst_inode && d_is_dir(a->dst_dentry)) {
23523+ au_fset_ren(a->auren_flags, ISDIR_DST);
23524+ if (unlikely(!a->exchange
23525+ && d_really_is_positive(a->src_dentry)
23526+ && !d_is_dir(a->src_dentry)))
23527+ goto out_free;
23528+ lock_flags |= AuLock_DIRS;
23529+ }
23530+ err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
23531+ lock_flags);
23532+ if (unlikely(err))
23533+ goto out_free;
23534+
23535+ err = au_d_hashed_positive(a->src_dentry);
23536+ if (unlikely(err))
23537+ goto out_unlock;
23538+ err = -ENOENT;
23539+ if (a->dst_inode) {
23540+ /*
23541+ * If it is a dir, VFS unhash it before this
23542+ * function. It means we cannot rely upon d_unhashed().
23543+ */
23544+ if (unlikely(!a->dst_inode->i_nlink))
23545+ goto out_unlock;
23546+ if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
23547+ err = au_d_hashed_positive(a->dst_dentry);
23548+ if (unlikely(err && !a->exchange))
23549+ goto out_unlock;
23550+ } else if (unlikely(IS_DEADDIR(a->dst_inode)))
23551+ goto out_unlock;
23552+ } else if (unlikely(d_unhashed(a->dst_dentry)))
23553+ goto out_unlock;
23554+
23555+ /*
23556+ * is it possible?
23557+ * yes, it happened (in linux-3.3-rcN) but I don't know why.
23558+ * there may exist a problem somewhere else.
23559+ */
23560+ err = -EINVAL;
23561+ if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
23562+ goto out_unlock;
23563+
23564+ au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
23565+ di_write_lock_parent(a->dst_parent);
23566+
23567+ /* which branch we process */
23568+ err = au_ren_wbr(a);
23569+ if (unlikely(err < 0))
23570+ goto out_parent;
23571+ a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
23572+ a->h_path.mnt = au_br_mnt(a->br);
23573+
23574+ /* are they available to be renamed */
23575+ err = au_ren_may_dir(a);
23576+ if (unlikely(err))
23577+ goto out_children;
23578+
23579+ /* prepare the writable parent dir on the same branch */
23580+ if (a->dst_btop == a->btgt) {
23581+ au_fset_ren(a->auren_flags, WHDST);
23582+ } else {
23583+ err = au_cpup_dirs(a->dst_dentry, a->btgt);
23584+ if (unlikely(err))
23585+ goto out_children;
23586+ }
23587+
23588+ err = 0;
23589+ if (!a->exchange) {
23590+ if (a->src_dir != a->dst_dir) {
23591+ /*
23592+ * this temporary unlock is safe,
23593+ * because both dir->i_mutex are locked.
23594+ */
23595+ di_write_unlock(a->dst_parent);
23596+ di_write_lock_parent(a->src_parent);
23597+ err = au_wr_dir_need_wh(a->src_dentry,
23598+ au_ftest_ren(a->auren_flags,
23599+ ISDIR_SRC),
23600+ &a->btgt);
23601+ di_write_unlock(a->src_parent);
23602+ di_write_lock2_parent(a->src_parent, a->dst_parent,
23603+ /*isdir*/1);
23604+ au_fclr_ren(a->auren_flags, ISSAMEDIR);
23605+ } else
23606+ err = au_wr_dir_need_wh(a->src_dentry,
23607+ au_ftest_ren(a->auren_flags,
23608+ ISDIR_SRC),
23609+ &a->btgt);
23610+ }
23611+ if (unlikely(err < 0))
23612+ goto out_children;
23613+ if (err)
23614+ au_fset_ren(a->auren_flags, WHSRC);
23615+
23616+ /* cpup src */
23617+ if (a->src_btop != a->btgt) {
23618+ err = au_pin(&pin, a->src_dentry, a->btgt,
23619+ au_opt_udba(a->src_dentry->d_sb),
23620+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23621+ if (!err) {
23622+ struct au_cp_generic cpg = {
23623+ .dentry = a->src_dentry,
23624+ .bdst = a->btgt,
23625+ .bsrc = a->src_btop,
23626+ .len = -1,
23627+ .pin = &pin,
23628+ .flags = AuCpup_DTIME | AuCpup_HOPEN
23629+ };
23630+ AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
23631+ err = au_sio_cpup_simple(&cpg);
23632+ au_unpin(&pin);
23633+ }
23634+ if (unlikely(err))
23635+ goto out_children;
23636+ a->src_btop = a->btgt;
23637+ a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
23638+ if (!a->exchange)
23639+ au_fset_ren(a->auren_flags, WHSRC);
23640+ }
23641+
23642+ /* cpup dst */
23643+ if (a->exchange && a->dst_inode
23644+ && a->dst_btop != a->btgt) {
23645+ err = au_pin(&pin, a->dst_dentry, a->btgt,
23646+ au_opt_udba(a->dst_dentry->d_sb),
23647+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23648+ if (!err) {
23649+ struct au_cp_generic cpg = {
23650+ .dentry = a->dst_dentry,
23651+ .bdst = a->btgt,
23652+ .bsrc = a->dst_btop,
23653+ .len = -1,
23654+ .pin = &pin,
23655+ .flags = AuCpup_DTIME | AuCpup_HOPEN
23656+ };
23657+ err = au_sio_cpup_simple(&cpg);
23658+ au_unpin(&pin);
23659+ }
23660+ if (unlikely(err))
23661+ goto out_children;
23662+ a->dst_btop = a->btgt;
23663+ a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
23664+ }
23665+
23666+ /* lock them all */
23667+ err = au_ren_lock(a);
23668+ if (unlikely(err))
23669+ /* leave the copied-up one */
23670+ goto out_children;
23671+
23672+ if (!a->exchange) {
23673+ if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
23674+ err = au_may_ren(a);
23675+ else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
23676+ err = -ENAMETOOLONG;
23677+ if (unlikely(err))
23678+ goto out_hdir;
23679+ }
23680+
23681+ /* store timestamps to be revertible */
23682+ au_ren_dt(a);
23683+
23684+ /* store dirren info */
23685+ if (au_ftest_ren(a->auren_flags, DIRREN)) {
23686+ err = au_dr_rename(a->src_dentry, a->btgt,
23687+ &a->dst_dentry->d_name, &rev);
23688+ AuTraceErr(err);
23689+ if (unlikely(err))
23690+ goto out_dt;
23691+ }
23692+
23693+ /* here we go */
23694+ err = do_rename(a);
23695+ if (unlikely(err))
23696+ goto out_dirren;
23697+
23698+ if (au_ftest_ren(a->auren_flags, DIRREN))
23699+ au_dr_rename_fin(a->src_dentry, a->btgt, rev);
23700+
23701+ /* update dir attributes */
23702+ au_ren_refresh_dir(a);
23703+
23704+ /* dput/iput all lower dentries */
23705+ au_ren_refresh(a);
23706+
23707+ goto out_hdir; /* success */
23708+
23709+out_dirren:
23710+ if (au_ftest_ren(a->auren_flags, DIRREN))
23711+ au_dr_rename_rev(a->src_dentry, a->btgt, rev);
23712+out_dt:
23713+ au_ren_rev_dt(err, a);
23714+out_hdir:
23715+ au_ren_unlock(a);
23716+out_children:
23717+ au_nhash_wh_free(&a->whlist);
23718+ if (err && a->dst_inode && a->dst_btop != a->btgt) {
23719+ AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
23720+ au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
23721+ au_set_dbtop(a->dst_dentry, a->dst_btop);
23722+ }
23723+out_parent:
23724+ if (!err) {
23725+ if (d_unhashed(a->src_dentry))
23726+ au_fset_ren(a->auren_flags, DROPPED_SRC);
23727+ if (d_unhashed(a->dst_dentry))
23728+ au_fset_ren(a->auren_flags, DROPPED_DST);
23729+ if (!a->exchange)
23730+ d_move(a->src_dentry, a->dst_dentry);
23731+ else {
23732+ d_exchange(a->src_dentry, a->dst_dentry);
23733+ if (au_ftest_ren(a->auren_flags, DROPPED_DST))
23734+ d_drop(a->dst_dentry);
23735+ }
23736+ if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
23737+ d_drop(a->src_dentry);
23738+ } else {
23739+ au_update_dbtop(a->dst_dentry);
23740+ if (!a->dst_inode)
23741+ d_drop(a->dst_dentry);
23742+ }
23743+ if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
23744+ di_write_unlock(a->dst_parent);
23745+ else
23746+ di_write_unlock2(a->src_parent, a->dst_parent);
23747+out_unlock:
23748+ aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
23749+out_free:
23750+ iput(a->dst_inode);
23751+ if (a->thargs)
23752+ au_whtmp_rmdir_free(a->thargs);
23753+ au_kfree_rcu(a);
23754+out:
23755+ AuTraceErr(err);
23756+ return err;
23757+}
23758diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
23759--- /usr/share/empty/fs/aufs/Kconfig 1970-01-01 01:00:00.000000000 +0100
23760+++ linux/fs/aufs/Kconfig 2021-02-24 13:33:42.741013619 +0100
23761@@ -0,0 +1,199 @@
23762+# SPDX-License-Identifier: GPL-2.0
23763+config AUFS_FS
23764+ tristate "Aufs (Advanced multi layered unification filesystem) support"
23765+ help
23766+ Aufs is a stackable unification filesystem such as Unionfs,
23767+ which unifies several directories and provides a merged single
23768+ directory.
23769+ In the early days, aufs was entirely re-designed and
23770+ re-implemented Unionfs Version 1.x series. Introducing many
23771+ original ideas, approaches and improvements, it becomes totally
23772+ different from Unionfs while keeping the basic features.
23773+
23774+if AUFS_FS
23775+choice
23776+ prompt "Maximum number of branches"
23777+ default AUFS_BRANCH_MAX_127
23778+ help
23779+ Specifies the maximum number of branches (or member directories)
23780+ in a single aufs. The larger value consumes more system
23781+ resources and has a minor impact to performance.
23782+config AUFS_BRANCH_MAX_127
23783+ bool "127"
23784+ help
23785+ Specifies the maximum number of branches (or member directories)
23786+ in a single aufs. The larger value consumes more system
23787+ resources and has a minor impact to performance.
23788+config AUFS_BRANCH_MAX_511
23789+ bool "511"
23790+ help
23791+ Specifies the maximum number of branches (or member directories)
23792+ in a single aufs. The larger value consumes more system
23793+ resources and has a minor impact to performance.
23794+config AUFS_BRANCH_MAX_1023
23795+ bool "1023"
23796+ help
23797+ Specifies the maximum number of branches (or member directories)
23798+ in a single aufs. The larger value consumes more system
23799+ resources and has a minor impact to performance.
23800+config AUFS_BRANCH_MAX_32767
23801+ bool "32767"
23802+ help
23803+ Specifies the maximum number of branches (or member directories)
23804+ in a single aufs. The larger value consumes more system
23805+ resources and has a minor impact to performance.
23806+endchoice
23807+
23808+config AUFS_SBILIST
23809+ bool
23810+ depends on AUFS_MAGIC_SYSRQ || PROC_FS
23811+ default y
23812+ help
23813+ Automatic configuration for internal use.
23814+ When aufs supports Magic SysRq or /proc, enabled automatically.
23815+
23816+config AUFS_HNOTIFY
23817+ bool "Detect direct branch access (bypassing aufs)"
23818+ help
23819+ If you want to modify files on branches directly, eg. bypassing aufs,
23820+ and want aufs to detect the changes of them fully, then enable this
23821+ option and use 'udba=notify' mount option.
23822+ Currently there is only one available configuration, "fsnotify".
23823+ It will have a negative impact to the performance.
23824+ See detail in aufs.5.
23825+
23826+choice
23827+ prompt "method" if AUFS_HNOTIFY
23828+ default AUFS_HFSNOTIFY
23829+config AUFS_HFSNOTIFY
23830+ bool "fsnotify"
23831+ select FSNOTIFY
23832+endchoice
23833+
23834+config AUFS_EXPORT
23835+ bool "NFS-exportable aufs"
23836+ depends on EXPORTFS
23837+ help
23838+ If you want to export your mounted aufs via NFS, then enable this
23839+ option. There are several requirements for this configuration.
23840+ See detail in aufs.5.
23841+
23842+config AUFS_INO_T_64
23843+ bool
23844+ depends on AUFS_EXPORT
23845+ depends on 64BIT && !(ALPHA || S390)
23846+ default y
23847+ help
23848+ Automatic configuration for internal use.
23849+ /* typedef unsigned long/int __kernel_ino_t */
23850+ /* alpha and s390x are int */
23851+
23852+config AUFS_XATTR
23853+ bool "support for XATTR/EA (including Security Labels)"
23854+ help
23855+ If your branch fs supports XATTR/EA and you want to make them
23856+ available in aufs too, then enable this opsion and specify the
23857+ branch attributes for EA.
23858+ See detail in aufs.5.
23859+
23860+config AUFS_FHSM
23861+ bool "File-based Hierarchical Storage Management"
23862+ help
23863+ Hierarchical Storage Management (or HSM) is a well-known feature
23864+ in the storage world. Aufs provides this feature as file-based.
23865+ with multiple branches.
23866+ These multiple branches are prioritized, ie. the topmost one
23867+ should be the fastest drive and be used heavily.
23868+
23869+config AUFS_RDU
23870+ bool "Readdir in userspace"
23871+ help
23872+ Aufs has two methods to provide a merged view for a directory,
23873+ by a user-space library and by kernel-space natively. The latter
23874+ is always enabled but sometimes large and slow.
23875+ If you enable this option, install the library in aufs2-util
23876+ package, and set some environment variables for your readdir(3),
23877+ then the work will be handled in user-space which generally
23878+ shows better performance in most cases.
23879+ See detail in aufs.5.
23880+
23881+config AUFS_DIRREN
23882+ bool "Workaround for rename(2)-ing a directory"
23883+ help
23884+ By default, aufs returns EXDEV error in renameing a dir who has
23885+ his child on the lower branch, since it is a bad idea to issue
23886+ rename(2) internally for every lower branch. But user may not
23887+ accept this behaviour. So here is a workaround to allow such
23888+ rename(2) and store some extra infromation on the writable
23889+ branch. Obviously this costs high (and I don't like it).
23890+ To use this feature, you need to enable this configuration AND
23891+ to specify the mount option `dirren.'
23892+ See details in aufs.5 and the design documents.
23893+
23894+config AUFS_SHWH
23895+ bool "Show whiteouts"
23896+ help
23897+ If you want to make the whiteouts in aufs visible, then enable
23898+ this option and specify 'shwh' mount option. Although it may
23899+ sounds like philosophy or something, but in technically it
23900+ simply shows the name of whiteout with keeping its behaviour.
23901+
23902+config AUFS_BR_RAMFS
23903+ bool "Ramfs (initramfs/rootfs) as an aufs branch"
23904+ help
23905+ If you want to use ramfs as an aufs branch fs, then enable this
23906+ option. Generally tmpfs is recommended.
23907+ Aufs prohibited them to be a branch fs by default, because
23908+ initramfs becomes unusable after switch_root or something
23909+ generally. If you sets initramfs as an aufs branch and boot your
23910+ system by switch_root, you will meet a problem easily since the
23911+ files in initramfs may be inaccessible.
23912+ Unless you are going to use ramfs as an aufs branch fs without
23913+ switch_root or something, leave it N.
23914+
23915+config AUFS_BR_FUSE
23916+ bool "Fuse fs as an aufs branch"
23917+ depends on FUSE_FS
23918+ select AUFS_POLL
23919+ help
23920+ If you want to use fuse-based userspace filesystem as an aufs
23921+ branch fs, then enable this option.
23922+ It implements the internal poll(2) operation which is
23923+ implemented by fuse only (curretnly).
23924+
23925+config AUFS_POLL
23926+ bool
23927+ help
23928+ Automatic configuration for internal use.
23929+
23930+config AUFS_BR_HFSPLUS
23931+ bool "Hfsplus as an aufs branch"
23932+ depends on HFSPLUS_FS
23933+ default y
23934+ help
23935+ If you want to use hfsplus fs as an aufs branch fs, then enable
23936+ this option. This option introduces a small overhead at
23937+ copying-up a file on hfsplus.
23938+
23939+config AUFS_BDEV_LOOP
23940+ bool
23941+ depends on BLK_DEV_LOOP
23942+ default y
23943+ help
23944+ Automatic configuration for internal use.
23945+ Convert =[ym] into =y.
23946+
23947+config AUFS_DEBUG
23948+ bool "Debug aufs"
23949+ help
23950+ Enable this to compile aufs internal debug code.
23951+ It will have a negative impact to the performance.
23952+
23953+config AUFS_MAGIC_SYSRQ
23954+ bool
23955+ depends on AUFS_DEBUG && MAGIC_SYSRQ
23956+ default y
23957+ help
23958+ Automatic configuration for internal use.
23959+ When aufs supports Magic SysRq, enabled automatically.
23960+endif
23961diff -urN /usr/share/empty/fs/aufs/lcnt.h linux/fs/aufs/lcnt.h
23962--- /usr/share/empty/fs/aufs/lcnt.h 1970-01-01 01:00:00.000000000 +0100
23963+++ linux/fs/aufs/lcnt.h 2021-02-24 13:33:42.747680497 +0100
23964@@ -0,0 +1,186 @@
23965+/* SPDX-License-Identifier: GPL-2.0 */
23966+/*
23967+ * Copyright (C) 2018-2020 Junjiro R. Okajima
23968+ *
23969+ * This program, aufs is free software; you can redistribute it and/or modify
23970+ * it under the terms of the GNU General Public License as published by
23971+ * the Free Software Foundation; either version 2 of the License, or
23972+ * (at your option) any later version.
23973+ *
23974+ * This program is distributed in the hope that it will be useful,
23975+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
23976+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23977+ * GNU General Public License for more details.
23978+ *
23979+ * You should have received a copy of the GNU General Public License
23980+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
23981+ */
23982+
23983+/*
23984+ * simple long counter wrapper
23985+ */
23986+
23987+#ifndef __AUFS_LCNT_H__
23988+#define __AUFS_LCNT_H__
23989+
23990+#ifdef __KERNEL__
23991+
23992+#include "debug.h"
23993+
23994+#define AuLCntATOMIC 1
23995+#define AuLCntPCPUCNT 2
23996+/*
23997+ * why does percpu_refcount require extra synchronize_rcu()s in
23998+ * au_br_do_free()
23999+ */
24000+#define AuLCntPCPUREF 3
24001+
24002+/* #define AuLCntChosen AuLCntATOMIC */
24003+#define AuLCntChosen AuLCntPCPUCNT
24004+/* #define AuLCntChosen AuLCntPCPUREF */
24005+
24006+#if AuLCntChosen == AuLCntATOMIC
24007+#include <linux/atomic.h>
24008+
24009+typedef atomic_long_t au_lcnt_t;
24010+
24011+static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
24012+{
24013+ atomic_long_set(cnt, 0);
24014+ return 0;
24015+}
24016+
24017+static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24018+{
24019+ /* empty */
24020+}
24021+
24022+static inline void au_lcnt_fin(au_lcnt_t *cnt __maybe_unused,
24023+ int do_sync __maybe_unused)
24024+{
24025+ /* empty */
24026+}
24027+
24028+static inline void au_lcnt_inc(au_lcnt_t *cnt)
24029+{
24030+ atomic_long_inc(cnt);
24031+}
24032+
24033+static inline void au_lcnt_dec(au_lcnt_t *cnt)
24034+{
24035+ atomic_long_dec(cnt);
24036+}
24037+
24038+static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
24039+{
24040+ return atomic_long_read(cnt);
24041+}
24042+#endif
24043+
24044+#if AuLCntChosen == AuLCntPCPUCNT
24045+#include <linux/percpu_counter.h>
24046+
24047+typedef struct percpu_counter au_lcnt_t;
24048+
24049+static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
24050+{
24051+ return percpu_counter_init(cnt, 0, GFP_NOFS);
24052+}
24053+
24054+static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24055+{
24056+ /* empty */
24057+}
24058+
24059+static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync __maybe_unused)
24060+{
24061+ percpu_counter_destroy(cnt);
24062+}
24063+
24064+static inline void au_lcnt_inc(au_lcnt_t *cnt)
24065+{
24066+ percpu_counter_inc(cnt);
24067+}
24068+
24069+static inline void au_lcnt_dec(au_lcnt_t *cnt)
24070+{
24071+ percpu_counter_dec(cnt);
24072+}
24073+
24074+static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
24075+{
24076+ s64 n;
24077+
24078+ n = percpu_counter_sum(cnt);
24079+ BUG_ON(n < 0);
24080+ if (LONG_MAX != LLONG_MAX
24081+ && n > LONG_MAX)
24082+ AuWarn1("%s\n", "wrap-around");
24083+
24084+ return n;
24085+}
24086+#endif
24087+
24088+#if AuLCntChosen == AuLCntPCPUREF
24089+#include <linux/percpu-refcount.h>
24090+
24091+typedef struct percpu_ref au_lcnt_t;
24092+
24093+static inline int au_lcnt_init(au_lcnt_t *cnt, percpu_ref_func_t *release)
24094+{
24095+ if (!release)
24096+ release = percpu_ref_exit;
24097+ return percpu_ref_init(cnt, release, /*percpu mode*/0, GFP_NOFS);
24098+}
24099+
24100+static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24101+{
24102+ synchronize_rcu();
24103+}
24104+
24105+static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync)
24106+{
24107+ percpu_ref_kill(cnt);
24108+ if (do_sync)
24109+ au_lcnt_wait_for_fin(cnt);
24110+}
24111+
24112+static inline void au_lcnt_inc(au_lcnt_t *cnt)
24113+{
24114+ percpu_ref_get(cnt);
24115+}
24116+
24117+static inline void au_lcnt_dec(au_lcnt_t *cnt)
24118+{
24119+ percpu_ref_put(cnt);
24120+}
24121+
24122+/*
24123+ * avoid calling this func as possible.
24124+ */
24125+static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev)
24126+{
24127+ long l;
24128+
24129+ percpu_ref_switch_to_atomic_sync(cnt);
24130+ l = atomic_long_read(&cnt->count);
24131+ if (do_rev)
24132+ percpu_ref_switch_to_percpu(cnt);
24133+
24134+ /* percpu_ref is initialized by 1 instead of 0 */
24135+ return l - 1;
24136+}
24137+#endif
24138+
24139+#ifdef CONFIG_AUFS_DEBUG
24140+#define AuLCntZero(val) do { \
24141+ long l = val; \
24142+ if (l) \
24143+ AuDbg("%s = %ld\n", #val, l); \
24144+} while (0)
24145+#else
24146+#define AuLCntZero(val) do {} while (0)
24147+#endif
24148+
24149+#endif /* __KERNEL__ */
24150+#endif /* __AUFS_LCNT_H__ */
24151diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
24152--- /usr/share/empty/fs/aufs/loop.c 1970-01-01 01:00:00.000000000 +0100
24153+++ linux/fs/aufs/loop.c 2021-02-24 13:33:42.747680497 +0100
24154@@ -0,0 +1,148 @@
24155+// SPDX-License-Identifier: GPL-2.0
24156+/*
24157+ * Copyright (C) 2005-2020 Junjiro R. Okajima
24158+ *
24159+ * This program, aufs is free software; you can redistribute it and/or modify
24160+ * it under the terms of the GNU General Public License as published by
24161+ * the Free Software Foundation; either version 2 of the License, or
24162+ * (at your option) any later version.
24163+ *
24164+ * This program is distributed in the hope that it will be useful,
24165+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24166+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24167+ * GNU General Public License for more details.
24168+ *
24169+ * You should have received a copy of the GNU General Public License
24170+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
24171+ */
24172+
24173+/*
24174+ * support for loopback block device as a branch
24175+ */
24176+
24177+#include "aufs.h"
24178+
24179+/* added into drivers/block/loop.c */
24180+static struct file *(*backing_file_func)(struct super_block *sb);
24181+
24182+/*
24183+ * test if two lower dentries have overlapping branches.
24184+ */
24185+int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
24186+{
24187+ struct super_block *h_sb;
24188+ struct file *backing_file;
24189+
24190+ if (unlikely(!backing_file_func)) {
24191+ /* don't load "loop" module here */
24192+ backing_file_func = symbol_get(loop_backing_file);
24193+ if (unlikely(!backing_file_func))
24194+ /* "loop" module is not loaded */
24195+ return 0;
24196+ }
24197+
24198+ h_sb = h_adding->d_sb;
24199+ backing_file = backing_file_func(h_sb);
24200+ if (!backing_file)
24201+ return 0;
24202+
24203+ h_adding = backing_file->f_path.dentry;
24204+ /*
24205+ * h_adding can be local NFS.
24206+ * in this case aufs cannot detect the loop.
24207+ */
24208+ if (unlikely(h_adding->d_sb == sb))
24209+ return 1;
24210+ return !!au_test_subdir(h_adding, sb->s_root);
24211+}
24212+
24213+/* true if a kernel thread named 'loop[0-9].*' accesses a file */
24214+int au_test_loopback_kthread(void)
24215+{
24216+ int ret;
24217+ struct task_struct *tsk = current;
24218+ char c, comm[sizeof(tsk->comm)];
24219+
24220+ ret = 0;
24221+ if (tsk->flags & PF_KTHREAD) {
24222+ get_task_comm(comm, tsk);
24223+ c = comm[4];
24224+ ret = ('0' <= c && c <= '9'
24225+ && !strncmp(comm, "loop", 4));
24226+ }
24227+
24228+ return ret;
24229+}
24230+
24231+/* ---------------------------------------------------------------------- */
24232+
24233+#define au_warn_loopback_step 16
24234+static int au_warn_loopback_nelem = au_warn_loopback_step;
24235+static unsigned long *au_warn_loopback_array;
24236+
24237+void au_warn_loopback(struct super_block *h_sb)
24238+{
24239+ int i, new_nelem;
24240+ unsigned long *a, magic;
24241+ static DEFINE_SPINLOCK(spin);
24242+
24243+ magic = h_sb->s_magic;
24244+ spin_lock(&spin);
24245+ a = au_warn_loopback_array;
24246+ for (i = 0; i < au_warn_loopback_nelem && *a; i++)
24247+ if (a[i] == magic) {
24248+ spin_unlock(&spin);
24249+ return;
24250+ }
24251+
24252+ /* h_sb is new to us, print it */
24253+ if (i < au_warn_loopback_nelem) {
24254+ a[i] = magic;
24255+ goto pr;
24256+ }
24257+
24258+ /* expand the array */
24259+ new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
24260+ a = au_kzrealloc(au_warn_loopback_array,
24261+ au_warn_loopback_nelem * sizeof(unsigned long),
24262+ new_nelem * sizeof(unsigned long), GFP_ATOMIC,
24263+ /*may_shrink*/0);
24264+ if (a) {
24265+ au_warn_loopback_nelem = new_nelem;
24266+ au_warn_loopback_array = a;
24267+ a[i] = magic;
24268+ goto pr;
24269+ }
24270+
24271+ spin_unlock(&spin);
24272+ AuWarn1("realloc failed, ignored\n");
24273+ return;
24274+
24275+pr:
24276+ spin_unlock(&spin);
24277+ pr_warn("you may want to try another patch for loopback file "
24278+ "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
24279+}
24280+
24281+int au_loopback_init(void)
24282+{
24283+ int err;
24284+ struct super_block *sb __maybe_unused;
24285+
24286+ BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(*au_warn_loopback_array));
24287+
24288+ err = 0;
24289+ au_warn_loopback_array = kcalloc(au_warn_loopback_step,
24290+ sizeof(unsigned long), GFP_NOFS);
24291+ if (unlikely(!au_warn_loopback_array))
24292+ err = -ENOMEM;
24293+
24294+ return err;
24295+}
24296+
24297+void au_loopback_fin(void)
24298+{
24299+ if (backing_file_func)
24300+ symbol_put(loop_backing_file);
24301+ au_kfree_try_rcu(au_warn_loopback_array);
24302+}
24303diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
24304--- /usr/share/empty/fs/aufs/loop.h 1970-01-01 01:00:00.000000000 +0100
24305+++ linux/fs/aufs/loop.h 2021-02-24 13:33:42.747680497 +0100
24306@@ -0,0 +1,55 @@
24307+/* SPDX-License-Identifier: GPL-2.0 */
24308+/*
24309+ * Copyright (C) 2005-2020 Junjiro R. Okajima
24310+ *
24311+ * This program, aufs is free software; you can redistribute it and/or modify
24312+ * it under the terms of the GNU General Public License as published by
24313+ * the Free Software Foundation; either version 2 of the License, or
24314+ * (at your option) any later version.
24315+ *
24316+ * This program is distributed in the hope that it will be useful,
24317+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24318+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24319+ * GNU General Public License for more details.
24320+ *
24321+ * You should have received a copy of the GNU General Public License
24322+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
24323+ */
24324+
24325+/*
24326+ * support for loopback mount as a branch
24327+ */
24328+
24329+#ifndef __AUFS_LOOP_H__
24330+#define __AUFS_LOOP_H__
24331+
24332+#ifdef __KERNEL__
24333+
24334+struct dentry;
24335+struct super_block;
24336+
24337+#ifdef CONFIG_AUFS_BDEV_LOOP
24338+/* drivers/block/loop.c */
24339+struct file *loop_backing_file(struct super_block *sb);
24340+
24341+/* loop.c */
24342+int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
24343+int au_test_loopback_kthread(void);
24344+void au_warn_loopback(struct super_block *h_sb);
24345+
24346+int au_loopback_init(void);
24347+void au_loopback_fin(void);
24348+#else
24349+AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
24350+
24351+AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
24352+ struct dentry *h_adding)
24353+AuStubInt0(au_test_loopback_kthread, void)
24354+AuStubVoid(au_warn_loopback, struct super_block *h_sb)
24355+
24356+AuStubInt0(au_loopback_init, void)
24357+AuStubVoid(au_loopback_fin, void)
24358+#endif /* BLK_DEV_LOOP */
24359+
24360+#endif /* __KERNEL__ */
24361+#endif /* __AUFS_LOOP_H__ */
24362diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
24363--- /usr/share/empty/fs/aufs/magic.mk 1970-01-01 01:00:00.000000000 +0100
24364+++ linux/fs/aufs/magic.mk 2021-02-24 13:33:42.747680497 +0100
24365@@ -0,0 +1,31 @@
24366+# SPDX-License-Identifier: GPL-2.0
24367+
24368+# defined in ${srctree}/fs/fuse/inode.c
24369+# tristate
24370+ifdef CONFIG_FUSE_FS
24371+ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
24372+endif
24373+
24374+# defined in ${srctree}/fs/xfs/xfs_sb.h
24375+# tristate
24376+ifdef CONFIG_XFS_FS
24377+ccflags-y += -DXFS_SB_MAGIC=0x58465342
24378+endif
24379+
24380+# defined in ${srctree}/fs/configfs/mount.c
24381+# tristate
24382+ifdef CONFIG_CONFIGFS_FS
24383+ccflags-y += -DCONFIGFS_MAGIC=0x62656570
24384+endif
24385+
24386+# defined in ${srctree}/fs/ubifs/ubifs.h
24387+# tristate
24388+ifdef CONFIG_UBIFS_FS
24389+ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
24390+endif
24391+
24392+# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
24393+# tristate
24394+ifdef CONFIG_HFSPLUS_FS
24395+ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
24396+endif
24397diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
24398--- /usr/share/empty/fs/aufs/Makefile 1970-01-01 01:00:00.000000000 +0100
24399+++ linux/fs/aufs/Makefile 2021-02-24 13:33:42.741013619 +0100
24400@@ -0,0 +1,46 @@
24401+# SPDX-License-Identifier: GPL-2.0
24402+
24403+include ${src}/magic.mk
24404+ifeq (${CONFIG_AUFS_FS},m)
24405+include ${src}/conf.mk
24406+endif
24407+-include ${src}/priv_def.mk
24408+
24409+# cf. include/linux/kernel.h
24410+# enable pr_debug
24411+ccflags-y += -DDEBUG
24412+# sparse requires the full pathname
24413+ifdef M
24414+ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
24415+else
24416+ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
24417+endif
24418+
24419+obj-$(CONFIG_AUFS_FS) += aufs.o
24420+aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
24421+ wkq.o vfsub.o dcsub.o \
24422+ cpup.o whout.o wbr_policy.o \
24423+ dinfo.o dentry.o \
24424+ dynop.o \
24425+ finfo.o file.o f_op.o \
24426+ dir.o vdir.o \
24427+ iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
24428+ mvdown.o ioctl.o
24429+
24430+# all are boolean
24431+aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
24432+aufs-$(CONFIG_SYSFS) += sysfs.o
24433+aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
24434+aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
24435+aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
24436+aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
24437+aufs-$(CONFIG_AUFS_EXPORT) += export.o
24438+aufs-$(CONFIG_AUFS_XATTR) += xattr.o
24439+aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
24440+aufs-$(CONFIG_AUFS_DIRREN) += dirren.o
24441+aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
24442+aufs-$(CONFIG_AUFS_POLL) += poll.o
24443+aufs-$(CONFIG_AUFS_RDU) += rdu.o
24444+aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
24445+aufs-$(CONFIG_AUFS_DEBUG) += debug.o
24446+aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
24447diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
24448--- /usr/share/empty/fs/aufs/module.c 1970-01-01 01:00:00.000000000 +0100
24449+++ linux/fs/aufs/module.c 2021-02-24 13:33:42.747680497 +0100
24450@@ -0,0 +1,273 @@
24451+// SPDX-License-Identifier: GPL-2.0
24452+/*
24453+ * Copyright (C) 2005-2020 Junjiro R. Okajima
24454+ *
24455+ * This program, aufs is free software; you can redistribute it and/or modify
24456+ * it under the terms of the GNU General Public License as published by
24457+ * the Free Software Foundation; either version 2 of the License, or
24458+ * (at your option) any later version.
24459+ *
24460+ * This program is distributed in the hope that it will be useful,
24461+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24462+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24463+ * GNU General Public License for more details.
24464+ *
24465+ * You should have received a copy of the GNU General Public License
24466+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
24467+ */
24468+
24469+/*
24470+ * module global variables and operations
24471+ */
24472+
24473+#include <linux/module.h>
24474+#include <linux/seq_file.h>
24475+#include "aufs.h"
24476+
24477+/* shrinkable realloc */
24478+void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
24479+{
24480+ size_t sz;
24481+ int diff;
24482+
24483+ sz = 0;
24484+ diff = -1;
24485+ if (p) {
24486+#if 0 /* unused */
24487+ if (!new_sz) {
24488+ au_kfree_rcu(p);
24489+ p = NULL;
24490+ goto out;
24491+ }
24492+#else
24493+ AuDebugOn(!new_sz);
24494+#endif
24495+ sz = ksize(p);
24496+ diff = au_kmidx_sub(sz, new_sz);
24497+ }
24498+ if (sz && !diff)
24499+ goto out;
24500+
24501+ if (sz < new_sz)
24502+ /* expand or SLOB */
24503+ p = krealloc(p, new_sz, gfp);
24504+ else if (new_sz < sz && may_shrink) {
24505+ /* shrink */
24506+ void *q;
24507+
24508+ q = kmalloc(new_sz, gfp);
24509+ if (q) {
24510+ if (p) {
24511+ memcpy(q, p, new_sz);
24512+ au_kfree_try_rcu(p);
24513+ }
24514+ p = q;
24515+ } else
24516+ p = NULL;
24517+ }
24518+
24519+out:
24520+ return p;
24521+}
24522+
24523+void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24524+ int may_shrink)
24525+{
24526+ p = au_krealloc(p, new_sz, gfp, may_shrink);
24527+ if (p && new_sz > nused)
24528+ memset(p + nused, 0, new_sz - nused);
24529+ return p;
24530+}
24531+
24532+/* ---------------------------------------------------------------------- */
24533+/*
24534+ * aufs caches
24535+ */
24536+struct kmem_cache *au_cache[AuCache_Last];
24537+
24538+static void au_cache_fin(void)
24539+{
24540+ int i;
24541+
24542+ /*
24543+ * Make sure all delayed rcu free inodes are flushed before we
24544+ * destroy cache.
24545+ */
24546+ rcu_barrier();
24547+
24548+ /* excluding AuCache_HNOTIFY */
24549+ BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
24550+ for (i = 0; i < AuCache_HNOTIFY; i++) {
24551+ kmem_cache_destroy(au_cache[i]);
24552+ au_cache[i] = NULL;
24553+ }
24554+}
24555+
24556+static int __init au_cache_init(void)
24557+{
24558+ au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
24559+ if (au_cache[AuCache_DINFO])
24560+ /* SLAB_DESTROY_BY_RCU */
24561+ au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
24562+ au_icntnr_init_once);
24563+ if (au_cache[AuCache_ICNTNR])
24564+ au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
24565+ au_fi_init_once);
24566+ if (au_cache[AuCache_FINFO])
24567+ au_cache[AuCache_VDIR] = AuCache(au_vdir);
24568+ if (au_cache[AuCache_VDIR])
24569+ au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
24570+ if (au_cache[AuCache_DEHSTR])
24571+ return 0;
24572+
24573+ au_cache_fin();
24574+ return -ENOMEM;
24575+}
24576+
24577+/* ---------------------------------------------------------------------- */
24578+
24579+int au_dir_roflags;
24580+
24581+#ifdef CONFIG_AUFS_SBILIST
24582+/*
24583+ * iterate_supers_type() doesn't protect us from
24584+ * remounting (branch management)
24585+ */
24586+struct hlist_bl_head au_sbilist;
24587+#endif
24588+
24589+/*
24590+ * functions for module interface.
24591+ */
24592+MODULE_LICENSE("GPL");
24593+/* MODULE_LICENSE("GPL v2"); */
24594+MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
24595+MODULE_DESCRIPTION(AUFS_NAME
24596+ " -- Advanced multi layered unification filesystem");
24597+MODULE_VERSION(AUFS_VERSION);
24598+MODULE_ALIAS_FS(AUFS_NAME);
24599+
24600+/* this module parameter has no meaning when SYSFS is disabled */
24601+int sysaufs_brs = 1;
24602+MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
24603+module_param_named(brs, sysaufs_brs, int, 0444);
24604+
24605+/* this module parameter has no meaning when USER_NS is disabled */
24606+bool au_userns;
24607+MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
24608+module_param_named(allow_userns, au_userns, bool, 0444);
24609+
24610+/* ---------------------------------------------------------------------- */
24611+
24612+static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
24613+
24614+int au_seq_path(struct seq_file *seq, struct path *path)
24615+{
24616+ int err;
24617+
24618+ err = seq_path(seq, path, au_esc_chars);
24619+ if (err >= 0)
24620+ err = 0;
24621+ else
24622+ err = -ENOMEM;
24623+
24624+ return err;
24625+}
24626+
24627+/* ---------------------------------------------------------------------- */
24628+
24629+static int __init aufs_init(void)
24630+{
24631+ int err, i;
24632+ char *p;
24633+
24634+ p = au_esc_chars;
24635+ for (i = 1; i <= ' '; i++)
24636+ *p++ = i;
24637+ *p++ = '\\';
24638+ *p++ = '\x7f';
24639+ *p = 0;
24640+
24641+ au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
24642+
24643+ memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
24644+ for (i = 0; i < AuIop_Last; i++)
24645+ aufs_iop_nogetattr[i].getattr = NULL;
24646+
24647+ memset(au_cache, 0, sizeof(au_cache)); /* including hnotify */
24648+
24649+ au_sbilist_init();
24650+ sysaufs_brs_init();
24651+ au_debug_init();
24652+ au_dy_init();
24653+ err = sysaufs_init();
24654+ if (unlikely(err))
24655+ goto out;
24656+ err = dbgaufs_init();
24657+ if (unlikely(err))
24658+ goto out_sysaufs;
24659+ err = au_procfs_init();
24660+ if (unlikely(err))
24661+ goto out_dbgaufs;
24662+ err = au_wkq_init();
24663+ if (unlikely(err))
24664+ goto out_procfs;
24665+ err = au_loopback_init();
24666+ if (unlikely(err))
24667+ goto out_wkq;
24668+ err = au_hnotify_init();
24669+ if (unlikely(err))
24670+ goto out_loopback;
24671+ err = au_sysrq_init();
24672+ if (unlikely(err))
24673+ goto out_hin;
24674+ err = au_cache_init();
24675+ if (unlikely(err))
24676+ goto out_sysrq;
24677+
24678+ aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
24679+ err = register_filesystem(&aufs_fs_type);
24680+ if (unlikely(err))
24681+ goto out_cache;
24682+
24683+ /* since we define pr_fmt, call printk directly */
24684+ printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
24685+ goto out; /* success */
24686+
24687+out_cache:
24688+ au_cache_fin();
24689+out_sysrq:
24690+ au_sysrq_fin();
24691+out_hin:
24692+ au_hnotify_fin();
24693+out_loopback:
24694+ au_loopback_fin();
24695+out_wkq:
24696+ au_wkq_fin();
24697+out_procfs:
24698+ au_procfs_fin();
24699+out_dbgaufs:
24700+ dbgaufs_fin();
24701+out_sysaufs:
24702+ sysaufs_fin();
24703+ au_dy_fin();
24704+out:
24705+ return err;
24706+}
24707+
24708+static void __exit aufs_exit(void)
24709+{
24710+ unregister_filesystem(&aufs_fs_type);
24711+ au_cache_fin();
24712+ au_sysrq_fin();
24713+ au_hnotify_fin();
24714+ au_loopback_fin();
24715+ au_wkq_fin();
24716+ au_procfs_fin();
24717+ dbgaufs_fin();
24718+ sysaufs_fin();
24719+ au_dy_fin();
24720+}
24721+
24722+module_init(aufs_init);
24723+module_exit(aufs_exit);
24724diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
24725--- /usr/share/empty/fs/aufs/module.h 1970-01-01 01:00:00.000000000 +0100
24726+++ linux/fs/aufs/module.h 2021-02-24 13:33:42.747680497 +0100
24727@@ -0,0 +1,166 @@
24728+/* SPDX-License-Identifier: GPL-2.0 */
24729+/*
24730+ * Copyright (C) 2005-2020 Junjiro R. Okajima
24731+ *
24732+ * This program, aufs is free software; you can redistribute it and/or modify
24733+ * it under the terms of the GNU General Public License as published by
24734+ * the Free Software Foundation; either version 2 of the License, or
24735+ * (at your option) any later version.
24736+ *
24737+ * This program is distributed in the hope that it will be useful,
24738+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24739+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24740+ * GNU General Public License for more details.
24741+ *
24742+ * You should have received a copy of the GNU General Public License
24743+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
24744+ */
24745+
24746+/*
24747+ * module initialization and module-global
24748+ */
24749+
24750+#ifndef __AUFS_MODULE_H__
24751+#define __AUFS_MODULE_H__
24752+
24753+#ifdef __KERNEL__
24754+
24755+#include <linux/slab.h>
24756+#include "debug.h"
24757+#include "dentry.h"
24758+#include "dir.h"
24759+#include "file.h"
24760+#include "inode.h"
24761+
24762+struct path;
24763+struct seq_file;
24764+
24765+/* module parameters */
24766+extern int sysaufs_brs;
24767+extern bool au_userns;
24768+
24769+/* ---------------------------------------------------------------------- */
24770+
24771+extern int au_dir_roflags;
24772+
24773+void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
24774+void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24775+ int may_shrink);
24776+
24777+/*
24778+ * Comparing the size of the object with sizeof(struct rcu_head)
24779+ * case 1: object is always larger
24780+ * --> au_kfree_rcu() or au_kfree_do_rcu()
24781+ * case 2: object is always smaller
24782+ * --> au_kfree_small()
24783+ * case 3: object can be any size
24784+ * --> au_kfree_try_rcu()
24785+ */
24786+
24787+static inline void au_kfree_do_rcu(const void *p)
24788+{
24789+ struct {
24790+ struct rcu_head rcu;
24791+ } *a = (void *)p;
24792+
24793+ kfree_rcu(a, rcu);
24794+}
24795+
24796+#define au_kfree_rcu(_p) do { \
24797+ typeof(_p) p = (_p); \
24798+ BUILD_BUG_ON(sizeof(*p) < sizeof(struct rcu_head)); \
24799+ if (p) \
24800+ au_kfree_do_rcu(p); \
24801+ } while (0)
24802+
24803+#define au_kfree_do_sz_test(sz) (sz >= sizeof(struct rcu_head))
24804+#define au_kfree_sz_test(p) (p && au_kfree_do_sz_test(ksize(p)))
24805+
24806+static inline void au_kfree_try_rcu(const void *p)
24807+{
24808+ if (!p)
24809+ return;
24810+ if (au_kfree_sz_test(p))
24811+ au_kfree_do_rcu(p);
24812+ else
24813+ kfree(p);
24814+}
24815+
24816+static inline void au_kfree_small(const void *p)
24817+{
24818+ if (!p)
24819+ return;
24820+ AuDebugOn(au_kfree_sz_test(p));
24821+ kfree(p);
24822+}
24823+
24824+static inline int au_kmidx_sub(size_t sz, size_t new_sz)
24825+{
24826+#ifndef CONFIG_SLOB
24827+ return kmalloc_index(sz) - kmalloc_index(new_sz);
24828+#else
24829+ return -1; /* SLOB is untested */
24830+#endif
24831+}
24832+
24833+int au_seq_path(struct seq_file *seq, struct path *path);
24834+
24835+#ifdef CONFIG_PROC_FS
24836+/* procfs.c */
24837+int __init au_procfs_init(void);
24838+void au_procfs_fin(void);
24839+#else
24840+AuStubInt0(au_procfs_init, void);
24841+AuStubVoid(au_procfs_fin, void);
24842+#endif
24843+
24844+/* ---------------------------------------------------------------------- */
24845+
24846+/* kmem cache */
24847+enum {
24848+ AuCache_DINFO,
24849+ AuCache_ICNTNR,
24850+ AuCache_FINFO,
24851+ AuCache_VDIR,
24852+ AuCache_DEHSTR,
24853+ AuCache_HNOTIFY, /* must be last */
24854+ AuCache_Last
24855+};
24856+
24857+extern struct kmem_cache *au_cache[AuCache_Last];
24858+
24859+#define AuCacheFlags (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
24860+#define AuCache(type) KMEM_CACHE(type, AuCacheFlags)
24861+#define AuCacheCtor(type, ctor) \
24862+ kmem_cache_create(#type, sizeof(struct type), \
24863+ __alignof__(struct type), AuCacheFlags, ctor)
24864+
24865+#define AuCacheFuncs(name, index) \
24866+ static inline struct au_##name *au_cache_alloc_##name(void) \
24867+ { return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); } \
24868+ static inline void au_cache_free_##name##_norcu(struct au_##name *p) \
24869+ { kmem_cache_free(au_cache[AuCache_##index], p); } \
24870+ \
24871+ static inline void au_cache_free_##name##_rcu_cb(struct rcu_head *rcu) \
24872+ { void *p = rcu; \
24873+ p -= offsetof(struct au_##name, rcu); \
24874+ kmem_cache_free(au_cache[AuCache_##index], p); } \
24875+ static inline void au_cache_free_##name##_rcu(struct au_##name *p) \
24876+ { BUILD_BUG_ON(sizeof(struct au_##name) < sizeof(struct rcu_head)); \
24877+ call_rcu(&p->rcu, au_cache_free_##name##_rcu_cb); } \
24878+ \
24879+ static inline void au_cache_free_##name(struct au_##name *p) \
24880+ { /* au_cache_free_##name##_norcu(p); */ \
24881+ au_cache_free_##name##_rcu(p); }
24882+
24883+AuCacheFuncs(dinfo, DINFO);
24884+AuCacheFuncs(icntnr, ICNTNR);
24885+AuCacheFuncs(finfo, FINFO);
24886+AuCacheFuncs(vdir, VDIR);
24887+AuCacheFuncs(vdir_dehstr, DEHSTR);
24888+#ifdef CONFIG_AUFS_HNOTIFY
24889+AuCacheFuncs(hnotify, HNOTIFY);
24890+#endif
24891+
24892+#endif /* __KERNEL__ */
24893+#endif /* __AUFS_MODULE_H__ */
24894diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
24895--- /usr/share/empty/fs/aufs/mvdown.c 1970-01-01 01:00:00.000000000 +0100
24896+++ linux/fs/aufs/mvdown.c 2021-02-24 13:33:42.747680497 +0100
24897@@ -0,0 +1,706 @@
24898+// SPDX-License-Identifier: GPL-2.0
24899+/*
24900+ * Copyright (C) 2011-2020 Junjiro R. Okajima
24901+ *
24902+ * This program, aufs is free software; you can redistribute it and/or modify
24903+ * it under the terms of the GNU General Public License as published by
24904+ * the Free Software Foundation; either version 2 of the License, or
24905+ * (at your option) any later version.
24906+ *
24907+ * This program is distributed in the hope that it will be useful,
24908+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24909+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24910+ * GNU General Public License for more details.
24911+ *
24912+ * You should have received a copy of the GNU General Public License
24913+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
24914+ */
24915+
24916+/*
24917+ * move-down, opposite of copy-up
24918+ */
24919+
24920+#include "aufs.h"
24921+
24922+struct au_mvd_args {
24923+ struct {
24924+ struct super_block *h_sb;
24925+ struct dentry *h_parent;
24926+ struct au_hinode *hdir;
24927+ struct inode *h_dir, *h_inode;
24928+ struct au_pin pin;
24929+ } info[AUFS_MVDOWN_NARRAY];
24930+
24931+ struct aufs_mvdown mvdown;
24932+ struct dentry *dentry, *parent;
24933+ struct inode *inode, *dir;
24934+ struct super_block *sb;
24935+ aufs_bindex_t bopq, bwh, bfound;
24936+ unsigned char rename_lock;
24937+};
24938+
24939+#define mvd_errno mvdown.au_errno
24940+#define mvd_bsrc mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
24941+#define mvd_src_brid mvdown.stbr[AUFS_MVDOWN_UPPER].brid
24942+#define mvd_bdst mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
24943+#define mvd_dst_brid mvdown.stbr[AUFS_MVDOWN_LOWER].brid
24944+
24945+#define mvd_h_src_sb info[AUFS_MVDOWN_UPPER].h_sb
24946+#define mvd_h_src_parent info[AUFS_MVDOWN_UPPER].h_parent
24947+#define mvd_hdir_src info[AUFS_MVDOWN_UPPER].hdir
24948+#define mvd_h_src_dir info[AUFS_MVDOWN_UPPER].h_dir
24949+#define mvd_h_src_inode info[AUFS_MVDOWN_UPPER].h_inode
24950+#define mvd_pin_src info[AUFS_MVDOWN_UPPER].pin
24951+
24952+#define mvd_h_dst_sb info[AUFS_MVDOWN_LOWER].h_sb
24953+#define mvd_h_dst_parent info[AUFS_MVDOWN_LOWER].h_parent
24954+#define mvd_hdir_dst info[AUFS_MVDOWN_LOWER].hdir
24955+#define mvd_h_dst_dir info[AUFS_MVDOWN_LOWER].h_dir
24956+#define mvd_h_dst_inode info[AUFS_MVDOWN_LOWER].h_inode
24957+#define mvd_pin_dst info[AUFS_MVDOWN_LOWER].pin
24958+
24959+#define AU_MVD_PR(flag, ...) do { \
24960+ if (flag) \
24961+ pr_err(__VA_ARGS__); \
24962+ } while (0)
24963+
24964+static int find_lower_writable(struct au_mvd_args *a)
24965+{
24966+ struct super_block *sb;
24967+ aufs_bindex_t bindex, bbot;
24968+ struct au_branch *br;
24969+
24970+ sb = a->sb;
24971+ bindex = a->mvd_bsrc;
24972+ bbot = au_sbbot(sb);
24973+ if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
24974+ for (bindex++; bindex <= bbot; bindex++) {
24975+ br = au_sbr(sb, bindex);
24976+ if (au_br_fhsm(br->br_perm)
24977+ && !sb_rdonly(au_br_sb(br)))
24978+ return bindex;
24979+ }
24980+ else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
24981+ for (bindex++; bindex <= bbot; bindex++) {
24982+ br = au_sbr(sb, bindex);
24983+ if (!au_br_rdonly(br))
24984+ return bindex;
24985+ }
24986+ else
24987+ for (bindex++; bindex <= bbot; bindex++) {
24988+ br = au_sbr(sb, bindex);
24989+ if (!sb_rdonly(au_br_sb(br))) {
24990+ if (au_br_rdonly(br))
24991+ a->mvdown.flags
24992+ |= AUFS_MVDOWN_ROLOWER_R;
24993+ return bindex;
24994+ }
24995+ }
24996+
24997+ return -1;
24998+}
24999+
25000+/* make the parent dir on bdst */
25001+static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
25002+{
25003+ int err;
25004+
25005+ err = 0;
25006+ a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
25007+ a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
25008+ a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
25009+ a->mvd_h_dst_parent = NULL;
25010+ if (au_dbbot(a->parent) >= a->mvd_bdst)
25011+ a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
25012+ if (!a->mvd_h_dst_parent) {
25013+ err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
25014+ if (unlikely(err)) {
25015+ AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
25016+ goto out;
25017+ }
25018+ a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
25019+ }
25020+
25021+out:
25022+ AuTraceErr(err);
25023+ return err;
25024+}
25025+
25026+/* lock them all */
25027+static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
25028+{
25029+ int err;
25030+ struct dentry *h_trap;
25031+
25032+ a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
25033+ a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
25034+ err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
25035+ au_opt_udba(a->sb),
25036+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25037+ AuTraceErr(err);
25038+ if (unlikely(err)) {
25039+ AU_MVD_PR(dmsg, "pin_dst failed\n");
25040+ goto out;
25041+ }
25042+
25043+ if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
25044+ a->rename_lock = 0;
25045+ au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
25046+ AuLsc_DI_PARENT, AuLsc_I_PARENT3,
25047+ au_opt_udba(a->sb),
25048+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25049+ err = au_do_pin(&a->mvd_pin_src);
25050+ AuTraceErr(err);
25051+ a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
25052+ if (unlikely(err)) {
25053+ AU_MVD_PR(dmsg, "pin_src failed\n");
25054+ goto out_dst;
25055+ }
25056+ goto out; /* success */
25057+ }
25058+
25059+ a->rename_lock = 1;
25060+ au_pin_hdir_unlock(&a->mvd_pin_dst);
25061+ err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
25062+ au_opt_udba(a->sb),
25063+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25064+ AuTraceErr(err);
25065+ a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
25066+ if (unlikely(err)) {
25067+ AU_MVD_PR(dmsg, "pin_src failed\n");
25068+ au_pin_hdir_lock(&a->mvd_pin_dst);
25069+ goto out_dst;
25070+ }
25071+ au_pin_hdir_unlock(&a->mvd_pin_src);
25072+ h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25073+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
25074+ if (h_trap) {
25075+ err = (h_trap != a->mvd_h_src_parent);
25076+ if (err)
25077+ err = (h_trap != a->mvd_h_dst_parent);
25078+ }
25079+ BUG_ON(err); /* it should never happen */
25080+ if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
25081+ err = -EBUSY;
25082+ AuTraceErr(err);
25083+ vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25084+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
25085+ au_pin_hdir_lock(&a->mvd_pin_src);
25086+ au_unpin(&a->mvd_pin_src);
25087+ au_pin_hdir_lock(&a->mvd_pin_dst);
25088+ goto out_dst;
25089+ }
25090+ goto out; /* success */
25091+
25092+out_dst:
25093+ au_unpin(&a->mvd_pin_dst);
25094+out:
25095+ AuTraceErr(err);
25096+ return err;
25097+}
25098+
25099+static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
25100+{
25101+ if (!a->rename_lock)
25102+ au_unpin(&a->mvd_pin_src);
25103+ else {
25104+ vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25105+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
25106+ au_pin_hdir_lock(&a->mvd_pin_src);
25107+ au_unpin(&a->mvd_pin_src);
25108+ au_pin_hdir_lock(&a->mvd_pin_dst);
25109+ }
25110+ au_unpin(&a->mvd_pin_dst);
25111+}
25112+
25113+/* copy-down the file */
25114+static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
25115+{
25116+ int err;
25117+ struct au_cp_generic cpg = {
25118+ .dentry = a->dentry,
25119+ .bdst = a->mvd_bdst,
25120+ .bsrc = a->mvd_bsrc,
25121+ .len = -1,
25122+ .pin = &a->mvd_pin_dst,
25123+ .flags = AuCpup_DTIME | AuCpup_HOPEN
25124+ };
25125+
25126+ AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
25127+ if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25128+ au_fset_cpup(cpg.flags, OVERWRITE);
25129+ if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
25130+ au_fset_cpup(cpg.flags, RWDST);
25131+ err = au_sio_cpdown_simple(&cpg);
25132+ if (unlikely(err))
25133+ AU_MVD_PR(dmsg, "cpdown failed\n");
25134+
25135+ AuTraceErr(err);
25136+ return err;
25137+}
25138+
25139+/*
25140+ * unlink the whiteout on bdst if exist which may be created by UDBA while we
25141+ * were sleeping
25142+ */
25143+static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
25144+{
25145+ int err;
25146+ struct path h_path;
25147+ struct au_branch *br;
25148+ struct inode *delegated;
25149+
25150+ br = au_sbr(a->sb, a->mvd_bdst);
25151+ h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
25152+ err = PTR_ERR(h_path.dentry);
25153+ if (IS_ERR(h_path.dentry)) {
25154+ AU_MVD_PR(dmsg, "wh_lkup failed\n");
25155+ goto out;
25156+ }
25157+
25158+ err = 0;
25159+ if (d_is_positive(h_path.dentry)) {
25160+ h_path.mnt = au_br_mnt(br);
25161+ delegated = NULL;
25162+ err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
25163+ &delegated, /*force*/0);
25164+ if (unlikely(err == -EWOULDBLOCK)) {
25165+ pr_warn("cannot retry for NFSv4 delegation"
25166+ " for an internal unlink\n");
25167+ iput(delegated);
25168+ }
25169+ if (unlikely(err))
25170+ AU_MVD_PR(dmsg, "wh_unlink failed\n");
25171+ }
25172+ dput(h_path.dentry);
25173+
25174+out:
25175+ AuTraceErr(err);
25176+ return err;
25177+}
25178+
25179+/*
25180+ * unlink the topmost h_dentry
25181+ */
25182+static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
25183+{
25184+ int err;
25185+ struct path h_path;
25186+ struct inode *delegated;
25187+
25188+ h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
25189+ h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
25190+ delegated = NULL;
25191+ err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
25192+ if (unlikely(err == -EWOULDBLOCK)) {
25193+ pr_warn("cannot retry for NFSv4 delegation"
25194+ " for an internal unlink\n");
25195+ iput(delegated);
25196+ }
25197+ if (unlikely(err))
25198+ AU_MVD_PR(dmsg, "unlink failed\n");
25199+
25200+ AuTraceErr(err);
25201+ return err;
25202+}
25203+
25204+/* Since mvdown succeeded, we ignore an error of this function */
25205+static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
25206+{
25207+ int err;
25208+ struct au_branch *br;
25209+
25210+ a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
25211+ br = au_sbr(a->sb, a->mvd_bsrc);
25212+ err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
25213+ if (!err) {
25214+ br = au_sbr(a->sb, a->mvd_bdst);
25215+ a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
25216+ err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
25217+ }
25218+ if (!err)
25219+ a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
25220+ else
25221+ AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
25222+}
25223+
25224+/*
25225+ * copy-down the file and unlink the bsrc file.
25226+ * - unlink the bdst whout if exist
25227+ * - copy-down the file (with whtmp name and rename)
25228+ * - unlink the bsrc file
25229+ */
25230+static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
25231+{
25232+ int err;
25233+
25234+ err = au_do_mkdir(dmsg, a);
25235+ if (!err)
25236+ err = au_do_lock(dmsg, a);
25237+ if (unlikely(err))
25238+ goto out;
25239+
25240+ /*
25241+ * do not revert the activities we made on bdst since they should be
25242+ * harmless in aufs.
25243+ */
25244+
25245+ err = au_do_cpdown(dmsg, a);
25246+ if (!err)
25247+ err = au_do_unlink_wh(dmsg, a);
25248+ if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
25249+ err = au_do_unlink(dmsg, a);
25250+ if (unlikely(err))
25251+ goto out_unlock;
25252+
25253+ AuDbg("%pd2, 0x%x, %d --> %d\n",
25254+ a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
25255+ if (find_lower_writable(a) < 0)
25256+ a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
25257+
25258+ if (a->mvdown.flags & AUFS_MVDOWN_STFS)
25259+ au_do_stfs(dmsg, a);
25260+
25261+ /* maintain internal array */
25262+ if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
25263+ au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
25264+ au_set_dbtop(a->dentry, a->mvd_bdst);
25265+ au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
25266+ au_set_ibtop(a->inode, a->mvd_bdst);
25267+ } else {
25268+ /* hide the lower */
25269+ au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
25270+ au_set_dbbot(a->dentry, a->mvd_bsrc);
25271+ au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
25272+ au_set_ibbot(a->inode, a->mvd_bsrc);
25273+ }
25274+ if (au_dbbot(a->dentry) < a->mvd_bdst)
25275+ au_set_dbbot(a->dentry, a->mvd_bdst);
25276+ if (au_ibbot(a->inode) < a->mvd_bdst)
25277+ au_set_ibbot(a->inode, a->mvd_bdst);
25278+
25279+out_unlock:
25280+ au_do_unlock(dmsg, a);
25281+out:
25282+ AuTraceErr(err);
25283+ return err;
25284+}
25285+
25286+/* ---------------------------------------------------------------------- */
25287+
25288+/* make sure the file is idle */
25289+static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
25290+{
25291+ int err, plinked;
25292+
25293+ err = 0;
25294+ plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
25295+ if (au_dbtop(a->dentry) == a->mvd_bsrc
25296+ && au_dcount(a->dentry) == 1
25297+ && atomic_read(&a->inode->i_count) == 1
25298+ /* && a->mvd_h_src_inode->i_nlink == 1 */
25299+ && (!plinked || !au_plink_test(a->inode))
25300+ && a->inode->i_nlink == 1)
25301+ goto out;
25302+
25303+ err = -EBUSY;
25304+ AU_MVD_PR(dmsg,
25305+ "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
25306+ a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
25307+ atomic_read(&a->inode->i_count), a->inode->i_nlink,
25308+ a->mvd_h_src_inode->i_nlink,
25309+ plinked, plinked ? au_plink_test(a->inode) : 0);
25310+
25311+out:
25312+ AuTraceErr(err);
25313+ return err;
25314+}
25315+
25316+/* make sure the parent dir is fine */
25317+static int au_mvd_args_parent(const unsigned char dmsg,
25318+ struct au_mvd_args *a)
25319+{
25320+ int err;
25321+ aufs_bindex_t bindex;
25322+
25323+ err = 0;
25324+ if (unlikely(au_alive_dir(a->parent))) {
25325+ err = -ENOENT;
25326+ AU_MVD_PR(dmsg, "parent dir is dead\n");
25327+ goto out;
25328+ }
25329+
25330+ a->bopq = au_dbdiropq(a->parent);
25331+ bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
25332+ AuDbg("b%d\n", bindex);
25333+ if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
25334+ || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
25335+ err = -EINVAL;
25336+ a->mvd_errno = EAU_MVDOWN_OPAQUE;
25337+ AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
25338+ a->bopq, a->mvd_bdst);
25339+ }
25340+
25341+out:
25342+ AuTraceErr(err);
25343+ return err;
25344+}
25345+
25346+static int au_mvd_args_intermediate(const unsigned char dmsg,
25347+ struct au_mvd_args *a)
25348+{
25349+ int err;
25350+ struct au_dinfo *dinfo, *tmp;
25351+
25352+ /* lookup the next lower positive entry */
25353+ err = -ENOMEM;
25354+ tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
25355+ if (unlikely(!tmp))
25356+ goto out;
25357+
25358+ a->bfound = -1;
25359+ a->bwh = -1;
25360+ dinfo = au_di(a->dentry);
25361+ au_di_cp(tmp, dinfo);
25362+ au_di_swap(tmp, dinfo);
25363+
25364+ /* returns the number of positive dentries */
25365+ err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
25366+ /* AuLkup_IGNORE_PERM */ 0);
25367+ if (!err)
25368+ a->bwh = au_dbwh(a->dentry);
25369+ else if (err > 0)
25370+ a->bfound = au_dbtop(a->dentry);
25371+
25372+ au_di_swap(tmp, dinfo);
25373+ au_rw_write_unlock(&tmp->di_rwsem);
25374+ au_di_free(tmp);
25375+ if (unlikely(err < 0))
25376+ AU_MVD_PR(dmsg, "failed look-up lower\n");
25377+
25378+ /*
25379+ * here, we have these cases.
25380+ * bfound == -1
25381+ * no positive dentry under bsrc. there are more sub-cases.
25382+ * bwh < 0
25383+ * there no whiteout, we can safely move-down.
25384+ * bwh <= bsrc
25385+ * impossible
25386+ * bsrc < bwh && bwh < bdst
25387+ * there is a whiteout on RO branch. cannot proceed.
25388+ * bwh == bdst
25389+ * there is a whiteout on the RW target branch. it should
25390+ * be removed.
25391+ * bdst < bwh
25392+ * there is a whiteout somewhere unrelated branch.
25393+ * -1 < bfound && bfound <= bsrc
25394+ * impossible.
25395+ * bfound < bdst
25396+ * found, but it is on RO branch between bsrc and bdst. cannot
25397+ * proceed.
25398+ * bfound == bdst
25399+ * found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
25400+ * error.
25401+ * bdst < bfound
25402+ * found, after we create the file on bdst, it will be hidden.
25403+ */
25404+
25405+ AuDebugOn(a->bfound == -1
25406+ && a->bwh != -1
25407+ && a->bwh <= a->mvd_bsrc);
25408+ AuDebugOn(-1 < a->bfound
25409+ && a->bfound <= a->mvd_bsrc);
25410+
25411+ err = -EINVAL;
25412+ if (a->bfound == -1
25413+ && a->mvd_bsrc < a->bwh
25414+ && a->bwh != -1
25415+ && a->bwh < a->mvd_bdst) {
25416+ a->mvd_errno = EAU_MVDOWN_WHITEOUT;
25417+ AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
25418+ a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
25419+ goto out;
25420+ } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
25421+ a->mvd_errno = EAU_MVDOWN_UPPER;
25422+ AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
25423+ a->mvd_bdst, a->bfound);
25424+ goto out;
25425+ }
25426+
25427+ err = 0; /* success */
25428+
25429+out:
25430+ AuTraceErr(err);
25431+ return err;
25432+}
25433+
25434+static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
25435+{
25436+ int err;
25437+
25438+ err = 0;
25439+ if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25440+ && a->bfound == a->mvd_bdst)
25441+ err = -EEXIST;
25442+ AuTraceErr(err);
25443+ return err;
25444+}
25445+
25446+static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
25447+{
25448+ int err;
25449+ struct au_branch *br;
25450+
25451+ err = -EISDIR;
25452+ if (unlikely(S_ISDIR(a->inode->i_mode)))
25453+ goto out;
25454+
25455+ err = -EINVAL;
25456+ if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
25457+ a->mvd_bsrc = au_ibtop(a->inode);
25458+ else {
25459+ a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
25460+ if (unlikely(a->mvd_bsrc < 0
25461+ || (a->mvd_bsrc < au_dbtop(a->dentry)
25462+ || au_dbbot(a->dentry) < a->mvd_bsrc
25463+ || !au_h_dptr(a->dentry, a->mvd_bsrc))
25464+ || (a->mvd_bsrc < au_ibtop(a->inode)
25465+ || au_ibbot(a->inode) < a->mvd_bsrc
25466+ || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
25467+ a->mvd_errno = EAU_MVDOWN_NOUPPER;
25468+ AU_MVD_PR(dmsg, "no upper\n");
25469+ goto out;
25470+ }
25471+ }
25472+ if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
25473+ a->mvd_errno = EAU_MVDOWN_BOTTOM;
25474+ AU_MVD_PR(dmsg, "on the bottom\n");
25475+ goto out;
25476+ }
25477+ a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
25478+ br = au_sbr(a->sb, a->mvd_bsrc);
25479+ err = au_br_rdonly(br);
25480+ if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
25481+ if (unlikely(err))
25482+ goto out;
25483+ } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
25484+ || IS_APPEND(a->mvd_h_src_inode))) {
25485+ if (err)
25486+ a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
25487+ /* go on */
25488+ } else
25489+ goto out;
25490+
25491+ err = -EINVAL;
25492+ if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
25493+ a->mvd_bdst = find_lower_writable(a);
25494+ if (unlikely(a->mvd_bdst < 0)) {
25495+ a->mvd_errno = EAU_MVDOWN_BOTTOM;
25496+ AU_MVD_PR(dmsg, "no writable lower branch\n");
25497+ goto out;
25498+ }
25499+ } else {
25500+ a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
25501+ if (unlikely(a->mvd_bdst < 0
25502+ || au_sbbot(a->sb) < a->mvd_bdst)) {
25503+ a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
25504+ AU_MVD_PR(dmsg, "no lower brid\n");
25505+ goto out;
25506+ }
25507+ }
25508+
25509+ err = au_mvd_args_busy(dmsg, a);
25510+ if (!err)
25511+ err = au_mvd_args_parent(dmsg, a);
25512+ if (!err)
25513+ err = au_mvd_args_intermediate(dmsg, a);
25514+ if (!err)
25515+ err = au_mvd_args_exist(dmsg, a);
25516+ if (!err)
25517+ AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
25518+
25519+out:
25520+ AuTraceErr(err);
25521+ return err;
25522+}
25523+
25524+int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
25525+{
25526+ int err, e;
25527+ unsigned char dmsg;
25528+ struct au_mvd_args *args;
25529+ struct inode *inode;
25530+
25531+ inode = d_inode(dentry);
25532+ err = -EPERM;
25533+ if (unlikely(!capable(CAP_SYS_ADMIN)))
25534+ goto out;
25535+
25536+ err = -ENOMEM;
25537+ args = kmalloc(sizeof(*args), GFP_NOFS);
25538+ if (unlikely(!args))
25539+ goto out;
25540+
25541+ err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
25542+ if (!err)
25543+ /* VERIFY_WRITE */
25544+ err = !access_ok(uarg, sizeof(*uarg));
25545+ if (unlikely(err)) {
25546+ err = -EFAULT;
25547+ AuTraceErr(err);
25548+ goto out_free;
25549+ }
25550+ AuDbg("flags 0x%x\n", args->mvdown.flags);
25551+ args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
25552+ args->mvdown.au_errno = 0;
25553+ args->dentry = dentry;
25554+ args->inode = inode;
25555+ args->sb = dentry->d_sb;
25556+
25557+ err = -ENOENT;
25558+ dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
25559+ args->parent = dget_parent(dentry);
25560+ args->dir = d_inode(args->parent);
25561+ inode_lock_nested(args->dir, I_MUTEX_PARENT);
25562+ dput(args->parent);
25563+ if (unlikely(args->parent != dentry->d_parent)) {
25564+ AU_MVD_PR(dmsg, "parent dir is moved\n");
25565+ goto out_dir;
25566+ }
25567+
25568+ inode_lock_nested(inode, I_MUTEX_CHILD);
25569+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
25570+ if (unlikely(err))
25571+ goto out_inode;
25572+
25573+ di_write_lock_parent(args->parent);
25574+ err = au_mvd_args(dmsg, args);
25575+ if (unlikely(err))
25576+ goto out_parent;
25577+
25578+ err = au_do_mvdown(dmsg, args);
25579+ if (unlikely(err))
25580+ goto out_parent;
25581+
25582+ au_cpup_attr_timesizes(args->dir);
25583+ au_cpup_attr_timesizes(inode);
25584+ if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
25585+ au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
25586+ /* au_digen_dec(dentry); */
25587+
25588+out_parent:
25589+ di_write_unlock(args->parent);
25590+ aufs_read_unlock(dentry, AuLock_DW);
25591+out_inode:
25592+ inode_unlock(inode);
25593+out_dir:
25594+ inode_unlock(args->dir);
25595+out_free:
25596+ e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
25597+ if (unlikely(e))
25598+ err = -EFAULT;
25599+ au_kfree_rcu(args);
25600+out:
25601+ AuTraceErr(err);
25602+ return err;
25603+}
25604diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
25605--- /usr/share/empty/fs/aufs/opts.c 1970-01-01 01:00:00.000000000 +0100
25606+++ linux/fs/aufs/opts.c 2021-02-24 13:33:42.747680497 +0100
25607@@ -0,0 +1,1880 @@
25608+// SPDX-License-Identifier: GPL-2.0
25609+/*
25610+ * Copyright (C) 2005-2020 Junjiro R. Okajima
25611+ *
25612+ * This program, aufs is free software; you can redistribute it and/or modify
25613+ * it under the terms of the GNU General Public License as published by
25614+ * the Free Software Foundation; either version 2 of the License, or
25615+ * (at your option) any later version.
25616+ *
25617+ * This program is distributed in the hope that it will be useful,
25618+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
25619+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25620+ * GNU General Public License for more details.
25621+ *
25622+ * You should have received a copy of the GNU General Public License
25623+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
25624+ */
25625+
25626+/*
25627+ * mount options/flags
25628+ */
25629+
25630+#include <linux/namei.h>
25631+#include <linux/types.h> /* a distribution requires */
25632+#include <linux/parser.h>
25633+#include "aufs.h"
25634+
25635+/* ---------------------------------------------------------------------- */
25636+
25637+enum {
25638+ Opt_br,
25639+ Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
25640+ Opt_idel, Opt_imod,
25641+ Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
25642+ Opt_rdblk_def, Opt_rdhash_def,
25643+ Opt_xino, Opt_noxino,
25644+ Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
25645+ Opt_trunc_xino_path, Opt_itrunc_xino,
25646+ Opt_trunc_xib, Opt_notrunc_xib,
25647+ Opt_shwh, Opt_noshwh,
25648+ Opt_plink, Opt_noplink, Opt_list_plink,
25649+ Opt_udba,
25650+ Opt_dio, Opt_nodio,
25651+ Opt_diropq_a, Opt_diropq_w,
25652+ Opt_warn_perm, Opt_nowarn_perm,
25653+ Opt_wbr_copyup, Opt_wbr_create,
25654+ Opt_fhsm_sec,
25655+ Opt_verbose, Opt_noverbose,
25656+ Opt_sum, Opt_nosum, Opt_wsum,
25657+ Opt_dirperm1, Opt_nodirperm1,
25658+ Opt_dirren, Opt_nodirren,
25659+ Opt_acl, Opt_noacl,
25660+ Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
25661+};
25662+
25663+static match_table_t options = {
25664+ {Opt_br, "br=%s"},
25665+ {Opt_br, "br:%s"},
25666+
25667+ {Opt_add, "add=%d:%s"},
25668+ {Opt_add, "add:%d:%s"},
25669+ {Opt_add, "ins=%d:%s"},
25670+ {Opt_add, "ins:%d:%s"},
25671+ {Opt_append, "append=%s"},
25672+ {Opt_append, "append:%s"},
25673+ {Opt_prepend, "prepend=%s"},
25674+ {Opt_prepend, "prepend:%s"},
25675+
25676+ {Opt_del, "del=%s"},
25677+ {Opt_del, "del:%s"},
25678+ /* {Opt_idel, "idel:%d"}, */
25679+ {Opt_mod, "mod=%s"},
25680+ {Opt_mod, "mod:%s"},
25681+ /* {Opt_imod, "imod:%d:%s"}, */
25682+
25683+ {Opt_dirwh, "dirwh=%d"},
25684+
25685+ {Opt_xino, "xino=%s"},
25686+ {Opt_noxino, "noxino"},
25687+ {Opt_trunc_xino, "trunc_xino"},
25688+ {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
25689+ {Opt_notrunc_xino, "notrunc_xino"},
25690+ {Opt_trunc_xino_path, "trunc_xino=%s"},
25691+ {Opt_itrunc_xino, "itrunc_xino=%d"},
25692+ /* {Opt_zxino, "zxino=%s"}, */
25693+ {Opt_trunc_xib, "trunc_xib"},
25694+ {Opt_notrunc_xib, "notrunc_xib"},
25695+
25696+#ifdef CONFIG_PROC_FS
25697+ {Opt_plink, "plink"},
25698+#else
25699+ {Opt_ignore_silent, "plink"},
25700+#endif
25701+
25702+ {Opt_noplink, "noplink"},
25703+
25704+#ifdef CONFIG_AUFS_DEBUG
25705+ {Opt_list_plink, "list_plink"},
25706+#endif
25707+
25708+ {Opt_udba, "udba=%s"},
25709+
25710+ {Opt_dio, "dio"},
25711+ {Opt_nodio, "nodio"},
25712+
25713+#ifdef CONFIG_AUFS_DIRREN
25714+ {Opt_dirren, "dirren"},
25715+ {Opt_nodirren, "nodirren"},
25716+#else
25717+ {Opt_ignore, "dirren"},
25718+ {Opt_ignore_silent, "nodirren"},
25719+#endif
25720+
25721+#ifdef CONFIG_AUFS_FHSM
25722+ {Opt_fhsm_sec, "fhsm_sec=%d"},
25723+#else
25724+ {Opt_ignore, "fhsm_sec=%d"},
25725+#endif
25726+
25727+ {Opt_diropq_a, "diropq=always"},
25728+ {Opt_diropq_a, "diropq=a"},
25729+ {Opt_diropq_w, "diropq=whiteouted"},
25730+ {Opt_diropq_w, "diropq=w"},
25731+
25732+ {Opt_warn_perm, "warn_perm"},
25733+ {Opt_nowarn_perm, "nowarn_perm"},
25734+
25735+ /* keep them temporary */
25736+ {Opt_ignore_silent, "nodlgt"},
25737+ {Opt_ignore, "clean_plink"},
25738+
25739+#ifdef CONFIG_AUFS_SHWH
25740+ {Opt_shwh, "shwh"},
25741+#endif
25742+ {Opt_noshwh, "noshwh"},
25743+
25744+ {Opt_dirperm1, "dirperm1"},
25745+ {Opt_nodirperm1, "nodirperm1"},
25746+
25747+ {Opt_verbose, "verbose"},
25748+ {Opt_verbose, "v"},
25749+ {Opt_noverbose, "noverbose"},
25750+ {Opt_noverbose, "quiet"},
25751+ {Opt_noverbose, "q"},
25752+ {Opt_noverbose, "silent"},
25753+
25754+ {Opt_sum, "sum"},
25755+ {Opt_nosum, "nosum"},
25756+ {Opt_wsum, "wsum"},
25757+
25758+ {Opt_rdcache, "rdcache=%d"},
25759+ {Opt_rdblk, "rdblk=%d"},
25760+ {Opt_rdblk_def, "rdblk=def"},
25761+ {Opt_rdhash, "rdhash=%d"},
25762+ {Opt_rdhash_def, "rdhash=def"},
25763+
25764+ {Opt_wbr_create, "create=%s"},
25765+ {Opt_wbr_create, "create_policy=%s"},
25766+ {Opt_wbr_copyup, "cpup=%s"},
25767+ {Opt_wbr_copyup, "copyup=%s"},
25768+ {Opt_wbr_copyup, "copyup_policy=%s"},
25769+
25770+ /* generic VFS flag */
25771+#ifdef CONFIG_FS_POSIX_ACL
25772+ {Opt_acl, "acl"},
25773+ {Opt_noacl, "noacl"},
25774+#else
25775+ {Opt_ignore, "acl"},
25776+ {Opt_ignore_silent, "noacl"},
25777+#endif
25778+
25779+ /* internal use for the scripts */
25780+ {Opt_ignore_silent, "si=%s"},
25781+
25782+ {Opt_br, "dirs=%s"},
25783+ {Opt_ignore, "debug=%d"},
25784+ {Opt_ignore, "delete=whiteout"},
25785+ {Opt_ignore, "delete=all"},
25786+ {Opt_ignore, "imap=%s"},
25787+
25788+ /* temporary workaround, due to old mount(8)? */
25789+ {Opt_ignore_silent, "relatime"},
25790+
25791+ {Opt_err, NULL}
25792+};
25793+
25794+/* ---------------------------------------------------------------------- */
25795+
25796+static const char *au_parser_pattern(int val, match_table_t tbl)
25797+{
25798+ struct match_token *p;
25799+
25800+ p = tbl;
25801+ while (p->pattern) {
25802+ if (p->token == val)
25803+ return p->pattern;
25804+ p++;
25805+ }
25806+ BUG();
25807+ return "??";
25808+}
25809+
25810+static const char *au_optstr(int *val, match_table_t tbl)
25811+{
25812+ struct match_token *p;
25813+ int v;
25814+
25815+ v = *val;
25816+ if (!v)
25817+ goto out;
25818+ p = tbl;
25819+ while (p->pattern) {
25820+ if (p->token
25821+ && (v & p->token) == p->token) {
25822+ *val &= ~p->token;
25823+ return p->pattern;
25824+ }
25825+ p++;
25826+ }
25827+
25828+out:
25829+ return NULL;
25830+}
25831+
25832+/* ---------------------------------------------------------------------- */
25833+
25834+static match_table_t brperm = {
25835+ {AuBrPerm_RO, AUFS_BRPERM_RO},
25836+ {AuBrPerm_RR, AUFS_BRPERM_RR},
25837+ {AuBrPerm_RW, AUFS_BRPERM_RW},
25838+ {0, NULL}
25839+};
25840+
25841+static match_table_t brattr = {
25842+ /* general */
25843+ {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
25844+ {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
25845+ /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
25846+ {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
25847+#ifdef CONFIG_AUFS_FHSM
25848+ {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
25849+#endif
25850+#ifdef CONFIG_AUFS_XATTR
25851+ {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
25852+ {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
25853+ {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
25854+ {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
25855+ {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
25856+ {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
25857+#endif
25858+
25859+ /* ro/rr branch */
25860+ {AuBrRAttr_WH, AUFS_BRRATTR_WH},
25861+
25862+ /* rw branch */
25863+ {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
25864+ {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
25865+
25866+ {0, NULL}
25867+};
25868+
25869+static int br_attr_val(char *str, match_table_t table, substring_t args[])
25870+{
25871+ int attr, v;
25872+ char *p;
25873+
25874+ attr = 0;
25875+ do {
25876+ p = strchr(str, '+');
25877+ if (p)
25878+ *p = 0;
25879+ v = match_token(str, table, args);
25880+ if (v) {
25881+ if (v & AuBrAttr_CMOO_Mask)
25882+ attr &= ~AuBrAttr_CMOO_Mask;
25883+ attr |= v;
25884+ } else {
25885+ if (p)
25886+ *p = '+';
25887+ pr_warn("ignored branch attribute %s\n", str);
25888+ break;
25889+ }
25890+ if (p)
25891+ str = p + 1;
25892+ } while (p);
25893+
25894+ return attr;
25895+}
25896+
25897+static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
25898+{
25899+ int sz;
25900+ const char *p;
25901+ char *q;
25902+
25903+ q = str->a;
25904+ *q = 0;
25905+ p = au_optstr(&perm, brattr);
25906+ if (p) {
25907+ sz = strlen(p);
25908+ memcpy(q, p, sz + 1);
25909+ q += sz;
25910+ } else
25911+ goto out;
25912+
25913+ do {
25914+ p = au_optstr(&perm, brattr);
25915+ if (p) {
25916+ *q++ = '+';
25917+ sz = strlen(p);
25918+ memcpy(q, p, sz + 1);
25919+ q += sz;
25920+ }
25921+ } while (p);
25922+
25923+out:
25924+ return q - str->a;
25925+}
25926+
25927+static int noinline_for_stack br_perm_val(char *perm)
25928+{
25929+ int val, bad, sz;
25930+ char *p;
25931+ substring_t args[MAX_OPT_ARGS];
25932+ au_br_perm_str_t attr;
25933+
25934+ p = strchr(perm, '+');
25935+ if (p)
25936+ *p = 0;
25937+ val = match_token(perm, brperm, args);
25938+ if (!val) {
25939+ if (p)
25940+ *p = '+';
25941+ pr_warn("ignored branch permission %s\n", perm);
25942+ val = AuBrPerm_RO;
25943+ goto out;
25944+ }
25945+ if (!p)
25946+ goto out;
25947+
25948+ val |= br_attr_val(p + 1, brattr, args);
25949+
25950+ bad = 0;
25951+ switch (val & AuBrPerm_Mask) {
25952+ case AuBrPerm_RO:
25953+ case AuBrPerm_RR:
25954+ bad = val & AuBrWAttr_Mask;
25955+ val &= ~AuBrWAttr_Mask;
25956+ break;
25957+ case AuBrPerm_RW:
25958+ bad = val & AuBrRAttr_Mask;
25959+ val &= ~AuBrRAttr_Mask;
25960+ break;
25961+ }
25962+
25963+ /*
25964+ * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
25965+ * does not treat it as an error, just warning.
25966+ * this is a tiny guard for the user operation.
25967+ */
25968+ if (val & AuBrAttr_UNPIN) {
25969+ bad |= AuBrAttr_UNPIN;
25970+ val &= ~AuBrAttr_UNPIN;
25971+ }
25972+
25973+ if (unlikely(bad)) {
25974+ sz = au_do_optstr_br_attr(&attr, bad);
25975+ AuDebugOn(!sz);
25976+ pr_warn("ignored branch attribute %s\n", attr.a);
25977+ }
25978+
25979+out:
25980+ return val;
25981+}
25982+
25983+void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
25984+{
25985+ au_br_perm_str_t attr;
25986+ const char *p;
25987+ char *q;
25988+ int sz;
25989+
25990+ q = str->a;
25991+ p = au_optstr(&perm, brperm);
25992+ AuDebugOn(!p || !*p);
25993+ sz = strlen(p);
25994+ memcpy(q, p, sz + 1);
25995+ q += sz;
25996+
25997+ sz = au_do_optstr_br_attr(&attr, perm);
25998+ if (sz) {
25999+ *q++ = '+';
26000+ memcpy(q, attr.a, sz + 1);
26001+ }
26002+
26003+ AuDebugOn(strlen(str->a) >= sizeof(str->a));
26004+}
26005+
26006+/* ---------------------------------------------------------------------- */
26007+
26008+static match_table_t udbalevel = {
26009+ {AuOpt_UDBA_REVAL, "reval"},
26010+ {AuOpt_UDBA_NONE, "none"},
26011+#ifdef CONFIG_AUFS_HNOTIFY
26012+ {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
26013+#ifdef CONFIG_AUFS_HFSNOTIFY
26014+ {AuOpt_UDBA_HNOTIFY, "fsnotify"},
26015+#endif
26016+#endif
26017+ {-1, NULL}
26018+};
26019+
26020+static int noinline_for_stack udba_val(char *str)
26021+{
26022+ substring_t args[MAX_OPT_ARGS];
26023+
26024+ return match_token(str, udbalevel, args);
26025+}
26026+
26027+const char *au_optstr_udba(int udba)
26028+{
26029+ return au_parser_pattern(udba, udbalevel);
26030+}
26031+
26032+/* ---------------------------------------------------------------------- */
26033+
26034+static match_table_t au_wbr_create_policy = {
26035+ {AuWbrCreate_TDP, "tdp"},
26036+ {AuWbrCreate_TDP, "top-down-parent"},
26037+ {AuWbrCreate_RR, "rr"},
26038+ {AuWbrCreate_RR, "round-robin"},
26039+ {AuWbrCreate_MFS, "mfs"},
26040+ {AuWbrCreate_MFS, "most-free-space"},
26041+ {AuWbrCreate_MFSV, "mfs:%d"},
26042+ {AuWbrCreate_MFSV, "most-free-space:%d"},
26043+
26044+ /* top-down regardless the parent, and then mfs */
26045+ {AuWbrCreate_TDMFS, "tdmfs:%d"},
26046+ {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
26047+
26048+ {AuWbrCreate_MFSRR, "mfsrr:%d"},
26049+ {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
26050+ {AuWbrCreate_PMFS, "pmfs"},
26051+ {AuWbrCreate_PMFSV, "pmfs:%d"},
26052+ {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
26053+ {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
26054+
26055+ {-1, NULL}
26056+};
26057+
26058+static int au_wbr_mfs_wmark(substring_t *arg, char *str,
26059+ struct au_opt_wbr_create *create)
26060+{
26061+ int err;
26062+ unsigned long long ull;
26063+
26064+ err = 0;
26065+ if (!match_u64(arg, &ull))
26066+ create->mfsrr_watermark = ull;
26067+ else {
26068+ pr_err("bad integer in %s\n", str);
26069+ err = -EINVAL;
26070+ }
26071+
26072+ return err;
26073+}
26074+
26075+static int au_wbr_mfs_sec(substring_t *arg, char *str,
26076+ struct au_opt_wbr_create *create)
26077+{
26078+ int n, err;
26079+
26080+ err = 0;
26081+ if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
26082+ create->mfs_second = n;
26083+ else {
26084+ pr_err("bad integer in %s\n", str);
26085+ err = -EINVAL;
26086+ }
26087+
26088+ return err;
26089+}
26090+
26091+static int noinline_for_stack
26092+au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
26093+{
26094+ int err, e;
26095+ substring_t args[MAX_OPT_ARGS];
26096+
26097+ err = match_token(str, au_wbr_create_policy, args);
26098+ create->wbr_create = err;
26099+ switch (err) {
26100+ case AuWbrCreate_MFSRRV:
26101+ case AuWbrCreate_TDMFSV:
26102+ case AuWbrCreate_PMFSRRV:
26103+ e = au_wbr_mfs_wmark(&args[0], str, create);
26104+ if (!e)
26105+ e = au_wbr_mfs_sec(&args[1], str, create);
26106+ if (unlikely(e))
26107+ err = e;
26108+ break;
26109+ case AuWbrCreate_MFSRR:
26110+ case AuWbrCreate_TDMFS:
26111+ case AuWbrCreate_PMFSRR:
26112+ e = au_wbr_mfs_wmark(&args[0], str, create);
26113+ if (unlikely(e)) {
26114+ err = e;
26115+ break;
26116+ }
26117+ fallthrough;
26118+ case AuWbrCreate_MFS:
26119+ case AuWbrCreate_PMFS:
26120+ create->mfs_second = AUFS_MFS_DEF_SEC;
26121+ break;
26122+ case AuWbrCreate_MFSV:
26123+ case AuWbrCreate_PMFSV:
26124+ e = au_wbr_mfs_sec(&args[0], str, create);
26125+ if (unlikely(e))
26126+ err = e;
26127+ break;
26128+ }
26129+
26130+ return err;
26131+}
26132+
26133+const char *au_optstr_wbr_create(int wbr_create)
26134+{
26135+ return au_parser_pattern(wbr_create, au_wbr_create_policy);
26136+}
26137+
26138+static match_table_t au_wbr_copyup_policy = {
26139+ {AuWbrCopyup_TDP, "tdp"},
26140+ {AuWbrCopyup_TDP, "top-down-parent"},
26141+ {AuWbrCopyup_BUP, "bup"},
26142+ {AuWbrCopyup_BUP, "bottom-up-parent"},
26143+ {AuWbrCopyup_BU, "bu"},
26144+ {AuWbrCopyup_BU, "bottom-up"},
26145+ {-1, NULL}
26146+};
26147+
26148+static int noinline_for_stack au_wbr_copyup_val(char *str)
26149+{
26150+ substring_t args[MAX_OPT_ARGS];
26151+
26152+ return match_token(str, au_wbr_copyup_policy, args);
26153+}
26154+
26155+const char *au_optstr_wbr_copyup(int wbr_copyup)
26156+{
26157+ return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
26158+}
26159+
26160+/* ---------------------------------------------------------------------- */
26161+
26162+static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
26163+
26164+static void dump_opts(struct au_opts *opts)
26165+{
26166+#ifdef CONFIG_AUFS_DEBUG
26167+ /* reduce stack space */
26168+ union {
26169+ struct au_opt_add *add;
26170+ struct au_opt_del *del;
26171+ struct au_opt_mod *mod;
26172+ struct au_opt_xino *xino;
26173+ struct au_opt_xino_itrunc *xino_itrunc;
26174+ struct au_opt_wbr_create *create;
26175+ } u;
26176+ struct au_opt *opt;
26177+
26178+ opt = opts->opt;
26179+ while (opt->type != Opt_tail) {
26180+ switch (opt->type) {
26181+ case Opt_add:
26182+ u.add = &opt->add;
26183+ AuDbg("add {b%d, %s, 0x%x, %p}\n",
26184+ u.add->bindex, u.add->pathname, u.add->perm,
26185+ u.add->path.dentry);
26186+ break;
26187+ case Opt_del:
26188+ case Opt_idel:
26189+ u.del = &opt->del;
26190+ AuDbg("del {%s, %p}\n",
26191+ u.del->pathname, u.del->h_path.dentry);
26192+ break;
26193+ case Opt_mod:
26194+ case Opt_imod:
26195+ u.mod = &opt->mod;
26196+ AuDbg("mod {%s, 0x%x, %p}\n",
26197+ u.mod->path, u.mod->perm, u.mod->h_root);
26198+ break;
26199+ case Opt_append:
26200+ u.add = &opt->add;
26201+ AuDbg("append {b%d, %s, 0x%x, %p}\n",
26202+ u.add->bindex, u.add->pathname, u.add->perm,
26203+ u.add->path.dentry);
26204+ break;
26205+ case Opt_prepend:
26206+ u.add = &opt->add;
26207+ AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
26208+ u.add->bindex, u.add->pathname, u.add->perm,
26209+ u.add->path.dentry);
26210+ break;
26211+ case Opt_dirwh:
26212+ AuDbg("dirwh %d\n", opt->dirwh);
26213+ break;
26214+ case Opt_rdcache:
26215+ AuDbg("rdcache %d\n", opt->rdcache);
26216+ break;
26217+ case Opt_rdblk:
26218+ AuDbg("rdblk %u\n", opt->rdblk);
26219+ break;
26220+ case Opt_rdblk_def:
26221+ AuDbg("rdblk_def\n");
26222+ break;
26223+ case Opt_rdhash:
26224+ AuDbg("rdhash %u\n", opt->rdhash);
26225+ break;
26226+ case Opt_rdhash_def:
26227+ AuDbg("rdhash_def\n");
26228+ break;
26229+ case Opt_xino:
26230+ u.xino = &opt->xino;
26231+ AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
26232+ break;
26233+ case Opt_trunc_xino:
26234+ AuLabel(trunc_xino);
26235+ break;
26236+ case Opt_notrunc_xino:
26237+ AuLabel(notrunc_xino);
26238+ break;
26239+ case Opt_trunc_xino_path:
26240+ case Opt_itrunc_xino:
26241+ u.xino_itrunc = &opt->xino_itrunc;
26242+ AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
26243+ break;
26244+ case Opt_noxino:
26245+ AuLabel(noxino);
26246+ break;
26247+ case Opt_trunc_xib:
26248+ AuLabel(trunc_xib);
26249+ break;
26250+ case Opt_notrunc_xib:
26251+ AuLabel(notrunc_xib);
26252+ break;
26253+ case Opt_shwh:
26254+ AuLabel(shwh);
26255+ break;
26256+ case Opt_noshwh:
26257+ AuLabel(noshwh);
26258+ break;
26259+ case Opt_dirperm1:
26260+ AuLabel(dirperm1);
26261+ break;
26262+ case Opt_nodirperm1:
26263+ AuLabel(nodirperm1);
26264+ break;
26265+ case Opt_plink:
26266+ AuLabel(plink);
26267+ break;
26268+ case Opt_noplink:
26269+ AuLabel(noplink);
26270+ break;
26271+ case Opt_list_plink:
26272+ AuLabel(list_plink);
26273+ break;
26274+ case Opt_udba:
26275+ AuDbg("udba %d, %s\n",
26276+ opt->udba, au_optstr_udba(opt->udba));
26277+ break;
26278+ case Opt_dio:
26279+ AuLabel(dio);
26280+ break;
26281+ case Opt_nodio:
26282+ AuLabel(nodio);
26283+ break;
26284+ case Opt_diropq_a:
26285+ AuLabel(diropq_a);
26286+ break;
26287+ case Opt_diropq_w:
26288+ AuLabel(diropq_w);
26289+ break;
26290+ case Opt_warn_perm:
26291+ AuLabel(warn_perm);
26292+ break;
26293+ case Opt_nowarn_perm:
26294+ AuLabel(nowarn_perm);
26295+ break;
26296+ case Opt_verbose:
26297+ AuLabel(verbose);
26298+ break;
26299+ case Opt_noverbose:
26300+ AuLabel(noverbose);
26301+ break;
26302+ case Opt_sum:
26303+ AuLabel(sum);
26304+ break;
26305+ case Opt_nosum:
26306+ AuLabel(nosum);
26307+ break;
26308+ case Opt_wsum:
26309+ AuLabel(wsum);
26310+ break;
26311+ case Opt_wbr_create:
26312+ u.create = &opt->wbr_create;
26313+ AuDbg("create %d, %s\n", u.create->wbr_create,
26314+ au_optstr_wbr_create(u.create->wbr_create));
26315+ switch (u.create->wbr_create) {
26316+ case AuWbrCreate_MFSV:
26317+ case AuWbrCreate_PMFSV:
26318+ AuDbg("%d sec\n", u.create->mfs_second);
26319+ break;
26320+ case AuWbrCreate_MFSRR:
26321+ case AuWbrCreate_TDMFS:
26322+ AuDbg("%llu watermark\n",
26323+ u.create->mfsrr_watermark);
26324+ break;
26325+ case AuWbrCreate_MFSRRV:
26326+ case AuWbrCreate_TDMFSV:
26327+ case AuWbrCreate_PMFSRRV:
26328+ AuDbg("%llu watermark, %d sec\n",
26329+ u.create->mfsrr_watermark,
26330+ u.create->mfs_second);
26331+ break;
26332+ }
26333+ break;
26334+ case Opt_wbr_copyup:
26335+ AuDbg("copyup %d, %s\n", opt->wbr_copyup,
26336+ au_optstr_wbr_copyup(opt->wbr_copyup));
26337+ break;
26338+ case Opt_fhsm_sec:
26339+ AuDbg("fhsm_sec %u\n", opt->fhsm_second);
26340+ break;
26341+ case Opt_dirren:
26342+ AuLabel(dirren);
26343+ break;
26344+ case Opt_nodirren:
26345+ AuLabel(nodirren);
26346+ break;
26347+ case Opt_acl:
26348+ AuLabel(acl);
26349+ break;
26350+ case Opt_noacl:
26351+ AuLabel(noacl);
26352+ break;
26353+ default:
26354+ BUG();
26355+ }
26356+ opt++;
26357+ }
26358+#endif
26359+}
26360+
26361+void au_opts_free(struct au_opts *opts)
26362+{
26363+ struct au_opt *opt;
26364+
26365+ opt = opts->opt;
26366+ while (opt->type != Opt_tail) {
26367+ switch (opt->type) {
26368+ case Opt_add:
26369+ case Opt_append:
26370+ case Opt_prepend:
26371+ path_put(&opt->add.path);
26372+ break;
26373+ case Opt_del:
26374+ case Opt_idel:
26375+ path_put(&opt->del.h_path);
26376+ break;
26377+ case Opt_mod:
26378+ case Opt_imod:
26379+ dput(opt->mod.h_root);
26380+ break;
26381+ case Opt_xino:
26382+ fput(opt->xino.file);
26383+ break;
26384+ }
26385+ opt++;
26386+ }
26387+}
26388+
26389+static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
26390+ aufs_bindex_t bindex)
26391+{
26392+ int err;
26393+ struct au_opt_add *add = &opt->add;
26394+ char *p;
26395+
26396+ add->bindex = bindex;
26397+ add->perm = AuBrPerm_RO;
26398+ add->pathname = opt_str;
26399+ p = strchr(opt_str, '=');
26400+ if (p) {
26401+ *p++ = 0;
26402+ if (*p)
26403+ add->perm = br_perm_val(p);
26404+ }
26405+
26406+ err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
26407+ if (!err) {
26408+ if (!p) {
26409+ add->perm = AuBrPerm_RO;
26410+ if (au_test_fs_rr(add->path.dentry->d_sb))
26411+ add->perm = AuBrPerm_RR;
26412+ else if (!bindex && !(sb_flags & SB_RDONLY))
26413+ add->perm = AuBrPerm_RW;
26414+ }
26415+ opt->type = Opt_add;
26416+ goto out;
26417+ }
26418+ pr_err("lookup failed %s (%d)\n", add->pathname, err);
26419+ err = -EINVAL;
26420+
26421+out:
26422+ return err;
26423+}
26424+
26425+static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
26426+{
26427+ int err;
26428+
26429+ del->pathname = args[0].from;
26430+ AuDbg("del path %s\n", del->pathname);
26431+
26432+ err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
26433+ if (unlikely(err))
26434+ pr_err("lookup failed %s (%d)\n", del->pathname, err);
26435+
26436+ return err;
26437+}
26438+
26439+#if 0 /* reserved for future use */
26440+static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
26441+ struct au_opt_del *del, substring_t args[])
26442+{
26443+ int err;
26444+ struct dentry *root;
26445+
26446+ err = -EINVAL;
26447+ root = sb->s_root;
26448+ aufs_read_lock(root, AuLock_FLUSH);
26449+ if (bindex < 0 || au_sbbot(sb) < bindex) {
26450+ pr_err("out of bounds, %d\n", bindex);
26451+ goto out;
26452+ }
26453+
26454+ err = 0;
26455+ del->h_path.dentry = dget(au_h_dptr(root, bindex));
26456+ del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
26457+
26458+out:
26459+ aufs_read_unlock(root, !AuLock_IR);
26460+ return err;
26461+}
26462+#endif
26463+
26464+static int noinline_for_stack
26465+au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
26466+{
26467+ int err;
26468+ struct path path;
26469+ char *p;
26470+
26471+ err = -EINVAL;
26472+ mod->path = args[0].from;
26473+ p = strchr(mod->path, '=');
26474+ if (unlikely(!p)) {
26475+ pr_err("no permission %s\n", args[0].from);
26476+ goto out;
26477+ }
26478+
26479+ *p++ = 0;
26480+ err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
26481+ if (unlikely(err)) {
26482+ pr_err("lookup failed %s (%d)\n", mod->path, err);
26483+ goto out;
26484+ }
26485+
26486+ mod->perm = br_perm_val(p);
26487+ AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
26488+ mod->h_root = dget(path.dentry);
26489+ path_put(&path);
26490+
26491+out:
26492+ return err;
26493+}
26494+
26495+#if 0 /* reserved for future use */
26496+static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
26497+ struct au_opt_mod *mod, substring_t args[])
26498+{
26499+ int err;
26500+ struct dentry *root;
26501+
26502+ err = -EINVAL;
26503+ root = sb->s_root;
26504+ aufs_read_lock(root, AuLock_FLUSH);
26505+ if (bindex < 0 || au_sbbot(sb) < bindex) {
26506+ pr_err("out of bounds, %d\n", bindex);
26507+ goto out;
26508+ }
26509+
26510+ err = 0;
26511+ mod->perm = br_perm_val(args[1].from);
26512+ AuDbg("mod path %s, perm 0x%x, %s\n",
26513+ mod->path, mod->perm, args[1].from);
26514+ mod->h_root = dget(au_h_dptr(root, bindex));
26515+
26516+out:
26517+ aufs_read_unlock(root, !AuLock_IR);
26518+ return err;
26519+}
26520+#endif
26521+
26522+static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
26523+ substring_t args[])
26524+{
26525+ int err;
26526+ struct file *file;
26527+
26528+ file = au_xino_create(sb, args[0].from, /*silent*/0, /*wbrtop*/0);
26529+ err = PTR_ERR(file);
26530+ if (IS_ERR(file))
26531+ goto out;
26532+
26533+ err = -EINVAL;
26534+ if (unlikely(file->f_path.dentry->d_sb == sb)) {
26535+ fput(file);
26536+ pr_err("%s must be outside\n", args[0].from);
26537+ goto out;
26538+ }
26539+
26540+ err = 0;
26541+ xino->file = file;
26542+ xino->path = args[0].from;
26543+
26544+out:
26545+ return err;
26546+}
26547+
26548+static int noinline_for_stack
26549+au_opts_parse_xino_itrunc_path(struct super_block *sb,
26550+ struct au_opt_xino_itrunc *xino_itrunc,
26551+ substring_t args[])
26552+{
26553+ int err;
26554+ aufs_bindex_t bbot, bindex;
26555+ struct path path;
26556+ struct dentry *root;
26557+
26558+ err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
26559+ if (unlikely(err)) {
26560+ pr_err("lookup failed %s (%d)\n", args[0].from, err);
26561+ goto out;
26562+ }
26563+
26564+ xino_itrunc->bindex = -1;
26565+ root = sb->s_root;
26566+ aufs_read_lock(root, AuLock_FLUSH);
26567+ bbot = au_sbbot(sb);
26568+ for (bindex = 0; bindex <= bbot; bindex++) {
26569+ if (au_h_dptr(root, bindex) == path.dentry) {
26570+ xino_itrunc->bindex = bindex;
26571+ break;
26572+ }
26573+ }
26574+ aufs_read_unlock(root, !AuLock_IR);
26575+ path_put(&path);
26576+
26577+ if (unlikely(xino_itrunc->bindex < 0)) {
26578+ pr_err("no such branch %s\n", args[0].from);
26579+ err = -EINVAL;
26580+ }
26581+
26582+out:
26583+ return err;
26584+}
26585+
26586+/* called without aufs lock */
26587+int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
26588+{
26589+ int err, n, token;
26590+ aufs_bindex_t bindex;
26591+ unsigned char skipped;
26592+ struct dentry *root;
26593+ struct au_opt *opt, *opt_tail;
26594+ char *opt_str;
26595+ /* reduce the stack space */
26596+ union {
26597+ struct au_opt_xino_itrunc *xino_itrunc;
26598+ struct au_opt_wbr_create *create;
26599+ } u;
26600+ struct {
26601+ substring_t args[MAX_OPT_ARGS];
26602+ } *a;
26603+
26604+ err = -ENOMEM;
26605+ a = kmalloc(sizeof(*a), GFP_NOFS);
26606+ if (unlikely(!a))
26607+ goto out;
26608+
26609+ root = sb->s_root;
26610+ err = 0;
26611+ bindex = 0;
26612+ opt = opts->opt;
26613+ opt_tail = opt + opts->max_opt - 1;
26614+ opt->type = Opt_tail;
26615+ while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
26616+ err = -EINVAL;
26617+ skipped = 0;
26618+ token = match_token(opt_str, options, a->args);
26619+ switch (token) {
26620+ case Opt_br:
26621+ err = 0;
26622+ while (!err && (opt_str = strsep(&a->args[0].from, ":"))
26623+ && *opt_str) {
26624+ err = opt_add(opt, opt_str, opts->sb_flags,
26625+ bindex++);
26626+ if (unlikely(!err && ++opt > opt_tail)) {
26627+ err = -E2BIG;
26628+ break;
26629+ }
26630+ opt->type = Opt_tail;
26631+ skipped = 1;
26632+ }
26633+ break;
26634+ case Opt_add:
26635+ if (unlikely(match_int(&a->args[0], &n))) {
26636+ pr_err("bad integer in %s\n", opt_str);
26637+ break;
26638+ }
26639+ bindex = n;
26640+ err = opt_add(opt, a->args[1].from, opts->sb_flags,
26641+ bindex);
26642+ if (!err)
26643+ opt->type = token;
26644+ break;
26645+ case Opt_append:
26646+ err = opt_add(opt, a->args[0].from, opts->sb_flags,
26647+ /*dummy bindex*/1);
26648+ if (!err)
26649+ opt->type = token;
26650+ break;
26651+ case Opt_prepend:
26652+ err = opt_add(opt, a->args[0].from, opts->sb_flags,
26653+ /*bindex*/0);
26654+ if (!err)
26655+ opt->type = token;
26656+ break;
26657+ case Opt_del:
26658+ err = au_opts_parse_del(&opt->del, a->args);
26659+ if (!err)
26660+ opt->type = token;
26661+ break;
26662+#if 0 /* reserved for future use */
26663+ case Opt_idel:
26664+ del->pathname = "(indexed)";
26665+ if (unlikely(match_int(&args[0], &n))) {
26666+ pr_err("bad integer in %s\n", opt_str);
26667+ break;
26668+ }
26669+ err = au_opts_parse_idel(sb, n, &opt->del, a->args);
26670+ if (!err)
26671+ opt->type = token;
26672+ break;
26673+#endif
26674+ case Opt_mod:
26675+ err = au_opts_parse_mod(&opt->mod, a->args);
26676+ if (!err)
26677+ opt->type = token;
26678+ break;
26679+#ifdef IMOD /* reserved for future use */
26680+ case Opt_imod:
26681+ u.mod->path = "(indexed)";
26682+ if (unlikely(match_int(&a->args[0], &n))) {
26683+ pr_err("bad integer in %s\n", opt_str);
26684+ break;
26685+ }
26686+ err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
26687+ if (!err)
26688+ opt->type = token;
26689+ break;
26690+#endif
26691+ case Opt_xino:
26692+ err = au_opts_parse_xino(sb, &opt->xino, a->args);
26693+ if (!err)
26694+ opt->type = token;
26695+ break;
26696+
26697+ case Opt_trunc_xino_path:
26698+ err = au_opts_parse_xino_itrunc_path
26699+ (sb, &opt->xino_itrunc, a->args);
26700+ if (!err)
26701+ opt->type = token;
26702+ break;
26703+
26704+ case Opt_itrunc_xino:
26705+ u.xino_itrunc = &opt->xino_itrunc;
26706+ if (unlikely(match_int(&a->args[0], &n))) {
26707+ pr_err("bad integer in %s\n", opt_str);
26708+ break;
26709+ }
26710+ u.xino_itrunc->bindex = n;
26711+ aufs_read_lock(root, AuLock_FLUSH);
26712+ if (n < 0 || au_sbbot(sb) < n) {
26713+ pr_err("out of bounds, %d\n", n);
26714+ aufs_read_unlock(root, !AuLock_IR);
26715+ break;
26716+ }
26717+ aufs_read_unlock(root, !AuLock_IR);
26718+ err = 0;
26719+ opt->type = token;
26720+ break;
26721+
26722+ case Opt_dirwh:
26723+ if (unlikely(match_int(&a->args[0], &opt->dirwh)))
26724+ break;
26725+ err = 0;
26726+ opt->type = token;
26727+ break;
26728+
26729+ case Opt_rdcache:
26730+ if (unlikely(match_int(&a->args[0], &n))) {
26731+ pr_err("bad integer in %s\n", opt_str);
26732+ break;
26733+ }
26734+ if (unlikely(n > AUFS_RDCACHE_MAX)) {
26735+ pr_err("rdcache must be smaller than %d\n",
26736+ AUFS_RDCACHE_MAX);
26737+ break;
26738+ }
26739+ opt->rdcache = n;
26740+ err = 0;
26741+ opt->type = token;
26742+ break;
26743+ case Opt_rdblk:
26744+ if (unlikely(match_int(&a->args[0], &n)
26745+ || n < 0
26746+ || n > KMALLOC_MAX_SIZE)) {
26747+ pr_err("bad integer in %s\n", opt_str);
26748+ break;
26749+ }
26750+ if (unlikely(n && n < NAME_MAX)) {
26751+ pr_err("rdblk must be larger than %d\n",
26752+ NAME_MAX);
26753+ break;
26754+ }
26755+ opt->rdblk = n;
26756+ err = 0;
26757+ opt->type = token;
26758+ break;
26759+ case Opt_rdhash:
26760+ if (unlikely(match_int(&a->args[0], &n)
26761+ || n < 0
26762+ || n * sizeof(struct hlist_head)
26763+ > KMALLOC_MAX_SIZE)) {
26764+ pr_err("bad integer in %s\n", opt_str);
26765+ break;
26766+ }
26767+ opt->rdhash = n;
26768+ err = 0;
26769+ opt->type = token;
26770+ break;
26771+
26772+ case Opt_trunc_xino:
26773+ case Opt_notrunc_xino:
26774+ case Opt_noxino:
26775+ case Opt_trunc_xib:
26776+ case Opt_notrunc_xib:
26777+ case Opt_shwh:
26778+ case Opt_noshwh:
26779+ case Opt_dirperm1:
26780+ case Opt_nodirperm1:
26781+ case Opt_plink:
26782+ case Opt_noplink:
26783+ case Opt_list_plink:
26784+ case Opt_dio:
26785+ case Opt_nodio:
26786+ case Opt_diropq_a:
26787+ case Opt_diropq_w:
26788+ case Opt_warn_perm:
26789+ case Opt_nowarn_perm:
26790+ case Opt_verbose:
26791+ case Opt_noverbose:
26792+ case Opt_sum:
26793+ case Opt_nosum:
26794+ case Opt_wsum:
26795+ case Opt_rdblk_def:
26796+ case Opt_rdhash_def:
26797+ case Opt_dirren:
26798+ case Opt_nodirren:
26799+ case Opt_acl:
26800+ case Opt_noacl:
26801+ err = 0;
26802+ opt->type = token;
26803+ break;
26804+
26805+ case Opt_udba:
26806+ opt->udba = udba_val(a->args[0].from);
26807+ if (opt->udba >= 0) {
26808+ err = 0;
26809+ opt->type = token;
26810+ } else
26811+ pr_err("wrong value, %s\n", opt_str);
26812+ break;
26813+
26814+ case Opt_wbr_create:
26815+ u.create = &opt->wbr_create;
26816+ u.create->wbr_create
26817+ = au_wbr_create_val(a->args[0].from, u.create);
26818+ if (u.create->wbr_create >= 0) {
26819+ err = 0;
26820+ opt->type = token;
26821+ } else
26822+ pr_err("wrong value, %s\n", opt_str);
26823+ break;
26824+ case Opt_wbr_copyup:
26825+ opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
26826+ if (opt->wbr_copyup >= 0) {
26827+ err = 0;
26828+ opt->type = token;
26829+ } else
26830+ pr_err("wrong value, %s\n", opt_str);
26831+ break;
26832+
26833+ case Opt_fhsm_sec:
26834+ if (unlikely(match_int(&a->args[0], &n)
26835+ || n < 0)) {
26836+ pr_err("bad integer in %s\n", opt_str);
26837+ break;
26838+ }
26839+ if (sysaufs_brs) {
26840+ opt->fhsm_second = n;
26841+ opt->type = token;
26842+ } else
26843+ pr_warn("ignored %s\n", opt_str);
26844+ err = 0;
26845+ break;
26846+
26847+ case Opt_ignore:
26848+ pr_warn("ignored %s\n", opt_str);
26849+ fallthrough;
26850+ case Opt_ignore_silent:
26851+ skipped = 1;
26852+ err = 0;
26853+ break;
26854+ case Opt_err:
26855+ pr_err("unknown option %s\n", opt_str);
26856+ break;
26857+ }
26858+
26859+ if (!err && !skipped) {
26860+ if (unlikely(++opt > opt_tail)) {
26861+ err = -E2BIG;
26862+ opt--;
26863+ opt->type = Opt_tail;
26864+ break;
26865+ }
26866+ opt->type = Opt_tail;
26867+ }
26868+ }
26869+
26870+ au_kfree_rcu(a);
26871+ dump_opts(opts);
26872+ if (unlikely(err))
26873+ au_opts_free(opts);
26874+
26875+out:
26876+ return err;
26877+}
26878+
26879+static int au_opt_wbr_create(struct super_block *sb,
26880+ struct au_opt_wbr_create *create)
26881+{
26882+ int err;
26883+ struct au_sbinfo *sbinfo;
26884+
26885+ SiMustWriteLock(sb);
26886+
26887+ err = 1; /* handled */
26888+ sbinfo = au_sbi(sb);
26889+ if (sbinfo->si_wbr_create_ops->fin) {
26890+ err = sbinfo->si_wbr_create_ops->fin(sb);
26891+ if (!err)
26892+ err = 1;
26893+ }
26894+
26895+ sbinfo->si_wbr_create = create->wbr_create;
26896+ sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
26897+ switch (create->wbr_create) {
26898+ case AuWbrCreate_MFSRRV:
26899+ case AuWbrCreate_MFSRR:
26900+ case AuWbrCreate_TDMFS:
26901+ case AuWbrCreate_TDMFSV:
26902+ case AuWbrCreate_PMFSRR:
26903+ case AuWbrCreate_PMFSRRV:
26904+ sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
26905+ fallthrough;
26906+ case AuWbrCreate_MFS:
26907+ case AuWbrCreate_MFSV:
26908+ case AuWbrCreate_PMFS:
26909+ case AuWbrCreate_PMFSV:
26910+ sbinfo->si_wbr_mfs.mfs_expire
26911+ = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
26912+ break;
26913+ }
26914+
26915+ if (sbinfo->si_wbr_create_ops->init)
26916+ sbinfo->si_wbr_create_ops->init(sb); /* ignore */
26917+
26918+ return err;
26919+}
26920+
26921+/*
26922+ * returns,
26923+ * plus: processed without an error
26924+ * zero: unprocessed
26925+ */
26926+static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
26927+ struct au_opts *opts)
26928+{
26929+ int err;
26930+ struct au_sbinfo *sbinfo;
26931+
26932+ SiMustWriteLock(sb);
26933+
26934+ err = 1; /* handled */
26935+ sbinfo = au_sbi(sb);
26936+ switch (opt->type) {
26937+ case Opt_udba:
26938+ sbinfo->si_mntflags &= ~AuOptMask_UDBA;
26939+ sbinfo->si_mntflags |= opt->udba;
26940+ opts->given_udba |= opt->udba;
26941+ break;
26942+
26943+ case Opt_plink:
26944+ au_opt_set(sbinfo->si_mntflags, PLINK);
26945+ break;
26946+ case Opt_noplink:
26947+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
26948+ au_plink_put(sb, /*verbose*/1);
26949+ au_opt_clr(sbinfo->si_mntflags, PLINK);
26950+ break;
26951+ case Opt_list_plink:
26952+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
26953+ au_plink_list(sb);
26954+ break;
26955+
26956+ case Opt_dio:
26957+ au_opt_set(sbinfo->si_mntflags, DIO);
26958+ au_fset_opts(opts->flags, REFRESH_DYAOP);
26959+ break;
26960+ case Opt_nodio:
26961+ au_opt_clr(sbinfo->si_mntflags, DIO);
26962+ au_fset_opts(opts->flags, REFRESH_DYAOP);
26963+ break;
26964+
26965+ case Opt_fhsm_sec:
26966+ au_fhsm_set(sbinfo, opt->fhsm_second);
26967+ break;
26968+
26969+ case Opt_diropq_a:
26970+ au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
26971+ break;
26972+ case Opt_diropq_w:
26973+ au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
26974+ break;
26975+
26976+ case Opt_warn_perm:
26977+ au_opt_set(sbinfo->si_mntflags, WARN_PERM);
26978+ break;
26979+ case Opt_nowarn_perm:
26980+ au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
26981+ break;
26982+
26983+ case Opt_verbose:
26984+ au_opt_set(sbinfo->si_mntflags, VERBOSE);
26985+ break;
26986+ case Opt_noverbose:
26987+ au_opt_clr(sbinfo->si_mntflags, VERBOSE);
26988+ break;
26989+
26990+ case Opt_sum:
26991+ au_opt_set(sbinfo->si_mntflags, SUM);
26992+ break;
26993+ case Opt_wsum:
26994+ au_opt_clr(sbinfo->si_mntflags, SUM);
26995+ au_opt_set(sbinfo->si_mntflags, SUM_W);
26996+ break;
26997+ case Opt_nosum:
26998+ au_opt_clr(sbinfo->si_mntflags, SUM);
26999+ au_opt_clr(sbinfo->si_mntflags, SUM_W);
27000+ break;
27001+
27002+ case Opt_wbr_create:
27003+ err = au_opt_wbr_create(sb, &opt->wbr_create);
27004+ break;
27005+ case Opt_wbr_copyup:
27006+ sbinfo->si_wbr_copyup = opt->wbr_copyup;
27007+ sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
27008+ break;
27009+
27010+ case Opt_dirwh:
27011+ sbinfo->si_dirwh = opt->dirwh;
27012+ break;
27013+
27014+ case Opt_rdcache:
27015+ sbinfo->si_rdcache
27016+ = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
27017+ break;
27018+ case Opt_rdblk:
27019+ sbinfo->si_rdblk = opt->rdblk;
27020+ break;
27021+ case Opt_rdblk_def:
27022+ sbinfo->si_rdblk = AUFS_RDBLK_DEF;
27023+ break;
27024+ case Opt_rdhash:
27025+ sbinfo->si_rdhash = opt->rdhash;
27026+ break;
27027+ case Opt_rdhash_def:
27028+ sbinfo->si_rdhash = AUFS_RDHASH_DEF;
27029+ break;
27030+
27031+ case Opt_shwh:
27032+ au_opt_set(sbinfo->si_mntflags, SHWH);
27033+ break;
27034+ case Opt_noshwh:
27035+ au_opt_clr(sbinfo->si_mntflags, SHWH);
27036+ break;
27037+
27038+ case Opt_dirperm1:
27039+ au_opt_set(sbinfo->si_mntflags, DIRPERM1);
27040+ break;
27041+ case Opt_nodirperm1:
27042+ au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
27043+ break;
27044+
27045+ case Opt_trunc_xino:
27046+ au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
27047+ break;
27048+ case Opt_notrunc_xino:
27049+ au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
27050+ break;
27051+
27052+ case Opt_trunc_xino_path:
27053+ case Opt_itrunc_xino:
27054+ err = au_xino_trunc(sb, opt->xino_itrunc.bindex,
27055+ /*idx_begin*/0);
27056+ if (!err)
27057+ err = 1;
27058+ break;
27059+
27060+ case Opt_trunc_xib:
27061+ au_fset_opts(opts->flags, TRUNC_XIB);
27062+ break;
27063+ case Opt_notrunc_xib:
27064+ au_fclr_opts(opts->flags, TRUNC_XIB);
27065+ break;
27066+
27067+ case Opt_dirren:
27068+ err = 1;
27069+ if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27070+ err = au_dr_opt_set(sb);
27071+ if (!err)
27072+ err = 1;
27073+ }
27074+ if (err == 1)
27075+ au_opt_set(sbinfo->si_mntflags, DIRREN);
27076+ break;
27077+ case Opt_nodirren:
27078+ err = 1;
27079+ if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27080+ err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
27081+ DR_FLUSHED));
27082+ if (!err)
27083+ err = 1;
27084+ }
27085+ if (err == 1)
27086+ au_opt_clr(sbinfo->si_mntflags, DIRREN);
27087+ break;
27088+
27089+ case Opt_acl:
27090+ sb->s_flags |= SB_POSIXACL;
27091+ break;
27092+ case Opt_noacl:
27093+ sb->s_flags &= ~SB_POSIXACL;
27094+ break;
27095+
27096+ default:
27097+ err = 0;
27098+ break;
27099+ }
27100+
27101+ return err;
27102+}
27103+
27104+/*
27105+ * returns tri-state.
27106+ * plus: processed without an error
27107+ * zero: unprocessed
27108+ * minus: error
27109+ */
27110+static int au_opt_br(struct super_block *sb, struct au_opt *opt,
27111+ struct au_opts *opts)
27112+{
27113+ int err, do_refresh;
27114+
27115+ err = 0;
27116+ switch (opt->type) {
27117+ case Opt_append:
27118+ opt->add.bindex = au_sbbot(sb) + 1;
27119+ if (opt->add.bindex < 0)
27120+ opt->add.bindex = 0;
27121+ goto add;
27122+ /* Always goto add, not fallthrough */
27123+ case Opt_prepend:
27124+ opt->add.bindex = 0;
27125+ fallthrough;
27126+ add: /* indented label */
27127+ case Opt_add:
27128+ err = au_br_add(sb, &opt->add,
27129+ au_ftest_opts(opts->flags, REMOUNT));
27130+ if (!err) {
27131+ err = 1;
27132+ au_fset_opts(opts->flags, REFRESH);
27133+ }
27134+ break;
27135+
27136+ case Opt_del:
27137+ case Opt_idel:
27138+ err = au_br_del(sb, &opt->del,
27139+ au_ftest_opts(opts->flags, REMOUNT));
27140+ if (!err) {
27141+ err = 1;
27142+ au_fset_opts(opts->flags, TRUNC_XIB);
27143+ au_fset_opts(opts->flags, REFRESH);
27144+ }
27145+ break;
27146+
27147+ case Opt_mod:
27148+ case Opt_imod:
27149+ err = au_br_mod(sb, &opt->mod,
27150+ au_ftest_opts(opts->flags, REMOUNT),
27151+ &do_refresh);
27152+ if (!err) {
27153+ err = 1;
27154+ if (do_refresh)
27155+ au_fset_opts(opts->flags, REFRESH);
27156+ }
27157+ break;
27158+ }
27159+ return err;
27160+}
27161+
27162+static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
27163+ struct au_opt_xino **opt_xino,
27164+ struct au_opts *opts)
27165+{
27166+ int err;
27167+
27168+ err = 0;
27169+ switch (opt->type) {
27170+ case Opt_xino:
27171+ err = au_xino_set(sb, &opt->xino,
27172+ !!au_ftest_opts(opts->flags, REMOUNT));
27173+ if (unlikely(err))
27174+ break;
27175+
27176+ *opt_xino = &opt->xino;
27177+ break;
27178+
27179+ case Opt_noxino:
27180+ au_xino_clr(sb);
27181+ *opt_xino = (void *)-1;
27182+ break;
27183+ }
27184+
27185+ return err;
27186+}
27187+
27188+int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27189+ unsigned int pending)
27190+{
27191+ int err, fhsm;
27192+ aufs_bindex_t bindex, bbot;
27193+ unsigned char do_plink, skip, do_free, can_no_dreval;
27194+ struct au_branch *br;
27195+ struct au_wbr *wbr;
27196+ struct dentry *root, *dentry;
27197+ struct inode *dir, *h_dir;
27198+ struct au_sbinfo *sbinfo;
27199+ struct au_hinode *hdir;
27200+
27201+ SiMustAnyLock(sb);
27202+
27203+ sbinfo = au_sbi(sb);
27204+ AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
27205+
27206+ if (!(sb_flags & SB_RDONLY)) {
27207+ if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
27208+ pr_warn("first branch should be rw\n");
27209+ if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
27210+ pr_warn_once("shwh should be used with ro\n");
27211+ }
27212+
27213+ if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
27214+ && !au_opt_test(sbinfo->si_mntflags, XINO))
27215+ pr_warn_once("udba=*notify requires xino\n");
27216+
27217+ if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
27218+ pr_warn_once("dirperm1 breaks the protection"
27219+ " by the permission bits on the lower branch\n");
27220+
27221+ err = 0;
27222+ fhsm = 0;
27223+ root = sb->s_root;
27224+ dir = d_inode(root);
27225+ do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
27226+ can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
27227+ UDBA_NONE);
27228+ bbot = au_sbbot(sb);
27229+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
27230+ skip = 0;
27231+ h_dir = au_h_iptr(dir, bindex);
27232+ br = au_sbr(sb, bindex);
27233+
27234+ if ((br->br_perm & AuBrAttr_ICEX)
27235+ && !h_dir->i_op->listxattr)
27236+ br->br_perm &= ~AuBrAttr_ICEX;
27237+#if 0 /* untested */
27238+ if ((br->br_perm & AuBrAttr_ICEX_SEC)
27239+ && (au_br_sb(br)->s_flags & SB_NOSEC))
27240+ br->br_perm &= ~AuBrAttr_ICEX_SEC;
27241+#endif
27242+
27243+ do_free = 0;
27244+ wbr = br->br_wbr;
27245+ if (wbr)
27246+ wbr_wh_read_lock(wbr);
27247+
27248+ if (!au_br_writable(br->br_perm)) {
27249+ do_free = !!wbr;
27250+ skip = (!wbr
27251+ || (!wbr->wbr_whbase
27252+ && !wbr->wbr_plink
27253+ && !wbr->wbr_orph));
27254+ } else if (!au_br_wh_linkable(br->br_perm)) {
27255+ /* skip = (!br->br_whbase && !br->br_orph); */
27256+ skip = (!wbr || !wbr->wbr_whbase);
27257+ if (skip && wbr) {
27258+ if (do_plink)
27259+ skip = !!wbr->wbr_plink;
27260+ else
27261+ skip = !wbr->wbr_plink;
27262+ }
27263+ } else {
27264+ /* skip = (br->br_whbase && br->br_ohph); */
27265+ skip = (wbr && wbr->wbr_whbase);
27266+ if (skip) {
27267+ if (do_plink)
27268+ skip = !!wbr->wbr_plink;
27269+ else
27270+ skip = !wbr->wbr_plink;
27271+ }
27272+ }
27273+ if (wbr)
27274+ wbr_wh_read_unlock(wbr);
27275+
27276+ if (can_no_dreval) {
27277+ dentry = br->br_path.dentry;
27278+ spin_lock(&dentry->d_lock);
27279+ if (dentry->d_flags &
27280+ (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
27281+ can_no_dreval = 0;
27282+ spin_unlock(&dentry->d_lock);
27283+ }
27284+
27285+ if (au_br_fhsm(br->br_perm)) {
27286+ fhsm++;
27287+ AuDebugOn(!br->br_fhsm);
27288+ }
27289+
27290+ if (skip)
27291+ continue;
27292+
27293+ hdir = au_hi(dir, bindex);
27294+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
27295+ if (wbr)
27296+ wbr_wh_write_lock(wbr);
27297+ err = au_wh_init(br, sb);
27298+ if (wbr)
27299+ wbr_wh_write_unlock(wbr);
27300+ au_hn_inode_unlock(hdir);
27301+
27302+ if (!err && do_free) {
27303+ au_kfree_rcu(wbr);
27304+ br->br_wbr = NULL;
27305+ }
27306+ }
27307+
27308+ if (can_no_dreval)
27309+ au_fset_si(sbinfo, NO_DREVAL);
27310+ else
27311+ au_fclr_si(sbinfo, NO_DREVAL);
27312+
27313+ if (fhsm >= 2) {
27314+ au_fset_si(sbinfo, FHSM);
27315+ for (bindex = bbot; bindex >= 0; bindex--) {
27316+ br = au_sbr(sb, bindex);
27317+ if (au_br_fhsm(br->br_perm)) {
27318+ au_fhsm_set_bottom(sb, bindex);
27319+ break;
27320+ }
27321+ }
27322+ } else {
27323+ au_fclr_si(sbinfo, FHSM);
27324+ au_fhsm_set_bottom(sb, -1);
27325+ }
27326+
27327+ return err;
27328+}
27329+
27330+int au_opts_mount(struct super_block *sb, struct au_opts *opts)
27331+{
27332+ int err;
27333+ unsigned int tmp;
27334+ aufs_bindex_t bindex, bbot;
27335+ struct au_opt *opt;
27336+ struct au_opt_xino *opt_xino, xino;
27337+ struct au_sbinfo *sbinfo;
27338+ struct au_branch *br;
27339+ struct inode *dir;
27340+
27341+ SiMustWriteLock(sb);
27342+
27343+ err = 0;
27344+ opt_xino = NULL;
27345+ opt = opts->opt;
27346+ while (err >= 0 && opt->type != Opt_tail)
27347+ err = au_opt_simple(sb, opt++, opts);
27348+ if (err > 0)
27349+ err = 0;
27350+ else if (unlikely(err < 0))
27351+ goto out;
27352+
27353+ /* disable xino and udba temporary */
27354+ sbinfo = au_sbi(sb);
27355+ tmp = sbinfo->si_mntflags;
27356+ au_opt_clr(sbinfo->si_mntflags, XINO);
27357+ au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
27358+
27359+ opt = opts->opt;
27360+ while (err >= 0 && opt->type != Opt_tail)
27361+ err = au_opt_br(sb, opt++, opts);
27362+ if (err > 0)
27363+ err = 0;
27364+ else if (unlikely(err < 0))
27365+ goto out;
27366+
27367+ bbot = au_sbbot(sb);
27368+ if (unlikely(bbot < 0)) {
27369+ err = -EINVAL;
27370+ pr_err("no branches\n");
27371+ goto out;
27372+ }
27373+
27374+ if (au_opt_test(tmp, XINO))
27375+ au_opt_set(sbinfo->si_mntflags, XINO);
27376+ opt = opts->opt;
27377+ while (!err && opt->type != Opt_tail)
27378+ err = au_opt_xino(sb, opt++, &opt_xino, opts);
27379+ if (unlikely(err))
27380+ goto out;
27381+
27382+ err = au_opts_verify(sb, sb->s_flags, tmp);
27383+ if (unlikely(err))
27384+ goto out;
27385+
27386+ /* restore xino */
27387+ if (au_opt_test(tmp, XINO) && !opt_xino) {
27388+ xino.file = au_xino_def(sb);
27389+ err = PTR_ERR(xino.file);
27390+ if (IS_ERR(xino.file))
27391+ goto out;
27392+
27393+ err = au_xino_set(sb, &xino, /*remount*/0);
27394+ fput(xino.file);
27395+ if (unlikely(err))
27396+ goto out;
27397+ }
27398+
27399+ /* restore udba */
27400+ tmp &= AuOptMask_UDBA;
27401+ sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27402+ sbinfo->si_mntflags |= tmp;
27403+ bbot = au_sbbot(sb);
27404+ for (bindex = 0; bindex <= bbot; bindex++) {
27405+ br = au_sbr(sb, bindex);
27406+ err = au_hnotify_reset_br(tmp, br, br->br_perm);
27407+ if (unlikely(err))
27408+ AuIOErr("hnotify failed on br %d, %d, ignored\n",
27409+ bindex, err);
27410+ /* go on even if err */
27411+ }
27412+ if (au_opt_test(tmp, UDBA_HNOTIFY)) {
27413+ dir = d_inode(sb->s_root);
27414+ au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
27415+ }
27416+
27417+out:
27418+ return err;
27419+}
27420+
27421+int au_opts_remount(struct super_block *sb, struct au_opts *opts)
27422+{
27423+ int err, rerr;
27424+ unsigned char no_dreval;
27425+ struct inode *dir;
27426+ struct au_opt_xino *opt_xino;
27427+ struct au_opt *opt;
27428+ struct au_sbinfo *sbinfo;
27429+
27430+ SiMustWriteLock(sb);
27431+
27432+ err = au_dr_opt_flush(sb);
27433+ if (unlikely(err))
27434+ goto out;
27435+ au_fset_opts(opts->flags, DR_FLUSHED);
27436+
27437+ dir = d_inode(sb->s_root);
27438+ sbinfo = au_sbi(sb);
27439+ opt_xino = NULL;
27440+ opt = opts->opt;
27441+ while (err >= 0 && opt->type != Opt_tail) {
27442+ err = au_opt_simple(sb, opt, opts);
27443+ if (!err)
27444+ err = au_opt_br(sb, opt, opts);
27445+ if (!err)
27446+ err = au_opt_xino(sb, opt, &opt_xino, opts);
27447+ opt++;
27448+ }
27449+ if (err > 0)
27450+ err = 0;
27451+ AuTraceErr(err);
27452+ /* go on even err */
27453+
27454+ no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
27455+ rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
27456+ if (unlikely(rerr && !err))
27457+ err = rerr;
27458+
27459+ if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
27460+ au_fset_opts(opts->flags, REFRESH_IDOP);
27461+
27462+ if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
27463+ rerr = au_xib_trunc(sb);
27464+ if (unlikely(rerr && !err))
27465+ err = rerr;
27466+ }
27467+
27468+ /* will be handled by the caller */
27469+ if (!au_ftest_opts(opts->flags, REFRESH)
27470+ && (opts->given_udba
27471+ || au_opt_test(sbinfo->si_mntflags, XINO)
27472+ || au_ftest_opts(opts->flags, REFRESH_IDOP)
27473+ ))
27474+ au_fset_opts(opts->flags, REFRESH);
27475+
27476+ AuDbg("status 0x%x\n", opts->flags);
27477+
27478+out:
27479+ return err;
27480+}
27481+
27482+/* ---------------------------------------------------------------------- */
27483+
27484+unsigned int au_opt_udba(struct super_block *sb)
27485+{
27486+ return au_mntflags(sb) & AuOptMask_UDBA;
27487+}
27488diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
27489--- /usr/share/empty/fs/aufs/opts.h 1970-01-01 01:00:00.000000000 +0100
27490+++ linux/fs/aufs/opts.h 2021-02-24 13:33:42.747680497 +0100
27491@@ -0,0 +1,225 @@
27492+/* SPDX-License-Identifier: GPL-2.0 */
27493+/*
27494+ * Copyright (C) 2005-2020 Junjiro R. Okajima
27495+ *
27496+ * This program, aufs is free software; you can redistribute it and/or modify
27497+ * it under the terms of the GNU General Public License as published by
27498+ * the Free Software Foundation; either version 2 of the License, or
27499+ * (at your option) any later version.
27500+ *
27501+ * This program is distributed in the hope that it will be useful,
27502+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
27503+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27504+ * GNU General Public License for more details.
27505+ *
27506+ * You should have received a copy of the GNU General Public License
27507+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
27508+ */
27509+
27510+/*
27511+ * mount options/flags
27512+ */
27513+
27514+#ifndef __AUFS_OPTS_H__
27515+#define __AUFS_OPTS_H__
27516+
27517+#ifdef __KERNEL__
27518+
27519+#include <linux/path.h>
27520+
27521+struct file;
27522+
27523+/* ---------------------------------------------------------------------- */
27524+
27525+/* mount flags */
27526+#define AuOpt_XINO 1 /* external inode number bitmap
27527+ and translation table */
27528+#define AuOpt_TRUNC_XINO (1 << 1) /* truncate xino files */
27529+#define AuOpt_UDBA_NONE (1 << 2) /* users direct branch access */
27530+#define AuOpt_UDBA_REVAL (1 << 3)
27531+#define AuOpt_UDBA_HNOTIFY (1 << 4)
27532+#define AuOpt_SHWH (1 << 5) /* show whiteout */
27533+#define AuOpt_PLINK (1 << 6) /* pseudo-link */
27534+#define AuOpt_DIRPERM1 (1 << 7) /* ignore the lower dir's perm
27535+ bits */
27536+#define AuOpt_ALWAYS_DIROPQ (1 << 9) /* policy to creating diropq */
27537+#define AuOpt_SUM (1 << 10) /* summation for statfs(2) */
27538+#define AuOpt_SUM_W (1 << 11) /* unimplemented */
27539+#define AuOpt_WARN_PERM (1 << 12) /* warn when add-branch */
27540+#define AuOpt_VERBOSE (1 << 13) /* print the cause of error */
27541+#define AuOpt_DIO (1 << 14) /* direct io */
27542+#define AuOpt_DIRREN (1 << 15) /* directory rename */
27543+
27544+#ifndef CONFIG_AUFS_HNOTIFY
27545+#undef AuOpt_UDBA_HNOTIFY
27546+#define AuOpt_UDBA_HNOTIFY 0
27547+#endif
27548+#ifndef CONFIG_AUFS_DIRREN
27549+#undef AuOpt_DIRREN
27550+#define AuOpt_DIRREN 0
27551+#endif
27552+#ifndef CONFIG_AUFS_SHWH
27553+#undef AuOpt_SHWH
27554+#define AuOpt_SHWH 0
27555+#endif
27556+
27557+#define AuOpt_Def (AuOpt_XINO \
27558+ | AuOpt_UDBA_REVAL \
27559+ | AuOpt_PLINK \
27560+ /* | AuOpt_DIRPERM1 */ \
27561+ | AuOpt_WARN_PERM)
27562+#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
27563+ | AuOpt_UDBA_REVAL \
27564+ | AuOpt_UDBA_HNOTIFY)
27565+
27566+#define au_opt_test(flags, name) (flags & AuOpt_##name)
27567+#define au_opt_set(flags, name) do { \
27568+ BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
27569+ ((flags) |= AuOpt_##name); \
27570+} while (0)
27571+#define au_opt_set_udba(flags, name) do { \
27572+ (flags) &= ~AuOptMask_UDBA; \
27573+ ((flags) |= AuOpt_##name); \
27574+} while (0)
27575+#define au_opt_clr(flags, name) do { \
27576+ ((flags) &= ~AuOpt_##name); \
27577+} while (0)
27578+
27579+static inline unsigned int au_opts_plink(unsigned int mntflags)
27580+{
27581+#ifdef CONFIG_PROC_FS
27582+ return mntflags;
27583+#else
27584+ return mntflags & ~AuOpt_PLINK;
27585+#endif
27586+}
27587+
27588+/* ---------------------------------------------------------------------- */
27589+
27590+/* policies to select one among multiple writable branches */
27591+enum {
27592+ AuWbrCreate_TDP, /* top down parent */
27593+ AuWbrCreate_RR, /* round robin */
27594+ AuWbrCreate_MFS, /* most free space */
27595+ AuWbrCreate_MFSV, /* mfs with seconds */
27596+ AuWbrCreate_MFSRR, /* mfs then rr */
27597+ AuWbrCreate_MFSRRV, /* mfs then rr with seconds */
27598+ AuWbrCreate_TDMFS, /* top down regardless parent and mfs */
27599+ AuWbrCreate_TDMFSV, /* top down regardless parent and mfs */
27600+ AuWbrCreate_PMFS, /* parent and mfs */
27601+ AuWbrCreate_PMFSV, /* parent and mfs with seconds */
27602+ AuWbrCreate_PMFSRR, /* parent, mfs and round-robin */
27603+ AuWbrCreate_PMFSRRV, /* plus seconds */
27604+
27605+ AuWbrCreate_Def = AuWbrCreate_TDP
27606+};
27607+
27608+enum {
27609+ AuWbrCopyup_TDP, /* top down parent */
27610+ AuWbrCopyup_BUP, /* bottom up parent */
27611+ AuWbrCopyup_BU, /* bottom up */
27612+
27613+ AuWbrCopyup_Def = AuWbrCopyup_TDP
27614+};
27615+
27616+/* ---------------------------------------------------------------------- */
27617+
27618+struct au_opt_add {
27619+ aufs_bindex_t bindex;
27620+ char *pathname;
27621+ int perm;
27622+ struct path path;
27623+};
27624+
27625+struct au_opt_del {
27626+ char *pathname;
27627+ struct path h_path;
27628+};
27629+
27630+struct au_opt_mod {
27631+ char *path;
27632+ int perm;
27633+ struct dentry *h_root;
27634+};
27635+
27636+struct au_opt_xino {
27637+ char *path;
27638+ struct file *file;
27639+};
27640+
27641+struct au_opt_xino_itrunc {
27642+ aufs_bindex_t bindex;
27643+};
27644+
27645+struct au_opt_wbr_create {
27646+ int wbr_create;
27647+ int mfs_second;
27648+ unsigned long long mfsrr_watermark;
27649+};
27650+
27651+struct au_opt {
27652+ int type;
27653+ union {
27654+ struct au_opt_xino xino;
27655+ struct au_opt_xino_itrunc xino_itrunc;
27656+ struct au_opt_add add;
27657+ struct au_opt_del del;
27658+ struct au_opt_mod mod;
27659+ int dirwh;
27660+ int rdcache;
27661+ unsigned int rdblk;
27662+ unsigned int rdhash;
27663+ int udba;
27664+ struct au_opt_wbr_create wbr_create;
27665+ int wbr_copyup;
27666+ unsigned int fhsm_second;
27667+ };
27668+};
27669+
27670+/* opts flags */
27671+#define AuOpts_REMOUNT 1
27672+#define AuOpts_REFRESH (1 << 1)
27673+#define AuOpts_TRUNC_XIB (1 << 2)
27674+#define AuOpts_REFRESH_DYAOP (1 << 3)
27675+#define AuOpts_REFRESH_IDOP (1 << 4)
27676+#define AuOpts_DR_FLUSHED (1 << 5)
27677+#define au_ftest_opts(flags, name) ((flags) & AuOpts_##name)
27678+#define au_fset_opts(flags, name) \
27679+ do { (flags) |= AuOpts_##name; } while (0)
27680+#define au_fclr_opts(flags, name) \
27681+ do { (flags) &= ~AuOpts_##name; } while (0)
27682+
27683+#ifndef CONFIG_AUFS_DIRREN
27684+#undef AuOpts_DR_FLUSHED
27685+#define AuOpts_DR_FLUSHED 0
27686+#endif
27687+
27688+struct au_opts {
27689+ struct au_opt *opt;
27690+ int max_opt;
27691+
27692+ unsigned int given_udba;
27693+ unsigned int flags;
27694+ unsigned long sb_flags;
27695+};
27696+
27697+/* ---------------------------------------------------------------------- */
27698+
27699+/* opts.c */
27700+void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
27701+const char *au_optstr_udba(int udba);
27702+const char *au_optstr_wbr_copyup(int wbr_copyup);
27703+const char *au_optstr_wbr_create(int wbr_create);
27704+
27705+void au_opts_free(struct au_opts *opts);
27706+struct super_block;
27707+int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
27708+int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27709+ unsigned int pending);
27710+int au_opts_mount(struct super_block *sb, struct au_opts *opts);
27711+int au_opts_remount(struct super_block *sb, struct au_opts *opts);
27712+
27713+unsigned int au_opt_udba(struct super_block *sb);
27714+
27715+#endif /* __KERNEL__ */
27716+#endif /* __AUFS_OPTS_H__ */
27717diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
27718--- /usr/share/empty/fs/aufs/plink.c 1970-01-01 01:00:00.000000000 +0100
27719+++ linux/fs/aufs/plink.c 2021-02-24 13:33:42.747680497 +0100
27720@@ -0,0 +1,516 @@
27721+// SPDX-License-Identifier: GPL-2.0
27722+/*
27723+ * Copyright (C) 2005-2020 Junjiro R. Okajima
27724+ *
27725+ * This program, aufs is free software; you can redistribute it and/or modify
27726+ * it under the terms of the GNU General Public License as published by
27727+ * the Free Software Foundation; either version 2 of the License, or
27728+ * (at your option) any later version.
27729+ *
27730+ * This program is distributed in the hope that it will be useful,
27731+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
27732+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27733+ * GNU General Public License for more details.
27734+ *
27735+ * You should have received a copy of the GNU General Public License
27736+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
27737+ */
27738+
27739+/*
27740+ * pseudo-link
27741+ */
27742+
27743+#include "aufs.h"
27744+
27745+/*
27746+ * the pseudo-link maintenance mode.
27747+ * during a user process maintains the pseudo-links,
27748+ * prohibit adding a new plink and branch manipulation.
27749+ *
27750+ * Flags
27751+ * NOPLM:
27752+ * For entry functions which will handle plink, and i_mutex is already held
27753+ * in VFS.
27754+ * They cannot wait and should return an error at once.
27755+ * Callers has to check the error.
27756+ * NOPLMW:
27757+ * For entry functions which will handle plink, but i_mutex is not held
27758+ * in VFS.
27759+ * They can wait the plink maintenance mode to finish.
27760+ *
27761+ * They behave like F_SETLK and F_SETLKW.
27762+ * If the caller never handle plink, then both flags are unnecessary.
27763+ */
27764+
27765+int au_plink_maint(struct super_block *sb, int flags)
27766+{
27767+ int err;
27768+ pid_t pid, ppid;
27769+ struct task_struct *parent, *prev;
27770+ struct au_sbinfo *sbi;
27771+
27772+ SiMustAnyLock(sb);
27773+
27774+ err = 0;
27775+ if (!au_opt_test(au_mntflags(sb), PLINK))
27776+ goto out;
27777+
27778+ sbi = au_sbi(sb);
27779+ pid = sbi->si_plink_maint_pid;
27780+ if (!pid || pid == current->pid)
27781+ goto out;
27782+
27783+ /* todo: it highly depends upon /sbin/mount.aufs */
27784+ prev = NULL;
27785+ parent = current;
27786+ ppid = 0;
27787+ rcu_read_lock();
27788+ while (1) {
27789+ parent = rcu_dereference(parent->real_parent);
27790+ if (parent == prev)
27791+ break;
27792+ ppid = task_pid_vnr(parent);
27793+ if (pid == ppid) {
27794+ rcu_read_unlock();
27795+ goto out;
27796+ }
27797+ prev = parent;
27798+ }
27799+ rcu_read_unlock();
27800+
27801+ if (au_ftest_lock(flags, NOPLMW)) {
27802+ /* if there is no i_mutex lock in VFS, we don't need to wait */
27803+ /* AuDebugOn(!lockdep_depth(current)); */
27804+ while (sbi->si_plink_maint_pid) {
27805+ si_read_unlock(sb);
27806+ /* gave up wake_up_bit() */
27807+ wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
27808+
27809+ if (au_ftest_lock(flags, FLUSH))
27810+ au_nwt_flush(&sbi->si_nowait);
27811+ si_noflush_read_lock(sb);
27812+ }
27813+ } else if (au_ftest_lock(flags, NOPLM)) {
27814+ AuDbg("ppid %d, pid %d\n", ppid, pid);
27815+ err = -EAGAIN;
27816+ }
27817+
27818+out:
27819+ return err;
27820+}
27821+
27822+void au_plink_maint_leave(struct au_sbinfo *sbinfo)
27823+{
27824+ spin_lock(&sbinfo->si_plink_maint_lock);
27825+ sbinfo->si_plink_maint_pid = 0;
27826+ spin_unlock(&sbinfo->si_plink_maint_lock);
27827+ wake_up_all(&sbinfo->si_plink_wq);
27828+}
27829+
27830+int au_plink_maint_enter(struct super_block *sb)
27831+{
27832+ int err;
27833+ struct au_sbinfo *sbinfo;
27834+
27835+ err = 0;
27836+ sbinfo = au_sbi(sb);
27837+ /* make sure i am the only one in this fs */
27838+ si_write_lock(sb, AuLock_FLUSH);
27839+ if (au_opt_test(au_mntflags(sb), PLINK)) {
27840+ spin_lock(&sbinfo->si_plink_maint_lock);
27841+ if (!sbinfo->si_plink_maint_pid)
27842+ sbinfo->si_plink_maint_pid = current->pid;
27843+ else
27844+ err = -EBUSY;
27845+ spin_unlock(&sbinfo->si_plink_maint_lock);
27846+ }
27847+ si_write_unlock(sb);
27848+
27849+ return err;
27850+}
27851+
27852+/* ---------------------------------------------------------------------- */
27853+
27854+#ifdef CONFIG_AUFS_DEBUG
27855+void au_plink_list(struct super_block *sb)
27856+{
27857+ int i;
27858+ struct au_sbinfo *sbinfo;
27859+ struct hlist_bl_head *hbl;
27860+ struct hlist_bl_node *pos;
27861+ struct au_icntnr *icntnr;
27862+
27863+ SiMustAnyLock(sb);
27864+
27865+ sbinfo = au_sbi(sb);
27866+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
27867+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
27868+
27869+ for (i = 0; i < AuPlink_NHASH; i++) {
27870+ hbl = sbinfo->si_plink + i;
27871+ hlist_bl_lock(hbl);
27872+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
27873+ AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
27874+ hlist_bl_unlock(hbl);
27875+ }
27876+}
27877+#endif
27878+
27879+/* is the inode pseudo-linked? */
27880+int au_plink_test(struct inode *inode)
27881+{
27882+ int found, i;
27883+ struct au_sbinfo *sbinfo;
27884+ struct hlist_bl_head *hbl;
27885+ struct hlist_bl_node *pos;
27886+ struct au_icntnr *icntnr;
27887+
27888+ sbinfo = au_sbi(inode->i_sb);
27889+ AuRwMustAnyLock(&sbinfo->si_rwsem);
27890+ AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
27891+ AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
27892+
27893+ found = 0;
27894+ i = au_plink_hash(inode->i_ino);
27895+ hbl = sbinfo->si_plink + i;
27896+ hlist_bl_lock(hbl);
27897+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
27898+ if (&icntnr->vfs_inode == inode) {
27899+ found = 1;
27900+ break;
27901+ }
27902+ hlist_bl_unlock(hbl);
27903+ return found;
27904+}
27905+
27906+/* ---------------------------------------------------------------------- */
27907+
27908+/*
27909+ * generate a name for plink.
27910+ * the file will be stored under AUFS_WH_PLINKDIR.
27911+ */
27912+/* 20 is max digits length of ulong 64 */
27913+#define PLINK_NAME_LEN ((20 + 1) * 2)
27914+
27915+static int plink_name(char *name, int len, struct inode *inode,
27916+ aufs_bindex_t bindex)
27917+{
27918+ int rlen;
27919+ struct inode *h_inode;
27920+
27921+ h_inode = au_h_iptr(inode, bindex);
27922+ rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
27923+ return rlen;
27924+}
27925+
27926+struct au_do_plink_lkup_args {
27927+ struct dentry **errp;
27928+ struct qstr *tgtname;
27929+ struct dentry *h_parent;
27930+ struct au_branch *br;
27931+};
27932+
27933+static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
27934+ struct dentry *h_parent,
27935+ struct au_branch *br)
27936+{
27937+ struct dentry *h_dentry;
27938+ struct inode *h_inode;
27939+
27940+ h_inode = d_inode(h_parent);
27941+ inode_lock_shared_nested(h_inode, AuLsc_I_CHILD2);
27942+ h_dentry = vfsub_lkup_one(tgtname, h_parent);
27943+ inode_unlock_shared(h_inode);
27944+ return h_dentry;
27945+}
27946+
27947+static void au_call_do_plink_lkup(void *args)
27948+{
27949+ struct au_do_plink_lkup_args *a = args;
27950+ *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
27951+}
27952+
27953+/* lookup the plink-ed @inode under the branch at @bindex */
27954+struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
27955+{
27956+ struct dentry *h_dentry, *h_parent;
27957+ struct au_branch *br;
27958+ int wkq_err;
27959+ char a[PLINK_NAME_LEN];
27960+ struct qstr tgtname = QSTR_INIT(a, 0);
27961+
27962+ AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
27963+
27964+ br = au_sbr(inode->i_sb, bindex);
27965+ h_parent = br->br_wbr->wbr_plink;
27966+ tgtname.len = plink_name(a, sizeof(a), inode, bindex);
27967+
27968+ if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
27969+ struct au_do_plink_lkup_args args = {
27970+ .errp = &h_dentry,
27971+ .tgtname = &tgtname,
27972+ .h_parent = h_parent,
27973+ .br = br
27974+ };
27975+
27976+ wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
27977+ if (unlikely(wkq_err))
27978+ h_dentry = ERR_PTR(wkq_err);
27979+ } else
27980+ h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
27981+
27982+ return h_dentry;
27983+}
27984+
27985+/* create a pseudo-link */
27986+static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
27987+ struct dentry *h_dentry, struct au_branch *br)
27988+{
27989+ int err;
27990+ struct path h_path = {
27991+ .mnt = au_br_mnt(br)
27992+ };
27993+ struct inode *h_dir, *delegated;
27994+
27995+ h_dir = d_inode(h_parent);
27996+ inode_lock_nested(h_dir, AuLsc_I_CHILD2);
27997+again:
27998+ h_path.dentry = vfsub_lkup_one(tgt, h_parent);
27999+ err = PTR_ERR(h_path.dentry);
28000+ if (IS_ERR(h_path.dentry))
28001+ goto out;
28002+
28003+ err = 0;
28004+ /* wh.plink dir is not monitored */
28005+ /* todo: is it really safe? */
28006+ if (d_is_positive(h_path.dentry)
28007+ && d_inode(h_path.dentry) != d_inode(h_dentry)) {
28008+ delegated = NULL;
28009+ err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
28010+ if (unlikely(err == -EWOULDBLOCK)) {
28011+ pr_warn("cannot retry for NFSv4 delegation"
28012+ " for an internal unlink\n");
28013+ iput(delegated);
28014+ }
28015+ dput(h_path.dentry);
28016+ h_path.dentry = NULL;
28017+ if (!err)
28018+ goto again;
28019+ }
28020+ if (!err && d_is_negative(h_path.dentry)) {
28021+ delegated = NULL;
28022+ err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
28023+ if (unlikely(err == -EWOULDBLOCK)) {
28024+ pr_warn("cannot retry for NFSv4 delegation"
28025+ " for an internal link\n");
28026+ iput(delegated);
28027+ }
28028+ }
28029+ dput(h_path.dentry);
28030+
28031+out:
28032+ inode_unlock(h_dir);
28033+ return err;
28034+}
28035+
28036+struct do_whplink_args {
28037+ int *errp;
28038+ struct qstr *tgt;
28039+ struct dentry *h_parent;
28040+ struct dentry *h_dentry;
28041+ struct au_branch *br;
28042+};
28043+
28044+static void call_do_whplink(void *args)
28045+{
28046+ struct do_whplink_args *a = args;
28047+ *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
28048+}
28049+
28050+static int whplink(struct dentry *h_dentry, struct inode *inode,
28051+ aufs_bindex_t bindex, struct au_branch *br)
28052+{
28053+ int err, wkq_err;
28054+ struct au_wbr *wbr;
28055+ struct dentry *h_parent;
28056+ char a[PLINK_NAME_LEN];
28057+ struct qstr tgtname = QSTR_INIT(a, 0);
28058+
28059+ wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
28060+ h_parent = wbr->wbr_plink;
28061+ tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28062+
28063+ /* always superio. */
28064+ if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
28065+ struct do_whplink_args args = {
28066+ .errp = &err,
28067+ .tgt = &tgtname,
28068+ .h_parent = h_parent,
28069+ .h_dentry = h_dentry,
28070+ .br = br
28071+ };
28072+ wkq_err = au_wkq_wait(call_do_whplink, &args);
28073+ if (unlikely(wkq_err))
28074+ err = wkq_err;
28075+ } else
28076+ err = do_whplink(&tgtname, h_parent, h_dentry, br);
28077+
28078+ return err;
28079+}
28080+
28081+/*
28082+ * create a new pseudo-link for @h_dentry on @bindex.
28083+ * the linked inode is held in aufs @inode.
28084+ */
28085+void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
28086+ struct dentry *h_dentry)
28087+{
28088+ struct super_block *sb;
28089+ struct au_sbinfo *sbinfo;
28090+ struct hlist_bl_head *hbl;
28091+ struct hlist_bl_node *pos;
28092+ struct au_icntnr *icntnr;
28093+ int found, err, cnt, i;
28094+
28095+ sb = inode->i_sb;
28096+ sbinfo = au_sbi(sb);
28097+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28098+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28099+
28100+ found = au_plink_test(inode);
28101+ if (found)
28102+ return;
28103+
28104+ i = au_plink_hash(inode->i_ino);
28105+ hbl = sbinfo->si_plink + i;
28106+ au_igrab(inode);
28107+
28108+ hlist_bl_lock(hbl);
28109+ hlist_bl_for_each_entry(icntnr, pos, hbl, plink) {
28110+ if (&icntnr->vfs_inode == inode) {
28111+ found = 1;
28112+ break;
28113+ }
28114+ }
28115+ if (!found) {
28116+ icntnr = container_of(inode, struct au_icntnr, vfs_inode);
28117+ hlist_bl_add_head(&icntnr->plink, hbl);
28118+ }
28119+ hlist_bl_unlock(hbl);
28120+ if (!found) {
28121+ cnt = au_hbl_count(hbl);
28122+#define msg "unexpectedly unbalanced or too many pseudo-links"
28123+ if (cnt > AUFS_PLINK_WARN)
28124+ AuWarn1(msg ", %d\n", cnt);
28125+#undef msg
28126+ err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
28127+ if (unlikely(err)) {
28128+ pr_warn("err %d, damaged pseudo link.\n", err);
28129+ au_hbl_del(&icntnr->plink, hbl);
28130+ iput(&icntnr->vfs_inode);
28131+ }
28132+ } else
28133+ iput(&icntnr->vfs_inode);
28134+}
28135+
28136+/* free all plinks */
28137+void au_plink_put(struct super_block *sb, int verbose)
28138+{
28139+ int i, warned;
28140+ struct au_sbinfo *sbinfo;
28141+ struct hlist_bl_head *hbl;
28142+ struct hlist_bl_node *pos, *tmp;
28143+ struct au_icntnr *icntnr;
28144+
28145+ SiMustWriteLock(sb);
28146+
28147+ sbinfo = au_sbi(sb);
28148+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28149+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28150+
28151+ /* no spin_lock since sbinfo is write-locked */
28152+ warned = 0;
28153+ for (i = 0; i < AuPlink_NHASH; i++) {
28154+ hbl = sbinfo->si_plink + i;
28155+ if (!warned && verbose && !hlist_bl_empty(hbl)) {
28156+ pr_warn("pseudo-link is not flushed");
28157+ warned = 1;
28158+ }
28159+ hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink)
28160+ iput(&icntnr->vfs_inode);
28161+ INIT_HLIST_BL_HEAD(hbl);
28162+ }
28163+}
28164+
28165+void au_plink_clean(struct super_block *sb, int verbose)
28166+{
28167+ struct dentry *root;
28168+
28169+ root = sb->s_root;
28170+ aufs_write_lock(root);
28171+ if (au_opt_test(au_mntflags(sb), PLINK))
28172+ au_plink_put(sb, verbose);
28173+ aufs_write_unlock(root);
28174+}
28175+
28176+static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
28177+{
28178+ int do_put;
28179+ aufs_bindex_t btop, bbot, bindex;
28180+
28181+ do_put = 0;
28182+ btop = au_ibtop(inode);
28183+ bbot = au_ibbot(inode);
28184+ if (btop >= 0) {
28185+ for (bindex = btop; bindex <= bbot; bindex++) {
28186+ if (!au_h_iptr(inode, bindex)
28187+ || au_ii_br_id(inode, bindex) != br_id)
28188+ continue;
28189+ au_set_h_iptr(inode, bindex, NULL, 0);
28190+ do_put = 1;
28191+ break;
28192+ }
28193+ if (do_put)
28194+ for (bindex = btop; bindex <= bbot; bindex++)
28195+ if (au_h_iptr(inode, bindex)) {
28196+ do_put = 0;
28197+ break;
28198+ }
28199+ } else
28200+ do_put = 1;
28201+
28202+ return do_put;
28203+}
28204+
28205+/* free the plinks on a branch specified by @br_id */
28206+void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
28207+{
28208+ struct au_sbinfo *sbinfo;
28209+ struct hlist_bl_head *hbl;
28210+ struct hlist_bl_node *pos, *tmp;
28211+ struct au_icntnr *icntnr;
28212+ struct inode *inode;
28213+ int i, do_put;
28214+
28215+ SiMustWriteLock(sb);
28216+
28217+ sbinfo = au_sbi(sb);
28218+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28219+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28220+
28221+ /* no bit_lock since sbinfo is write-locked */
28222+ for (i = 0; i < AuPlink_NHASH; i++) {
28223+ hbl = sbinfo->si_plink + i;
28224+ hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink) {
28225+ inode = au_igrab(&icntnr->vfs_inode);
28226+ ii_write_lock_child(inode);
28227+ do_put = au_plink_do_half_refresh(inode, br_id);
28228+ if (do_put) {
28229+ hlist_bl_del(&icntnr->plink);
28230+ iput(inode);
28231+ }
28232+ ii_write_unlock(inode);
28233+ iput(inode);
28234+ }
28235+ }
28236+}
28237diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
28238--- /usr/share/empty/fs/aufs/poll.c 1970-01-01 01:00:00.000000000 +0100
28239+++ linux/fs/aufs/poll.c 2021-02-24 13:33:42.747680497 +0100
28240@@ -0,0 +1,51 @@
28241+// SPDX-License-Identifier: GPL-2.0
28242+/*
28243+ * Copyright (C) 2005-2020 Junjiro R. Okajima
28244+ *
28245+ * This program, aufs is free software; you can redistribute it and/or modify
28246+ * it under the terms of the GNU General Public License as published by
28247+ * the Free Software Foundation; either version 2 of the License, or
28248+ * (at your option) any later version.
28249+ *
28250+ * This program is distributed in the hope that it will be useful,
28251+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28252+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28253+ * GNU General Public License for more details.
28254+ *
28255+ * You should have received a copy of the GNU General Public License
28256+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
28257+ */
28258+
28259+/*
28260+ * poll operation
28261+ * There is only one filesystem which implements ->poll operation, currently.
28262+ */
28263+
28264+#include "aufs.h"
28265+
28266+__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt)
28267+{
28268+ __poll_t mask;
28269+ struct file *h_file;
28270+ struct super_block *sb;
28271+
28272+ /* We should pretend an error happened. */
28273+ mask = EPOLLERR /* | EPOLLIN | EPOLLOUT */;
28274+ sb = file->f_path.dentry->d_sb;
28275+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
28276+
28277+ h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
28278+ if (IS_ERR(h_file)) {
28279+ AuDbg("h_file %ld\n", PTR_ERR(h_file));
28280+ goto out;
28281+ }
28282+
28283+ mask = vfs_poll(h_file, pt);
28284+ fput(h_file); /* instead of au_read_post() */
28285+
28286+out:
28287+ si_read_unlock(sb);
28288+ if (mask & EPOLLERR)
28289+ AuDbg("mask 0x%x\n", mask);
28290+ return mask;
28291+}
28292diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
28293--- /usr/share/empty/fs/aufs/posix_acl.c 1970-01-01 01:00:00.000000000 +0100
28294+++ linux/fs/aufs/posix_acl.c 2021-06-30 21:35:11.397206648 +0200
28295@@ -0,0 +1,106 @@
28296+// SPDX-License-Identifier: GPL-2.0
28297+/*
28298+ * Copyright (C) 2014-2020 Junjiro R. Okajima
28299+ *
28300+ * This program, aufs is free software; you can redistribute it and/or modify
28301+ * it under the terms of the GNU General Public License as published by
28302+ * the Free Software Foundation; either version 2 of the License, or
28303+ * (at your option) any later version.
28304+ *
28305+ * This program is distributed in the hope that it will be useful,
28306+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28307+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28308+ * GNU General Public License for more details.
28309+ *
28310+ * You should have received a copy of the GNU General Public License
28311+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
28312+ */
28313+
28314+/*
28315+ * posix acl operations
28316+ */
28317+
28318+#include <linux/fs.h>
28319+#include "aufs.h"
28320+
28321+struct posix_acl *aufs_get_acl(struct inode *inode, int type)
28322+{
28323+ struct posix_acl *acl;
28324+ int err;
28325+ aufs_bindex_t bindex;
28326+ struct inode *h_inode;
28327+ struct super_block *sb;
28328+
28329+ acl = NULL;
28330+ sb = inode->i_sb;
28331+ si_read_lock(sb, AuLock_FLUSH);
28332+ ii_read_lock_child(inode);
28333+ if (!(sb->s_flags & SB_POSIXACL))
28334+ goto out;
28335+
28336+ bindex = au_ibtop(inode);
28337+ h_inode = au_h_iptr(inode, bindex);
28338+ if (unlikely(!h_inode
28339+ || ((h_inode->i_mode & S_IFMT)
28340+ != (inode->i_mode & S_IFMT)))) {
28341+ err = au_busy_or_stale();
28342+ acl = ERR_PTR(err);
28343+ goto out;
28344+ }
28345+
28346+ /* always topmost only */
28347+ acl = get_acl(h_inode, type);
28348+ if (IS_ERR(acl))
28349+ forget_cached_acl(inode, type);
28350+ else
28351+ set_cached_acl(inode, type, acl);
28352+
28353+out:
28354+ ii_read_unlock(inode);
28355+ si_read_unlock(sb);
28356+
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-02-24 13:33:42.747680497 +0100
28405@@ -0,0 +1,170 @@
28406+// SPDX-License-Identifier: GPL-2.0
28407+/*
28408+ * Copyright (C) 2010-2020 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-02-24 13:33:42.747680497 +0100
28579@@ -0,0 +1,384 @@
28580+// SPDX-License-Identifier: GPL-2.0
28581+/*
28582+ * Copyright (C) 2005-2020 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-02-24 13:33:42.747680497 +0100
28967@@ -0,0 +1,85 @@
28968+/* SPDX-License-Identifier: GPL-2.0 */
28969+/*
28970+ * Copyright (C) 2005-2020 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-02-24 13:33:42.747680497 +0100
29056@@ -0,0 +1,314 @@
29057+// SPDX-License-Identifier: GPL-2.0
29058+/*
29059+ * Copyright (C) 2005-2020 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-06-30 21:35:11.397206648 +0200
29374@@ -0,0 +1,1050 @@
29375+// SPDX-License-Identifier: GPL-2.0
29376+/*
29377+ * Copyright (C) 2005-2020 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-02-24 13:33:42.747680497 +0100
30428@@ -0,0 +1,587 @@
30429+/* SPDX-License-Identifier: GPL-2.0 */
30430+/*
30431+ * Copyright (C) 2005-2020 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-02-24 13:33:42.747680497 +0100
31019@@ -0,0 +1,93 @@
31020+// SPDX-License-Identifier: GPL-2.0
31021+/*
31022+ * Copyright (C) 2005-2020 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-02-24 13:33:42.747680497 +0100
31116@@ -0,0 +1,102 @@
31117+/* SPDX-License-Identifier: GPL-2.0 */
31118+/*
31119+ * Copyright (C) 2005-2020 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-02-24 13:33:42.747680497 +0100
31222@@ -0,0 +1,374 @@
31223+// SPDX-License-Identifier: GPL-2.0
31224+/*
31225+ * Copyright (C) 2005-2020 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-02-24 13:33:42.747680497 +0100
31600@@ -0,0 +1,149 @@
31601+// SPDX-License-Identifier: GPL-2.0
31602+/*
31603+ * Copyright (C) 2005-2020 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-02-24 13:33:42.747680497 +0100
31753@@ -0,0 +1,896 @@
31754+// SPDX-License-Identifier: GPL-2.0
31755+/*
31756+ * Copyright (C) 2005-2020 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-06-30 21:35:11.397206648 +0200
32653@@ -0,0 +1,916 @@
32654+// SPDX-License-Identifier: GPL-2.0
32655+/*
32656+ * Copyright (C) 2005-2020 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+ /* for remote fs, leave work for its getattr or d_revalidate */
32715+ /* for bad i_attr fs, handle them in aufs_getattr() */
32716+ /* still some fs may acquire i_mutex. we need to skip them */
32717+ err = 0;
32718+ if (!did)
32719+ did = &err;
32720+ h_sb = h_path->dentry->d_sb;
32721+ *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
32722+ if (*did)
32723+ err = vfsub_getattr(h_path, &st);
32724+
32725+ return err;
32726+}
32727+
32728+/* ---------------------------------------------------------------------- */
32729+
32730+struct file *vfsub_dentry_open(struct path *path, int flags)
32731+{
32732+ return dentry_open(path, flags /* | __FMODE_NONOTIFY */,
32733+ current_cred());
32734+}
32735+
32736+struct file *vfsub_filp_open(const char *path, int oflags, int mode)
32737+{
32738+ struct file *file;
32739+
32740+ lockdep_off();
32741+ file = filp_open(path,
32742+ oflags /* | __FMODE_NONOTIFY */,
32743+ mode);
32744+ lockdep_on();
32745+ if (IS_ERR(file))
32746+ goto out;
32747+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
32748+
32749+out:
32750+ return file;
32751+}
32752+
32753+/*
32754+ * Ideally this function should call VFS:do_last() in order to keep all its
32755+ * checkings. But it is very hard for aufs to regenerate several VFS internal
32756+ * structure such as nameidata. This is a second (or third) best approach.
32757+ * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
32758+ */
32759+int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
32760+ struct vfsub_aopen_args *args)
32761+{
32762+ int err;
32763+ struct au_branch *br = args->br;
32764+ struct file *file = args->file;
32765+ /* copied from linux/fs/namei.c:atomic_open() */
32766+ struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
32767+
32768+ IMustLock(dir);
32769+ AuDebugOn(!dir->i_op->atomic_open);
32770+
32771+ err = au_br_test_oflag(args->open_flag, br);
32772+ if (unlikely(err))
32773+ goto out;
32774+
32775+ au_lcnt_inc(&br->br_nfiles);
32776+ file->f_path.dentry = DENTRY_NOT_SET;
32777+ file->f_path.mnt = au_br_mnt(br);
32778+ AuDbg("%ps\n", dir->i_op->atomic_open);
32779+ err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
32780+ args->create_mode);
32781+ if (unlikely(err < 0)) {
32782+ au_lcnt_dec(&br->br_nfiles);
32783+ goto out;
32784+ }
32785+
32786+ /* temporary workaround for nfsv4 branch */
32787+ if (au_test_nfs(dir->i_sb))
32788+ nfs_mark_for_revalidate(dir);
32789+
32790+ if (file->f_mode & FMODE_CREATED)
32791+ fsnotify_create(dir, dentry);
32792+ if (!(file->f_mode & FMODE_OPENED)) {
32793+ au_lcnt_dec(&br->br_nfiles);
32794+ goto out;
32795+ }
32796+
32797+ /* todo: call VFS:may_open() here */
32798+ /* todo: ima_file_check() too? */
32799+ if (!err && (args->open_flag & __FMODE_EXEC))
32800+ err = deny_write_access(file);
32801+ if (!err)
32802+ fsnotify_open(file);
32803+ else
32804+ au_lcnt_dec(&br->br_nfiles);
32805+ /* note that the file is created and still opened */
32806+
32807+out:
32808+ return err;
32809+}
32810+
32811+int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
32812+{
32813+ int err;
32814+
32815+ err = kern_path(name, flags, path);
32816+ if (!err && d_is_positive(path->dentry))
32817+ vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
32818+ return err;
32819+}
32820+
32821+struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
32822+ struct dentry *parent, int len)
32823+{
32824+ struct path path = {
32825+ .mnt = NULL
32826+ };
32827+
32828+ path.dentry = lookup_one_len_unlocked(name, parent, len);
32829+ if (IS_ERR(path.dentry))
32830+ goto out;
32831+ if (d_is_positive(path.dentry))
32832+ vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32833+
32834+out:
32835+ AuTraceErrPtr(path.dentry);
32836+ return path.dentry;
32837+}
32838+
32839+struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
32840+ int len)
32841+{
32842+ struct path path = {
32843+ .mnt = NULL
32844+ };
32845+
32846+ /* VFS checks it too, but by WARN_ON_ONCE() */
32847+ IMustLock(d_inode(parent));
32848+
32849+ path.dentry = lookup_one_len(name, parent, len);
32850+ if (IS_ERR(path.dentry))
32851+ goto out;
32852+ if (d_is_positive(path.dentry))
32853+ vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32854+
32855+out:
32856+ AuTraceErrPtr(path.dentry);
32857+ return path.dentry;
32858+}
32859+
32860+void vfsub_call_lkup_one(void *args)
32861+{
32862+ struct vfsub_lkup_one_args *a = args;
32863+ *a->errp = vfsub_lkup_one(a->name, a->parent);
32864+}
32865+
32866+/* ---------------------------------------------------------------------- */
32867+
32868+struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
32869+ struct dentry *d2, struct au_hinode *hdir2)
32870+{
32871+ struct dentry *d;
32872+
32873+ lockdep_off();
32874+ d = lock_rename(d1, d2);
32875+ lockdep_on();
32876+ au_hn_suspend(hdir1);
32877+ if (hdir1 != hdir2)
32878+ au_hn_suspend(hdir2);
32879+
32880+ return d;
32881+}
32882+
32883+void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
32884+ struct dentry *d2, struct au_hinode *hdir2)
32885+{
32886+ au_hn_resume(hdir1);
32887+ if (hdir1 != hdir2)
32888+ au_hn_resume(hdir2);
32889+ lockdep_off();
32890+ unlock_rename(d1, d2);
32891+ lockdep_on();
32892+}
32893+
32894+/* ---------------------------------------------------------------------- */
32895+
32896+int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
32897+{
32898+ int err;
32899+ struct dentry *d;
32900+ struct user_namespace *userns;
32901+
32902+ IMustLock(dir);
32903+
32904+ d = path->dentry;
32905+ path->dentry = d->d_parent;
32906+ err = security_path_mknod(path, d, mode, 0);
32907+ path->dentry = d;
32908+ if (unlikely(err))
32909+ goto out;
32910+ userns = mnt_user_ns(path->mnt);
32911+
32912+ lockdep_off();
32913+ err = vfs_create(userns, dir, path->dentry, mode, want_excl);
32914+ lockdep_on();
32915+ if (!err) {
32916+ struct path tmp = *path;
32917+ int did;
32918+
32919+ vfsub_update_h_iattr(&tmp, &did);
32920+ if (did) {
32921+ tmp.dentry = path->dentry->d_parent;
32922+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32923+ }
32924+ /*ignore*/
32925+ }
32926+
32927+out:
32928+ return err;
32929+}
32930+
32931+int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
32932+{
32933+ int err;
32934+ struct dentry *d;
32935+ struct user_namespace *userns;
32936+
32937+ IMustLock(dir);
32938+
32939+ d = path->dentry;
32940+ path->dentry = d->d_parent;
32941+ err = security_path_symlink(path, d, symname);
32942+ path->dentry = d;
32943+ if (unlikely(err))
32944+ goto out;
32945+ userns = mnt_user_ns(path->mnt);
32946+
32947+ lockdep_off();
32948+ err = vfs_symlink(userns, dir, path->dentry, symname);
32949+ lockdep_on();
32950+ if (!err) {
32951+ struct path tmp = *path;
32952+ int did;
32953+
32954+ vfsub_update_h_iattr(&tmp, &did);
32955+ if (did) {
32956+ tmp.dentry = path->dentry->d_parent;
32957+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32958+ }
32959+ /*ignore*/
32960+ }
32961+
32962+out:
32963+ return err;
32964+}
32965+
32966+int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
32967+{
32968+ int err;
32969+ struct dentry *d;
32970+ struct user_namespace *userns;
32971+
32972+ IMustLock(dir);
32973+
32974+ d = path->dentry;
32975+ path->dentry = d->d_parent;
32976+ err = security_path_mknod(path, d, mode, new_encode_dev(dev));
32977+ path->dentry = d;
32978+ if (unlikely(err))
32979+ goto out;
32980+ userns = mnt_user_ns(path->mnt);
32981+
32982+ lockdep_off();
32983+ err = vfs_mknod(userns, dir, path->dentry, mode, dev);
32984+ lockdep_on();
32985+ if (!err) {
32986+ struct path tmp = *path;
32987+ int did;
32988+
32989+ vfsub_update_h_iattr(&tmp, &did);
32990+ if (did) {
32991+ tmp.dentry = path->dentry->d_parent;
32992+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
32993+ }
32994+ /*ignore*/
32995+ }
32996+
32997+out:
32998+ return err;
32999+}
33000+
33001+static int au_test_nlink(struct inode *inode)
33002+{
33003+ const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
33004+
33005+ if (!au_test_fs_no_limit_nlink(inode->i_sb)
33006+ || inode->i_nlink < link_max)
33007+ return 0;
33008+ return -EMLINK;
33009+}
33010+
33011+int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
33012+ struct inode **delegated_inode)
33013+{
33014+ int err;
33015+ struct dentry *d;
33016+ struct user_namespace *userns;
33017+
33018+ IMustLock(dir);
33019+
33020+ err = au_test_nlink(d_inode(src_dentry));
33021+ if (unlikely(err))
33022+ return err;
33023+
33024+ /* we don't call may_linkat() */
33025+ d = path->dentry;
33026+ path->dentry = d->d_parent;
33027+ err = security_path_link(src_dentry, path, d);
33028+ path->dentry = d;
33029+ if (unlikely(err))
33030+ goto out;
33031+ userns = mnt_user_ns(path->mnt);
33032+
33033+ lockdep_off();
33034+ err = vfs_link(src_dentry, userns, dir, path->dentry, delegated_inode);
33035+ lockdep_on();
33036+ if (!err) {
33037+ struct path tmp = *path;
33038+ int did;
33039+
33040+ /* fuse has different memory inode for the same inumber */
33041+ vfsub_update_h_iattr(&tmp, &did);
33042+ if (did) {
33043+ tmp.dentry = path->dentry->d_parent;
33044+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33045+ tmp.dentry = src_dentry;
33046+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33047+ }
33048+ /*ignore*/
33049+ }
33050+
33051+out:
33052+ return err;
33053+}
33054+
33055+int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
33056+ struct inode *dir, struct path *path,
33057+ struct inode **delegated_inode, unsigned int flags)
33058+{
33059+ int err;
33060+ struct renamedata rd;
33061+ struct path tmp = {
33062+ .mnt = path->mnt
33063+ };
33064+ struct dentry *d;
33065+
33066+ IMustLock(dir);
33067+ IMustLock(src_dir);
33068+
33069+ d = path->dentry;
33070+ path->dentry = d->d_parent;
33071+ tmp.dentry = src_dentry->d_parent;
33072+ err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
33073+ path->dentry = d;
33074+ if (unlikely(err))
33075+ goto out;
33076+
33077+ rd.old_mnt_userns = mnt_user_ns(path->mnt);
33078+ rd.old_dir = src_dir;
33079+ rd.old_dentry = src_dentry;
33080+ rd.new_mnt_userns = rd.old_mnt_userns;
33081+ rd.new_dir = dir;
33082+ rd.new_dentry = path->dentry;
33083+ rd.delegated_inode = delegated_inode;
33084+ rd.flags = flags;
33085+ lockdep_off();
33086+ err = vfs_rename(&rd);
33087+ lockdep_on();
33088+ if (!err) {
33089+ int did;
33090+
33091+ tmp.dentry = d->d_parent;
33092+ vfsub_update_h_iattr(&tmp, &did);
33093+ if (did) {
33094+ tmp.dentry = src_dentry;
33095+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33096+ tmp.dentry = src_dentry->d_parent;
33097+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33098+ }
33099+ /*ignore*/
33100+ }
33101+
33102+out:
33103+ return err;
33104+}
33105+
33106+int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
33107+{
33108+ int err;
33109+ struct dentry *d;
33110+ struct user_namespace *userns;
33111+
33112+ IMustLock(dir);
33113+
33114+ d = path->dentry;
33115+ path->dentry = d->d_parent;
33116+ err = security_path_mkdir(path, d, mode);
33117+ path->dentry = d;
33118+ if (unlikely(err))
33119+ goto out;
33120+ userns = mnt_user_ns(path->mnt);
33121+
33122+ lockdep_off();
33123+ err = vfs_mkdir(userns, dir, path->dentry, mode);
33124+ lockdep_on();
33125+ if (!err) {
33126+ struct path tmp = *path;
33127+ int did;
33128+
33129+ vfsub_update_h_iattr(&tmp, &did);
33130+ if (did) {
33131+ tmp.dentry = path->dentry->d_parent;
33132+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
33133+ }
33134+ /*ignore*/
33135+ }
33136+
33137+out:
33138+ return err;
33139+}
33140+
33141+int vfsub_rmdir(struct inode *dir, struct path *path)
33142+{
33143+ int err;
33144+ struct dentry *d;
33145+ struct user_namespace *userns;
33146+
33147+ IMustLock(dir);
33148+
33149+ d = path->dentry;
33150+ path->dentry = d->d_parent;
33151+ err = security_path_rmdir(path, d);
33152+ path->dentry = d;
33153+ if (unlikely(err))
33154+ goto out;
33155+ userns = mnt_user_ns(path->mnt);
33156+
33157+ lockdep_off();
33158+ err = vfs_rmdir(userns, dir, path->dentry);
33159+ lockdep_on();
33160+ if (!err) {
33161+ struct path tmp = {
33162+ .dentry = path->dentry->d_parent,
33163+ .mnt = path->mnt
33164+ };
33165+
33166+ vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33167+ }
33168+
33169+out:
33170+ return err;
33171+}
33172+
33173+/* ---------------------------------------------------------------------- */
33174+
33175+/* todo: support mmap_sem? */
33176+ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33177+ loff_t *ppos)
33178+{
33179+ ssize_t err;
33180+
33181+ lockdep_off();
33182+ err = vfs_read(file, ubuf, count, ppos);
33183+ lockdep_on();
33184+ if (err >= 0)
33185+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33186+ return err;
33187+}
33188+
33189+ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33190+ loff_t *ppos)
33191+{
33192+ ssize_t err;
33193+
33194+ lockdep_off();
33195+ err = kernel_read(file, kbuf, count, ppos);
33196+ lockdep_on();
33197+ AuTraceErr(err);
33198+ if (err >= 0)
33199+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33200+ return err;
33201+}
33202+
33203+ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33204+ loff_t *ppos)
33205+{
33206+ ssize_t err;
33207+
33208+ lockdep_off();
33209+ err = vfs_write(file, ubuf, count, ppos);
33210+ lockdep_on();
33211+ if (err >= 0)
33212+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33213+ return err;
33214+}
33215+
33216+ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
33217+{
33218+ ssize_t err;
33219+
33220+ lockdep_off();
33221+ err = kernel_write(file, kbuf, count, ppos);
33222+ lockdep_on();
33223+ if (err >= 0)
33224+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33225+ return err;
33226+}
33227+
33228+int vfsub_flush(struct file *file, fl_owner_t id)
33229+{
33230+ int err;
33231+
33232+ err = 0;
33233+ if (file->f_op->flush) {
33234+ if (!au_test_nfs(file->f_path.dentry->d_sb))
33235+ err = file->f_op->flush(file, id);
33236+ else {
33237+ lockdep_off();
33238+ err = file->f_op->flush(file, id);
33239+ lockdep_on();
33240+ }
33241+ if (!err)
33242+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
33243+ /*ignore*/
33244+ }
33245+ return err;
33246+}
33247+
33248+int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
33249+{
33250+ int err;
33251+
33252+ AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
33253+
33254+ lockdep_off();
33255+ err = iterate_dir(file, ctx);
33256+ lockdep_on();
33257+ if (err >= 0)
33258+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33259+
33260+ return err;
33261+}
33262+
33263+long vfsub_splice_to(struct file *in, loff_t *ppos,
33264+ struct pipe_inode_info *pipe, size_t len,
33265+ unsigned int flags)
33266+{
33267+ long err;
33268+
33269+ lockdep_off();
33270+ err = do_splice_to(in, ppos, pipe, len, flags);
33271+ lockdep_on();
33272+ file_accessed(in);
33273+ if (err >= 0)
33274+ vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
33275+ return err;
33276+}
33277+
33278+long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33279+ loff_t *ppos, size_t len, unsigned int flags)
33280+{
33281+ long err;
33282+
33283+ lockdep_off();
33284+ err = do_splice_from(pipe, out, ppos, len, flags);
33285+ lockdep_on();
33286+ if (err >= 0)
33287+ vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
33288+ return err;
33289+}
33290+
33291+int vfsub_fsync(struct file *file, struct path *path, int datasync)
33292+{
33293+ int err;
33294+
33295+ /* file can be NULL */
33296+ lockdep_off();
33297+ err = vfs_fsync(file, datasync);
33298+ lockdep_on();
33299+ if (!err) {
33300+ if (!path) {
33301+ AuDebugOn(!file);
33302+ path = &file->f_path;
33303+ }
33304+ vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33305+ }
33306+ return err;
33307+}
33308+
33309+/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
33310+int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33311+ struct file *h_file)
33312+{
33313+ int err;
33314+ struct inode *h_inode;
33315+ struct super_block *h_sb;
33316+ struct user_namespace *h_userns;
33317+
33318+ if (!h_file) {
33319+ err = vfsub_truncate(h_path, length);
33320+ goto out;
33321+ }
33322+
33323+ h_inode = d_inode(h_path->dentry);
33324+ h_sb = h_inode->i_sb;
33325+ lockdep_off();
33326+ sb_start_write(h_sb);
33327+ lockdep_on();
33328+ err = locks_verify_truncate(h_inode, h_file, length);
33329+ if (!err)
33330+ err = security_path_truncate(h_path);
33331+ if (!err) {
33332+ h_userns = mnt_user_ns(h_path->mnt);
33333+ lockdep_off();
33334+ err = do_truncate(h_userns, h_path->dentry, length, attr,
33335+ h_file);
33336+ lockdep_on();
33337+ }
33338+ lockdep_off();
33339+ sb_end_write(h_sb);
33340+ lockdep_on();
33341+
33342+out:
33343+ return err;
33344+}
33345+
33346+/* ---------------------------------------------------------------------- */
33347+
33348+struct au_vfsub_mkdir_args {
33349+ int *errp;
33350+ struct inode *dir;
33351+ struct path *path;
33352+ int mode;
33353+};
33354+
33355+static void au_call_vfsub_mkdir(void *args)
33356+{
33357+ struct au_vfsub_mkdir_args *a = args;
33358+ *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
33359+}
33360+
33361+int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
33362+{
33363+ int err, do_sio, wkq_err;
33364+ struct user_namespace *userns;
33365+
33366+ userns = mnt_user_ns(path->mnt);
33367+ do_sio = au_test_h_perm_sio(userns, dir, MAY_EXEC | MAY_WRITE);
33368+ if (!do_sio) {
33369+ lockdep_off();
33370+ err = vfsub_mkdir(dir, path, mode);
33371+ lockdep_on();
33372+ } else {
33373+ struct au_vfsub_mkdir_args args = {
33374+ .errp = &err,
33375+ .dir = dir,
33376+ .path = path,
33377+ .mode = mode
33378+ };
33379+ wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
33380+ if (unlikely(wkq_err))
33381+ err = wkq_err;
33382+ }
33383+
33384+ return err;
33385+}
33386+
33387+struct au_vfsub_rmdir_args {
33388+ int *errp;
33389+ struct inode *dir;
33390+ struct path *path;
33391+};
33392+
33393+static void au_call_vfsub_rmdir(void *args)
33394+{
33395+ struct au_vfsub_rmdir_args *a = args;
33396+ *a->errp = vfsub_rmdir(a->dir, a->path);
33397+}
33398+
33399+int vfsub_sio_rmdir(struct inode *dir, struct path *path)
33400+{
33401+ int err, do_sio, wkq_err;
33402+ struct user_namespace *userns;
33403+
33404+ userns = mnt_user_ns(path->mnt);
33405+ do_sio = au_test_h_perm_sio(userns, dir, MAY_EXEC | MAY_WRITE);
33406+ if (!do_sio) {
33407+ lockdep_off();
33408+ err = vfsub_rmdir(dir, path);
33409+ lockdep_on();
33410+ } else {
33411+ struct au_vfsub_rmdir_args args = {
33412+ .errp = &err,
33413+ .dir = dir,
33414+ .path = path
33415+ };
33416+ wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
33417+ if (unlikely(wkq_err))
33418+ err = wkq_err;
33419+ }
33420+
33421+ return err;
33422+}
33423+
33424+/* ---------------------------------------------------------------------- */
33425+
33426+struct notify_change_args {
33427+ int *errp;
33428+ struct path *path;
33429+ struct iattr *ia;
33430+ struct inode **delegated_inode;
33431+};
33432+
33433+static void call_notify_change(void *args)
33434+{
33435+ struct notify_change_args *a = args;
33436+ struct inode *h_inode;
33437+ struct user_namespace *userns;
33438+
33439+ h_inode = d_inode(a->path->dentry);
33440+ IMustLock(h_inode);
33441+
33442+ *a->errp = -EPERM;
33443+ if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
33444+ userns = mnt_user_ns(a->path->mnt);
33445+ lockdep_off();
33446+ *a->errp = notify_change(userns, a->path->dentry, a->ia,
33447+ a->delegated_inode);
33448+ lockdep_on();
33449+ if (!*a->errp)
33450+ vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
33451+ }
33452+ AuTraceErr(*a->errp);
33453+}
33454+
33455+int vfsub_notify_change(struct path *path, struct iattr *ia,
33456+ struct inode **delegated_inode)
33457+{
33458+ int err;
33459+ struct notify_change_args args = {
33460+ .errp = &err,
33461+ .path = path,
33462+ .ia = ia,
33463+ .delegated_inode = delegated_inode
33464+ };
33465+
33466+ call_notify_change(&args);
33467+
33468+ return err;
33469+}
33470+
33471+int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33472+ struct inode **delegated_inode)
33473+{
33474+ int err, wkq_err;
33475+ struct notify_change_args args = {
33476+ .errp = &err,
33477+ .path = path,
33478+ .ia = ia,
33479+ .delegated_inode = delegated_inode
33480+ };
33481+
33482+ wkq_err = au_wkq_wait(call_notify_change, &args);
33483+ if (unlikely(wkq_err))
33484+ err = wkq_err;
33485+
33486+ return err;
33487+}
33488+
33489+/* ---------------------------------------------------------------------- */
33490+
33491+struct unlink_args {
33492+ int *errp;
33493+ struct inode *dir;
33494+ struct path *path;
33495+ struct inode **delegated_inode;
33496+};
33497+
33498+static void call_unlink(void *args)
33499+{
33500+ struct unlink_args *a = args;
33501+ struct dentry *d = a->path->dentry;
33502+ struct inode *h_inode;
33503+ struct user_namespace *userns;
33504+ const int stop_sillyrename = (au_test_nfs(d->d_sb)
33505+ && au_dcount(d) == 1);
33506+
33507+ IMustLock(a->dir);
33508+
33509+ a->path->dentry = d->d_parent;
33510+ *a->errp = security_path_unlink(a->path, d);
33511+ a->path->dentry = d;
33512+ if (unlikely(*a->errp))
33513+ return;
33514+
33515+ if (!stop_sillyrename)
33516+ dget(d);
33517+ h_inode = NULL;
33518+ if (d_is_positive(d)) {
33519+ h_inode = d_inode(d);
33520+ ihold(h_inode);
33521+ }
33522+
33523+ userns = mnt_user_ns(a->path->mnt);
33524+ lockdep_off();
33525+ *a->errp = vfs_unlink(userns, a->dir, d, a->delegated_inode);
33526+ lockdep_on();
33527+ if (!*a->errp) {
33528+ struct path tmp = {
33529+ .dentry = d->d_parent,
33530+ .mnt = a->path->mnt
33531+ };
33532+ vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33533+ }
33534+
33535+ if (!stop_sillyrename)
33536+ dput(d);
33537+ if (h_inode)
33538+ iput(h_inode);
33539+
33540+ AuTraceErr(*a->errp);
33541+}
33542+
33543+/*
33544+ * @dir: must be locked.
33545+ * @dentry: target dentry.
33546+ */
33547+int vfsub_unlink(struct inode *dir, struct path *path,
33548+ struct inode **delegated_inode, int force)
33549+{
33550+ int err;
33551+ struct unlink_args args = {
33552+ .errp = &err,
33553+ .dir = dir,
33554+ .path = path,
33555+ .delegated_inode = delegated_inode
33556+ };
33557+
33558+ if (!force)
33559+ call_unlink(&args);
33560+ else {
33561+ int wkq_err;
33562+
33563+ wkq_err = au_wkq_wait(call_unlink, &args);
33564+ if (unlikely(wkq_err))
33565+ err = wkq_err;
33566+ }
33567+
33568+ return err;
33569+}
33570diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
33571--- /usr/share/empty/fs/aufs/vfsub.h 1970-01-01 01:00:00.000000000 +0100
33572+++ linux/fs/aufs/vfsub.h 2021-06-30 21:35:11.397206648 +0200
33573@@ -0,0 +1,358 @@
33574+/* SPDX-License-Identifier: GPL-2.0 */
33575+/*
33576+ * Copyright (C) 2005-2020 Junjiro R. Okajima
33577+ *
33578+ * This program, aufs is free software; you can redistribute it and/or modify
33579+ * it under the terms of the GNU General Public License as published by
33580+ * the Free Software Foundation; either version 2 of the License, or
33581+ * (at your option) any later version.
33582+ *
33583+ * This program is distributed in the hope that it will be useful,
33584+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
33585+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33586+ * GNU General Public License for more details.
33587+ *
33588+ * You should have received a copy of the GNU General Public License
33589+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
33590+ */
33591+
33592+/*
33593+ * sub-routines for VFS
33594+ */
33595+
33596+#ifndef __AUFS_VFSUB_H__
33597+#define __AUFS_VFSUB_H__
33598+
33599+#ifdef __KERNEL__
33600+
33601+#include <linux/fs.h>
33602+#include <linux/mount.h>
33603+#include <linux/posix_acl.h>
33604+#include <linux/xattr.h>
33605+#include "debug.h"
33606+
33607+/* copied from linux/fs/internal.h */
33608+/* todo: BAD approach!! */
33609+extern void __mnt_drop_write(struct vfsmount *);
33610+extern struct file *alloc_empty_file(int, const struct cred *);
33611+
33612+/* ---------------------------------------------------------------------- */
33613+
33614+/* lock subclass for lower inode */
33615+/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
33616+/* reduce? gave up. */
33617+enum {
33618+ AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
33619+ AuLsc_I_PARENT, /* lower inode, parent first */
33620+ AuLsc_I_PARENT2, /* copyup dirs */
33621+ AuLsc_I_PARENT3, /* copyup wh */
33622+ AuLsc_I_CHILD,
33623+ AuLsc_I_CHILD2,
33624+ AuLsc_I_End
33625+};
33626+
33627+/* to debug easier, do not make them inlined functions */
33628+#define MtxMustLock(mtx) AuDebugOn(!mutex_is_locked(mtx))
33629+#define IMustLock(i) AuDebugOn(!inode_is_locked(i))
33630+
33631+/* ---------------------------------------------------------------------- */
33632+
33633+static inline void vfsub_drop_nlink(struct inode *inode)
33634+{
33635+ AuDebugOn(!inode->i_nlink);
33636+ drop_nlink(inode);
33637+}
33638+
33639+static inline void vfsub_dead_dir(struct inode *inode)
33640+{
33641+ AuDebugOn(!S_ISDIR(inode->i_mode));
33642+ inode->i_flags |= S_DEAD;
33643+ clear_nlink(inode);
33644+}
33645+
33646+static inline int vfsub_native_ro(struct inode *inode)
33647+{
33648+ return sb_rdonly(inode->i_sb)
33649+ || IS_RDONLY(inode)
33650+ /* || IS_APPEND(inode) */
33651+ || IS_IMMUTABLE(inode);
33652+}
33653+
33654+#ifdef CONFIG_AUFS_BR_FUSE
33655+int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
33656+#else
33657+AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
33658+#endif
33659+
33660+int vfsub_sync_filesystem(struct super_block *h_sb, int wait);
33661+
33662+/* ---------------------------------------------------------------------- */
33663+
33664+int vfsub_update_h_iattr(struct path *h_path, int *did);
33665+struct file *vfsub_dentry_open(struct path *path, int flags);
33666+struct file *vfsub_filp_open(const char *path, int oflags, int mode);
33667+struct au_branch;
33668+struct vfsub_aopen_args {
33669+ struct file *file;
33670+ unsigned int open_flag;
33671+ umode_t create_mode;
33672+ struct au_branch *br;
33673+};
33674+int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
33675+ struct vfsub_aopen_args *args);
33676+int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
33677+
33678+struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33679+ struct dentry *parent, int len);
33680+struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
33681+ int len);
33682+
33683+struct vfsub_lkup_one_args {
33684+ struct dentry **errp;
33685+ struct qstr *name;
33686+ struct dentry *parent;
33687+};
33688+
33689+static inline struct dentry *vfsub_lkup_one(struct qstr *name,
33690+ struct dentry *parent)
33691+{
33692+ return vfsub_lookup_one_len(name->name, parent, name->len);
33693+}
33694+
33695+void vfsub_call_lkup_one(void *args);
33696+
33697+/* ---------------------------------------------------------------------- */
33698+
33699+static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
33700+{
33701+ int err;
33702+
33703+ lockdep_off();
33704+ err = mnt_want_write(mnt);
33705+ lockdep_on();
33706+ return err;
33707+}
33708+
33709+static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
33710+{
33711+ lockdep_off();
33712+ mnt_drop_write(mnt);
33713+ lockdep_on();
33714+}
33715+
33716+#if 0 /* reserved */
33717+static inline void vfsub_mnt_drop_write_file(struct file *file)
33718+{
33719+ lockdep_off();
33720+ mnt_drop_write_file(file);
33721+ lockdep_on();
33722+}
33723+#endif
33724+
33725+/* ---------------------------------------------------------------------- */
33726+
33727+struct au_hinode;
33728+struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33729+ struct dentry *d2, struct au_hinode *hdir2);
33730+void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33731+ struct dentry *d2, struct au_hinode *hdir2);
33732+
33733+int vfsub_create(struct inode *dir, struct path *path, int mode,
33734+ bool want_excl);
33735+int vfsub_symlink(struct inode *dir, struct path *path,
33736+ const char *symname);
33737+int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
33738+int vfsub_link(struct dentry *src_dentry, struct inode *dir,
33739+ struct path *path, struct inode **delegated_inode);
33740+int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
33741+ struct inode *hdir, struct path *path,
33742+ struct inode **delegated_inode, unsigned int flags);
33743+int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
33744+int vfsub_rmdir(struct inode *dir, struct path *path);
33745+
33746+/* ---------------------------------------------------------------------- */
33747+
33748+ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33749+ loff_t *ppos);
33750+ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33751+ loff_t *ppos);
33752+ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33753+ loff_t *ppos);
33754+ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
33755+ loff_t *ppos);
33756+int vfsub_flush(struct file *file, fl_owner_t id);
33757+int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
33758+
33759+static inline loff_t vfsub_f_size_read(struct file *file)
33760+{
33761+ return i_size_read(file_inode(file));
33762+}
33763+
33764+static inline unsigned int vfsub_file_flags(struct file *file)
33765+{
33766+ unsigned int flags;
33767+
33768+ spin_lock(&file->f_lock);
33769+ flags = file->f_flags;
33770+ spin_unlock(&file->f_lock);
33771+
33772+ return flags;
33773+}
33774+
33775+static inline int vfsub_file_execed(struct file *file)
33776+{
33777+ /* todo: direct access f_flags */
33778+ return !!(vfsub_file_flags(file) & __FMODE_EXEC);
33779+}
33780+
33781+#if 0 /* reserved */
33782+static inline void vfsub_file_accessed(struct file *h_file)
33783+{
33784+ file_accessed(h_file);
33785+ vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
33786+}
33787+#endif
33788+
33789+#if 0 /* reserved */
33790+static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
33791+ struct dentry *h_dentry)
33792+{
33793+ struct path h_path = {
33794+ .dentry = h_dentry,
33795+ .mnt = h_mnt
33796+ };
33797+ touch_atime(&h_path);
33798+ vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
33799+}
33800+#endif
33801+
33802+static inline int vfsub_update_time(struct inode *h_inode,
33803+ struct timespec64 *ts, int flags)
33804+{
33805+ return update_time(h_inode, ts, flags);
33806+ /* no vfsub_update_h_iattr() since we don't have struct path */
33807+}
33808+
33809+#ifdef CONFIG_FS_POSIX_ACL
33810+static inline int vfsub_acl_chmod(struct user_namespace *h_userns,
33811+ struct inode *h_inode, umode_t h_mode)
33812+{
33813+ int err;
33814+
33815+ err = posix_acl_chmod(h_userns, h_inode, h_mode);
33816+ if (err == -EOPNOTSUPP)
33817+ err = 0;
33818+ return err;
33819+}
33820+#else
33821+AuStubInt0(vfsub_acl_chmod, struct user_namespace *h_userns,
33822+ struct inode *h_inode, umode_t h_mode);
33823+#endif
33824+
33825+long vfsub_splice_to(struct file *in, loff_t *ppos,
33826+ struct pipe_inode_info *pipe, size_t len,
33827+ unsigned int flags);
33828+long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33829+ loff_t *ppos, size_t len, unsigned int flags);
33830+
33831+static inline long vfsub_truncate(struct path *path, loff_t length)
33832+{
33833+ long err;
33834+
33835+ lockdep_off();
33836+ err = vfs_truncate(path, length);
33837+ lockdep_on();
33838+ return err;
33839+}
33840+
33841+int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33842+ struct file *h_file);
33843+int vfsub_fsync(struct file *file, struct path *path, int datasync);
33844+
33845+/*
33846+ * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
33847+ * ioctl.
33848+ */
33849+static inline loff_t vfsub_clone_file_range(struct file *src, struct file *dst,
33850+ loff_t len)
33851+{
33852+ loff_t err;
33853+
33854+ lockdep_off();
33855+ err = vfs_clone_file_range(src, 0, dst, 0, len, /*remap_flags*/0);
33856+ lockdep_on();
33857+
33858+ return err;
33859+}
33860+
33861+/* copy_file_range(2) is a systemcall */
33862+static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
33863+ struct file *dst, loff_t dst_pos,
33864+ size_t len, unsigned int flags)
33865+{
33866+ ssize_t ssz;
33867+
33868+ lockdep_off();
33869+ ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
33870+ lockdep_on();
33871+
33872+ return ssz;
33873+}
33874+
33875+/* ---------------------------------------------------------------------- */
33876+
33877+static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
33878+{
33879+ loff_t err;
33880+
33881+ lockdep_off();
33882+ err = vfs_llseek(file, offset, origin);
33883+ lockdep_on();
33884+ return err;
33885+}
33886+
33887+/* ---------------------------------------------------------------------- */
33888+
33889+int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
33890+int vfsub_sio_rmdir(struct inode *dir, struct path *path);
33891+int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33892+ struct inode **delegated_inode);
33893+int vfsub_notify_change(struct path *path, struct iattr *ia,
33894+ struct inode **delegated_inode);
33895+int vfsub_unlink(struct inode *dir, struct path *path,
33896+ struct inode **delegated_inode, int force);
33897+
33898+static inline int vfsub_getattr(const struct path *path, struct kstat *st)
33899+{
33900+ return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
33901+}
33902+
33903+/* ---------------------------------------------------------------------- */
33904+
33905+static inline int vfsub_setxattr(struct user_namespace *userns,
33906+ struct dentry *dentry, const char *name,
33907+ const void *value, size_t size, int flags)
33908+{
33909+ int err;
33910+
33911+ lockdep_off();
33912+ err = vfs_setxattr(userns, dentry, name, value, size, flags);
33913+ lockdep_on();
33914+
33915+ return err;
33916+}
33917+
33918+static inline int vfsub_removexattr(struct user_namespace *userns,
33919+ struct dentry *dentry, const char *name)
33920+{
33921+ int err;
33922+
33923+ lockdep_off();
33924+ err = vfs_removexattr(userns, dentry, name);
33925+ lockdep_on();
33926+
33927+ return err;
33928+}
33929+
33930+#endif /* __KERNEL__ */
33931+#endif /* __AUFS_VFSUB_H__ */
33932diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
33933--- /usr/share/empty/fs/aufs/wbr_policy.c 1970-01-01 01:00:00.000000000 +0100
33934+++ linux/fs/aufs/wbr_policy.c 2021-02-24 13:33:42.747680497 +0100
33935@@ -0,0 +1,830 @@
33936+// SPDX-License-Identifier: GPL-2.0
33937+/*
33938+ * Copyright (C) 2005-2020 Junjiro R. Okajima
33939+ *
33940+ * This program, aufs is free software; you can redistribute it and/or modify
33941+ * it under the terms of the GNU General Public License as published by
33942+ * the Free Software Foundation; either version 2 of the License, or
33943+ * (at your option) any later version.
33944+ *
33945+ * This program is distributed in the hope that it will be useful,
33946+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
33947+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33948+ * GNU General Public License for more details.
33949+ *
33950+ * You should have received a copy of the GNU General Public License
33951+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
33952+ */
33953+
33954+/*
33955+ * policies for selecting one among multiple writable branches
33956+ */
33957+
33958+#include <linux/statfs.h>
33959+#include "aufs.h"
33960+
33961+/* subset of cpup_attr() */
33962+static noinline_for_stack
33963+int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
33964+{
33965+ int err, sbits;
33966+ struct iattr ia;
33967+ struct inode *h_isrc;
33968+
33969+ h_isrc = d_inode(h_src);
33970+ ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
33971+ ia.ia_mode = h_isrc->i_mode;
33972+ ia.ia_uid = h_isrc->i_uid;
33973+ ia.ia_gid = h_isrc->i_gid;
33974+ sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
33975+ au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
33976+ /* no delegation since it is just created */
33977+ err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
33978+
33979+ /* is this nfs only? */
33980+ if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
33981+ ia.ia_valid = ATTR_FORCE | ATTR_MODE;
33982+ ia.ia_mode = h_isrc->i_mode;
33983+ err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
33984+ }
33985+
33986+ return err;
33987+}
33988+
33989+#define AuCpdown_PARENT_OPQ 1
33990+#define AuCpdown_WHED (1 << 1)
33991+#define AuCpdown_MADE_DIR (1 << 2)
33992+#define AuCpdown_DIROPQ (1 << 3)
33993+#define au_ftest_cpdown(flags, name) ((flags) & AuCpdown_##name)
33994+#define au_fset_cpdown(flags, name) \
33995+ do { (flags) |= AuCpdown_##name; } while (0)
33996+#define au_fclr_cpdown(flags, name) \
33997+ do { (flags) &= ~AuCpdown_##name; } while (0)
33998+
33999+static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
34000+ unsigned int *flags)
34001+{
34002+ int err;
34003+ struct dentry *opq_dentry;
34004+
34005+ opq_dentry = au_diropq_create(dentry, bdst);
34006+ err = PTR_ERR(opq_dentry);
34007+ if (IS_ERR(opq_dentry))
34008+ goto out;
34009+ dput(opq_dentry);
34010+ au_fset_cpdown(*flags, DIROPQ);
34011+
34012+out:
34013+ return err;
34014+}
34015+
34016+static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
34017+ struct inode *dir, aufs_bindex_t bdst)
34018+{
34019+ int err;
34020+ struct path h_path;
34021+ struct au_branch *br;
34022+
34023+ br = au_sbr(dentry->d_sb, bdst);
34024+ h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
34025+ err = PTR_ERR(h_path.dentry);
34026+ if (IS_ERR(h_path.dentry))
34027+ goto out;
34028+
34029+ err = 0;
34030+ if (d_is_positive(h_path.dentry)) {
34031+ h_path.mnt = au_br_mnt(br);
34032+ err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
34033+ dentry);
34034+ }
34035+ dput(h_path.dentry);
34036+
34037+out:
34038+ return err;
34039+}
34040+
34041+static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
34042+ struct au_pin *pin,
34043+ struct dentry *h_parent, void *arg)
34044+{
34045+ int err, rerr;
34046+ aufs_bindex_t bopq, btop;
34047+ struct path h_path;
34048+ struct dentry *parent;
34049+ struct inode *h_dir, *h_inode, *inode, *dir;
34050+ unsigned int *flags = arg;
34051+
34052+ btop = au_dbtop(dentry);
34053+ /* dentry is di-locked */
34054+ parent = dget_parent(dentry);
34055+ dir = d_inode(parent);
34056+ h_dir = d_inode(h_parent);
34057+ AuDebugOn(h_dir != au_h_iptr(dir, bdst));
34058+ IMustLock(h_dir);
34059+
34060+ err = au_lkup_neg(dentry, bdst, /*wh*/0);
34061+ if (unlikely(err < 0))
34062+ goto out;
34063+ h_path.dentry = au_h_dptr(dentry, bdst);
34064+ h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
34065+ err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path, 0755);
34066+ if (unlikely(err))
34067+ goto out_put;
34068+ au_fset_cpdown(*flags, MADE_DIR);
34069+
34070+ bopq = au_dbdiropq(dentry);
34071+ au_fclr_cpdown(*flags, WHED);
34072+ au_fclr_cpdown(*flags, DIROPQ);
34073+ if (au_dbwh(dentry) == bdst)
34074+ au_fset_cpdown(*flags, WHED);
34075+ if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
34076+ au_fset_cpdown(*flags, PARENT_OPQ);
34077+ h_inode = d_inode(h_path.dentry);
34078+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
34079+ if (au_ftest_cpdown(*flags, WHED)) {
34080+ err = au_cpdown_dir_opq(dentry, bdst, flags);
34081+ if (unlikely(err)) {
34082+ inode_unlock(h_inode);
34083+ goto out_dir;
34084+ }
34085+ }
34086+
34087+ err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
34088+ inode_unlock(h_inode);
34089+ if (unlikely(err))
34090+ goto out_opq;
34091+
34092+ if (au_ftest_cpdown(*flags, WHED)) {
34093+ err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
34094+ if (unlikely(err))
34095+ goto out_opq;
34096+ }
34097+
34098+ inode = d_inode(dentry);
34099+ if (au_ibbot(inode) < bdst)
34100+ au_set_ibbot(inode, bdst);
34101+ au_set_h_iptr(inode, bdst, au_igrab(h_inode),
34102+ au_hi_flags(inode, /*isdir*/1));
34103+ au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
34104+ goto out; /* success */
34105+
34106+ /* revert */
34107+out_opq:
34108+ if (au_ftest_cpdown(*flags, DIROPQ)) {
34109+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
34110+ rerr = au_diropq_remove(dentry, bdst);
34111+ inode_unlock(h_inode);
34112+ if (unlikely(rerr)) {
34113+ AuIOErr("failed removing diropq for %pd b%d (%d)\n",
34114+ dentry, bdst, rerr);
34115+ err = -EIO;
34116+ goto out;
34117+ }
34118+ }
34119+out_dir:
34120+ if (au_ftest_cpdown(*flags, MADE_DIR)) {
34121+ rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
34122+ if (unlikely(rerr)) {
34123+ AuIOErr("failed removing %pd b%d (%d)\n",
34124+ dentry, bdst, rerr);
34125+ err = -EIO;
34126+ }
34127+ }
34128+out_put:
34129+ au_set_h_dptr(dentry, bdst, NULL);
34130+ if (au_dbbot(dentry) == bdst)
34131+ au_update_dbbot(dentry);
34132+out:
34133+ dput(parent);
34134+ return err;
34135+}
34136+
34137+int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
34138+{
34139+ int err;
34140+ unsigned int flags;
34141+
34142+ flags = 0;
34143+ err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
34144+
34145+ return err;
34146+}
34147+
34148+/* ---------------------------------------------------------------------- */
34149+
34150+/* policies for create */
34151+
34152+int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
34153+{
34154+ int err, i, j, ndentry;
34155+ aufs_bindex_t bopq;
34156+ struct au_dcsub_pages dpages;
34157+ struct au_dpage *dpage;
34158+ struct dentry **dentries, *parent, *d;
34159+
34160+ err = au_dpages_init(&dpages, GFP_NOFS);
34161+ if (unlikely(err))
34162+ goto out;
34163+ parent = dget_parent(dentry);
34164+ err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
34165+ if (unlikely(err))
34166+ goto out_free;
34167+
34168+ err = bindex;
34169+ for (i = 0; i < dpages.ndpage; i++) {
34170+ dpage = dpages.dpages + i;
34171+ dentries = dpage->dentries;
34172+ ndentry = dpage->ndentry;
34173+ for (j = 0; j < ndentry; j++) {
34174+ d = dentries[j];
34175+ di_read_lock_parent2(d, !AuLock_IR);
34176+ bopq = au_dbdiropq(d);
34177+ di_read_unlock(d, !AuLock_IR);
34178+ if (bopq >= 0 && bopq < err)
34179+ err = bopq;
34180+ }
34181+ }
34182+
34183+out_free:
34184+ dput(parent);
34185+ au_dpages_free(&dpages);
34186+out:
34187+ return err;
34188+}
34189+
34190+static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
34191+{
34192+ for (; bindex >= 0; bindex--)
34193+ if (!au_br_rdonly(au_sbr(sb, bindex)))
34194+ return bindex;
34195+ return -EROFS;
34196+}
34197+
34198+/* top down parent */
34199+static int au_wbr_create_tdp(struct dentry *dentry,
34200+ unsigned int flags __maybe_unused)
34201+{
34202+ int err;
34203+ aufs_bindex_t btop, bindex;
34204+ struct super_block *sb;
34205+ struct dentry *parent, *h_parent;
34206+
34207+ sb = dentry->d_sb;
34208+ btop = au_dbtop(dentry);
34209+ err = btop;
34210+ if (!au_br_rdonly(au_sbr(sb, btop)))
34211+ goto out;
34212+
34213+ err = -EROFS;
34214+ parent = dget_parent(dentry);
34215+ for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
34216+ h_parent = au_h_dptr(parent, bindex);
34217+ if (!h_parent || d_is_negative(h_parent))
34218+ continue;
34219+
34220+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
34221+ err = bindex;
34222+ break;
34223+ }
34224+ }
34225+ dput(parent);
34226+
34227+ /* bottom up here */
34228+ if (unlikely(err < 0)) {
34229+ err = au_wbr_bu(sb, btop - 1);
34230+ if (err >= 0)
34231+ err = au_wbr_nonopq(dentry, err);
34232+ }
34233+
34234+out:
34235+ AuDbg("b%d\n", err);
34236+ return err;
34237+}
34238+
34239+/* ---------------------------------------------------------------------- */
34240+
34241+/* an exception for the policy other than tdp */
34242+static int au_wbr_create_exp(struct dentry *dentry)
34243+{
34244+ int err;
34245+ aufs_bindex_t bwh, bdiropq;
34246+ struct dentry *parent;
34247+
34248+ err = -1;
34249+ bwh = au_dbwh(dentry);
34250+ parent = dget_parent(dentry);
34251+ bdiropq = au_dbdiropq(parent);
34252+ if (bwh >= 0) {
34253+ if (bdiropq >= 0)
34254+ err = min(bdiropq, bwh);
34255+ else
34256+ err = bwh;
34257+ AuDbg("%d\n", err);
34258+ } else if (bdiropq >= 0) {
34259+ err = bdiropq;
34260+ AuDbg("%d\n", err);
34261+ }
34262+ dput(parent);
34263+
34264+ if (err >= 0)
34265+ err = au_wbr_nonopq(dentry, err);
34266+
34267+ if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
34268+ err = -1;
34269+
34270+ AuDbg("%d\n", err);
34271+ return err;
34272+}
34273+
34274+/* ---------------------------------------------------------------------- */
34275+
34276+/* round robin */
34277+static int au_wbr_create_init_rr(struct super_block *sb)
34278+{
34279+ int err;
34280+
34281+ err = au_wbr_bu(sb, au_sbbot(sb));
34282+ atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
34283+ /* smp_mb(); */
34284+
34285+ AuDbg("b%d\n", err);
34286+ return err;
34287+}
34288+
34289+static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
34290+{
34291+ int err, nbr;
34292+ unsigned int u;
34293+ aufs_bindex_t bindex, bbot;
34294+ struct super_block *sb;
34295+ atomic_t *next;
34296+
34297+ err = au_wbr_create_exp(dentry);
34298+ if (err >= 0)
34299+ goto out;
34300+
34301+ sb = dentry->d_sb;
34302+ next = &au_sbi(sb)->si_wbr_rr_next;
34303+ bbot = au_sbbot(sb);
34304+ nbr = bbot + 1;
34305+ for (bindex = 0; bindex <= bbot; bindex++) {
34306+ if (!au_ftest_wbr(flags, DIR)) {
34307+ err = atomic_dec_return(next) + 1;
34308+ /* modulo for 0 is meaningless */
34309+ if (unlikely(!err))
34310+ err = atomic_dec_return(next) + 1;
34311+ } else
34312+ err = atomic_read(next);
34313+ AuDbg("%d\n", err);
34314+ u = err;
34315+ err = u % nbr;
34316+ AuDbg("%d\n", err);
34317+ if (!au_br_rdonly(au_sbr(sb, err)))
34318+ break;
34319+ err = -EROFS;
34320+ }
34321+
34322+ if (err >= 0)
34323+ err = au_wbr_nonopq(dentry, err);
34324+
34325+out:
34326+ AuDbg("%d\n", err);
34327+ return err;
34328+}
34329+
34330+/* ---------------------------------------------------------------------- */
34331+
34332+/* most free space */
34333+static void au_mfs(struct dentry *dentry, struct dentry *parent)
34334+{
34335+ struct super_block *sb;
34336+ struct au_branch *br;
34337+ struct au_wbr_mfs *mfs;
34338+ struct dentry *h_parent;
34339+ aufs_bindex_t bindex, bbot;
34340+ int err;
34341+ unsigned long long b, bavail;
34342+ struct path h_path;
34343+ /* reduce the stack usage */
34344+ struct kstatfs *st;
34345+
34346+ st = kmalloc(sizeof(*st), GFP_NOFS);
34347+ if (unlikely(!st)) {
34348+ AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
34349+ return;
34350+ }
34351+
34352+ bavail = 0;
34353+ sb = dentry->d_sb;
34354+ mfs = &au_sbi(sb)->si_wbr_mfs;
34355+ MtxMustLock(&mfs->mfs_lock);
34356+ mfs->mfs_bindex = -EROFS;
34357+ mfs->mfsrr_bytes = 0;
34358+ if (!parent) {
34359+ bindex = 0;
34360+ bbot = au_sbbot(sb);
34361+ } else {
34362+ bindex = au_dbtop(parent);
34363+ bbot = au_dbtaildir(parent);
34364+ }
34365+
34366+ for (; bindex <= bbot; bindex++) {
34367+ if (parent) {
34368+ h_parent = au_h_dptr(parent, bindex);
34369+ if (!h_parent || d_is_negative(h_parent))
34370+ continue;
34371+ }
34372+ br = au_sbr(sb, bindex);
34373+ if (au_br_rdonly(br))
34374+ continue;
34375+
34376+ /* sb->s_root for NFS is unreliable */
34377+ h_path.mnt = au_br_mnt(br);
34378+ h_path.dentry = h_path.mnt->mnt_root;
34379+ err = vfs_statfs(&h_path, st);
34380+ if (unlikely(err)) {
34381+ AuWarn1("failed statfs, b%d, %d\n", bindex, err);
34382+ continue;
34383+ }
34384+
34385+ /* when the available size is equal, select the lower one */
34386+ BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
34387+ || sizeof(b) < sizeof(st->f_bsize));
34388+ b = st->f_bavail * st->f_bsize;
34389+ br->br_wbr->wbr_bytes = b;
34390+ if (b >= bavail) {
34391+ bavail = b;
34392+ mfs->mfs_bindex = bindex;
34393+ mfs->mfs_jiffy = jiffies;
34394+ }
34395+ }
34396+
34397+ mfs->mfsrr_bytes = bavail;
34398+ AuDbg("b%d\n", mfs->mfs_bindex);
34399+ au_kfree_rcu(st);
34400+}
34401+
34402+static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
34403+{
34404+ int err;
34405+ struct dentry *parent;
34406+ struct super_block *sb;
34407+ struct au_wbr_mfs *mfs;
34408+
34409+ err = au_wbr_create_exp(dentry);
34410+ if (err >= 0)
34411+ goto out;
34412+
34413+ sb = dentry->d_sb;
34414+ parent = NULL;
34415+ if (au_ftest_wbr(flags, PARENT))
34416+ parent = dget_parent(dentry);
34417+ mfs = &au_sbi(sb)->si_wbr_mfs;
34418+ mutex_lock(&mfs->mfs_lock);
34419+ if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34420+ || mfs->mfs_bindex < 0
34421+ || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
34422+ au_mfs(dentry, parent);
34423+ mutex_unlock(&mfs->mfs_lock);
34424+ err = mfs->mfs_bindex;
34425+ dput(parent);
34426+
34427+ if (err >= 0)
34428+ err = au_wbr_nonopq(dentry, err);
34429+
34430+out:
34431+ AuDbg("b%d\n", err);
34432+ return err;
34433+}
34434+
34435+static int au_wbr_create_init_mfs(struct super_block *sb)
34436+{
34437+ struct au_wbr_mfs *mfs;
34438+
34439+ mfs = &au_sbi(sb)->si_wbr_mfs;
34440+ mutex_init(&mfs->mfs_lock);
34441+ mfs->mfs_jiffy = 0;
34442+ mfs->mfs_bindex = -EROFS;
34443+
34444+ return 0;
34445+}
34446+
34447+static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
34448+{
34449+ mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
34450+ return 0;
34451+}
34452+
34453+/* ---------------------------------------------------------------------- */
34454+
34455+/* top down regardless parent, and then mfs */
34456+static int au_wbr_create_tdmfs(struct dentry *dentry,
34457+ unsigned int flags __maybe_unused)
34458+{
34459+ int err;
34460+ aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
34461+ unsigned long long watermark;
34462+ struct super_block *sb;
34463+ struct au_wbr_mfs *mfs;
34464+ struct au_branch *br;
34465+ struct dentry *parent;
34466+
34467+ sb = dentry->d_sb;
34468+ mfs = &au_sbi(sb)->si_wbr_mfs;
34469+ mutex_lock(&mfs->mfs_lock);
34470+ if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34471+ || mfs->mfs_bindex < 0)
34472+ au_mfs(dentry, /*parent*/NULL);
34473+ watermark = mfs->mfsrr_watermark;
34474+ bmfs = mfs->mfs_bindex;
34475+ mutex_unlock(&mfs->mfs_lock);
34476+
34477+ /* another style of au_wbr_create_exp() */
34478+ bwh = au_dbwh(dentry);
34479+ parent = dget_parent(dentry);
34480+ btail = au_dbtaildir(parent);
34481+ if (bwh >= 0 && bwh < btail)
34482+ btail = bwh;
34483+
34484+ err = au_wbr_nonopq(dentry, btail);
34485+ if (unlikely(err < 0))
34486+ goto out;
34487+ btail = err;
34488+ bfound = -1;
34489+ for (bindex = 0; bindex <= btail; bindex++) {
34490+ br = au_sbr(sb, bindex);
34491+ if (au_br_rdonly(br))
34492+ continue;
34493+ if (br->br_wbr->wbr_bytes > watermark) {
34494+ bfound = bindex;
34495+ break;
34496+ }
34497+ }
34498+ err = bfound;
34499+ if (err < 0)
34500+ err = bmfs;
34501+
34502+out:
34503+ dput(parent);
34504+ AuDbg("b%d\n", err);
34505+ return err;
34506+}
34507+
34508+/* ---------------------------------------------------------------------- */
34509+
34510+/* most free space and then round robin */
34511+static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
34512+{
34513+ int err;
34514+ struct au_wbr_mfs *mfs;
34515+
34516+ err = au_wbr_create_mfs(dentry, flags);
34517+ if (err >= 0) {
34518+ mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
34519+ mutex_lock(&mfs->mfs_lock);
34520+ if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
34521+ err = au_wbr_create_rr(dentry, flags);
34522+ mutex_unlock(&mfs->mfs_lock);
34523+ }
34524+
34525+ AuDbg("b%d\n", err);
34526+ return err;
34527+}
34528+
34529+static int au_wbr_create_init_mfsrr(struct super_block *sb)
34530+{
34531+ int err;
34532+
34533+ au_wbr_create_init_mfs(sb); /* ignore */
34534+ err = au_wbr_create_init_rr(sb);
34535+
34536+ return err;
34537+}
34538+
34539+/* ---------------------------------------------------------------------- */
34540+
34541+/* top down parent and most free space */
34542+static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
34543+{
34544+ int err, e2;
34545+ unsigned long long b;
34546+ aufs_bindex_t bindex, btop, bbot;
34547+ struct super_block *sb;
34548+ struct dentry *parent, *h_parent;
34549+ struct au_branch *br;
34550+
34551+ err = au_wbr_create_tdp(dentry, flags);
34552+ if (unlikely(err < 0))
34553+ goto out;
34554+ parent = dget_parent(dentry);
34555+ btop = au_dbtop(parent);
34556+ bbot = au_dbtaildir(parent);
34557+ if (btop == bbot)
34558+ goto out_parent; /* success */
34559+
34560+ e2 = au_wbr_create_mfs(dentry, flags);
34561+ if (e2 < 0)
34562+ goto out_parent; /* success */
34563+
34564+ /* when the available size is equal, select upper one */
34565+ sb = dentry->d_sb;
34566+ br = au_sbr(sb, err);
34567+ b = br->br_wbr->wbr_bytes;
34568+ AuDbg("b%d, %llu\n", err, b);
34569+
34570+ for (bindex = btop; bindex <= bbot; bindex++) {
34571+ h_parent = au_h_dptr(parent, bindex);
34572+ if (!h_parent || d_is_negative(h_parent))
34573+ continue;
34574+
34575+ br = au_sbr(sb, bindex);
34576+ if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
34577+ b = br->br_wbr->wbr_bytes;
34578+ err = bindex;
34579+ AuDbg("b%d, %llu\n", err, b);
34580+ }
34581+ }
34582+
34583+ if (err >= 0)
34584+ err = au_wbr_nonopq(dentry, err);
34585+
34586+out_parent:
34587+ dput(parent);
34588+out:
34589+ AuDbg("b%d\n", err);
34590+ return err;
34591+}
34592+
34593+/* ---------------------------------------------------------------------- */
34594+
34595+/*
34596+ * - top down parent
34597+ * - most free space with parent
34598+ * - most free space round-robin regardless parent
34599+ */
34600+static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
34601+{
34602+ int err;
34603+ unsigned long long watermark;
34604+ struct super_block *sb;
34605+ struct au_branch *br;
34606+ struct au_wbr_mfs *mfs;
34607+
34608+ err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
34609+ if (unlikely(err < 0))
34610+ goto out;
34611+
34612+ sb = dentry->d_sb;
34613+ br = au_sbr(sb, err);
34614+ mfs = &au_sbi(sb)->si_wbr_mfs;
34615+ mutex_lock(&mfs->mfs_lock);
34616+ watermark = mfs->mfsrr_watermark;
34617+ mutex_unlock(&mfs->mfs_lock);
34618+ if (br->br_wbr->wbr_bytes < watermark)
34619+ /* regardless the parent dir */
34620+ err = au_wbr_create_mfsrr(dentry, flags);
34621+
34622+out:
34623+ AuDbg("b%d\n", err);
34624+ return err;
34625+}
34626+
34627+/* ---------------------------------------------------------------------- */
34628+
34629+/* policies for copyup */
34630+
34631+/* top down parent */
34632+static int au_wbr_copyup_tdp(struct dentry *dentry)
34633+{
34634+ return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
34635+}
34636+
34637+/* bottom up parent */
34638+static int au_wbr_copyup_bup(struct dentry *dentry)
34639+{
34640+ int err;
34641+ aufs_bindex_t bindex, btop;
34642+ struct dentry *parent, *h_parent;
34643+ struct super_block *sb;
34644+
34645+ err = -EROFS;
34646+ sb = dentry->d_sb;
34647+ parent = dget_parent(dentry);
34648+ btop = au_dbtop(parent);
34649+ for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
34650+ h_parent = au_h_dptr(parent, bindex);
34651+ if (!h_parent || d_is_negative(h_parent))
34652+ continue;
34653+
34654+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
34655+ err = bindex;
34656+ break;
34657+ }
34658+ }
34659+ dput(parent);
34660+
34661+ /* bottom up here */
34662+ if (unlikely(err < 0))
34663+ err = au_wbr_bu(sb, btop - 1);
34664+
34665+ AuDbg("b%d\n", err);
34666+ return err;
34667+}
34668+
34669+/* bottom up */
34670+int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
34671+{
34672+ int err;
34673+
34674+ err = au_wbr_bu(dentry->d_sb, btop);
34675+ AuDbg("b%d\n", err);
34676+ if (err > btop)
34677+ err = au_wbr_nonopq(dentry, err);
34678+
34679+ AuDbg("b%d\n", err);
34680+ return err;
34681+}
34682+
34683+static int au_wbr_copyup_bu(struct dentry *dentry)
34684+{
34685+ int err;
34686+ aufs_bindex_t btop;
34687+
34688+ btop = au_dbtop(dentry);
34689+ err = au_wbr_do_copyup_bu(dentry, btop);
34690+ return err;
34691+}
34692+
34693+/* ---------------------------------------------------------------------- */
34694+
34695+struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
34696+ [AuWbrCopyup_TDP] = {
34697+ .copyup = au_wbr_copyup_tdp
34698+ },
34699+ [AuWbrCopyup_BUP] = {
34700+ .copyup = au_wbr_copyup_bup
34701+ },
34702+ [AuWbrCopyup_BU] = {
34703+ .copyup = au_wbr_copyup_bu
34704+ }
34705+};
34706+
34707+struct au_wbr_create_operations au_wbr_create_ops[] = {
34708+ [AuWbrCreate_TDP] = {
34709+ .create = au_wbr_create_tdp
34710+ },
34711+ [AuWbrCreate_RR] = {
34712+ .create = au_wbr_create_rr,
34713+ .init = au_wbr_create_init_rr
34714+ },
34715+ [AuWbrCreate_MFS] = {
34716+ .create = au_wbr_create_mfs,
34717+ .init = au_wbr_create_init_mfs,
34718+ .fin = au_wbr_create_fin_mfs
34719+ },
34720+ [AuWbrCreate_MFSV] = {
34721+ .create = au_wbr_create_mfs,
34722+ .init = au_wbr_create_init_mfs,
34723+ .fin = au_wbr_create_fin_mfs
34724+ },
34725+ [AuWbrCreate_MFSRR] = {
34726+ .create = au_wbr_create_mfsrr,
34727+ .init = au_wbr_create_init_mfsrr,
34728+ .fin = au_wbr_create_fin_mfs
34729+ },
34730+ [AuWbrCreate_MFSRRV] = {
34731+ .create = au_wbr_create_mfsrr,
34732+ .init = au_wbr_create_init_mfsrr,
34733+ .fin = au_wbr_create_fin_mfs
34734+ },
34735+ [AuWbrCreate_TDMFS] = {
34736+ .create = au_wbr_create_tdmfs,
34737+ .init = au_wbr_create_init_mfs,
34738+ .fin = au_wbr_create_fin_mfs
34739+ },
34740+ [AuWbrCreate_TDMFSV] = {
34741+ .create = au_wbr_create_tdmfs,
34742+ .init = au_wbr_create_init_mfs,
34743+ .fin = au_wbr_create_fin_mfs
34744+ },
34745+ [AuWbrCreate_PMFS] = {
34746+ .create = au_wbr_create_pmfs,
34747+ .init = au_wbr_create_init_mfs,
34748+ .fin = au_wbr_create_fin_mfs
34749+ },
34750+ [AuWbrCreate_PMFSV] = {
34751+ .create = au_wbr_create_pmfs,
34752+ .init = au_wbr_create_init_mfs,
34753+ .fin = au_wbr_create_fin_mfs
34754+ },
34755+ [AuWbrCreate_PMFSRR] = {
34756+ .create = au_wbr_create_pmfsrr,
34757+ .init = au_wbr_create_init_mfsrr,
34758+ .fin = au_wbr_create_fin_mfs
34759+ },
34760+ [AuWbrCreate_PMFSRRV] = {
34761+ .create = au_wbr_create_pmfsrr,
34762+ .init = au_wbr_create_init_mfsrr,
34763+ .fin = au_wbr_create_fin_mfs
34764+ }
34765+};
34766diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
34767--- /usr/share/empty/fs/aufs/whout.c 1970-01-01 01:00:00.000000000 +0100
34768+++ linux/fs/aufs/whout.c 2021-06-30 21:35:11.397206648 +0200
34769@@ -0,0 +1,1070 @@
34770+// SPDX-License-Identifier: GPL-2.0
34771+/*
34772+ * Copyright (C) 2005-2020 Junjiro R. Okajima
34773+ *
34774+ * This program, aufs is free software; you can redistribute it and/or modify
34775+ * it under the terms of the GNU General Public License as published by
34776+ * the Free Software Foundation; either version 2 of the License, or
34777+ * (at your option) any later version.
34778+ *
34779+ * This program is distributed in the hope that it will be useful,
34780+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
34781+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34782+ * GNU General Public License for more details.
34783+ *
34784+ * You should have received a copy of the GNU General Public License
34785+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
34786+ */
34787+
34788+/*
34789+ * whiteout for logical deletion and opaque directory
34790+ */
34791+
34792+#include "aufs.h"
34793+
34794+#define WH_MASK 0444
34795+
34796+/*
34797+ * If a directory contains this file, then it is opaque. We start with the
34798+ * .wh. flag so that it is blocked by lookup.
34799+ */
34800+static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
34801+ sizeof(AUFS_WH_DIROPQ) - 1);
34802+
34803+/*
34804+ * generate whiteout name, which is NOT terminated by NULL.
34805+ * @name: original d_name.name
34806+ * @len: original d_name.len
34807+ * @wh: whiteout qstr
34808+ * returns zero when succeeds, otherwise error.
34809+ * succeeded value as wh->name should be freed by kfree().
34810+ */
34811+int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
34812+{
34813+ char *p;
34814+
34815+ if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
34816+ return -ENAMETOOLONG;
34817+
34818+ wh->len = name->len + AUFS_WH_PFX_LEN;
34819+ p = kmalloc(wh->len, GFP_NOFS);
34820+ wh->name = p;
34821+ if (p) {
34822+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
34823+ memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
34824+ /* smp_mb(); */
34825+ return 0;
34826+ }
34827+ return -ENOMEM;
34828+}
34829+
34830+/* ---------------------------------------------------------------------- */
34831+
34832+/*
34833+ * test if the @wh_name exists under @h_parent.
34834+ * @try_sio specifies the necessary of super-io.
34835+ */
34836+int au_wh_test(struct user_namespace *h_userns, struct dentry *h_parent,
34837+ struct qstr *wh_name, int try_sio)
34838+{
34839+ int err;
34840+ struct dentry *wh_dentry;
34841+
34842+ if (!try_sio)
34843+ wh_dentry = vfsub_lkup_one(wh_name, h_parent);
34844+ else
34845+ wh_dentry = au_sio_lkup_one(h_userns, wh_name, h_parent);
34846+ err = PTR_ERR(wh_dentry);
34847+ if (IS_ERR(wh_dentry)) {
34848+ if (err == -ENAMETOOLONG)
34849+ err = 0;
34850+ goto out;
34851+ }
34852+
34853+ err = 0;
34854+ if (d_is_negative(wh_dentry))
34855+ goto out_wh; /* success */
34856+
34857+ err = 1;
34858+ if (d_is_reg(wh_dentry))
34859+ goto out_wh; /* success */
34860+
34861+ err = -EIO;
34862+ AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
34863+ wh_dentry, d_inode(wh_dentry)->i_mode);
34864+
34865+out_wh:
34866+ dput(wh_dentry);
34867+out:
34868+ return err;
34869+}
34870+
34871+/*
34872+ * test if the @h_dentry sets opaque or not.
34873+ */
34874+int au_diropq_test(struct user_namespace *h_userns, struct dentry *h_dentry)
34875+{
34876+ int err;
34877+ struct inode *h_dir;
34878+
34879+ h_dir = d_inode(h_dentry);
34880+ err = au_wh_test(h_userns, h_dentry, &diropq_name,
34881+ au_test_h_perm_sio(h_userns, h_dir, MAY_EXEC));
34882+ return err;
34883+}
34884+
34885+/*
34886+ * returns a negative dentry whose name is unique and temporary.
34887+ */
34888+struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
34889+ struct qstr *prefix)
34890+{
34891+ struct dentry *dentry;
34892+ int i;
34893+ char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
34894+ *name, *p;
34895+ /* strict atomic_t is unnecessary here */
34896+ static unsigned short cnt;
34897+ struct qstr qs;
34898+ struct user_namespace *h_userns;
34899+
34900+ BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
34901+
34902+ name = defname;
34903+ qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
34904+ if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
34905+ dentry = ERR_PTR(-ENAMETOOLONG);
34906+ if (unlikely(qs.len > NAME_MAX))
34907+ goto out;
34908+ dentry = ERR_PTR(-ENOMEM);
34909+ name = kmalloc(qs.len + 1, GFP_NOFS);
34910+ if (unlikely(!name))
34911+ goto out;
34912+ }
34913+
34914+ /* doubly whiteout-ed */
34915+ memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
34916+ p = name + AUFS_WH_PFX_LEN * 2;
34917+ memcpy(p, prefix->name, prefix->len);
34918+ p += prefix->len;
34919+ *p++ = '.';
34920+ AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
34921+
34922+ h_userns = au_br_userns(br);
34923+ qs.name = name;
34924+ for (i = 0; i < 3; i++) {
34925+ sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
34926+ dentry = au_sio_lkup_one(h_userns, &qs, h_parent);
34927+ if (IS_ERR(dentry) || d_is_negative(dentry))
34928+ goto out_name;
34929+ dput(dentry);
34930+ }
34931+ /* pr_warn("could not get random name\n"); */
34932+ dentry = ERR_PTR(-EEXIST);
34933+ AuDbg("%.*s\n", AuLNPair(&qs));
34934+ BUG();
34935+
34936+out_name:
34937+ if (name != defname)
34938+ au_kfree_try_rcu(name);
34939+out:
34940+ AuTraceErrPtr(dentry);
34941+ return dentry;
34942+}
34943+
34944+/*
34945+ * rename the @h_dentry on @br to the whiteouted temporary name.
34946+ */
34947+int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
34948+{
34949+ int err;
34950+ struct path h_path = {
34951+ .mnt = au_br_mnt(br)
34952+ };
34953+ struct inode *h_dir, *delegated;
34954+ struct dentry *h_parent;
34955+
34956+ h_parent = h_dentry->d_parent; /* dir inode is locked */
34957+ h_dir = d_inode(h_parent);
34958+ IMustLock(h_dir);
34959+
34960+ h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
34961+ err = PTR_ERR(h_path.dentry);
34962+ if (IS_ERR(h_path.dentry))
34963+ goto out;
34964+
34965+ /* under the same dir, no need to lock_rename() */
34966+ delegated = NULL;
34967+ err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
34968+ /*flags*/0);
34969+ AuTraceErr(err);
34970+ if (unlikely(err == -EWOULDBLOCK)) {
34971+ pr_warn("cannot retry for NFSv4 delegation"
34972+ " for an internal rename\n");
34973+ iput(delegated);
34974+ }
34975+ dput(h_path.dentry);
34976+
34977+out:
34978+ AuTraceErr(err);
34979+ return err;
34980+}
34981+
34982+/* ---------------------------------------------------------------------- */
34983+/*
34984+ * functions for removing a whiteout
34985+ */
34986+
34987+static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
34988+{
34989+ int err, force;
34990+ struct inode *delegated;
34991+
34992+ /*
34993+ * forces superio when the dir has a sticky bit.
34994+ * this may be a violation of unix fs semantics.
34995+ */
34996+ force = (h_dir->i_mode & S_ISVTX)
34997+ && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
34998+ delegated = NULL;
34999+ err = vfsub_unlink(h_dir, h_path, &delegated, force);
35000+ if (unlikely(err == -EWOULDBLOCK)) {
35001+ pr_warn("cannot retry for NFSv4 delegation"
35002+ " for an internal unlink\n");
35003+ iput(delegated);
35004+ }
35005+ return err;
35006+}
35007+
35008+int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35009+ struct dentry *dentry)
35010+{
35011+ int err;
35012+
35013+ err = do_unlink_wh(h_dir, h_path);
35014+ if (!err && dentry)
35015+ au_set_dbwh(dentry, -1);
35016+
35017+ return err;
35018+}
35019+
35020+static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
35021+ struct au_branch *br)
35022+{
35023+ int err;
35024+ struct path h_path = {
35025+ .mnt = au_br_mnt(br)
35026+ };
35027+
35028+ err = 0;
35029+ h_path.dentry = vfsub_lkup_one(wh, h_parent);
35030+ if (IS_ERR(h_path.dentry))
35031+ err = PTR_ERR(h_path.dentry);
35032+ else {
35033+ if (d_is_reg(h_path.dentry))
35034+ err = do_unlink_wh(d_inode(h_parent), &h_path);
35035+ dput(h_path.dentry);
35036+ }
35037+
35038+ return err;
35039+}
35040+
35041+/* ---------------------------------------------------------------------- */
35042+/*
35043+ * initialize/clean whiteout for a branch
35044+ */
35045+
35046+static void au_wh_clean(struct inode *h_dir, struct path *whpath,
35047+ const int isdir)
35048+{
35049+ int err;
35050+ struct inode *delegated;
35051+
35052+ if (d_is_negative(whpath->dentry))
35053+ return;
35054+
35055+ if (isdir)
35056+ err = vfsub_rmdir(h_dir, whpath);
35057+ else {
35058+ delegated = NULL;
35059+ err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
35060+ if (unlikely(err == -EWOULDBLOCK)) {
35061+ pr_warn("cannot retry for NFSv4 delegation"
35062+ " for an internal unlink\n");
35063+ iput(delegated);
35064+ }
35065+ }
35066+ if (unlikely(err))
35067+ pr_warn("failed removing %pd (%d), ignored.\n",
35068+ whpath->dentry, err);
35069+}
35070+
35071+static int test_linkable(struct dentry *h_root)
35072+{
35073+ struct inode *h_dir = d_inode(h_root);
35074+
35075+ if (h_dir->i_op->link)
35076+ return 0;
35077+
35078+ pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
35079+ h_root, au_sbtype(h_root->d_sb));
35080+ return -ENOSYS; /* the branch doesn't have its ->link() */
35081+}
35082+
35083+/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
35084+static int au_whdir(struct inode *h_dir, struct path *path)
35085+{
35086+ int err;
35087+
35088+ err = -EEXIST;
35089+ if (d_is_negative(path->dentry)) {
35090+ int mode = 0700;
35091+
35092+ if (au_test_nfs(path->dentry->d_sb))
35093+ mode |= 0111;
35094+ err = vfsub_mkdir(h_dir, path, mode);
35095+ } else if (d_is_dir(path->dentry))
35096+ err = 0;
35097+ else
35098+ pr_err("unknown %pd exists\n", path->dentry);
35099+
35100+ return err;
35101+}
35102+
35103+struct au_wh_base {
35104+ const struct qstr *name;
35105+ struct dentry *dentry;
35106+};
35107+
35108+static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
35109+ struct path *h_path)
35110+{
35111+ h_path->dentry = base[AuBrWh_BASE].dentry;
35112+ au_wh_clean(h_dir, h_path, /*isdir*/0);
35113+ h_path->dentry = base[AuBrWh_PLINK].dentry;
35114+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35115+ h_path->dentry = base[AuBrWh_ORPH].dentry;
35116+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35117+}
35118+
35119+/*
35120+ * returns tri-state,
35121+ * minus: error, caller should print the message
35122+ * zero: success
35123+ * plus: error, caller should NOT print the message
35124+ */
35125+static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
35126+ int do_plink, struct au_wh_base base[],
35127+ struct path *h_path)
35128+{
35129+ int err;
35130+ struct inode *h_dir;
35131+
35132+ h_dir = d_inode(h_root);
35133+ h_path->dentry = base[AuBrWh_BASE].dentry;
35134+ au_wh_clean(h_dir, h_path, /*isdir*/0);
35135+ h_path->dentry = base[AuBrWh_PLINK].dentry;
35136+ if (do_plink) {
35137+ err = test_linkable(h_root);
35138+ if (unlikely(err)) {
35139+ err = 1;
35140+ goto out;
35141+ }
35142+
35143+ err = au_whdir(h_dir, h_path);
35144+ if (unlikely(err))
35145+ goto out;
35146+ wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35147+ } else
35148+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35149+ h_path->dentry = base[AuBrWh_ORPH].dentry;
35150+ err = au_whdir(h_dir, h_path);
35151+ if (unlikely(err))
35152+ goto out;
35153+ wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35154+
35155+out:
35156+ return err;
35157+}
35158+
35159+/*
35160+ * for the moment, aufs supports the branch filesystem which does not support
35161+ * link(2). testing on FAT which does not support i_op->setattr() fully either,
35162+ * copyup failed. finally, such filesystem will not be used as the writable
35163+ * branch.
35164+ *
35165+ * returns tri-state, see above.
35166+ */
35167+static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
35168+ int do_plink, struct au_wh_base base[],
35169+ struct path *h_path)
35170+{
35171+ int err;
35172+ struct inode *h_dir;
35173+
35174+ WbrWhMustWriteLock(wbr);
35175+
35176+ err = test_linkable(h_root);
35177+ if (unlikely(err)) {
35178+ err = 1;
35179+ goto out;
35180+ }
35181+
35182+ /*
35183+ * todo: should this create be done in /sbin/mount.aufs helper?
35184+ */
35185+ err = -EEXIST;
35186+ h_dir = d_inode(h_root);
35187+ if (d_is_negative(base[AuBrWh_BASE].dentry)) {
35188+ h_path->dentry = base[AuBrWh_BASE].dentry;
35189+ err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
35190+ } else if (d_is_reg(base[AuBrWh_BASE].dentry))
35191+ err = 0;
35192+ else
35193+ pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
35194+ if (unlikely(err))
35195+ goto out;
35196+
35197+ h_path->dentry = base[AuBrWh_PLINK].dentry;
35198+ if (do_plink) {
35199+ err = au_whdir(h_dir, h_path);
35200+ if (unlikely(err))
35201+ goto out;
35202+ wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35203+ } else
35204+ au_wh_clean(h_dir, h_path, /*isdir*/1);
35205+ wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
35206+
35207+ h_path->dentry = base[AuBrWh_ORPH].dentry;
35208+ err = au_whdir(h_dir, h_path);
35209+ if (unlikely(err))
35210+ goto out;
35211+ wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35212+
35213+out:
35214+ return err;
35215+}
35216+
35217+/*
35218+ * initialize the whiteout base file/dir for @br.
35219+ */
35220+int au_wh_init(struct au_branch *br, struct super_block *sb)
35221+{
35222+ int err, i;
35223+ const unsigned char do_plink
35224+ = !!au_opt_test(au_mntflags(sb), PLINK);
35225+ struct inode *h_dir;
35226+ struct path path = br->br_path;
35227+ struct dentry *h_root = path.dentry;
35228+ struct au_wbr *wbr = br->br_wbr;
35229+ static const struct qstr base_name[] = {
35230+ [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
35231+ sizeof(AUFS_BASE_NAME) - 1),
35232+ [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
35233+ sizeof(AUFS_PLINKDIR_NAME) - 1),
35234+ [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
35235+ sizeof(AUFS_ORPHDIR_NAME) - 1)
35236+ };
35237+ struct au_wh_base base[] = {
35238+ [AuBrWh_BASE] = {
35239+ .name = base_name + AuBrWh_BASE,
35240+ .dentry = NULL
35241+ },
35242+ [AuBrWh_PLINK] = {
35243+ .name = base_name + AuBrWh_PLINK,
35244+ .dentry = NULL
35245+ },
35246+ [AuBrWh_ORPH] = {
35247+ .name = base_name + AuBrWh_ORPH,
35248+ .dentry = NULL
35249+ }
35250+ };
35251+
35252+ if (wbr)
35253+ WbrWhMustWriteLock(wbr);
35254+
35255+ for (i = 0; i < AuBrWh_Last; i++) {
35256+ /* doubly whiteouted */
35257+ struct dentry *d;
35258+
35259+ d = au_wh_lkup(h_root, (void *)base[i].name, br);
35260+ err = PTR_ERR(d);
35261+ if (IS_ERR(d))
35262+ goto out;
35263+
35264+ base[i].dentry = d;
35265+ AuDebugOn(wbr
35266+ && wbr->wbr_wh[i]
35267+ && wbr->wbr_wh[i] != base[i].dentry);
35268+ }
35269+
35270+ if (wbr)
35271+ for (i = 0; i < AuBrWh_Last; i++) {
35272+ dput(wbr->wbr_wh[i]);
35273+ wbr->wbr_wh[i] = NULL;
35274+ }
35275+
35276+ err = 0;
35277+ if (!au_br_writable(br->br_perm)) {
35278+ h_dir = d_inode(h_root);
35279+ au_wh_init_ro(h_dir, base, &path);
35280+ } else if (!au_br_wh_linkable(br->br_perm)) {
35281+ err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
35282+ if (err > 0)
35283+ goto out;
35284+ else if (err)
35285+ goto out_err;
35286+ } else {
35287+ err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
35288+ if (err > 0)
35289+ goto out;
35290+ else if (err)
35291+ goto out_err;
35292+ }
35293+ goto out; /* success */
35294+
35295+out_err:
35296+ pr_err("an error(%d) on the writable branch %pd(%s)\n",
35297+ err, h_root, au_sbtype(h_root->d_sb));
35298+out:
35299+ for (i = 0; i < AuBrWh_Last; i++)
35300+ dput(base[i].dentry);
35301+ return err;
35302+}
35303+
35304+/* ---------------------------------------------------------------------- */
35305+/*
35306+ * whiteouts are all hard-linked usually.
35307+ * when its link count reaches a ceiling, we create a new whiteout base
35308+ * asynchronously.
35309+ */
35310+
35311+struct reinit_br_wh {
35312+ struct super_block *sb;
35313+ struct au_branch *br;
35314+};
35315+
35316+static void reinit_br_wh(void *arg)
35317+{
35318+ int err;
35319+ aufs_bindex_t bindex;
35320+ struct path h_path;
35321+ struct reinit_br_wh *a = arg;
35322+ struct au_wbr *wbr;
35323+ struct inode *dir, *delegated;
35324+ struct dentry *h_root;
35325+ struct au_hinode *hdir;
35326+
35327+ err = 0;
35328+ wbr = a->br->br_wbr;
35329+ /* big aufs lock */
35330+ si_noflush_write_lock(a->sb);
35331+ if (!au_br_writable(a->br->br_perm))
35332+ goto out;
35333+ bindex = au_br_index(a->sb, a->br->br_id);
35334+ if (unlikely(bindex < 0))
35335+ goto out;
35336+
35337+ di_read_lock_parent(a->sb->s_root, AuLock_IR);
35338+ dir = d_inode(a->sb->s_root);
35339+ hdir = au_hi(dir, bindex);
35340+ h_root = au_h_dptr(a->sb->s_root, bindex);
35341+ AuDebugOn(h_root != au_br_dentry(a->br));
35342+
35343+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
35344+ wbr_wh_write_lock(wbr);
35345+ err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
35346+ h_root, a->br);
35347+ if (!err) {
35348+ h_path.dentry = wbr->wbr_whbase;
35349+ h_path.mnt = au_br_mnt(a->br);
35350+ delegated = NULL;
35351+ err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
35352+ /*force*/0);
35353+ if (unlikely(err == -EWOULDBLOCK)) {
35354+ pr_warn("cannot retry for NFSv4 delegation"
35355+ " for an internal unlink\n");
35356+ iput(delegated);
35357+ }
35358+ } else {
35359+ pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
35360+ err = 0;
35361+ }
35362+ dput(wbr->wbr_whbase);
35363+ wbr->wbr_whbase = NULL;
35364+ if (!err)
35365+ err = au_wh_init(a->br, a->sb);
35366+ wbr_wh_write_unlock(wbr);
35367+ au_hn_inode_unlock(hdir);
35368+ di_read_unlock(a->sb->s_root, AuLock_IR);
35369+ if (!err)
35370+ au_fhsm_wrote(a->sb, bindex, /*force*/0);
35371+
35372+out:
35373+ if (wbr)
35374+ atomic_dec(&wbr->wbr_wh_running);
35375+ au_lcnt_dec(&a->br->br_count);
35376+ si_write_unlock(a->sb);
35377+ au_nwt_done(&au_sbi(a->sb)->si_nowait);
35378+ au_kfree_rcu(a);
35379+ if (unlikely(err))
35380+ AuIOErr("err %d\n", err);
35381+}
35382+
35383+static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
35384+{
35385+ int do_dec, wkq_err;
35386+ struct reinit_br_wh *arg;
35387+
35388+ do_dec = 1;
35389+ if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
35390+ goto out;
35391+
35392+ /* ignore ENOMEM */
35393+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
35394+ if (arg) {
35395+ /*
35396+ * dec(wh_running), kfree(arg) and dec(br_count)
35397+ * in reinit function
35398+ */
35399+ arg->sb = sb;
35400+ arg->br = br;
35401+ au_lcnt_inc(&br->br_count);
35402+ wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
35403+ if (unlikely(wkq_err)) {
35404+ atomic_dec(&br->br_wbr->wbr_wh_running);
35405+ au_lcnt_dec(&br->br_count);
35406+ au_kfree_rcu(arg);
35407+ }
35408+ do_dec = 0;
35409+ }
35410+
35411+out:
35412+ if (do_dec)
35413+ atomic_dec(&br->br_wbr->wbr_wh_running);
35414+}
35415+
35416+/* ---------------------------------------------------------------------- */
35417+
35418+/*
35419+ * create the whiteout @wh.
35420+ */
35421+static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
35422+ struct dentry *wh)
35423+{
35424+ int err;
35425+ struct path h_path = {
35426+ .dentry = wh
35427+ };
35428+ struct au_branch *br;
35429+ struct au_wbr *wbr;
35430+ struct dentry *h_parent;
35431+ struct inode *h_dir, *delegated;
35432+
35433+ h_parent = wh->d_parent; /* dir inode is locked */
35434+ h_dir = d_inode(h_parent);
35435+ IMustLock(h_dir);
35436+
35437+ br = au_sbr(sb, bindex);
35438+ h_path.mnt = au_br_mnt(br);
35439+ wbr = br->br_wbr;
35440+ wbr_wh_read_lock(wbr);
35441+ if (wbr->wbr_whbase) {
35442+ delegated = NULL;
35443+ err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
35444+ if (unlikely(err == -EWOULDBLOCK)) {
35445+ pr_warn("cannot retry for NFSv4 delegation"
35446+ " for an internal link\n");
35447+ iput(delegated);
35448+ }
35449+ if (!err || err != -EMLINK)
35450+ goto out;
35451+
35452+ /* link count full. re-initialize br_whbase. */
35453+ kick_reinit_br_wh(sb, br);
35454+ }
35455+
35456+ /* return this error in this context */
35457+ err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
35458+ if (!err)
35459+ au_fhsm_wrote(sb, bindex, /*force*/0);
35460+
35461+out:
35462+ wbr_wh_read_unlock(wbr);
35463+ return err;
35464+}
35465+
35466+/* ---------------------------------------------------------------------- */
35467+
35468+/*
35469+ * create or remove the diropq.
35470+ */
35471+static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
35472+ unsigned int flags)
35473+{
35474+ struct dentry *opq_dentry, *h_dentry;
35475+ struct super_block *sb;
35476+ struct au_branch *br;
35477+ int err;
35478+
35479+ sb = dentry->d_sb;
35480+ br = au_sbr(sb, bindex);
35481+ h_dentry = au_h_dptr(dentry, bindex);
35482+ opq_dentry = vfsub_lkup_one(&diropq_name, h_dentry);
35483+ if (IS_ERR(opq_dentry))
35484+ goto out;
35485+
35486+ if (au_ftest_diropq(flags, CREATE)) {
35487+ err = link_or_create_wh(sb, bindex, opq_dentry);
35488+ if (!err) {
35489+ au_set_dbdiropq(dentry, bindex);
35490+ goto out; /* success */
35491+ }
35492+ } else {
35493+ struct path tmp = {
35494+ .dentry = opq_dentry,
35495+ .mnt = au_br_mnt(br)
35496+ };
35497+ err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &tmp);
35498+ if (!err)
35499+ au_set_dbdiropq(dentry, -1);
35500+ }
35501+ dput(opq_dentry);
35502+ opq_dentry = ERR_PTR(err);
35503+
35504+out:
35505+ return opq_dentry;
35506+}
35507+
35508+struct do_diropq_args {
35509+ struct dentry **errp;
35510+ struct dentry *dentry;
35511+ aufs_bindex_t bindex;
35512+ unsigned int flags;
35513+};
35514+
35515+static void call_do_diropq(void *args)
35516+{
35517+ struct do_diropq_args *a = args;
35518+ *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
35519+}
35520+
35521+struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35522+ unsigned int flags)
35523+{
35524+ struct dentry *diropq, *h_dentry;
35525+ struct user_namespace *h_userns;
35526+
35527+ h_userns = au_sbr_userns(dentry->d_sb, bindex);
35528+ h_dentry = au_h_dptr(dentry, bindex);
35529+ if (!au_test_h_perm_sio(h_userns, d_inode(h_dentry),
35530+ MAY_EXEC | MAY_WRITE))
35531+ diropq = do_diropq(dentry, bindex, flags);
35532+ else {
35533+ int wkq_err;
35534+ struct do_diropq_args args = {
35535+ .errp = &diropq,
35536+ .dentry = dentry,
35537+ .bindex = bindex,
35538+ .flags = flags
35539+ };
35540+
35541+ wkq_err = au_wkq_wait(call_do_diropq, &args);
35542+ if (unlikely(wkq_err))
35543+ diropq = ERR_PTR(wkq_err);
35544+ }
35545+
35546+ return diropq;
35547+}
35548+
35549+/* ---------------------------------------------------------------------- */
35550+
35551+/*
35552+ * lookup whiteout dentry.
35553+ * @h_parent: lower parent dentry which must exist and be locked
35554+ * @base_name: name of dentry which will be whiteouted
35555+ * returns dentry for whiteout.
35556+ */
35557+struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35558+ struct au_branch *br)
35559+{
35560+ int err;
35561+ struct qstr wh_name;
35562+ struct dentry *wh_dentry;
35563+
35564+ err = au_wh_name_alloc(&wh_name, base_name);
35565+ wh_dentry = ERR_PTR(err);
35566+ if (!err) {
35567+ wh_dentry = vfsub_lkup_one(&wh_name, h_parent);
35568+ au_kfree_try_rcu(wh_name.name);
35569+ }
35570+ return wh_dentry;
35571+}
35572+
35573+/*
35574+ * link/create a whiteout for @dentry on @bindex.
35575+ */
35576+struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35577+ struct dentry *h_parent)
35578+{
35579+ struct dentry *wh_dentry;
35580+ struct super_block *sb;
35581+ int err;
35582+
35583+ sb = dentry->d_sb;
35584+ wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
35585+ if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
35586+ err = link_or_create_wh(sb, bindex, wh_dentry);
35587+ if (!err) {
35588+ au_set_dbwh(dentry, bindex);
35589+ au_fhsm_wrote(sb, bindex, /*force*/0);
35590+ } else {
35591+ dput(wh_dentry);
35592+ wh_dentry = ERR_PTR(err);
35593+ }
35594+ }
35595+
35596+ return wh_dentry;
35597+}
35598+
35599+/* ---------------------------------------------------------------------- */
35600+
35601+/* Delete all whiteouts in this directory on branch bindex. */
35602+static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
35603+ aufs_bindex_t bindex, struct au_branch *br)
35604+{
35605+ int err;
35606+ unsigned long ul, n;
35607+ struct qstr wh_name;
35608+ char *p;
35609+ struct hlist_head *head;
35610+ struct au_vdir_wh *pos;
35611+ struct au_vdir_destr *str;
35612+
35613+ err = -ENOMEM;
35614+ p = (void *)__get_free_page(GFP_NOFS);
35615+ wh_name.name = p;
35616+ if (unlikely(!wh_name.name))
35617+ goto out;
35618+
35619+ err = 0;
35620+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35621+ p += AUFS_WH_PFX_LEN;
35622+ n = whlist->nh_num;
35623+ head = whlist->nh_head;
35624+ for (ul = 0; !err && ul < n; ul++, head++) {
35625+ hlist_for_each_entry(pos, head, wh_hash) {
35626+ if (pos->wh_bindex != bindex)
35627+ continue;
35628+
35629+ str = &pos->wh_str;
35630+ if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
35631+ memcpy(p, str->name, str->len);
35632+ wh_name.len = AUFS_WH_PFX_LEN + str->len;
35633+ err = unlink_wh_name(h_dentry, &wh_name, br);
35634+ if (!err)
35635+ continue;
35636+ break;
35637+ }
35638+ AuIOErr("whiteout name too long %.*s\n",
35639+ str->len, str->name);
35640+ err = -EIO;
35641+ break;
35642+ }
35643+ }
35644+ free_page((unsigned long)wh_name.name);
35645+
35646+out:
35647+ return err;
35648+}
35649+
35650+struct del_wh_children_args {
35651+ int *errp;
35652+ struct dentry *h_dentry;
35653+ struct au_nhash *whlist;
35654+ aufs_bindex_t bindex;
35655+ struct au_branch *br;
35656+};
35657+
35658+static void call_del_wh_children(void *args)
35659+{
35660+ struct del_wh_children_args *a = args;
35661+ *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
35662+}
35663+
35664+/* ---------------------------------------------------------------------- */
35665+
35666+struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
35667+{
35668+ struct au_whtmp_rmdir *whtmp;
35669+ int err;
35670+ unsigned int rdhash;
35671+
35672+ SiMustAnyLock(sb);
35673+
35674+ whtmp = kzalloc(sizeof(*whtmp), gfp);
35675+ if (unlikely(!whtmp)) {
35676+ whtmp = ERR_PTR(-ENOMEM);
35677+ goto out;
35678+ }
35679+
35680+ /* no estimation for dir size */
35681+ rdhash = au_sbi(sb)->si_rdhash;
35682+ if (!rdhash)
35683+ rdhash = AUFS_RDHASH_DEF;
35684+ err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
35685+ if (unlikely(err)) {
35686+ au_kfree_rcu(whtmp);
35687+ whtmp = ERR_PTR(err);
35688+ }
35689+
35690+out:
35691+ return whtmp;
35692+}
35693+
35694+void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
35695+{
35696+ if (whtmp->br)
35697+ au_lcnt_dec(&whtmp->br->br_count);
35698+ dput(whtmp->wh_dentry);
35699+ iput(whtmp->dir);
35700+ au_nhash_wh_free(&whtmp->whlist);
35701+ au_kfree_rcu(whtmp);
35702+}
35703+
35704+/*
35705+ * rmdir the whiteouted temporary named dir @h_dentry.
35706+ * @whlist: whiteouted children.
35707+ */
35708+int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35709+ struct dentry *wh_dentry, struct au_nhash *whlist)
35710+{
35711+ int err;
35712+ unsigned int h_nlink;
35713+ struct path h_tmp;
35714+ struct inode *wh_inode, *h_dir;
35715+ struct au_branch *br;
35716+ struct user_namespace *h_userns;
35717+
35718+ h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
35719+ IMustLock(h_dir);
35720+
35721+ br = au_sbr(dir->i_sb, bindex);
35722+ h_userns = au_br_userns(br);
35723+ wh_inode = d_inode(wh_dentry);
35724+ inode_lock_nested(wh_inode, AuLsc_I_CHILD);
35725+
35726+ /*
35727+ * someone else might change some whiteouts while we were sleeping.
35728+ * it means this whlist may have an obsoleted entry.
35729+ */
35730+ if (!au_test_h_perm_sio(h_userns, wh_inode, MAY_EXEC | MAY_WRITE))
35731+ err = del_wh_children(wh_dentry, whlist, bindex, br);
35732+ else {
35733+ int wkq_err;
35734+ struct del_wh_children_args args = {
35735+ .errp = &err,
35736+ .h_dentry = wh_dentry,
35737+ .whlist = whlist,
35738+ .bindex = bindex,
35739+ .br = br
35740+ };
35741+
35742+ wkq_err = au_wkq_wait(call_del_wh_children, &args);
35743+ if (unlikely(wkq_err))
35744+ err = wkq_err;
35745+ }
35746+ inode_unlock(wh_inode);
35747+
35748+ if (!err) {
35749+ h_tmp.dentry = wh_dentry;
35750+ h_tmp.mnt = au_br_mnt(br);
35751+ h_nlink = h_dir->i_nlink;
35752+ err = vfsub_rmdir(h_dir, &h_tmp);
35753+ /* some fs doesn't change the parent nlink in some cases */
35754+ h_nlink -= h_dir->i_nlink;
35755+ }
35756+
35757+ if (!err) {
35758+ if (au_ibtop(dir) == bindex) {
35759+ /* todo: dir->i_mutex is necessary */
35760+ au_cpup_attr_timesizes(dir);
35761+ if (h_nlink)
35762+ vfsub_drop_nlink(dir);
35763+ }
35764+ return 0; /* success */
35765+ }
35766+
35767+ pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
35768+ return err;
35769+}
35770+
35771+static void call_rmdir_whtmp(void *args)
35772+{
35773+ int err;
35774+ aufs_bindex_t bindex;
35775+ struct au_whtmp_rmdir *a = args;
35776+ struct super_block *sb;
35777+ struct dentry *h_parent;
35778+ struct inode *h_dir;
35779+ struct au_hinode *hdir;
35780+
35781+ /* rmdir by nfsd may cause deadlock with this i_mutex */
35782+ /* inode_lock(a->dir); */
35783+ err = -EROFS;
35784+ sb = a->dir->i_sb;
35785+ si_read_lock(sb, !AuLock_FLUSH);
35786+ if (!au_br_writable(a->br->br_perm))
35787+ goto out;
35788+ bindex = au_br_index(sb, a->br->br_id);
35789+ if (unlikely(bindex < 0))
35790+ goto out;
35791+
35792+ err = -EIO;
35793+ ii_write_lock_parent(a->dir);
35794+ h_parent = dget_parent(a->wh_dentry);
35795+ h_dir = d_inode(h_parent);
35796+ hdir = au_hi(a->dir, bindex);
35797+ err = vfsub_mnt_want_write(au_br_mnt(a->br));
35798+ if (unlikely(err))
35799+ goto out_mnt;
35800+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
35801+ err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
35802+ a->br);
35803+ if (!err)
35804+ err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
35805+ au_hn_inode_unlock(hdir);
35806+ vfsub_mnt_drop_write(au_br_mnt(a->br));
35807+
35808+out_mnt:
35809+ dput(h_parent);
35810+ ii_write_unlock(a->dir);
35811+out:
35812+ /* inode_unlock(a->dir); */
35813+ au_whtmp_rmdir_free(a);
35814+ si_read_unlock(sb);
35815+ au_nwt_done(&au_sbi(sb)->si_nowait);
35816+ if (unlikely(err))
35817+ AuIOErr("err %d\n", err);
35818+}
35819+
35820+void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35821+ struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
35822+{
35823+ int wkq_err;
35824+ struct super_block *sb;
35825+
35826+ IMustLock(dir);
35827+
35828+ /* all post-process will be done in do_rmdir_whtmp(). */
35829+ sb = dir->i_sb;
35830+ args->dir = au_igrab(dir);
35831+ args->br = au_sbr(sb, bindex);
35832+ au_lcnt_inc(&args->br->br_count);
35833+ args->wh_dentry = dget(wh_dentry);
35834+ wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
35835+ if (unlikely(wkq_err)) {
35836+ pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
35837+ au_whtmp_rmdir_free(args);
35838+ }
35839+}
35840diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
35841--- /usr/share/empty/fs/aufs/whout.h 1970-01-01 01:00:00.000000000 +0100
35842+++ linux/fs/aufs/whout.h 2021-06-30 21:35:11.397206648 +0200
35843@@ -0,0 +1,87 @@
35844+/* SPDX-License-Identifier: GPL-2.0 */
35845+/*
35846+ * Copyright (C) 2005-2020 Junjiro R. Okajima
35847+ *
35848+ * This program, aufs is free software; you can redistribute it and/or modify
35849+ * it under the terms of the GNU General Public License as published by
35850+ * the Free Software Foundation; either version 2 of the License, or
35851+ * (at your option) any later version.
35852+ *
35853+ * This program is distributed in the hope that it will be useful,
35854+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
35855+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35856+ * GNU General Public License for more details.
35857+ *
35858+ * You should have received a copy of the GNU General Public License
35859+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
35860+ */
35861+
35862+/*
35863+ * whiteout for logical deletion and opaque directory
35864+ */
35865+
35866+#ifndef __AUFS_WHOUT_H__
35867+#define __AUFS_WHOUT_H__
35868+
35869+#ifdef __KERNEL__
35870+
35871+#include "dir.h"
35872+
35873+/* whout.c */
35874+int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
35875+int au_wh_test(struct user_namespace *h_userns, struct dentry *h_parent,
35876+ struct qstr *wh_name, int try_sio);
35877+int au_diropq_test(struct user_namespace *h_userns, struct dentry *h_dentry);
35878+struct au_branch;
35879+struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
35880+ struct qstr *prefix);
35881+int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
35882+int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35883+ struct dentry *dentry);
35884+int au_wh_init(struct au_branch *br, struct super_block *sb);
35885+
35886+/* diropq flags */
35887+#define AuDiropq_CREATE 1
35888+#define au_ftest_diropq(flags, name) ((flags) & AuDiropq_##name)
35889+#define au_fset_diropq(flags, name) \
35890+ do { (flags) |= AuDiropq_##name; } while (0)
35891+#define au_fclr_diropq(flags, name) \
35892+ do { (flags) &= ~AuDiropq_##name; } while (0)
35893+
35894+struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35895+ unsigned int flags);
35896+struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35897+ struct au_branch *br);
35898+struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35899+ struct dentry *h_parent);
35900+
35901+/* real rmdir for the whiteout-ed dir */
35902+struct au_whtmp_rmdir {
35903+ struct inode *dir;
35904+ struct au_branch *br;
35905+ struct dentry *wh_dentry;
35906+ struct au_nhash whlist;
35907+};
35908+
35909+struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
35910+void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
35911+int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35912+ struct dentry *wh_dentry, struct au_nhash *whlist);
35913+void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35914+ struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
35915+
35916+/* ---------------------------------------------------------------------- */
35917+
35918+static inline struct dentry *au_diropq_create(struct dentry *dentry,
35919+ aufs_bindex_t bindex)
35920+{
35921+ return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
35922+}
35923+
35924+static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
35925+{
35926+ return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
35927+}
35928+
35929+#endif /* __KERNEL__ */
35930+#endif /* __AUFS_WHOUT_H__ */
35931diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
35932--- /usr/share/empty/fs/aufs/wkq.c 1970-01-01 01:00:00.000000000 +0100
35933+++ linux/fs/aufs/wkq.c 2021-02-24 13:33:42.751013936 +0100
35934@@ -0,0 +1,372 @@
35935+// SPDX-License-Identifier: GPL-2.0
35936+/*
35937+ * Copyright (C) 2005-2020 Junjiro R. Okajima
35938+ *
35939+ * This program, aufs is free software; you can redistribute it and/or modify
35940+ * it under the terms of the GNU General Public License as published by
35941+ * the Free Software Foundation; either version 2 of the License, or
35942+ * (at your option) any later version.
35943+ *
35944+ * This program is distributed in the hope that it will be useful,
35945+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
35946+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35947+ * GNU General Public License for more details.
35948+ *
35949+ * You should have received a copy of the GNU General Public License
35950+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
35951+ */
35952+
35953+/*
35954+ * workqueue for asynchronous/super-io operations
35955+ * todo: try new credential scheme
35956+ */
35957+
35958+#include <linux/module.h>
35959+#include "aufs.h"
35960+
35961+/* internal workqueue named AUFS_WKQ_NAME */
35962+
35963+static struct workqueue_struct *au_wkq;
35964+
35965+struct au_wkinfo {
35966+ struct work_struct wk;
35967+ struct kobject *kobj;
35968+
35969+ unsigned int flags; /* see wkq.h */
35970+
35971+ au_wkq_func_t func;
35972+ void *args;
35973+
35974+#ifdef CONFIG_LOCKDEP
35975+ int dont_check;
35976+ struct held_lock **hlock;
35977+#endif
35978+
35979+ struct completion *comp;
35980+};
35981+
35982+/* ---------------------------------------------------------------------- */
35983+/*
35984+ * Aufs passes some operations to the workqueue such as the internal copyup.
35985+ * This scheme looks rather unnatural for LOCKDEP debugging feature, since the
35986+ * job run by workqueue depends upon the locks acquired in the other task.
35987+ * Delegating a small operation to the workqueue, aufs passes its lockdep
35988+ * information too. And the job in the workqueue restores the info in order to
35989+ * pretend as if it acquired those locks. This is just to make LOCKDEP work
35990+ * correctly and expectedly.
35991+ */
35992+
35993+#ifndef CONFIG_LOCKDEP
35994+AuStubInt0(au_wkq_lockdep_alloc, struct au_wkinfo *wkinfo);
35995+AuStubVoid(au_wkq_lockdep_free, struct au_wkinfo *wkinfo);
35996+AuStubVoid(au_wkq_lockdep_pre, struct au_wkinfo *wkinfo);
35997+AuStubVoid(au_wkq_lockdep_post, struct au_wkinfo *wkinfo);
35998+AuStubVoid(au_wkq_lockdep_init, struct au_wkinfo *wkinfo);
35999+#else
36000+static void au_wkq_lockdep_init(struct au_wkinfo *wkinfo)
36001+{
36002+ wkinfo->hlock = NULL;
36003+ wkinfo->dont_check = 0;
36004+}
36005+
36006+/*
36007+ * 1: matched
36008+ * 0: unmatched
36009+ */
36010+static int au_wkq_lockdep_test(struct lock_class_key *key, const char *name)
36011+{
36012+ static DEFINE_SPINLOCK(spin);
36013+ static struct {
36014+ char *name;
36015+ struct lock_class_key *key;
36016+ } a[] = {
36017+ { .name = "&sbinfo->si_rwsem" },
36018+ { .name = "&finfo->fi_rwsem" },
36019+ { .name = "&dinfo->di_rwsem" },
36020+ { .name = "&iinfo->ii_rwsem" }
36021+ };
36022+ static int set;
36023+ int i;
36024+
36025+ /* lockless read from 'set.' see below */
36026+ if (set == ARRAY_SIZE(a)) {
36027+ for (i = 0; i < ARRAY_SIZE(a); i++)
36028+ if (a[i].key == key)
36029+ goto match;
36030+ goto unmatch;
36031+ }
36032+
36033+ spin_lock(&spin);
36034+ if (set)
36035+ for (i = 0; i < ARRAY_SIZE(a); i++)
36036+ if (a[i].key == key) {
36037+ spin_unlock(&spin);
36038+ goto match;
36039+ }
36040+ for (i = 0; i < ARRAY_SIZE(a); i++) {
36041+ if (a[i].key) {
36042+ if (unlikely(a[i].key == key)) { /* rare but possible */
36043+ spin_unlock(&spin);
36044+ goto match;
36045+ } else
36046+ continue;
36047+ }
36048+ if (strstr(a[i].name, name)) {
36049+ /*
36050+ * the order of these three lines is important for the
36051+ * lockless read above.
36052+ */
36053+ a[i].key = key;
36054+ spin_unlock(&spin);
36055+ set++;
36056+ /* AuDbg("%d, %s\n", set, name); */
36057+ goto match;
36058+ }
36059+ }
36060+ spin_unlock(&spin);
36061+ goto unmatch;
36062+
36063+match:
36064+ return 1;
36065+unmatch:
36066+ return 0;
36067+}
36068+
36069+static int au_wkq_lockdep_alloc(struct au_wkinfo *wkinfo)
36070+{
36071+ int err, n;
36072+ struct task_struct *curr;
36073+ struct held_lock **hl, *held_locks, *p;
36074+
36075+ err = 0;
36076+ curr = current;
36077+ wkinfo->dont_check = lockdep_recursing(curr);
36078+ if (wkinfo->dont_check)
36079+ goto out;
36080+ n = curr->lockdep_depth;
36081+ if (!n)
36082+ goto out;
36083+
36084+ err = -ENOMEM;
36085+ wkinfo->hlock = kmalloc_array(n + 1, sizeof(*wkinfo->hlock), GFP_NOFS);
36086+ if (unlikely(!wkinfo->hlock))
36087+ goto out;
36088+
36089+ err = 0;
36090+#if 0 /* left for debugging */
36091+ if (0 && au_debug_test())
36092+ lockdep_print_held_locks(curr);
36093+#endif
36094+ held_locks = curr->held_locks;
36095+ hl = wkinfo->hlock;
36096+ while (n--) {
36097+ p = held_locks++;
36098+ if (au_wkq_lockdep_test(p->instance->key, p->instance->name))
36099+ *hl++ = p;
36100+ }
36101+ *hl = NULL;
36102+
36103+out:
36104+ return err;
36105+}
36106+
36107+static void au_wkq_lockdep_free(struct au_wkinfo *wkinfo)
36108+{
36109+ au_kfree_try_rcu(wkinfo->hlock);
36110+}
36111+
36112+static void au_wkq_lockdep_pre(struct au_wkinfo *wkinfo)
36113+{
36114+ struct held_lock *p, **hl = wkinfo->hlock;
36115+ int subclass;
36116+
36117+ if (wkinfo->dont_check)
36118+ lockdep_off();
36119+ if (!hl)
36120+ return;
36121+ while ((p = *hl++)) { /* assignment */
36122+ subclass = lockdep_hlock_class(p)->subclass;
36123+ /* AuDbg("%s, %d\n", p->instance->name, subclass); */
36124+ if (p->read)
36125+ rwsem_acquire_read(p->instance, subclass, 0,
36126+ /*p->acquire_ip*/_RET_IP_);
36127+ else
36128+ rwsem_acquire(p->instance, subclass, 0,
36129+ /*p->acquire_ip*/_RET_IP_);
36130+ }
36131+}
36132+
36133+static void au_wkq_lockdep_post(struct au_wkinfo *wkinfo)
36134+{
36135+ struct held_lock *p, **hl = wkinfo->hlock;
36136+
36137+ if (wkinfo->dont_check)
36138+ lockdep_on();
36139+ if (!hl)
36140+ return;
36141+ while ((p = *hl++)) /* assignment */
36142+ rwsem_release(p->instance, /*p->acquire_ip*/_RET_IP_);
36143+}
36144+#endif
36145+
36146+static void wkq_func(struct work_struct *wk)
36147+{
36148+ struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
36149+
36150+ AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
36151+ AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
36152+
36153+ au_wkq_lockdep_pre(wkinfo);
36154+ wkinfo->func(wkinfo->args);
36155+ au_wkq_lockdep_post(wkinfo);
36156+ if (au_ftest_wkq(wkinfo->flags, WAIT))
36157+ complete(wkinfo->comp);
36158+ else {
36159+ kobject_put(wkinfo->kobj);
36160+ module_put(THIS_MODULE); /* todo: ?? */
36161+ au_kfree_rcu(wkinfo);
36162+ }
36163+}
36164+
36165+/*
36166+ * Since struct completion is large, try allocating it dynamically.
36167+ */
36168+#define AuWkqCompDeclare(name) struct completion *comp = NULL
36169+
36170+static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
36171+{
36172+ *comp = kmalloc(sizeof(**comp), GFP_NOFS);
36173+ if (*comp) {
36174+ init_completion(*comp);
36175+ wkinfo->comp = *comp;
36176+ return 0;
36177+ }
36178+ return -ENOMEM;
36179+}
36180+
36181+static void au_wkq_comp_free(struct completion *comp)
36182+{
36183+ au_kfree_rcu(comp);
36184+}
36185+
36186+static void au_wkq_run(struct au_wkinfo *wkinfo)
36187+{
36188+ if (au_ftest_wkq(wkinfo->flags, NEST)) {
36189+ if (au_wkq_test()) {
36190+ AuWarn1("wkq from wkq, unless silly-rename on NFS,"
36191+ " due to a dead dir by UDBA,"
36192+ " or async xino write?\n");
36193+ AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
36194+ }
36195+ } else
36196+ au_dbg_verify_kthread();
36197+
36198+ if (au_ftest_wkq(wkinfo->flags, WAIT)) {
36199+ INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
36200+ queue_work(au_wkq, &wkinfo->wk);
36201+ } else {
36202+ INIT_WORK(&wkinfo->wk, wkq_func);
36203+ schedule_work(&wkinfo->wk);
36204+ }
36205+}
36206+
36207+/*
36208+ * Be careful. It is easy to make deadlock happen.
36209+ * processA: lock, wkq and wait
36210+ * processB: wkq and wait, lock in wkq
36211+ * --> deadlock
36212+ */
36213+int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
36214+{
36215+ int err;
36216+ AuWkqCompDeclare(comp);
36217+ struct au_wkinfo wkinfo = {
36218+ .flags = flags,
36219+ .func = func,
36220+ .args = args
36221+ };
36222+
36223+ err = au_wkq_comp_alloc(&wkinfo, &comp);
36224+ if (unlikely(err))
36225+ goto out;
36226+ err = au_wkq_lockdep_alloc(&wkinfo);
36227+ if (unlikely(err))
36228+ goto out_comp;
36229+ if (!err) {
36230+ au_wkq_run(&wkinfo);
36231+ /* no timeout, no interrupt */
36232+ wait_for_completion(wkinfo.comp);
36233+ }
36234+ au_wkq_lockdep_free(&wkinfo);
36235+
36236+out_comp:
36237+ au_wkq_comp_free(comp);
36238+out:
36239+ destroy_work_on_stack(&wkinfo.wk);
36240+ return err;
36241+}
36242+
36243+/*
36244+ * Note: dget/dput() in func for aufs dentries are not supported. It will be a
36245+ * problem in a concurrent umounting.
36246+ */
36247+int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36248+ unsigned int flags)
36249+{
36250+ int err;
36251+ struct au_wkinfo *wkinfo;
36252+
36253+ atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
36254+
36255+ /*
36256+ * wkq_func() must free this wkinfo.
36257+ * it highly depends upon the implementation of workqueue.
36258+ */
36259+ err = 0;
36260+ wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
36261+ if (wkinfo) {
36262+ wkinfo->kobj = &au_sbi(sb)->si_kobj;
36263+ wkinfo->flags = flags & ~AuWkq_WAIT;
36264+ wkinfo->func = func;
36265+ wkinfo->args = args;
36266+ wkinfo->comp = NULL;
36267+ au_wkq_lockdep_init(wkinfo);
36268+ kobject_get(wkinfo->kobj);
36269+ __module_get(THIS_MODULE); /* todo: ?? */
36270+
36271+ au_wkq_run(wkinfo);
36272+ } else {
36273+ err = -ENOMEM;
36274+ au_nwt_done(&au_sbi(sb)->si_nowait);
36275+ }
36276+
36277+ return err;
36278+}
36279+
36280+/* ---------------------------------------------------------------------- */
36281+
36282+void au_nwt_init(struct au_nowait_tasks *nwt)
36283+{
36284+ atomic_set(&nwt->nw_len, 0);
36285+ /* smp_mb(); */ /* atomic_set */
36286+ init_waitqueue_head(&nwt->nw_wq);
36287+}
36288+
36289+void au_wkq_fin(void)
36290+{
36291+ destroy_workqueue(au_wkq);
36292+}
36293+
36294+int __init au_wkq_init(void)
36295+{
36296+ int err;
36297+
36298+ err = 0;
36299+ au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
36300+ if (IS_ERR(au_wkq))
36301+ err = PTR_ERR(au_wkq);
36302+ else if (!au_wkq)
36303+ err = -ENOMEM;
36304+
36305+ return err;
36306+}
36307diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
36308--- /usr/share/empty/fs/aufs/wkq.h 1970-01-01 01:00:00.000000000 +0100
36309+++ linux/fs/aufs/wkq.h 2021-02-24 13:33:42.751013936 +0100
36310@@ -0,0 +1,89 @@
36311+/* SPDX-License-Identifier: GPL-2.0 */
36312+/*
36313+ * Copyright (C) 2005-2020 Junjiro R. Okajima
36314+ *
36315+ * This program, aufs is free software; you can redistribute it and/or modify
36316+ * it under the terms of the GNU General Public License as published by
36317+ * the Free Software Foundation; either version 2 of the License, or
36318+ * (at your option) any later version.
36319+ *
36320+ * This program is distributed in the hope that it will be useful,
36321+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36322+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36323+ * GNU General Public License for more details.
36324+ *
36325+ * You should have received a copy of the GNU General Public License
36326+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
36327+ */
36328+
36329+/*
36330+ * workqueue for asynchronous/super-io operations
36331+ * todo: try new credentials management scheme
36332+ */
36333+
36334+#ifndef __AUFS_WKQ_H__
36335+#define __AUFS_WKQ_H__
36336+
36337+#ifdef __KERNEL__
36338+
36339+#include <linux/wait.h>
36340+
36341+struct super_block;
36342+
36343+/* ---------------------------------------------------------------------- */
36344+
36345+/*
36346+ * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
36347+ */
36348+struct au_nowait_tasks {
36349+ atomic_t nw_len;
36350+ wait_queue_head_t nw_wq;
36351+};
36352+
36353+/* ---------------------------------------------------------------------- */
36354+
36355+typedef void (*au_wkq_func_t)(void *args);
36356+
36357+/* wkq flags */
36358+#define AuWkq_WAIT 1
36359+#define AuWkq_NEST (1 << 1)
36360+#define au_ftest_wkq(flags, name) ((flags) & AuWkq_##name)
36361+#define au_fset_wkq(flags, name) \
36362+ do { (flags) |= AuWkq_##name; } while (0)
36363+#define au_fclr_wkq(flags, name) \
36364+ do { (flags) &= ~AuWkq_##name; } while (0)
36365+
36366+/* wkq.c */
36367+int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
36368+int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36369+ unsigned int flags);
36370+void au_nwt_init(struct au_nowait_tasks *nwt);
36371+int __init au_wkq_init(void);
36372+void au_wkq_fin(void);
36373+
36374+/* ---------------------------------------------------------------------- */
36375+
36376+static inline int au_wkq_test(void)
36377+{
36378+ return current->flags & PF_WQ_WORKER;
36379+}
36380+
36381+static inline int au_wkq_wait(au_wkq_func_t func, void *args)
36382+{
36383+ return au_wkq_do_wait(AuWkq_WAIT, func, args);
36384+}
36385+
36386+static inline void au_nwt_done(struct au_nowait_tasks *nwt)
36387+{
36388+ if (atomic_dec_and_test(&nwt->nw_len))
36389+ wake_up_all(&nwt->nw_wq);
36390+}
36391+
36392+static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
36393+{
36394+ wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
36395+ return 0;
36396+}
36397+
36398+#endif /* __KERNEL__ */
36399+#endif /* __AUFS_WKQ_H__ */
36400diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
36401--- /usr/share/empty/fs/aufs/xattr.c 1970-01-01 01:00:00.000000000 +0100
36402+++ linux/fs/aufs/xattr.c 2021-06-30 21:35:11.397206648 +0200
36403@@ -0,0 +1,368 @@
36404+// SPDX-License-Identifier: GPL-2.0
36405+/*
36406+ * Copyright (C) 2014-2020 Junjiro R. Okajima
36407+ *
36408+ * This program, aufs is free software; you can redistribute it and/or modify
36409+ * it under the terms of the GNU General Public License as published by
36410+ * the Free Software Foundation; either version 2 of the License, or
36411+ * (at your option) any later version.
36412+ *
36413+ * This program is distributed in the hope that it will be useful,
36414+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36415+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36416+ * GNU General Public License for more details.
36417+ *
36418+ * You should have received a copy of the GNU General Public License
36419+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
36420+ */
36421+
36422+/*
36423+ * handling xattr functions
36424+ */
36425+
36426+#include <linux/fs.h>
36427+#include <linux/posix_acl_xattr.h>
36428+#include <linux/xattr.h>
36429+#include "aufs.h"
36430+
36431+static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
36432+{
36433+ if (!ignore_flags)
36434+ goto out;
36435+ switch (err) {
36436+ case -ENOMEM:
36437+ case -EDQUOT:
36438+ goto out;
36439+ }
36440+
36441+ if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
36442+ err = 0;
36443+ goto out;
36444+ }
36445+
36446+#define cmp(brattr, prefix) do { \
36447+ if (!strncmp(name, XATTR_##prefix##_PREFIX, \
36448+ XATTR_##prefix##_PREFIX_LEN)) { \
36449+ if (ignore_flags & AuBrAttr_ICEX_##brattr) \
36450+ err = 0; \
36451+ goto out; \
36452+ } \
36453+ } while (0)
36454+
36455+ cmp(SEC, SECURITY);
36456+ cmp(SYS, SYSTEM);
36457+ cmp(TR, TRUSTED);
36458+ cmp(USR, USER);
36459+#undef cmp
36460+
36461+ if (ignore_flags & AuBrAttr_ICEX_OTH)
36462+ err = 0;
36463+
36464+out:
36465+ return err;
36466+}
36467+
36468+static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
36469+
36470+static int au_do_cpup_xattr(struct path *h_dst, struct path *h_src,
36471+ char *name, char **buf, unsigned int ignore_flags,
36472+ unsigned int verbose)
36473+{
36474+ int err;
36475+ ssize_t ssz;
36476+ struct inode *h_idst;
36477+ struct dentry *h_dst_dentry, *h_src_dentry;
36478+ struct user_namespace *h_dst_userns, *h_src_userns;
36479+
36480+ h_src_userns = mnt_user_ns(h_src->mnt);
36481+ h_src_dentry = h_src->dentry;
36482+ ssz = vfs_getxattr_alloc(h_src_userns, h_src_dentry, name, buf, 0,
36483+ GFP_NOFS);
36484+ err = ssz;
36485+ if (unlikely(err <= 0)) {
36486+ if (err == -ENODATA
36487+ || (err == -EOPNOTSUPP
36488+ && ((ignore_flags & au_xattr_out_of_list)
36489+ || (au_test_nfs_noacl(d_inode(h_src_dentry))
36490+ && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
36491+ || !strcmp(name,
36492+ XATTR_NAME_POSIX_ACL_DEFAULT))))
36493+ ))
36494+ err = 0;
36495+ if (err && (verbose || au_debug_test()))
36496+ pr_err("%s, err %d\n", name, err);
36497+ goto out;
36498+ }
36499+
36500+ /* unlock it temporary */
36501+ h_dst_userns = mnt_user_ns(h_dst->mnt);
36502+ h_dst_dentry = h_dst->dentry;
36503+ h_idst = d_inode(h_dst_dentry);
36504+ inode_unlock(h_idst);
36505+ err = vfsub_setxattr(h_dst_userns, h_dst_dentry, name, *buf, ssz,
36506+ /*flags*/0);
36507+ inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36508+ if (unlikely(err)) {
36509+ if (verbose || au_debug_test())
36510+ pr_err("%s, err %d\n", name, err);
36511+ err = au_xattr_ignore(err, name, ignore_flags);
36512+ }
36513+
36514+out:
36515+ return err;
36516+}
36517+
36518+int au_cpup_xattr(struct path *h_dst, struct path *h_src, int ignore_flags,
36519+ unsigned int verbose)
36520+{
36521+ int err, unlocked, acl_access, acl_default;
36522+ ssize_t ssz;
36523+ struct dentry *h_dst_dentry, *h_src_dentry;
36524+ struct inode *h_isrc, *h_idst;
36525+ char *value, *p, *o, *e;
36526+
36527+ /* try stopping to update the source inode while we are referencing */
36528+ /* there should not be the parent-child relationship between them */
36529+ h_dst_dentry = h_dst->dentry;
36530+ h_idst = d_inode(h_dst_dentry);
36531+ h_src_dentry = h_src->dentry;
36532+ h_isrc = d_inode(h_src_dentry);
36533+ inode_unlock(h_idst);
36534+ inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
36535+ inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36536+ unlocked = 0;
36537+
36538+ /* some filesystems don't list POSIX ACL, for example tmpfs */
36539+ ssz = vfs_listxattr(h_src_dentry, NULL, 0);
36540+ err = ssz;
36541+ if (unlikely(err < 0)) {
36542+ AuTraceErr(err);
36543+ if (err == -ENODATA
36544+ || err == -EOPNOTSUPP)
36545+ err = 0; /* ignore */
36546+ goto out;
36547+ }
36548+
36549+ err = 0;
36550+ p = NULL;
36551+ o = NULL;
36552+ if (ssz) {
36553+ err = -ENOMEM;
36554+ p = kmalloc(ssz, GFP_NOFS);
36555+ o = p;
36556+ if (unlikely(!p))
36557+ goto out;
36558+ err = vfs_listxattr(h_src_dentry, p, ssz);
36559+ }
36560+ inode_unlock_shared(h_isrc);
36561+ unlocked = 1;
36562+ AuDbg("err %d, ssz %zd\n", err, ssz);
36563+ if (unlikely(err < 0))
36564+ goto out_free;
36565+
36566+ err = 0;
36567+ e = p + ssz;
36568+ value = NULL;
36569+ acl_access = 0;
36570+ acl_default = 0;
36571+ while (!err && p < e) {
36572+ acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
36573+ sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
36574+ acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
36575+ sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
36576+ - 1);
36577+ err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
36578+ verbose);
36579+ p += strlen(p) + 1;
36580+ }
36581+ AuTraceErr(err);
36582+ ignore_flags |= au_xattr_out_of_list;
36583+ if (!err && !acl_access) {
36584+ err = au_do_cpup_xattr(h_dst, h_src,
36585+ XATTR_NAME_POSIX_ACL_ACCESS, &value,
36586+ ignore_flags, verbose);
36587+ AuTraceErr(err);
36588+ }
36589+ if (!err && !acl_default) {
36590+ err = au_do_cpup_xattr(h_dst, h_src,
36591+ XATTR_NAME_POSIX_ACL_DEFAULT, &value,
36592+ ignore_flags, verbose);
36593+ AuTraceErr(err);
36594+ }
36595+
36596+ au_kfree_try_rcu(value);
36597+
36598+out_free:
36599+ au_kfree_try_rcu(o);
36600+out:
36601+ if (!unlocked)
36602+ inode_unlock_shared(h_isrc);
36603+ AuTraceErr(err);
36604+ return err;
36605+}
36606+
36607+/* ---------------------------------------------------------------------- */
36608+
36609+static int au_smack_reentering(struct super_block *sb)
36610+{
36611+#if IS_ENABLED(CONFIG_SECURITY_SMACK) || IS_ENABLED(CONFIG_SECURITY_SELINUX)
36612+ /*
36613+ * as a part of lookup, smack_d_instantiate() is called, and it calls
36614+ * i_op->getxattr(). ouch.
36615+ */
36616+ return si_pid_test(sb);
36617+#else
36618+ return 0;
36619+#endif
36620+}
36621+
36622+enum {
36623+ AU_XATTR_LIST,
36624+ AU_XATTR_GET
36625+};
36626+
36627+struct au_lgxattr {
36628+ int type;
36629+ union {
36630+ struct {
36631+ char *list;
36632+ size_t size;
36633+ } list;
36634+ struct {
36635+ const char *name;
36636+ void *value;
36637+ size_t size;
36638+ } get;
36639+ } u;
36640+};
36641+
36642+static ssize_t au_lgxattr(struct dentry *dentry, struct inode *inode,
36643+ struct au_lgxattr *arg)
36644+{
36645+ ssize_t err;
36646+ int reenter;
36647+ struct path h_path;
36648+ struct super_block *sb;
36649+
36650+ sb = dentry->d_sb;
36651+ reenter = au_smack_reentering(sb);
36652+ if (!reenter) {
36653+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
36654+ if (unlikely(err))
36655+ goto out;
36656+ }
36657+ err = au_h_path_getattr(dentry, inode, /*force*/1, &h_path, reenter);
36658+ if (unlikely(err))
36659+ goto out_si;
36660+ if (unlikely(!h_path.dentry))
36661+ /* illegally overlapped or something */
36662+ goto out_di; /* pretending success */
36663+
36664+ /* always topmost entry only */
36665+ switch (arg->type) {
36666+ case AU_XATTR_LIST:
36667+ err = vfs_listxattr(h_path.dentry,
36668+ arg->u.list.list, arg->u.list.size);
36669+ break;
36670+ case AU_XATTR_GET:
36671+ AuDebugOn(d_is_negative(h_path.dentry));
36672+ err = vfs_getxattr(mnt_user_ns(h_path.mnt), h_path.dentry,
36673+ arg->u.get.name, arg->u.get.value,
36674+ arg->u.get.size);
36675+ break;
36676+ }
36677+
36678+out_di:
36679+ if (!reenter)
36680+ di_read_unlock(dentry, AuLock_IR);
36681+out_si:
36682+ if (!reenter)
36683+ si_read_unlock(sb);
36684+out:
36685+ AuTraceErr(err);
36686+ return err;
36687+}
36688+
36689+ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
36690+{
36691+ struct au_lgxattr arg = {
36692+ .type = AU_XATTR_LIST,
36693+ .u.list = {
36694+ .list = list,
36695+ .size = size
36696+ },
36697+ };
36698+
36699+ return au_lgxattr(dentry, /*inode*/NULL, &arg);
36700+}
36701+
36702+static ssize_t au_getxattr(struct dentry *dentry, struct inode *inode,
36703+ const char *name, void *value, size_t size)
36704+{
36705+ struct au_lgxattr arg = {
36706+ .type = AU_XATTR_GET,
36707+ .u.get = {
36708+ .name = name,
36709+ .value = value,
36710+ .size = size
36711+ },
36712+ };
36713+
36714+ return au_lgxattr(dentry, inode, &arg);
36715+}
36716+
36717+static int au_setxattr(struct dentry *dentry, struct inode *inode,
36718+ const char *name, const void *value, size_t size,
36719+ int flags)
36720+{
36721+ struct au_sxattr arg = {
36722+ .type = AU_XATTR_SET,
36723+ .u.set = {
36724+ .name = name,
36725+ .value = value,
36726+ .size = size,
36727+ .flags = flags
36728+ },
36729+ };
36730+
36731+ return au_sxattr(dentry, inode, &arg);
36732+}
36733+
36734+/* ---------------------------------------------------------------------- */
36735+
36736+static int au_xattr_get(const struct xattr_handler *handler,
36737+ struct dentry *dentry, struct inode *inode,
36738+ const char *name, void *buffer, size_t size)
36739+{
36740+ return au_getxattr(dentry, inode, name, buffer, size);
36741+}
36742+
36743+static int au_xattr_set(const struct xattr_handler *handler,
36744+ struct user_namespace *userns,
36745+ struct dentry *dentry, struct inode *inode,
36746+ const char *name, const void *value, size_t size,
36747+ int flags)
36748+{
36749+ return au_setxattr(dentry, inode, name, value, size, flags);
36750+}
36751+
36752+static const struct xattr_handler au_xattr_handler = {
36753+ .name = "",
36754+ .prefix = "",
36755+ .get = au_xattr_get,
36756+ .set = au_xattr_set
36757+};
36758+
36759+static const struct xattr_handler *au_xattr_handlers[] = {
36760+#ifdef CONFIG_FS_POSIX_ACL
36761+ &posix_acl_access_xattr_handler,
36762+ &posix_acl_default_xattr_handler,
36763+#endif
36764+ &au_xattr_handler, /* must be last */
36765+ NULL
36766+};
36767+
36768+void au_xattr_init(struct super_block *sb)
36769+{
36770+ sb->s_xattr = au_xattr_handlers;
36771+}
36772diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
36773--- /usr/share/empty/fs/aufs/xino.c 1970-01-01 01:00:00.000000000 +0100
36774+++ linux/fs/aufs/xino.c 2021-06-30 21:35:11.397206648 +0200
36775@@ -0,0 +1,1925 @@
36776+// SPDX-License-Identifier: GPL-2.0
36777+/*
36778+ * Copyright (C) 2005-2020 Junjiro R. Okajima
36779+ *
36780+ * This program, aufs is free software; you can redistribute it and/or modify
36781+ * it under the terms of the GNU General Public License as published by
36782+ * the Free Software Foundation; either version 2 of the License, or
36783+ * (at your option) any later version.
36784+ *
36785+ * This program is distributed in the hope that it will be useful,
36786+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36787+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36788+ * GNU General Public License for more details.
36789+ *
36790+ * You should have received a copy of the GNU General Public License
36791+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
36792+ */
36793+
36794+/*
36795+ * external inode number translation table and bitmap
36796+ *
36797+ * things to consider
36798+ * - the lifetime
36799+ * + au_xino object
36800+ * + XINO files (xino, xib, xigen)
36801+ * + dynamic debugfs entries (xiN)
36802+ * + static debugfs entries (xib, xigen)
36803+ * + static sysfs entry (xi_path)
36804+ * - several entry points to handle them.
36805+ * + mount(2) without xino option (default)
36806+ * + mount(2) with xino option
36807+ * + mount(2) with noxino option
36808+ * + umount(2)
36809+ * + remount with add/del branches
36810+ * + remount with xino/noxino options
36811+ */
36812+
36813+#include <linux/seq_file.h>
36814+#include <linux/statfs.h>
36815+#include "aufs.h"
36816+
36817+static aufs_bindex_t sbr_find_shared(struct super_block *sb, aufs_bindex_t btop,
36818+ aufs_bindex_t bbot,
36819+ struct super_block *h_sb)
36820+{
36821+ /* todo: try binary-search if the branches are many */
36822+ for (; btop <= bbot; btop++)
36823+ if (h_sb == au_sbr_sb(sb, btop))
36824+ return btop;
36825+ return -1;
36826+}
36827+
36828+/*
36829+ * find another branch who is on the same filesystem of the specified
36830+ * branch{@btgt}. search until @bbot.
36831+ */
36832+static aufs_bindex_t is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
36833+ aufs_bindex_t bbot)
36834+{
36835+ aufs_bindex_t bindex;
36836+ struct super_block *tgt_sb;
36837+
36838+ tgt_sb = au_sbr_sb(sb, btgt);
36839+ bindex = sbr_find_shared(sb, /*btop*/0, btgt - 1, tgt_sb);
36840+ if (bindex < 0)
36841+ bindex = sbr_find_shared(sb, btgt + 1, bbot, tgt_sb);
36842+
36843+ return bindex;
36844+}
36845+
36846+/* ---------------------------------------------------------------------- */
36847+
36848+/*
36849+ * stop unnecessary notify events at creating xino files
36850+ */
36851+
36852+aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry)
36853+{
36854+ aufs_bindex_t bfound, bindex, bbot;
36855+ struct dentry *parent;
36856+ struct au_branch *br;
36857+
36858+ bfound = -1;
36859+ parent = dentry->d_parent; /* safe d_parent access */
36860+ bbot = au_sbbot(sb);
36861+ for (bindex = 0; bindex <= bbot; bindex++) {
36862+ br = au_sbr(sb, bindex);
36863+ if (au_br_dentry(br) == parent) {
36864+ bfound = bindex;
36865+ break;
36866+ }
36867+ }
36868+
36869+ AuDbg("bfound b%d\n", bfound);
36870+ return bfound;
36871+}
36872+
36873+struct au_xino_lock_dir {
36874+ struct au_hinode *hdir;
36875+ struct dentry *parent;
36876+ struct inode *dir;
36877+};
36878+
36879+static struct dentry *au_dget_parent_lock(struct dentry *dentry,
36880+ unsigned int lsc)
36881+{
36882+ struct dentry *parent;
36883+ struct inode *dir;
36884+
36885+ parent = dget_parent(dentry);
36886+ dir = d_inode(parent);
36887+ inode_lock_nested(dir, lsc);
36888+#if 0 /* it should not happen */
36889+ spin_lock(&dentry->d_lock);
36890+ if (unlikely(dentry->d_parent != parent)) {
36891+ spin_unlock(&dentry->d_lock);
36892+ inode_unlock(dir);
36893+ dput(parent);
36894+ parent = NULL;
36895+ goto out;
36896+ }
36897+ spin_unlock(&dentry->d_lock);
36898+
36899+out:
36900+#endif
36901+ return parent;
36902+}
36903+
36904+static void au_xino_lock_dir(struct super_block *sb, struct path *xipath,
36905+ struct au_xino_lock_dir *ldir)
36906+{
36907+ aufs_bindex_t bindex;
36908+
36909+ ldir->hdir = NULL;
36910+ bindex = au_xi_root(sb, xipath->dentry);
36911+ if (bindex >= 0) {
36912+ /* rw branch root */
36913+ ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
36914+ au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
36915+ } else {
36916+ /* other */
36917+ ldir->parent = au_dget_parent_lock(xipath->dentry,
36918+ AuLsc_I_PARENT);
36919+ ldir->dir = d_inode(ldir->parent);
36920+ }
36921+}
36922+
36923+static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
36924+{
36925+ if (ldir->hdir)
36926+ au_hn_inode_unlock(ldir->hdir);
36927+ else {
36928+ inode_unlock(ldir->dir);
36929+ dput(ldir->parent);
36930+ }
36931+}
36932+
36933+/* ---------------------------------------------------------------------- */
36934+
36935+/*
36936+ * create and set a new xino file
36937+ */
36938+struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
36939+ int wbrtop)
36940+{
36941+ struct file *file;
36942+ struct dentry *h_parent, *d;
36943+ struct inode *h_dir, *inode;
36944+ int err;
36945+ static DEFINE_MUTEX(mtx);
36946+
36947+ /*
36948+ * at mount-time, and the xino file is the default path,
36949+ * hnotify is disabled so we have no notify events to ignore.
36950+ * when a user specified the xino, we cannot get au_hdir to be ignored.
36951+ */
36952+ if (!wbrtop)
36953+ mutex_lock(&mtx);
36954+ file = vfsub_filp_open(fpath, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
36955+ /* | __FMODE_NONOTIFY */,
36956+ 0666);
36957+ if (IS_ERR(file)) {
36958+ if (!wbrtop)
36959+ mutex_unlock(&mtx);
36960+ if (!silent)
36961+ pr_err("open %s(%ld)\n", fpath, PTR_ERR(file));
36962+ return file;
36963+ }
36964+
36965+ /* keep file count */
36966+ err = 0;
36967+ d = file->f_path.dentry;
36968+ h_parent = au_dget_parent_lock(d, AuLsc_I_PARENT);
36969+ if (!wbrtop)
36970+ mutex_unlock(&mtx);
36971+ /* mnt_want_write() is unnecessary here */
36972+ h_dir = d_inode(h_parent);
36973+ inode = file_inode(file);
36974+ /* no delegation since it is just created */
36975+ if (inode->i_nlink)
36976+ err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
36977+ /*force*/0);
36978+ inode_unlock(h_dir);
36979+ dput(h_parent);
36980+ if (unlikely(err)) {
36981+ if (!silent)
36982+ pr_err("unlink %s(%d)\n", fpath, err);
36983+ goto out;
36984+ }
36985+
36986+ err = -EINVAL;
36987+ if (unlikely(sb == d->d_sb)) {
36988+ if (!silent)
36989+ pr_err("%s must be outside\n", fpath);
36990+ goto out;
36991+ }
36992+ if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
36993+ if (!silent)
36994+ pr_err("xino doesn't support %s(%s)\n",
36995+ fpath, au_sbtype(d->d_sb));
36996+ goto out;
36997+ }
36998+ return file; /* success */
36999+
37000+out:
37001+ fput(file);
37002+ file = ERR_PTR(err);
37003+ return file;
37004+}
37005+
37006+/*
37007+ * create a new xinofile at the same place/path as @base.
37008+ */
37009+struct file *au_xino_create2(struct super_block *sb, struct path *base,
37010+ struct file *copy_src)
37011+{
37012+ struct file *file;
37013+ struct dentry *dentry, *parent;
37014+ struct inode *dir, *delegated;
37015+ struct qstr *name;
37016+ struct path path;
37017+ int err, do_unlock;
37018+ struct au_xino_lock_dir ldir;
37019+
37020+ do_unlock = 1;
37021+ au_xino_lock_dir(sb, base, &ldir);
37022+ dentry = base->dentry;
37023+ parent = dentry->d_parent; /* dir inode is locked */
37024+ dir = d_inode(parent);
37025+ IMustLock(dir);
37026+
37027+ name = &dentry->d_name;
37028+ path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
37029+ if (IS_ERR(path.dentry)) {
37030+ file = (void *)path.dentry;
37031+ pr_err("%pd lookup err %ld\n", dentry, PTR_ERR(path.dentry));
37032+ goto out;
37033+ }
37034+
37035+ /* no need to mnt_want_write() since we call dentry_open() later */
37036+ err = vfs_create(mnt_user_ns(base->mnt), dir, path.dentry, 0666, NULL);
37037+ if (unlikely(err)) {
37038+ file = ERR_PTR(err);
37039+ pr_err("%pd create err %d\n", dentry, err);
37040+ goto out_dput;
37041+ }
37042+
37043+ path.mnt = base->mnt;
37044+ file = vfsub_dentry_open(&path,
37045+ O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37046+ /* | __FMODE_NONOTIFY */);
37047+ if (IS_ERR(file)) {
37048+ pr_err("%pd open err %ld\n", dentry, PTR_ERR(file));
37049+ goto out_dput;
37050+ }
37051+
37052+ delegated = NULL;
37053+ err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
37054+ au_xino_unlock_dir(&ldir);
37055+ do_unlock = 0;
37056+ if (unlikely(err == -EWOULDBLOCK)) {
37057+ pr_warn("cannot retry for NFSv4 delegation"
37058+ " for an internal unlink\n");
37059+ iput(delegated);
37060+ }
37061+ if (unlikely(err)) {
37062+ pr_err("%pd unlink err %d\n", dentry, err);
37063+ goto out_fput;
37064+ }
37065+
37066+ if (copy_src) {
37067+ /* no one can touch copy_src xino */
37068+ err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
37069+ if (unlikely(err)) {
37070+ pr_err("%pd copy err %d\n", dentry, err);
37071+ goto out_fput;
37072+ }
37073+ }
37074+ goto out_dput; /* success */
37075+
37076+out_fput:
37077+ fput(file);
37078+ file = ERR_PTR(err);
37079+out_dput:
37080+ dput(path.dentry);
37081+out:
37082+ if (do_unlock)
37083+ au_xino_unlock_dir(&ldir);
37084+ return file;
37085+}
37086+
37087+struct file *au_xino_file1(struct au_xino *xi)
37088+{
37089+ struct file *file;
37090+ unsigned int u, nfile;
37091+
37092+ file = NULL;
37093+ nfile = xi->xi_nfile;
37094+ for (u = 0; u < nfile; u++) {
37095+ file = xi->xi_file[u];
37096+ if (file)
37097+ break;
37098+ }
37099+
37100+ return file;
37101+}
37102+
37103+static int au_xino_file_set(struct au_xino *xi, int idx, struct file *file)
37104+{
37105+ int err;
37106+ struct file *f;
37107+ void *p;
37108+
37109+ if (file)
37110+ get_file(file);
37111+
37112+ err = 0;
37113+ f = NULL;
37114+ if (idx < xi->xi_nfile) {
37115+ f = xi->xi_file[idx];
37116+ if (f)
37117+ fput(f);
37118+ } else {
37119+ p = au_kzrealloc(xi->xi_file,
37120+ sizeof(*xi->xi_file) * xi->xi_nfile,
37121+ sizeof(*xi->xi_file) * (idx + 1),
37122+ GFP_NOFS, /*may_shrink*/0);
37123+ if (p) {
37124+ MtxMustLock(&xi->xi_mtx);
37125+ xi->xi_file = p;
37126+ xi->xi_nfile = idx + 1;
37127+ } else {
37128+ err = -ENOMEM;
37129+ if (file)
37130+ fput(file);
37131+ goto out;
37132+ }
37133+ }
37134+ xi->xi_file[idx] = file;
37135+
37136+out:
37137+ return err;
37138+}
37139+
37140+/*
37141+ * if @xinew->xi is not set, then create new xigen file.
37142+ */
37143+struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew)
37144+{
37145+ struct file *file;
37146+ int err;
37147+
37148+ SiMustAnyLock(sb);
37149+
37150+ file = au_xino_create2(sb, xinew->base, xinew->copy_src);
37151+ if (IS_ERR(file)) {
37152+ err = PTR_ERR(file);
37153+ pr_err("%s[%d], err %d\n",
37154+ xinew->xi ? "xino" : "xigen",
37155+ xinew->idx, err);
37156+ goto out;
37157+ }
37158+
37159+ if (xinew->xi)
37160+ err = au_xino_file_set(xinew->xi, xinew->idx, file);
37161+ else {
37162+ BUG();
37163+ /* todo: make xigen file an array */
37164+ /* err = au_xigen_file_set(sb, xinew->idx, file); */
37165+ }
37166+ fput(file);
37167+ if (unlikely(err))
37168+ file = ERR_PTR(err);
37169+
37170+out:
37171+ return file;
37172+}
37173+
37174+/* ---------------------------------------------------------------------- */
37175+
37176+/*
37177+ * truncate xino files
37178+ */
37179+static int au_xino_do_trunc(struct super_block *sb, aufs_bindex_t bindex,
37180+ int idx, struct kstatfs *st)
37181+{
37182+ int err;
37183+ blkcnt_t blocks;
37184+ struct file *file, *new_xino;
37185+ struct au_xi_new xinew = {
37186+ .idx = idx
37187+ };
37188+
37189+ err = 0;
37190+ xinew.xi = au_sbr(sb, bindex)->br_xino;
37191+ file = au_xino_file(xinew.xi, idx);
37192+ if (!file)
37193+ goto out;
37194+
37195+ xinew.base = &file->f_path;
37196+ err = vfs_statfs(xinew.base, st);
37197+ if (unlikely(err)) {
37198+ AuErr1("statfs err %d, ignored\n", err);
37199+ err = 0;
37200+ goto out;
37201+ }
37202+
37203+ blocks = file_inode(file)->i_blocks;
37204+ pr_info("begin truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37205+ bindex, idx, (u64)blocks, st->f_bfree, st->f_blocks);
37206+
37207+ xinew.copy_src = file;
37208+ new_xino = au_xi_new(sb, &xinew);
37209+ if (IS_ERR(new_xino)) {
37210+ err = PTR_ERR(new_xino);
37211+ pr_err("xino(b%d-%d), err %d, ignored\n", bindex, idx, err);
37212+ goto out;
37213+ }
37214+
37215+ err = vfs_statfs(&new_xino->f_path, st);
37216+ if (!err)
37217+ pr_info("end truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37218+ bindex, idx, (u64)file_inode(new_xino)->i_blocks,
37219+ st->f_bfree, st->f_blocks);
37220+ else {
37221+ AuErr1("statfs err %d, ignored\n", err);
37222+ err = 0;
37223+ }
37224+
37225+out:
37226+ return err;
37227+}
37228+
37229+int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin)
37230+{
37231+ int err, i;
37232+ unsigned long jiffy;
37233+ aufs_bindex_t bbot;
37234+ struct kstatfs *st;
37235+ struct au_branch *br;
37236+ struct au_xino *xi;
37237+
37238+ err = -ENOMEM;
37239+ st = kmalloc(sizeof(*st), GFP_NOFS);
37240+ if (unlikely(!st))
37241+ goto out;
37242+
37243+ err = -EINVAL;
37244+ bbot = au_sbbot(sb);
37245+ if (unlikely(bindex < 0 || bbot < bindex))
37246+ goto out_st;
37247+
37248+ err = 0;
37249+ jiffy = jiffies;
37250+ br = au_sbr(sb, bindex);
37251+ xi = br->br_xino;
37252+ for (i = idx_begin; !err && i < xi->xi_nfile; i++)
37253+ err = au_xino_do_trunc(sb, bindex, i, st);
37254+ if (!err)
37255+ au_sbi(sb)->si_xino_jiffy = jiffy;
37256+
37257+out_st:
37258+ au_kfree_rcu(st);
37259+out:
37260+ return err;
37261+}
37262+
37263+struct xino_do_trunc_args {
37264+ struct super_block *sb;
37265+ struct au_branch *br;
37266+ int idx;
37267+};
37268+
37269+static void xino_do_trunc(void *_args)
37270+{
37271+ struct xino_do_trunc_args *args = _args;
37272+ struct super_block *sb;
37273+ struct au_branch *br;
37274+ struct inode *dir;
37275+ int err, idx;
37276+ aufs_bindex_t bindex;
37277+
37278+ err = 0;
37279+ sb = args->sb;
37280+ dir = d_inode(sb->s_root);
37281+ br = args->br;
37282+ idx = args->idx;
37283+
37284+ si_noflush_write_lock(sb);
37285+ ii_read_lock_parent(dir);
37286+ bindex = au_br_index(sb, br->br_id);
37287+ err = au_xino_trunc(sb, bindex, idx);
37288+ ii_read_unlock(dir);
37289+ if (unlikely(err))
37290+ pr_warn("err b%d, (%d)\n", bindex, err);
37291+ atomic_dec(&br->br_xino->xi_truncating);
37292+ au_lcnt_dec(&br->br_count);
37293+ si_write_unlock(sb);
37294+ au_nwt_done(&au_sbi(sb)->si_nowait);
37295+ au_kfree_rcu(args);
37296+}
37297+
37298+/*
37299+ * returns the index in the xi_file array whose corresponding file is necessary
37300+ * to truncate, or -1 which means no need to truncate.
37301+ */
37302+static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
37303+{
37304+ int err;
37305+ unsigned int u;
37306+ struct kstatfs st;
37307+ struct au_sbinfo *sbinfo;
37308+ struct au_xino *xi;
37309+ struct file *file;
37310+
37311+ /* todo: si_xino_expire and the ratio should be customizable */
37312+ sbinfo = au_sbi(sb);
37313+ if (time_before(jiffies,
37314+ sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
37315+ return -1;
37316+
37317+ /* truncation border */
37318+ xi = br->br_xino;
37319+ for (u = 0; u < xi->xi_nfile; u++) {
37320+ file = au_xino_file(xi, u);
37321+ if (!file)
37322+ continue;
37323+
37324+ err = vfs_statfs(&file->f_path, &st);
37325+ if (unlikely(err)) {
37326+ AuErr1("statfs err %d, ignored\n", err);
37327+ return -1;
37328+ }
37329+ if (div64_u64(st.f_bfree * 100, st.f_blocks)
37330+ >= AUFS_XINO_DEF_TRUNC)
37331+ return u;
37332+ }
37333+
37334+ return -1;
37335+}
37336+
37337+static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
37338+{
37339+ int idx;
37340+ struct xino_do_trunc_args *args;
37341+ int wkq_err;
37342+
37343+ idx = xino_trunc_test(sb, br);
37344+ if (idx < 0)
37345+ return;
37346+
37347+ if (atomic_inc_return(&br->br_xino->xi_truncating) > 1)
37348+ goto out;
37349+
37350+ /* lock and kfree() will be called in trunc_xino() */
37351+ args = kmalloc(sizeof(*args), GFP_NOFS);
37352+ if (unlikely(!args)) {
37353+ AuErr1("no memory\n");
37354+ goto out;
37355+ }
37356+
37357+ au_lcnt_inc(&br->br_count);
37358+ args->sb = sb;
37359+ args->br = br;
37360+ args->idx = idx;
37361+ wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
37362+ if (!wkq_err)
37363+ return; /* success */
37364+
37365+ pr_err("wkq %d\n", wkq_err);
37366+ au_lcnt_dec(&br->br_count);
37367+ au_kfree_rcu(args);
37368+
37369+out:
37370+ atomic_dec(&br->br_xino->xi_truncating);
37371+}
37372+
37373+/* ---------------------------------------------------------------------- */
37374+
37375+struct au_xi_calc {
37376+ int idx;
37377+ loff_t pos;
37378+};
37379+
37380+static void au_xi_calc(struct super_block *sb, ino_t h_ino,
37381+ struct au_xi_calc *calc)
37382+{
37383+ loff_t maxent;
37384+
37385+ maxent = au_xi_maxent(sb);
37386+ calc->idx = div64_u64_rem(h_ino, maxent, &calc->pos);
37387+ calc->pos *= sizeof(ino_t);
37388+}
37389+
37390+static int au_xino_do_new_async(struct super_block *sb, struct au_branch *br,
37391+ struct au_xi_calc *calc)
37392+{
37393+ int err;
37394+ struct file *file;
37395+ struct au_xino *xi = br->br_xino;
37396+ struct au_xi_new xinew = {
37397+ .xi = xi
37398+ };
37399+
37400+ SiMustAnyLock(sb);
37401+
37402+ err = 0;
37403+ if (!xi)
37404+ goto out;
37405+
37406+ mutex_lock(&xi->xi_mtx);
37407+ file = au_xino_file(xi, calc->idx);
37408+ if (file)
37409+ goto out_mtx;
37410+
37411+ file = au_xino_file(xi, /*idx*/-1);
37412+ AuDebugOn(!file);
37413+ xinew.idx = calc->idx;
37414+ xinew.base = &file->f_path;
37415+ /* xinew.copy_src = NULL; */
37416+ file = au_xi_new(sb, &xinew);
37417+ if (IS_ERR(file))
37418+ err = PTR_ERR(file);
37419+
37420+out_mtx:
37421+ mutex_unlock(&xi->xi_mtx);
37422+out:
37423+ return err;
37424+}
37425+
37426+struct au_xino_do_new_async_args {
37427+ struct super_block *sb;
37428+ struct au_branch *br;
37429+ struct au_xi_calc calc;
37430+ ino_t ino;
37431+};
37432+
37433+struct au_xi_writing {
37434+ struct hlist_bl_node node;
37435+ ino_t h_ino, ino;
37436+};
37437+
37438+static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37439+ ino_t ino);
37440+
37441+static void au_xino_call_do_new_async(void *args)
37442+{
37443+ struct au_xino_do_new_async_args *a = args;
37444+ struct au_branch *br;
37445+ struct super_block *sb;
37446+ struct au_sbinfo *sbi;
37447+ struct inode *root;
37448+ struct file *file;
37449+ struct au_xi_writing *del, *p;
37450+ struct hlist_bl_head *hbl;
37451+ struct hlist_bl_node *pos;
37452+ int err;
37453+
37454+ br = a->br;
37455+ sb = a->sb;
37456+ sbi = au_sbi(sb);
37457+ si_noflush_read_lock(sb);
37458+ root = d_inode(sb->s_root);
37459+ ii_read_lock_child(root);
37460+ err = au_xino_do_new_async(sb, br, &a->calc);
37461+ if (unlikely(err)) {
37462+ AuIOErr("err %d\n", err);
37463+ goto out;
37464+ }
37465+
37466+ file = au_xino_file(br->br_xino, a->calc.idx);
37467+ AuDebugOn(!file);
37468+ err = au_xino_do_write(file, &a->calc, a->ino);
37469+ if (unlikely(err)) {
37470+ AuIOErr("err %d\n", err);
37471+ goto out;
37472+ }
37473+
37474+ del = NULL;
37475+ hbl = &br->br_xino->xi_writing;
37476+ hlist_bl_lock(hbl);
37477+ au_hbl_for_each(pos, hbl) {
37478+ p = container_of(pos, struct au_xi_writing, node);
37479+ if (p->ino == a->ino) {
37480+ del = p;
37481+ hlist_bl_del(&p->node);
37482+ break;
37483+ }
37484+ }
37485+ hlist_bl_unlock(hbl);
37486+ au_kfree_rcu(del);
37487+
37488+out:
37489+ au_lcnt_dec(&br->br_count);
37490+ ii_read_unlock(root);
37491+ si_read_unlock(sb);
37492+ au_nwt_done(&sbi->si_nowait);
37493+ au_kfree_rcu(a);
37494+}
37495+
37496+/*
37497+ * create a new xino file asynchronously
37498+ */
37499+static int au_xino_new_async(struct super_block *sb, struct au_branch *br,
37500+ struct au_xi_calc *calc, ino_t ino)
37501+{
37502+ int err;
37503+ struct au_xino_do_new_async_args *arg;
37504+
37505+ err = -ENOMEM;
37506+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
37507+ if (unlikely(!arg))
37508+ goto out;
37509+
37510+ arg->sb = sb;
37511+ arg->br = br;
37512+ arg->calc = *calc;
37513+ arg->ino = ino;
37514+ au_lcnt_inc(&br->br_count);
37515+ err = au_wkq_nowait(au_xino_call_do_new_async, arg, sb, AuWkq_NEST);
37516+ if (unlikely(err)) {
37517+ pr_err("wkq %d\n", err);
37518+ au_lcnt_dec(&br->br_count);
37519+ au_kfree_rcu(arg);
37520+ }
37521+
37522+out:
37523+ return err;
37524+}
37525+
37526+/*
37527+ * read @ino from xinofile for the specified branch{@sb, @bindex}
37528+ * at the position of @h_ino.
37529+ */
37530+int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37531+ ino_t *ino)
37532+{
37533+ int err;
37534+ ssize_t sz;
37535+ struct au_xi_calc calc;
37536+ struct au_sbinfo *sbinfo;
37537+ struct file *file;
37538+ struct au_xino *xi;
37539+ struct hlist_bl_head *hbl;
37540+ struct hlist_bl_node *pos;
37541+ struct au_xi_writing *p;
37542+
37543+ *ino = 0;
37544+ if (!au_opt_test(au_mntflags(sb), XINO))
37545+ return 0; /* no xino */
37546+
37547+ err = 0;
37548+ au_xi_calc(sb, h_ino, &calc);
37549+ xi = au_sbr(sb, bindex)->br_xino;
37550+ file = au_xino_file(xi, calc.idx);
37551+ if (!file) {
37552+ hbl = &xi->xi_writing;
37553+ hlist_bl_lock(hbl);
37554+ au_hbl_for_each(pos, hbl) {
37555+ p = container_of(pos, struct au_xi_writing, node);
37556+ if (p->h_ino == h_ino) {
37557+ AuDbg("hi%llu, i%llu, found\n",
37558+ (u64)p->h_ino, (u64)p->ino);
37559+ *ino = p->ino;
37560+ break;
37561+ }
37562+ }
37563+ hlist_bl_unlock(hbl);
37564+ return 0;
37565+ } else if (vfsub_f_size_read(file) < calc.pos + sizeof(*ino))
37566+ return 0; /* no xino */
37567+
37568+ sbinfo = au_sbi(sb);
37569+ sz = xino_fread(file, ino, sizeof(*ino), &calc.pos);
37570+ if (sz == sizeof(*ino))
37571+ return 0; /* success */
37572+
37573+ err = sz;
37574+ if (unlikely(sz >= 0)) {
37575+ err = -EIO;
37576+ AuIOErr("xino read error (%zd)\n", sz);
37577+ }
37578+ return err;
37579+}
37580+
37581+static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37582+ ino_t ino)
37583+{
37584+ ssize_t sz;
37585+
37586+ sz = xino_fwrite(file, &ino, sizeof(ino), &calc->pos);
37587+ if (sz == sizeof(ino))
37588+ return 0; /* success */
37589+
37590+ AuIOErr("write failed (%zd)\n", sz);
37591+ return -EIO;
37592+}
37593+
37594+/*
37595+ * write @ino to the xinofile for the specified branch{@sb, @bindex}
37596+ * at the position of @h_ino.
37597+ * even if @ino is zero, it is written to the xinofile and means no entry.
37598+ * if the size of the xino file on a specific filesystem exceeds the watermark,
37599+ * try truncating it.
37600+ */
37601+int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37602+ ino_t ino)
37603+{
37604+ int err;
37605+ unsigned int mnt_flags;
37606+ struct au_xi_calc calc;
37607+ struct file *file;
37608+ struct au_branch *br;
37609+ struct au_xino *xi;
37610+ struct au_xi_writing *p;
37611+
37612+ SiMustAnyLock(sb);
37613+
37614+ mnt_flags = au_mntflags(sb);
37615+ if (!au_opt_test(mnt_flags, XINO))
37616+ return 0;
37617+
37618+ au_xi_calc(sb, h_ino, &calc);
37619+ br = au_sbr(sb, bindex);
37620+ xi = br->br_xino;
37621+ file = au_xino_file(xi, calc.idx);
37622+ if (!file) {
37623+ /* store the inum pair into the list */
37624+ p = kmalloc(sizeof(*p), GFP_NOFS | __GFP_NOFAIL);
37625+ p->h_ino = h_ino;
37626+ p->ino = ino;
37627+ au_hbl_add(&p->node, &xi->xi_writing);
37628+
37629+ /* create and write a new xino file asynchronously */
37630+ err = au_xino_new_async(sb, br, &calc, ino);
37631+ if (!err)
37632+ return 0; /* success */
37633+ goto out;
37634+ }
37635+
37636+ err = au_xino_do_write(file, &calc, ino);
37637+ if (!err) {
37638+ br = au_sbr(sb, bindex);
37639+ if (au_opt_test(mnt_flags, TRUNC_XINO)
37640+ && au_test_fs_trunc_xino(au_br_sb(br)))
37641+ xino_try_trunc(sb, br);
37642+ return 0; /* success */
37643+ }
37644+
37645+out:
37646+ AuIOErr("write failed (%d)\n", err);
37647+ return -EIO;
37648+}
37649+
37650+static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
37651+ loff_t *pos);
37652+
37653+/* todo: unnecessary to support mmap_sem since kernel-space? */
37654+ssize_t xino_fread(struct file *file, void *kbuf, size_t size, loff_t *pos)
37655+{
37656+ ssize_t err;
37657+ int i;
37658+ const int prevent_endless = 10;
37659+
37660+ i = 0;
37661+ do {
37662+ err = vfsub_read_k(file, kbuf, size, pos);
37663+ if (err == -EINTR
37664+ && !au_wkq_test()
37665+ && fatal_signal_pending(current)) {
37666+ err = xino_fread_wkq(file, kbuf, size, pos);
37667+ BUG_ON(err == -EINTR);
37668+ }
37669+ } while (i++ < prevent_endless
37670+ && (err == -EAGAIN || err == -EINTR));
37671+
37672+#if 0 /* reserved for future use */
37673+ if (err > 0)
37674+ fsnotify_access(file->f_path.dentry);
37675+#endif
37676+
37677+ return err;
37678+}
37679+
37680+struct xino_fread_args {
37681+ ssize_t *errp;
37682+ struct file *file;
37683+ void *buf;
37684+ size_t size;
37685+ loff_t *pos;
37686+};
37687+
37688+static void call_xino_fread(void *args)
37689+{
37690+ struct xino_fread_args *a = args;
37691+ *a->errp = xino_fread(a->file, a->buf, a->size, a->pos);
37692+}
37693+
37694+static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
37695+ loff_t *pos)
37696+{
37697+ ssize_t err;
37698+ int wkq_err;
37699+ struct xino_fread_args args = {
37700+ .errp = &err,
37701+ .file = file,
37702+ .buf = buf,
37703+ .size = size,
37704+ .pos = pos
37705+ };
37706+
37707+ wkq_err = au_wkq_wait(call_xino_fread, &args);
37708+ if (unlikely(wkq_err))
37709+ err = wkq_err;
37710+
37711+ return err;
37712+}
37713+
37714+static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
37715+ loff_t *pos);
37716+
37717+static ssize_t do_xino_fwrite(struct file *file, void *kbuf, size_t size,
37718+ loff_t *pos)
37719+{
37720+ ssize_t err;
37721+ int i;
37722+ const int prevent_endless = 10;
37723+
37724+ i = 0;
37725+ do {
37726+ err = vfsub_write_k(file, kbuf, size, pos);
37727+ if (err == -EINTR
37728+ && !au_wkq_test()
37729+ && fatal_signal_pending(current)) {
37730+ err = xino_fwrite_wkq(file, kbuf, size, pos);
37731+ BUG_ON(err == -EINTR);
37732+ }
37733+ } while (i++ < prevent_endless
37734+ && (err == -EAGAIN || err == -EINTR));
37735+
37736+#if 0 /* reserved for future use */
37737+ if (err > 0)
37738+ fsnotify_modify(file->f_path.dentry);
37739+#endif
37740+
37741+ return err;
37742+}
37743+
37744+struct do_xino_fwrite_args {
37745+ ssize_t *errp;
37746+ struct file *file;
37747+ void *buf;
37748+ size_t size;
37749+ loff_t *pos;
37750+};
37751+
37752+static void call_do_xino_fwrite(void *args)
37753+{
37754+ struct do_xino_fwrite_args *a = args;
37755+ *a->errp = do_xino_fwrite(a->file, a->buf, a->size, a->pos);
37756+}
37757+
37758+static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
37759+ loff_t *pos)
37760+{
37761+ ssize_t err;
37762+ int wkq_err;
37763+ struct do_xino_fwrite_args args = {
37764+ .errp = &err,
37765+ .file = file,
37766+ .buf = buf,
37767+ .size = size,
37768+ .pos = pos
37769+ };
37770+
37771+ /*
37772+ * it breaks RLIMIT_FSIZE and normal user's limit,
37773+ * users should care about quota and real 'filesystem full.'
37774+ */
37775+ wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
37776+ if (unlikely(wkq_err))
37777+ err = wkq_err;
37778+
37779+ return err;
37780+}
37781+
37782+ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos)
37783+{
37784+ ssize_t err;
37785+
37786+ if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
37787+ lockdep_off();
37788+ err = do_xino_fwrite(file, buf, size, pos);
37789+ lockdep_on();
37790+ } else {
37791+ lockdep_off();
37792+ err = xino_fwrite_wkq(file, buf, size, pos);
37793+ lockdep_on();
37794+ }
37795+
37796+ return err;
37797+}
37798+
37799+/* ---------------------------------------------------------------------- */
37800+
37801+/*
37802+ * inode number bitmap
37803+ */
37804+static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
37805+static ino_t xib_calc_ino(unsigned long pindex, int bit)
37806+{
37807+ ino_t ino;
37808+
37809+ AuDebugOn(bit < 0 || page_bits <= bit);
37810+ ino = AUFS_FIRST_INO + pindex * page_bits + bit;
37811+ return ino;
37812+}
37813+
37814+static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
37815+{
37816+ AuDebugOn(ino < AUFS_FIRST_INO);
37817+ ino -= AUFS_FIRST_INO;
37818+ *pindex = ino / page_bits;
37819+ *bit = ino % page_bits;
37820+}
37821+
37822+static int xib_pindex(struct super_block *sb, unsigned long pindex)
37823+{
37824+ int err;
37825+ loff_t pos;
37826+ ssize_t sz;
37827+ struct au_sbinfo *sbinfo;
37828+ struct file *xib;
37829+ unsigned long *p;
37830+
37831+ sbinfo = au_sbi(sb);
37832+ MtxMustLock(&sbinfo->si_xib_mtx);
37833+ AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
37834+ || !au_opt_test(sbinfo->si_mntflags, XINO));
37835+
37836+ if (pindex == sbinfo->si_xib_last_pindex)
37837+ return 0;
37838+
37839+ xib = sbinfo->si_xib;
37840+ p = sbinfo->si_xib_buf;
37841+ pos = sbinfo->si_xib_last_pindex;
37842+ pos *= PAGE_SIZE;
37843+ sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
37844+ if (unlikely(sz != PAGE_SIZE))
37845+ goto out;
37846+
37847+ pos = pindex;
37848+ pos *= PAGE_SIZE;
37849+ if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
37850+ sz = xino_fread(xib, p, PAGE_SIZE, &pos);
37851+ else {
37852+ memset(p, 0, PAGE_SIZE);
37853+ sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
37854+ }
37855+ if (sz == PAGE_SIZE) {
37856+ sbinfo->si_xib_last_pindex = pindex;
37857+ return 0; /* success */
37858+ }
37859+
37860+out:
37861+ AuIOErr1("write failed (%zd)\n", sz);
37862+ err = sz;
37863+ if (sz >= 0)
37864+ err = -EIO;
37865+ return err;
37866+}
37867+
37868+static void au_xib_clear_bit(struct inode *inode)
37869+{
37870+ int err, bit;
37871+ unsigned long pindex;
37872+ struct super_block *sb;
37873+ struct au_sbinfo *sbinfo;
37874+
37875+ AuDebugOn(inode->i_nlink);
37876+
37877+ sb = inode->i_sb;
37878+ xib_calc_bit(inode->i_ino, &pindex, &bit);
37879+ AuDebugOn(page_bits <= bit);
37880+ sbinfo = au_sbi(sb);
37881+ mutex_lock(&sbinfo->si_xib_mtx);
37882+ err = xib_pindex(sb, pindex);
37883+ if (!err) {
37884+ clear_bit(bit, sbinfo->si_xib_buf);
37885+ sbinfo->si_xib_next_bit = bit;
37886+ }
37887+ mutex_unlock(&sbinfo->si_xib_mtx);
37888+}
37889+
37890+/* ---------------------------------------------------------------------- */
37891+
37892+/*
37893+ * truncate a xino bitmap file
37894+ */
37895+
37896+/* todo: slow */
37897+static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
37898+{
37899+ int err, bit;
37900+ ssize_t sz;
37901+ unsigned long pindex;
37902+ loff_t pos, pend;
37903+ struct au_sbinfo *sbinfo;
37904+ ino_t *ino;
37905+ unsigned long *p;
37906+
37907+ err = 0;
37908+ sbinfo = au_sbi(sb);
37909+ MtxMustLock(&sbinfo->si_xib_mtx);
37910+ p = sbinfo->si_xib_buf;
37911+ pend = vfsub_f_size_read(file);
37912+ pos = 0;
37913+ while (pos < pend) {
37914+ sz = xino_fread(file, page, PAGE_SIZE, &pos);
37915+ err = sz;
37916+ if (unlikely(sz <= 0))
37917+ goto out;
37918+
37919+ err = 0;
37920+ for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
37921+ if (unlikely(*ino < AUFS_FIRST_INO))
37922+ continue;
37923+
37924+ xib_calc_bit(*ino, &pindex, &bit);
37925+ AuDebugOn(page_bits <= bit);
37926+ err = xib_pindex(sb, pindex);
37927+ if (!err)
37928+ set_bit(bit, p);
37929+ else
37930+ goto out;
37931+ }
37932+ }
37933+
37934+out:
37935+ return err;
37936+}
37937+
37938+static int xib_restore(struct super_block *sb)
37939+{
37940+ int err, i;
37941+ unsigned int nfile;
37942+ aufs_bindex_t bindex, bbot;
37943+ void *page;
37944+ struct au_branch *br;
37945+ struct au_xino *xi;
37946+ struct file *file;
37947+
37948+ err = -ENOMEM;
37949+ page = (void *)__get_free_page(GFP_NOFS);
37950+ if (unlikely(!page))
37951+ goto out;
37952+
37953+ err = 0;
37954+ bbot = au_sbbot(sb);
37955+ for (bindex = 0; !err && bindex <= bbot; bindex++)
37956+ if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0) {
37957+ br = au_sbr(sb, bindex);
37958+ xi = br->br_xino;
37959+ nfile = xi->xi_nfile;
37960+ for (i = 0; i < nfile; i++) {
37961+ file = au_xino_file(xi, i);
37962+ if (file)
37963+ err = do_xib_restore(sb, file, page);
37964+ }
37965+ } else
37966+ AuDbg("skip shared b%d\n", bindex);
37967+ free_page((unsigned long)page);
37968+
37969+out:
37970+ return err;
37971+}
37972+
37973+int au_xib_trunc(struct super_block *sb)
37974+{
37975+ int err;
37976+ ssize_t sz;
37977+ loff_t pos;
37978+ struct au_sbinfo *sbinfo;
37979+ unsigned long *p;
37980+ struct file *file;
37981+
37982+ SiMustWriteLock(sb);
37983+
37984+ err = 0;
37985+ sbinfo = au_sbi(sb);
37986+ if (!au_opt_test(sbinfo->si_mntflags, XINO))
37987+ goto out;
37988+
37989+ file = sbinfo->si_xib;
37990+ if (vfsub_f_size_read(file) <= PAGE_SIZE)
37991+ goto out;
37992+
37993+ file = au_xino_create2(sb, &sbinfo->si_xib->f_path, NULL);
37994+ err = PTR_ERR(file);
37995+ if (IS_ERR(file))
37996+ goto out;
37997+ fput(sbinfo->si_xib);
37998+ sbinfo->si_xib = file;
37999+
38000+ p = sbinfo->si_xib_buf;
38001+ memset(p, 0, PAGE_SIZE);
38002+ pos = 0;
38003+ sz = xino_fwrite(sbinfo->si_xib, p, PAGE_SIZE, &pos);
38004+ if (unlikely(sz != PAGE_SIZE)) {
38005+ err = sz;
38006+ AuIOErr("err %d\n", err);
38007+ if (sz >= 0)
38008+ err = -EIO;
38009+ goto out;
38010+ }
38011+
38012+ mutex_lock(&sbinfo->si_xib_mtx);
38013+ /* mnt_want_write() is unnecessary here */
38014+ err = xib_restore(sb);
38015+ mutex_unlock(&sbinfo->si_xib_mtx);
38016+
38017+out:
38018+ return err;
38019+}
38020+
38021+/* ---------------------------------------------------------------------- */
38022+
38023+struct au_xino *au_xino_alloc(unsigned int nfile)
38024+{
38025+ struct au_xino *xi;
38026+
38027+ xi = kzalloc(sizeof(*xi), GFP_NOFS);
38028+ if (unlikely(!xi))
38029+ goto out;
38030+ xi->xi_nfile = nfile;
38031+ xi->xi_file = kcalloc(nfile, sizeof(*xi->xi_file), GFP_NOFS);
38032+ if (unlikely(!xi->xi_file))
38033+ goto out_free;
38034+
38035+ xi->xi_nondir.total = 8; /* initial size */
38036+ xi->xi_nondir.array = kcalloc(xi->xi_nondir.total, sizeof(ino_t),
38037+ GFP_NOFS);
38038+ if (unlikely(!xi->xi_nondir.array))
38039+ goto out_file;
38040+
38041+ spin_lock_init(&xi->xi_nondir.spin);
38042+ init_waitqueue_head(&xi->xi_nondir.wqh);
38043+ mutex_init(&xi->xi_mtx);
38044+ INIT_HLIST_BL_HEAD(&xi->xi_writing);
38045+ atomic_set(&xi->xi_truncating, 0);
38046+ kref_init(&xi->xi_kref);
38047+ goto out; /* success */
38048+
38049+out_file:
38050+ au_kfree_try_rcu(xi->xi_file);
38051+out_free:
38052+ au_kfree_rcu(xi);
38053+ xi = NULL;
38054+out:
38055+ return xi;
38056+}
38057+
38058+static int au_xino_init(struct au_branch *br, int idx, struct file *file)
38059+{
38060+ int err;
38061+ struct au_xino *xi;
38062+
38063+ err = 0;
38064+ xi = au_xino_alloc(idx + 1);
38065+ if (unlikely(!xi)) {
38066+ err = -ENOMEM;
38067+ goto out;
38068+ }
38069+
38070+ if (file)
38071+ get_file(file);
38072+ xi->xi_file[idx] = file;
38073+ AuDebugOn(br->br_xino);
38074+ br->br_xino = xi;
38075+
38076+out:
38077+ return err;
38078+}
38079+
38080+static void au_xino_release(struct kref *kref)
38081+{
38082+ struct au_xino *xi;
38083+ int i;
38084+ unsigned long ul;
38085+ struct hlist_bl_head *hbl;
38086+ struct hlist_bl_node *pos, *n;
38087+ struct au_xi_writing *p;
38088+
38089+ xi = container_of(kref, struct au_xino, xi_kref);
38090+ for (i = 0; i < xi->xi_nfile; i++)
38091+ if (xi->xi_file[i])
38092+ fput(xi->xi_file[i]);
38093+ for (i = xi->xi_nondir.total - 1; i >= 0; i--)
38094+ AuDebugOn(xi->xi_nondir.array[i]);
38095+ mutex_destroy(&xi->xi_mtx);
38096+ hbl = &xi->xi_writing;
38097+ ul = au_hbl_count(hbl);
38098+ if (unlikely(ul)) {
38099+ pr_warn("xi_writing %lu\n", ul);
38100+ hlist_bl_lock(hbl);
38101+ hlist_bl_for_each_entry_safe(p, pos, n, hbl, node) {
38102+ hlist_bl_del(&p->node);
38103+ /* kmemleak reported au_kfree_rcu() doesn't free it */
38104+ kfree(p);
38105+ }
38106+ hlist_bl_unlock(hbl);
38107+ }
38108+ au_kfree_try_rcu(xi->xi_file);
38109+ au_kfree_try_rcu(xi->xi_nondir.array);
38110+ au_kfree_rcu(xi);
38111+}
38112+
38113+int au_xino_put(struct au_branch *br)
38114+{
38115+ int ret;
38116+ struct au_xino *xi;
38117+
38118+ ret = 0;
38119+ xi = br->br_xino;
38120+ if (xi) {
38121+ br->br_xino = NULL;
38122+ ret = kref_put(&xi->xi_kref, au_xino_release);
38123+ }
38124+
38125+ return ret;
38126+}
38127+
38128+/* ---------------------------------------------------------------------- */
38129+
38130+/*
38131+ * xino mount option handlers
38132+ */
38133+
38134+/* xino bitmap */
38135+static void xino_clear_xib(struct super_block *sb)
38136+{
38137+ struct au_sbinfo *sbinfo;
38138+
38139+ SiMustWriteLock(sb);
38140+
38141+ sbinfo = au_sbi(sb);
38142+ if (sbinfo->si_xib)
38143+ fput(sbinfo->si_xib);
38144+ sbinfo->si_xib = NULL;
38145+ if (sbinfo->si_xib_buf)
38146+ free_page((unsigned long)sbinfo->si_xib_buf);
38147+ sbinfo->si_xib_buf = NULL;
38148+}
38149+
38150+static int au_xino_set_xib(struct super_block *sb, struct path *path)
38151+{
38152+ int err;
38153+ loff_t pos;
38154+ struct au_sbinfo *sbinfo;
38155+ struct file *file;
38156+ struct super_block *xi_sb;
38157+
38158+ SiMustWriteLock(sb);
38159+
38160+ sbinfo = au_sbi(sb);
38161+ file = au_xino_create2(sb, path, sbinfo->si_xib);
38162+ err = PTR_ERR(file);
38163+ if (IS_ERR(file))
38164+ goto out;
38165+ if (sbinfo->si_xib)
38166+ fput(sbinfo->si_xib);
38167+ sbinfo->si_xib = file;
38168+ xi_sb = file_inode(file)->i_sb;
38169+ sbinfo->si_ximaxent = xi_sb->s_maxbytes;
38170+ if (unlikely(sbinfo->si_ximaxent < PAGE_SIZE)) {
38171+ err = -EIO;
38172+ pr_err("s_maxbytes(%llu) on %s is too small\n",
38173+ (u64)sbinfo->si_ximaxent, au_sbtype(xi_sb));
38174+ goto out_unset;
38175+ }
38176+ sbinfo->si_ximaxent /= sizeof(ino_t);
38177+
38178+ err = -ENOMEM;
38179+ if (!sbinfo->si_xib_buf)
38180+ sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
38181+ if (unlikely(!sbinfo->si_xib_buf))
38182+ goto out_unset;
38183+
38184+ sbinfo->si_xib_last_pindex = 0;
38185+ sbinfo->si_xib_next_bit = 0;
38186+ if (vfsub_f_size_read(file) < PAGE_SIZE) {
38187+ pos = 0;
38188+ err = xino_fwrite(file, sbinfo->si_xib_buf, PAGE_SIZE, &pos);
38189+ if (unlikely(err != PAGE_SIZE))
38190+ goto out_free;
38191+ }
38192+ err = 0;
38193+ goto out; /* success */
38194+
38195+out_free:
38196+ if (sbinfo->si_xib_buf)
38197+ free_page((unsigned long)sbinfo->si_xib_buf);
38198+ sbinfo->si_xib_buf = NULL;
38199+ if (err >= 0)
38200+ err = -EIO;
38201+out_unset:
38202+ fput(sbinfo->si_xib);
38203+ sbinfo->si_xib = NULL;
38204+out:
38205+ AuTraceErr(err);
38206+ return err;
38207+}
38208+
38209+/* xino for each branch */
38210+static void xino_clear_br(struct super_block *sb)
38211+{
38212+ aufs_bindex_t bindex, bbot;
38213+ struct au_branch *br;
38214+
38215+ bbot = au_sbbot(sb);
38216+ for (bindex = 0; bindex <= bbot; bindex++) {
38217+ br = au_sbr(sb, bindex);
38218+ AuDebugOn(!br);
38219+ au_xino_put(br);
38220+ }
38221+}
38222+
38223+static void au_xino_set_br_shared(struct super_block *sb, struct au_branch *br,
38224+ aufs_bindex_t bshared)
38225+{
38226+ struct au_branch *brshared;
38227+
38228+ brshared = au_sbr(sb, bshared);
38229+ AuDebugOn(!brshared->br_xino);
38230+ AuDebugOn(!brshared->br_xino->xi_file);
38231+ if (br->br_xino != brshared->br_xino) {
38232+ au_xino_get(brshared);
38233+ au_xino_put(br);
38234+ br->br_xino = brshared->br_xino;
38235+ }
38236+}
38237+
38238+struct au_xino_do_set_br {
38239+ struct au_branch *br;
38240+ ino_t h_ino;
38241+ aufs_bindex_t bshared;
38242+};
38243+
38244+static int au_xino_do_set_br(struct super_block *sb, struct path *path,
38245+ struct au_xino_do_set_br *args)
38246+{
38247+ int err;
38248+ struct au_xi_calc calc;
38249+ struct file *file;
38250+ struct au_branch *br;
38251+ struct au_xi_new xinew = {
38252+ .base = path
38253+ };
38254+
38255+ br = args->br;
38256+ xinew.xi = br->br_xino;
38257+ au_xi_calc(sb, args->h_ino, &calc);
38258+ xinew.copy_src = au_xino_file(xinew.xi, calc.idx);
38259+ if (args->bshared >= 0)
38260+ /* shared xino */
38261+ au_xino_set_br_shared(sb, br, args->bshared);
38262+ else if (!xinew.xi) {
38263+ /* new xino */
38264+ err = au_xino_init(br, calc.idx, xinew.copy_src);
38265+ if (unlikely(err))
38266+ goto out;
38267+ }
38268+
38269+ /* force re-creating */
38270+ xinew.xi = br->br_xino;
38271+ xinew.idx = calc.idx;
38272+ mutex_lock(&xinew.xi->xi_mtx);
38273+ file = au_xi_new(sb, &xinew);
38274+ mutex_unlock(&xinew.xi->xi_mtx);
38275+ err = PTR_ERR(file);
38276+ if (IS_ERR(file))
38277+ goto out;
38278+ AuDebugOn(!file);
38279+
38280+ err = au_xino_do_write(file, &calc, AUFS_ROOT_INO);
38281+ if (unlikely(err))
38282+ au_xino_put(br);
38283+
38284+out:
38285+ AuTraceErr(err);
38286+ return err;
38287+}
38288+
38289+static int au_xino_set_br(struct super_block *sb, struct path *path)
38290+{
38291+ int err;
38292+ aufs_bindex_t bindex, bbot;
38293+ struct au_xino_do_set_br args;
38294+ struct inode *inode;
38295+
38296+ SiMustWriteLock(sb);
38297+
38298+ bbot = au_sbbot(sb);
38299+ inode = d_inode(sb->s_root);
38300+ for (bindex = 0; bindex <= bbot; bindex++) {
38301+ args.h_ino = au_h_iptr(inode, bindex)->i_ino;
38302+ args.br = au_sbr(sb, bindex);
38303+ args.bshared = is_sb_shared(sb, bindex, bindex - 1);
38304+ err = au_xino_do_set_br(sb, path, &args);
38305+ if (unlikely(err))
38306+ break;
38307+ }
38308+
38309+ AuTraceErr(err);
38310+ return err;
38311+}
38312+
38313+void au_xino_clr(struct super_block *sb)
38314+{
38315+ struct au_sbinfo *sbinfo;
38316+
38317+ au_xigen_clr(sb);
38318+ xino_clear_xib(sb);
38319+ xino_clear_br(sb);
38320+ dbgaufs_brs_del(sb, 0);
38321+ sbinfo = au_sbi(sb);
38322+ /* lvalue, do not call au_mntflags() */
38323+ au_opt_clr(sbinfo->si_mntflags, XINO);
38324+}
38325+
38326+int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount)
38327+{
38328+ int err, skip;
38329+ struct dentry *dentry, *parent, *cur_dentry, *cur_parent;
38330+ struct qstr *dname, *cur_name;
38331+ struct file *cur_xino;
38332+ struct au_sbinfo *sbinfo;
38333+ struct path *path, *cur_path;
38334+
38335+ SiMustWriteLock(sb);
38336+
38337+ err = 0;
38338+ sbinfo = au_sbi(sb);
38339+ path = &xiopt->file->f_path;
38340+ dentry = path->dentry;
38341+ parent = dget_parent(dentry);
38342+ if (remount) {
38343+ skip = 0;
38344+ cur_xino = sbinfo->si_xib;
38345+ if (cur_xino) {
38346+ cur_path = &cur_xino->f_path;
38347+ cur_dentry = cur_path->dentry;
38348+ cur_parent = dget_parent(cur_dentry);
38349+ cur_name = &cur_dentry->d_name;
38350+ dname = &dentry->d_name;
38351+ skip = (cur_parent == parent
38352+ && au_qstreq(dname, cur_name));
38353+ dput(cur_parent);
38354+ }
38355+ if (skip)
38356+ goto out;
38357+ }
38358+
38359+ au_opt_set(sbinfo->si_mntflags, XINO);
38360+ err = au_xino_set_xib(sb, path);
38361+ /* si_x{read,write} are set */
38362+ if (!err)
38363+ err = au_xigen_set(sb, path);
38364+ if (!err)
38365+ err = au_xino_set_br(sb, path);
38366+ if (!err) {
38367+ dbgaufs_brs_add(sb, 0, /*topdown*/1);
38368+ goto out; /* success */
38369+ }
38370+
38371+ /* reset all */
38372+ AuIOErr("failed setting xino(%d).\n", err);
38373+ au_xino_clr(sb);
38374+
38375+out:
38376+ dput(parent);
38377+ return err;
38378+}
38379+
38380+/*
38381+ * create a xinofile at the default place/path.
38382+ */
38383+struct file *au_xino_def(struct super_block *sb)
38384+{
38385+ struct file *file;
38386+ char *page, *p;
38387+ struct au_branch *br;
38388+ struct super_block *h_sb;
38389+ struct path path;
38390+ aufs_bindex_t bbot, bindex, bwr;
38391+
38392+ br = NULL;
38393+ bbot = au_sbbot(sb);
38394+ bwr = -1;
38395+ for (bindex = 0; bindex <= bbot; bindex++) {
38396+ br = au_sbr(sb, bindex);
38397+ if (au_br_writable(br->br_perm)
38398+ && !au_test_fs_bad_xino(au_br_sb(br))) {
38399+ bwr = bindex;
38400+ break;
38401+ }
38402+ }
38403+
38404+ if (bwr >= 0) {
38405+ file = ERR_PTR(-ENOMEM);
38406+ page = (void *)__get_free_page(GFP_NOFS);
38407+ if (unlikely(!page))
38408+ goto out;
38409+ path.mnt = au_br_mnt(br);
38410+ path.dentry = au_h_dptr(sb->s_root, bwr);
38411+ p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
38412+ file = (void *)p;
38413+ if (!IS_ERR(p)) {
38414+ strcat(p, "/" AUFS_XINO_FNAME);
38415+ AuDbg("%s\n", p);
38416+ file = au_xino_create(sb, p, /*silent*/0, /*wbrtop*/1);
38417+ }
38418+ free_page((unsigned long)page);
38419+ } else {
38420+ file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0,
38421+ /*wbrtop*/0);
38422+ if (IS_ERR(file))
38423+ goto out;
38424+ h_sb = file->f_path.dentry->d_sb;
38425+ if (unlikely(au_test_fs_bad_xino(h_sb))) {
38426+ pr_err("xino doesn't support %s(%s)\n",
38427+ AUFS_XINO_DEFPATH, au_sbtype(h_sb));
38428+ fput(file);
38429+ file = ERR_PTR(-EINVAL);
38430+ }
38431+ }
38432+
38433+out:
38434+ return file;
38435+}
38436+
38437+/* ---------------------------------------------------------------------- */
38438+
38439+/*
38440+ * initialize the xinofile for the specified branch @br
38441+ * at the place/path where @base_file indicates.
38442+ * test whether another branch is on the same filesystem or not,
38443+ * if found then share the xinofile with another branch.
38444+ */
38445+int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
38446+ struct path *base)
38447+{
38448+ int err;
38449+ struct au_xino_do_set_br args = {
38450+ .h_ino = h_ino,
38451+ .br = br
38452+ };
38453+
38454+ args.bshared = sbr_find_shared(sb, /*btop*/0, au_sbbot(sb),
38455+ au_br_sb(br));
38456+ err = au_xino_do_set_br(sb, base, &args);
38457+ if (unlikely(err))
38458+ au_xino_put(br);
38459+
38460+ return err;
38461+}
38462+
38463+/* ---------------------------------------------------------------------- */
38464+
38465+/*
38466+ * get an unused inode number from bitmap
38467+ */
38468+ino_t au_xino_new_ino(struct super_block *sb)
38469+{
38470+ ino_t ino;
38471+ unsigned long *p, pindex, ul, pend;
38472+ struct au_sbinfo *sbinfo;
38473+ struct file *file;
38474+ int free_bit, err;
38475+
38476+ if (!au_opt_test(au_mntflags(sb), XINO))
38477+ return iunique(sb, AUFS_FIRST_INO);
38478+
38479+ sbinfo = au_sbi(sb);
38480+ mutex_lock(&sbinfo->si_xib_mtx);
38481+ p = sbinfo->si_xib_buf;
38482+ free_bit = sbinfo->si_xib_next_bit;
38483+ if (free_bit < page_bits && !test_bit(free_bit, p))
38484+ goto out; /* success */
38485+ free_bit = find_first_zero_bit(p, page_bits);
38486+ if (free_bit < page_bits)
38487+ goto out; /* success */
38488+
38489+ pindex = sbinfo->si_xib_last_pindex;
38490+ for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
38491+ err = xib_pindex(sb, ul);
38492+ if (unlikely(err))
38493+ goto out_err;
38494+ free_bit = find_first_zero_bit(p, page_bits);
38495+ if (free_bit < page_bits)
38496+ goto out; /* success */
38497+ }
38498+
38499+ file = sbinfo->si_xib;
38500+ pend = vfsub_f_size_read(file) / PAGE_SIZE;
38501+ for (ul = pindex + 1; ul <= pend; ul++) {
38502+ err = xib_pindex(sb, ul);
38503+ if (unlikely(err))
38504+ goto out_err;
38505+ free_bit = find_first_zero_bit(p, page_bits);
38506+ if (free_bit < page_bits)
38507+ goto out; /* success */
38508+ }
38509+ BUG();
38510+
38511+out:
38512+ set_bit(free_bit, p);
38513+ sbinfo->si_xib_next_bit = free_bit + 1;
38514+ pindex = sbinfo->si_xib_last_pindex;
38515+ mutex_unlock(&sbinfo->si_xib_mtx);
38516+ ino = xib_calc_ino(pindex, free_bit);
38517+ AuDbg("i%lu\n", (unsigned long)ino);
38518+ return ino;
38519+out_err:
38520+ mutex_unlock(&sbinfo->si_xib_mtx);
38521+ AuDbg("i0\n");
38522+ return 0;
38523+}
38524+
38525+/* for s_op->delete_inode() */
38526+void au_xino_delete_inode(struct inode *inode, const int unlinked)
38527+{
38528+ int err;
38529+ unsigned int mnt_flags;
38530+ aufs_bindex_t bindex, bbot, bi;
38531+ unsigned char try_trunc;
38532+ struct au_iinfo *iinfo;
38533+ struct super_block *sb;
38534+ struct au_hinode *hi;
38535+ struct inode *h_inode;
38536+ struct au_branch *br;
38537+ struct au_xi_calc calc;
38538+ struct file *file;
38539+
38540+ AuDebugOn(au_is_bad_inode(inode));
38541+
38542+ sb = inode->i_sb;
38543+ mnt_flags = au_mntflags(sb);
38544+ if (!au_opt_test(mnt_flags, XINO)
38545+ || inode->i_ino == AUFS_ROOT_INO)
38546+ return;
38547+
38548+ if (unlinked) {
38549+ au_xigen_inc(inode);
38550+ au_xib_clear_bit(inode);
38551+ }
38552+
38553+ iinfo = au_ii(inode);
38554+ bindex = iinfo->ii_btop;
38555+ if (bindex < 0)
38556+ return;
38557+
38558+ try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
38559+ hi = au_hinode(iinfo, bindex);
38560+ bbot = iinfo->ii_bbot;
38561+ for (; bindex <= bbot; bindex++, hi++) {
38562+ h_inode = hi->hi_inode;
38563+ if (!h_inode
38564+ || (!unlinked && h_inode->i_nlink))
38565+ continue;
38566+
38567+ /* inode may not be revalidated */
38568+ bi = au_br_index(sb, hi->hi_id);
38569+ if (bi < 0)
38570+ continue;
38571+
38572+ br = au_sbr(sb, bi);
38573+ au_xi_calc(sb, h_inode->i_ino, &calc);
38574+ file = au_xino_file(br->br_xino, calc.idx);
38575+ if (IS_ERR_OR_NULL(file))
38576+ continue;
38577+
38578+ err = au_xino_do_write(file, &calc, /*ino*/0);
38579+ if (!err && try_trunc
38580+ && au_test_fs_trunc_xino(au_br_sb(br)))
38581+ xino_try_trunc(sb, br);
38582+ }
38583+}
38584+
38585+/* ---------------------------------------------------------------------- */
38586+
38587+static int au_xinondir_find(struct au_xino *xi, ino_t h_ino)
38588+{
38589+ int found, total, i;
38590+
38591+ found = -1;
38592+ total = xi->xi_nondir.total;
38593+ for (i = 0; i < total; i++) {
38594+ if (xi->xi_nondir.array[i] != h_ino)
38595+ continue;
38596+ found = i;
38597+ break;
38598+ }
38599+
38600+ return found;
38601+}
38602+
38603+static int au_xinondir_expand(struct au_xino *xi)
38604+{
38605+ int err, sz;
38606+ ino_t *p;
38607+
38608+ BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
38609+
38610+ err = -ENOMEM;
38611+ sz = xi->xi_nondir.total * sizeof(ino_t);
38612+ if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
38613+ goto out;
38614+ p = au_kzrealloc(xi->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
38615+ /*may_shrink*/0);
38616+ if (p) {
38617+ xi->xi_nondir.array = p;
38618+ xi->xi_nondir.total <<= 1;
38619+ AuDbg("xi_nondir.total %d\n", xi->xi_nondir.total);
38620+ err = 0;
38621+ }
38622+
38623+out:
38624+ return err;
38625+}
38626+
38627+void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
38628+ ino_t h_ino, int idx)
38629+{
38630+ struct au_xino *xi;
38631+
38632+ AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
38633+ xi = au_sbr(sb, bindex)->br_xino;
38634+ AuDebugOn(idx < 0 || xi->xi_nondir.total <= idx);
38635+
38636+ spin_lock(&xi->xi_nondir.spin);
38637+ AuDebugOn(xi->xi_nondir.array[idx] != h_ino);
38638+ xi->xi_nondir.array[idx] = 0;
38639+ spin_unlock(&xi->xi_nondir.spin);
38640+ wake_up_all(&xi->xi_nondir.wqh);
38641+}
38642+
38643+int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
38644+ int *idx)
38645+{
38646+ int err, found, empty;
38647+ struct au_xino *xi;
38648+
38649+ err = 0;
38650+ *idx = -1;
38651+ if (!au_opt_test(au_mntflags(sb), XINO))
38652+ goto out; /* no xino */
38653+
38654+ xi = au_sbr(sb, bindex)->br_xino;
38655+
38656+again:
38657+ spin_lock(&xi->xi_nondir.spin);
38658+ found = au_xinondir_find(xi, h_ino);
38659+ if (found == -1) {
38660+ empty = au_xinondir_find(xi, /*h_ino*/0);
38661+ if (empty == -1) {
38662+ empty = xi->xi_nondir.total;
38663+ err = au_xinondir_expand(xi);
38664+ if (unlikely(err))
38665+ goto out_unlock;
38666+ }
38667+ xi->xi_nondir.array[empty] = h_ino;
38668+ *idx = empty;
38669+ } else {
38670+ spin_unlock(&xi->xi_nondir.spin);
38671+ wait_event(xi->xi_nondir.wqh,
38672+ xi->xi_nondir.array[found] != h_ino);
38673+ goto again;
38674+ }
38675+
38676+out_unlock:
38677+ spin_unlock(&xi->xi_nondir.spin);
38678+out:
38679+ return err;
38680+}
38681+
38682+/* ---------------------------------------------------------------------- */
38683+
38684+int au_xino_path(struct seq_file *seq, struct file *file)
38685+{
38686+ int err;
38687+
38688+ err = au_seq_path(seq, &file->f_path);
38689+ if (unlikely(err))
38690+ goto out;
38691+
38692+#define Deleted "\\040(deleted)"
38693+ seq->count -= sizeof(Deleted) - 1;
38694+ AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
38695+ sizeof(Deleted) - 1));
38696+#undef Deleted
38697+
38698+out:
38699+ return err;
38700+}
38701diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
38702--- /usr/share/empty/include/uapi/linux/aufs_type.h 1970-01-01 01:00:00.000000000 +0100
38703+++ linux/include/uapi/linux/aufs_type.h 2021-06-30 21:35:11.397206648 +0200
38704@@ -0,0 +1,452 @@
38705+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
38706+/*
38707+ * Copyright (C) 2005-2020 Junjiro R. Okajima
38708+ *
38709+ * This program, aufs is free software; you can redistribute it and/or modify
38710+ * it under the terms of the GNU General Public License as published by
38711+ * the Free Software Foundation; either version 2 of the License, or
38712+ * (at your option) any later version.
38713+ *
38714+ * This program is distributed in the hope that it will be useful,
38715+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
38716+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38717+ * GNU General Public License for more details.
38718+ *
38719+ * You should have received a copy of the GNU General Public License
38720+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
38721+ */
38722+
38723+#ifndef __AUFS_TYPE_H__
38724+#define __AUFS_TYPE_H__
38725+
38726+#define AUFS_NAME "aufs"
38727+
38728+#ifdef __KERNEL__
38729+/*
38730+ * define it before including all other headers.
38731+ * sched.h may use pr_* macros before defining "current", so define the
38732+ * no-current version first, and re-define later.
38733+ */
38734+#define pr_fmt(fmt) AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
38735+#include <linux/sched.h>
38736+#undef pr_fmt
38737+#define pr_fmt(fmt) \
38738+ AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
38739+ (int)sizeof(current->comm), current->comm, current->pid
38740+#include <linux/limits.h>
38741+#else
38742+#include <stdint.h>
38743+#include <sys/types.h>
38744+#include <limits.h>
38745+#endif /* __KERNEL__ */
38746+
38747+#define AUFS_VERSION "5.x-rcN-20210517"
38748+
38749+/* todo? move this to linux-2.6.19/include/magic.h */
38750+#define AUFS_SUPER_MAGIC ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
38751+
38752+/* ---------------------------------------------------------------------- */
38753+
38754+#ifdef __KERNEL__
38755+#ifdef CONFIG_AUFS_BRANCH_MAX_127
38756+typedef int8_t aufs_bindex_t;
38757+#define AUFS_BRANCH_MAX 127
38758+#else
38759+typedef int16_t aufs_bindex_t;
38760+#ifdef CONFIG_AUFS_BRANCH_MAX_511
38761+#define AUFS_BRANCH_MAX 511
38762+#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
38763+#define AUFS_BRANCH_MAX 1023
38764+#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
38765+#define AUFS_BRANCH_MAX 32767
38766+#endif
38767+#endif
38768+
38769+#ifndef AUFS_BRANCH_MAX
38770+#error unknown CONFIG_AUFS_BRANCH_MAX value
38771+#endif
38772+#endif /* __KERNEL__ */
38773+
38774+/* ---------------------------------------------------------------------- */
38775+
38776+#define AUFS_FSTYPE AUFS_NAME
38777+
38778+#define AUFS_ROOT_INO 2
38779+#define AUFS_FIRST_INO 11
38780+
38781+#define AUFS_WH_PFX ".wh."
38782+#define AUFS_WH_PFX_LEN ((int)sizeof(AUFS_WH_PFX) - 1)
38783+#define AUFS_WH_TMP_LEN 4
38784+/* a limit for rmdir/rename a dir and copyup */
38785+#define AUFS_MAX_NAMELEN (NAME_MAX \
38786+ - AUFS_WH_PFX_LEN * 2 /* doubly whiteouted */\
38787+ - 1 /* dot */\
38788+ - AUFS_WH_TMP_LEN) /* hex */
38789+#define AUFS_XINO_FNAME "." AUFS_NAME ".xino"
38790+#define AUFS_XINO_DEFPATH "/tmp/" AUFS_XINO_FNAME
38791+#define AUFS_XINO_DEF_SEC 30 /* seconds */
38792+#define AUFS_XINO_DEF_TRUNC 45 /* percentage */
38793+#define AUFS_DIRWH_DEF 3
38794+#define AUFS_RDCACHE_DEF 10 /* seconds */
38795+#define AUFS_RDCACHE_MAX 3600 /* seconds */
38796+#define AUFS_RDBLK_DEF 512 /* bytes */
38797+#define AUFS_RDHASH_DEF 32
38798+#define AUFS_WKQ_NAME AUFS_NAME "d"
38799+#define AUFS_MFS_DEF_SEC 30 /* seconds */
38800+#define AUFS_MFS_MAX_SEC 3600 /* seconds */
38801+#define AUFS_FHSM_CACHE_DEF_SEC 30 /* seconds */
38802+#define AUFS_PLINK_WARN 50 /* number of plinks in a single bucket */
38803+
38804+/* pseudo-link maintenace under /proc */
38805+#define AUFS_PLINK_MAINT_NAME "plink_maint"
38806+#define AUFS_PLINK_MAINT_DIR "fs/" AUFS_NAME
38807+#define AUFS_PLINK_MAINT_PATH AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
38808+
38809+/* dirren, renamed dir */
38810+#define AUFS_DR_INFO_PFX AUFS_WH_PFX ".dr."
38811+#define AUFS_DR_BRHINO_NAME AUFS_WH_PFX "hino"
38812+/* whiteouted doubly */
38813+#define AUFS_WH_DR_INFO_PFX AUFS_WH_PFX AUFS_DR_INFO_PFX
38814+#define AUFS_WH_DR_BRHINO AUFS_WH_PFX AUFS_DR_BRHINO_NAME
38815+
38816+#define AUFS_DIROPQ_NAME AUFS_WH_PFX ".opq" /* whiteouted doubly */
38817+#define AUFS_WH_DIROPQ AUFS_WH_PFX AUFS_DIROPQ_NAME
38818+
38819+#define AUFS_BASE_NAME AUFS_WH_PFX AUFS_NAME
38820+#define AUFS_PLINKDIR_NAME AUFS_WH_PFX "plnk"
38821+#define AUFS_ORPHDIR_NAME AUFS_WH_PFX "orph"
38822+
38823+/* doubly whiteouted */
38824+#define AUFS_WH_BASE AUFS_WH_PFX AUFS_BASE_NAME
38825+#define AUFS_WH_PLINKDIR AUFS_WH_PFX AUFS_PLINKDIR_NAME
38826+#define AUFS_WH_ORPHDIR AUFS_WH_PFX AUFS_ORPHDIR_NAME
38827+
38828+/* branch permissions and attributes */
38829+#define AUFS_BRPERM_RW "rw"
38830+#define AUFS_BRPERM_RO "ro"
38831+#define AUFS_BRPERM_RR "rr"
38832+#define AUFS_BRATTR_COO_REG "coo_reg"
38833+#define AUFS_BRATTR_COO_ALL "coo_all"
38834+#define AUFS_BRATTR_FHSM "fhsm"
38835+#define AUFS_BRATTR_UNPIN "unpin"
38836+#define AUFS_BRATTR_ICEX "icex"
38837+#define AUFS_BRATTR_ICEX_SEC "icexsec"
38838+#define AUFS_BRATTR_ICEX_SYS "icexsys"
38839+#define AUFS_BRATTR_ICEX_TR "icextr"
38840+#define AUFS_BRATTR_ICEX_USR "icexusr"
38841+#define AUFS_BRATTR_ICEX_OTH "icexoth"
38842+#define AUFS_BRRATTR_WH "wh"
38843+#define AUFS_BRWATTR_NLWH "nolwh"
38844+#define AUFS_BRWATTR_MOO "moo"
38845+
38846+#define AuBrPerm_RW 1 /* writable, hardlinkable wh */
38847+#define AuBrPerm_RO (1 << 1) /* readonly */
38848+#define AuBrPerm_RR (1 << 2) /* natively readonly */
38849+#define AuBrPerm_Mask (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
38850+
38851+#define AuBrAttr_COO_REG (1 << 3) /* copy-up on open */
38852+#define AuBrAttr_COO_ALL (1 << 4)
38853+#define AuBrAttr_COO_Mask (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
38854+
38855+#define AuBrAttr_FHSM (1 << 5) /* file-based hsm */
38856+#define AuBrAttr_UNPIN (1 << 6) /* rename-able top dir of
38857+ branch. meaningless since
38858+ linux-3.18-rc1 */
38859+
38860+/* ignore error in copying XATTR */
38861+#define AuBrAttr_ICEX_SEC (1 << 7)
38862+#define AuBrAttr_ICEX_SYS (1 << 8)
38863+#define AuBrAttr_ICEX_TR (1 << 9)
38864+#define AuBrAttr_ICEX_USR (1 << 10)
38865+#define AuBrAttr_ICEX_OTH (1 << 11)
38866+#define AuBrAttr_ICEX (AuBrAttr_ICEX_SEC \
38867+ | AuBrAttr_ICEX_SYS \
38868+ | AuBrAttr_ICEX_TR \
38869+ | AuBrAttr_ICEX_USR \
38870+ | AuBrAttr_ICEX_OTH)
38871+
38872+#define AuBrRAttr_WH (1 << 12) /* whiteout-able */
38873+#define AuBrRAttr_Mask AuBrRAttr_WH
38874+
38875+#define AuBrWAttr_NoLinkWH (1 << 13) /* un-hardlinkable whiteouts */
38876+#define AuBrWAttr_MOO (1 << 14) /* move-up on open */
38877+#define AuBrWAttr_Mask (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
38878+
38879+#define AuBrAttr_CMOO_Mask (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
38880+
38881+/* #warning test userspace */
38882+#ifdef __KERNEL__
38883+#ifndef CONFIG_AUFS_FHSM
38884+#undef AuBrAttr_FHSM
38885+#define AuBrAttr_FHSM 0
38886+#endif
38887+#ifndef CONFIG_AUFS_XATTR
38888+#undef AuBrAttr_ICEX
38889+#define AuBrAttr_ICEX 0
38890+#undef AuBrAttr_ICEX_SEC
38891+#define AuBrAttr_ICEX_SEC 0
38892+#undef AuBrAttr_ICEX_SYS
38893+#define AuBrAttr_ICEX_SYS 0
38894+#undef AuBrAttr_ICEX_TR
38895+#define AuBrAttr_ICEX_TR 0
38896+#undef AuBrAttr_ICEX_USR
38897+#define AuBrAttr_ICEX_USR 0
38898+#undef AuBrAttr_ICEX_OTH
38899+#define AuBrAttr_ICEX_OTH 0
38900+#endif
38901+#endif
38902+
38903+/* the longest combination */
38904+/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
38905+#define AuBrPermStrSz sizeof(AUFS_BRPERM_RW \
38906+ "+" AUFS_BRATTR_COO_REG \
38907+ "+" AUFS_BRATTR_FHSM \
38908+ "+" AUFS_BRATTR_UNPIN \
38909+ "+" AUFS_BRATTR_ICEX_SEC \
38910+ "+" AUFS_BRATTR_ICEX_SYS \
38911+ "+" AUFS_BRATTR_ICEX_USR \
38912+ "+" AUFS_BRATTR_ICEX_OTH \
38913+ "+" AUFS_BRWATTR_NLWH)
38914+
38915+typedef struct {
38916+ char a[AuBrPermStrSz];
38917+} au_br_perm_str_t;
38918+
38919+static inline int au_br_writable(int brperm)
38920+{
38921+ return brperm & AuBrPerm_RW;
38922+}
38923+
38924+static inline int au_br_whable(int brperm)
38925+{
38926+ return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
38927+}
38928+
38929+static inline int au_br_wh_linkable(int brperm)
38930+{
38931+ return !(brperm & AuBrWAttr_NoLinkWH);
38932+}
38933+
38934+static inline int au_br_cmoo(int brperm)
38935+{
38936+ return brperm & AuBrAttr_CMOO_Mask;
38937+}
38938+
38939+static inline int au_br_fhsm(int brperm)
38940+{
38941+ return brperm & AuBrAttr_FHSM;
38942+}
38943+
38944+/* ---------------------------------------------------------------------- */
38945+
38946+/* ioctl */
38947+enum {
38948+ /* readdir in userspace */
38949+ AuCtl_RDU,
38950+ AuCtl_RDU_INO,
38951+
38952+ AuCtl_WBR_FD, /* pathconf wrapper */
38953+ AuCtl_IBUSY, /* busy inode */
38954+ AuCtl_MVDOWN, /* move-down */
38955+ AuCtl_BR, /* info about branches */
38956+ AuCtl_FHSM_FD /* connection for fhsm */
38957+};
38958+
38959+/* borrowed from linux/include/linux/kernel.h */
38960+#ifndef ALIGN
38961+#ifdef _GNU_SOURCE
38962+#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
38963+#else
38964+#define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
38965+#endif
38966+#define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
38967+#endif
38968+
38969+/* borrowed from linux/include/linux/compiler-gcc3.h */
38970+#ifndef __aligned
38971+#define __aligned(x) __attribute__((aligned(x)))
38972+#endif
38973+
38974+#ifdef __KERNEL__
38975+#ifndef __packed
38976+#define __packed __attribute__((packed))
38977+#endif
38978+#endif
38979+
38980+struct au_rdu_cookie {
38981+ uint64_t h_pos;
38982+ int16_t bindex;
38983+ uint8_t flags;
38984+ uint8_t pad;
38985+ uint32_t generation;
38986+} __aligned(8);
38987+
38988+struct au_rdu_ent {
38989+ uint64_t ino;
38990+ int16_t bindex;
38991+ uint8_t type;
38992+ uint8_t nlen;
38993+ uint8_t wh;
38994+ char name[];
38995+} __aligned(8);
38996+
38997+static inline int au_rdu_len(int nlen)
38998+{
38999+ /* include the terminating NULL */
39000+ return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
39001+ sizeof(uint64_t));
39002+}
39003+
39004+union au_rdu_ent_ul {
39005+ struct au_rdu_ent __user *e;
39006+ uint64_t ul;
39007+};
39008+
39009+enum {
39010+ AufsCtlRduV_SZ,
39011+ AufsCtlRduV_End
39012+};
39013+
39014+struct aufs_rdu {
39015+ /* input */
39016+ union {
39017+ uint64_t sz; /* AuCtl_RDU */
39018+ uint64_t nent; /* AuCtl_RDU_INO */
39019+ };
39020+ union au_rdu_ent_ul ent;
39021+ uint16_t verify[AufsCtlRduV_End];
39022+
39023+ /* input/output */
39024+ uint32_t blk;
39025+
39026+ /* output */
39027+ union au_rdu_ent_ul tail;
39028+ /* number of entries which were added in a single call */
39029+ uint64_t rent;
39030+ uint8_t full;
39031+ uint8_t shwh;
39032+
39033+ struct au_rdu_cookie cookie;
39034+} __aligned(8);
39035+
39036+/* ---------------------------------------------------------------------- */
39037+
39038+/* dirren. the branch is identified by the filename who contains this */
39039+struct au_drinfo {
39040+ uint64_t ino;
39041+ union {
39042+ uint8_t oldnamelen;
39043+ uint64_t _padding;
39044+ };
39045+ uint8_t oldname[];
39046+} __aligned(8);
39047+
39048+struct au_drinfo_fdata {
39049+ uint32_t magic;
39050+ struct au_drinfo drinfo;
39051+} __aligned(8);
39052+
39053+#define AUFS_DRINFO_MAGIC_V1 ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x01)
39054+/* future */
39055+#define AUFS_DRINFO_MAGIC_V2 ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x02)
39056+
39057+/* ---------------------------------------------------------------------- */
39058+
39059+struct aufs_wbr_fd {
39060+ uint32_t oflags;
39061+ int16_t brid;
39062+} __aligned(8);
39063+
39064+/* ---------------------------------------------------------------------- */
39065+
39066+struct aufs_ibusy {
39067+ uint64_t ino, h_ino;
39068+ int16_t bindex;
39069+} __aligned(8);
39070+
39071+/* ---------------------------------------------------------------------- */
39072+
39073+/* error code for move-down */
39074+/* the actual message strings are implemented in aufs-util.git */
39075+enum {
39076+ EAU_MVDOWN_OPAQUE = 1,
39077+ EAU_MVDOWN_WHITEOUT,
39078+ EAU_MVDOWN_UPPER,
39079+ EAU_MVDOWN_BOTTOM,
39080+ EAU_MVDOWN_NOUPPER,
39081+ EAU_MVDOWN_NOLOWERBR,
39082+ EAU_Last
39083+};
39084+
39085+/* flags for move-down */
39086+#define AUFS_MVDOWN_DMSG 1
39087+#define AUFS_MVDOWN_OWLOWER (1 << 1) /* overwrite lower */
39088+#define AUFS_MVDOWN_KUPPER (1 << 2) /* keep upper */
39089+#define AUFS_MVDOWN_ROLOWER (1 << 3) /* do even if lower is RO */
39090+#define AUFS_MVDOWN_ROLOWER_R (1 << 4) /* did on lower RO */
39091+#define AUFS_MVDOWN_ROUPPER (1 << 5) /* do even if upper is RO */
39092+#define AUFS_MVDOWN_ROUPPER_R (1 << 6) /* did on upper RO */
39093+#define AUFS_MVDOWN_BRID_UPPER (1 << 7) /* upper brid */
39094+#define AUFS_MVDOWN_BRID_LOWER (1 << 8) /* lower brid */
39095+#define AUFS_MVDOWN_FHSM_LOWER (1 << 9) /* find fhsm attr for lower */
39096+#define AUFS_MVDOWN_STFS (1 << 10) /* req. stfs */
39097+#define AUFS_MVDOWN_STFS_FAILED (1 << 11) /* output: stfs is unusable */
39098+#define AUFS_MVDOWN_BOTTOM (1 << 12) /* output: no more lowers */
39099+
39100+/* index for move-down */
39101+enum {
39102+ AUFS_MVDOWN_UPPER,
39103+ AUFS_MVDOWN_LOWER,
39104+ AUFS_MVDOWN_NARRAY
39105+};
39106+
39107+/*
39108+ * additional info of move-down
39109+ * number of free blocks and inodes.
39110+ * subset of struct kstatfs, but smaller and always 64bit.
39111+ */
39112+struct aufs_stfs {
39113+ uint64_t f_blocks;
39114+ uint64_t f_bavail;
39115+ uint64_t f_files;
39116+ uint64_t f_ffree;
39117+};
39118+
39119+struct aufs_stbr {
39120+ int16_t brid; /* optional input */
39121+ int16_t bindex; /* output */
39122+ struct aufs_stfs stfs; /* output when AUFS_MVDOWN_STFS set */
39123+} __aligned(8);
39124+
39125+struct aufs_mvdown {
39126+ uint32_t flags; /* input/output */
39127+ struct aufs_stbr stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
39128+ int8_t au_errno; /* output */
39129+} __aligned(8);
39130+
39131+/* ---------------------------------------------------------------------- */
39132+
39133+union aufs_brinfo {
39134+ /* PATH_MAX may differ between kernel-space and user-space */
39135+ char _spacer[4096];
39136+ struct {
39137+ int16_t id;
39138+ int perm;
39139+ char path[];
39140+ };
39141+} __aligned(8);
39142+
39143+/* ---------------------------------------------------------------------- */
39144+
39145+#define AuCtlType 'A'
39146+#define AUFS_CTL_RDU _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
39147+#define AUFS_CTL_RDU_INO _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
39148+#define AUFS_CTL_WBR_FD _IOW(AuCtlType, AuCtl_WBR_FD, \
39149+ struct aufs_wbr_fd)
39150+#define AUFS_CTL_IBUSY _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
39151+#define AUFS_CTL_MVDOWN _IOWR(AuCtlType, AuCtl_MVDOWN, \
39152+ struct aufs_mvdown)
39153+#define AUFS_CTL_BRINFO _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
39154+#define AUFS_CTL_FHSM_FD _IOW(AuCtlType, AuCtl_FHSM_FD, int)
39155+
39156+#endif /* __AUFS_TYPE_H__ */
39157SPDX-License-Identifier: GPL-2.0
39158aufs5.x-rcN loopback patch
39159
39160diff --git a/drivers/block/loop.c b/drivers/block/loop.c
39161index 794a8a1341989..0e5664a6753e5 100644
39162--- a/drivers/block/loop.c
39163+++ b/drivers/block/loop.c
39164@@ -647,6 +647,15 @@ static inline void loop_update_dio(struct loop_device *lo)
39165 lo->use_dio);
39166 }
39167
39168+static struct file *loop_real_file(struct file *file)
39169+{
39170+ struct file *f = NULL;
39171+
39172+ if (file->f_path.dentry->d_sb->s_op->real_loop)
39173+ f = file->f_path.dentry->d_sb->s_op->real_loop(file);
39174+ return f;
39175+}
39176+
39177 static void loop_reread_partitions(struct loop_device *lo,
39178 struct block_device *bdev)
39179 {
39180@@ -702,6 +711,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39181 unsigned int arg)
39182 {
39183 struct file *file = NULL, *old_file;
39184+ struct file *f, *virt_file = NULL, *old_virt_file;
39185 int error;
39186 bool partscan;
39187
39188@@ -721,12 +731,19 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39189 file = fget(arg);
39190 if (!file)
39191 goto out_err;
39192+ f = loop_real_file(file);
39193+ if (f) {
39194+ virt_file = file;
39195+ file = f;
39196+ get_file(file);
39197+ }
39198
39199 error = loop_validate_file(file, bdev);
39200 if (error)
39201 goto out_err;
39202
39203 old_file = lo->lo_backing_file;
39204+ old_virt_file = lo->lo_backing_virt_file;
39205
39206 error = -EINVAL;
39207
39208@@ -738,6 +755,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39209 blk_mq_freeze_queue(lo->lo_queue);
39210 mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
39211 lo->lo_backing_file = file;
39212+ lo->lo_backing_virt_file = virt_file;
39213 lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
39214 mapping_set_gfp_mask(file->f_mapping,
39215 lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39216@@ -751,6 +769,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39217 * dependency.
39218 */
39219 fput(old_file);
39220+ if (old_virt_file)
39221+ fput(old_virt_file);
39222 if (partscan)
39223 loop_reread_partitions(lo, bdev);
39224 return 0;
39225@@ -759,6 +779,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39226 mutex_unlock(&lo->lo_mutex);
39227 if (file)
39228 fput(file);
39229+ if (virt_file)
39230+ fput(virt_file);
39231 return error;
39232 }
39233
39234@@ -1085,7 +1107,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39235 struct block_device *bdev,
39236 const struct loop_config *config)
39237 {
39238- struct file *file;
39239+ struct file *file, *f, *virt_file = NULL;
39240 struct inode *inode;
39241 struct address_space *mapping;
39242 int error;
39243@@ -1100,6 +1122,12 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39244 file = fget(config->fd);
39245 if (!file)
39246 goto out;
39247+ f = loop_real_file(file);
39248+ if (f) {
39249+ virt_file = file;
39250+ file = f;
39251+ get_file(file);
39252+ }
39253
39254 /*
39255 * If we don't hold exclusive handle for the device, upgrade to it
39256@@ -1154,6 +1182,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39257 lo->use_dio = lo->lo_flags & LO_FLAGS_DIRECT_IO;
39258 lo->lo_device = bdev;
39259 lo->lo_backing_file = file;
39260+ lo->lo_backing_virt_file = virt_file;
39261 lo->old_gfp_mask = mapping_gfp_mask(mapping);
39262 mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39263
39264@@ -1204,6 +1233,8 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39265 bd_abort_claiming(bdev, loop_configure);
39266 out_putf:
39267 fput(file);
39268+ if (virt_file)
39269+ fput(virt_file);
39270 out:
39271 /* This is safe: open() is still holding a reference. */
39272 module_put(THIS_MODULE);
39273@@ -1213,6 +1244,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39274 static int __loop_clr_fd(struct loop_device *lo, bool release)
39275 {
39276 struct file *filp = NULL;
39277+ struct file *virt_filp = lo->lo_backing_virt_file;
39278 gfp_t gfp = lo->old_gfp_mask;
39279 struct block_device *bdev = lo->lo_device;
39280 int err = 0;
39281@@ -1239,6 +1271,7 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
39282
39283 spin_lock_irq(&lo->lo_lock);
39284 lo->lo_backing_file = NULL;
39285+ lo->lo_backing_virt_file = NULL;
39286 spin_unlock_irq(&lo->lo_lock);
39287
39288 loop_release_xfer(lo);
39289@@ -1320,6 +1353,8 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
39290 */
39291 if (filp)
39292 fput(filp);
39293+ if (virt_filp)
39294+ fput(virt_filp);
39295 return err;
39296 }
39297
39298diff --git a/drivers/block/loop.h b/drivers/block/loop.h
39299index a3c04f310672e..161c3c5d1c22b 100644
39300--- a/drivers/block/loop.h
39301+++ b/drivers/block/loop.h
39302@@ -46,7 +46,7 @@ struct loop_device {
39303 int (*ioctl)(struct loop_device *, int cmd,
39304 unsigned long arg);
39305
39306- struct file * lo_backing_file;
39307+ struct file *lo_backing_file, *lo_backing_virt_file;
39308 struct block_device *lo_device;
39309 void *key_data;
39310
39311diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
39312index 9f58ba0cb769f..1c2267a5a2ae1 100644
39313--- a/fs/aufs/f_op.c
39314+++ b/fs/aufs/f_op.c
39315@@ -304,7 +304,7 @@ static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
39316 if (IS_ERR(h_file))
39317 goto out;
39318
39319- if (au_test_loopback_kthread()) {
39320+ if (0 && au_test_loopback_kthread()) {
39321 au_warn_loopback(h_file->f_path.dentry->d_sb);
39322 if (file->f_mapping != h_file->f_mapping) {
39323 file->f_mapping = h_file->f_mapping;
39324diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
39325index a8b63acc62045..9d97c3af5686a 100644
39326--- a/fs/aufs/loop.c
39327+++ b/fs/aufs/loop.c
39328@@ -133,3 +133,19 @@ void au_loopback_fin(void)
39329 symbol_put(loop_backing_file);
39330 au_kfree_try_rcu(au_warn_loopback_array);
39331 }
39332+
39333+/* ---------------------------------------------------------------------- */
39334+
39335+/* support the loopback block device insude aufs */
39336+
39337+struct file *aufs_real_loop(struct file *file)
39338+{
39339+ struct file *f;
39340+
39341+ BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
39342+ fi_read_lock(file);
39343+ f = au_hf_top(file);
39344+ fi_read_unlock(file);
39345+ AuDebugOn(!f);
39346+ return f;
39347+}
39348diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
39349index 94f4f80ae33bf..ca1194354aff4 100644
39350--- a/fs/aufs/loop.h
39351+++ b/fs/aufs/loop.h
39352@@ -26,6 +26,8 @@ void au_warn_loopback(struct super_block *h_sb);
39353
39354 int au_loopback_init(void);
39355 void au_loopback_fin(void);
39356+
39357+struct file *aufs_real_loop(struct file *file);
39358 #else
39359 AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
39360
39361@@ -36,6 +38,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
39362
39363 AuStubInt0(au_loopback_init, void)
39364 AuStubVoid(au_loopback_fin, void)
39365+
39366+AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
39367 #endif /* BLK_DEV_LOOP */
39368
39369 #endif /* __KERNEL__ */
39370diff --git a/fs/aufs/super.c b/fs/aufs/super.c
39371index d252963a87b53..ecfc5fc96ad8c 100644
39372--- a/fs/aufs/super.c
39373+++ b/fs/aufs/super.c
39374@@ -844,7 +844,10 @@ static const struct super_operations aufs_sop = {
39375 .statfs = aufs_statfs,
39376 .put_super = aufs_put_super,
39377 .sync_fs = aufs_sync_fs,
39378- .remount_fs = aufs_remount_fs
39379+ .remount_fs = aufs_remount_fs,
39380+#ifdef CONFIG_AUFS_BDEV_LOOP
39381+ .real_loop = aufs_real_loop
39382+#endif
39383 };
39384
39385 /* ---------------------------------------------------------------------- */
39386diff --git a/include/linux/fs.h b/include/linux/fs.h
39387index 93eb43e002d97..8b392f6b36a62 100644
39388--- a/include/linux/fs.h
39389+++ b/include/linux/fs.h
39390@@ -2178,6 +2178,10 @@ struct super_operations {
39391 struct shrink_control *);
39392 long (*free_cached_objects)(struct super_block *,
39393 struct shrink_control *);
39394+#if IS_ENABLED(CONFIG_BLK_DEV_LOOP) || IS_ENABLED(CONFIG_BLK_DEV_LOOP_MODULE)
39395+ /* and aufs */
39396+ struct file *(*real_loop)(struct file *);
39397+#endif
39398 };
39399
39400 /*
This page took 0.696556 seconds and 4 git commands to generate.